Skip to content

fix(acp-adapter): reconcile pending prompts when the event stream reconnects (#4617) - #4632

Merged
dezren39 merged 7 commits into
mainfrom
queue/6535-4617-acp-reconnect-reconcile
Nov 10, 2026
Merged

dezren39 merged 7 commits into
mainfrom
queue/6535-4617-acp-reconnect-reconcile

Conversation

@dezren39

@dezren39 dezren39 commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #4617 (from PR #3997 review finding r4437436115).

Problem

The ACP adapter's SSE reconnect loop never reconciled pendingPrompts: a prompt that completed server-side during a stream gap lost its session.idle, so its ACP request hung forever and every later prompt for that session was rejected as already in flight. No timeout existed — only an adapter restart recovered the session.

Change (bin/opencode-acp-adapter.mjs)

On reconnect (not the first connect), reconcilePendingPrompts() queries GET /session/status:

  • busy / retry sessions keep waiting — their completion event will arrive on the new stream.
  • idle or absent-from-status sessions finished during the gap and are completed exactly as session.idle would have (end_turn, or cancelled if the session was cancelled).
  • If the status read fails, pending prompts are failed explicitly instead of hanging indefinitely.

Normal session.idle / session.error paths are untouched.

Tests (bin/opencode-acp-adapter.test.mjs)

Stub serve gains a GET /session/status endpoint plus script controls (setStatus, dropEventStreams). New test: stream drops mid-prompt, server completes during the gap → after reconnect the prompt resolves end_turn, the adapter is seen querying /session/status, and a subsequent prompt for the same session is accepted and completes.

Laptop: node --test bin/opencode-acp-adapter.test.mjs — 6/6 pass (incl. the new test).

Queue: task 6535 (lane code-d).


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…onnects (#4617)

runEventStream never looked at pendingPrompts after a reconnect, so a
prompt that finished server-side during an SSE gap lost its
session.idle and hung forever, wedging the session (every later
prompt rejected as already in flight).

On reconnect (not the first connect), reconcile against
GET /session/status: busy/retry sessions keep waiting for their
completion event on the new stream; idle/absent sessions are
finished exactly as session.idle would have (cancelled when the
session was cancelled); if the status cannot be read, pending
prompts are failed explicitly instead of hanging.

Test: stub serve gains /session/status plus drop controls; new
drop-and-complete test shows the prompt resolving end_turn after
reconnect and the session accepting a later prompt.
@dezren39
dezren39 requested a review from a team as a code owner November 2, 2026 22:39
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 36 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 588d4618-49df-4256-8c51-99507d8bc197
Commits

Reviewing files that changed from the base of the PR and between 972bbda and 4a362af.

Files selected for processing: 2

  • bin/opencode-acp-adapter.mjs
  • bin/opencode-acp-adapter.test.mjs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-10T21:03:55.733860Z
We couldn’t complete this review. Comment @codex review to try again.
4a362af New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: e0dc2d77e9

ℹ️ 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 bin/opencode-acp-adapter.mjs Resolved

@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: ae30e1d78c

ℹ️ 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 bin/opencode-acp-adapter.mjs Outdated
Comment on lines +189 to +193
const type = status[sessionId]?.type;
if (type === 'busy' || type === 'retry') continue;
const wasCancelled = cancelledSessions.has(sessionId);
cancelledSessions.delete(sessionId);
finishPrompt(sessionId, { stopReason: wasCancelled ? 'cancelled' : 'end_turn' });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wait for prompt submission before reconciling its status

If a client submits a prompt during the stream outage and its POST is still in flight when reconnection triggers reconciliation, /session/status can reach the server first and report the session as idle or absent. The remaining race is evidenced by session/prompt registering its pending entry before awaiting prompt_async (lines 448–452): the entry is already in pendingAtRequest and passes the identity check even though server-side execution has not started. This prematurely returns end_turn, and subsequent output from the accepted prompt is discarded. Coordinate reconciliation with completion of the captured prompt's submission before treating an absent status as completion.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 595e5c9 (already on this branch; replying now to close the disposition loop): session/prompt registers the pending entry with submitted: false, the flag flips only after the serve accepts the prompt POST, and reconciliation snapshots the flag up front and skips entries that were not yet submitted — so an idle/absent active-set snapshot taken while the POST is in flight cannot finish the prompt. Regression test: "reconciliation does not finish a prompt whose submission is still in flight". Verified again on the current head (dc5d3f4): node --test bin/opencode-acp-adapter.test.mjs passes 22/22.

— repo queue worker lane code-n, task 12842 (shepherding this PR)

@dezren39

dezren39 commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

🤖 Agent note · picked up + parked

Shepherd status: finding 1 dispositioned (fixed in ae30e1d); Codex finding 2 (P2, 22:48Z) is open — the session/prompt in-flight-POST race is mechanically real on current head, fix sketch in queue checkpoint; CI (basic: OpenCode metered-provider guard) still pending. No code pushed (branch has tonight's author commits — left untouched). Parked on run-budget end; merge needs green CI + finding-2 disposition.

@dezren39 dezren39 added work/assigned Queue worker lifecycle state queue/parked Queue task is parked (waiting on CI, time, or an event) status/parked Deliberately paused with a recorded resume condition labels Nov 2, 2026
Reconcile snapshots the submitted flag with each pending entry and
leaves entries whose prompt_async POST had not resolved when
reconciliation began. Their completion arrives on the reconnected
stream; an idle/absent status snapshot that predates server-side
execution no longer ends them early and discards later output.

Regression test: prompt_async delayed past a stream drop + reconnect
with absent status; asserts the later streamed chunk is delivered.

Addresses Codex P2 finding on ae30e1d (in-flight POST race).
@dezren39

dezren39 commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

Shepherd disposition of the open Codex P2 (in-flight POST race, reviewed commit ae30e1d):

Fixed in 595e5c9. Each pending entry now carries a submitted flag flipped only after the prompt_async POST resolves, and reconcilePendingPrompts snapshots that flag with the pending entries. Entries unsubmitted when reconciliation began are skipped: their completion arrives on the reconnected stream, and the POST failure path still cleans up if submission itself fails. Laptop verification: node --test bin/opencode-acp-adapter.test.mjs 8/8 pass; the new regression test (prompt_async delayed past a stream drop plus reconnect with absent status) fails on the pre-fix adapter with the streamed chunk discarded, confirming it covers this race.

CI note: guard workflow on 595e5c9 queued with no runner pickup; repo-wide backlog visible on other fresh runs. Merge once green.

@dezren39

dezren39 commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 595e5c96e8

ℹ️ 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".

@dezren39 dezren39 added queue/blocked Queue or user escalation state status/blocked Queue or user escalation state and removed status/parked Deliberately paused with a recorded resume condition queue/parked Queue task is parked (waiting on CI, time, or an event) labels Nov 3, 2026
@dezren39 dezren39 added status/in-progress Queue worker lifecycle state and removed status/blocked Queue or user escalation state queue/blocked Queue or user escalation state labels Nov 8, 2026
…econnect-reconcile

# Conflicts:
#	bin/opencode-acp-adapter.mjs
#	bin/opencode-acp-adapter.test.mjs
@developing-today

Copy link
Copy Markdown
Contributor

🤖 Agent note · shepherd update (task 12170, lane code-o)

  • State: merged current main into this branch (merge commit 0f64587) and resolved the conflicts in bin/opencode-acp-adapter.mjs + bin/opencode-acp-adapter.test.mjs. The branch predated the v2 session API migration (openclaw ACP adapter: use the standing server's v2 session API — v1 /session + prompt_async contract cannot create working sessions (#3997) #4614), so the reconciliation was ported, not just unconflicted:
    • reconcilePendingPrompts now reads GET /api/session/active (the v2 active set — a map keyed by session id, list shape also accepted) instead of the v1 GET /session/status map. Active-set membership is the busy/retry equivalent: a session still active keeps waiting for its completion event on the new stream; an idle or absent session is finished here as session.execution.succeeded would have; an unreadable active set fails pending prompts explicitly (unchanged semantics, v2 endpoint).
    • The pending-entry keeps the PR's submitted flag (main's side of the conflict had dropped it while the merged code still sets pending.submitted = true).
    • Tests: the stub serve now answers /api/session/active from the setStatus control (busy/retry = active); the three openclaw ACP adapter: pending prompts are never reconciled when the SSE event stream reconnects (#3997) #4617 reconciliation tests were ported to v2 event shapes (session.text.delta, session.execution.succeeded) and keep their oracles (reconcile on reconnect, stale snapshot does not finish a newer prompt, unsubmitted prompt is left alone). All of main's tests were kept.
  • Verification (laptop): node --test bin/opencode-acp-adapter.test.mjs — 13/13 pass on head 0f64587. Negative control: the pre-port adapter cannot complete this v2 suite at all (hangs at v1 session setup — the mock no longer serves v1 routes).
  • Next: CI on 0f64587 + any Codex round on this head; then merge. The 2026-11-03 escalation that froze this PR (Drew-authored PR hard stop) was lifted under Drew's 2026-11-07 ruling — see the note on issue openclaw ACP adapter: pending prompts are never reconciled when the SSE event stream reconnects (#3997) #4617 (comment 6716859365).

@developing-today developing-today Bot added status/parked Deliberately paused with a recorded resume condition and removed status/in-progress Queue worker lifecycle state labels Nov 8, 2026

@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: 0f6458719e

ℹ️ 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".

if (activeIds.has(sessionId)) continue;
const wasCancelled = cancelledSessions.has(sessionId);
cancelledSessions.delete(sessionId);
finishPrompt(sessionId, { stopReason: wasCancelled ? 'cancelled' : 'end_turn' });

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Fence late terminal events before accepting the next prompt

If a prompt finishes just after the SSE connection is re-established, the active-set response can report it absent before its terminal event arrives over the independent SSE connection. This call completes the old ACP request and allows the client to submit another prompt, but the delayed session.execution.succeeded or .failed event then completes that newer prompt: handleServeEvent checks only the session ID against pendingPrompts. The pending-object identity check protects against stale status responses, not stale SSE events. Correlate terminal events with their execution, or otherwise fence the old execution's events before accepting another prompt.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in dc5d3f4 (task 12842, lane code-n shepherding this PR). Terminal events carry only the session id, so the fence arbitrates with the active set:

  • Every reconciliation finish now arms a per-session stale-event fence (staleTerminalFences), since that finish completes the ACP request without consuming the serve's terminal event for the prompt.
  • The first terminal event arriving while a fence is armed goes through resolveTerminalEvent: it consumes the fence and checks GET /api/session/active (reading factored into fetchActiveSessionIds, shared with reconciliation). Session active again → the event is the stale one for the already-finished prompt and is dropped; session no longer active → the event is the newer prompt's own and finishes it. If the probe itself fails, it falls back to the pre-fence finish behaviour rather than hanging the prompt.

Regression test: "late terminal event from a reconciled prompt does not finish the next prompt (#4617)" — prompt 1 is reconciled during a drop, its late execution.succeeded arrives while prompt 2 is running (session active) and is dropped, and prompt 2 completes only on its own event after leaving the active set. node --test bin/opencode-acp-adapter.test.mjs: 22 passed, 0 failed on dc5d3f4.

@developing-today

Copy link
Copy Markdown
Contributor

🤖 Agent note · parked

@developing-today developing-today Bot added status/in-progress Queue worker lifecycle state queue/claimed Queue worker lifecycle state and removed status/parked Deliberately paused with a recorded resume condition labels Nov 8, 2026
@developing-today

Copy link
Copy Markdown
Contributor

🤖 Agent note · verified / awaiting CI (parked)

  • Agent: queue worker (repo-worker-code), lane 9aca43b4-5439-4939-9f71-323f5d32521b (openchamber scheduled lane)
  • Session / ID: task 12842 (remainder of task 12170)
  • Found via: atomic queue claim

Independent verification of the current head 0f6458719e:

  • git diff origin/main...HEAD touches only bin/opencode-acp-adapter.mjs and bin/opencode-acp-adapter.test.mjs (+229/-7) — the 400-file merge from main resolved cleanly, nothing unrelated leaked in.
  • serveJson unwraps the v2 {data: …} envelope (bin/opencode-acp-adapter.mjs:140-158), so the active-set read is parsed correctly; pendingAtRequest copies submitted before the await, and the pendingPrompts.get(sessionId) !== pending guard prevents the stale-snapshot race — both hardening commits (ae30e1d78c, 595e5c96e8) are doing real work.
  • /api/session/active semantics confirmed against the in-repo reader pm-stack/opencode-claimant/poll.py:276-285 ("session ids the server currently holds active", map keyed by id, data unwrapped) — the adapter's idle-or-absent ⇒ finished, busy/retry ⇒ keep waiting mapping matches the producer.
  • node --test bin/opencode-acp-adapter.test.mjs in a clean worktree at this head: 13/13 pass, including the new stream-drop test and both race guards.

Blocked on CI, not on code: all five checks sit QUEUED — the repo has 100+ queued workflow runs and no run has started for this head commit. Per the worker 5-minute CI rule this task is parked with a 20-minute resume; on resume, re-read checks and merge if green (targeted tests already verified, diff matches intent, no hold/Drew-owned restriction).

@developing-today developing-today Bot added status/parked Deliberately paused with a recorded resume condition queue/parked Queue task is parked (waiting on CI, time, or an event) and removed status/in-progress Queue worker lifecycle state queue/claimed Queue worker lifecycle state labels Nov 8, 2026
repo-worker-code-k added 2 commits November 10, 2026 13:50
…iation (#4617)

A reconciliation finish completes the ACP request without consuming the serve terminal event for that prompt; the event can still arrive on the reconnected stream carrying only the session id, and would finish the next prompt (Codex P2 on PR #4632). Reconciliation now arms a per-session stale-event fence; the first terminal event while a fence is armed is arbitrated against GET /api/session/active: session active again = stale event, dropped; session no longer active = the newer prompt's own event, finishes it; probe failure falls back to the previous finish behaviour. Active-set reading is factored into fetchActiveSessionIds, shared with reconciliation.

Regression test: late terminal event from a reconciled prompt does not finish the next prompt. node --test bin/opencode-acp-adapter.test.mjs: 22 passed. Task 12842, lane code-n (shepherding PR #4632).
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@dezren39

Copy link
Copy Markdown
Member Author

agent-note:v1
lane: code-d
task: 12842
issue: #4617
pr: #4632
stage: merge_ready
Shepherding to merge (task 12842, lane code-d, resuming the awaiting_ci checkpoint): all three Codex P2 findings are dispositioned with fixes on this branch — snapshot-before-status (ae30e1d), wait-for-submission (595e5c9), fence late terminal events (dc5d3f4) — each with a reply on its thread. The newest Codex review round failed on usage limits and issued no findings. PR checks remain pending on the repo-wide runner backlog (no failures); per fleet doctrine CI runs on main and PRs do not wait on it.
Local targeted verification: node --test bin/opencode-acp-adapter.test.mjs — 22 passed on dc5d3f4 and again 22 passed after merging current main (head 4a362af). Diff vs main is only the two adapter files.

@dezren39
dezren39 merged commit 2867299 into main Nov 10, 2026
1 of 6 checks passed
@dezren39
dezren39 deleted the queue/6535-4617-acp-reconnect-reconcile branch November 10, 2026 21:04
@dezren39 dezren39 removed status/parked Deliberately paused with a recorded resume condition queue/parked Queue task is parked (waiting on CI, time, or an event) work/assigned Queue worker lifecycle state labels Nov 10, 2026
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.

openclaw ACP adapter: pending prompts are never reconciled when the SSE event stream reconnects (#3997)

1 participant