Skip to content

Code-index lane walks read a file in linear time - #448

Merged
zlareb1 merged 21 commits into
mainfrom
sec/lane-walks-linear
Nov 10, 2026
Merged

zlareb1 merged 21 commits into
mainfrom
sec/lane-walks-linear

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 9, 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 code index reads Markdown, Python layout manifests (setup.py, pyproject.toml, conftest.py sets), Dockerfile, Jenkinsfile, Angular, Vue, Svelte, Astro and dbt files, CMake command rows, and route and environment-variable facets, in time linear in the file's size. A crafted file, in an indexed repository or in the files a request posts for the PR overlay, could hold a serving replica or the indexer for minutes to hours.

Why

The language lanes outside tree-sitter walk a file with regular expressions and Python loops, and the parse deadline does not cover them. Several walks cost more than linear time on input a repository or a pr_files caller controls:

  • The Markdown heading pattern ^(#{1,6})\s+(.+?)\s*#*\s*$ backtracks cubically on one heading line with a long run of spaces. One re.match call holds the GIL, so the whole process stops, event loop included: about 1 s at 1,000 spaces, hours at 20 KB. The PR overlay parses posted Markdown files on the serving replica, so any reader of a code namespace reaches it.
  • Quadratic walks: Markdown section ends (a slice of the remaining headings per heading, in both callers), link includes (a list membership test per link) and qualified names and heading paths (each repeats every ancestor's title, of any length); Svelte and Astro event attributes (a regex search from each on of a | modifier chain); setup.py console_scripts lists (each unclosed list scanned to the end, then rescanned from every later match, on the event loop); Dockerfile rows (continuation rows re-gathered per row), --flag stripping (a slice per flag) and stage ends (a scan of every FROM per stage); a line count from the top of the file per site in Angular, Vue and Svelte templates, Angular component classes, Jenkinsfile steps, setup.py entry points, Python routes, and environment-variable reads in Python and JavaScript/TypeScript; a router or controller prefix of any length joined into every route under it, and joined again on every include_router call; a route path of any length copied once per entry of its methods list; Angular inline templates (a search from the top of the file per class); dbt tags=[ without a closing ], route methods=[, and include_router(, add_url_rule( and Route( calls without a closing ) (a regex scan to the end from each opener); the restore of {% raw %} blocks (a whole-text replace per block); Vue macro members (pop(0) per node); CMake commands (a slice of the rest of the file per row); Python layout roots from pyproject.toml, setup.py and conftest.py (a list membership test per root).

Each fix is the smallest one that makes the walk linear:

  • The heading pattern no longer matches the closing #s; rstrip removes them, with the same result as the old pattern on every line.
  • Loops run by index or resume where the last step stopped; line numbers come from one newline-offset list and bisect (one shared helper, text_lines.py, which replaces the copies in lang_sql_jinja.py and ts_routes.py), or a count carried forward; an Angular inline template's line comes from its own offset; the bracket and call-argument patterns stop at the next opener or the next same call; a Python route path is cut at 400 characters before it is joined to each method, a router's or NestJS controller's prefix at 400 characters after its leading slashes, twice what a route name keeps, and a TypeScript route name at 200 as a Python one is; raw blocks are restored in one re.sub; the Vue member walk pops from the end of a reversed stack; Dockerfile flags are stripped by one anchored possessive pattern; a CMake command is matched at its offset.
  • A Dockerfile row that is not an instruction now skips its continuation rows, as Docker reads them; before, each continuation row was tried as an instruction.
  • setup.py list scans resume after the list they scanned, so a "console_scripts": [ inside an unclosed list is not read twice.
  • The Python layout (layout_from_texts) runs in a thread, off the event loop, one read per namespace at a time; its roots are deduplicated through a dict.

Scope and risk

  • Output is otherwise unchanged. The intended differences:
    • A Dockerfile row that is not an instruction (a lowercase instruction among them, which the lane does not read) keeps its \ continuation rows; before, a continuation row that starts with an uppercase word was read as an instruction.
    • A setup.py entry-point list inside one the scan leaves open is read once, as part of the outer list. The scan counts brackets without skipping strings or comments, so an unmatched [ in a comment inside a console_scripts list now files a following gui_scripts entry under console_scripts only; before, it was filed under both.
    • An Angular inline template's line comes from its own position; before, it came from the first copy of its text anywhere in the file (a comment, a string, another class).
    • A Markdown heading title, qualified name and chunk heading path are cut to 1,000 characters, a cut title or qualified name ending in a hash of the whole so it stays distinct (the longest of the 8,252 headings in the files below has 680 characters, the longest qualified name 804).
    • A Svelte or Astro event with camel-case modifiers (on:submit|preventDefault={save}) names its handler through the event pattern; before, only a same-file function was found, through the prop pattern, so stored edges do not change. An on…= attribute written directly after a | (a|onclick={f}) is not read.
    • A CMake command with a line break between its name and ( counts that break in its own end line, and no longer shifts the line numbers of the commands after it.
    • A TypeScript route name is cut to 200 characters, as a Python one already was.
    • A Python add_url_rule( or Route( call whose arguments hold another opener of the same call before the ), or an include_router( call that holds one before its prefix= (one in a comment, say), is read from the inner opener; before, the outer call read on past it.
    • A dbt tags list or a route methods list with a bracket inside it gives no tags (or the default method); before, it read the list up to the first closing bracket.
  • py_layouts.py is in the extractor stamp (edge stage, Python family): the next round re-derives the edge rows of Python files once, and the chunk and guard rows that derive from them, with the same result apart from the nested setup.py list difference above. The other modules are outside the stamp. Rows already stored keep the old values of the intended differences until their file is ingested again; loom code backfill --paths-file corrects them sooner.
  • Left alone: the request-text patterns in answers.py, answers_cache.py, history.py and message_grep.py. Their inputs are capped (2,000 to 20,000 characters) and they cost at most 30 ms at the cap.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rolling upgrade: no stored format changes.
  • Rollback: reverting brings the slow walks back.

Tests

  • tests/unit/test_code_regex_cost.py covers the new shapes. A # a prefix with a run of spaces, and a <word>=[ unit per word a pattern spells, catch the heading, dbt tags and route methods patterns. test_a_lane_walk_costs_linear_time runs each of 32 walks (tests/unit/lane_walks.py) at 100,000 and 400,000 characters in a child process, which arms a 60 s faulthandler dump and has a 120 s timeout, so a GIL-holding regression fails with the child's stack instead of hanging the suite. It asks for the 4N time to be under 5 s and under 8 times the N time, or under 0.1 s. On main all 32 fail (the heading line with spaces and the setup.py lists by the deadline). The Python router-prefix cut stays under the ratio on main at these sizes, so test_code_py_symbols.py checks the stored prefix's length instead. With the fix all pass. The Python route walks time the lane's call-site scans, not the file's tree-sitter parse, so the bound measures the walk.
  • tests/unit/test_code_py_layouts.py::test_the_layout_is_read_off_the_event_loop fails on main and passes here; test_files_that_miss_the_layout_cache_at_once_share_one_read checks that concurrent misses share one read in the thread. The intended differences have a test each in test_code_host_islands.py, test_code_py_layouts.py, test_code_lang_angular.py, test_code_sql_jinja_dbt.py, test_code_py_symbols.py, test_lang_markdown.py, test_code_lang_svelte_astro.py, test_lang_cmake.py and test_code_routes.py.
  • The same output: every Markdown, SQL, HTML, CMake, Vue, Dockerfile, Jenkinsfile and manifest file in this repository and the frontend's dependencies (920 files), read with every changed function before and after, gives identical results, as do the route, environment-variable and inline-template facets of the 16,394 Python, TypeScript and JavaScript files there and the Python layout of their manifests. A differential run against the main modules over 3,000 random token mixes per lane, and 200,000 random heading lines and flag strings, also matched, apart from the Dockerfile continuation rows and the nested setup.py list above.
  • The lane test files (test_lang_markdown.py, test_code_host_islands.py, test_code_lang_angular.py, test_code_vue_lane.py, test_code_lang_svelte_astro.py, test_code_sql_jinja_dbt.py, test_lang_cmake.py, test_code_py_layouts.py, the route tests and the others that import these modules) pass. make format lint typecheck passes.

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 cost was measured in a child process before and after the fix, and the output was compared on real files and against the previous modules.

  • 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. (none touched)
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (none)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (none)
  • The frontend builds, if frontend/ changed. (unchanged)
  • 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 sec/lane-walks-linear branch 3 times, most recently from 21bc1b5 to e488af1 Compare November 9, 2026 23:13
zlareb1 and others added 21 commits November 10, 2026 02:14
A Markdown heading's closing hashes are stripped in Python instead of by a pattern that backtracks cubically on a run of spaces while holding the GIL. Section ends, includes, setup.py entry points, Dockerfile rows, flags and stages, Jenkinsfile steps, template site lines, Vue macro members, dbt tags and raw blocks, CMake commands and route methods no longer rescan, slice or count the file from the top per item. setup.py layouts are read off the event loop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The Python route call patterns stop at the next same call, routes and environment reads take their lines by bisection through one shared helper, and an Angular inline template takes its line from its own offset.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A heading title is cut to 1,000 characters, since every descendant's qualified name repeats it, and a Svelte event attribute is not searched from inside a modifier chain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Each repeats every ancestor's title, so deep sections under long titles grew the output with depth times title length per section.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…camel-case modifiers name their handler

Qualified names and heading paths are cut to 1,000 characters, a cut name ending in a hash of the whole. The event pattern reads camel-case modifiers, and a CMake command's line breaks before its `(` are counted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…efix cut is checked directly

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… prefix is cut after its leading slash

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…cut in one place

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]>
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]>
…over each per-method cut

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]>
@zlareb1
zlareb1 force-pushed the sec/lane-walks-linear branch from 3572a08 to 761e2ea Compare November 10, 2026 02:14
@zlareb1
zlareb1 merged commit 574bc91 into main Nov 10, 2026
36 of 38 checks passed
@zlareb1
zlareb1 deleted the sec/lane-walks-linear branch November 10, 2026 02:42
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