Skip to content

fix(opencode): reconcile turns after event stream EOF - #2684

Open
timigod wants to merge 10 commits into
getpaseo:mainfrom
timigod:review/v025-opencode-eof-reconcile
Open

fix(opencode): reconcile turns after event stream EOF#2684
timigod wants to merge 10 commits into
getpaseo:mainfrom
timigod:review/v025-opencode-eof-reconcile

Conversation

@timigod

@timigod timigod commented Jul 31, 2026

Copy link
Copy Markdown

Linked issue

Closes #2271

Supersedes #2272 with the same bounded outcome rebuilt and re-reviewed from the latest stable release, v0.2.5 (6fc491e6220fba6543bbbe4bf1b1f58cfe59228b). The old PR remains unchanged so its discussion and evidence stay readable.

Type of change

  • Bug fix
  • New feature
  • Enhancement
  • Refactor
  • Docs

What does this PR do

The OpenCode adapter can lose the terminal portion of a turn when the global event stream ends before the provider session has finished. This change reconciles the active turn from persisted provider state instead of treating stream EOF alone as terminal truth.

The focused replacement:

  • keeps turn ownership explicit while reconciling persisted provider messages;
  • recovers multi-step turns without accepting ambiguous or unrelated assistant output;
  • preserves completed usage and cost, including failed and canceled terminal turns;
  • isolates persisted terminal errors from later turns;
  • keeps the normal live event stream as the primary path; and
  • preserves current upstream autonomous-turn behavior: idle persisted user content does not start a turn, while busy status and permission-first live activity do.

It contains no Plexer policy, host topology, fleet routing, release bump, or machine-local behavior.

How did you verify it

The current head is a69c283a4bff686834df67e62cc0c2721c0298ca. It retains the stable-derived change and merges current upstream main at 70ed70d36e2eabd3876a22931954319ec202c902 without force.

  • 114 focused OpenCode adapter tests passed on Node 24.
  • Full server and CLI dependency build passed.
  • Server/CLI/protocol dependency typecheck passed.
  • Targeted lint and formatting passed.
  • The all-workspace pre-commit lint, format, and typecheck passed.
  • Greptile reviewed the exact current head and reported 5/5 confidence with no blocking defect.
  • GitHub reports the PR mergeable with no content conflict.

The focused tests cover disconnected completion, missing suffix recovery, pending permission/question states, provider turn ownership, multi-step persisted messages, failure/cancel outcomes, usage and cost, ambiguous-state rejection, idle persisted user content, busy autonomous activity, and permission-first activity.

No UI or desktop bundle is changed. This branch has not been installed or activated on either live daemon.

Checklist

  • One focused change
  • npm run typecheck passes
  • npm run lint passes
  • npm run format passes
  • QA evidence
  • Tests added or updated where it made sense

This contribution was developed with AI assistance; every verification claim above comes from an actual run against the submitted branch.

@greptile-apps

greptile-apps Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR makes OpenCode turn startup and EOF handling cancellation-aware.

  • Reconciles owned foreground and autonomous turns from persisted provider messages when the event stream ends prematurely.
  • Captures a pre-dispatch message baseline for slash commands and aborts that read on interrupt or close.
  • Preserves terminal outcomes, usage, cost, and turn ownership while rejecting ambiguous persisted history.
  • Adds focused harness support and tests for EOF recovery, cancellation races, multi-step turns, and terminal-state isolation.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported slash-command startup issue is addressed by passing the active turn’s abort signal into the baseline history request, checking cancellation before dispatch, and covering both interrupt and close paths.

Important Files Changed

Filename Overview
packages/server/src/server/agent/providers/opencode-agent.ts Adds explicit turn ownership, abortable slash-command baseline reads, and persisted-state reconciliation after event-stream EOF.
packages/server/src/server/agent/providers/opencode-agent.test.ts Adds behavioral coverage for EOF reconciliation, ownership, usage, ambiguous histories, and interrupt/close races.
packages/server/src/server/agent/providers/opencode/test-utils/test-opencode-harness.ts Extends the OpenCode test client with injectable message-history request behavior for cancellation and race tests.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[startTurn] --> B{Slash command?}
  B -- Yes --> C[Read persisted-message baseline]
  C --> D{Interrupted or closed?}
  D -- Yes --> E[Abort baseline request]
  D -- No --> F[Dispatch command]
  B -- No --> G[Dispatch prompt]
  F --> H[Consume live event stream]
  G --> H
  H --> I{Terminal event received?}
  I -- Yes --> J[Finish turn from live event]
  I -- No, stream EOF --> K[Read persisted messages]
  K --> L{Owned terminal state unambiguous?}
  L -- Yes --> M[Restore usage and emit terminal event]
  L -- No --> N[Fail turn at EOF]
Loading

Reviews (5): Last reviewed commit: "Merge upstream main into OpenCode EOF re..." | Re-trigger Greptile

Comment thread packages/server/src/server/agent/providers/opencode-agent.ts Outdated
@timigod

timigod commented Jul 31, 2026

Copy link
Copy Markdown
Author

Fixed in 2238d3b416417124fc59af2a526779c250efd661. The pre-dispatch history lookup now uses the active turn AbortSignal, and the continuation checks cancellation before dispatch. Added interrupt and close regressions proving the pending lookup, startTurn, and close all settle without dispatching the slash command. Verification: 92 focused OpenCode tests, full server build, repository-wide typecheck, and pre-commit lint/format/typecheck all pass.

@timigod
timigod marked this pull request as draft August 1, 2026 09:21
@timigod
timigod marked this pull request as ready for review August 1, 2026 09:53
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.

OpenCode turns still fail with "event stream ended before the turn reached a terminal state" when the global SSE stream drops mid-turn

1 participant