refactor(commands): declare a command option once and derive its surfaces
Closes #2410. Child 1 of #2409.
Implements the removable pass-through hops the hop trace on #2410 named, and
nothing else. An option is now declared once, and each surface reads that
declaration instead of a copy of it.
What changed
1 — the dispatch pass-through (trace hop group 1).
DISPATCH_CONTEXT_FLAG_KEYS in src/core/dispatch-context.ts declares the
flags that reach platform execution unchanged. DispatchContext is
Pick<CommandFlags, …> of that list, and the daemon's contextFromFlags copies
exactly it. The mapper keeps only what it decides: the request-scope fields,
clickButton (resolved), the screenshot family, and the maestro-nested capture
backend. An identically-named flag costs one edit instead of two files, which is
the same collapse screenshotFlagsFromOptions already shipped one line below.
2 — the snapshot flag↔option rename, nine sites (group 2).
SNAPSHOT_OPTION_FLAGS in @agent-device/kernel/snapshot declares the eight
pairs once (customActions ↔ snapshotCustomActions and siblings) with both
projections, generalising the shipped screenshotFlagsFromOptions /
screenshotOptionsFromFlags. Every projection takes an explicit key list, so
declaring a pair never silently widens a seam that cannot honour it. The nine
hand-written renames — the CLI reader, the client option projection, three
daemon capture inputs, the presentation key, SNAPSHOT_OPTION_FLAGS' former
local copy — now read the declaration. src/daemon/request-router.ts's
--actions/--raw rejection, the presentation-key decision and the replay
journal's wire key are untouched: they decide things.
3 — the duplicate per-command option types (group 3).
SnapshotCommandOptionFields is the one statement of the six snapshot capture
keys. CaptureSnapshotOptions (public SDK), CommandExecutionOptions (internal
request bag) and SnapshotCommandOptions (command runtime) reference it instead
of re-listing them.
5 + 6 — one owner for an option's prose and its type (groups 5, 6).
FlagDefinition now carries both audiences side by side: usageDescription for
--help and inputDescription for the tool/SDK field. They stay two strings —
they are legitimately two audiences with two lengths — but one owner, so a
rewrite of one is a rewrite of both. optionField(key) derives a command's
input field from that declaration: the JSON schema shape and bounds from type
/ enumValues / min / max, the description from inputDescription, the
TypeScript value type from CliFlags[key]. Applied to the two traced options
(open --foreground, snapshot --actions), so the copy in cli-flags.ts and
the hand-written booleanField('…') in each command are gone.
The public SDK JSDoc stays. AppOpenOptions.foreground and
CaptureSnapshotOptions.customActions keep their editor documentation: a
.d.ts is read where no FlagDefinition resolves, and this repo has no step
that generates those docs, so removing them would have been a silent API
documentation regression rather than a de-duplication. Documentation on a public
type is not a second statement of what the option does — but it is the same
sentence in a second place, so it is now the option's ONE declaration verbatim
(previously each field carried a third, differently-worded copy) and pinned to
it by a test, below. CaptureSnapshotOptions lifts customActions out of the
SnapshotCommandOptionFields spread only to carry that prose; its type is still
SnapshotCommandOptionFields['customActions'], so dropping the key from the
declaration fails to compile here.
No help text, MCP tool schema or wire shape changes: pnpm check:mcp-metadata
is clean and no CLI grammar or help test was edited.
Proof — #1693 replayed (corrected 2026-09-09, was wrong)
The previous revision understated both replays. Corrected counts below,
verified live on this branch (edits made, tests run, then reverted — see the
review comment for the itemized packaged-size table this also required).
#1693 added open --foreground — on origin/main that one option costs
at least 16 files across 4 layers (#2410's own baseline). Replayed as an
addition on this shape — a new boolean option wired the same way
(--dry-run on open, added end to end and then reverted):
contracts type: M packages/contracts/src/cli-flags.ts
descriptor: M src/commands/cli-grammar/flag-definitions-action.ts
owning handler: M src/commands/management/app.ts
tests: a test for the option's own behaviour
Four files, matching #2410's acceptance criterion exactly (contracts type,
descriptor, owning handler, tests) — down from 16, none of them under
src/cli-schema/, none in an MCP parity test. Verified: with only the three
production files above edited (no test), pnpm typecheck,
command-descriptor-parity, mcp/command-tools-parity and
sync-mcp-metadata --check all stayed green, unedited — a real addition would
still add its own behaviour test, which is the fourth file.
Separately — this is what the previous revision got wrong. Rewording the
description of an option that already carries hand-authored SDK
documentation, such as open --foreground's inputDescription, is not a
one-file edit on this shape:
before (origin/main): 4 files carry the same sentence
after (this branch): M src/commands/cli-grammar/flag-definitions-action.ts
M packages/contracts/src/client-app.ts
Two production files, zero test files — not one. The JSDoc-equality test
added at 79f7161 (each documented SDK option field carries its option declaration verbatim) pins client-app.ts's hover doc to the same
inputDescription, so editing the descriptor alone now fails that test.
Verified live: editing foreground's inputDescription alone turns
command-input-option-field.test.ts red; it is only green again once
client-app.ts's JSDoc is edited to match. This is the intended second edit,
not a regression — it is what "pinned so they cannot drift" (Tests, below)
means in practice.
That second edit is not universal: it only applies to the 2 of 156 declared
options (foreground, snapshotCustomActions) that carry a hand-authored
public SDK doc comment by design. For the other 154, a description edit is
still one file — nothing pins their (nonexistent) SDK doc to the declaration.
Both replays keep the rest of the original claim: no edit under
src/cli-schema/, none in an MCP parity test, none in
src/core/dispatch-context.ts, src/daemon/context.ts or
src/commands/command-flags.ts. --help output is untouched by a
description-only edit — only the MCP/SDK field description and, for the two
documented fields, the SDK hover doc move.
Tests
Neither parity test was redundant, so neither was deleted:
src/mcp/__tests__/command-tools-parity.test.tsis a behaviour test, not a
comparison of two hand-written copies; the MCP input schema was already
derived onmain(trace correction 1).src/__tests__/command-descriptor-parity.test.tsis command-level only and
has no option content (trace correction 2).
What did not exist is a test at the option level. Added
src/commands/command-input-option-field.test.ts in the planted-divergence
form: a divergence planted in the one declaration must move the derived surface
— which a second hand-written copy could not do.
The plant lands before the surfaces are built. Command metadata, and the MCP
tool schema built from it, are constructed at module load, so a plant applied
afterwards would only prove that optionField reads its own argument.
buildWithPlantedOption resets the module graph, writes the divergence onto the
declaration, and only then imports command-metadata.ts and
mcp/command-tools.ts, so what the assertions observe is the production
derivation running over the planted declaration. Both derived surfaces are
asserted — the command metadata property and the MCP tool's input schema
property — for a planted description (open --foreground) and for a planted
value type with bounds (snapshot --actions, boolean → int with min/max).
The guard has teeth: reverting either production use to its old hand-written
field turns the corresponding test red (both red runs are recorded in the review
comment on this PR).
Plus: each documented SDK option field's JSDoc must equal its declaration's
inputDescription verbatim (read out of the .ts source), every declared tool
audience is consumed by a command (no orphan prose), an option with no declared
tool audience cannot be derived into a field (fail-closed), and the two
audiences stay distinct.
Named but not done here
- Group 4,
usageOverrideon option-bearing commands.snapshot's
generated synopsis is not byte-identical to its override (the generator
appends[--record]and expandsSNAPSHOT_FLAGSin a different order), so
dropping the override changes--helpoutput. That is a help-text decision,
not a pass-through collapse, and this PR was required to leave the help tests
green and unchanged. Worth its own change. - Moving
PROJECT_CONFIG_FLAG_KEYSandSANITIZED_FLAG_KEYSonto the option
declaration. The trace lists these under "must NOT be collapsed — move, never
infer". Moving them means adding a required field to ~200 flag declarations and
saves nothing on the #1693 replay; it belongs to the #1665-shaped follow-up.
Packaged size (added 2026-09-09)
The size gate's +3.3 kB/+2.2 kB is itemized per-file in the review comment on
this PR (same method as scripts/size-report.mjs: pnpm build +
npm pack --dry-run --json, diffed against main@9d7d60c5e0). One line item
was pure waste — an internal refactor-rationale JSDoc comment on
SNAPSHOT_OPTION_FLAGS (packages/kernel/src/snapshot.ts) that
rolldown-plugin-dts was bundling in full into the public .d.ts — fixed at
9990ea1 by moving it to a source comment with a short hover doc in its place
(no behaviour change). That cuts the growth to +2.3 kB / +1.8 kB. The rest is
the shared snapshot-option type surface itself and the two SDK JSDoc blocks
this review asked to keep verbatim-pinned; the comment explains why a smaller
design for those was rejected.
Verification
pnpm typecheck ✅ · pnpm test:unit 9359 passed ✅ · pnpm lint ✅ ·
pnpm check:layering ✅ · pnpm check:fallow ✅ (no issues in changed files) ·
pnpm check:mcp-metadata ✅ · eager-closure budgets 577/577 ✅ ·
pnpm check:affected --run ✅ (770 files, 5828 tests, after the 9990ea1 fix).
packages/host-kit/src/code-signature.test.ts fails 3 tests on this branch and
fails the same 3 on the origin/main checkout — pre-existing, not from this
change.