Skip to content

perf(source-control): hard-link node_modules from a per-lockfile seed - #17170

Merged
kyle-sexton merged 5 commits into
mainfrom
perf/worktree-node-modules-seed
Nov 18, 2026
Merged

kyle-sexton merged 5 commits into
mainfrom
perf/worktree-node-modules-seed

Conversation

@kyle-sexton

@kyle-sexton kyle-sexton commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Closes #17084

Summary

worktree-create.sh ran npm ci in every new worktree. On one WSL host that was 330 node_modules copies (median 521 MB, 154 GB in total, 29% of the used disk), plus most of the filesystem's inodes. Worktrees now share one hard-linked copy per lockfile.

Fix

  • npm ci runs once per distinct install into <root>/.npm-seed/<key>, under flock; the seed's files are made read-only and cp -al hard-links that node_modules into each worktree. The key hashes the lockfile, package.json, .npmrc, the Node ABI, platform and architecture, NODE_ENV and npm_config_* variables. The pinned tools in node_modules/.bin (Biome, markdownlint-cli2, the pinned Claude Code) keep resolving as before.
  • Not a symlink: npm ci empties a symlinked node_modules' target (npm/cli lib/commands/ci.js L122-129), which would break every worktree at once.
  • Falls back to the old per-worktree npm ci without flock, node or a working cp -al (macOS, most Windows shells), for a package with workspaces, install lifecycle scripts or file:/link: dependencies, when the seed install fails, or with WORKTREE_CREATE_NPM_SEED=0. Seeds unused for WORKTREE_CREATE_NPM_SEED_DAYS (default 14) are pruned. The lock wait and the install share the existing 45 s cap, so creation stays inside the WorktreeCreate hook's 60 s.
  • Read-only seed files make an in-place write under node_modules fail instead of changing every worktree's copy; npm itself replaces files, so npm ci and npm install in a worktree still work (probed below).

Verification

  • bash plugins/source-control/scripts/worktree-create.test.sh: 388 pass, 0 fail, including 18 new cases (one npm ci for two worktrees, hard-linked files, opt-out, pruning, read-only seed, a NODE_ENV change builds its own seed, the shared cap, fallback after a failed seed install, no seed for a lifecycle script). The cases are skipped where flock or a hard-linking cp -al is absent.
  • shellcheck on both scripts: clean.
  • Real-npm smoke on this repo's package.json and package-lock.json (Linux, ext4): two worktrees plus the seed took 528 MB in one du (one copy); the second creation took 0 s instead of 3 s; node_modules/.bin/biome --version printed 2.6.7 and claude --version 2.1.327 in both from the read-only seed; after npm ci inside one worktree the other still ran Biome.
  • Research behind the design (local probes on npm 11.19.0 and 12.2.3): npm ci and npm install in a hard-linked worktree write new inodes and leave the seed intact; through a symlink they empty the target.

Related

🤖 Generated with Claude Code

kyle-sexton and others added 3 commits November 18, 2026 09:37
worktree-create.sh ran npm ci in every new worktree. On one host that
was 330 copies of node_modules (median 521 MB, 154 GB in total).

It now runs npm ci once per distinct package-lock.json into
<root>/.npm-seed/<sha256>, under flock, and hard-links that tree into
each worktree with cp -al. A later npm ci or npm install inside a
worktree writes new files and leaves the seed intact. A symlink is not
used because npm ci empties the target of a symlinked node_modules.

Without flock, a SHA-256 tool or a working cp -al, or with
WORKTREE_CREATE_NPM_SEED=0, the helper runs npm ci in the worktree as
before. Seeds unused for WORKTREE_CREATE_NPM_SEED_DAYS (default 14) are
pruned. The existing cap and warnings are unchanged.

Closes #17084

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review November 18, 2026 14:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T14:55:59.249446Z eaa32b4 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eaa32b463a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-review: 3 inline finding(s) on eaa32b463a92ab340a992f4d4babc4aa219f68c8; the summary is in this lane's pull request comment.

claude-lane: claude-review

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 4db2434ef706ee64800a509747eae15e1e0cfdd0: read 1 of 1 changed file(s); 0 finding(s), 0 as inline comments.

Code review (incremental, blocking only): 4db2434 on top of b40e2f3

