Skip to content

fix(session): bind handoff/current to the active Codex session - #180

Merged
chonalchendo merged 4 commits into
masterfrom
anvil/codex-session-binding
Jun 4, 2026
Merged

chonalchendo merged 4 commits into
masterfrom
anvil/codex-session-binding

Conversation

@chonalchendo

Copy link
Copy Markdown
Owner

What

anvil session handoff and anvil session current resolved "the current session" solely from CLAUDE_CODE_SESSION_ID (resolveCurrentSession), erroring under Codex — which exports no session-id env var (openai/codex#8923) and has no SessionStart hook to pre-create the session file. The result: context could not be carried across Codex sessions.

This makes the write side work under Codex (resume/list already worked — they mtime-scan 10-sessions/):

  • resolveCurrentSession falls back to the active Codex session when CLAUDE_CODE_SESSION_ID is unset, reading the newest rollout transcript under $CODEX_HOME/sessions (default ~/.codex/sessions). The rollout filename's trailing id (after the fixed timestamp prefix) is the binding key — no JSONL parsing, agnostic to the id's internal shape.
  • session handoff lazily creates the session file (source=codex) on first call, since Codex has no SessionStart hook to do it.
  • The CLAUDE_CODE_SESSION_ID path stays first and unchanged; source now flows out of resolveCurrentSession so each CLI gets the right missing-file behaviour.

Refactor: extracts writeSessionFile (shared by session create and the handoff lazy-create) + shortID; adds codex to the session source enum.

Scope / non-goals

Newest-rollout-wins mirrors the existing single-active-session assumption (no concurrent-Codex-session disambiguation). No Codex SessionStart-equivalent hook (none exists; lazy-create covers it). Closes the milestone AC "the Codex session-binding gap is resolved."

Verification

  • Direct: go test ./internal/cli/ -run TestSessionCodexBinding — current resolves the rollout id; handoff lazily creates the file with source=codex and the written body.
  • Indirect (live, against built binary): a fake $CODEX_HOME rollout + env -u CLAUDE_CODE_SESSION_ID ./bin/anvil session handoff creates 10-sessions/<id>.md, session current resolves the id, and session resume --json reads the handoff back. Full suite 907 green; lint clean.

Closes anvil.0037.

🤖 Generated with Claude Code

chonalchendo and others added 2 commits June 3, 2026 18:07
`anvil session handoff`/`current` resolved the session id solely from
CLAUDE_CODE_SESSION_ID, erroring under Codex (which exports no such env
var, openai/codex#8923) and making cross-session context handoff
impossible. resolveCurrentSession now falls back to the active Codex
session, read from the newest rollout file under $CODEX_HOME/sessions
(default ~/.codex/sessions); the rollout filename's trailing id is the
binding key. Since Codex has no SessionStart hook to pre-create the
session file, the first handoff creates it lazily (source=codex). The
CLAUDE_CODE_SESSION_ID path stays first and unchanged.

Extracts writeSessionFile (shared by create + handoff lazy-create) and
adds "codex" to the session source enum.

Closes anvil.0037.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… source enum

- handing-off-session SKILL.md: document the Codex session-binding fallback
  (newest $CODEX_HOME rollout + lazy file creation) so an agent under Codex
  knows handoff is supported.
- vault-schemas.md: add `codex` to the session source enum reference, matching
  schemas/session.schema.json and validSessionSources.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@chonalchendo

Copy link
Copy Markdown
Owner Author

Addressed 3 findings from independent review (report ac968cb485cab1364) as of 0ad828f: 2 fixes, 1 skip-with-reason.

  • [high] handing-off-session SKILL.md:79 stale — fixed in 0ad828f: documents the Codex binding (newest $CODEX_HOME rollout + lazy file creation) so an agent under Codex knows handoff works.
  • [high] vault-schemas.md:254 source enum — fixed in 0ad828f: added codex, matching the schema + validSessionSources.
  • [low] codex cold-path double-write (session.go:143) — skip-with-reason. The first handoff of a Codex session writes the file twice (frontmatter via writeSessionFile, then body); it's idempotent on a single file and only on the cold path. Folding the body into the shared helper would add a body param used by one of two callers — net complexity for no behavioural gain. Reviewer concurred.

chonalchendo and others added 2 commits June 3, 2026 18:13
…gth gate)

The Codex-binding additions pushed session.go past the CI 500-line cap for
non-test Go files. Move the cohesive session-id resolution unit
(envSessionID, resolveCurrentSession, codexSessionID, codexRolloutID) into
session_resolve.go in the same package. No behaviour change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
When $CODEX_HOME/sessions holds rollout-*.jsonl files but none match the
expected rollout-<YYYY-MM-DDThh-mm-ss>-<id>.jsonl shape, say so and name the
shape — rather than reporting the generic "no active session" miss. This makes
a future Codex naming-format change diagnosable instead of silent. The
no-rollout-at-all path keeps its own message.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@chonalchendo
chonalchendo merged commit 9b090de into master Jun 4, 2026
1 check passed
@chonalchendo
chonalchendo deleted the anvil/codex-session-binding branch June 4, 2026 09:14
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.

1 participant