Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,28 @@ NOTE: At any point in time through this workflow you should feel free to ask the
!hasToolCalls &&
lastUser.id < lastAssistant.id
) {
// Before exiting, check if new messages arrived while we were running
// This handles the race condition in prompt_async where messages can arrive
// while the runner is transitioning between states
// See: https://github.com/anomalyco/opencode/issues/...
const latestMsgs = yield* MessageV2.filterCompactedEffect(sessionID)
const newUserMsg = latestMsgs.findLast(
(m) => m.info.role === "user" && m.info.id > lastAssistant.id,
)

// If a new user message arrived while we were running, continue the loop
// instead of exiting, so we can process it
if (newUserMsg) {
log.info("detected new message during exit, continuing loop", {
sessionID,
lastAssistantId: lastAssistant.id,
newMsgId: newUserMsg.info.id,
})
// Reset step counter for the new message
step = 0
continue
}

log.info("exiting loop", { sessionID })
break
}
Expand Down
Loading