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

feat(workflows): add JSON output for workflow run resume and status

Open doquanghuy opened this issue 5 months ago • 6 comments

Description

Closes #2811.

Adds an opt-in --json flag to workflow run, workflow resume, and workflow status, giving automation a machine-readable view of workflow terminal states (completed / paused / failed / aborted) and the run id, instead of scraping human prose or reading internal state.json files.

// specify workflow run wf.yml --json
{ "run_id": "662bf791", "workflow_id": "smoke", "status": "paused",
  "current_step_id": "ask", "current_step_index": 0 }

status --json additionally reports per-step states (single run) or a runs list (no run id).

What changed

  • --json option on the three commands; a shared _workflow_run_payload() builder for run/resume and a shared _emit_workflow_json() emitter.
  • JSON is emitted via plain stdout (not console.print) so Rich markup, highlighting, and line-wrapping can never alter the object — verified by a test asserting the output contains no ANSI/markup and round-trips. (This is intentionally not routed through Rich the way version --features --json is.)
  • When --json is set, the step-progress callback and banners are suppressed so stdout is a single parseable object.
  • Reference docs updated (docs/reference/workflows.md).

Compatibility

  • Default (no --json) output and exit codes are unchanged.
  • Usage/lookup errors (e.g. run not found) remain human-readable on the existing path — the JSON object represents a run outcome, not error reporting. Happy to extend to JSON errors if you'd prefer.

Testing

  • [x] Tested locally with uv run specify --help
  • [x] uv sync --extra test && uv run pytest — 3313 passed, 40 skipped; ruff check src/ clean
  • [x] Manually verified run/status (single + list)/resume --json produce parseable output and that default output is unchanged

Tests in tests/test_workflows.py::TestWorkflowJsonOutput cover completed/paused run JSON, status (single + list), resume JSON, default output staying human (non-JSON), and that --json output carries no markup/ANSI.

AI Disclosure

  • [ ] I did not use AI assistance for this contribution
  • [x] I did use AI assistance (describe below)

Used Claude to implement the flag, tests, and docs. Behaviour was verified locally and the diff reviewed before submission.


Per CONTRIBUTING.md (new CLI arguments should be agreed first), #2811 is the discussion issue and this PR is the concrete proposal — happy to adjust the payload shape or hold it. @mnriem a steer on whether this is wanted and on the field set would be appreciated.

doquanghuy avatar Jun 02 '26 10:06 doquanghuy

Thanks @Copilot — fixed in the latest push.

The --json example in docs/reference/workflows.md now shows the exact pretty-printed output the CLI emits (json.dumps(..., indent=2), real key order) in a ```json block, instead of the wrapped single-line form. Verified the documented object parses and matches the emitter's field order.

@mnriem ready for review.

doquanghuy avatar Jun 03 '26 04:06 doquanghuy

Pushed an update that brings the branch up to date with main and addresses Copilot's review.

Up to date with main. Resolved the overlap with the new resume --input work (#2815): workflow resume now carries both --input and --json, and run/resume share main's _parse_input_values helper. Full uv run pytest is green (3472 passed), ruff check src/ and markdownlint clean.

Copilot's notes.

  • status <run_id> --json omitted current_step_index. The single-run status payload is now built on the shared _workflow_run_payload(state) ({**_workflow_run_payload(state), created_at, updated_at, steps}), so the common step-position fields are byte-identical across run/resume/status and can't drift again. Added an assertion locking that equivalence.
  • Docs example output format. The example is pretty-printed (indent=2) to match exactly what's emitted, with a note that it's plain stdout, no Rich markup.
  • wf.yml → "workflow_id": "wf" could read as filename-derived. Switched the example to my-pipeline.yml with "workflow_id": "build-and-review" and a one-line note that workflow_id is the YAML workflow.id, not the file name.

The payload shape is otherwise unchanged from the original proposal. @mnriem a steer on whether this is wanted, and on the field set, would be appreciated.

doquanghuy avatar Jun 03 '26 17:06 doquanghuy

Pushed abfa237 addressing Copilot's latest review.

--json stdout could be corrupted by step output. Suppressing the banner and the step-start callback wasn't enough — steps still write to stdout while the engine runs (the gate step prints its prompt; the prompt step runs a CLI subprocess that inherits the stdout fd). I now wrap engine.execute()/engine.resume() in a file-descriptor-level redirect (dup2, fd 1 → fd 2) whenever --json is set, so both Python-level prints and inherited-fd subprocess output go to stderr while stdout carries only the single JSON object. Step progress stays visible on stderr; status doesn't run the engine, so it's untouched. Default (non---json) behaviour is unchanged.

Tests cover both pollution channels (a Python print and a real subprocess) via fd-level capfd capture, plus the inactive no-op path. Docs note the stdout/stderr split. Full uv run pytest green (3474 passed), ruff check src/ and markdownlint clean.

@mnriem ready for another look when you have a moment.

doquanghuy avatar Jun 03 '26 21:06 doquanghuy

Pushed ea95653: fixed the stray \s escape in the redirect helper's docstring and its test comment (now reads "prints"). No behaviour change. pytest green, ruff clean. @mnriem ready for another look.

doquanghuy avatar Jun 03 '26 22:06 doquanghuy

Please resolve conflicts

mnriem avatar Jun 04 '26 12:06 mnriem

@mnriem conflicts resolved — merged latest main (9575f2a), PR is MERGEABLE again.

The only conflict was in docs/reference/workflows.md: main rewrote the "project required" note to document running a workflow from a local file outside a project. I kept that updated note and re-attached the --json example/description above it. Source and tests auto-merged cleanly.

Full uv run pytest green (3507 passed), ruff check src/ and markdownlint clean.

doquanghuy avatar Jun 04 '26 12:06 doquanghuy

Thank you!

mnriem avatar Jun 04 '26 16:06 mnriem