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): include stable task ids in creation events (#102144)

Peter Kirkhamb29ce0968b1a
diff --git a/products/desktop/docs/CONVENTIONS.md b/products/desktop/docs/CONVENTIONS.md
index c17663a06840..51215af099fb 100644
--- a/products/desktop/docs/CONVENTIONS.md
+++ b/products/desktop/docs/CONVENTIONS.md
@@ -225,6 +225,14 @@ Main-process events use `trackAppEvent(eventName, properties)` from `apps/code/s
Both clients set `team: "posthog-code"` as a super-property.
+### Task creation identity
+
+Every client `Task created` event includes `task_id` from the task creation result, including local, worktree, cloud, and setup tasks.
+Use this stable ID to count distinct tasks, not event IDs or run IDs.
+Keep the existing capture points: a retained task can still emit an event after workspace provisioning fails, while a failed creation does not emit a success event.
+Retries and resumed tasks must use the returned task ID, not generate a new analytics ID.
+Older clients do not send this property, so check coverage by client version and workspace mode before using it for distinct-task metrics.
+
### Network metrics
The network duration metric uses backend URLs from the shared region configuration, including a configured custom cloud.
diff --git a/products/desktop/packages/shared/src/analytics-events.ts b/products/desktop/packages/shared/src/analytics-events.ts
index d52ed8ad4c50..b3c7fbe3e68f 100644
--- a/products/desktop/packages/shared/src/analytics-events.ts
+++ b/products/desktop/packages/shared/src/analytics-events.ts
@@ -90,6 +90,7 @@ export interface TaskListViewProperties {
}
export interface TaskCreateProperties {
+ task_id: string;
auto_run: boolean;
created_from: TaskCreatedFrom;
repository_provider?: RepositoryProvider;
diff --git a/products/desktop/packages/ui/src/features/inbox/components/ConfigureAgentsSection.tsx b/products/desktop/packages/ui/src/features/inbox/components/ConfigureAgentsSection.tsx
index 43f218b5df49..0c87317cd18c 100644
--- a/products/desktop/packages/ui/src/features/inbox/components/ConfigureAgentsSection.tsx
+++ b/products/desktop/packages/ui/src/features/inbox/components/ConfigureAgentsSection.tsx
@@ -350,6 +350,7 @@ function SetupTaskSection() {
});
if (result.success) {
track(ANALYTICS_EVENTS.TASK_CREATED, {
+ task_id: result.data.task.id,
auto_run: true,
created_from: "command-menu",
repository_provider: "github",
diff --git a/products/desktop/packages/ui/src/features/inbox/hooks/useDiscussReport.test.tsx b/products/desktop/packages/ui/src/features/inbox/hooks/useDiscussReport.test.tsx
index 213193cbf990..2840708e4cef 100644
--- a/products/desktop/packages/ui/src/features/inbox/hooks/useDiscussReport.test.tsx
+++ b/products/desktop/packages/ui/src/features/inbox/hooks/useDiscussReport.test.tsx
@@ -7,7 +7,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const createTask = vi.hoisted(() =>
vi.fn().mockResolvedValue({
success: true,
- task: { id: "task-1" },
+ data: { task: { id: "task-1" }, workspace: null },
}),
);
const getUserIntegrationIdForRepo = vi.hoisted(() => vi.fn(() => "ghu_1"));
@@ -90,7 +90,10 @@ const report = {
describe("useDiscussReport", () => {
beforeEach(() => {
vi.clearAllMocks();
- createTask.mockResolvedValue({ success: true, task: { id: "task-1" } });
+ createTask.mockResolvedValue({
+ success: true,
+ data: { task: { id: "task-1" }, workspace: null },
+ });
getSignalReportSignals.mockResolvedValue({
report: null,
signals: [
diff --git a/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.test.tsx b/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.test.tsx
index af96d36e18dc..5968b94050dc 100644
--- a/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.test.tsx
+++ b/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.test.tsx
@@ -11,6 +11,7 @@ const mocks = vi.hoisted(() => ({
openTask: vi.fn(),
success: vi.fn(),
error: vi.fn(),
+ track: vi.fn(),
}));
vi.mock("@posthog/di/react", () => ({
useService: () => ({ createTask: mocks.createTask }),
@@ -51,7 +52,7 @@ vi.mock("@posthog/ui/primitives/toast", () => ({
},
}));
vi.mock("@posthog/ui/router/useOpenTask", () => ({ openTask: mocks.openTask }));
-vi.mock("@posthog/ui/shell/analytics", () => ({ track: vi.fn() }));
+vi.mock("@posthog/ui/shell/analytics", () => ({ track: mocks.track }));
vi.mock("@posthog/ui/shell/logger", () => ({
logger: { scope: () => ({ error: mocks.error }) },
}));
@@ -135,6 +136,13 @@ describe("useInboxCloudTaskRunner", () => {
expect(onTaskStarted).toHaveBeenCalledTimes(success ? 1 : 0);
expect(result.current.isRunning).toBe(false);
expect(mocks.openTask).not.toHaveBeenCalled();
+ expect(
+ mocks.track.mock.calls.filter(([event]) => event === "Task created"),
+ ).toEqual(
+ success
+ ? [["Task created", expect.objectContaining({ task_id: task.id })]]
+ : [],
+ );
if (success) {
const options = mocks.success.mock.calls[0][1];
expect(options.action.label).toBe("View task");
@@ -180,10 +188,18 @@ describe("useInboxCloudTaskRunner", () => {
expect(result.current.isRunning).toBe(false);
expect(onTaskStarted).not.toHaveBeenCalled();
expect(mocks.createTask).not.toHaveBeenCalled();
+ expect(
+ mocks.track.mock.calls.filter(([event]) => event === "Task created"),
+ ).toEqual([]);
await act(async () => {
expect(await result.current.run()).toBe(true);
});
expect(mocks.createTask).toHaveBeenCalledOnce();
expect(onTaskStarted).toHaveBeenCalledOnce();
+ expect(
+ mocks.track.mock.calls.filter(([event]) => event === "Task created"),
+ ).toEqual([
+ ["Task created", expect.objectContaining({ task_id: task.id })],
+ ]);
});
});
diff --git a/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.ts b/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.ts
index d247bb4afdbb..947bfc8a5860 100644
--- a/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.ts
+++ b/products/desktop/packages/ui/src/features/inbox/hooks/useInboxCloudTaskRunner.ts
@@ -322,6 +322,7 @@ export function useInboxCloudTaskRunner({
: { cloud_run_source: "manual" }),
adapter,
...analyticsExtras,
+ task_id: result.data.task.id,
space_context_mode: "none",
});
} else {
diff --git a/products/desktop/packages/ui/src/features/scouts/hooks/useScoutChatTask.test.tsx b/products/desktop/packages/ui/src/features/scouts/hooks/useScoutChatTask.test.tsx
index 68ec23fa118c..4ae1612bd012 100644
--- a/products/desktop/packages/ui/src/features/scouts/hooks/useScoutChatTask.test.tsx
+++ b/products/desktop/packages/ui/src/features/scouts/hooks/useScoutChatTask.test.tsx
@@ -4,7 +4,10 @@ import type { ReactNode } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
const createTask = vi.hoisted(() =>
- vi.fn().mockResolvedValue({ success: true, task: { id: "task-1" } }),
+ vi.fn().mockResolvedValue({
+ success: true,
+ data: { task: { id: "task-1" }, workspace: null },
+ }),
);
const getUserIntegrationIdForRepo = vi.hoisted(() => vi.fn(() => "ghu_1"));
const resolveDefaultCloudRepository = vi.hoisted(() => vi.fn());
@@ -133,7 +136,15 @@ describe("scout task result", () => {
it.each([true, false])(
"returns task creation success: %s",
async (success) => {
- createTask.mockResolvedValueOnce({ success, task: { id: "task-1" } });
+ createTask.mockResolvedValueOnce(
+ success
+ ? { success: true, data: { task: { id: "task-1" }, workspace: null } }
+ : {
+ success: false,
+ error: "Task creation failed",
+ failedStep: "create",
+ },
+ );
const { result } = renderHook(
() =>
useScoutChatTask({
diff --git a/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.test.tsx b/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.test.tsx
index 6d5652bc4c86..3b5a4ba8f1e8 100644
--- a/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.test.tsx
+++ b/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.test.tsx
@@ -105,6 +105,7 @@ describe("useStartTaskFromWorktree", () => {
expect(trackMock).toHaveBeenCalledWith(
"Task created",
expect.objectContaining({
+ task_id: task.id,
created_from: "sidebar-worktree",
workspace_mode: "worktree",
}),
@@ -112,6 +113,7 @@ describe("useStartTaskFromWorktree", () => {
expect(queryFilterMock).toHaveBeenCalledWith({
mainRepoPath: MAIN_REPO_PATH,
});
+ expect(trackMock).toHaveBeenCalledTimes(1);
expect(toastErrorMock).not.toHaveBeenCalled();
expect(setFailedMock).not.toHaveBeenCalled();
await waitFor(() =>
@@ -179,7 +181,10 @@ describe("useStartTaskFromWorktree", () => {
);
// Provisioning failure is still a successful task creation: the create
// event still fires and the adoptable-worktrees list still refetches.
- expect(trackMock).toHaveBeenCalled();
+ expect(trackMock).toHaveBeenCalledExactlyOnceWith(
+ "Task created",
+ expect.objectContaining({ task_id: task.id }),
+ );
expect(queryFilterMock).toHaveBeenCalledWith({
mainRepoPath: MAIN_REPO_PATH,
});
diff --git a/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.ts b/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.ts
index 33d109099a06..67ca610d2636 100644
--- a/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.ts
+++ b/products/desktop/packages/ui/src/features/sidebar/useStartTaskFromWorktree.ts
@@ -58,6 +58,7 @@ export function useStartTaskFromWorktree(mainRepoPath: string) {
);
}
track(ANALYTICS_EVENTS.TASK_CREATED, {
+ task_id: result.data.task.id,
auto_run: false,
created_from: "sidebar-worktree",
workspace_mode: "worktree",
diff --git a/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.test.tsx b/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.test.tsx
index 53ff7b08e87c..c8af7772bc88 100644
--- a/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.test.tsx
+++ b/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.test.tsx
@@ -162,7 +162,7 @@ function wrapper({ children }: { children: ReactNode }) {
function renderTaskCreation(
content: EditorContent,
- workspaceMode: "local" | "cloud" = "local",
+ workspaceMode: "local" | "worktree" | "cloud" = "local",
runtime: "acp" | "pi" = "acp",
) {
return renderHook(
@@ -241,6 +241,9 @@ describe("useTaskCreation prompt records", () => {
});
expect(createTaskMock).toHaveBeenCalledTimes(createTaskCalls);
+ expect(
+ trackMock.mock.calls.filter(([event]) => event === "Task created"),
+ ).toEqual([]);
expect(
useTaskInputHistoryStore.getState().entries.map((e) => e.text),
).toEqual(["Check the build"]);
@@ -296,7 +299,7 @@ describe("useTaskCreation prompt records", () => {
});
});
- it.each(["local", "cloud"] as const)(
+ it.each(["local", "worktree", "cloud"] as const)(
"keeps the submitted prompt visible after successful %s creation until the chat takes over",
async (workspaceMode) => {
const brief = textToContent("Check the build");
@@ -311,6 +314,17 @@ describe("useTaskCreation prompt records", () => {
expect(await result.current.handleSubmit()).toBe(true);
});
+ expect(
+ trackMock.mock.calls.filter(([event]) => event === "Task created"),
+ ).toEqual([
+ [
+ "Task created",
+ expect.objectContaining({
+ task_id: "task-1",
+ workspace_mode: workspaceMode,
+ }),
+ ],
+ ]);
expect(pendingTaskPromptStoreApi.get("task-1")?.contentXml).toBe(
contentToXml(brief).trim(),
);
diff --git a/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.ts b/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.ts
index 22d64a48d6c0..cf837a964de4 100644
--- a/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.ts
+++ b/products/desktop/packages/ui/src/features/task-detail/hooks/useTaskCreation.ts
@@ -139,6 +139,7 @@ interface UseTaskCreationReturn {
async function trackTaskCreated(
input: TaskCreationInput,
+ taskId: string,
selectedDirectory: string,
hostClient: HostTrpcClient,
codexModelAccess?: ModelAccess,
@@ -164,6 +165,7 @@ async function trackTaskCreated(
}
track(ANALYTICS_EVENTS.TASK_CREATED, {
+ task_id: taskId,
auto_run: !!input.executionMode,
created_from: "command-menu",
repository_provider: input.repository ? "github" : "none",
@@ -620,6 +622,7 @@ export function useTaskCreation({
}
void trackTaskCreated(
input,
+ result.data.task.id,
selectedDirectory,
hostClient,
input.codexModelAccess,