Skip to content

Decide: a build that reuses webpack's persistent cache (Next.js's .next/cache) still ships the unpatched package after a patch, while apply, vendor --check and vex call it patched: invalidate the cache, warn, or document? #2340

Backlog

Description

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L72), on main at cfc870f, with webpack 5.99.9, Next.js 15.3.3 and 16.4.6, and npm 10.9.9 on Node 22. This is a decision issue: every fix adds behavior CLI_CONTRACT.md does not describe, so the routine will not open a PR for it without an answer.

The question

webpack's persistent cache (cache: { type: 'filesystem' }, and the .next/cache/webpack that next build keeps) revalidates a module under node_modules/ by its package's name and version, never by its bytes. A Socket patch changes the bytes and keeps the version, in all three modes. So a build that reuses a cache written before the patch emits the unpatched module, while apply, apply --check, vendor --check and vex all describe the package as patched, which on disk it is.

What should socket-patch do about a bundler cache that holds the unpatched build of a package it just patched: invalidate it, warn about it, or document it?

Who it hurts: a team whose CI restores .next/cache between runs, as Next.js's guide to CI build caching describes, and patches in agent mode (npm ci, socket-patch apply, next build). Agent mode leaves the lockfile alone, so a cache key built from the lockfile still hits. The deployed bundle carries the vulnerable code, the VEX document says not_affected, and every later build repeats it until the dependency's version changes or someone deletes the cache. The same happens on a developer's machine in any mode.

Evidence

A patch that makes left-pad 1.3.0 return socket-patched-1, served by a stand-in patch API.

Plain webpack, agent mode. webpack.config.js is { mode: 'production', entry: './src/index.js', cache: { type: 'filesystem' } }, and the entry prints require('left-pad')('7', 3, '0').

$ npx webpack && node dist/main.js
webpack 5.99.9 compiled successfully in 242 ms
007
$ socket-patch scan --mode agent --yes; echo "exit $?"
Applied 1 patch.
  HIGH  left-pad@1.3.0  CVE-2026-0001
exit 0
$ node -p 'require("left-pad")("7", 3, "0")'
socket-patched-1
$ npx webpack && node dist/main.js
cached modules 1.5 KiB [cached] 2 modules
webpack 5.99.9 compiled successfully in 79 ms
007
$ socket-patch apply --check; echo "exit $?"
Patches are in sync (1 patch checked).
exit 0
$ socket-patch vex --product pkg:npm/wp-app@1.0.0 -O - | jq -c '.statements[] | [.vulnerability.name, .status, .justification]'
["GHSA-aaaa-bbbb-0001","not_affected","inline_mitigations_already_exist"]
$ socket-patch scan --mode agent --yes --json | jq -c '[.status, .warnings]'
["success",null]
$ rm -rf node_modules/.cache && npx webpack && node dist/main.js     # control
webpack 5.99.9 compiled successfully in 206 ms
socket-patched-1
$ socket-patch rollback --yes && npx webpack && node dist/main.js    # the other direction
Rolled back 1 package:
  pkg:npm/left-pad@1.3.0
webpack 5.99.9 compiled successfully in 87 ms
socket-patched-1

With snapshot: { managedPaths: [] } added to the config, the build after the patch prints socket-patched-1. That is the control for the cause.

Next.js 15.3.3, agent mode. One App Router page (app/a/page.js, a server component) and one Pages Router page (pages/index.js), each rendering leftPad('7', 3, '0').

Step /a prerenders Client chunk of / Emitted files that hold the patched function
next build, before any patch 007 index-e84bc8343f8c228b.js none
socket-patch scan --mode agent --yes, then next build 007 index-e84bc8343f8c228b.js, the same file none
the CI order with .next/cache kept: rm -rf node_modules && npm ci && socket-patch apply && next build 007 the same file none
rm -rf .next/cache && next build (control) socket-patched-1 index-9c555ab31d784d64.js .next/server/app/a/page.js and the client chunk

