Files
orca/src/shared/telemetry-workspace-create-schemas.ts
T
Brennan Benson d6d2795da5 chore(worktree): include create timing and spare outcome in the workspace create events (#24483)
* chore(worktree): include create timing and spare outcome in the workspace-created event

The workspace_created and workspace_create_failed events gain optional,
numbers-and-enums-only fields built from what the create already measured:
total and per-phase durations, the prepared-checkout hit/miss and miss
reason, the execution host (local/WSL/SSH), a worktree count bucket, how
many other creates were in flight, whether the repo has a post-checkout
hook (file existence only, probed after the create returns), and for a
failure the phase it died in plus elapsed time. No new git process runs;
consent and opt-out are unchanged.

* fix(worktree): attribute failed_phase by error, label WSL-path repos, skip the hook check with telemetry off

- failed_phase now names the outermost timed step the thrown error (or its cause) left, so a
  caught failure or a concurrent sibling step can no longer be misattributed; the old-relay SSH
  error keeps its cause so it still reads as git_worktree_add.
- execution_host follows the same rule Git routing uses, so a \\wsl.localhost repo reads wsl.
- The post-checkout hook check does not read the repo when telemetry is disabled.
- Privacy page mentions the miss reason code and the failed step.

* test(worktree): pin the old-relay SSH add error to git_worktree_add through its cause

* fix(worktree): name the create event field sets for their role, and type the old-relay test's caught error

* fix(worktree): send create events from runtime creates and record what the spare checkout did

Runtime creates (CLI, agents, phone app, paired clients, orchestration, server
automations) reuse prepared checkouts like the app's own creates, but recorded
no timing and sent no events. Both entry points now start one shared sender
(workspace-create-telemetry.ts), so every create sends exactly one event with
the same fields, plus create_entry_point (app | runtime).

Spare-checkout fields:
- concurrent_preparations: peak prepared-checkout builds and background
  discards running during the create, excluding the one it used; the window
  closes before the create's own re-arm starts.
- prepared_checkout_claim / prepared_checkout_discard phases, so on a miss
  git_worktree_add minus the prepared_checkout_* phases is the plain checkout.
- prepared_checkout_reset (none | base_moved | retargeted) replaces the
  retargeted flag; prepared_checkout_origin (prefetch | rearm) on hits.
- workspace_create_failed carries the spare outcome and its wait.
- repo_index_size_bucket from one stat of .git/index in the existing
  post-create probe (telemetry on, local/WSL only, 2 s cap).

* fix(worktree): add spare build and idle time, the re-arm prefetch origin, and a tracked-file count

- prepared_checkout_build_ms / prepared_checkout_idle_ms on hits: from arming
  the spare to ready, and how long it sat ready before the claim (0 when the
  create waited). readyAt is recorded in the pool's existing ready handler.
- prepared_checkout_origin gains rearm_then_prefetch: an automatic re-arm that
  the dialog prefetch then asked for too, so rearm means the re-arm alone.
- repo_file_count_bucket replaces the index byte size: the entry count from
  the 12-byte index header, which is the same in every index version; left
  out for a split or sparse index.
- The shared sender never lets a failed send change the create's result or
  error; it logs instead and still ends the create's concurrency membership.
- Tests pin the runtime SSH create's timing hand-off and the throwing-send
  cases on both entry points.

* fix(worktree): leave out the file count under any sparse checkout and time spare builds monotonically

- The repo probe also reads .git/config.worktree, where git sparse-checkout
  --sparse-index writes index.sparse, and omits the file count whenever
  sparse checkout or a sparse index is on in either file, with Git's boolean
  spellings. core.hooksPath there is honoured too.
- prepared_checkout_build_ms / _idle_ms use performance.now(), like every
  other duration; the build is timed from its own start (buildStartedAt).
- The origin field comment names all three values.

* fix(worktree): keep the spare's build time on its first build and count worktrees by lock reason

- prepared_checkout_build_ms runs from the first build's start (including any
  wait for the base fetch it is built on) to its first ready; a later tip
  refresh no longer restarts it, though it still counts as new preparation
  work. prepared_checkout_idle_ms runs from the latest ready (build or
  refresh) to the claim.
- The worktree count reads each .git/worktrees entry's locked file and leaves
  out entries whose lock reason names an Orca preparation, the way the
  listing does, instead of subtracting this process's spares. That covers
  spares from other processes, crash leftovers and spares being discarded,
  and cannot run one low while a spare's admin dir does not exist yet. It has
  its own 1.5 s cap inside the probe.
2026-10-02 12:46:13 -07:00

122 lines
4.9 KiB
TypeScript

import { z } from 'zod'
import {
PREPARED_CHECKOUT_MISS_REASONS,
PREPARED_CHECKOUT_ORIGINS,
PREPARED_CHECKOUT_RESETS,
WORKSPACE_CREATE_ENTRY_POINTS,
WORKTREE_CREATE_EXECUTION_HOSTS,
WORKTREE_CREATE_PHASES,
type WorktreeCreatePhase
} from './worktree/create-timing-vocabulary'
// Why numbers and closed enums only: these fields explain where create time went on real
// machines; a path, branch, ref or error string would identify the repo, so none may appear.
// Every field is optional: folder workspaces and creates outside the timed paths omit them.
const durationMsSchema = z.number().int().nonnegative().optional()
export const WORKTREE_COUNT_BUCKETS = [
'1',
'2-5',
'6-20',
'21-100',
'101-300',
'301-1000',
'1001+'
] as const
/** Files the main repo tracks (its index entry count); checkout cost grows with it, and these split
* small projects, typical apps, kernel-sized repos, large monorepos and the very largest. */
export const REPO_FILE_COUNT_BUCKETS = ['<1k', '1k-10k', '10k-100k', '100k-500k', '500k+'] as const
export const POST_CHECKOUT_HOOK_PRESENCE_VALUES = [
'present',
'absent',
'custom_hooks_path',
'unknown'
] as const
const phaseDurationProperties = {
resolve_name_ms: durationMsSchema,
refresh_base_ref_ms: durationMsSchema,
git_worktree_add_ms: durationMsSchema,
prepared_checkout_claim_ms: durationMsSchema,
prepared_checkout_wait_ms: durationMsSchema,
prepared_checkout_finalize_ms: durationMsSchema,
prepared_checkout_discard_ms: durationMsSchema,
list_created_worktree_ms: durationMsSchema,
persist_metadata_ms: durationMsSchema,
create_symlinks_ms: durationMsSchema,
resolve_shared_directories_ms: durationMsSchema,
resolve_worktreeinclude_ms: durationMsSchema,
create_shared_directories_ms: durationMsSchema,
copy_worktreeinclude_ms: durationMsSchema,
prepare_setup_ms: durationMsSchema,
spawn_startup_terminal_ms: durationMsSchema
}
// Compile-time: one duration key per timed phase, no more and no fewer.
type _PhaseKeys = `${WorktreeCreatePhase}_ms`
type _PhaseDurationKeys = keyof typeof phaseDurationProperties
type _PhaseDurationSync = [_PhaseKeys] extends [_PhaseDurationKeys]
? [_PhaseDurationKeys] extends [_PhaseKeys]
? true
: never
: never
const _phaseDurationSyncCheck: _PhaseDurationSync = true
void _phaseDurationSyncCheck
const countSchema = z.number().int().nonnegative().optional()
/** Fields both events carry: which entry point ran, where Git ran, and what competed for the disk. */
const workspaceCreateContextProperties = {
create_entry_point: z.enum(WORKSPACE_CREATE_ENTRY_POINTS).optional(),
execution_host: z.enum(WORKTREE_CREATE_EXECUTION_HOSTS).optional(),
/** Most other worktree creates running in this app at once during this one. */
concurrent_creates: countSchema,
/** Most prepared-checkout builds and discards running at once during this create, not counting
* the prepared checkout this create used. */
concurrent_preparations: countSchema
}
/** The prepared-checkout outcome, sent on success and failure alike. */
const preparedCheckoutProperties = {
prepared_checkout: z.enum(['hit', 'miss']).optional(),
/** Hit only: the reset the prepared checkout needed before it was handed over. */
prepared_checkout_reset: z.enum(PREPARED_CHECKOUT_RESETS).optional(),
/** Hit only: the new-worktree UI's prefetch, the automatic burst replacement, or that
* replacement which the prefetch then asked for too. */
prepared_checkout_origin: z.enum(PREPARED_CHECKOUT_ORIGINS).optional(),
/** Hit only: from the start of the prepared checkout's build to it being ready. */
prepared_checkout_build_ms: durationMsSchema,
/** Hit only: how long it sat ready before this create claimed it; 0 when the create waited. */
prepared_checkout_idle_ms: durationMsSchema,
prepared_checkout_miss_reason: z.enum(PREPARED_CHECKOUT_MISS_REASONS).optional()
}
export const workspaceCreatedTimingProperties = {
total_ms: durationMsSchema,
/** Wall-clock time no timed phase covers. */
unattributed_ms: durationMsSchema,
// The prepared_checkout_* phases run inside git_worktree_add, so the plain checkout is the
// add minus them.
...phaseDurationProperties,
...preparedCheckoutProperties,
...workspaceCreateContextProperties,
worktree_count_bucket: z.enum(WORKTREE_COUNT_BUCKETS).optional(),
repo_file_count_bucket: z.enum(REPO_FILE_COUNT_BUCKETS).optional(),
post_checkout_hook: z.enum(POST_CHECKOUT_HOOK_PRESENCE_VALUES).optional()
}
/** `untimed` = the create failed outside every timed phase. */
export const WORKSPACE_CREATE_FAILED_PHASE_VALUES = [...WORKTREE_CREATE_PHASES, 'untimed'] as const
export const workspaceCreateFailedProperties = {
failed_phase: z.enum(WORKSPACE_CREATE_FAILED_PHASE_VALUES).optional(),
/** Elapsed time from the start of the create to the failure. */
total_ms: durationMsSchema,
...preparedCheckoutProperties,
prepared_checkout_wait_ms: durationMsSchema,
...workspaceCreateContextProperties
}