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

refactor(plugin): port patch-risk validation to TypeScript (#838)

Kyle Brown230a5668e2a2
diff --git a/.github/workflows/native-windows.yml b/.github/workflows/native-windows.yml
index e3c7d18df..d67be284a 100644
--- a/.github/workflows/native-windows.yml
+++ b/.github/workflows/native-windows.yml
@@ -37,6 +37,9 @@ jobs:
with:
architecture: ${{ matrix.arch }}
compile: true
+ - name: Install helper dependencies
+ working-directory: .
+ run: pnpm --dir plugins/codex-security/mcp-app install --frozen-lockfile
- name: Set up Rust
run: rustup toolchain install 1.97.1 --profile minimal --component rustfmt --component clippy --target ${{ matrix.target }}
- name: Check Rust source
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.ts
index 29d07df72..8aad95453 100644
--- a/plugins/codex-security/mcp-app/helpers-main.ts
+++ b/plugins/codex-security/mcp-app/helpers-main.ts
@@ -3,6 +3,7 @@ import { resolveSecurityMdCommand } from "./src/helpers/resolve-security-md";
import { decodePosixBytes } from "./src/helpers/posix-path";
import { windowsBinding } from "./src/native";
import { normalizeCandidatesCommand } from "./src/helpers/normalize-candidates";
+import { validatePatchRiskAssessmentCommand } from "./src/helpers/validate-patch-risk-assessment";
let commandLine = process.argv.slice(2);
if (process.platform === "win32") {
@@ -32,9 +33,11 @@ if (command === "resolve-security-md") {
process.exitCode = resolveSecurityMdCommand(args, posixHome);
} else if (command === "normalize-candidates") {
process.exitCode = normalizeCandidatesCommand(args, posixHome);
+} else if (command === "validate-patch-risk-assessment") {
+ process.exitCode = validatePatchRiskAssessmentCommand(args);
} else {
console.error(
- "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates> [options]",
+ "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates | validate-patch-risk-assessment> [options]",
);
process.exitCode = 2;
}
diff --git a/plugins/codex-security/mcp-app/package.json b/plugins/codex-security/mcp-app/package.json
index 061bd21db..b8702ce72 100644
--- a/plugins/codex-security/mcp-app/package.json
+++ b/plugins/codex-security/mcp-app/package.json
@@ -12,6 +12,7 @@
"dependencies": {
"@modelcontextprotocol/sdk": "^1.31.0",
"@openai/codex-sdk": "0.162.0-alpha.16",
+ "ajv": "8.20.0",
"smol-toml": "1.9.0",
"zod": "^4.6.5"
},
diff --git a/plugins/codex-security/mcp-app/pnpm-lock.yaml b/plugins/codex-security/mcp-app/pnpm-lock.yaml
index 70e7e37d7..ab97340eb 100644
--- a/plugins/codex-security/mcp-app/pnpm-lock.yaml
+++ b/plugins/codex-security/mcp-app/pnpm-lock.yaml
@@ -14,6 +14,9 @@ importers:
'@openai/codex-sdk':
specifier: 0.162.0-alpha.16
version: 0.162.0-alpha.16
+ ajv:
+ specifier: 8.20.0
+ version: 8.20.0
smol-toml:
specifier: 1.9.0
version: 1.9.0
diff --git a/plugins/codex-security/mcp-app/src/helpers/helper-files.ts b/plugins/codex-security/mcp-app/src/helpers/helper-files.ts
new file mode 100644
index 000000000..ce6b4fff9
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/helpers/helper-files.ts
@@ -0,0 +1,39 @@
+import { readFileSync } from "node:fs";
+import { windowsBinding } from "../native";
+import { widePath, windowsFileSystem } from "../../../native/windows-files.mjs";
+import { encodePosixPath } from "./posix-path";
+import { escapeControls } from "./json";
+import { parsedPath } from "./resolve-security-md";
+
+export function filesystemErrorMessage(error: unknown): string {
+ const message = error instanceof Error ? error.message : String(error);
+ // Keep terminal controls in filesystem diagnostics escaped.
+ return error instanceof Error && ("errno" in error || "winerror" in error)
+ ? escapeControls(message)
+ : message;
+}
+
+export function normalizePath(path: string): string {
+ if (process.platform === "win32") return parsedPath(path);
+ const prefix =
+ path.startsWith("//") && !path.startsWith("///")
+ ? "//"
+ : path.startsWith("/")
+ ? "/"
+ : "";
+ // Remove empty/dot components without changing symlink-sensitive "..".
+ return (
+ prefix +
+ path
+ .split("/")
+ .filter((part) => part && part !== ".")
+ .join("/") || "."
+ );
+}
+
+export function readFile(path: string | number): Buffer {
+ if (typeof path === "number") return readFileSync(path);
+ return process.platform === "win32"
+ ? windowsFileSystem(windowsBinding()).readFile(widePath(path))
+ : readFileSync(encodePosixPath(path));
+}
diff --git a/plugins/codex-security/mcp-app/src/helpers/json.ts b/plugins/codex-security/mcp-app/src/helpers/json.ts
new file mode 100644
index 000000000..2e920ba73
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/helpers/json.ts
@@ -0,0 +1,48 @@
+export function object(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && !Array.isArray(value);
+}
+
+export function escapeControls(text: string): string {
+ return text.replace(/[\p{Cc}\p{Cf}]/gu, (character) => {
+ const point = character.codePointAt(0)!;
+ return point > 0xffff
+ ? `\\u{${point.toString(16)}}`
+ : `\\u${point.toString(16).padStart(4, "0")}`;
+ });
+}
+
+export function formatDiagnostic(value: unknown): string {
+ return escapeControls(JSON.stringify(value));
+}
+
+export function parseJson(source: string): unknown {
+ try {
+ // Keep large artifact integers exact when a document is read and rewritten.
+ return JSON.parse(
+ source,
+ (_key, value: unknown, context?: { source?: string }) =>
+ typeof value === "number" &&
+ !Number.isSafeInteger(value) &&
+ /^-?[0-9]+$/u.test(context?.source ?? "")
+ ? BigInt(context!.source!)
+ : value,
+ );
+ } catch (error) {
+ if (error instanceof SyntaxError)
+ error.message = escapeControls(error.message);
+ throw error;
+ }
+}
+
+export function stringifyJson(value: unknown): string {
+ return JSON.stringify(
+ value,
+ (_key, item: unknown) =>
+ typeof item === "bigint"
+ ? (JSON as typeof JSON & { rawJSON(text: string): unknown }).rawJSON(
+ String(item),
+ )
+ : item,
+ 2,
+ );
+}
diff --git a/plugins/codex-security/mcp-app/src/helpers/validate-patch-risk-assessment.ts b/plugins/codex-security/mcp-app/src/helpers/validate-patch-risk-assessment.ts
new file mode 100644
index 000000000..230029b3e
--- /dev/null
+++ b/plugins/codex-security/mcp-app/src/helpers/validate-patch-risk-assessment.ts
@@ -0,0 +1,187 @@
+import { parseArgs } from "node:util";
+import Ajv2020 from "ajv/dist/2020.js";
+import { decodeUtf8 } from "./utf8";
+import assessmentSchema from "../../../schemas/patch-risk-assessment.schema.json";
+import {
+ filesystemErrorMessage,
+ normalizePath,
+ readFile,
+} from "./helper-files";
+import { decodePosixBytes } from "./posix-path";
+import { object, parseJson } from "./json";
+
+interface Assessment {
+ recommendation: "merge" | "revise" | "no_op" | "block" | "hold_for_evidence";
+ workflowLabel: string;
+ impact: { rating: string };
+ regressionLikelihood: { rating: string };
+ regressionProtection: { rating: string; exactHeadChecksPassed: boolean };
+ recoverability: { rating: string };
+ confidence: { rating: string };
+ applicability: { status: string };
+ statusQuoRisk: { rating: string };
+ affectedRuntimeRoots: string[];
+ autoMergeExclusions: string[];
+ materialBoundaries: { result: string }[];
+ validation: { status: string }[];
+ unknowns: { decisionCritical: boolean }[];
+ evidencePlan: unknown[];
+}
+
+const ajv = new Ajv2020({ strict: false });
+const assessmentValidator = ajv.compile(assessmentSchema);
+
+const nonApplicable = new Set([
+ "no_live_effect",
+ "wrong_owner",
+ "duplicate",
+ "superseded",
+]);
+
+export function validatePatchRiskAssessment(value: unknown): string[] {
+ if (!object(value)) return ["assessment must be a JSON object"];
+ if (!assessmentValidator(value))
+ return [
+ ajv.errorsText(assessmentValidator.errors, {
+ dataVar: "patch-risk-assessment.schema",
+ }),
+ ];
+ const assessment = value as unknown as Assessment;
+ const {
+ recommendation,
+ workflowLabel,
+ unknowns,
+ evidencePlan,
+ materialBoundaries: boundaries,
+ } = assessment;
+ const applicability = assessment.applicability.status;
+ const affirmativeFailure =
+ assessment.regressionLikelihood.rating === "critical" ||
+ boundaries.some((item) => item.result === "contradicted") ||
+ assessment.validation.some((item) => item.status === "failed");
+ const errors: string[] = [];
+
+ if (recommendation === "merge") {
+ if (
+ !["auto_merge_candidate", "human_review_required"].includes(workflowLabel)
+ )
+ errors.push(
+ "merge requires an auto-merge or human-review workflow label",
+ );
+ if (applicability !== "confirmed")
+ errors.push("merge requires confirmed applicability");
+ if (unknowns.some((item) => item.decisionCritical))
+ errors.push("merge cannot retain a decision-critical unknown");
+ if (boundaries.some((item) => item.result !== "supported"))
+ errors.push("merge requires every material boundary to be supported");
+ if (assessment.validation.some((item) => item.status === "failed"))
+ errors.push("merge cannot retain a failed validation");
+ if (evidencePlan.length)
+ errors.push("merge cannot retain an evidence plan");
+ } else if (workflowLabel !== recommendation) {
+ errors.push("non-merge workflow label must match the recommendation");
+ }
+
+ if (recommendation === "hold_for_evidence") {
+ if (!unknowns.some((item) => item.decisionCritical))
+ errors.push("hold_for_evidence requires a decision-critical unknown");
+ if (!evidencePlan.length)
+ errors.push("hold_for_evidence requires a bounded evidence plan");
+ if (affirmativeFailure)
+ errors.push("hold_for_evidence cannot defer an established defect");
+ } else if (evidencePlan.length) {
+ errors.push("only hold_for_evidence may include an evidence plan");
+ }
+
+ if (recommendation === "no_op") {
+ if (!nonApplicable.has(applicability))
+ errors.push("no_op requires an established non-applicable disposition");
+ if (unknowns.some((item) => item.decisionCritical))
+ errors.push("no_op cannot retain a decision-critical unknown");
+ } else if (nonApplicable.has(applicability)) {
+ errors.push("an established non-applicable disposition requires no_op");
+ }
+
+ if (["revise", "block"].includes(recommendation) && !affirmativeFailure)
+ errors.push(`${recommendation} requires affirmative failure evidence`);
+
+ if (workflowLabel === "auto_merge_candidate") {
+ const requirements: Record<string, boolean> = {
+ "impact.rating": assessment.impact.rating === "low",
+ "regressionLikelihood.rating":
+ assessment.regressionLikelihood.rating === "low",
+ "regressionProtection.rating":
+ assessment.regressionProtection.rating === "strong",
+ "regressionProtection.exactHeadChecksPassed":
+ assessment.regressionProtection.exactHeadChecksPassed,
+ "recoverability.rating": assessment.recoverability.rating === "easy",
+ "confidence.rating": assessment.confidence.rating === "high",
+ "applicability.status": applicability === "confirmed",
+ affectedRuntimeRoots: assessment.affectedRuntimeRoots.length > 0,
+ "statusQuoRisk.rating": assessment.statusQuoRisk.rating !== "unknown",
+ autoMergeExclusions: assessment.autoMergeExclusions.length === 0,
+ unknowns: unknowns.length === 0,
+ validation: assessment.validation.every(
+ (item) => item.status === "passed",
+ ),
+ };
+ for (const [field, passed] of Object.entries(requirements))
+ if (!passed) errors.push(`auto_merge_candidate gate failed: ${field}`);
+ }
+ return errors;
+}
+
+function readAssessment(path: string): unknown {
+ let contents: Buffer;
+ try {
+ contents = readFile(path === "-" ? 0 : normalizePath(path));
+ } catch (error) {
+ throw new Error(`cannot read assessment: ${filesystemErrorMessage(error)}`);
+ }
+ const text =
+ path === "-"
+ ? decodePosixBytes(contents)
+ : decodeUtf8(contents).replace(/\r\n?/gu, "\n");
+ try {
+ return parseJson(text);
+ } catch (error) {
+ if (error instanceof SyntaxError)
+ throw new Error(`cannot read assessment: ${error.message}`);
+ throw error;
+ }
+}
+
+export function validatePatchRiskAssessmentCommand(args: string[]): number {
+ const usage =
+ "usage: launch_codex_security_mcp[.cmd] --helper validate-patch-risk-assessment [-h] assessment";
+ let path: string;
+ try {
+ const { values, positionals } = parseArgs({
+ args,
+ allowPositionals: true,
+ options: { help: { type: "boolean", short: "h" } },
+ });
+ if (values.help) {
+ console.log(
+ `${usage}\n\nValidate a patch-risk assessment.\n\npositional arguments:\n assessment Assessment JSON path, or - for stdin.\n\noptions:\n -h, --help show this help message and exit`,
+ );
+ return 0;
+ }
+ if (positionals.length !== 1)
+ throw new Error("exactly one assessment path or - for stdin is required");
+ path = positionals[0]!;
+ } catch (error) {
+ console.error(
+ `${usage}\n${error instanceof Error ? error.message : String(error)}`,
+ );
+ return 2;
+ }
+ try {
+ const errors = validatePatchRiskAssessment(readAssessment(path));
+ for (const error of errors) console.error(error);
+ return errors.length ? 1 : 0;
+ } catch (error) {
+ console.error(error instanceof Error ? error.message : String(error));
+ return 1;
+ }
+}
diff --git a/plugins/codex-security/native/README.md b/plugins/codex-security/native/README.md
index c60b3b5d9..0a325cd3e 100644
--- a/plugins/codex-security/native/README.md
+++ b/plugins/codex-security/native/README.md
@@ -33,7 +33,7 @@ Windows uses `windows-binding.mts` and the same Rust crate. `WindowsHandle` owns
The binding exposes synchronous file and recursive directory creation, attributes and reparse tags, identity and final/opened names, read/write, and exact-handle rename and deletion. Rust's `File` supplies ordinary I/O. Calls return numeric Windows errors, including 6 for closed handles. Buffer ranges and path encoding are checked before use. Overlapped handles are unsupported because pending operations could retain native buffers beyond the call. Path authorization, ancestor traversal, and reparse-point policy remain the caller's responsibility.
-Four additional operations preserve Windows strings at the Node boundary. `windowsArguments` returns the complete OS argument vector, including the executable and Node options, using Rust's CRT-compatible parser. `windowsEnvironment` reads one wide environment name and distinguishes an absent value (`null`) from an empty buffer. `windowsAbsolutePath` resolves against the native current directory and drive directories without requiring the destination to exist. `windowsDirectoryEntries` uses `std::fs::read_dir` and cached `DirEntry::file_type()` values without opening each child; names remain UTF-16LE, and construction or iteration failures return their numeric Windows error and an empty array. Directory symlinks and junctions have both directory and symbolic-link flags. The typed adapter exposes this enumerator through `entriesWithTypes`, which `resolve-security-md --list` uses on Windows.
+Four additional operations preserve Windows strings at the Node boundary. `windowsArguments` returns the complete OS argument vector, including the executable and Node options, using Rust's CRT-compatible parser. `windowsEnvironment` reads one wide environment name and distinguishes an absent value (`null`) from an empty buffer. `windowsAbsolutePath` resolves against the native current directory and drive directories without requiring the destination to exist. `windowsDirectoryEntries` uses `std::fs::read_dir` and cached `DirEntry::file_type()` values without opening each child; names remain UTF-16LE, and construction or iteration failures return their numeric Windows error and an empty array. Directory symlinks and junctions have both directory and symbolic-link flags. The typed adapter exposes this enumerator through `entriesWithTypes`, which `resolve-security-md --list` uses on Windows. Assessment validation shares the typed wide-path reader.
`windows-files.mts` uses native absolute and final paths, preserving raw UTF-16 and the returned device prefix. Non-strict `realpath` resolves the nearest existing ancestor of a missing path; dangling links and other access errors remain errors. Callers must check containment independently. It follows native Windows path behavior rather than emulating Python's special cases for device names, whitespace, verbatim dot segments, and prefix removal. `stat(path, false)` retains exact symbolic-link and reparse-point metadata so callers can reject junction traversal independently of the enumerator's link label. The SDK's public runtime floor remains Node 22.13.0. Node 20.0.0 is an additional native-foundation compatibility proof; it does not change the SDK engine requirement.
diff --git a/plugins/codex-security/native/examples/windows-wide-launcher.rs b/plugins/codex-security/native/examples/windows-wide-launcher.rs
index 1392cce38..672144e8b 100644
--- a/plugins/codex-security/native/examples/windows-wide-launcher.rs
+++ b/plugins/codex-security/native/examples/windows-wide-launcher.rs
@@ -157,15 +157,16 @@ fn main() -> std::io::Result<()> {
let repo = root.join(&cwds[0]).join(&repos[0]);
let output_name = raw("out-", 0xdfff);
let output = repo.join(&output_name);
- let invoke = |args: &[PathBuf]| {
+ let invoke = |command: &str, args: &[PathBuf]| {
Command::new(&node)
.arg(&script)
- .args(["--helper", "resolve-security-md"])
+ .args(["--helper", command])
.args(args)
.current_dir(&repo)
.env("USERPROFILE", &repo)
.output()
};
+ let policy = |args: &[PathBuf]| invoke("resolve-security-md", args);
for (repo_arg, scope_arg, output_arg) in [
(repo.clone(), PathBuf::from(&scopes[0]), output.clone()),
(
@@ -179,7 +180,7 @@ fn main() -> std::io::Result<()> {
PathBuf::from(&output_name),
),
] {
- let child = invoke(&[
+ let child = policy(&[
"--repo".into(),
repo_arg,
"--scope".into(),
@@ -206,7 +207,7 @@ fn main() -> std::io::Result<()> {
fs::remove_file(&output)?;
}
for scope in [PathBuf::from("."), PathBuf::from(&scopes[0]).join("..")] {
- let child = invoke(&["--repo".into(), repo.clone(), "--scope".into(), scope])?;
+ let child = policy(&["--repo".into(), repo.clone(), "--scope".into(), scope])?;
let expected = b"## SECURITY.md source: \"SECURITY.md\"\n\nroot raw\n";
if !child.status.success() || !child.stderr.is_empty() || child.stdout != expected {
return Err(io::Error::other(
@@ -214,7 +215,7 @@ fn main() -> std::io::Result<()> {
));
}
}
- let listing = invoke(&["--repo".into(), "~".into(), "--list".into()])?;
+ let listing = policy(&["--repo".into(), "~".into(), "--list".into()])?;
let expected =
b"[\"SECURITY.md\", \"scope-\\udfff/SECURITY.md\", \"scope-\\ufffd/SECURITY.md\"]\n";
if !listing.status.success() || !listing.stderr.is_empty() || listing.stdout != expected {
@@ -236,7 +237,7 @@ fn main() -> std::io::Result<()> {
let sibling_policy = sibling.join("SECURITY.md");
fs::write(&sibling_policy, "sibling policy\n")?;
let verify_outside_root = |scope: &Path| -> io::Result<()> {
- let result = invoke(&[
+ let result = policy(&[
"--repo".into(),
identity_root.clone(),
"--scope".into(),
@@ -292,20 +293,20 @@ fn main() -> std::io::Result<()> {
"\n",
);
let candidate = |repo_arg: &Path, input: &Path, scope: &Path, output: &Path| {
- Command::new(&node)
- .arg(&script)
- .args(["--helper", "normalize-candidates", "--repo-root"])
- .arg(repo_arg)
- .arg("--input")
- .arg(input)
- .arg("--in-scope-files")
- .arg(scope)
- .arg("--out")
- .arg(output)
- .arg("--allow-missing-in-scope")
- .current_dir(&repo)
- .env("USERPROFILE", &repo)
- .output()
+ invoke(
+ "normalize-candidates",
+ &[
+ "--repo-root".into(),
+ repo_arg.into(),
+ "--input".into(),
+ input.into(),
+ "--in-scope-files".into(),
+ scope.into(),
+ "--out".into(),
+ output.into(),
+ "--allow-missing-in-scope".into(),
+ ],
+ )
};
fs::write(&output, "previous output")?;
for (index, prefix) in [repo.clone(), PathBuf::from("~"), PathBuf::from(".")]
@@ -362,8 +363,47 @@ fn main() -> std::io::Result<()> {
));
}
}
+ let assessment_name = raw("assessment-", 0xd800);
+ let assessment_path = repo.join(&assessment_name);
+ let replacement_assessment = repo.join(raw("assessment-", 0xfffd));
+ let assessment = r#"{
+ "schemaVersion":1,
+ "patch":{"repository":"example/project","sourceType":"patch_file","base":"base","head":"head","changedFiles":["src/example.ts"],"sha256":"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"},
+ "recommendation":"no_op","workflowLabel":"no_op",
+ "impact":{"rating":"low","rationale":"No active path changes."},
+ "regressionLikelihood":{"rating":"low","rationale":"No live effect."},
+ "regressionProtection":{"rating":"strong","rationale":"Fixture validated.","exactHeadChecksPassed":true},
+ "recoverability":{"rating":"easy","rationale":"Local change."},
+ "confidence":{"rating":"high","rationale":"Known fixture."},
+ "applicability":{"status":"no_live_effect","rationale":"Synthetic input."},
+ "statusQuoRisk":{"rating":"low","rationale":"No live effect."},
+ "autoMergeExclusions":[],"affectedRuntimeRoots":[],"materialBoundaries":[],
+ "validation":[{"name":"fixture","status":"passed","protects":"Validator input."}],
+ "unknowns":[],"evidencePlan":[]
+ }"#;
+ fs::write(&assessment_path, assessment)?;
+ fs::write(&replacement_assessment, "replacement assessment sentinel")?;
+ for input in [assessment_path.clone(), PathBuf::from(&assessment_name)] {
+ let child = Command::new(&node)
+ .arg(&script)
+ .args(["--helper", "validate-patch-risk-assessment"])
+ .arg(input)
+ .current_dir(&repo)
+ .output()?;
+ if !child.status.success() || !child.stdout.is_empty() || !child.stderr.is_empty() {
+ return Err(io::Error::other(format!(
+ "Wide assessment helper failed: {}",
+ String::from_utf8_lossy(&child.stderr)
+ )));
+ }
+ }
+ if fs::read(&assessment_path)? != assessment.as_bytes()
+ || fs::read(&replacement_assessment)? != b"replacement assessment sentinel"
+ {
+ return Err(io::Error::other("Assessment validation changed its input"));
+ }
println!(
- "{{\"policyHelperRawPaths\":true,\"candidateHelperRawPaths\":true,\"directoryIdentity\":true,\"policySymlinkBoundary\":{symlinks}}}"
+ "{{\"policyHelperRawPaths\":true,\"candidateHelperRawPaths\":true,\"assessmentHelperRawPaths\":true,\"directoryIdentity\":true,\"policySymlinkBoundary\":{symlinks}}}"
);
Ok(())
}
diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.json
index 5ed82be3c..49920db1b 100644
--- a/plugins/codex-security/plugin-files.json
+++ b/plugins/codex-security/plugin-files.json
@@ -108,7 +108,6 @@
"skills/assess-patch-risk/SKILL.md",
"skills/assess-patch-risk/agents/openai.yaml",
"skills/assess-patch-risk/references/risk-rubric.md",
- "skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py",
"skills/deep-security-scan/SKILL.md",
"skills/deep-security-scan/agents/openai.yaml",
"skills/define-security-policy/SKILL.md",
diff --git a/plugins/codex-security/skills/assess-patch-risk/SKILL.md b/plugins/codex-security/skills/assess-patch-risk/SKILL.md
index ad6040afe..14dbee6ce 100644
--- a/plugins/codex-security/skills/assess-patch-risk/SKILL.md
+++ b/plugins/codex-security/skills/assess-patch-risk/SKILL.md
@@ -62,15 +62,26 @@ Return both a concise Markdown report and a JSON object conforming to [`../../sc
7. top risk drivers, protective factors, and status-quo risk; and
8. unknowns plus the bounded evidence plan when held.
-This skill lives at `<plugin-root>/skills/assess-patch-risk/SKILL.md`, so `<plugin-root>` is two directories up. Resolve `<python_command>` to the configured Python interpreter (`"$PYTHON"` in POSIX shells or `& "$env:PYTHON"` in PowerShell), otherwise use `python` on Windows and `python3` on Unix-like hosts.
+This skill lives at `<plugin-root>/skills/assess-patch-risk/SKILL.md`, so `<plugin-root>` is two directories up.
Before returning the result, validate the JSON from any working directory with:
```text
-<python_command> <plugin-root>/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py <assessment.json>
+<plugin-root>/scripts/launch_codex_security_mcp --helper validate-patch-risk-assessment <assessment.json>
```
-Pass `-` as `<assessment.json>` to read the assessment from standard input without creating a file.
+On Windows, use this PowerShell invocation. Replace the placeholders inside the single quotes with literal paths, doubling any single quote in a path:
+
+```powershell
+$env:patchRiskPluginRoot = Convert-Path -LiteralPath '<plugin-root>' -ErrorAction Stop
+$env:patchRiskAssessment = '<assessment.json>'
+if ($env:patchRiskAssessment -ne '-') {
+ $env:patchRiskAssessment = Convert-Path -LiteralPath $env:patchRiskAssessment -ErrorAction Stop
+}
+cmd.exe /d /v:off /s /c '""%patchRiskPluginRoot%\scripts\launch_codex_security_mcp.cmd" --helper validate-patch-risk-assessment "%patchRiskAssessment%""'
+```
+
+These two environment variables are temporary values in the calling shell, not application settings. Resolve paths against PowerShell's current location before CMD starts, including when that location is a UNC share. Missing paths stop the invocation with PowerShell's path error. CMD expands the references once, preserving literal `%` and `!` in the paths. Pass `-` as `<assessment.json>` to read the assessment from standard input without creating a file. The validator uses the bundled Node runtime helper and does not require Python.
Correct structural or invariant errors by revisiting the evidence; never change a recommendation merely to make validation pass. Return the validated JSON in the response. Write it to disk only when the caller requests an artifact, and keep every assessment-created file outside the subject checkout and its Git directories.
diff --git a/plugins/codex-security/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py b/plugins/codex-security/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py
deleted file mode 100644
index cc4b67195..000000000
--- a/plugins/codex-security/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py
+++ /dev/null
@@ -1,163 +0,0 @@
-#!/usr/bin/env python3
-from __future__ import annotations
-
-import argparse
-import importlib.util
-import json
-import sys
-from pathlib import Path
-from types import ModuleType
-from typing import Any
-
-PLUGIN_ROOT = Path(__file__).resolve().parents[3]
-SCHEMA_PATH = PLUGIN_ROOT / "schemas" / "patch-risk-assessment.schema.json"
-NON_APPLICABLE = {"no_live_effect", "wrong_owner", "duplicate", "superseded"}
-
-
-def load_scan_contract_validator() -> ModuleType:
- script = PLUGIN_ROOT / "scripts" / "finalize_scan_contract.py"
- spec = importlib.util.spec_from_file_location("codex_security_scan_contract", script)
- if spec is None or spec.loader is None:
- raise RuntimeError(f"could not load scan contract validator: {script}")
- module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
- return module
-
-
-SCAN_CONTRACT = load_scan_contract_validator()
-
-
-def parse_args() -> argparse.Namespace:
- parser = argparse.ArgumentParser(description="Validate a patch-risk assessment.")
- parser.add_argument("assessment", help="Assessment JSON path, or - for stdin.")
- return parser.parse_args()
-
-
-def reject_duplicate_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
- value: dict[str, Any] = {}
- for key, item in pairs:
- if key in value:
- raise ValueError(f"duplicate JSON object key: {key}")
- value[key] = item
- return value
-
-
-def read_object(path: str) -> dict[str, Any]:
- try:
- text = sys.stdin.read() if path == "-" else Path(path).read_text(encoding="utf-8")
- value = json.loads(text, object_pairs_hook=reject_duplicate_keys)
- except (OSError, json.JSONDecodeError) as error:
- raise ValueError(f"cannot read assessment: {error}") from error
- if not isinstance(value, dict):
- raise ValueError("assessment must be a JSON object")
- return value
-
-
-def schema_errors(value: dict[str, Any]) -> list[str]:
- try:
- SCAN_CONTRACT.validate_against_schema(value, SCHEMA_PATH)
- except (OSError, ValueError, RecursionError) as error:
- return [str(error)]
- return []
-
-
-def semantic_errors(value: dict[str, Any]) -> list[str]:
- recommendation = value["recommendation"]
- workflow_label = value["workflowLabel"]
- unknowns = value["unknowns"]
- evidence_plan = value["evidencePlan"]
- boundaries = value["materialBoundaries"]
- applicability_status = value["applicability"]["status"]
- affirmative_failure = (
- value["regressionLikelihood"]["rating"] == "critical"
- or any(item["result"] == "contradicted" for item in boundaries)
- or any(item["status"] == "failed" for item in value["validation"])
- )
- errors: list[str] = []
-
- if recommendation == "merge":
- if workflow_label not in {"auto_merge_candidate", "human_review_required"}:
- errors.append("merge requires an auto-merge or human-review workflow label")
- if value["applicability"]["status"] != "confirmed":
- errors.append("merge requires confirmed applicability")
- if any(item["decisionCritical"] for item in unknowns):
- errors.append("merge cannot retain a decision-critical unknown")
- if any(item["result"] != "supported" for item in boundaries):
- errors.append("merge requires every material boundary to be supported")
- if any(item["status"] == "failed" for item in value["validation"]):
- errors.append("merge cannot retain a failed validation")
- if evidence_plan:
- errors.append("merge cannot retain an evidence plan")
- elif workflow_label != recommendation:
- errors.append("non-merge workflow label must match the recommendation")
-
- if recommendation == "hold_for_evidence":
- if not any(item["decisionCritical"] for item in unknowns):
- errors.append("hold_for_evidence requires a decision-critical unknown")
- if not evidence_plan:
- errors.append("hold_for_evidence requires a bounded evidence plan")
- if affirmative_failure:
- errors.append("hold_for_evidence cannot defer an established defect")
- elif evidence_plan:
- errors.append("only hold_for_evidence may include an evidence plan")
-
- if recommendation == "no_op":
- if applicability_status not in NON_APPLICABLE:
- errors.append("no_op requires an established non-applicable disposition")
- if any(item["decisionCritical"] for item in unknowns):
- errors.append("no_op cannot retain a decision-critical unknown")
- elif applicability_status in NON_APPLICABLE:
- errors.append("an established non-applicable disposition requires no_op")
-
- if recommendation in {"revise", "block"}:
- if not affirmative_failure:
- errors.append(f"{recommendation} requires affirmative failure evidence")
-
- if workflow_label == "auto_merge_candidate":
- auto_merge_requirements = {
- "impact.rating": value["impact"]["rating"] == "low",
- "regressionLikelihood.rating": value["regressionLikelihood"]["rating"] == "low",
- "regressionProtection.rating": value["regressionProtection"]["rating"] == "strong",
- "regressionProtection.exactHeadChecksPassed": value["regressionProtection"][
- "exactHeadChecksPassed"
- ],
- "recoverability.rating": value["recoverability"]["rating"] == "easy",
- "confidence.rating": value["confidence"]["rating"] == "high",
- "applicability.status": value["applicability"]["status"] == "confirmed",
- "affectedRuntimeRoots": bool(value["affectedRuntimeRoots"]),
- "statusQuoRisk.rating": value["statusQuoRisk"]["rating"] != "unknown",
- "autoMergeExclusions": not value["autoMergeExclusions"],
- "unknowns": not unknowns,
- "validation": all(item["status"] == "passed" for item in value["validation"]),
- }
- for field, passed in auto_merge_requirements.items():
- if not passed:
- errors.append(f"auto_merge_candidate gate failed: {field}")
-
- return errors
-
-
-def validate(value: dict[str, Any]) -> list[str]:
- errors = schema_errors(value)
- if errors:
- return errors
- return semantic_errors(value)
-
-
-def main() -> int:
- args = parse_args()
- try:
- value = read_object(args.assessment)
- errors = validate(value)
- except ValueError as error:
- print(error, file=sys.stderr)
- return 1
- if errors:
- for error in errors:
- print(error, file=sys.stderr)
- return 1
- return 0
-
-
-if __name__ == "__main__":
- raise SystemExit(main())
diff --git a/plugins/codex-security/tests/test_patch_risk_contract.py b/plugins/codex-security/tests/test_patch_risk_contract.py
deleted file mode 100644
index 88b301e4d..000000000
--- a/plugins/codex-security/tests/test_patch_risk_contract.py
+++ /dev/null
@@ -1,233 +0,0 @@
-from __future__ import annotations
-
-import copy
-import json
-import subprocess
-import sys
-from pathlib import Path
-from typing import Any
-
-from jsonschema import Draft202012Validator
-
-PLUGIN_ROOT = Path(__file__).resolve().parents[1]
-SCHEMA_PATH = PLUGIN_ROOT / "schemas" / "patch-risk-assessment.schema.json"
-VALIDATOR_PATH = (
- PLUGIN_ROOT / "skills" / "assess-patch-risk" / "scripts" / "validate_patch_risk_assessment.py"
-)
-
-
-def assessment() -> dict[str, Any]:
- return {
- "schemaVersion": 1,
- "patch": {
- "repository": "example/project",
- "sourceType": "pull_request_diff",
- "base": "a" * 40,
- "head": "b" * 40,
- "changedFiles": ["src/request.ts"],
- "sha256": "c" * 64,
- },
- "recommendation": "merge",
- "workflowLabel": "human_review_required",
- "impact": {"rating": "moderate", "rationale": "A bounded caller can fail."},
- "regressionLikelihood": {
- "rating": "low",
- "rationale": "The changed path and its caller are covered.",
- },
- "regressionProtection": {
- "rating": "strong",
- "rationale": "Focused and integration checks passed at the exact head.",
- "exactHeadChecksPassed": True,
- },
- "recoverability": {"rating": "easy", "rationale": "A revert is isolated."},
- "confidence": {"rating": "high", "rationale": "Runtime callers are known."},
- "applicability": {"status": "confirmed", "rationale": "The path is deployed."},
- "statusQuoRisk": {"rating": "moderate", "rationale": "The defect remains."},
- "autoMergeExclusions": [],
- "affectedRuntimeRoots": ["service.request"],
- "materialBoundaries": [
- {
- "id": "request-contract",
- "invariant": "Supported requests retain their existing response contract.",
- "runtimeRoot": "service.request",
- "counterexample": "A supported request takes the changed branch.",
- "legitimateControl": "A supported request takes the unchanged branch.",
- "result": "supported",
- }
- ],
- "validation": [
- {
- "name": "focused request tests",
- "status": "passed",
- "protects": "Changed behavior through the production caller.",
- }
- ],
- "unknowns": [],
- "evidencePlan": [],
- }
-
-
-def validate(tmp_path: Path, payload: dict[str, Any]) -> subprocess.CompletedProcess[str]:
- assessment_path = tmp_path / "assessment.json"
- assessment_path.write_text(json.dumps(payload), encoding="utf-8")
- return subprocess.run(
- [sys.executable, str(VALIDATOR_PATH), str(assessment_path)],
- cwd=PLUGIN_ROOT,
- capture_output=True,
- text=True,
- check=False,
- )
-
-
-def test_schema_is_valid_draft_2020_12() -> None:
- schema = json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
- Draft202012Validator.check_schema(schema)
-
-
-def test_supported_human_review_merge_is_valid(tmp_path: Path) -> None:
- assert validate(tmp_path, assessment()).returncode == 0
-
-
-def test_strict_low_risk_assessment_can_be_auto_merge_candidate(tmp_path: Path) -> None:
- payload = assessment()
- payload["workflowLabel"] = "auto_merge_candidate"
- payload["impact"]["rating"] = "low"
-
- assert validate(tmp_path, payload).returncode == 0
-
-
-def test_auto_merge_rejects_non_low_impact(tmp_path: Path) -> None:
- payload = assessment()
- payload["workflowLabel"] = "auto_merge_candidate"
-
- assert validate(tmp_path, payload).returncode != 0
-
-
-def test_auto_merge_rejects_materially_excluded_change(tmp_path: Path) -> None:
- payload = assessment()
- payload["workflowLabel"] = "auto_merge_candidate"
- payload["impact"]["rating"] = "low"
- payload["autoMergeExclusions"] = ["public_contract"]
-
- assert validate(tmp_path, payload).returncode != 0
-
-
-def test_merge_rejects_decision_critical_unknown(tmp_path: Path) -> None:
- payload = assessment()
- payload["unknowns"] = [
- {"summary": "Deployment ownership is unresolved.", "decisionCritical": True}
- ]
-
- assert validate(tmp_path, payload).returncode != 0
-
-
-def test_merge_rejects_unknown_applicability(tmp_path: Path) -> None:
- payload = assessment()
- payload["applicability"] = {
- "status": "unknown",
- "rationale": "The supported runtime owner is unresolved.",
- }
-
- result = validate(tmp_path, payload)
-
- assert result.returncode != 0
- assert "merge requires confirmed applicability" in result.stderr
-
-
-def test_auto_merge_requires_affected_runtime_root(tmp_path: Path) -> None:
- payload = assessment()
- payload["workflowLabel"] = "auto_merge_candidate"
- payload["impact"]["rating"] = "low"
- payload["affectedRuntimeRoots"] = []
-
- result = validate(tmp_path, payload)
-
- assert result.returncode != 0
- assert "auto_merge_candidate gate failed: affectedRuntimeRoots" in result.stderr
-
-
-def test_hold_requires_bounded_evidence_plan(tmp_path: Path) -> None:
- payload = assessment()
- payload["recommendation"] = "hold_for_evidence"
- payload["workflowLabel"] = "hold_for_evidence"
- payload["unknowns"] = [
- {"summary": "The rollout target is unavailable.", "decisionCritical": True}
- ]
-
- assert validate(tmp_path, payload).returncode != 0
-
- payload["evidencePlan"] = [
- {
- "question": "Does the changed configuration own the rollout target?",
- "action": "Inspect the checked-in deployment mapping.",
- "outcomes": {
- "supported": "merge",
- "contradicted": "no_op",
- "unavailable": "hold_for_evidence",
- },
- }
- ]
- assert validate(tmp_path, payload).returncode == 0
-
-
-def test_no_op_requires_non_applicable_disposition(tmp_path: Path) -> None:
- payload = assessment()
- payload["recommendation"] = "no_op"
- payload["workflowLabel"] = "no_op"
-
- assert validate(tmp_path, payload).returncode != 0
-
- payload["applicability"] = {
- "status": "superseded",
- "rationale": "A narrower patch already landed.",
- }
- assert validate(tmp_path, payload).returncode == 0
-
-
-def test_no_op_rejects_decision_critical_unknown(tmp_path: Path) -> None:
- payload = assessment()
- payload["recommendation"] = "no_op"
- payload["workflowLabel"] = "no_op"
- payload["applicability"] = {
- "status": "superseded",
- "rationale": "A sibling patch may cover the affected runtime.",
- }
- payload["unknowns"] = [
- {
- "summary": "Whether the sibling covers the runtime is unresolved.",
- "decisionCritical": True,
- }
- ]
-
- result = validate(tmp_path, payload)
-
- assert result.returncode != 0
- assert "no_op cannot retain a decision-critical unknown" in result.stderr
-
-
-def test_raw_worktree_is_not_a_supported_patch_source(tmp_path: Path) -> None:
- payload = assessment()
- payload["patch"]["sourceType"] = "raw_worktree"
-
- assert validate(tmp_path, payload).returncode != 0
-
-
-def test_non_merge_recommendation_requires_matching_workflow_label(tmp_path: Path) -> None:
- payload = assessment()
- payload["recommendation"] = "revise"
- payload["validation"][0]["status"] = "failed"
-
- assert validate(tmp_path, payload).returncode != 0
-
- payload["workflowLabel"] = "revise"
- assert validate(tmp_path, payload).returncode == 0
-
-
-def test_validator_does_not_modify_input(tmp_path: Path) -> None:
- payload = assessment()
- original = copy.deepcopy(payload)
-
- result = validate(tmp_path, payload)
-
- assert result.returncode == 0
- assert payload == original
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 70f4d749c..4933f69ef 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -7488,7 +7488,7 @@ async function runSkill(
),
"Assess the immutable patch artifact described by this JSON object:",
JSON.stringify(options.patchArtifact),
- `Validate the JSON assessment with ${JSON.stringify(join(plugin, "skills", skill, "scripts", "validate_patch_risk_assessment.py"))} as required by the skill.`,
+ `For the skill's platform-specific validation command, <plugin-root> is this literal path (JSON string): ${JSON.stringify(plugin)}. Validate the JSON assessment as required by the skill.`,
"Wrap only the concise Markdown report between these exact marker lines:",
PATCH_RISK_SUMMARY_START,
PATCH_RISK_SUMMARY_END,
diff --git a/sdk/typescript/tests-ts/cli-patch.test.ts b/sdk/typescript/tests-ts/cli-patch.test.ts
index 37d3cb679..0213628bb 100644
--- a/sdk/typescript/tests-ts/cli-patch.test.ts
+++ b/sdk/typescript/tests-ts/cli-patch.test.ts
@@ -1367,6 +1367,14 @@ describe("scan and patch workflow", () => {
expect(output.appServer?.prompt).toContain(
"<!-- codex-security:patch-risk-summary:end -->",
);
+ expect(output.appServer?.prompt).toContain(
+ "--helper validate-patch-risk-assessment <assessment.json>",
+ );
+ expect(output.appServer?.prompt).toContain(
+ process.platform === "win32"
+ ? "launch_codex_security_mcp.cmd"
+ : "launch_codex_security_mcp",
+ );
const artifact = JSON.parse(
output
.appServer!.prompt.split("\n")
diff --git a/sdk/typescript/tests-ts/mcp-launcher.test.ts b/sdk/typescript/tests-ts/mcp-launcher.test.ts
index 317485423..73f44d673 100644
--- a/sdk/typescript/tests-ts/mcp-launcher.test.ts
+++ b/sdk/typescript/tests-ts/mcp-launcher.test.ts
@@ -13,20 +13,7 @@ import { tmpdir } from "node:os";
import { dirname, join, parse } from "node:path";
import { expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
-
-async function removeTemporaryDirectory(path: string): Promise<void> {
- // Bun 1.3.14 ignores fs.rm's retry options.
- for (let attempt = 0; ; attempt++) {
- try {
- await rm(path, { recursive: true, force: true });
- return;
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code !== "EBUSY" || attempt === 10)
- throw error;
- await Bun.sleep(100 * (attempt + 1));
- }
- }
-}
+import { removeTemporaryDirectory } from "./support/temporary-directories.js";
test.skipIf(process.platform !== "win32")(
"launches the PATH Node executable independently of command extensions and the caller directory",
diff --git a/sdk/typescript/tests-ts/patch-risk-contract.test.ts b/sdk/typescript/tests-ts/patch-risk-contract.test.ts
index 02c0102c7..b0d6aaaa3 100644
--- a/sdk/typescript/tests-ts/patch-risk-contract.test.ts
+++ b/sdk/typescript/tests-ts/patch-risk-contract.test.ts
@@ -1,11 +1,25 @@
-import { pythonExecutable } from "./support/python.js";
import { spawnSync } from "node:child_process";
-import { mkdtemp, readFile, rm } from "node:fs/promises";
+import {
+ copyFile,
+ cp,
+ mkdir,
+ mkdtemp,
+ readFile,
+ rm,
+ symlink,
+ writeFile,
+} from "node:fs/promises";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join, relative } from "node:path";
import Ajv2020 from "ajv/dist/2020.js";
-import { describe, expect, test } from "bun:test";
+import { afterEach, describe, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
+import { runCommand } from "./support/shell.js";
+import { removeTemporaryDirectory } from "./support/temporary-directories.js";
+import {
+ hasWindowsLoopbackShare,
+ windowsLoopbackPath,
+} from "./windows-helper-location.js";
interface Assessment {
[key: string]: unknown;
@@ -62,14 +76,15 @@ const schemaPath = join(
"schemas",
"patch-risk-assessment.schema.json",
);
-const validatorPath = join(
- PLUGIN_ROOT,
- "skills",
- "assess-patch-risk",
- "scripts",
- "validate_patch_risk_assessment.py",
-);
-const python = pythonExecutable();
+const node = Bun.which("node")!;
+const helper = join(PLUGIN_ROOT, "mcp", "helpers.mjs");
+const powershellDirectories: string[] = [];
+
+afterEach(async () => {
+ await Promise.all(
+ powershellDirectories.splice(0).map(removeTemporaryDirectory),
+ );
+});
function assessment(): Assessment {
return {
@@ -129,13 +144,13 @@ function assessment(): Assessment {
};
}
-function validateText(input: string, cwd = PLUGIN_ROOT) {
- expect(python).toBeDefined();
- expect(python).not.toBeNull();
- return spawnSync(python!, ["-I", "-B", "-S", validatorPath, "-"], {
+function validateText(input: string | Buffer, cwd = PLUGIN_ROOT, args = ["-"]) {
+ return spawnSync(node, [helper, "validate-patch-risk-assessment", ...args], {
cwd,
encoding: "utf8",
input,
+ env: { ...process.env, PATH: "", PYTHON: join(cwd, "unavailable-python") },
+ maxBuffer: Infinity,
});
}
@@ -143,24 +158,8 @@ function validate(payload: Assessment) {
return validateText(JSON.stringify(payload));
}
-function validateWithSharedSchema(payload: Assessment) {
- expect(python).toBeDefined();
- expect(python).not.toBeNull();
- const program = [
- "import json, pathlib, sys",
- "sys.path.insert(0, sys.argv[1])",
- "import finalize_scan_contract as finalizer",
- "finalizer.validate_against_schema(json.load(sys.stdin), pathlib.Path(sys.argv[2]))",
- ].join("\n");
- return spawnSync(
- python!,
- ["-I", "-B", "-S", "-c", program, join(PLUGIN_ROOT, "scripts"), schemaPath],
- { encoding: "utf8", input: JSON.stringify(payload) },
- );
-}
-
describe("patch risk assessment contract", () => {
- test("resolves the validator from the installed skill", async () => {
+ test("loads the installed helper without Python from another working directory", async () => {
const outside = await mkdtemp(join(tmpdir(), "patch-risk-contract-"));
try {
const result = validateText(JSON.stringify(assessment()), outside);
@@ -188,8 +187,8 @@ describe("patch risk assessment contract", () => {
expect(validateSchema(rawWorktree)).toBe(false);
});
- test("enforces the patch-risk schema through the shared validator", () => {
- const valid = validateWithSharedSchema(assessment());
+ test("enforces the published patch-risk schema", () => {
+ const valid = validate(assessment());
expect(valid.status, valid.stderr).toBe(0);
const duplicateChangedFiles = assessment();
@@ -197,15 +196,15 @@ describe("patch risk assessment contract", () => {
"src/request.ts",
"src/request.ts",
];
- expect(validateWithSharedSchema(duplicateChangedFiles).status).not.toBe(0);
+ expect(validate(duplicateChangedFiles).status).not.toBe(0);
const emptyRationale = assessment();
emptyRationale.impact.rationale = "";
- expect(validateWithSharedSchema(emptyRationale).status).not.toBe(0);
+ expect(validate(emptyRationale).status).not.toBe(0);
const duplicateItems = assessment();
duplicateItems.autoMergeExclusions = ["migration", "migration"];
- expect(validateWithSharedSchema(duplicateItems).status).not.toBe(0);
+ expect(validate(duplicateItems).status).not.toBe(0);
const tooManyEvidenceSteps = assessment();
tooManyEvidenceSteps.evidencePlan = Array.from(
@@ -216,7 +215,9 @@ describe("patch risk assessment contract", () => {
outcomes: { supported: "merge", contradicted: "revise" },
}),
);
- expect(validateWithSharedSchema(tooManyEvidenceSteps).status).not.toBe(0);
+ const tooManySteps = validate(tooManyEvidenceSteps);
+ expect(tooManySteps.status).toBe(1);
+ expect(tooManySteps.stderr).toContain("evidencePlan");
const incompleteOutcomes = assessment();
incompleteOutcomes.evidencePlan = [
@@ -226,7 +227,9 @@ describe("patch risk assessment contract", () => {
outcomes: { supported: "merge" },
},
];
- expect(validateWithSharedSchema(incompleteOutcomes).status).not.toBe(0);
+ const tooFewOutcomes = validate(incompleteOutcomes);
+ expect(tooFewOutcomes.status).toBe(1);
+ expect(tooFewOutcomes.stderr).toContain("outcomes");
const emptyOutcome = assessment();
emptyOutcome.evidencePlan = [
@@ -236,10 +239,12 @@ describe("patch risk assessment contract", () => {
outcomes: { supported: "", contradicted: "revise" },
},
];
- expect(validateWithSharedSchema(emptyOutcome).status).not.toBe(0);
+ const invalidOutcome = validate(emptyOutcome);
+ expect(invalidOutcome.status).toBe(1);
+ expect(invalidOutcome.stderr).toContain("outcomes/supported");
});
- test("enforces the published schema without site packages", async () => {
+ test("enforces the published schema without Python", async () => {
const schema = JSON.parse(await readFile(schemaPath, "utf8"));
const validateSchema = new Ajv2020({
strict: false,
@@ -281,7 +286,7 @@ describe("patch risk assessment contract", () => {
}
});
- test("accepts a supported human-review merge without site packages", () => {
+ test("accepts a supported human-review merge without Python", () => {
const result = validate(assessment());
expect(result.status, result.stderr).toBe(0);
});
@@ -334,6 +339,15 @@ describe("patch risk assessment contract", () => {
];
const accepted = validate(payload);
expect(accepted.status, accepted.stderr).toBe(0);
+
+ payload.evidencePlan[0]!.outcomes = JSON.parse(
+ '{"__proto__":"merge","contradicted":"revise"}',
+ );
+ expect(validate(payload).status).toBe(0);
+ payload.evidencePlan[0]!.outcomes["__proto__"] = "unsupported";
+ const invalidOutcome = validate(payload);
+ expect(invalidOutcome.status).toBe(1);
+ expect(invalidOutcome.stderr).toContain("outcomes");
});
test("requires an established non-applicable no-op", () => {
@@ -410,16 +424,593 @@ describe("patch risk assessment contract", () => {
expect(validate(payload).status).not.toBe(0);
});
- test("rejects duplicate JSON object keys", () => {
- const serialized = JSON.stringify(assessment()).replace(
- '"recommendation":"merge"',
- '"recommendation":"block","recommendation":"merge"',
+ test("preserves the ordered auto-merge gates", () => {
+ const cases: Array<[string, (value: Assessment) => void]> = [
+ [
+ "impact.rating",
+ (value) => {
+ value.impact.rating = "moderate";
+ },
+ ],
+ [
+ "regressionLikelihood.rating",
+ (value) => {
+ value.regressionLikelihood.rating = "high";
+ },
+ ],
+ [
+ "regressionProtection.rating",
+ (value) => {
+ value.regressionProtection.rating = "partial";
+ },
+ ],
+ [
+ "regressionProtection.exactHeadChecksPassed",
+ (value) => {
+ value.regressionProtection.exactHeadChecksPassed = false;
+ },
+ ],
+ [
+ "recoverability.rating",
+ (value) => {
+ value.recoverability.rating = "managed";
+ },
+ ],
+ [
+ "confidence.rating",
+ (value) => {
+ value.confidence.rating = "moderate";
+ },
+ ],
+ [
+ "applicability.status",
+ (value) => {
+ value.applicability.status = "unknown";
+ },
+ ],
+ [
+ "affectedRuntimeRoots",
+ (value) => {
+ value.affectedRuntimeRoots = [];
+ },
+ ],
+ [
+ "statusQuoRisk.rating",
+ (value) => {
+ value.statusQuoRisk.rating = "unknown";
+ },
+ ],
+ [
+ "autoMergeExclusions",
+ (value) => {
+ value.autoMergeExclusions = ["public_contract"];
+ },
+ ],
+ [
+ "unknowns",
+ (value) => {
+ value.unknowns = [
+ { summary: "A non-critical detail.", decisionCritical: false },
+ ];
+ },
+ ],
+ [
+ "validation",
+ (value) => {
+ value.validation[0]!.status = "skipped";
+ },
+ ],
+ ];
+ const payload = assessment();
+ payload.workflowLabel = "auto_merge_candidate";
+ payload.impact.rating = "low";
+ expect(validate(payload).status).toBe(0);
+ for (const [field, mutate] of cases) {
+ const changed = structuredClone(payload);
+ mutate(changed);
+ const result = validate(changed);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain(
+ `auto_merge_candidate gate failed: ${field}`,
+ );
+ mutate(payload);
+ }
+ expect(validate(payload).stderr.trim().split("\n")).toEqual([
+ "merge requires confirmed applicability",
+ ...cases.map(([field]) => `auto_merge_candidate gate failed: ${field}`),
+ ]);
+ });
+
+ test("keeps all merge errors in their established order", () => {
+ const payload = assessment();
+ payload.workflowLabel = "block";
+ payload.applicability.status = "wrong_owner";
+ payload.unknowns = [
+ { summary: "The owner is unknown.", decisionCritical: true },
+ ];
+ payload.materialBoundaries[0]!.result = "unresolved";
+ payload.validation[0]!.status = "failed";
+ payload.evidencePlan = [
+ {
+ question: "Who owns this?",
+ action: "Inspect the mapping.",
+ outcomes: { owned: "revise", unowned: "no_op" },
+ },
+ ];
+ const result = validate(payload);
+ expect(result.status).toBe(1);
+ expect(result.stdout).toBe("");
+ expect(result.stderr.trim().split("\n")).toEqual([
+ "merge requires an auto-merge or human-review workflow label",
+ "merge requires confirmed applicability",
+ "merge cannot retain a decision-critical unknown",
+ "merge requires every material boundary to be supported",
+ "merge cannot retain a failed validation",
+ "merge cannot retain an evidence plan",
+ "only hold_for_evidence may include an evidence plan",
+ "an established non-applicable disposition requires no_op",
+ ]);
+ delete (payload as Record<string, unknown>)["patch"];
+ const missingPatch = validate(payload);
+ expect(missingPatch.status).toBe(1);
+ expect(missingPatch.stderr).toContain("patch-risk-assessment.schema");
+ });
+
+ test("requires matching non-merge labels and a settled no-op disposition", () => {
+ const payload = assessment();
+ payload.recommendation = "revise";
+ payload.validation[0]!.status = "failed";
+ expect(validate(payload).stderr).toBe(
+ "non-merge workflow label must match the recommendation\n",
);
+ payload.workflowLabel = "revise";
+ expect(validate(payload).status).toBe(0);
+ for (const status of [
+ "no_live_effect",
+ "wrong_owner",
+ "duplicate",
+ "superseded",
+ ]) {
+ const noOp = assessment();
+ noOp.recommendation = noOp.workflowLabel = "no_op";
+ noOp.applicability.status = status;
+ expect(validate(noOp).status).toBe(0);
+ noOp.unknowns = [
+ { summary: "Coverage is unresolved.", decisionCritical: true },
+ ];
+ expect(validate(noOp).stderr).toBe(
+ "no_op cannot retain a decision-critical unknown\n",
+ );
+ }
+ });
- const rejected = validateText(serialized);
- expect(rejected.status).not.toBe(0);
- expect(rejected.stderr).toContain(
- "duplicate JSON object key: recommendation",
+ test("keeps each established failure out of an evidence hold", () => {
+ const failures: Array<(value: Assessment) => void> = [
+ (value) => {
+ value.regressionLikelihood.rating = "critical";
+ },
+ (value) => {
+ value.materialBoundaries[0]!.result = "contradicted";
+ },
+ (value) => {
+ value.validation[0]!.status = "failed";
+ },
+ ];
+ for (const fail of failures) {
+ const payload = assessment();
+ payload.recommendation = payload.workflowLabel = "hold_for_evidence";
+ payload.unknowns = [
+ { summary: "A rollout detail.", decisionCritical: true },
+ ];
+ payload.evidencePlan = [
+ {
+ question: "Which rollout?",
+ action: "Inspect deployment.",
+ outcomes: { owned: "merge", unowned: "no_op" },
+ },
+ ];
+ fail(payload);
+ expect(validate(payload).stderr).toBe(
+ "hold_for_evidence cannot defer an established defect\n",
+ );
+ payload.evidencePlan = [];
+ for (const recommendation of ["revise", "block"]) {
+ payload.recommendation = payload.workflowLabel = recommendation;
+ expect(validate(payload).status).toBe(0);
+ }
+ }
+ });
+
+ test("requires numeric schema version 1", () => {
+ const serialized = JSON.stringify(assessment());
+ for (const token of ["1", "1.0", "1e0"]) {
+ const result = validateText(
+ serialized.replace('"schemaVersion":1', `"schemaVersion":${token}`),
+ );
+ expect(result.status, result.stderr).toBe(0);
+ }
+ for (const token of [
+ "true",
+ "false",
+ '"1"',
+ "0",
+ "null",
+ "9007199254740993",
+ ]) {
+ const result = validateText(
+ serialized.replace('"schemaVersion":1', `"schemaVersion":${token}`),
+ );
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain("schemaVersion");
+ }
+ });
+
+ test("accepts large assessments and rejects truncated JSON", async () => {
+ const complete = assessment();
+ complete.impact.rationale = '"'.repeat(5_000_000);
+ const truncated =
+ '{"impact":{"rationale":"' + '\\"'.repeat(5_000_000) + "\\";
+ for (const [input, status] of [
+ [truncated, 1],
+ [JSON.stringify(complete), 0],
+ ] as const) {
+ const result = await runCommand(
+ node,
+ [helper, "validate-patch-risk-assessment", "-"],
+ {
+ input,
+ timeout: 5000,
+ },
+ );
+ expect(result.signal, result.error?.message).toBeNull();
+ expect(result.status, result.stderr).toBe(status);
+ if (status === 1)
+ expect(result.stderr).toContain("cannot read assessment:");
+ else expect(result.stderr).toBe("");
+ }
+ });
+
+ test("rejects malformed JSON with a useful diagnostic", () => {
+ for (const text of [
+ "\ufeff{}",
+ '{"x":1,}',
+ '{"x" "\\q"}',
+ '[0 "\\q"]',
+ '{"x":"\\uZZZZ"}',
+ '{"x":"\\q"}',
+ '"\\q',
+ '"\\u123',
+ '"truncated\\',
+ '{"x":"line\n"}',
+ '{"x":"😀\\q"}',
+ "{} false",
+ '{"schemaVersion":NaN}',
+ '{"schemaVersion":Infinity}',
+ ]) {
+ const result = validateText(text);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain("cannot read assessment:");
+ }
+ const array = validateText("[]");
+ expect(array.status).toBe(1);
+ expect(array.stderr).toContain("assessment must be a JSON object");
+ });
+
+ test("escapes terminal controls echoed by JSON syntax errors", () => {
+ for (const [control, escaped] of [
+ ["\u001b", "\\u001b"],
+ ["\u202e", "\\u202e"],
+ ["\u{e0001}", "\\u{e0001}"],
+ ] as const) {
+ const result = validateText(`${control}[2J`);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain("cannot read assessment:");
+ expect(result.stderr).not.toContain(control);
+ expect(result.stderr).toContain(escaped);
+ }
+ });
+
+ test("reads file and stdin inputs without rewriting the assessment", async () => {
+ const outside = await mkdtemp(join(tmpdir(), "patch-risk-files-"));
+ try {
+ const original =
+ JSON.stringify(assessment(), null, 2).replaceAll("\n", "\r\n") + "\r\n";
+ for (const name of ["assessment with spaces.json", "-1", "- item", "-"]) {
+ const path = join(outside, name);
+ await writeFile(path, original);
+ const result = validateText("not stdin", outside, [
+ name.startsWith("-") ? `./${name}` : name,
+ ]);
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.stdout).toBe("");
+ expect(result.stderr).toBe("");
+ expect(await readFile(path, "utf8")).toBe(original);
+ expect(validateText("", outside, ["--", name]).status).toBe(
+ name === "-" ? 1 : 0,
+ );
+ }
+ const file = join(outside, "assessment with spaces.json");
+ if (process.platform !== "win32") {
+ for (const suffix of ["/.", "///", "/./."])
+ expect(validateText("", outside, [file + suffix]).status).toBe(0);
+ await mkdir(join(outside, "child", "nested"), { recursive: true });
+ await symlink("child/nested", join(outside, "link"));
+ await writeFile(join(outside, "child", "assessment.json"), original);
+ await writeFile(join(outside, "assessment.json"), "wrong sibling");
+ expect(
+ validateText("", outside, ["link/../assessment.json/."]).status,
+ ).toBe(0);
+ }
+ expect(validateText(original, outside).status).toBe(0);
+ if (process.platform !== "win32") {
+ const launched = spawnSync(
+ join(PLUGIN_ROOT, "scripts", "launch_codex_security_mcp"),
+ ["--helper", "validate-patch-risk-assessment", "-"],
+ {
+ cwd: outside,
+ input: original,
+ encoding: "utf8",
+ env: { ...process.env, CODEX_MCP_NODE_PATH: node },
+ },
+ );
+ expect(launched.status, launched.stderr).toBe(0);
+ expect(launched.stdout).toBe("");
+ expect(launched.stderr).toBe("");
+ }
+ const invalid = join(outside, "invalid.json");
+ const malformed = '{\r\n"x":1\r\n"y":2}';
+ await writeFile(invalid, malformed);
+ expect(validateText(malformed).stderr).toContain(
+ "cannot read assessment:",
+ );
+ expect(validateText("", outside, [invalid]).stderr).toContain(
+ "cannot read assessment:",
+ );
+ await writeFile(invalid, Buffer.from([0xff]));
+ expect(validateText("", outside, [invalid]).status).toBe(1);
+ expect(validateText("", outside, ["missing.json"]).stderr).toContain(
+ "cannot read assessment:",
+ );
+ } finally {
+ await rm(outside, { recursive: true, force: true });
+ }
+ });
+
+ test("escapes terminal controls in assessment file-read error paths", async () => {
+ const outside = await mkdtemp(join(tmpdir(), "patch-risk-read-error-"));
+ try {
+ for (const [control, escaped] of [
+ ["\u001b", "\\u001b"],
+ ["\u202e", "\\u202e"],
+ ["\u{e0001}", "\\u{e0001}"],
+ ] as const) {
+ const file = join(outside, `missing-${control}[2J.json`);
+ const result = validateText("", outside, [file]);
+ expect(result.status).toBe(1);
+ expect(result.stdout).toBe("");
+ expect(result.stderr).toContain(`missing-${escaped}[2J.json`);
+ expect(result.stderr).not.toContain(control);
+ }
+ } finally {
+ await rm(outside, { recursive: true, force: true });
+ }
+ });
+
+ for (const location of ["absolute", "relative", "unc"] as const)
+ test.skipIf(
+ process.platform !== "win32" ||
+ (location === "unc" && !hasWindowsLoopbackShare),
+ )(
+ `executes the documented PowerShell command with ${location} literal assessment and plugin paths`,
+ async () => {
+ const outside = await mkdtemp(join(tmpdir(), "patch-risk-powershell-"));
+ powershellDirectories.push(outside);
+ const plugin = join(outside, "plugin %PLUGIN% !EXPAND! caf\u00e9's");
+ const expandedPlugin = plugin.replace("%PLUGIN%", "expanded-plugin");
+ const file = join(
+ outside,
+ "review-%USERNAME% !EXPAND! \u96ea's",
+ "assessment.json",
+ );
+ const expandedFile = file.replace("%USERNAME%", "expanded-user");
+ await mkdir(join(plugin, "scripts"), { recursive: true });
+ await cp(join(PLUGIN_ROOT, "mcp"), join(plugin, "mcp"), {
+ recursive: true,
+ });
+ await copyFile(
+ join(PLUGIN_ROOT, "scripts", "launch_codex_security_mcp.cmd"),
+ join(plugin, "scripts", "launch_codex_security_mcp.cmd"),
+ );
+ await mkdir(join(expandedPlugin, "scripts"), { recursive: true });
+ await writeFile(
+ join(expandedPlugin, "scripts", "launch_codex_security_mcp.cmd"),
+ "@echo expanded-plugin-used\r\n@exit /b 0\r\n",
+ );
+ for (const path of [file, expandedFile])
+ await mkdir(dirname(path), { recursive: true });
+ const original = JSON.stringify(assessment()).replace(
+ "example/project",
+ "example/caf\u00e9-\u96ea",
+ );
+ await writeFile(expandedFile, original);
+ const skill = await readFile(
+ join(PLUGIN_ROOT, "skills", "assess-patch-risk", "SKILL.md"),
+ "utf8",
+ );
+ const command = /```powershell\r?\n([\s\S]*?)\r?\n```/u.exec(
+ skill,
+ )?.[1];
+ expect(command).toBeDefined();
+ const quote = (value: string) => value.replaceAll("'", "''");
+ const caller = join(outside, "caller");
+ await mkdir(caller);
+ const workingDirectory =
+ location === "unc" ? windowsLoopbackPath(caller) : caller;
+ const argument = (path: string) =>
+ location === "absolute" ? path : relative(caller, path);
+ const powershells = [
+ join(
+ process.env["SystemRoot"]!,
+ "System32",
+ "WindowsPowerShell",
+ "v1.0",
+ "powershell.exe",
+ ),
+ Bun.which("pwsh"),
+ ].filter((value): value is string => value !== null);
+ const fixtureEnvironment = {
+ SystemRoot: process.env["SystemRoot"],
+ PATH: join(process.env["SystemRoot"]!, "System32"),
+ HOME: outside,
+ USERPROFILE: outside,
+ LOCALAPPDATA: outside,
+ XDG_CACHE_HOME: outside,
+ CODEX_MCP_NODE_PATH: node,
+ PLUGIN: "expanded-plugin",
+ USERNAME: "expanded-user",
+ EXPAND: "expanded-bang",
+ };
+ const overrides = new Set(
+ Object.keys(fixtureEnvironment).map((key) => key.toUpperCase()),
+ );
+ const inherited = Object.fromEntries(
+ Object.entries(process.env).filter(
+ ([key]) => !overrides.has(key.toUpperCase()),
+ ),
+ );
+ for (const powershell of powershells) {
+ for (const input of [
+ "invalid",
+ "valid",
+ "stdin",
+ "pipeline",
+ "missing",
+ ]) {
+ await writeFile(file, input === "invalid" ? "{}" : original);
+ if (input === "missing") await rm(file);
+ const script =
+ "$ProgressPreference = 'SilentlyContinue'\n" +
+ `Set-Location -LiteralPath '${quote(workingDirectory)}' -ErrorAction Stop\n` +
+ "$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'\n" +
+ command!
+ .replace("<plugin-root>", quote(argument(plugin)))
+ .replace(
+ "<assessment.json>",
+ quote(
+ input === "stdin" || input === "pipeline"
+ ? "-"
+ : argument(file),
+ ),
+ ) +
+ "\n";
+ const result = await runCommand(
+ powershell,
+ [
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-EncodedCommand",
+ Buffer.from(
+ input === "pipeline"
+ ? script.replace(
+ /^cmd\.exe/m,
+ `'${quote(original)}' | cmd.exe`,
+ )
+ : script,
+ "utf16le",
+ ).toString("base64"),
+ ],
+ {
+ cwd: outside,
+ env: { ...inherited, ...fixtureEnvironment },
+ timeout: 30_000,
+ input: input === "pipeline" ? "" : original,
+ windowsHide: true,
+ },
+ );
+ const diagnostics = JSON.stringify(
+ {
+ powershell,
+ location,
+ input,
+ workingDirectory,
+ plugin: argument(plugin),
+ assessment:
+ input === "stdin" || input === "pipeline"
+ ? "-"
+ : argument(file),
+ status: result.status,
+ signal: result.signal,
+ error: result.error?.message,
+ stdout: result.stdout,
+ stderr: result.stderr,
+ },
+ null,
+ 2,
+ );
+ expect(result.stdout, diagnostics).not.toContain(
+ "expanded-plugin-used",
+ );
+ expect(result.status, diagnostics).toBe(
+ input === "invalid" || input === "missing" ? 1 : 0,
+ );
+ if (location !== "unc") expect(result.stdout, diagnostics).toBe("");
+ if (input === "invalid")
+ expect(result.stderr, diagnostics).toContain(
+ "patch-risk-assessment.schema",
+ );
+ else if (input === "missing")
+ expect(result.stderr, diagnostics).toContain("Convert-Path");
+ else if (location !== "unc")
+ expect(result.stderr, diagnostics).toBe("");
+ if (input === "missing")
+ await expect(readFile(file), diagnostics).rejects.toMatchObject({
+ code: "ENOENT",
+ });
+ else
+ await expect(readFile(file, "utf8"), diagnostics).resolves.toBe(
+ input === "invalid" ? "{}" : original,
+ );
+ }
+ }
+ },
);
+
+ test.skipIf(process.platform === "win32")(
+ "keeps stdin surrogate escapes distinct from replacement characters",
+ () => {
+ const serialized = JSON.stringify(assessment()).replace(
+ '"changedFiles":["src/request.ts"]',
+ '"changedFiles":["RAW","\\ufffd"]',
+ );
+ const [before, after] = serialized.split("RAW");
+ const result = validateText(
+ Buffer.concat([
+ Buffer.from(before!),
+ Buffer.from([0xff]),
+ Buffer.from(after!),
+ ]),
+ );
+ expect(result.status, result.stderr).toBe(0);
+ },
+ );
+
+ test("supports documented help, positional arguments, and parser exit statuses", () => {
+ for (const args of [["-h"], ["--help"]]) {
+ const result = validateText("", PLUGIN_ROOT, args);
+ expect(result.status).toBe(0);
+ expect(result.stdout).toContain("Validate a patch-risk assessment.");
+ }
+ for (const args of [
+ [],
+ ["--"],
+ ["--bad"],
+ ["one", "two"],
+ ["--help=bad"],
+ ["-h=bad"],
+ ]) {
+ expect(validateText("", PLUGIN_ROOT, args).status).toBe(2);
+ }
+ expect(validateText("", PLUGIN_ROOT, ["--", "-h"]).status).toBe(1);
});
});
diff --git a/sdk/typescript/tests-ts/shell.test.ts b/sdk/typescript/tests-ts/shell.test.ts
new file mode 100644
index 000000000..8a5bbb04f
--- /dev/null
+++ b/sdk/typescript/tests-ts/shell.test.ts
@@ -0,0 +1,28 @@
+import { expect, test } from "bun:test";
+import { runCommand } from "./support/shell.js";
+
+test("captures asynchronous command input, output, and failure diagnostics", async () => {
+ const input = "literal %USERNAME% !EXPAND! caf\u00e9's \u96ea\n";
+ const result = await runCommand(
+ process.execPath,
+ [
+ "-e",
+ `
+ let input = "";
+ process.stdin.setEncoding("utf8");
+ process.stdin.on("data", (chunk) => { input += chunk; });
+ process.stdin.on("end", () => {
+ process.stdout.write(input);
+ process.stderr.write("native stderr\\n");
+ process.exitCode = 23;
+ });
+ `,
+ ],
+ { input, timeout: 30_000, windowsHide: true },
+ );
+ expect(result.status).toBe(23);
+ expect(result.stdout).toBe(input);
+ expect(result.stderr).toBe("native stderr\n");
+ expect(result.signal).toBeNull();
+ expect(result.error).toBeInstanceOf(Error);
+});
diff --git a/sdk/typescript/tests-ts/support/shell.ts b/sdk/typescript/tests-ts/support/shell.ts
index 292530a36..ce44ee29e 100644
--- a/sdk/typescript/tests-ts/support/shell.ts
+++ b/sdk/typescript/tests-ts/support/shell.ts
@@ -22,16 +22,29 @@ export function runCommand(
env?: NodeJS.ProcessEnv;
input?: string;
timeout: number;
+ windowsHide?: boolean;
},
-): Promise<{ status: number | null; stdout: string; stderr: string }> {
+): Promise<{
+ status: number | null;
+ stdout: string;
+ stderr: string;
+ signal: NodeJS.Signals | null;
+ error: Error | null;
+}> {
// Avoid Bun's premature synchronous timeouts while keeping pipe reads bounded.
return new Promise((resolve, reject) => {
const child = execFile(
command,
args,
{ ...options, encoding: "utf8" },
- (_error, stdout, stderr) => {
- resolve({ status: child.exitCode, stdout, stderr });
+ (error, stdout, stderr) => {
+ resolve({
+ status: child.exitCode,
+ stdout,
+ stderr,
+ signal: child.signalCode,
+ error,
+ });
},
);
child.stdin?.on("error", reject);
diff --git a/sdk/typescript/tests-ts/support/temporary-directories.ts b/sdk/typescript/tests-ts/support/temporary-directories.ts
index cf7b0740e..f32017a34 100644
--- a/sdk/typescript/tests-ts/support/temporary-directories.ts
+++ b/sdk/typescript/tests-ts/support/temporary-directories.ts
@@ -1,8 +1,23 @@
+import { rm } from "node:fs/promises";
import {
createTemporaryDirectories,
temporaryDirectory as createPluginTemporaryDirectory,
} from "../../../../plugins/codex-security/mcp-app/tests/support/temporary-directories.ts";
+export async function removeTemporaryDirectory(path: string): Promise<void> {
+ // Bun 1.3.14 ignores fs.rm's retry options.
+ for (let attempt = 0; ; attempt++) {
+ try {
+ await rm(path, { recursive: true, force: true });
+ return;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "EBUSY" || attempt === 10)
+ throw error;
+ await Bun.sleep(100 * (attempt + 1));
+ }
+ }
+}
+
export { createTemporaryDirectories };
export function createApiTestFixtures(
diff --git a/sdk/typescript/tests-ts/windows-helper-location.ts b/sdk/typescript/tests-ts/windows-helper-location.ts
new file mode 100644
index 000000000..a2aca2750
--- /dev/null
+++ b/sdk/typescript/tests-ts/windows-helper-location.ts
@@ -0,0 +1,12 @@
+import { existsSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { parse } from "node:path";
+
+export function windowsLoopbackPath(path: string): string {
+ const root = parse(path).root;
+ // File URLs normalize localhost to an empty host, which breaks Node ESM on UNC paths.
+ return `\\\\127.0.0.1\\${root[0]}$\\${path.slice(root.length)}`;
+}
+
+export const hasWindowsLoopbackShare =
+ process.platform === "win32" && existsSync(windowsLoopbackPath(tmpdir()));