-
Notifications
You must be signed in to change notification settings - Fork 53
feat(agent): persist native session state in workspace snapshots #3333
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { | ||
| lstat, | ||
| mkdir, | ||
| mkdtemp, | ||
| readFile, | ||
| readlink, | ||
| rm, | ||
| writeFile, | ||
| } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { configurePersistentAgentState } from "./persistent-agent-state"; | ||
|
|
||
| describe("configurePersistentAgentState", () => { | ||
| let testRoot: string; | ||
| let homeDir: string; | ||
| let stateRoot: string; | ||
|
|
||
| beforeEach(async () => { | ||
| testRoot = await mkdtemp(path.join(tmpdir(), "persistent-agent-state-")); | ||
| homeDir = path.join(testRoot, "home"); | ||
| stateRoot = path.join(testRoot, "workspace", ".posthog", "agent-state"); | ||
| vi.stubEnv("CLAUDE_CONFIG_DIR", ""); | ||
| vi.stubEnv("CODEX_HOME", ""); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| vi.unstubAllEnvs(); | ||
| await rm(testRoot, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("routes native session state into the durable workspace root", async () => { | ||
| const claudeSkillsDir = path.join(homeDir, ".claude", "skills"); | ||
| const codexConfigPath = path.join(homeDir, ".codex", "config.toml"); | ||
| await mkdir(claudeSkillsDir, { recursive: true }); | ||
| await mkdir(path.dirname(codexConfigPath), { recursive: true }); | ||
| await writeFile(path.join(claudeSkillsDir, "catalog.txt"), "fresh"); | ||
| await writeFile(codexConfigPath, "fresh = true"); | ||
|
|
||
| await configurePersistentAgentState(stateRoot, homeDir); | ||
|
|
||
| const mappings = [ | ||
| [".claude/projects", "claude/projects"], | ||
| [".claude/session-env", "claude/session-env"], | ||
| [".claude/plans", "claude/plans"], | ||
| [".claude/todos", "claude/todos"], | ||
| [".codex/sessions", "codex/sessions"], | ||
| [".codex/shell_snapshots", "codex/shell_snapshots"], | ||
| ]; | ||
|
|
||
| for (const [sourceRelative, targetRelative] of mappings) { | ||
| const source = path.join(homeDir, sourceRelative); | ||
| const target = path.join(stateRoot, targetRelative); | ||
| expect((await lstat(source)).isSymbolicLink()).toBe(true); | ||
| expect(path.resolve(path.dirname(source), await readlink(source))).toBe( | ||
| target, | ||
| ); | ||
| await writeFile(path.join(source, "marker"), sourceRelative); | ||
| await expect( | ||
| readFile(path.join(target, "marker"), "utf-8"), | ||
| ).resolves.toBe(sourceRelative); | ||
| } | ||
|
|
||
| await configurePersistentAgentState(stateRoot, homeDir); | ||
|
|
||
| await expect( | ||
| readFile(path.join(claudeSkillsDir, "catalog.txt"), "utf-8"), | ||
| ).resolves.toBe("fresh"); | ||
| await expect(readFile(codexConfigPath, "utf-8")).resolves.toBe( | ||
| "fresh = true", | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { lstat, mkdir, readlink, rm, symlink } from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| interface PersistentDirectory { | ||
| source: string; | ||
| target: string; | ||
| } | ||
|
|
||
| function isMissingPathError(error: unknown): boolean { | ||
| return ( | ||
| error instanceof Error && | ||
| "code" in error && | ||
| (error as NodeJS.ErrnoException).code === "ENOENT" | ||
| ); | ||
| } | ||
|
|
||
| async function linkPersistentDirectory({ | ||
| source, | ||
| target, | ||
| }: PersistentDirectory): Promise<void> { | ||
| const resolvedSource = path.resolve(source); | ||
| const resolvedTarget = path.resolve(target); | ||
| if (resolvedSource === resolvedTarget) return; | ||
|
|
||
| await mkdir(resolvedTarget, { recursive: true }); | ||
| await mkdir(path.dirname(resolvedSource), { recursive: true }); | ||
|
|
||
| try { | ||
| const sourceStats = await lstat(resolvedSource); | ||
| if (sourceStats.isSymbolicLink()) { | ||
| const currentTarget = path.resolve( | ||
| path.dirname(resolvedSource), | ||
| await readlink(resolvedSource), | ||
| ); | ||
| if (currentTarget === resolvedTarget) return; | ||
| } | ||
| await rm(resolvedSource, { recursive: true, force: true }); | ||
| } catch (error) { | ||
| if (!isMissingPathError(error)) throw error; | ||
| } | ||
|
|
||
| await symlink(resolvedTarget, resolvedSource, "dir"); | ||
| } | ||
|
|
||
| export async function configurePersistentAgentState( | ||
| stateRoot: string, | ||
| homeDir: string = os.homedir(), | ||
| ): Promise<void> { | ||
| if (!path.isAbsolute(stateRoot)) { | ||
| throw new Error("Persistent agent state root must be an absolute path"); | ||
| } | ||
|
|
||
| const claudeConfigDir = | ||
| process.env.CLAUDE_CONFIG_DIR || path.join(homeDir, ".claude"); | ||
| const codexHome = process.env.CODEX_HOME || path.join(homeDir, ".codex"); | ||
| const directories: PersistentDirectory[] = [ | ||
| { | ||
| source: path.join(claudeConfigDir, "projects"), | ||
| target: path.join(stateRoot, "claude", "projects"), | ||
| }, | ||
| { | ||
| source: path.join(claudeConfigDir, "session-env"), | ||
| target: path.join(stateRoot, "claude", "session-env"), | ||
| }, | ||
| { | ||
| source: path.join(claudeConfigDir, "plans"), | ||
| target: path.join(stateRoot, "claude", "plans"), | ||
| }, | ||
| { | ||
| source: path.join(claudeConfigDir, "todos"), | ||
| target: path.join(stateRoot, "claude", "todos"), | ||
| }, | ||
| { | ||
| source: path.join(codexHome, "sessions"), | ||
| target: path.join(stateRoot, "codex", "sessions"), | ||
| }, | ||
| { | ||
| source: path.join(codexHome, "shell_snapshots"), | ||
| target: path.join(stateRoot, "codex", "shell_snapshots"), | ||
| }, | ||
| ]; | ||
|
|
||
| await Promise.all(directories.map(linkPersistentDirectory)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If one mapping fails with a filesystem error while the other |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
POSTHOG_AGENT_STATE_DIRis enabled after Claude or Codex has already written native state, an existingprojectsorsessionsdirectory reaches thisrmbefore any migration happens. The directory is deleted and then replaced with a symlink to an empty durable target, so previously resumable native sessions are lost instead of being preserved.