spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

fix(extensions): make mandatory-hook dispatch contract self-contained

Open Quratulain-bilal opened this issue 5 months ago • 5 comments

Fixes #2730

Problem

The EXECUTE_COMMAND: directive emitted for optional: false hooks is meant as a dispatch signal. In agent-direct invocations (a slash command run inside Claude Code, Cursor, Codex, etc. — the default pattern for extension users), nothing watches for it. The agent emits the directive, waits, and moves on — the hook never runs, and the failure is completely silent.

This is the execution half of #2688: PR #2713 (v0.8.15) fixed the emission half (the directive now reliably appears in agent output), but nothing dispatches it.

This implements Resolution Path 1 from the issue — the lowest-cost fix: make the contract self-contained so the agent performs the dispatch itself.

Changes

  • format_hook_message() (src/specify_cli/extensions.py): after the EXECUTE_COMMAND: / EXECUTE_COMMAND_INVOCATION: lines, the message now tells the agent the directive alone executes nothing, and it must invoke the rendered hook command itself and wait for completion before continuing.
  • templates/commands/*.md (all 9 command templates): added the same dispatch-contract bullet after every mandatory-hook block (pre- and post-execution, 18 sites), and tightened the Done When checklist — emitting EXECUTE_COMMAND: alone does not count as dispatched.
  • extensions/EXTENSION-API-REFERENCE.md: documented the dispatch contract so extension authors don't assume an external dispatcher will pick up the directive.

Backward compatibility

The EXECUTE_COMMAND: / EXECUTE_COMMAND_INVOCATION: directive lines are unchanged and still emitted. To be precise: I could not find a code path in this repository that currently consumes the directive — HookExecutor.execute_hook() exists but has no production callers, and the auto_execute_hooks setting is written as a default but never read for dispatch. The directive is preserved for any external or future consumer, but nothing in-repo depends on it today, which is consistent with the failure mode reported in #2730. Optional hooks are untouched: they remain a user-facing prompt with no self-dispatch mandate.

Tests

  • test_mandatory_hook_message_instructs_agent_to_dispatch — the runtime hook message contains the self-dispatch instruction with the rendered invocation.
  • test_optional_hook_message_has_no_dispatch_mandate — optional hooks stay advisory; no behavioral change.
  • test_bundled_templates_state_directive_dispatch_contract — every EXECUTE_COMMAND block in every bundled template carries the contract bullet, so a future template edit can't silently regress agent-direct invocations back to emit-and-stall.

Full tests/test_extensions.py suite passes (248 passed). The two pre-existing test_setup_tasks.py PowerShell-hint failures reproduce on clean main and are unrelated to this change.

AI disclosure

This PR was authored with AI assistance (Claude Code) code changes, tests, and description under my direction and review.

Quratulain-bilal avatar Jun 09 '26 03:06 Quratulain-bilal

closed this by accident while editing the description - reopened.

on the orchestrator question: you're right, I rechecked and couldn't find anything in the repo that actually consumes EXECUTE_COMMAND today. execute_hook() has no production callers and auto_execute_hooks gets written as a default but never read. I picked that framing up from the issue author's wording in #2730 without verifying it myself. fixed the PR description.

and yes, this was built with AI assistance (Claude Code) - code, tests and description, reviewed by me. added the disclosure to the PR body.

if the template changes are too broad I can cut it down to just the format_hook_message() change + tests and do the templates as a follow-up.

Quratulain-bilal avatar Jun 09 '26 16:06 Quratulain-bilal

Please address Copilot feedback

mnriem avatar Jun 09 '26 23:06 mnriem

Yes, the template changes are way too broad. We should look into a fix with a smaller surface

mnriem avatar Jun 11 '26 12:06 mnriem

makes sense, agree it's too broad.

while trying to shrink it I found the reason it spread: format_hook_message() in extensions.py isn't actually reached at runtime its only caller is check_hooks_for_event(), which nothing in the CLI or agent path calls. the hook block is emitted by the agent following the command-template instructions (it reads .specify/extensions.yml and outputs the block itself), so the templates are where the behavior actually lives hence all 9.

two smaller options: one line in the mandatory-hook instruction of each template, no example-block edits - still 9 files but a tiny diff each or fix just plan as the pattern, then the rest in a follow-up

any preference on direction?

Quratulain-bilal avatar Jun 13 '26 12:06 Quratulain-bilal

Pick one and lets see how it looks

mnriem avatar Jun 16 '26 13:06 mnriem

went with option 1 - one instruction line after each mandatory-hook block, example blocks untouched. rebased on current main so the diff is clean: 9 files, 18 lines.

Quratulain-bilal avatar Jun 17 '26 06:06 Quratulain-bilal

@mnriem this is ready for another look when you have time - went with the minimal option (one line per template, examples untouched, 9 files / 18 lines). no rush, just flagging it's been sitting since the rebase.

Quratulain-bilal avatar Jun 24 '26 17:06 Quratulain-bilal

Please address Copilot feedback

mnriem avatar Jun 24 '26 20:06 mnriem

addressed both Copilot points:

  • the dispatch line no longer leans on the literal /{command} id. it now tells the agent to run the hook the way it'd run the command itself in this agent/session, and calls out that the invocation can differ from the {command} id (e.g. /skill:speckit-... or $speckit-... in skills mode). so the id-vs-invocation distinction is explicit now.
  • fixed the PR description - it was stale from the earlier broad version and claimed runtime/API-ref/test changes that aren't in this minimal cut. the change is now purely the template instruction lines, and the description says exactly that.

also rebased on current main, diff is 10 files / 20 lines.

Quratulain-bilal avatar Jun 25 '26 11:06 Quratulain-bilal

Thank you!

mnriem avatar Jun 25 '26 12:06 mnriem