Skip to content

About

Agents resume their own session after a reboot, via OSC 7501 resume=

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

agent-resume

Coding agents come back in their own conversation after a reboot, in the same terminal. Each agent's hook tells the terminal runtime how to resume it. The runtime keeps that command and runs it in the restored terminal.

It works with herdr-core 0.1.23 and later, which implements the Program Resume Protocol (OSC 7577). Outside herdr-core ($HC_TERMINAL_ID or $OSC7577_TOKEN unset) everything here does nothing.

Install

./install            # lists the agents it can install
./install claude     # installs one

For each agent you name, it:

  1. copies resume-report to ~/.local/bin (BIN_DIR changes that, and it has to be on PATH);
  2. copies that agent's hook or plugin into the agent's own folder;
  3. adds the hook to the agent's settings, keeping everything else in them.

Running it again updates. Nothing points back into the checkout, so you can move or delete it afterwards.

  • ./install --dry-run <agent> prints what it would change, and changes nothing.
  • ./install --remove <agent> undoes it. resume-report stays, since other agents may use it.
  • It needs jq.
  • A settings file that is a link (a dotfile manager such as chezmoi or stow owns it) is never edited. The installer prints the snippet to add instead.

To install by hand (no jq, or you'd rather see every step), follow MANUAL_INSTALL.md.

Codex: installing it turns off Codex's shared app-server daemon (daemon_auto_start = false), and with it the "← for agents" view (codex agents). With the daemon on, every Codex window reports into the first window's terminal. See Codex.

How it works

bin/resume-report COMMAND [ARG...] is the only shared piece, and it knows no agents. Each agent's own hook calls it with that agent's full resume command:

resume-report claude --resume 7f3a…

It writes one OSC 7577 report to the agent's terminal:

ESC ] 7577 ; pid=<agent pid>:time=<unix seconds>:argv=Y2xhdWRl,LS1yZXN1bWU=,…:mac=<sha256 hex> ESC \
  • Format: each argument is standard base64 (padded), and arguments are separated by commas. pid is the agent's process, not the hook's.
  • Token: herdr-core gives each terminal a secret OSC7577_TOKEN, and the hooks inherit it. mac is the SHA-256 of everything before :mac=, then :, then the token. herdr-core takes a report only with the right mac, a time within a minute of its clock, and a pid that is a live process of the same user under the terminal. Text that merely shows up on screen, such as a file or an agent's output, can't plant a command. Without a token, nothing is written.
  • Which terminal: hooks have no terminal of their own, so the script walks up its parent processes to the nearest one named COMMAND and writes to that process's terminal. If that process has no terminal (a nested claude -p, for example), nothing is written. An agent whose process isn't named like its command names its process in RESUME_REPORT_PID instead.
  • Errors: it never fails and never writes to stdout, so it can't break a hook or reach the model's context.

herdr-core ties the command to the agent's process. It drops the command when that process exits, keeps it through a reboot or other loss, and takes a newer report from the same process in its place, so reporting again on every turn is fine. It runs the command in the agent's own working directory. It needs base64 and sha256sum (or shasum -a 256 on macOS).

Agents

Agent ./install Reports When
Claude Code claude claude --resume <session_id> (plain claude before the first message) SessionStart, Stop
Codex codex codex resume <session_id> SessionStart, UserPromptSubmit
amp amp amp threads continue <thread> session.start, agent.end
opencode 2 opencode opencode --session <id> the open session changes (checked each second)
goose goose goose session --resume --session-id <id> SessionStart, UserPromptSubmit
Grok grok grok --resume <session_id> SessionStart, Stop
Antigravity CLI agy agy --conversation <conversationId> SessionStart, Stop
Qwen Code qwen qwen --resume <session_id> (plain qwen before the first message) SessionStart, Stop

Each agent's files are in agents/<name>/, with its install steps.

Pi (1.7.0 and later) and Crush (0.103.0 and later) report OSC 7577 themselves, so they need nothing from here. Both remove OSC7577_TOKEN before any hook or extension runs, so a hook couldn't report for them anyway.

A resume command brings back the conversation, not the options the agent was started with. An agent started with a model flag (agy --model …, grok -m …, amp -m low) comes back on its default model.

Codex

Codex's hooks run in a shared app-server daemon, a child of the first Codex window, which every later window reuses. Every window's hooks would then report into the first window's terminal. With daemon_auto_start = false, each window runs its own server and its hooks run under that window. Sub-agents still work, and their hooks don't fire, so the parent's command stays.

Warning: this turns off Codex's "← for agents" view (codex agents, the Agent Command Center), which browses sessions on the shared daemon. Without this setting, only use the Codex hook if you run one Codex window at a time. With more, the first window gets the wrong resume command and the others get none.

The setting only stops a window from starting the daemon. A window still joins one that is already running, and codex agents (or remote control) starts one even with the setting off. Windows opened while it runs report into the wrong terminal again, until it exits. Codex 0.168.0 briefly dropped the setting and then brought it back (#58940, #59447). Starting every window with codex --no-daemon rules all of this out, but it is a command-line flag, so it needs an alias or wrapper of your own.

Codex hooks fire on the first real turn, not when a window opens or codex resume runs.

Notes on the others

  • amp runs plugins in a helper process that is also named amp and has no terminal, under the window's amp. That is why resume-report walks on through a same-named parent.
  • opencode 2 plugins have two parts. The server part runs in the background service that every window shares, so it can't know its terminal. The TUI part runs in the window's own process. This plugin is TUI only. It reads context.ui.router.current(), which is {type: "session", sessionID} while a session is open.
  • agy starts a session with its first prompt, so a window that was never used reports nothing.
  • goose saves a session when it starts, so even an unused window comes back in its own session.
  • Grok also runs the hooks in ~/.claude/settings.json. The Claude hook finds no claude process above it there and does nothing.
  • agy reads a JSON reply from its hooks on stdout, so its hook answers {}.
  • Qwen Code runs as a Node script, so its process is named node-MainThread. Its hook runs as Qwen's own child and names its parent in RESUME_REPORT_PID.

Tests

tests/run.sh                         # encoding, hook input, the installer
tests/run.sh /path/to/herdr-core     # plus end to end in an isolated core

The installer tests use a scratch home folder. The end-to-end part runs its own core, with its own HC_SESSION and scratch dirs under /var/tmp, and removes them afterwards. It never touches your running core.

Working on this repo, or adding an agent: see AGENTS.md.

License

MIT, see LICENSE.

About

Agents resume their own session after a reboot, via OSC 7501 resume=

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages