refactor(commands): declare project-config admission and recorder sanitization on the flag declaration

Merged
TH
3mo ago
+589-149

What

A flag's two fail-closed properties — may it be set from a project agent-device.json and does the session recorder copy it into SessionAction.flags — move off their hand-maintained allowlists and onto each FlagDefinition as required projectConfig / recorded fields. Omitting either is now a type error, so the compiler holds the fail-closed property a list held by omission.

Closes #2445 (Wave 2 child of #2409; the "#1665-shaped follow-up" named in #2421).

Changes

  • FlagDefinition gains required projectConfig: boolean and recorded: boolean. All 156 declarations plus the 6 ScreenshotSpecificFlagDefinition entries carry both.
  • recorded is constrained to CommandFlags keys by the type: FlagDefinition is a union that pins recorded to false for a NonRecordableFlagKey = Exclude<FlagKey, keyof CommandFlags> (json / help / version / replayMaestro / daemonAuthToken). This preserves the satisfies readonly (keyof CommandFlags)[] guard the old list carried, so recorded: true on a CLI-only key does not compile and cannot leak into a recorded action.
  • cli-config.ts and session-action-recorder.ts derive their sets from the registry and no longer list keys (PROJECT_CONFIG_FLAG_KEYS / SANITIZED_FLAG_KEYS literals deleted). SCREENSHOT_ACTION_FLAG_KEYS stays (screenshot-options.test.ts still consumes it).
  • Derivations (projectConfigFlagKeys() / recordedFlagKeys() in flag-registry.ts) recompute per call so each consumer freezes its set at its own module load — which is what lets a planted declaration move the surface. recordedFlagKeys() returns ReadonlySet<RecordableFlagKey> via a narrowing predicate, so sanitizeFlags needs no cast.
  • The recorder reaches the derivation through the cli-schema/command-schema.ts seam: R2 commands-floor forbids daemon/ -> commands/.
  • Planted-divergence tests per the #2421 pattern, plus two @ts-expect-error compile-time pins (missing fields; a CLI-only key cannot opt into recording).
  • docs/agents/cli-flags.md points at the declaration fields, not the deleted allowlist.

Acceptance (from the issue)

  • Re-doing #1665 edits the declaration, no allowlist: both consumer files now derive; re-adding a flag touches only the declaration.
  • Derived sets equal the deleted sets — one-off diff is empty (verified against HEAD):
    • PROJECT_CONFIG: derived 85 = original 85; only-derived [], only-orig []
    • RECORDED: derived 39 = original 39; only-derived [], only-orig []
    • cli-config-trust.test.ts (19) and session-action-recorder.test.ts (27) stay green with no edits.
  • A declaration without both fields does not compile: enforced by the required fields + @ts-expect-error pins; full pnpm typecheck green.

Size & design tradeoff

The size bot reports roughly +3 kB unpacked (the exact figure drifts run to run — it has shown +3.0 to +3.3 kB; download is slightly negative; startup within noise). The growth is the two required booleans repeated across all 162 declarations in the emitted bundles — inherent to stating the property per declaration rather than in one list.

The smaller alternative is to keep the two sets as standalone allowlists and not touch the declarations: near-zero bundle cost, but it is exactly the "fail-closed by omission, so a new flag edits a second file anyway" shape #2410/#2445 retire, and it cannot express the CommandFlags guard on a per-declaration basis. Retaining required declarations (so omission is a type error, not a silent drop) over another allowlist is the deliberate tradeoff here: +3 kB unpacked, one file per flag instead of two, and the guard moves onto the declaration.

Verification

pnpm build, pnpm typecheck (all packages + examples), pnpm lint, pnpm format:check, pnpm check:layering, check:production-exports, check:mcp-metadata, check:gate-manifest, check:bundle-owner-files, check:package — all green. Targeted suites: src/daemon (2639), packages/contracts + src/commands/capture + src/cli/parser (447), plus cli-schema/commands/cli-grammar and the new test. Rebased onto main (3bbeb61917); no upstream change touched these files.

Notes

  • docs/adr/0012-interactive-replay.md still names SANITIZED_FLAG_KEYS in a point-in-time record; left unchanged (ADRs are historical and the described behavior — --settle/--verify dropped from recordings — is unchanged).