diff --git a/server/src/__tests__/attention-retention-work.test.ts b/server/src/__tests__/attention-retention-work.test.ts index fbbae5a232e..e13d8a1931c 100644 --- a/server/src/__tests__/attention-retention-work.test.ts +++ b/server/src/__tests__/attention-retention-work.test.ts @@ -290,6 +290,54 @@ const delay = DEFAULT_DECISION_ARCHIVE_DAYS * 86_400_000; expect(bytes("issues")).toBeLessThan(60_000); }); + it("reads blocker retention state once per page, not once per blocked task", async () => { + const blockers = await db.insert(issues).values(Array.from({ length: 160 }, () => ({ companyId, title: randomUUID(), + status: "todo", createdByAgentId: agentId, createdAt: old(), updatedAt: old() }))).returning({ id: issues.id }); + const blocked = await db.insert(issues).values(blockers.map(() => ({ companyId, title: randomUUID(), status: "blocked", + createdByAgentId: agentId, createdAt: old(), updatedAt: old() }))).returning({ id: issues.id }); + await db.insert(issueRelations).values(blocked.map((row, index) => ({ companyId, issueId: blockers[index].id, + relatedIssueId: row.id, type: "blocks" as const }))); + const at = old(); + const kept = blockers.slice(0, 40), archived = blockers.slice(40, 80), plain = blockers.slice(80, 120), missing = blockers.slice(120); + await db.insert(decisionRetention).values([ + ...kept.map(row => ({ companyId, sourceKind: "blocker_attention", sourceId: row.id, sourceActivityAt: at, keep: true })), + ...archived.map(row => ({ companyId, sourceKind: "blocker_attention", sourceId: row.id, sourceActivityAt: at, archivedAt: new Date() })), + ...plain.map(row => ({ companyId, sourceKind: "blocker_attention", sourceId: row.id, sourceActivityAt: at })), + ]); + const service = attentionRetentionService(db), scope = { companyId, sourceKind: "blocker_attention" as const }; + const observed = createDb(externalUrl ?? temporary!.connectionString), events: Array> = [], now = Date.now(); + const capture = createFullDatabaseResponseCapture({ start: now - 100, end: now + 59_000 }, event => events.push(event)); + installDatabaseResponseCapture(observed, { decisionRetention }, capture); + const retentionReads: string[] = []; + observed.$client.options.debug = (_connection, query) => { if (query.includes('from "decision_retention"')) retentionReads.push(query); }; + const entries: Array<{ id: string; sourceId: string; rootId: string; variant?: string }> = []; + let pages = 0; + try { + const observedService = attentionRetentionService(observed); + for (; pages < 4; pages++) { + const loaded = await observedService.load(scope); + entries.push(...(loaded.rows ?? [])); + if (loaded.complete) { pages++; break; } + } + capture.flush(); + } finally { capture.flush(); await observed.$client.end(); } + const retentionBytes = events.filter(event => event.table === "decision_retention") + .reduce((total, event) => total + Number(event.responseValueBytes), 0); + expect(retentionBytes).toBeLessThan(10_000); + expect(retentionReads.length).toBeLessThanOrEqual(pages); + expect(retentionReads.every(query => !query.includes('"source_activity_at"'))).toBe(true); + const listed = new Set(entries.filter(entry => entry.variant === "terminal").map(entry => entry.sourceId)); + expect(listed).toEqual(new Set([...plain, ...missing].map(row => row.id))); + const byRoot = new Map(blocked.map((row, index) => [row.id, blockers[index].id])); + for (const root of [blocked[0].id, blocked[40].id]) { + const candidate = { sourceKind: "blocker_attention" as const, sourceId: root, companyId, activityAt: at.toISOString(), + deadlineAt: Number.MAX_SAFE_INTEGER, issueId: root, id: `blocker_attention:${root}:terminal`, rootId: root, + variant: "terminal" as const, dueAt: Number.MAX_SAFE_INTEGER }; + expect(byRoot.get(root)).toBeTruthy(); + expect(await service.check(candidate)).toBeNull(); + } + }); + it("batches 100 failed runs and rechecks late visibility and supersession before archive", async () => { const origins = await db.insert(issues).values(Array.from({ length: 100 }, () => ({ companyId, title: randomUUID(), status: "todo", updatedAt: old() }))).returning(); @@ -332,7 +380,7 @@ const delay = DEFAULT_DECISION_ARCHIVE_DAYS * 86_400_000; const queries = [...statements]; expect(page.rows).toHaveLength(200); expect(page.complete).toBe(false); const retentionReads = queries.filter(query => query.includes('from "decision_retention"')); - expect(retentionReads).toHaveLength(200); + expect(retentionReads).toHaveLength(1); expect(queries.length - retentionReads.length).toBeLessThanOrEqual(30); const terminal = page.rows!.find(entry => entry.rootId === roots[99].id && entry.variant === "terminal")!; expect(terminal.sourceId).toBe(terminals[99].id); diff --git a/server/src/services/attention-retention.ts b/server/src/services/attention-retention.ts index 930d27eeba6..8f980f785d8 100644 --- a/server/src/services/attention-retention.ts +++ b/server/src/services/attention-retention.ts @@ -44,6 +44,7 @@ type PageEvidence = { newerRuns: Set; deferredRoots: Set; blockers: Awaited["listBlockerAttention"]>>; + blockerRetention: Map; }; const sourceTables: Record = { @@ -247,11 +248,18 @@ export function attentionRetentionService(db: Db) { const terminalIds = [...blockers.values()].filter(terminalBlockerRequiresAttention) .map(attention => attention.terminalBlockerIssueId).filter((id): id is string => id != null && !visible.has(id)); for (const row of await readIssues(terminalIds)) visible.set(row.id, row); + const retentionIds = [...new Set([...blockerIds, ...[...blockers.values()].filter(terminalBlockerRequiresAttention) + .map(attention => attention.terminalBlockerIssueId).filter((id): id is string => id != null)])]; + const blockerRetention = new Map(retentionIds.length ? (await database.select({ sourceId: decisionRetention.sourceId, + keep: decisionRetention.keep, archivedAt: decisionRetention.archivedAt }).from(decisionRetention).where(and( + eq(decisionRetention.companyId, companyId), eq(decisionRetention.sourceKind, "blocker_attention"), + sql`${decisionRetention.sourceId} in (select jsonb_array_elements_text(${JSON.stringify(retentionIds)}::jsonb))`))) + .map(row => [row.sourceId, { keep: row.keep, archivedAt: row.archivedAt }]) : []); result.set(companyId, { decisions: new Map(decisionRows.map(row => [row.id, row])), recoveries: new Map(recoveryRows.map(row => [row.id, row])), runs: new Map(runRows.map(row => [row.id, row])), agents: new Set(runAgents.map(row => row.id)), visibleIssues: visible, newerRuns: await listFailedRunsWithNewerRun(database, companyId, - runRows), blockers, deferredRoots: blockerBatch.deferredRootIds, + runRows), blockers, deferredRoots: blockerBatch.deferredRootIds, blockerRetention, }); } return result; @@ -307,8 +315,9 @@ export function attentionRetentionService(db: Db) { where company_id = ${candidate.companyId}::uuid and id = ${sourceId}::uuid`) as unknown as Array<{ at: string; due: string }>)[0]; activityAt = activity.at; dueAt = Number(activity.due); } - const [retention] = await database.select().from(decisionRetention).where(and(eq(decisionRetention.companyId, candidate.companyId), - eq(decisionRetention.sourceKind, candidate.sourceKind), eq(decisionRetention.sourceId, sourceId))); + const retention = evidence ? evidence.blockerRetention.get(sourceId) : (await database.select({ keep: decisionRetention.keep, + archivedAt: decisionRetention.archivedAt }).from(decisionRetention).where(and(eq(decisionRetention.companyId, candidate.companyId), + eq(decisionRetention.sourceKind, candidate.sourceKind), eq(decisionRetention.sourceId, sourceId))))[0]; if (retention?.keep || retention?.archivedAt) return null; } else if (candidate.sourceKind === "failed_run") { const run: RunProbe | undefined = evidence ? evidence.runs.get(sourceId) : (await database.select(runProbeColumns).from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, candidate.companyId), eq(heartbeatRuns.id, sourceId))))[0]; diff --git a/server/src/services/native-runtime/native-provider-capacity-retry.ts b/server/src/services/native-runtime/native-provider-capacity-retry.ts index 334e5880226..a472054ee17 100644 --- a/server/src/services/native-runtime/native-provider-capacity-retry.ts +++ b/server/src/services/native-runtime/native-provider-capacity-retry.ts @@ -1,4 +1,4 @@ -import { notifyAttentionRetentionWork } from "../retention-work-notifications.js"; +import { notifyIssueAttentionRetentionWork } from "../retention-work-notifications.js"; import { notifyScheduledRetryDispatch } from "../run-dispatch-notifications.js"; import { randomUUID } from "node:crypto"; import { and, eq } from "drizzle-orm"; @@ -51,7 +51,7 @@ export async function materializeNativeProviderCapacityRetry(input: { scheduledRetryAt: dueAt.toISOString(), executionRetryAccounting: accountingForScheduledRetry(run, NATIVE_PROVIDER_OVERLOADED_CODE, attempt), }; - await notifyAttentionRetentionWork(input.tx, { companyId: input.companyId }); + await notifyIssueAttentionRetentionWork(input.tx, { companyId: input.companyId, issueId: input.issueId, runId: run.id }); await notifyScheduledRetryDispatch(input.tx, { companyId: run.companyId, runId: run.id }); const [wake] = await input.tx.insert(agentWakeupRequests).values({ companyId: run.companyId, agentId: run.agentId, source: "automation", triggerDetail: "system", diff --git a/server/src/services/native-runtime/native-review-participant.ts b/server/src/services/native-runtime/native-review-participant.ts index b51627d1352..ccd832f3ecb 100644 --- a/server/src/services/native-runtime/native-review-participant.ts +++ b/server/src/services/native-runtime/native-review-participant.ts @@ -1,5 +1,5 @@ import { notifyStatusCardIssueChange } from "../status-card-notifications.js"; -import { notifyAttentionRetentionWork } from "../retention-work-notifications.js"; +import { notifyIssueAttentionRetentionWork } from "../retention-work-notifications.js"; import { agents, heartbeatRuns, @@ -243,7 +243,7 @@ export async function claimNativeReviewExecutionLock(db: Db, input: { || admitted.nativeReviewInteractionId !== expected.nativeReviewInteractionId || admitted.nativeReviewDecisionId !== expected.nativeReviewDecisionId) return false; await notifyStatusCardIssueChange(tx, input.companyId); - await notifyAttentionRetentionWork(tx, { companyId: input.companyId }); + await notifyIssueAttentionRetentionWork(tx, { companyId: input.companyId, issueId: input.issueId, runId: input.runId }); const claimed = await tx.update(issues).set({ executionRunId: input.runId, executionAgentNameKey: input.agentNameKey, diff --git a/server/src/services/native-runtime/native-workspace-reconstruction.test.ts b/server/src/services/native-runtime/native-workspace-reconstruction.test.ts index 8afb9820bee..08b26099219 100644 --- a/server/src/services/native-runtime/native-workspace-reconstruction.test.ts +++ b/server/src/services/native-runtime/native-workspace-reconstruction.test.ts @@ -161,6 +161,18 @@ describe("reconstruction after confirmed native workspace loss", () => { } finally { unsubscribe(); unsubscribeRoutine(); } }); + it("scopes the attention retention hint of a reconstruction to the task graph", async () => { + const f = await seed(); + const hints: string[] = []; + const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.attentionRetention, (event, values) => { + if (event === "settled") hints.push(...values ?? []); + }); + try { await db.transaction(tx => reconstructNativeWorkspace({ ...f, db: tx as unknown as Db })); } finally { unsubscribe(); } + expect(hints).not.toContain(JSON.stringify({ companyId: f.companyId })); + expect(hints.some(hint => (JSON.parse(hint) as { issueIds?: string[]; issueId?: string }).issueIds?.includes(f.issueId) + || (JSON.parse(hint) as { issueId?: string }).issueId === f.issueId)).toBe(true); + }); + it("pins a historical cancelled workspace through public heartbeat startup without replaying its turn", async () => { const f = await seed(); const later = await interveningCancelledRun(f, false); diff --git a/server/src/services/native-runtime/native-workspace-reconstruction.ts b/server/src/services/native-runtime/native-workspace-reconstruction.ts index aa20b852e14..9d95f5b22db 100644 --- a/server/src/services/native-runtime/native-workspace-reconstruction.ts +++ b/server/src/services/native-runtime/native-workspace-reconstruction.ts @@ -1,5 +1,5 @@ import { notifyStatusCardIssueChange } from "../status-card-notifications.js"; -import { notifyAttentionRetentionWork } from "../retention-work-notifications.js"; +import { notifyIssueAttentionRetentionWork } from "../retention-work-notifications.js"; import { notifyRoutineExecutionStatus } from "../routine-execution-status-notifications.js"; import { DELIVERY_QUEUES, notifyDeliveryWork } from "../delivery-work-notifications.js"; import { notifyUnsafeWorkspaceExportRecovery } from "../unsafe-workspace-export-notifications.js"; @@ -183,7 +183,7 @@ export async function reconstructNativeWorkspace(input: { ...(source.interveningExecutions.length ? { interveningExecutions: source.interveningExecutions } : {}) } }; await markExecutionReconciliation(db, source.action, decision, input.actorId); const now = new Date(); - await notifyAttentionRetentionWork(tx, { companyId: input.companyId }); + await notifyIssueAttentionRetentionWork(tx, { companyId: input.companyId, issueId: input.issueId }); await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.issueId }); await tx.update(issueRecoveryActions).set({ status: "resolved", outcome: "handed_back", resolvedAt: now, updatedAt: now, resolutionNote: "workspace_reconstruction_authorized", nextAction: "Continue from saved sources. The original unexported edits are lost; verify the selected attachments before rebuilding. Do not repeat completed external actions or reset the original work limits.",