Skip to content

fix(session): use parentID instead of timestamp for loop exit condition#21365

Open
okottorika wants to merge 2 commits intoanomalyco:devfrom
okottorika:fix/duplicate-assistant-messages-webui
Open

fix(session): use parentID instead of timestamp for loop exit condition#21365
okottorika wants to merge 2 commits intoanomalyco:devfrom
okottorika:fix/duplicate-assistant-messages-webui

Conversation

@okottorika
Copy link
Copy Markdown

@okottorika okottorika commented Apr 7, 2026

Issue for this PR

Closes #21335

Type of change

  • Bug fix

What does this PR do?

The Web UI triggers duplicate assistant responses when sending a prompt. The processing loop fails to exit because its condition lastUser.id < lastAssistant.id relies on timestamp-based ID ordering. Since the Web UI generates user message IDs client-side and the server generates assistant IDs independently, clock skew between the two breaks the exit condition, causing the loop to continue generating responses.

The fix replaces the ID ordering check with lastAssistant.parentID === lastUser.id, which directly verifies the assistant message belongs to the current user prompt. This field is already populated and does not require any schema changes.

The TUI is not affected because it uses the synchronous session.prompt endpoint rather than session.promptAsync.

How did you verify your code works?

Added a test case in packages/opencode/test/session/prompt-effect.test.ts that simulates clock skew by creating messages with timestamps that would sort incorrectly under the old logic. The test confirms the loop exits correctly using the parentID check regardless of ID ordering.

The existing test "loop exits immediately when last assistant has stop finish" continues to pass since the seeded assistant message already has parentID set to the user message ID.

Screenshots / recordings

N/A - no UI changes

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The loop exit condition compared user.id < assistant.id which breaks
when client/server clocks diverge. Web UI generates message IDs
client-side while server generates assistant IDs, causing clock skew
that makes user IDs sort after assistant IDs. This triggers an infinite
loop generating duplicate assistant messages.

Fix checks assistant.parentID === user.id instead, which correctly
identifies whether the assistant message is responding to the current
user message regardless of ID ordering.

Fixes anomalyco#21335

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate PRs Found

PR #21334 - fix(session): use parentID instead of timestamp for loop exit condition

PR #21361 - fix: prevent web ui session loop by removing client-side message id g...

PR #14307 - fix: use parentID matching instead of ID ordering for prompt loop exit and message rendering

PR #11869 - fix: server-generated message IDs to prevent client/server clock skew

The most likely duplicate is PR #21334, which uses the exact same solution (parentID instead of ID ordering) for the same issue.

@okottorika okottorika changed the title fix: use parentID instead of ID ordering for loop exit condition fix(session): use parentID instead of timestamp for loop exit condition Apr 7, 2026
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

The test was creating different IDs for parentID and user message ID
due to the global counter incrementing on each Identifier.create() call.
Create the user ID first and reuse it for both the user message and
the assistant's parentID. Also added the missing text part for the
assistant message to match the seed helper.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
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.

Duplicate assistant messages in Web UI

1 participant