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: restore Codex Security attribution and completed scans (#371)

mldangelo-oai64a413831d6a
diff --git a/sdk/typescript/_bundled_plugin/.codex-plugin/plugin.json b/sdk/typescript/_bundled_plugin/.codex-plugin/plugin.json
index e75b1ff63..04c30b9c6 100644
--- a/sdk/typescript/_bundled_plugin/.codex-plugin/plugin.json
+++ b/sdk/typescript/_bundled_plugin/.codex-plugin/plugin.json
@@ -1,6 +1,6 @@
{
"name": "codex-security",
- "version": "0.1.19",
+ "version": "0.1.20",
"description": "Codex Security workflows for security scans, analysis, and investigation.",
"author": {
"name": "OpenAI"
diff --git a/sdk/typescript/_bundled_plugin/.mcp.json b/sdk/typescript/_bundled_plugin/.mcp.json
index 8545d3821..ef0dc3a21 100644
--- a/sdk/typescript/_bundled_plugin/.mcp.json
+++ b/sdk/typescript/_bundled_plugin/.mcp.json
@@ -31,6 +31,7 @@
"CODEX_SECURITY_KNOWLEDGE_BASE",
"CODEX_SECURITY_SCAN_ROOT",
"CODEX_SECURITY_STATE_DIR",
+ "CODEX_SECURITY_SURFACE",
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
diff --git a/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-000 b/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-000
index ac3c8bf46..e68c8dd68 100644
Binary files a/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-000 and b/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-000 differ
diff --git a/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-001 b/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-001
index 43ce65f86..5ddf9950d 100644
Binary files a/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-001 and b/sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-001 differ
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_usage.py b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_usage.py
index 159c405c2..be5bc9282 100644
--- a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_usage.py
+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_usage.py
@@ -189,10 +189,18 @@ def _codex_state_database() -> Path | None:
return path.resolve() if path.is_file() and os.access(path, os.R_OK) else None
configured_home = os.environ.get("CODEX_HOME", "").strip()
- codex_home = Path(configured_home).expanduser() if configured_home else Path.home() / ".codex"
+ codex_home = (
+ Path(configured_home).expanduser()
+ if configured_home
+ else Path.home() / ".codex"
+ )
configured_sqlite_home = os.environ.get("CODEX_SQLITE_HOME", "").strip()
search_roots = [
- *([Path(configured_sqlite_home).expanduser()] if configured_sqlite_home else []),
+ *(
+ [Path(configured_sqlite_home).expanduser()]
+ if configured_sqlite_home
+ else []
+ ),
codex_home,
codex_home / "sqlite",
]
@@ -329,7 +337,9 @@ def _require_state_columns(
}
columns = {str(row["name"]) for row in connection.execute(statements[table])}
if not required.issubset(columns):
- raise ValueError("Codex state graph does not expose the required thread columns.")
+ raise ValueError(
+ "Codex state graph does not expose the required thread columns."
+ )
def _rollout_path(value: object) -> Path | None:
@@ -375,7 +385,9 @@ def _read_rollout_usage(
event = json.loads(raw_line)
except (UnicodeError, ValueError):
if line_number == 1:
- raise ValueError("The rollout session metadata is unreadable.") from None
+ raise ValueError(
+ "The rollout session metadata is unreadable."
+ ) from None
if boundary_reached:
warnings.add("rollout_record_invalid")
continue
@@ -418,7 +430,10 @@ def _read_rollout_usage(
warnings.add("thread_outside_scan_window")
return total, warnings
boundary_reached = True
- elif event.get("type") == "event_msg" and payload.get("type") == "token_count":
+ elif (
+ event.get("type") == "event_msg"
+ and payload.get("type") == "token_count"
+ ):
inherited_usage = _token_snapshot(payload)
if inherited_usage is not None:
previous = inherited_usage
@@ -497,10 +512,25 @@ def _token_snapshot(payload: Mapping[str, Any]) -> dict[str, int] | None:
usage = info.get("total_token_usage")
if not isinstance(usage, dict):
return None
+ cache_write = usage.get(
+ "cache_write_input_tokens", usage.get("cache_write_tokens", 0)
+ )
+ legacy_cache_write = usage.get("cache_write_tokens")
+ input_tokens = usage.get("input_tokens")
+ cached_input_tokens = usage.get("cached_input_tokens", 0)
+ if (
+ cache_write == 0
+ and type(legacy_cache_write) is int
+ and legacy_cache_write > 0
+ and type(input_tokens) is int
+ and type(cached_input_tokens) is int
+ and cached_input_tokens + legacy_cache_write <= input_tokens
+ ):
+ cache_write = legacy_cache_write
result: dict[str, int] = {}
for source_key, result_key in TOKEN_FIELDS.items():
value = (
- usage.get(source_key, usage.get("cache_write_tokens", 0))
+ cache_write
if source_key == "cache_write_input_tokens"
else usage.get(source_key, 0)
)
@@ -511,7 +541,10 @@ def _token_snapshot(payload: Mapping[str, Any]) -> dict[str, int] | None:
):
return None
result[result_key] = value
- if result["cachedInputTokens"] + result["cacheWriteInputTokens"] > result["inputTokens"]:
+ if (
+ result["cachedInputTokens"] + result["cacheWriteInputTokens"]
+ > result["inputTokens"]
+ ):
return None
result["totalTokens"] = result["inputTokens"] + result["outputTokens"]
return result
@@ -540,7 +573,9 @@ def _timestamp(value: object) -> datetime | None:
return parsed.astimezone(timezone.utc) if parsed.tzinfo is not None else None
-def _unavailable_usage(reason: str, *, warnings: set[str] | None = None) -> dict[str, Any]:
+def _unavailable_usage(
+ reason: str, *, warnings: set[str] | None = None
+) -> dict[str, Any]:
return {
"coverage": "unavailable",
"source": "codex_rollout",
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index a36ce8a7f..ac27122a6 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -55,8 +55,8 @@
"dependencies": {
"@inquirer/prompts": "8.3.0",
"@octokit/core": "7.0.6",
- "@openai/codex": "0.144.6",
- "@openai/codex-sdk": "0.144.6",
+ "@openai/codex": "0.148.0-alpha.8",
+ "@openai/codex-sdk": "0.148.0-alpha.8",
"ajv": "8.20.0",
"extract-zip": "2.0.1",
"fast-uri": "3.1.5",
diff --git a/sdk/typescript/pnpm-lock.yaml b/sdk/typescript/pnpm-lock.yaml
index 388e49e40..178ce1350 100644
--- a/sdk/typescript/pnpm-lock.yaml
+++ b/sdk/typescript/pnpm-lock.yaml
@@ -15,11 +15,11 @@ importers:
specifier: 7.0.6
version: 7.0.6
'@openai/codex':
- specifier: 0.144.6
- version: 0.144.6
+ specifier: 0.148.0-alpha.8
+ version: 0.148.0-alpha.8
'@openai/codex-sdk':
- specifier: 0.144.6
- version: 0.144.6
+ specifier: 0.148.0-alpha.8
+ version: 0.148.0-alpha.8
ajv:
specifier: 8.20.0
version: 8.20.0
@@ -323,47 +323,47 @@ packages:
'@octokit/[email protected]':
resolution: {integrity: sha512-sKq+9r1Mm4efXW1FCk7hFSeJo4QKreL/tTbR0rz/qx/r1Oa2VV83LTA/H/MuCOX7uCIJmQVRKBcbmWoySjAnSg==}
- '@openai/[email protected]':
- resolution: {integrity: sha512-jFgXHjFq2//PTfJa8CySqhUilRBPVmCLuQoH5F+iJ2x4owZwPlnqmIbCkWIJJ6IxDuQdjf3ewg0LaDtc3i6h9Q==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-3t54+WC9E/YANQIf2LomahmLXYe8pOuCe5//wZi4tE7o2sjJPCBA01g3ySlMUjXdQrVZC7J+KzOekTBGabwLnA==}
engines: {node: '>=18'}
- '@openai/[email protected]':
- resolution: {integrity: sha512-wk+2CWiBNXiJLBoN2D08N9RceWkSBnlgk5g2K1a4CXrP/C0gdlHyRUG7RFzm9y41DCK/7tvCct233JVxyFmznw==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-+fOazxjtlG8SMs/TJi96Ta3OcZd2fgTR2Qi25eOemr6ehLYHFag4mEU7Ko2KLNoLOk8xtbvVvduZa0Ebk2AGOg==}
engines: {node: '>=16'}
hasBin: true
- '@openai/[email protected]':
- resolution: {integrity: sha512-6zgvh70MzBNSeT17HEhSOrmmGGZGAKzSC7x6JAq+edkJkdPYA9P0I1tG7aJ49GlBkBxuC+MKBH1qm6+2Cghcww==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-9V4LsW8Sg5u8yyR1HwLdtXo7ywIfQnyRJbcxnUfT2k2Y7CPn+DFnK/zPtXJdpXC2BVh4NTjyLKDGj+iD2UgqSA==}
engines: {node: '>=16'}
cpu: [arm64]
os: [darwin]
- '@openai/[email protected]':
- resolution: {integrity: sha512-THRyPG0zSU6M8NQAge1LHEHsJDnoH4BpKsfJHB/qe3Fm+Wf6zqAmWJFlOKzBm27m0K2Hq3za4Ac2I5p5i4yp/A==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-D3V9D9v/UanU+Qf2wm3c937Dfw+/WlvWgq03yPtOlrzPWiQMorEYwdHvHdH5b2qz9pCCp1LxQicqLPbME6PjoA==}
engines: {node: '>=16'}
cpu: [x64]
os: [darwin]
- '@openai/[email protected]':
- resolution: {integrity: sha512-PGiLXMN+2IQRkf7tOLi64dMInjU1pRLbz0Rwfj/yt2Y97SZQqAjFQoi2wmswmqtqMDnfwCPTC1DRXVQkvU6T6Q==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-bGv145obh67MDW28apTtIX8LSho3vz7mQhknfIfYHhwkUFLb7vInI68CQqST7ZxkBVje7mCjvqzP/Bz38rf2bg==}
engines: {node: '>=16'}
cpu: [arm64]
os: [linux]
- '@openai/[email protected]':
- resolution: {integrity: sha512-4E7EnzCg0OnBxCyYnwJ+qnZwWHYe0YScr5ucKWbngE9u4+0XrpWELqq2Kn9jl5GZK8MDjU7PrJwFIwusHOHjuw==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-UbWizYL5piFXvG/O9ykqOd7G5dc9SmG/rFIWwWIKrWqhL/OVdO31WbRXa4OqhnRGmoOmmgPey1Smi2X0rGm9kw==}
engines: {node: '>=16'}
cpu: [x64]
os: [linux]
- '@openai/[email protected]':
- resolution: {integrity: sha512-SpMjXJLW43JzMP0K62mVcYfmFcpk0BK4AOgYmWSfyZHs3iRtHMd0UYw7605n/9lwkT2EqbwQLT2omZFeKJFzwA==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-YwKtu/g2hEN1AquplybyRuacoSJOw1blVEED+y3GsImnG5BhfOKDV4c5XOvgohbPXs1nUbuMP6y8s4P3KRC4uQ==}
engines: {node: '>=16'}
cpu: [arm64]
os: [win32]
- '@openai/[email protected]':
- resolution: {integrity: sha512-dN39VnjEthKz5io1RNWwZDtErdSn07nW3pGUgvlA6DMxgm/nuGaIAZO/sG/Hgxq/x5j9HteAENfrFgVkpZ0lFg==}
+ '@openai/[email protected]':
+ resolution: {integrity: sha512-tBW2FpwZHHSYw13pKKwRJzUh5Xp8KnwsaOqoUXX4s2tq+ydq4TZqReVN0LR+7lhajDVEm2e5rfGNUCh+B1asmw==}
engines: {node: '>=16'}
cpu: [x64]
os: [win32]
@@ -817,35 +817,35 @@ snapshots:
dependencies:
'@octokit/openapi-types': 27.0.0
- '@openai/[email protected]':
+ '@openai/[email protected]':
dependencies:
- '@openai/codex': 0.144.6
+ '@openai/codex': 0.148.0-alpha.8
- '@openai/[email protected]':
+ '@openai/[email protected]':
optionalDependencies:
- '@openai/codex-darwin-arm64': '@openai/[email protected]'
- '@openai/codex-darwin-x64': '@openai/[email protected]'
- '@openai/codex-linux-arm64': '@openai/[email protected]'
- '@openai/codex-linux-x64': '@openai/[email protected]'
- '@openai/codex-win32-arm64': '@openai/[email protected]'
- '@openai/codex-win32-x64': '@openai/[email protected]'
-
- '@openai/[email protected]':
+ '@openai/codex-darwin-arm64': '@openai/[email protected]'
+ '@openai/codex-darwin-x64': '@openai/[email protected]'
+ '@openai/codex-linux-arm64': '@openai/[email protected]'
+ '@openai/codex-linux-x64': '@openai/[email protected]'
+ '@openai/codex-win32-arm64': '@openai/[email protected]'
+ '@openai/codex-win32-x64': '@openai/[email protected]'
+
+ '@openai/[email protected]':
optional: true
- '@openai/[email protected]':
+ '@openai/[email protected]':
optional: true
- '@openai/[email protected]':
+ '@openai/[email protected]':
optional: true
- '@openai/[email protected]':
+ '@openai/[email protected]':
optional: true
- '@openai/[email protected]':
+ '@openai/[email protected]':
optional: true
- '@openai/[email protected]':
+ '@openai/[email protected]':
optional: true
'@scalar/[email protected]': {}
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index c6cdc1375..0f19f691a 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -67,6 +67,7 @@ import type { SeverityLevel } from "./models.js";
import { scanActivitiesFromEvent, type ScanActivity } from "./scan-activity.js";
import {
matchCompletedScan,
+ matchScanFindingsInternal,
type matchScanFindings,
} from "./scan-comparison.js";
import {
@@ -966,6 +967,7 @@ export class CodexSecurity {
CODEX_SECURITY_SCAN_DIR: scanDir,
CODEX_SECURITY_PLUGIN_ROOT: shellPluginRoot,
CODEX_SECURITY_STATE_DIR: stateDirectory,
+ CODEX_SECURITY_SURFACE: this.#surface,
CODEX_SECURITY_SCAN_ID: scanId,
CODEX_SECURITY_TARGET_ID: targetId,
CODEX_SECURITY_TARGET_DISPLAY_NAME: basename(repo),
@@ -1266,7 +1268,12 @@ export class CodexSecurity {
falsePositives: falsePositiveExamples as Record<string, unknown>[],
findings: result.findings.findings,
workbench: runWorkbench,
- matchFindings: this.#dependencies.matchFindings,
+ matchFindings:
+ this.#dependencies.matchFindings ??
+ ((input, comparisonOptions) =>
+ matchScanFindingsInternal(input, comparisonOptions, {
+ surface: this.#surface,
+ })),
environment,
model,
signal,
@@ -2050,7 +2057,7 @@ export async function runScanEvents(
let lastStreamError: string | null = null;
let tacStatusReported = false;
try {
- for await (const event of options.events) {
+ for await (const event of scanEventsWithOptionalUsage(options.events)) {
if (!tacStatusReported) {
const tacStatus = trustedAccessStatusFromEvent(event);
if (tacStatus !== null) {
@@ -2211,6 +2218,24 @@ export async function runScanEvents(
}
}
+async function* scanEventsWithOptionalUsage(
+ events: AsyncGenerator<ScanEvent>,
+): AsyncGenerator<ScanEvent> {
+ try {
+ yield* events;
+ } catch (error) {
+ if (
+ error instanceof TypeError &&
+ /\b(?:null|undefined)\b/u.test(error.message) &&
+ /\bcache_write_input_tokens\b/u.test(error.message)
+ ) {
+ yield { type: "turn.completed", usage: null };
+ return;
+ }
+ throw error;
+ }
+}
+
function trustedAccessStatusFromEvent(
event: ScanEvent,
): ScanTrustedAccessStatus | null {
diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.ts
index 45e23021f..5bb5c1061 100644
--- a/sdk/typescript/src/cost.ts
+++ b/sdk/typescript/src/cost.ts
@@ -755,8 +755,17 @@ function tokenUsage(value: unknown): ScanTokenUsage | null {
if (!isRecord(value)) return null;
const input = value["input_tokens"];
const cached = value["cached_input_tokens"] ?? 0;
+ const canonicalCacheWrite = value["cache_write_input_tokens"];
+ const legacyCacheWrite = value["cache_write_tokens"];
const cacheWrite =
- value["cache_write_input_tokens"] ?? value["cache_write_tokens"] ?? 0;
+ canonicalCacheWrite === 0 &&
+ isTokenCount(input) &&
+ isTokenCount(cached) &&
+ isTokenCount(legacyCacheWrite) &&
+ legacyCacheWrite > 0 &&
+ cached + legacyCacheWrite <= input
+ ? legacyCacheWrite
+ : canonicalCacheWrite ?? legacyCacheWrite ?? 0;
const output = value["output_tokens"];
const reasoning = value["reasoning_output_tokens"] ?? 0;
if (
diff --git a/sdk/typescript/src/version.ts b/sdk/typescript/src/version.ts
index a17a0fee5..65f403962 100644
--- a/sdk/typescript/src/version.ts
+++ b/sdk/typescript/src/version.ts
@@ -9,7 +9,7 @@ const PACKAGE_VERSIONS = packageVersions(
export const VERSION = PACKAGE_VERSIONS.package;
export const CODEX_SDK_VERSION = PACKAGE_VERSIONS.sdk;
export const CODEX_EXECUTABLE_VERSION = PACKAGE_VERSIONS.executable;
-export const BUNDLED_PLUGIN_VERSION = "0.1.19" as const;
+export const BUNDLED_PLUGIN_VERSION = "0.1.20" as const;
const PACKAGE_NAME = "@openai/codex-security";
const VERSION_PATTERN =
diff --git a/sdk/typescript/tests-ts/api-attribution-concurrency.test.ts b/sdk/typescript/tests-ts/api-attribution-concurrency.test.ts
new file mode 100644
index 000000000..7869890ac
--- /dev/null
+++ b/sdk/typescript/tests-ts/api-attribution-concurrency.test.ts
@@ -0,0 +1,141 @@
+import { mkdir, readFile } from "node:fs/promises";
+import { join } from "node:path";
+import type { CodexOptions } from "@openai/codex-sdk";
+import { afterEach, describe, expect, test } from "bun:test";
+import { parse as parseToml } from "smol-toml";
+import { CodexSecurity } from "../src/index.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { createApiTestFixtures } from "./support/api-events.js";
+
+const fixtures = createApiTestFixtures();
+const InternalCodexSecurity = CodexSecurity as unknown as new (
+ config: Record<string, unknown>,
+ dependencies: Record<string, unknown>,
+ runtimeOptions?: { surface: "cli" | "sdk" },
+) => CodexSecurity;
+
+afterEach(async () => {
+ await fixtures.cleanup();
+});
+
+describe("delegated scan attribution", () => {
+ test.each(["standard", "deep"] as const)(
+ "keeps overlapping CLI and SDK %s scans concurrent and correctly attributed",
+ async (mode) => {
+ const root = await fixtures.temporaryDirectory();
+ const repository = join(root, "repository");
+ const ambientHome = join(root, "ambient-home");
+ const stateDirectory = join(root, "state");
+ const credentialHome = join(stateDirectory, "codex-home");
+ await mkdir(repository);
+ await mkdir(ambientHome);
+ let active = 0;
+ let maximumActive = 0;
+ let releaseConcurrentScans!: () => void;
+ const concurrentScans = new Promise<void>((resolve) => {
+ releaseConcurrentScans = resolve;
+ });
+
+ const clients = await Promise.all(
+ (["cli", "sdk"] as const).map(async (surface) => {
+ const scanDirectory = join(root, `${surface}-scan`);
+ await mkdir(scanDirectory, { mode: 0o700 });
+ return new InternalCodexSecurity(
+ { pluginPath: PLUGIN_ROOT },
+ {
+ environment: {
+ CODEX_HOME: ambientHome,
+ CODEX_SECURITY_STATE_DIR: stateDirectory,
+ CODEX_SECURITY_SURFACE: "spoofed",
+ OPENAI_API_KEY: `synthetic-${surface}-key`,
+ },
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDirectory,
+ repositoryRevision: async () => "deadbeef",
+ runWorkbench: async (
+ _options: unknown,
+ args: readonly string[],
+ ) => {
+ if (args[0] === "register-cli-scan") {
+ return {
+ scanId: `scan_${surface}`,
+ targetId: `target_${surface}`,
+ targetRevision: "deadbeef",
+ scanDir: scanDirectory,
+ contract: { target: { allowedKinds: ["git_revision"] } },
+ };
+ }
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId: `scan_${surface}`,
+ targetId: `target_${surface}`,
+ falsePositives: [],
+ };
+ }
+ return {};
+ },
+ createCodex: (options: CodexOptions) => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ active += 1;
+ maximumActive = Math.max(maximumActive, active);
+ if (active === 2) releaseConcurrentScans();
+ try {
+ expect(options.env?.["CODEX_HOME"]).toBe(credentialHome);
+ expect(options.env?.["CODEX_SECURITY_SURFACE"]).toBe(
+ surface,
+ );
+ expect(options.config).toMatchObject({
+ responses_api_metadata: {
+ codex_security_surface: surface,
+ },
+ });
+ await concurrentScans;
+ const sharedConfig = parseToml(
+ await readFile(
+ join(credentialHome, "config.toml"),
+ "utf8",
+ ),
+ );
+ expect(sharedConfig).not.toHaveProperty(
+ "responses_api_metadata",
+ );
+ expect(options.env?.["CODEX_SECURITY_SURFACE"]).toBe(
+ surface,
+ );
+ throw new Error("delegated attribution observed");
+ } finally {
+ active -= 1;
+ }
+ },
+ }),
+ }),
+ },
+ { surface },
+ );
+ }),
+ );
+
+ try {
+ const results = await Promise.allSettled(
+ clients.map((client) =>
+ client.run(repository, { mode }).finally(releaseConcurrentScans),
+ ),
+ );
+ for (const result of results) {
+ expect(result).toMatchObject({
+ status: "rejected",
+ reason: expect.objectContaining({
+ message: "delegated attribution observed",
+ }),
+ });
+ }
+ expect(maximumActive).toBe(2);
+ } finally {
+ releaseConcurrentScans();
+ await Promise.all(clients.map(async (client) => await client.close()));
+ }
+ },
+ );
+});
diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.ts
index 6ff1fb656..9c7b32c49 100644
--- a/sdk/typescript/tests-ts/api-events.test.ts
+++ b/sdk/typescript/tests-ts/api-events.test.ts
@@ -1,7 +1,11 @@
import { mkdir, stat } from "node:fs/promises";
import { existsSync } from "node:fs";
import { join } from "node:path";
-import type { McpToolCallItem, ThreadEvent } from "@openai/codex-sdk";
+import {
+ Codex,
+ type McpToolCallItem,
+ type ThreadEvent,
+} from "@openai/codex-sdk";
import { afterEach, describe, expect, test } from "bun:test";
import { runScanEvents } from "../src/api.js";
import {
@@ -70,6 +74,7 @@ async function* tacEvents(
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -113,6 +118,53 @@ describe("one-shot scan events", () => {
});
});
+ test.each([null, undefined])(
+ "preserves completed scans when the real Codex SDK receives %p token usage",
+ async (usage) => {
+ const scanDir = await copyCompletedScan(await temporaryDirectory());
+ const thread = new Codex({
+ codexPathOverride: process.execPath,
+ }).startThread();
+ const executable = thread as unknown as {
+ _exec: { run(): AsyncGenerator<string> };
+ };
+ executable._exec.run = async function* () {
+ yield JSON.stringify({ type: "thread.started", thread_id: "thread-1" });
+ yield JSON.stringify({
+ type: "item.completed",
+ item: {
+ id: "message-1",
+ type: "agent_message",
+ text: "scan complete",
+ },
+ });
+ yield JSON.stringify({ type: "turn.completed", usage });
+ };
+
+ const { events } = await thread.runStreamed("Scan the repository.");
+ const result = await runEvents(scanDir, events);
+
+ expect(result.threadId).toBe("thread-1");
+ expect(result.turnResult).toMatchObject({
+ status: "completed",
+ finalResponse: "scan complete",
+ });
+ expect(result.cost).toBeNull();
+ },
+ );
+
+ test("does not suppress unrelated SDK stream failures", async () => {
+ const scanDir = await copyCompletedScan(await temporaryDirectory());
+ async function* failedEvents(): AsyncGenerator<ThreadEvent> {
+ yield { type: "thread.started", thread_id: "thread-1" };
+ throw new TypeError("Cannot read properties of null (reading 'message')");
+ }
+
+ await expect(runEvents(scanDir, failedEvents())).rejects.toThrow(
+ "reading 'message'",
+ );
+ });
+
test("reports granted trusted cyber access once without a warning", async () => {
const scanDir = await copyCompletedScan(await temporaryDirectory());
const warnings: string[] = [];
@@ -451,6 +503,7 @@ describe("one-shot scan events", () => {
expect(usage).toMatchObject({
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
});
expect(existsSync(join(scanDir, "scan-manifest.json"))).toBe(false);
@@ -580,6 +633,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 1,
cached_input_tokens: 0,
+ cache_write_input_tokens: 0,
output_tokens: 1,
reasoning_output_tokens: 0,
},
@@ -640,6 +694,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -689,6 +744,7 @@ describe("one-shot scan events", () => {
const usage = {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
};
@@ -720,6 +776,7 @@ describe("one-shot scan events", () => {
const usage = {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
};
@@ -957,6 +1014,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -1001,6 +1059,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -1052,6 +1111,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -1103,6 +1163,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -1150,6 +1211,7 @@ describe("one-shot scan events", () => {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
diff --git a/sdk/typescript/tests-ts/api-preflight-config.test.ts b/sdk/typescript/tests-ts/api-preflight-config.test.ts
index 795aeb8b8..0fe94b338 100644
--- a/sdk/typescript/tests-ts/api-preflight-config.test.ts
+++ b/sdk/typescript/tests-ts/api-preflight-config.test.ts
@@ -106,6 +106,24 @@ describe("CodexSecurity preflight configuration", () => {
});
});
+ test("preserves configured Responses metadata without persisting scan attribution", () => {
+ const stateDirectory = join(tmpdir(), "codex-security-persistent-state");
+ const credentialHome = join(stateDirectory, "codex-home");
+ const config = scanRuntimeCodexConfig(
+ {
+ responses_api_metadata: {
+ request_trace: "preserve-configured-metadata",
+ },
+ },
+ stateDirectory,
+ credentialHome,
+ );
+
+ expect(config["responses_api_metadata"]).toEqual({
+ request_trace: "preserve-configured-metadata",
+ });
+ });
+
test("projects only capability and trust metadata into the readable preflight config", async () => {
const root = await temporaryDirectory();
const configPath = join(root, "config-preflight.toml");
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 797a4685e..164fed9c7 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -260,6 +260,7 @@ async function* completedEvents(): AsyncGenerator<ThreadEvent> {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},
@@ -3022,6 +3023,7 @@ describe("CodexSecurity orchestration", () => {
const usage = {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
};
@@ -3559,18 +3561,25 @@ describe("CodexSecurity orchestration", () => {
}
return {};
},
- createCodex: () => ({
- startThread: () => ({
- id: null,
- async runStreamed() {
+ createCodex: (options: CodexOptions) => ({
+ startThread(threadOptions: Parameters<Codex["startThread"]>[0]) {
+ const thread = new Codex({
+ ...options,
+ codexPathOverride: process.execPath,
+ }).startThread(threadOptions);
+ const executable = thread as unknown as {
+ _exec: { run(): AsyncGenerator<string> };
+ };
+ executable._exec.run = async function* () {
await copyCompletedScan(root);
- async function* events() {
- yield { type: "thread.started", thread_id: "scan-thread" };
- yield { type: "turn.completed", usage: null };
- }
- return { events: events() };
- },
- }),
+ yield JSON.stringify({
+ type: "thread.started",
+ thread_id: "scan-thread",
+ });
+ yield JSON.stringify({ type: "turn.completed", usage: null });
+ };
+ return thread;
+ },
}),
},
);
@@ -4039,6 +4048,7 @@ describe("CodexSecurity orchestration", () => {
join(codexHome!, "config.toml"),
"utf8",
);
+ expect(options.env?.["CODEX_SECURITY_SURFACE"]).toBe("sdk");
expect(codexConfig).toContain(
'model_reasoning_summary = "detailed"',
);
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 6df445e48..d526421c8 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -36,6 +36,7 @@ import {
} from "../src/index.js";
import { main, parseCodexOverrides, Progress } from "../src/cli.js";
import { scanPreflightCodexConfig } from "../src/api.js";
+import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "../src/version.js";
import {
DEFAULT_CODEX_CONFIG,
FIREWORKS_CODEX_PROVIDER,
@@ -1011,8 +1012,8 @@ describe("CLI", () => {
bundledPluginVersion: BUNDLED_PLUGIN_VERSION,
scanMcp: false,
cliVersion: VERSION,
- codexVersion: "0.144.6",
- codexSdkVersion: "0.144.6",
+ codexVersion: CODEX_EXECUTABLE_VERSION,
+ codexSdkVersion: CODEX_SDK_VERSION,
model: "gpt-5.6-sol",
reasoningEffort: "xhigh",
nextStep: "codex-security scan . --dry-run",
diff --git a/sdk/typescript/tests-ts/cost.test.ts b/sdk/typescript/tests-ts/cost.test.ts
index 9b01a9b42..520efe90c 100644
--- a/sdk/typescript/tests-ts/cost.test.ts
+++ b/sdk/typescript/tests-ts/cost.test.ts
@@ -116,47 +116,54 @@ function progressMessage(
}
describe("scan cost", () => {
- test("retains cached and alternate cache-write usage in workbench totals", async () => {
- const { PLUGIN_ROOT } = await import("./plugin-root.js");
- const python = Bun.which("python3") ?? Bun.which("python");
- expect(python).not.toBeNull();
- const usage = {
- input_tokens: 100,
- cached_input_tokens: 40,
- cache_write_tokens: 15,
- output_tokens: 20,
- reasoning_output_tokens: 5,
- total_tokens: 120,
- };
- const probe = [
- "import json, sys",
- "sys.path.insert(0, sys.argv[1])",
- "import workbench_scan_usage",
- "payload = {'info': {'total_token_usage': json.loads(sys.argv[2])}}",
- "print(json.dumps(workbench_scan_usage._token_snapshot(payload)))",
- ].join("\n");
- const result = spawnSync(
- python!,
- [
- "-I",
- "-B",
- "-c",
- probe,
- join(PLUGIN_ROOT, "scripts"),
- JSON.stringify(usage),
- ],
- { encoding: "utf8" },
- );
-
- expect(result.status, result.stderr).toBe(0);
- expect(JSON.parse(result.stdout)).toMatchObject({
- inputTokens: 100,
- cachedInputTokens: 40,
- cacheWriteInputTokens: 15,
- outputTokens: 20,
- totalTokens: 120,
- });
- });
+ test.each([
+ [{ cache_write_tokens: 15 }, 15],
+ [{ cache_write_input_tokens: 0, cache_write_tokens: 15 }, 15],
+ [{ cache_write_input_tokens: 0, cache_write_tokens: 80 }, 0],
+ ] as const)(
+ "keeps workbench cache-write normalization aligned with SDK usage",
+ async (cacheWrites, expectedCacheWrites) => {
+ const { PLUGIN_ROOT } = await import("./plugin-root.js");
+ const python = Bun.which("python3") ?? Bun.which("python");
+ expect(python).not.toBeNull();
+ const usage = {
+ input_tokens: 100,
+ cached_input_tokens: 40,
+ ...cacheWrites,
+ output_tokens: 20,
+ reasoning_output_tokens: 5,
+ total_tokens: 120,
+ };
+ const probe = [
+ "import json, sys",
+ "sys.path.insert(0, sys.argv[1])",
+ "import workbench_scan_usage",
+ "payload = {'info': {'total_token_usage': json.loads(sys.argv[2])}}",
+ "print(json.dumps(workbench_scan_usage._token_snapshot(payload)))",
+ ].join("\n");
+ const result = spawnSync(
+ python!,
+ [
+ "-I",
+ "-B",
+ "-c",
+ probe,
+ join(PLUGIN_ROOT, "scripts"),
+ JSON.stringify(usage),
+ ],
+ { encoding: "utf8" },
+ );
+
+ expect(result.status, result.stderr).toBe(0);
+ expect(JSON.parse(result.stdout)).toMatchObject({
+ inputTokens: 100,
+ cachedInputTokens: 40,
+ cacheWriteInputTokens: expectedCacheWrites,
+ outputTokens: 20,
+ totalTokens: 120,
+ });
+ },
+ );
test("uses published GPT-5.6 model rates", () => {
const usage = { input_tokens: 1_000_000, output_tokens: 1_000_000 };
@@ -247,6 +254,30 @@ describe("scan cost", () => {
).toBe(0.0051);
});
+ test("preserves legacy cache writes after SDK normalization adds zero", () => {
+ expect(
+ estimateScanCost("gpt-5.6-sol", {
+ input_tokens: 1_000,
+ cached_input_tokens: 100,
+ cache_write_input_tokens: 0,
+ cache_write_tokens: 200,
+ output_tokens: 10,
+ }),
+ ).toMatchObject({ cacheWriteInputTokens: 200, estimatedUsd: 0.0051 });
+ });
+
+ test("ignores impossible legacy cache writes while retaining canonical usage", () => {
+ expect(
+ estimateScanCost("gpt-5.6-sol", {
+ input_tokens: 1_000,
+ cached_input_tokens: 100,
+ cache_write_input_tokens: 0,
+ cache_write_tokens: 1_001,
+ output_tokens: 10,
+ }),
+ ).toMatchObject({ cacheWriteInputTokens: 0, estimatedUsd: 0.00485 });
+ });
+
test("does not double-charge reasoning tokens included in output", () => {
expect(
estimateScanCost("gpt-5.6-sol", {
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 4f12e7af6..2a3f38fee 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -33,6 +33,7 @@ import { brotliDecompressSync } from "node:zlib";
import { afterEach, describe, expect, mock, test } from "bun:test";
import { strToU8, zipSync } from "fflate";
import {
+ BUNDLED_PLUGIN_VERSION,
bootstrapPlugin,
bundledPluginRoot,
createIsolatedHome,
@@ -1423,6 +1424,68 @@ describe("plugin runtime preparation", () => {
]);
});
+ test("refreshes cached plugins before forwarding delegated scan attribution", async () => {
+ const root = await temporaryDirectory();
+ const previous = await plugin(join(root, "previous"), "0.1.19");
+ await writeFile(
+ join(previous, ".mcp.json"),
+ JSON.stringify({
+ mcpServers: { "codex-security": { env_vars: [] } },
+ }),
+ );
+ const home = join(root, "home");
+ const marketplace = join(home, "sdk-marketplace");
+ await mkdir(home);
+ const runCodex: NonNullable<
+ NonNullable<Parameters<typeof bootstrapPlugin>[2]>["runCodex"]
+ > = async (_command, args) => {
+ if (args[1] === "marketplace") {
+ await writeFile(
+ join(home, "config.toml"),
+ `[marketplaces.codex-security-sdk]\nsource_type = "local"\nsource = ${JSON.stringify(marketplace)}\n`,
+ );
+ return "";
+ }
+ const manifest = JSON.parse(
+ await readFile(
+ join(
+ marketplace,
+ "plugins",
+ "codex-security",
+ ".codex-plugin",
+ "plugin.json",
+ ),
+ "utf8",
+ ),
+ ) as { version: string };
+ return JSON.stringify({
+ installedPath: join(home, "installed", manifest.version),
+ version: manifest.version,
+ });
+ };
+ const options = {
+ codexCommand: { command: "/codex", prefixArgs: [] },
+ runCodex,
+ };
+
+ expect((await bootstrapPlugin(home, previous, options)).version).toBe(
+ "0.1.19",
+ );
+ const upgraded = await bootstrapPlugin(home, PLUGIN_ROOT, options);
+ const configuration = JSON.parse(
+ await readFile(
+ join(marketplace, "plugins", "codex-security", ".mcp.json"),
+ "utf8",
+ ),
+ ) as { mcpServers: Record<string, { env_vars: string[] }> };
+
+ expect(upgraded.version).toBe(BUNDLED_PLUGIN_VERSION);
+ expect(upgraded.version).not.toBe("0.1.19");
+ expect(configuration.mcpServers["codex-security"]?.env_vars).toContain(
+ "CODEX_SECURITY_SURFACE",
+ );
+ });
+
test("rejects plugin installs without the selected path and version", async () => {
for (const output of [
"not JSON",
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index 9234de44e..c1de5c7d0 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -68,6 +68,7 @@ export async function* completedEvents(): AsyncGenerator<ThreadEvent> {
usage: {
input_tokens: 10,
cached_input_tokens: 2,
+ cache_write_input_tokens: 0,
output_tokens: 3,
reasoning_output_tokens: 1,
},