Skip to content

refactor(commands): route slash commands through one dispatcher with a typed CommandContext - #2098

Merged
will-lamerton merged 2 commits into
mainfrom
refactor/command-dispatcher
Nov 11, 2026
Merged

will-lamerton merged 2 commits into
mainfrom
refactor/command-dispatcher

Conversation

@will-lamerton

Copy link
Copy Markdown
Member

Why

source/app/utils/app-util.ts dispatched slash commands as a chain of ~16 handlers, each re-checking commandParts[0], before falling back to the lazy registry. About ten commands in source/commands/ are empty stubs whose real logic lives in that chain, so a stateful command touches three files, and completion is signalled with setTimeout(onCommandComplete, 100) scattered across handlers. This is PR 1 of a sequence that moves every command onto one dispatch path.

What this PR does

  • One dispatcher. source/app/utils/command-dispatcher.ts exports dispatchSlashCommand, which runs the existing resolution order unchanged: custom commands → MCP prompts → aliases (aliases.ts) → inline ?key=value overrides → registry. app-util.ts keeps bash passthrough, handleMessageSubmission and createClearMessagesHandler.
  • StatefulCommand + CommandContext (source/types/commands.ts). A command that changes app state exports run(context) instead of a stub handler. It gets a typed CommandContext (args, messages, and only the setters/services commands use so far) and returns a CommandCompletion ('done' or 'done-after-render'). The dispatcher is the single place that turns that into onCommandComplete. 'done-after-render' keeps the existing 100 ms delay so behaviour is unchanged; removing the delay is for a later PR.
  • Registry. CommandRegistry.load(name) returns the real module (cached), so the dispatcher can tell a stateful command from a read-only one after the progressLabel spinner is mounted (lazy loading and spinner timing are unchanged). The metadata proxy from get() throws a clear error if something tries to run a stateful command through handler.
  • Proof migrations: /clear, /rename, /status now hold their own logic in source/commands/{clear,rename,status} and are gone from handleSpecialCommand.
  • Moves only, no edits: the remaining app-util handlers (handleSpecialCommand + checkpoint load, /stats, /login) move to app/utils/handlers/{special-command,stats,login}-handler.ts and still run before the registry until they are migrated.
  • CLAUDE.md's Command System section describes the dispatcher and both command shapes. Stale "handled in app-util.ts" comments now point at the right files.

Behaviour

No change intended. CommandContext.args uses the submission's commandArgs when set (VS Code context stripped), the same as the old special-command path. /rename, /clear and /status complete on the same 100 ms delay as before. A project command named clear still wins over the built-in.

Tests

  • New command-dispatcher.spec.ts: context args and messages, commandArgs precedence, 'done' vs 'done-after-render' timing, /clear ordering (messages, then clear counter), /status, project-command precedence over a stateful built-in, spinner released when a stateful command throws, unknown commands still reach the registry's error, and the proxy refusing to run a stateful command.
  • clear.spec.tsx used to check that the stub handler returned undefined. It now checks the same behaviour through run: messages are cleared, memory proposals dropped, the app notified, and the command completes after render.
  • All existing app-util.spec.ts tests pass unchanged (rename, status completion timing, aliases, inline overrides, retry/resume, login, stats), as do lazy-registry.spec.ts, aliases.spec.ts, help.spec.tsx and commands.spec.ts.
  • Ran tsc --noEmit, biome check ., knip and knip --production --include files, plus AVA over source/app, source/commands, source/commands.spec.ts, source/hooks, source/plain and the user-input specs. One local failure, app-container.spec "narrow boot summary places branch on its own line", depends on the branch name (it fails the same way without this change).

Next

Migrate the rest in batches (mode-entering commands: model/settings/explorer/ide/tune; then checkpoint load, resume, retry; stats/login with a deferred completion; compact/context-max/create handlers), delete create-stub-command.ts and the handler chain, and replace the remaining setTimeout completions.

🤖 Generated with Claude Code

…a typed CommandContext

Move slash-command routing out of app-util.ts into
source/app/utils/command-dispatcher.ts and add a StatefulCommand shape:
a command that changes app state exports `run(context)`, gets a typed
CommandContext from the dispatcher and returns a CommandCompletion
instead of calling a completion timer itself.

/clear, /rename and /status move to it as the proof, so their logic now
lives in their own modules rather than in handleSpecialCommand. The
remaining app-util handlers move unchanged into app/utils/handlers/ and
still run before the registry until they are migrated.

No behaviour change.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions github-actions Bot added the area:tui Terminal UI label Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

No changeset found

This PR does not add a changeset, so it will not appear in the changelog or trigger a release.

If the change is user-facing, add one:

pnpm changeset

Pick a bump (patch / minor / major) and write the changelog entry in our usual voice ("Added X... Thanks to @you. Closes #123."), then commit the generated .changeset/*.md file.

If this PR is docs-only or a chore that needs no release note, you can ignore this — or run pnpm changeset --empty to record that intentionally.

@github-actions

Copy link
Copy Markdown
Contributor

nc-review could not produce a verdict this run (agent exit 1). This is a problem with the review agent, not with your pull request. A maintainer can retry with /re-review.

@will-lamerton
will-lamerton merged commit 0929c0b into main Nov 11, 2026
18 checks passed
@will-lamerton
will-lamerton deleted the refactor/command-dispatcher branch November 11, 2026 16:55
jay23606 added a commit to jay23606/nanocoder that referenced this pull request Nov 12, 2026
…a typed CommandContext (Nano-Collective#2098)

Move slash-command routing out of app-util.ts into
source/app/utils/command-dispatcher.ts and add a StatefulCommand shape:
a command that changes app state exports `run(context)`, gets a typed
CommandContext from the dispatcher and returns a CommandCompletion
instead of calling a completion timer itself.

/clear, /rename and /status move to it as the proof, so their logic now
lives in their own modules rather than in handleSpecialCommand. The
remaining app-util handlers move unchanged into app/utils/handlers/ and
still run before the registry until they are migrated.

No behaviour change.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tui Terminal UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant