Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f36db6c114 | |||
| fb5726baa8 | |||
| 71751ac339 | |||
| c6364f4b2e |
@@ -1,7 +1,8 @@
|
||||
export * as SessionCompaction from "./compaction"
|
||||
|
||||
import { LLM, LLMClient, LLMError, LLMEvent, Message, type LLMRequest, type Model } from "@opencode-ai/llm"
|
||||
import { Context, DateTime, Effect, Layer, Stream } from "effect"
|
||||
import { SessionError } from "@opencode-ai/schema/session-error"
|
||||
import { Context, Effect, Layer, Stream } from "effect"
|
||||
import { Config } from "../config"
|
||||
import { EventV2 } from "../event"
|
||||
import { makeLocationNode } from "../effect/app-node"
|
||||
@@ -10,6 +11,7 @@ import { SessionEvent } from "./event"
|
||||
import type { SessionMessage } from "./message"
|
||||
import { SessionRunnerModel } from "./runner/model"
|
||||
import { SessionSchema } from "./schema"
|
||||
import { toSessionError } from "./to-session-error"
|
||||
import { Token } from "../util/token"
|
||||
|
||||
const DEFAULT_BUFFER = 20_000
|
||||
@@ -59,7 +61,8 @@ type Dependencies = {
|
||||
readonly llm: {
|
||||
readonly stream: (request: LLMRequest) => Stream.Stream<LLMEvent, LLMError>
|
||||
}
|
||||
readonly config: readonly Config.Entry[]
|
||||
readonly config: Settings
|
||||
readonly models: SessionRunnerModel.Interface
|
||||
}
|
||||
|
||||
export type AutoInput = {
|
||||
@@ -75,6 +78,17 @@ type CompactInput = {
|
||||
readonly inputID?: SessionMessage.ID
|
||||
}
|
||||
|
||||
type Selection = {
|
||||
readonly head: string
|
||||
readonly recent: string
|
||||
}
|
||||
|
||||
type FailureTarget = {
|
||||
readonly sessionID: SessionSchema.ID
|
||||
readonly reason: SessionMessage.Compaction["reason"]
|
||||
readonly inputID?: SessionMessage.ID
|
||||
}
|
||||
|
||||
export type ManualInput = {
|
||||
readonly session: SessionSchema.Info
|
||||
readonly messages: readonly SessionMessage.Info[]
|
||||
@@ -101,7 +115,7 @@ export const serializeToolContent = (content: SessionMessage.ToolStateCompleted[
|
||||
)
|
||||
.join("\n")
|
||||
|
||||
const serialize = (message: SessionMessage.Info) => {
|
||||
const serializeMessage = (message: SessionMessage.Info) => {
|
||||
if (message.type === "user") {
|
||||
const files =
|
||||
message.files?.map(
|
||||
@@ -134,7 +148,7 @@ const serialize = (message: SessionMessage.Info) => {
|
||||
return ""
|
||||
}
|
||||
|
||||
const settings = (documents: readonly Config.Entry[]) => {
|
||||
const resolveSettings = (documents: readonly Config.Entry[]) => {
|
||||
const configured = documents
|
||||
.filter((entry): entry is Config.Document => entry.type === "document")
|
||||
.flatMap((entry) => (entry.info.compaction ? [entry.info.compaction] : []))
|
||||
@@ -148,13 +162,13 @@ const settings = (documents: readonly Config.Entry[]) => {
|
||||
)
|
||||
}
|
||||
|
||||
const select = (
|
||||
const selectCompactionContext = (
|
||||
messages: readonly SessionMessage.Info[],
|
||||
tokens: number,
|
||||
): { readonly head: string; readonly recent: string } | undefined => {
|
||||
): Selection | undefined => {
|
||||
const conversation = messages
|
||||
.filter((message) => message.type !== "compaction")
|
||||
.map(serialize)
|
||||
.map(serializeMessage)
|
||||
.filter(Boolean)
|
||||
if (conversation.length === 0) return undefined
|
||||
let total = 0
|
||||
@@ -181,6 +195,15 @@ const select = (
|
||||
}
|
||||
}
|
||||
|
||||
const findCompletedSummary = (messages: readonly SessionMessage.Info[]) =>
|
||||
messages.find(
|
||||
(message): message is SessionMessage.CompactionCompleted =>
|
||||
message.type === "compaction" && message.status === "completed",
|
||||
)
|
||||
|
||||
const resolveOutputLimit = (request: LLMRequest) =>
|
||||
request.generation?.maxTokens ?? request.model.route.defaults.limits?.output ?? 0
|
||||
|
||||
export const buildPrompt = (input: { readonly previousSummary?: string; readonly context: readonly string[] }) =>
|
||||
[
|
||||
input.previousSummary
|
||||
@@ -191,7 +214,14 @@ export const buildPrompt = (input: { readonly previousSummary?: string; readonly
|
||||
].join("\n\n")
|
||||
|
||||
const make = (dependencies: Dependencies) => {
|
||||
const config = settings(dependencies.config)
|
||||
const config = dependencies.config
|
||||
const publishFailure = (target: FailureTarget, error: SessionError.Error) =>
|
||||
dependencies.events.publish(SessionEvent.Compaction.Failed, {
|
||||
sessionID: target.sessionID,
|
||||
reason: target.reason,
|
||||
error,
|
||||
inputID: target.inputID,
|
||||
})
|
||||
const compact = Effect.fn("SessionCompaction.compact")(function* (input: {
|
||||
readonly sessionID: SessionSchema.ID
|
||||
readonly model: Model
|
||||
@@ -202,12 +232,9 @@ const make = (dependencies: Dependencies) => {
|
||||
readonly output?: number
|
||||
readonly inputID?: SessionMessage.ID
|
||||
}) {
|
||||
const context = input.model.route.defaults.limits?.context
|
||||
if (context === undefined || context <= 0) return false
|
||||
const output = input.output ?? input.model.route.defaults.limits?.output ?? 0
|
||||
const summaryPrompt = buildPrompt({ previousSummary: input.previousSummary, context: input.context })
|
||||
const summaryOutput = Math.min(output || SUMMARY_OUTPUT_TOKENS, SUMMARY_OUTPUT_TOKENS)
|
||||
if (Token.estimate(summaryPrompt) > context - summaryOutput) return false
|
||||
yield* dependencies.events.publish(SessionEvent.Compaction.Started, {
|
||||
sessionID: input.sessionID,
|
||||
reason: input.reason,
|
||||
@@ -216,8 +243,8 @@ const make = (dependencies: Dependencies) => {
|
||||
})
|
||||
|
||||
const chunks: string[] = []
|
||||
let failed = false
|
||||
const summarized = yield* dependencies.llm
|
||||
let failure: SessionError.Error | undefined
|
||||
yield* dependencies.llm
|
||||
.stream(
|
||||
LLM.request({
|
||||
model: input.model,
|
||||
@@ -228,7 +255,11 @@ const make = (dependencies: Dependencies) => {
|
||||
)
|
||||
.pipe(
|
||||
Stream.runForEach((event) => {
|
||||
if (LLMEvent.is.providerError(event)) failed = true
|
||||
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, {
|
||||
@@ -238,27 +269,26 @@ const make = (dependencies: Dependencies) => {
|
||||
}
|
||||
return Effect.void
|
||||
}),
|
||||
Effect.as(true),
|
||||
Effect.catchTag("LLM.Error", () => Effect.succeed(false)),
|
||||
Effect.catchTag("LLM.Error", (error) =>
|
||||
Effect.sync(() => {
|
||||
failure = toSessionError(error)
|
||||
}),
|
||||
),
|
||||
Effect.onInterrupt(() =>
|
||||
input.reason === "auto"
|
||||
? dependencies.events.publish(SessionEvent.Compaction.Failed, {
|
||||
sessionID: input.sessionID,
|
||||
reason: input.reason,
|
||||
error: { type: "compaction.interrupted", message: "Compaction was interrupted" },
|
||||
inputID: input.inputID,
|
||||
? publishFailure(input, {
|
||||
type: "compaction.interrupted",
|
||||
message: "Compaction was interrupted",
|
||||
})
|
||||
: Effect.void,
|
||||
),
|
||||
)
|
||||
const summary = chunks.join("")
|
||||
if (!summarized || failed || !summary.trim()) {
|
||||
yield* dependencies.events.publish(SessionEvent.Compaction.Failed, {
|
||||
sessionID: input.sessionID,
|
||||
reason: input.reason,
|
||||
error: { type: "compaction.failed", message: "Compaction produced no summary" },
|
||||
inputID: input.inputID,
|
||||
})
|
||||
if (failure || !summary.trim()) {
|
||||
yield* publishFailure(
|
||||
input,
|
||||
failure ?? { type: "compaction.failed", message: "Compaction produced no summary" },
|
||||
)
|
||||
return false
|
||||
}
|
||||
yield* dependencies.events.publish(SessionEvent.Compaction.Ended, {
|
||||
@@ -269,61 +299,103 @@ const make = (dependencies: Dependencies) => {
|
||||
})
|
||||
return true
|
||||
})
|
||||
const compactAfterOverflow = Effect.fn("SessionCompaction.compactAfterOverflow")(function* (input: AutoInput) {
|
||||
return yield* compactSelected({
|
||||
sessionID: input.sessionID,
|
||||
messages: input.messages,
|
||||
model: input.request.model,
|
||||
reason: "auto",
|
||||
force: false,
|
||||
output: input.request.generation?.maxTokens ?? input.request.model.route.defaults.limits?.output ?? 0,
|
||||
})
|
||||
})
|
||||
const compactSelected = Effect.fn("SessionCompaction.compactSelected")(function* (
|
||||
input: CompactInput & {
|
||||
readonly reason: SessionMessage.Compaction["reason"]
|
||||
readonly force: boolean
|
||||
readonly output?: number
|
||||
},
|
||||
selected: Selection,
|
||||
) {
|
||||
const context = input.model.route.defaults.limits?.context
|
||||
if (context === undefined || context <= 0) return false
|
||||
const selected = select(input.messages, config.tokens)
|
||||
if (!selected) return false
|
||||
const previousSummary = input.messages.find(
|
||||
(message) => message.type === "compaction" && message.status === "completed",
|
||||
)
|
||||
const hasHead = selected.head.length > 0
|
||||
if (!hasHead && previousSummary?.type !== "compaction" && !input.force) return false
|
||||
const forcedShortContext = input.force && !hasHead
|
||||
const previousRecent = previousSummary?.type === "compaction" ? previousSummary.recent : ""
|
||||
const previousSummary = findCompletedSummary(input.messages)
|
||||
const summarizeRecent = selected.head.length === 0
|
||||
const previousRecent = previousSummary?.recent ?? ""
|
||||
return yield* compact({
|
||||
sessionID: input.sessionID,
|
||||
model: input.model,
|
||||
reason: input.reason,
|
||||
previousSummary: previousSummary?.type === "compaction" ? previousSummary.summary : undefined,
|
||||
context: (forcedShortContext ? [previousRecent, selected.recent] : [previousRecent, selected.head]).filter(
|
||||
previousSummary: previousSummary?.summary,
|
||||
context: (summarizeRecent ? [previousRecent, selected.recent] : [previousRecent, selected.head]).filter(
|
||||
Boolean,
|
||||
),
|
||||
recent: forcedShortContext ? "" : selected.recent,
|
||||
recent: summarizeRecent ? "" : selected.recent,
|
||||
output: input.output,
|
||||
inputID: input.inputID,
|
||||
})
|
||||
})
|
||||
const compactManual = Effect.fn("SessionCompaction.compactManual")(function* (input: CompactInput) {
|
||||
return yield* compactSelected({ ...input, reason: "manual", force: true })
|
||||
const compactAfterOverflow = Effect.fn("SessionCompaction.compactAfterOverflow")(function* (input: AutoInput) {
|
||||
const selected = selectCompactionContext(input.messages, config.tokens)
|
||||
if (!selected) return false
|
||||
return yield* compactSelected(
|
||||
{
|
||||
sessionID: input.sessionID,
|
||||
messages: input.messages,
|
||||
model: input.request.model,
|
||||
reason: "auto",
|
||||
output: resolveOutputLimit(input.request),
|
||||
},
|
||||
selected,
|
||||
)
|
||||
})
|
||||
const compactManual = Effect.fn("SessionCompaction.compactManual")(function* (input: ManualInput) {
|
||||
const target = {
|
||||
sessionID: input.session.id,
|
||||
reason: "manual",
|
||||
inputID: input.inputID,
|
||||
} satisfies FailureTarget
|
||||
const selected = selectCompactionContext(input.messages, config.tokens)
|
||||
if (!selected) {
|
||||
yield* publishFailure(target, { type: "compaction.unavailable", message: "Nothing to compact yet" })
|
||||
return false
|
||||
}
|
||||
const resolved = yield* dependencies.models.resolve(input.session).pipe(
|
||||
Effect.catch((error) => publishFailure(target, toSessionError(error)).pipe(Effect.as(undefined))),
|
||||
)
|
||||
if (!resolved) return false
|
||||
return yield* compactSelected(
|
||||
{
|
||||
...target,
|
||||
messages: input.messages,
|
||||
model: resolved.model,
|
||||
},
|
||||
selected,
|
||||
)
|
||||
})
|
||||
const compactIfNeeded = Effect.fn("SessionCompaction.compactIfNeeded")(function* (input: AutoInput) {
|
||||
if (!config.auto) return false
|
||||
const context = input.request.model.route.defaults.limits?.context
|
||||
if (context === undefined || context <= 0) return false
|
||||
const output = input.request.generation?.maxTokens ?? input.request.model.route.defaults.limits?.output ?? 0
|
||||
const output = resolveOutputLimit(input.request)
|
||||
if (
|
||||
estimate({ system: input.request.system, messages: input.request.messages, tools: input.request.tools }) <=
|
||||
context - Math.max(output, config.buffer)
|
||||
)
|
||||
return false
|
||||
return yield* compactAfterOverflow(input)
|
||||
const selected = selectCompactionContext(input.messages, config.tokens)
|
||||
if (!selected) return false
|
||||
const previousSummary = findCompletedSummary(input.messages)
|
||||
if (!selected.head && !previousSummary) return false
|
||||
const previousRecent = previousSummary?.recent ?? ""
|
||||
const summaryContext = [previousRecent, selected.head].filter(Boolean)
|
||||
const summaryOutput = Math.min(output || SUMMARY_OUTPUT_TOKENS, SUMMARY_OUTPUT_TOKENS)
|
||||
if (
|
||||
Token.estimate(
|
||||
buildPrompt({
|
||||
previousSummary: previousSummary?.summary,
|
||||
context: summaryContext,
|
||||
}),
|
||||
) >
|
||||
context - summaryOutput
|
||||
)
|
||||
return false
|
||||
return yield* compact({
|
||||
sessionID: input.sessionID,
|
||||
model: input.request.model,
|
||||
reason: "auto",
|
||||
previousSummary: previousSummary?.summary,
|
||||
context: summaryContext,
|
||||
recent: selected.recent,
|
||||
output,
|
||||
})
|
||||
})
|
||||
return {
|
||||
compactIfNeeded,
|
||||
@@ -339,22 +411,7 @@ export const layer = Layer.effect(
|
||||
const llm = yield* LLMClient.Service
|
||||
const config = yield* Config.Service
|
||||
const models = yield* SessionRunnerModel.Service
|
||||
const compaction = make({ events, llm, config: yield* config.entries() })
|
||||
|
||||
return Service.of({
|
||||
compactIfNeeded: compaction.compactIfNeeded,
|
||||
compactAfterOverflow: compaction.compactAfterOverflow,
|
||||
compactManual: Effect.fn("SessionCompaction.compactManual")(function* (input) {
|
||||
const resolved = yield* models.resolve(input.session).pipe(Effect.catch(() => Effect.succeed(undefined)))
|
||||
if (!resolved) return false
|
||||
return yield* compaction.compactManual({
|
||||
sessionID: input.session.id,
|
||||
messages: input.messages,
|
||||
model: resolved.model,
|
||||
inputID: input.inputID,
|
||||
})
|
||||
}),
|
||||
})
|
||||
return Service.of(make({ events, llm, models, config: resolveSettings(yield* config.entries()) }))
|
||||
}),
|
||||
)
|
||||
|
||||
|
||||
@@ -128,6 +128,11 @@ const compactModel = Model.make({
|
||||
provider: "fake",
|
||||
route: OpenAIChat.route.with({ limits: { context: 4_000, output: 50 } }),
|
||||
})
|
||||
const undersizedContextModel = Model.make({
|
||||
id: "undersized-context",
|
||||
provider: "fake",
|
||||
route: OpenAIChat.route.with({ limits: { context: 1, output: 1_000 } }),
|
||||
})
|
||||
const recoveryModel = Model.make({
|
||||
id: "recovery",
|
||||
provider: "fake",
|
||||
@@ -1534,11 +1539,12 @@ describe("SessionRunnerLLM", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("settles an admitted manual compaction that cannot start", () =>
|
||||
it.effect("explains when manual compaction has no history", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* setup
|
||||
const session = yield* SessionV2.Service
|
||||
const compaction = yield* session.compact({ sessionID })
|
||||
modelResolveHook = Effect.die("model resolution should not run")
|
||||
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
@@ -1547,7 +1553,7 @@ describe("SessionRunnerLLM", () => {
|
||||
type: "compaction",
|
||||
status: "failed",
|
||||
reason: "manual",
|
||||
error: { message: "Compaction could not start" },
|
||||
error: { type: "compaction.unavailable", message: "Nothing to compact yet" },
|
||||
})
|
||||
expect(
|
||||
(yield* recordedEventTypes(sessionID)).filter(
|
||||
@@ -1557,10 +1563,73 @@ describe("SessionRunnerLLM", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("manually compacts when the model has no context limit", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setup
|
||||
response = reply.text("Earlier answer", "text-manual-unknown-history")
|
||||
yield* admit(session, "Earlier question")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
requests.length = 0
|
||||
response = reply.text("Manual summary", "text-manual-unknown-summary")
|
||||
const compaction = yield* session.compact({ sessionID })
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
expect(requests).toHaveLength(1)
|
||||
expect(userTexts(requests[0])[0]).toContain("Earlier question")
|
||||
expect((yield* session.messages({ sessionID })).find((message) => message.id === compaction.id)).toMatchObject({
|
||||
type: "compaction",
|
||||
status: "completed",
|
||||
summary: "Manual summary",
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("preserves provider errors from manual compaction", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setup
|
||||
response = reply.text("Earlier answer", "text-manual-provider-history")
|
||||
yield* admit(session, "Earlier question")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
response = [LLMEvent.providerError({ message: "summary unavailable" })]
|
||||
const compaction = yield* session.compact({ sessionID })
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
expect((yield* session.messages({ sessionID })).find((message) => message.id === compaction.id)).toMatchObject({
|
||||
type: "compaction",
|
||||
status: "failed",
|
||||
error: { type: "provider.error", message: "summary unavailable" },
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("preserves typed provider failures from manual compaction", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setup
|
||||
response = reply.text("Earlier answer", "text-manual-failure-history")
|
||||
yield* admit(session, "Earlier question")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
responseStream = Stream.fail(providerUnavailable())
|
||||
const compaction = yield* session.compact({ sessionID })
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
expect((yield* session.messages({ sessionID })).find((message) => message.id === compaction.id)).toMatchObject({
|
||||
type: "compaction",
|
||||
status: "failed",
|
||||
error: { type: "provider.transport", message: "Provider unavailable" },
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("settles an admitted manual compaction when pre-start resolution throws", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* setup
|
||||
const session = yield* SessionV2.Service
|
||||
const session = yield* setup
|
||||
response = reply.text("Earlier answer", "text-manual-resolution-history")
|
||||
yield* admit(session, "Earlier question")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
const compaction = yield* session.compact({ sessionID })
|
||||
modelResolveHook = Effect.die("model resolution failed")
|
||||
|
||||
@@ -1659,6 +1728,46 @@ describe("SessionRunnerLLM", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("recovers from provider context overflow without a configured context limit", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setupOverflowRecovery
|
||||
currentModel = model
|
||||
responses = [
|
||||
[LLMEvent.providerError({ message: "prompt too long", classification: "context-overflow" })],
|
||||
reply.text("## Objective\n- Recover unknown limit", "text-summary-unknown-limit"),
|
||||
reply.text("Recovered", "text-final-unknown-limit"),
|
||||
]
|
||||
yield* admit(session, "Continue")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
expect(requests).toHaveLength(3)
|
||||
expect(yield* session.context(sessionID)).toMatchObject([
|
||||
{ type: "compaction", summary: "## Objective\n- Recover unknown limit" },
|
||||
{ type: "assistant", finish: "stop" },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("recovers from provider context overflow despite an undersized configured context limit", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setupOverflowRecovery
|
||||
currentModel = undersizedContextModel
|
||||
responses = [
|
||||
[LLMEvent.providerError({ message: "prompt too long", classification: "context-overflow" })],
|
||||
reply.text("## Objective\n- Recover undersized limit", "text-summary-undersized-limit"),
|
||||
reply.text("Recovered", "text-final-undersized-limit"),
|
||||
]
|
||||
yield* admit(session, "Continue")
|
||||
yield* session.resume(sessionID)
|
||||
|
||||
expect(requests).toHaveLength(3)
|
||||
expect(yield* session.context(sessionID)).toMatchObject([
|
||||
{ type: "compaction", summary: "## Objective\n- Recover undersized limit" },
|
||||
{ type: "assistant", finish: "stop" },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("persists a second context overflow after one recovery", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* setupOverflowRecovery
|
||||
@@ -1718,7 +1827,14 @@ describe("SessionRunnerLLM", () => {
|
||||
|
||||
expect(requests).toHaveLength(2)
|
||||
const context = yield* session.context(sessionID)
|
||||
expect(context).toContainEqual(expect.objectContaining({ type: "compaction", status: "failed", reason: "auto" }))
|
||||
expect(context).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: "compaction",
|
||||
status: "failed",
|
||||
reason: "auto",
|
||||
error: { type: "provider.error", message: "summary unavailable" },
|
||||
}),
|
||||
)
|
||||
expect(context.slice(-3)).toMatchObject([
|
||||
{ type: "user", text: "Continue" },
|
||||
{ type: "compaction", status: "failed", reason: "auto" },
|
||||
|
||||
Reference in New Issue
Block a user