diff --git a/server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts b/server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts index 508e7124061..b1300e679fb 100644 --- a/server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts +++ b/server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts @@ -467,6 +467,9 @@ function createRunContextDb( }), })), insert: vi.fn(() => ({ values: vi.fn(async () => undefined) })), + update: vi.fn(() => ({ + set: vi.fn(() => ({ where: vi.fn(async () => undefined) })), + })), }; return dbStub; } @@ -1892,6 +1895,72 @@ describe("agent issue mutation checkout ownership", () => { ); }); + it("keeps a committed blocked PATCH successful when the owner wake fails", async () => { + const existing = makeIssue({ + status: "in_progress", + assigneeAgentId: ownerAgentId, + blockedOwnerNotifiedAt: null, + }); + mockIssueService.getById.mockResolvedValue(existing); + mockIssueService.update.mockImplementation(async (_id: string, patch: Record) => ({ + ...existing, + ...patch, + blockedTransitionAt: new Date(), + })); + mockHeartbeatService.wakeup.mockRejectedValueOnce(new Error("wake dispatch failed")); + const db = createRunContextDb(); + + const res = await request(await createApp(ownerActor(), db)).patch(`/api/issues/${issueId}`).send({ + status: "blocked", + unblockDescriptor: { owner: { agentId: ownerAgentId }, action: "Review the blocker" }, + }); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(res.body).toMatchObject({ + status: "blocked", + blockedOwnerNotifiedAt: null, + unblockDescriptor: { owner: { agentId: ownerAgentId }, action: "Review the blocker" }, + }); + expect(mockHeartbeatService.wakeup).toHaveBeenCalledOnce(); + expect(db.update).not.toHaveBeenCalled(); + }); + + it("keeps a committed blocked PATCH successful when the notification marker write fails", async () => { + const existing = makeIssue({ + status: "in_progress", + assigneeAgentId: ownerAgentId, + blockedOwnerNotifiedAt: null, + }); + mockIssueService.getById.mockResolvedValue(existing); + mockIssueService.update.mockImplementation(async (_id: string, patch: Record) => ({ + ...existing, + ...patch, + blockedTransitionAt: new Date(), + })); + const db = createRunContextDb(); + db.update.mockImplementationOnce(() => ({ + set: vi.fn(() => ({ + where: vi.fn(async () => { + throw new Error("delivery marker write failed"); + }), + })), + })); + + const res = await request(await createApp(ownerActor(), db)).patch(`/api/issues/${issueId}`).send({ + status: "blocked", + unblockDescriptor: { owner: { agentId: ownerAgentId }, action: "Review the blocker" }, + }); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(res.body).toMatchObject({ + status: "blocked", + blockedOwnerNotifiedAt: null, + unblockDescriptor: { owner: { agentId: ownerAgentId }, action: "Review the blocker" }, + }); + expect(mockHeartbeatService.wakeup).toHaveBeenCalledOnce(); + expect(db.update).toHaveBeenCalledOnce(); + }); + it.each(["bare blocked update", "human-owned inbox fixture"] as const)( "applies the production blocked contract to the %s", async kind => { const actor = boardActor(); diff --git a/server/src/__tests__/routable-blocked.test.ts b/server/src/__tests__/routable-blocked.test.ts index eb2275822c8..e843a406535 100644 --- a/server/src/__tests__/routable-blocked.test.ts +++ b/server/src/__tests__/routable-blocked.test.ts @@ -25,17 +25,24 @@ describe("routable blocked notifications", () => { it("wakes the named agent and records delivery on a prospective transition", async () => { const wakeup = vi.fn(async () => undefined); const markNotified = vi.fn(async () => undefined); + const onError = vi.fn(async () => undefined); const now = new Date("2026-07-23T18:30:00.000Z"); const issue = blockedIssue(); - await expect(deliverAgentUnblockNotification({ issue, wakeup, markNotified, now: () => now })) - .resolves.toBe(true); + await expect(deliverAgentUnblockNotification({ + issue, + wakeup, + markNotified, + onError, + now: () => now, + })).resolves.toBe(true); expect(wakeup).toHaveBeenCalledWith(agentId, expect.objectContaining({ reason: "issue_unblock_requested", idempotencyKey: `issue-unblock:${issue.id}:${issue.blockedTransitionAt!.toISOString()}`, payload: { issueId: issue.id, action: "Review the finding" }, })); expect(markNotified).toHaveBeenCalledWith(now); + expect(onError).not.toHaveBeenCalled(); }); it("leaves pre-existing blocked issues untouched", async () => { @@ -46,6 +53,7 @@ describe("routable blocked notifications", () => { issue: blockedIssue({ transitionAt: new Date(ROUTABLE_BLOCKED_ROLLOUT_AT.getTime() - 1) }), wakeup, markNotified, + onError: vi.fn(async () => undefined), })).resolves.toBe(false); expect(wakeup).not.toHaveBeenCalled(); expect(markNotified).not.toHaveBeenCalled(); @@ -61,11 +69,13 @@ describe("routable blocked notifications", () => { issue: blockedIssue({ transitionAt: firstTransition, notifiedAt: new Date() }), wakeup, markNotified, + onError: vi.fn(async () => undefined), }); await deliverAgentUnblockNotification({ issue: blockedIssue({ transitionAt: secondTransition }), wakeup, markNotified, + onError: vi.fn(async () => undefined), }); expect(wakeup).toHaveBeenCalledTimes(1); @@ -73,4 +83,42 @@ describe("routable blocked notifications", () => { idempotencyKey: expect.stringContaining(secondTransition.toISOString()), }); }); + + it("keeps a committed blocked transition successful when the wake fails", async () => { + const error = new Error("wake dispatch failed"); + const wakeup = vi.fn(async () => { + throw error; + }); + const markNotified = vi.fn(async () => undefined); + const onError = vi.fn(async () => undefined); + + await expect(deliverAgentUnblockNotification({ + issue: blockedIssue(), + wakeup, + markNotified, + onError, + })).resolves.toBe(false); + expect(markNotified).not.toHaveBeenCalled(); + expect(onError).toHaveBeenCalledOnce(); + expect(onError).toHaveBeenCalledWith(error); + }); + + it("keeps a committed blocked transition successful when delivery marking fails", async () => { + const error = new Error("delivery mark failed"); + const wakeup = vi.fn(async () => undefined); + const markNotified = vi.fn(async () => { + throw error; + }); + const onError = vi.fn(async () => undefined); + + await expect(deliverAgentUnblockNotification({ + issue: blockedIssue(), + wakeup, + markNotified, + onError, + })).resolves.toBe(false); + expect(wakeup).toHaveBeenCalledOnce(); + expect(onError).toHaveBeenCalledOnce(); + expect(onError).toHaveBeenCalledWith(error); + }); }); diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index 280168dab89..20657ce867a 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -16655,19 +16655,29 @@ export function issueRoutes( issue: blockedIssue, wakeup: heartbeat.wakeup, markNotified: async (blockedOwnerNotifiedAt) => { + await db + .update(issueRows) + .set({ blockedOwnerNotifiedAt }) + .where( + and( + eq(issueRows.id, blockedIssue.id), + eq(issueRows.companyId, blockedIssue.companyId), + ), + ); ownerNotifiedAt = blockedOwnerNotifiedAt; }, + onError: async (err) => { + logger.warn( + { + err, + issueId: blockedIssue.id, + unblockOwner: blockedIssue.unblockDescriptor?.owner ?? null, + }, + "failed to deliver agent unblock notification after issue update", + ); + }, }); if (ownerNotifiedAt) { - await db - .update(issueRows) - .set({ blockedOwnerNotifiedAt: ownerNotifiedAt }) - .where( - and( - eq(issueRows.id, blockedIssue.id), - eq(issueRows.companyId, blockedIssue.companyId), - ), - ); issue = { ...blockedIssue, blockedOwnerNotifiedAt: ownerNotifiedAt }; } } diff --git a/server/src/services/routable-blocked.ts b/server/src/services/routable-blocked.ts index d2b91b774ca..e32e75963cd 100644 --- a/server/src/services/routable-blocked.ts +++ b/server/src/services/routable-blocked.ts @@ -31,6 +31,7 @@ export async function deliverAgentUnblockNotification(input: { contextSnapshot: { wakeReason: "issue_unblock_requested"; issueId: string; taskId: string }; }) => Promise; markNotified: (notifiedAt: Date) => Promise; + onError: (error: unknown) => Promise; now?: () => Date; }) { const { issue } = input; @@ -41,14 +42,19 @@ export async function deliverAgentUnblockNotification(input: { const owner = issue.unblockDescriptor.owner; if (owner === "board" || !("agentId" in owner)) return false; - await input.wakeup(owner.agentId, { - source: "automation", - triggerDetail: "system", - reason: "issue_unblock_requested", - idempotencyKey: `issue-unblock:${issue.id}:${issue.blockedTransitionAt.toISOString()}`, - payload: { issueId: issue.id, action: issue.unblockDescriptor.action }, - contextSnapshot: { wakeReason: "issue_unblock_requested", issueId: issue.id, taskId: issue.id }, - }); - await input.markNotified((input.now ?? (() => new Date()))()); + try { + await input.wakeup(owner.agentId, { + source: "automation", + triggerDetail: "system", + reason: "issue_unblock_requested", + idempotencyKey: `issue-unblock:${issue.id}:${issue.blockedTransitionAt.toISOString()}`, + payload: { issueId: issue.id, action: issue.unblockDescriptor.action }, + contextSnapshot: { wakeReason: "issue_unblock_requested", issueId: issue.id, taskId: issue.id }, + }); + await input.markNotified((input.now ?? (() => new Date()))()); + } catch (error) { + await input.onError(error); + return false; + } return true; }