mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-13 07:12:00 +01:00
perf(ui): speed up task and inbox cold opens (#17756)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Task lists and inboxes are the main entry points to that work. > - Cold opens fetch overlapping task lists, task descriptions, and run details before useful rows settle. > - Independent responses can insert rows above content that a person has started to read. > - This pull request loads a small task index first and adds details only for visible rows. > - It keeps row order and geometry stable while those details arrive. ## Linked Issues or Issue Description **What existing behavior does this improve?** Cold opens of Tasks and Inbox, including the shared navigation badge. **Subsystem affected** Server list reads, shared response contracts, and the React UI. **Current behavior** The badge fetches full task bodies. Mine fetches several overlapping lists. Historical diagnostics and row metadata add work before the first useful list. A fast failed-run response can appear before task rows, then move down when tasks arrive. **Proposed behavior** Load the selected inbox source and its required context together. Use an index without task descriptions or recovery diagnostics. Fetch diagnostic details for up to 50 visible rows. Keep skeletons, recovery indicators, external status, and live metadata in fixed space. **Reason and benefit** Reduce work on a cold open and prevent loading-induced row movement. On the same 100 local fixture tasks, the compact response was 262,441 decoded bytes and the index was 110,909 bytes. This comparison excludes compression and is not a page latency benchmark. **Breaking changes** Existing full and compact responses remain available. New index/row projections are opt-in and marked as partial data. Mine keeps its existing most-recent-100 display limit, now selected on the server. Existing filters and group preferences remain in the client. No database migration. **Additional context** Refs: #17593. Related: #13095, #17428, #13803. This change addresses collection overfetch and layout stability. It does not replace the blocked-inbox query work or the database activity-index work. ## What Changed - Add authorized index and row projections, with bounded task and parent ID batches. - Replace badge list downloads with unread IDs and aggregate counts. Preserve actor privacy, dismissals, and local archive behavior. - Load only the selected inbox source, plus bounded parent and failed-run context. Return only relevant failures from the bounded run window. - Batch hierarchy reads, including at most one authorized child per collapsed parent in one request. Collapsed markers do not consume the expanded row budget, and partial descendant counts are not shown as totals. Enrich visible rows without replacing membership, ordering, titles, or read state. - Reserve row geometry on desktop and mobile. Reveal the initial inbox only after its sources settle. Preserve legacy task-list reads. - Start collection module downloads during access checks. Keep partial rows out of the task-detail cache. - Keep ETags and concurrent request sharing for the new projections, but skip the short server response TTL so archive undo sees fresh state. ## Verification - On final head `ef6afb2a56`, the full CI workflow passed, including build, typecheck, server/workspace/runner suites, and all eight browser shards. A server shard reported **1,489 passing tests** before runner shutdown canceled the job; its single infrastructure retry passed. Both Greptile and Paperclip Review are **5/5**, with zero unresolved review threads. - The optional external **Entire Gates** check remains `unassessed` because it has no active assessed Change for this comparison. Entire is not set up in this checkout. The repository ruleset requires `ci / verify` and `ci / e2e`; both passed. No gate configuration was changed. - Passed workspace typecheck after the final API batching change. The full production build also passed on the final commit; the exact-retry E2E run built the shipped UI. - Passed token gates and map validation. The final batch-preview pass has 91 passing route/hierarchy/component tests, including privacy, request bounds, 30 collapsed parents in one read, and a 120-child parent alongside a sibling. - Passed the 166-test focused UI/inbox/badge run, 47 additional UI regression tests, and 34 issue-list route tests. The latest regressions cover low-trust agent badge IDs, cold company selection, 30 description-only Mine matches with archive actions, retrying optional row details, and collapsed/expanded siblings. - The full local `pnpm test:run` spanned the rebase and reported failures in five suites before it was stopped; it is not claimed as a complete pass. A fresh serial rerun of all five suites passed **452/452** tests (tool gateway, native resume, project repositories, workspace recovery, and runner selection). Full current-commit coverage is checked by CI. - Real browser: delayed index and row-detail responses on Tasks and Mine. The first placeholder and first row had the same origin and 44-pixel height. Twelve rows on each page had zero position, height, or title-width change after recovery details arrived. - Real browser: reviewed long titles and recovery status at a 390 by 844 viewport. Task text stays on one line and recovery uses a compact indicator. - Three fresh document loads per page on final commit `260893bf92`, with HTTP cache retained on a warm local development server: Tasks 1.031–1.190 seconds; Mine 0.999–1.193 seconds. Browser-control overhead is included. These values must not be compared directly with staging latency or the earlier local samples taken during a full test run. - The browser archive test exposed stale results after Undo. The new projections now bypass the server TTL; immediate-mutation regression tests pass. The final browser retry passed: Archive followed by Undo restored exactly one row and the badge count. - CI exposed a hook-order crash when company selection resolved after the first inbox render. The hook now runs before the empty-company return. All seven affected exact-retry and legacy-continuation browser scenarios pass locally after the fix. - Real browser review regressions: 97 description-only source matches retained Archive actions; deliberately failed optional row reads displayed a retry notice while all 12 observed row positions and heights stayed unchanged. Retry restored the recovery indicator. Network fault injection was removed. - The final production build and workspace typecheck passed. Final focused verification passed 153 UI/helper tests and 26 heartbeat privacy route tests. Final review regressions cover newer diagnostics arriving during the index read, reloading diagnostics when the same task ID changes version, optional source failure/retry without hiding available rows, and required external-status filter failure. The latest-failures route test verifies bounded selection, agent filtering, success suppressing an older failure, and private-run redaction. - Real browser: deliberately failed the run source, then retried it. Twelve available task rows kept the same position and 44-pixel height before, during, and after retry. No loading skeleton replaced them. Fault injection was removed. - Hierarchy review decision: the cold-open barrier stays because inserting ancestors or expanded children after revealing index rows would cause layout shifts. Existing hierarchy context now survives subsequent requests, with a regression proving a new ancestor read does not blank the list. The bounded recursive tree read can still add latency for unusually deep expanded trees. - Tasks now handles a failed external-status filter with an explicit error and Retry, suppressing a false empty state. All 52 Tasks component tests pass, including failure-to-success retry. One existing large-tree test timed out while workspace typechecking ran concurrently; the whole component suite passed when rerun without that competing work. Workspace typecheck, the full production build, and token gates passed for this fix. ## Risks - Saved filters and grouping still operate on the existing bounded collection. This is not a new global server pagination model. - Fixed row slots reduce available title width. Long titles remain accessible through task navigation. - Recovery and related diagnostics arrive after core rows. Their responses cannot replace ordering fields, and stale versions are rejected. - Index/row requests now revalidate against current data instead of a short server TTL. They still share concurrent reads and use ETags. - This has not been deployed by this pull request. Staging latency must be measured after deployment under comparable conditions. ## Model Used OpenAI Codex, GPT-6 family. The exact runtime model ID and context-window size are not exposed in this session. Used code editing, terminal execution, and real-browser diagnostics. ## 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 - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
dfedd73f56
commit
5061ab7b35
41 files changed
+1214
-551
No files matched your search
@@ -328,6 +328,37 @@ describeEmbeddedPostgres("heartbeat run privacy routes", { concurrent: false },
|
||||
expect(maintenance.body.resultJson.summary).toBe("Maintenance complete");
|
||||
});
|
||||
|
||||
it("filters latest failures within the requested run window and preserves private-run redaction", async () => {
|
||||
const f = await seedFixture();
|
||||
await db.update(heartbeatRuns).set({ status: "failed", createdAt: new Date("2026-10-26T10:00:00Z") })
|
||||
.where(eq(heartbeatRuns.id, f.privateRunId));
|
||||
await db.update(heartbeatRuns).set({ createdAt: new Date("2026-10-26T09:00:00Z") })
|
||||
.where(eq(heartbeatRuns.id, f.maintenanceRunId));
|
||||
const timeoutId = randomUUID();
|
||||
await db.insert(heartbeatRuns).values({
|
||||
id: timeoutId, companyId: f.companyId, agentId: f.otherAgentId, scopeKind: "company",
|
||||
invocationSource: "timer", status: "timed_out", createdAt: new Date("2026-10-26T11:00:00Z"),
|
||||
});
|
||||
const app = createApp(f.companyId, f.otherAgentId);
|
||||
const path = `/api/companies/${f.companyId}/heartbeat-runs?summary=true&latestFailuresOnly=true`;
|
||||
const response = await request(app).get(path).expect(200);
|
||||
expect(response.body.map((run: { id: string }) => run.id)).toEqual([timeoutId, f.privateRunId]);
|
||||
const privateStub = response.body[1];
|
||||
expect(privateStub).toMatchObject({ redacted: true, status: "failed" });
|
||||
expect(privateStub).not.toHaveProperty("issueId");
|
||||
expect(privateStub).not.toHaveProperty("contextSnapshot");
|
||||
expect(JSON.stringify(response.body)).not.toContain("Confidential email triage");
|
||||
const bounded = await request(app).get(`${path}&limit=1`).expect(200);
|
||||
expect(bounded.body.map((run: { id: string }) => run.id)).toEqual([timeoutId]);
|
||||
const agentOnly = await request(app).get(`${path}&agentId=${f.ownerAgentId}`).expect(200);
|
||||
expect(agentOnly.body.map((run: { id: string }) => run.id)).toEqual([f.privateRunId]);
|
||||
// A newer success suppresses an older failure rather than selecting the latest failure alone.
|
||||
await db.update(heartbeatRuns).set({ createdAt: new Date("2026-10-26T12:00:00Z") })
|
||||
.where(eq(heartbeatRuns.id, f.maintenanceRunId));
|
||||
const recovered = await request(app).get(path).expect(200);
|
||||
expect(recovered.body.map((run: { id: string }) => run.id)).toEqual([timeoutId]);
|
||||
});
|
||||
|
||||
it("returns full private-run content to the owner and an explicitly granted agent", async () => {
|
||||
const fixture = await seedFixture();
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ import {
|
||||
heartbeatRuns,
|
||||
inboxDismissals,
|
||||
invites,
|
||||
issues,
|
||||
issueComments,
|
||||
issueReadStates,
|
||||
joinRequests,
|
||||
} from "@paperclipai/db";
|
||||
import {
|
||||
@@ -22,6 +25,7 @@ import { inboxDismissalRoutes } from "../routes/inbox-dismissals.js";
|
||||
import { inboxDismissalService } from "../services/inbox-dismissals.ts";
|
||||
import { sidebarBadgeRoutes } from "../routes/sidebar-badges.js";
|
||||
import { sidebarBadgeService } from "../services/sidebar-badges.ts";
|
||||
import { LOW_TRUST_REVIEW_PRESET } from "@paperclipai/shared";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
@@ -46,6 +50,9 @@ describeEmbeddedPostgres("inbox dismissals", () => {
|
||||
}, 20_000);
|
||||
|
||||
afterEach(async () => {
|
||||
await db.delete(issueReadStates);
|
||||
await db.delete(issueComments);
|
||||
await db.delete(issues);
|
||||
await db.delete(inboxDismissals);
|
||||
await db.delete(joinRequests);
|
||||
await db.delete(invites);
|
||||
@@ -100,6 +107,49 @@ describeEmbeddedPostgres("inbox dismissals", () => {
|
||||
expect(response.body.inbox).toBe(expected);
|
||||
});
|
||||
|
||||
it("counts only unread Mine tasks without returning their bodies", async () => {
|
||||
const companyId = randomUUID(), issueId = randomUUID(), otherId = randomUUID();
|
||||
await db.insert(companies).values({ id: companyId, name: "Badge fixture", issuePrefix: "BADGE" });
|
||||
const createdAt = new Date("2026-03-11T01:00:00Z");
|
||||
const replyAt = new Date("2026-03-11T02:00:00Z");
|
||||
await db.insert(issues).values([
|
||||
{ id: issueId, companyId, title: "Mine", description: "Large body".repeat(10000), createdByUserId: "local-board", createdAt },
|
||||
{ id: otherId, companyId, title: "Not mine", createdByUserId: "someone-else", createdAt },
|
||||
]);
|
||||
await db.insert(issueComments).values([issueId, otherId].map((id) => ({ companyId, issueId: id, authorType: "user" as const, authorUserId: "someone-else", body: "Reply", createdAt: replyAt })));
|
||||
const app = express();
|
||||
app.use((req, _res, next) => {
|
||||
req.actor = { type: "board", source: "local_implicit", userId: "local-board", companyIds: [companyId], isInstanceAdmin: true };
|
||||
next();
|
||||
});
|
||||
app.use("/api", sidebarBadgeRoutes(db));
|
||||
app.use(errorHandler);
|
||||
const response = await request(app).get(`/api/companies/${companyId}/sidebar-badges`).expect(200);
|
||||
expect(response.body).toMatchObject({ inbox: 1, unreadIssueIds: [issueId] });
|
||||
expect(JSON.stringify(response.body).length).toBeLessThan(300);
|
||||
const agentId = randomUUID();
|
||||
await db.insert(agents).values({
|
||||
id: agentId, companyId, name: "Restricted reviewer", role: "engineer",
|
||||
permissions: {
|
||||
trustPreset: LOW_TRUST_REVIEW_PRESET,
|
||||
authorizationPolicy: { trustBoundary: { mode: LOW_TRUST_REVIEW_PRESET, rootIssueId: otherId, projectIds: [] } },
|
||||
},
|
||||
});
|
||||
const agentApp = express();
|
||||
agentApp.use((req, _res, next) => {
|
||||
req.actor = { type: "agent", source: "agent_jwt", agentId, companyId, onBehalfOfUserId: "local-board",
|
||||
onBehalfOfMemberships: [{ companyId, membershipRole: "member", status: "active" }] };
|
||||
next();
|
||||
});
|
||||
agentApp.use("/api", sidebarBadgeRoutes(db));
|
||||
agentApp.use(errorHandler);
|
||||
const agentResponse = await request(agentApp).get(`/api/companies/${companyId}/sidebar-badges`).expect(200);
|
||||
expect(agentResponse.body).toMatchObject({ inbox: 0, unreadIssueIds: [] });
|
||||
await db.insert(issueReadStates).values({ companyId, issueId, userId: "local-board", lastReadAt: replyAt });
|
||||
const read = await request(app).get(`/api/companies/${companyId}/sidebar-badges`).expect(200);
|
||||
expect(read.body).toMatchObject({ inbox: 0, unreadIssueIds: [] });
|
||||
});
|
||||
|
||||
it("upserts a single dismissal record per user and inbox item key", async () => {
|
||||
const companyId = randomUUID();
|
||||
const userId = "board-user";
|
||||
|
||||
@@ -97,6 +97,80 @@ describeEmbeddedPostgres("issue list routes assigneeAgentId filter", () => {
|
||||
});
|
||||
}
|
||||
|
||||
it.each(["index", "rows"])("revalidates %s rows immediately after a mutation", async (view) => {
|
||||
const companyId = randomUUID(), issueId = randomUUID();
|
||||
await db.insert(companies).values({ id: companyId, name: "Fresh list", issuePrefix: uniqueIssuePrefix() });
|
||||
await seedCloudTenantMember(companyId);
|
||||
await db.insert(issues).values({ id: issueId, companyId, title: "Before" });
|
||||
const app = createApp(companyId);
|
||||
const before = await request(app).get(`/api/companies/${companyId}/issues`).query({ view, ids: issueId }).expect(200);
|
||||
await db.update(issues).set({ title: "After" }).where(eq(issues.id, issueId));
|
||||
const after = await request(app).get(`/api/companies/${companyId}/issues`).query({ view, ids: issueId })
|
||||
.set("If-None-Match", before.headers.etag).expect(200);
|
||||
expect(after.body[0].title).toBe("After");
|
||||
expect(after.headers.etag).not.toBe(before.headers.etag);
|
||||
expect(after.headers["x-paperclip-request-cache"]).toBe("miss");
|
||||
});
|
||||
|
||||
it("keeps index and row batches scoped to readable tasks and validates their bounds", async () => {
|
||||
vi.stubEnv("PAPERCLIP_ISSUE_PRIVACY_MODE", "enforce");
|
||||
try {
|
||||
const companyId = randomUUID(), foreignCompanyId = randomUUID();
|
||||
const parentId = randomUUID(), childId = randomUUID(), privateId = randomUUID(), foreignId = randomUUID();
|
||||
await db.insert(companies).values([
|
||||
{ id: companyId, name: "Lists", issuePrefix: uniqueIssuePrefix() },
|
||||
{ id: foreignCompanyId, name: "Other", issuePrefix: uniqueIssuePrefix() },
|
||||
]);
|
||||
await seedCloudTenantMember(companyId);
|
||||
await db.insert(issues).values([
|
||||
{ id: parentId, companyId, title: "Public parent" },
|
||||
{ id: childId, companyId, parentId, title: "Public child" },
|
||||
{ id: privateId, companyId, parentId, title: "Private child", visibility: "private", privacyRootIssueId: privateId, createdByUserId: "someone-else" },
|
||||
{ id: foreignId, companyId: foreignCompanyId, title: "Foreign task" },
|
||||
]);
|
||||
const app = createApp(companyId);
|
||||
for (const view of ["index", "rows"]) {
|
||||
const selected = await request(app).get(`/api/companies/${companyId}/issues`).query({ view, ids: [parentId, privateId, foreignId].join(",") });
|
||||
expect(selected.status, JSON.stringify(selected.body)).toBe(200);
|
||||
expect(selected.body.map((row: { id: string }) => row.id)).toEqual([parentId]);
|
||||
const children = await request(app).get(`/api/companies/${companyId}/issues`).query({ view, parentIds: parentId });
|
||||
expect(children.status).toBe(200);
|
||||
expect(children.body.map((row: { id: string }) => row.id)).toEqual([childId]);
|
||||
}
|
||||
const preview = await request(app).get(`/api/companies/${companyId}/issues`).query({ view: "index", parentIds: parentId, oneChildPerParent: true }).expect(200);
|
||||
expect(preview.body.map((row: { id: string }) => row.id)).toEqual([childId]);
|
||||
for (const query of [
|
||||
{ view: "index", ids: "invalid" },
|
||||
{ view: "index", parentIds: Array.from({ length: 101 }, () => randomUUID()).join(",") },
|
||||
{ view: "index", ids: parentId, attention: "blocked" },
|
||||
{ view: "compact", ids: parentId },
|
||||
{ view: "index", oneChildPerParent: true },
|
||||
{ view: "rows", parentIds: parentId, oneChildPerParent: true },
|
||||
{ view: "index", parentIds: parentId, oneChildPerParent: "invalid" },
|
||||
]) {
|
||||
expect((await request(app).get(`/api/companies/${companyId}/issues`).query(query)).status).toBe(400);
|
||||
}
|
||||
} finally { vi.unstubAllEnvs(); }
|
||||
});
|
||||
|
||||
it("returns one child per parent without letting a large collapsed tree fill the page", async () => {
|
||||
const companyId = randomUUID(), firstParentId = randomUUID(), secondParentId = randomUUID();
|
||||
await db.insert(companies).values({ id: companyId, name: "Collapsed trees", issuePrefix: uniqueIssuePrefix() });
|
||||
await seedCloudTenantMember(companyId);
|
||||
await db.insert(issues).values([
|
||||
{ id: firstParentId, companyId, title: "First parent" },
|
||||
{ id: secondParentId, companyId, title: "Second parent" },
|
||||
]);
|
||||
await db.insert(issues).values([
|
||||
...Array.from({ length: 120 }, (_, index) => ({ companyId, parentId: firstParentId, title: `Child ${index}` })),
|
||||
{ companyId, parentId: secondParentId, title: "Other child" },
|
||||
]);
|
||||
const response = await request(createApp(companyId)).get(`/api/companies/${companyId}/issues`)
|
||||
.query({ view: "index", parentIds: [firstParentId, secondParentId].join(","), oneChildPerParent: true, limit: 2 }).expect(200);
|
||||
expect(response.body).toHaveLength(2);
|
||||
expect(new Set(response.body.map((row: { parentId: string }) => row.parentId))).toEqual(new Set([firstParentId, secondParentId]));
|
||||
});
|
||||
|
||||
it("returns only unassigned issues for assigneeAgentId=null", async () => {
|
||||
const companyId = randomUUID();
|
||||
const assigneeAgentId = randomUUID();
|
||||
@@ -240,6 +314,15 @@ describeEmbeddedPostgres("issue list routes assigneeAgentId filter", () => {
|
||||
assigneeAgentId: ownerAgentId,
|
||||
},
|
||||
});
|
||||
const index = await request(app).get(`/api/companies/${companyId}/issues`).query({ view: "index", limit: "20" });
|
||||
expect(index.status).toBe(200);
|
||||
expect(index.body).toHaveLength(1);
|
||||
expect(index.body[0]).toMatchObject({ id: issueId, listProjection: "index", description: null, activeRecoveryAction: null, successfulRunHandoff: null });
|
||||
expect(index.headers.etag).not.toBe(res.headers.etag);
|
||||
const detailRows = await request(app).get(`/api/companies/${companyId}/issues`).query({ view: "rows", ids: issueId });
|
||||
expect(detailRows.status).toBe(200);
|
||||
expect(detailRows.body[0]).toMatchObject({ id: issueId, listProjection: "rows", description: null, activeRecoveryAction: { id: recoveryAction.id } });
|
||||
|
||||
expect(res.body[0]).not.toHaveProperty("workProducts");
|
||||
expect(res.body[0]).not.toHaveProperty("project");
|
||||
expect(res.body[0]).not.toHaveProperty("goal");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { latestFailedRunsByAgent } from "@paperclipai/shared";
|
||||
import { prepareRuntimeSettingsPatch } from "../services/agent-runtime-settings.js";
|
||||
import { completeSetupTokenAiConnection } from "../services/setup-token-ai-connection.js";
|
||||
import { cancellationRequestId } from "../services/native-runtime/native-cancellation-request.js";
|
||||
@@ -6910,7 +6911,8 @@ export function agentRoutes(
|
||||
? Math.max(1, Math.min(HEARTBEAT_RUN_LIST_MAX_LIMIT, parseInt(limitParam, 10) || HEARTBEAT_RUN_LIST_DEFAULT_LIMIT))
|
||||
: HEARTBEAT_RUN_LIST_DEFAULT_LIMIT;
|
||||
const summary = req.query.summary === "true" || req.query.summary === "1";
|
||||
const runs = await heartbeat.list(companyId, agentId, limit, { summary });
|
||||
const recentRuns = await heartbeat.list(companyId, agentId, limit, { summary });
|
||||
const runs = req.query.latestFailuresOnly === "true" ? latestFailedRunsByAgent(recentRuns) : recentRuns;
|
||||
const readDecisions = await getHeartbeatRunReadDecisions(db, access, req.actor, runs);
|
||||
res.json(await runRedactions.redactForRuns(companyId, runs.map((run, index) =>
|
||||
readDecisions[index] ? run : redactHeartbeatRunListRow(run)),
|
||||
|
||||
@@ -8354,7 +8354,26 @@ export function issueRoutes(
|
||||
const sortField = req.query.sortField as string | undefined;
|
||||
const sortDir = req.query.sortDir as string | undefined;
|
||||
const view = req.query.view as string | undefined;
|
||||
const compactView = view === "compact";
|
||||
const compactView = view === "compact" || view === "index" || view === "rows";
|
||||
const issueIds = typeof req.query.ids === "string" ? [...new Set(req.query.ids.split(","))] : undefined;
|
||||
const parentIds = typeof req.query.parentIds === "string" ? [...new Set(req.query.parentIds.split(","))] : undefined;
|
||||
const oneChildPerParent = parseOptionalBooleanQuery(req.query.oneChildPerParent);
|
||||
if (oneChildPerParent === null || (oneChildPerParent && (view !== "index" || !parentIds))) {
|
||||
res.status(400).json({ error: "oneChildPerParent must be a boolean and requires view=index with parentIds" });
|
||||
return;
|
||||
}
|
||||
if (req.query.ids !== undefined && (!issueIds?.length || issueIds.length > 100 || issueIds.some((id) => !z.string().uuid().safeParse(id).success))) {
|
||||
res.status(400).json({ error: "ids must contain one to 100 comma-separated UUIDs" });
|
||||
return;
|
||||
}
|
||||
if (req.query.parentIds !== undefined && (!parentIds?.length || parentIds.length > 100 || parentIds.some((id) => !z.string().uuid().safeParse(id).success))) {
|
||||
res.status(400).json({ error: "parentIds must contain one to 100 comma-separated UUIDs" });
|
||||
return;
|
||||
}
|
||||
if ((issueIds || parentIds) && (attention || (view !== "index" && view !== "rows"))) {
|
||||
res.status(400).json({ error: "ids and parentIds require view=index or view=rows without attention" });
|
||||
return;
|
||||
}
|
||||
const hasPlanDocument = parseOptionalBooleanQuery(
|
||||
req.query.hasPlanDocument,
|
||||
);
|
||||
@@ -8421,8 +8440,8 @@ export function issueRoutes(
|
||||
.json({ error: "attention must be 'blocked' when provided" });
|
||||
return;
|
||||
}
|
||||
if (view !== undefined && view !== "compact") {
|
||||
res.status(400).json({ error: "view must be 'compact' when provided" });
|
||||
if (view !== undefined && !["compact", "index", "rows"].includes(view)) {
|
||||
res.status(400).json({ error: "view must be compact, index, or rows when provided" });
|
||||
return;
|
||||
}
|
||||
if (
|
||||
@@ -8509,6 +8528,10 @@ export function issueRoutes(
|
||||
const offset = parsedOffset ?? 0;
|
||||
|
||||
const listFilters: IssueFilters = {
|
||||
issueIds,
|
||||
parentIds,
|
||||
oneChildPerParent,
|
||||
listProjection: view === "index" || view === "rows" ? view : undefined,
|
||||
readCondition: await issueReadSqlCondition(db, req.actor),
|
||||
attention: attention === "blocked" ? "blocked" : undefined,
|
||||
status: req.query.status as string | string[] | undefined,
|
||||
@@ -8564,14 +8587,16 @@ export function issueRoutes(
|
||||
companyId,
|
||||
normalizedQuery: {
|
||||
...requestKeyFilters,
|
||||
view: compactView ? "compact" : undefined,
|
||||
view,
|
||||
},
|
||||
});
|
||||
const coordinated = await coordinateIssueListGet({
|
||||
req,
|
||||
companyId,
|
||||
requestKey,
|
||||
allowTtlCache: compactView,
|
||||
// The new collection projections must reflect read/archive/undo mutations
|
||||
// immediately. Keep in-flight coalescing and ETags, without a stale TTL body.
|
||||
allowTtlCache: view === "compact",
|
||||
diagnostics: opts.issueListDiagnostics,
|
||||
compute: async () => {
|
||||
const rawResult = await svc.list(companyId, listFilters);
|
||||
@@ -8579,6 +8604,10 @@ export function issueRoutes(
|
||||
? rawResult
|
||||
: await filterIssuesForActor(req, rawResult);
|
||||
const result = await redactIssueRelationEdgesOnRows(req, companyId, visibleRows);
|
||||
if (view === "index") {
|
||||
const body = result.map((issue) => ({ ...toCompactIssue(issue), listProjection: "index" as const }));
|
||||
return { kind: "compact", body, etag: compactIssueListEtag(body), cacheControl: "private, must-revalidate" };
|
||||
}
|
||||
const issueIds = result.map((issue) => issue.id);
|
||||
const interruptions = await taskInterruptionPresentations(db, companyId, result);
|
||||
if (compactView) {
|
||||
@@ -8603,12 +8632,12 @@ export function issueRoutes(
|
||||
}),
|
||||
);
|
||||
const compactResult = result.map((issue) =>
|
||||
toCompactIssue({
|
||||
({ ...toCompactIssue({
|
||||
...issue,
|
||||
activeRecoveryAction: recoveryActionByIssue.get(issue.id) ?? null,
|
||||
interruption: interruptions.get(issue.id) ?? null,
|
||||
successfulRunHandoff: handoffStates.get(issue.id) ?? null,
|
||||
}),
|
||||
}), ...(view === "rows" ? { listProjection: "rows" as const } : {}) }),
|
||||
);
|
||||
return {
|
||||
kind: "compact",
|
||||
|
||||
@@ -4269,10 +4269,15 @@ registry.registerPath({
|
||||
tags: ["issues"],
|
||||
summary: "List issues in a company",
|
||||
description:
|
||||
"Use `view=compact` for the board issue-list row contract. The default response remains the broad compatibility contract.",
|
||||
"Use view=index for collection membership/order without task bodies or diagnostic enrichment; view=rows enriches visible rows without task bodies. These responses include listProjection and cannot stand in for a full issue. view=compact retains the board row compatibility contract. The default response remains the broad compatibility contract. All projections enforce company and task access.",
|
||||
request: {
|
||||
params: z.object({ companyId: z.string() }),
|
||||
query: z.object({ view: z.enum(["compact"]).optional() }).passthrough(),
|
||||
query: z.object({
|
||||
view: z.enum(["compact", "index", "rows"]).optional(),
|
||||
ids: z.string().optional().describe("One to 100 comma-separated task UUIDs; requires index or rows without attention."),
|
||||
parentIds: z.string().optional().describe("One to 100 comma-separated parent UUIDs; requires index or rows without attention."),
|
||||
oneChildPerParent: z.boolean().optional().describe("Return at most one readable child per parent; requires view=index and parentIds."),
|
||||
}).passthrough(),
|
||||
},
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
@@ -7477,6 +7482,7 @@ registry.registerPath({
|
||||
agentId: z.string().optional(),
|
||||
limit: z.coerce.number().int().min(1).max(1000).default(200),
|
||||
summary: z.enum(["true", "false", "1", "0"]).optional(),
|
||||
latestFailuresOnly: z.enum(["true", "false"]).optional().describe("Return failures only when the agent's latest run within the bounded result failed or timed out. Applied before read redaction."),
|
||||
}),
|
||||
},
|
||||
responses: { 200: r.ok(), 401: r.unauthorized },
|
||||
|
||||
@@ -6,6 +6,8 @@ import { sidebarBadgeService } from "../services/sidebar-badges.js";
|
||||
import { accessService } from "../services/access.js";
|
||||
import { collapseDuplicatePendingHumanJoinRequests } from "../lib/join-request-dedupe.js";
|
||||
import { assertCompanyAccess } from "./authz.js";
|
||||
import { issueService } from "../services/issues.js";
|
||||
import { approvalReadSqlCondition, issueReadSqlCondition } from "../services/authorization.js";
|
||||
|
||||
function buildDismissedAtByKey(
|
||||
dismissals: Array<{ itemKey: string; kind: string; dismissedAt: Date | string; snoozedUntil: Date | string | null }>,
|
||||
@@ -27,6 +29,7 @@ export function sidebarBadgeRoutes(db: Db) {
|
||||
const router = Router();
|
||||
const svc = sidebarBadgeService(db);
|
||||
const access = accessService(db);
|
||||
const issues = issueService(db);
|
||||
|
||||
router.get("/companies/:companyId/sidebar-badges", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
@@ -77,15 +80,24 @@ export function sidebarBadgeRoutes(db: Db) {
|
||||
: new Map<string, number>();
|
||||
|
||||
// Company health alerts belong in All, not the personal inbox badge.
|
||||
// These IDs reconcile optimistic archives in the board UI. An agent's
|
||||
// responsible user does not grant it access to that user's task collection.
|
||||
const currentUserId = req.actor.type === "board" ? req.actor.userId ?? null : null;
|
||||
const unreadIssueIds = currentUserId
|
||||
? await issues.unreadInboxIssueIds(companyId, currentUserId, await issueReadSqlCondition(db, req.actor))
|
||||
: [];
|
||||
const badges = await svc.get(companyId, {
|
||||
currentUserId: req.actor.type === "board"
|
||||
? req.actor.userId ?? null
|
||||
: req.actor.onBehalfOfUserId ?? null,
|
||||
dismissals: dismissedAtByKey,
|
||||
joinRequests: visibleJoinRequests,
|
||||
unreadTouchedIssues: unreadIssueIds.length,
|
||||
approvalReadCondition: await approvalReadSqlCondition(db, req.actor),
|
||||
actor: req.actor,
|
||||
});
|
||||
|
||||
res.json(badges);
|
||||
res.json({ ...badges, unreadIssueIds });
|
||||
});
|
||||
|
||||
return router;
|
||||
|
||||
@@ -1876,6 +1876,10 @@ export interface IssueFilters {
|
||||
*/
|
||||
assigneeAgentId?: string | null;
|
||||
participantAgentId?: string;
|
||||
issueIds?: string[];
|
||||
listProjection?: "index" | "rows";
|
||||
parentIds?: string[];
|
||||
oneChildPerParent?: boolean;
|
||||
assigneeUserId?: string;
|
||||
touchedByUserId?: string;
|
||||
inboxArchivedByUserId?: string;
|
||||
@@ -8038,6 +8042,32 @@ export function issueService(db: Db) {
|
||||
clearCheckoutRunIfTerminal,
|
||||
addStopRelayCommentIfNeeded,
|
||||
|
||||
unreadInboxIssueIds: async (companyId: string, userId: string, readCondition: SQL<boolean>) => {
|
||||
// Rank the visible Mine set before counting unread, just like the inbox.
|
||||
// This projection never reads descriptions, run payloads, or recovery details.
|
||||
const rows = await db.select({
|
||||
id: issues.id,
|
||||
}).from(issues).where(and(
|
||||
eq(issues.companyId, companyId),
|
||||
visibleIssueCondition(),
|
||||
isNull(issues.conversationAgentId),
|
||||
nonPluginOperationIssueCondition(),
|
||||
ne(issues.status, "cancelled"),
|
||||
touchedByUserCondition(companyId, userId),
|
||||
inboxVisibleForUserCondition(companyId, userId),
|
||||
readCondition,
|
||||
)).orderBy(desc(issueCanonicalLastActivityAtExpr(companyId)), desc(issues.updatedAt), desc(issues.id)).limit(100);
|
||||
if (rows.length === 0) return [];
|
||||
// Keep correlated predicates in WHERE (as in the list's user stats).
|
||||
// A single-table Drizzle SELECT projection unqualifies nested columns.
|
||||
return db.select({ id: issues.id }).from(issues).where(and(
|
||||
eq(issues.companyId, companyId),
|
||||
inArray(issues.id, rows.map((row) => row.id)),
|
||||
unreadForUserCondition(companyId, userId),
|
||||
readCondition,
|
||||
)).then((unread) => unread.map((row) => row.id));
|
||||
},
|
||||
|
||||
list: async (companyId: string, filters?: IssueFilters) => {
|
||||
if (filters?.sortField === "id" && filters.attention) {
|
||||
throw unprocessable("ID ordering is not supported for blocked attention lists");
|
||||
@@ -8060,7 +8090,15 @@ export function issueService(db: Db) {
|
||||
eq(issues.companyId, companyId),
|
||||
visibleIssueCondition(),
|
||||
];
|
||||
if (!filters?.q?.trim()) {
|
||||
if (filters?.issueIds) {
|
||||
if (filters.issueIds.length === 0) return [];
|
||||
conditions.push(inArray(issues.id, filters.issueIds));
|
||||
}
|
||||
if (filters?.parentIds) {
|
||||
if (filters.parentIds.length === 0) return [];
|
||||
conditions.push(inArray(issues.parentId, filters.parentIds));
|
||||
}
|
||||
if (!filters?.q?.trim() && !filters?.issueIds) {
|
||||
conditions.push(isNull(issues.conversationAgentId));
|
||||
if (!filters?.touchedByUserId && !filters?.unreadForUserId && !filters?.inboxArchivedByUserId) {
|
||||
conditions.push(nonIdleSlackIssueCondition());
|
||||
@@ -8218,7 +8256,14 @@ export function issueService(db: Db) {
|
||||
}
|
||||
const priorityOrder = sql`CASE ${issues.priority} WHEN 'critical' THEN 0 WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 ELSE 4 END`;
|
||||
const searchOrder = sql<number>`-task_search.score`;
|
||||
const issueSource = db.select(issueListSelect).from(issues);
|
||||
const selectedFields = filters?.listProjection ? {
|
||||
...issueListSelect,
|
||||
description: sql<string | null>`NULL`,
|
||||
descriptionTruncated: sql<boolean>`false`,
|
||||
} : issueListSelect;
|
||||
const issueSource = (filters?.oneChildPerParent
|
||||
? db.selectDistinctOn([issues.parentId], selectedFields)
|
||||
: db.select(selectedFields)).from(issues);
|
||||
const searchedSource = hasSearch
|
||||
? issueSource.innerJoin(sql`(
|
||||
${taskSearchCtes(companyId, taskSearch, true, and(...conditions), filters?.readCondition)}
|
||||
@@ -8228,6 +8273,7 @@ export function issueService(db: Db) {
|
||||
const baseQuery = searchedSource
|
||||
.where(and(...conditions))
|
||||
.orderBy(
|
||||
...(filters?.oneChildPerParent ? [asc(issues.parentId)] : []),
|
||||
...issueListOrderBy(companyId, {
|
||||
hasSearch,
|
||||
priorityOrder,
|
||||
@@ -8252,7 +8298,7 @@ export function issueService(db: Db) {
|
||||
),
|
||||
}));
|
||||
const withLabels = await withIssueLabels(db, rows);
|
||||
const runMap = await activeRunMapForIssues(db, withLabels);
|
||||
const runMap = filters?.listProjection === "index" ? new Map() : await activeRunMapForIssues(db, withLabels);
|
||||
const withRuns = withActiveRuns(withLabels, runMap);
|
||||
if (withRuns.length === 0) {
|
||||
return withRuns;
|
||||
@@ -8298,8 +8344,8 @@ export function issueService(db: Db) {
|
||||
reviewAttentionByIssueId,
|
||||
blockedInboxAttentionByIssueId,
|
||||
] = await Promise.all([
|
||||
listIssueBlockerAttentionMap(db, companyId, withRuns),
|
||||
listIssueReviewAttentionMap(db, companyId, withRuns),
|
||||
filters?.listProjection === "index" ? Promise.resolve(new Map<string, IssueBlockerAttention>()) : listIssueBlockerAttentionMap(db, companyId, withRuns),
|
||||
filters?.listProjection === "index" ? Promise.resolve(new Map<string, IssueReviewAttention>()) : listIssueReviewAttentionMap(db, companyId, withRuns),
|
||||
includeBlockedInboxAttention
|
||||
? listIssueBlockedInboxAttentionMap(db, companyId, withRuns)
|
||||
: Promise.resolve(new Map<string, IssueBlockedInboxAttention>()),
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { and, desc, eq, inArray, not } from "drizzle-orm";
|
||||
import { and, desc, eq, inArray, not, type SQL } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { agents, approvals, heartbeatRuns } from "@paperclipai/db";
|
||||
import { isHeartbeatRunVisibleInMine, type SidebarBadges } from "@paperclipai/shared";
|
||||
import { isHeartbeatRunVisibleInMine, latestFailedRunsByAgent, type SidebarBadges } from "@paperclipai/shared";
|
||||
import { authorizationService, type AuthorizationActor } from "./authorization.js";
|
||||
import { getHeartbeatRunReadDecisions } from "./heartbeat-run-privacy.js";
|
||||
|
||||
const ACTIONABLE_APPROVAL_STATUSES = ["pending", "revision_requested"];
|
||||
const FAILED_HEARTBEAT_STATUSES = ["failed", "timed_out"];
|
||||
|
||||
function normalizeTimestamp(value: Date | string | null | undefined): number {
|
||||
if (!value) return 0;
|
||||
@@ -23,6 +24,7 @@ function isDismissed(
|
||||
}
|
||||
|
||||
export function sidebarBadgeService(db: Db) {
|
||||
const authorization = authorizationService(db);
|
||||
return {
|
||||
get: async (
|
||||
companyId: string,
|
||||
@@ -31,6 +33,8 @@ export function sidebarBadgeService(db: Db) {
|
||||
dismissals?: ReadonlyMap<string, number>;
|
||||
joinRequests?: Array<{ id: string; updatedAt: Date | string | null; createdAt: Date | string }>;
|
||||
unreadTouchedIssues?: number;
|
||||
approvalReadCondition?: SQL<boolean>;
|
||||
actor?: AuthorizationActor;
|
||||
},
|
||||
): Promise<SidebarBadges> => {
|
||||
const actionableApprovals = await db
|
||||
@@ -40,16 +44,21 @@ export function sidebarBadgeService(db: Db) {
|
||||
and(
|
||||
eq(approvals.companyId, companyId),
|
||||
inArray(approvals.status, ACTIONABLE_APPROVAL_STATUSES),
|
||||
extra?.approvalReadCondition,
|
||||
),
|
||||
)
|
||||
.then((rows) =>
|
||||
rows.filter((row) => !isDismissed(extra?.dismissals ?? new Map(), `approval:${row.id}`, row.updatedAt)).length
|
||||
);
|
||||
|
||||
const latestRunByAgent = await db
|
||||
.selectDistinctOn([heartbeatRuns.agentId], {
|
||||
const recentRuns = await db
|
||||
.select({
|
||||
id: heartbeatRuns.id,
|
||||
runStatus: heartbeatRuns.status,
|
||||
agentId: heartbeatRuns.agentId,
|
||||
companyId: heartbeatRuns.companyId,
|
||||
scopeKind: heartbeatRuns.scopeKind,
|
||||
issueId: heartbeatRuns.issueId,
|
||||
status: heartbeatRuns.status,
|
||||
responsibleUserId: heartbeatRuns.responsibleUserId,
|
||||
createdAt: heartbeatRuns.createdAt,
|
||||
})
|
||||
@@ -62,13 +71,16 @@ export function sidebarBadgeService(db: Db) {
|
||||
not(eq(agents.status, "terminated")),
|
||||
),
|
||||
)
|
||||
.orderBy(heartbeatRuns.agentId, desc(heartbeatRuns.createdAt));
|
||||
.orderBy(desc(heartbeatRuns.createdAt))
|
||||
.limit(200);
|
||||
|
||||
const failedRuns = latestRunByAgent.filter((row) =>
|
||||
FAILED_HEARTBEAT_STATUSES.includes(row.runStatus)
|
||||
&& (extra?.currentUserId === undefined || isHeartbeatRunVisibleInMine(row, extra.currentUserId))
|
||||
const failures = latestFailedRunsByAgent(recentRuns).filter((row) =>
|
||||
(extra?.currentUserId === undefined || isHeartbeatRunVisibleInMine(row, extra.currentUserId))
|
||||
&& !isDismissed(extra?.dismissals ?? new Map(), `run:${row.id}`, row.createdAt),
|
||||
).length;
|
||||
);
|
||||
const failedRuns = extra?.actor
|
||||
? (await getHeartbeatRunReadDecisions(db, authorization, extra.actor, failures)).filter(Boolean).length
|
||||
: failures.length;
|
||||
|
||||
const joinRequests = (extra?.joinRequests ?? []).filter((row) =>
|
||||
!isDismissed(
|
||||
|
||||
Reference in new issue
Block a user