Skip to content

Add an experimental hot-patch subcommand - #2068

Merged
Mikola Lysenko (mikolalysenko) merged 3 commits into
mainfrom
feat/hot-patch
Nov 14, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 3 commits into
mainfrom
feat/hot-patch

Conversation

@mikolalysenko

Copy link
Copy Markdown
Collaborator

Adds hot patch mode as a hidden, experimental subcommand. socket-patch hot-patch -- <command> patches everything installed on the machine in place, saves nothing, and then becomes the command. As a container's entrypoint it makes a restart the way a patch is deployed or withdrawn, with no image rebuild.

COPY socket-patch /usr/local/bin/socket-patch
ENTRYPOINT ["socket-patch", "hot-patch", "--"]
CMD ["node", "server.js"]
$ docker run --rm my-app
socket-patch: applied 17 patches [106 project + 186 global packages checked, 3.2s]
app: listening on :3000 as pid 1

scan and every other subcommand behave as before. The one change outside the new subcommand is the stop-signal fix below.

What hot patch mode does

  • The command always starts. A flag or environment value that does not parse, an unreachable API, packages that cannot be written and a panic are each one line on stderr, and the command starts anyway. socket-patch execs the command, which keeps the PID, the signals, the environment it was given and its exit status.
  • Patches files as they are on disk, like agent mode. It runs two ordinary agent-mode scans: the project under --cwd, then the system's globally installed packages (--global runs only the second).
  • Nothing is saved. The manifests and patch data the engine works from live in a temporary directory that is removed before the command starts. --manifest-path, --prune, --sync and --vex are refused.
  • Every patch a scan would select is applied. There is no severity default. --min-severity, SOCKET_MIN_SEVERITY and socket.yml narrow it as they do for scan, and they are where a policy or a target list would plug in later.
  • A time limit. A pass that has not started writing patches within --timeout seconds of the start (default 10) is dropped, and it has touched nothing. A pass that has started writing is never interrupted.
  • One socket-patch: ... line on stderr, nothing on stdout.
  • SOCKET_OFFLINE=1 is the off switch.

The subcommand takes scan's options plus --timeout. Without a command after -- it patches nothing and exits 2.

Two things differ from running the two scans by hand:

  • The project pass leaves Go modules to the global pass. A project run does not patch a Go module in place: it redirects the module through go.mod to a copy under .socket/, and drops the redirects its manifest does not list. This mode's manifest starts empty and it writes nothing but installed packages.
  • The global pass reads the working directory's socket.yml, which a plain --global scan does not. The two passes can reach the same installed copy (a Python image's system site-packages), and a package the repository excludes has to stay unpatched in both.

The stop-signal fix (every subcommand)

The kernel drops a signal that PID 1 re-raises with the default disposition, so when socket-patch was a container's main process docker stop did nothing until the grace period ran out. The handler now ends the process itself when it is PID 1, with exit status 128 + the signal's number. Measured with docker stop during a check that was waiting on a dead API: 0.2 s, exit 143.

Start-up cost

Time added before the command starts, measured inside the container on a laptop (Docker Desktop, linux/arm64) against the public patch API with no token, median of 5 fresh containers:

Image Packages (project + global) Patches applied Added to start-up
Small Node app 106 + 186 17 3.2 s
Large Node app 1,158 + 186 19 3.6 s (slowest 4.2 s)
Small Python app 37 + 37 4 2.1 s

Nearly all of it is round trips to the patch API. SOCKET_OFFLINE=1 adds 2 to 3 ms, and an API that never answers costs the time limit. The default limit is 10 s because these starts were too close to 5 s.

Size

src/hot_patch.rs is new (about 510 lines plus its unit tests). The rest of the non-test change is about 130 lines in six files: main.rs cuts the command off the command line before parsing and makes every way out start it, lib.rs declares the hidden subcommand, ui/mod.rs hands scan's JSON document to hot patch mode instead of printing it, agent_download.rs marks the start of the apply step, scan/policy.rs lets the global pass read socket.yml in this mode, and interrupt.rs holds the PID 1 fix. ScanArgs, the patch engine, the crawlers and the API client are untouched.

