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): show starting state on session dot (#87719)

Adam Leith13b00872f1e6
diff --git a/products/desktop/packages/core/src/sessions/sessionService.ts b/products/desktop/packages/core/src/sessions/sessionService.ts
index 57fb182b4e30..925e5c7fda5d 100644
--- a/products/desktop/packages/core/src/sessions/sessionService.ts
+++ b/products/desktop/packages/core/src/sessions/sessionService.ts
@@ -369,6 +369,8 @@ export interface SessionTrpc {
export interface ISessionStore {
setSession(session: AgentSession): void;
removeSession(taskRunId: string): void;
+ setTaskStarting?(taskId: string): void;
+ clearTaskStarting?(taskId: string): void;
updateSession(taskRunId: string, updates: Partial<AgentSession>): void;
appendEvents(
taskRunId: string,
@@ -1914,9 +1916,13 @@ export class SessionService {
// Check for existing connected session
const existingSession = this.d.store.getSessionByTaskId(taskId);
if (existingSession?.status === "connected") {
+ this.d.store.clearTaskStarting?.(taskId);
this.d.log.info("Already connected to task", { taskId });
return;
}
+ if (task.latest_run?.environment !== "cloud") {
+ this.d.store.setTaskStarting?.(taskId);
+ }
if (existingSession?.status === "connecting") {
this.d.log.info("Session already in connecting state", { taskId });
return;
@@ -5239,6 +5245,7 @@ export class SessionService {
runtimeOptions = getCloudRuntimeOptions(session, previousRun);
try {
+ this.markTaskCreationInFlight(session.taskId);
// Backend derives the snapshot from resumeFromRunId and restores the sandbox.
updatedTask = await authCredentials.client.runTaskInCloud(
session.taskId,
@@ -5278,11 +5285,13 @@ export class SessionService {
throw error;
}
} catch (error) {
+ this.clearTaskCreationInFlight(session.taskId);
rollbackOptimisticPrompt();
throw error;
}
const newRun = updatedTask.latest_run;
if (!newRun?.id) {
+ this.clearTaskCreationInFlight(session.taskId);
rollbackOptimisticPrompt();
throw new Error("Failed to create resume run");
}
@@ -8226,6 +8235,12 @@ export class SessionService {
public markTaskCreationInFlight(taskId: string): void {
this.taskCreationMarks.set(taskId, Date.now());
+ this.d.store.setTaskStarting?.(taskId);
+ }
+
+ private clearTaskCreationInFlight(taskId: string): void {
+ this.taskCreationMarks.delete(taskId);
+ this.d.store.clearTaskStarting?.(taskId);
}
private isTaskCreationInFlight(taskId: string): boolean {
@@ -8234,7 +8249,7 @@ export class SessionService {
const expired =
Date.now() - markedAt > SessionService.TASK_CREATION_IN_FLIGHT_TTL_MS;
if (expired) {
- this.taskCreationMarks.delete(taskId);
+ this.clearTaskCreationInFlight(taskId);
return false;
}
return true;
diff --git a/products/desktop/packages/core/src/sessions/sessionServiceRecovery.test.ts b/products/desktop/packages/core/src/sessions/sessionServiceRecovery.test.ts
index cb48522111b7..9c0d86226b2b 100644
--- a/products/desktop/packages/core/src/sessions/sessionServiceRecovery.test.ts
+++ b/products/desktop/packages/core/src/sessions/sessionServiceRecovery.test.ts
@@ -45,6 +45,8 @@ function createHarness({ spyConnect = true } = {}) {
getSessionByTaskId: (taskId: string) =>
Object.values(sessions).find((s) => s.taskId === taskId),
removeSession: vi.fn(),
+ setTaskStarting: vi.fn(),
+ clearTaskStarting: vi.fn(),
updateSession: vi.fn(),
};
const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() };
@@ -73,7 +75,7 @@ function createHarness({ spyConnect = true } = {}) {
const connectToTask = spyConnect
? vi.spyOn(service, "connectToTask").mockResolvedValue(undefined)
: undefined;
- return { service, sessions, connectToTask, log };
+ return { service, sessions, connectToTask, log, store };
}
function reconcile(
@@ -162,14 +164,22 @@ describe("SessionService run-less local task recovery", () => {
expect(connectToTask).toHaveBeenCalledTimes(1);
});
- it("clears the in-flight mark when a connect starts", async () => {
- const { service, sessions } = createHarness({ spyConnect: false });
+ it("keeps the starting marker while clearing the recovery in-flight mark", async () => {
+ const { service, sessions, store } = createHarness({ spyConnect: false });
const task = makeTask();
- sessions[`run-${task.id}`] = makeSession(task.id);
+ sessions[`run-${task.id}`] = {
+ ...makeSession(task.id),
+ status: "connecting",
+ };
service.markTaskCreationInFlight(task.id);
+ vi.mocked(store.setTaskStarting).mockClear();
+ vi.mocked(store.clearTaskStarting).mockClear();
await service.connectToTask({ task, repoPath: "/repo" });
+ expect(store.setTaskStarting).toHaveBeenCalledWith(task.id);
+ expect(store.clearTaskStarting).not.toHaveBeenCalled();
+
const connectToTask = vi
.spyOn(service, "connectToTask")
.mockResolvedValue(undefined);
diff --git a/products/desktop/packages/core/src/sessions/sessionStore.ts b/products/desktop/packages/core/src/sessions/sessionStore.ts
index 469681044b6d..0338810b1298 100644
--- a/products/desktop/packages/core/src/sessions/sessionStore.ts
+++ b/products/desktop/packages/core/src/sessions/sessionStore.ts
@@ -25,12 +25,15 @@ export interface SessionState {
sessions: Record<string, AgentSession>;
/** Index mapping taskId -> taskRunId for O(1) lookups */
taskIdIndex: Record<string, string>;
+ /** Task ids whose first/resumed agent session is being created. */
+ startingTaskIds: Record<string, true>;
}
export const sessionStore = createStore<SessionState>()(
immer(() => ({
sessions: {},
taskIdIndex: {},
+ startingTaskIds: {},
})),
);
@@ -94,6 +97,7 @@ export const sessionStoreSetters = {
state.sessions[session.taskRunId] = session;
state.taskIdIndex[session.taskId] = session.taskRunId;
+ delete state.startingTaskIds[session.taskId];
});
},
@@ -102,11 +106,24 @@ export const sessionStoreSetters = {
const session = state.sessions[taskRunId];
if (session) {
delete state.taskIdIndex[session.taskId];
+ delete state.startingTaskIds[session.taskId];
}
delete state.sessions[taskRunId];
});
},
+ setTaskStarting: (taskId: string) => {
+ sessionStore.setState((state) => {
+ state.startingTaskIds[taskId] = true;
+ });
+ },
+
+ clearTaskStarting: (taskId: string) => {
+ sessionStore.setState((state) => {
+ delete state.startingTaskIds[taskId];
+ });
+ },
+
updateSession: (taskRunId: string, updates: Partial<AgentSession>) => {
sessionStore.setState((state) => {
if (state.sessions[taskRunId]) {
@@ -448,6 +465,7 @@ export const sessionStoreSetters = {
sessionStore.setState((state) => {
state.sessions = {};
state.taskIdIndex = {};
+ state.startingTaskIds = {};
});
},
};
diff --git a/products/desktop/packages/core/src/sessions/sessionStoreEviction.test.ts b/products/desktop/packages/core/src/sessions/sessionStoreEviction.test.ts
index ff04eccb72fc..4e37a235d63f 100644
--- a/products/desktop/packages/core/src/sessions/sessionStoreEviction.test.ts
+++ b/products/desktop/packages/core/src/sessions/sessionStoreEviction.test.ts
@@ -24,6 +24,21 @@ function seedWithEvents() {
afterEach(() => sessionStoreSetters.removeSession(RUN));
describe("evictEvents / restoreEvents", () => {
+ it("clears a starting marker when the session arrives", () => {
+ sessionStoreSetters.setTaskStarting(TASK);
+
+ sessionStoreSetters.setSession({
+ taskRunId: RUN,
+ taskId: TASK,
+ events: [],
+ messageQueue: [],
+ pendingPermissions: new Map(),
+ status: "connected",
+ } as unknown as AgentSession);
+
+ expect(sessionStore.getState().startingTaskIds[TASK]).toBeUndefined();
+ });
+
it("evictEvents frees the transcript and resets the line cursor", () => {
seedWithEvents();
expect(sessionStore.getState().sessions[RUN].events).toHaveLength(1);
diff --git a/products/desktop/packages/ui/src/features/canvas/components/ChannelItemRow.test.tsx b/products/desktop/packages/ui/src/features/canvas/components/ChannelItemRow.test.tsx
index 1c55cedca0c2..606610b95571 100644
--- a/products/desktop/packages/ui/src/features/canvas/components/ChannelItemRow.test.tsx
+++ b/products/desktop/packages/ui/src/features/canvas/components/ChannelItemRow.test.tsx
@@ -132,6 +132,11 @@ describe("ChannelItemRow", () => {
// rather than the status: starting, live but stalled, or something to read.
it.each([
["a permission prompt", { needsPermission: true }, "Needs your input"],
+ [
+ "an agent session being created",
+ { isAgentSessionStarting: true },
+ "Starting",
+ ],
["a streaming agent", { isGenerating: true }, "Working"],
[
// A background run is one-shot and unattended, so its in_progress really
diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useChannelTaskStatus.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useChannelTaskStatus.ts
index 14c29d99a4d0..78475f73bb3e 100644
--- a/products/desktop/packages/ui/src/features/canvas/hooks/useChannelTaskStatus.ts
+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useChannelTaskStatus.ts
@@ -1,6 +1,7 @@
import type { ChannelItemModel } from "@posthog/core/canvas/channelItems";
import type { Task } from "@posthog/shared/domain-types";
import { useChannelTaskData } from "@posthog/ui/features/canvas/hooks/useChannelTaskData";
+import { useTaskSessionStarting } from "@posthog/ui/features/sessions/useSession";
import type { TaskStatusInput } from "@posthog/ui/features/sidebar/components/items/taskStatusVocabulary";
import { useTaskPrStatus } from "@posthog/ui/features/sidebar/useTaskPrStatus";
import { useWorkspace } from "@posthog/ui/features/workspace/useWorkspace";
@@ -32,6 +33,7 @@ export function useTaskStatusInput(
): TaskStatusInput | null {
const taskData = useChannelTaskData(task);
const workspace = useWorkspace(task?.id);
+ const isAgentSessionStarting = useTaskSessionStarting(task?.id);
const { prState, hasDiff, prUrl } = useTaskPrStatus({
// An empty id is the hook's own "nothing to look up", so this asks for no
// query rather than one it throws away.
@@ -56,6 +58,7 @@ export function useTaskStatusInput(
slackThreadUrl: taskData.slackThreadUrl,
prState,
hasDiff,
+ isAgentSessionStarting,
// The url is the early signal: a cloud run writes it the moment it opens the
// PR, long before (or without ever) resolving the PR's state. A local run
// has no cloud url, so the one the host cached against the task stands in.
diff --git a/products/desktop/packages/ui/src/features/sessions/sessionServiceHost.test.ts b/products/desktop/packages/ui/src/features/sessions/sessionServiceHost.test.ts
index 2cf31aa2fbf1..ac294efc13fc 100644
--- a/products/desktop/packages/ui/src/features/sessions/sessionServiceHost.test.ts
+++ b/products/desktop/packages/ui/src/features/sessions/sessionServiceHost.test.ts
@@ -67,6 +67,8 @@ const mockTrpcOs = vi.hoisted(() => ({
const mockSessionStoreSetters = vi.hoisted(() => ({
setSession: vi.fn(),
removeSession: vi.fn(),
+ setTaskStarting: vi.fn(),
+ clearTaskStarting: vi.fn(),
updateSession: vi.fn(),
updateCloudStatus: vi.fn(),
appendEvents: vi.fn(),
@@ -7755,6 +7757,9 @@ describe("SessionService", () => {
);
expect(result.stopReason).toBe("queued");
+ expect(mockSessionStoreSetters.setTaskStarting).toHaveBeenCalledWith(
+ "task-123",
+ );
expect(mockAuthenticatedClient.runTaskInCloud).toHaveBeenCalledWith(
"task-123",
"feature/codex-run",
diff --git a/products/desktop/packages/ui/src/features/sessions/useSession.ts b/products/desktop/packages/ui/src/features/sessions/useSession.ts
index 9b2e9bb9cc20..4770381c960e 100644
--- a/products/desktop/packages/ui/src/features/sessions/useSession.ts
+++ b/products/desktop/packages/ui/src/features/sessions/useSession.ts
@@ -198,3 +198,10 @@ export const useSessionHandoffInProgress = (
return s.sessions[taskRunId]?.handoffInProgress ?? false;
});
};
+
+export const useTaskSessionStarting = (taskId: string | undefined): boolean => {
+ return useSessionStore((s) => {
+ if (!taskId) return false;
+ return s.startingTaskIds[taskId] === true;
+ });
+};
diff --git a/products/desktop/packages/ui/src/features/sidebar/components/items/taskStatusVocabulary.ts b/products/desktop/packages/ui/src/features/sidebar/components/items/taskStatusVocabulary.ts
index efdaa8c2979f..81902ee43e64 100644
--- a/products/desktop/packages/ui/src/features/sidebar/components/items/taskStatusVocabulary.ts
+++ b/products/desktop/packages/ui/src/features/sidebar/components/items/taskStatusVocabulary.ts
@@ -24,6 +24,7 @@ import { SlackMark } from "@posthog/ui/primitives/SlackMark";
*/
export type TaskStatusInput = TaskIconProps & {
prUrl?: string | null;
+ isAgentSessionStarting?: boolean;
};
/**
@@ -146,13 +147,14 @@ export function taskDot(props: TaskStatusInput): TaskDot {
// status, so it can sit there for hours after the agent is done with it.
const isStartingCloudRun =
props.taskRunStatus === "queued" && props.workspaceMode === "cloud";
- if (props.isGenerating || isStartingCloudRun) {
+ const isStarting = props.isAgentSessionStarting || isStartingCloudRun;
+ if (props.isGenerating || isStarting) {
return {
tone: "yellow",
style: "solid",
pulse: false,
spinner: true,
- label: props.isGenerating ? "Working" : "Starting",
+ label: props.isGenerating && !isStarting ? "Working" : "Starting",
};
}
// Only a background run's status is a claim about work. An interactive run is