fix: preserve agent context during compaction (#4556)

This commit is contained in:
Patrick Wolf 2025-11-21 00:13:10 -08:00 committed by GitHub
parent e018e16898
commit 416f2964b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 3 deletions

View file

@ -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,

View file

@ -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(),
},

View file

@ -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