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(desktop): resolve cloud resume readiness from live sandbox state (#79265)

posthog[bot]78cf6c9b3522
diff --git a/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.test.ts b/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.test.ts
index cb87ada33cf6..ea0c27fdc4e2 100644
--- a/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.test.ts
+++ b/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.test.ts
@@ -241,7 +241,7 @@ describe("CloudPiSessionClient", () => {
}
});
- it("waits for a fresh start when a queued resume snapshot contains an old start", async () => {
+ it("waits for a fresh start when a resume snapshot's sandbox has stopped", async () => {
const cloud = createCloudTaskClient();
vi.mocked(cloud.client.sendCommand).mockResolvedValue({
success: true,
@@ -261,6 +261,7 @@ describe("CloudPiSessionClient", () => {
runId: "run-1",
kind: "snapshot",
status: "in_progress",
+ sandboxAlive: false,
newEntries: [{ type: "pi_run_started" }],
totalEntryCount: 1,
});
@@ -280,6 +281,35 @@ describe("CloudPiSessionClient", () => {
expect(cloud.client.sendCommand).toHaveBeenCalledOnce();
});
+ it("becomes ready from a live snapshot when the fetched status was stale", async () => {
+ const cloud = createCloudTaskClient();
+ vi.mocked(cloud.client.sendCommand).mockResolvedValue({
+ success: true,
+ result: {
+ type: "response",
+ command: "get_state",
+ success: true,
+ data: { isStreaming: false },
+ },
+ });
+ const session = new CloudPiSessionClient(cloud.client, context("queued"));
+ session.onConversationEvent(vi.fn(), vi.fn());
+
+ const state = session.client.getState();
+ cloud.sendUpdate({
+ taskId: "task-1",
+ runId: "run-1",
+ kind: "snapshot",
+ status: "in_progress",
+ sandboxAlive: true,
+ newEntries: [{ type: "pi_run_started" }],
+ totalEntryCount: 1,
+ });
+
+ await expect(state).resolves.toMatchObject({ isStreaming: false });
+ expect(cloud.client.sendCommand).toHaveBeenCalledOnce();
+ });
+
it("waits for subscription readiness before watching and only unsubscribes on cleanup", async () => {
const cloud = createCloudTaskClient(false);
vi.mocked(cloud.client.watch).mockImplementation(async () => {
diff --git a/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.ts b/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.ts
index 00eba1a0b3e6..ad1e8af98cca 100644
--- a/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.ts
+++ b/products/desktop/packages/core/src/pi-runtime/cloudPiSessionClient.ts
@@ -336,10 +336,15 @@ export class CloudPiSessionClient implements PiSession {
onError: (error: unknown) => void,
onCloudStatus?: (status: TaskRunStatus) => void,
): void {
+ // A snapshot's historical pi_run_started only proves the runtime is ready
+ // to take commands when the sandbox behind it is still alive. On a resume
+ // whose sandbox has stopped the sandbox reports dead, so we hold off until a
+ // fresh start arrives over the live log stream. The run status fetched when
+ // the session opened can lag reality, so trust the snapshot's own signals.
const snapshotCanProveReadiness =
update.kind === "snapshot" &&
- this.context.runStatus === "in_progress" &&
- update.status === "in_progress";
+ update.status === "in_progress" &&
+ update.sandboxAlive !== false;
const hasCurrentReadinessEvent =
(update.kind === "logs" || snapshotCanProveReadiness) &&
update.newEntries.some((entry) => entry.type === "pi_run_started");