Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions packages/core/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export const buildPrompt = (input: { readonly previousSummary?: string; readonly
].join("\n\n")
}

export const buildReplayPrompt = () =>
[
"Create a new anchored summary from the conversation messages above so another coding agent can continue the work.",
"Do not continue the task or call tools. Output only the summary.",
SUMMARY_TEMPLATE,
].join("\n\n")

export const make = (dependencies: Dependencies) => {
const config = settings(dependencies.config)
const compactAfterOverflow = Effect.fn("SessionCompaction.compactAfterOverflow")(function* (input: Input) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/v1/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export const Info = Schema.Struct({
reserved: Schema.optional(NonNegativeInt).annotate({
description: "Token buffer for compaction. Leaves enough window to avoid overflow during compaction.",
}),
preserve_prefix_cache: Schema.optional(Schema.Boolean).annotate({
description:
"Reuse the current request prefix during compaction for supported OpenAI-compatible providers. Falls back to serialized compaction when replay is unsafe (experimental, default: false)",
}),
}),
),
experimental: Schema.optional(
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/session-compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ test("compaction prompt gives update instructions for a prior summary", () => {
expect(prompt).toContain('Update "Objective" and "Next Move" to reflect the current work state.')
})

test("replay prompt summarizes preceding messages", () => {
const prompt = SessionCompaction.buildReplayPrompt()
expect(prompt).toContain("conversation messages above")
expect(prompt).toContain("Do not continue the task or call tools")
expect(prompt).not.toContain("<conversation>")
})

test("compaction describes tool media without embedding base64", () => {
const base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
const serialized = SessionCompaction.serializeToolContent([
Expand Down
155 changes: 114 additions & 41 deletions packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { RuntimeFlags } from "@/effect/runtime-flags"
import { EventV2Bridge } from "@/event-v2-bridge"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"
import { buildPrompt } from "@opencode-ai/core/session/compaction"
import { buildPrompt, buildReplayPrompt } from "@opencode-ai/core/session/compaction"
import { SessionCompactionEvent } from "@opencode-ai/schema/session-compaction-event"

export const Event = SessionCompactionEvent
Expand Down Expand Up @@ -162,6 +162,26 @@ function splitTurn(input: {
})
}

function containsMedia(messages: SessionV1.WithParts[]) {
return messages.some((message) =>
message.parts.some((part) => {
if (part.type === "file") return MessageV2.isMedia(part.mime)
if (part.type !== "tool" || part.state.status !== "completed") return false
return part.state.attachments?.some((attachment) => MessageV2.isMedia(attachment.mime)) ?? false
}),
)
}

export type Run = (input: {
user: SessionV1.User
agent: Agent.Info
model: Provider.Model
processor: SessionProcessor.Handle
messages: SessionV1.WithParts[]
prompt: string
bypassAgentCheck: boolean
}) => Effect.Effect<SessionProcessor.Result>

export interface Interface {
readonly isOverflow: (input: {
tokens: SessionV1.Assistant["tokens"]
Expand All @@ -174,6 +194,7 @@ export interface Interface {
sessionID: SessionID
auto: boolean
overflow?: boolean
run?: Run
}) => Effect.Effect<"continue" | "stop">
readonly create: (input: {
sessionID: SessionID
Expand Down Expand Up @@ -322,6 +343,7 @@ const layer = Layer.effect(
sessionID: SessionID
auto: boolean
overflow?: boolean
run?: Run
}) {
const parent = input.messages.findLast((m) => m.info.id === input.parentID)
if (!parent || parent.info.role !== "user") {
Expand Down Expand Up @@ -355,40 +377,77 @@ const layer = Layer.effect(
}
}

const agent = yield* agents.get("compaction")
const model = agent.model
? yield* provider.getModel(agent.model.providerID, agent.model.modelID).pipe(Effect.orDie)
const compactionAgent = yield* agents.get("compaction")
const compactionModel = compactionAgent.model
? yield* provider.getModel(compactionAgent.model.providerID, compactionAgent.model.modelID).pipe(Effect.orDie)
: yield* provider.getModel(userMessage.model.providerID, userMessage.model.modelID).pipe(Effect.orDie)
const cfg = yield* config.get()
const history = compactionPart && messages.at(-1)?.info.id === input.parentID ? messages.slice(0, -1) : messages
const prior = completedCompactions(history)
const hidden = new Set(prior.flatMap((item) => [item.userIndex, item.assistantIndex]))
const previousSummary = prior.at(-1)?.summary
const filtered = history.filter((_, index) => !hidden.has(index))
// Replay cannot merge an earlier summary without changing the cached prefix.
const replayRequest =
cfg.compaction?.preserve_prefix_cache === true &&
input.run !== undefined &&
input.overflow !== true &&
cfg.agent?.compaction === undefined &&
prior.length === 0
? { run: input.run }
: undefined
const active = replayRequest
? history.findLast(
(message): message is SessionV1.WithParts & { info: SessionV1.User } =>
message.info.role === "user" && !message.parts.some((part) => part.type === "compaction"),
)
: undefined
const activeAgent = active ? yield* agents.get(active.info.agent) : undefined
const activeModel = active
? yield* provider.getModel(active.info.model.providerID, active.info.model.modelID).pipe(Effect.orDie)
: undefined
const replayModel =
active !== undefined &&
active.info.format?.type !== "json_schema" &&
activeAgent !== undefined &&
activeModel !== undefined &&
activeModel.api.npm === "@ai-sdk/openai-compatible" &&
!activeModel.providerID.startsWith("opencode") &&
activeModel.id === compactionModel.id &&
activeModel.providerID === compactionModel.providerID
? { user: active, agent: activeAgent, model: activeModel }
: undefined
const selected = yield* select({
messages: history.filter((_, index) => !hidden.has(index)),
messages: filtered,
cfg,
model,
model: replayModel?.model ?? compactionModel,
})
const tail = selected.tail_start_id
? filtered.find((message) => message.info.id === selected.tail_start_id)
: undefined
// Structured replay is deliberately narrow; every other request keeps the
// provider-safe serialized path.
const replayCandidate =
replayRequest && replayModel !== undefined && tail?.info.role === "user" && !containsMedia(selected.head)
? { ...replayModel, run: replayRequest.run }
: undefined
// Allow plugins to inject context or replace compaction prompt.
const compacting = yield* plugin.trigger(
"experimental.session.compacting",
{ sessionID: input.sessionID },
{ context: [], prompt: undefined },
)
const replayContext =
compacting.prompt === undefined && compacting.context.length === 0 ? replayCandidate : undefined
const agent = replayContext?.agent ?? compactionAgent
const model = replayContext?.model ?? compactionModel
const msgs = structuredClone(selected.head)
yield* plugin.trigger("experimental.chat.messages.transform", {}, { messages: msgs })
const conversation = msgs.map(serialize).filter(Boolean).join("\n\n")
const nextPrompt =
const fallbackPrompt =
compacting.prompt ??
[
buildPrompt({
previousSummary,
context: [conversation],
}),
...compacting.context,
]
.filter(Boolean)
.join("\n\n")
[buildPrompt({ previousSummary, context: [conversation] }), ...compacting.context].filter(Boolean).join("\n\n")
const nextPrompt = replayContext ? buildReplayPrompt() : fallbackPrompt
const ctx = yield* InstanceState.context
const msg: SessionV1.Assistant = {
id: MessageID.ascending(),
Expand All @@ -397,7 +456,7 @@ const layer = Layer.effect(
sessionID: input.sessionID,
mode: "compaction",
agent: "compaction",
variant: userMessage.model.variant,
variant: replayContext?.user.info.model.variant ?? userMessage.model.variant,
summary: true,
path: {
cwd: ctx.directory,
Expand All @@ -422,30 +481,44 @@ const layer = Layer.effect(
sessionID: input.sessionID,
model,
})
const result = yield* processor.process({
user: userMessage,
agent,
sessionID: input.sessionID,
tools: {},
system: [],
messages: [
{
role: "user",
content: [
{
type: "text",
text: [
nextPrompt,
...(compacting.prompt ? ["The following is the conversation history:", conversation] : []),
]
.filter(Boolean)
.join("\n\n"),
},
],
},
],
model,
})
const runSerialized = () =>
processor.process({
user: userMessage,
agent: compactionAgent,
sessionID: input.sessionID,
tools: {},
system: [],
messages: [
{
role: "user",
content: [
{
type: "text",
text: [
fallbackPrompt,
...(compacting.prompt ? ["The following is the conversation history:", conversation] : []),
]
.filter(Boolean)
.join("\n\n"),
},
],
},
],
model: compactionModel,
})
const result = replayContext
? yield* replayContext
.run({
user: replayContext.user.info,
agent,
model,
processor,
messages: msgs,
prompt: nextPrompt,
bypassAgentCheck: replayContext.user.parts.some((part) => part.type === "agent"),
})
.pipe(Effect.flatMap((result) => (result === "compact" ? runSerialized() : Effect.succeed(result))))
: yield* runSerialized()

if (result === "compact") {
processor.message.error = new SessionV1.ContextOverflowError({
Expand Down
Loading
Loading