[PR #11443] fix(opencode): lastUser identification and loop exit condition #13789

Open
opened 2026-02-16 18:18:37 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11443

State: open
Merged: No


What does this PR do?

  1. Fixed a bug where lastUser is incorrectly identified in multi-turn situations and exits the loop prematurely without generating a response.
    In the below:
    https://github.com/anomalyco/opencode/blob/f834915d3fbcf05137fc61fc51b9fa07b815e82c/packages/opencode/src/session/prompt.ts#L282-L284 msgs are traversed in reverse order, but msgs is not guaranteed to be chronologically ordered. The loop then identifies the wrong lastUser and the exit condition is incorrectly evaluated.
    Fix: Sort msgs by created timestamp to ensure chronological order

  2. Changed the loop exit condition from lexicographical ID comparison to a parent-child relationship check to prevent infinite loops.
    In the below:
    https://github.com/anomalyco/opencode/blob/f834915d3fbcf05137fc61fc51b9fa07b815e82c/packages/opencode/src/session/prompt.ts#L296-L300 even if lastUser and lastAssistant are correctly found, if lastUser.id is lexicographically higher than lastAssistant.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 message

How 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 msgs and the IDs of messages.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11443 **State:** open **Merged:** No --- ### What does this PR do? 1. Fixed a bug where `lastUser` is incorrectly identified in multi-turn situations and exits the loop prematurely without generating a response. In the below: https://github.com/anomalyco/opencode/blob/f834915d3fbcf05137fc61fc51b9fa07b815e82c/packages/opencode/src/session/prompt.ts#L282-L284 `msgs` are traversed in reverse order, but `msgs` is not guaranteed to be chronologically ordered. The loop then identifies the wrong `lastUser` and the exit condition is incorrectly evaluated. **Fix**: Sort `msgs` by `created` timestamp to ensure chronological order 2. Changed the loop exit condition from lexicographical ID comparison to a parent-child relationship check to prevent infinite loops. In the below: https://github.com/anomalyco/opencode/blob/f834915d3fbcf05137fc61fc51b9fa07b815e82c/packages/opencode/src/session/prompt.ts#L296-L300 even if `lastUser` and `lastAssistant` are correctly found, if `lastUser.id` is lexicographically higher than `lastAssistant.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 message ### How 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 `msgs` and the IDs of messages.
yindo added the pull-request label 2026-02-16 18:18:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13789