# Claude resume After a reboot, a WSL restart or a herdr-core restart, Herdr brings every tab back with a bare shell where Claude Code was running. With this mod you open the Herdr window and, a couple of seconds later, each of those shells types `cd '' && claude --resume ` by itself and Claude opens on its old conversation. A toast says how many came back. Sessions you quit yourself stay quit. The mod has no visible UI. ![Claude resume](preview.png) ## What you see - **Nothing, most of the time.** The mod sits in a floating block that renders an empty `div()`, so Herdr draws no frame for it. - **In a restored terminal:** the typed line `cd '/path/to/project' && claude --resume 3f9c2a71-…`, then Claude Code resuming that conversation. - **A toast** (the shipped `toast` mod, bottom right) after each pass that typed or skipped something: - title `Claude: N session(s) resumed`, or `Claude: nothing resumed` if every one was skipped; - body: the terminals' `where` labels (`space / tab`), then skipped ones as `where: reason`, joined with ` · `; - level `success`, or `warning` if anything was skipped; timeout 6000 ms. - **A warning toast** when Claude runs in a terminal without being an agent to Herdr: - title `Claude not in Agents: /exit and relaunch it` (or `these N`); - body: the terminals' `where` labels, joined with ` · `; - level `warning`; timeout 20000 ms. Also logged to the console. ## How it behaves - **Recording.** On every pass, for each local live terminal that Herdr shows (it has a `herdr.space` label) whose foreground program is `claude`, it reads `~/.claude/sessions/.json` (`sessionId`, `cwd`) and stores `{session, cwd, shell, boot}` as the terminal's `claude-resume` label in herdr-core. `shell` is the terminal's own pid (its shell), `boot` identifies the current boot: `/proc/sys/kernel/random/boot_id` on Linux, `sysctl -n kern.bootsessionuuid` on macOS, `""` if neither answers. It only rewrites the label when something changed. - **Deciding, for a labelled terminal without Claude in front:** - same shell pid and same boot → you quit Claude yourself: clear the label; - a different shell or another boot, and the foreground is the shell itself (a `zsh`, `bash`, `sh`, `dash`, `fish` or `ksh` whose pid is the terminal's) → lost: resume it; - a different shell, but something else in front → wait; after 60 s of that, clear the label (you're using that shell); - no pid yet (the core hasn't restarted the shell) → wait. - **Resuming.** Right before typing it reads the terminal again and only goes on if the shell pid, the foreground and the label are unchanged. It skips a session already open in another terminal and a folder that can't go between single quotes. Then it types the command with `hc send` and rewrites the label with the new shell pid and boot. If Claude comes up, the next pass records it again. If it fails back to the shell, the next pass sees the same shell and clears the label, so it never retries in a loop. - **Pace.** Every 2 s while something is pending (a labelled terminal without a pid, a lost shell still busy, or no terminals at all in the first 60 s after the mod starts), otherwise every 15 s. One pass at a time. - **Agent statuses** (working, blocked, done, idle) don't matter for resuming: only the foreground process does. - **Missing agents.** For each terminal whose foreground is Claude and whose `hc get` says `reported: false` (Claude never sent an OSC 7501 status report), it notes when it first saw that Claude pid. After 30 s it warns, once per terminal and Claude pid. A Claude that reports, exits or is replaced is forgotten, so a new silent one warns again. A healthy Claude reports within a second or two of starting. - No clicks, keys or hovers. ## Built from - `mods/claude-resume/mod.json`: `{ "place": "none", "title": "Claude resume", "version": "0.3.0" }`. - `mods/claude-resume/main.js`: one `View`. - `init` starts `cx.timer.every(2000, …)`, which runs a pass when one is pending or 15 s have gone by since the last, and runs one pass at once. - A pass is `cx.spawn(async (cx) => this.check(cx))`, guarded by a `busy` flag. - `render()` returns `div()`. - `herdr` functions: `terminals()` (live, local, with `labels["herdr.space"]`), `hc(["get", t, "--json"])` (`pid`, `foreground {pid, name, argv}`, `labels`, `reported`), `hc(["label", t, "claude-resume="])` and `hc(["label", t, "claude-resume="])` to clear, `hc(["send", t, text])`, `exec("cat", ["/proc/sys/kernel/random/boot_id"])` then, if that fails or is empty, `exec("sysctl", ["-n", "kern.bootsessionuuid"])`, `readTextFile`, `home()`, `notify()`. - Layout: a floating block on the root of a cloned layout, `{ "id": "claude-resume", "content": { "mod": "claude-resume" }, "edge": "bottom", "align": "end" }`. - No shipped mods are changed, no keys, no shared state, no network. ## Watch out - **Write `mod.json` before `main.js`.** A folder Herdr sees without `mod.json` is docked into the shown layout as a 320 px block. That is a second live copy of the mod, so a lost session gets typed twice: the second copy lands in Claude's input box. It also leaves an empty 320 px strip on the right. - **Don't wait for `hc send`.** On a shell it takes about 5 s to return (`{"started": false}`), because it waits to see whether an agent turn starts. Awaiting it delays the label, the toast and every next terminal. Collect the promises and await them all at the end of the pass. - **Keep the boot id.** After a reboot pids start over, so the restored shell can get the same pid the old one had. Comparing pids alone would read "you quit Claude". `/proc` doesn't exist on macOS: there `cat` exits 1 with empty output (`exec` resolves, it doesn't reject), so check `code` and fall back to `sysctl`. - **The restored shell is busy for a moment** while its rc files run. Treating "something else in front" as "you're using it" right away drops the session. Give it a grace period. - **Re-read the terminal right before typing**, and call `cx.notify()` there: a copy Herdr replaced on reload stops at its next `cx` use instead of typing. - **Point the label at the new shell after typing**, or a failed resume is retried on every pass. - `~/.claude/sessions/.json` is Claude Code's internal file, not an API. The mod assumes `sessionId` and `cwd` fields. - `claude --resume ` finds a session only from its project folder, hence the `cd` first. - **Detecting Claude on macOS:** `foreground.name` is Claude's version (`2.1.318`), not `claude`; match `argv[0]` too. - Linux and macOS. Only local terminals. Flags Claude was launched with are not restored. - It only runs while a layout with the floating block is shown. - **Claude can come up without ever reporting its status** (after a reboot, 8 of 9 resumed Claudes did; the cause wasn't found and couldn't be reproduced). Such a Claude works but isn't an agent to Herdr. Don't use agent status to decide what to resume; only warn about it. - **Testing the warning:** a copy of `/bin/sleep` named `claude` is killed by macOS code signing. Use `(exec -a claude sleep 600)` at a shell: `argv[0]` is `claude` and it never reports. ## Done when - `herdr-app check mods/claude-resume` passes and `herdr-app status` shows no errors. - `herdr-app shot` shows no frame or strip for the mod. - Within 15 s of installing, every terminal running Claude Code has a `claude-resume` label: `hc get --json` → `labels["claude-resume"]` with that session's id. - Quitting Claude with `/exit` clears its terminal's label within 15 s, and nothing is typed. - A fake loss, without restarting: in a new tab at a shell, set `hc label 'claude-resume={"session":"00000000-0000-4000-8000-000000000000","cwd":"/tmp","shell":1,"boot":""}'` (`` from `cat /proc/sys/kernel/random/boot_id`, or `sysctl -n kern.bootsessionuuid` on macOS). Within 15 s the shell shows the `cd '/tmp' && claude --resume …` line exactly once and a toast appears. When Claude exits back to the shell, the label is cleared and nothing is typed again. - A silent Claude: at a shell in a new tab, run `(exec -a claude sleep 600)`. Within 45 s a warning toast names that tab, once. No toast names a terminal whose Claude shows in Agents. Kill the `sleep` and start it again: a new warning within 45 s. - After a real WSL or machine restart, opening Herdr brings each Claude session back within a few seconds of its shell, typed once.