Skip to content

Request-path regexes read long input in linear time - #317

Merged
zlareb1 merged 36 commits into
mainfrom
sec/request-path-regexes
Nov 8, 2026
Merged

zlareb1 merged 36 commits into
mainfrom
sec/request-path-regexes

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (data loss, cross-tenant read, auth bypass)

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 regexes that read request text on the event loop (dashboard asset names, memory.set file detection and its stack-trace hint, code.verify_citations, code.search, code.trace, tracker.similar and tracker.duplicate_check frames, memory.ask, memory.set_from_messages, knowledge.search, loom_search, SCIM group filters and others) run in time linear in their input. tracker.similar and tracker.duplicate_check refuse a frame, symbol or path over 4,000 characters, and GET /assets/ treats a file name over 255 characters as not a hashed asset and answers 404 for a path over 1,024 characters.

Why

A scan timed every compiled regex the server loads on long repeats of each character class, and flagged about sixty whose time grows with the square (or worse) of the input. A triage traced each to its callers. Eighteen run on the API's event loop over text a caller sends, so one request blocks every other request on that server for its duration:

  • GET /assets/<name> needs no token: a 64 KB name took 3.5 s, about 870 s at 1 MB.
  • code.verify_citations (viewer, text up to 200 KB): 85 s, and 38 s at 80 KB for a GitHub blob URL.
  • memory.set file detection (writer, value up to the 32 MiB body): 25 s at 64 KB; its stack-trace hint, 19.9 s at 32 KB of blank lines.
  • memory.set_from_messages forget sentences: 35 s for a 64 KB sentence, and exponential in a repeated "no wait" (1.8 s at 20 repeats); memory.ask amounts: 17.6 s at 16 KB of digits, through rows a writer stored.
  • tracker.similar / duplicate_check (viewer, 60 frames of any length): 101 s.
  • knowledge.search exact-key terms (no query length cap): 5.7 s at 32 KB.
  • code.search qualified names; activity narration (cubic on whitespace in one clause) and its failed-attempt clause; memory.ask handle and PascalCase identifiers in the question (exponential: about 4 s at 42 characters, minutes at 50); conflict parentheticals, value-update tokens, memory.ask identifiers, SCIM /Groups filters (SCIM token), replay result lines, registration class names: from 1 s to 18 s on inputs a caller controls.

Two more scans, of the patterns under server/memory/domain and server/code/domain (845) and under server/insights and server/knowledge, timed at 4 KB and 16 KB and projected to 200 KB, found the rest on request paths: search and ask evidence enumeration lines, question and query trailing punctuation, memory.ask number words and question parts, the stack-trace hint's frame lines and Assertion … failed head, knowledge.search terms, and the transcript cue tag and voice patterns of the recording source sync (a worker, fixed while there).

Design

Each pattern that rescanned a run from every character now starts a match only where one can begin: a lookbehind at the start of a word, a path's first directory, a token or a run of blanks, or a possessive or bounded repeat where the start cannot be fixed. Where a lookbehind would change which names are read, the rewrite skips a leading run and returns the name in a group, and each caller reads that group for that pattern, so matches on ordinary input are identical. The forget prefix lists "no wait" no more: it is read as "no" then "wait", and as its own word too a run of them parsed in 2^n ways.

Some costs were in the code around a pattern:

  • An asset name over 255 characters is not a hashed asset (no file name is longer), and an asset path over 1,024 characters is a 404 before any disk or ClickHouse read (resolving it on disk cost its length times its depth).
  • A forget request's clause is read to its last non-blank in one pass, and the replacement cut reads a blank run from its first blank, so each sentence under the 400-character cap costs its length (the cap bounds a sentence, not how many a message holds). forget_targets reads the content words of its candidate rows and batch facts once, on the first request that needs them, not once per request; replacement_facts and its said-corrections read what was asked to forget once; the write step resolves at most the latest 50 of a window's forget requests by content (each costs a candidate read and a pass over the candidates; it logs when a window holds more, and the result's forget_requests counts the ones resolved), and the extractor's own forget entries are resolved as before. A window states at most 50 premises (premise_facts; each is compared with every earlier one). A forget sentence over 400 characters is skipped by forget_sentences, as forget_requests_from_messages already skips it. A message's other sentences are added once, not once per request in it, and a request sentence no longer counts as another request's other sentence.
  • memory.set file detection reads its commit cue once per text, not once per hex run.
  • A path's leading ./ and diff a//b/ prefixes are stepped past and sliced once (memory.set file references, code.verify_citations, the code plane's path helpers, through one helper in server/core/relative_paths.py): a slice per prefix copied the rest of the path each time.
  • code.verify_citations retries a name that found nothing only as its suffixes of at most 8 :: parts: each retry is one lookup, and a cited name has no length bound of its own.
  • code.bisect strips a bot's marker blocks from an issue body against closers indexed once, and its HTML comments only up to the last -->, so an opener with no closer is not a scan to the end (32 KB of unclosed comments: 1.3 s before (16x per 4x); 64 KB after: 4 ms).
  • code.verify_citations finds the fence that may hold a citation by bisection over the sorted fences, not by walking every fence for every citation.
  • memory.ask amounts read a sentence's entities once and find each amount's neighbours by position.
  • A GitHub blob URL's revision is one segment: a path after a longer revision is also a path after the first segment, so a longer one was never chosen, and trying each re-read the path after it.
  • The stack-trace hint's Assertion … failed head is read once per line by a string search instead of a pattern that rescanned the line from each Assertion .
  • A transcript cue's tags are read up to its last >: no tag closes after it.
  • tracker.similar and tracker.duplicate_check frames, symbols and paths get a 4,000-character limit per item, since the work is linear in their total; loom_search's tracker leg and code.bisect's fix check drop a longer frame instead of failing, through one filter beside the bound.

Each rewrite was fuzzed against the original on 20,000 to 100,000 random strings and on real samples; eleven of them (TRACE_SYMBOL_RE, _TRACE_LINE_RE, _MIXED_RE, _PATHLIKE_RE, exact_keys._TERM, defect_answer._SEGMENT_SPLIT_RE, ask_threshold._NUM_RE and _PART_SPLIT_RE, prompts._AMOUNT_RE, forget_requests._REPLACEMENT_CUT_RE, activity_facts._FAILED_ATTEMPT_RE) keep that check as a seeded 2,000-string unit test against the original patterns, and the rest rest on their modules' tests and the fuzz runs. Results are identical except for these intended differences on unusual input:

  • a file, path or qualified-name match that began right after @, + or ::~ inside a longer token is no longer read;
  • a URL scheme is read up to 32 characters;
  • a WebVTT voice span's class ends at any blank, so <v.loud\tx Alice> names x Alice (the recording source sync, a worker);
  • a cited name of more than 9 :: parts is retried only as its suffixes of at most 8 parts;
  • a narration whose "about" topic is blanks only ("about returned") is no longer read as a tool-result narration;
  • a pytest id or test_ name whose [param] holds a [ is read without the param;
  • a GitHub blob URL with an empty segment (blob/main//src/a.py) is not read as a blob URL;
  • a path glued to the previous frame's or file's line number (x/a.cpp:12/b/c.cpp:3) is not read;
  • a narration's path-like token glued right after a backtick span is read with each run around its separators at most 63 characters;
  • a window with more than 50 forget requests read from its messages resolves the latest 50 by content (a repeated request counts at its latest mention) and counts those in forget_requests (the extractor's own forget entries still resolve), a window with more than 50 assistant premises keeps the first 50, and a forget sentence over 400 characters is skipped (and kept out of the message's other sentences when it opens like a request), and a message with two or more forget requests returns each request's replacement and its other sentences once;
  • a frame, symbol or path over 4,000 characters is refused by tracker.similar and tracker.duplicate_check, and dropped by loom_search's tracker leg and code.bisect's fix check;
  • an asset name over 255 characters is not cached as hashed.

A regression guard, tests/unit/test_request_regexes_linear.py, times every fixed pattern with the call its code makes at n and 4n and requires under 8x growth (a quadratic one grows 16x), and checks each new bound. Each read is capped at 10 s, so an exponential regression fails its case instead of hanging the job. With the old patterns swapped in, every timing case fails (15 to 16x, 65x for the cubic one); the exponential ones do not finish.

Left as they are, each with its bound:

  • memory.ask question patterns that cross lines or rescan a digit run (ask_option_guard option and date lines, question_literals state tails, the split_parts date line, ask_threshold's threshold and option lines): the question is at most 2,000 characters, 10 to 70 ms.
  • Patterns over model output (ask_recount, premise_line, corpus_synthesis, ask_threshold item heads, ask_option_guard answer lines, extraction_specifics, extraction_temporal, session_brief): bounded by the model's output tokens.
  • Worker paths (merge_guard in consolidation; session_echoes and durable_facts in extraction): off the request path.
  • The mentor's identifier check (the query is at most 200 characters) and the dataset duration and row-range shapes (read with fullmatch, linear).
  • The code-index parsers under server/code/domain/extract/ and the extractor_stamp modules (a code change there re-runs ingest stages in every repository): their input is a repository's own files, capped at 1 MB, read in the ingest worker.
  • sql_names._STATED_CLASS_RE on a line of many <…> groups or unclosed <: code chunks are capped at 6,000 characters, about 8 ms a line; its whitespace case is fixed.

Scope and risk

40 server files: the regex rewrites (45 patterns) and the bounds and loops described above, their callers, the new core/relative_paths.py helper, the forget_requests and premise_facts result fields' comments, and one tool description said in fewer words (the frame and path bounds add about a dozen tokens to the coding listing, whose ceiling test_mcp_listing_budget.py holds); plus the regenerated docs/reference/operations/memory.md and tracker.md rows, a clause on the forget cap in docs/features/memory/extraction.md, the asset-path case in test_frontend_asset_fallback.py, the code.bisect fix-check case in test_dupcheck_links.py, the guard test, three cases in test_forget_and_switch.py, and test_memory_pipeline.py's content-match tests moved off forget_targets_by_content, which no server code calls any more and is removed. The seven new bounds carry # not a setting: with their reason. No stored data or schema changes. The blacklist and sensitivity regexes are in a separate pull request.

Compatibility, rollout and rollback

  • Wire: tracker.similar and tracker.duplicate_check answer 422 for a frames, symbols or paths item over 4,000 characters. GET /assets/<name> with a name over 255 characters is served as a non-hashed path (no long cache), and a path over 1,024 characters answers 404. memory.set_from_messages resolves by content, and counts in forget_requests, at most a window's latest 50 forget requests.
  • Operator steps: none.
  • Rollback: revert.

Tests

tests/unit/test_request_regexes_linear.py has 57 timing cases over 44 call sites, 16 bound cases (the premise cap, asset name, forget sentence and a long request beside it, forget sentences with blank runs, forget_targets over many requests, qualifier retries, ./ runs on four callers, frames on two inputs, symbols, paths, the tracker leg, the shared frame filter) and 11 seeded equivalence cases; test_forget_and_switch.py adds a message with two forget requests (its other sentences kept once), a correction older than the latest 50 forget requests (it keeps its replacement while the per-request reads stop at 50), and a repeated request resolved at its latest mention. On the head rebased onto main, 162 unit files run serially (3,389 tests: every MCP listing, budget and schema test, test_tunable_constants.py, test_public_copy.py and each touched module's) pass, as do make format-check, make lint, make typecheck and the reference and SDK generator checks. Per-pattern timings before and after, on one host:

Pattern Before After
frontend_assets._HASHED 3.5 s at 64 KB 0.0 s (name cap)
citations._BLOB_URL_RE 38 s at 80 KB 0.027 s at 200 KB
citations._BARE_LOC_RE 44 s at 200 KB 0.005 s
citations._NAME_RE 0.82 s at 16 KB 0.025 s at 200 KB
facets commit cue 0.86 s at 32 KB 0.045 s at 200 KB
forget_requests._FORGET_MSG_RE 1.8 s at 20 repeats, 4x per 2 0.0005 s at 500
_FORGET_MSG_RE tail and _REPLACEMENT_CUT_RE, 1 MB of 400-character requests with blank runs (forget_requests_from_messages + forget_sentences) 6.9 s 0.19 s
forget_targets, 8,000 requests 3.8 s (16x per 4x) linear: candidates read once (a window's message requests are also capped at 50)
premise_facts_from_messages, 400 assistant turns of premises 12.8 s (14x per 4x) 0.006 s (capped at 50)
identifiers._INJECT_HANDLE_RE about 4 s at 42 characters, 1.6x per character 0.0016 s at 200 KB
activity_facts._FAILED_ATTEMPT_RE 4.2 s at 8 KB 0.008 s at 200 KB
activity_facts._TOOL_SUBJECT_RE 1.4 s at 300 characters (cubic) 0.02 s at 64 KB
activity_facts._PATHLIKE_RE 3.2 s at 16 KB 0.4 s at 1 MB of backtick-led starts, its worst shape
mentor_rules._TRACE_LINE_RES 19.9 s at 32 KB 0.02 s at 200 KB
exact_keys._TERM 5.7 s at 32 KB 0.027 s at 200 KB
prompts._AMOUNT_RE 17.6 s at 16 KB 0.03 s at 200 KB
prompts.amount_candidates 2.2 s for 2,000 amounts 0.08 s for 25,000
defect_answer._SEGMENT_SPLIT_RE 4.5 s at 32 KB 0.011 s at 200 KB
passage_trim._ENUM_LINE_RE 0.58 s at 8 KB 0.006 s at 200 KB
query_trim._QUALIFIED_RE 2.6 s at 16 KB 0.018 s at 200 KB
observe_replay._RESULT_LINE 5.0 s at 16 KB 0.010 s at 200 KB
sql_names._STATED_CLASS_RE 6.4 s at 16 KB 0.001 s at 64 KB

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. Each rewrite was timed and fuzzed against the original; I read the diff and the guard test.

  • 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: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • 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 (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) 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; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 27 commits November 8, 2026 01:36
Each pattern that rescanned a run of text from every character now starts
a match only where one can begin: at the first letter of a word, the start
of a path's first directory, or the start of a token. Matches on ordinary
input are unchanged. The rewrites that add a lookbehind return the name in
group 1, and their callers read that group.

- GET /assets: a file name over 255 characters is not a hashed asset.
- memory.set file detection, code.verify_citations, code.search trims,
  code.trace, tracker.similar and tracker.duplicate_check frames,
  memory.ask amounts and identifiers, activity narration, conflict and
  value-update tokens, SCIM group filters, replay result lines,
  identifier tokens and registration class names.
- A forget sentence over 400 characters is skipped by forget_sentences
  too, as forget_requests_from_messages already skips it.
- tracker.similar and tracker.duplicate_check refuse a frame or symbol
  line over 4000 characters.

Intended differences: a file or path match that began right after `@` or
`+` inside a longer token is no longer read, a URL scheme is read up to 32
characters, a tracker path glued to the previous frame's line number is
not read, and a memory.ask file name or a narration's path-like token over
255 characters is read in part.

tests/unit/test_request_regexes_linear.py checks every fixed pattern
grows under 8x from n to 4n, and checks each new bound.

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

- memory.ask's PascalCase handle and type patterns read the humps after
  the first as one run: a word could split into humps in many ways, and
  trying each was exponential in its length.
- The failed-attempt check reads each clause once, from its first
  attempt word.
- The file:line and pytest path::name patterns start at a path's first
  word; the name is group 1.
- The stated-class pattern reads a run of blanks once.
- tracker.duplicate_check's description drops a closing clause, so the
  coding listing stays under its token ceiling with the frame bounds.

Matches are unchanged on ordinary input. One intended difference: a
file:line glued to the previous one's line number (`a.cpp:12/b.cpp:3`)
is no longer read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…st-path regexes read in linear time

- code.verify_citations: a blob URL's rev is at most four segments, so
  each one tried no longer re-reads the whole path after it.
- memory.set file detection reads the commit cue once per text, not once
  per hex run.
- forget_sentences adds a message's other sentences once, however many
  forget requests it holds.
- amount_candidates reads a sentence's entities once and finds each
  amount's neighbours by position.
- The tracker defect split, sensitivity's email shape, enumeration
  lines, trailing punctuation, number words and part splits start a match
  only where one can begin.
- The pytest-id and file:line loops read group 1 for the one pattern that
  has it, by name.
- Path-like tokens are read over at most 63 characters right after a
  backtick, 255 from a token's start.
- loom_search's tracker leg drops a frame longer than
  tracker.duplicate_check accepts instead of failing the leg.

Matches are unchanged on ordinary input. Intended difference:
forget_sentences returns a message's other sentences once when the
message holds two or more forget requests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The sensitivity detectors' patterns are changed in a separate pull
request; this one no longer edits the email shape or guards it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nd stack-trace hints read in linear time

- The forget prefix no longer lists "no wait": it is read as "no" then
  "wait", and as its own word too a run of them parsed in 2^n ways.
- code.verify_citations does not start a qualified name at a
  destructor part (`::~Foo`).
- knowledge.search tries an exact-key dotted path once per run, from
  its first letter; the term is group 1 or group 2.
- A transcript cue's tags are read up to its last `>`.
- The memory.set stack-trace hint reads each line's own leading blanks,
  and a frame's name run once.
- code.bisect's fix check drops a frame longer than
  tracker.duplicate_check accepts, as loom_search's tracker leg does.
- The new length bounds say why they are not settings.
- A seeded test checks four rewrites against the patterns they replaced,
  and a forget test covers a message with two requests.

Matches are unchanged on ordinary input. Intended difference: a
qualified name starting right after `::~` inside a longer token is no
longer read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…checks six more rewrites

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… shared frame filter

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…and file names read from a token's start

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… the other sentences, and one helper strips leading ./

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…he batch once

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…rds are read only when a request needs them

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…he requests a window holds

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…those; each forget entry's words are read once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…_requests says what it counts

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

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…atch tests go through forget_targets

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ge's facts

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ut read plainer

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… overlap builds each suffix once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…x check's filter is tested at its call

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 9 commits November 8, 2026 01:36
… comment names symbols

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ved on disk

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… keeps each file with its tail

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… on its result field

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]>
…s the stored rows

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]>
…mment states the present

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the sec/request-path-regexes branch from fbd102f to ebd03c3 Compare November 8, 2026 01:38
@zlareb1
zlareb1 merged commit 1c8e255 into main Nov 8, 2026
37 of 38 checks passed
@zlareb1
zlareb1 deleted the sec/request-path-regexes branch November 8, 2026 01:50
@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