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(preview): simplify fitting source previews to byte limits (#1350)

mldangelo-oaib470bb091dab
diff --git a/plugins/codex-security/scripts/generate_rank_input.py b/plugins/codex-security/scripts/generate_rank_input.py
index 1623b605b..92cc34887 100644
--- a/plugins/codex-security/scripts/generate_rank_input.py
+++ b/plugins/codex-security/scripts/generate_rank_input.py
@@ -487,11 +487,9 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:
)
if is_binary_sample(content):
continue
- preview, is_binary = preview_for_bytes(
+ preview, _ = preview_for_bytes(
rel, content[:DEFAULT_PREVIEW_READ_BYTES], args.preview_bytes
)
- if is_binary:
- continue
elif status != "D" and not path.is_symlink() and path.is_file():
try:
path.resolve(strict=True).relative_to(repo)
diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.py
index 2f1b989bc..f568f6111 100644
--- a/plugins/codex-security/scripts/rank_preview.py
+++ b/plugins/codex-security/scripts/rank_preview.py
@@ -6,6 +6,7 @@ import argparse
import ast
import json
import re
+from bisect import bisect_right
from pathlib import Path
DEFAULT_PREVIEW_BYTES = 1024
@@ -179,17 +180,9 @@ def fit_preview_lines(lines: list[str], max_bytes: int) -> str:
content_lines = [line for line in lines if line != "..."]
if not content_lines:
return truncate_utf8(full_preview, max_bytes)
- low = 0
high = max(len(line.encode("utf-8")) for line in content_lines)
- best = ""
- while low <= high:
- middle = (low + high) // 2
- candidate = render(middle)
- if len(candidate.encode("utf-8")) <= max_bytes:
- best = candidate
- low = middle + 1
- else:
- high = middle - 1
+ 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)