From 416f2964b56dd92b69122040e010a6400cfaf644 Mon Sep 17 00:00:00 2001 From: Patrick Wolf Date: Fri, 21 Nov 2025 00:13:10 -0800 Subject: [PATCH] fix: preserve agent context during compaction (#4556) --- packages/opencode/src/server/server.ts | 10 ++++++++++ packages/opencode/src/session/compaction.ts | 8 +++++--- packages/opencode/src/session/prompt.ts | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/server/server.ts b/packages/opencode/src/server/server.ts index 1ab4185c2..55f62fd7f 100644 --- a/packages/opencode/src/server/server.ts +++ b/packages/opencode/src/server/server.ts @@ -813,8 +813,18 @@ export namespace Server { async (c) => { const id = c.req.valid("param").id const body = c.req.valid("json") + const msgs = await Session.messages({ sessionID: id }) + let currentAgent = "build" + for (let i = msgs.length - 1; i >= 0; i--) { + const info = msgs[i].info + if (info.role === "user") { + currentAgent = info.agent || "build" + break + } + } await SessionCompaction.create({ sessionID: id, + agent: currentAgent, model: { providerID: body.providerID, modelID: body.modelID, diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index 6bad9bbad..b5af11ef8 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -92,6 +92,7 @@ export namespace SessionCompaction { providerID: string modelID: string } + agent: string abort: AbortSignal }) { const model = await Provider.getModel(input.model.providerID, input.model.modelID) @@ -101,7 +102,7 @@ export namespace SessionCompaction { role: "assistant", parentID: input.parentID, sessionID: input.sessionID, - mode: "build", + mode: input.agent, summary: true, path: { cwd: Instance.directory, @@ -199,7 +200,7 @@ export namespace SessionCompaction { time: { created: Date.now(), }, - agent: "build", + agent: input.agent, model: input.model, }) await Session.updatePart({ @@ -222,6 +223,7 @@ export namespace SessionCompaction { export const create = fn( z.object({ sessionID: Identifier.schema("session"), + agent: z.string(), model: z.object({ providerID: z.string(), modelID: z.string(), @@ -233,7 +235,7 @@ export namespace SessionCompaction { role: "user", model: input.model, sessionID: input.sessionID, - agent: "build", + agent: input.agent, time: { created: Date.now(), }, diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index ff060ac21..74ddf1145 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -398,6 +398,7 @@ export namespace SessionPrompt { messages: msgs, parentID: lastUser.id, abort, + agent: lastUser.agent, model: { providerID: model.providerID, modelID: model.modelID, @@ -416,6 +417,7 @@ export namespace SessionPrompt { ) { await SessionCompaction.create({ sessionID, + agent: lastUser.agent, model: lastUser.model, }) continue