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): keep the new-task prefill through the tab-stamp remount (#92101)

Jack Tomei1712ad080f55
diff --git a/products/desktop/docs/ARCHITECTURE.md b/products/desktop/docs/ARCHITECTURE.md
index 6681393b9696..5c58515d5ab4 100644
--- a/products/desktop/docs/ARCHITECTURE.md
+++ b/products/desktop/docs/ARCHITECTURE.md
@@ -36,6 +36,11 @@ Two tRPC surfaces exist:
## New task startup
+New-task screens wait for the route's browser tab ID before mounting the
+composer. This keeps prefilled prompts in the tab's draft instead of a temporary
+session. Consuming a prompt clears its content, but keeps the request ID so model
+and mode settings can apply after their options load.
+
Submitting a task keeps the composer busy until creation opens the task's chat.
The chat displays the submitted prompt with the current startup status until
initialization ends and the transcript or error state takes over. The waiting
diff --git a/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.test.tsx
index 95a2b8c98b26..72ceeba9f015 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.test.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.test.tsx
@@ -18,12 +18,14 @@ const {
useContextLayerFlag,
useChannelWikiContext,
taskInputProps,
+ routeState,
} = vi.hoisted(() => ({
track: vi.fn(),
useFolderInstructions: vi.fn(),
useContextLayerFlag: vi.fn(),
useChannelWikiContext: vi.fn(),
taskInputProps: vi.fn(),
+ routeState: { tabId: "tab-1" as string | undefined },
}));
// What the hook returns when the space has no wiki page, so the legacy
@@ -114,15 +116,15 @@ vi.mock("@tanstack/react-router", () => ({
select,
}: {
select: (s: {
- matches: { routeId: string; params: Record<string, string> }[];
- location: { state: { tabId: string } };
+ matches: { fullPath: string; params: Record<string, string> }[];
+ location: { state: { tabId: string | undefined } };
}) => unknown;
}) =>
select({
matches: [
- { routeId: "/spaces/$channelId/new", params: { channelId: "chan-1" } },
+ { fullPath: "/spaces/$channelId/new", params: { channelId: "chan-1" } },
],
- location: { state: { tabId: "tab-1" } },
+ location: { state: routeState },
}),
}));
@@ -144,9 +146,33 @@ describe("SpaceNewTask context panel", () => {
useContextLayerFlag.mockReturnValue(false);
useChannelWikiContext.mockReturnValue(NO_WIKI_PAGE);
taskInputProps.mockReset();
+ routeState.tabId = "tab-1";
useTaskInputPrefillStore.setState({ prefill: {} });
});
+ it("waits for the tab before passing the prompt to the composer", () => {
+ useFolderInstructions.mockReturnValue({ data: undefined });
+ routeState.tabId = undefined;
+ useTaskInputPrefillStore.setState({
+ prefill: { initialPrompt: "Check the build", requestId: "req-1" },
+ });
+
+ const { rerender } = render(<SpaceNewTask channelId="chan-1" />);
+
+ expect(taskInputProps).not.toHaveBeenCalled();
+
+ routeState.tabId = "tab-1";
+ rerender(<SpaceNewTask channelId="chan-1" />);
+
+ expect(taskInputProps).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ sessionId: "task-input:tab-1",
+ initialPrompt: "Check the build",
+ initialPromptKey: "req-1",
+ }),
+ );
+ });
+
it("blocks submission while the enabled wiki page is unresolved", () => {
useContextLayerFlag.mockReturnValue(true);
useChannelWikiContext.mockReturnValue({
diff --git a/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.tsx b/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.tsx
index a2c2428ff545..7c47c2aa34e1 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/SpaceNewTask.tsx
@@ -17,7 +17,6 @@ import { ResizableSidebar } from "@posthog/ui/primitives/ResizableSidebar";
import { toast } from "@posthog/ui/primitives/toast";
import { useAppView } from "@posthog/ui/router/useAppView";
import { track } from "@posthog/ui/shell/analytics";
-import { Flex } from "@radix-ui/themes";
import { useNavigate, useRouterState } from "@tanstack/react-router";
import { useCallback, useMemo, useState } from "react";
@@ -127,8 +126,10 @@ export function SpaceNewTask({ channelId }: { channelId: string }) {
[navigate],
);
+ if (!tabId) return null;
+
return (
- <Flex className="h-full min-w-0 flex-1">
+ <div className="flex h-full min-w-0 flex-1">
<div className="min-w-0 flex-1">
<TaskInput
key={taskInputSessionId}
@@ -198,6 +199,6 @@ export function SpaceNewTask({ channelId }: { channelId: string }) {
/>
) : null}
</ResizableSidebar>
- </Flex>
+ </div>
);
}
diff --git a/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.test.tsx b/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.test.tsx
new file mode 100644
index 000000000000..8dea34544a46
--- /dev/null
+++ b/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.test.tsx
@@ -0,0 +1,77 @@
+import { render, screen } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const { routeState } = vi.hoisted(() => ({
+ routeState: { tabId: undefined as string | undefined },
+}));
+
+vi.mock("@tanstack/react-router", () => ({
+ useRouterState: ({ select }: { select: (state: unknown) => unknown }) =>
+ select({ location: { state: routeState } }),
+}));
+vi.mock("@posthog/ui/features/canvas/hooks/useChannelsWorld", () => ({
+ useChannelsWorld: () => false,
+}));
+vi.mock("@posthog/ui/router/useAppView", () => ({
+ useAppView: () => {
+ const prefill = useTaskInputPrefillStore((state) => state.prefill);
+ return { ...prefill, taskInputRequestId: prefill.requestId };
+ },
+}));
+vi.mock("./TaskInput", () => ({
+ TaskInput: ({
+ sessionId,
+ initialPrompt,
+ initialPromptKey,
+ }: {
+ sessionId: string;
+ initialPrompt?: string;
+ initialPromptKey?: string;
+ }) => {
+ const [prompt, setPrompt] = useState("");
+ useEffect(() => {
+ if (!initialPrompt || !initialPromptKey) return;
+ setPrompt(initialPrompt);
+ useTaskInputPrefillStore.getState().consumePrompt(initialPromptKey);
+ }, [initialPrompt, initialPromptKey]);
+ return (
+ <input
+ aria-label="Prompt"
+ data-session-id={sessionId}
+ value={prompt}
+ onChange={(event) => setPrompt(event.target.value)}
+ />
+ );
+ },
+}));
+
+import { useTaskInputPrefillStore } from "@posthog/ui/features/task-detail/stores/taskInputPrefillStore";
+import { useEffect, useState } from "react";
+import { NewTaskScreen } from "./NewTaskScreen";
+
+describe("NewTaskScreen", () => {
+ beforeEach(() => {
+ routeState.tabId = undefined;
+ useTaskInputPrefillStore.setState({
+ prefill: { requestId: "req-1", initialPrompt: "Check the build" },
+ });
+ });
+
+ it("keeps the prompt when navigation assigns its tab", () => {
+ const { rerender } = render(<NewTaskScreen />);
+
+ routeState.tabId = "tab-1";
+ rerender(<NewTaskScreen />);
+
+ expect(screen.getByRole("textbox", { name: "Prompt" })).toHaveValue(
+ "Check the build",
+ );
+ expect(screen.getByRole("textbox", { name: "Prompt" })).toHaveAttribute(
+ "data-session-id",
+ "task-input:tab-1",
+ );
+ expect(
+ useTaskInputPrefillStore.getState().prefill.initialPrompt,
+ ).toBeUndefined();
+ });
+});
diff --git a/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.tsx b/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.tsx
index 2ccdb60221c1..7ff8a2b8dfaf 100644
--- a/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.tsx
+++ b/products/desktop/packages/ui/src/features/task-detail/components/NewTaskScreen.tsx
@@ -18,6 +18,8 @@ export function NewTaskScreen() {
});
const sessionId = getTaskInputSessionId(tabId);
+ if (!tabId) return null;
+
return (
<TaskInput
key={sessionId}
diff --git a/products/desktop/packages/ui/src/features/task-detail/stores/taskInputPrefillStore.ts b/products/desktop/packages/ui/src/features/task-detail/stores/taskInputPrefillStore.ts
index fe1a0f76fb5d..67b5db6f53fa 100644
--- a/products/desktop/packages/ui/src/features/task-detail/stores/taskInputPrefillStore.ts
+++ b/products/desktop/packages/ui/src/features/task-detail/stores/taskInputPrefillStore.ts
@@ -68,7 +68,6 @@ export const useTaskInputPrefillStore = create<PrefillStoreState>((set) => ({
initialPrompt: undefined,
initialContent: undefined,
recoveredFromKey: undefined,
- requestId: undefined,
},
}
: s,
diff --git a/products/desktop/packages/ui/src/router/navigationBridge.test.ts b/products/desktop/packages/ui/src/router/navigationBridge.test.ts
new file mode 100644
index 000000000000..1763bb9a220d
--- /dev/null
+++ b/products/desktop/packages/ui/src/router/navigationBridge.test.ts
@@ -0,0 +1,81 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ getRouterOrNull: vi.fn(),
+}));
+
+vi.mock("./routerRef", () => ({
+ getRouterOrNull: mocks.getRouterOrNull,
+}));
+
+import {
+ navigateToChannelNewTask,
+ navigateToNewTask,
+} from "./navigationBridge";
+
+type StateUpdater = (prev: Record<string, unknown>) => Record<string, unknown>;
+
+// The composer screens key their draft session on `state.tabId`
+// (getTaskInputSessionId) and remount on that key. A new-task entry born
+// without the tag gets stamped by the tab strip a beat later, which flips the
+// key and remounts the composer AFTER it consumed the one-shot prefill — the
+// prompt handed to openTaskInput (a posthog-code://new?prompt= deep link, a
+// show_actions compose button) silently vanished. These pin the entry to be
+// born already tagged with the tab it stays in.
+describe("new-task navigation carries the tab tag", () => {
+ const navigate = vi.fn();
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.getRouterOrNull.mockReturnValue({ navigate });
+ });
+
+ it("navigateToNewTask preserves the current entry's tabId", () => {
+ navigateToNewTask();
+
+ expect(navigate).toHaveBeenCalledTimes(1);
+ const { to, state } = navigate.mock.calls[0][0];
+ expect(to).toBe("/new");
+ expect((state as StateUpdater)({ tabId: "tab-1" })).toEqual({
+ tabId: "tab-1",
+ });
+ });
+
+ it("navigateToChannelNewTask preserves the current entry's tabId", () => {
+ navigateToChannelNewTask("chan-1");
+
+ expect(navigate).toHaveBeenCalledTimes(1);
+ const call = navigate.mock.calls[0][0];
+ expect(call.to).toBe("/spaces/$channelId/new");
+ expect(call.params).toEqual({ channelId: "chan-1" });
+ expect((call.state as StateUpdater)({ tabId: "tab-9" })).toEqual({
+ tabId: "tab-9",
+ });
+ });
+
+ // Route-scoped state (loopListOrigin, inboxBackOrigin) describes the entry
+ // being left; only the tab tag may survive onto the new-task entry.
+ it("carries only the tab tag, not other state keys", () => {
+ navigateToNewTask();
+
+ const { state } = navigate.mock.calls[0][0];
+ expect(
+ (state as StateUpdater)({ tabId: "tab-1", loopListOrigin: "loops" }),
+ ).toEqual({ tabId: "tab-1" });
+ });
+
+ it("leaves an untagged entry untagged", () => {
+ navigateToNewTask();
+
+ const { state } = navigate.mock.calls[0][0];
+ expect((state as StateUpdater)({})).toEqual({});
+ });
+
+ it("degrades to a no-op when the router is not mounted", () => {
+ mocks.getRouterOrNull.mockReturnValue(null);
+
+ expect(() => navigateToNewTask()).not.toThrow();
+ expect(() => navigateToChannelNewTask("chan-1")).not.toThrow();
+ expect(navigate).not.toHaveBeenCalled();
+ });
+});
diff --git a/products/desktop/packages/ui/src/router/navigationBridge.ts b/products/desktop/packages/ui/src/router/navigationBridge.ts
index 7f43a27dce1d..b874884ef000 100644
--- a/products/desktop/packages/ui/src/router/navigationBridge.ts
+++ b/products/desktop/packages/ui/src/router/navigationBridge.ts
@@ -13,8 +13,22 @@ import { getRouterOrNull } from "./routerRef";
// (early boot, unit tests). These are renderer conveniences — they must never
// throw just because the router singleton hasn't been created.
+// A plain navigation never changes which tab you are in; the tab strip
+// re-stamps the new history entry with the active tab after the fact (see
+// decideTabNavigation). The new-task screens key their composer session on
+// `state.tabId` (getTaskInputSessionId) and remount on that key, so an entry
+// born unstamped flips the key mid-mount and the remounted composer finds the
+// one-shot prefill already consumed — silently dropping prompts handed to
+// openTaskInput (posthog-code://new?prompt= deep links, show_actions compose
+// buttons). Carrying the tag forward matches what the strip would stamp, so
+// the session key never changes under the composer. Only the tag: the other
+// state keys (loopListOrigin, inboxBackOrigin) describe the route being left.
+const keepTabTag = (prev: { tabId?: string }): { tabId?: string } => ({
+ tabId: prev.tabId,
+});
+
export function navigateToNewTask(): void {
- void getRouterOrNull()?.navigate({ to: "/new" });
+ void getRouterOrNull()?.navigate({ to: "/new", state: keepTabTag });
}
export function navigateToTaskDetail(taskId: string): void {
@@ -75,6 +89,7 @@ export function navigateToChannelNewTask(channelId: string): void {
void getRouterOrNull()?.navigate({
to: "/spaces/$channelId/new",
params: { channelId },
+ state: keepTabTag,
});
}
diff --git a/products/desktop/packages/ui/src/router/useOpenTask.test.ts b/products/desktop/packages/ui/src/router/useOpenTask.test.ts
index 89d3a9914ce4..a613c09a81ec 100644
--- a/products/desktop/packages/ui/src/router/useOpenTask.test.ts
+++ b/products/desktop/packages/ui/src/router/useOpenTask.test.ts
@@ -107,7 +107,7 @@ describe("taskInputPrefillStore.consumePrompt", () => {
useTaskInputPrefillStore.setState({ prefill: {} });
});
- it("retires the prompt it was given", () => {
+ it("retires the prompt but keeps the key for delayed config", () => {
useTaskInputPrefillStore.setState({
prefill: { requestId: "r1", initialPrompt: "hello", folderId: "f1" },
});
@@ -116,7 +116,7 @@ describe("taskInputPrefillStore.consumePrompt", () => {
const { prefill } = useTaskInputPrefillStore.getState();
expect(prefill.initialPrompt).toBeUndefined();
- expect(prefill.requestId).toBeUndefined();
+ expect(prefill.requestId).toBe("r1");
// Folder scoping is not a one-shot prompt; it must survive.
expect(prefill.folderId).toBe("f1");
});