OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(history): preserve native turn failures observed before cancellation (#735)
HughChaw8a44cb055767
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 6c8e27f91..12e0a2b73 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -2039,6 +2039,7 @@ export class CodexSecurity {status: "pending" as "pending" | "running" | "complete",};let completionCost: ScanCost | null = null;+ let nativeTurnFailure: string | undefined;let scanTurnStatus: "in_progress" | "completed" | "failed" | null = null;let budgetRecovery: {expectation: ScanExpectation;@@ -3288,8 +3289,16 @@ export class CodexSecurity {modelProvider,workbenchValidated: true,model,- onTurnStatus: (status) => {+ onTurnStatus: (status, message) => {scanTurnStatus = status;+ // A later caller abort during iterator cleanup cannot undo a reported failure.+ if (+ status === "failed" &&+ !directDeepScan &&+ !this.#dependencies.hostedScan &&+ !signal.aborted+ )+ nativeTurnFailure = message;},onThreadStarted: async (threadId) => {if (resumeThreadId !== undefined && threadId !== resumeThreadId) {@@ -3683,6 +3692,9 @@ export class CodexSecurity {callerSignal.aborted && Object.is(signal.reason, callerSignal.reason)? callerSignal: undefined;+ const priorTurnFailure = callerCancellation+ ? nativeTurnFailure+ : undefined;let failure =signal.aborted && callerCancellation === undefined? signal.reason@@ -3825,8 +3837,9 @@ export class CodexSecurity {this.#dependencies.hostedScan? hostedScanFailureStatus(failure, callerCancellation) ==="canceled"- : explicitlyCanceled(callerCancellation),- failure,+ : priorTurnFailure === undefined &&+ explicitlyCanceled(callerCancellation),+ priorTurnFailure ?? failure,snapshot?.cost,);activeScan = null;@@ -5297,7 +5310,7 @@ interface ScanEventRunOptions extends Pick<ScanOptions, "onReconnect"> {workbenchValidated?: boolean;model?: string;onFinalize?: (usage: unknown) => Promise<unknown>;- onTurnStatus?: (status: "completed" | "failed") => void;+ onTurnStatus?: (status: "completed" | "failed", message?: string) => void;onThreadStarted?: (threadId: string) => Promise<void> | void;onScanStarted?: () => void;onTrustedAccessStatus?: (status: ScanTrustedAccessStatus) => void;@@ -5323,7 +5336,8 @@ export async function runScanEvents(onEvent: async (event) => {if (event.type === "turn.completed")options.onTurnStatus?.("completed");- if (event.type === "turn.failed") options.onTurnStatus?.("failed");+ if (event.type === "turn.failed")+ options.onTurnStatus?.("failed", turnFailureMessage(event["error"]));if (!tacStatusReported &&options.modelProvider !== "amazon-bedrock" &&diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex 5184dda02..354ded7b7 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -3784,6 +3784,40 @@ if __name__ == "__main__":await client.close();});+ test.each([true, false])(+ "records a native failure only when its terminal event preceded cancellation (failure first: %p)",+ async (failureFirst) => {+ const { repository, codexHome, scanDir } = await scanDirectories();+ const controller = new AbortController();+ const message = "Synthetic native turn failure";+ async function* events(): AsyncGenerator<ThreadEvent> {+ if (!failureFirst) controller.abort();+ try {+ yield { type: "turn.failed", error: { message } };+ } finally {+ // The reader closes a failed stream after observing its terminal event.+ if (failureFirst) controller.abort();+ }+ }+ let stopped: readonly string[] | undefined;+ await using client = TestClient.withDependencies({+ ...scanRuntimeDependencies(codexHome, scanDir),+ createCodex: codexFactory(async () => ({ events: events() })),+ runWorkbench: async (_options, args, input) => {+ if (args[0] === "fail-scan" || args[0] === "cancel-scan")+ stopped = args;+ return mockWorkbench(args, input);+ },+ });+ await expect(+ client.run(repository, { signal: controller.signal }),+ ).rejects.toThrow();+ expect(controller.signal.aborted).toBe(true);+ expect(stopped?.[0]).toBe(failureFirst ? "fail-scan" : "cancel-scan");+ if (failureFirst) expect(stopped).toContain(`--message=${message}`);+ },+ );+test("reports a Deep Scan terminal failure instead of a completion-state error", async () => {const { root, repository, codexHome, scanDir } = await scanDirectories();const commands: string[] = [];