Skip to content

fix(ingest): stop spreading a child subtree into push in walkFilesystem - #315

Merged
shhdwi merged 1 commit into
trailhq:mainfrom
datrixlab:fix/walk-filesystem-arg-cap
Nov 9, 2026
Merged

shhdwi merged 1 commit into
trailhq:mainfrom
datrixlab:fix/walk-filesystem-arg-cap

Conversation

@datrixlab

Copy link
Copy Markdown
Contributor

What

walkFilesystem appends a child subtree with

out.push(...walkFilesystem(full, includes));

which makes the child array the argument list of push(). V8 caps that at roughly 128k, so a single unignored subtree with more paths than that throws RangeError: Maximum call stack size exceeded at trivial directory depth — the message is about the argument list, not recursion depth.

Appending in a loop removes the cap. Nothing else changes: same paths, same order.

Fixes #314

Why it only bites some repos

Only the git-less path reaches this. When listGitFiles returns null the walker collects every non-dot file first and filters by language later, so any large cache, browser profile or dataset directory in a repo that was never git init-ed is fatal — while the same tree under Git is fine because git ls-files respects .gitignore. That is what makes it look nondeterministic across a fleet, as the reporter described.

Tests

Two, deliberately split, because one of them cannot do the other's job:

  1. walkDir collects a large nested subtree outside Git without dropping paths — a wide, deep subtree outside Git comes back flat and complete. Runs on every platform, ~40ms. It pins the accumulation and would catch a botched loop, but it cannot reach the argument cap, so it passes on the old code too. I am flagging that rather than presenting it as proof.
  2. walkDir survives a subtree larger than V8's argument limit (#314) — the real reproduction, 150k files. On the previous code it fails with exactly Maximum call stack size exceeded; with this change it passes.

Verified both directions: reverting only src/ingest/fs.ts and re-running gives # pass 18 / # fail 1, and the one failure is test 2 with that message. With the fix, # pass 19 / # fail 0 in 6.9s.

The heavy test is skipped on Windows. CI runs windows-latest too, and creating 150k files there is far slower than the ~2.5s it takes on Linux. If you would rather not pay even the Linux cost in CI, say so and I will drop test 2 and keep test 1 — the fix stands on its own either way.

Measurements taken while writing this

  • On Node 22, a spread of 125k elements still succeeds; 150k throws. The reporter's ~65k figure is the commonly quoted number but the real boundary is higher, which is why the test uses 150k rather than something just over 65k.
  • --stack-size does not move that boundary. I tried 500/200/100 with 2k–10k elements: all fine. It is an argument-count limit, not stack depth — worth knowing if anyone later tries to make the test cheaper by shrinking the stack.
  • The other push(...) sites in src/graph/build.ts (nodes, rawEdges) accumulate per file rather than per subtree, so they do not reach the cap. Left untouched to keep this focused.

`out.push(...walkFilesystem(full, includes))` turns the child's array into
push()'s argument list, and V8 caps that at roughly 128k. A single
unignored subtree with more paths than that throws "Maximum call stack
size exceeded" at trivial directory depth — the message is about the
argument list, not recursion.

Only the git-less path reaches this: when listGitFiles returns null the
walker collects every non-dot file first and filters by language later,
so any large cache, browser profile or dataset directory in a repo that
was never `git init`-ed is fatal. Appending in a loop removes the cap
without changing what is collected or in what order.

Two tests, deliberately split:

- a cheap one that pins the accumulation itself (a wide, deep subtree
  outside Git comes back flat and complete). It runs everywhere and would
  catch a botched loop, but it cannot reach the argument cap.
- one that actually reproduces the crash with 150k files. It fails with
  "Maximum call stack size exceeded" on the previous code and passes with
  this one. CI also runs windows-latest, where creating that many files
  is far slower, so it is skipped there.

Measured while writing this: on Node 22 a spread of 125k elements still
succeeds and 150k throws, and `--stack-size` does not move the boundary —
it is an argument-count limit, not stack depth.
@trailhq-graft

trailhq-graft Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

🌱 graft blast radius

1 area changed → 2 areas can be affected. 4 dependent symbols, depth 2.
Tests: no test reaches Filesystem Traversal.
Tag: @anirudhkumar-nanonets — 3 of 3 areas · @shhdwi — Dependency Graph · @yescine — Filesystem Traversal

