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(ranking): reuse validated inputs and simplify preview sizing (#1369)

mldangelo-oai96583fc32d09
diff --git a/plugins/codex-security/mcp-app/src/helpers/rank-pool.ts b/plugins/codex-security/mcp-app/src/helpers/rank-pool.ts
index 1b384b4b1..d54d954a5 100644
--- a/plugins/codex-security/mcp-app/src/helpers/rank-pool.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/rank-pool.ts
@@ -169,7 +169,7 @@ function validatePlan(plan: string, directory: string) {
throw new Error(
`${plan}: workers must contain exactly ${workerCount} worker assignments`,
);
- const workers = payload.workers.map((raw: unknown, index): Worker => {
+ payload.workers.forEach((raw: unknown, index) => {
const label = `${plan}: workers[${index}]`;
if (!object(raw)) throw new Error(`${label} must be a JSON object`);
fields(raw, ["slot", "input_shards", "output_shards"], label);
@@ -187,12 +187,8 @@ function validatePlan(plan: string, directory: string) {
);
if (!same(assignedOutputs, assignedInputs.map(outputShardName)))
throw new Error(`${label}.output_shards do not match its input_shards`);
- return {
- slot: slot,
- input_shards: assignedInputs,
- output_shards: assignedOutputs,
- };
});
+ const workers = payload.workers as Worker[];
const counts = new Map<string, number>();
for (const worker of workers)
for (const name of worker.input_shards)
diff --git a/plugins/codex-security/mcp-app/src/helpers/rank-shards.ts b/plugins/codex-security/mcp-app/src/helpers/rank-shards.ts
index 640514c4a..d512cd93a 100644
--- a/plugins/codex-security/mcp-app/src/helpers/rank-shards.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/rank-shards.ts
@@ -181,29 +181,27 @@ function mergeShards(
details.push(`unexpected output shards ${formatDiagnostic(unexpected)}`);
throw new Error(`Rank shard outputs are incomplete: ${details.join("; ")}`);
}
- const shardedInputs: RankRow[] = [];
+ let inputIndex = 0;
+ let partitionMatches = true;
const outputByPath = new Map<string, RankRow>();
for (const shard of shards) {
const outputShard = childPath(directory, outputShardName(basename(shard)));
const [inputs, outputs] = validateShard(shard, outputShard);
- for (const row of inputs) shardedInputs.push(row);
+ for (const row of inputs) {
+ const expectedRow = authoritative[inputIndex++];
+ partitionMatches &&=
+ expectedRow !== undefined &&
+ row.path === expectedRow.path &&
+ row.area === expectedRow.area &&
+ row.preview === expectedRow.preview;
+ }
for (const row of outputs) {
if (outputByPath.has(row.path))
throw new Error(`Rank outputs contain duplicate path: ${row.path}`);
outputByPath.set(row.path, row);
}
}
- if (
- shardedInputs.length !== authoritative.length ||
- shardedInputs.some((row, index) => {
- const expectedRow = authoritative[index]!;
- return (
- row.path !== expectedRow.path ||
- row.area !== expectedRow.area ||
- row.preview !== expectedRow.preview
- );
- })
- )
+ if (inputIndex !== authoritative.length || !partitionMatches)
throw new Error(
"Rank input shards do not exactly partition the authoritative rank input",
);
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index f568f6111..3e691fd03 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -177,15 +177,12 @@ def fit_preview_lines(lines: list[str], max_bytes: int) -> str:
line if line == "..." else truncate_utf8(line, line_bytes).rstrip() for line in lines
)
- content_lines = [line for line in lines if line != "..."]
- if not content_lines:
- return truncate_utf8(full_preview, max_bytes)
- high = max(len(line.encode("utf-8")) for line in content_lines)
- length = bisect_right(range(high + 1), max_bytes, key=lambda n: len(render(n).encode("utf-8")))
- best = render(length - 1) if length else ""
- if best:
- return best
- return truncate_utf8(full_preview, max_bytes)
+ high = max(len(line.encode("utf-8")) for line in lines)
+ limit = bisect_right(
+ range(high + 1), max_bytes, key=lambda size: len(render(size).encode("utf-8"))
+ )
+ best = render(limit - 1) if limit else ""
+ return best or truncate_utf8(full_preview, max_bytes)
def python_decorators(node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef) -> str:
diff --git a/sdk/typescript/src/deduplication/records.ts b/sdk/typescript/src/deduplication/records.ts
index 860126cd5..85b0373f3 100644
--- a/sdk/typescript/src/deduplication/records.ts
+++ b/sdk/typescript/src/deduplication/records.ts
@@ -140,8 +140,8 @@ export async function deduplicateRecords(
groups: [],
unresolved: [],
};
- const sourceFindings = [...relationships.keys()].map((id) =>
- observations.get(id)!,
+ const sourceFindingIds = [...relationships.keys()].map(
+ (id) => observations.get(id)!.findingId,
);
const reviewer = new CodexDeduplicationReviewer({
async run<T>({
@@ -206,9 +206,7 @@ export async function deduplicateRecords(
);
let groups: string[][];
try {
- const decisions = await algorithm.run(
- sourceFindings.map((finding) => finding.findingId),
- );
+ const decisions = await algorithm.run(sourceFindingIds);
options.signal?.throwIfAborted();
groups = decisions.duplicateGroups;
} catch (error) {
@@ -224,9 +222,7 @@ export async function deduplicateRecords(
}
const grouped = new Set(groups.flat());
groups.push(
- ...sourceFindings
- .filter((finding) => !grouped.has(finding.findingId))
- .map((finding) => [finding.findingId]),
+ ...sourceFindingIds.filter((id) => !grouped.has(id)).map((id) => [id]),
);
for (const group of groups) {
const observationIds = group.map((id) => references.get(id)!);
diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.ts
index 2f19d9457..4ba52e932 100644
--- a/sdk/typescript/src/scan-comparison.ts
+++ b/sdk/typescript/src/scan-comparison.ts
@@ -1005,9 +1005,7 @@ function comparisonPrompt(
}
function characterCount(value: string): number {
- let count = 0;
- for (const _character of value) count += 1;
- return count;
+ return value[Symbol.iterator]().reduce((count) => count + 1, 0);
}
function cataloguePages(input: CataloguePage): CataloguePage[] {