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

feat: surface gate detail in the workflow run/resume --json payload

Open doquanghuy opened this issue 5 months ago • 2 comments

Description

Reference implementation for #2964 — for discussion, direction welcome.

When a run pauses at a gate, the --json outcome now carries a gate block (step_id / message / options / choice) so orchestrators can detect "human review needed" and present the options without parsing the human-facing stream. Two small pieces:

  1. The engine records each step's type in the run state's step results (one added line in step_data — previously the type was not recoverable from state).
  2. _workflow_run_payload adds the gate block via a _gate_outcome helper when the run's current step is a gate. choice populates when the outcome ends at the gate with a decision recorded (e.g. an interactive rejection with on_reject: abort → a failed payload carrying "choice": "reject"; an on_reject: retry pause likewise). A mid-flow approval proceeds past the gate, so the block clears — by design. Non-gate runs and runs that end elsewhere are unchanged — no gate key, payload byte-identical to today.

The issue lists alternatives (a generic paused_step block; a dedicated status value) — happy to rework toward either.

Testing

  • [x] Ran existing tests with uv sync && uv run pytest — full suite 3727 passed
  • [x] Two new CLI-level tests (TestWorkflowRunGateOutcomeJson): a gate pause carries the exact block (CliRunner stdin is non-TTY, so the gate pauses); a completed run has no gate key — the gate-pause test is red against current main, green with the change (verified both directions)
  • [x] uvx ruff check src/ — clean
  • [x] Tested locally with uv run specify --help
  • [ ] Tested with a sample project (covered by the CLI-level tests, which drive a real gate workflow through workflow run --json)

AI Disclosure

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

Code, tests, and this description were authored with AI assistance (Claude); verified by running the repo's test suite and ruff locally in both red and green directions.

doquanghuy avatar Jun 12 '26 17:06 doquanghuy

@mnriem when you have a moment, would appreciate your thoughts on the direction here — the issue lists the alternatives considered, and I'm happy to rework toward whichever shape fits Spec Kit best.

doquanghuy avatar Jun 12 '26 17:06 doquanghuy

Please address Copilot feedback

mnriem avatar Jun 16 '26 20:06 mnriem

@mnriem Thanks for the review — addressed the Copilot feedback:

  • _gate_outcome() now only surfaces the gate block while the run is actually paused (guards on status == paused). Since RunState.current_step_id isn't cleared on completion, a completed/failed run whose last step was a gate no longer leaks stale gate detail in run/resume/status --json.
  • Hardened the _run_json test helper to assert CLI success before JSON-parsing, and added direct coverage for the suppression guard.

Full suite green; ruff check src/ clean. Ready for another look.

doquanghuy avatar Jun 17 '26 02:06 doquanghuy

@mnriem Pushed 5fd0f85 addressing the latest Copilot round:

  • Abort path now surfaces the gate block. _gate_outcome() emits the gate detail for aborted runs too, not only paused. Abort is the only path that sets ABORTED (gate rejection with on_reject: abort) and it leaves current_step_id on that gate, so an orchestrator can read the recorded choice for the stop. completed/failed stay suppressed.
  • Stable JSON schema. message is coerced to a string — GateStep only coerces it for expression interpolation, so a non-string YAML literal could otherwise leak into the payload.
  • Tests: added a CLI-level aborted-path test (test_gate_abort_carries_gate_block, asserts status == aborted and choice == reject), a message-coercion test, and extended the suppression test to allow aborted. Shared the run helper via _invoke_json to avoid duplicating invoke boilerplate.

doquanghuy avatar Jun 17 '26 13:06 doquanghuy

Please address Copilot feedback and rebase on upstream/main

mnriem avatar Jun 17 '26 15:06 mnriem

@mnriem Pushed 5f60408 for the latest Copilot round:

  • The gate-abort test parsed stdout without first asserting the CLI exited cleanly, so an invoke failure would have surfaced as an opaque JSON decode error. It now routes through _run_json (which asserts exit_code == 0 before parsing), and I dropped the now-redundant _invoke_json helper — a gate abort emits the payload and returns, so the run exits cleanly.

Full tests/test_workflows.py green (212 passed).

doquanghuy avatar Jun 17 '26 15:06 doquanghuy

@mnriem Pushed 3e303fb addressing the latest Copilot round:

  • Run-helper assertion message now uses result.output instead of result.stdout. Under --json, step output is redirected off stdout, so a failing run's useful diagnostics live on result.output; the JSON parse still reads stdout. This also brings _run_json in line with the other CLI tests in the file.
  • StepContext.steps docstring updated from the old 5-key entry shape to the canonical 7-key shape the engine actually persists (type, integration, model, options, input, output, status), so step authors and debuggers see the real record.

Full tests/test_workflows.py green (212 passed); ruff clean.

Heads up: GitHub now shows this branch as conflicting with main — that's from #2959 and #2963 having merged (they touch the same run-command / engine.py / test regions). I've left the branch as-is rather than rebase a public PR unprompted; happy to rebase onto current main and resolve if you'd like, just say the word.

doquanghuy avatar Jun 17 '26 17:06 doquanghuy

@mnriem Rebased onto current main and resolved the conflict (force-pushed 24d6e85). The branch is now MERGEABLE.

What the rebase touched:

  • Conflict was a clean "keep both": #2959 added TestWorkflowRunExitCodes exactly where this PR adds TestWorkflowRunGateOutcomeJson — both classes are preserved in full. _workflow_run_payload now (correctly) calls _gate_outcome and keeps _run_outcome_exit_code; the step_data["type"] field this PR relies on sits cleanly alongside the rest.
  • One real semantic interaction with #2959: a gate abort now emits the --json payload and then exits non-zero (aborted → 1). I gave the test helper an expected_exit param (default 0) so the abort test asserts exit 1 while paused/completed stay at 0 — single shared helper, no duplicated invoke boilerplate.

All prior Copilot fixes are intact (paused/aborted guard, message coercion, result.output assert message, 7-key StepContext.steps docstring). Full specify_cli test suite green locally (3879 passed); ruff clean.

doquanghuy avatar Jun 17 '26 17:06 doquanghuy

@mnriem Pushed d322cf5 for the latest Copilot comment:

  • Backward-compatible gate detection. A run paused by an older version has no persisted step type, so _gate_outcome would have dropped its gate block on resume. Added _is_gate_step: it prefers the type field, and when type is absent falls back to the gate's unique output signature (on_reject, which only GateStep writes). A record carrying a different known type is still not treated as a gate.
  • Options normalized to strings. Mirroring the existing message coercion, options is now normalized to a list of strings so an unvalidated workflow with non-string options can't destabilize the JSON schema.
  • Tests: options coercion, type-less gate detection (resume path), and a type-less non-gate negative case.

Full tests/test_workflows.py green (291 passed); ruff clean.

doquanghuy avatar Jun 17 '26 18:06 doquanghuy

@mnriem Pushed a97cd18 for the latest Copilot comment:

  • Non-list options now normalized too. The previous pass only mapped a list, returning the raw value for any other shape — so a scalar or tuple leaked through unnormalized, contradicting the list[str] intent. Extracted _normalize_gate_options: None stays None; a list/tuple maps each element through str; any other scalar becomes a single-element list (a bare string is treated as one option, never iterated character-by-character). The emitted schema is now always list[str] | None.
  • Tests extended to cover list, tuple, bare string, numeric scalar, and None.

Full tests/test_workflows.py green (291 passed); ruff clean.

doquanghuy avatar Jun 17 '26 19:06 doquanghuy

@mnriem Pushed c4297a0 for the latest Copilot round:

  • choice now normalized too. _gate_outcome normalized message and options but passed choice through as-is — an unvalidated gate can record a non-string choice, contradicting the stable-schema rationale. It is now coerced to str | None (None still means "no decision yet"), consistent with the other two fields. Added a focused choice-coercion test (None / string / non-string).
  • Portable plain-gate test. The no-gate test workflow used run: "true", which fails under cmd.exe on Windows (ShellStep uses shell=True). Switched to the cross-platform run: "exit 0", matching the exit-code suite's workflows. (Scoped to the one occurrence this PR introduced; two pre-existing run: "true" lines elsewhere are upstream and left untouched to keep the diff tight.)

Full tests/test_workflows.py green (292 passed); ruff clean.

doquanghuy avatar Jun 17 '26 19:06 doquanghuy

@mnriem Pushed be970e7 addressing the latest Copilot round (pyproject.toml / CHANGELOG.md).

Both findings traced to a single stray commit on the branch: chore: bump version to 0.11.2 (authored by github-actions[bot]), which had leaked in and bumped pyproject.toml 0.11.2.dev0 → 0.11.2 plus added a full 0.11.2 release-note block to CHANGELOG.md. That's release automation, not part of this feature.

  • Rebased onto current upstream/main and dropped the bot bump commit. Upstream has since cut 0.11.2 and moved to 0.11.3.dev0; the branch now inherits that verbatim. The PR diff no longer touches pyproject.toml or CHANGELOG.md at all — it's back to just the gate-JSON feature (4 files: __init__.py, workflows/base.py, workflows/engine.py, tests/test_workflows.py).
  • All prior Copilot fixes are intact (paused/aborted guard, message/options/choice coercion, backward-compat gate detection, result.output assert message, 7-key StepContext.steps docstring, portable exit 0 plain-gate test).

Full tests/test_workflows.py green (296 passed).

doquanghuy avatar Jun 19 '26 18:06 doquanghuy

Thank you!

mnriem avatar Jun 22 '26 12:06 mnriem