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
6 changes: 5 additions & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ export namespace SessionPrompt {
log.info("loop", { step, sessionID })
if (abort.aborted) break
let msgs = await MessageV2.filterCompacted(MessageV2.stream(sessionID))
// Sort chronologically to ensure that `lastUser` and `lastAssistant` are found correctly
msgs.sort((a, b) => a.info.time.created - b.info.time.created)

let lastUser: MessageV2.User | undefined
let lastAssistant: MessageV2.Assistant | undefined
Expand All @@ -293,10 +295,12 @@ export namespace SessionPrompt {
}

if (!lastUser) throw new Error("No user message found in stream. This should never happen.")

// Check if we have a finished assistant response to the last user message
if (
lastAssistant?.finish &&
!["tool-calls", "unknown"].includes(lastAssistant.finish) &&
lastUser.id < lastAssistant.id
lastUser.id === lastAssistant.parentID
) {
log.info("exiting loop", { sessionID })
break
Expand Down
Loading