Skip to content
Back to skills

Verify Cache Lock

ASecurity

Model-check the mutagate cache lock (lock, breakLock, create, owner, dead, abandoned, abandonedBreak, the release function and updateState in scripts/mutagate.mjs; ADR-0023) with TLA+/TLC, and replay its paths on the real code one syscall at a time. Use before you commit a change to any of these functions or to a lock budget, when you review such a change, or when you ask whether parallel hook calls, workers and killed processes can end in two holders of one lock or a lost update of the sessi...

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 4, 2026
ai-agentsrustgojavanodeawsperformance

Works with

  • cli

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned December 4, 2026

npx -y skills add keyboardsamurai/mutagate --skill verify-cache-lock --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Verify Cache Lock?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Verify Cache Lock
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/keyboardsamurai-verify-cache-lock/badge)](https://www.skillsdirectory.com/skills/keyboardsamurai-verify-cache-lock)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: verify-cache-lock
description: Model-check the mutagate cache lock (lock, breakLock, create, owner, dead, abandoned, abandonedBreak, the release function and updateState in scripts/mutagate.mjs; ADR-0023) with TLA+/TLC, and replay its paths on the real code one syscall at a time. Use before you commit a change to any of these functions or to a lock budget, when you review such a change, or when you ask whether parallel hook calls, workers and killed processes can end in two holders of one lock or a lost update of the session state.
---

# Verify the cache lock

Every hook call, worker and CLI call of one session shares one data dir. `lock` (one file that is a hard link with pid and token, heartbeat, `.break`) keeps them apart. Three promises, checked over every order of the syscalls of three processes in a TLA+ model:

- **P1 `OneHolder`.** Two processes never hold the same lock at once. Harm: two mutation runs of one target at once, or two writers of one session state.
- **P2 `NoLostUpdate`.** An update that `updateState` wrote stays in the session state. Harm: a test registration is gone, the gate never sees the target, the agent stops on a weak test, and mutagate says nothing.
- **P3 `HealthyHoldersExclude`.** Two processes that were never frozen never hold the same lock at once. ADR-0023 says that only a holder that does not touch its lock loses it. Harm: as P1, for processes that did nothing wrong.

Two more checks ride along. `DepthIsEnough`: no process needs a break level above `.break.break.break`, so the model cuts nothing off. `GoalNoTakeover` is a goal, not a property: it must break, which shows that the model can break the lock of a killed process, so P1 does not pass only because nothing ever breaks a lock.

Tool: TLA+ with the TLC model checker, because the question is the order of steps among processes, not one function's input. `repro.mjs` replays model paths on the real `scripts/mutagate.mjs`: each process is a child that stops before each syscall of `lock` and of the state file, and the test lets one child take one step at a time. It fails when the code is at another step than the path names.

## Run

From the repo root:

```sh
node .claude/skills/verify-cache-lock/check.mjs             # about 30 s
node .claude/skills/verify-cache-lock/check.mjs --mutants   # about 105 s; after you change Lock.tla: each variant must give its outcome
PROCS=4 node .claude/skills/verify-cache-lock/check.mjs     # all kill rows with four processes, about 30 s
PROCS=4 CONFIGS=sleep,sleepKill,freeze node .claude/skills/verify-cache-lock/check.mjs   # these rows with four processes, about 65 s; the same for sleepShort,sleepKillShort,freezeShort, about 50 s
```

Needs Java 11+ and `tla2tools.jar` at `~/.cache/tla/tla2tools.jar` or in `TLA2TOOLS`. Pinned: TLC2 Version 2026.10.24.013619 (rev 2ef2af8), sha256 `d15655eeaff4b3f7b3d65c46b838bc0f35d131b2ef4b1e473689b62c98d4726f`; `check.mjs` warns on another jar. Exit 0: every outcome as expected. Exit 1: read the table. Exit 2: no TLC.

The configurations say what goes wrong besides the order of steps. `kill`: a signal ends up to two processes at any step (a hook timeout). `sleep`: the system sleeps once for over 60 s. `sleepKill`: one kill and one sleep. `freeze`: one process stops once for over 60 s while the others run (SIGSTOP, a paused VM). `mixed`: one kill, one sleep and one freeze. `kill4`: `kill` with four processes. A row that ends in `Short` (`sleepShort`, `sleepKillShort`, `freezeShort`, `mixedShort`) gives process 1 a short budget: the prune (0), a runner lock wait near the run deadline, or `runTarget` with little budget left.

Baseline at the sync commit `ba5a872` (exit 0):

```
kill           OneHolder             holds   ok
kill           NoLostUpdate          holds   ok
kill           DepthIsEnough         holds   ok
kill           GoalNoTakeover        broken  ok, goal reached
sleep          OneHolder             holds   ok
sleep          NoLostUpdate          holds   ok
sleepKill      OneHolder             holds   ok
sleepKill      NoLostUpdate          holds   ok
sleepKill      DepthIsEnough         holds   ok
freeze         OneHolder             broken  ok, the ceiling that ADR-0023 states
freeze         HealthyHoldersExclude holds   ok
freeze         DepthIsEnough         holds   ok
mixed          HealthyHoldersExclude holds   ok
kill4          OneHolder             holds   ok
sleepShort     OneHolder             holds   ok
sleepShort     NoLostUpdate          holds   ok
sleepKillShort OneHolder             holds   ok
sleepKillShort NoLostUpdate          holds   ok
sleepKillShort DepthIsEnough         holds   ok
freezeShort    HealthyHoldersExclude holds   ok
mixedShort     HealthyHoldersExclude holds   ok
real-code replays: 10 "holds" tests and 1 "finding" test pass
```

Read every TLC result with the pstack `/statechart` skill: it tells each violation as its shortest path in plain words and shows which violations a change fixes; on a pass it draws how the lock moves between states. Its inputs are here: `Lock.tla`, `Lock.cfg` (the `kill` configuration; expected broken there: `GoalNoTakeover` only) and `Lock.map.mjs` (the labels in this repo's words). For a row that `check.mjs` flags, give it the `Lock.tla` and `Lock.cfg` in the folder that `check.mjs` prints, with this folder's `Lock.map.mjs`.

## Findings

Fixed in `25e9a95`: **F4 (P1, P2, P3; `sleep`, `sleepKill`, `sleepShort`, `mixed`).** The wait for a live pid was `now - seen.at` on `performance.now`, which counts a system sleep on macOS (Node 22.17.1, libuv 1.51) and probably on Windows, so after a wake the first poll of a waiter with any budget removed the lock of a holder that ran, and an update was lost; the break came before the budget check, so a waiter whose budget ended in the sleep broke it too. The model hid it: `Stat` let the wait end only for a file that no process tends, and the replays aged the lock file, which starts the wait again, and moved the clock by 3 s. Now each poll adds at most 1 s to the wait (172-174), and after its budget a waiter breaks nothing (only its first try breaks the lock of a dead pid). `watch` in `Lock.tla` holds what a wait saw before a sleep; variant 14 (`Capped == FALSE`) puts the old wait back and breaks `sleep OneHolder` and `NoLostUpdate`, `sleepKill OneHolder` and `mixed HealthyHoldersExclude`. The replays `holds (P1, P2, sleep that the monotonic clock counts; F4 before the fix)` and `holds (P1, P2, sleep of both clocks; F4 before the fix)` fail on the code before the fix (B takes `.break`). The model does not show the budget rule: it lets a waiter break at any poll.

Fixed in `02555d0`: **F3 (P1, P2, P3; `sleepShort`, `sleepKillShort`, `mixedShort`).** A waiter with a short budget broke the lock of a live pid after half its budget (0 for the prune, 2.5 s for `updateState`); after a system sleep the heartbeat of a holder that runs comes up to 10 s late, so the waiter removed the lock of that holder and an update was lost. Now the wait for a live pid is 15 s for every budget (174), and a waiter with less budget gets no lock. Variant 13 puts the old wait back; the replay `holds (P1, P2, sleep, short wait; F3 before the fix)` was a `finding` replay before the fix (B breaks the lock of A after 3 s, `events()` is `['B']`).

Fixed in `b09123c` (the baseline before it: commit `0b020c9`, with the paths step by step and a `finding` replay of each):

- **F1 (P1, P2; `sleepKill`), fixed.** After a system sleep a breaker removed the lock of a process that ran. Root 1: two `{}` reads of two lock files (empty between `openSync` and `writeFileSync`, or no file) counted as the same token. Now a lock file is a hard link that holds its token from the first instant (`create`), and `owner` gives `null` for no file, which breaks nothing. Root 2: a waiter broke a `.break` that was over 60 s old, also when its holder ran. Now a break lock goes only when its pid is dead (`abandonedBreak`); a break lock with no pid (an empty file, only with no hard links) goes when it is over 60 s old. Variants 7, 8 and 9 put each old behaviour back.
- **F2 (P3; `freeze`), fixed.** A frozen holder released between the check and the `rmSync` of a breaker, a third process made a new lock, and the breaker removed it. Now only the holder of `.break` removes the lock file, a release too. Variant 10 puts the old release back.

The model check found the no-file half of root 1 in the first design of the fix, and only with one kill, one sleep and one freeze together: process 3 reads the owner of `.break` when there is no file, a killed process leaves a `.break`, process 3 takes that old file for the one it read, and after the sleep it removes the `.break` of a breaker that runs. That is why `mixed` is a row.

Only four processes and two kills show that the token compare of a break lock (140) is necessary, so `kill4` is a row too. Without the compare: a breaker reads the owner of `.break`, a process that runs; that process removes its `.break`, takes the lock and is killed; the breaker then finds its pid dead and removes the `.break` that a fourth process made. The compare stops it, because the owner that the breaker judged dead before it took `.break.break` cannot remove or change the file after that. Variant 12 and the four-process replay keep this.

Accepted, not fixed:

- **F1 and F2 with no hard links (variant 7).** Where `linkSync` fails (FAT, exFAT, some network shares), `create` opens the lock file with `wx` and then writes it, as before the fix. The file is empty between the two syscalls. A kill, a sleep or a freeze between them can still give two holders: `sleepKill OneHolder` and `freeze HealthyHoldersExclude` break on variant 7, and the replay `finding F1 with no hard links` shows the lost update on the code. With kills only, the lock holds there too (variant 7, `kill` rows). ADR-0023 states this in Consequences.

`freeze OneHolder` is broken by design: the frozen holder loses its lock and does not know (ADR-0023, Consequences). It is listed as the ceiling, not as a finding.

## Coverage at sync commit `ba5a872`

Move this commit forward (here and `SYNC` in `check.mjs`) only after you confirmed that the model describes the code at the new commit.

| Code at `ba5a872`, scripts/mutagate.mjs | Model |
| --- | --- |
| `json` 25, `cacheJson` 27, `state` 115-117 | `ReadState`: one read of the state file |
| `atomic` 33-38 | `WriteState`: the rename (37) is the step; its tries after a refusal on Windows are not in the model (a refused rename changes nothing) |
| `alive` 118, `owner` and `dead` 121 | `alive`, `Owner` (`Gone` for no file, 0 for a file with no token, else the pid), `Dead` |
| `abandoned` 122, `abandonedBreak` 130 | `Abandoned(k, owner)` in `Stat` and `Recheck`; `old`, `Age`, `Sleep` |
| `create` 126-127 | `Open` (`linkSync`, 126); with `HardLinks == FALSE`: `Open` (`wx`) and `Write` (127) |
| `breakLock` 134-142: `create(b)` 137; `owner(b)`, `abandonedBreak` and `breakLock(b)` 138; `null` 139; read again, token compare, `rmSync`, `un()` 140 | the levels 1 to 3: `Open`, `ReadOwner`, `Stat`, `Null`, `Reread`, `Recheck`, `Remove`, `Unlink` |
| `lock` 143-187: token, private file and body 145 | the pid in `file[k]` stands for the token; the private file is no variable |
| write or touch of the private file 152; `create(p)` 153 | `Open` at level 0 (the write and the touch are no steps) |
| heartbeat 154 | `Touch` (level 0 only) |
| release function 157-164 | `WriteState` (starts it), the level 1 call, `Mine`, `Drop`, `Unlink`; `Null` for another try and for the lock file that stays |
| `owner`, `statSync`, `now` and `late` 171; `seen` 148, 172-173; decision and `breakLock(p)` 174: a live pid after 15 s of watched time, at most 1 s per poll, whatever the budget; after the budget no break, but at the first try | `ReadOwner`, `Stat` (`window`; `watch` and `Capped` for the wait before F4, `Short` and `FixedWindow` for the wait before F3); a mtime change sets `watch` to `"stale"` (`Moved`) |
| read again 177; re-check and `rmSync` 178; `unbreak` 179; `continue` 180 | `Reread`, `Recheck`, `Remove`, `Unlink` |
| budget end (`late`) and poll 182-184; the budgets of the callers: `updateState` 5 s (189), `runTarget` (456), prune.cjs:29 (0), `waitLock` runners.cjs:11 | `Null`: poll again or give up, a free choice; `Short`: a waiter whose budget can end before its wait of 15 s |
| `updateState` 188-199 | `ReadState`, `WriteState`, the release; "busy" for `session state busy` (190) |

`check.mjs` prints each hunk of `scripts/mutagate.mjs` that changed in lines 23-38 and 113-199 since the sync commit; such a hunk needs a model review. The comments in `Lock.tla` cite these lines. `repro.mjs` names the syscall of each step: `link`, `owner`, `stat`, `rm`, `unlink` with the level (and `open`, `write` with no hard links); `mine`, `touch`, `unmine` for the private file of the lock call and `sweep` for the one of a dead holder; `read` and `put` for the state file; `alive` for `process.kill(pid, 0)`, only with `setup(t, { ALIVE: '1' })`. `start('D')` makes a fourth process.

## Scope

Verified: every order of the syscalls of 3 processes that each call `updateState` once on one session state file, with the faults of each configuration. A waiter may give up at any poll, a release may give up at any try, and a file may get old as soon as its process is dead or frozen, so time is free choice. Distinct states: 34,456 under `kill`, 64,021 under `sleepKill`, 75,805 under `freeze`, 1,190,527 under `mixed`, 2,424,383 under `kill4`. Each `Short` row has the state count of its row without `Short`: after the fix a short budget adds no state. One run of each other configuration with `PROCS=4` at `25e9a95`: `sleep` holds over 242,805 states, `sleepKill` over 3,936,029, `freeze` (P3) over 3,469,385. `sleepShort`, `sleepKillShort` and `freezeShort` were last run with four processes at `02555d0`, and `mixedShort` never. One run each with four processes and a `Lock.cfg` by hand, at `02555d0` (the model with no `watch`): two kills and one sleep hold over 4,912,591 states (P1), one kill and one freeze over 19,423,457 (P3), one sleep and one freeze over 6,673,305 (P3), and one kill, one sleep and one freeze over 58,961,829 (P3).

Assumptions; the result says nothing when one is false:

- Each `fs` call in `lock` and `breakLock` is one step. `owner` is one `readFileSync`: it reads one file whole, or finds none.
- The write, the touch and the removal of the private file `<lock>.<token>` are no steps: no other process reads that file. The lock file gets the mtime of the private file, which the step before `create` sets; a sleep or a freeze between the two gives a state that the model reaches with `Sleep` or `Age` after `Open`.
- At a break lock, `dead(o)` (`process.kill(pid, 0)`) is its own step in the model (`Stat` and `Recheck` above level 0). `repro.mjs` stops before `fs` calls; only with `ALIVE: '1'` it also stops before that call, so that a replay can put another process between the read and the answer.
- A lock call has one token for its lock file and its break locks, and each process of the model makes one lock call. So the model does not tell two break locks of one process apart, as the code cannot.
- The filesystem of the cache root has hard links. Without them: Findings, Accepted.
- A pid names one process for every process: no pid is used again while a lock file names it, and all processes share one pid namespace. A sandbox with its own pid namespace that shares the cache root sees each outside holder as dead and breaks its lock at once. A dead holder of a break lock whose pid another process got looks alive: no process removes that break lock, and the lock stalls (ADR-0023, Consequences). The model checks no liveness, so it does not show this.
- A lock file gets over 60 s old only when its process is dead or frozen, when its release left it, or after a system sleep. So a process that runs is never away for 60 s between two steps, and the wall clock and the file mtimes agree (no clock step, no network filesystem with another clock).
- The wait of lines 172-174 is not modeled as time; the model assumes its effect: at level 0 a process that runs touches or releases its lock before the wait of a waiter ends. That is 15 s for every budget against the 10 s heartbeat; a waiter whose budget ends first gets no lock, and the model lets a waiter in `Short` break as the others, which is more than the code does. A poll adds at most 1 s, so up to the late heartbeat after a wake a waiter watches at most 11 s (10 s of run time and 1 s for the sleep), whether the clocks count sleep or not (ADR-0023, Rationale). It needs a holder whose event loop is free at least every 4 s (not measured for `runTarget`, which fingerprints with synchronous code), and one sleep before that heartbeat: each further sleep adds 1 s. `watch` decides nothing while `Capped` is TRUE. `check.mjs --mutants` variant 4 shows that the `sleep` rows break without this wait, variant 13 that the `Short` rows break with the wait of half the budget before F3, and variant 14 that the `sleep` rows break with the wait before F4.
- POSIX files: with no hard links, a write through the fd of a removed file changes nothing at the path.

Shown necessary by a variant that breaks a check: the exclusive create (1), the break lock (2, 10), the wait (4), the wait of 15 s for every budget (13), at most 1 s per poll (14), the token checks (5 after a sleep, 6 after a freeze, 12 at a break lock with four processes), the hard link (7), `null` for no file (8), a break lock that goes only with a dead pid (9).

Not shown able to fail: the second abandoned check under a break lock (`abandoned(p, n)` 178, `abandonedBreak(b, n)` 140), with three and with four processes. With hard links the same token is the same file, so it only keeps the lock of a frozen holder that touched it again; it is necessary with no hard links (variants 3 and 11).

Not modeled: that a waiter gets the lock in the end (a break lock of a frozen holder stops every takeover and every release of that lock until it continues); torn state JSON (`cacheJson`); the prune check of prune.cjs:18 and `inFlight` (251), which read `abandoned` (the lock call of the prune, prune.cjs:29, is a waiter in `Short`); what the other holders do inside the lock (`runTarget` 456; tool install, JVM compile and .NET locks, runners.cjs:11), they share `lock`, so P1 is their exclusion too; more than two kills, two sleeps or two freezes in one configuration; Windows.

## Tell a real bug from a modeling error

1. Read the counterexample with `/statechart`, or open `tlc.out` in the folder that `check.mjs` prints.
2. For each step, open the code line that the action's comment cites at the current commit. When the code cannot take the step (a guard or an order that the model lacks), the model is wrong: fix the action, cite the line, rerun with `--mutants`.
3. When the code can take every step, replay the path in `repro.mjs`: one `step(process, ...)` call per model action that is an `fs` call, `kill` for a signal, `age` for 60 s with no touch (and for a sleep), `beat` for the heartbeat of a holder that runs again, `...WATCH()` for the wait of lines 172-174 (15 more polls of 1 s each, `+1s` on a `stat`), `+3s` for 3 s, `+120s` on a `stat` for a sleep that the monotonic clock counts, `+sleep` for a sleep of the monotonic and the wall clock of that process, and `setup(t, { NOLINK: '1' })` for a filesystem with no hard links. When the replay ends with both processes done and an update gone, it is a real bug: report it with the steps in the code's words, keep the replay as a `finding` test, add a `CHECKS` entry with a new id (`F5`), and add it to Findings. Never weaken a property or the model to make it pass, and change product code only when the user asks.
4. A "holds" replay that fails with "is at another step than the model path" means that the code's syscalls changed order or number. Read the hunk, then decide between 2 and 3.

## When a change is intended

1. Read the hunks that `check.mjs` prints and find their rows in Coverage.
2. Change the model actions in those rows to match the new code, one action per syscall. Update the line citations in `Lock.tla`, in `repro.mjs` and in this file, and the step lists of the replays.
3. Run `check.mjs --mutants`. When a variant no longer applies, rewrite its model text in `MUTANTS` so it changes the same guard.
4. When a finding is fixed, its rows show `NOT BROKEN` and its `finding` test in `repro.mjs` fails. Set its `CHECKS` entries to `holds`, turn the test into a `holds` test with the fixed outcome, add a variant that puts the old code back and must break the check, and move the finding to a one-line "Fixed in <commit>" note. Then look at the variants that hold (3, the `kill` rows of 5, 6, 7, 12 and 14, and the `sleep` row of 13): a fix can make a check necessary, and its rows must then say `broken`.
5. When the code adds a state that a property depends on (a new way to judge a lock abandoned, a new file beside the lock, a new budget), add it to the model before you trust a pass. Run one size up as well, in faults (two of a kind, or `mixed`) and in processes (`PROCS=4`), before you delete a check that a variant calls unnecessary: three processes with one kind of fault showed neither the no-file half of F1 nor the need for the token compare of a break lock.
6. Move the sync commit (this file and `SYNC` in `check.mjs`) to the new commit.

Files in this skill

  • Lock.cfg414 B
  • Lock.map.mjs4.1 KB
  • Lock.tla17.5 KB
  • SKILL.md21 KB
  • check.mjs13.2 KB
  • repro.mjs21 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.