refactor(#2360): host dispatch table + state cutover pilot (ADR-2346 P1) (#2364)

* refactor(#2360): host dispatch table + state cutover pilot (ADR-2346 P1)

Pilot cutover for ADR-2346 Phase 1 (epic #2345). Introduces the Layer-2 host
dispatch table — dispatchHostCommand + HOST_COMMAND_ROUTERS, consulted in
runCommand's default case after capability/overlay dispatch, before the
unknown-command error. Migrates 'state' as the pilot: removes the hardcoded
case 'state': arm; state now dispatches default -> dispatchHostCommand ->
routeStateCommand, byte-identical to the old path (proven by the new
state-command-cutover equivalence test, 5-category template).

Host commands are NOT capabilities (core, non-toggleable, no tier/activationKey)
— the capability registry stays reserved for toggleable feature bundles per
ADR-959. This is the host-vs-capability distinction the merged ADR-2346 lacked;
the ADR is corrected here alongside the code that realizes it.

- gsd-core/bin/gsd-tools.cjs: HOST_COMMAND_ROUTERS + dispatchHostCommand
  (prototype-pollution-safe); wired into default case; case 'state': removed;
  dispatchHostCommand + HOST_COMMAND_ROUTERS exported for tests.
- tests/state-command-cutover.test.cjs: UNIT/DISPATCH/BEHAVIOR/REGISTRY
  equivalence (recording-mock + runGsdTools end-to-end + pollution guard).
- docs/adr/2346-*.md: refine Decision 1/2 to the host-table vs capability-
  registry model (correction that did not land in the merged #2355).

Behavior-preserving. Subsequent P1b/c PRs migrate phase/init/roadmap/validate/
verify using this proven template.

Closes #2360.

* test(#2360): regenerate golden fixtures + allowlist for state cutover

Bookkeeping for the gsd-tools.cjs change: npm run gen:golden regenerates the
install-parity fixtures (gsd-tools.cjs content hash changed), and the new
tests/state-command-cutover.test.cjs is added to the lint-test-file-count
allowlist under the 'state' prefix.

* refactor(#2360): migrate remaining Tier-1 routers (phase/init/roadmap/validate/verify)

Completes P1: all 6 Tier-1 host routers now dispatch via HOST_COMMAND_ROUTERS
(state landed in the pilot commit). init preserves its #1688 warnIfStaleBake
pre-hook; validate binds the output emitter. Cutover test extended to assert
all 6 are consumed + owned. Golden install-parity fixtures regenerated.
This commit is contained in:
Tom Boucher
2026-07-17 09:19:28 -04:00
committed by GitHub
parent f1a91072b6
commit cf004df678
23 changed files with 259 additions and 97 deletions

View File

@@ -562,6 +562,57 @@ function dispatchOverlayCapabilityCommand({ command, args, cwd, raw, error, load
return true;
}
// ─── ADR-2346 (epic #2345): host dispatch table ───────────────────────────────
// Layer-2 of the two-layer dispatch. Core, non-capability host commands live
// here — NOT in the capability registry (ADR-959's commandFamilies is reserved
// for toggleable feature capabilities: graphify/audit/intel). A host command
// like `state` is core, non-toggleable, carries no tier/activationKey, so it
// cannot be a capability. Each entry maps a top-level command to its standard
// `route*Command` router (the same routers the hardcoded `case` arms called).
// Consulted in runCommand's `default` case, after capability + overlay
// dispatch, before the unknown-command error. A migrated command's `case` arm
// is removed at cutover so it reaches here; an unmigrated command still hits
// its `case` (collision structurally impossible, same property as ADR-959).
const HOST_COMMAND_ROUTERS = {
// Each entry wraps its `route*Command` router so it receives the module-scope
// lib the old `case` arm passed, plus the per-dispatch context
// { args, cwd, raw, error }. Closes over module-scope libs (state/phase/…)
// exactly as the old inline arms did — byte-identical dispatch.
state: (ctx) => routeStateCommand({ state, ...ctx }),
phase: (ctx) => routePhaseCommand({ phase, ...ctx }),
roadmap: (ctx) => routeRoadmapCommand({ roadmap, ...ctx }),
verify: (ctx) => routeVerifyCommand({ verify, ...ctx }),
// validate additionally binds the module-scope `output` emitter.
validate: (ctx) => routeValidateCommand({ verify, output, ...ctx }),
// init preserves the #1688 stale-bake warning (best-effort, swallowed) that
// ran before the router in the old `case 'init':` arm.
init: (ctx) => {
try { warnIfStaleBake(ctx.cwd); } catch { /* guard must never break init */ }
routeInitCommand({ init, ...ctx });
},
};
// Returns true when consumed (suppress "Unknown command"), false to fall
// through. Prototype-pollution-safe: own-property lookup rejects
// `__proto__`/`constructor`/`prototype` command keys (same guard as
// dispatchCapabilityCommand).
function dispatchHostCommand({ command, args, cwd, raw, error }) {
if (
command === '__proto__' ||
command === 'constructor' ||
command === 'prototype'
) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(HOST_COMMAND_ROUTERS, command)) {
return false;
}
const router = HOST_COMMAND_ROUTERS[command];
if (typeof router !== 'function') return false;
router({ args, cwd, raw, error });
return true; // consumed — don't emit "Unknown command"
}
// ─── Arg parsing helpers ──────────────────────────────────────────────────────
// ─── CLI Router ───────────────────────────────────────────────────────────────
@@ -877,17 +928,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'state': {
routeStateCommand({
state,
args,
cwd,
raw,
error,
});
break;
}
case 'resolve-model': {
commands.cmdResolveModel(cwd, args[1], raw);
break;
@@ -1119,17 +1159,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'verify': {
routeVerifyCommand({
verify,
args,
cwd,
raw,
error,
});
break;
}
case 'eval': {
routeEvalCommand({ evalMod, args, cwd, raw, error });
break;
@@ -1475,17 +1504,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'roadmap': {
routeRoadmapCommand({
roadmap,
args,
cwd,
raw,
error,
});
break;
}
case 'assumption-delta': {
// #1561 — advisory architecture checkpoint. `scan <phase>` reads the
// phase section via the same resolver as roadmap.get-phase and runs the
@@ -1540,17 +1558,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'phase': {
routePhaseCommand({
phase,
args,
cwd,
raw,
error,
});
break;
}
case 'milestone': {
const subcommand = args[1];
if (subcommand === 'complete') {
@@ -1568,18 +1575,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'validate': {
routeValidateCommand({
verify,
args,
cwd,
raw,
output: output,
error,
});
break;
}
case 'progress': {
const subcommand = args[1] || 'json';
commands.cmdProgressRender(cwd, subcommand, raw);
@@ -1630,21 +1625,6 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
break;
}
case 'init': {
// #1688: warn (at most once per process) if the user edited model_overrides
// without re-running `gsd install <runtime>` on a static-frontmatter runtime.
// Best-effort, stderr-only, swallowed errors — never blocks the command.
try { warnIfStaleBake(cwd); } catch { /* guard must never break init */ }
routeInitCommand({
init,
args,
cwd,
raw,
error,
});
break;
}
case 'loop': {
// loop render-hooks <point>
const loopSubcommand = args[1];
@@ -3232,6 +3212,12 @@ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand
// require()-ing its router FROM the capability's install root (confined to that root).
if (dispatchOverlayCapabilityCommand({ command, args, cwd, raw, error })) break;
// ADR-2346 (epic #2345): host dispatch table — core, non-capability
// commands (state, …) routed via their `route*Command` router instead of
// a hardcoded `case` arm. Tried after capability/overlay dispatch and
// before the unknown-command error.
if (dispatchHostCommand({ command, args, cwd, raw, error })) break;
// #3243: if the caller passed a dotted form (e.g. "foo.bar"), the shim
// above split it so `command` here is the head ("foo"). Use
// originalCommand to reconstruct the original dotted form and suggest
@@ -3263,4 +3249,4 @@ if (require.main === module) {
// synthetic registry + requireModule injections.
// ADR-1244 Phase 5: export dispatchOverlayCapabilityCommand + defaultRequireFromInstallRoot for
// the third-party overlay dispatch + install-root confinement tests.
module.exports = { dispatchCapabilityCommand, dispatchOverlayCapabilityCommand, defaultRequireFromInstallRoot };
module.exports = { dispatchCapabilityCommand, dispatchOverlayCapabilityCommand, defaultRequireFromInstallRoot, dispatchHostCommand, HOST_COMMAND_ROUTERS };