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(mcp): read completed scan artifacts after checkout removal (#2050)

Michael D'Angelofc97f7b831c9
diff --git a/plugins/codex-security/mcp-app/src/artifact-context.ts b/plugins/codex-security/mcp-app/src/artifact-context.ts
index 22b87eace..6ef8a2c1f 100644
--- a/plugins/codex-security/mcp-app/src/artifact-context.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-context.ts
@@ -87,6 +87,7 @@ export async function createScanArtifactContext(
repoRoot: await canonicalDirectory(
rawRepoRoot,
"Codex Security scan target root",
+ options.requireRunning === false && status === "complete",
),
layout: "scan",
scanId,
@@ -119,12 +120,22 @@ function scanRecord(
export async function canonicalDirectory(
value: string,
label: string,
+ allowMissing = false,
): Promise<string> {
if (!value || !isAbsolute(value)) {
throw new Error(label + " must be an absolute directory.");
}
const requested = resolve(value);
- if (!(await fs.lstat(requested).catch(() => undefined))?.isDirectory()) {
+ let entry;
+ try {
+ entry = await fs.lstat(requested);
+ } catch (error) {
+ // Completed artifact reads use the scan directory after a checkout is removed.
+ if (allowMissing && (error as NodeJS.ErrnoException).code === "ENOENT") {
+ return requested;
+ }
+ }
+ if (!entry?.isDirectory()) {
throw new Error(label + " is not a safe regular directory.");
}
try {
diff --git a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts
index 71c3d3a4c..9a3ba0cda 100644
--- a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts
+++ b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts
@@ -44,6 +44,7 @@ import {
mkdir,
readFile,
realpath,
+ rename,
rm,
writeFile,
} from "node:fs/promises";
@@ -411,10 +412,27 @@ async function testCompactDiffScanCompletion(
}),
`${runtimeLabel}: record compact diff canonical semantics`,
);
+ const note = {
+ scanId,
+ handoffClaimToken,
+ storage: "persistent",
+ path: "artifacts/review-note.md",
+ };
+ const content = "Saved with the scan.\n";
+ requireSuccessfulTool(
+ await call("save_codex_security_artifact", { ...note, content }),
+ `${runtimeLabel}: save supplemental note`,
+ );
requireSuccessfulTool<ScanResult>(
await call("complete_codex_security_scan", { scanId, handoffClaimToken }),
`${runtimeLabel}: complete compact diff scan`,
);
+ await rename(repoRoot, repoRoot + "-moved");
+ const saved = requireSuccessfulTool<{ content: string }>(
+ await call("read_codex_security_artifact", note),
+ `${runtimeLabel}: read supplemental note after moving its checkout`,
+ );
+ assert.equal(saved.content, content);
const completed = requireSuccessfulTool<CompletedResult>(
await call("get_codex_security_completed_scan", {
scanId,