Repository navigation
The prompt-pack and code-shunt hooks match their tag and pattern regexes in linear time - #365
Merged
Merged
Conversation
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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.mjsWRAPPED_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 andwrappedBlock, 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,(?:\\.|[^\/\\])+, asghIssueSearchInCommandalready writes it. A lone backslash no longer stands for itself before the closing/:sed -n '/a\/p'andawk '/a\/'used to parse to a grep fora\; 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
Tests
tests/prompt-pack.test.mjs:<task>+ 2000 spaces under 500 ms (8.8 s onmain, 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 onmain, 1.1 ms here);sed -n '/a\/b/p'and the awk form still parse to the grep shape with patterna/b; the unterminatedsed -n '/a\/p'andawk '/a\/'parse to nothing.node --teston 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 typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited.frontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code