fix: key dashboard task cards by the native issue id (#15032)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The dashboard draws one task card per issue
> - `selectDashboardRunIds` reads only `contextSnapshot.issueId`
> - A native run stores the task on `nativeIssueId`
> - Older runs store it on `contextSnapshot.taskId`
> - The same task is then drawn as two cards
> - This pull request coalesces the bound issue id, then native issue
id, then context issue id, then task id
> - The benefit is that one task has one dashboard card

## Linked Issues or Issue Description

No open issue covers this. I searched open and closed issues and pull
requests.

**What happened?**

Dashboard task cards ignore the bound and native issue ids.
`selectDashboardRunIds` uses only `contextSnapshot.issueId`. Recovery
already reads `nativeIssueId`, then the context issue id, then `taskId`.
A native or bound run and a context run for the same task both take a
card slot.

**Expected behavior**

Use `coalesce(issueId, nativeIssueId, contextSnapshot.issueId,
contextSnapshot.taskId)` for the card key. Two runs for the same task
collapse to one card.

**Steps to reproduce**

1. Insert two runs for one company with the same `nativeIssueId`.
2. Give them different `contextSnapshot.issueId` values, or omit
`issueId` and set `taskId`.
3. Call `selectDashboardRunIds` with a small limit.
4. Observe that both runs are selected.

**Paperclip version or commit**

master at 215586d127

**Deployment mode**

Local dev (pnpm dev)

## What Changed

- Add `dashboardTaskIssueId`: the bound `issue_id` column first, then
the recovery coalesce order.
- Use it in `selectDashboardRunIds`.
- Add a test that collapses a native pair and a task-id pair, and keeps
a different context issue separate.
- Add a test that collapses a run bound to an issue with a context run
for the same issue.
- After merging master, the live-runs route keeps master's `issueId`
column (the bound `issue_id`), since the run read check uses it.

## Verification

- `pnpm exec vitest run src/__tests__/dashboard-service.test.ts --config
vitest.config.ts` in `server`
- Result: 1 file passed, 6 tests passed

## Risks

- Only card selection changes. The live-runs response and its read check
are unchanged from master.

## Model Used

- No model runtime is part of this change.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge
This commit is contained in:
Andrii Furmanets authored and GitHub committed 2026-12-04 21:04:45 -08:00
1 parent 51b398194a
commit dbfcbb532b
2 files changed
+98 -2

No files matched your search

+92 -1
View File
@@ -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();
@@ -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<string | null>`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<string[]> {
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<string | null>`${heartbeatRuns.contextSnapshot} ->> 'issueId'`,
issueId: dashboardTaskIssueId(),
};
const [activeRuns, recentRuns] = await Promise.all([
db.select(columns)