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

perf: defer CLI and plugin validators until first use (#2140)

Michael D'Angelo065d2e31ef18
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.ts
index 5f72d187a..1de1e0015 100644
--- a/plugins/codex-security/mcp-app/helpers-main.ts
+++ b/plugins/codex-security/mcp-app/helpers-main.ts
@@ -13,7 +13,6 @@ import {
normalizeCandidateBatch,
normalizeCandidatesCommand,
} from "./src/helpers/normalize-candidates";
-import { validatePatchRiskAssessmentCommand } from "./src/helpers/validate-patch-risk-assessment";
import { deepReviewInputCommand } from "./src/helpers/deep-review-input";
import { inventoryCommand } from "./src/helpers/generate-inventory";
import { bindRepoScopesCommand } from "./src/helpers/bind-repo-scopes";
@@ -99,7 +98,11 @@ function runHelper(): void {
} else if (command === "normalize-candidates") {
process.exitCode = normalizeCandidatesCommand(args, posixHome);
} else if (command === "validate-patch-risk-assessment") {
- process.exitCode = validatePatchRiskAssessmentCommand(args);
+ void import("./src/helpers/validate-patch-risk-assessment").then(
+ ({ validatePatchRiskAssessmentCommand }) => {
+ process.exitCode = validatePatchRiskAssessmentCommand(args);
+ },
+ );
} else if (command === "copy-deep-review-input") {
process.exitCode = deepReviewInputCommand(args, posixHome);
} else if (command === "bind-repo-scopes") {
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index 83e9237ea..37a48bea4 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -63,59 +63,65 @@ function provisionalPaths(schema: Record<string, unknown>) {
}
// Canonical files can retain fields that the compact tool input does not accept.
-const canonicalScanDraftInputSchema = fileScanDraftInputSchema.extend({
- scope: canonicalFieldSchema({
- ...scanManifestDocument.properties.scan.properties.scope,
- $defs: scanManifestDocument.$defs,
- required:
- z.toJSONSchema(draftDefinitions.scope, { io: "input" }).required ?? [],
- }).optional(),
- threatModel: canonicalFieldSchema({
- ...scanManifestDocument.properties.scan.properties.threatModel,
- $defs: scanManifestDocument.$defs,
- }).optional(),
- findings: canonicalFieldSchema({
- ...findingsDocument.properties.findings,
- $defs: findingsDocument.$defs,
- items: {
- ...findingsDocument.properties.findings.items,
- required: z.toJSONSchema(draftDefinitions.finding, { io: "input" })
- .required,
- },
- }),
- reviewState: draftDefinitions.reviewState.extend({
- surfaces: canonicalFieldSchema({
- ...coverageDocument.properties.surfaces,
- items: {
- ...coverageDocument.properties.surfaces.items,
- required: z.toJSONSchema(draftDefinitions.surface, { io: "input" })
- .required,
- },
- }),
- deferred: canonicalFieldSchema({
- ...coverageDocument.properties.deferred,
- items: {
- ...coverageDocument.properties.deferred.items,
- properties: {
- ...deferredFields,
- paths: provisionalPaths(deferredFields.paths),
+const canonicalScanDraftInputSchema = z.lazy(
+ () =>
+ fileScanDraftInputSchema.extend({
+ scope: canonicalFieldSchema({
+ ...scanManifestDocument.properties.scan.properties.scope,
+ $defs: scanManifestDocument.$defs,
+ required:
+ z.toJSONSchema(draftDefinitions.scope, { io: "input" }).required ??
+ [],
+ }).optional(),
+ threatModel: canonicalFieldSchema({
+ ...scanManifestDocument.properties.scan.properties.threatModel,
+ $defs: scanManifestDocument.$defs,
+ }).optional(),
+ findings: canonicalFieldSchema({
+ ...findingsDocument.properties.findings,
+ $defs: findingsDocument.$defs,
+ items: {
+ ...findingsDocument.properties.findings.items,
+ required: z.toJSONSchema(draftDefinitions.finding, { io: "input" })
+ .required,
},
- required: z.toJSONSchema(
- draftDefinitions.reviewState.shape.deferred.element,
- { io: "input" },
- ).required,
- },
- }),
- openQuestions: z
- .array(
- z.union([
- z.string(),
- canonicalFieldSchema(coverageDocument.properties.openQuestions.items),
- ]),
- )
- .optional(),
- }),
-}) as z.ZodType<ScanDraftInput>;
+ }),
+ reviewState: draftDefinitions.reviewState.extend({
+ surfaces: canonicalFieldSchema({
+ ...coverageDocument.properties.surfaces,
+ items: {
+ ...coverageDocument.properties.surfaces.items,
+ required: z.toJSONSchema(draftDefinitions.surface, { io: "input" })
+ .required,
+ },
+ }),
+ deferred: canonicalFieldSchema({
+ ...coverageDocument.properties.deferred,
+ items: {
+ ...coverageDocument.properties.deferred.items,
+ properties: {
+ ...deferredFields,
+ paths: provisionalPaths(deferredFields.paths),
+ },
+ required: z.toJSONSchema(
+ draftDefinitions.reviewState.shape.deferred.element,
+ { io: "input" },
+ ).required,
+ },
+ }),
+ openQuestions: z
+ .array(
+ z.union([
+ z.string(),
+ canonicalFieldSchema(
+ coverageDocument.properties.openQuestions.items,
+ ),
+ ]),
+ )
+ .optional(),
+ }),
+ }) as z.ZodType<ScanDraftInput>,
+);
export interface ScanDraftResult {
scanId: string;
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts b/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
index 016a4f10b..0e08c4d1e 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/project-config.ts
@@ -1,11 +1,11 @@
import { readFile, realpath } from "node:fs/promises";
import { homedir } from "node:os";
import { extname, isAbsolute, resolve } from "node:path";
-import Ajv from "ajv";
+import Ajv, { type ValidateFunction } from "ajv";
import { parseDocument } from "yaml";
import schema from "./project-config.schema.json";
-const validate = new Ajv({ allErrors: true }).compile(schema);
+let validate: ValidateFunction | undefined;
const fields = {
workers: "workers",
subagents_per_worker: "subagents",
@@ -54,6 +54,7 @@ export async function readDeepScanProjectConfig(
{ cause: error },
);
}
+ validate ??= new Ajv({ allErrors: true }).compile(schema);
if (!validate(value)) {
throw new Error(
`Invalid project configuration at ${path}: ${validate.errors!.map((issue) => `${issue.instancePath || "configuration"}: ${issue.keyword === "additionalProperties" ? `Unknown key ${issue.params["additionalProperty"]}.` : issue.message}`).join("; ")}`,
diff --git a/sdk/typescript/src/external-import-contract.ts b/sdk/typescript/src/external-import-contract.ts
index 293430efe..e9b88383b 100644
--- a/sdk/typescript/src/external-import-contract.ts
+++ b/sdk/typescript/src/external-import-contract.ts
@@ -1,6 +1,6 @@
import { isIP } from "node:net";
import { isNumberObject } from "node:util/types";
-import Ajv2020 from "ajv/dist/2020.js";
+import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
import schema from "../schemas/external-findings.schema.json" with { type: "json" };
import type {
ExternalFindingEvidence,
@@ -185,11 +185,12 @@ function validator<T>(
name: keyof typeof schema.$defs,
target: ExternalImportTarget = "cloud",
) {
- const check = ajv.compile<T>({
- $defs: (target === "local" ? localSchema : schema).$defs,
- $ref: `#/$defs/${name}`,
- });
+ let check: ValidateFunction<T> | undefined;
return (input: unknown): T => {
+ check ??= ajv.compile<T>({
+ $defs: (target === "local" ? localSchema : schema).$defs,
+ $ref: `#/$defs/${name}`,
+ });
if (name === "ImportedFindingEvidence")
normalizeEvidenceDetails(input, target);
else if (input && typeof input === "object") {