mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 11:47:42 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work.
> - First-time Cloud users currently must connect an external AI
provider during onboarding before they can talk to their first agent.
> - Requiring provider setup up front adds friction before users
experience their first agent chat.
> - Paperclip needs a multi-variant onboarding experiment
(`onboarding-flow`) that defaults to `baseline` while allowing an
alternate `free_inference_1` path that skips the model-connection screen
and starts with capped starter inference.
> - When a trial-only workspace either exhausts its starter credit or
creates its first executable task or subtask, Paperclip must hold the
new task, prompt the user to connect a model provider, keep LLM actions
blocked if the modal is dismissed, and automatically resume held work
once a model is connected.
> - This pull request adds multi-variant onboarding resolution, PostHog
multi-variant attribution, dual-trigger connect-model gating with
restart-safe DB hydration, the `free_inference_1` UI onboarding flow,
and full-flow Storybooks.
> - The benefit is a safe, measurable onboarding experiment that leaves
`baseline` and self-hosted installs unchanged while letting opted-in
Cloud workspaces reach their first chat immediately and upgrade
seamlessly when real work begins.
## Linked Issues or Issue Description
**Subsystem affected**
Cross-cutting (`packages/shared/`, `server/`, `ui/`)
**Problem or motivation**
New Cloud users currently hit a mandatory "Connect a model" step during
onboarding before they can open their first agent chat. Existing
trial-inference gating only supported a binary flag and only triggered
on starter-credit exhaustion, not on subtask or manual task creation,
and did not track multi-variant onboarding cohorts cleanly in PostHog.
**Proposed solution**
Introduce the extensible `onboarding-flow` experiment (`baseline`
default, `free_inference_1` variant) across shared contracts, server
services, and UI:
- Resolve `onboardingVariant` from the Cloud control plane (`GET
/v1/tenant/onboarding-config`) with safe fallbacks to
`PAPERCLIP_ONBOARDING_VARIANT`, managed config, and `"baseline"`.
- In `free_inference_1`, skip Step 4 ("Connect a model") in
`OnboardingWizard`, hire the first agent on starter inference, and drop
the user straight into the first chat.
- Support a dual-trigger connect-model gate (`exhausted` and
`work_requires_model`) that holds newly created tasks/subtasks on
trial-only workspaces (hydrated from DB across restarts), returns HTTP
402 for blocked LLM chat/runs, keeps the modal dismissible while
blocking chat composers with a "Connect a model" CTA banner, and
automatically resumes held work after provider migration.
- Stamp `$feature/onboarding-flow`, `onboarding_variant`, and immutable
`initial_onboarding_variant` across server PostHog events,
`$groupidentify` (with top-level `$group_set_once`), and browser PostHog
super-properties / `identify()`.
**Alternatives considered**
- Baking per-stack experiment variants into warm-pool container config
was ruled out because warm-pool stacks must remain homogeneous until
claim time.
- Failing agent subtask creation calls on trial-only workspaces was
ruled out because it breaks the agent's first-chat experience; creating
the task in a held state and auto-resuming it after model connection
preserves user intent.
## What Changed
- **`packages/shared`**: Added `onboarding-flow` variant
constants/helpers, `CloudTrialGateReason` (`"exhausted" |
"work_requires_model"`), `CloudTrialHeldIssueSummary`, `gateReason`,
`llmBlocked`, `heldIssueIds`, `heldIssues`, `onboardingVariant`, and
`resumedIssueIds` on cloud-inference and session/browser-analytics
contracts.
- **`server/`**:
- Added Ed25519-signed `GET /v1/tenant/onboarding-config` resolution
with warm-pool claim cache invalidation and safe `"baseline"` default.
- Added dual-trigger trial gating (`exhausted` + `work_requires_model`)
with DB-backed held-issue hydration across restarts, HTTP 402 refusal
for blocked trial runs and chat sends, and automatic wake/resume of held
subtasks upon migration.
- Updated PostHog server telemetry, `$groupidentify` (top-level
`$group_set` and `$group_set_once`), `$feature_flag_called`, and
`BrowserAnalyticsConfig` to stamp `onboarding-flow` multi-variant
properties while preserving backwards-compatible `cloud-trial-inference`
properties.
- Added and updated Vitest suites covering multi-variant resolution,
PostHog attribution, held subtask creation, cold-restart hydration, LLM
run refusal, and auto-resume.
- **`ui/`**:
- Updated `OnboardingWizard` so `free_inference_1` skips Step 4
("Connect a model"), auto-hires with `cloudTrialInference: true`, and
launches straight into the first chat while leaving `baseline`
unchanged.
- Updated `CloudTrialGate` and `CloudTrialExhaustedModal` for
dual-trigger copy (`work_requires_model` vs `exhausted`), waiting
subtask pills, and dismissible modal behavior (`required={false}`).
- Added `CloudTrialBlockedBanner` and wired LLM-blocked composer
disabling + "Connect a model" CTA into `IssueDetail` / `AgentChat` and
`DashboardChatComposer`, plus status refresh on manual task creation in
`NewIssueDialog`.
- Updated `product-analytics.ts` + tests to register
`$feature/onboarding-flow` and `onboarding_variant` super-properties and
`$set_once.initial_onboarding_variant`.
- Added full-flow Storybooks
(`free-inference-onboarding-flow.stories.tsx`) covering all 7 onboarding
and gate states.
## Verification
- `pnpm --filter @paperclipai/shared typecheck && pnpm --filter
@paperclipai/server typecheck` (0 errors)
- `pnpm --filter @paperclipai/ui typecheck` (0 errors)
- 9 targeted server Vitest suites (`cloud-trial-inference`,
`cloud-posthog-config`, `cloud-posthog-browser`,
`posthog-direct-telemetry`, `posthog-direct-transport`, `auth-routes`,
`cloud-runtime-identity`, `onboarding-seed-route`,
`issue-onboarding-first-task-routes`): 196/196 passed
- 4 targeted UI Vitest suites (`CloudTrialGate.test.tsx`,
`product-analytics.test.ts`, `onboarding-analytics.test.tsx`,
`Layout.mobile.test.tsx`): 37/37 passed
- `pnpm --filter @paperclipai/ui build` and `pnpm --filter
@paperclipai/ui build-storybook`: passed
- Visually inspected Storybook renders across all 7 `free_inference_1`
states in headless Chromium via Playwright with zero page errors.
## Risks
- Low risk to existing users: `onboardingVariant` defaults to
`"baseline"` everywhere, leaving self-hosted installs and unflagged
Cloud stacks unchanged.
- No database schema migrations are required; held trial subtasks are
hydrated from existing `issues` and `toolConnections` tables on restart.
- Requires the companion `paperclip-cloud` control-plane endpoint (`GET
/v1/tenant/onboarding-config`) to activate non-baseline variants in
Cloud; falls back safely to `"baseline"` if unavailable.
## Model Used
- Google Gemini (Antigravity agentic coding environment with multi-agent
swarm, tool use, code execution, and Playwright Storybook verification)
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
---------
Co-authored-by: Paperclip <noreply@paperclip.ing>
190 lines
8.3 KiB
TypeScript
190 lines
8.3 KiB
TypeScript
import {
|
|
DEFAULT_ONBOARDING_VARIANT,
|
|
FREE_INFERENCE_ONBOARDING_VARIANT,
|
|
ONBOARDING_FLOW_FLAG_KEY,
|
|
isFreeInferenceOnboardingVariant,
|
|
normalizeOnboardingVariant,
|
|
type OnboardingVariant,
|
|
} from "@paperclipai/shared";
|
|
import { isCloudManagedInstance, type CloudInstanceEnv } from "./cloud-instance.js";
|
|
import {
|
|
getResolvedCloudTrialInferenceEnabled,
|
|
getResolvedOnboardingVariant,
|
|
} from "./cloud-posthog-cohort.js";
|
|
|
|
/**
|
|
* PostHog experiment / feature flag key for the Cloud starter-inference A/B test
|
|
* (legacy binary flag kept for backwards compatibility alongside `onboarding-flow`).
|
|
*/
|
|
export const CLOUD_TRIAL_INFERENCE_FLAG_KEY = "cloud-trial-inference";
|
|
export type CloudTrialInferenceCohort = "treatment" | "control";
|
|
|
|
export { ONBOARDING_FLOW_FLAG_KEY, DEFAULT_ONBOARDING_VARIANT, FREE_INFERENCE_ONBOARDING_VARIANT };
|
|
export type { OnboardingVariant };
|
|
|
|
export function cloudTrialInferenceCohort(enabled: boolean): CloudTrialInferenceCohort {
|
|
return enabled ? "treatment" : "control";
|
|
}
|
|
|
|
function parseManagedConfigObject(env: CloudInstanceEnv): Record<string, unknown> | null {
|
|
const raw = env.PAPERCLIP_MANAGED_CONFIG?.trim();
|
|
if (!raw) return null;
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
? (parsed as Record<string, unknown>)
|
|
: null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolves the synchronous onboarding experiment variant from process state,
|
|
* `PAPERCLIP_ONBOARDING_VARIANT`, `PAPERCLIP_MANAGED_CONFIG`, or legacy
|
|
* `enableCloudTrialInference`. Defaults safely to `"baseline"` so self-hosted
|
|
* and unflagged Cloud stacks never enter an experimental onboarding flow.
|
|
*/
|
|
export function resolveOnboardingVariant(env: CloudInstanceEnv = process.env): OnboardingVariant {
|
|
if (env === process.env) {
|
|
const resolved = getResolvedOnboardingVariant();
|
|
if (resolved !== null) return resolved;
|
|
}
|
|
const envVariant = env.PAPERCLIP_ONBOARDING_VARIANT?.trim();
|
|
if (envVariant) return normalizeOnboardingVariant(envVariant);
|
|
|
|
const parsed = parseManagedConfigObject(env);
|
|
const features =
|
|
parsed?.features && typeof parsed.features === "object" && !Array.isArray(parsed.features)
|
|
? (parsed.features as Record<string, unknown>)
|
|
: null;
|
|
if (typeof parsed?.onboardingVariant === "string" && parsed.onboardingVariant.trim()) {
|
|
return normalizeOnboardingVariant(parsed.onboardingVariant);
|
|
}
|
|
if (typeof features?.onboardingVariant === "string" && features.onboardingVariant.trim()) {
|
|
return normalizeOnboardingVariant(features.onboardingVariant);
|
|
}
|
|
if (features?.enableCloudTrialInference === true) {
|
|
return FREE_INFERENCE_ONBOARDING_VARIANT;
|
|
}
|
|
if (env === process.env && getResolvedCloudTrialInferenceEnabled() === true) {
|
|
return FREE_INFERENCE_ONBOARDING_VARIANT;
|
|
}
|
|
return DEFAULT_ONBOARDING_VARIANT;
|
|
}
|
|
|
|
/**
|
|
* Resolves whether `enableCloudTrialInference` is active in this stack's
|
|
* resolved state, `PAPERCLIP_ONBOARDING_VARIANT`, or `PAPERCLIP_MANAGED_CONFIG`
|
|
* document. Fails closed (`false`) if absent or malformed so analytics never
|
|
* throw on startup.
|
|
*/
|
|
export function resolveCloudTrialInferenceEnabled(env: CloudInstanceEnv = process.env): boolean {
|
|
if (env === process.env) {
|
|
const resolved = getResolvedCloudTrialInferenceEnabled();
|
|
if (resolved !== null) return resolved;
|
|
const resolvedVariant = getResolvedOnboardingVariant();
|
|
if (resolvedVariant !== null) return isFreeInferenceOnboardingVariant(resolvedVariant);
|
|
}
|
|
const envVariant = env.PAPERCLIP_ONBOARDING_VARIANT?.trim();
|
|
if (envVariant) return isFreeInferenceOnboardingVariant(envVariant);
|
|
|
|
const parsed = parseManagedConfigObject(env);
|
|
const features =
|
|
parsed?.features && typeof parsed.features === "object" && !Array.isArray(parsed.features)
|
|
? (parsed.features as Record<string, unknown>)
|
|
: null;
|
|
if (features?.enableCloudTrialInference === true) return true;
|
|
if (typeof parsed?.onboardingVariant === "string" && parsed.onboardingVariant.trim()) {
|
|
return isFreeInferenceOnboardingVariant(parsed.onboardingVariant);
|
|
}
|
|
if (typeof features?.onboardingVariant === "string" && features.onboardingVariant.trim()) {
|
|
return isFreeInferenceOnboardingVariant(features.onboardingVariant);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export function isOnboardingVariantExplicitlyResolved(env: CloudInstanceEnv = process.env): boolean {
|
|
if (env === process.env && getResolvedOnboardingVariant() !== null) return true;
|
|
if (env.PAPERCLIP_ONBOARDING_VARIANT?.trim()) return true;
|
|
// On Cloud-managed stacks (including unclaimed warm-pool instances before
|
|
// runtime identity/signing keys are attached), defer locking
|
|
// `$group_set_once.initial_onboarding_variant` until the Cloud assignment
|
|
// actually resolves via `/v1/tenant/onboarding-config` or exposure sync.
|
|
if (isCloudManagedInstance(env)) return false;
|
|
const parsed = parseManagedConfigObject(env);
|
|
const features =
|
|
parsed?.features && typeof parsed.features === "object" && !Array.isArray(parsed.features)
|
|
? (parsed.features as Record<string, unknown>)
|
|
: null;
|
|
if (typeof parsed?.onboardingVariant === "string" && parsed.onboardingVariant.trim()) return true;
|
|
if (typeof features?.onboardingVariant === "string" && features.onboardingVariant.trim()) return true;
|
|
if (features?.enableCloudTrialInference === true) return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* PostHog settings for a Paperclip Cloud stack. Present only on a
|
|
* Cloud-managed instance that has a project token and a stack id; every
|
|
* self-hosted install resolves `null` and keeps the collector path.
|
|
*/
|
|
export type CloudPostHogConfig = {
|
|
token: string; // PAPERCLIP_POSTHOG_PROJECT_TOKEN
|
|
host: string; // PAPERCLIP_POSTHOG_HOST, default "https://us.i.posthog.com"
|
|
environment: string | null; // PAPERCLIP_POSTHOG_ENVIRONMENT
|
|
stackId: string; // PAPERCLIP_CLOUD_STACK_ID
|
|
directEnabled: boolean; // PAPERCLIP_POSTHOG_DIRECT !== "0"
|
|
browserEnabled: boolean; // PAPERCLIP_POSTHOG_BROWSER_ENABLED === "1"
|
|
replayEnabled: boolean; // browserEnabled && PAPERCLIP_POSTHOG_REPLAY_ENABLED !== "0"
|
|
replayWorkContentEnabled: boolean;
|
|
cloudTrialInferenceEnabled: boolean; // PAPERCLIP_MANAGED_CONFIG.features.enableCloudTrialInference
|
|
onboardingVariant?: OnboardingVariant; // "baseline", "free_inference_1", etc.
|
|
/** True only when the variant came from an explicit source (Cloud config, env, or managed config), not an unresolved warm-pool boot default. */
|
|
onboardingVariantResolved?: boolean;
|
|
};
|
|
|
|
export const DEFAULT_CLOUD_POSTHOG_HOST = "https://us.i.posthog.com";
|
|
|
|
function readEnv(env: CloudInstanceEnv, name: string): string | null {
|
|
const value = env[name]?.trim();
|
|
return value ? value : null;
|
|
}
|
|
|
|
export function resolveCloudPostHogConfig(
|
|
env: Record<string, string | undefined> = process.env,
|
|
): CloudPostHogConfig | null {
|
|
if (!isCloudManagedInstance(env)) return null;
|
|
const token = readEnv(env, "PAPERCLIP_POSTHOG_PROJECT_TOKEN");
|
|
const stackId = readEnv(env, "PAPERCLIP_CLOUD_STACK_ID");
|
|
if (!token || !stackId) return null;
|
|
|
|
const browserEnabled = readEnv(env, "PAPERCLIP_POSTHOG_BROWSER_ENABLED") === "1";
|
|
const replayEnabled = browserEnabled && readEnv(env, "PAPERCLIP_POSTHOG_REPLAY_ENABLED") !== "0";
|
|
const onboardingVariant = resolveOnboardingVariant(env);
|
|
const onboardingVariantResolved = isOnboardingVariantExplicitlyResolved(env);
|
|
return {
|
|
token,
|
|
host: (readEnv(env, "PAPERCLIP_POSTHOG_HOST") ?? DEFAULT_CLOUD_POSTHOG_HOST).replace(/\/+$/, ""),
|
|
environment: readEnv(env, "PAPERCLIP_POSTHOG_ENVIRONMENT"),
|
|
stackId,
|
|
directEnabled: readEnv(env, "PAPERCLIP_POSTHOG_DIRECT") !== "0",
|
|
browserEnabled,
|
|
replayEnabled,
|
|
replayWorkContentEnabled: replayEnabled && readEnv(env, "PAPERCLIP_POSTHOG_REPLAY_WORK_CONTENT_ENABLED") === "1",
|
|
cloudTrialInferenceEnabled: resolveCloudTrialInferenceEnabled(env),
|
|
onboardingVariant,
|
|
...(onboardingVariantResolved ? { onboardingVariantResolved: true } : {}),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Whether server events go straight to PostHog. Requires the kill switch to
|
|
* be on and an environment: every direct event carries `environment`, so
|
|
* without one the stack keeps using the collector.
|
|
*/
|
|
export function isPostHogDirectDeliveryEnabled(
|
|
config: CloudPostHogConfig | null,
|
|
): config is CloudPostHogConfig & { environment: string } {
|
|
return Boolean(config?.directEnabled && config.environment);
|
|
}
|