flowchart TB
  A0(("Dependency Graph<br/>3 symbols"))
  A1(("Context Discovery<br/>1 symbol"))
  classDef reached fill:#D9EDF3,stroke:#3AA7C9,stroke-width:1.5px,color:#0E313C;
  class A0,A1 reached;
Loading
Can be affected Symbols Nearest hop Reached from
Dependency Graph 3 src/graph/build.ts:L151-L410 buildGraph — calls, depth 2 Filesystem Traversal
Context Discovery 1 src/context/build.ts:L109-L122 listContextFiles — calls, depth 2 Filesystem Traversal
Who knows this code — 4 people across 3 areas
Area Who knows it
Filesystem Traversal · changed @anirudhkumar-nanonets — 3 commits, last 1mo ago · @yescine — 2 commits, last 29d ago
Dependency Graph · affected @anirudhkumar-nanonets — 17 commits, last 20d ago · @shhdwi — 9 commits, last 27d ago
Context Discovery · affected @anirudhkumar-nanonets — 4 commits, last 20d ago · @Frankie-Xu — 2 commits, last 9d ago

Ownership is git history over each area's own files, weighted towards recent work (120-day half-life). Merge commits and bots are dropped, and you are dropped from your own PR. A name with no @ has no GitHub handle in its commit email — tag them by hand, or add a .mailmap entry. A suggestion from history, not a CODEOWNERS rule.

All 4 dependent symbols, grouped by area

Dependency Graph — 3 symbols in 3 files

  • src/graph/build.ts:L151-L410 — buildGraph (calls, depth 2)
  • src/graph/scopes.ts:L133-L272 — discoverScopes (calls, depth 2)
  • src/graph/source-files.ts:L64-L85 — listSourceFiles (calls, depth 2)

Context Discovery — 1 symbol in 1 file

  • src/context/build.ts:L109-L122 — listContextFiles (calls, depth 2)
Test signal per changed area — 1 ✗

Reached = a node under a test path has a resolved edge into the changed symbol. It undercounts anything called indirectly — through a CLI, a spawned process or a dynamic import — so read a low ratio as “look here”, never as a coverage gate.

  • ✗ Filesystem Traversal — 0 of 1 reached · no test file reaches it
    • not reached: walkFilesystem

graft blast · origin/main...HEAD · depth 2 · 2 changed files

Open the interactive graph → — click an area to see its dependent symbols at file:line.

@sven-n

sven-n commented Oct 1, 2026

Copy link
Copy Markdown

Can you merge this, please? We need the patch for our large non-git codebase, too 😅

yungSNAT pushed a commit to yungSNAT/GrievousGreps that referenced this pull request Oct 31, 2026
* fix(init): keep machine-wide wiring when init omits a host

init converges a repository by retracting every host not named in
--agents, and that sweep also ran at machine-wide scope. So
`graft init B --agents cursor` removed the user-level Claude hooks,
shim and MCP entry and the Codex hooks that every other repository on
the machine depends on. The init-time retract is now repository-only;
`uninstall` still removes the machine-wide files.

Ported from nishadherath/fork_Graft a241f6fb308d (MIT). Upstream issue
trailhq/Trail-Code#482.

Co-authored-by: Nishad Herath <[email protected]>

* fix(cli-meta): start npm through the shell on Windows

