fix(core): bound compaction request size
This commit is contained in:
parent
56a7c06a80
commit
4cf877a9c1
2 changed files with 120 additions and 56 deletions
|
|
@ -17,6 +17,8 @@ import { Token } from "../util/token"
|
||||||
const DEFAULT_BUFFER = 20_000
|
const DEFAULT_BUFFER = 20_000
|
||||||
const DEFAULT_KEEP_TOKENS = 8_000
|
const DEFAULT_KEEP_TOKENS = 8_000
|
||||||
const TOOL_OUTPUT_MAX_CHARS = 2_000
|
const TOOL_OUTPUT_MAX_CHARS = 2_000
|
||||||
|
const COMPACTION_CHUNK_TOKENS = 32_000
|
||||||
|
const SUMMARY_OUTPUT_TOKENS = 4_096
|
||||||
const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside <template> and keep the section order unchanged. Do not include the <template> tags in your response.
|
const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside <template> and keep the section order unchanged. Do not include the <template> tags in your response.
|
||||||
<template>
|
<template>
|
||||||
## Objective
|
## Objective
|
||||||
|
|
@ -80,7 +82,8 @@ type Plan = {
|
||||||
readonly sessionID: SessionSchema.ID
|
readonly sessionID: SessionSchema.ID
|
||||||
readonly model: Model
|
readonly model: Model
|
||||||
readonly reason: SessionMessage.Compaction["reason"]
|
readonly reason: SessionMessage.Compaction["reason"]
|
||||||
readonly prompt: string
|
readonly previousSummary?: string
|
||||||
|
readonly context: readonly string[]
|
||||||
readonly recent: string
|
readonly recent: string
|
||||||
readonly inputID?: SessionMessage.ID
|
readonly inputID?: SessionMessage.ID
|
||||||
}
|
}
|
||||||
|
|
@ -199,6 +202,14 @@ export const buildPrompt = (input: { readonly previousSummary?: string; readonly
|
||||||
...input.context,
|
...input.context,
|
||||||
].join("\n\n")
|
].join("\n\n")
|
||||||
|
|
||||||
|
const chunkContext = (context: readonly string[]) => {
|
||||||
|
const value = context.filter(Boolean).join("\n\n")
|
||||||
|
const size = COMPACTION_CHUNK_TOKENS * 4
|
||||||
|
return Array.from({ length: Math.ceil(value.length / size) }, (_, index) =>
|
||||||
|
value.slice(index * size, (index + 1) * size),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const planContent = (messages: readonly SessionMessage.Info[], tokens: number) => {
|
const planContent = (messages: readonly SessionMessage.Info[], tokens: number) => {
|
||||||
const selected = select(messages, tokens)
|
const selected = select(messages, tokens)
|
||||||
if (!selected) return
|
if (!selected) return
|
||||||
|
|
@ -208,10 +219,8 @@ const planContent = (messages: readonly SessionMessage.Info[], tokens: number) =
|
||||||
const previousRecent = previousSummary?.type === "compaction" ? previousSummary.recent : ""
|
const previousRecent = previousSummary?.type === "compaction" ? previousSummary.recent : ""
|
||||||
const summarizeRecent = !previousRecent && !selected.head
|
const summarizeRecent = !previousRecent && !selected.head
|
||||||
return {
|
return {
|
||||||
prompt: buildPrompt({
|
previousSummary: previousSummary?.type === "compaction" ? previousSummary.summary : undefined,
|
||||||
previousSummary: previousSummary?.type === "compaction" ? previousSummary.summary : undefined,
|
context: chunkContext(summarizeRecent ? [selected.recent] : [previousRecent, selected.head]),
|
||||||
context: summarizeRecent ? [selected.recent] : [previousRecent, selected.head].filter(Boolean),
|
|
||||||
}),
|
|
||||||
recent: summarizeRecent ? "" : selected.recent,
|
recent: summarizeRecent ? "" : selected.recent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -235,62 +244,67 @@ const make = (dependencies: Dependencies) => {
|
||||||
inputID: plan.inputID,
|
inputID: plan.inputID,
|
||||||
})
|
})
|
||||||
|
|
||||||
const chunks: string[] = []
|
let summary = plan.previousSummary
|
||||||
let failure: SessionError.Error | undefined
|
for (const [index, context] of plan.context.entries()) {
|
||||||
yield* dependencies.llm
|
const chunks: string[] = []
|
||||||
.stream(
|
let failure: SessionError.Error | undefined
|
||||||
LLM.request({
|
const publish = index === plan.context.length - 1
|
||||||
model: plan.model,
|
yield* dependencies.llm
|
||||||
messages: [Message.user(plan.prompt)],
|
.stream(
|
||||||
tools: [],
|
LLM.request({
|
||||||
}),
|
model: plan.model,
|
||||||
)
|
messages: [Message.user(buildPrompt({ previousSummary: summary, context: [context] }))],
|
||||||
.pipe(
|
tools: [],
|
||||||
Stream.runForEach((event) => {
|
generation: { maxTokens: SUMMARY_OUTPUT_TOKENS },
|
||||||
if (LLMEvent.is.providerError(event))
|
|
||||||
failure = {
|
|
||||||
type: event.classification === "context-overflow" ? "provider.invalid-request" : "provider.error",
|
|
||||||
message: event.message,
|
|
||||||
}
|
|
||||||
if (LLMEvent.is.textDelta(event)) {
|
|
||||||
chunks.push(event.text)
|
|
||||||
return dependencies.events.publish(SessionEvent.Compaction.Delta, {
|
|
||||||
sessionID: plan.sessionID,
|
|
||||||
text: event.text,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return Effect.void
|
|
||||||
}),
|
|
||||||
Effect.catchTag("LLM.Error", (error) =>
|
|
||||||
Effect.sync(() => {
|
|
||||||
failure = toSessionError(error)
|
|
||||||
}),
|
}),
|
||||||
),
|
)
|
||||||
Effect.onInterrupt(() =>
|
.pipe(
|
||||||
plan.reason === "auto"
|
Stream.runForEach((event) => {
|
||||||
? failed({
|
if (LLMEvent.is.providerError(event))
|
||||||
sessionID: plan.sessionID,
|
failure = {
|
||||||
reason: plan.reason,
|
type: event.classification === "context-overflow" ? "provider.invalid-request" : "provider.error",
|
||||||
error: { type: "compaction.interrupted", message: "Compaction was interrupted" },
|
message: event.message,
|
||||||
inputID: plan.inputID,
|
}
|
||||||
}).pipe(Effect.asVoid)
|
if (LLMEvent.is.textDelta(event)) {
|
||||||
: Effect.void,
|
chunks.push(event.text)
|
||||||
),
|
if (publish)
|
||||||
)
|
return dependencies.events.publish(SessionEvent.Compaction.Delta, {
|
||||||
const summary = chunks.join("")
|
sessionID: plan.sessionID,
|
||||||
if (failure || !summary.trim()) {
|
text: event.text,
|
||||||
const error = failure ?? { type: "compaction.failed" as const, message: "Compaction produced no summary" }
|
})
|
||||||
return yield* failed({
|
}
|
||||||
sessionID: plan.sessionID,
|
return Effect.void
|
||||||
reason: plan.reason,
|
}),
|
||||||
error,
|
Effect.catchTag("LLM.Error", (error) =>
|
||||||
inputID: plan.inputID,
|
Effect.sync(() => {
|
||||||
})
|
failure = toSessionError(error)
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
Effect.onInterrupt(() =>
|
||||||
|
plan.reason === "auto"
|
||||||
|
? failed({
|
||||||
|
sessionID: plan.sessionID,
|
||||||
|
reason: plan.reason,
|
||||||
|
error: { type: "compaction.interrupted", message: "Compaction was interrupted" },
|
||||||
|
inputID: plan.inputID,
|
||||||
|
}).pipe(Effect.asVoid)
|
||||||
|
: Effect.void,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
const next = chunks.join("")
|
||||||
|
if (failure || !next.trim())
|
||||||
|
return yield* failed({
|
||||||
|
sessionID: plan.sessionID,
|
||||||
|
reason: plan.reason,
|
||||||
|
error: failure ?? { type: "compaction.failed", message: "Compaction produced no summary" },
|
||||||
|
inputID: plan.inputID,
|
||||||
|
})
|
||||||
|
summary = next
|
||||||
}
|
}
|
||||||
yield* dependencies.events.publish(SessionEvent.Compaction.Ended, {
|
yield* dependencies.events.publish(SessionEvent.Compaction.Ended, {
|
||||||
sessionID: plan.sessionID,
|
sessionID: plan.sessionID,
|
||||||
reason: plan.reason,
|
reason: plan.reason,
|
||||||
text: summary,
|
text: summary ?? "",
|
||||||
recent: plan.recent,
|
recent: plan.recent,
|
||||||
})
|
})
|
||||||
return { status: "completed" as const }
|
return { status: "completed" as const }
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ it.effect("manual compaction summarizes short context instead of no-op", () =>
|
||||||
expect(Array.from(yield* Fiber.join(delta)).map((event) => event.data.text)).toEqual(["manual summary"])
|
expect(Array.from(yield* Fiber.join(delta)).map((event) => event.data.text)).toEqual(["manual summary"])
|
||||||
|
|
||||||
expect(requests).toHaveLength(1)
|
expect(requests).toHaveLength(1)
|
||||||
expect(requests[0]?.generation).toBeUndefined()
|
expect(requests[0]?.generation?.maxTokens).toBe(4_096)
|
||||||
expect(JSON.stringify(requests[0]?.messages)).toContain("Manual compaction should include this short conversation.")
|
expect(JSON.stringify(requests[0]?.messages)).toContain("Manual compaction should include this short conversation.")
|
||||||
expect(yield* store.context(sessionID)).toMatchObject([
|
expect(yield* store.context(sessionID)).toMatchObject([
|
||||||
{ type: "compaction", reason: "manual", summary: "manual summary", recent: "" },
|
{ type: "compaction", reason: "manual", summary: "manual summary", recent: "" },
|
||||||
|
|
@ -170,3 +170,53 @@ it.effect("manual compaction summarizes short context instead of no-op", () =>
|
||||||
])
|
])
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it.effect("manual compaction rolls large histories through bounded requests", () =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
requests = []
|
||||||
|
const db = (yield* Database.Service).db
|
||||||
|
const compaction = yield* SessionCompaction.Service
|
||||||
|
const store = yield* SessionStore.Service
|
||||||
|
const sessionID = SessionV2.ID.make("ses_bounded_compaction")
|
||||||
|
yield* db
|
||||||
|
.insert(ProjectTable)
|
||||||
|
.values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
|
||||||
|
.onConflictDoNothing()
|
||||||
|
.run()
|
||||||
|
.pipe(Effect.orDie)
|
||||||
|
yield* db
|
||||||
|
.insert(SessionTable)
|
||||||
|
.values({
|
||||||
|
id: sessionID,
|
||||||
|
project_id: Project.ID.global,
|
||||||
|
slug: "bounded-compaction",
|
||||||
|
directory: "/project",
|
||||||
|
title: "Bounded compaction",
|
||||||
|
version: "test",
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
.pipe(Effect.orDie)
|
||||||
|
const session = yield* store
|
||||||
|
.get(sessionID)
|
||||||
|
.pipe(Effect.flatMap((value) => (value ? Effect.succeed(value) : Effect.die("test session missing"))))
|
||||||
|
|
||||||
|
expect(
|
||||||
|
yield* compaction.compactManual({
|
||||||
|
session,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: SessionMessage.ID.create(),
|
||||||
|
type: "user",
|
||||||
|
text: "a".repeat(300_000),
|
||||||
|
time: { created: DateTime.makeUnsafe(0) },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
inputID: SessionMessage.ID.make("msg_bounded_compaction"),
|
||||||
|
}),
|
||||||
|
).toEqual({ status: "completed" })
|
||||||
|
|
||||||
|
expect(requests.length).toBeGreaterThan(1)
|
||||||
|
expect(requests.every((request) => JSON.stringify(request.messages).length < 150_000)).toBe(true)
|
||||||
|
expect(JSON.stringify(requests[1]?.messages)).toContain("manual summary")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue