diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index 226cdccff88..73473731d2c 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -100,6 +100,8 @@ type Prepared = Effect.Success line.startsWith("##")) - const filled = (text: string) => text.split("\n").some((line) => headings.includes(line.trim())) + const filled = (reply: Streamed) => + !reply.toolCalls && reply.text.split("\n").some((line) => headings.includes(line.trim())) const prepared = yield* prepare(context, messages, budget) // Size the unchanged request with the number that decided to compact: the last response's count plus an // estimate of what came after it. Characters overstate a conversation whose old reasoning the provider drops. @@ -475,7 +478,7 @@ export const layer = Layer.effect( const measured = trigger.reason === "overflow" ? undefined : measuredPrompt({ ...context, messages }) // Hooks saw the request without the summary prompt, so it is appended here. A reply that ignores the - // template gets one reminder before it counts as a failure. + // template, or stops to call a tool, gets one reminder before it counts as a failure. const send = (request: LLMRequest, size?: Estimate) => Effect.gen(function* () { const prompted = fitOutput( @@ -484,7 +487,7 @@ export const layer = Layer.effect( size && { ...size, estimated: size.estimated + Token.estimate(prompt) }, ) const reply = yield* stream(context, prompted, prepared.options, background) - if (filled(reply.text)) return reply + if (filled(reply)) return reply const nudged = fitOutput( { ...prompted, messages: [...prompted.messages, Message.user(NUDGE)] }, @@ -492,13 +495,15 @@ export const layer = Layer.effect( size && { ...size, estimated: size.estimated + Token.estimate(prompt) + Token.estimate(NUDGE) }, ) const retry = yield* stream(context, nudged, prepared.options, background) - if (filled(retry.text)) return retry + if (filled(retry)) return retry return yield* Effect.fail({ error: { type: "compaction.failed", - message: retry.text.trim() - ? "Compaction summary did not match the required template" - : "Compaction produced no summary", + message: !retry.text.trim() + ? "Compaction produced no summary" + : retry.toolCalls + ? "Compaction summary was cut off by a tool call" + : "Compaction summary did not match the required template", }, }) }) @@ -816,6 +821,8 @@ export const layer = Layer.effect( }) case "content-filter": return unusable(contentFilterError("Compaction summary was blocked by the provider", event.reason)) + case "tool-calls": + return Effect.succeed({ ...streamed, toolCalls: true }) default: return Effect.succeed(streamed) } diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 99ca1729921..0530aeb3012 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -2898,6 +2898,33 @@ describe("SessionRunnerLLM", () => { } } + for (const summary of [true, false]) { + scenario(`compaction ${summary ? "recovers from" : "rejects"} a summary cut off by a tool call`, function* (s) { + yield* s.llm.push(TestLLM.text("Earlier answer", "summary-history")) + yield* s.runPrompt("Earlier question") + s.requests.length = 0 + const cut = TestLLM.toolCalls( + LLMEvent.textDelta({ id: "summary-cut", text: "## Objective\n- Fix the compac" }), + LLMEvent.toolCall({ id: "call-summary", name: "echo", input: { text: "must not execute" } }), + ) + yield* s.llm.push(cut, summary ? TestLLM.text("## Objective\n- Fix compaction", "summary-recovered") : cut) + const compact = yield* s.session.compact({ sessionID }) + yield* s.resume + + expect(s.requests).toHaveLength(2) + expect(userTexts(s.requests[1]).at(-1)).toContain("did not fill in the required summary template") + expect(s.executions).toEqual([]) + expect((yield* s.messages).find((message) => message.id === compact.id)).toMatchObject( + summary + ? { status: "completed", summary: "## Objective\n- Fix compaction" } + : { + status: "failed", + error: { type: "compaction.failed", message: "Compaction summary was cut off by a tool call" }, + }, + ) + }) + } + scenario("waits for plugin activation before each step and tool call", function* (s) { const waits: Array<[number, number]> = [] s.activationHook = Effect.sync(() => {