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

fix(logs): speed up compressed scan session discovery (#2489)

Michael D'Angeloa7ef304523f2
diff --git a/sdk/typescript/src/scan-logs.ts b/sdk/typescript/src/scan-logs.ts
index 9f08cc55f..c422392f2 100644
--- a/sdk/typescript/src/scan-logs.ts
+++ b/sdk/typescript/src/scan-logs.ts
@@ -1,6 +1,7 @@
import { basename, dirname, join, relative } from "node:path";
import { isDeepStrictEqual } from "node:util";
-import { sessionLines } from "./session-lines.js";
+import { firstNativeSessionLine, sessionLines } from "./session-lines.js";
+import { parseJson } from "./value.js";
import { isRecord } from "./record.js";
import { sessionFiles } from "./cost.js";
import { CodexSecurityError } from "./errors.js";
@@ -70,23 +71,20 @@ type SessionLog = NonNullable<Awaited<ReturnType<typeof findScanSession>>>;
async function* scanSessions(codexHome: string, directory = "sessions") {
for await (const path of sessionFiles(join(codexHome, directory), true)) {
- for await (const first of sessionEvents(path)) {
- if (first["type"] !== "session_meta" || !isRecord(first["payload"])) {
- break;
- }
- const metadata = first["payload"];
- const threadId = metadata["id"];
- if (typeof threadId !== "string") break;
- yield {
- threadId,
- parentThreadId: sessionParentThreadId(metadata),
- startedAt: sessionStartedAt(metadata["timestamp"]),
- workingDirectory:
- typeof metadata["cwd"] === "string" ? metadata["cwd"] : null,
- path,
- };
- break;
- }
+ const first = await firstSessionEvent(path);
+ if (first?.["type"] !== "session_meta" || !isRecord(first["payload"]))
+ continue;
+ const metadata = first["payload"];
+ const threadId = metadata["id"];
+ if (typeof threadId !== "string") continue;
+ yield {
+ threadId,
+ parentThreadId: sessionParentThreadId(metadata),
+ startedAt: sessionStartedAt(metadata["timestamp"]),
+ workingDirectory:
+ typeof metadata["cwd"] === "string" ? metadata["cwd"] : null,
+ path,
+ };
}
}
@@ -269,6 +267,16 @@ async function extendsSessionLog(
}
}
+async function firstSessionEvent(path: string) {
+ const line = await firstNativeSessionLine(path);
+ if (line !== undefined) {
+ const event = parseJson(() => line);
+ if (isRecord(event)) return event;
+ }
+ // Also handles metadata split across frames and malformed prefix lines.
+ for await (const event of sessionEvents(path)) return event;
+}
+
async function* sessionEvents(
path: string,
): AsyncGenerator<Record<string, unknown>> {
diff --git a/sdk/typescript/src/session-lines.ts b/sdk/typescript/src/session-lines.ts
index 6d6f66a6c..6f39a4b2b 100644
--- a/sdk/typescript/src/session-lines.ts
+++ b/sdk/typescript/src/session-lines.ts
@@ -1,4 +1,5 @@
import { createReadStream } from "node:fs";
+import zlib from "node:zlib";
import { createInterface } from "node:readline";
import { pipeline, type Transform } from "node:stream";
import { join } from "node:path";
@@ -22,6 +23,32 @@ export async function* sessionLines(path: string): AsyncGenerator<string> {
}
}
+/** Native zstd can stop at the first frame. Only a newline completes a header. */
+export async function firstNativeSessionLine(
+ path: string,
+): Promise<string | undefined> {
+ if (!path.endsWith(".zst") || typeof zlib.createZstdDecompress !== "function")
+ return;
+ const stream = pipeline(
+ createReadStream(path),
+ zlib.createZstdDecompress(),
+ () => {},
+ );
+ try {
+ stream.setEncoding("utf8");
+ let line = "";
+ for await (const chunk of stream as AsyncIterable<string>) {
+ const newline = chunk.search(/[\r\n]/u);
+ if (newline >= 0) return line + chunk.slice(0, newline);
+ line += chunk;
+ }
+ } catch {
+ // The complete portable reader handles unsupported or damaged frames.
+ } finally {
+ stream.destroy();
+ }
+}
+
async function sessionDecompressor(): Promise<Transform> {
// Decode every concatenated frame and reject truncated tails. Native Node
// Zstandard streams can finish successfully after only the first frame.
diff --git a/sdk/typescript/tests-ts/scan-logs.test.ts b/sdk/typescript/tests-ts/scan-logs.test.ts
index c55e034e8..491445dcc 100644
--- a/sdk/typescript/tests-ts/scan-logs.test.ts
+++ b/sdk/typescript/tests-ts/scan-logs.test.ts
@@ -451,7 +451,7 @@ describe("saved scan logs", () => {
expect(JSON.stringify(result)).not.toContain("private unrelated scan");
});
- test("reads sessions that Codex compressed", async () => {
+ test("reads compressed sessions with metadata split across frames", async () => {
const home = await temporaryHome();
await writeSession(
home,
@@ -470,7 +470,14 @@ describe("saved scan logs", () => {
const directory = join(home, "sessions", "2026", "08", "11");
for (const threadId of ["parent", "worker"]) {
const path = join(directory, `rollout-${threadId}.jsonl`);
- await writeFile(`${path}.zst`, zstdCompressSync(await readFile(path)));
+ const contents = await readFile(path);
+ await writeFile(
+ `${path}.zst`,
+ Buffer.concat([
+ zstdCompressSync(contents.subarray(0, 1)),
+ zstdCompressSync(contents.subarray(1)),
+ ]),
+ );
unlinkSync(path);
}
@@ -497,6 +504,43 @@ describe("saved scan logs", () => {
]);
});
+ test("treats a lone carriage return as a compressed metadata line boundary", async () => {
+ const home = await temporaryHome();
+ const directory = join(home, "sessions");
+ await mkdir(directory);
+ const contents =
+ '{"type":"session_meta",\r"payload":{"id":"wrong"}}\n' +
+ '{"type":"session_meta","payload":{"id":"actual"}}\n';
+ await writeFile(
+ join(directory, "rollout.jsonl.zst"),
+ zstdCompressSync(Buffer.from(contents)),
+ );
+
+ expect(await findScanSession(home, "wrong")).toBeNull();
+ expect(await findScanSession(home, "actual")).toMatchObject({
+ threadId: "actual",
+ });
+ });
+
+ test("rejects a truncated final frame in a selected rollout", async () => {
+ const home = await temporaryHome();
+ const directory = join(home, "sessions");
+ await mkdir(directory);
+ const header = '{"type":"session_meta","payload":{"id":"parent"}}\n';
+ const event = JSON.stringify(commandEvent("parent work", "parent-call"));
+ const tail = zstdCompressSync(Buffer.from(event + "\n"));
+ await writeFile(
+ join(directory, "rollout-parent.jsonl.zst"),
+ Buffer.concat([
+ zstdCompressSync(Buffer.from(header)),
+ tail.subarray(0, tail.length - 1),
+ ]),
+ );
+ await expect(
+ readScanLogs({ scanId: "scan-1", threadId: "parent", codexHome: home }),
+ ).rejects.toThrow();
+ });
+
test("excludes inherited parent history from worker logs", async () => {
const home = await temporaryHome();
await writeSession(home, "parent", []);