perf(usage): the first usage scan skips decoding transcript lines that carry no usage (#17255)
This commit is contained in:
1 parent
499a53c307
commit
7f02b84958
6 files changed
+93
-19
No files matched your search
@@ -8,7 +8,7 @@ import * as NodePath from "node:path";
|
||||
import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest";
|
||||
|
||||
import { TEST_FORMATS } from "./usageTestFormats.ts";
|
||||
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
|
||||
import { readTranscriptRecords, transcriptLineGate } from "./usageTranscriptReader.ts";
|
||||
|
||||
let dir: string;
|
||||
|
||||
@@ -218,9 +218,63 @@ describe("readTranscriptRecords resume", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a CRLF Codex reducer line whose marker straddles a stream chunk", async () => {
|
||||
const path = NodePath.join(dir, "rollout.jsonl");
|
||||
const crlf = (line: string) => line.replace("\n", "\r\n");
|
||||
const modelLine = crlf(codexModelLine("gpt-5.2-codex"));
|
||||
// Multi-byte tool output that fails the gate, sized so the 256 KiB chunk
|
||||
// boundary falls inside the model line's `"turn_context"` marker.
|
||||
const head = crlf(codexMetaLine());
|
||||
const fillerBytes =
|
||||
256 * 1024 - Buffer.byteLength(head) - modelLine.indexOf("turn_context") - 4;
|
||||
const fillerEnvelope = crlf(`${JSON.stringify({ type: "response_item", output: "" })}\n`);
|
||||
const padding = "é".repeat((fillerBytes - Buffer.byteLength(fillerEnvelope)) / 2);
|
||||
const filler = crlf(`${JSON.stringify({ type: "response_item", output: padding })}\n`);
|
||||
const content = head + filler + modelLine + crlf(codexUsageLine(40, 2));
|
||||
await NodeFSP.writeFile(path, content);
|
||||
|
||||
const result = await readTranscriptRecords(path, TEST_FORMATS.codex);
|
||||
assert.isNotNull(result);
|
||||
assert.strictEqual(result.records.length, 1);
|
||||
assert.strictEqual(result.records[0]?.model, "gpt-5.2-codex");
|
||||
assert.strictEqual(result.records[0]?.sessionId, "codex-session-1");
|
||||
assert.strictEqual(result.position.resumeOffset, Buffer.byteLength(content));
|
||||
});
|
||||
|
||||
it("returns null for an unreadable file", async () => {
|
||||
assert.isNull(
|
||||
await readTranscriptRecords(NodePath.join(dir, "missing.jsonl"), TEST_FORMATS.claude),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("transcriptLineGate", () => {
|
||||
const gates = {
|
||||
one: transcriptLineGate(['"usage"']),
|
||||
several: transcriptLineGate(['"token_count"', '"session_meta"', '"a.b(c)"']),
|
||||
};
|
||||
const markers = { one: ['"usage"'], several: ['"token_count"', '"session_meta"', '"a.b(c)"'] };
|
||||
|
||||
it("passes only lines containing one of its markers", () => {
|
||||
for (const kind of ["one", "several"] as const) {
|
||||
for (const marker of markers[kind]) {
|
||||
assert.isTrue(gates[kind](Buffer.from(`{"payload":{${marker}:1}}`)));
|
||||
// The same text without its quotes, or with one character changed.
|
||||
assert.isFalse(gates[kind](Buffer.from(`{"text":"see ${marker.slice(1, -1)} here"}`)));
|
||||
assert.isFalse(gates[kind](Buffer.from(`{${marker.replace(/[._]|usage/, "x")}:1}`)));
|
||||
}
|
||||
assert.isFalse(gates[kind](Buffer.alloc(0)));
|
||||
}
|
||||
});
|
||||
|
||||
it("matches markers beside multi-byte and malformed UTF-8", () => {
|
||||
const invalid = Buffer.from([0xff, 0xc3]);
|
||||
for (const kind of ["one", "several"] as const) {
|
||||
for (const marker of markers[kind]) {
|
||||
assert.isTrue(gates[kind](Buffer.from(`{"text":"日本語 é",${marker}:1}`)));
|
||||
assert.isTrue(gates[kind](Buffer.concat([invalid, Buffer.from(marker), invalid])));
|
||||
assert.isFalse(gates[kind](Buffer.from(`{"text":"日本語 é ${marker.slice(1, -1)}"}`)));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user