fix(opencode): lastUser identification and loop exit condition #11443
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.
What does this PR do?
Fixed a bug where
lastUseris incorrectly identified in multi-turn situations and exits the loop prematurely without generating a response.In the below:
opencode/packages/opencode/src/session/prompt.ts
Lines 282 to 284 in f834915
msgsare traversed in reverse order, butmsgsis not guaranteed to be chronologically ordered. The loop then identifies the wronglastUserand the exit condition is incorrectly evaluated.Fix: Sort
msgsbycreatedtimestamp to ensure chronological orderChanged the loop exit condition from lexicographical ID comparison to a parent-child relationship check to prevent infinite loops.
In the below:
opencode/packages/opencode/src/session/prompt.ts
Lines 296 to 300 in f834915
lastUserandlastAssistantare correctly found, iflastUser.idis lexicographically higher thanlastAssistant.id, it does not break the loop and continues the steps infinitely.Fix: Change exit condition from ID comparison to
lastFinished.parentID === lastUser.id, which properly checks if the finished message is a response to the user messageHow did you verify your code works?
It was hard to reproduce the issue, since sometimes the conditions are evaluated as expected by chance. I tested a multi-turn conversation scenario several times by logging
msgsand the IDs of messages.