Repository navigation
Code-index extractors and PR text read a file in linear time - #463
Merged
Merged
Conversation
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]>
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]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
sec/code-index-linear-walks
branch
from
November 10, 2026 10:36
900aabe to
c755ed0
Compare
This was referenced Nov 10, 2026
Merged
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.
Related: #448
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 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_filescaller or a PR author controls:${VAR}use expanded to every entry ofVAR, building onebuildsfacet 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. Eachfunction/macroalso scanned every later command for itsend….configparserreadssetup.cfg,pytest.iniandtox.iniwith 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.<!--.*?-->scans to the end from each unclosed<!--, inside one regex call (0.5 s at the 20,000-charactercode.pr_checkcap, 5.3 s at a PR body's 65,536). The linked-issue pattern\s*:?\s*splits a run of spaces afterfixevery way (2.7 s at 20,000 characters, 29 s at 65,536).inover 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 Rustusepath joined and deduplicated every prefix of its segments before keeping the first 12. Afrom <long module> import a, b, …statement, or a Rustuse <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:
buildsfacet 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 ofend…commands, bisected.configparserreads 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.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.uselist builds each leaf's path once, into one set for the statement.usestops at its candidate cap; a Python file's candidate list is built only for the probing read, not when the live path set answers._OVERSIZEto_EMPTY_VERDICTS).Scope and risk
buildsfacet 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.from m import xwhose joinedm.x(or.xafter a relative.) would reach 256 characters names onlym; a Rustuseleaf 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 MBfromstatement of one member repeated peaks at the parse's own 120 MB, the same as when no member is joined).setup.cfg,pytest.iniortox.iniline has its whitespace runs before its first delimiter squeezed. The names and values read from them (package_dir,packages.findwhere,pythonpath) hold no such run.code_ingest_sibling_text_ttl_s), and a layout, Go module index, workspace registry or resolved tsconfig built with it is cached.py_layouts.pyandlang_python.pyare in the extractor stamp (Python family;lang_python.pyis 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_forand_module_filesare now public (roots_for,module_files), sincepy_layouts.pyreads them. The other modules are outside the stamp.>(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 tsconfigpathsmatch 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
Tests
tests/unit/lane_walks.pyhas 21 new walks, timed bytest_a_lane_walk_costs_linear_timeat 100,000 and 400,000 characters in a child process with a deadline: CMake variable uses (sources, glob words that name none,FILE_SETentries, one long entry) and unclosed functions, Python entry points per file and of one module,setup.cfgname spaces, ambiguous Markdown references, unclosed PR comments, spaces afterfixin a PR body, Markdown lane includes, the Python parse's import list and long-module members, a Rust longuseprefix, and the Python, TypeScript, Rust (two), Go and Protobuf import probes. Onmainall 21 fail (the long CMake entry by memory: 500 MB at 100,000 characters, aMemoryErrorunder a 3 GB limit at 400,000; 24 MB here); here all 53 walks pass.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, sincebounded_parsegives 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 (astr.findto 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_itfails for a lane added without a sample, or a sample that parses as another lane. Onmainthe guard fails for CMake and Python (the parse's import list). Three lanes are still over linear on some shapes, listed inKNOWN_SLOWas 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_keptfails onmain(three layout builds for three files) and passes here.test_lang_cmake.pychecks the facet cap, the source length cap and that a function's end is unchanged;test_code_py_layouts.pythe entry-point cap, a package entry point under a manifest root and a run of spaces before an ini delimiter;test_lang_markdown.pya qualified mention that ends two candidates;test_code_pr_lexemes.pycomment stripping against the removed pattern;test_code_py_symbols.pyandtest_code_lang_rust.pythe joined-name cap..cfg/.ini/.tomlfile 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_unitsover 3,000 random CMake files of variables, globs, targets, sources, functions and tests (16,036 facets) gives the same output too.make format lint typecheckpasses.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 typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.docs/reference/configuration.mdand thedocs/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).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code