Skip to content

refactor(cli): split the run() god-function into a dispatch table (H3)#104

Merged
CodeWithJuber merged 1 commit into
masterfrom
claude/loop-goal-worktree-issues-il3uk9
Jul 20, 2026
Merged

refactor(cli): split the run() god-function into a dispatch table (H3)#104
CodeWithJuber merged 1 commit into
masterfrom
claude/loop-goal-worktree-issues-il3uk9

Conversation

@CodeWithJuber

Copy link
Copy Markdown
Owner

What & why

Fixes H3 from the deep review — the 2,420-line run() god-function in src/cli.js
(97% of the file; a flat 44-branch if (cmd === …) chain the review flagged as
"unreviewable, a merge-conflict magnet, boilerplate per new command").

run() is now a ~44-line dispatcher: it looks the command up in a HANDLERS map and
delegates. Each command is a module-scope async (argv, cmd) handler — independently
navigable (jump to HANDLERS.verify) and independently mergeable, which ends the
merge-conflict-magnet problem.

Behavior is byte-identical. Same argv in → same stdout/stderr/exit-code out. The
contract the whole test estate relies on (35+ files spawn node src/cli.js <args> and
assert output/exit codes) is unchanged and green. Preserved exactly:

  • pre-dispatch middleware (--help/-h interception, first-run hint, the cortex-mcp
    server exemption) runs before the table lookup;
  • the unknown-command suggestion + the "not wired yet" non-zero-exit fallback;
  • the process.exitCode (never process.exit()) error model;
  • the symlink-resolving main-module import guard (importing the package root still does
    not execute the CLI).

Handler signatures are trimmed to the params each body actually uses, so the refactor
introduces no unused-parameter warnings (biome stays at its baseline 10 warnings, 0
errors).

Checklist

  • npm test passes (1069 pass / 0 fail / 2 gated skips)
  • npm run check passes (Biome — 0 errors)
  • Conventional commit message
  • CHANGELOG.md updated under ## [Unreleased]
  • No new runtime dependency
  • Substrate/docs updated if affected (n/a — pure internal restructure)

Risk & rollback

  • Risk level: low — pure mechanical restructure, no behavior change, full suite green.
  • Rollback plan: revert the single commit; no data/schema changes.

Extra checks

  • npm run typecheck passes
  • node src/cli.js docs check green
  • Importing the package root does not execute the CLI (M1 guard intact)
  • Verified dispatch smokes: --help, brand, catalog, verify --json, unknown-command → exit 1

🤖 Generated with Claude Code

https://claude.ai/code/session_01C7htTiesXKLvAUtv6ET2jz


Generated by Claude Code

The former ~2,420-line run() (a flat 44-branch `if (cmd === …)` chain, 97% of
src/cli.js) is now a ~44-line dispatcher: it looks the command up in a HANDLERS
map and delegates. Each command is a module-scope `async (argv, cmd)` handler —
independently navigable and mergeable, ending the merge-conflict-magnet problem.

Behavior is byte-identical: same argv in, same stdout/stderr/exit-code out. The
pre-dispatch middleware (--help interception, first-run hint, cortex-mcp), the
unknown-command fallback, the process.exitCode error model, and the main-module
import guard are all preserved. Handler signatures are trimmed to the params each
body actually uses, so no unused-parameter warnings are introduced.

Verified: full spawn-based suite 1069 pass / 0 fail / 2 skip, biome 0 errors,
tsc 0 errors, `docs check` green, and importing the package root still does not
execute the CLI.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01C7htTiesXKLvAUtv6ET2jz
@CodeWithJuber
CodeWithJuber marked this pull request as ready for review July 20, 2026 15:04
@CodeWithJuber
CodeWithJuber merged commit 6d3fa37 into master Jul 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants