Skip to content

fix(patchy): a refresh killed mid-activation no longer locks the repo - #695

Merged
allisonmahmood merged 3 commits into
mainfrom
fix/580-stale-refresh-lock
Oct 15, 2026
Merged

allisonmahmood merged 3 commits into
mainfrom
fix/580-stale-refresh-lock

Conversation

@allisonmahmood

Copy link
Copy Markdown
Owner

A refresh, add, remove or dev start killed while holding .patchy/refresh-lock (SIGKILL, a closed terminal) left the lock behind, so every later run in the repo failed with Begin project transaction failed. until someone found and deleted it. A kill between activation's root swaps also left a mix of old and new managed files, and the dead run's .patchy/refresh-* directory, which can hold a whole old node_modules, was never removed.

Fix

  • The project lock records its owner (pid, process birth, nonce) the way the dev state lock already did. The dev lock's claim core and its birth/sameProcess helpers move to ownerLock.ts, and both locks use it. A lock whose owner is gone is reclaimed. A live owner is refused with "Another patchy command is updating this repo's managed files. Retry when it finishes." A lock with no identifiable owner (an empty slot that stays empty on a brief recheck, a malformed owner file, an owner without a birth) is never reclaimed: the refusal names .patchy/refresh-lock and says to remove it only if no other patchy command is running in the repo.
  • The refusal is a tagged ProjectLocked error. The refresh and dev-preparation IO wrappers pass it through like ProjectChanged, so it reaches text and --json instead of " failed.".
  • A refresh that finds a dead run's transaction directory forces its install, since the pins can match while node_modules does not. It removes those directories once the transaction succeeds. Regeneration rewrites every managed root; the leftover snapshot is not restored. Dev start never installs, so it leaves them for the next refresh.
  • An identity that cannot be read (no ps, an unreadable procfs) never counts as a dead owner. birth now returns nothing only when the process is gone and throws when it cannot tell. The lock then refuses, and dev's signal and cleanup paths stop rather than treat a live daemon as gone. The dev lock still requires this process's own birth, as before.
  • Windows keeps working as before: a process whose birth is unreadable still takes the lock (a claim without a birth is never reclaimed), and EPERM from a rename onto an existing directory counts as occupied.
  • The slot is inspected before the rename, because Linux rename silently replaces an empty directory, which is what a pre-fix CLI holds.

Tested by hand

pnpm dev up brightline, tier 2 new-business repo. Every managed root (patchy/_generated and the seven patchy-* skills) was first marked as old. The refresh was SIGKILLed the moment the new patchy/_generated was renamed into place, by an inotify watch on patchy/.

