xa.sessions

The unified Session domain model and multi-host discovery.

This layer is the single thing CLI / HTTP / UI renderers iterate over. Discovery and actions are delegated to xa.hosts; this module composes them into a host-agnostic API.

Functions

get_session(session_id, *[, hosts, claude_home])

Find one session by full ID or unique prefix.

iter_local_sessions(*[, claude_home, ...])

Legacy local-only iterator.

kill_session(session, *[, hosts])

Kill a live Session, scoped to what actually belongs to it.

launch_opts(facts, *[, claude_home, ...])

(resume kwargs, warning) that bring facts back as it was launched.

list_sessions(*[, hosts, project, ...])

Return sessions sorted by recency (live first, then newest-modified).

resume(session, *[, cwd, name, hosts])

Resume a transcript / archived session on its originating host, as it was launched.

resume_shell_command(facts, *[, ...])

(shell command, warning) that resumes facts' session as it was launched.

Classes

Session(id, claude_session_id, ...[, ...])

Canonical session record — valid across hosts and states.

class xa.sessions.Session(id, claude_session_id, bridge_session_id, host, cwd, project_slug, state, live_pid, tmux_name, name, summary, first_user_message, turn_count, forked_from, created, modified, url, url_source, transcript_path, pre_first_turn=False, attention=None, attention_hint=None, tmux_pane=None, pane_provider=None, pane_ref=None)[source]

Bases: object

Canonical session record — valid across hosts and states.

Fields that don’t apply to a given state are None. Discovery functions return fresh instances; callers should treat it as immutable.

property pane_address: Optional[PaneAddress]

Where this session’s pane is, or None (a session with no terminal).

xa.sessions.get_session(session_id, *, hosts=None, claude_home=None)[source]

Find one session by full ID or unique prefix.

Raises LookupError if the prefix is ambiguous.

Return type:

Optional[Session]

xa.sessions.iter_local_sessions(*, claude_home=PosixPath('/home/runner/.claude'), project_slug=None, include_live=True, tmux_bin=None)[source]

Legacy local-only iterator.

Retained for backward compatibility and because some tests construct sessions directly from a fake ~/.claude/. Wraps a fresh LocalHost.

Return type:

Iterator[Session]

xa.sessions.kill_session(session, *, hosts=None)[source]

Kill a live Session, scoped to what actually belongs to it.

Kills the backing tmux session only when it is dedicated to this claude (single pane, claude verified in it) — a claude living in one window of a shared multi-window workspace must not take the whole workspace down, so there we SIGTERM the identity-verified pid instead. Remote hosts keep the name-kill contract (their server applies its own scoping). Returns a short description of what was killed (e.g. "tmux:mysess" / "pid:1234").

Return type:

str

xa.sessions.launch_opts(facts, *, claude_home=PosixPath('/home/runner/.claude'), permission_mode=None, euid=None)[source]

(resume kwargs, warning) that bring facts back as it was launched.

Effort is deliberately not restored: the recorded per-turn value can sit below the account’s effortLevel, and the issue asked for the model. permission_mode ('': claude’s default) replaces the recorded mode, bypass is then not allowed and there is no warning; euid stands in for the process’s own when deciding whether bypass is refused.

Return type:

tuple[dict, Optional[str]]

xa.sessions.list_sessions(*, hosts=None, project=None, include_forks=True, include_live=True, state=None, limit=None, claude_home=None, tmux_bin=None)[source]

Return sessions sorted by recency (live first, then newest-modified).

When hosts is None we default to a single LocalHost. If claude_home or tmux_bin are given, they’re forwarded into that default LocalHost — handy for fixture-based tests.

Filters:

  • project — substring match against cwd (case-insensitive)

  • include_forks=False — drop sessions with a forked_from

  • include_live=False — skip live discovery (per host)

  • state — one of "live" / "archived" / "transcript_only"

Return type:

list[Session]

xa.sessions.resume(session, *, cwd=None, name=None, hosts=None, **opts)[source]

Resume a transcript / archived session on its originating host, as it was launched.

Whatever the caller leaves unset comes from the transcript (launch_opts()): the cwd it was started in, its model when that differs from the account default’s family, and its permission mode, so a session started with --dangerously-skip-permissions comes back with it. permission_mode='' asks for claude’s default explicitly. When a launch fact had to be dropped (bypass refused to root), the result’s warning says so.

Only for a local host: a remote one gets exactly what the caller set, and its own xa reads its own transcript the same way.

Return type:

SpawnResult

xa.sessions.resume_shell_command(facts, *, claude_home=PosixPath('/home/runner/.claude'), claude_bin='claude', cwd=None, remote_control=False, permission_mode=None, euid=None)[source]

(shell command, warning) that resumes facts’ session as it was launched.

The one place that assembles the line: cd into the cwd it was started in, the account’s environment (claude_home), --resume <id>, the permission flags and the model (launch_opts()), every token shell-quoted. The command is for a fresh shell and carries no secret. It never runs claude_bin: every flag is written, so run it where that claude is new enough (xa.claude_cli.prepare_spawn() is what probes). warning is what had to be dropped (bypass refused to root), else None.

>>> from xa.claude_fs import LaunchFacts
>>> f = LaunchFacts('s1', Path('/t.jsonl'), '/w x', '', False, None, None, None, None, 0.0)
>>> cmd, warning = resume_shell_command(f, claude_home=Path('/nonexistent/.claude'))
>>> cmd.startswith("cd '/w x' && exec env"), '--resume s1' in cmd, warning
(True, True, None)
Return type:

tuple[str, Optional[str]]