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.

feat(desktop): give local pi tasks a task run and tag their generations (#122376)

Brandon1871a6ee182a
diff --git a/packages/agent/packages/agent-contracts/src/task-context.ts b/packages/agent/packages/agent-contracts/src/task-context.ts
index 59b3fbbed86c..ac0d5e0f560b 100644
--- a/packages/agent/packages/agent-contracts/src/task-context.ts
+++ b/packages/agent/packages/agent-contracts/src/task-context.ts
@@ -1,5 +1,6 @@
export interface TaskContextInput {
taskId: string;
+ taskRunId?: string;
cwd: string;
customInstructions?: string;
additionalDirectories?: string[];
diff --git a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
index 4b51d9b1f061..cc7495281a7d 100644
--- a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
+++ b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
@@ -131,6 +131,7 @@ describe("DesktopPiRpcClientFactory", () => {
factory.create({
taskContext: {
taskId: "task-1",
+ taskRunId: "run-1",
cwd: "/workspace",
customInstructions: "Keep the patch small.",
additionalDirectories: ["/tmp/shared"],
@@ -143,7 +144,11 @@ describe("DesktopPiRpcClientFactory", () => {
legacyGatewayUrl: getLlmGatewayUrl(getCloudUrlFromRegion("dev-cloud")),
headers: {
"x-posthog-property-task_id": "task-1",
+ "x-posthog-property-task_run_id": "run-1",
+ "x-posthog-property-task_runtime_adapter": "pi",
+ "x-posthog-property-task_execution_environment": "local",
"x-posthog-property-$ai_session_id": "task-1",
+ "X-PostHog-Task-Run-Id": "run-1",
"X-PostHog-Project-Id": "1",
},
});
@@ -188,6 +193,7 @@ describe("DesktopPiRpcClientFactory", () => {
projectId: 1,
apiHost: "https://app.dev.posthog.dev",
taskId: "task-1",
+ taskRunId: "run-1",
cwd: "/workspace",
environment: "local",
customInstructions: "Keep the patch small.",
@@ -290,6 +296,8 @@ describe("DesktopPiRpcClientFactory", () => {
legacyGatewayUrl: getLlmGatewayUrl("https://us.posthog.com"),
headers: {
"x-posthog-property-task_id": "task-9",
+ "x-posthog-property-task_runtime_adapter": "pi",
+ "x-posthog-property-task_execution_environment": "local",
"x-posthog-property-$ai_session_id": "task-9",
"X-PostHog-Project-Id": "7",
},
diff --git a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
index d828165c7d8e..df43e78cee96 100644
--- a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
+++ b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
@@ -14,7 +14,10 @@ import {
type McpServerConnection,
} from "@posthog/shared";
import { buildPosthogScopedPropertyHeaderRecord } from "@posthog/shared/posthog-property-headers";
-import type { TaskContext } from "@posthog/shared/task-context";
+import type {
+ TaskContext,
+ TaskContextInput,
+} from "@posthog/shared/task-context";
import { prepareContextWiki } from "@posthog/workspace-server/services/agent/context-wiki";
import {
AGENT_AUTH,
@@ -70,11 +73,7 @@ export class DesktopPiRpcClientFactory implements PiRpcClientFactory {
// Four independent round-trips: proxy URL, auth proxy, MCP config, wiki mount.
const [baseUrl, enrichmentApiUrl, mcpConfiguration, contextWikiPath] =
await Promise.all([
- this.getProxyUrl(
- credentials.region,
- projectId,
- input.taskContext.taskId,
- ),
+ this.getProxyUrl(credentials.region, projectId, input.taskContext),
this.authProxy.start(access.apiHost),
this.mcpServerSource.getMcpRuntimeConfiguration(),
this.mountContextWiki(projectId),
@@ -166,7 +165,7 @@ export class DesktopPiRpcClientFactory implements PiRpcClientFactory {
private async getProxyUrl(
region: CloudRegion,
projectId: number,
- taskId: string,
+ { taskId, taskRunId }: Pick<TaskContextInput, "taskId" | "taskRunId">,
): Promise<string> {
const { proxyUrl } = await resolveGatewayProxy({
authProxy: this.authProxy,
@@ -174,7 +173,13 @@ export class DesktopPiRpcClientFactory implements PiRpcClientFactory {
legacyGatewayUrl: getLlmGatewayUrl(getCloudUrlFromRegion(region)),
projectId,
headers: buildPosthogScopedPropertyHeaderRecord(
- { task_id: taskId, $ai_session_id: taskId },
+ {
+ task_id: taskId,
+ task_run_id: taskRunId,
+ task_runtime_adapter: "pi",
+ task_execution_environment: "local",
+ $ai_session_id: taskId,
+ },
projectId,
),
});
diff --git a/products/desktop/packages/core/src/pi-runtime/piRunner.ts b/products/desktop/packages/core/src/pi-runtime/piRunner.ts
index d5861ed7fa4a..60a6509c6653 100644
--- a/products/desktop/packages/core/src/pi-runtime/piRunner.ts
+++ b/products/desktop/packages/core/src/pi-runtime/piRunner.ts
@@ -9,7 +9,7 @@ export interface PiRunInput {
}
export interface PiResumeInput {
- taskContext: Pick<TaskContextInput, "taskId" | "cwd">;
+ taskContext: Pick<TaskContextInput, "taskId" | "taskRunId" | "cwd">;
}
export interface PiRunner {
diff --git a/products/desktop/packages/core/src/pi-runtime/piSessionController.test.ts b/products/desktop/packages/core/src/pi-runtime/piSessionController.test.ts
index d0ac97d35c81..1ef95bbfd789 100644
--- a/products/desktop/packages/core/src/pi-runtime/piSessionController.test.ts
+++ b/products/desktop/packages/core/src/pi-runtime/piSessionController.test.ts
@@ -1071,6 +1071,51 @@ describe("PiSessionController", () => {
},
);
+ it.each([
+ ["a loaded session with a local run", true, "local", false],
+ ["a loaded session with a cloud run", true, "cloud", true],
+ ["a failed load of a local run", false, "local", false],
+ ["a failed load of a cloud run", false, "cloud", true],
+ ["a run that cannot be fetched", false, "rejects", false],
+ ] as const)(
+ "restart with %s resumes in the cloud only for a cloud run",
+ async (_label, sessionLoads, fetchedRun, resumesInCloud) => {
+ const session = createSession();
+ const provider: PiSessionProvider = {
+ get: sessionLoads
+ ? vi.fn(async () => session)
+ : vi.fn(async () => {
+ throw new Error("Pi failed to start");
+ }),
+ };
+ const taskService = {
+ openTask: vi.fn(async () => ({ success: true })),
+ getTask: vi.fn(async () => {
+ if (fetchedRun === "rejects") {
+ throw new Error("Not authenticated");
+ }
+ return {
+ id: "task-1",
+ latest_run: { id: "run-1", environment: fetchedRun },
+ };
+ }),
+ resumeCloudPiRun: vi.fn(async () => ({ id: "run-1" })),
+ } as unknown as TaskService;
+ const controller = new PiSessionController(
+ provider,
+ taskService,
+ createLogger().logger,
+ );
+
+ await controller.connect("task-1", "run-1").catch(() => {});
+ await controller.restart("task-1").catch(() => {});
+
+ expect(vi.mocked(taskService.resumeCloudPiRun).mock.calls.length).toBe(
+ resumesInCloud ? 1 : 0,
+ );
+ },
+ );
+
it("keeps a background cloud session until the run ends", async () => {
const session: PiSession = {
...createSession(),
diff --git a/products/desktop/packages/core/src/pi-runtime/piSessionController.ts b/products/desktop/packages/core/src/pi-runtime/piSessionController.ts
index da9103875d7c..db1e220f586c 100644
--- a/products/desktop/packages/core/src/pi-runtime/piSessionController.ts
+++ b/products/desktop/packages/core/src/pi-runtime/piSessionController.ts
@@ -438,7 +438,7 @@ export class PiSessionController {
return;
}
const taskRunId = this.taskRunIds.get(taskId);
- if (!taskRunId) {
+ if (!taskRunId || !(await this.isCloudRun(taskId, taskRunId))) {
await this.retry(taskId);
return;
}
@@ -1838,6 +1838,24 @@ export class PiSessionController {
this.subscriptions.delete(taskId);
}
+ /** Local Pi tasks also carry a run id, so a bound id alone does not mean a cloud run. */
+ private async isCloudRun(
+ taskId: string,
+ taskRunId: string,
+ ): Promise<boolean> {
+ const task = await this.taskService
+ .getTask(taskId, taskRunId)
+ .catch((error: unknown) => {
+ this.log.warn("Pi restart could not load the run; retrying locally", {
+ taskId,
+ taskRunId,
+ error,
+ });
+ return undefined;
+ });
+ return task?.latest_run?.environment === "cloud";
+ }
+
private getPiSession(taskId: string): Promise<PiSession> {
const existing = this.sessions.get(taskId);
if (existing) {
diff --git a/products/desktop/packages/core/src/task-detail/taskCreationSaga.test.ts b/products/desktop/packages/core/src/task-detail/taskCreationSaga.test.ts
index 0a74a64400a0..8d705a1c42d9 100644
--- a/products/desktop/packages/core/src/task-detail/taskCreationSaga.test.ts
+++ b/products/desktop/packages/core/src/task-detail/taskCreationSaga.test.ts
@@ -431,9 +431,14 @@ describe("TaskCreationSaga", () => {
});
const createdTask = createTask({ repository: undefined });
const createTaskRequest = vi.fn().mockResolvedValue(createdTask);
+ const localRun = createRun({ environment: "local" });
+ const createTaskRun = vi.fn().mockResolvedValue(localRun);
const onTaskReady = vi.fn();
piRunner.create.mockImplementationOnce(() => piSessionCreated);
- const saga = makeSaga({ createTask: createTaskRequest }, { onTaskReady });
+ const saga = makeSaga(
+ { createTask: createTaskRequest, createTaskRun },
+ { onTaskReady },
+ );
const run = saga.run({
content: "Draft a launch email",
@@ -454,15 +459,17 @@ describe("TaskCreationSaga", () => {
expect(result.success).toBe(true);
expect(onTaskReady).toHaveBeenCalledWith({
- task: createdTask,
+ task: { ...createdTask, latest_run: localRun },
workspace: null,
});
expect(createTaskRequest).toHaveBeenCalledWith(
expect.objectContaining({ runtime: "pi" }),
);
+ expect(createTaskRun).toHaveBeenCalledWith("task-123");
expect(piRunner.create).toHaveBeenCalledWith({
taskContext: {
taskId: "task-123",
+ taskRunId: "run-123",
cwd: "/tmp/scratch/task-123",
customInstructions: "Keep the patch small.",
additionalDirectories: ["/tmp/shared"],
@@ -476,6 +483,45 @@ describe("TaskCreationSaga", () => {
expect(sessionService.markTaskCreationInFlight).not.toHaveBeenCalled();
});
+ it.each([
+ ["the run call fails", vi.fn().mockRejectedValue(new Error("503"))],
+ ["the run has no id", vi.fn().mockResolvedValue(createRun({ id: "" }))],
+ ])(
+ "starts a new local Pi task untagged when %s",
+ async (_label, createTaskRun) => {
+ const createdTask = createTask({ repository: undefined });
+ const deleteTask = vi.fn();
+ const onTaskReady = vi.fn();
+ const saga = makeSaga(
+ {
+ createTask: vi.fn().mockResolvedValue(createdTask),
+ createTaskRun,
+ deleteTask,
+ },
+ { onTaskReady },
+ );
+
+ const result = await saga.run({
+ content: "Draft a launch email",
+ workspaceMode: "local",
+ runtime: "pi",
+ allowNoRepo: true,
+ });
+
+ expect(result.success).toBe(true);
+ expect(deleteTask).not.toHaveBeenCalled();
+ expect(piRunner.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ taskContext: expect.objectContaining({ taskRunId: undefined }),
+ }),
+ );
+ expect(onTaskReady).toHaveBeenCalledWith({
+ task: createdTask,
+ workspace: null,
+ });
+ },
+ );
+
it("starts a Pi session before surfacing a new worktree task", async () => {
let resolvePiSession: () => void = () => {};
const piSessionCreated = new Promise<void>((resolve) => {
@@ -486,7 +532,12 @@ describe("TaskCreationSaga", () => {
mockHost.addFolder.mockResolvedValue({ id: "folder-1", path: "/repo" });
piRunner.create.mockImplementationOnce(() => piSessionCreated);
const saga = makeSaga(
- { createTask: vi.fn().mockResolvedValue(createdTask) },
+ {
+ createTask: vi.fn().mockResolvedValue(createdTask),
+ createTaskRun: vi
+ .fn()
+ .mockResolvedValue(createRun({ environment: "local" })),
+ },
{ onTaskReady },
);
@@ -506,7 +557,10 @@ describe("TaskCreationSaga", () => {
expect(result).toMatchObject({ success: true });
expect(mockHost.createWorkspace).toHaveBeenCalledOnce();
expect(onTaskReady).toHaveBeenCalledWith({
- task: createdTask,
+ task: {
+ ...createdTask,
+ latest_run: expect.objectContaining({ id: "run-123" }),
+ },
workspace: expect.objectContaining({ taskId: createdTask.id }),
});
});
diff --git a/products/desktop/packages/core/src/task-detail/taskCreationSaga.ts b/products/desktop/packages/core/src/task-detail/taskCreationSaga.ts
index aad1fefcec1c..2750ddbe9baf 100644
--- a/products/desktop/packages/core/src/task-detail/taskCreationSaga.ts
+++ b/products/desktop/packages/core/src/task-detail/taskCreationSaga.ts
@@ -585,9 +585,26 @@ export class TaskCreationSaga extends Saga<
.filter((part): part is string => !!part)
.join("\n\n");
+ // The run only attributes spend; a failure starts Pi untagged and keeps the task.
+ const taskRun = await this.deps.posthogClient
+ .createTaskRun(task.id)
+ .catch((error: unknown) => {
+ this.log.warn("Pi task run creation failed", {
+ taskId: task.id,
+ error,
+ });
+ return undefined;
+ });
+ const taskRunId = taskRun?.id || undefined;
+ if (taskRun?.id) {
+ // The Pi view keys its session on `latest_run`; a run that appears only after a refetch resets the live session.
+ task = { ...task, latest_run: taskRun };
+ }
+
await this.deps.piRunner.create({
taskContext: {
taskId: task.id,
+ taskRunId,
cwd: agentCwd ?? "",
customInstructions: input.customInstructions,
additionalDirectories: input.additionalDirectories,
diff --git a/products/desktop/packages/core/src/task-detail/taskService.test.ts b/products/desktop/packages/core/src/task-detail/taskService.test.ts
index 1c3230baecc2..af22bb5d03a0 100644
--- a/products/desktop/packages/core/src/task-detail/taskService.test.ts
+++ b/products/desktop/packages/core/src/task-detail/taskService.test.ts
@@ -66,16 +66,19 @@ function makeService(overrides: Partial<SessionService> = {}): TaskService {
}
describe("TaskService.openTask", () => {
- it("passes the main repository path when resuming Pi in a worktree", async () => {
+ it.each([
+ [
+ "a worktree",
+ { folderPath: "/repo", worktreePath: "/worktrees/task-1" },
+ "/worktrees/task-1",
+ ],
+ ["a scratch dir", null, "/scratch/task-1"],
+ ])("resumes Pi in %s with the task's run", async (_label, workspace, cwd) => {
const task = {
id: "task-1",
runtime: "pi",
latest_run: { id: "run-1", environment: "local", status: "running" },
};
- const workspace = {
- folderPath: "/repo",
- worktreePath: "/worktrees/task-1",
- };
const piRunner = {
create: vi.fn(),
resume: vi.fn(async () => {}),
@@ -87,6 +90,7 @@ describe("TaskService.openTask", () => {
getTask: vi.fn(async () => task),
})),
getWorkspace: vi.fn(async () => workspace),
+ ensureScratchDir: vi.fn(async () => "/scratch/task-1"),
} as unknown as ITaskCreationHost,
{} as SessionService,
{} as TaskCreationEffects,
@@ -99,7 +103,7 @@ describe("TaskService.openTask", () => {
success: true,
});
expect(piRunner.resume).toHaveBeenCalledWith({
- taskContext: { taskId: "task-1", cwd: "/worktrees/task-1" },
+ taskContext: { taskId: "task-1", taskRunId: "run-1", cwd },
});
});
diff --git a/products/desktop/packages/core/src/task-detail/taskService.ts b/products/desktop/packages/core/src/task-detail/taskService.ts
index c7add9a2299f..bca074f68350 100644
--- a/products/desktop/packages/core/src/task-detail/taskService.ts
+++ b/products/desktop/packages/core/src/task-detail/taskService.ts
@@ -288,6 +288,7 @@ export class TaskService {
await this.piRunner.resume({
taskContext: {
taskId,
+ taskRunId: task.latest_run?.id,
cwd:
existingWorkspace.worktreePath ?? existingWorkspace.folderPath,
},
@@ -311,7 +312,9 @@ export class TaskService {
if (runtime === "pi") {
try {
const cwd = await this.host.ensureScratchDir(taskId);
- await this.piRunner.resume({ taskContext: { taskId, cwd } });
+ await this.piRunner.resume({
+ taskContext: { taskId, taskRunId: task.latest_run?.id, cwd },
+ });
return {
success: true,
data: { task, workspace: null },
diff --git a/products/desktop/packages/workspace-server/src/services/pi-session/schemas.test.ts b/products/desktop/packages/workspace-server/src/services/pi-session/schemas.test.ts
new file mode 100644
index 000000000000..ca6108856a87
--- /dev/null
+++ b/products/desktop/packages/workspace-server/src/services/pi-session/schemas.test.ts
@@ -0,0 +1,22 @@
+import { describe, expect, it } from "vitest";
+import { resumePiSessionInput, startPiSessionInput } from "./schemas";
+
+describe("pi-session schemas", () => {
+ it.each([
+ [
+ "start",
+ startPiSessionInput,
+ {
+ taskContext: { taskId: "task-1", taskRunId: "run-1", cwd: "/w" },
+ prompt: "hi",
+ },
+ ],
+ [
+ "resume",
+ resumePiSessionInput,
+ { taskContext: { taskId: "task-1", taskRunId: "run-1", cwd: "/w" } },
+ ],
+ ])("keeps the task run id through the %s input", (_label, schema, input) => {
+ expect(schema.parse(input).taskContext.taskRunId).toBe("run-1");
+ });
+});
diff --git a/products/desktop/packages/workspace-server/src/services/pi-session/schemas.ts b/products/desktop/packages/workspace-server/src/services/pi-session/schemas.ts
index 91b5c89e34a1..5abf6ac8e5ca 100644
--- a/products/desktop/packages/workspace-server/src/services/pi-session/schemas.ts
+++ b/products/desktop/packages/workspace-server/src/services/pi-session/schemas.ts
@@ -13,6 +13,7 @@ export { piExtensionEventSchema, piRpcResponseSchema };
const piTaskContextInput = z.object({
taskId: z.string(),
+ taskRunId: z.string().optional(),
cwd: z.string(),
customInstructions: z.string().optional(),
additionalDirectories: z.array(z.string()).optional(),
@@ -40,7 +41,11 @@ export const piSessionHealthOutput = z.object({
});
export const resumePiSessionInput = z.object({
- taskContext: piTaskContextInput.pick({ taskId: true, cwd: true }),
+ taskContext: piTaskContextInput.pick({
+ taskId: true,
+ taskRunId: true,
+ cwd: true,
+ }),
});
export type ResumePiSessionInput = z.infer<typeof resumePiSessionInput>;