From 7bd424d50f4355382ade440c69c9572fa2ce103e Mon Sep 17 00:00:00 2001 From: Sergio-LPA Date: Sat, 5 Dec 2026 11:58:35 +0000 Subject: [PATCH] fix(issues): preserve PATCH result when unblock wake fails (#20425) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The issue API commits status changes before it sends some follow-up notifications. > - A blocked transition can therefore commit before the control plane wakes the named unblock owner. > - A wake failure currently escapes the route and changes the HTTP response to 500. > - The client then sees a failure even though the issue is already blocked. > - This pull request contains failures from that post-commit notification path. > - The benefit is an HTTP result that stays consistent with the committed issue update. ## Linked Issues or Issue Description Refs #3824 Related pull request: #19209 The related pull request maps a routine-slot constraint to HTTP 409. This change covers a different boundary. The target issue update has already committed when the unblock notification starts. The route must not replace that successful result with a notification error. ## What Changed - Catch wake and delivery-marker failures in the unblock notification helper. - Log the post-commit notification failure at the route boundary. - Persist `blockedOwnerNotifiedAt` only after the wake succeeds. - Keep the marker empty when delivery fails so a later attempt can use the same idempotency key. - Add regression tests for wake failures and marker failures. ## Verification - Before the service change, the two new failure tests rejected with their injected errors. - `HOST=127.0.0.1 node_modules/.bin/vitest run server/src/__tests__/routable-blocked.test.ts` passes 5 tests. - `HOST=127.0.0.1 node_modules/.bin/vitest run server/src/__tests__/routable-blocked.test.ts server/src/__tests__/issue-agent-mutation-ownership-routes.test.ts` passes 233 tests. This includes HTTP PATCH regressions for wake and marker-write failures. - `pnpm --filter @paperclipai/plugin-sdk build` passes. - `git diff --check` passes. ## Risks - Risk is low. This change does not add a migration or change the issue transaction. - A failed notification no longer changes the PATCH result. The route emits a warning with the issue and owner context. - A failed notification keeps `blockedOwnerNotifiedAt` empty. The existing idempotency key limits duplicate wake delivery during a later attempt. > This is a focused bug fix. It does not duplicate planned roadmap work. ## Model Used - OpenAI Codex with GPT-5, extended reasoning, tool use, and code execution. The context window size was not exposed to the agent. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes (no documentation change is needed for this internal error path) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green (59 checks on `21ad144`: 56 success, 3 skipped, 0 failures; `ci / verify`, `ci / e2e`, and `review` passed) - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups (`Greptile Review` success on `21ad144`; 0 open review threads) - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Builder Co-authored-by: Paperclip --- ...ue-agent-mutation-ownership-routes.test.ts | 69 +++++++++++++++++++ server/src/__tests__/routable-blocked.test.ts | 52 +++++++++++++- server/src/routes/issues.ts | 28 +++++--- server/src/services/routable-blocked.ts | 24 ++++--- 4 files changed, 153 insertions(+), 20 deletions(-) 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; }