← Back to Pull requests

fix(step): report steps stopped by Ctrl-C as cancelled instead of failed #1643

Closed
unknown closed this pull request without merging on Oct 04, 2026 02:30 UTC
View on GitHub
unknown unknown commented on Oct 04, 2026 01:24 UTC
Author

<!-- entire-trail-link-start -->
https://entire.io/gh/jdx/hk/trails/219
<!-- entire-trail-link-end -->

Problem

Interrupting hk check with Ctrl-C reported the interrupted step as failed, as if its command had errored. Automation reading --format jsonl or the MCP result saw a failed step (and a failed run) for something the user stopped on purpose.

Reproduction: one step running sleep, hk --format jsonl check --all, then SIGINT.

Before (origin/main, HK_LIBGIT2=1 and 0 identical):

exit=1
step_completed  status: "failed"
run_completed   {"status":"failed","failure":"sleep 3133..."}   steps: slow=failed
human summary:  ✗ slow – ERROR

After:

exit=1
step_completed  status: "cancelled"
run_completed   {"status":"cancelled","failure":null}           steps: slow=cancelled
human summary:  ⚠ slow – cancelled

(The step-event values above are copied from real runs of the two binaries; the summary lines are abbreviated from the terminal output.)

What changes

  • One classifier, is_cancelled_run_error (a cancellation error while the run's cancel token is cancelled), decides that an error is a cancellation. The join_jobs failure callback, the per-job error path, and the step group's error fallback use it through status_error: a cancellation ends the step cancelled and never calls mark_step_failed.

  • A cancelled job does not settle its step on its own: a sibling job's real failure wins over the cancellation in either order, so the step and run stay failed and keep the failure text.

  • A run its user interrupted reports status cancelled with no failure text. The MCP server reports a run whose result says cancelled as cancelled, matching a run stopped by cancel_run.

  • The human summary shows cancelled instead of a red ERROR for the interrupted step (fail-fast aborts still say aborted).

  • docs/agents.md (and the sea shanty page, restamped) now state the cancelled step and run statuses. The cancelled status already existed for steps stopped by a fail-fast abort; no schema change.
  • Unchanged: the process exit status is 1 with command was cancelled in stderr, exactly as on main (measured on both). The hook-level hk ERROR check: hook finished with error log line is still printed. Real failures, --no-fail-fast, allowed failures, diagnostics recording and dependency wake-ups from #1642 behave as before. A real failure that happened before the Ctrl-C still reports failed; only the interrupted step is cancelled.

    Validation

  • New test/ctrl_c_cancelled.bats under HK_LIBGIT2=1 and 0: interrupted sleeping step is cancelled with a cancelled run, no failed steps, command was cancelled in stderr and exit 1; a real failure still reports failed; a Ctrl-C after a real failure (--no-fail-fast) keeps bad failed and cancels only slow. Tests 1 and 3 fail against the main binary and pass here.

  • New unit tests for the classifier and the step status transitions (cancellation is not errored/failed; ordinary errors still are, even during a cancel; a cancel after a real failure keeps it).

  • fail_fast_config, diagnostics, depends, allow_failure, failure_output, structured_output, mcp_cancel_run bats files: all ok, counts equal plans, under both HK_LIBGIT2 values. cargo test step::, mcp, --bin hk cli:: pass; fmt and clippy --all-targets -D warnings clean.
  • Not verified: a Windows run, and an MCP-level test of an externally interrupted hk (the cancelled mapping in mcp.rs is exercised only by reading; mcp_cancel_run covers the cancel_run path). The hk agent stop-hook command is not on this branch, so it was not touched.

    🤖 Generated with Claude Code

    AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Changes run/step outcome classification across execution, structured JSONL, and MCP; behavior is well-tested but affects automation that interpreted interrupt exit codes as failures.

    > Overview

    Ctrl-C and fail-fast stops now surface as cancelled in structured output and MCP, instead of treating user-interrupted work like command failures.

    > A shared is_cancelled_run_error classifier (cancellation error while the run’s cancel token is active) drives status_error paths in steps, jobs, and join_jobs, so interrupted steps are aborted/cancelled and not marked failed. Real failures still win when they occur before or alongside a cancel (including sibling job ordering). emit_run omits failure for user-cancelled runs; the MCP server uses final_run_status so hk’s cancelled run_result maps to run status cancelled even on non-zero exit.

    > Agent docs describe the new step/run semantics. test/ctrl_c_cancelled.bats plus unit tests cover Ctrl-C, failure-before-interrupt, and MCP status mapping.

    > <sup>Reviewed by Cursor Bugbot for commit 6cd121b8a2db2c91e647da184e6b631e3df59fe7. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jdx Review duration: 65m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1643"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%
    Repository