Repository navigation
feat: restore Codex, OpenCode and Pi sessions - #57
Conversation
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
left a comment
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
nit: os.Link has no fallback on filesystems without hard-link support (some network or FAT mounts). I couldn't test this locally.
There was a problem hiding this comment.
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.
| case "claude": | ||
| return restoreClaudeContext(store, name, *project, *jsonOutput) | ||
| case "codex": | ||
| result, err = store.RestoreCodex(name, *project, codexHome()) |
There was a problem hiding this comment.
nit: --project doesn't affect where Codex restores, but the "Next: cd && codex resume" hint implies it does.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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 theattachhooks, as it already did for Claude Code.$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 ownsession_metarecord.$PI_CODING_AGENT_DIR/sessions/--<project>--/(default~/.pi/agent), or$PI_CODING_AGENT_SESSION_DIR.opencode importin the destination project;opencodemust be onPATH.Safety
.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.--projectdoes not affect Codex restore; the hint iscodex resume <id>.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 captureis still Claude Code only; the other harnesses are captured throughattachhooks.Context: rossoctl/moca#519 (docs follow-up), where the docs describe capture and restore for all four harnesses.
🤖 Generated with Claude Code