Skip to content

The prompt-pack and code-shunt hooks match their tag and pattern regexes in linear time - #365

Merged
zlareb1 merged 5 commits into
mainfrom
fix/plugin-hook-regexes-linear
Nov 7, 2026
Merged

zlareb1 merged 5 commits into
mainfrom
fix/plugin-hook-regexes-linear

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The Claude Code and Codex plugins' prompt-pack and code-shunt hooks no longer stall a prompt submit or a Bash call for seconds on a prompt with a long whitespace run after an opening tag, or a sed or awk program with a run of backslashes.

Why

Two regexes in the hooks backtrack super-linearly on text the hook does not control, and each runs synchronously, so the hook's own timers cannot fire:

  • lib/prompt-pack.mjs WRAPPED_RE (<(tag)>\s*([\s\S]*?)\s*<\/\1>), on every UserPromptSubmit: three quantifiers can take the same whitespace, so <task> followed by 2000 spaces and no close tag took 4.4 s (cubic). It is replaced by a regex for the open tag alone and wrappedBlock, which looks for that tag's close once and trims the body. It still takes the first open tag that has a close after it and pairs it with the nearest close.
  • lib/shell-command.mjs, the awk /^\/((?:\\.|[^\/])+)\// and sed /^\/((?:\\.|[^\/])+)\/p$/ patterns, on every Bash PreToolUse in an indexed repository: a backslash matches both alternatives, so each pair doubles the paths. With 42 backslashes sed and awk together took 8 s. The alternatives are now disjoint, (?:\\.|[^\/\\])+, as ghIssueSearchInCommand already writes it. A lone backslash no longer stands for itself before the closing /: sed -n '/a\/p' and awk '/a\/' used to parse to a grep for a\; they never end, as sed and awk read them, and now parse to nothing.

The Codex copies come from scripts.codegen.agent_plugin_sync. Both manifests go to 1.2.13 (1.2.11 and 1.2.12 are taken by open pull requests).

Scope and risk

The two hook libraries. Behaviour on ordinary input is unchanged, pinned by the tests below.

Compatibility, rollout and rollback

  • Wire: none. Plugin version 1.2.13. Rollback: revert.

Tests

  • tests/prompt-pack.test.mjs: <task> + 2000 spaces under 500 ms (8.8 s on main, 1.2 ms here); a wrapped <task> block with blank lines around its text still comes back trimmed.
  • tests/code-shunt.test.mjs: sed and awk with 42 backslashes under 500 ms (8 s on main, 1.1 ms here); sed -n '/a\/b/p' and the awk form still parse to the grep shape with pattern a/b; the unterminated sed -n '/a\/p' and awk '/a\/' parse to nothing.
  • node --test on prompt-pack, prompt-pack-hook, code-shunt and code-shunt-hook: 74 passed. tests/unit/test_codex_plugin.py: 17 passed.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 5 commits November 7, 2026 11:58
…xes in linear time

A prompt holding "<task>" followed by 2000 spaces and no close tag took
about 4.4 s in shouldAsk, and a sed or awk command whose pattern holds a
run of 42 backslashes took about 8 s in parseBashBulk. Both matches run
synchronously inside a hook, so the hook's timers could not fire.

Both regexes let two branches consume the same characters: the wrapper
regex put \s* on each side of a lazy body that also matches whitespace,
and the sed and awk regexes let a backslash match either as the start
of an escape or as a plain character, so a failed match retried every
split of the run.

The wrapper block is now found by matching the open tag, then scanning
for its close tag at most once per tag name, and trimming the body. The
sed and awk regexes use disjoint alternatives, (?:\\.|[^\/\\])+, as the
gh issue search parser already does. Both plugin manifests go to 1.2.13.

Tests: a timing check for each case (under 500 ms; on main they take
seconds), a wrapped block with surrounding whitespace that is still
extracted trimmed, and sed and awk patterns with an escaped slash that
still parse to the grep shape.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…dy is trimmed

The wrapper-block tests now cover an earlier unclosed tag being skipped,
tags pairing across case, the first open tag pairing with the nearest
close, and a body that falls under the minimum length only once trimmed.
Each of these fails if the matching loop stops at the first unclosed tag,
matches the close tag case-sensitively, takes the farthest close, or drops
the trim. The comment on wrappedBlock now states what bounds its cost.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ep shape

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 6fc36c8 into main Nov 7, 2026
34 checks passed
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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.

1 participant