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
23 changes: 23 additions & 0 deletions packages/agent/src/adapters/claude/session/jsonl-hydration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,29 @@ describe("conversationTurnsToJsonlEntries", () => {
expect(conv[1].parentUuid).toBe(conv[0].uuid);
});

it("seeds only the last assistant message with the estimated context size", () => {
const lines = conversationTurnsToJsonlEntries(
[
{ role: "user", content: [{ type: "text", text: "q".repeat(400) }] },
{
role: "assistant",
content: [{ type: "text", text: "a".repeat(400) }],
},
{ role: "user", content: [{ type: "text", text: "more" }] },
{ role: "assistant", content: [{ type: "text", text: "final" }] },
],
config,
);

const assistants = parseConversationEntries(lines).filter(
(entry) => entry.type === "assistant",
);
expect(assistants.at(-1)?.message.usage.input_tokens).toBeGreaterThan(0);
for (const entry of assistants.slice(0, -1)) {
expect(entry.message.usage.input_tokens).toBe(0);
}
});

it("emits one line per assistant block with shared message id", () => {
const lines = conversationTurnsToJsonlEntries(
[
Expand Down
10 changes: 9 additions & 1 deletion packages/agent/src/adapters/claude/session/jsonl-hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ export function conversationTurnsToJsonlEntries(
const permissionMode = config.permissionMode ?? "default";
const baseTime = Date.now() - turns.length * 3000;
let turnIndex = 0;
// Seed the last assistant message with the estimated context size so the
// SDK's auto-compact accounting isn't blind until the first API response.
const estimatedTokens = turns.reduce(
(sum, turn) => sum + estimateTurnTokens(turn),
0,
);
const lastAssistantTurn = turns.findLast((turn) => turn.role === "assistant");

for (const turn of turns) {
const timestamp = new Date(baseTime + turnIndex * 3000).toISOString();
Expand Down Expand Up @@ -530,7 +537,8 @@ export function conversationTurnsToJsonlEntries(
stop_reason: isLast ? lastStopReason : null,
stop_sequence: null,
usage: {
input_tokens: 0,
input_tokens:
turn === lastAssistantTurn && isLast ? estimatedTokens : 0,
cache_creation_input_tokens: 0,
cache_read_input_tokens: 0,
output_tokens: 0,
Expand Down
Loading