Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor: simplify session cost tracking (#1456)

mldangelo-oai07c3783902c2
diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.ts
index c7cb785bd..851883500 100644
--- a/sdk/typescript/src/cost.ts
+++ b/sdk/typescript/src/cost.ts
@@ -1,6 +1,7 @@
import { parseJson } from "./value.js";
import { open, readdir } from "node:fs/promises";
import { join } from "node:path";
+import { StringDecoder } from "node:string_decoder";
import { isRecord } from "./record.js";
import {
estimateScanCost,
@@ -48,7 +49,8 @@ interface SessionReasoning {
interface SessionUsage {
offset: number;
- pendingLine: Buffer[];
+ decoder: StringDecoder;
+ pendingLine: string[];
unreadable: boolean;
threadId: string | null;
parentThreadId: string | null;
@@ -94,6 +96,7 @@ const SESSION_READ_SIZE = 64 * 1_024;
function createSessionUsage(): SessionUsage {
return {
offset: 0,
+ decoder: new StringDecoder("utf8"),
pendingLine: [],
unreadable: false,
threadId: null,
@@ -439,7 +442,14 @@ async function readSessionUsage(
if (bytesRead === 0) return;
session.offset += bytesRead;
try {
- readSessionChunk(buffer.subarray(0, bytesRead), session, repository);
+ const lines = session.decoder
+ .write(buffer.subarray(0, bytesRead))
+ .split("\n");
+ session.pendingLine.push(lines[0]!);
+ for (const line of lines.slice(1)) {
+ readSessionEvent(session.pendingLine.join(""), session, repository);
+ session.pendingLine = [line];
+ }
} catch (error) {
session.unreadable = true;
session.pendingLine = [];
@@ -451,39 +461,6 @@ async function readSessionUsage(
}
}
-function readSessionChunk(
- contents: Buffer,
- session: SessionUsage,
- repository?: string,
-): void {
- let lineStart = 0;
- while (lineStart < contents.length) {
- const newline = contents.indexOf(0x0a, lineStart);
- const lineEnd = newline === -1 ? contents.length : newline;
- const fragment = contents.subarray(lineStart, lineEnd);
-
- if (newline === -1) {
- if (fragment.length > 0) {
- session.pendingLine.push(Buffer.from(fragment));
- }
- return;
- }
-
- if (session.pendingLine.length === 0) {
- readSessionEvent(fragment.toString("utf8"), session, repository);
- } else {
- if (fragment.length > 0) session.pendingLine.push(Buffer.from(fragment));
- readSessionEvent(
- Buffer.concat(session.pendingLine).toString("utf8"),
- session,
- repository,
- );
- session.pendingLine = [];
- }
- lineStart = newline + 1;
- }
-}
-
function readSessionEvent(
line: string,
session: SessionUsage,
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index 573bf6e35..f5a9c69eb 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -56,6 +56,29 @@ async function workerSessionFixture() {
return { home, usage, parent, worker };
}
+function costTracker(
+ home: string,
+ options: Omit<
+ ConstructorParameters<typeof ScanCostTracker>[0],
+ "codexHome" | "model"
+ > = {},
+) {
+ return new ScanCostTracker({
+ codexHome: home,
+ model: "gpt-5.6-sol",
+ ...options,
+ });
+}
+
+function activityTracker(home: string) {
+ const activities: ScanActivity[] = [];
+ const tracker = costTracker(home, {
+ repository: "/code/juice-shop",
+ onActivity: (activity) => activities.push(activity),
+ });
+ return { activities, tracker };
+}
+
async function appendSessionItem(
path: string,
payload: Readonly<Record<string, unknown>>,
@@ -457,9 +480,7 @@ describe("live scan cost tracking", () => {
output_tokens: 10,
});
const releases: Array<() => void> = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
maxCostUsd: 1,
});
const refresh = tracker.refresh.bind(tracker);
@@ -491,9 +512,7 @@ describe("live scan cost tracking", () => {
let traversals = 0;
const { promise: blocked, resolve: release } =
Promise.withResolvers<void>();
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
maxCostUsd: 1,
onError: (error) => {
if (error instanceof Error) errors.push(error.message);
@@ -529,9 +548,7 @@ describe("live scan cost tracking", () => {
});
const { promise: reportedCost, resolve: reportCost } =
Promise.withResolvers<unknown>();
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
onCost: reportCost,
});
tracker.start("scan-thread");
@@ -574,9 +591,7 @@ describe("live scan cost tracking", () => {
{ parent: "other-scan" },
);
const workers: ScanWorkerEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
onWorkerEvent: (event) => workers.push(event),
});
tracker.start("scan-thread");
@@ -609,13 +624,10 @@ describe("live scan cost tracking", () => {
test("polls for workers with only a worker observer and keeps concurrent scans separate", async () => {
const home = await codexHome();
const workers: ScanWorkerEvent[][] = [[], []];
- const trackers = workers.map(
- (events) =>
- new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- onWorkerEvent: (event) => events.push(event),
- }),
+ const trackers = workers.map((events) =>
+ costTracker(home, {
+ onWorkerEvent: (event) => events.push(event),
+ }),
);
trackers.forEach((tracker, index) => tracker.start(`scan-${index}`));
try {
@@ -631,9 +643,7 @@ describe("live scan cost tracking", () => {
await Promise.all(trackers.map((tracker) => tracker.stop()));
}
const resumed: ScanWorkerEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
onWorkerEvent: (event) => resumed.push(event),
});
tracker.start("scan-0");
@@ -666,9 +676,7 @@ describe("live scan cost tracking", () => {
output_tokens: 1_000_000,
});
const events: ScanSessionEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
onSessionEvent: (event) => events.push(event),
});
tracker.start("scan-thread");
@@ -755,10 +763,8 @@ describe("live scan cost tracking", () => {
await appendSessionItem(unrelated, message("Unrelated output."));
const events: ScanSessionEvent[] = [];
const workers: ScanWorkerEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
+ const tracker = costTracker(home, {
scanDirectory,
- model: "gpt-5.6-sol",
onSessionEvent: (event) => events.push(event),
onWorkerEvent: (event) => workers.push(event),
});
@@ -973,9 +979,7 @@ describe("live scan cost tracking", () => {
);
const events: ScanSessionEvent[] = [];
const workers: ScanWorkerEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
scanDirectory,
onSessionEvent: (event) => events.push(event),
onWorkerEvent: (event) => workers.push(event),
@@ -1114,9 +1118,7 @@ describe("live scan cost tracking", () => {
{ parent: "bystander" },
);
const events: ScanSessionEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
scanDirectory,
maxCostUsd: 0.01,
onSessionEvent: (event) => events.push(event),
@@ -1171,9 +1173,7 @@ describe("live scan cost tracking", () => {
{ input_tokens: 250, output_tokens: 2 },
{ parent: "scan-thread" },
);
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
scanDirectory,
});
tracker.start("scan-thread");
@@ -1387,9 +1387,7 @@ describe("live scan cost tracking", () => {
const maxCostUsd = 0.001;
const observedCosts: number[] = [];
const forwardedEvents: ScanSessionEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
maxCostUsd,
onCost: ({ estimatedUsd }) => observedCosts.push(estimatedUsd),
onSessionEvent: (event) => forwardedEvents.push(event),
@@ -1461,9 +1459,7 @@ describe("live scan cost tracking", () => {
const activities: ScanActivity[] = [];
const events: ScanSessionEvent[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
repository: "/code/juice-shop",
onActivity: (activity) => activities.push(activity),
onSessionEvent: (event) => events.push(event),
@@ -1514,13 +1510,7 @@ describe("live scan cost tracking", () => {
content: [{ type: "output_text", text: "The query uses request input." }],
});
- const activities: ScanActivity[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- repository: "/code/juice-shop",
- onActivity: (activity) => activities.push(activity),
- });
+ const { activities, tracker } = activityTracker(home);
tracker.start("scan-thread");
await tracker.stop();
@@ -1595,9 +1585,7 @@ describe("live scan cost tracking", () => {
const activities: ScanActivity[] = [];
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
repository: "/code/juice-shop",
expectedFilesTotal: 8,
onActivity: (activity) => activities.push(activity),
@@ -1702,13 +1690,7 @@ describe("live scan cost tracking", () => {
.join("\n"),
);
- const activities: ScanActivity[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- repository: "/code/juice-shop",
- onActivity: (activity) => activities.push(activity),
- });
+ const { activities, tracker } = activityTracker(home);
tracker.start("scan-thread");
await tracker.stop();
@@ -1766,13 +1748,7 @@ describe("live scan cost tracking", () => {
.join("\n"),
);
- const activities: ScanActivity[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- repository: "/code/juice-shop",
- onActivity: (activity) => activities.push(activity),
- });
+ const { activities, tracker } = activityTracker(home);
tracker.start("scan-thread");
await tracker.stop();
@@ -1803,13 +1779,7 @@ describe("live scan cost tracking", () => {
encrypted_content: "must-never-be-displayed",
});
- const activities: ScanActivity[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- repository: "/code/juice-shop",
- onActivity: (activity) => activities.push(activity),
- });
+ const { activities, tracker } = activityTracker(home);
tracker.start("scan-thread");
await tracker.stop();
@@ -1848,9 +1818,7 @@ describe("live scan cost tracking", () => {
await appendSessionItem(unrelated, progressMessage(7));
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 8,
onProgress: (progress) => updates.push(progress),
});
@@ -1871,9 +1839,7 @@ describe("live scan cost tracking", () => {
});
const unrelated = await writeSession(home, "unrelated-thread", usage);
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 1_258,
onProgress: (progress) => updates.push(progress),
});
@@ -1937,9 +1903,7 @@ describe("live scan cost tracking", () => {
});
const unrelated = await writeSession(home, "unrelated-worker", usage);
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 4_198,
onProgress: (progress) => updates.push(progress),
});
@@ -2033,9 +1997,7 @@ describe("live scan cost tracking", () => {
}
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 8,
onProgress: (progress) => updates.push(progress),
});
@@ -2055,9 +2017,7 @@ describe("live scan cost tracking", () => {
const { promise: reportedProgress, resolve: reportProgress } =
Promise.withResolvers<ScanProgress>();
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 8,
onProgress: reportProgress,
});
@@ -2077,9 +2037,7 @@ describe("live scan cost tracking", () => {
test("reports newly completed worker batches once per progress update", async () => {
const { home, worker } = await workerSessionFixture();
const updates: ScanProgress[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
expectedFilesTotal: 8,
onProgress: (progress) => updates.push(progress),
});
@@ -2153,17 +2111,26 @@ describe("live scan cost tracking", () => {
type: "event_msg",
payload: {
type: "token_count",
+ details: "😀",
info: {
total_token_usage: { input_tokens: 250, output_tokens: 20 },
},
},
});
+ const encoded = Buffer.from(event);
+ const split = encoded.indexOf(Buffer.from("😀")) + 2;
const padding = " ".repeat(128 * 1_024);
- await appendFile(path, `${padding}${event.slice(0, 40)}`);
+ await appendFile(
+ path,
+ Buffer.concat([Buffer.from(padding), encoded.subarray(0, split)]),
+ );
expect((await tracker.refresh()).cost?.inputTokens).toBe(100);
expect(events).toHaveLength(2);
- await appendFile(path, `${event.slice(40)}\n`);
+ await appendFile(
+ path,
+ Buffer.concat([encoded.subarray(split), Buffer.from("\n")]),
+ );
expect((await tracker.stop()).cost?.inputTokens).toBe(250);
expect(events).toHaveLength(3);
expect(events.at(-1)?.event).toEqual(JSON.parse(event));
@@ -2206,9 +2173,7 @@ describe("live scan cost tracking", () => {
output_tokens: 30,
});
const updates: number[] = [];
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
+ const tracker = costTracker(home, {
maxCostUsd: 0.005,
onCost: (cost) => updates.push(cost.estimatedUsd),
});
@@ -2235,10 +2200,7 @@ describe("live scan cost tracking", () => {
{ input_tokens: 100, output_tokens: 0 },
{ parent: "scan-thread" },
);
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- });
+ const tracker = costTracker(home);
tracker.start("scan-thread");
const snapshot = await tracker.stop({
@@ -2283,10 +2245,7 @@ describe("live scan cost tracking", () => {
"accounts for a separate validation turn with %s usage",
async (source) => {
const home = await codexHome();
- const tracker = new ScanCostTracker({
- codexHome: home,
- model: "gpt-5.6-sol",
- });
+ const tracker = costTracker(home);
tracker.start("scan-thread");
const usage = { input_tokens: 500, output_tokens: 0 };
tracker.recordUsage(