diff --git a/packages/core/src/session/runner/llm.ts b/packages/core/src/session/runner/llm.ts index de258fd3d16..9c82bbf7461 100644 --- a/packages/core/src/session/runner/llm.ts +++ b/packages/core/src/session/runner/llm.ts @@ -341,6 +341,7 @@ const layer = Layer.effect( assistantMessageID, }), Continue: Effect.fnUntraced(function* (outcome) { + if (!outcome.empty) recoverEmpty = true yield* retry.wait({ decision: outcome.decision, error: outcome.error, @@ -350,6 +351,7 @@ const layer = Layer.effect( assistantMessageID = SessionMessage.ID.create() }), OutputLimit: Effect.fn("SessionRunner.continueOutputLimit")(function* (outcome) { + if (!outcome.empty) recoverEmpty = true // The current response is included. User input or any other finish breaks the streak. const recent = yield* db .select({ diff --git a/packages/core/src/session/runner/step.ts b/packages/core/src/session/runner/step.ts index 66762f82517..f61a99bb6f9 100644 --- a/packages/core/src/session/runner/step.ts +++ b/packages/core/src/session/runner/step.ts @@ -34,10 +34,11 @@ export type Outcome = Data.TaggedEnum<{ Continue: { readonly error: SessionError.Error readonly decision: SessionRunnerRetry.Decision + readonly empty: boolean } RecoverFull: {} Compacted: {} - OutputLimit: { readonly needsContinuation: boolean } + OutputLimit: { readonly needsContinuation: boolean; readonly empty: boolean } }> export const Outcome = Data.taggedEnum() @@ -278,7 +279,7 @@ export const make = Effect.gen(function* () { tools.declines.length === 0 && !tools.interrupted ) - return Outcome.Continue({ error: llmError, decision: retry }) + return Outcome.Continue({ error: llmError, decision: retry, empty: record.empty }) if (Exit.isFailure(stream)) return yield* Effect.failCause(stream.cause) if (tools.declines.length > 0) { @@ -289,7 +290,7 @@ export const make = Effect.gen(function* () { if (tools.interrupted && Exit.isFailure(joined)) return yield* Effect.failCause(joined.cause) if (record.failure) return yield* new StepFailedError({ error: record.failure }) const needsContinuation = input.prepared.request.toolChoice?.type !== "none" && record.needsContinuation - if (record.finish?.finish === "length") return Outcome.OutputLimit({ needsContinuation }) + if (record.finish?.finish === "length") return Outcome.OutputLimit({ needsContinuation, empty: record.empty }) if (record.finish?.finish === "stop" && record.empty) return Outcome.Empty() return Outcome.Completed({ needsContinuation }) }), diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 9405ccd8542..e30d31da780 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -6096,6 +6096,46 @@ describe("SessionRunnerLLM", () => { }) } + for (const recovery of ["output limit", "incomplete stream"] as const) { + scenario(`resets empty response recovery after nonempty ${recovery} output`, function* (s) { + const partial = [ + LLMEvent.textStart({ id: "partial" }), + LLMEvent.textDelta({ id: "partial", text: "Progress" }), + LLMEvent.textEnd({ id: "partial" }), + ] + yield* s.llm.push( + TestLLM.stop(), + recovery === "output limit" + ? TestLLM.complete({ reason: { normalized: "length" } }, ...partial) + : TestLLM.failAfter(incompleteStream(), LLMEvent.stepStart({ index: 0 }), ...partial), + TestLLM.stop(), + TestLLM.text("Finished", "finished"), + ) + + const scheduled = yield* subscribeRetries(s) + const run = yield* s.runPrompt("Finish the task").pipe(Effect.forkChild) + if (recovery === "incomplete stream") { + yield* Queue.take(scheduled) + yield* TestClock.adjust("2400 millis") + } + yield* Fiber.join(run) + + expect(s.requests).toHaveLength(4) + expect(userTexts(s.requests[1]).at(-1)).toBe(userTexts(s.requests[3]).at(-1)) + expect((yield* s.messages).filter((message) => message.type === "synthetic")).toHaveLength(3) + expect((yield* s.context).at(-1)).toMatchObject(Expected.assistant({}, [Expected.text("Finished")])) + }) + } + + scenario("keeps the empty response bound across an empty output-limit response", function* (s) { + yield* s.llm.push(TestLLM.stop(), TestLLM.complete({ reason: { normalized: "length" } }), TestLLM.stop()) + + expect(yield* s.runPrompt("Finish the task").pipe(Effect.flip)).toMatchObject({ + error: { type: "empty-response" }, + }) + expect(s.requests).toHaveLength(3) + }) + scenario("accepts text supplied only at text end without empty response recovery", function* (s) { yield* s.llm.push( TestLLM.complete(