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(cli): report component scan progress headlessly (#807)

HughChaw39382e364501
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 67610c6c6..2bd94e487 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -125,7 +125,10 @@ import {
import type { Finding, SeverityLevel } from "./models.js";
import { runMultiscan } from "./multiscan.js";
import { componentPlanSchema, planComponents } from "./component-plan.js";
-import { runComponentScans } from "./component-scan.js";
+import {
+ runComponentScans,
+ type ComponentScanEvent,
+} from "./component-scan.js";
import {
checkScanPublication,
forceTerminatePublicationProcesses as terminatePublishers,
@@ -3530,6 +3533,7 @@ export async function main(
async run({ args, options }) {
const controller = new AbortController();
let dashboard: ScanDashboard | null = null;
+ const componentNames = new Map<string, string>();
const stopDashboard = (): void => {
try {
dashboard?.stop();
@@ -3624,10 +3628,20 @@ export async function main(
createSecurity: dependencies.createSecurity,
planComponents: dependencies.planComponents,
matchFindings: dependencies.matchFindings,
- onPlan: (components) => dashboard?.setComponents(components),
+ onPlan: (components) => {
+ for (const component of components)
+ componentNames.set(component.id, component.name);
+ dashboard?.setComponents(components);
+ },
onScanEvent:
dashboard === null
- ? undefined
+ ? (event) => {
+ const componentName =
+ componentNames.get(event.componentId) ??
+ event.componentId;
+ const line = componentScanEventLine(componentName, event);
+ if (line !== null) errorOutput.write(line);
+ }
: (event) => dashboard?.recordComponentEvent(event),
onDeduplicationStarted: () => {
if (dashboard !== null)
@@ -7637,6 +7651,24 @@ function formatTokenUsage(usage: unknown): string | null {
);
}
+function componentScanEventLine(
+ componentName: string,
+ event: ComponentScanEvent,
+): string | null {
+ if (event.type === "progress") {
+ const progress = event.value;
+ return `codex-security: ${componentName} ${scanPhase(progress.phase)} | Files: ${progress.filesCompleted.toLocaleString("en-US")}/${progress.filesTotal.toLocaleString("en-US")}\n`;
+ }
+ if (event.type !== "cost") return null;
+ const cost = event.value;
+ const tokens = formatTokenUsage({
+ input_tokens: cost.inputTokens,
+ cached_input_tokens: cost.cachedInputTokens,
+ output_tokens: cost.outputTokens,
+ });
+ return `codex-security: ${componentName} | ${tokens === null ? "" : `Tokens: ${tokens} | `}Cost: ${formatUsd(cost.estimatedUsd)}\n`;
+}
+
function protectedRootErrorMessage(
error: OutputInsideProtectedRootError,
): string {
diff --git a/sdk/typescript/tests-ts/component-scan.test.ts b/sdk/typescript/tests-ts/component-scan.test.ts
index 58f9810b1..2a58f69cd 100644
--- a/sdk/typescript/tests-ts/component-scan.test.ts
+++ b/sdk/typescript/tests-ts/component-scan.test.ts
@@ -463,14 +463,20 @@ test.each(["dashboard", "headless", "ci"])(
presentation === "ci" ? { CI: "true" } : { NO_COLOR: "1" },
}),
createSecurity: client(async (_repository, options) => {
- expect(typeof options.onProgress).toBe(
- presentation === "dashboard" ? "function" : "undefined",
- );
+ expect(typeof options.onProgress).toBe("function");
options.onProgress?.({
phase: "validation",
filesCompleted: 2,
filesTotal: 2,
});
+ options.onCost?.({
+ model: "gpt-5.6",
+ inputTokens: 100,
+ cachedInputTokens: 10,
+ cacheWriteInputTokens: 0,
+ outputTokens: 20,
+ estimatedUsd: 0.00123,
+ });
return completed(options);
}),
},
@@ -490,6 +496,14 @@ test.each(["dashboard", "headless", "ci"])(
stderr.text().indexOf("Component scans:"),
);
} else expect(stderr.text()).toContain("apps/api completed");
+ if (presentation !== "dashboard") {
+ expect(stderr.text()).toContain(
+ "apps/api validating findings | Files: 2/2",
+ );
+ expect(stderr.text()).toContain(
+ "apps/api | Tokens: 100 input, 10 cached, 20 output | Cost: $0.00123",
+ );
+ }
expect(
[...signals.listeners.values()].every(
(listeners) => listeners.size === 0,