Skip to content

Code-index extractors and PR text read a file in linear time - #463

Merged
zlareb1 merged 18 commits into
mainfrom
sec/code-index-linear-walks
Nov 10, 2026
Merged

zlareb1 merged 18 commits into
mainfrom
sec/code-index-linear-walks

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Related: #448

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 CMake files, Python manifests and entry points, Markdown references, the import candidate lists of Python, TypeScript, Rust, Go and Protobuf files, and the HTML comments of PR descriptions in time linear in their size. A crafted file in an indexed repository, a file posted for the PR overlay, or a PR description could hold a replica or the indexer for seconds to hours, or exhaust its memory.

Why

#448 made the lane walks linear. These walks, outside its diff, still cost more than linear time on input a repository, a pr_files caller or a PR author controls:

  • CMake: each ${VAR} use expanded to every entry of VAR, building one builds facet per use and entry. One 32 KB file built 12.8 million facets in 36 s; at 1 MB the parse runs out of memory. The PR overlay parses posted CMake files on the serving replica, so any reader of a code namespace reaches it. Each function / macro also scanned every later command for its end….
  • Python layouts: each Python file of a pass checked every entry point's import candidates under every root (15.8 s per file with 71,000 entry points and 64 roots), and a module named by every entry point got one facet per entry point, in every manifest. configparser reads setup.cfg, pytest.ini and tox.ini with an option pattern that backtracks quadratically on a whitespace run before a line's delimiter, inside one regex call that holds the GIL (7 s at 20,000 spaces, hours at the 1 MiB manifest cap). An empty manifest read from the mirror was not cached, and so neither was the layout: every Python file of a pass read the manifest again and rebuilt the layout from up to 200 manifests.
  • Markdown: every mention of an ambiguous name scanned all its definitions (sites times definitions).
  • PR descriptions: <!--.*?--> scans to the end from each unclosed <!--, inside one regex call (0.5 s at the 20,000-character code.pr_check cap, 5.3 s at a PR body's 65,536). The linked-issue pattern \s*:?\s* splits a run of spaces after fix every way (2.7 s at 20,000 characters, 29 s at 65,536).
  • Import probes and lane includes: every candidate path was deduplicated by in over a growing list, on the event loop (a Python file of 4,000 imports took 45 s, a TypeScript one 25 s, a Markdown file of 100 KB of links 11.7 s). A Rust use path joined and deduplicated every prefix of its segments before keeping the first 12. A from <long module> import a, b, … statement, or a Rust use <long prefix>::{a, b, …}, built one copy of the module or prefix per member (466 MB for a 169 KB Python file; 1.3 GB for a 100 KB Rust file).

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

  • CMake variables keep only the entries that give a builds facet and expand lazily, a source argument longer than 4,096 characters gives none, and sources stop being added once a file has 10,000 facets; a function's end comes from one list of end… commands, bisected.
  • A Python file looks up the entry points it serves by its own module stems under each root; the layout keeps the entry points by stem, and at most 10,000 of them.
  • Before configparser reads a manifest, the whitespace runs inside the text before each line's first = or : are cut to one space and the run right before the delimiter dropped, indent kept.
  • A bare name's candidates are sorted once by reversed qualified name, so the ones a qualified mention ends with are found by bisection.
  • HTML comments are dropped with str.find, stopping at the first <!-- with no --> after it, as the pattern did; the linked-issue pattern's space runs are possessive (\s*+(?::\s*+)?), which matches the same text.
  • A Python member or Rust leaf is joined to its module or prefix only while the joined name stays under 256 characters, and a statement's specifiers are deduplicated as they are read; a nested Rust use list builds each leaf's path once, into one set for the statement.
  • The probes, resolvers, includes and the Python parse's import list deduplicate through dicts; a Rust use stops at its candidate cap; a Python file's candidate list is built only for the probing read, not when the live path set answers.
  • An empty blob at a pass's commit is kept as a verdict, as an oversize one already was (the cache is renamed from _OVERSIZE to _EMPTY_VERDICTS).

Scope and risk

  • Output is unchanged apart from:
    • A CMake file adds no builds facet once it has 10,000 facets, nor for a source argument longer than 4,096 characters, so a file's facet names hold at most about 41 MB.
    • A Python layout keeps the first 10,000 entry points of its manifests, shallow manifests first.
    • A Python from m import x whose joined m.x (or .x after a relative .) would reach 256 characters names only m; a Rust use leaf joined to the prefix of its enclosing lists is dropped once that joined path would reach 256 characters (a path with nothing joined is kept whole). A 1 MB file of distinct members still holds about 140,000 specifiers: about 100 MB at peak for Python, about 270 MB for Rust with its candidate list; a member repeated is one specifier as it is read (a 1 MB from statement of one member repeated peaks at the parse's own 120 MB, the same as when no member is joined).
    • A setup.cfg, pytest.ini or tox.ini line has its whitespace runs before its first delimiter squeezed. The names and values read from them (package_dir, packages.find where, pythonpath) hold no such run.
    • An empty manifest at a pass's commit is not read again for the cache's lifetime (code_ingest_sibling_text_ttl_s), and a layout, Go module index, workspace registry or resolved tsconfig built with it is cached.
  • py_layouts.py and lang_python.py are in the extractor stamp (Python family; lang_python.py is a symbol-stage module, so the chunk, symbol, edge and guard stages all move): the next round re-derives the rows of Python files once, with the same result. lang_python.py's _roots_for and _module_files are now public (roots_for, module_files), since py_layouts.py reads them. The other modules are outside the stamp.
  • Left alone, recorded for follow-up: the CI-block pattern in PR text, quadratic on a block start followed by repeated block-end markers with no > (95 ms at 65,536 characters, 6 ms at 16,000); the ancestor ladders of a file under a very deep directory (each import builds one candidate per ancestor directory); the chunk-read path a pass takes without its mirror, where an empty manifest still reads as not yet indexed, so the Python layout is not cached there; the Go module lookup per import (imports times module-index entries); the tsconfig paths match per bare import (imports times patterns); the Python parse's per-class member scan (classes times symbols); the CMake external-edge candidate filter per link (links times same-named definitions); the candidate lists the TypeScript, Rust and Protobuf lanes build even when the live set answers (linear, unread); and the Python resolution ladder per import, linear with up to 66 roots each (80,000 imports with 64 roots: 2.2 s).

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/lane_walks.py has 21 new walks, timed by test_a_lane_walk_costs_linear_time at 100,000 and 400,000 characters in a child process with a deadline: CMake variable uses (sources, glob words that name none, FILE_SET entries, one long entry) and unclosed functions, Python entry points per file and of one module, setup.cfg name spaces, ambiguous Markdown references, unclosed PR comments, spaces after fix in a PR body, Markdown lane includes, the Python parse's import list and long-module members, a Rust long use prefix, and the Python, TypeScript, Rust (two), Go and Protobuf import probes. On main all 21 fail (the long CMake entry by memory: 500 MB at 100,000 characters, a MemoryError under a 3 GB limit at 400,000; 24 MB here); here all 53 walks pass.
  • A class guard, test_a_registered_lane_costs_linear_time, runs every lane parser in the ingest's parser registry (ingest_parse._PARSERS) over 82 generated shapes (runs of one character, one name repeated, distinct numbered names, unclosed openers, entries times uses) at 5,000 and 20,000 characters (20,000 and 80,000 for the distinct-name shapes, whose quadratic dedupes run at C speed), one child process per lane with a deadline and a 3 GB memory limit. Each file goes through the parse, its chunks and facets, its call sites against an empty name table, and a tree-less lane's own edges (TLA+, TLC, FlatBuffers, CMake, Markdown, Prisma and the site lanes) against a name table, under the recursion limit the edge stage retries with. The tree-sitter parse's time is not counted, since bounded_parse gives each parse a CPU deadline, but the bytes parsed must grow at most linearly; a parse that runs out of memory fails the guard. A shape that makes a content-detected lane read as another one (a manifest shape holding {{) is timed under that other lane only. Not covered: the lanes the parse picks outside the registry (SQL, Bats, the host-file lanes), call binding against repository candidates, import and include resolution (_resolve_lane_includes, timed by the probe walks above instead), the Go, Python and TypeScript lane contexts, header context, entry-point facets against a layout, and the TypeScript island, reference and barrel edges, all of which need the store; and a quadratic rescan at C speed (a str.find to the end from each opener) that stays under 50 ms at 20,000 characters, which only a walk of its own at 100,000 characters catches. test_every_registered_lane_has_a_sample_that_reads_as_it fails for a lane added without a sample, or a sample that parses as another lane. On main the guard fails for CMake and Python (the parse's import list). Three lanes are still over linear on some shapes, listed in KNOWN_SLOW as follow-up fixes: Rust (7 shapes), and TypeScript and JavaScript (5 template-literal shapes under the deep recursion limit). The guard fails on any other shape there, and once a listed shape runs clearly in linear time, so each entry goes with its fix.
  • test_code_ingest_git_text.py::test_an_empty_manifest_is_read_once_and_the_python_layout_kept fails on main (three layout builds for three files) and passes here. test_lang_cmake.py checks the facet cap, the source length cap and that a function's end is unchanged; test_code_py_layouts.py the entry-point cap, a package entry point under a manifest root and a run of spaces before an ini delimiter; test_lang_markdown.py a qualified mention that ends two candidates; test_code_pr_lexemes.py comment stripping against the removed pattern; test_code_py_symbols.py and test_code_lang_rust.py the joined-name cap.
  • The same output: every CMake, Markdown, manifest and .cfg/.ini/.toml file of this repository, and the import lists of its 5,500 Python, TypeScript, Rust, Go and Protobuf files, read with every changed function before and after, give identical results: Python layout roots, entry-point facets of every Python file against an extra 3,000 entry points, import candidates, probes and resolutions, lane includes, stripped PR text, and 46,163 Markdown reference edges against randomized name tables with qualified mentions. cmake_units over 3,000 random CMake files of variables, globs, targets, sources, functions and tests (16,036 facets) gives the same output too.
  • The test files that import the changed modules (814 tests, the lane corpus included) 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 with a deadline before and after the fix, and the output was compared on this repository's files.

  • 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 18 commits November 10, 2026 09:42
CMake variable expansion is lazy and a file's source facets stop at 10,000; a CMake function finds its end by bisection; a Python file finds the entry points it serves by its own module stems; configparser reads manifest lines with the whitespace runs before their delimiter squeezed; Markdown references find a qualified name's candidates by bisection; PR text drops HTML comments with str.find; the import probes and lane includes deduplicate through dicts; an empty manifest at a pass's commit is a cached verdict, so the Python layout is cached.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…out keeps at most 10,000 entry points

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…candidate list is built only for the probing read

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…cache is named for its verdicts

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…Python modules and Rust prefixes are not joined to each member

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…on limit, counts parses, limits memory and checks each listed shape

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…rsed bytes, fails on a parse out of memory and retries listed shapes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s numbered names, warms each lane and checks its listed shapes are timed

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ths go straight into the statement's set

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…the language guide names the guard's sample

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… docstring names the lane guard

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e parity test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ring tidy-ups

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/code-index-linear-walks branch from 900aabe to c755ed0 Compare November 10, 2026 10:36
@zlareb1
zlareb1 merged commit a2235ba into main Nov 10, 2026
35 of 37 checks passed
@zlareb1
zlareb1 deleted the sec/code-index-linear-walks branch November 10, 2026 10:38
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