Skip to content

fix: eliminate catastrophic regex backtracking in postprocess script#5491

Merged
lpcox merged 1 commit into
mainfrom
fix/postprocess-catastrophic-backtracking
Jun 24, 2026
Merged

fix: eliminate catastrophic regex backtracking in postprocess script#5491
lpcox merged 1 commit into
mainfrom
fix/postprocess-catastrophic-backtracking

Conversation

@lpcox

@lpcox lpcox commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Problem

scripts/ci/postprocess-smoke-workflows.ts hung for minutes on every run. Root cause: CODEX_PROXY_ENV_KEY_REGEX triggered catastrophic backtracking whenever it failed to match — which is the common case once the [model_providers.openai-proxy] block exists but the env_key line has already been stripped (the normal state after the first post-process run).

Two ambiguities drove the exponential backtracking:

  1. \s also matches newlines, so ^\s+.*\n could consume line breaks two different ways.
  2. [ \t]+ is variable-length, so ^[ \t]+.* could split a single line's indent and body many ways.

Fix

  • Anchor each repeated inner line to exactly one leading space/tab (^[ \t].*) so every physical line matches a single way → linear matching.
  • Add a cheap content.includes('env_key = "OPENAI_API_KEY"') short-circuit guard so the regex is skipped entirely on already-processed files.

Result

Script runtime drops from minutes → ~1.4s. tsc --noEmit clean; output (the env_key strip + xpia transforms) is unchanged on the codex lock files.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

CODEX_PROXY_ENV_KEY_REGEX hung the postprocess script for minutes when it
failed to match (the common case once the openai-proxy block exists but the
env_key line is already stripped). Two ambiguities drove exponential
backtracking: \\s also matches newlines, and [ \\t]+ is variable-length so
^[ \\t]+.* could split a single line many ways.

Anchor each repeated line to exactly one leading space/tab (^[ \\t].*) so every
physical line matches a single way, making the match linear. Add a cheap
content.includes guard to skip the regex entirely on already-processed files.

Script runtime drops from minutes to ~1.4s.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 24, 2026 16:03
@github-actions

Copy link
Copy Markdown
Contributor

✅ Coverage Check Passed

Overall Coverage

Metric Base PR Delta
Lines 98.10% 98.14% 📈 +0.04%
Statements 98.03% 98.07% 📈 +0.04%
Functions 99.52% 99.52% ➡️ +0.00%
Branches 93.81% 93.81% ➡️ +0.00%
📁 Per-file Coverage Changes (1 files)
File Lines (Before → After) Statements (Before → After)
src/workdir-setup.ts 92.7% → 94.5% (+1.82%) 92.7% → 94.5% (+1.82%)

Coverage comparison generated by scripts/ci/compare-coverage.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR speeds up the CI smoke-workflow postprocessing script by preventing catastrophic regex backtracking when stripping a legacy env_key line from Codex/OpenAI proxy provider blocks, and by skipping the regex entirely when the target line is absent.

Changes:

  • Reworks CODEX_PROXY_ENV_KEY_REGEX to avoid ambiguous repeated subpatterns that caused exponential backtracking on non-matches.
  • Adds a cheap content.includes('env_key = "OPENAI_API_KEY"') guard before running the regex.
Show a summary per file
File Description
scripts/ci/postprocess-smoke-workflows.ts Makes env_key stripping linear-time and adds an includes() short-circuit to avoid unnecessary regex work.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment on lines 841 to +842
const CODEX_PROXY_ENV_KEY_REGEX =
/(^\s+\[model_providers\.openai-proxy\]\n(?:^\s+.*\n)*?)^\s+env_key = "OPENAI_API_KEY"\n/m;
/(^[ \t]+\[model_providers\.openai-proxy\]\n(?:^[ \t].*\n)*?)^[ \t]+env_key = "OPENAI_API_KEY"\n/m;
@lpcox lpcox merged commit dabee9d into main Jun 24, 2026
24 checks passed
@lpcox lpcox deleted the fix/postprocess-catastrophic-backtracking branch June 24, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants