OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(cost): preserve worker activity when a session log is unreadable (#970)
Chen Yufeiyang6703d0a3b2b7
diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.tsindex db03c6180..acce8e102 100644--- a/sdk/typescript/src/cost.ts+++ b/sdk/typescript/src/cost.ts@@ -2272,21 +2272,36 @@ export class ScanCostTracker {: this.#options.repository;const progressThread =this.#options.onProgress === undefined ? undefined : this.#threadId;- const unreadable: Array<{ session: SessionUsage; error: unknown }> = [];- for await (const path of sessionFiles(- join(this.#options.codexHome, "sessions"),- )) {- let session = this.#sessions.get(path);- if (session === undefined) {- session = createSessionUsage(observeUsage);- this.#sessions.set(path, session);- }- try {- await readSessionUsage(path, session, repository, progressThread);- } catch (error) {- if (session.threadId === null) throw error;- unreadable.push({ session, error });+ const observeHealthySessions =+ this.#options.maxCostUsd === undefined &&+ (this.#options.onActivity !== undefined ||+ this.#options.onProgress !== undefined ||+ this.#options.onSessionEvent !== undefined ||+ this.#options.onWorkerEvent !== undefined);+ const unreadable = new Map<SessionUsage, unknown>();+ let blocked: [SessionUsage, unknown] | undefined;+ try {+ for await (const path of sessionFiles(+ join(this.#options.codexHome, "sessions"),+ )) {+ let session = this.#sessions.get(path);+ if (session === undefined) {+ session = createSessionUsage(observeUsage);+ this.#sessions.set(path, session);+ }+ try {+ await readSessionUsage(path, session, repository, progressThread);+ } catch (error) {+ if (session.threadId === null) {+ if (!observeHealthySessions || !isDeniedLogRead(error)) throw error;+ blocked ??= [session, error];+ }+ unreadable.set(session, error);+ }}+ } catch (error) {+ // Traversal stopped. Preserve the read error that already blocked this poll.+ if (!blocked) throw error;}const included = new Set([this.#threadId, ...this.#receipts.keys()]);@@ -2329,40 +2344,55 @@ export class ScanCostTracker {}}} while (included.size !== previousSize);- for (const { session, error } of unreadable) {- if (included.has(session.threadId!)) throw error;- }+ blocked ??= unreadable+ .entries()+ .find(+ ([session]) =>+ session.threadId !== null && included.has(session.threadId),+ );+ if (blocked && !observeHealthySessions) throw blocked[1];const usages = new Map(this.#receipts);for (const [path, session] of this.#sessions) {const threadId = session.threadId;- if (threadId === null || !included.has(threadId)) continue;+ if (+ threadId === null ||+ !included.has(threadId) ||+ unreadable.has(session)+ )+ continue;if (this.#options.onSessionEvent !== undefined &&session.events === undefined) {- // Replay event history through the already-read snapshot without- // replacing its authoritative progress, usage, or partial-line decoder.- const replay: SessionUsage = createSessionUsage(false);- replay.events = [];- await readSessionUsage(- path,- replay,- undefined,- undefined,- session.offset,- );- if (replay.offset === session.offset) {- session.events = replay.events;- // At stop there is no next poll to read usage appended during replay.- if (this.#timer === null)- await readSessionUsage(- path,- session,- repository,- progressThread,- "file-size",- );+ try {+ // Replay event history through the already-read snapshot without+ // replacing its authoritative progress, usage, or partial-line decoder.+ const replay: SessionUsage = createSessionUsage(false);+ replay.events = [];+ await readSessionUsage(+ path,+ replay,+ undefined,+ undefined,+ session.offset,+ );+ if (replay.offset === session.offset) {+ session.events = replay.events;+ // At stop there is no next poll to read usage appended during replay.+ if (this.#timer === null)+ await readSessionUsage(+ path,+ session,+ repository,+ progressThread,+ "file-size",+ );+ }+ } catch (error) {+ if (!observeHealthySessions) throw error;+ blocked ??= [session, error];+ continue;}}let worker: number | undefined;@@ -2443,6 +2473,8 @@ export class ScanCostTracker {usages.set(threadId, session.usage);}}+ // Healthy sessions can report activity even when a read blocks the total.+ if (blocked) throw blocked[1];let usage: ScanTokenUsage | null = null;let complete = true;for (const value of usages.values()) {@@ -3644,6 +3676,14 @@ function subtractTokenUsage(});}+function isDeniedLogRead(error: unknown): boolean {+ return (+ isRecord(error) &&+ (error["code"] === "EACCES" || error["code"] === "EPERM") &&+ (error["syscall"] === "open" || error["syscall"] === "read")+ );+}+function isMissingFile(error: unknown): boolean {return isRecord(error) && error["code"] === "ENOENT";}diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.tsindex 3d19b3b74..a2ac8b73b 100644--- a/sdk/typescript/tests-ts/cost.test.ts+++ b/sdk/typescript/tests-ts/cost.test.ts@@ -3177,14 +3177,55 @@ describe("live scan cost tracking", () => {},);+ test.each(["EACCES", "EPERM"])(+ "stops at the first unidentified log whose stat fails with %s on each poll",+ async (code) => {+ const home = await codexHome();+ await Promise.all(+ ["a", "b", "c", "d"].map((id) =>+ writeSession(home, id, { input_tokens: 10 }),+ ),+ );+ const options: ConstructorParameters<typeof ScanCostTracker>[0] = {+ codexHome: home,+ model: "gpt-5.6-sol",+ };+ const tracker = new ScanCostTracker(options);+ tracker.start("scan-thread");+ options.onSessionEvent = () => {};+ try {+ using stat = spyOn(filesystem, "stat").mockImplementation(+ async (path) => {+ throw Object.assign(new Error(`Synthetic ${code}`), {+ code,+ syscall: "stat",+ path: String(path),+ });+ },+ );+ for (const poll of [1, 2]) {+ await expect(tracker.refresh()).rejects.toMatchObject({ code });+ expect(stat).toHaveBeenCalledTimes(poll);+ }+ } finally {+ await tracker.stop();+ }+ },+ );+test.each([- ["EACCES", true],- ["EACCES", false],- ["EPERM", true],- ["EPERM", false],+ ["EACCES", true, "budget"],+ ["EACCES", false, "budget"],+ ["EPERM", true, "budget"],+ ["EPERM", false, "budget"],+ ["EACCES", true, "observe"],+ ["EPERM", false, "observe"],+ ["EACCES", true, "both"],+ ["EPERM", false, "both"],] as const)(- "retains and retries a %s worker when its parent appears later (already identified: %j)",- async (code, identified) => {+ "retains and retries a %s worker when its parent appears later (identified: %j, tracking: %s)",+ async (code, identified, tracking) => {+ const capped = tracking !== "observe";const home = await codexHome();const usage = (input_tokens: number) => ({input_tokens,@@ -3195,27 +3236,37 @@ describe("live scan cost tracking", () => {parent: "middle",});let denied = !identified;+ let newLog: string | undefined;let attempts = 0;const opened = filesystem.open;const opening = spyOn(filesystem, "open").mockImplementation(async (...args: Parameters<typeof filesystem.open>) => {- if (String(args[0]) === worker && denied) {+ const path = String(args[0]);+ if (denied && (path === worker || path === newLog)) {attempts += 1;throw Object.assign(new Error(`Synthetic ${code}`), {code,syscall: "open",- path: worker,+ path,});}return await opened(...args);},);const reported: number[] = [];+ const observed: string[] = [];+ const progress: number[] = [];const tracker = new ScanCostTracker({codexHome: home,model: "gpt-5.6-sol",- maxCostUsd: 1,+ maxCostUsd: capped ? 1 : undefined,onCost: (cost) => reported.push(cost.inputTokens),+ ...(tracking !== "budget" && {+ onProgress: (update) => progress.push(update.filesCompleted),+ onSessionEvent: ({ threadId, event }) => {+ if (event["type"] === "session_meta") observed.push(threadId);+ },+ }),});tracker.start("scan-thread");try {@@ -3226,17 +3277,63 @@ describe("live scan cost tracking", () => {} else {await expect(tracker.refresh()).rejects.toMatchObject({ code });}- await appendFile(- worker,- `${JSON.stringify({ type: "event_msg", payload: { type: "token_count", info: { total_token_usage: usage(1_000_000) } } })}\n`,- );- await writeSession(home, "middle", usage(20), {+ const appendWorkerUsage = () =>+ appendFile(+ worker,+ JSON.stringify(legacyUsageRecord(usage(1_000_000))) + "\n",+ );+ if (capped) await appendWorkerUsage();+ const middle = await writeSession(home, "middle", usage(20), {parent: "scan-thread",});await expect(tracker.refresh()).rejects.toMatchObject({ code });+ expect(observed.filter((id) => id === "middle")).toHaveLength(+ capped ? 0 : 1,+ );+ await appendSessionItem(middle, progressMessage(1));+ if (!capped) await appendWorkerUsage();await expect(tracker.refresh()).rejects.toMatchObject({ code });+ expect(progress).toEqual(capped ? [] : [1]);+ expect(reported).not.toContain(30);+ if (!capped && identified) {+ const directory = parse(worker).dir;+ newLog = join(directory, "z-new-log.jsonl");+ await writeFile(newLog, "{}\n");+ const tail = join(directory, "zz-directory");+ await filesystem.mkdir(tail);+ const entries = await filesystem.readdir(directory, {+ withFileTypes: true,+ });+ entries.sort((left, right) => left.name.localeCompare(right.name));+ const readdir = filesystem.readdir;+ using _knownFirst = spyOn(filesystem, "readdir").mockImplementation(+ async (...args) => {+ if (args[0] === tail)+ throw Object.assign(new Error("Cannot list later directory"), {+ code,+ syscall: "scandir",+ path: tail,+ });+ return args[0] === directory+ ? entries+ : Reflect.apply(readdir, filesystem, args);+ },+ );+ await expect(tracker.refresh()).rejects.toMatchObject({+ code,+ path: newLog,+ });+ }denied = false;const snapshot = await tracker.stop();+ if (tracking !== "budget") {+ expect(observed.toSorted()).toEqual([+ "middle",+ "scan-thread",+ "worker",+ ]);+ expect(progress).toEqual([1]);+ }expect(snapshot.cost?.inputTokens).toBe(1_000_030);expect(snapshot.cost!.estimatedUsd).toBeGreaterThan(1);expect(reported).toContain(1_000_030);