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 last run for workflow-backed loops (#107725)

Harley Alexandere8ce36b914bd
diff --git a/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.test.tsx b/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.test.tsx
new file mode 100644
index 000000000000..9b09a3b6296c
--- /dev/null
+++ b/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.test.tsx
@@ -0,0 +1,71 @@
+import { runLoop } from "@posthog/api-client/loops";
+import { useLoopsHogFlowsEnabled } from "@posthog/ui/features/feature-flags/useLoopsHogFlowsEnabled";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { renderHook } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { runLoopHogFlow } from "../loopHogFlowWrites";
+import { loopsKeys } from "./loopsKeys";
+import { useRunLoop } from "./useLoopMutations";
+
+vi.mock("@posthog/api-client/loops", () => ({
+ runLoop: vi.fn(),
+}));
+vi.mock("@posthog/api-client/hogFlowLoops", () => ({
+ deleteHogFlow: vi.fn(),
+}));
+vi.mock("@posthog/ui/features/feature-flags/useLoopsHogFlowsEnabled", () => ({
+ useLoopsHogFlowsEnabled: vi.fn(),
+}));
+vi.mock("../loopHogFlowWrites", () => ({
+ LoopScheduleSaveError: class extends Error {},
+ createLoopHogFlow: vi.fn(),
+ runLoopHogFlow: vi.fn(),
+ setLoopHogFlowEnabled: vi.fn(),
+ updateLoopHogFlow: vi.fn(),
+}));
+vi.mock("../loopHogFlowMapping", () => ({
+ formValuesToHogFlowWrite: vi.fn(),
+ hogFlowToLoop: vi.fn(),
+}));
+vi.mock("./useLoopsClient", () => ({
+ useLoopsClient: () => ({ client: {}, projectId: "1" }),
+}));
+
+const FIRED = {
+ created: true,
+ reason: "created",
+ task_id: null,
+ task_run_id: null,
+} as const;
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ vi.mocked(runLoop).mockResolvedValue(FIRED);
+ vi.mocked(runLoopHogFlow).mockResolvedValue(FIRED);
+});
+
+describe("useRunLoop", () => {
+ it.each([
+ { hogFlows: true, listKey: loopsKeys.hogFlowList("1") },
+ { hogFlows: false, listKey: loopsKeys.list("1") },
+ ])(
+ "marks the cached loop list stale after a run (hogFlows: $hogFlows)",
+ async ({ hogFlows, listKey }) => {
+ vi.mocked(useLoopsHogFlowsEnabled).mockReturnValue(hogFlows);
+ const queryClient = new QueryClient();
+ queryClient.setQueryData(listKey, []);
+ const { result } = renderHook(() => useRunLoop("loop-1"), {
+ wrapper: ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={queryClient}>
+ {children}
+ </QueryClientProvider>
+ ),
+ });
+
+ await result.current.mutateAsync();
+
+ expect(queryClient.getQueryState(listKey)?.isInvalidated).toBe(true);
+ },
+ );
+});
diff --git a/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.ts b/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.ts
index 57deaad991a3..f4e8de3c7418 100644
--- a/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.ts
+++ b/products/desktop/packages/ui/src/features/loops/hooks/useLoopMutations.ts
@@ -223,6 +223,8 @@ export function useRunLoop(loopId: string) {
void queryClient.invalidateQueries({
queryKey: loopsKeys.hogFlow(projectId, loopId),
});
+ // The list shows each loop's last run, so it goes stale when a run fires.
+ invalidateLoopLists(queryClient, projectId);
},
});
}
diff --git a/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.test.ts b/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.test.ts
index db8b669171b6..5045b9690f1b 100644
--- a/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.test.ts
+++ b/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.test.ts
@@ -324,6 +324,31 @@ describe("loopHogFlowMapping", () => {
expect(hogFlowTeamSkills(flow)).toEqual(["error-triage"]);
});
+ it.each([
+ {
+ name: "the newest task's run",
+ last_run: {
+ task_id: "task-1",
+ status: "failed",
+ ran_at: "2026-09-03T08:00:00Z",
+ },
+ expected: {
+ last_run_at: "2026-09-03T08:00:00Z",
+ last_run_status: "failed",
+ },
+ },
+ {
+ name: "no run for a flow without tasks",
+ last_run: null,
+ expected: { last_run_at: null, last_run_status: null },
+ },
+ ])("reads $name as the loop's last run", ({ last_run, expected }) => {
+ const flow = { ...flowFromWrite(scheduleValues()), last_run };
+ expect(hogFlowToLoop(flow, { projectId: PROJECT_ID })).toMatchObject(
+ expected,
+ );
+ });
+
it("treats a list row without schedules as loop-shaped with an empty cadence", () => {
const { schedules: _omitted, ...row } = flowFromWrite(scheduleValues());
expect(isLoopShapedHogFlow(row)).toBe(true);
diff --git a/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.ts b/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.ts
index 0dfe1d320244..b8b946c6a3a0 100644
--- a/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.ts
+++ b/products/desktop/packages/ui/src/features/loops/loopHogFlowMapping.ts
@@ -64,6 +64,9 @@ export interface LoopHogFlowSource {
* the detail carries it; a list row leaves it undefined. */
draft?: unknown;
draft_updated_at?: string | null;
+ /** The newest task the workflow created. A backend that predates the field
+ * omits it, which reads as a loop that never ran. */
+ last_run?: { task_id: string; status: string; ran_at: string } | null;
}
/** Task inputs the form owns. Anything else on the step is left as found. */
@@ -590,8 +593,8 @@ export function hogFlowToLoop(
context_target: spaceFromTaskInputs(inputs),
internal: false,
origin_product: LOOPS_ORIGIN_PRODUCT,
- last_run_at: null,
- last_run_status: null,
+ last_run_at: flow.last_run?.ran_at ?? null,
+ last_run_status: flow.last_run ? loopRunStatus(flow.last_run.status) : null,
last_error: null,
consecutive_failures: 0,
created_at: flow.created_at,