Skip to content

Commit 937665a

Browse files
committed
fix(session): use parentID instead of timestamp for loop exit condition
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
1 parent 3c96bf8 commit 937665a

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13741374
lastAssistant?.finish &&
13751375
!["tool-calls"].includes(lastAssistant.finish) &&
13761376
!hasToolCalls &&
1377-
lastUser.id < lastAssistant.id
1377+
(
1378+
// Use parentID to determine if assistant message is a response to the last user message.
1379+
// This is more reliable than timestamp comparison when client/server clocks are out of sync.
1380+
lastAssistant.parentID === lastUser.id ||
1381+
// Fallback to timestamp comparison for backward compatibility with messages
1382+
// that may not have parentID set.
1383+
(!lastAssistant.parentID && lastUser.id < lastAssistant.id)
1384+
)
13781385
) {
13791386
log.info("exiting loop", { sessionID })
13801387
break

0 commit comments

Comments
 (0)