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

fix(cursor-agent): enable headless CLI dispatch end-to-end (-p --trust --approve-mcps --force + Windows .cmd shim resolution)

Open One-TheOnly opened this issue 6 months ago • 6 comments

## Summary

Enable specify workflow run to dispatch the cursor-agent CLI headlessly end-to-end on Windows, matching the way it already dispatches claude / codex / gemini. Existing in-IDE skill flow (via .cursor/skills/speckit-*/SKILL.md) is preserved exactly — this PR only adds the CLI dispatch path on top.

Fixes #2629.

Integration config (final): requires_cli remains False so specify init --ai cursor-agent works without the CLI installed (IDE/skills flow unchanged). Workflow dispatch is enabled by overriding build_exec_args() — the same pattern as CopilotIntegration — with CLI presence enforced at dispatch time via shutil.which, not as a hard specify init precheck.

Problem

Before this PR, specify workflow run speckit --input integration=cursor-agent ... fails immediately with:

Cannot dispatch command 'speckit.specify': integration 'cursor-agent' CLI not found or not installed.
Install the CLI tool or check 'specify integration list'.

even when cursor-agent is installed and healthy (cursor-agent --version returns 2026.05.16-0338208).

Three separate gaps each independently block the headless path; this PR fixes all three.

Commits

Functional fixes (three gaps)

1) 1c55988 fix(cursor-agent): enable CLI dispatch via -p --trust headless mode

CursorAgentIntegration had requires_cli: False and did not override build_exec_args, so SkillsIntegration.build_exec_args returned None, the dispatcher in workflows/steps/command/__init__.py::_try_dispatch returned None, and the misleading CLI-missing error was surfaced. The CLI is actually fully headless-capable:

  • -p / --print — print mode, "Has access to all tools, including write and shell"
  • --output-format text | json | stream-json
  • --model <name>
  • --trust — "Trust the current workspace without prompting (only works with --print/headless mode)"

Without --trust, the CLI exits non-zero with âš  Workspace Trust Required.

This commit (superseded on requires_cli by ff07cc0 below — kept here for history):

  • config["install_url"]: None → https://docs.cursor.com/en/cli/overview
  • First override of build_exec_args to emit [self.key, "-p", "--trust", prompt, …] (later commits add --approve-mcps --force; ff07cc0 sets requires_cli back to False for IDE-first init).

2) 454d3d0 fix(integrations): resolve .cmd/.bat shims before subprocess.run

After commit (1), the dispatcher still failed on Windows with FileNotFoundError [WinError 2]. Root cause:

  • shutil.which("cursor-agent") returns cursor-agent.CMD (honoring PATHEXT).
  • subprocess.run(["cursor-agent", ...]) calls CreateProcess, which does not consult PATHEXT, so the bare command name fails to launch.

The same bug affects every .cmd/.bat-wrapped CLI on Windows (verified for codex as well; only .exe CLIs like claude were unaffected).

Fix in IntegrationBase.dispatch_command: resolve exec_args[0] via shutil.which before subprocess.run. No-op on POSIX (no PATHEXT to consult); transparent fix on Windows.

3) 595bcab fix(cursor-agent): inject --approve-mcps --force for headless MCP/tool access

After commits (1)+(2), subprocess.run succeeds and cursor-agent starts, but the first MCP call (e.g. to read a DingTalk PRD) fails with The tool call was rejected. I can't retrieve the document name without permission to call the tool. Two more flags are needed:

  • --approve-mcps — without it, every MCP server stays not loaded (needs approval) and tool calls are silently dropped.
  • --force — without it, the agent halts on the first tool-call approval prompt; the workflow returns a misleading non-zero exit. With it, cursor-agent matches the implicit "trusted environment" semantics that claude -p and codex --exec already have by default — the right semantics for an unattended specify workflow run.

Final argv after all three commits:

args = [self.key, "-p", "--trust", "--approve-mcps", "--force", prompt]
if model: args.extend(["--model", model])
if output_json: args.extend(["--output-format", "json"])

Review feedback (ff07cc0, 422b250, 6e3819c)

4) ff07cc0 refactor(cursor-agent): express dispatch support via build_exec_args() instead of requires_cli

Addresses Copilot review: keep requires_cli: False so specify init does not hard-fail for IDE-only users. Remove the requires_cli guard inside build_exec_args(); dispatch support is signalled by overriding build_exec_args() (mirrors CopilotIntegration). Headless argv shape unchanged: -p --trust --approve-mcps --force.

5) 422b250 test(cursor-agent): use urlparse hostname check and cover dispatch without requires_cli

Addresses CodeQL: replace "cursor.com" in url substring check with urlparse(...).hostname comparison; add regression test that dispatch works with requires_cli=False.

6) 6e3819c — docstring: mention --approve-mcps --force in module-level dispatch description (Copilot doc consistency).

End-to-end verification (real DingTalk PRD, just a URL as input)

Repro on Windows 10 + cursor-agent 2026.05.16-0338208:

cd D:\Code\MBM\MBM-Backend     # real Maven multi-module Java project
specify workflow run speckit `
  --input "spec=https://alidocs.dingtalk.com/i/nodes/<dingtalk-prd-node>" `
  --input "integration=cursor-agent" `
  --input "scope=full"

Two independent runs (df8c4b9b, 388d3c15) both reach the review-spec gate:

specify step:
  dispatched=true, exit_code=0, status=completed, duration ~156s / ~147s

agent behaviour (autonomously, from a URL-only spec input):
  - calls dingtalk-doc MCP get_document_content to fetch the PRD
  - reads project constitution + existing specs/ layout
  - decides specs/ subpath itself (e.g. specs/tooling/mold-assembly/202605/<feature>/)
  - produces spec.md (31.5 KB) + checklists/requirements.md
  - updates .specify/feature.json and specs/menu-dictionary.md
  - marks 3 [NEEDS CLARIFICATION] items for Gate 1 review

next step (review-spec gate):
  (gate started; status would be `paused` on a normal completion)

(Both runs above were eventually cancelled by Ctrl+C while the gate step was just starting, which surfaced an [Errno 22] Invalid argument from the interrupted subprocess.run. That was an artifact of my cancellation, not a gate-step bug — review-spec is a gate step and its designed terminal status is paused. Independent of this PR either way.)

Backward compatibility

  • In-IDE skill flow (.cursor/skills/speckit-*/SKILL.md, --skills default) — unchanged.
  • multi_install_safe, context_file, registrar_config, options() — unchanged.
  • POSIX behaviour — shutil.which lookup is a no-op for already-absolute exec_args[0], and the --approve-mcps --force flags exist on Linux/macOS cursor-agent identically.
  • catalog.json, workflow.yml, templates — untouched.
  • Other integrations (claude, codex, gemini) — their build_exec_args paths are not touched; the only shared change is base.py::dispatch_command adding the shutil.which shim resolution, which only takes effect when shutil.which returns a different path than the input (i.e. .cmd/.bat shim case on Windows).

Testing

$ uv run python -m pytest tests/integrations/test_integration_cursor_agent.py -v
============================= 43 passed in 3.72s ==============================

$ uv run python -m pytest -q
========== 12 failed, 2867 passed, 109 skipped in 372.85s (0:06:12) ===========
  • 44 cursor-agent integration tests pass (CI green on this branch).
  • New tests pin:
    • requires_cli is False (IDE-first init) and install_url is set; dispatch via build_exec_args() override
    • install_url hostname validated with urlparse (not substring match)
    • Exact argv layout [cursor-agent, -p, --trust, --approve-mcps, --force, …] (default + text-output + with-model variants)
    • All four mandatory headless flags are always present together
    • Hyphenated skill invocation (/speckit-plan not /speckit.plan)
    • .cmd shim resolution: subprocess receives full cursor-agent.CMD path, not bare cursor-agent
    • Passthrough when shutil.which returns None
  • Existing tests/test_workflows.py::TestCommandStep::test_dispatch_with_mock_cli and test_dispatch_failure_returns_failed_status updated to patch shutil.which at the base.py layer (was previously only patching the dispatcher layer, which made the test environment-sensitive — passed on CI with no claude on PATH, failed locally with claude installed).
  • Full repo: 2867 passed, 0 regressions from this PR. The 12 remaining pre-existing failures are unrelated Windows symlink privilege issues (WinError 1314) requiring elevated runner — not introduced or affected by this PR.

Out of scope (deliberately)

  • The misleading "CLI not found or not installed. Install the CLI tool." error message itself (it conflates 5 distinct dispatch failure modes into one). Worth a separate PR to split per-failure-mode messages — happy to do that as a follow-up if maintainers want.
  • Adding cursor-agent to bundled speckit workflow's requires.integrations.any list (currently ["copilot", "claude", "gemini"]). Not strictly required — specify workflow run does not enforce that list strictly today — but worth a follow-up so specify workflow info speckit is consistent with the actual support matrix.
  • docs/reference/integrations.md page for cursor-agent (none exists today; happy to add if wanted).
  • Adding a speckit.git.feature step to speckit workflow.yml (current workflow doesn't auto-create feature branches; that's by design but worth surfacing as an opt-in for teams that want branch-per-feature).

One-TheOnly avatar May 19 '26 06:05 One-TheOnly

Please address code scanning resulsts

mnriem avatar May 26 '26 12:05 mnriem

Thanks for the review! I've addressed both points in ff07cc0 + 422b250:

  • Copilot (requires_cli): reverted requires_cli to False so specify init no longer hard-fails for IDE-only users. Dispatch support is now expressed by overriding build_exec_args() (same shape as CopilotIntegration), and the requires_cli guard in build_exec_args() is removed.
  • CodeQL (URL substring): replaced the "cursor.com" in url substring check with an exact urlparse(...).hostname comparison, plus a regression test.

pytest tests/integrations/test_integration_cursor_agent.py is green (44 passed) and ruff check src/ is clean. Could you re-run the workflows when you get a chance?

One-TheOnly avatar May 29 '26 05:05 One-TheOnly

Please address Copilot feedback

mnriem avatar Jun 01 '26 15:06 mnriem

This autofix suggestion was applied.

One-TheOnly avatar Jun 03 '26 08:06 One-TheOnly

Can you please update the PR description as per Copilot feedback

mnriem avatar Jun 03 '26 21:06 mnriem

@mnriem Updated the PR description to match the current implementation:

  • requires_cli stays False (IDE-first specify init; dispatch via build_exec_args() override, same as CopilotIntegration).
  • Documented review commits ff07cc0, 422b250, and 6e3819c (docstring flags).
  • Corrected the Testing section (no longer claims requires_cli is True).

CI on 6e3819c is all green. Ready for another look when you have time.

One-TheOnly avatar Jun 04 '26 01:06 One-TheOnly