This push changes plugins/source-control/scripts/worktree-create.sh and nothing else. It fixes this lane's last finding: the two install progress lines printed the remaining budget, which made the cap 08 test flaky.

  • Both printf lines now print $deps_cap. One is the seed install line (L1445), the other the in-place fallback line (L1491). run_capped still gets $left, so the shared budget for the lock wait and the install does not change.
  • deps_cap is set at the top level (L1384-1389), so npm_seed_install can read it. It is converted to base 10 there (10#$deps_cap), so a cap of 08 prints capped at 8s. That is what the unchanged test assertion expects, and it no longer depends on when SECONDS ticks.
  • The budget-spent warning at L1500 already printed $deps_cap, so all three messages now give the same number.

Closing issue #17084: it states no acceptance criteria.

Verification: I ran nothing, because this lane has no shell. Whether the tests pass is still the author's claim and is unverified.

Checks:

  • Loosened-check verdict: fail. That is the default, because the wrapper wrote no loosened_check_verdict: line; patterns: 0. This push loosens no check.
  • No plugin manifest changed.
  • No cost hold.

This review was blocking only because this lane already reviewed an earlier head. No candidates came up, and I ran no verifier subagent.

Verifier dropped 0 of 0 candidates.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-security-review: 1 inline finding(s) on eaa32b463a92ab340a992f4d4babc4aa219f68c8; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 4db2434ef706ee64800a509747eae15e1e0cfdd0: read 1 of 1 changed file(s); 0 finding(s), 0 as inline comments.

Security review: PR #17170, incremental (b40e2f3..4db2434)

Scope. I read every hunk of the one file in the incremental scope, plugins/source-control/scripts/worktree-create.sh. It is not a trusted path, trust control, merge hold, workflow or plugin manifest. This lane already reviewed an earlier head, so this review reports CRITICAL and IMPORTANT findings only.

What changed. The push edits two printf lines (L1445 and L1491). Each install message now prints the configured cap $deps_cap instead of the remaining budget $left. The values the messages print are integers the script sets itself, and the messages go only to stderr. The calls that enforce the time limit (run_capped "$left" bash -c '…' _ "<path>") are unchanged. No input reaches a command, path or credential through this change.

Prior finding. My SUGGESTION about the hard-linked node_modules was fixed in the previous push, and this push leaves that code alone.

No instruction surface changed, and there is no cost impact. I found no security issues.

critical-findings: 0

@github-actions

Copy link
Copy Markdown
Contributor

The pr-verify verifier failed head eaa32b463a92ab340a992f4d4babc4aa219f68c8. What it wrote, as data:

What it wrote (23 lines)
Summary: Issue #17084 asks the helper to stop making a full node_modules copy in every worktree. Its
  option 2 is to hard-link from a template tree. The PR's author also filed the issue, and the PR
  delivers option 2: `npm ci` runs once per lockfile into `<root>/.npm-seed/<sha256>` under flock,
  and each worktree gets a `cp -al` copy of that seed. The PR also adds an opt-out, a fallback for
  hosts without flock, a SHA-256 tool or a working `cp -al`, and pruning of stale seeds. It updates
  the docs and adds 9 test cases. Test Bash (0/1/2) and Lint Shell passed on the judged head
  (https://github.com/melodic-software/claude-code-plugins/actions/runs/44329001750/job/134888249327).
  The capture was blocked by the credit gate, which is no evidence either way. It still fails on one
  regression. The seed directory holds only package.json, package-lock.json and .npmrc. In a repo
  whose root package.json declares workspaces, depends on local `file:`/`link:` packages, or runs
  install-time scripts (preinstall/install/postinstall/prepare) that read files in the repo, `npm
  ci` fails in the seed. The helper then only warns and installs nothing in the worktree, where the
  old in-place `npm ci` worked. Root lifecycle scripts also no longer run in the worktree.
- plugins/source-control/scripts/worktree-create.sh:72: when `npm ci` fails in the seed (any
  non-zero exit other than the 124 cap), the function returns that exit code and nothing installs in
  the worktree. In a repo with npm workspaces, local `file:`/`link:` dependencies, or root
  install-time scripts that need repo files, this leaves the worktree without node_modules, where
  the old in-place `npm ci` worked. Fix: return SEED_UNAVAILABLE on that failure so the caller falls
  back to `npm ci` in the worktree. Better, also skip seeding up front when package.json declares
  `workspaces` or root install scripts, or the lockfile has local `file:`/`link:` entries.
- plugins/source-control/scripts/worktree-create.test.sh:155: add a case where the fake npm fails
  only when run inside `.npm-seed`. Assert that the worktree still gets its own node_modules through
  the in-place fallback, and that a workspaces or install-script package.json skips the seed.

This pull request leaves the merge queue, and ci-status holds it out while it carries pr-verify-failed. A pr-verify pass removes the label, at a new commit or at this same head: once a trusted person edits the body, and only trusted people have edited it since the fail, this head is judged again, so a fail on the body or the issues it links needs only the body fixed, no new commit.

@github-actions github-actions Bot added the pr-verify-failed pr-verify failed this head; a pass at a new commit, or after a trusted body edit, removes it. label Nov 18, 2026
Review fixes for the per-lockfile seed:

- A seed install that fails (not the cap) now falls back to npm ci in
  the worktree, and a package.json with workspaces, an install
  lifecycle script or a file:/link: dependency skips seeding, since the
  seed holds only package.json, package-lock.json and .npmrc.
- The seed key hashes package-lock.json, package.json, .npmrc, the Node
  ABI, platform and architecture, NODE_ENV and npm_config_* variables,
  so a Node upgrade or a different npm configuration builds its own seed.
  node computes the key, replacing sha256sum/shasum.
- Waiting for the seed lock and installing share one cap, so creation
  stays inside the WorktreeCreate hook's 60 s.
- The seed's files are made read-only, so an in-place write in one
  worktree fails instead of changing every worktree's copy; npm ci and
  npm install in a worktree still replace files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-review: 1 inline finding(s) on b40e2f37afffece300c8410b25f8ab6f407088a1; the summary is in this lane's pull request comment.

claude-lane: claude-review

@github-actions github-actions Bot removed the pr-verify-failed pr-verify failed this head; a pass at a new commit, or after a trusted body edit, removes it. label Nov 18, 2026
… budget

The remaining budget can read one second below the cap when a second boundary falls inside the seed setup, which made the cap 08 test flaky.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit a1ec2dc Nov 18, 2026
110 of 114 checks passed
@kyle-sexton
kyle-sexton deleted the perf/worktree-node-modules-seed branch November 18, 2026 16:02
@github-actions github-actions Bot added the merge-path: owner-agent Post-merge accounting record (README.md, Merge paths) label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-path: owner-agent Post-merge accounting record (README.md, Merge paths)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(source-control): npm ci in every worktree uses 154 GB of disk

1 participant