keep recent turns during session compaction
This commit is contained in:
@@ -86,13 +86,14 @@ async function addAssistant(
|
||||
return id
|
||||
}
|
||||
|
||||
async function addCompactionPart(sessionID: SessionID, messageID: MessageID) {
|
||||
async function addCompactionPart(sessionID: SessionID, messageID: MessageID, tailStartID?: MessageID) {
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID,
|
||||
messageID,
|
||||
type: "compaction",
|
||||
auto: true,
|
||||
tail_start_id: tailStartID,
|
||||
} as any)
|
||||
}
|
||||
|
||||
@@ -759,6 +760,139 @@ describe("MessageV2.filterCompacted", () => {
|
||||
})
|
||||
})
|
||||
|
||||
test("retains original tail when compaction stores tail_start_id", async () => {
|
||||
await Instance.provide({
|
||||
directory: root,
|
||||
fn: async () => {
|
||||
const session = await Session.create({})
|
||||
|
||||
const u1 = await addUser(session.id, "first")
|
||||
const a1 = await addAssistant(session.id, u1, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a1,
|
||||
type: "text",
|
||||
text: "first reply",
|
||||
})
|
||||
|
||||
const u2 = await addUser(session.id, "second")
|
||||
const a2 = await addAssistant(session.id, u2, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a2,
|
||||
type: "text",
|
||||
text: "second reply",
|
||||
})
|
||||
|
||||
const c1 = await addUser(session.id)
|
||||
await addCompactionPart(session.id, c1, u2)
|
||||
const s1 = await addAssistant(session.id, c1, { summary: true, finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: s1,
|
||||
type: "text",
|
||||
text: "summary",
|
||||
})
|
||||
|
||||
const u3 = await addUser(session.id, "third")
|
||||
const a3 = await addAssistant(session.id, u3, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a3,
|
||||
type: "text",
|
||||
text: "third reply",
|
||||
})
|
||||
|
||||
const result = MessageV2.filterCompacted(MessageV2.stream(session.id))
|
||||
|
||||
expect(result.map((item) => item.info.id)).toEqual([u2, a2, c1, s1, u3, a3])
|
||||
|
||||
await Session.remove(session.id)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("prefers latest compaction boundary when repeated compactions exist", async () => {
|
||||
await Instance.provide({
|
||||
directory: root,
|
||||
fn: async () => {
|
||||
const session = await Session.create({})
|
||||
|
||||
const u1 = await addUser(session.id, "first")
|
||||
const a1 = await addAssistant(session.id, u1, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a1,
|
||||
type: "text",
|
||||
text: "first reply",
|
||||
})
|
||||
|
||||
const u2 = await addUser(session.id, "second")
|
||||
const a2 = await addAssistant(session.id, u2, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a2,
|
||||
type: "text",
|
||||
text: "second reply",
|
||||
})
|
||||
|
||||
const c1 = await addUser(session.id)
|
||||
await addCompactionPart(session.id, c1, u2)
|
||||
const s1 = await addAssistant(session.id, c1, { summary: true, finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: s1,
|
||||
type: "text",
|
||||
text: "summary one",
|
||||
})
|
||||
|
||||
const u3 = await addUser(session.id, "third")
|
||||
const a3 = await addAssistant(session.id, u3, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a3,
|
||||
type: "text",
|
||||
text: "third reply",
|
||||
})
|
||||
|
||||
const c2 = await addUser(session.id)
|
||||
await addCompactionPart(session.id, c2, u3)
|
||||
const s2 = await addAssistant(session.id, c2, { summary: true, finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: s2,
|
||||
type: "text",
|
||||
text: "summary two",
|
||||
})
|
||||
|
||||
const u4 = await addUser(session.id, "fourth")
|
||||
const a4 = await addAssistant(session.id, u4, { finish: "end_turn" })
|
||||
await Session.updatePart({
|
||||
id: PartID.ascending(),
|
||||
sessionID: session.id,
|
||||
messageID: a4,
|
||||
type: "text",
|
||||
text: "fourth reply",
|
||||
})
|
||||
|
||||
const result = MessageV2.filterCompacted(MessageV2.stream(session.id))
|
||||
|
||||
expect(result.map((item) => item.info.id)).toEqual([u3, a3, c2, s2, u4, a4])
|
||||
|
||||
await Session.remove(session.id)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("works with array input", () => {
|
||||
// filterCompacted accepts any Iterable, not just generators
|
||||
const id = MessageID.ascending()
|
||||
|
||||
Reference in New Issue
Block a user