Skip to content

Keep the replace of the version in use when a stale Go patch record is undone - #2428

Merged
Mikola Lysenko (mikolalysenko) merged 8 commits into
mainfrom
arch-refactor/2414-go-undo-keeps-other-version
Dec 2, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 8 commits into
mainfrom
arch-refactor/2414-go-undo-keeps-other-version

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code

Fixes #2414.

Problem

Go keys a replace by module and version, and so does a patch record, but a module has one socket-owned replace. After a version bump of a patched module the manifest holds two records of it (the scan that patches the new version leaves the old record failing), and the replace serves the new version.

The undo of a Go patch dropped the module's replace whatever it was keyed at. So remove or rollback of the stale record exits 0, reports "Rolled back 1 package", and deletes the directive of the version in use. The next build links the unpatched module while the manifest still records its patch. A scan's summary asks the user to commit .socket/ and go.mod, and a Go project patched that way runs no apply in CI, so the unpatched state ships.

The same class was fixed before in two neighbours and not here: the reconcile's stale-copy prune was keyed by the copy in a2f07e0 (#223), and the vendored failure cleanup got its path condition in 08ea9ba (#150).

Evidence on main (9f2bc04)

Real Go 1.24.7, a module served from a file proxy, the binary built from main. The project is on v0.9.1 with its patch applied; the manifest also holds the v0.8.1 record:

$ grep replace go.mod
replace example.com/upstream v0.9.1 => ./.socket/go-patches/example.com/upstream@v0.9.1
$ go run .
PATCHED v0.9.1
$ socket-patch remove pkg:golang/example.com/upstream@v0.8.1 --yes --offline
Rolled back 1 package
Removed 1 patch from manifest:
  - pkg:golang/example.com/upstream@v0.8.1
exit 0
$ grep replace go.mod
(no replace)
$ go run .
upstream v0.9.1
$ socket-patch apply --check -e golang
Error: Patches are OUT OF SYNC:
  missing go.mod `replace` for pkg:golang/example.com/upstream@v0.9.1
exit 1

rollback <purl> gives the same. It also deletes the go.sum lines saved for v0.9.1 from .socket/go-patches/.upstream-go.sum.

The test added to the CLI's rollback suite, on main:

test rollback_failure_paths::undoing_a_stale_go_record_keeps_the_replace_of_the_version_in_use ... FAILED
    "rolledBack": 1,
  left: "module covgapproj\n\ngo 1.21\n\nrequire github.com/covgap/bumped v0.9.1\n"
 right: "module covgapproj\n\ngo 1.21\n\nrequire github.com/covgap/bumped v0.9.1\n\nreplace github.com/covgap/bumped v0.9.1 => ./.socket/go-patches/github.com/covgap/bumped@v0.9.1\n"

Fix

One rule for what a removal may take from go.mod / go.work, in core vendor/go_mod_edit.rs:

  • An operation on a record (the agent-mode undo, the vendored revert, the cleanup of a failed apply) takes the record's directives: the ones its owner holds for its version of the module, and any socket-owned one that points into the directory it removes. drop_record_replace is new; the undo forgets the saved go.sum lines of exactly the directives it returns.
  • An operation on a directory (the reconcile's prune of an orphan copy or directive, the vendored failure cleanup) takes the directives that point into it. drop_replace_entry now takes that directory in place of an owner.

A directive keyed at another version and pointing at another copy is another record's, and stays.

With this branch the transcript above ends:

$ grep replace go.mod
replace example.com/upstream v0.9.1 => ./.socket/go-patches/example.com/upstream@v0.9.1
$ go run .
PATCHED v0.9.1
$ socket-patch apply --check -e golang
Patches are in sync (1 patch checked).

Deleted

The owner-wide drop on disk. remove_replace_entry by owner stays as the pure function the hosted undo calls after it has matched the directive by its patch uuid.

Behavior change

  • remove / rollback of a Go record, and a failed apply of one, leave the replace of another record of the module. That is the fix.
  • The same commands now also leave a go-patches replace of another version that no record claims (the manifest moved to a new version and no apply has run since). main dropped it in passing. It cannot be told from the case above without the manifest, which the core undo does not see. apply --check names it ("orphan go.mod replace", exit 1) and the next apply removes it ("Removed 1 stale Go patch redirect"); both run here.
  • No JSON shape, flag, exit code or file format changes. CLI_CONTRACT.md describes none of this, and already says Go keys replace by version.

Risk

Low for the common shape: one record per module behaves as before, and every existing Go test passes unchanged except for the new argument. The care is in drifted go.mod files, which the tests cover: a directive keyed at the record's version but pointing at another copy is still dropped by the undo and by a failed heal, as on main; a directive with no version, or with a trailing separator, is matched by the copy it points at.

Tests

Test On the old rule
CLI undoing_a_stale_go_record_keeps_the_replace_of_the_version_in_use (rollback and remove) red on main
core test_remove_keeps_the_directive_of_another_version (also: the saved go.sum lines of the version in use stay) red
core test_failed_apply_keeps_the_directive_of_another_version red
core test_revert_keeps_the_directive_of_another_vendored_record red
core test_drop_takes_only_the_directives_into_the_directory red
core test_remove_drops_its_versions_directive_at_another_copy, test_failed_heal_drops_its_versions_directive_at_another_copy, test_drop_record_replace_takes_the_records_directives pin what main already did for a drifted directive

"Red" for the core tests is a build with the selection switched back to "every socket-owned directive of the module"; the switch was never committed.

Run on the final head: CI's clippy, core's 8,700 unit tests, and 15 CLI suites, among them the three real-toolchain Go suites (e2e_golang_build, e2e_golang_workspace_build, e2e_vendor_golang_build), rollback, remove, apply, vendor, vex and scan.

Review

Five passes of /code-review high. Fixed: the failed-apply cleanup took another record's directive; the undo and the failed heal must still drop a directive keyed at their own version; the reconcile's probe and its drop asked different questions; three stale comments. The last pass's finding was fixed and tested but not reviewed again.

Left, with the reason:

  • .upstream-go.sum keeps the saved lines of the version a bump moved off after every Go patch is undone, so .socket/go-patches/ is not pruned. Pre-existing, the same on main. I tried to forget them in the undo and took it out: two review passes found cases where that deleted lines nothing had put back.
  • The vendored revert still reports "takeover not restored" when another vendored record keeps the module redirected. The fact lives on the reverted ledger entry alone, so staying silent would lose the only hint; main prints it too.
  • A failed first vendor run can leave the root go.mod mirror pointing into a removed uuid dir when the go.work write fails after the mirror's. Pre-existing, read and not reproduced.
  • remove_go_redirect keeps its owner parameter, which base_rel implies. Its one caller is in a file an open PR changes.

Not run

  • Two vendored records of one Go module through the CLI: vendoring needs the patch service for each artifact. The revert is covered at the function level.
  • A workspace where the directive is in go.work, beyond the existing suites; Windows and macOS; Go releases other than 1.24.7.

Diff

git diff --stat: 5 files, +752 −136. Production: +210 −92 in three core files. Tests: +542 −44.


Generated by Claude Code

Assisted-by: Claude Code
@mikolalysenko Mikola Lysenko (mikolalysenko) added arch-refactor PR opened by the scheduled architecture refactor routine refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code labels Dec 1, 2026
Go keys a replace by module and version, and so does a patch record,
but a module has one socket-owned replace. After a version bump of a
patched module the manifest holds two records of it and the replace
serves the newer. The undo of an agent-mode Go patch dropped the
module's replace whatever it pointed at, so `remove` or `rollback` of
the stale record exited 0 and took the directive of the version in
use: the next build linked the unpatched module while the manifest
still recorded its patch. The vendored revert did the same by owner.

drop_replace_entry now takes the directory its caller is removing
(the record's copy under .socket/go-patches, or a vendored patch's
uuid dir) and drops the socket-owned directives of the module that
point into it: the ones that would dangle, and no other. The go.sum
lines saved for another record's directive are neither put back nor
forgotten. The reconcile passes the directive's own target, which is
what its path check already asked for.

The cleanup of a failed apply keeps dropping every directive the
owner holds for the module, as the upsert it undoes rewrites the
module's one directive whatever it pointed at.

Refs #2414.

Assisted-by: Claude Code
Runs `rollback <purl>` and `remove <purl>` of the record a version
bump left behind, beside the record of the version in use. Each exits
0, removes the stale record's copy and leaves go.mod, the other copy
and the other record as they were. On main both commands delete the
replace of the version in use.

Refs #2414.

Assisted-by: Claude Code
Three more places took or left something by module where the record
decides, found in review of the previous commit.

The cleanup of a failed apply dropped every directive the owner held
for the module. With a second record of the module wired, a failed
apply of the first took the second's directive although its copy was
intact. It now drops only a directive that points at the copy the
failure removed, like the undo, so drop_replace_entry_for_cleanup takes
the directory too.

The go.sum lines saved for a record stayed in .upstream-go.sum once a
bump had moved the directive to another version: the undo forgot only
the lines of a directive it found. The undo, and the reconcile when it
prunes an orphan copy, now forget the record's own lines, so nothing
of an undone record keeps .socket/go-patches/ alive.

Reverting a vendored record of a module that another vendored record
still redirects no longer reports a takeover that was not restored:
the module is still vendored, and `socket-patch apply` is not the
remedy.

Refs #2414.

Assisted-by: Claude Code
Review of the previous commit found two things it got wrong and one
probe that disagreed with the drop it guards.

The undo forgot the go.sum lines saved under the record's module and
version even when a directive had pointed at the copy. For a directive
the restore cannot key (one with no version on its left) that deleted
lines nothing had put back. The undo and the reconcile now forget the
lines of the directives they dropped, as before, and the record's own
only when no directive pointed at its copy.

The vendored revert went back to reporting a takeover it does not
restore whatever else redirects the module. The fact lives on the
reverted ledger entry alone, so staying silent beside another record
lost the only hint that `socket-patch apply` puts the redirect back.

The reconcile looked for a directive at an orphan copy by its exact
text and then dropped by the directory. It now asks the same question
both times, so a directive spelled with a trailing separator goes
with the copy it points at.

Refs #2414.

Assisted-by: Claude Code
The undo and the reconcile forgot the go.sum lines saved under a
record's own module and version when no directive pointed at its copy.
Two more cases in review showed that reading as "nothing was dropped"
when something was: a directive the reconcile's first pass had already
dropped, and one in a go.work the reader skips. In both the lines were
deleted without having been put back.

Those lines are left as they are on main: an undo forgets the lines of
the directives it dropped and no others. What a version bump leaves in
.upstream-go.sum for the version it moved off stays there, as before.

Two comments now describe the drop by directory they sit beside.

Refs #2414.

Assisted-by: Claude Code
The undo dropped only a directive that pointed at the record's own
copy. A directive keyed at the record's version but pointing at
another copy (the wrong-path drift `apply --check` names) is the
record's as well: Go redirects that version through it. Left in place
it kept the version redirected with no record behind it, where main
dropped it.

drop_record_replace takes what belongs to one record: the directives
its owner holds for its version of the module, wherever they point,
and any socket-owned one into the directory the undo removes. The
agent-mode undo and the vendored revert call it and forget the saved
go.sum lines of exactly the directives it returns. A directive keyed
at another version and pointing at another copy is another record's
and still stays.

drop_replace_entry keeps the by-directory rule for the reconcile and
the cleanup of a failed apply, which remove a directory and have no
record to go by.

Refs #2414.

Assisted-by: Claude Code
The cleanup of a failed apply dropped only a directive that pointed
at the copy the failure removed. A directive keyed at the record's
version but pointing at another copy, which the apply was healing,
survived the failure; with that other copy gone every `go build`
failed on it, where main dropped it and fell back to the unpatched
module.

The cleanup now takes the record's directives, as the undo does: the
ones the owner holds for this version, and any into this copy. The
directive of another record of the module still stays.

Refs #2414.

Assisted-by: Claude Code
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 2, 2026 01:11
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes how go.mod/go.work replace lines are selected during undo, reconcile, and failure cleanup—core to Go patching—though scoped to record/directory and heavily tested for multi-version and drift cases.

Overview
Fixes a bug where remove / rollback of a stale Go patch record (after a version bump left two manifest entries for one module) removed the module's only socket-owned replace, even when it keyed and pointed at the in-use version. Builds then linked unpatched code while the live patch record remained.

Go replace handling is refactored around a Taken selector and two removal modes:

  • drop_record_replace — undo / failed-apply cleanup: drops directives owned for that record's version and any socket-owned line targeting that record's copy directory.
  • drop_replace_entry — reconcile / orphan prune / vendored failure cleanup: drops only directives pointing into the directory being removed (replaces owner-wide removal).

remove_go_redirect, vendored revert, reconcile stale-copy logic, and failed-redirect teardown are updated to use these rules so another version's replace, copy, and saved go.sum lines stay intact.

New CLI and core tests cover stale-record undo, failed apply, vendored multi-record revert, and path-exact directive matching (including drifted wrong-target cases).

Reviewed by Cursor Bugbot for commit 658f738. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
After a Go version bump the manifest can hold two records of one module, but the module has only one socket-owned replace. Removing or rolling back the stale record used to delete that directive, which unpatched the version in use while still exiting 0. Now an undo takes only its own record's directives: the owner's directive at that record's version, plus any directive that points into the copy being removed. The directory-based prune and the failure cleanup take only directives that point into the directory being deleted.

Risk
Medium. It changes which go.mod/go.work lines the undo, the reconcile prune and the failed-apply cleanup remove. If the matching were wrong, a directive could be left pointing at a deleted copy and break go build. Each removal path still drops any directive that points into the directory it deletes, and the tests cover that.

Look here

  • go_mod_edit.rs:155: targets, the path-prefix match (handles ./, a trailing / and \; does not match a sibling that only shares the prefix)
  • go_mod_edit.rs:188: Taken::takes, the one rule every removal uses
  • [golang_local.rs:524](https://github.com/SocketDev/socket-patch/blob/658f738f3bc9d96476992e17ff782d7f844adc1f/crates/socket-patch-core/src/patch/redirect/golang_local.rs#L524): remove_go_redirect now forgets only the saved go.sum lines of the directives it dropped
  • [golang_local.rs:645](https://github.com/SocketDev/socket-patch/blob/658f738f3bc9d96476992e17ff782d7f844adc1f/crates/socket-patch-core/src/patch/redirect/golang_local.rs#L645): the reconcile's stale-copy prune
  • golang.rs:696: the vendored revert, keyed by the record

Verified

Changes I made
None.

Open questions
None blocking. One behaviour change to note: if a bumped version fails to apply, the cleanup now leaves the old version's directive and copy in place instead of dropping them. That is the state the project was in before the apply.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Dec 2, 2026

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.

[agent] Reviewed the full diff at 658f738: undoing a stale Go record now takes only the directive keyed at the record's version or pointing into its copy, every removal path still drops directives into the directory it deletes, and the go.sum backups follow the dropped directives. No defects found.

Non-blocking note

Reverting a stale vendored record of the same version under a different uuid still takes the live directive, as on main; no test covers it.

@mikolalysenko
Mikola Lysenko (mikolalysenko) added this pull request to the merge queue Dec 2, 2026
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] On the non-blocking note in the approval: agreed, and left as it is on main.

The revert takes the vendor-owned directive keyed at its record's version, so a second vendored record of the same module and version under another uuid would lose its directive to it. The vendor ledger is a map keyed by purl (VendorState::entries, core vendor/state.rs), and a newer patch for a key replaces that key's entry, so two such records need two spellings of one purl in the ledger. I did not build that state, and no test covers it.

Keying that arm by the entry's uuid dir alone would close it, but it would also stop the revert from dropping a same-version directive that drifted to another path, which main drops. I am not pushing for it: the PR is approved and in the merge queue. It is one more item for the follow-up list on #2414.


Generated by Claude Code

Merged via the queue into main with commit aaba467 Dec 2, 2026
89 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the arch-refactor/2414-go-undo-keeps-other-version branch December 2, 2026 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-refactor PR opened by the scheduled architecture refactor routine Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review refactor Structural change: duplicated code or logic, missing abstraction, layering, dead code

Projects

None yet

3 participants