Skip to content

feat(agent): resume codex cloud threads natively on restarts#3216

Merged
k11kirky merged 2 commits into
mainfrom
posthog-code/codex-native-cloud-resume
Jul 7, 2026
Merged

feat(agent): resume codex cloud threads natively on restarts#3216
k11kirky merged 2 commits into
mainfrom
posthog-code/codex-native-cloud-resume

Conversation

@k11kirky

@k11kirky k11kirky commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Problem

The Codex adapter already supports native resume (thread/resume via ACP resumeSession), but the cloud agent-server never used it — prepareNativeResume hard-returned null for anything non-Claude. Every Codex cloud resume therefore started a fresh thread and injected the prior conversation as a hidden Markdown blob, losing reasoning context, compaction state, structured tool history, and prompt-cache continuity.

Change

  • prepareNativeResume now handles Codex: it finds the prior session id from the run log (for Codex the ACP sessionId is the codex thread id) and checks whether the thread's rollout file still exists under CODEX_HOME/sessions/ via the new hasCodexThreadState helper. If so, the server calls resumeSession and the codex app-server restores the full thread natively — reasoning items, compaction state and all — with the continuation prompt riding the resumed thread.
  • The rollout only survives a snapshot (warm) sandbox restart, and there is no faithful cold-hydration equivalent (rollouts contain encrypted reasoning we can't reconstruct from logs), so a cold sandbox keeps today's Markdown-summary fallback. Warm resumes also skip the redundant git-checkpoint restore, matching Claude's warm path.
  • Safety net: if resumeSession fails at boot (e.g. a corrupt rollout), the server logs a warning and falls back to a fresh session with the summary resume instead of failing the run. Applies to Claude too, which previously had no fallback there.

Testing

  • 5 new unit tests for the rollout lookup, 3 new agent-server tests covering warm-resume, thread-gone, and no-session-id paths.
  • Full @posthog/agent suite: 1093 passed; remaining failures are pre-existing on main and sandbox-environment artifacts (ambient GH_TOKEN, chmod no-op as root).
  • tsc --noEmit and Biome clean.

Follow-up (not in this PR)

  • Desktop→cloud handoffs could ship the rollout file into the sandbox/checkpoint to make cold restores native too — needs a transport for CODEX_HOME state.
  • "Can this session be natively resumed" could become an adapter capability instead of per-adapter branching in the server, but Claude's check isn't adapter-self-contained (hydration needs the server's API client), so that refactor is deferred.

prepareNativeResume was Claude-only, so every codex cloud resume started a
fresh thread and injected the prior conversation as a hidden markdown blob,
dropping reasoning context, compaction state, and cache continuity.

Now, when the thread's rollout file survived in CODEX_HOME (snapshot/warm
sandbox restart), the server resumes the thread via the adapter's native
thread/resume path. Cold restores keep the summary fallback (rollouts hold
encrypted reasoning that cannot be rebuilt from logs), and a failed native
resume now falls back to a fresh session instead of failing the run.

Generated-By: PostHog Code
Task-Id: 958c74c2-c281-438d-b112-f74dab2d1e48
@trunk-io

trunk-io Bot commented Jul 7, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 30042b7.

@greptile-apps

greptile-apps Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. packages/agent/src/server/agent-server.test.ts, line 1840-1845 (link)

    P2 Two null-expectation cases are good candidates for it.each

    The "thread state gone" and "no prior session id" tests both call prepare(codexServer(…)) and assert toBeNull(). They differ only in the sessionId passed to codexServer. An it.each with [THREAD_ID, "no rollout present"] / [null, "no session id"] pairs would reduce duplication and align with the repo's parameterised-test preference.

    Context Used: Do not attempt to comment on incorrect alphabetica... (source)

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "feat(agent): resume codex cloud threads ..." | Re-trigger Greptile

Comment thread packages/agent/src/adapters/codex-app-server/thread-state.ts
Comment thread packages/agent/src/adapters/codex-app-server/thread-state.test.ts Outdated
- drop the no-op .toString() on readdir entries (string[] in this form)
- parameterise the identical-setup thread-state and codex fallback tests
  with it.each per repo convention

Generated-By: PostHog Code
Task-Id: 958c74c2-c281-438d-b112-f74dab2d1e48
@k11kirky k11kirky requested a review from tatoalo July 7, 2026 10:23
@k11kirky k11kirky changed the title feat(agent): resume codex cloud threads natively on warm restarts feat(agent): resume codex cloud threads natively on restarts Jul 7, 2026
@k11kirky k11kirky merged commit 735b4d9 into main Jul 7, 2026
24 checks passed
@k11kirky k11kirky deleted the posthog-code/codex-native-cloud-resume branch July 7, 2026 10:49
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.

2 participants