On Windows npm is the npm.cmd shim, which spawnSync cannot start
without a shell, so `graft version` always read "unreachable", the
update check stored nothing and `graft upgrade` failed with ENOENT. All
npm calls now go through one helper that passes a joined command line
to the shell on Windows (avoiding Node 24's DEP0190 warning) and spawns
npm directly elsewhere. The tests put a fake npm first on PATH.

Ported from ansidium/trail-code-se7en 4fa301e664a8 (MIT). Upstream
issue trailhq/Trail-Code#487.

Co-authored-by: ansidium <[email protected]>

* fix: keep a --dir on another drive out of the repo's ignore files

On Windows a --dir on another drive has no relative path, so
relative() returns the absolute target. That passed the
startsWith("..") outside-repo check and appended /C:/... to the
repository's .gitignore and .ignore. Both writers now share
repoRelativeDir, which rejects anything not strictly inside the root.

Ported from nishadherath/fork_Graft 50122fbd500f (MIT). Upstream issue
trailhq/Trail-Code#384.

Co-authored-by: Nishad Herath <[email protected]>

* fix(graph): re-parse a cached entry that holds a parse error

A grammar abort is a process-global, run-dependent failure, but the
extraction cache stores it as a per-file entry keyed on content hash and
replays it on every later build. An untouched file then stays broken
forever, and a whole language can drop out of a graph that still exits 0
with a success line.

Let an entry carrying an error fall through to the parse path instead of
being replayed. The entry is still persisted, so the freshness probe does
not report the file as new on every query; it just never stands in for a
result again. Re-parsing costs only files that produced no nodes, and a
run that succeeds repairs the entry.

Upstream issue trailhq/Trail-Code#312, PR trailhq/Trail-Code#317 (MIT).

Cherry-picked from Ercaner1988/Graft 14c0bcd7d223d1cc92bcca3c5995adc236e497e8.

* fix(ingest): stop spreading a child subtree into push in walkFilesystem

`out.push(...walkFilesystem(full, includes))` turns the child's array into
push()'s argument list, and V8 caps that at roughly 128k. A single
unignored subtree with more paths than that throws "Maximum call stack
size exceeded" at trivial directory depth — the message is about the
argument list, not recursion.

Only the git-less path reaches this: when listGitFiles returns null the
walker collects every non-dot file first and filters by language later,
so any large cache, browser profile or dataset directory in a repo that
was never `git init`-ed is fatal. Appending in a loop removes the cap
without changing what is collected or in what order.

Two tests, deliberately split:

- a cheap one that pins the accumulation itself (a wide, deep subtree
  outside Git comes back flat and complete). It runs everywhere and would
  catch a botched loop, but it cannot reach the argument cap.
- one that actually reproduces the crash with 150k files. It fails with
  "Maximum call stack size exceeded" on the previous code and passes with
  this one. CI also runs windows-latest, where creating that many files
  is far slower, so it is skipped there.

Measured while writing this: on Node 22 a spread of 125k elements still
succeeds and 150k throws, and `--stack-size` does not move the boundary —
it is an argument-count limit, not stack depth.

Upstream issue trailhq/Trail-Code#314, PR trailhq/Trail-Code#315 (MIT).

Cherry-picked from Ercaner1988/Graft 88a0442969adacc9353e36a85159c40fdd44a6e7.

* test(ingest): run the argument-cap walker test on Windows

The 150,000-file regression test for the walkFilesystem stack overflow
was POSIX-only because creating the files is slow on Windows. It takes
about 35 seconds there, fails with "Maximum call stack size exceeded"
without the fix, and covers the platform graft is used on daily.

* test(generic): expect a cached grammar failure to be re-parsed

The re-parse fix for cached parse errors changes one assertion in the
throwing-grammar test: an unchanged file whose last parse failed is now
parsed again instead of replayed. The test's point still holds: the
failure is cached as a failure, not an empty success, and the rebuild
reports it again.

* style(cli-meta): spawn npm with the repo's platform shell idiom

Use one spawnSync call with `shell: win`, as signup.ts, cli.ts and
shim-template.ts already do, and mark the PATH lookup of npm as intended
for SonarCloud. Behaviour is unchanged: a joined command line through
the shell on Windows, a direct spawn elsewhere.

* style(cli-meta): mark the Windows npm shell spawn as reviewed for Semgrep

The shell is required to start npm.cmd on Windows and every argument is a fixed token, so suppress the spawn-shell-true finding inline with that reason.

---------

Co-authored-by: Ryan Duguid <[email protected]>
Co-authored-by: Nishad Herath <[email protected]>
Co-authored-by: ansidium <[email protected]>
Co-authored-by: datrixlab <[email protected]>
(cherry picked from commit 87663089946176a111f15b6c7f0e2b67b47af3bb)
@shhdwi
shhdwi merged commit ab5822a into trailhq:main Nov 9, 2026
2 checks passed
github-actions Bot added a commit that referenced this pull request Nov 9, 2026
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.

walkFilesystem crashes with "Maximum call stack size exceeded" on large unignored subtrees (spread-push at fs.js:290, non-git repos)

3 participants