Skip to content

fix(agent-mode): stop cancelled OpenCode retries - #2685

Merged
logancyang merged 4 commits into
v4-previewfrom
codex/fix-214-opencode-stream-cancel
Jul 16, 2026
Merged

fix(agent-mode): stop cancelled OpenCode retries#2685
logancyang merged 4 commits into
v4-previewfrom
codex/fix-214-opencode-stream-cancel

Conversation

@logancyang

@logancyang logancyang commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Fixes logancyang/obsidian-copilot-preview#214

GitHub did not register the cross-repo closing reference for this v4-preview PR; close issue #214 manually on merge if it remains open.

Problem

Agent Mode pinned OpenCode 1.15.13, whose ACP session/cancel handler acknowledged Stop without aborting the backing turn. AgentSession also awaited backend.prompt() directly, so its local abort controller could not settle the UI turn when cancellation failed.

The attached production trace shows the resulting failure mode: OpenCode emitted visible thought and answer chunks, went silent for 139.484 seconds, retried the same 76,961-token request, and continued sending retry chunks after Stop. Both model-service requests completed normally; the gap was downstream of the model service.

Fix

  • Pin and require OpenCode 1.16.0, the first release whose ACP session/cancel aborts the backing request while preserving the session.
  • Race the single-agent prompt against the turn's local abort signal so Stop settles the UI as cancelled even if the cancel RPC fails.
  • Abort locally before notifying the backend, and clear stream routing state so chunks emitted during or after cancellation cannot mutate the partial answer.
  • Preserve already-visible content and mark the turn cancelled instead of appending a duplicate retry attempt.

This deliberately does not add a generic inactivity timeout. ACP does not expose a safe boundary that distinguishes an interrupted model stream from legitimate long-running tool work, and the upstream stream-retry change is not merged. Healthy thought-to-answer streaming remains unchanged.

Verification

  • npm test -- --runInBand — 308 suites, 4,436 tests passed
  • npm test -- --runInBand -t cancel — 15 suites, 21 matching tests passed
  • Focused AgentSession/OpenCode lifecycle suites — 181 tests passed
  • npm run format
  • npm run lint
  • npm run build

Regression coverage emits visible thought and partial-answer chunks, simulates a failed backend cancellation plus later retry output, and verifies that the turn becomes idle/cancelled, retains only the original partial response, rejects trailing chunks, and can run a later prompt once the backing request settles.

OpenCode upstream cancellation fix: anomalyco/opencode#30145

🤖 Generated with Codex

@logancyang
logancyang marked this pull request as ready for review July 15, 2026 23:11
@logancyang
logancyang requested a review from zeroliu July 15, 2026 23:12

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c0e50491d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agentMode/session/AgentSession.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 101c63aaa8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agentMode/session/AgentSession.ts
@logancyang
logancyang merged commit e89bd56 into v4-preview Jul 16, 2026
2 checks passed
@logancyang
logancyang deleted the codex/fix-214-opencode-stream-cancel branch July 16, 2026 07:27
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.

1 participant