Skip to content

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

Open
YLRong wants to merge 1 commit intoanomalyco:devfrom
YLRong:fix/parentid-loop-exit
Open

fix(session): use parentID instead of timestamp for loop exit condition#21334
YLRong wants to merge 1 commit intoanomalyco:devfrom
YLRong:fix/parentid-loop-exit

Conversation

@YLRong
Copy link
Copy Markdown

@YLRong YLRong commented Apr 7, 2026

Issue for this PR

Closes #14935

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes duplicate assistant responses caused by clock skew between client and server systems.

Problem: When client and server have different system times (clock skew), the prompt loop's exit condition lastUser.id < lastAssistant.id fails because message IDs are timestamp-based. This causes the loop to continue and generate a second assistant response for the same user message.

Solution: Replace timestamp-based comparison with explicit parentID relationship check. The assistant message's parentID field already stores the ID of the user message it responds to, making this a more reliable indicator than timestamp comparison.

The change adds:

  • Primary check using lastAssistant.parentID === lastUser.id
  • Fallback to timestamp comparison for backward compatibility with messages that may not have parentID set

This fix is needed because the timestamp-based approach assumes client and server clocks are synchronized, which is not always true in real-world deployments.

How did you verify your code works?

Tested with OpenChamber 1.9.3 + OpenCode 1.3.17:

  1. Reproduced the issue by setting server time 10 seconds faster than client
  2. Confirmed duplicate assistant responses before the fix
  3. Verified single response after the fix
  4. Tested with server time 10 seconds slower - works correctly

The fix uses existing fields (parentID) that are already populated correctly, so no migration is needed.

Screenshots / recordings

N/A - This is a backend logic fix

Checklist

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

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

I found potentially related PRs that address similar issues:

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

  2. fix: server-generated message IDs to prevent client/server clock skew #11869 - "fix: server-generated message IDs to prevent client/server clock skew"

These PRs appear to be addressing the same or very similar issues with the prompt loop exit condition and clock skew. You should verify if PR #14307 is a previous attempt at the same fix or if there's overlap in scope.

@YLRong YLRong force-pushed the fix/parentid-loop-exit branch from e91f050 to f5dc565 Compare April 7, 2026 13:59
Replace timestamp-based comparison with parentID check to prevent duplicate
assistant responses caused by clock skew between client and server.

When client and server have different system times, the ID-based comparison
lastUser.id < lastAssistant.id fails, causing the prompt loop to continue
and generate a second assistant response.

This fix uses the explicit parentID relationship to determine if the
assistant message is a response to the last user message, which is
immune to clock skew issues.

For backward compatibility, the timestamp comparison is kept as a
fallback for messages that may not have parentID set.

Fixes anomalyco#14935
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.

UI bug: Assistant responses rendered twice/duplicated

1 participant