mirror of
https://github.com/stablyai/orca.git
synced 2026-10-26 00:03:55 +00:00
* feat(telemetry): instrument on_path:false triage on onboarding_agent_picked Adds path_source and path_failure_reason to onboarding_agent_picked so the ~30% on_path:false rate on dashboard 1562016 can be split between shell hydration failures and genuinely-not-on-PATH cases before picking a fix. See docs/agent-on-path-detection.md. Co-authored-by: Orca <help@stably.ai> * fix(telemetry): close PathSource compile-time-sync hole Add `_PathSourceSync` guard mirroring `_PathFailureReasonSync` so adding a new `PathSource` value to the alias without updating the schema (or vice versa) fails the build. Without it, drift would silently drop `onboarding_agent_picked` at the strict validator. Also replace stale line-number references in docs/agent-on-path-detection.md with named function/handler references that survive future edits. Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
688 lines
29 KiB
TypeScript
688 lines
29 KiB
TypeScript
/* eslint-disable max-lines -- Why: this is the single source of truth for every telemetry event schema, enum, and the cohort-injection set predicates. Splitting it would scatter the .strict() / Zod-first doctrine across files and break the EventMap derivation that makes adding an event a one-line change. */
|
|
// Single source of truth for telemetry event names, schemas, and enums.
|
|
//
|
|
// Zod-first: every event schema is declared once and the compile-time
|
|
// `EventMap` is `z.infer`-derived from the same record the runtime validator
|
|
// consumes. There is no parallel `EVENT_SPEC` / hand-rolled union to drift
|
|
// out of sync with. Adding an event means adding a schema to `eventSchemas`;
|
|
// `EventMap` picks it up automatically and call sites that reference an
|
|
// unknown event name fail `tsc`.
|
|
//
|
|
// `.strict()` on every object schema is the runtime counterpart to "no extra
|
|
// keys." Free-form string fields carry an explicit `.max(N)` cap at the
|
|
// schema — the cap and the schema are the same thing; the validator does not
|
|
// re-check string length.
|
|
|
|
import { z } from 'zod'
|
|
|
|
import { AGENT_HOOK_TARGETS } from './agent-hook-types'
|
|
import { ONBOARDING_FINAL_STEP } from './constants'
|
|
import type {
|
|
DiscoveryStatusEmitted,
|
|
GlobalSettings,
|
|
OnboardingChecklistState,
|
|
PathSource,
|
|
ShellHydrationFailureReason
|
|
} from './types'
|
|
|
|
// ── Shared property enums ───────────────────────────────────────────────
|
|
|
|
// Mirrors the shipped `TuiAgent` launch surface, with one deliberate shift:
|
|
// `claude` in settings/launch state ↔ `claude-code` here (product, not CLI
|
|
// string) so dashboards read cleanly.
|
|
//
|
|
// `other` remains as a telemetry escape hatch, but project-owned TuiAgents
|
|
// should map to concrete values; see `tuiAgentToAgentKind`.
|
|
export const AGENT_KIND_VALUES = [
|
|
'claude-code',
|
|
'codex',
|
|
'autohand',
|
|
'opencode',
|
|
'pi',
|
|
'gemini',
|
|
'aider',
|
|
'goose',
|
|
'amp',
|
|
'kilo',
|
|
'kiro',
|
|
'crush',
|
|
'aug',
|
|
'cline',
|
|
'codebuff',
|
|
'continue',
|
|
'cursor',
|
|
'droid',
|
|
'kimi',
|
|
'mistral-vibe',
|
|
'qwen-code',
|
|
'rovo',
|
|
'hermes',
|
|
'copilot',
|
|
'other'
|
|
] as const
|
|
export const agentKindSchema = z.enum(AGENT_KIND_VALUES)
|
|
export type AgentKind = z.infer<typeof agentKindSchema>
|
|
|
|
// Trimmed to a small set of values Orca's PTY-typed-command launch architecture
|
|
// can emit:
|
|
// - `binary_not_found` — `provider.spawn` ENOENT (the *shell* binary is
|
|
// missing). The agent CLI being missing is invisible: Orca spawns a
|
|
// healthy shell and types the command, and bash/zsh's "command not found"
|
|
// surfaces only as terminal output.
|
|
// - `paste_readiness_timeout` — bracketed-paste readiness wait timed out.
|
|
// The agent process spawned but its TUI input box didn't reach a ready
|
|
// state before the watchdog deadline, so the queued draft was dropped.
|
|
// - `unknown` — every other thrown error (env-build failures,
|
|
// unclassifiable shell-spawn errors).
|
|
// Provider-side errors (`auth_expired`, `rate_limited`, `network_timeout`,
|
|
// `provider_*`) happen inside the agent CLI subprocess and are not observable
|
|
// to Orca — see telemetry-plan.md §Decision: Defer per-incident error fields.
|
|
// Adding a new value is additive-safe; do it when the call site lands, not in
|
|
// anticipation.
|
|
export const errorClassSchema = z.enum(['binary_not_found', 'paste_readiness_timeout', 'unknown'])
|
|
export type ErrorClass = z.infer<typeof errorClassSchema>
|
|
|
|
export const repoMethodSchema = z.enum(['folder_picker', 'clone_url', 'drag_drop'])
|
|
export type RepoMethod = z.infer<typeof repoMethodSchema>
|
|
|
|
// Five Setup-step affordances the user can pick after `repo_added` fires (see
|
|
// AddRepoSetupStep). One enum because every value lives on the same screen and
|
|
// the funnel question is "which one did they pick" — adding a sixth value
|
|
// later is additive-safe per the schema-evolution doctrine below.
|
|
export const addRepoSetupStepActionSchema = z.enum([
|
|
'create_worktree',
|
|
'configure',
|
|
'skip',
|
|
'open_existing',
|
|
'back'
|
|
])
|
|
export type AddRepoSetupStepAction = z.infer<typeof addRepoSetupStepActionSchema>
|
|
|
|
// Deliberately a separate enum from `errorClassSchema` (PTY-spawn taxonomy):
|
|
// different domain — this one buckets git/filesystem failures thrown by
|
|
// `createLocalWorktree` / `createRemoteWorktree`. Merging the two would lock
|
|
// both domains to the union forever, which the schema-evolution comment
|
|
// below warns against.
|
|
export const workspaceCreateErrorClassSchema = z.enum([
|
|
'git_failed',
|
|
'path_collision',
|
|
'permission_denied',
|
|
'base_ref_missing',
|
|
'unknown'
|
|
])
|
|
export type WorkspaceCreateErrorClass = z.infer<typeof workspaceCreateErrorClassSchema>
|
|
|
|
export const workspaceSourceSchema = z.enum([
|
|
'command_palette',
|
|
'sidebar',
|
|
'shortcut',
|
|
'drag_drop',
|
|
'onboarding',
|
|
'unknown'
|
|
])
|
|
export type WorkspaceSource = z.infer<typeof workspaceSourceSchema>
|
|
|
|
export const launchSourceSchema = z.enum([
|
|
'command_palette',
|
|
'sidebar',
|
|
'tab_bar_quick_launch',
|
|
'task_page',
|
|
'new_workspace_composer',
|
|
'workspace_jump_palette',
|
|
'shortcut',
|
|
'onboarding',
|
|
'diff_notes_send',
|
|
'unknown'
|
|
])
|
|
export type LaunchSource = z.infer<typeof launchSourceSchema>
|
|
|
|
export const requestKindSchema = z.enum(['new', 'resume', 'followup'])
|
|
export type RequestKind = z.infer<typeof requestKindSchema>
|
|
|
|
// `env_var` is deliberately absent — env-var and CI paths override consent at
|
|
// runtime only (see consent.ts); they never mutate `optedIn` and therefore
|
|
// never fire a `telemetry_opted_in/out` event. If a future path explicitly
|
|
// persists an env-var-driven opt-out, add `env_var` back here together with
|
|
// the call site.
|
|
//
|
|
// `first_launch_notice` (new-user disclosure toast) is deliberately absent —
|
|
// the new-user cohort has no first-launch surface (see telemetry-plan.md
|
|
// §First-launch experience). Opt-outs from new users come through
|
|
// `via: 'settings'`.
|
|
export const optInViaSchema = z.enum(['first_launch_banner', 'settings'])
|
|
export type OptInVia = z.infer<typeof optInViaSchema>
|
|
|
|
// Whitelist of settings whose `setting_key` may be emitted on
|
|
// `settings_changed`. If a setting isn't in this list, we do not emit.
|
|
//
|
|
// Keys are camelCase to match the actual field names in `GlobalSettings`.
|
|
// `orca_channel` is intentionally absent — it is a build-time common
|
|
// property baked in from `ORCA_BUILD_IDENTITY`, not a user-togglable setting.
|
|
//
|
|
// Intentionally does NOT include the telemetry opt-in toggle — that is
|
|
// covered by the dedicated `telemetry_opted_in` / `telemetry_opted_out`
|
|
// events, which carry `via` context that a plain `settings_changed` could
|
|
// not. Listing it here would double-fire.
|
|
//
|
|
// Kept as an `as const` tuple so the Zod enum below and any call-site usage
|
|
// share one array — typo-drift is impossible.
|
|
type BooleanGlobalSettingsKey = {
|
|
[Key in keyof GlobalSettings]-?: GlobalSettings[Key] extends boolean ? Key : never
|
|
}[keyof GlobalSettings]
|
|
export const SETTINGS_CHANGED_WHITELIST = [
|
|
'editorAutoSave',
|
|
'openLinksInApp',
|
|
'experimentalMobile',
|
|
'experimentalPet',
|
|
'experimentalWorktreeSymlinks',
|
|
'geminiCliOAuthEnabled'
|
|
] as const satisfies readonly BooleanGlobalSettingsKey[]
|
|
export const settingsChangedKeySchema = z.enum(SETTINGS_CHANGED_WHITELIST)
|
|
export type SettingsChangedKey = z.infer<typeof settingsChangedKeySchema>
|
|
|
|
// ── Per-event schemas ───────────────────────────────────────────────────
|
|
//
|
|
// `.strict()` on every object is what enforces "no extra keys" at runtime —
|
|
// the validator does not need a separate extra-key check because zod rejects
|
|
// unknown keys at parse time. This is the runtime counterpart to the
|
|
// compile-time "unions of string literals, no raw `string`" rule.
|
|
|
|
// Cohort signal — see docs/onboarding-funnel-cohort-addendum.md. One integer
|
|
// shared across the events listed in `COHORT_EXTENDED` below: the count of
|
|
// repos the user has at emit time, read from `store.getRepos().length`.
|
|
// `.int().nonnegative()` constrains malformed values to the floor;
|
|
// `.optional()` lets the classifier's fail-soft fallback (returning
|
|
// `undefined`) validate cleanly so a read error never crashes a track call.
|
|
const nthRepoAddedSchema = z.number().int().nonnegative().optional()
|
|
|
|
const appOpenedSchema = z.object({ nth_repo_added: nthRepoAddedSchema }).strict()
|
|
|
|
const repoAddedSchema = z
|
|
.object({ method: repoMethodSchema, nth_repo_added: nthRepoAddedSchema })
|
|
.strict()
|
|
|
|
const workspaceCreatedSchema = z
|
|
.object({
|
|
source: workspaceSourceSchema,
|
|
from_existing_branch: z.boolean(),
|
|
nth_repo_added: nthRepoAddedSchema
|
|
})
|
|
.strict()
|
|
|
|
const agentStartedSchema = z
|
|
.object({
|
|
agent_kind: agentKindSchema,
|
|
launch_source: launchSourceSchema,
|
|
request_kind: requestKindSchema,
|
|
nth_repo_added: nthRepoAddedSchema
|
|
})
|
|
.strict()
|
|
|
|
// Enum-only by design for both fields. `error_message` and `error_stack` are
|
|
// deliberately absent — `.strict()` rejects either key if a call site ever
|
|
// tries to attach one, which fails the validator and drops the event. Raw
|
|
// error strings carry arbitrary user/workspace/path content; keeping them off
|
|
// the wire is the only way to guarantee we never transmit them by accident.
|
|
const agentErrorSchema = z
|
|
.object({
|
|
error_class: errorClassSchema,
|
|
agent_kind: agentKindSchema,
|
|
nth_repo_added: nthRepoAddedSchema
|
|
})
|
|
.strict()
|
|
|
|
const settingsChangedSchema = z
|
|
.object({
|
|
setting_key: settingsChangedKeySchema,
|
|
value_kind: z.enum(['bool', 'enum'])
|
|
})
|
|
.strict()
|
|
|
|
const telemetryOptedInSchema = z.object({ via: optInViaSchema }).strict()
|
|
const telemetryOptedOutSchema = z.object({ via: optInViaSchema }).strict()
|
|
|
|
const addRepoSetupStepActionEventSchema = z
|
|
.object({ action: addRepoSetupStepActionSchema, nth_repo_added: nthRepoAddedSchema })
|
|
.strict()
|
|
|
|
// Why: same enum-only discipline as `agent_error` — `.strict()` rejects raw
|
|
// error strings if a future call site tries to attach `error_message` /
|
|
// `error_stack`. The classifier in worktrees.ts reads `error.message` to
|
|
// bucket into the enum, but those strings never cross the wire.
|
|
const workspaceCreateFailedSchema = z
|
|
.object({
|
|
source: workspaceSourceSchema,
|
|
error_class: workspaceCreateErrorClassSchema,
|
|
nth_repo_added: nthRepoAddedSchema
|
|
})
|
|
.strict()
|
|
|
|
// Managed-hook installer per-agent label. Distinct from `AGENT_KIND_VALUES`:
|
|
// hook installation only targets these four agents and the labels here match
|
|
// the `*HookService.install()` call sites in `src/main/index.ts`. `claude`
|
|
// (not `claude-code`) is intentional — the failure is about Claude Code's
|
|
// `~/.claude/settings.json`, not the broader product taxonomy. Sourced from
|
|
// `AGENT_HOOK_TARGETS` so the wire enum and the IPC `AgentHookTarget` type
|
|
// cannot drift if a fifth hook-install agent is added.
|
|
export const hookInstallAgentSchema = z.enum(AGENT_HOOK_TARGETS)
|
|
export type HookInstallAgent = z.infer<typeof hookInstallAgentSchema>
|
|
|
|
// Why: install failures are config-file-shape errors (malformed JSON, missing
|
|
// keys, ACL denials on `~/.claude` etc.) — not user content. The 200-char
|
|
// cap is the truncation contract; callers must truncate before calling
|
|
// `track`, and the validator will drop overlength strings via `.max(200)`.
|
|
const agentHookInstallFailedSchema = z
|
|
.object({
|
|
agent: hookInstallAgentSchema,
|
|
error_message: z.string().max(200)
|
|
})
|
|
.strict()
|
|
|
|
// ── Onboarding ──────────────────────────────────────────────────────────
|
|
//
|
|
// Closed enums only — no raw paths, repo names, clone URLs, or error
|
|
// strings. The funnel exists to measure activation, not to debug specific
|
|
// user repos.
|
|
// Why: bound is derived from ONBOARDING_FINAL_STEP so adding a wizard step
|
|
// only requires bumping the constant. Zod can't build a literal-union from a
|
|
// numeric constant without runtime gymnastics, so we use a clamped int range.
|
|
const onboardingStepSchema = z.number().int().min(1).max(ONBOARDING_FINAL_STEP)
|
|
const onboardingPathSchema = z.enum(['open_folder', 'clone_url'])
|
|
const onboardingFailureReasonSchema = z.enum([
|
|
'invalid_path',
|
|
'clone_failed',
|
|
'cancelled',
|
|
'unknown'
|
|
])
|
|
const onboardingValueKindSchema = z.enum(['agent', 'theme', 'notifications', 'repo'])
|
|
// `dismissed` from `OnboardingChecklistState` is intentionally excluded —
|
|
// it is a UI panel-visibility flag, not an activation event, so it never
|
|
// fires `activation_checklist_item_completed`. Keep this list in sync with
|
|
// the activation keys of `OnboardingChecklistState` in shared/types.ts.
|
|
const onboardingChecklistItemSchema = z.enum([
|
|
'addedRepo',
|
|
'addedFolder',
|
|
'choseAgent',
|
|
'ranFirstAgent',
|
|
'ranSecondAgentOnSameTask',
|
|
'triedCmdJ',
|
|
'shapedSidebar',
|
|
'reviewedDiff',
|
|
'openedPr',
|
|
'openedFile',
|
|
'ranAgentOnFile'
|
|
])
|
|
|
|
// Why: compile-time guard that the enum above stays in lockstep with the
|
|
// activation keys of OnboardingChecklistState (everything except the UI-only
|
|
// `dismissed` flag). Adding/removing a checklist key without updating this
|
|
// schema breaks the build here rather than silently dropping telemetry.
|
|
type _OnboardingChecklistItemSync =
|
|
z.infer<typeof onboardingChecklistItemSchema> extends Exclude<
|
|
keyof OnboardingChecklistState,
|
|
'dismissed'
|
|
>
|
|
? Exclude<keyof OnboardingChecklistState, 'dismissed'> extends z.infer<
|
|
typeof onboardingChecklistItemSchema
|
|
>
|
|
? true
|
|
: never
|
|
: never
|
|
const _onboardingChecklistItemSyncCheck: _OnboardingChecklistItemSync = true
|
|
void _onboardingChecklistItemSyncCheck
|
|
|
|
// Cohort discriminator threaded onto every onboarding-wizard event by the
|
|
// IPC `telemetry:track` handler (mirrors `nth_repo_added`). `.optional()` is
|
|
// load-bearing: the classifier returns `undefined` when settings can't be
|
|
// read, and `.strict()` would otherwise reject the event entirely.
|
|
//
|
|
// Adding a new onboarding event: include `cohort: cohortSchema` on its
|
|
// schema. The injection set in `telemetry:track` is derived from
|
|
// `'cohort' in schema.shape`, so there is no parallel hand-maintained list.
|
|
const cohortSchema = z.enum(['fresh_install', 'upgrade_backfill']).optional()
|
|
|
|
// `'button' | 'keyboard'` records whether the user advanced via a footer
|
|
// button click or via Cmd/Ctrl+Enter. Skip and dismiss don't have a keyboard
|
|
// path today (the field will only ever be `'button'` for those events) but
|
|
// the uniform shape lets a future keyboard skip arrive without a schema
|
|
// migration.
|
|
const advancedViaSchema = z.enum(['button', 'keyboard']).optional()
|
|
|
|
const onboardingStartedSchema = z
|
|
.object({ resumed_from_step: onboardingStepSchema.optional(), cohort: cohortSchema })
|
|
.strict()
|
|
const onboardingStepViewedSchema = z
|
|
.object({ step: onboardingStepSchema, cohort: cohortSchema })
|
|
.strict()
|
|
const onboardingStepCompletedSchema = z
|
|
.object({
|
|
step: onboardingStepSchema,
|
|
value_kind: onboardingValueKindSchema,
|
|
duration_ms: z.number().int().nonnegative().optional(),
|
|
advanced_via: advancedViaSchema,
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const onboardingStepSkippedSchema = z
|
|
.object({
|
|
step: onboardingStepSchema,
|
|
duration_ms: z.number().int().nonnegative().optional(),
|
|
advanced_via: advancedViaSchema,
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const onboardingStep4PathClickedSchema = z
|
|
.object({ path: onboardingPathSchema, cohort: cohortSchema })
|
|
.strict()
|
|
const onboardingStep4PathFailedSchema = z
|
|
.object({
|
|
path: onboardingPathSchema,
|
|
reason: onboardingFailureReasonSchema,
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const onboardingCompletedSchema = z
|
|
.object({
|
|
path: onboardingPathSchema,
|
|
is_git_repo: z.boolean(),
|
|
total_duration_ms: z.number().int().nonnegative(),
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const onboardingDismissedSchema = z
|
|
.object({
|
|
last_step: onboardingStepSchema,
|
|
duration_ms: z.number().int().nonnegative().optional(),
|
|
advanced_via: advancedViaSchema,
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const activationChecklistItemCompletedSchema = z
|
|
.object({
|
|
item: onboardingChecklistItemSchema,
|
|
time_since_completed_ms: z.number().int().nonnegative()
|
|
})
|
|
.strict()
|
|
|
|
// Why: see docs/agent-on-path-detection.md. Disambiguates `on_path: false`
|
|
// rows on dashboard 1562016 — distinguishes shell-hydration failure (where
|
|
// `on_path` is misleading because Orca's view of PATH is incomplete) from
|
|
// genuinely-not-on-PATH (where the field is reporting accurately). Closed
|
|
// enum kept in lockstep with `ShellHydrationFailureReason` via a compile-time
|
|
// guard below.
|
|
const pathSourceSchema = z.enum(['shell_hydrate', 'sync_seed_only'])
|
|
const pathFailureReasonSchema = z.enum(['none', 'no_shell', 'timeout', 'spawn_error', 'empty_path'])
|
|
|
|
// Compile-time guard: schema enum must match `ShellHydrationFailureReason`.
|
|
// Adding a new failure mode in `hydrate-shell-path.ts` without updating both
|
|
// the shared alias and this schema breaks the build here. Without the guard,
|
|
// a new enum value would ship `failureReason` strings the strict validator
|
|
// rejects, dropping the entire `onboarding_agent_picked` event at parse time
|
|
// and losing the `agent_kind`/`on_path` data on that pick.
|
|
type _PathFailureReasonSync =
|
|
z.infer<typeof pathFailureReasonSchema> extends ShellHydrationFailureReason
|
|
? ShellHydrationFailureReason extends z.infer<typeof pathFailureReasonSchema>
|
|
? true
|
|
: never
|
|
: never
|
|
const _pathFailureReasonSyncCheck: _PathFailureReasonSync = true
|
|
void _pathFailureReasonSyncCheck
|
|
|
|
type _PathSourceSync =
|
|
z.infer<typeof pathSourceSchema> extends PathSource
|
|
? PathSource extends z.infer<typeof pathSourceSchema>
|
|
? true
|
|
: never
|
|
: never
|
|
const _pathSourceSyncCheck: _PathSourceSync = true
|
|
void _pathSourceSyncCheck
|
|
|
|
// Fired at click time from `setSelectedAgentInteractive` so we capture
|
|
// mind-changes within the step rather than just the final pick. `agent_kind`
|
|
// uses `tuiAgentToAgentKind` so the wire enum stays closed even when stale
|
|
// persisted settings present a string outside `TuiAgent` (the fallback is
|
|
// `'other'`).
|
|
const onboardingAgentPickedSchema = z
|
|
.object({
|
|
agent_kind: agentKindSchema,
|
|
on_path: z.boolean(),
|
|
detected_count: z.number().int().nonnegative(),
|
|
// `'pending'` when the merged isDetectingAgents/isRefreshingAgents flag
|
|
// is truthy at click time — distinguishes "picked the only detected
|
|
// agent" from "picked before detection finished."
|
|
detection_state: z.enum(['complete', 'pending']),
|
|
// `true` when the selected agent lived under the `<details>` disclosure
|
|
// ("Show N more"). Signals whether users go looking for less-popular
|
|
// agents — input for catalog ordering decisions.
|
|
from_collapsed_section: z.boolean(),
|
|
// Why: instrumentation for the `on_path:false` triage. `.optional()` is
|
|
// load-bearing — events emitted before this deploy validate cleanly under
|
|
// `.strict()`. See docs/agent-on-path-detection.md.
|
|
path_source: pathSourceSchema.optional(),
|
|
path_failure_reason: pathFailureReasonSchema.optional(),
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
|
|
// Mirrors the renderer's DiscoveryState taxonomy in ThemeStep.tsx. `failed`
|
|
// is intentionally NOT a discovery state — it is the outcome of an Import
|
|
// attempt, reported by `onboarding_ghostty_import_failed`.
|
|
const ghosttyDiscoveryStateSchema = z.enum(['found', 'absent', 'imported'])
|
|
|
|
// Compile-time guard: every member of ghosttyDiscoveryStateSchema must be a
|
|
// discovery `status` the renderer can actually emit. Adding a new
|
|
// DiscoveryState member in ThemeStep.tsx without updating the schema (or
|
|
// vice versa) breaks the build here rather than silently dropping telemetry.
|
|
type _GhosttyDiscoveryStateSync =
|
|
z.infer<typeof ghosttyDiscoveryStateSchema> extends DiscoveryStatusEmitted
|
|
? DiscoveryStatusEmitted extends z.infer<typeof ghosttyDiscoveryStateSchema>
|
|
? true
|
|
: never
|
|
: never
|
|
const _ghosttyDiscoveryStateSyncCheck: _GhosttyDiscoveryStateSync = true
|
|
void _ghosttyDiscoveryStateSyncCheck
|
|
|
|
const onboardingGhosttyDiscoveredSchema = z
|
|
.object({
|
|
state: ghosttyDiscoveryStateSchema,
|
|
// Bucketed, not raw, count: exact group counts are an environment
|
|
// fingerprint (heavy customizers are uniquely identifiable). Buckets
|
|
// cover the nine possible group labels in `humanFields()` without
|
|
// re-emitting the count itself.
|
|
field_group_count_bucket: z.enum(['0', '1-3', '4-7', '8+']),
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
const onboardingGhosttyImportClickedSchema = z.object({ cohort: cohortSchema }).strict()
|
|
const onboardingGhosttyImportFailedSchema = z
|
|
.object({
|
|
// `'no_config'` is reserved for a future explicit "preview returned
|
|
// found:false" branch. Today's call sites emit `'empty_diff'` (the
|
|
// import resolved to no changes) or `'unknown'` (caught throw).
|
|
reason: z.enum(['no_config', 'empty_diff', 'unknown']),
|
|
cohort: cohortSchema
|
|
})
|
|
.strict()
|
|
|
|
// ── Event registry: the one record the validator consumes ───────────────
|
|
//
|
|
// The validator does `eventSchemas[name].safeParse(props)`. `EventMap` is
|
|
// `z.infer`-derived from this record, so there is exactly one source of
|
|
// truth for both compile-time types and runtime validation.
|
|
//
|
|
// Schema-evolution / versioning doctrine:
|
|
// Breaking changes (renaming a field, changing an enum's meaning, removing a
|
|
// required key) require a new event name (e.g. `agent_started_v2`), not an
|
|
// in-place edit. Additive-optional fields (`z.field().optional()`) are safe
|
|
// to add in place. This keeps PostHog funnels clean — an in-place breaking
|
|
// change silently blends pre- and post-change rows under one event name,
|
|
// which cannot be unmixed after the fact.
|
|
export const eventSchemas = {
|
|
app_opened: appOpenedSchema,
|
|
|
|
repo_added: repoAddedSchema,
|
|
add_repo_setup_step_action: addRepoSetupStepActionEventSchema,
|
|
workspace_created: workspaceCreatedSchema,
|
|
workspace_create_failed: workspaceCreateFailedSchema,
|
|
|
|
agent_started: agentStartedSchema,
|
|
agent_error: agentErrorSchema,
|
|
agent_hook_install_failed: agentHookInstallFailedSchema,
|
|
|
|
settings_changed: settingsChangedSchema,
|
|
|
|
telemetry_opted_in: telemetryOptedInSchema,
|
|
telemetry_opted_out: telemetryOptedOutSchema,
|
|
|
|
onboarding_started: onboardingStartedSchema,
|
|
onboarding_step_viewed: onboardingStepViewedSchema,
|
|
onboarding_step_completed: onboardingStepCompletedSchema,
|
|
onboarding_step_skipped: onboardingStepSkippedSchema,
|
|
onboarding_step4_path_clicked: onboardingStep4PathClickedSchema,
|
|
onboarding_step4_path_failed: onboardingStep4PathFailedSchema,
|
|
onboarding_completed: onboardingCompletedSchema,
|
|
onboarding_dismissed: onboardingDismissedSchema,
|
|
onboarding_agent_picked: onboardingAgentPickedSchema,
|
|
onboarding_ghostty_discovered: onboardingGhosttyDiscoveredSchema,
|
|
onboarding_ghostty_import_clicked: onboardingGhosttyImportClickedSchema,
|
|
onboarding_ghostty_import_failed: onboardingGhosttyImportFailedSchema,
|
|
activation_checklist_item_completed: activationChecklistItemCompletedSchema
|
|
} as const
|
|
|
|
export type EventMap = { [N in keyof typeof eventSchemas]: z.infer<(typeof eventSchemas)[N]> }
|
|
export type EventName = keyof EventMap
|
|
export type EventProps<N extends EventName> = EventMap[N]
|
|
|
|
// Why: events whose schemas declare a given property name. Extracted so the
|
|
// cast (Object.entries → [EventName, ZodTypeAny]) stays in one place; if the
|
|
// schema-registry shape ever changes, only one site needs to update.
|
|
// Safely skips non-`ZodObject` schemas (e.g. a future `z.discriminatedUnion`
|
|
// or `z.union`) — those have no `.shape`, and probing `key in undefined`
|
|
// would throw at module load and take the telemetry module down on import.
|
|
function eventsWithShapeKey(key: string): ReadonlySet<EventName> {
|
|
return new Set(
|
|
(Object.entries(eventSchemas) as [EventName, z.ZodTypeAny][])
|
|
.filter(([, schema]) => schema instanceof z.ZodObject && key in schema.shape)
|
|
.map(([name]) => name)
|
|
)
|
|
}
|
|
|
|
// Events whose schemas declare `nth_repo_added`. Derived from `eventSchemas`
|
|
// at module load by probing each schema's `.shape` — there is no parallel
|
|
// hand-maintained list to drift out of sync. The IPC `telemetry:track`
|
|
// handler injects the cohort property only when the incoming event name is
|
|
// in this set: the schemas are `.strict()`, so injecting `nth_repo_added`
|
|
// on an event whose schema does not declare it would fail validation and
|
|
// silently drop the entire event.
|
|
//
|
|
// Schema-additions checklist for adding a new cohort-extended event:
|
|
// add `nth_repo_added: nthRepoAddedSchema` to the event's schema above.
|
|
// That is the *only* step — this set updates automatically.
|
|
const COHORT_EXTENDED_SET = eventsWithShapeKey('nth_repo_added')
|
|
export const COHORT_EXTENDED: readonly EventName[] = Array.from(COHORT_EXTENDED_SET)
|
|
|
|
// Compile-time roster of events that must declare `nth_repo_added`. Same
|
|
// rationale as `_OnboardingCohortRosterSync` below — guards the runtime
|
|
// injection set against silent schema drift.
|
|
type _CohortExtendedRoster =
|
|
| 'app_opened'
|
|
| 'repo_added'
|
|
| 'add_repo_setup_step_action'
|
|
| 'workspace_created'
|
|
| 'workspace_create_failed'
|
|
| 'agent_started'
|
|
| 'agent_error'
|
|
type _DerivedCohortExtendedEvents = {
|
|
[N in EventName]: 'nth_repo_added' extends keyof EventMap[N] ? N : never
|
|
}[EventName]
|
|
type _CohortExtendedRosterSync = _CohortExtendedRoster extends _DerivedCohortExtendedEvents
|
|
? _DerivedCohortExtendedEvents extends _CohortExtendedRoster
|
|
? true
|
|
: never
|
|
: never
|
|
const _cohortExtendedRosterSyncCheck: _CohortExtendedRosterSync = true
|
|
void _cohortExtendedRosterSyncCheck
|
|
|
|
export function isCohortExtendedEvent(name: EventName): boolean {
|
|
return COHORT_EXTENDED_SET.has(name)
|
|
}
|
|
|
|
// Onboarding events — derived the same way as `COHORT_EXTENDED_SET`: probe
|
|
// each schema's `.shape` for the `cohort` key. The IPC `telemetry:track`
|
|
// handler injects the onboarding cohort property only when the incoming
|
|
// event name is in this set; schemas are `.strict()`, so injecting `cohort`
|
|
// on an event whose schema does not declare it would fail validation and
|
|
// silently drop the entire event.
|
|
//
|
|
// Adding a new onboarding event: include `cohort: cohortSchema` on its
|
|
// schema. This set updates automatically.
|
|
const ONBOARDING_COHORT_SET = eventsWithShapeKey('cohort')
|
|
// `NonNullable` strips `undefined` introduced by `cohortSchema`'s `.optional()`.
|
|
export type OnboardingCohort = NonNullable<z.infer<typeof cohortSchema>>
|
|
|
|
// Compile-time roster of events that must declare `cohort`. If a schema
|
|
// refactor drops the field from one of these, this fails tsc rather than
|
|
// silently dropping the event from the runtime injection set above (which
|
|
// the `.optional()` schema would tolerate without any test failure).
|
|
//
|
|
// Adding a new onboarding event: add its name here AND declare
|
|
// `cohort: cohortSchema` on its schema. Both are required.
|
|
type _OnboardingCohortRoster =
|
|
| 'onboarding_started'
|
|
| 'onboarding_step_viewed'
|
|
| 'onboarding_step_completed'
|
|
| 'onboarding_step_skipped'
|
|
| 'onboarding_step4_path_clicked'
|
|
| 'onboarding_step4_path_failed'
|
|
| 'onboarding_completed'
|
|
| 'onboarding_dismissed'
|
|
| 'onboarding_agent_picked'
|
|
| 'onboarding_ghostty_discovered'
|
|
| 'onboarding_ghostty_import_clicked'
|
|
| 'onboarding_ghostty_import_failed'
|
|
type _DerivedOnboardingCohortEvents = {
|
|
[N in EventName]: 'cohort' extends keyof EventMap[N] ? N : never
|
|
}[EventName]
|
|
type _OnboardingCohortRosterSync = _OnboardingCohortRoster extends _DerivedOnboardingCohortEvents
|
|
? _DerivedOnboardingCohortEvents extends _OnboardingCohortRoster
|
|
? true
|
|
: never
|
|
: never
|
|
const _onboardingCohortRosterSyncCheck: _OnboardingCohortRosterSync = true
|
|
void _onboardingCohortRosterSyncCheck
|
|
|
|
export function isOnboardingEvent(name: EventName): boolean {
|
|
return ONBOARDING_COHORT_SET.has(name)
|
|
}
|
|
|
|
// Common props attached by the client — declared here so the validator knows
|
|
// which keys to allow on every outgoing event.
|
|
//
|
|
// No `env: 'prod' | 'dev'` property. Every transmitted event is by
|
|
// construction from an official CI build, so a wire discriminator would be
|
|
// redundant. Contributor / `pnpm dev` builds do not transmit at all; they
|
|
// console-mirror.
|
|
//
|
|
// Every string field carries the 64-char cap directly — this is what the
|
|
// validator's "string-length cap" rule is made of; there is no separate
|
|
// post-parse length check to keep in sync with the schema.
|
|
export const commonPropsSchema = z
|
|
.object({
|
|
app_version: z.string().max(64),
|
|
platform: z.string().max(64),
|
|
arch: z.string().max(64),
|
|
os_release: z.string().max(64),
|
|
// `install_id` is used as PostHog's `distinctId` and `session_id` is the
|
|
// per-process correlation key — an empty string on either would collapse
|
|
// unrelated events into a single synthetic "user" / "session" and
|
|
// silently corrupt analytics. `.min(1)` rejects that actual observed
|
|
// failure mode without pinning the shape to UUIDs (both ids come from
|
|
// `randomUUID()` today, but forward-compatibility with a future id
|
|
// scheme is cheap to preserve).
|
|
install_id: z.string().min(1).max(64),
|
|
session_id: z.string().min(1).max(64),
|
|
orca_channel: z.enum(['stable', 'rc'])
|
|
})
|
|
.strict()
|
|
export type CommonProps = z.infer<typeof commonPropsSchema>
|