Skip to content

Commit d48dec5

Browse files
committed
Classify Ctrl-C as cancellation
1 parent a75bed6 commit d48dec5

8 files changed

Lines changed: 95 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This project follows Semantic Versioning.
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Made `Ctrl-C` report normal workflow cancellation with exit code 130 instead of a failed Codex review.
12+
913
## [0.6.5] - 2026-07-27
1014

1115
### Fixed

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ The required event catalog is:
251251
| `review_completed` | `stage=review`, `model`, `reasoning_effort`, `review_phase`, `cycle`, `status=clean\|findings\|failed`, and `duration_ms`. A classified result (`clean` or `findings`) also requires all findings counters plus `review_scope=branch_and_worktree`, `review_base` (resolved commit SHA), `review_merge_base` and `review_base_source=explicit\|open_pr\|branch_merge_base\|remote_default`; on command or classification failure these fields and counters are omitted. This is the review stage's sole completion record. |
252252
| `stage_completed` | `stage=fix-findings\|finalize\|fix-ci`, `model`, `reasoning_effort`, `status=success\|failed`, and `duration_ms`; `fix-findings` also has `review_phase` and `cycle`, while `fix-ci` has `review_phase`. A successfully parsed finalization response also requires `verdict=SUCCESS\|CI_FAILED\|FAILED`; an invocation or parsing failure uses `status=failed` and omits `verdict`. |
253253
| `step_completed` | `stage=finalize`, `model`, `reasoning_effort`, `step=commit\|push\|change_request\|ci`, and `status=success\|skipped\|failed\|unknown`. Each finalization attempt emits one record for every listed step; a step that is inapplicable or not reached is `skipped`, while an outcome that cannot be established is `unknown`. |
254-
| `run_completed` | `status=success\|findings_remaining\|operational_failure\|ci_failure`, `exit_code`, and `total_duration_ms`. For `findings_remaining`, also `checkpoint_status=committed_local\|no_changes\|not_attempted`; `committed_local` additionally requires percent-encoded `checkpoint_branch` and `checkpoint_commit`, while `not_attempted` requires `checkpoint_reason=fix_budget_exhausted\|pre_existing_changes`. |
254+
| `run_completed` | `status=success\|findings_remaining\|operational_failure\|ci_failure\|cancelled`, `exit_code`, and `total_duration_ms`. `cancelled` always has `exit_code=130`. For `findings_remaining`, also `checkpoint_status=committed_local\|no_changes\|not_attempted`; `committed_local` additionally requires percent-encoded `checkpoint_branch` and `checkpoint_commit`, while `not_attempted` requires `checkpoint_reason=fix_budget_exhausted\|pre_existing_changes`. |
255255

256256
For example:
257257

@@ -298,9 +298,10 @@ When diagnostic session logging is enabled and its record directory has been cre
298298
| Run succeeds | `22:14:05 Done (8m 45s)` |
299299
| Findings remain | `22:14:05 Stopped: review findings remain (8m 45s, exit 1)` |
300300
| Operational failure | `22:14:05 Failed due to an operational error (8m 45s, exit 2)` |
301+
| Interrupted with `Ctrl-C` | `22:14:05 Cancelled (8m 45s, exit 130)` |
301302
| CI remains red | `22:14:05 Stopped: CI is still failing (8m 45s, exit 3)` |
302303

303-
A findings summary always includes the total. It always renders `P0`, `P1` and `P2`, then appends non-zero `P3` and `Unknown` counts. Successful terminal lines omit exit `0`; failure terminal lines retain exit codes `1`, `2`, and `3`.
304+
A findings summary always includes the total. It always renders `P0`, `P1` and `P2`, then appends non-zero `P3` and `Unknown` counts. Successful terminal lines omit exit `0`; non-success terminal lines retain exit codes `1`, `2`, `3`, and `130` as applicable.
304305

305306
### Liveness
306307

@@ -319,7 +320,7 @@ Heartbeat is disabled by default, accepts `0` or a Go duration of at least `1s`,
319320

320321
### Interactive agent output view
321322

322-
In `human` mode, when both standard input and standard output are terminals and `TERM` is neither empty nor `dumb`, Code-Converge prints `Interactive view available: press i to open` once at startup and accepts one-key terminal input. Press `i` during a workflow to toggle a split view without interrupting the active Codex process. `Ctrl-C` always retains its normal interrupt behavior: it cancels the workflow and terminates its active subprocess group. The upper pane retains the workflow log; the lower pane shows arriving stdout and stderr from the active agent. Stderr lines are marked `[stderr]`.
323+
In `human` mode, when both standard input and standard output are terminals and `TERM` is neither empty nor `dumb`, Code-Converge prints `Interactive view available: press i to open` once at startup and accepts one-key terminal input. Press `i` during a workflow to toggle a split view without interrupting the active Codex process. `Ctrl-C` always retains its normal interrupt behavior: it cancels the workflow, terminates its active subprocess group, reports `Cancelled`, and exits 130 rather than reporting an operational failure. The upper pane retains the workflow log; the lower pane shows arriving stdout and stderr from the active agent. Stderr lines are marked `[stderr]`.
323324

324325
The view uses the terminal alternate screen and restores it when closed, on workflow completion, cancellation, interruption, setup failure, or panic unwinding. Each pane retains its most recent 2,000 logical lines; long lines wrap to the current terminal width. `Tab` selects a pane, arrow keys and Page Up/Down scroll it, and `End` returns it to the live tail. A view opened before an agent starts says `No active agent output`; completion leaves the final stream visible until the next agent stage.
325326

‎internal/event/event.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ func renderHuman(eventName string, fields []Field, maxCycles, maxCIRecoveries in
618618
}
619619
case "operational_failure":
620620
return fmt.Sprintf("Failed due to an operational error (%s, exit 2)", d), nil
621+
case "cancelled":
622+
return fmt.Sprintf("Cancelled (%s, exit 130)", d), nil
621623
case "ci_failure":
622624
return fmt.Sprintf("Stopped: CI is still failing (%s, exit 3)", d), nil
623625
}

‎internal/event/event_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func TestHumanEventCatalog(t *testing.T) {
7676
{"encoded checkpoint branch", "run_completed", []Field{F("status", "findings_remaining"), F("exit_code", "1"), F("checkpoint_status", "committed_local"), F("checkpoint_branch", "feature%3Da"), F("checkpoint_commit", "abc1234"), F("total_duration_ms", "525000")}, "10:04:05 Stopped: fix budget exhausted; finalization was not reached; checkpoint committed locally on feature=a at abc1234 and not pushed (8m 45s, exit 1)\n"},
7777
{"checkpoint skipped", "run_completed", []Field{F("status", "findings_remaining"), F("exit_code", "1"), F("checkpoint_status", "not_attempted"), F("checkpoint_reason", "pre_existing_changes"), F("total_duration_ms", "525000")}, "10:04:05 Stopped: fix budget exhausted; finalization was not reached; checkpoint was skipped because the worktree had pre-existing changes (8m 45s, exit 1)\n"},
7878
{"operational", "run_completed", []Field{F("status", "operational_failure"), F("exit_code", "2"), F("total_duration_ms", "525000")}, "10:04:05 Failed due to an operational error (8m 45s, exit 2)\n"},
79+
{"cancelled", "run_completed", []Field{F("status", "cancelled"), F("exit_code", "130"), F("total_duration_ms", "525000")}, "10:04:05 Cancelled (8m 45s, exit 130)\n"},
7980
{"ci failure", "run_completed", []Field{F("status", "ci_failure"), F("exit_code", "3"), F("total_duration_ms", "525000")}, "10:04:05 Stopped: CI is still failing (8m 45s, exit 3)\n"},
8081
}
8182
for _, test := range tests {

‎internal/workflow/workflow.go‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
ExitFindingsRemaining = 1
2020
ExitOperational = 2
2121
ExitCI = 3
22+
ExitInterrupted = 130
2223
)
2324

