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
41 changes: 24 additions & 17 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,26 @@ const layer = Layer.effect(
}
})

// Finalize partially streamed parts so a failed attempt does not leave
// unclosed reasoning/text blocks behind before the stream is retried.
const sealPartialParts = Effect.fn("SessionProcessor.sealPartialParts")(function* () {
if (ctx.currentText) {
const end = Date.now()
ctx.currentText.time = { start: ctx.currentText.time?.start ?? end, end }
yield* session.updatePart(ctx.currentText)
ctx.currentText = undefined
}

for (const part of Object.values(ctx.reasoningMap)) {
const end = Date.now()
yield* session.updatePart({
...part,
time: { start: part.time.start ?? end, end },
})
}
ctx.reasoningMap = {}
})

const cleanup = Effect.fn("SessionProcessor.cleanup")(function* () {
if (ctx.snapshot) {
const patch = yield* snapshot.patch(ctx.snapshot)
Expand All @@ -552,21 +572,7 @@ const layer = Layer.effect(
ctx.snapshot = undefined
}

if (ctx.currentText) {
const end = Date.now()
ctx.currentText.time = { start: ctx.currentText.time?.start ?? end, end }
yield* session.updatePart(ctx.currentText)
ctx.currentText = undefined
}

for (const part of Object.values(ctx.reasoningMap)) {
const end = Date.now()
yield* session.updatePart({
...part,
time: { start: part.time.start ?? end, end },
})
}
ctx.reasoningMap = {}
yield* sealPartialParts()

yield* Effect.forEach(
Object.values(ctx.toolcalls),
Expand Down Expand Up @@ -634,8 +640,9 @@ const layer = Layer.effect(

return yield* Effect.gen(function* () {
yield* Effect.gen(function* () {
ctx.currentText = undefined
ctx.reasoningMap = {}
// A retried attempt replays the request from scratch, so close out
// any reasoning/text parts the failed attempt left dangling.
yield* sealPartialParts()
yield* status.set(ctx.sessionID, { type: "busy" })
const stream = llm.stream(streamInput)

Expand Down
Loading