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

test(sdk): reuse API and session fixtures (#1229)

mldangelo-oai0462b85d5db2
diff --git a/sdk/typescript/tests-ts/api-post-scan.test.ts b/sdk/typescript/tests-ts/api-post-scan.test.ts
index e0ba2f9f9..809559ab5 100644
--- a/sdk/typescript/tests-ts/api-post-scan.test.ts
+++ b/sdk/typescript/tests-ts/api-post-scan.test.ts
@@ -86,7 +86,6 @@ async function startPostScan(scenario: PostScanScenario) {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => runtime,
resolvePluginPython: async () => python!,
prepareOutputDir: async () => scanDir,
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index bfa180d1d..717d4735a 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -78,9 +78,11 @@ import {
completedCodex,
failedEvents,
preparedRuntime,
+ scanRuntimeDependencies,
} from "./support/api-events.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { runTestInSubprocess } from "./support/test-subprocess.js";
+import { writeSession as writeUsageSession } from "./support/usage-rollout.js";
import { FindingWorkflow } from "../src/finding-workflow.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "../src/deep-scan-defaults.js";
import { pythonExecutable, nodeCommand, gitText } from "./support/shell.js";
@@ -138,10 +140,7 @@ test.each(["completed", "receipt-lost", "scan-interrupted", "prompt-files"])(
{},
{
environment,
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (options, args, input) => {
if (args[0] === "finding-workflow") {
const payload = JSON.parse(input!);
@@ -366,40 +365,6 @@ function nodeCodex(script: string): {
};
}
-async function writeUsageSession(
- codexHome: string,
- threadId: string,
- usage: Record<string, number>,
- parentThreadId?: string,
-): Promise<string> {
- const directory = join(codexHome, "sessions", "2026", "07", "26");
- await mkdir(directory, { recursive: true });
- const path = join(directory, `rollout-${threadId}.jsonl`);
- await writeFile(
- path,
- [
- JSON.stringify({
- type: "session_meta",
- payload: {
- id: threadId,
- ...(parentThreadId === undefined
- ? {}
- : { parent_thread_id: parentThreadId }),
- },
- }),
- JSON.stringify({
- type: "event_msg",
- payload: {
- type: "token_count",
- info: { total_token_usage: usage },
- },
- }),
- "",
- ].join("\n"),
- );
- return path;
-}
-
async function appendUsage(path: string, inputTokens: number): Promise<void> {
await appendFile(
path,
@@ -823,7 +788,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{ codexOverrides },
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
@@ -942,7 +906,7 @@ describe("CodexSecurity orchestration", () => {
});
const invalidConfig = new TestClient(
{ codexOverrides: { plugins: { unexpected: true } } },
- { environment: {} },
+ {},
);
await expect(invalidConfig.preflight(repository)).rejects.toThrow(
"Codex Security owns plugin loading configuration",
@@ -999,7 +963,6 @@ describe("CodexSecurity orchestration", () => {
},
},
{
- environment: {},
prepareRuntime,
},
);
@@ -1023,7 +986,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -1041,7 +1003,6 @@ describe("CodexSecurity orchestration", () => {
const configured = new TestClient(
{ codexOverrides: { model } },
{
- environment: {},
prepareRuntime,
},
);
@@ -1058,7 +1019,7 @@ describe("CodexSecurity orchestration", () => {
const unpriced = new TestClient(
{ codexOverrides: { model: "unknown-model" } },
- { environment: {} },
+ {},
);
await expect(
unpriced.preflight(repository, { maxCostUsd: 5 }),
@@ -1076,7 +1037,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -1203,7 +1163,7 @@ describe("CodexSecurity orchestration", () => {
{ model_reasoning_effort: false },
];
for (const codexOverrides of invalidSettings) {
- const client = new TestClient({ codexOverrides }, { environment: {} });
+ const client = new TestClient({ codexOverrides }, {});
await expect(client.preflight(repository)).rejects.toThrow(
/model|reasoning effort/u,
);
@@ -1694,10 +1654,7 @@ describe("CodexSecurity orchestration", () => {
{ codexOverrides: { model_provider: "amazon-bedrock" } },
{
environment: { AWS_PROFILE: "synthetic-bedrock" },
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: () => ({
startThread: () => ({
id: null,
@@ -1892,7 +1849,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
@@ -1963,7 +1919,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
@@ -2006,7 +1961,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -2053,7 +2007,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
@@ -2157,7 +2110,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -2202,7 +2154,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -2257,7 +2208,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -2288,7 +2238,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime,
},
);
@@ -2405,7 +2354,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => {
process.chdir(elsewhere);
return preparedRuntime(codexHome);
@@ -2682,11 +2630,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -2898,10 +2842,7 @@ describe("CodexSecurity orchestration", () => {
CODEX_HOME: "~\\ambient-home",
USERPROFILE: root,
},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: codexFactory(deepSettingsCaptured),
},
);
@@ -2952,10 +2893,7 @@ describe("CodexSecurity orchestration", () => {
{},
{
environment: { CODEX_HOME: ambientHome },
- prepareRuntime: async () => preparedRuntime(runtimeHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(runtimeHome, scanDir),
createCodex: codexFactory(deepSettingsCaptured),
},
);
@@ -3035,10 +2973,7 @@ describe("CodexSecurity orchestration", () => {
? relative(process.cwd(), codexHome)
: codexHome,
},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: codexFactory(deepSettingsCaptured),
},
);
@@ -3110,10 +3045,7 @@ describe("CodexSecurity orchestration", () => {
{},
{
environment: { CODEX_HOME: codexHome.toUpperCase() },
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: codexFactory(deepSettingsCaptured),
},
);
@@ -3137,11 +3069,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3170,11 +3098,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3210,11 +3134,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3259,11 +3179,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3301,11 +3217,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3377,11 +3289,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3397,12 +3305,10 @@ describe("CodexSecurity orchestration", () => {
await copyCompletedScan(root);
await Promise.all([
writeUsageSession(codexHome, "thread-1", usage),
- writeUsageSession(
- codexHome,
- "worker-thread",
- usage,
- "thread-1",
- ),
+ writeUsageSession(codexHome, "worker-thread", usage, {
+ parent: "thread-1",
+ parentField: "parent_thread_id",
+ }),
writeUsageSession(codexHome, "unrelated-thread", usage),
]);
const marker = (
@@ -3513,11 +3419,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3614,7 +3516,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => ({
...runtime,
plugin: {
@@ -3667,11 +3568,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3770,11 +3667,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -3944,11 +3837,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -4007,11 +3896,7 @@ describe("CodexSecurity orchestration", () => {
},
},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: recordingWorkbench(commands),
createCodex: completedCodex(root, copyCompletedScan),
},
@@ -4051,11 +3936,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
...(setupFails
? {
prepareScanArtifactRestorer: async () => fail(failureMessage),
@@ -4120,11 +4001,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: () => ({
startThread: () => ({
id: "thread-1",
@@ -4208,10 +4085,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
prepareScanArtifactRestorer: async () => ({
restore: async (name, contents) => {
if (scenario === "restore failure")
@@ -4298,7 +4172,7 @@ describe("CodexSecurity orchestration", () => {
codexHome,
"worker-thread",
{ input_tokens: 100, output_tokens: 0 },
- "scan-thread",
+ { parent: "scan-thread", parentField: "parent_thread_id" },
);
await firstApproval;
await appendUsage(path, 1_700);
@@ -4323,11 +4197,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: recordingWorkbench(commands),
createCodex: () => ({
startThread,
@@ -4397,11 +4267,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (_options, args, input) => {
commands.push(args);
if (scenario === "save-failed" && args[0] === "set-scan-cost-limit")
@@ -4529,7 +4395,7 @@ describe("CodexSecurity orchestration", () => {
cached_input_tokens: 100,
output_tokens: 20,
},
- "scan-thread",
+ { parent: "scan-thread", parentField: "parent_thread_id" },
),
]);
await (options.signal.aborted
@@ -4548,11 +4414,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: recordingWorkbench(commands),
createCodex: codexFactory(runStreamed),
},
@@ -4639,11 +4501,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: async (
_options: unknown,
args: readonly string[],
@@ -4735,11 +4593,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
runWorkbench: recordingWorkbench(commands),
createCodex: (options: CodexOptions) => ({
startThread(threadOptions: Parameters<Codex["startThread"]>[0]) {
@@ -4832,7 +4686,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: runtimePreparer(codexHome, () => ({
environment: {
PATH: [knowledgeBaseBin, trustedBin, dirname(trustedGit)].join(
@@ -4924,11 +4777,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: (options: CodexOptions) => ({
startThread: () => ({
id: null,
@@ -5181,7 +5030,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
createCodex: codexMustNotStart,
},
@@ -5465,7 +5313,6 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: runtimePreparer(codexHome, () => ({
plugin: {
...preparedRuntime(codexHome).plugin,
@@ -5587,7 +5434,6 @@ describe("CodexSecurity orchestration", () => {
},
},
{
- environment: {},
prepareRuntime: async () => {
const runtime = preparedRuntime(codexHome);
return {
@@ -5910,11 +5756,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: (options: CodexOptions) => ({
startThread: () => ({
id: null,
@@ -5965,11 +5807,7 @@ describe("CodexSecurity orchestration", () => {
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: codexFactory(runStreamed),
},
);
@@ -6071,7 +5909,7 @@ describe("CodexSecurity orchestration", () => {
);
git("checkout", "-q", "main");
- const client = new TestClient({}, { environment: {} });
+ const client = new TestClient({}, {});
await expect(
client.run(repository, {
target: DiffTarget.refs({ base: "main", head: "feature" }),
@@ -6643,7 +6481,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => {
if (attempts++ === 0) throw preparationFailure;
started.resolve();
@@ -6672,7 +6509,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => {
started.resolve();
return await prepared.promise;
@@ -6708,7 +6544,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
@@ -6736,11 +6571,7 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const client = new TestClient(
{},
{
- environment: {},
- prepareRuntime: async () => preparedRuntime(codexHome),
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
+ ...scanRuntimeDependencies(codexHome, scanDir),
createCodex: () => ({
startThread: () => ({
id: null,
@@ -6793,7 +6624,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const client = new TestClient(
{},
{
- environment: {},
prepareRuntime: runtimePreparer(codexHome, () => ({
environment: { CODEX_HOME: codexHome },
})),
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index 4b7b77c72..021656058 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -1,6 +1,6 @@
import { jsonLines } from "./support/json.js";
import { spawnSync } from "node:child_process";
-import { appendFile, mkdir, writeFile } from "node:fs/promises";
+import { appendFile, writeFile } from "node:fs/promises";
import { join, parse, sep } from "node:path";
import { Codex } from "@openai/codex-sdk";
import { afterEach, describe, expect, test } from "bun:test";
@@ -21,6 +21,9 @@ import {
lowerUuid7Turn,
ownedPythonUsage,
ownedSdkUsage,
+ parentFields,
+ parentMetadata,
+ writeSession,
ownershipRollout,
readPythonRolloutUsage,
scanThreadId,
@@ -31,19 +34,6 @@ import { createApiTestFixtures } from "./support/temporary-directories.js";
const { temporaryDirectory: codexHome, cleanup } = createApiTestFixtures(
"codex-security-cost-",
);
-const parentFields = ["source", "parent_thread_id", "forked_from_id"] as const;
-type SessionParentField = (typeof parentFields)[number];
-
-function parentMetadata(parentThreadId: string, field: SessionParentField) {
- return field === "source"
- ? {
- source: {
- subagent: { thread_spawn: { parent_thread_id: parentThreadId } },
- },
- }
- : { [field]: parentThreadId };
-}
-
async function waitFor(check: () => boolean): Promise<void> {
for (let attempt = 0; attempt < 100; attempt += 1) {
if (check()) return;
@@ -54,43 +44,14 @@ async function waitFor(check: () => boolean): Promise<void> {
afterEach(cleanup);
-async function writeSession(
- home: string,
- threadId: string,
- usage: Record<string, number>,
- parentThreadId?: string,
- workingDirectory?: string,
- timestamp?: string,
- parentField: SessionParentField = "source",
-): Promise<string> {
- const directory = join(home, "sessions", "2026", "07", "26");
- await mkdir(directory, { recursive: true });
- const path = join(directory, `rollout-${threadId}.jsonl`);
- await writeFile(
- path,
- [
- JSON.stringify({
- type: "session_meta",
- payload: {
- id: threadId,
- ...(workingDirectory === undefined ? {} : { cwd: workingDirectory }),
- ...(timestamp === undefined ? {} : { timestamp }),
- ...(parentThreadId === undefined
- ? {}
- : parentMetadata(parentThreadId, parentField)),
- },
- }),
- JSON.stringify({
- type: "event_msg",
- payload: {
- type: "token_count",
- info: { total_token_usage: usage },
- },
- }),
- "",
- ].join("\n"),
- );
- return path;
+async function workerSessionFixture() {
+ const home = await codexHome();
+ const usage = { input_tokens: 100, output_tokens: 10 };
+ const parent = await writeSession(home, "scan-thread", usage);
+ const worker = await writeSession(home, "worker-thread", usage, {
+ parent: "scan-thread",
+ });
+ return { home, usage, parent, worker };
}
async function appendSessionItem(
@@ -446,7 +407,7 @@ describe("live scan cost tracking", () => {
cache_write_input_tokens: 50,
output_tokens: 20,
},
- "scan-thread",
+ { parent: "scan-thread" },
);
const tracker = new ScanCostTracker({
codexHome: home,
@@ -611,7 +572,7 @@ describe("live scan cost tracking", () => {
output_tokens: 5,
reasoning_output_tokens: 1,
},
- "scan-thread",
+ { parent: "scan-thread" },
);
await writeSession(home, "unrelated-thread", {
input_tokens: 1_000_000,
@@ -680,21 +641,14 @@ describe("live scan cost tracking", () => {
const scanDirectory = join(home, "scan");
const usage = { input_tokens: 10, output_tokens: 1 };
const writeMain = () =>
- writeSession(
- home,
- "scan-thread",
- usage,
- undefined,
- scanDirectory,
- "2026-07-26T12:00:00Z",
- );
+ writeSession(home, "scan-thread", usage, {
+ cwd: scanDirectory,
+ timestamp: "2026-07-26T12:00:00Z",
+ });
if (missing === "parent") await writeMain();
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- missing === "parent" ? "parent-worker" : undefined,
- join(
+ const worker = await writeSession(home, "worker-thread", usage, {
+ parent: missing === "parent" ? "parent-worker" : undefined,
+ cwd: join(
scanDirectory,
"artifacts",
"deep_discovery",
@@ -702,8 +656,8 @@ describe("live scan cost tracking", () => {
"one",
"output",
),
- "2026-07-26T12:01:00Z",
- );
+ timestamp: "2026-07-26T12:01:00Z",
+ });
const message = (text: string) => ({
type: "message",
role: "assistant",
@@ -726,7 +680,9 @@ describe("live scan cost tracking", () => {
);
if (missing === "parent") {
- await writeSession(home, "parent-worker", usage, "scan-thread");
+ await writeSession(home, "parent-worker", usage, {
+ parent: "scan-thread",
+ });
} else {
await writeMain();
}
@@ -770,74 +726,81 @@ describe("live scan cost tracking", () => {
home,
"scan-thread",
{ input_tokens: 1_000, output_tokens: 10 },
- undefined,
- scanDirectory,
- "2026-07-26T12:00:00.900Z",
+ { cwd: scanDirectory, timestamp: "2026-07-26T12:00:00.900Z" },
);
await writeSession(
home,
"deep-worker",
{ input_tokens: 250, output_tokens: 2 },
- undefined,
- process.platform === "win32"
- ? workerDirectory.toUpperCase()
- : workerDirectory,
- "2026-07-26T12:00:00.900Z",
+ {
+ cwd:
+ process.platform === "win32"
+ ? workerDirectory.toUpperCase()
+ : workerDirectory,
+ timestamp: "2026-07-26T12:00:00.900Z",
+ },
);
await writeSession(
home,
"deep-reducer",
{ input_tokens: 125, output_tokens: 1 },
- undefined,
- (process.platform === "win32" ? artifacts.toUpperCase() : artifacts) +
- sep,
- "2026-07-26T12:02:00Z",
+ {
+ cwd:
+ (process.platform === "win32"
+ ? artifacts.toUpperCase()
+ : artifacts) + sep,
+ timestamp: "2026-07-26T12:02:00Z",
+ },
);
await writeSession(
home,
"deep-worker-child",
{ input_tokens: 50, output_tokens: 1 },
- "deep-worker",
- undefined,
- undefined,
- parentField,
+ { parent: "deep-worker", parentField },
);
await writeSession(
home,
"unrelated-thread",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- undefined,
- `${scanDirectory}-other`,
+ { cwd: `${scanDirectory}-other` },
);
await writeSession(
home,
"previous-scan",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- undefined,
- join(scanDirectory, "artifacts", "deep_discovery", "previous-worker"),
- "2026-07-26T11:59:00Z",
+ {
+ cwd: join(
+ scanDirectory,
+ "artifacts",
+ "deep_discovery",
+ "previous-worker",
+ ),
+ timestamp: "2026-07-26T11:59:00Z",
+ },
);
await writeSession(
home,
"unknown-start",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- undefined,
- join(
- scanDirectory,
- "artifacts",
- "deep_discovery",
- "workers",
- "stale",
- "output",
- ),
+ {
+ cwd: join(
+ scanDirectory,
+ "artifacts",
+ "deep_discovery",
+ "workers",
+ "stale",
+ "output",
+ ),
+ },
);
await writeSession(
home,
"nested-scan",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- undefined,
- join(scanDirectory, "nested", "artifacts"),
- "2026-07-26T12:03:00Z",
+ {
+ cwd: join(scanDirectory, "nested", "artifacts"),
+ timestamp: "2026-07-26T12:03:00Z",
+ },
);
const events: ScanSessionEvent[] = [];
const tracker = new ScanCostTracker({
@@ -941,39 +904,40 @@ describe("live scan cost tracking", () => {
home,
"scan-thread",
{ input_tokens: 1_000, output_tokens: 10 },
- undefined,
- scanDirectory,
- "2026-07-26T12:00:00.900Z",
+ { cwd: scanDirectory, timestamp: "2026-07-26T12:00:00.900Z" },
);
await writeSession(
home,
"deep-worker",
{ input_tokens: 250, output_tokens: 2 },
- undefined,
- join(
- scanDirectory,
- "artifacts",
- "deep_discovery",
- "workers",
- "worker",
- "output",
- ),
- "2026-07-26T12:00:00.950Z",
+ {
+ cwd: join(
+ scanDirectory,
+ "artifacts",
+ "deep_discovery",
+ "workers",
+ "worker",
+ "output",
+ ),
+ timestamp: "2026-07-26T12:00:00.950Z",
+ },
);
await writeSession(
home,
"bystander",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- parentThreadId,
- workingDirectory(scanDirectory),
- timestamp,
- parentField,
+ {
+ parent: parentThreadId,
+ cwd: workingDirectory(scanDirectory),
+ timestamp,
+ parentField,
+ },
);
await writeSession(
home,
"bystander-child",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- "bystander",
+ { parent: "bystander" },
);
const events: ScanSessionEvent[] = [];
const tracker = new ScanCostTracker({
@@ -1016,23 +980,22 @@ describe("live scan cost tracking", () => {
home,
"scan-thread",
{ input_tokens: 1_000, output_tokens: 10 },
- undefined,
- scanDirectory,
- timestamp,
+ { cwd: scanDirectory, timestamp },
);
await writeSession(
home,
"independent-worker",
{ input_tokens: 1_000_000, output_tokens: 1_000_000 },
- undefined,
- join(scanDirectory, "artifacts"),
- "2026-07-26T12:01:00Z",
+ {
+ cwd: join(scanDirectory, "artifacts"),
+ timestamp: "2026-07-26T12:01:00Z",
+ },
);
await writeSession(
home,
"child-worker",
{ input_tokens: 250, output_tokens: 2 },
- "scan-thread",
+ { parent: "scan-thread" },
);
const tracker = new ScanCostTracker({
codexHome: home,
@@ -1235,7 +1198,7 @@ describe("live scan cost tracking", () => {
home,
childUuid7Thread,
{ input_tokens: 1_100, output_tokens: 110 },
- scanThreadId,
+ { parent: scanThreadId },
);
const rollout = ownershipRollout(replayedTurnIds);
await writeFile(rolloutPath, jsonLines(rollout) + "\n");
@@ -1280,15 +1243,12 @@ describe("live scan cost tracking", () => {
});
test("forwards actions from this scan's delegated workers only", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- const parentPath = await writeSession(home, "scan-thread", usage);
- const workerPath = await writeSession(
+ const {
home,
- "worker-thread",
usage,
- "scan-thread",
- );
+ parent: parentPath,
+ worker: workerPath,
+ } = await workerSessionFixture();
const unrelatedPath = await writeSession(home, "unrelated-thread", usage);
const command =
'rg -n "password" "$CODEX_SECURITY_REPOSITORY/routes/login.ts"';
@@ -1351,15 +1311,7 @@ describe("live scan cost tracking", () => {
});
test("forwards genuine worker reasoning and transcript text", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const path = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker: path } = await workerSessionFixture();
await appendSessionItem(path, {
id: "thinking-1",
type: "reasoning",
@@ -1404,15 +1356,7 @@ describe("live scan cost tracking", () => {
});
test("streams worker reasoning and commentary from live session events once", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
await appendFile(
worker,
[
@@ -1491,15 +1435,7 @@ describe("live scan cost tracking", () => {
});
test("expands streamed worker reasoning without duplicating summaries or exposing encrypted content", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
const details = `${"The query reaches a privileged tenant boundary. ".repeat(30)}Final authorization check.`;
const raw = `**The route builds SQL from request parameters.** ${details}`;
await appendFile(
@@ -1611,15 +1547,7 @@ describe("live scan cost tracking", () => {
});
test("keeps distinct streamed worker reasoning summaries separate", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
const summaries = [
"**Planning discovery worker tasks**",
"**Preparing thorough file batch reading**",
@@ -1672,15 +1600,7 @@ describe("live scan cost tracking", () => {
});
test("splits worker reasoning summaries without streamed events", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
await appendSessionItem(worker, {
id: "reasoning-1",
type: "reasoning",
@@ -1726,21 +1646,10 @@ describe("live scan cost tracking", () => {
});
test("forwards reviewed-file progress from descendant workers only", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- const parent = await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
- const descendant = await writeSession(
- home,
- "nested-worker-thread",
- usage,
- "worker-thread",
- );
+ const { home, usage, parent, worker } = await workerSessionFixture();
+ const descendant = await writeSession(home, "nested-worker-thread", usage, {
+ parent: "worker-thread",
+ });
const unrelated = await writeSession(home, "unrelated-thread", usage);
await appendSessionItem(parent, progressMessage(1));
@@ -1767,21 +1676,10 @@ describe("live scan cost tracking", () => {
});
test("aggregates worker progress without regressing or changing assigned shards", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- const parent = await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
- const otherWorker = await writeSession(
- home,
- "other-worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, usage, parent, worker } = await workerSessionFixture();
+ const otherWorker = await writeSession(home, "other-worker-thread", usage, {
+ parent: "scan-thread",
+ });
const unrelated = await writeSession(home, "unrelated-thread", usage);
const updates: ScanProgress[] = [];
const tracker = new ScanCostTracker({
@@ -1842,8 +1740,12 @@ describe("live scan cost tracking", () => {
const home = await codexHome();
const usage = { input_tokens: 100, output_tokens: 10 };
await writeSession(home, "scan-thread", usage);
- const first = await writeSession(home, "worker-a", usage, "scan-thread");
- const second = await writeSession(home, "worker-b", usage, "scan-thread");
+ const first = await writeSession(home, "worker-a", usage, {
+ parent: "scan-thread",
+ });
+ const second = await writeSession(home, "worker-b", usage, {
+ parent: "scan-thread",
+ });
const unrelated = await writeSession(home, "unrelated-worker", usage);
const updates: ScanProgress[] = [];
const tracker = new ScanCostTracker({
@@ -1875,15 +1777,7 @@ describe("live scan cost tracking", () => {
});
test("counts only explicit successful worker review receipts", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
const marker = (filesCompleted: number) =>
`CODEX_SECURITY_SCAN_PROGRESS ${JSON.stringify({
phase: "discovery",
@@ -1967,15 +1861,7 @@ describe("live scan cost tracking", () => {
});
test("polls worker file progress without another observer", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
await appendSessionItem(worker, progressMessage(3));
const { promise: reportedProgress, resolve: reportProgress } =
@@ -2000,15 +1886,7 @@ describe("live scan cost tracking", () => {
});
test("reports newly completed worker batches once per progress update", async () => {
- const home = await codexHome();
- const usage = { input_tokens: 100, output_tokens: 10 };
- await writeSession(home, "scan-thread", usage);
- const worker = await writeSession(
- home,
- "worker-thread",
- usage,
- "scan-thread",
- );
+ const { home, worker } = await workerSessionFixture();
const updates: ScanProgress[] = [];
const tracker = new ScanCostTracker({
codexHome: home,
@@ -2166,7 +2044,7 @@ describe("live scan cost tracking", () => {
home,
"worker-thread",
{ input_tokens: 100, output_tokens: 0 },
- "scan-thread",
+ { parent: "scan-thread" },
);
const tracker = new ScanCostTracker({
codexHome: home,
diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.ts
index dd8ff176c..ab8478184 100644
--- a/sdk/typescript/tests-ts/import-scan.test.ts
+++ b/sdk/typescript/tests-ts/import-scan.test.ts
@@ -120,7 +120,11 @@ async function fixture(format: "csv" | "json" = "csv") {
return {
root,
stateDirectory,
- environment,
+ dependencies: {
+ environment,
+ // Import persistence uses real Python; runtime tests cover interpreter discovery.
+ resolvePluginPython: async () => python!,
+ },
python: python!,
source,
options,
@@ -166,7 +170,7 @@ test.each(["csv", "json"] as const)(
async (format) => {
const context = await fixture(format);
const result = completed(
- await importScan(context.options, { environment: context.environment }),
+ await importScan(context.options, context.dependencies),
);
expect(result.findings.findings).toHaveLength(2);
expect(
@@ -277,7 +281,7 @@ test.each(["csv", "json"] as const)(
await expect(
importScan(
{ ...context.options, sourcePath: linked, dryRun },
- { environment: context.environment },
+ context.dependencies,
),
).rejects.toThrow("Import source must be a regular file");
await expect(stat(context.stateDirectory)).rejects.toMatchObject({
@@ -307,7 +311,7 @@ test.each(["csv", "json"] as const)(
sourcePath: join(linked, `findings.${format}`),
dryRun,
},
- { environment: context.environment },
+ context.dependencies,
),
).rejects.toThrow("Import source must not traverse directory links");
await expect(stat(context.stateDirectory)).rejects.toMatchObject({
@@ -325,7 +329,7 @@ test.each(process.platform === "win32" ? ["directory"] : ["directory", "FIFO"])(
if (kind === "directory") await mkdir(context.options.sourcePath);
else execFileSync("mkfifo", [context.options.sourcePath]);
await expect(
- importScan(context.options, { environment: context.environment }),
+ importScan(context.options, context.dependencies),
).rejects.toThrow("Import source must be a regular file");
await expect(stat(context.stateDirectory)).rejects.toMatchObject({
code: "ENOENT",
@@ -370,7 +374,7 @@ test.each(["regular file", "symbolic link"])(
);
try {
await expect(
- importScan(context.options, { environment: context.environment }),
+ importScan(context.options, context.dependencies),
).rejects.toThrow();
expect(replaced).toBe(true);
expect(observeRead).not.toHaveBeenCalled();
@@ -387,7 +391,7 @@ test.each(["regular file", "symbolic link"])(
test("reimporting the retained source preserves finding identities with new scan occurrences", async () => {
const context = await fixture();
const first = completed(
- await importScan(context.options, { environment: context.environment }),
+ await importScan(context.options, context.dependencies),
);
const recipe = await runWorkbench(context.workbenchOptions, [
"get-scan-recipe",
@@ -404,7 +408,7 @@ test("reimporting the retained source preserves finding identities with new scan
sourcePath: imported.sourcePath,
parentScanId: first.manifest.scan.id,
},
- { environment: context.environment },
+ context.dependencies,
),
);
expect(second.manifest.scan.id).not.toBe(first.manifest.scan.id);
@@ -433,7 +437,7 @@ test("dry run validates source rows without creating database state or invoking
const result = await importScan(
{ ...context.options, dryRun: true },
{
- environment: context.environment,
+ ...context.dependencies,
runWorkbench: unexpected,
resolvePluginPython: unexpected,
},
@@ -452,7 +456,7 @@ test("dry run validates source rows without creating database state or invoking
importScan(
{ ...context.options, dryRun: true },
{
- environment: context.environment,
+ ...context.dependencies,
runWorkbench: unexpected,
},
),
@@ -468,20 +472,14 @@ test("imports archive prior output without replacing its saved findings", async
...context.options,
outputDir: join(context.root, "results"),
};
- const first = completed(
- await importScan(options, { environment: context.environment }),
- );
+ const first = completed(await importScan(options, context.dependencies));
const originalManifest = await readFile(first.manifestPath, "utf8");
- await expect(
- importScan(options, { environment: context.environment }),
- ).rejects.toThrow();
+ await expect(importScan(options, context.dependencies)).rejects.toThrow();
expect(await readFile(first.manifestPath, "utf8")).toBe(originalManifest);
const second = completed(
await importScan(
{ ...options, archiveExisting: true },
- {
- environment: context.environment,
- },
+ context.dependencies,
),
);
const scans = await storedScans(context);
@@ -515,7 +513,7 @@ test.each(["failure", "abort"] as const)(
const operation = importScan(
{ ...context.options, signal: controller.signal },
{
- environment: context.environment,
+ ...context.dependencies,
runWorkbench: run,
},
);
diff --git a/sdk/typescript/tests-ts/scan-dashboard.test.ts b/sdk/typescript/tests-ts/scan-dashboard.test.ts
index ec6401ae9..eb6dccf67 100644
--- a/sdk/typescript/tests-ts/scan-dashboard.test.ts
+++ b/sdk/typescript/tests-ts/scan-dashboard.test.ts
@@ -17,6 +17,17 @@ function fakeClock(now: () => number = () => STARTED_AT) {
};
}
+function createDashboard(
+ stream: ConstructorParameters<typeof ScanDashboard>[0],
+ options: Partial<ConstructorParameters<typeof ScanDashboard>[1]> = {},
+) {
+ return new ScanDashboard(stream, {
+ repository: "/code/juice-shop",
+ clock: fakeClock(),
+ ...options,
+ });
+}
+
function lastFrame(stderr: ReturnType<typeof capture>): string {
return stripVTControlCharacters(stderr.text())
.split("CODEX SECURITY · juice-shop")
@@ -44,13 +55,9 @@ class DashboardTestInput extends EventEmitter {
describe("live scan dashboard", () => {
test("keeps cost bounds and assumptions readable on a narrow terminal", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 60, rows: 24 },
- {
- repository: "/synthetic/repository",
- showCost: true,
- clock: fakeClock(),
- },
+ { repository: "/synthetic/repository", showCost: true },
);
dashboard.start();
dashboard.setCost({
@@ -77,10 +84,9 @@ describe("live scan dashboard", () => {
test("edits a higher total budget while continuing to show live cost", async () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(stderr.stream, {
+ const dashboard = createDashboard(stderr.stream, {
repository: "/synthetic/repository",
maxCostUsd: 20,
- clock: fakeClock(),
input,
});
const controller = new AbortController();
@@ -131,10 +137,9 @@ describe("live scan dashboard", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
let interrupted = false;
- const dashboard = new ScanDashboard(stderr.stream, {
+ const dashboard = createDashboard(stderr.stream, {
repository: "/synthetic/repository",
maxCostUsd: 20,
- clock: fakeClock(),
input,
onInterrupt: () => {
interrupted = true;
@@ -317,13 +322,12 @@ describe("live scan dashboard", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
const onInterrupt = mock();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 80, rows: 14 },
{
repository: "/synthetic/project",
presentation: "components",
input,
- clock: fakeClock(),
onInterrupt,
},
);
@@ -366,14 +370,13 @@ describe("live scan dashboard", () => {
test("renders publication progress without scan-only inventory and cost fields", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 100, rows: 18 },
{
repository: "/synthetic/payments-api",
presentation: "publication",
input,
color: false,
- clock: fakeClock(),
},
);
@@ -443,10 +446,9 @@ describe("live scan dashboard", () => {
: "Raw mode cleanup failed.",
);
};
- const dashboard = new ScanDashboard(stderr.stream, {
+ const dashboard = createDashboard(stderr.stream, {
repository: "/synthetic/repository",
input,
- clock: fakeClock(),
});
expect(() => dashboard.start()).toThrow("Raw mode initialization failed.");
@@ -462,11 +464,11 @@ describe("live scan dashboard", () => {
input.isRaw = enabled;
return input;
};
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{
write: failingDashboardOutput(output),
},
- { repository: "/synthetic/repository", input, clock: fakeClock() },
+ { repository: "/synthetic/repository", input },
);
expect(() => dashboard.start()).toThrow("Dashboard rendering failed.");
@@ -621,13 +623,9 @@ describe("live scan dashboard", () => {
test("hides stage and file counts during Deep scans without wasting screen rows", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 80, rows: 12 },
- {
- repository: "/code/juice-shop",
- mode: "deep",
- clock: fakeClock(),
- },
+ { mode: "deep" },
);
dashboard.start();
@@ -707,10 +705,7 @@ describe("live scan dashboard", () => {
test("shows repeated file inventory commands as one activity", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream);
dashboard.start();
for (const [id, description] of [
@@ -735,10 +730,7 @@ describe("live scan dashboard", () => {
test("labels actual delegated worker activity", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream);
dashboard.start();
dashboard.record({
@@ -758,10 +750,7 @@ describe("live scan dashboard", () => {
test("keeps parent and worker file inventories distinct", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream);
dashboard.start();
for (const activity of [
@@ -787,14 +776,9 @@ describe("live scan dashboard", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
const onInterrupt = mock();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 80, rows: 14 },
- {
- repository: "/code/juice-shop",
- input,
- onInterrupt,
- clock: fakeClock(),
- },
+ { input, onInterrupt },
);
dashboard.start();
@@ -853,9 +837,9 @@ describe("live scan dashboard", () => {
test("batches trackpad scroll events without dropping movement", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 80, rows: 14 },
- { repository: "/code/juice-shop", input, clock: fakeClock() },
+ { input },
);
dashboard.start();
@@ -890,9 +874,9 @@ describe("live scan dashboard", () => {
test("scrolls half a page with Mac-friendly Ctrl+U and Ctrl+D", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 80, rows: 14 },
- { repository: "/code/juice-shop", input, clock: fakeClock() },
+ { input },
);
dashboard.start();
@@ -930,14 +914,9 @@ describe("live scan dashboard", () => {
test("preserves chronological session events and strips terminal controls from activity", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 140, rows: 24 },
- {
- repository: "/code/juice-shop",
- input,
- color: true,
- clock: fakeClock(),
- },
+ { input, color: true },
);
dashboard.start();
@@ -1065,9 +1044,9 @@ describe("live scan dashboard", () => {
test("filters workers and scrolls the details separately from scan activity", () => {
const stderr = capture(true);
const input = new DashboardTestInput();
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 120, rows: 14 },
- { repository: "/code/juice-shop", input, clock: fakeClock() },
+ { input },
);
let payloadReads = 0;
const event = (threadId: string, text: string, worker?: number) => {
@@ -1148,13 +1127,11 @@ describe("live scan dashboard", () => {
test("wraps real reasoning and assistant prose into chronological history", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
- { ...stderr.stream, columns: 55, rows: 18 },
- {
- repository: "/code/juice-shop",
- clock: fakeClock(),
- },
- );
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 55,
+ rows: 18,
+ });
dashboard.start();
dashboard.record({
@@ -1191,13 +1168,9 @@ describe("live scan dashboard", () => {
test("shows streamed worker reasoning fully expanded and updates it in place", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 72, rows: 45 },
- {
- repository: "/code/juice-shop",
- color: true,
- clock: fakeClock(),
- },
+ { color: true },
);
const details = `${"The login route crosses an organization authorization boundary. ".repeat(18)}Final tenant-isolation check.`;
@@ -1232,10 +1205,11 @@ describe("live scan dashboard", () => {
const stderr = capture(true);
const target =
"/private/tmp/codex security/scans/promptfoo-cloud/artifacts/02_discovery/raw_candidates_02.jsonl";
- const dashboard = new ScanDashboard(
- { ...stderr.stream, columns: 55, rows: 18 },
- { repository: "/code/juice-shop", clock: fakeClock() },
- );
+ const dashboard = createDashboard({
+ ...stderr.stream,
+ columns: 55,
+ rows: 18,
+ });
dashboard.start();
dashboard.record({
@@ -1259,9 +1233,9 @@ describe("live scan dashboard", () => {
test("renders fenced Markdown code with exact indentation and one timestamp", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 90, rows: 18 },
- { repository: "/code/juice-shop", color: true, clock: fakeClock() },
+ { color: true },
);
dashboard.start();
@@ -1298,9 +1272,9 @@ describe("live scan dashboard", () => {
test("renders inline code while preserving clickable links and exact commands", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 120, rows: 18 },
- { repository: "/code/juice-shop", color: true, clock: fakeClock() },
+ { color: true },
);
dashboard.start();
@@ -1339,13 +1313,9 @@ describe("live scan dashboard", () => {
test("preserves external Markdown link targets and rejects unsafe links", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 120, rows: 18 },
- {
- repository: "/code/juice-shop",
- color: false,
- clock: fakeClock(),
- },
+ { color: false },
);
dashboard.start();
@@ -1381,13 +1351,9 @@ describe("live scan dashboard", () => {
test("preserves credential-shaped activity descriptions across line wrapping", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(
+ const dashboard = createDashboard(
{ ...stderr.stream, columns: 50, rows: 18 },
- {
- repository: "/code/juice-shop",
- color: false,
- clock: fakeClock(),
- },
+ { color: false },
);
dashboard.start();
@@ -1407,11 +1373,7 @@ describe("live scan dashboard", () => {
test("colors important activity while keeping prose readable across terminal themes", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- color: true,
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream, { color: true });
dashboard.start();
dashboard.record({
@@ -1465,11 +1427,7 @@ describe("live scan dashboard", () => {
test("colors successful scan milestones and warnings distinctly", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- color: true,
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream, { color: true });
dashboard.start();
for (const status of [
@@ -1493,11 +1451,7 @@ describe("live scan dashboard", () => {
test("leaves activity uncolored when colors are disabled", () => {
const stderr = capture(true);
- const dashboard = new ScanDashboard(stderr.stream, {
- repository: "/code/juice-shop",
- color: false,
- clock: fakeClock(),
- });
+ const dashboard = createDashboard(stderr.stream, { color: false });
dashboard.start();
dashboard.record({
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index 24aeaa774..5fa3df61d 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -30,6 +30,15 @@ export function preparedRuntime(
};
}
+export function scanRuntimeDependencies(codexHome: string, scanDir: string) {
+ return {
+ prepareRuntime: async () => preparedRuntime(codexHome),
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ };
+}
+
export type ScanObserverName = Parameters<
NonNullable<ScanOptions["onObserverError"]>
>[0];
diff --git a/sdk/typescript/tests-ts/support/usage-rollout.ts b/sdk/typescript/tests-ts/support/usage-rollout.ts
index 6b53157de..4e5342447 100644
--- a/sdk/typescript/tests-ts/support/usage-rollout.ts
+++ b/sdk/typescript/tests-ts/support/usage-rollout.ts
@@ -1,3 +1,4 @@
+import { mkdir, writeFile } from "node:fs/promises";
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { expect } from "bun:test";
@@ -125,3 +126,67 @@ export function readPythonRolloutUsage(
expect(result.status, result.stderr).toBe(0);
return JSON.parse(result.stdout) as unknown;
}
+
+export const parentFields = [
+ "source",
+ "parent_thread_id",
+ "forked_from_id",
+] as const;
+type SessionParentField = (typeof parentFields)[number];
+
+export function parentMetadata(
+ parentThreadId: string,
+ field: SessionParentField,
+) {
+ return field === "source"
+ ? {
+ source: {
+ subagent: { thread_spawn: { parent_thread_id: parentThreadId } },
+ },
+ }
+ : { [field]: parentThreadId };
+}
+
+export async function writeSession(
+ home: string,
+ threadId: string,
+ usage: Record<string, number>,
+ {
+ parent,
+ cwd,
+ timestamp,
+ parentField = "source",
+ }: {
+ parent?: string;
+ cwd?: string;
+ timestamp?: string;
+ parentField?: SessionParentField;
+ } = {},
+): Promise<string> {
+ const directory = join(home, "sessions", "2026", "07", "26");
+ await mkdir(directory, { recursive: true });
+ const path = join(directory, `rollout-${threadId}.jsonl`);
+ await writeFile(
+ path,
+ [
+ JSON.stringify({
+ type: "session_meta",
+ payload: {
+ id: threadId,
+ ...(cwd === undefined ? {} : { cwd }),
+ ...(timestamp === undefined ? {} : { timestamp }),
+ ...(parent === undefined ? {} : parentMetadata(parent, parentField)),
+ },
+ }),
+ JSON.stringify({
+ type: "event_msg",
+ payload: {
+ type: "token_count",
+ info: { total_token_usage: usage },
+ },
+ }),
+ "",
+ ].join("\n"),
+ );
+ return path;
+}