Repository navigation
Backstop: exit early when there is nothing to undo - #108
Conversation
f5a2765 to
f1a3394
Compare
launchd runs backstop.sh every 60 s. The app never removes state.json, so after the first session every run between sessions takes the lock and checks a clean journal key by key with plutil: 38 processes and about 106 ms of CPU each time, on a MacBook with macOS 27.0.1. With no session.json and a state.json that is exactly the clean journal the app writes, the script now exits 0 before the lock, without starting a process (about 2 ms). Any other journal, any session.json and --force take the full run as before.
f1a3394 to
3dfc607
Compare
Real-Mac evidence for
|
|
@greptileai please review this PR. |
End-to-end test in two VMsThe early exit works on macOS 26.6.2 and 27.0. Between sessions, a recovery agent run started 41 programs on main and 0 with this PR. Crash recovery still works the same. One case keeps main's cost: right after the agent itself recovers a crashed session, its runs still start 41 programs, until the app's next session ends. SetupTwo Tart VMs on the maintainer's Mac mini, main (b6a7499) against this PR's head (3dfc607):
Sessions were started from the menu bar with a mouse click. After each step below, the script ran the installed Results
The 41 on main are plutil ×33, chmod ×5, mkdir, lockf and head. Every The recovery looked the same in all four runs: The case that stays at 41The early exit needs no Rerun on macOS 27.0.1The macOS 27 VM was updated to 27.0.1 (26A434), the newest release, and these runs were repeated with the same commits and scripts. The results match 27.0. The macOS 26 VM stays on 26.6.2, the version GitHub's hosted macOS 26 runner uses.
Every Not covered
Per-run dtrace output is in 🤖 Generated with Claude Code |
What this fixes
The recovery agent runs
backstop.shevery 60 seconds, all day, whether or not Insomnia is in use. The app never removesstate.json, so after your first session every one of those runs takes the lock and checks a clean journal key by key withplutil, just to find out there is nothing to do.On my MacBook that is 38 processes and about 106 ms of CPU, every minute.
The change
When there is no
session.jsonandstate.jsonis exactly the clean journal the app writes, the script now exits 0 right away: before the lock, without starting a process.The agent's own
shandcodesigncheck come on top in both cases. I measured by tracing the script withbash -xin a throwawayINSOMNIA_HOMEand timing 30 runs.Everything else takes the full run exactly as before: any other journal (even one extra key, or the same keys in another order), any
session.json, and--force.Why skipping the lock is safe
The app publishes both files with a single rename, so a read never sees half a file. And when those two files say "no session, nothing journaled", there is nothing to undo whichever side of a transaction the read lands on. The full run would wait for the lock and then exit 0 from the same files.
It also skips
tighten.install.sh(which runs with--force) and every non-idle run still do it.Tests
Three new tests in
RecoveryScriptTests. They hold the recovery lock and check that the idle run still returns 0, while every near miss waits for the lock and exits 75. They fail onmain.swift teston this branch: 1,237 tests, 0 failures, 13 skipped (no tmux on this Mac)./bin/bash -npasses under 3.2. I don't have ShellCheck here, so CI is the first lint.On a real Mac
MacBook (Apple silicon), macOS 27.0.1 (26A434), installed with
scripts/install.sh:state.jsoncame back byte for byte as the clean journal.launchctl printshowed 11 runs of the agent with last exit code 0, and the backstop wrote nothing toinsomnia.log.The build I installed was this branch plus #93, #102, #61, #74, #101 and #68, because I need #93 to use the app on macOS 27. So its
backstop.shalso carries #102's change. Not tried on macOS 26.

Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
backstop.shnow exits before taking the lock when no session exists and the journal exactly matches the app's clean output.Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Backstop starts] --> B{Not forced, no session, regular journal?} B -- No --> E[Take recovery lock] B -- Yes --> C[Read journal with shell builtin] C --> D{Exact clean tokens and no NUL?} D -- Yes --> F[Exit 0 without changes] D -- No --> E E --> G[Run existing recovery checks]Reviews (3) · Last reviewed commit: "Backstop: exit early when there is nothi..." · Reviewed by Greptile