Skip to content

code.search, tracker.ask and the decision extractor match identifier regexes in linear time - #342

Merged
zlareb1 merged 7 commits into
mainfrom
fix/identifier-regexes-linear
Nov 7, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
fix/identifier-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):

code.search (and the code grounding of memory.ask), tracker.ask and the decision extractor read identifiers in linear time, so a query holding a long run of capitals or digits no longer blocks them for seconds or minutes. memory.ask's own identifier patterns are fixed in #317.

Why

Five patterns (two in EMBEDDED_IDENT_RE, one each in the tracker, the decision extractor and the fake model) repeated a group whose body could also continue the previous repetition. In (?:[A-Z][a-zA-Z0-9]*)+ a capital can end one hump or start the next, so on a run of capitals followed by a word character (where the trailing \b fails) the engine tries every way to split the run: about 2ⁿ for n characters. The calls run synchronously on the request's event loop.

Pattern Reached from Failing input Measured on main
EMBEDDED_IDENT_RE (server/code/domain/idents.py) every code.search query (no length limit) and memory.ask code grounding (question and excerpts) "Ab" + "A" * n + "_", "ab" + "A" * n + "_" 8.9 s at n = 28, ×2 per character
_CAMEL_RE (server/tracker/domain/catalogue.py) tracker.ask anchor extraction "Ab" + "0" * n + "_" 21 s at n = 28
_BARE_CAMEL (server/memory/domain/decision_extract.py) the decision extractor's mention sort as above never finishes at 2,000

The same shape is in the fake model's _CAMEL_RE (server/llm/fake.py), fixed with them.

Each pattern is rewritten as the same language with one way to match. A repeated hump that may contain capitals absorbs every later hump, so X(?:[A-Z][a-zA-Z0-9]+)+ is X[A-Z][a-zA-Z0-9]+. The tracker's [A-Z][a-z0-9]+(?:[A-Z0-9][a-z0-9]*)+ is "a capital, a lowercase letter or digit, then any letters and digits holding at least one capital or digit", written [A-Z][a-z0-9][a-z]*[A-Z0-9][A-Za-z0-9]*.

Scope and risk

memory.ask's _INJECT_HANDLE_RE and _TECH_PASCAL_RE (server/memory/app/ask/identifiers.py) have the same shape; open PR #317 rewrites them, so this PR leaves them out.

Matches are unchanged: for each pattern here, old and new give the same spans and groups on 300,000 random strings of 1 to 14 characters over capitals, lowercase, digits, _, :, ~, -, space and a non-ASCII letter (the script is below). Recall and ranking are therefore untouched, so no benchmark is needed. idents.py and catalogue.py are not in the code-index extractor stamp, so no index stage re-runs.

Equivalence check
import random, re
pairs = {
    "embedded": (
        r"\b(?:[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_~][A-Za-z0-9_]*)+|[A-Z][a-z0-9]+(?:[A-Z][a-zA-Z0-9]*)+|[a-z][a-z0-9]*(?:[A-Z][a-zA-Z0-9]*)+|[a-z][a-z0-9]*(?:_[a-z0-9]+)+)\b",
        r"\b(?:[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_~][A-Za-z0-9_]*)+|[A-Z][a-z0-9]+[A-Z][a-zA-Z0-9]*|[a-z][a-z0-9]*[A-Z][a-zA-Z0-9]*|[a-z][a-z0-9]*(?:_[a-z0-9]+)+)\b",
    ),
    "tracker_camel": (r"\b([A-Z][a-z0-9]+(?:[A-Z0-9][a-z0-9]*)+)\b", r"\b([A-Z][a-z0-9][a-z]*[A-Z0-9][A-Za-z0-9]*)\b"),
    "bare_camel": (r"^[A-Z][a-z0-9]+(?:[A-Z][A-Za-z0-9]*)+$", r"^[A-Z][a-z0-9]+[A-Z][A-Za-z0-9]*$"),
    "fake_camel": (r"\b[A-Z][a-z0-9]+(?:[A-Z][A-Za-z0-9]*)+\b", r"\b[A-Z][a-z0-9]+[A-Z][A-Za-z0-9]*\b"),
}
alpha = "ABZabz019_ :~-é"
rng = random.Random(7)
for name, (old, new) in pairs.items():
    o, n = re.compile(old), re.compile(new)
    for _ in range(300_000):
        s = "".join(rng.choice(alpha) for _ in range(rng.randint(1, 14)))
        assert [(m.span(), m.groups()) for m in o.finditer(s)] == [(m.span(), m.groups()) for m in n.finditer(s)], (name, s)
    print(name, "same")

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: revert.

Tests

  • tests/unit/test_identifier_regexes_linear.py (new): each caller on a capital, camel or digit run sized to take the old pattern 2 to 10 s (25 to 40 characters) must take under 1 s of CPU, so a regression fails in seconds rather than hanging the job. On main every case fails; here they take microseconds. A second test pins what the shapes still match.
  • Every unit test file that imports a touched module: 1,206 passed.
  • make format-check, ruff, mypy on the touched modules: pass.

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; the timings, the language equivalence and the fuzz check were verified by hand.

  • 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
zlareb1 force-pushed the fix/identifier-regexes-linear branch from 22d4a70 to 0f54a3d Compare November 7, 2026 10:26
@zlareb1 zlareb1 changed the title Identifier regexes over a question, a code query or an excerpt match in linear time code.search, tracker.ask and the decision extractor match identifier regexes in linear time Nov 7, 2026
zlareb1 and others added 7 commits November 7, 2026 12:00
…in linear time

Five patterns repeated a group whose body could also continue the previous repetition, such as (?:[A-Z][a-zA-Z0-9]+)+, so a run of capitals or digits ending in a word character backtracked through every split: 'Ab' + 'A' * 40 + '_' blocked memory.ask for seconds, code.search's embedded identifiers doubled per character, and tracker.ask's anchors took 21 s on 28 digits. Each is rewritten as the same language with one way to match, e.g. [A-Z][a-z]+[A-Z][a-zA-Z0-9]+; old and new give the same matches on 300,000 random strings each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…tern in seconds, not hang

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]>
…path regex change

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the fix/identifier-regexes-linear branch from cc36b71 to e105a37 Compare November 7, 2026 12:00
@zlareb1
zlareb1 merged commit 5aef52b into main Nov 7, 2026
38 checks passed
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