Skip to content

Commit 358f94b

Browse files
committed
fix(session): prevent concurrent prompt() from creating duplicate user messages
Two prompt() calls can hit the same session at the same time. Both call createUserMessage() and the loop picks the wrong one, so subagents end up running on the parent model instead of their own. Added a Set to track active sessions and queue the second caller. Fixes anomalyco#6636 Relates: anomalyco#11699, anomalyco#6928, anomalyco#8700
1 parent 802ccd3 commit 358f94b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,23 @@ export namespace SessionPrompt {
156156
})
157157
export type PromptInput = z.infer<typeof PromptInput>
158158

159+
// tracks sessions with an active prompt() call to avoid duplicate messages (#6636)
160+
const _busy = new Set<string>()
161+
159162
export const prompt = fn(PromptInput, async (input) => {
163+
while (_busy.has(input.sessionID)) {
164+
const s = state()
165+
if (s[input.sessionID]) {
166+
return new Promise<MessageV2.WithParts>((resolve, reject) => {
167+
s[input.sessionID].callbacks.push({ resolve, reject })
168+
})
169+
}
170+
await new Promise((r) => setTimeout(r, 100))
171+
}
172+
173+
_busy.add(input.sessionID)
174+
using _guard = defer(() => void _busy.delete(input.sessionID))
175+
160176
const session = await Session.get(input.sessionID)
161177
await SessionRevert.cleanup(session)
162178

0 commit comments

Comments
 (0)