Repository navigation
Description
[agent] Found by the architecture refactor routine while running the cleanup of hosted mode's pnpm registry route over hand-derived states of the golden fixtures. Run on main at cfc870f with the binary built from it. The registry route is on main and not in a release yet (v5.1.0 does not hold #2167).
Problem
CLI_CONTRACT.md (line 344, Cleanup) says of the registry route: "nothing of the user's is touched", "a release that a lock still keys by a registry is no leftover", and "the route is never taken out of the workspace file while a lock beside it may still install through it". For a lock the cleanup cannot read it names a warning, registry_cleanup_skipped: "the pnpm locks or the workspace file could not be read to tell what still uses the route; nothing was removed".
A pnpm-lock.yaml with git conflict markers under packages: is read as a lock that ends at the first marker. Every routed release keyed after that line is taken for one that left the lock. So repair, scan --prune and scan --mode hosted --json remove those releases' registries and overrides from pnpm-workspace.yaml, remove their records from the lock's overrides: block (they write into the conflicted lock), print Removed N pnpm registry entries that no lockfile uses any more, and exit 0. With the conflict on the first entry under packages: the whole route goes, and a pnpm-workspace.yaml that hosted mode created is deleted.
Who it hurts: a lockfile conflict is the most common conflict a merge or rebase leaves, and the conflict need not touch a patched package. After the developer resolves it, the lock still keys the patched releases by registries the workspace file no longer declares, and still records overrides the file no longer holds. The contract's own measurements say what pnpm does with each: it rejects a routed lock whose registries are not declared (ERR_PNPM_MISSING_NAMED_REGISTRY), and it refuses a frozen install when a lock's overrides: and the file's disagree. repair does not put the entries back (it restores no pin), so the project stays that way until the workspace file is restored from version control or a hosted scan is granted the same patches again.
Evidence
The project is the several case of crates/socket-patch-core/tests/fixtures/pnpm-registry-overrides/ as a hosted scan leaves it (project/package.json, expected/pnpm-lock.yaml, expected/pnpm-workspace.yaml): four patched releases, each through its own registry. Two branches then bump mid, a dependency with no patch, to different versions, and git merges them. SOCKET_PATCH_SERVER_URL=https://patch.test is set, because the fixtures' registries are on that host.
$ git merge bump-a
Auto-merging package.json
CONFLICT (content): Merge conflict in package.json
Auto-merging pnpm-lock.yaml
CONFLICT (content): Merge conflict in pnpm-lock.yaml
Automatic merge failed; fix conflicts and then commit the result.
$ grep -n "^<<<<<<<\|^>>>>>>>" pnpm-lock.yaml
21:<<<<<<< HEAD
27:>>>>>>> bump-a
45:<<<<<<< HEAD
51:>>>>>>> bump-a
69:<<<<<<< HEAD
73:>>>>>>> bump-a
$ sed -n "/^packages:/,/^snapshots:/p" pnpm-lock.yaml | grep "@socket-" # all four are still keyed by their registries
'@scope/direct@socket-11111111:1.0.0':
leaf@socket-33333333:1.0.0:
native@socket-44444444:1.0.0:
old@socket-22222222:1.0.0:
$ socket-patch repair; echo "exit $?"
Removed 6 pnpm registry entries that no lockfile uses any more:
- overrides entry `native@1.0.0` of pnpm-lock.yaml
- overrides entry `old@1.0.0` of pnpm-lock.yaml
- namedRegistries entry `socket-22222222` of pnpm-workspace.yaml
- namedRegistries entry `socket-44444444` of pnpm-workspace.yaml
- overrides entry `native@1.0.0` of pnpm-workspace.yaml
- overrides entry `old@1.0.0` of pnpm-workspace.yaml
Hosted patches need no local repair; re-run `scan --mode hosted` to refresh the lockfile (it also re-checks for stale pre-hosted installs).
exit 0
$ git diff -- pnpm-workspace.yaml | grep "^- "
- socket-22222222: https://patch.test/patch/npm/old/1.0.0/<token>/22222222-2222-4222-8222-222222222222/
- socket-44444444: https://patch.test/patch/npm/native/1.0.0/<token>/44444444-4444-4444-8444-444444444444/
- 'native@1.0.0': 'socket-44444444:1.0.0'
- 'old@1.0.0': 'socket-22222222:1.0.0'
$ git checkout --ours pnpm-lock.yaml package.json # the developer resolves the conflict
Updated 2 paths from the index
$ sed -n "/^overrides:/,/^$/p" pnpm-lock.yaml
overrides:
'@scope/direct@1.0.0': socket-11111111:1.0.0
leaf@1.0.0: socket-33333333:1.0.0
native@1.0.0: socket-44444444:1.0.0
old@1.0.0: socket-22222222:1.0.0
$ sed -n "/^namedRegistries:/,\$p" pnpm-workspace.yaml | cut -c1-60
namedRegistries:
socket-11111111: https://patch.test/patch/npm/@scope/direc
socket-33333333: https://patch.test/patch/npm/leaf/1.0.0/2
overrides:
'@scope/direct@1.0.0': 'socket-11111111:1.0.0'
'leaf@1.0.0': 'socket-33333333:1.0.0'
$ socket-patch repair --dry-run; echo "exit $?"
Hosted patches need no local repair; re-run `scan --mode hosted` to refresh the lockfile (it also re-checks for stale pre-hosted installs).
exit 0
Once the conflict is resolved, the lock keys four releases by their registries and records four overrides, the workspace file declares two registries and two overrides, and repair has nothing to say about it. Run before the wet repair, repair --dry-run lists the same six entries under Would remove and writes nothing.
Where the conflict sits decides what goes (repair --dry-run, the same project, one conflict each):
| The lock | Entries it would remove |
|---|---|
| intact (control) | 0 |
a conflict under importers:, under snapshots:, or in the lock's overrides: block |
0 |
a conflict under packages: on mid (no patch) |
6: the registries and overrides of native and old, which sort after it |
the same in diff3 style (|||||||) |
6 |
a conflict on the last entry under packages: |
3 |
a conflict on the first entry under packages: |
12: the whole route. The wet run deletes pnpm-workspace.yaml |
The other two commands that tidy the route, over the conflict on mid, with a patch API that offers nothing (their --json envelopes, summarized):
| Command | Exit, status | registry_entry_unused events |
Files written |
|---|---|---|---|
scan --mode agent --prune --json |
0, success |
removed: native@1.0.0, old@1.0.0, socket-22222222, socket-44444444 |
pnpm-lock.yaml, pnpm-workspace.yaml |
scan --mode hosted --json |
0, success |
the same | the same |
The cause is the reader. OverrideLock::parse (core formats/pnpm/registry_alias.rs, lines 241 to 298) refuses a lock with mixed line endings, another lockfile version, packages: without snapshots:, or an entry that runs past an empty line. It does not look at the lock's column-0 lines. section_bounds (core formats/pnpm/lines.rs, lines 14 to 24) ends a section at "the first following column-0 line", and a conflict marker is one, so aliased() (lines 335 to 349) lists the packages: keys before the marker only. pnpm_prune (core hosted/engine.rs, line 5533) has the right guard, "A lock these edits cannot read may still key a release by a registry of ours, or record an override of ours: everything stays" (line 5541), but the parse succeeds, so the guard lets the lock through.
A twin of the existing unit test a_lock_that_cannot_be_read_keeps_everything (core patch/redirect/upstream/route_sweep_tests.rs, line 501: mixed line endings), with conflict markers in place of the mixed endings, fails on main:
/// A lock with git conflict markers under `packages:` still keys both
/// releases by their registries.
#[tokio::test]
async fn zz_probe_a_conflicted_lock_keeps_everything() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
let workspace = format!(
"packages:\n - '.'\n{}",
maps(&[leaf_registry(), mid_registry()], &[LEAF, MID])
);
write(root, "pnpm-workspace.yaml", &workspace);
let clean = lock(&[LEAF, MID], &[LEAF_ROUTED, MID_ROUTED]);
// Control: the lock as pnpm wrote it loses nothing.
write(root, "pnpm-lock.yaml", &clean);
let swept = sweep(root, false).await;
assert_eq!(removed(&swept), [], "control: the intact lock");
// Two branches changed leaf's entry differently: git leaves both.
let ours = " resolution: {integrity: sha512-PATCHED==, tarball: https://patch.test/patch/npm/leaf/1.0.0/9f6b2c4e-1d3a-4f6b-8c2d-7e5a9b1c3d5f/11111111-1111-4111-8111-111111111111/leaf-1.0.0.tgz}\n";
assert!(clean.contains(ours), "{clean}");
let conflicted = clean.replacen(
ours,
&format!("<<<<<<< HEAD\n{ours} engines: {{node: '>=8'}}\n=======\n{ours} engines: {{node: '>=10'}}\n>>>>>>> main\n"),
1,
);
assert!(conflicted.contains("mid@socket-22222222:2.0.0"));
write(root, "pnpm-lock.yaml", &conflicted);
let swept = sweep(root, false).await;
assert_eq!(removed(&swept), [], "a lock with conflict markers");
assert_eq!(read(root, "pnpm-workspace.yaml").unwrap(), workspace);
assert_eq!(read(root, "pnpm-lock.yaml").unwrap(), conflicted);
}$ cargo test -p socket-patch-core --lib -- route_sweep_tests
test patch::redirect::upstream::route_sweep_tests::a_lock_that_cannot_be_read_keeps_everything ... ok
test patch::redirect::upstream::route_sweep_tests::zz_probe_a_conflicted_lock_keeps_everything ... FAILED
assertion `left == right` failed: a lock with conflict markers
left: [("pnpm-lock.yaml", "lockOverride", "mid@2.0.0"), ("pnpm-workspace.yaml", "registry", "socket-22222222"), ("pnpm-workspace.yaml", "override", "mid@2.0.0")]
right: []
test result: FAILED. 9 passed; 1 failed; 0 ignored; 0 measured; 8633 filtered out
The first assertion, over the intact lock, passes: the failure is the one after the markers go in.
Seen beside it, and not part of this issue: the lockfile inventory reads the same conflicted lock as three of its five packages (Found 3 packages (3 npm)), with no warning that the lock could not be read whole.
Not checked: real pnpm 11.20 or later on the tree after the conflict is resolved (this sandbox has pnpm 10.28). What pnpm does there is taken from the contract's own measurements. rollback and remove over a conflicted routed lock were not run.
Planned fix
OverrideLock::parse refuses a lock whose project document has a column-0 line that is not a key of a mapping (a conflict marker, or any other stray line), with a reason that names the line, as it refuses mixed line endings today. The guard in pnpm_prune then keeps everything, and so does every other caller that already handles a lock the edits cannot read. The sweep reports registry_cleanup_skipped for such a lock that names a route, as the contract describes, so the run says why nothing was tidied. The unit test above goes in beside its twin, with one CLI test over a conflicted lock for repair.
The contract settles the behavior, so this is not a decision issue. The routine has three PRs open and awaiting review, so no PR is opened for this yet.
Generated by Claude Code
Activity
refactor: #2327 and #2328 filed; the addon, env spellings and the pnp…