: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): do not count opening a task or canvas as activity (#104973)
Shy Altera4f460bfdae3
diff --git a/products/desktop/packages/core/src/sessions/cloudTaskUpdateNotifications.test.ts b/products/desktop/packages/core/src/sessions/cloudTaskUpdateNotifications.test.tsindex d4ed6100ba5c..b65bac48b943 100644--- a/products/desktop/packages/core/src/sessions/cloudTaskUpdateNotifications.test.ts+++ b/products/desktop/packages/core/src/sessions/cloudTaskUpdateNotifications.test.ts@@ -766,6 +766,9 @@ describe("cloud task update notifications", () => {totalEntryCount: 1,});expect(harness.notifyPermissionRequest).toHaveBeenCalledTimes(1);+ // A request replayed out of a snapshot is what opening the session shows+ // the reader, so it must leave the activity clock where it was.+ expect(harness.markActivity).not.toHaveBeenCalled();harness.sendUpdate({taskId: TASK_ID,@@ -776,5 +779,6 @@ describe("cloud task update notifications", () => {options: [],});expect(harness.notifyPermissionRequest).toHaveBeenCalledTimes(2);+ expect(harness.markActivity).toHaveBeenCalledTimes(1);});});diff --git a/products/desktop/packages/core/src/sessions/sessionService.ts b/products/desktop/packages/core/src/sessions/sessionService.tsindex fe872a193c3e..9cba555e7e92 100644--- a/products/desktop/packages/core/src/sessions/sessionService.ts+++ b/products/desktop/packages/core/src/sessions/sessionService.ts@@ -4052,6 +4052,7 @@ export class SessionService {private handleCloudPermissionRequest(taskRunId: string,update: DerivedPermissionRequest,+ { isLive = false }: { isLive?: boolean } = {},): void {this.d.log.info("Cloud permission request received", {taskRunId,@@ -4102,7 +4103,10 @@ export class SessionService {});this.d.store.setPendingPermissions(taskRunId, newPermissions);- this.d.taskViewedApi.markActivity(session.taskId);+ // A replayed request is history the reader just opened, not new activity.+ if (isLive) {+ this.d.taskViewedApi.markActivity(session.taskId);+ }this.notifyNeedsInput(taskRunId, session, "cloud_permission_request");}@@ -8825,7 +8829,7 @@ export class SessionService {}if (update.kind === "permission_request") {- this.handleCloudPermissionRequest(taskRunId, update);+ this.handleCloudPermissionRequest(taskRunId, update, { isLive: true });return;}diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.tsnew file mode 100644index 000000000000..115667b5a575--- /dev/null+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.test.ts@@ -0,0 +1,119 @@+import type { ChannelSessionFacts } from "@posthog/core/canvas/channelItems";+import type { DashboardRecord } from "@posthog/core/canvas/dashboardSchemas";+import type { Task } from "@posthog/shared/domain-types";+import { describe, expect, it } from "vitest";+import { selectRecentWorkItems } from "./useRecentWorkItems";++const ME = "me-uuid";+const OLD = 1_000;+const NEW = 9_000;+const VIEWED_NOW = 100_000;++const NO_SESSION_FACTS: ChannelSessionFacts = {+ needsInputTaskIds: new Set(),+ viewedTimestamps: {},+ workspaceByTaskId: new Map(),+};++function canvas(over: Partial<DashboardRecord> = {}): DashboardRecord {+ return {+ id: "canvas-old",+ channelId: "c1",+ name: "Canvas",+ kind: "freeform" as const,+ description: "",+ templateId: "freeform",+ createdAt: 0,+ updatedAt: OLD,+ createdByUuid: ME,+ ...over,+ };+}++function task(over: Partial<Task> = {}): Task {+ return {+ id: "task-new",+ title: "Task",+ created_at: new Date(0).toISOString(),+ last_activity_at: new Date(NEW).toISOString(),+ ...over,+ } as Task;+}++function select(+ over: Partial<Parameters<typeof selectRecentWorkItems>[0]> = {},+) {+ return selectRecentWorkItems({+ dashboards: [],+ tasks: [],+ lastViewedByCanvasId: {},+ meUuid: ME,+ archivedTaskIds: new Set(),+ pinnedTaskIds: new Set(),+ sessionFacts: NO_SESSION_FACTS,+ ...over,+ });+}++function keys(over: Partial<Parameters<typeof selectRecentWorkItems>[0]> = {}) {+ return select(over).map(({ item }) => item.key);+}++describe("selectRecentWorkItems", () => {+ it.each([+ {+ case: "keeps a canvas the user just opened below newer work",+ over: {+ dashboards: [canvas()],+ tasks: [task()],+ lastViewedByCanvasId: { "canvas-old": VIEWED_NOW },+ },+ expected: ["task:task-new", "canvas:canvas-old"],+ },+ {+ case: "keeps a task the user just opened below newer work",+ over: {+ dashboards: [canvas({ id: "canvas-new", updatedAt: NEW })],+ tasks: [+ task({+ id: "task-old",+ last_activity_at: new Date(OLD).toISOString(),+ }),+ ],+ sessionFacts: {+ ...NO_SESSION_FACTS,+ viewedTimestamps: { "task-old": { lastViewedAt: VIEWED_NOW } },+ },+ },+ expected: ["canvas:canvas-new", "task:task-old"],+ },+ {+ case: "lists a canvas somebody else made once the user has opened it",+ over: {+ dashboards: [canvas({ createdByUuid: "other-uuid" })],+ lastViewedByCanvasId: { "canvas-old": VIEWED_NOW },+ },+ expected: ["canvas:canvas-old"],+ },+ {+ case: "drops a canvas somebody else made that the user never opened",+ over: { dashboards: [canvas({ createdByUuid: "other-uuid" })] },+ expected: [],+ },+ ])("$case", ({ over, expected }) => {+ expect(keys(over)).toEqual(expected);+ });++ it("carries each item's own channel", () => {+ expect(+ select({+ dashboards: [canvas({ channelId: "canvas-channel" })],+ tasks: [task({ channel: "task-channel" })],+ lastViewedByCanvasId: { "canvas-old": VIEWED_NOW },+ }),+ ).toEqual([+ expect.objectContaining({ channelId: "task-channel" }),+ expect.objectContaining({ channelId: "canvas-channel" }),+ ]);+ });+});diff --git a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.tsindex 8e671ee8dff5..ddc200db7a8d 100644--- a/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts+++ b/products/desktop/packages/ui/src/features/canvas/hooks/useRecentWorkItems.ts@@ -1,7 +1,10 @@import {buildChannelItems,type ChannelItemModel,+ type ChannelSessionFacts,} from "@posthog/core/canvas/channelItems";+import type { DashboardRecord } from "@posthog/core/canvas/dashboardSchemas";+import type { Task } from "@posthog/shared/domain-types";import { useArchivedTaskIds } from "@posthog/ui/features/archive/useArchivedTaskIds";import { useOptionalAuthenticatedClient } from "@posthog/ui/features/auth/authClient";import { useCurrentUser } from "@posthog/ui/features/auth/useCurrentUser";@@ -19,6 +22,48 @@ export interface RecentWorkItem {channelId: string | undefined;}+export function selectRecentWorkItems({+ dashboards,+ tasks,+ lastViewedByCanvasId,+ meUuid,+ archivedTaskIds,+ pinnedTaskIds,+ sessionFacts,+}: {+ dashboards: readonly DashboardRecord[];+ tasks: readonly Task[];+ lastViewedByCanvasId: Record<string, number>;+ meUuid: string | null;+ archivedTaskIds: ReadonlySet<string>;+ pinnedTaskIds: ReadonlySet<string>;+ sessionFacts: ChannelSessionFacts;+}): RecentWorkItem[] {+ const mine = dashboards.filter(+ (canvas) =>+ lastViewedByCanvasId[canvas.id] != null ||+ (meUuid != null && canvas.createdByUuid === meUuid),+ );+ const canvasChannelId = new Map(mine.map((c) => [c.id, c.channelId]));+ // The order is the items' own activity time. Opening an item is not activity,+ // so the local "viewed" time stays out of it and only says which canvases+ // belong to this list.+ return buildChannelItems({+ dashboards: mine,+ feedTasks: tasks,+ archivedTaskIds,+ pinnedTaskIds,+ ownedBy: null,+ sessionFacts,+ })+ .sort((a, b) => b.ts - a.ts)+ .slice(0, RECENT_WORK_CAP)+ .map((item) => ({+ item,+ channelId: item.task?.channel ?? canvasChannelId.get(item.id),+ }));+}+export function useRecentWorkItems(): {items: RecentWorkItem[];isLoading: boolean;@@ -35,46 +80,27 @@ export function useRecentWorkItems(): {const { data: currentUser } = useCurrentUser({ client });const meUuid = currentUser?.uuid ?? null;- const items = useMemo<RecentWorkItem[]>(() => {- const mine = dashboards.filter(- (canvas) =>- lastViewedByCanvasId[canvas.id] != null ||- (meUuid != null && canvas.createdByUuid === meUuid),- );- const channelByKey = new Map<string, string | undefined>();- for (const canvas of mine)- channelByKey.set(`canvas:${canvas.id}`, canvas.channelId);- for (const task of tasks) {- channelByKey.set(`task:${task.id}`, task.channel ?? undefined);- }- const built = buildChannelItems({- dashboards: mine,- feedTasks: tasks,+ const items = useMemo<RecentWorkItem[]>(+ () =>+ selectRecentWorkItems({+ dashboards,+ tasks,+ lastViewedByCanvasId,+ meUuid,+ archivedTaskIds,+ pinnedTaskIds,+ sessionFacts,+ }),+ [+ dashboards,+ lastViewedByCanvasId,+ meUuid,+ tasks,archivedTaskIds,pinnedTaskIds,- ownedBy: null,sessionFacts,- });- return built- .map((item) => {- const viewedAt =- item.kind === "canvas"- ? (lastViewedByCanvasId[item.id] ?? 0)- : (sessionFacts.viewedTimestamps[item.id]?.lastViewedAt ?? 0);- return { item: { ...item, ts: Math.max(item.ts, viewedAt) } };- })- .sort((a, b) => b.item.ts - a.item.ts)- .slice(0, RECENT_WORK_CAP)- .map(({ item }) => ({ item, channelId: channelByKey.get(item.key) }));- }, [- dashboards,- lastViewedByCanvasId,- meUuid,- tasks,- archivedTaskIds,- pinnedTaskIds,- sessionFacts,- ]);+ ],+ );return { items, isLoading: tasksLoading || canvasesLoading };}