xa.recovery¶
Bring back the sessions a shutdown killed, each as it was launched.
Two questions, answered from the transcripts alone (the processes are gone):
Which sessions were alive at the last shutdown? When a machine shuts down, every running claude writes a final
cost-stateentry on its way out, so its transcript’s last write lands within seconds of the shutdown.sessions_alive_at_shutdown()returns the transcripts whose last writefalls in
[shutdown - window, boot). The shutdown time comes fromlast(last_shutdown_time()), the boot time from the kernel (boot_time()), and the record counts only when it belongs to this boot. Sessions resumed since then, running now, or already sitting in a tmux pane started with--resume <id>are never candidates, so applying twice resumes nothing twice. Headless runs (claude -p, the SDK) are skipped.How was each one launched?
xa.claude_fs.launch_facts()reads one transcript for whatclaude --resumeneeds in order to come back the same:the cwd: the first
cwdwhose slug is the transcript’s project folder. A session thatcd-ed elsewhere records later cwds too, and resuming in the last one puts it in the wrong folder (thorwhalen/xa#23);the permission mode: the last one recorded, plus whether bypass was ever reached (only a session started with the bypass flag can reach it);
the model of the last assistant turn, pinned only when its family differs from the account default’s (
xa.claude_fs.resume_model()).
What a shutdown with no clean exit (a power cut, a kernel panic) leaves is not covered: no process wrote anything at the moment it died, so there is nothing to time it by.
Module Attributes
How long before the shutdown record a session's last write may land and still count as "alive at shutdown". |
Functions
|
Unix time the machine last booted, or |
|
Unix time of the shutdown that preceded the current boot, or |
|
Session ids running right now: a live claude, or a tmux pane resuming them. |
|
The first |
|
Resume every candidate in its own tmux session, each as it was launched. |
|
The sessions of |
|
A tmux session name for |
Classes
|
One candidate's outcome: a |
|
The candidates, and the times they were picked by (for the dry run). |
- xa.recovery.DFLT_WINDOW_SEC = 120.0¶
How long before the shutdown record a session’s last write may land and still count as “alive at shutdown”.
lasthas minute resolution, so this has to cover at least the minute it rounds down.
- class xa.recovery.Resumed(facts, result, skipped=None, warning=None)[source]¶
Bases:
objectOne candidate’s outcome: a
SpawnResult, or why not.
- class xa.recovery.ShutdownScan(candidates, shutdown, boot, anchor_source, lo, hi)[source]¶
Bases:
objectThe candidates, and the times they were picked by (for the dry run).
- xa.recovery.boot_time(*, run=<function _run>)[source]¶
Unix time the machine last booted, or
Nonewhen it cannot be read./proc/stat’sbtimeon Linux,sysctl kern.boottimeelsewhere.
- xa.recovery.last_shutdown_time(*, before=None, run=<function _run>)[source]¶
Unix time of the shutdown that preceded the current boot, or
None.Read from
last(minute resolution), newest record first: the current boot’srebootline, then its shutdown. A secondrebootline before any shutdown means this boot followed a crash (no clean shutdown), and an older record must not stand in for it.last -x reboot shutdownon Linux; macOS prints both kinds underlast reboot.
- xa.recovery.live_session_ids(*, claude_home, tmux_bin=None)[source]¶
Session ids running right now: a live claude, or a tmux pane resuming them.
The pane scan covers a resume that has not written anything yet (it is still at a trust or login prompt), which the session files cannot show.
- xa.recovery.parse_last_stamp(text, *, before)[source]¶
The first
Tue Nov 10 09:50-style stamp intext, as unix time.lastprints no year, so the year is the latest one that puts the stamp at or beforebefore(local time).>>> import time as t >>> before = t.mktime((2026, 11, 10, 10, 0, 0, 0, 0, -1)) >>> ts = parse_last_stamp('shutdown time Tue Nov 10 09:50', before=before) >>> t.localtime(ts)[:5] (2026, 11, 10, 9, 50)
- xa.recovery.resume_all(candidates, *, claude_home=PosixPath('/home/runner/.claude'), claude_bin=None, tmux_bin=None, url_timeout_sec=120.0, auto_remote_control=True, permission_mode=None, euid=None)[source]¶
Resume every candidate in its own tmux session, each as it was launched.
All panes are launched first and only then waited on, so the whole batch takes about one
url_timeout_sec, not one per session. A pane that comes up without a Remote Control URL gets thexa.reviveverdict for what it is showing (needs_trustfor a folder the account has not trusted,needs_login, …) inresult.attention, rather than a hang.permission_modeoverrides every candidate’s recorded mode ('': claude’s default).
- xa.recovery.sessions_alive_at_shutdown(*, claude_home=PosixPath('/home/runner/.claude'), window_sec=120.0, shutdown=None, boot=None, live_ids=None, tmux_bin=None)[source]¶
The sessions of
claude_homewhose last write fell at the last shutdown.The window is
[shutdown - window_sec, boot). With no shutdown record for this boot (a crash, orlastunreadable), the anchor is the latest transcript write before boot instead, and the window is[anchor - window_sec, anchor]: the dry run says which was used.shutdown/boot/live_idsdefault to what this machine reports. Sessions inlive_ids(running now) and headless runs are never candidates.- Return type:
- xa.recovery.tmux_name_for(facts, *, taken=())[source]¶
A tmux session name for
facts: its own name, sanitised, made unique.>>> f = LaunchFacts('0123456789', Path('t'), None, '', False, None, None, 'my session', None, 0.0) >>> tmux_name_for(f), tmux_name_for(f, taken={'my-session'}) ('my-session', 'my-session-2')
- Return type: