Skip to content

feat: restore Codex, OpenCode and Pi sessions - #57

Merged
moonlight16 merged 2 commits into
mainfrom
feat/restore-all-harnesses
Oct 15, 2026
Merged

moonlight16 merged 2 commits into
mainfrom
feat/restore-all-harnesses

Conversation

@moonlight16

@moonlight16 moonlight16 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Closes #55 (restore native harness state for Codex, OpenCode and Pi).

contextctl ctx restore NAME --harness codex|opencode|pi [--project P] [--json] restores sessions captured by the attach hooks, as it already did for Claude Code.

Harness Where restore writes
Codex $CODEX_HOME/sessions/YYYY/MM/DD/rollout-<time>-<id>.jsonl (default ~/.codex). Capture keeps only <id>.jsonl, so the file name is rebuilt from the session's own session_meta record.
Pi $PI_CODING_AGENT_DIR/sessions/--<project>--/ (default ~/.pi/agent), or $PI_CODING_AGENT_SESSION_DIR.
OpenCode OpenCode keeps sessions in SQLite, so restore runs opencode import in the destination project; opencode must be on PATH.

Safety

  • Codex and Pi restore check every session before writing any, including content conflicts and directory links in each destination path, so a refusal leaves local state untouched. OpenCode restore checks that no session exists before importing any, but imports are not atomic across sessions: if a later import fails, earlier imports remain.
  • Writes stay inside the session directory: symlinks, special files, unsafe session ids and symlinked directories are refused, and a file that appears mid-restore is never replaced.
  • A compressed (.jsonl.zst) local Codex copy is refused with a clear message, since restore does not compare compressed rollouts. Without hard-link support, restore falls back to an exclusive create, which still never overwrites. --project does not affect Codex restore; the hint is codex resume <id>.
  • Codex and Pi work per session, because Codex's session directory is shared across projects: an identical session is skipped, a different one is refused. OpenCode refuses a session that already exists.

Tested: a capture → bundle → restore round trip into a fresh home for each harness, refusal of newer local state, confinement cases, and a manual run in temporary homes where all four harnesses restored identically. Not yet tested: resuming a restored session in a real Codex, OpenCode or Pi; the layouts follow each harness's source (Codex main, OpenCode v1.19, pi-fork).

Translating state between harnesses (for example Claude Code → Codex) remains out of scope. Manual ctx capture is still Claude Code only; the other harnesses are captured through attach hooks.

Context: rossoctl/moca#519 (docs follow-up), where the docs describe capture and restore for all four harnesses.

🤖 Generated with Claude Code

contextctl ctx restore --harness codex|opencode|pi restores sessions captured by attach hooks, as it already did for Claude Code. Codex and Pi sessions are written back into the harness's session directory; OpenCode sessions are imported with opencode import. Restore checks every session before writing, writes only inside the session directory, and skips an identical session or refuses a different one.

Closes #55.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Jeremy Cohn <[email protected]>

@evaline-ju evaline-ju left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran the capture → restore flow locally with contextctl. OpenCode restore used a real opencode 1.18.30 in isolated XDG dirs, and import bound the session to the destination project. Codex and Pi restore were exercised only with hand-written fixture sessions, because I didn't have those harnesses set up. I did not check Codex's rollout layout or filename timestamp (local time vs UTC), the session_meta shape, Pi's project-key naming, or resuming a restored session in either harness. The PR description notes the same gap. A symlinked directory in the destination never let a write escape the session root. I only tested on macOS.

}
plan = append(plan, planned{session: session, destination: filepath.Join(root, relative), data: data})
}
for _, item := range plan {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: "Every session is checked before anything is written, so a refusal leaves local state untouched" doesn't hold for confinement refusals, which are only detected in writeConfinedFile during this write loop. I reproduced it with two captured Codex sessions (2026 and 2027) and sessions/2027 as a symlink: the 2026 rollout was written, then the 2027 session failed with refusing to write outside, exit 1, leaving partial state. Nothing escaped the root. Consider validating each planned destination path before the first write, or narrowing the wording in the PR body, docs/harness-attachments.md, and the help text. The guarantee does hold for content conflicts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for reproducing it. Fixed in 1b05ee6: the planning phase now checks every Codex and Pi destination's directories for links before the first write, and the write still re-checks. Your case (2026 and 2027 sessions, sessions/2027 as a link) is now a test: restore refuses with nothing written. One behavior change: an identical copy reached through a planted directory link is now refused rather than reported as already present.

local time.
- OpenCode stores sessions in its own database, so restore runs `opencode import` instead of
writing files. OpenCode must be on `PATH`. Import binds each session to the destination project.
If an import fails partway, the sessions imported before the failure remain.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: The docs warn about partial OpenCode imports but not partial Codex/Pi writes (see the comment on restoreSessionFiles). If you keep the late-failure behavior, apply the same note to all harnesses.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 1b05ee6: the docs and help text state the guarantee per harness. Codex and Pi refuse before writing anything; OpenCode imports can still leave earlier sessions if a later import fails.

}

// findCodexRollouts finds local rollouts, live or archived, for a thread ID.
func findCodexRollouts(codexHome, id string) ([]string, error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: findCodexRollouts matches .jsonl.zst, but restore compares raw bytes against the plain rollout. A compressed archived copy of the same thread is always reported as "already exists with different content" (I reproduced this with an empty .zst). The refusal is safe but misleading, so a clearer message would help.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 1b05ee6: a compressed copy is now refused with "a compressed copy of Codex session exists at ; restore does not compare compressed rollouts", with a test.

if err := temporary.Close(); err != nil {
return err
}
if err := os.Link(temporary.Name(), destination); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: os.Link has no fallback on filesystems without hard-link support (some network or FAT mounts). I couldn't test this locally.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 1b05ee6: on EXDEV, EPERM, ENOTSUP or EOPNOTSUPP from the link, restore falls back to an exclusive create (O_CREATE|O_EXCL), which still never replaces a file. Tested with an injected EXDEV.

Comment thread cmd/contextctl/main.go
case "claude":
return restoreClaudeContext(store, name, *project, *jsonOutput)
case "codex":
result, err = store.RestoreCodex(name, *project, codexHome())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: --project doesn't affect where Codex restores, but the "Next: cd && codex resume" hint implies it does.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 1b05ee6: the Codex hint is now codex resume <id> (Codex finds the session by id; it keeps its original working directory), and the help says --project does not change where Codex restores.

- Check every Codex and Pi destination's confinement before writing any session, so a refusal never leaves partial state.
- Refuse a compressed (.jsonl.zst) local Codex copy with a clear message.
- Fall back to an exclusive create when the filesystem has no hard links.
- The Codex hint is now codex resume <id>; --project does not affect Codex restore.
- Docs state the per-harness guarantee; OpenCode imports remain non-atomic across sessions.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Jeremy Cohn <[email protected]>

@evaline-ju evaline-ju left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fixes in 1b05ee6 address my earlier comments. I reran the partial-write repro (2026 and 2027 Codex sessions with sessions/2027 as a symlink) and restore now refuses with nothing written, and the compressed-copy refusal and the new codex resume <id> hint behave as described. I could only confirm the no-hard-link fallback through the injected-error test. Codex and Pi restore were exercised only with hand-written fixture sessions, because I didn't have those harnesses set up, so Codex's rollout layout and timestamp, Pi's project-key naming, and resuming a restored session in either harness remain unverified. I only tested on macOS.

@moonlight16
moonlight16 merged commit 7951826 into main Oct 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

contextctl: restore native harness state for Codex, OpenCode and Pi

2 participants