Testing

  • tests/hot_patch_e2e.rs: 19 end-to-end tests that spawn the real binary against a mock patch API. They cover both passes patching and the command's exit status, --global, the same PID, the command's own flags, an unreachable API, the time limit (the whole run, and the global pass alone), a severity floor, a dry run, socket.yml covering the global pass, a rollout cap, a withdrawn patch, a restart, usage errors and refused flags, the off switch, a missing command, the unchanged environment, a panic, and a stop signal.
  • Unit tests in hot_patch.rs for the command-line split, the passes, the time parser and the summary line.
  • Run by hand in Docker (linux/arm64) with a Node and a Python image: root, non-root (reported, app starts), read-only root file system (reported, app starts), no network (app starts at the limit), stderr on a full device (app starts; the previous build exited 101), docker stop during the check.
  • The full CLI suite on macOS (arm64): 4,930 passed, 2 failed, 418 ignored. The 2 failures are e2e_vendor_cargo_build tests that need old x86 cargo toolchains this Mac cannot run ('cargo': Bad CPU type in executable); they fail the same way on main. The hot-patch end-to-end tests also pass on Linux (in the rust:1.93-bookworm image), including the one that needs /dev/full.
  • The non-Unix path (run the command as a child and exit with its status) was not compiled or run locally.

Known gaps

Review findings I chose not to fix in this PR, and why:

  • No lock across containers. The apply lock lives with the temporary manifest, so two containers that share one mounted dependency tree can patch the same files at once. Each file is written by an atomic rename and checked against its hash before and after, and taking the lock in the project would mean leaving a .socket/ directory behind.
  • A hung package-manager probe can outlive the time limit. If a probe such as yarn global dir is still running when the limit is reached, it is left as a child of the command. Closing this needs a registry of live children in core.
  • The temporary directory is left behind when a stop signal ends the run. A signal handler cannot remove a directory tree safely. A normal run, a timeout and every refusal remove it.
  • The two passes can cover the same packages. In a Python image without a virtualenv, both passes find the system site-packages and each queries the API for them. The second pass finds the patches in place. It costs one extra round of queries.
  • Telemetry still gets its short exit grace (up to 300 ms) before the command starts, when telemetry is on.
  • SIGPIPE is not passed through. The command starts with SIGPIPE at its default even where socket-patch inherited it as ignored (under systemd, for example), because Rust's start-up replaces that setting before any of our code runs. In a container the two are the same.
  • Hot patch mode is carried by a few process-wide statics (is a run active, has the apply step started, the documents reported) that ui::print_json, the agent engine and the policy loader consult. Passing them through ScanArgs instead would have touched every ScanArgs literal in the tests.
  • Path filters in socket.yml judge project paths only. The global pass honors the file's package exclusions, severity floor and enabled, but a root excluded by ignorePaths does not exclude global packages.

Also out of scope here: containers that cannot write to their packages (non-root with root-owned packages, read-only root) get nothing patched, and a patch withdrawn after a container was patched is not rolled back when that container restarts in place.

/code-review high was run on this diff; its confirmed findings are fixed or listed above.

🤖 Generated with Claude Code

The kernel shields a container's main process from every signal it does
not handle. The handler cleaned up and re-raised the signal with the
default disposition, which as PID 1 is dropped: the run carried on, and
`docker stop` waited out its grace period before a SIGKILL. The handler
now ends the process itself when it is PID 1, with the status a signal
death would report (128 + the signal's number).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
  ENTRYPOINT ["socket-patch", "hot-patch", "--"]
  CMD ["node", "server.js"]

Hot patch mode patches everything installed on the machine in place,
saves nothing, and then replaces itself with the command after `--`,
which keeps the PID, the environment and the exit status it would have
had. As a container's entrypoint it makes a restart the way a patch is
deployed or withdrawn.

The hidden `hot-patch` subcommand takes scan's options plus --timeout:

- it always starts the command: a flag that does not parse, an
  unreachable API, packages that cannot be written and a panic are each
  one line on stderr;
- it runs two agent-mode scans, the project under --cwd and then the
  globally installed packages (--global runs only the second). The
  project pass leaves Go to the global one, because a project run
  redirects Go modules through go.mod instead of patching them in place,
  and the global pass reads the working directory's socket.yml too, so a
  package the repository excludes stays unpatched wherever it is;
- it saves nothing: the manifests and patch data live in a temporary
  directory that is removed before the command starts, and
  --manifest-path, --prune, --sync, --vex and --max-new-patches are
  refused;
- it applies every patch a scan would select. --min-severity,
  SOCKET_MIN_SEVERITY and socket.yml narrow that as they do for scan;
- it drops a pass that has not started writing patches within --timeout
  seconds of the start (default 10), and never interrupts a write;
- it prints one `socket-patch: ...` line on stderr and nothing on stdout.

SOCKET_OFFLINE=1 is the off switch. Without a command after `--` it
patches nothing and exits 2.

The subcommand is hidden and experimental: nothing about it is stable.
Scan and every other subcommand behave as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
In-place patching and process replacement affect container startup and installed packages globally; the PID 1 signal exit change applies to every subcommand, though hot-patch itself is hidden and experimental.

Overview
Adds a hidden experimental socket-patch hot-patch [OPTIONS] -- <COMMAND> mode aimed at container ENTRYPOINT use: it runs two agent-mode scans (project under --cwd, then global installs), applies patches in place with no manifest or .socket/ persistence, prints a single socket-patch: … line on stderr, then execs the command after -- (preserving PID, env, and exit status). main is restructured to split argv at -- before clap, route all exits through leave/exec, and still start the wrapped command on parse/API/apply failures or panic.

Hot-patch behavior is wired through scan/apply: refused flags include save-oriented options (--manifest-path, --prune, --sync, --vex, --max-new-patches); --timeout can drop a pass only until the first install write (mark_apply_started); global scans read socket.yml from --cwd when hot-patch is active; scan JSON is captured for the summary line instead of stdout.

All subcommands: interrupt handling now exits PID 1 with 128 + signal after restoring default disposition, so docker stop works when socket-patch is the container main process.

Docs (CLI_CONTRACT.md), global-args coverage, unit tests in hot_patch.rs, and unix e2e tests in hot_patch_e2e.rs accompany the change.

Reviewed by Cursor Bugbot for commit 0fe56a8. Configure here.

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 0fe56a8. Configure here.

On a restart, a package both passes reach (a Python image's system
site-packages) is found patched by each, and the summary line said so
twice. The in-place count is now of packages, like the failed and
dry-run counts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Adds a hidden, experimental socket-patch hot-patch -- <command> subcommand meant to be a container's entrypoint: it runs an agent-mode scan of the project and then of the global packages, patches what is installed in place within a time limit (10 s by default), saves nothing, and then execs the command. Separately, for every subcommand, a stop signal now ends socket-patch when it is PID 1 (before, docker stop waited out the grace period).

Risk: medium. The new mode is hidden and opt-in, but main.rs was restructured for every subcommand (the #[tokio::main] body moved into run, and every exit goes through leave), and the PID 1 signal change applies to all commands. For any command line whose first argument is not hot-patch, the argv and exit codes are unchanged. The non-Unix path (replace_process runs the command as a child process) was not compiled on this PR. windows-compile-check runs only in the merge queue, and I couldn't cross-compile here because there is no mingw.

Look here

  • main.rs:17 and main.rs:35: the new main/run split, and the catch_unwind that makes the command start even after a panic (fine, since no profile sets panic = "abort")
  • hot_patch.rs:268: run_pass drops a pass at the deadline only if it has not started applying
  • hot_patch.rs:153: prepare refuses --manifest-path/--prune/--sync/--vex/--max-new-patches and creates the temporary state directory
  • interrupt.rs:71: _exit(128 + sig) when PID 1, for every subcommand
  • scan/policy.rs:79: the global pass reads the cwd's socket.yml, but only in hot-patch mode

Verified

  • I read the whole non-test diff and the description, and they match.
  • CHANGELOG.md is untouched.
  • The Bugbot double-count finding is fixed in 1785da1 (in_place now goes through distinct, with a restart unit test), and I resolved its thread.
  • Locally on Linux, both test sets pass: cargo test -p socket-patch-cli --lib hot_patch (14 passed) and --test hot_patch_e2e (19 passed).
  • CI is green on 1785da1, including ci-ok and clippy.

Changes I made: none.

Open questions

  • The known gaps in the description were accepted deliberately: no lock across containers, a hung probe can outlive the limit, and the temporary directory is left behind on a stop signal. They are worth a glance before you approve.
  • Minor: on the panic path, dropping the tokio runtime during unwind waits for any spawn_blocking task still running, so a panic combined with a hung probe could delay the command's start. This is unlikely, and I'm not blocking on it.

Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 13, 2026
Merged via the queue into main with commit 63f791d Nov 14, 2026
49 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the feat/hot-patch branch November 14, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants