Skip to content

feat: support multi-tab OSC 7577 terminal program resume - #5065

Merged
aheritier merged 6 commits into
mainfrom
feat/osc-7577-session-resume
Dec 1, 2026
Merged

aheritier merged 6 commits into
mainfrom
feat/osc-7577-session-resume

Conversation

@aheritier

@aheritier aheritier commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Add per-process, instance-based terminal program resume via OSC 7577 (docker-agent run --resume-instance <id>) so compatible terminals (such as Rex) can restore an interactive standalone docker-agent TUI process with its ordered persisted session tabs and active tab after a terminal restart.

Closes #5062

Reference Resources

Reviewers can use these references to evaluate protocol compliance, threat model, and terminal integration:

Design & Architecture

  1. Protocol & Verified Private Instance Storage (pkg/programresume)

    • Encodes authenticated OSC 7577 report and clear sequences (\x1b]7577;...mac=<sha256>\x1b\\) with strict token, path, control-character, and 2048-byte validation.
    • Persists launch context and tab identities (never OSC7577_TOKEN, credentials, or conversation messages) in versioned files under <data-dir>/resume-instances/<id>.json.
    • Walks every path component via open directory descriptors, verifies ownership, modes, symlinks (allowing only root-owned /private/* links on macOS), and ACLs (macOS extended ACLs via cgo; Linux system.posix_acl_access / system.nfs4_acl + local filesystem magic checks), confines file operations to a verified os.Root, fails closed on Windows and non-cgo macOS, and prunes abandoned files older than 30 days.
  2. Startup Isolation & Background Lifecycle Reporting (cmd/root/root.go, cmd/root/program_resume.go, pkg/selfupdate, pkg/session)

    • Strips OSC7577_TOKEN from os.Environ() immediately at startup before any child process or updater runs; passes a valid standalone token exclusively across a verified selfupdate re-exec and skips self-update on --resume-instance.
    • Reports only eligible standalone interactive full-TUI runs targeting a local regular agent file (rejecting aliases, built-ins, OCI/URL refs, plugins, sandboxes, remotes, prompts/attachments, unsupported flags, and model/gateway env overrides).
    • Performs metadata-only SQLite lookups (session.ResumeMetadataStore), disk writes, and OSC stdout writes off the Bubble Tea UI loop with 1s trailing throttle coalescing, 12h periodic mtime refresh, and bounded cleanup safe against wedged terminals or forced TUI shutdown.
    • Clears both the terminal resume record and instance file on intentional exit (/exit on the last tab or confirmed quit); preserves both on shutdown/hangup and re-reports after resume.
  3. Fail-Closed Preflight & Isolated Multi-Tab Restore (cmd/root/resume_instance.go, cmd/root/instance_preflight.go, cmd/root/resume_hooks.go, pkg/config/hooks_dropin.go, pkg/tui)

    • Validates the canonical agent file (loaded directly without aliases), runtime.sandbox: false, saved config directory, strict settings.hooks + hooks.d drop-ins, .agentsignore absence/inspectability, and every tab's session ID, working directory, symlink target, safety policy, and permissions fingerprint before starting any runtime.
    • Restores ordered tabs and active tab in isolation (tui.WithInstanceTabs) without reading or mutating shared restore_tabs (tuistate.db), and keeps explicit --session runs from overwriting saved restore_tabs layouts.
    • Keeps derived .agentsignore deny rules advisory in LocalRuntime.PermissionsInfo() (reading from the active filesystem toolset's captured AgentsIgnoreMatcher snapshot) rather than merging them into live Team.SetPermissions, preserving .agentsignore negation rules (*.key + !public.key) at runtime and keeping /permissions display in sync with startup enforcement.

Commit Breakdown

  1. feat(programresume): add OSC 7577 protocol and verified private instance store
  2. feat(resume): add metadata reader, strict hook drop-ins, and re-exec env handoff
  3. feat(tui): support isolated instance tab restoration and program resume hooks
  4. fix(permissions): keep derived .agentsignore deny rules advisory for /permissions
  5. feat(cli): wire OSC 7577 reporting and --resume-instance restoration
  6. docs(sessions): document OSC 7577 terminal program resume

How to Evaluate & Verify

  • Automated tests & linters (all passing locally and in CI):
    • go test -race -shuffle=on ./pkg/programresume/... ./pkg/session/... ./pkg/config/... ./pkg/selfupdate/... ./pkg/app/... ./pkg/tui/... ./cmd/root/...
    • Cross-compiles for Linux (linux/amd64), Windows (windows/amd64), and non-cgo macOS (CGO_ENABLED=0 darwin/arm64)
    • go run ./lint . and golangci-lint across macOS, Linux, and Windows targets
  • Manual evaluation in a supporting terminal (e.g. Rex):
    • Configure a Rex resume rule trusting the standalone docker-agent binary and run --resume-instance.
    • Launch docker-agent run ./agent.yaml, create/persist messages across one or more tabs, and restart the terminal (or send SIGHUP/SIGTERM) — verify the process resumes with its persisted tabs and active tab intact.
    • Launch again, quit intentionally with /exit (last tab) or confirmed Ctrl+C, and restart the terminal — verify the instance is cleared and does not auto-resume.
    • Note: live Rex restart/resume has not yet been verified manually on this branch; all protocol, lifecycle, preflight, and multi-tab restoration paths are covered by automated tests.

@aheritier
aheritier requested a review from a team as a code owner November 28, 2026 13:02
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/cli CLI commands, flags, output formatting kind/feat PR adds a new feature (maps to feat:). Use on PRs only. area/docs Documentation changes labels Nov 28, 2026
Copilot AI balanced review requested due to automatic review settings November 28, 2026 13:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved resume-reporting defects can prevent updates, erase records during shutdown, or restore the wrong workspace, while nonportable test fixtures fail blocking Windows CI.

6 open issues
What changed in this PR

Adds terminal-driven resume for supported standalone interactive sessions using authenticated OSC 7577 reports, building on existing session persistence.

Changes:

  • Reports the active persisted session and validates its working directory and saved safety policy on resume.
  • Isolates resume tokens from child processes and connects reporting and clearing to TUI lifecycle events.
  • Adds protocol and lifecycle tests, plus documentation of supported runs, trust requirements, and limitations.
File Description
pkg/​tui/​tui.go Connects resume callbacks to TUI updates and intentional exits.
pkg/​tui/​tabs.go Gives explicit sessions precedence over saved tabs.
pkg/​tui/​program_resume.go Coordinates active-session reporting and clearing.
pkg/​tui/​program_resume_test.go Tests lifecycle callbacks and explicit-session behavior.
pkg/​programresume/​report.go Encodes and authenticates OSC 7577 reports.
pkg/​programresume/​report_test.go Tests protocol encoding and input validation.
docs/​features/​sessions/​index.md Documents resume support, trust, and limitations.
cmd/​root/​run.go Integrates reporting and existing-session resume checks.
cmd/​root/​root.go Removes the token from the environment and skips self-update.
cmd/​root/​program_resume.go Selects supported runs and validates reportable sessions.
cmd/​root/​program_resume_test.go Tests persistence checks, token isolation, and supported flags.

Review effort: Balanced

Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume_test.go Resolved
Comment thread pkg/programresume/report_test.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
@aheritier
aheritier marked this pull request as draft November 28, 2026 22:41
@aheritier aheritier added the status/needs-rebase PR has merge conflicts or is out of date with main label Nov 28, 2026
@aheritier

Copy link
Copy Markdown
Collaborator Author

👋 This PR has merge conflicts with the base branch. Please rebase or merge the latest base branch and resolve them. I've added status/needs-rebase; it'll be picked back up automatically once the conflicts are cleared.

@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from 8a8393c to a6c42c4 Compare November 30, 2026 00:16
Copilot AI balanced review requested due to automatic review settings November 30, 2026 00:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread pkg/tui/tui.go Resolved
@aheritier aheritier removed the status/needs-rebase PR has merge conflicts or is out of date with main label Nov 30, 2026
Copilot AI balanced review requested due to automatic review settings November 30, 2026 08:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review from reading the diff and surrounding code (nothing was run). Two issues worth fixing before merge, three minor ones.

Also: the PR description still says the terminal restores "the active session, not the tab layout", while the code and docs now restore all tabs.

Comment thread pkg/tui/tui.go Resolved
Comment thread pkg/tui/tabs.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/root.go Resolved
Comment thread pkg/tui/tui.go Resolved
Comment thread cmd/root/resume_instance_test.go Resolved
Copilot AI balanced review requested due to automatic review settings November 30, 2026 11:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Startup self-update loses captured resume tokens during re-exec, while age-based pruning can delete live instance files, both breaking terminal-resume support.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread cmd/root/root.go Resolved
Comment thread pkg/programresume/instance.go Resolved
@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from 99408b8 to 4cad44c Compare November 30, 2026 12:58
Copilot AI balanced review requested due to automatic review settings November 30, 2026 12:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved bugs can deny files explicitly re-included by .agentsignore, leave resume disabled, stop instance refreshes, and prevent saved-state deletion during forced shutdown.

4 open issues
3 resolved since last review

Review effort: Balanced

Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/program_resume.go Resolved
Comment thread cmd/root/run.go Resolved
Copilot AI balanced review requested due to automatic review settings November 30, 2026 14:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

NFSv4 ACL inspection errors can bypass the security check, and the first tab can remain uninitialized, leaving its saved conversation and sidebar state unloaded.

2 open issues
2 resolved since last review

Review effort: Balanced

Comment thread pkg/programresume/instance_acl_linux.go Resolved
Comment thread pkg/tui/tabs.go Resolved
@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from f93b031 to 3bbf1d1 Compare November 30, 2026 23:07
Copilot AI balanced review requested due to automatic review settings November 30, 2026 23:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved resume flaws can accept changed workspace symlink targets, silently lose global permissions and hooks when the saved config directory is unavailable, and leave intentionally exited instances restorable without a valid OSC token.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread cmd/root/resume_instance.go Resolved
Comment thread cmd/root/resume_instance.go Resolved
Comment thread cmd/root/resume_instance.go Resolved
…nce store

Implement authenticated OSC 7577 report/clear encoding (`Report`, `Clear`,
`ValidToken`) and private per-process instance storage under
`<data-dir>/resume-instances/<id>.json`.

Instance storage walks every path component via open directory descriptors,
verifies ownership, modes, symlinks (allowing only root-owned `/private/*`
system links on macOS), and ACLs (macOS extended ACLs via cgo and Linux
POSIX/NFSv4 ACLs + local filesystem checks), confines file operations to a
verified `os.Root`, fails closed on Windows and non-cgo macOS, and supports
pruning abandoned files older than 30 days.

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
…env handoff

Add the runtime plumbing needed for safe, low-overhead terminal resume:
- `session.ResumeMetadataStore` to query only session identity, working
  directory, safety policy, and permissions without decoding conversation history.
- `config.LoadHookDropInsStrict` to fail closed on unreadable or invalid
  `hooks.d` drop-ins instead of skipping them silently.
- `selfupdate.Updater.ReExecEnv` so startup can strip `OSC7577_TOKEN` from
  `os.Environ()` immediately while handing a valid standalone token across a
  verified self-update `re-exec` without exposing it to child processes.

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from 3bbf1d1 to 49fd6e7 Compare December 1, 2026 08:10
Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The permissions dialog can claim files are blocked while runtime access remains allowed, and starring a fresh session can leave it unavailable for terminal restoration.

2 open issues
3 resolved since last review

Review effort: Balanced

Comment thread pkg/app/app.go Resolved
Comment thread pkg/tui/program_resume.go Resolved
…me hooks

Add TUI options and lifecycle wiring for per-process terminal resume:
- `WithProgramResume` to emit filtered UI tab snapshots on relevant state
  changes, re-report on `tea.ResumeMsg`, and clear only on intentional exit
  (`/exit` on the last tab or confirmed quit) while preserving state on shutdown.
- `WithInstanceTabs` to restore a resumed instance's ordered tabs and active
  tab without reading or mutating shared `restore_tabs` (`tuistate.db`) state.
- `WithExplicitSession` to give explicit `--session` precedence over
  `restore_tabs` without overwriting saved tab layouts, and clear failed
  pending restores cleanly (replaying parked attention dialogs on rollback).

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
…/permissions

Stop merging lossy permissions.FromAgentsIgnore deny patterns into live
Team.SetPermissions, which previously blocked gitignore negation rules such
as "*.key" followed by "!public.key".

Instead, preserve the raw patterns captured on fsx.AgentsIgnoreMatcher when
the filesystem toolset is constructed and expose them through
LocalRuntime.PermissionsInfo() as advisory deny rules for /permissions,
keeping the displayed rules aligned with the runtime's enforced snapshot
even if .agentsignore changes on disk later.

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from 49fd6e7 to 5745071 Compare December 1, 2026 08:36
Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Resume rejects a missing saved config directory even though a normal run can advertise an instance without creating it, leaving that instance unable to resume despite unchanged settings.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread cmd/root/program_resume.go Resolved
Wire per-process OSC 7577 reporting and `docker-agent run --resume-instance <id>`
restoration into the standalone CLI:
- Strip `OSC7577_TOKEN` at startup before child processes or self-update run,
  passing valid standalone tokens only via verified `selfupdate` re-exec and
  skipping self-update on `--resume-instance`.
- Report eligible standalone interactive TUI runs on a background worker off the
  UI loop with 1s trailing throttle coalescing, 12h periodic mtime refresh, and
  bounded shutdown/clear deletion safe against wedged terminals or forced exit.
- Preflight and restore all persisted tabs in their validated working
  directories using the instance's saved config directory, strict hook checks,
  alias-free local agent loading, `runtime.sandbox` rejection, and
  `.agentsignore` fail-closed policy before starting any runtime.

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
Document explicit `--session` precedence over `restore_tabs` and the OSC 7577
terminal program resume behavior, Rex trust rules, supported runs, security
preflight checks, lifecycle, and platform boundaries.

Signed-off-by: Arnaud Héritier <arnaud.heritier@docker.com>
@aheritier
aheritier force-pushed the feat/osc-7577-session-resume branch from 5745071 to 5182152 Compare December 1, 2026 08:51
Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Security-sensitive ACL checks and authenticated terminal restoration require human validation on supported Linux and cgo macOS builds with a real OSC 7577 terminal, as live Rex restart/resume remains unverified.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@aheritier
aheritier marked this pull request as ready for review December 1, 2026 09:58
@aheritier
aheritier added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 49be71b Dec 1, 2026
36 of 59 checks passed
@aheritier
aheritier deleted the feat/osc-7577-session-resume branch December 1, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli CLI commands, flags, output formatting area/docs Documentation changes area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/tui For features/issues/fixes related to the TUI kind/feat PR adds a new feature (maps to feat:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support terminal program resume with OSC 7577

3 participants