2425
type Agent interface {
@@ -63,6 +64,9 @@ func (w *Workflow) Run(ctx context.Context) int {
6364
lastCheckpoint := repository.Checkpoint{}
6465
checkpointSkipReason := ""
6566
for {
67+
if ctx.Err() != nil {
68+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
69+
}
6670
stageStarted := now()
6771
if !w.emit("stage_started", event.F("stage", "review"), event.F("model", w.stageModel("review")), event.F("reasoning_effort", w.stageReasoningEffort("review")), intField("review_phase", phase), intField("cycle", cycle)) {
6872
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
@@ -77,7 +81,9 @@ func (w *Workflow) Run(ctx context.Context) int {
7781
}
7882
review, err := w.Agent.Review(runner.WithStageContext(stageCtx, runner.StageContext{Stage: "review", ReviewPhase: phase, Cycle: cycle, Model: w.stageModel("review"), ReasoningEffort: w.stageReasoningEffort("review")}))
7983
var presentationErr error
80-
if err != nil {
84+
if err != nil && ctx.Err() != nil {
85+
presentationErr = w.Log.CompleteAgent("review cancelled")
86+
} else if err != nil {
8187
presentationErr = w.Log.CompleteAgent("review failed")
8288
} else {
8389
presentationErr = w.Log.CompleteAgent("review completed")
@@ -94,12 +100,18 @@ func (w *Workflow) Run(ctx context.Context) int {
94100
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
95101
}
96102
if err != nil {
103+
if ctx.Err() != nil {
104+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
105+
}
97106
if !w.emit("review_completed", event.F("stage", "review"), event.F("model", w.stageModel("review")), event.F("reasoning_effort", w.stageReasoningEffort("review")), intField("review_phase", phase), intField("cycle", cycle), event.F("status", "failed"), duration) {
98107
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
99108
}
100109
w.diagnostic("review failed", err)
101110
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
102111
}
112+
if ctx.Err() != nil {
113+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
114+
}
103115
status := "findings"
104116
if review.Clean {
105117
status = "clean"
@@ -128,6 +140,9 @@ func (w *Workflow) Run(ctx context.Context) int {
128140
if w.Repository != nil {
129141
clean, err := w.Repository.IsClean(ctx)
130142
if err != nil {
143+
if ctx.Err() != nil {
144+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
145+
}
131146
w.diagnostic("checkpoint status failed", err)
132147
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
133148
}
@@ -139,6 +154,9 @@ func (w *Workflow) Run(ctx context.Context) int {
139154
}
140155
initialHead, err = w.Repository.Head(ctx)
141156
if err != nil {
157+
if ctx.Err() != nil {
158+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
159+
}
142160
w.diagnostic("checkpoint head failed", err)
143161
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
144162
}
@@ -157,7 +175,9 @@ func (w *Workflow) Run(ctx context.Context) int {
157175
}
158176
err = w.Agent.FixFindings(runner.WithStageContext(stageCtx, runner.StageContext{Stage: "fix-findings", ReviewPhase: phase, Cycle: cycle, Model: w.stageModel("fix-findings"), ReasoningEffort: w.stageReasoningEffort("fix-findings")}), review.Report)
159177
presentationErr = nil
160-
if err != nil {
178+
if err != nil && ctx.Err() != nil {
179+
presentationErr = w.Log.CompleteAgent("fix-findings cancelled")
180+
} else if err != nil {
161181
presentationErr = w.Log.CompleteAgent("fix-findings failed")
162182
} else {
163183
presentationErr = w.Log.CompleteAgent("fix-findings completed")
@@ -172,6 +192,12 @@ func (w *Workflow) Run(ctx context.Context) int {
172192
w.diagnostic("render interactive view", presentationErr)
173193
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
174194
}
195+
if err != nil && ctx.Err() != nil {
196+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
197+
}
198+
if ctx.Err() != nil {
199+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
200+
}
175201
stageStatus := "success"
176202
if err != nil {
177203
stageStatus = "failed"
@@ -186,6 +212,9 @@ func (w *Workflow) Run(ctx context.Context) int {
186212
if w.Repository != nil {
187213
checkpoint, checkpointErr := w.Repository.Checkpoint(ctx, initialHead, canCheckpoint)
188214
if checkpointErr != nil {
215+
if ctx.Err() != nil {
216+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
217+
}
189218
w.diagnostic("findings checkpoint failed", checkpointErr)
190219
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
191220
}
@@ -202,6 +231,9 @@ func (w *Workflow) Run(ctx context.Context) int {
202231
if w.Repository != nil {
203232
hasChanges, err := w.Repository.HasChanges(ctx)
204233
if err != nil {
234+
if ctx.Err() != nil {
235+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
236+
}
205237
w.diagnostic("repository status failed", err)
206238
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
207239
}
@@ -224,7 +256,9 @@ func (w *Workflow) Run(ctx context.Context) int {
224256
}
225257
finalization, err := w.Agent.Finalize(runner.WithStageContext(stageCtx, runner.StageContext{Stage: "finalize", ReviewPhase: phase, Cycle: cycle, Model: w.stageModel("finalize"), ReasoningEffort: w.stageReasoningEffort("finalize")}), checkpointed)
226258
presentationErr = nil
227-
if err != nil {
259+
if err != nil && ctx.Err() != nil {
260+
presentationErr = w.Log.CompleteAgent("finalize cancelled")
261+
} else if err != nil {
228262
presentationErr = w.Log.CompleteAgent("finalize failed")
229263
} else {
230264
presentationErr = w.Log.CompleteAgent("finalize completed")
@@ -240,12 +274,18 @@ func (w *Workflow) Run(ctx context.Context) int {
240274
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
241275
}
242276
if err != nil {
277+
if ctx.Err() != nil {
278+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
279+
}
243280
if !w.emitUnknownSteps() || !w.emit("stage_completed", event.F("stage", "finalize"), event.F("model", w.stageModel("finalize")), event.F("reasoning_effort", w.stageReasoningEffort("finalize")), event.F("status", "failed"), durationField(now().Sub(stageStarted))) {
244281
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
245282
}
246283
w.diagnostic("finalization failed", err)
247284
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
248285
}
286+
if ctx.Err() != nil {
287+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
288+
}
249289
if !w.emitSteps(finalization) || !w.emit("stage_completed", event.F("stage", "finalize"), event.F("model", w.stageModel("finalize")), event.F("reasoning_effort", w.stageReasoningEffort("finalize")), event.F("status", "success"), event.F("verdict", finalization.Verdict), durationField(now().Sub(stageStarted))) {
250290
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
251291
}
@@ -278,7 +318,9 @@ func (w *Workflow) Run(ctx context.Context) int {
278318
}
279319
err = w.Agent.FixCI(runner.WithStageContext(stageCtx, runner.StageContext{Stage: "fix-ci", ReviewPhase: phase, Cycle: cycle, Model: w.stageModel("fix-ci"), ReasoningEffort: w.stageReasoningEffort("fix-ci")}))
280320
presentationErr = nil
281-
if err != nil {
321+
if err != nil && ctx.Err() != nil {
322+
presentationErr = w.Log.CompleteAgent("fix-ci cancelled")
323+
} else if err != nil {
282324
presentationErr = w.Log.CompleteAgent("fix-ci failed")
283325
} else {
284326
presentationErr = w.Log.CompleteAgent("fix-ci completed")
@@ -293,6 +335,12 @@ func (w *Workflow) Run(ctx context.Context) int {
293335
w.diagnostic("render interactive view", presentationErr)
294336
return w.complete("operational_failure", ExitOperational, now().Sub(runStarted))
295337
}
338+
if err != nil && ctx.Err() != nil {
339+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
340+
}
341+
if ctx.Err() != nil {
342+
return w.complete("cancelled", ExitInterrupted, now().Sub(runStarted))
343+
}
296344
stageStatus := "success"
297345
if err != nil {
298346
stageStatus = "failed"

‎internal/workflow/workflow_test.go‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,19 +603,38 @@ func TestCancellationStopsActiveLivenessWithoutLateWrites(t *testing.T) {
603603
go func() { result <- w.Run(ctx) }()
604604
<-started
605605
cancel()
606-
if code := <-result; code != ExitOperational {
606+
if code := <-result; code != ExitInterrupted {
607607
t.Fatalf("code=%d output=%q stderr=%q", code, output.String(), stderr.String())
608608
}
609609
before := output.String()
610610
ticks <- time.Now().Add(time.Minute)
611611
if after := output.String(); after != before {
612612
t.Fatalf("late output after cancellation: before=%q after=%q", before, after)
613613
}
614-
if !strings.Contains(before, "[1/0] [gpt-5.6-sol/medium] Review failed") || !strings.Contains(before, "Failed due to an operational error") {
614+
if !strings.Contains(before, "Cancelled") || strings.Contains(before, "Review failed") || strings.Contains(before, "Failed due to an operational error") {
615615
t.Fatalf("missing cancellation terminal output: %q", before)
616616
}
617617
}
618618

619+
func TestCancellationEmitsCancelledKVResult(t *testing.T) {
620+
started := make(chan struct{})
621+
agent := &fakeAgent{reviewWait: true, reviewStarted: started}
622+
var output, stderr bytes.Buffer
623+
w := Workflow{Config: config.Config{}, Agent: agent, Log: &event.Logger{Out: &output}, Err: &stderr}
624+
ctx, cancel := context.WithCancel(context.Background())
625+
result := make(chan int, 1)
626+
go func() { result <- w.Run(ctx) }()
627+
<-started
628+
cancel()
629+
if code := <-result; code != ExitInterrupted {
630+
t.Fatalf("code=%d output=%q stderr=%q", code, output.String(), stderr.String())
631+
}
632+
assertRecord(t, output.String(), "event=run_completed", "status=cancelled", "exit_code=130")
633+
if strings.Contains(output.String(), "event=review_completed") || stderr.Len() != 0 {
634+
t.Fatalf("output=%q stderr=%q", output.String(), stderr.String())
635+
}
636+
}
637+
619638
func TestCIFixResetsPhaseAndFixes(t *testing.T) {
620639
agent := &fakeAgent{
621640
reviews: []codex.ReviewResult{clean(), findings(), clean()},

‎memory-bank/features/FT-010/brief.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ An interactive terminal run can show the continuing workflow log and the active
3737
- `REQ-03` The lower pane presents output from the currently active Codex process as it arrives.
3838
- `REQ-04` A stage change clearly identifies or replaces the active agent stream so output from separate processes is not mixed.
3939
- `REQ-05` The feature defines behavior for resize, pane scrolling, long lines, ANSI/control sequences, process completion/errors, and pressing `i` when no agent is active.
40-
- `REQ-06` Terminal state is restored cleanly on exit, interruption, and panic; returning from the view leaves the invoking shell usable. `Ctrl-C` remains a reliable normal workflow interruption while one-key input is enabled.
40+
- `REQ-06` Terminal state is restored cleanly on exit, interruption, and panic; returning from the view leaves the invoking shell usable. `Ctrl-C` remains a reliable normal workflow interruption while one-key input is enabled: it reports cancellation and exits 130, not an operational failure.
4141
- `REQ-07` Non-interactive execution preserves the existing workflow stdout event format, does not require a TTY, and keeps agent output excluded from machine-readable workflow stdout.
4242
- `REQ-08` Tests and documentation describe interactive-mode prerequisites and key bindings.
4343

0 commit comments

Comments
 (0)