Skip to content

fix(scan): propagate Ctrl-C cancellation to scan runs - #996

Open
AllenMuu wants to merge 3 commits into
alibaba:mainfrom
AllenMuu:feat/scan-cancellation-resume
Open

fix(scan): propagate Ctrl-C cancellation to scan runs#996
AllenMuu wants to merge 3 commits into
alibaba:mainfrom
AllenMuu:feat/scan-cancellation-resume

Conversation

@AllenMuu

@AllenMuu AllenMuu commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Description

Makes Ctrl-C cancellation a durable, user-visible terminal outcome for OCR sessions.

  • Propagates the ocr scan command context through preview, telemetry, scan.Agent.Run, and result emission.
  • Persists cancelled sessions with:
    • terminal_reason: "cancelled"
    • cancellation_reason: "context canceled"
  • Preserves completed scan checkpoints so --resume reuses completed files and reruns only unfinished files.
  • Aligns cancellation handling across scan and diff-review paths, including cancellation while loading diffs and before a skipped review is finalized.
  • Keeps cancellation distinct from deadline expiration and ordinary input/enumeration failures.
  • Makes ocr session list/show and ocr viewer agree on cancelled sessions. Viewer details now show aborted (cancelled: context canceled).
  • Skips scan dedup/project-summary LLM work after cancellation, avoiding requests that would immediately fail.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally
  • Manual testing

Regression coverage verifies:

  • cancelled scan sessions persist session_end with cancelled terminal metadata;
  • cancellation during scan enumeration is finalized, while deadline and ordinary enumeration failures are not misclassified as user cancellation;
  • a completed scan item is checkpointed before cancellation, and resume reuses it while rerunning unfinished items;
  • cancelled review sessions are consistently classified during diff loading and dispatch;
  • viewer list/detail parsing and detail-page rendering expose the cancelled state and reason;
  • cancelled scan runs do not create dedup or project-summary LLM requests;
  • the emitted trace ID belongs to the scan.run span.

Also ran:

  • make test (race-enabled)
  • make check (license, English-only check, formatting, module tidy, and go vet)
  • git diff --check

Live LLM verification with Volcengine Ark (doubao-seed-2-1-turbo-260628):

  • ocr llm test passed.
  • A single-file scan completed successfully.
  • During a two-file scan, SIGINT was sent after the first file checkpoint completed.
  • The cancelled session preserved the completed checkpoint.
  • ocr scan --resume reused the completed file and ran only the unfinished file.
  • Some OpenAI-compatible models did not emit OCR's required task_done tool call; this provider-compatibility behavior is tracked separately from this cancellation fix.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (not applicable; no user-facing documentation changes are needed)
  • I have signed the CLA

Related Issues

Closes #995

@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 2 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)
  • ❌ Failed to post inline: 1 comment(s)

high

📄 cmd/opencodereview/scan_cmd.go (L226-L227)

⚠️ GitHub could not post this as an inline comment: Lines 226-227 could not be resolved (outside PR diff hunks)

Bug: After the refactoring, ctx now refers to the signal-aware context passed in, while runCtx is the context with the newly created span. TraceIDFromContext(ctx) will return an empty string (or a parent trace ID if one happened to exist), not the trace ID of the "scan.run" span we just created.

This should use runCtx to get the trace ID of the span that was started on line 223. The old code used ctx because that was the variable holding the span context before the refactoring renamed it to runCtx.

💡 Suggested Change

Before:

	if telemetry.IsEnabled() {
		traceID = telemetry.TraceIDFromContext(ctx)

After:

	if telemetry.IsEnabled() {
		traceID = telemetry.TraceIDFromContext(runCtx)

Comment thread cmd/opencodereview/scan_cmd.go Outdated
@AllenMuu
AllenMuu force-pushed the feat/scan-cancellation-resume branch from 1d5eba3 to 3dcc865 Compare August 18, 2026 12:26
@AllenMuu

Copy link
Copy Markdown
Contributor Author

Fixed in 3dcc865.

  • The scan trace ID is now read from the runCtx returned by telemetry.StartSpan, preserving the scan.run span trace.
  • Added a regression test for the trace context selection.
  • Added deterministic coverage for completed scan checkpoint → cancellation → persisted session → resume, asserting only unfinished files call the LLM after resume.

Validated with make check and make test (race enabled).

@AllenMuu
AllenMuu marked this pull request as ready for review August 18, 2026 13:33
@AllenMuu

Copy link
Copy Markdown
Contributor Author

During manual Ctrl-C/resume testing today, I found several cancellation gaps beyond the original scan context propagation:

  • cancellation terminal metadata was not persisted consistently across scan and review paths;
  • ocr session and ocr viewer could present different status for the same cancelled session;
  • post-cancellation scan work could still attempt dedup or project-summary LLM requests.

Addressed in 16f8f45 (fix(session): persist cancelled scan and review sessions). The commit persists and surfaces cancelled session state, aligns scan/review handling, preserves resume checkpoints, and adds regression coverage for these paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scan does not propagate Ctrl-C cancellation to the scan runtime

1 participant