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

feat(claude): run /analyze in a forked subagent

Open echarrod opened this issue 6 months ago • 7 comments

Summary

Adds context: fork + agent: general-purpose to the generated /analyze Claude skill so it runs in an isolated subagent context.

Refs #752 — scoped narrowly to /analyze rather than every command, because most spec-kit commands are interactive or have side effects and rely on conversation continuity.

Why /analyze specifically

/analyze is the strongest fit for context: fork in the spec-kit command set:

  • Explicitly read-only. The template states STRICTLY READ-ONLY: Do not modify any files.
  • Heavy inputs that collapse to a short report. Loads spec.md, plan.md, tasks.md, and the constitution; emits a compact findings table.
  • No conversation history needed. The task is fully described by the skill content and $ARGUMENTS.
  • Matches the docs' canonical example. Run skills in a subagent shows exactly this pattern.

Net effect: the artefact contents stay inside the subagent. The main session only sees the report — the user keeps a clean conversation context for follow-up work, which is the concern raised in #752.

Why general-purpose over Explore

Claude Code's Explore agent description warns it's unsuitable for "cross-file consistency checks, design-doc auditing, or open-ended analysis" — which is exactly what /analyze does. general-purpose has the same read-only tool surface without that caveat.

Why per-command opt-in (not a global flag)

Other spec-kit commands aren't safe to fork:

  • /specify, /plan, /tasks, /implement, /clarify, /checklist, /constitution, /taskstoissues — all interactive, accept follow-up turns, or write files based on conversation context. Forking would break them.

So this PR introduces a FORK_CONTEXT_COMMANDS mapping mirroring the existing per-command ARGUMENT_HINTS pattern, with analyze as the single entry. New commands can opt in if they fit the same criteria.

Implementation

  • FORK_CONTEXT_COMMANDS: dict[str, dict[str, str]] — stem → frontmatter overrides.
  • The per-command frontmatter injection (both argument-hint and fork context) lives in ClaudeIntegration.post_process_skill_content() — the single method every skill-generation path calls (setup(), preset overrides, and extensions). It derives the command stem from the frontmatter name: and injects each key/value via the idempotent _inject_frontmatter_flag / inject_argument_hint helpers. This means a preset or extension that regenerates speckit-analyze keeps context: fork instead of silently dropping it — previously the injection lived only in setup(), so those paths bypassed it. setup() is now just the post-process loop.
  • Non-Claude integrations are untouched — context: fork is Claude-Code-specific frontmatter.

Tests

Seven tests under TestClaudeForkContext:

  • test_analyze_skill_runs_in_forked_subagent — speckit-analyze has context: fork + agent: general-purpose
  • test_other_skills_do_not_fork — no other speckit-* skill gains context or agent
  • test_fork_flags_inside_frontmatter — keys land in frontmatter, not body
  • test_fork_injection_idempotent — re-running setup doesn't duplicate keys
  • test_fork_context_injected_via_post_process — the preset/extension path (post_process_skill_content) also injects fork context + argument-hint
  • test_post_process_no_fork_for_other_skills — non-fork skills gain nothing via that path
  • test_post_process_fork_idempotent — re-running post_process doesn't duplicate keys

Test plan for reviewers

  1. uv run --extra test python -m pytest tests/integrations/test_integration_claude.py -v — 37 tests pass (30 existing + 7 fork-context)
  2. uv run specify init test-proj --ai claude --script sh --no-git --ignore-agent-tools and inspect test-proj/.claude/skills/speckit-analyze/SKILL.md — frontmatter should contain context: fork and agent: general-purpose
  3. Run /speckit-analyze in a populated spec-kit project — output should be a single summarised report; the artefact contents should not appear in the main conversation

echarrod avatar May 11 '26 11:05 echarrod

Please deliver this as a preset as per https://github.com/github/spec-kit/tree/main/presets

mnriem avatar May 11 '26 20:05 mnriem

Please deliver this as a preset as per https://github.com/github/spec-kit/tree/main/presets

Hi @mnriem, my thinking was that this would be useful behaviour to all, regardless of whether you've opted in to the preset or not. Are you concerned with it becoming default behaviour?

echarrod avatar May 12 '26 17:05 echarrod

It is a matter of traceability. Not every one would want sub agents to get triggered automatically. Hence why it would be better as a preset so one can opt into it. And also making sure that the core stays agnostic as much as possible

mnriem avatar May 12 '26 19:05 mnriem

Pushed ebf98c1 to close a gap in this change: the fork-context (and argument-hint) frontmatter was injected only in ClaudeIntegration.setup(), so skills generated through post_process_skill_content() directly - presets and extensions - bypassed it. A preset overriding speckit-analyze, for example, would silently drop context: fork.

The per-command injection now lives in post_process_skill_content() - the one method every generation path calls - keyed off the frontmatter name:, so setup(), presets, and extensions stay consistent. setup() is now just the post-process loop. Added 3 regression tests for the post_process path (injection applied, non-fork skills untouched, idempotency).

echarrod avatar Jun 11 '26 11:06 echarrod

Please resolve conflicts

mnriem avatar Jun 11 '26 16:06 mnriem

Please resolve conflicts

Resolved now 👍

echarrod avatar Jun 16 '26 09:06 echarrod

Please address Copilot feedback

mnriem avatar Jun 16 '26 19:06 mnriem

Hi @mnriem, addressed now 🙏

echarrod avatar Jun 18 '26 14:06 echarrod

Thank you!

mnriem avatar Jun 19 '26 15:06 mnriem