From a4a0668a5ce0c80012924e49eabb8c9c92bc75c1 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:39:21 -0600 Subject: [PATCH] perf(attention): count the sidebar summary without the reads that only a card shows (#21159) GET /attention/summary returns counts. To get them it ran the feed with a flag that skipped card bodies, and still read every blocked task through the full task list, every review two more times (one time with its description), the blocker and the blocked work of each card, and the spend of every budget policy. The summary keeps the collection path of the feed, so a card is counted by the rule that shows it. It no longer reads what only a card shows: - blocked work: the rows of the task list, in list order, with only the columns blocker attention reads (issueService.listBlockerAttentionRoots), classified by the function the full list calls - reviews: the review rows carry the card's task condition, so the summary does not read each review again - tasks of cards: without project and workspace - budget alerts: the open incidents (budgetService.openIncidents), not the spend of every policy - a fingerprint only for a card that has a dismissal; no queue membership and no "transferred by" read The review classification reads the 20 columns it uses, not the whole task. The request makes the same writes as before (decision queue seeds, retention states) through unchanged code. The feed, the task index and the task list return what they returned before. One summary request at 3,000 tasks: 133 -> 78 statements, 2,718 -> 842 KB fetched, 604 -> 144 KB of SQL text, 100 -> 45 MB allocated. Co-Authored-By: Claude Fable 5.2 --- server/scripts/benchmark-review-attention.ts | 79 ++-- .../attention-summary-counts.test.ts | 391 ++++++++++++++++++ .../helpers/attention-summary-fixture.ts | 362 ++++++++++++++++ .../helpers/database-statement-meter.ts | 90 ++++ server/src/services/attention.ts | 154 +++++-- server/src/services/budgets.ts | 20 +- server/src/services/issues.ts | 71 +++- 7 files changed, 1085 insertions(+), 82 deletions(-) create mode 100644 server/src/__tests__/attention-summary-counts.test.ts create mode 100644 server/src/__tests__/helpers/attention-summary-fixture.ts create mode 100644 server/src/__tests__/helpers/database-statement-meter.ts diff --git a/server/scripts/benchmark-review-attention.ts b/server/scripts/benchmark-review-attention.ts index f8af18a08c1..696757767b7 100644 --- a/server/scripts/benchmark-review-attention.ts +++ b/server/scripts/benchmark-review-attention.ts @@ -6,20 +6,24 @@ * GET /api/companies/:companyId/issues (the dashboard task list) * * For each request and each fixture size the script reports the statements - * sent, the server time, the process CPU time, the time the main thread was - * busy, the bytes allocated, and where a CPU profile of the handler puts the - * time. With --baseline-ref it runs the pre-fix modules in a second process - * against the same database and fails unless both versions return the same - * summary counts, the same attention feed, the same task list and the same - * review attention for every task in review. + * and transactions sent, the rows and the bytes that the database returned, + * the server time, the process CPU time, the time the main thread was busy, + * the bytes allocated, and where a CPU profile of the handler puts the time. + * With --baseline-ref it runs the pre-fix modules in a second process against + * the same database and fails unless both versions return the same summary + * counts, the same attention feed, the same task index, the same task list + * and the same review attention for every task in review. * * pnpm --filter @paperclipai/server exec tsx scripts/benchmark-review-attention.ts \ - * --baseline-ref= --tasks=3000,9000 --agents=60 + * --baseline-ref= --tasks=3000,9000 --agents=60 --company-sources * * Options: --tasks (comma-separated fixture sizes, default 3000,9000), - * --agents (default 60), --rounds (default 3: each version runs in a new - * process once per round, in alternating order), --samples (requests measured - * per round, default 5), --cpu-profile-dir (keep the .cpuprofile files). + * --agents (default 60), --company-sources (add what the review fixture leaves + * out: budget policies, pull requests, plans, questions and failed runs), + * --rounds (default 3: each version runs in a new process once per round, in + * alternating order), --samples (requests measured per round, default 5), + * --cpu-profile-dir (keep the .cpuprofile files), --statement-log-dir (write + * every statement of one more request of each kind). */ import assert from "node:assert/strict"; import { execFileSync, spawn } from "node:child_process"; @@ -35,6 +39,8 @@ import { Session } from "node:inspector/promises"; import express from "express"; import { and, desc, eq } from "drizzle-orm"; import { createDb, issues } from "@paperclipai/db"; +import { meterDatabaseStatements } from "../src/__tests__/helpers/database-statement-meter.js"; +import { seedAttentionSummaryVolume } from "../src/__tests__/helpers/attention-summary-fixture.js"; import { startEmbeddedPostgresTestDatabase } from "../src/__tests__/helpers/embedded-postgres.js"; import { seedReviewAttentionFixture } from "../src/__tests__/helpers/review-attention-fixture.js"; @@ -44,13 +50,17 @@ const scriptPath = fileURLToPath(import.meta.url); const git = (...args: string[]) => execFileSync("git", args, { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); // The modules of the fix. The baseline process loads their pre-fix source; // every other module is the current tree in both processes. -const modules = ["server/src/services/issues.ts", "server/src/services/recovery/issue-graph-liveness.ts"]; -// Functions of the review-attention read path, reported from the CPU profile -// with the time spent in them and everything they call. +const modules = ["server/src/services/issues.ts", "server/src/services/recovery/issue-graph-liveness.ts", + "server/src/services/attention.ts", "server/src/services/budgets.ts"]; +// Functions of the attention read path, reported from the CPU profile with the +// time spent in them and everything they call. const profiledFunctions = ["listIssueReviewAttentionMap", "classifyIssueGraphLiveness", "classifyIssueReviewPaths", "reviewFinding", "ownerCandidatesForRecoveryIssue", "orderedInvokableAgents", "isInvokableAgent", "getAgentWorkEligibility", "getAgentOrgChainHealth", "listIssueBlockerAttentionMap", "listIssueBlockedInboxAttentionMap", - "filterIssuesForActor", "enrichAttentionItems"]; + "filterIssuesForActor", "enrichAttentionItems", "readIssueBlockerAttentionBatch", "issueSummaryMap", "blockingIssueMap", + "blockedWorkCountMap", "resolveIssueAudienceContextMap", "syncItems", "materializeSeedSources", "withIssueLabels", + "lastActivityStatsForIssues", "blockedByMapForIssues", "decodeDatabaseTextPreview", "buildPolicySummary", + "attentionChangeFingerprint"]; const round = (value: number, digits = 1) => Number(value.toFixed(digits)); const median = (values: number[]) => [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)]!; // Bytes this process has allocated since it started, also the collected ones. @@ -112,14 +122,7 @@ async function runArm() { const { issueService } = await import("../src/services/issues.js"); const companyId = arg("company")!, userId = arg("user")!, samples = Number(arg("samples") ?? 5); const db = createDb(arg("database-url")!); - const client = db.$client as unknown as { unsafe: (query: string, ...rest: unknown[]) => unknown }; - const unsafe = client.unsafe; - let statements = 0, sqlBytes = 0; - client.unsafe = function (this: unknown, query: string, ...rest: unknown[]) { - statements += 1; - sqlBytes += Buffer.byteLength(query); - return unsafe.call(this, query, ...rest); - }; + const meter = meterDatabaseStatements(db, arg("database-url")!); let serverMs = 0; const app = express(); app.use((req, res, next) => { @@ -150,14 +153,24 @@ async function runArm() { const measured: Array> = []; for (let sample = 0; sample < samples; sample++) { globalThis.gc?.(); - statements = 0; sqlBytes = 0; + const database = meter.read(); const cpu = process.cpuUsage(), loop = performance.eventLoopUtilization(); const allocated = allocatedBytes(), started = performance.now(); await get(route); - const used = process.cpuUsage(cpu); + const used = process.cpuUsage(cpu), sent = meter.read(); measured.push({ wallMs: performance.now() - started, serverMs, cpuMs: (used.user + used.system) / 1_000, mainThreadBusyMs: performance.eventLoopUtilization(loop).active, - allocatedMb: (allocatedBytes() - allocated) / 1_048_576, statements, sqlBytes }); + allocatedMb: (allocatedBytes() - allocated) / 1_048_576, statements: sent.statements - database.statements, + transactions: sent.transactions - database.transactions, waits: sent.waits - database.waits, + rows: sent.rows - database.rows, + sqlBytes: sent.sqlBytes - database.sqlBytes, fetchedBytes: sent.bytesReceived - database.bytesReceived }); + } + if (arg("statement-log")) { + meter.startLog(); + await get(route); + await fs.writeFile(`${arg("statement-log")}.${name}.json`, JSON.stringify(meter.stopLog().map(statement => ({ + ...statement, ms: round(statement.ms), sql: statement.sql.replace(/\s+/g, " ").slice(0, 400), sqlBytes: Buffer.byteLength(statement.sql), + })), null, 1)); } const profiler = new Session(); profiler.connect(); @@ -174,6 +187,7 @@ async function runArm() { // What both versions must agree on. const summary = JSON.parse((await get(requests.summary)).toString("utf8")); const feed = JSON.parse((await get("/attention?all=true")).toString("utf8")); + const taskIndex = JSON.parse((await get("/attention/tasks")).toString("utf8")); const taskList = JSON.parse((await get(requests.taskList)).toString("utf8")) as Array<{ status: string; reviewAttention?: { state: string } }>; const reviewRows = await db.select().from(issues) .where(and(eq(issues.companyId, companyId), eq(issues.status, "in_review"))).orderBy(desc(issues.id)); @@ -186,7 +200,8 @@ async function runArm() { taskListRows: taskList.length, taskListReviews: taskList.filter(row => row.status === "in_review").length, reviewAttentionStates: states }, digests: { summary: digest({ ...summary, generatedAt: null }), feed: digest({ ...feed, generatedAt: null }), - taskList: digest(taskList), reviewAttention: digest(reviewAttention) }, + taskIndex: digest({ ...taskIndex, generatedAt: null }), taskList: digest(taskList), + reviewAttention: digest(reviewAttention) }, })}`); } finally { await new Promise(resolve => server.close(resolve)); @@ -236,6 +251,7 @@ register(new URL("./hooks.mjs", import.meta.url), { data: ${JSON.stringify(redir // A second company with the same shapes, which no request may read. await seedReviewAttentionFixture(db, { tasks: Math.ceil(tasks / 5), agents: agentCount, prefix: "OTH" }); const fixture = await seedReviewAttentionFixture(db, { tasks, agents: agentCount, prefix: "SYN" }); + const companySources = process.argv.includes("--company-sources") ? await seedAttentionSummaryVolume(db, fixture) : null; await db.$client.unsafe("vacuum analyze"); await db.$client.end({ timeout: 0 }); const seedMs = Math.round(performance.now() - seedStarted); @@ -250,7 +266,8 @@ register(new URL("./hooks.mjs", import.meta.url), { data: ${JSON.stringify(redir ...(arm === "baseline" ? ["--import", pathToFileURL(path.join(baselineDir!, "register.mjs")).href] : []), scriptPath, `--arm=${arm}`, `--database-url=${temporary.connectionString}`, `--company=${fixture.companyId}`, `--user=${fixture.userId}`, `--samples=${samples}`, - ...(arg("cpu-profile-dir") ? [`--cpu-profile=${path.join(arg("cpu-profile-dir")!, `${arm}-${tasks}`)}`] : [])], + ...(arg("cpu-profile-dir") ? [`--cpu-profile=${path.join(arg("cpu-profile-dir")!, `${arm}-${tasks}`)}`] : []), + ...(arg("statement-log-dir") ? [`--statement-log=${path.join(arg("statement-log-dir")!, `${arm}-${tasks}`)}`] : [])], { cwd: path.join(root, "server"), stdio: ["ignore", "pipe", "inherit"], env: { ...process.env, NODE_ENV: "production" } }); let stdout = ""; child.stdout.on("data", chunk => { stdout += chunk; }); @@ -265,7 +282,7 @@ register(new URL("./hooks.mjs", import.meta.url), { data: ${JSON.stringify(redir for (const output of Object.values(byArm).flat()) { assert.deepEqual(output.shape, first.shape, "Every run of both versions must return the same counts"); assert.deepEqual(output.digests, first.digests, - "Every run of both versions must return the same summary, feed, task list and review attention"); + "Every run of both versions must return the same summary, feed, task index, task list and review attention"); } // The samples of every round of a version, and the profile of its last round. const summarize = (outputs: ArmOutput[]) => Object.fromEntries(Object.keys(first.results).map(name => { @@ -274,7 +291,9 @@ register(new URL("./hooks.mjs", import.meta.url), { data: ${JSON.stringify(redir const last = outputs.at(-1)!.results[name]!; return [name, { route: last.route, responseBytes: last.responseBytes, statements: median(field("statements")), - sqlTextBytes: median(field("sqlBytes")), + transactions: median(field("transactions")), databaseWaits: median(field("waits")), + rowsFetched: median(field("rows")), + fetchedBytes: median(field("fetchedBytes")), sqlTextBytes: median(field("sqlBytes")), serverMs: { median: round(median(field("serverMs"))), min: round(Math.min(...field("serverMs"))) }, cpuMs: { median: round(median(field("cpuMs"))), min: round(Math.min(...field("cpuMs"))) }, mainThreadBusyMs: { median: round(median(field("mainThreadBusyMs"))), min: round(Math.min(...field("mainThreadBusyMs"))) }, @@ -282,7 +301,7 @@ register(new URL("./hooks.mjs", import.meta.url), { data: ${JSON.stringify(redir }]; })); report[`tasks=${tasks}`] = { - fixture: { synthetic: true, ...fixture.counts, otherCompanyTasks: Math.ceil(tasks / 5), seedMs }, + fixture: { synthetic: true, ...fixture.counts, companySources, otherCompanyTasks: Math.ceil(tasks / 5), seedMs }, shape: first.shape, identicalResponses: byArm.baseline ? Object.keys(first.digests) : "no baseline", ...Object.fromEntries(Object.entries(byArm).map(([arm, outputs]) => [arm, summarize(outputs)])), diff --git a/server/src/__tests__/attention-summary-counts.test.ts b/server/src/__tests__/attention-summary-counts.test.ts new file mode 100644 index 00000000000..ef6e857f411 --- /dev/null +++ b/server/src/__tests__/attention-summary-counts.test.ts @@ -0,0 +1,391 @@ +import express from "express"; +import request from "supertest"; +import { and, eq, sql } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + approvals, companies, createDb, decisionQueueItems, decisionRetention, inboxDismissals, issues, type Db, +} from "@paperclipai/db"; +import { ATTENTION_SOURCE_KINDS, type AttentionFeed, type AttentionItem, type AttentionSummary } from "@paperclipai/shared"; +import { + ATTENTION_SUMMARY_BOARD_ACTORS, seedAttentionSummaryFixture, seedAttentionSummaryVolume, type AttentionSummaryFixture, +} from "./helpers/attention-summary-fixture.js"; +import { meterDatabaseStatements } from "./helpers/database-statement-meter.js"; +import { + getEmbeddedPostgresTestSupport, + startEmbeddedPostgresTestDatabase, +} from "./helpers/embedded-postgres.js"; +import { seedReviewAttentionFixture, type ReviewAttentionFixture } from "./helpers/review-attention-fixture.js"; +import { errorHandler } from "../middleware/index.js"; +import { attentionRoutes } from "../routes/attention.js"; +import { attentionService } from "../services/attention.js"; +import { issueReadSqlCondition, type AuthorizationActor } from "../services/authorization.js"; +import { issueService } from "../services/issues.js"; + +const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport(); +const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip; + +if (!embeddedPostgresSupport.supported) { + console.warn( + `Skipping embedded Postgres attention summary tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`, + ); +} + +type Counts = Pick; +const VIEWS = ["mine", "unassigned", "company", "archived", "all"] as const; +const counts = (value: Counts): Counts => ({ + totalCount: value.totalCount, deskBadgeCount: value.deskBadgeCount, viewCounts: value.viewCounts, + countsBySourceKind: value.countsBySourceKind, +}); +const taskIdsOf = (items: AttentionItem[]) => [...new Set(items.flatMap((item) => + item.relatedIssue ? [item.relatedIssue.id] : item.subject.kind === "issue" ? [item.subject.id] : []))].sort(); +// A card that is evidence of another card counts with that card. +const leads = (items: AttentionItem[]) => items.filter((item) => !item.obligationLeadId || item.obligationLeadId === item.id); + +/** + * The numbers the full feed implies, counted from its cards: the cards of the + * unfiltered feed, and the leading cards of each view. + */ +async function countsFromFeed(db: Db, companyId: string, options: { userId: string; actor: AuthorizationActor }) { + const service = attentionService(db); + const everything = { ...options, all: true, allowUnscopedAll: true } as const; + const feed = await service.list(companyId, everything); + const viewCounts = { mine: 0, unassigned: 0, company: 0, archived: 0, all: 0 }; + for (const view of VIEWS) viewCounts[view] = leads((await service.list(companyId, { ...everything, view })).items).length; + const countsBySourceKind = Object.fromEntries(ATTENTION_SOURCE_KINDS.map((kind) => [kind, 0])) as Counts["countsBySourceKind"]; + for (const item of feed.items) countsBySourceKind[item.sourceKind] += 1; + const counted: Counts = { totalCount: feed.items.length, deskBadgeCount: viewCounts.mine, viewCounts, countsBySourceKind }; + return { feed, counted }; +} + +describeEmbeddedPostgres("attention summary: the counts of the full feed", () => { + let db!: ReturnType; + let tempDb: Awaited> | null = null; + let fixture!: AttentionSummaryFixture; + const reader = (name: (typeof ATTENTION_SUMMARY_BOARD_ACTORS)[number]) => { + const actor = fixture.actors[name]; + return { userId: actor.userId!, actor }; + }; + + beforeAll(async () => { + tempDb = await startEmbeddedPostgresTestDatabase("paperclip-attention-summary-counts-"); + db = createDb(tempDb.connectionString); + fixture = await seedAttentionSummaryFixture(db); + }, 120_000); + + afterAll(async () => { + await db?.$client.end({ timeout: 1 }); + await tempDb?.cleanup(); + }); + + it("counts what the feed shows before any feed was read, and seeds what the feed seeds", async () => { + const owner = reader("board owner"); + // The first read of this database is a summary: it has no state of an earlier feed read to lean on. + const summary = await attentionService(db).summary(fixture.companyId, owner); + const stored = async () => ({ + retention: (await db.select({ id: decisionRetention.id }).from(decisionRetention) + .where(eq(decisionRetention.companyId, fixture.companyId))).length, + queueItems: (await db.select({ id: decisionQueueItems.id }).from(decisionQueueItems) + .where(eq(decisionQueueItems.companyId, fixture.companyId))).length, + }); + const afterSummary = await stored(); + const { feed, counted } = await countsFromFeed(db, fixture.companyId, owner); + expect(counts(summary)).toEqual(counted); + expect(counts(feed)).toEqual(counted); + // The feed found every retention state and every queue member it writes. + expect(await stored()).toEqual(afterSummary); + expect(afterSummary.retention).toBeGreaterThanOrEqual(feed.items.length); + expect(afterSummary.queueItems).toBeGreaterThan(0); + + // The fixture reaches every source, every view and cards that join into one obligation. + for (const kind of ATTENTION_SOURCE_KINDS) { + if (kind !== "productivity_review") expect(summary.countsBySourceKind[kind], kind).toBeGreaterThan(0); + } + expect(summary.viewCounts.mine).toBeGreaterThan(0); + expect(summary.viewCounts.unassigned).toBeGreaterThan(0); + expect(summary.viewCounts.company).toBeGreaterThan(0); + expect(feed.items.filter((item) => item.obligationLeadId && item.obligationLeadId !== item.id).length).toBeGreaterThan(2); + expect(summary.viewCounts.all).toBeLessThan(summary.totalCount); + const cardIds = new Set(feed.items.map((item) => item.subject.id)); + for (const [name, id] of Object.entries(fixture.named)) expect(cardIds.has(id), name).toBe(name !== "forViewer"); + for (const [name, id] of Object.entries(fixture.notCards)) expect(cardIds.has(id), name).toBe(false); + for (const id of [...Object.values(fixture.cards.unreadablePrivate), ...Object.values(fixture.cards.hidden), + ...Object.values(fixture.foreign)]) expect(cardIds.has(id)).toBe(false); + }, 120_000); + + it("gives every board actor the counts and the tasks of their own feed", async () => { + const totals = new Map(); + for (const name of ATTENTION_SUMMARY_BOARD_ACTORS) { + const options = reader(name); + const { feed, counted } = await countsFromFeed(db, fixture.companyId, options); + const summary = await attentionService(db).summary(fixture.companyId, options); + expect(counts(summary), name).toEqual(counted); + expect(counts(feed), name).toEqual(counted); + const index = await attentionService(db).taskIndex(fixture.companyId, options); + expect(counts(index), name).toEqual(counted); + expect([...index.taskIds].sort(), name).toEqual(taskIdsOf(feed.items)); + totals.set(name, summary.totalCount); + } + // Read access changes the numbers: a reader of every task, an owner, a + // member without a private task and a user of another company. + expect(totals.get("instance admin")!).toBeGreaterThan(totals.get("board owner")!); + expect(totals.get("board owner")!).toBeGreaterThan(totals.get("board member")!); + expect(totals.get("board operator")!).toBeGreaterThan(totals.get("board member")!); + expect(totals.get("board viewer")!).toBeGreaterThan(totals.get("board member")!); + expect(totals.get("local implicit board")).toBe(totals.get("instance admin")); + expect(totals.get("board without membership")!).toBeLessThanOrEqual(totals.get("board member")!); + }, 240_000); + + it("leaves out what a person archived or snoozed, and counts a card again when its content changed", async () => { + const service = attentionService(db); + for (const name of ["board owner", "board operator"] as const) { + const options = reader(name); + const before = await service.summary(fixture.companyId, options); + const feed = await service.list(fixture.companyId, { ...options, all: true, allowUnscopedAll: true }); + const first = (kind: AttentionItem["sourceKind"], accept: (item: AttentionItem) => boolean = () => true) => { + const item = feed.items.find((candidate) => candidate.sourceKind === kind && accept(candidate)); + expect(item, `${name}: ${kind}`).toBeTruthy(); + return item!; + }; + const isLead = (item: AttentionItem) => !item.obligationLeadId || item.obligationLeadId === item.id; + const changed = first("approval", (item) => isLead(item) && item.subject.id !== fixture.overrideApprovalId); + const hidden = [ + changed, first("review", isLead), first("blocker_attention", isLead), first("decision"), first("join_request"), + // Evidence of an obligation: hiding it hides the obligation. + first("blocker_attention", (item) => !isLead(item)), + ]; + for (const item of hidden) { + await service.hideForUser(fixture.companyId, { ...options, sourceKind: item.sourceKind, sourceId: item.subject.id, + mode: "archive", fingerprint: item.changeFingerprint }); + } + const snoozed = first("issue_thread_interaction", isLead); + await service.hideForUser(fixture.companyId, { ...options, sourceKind: snoozed.sourceKind, sourceId: snoozed.subject.id, + mode: "snooze", until: new Date(Date.now() + 86_400_000).toISOString(), fingerprint: snoozed.changeFingerprint }); + // Dismissals saved before cards had a fingerprint: one after the last + // activity of its card (still hidden) and one before it (shown again). + const hiddenLeadIds = new Set(hidden.map((item) => item.obligationLeadId ?? item.id)); + const legacy = feed.items + .filter((item) => item.sourceKind === "failed_run" && isLead(item) && !hiddenLeadIds.has(item.id)).slice(0, 2); + expect(legacy).toHaveLength(2); + await db.insert(inboxDismissals).values([ + { companyId: fixture.companyId, userId: options.userId, itemKey: legacy[0]!.dismissalKey, dismissedAt: new Date() }, + { companyId: fixture.companyId, userId: options.userId, itemKey: legacy[1]!.dismissalKey, dismissedAt: new Date(0) }, + // A snooze that ended, and a dismissal of a card of another company. + { companyId: fixture.companyId, userId: options.userId, itemKey: first("agent_error_alert").dismissalKey, + kind: "snooze", dismissedAt: new Date(), snoozedUntil: new Date(Date.now() - 60_000) }, + { companyId: fixture.foreignCompanyId, userId: options.userId, itemKey: first("recovery_action").dismissalKey, + dismissedAt: new Date() }, + ]); + + const { counted } = await countsFromFeed(db, fixture.companyId, options); + const hiddenSummary = await service.summary(fixture.companyId, options); + expect(counts(hiddenSummary), name).toEqual(counted); + expect(hiddenSummary.viewCounts.archived, name).toBeGreaterThanOrEqual(7); + expect(hiddenSummary.viewCounts.all, name).toBeLessThanOrEqual(before.viewCounts.all - 7); + + // A new proposal on an archived approval is a new card for the person. + await db.update(approvals).set({ payload: { title: `A new proposal for ${name}` } }).where(eq(approvals.id, changed.subject.id)); + const returned = await countsFromFeed(db, fixture.companyId, options); + const returnedSummary = await service.summary(fixture.companyId, options); + expect(counts(returnedSummary), name).toEqual(returned.counted); + expect(returnedSummary.viewCounts.archived, name).toBe(hiddenSummary.viewCounts.archived - 1); + expect(returnedSummary.viewCounts.all, name).toBe(hiddenSummary.viewCounts.all + 1); + } + // What one person hid stays in the count of every other person. + for (const name of ["board admin", "board viewer"] as const) { + const options = reader(name); + const { counted } = await countsFromFeed(db, fixture.companyId, options); + const summary = await service.summary(fixture.companyId, options); + expect(counts(summary), name).toEqual(counted); + expect(summary.viewCounts.archived, name).toBe(0); + } + }, 240_000); + + it("moves the cards without an owner to the default decision owner of the company", async () => { + const service = attentionService(db); + const before = await service.summary(fixture.companyId, reader("board admin")); + expect(before.viewCounts.unassigned).toBeGreaterThan(0); + await db.update(companies).set({ defaultDecisionOwnerUserId: fixture.users.admin }).where(eq(companies.id, fixture.companyId)); + try { + for (const name of ["board admin", "board owner", "board member", "instance admin"] as const) { + const options = reader(name); + const { counted } = await countsFromFeed(db, fixture.companyId, options); + const summary = await service.summary(fixture.companyId, options); + expect(counts(summary), name).toEqual(counted); + expect(summary.viewCounts.unassigned, name).toBe(0); + } + const after = await service.summary(fixture.companyId, reader("board admin")); + expect(after.viewCounts.mine).toBeGreaterThanOrEqual(before.viewCounts.mine + before.viewCounts.unassigned); + } finally { + await db.update(companies).set({ defaultDecisionOwnerUserId: null }).where(eq(companies.id, fixture.companyId)); + } + }, 120_000); + + it("reads one company, and returns zeros for a company without cards", async () => { + const service = attentionService(db); + const foreign = fixture.actors["board without membership"]; + const foreignOptions = { userId: foreign.userId!, actor: foreign }; + const { feed, counted } = await countsFromFeed(db, fixture.foreignCompanyId, foreignOptions); + expect(counts(await service.summary(fixture.foreignCompanyId, foreignOptions))).toEqual(counted); + // The approval, the question, the decision, the failed run, the review + // and the agent in error of the other company, and nothing of this one. + expect(new Set(feed.items.map((item) => item.companyId))).toEqual(new Set([fixture.foreignCompanyId])); + expect(counted.totalCount).toBe(6); + + const empty = await service.summary(fixture.emptyCompanyId, reader("board owner")); + const emptyFeed = await countsFromFeed(db, fixture.emptyCompanyId, reader("board owner")); + expect(counts(empty)).toEqual(emptyFeed.counted); + expect(counts(empty)).toEqual({ + totalCount: 0, deskBadgeCount: 0, viewCounts: { mine: 0, unassigned: 0, company: 0, archived: 0, all: 0 }, + countsBySourceKind: Object.fromEntries(ATTENTION_SOURCE_KINDS.map((kind) => [kind, 0])), + }); + }, 120_000); + + it("serves the same counts on the route, to board users of the company only", async () => { + const app = (actor: unknown) => { + const testApp = express(); + testApp.use((req, _res, next) => { + (req as unknown as { actor: unknown }).actor = actor; + next(); + }); + testApp.use("/api", attentionRoutes(db)); + testApp.use(errorHandler); + return testApp; + }; + const route = `/api/companies/${fixture.companyId}/attention/summary`; + for (const name of ["board owner", "board member", "cloud tenant viewer"] as const) { + const options = reader(name); + const response = await request(app(options.actor)).get(route).expect(200); + expect(response.body, name).not.toHaveProperty("items"); + expect(counts(response.body as AttentionSummary), name) + .toEqual(counts(await attentionService(db).summary(fixture.companyId, options))); + const page = await request(app(options.actor)).get(`/api/companies/${fixture.companyId}/attention?limit=5`).expect(200); + expect(counts(response.body as AttentionSummary), name).toEqual(counts(page.body as AttentionFeed)); + } + await request(app(fixture.actors["board without membership"])).get(route).expect(403); + await request(app(fixture.actors["agent key"])).get(route).expect(403); + await request(app(fixture.actors.unauthenticated)).get(route).expect(401); + }, 120_000); + + it("reads the blocked tasks of the task list, in the order of the task list", async () => { + const tasks = issueService(db); + // Blocked tasks that the task list leaves out: a plugin operation and a harness task. + await db.insert(issues).values([ + { companyId: fixture.companyId, title: "Plugin operation", status: "blocked", originKind: "plugin:example:operation" }, + { companyId: fixture.companyId, title: "Harness task", status: "blocked", harnessKind: "review" }, + ]); + for (const name of ["board owner", "board member", "instance admin"] as const) { + const { actor } = reader(name); + const readCondition = await issueReadSqlCondition(db, actor); + const listed = await tasks.list(fixture.companyId, { status: "blocked", includeBlockedBy: true, readCondition }); + const roots = await tasks.listBlockerAttentionRoots(fixture.companyId, { status: "blocked", readCondition }); + expect(roots.map((root) => root.id), name).toEqual(listed.map((row) => row.id)); + expect(roots.length, name).toBeGreaterThan(8); + const attention = await tasks.listBlockerAttention(fixture.companyId, roots); + for (const row of listed as Array<(typeof listed)[number] & { blockerAttention?: unknown }>) { + expect(attention.get(row.id), `${name}: ${row.title}`).toEqual(row.blockerAttention); + } + } + const everyBlocked = await db.select({ id: issues.id }).from(issues) + .where(and(eq(issues.companyId, fixture.companyId), eq(issues.status, "blocked"))); + const readable = await tasks.listBlockerAttentionRoots(fixture.companyId, { status: "blocked", readCondition: sql`true` }); + expect(everyBlocked.length - readable.length).toBe(2); + }, 120_000); +}); + +describeEmbeddedPostgres("attention summary: what one request reads", () => { + let db!: ReturnType; + let tempDb: Awaited> | null = null; + let meter!: ReturnType; + const companies: Record = {}; + + beforeAll(async () => { + tempDb = await startEmbeddedPostgresTestDatabase("paperclip-attention-summary-cost-"); + db = createDb(tempDb.connectionString); + meter = meterDatabaseStatements(db, tempDb.connectionString); + // Two sizes on the two sides of the batch size of the related-row reads + // (500), and three small companies that differ in one property each. + companies.small = await seedReviewAttentionFixture(db, { tasks: 2_000, agents: 24, prefix: "SML", descriptionBytes: 200 }); + companies.large = await seedReviewAttentionFixture(db, { tasks: 6_000, agents: 24, prefix: "LRG", descriptionBytes: 200 }); + companies.plain = await seedReviewAttentionFixture(db, { tasks: 600, agents: 24, prefix: "PLN", descriptionBytes: 200 }); + companies.longText = await seedReviewAttentionFixture(db, { tasks: 600, agents: 24, prefix: "TXT", descriptionBytes: 20_000 }); + companies.budgets = await seedReviewAttentionFixture(db, { tasks: 600, agents: 24, prefix: "BDG", descriptionBytes: 200 }); + for (const name of ["plain", "longText"]) await seedAttentionSummaryVolume(db, companies[name]!, { budgetPolicies: 0 }); + await seedAttentionSummaryVolume(db, companies.budgets!, { budgetPolicies: 20 }); + await db.$client.unsafe("analyze"); + }, 240_000); + + afterAll(async () => { + meter?.uninstall(); + await db?.$client.end({ timeout: 1 }); + await tempDb?.cleanup(); + }); + + const actorOf = (company: ReviewAttentionFixture): AuthorizationActor => ({ + type: "board", source: "session", userId: company.userId, companyIds: [company.companyId], isInstanceAdmin: false, + }); + async function metered(read: () => Promise) { + const before = meter.read(); + const result = await read(); + const after = meter.read(); + return { + result, statements: after.statements - before.statements, transactions: after.transactions - before.transactions, + rows: after.rows - before.rows, bytes: after.bytesReceived - before.bytesReceived, + }; + } + /** One summary request after an earlier one has written the retention states and seeded the queues. */ + async function summaryCost(company: ReviewAttentionFixture) { + const options = { userId: company.userId, actor: actorOf(company) }; + await attentionService(db).summary(company.companyId, options); + return metered(() => attentionService(db).summary(company.companyId, options)); + } + /** What the two classifications that a count depends on read for the same tasks. */ + async function classificationCost(company: ReviewAttentionFixture) { + const rowsOf = (status: string) => db.select().from(issues) + .where(and(eq(issues.companyId, company.companyId), eq(issues.status, status))); + const [blocked, reviews] = [await rowsOf("blocked"), await rowsOf("in_review")]; + const blocker = await metered(() => issueService(db).listBlockerAttention(company.companyId, blocked)); + const review = await metered(() => issueService(db).listReviewAttention(company.companyId, reviews)); + return { statements: blocker.statements + review.statements, rows: blocker.rows + review.rows }; + } + + it("issues a fixed number of statements of its own, whatever the number of cards", async () => { + const [small, large] = [await summaryCost(companies.small!), await summaryCost(companies.large!)]; + const [smallClassification, largeClassification] = [ + await classificationCost(companies.small!), await classificationCost(companies.large!), + ]; + expect(large.result.totalCount).toBeGreaterThan(small.result.totalCount * 2.5); + expect(large.result.countsBySourceKind.blocker_attention).toBeGreaterThan(150); + expect(large.result.countsBySourceKind.review).toBeGreaterThan(250); + // The classifications read their related rows in batches, so their + // statements grow with the tasks. Nothing else in the request does. + expect(largeClassification.statements).toBeGreaterThan(smallClassification.statements); + const ownStatements = { small: small.statements - smallClassification.statements, large: large.statements - largeClassification.statements }; + expect(ownStatements.large).toBe(ownStatements.small); + expect(ownStatements.large).toBeLessThanOrEqual(30); + // Rows: each blocked task and each review one time, each card's task for + // its audience, its retention state, and the rows of the classifications. + const ownRows = large.rows - largeClassification.rows; + expect(ownRows).toBeLessThanOrEqual(companies.large!.counts.blocked + companies.large!.counts.reviews + 4 * large.result.totalCount); + }, 240_000); + + it("reads no task description", async () => { + const [plain, longText] = [await summaryCost(companies.plain!), await summaryCost(companies.longText!)]; + expect(counts(longText.result)).toEqual(counts(plain.result)); + expect(longText.statements).toBe(plain.statements); + expect(longText.rows).toBe(plain.rows); + // 600 tasks with 20,000 bytes of description each against 200 bytes each. + expect(longText.bytes).toBeLessThan(plain.bytes * 1.05); + }, 120_000); + + it("does not sum the spend of the budget policies", async () => { + const [plain, budgets] = [await summaryCost(companies.plain!), await summaryCost(companies.budgets!)]; + expect(counts(budgets.result)).toEqual(counts(plain.result)); + expect(budgets.statements).toBe(plain.statements); + // Both companies have cards that seed the decision queues, so the request + // also made the writes of the feed: the queues hold those cards. + expect(budgets.result.countsBySourceKind.issue_thread_interaction).toBeGreaterThanOrEqual(30); + const seeded = await db.select({ id: decisionQueueItems.id }).from(decisionQueueItems) + .where(eq(decisionQueueItems.companyId, companies.budgets!.companyId)); + expect(seeded.length).toBeGreaterThanOrEqual(30); + }, 120_000); +}); diff --git a/server/src/__tests__/helpers/attention-summary-fixture.ts b/server/src/__tests__/helpers/attention-summary-fixture.ts new file mode 100644 index 00000000000..64b90a70607 --- /dev/null +++ b/server/src/__tests__/helpers/attention-summary-fixture.ts @@ -0,0 +1,362 @@ +import { randomUUID } from "node:crypto"; +import { and, eq, inArray } from "drizzle-orm"; +import { + agents, + approvals, + budgetIncidents, + budgetPolicies, + companies, + companyMemberships, + decisionBundles, + decisionRetention, + decisionTriage, + decisions, + heartbeatRunEvents, + heartbeatRuns, + invites, + issueApprovals, + issueRecoveryActions, + issueRelations, + issueThreadInteractions, + issueWorkProducts, + issues, + joinRequests, + projects, + type Db, +} from "@paperclipai/db"; +import { seedIssueReadVisibilityFixture } from "./issue-read-visibility-fixture.js"; +import type { ReviewAttentionFixture } from "./review-attention-fixture.js"; + +const HOUR_MS = 3_600_000, DAY_MS = 24 * HOUR_MS; +const EXHAUSTED = "Bounded retry exhausted after 4 scheduled attempts; no further automatic retry will be queued"; +const QUESTIONS = { + version: 1, + questions: [{ id: "q1", prompt: "Which option?", options: [{ id: "a", label: "A" }, { id: "b", label: "B" }] }], +}; + +/** The board actors of the visibility fixture: every membership role and every way a board user signs in. */ +export const ATTENTION_SUMMARY_BOARD_ACTORS = [ + "board owner", "board admin", "board operator", "board viewer", "board member", "board key operator", + "cloud tenant owner", "cloud tenant viewer", "instance admin", "local implicit board", "board without membership", +] as const; + +export type AttentionSummaryFixture = Awaited>; + +/** + * One company with a card of every attention source on every kind of task a + * reader may or may not see: open tasks, private tasks readable through + * creation, a grant or an assignment, private tasks nobody in the fixture may + * read, tasks of a private project, a hidden task and a chat container. It + * adds cards that join into one obligation, cards for one person, cards with + * no owner, old cards that move to an escalation owner, snoozed and archived + * cards, and the same sources in a second company. A third company is empty. + */ +export async function seedAttentionSummaryFixture(db: Db) { + const visibility = await seedIssueReadVisibilityFixture(db, { publicCount: 12 }); + const { companyId, foreignCompanyId, users, publicIds } = visibility; + const now = Date.now(); + const at = (msAgo: number) => new Date(now - msAgo); + + const pausedAgentId = randomUUID(), erroredAgentId = randomUUID(), terminatedAgentId = randomUUID(); + const foreignErroredAgentId = randomUUID(); + await db.insert(agents).values([ + { id: pausedAgentId, companyId, name: "Paused", role: "engineer", adapterType: "process", status: "paused" }, + { id: erroredAgentId, companyId, name: "Errored", role: "engineer", adapterType: "process", status: "error", errorReason: "No credentials" }, + { id: terminatedAgentId, companyId, name: "Terminated", role: "engineer", adapterType: "process", status: "terminated" }, + { id: foreignErroredAgentId, companyId: foreignCompanyId, name: "Foreign errored", role: "engineer", adapterType: "process", + status: "error", errorReason: "No credentials" }, + ]); + // No default decision owner: a card without an owner waits in Unassigned. + await db.update(companies).set({ budgetOwnerUserId: users.owner }).where(eq(companies.id, companyId)); + await db.update(projects).set({ decisionOwnerUserId: users.operator }).where(eq(projects.id, visibility.openProjectId)); + + const approval = async (input: { + company?: string; issueId?: string | null; type?: string; requestedByUserId?: string | null; + requestedByAgentId?: string | null; updatedAt?: Date; title?: string; + }) => { + const id = randomUUID(), company = input.company ?? companyId; + await db.insert(approvals).values({ + id, companyId: company, type: input.type ?? "request_board_approval", status: "pending", + payload: { title: input.title ?? "Approve this" }, requestedByUserId: input.requestedByUserId ?? null, + requestedByAgentId: input.requestedByAgentId ?? null, + createdAt: input.updatedAt ?? at(2 * HOUR_MS), updatedAt: input.updatedAt ?? at(2 * HOUR_MS), + }); + if (input.issueId) await db.insert(issueApprovals).values({ companyId: company, issueId: input.issueId, approvalId: id }); + return id; + }; + const interaction = async (input: { + company?: string; issueId: string; kind?: string; payload?: Record; status?: string; + addresseeUserId?: string | null; addresseeAgentId?: string | null; createdByAgentId?: string | null; createdAt?: Date; + }) => { + const id = randomUUID(); + await db.insert(issueThreadInteractions).values({ + id, companyId: input.company ?? companyId, issueId: input.issueId, kind: input.kind ?? "ask_user_questions", + status: input.status ?? "pending", continuationPolicy: "wake_assignee", + payload: (input.payload ?? QUESTIONS) as never, + addresseeUserId: input.addresseeUserId ?? null, addresseeAgentId: input.addresseeAgentId ?? null, + createdByAgentId: input.createdByAgentId ?? null, + createdAt: input.createdAt ?? at(3 * HOUR_MS), updatedAt: input.createdAt ?? at(3 * HOUR_MS), + }); + return id; + }; + const run = async (input: { company?: string; agentId: string; issueId?: string | null; status: string; createdAt: Date }) => { + const id = randomUUID(); + await db.insert(heartbeatRuns).values({ + id, companyId: input.company ?? companyId, agentId: input.agentId, status: input.status, + contextSnapshot: input.issueId ? { issueId: input.issueId } : {}, + createdAt: input.createdAt, updatedAt: input.createdAt, finishedAt: input.createdAt, + }); + return id; + }; + const failedRun = async (input: { company?: string; agentId: string; issueId?: string | null; createdAt?: Date }) => { + const id = await run({ ...input, status: "failed", createdAt: input.createdAt ?? at(HOUR_MS) }); + await db.insert(heartbeatRunEvents).values({ + companyId: input.company ?? companyId, runId: id, agentId: input.agentId, seq: 1, eventType: "lifecycle", message: EXHAUSTED, + }); + return id; + }; + const decision = async (input: { company?: string; agentId: string; issueId: string; bundle?: boolean }) => { + const id = randomUUID(), company = input.company ?? companyId; + // Older than every failed run, so it never counts as a newer run of the task. + const originRunId = await run({ company, agentId: input.agentId, issueId: input.issueId, status: "succeeded", createdAt: at(2 * DAY_MS) }); + const origin = { companyId: company, originAgentId: input.agentId, originIssueId: input.issueId, originRunId }; + const bundleId = input.bundle + ? (await db.insert(decisionBundles).values({ ...origin, title: "Bundle", summary: "Two decisions" }).returning())[0]!.id + : null; + await db.insert(decisions).values({ + id, ...origin, bundleId, title: "Choose", body: "Choose one option.", options: [], status: "open", + signedSpec: "test", targetSnapshots: {}, expiresAt: new Date(now + 7 * DAY_MS), + createdAt: at(4 * HOUR_MS), updatedAt: at(4 * HOUR_MS), + }); + return id; + }; + const recovery = async (input: { issueId: string; ownerType: "user" | "board"; ownerUserId?: string | null; status?: string }) => { + const id = randomUUID(); + await db.insert(issueRecoveryActions).values({ + id, companyId, sourceIssueId: input.issueId, kind: "missing_disposition", status: input.status ?? "active", + ownerType: input.ownerType, ownerUserId: input.ownerUserId ?? null, cause: "missing_disposition", + fingerprint: `recovery:${id}`, evidence: {}, nextAction: "Choose the final disposition.", + }); + return id; + }; + /** One card of each source that hangs on a task. */ + const cardsOn = async (issueId: string, agentId = visibility.readerAgentId) => ({ + approvalId: await approval({ issueId, requestedByAgentId: agentId }), + interactionId: await interaction({ issueId, createdByAgentId: agentId }), + decisionId: await decision({ agentId, issueId }), + recoveryId: await recovery({ issueId, ownerType: "user", ownerUserId: users.operator }), + failedRunId: await failedRun({ agentId, issueId }), + }); + + // Cards on each kind of task. A reader gets the cards of the tasks they can read. + const cards = { + open: await cardsOn(publicIds[0]!), + openInProject: await cardsOn(publicIds[2]!), + ownedPrivate: await cardsOn(visibility.ownedPrivateIds[0]!), + ownedPrivateChild: await cardsOn(visibility.ownedPrivateIds[1]!), + grantedPrivate: await cardsOn(visibility.grantedPrivateIds[0]!), + assignedPrivate: await cardsOn(visibility.assignedPrivateIds[1]!, visibility.assigneeAgentId), + unreadablePrivate: await cardsOn(visibility.unreadablePrivateIds[0]!), + privateProject: await cardsOn(visibility.privateProjectIssueIds[0]!), + hidden: await cardsOn(visibility.hiddenId), + }; + await db.insert(issueWorkProducts).values([publicIds[0]!, visibility.ownedPrivateIds[0]!].map((issueId) => ({ + companyId, issueId, type: "pull_request", provider: "github", title: "Pull request", status: "open", + }))); + + // Reviews: for a person, for a human participant, behind a pending approval, + // and stalled; one of each on tasks that only some readers see. + const reviewFor = (userId: string) => ({ + status: "pending", currentStageId: randomUUID(), currentStageIndex: 0, currentStageType: "review", + currentParticipant: { type: "user", userId, agentId: null }, returnAssignee: null, completedStageIds: [], + lastDecisionId: null, lastDecisionOutcome: null, + }); + const review = (issueId: string, values: Partial) => + db.update(issues).set({ status: "in_review", updatedAt: at(5 * HOUR_MS), ...values }).where(eq(issues.id, issueId)); + await review(publicIds[4]!, { assigneeUserId: users.operator }); + await review(publicIds[5]!, { assigneeAgentId: visibility.readerAgentId, executionState: reviewFor(users.viewer) }); + await review(publicIds[6]!, { assigneeAgentId: visibility.readerAgentId }); + const reviewApprovalId = await approval({ issueId: publicIds[6]!, requestedByUserId: users.member }); + await review(publicIds[7]!, { assigneeAgentId: visibility.readerAgentId }); + await review(visibility.ownedPrivateIds[2]!, { assigneeUserId: users.owner }); + await review(visibility.grantedPrivateIds[2]!, { assigneeAgentId: visibility.readerAgentId }); + await review(visibility.unreadablePrivateIds[2]!, { assigneeUserId: visibility.strangerUserId }); + await review(visibility.privateProjectIssueIds[1]!, { assigneeAgentId: visibility.readerAgentId }); + + // Blocked work: tasks a person must unblock, and tasks behind blockers that + // nobody works on. Two tasks behind one blocker changed at the same instant, + // so the order of the task list decides which one its card names. + const blockedAt = at(6 * HOUR_MS), descriptorAt = at(HOUR_MS); + const task = async (values: Partial & { title: string; status: string }) => { + const id = randomUUID(); + await db.insert(issues).values({ id, companyId, createdByUserId: visibility.strangerUserId, ...values }); + return id; + }; + const boardOwned = await task({ title: "Board must unblock", status: "blocked", blockedTransitionAt: descriptorAt, + unblockDescriptor: { owner: "board", action: "Choose how to continue." } as never }); + const operatorOwned = await task({ title: "Operator must unblock", status: "blocked", blockedTransitionAt: descriptorAt, + unblockDescriptor: { owner: { userId: users.operator }, action: "Send the contract." } as never }); + const privateBoardOwned = visibility.unreadablePrivateIds[1]!; + await db.update(issues).set({ status: "blocked", blockedTransitionAt: descriptorAt, + unblockDescriptor: { owner: "board", action: "Choose how to continue." } as never }).where(eq(issues.id, privateBoardOwned)); + const unownedBlocker = await task({ title: "Nobody works on this", status: "todo" }); + const pausedBlocker = await task({ title: "Paused agent works on this", status: "todo", assigneeAgentId: pausedAgentId }); + const cancelledBlocker = await task({ title: "Cancelled", status: "cancelled" }); + const sharedBlocker = await task({ title: "Two tasks wait for this", status: "todo" }); + const waiting = await Promise.all([unownedBlocker, pausedBlocker, cancelledBlocker, sharedBlocker, sharedBlocker, + visibility.unreadablePrivateIds[0]!, visibility.ownedPrivateIds[1]!].map(async (blockerId, index) => { + const id = await task({ title: `Waits ${index}`, status: "blocked", priority: index === 4 ? "high" : "medium", + createdAt: blockedAt, updatedAt: blockedAt, blockedTransitionAt: blockedAt }); + await db.insert(issueRelations).values({ companyId, issueId: blockerId, relatedIssueId: id, type: "blocks" }); + return id; + })); + // A chain: this task waits for a blocked task, which waits for nobody's task. + const chained = await task({ title: "Waits behind a chain", status: "blocked", blockedTransitionAt: blockedAt }); + await db.insert(issueRelations).values({ companyId, issueId: waiting[0]!, relatedIssueId: chained, type: "blocks" }); + // A failed run on a task a person must unblock joins that card. + await failedRun({ agentId: visibility.readerAgentId, issueId: boardOwned }); + await failedRun({ agentId: visibility.readerAgentId, issueId: sharedBlocker }); + + // Failed runs that are not cards: a newer run of the same task, a terminated agent. + const retried = await failedRun({ agentId: visibility.readerAgentId, issueId: publicIds[8]!, createdAt: at(3 * HOUR_MS) }); + await run({ agentId: visibility.readerAgentId, issueId: publicIds[8]!, status: "succeeded", createdAt: at(2 * HOUR_MS) }); + const terminated = await failedRun({ agentId: terminatedAgentId, issueId: publicIds[9]! }); + const withoutTask = await failedRun({ agentId: visibility.readerAgentId, issueId: null }); + + // Requests with one recipient, with an agent recipient, replaced requests and a chat container. + const forViewer = await interaction({ issueId: publicIds[1]!, addresseeUserId: users.viewer }); + const forWorkingAgent = await interaction({ issueId: publicIds[1]!, addresseeAgentId: visibility.readerAgentId }); + const forPausedAgent = await interaction({ issueId: publicIds[1]!, addresseeAgentId: pausedAgentId }); + const plan = { version: 1, prompt: "Accept the plan?", target: { type: "issue_document", key: "plan", revisionId: randomUUID() } }; + const replacedPlan = await interaction({ issueId: publicIds[3]!, kind: "request_confirmation", payload: plan, createdAt: at(9 * HOUR_MS) }); + const currentPlan = await interaction({ issueId: publicIds[3]!, kind: "request_confirmation", payload: plan, createdAt: at(8 * HOUR_MS) }); + const settledOver = await interaction({ issueId: publicIds[10]!, kind: "request_confirmation", + payload: { version: 1, prompt: "Continue?" }, createdAt: at(9 * HOUR_MS) }); + await interaction({ issueId: publicIds[10]!, kind: "request_confirmation", status: "accepted", + payload: { version: 1, prompt: "Continue?" }, createdAt: at(8 * HOUR_MS) }); + const governed = await interaction({ issueId: publicIds[10]!, kind: "request_confirmation", createdAt: at(10 * HOUR_MS), + payload: { version: 1, prompt: "Run the tool?", toolAction: { tool: "deploy", arguments: {} } } }); + const chat = await task({ title: "Chat", status: "todo", conversationAgentId: visibility.readerAgentId, + conversationUserId: users.member, conversationState: "active", assigneeAgentId: visibility.readerAgentId }); + const inChat = await interaction({ issueId: chat }); + const chatApproval = await approval({ issueId: chat, requestedByAgentId: visibility.readerAgentId }); + + // Approvals without a task: from a person, from an agent, a hire, and an old one. + const fromOwner = await approval({ requestedByUserId: users.owner }); + const fromAgent = await approval({ requestedByAgentId: visibility.readerAgentId }); + const hire = await approval({ type: "hire_agent", requestedByAgentId: visibility.readerAgentId }); + const oldFromViewer = await approval({ requestedByUserId: users.viewer, updatedAt: at(40 * DAY_MS) }); + const oldUnowned = await interaction({ issueId: publicIds[11]!, createdAt: at(10 * DAY_MS) }); + const bundled = await decision({ agentId: visibility.readerAgentId, issueId: publicIds[1]!, bundle: true }); + + // Company alerts: join request, budget incidents, agents in error. + const [invite] = await db.insert(invites).values({ companyId, tokenHash: `hash-${randomUUID()}`, allowedJoinTypes: "both", + expiresAt: new Date(now + DAY_MS) }).returning(); + const joinRequestId = randomUUID(); + await db.insert(joinRequests).values({ id: joinRequestId, inviteId: invite!.id, companyId, requestType: "human", + status: "pending_approval", requestIp: "127.0.0.1", requestEmailSnapshot: "new@example.test" }); + const policies = await db.insert(budgetPolicies).values([ + { companyId, scopeType: "company", scopeId: companyId, metric: "billed_cents", windowKind: "calendar_month_utc", amount: 100 }, + { companyId, scopeType: "company", scopeId: companyId, metric: "billed_cents", windowKind: "lifetime", amount: 100 }, + { companyId, scopeType: "agent", scopeId: visibility.readerAgentId, metric: "billed_cents", windowKind: "calendar_month_utc", amount: 100 }, + ]).returning(); + const overrideApprovalId = await approval({ type: "budget_override_required", requestedByAgentId: visibility.readerAgentId }); + const window = { windowStart: new Date(Date.UTC(2026, 10, 1)), windowEnd: new Date(Date.UTC(2026, 11, 1)) }; + const incidents = await db.insert(budgetIncidents).values([ + { ...window, companyId, policyId: policies[0]!.id, scopeType: "company", scopeId: companyId, metric: "billed_cents", + windowKind: "calendar_month_utc", thresholdType: "soft", amountLimit: 100, amountObserved: 90, status: "open" }, + { ...window, companyId, policyId: policies[1]!.id, scopeType: "company", scopeId: companyId, metric: "billed_cents", + windowKind: "lifetime", thresholdType: "soft", amountLimit: 100, amountObserved: 50, status: "open" }, + { ...window, companyId, policyId: policies[2]!.id, scopeType: "agent", scopeId: visibility.readerAgentId, metric: "billed_cents", + windowKind: "calendar_month_utc", thresholdType: "hard", amountLimit: 100, amountObserved: 120, status: "open", + approvalId: overrideApprovalId }, + ]).returning(); + + // Cards that the company snoozed or archived for everybody. + const snoozedForAll = await approval({ requestedByUserId: users.owner }); + await db.insert(decisionTriage).values({ companyId, sourceKind: "approval", sourceId: snoozedForAll, decideBy: "whenever", + setByType: "user", setByUserId: users.owner, snoozedUntil: new Date(now + DAY_MS) }); + const archivedAt = at(12 * HOUR_MS); + const archivedForAll = await interaction({ issueId: publicIds[11]!, createdAt: archivedAt }); + await db.insert(decisionRetention).values({ companyId, sourceKind: "issue_thread_interaction", sourceId: archivedForAll, + sourceActivityAt: archivedAt, archivedAt: at(HOUR_MS), archivedReason: "retention", archivedByType: "system" }); + + // The same sources in another company. No count of this company may read them. + const foreignIssueId = visibility.foreignIds[0]!; + const foreign = { + approvalId: await approval({ company: foreignCompanyId, issueId: foreignIssueId, requestedByAgentId: visibility.foreignAgentId }), + interactionId: await interaction({ company: foreignCompanyId, issueId: foreignIssueId }), + decisionId: await decision({ company: foreignCompanyId, agentId: visibility.foreignAgentId, issueId: foreignIssueId }), + failedRunId: await failedRun({ company: foreignCompanyId, agentId: visibility.foreignAgentId, issueId: foreignIssueId }), + }; + await db.update(issues).set({ status: "in_review", assigneeUserId: visibility.outsiderUserId }) + .where(eq(issues.id, visibility.foreignIds[1]!)); + + const emptyCompanyId = randomUUID(); + await db.insert(companies).values({ id: emptyCompanyId, name: `Empty ${emptyCompanyId}`, + issuePrefix: `E${emptyCompanyId.replaceAll("-", "").slice(0, 6).toUpperCase()}`, requireBoardApprovalForNewAgents: false }); + await db.insert(companyMemberships).values({ companyId: emptyCompanyId, principalType: "user", principalId: users.owner, + status: "active", membershipRole: "owner" }); + + return { + ...visibility, emptyCompanyId, pausedAgentId, erroredAgentId, terminatedAgentId, foreignErroredAgentId, + cards, foreign, + reviewApprovalId, overrideApprovalId, joinRequestId, incidentIds: incidents.map((incident) => incident.id), + blocked: { boardOwned, operatorOwned, privateBoardOwned, waiting, chained, unownedBlocker, pausedBlocker, cancelledBlocker, sharedBlocker }, + notCards: { retried, terminated, forWorkingAgent, replacedPlan, settledOver, chatApproval, snoozedForAll, archivedForAll }, + named: { forViewer, forPausedAgent, currentPlan, governed, inChat, fromOwner, fromAgent, hire, oldFromViewer, oldUnowned, + bundled, withoutTask }, + }; +} + +/** + * Sources that a real company has and the review fixture leaves out: budget + * policies, pull requests, plans and questions that seed the decision queues, + * open decisions and failed runs. They make one summary request read what the + * request of a real company reads. + */ +export async function seedAttentionSummaryVolume(db: Db, fixture: ReviewAttentionFixture, options: { + budgetPolicies?: number; pullRequests?: number; questions?: number; plans?: number; failedRuns?: number; +} = {}) { + const { companyId, agentIds } = fixture; + const count = (value: number | undefined, fallback: number) => value ?? fallback; + const working = await db.select({ id: agents.id }).from(agents) + .where(and(eq(agents.companyId, companyId), inArray(agents.status, ["idle", "running"]))) + .then((rows) => new Set(rows.map((row) => row.id))); + const workingAgentIds = agentIds.filter((id) => working.has(id)); + const pick = (values: Value[], index: number) => values[index % values.length]!; + const taskIds = fixture.taskIds; + const chunked = async (rows: Row[], write: (chunk: Row[]) => Promise) => { + for (let start = 0; start < rows.length; start += 500) await write(rows.slice(start, start + 500)); + }; + + const policyCount = count(options.budgetPolicies, Math.min(agentIds.length, 20)); + await chunked(Array.from({ length: policyCount }, (_unused, index) => index === 0 + ? { companyId, scopeType: "company", scopeId: companyId, metric: "billed_cents", windowKind: "calendar_month_utc", amount: 100_000 } + : { companyId, scopeType: "agent", scopeId: agentIds[index % agentIds.length]!, metric: "billed_cents", + windowKind: index < agentIds.length ? "calendar_month_utc" : "lifetime", amount: 10_000 }), + (chunk) => db.insert(budgetPolicies).values(chunk).onConflictDoNothing()); + + await chunked(Array.from({ length: count(options.pullRequests, Math.floor(fixture.reviewIds.length / 4)) }, (_unused, index) => ({ + companyId, issueId: fixture.reviewIds[index * 4]!, type: "pull_request", provider: "github", title: "Pull request", status: "open", + })), (chunk) => db.insert(issueWorkProducts).values(chunk)); + + const questions = count(options.questions, 20), plans = count(options.plans, 20); + await chunked(Array.from({ length: questions + plans }, (_unused, index) => ({ + companyId, issueId: pick(taskIds, index * 17 + 3), status: "pending", continuationPolicy: "wake_assignee", + kind: index < questions ? "ask_user_questions" : "request_confirmation", + payload: (index < questions + ? QUESTIONS + : { version: 1, prompt: "Accept the plan?", target: { type: "issue_document", key: "plan", revisionId: randomUUID() } }) as never, + })), (chunk) => db.insert(issueThreadInteractions).values(chunk)); + + const failed = Array.from({ length: count(options.failedRuns, 20) }, (_unused, index) => ({ + id: randomUUID(), companyId, agentId: pick(workingAgentIds, index), status: "failed", + contextSnapshot: { issueId: pick(taskIds, index * 29 + 11) }, + })); + await chunked(failed, (chunk) => db.insert(heartbeatRuns).values(chunk)); + await chunked(failed, (chunk) => db.insert(heartbeatRunEvents).values(chunk.map((failedRun) => ({ + companyId, runId: failedRun.id, agentId: failedRun.agentId, seq: 1, eventType: "lifecycle", message: EXHAUSTED, + })))); + return { budgetPolicies: policyCount, questions, plans, failedRuns: failed.length }; +} diff --git a/server/src/__tests__/helpers/database-statement-meter.ts b/server/src/__tests__/helpers/database-statement-meter.ts new file mode 100644 index 00000000000..b6dfb7affb3 --- /dev/null +++ b/server/src/__tests__/helpers/database-statement-meter.ts @@ -0,0 +1,90 @@ +import net from "node:net"; +import type { Db } from "@paperclipai/db"; + +export type MeteredStatement = { sql: string; parameters: number; rows: number; ms: number; inTransaction: boolean }; + +type Method = (this: unknown, ...args: unknown[]) => unknown; + +/** + * Counts what a database client sends and receives: the statements, also the + * ones inside a transaction and the BEGIN and COMMIT of each transaction, the + * rows they return, and the bytes the server sends back on the connections of + * the client. `waits` counts the times the client went from no statement in + * flight to one: statements sent together wait one time, statements sent one + * after the other wait one time each. Install the meter before the first + * statement of the client. + */ +export function meterDatabaseStatements(db: Db, databaseUrl: string) { + const port = Number(new URL(databaseUrl).port); + const sockets = new Set(); + const connect = net.Socket.prototype.connect as Method; + net.Socket.prototype.connect = function (this: net.Socket, ...args: unknown[]) { + const first = args[0] as number | { port?: number } | undefined; + if ((typeof first === "object" ? first?.port : first) === port) sockets.add(this); + return Reflect.apply(connect, this, args); + } as typeof net.Socket.prototype.connect; + + const totals = { statements: 0, transactions: 0, waits: 0, rows: 0, sqlBytes: 0 }; + let inFlight = 0; + let log: MeteredStatement[] | null = null; + const visited = new WeakSet(); + const visit = (value: unknown, inTransaction: boolean) => { + if ((typeof value !== "object" || value === null) && typeof value !== "function") return; + if (visited.has(value)) return; + visited.add(value); + const target = value as Record; + const unsafe = target.unsafe; + if (typeof unsafe === "function") { + target.unsafe = function (this: unknown, ...args: unknown[]) { + const query = String(args[0]), started = performance.now(); + totals.statements += 1; + totals.sqlBytes += Buffer.byteLength(query); + if (inFlight === 0) totals.waits += 1; + inFlight += 1; + const pending = Reflect.apply(unsafe, this, args) as PromiseLike<{ length: number }>; + const entry = { sql: query, parameters: Array.isArray(args[1]) ? args[1].length : 0, rows: 0, ms: 0, inTransaction }; + log?.push(entry); + // The query starts one microtask after its first `then`, so a caller + // can still select the result format on the returned query. + pending.then((result) => { + inFlight -= 1; + totals.rows += result.length; + entry.rows = result.length; + entry.ms = performance.now() - started; + }, () => { inFlight -= 1; }); + return pending; + }; + } + for (const method of ["begin", "savepoint"]) { + const original = target[method]; + if (typeof original !== "function") continue; + target[method] = function (this: unknown, ...args: unknown[]) { + // The driver sends BEGIN through `unsafe`, which counts it above. It + // sends COMMIT, ROLLBACK and SAVEPOINT on its own, one for each call. + totals.statements += 1; + totals.waits += 1; + if (method === "begin") totals.transactions += 1; + return Reflect.apply(original, this, args.map(arg => typeof arg !== "function" ? arg + : function (this: unknown, ...inner: unknown[]) { + visit(inner[0], true); + return Reflect.apply(arg, this, inner); + })); + } satisfies Method; + } + }; + visit(db.$client, false); + + const bytesReceived = () => [...sockets].reduce((sum, socket) => sum + socket.bytesRead, 0); + return { + /** The totals since the client started. Subtract two readings to meter one request. */ + read: () => ({ ...totals, bytesReceived: bytesReceived() }), + /** Records every statement until `stopLog`. */ + startLog: () => { log = []; }, + stopLog: () => { + const recorded = log ?? []; + log = null; + return recorded; + }, + uninstall: () => { net.Socket.prototype.connect = connect as typeof net.Socket.prototype.connect; }, + }; +} diff --git a/server/src/services/attention.ts b/server/src/services/attention.ts index 868d80cc127..561e07d56bf 100644 --- a/server/src/services/attention.ts +++ b/server/src/services/attention.ts @@ -185,6 +185,11 @@ type AttentionListOptions = AttentionFeedQuery & { excludeSources?: readonly AttentionSourceKind[]; /** Count the canonical complete set without card bodies or presentation reads. */ summaryOnly?: boolean; + /** + * `summaryOnly` for a caller that reads the counts and not the items: it + * also skips the reads that only say which task a card belongs to. + */ + countsOnly?: boolean; }; type AttentionServiceOptions = { @@ -641,10 +646,17 @@ function parseActivityBoundary(value: string | undefined, field: "activitySince" return parsed; } -async function enrichAttentionItems(db: Db, companyId: string, items: AttentionItem[], now: number, includePresentation = true) { +async function enrichAttentionItems( + db: Db, + companyId: string, + items: AttentionItem[], + now: number, + includePresentation = true, + includeQueues = true, +) { if (items.length === 0) return items; const sourceIds = [...new Set(items.map((item) => item.subject.id))]; - const queueRows = await db + const queueRows = !includeQueues ? [] : await db .select({ sourceKind: decisionQueueItems.sourceKind, sourceId: decisionQueueItems.sourceId, @@ -929,9 +941,41 @@ async function issueSummaryMap( issueIds: Array, readCondition: SQL = sql`true`, includeConversations = false, + /** False for a count: the same tasks, without their project and workspace. */ + includeContext = true, ) { const ids = [...new Set(issueIds.filter((value): value is string => Boolean(value)))]; if (ids.length === 0) return new Map(); + const readable = and( + eq(issues.companyId, companyId), + inArray(issues.id, ids), + attentionIssueCondition(includeConversations), + readCondition, + ); + if (!includeContext) { + const subjectRows = await db + .select({ + id: issues.id, + companyId: issues.companyId, + identifier: issues.identifier, + title: issues.title, + status: issues.status, + priority: issues.priority, + reviewPolicy: issues.reviewPolicy, + assigneeAgentId: issues.assigneeAgentId, + assigneeUserId: issues.assigneeUserId, + createdAt: issues.createdAt, + updatedAt: issues.updatedAt, + }) + .from(issues) + .where(readable); + return new Map(subjectRows.map((row) => [row.id, { + ...row, + reviewPolicy: row.reviewPolicy ?? null, + project: null, + workspace: null, + }])); + } const rows = await db .select({ id: issues.id, @@ -958,13 +1002,8 @@ async function issueSummaryMap( eq(issues.projectWorkspaceId, projectWorkspaces.id), eq(projectWorkspaces.companyId, companyId), )) - .where(and( - eq(issues.companyId, companyId), - inArray(issues.id, ids), - attentionIssueCondition(includeConversations), - readCondition, - )); - return new Map(rows.map((row) => [row.id, { + .where(readable); + return new Map(rows.map((row) => [row.id, { id: row.id, companyId: row.companyId, identifier: row.identifier, @@ -1688,9 +1727,14 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions const prefix = companyConfig.prefix; const includeDismissed = options.includeDismissed === true; const now = serviceOptions.now?.() ?? Date.now(); - const summaryOnly = options.summaryOnly === true; + const countsOnly = options.countsOnly === true; + const summaryOnly = options.summaryOnly === true || countsOnly; const loadImages = (...args: Parameters) => summaryOnly ? Promise.resolve(new Map()) : issueImageMap(...args); + // The tasks of these ids that the reader can see in the feed. A count + // needs the same answer, without each task's project and workspace. + const loadIssues = (issueIds: Array, includeConversations = false) => + issueSummaryMap(db, companyId, issueIds, readCondition, includeConversations, !summaryOnly); const view: AttentionFeedView = options.view ?? "all"; const collected: AttentionItem[] = []; // Raw audience inputs and important-change facts captured per item during @@ -1731,12 +1775,7 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions .where(and(eq(approvals.companyId, companyId), eq(approvals.status, "pending"), approvalExecutionIssueCondition())) .orderBy(desc(approvals.updatedAt), desc(approvals.id)); - const readableApprovalIssues = await issueSummaryMap( - db, - companyId, - pendingApprovals.map((row) => row.linkedIssueId), - readCondition, - ); + const readableApprovalIssues = await loadIssues(pendingApprovals.map((row) => row.linkedIssueId)); for (const approval of pendingApprovals) { const linkedIssueId = approval.linkedIssueId ?? null; @@ -1854,7 +1893,7 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions const [interactionIssueMap, interactionImageMap, interactionPlanDocumentMap] = await Promise.all([ // A pending human request remains actionable in a persistent chat even // though its idle container is intentionally absent from work queues. - issueSummaryMap(db, companyId, visibleInteractionRows.map((row) => row.issueId), readCondition, true), + loadIssues(visibleInteractionRows.map((row) => row.issueId), true), loadImages(db, companyId, visibleInteractionRows.map((row) => row.issueId), readCondition), summaryOnly ? Promise.resolve(new Map()) : planDocumentMap(db, companyId, visibleInteractionRows.map((row) => row.issueId), readCondition), @@ -1943,8 +1982,9 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions const bundleIds = [...new Set(openDecisions.map((decision) => decision.bundleId).filter((value): value is string => Boolean(value)))]; const bundleTitleMap = new Map(); const [decisionIssueMap, bundleRows] = await Promise.all([ - issueSummaryMap(db, companyId, openDecisions.map((decision) => decision.originIssueId), readCondition), - bundleIds.length > 0 + loadIssues(openDecisions.map((decision) => decision.originIssueId)), + // A count does not show the title of a bundle. + bundleIds.length > 0 && !summaryOnly ? db.select({ id: decisionBundles.id, title: decisionBundles.title }) .from(decisionBundles).where(and(eq(decisionBundles.companyId, companyId), inArray(decisionBundles.id, bundleIds))) : Promise.resolve([]), @@ -2057,12 +2097,7 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions )) .orderBy(desc(issueRecoveryActions.updatedAt), desc(issueRecoveryActions.id)); const [recoveryIssueMap, recoveryImageMap] = await Promise.all([ - issueSummaryMap( - db, - companyId, - recoveryRows.flatMap((row) => [row.sourceIssueId, row.recoveryIssueId]), - readCondition, - ), + loadIssues(recoveryRows.flatMap((row) => [row.sourceIssueId, row.recoveryIssueId])), loadImages(db, companyId, recoveryRows.map((row) => row.sourceIssueId), readCondition), ]); @@ -2123,7 +2158,16 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions }); } - const blockedIssues = await issueService(db).list(companyId, { status: "blocked", includeBlockedBy: true, readCondition }); + // A count needs the blocked tasks that the task list returns, in the + // order of the list, and the blocker attention of each one. It asks the + // list for those rows alone and classifies them with the function that + // the full list calls. + const blockedIssues = summaryOnly + ? await issueService(db).listBlockerAttentionRoots(companyId, { status: "blocked", readCondition }) + : await issueService(db).list(companyId, { status: "blocked", includeBlockedBy: true, readCondition }); + const countedBlockerAttention = summaryOnly + ? await issueService(db).listBlockerAttention(companyId, blockedIssues) + : null; type BlockedAttentionIssue = IssueSubjectRow & { blockerAttention?: { state?: string; @@ -2136,19 +2180,30 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions blockedTransitionAt?: Date | null; }; const typedBlockedIssues = blockedIssues as BlockedAttentionIssue[]; + const blockerAttentionOf = (issue: BlockedAttentionIssue) => countedBlockerAttention + ? countedBlockerAttention.get(issue.id) + : issue.blockerAttention; const terminalBlockerIssueIds = typedBlockedIssues - .map((issue) => issue.blockerAttention?.terminalBlockerIssueId) + .map((issue) => blockerAttentionOf(issue)?.terminalBlockerIssueId) .filter((issueId): issueId is string => Boolean(issueId)); const [blockedIssueSummaries, terminalBlockerSummaries, blockerImageMap, blockingIssues] = await Promise.all([ - issueSummaryMap(db, companyId, blockedIssues.map((issue) => issue.id), readCondition), - issueSummaryMap(db, companyId, terminalBlockerIssueIds, readCondition), + // Each blocked task as its card shows it, and as the task of the + // card of its blocker. A count reads neither. + countsOnly + ? Promise.resolve(new Map()) + : loadIssues(blockedIssues.map((issue) => issue.id)), + loadIssues(terminalBlockerIssueIds), loadImages( db, companyId, [...blockedIssues.map((issue) => issue.id), ...terminalBlockerIssueIds], readCondition, ), - blockingIssueMap(db, companyId, blockedIssues.map((issue) => issue.id), readCondition), + // The blocker a card names and, below, the number of tasks a blocker + // holds are card text. + summaryOnly + ? Promise.resolve(new Map()) + : blockingIssueMap(db, companyId, blockedIssues.map((issue) => issue.id), readCondition), ]); const terminalCandidates = new Map() + : await blockedWorkCountMap(db, companyId, [...terminalCandidates.keys()]); for (const [terminalIssueId, candidate] of terminalCandidates) { const blockedTaskCount = blockedWorkCounts.get(terminalIssueId) ?? 0; const taskLabel = blockedTaskCount === 1 ? "task" : "tasks"; @@ -2276,6 +2333,9 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions eq(issues.status, "in_review"), executionIssueCondition(), readCondition, + // A count does not read each review again as a card (below), so this + // read applies the card's task condition itself. + summaryOnly ? attentionIssueCondition() : undefined, )) .orderBy(desc(issues.updatedAt), desc(issues.id)); const reviewIssueIds = reviewRows.map((row) => row.id); @@ -2300,7 +2360,9 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions } const [reviewAttentionByIssueId, reviewIssueMap, reviewImageMap] = await Promise.all([ issueService(db).listReviewAttention(companyId, reviewRows), - issueSummaryMap(db, companyId, reviewIssueIds, readCondition), + summaryOnly + ? Promise.resolve(new Map(reviewRows.map((row) => [row.id, row]))) + : issueSummaryMap(db, companyId, reviewIssueIds, readCondition), loadImages(db, companyId, reviewIssueIds, readCondition), ]); @@ -2380,12 +2442,7 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions const failedRows = await listAttentionExhaustedRuns(db, companyId); const failedIssueIds = failedRows.map((row) => readRunIssueId(row.contextSnapshot)); const [failedIssueMap, failedImageMap, failedRunsWithNewerRun] = await Promise.all([ - issueSummaryMap( - db, - companyId, - failedIssueIds, - readCondition, - ), + loadIssues(failedIssueIds), loadImages(db, companyId, failedIssueIds, readCondition), listFailedRunsWithNewerRun(db, companyId, failedRows.map((row, index) => ({ id: row.id, @@ -2458,8 +2515,12 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions }, issueId ? { key: `blocked:${issueId}`, anchor: false } : null); } - const budgetOverview = await budgetService(db).overview(companyId); - for (const incident of budgetOverview.activeIncidents) { + // A count reads the open incidents of the budget overview without the + // spend of every policy, which the overview also sums. + const openBudgetIncidents = summaryOnly + ? await budgetService(db).openIncidents(companyId) + : (await budgetService(db).overview(companyId)).activeIncidents; + for (const incident of openBudgetIncidents) { const observedPercent = budgetObservedPercent(incident.amountObserved, incident.amountLimit); if (incident.thresholdType !== "hard" && observedPercent < ATTENTION_BUDGET_WARNING_PERCENT) continue; const dedupKey = `budget:${incident.policyId}:${toIso(incident.windowStart)}:${incident.thresholdType}`; @@ -2641,6 +2702,8 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions // the fingerprint below reads the obligation's audience. assignAttentionObligations(currentItems, obligationHintByDedupKey); for (const item of currentItems) { + // A count reads a fingerprint only to compare it with a dismissal. + if (summaryOnly && !dismissals.has(item.dismissalKey)) continue; const change = changeBasisByDedupKey.get(item.dedupKey) ?? { content: null, since: item.createdAt }; item.changeFingerprint = attentionChangeFingerprint({ sourceKind: item.sourceKind, @@ -2656,11 +2719,14 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions // "Transferred by @x" on a card that reached its person through a // transfer (PAP-749 plan ยง6, Rule 12). - await attachTransferredBy(db, companyId, currentItems); + if (!summaryOnly) await attachTransferredBy(db, companyId, currentItems); const collectedItems = currentItems.sort(compareAttentionItems); await decisionQueueService(db).materializeSeededQueues(companyId, collectedItems); - const enrichedItems = await enrichAttentionItems(db, companyId, collectedItems, now, !summaryOnly); + // Queue membership filters a feed by queue and labels its cards. A + // count without a queue filter does not read it. + const enrichedItems = await enrichAttentionItems(db, companyId, collectedItems, now, !summaryOnly, + !summaryOnly || Boolean(options.queue?.trim())); const activitySince = parseActivityBoundary(options.activitySince, "activitySince"); const activityUntil = parseActivityBoundary(options.activityUntil, "activityUntil"); @@ -2815,7 +2881,7 @@ export function attentionService(db: Db, serviceOptions: AttentionServiceOptions }, summary: async (companyId: string, options: Pick = {}): Promise => { const { items: _items, nextCursor: _cursor, ...summary } = await list(companyId, { - ...options, all: true, allowUnscopedAll: true, summaryOnly: true, + ...options, all: true, allowUnscopedAll: true, countsOnly: true, }); return summary; }, diff --git a/server/src/services/budgets.ts b/server/src/services/budgets.ts index 66cc017392d..76cda656eba 100644 --- a/server/src/services/budgets.ts +++ b/server/src/services/budgets.ts @@ -684,6 +684,14 @@ export function budgetServiceInTransaction(db: Db, publications: ActivityPublica }); } + async function listOpenIncidents(companyId: string): Promise { + return hydrateIncidentRows(await db + .select() + .from(budgetIncidents) + .where(and(eq(budgetIncidents.companyId, companyId), eq(budgetIncidents.status, "open"))) + .orderBy(desc(budgetIncidents.createdAt))); + } + async function reconcileScope(companyId: string, scopeType: BudgetScopeType, scopeId: string, prepared?: { scope: ScopeRecord; policies: PolicyRow[]; observations: ObservedSpend[] }) { const scope = prepared?.scope ?? await resolveScopeRecord(db, scopeType, scopeId); @@ -982,15 +990,13 @@ export function budgetServiceInTransaction(db: Db, publications: ActivityPublica return buildPolicySummary(row); }, + /** The `activeIncidents` of `overview`, without the spend of every policy. */ + openIncidents: listOpenIncidents, + overview: async (companyId: string): Promise => { const rows = await listPolicyRows(companyId); const policies = await mapBudgetReads(rows, buildPolicySummary); - const activeIncidentRows = await db - .select() - .from(budgetIncidents) - .where(and(eq(budgetIncidents.companyId, companyId), eq(budgetIncidents.status, "open"))) - .orderBy(desc(budgetIncidents.createdAt)); - const activeIncidents = await hydrateIncidentRows(activeIncidentRows); + const activeIncidents = await listOpenIncidents(companyId); return { companyId, policies, @@ -1373,6 +1379,8 @@ export function budgetService(db: Db, _hooks: BudgetServiceHooks = {}) { tx => budgetServiceInTransaction(tx).listPolicies(companyId)), overview: (companyId: string) => withAccountingReadSnapshot(db, companyId, tx => budgetServiceInTransaction(tx).overview(companyId)), + openIncidents: (companyId: string) => withAccountingReadSnapshot(db, companyId, + tx => budgetServiceInTransaction(tx).openIncidents(companyId)), evaluateAdmission: (companyId: string, agentId: string | null, context?: AdmissionContext) => withAccountingReadSnapshot(db, companyId, tx => budgetServiceInTransaction(tx).evaluateAdmission(companyId, agentId, context)), evaluateAffectedScopes: (companyId: string, agentId: string | null, projectId: string | null) => diff --git a/server/src/services/issues.ts b/server/src/services/issues.ts index 13cbda154d4..e6f3455bf8f 100644 --- a/server/src/services/issues.ts +++ b/server/src/services/issues.ts @@ -1875,6 +1875,8 @@ export type IssueListPhase = export interface IssueListOptions { /** Wraps each internal read so callers can report per-phase timing. */ timePhase?: (phase: IssueListPhase, read: () => Promise) => Promise; + /** Internal to `listBlockerAttentionRoots`: return `IssueBlockerAttentionRoot` rows. */ + rows?: "blocker_attention_roots"; } const untimedIssueListPhase = (_phase: IssueListPhase, read: () => Promise) => read(); @@ -3507,6 +3509,24 @@ type IssueBlockerAttentionInputNode = Pick< | "assigneeUserId" > & { executionRunId?: string | null }; +const blockerAttentionRootSelect = { + id: issues.id, + companyId: issues.companyId, + parentId: issues.parentId, + identifier: issues.identifier, + title: issues.title, + status: issues.status, + priority: issues.priority, + assigneeAgentId: issues.assigneeAgentId, + assigneeUserId: issues.assigneeUserId, + executionRunId: issues.executionRunId, + unblockDescriptor: issues.unblockDescriptor, + blockedTransitionAt: issues.blockedTransitionAt, + createdAt: issues.createdAt, + updatedAt: issues.updatedAt, +}; +export type IssueBlockerAttentionRoot = Pick; + type IssueBlockerAttentionEdge = { issueId: string; blockerIssueId: string; @@ -4716,6 +4736,31 @@ function reviewAttentionNone(): IssueReviewAttention { return { state: "none", paths: [], reason: null }; } +// What the review classification reads of a task. It never reads the +// description, so a request for many reviews does not load every task body. +const reviewAttentionIssueSelect = { + id: issues.id, + companyId: issues.companyId, + identifier: issues.identifier, + title: issues.title, + status: issues.status, + projectId: issues.projectId, + goalId: issues.goalId, + parentId: issues.parentId, + assigneeAgentId: issues.assigneeAgentId, + assigneeUserId: issues.assigneeUserId, + createdByAgentId: issues.createdByAgentId, + createdByUserId: issues.createdByUserId, + conversationAgentId: issues.conversationAgentId, + conversationUserId: issues.conversationUserId, + conversationState: issues.conversationState, + executionPolicy: issues.executionPolicy, + executionState: issues.executionState, + monitorNextCheckAt: issues.monitorNextCheckAt, + monitorAttemptCount: issues.monitorAttemptCount, + updatedAt: issues.updatedAt, +}; + async function listIssueReviewAttentionMap( dbOrTx: any, companyId: string, @@ -4730,14 +4775,14 @@ async function listIssueReviewAttentionMap( .map((row) => row.id); if (reviewIds.length === 0) return result; - const reviewIssues: IssueRow[] = []; + const reviewIssues: Array> = []; for (const chunk of chunkList( reviewIds, ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE, )) { reviewIssues.push( ...(await dbOrTx - .select() + .select(reviewAttentionIssueSelect) .from(issues) .where( and(eq(issues.companyId, companyId), inArray(issues.id, chunk), nonIdleSlackIssueCondition()), @@ -8679,6 +8724,13 @@ export function issueService(db: Db) { offset > 0 ? limit === undefined ? query.offset(offset) : query.limit(limit).offset(offset) : limit === undefined ? query : query.limit(limit); + if (options?.rows === "blocker_attention_roots") { + // The tasks and the order of the full rows below. Only + // `listBlockerAttentionRoots` asks for this page, and it states the type. + return paginate( + db.select(blockerAttentionRootSelect).from(issues).where(and(...conditions)).orderBy(...orderBy).$dynamic(), + ) as never; + } // Phase 1 of the participation path: the page's ids in final order. The // select-list subqueries below then run only for these rows. const pageIds = participationCandidates @@ -9730,6 +9782,21 @@ export function issueService(db: Db) { return listIssueBlockerAttentionMap(dbOrTx, companyId, issueRows); }, + /** + * The tasks that `list` returns for a status, in the order of `list`, as + * the rows that `listBlockerAttention` and the attention feed read. A + * count of blocked work uses it: it must classify the same tasks in the + * same order as the task list, and it has no use for their descriptions, + * labels, runs, activity or review attention. + */ + listBlockerAttentionRoots: async ( + companyId: string, + filters: { status: string; readCondition?: SQL }, + ): Promise => { + const roots: unknown = await service.list(companyId, filters, { rows: "blocker_attention_roots" }); + return roots as IssueBlockerAttentionRoot[]; + }, + listReviewAttention: async ( companyId: string, issueRows: IssueReviewAttentionInput[],