Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e320291
Start fix for gem apply in unused gem homes
claude Dec 1, 2026
eeb55bb
Merge branch 'main' into agent/fix-gem-apply-unused-system-homes
mikolalysenko Dec 1, 2026
d621826
Lift the gem home rule of vex and apply --check into one helper
mikolalysenko Dec 1, 2026
d8e11ce
Leave gem copies Bundler does not load unwritten in agent apply (#2313)
mikolalysenko Dec 1, 2026
1345567
Document the gem write scope of agent apply
mikolalysenko Dec 1, 2026
df967e6
Leave another project's gems out of an agent-mode scan (#2313)
mikolalysenko Dec 1, 2026
476fac4
Document what an agent-mode scan selects for gems
mikolalysenko Dec 1, 2026
9c6e4a5
Merge branch 'main' into agent/fix-gem-apply-unused-system-homes
mikolalysenko Dec 1, 2026
678a5df
Keep Bundler in a gem env home as a copy the project loads
mikolalysenko Dec 1, 2026
9d5ba4d
Skip another project's gem in apply instead of failing
mikolalysenko Dec 1, 2026
6e3c83b
Treat a gem only another project's home holds as not installed in sca…
mikolalysenko Dec 1, 2026
1b52b44
Leave another project's untouched gem copy out of rollback
mikolalysenko Dec 1, 2026
8016bd8
Document the gem rules for Bundler, other projects' gems and rollback
mikolalysenko Dec 1, 2026
6f344ae
Keep a gem the lock holds as platform builds out of the other-project…
mikolalysenko Dec 1, 2026
2eeab37
Pin that scan --sync keeps a record for another project's gem
mikolalysenko Dec 1, 2026
7c2d9e7
Merge branch 'main' into agent/fix-gem-apply-unused-system-homes
mikolalysenko Dec 1, 2026
bc49817
Identify Bundler by the purl's gem name
mikolalysenko Dec 1, 2026
7b39514
Record by name with get --save-only as before
mikolalysenko Dec 1, 2026
461debf
Call a gem another project's only when a Bundler lock was read
mikolalysenko Dec 1, 2026
bc3fe4e
Document the lock condition, the new skip code and two gaps
mikolalysenko Dec 1, 2026
a32963e
Pin a lock on disk that bundler may not load
mikolalysenko Dec 1, 2026
d89357c
Merge branch 'main' into agent/fix-gem-apply-unused-system-homes
mikolalysenko Dec 2, 2026
bad4acf
Box the gem home checks out of the long-lived command frames
mikolalysenko Dec 2, 2026
1c74732
Box the hand-offs of the scan to gem apply chain
claude Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ The rewriter reads a fixed set of candidate files from the project root: the npm
- When several confirmed variant purls resolve to one installed dir, ANY of them judging it patched keeps it quiet.
- Staleness needs **positive evidence** — bytes actually read that are not the patched state — so missing or unreadable files never mark a copy stale.
- A committed cache-dir archive whose sha256 is readable and differs from the patched artifact's is stale by itself, even with no installed dir at all (a fresh checkout with a committed stale cache). That covers the ruby gem's `<name>-<version>.gem` and each platform build's `<name>-<version>-<platform>.gem`.
- Standalone `vex` and `apply --check` judge gem copies by the same rule (#1098): when Bundler doesn't use system gems, a copy in a `gem env` home is not one the project loads and is not verified, unless it is a default gem (its spec under `specifications/default/`), which Bundler loads from the system home under any path.
- Standalone `vex` and `apply --check` judge gem copies by the same rule (#1098): when Bundler doesn't use system gems, a copy in a `gem env` home is not one the project loads and is not verified, unless it is a default gem (its spec under `specifications/default/`) or Bundler itself, which Bundler loads from the system home under any path. Agent `apply` writes by that rule too (#2313): it leaves such a copy alone (see "Copy classes").

*The refresh* (default; `--no-gem-install-refresh`, env `SOCKET_NO_GEM_INSTALL_REFRESH`, turns it off).

Expand Down Expand Up @@ -1518,7 +1518,8 @@ the model is **not uniform** today:
"nothing installed" absence the hosted lockfile basis excuses), while `apply`/`rollback` never
touch it (#709). Explicit env/config/standalone roots only count when `--cwd` holds a Bundler
manifest/lockfile. When the default `vendor/bundle` root holds no store, the gem homes `gem env`
reports are appended (default gems like rexml/json only ever live there). When the Bundler tier that
reports are appended (default gems like json/uri only ever live there); which of the copies
found there are written is the "Copy classes" rule below. When the Bundler tier that
decides the install path (the first of the app config, the environment and the global config that
sets `path`, `path.system` or `disable_shared_gems`) sets a truthy `path.system` (Bundler's own
coercion: anything but `false`/`f`/`no`/`n`/`0`/empty), bundler installs into and loads from the
Expand Down Expand Up @@ -1555,13 +1556,82 @@ the model is **not uniform** today:
*Copy classes (additive to the multi-copy vocabulary):* a copy under a **bundle-path store**
(config/env/default root) is PRIMARY — a variant mismatch or write failure there fails the run,
as always. A copy in a **`gem env` fallback home** (rvm `@global`, `--user-install`, system gem
dirs — shared, often root-owned) is patched too when it matches and is writable, but becomes
BEST-EFFORT once at least one bundle-store copy applied: its mismatch/write failure surfaces as a
non-fatal `skipped` event (`errorCode: gem_fallback_home_skipped`, detail names the copy's path
and reason; gated stderr twin on the human path) instead of failing a run whose loaded copy is
patched. With **no** bundle-store copy (the historic fallback-only layout, and every `--global`
run) the fallback-home copy IS the primary install and keeps loud-fail parity with pre-bundle-path
`apply`.
dirs — shared, often root-owned) is found only while the default `vendor/bundle` root holds no
store (above). It is a write target of `apply`, and of the apply that `get` and
`scan --mode agent` run, only when Bundler loads it for the project (#2313):

- when Bundler uses system gems (the rule under "Gem stale-install guard": no deployment store
under `vendor/bundle`, and no explicit or deployment `path`), every copy there is the
project's and is patched;
- under an explicit or deployment `path`, two kinds of copy are patched there, because Bundler
loads them from the gem home they are installed in under any path: a default gem (its spec
under `specifications/default/`: `json`, `uri`, `set`), and Bundler itself (a `bundler-<version>`
dir, as `gem install bundler` leaves it; every installed version, since which one runs also
depends on `BUNDLED WITH`, `BUNDLER_VERSION` and Bundler's own version switch). A bundled gem
(`rexml`, `rake`) has an ordinary spec and follows the ordinary rule: Bundler installs it,
like every other gem, into the path, so a copy of it in a `gem env` home belongs to other
projects on the machine and is never written.

A gem with no copy `apply` writes is one of three things:

- **in the project's lock**: not installed yet (a fresh clone before `bundle install`). It is
`package_not_installed` with the lockfile-only detail, a calm skip, exit 0;
- **not in a lock that was read, with a copy in a `gem env` home**: another project's gem. Its
record (one an earlier release wrote when it patched that copy, or one a `get` of the exact
purl wrote) is a `skipped` event with `errorCode: gem_other_project_copy_skipped`, keyed by
the manifest key, whose detail names the copy, says why it was left, and gives the command
that drops the record (`socket-patch rollback <purl>`, with `--cwd` when the run had one); the
human run prints it as a `Warning:`, and the summary counts the patch as `not applicable`. It
never fails the run and is not counted as an all-miss;
- **anything else**: `package_not_installed`, with the result a package that is not installed
has (the all-miss error, exit 1, when no targeted patch matched). That covers a gem with no
copy anywhere, a gem the lock holds only as precompiled platform builds (the lock names it,
so it is the project's), and every gem of a project whose Bundler lock was not read: a
project with no lock yet (a gem library that commits none), and the layouts reported as
`gem_lock_unsupported` (an unsupported `BUNDLE_GEMFILE` / `BUNDLE_LOCKFILE`, a `Gemfile` +
`gems.rb` twin). Only a lock that was read can say the project does not use a gem, so no
`rollback` is advised there.

`scan --mode agent` and `get` in agent mode select by the same rule, so that neither records a
patch its apply step has no copy to write. A gem whose every copy is one `apply` does not write
is not installed for the project, whether or not a lock was read. The scan leaves it out of the
crawl: one the lock lists is then lockfile-only, exactly as when no gem home holds it
(`notInstalled`, skipped as `package_not_installed` before any download, nothing recorded), and
any other is no candidate at all (not counted in `scannedPackages`, not queried, not shown),
like a gem that is installed nowhere. That includes a gem the lock holds only as precompiled
platform builds: an agent-mode scan's lockfile supplement never names those, so such a gem is
not reported as not installed, with or without a copy in a `gem env` home. `get <name>`
resolves a name among the installed packages, so such a gem's name matches nothing, and
`get <CVE|GHSA>` narrows it away as `package_not_installed`. `--save-only` applies nothing and
is not narrowed: by name, advisory or purl it records the patch. An exact purl is never
narrowed, for any package: `get pkg:gem/<name>@<version>` records the patch, and the apply
step reports one of the three cases above. The prune pass (`--prune` / `--sync`) still counts
such a copy as installed, so a record an earlier release wrote for it is kept until `rollback`
or `remove` drops it.

`apply --check` and `vex` judge the copies `apply` writes. `rollback` and `remove` restore a copy
in a `gem env` home that holds the patch, because a release before this rule may have patched it
there; a copy there that `apply` does not write and that holds nothing of the patch is left out,
whatever its content, and its record is dropped as for a package that is not installed (never a
`hash_mismatch` failure over another project's edit). A copy in a shared gem home that another
project patched with the same patch, in system-gems mode, cannot be told from one this
project's earlier release patched, so `rollback` and `remove` here restore it too. A copy there
that an earlier release patched BESIDE the project's own bundle-path copy stays patched after an
upgrade, and `apply` says nothing of it: the project's copy is the one it writes and reports.
`socket-patch rollback <purl> --preserve-state` restores both copies and keeps the record, and
the `socket-patch apply` after it patches the project's copy alone. A `--global`
run patches the `gem env` homes as before. Once the default `vendor/bundle` root holds a store,
no local run searches those homes: a default gem there is not patched, and `rollback` leaves a
copy an earlier run patched there, keeps its manifest record and names
`socket-patch rollback <purl> -g` in a warning.

A fallback-home copy that is written becomes BEST-EFFORT once at least one bundle-store copy
applied: its mismatch/write failure surfaces as a non-fatal `skipped` event
(`errorCode: gem_fallback_home_skipped`, detail names the copy's path and reason; gated stderr
twin on the human path) instead of failing a run whose loaded copy is patched. With **no**
bundle-store copy (Bundler using system gems, a default gem or Bundler that is in no store, and
every `--global` run) the fallback-home copy IS the primary install and keeps loud-fail parity
with pre-bundle-path `apply`.

**Intended (gap):** the cwd-only ecosystems *should* also auto-discover per-subproject lockfiles when
run from the repo root, matching the npm workspace model. The npm-vs-others asymmetry is a known
Expand Down Expand Up @@ -2476,6 +2546,8 @@ command's integration tests check its output.
|---------------------------|------------------|---------|
| `already_patched` | `skipped` | apply: every file's hash already matches `afterHash`. |
| `not_in_platform_build` | `skipped` | apply (gem): the installed copy is the JRuby (`java`) build of the gem and no file of the patch is part of that build (a patch to the C extension or a library it packs). Nothing was written and nothing is left to do: the code the patch changes is not in that build, `apply --check` is in sync and `vex` attests the package (`vulnerable_code_not_present`), and a nested `scan --mode agent` counts the patch among those in place, as it counts `already_patched`. `reason` says so in plain words. rollback (gem): the same copy; nothing of the patch was applied to it, so nothing was restored. See "The JRuby build of a gem". |
| `gem_fallback_home_skipped` | `skipped` | apply (gem), and the apply that `get` / `scan --mode agent` run: a best-effort copy in a `gem env` home (the system gems, `~/.gem`, an rbenv or asdf home) was left unpatched, because it matched no variant or could not be written, while a bundle-path copy of the gem applied. The patch IS in effect on the copy bundler loads. Keyed by the base purl; the detail names the copy's path and the reason. Never flips status or exit. See "Copy classes". |
| `gem_other_project_copy_skipped` | `skipped` | apply (gem), and the apply that `get` / `scan --mode agent` run: the record is for another project's gem. Its every copy sits in a `gem env` home Bundler does not load the project's gems from, and the project's Bundler lock, which was read, does not list it. Nothing was applied. Keyed by the manifest key; the detail names the copy and gives `socket-patch rollback <purl>`, which drops the record and restores the copy if an earlier release patched it. Never flips status or exit; the human summary counts the patch as `not applicable`. With no lock read the gem is `package_not_installed` instead. See "Copy classes". |
| `package_not_installed` | `skipped` | apply: manifest entry has no matching installed package (an installed gem or PyPI copy that holds a patched file with content no record of its release was made for is `no_matching_variant` instead). When the project's lockfiles resolve the purl (a platform-gated optional dependency, a devDependency under `--omit=dev`), the detail says it is lockfile-only and the entry never fails the run, even when no other patch matched; only an all-miss run with an unmatched purl that no lock resolves exits 1 (`partialFailure`). **Cargo is never lockfile-only (v5.0, #616)**: `cargo fetch` unpacks every `Cargo.lock` entry (target-gated ones included), so a locked crate missing from `$CARGO_HOME/registry/src` was simply not fetched yet (a cold CI cache, a pruned `registry/src`) and the next `cargo build` would compile it unpatched; it counts as an unresolved purl (an all-miss run exits 1, as in 4.x) and its detail, the human error and the human warning say to run `cargo fetch` first. A crate `Cargo.lock` takes from a path, git, `[patch]` or `[replace]` source is never looked up in the registry cache (#506): cargo does not build that copy, so the same detail also names that case. |
| `apply_failed` | `failed` | apply: hash mismatch, write error, archive read error. Also a manifest key that is not a purl (see `invalid_purl`). The copy is left as it was: every file is staged (its blob read and hash-checked, with the file's final mode and owner) before the first is renamed into place, and a rename that fails puts the files already replaced back. The `error` names the file (`Cannot apply patch: <file> - <cause>`); a blob that does not hold its hash's bytes is named with the remedy (`blob <path> is corrupt … Run \`socket-patch repair\``). Only when putting a file back fails too does the `error` end with the files left changed; the original of such a file is kept beside it as `.socket-kept-<file>-<uuid>`, a name no later run removes. A run killed between two renames can still leave some files patched; the next `apply` finishes it. A killed run also leaves `.socket-stage-<file>-<uuid>` / `.socket-orig-<file>-<uuid>` files beside the patch's files (staged content and the second names of the files being replaced, never the package's own content); an `apply` or `rollback` of that package removes them once they are ten minutes old. |
| `native_extension_not_rebuilt` | `failed` | apply (gem), and the apply that `get` / `scan --mode agent` run: the patch is incomplete. Its files were written into the installed copy and are left there (unlike `apply_failed`, where the copy is as it was), and the compiled code the copy runs is not the patch's: the build compiled nothing of it, or, on the JRuby build of a gem, the patch changes sources of the gem's jars and does not bring the jars. `error` says what was written and what is not in effect; `files` lists what this run wrote. The run exits 1 (`partialFailure`), the patch stays recorded, `apply --check` is out of sync and `vex` does not attest it; `rollback` restores the files. The same code is a `warnings[]` entry of the run, and the `apply --check` / `vex` tag of such a copy. See "A build that builds nothing of the patch". |
Expand Down
23 changes: 14 additions & 9 deletions crates/socket-patch-cli/src/commands/agent_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2586,16 +2586,21 @@ pub async fn download_and_apply_patches_into(
// Past the network, about to write to installed packages: a
// hot-patch pass is no longer dropped at its time limit.
crate::hot_patch::mark_apply_started();
// Boxed: this frame stays under the whole nested apply (see
// `boxed`).
let command = telemetry_command(env);
apply_report = Some(
run_nested_apply(
telemetry_command(env),
nested_apply_args_from_params(params, run, &manifest_path),
params.json,
!params.user_patch_skips_reported,
&recorded,
run.api_client,
lock,
)
crate::ecosystem_dispatch::boxed(|| {
run_nested_apply(
command,
nested_apply_args_from_params(params, run, &manifest_path),
params.json,
!params.user_patch_skips_reported,
&recorded,
run.api_client,
lock,
)
})
.await,
);
}
Expand Down
Loading
Loading