Before (origin/main's CLI from source):

killed refresh after moved_to: _generated
old roots: patchy-files patchy-look patchy-loop patchy-members patchy-preact patchy-server patchy-tables
.patchy: publish refresh-aAFKko refresh-lock
refresh-lock contents: []

$ patchy refresh --json
{"ok":false,"error":"Begin project transaction failed.","kind":"local"}       exit 1
$ patchy dev --json
{"ok":false,"error":"Begin project transaction failed.","kind":"local"}       exit 1

This branch, against that pre-fix empty lock (refused, never reclaimed):

$ patchy refresh --json
{"ok":false,"error":"Could not identify the patchy command holding .patchy/refresh-lock. Remove it only if no other patchy command is running in this repo, then retry.","kind":"local"}   exit 1
$ patchy refresh
Could not identify the patchy command holding .patchy/refresh-lock. Remove it only if no other patchy command is running in this repo, then retry.   exit 1
$ pnpm patchy dev --json
(same message)   exit 1

This branch, killed mid-activation and recovered. The lock was removed as the message says, and refresh-aAFKko from main's run was left in place.

killed refresh after moved_to: _generated
old roots: patchy-look patchy-loop patchy-members patchy-preact patchy-server patchy-tables
.patchy: dev publish refresh-aAFKko refresh-Kc5vOm refresh-lock
refresh-lock: {"pid":1803976,"birth":"<boot-id>:54023689","nonce":"14e2e82e-…"}

$ patchy refresh --json
{"ok":true,"release":{"from":"0.11.0","to":"0.11.0"},"changed":{"pin":false,"generated":[],"skills":["patchy-look","patchy-loop","patchy-members","patchy-preact","patchy-server","patchy-tables"],"fixtures":[]},"addedCapabilities":[],"warnings":[]}   exit 0
old roots: (none)
.patchy: dev publish
node_modules recreated by this refresh (pins unchanged): .modules.yaml 20:44:37 → 20:44:40

$ pnpm patchy dev --json
{"ok":true,"healthy":true,…}   exit 0

A live owner. A refresh was frozen with SIGSTOP right after it claimed the lock:

$ patchy refresh --json
{"ok":false,"error":"Another patchy command is updating this repo's managed files. Retry when it finishes.","kind":"local"}   exit 1
$ patchy refresh
Another patchy command is updating this repo's managed files. Retry when it finishes.   exit 1
$ pnpm patchy dev --json
{"ok":false,"error":"Another patchy command is updating this repo's managed files. Retry when it finishes.","kind":"local"}   exit 1
(SIGCONT) holder: {"ok":true,…}   exit 0; .patchy: dev publish

The environment's global patchy dev fails on tier 2 repos with Cannot find module 'workerd/package.json', on untouched repos too. It is unrelated to this change, so dev was checked through the repo's own pnpm patchy dev, as the issue's repro does.

Surfaces

  • The CLI. No new command, flag, exit code or error code: the refusal is exit 1, kind: "local", so ADR-0004 is unchanged. packages/patchy/README.md's refresh section now says how an interrupted run recovers. No skill describes the lock.
  • Reverse states. A dead owner's lock is reclaimed automatically. An unidentifiable one names its way out.
  • Wire contract, dev loop, vocabulary, migrations, company resources, served pages and repo layout: not touched.

Out of scope, per the brief: crash-atomic activation, repos already stuck under a pre-fix CLI (one rm -r .patchy/refresh-lock), and patchy dev on Windows.

Closes #580

Made by Claude Opus 5.5 in Claude Code, running in T3 Code.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

GPT 6.1 Sol (xhigh) reviewed this in three rounds. Every finding is fixed or dismissed below.

Round 1 (on 1d7ea13)

  1. High: an identity lookup that fails (no ps, unreadable procfs) counted as a dead owner, so a live lock could be reclaimed. Fixed in 47ec6e8: birth returns nothing only when the process is gone and throws when it cannot tell. claim refuses on a throw, and dev's io-wrapped callers fail safe.
  2. Medium: the dev lock lost its own-birth guard. Fixed in 47ec6e8 and tightened in 0fe1935.
  3. Medium: a pre-fix CLI's bare mkdir landing between readdir and rename is replaced by the rename. Dismissed: it needs a pre-fix and a post-fix CLI starting in the same repo within that window. Node has no RENAME_NOREPLACE, and a slot that can be empty while owned would break the reclaim-safe protocol the dev lock relies on. The brief puts pre-fix CLIs out of scope; an observed empty slot is never reclaimed, and the code comment names the window.
  4. Low: tests. Partly fixed in 47ec6e8: the CLI recovery test now asserts that previous-generation generated and skill bytes are rewritten, and a unit test shows a failed install keeps the orphan. Not added: a dev-start busy test (checked by hand through pnpm patchy dev; it shares the wrapper predicate with refresh, which is tested) and the Windows EPERM / no-own-birth paths (not reproducible on Linux without mocking).
  5. Low: the patchy-loop skill and ADR-0004 say "failure retains the previous set". Dismissed: the brief scopes the doc change to the README, and those lines are about reported failures, which still keep the previous set. After a kill, the next refresh or dev start rewrites every managed root on its own and both refusals say what to do, so an agent needs no new instruction.

Round 2 (on 47ec6e8; it agreed the dismissals of 3 and 5 hold)

  1. High: ps exiting 1 with a diagnostic, or exiting 0 with empty output, still counted as gone. Fixed in 0fe1935: only a silent exit 1 means gone.
  2. Medium: the dev guard discarded its validated birth, and claim re-read it, swallowing a transient failure into a birthless dev lock. Fixed in 0fe1935: claim takes the caller's own birth.

Round 3 (on 0fe1935): both resolved, no new findings.

@github-actions github-actions Bot added area:cli Touches the cli area area:docs Touches the docs area size:m 100-500 changed lines labels Oct 15, 2026
The project lock now records its owner through the dev lock's claim core,
moved to ownerLock.ts. A dead owner's lock is reclaimed; a live or
unidentifiable owner is refused with a message that reaches the person.
Refresh reinstalls when a dead run's transaction directory is present and
removes it once that install succeeds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ck owner

birth now returns undefined only when the process is gone and throws when
it cannot tell (no ps, unreadable procfs), so claim refuses instead of
reclaiming and dev callers fail safe. The dev lock again requires its own
birth, an owner pid must be positive, and the tests cover stale managed
bytes and a failed install keeping the orphan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ps printing a diagnostic, or printing nothing on success, now cannot tell
rather than meaning gone. claim takes the caller's own birth, so the dev
lock records the birth it validated and the project lock records none only
where its lookup fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the fix/580-stale-refresh-lock branch from 0fe1935 to ff671d1 Compare October 15, 2026 20:44
@allisonmahmood
allisonmahmood merged commit abaf770 into main Oct 15, 2026
13 checks passed
@allisonmahmood
allisonmahmood deleted the fix/580-stale-refresh-lock branch October 15, 2026 20:56
allisonmahmood added a commit that referenced this pull request Oct 16, 2026
…u when they fail (#720)

Release 13's What's new entry, written from `c9b8fc4`, ahead of
deploying it. `patchy` and `CURRENT_RELEASE` move to `0.13.0`, so CLIs
and patch repos installed before this deploy are told to upgrade.

| Change | PRs |
| --- | --- |
| New: Home puts the patches you use first | #713, #716, #719 |
| New: Every patch in one list | #715 |
| Improved: Each patch's page leads with Open | #717 |
| New: See when your tools are failing | #718 |
| Fixed: Everyone has a name | #687 |
| Improved: Your company's look is easier to get right | #697, #696,
#703, #688 |
| Improved: Forms follow your look's error states | #691, #692 |
| Improved: Clearer answers when a publish goes wrong | #686, #685, #689
|
| Fixed: Your agent's Patchy commands are steadier | #695, #704, #680,
#681 |
| Behind the scenes: stricter checks on static pages and tools' database
requests; malformed requests are logged | #682, #693, #683 |
| Behind the scenes: usage numbers count every publish, sharing change
and restore | #698, #699, #700, #690 |
| Test and tooling only, no line | #705, #702, #701, #694, #684, #607 |

Made by Claude Opus 5.5 in Claude Code, running in T3 Code.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli Touches the cli area area:docs Touches the docs area size:m 100-500 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(patchy): a crash mid-activation leaves a stale lock

1 participant