refactor: declare command timeout policy on descriptors (ADR 0011)

Merged
TH
5mo ago
+342-61

Implements the "Timeout policy joins the descriptor registry" step of ADR 0011 (migration step 5).

Why

The wait timeout bug (#1075) existed because request-envelope budgets and on-timeout daemon policy lived in two hand-maintained command-name lists in the client, and nothing forced a command to appear in either:

  • isExplicitTimeoutCommand in src/daemon/client/daemon-client.ts — which commands' --timeout flag bounds the request envelope;
  • DAEMON_PRESERVING_TIMEOUT_COMMANDS / shouldResetDaemonAfterRequestTimeout in src/daemon/client/daemon-client-timeout.ts — which commands keep the daemon alive when their request times out.

wait fell through both: its user budget travels as a positional (invisible to the flag list), and it shared snapshot's stalled-accessibility-bridge failure mode without sharing its preserve-daemon entry.

What

Both hand lists are deleted. Every command descriptor (ADR 0008 registry, src/core/command-descriptor/registry.ts) now declares a required timeoutPolicy:

timeoutPolicy: {
  budget:
    | { source: 'none' }
    | { source: 'flag' }                                  // --timeout overrides the envelope
    | { source: 'positional-parser'; parser: (positionals) => number | null };
  envelopeMs: number | 'unbounded';
  onTimeout: 'preserve-daemon' | 'reset-daemon';
}

Required, not defaulted-by-omission: most entries reference the explicit shared DEFAULT_TIMEOUT_POLICY constant, so adding a command forces a decision at the descriptor (tsc rejects an entry without one), matching the ADR's completeness philosophy and the registry's existing required-batchable convention.

Derived mapping (behavior-identical to the deleted lists):

CommandbudgetenvelopeonTimeout
snapshotflag90spreserve-daemon
waitpositional-parser (resolveWaitBudgetMs)90s (widened to budget+30s, never shrunk)preserve-daemon
findnone90spreserve-daemon
prepareflag240sreset
replayflag90sreset
install / reinstall / install_sourcenone180sreset
testnoneunboundedreset
everything else / unknownnone90sreset

The client (resolveDaemonRequestTimeoutMs, shouldResetDaemonAfterRequestTimeout) now derives everything from resolveCommandTimeoutPolicy(command). The wait +30s margin and the never-shrink-below-envelope rule are preserved verbatim, generalized to any positional-parser budget. The envelope constants moved from src/daemon/request-timeouts.ts (deleted) to src/core/command-descriptor/timeout-policy.ts, next to the policies they parameterize (daemon→core is the sanctioned import direction; the layering guard stays green).

Gates and oracles

  • Completeness gate — src/core/command-descriptor/__tests__/timeout-policy.test.ts, in the style of the ADR 0011 Layer-1 gate: every public command must have a descriptor with a declared policy, and every deviation from the default (preserve-daemon set, flag/positional budget sets, non-90s envelopes) is a bounded diffable list that can only change in the PR that edits it.
  • Behavioral oracle — the pre-existing tests in src/utils/__tests__/daemon-client.test.ts (wait budget arithmetic, preserve set, prepare/install envelopes, test unbounded) pass byte-for-byte unchanged — not even imports — proving the migration is exact.

Verification

  • pnpm format:check, pnpm typecheck, pnpm lint, pnpm check:layering — green
  • pnpm exec fallow audit --base origin/main — no new issues
  • pnpm exec vitest run src/utils src/daemon src/core src/contracts — 180 files / 1569 tests passed
  • pnpm exec vitest run test/integration — 27 files / 89 tests passed (one known doctor/Metro contention flake reproduced once, passed in isolation and on full-suite rerun)