Skip to content

Commit 1882130

Browse files
djgpp6qwencoder
andcommitted
fix: use parentID instead of ID ordering for loop exit condition
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 <[email protected]>
1 parent 6a5aae9 commit 1882130

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13771377
lastAssistant?.finish &&
13781378
!["tool-calls"].includes(lastAssistant.finish) &&
13791379
!hasToolCalls &&
1380-
lastUser.id < lastAssistant.id
1380+
lastAssistant.parentID === lastUser.id
13811381
) {
13821382
log.info("exiting loop", { sessionID })
13831383
break

‎packages/opencode/test/session/prompt-effect.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { ToolRegistry } from "../../src/tool/registry"
3434
import { Truncate } from "../../src/tool/truncate"
3535
import { Log } from "../../src/util/log"
3636
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
37+
import { Identifier } from "../../src/id/id"
3738
import { provideTmpdirInstance, provideTmpdirServer } from "../fixture/fixture"
3839
import { testEffect } from "../lib/effect"
3940
import { reply, TestLLMServer } from "../lib/llm-server"
@@ -325,6 +326,60 @@ it.live("loop exits immediately when last assistant has stop finish", () =>
325326
),
326327
)
327328

329+
it.live("loop exits when assistant parentID matches user message even with clock skew", () =>
330+
provideTmpdirServer(
331+
Effect.fnUntraced(function* ({ llm }) {
332+
const prompt = yield* SessionPrompt.Service
333+
const sessions = yield* Session.Service
334+
const chat = yield* sessions.create({ title: "Clock skew test" })
335+
336+
// Simulate client clock being ahead: create assistant message first (server time),
337+
// then create user message with a "future" timestamp that would sort after the assistant
338+
const assistantTime = Date.now() - 1000 // 1 second in the past
339+
const userTime = Date.now() + 1000 // 1 second in the future
340+
341+
// Create assistant message with older timestamp
342+
const assistantID = Identifier.create("message", false, assistantTime)
343+
const assistant: MessageV2.Assistant = {
344+
id: MessageID.make(assistantID),
345+
role: "assistant",
346+
parentID: MessageID.make(Identifier.create("message", false, userTime)),
347+
sessionID: chat.id,
348+
mode: "build",
349+
agent: "build",
350+
cost: 0,
351+
path: { cwd: "/tmp", root: "/tmp" },
352+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
353+
modelID: ref.modelID,
354+
providerID: ref.providerID,
355+
time: { created: assistantTime },
356+
finish: "stop",
357+
}
358+
yield* sessions.updateMessage(assistant)
359+
360+
// Create user message with newer timestamp (simulating client clock ahead)
361+
const userID = Identifier.create("message", false, userTime)
362+
const userMsg: MessageV2.User = {
363+
id: MessageID.make(userID),
364+
role: "user",
365+
sessionID: chat.id,
366+
agent: "build",
367+
model: ref,
368+
time: { created: userTime },
369+
}
370+
yield* sessions.updateMessage(userMsg)
371+
372+
// The loop should still exit because it checks parentID === user.id,
373+
// not ID ordering
374+
const result = yield* prompt.loop({ sessionID: chat.id })
375+
expect(result.info.role).toBe("assistant")
376+
if (result.info.role === "assistant") expect(result.info.finish).toBe("stop")
377+
expect(yield* llm.calls).toBe(0)
378+
}),
379+
{ git: true, config: providerCfg },
380+
),
381+
)
382+
328383
it.live("loop calls LLM and returns assistant message", () =>
329384
provideTmpdirServer(
330385
Effect.fnUntraced(function* ({ llm }) {

0 commit comments

Comments
 (0)