fix(session): use transcript position instead of lexical ID compare in prompt loop#24379
Open
tiffanychum wants to merge 1 commit intoanomalyco:devfrom
Open
fix(session): use transcript position instead of lexical ID compare in prompt loop#24379tiffanychum wants to merge 1 commit intoanomalyco:devfrom
tiffanychum wants to merge 1 commit intoanomalyco:devfrom
Conversation
…n prompt loop SessionPrompt.run compared lastUser.id < lastAssistant.id (and m.info.id <= lastFinished.id) to decide transcript ordering. The HTTP API explicitly accepts caller-supplied messageIDs, so opaque ID order is not guaranteed to match transcript order. A custom user ID that lex-sorts after the assistant's auto-generated ID caused the loop to keep running past finish=stop, emitting an assistant-last transcript that downstream providers reject. Track lastUser/lastAssistant/lastFinished indices in the same backward walk and use array positions (canonical transcript order from filterCompactedEffect) for the loop-exit and post-finish reminder checks. Behavior is unchanged for OpenCode-generated monotonic IDs. Closes anomalyco#23490
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #23490
Type of change
What does this PR do?
SessionPrompt.rundecided transcript ordering by comparing opaque message IDs:The HTTP API explicitly accepts caller-supplied
messageIDon prompt input, but those checks assume IDs always preserve OpenCode's internal monotonic ordering. When a caller supplies amessageIDthat lexically sorts greater thanMessageID.ascending()output (e.g.msg_zzzzzzzz...), the comparison reverses and:finish="stop"no longer fires → loop runs an extra iteration after the assistant turn already completed.The fix tracks
lastUser/lastAssistant/lastFinishedarray indices during the same backward walk that already collects those messages, then uses array position (the canonical transcript order produced byMessageV2.filterCompactedEffect→page()→ORDER BY time_created, id) for both decisions. No new traversals; behavior is unchanged for OpenCode-generated monotonic IDs.Two sites updated:
runLoop.How did you verify your code works?
Added a regression test in
packages/opencode/test/session/prompt.test.ts:It seeds a session with:
msg_zzzzzzzzzzzzzzzzzzzzzzzz(lex-greater than any hex-prefixedMessageID.ascending()),finish: "stop"andtime.createdstrictly greater so transcript ordering is unambiguous.Then asserts
prompt.loopreturns the assistant message and the LLM mock receives 0 calls.Verification:
dev(loop times out re-calling LLM with no replies queued — exactly the reporter's symptom).bun test test/session/→ 315 pass / 0 fail.bun typecheckclean inpackages/opencode.bun turbo typecheckclean repo-wide (13/13).Screenshots / recordings
N/A — non-UI fix.
Checklist