-
Notifications
You must be signed in to change notification settings - Fork 54
feat(agent): resume codex cloud threads natively on restarts #3216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/agent/src/adapters/codex-app-server/thread-state.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { hasCodexThreadState } from "./thread-state"; | ||
|
|
||
| const THREAD_ID = "0199a5c3-2f60-7b21-9c39-1d2e3f4a5b6c"; | ||
|
|
||
| describe("hasCodexThreadState", () => { | ||
| let codexHome: string; | ||
|
|
||
| beforeEach(async () => { | ||
| codexHome = await mkdtemp(join(tmpdir(), "codex-home-")); | ||
| vi.stubEnv("CODEX_HOME", codexHome); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| vi.unstubAllEnvs(); | ||
| await rm(codexHome, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| const writeRollout = async (threadId: string) => { | ||
| const dir = join(codexHome, "sessions", "2026", "07", "07"); | ||
| await mkdir(dir, { recursive: true }); | ||
| await writeFile( | ||
| join(dir, `rollout-2026-07-07T10-00-00-${threadId}.jsonl`), | ||
| "", | ||
| ); | ||
| }; | ||
|
|
||
| it("finds a persisted rollout for the thread id", async () => { | ||
| await writeRollout(THREAD_ID); | ||
| await expect(hasCodexThreadState(THREAD_ID)).resolves.toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when only a different thread's rollout exists", async () => { | ||
| await writeRollout(THREAD_ID); | ||
| await expect( | ||
| hasCodexThreadState("11111111-2222-3333-4444-555555555555"), | ||
| ).resolves.toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when there is no sessions directory", async () => { | ||
| await expect(hasCodexThreadState(THREAD_ID)).resolves.toBe(false); | ||
| }); | ||
|
|
||
| it("returns false for an empty thread id", async () => { | ||
| await writeRollout(THREAD_ID); | ||
| await expect(hasCodexThreadState("")).resolves.toBe(false); | ||
| }); | ||
|
|
||
| it("ignores files that are not rollouts", async () => { | ||
| const dir = join(codexHome, "sessions", "2026", "07", "07"); | ||
| await mkdir(dir, { recursive: true }); | ||
| await writeFile(join(dir, `notes-${THREAD_ID}.jsonl`), ""); | ||
| await expect(hasCodexThreadState(THREAD_ID)).resolves.toBe(false); | ||
| }); | ||
| }); | ||
25 changes: 25 additions & 0 deletions
25
packages/agent/src/adapters/codex-app-server/thread-state.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { readdir } from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| /** | ||
| * Whether codex still holds the persisted rollout for `threadId` — written as | ||
| * `CODEX_HOME/sessions/YYYY/MM/DD/rollout-<timestamp>-<threadId>.jsonl` — i.e. | ||
| * whether `thread/resume` can restore the thread natively. Mirrors codex's own | ||
| * CODEX_HOME resolution (env override, else ~/.codex). | ||
| */ | ||
| export async function hasCodexThreadState(threadId: string): Promise<boolean> { | ||
| if (!threadId) return false; | ||
| const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex"); | ||
| const sessionsDir = path.join(codexHome, "sessions"); | ||
| const suffix = `-${threadId}.jsonl`; | ||
| try { | ||
| const entries = await readdir(sessionsDir, { recursive: true }); | ||
| return entries.some((entry) => { | ||
| const name = path.basename(entry.toString()); | ||
| return name.startsWith("rollout-") && name.endsWith(suffix); | ||
| }); | ||
|
k11kirky marked this conversation as resolved.
|
||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.