Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): persist native session state in directory snapshots (#116925)

Alessandro Pogliaghiafb39a94afaa
diff --git a/docs/internal/sandboxes-setup-guide.md b/docs/internal/sandboxes-setup-guide.md
index 14e97d1e52bf..c1897ba57ad5 100644
--- a/docs/internal/sandboxes-setup-guide.md
+++ b/docs/internal/sandboxes-setup-guide.md
@@ -133,6 +133,11 @@ keeps the live sandbox. The event relay stays active until startup finishes, and
an abandoned handoff with new activity requests a fresh snapshot.
Directory resume snapshots cover `/tmp/workspace`, including nested Git worktrees
and agent state. Paths outside that directory are not included.
+Cloud launches set `POSTHOG_AGENT_STATE_DIR=/tmp/workspace/.posthog/agent-state`.
+The agent server links Claude's projects, session environment, plans and todos, and
+Codex's sessions and shell snapshots into that directory before starting a session.
+Authentication and platform configuration stay outside these mappings. Pi restores
+its session history through the task-session API.
## Running via the UI
diff --git a/products/tasks/backend/logic/services/agent_server_launcher.py b/products/tasks/backend/logic/services/agent_server_launcher.py
index 6f4aa21de26b..5fa95f235871 100644
--- a/products/tasks/backend/logic/services/agent_server_launcher.py
+++ b/products/tasks/backend/logic/services/agent_server_launcher.py
@@ -386,6 +386,7 @@ class AgentServerLaunchMixin(SandboxBase):
unset_flags = "".join(f"-u {name} " for name in SANDBOX_AGENT_LAUNCH_UNSET_ENV_VARS)
server_cmd = (
f"env {unset_flags}BASH_ENV={shlex.quote(BASH_ENV_SCRIPT)} "
+ f"POSTHOG_AGENT_STATE_DIR={shlex.quote(f'{WORKING_DIR}/.posthog/agent-state')} "
f"{env_prefix}./node_modules/.bin/agent-server --port {AGENT_SERVER_PORT}{repo_flag} "
f"--taskId {shlex.quote(task_id)} --runId {shlex.quote(run_id)} --mode {shlex.quote(mode)}"
f"{create_pr_flag}{auto_publish_flag}{branch_flag}{mcp_servers_arg}{relay_mcp_servers_arg}"
diff --git a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py b/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
index 5c620b436cce..feb7ff86ee4c 100644
--- a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
+++ b/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
@@ -617,6 +617,7 @@ class TestModalSandboxAgentServer:
assert f"--taskId {shlex.quote('task-123')}" in command
assert f"--runId {shlex.quote('run-456')}" in command
assert f"POSTHOG_SANDBOX_ID={shlex.quote(mock_sandbox.id)}" in command
+ assert "POSTHOG_AGENT_STATE_DIR=/tmp/workspace/.posthog/agent-state" in command
assert "--sandboxId" not in command
assert f"--mode {shlex.quote('background')}" in command
assert "--createPr true" in command