Skip to content

About

OpenCode plugin that lets your terminal bring a session back after it restarts, using the Program Resume Protocol (OSC 7577)

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

opencode-osc7577

CI License: MIT

An OpenCode plugin that tells your terminal how to bring a session back. When the terminal restarts after an update, a crash or a reboot, each pane reopens OpenCode on the session it was showing, instead of an empty shell.

It implements the program side of the Program Resume Protocol (OSC 7577), a generic terminal protocol. It works in any terminal that implements the protocol, such as Rex, and does nothing in one that doesn't.

Install

opencode plugin add github:oriori1703/opencode-osc7577

This adds the plugin to plugins in your global opencode.json(c). One entry loads both of its parts, the one in the server and the one in the terminal UI. To use a checkout instead, add its directory:

{
  "$schema": "https://opencode.ai/config.json",
  "plugins": ["/path/to/opencode-osc7577"],
}

OpenCode loads the plugin without a restart, and sessions that are already open start reporting right away.

There is nothing to configure. The plugin is written for OpenCode 2 and tested with 2.4.2.

What it reports

When What the terminal is told
You open or switch to a session Resume with opencode --session <id> in the current directory.
You go back to the home screen, for example with /new Forget the session. A new one is reported once it exists.
You quit OpenCode Forget the session.
The terminal ends OpenCode (SIGHUP or SIGTERM) Nothing, so the session stays remembered.
OpenCode is continued after being suspended The session again, in case another program replaced it.

Three of the flags OpenCode was started with are added to the command, because the session would come back as something else without them: --auto, --standalone and --server <url>. Every other flag is left out. --continue and --prompt, for example, picked a session or started a task, and --session does that job now.

Whether and when a program is resumed is up to the terminal. A report is a hint.

Rex

Rex has a built-in resume rule for opencode --session <id>, so a session started with plain opencode is resumed with no setup.

That rule trusts the command with nothing after it. A session started with --auto reports opencode --session <id> --auto, which the rule doesn't cover, so Rex opens a shell in its place. To have Rex resume those sessions too, replace the rule in your init.lua:

rex.set("resume.trust.opencode", {
  command = { "opencode", "--session", rex.match.id },
  flags = {
    ["--auto"] = "keep",
    ["--session"] = { "drop", values = 1 },
    ["-s"] = { "drop", values = 1 },
    ["--continue"] = "drop",
    ["-c"] = "drop",
    ["--prompt"] = { "drop", values = 1 },
  },
})

A rule with flags only resumes a program whose flags are all listed, which is why the ones that are dropped appear too. A session resumed with --auto approves every permission that isn't explicitly denied, without you starting it that way again, so add the rule only if you want that.

To see what a pane reported and whether Rex trusts it, list the pane's records:

rex block call -b BLOCK program_resume_records

Security

A resume command is something the terminal later runs as you, without you typing it. The protocol protects it with a token that the terminal puts in the environment of the programs it starts.

  • The plugin never writes the token anywhere. A report only carries a hash made with it.
  • The token is removed from OpenCode's environment and from the environment of every command a tool runs. Otherwise a command the agent runs could register a resume command of its own, which the terminal would run later, outside OpenCode's permission checks.
  • The only command the plugin reports is opencode --session <id> with the flags listed above. The session ID comes from OpenCode, never from a model or a file.

Limitations

  • Reports go through the output queue of OpenCode's renderer, so that they can't land in the middle of a frame. That queue isn't part of the plugin API. If a release of OpenCode removes it, the plugin writes to the terminal directly instead.
  • Developed and tested on macOS with Rex. Linux should behave the same. Windows is untested.

Development

npm install
npm test
npm run typecheck

The tests run the TypeScript sources directly, which needs Node.js 22.18 or later. There is no build step: OpenCode loads the sources as they are.

File Contents
src/osc7577.ts Builds reports. The tests check it against the examples in the specification.
src/launch.ts Picks the flags to keep from OpenCode's command line.
src/tui.ts The terminal UI part, which follows the session on screen and sends the reports.
src/index.ts The server part, which keeps the token away from tool commands.

scripts/pty-reports.py shows what OpenCode really sends. It runs a command in a pseudo-terminal with a token of its own and lists the reports it sees. With the plugin loaded by your OpenCode:

python3 scripts/pty-reports.py 'wait:5,sig:SIGHUP,wait:2' -- opencode --session ses_...

License

MIT

About

OpenCode plugin that lets your terminal bring a session back after it restarts, using the Program Resume Protocol (OSC 7577)

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages