Skip to content
Merged
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
22 changes: 15 additions & 7 deletions lib/v2/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,23 @@ function output(content: ReadonlyArray<{ type: string; text?: string }>): unknow

export function project(native: Message[], entries: History, session: Session) {
const byID = new Map(entries.map((entry) => [entry.id, entry]))
const results = new Map<string, ToolResultPart>()
const owners = new Map<string, string>()
// Providers can reuse tool call IDs across turns, so pair each result with the
// closest preceding call that has the same ID.
const calls = new Map<string, { part: ContentPart; messageId?: string }>()
const results = new Map<ContentPart, ToolResultPart>()
const owners = new Map<ContentPart, string>()
const links = new Map<ContentPart, Part>()
const originals = new Map<string, WithParts>()
const messages: WithParts[] = []
for (const message of native)
for (const part of message.content) {
if (part.type === "tool-result") results.set(part.id, part)
if (part.type === "tool-call" && message.id) owners.set(part.id, message.id)
if (part.type === "tool-call") calls.set(part.id, { part, messageId: message.id })
if (part.type === "tool-result") {
const call = calls.get(part.id)
if (!call) continue
results.set(call.part, part)
if (call.messageId) owners.set(part, call.messageId)
}
}
for (const message of native) {
if (!message.id || !["user", "assistant"].includes(message.role)) continue
Expand All @@ -122,7 +130,7 @@ export function project(native: Message[], entries: History, session: Session) {
url: typeof part.data === "string" ? part.data : "",
}
if (part.type === "tool-call") {
const result = results.get(part.id)
const result = results.get(part)
const entry = byID.get(message.id)
const durable =
entry?.type === "assistant"
Expand Down Expand Up @@ -206,8 +214,8 @@ export function project(native: Message[], entries: History, session: Session) {
if (part.type === "tool-call") content.push(...added.splice(0))
if (
part.type === "tool-result" &&
originals.has(owners.get(part.id)!) &&
!retained.has(owners.get(part.id)!)
originals.has(owners.get(part)!) &&
!retained.has(owners.get(part)!)
)
continue
const edit = links.get(part)
Expand Down
37 changes: 37 additions & 0 deletions tests/v2-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,43 @@ test("V2 inserts summaries after native checkpoints without an ordinary user mes
assert.deepEqual(restored[1]?.content, [{ type: "text", text: "CHECKPOINT_SUMMARY" }])
})

test("V2 compression keeps results paired when tool call IDs are reused", () => {
const call = (input: string) => ({
type: "tool-call",
id: "bash:0",
name: "bash",
input: { command: input },
})
const result = (value: string) => ({
role: "tool",
content: [
{ type: "tool-result", id: "bash:0", name: "bash", result: { type: "text", value } },
],
})
const native = [
{ id: "msg_user", role: "user", content: [{ type: "text", text: "Run both" }] },
{ id: "msg_first", role: "assistant", content: [call("echo one")] },
result("one"),
{ id: "msg_second", role: "assistant", content: [call("echo two")] },
result("two"),
{ id: "msg_latest", role: "user", content: [{ type: "text", text: "Continue" }] },
] as Message[]
const view = project(native, entries(native), session)
const outputs = view.messages.flatMap((message) =>
message.parts.flatMap((part) => (part.type === "tool" ? [part.state.output] : [])),
)
assert.deepEqual(outputs, ["one", "two"])

const state = createSessionState("compact")
state.prune.messages.byMessageId.set("msg_first", {
tokenCount: 100,
allBlockIds: [1],
activeBlockIds: [1],
})
prune(state, logger, config, view.messages)
assert.deepEqual(view.restore(), [native[0], native[3], native[4], native[5]])
})

test("completed V2 compaction entries count as native compaction", () => {
const entries = [
{ id: "cmp_one", type: "compaction", status: "completed", time: { created: 5 } },
Expand Down
Loading