diff --git a/server/src/__tests__/dashboard-service.test.ts b/server/src/__tests__/dashboard-service.test.ts index 71b6ff9cd24..f166bc5837b 100644 --- a/server/src/__tests__/dashboard-service.test.ts +++ b/server/src/__tests__/dashboard-service.test.ts @@ -1,6 +1,6 @@ import { randomUUID } from "node:crypto"; import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; -import { agents, companies, createDb, heartbeatRuns } from "@paperclipai/db"; +import { agents, companies, createDb, heartbeatRuns, issues } from "@paperclipai/db"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase, @@ -49,6 +49,7 @@ describeEmbeddedPostgres("dashboard service", () => { afterEach(async () => { await db.delete(heartbeatRuns); + await db.delete(issues); await db.delete(agents); await db.delete(companies); }); @@ -93,6 +94,96 @@ describeEmbeddedPostgres("dashboard service", () => { ]); }); + it("collapses task cards on native issue id before context issue or task id", async () => { + const companyId = randomUUID(); + const agentId = randomUUID(); + const nativeIssueId = randomUUID(); + const staleContextIssueId = randomUUID(); + const taskId = randomUUID(); + const otherIssueId = randomUUID(); + const activeNativeRunId = randomUUID(); + const newerNativeRunId = randomUUID(); + const newerTaskRunId = randomUUID(); + const olderTaskRunId = randomUUID(); + const otherIssueRunId = randomUUID(); + + await db.insert(companies).values({ id: companyId, name: "Native cards", issuePrefix: "NAT" }); + await db.insert(agents).values({ + id: agentId, companyId, name: "Builder", role: "engineer", adapterType: "codex_local", + adapterConfig: {}, runtimeConfig: {}, permissions: {}, + }); + await db.insert(heartbeatRuns).values([ + { + id: activeNativeRunId, companyId, agentId, status: "running", nativeIssueId, + contextSnapshot: { taskId: randomUUID() }, + createdAt: new Date("2026-04-10T08:00:00.000Z"), + }, + { + // Sorts ahead of the distinct issue. Without nativeIssueId this occupies a slot. + id: newerNativeRunId, companyId, agentId, status: "succeeded", nativeIssueId, + contextSnapshot: { issueId: staleContextIssueId }, + createdAt: new Date("2026-04-10T09:40:00.000Z"), + }, + { + // Sorts ahead of the older run for the same task. Without taskId both are kept. + id: newerTaskRunId, companyId, agentId, status: "succeeded", + contextSnapshot: { taskId }, + createdAt: new Date("2026-04-10T09:35:00.000Z"), + }, + { + id: olderTaskRunId, companyId, agentId, status: "succeeded", + contextSnapshot: { taskId }, + createdAt: new Date("2026-04-10T09:20:00.000Z"), + }, + { + id: otherIssueRunId, companyId, agentId, status: "succeeded", + contextSnapshot: { issueId: otherIssueId }, + createdAt: new Date("2026-04-10T09:10:00.000Z"), + }, + ]); + + expect(await selectDashboardRunIds(db, companyId, 3)).toEqual([ + activeNativeRunId, newerTaskRunId, otherIssueRunId, + ]); + }); + + it("collapses task cards on the bound issue id before context ids", async () => { + const companyId = randomUUID(); + const agentId = randomUUID(); + const boundIssueId = randomUUID(); + const otherIssueId = randomUUID(); + const boundRunId = randomUUID(); + const contextRunId = randomUUID(); + const otherIssueRunId = randomUUID(); + + await db.insert(companies).values({ id: companyId, name: "Bound cards", issuePrefix: "BND" }); + await db.insert(agents).values({ + id: agentId, companyId, name: "Builder", role: "engineer", adapterType: "codex_local", + adapterConfig: {}, runtimeConfig: {}, permissions: {}, + }); + await db.insert(issues).values({ id: boundIssueId, companyId, title: "Bound task" }); + await db.insert(heartbeatRuns).values([ + { + // Bound to the issue, with a stale context id. Without issueId this occupies two slots. + id: boundRunId, companyId, agentId, status: "succeeded", scopeKind: "issue", issueId: boundIssueId, + contextSnapshot: { issueId: randomUUID() }, + createdAt: new Date("2026-04-10T09:40:00.000Z"), + }, + { + id: contextRunId, companyId, agentId, status: "succeeded", + contextSnapshot: { issueId: boundIssueId }, + createdAt: new Date("2026-04-10T09:30:00.000Z"), + }, + { + id: otherIssueRunId, companyId, agentId, status: "succeeded", + contextSnapshot: { issueId: otherIssueId }, + createdAt: new Date("2026-04-10T09:10:00.000Z"), + }, + ]); + + expect(await selectDashboardRunIds(db, companyId, 2)).toEqual([boundRunId, otherIssueRunId]); + }); + it("aggregates the full 14-day run activity window without recent-run truncation", async () => { const companyId = randomUUID(); const otherCompanyId = randomUUID(); diff --git a/server/src/services/dashboard-run-selection.ts b/server/src/services/dashboard-run-selection.ts index 1efe82f9dae..3b09c6126d2 100644 --- a/server/src/services/dashboard-run-selection.ts +++ b/server/src/services/dashboard-run-selection.ts @@ -5,6 +5,11 @@ import { and, desc, eq, inArray, sql } from "drizzle-orm"; const MAX_CANDIDATES_PER_POOL = 1_000; const ACTIVE_STATUSES = ["queued", "running"]; +/** Task identity for a card: bound issue, then native issue, then context issue, then task id. */ +function dashboardTaskIssueId() { + return sql`coalesce(${heartbeatRuns.issueId}::text, ${heartbeatRuns.nativeIssueId}::text, ${heartbeatRuns.contextSnapshot}->>'issueId', ${heartbeatRuns.contextSnapshot}->>'taskId')`; +} + /** Select recent dashboard cards without ranking the company's full run history. */ export async function selectDashboardRunIds(db: Db, companyId: string, limit: number): Promise { if (limit <= 0) return []; @@ -13,7 +18,7 @@ export async function selectDashboardRunIds(db: Db, companyId: string, limit: nu id: heartbeatRuns.id, status: heartbeatRuns.status, createdAt: heartbeatRuns.createdAt, - issueId: sql`${heartbeatRuns.contextSnapshot} ->> 'issueId'`, + issueId: dashboardTaskIssueId(), }; const [activeRuns, recentRuns] = await Promise.all([ db.select(columns)