Both cached builds print Compiled successfully in 0ms. The Pages Router page prerenders socket-patched-1 in every patched row, because Next.js loads the package from node_modules/ on the server for that router; the bundle it sends to the browser is the stale one.

Next.js 16.4.6, the same App Router page. Its default bundler, Turbopack, is not affected: the build after the patch reuses .next/cache/turbopack and prerenders socket-patched-1, with and without experimental.turbopackFileSystemCacheForBuild. next build --webpack is affected as 15.3.3 is: 007 after the patch, and socket-patched-1 once .next/cache is removed. So this is webpack's cache: plain webpack with the filesystem cache, Next.js up to 15 by default, and Next.js 16 built with --webpack.

Plain webpack, vendored mode, with the cache outside node_modules/ (cacheDirectory: path.resolve(__dirname, '.webpack-cache')), so that reinstalling does not remove it:

$ npx webpack && node dist/main.js
007
$ socket-patch scan --mode vendored --yes
Vendored 1 package.
$ npm install
changed 1 package in 325ms
$ node -p 'require("left-pad")("7", 3, "0")'
socket-patched-1
$ npx webpack && node dist/main.js
cached modules 1.5 KiB [cached] 2 modules
007
$ npm ci && npx webpack && node dist/main.js
added 117 packages in 1s
007
$ socket-patch vendor --check
pkg:npm/left-pad@1.3.0: committed artifact and wiring verified
$ socket-patch vex --product pkg:npm/wp-app@1.0.0 -O - | jq -r '.statements[].status'
not_affected
$ rm -rf .webpack-cache && npx webpack && node dist/main.js     # control
socket-patched-1

Cause

webpack trusts a "managed path" to change only when its package manager changes it. The default managed path is the node_modules/ directory (webpack lib/config/defaults.js, lines 505 to 522), and the snapshot of a package under it is the string name@version from its package.json (lib/FileSystemInfo.js, line 4043). Next.js sets the same managed paths and puts the cache in <distDir>/cache/webpack (its build/webpack-config.js).

socket-patch has met this class twice and fixed it both times in agent mode, by invalidating the compiled copy and warning when it cannot:

Go has the warning alone (go_installed_tool_stale). All three codes are in the contract's list of agent-flow warnings (line 407). The npm family has no counterpart, and neither the contract nor docs/ mentions a bundler cache.

Options

  1. Invalidate, as for Cargo and Python. When a run changes the bytes of an npm package (agent-mode apply, rollback, remove, and a scan that applies), remove the webpack cache directories at their default places under the project (node_modules/.cache/webpack, .next/cache/webpack), and warn with a new code where one could not be removed. webpack keeps its cache in pack files, so one package cannot be dropped from it: the whole directory goes, and the next build is a cold one. An agent-mode CI job re-applies after every npm ci, so it would build cold every time unless the removal is limited to runs that change which patches are applied.
  2. Warn only. A new run-level warning names each cache directory found and the command that removes it. Nothing in the project is deleted. Additive, and never a status or exit change, like the three codes above.
  3. Document only. docs/ says what to do: add .socket/ to the key of a restored build cache, or set snapshot.managedPaths (webpack) so patched packages are checked by content.

Hosted and vendored mode change the installed tree through the package manager, when socket-patch is not running, so options 1 and 2 can act there only in the scan that rewires the lock. A cache restored by a later job is reached by documentation alone.

The routine's recommendation is option 2 with option 3, and option 1 only if a maintainer wants socket-patch to delete a directory a framework owns. A custom cacheDirectory or distDir is in a JavaScript config file that socket-patch cannot evaluate, so every option covers the default locations only.

Not run: Rspack, Parcel, Vite's dependency pre-bundle in node_modules/.vite, and the task caches of Turborepo and Nx. Each keeps build output keyed on something a patch may not change, and each needs its own run before anything is said about it.


Generated by Claude Code

Activity

  1. added
    bug
    arch-audit
    on Nov 28, 2026
  2. added a commit that references this issue on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:needs-humanagent:triagedarch-auditbugpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions