diff --git a/server/src/__tests__/headless-board-events-live-sync.test.ts b/server/src/__tests__/headless-board-events-live-sync.test.ts new file mode 100644 index 00000000000..14f16fac450 --- /dev/null +++ b/server/src/__tests__/headless-board-events-live-sync.test.ts @@ -0,0 +1,137 @@ +import { describe, expect, it } from "vitest"; +import type { BoardEventView, LiveEvent } from "@paperclipai/shared"; +import { + buildBoardEventLiveActivityPayload, + publishCommittedBoardEvent, +} from "../services/headless-board-events-write.js"; +import { subscribeCompanyLiveEvents } from "../services/live-events.js"; + +function makeBoardEvent( + overrides: Partial = {}, +): BoardEventView { + return { + cursor: "evt_42", + seq: 42, + type: "comment.posted", + companyId: "company-live-sync-test", + channel: "general", + issueId: "11111111-1111-4111-8111-111111111111", + identifier: "DEM-1", + actor: { + type: "user", + id: "local-board", + name: null, + profile: "ci-bot", + }, + payload: { + commentId: "22222222-2222-4222-8222-222222222222", + body: "unit tests brown", + }, + createdAt: "2026-12-02T16:00:00.000Z", + ...overrides, + }; +} + +describe("headless board events live sync", () => { + it("maps comment.posted board events to structured issue activity.logged live events", () => { + const received: LiveEvent[] = []; + const unsubscribe = subscribeCompanyLiveEvents("company-live-sync-test", (event) => { + received.push(event); + }); + + try { + publishCommittedBoardEvent(makeBoardEvent()); + } finally { + unsubscribe(); + } + + expect(received).toHaveLength(1); + expect(received[0]).toMatchObject({ + companyId: "company-live-sync-test", + type: "activity.logged", + payload: { + action: "board.comment.posted", + entityType: "issue", + entityId: "11111111-1111-4111-8111-111111111111", + actorType: "user", + actorId: "local-board", + boardEventSeq: 42, + boardEventCursor: "evt_42", + boardEventType: "comment.posted", + channel: "general", + issueId: "11111111-1111-4111-8111-111111111111", + identifier: "DEM-1", + details: { + identifier: "DEM-1", + channel: "general", + actorProfile: "ci-bot", + commentId: "22222222-2222-4222-8222-222222222222", + boardEventType: "comment.posted", + boardEventSeq: 42, + boardEventCursor: "evt_42", + source: "headless", + }, + }, + }); + }); + + it("maps interaction and approval board events to their canonical live actions", () => { + expect( + buildBoardEventLiveActivityPayload( + makeBoardEvent({ type: "interaction.created", payload: { interactionId: "int-1" } }), + ), + ).toMatchObject({ + action: "issue.thread_interaction_created", + entityType: "issue", + entityId: "11111111-1111-4111-8111-111111111111", + }); + + expect( + buildBoardEventLiveActivityPayload( + makeBoardEvent({ type: "interaction.resolved", payload: { interactionId: "int-1" } }), + ), + ).toMatchObject({ + action: "issue.thread_interaction_resolved", + entityType: "issue", + entityId: "11111111-1111-4111-8111-111111111111", + }); + + expect( + buildBoardEventLiveActivityPayload( + makeBoardEvent({ + type: "approval.resolved", + issueId: null, + identifier: null, + payload: { approvalId: "approval-1" }, + }), + ), + ).toMatchObject({ + action: "approval.decided", + entityType: "approval", + entityId: "approval-1", + }); + }); + + it("maps task queue, workflow, and dependency board events onto the target issue", () => { + for (const type of [ + "task.claimed", + "task.completed", + "task.failed", + "task.dependencies_changed", + "labels.changed", + ] as const) { + expect( + buildBoardEventLiveActivityPayload(makeBoardEvent({ type, payload: {} })), + ).toMatchObject({ + action: `board.${type}`, + entityType: "issue", + entityId: "11111111-1111-4111-8111-111111111111", + details: expect.objectContaining({ + identifier: "DEM-1", + boardEventType: type, + source: "headless", + }), + }); + } + }); +}); diff --git a/server/src/routes/headless-channels-schemas.ts b/server/src/routes/headless-channels-schemas.ts index 9aa9e81b3c3..91928308660 100644 --- a/server/src/routes/headless-channels-schemas.ts +++ b/server/src/routes/headless-channels-schemas.ts @@ -834,6 +834,8 @@ export function headlessChannelsSchemasRoutes( commentId: inserted!.id, identifier, internal: Boolean(b.internal), + source: "headless", + ...(actor.actorProfile ? { actorProfile: actor.actorProfile } : {}), }, }, publications, diff --git a/server/src/services/headless-board-events-write.ts b/server/src/services/headless-board-events-write.ts index 44235e6a73f..5c63a8588a3 100644 --- a/server/src/services/headless-board-events-write.ts +++ b/server/src/services/headless-board-events-write.ts @@ -80,19 +80,67 @@ export function subscribeCompanyBoardEvents( return () => boardEventEmitter.off(companyId, safeListener); } +function mapBoardEventTypeToActivityAction(type: BoardEventType): string { + switch (type) { + case "interaction.created": + return "issue.thread_interaction_created"; + case "interaction.resolved": + return "issue.thread_interaction_resolved"; + case "approval.resolved": + return "approval.decided"; + default: + return `board.${type}`; + } +} + +export function buildBoardEventLiveActivityPayload( + event: BoardEventView, +): Record { + const approvalId = + event.type === "approval.resolved" && + typeof event.payload.approvalId === "string" + ? event.payload.approvalId + : null; + const entityType = event.issueId + ? "issue" + : event.type === "approval.resolved" + ? "approval" + : "channel"; + const entityId = event.issueId ?? approvalId ?? event.channel ?? null; + const commentId = + typeof event.payload.commentId === "string" ? event.payload.commentId : null; + + return { + action: mapBoardEventTypeToActivityAction(event.type), + entityType, + entityId, + actorType: event.actor.type, + actorId: event.actor.id ?? null, + details: { + ...(event.identifier ? { identifier: event.identifier } : {}), + ...(event.channel ? { channel: event.channel } : {}), + ...(event.actor.profile ? { actorProfile: event.actor.profile } : {}), + ...(commentId ? { commentId } : {}), + boardEventType: event.type, + boardEventSeq: event.seq, + boardEventCursor: event.cursor, + source: "headless", + }, + boardEventSeq: event.seq, + boardEventCursor: event.cursor, + boardEventType: event.type, + channel: event.channel, + issueId: event.issueId, + identifier: event.identifier, + }; +} + export function publishCommittedBoardEvent(event: BoardEventView): void { boardEventEmitter.emit(event.companyId, event); publishLiveEvent({ companyId: event.companyId, type: "activity.logged", - payload: { - boardEventSeq: event.seq, - boardEventCursor: event.cursor, - boardEventType: event.type, - channel: event.channel, - issueId: event.issueId, - identifier: event.identifier, - }, + payload: buildBoardEventLiveActivityPayload(event), }); } diff --git a/server/src/services/headless-comment-group-commit.ts b/server/src/services/headless-comment-group-commit.ts index 22ca18806bd..fdd35b1e3ce 100644 --- a/server/src/services/headless-comment-group-commit.ts +++ b/server/src/services/headless-comment-group-commit.ts @@ -253,6 +253,10 @@ async function runCoalescedCommentsBatch( commentId, identifier, internal: item.internal, + source: "headless", + ...(item.actor.actorProfile + ? { actorProfile: item.actor.actorProfile } + : {}), }, }; }, diff --git a/ui/src/context/LiveUpdatesProvider.test.ts b/ui/src/context/LiveUpdatesProvider.test.ts index bf8b61bd04a..635f18129f8 100644 --- a/ui/src/context/LiveUpdatesProvider.test.ts +++ b/ui/src/context/LiveUpdatesProvider.test.ts @@ -1457,6 +1457,175 @@ describe("LiveUpdatesProvider issue invalidation", () => { client.clear(); }); + it("actively refreshes an open unfocused issue page on headless pc reply even as local-board", () => { + const client = new QueryClient(); + client.setQueryData(queryKeys.issues.detail("DEM-1"), { + id: "issue-dem-1", + companyId: "company-1", + identifier: "DEM-1", + title: "Demo issue", + }); + const invalidate = vi.spyOn(client, "invalidateQueries"); + + __liveUpdatesTestUtils.invalidateActivityQueries( + client, + "company-1", + { + entityType: "issue", + entityId: "issue-dem-1", + action: "issue.comment_added", + actorType: "user", + actorId: "local-board", + details: { + commentId: "comment-pc-1", + identifier: "DEM-1", + internal: false, + source: "headless", + actorProfile: "ci-bot", + }, + }, + { userId: "local-board", agentId: null }, + { pathname: "/DEM/issues/DEM-1", isForegrounded: false }, + ); + + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.detail("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.activity("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.comments("DEM-1") }); + expect(invalidate).not.toHaveBeenCalledWith({ + queryKey: queryKeys.issues.comments("DEM-1"), + refetchType: "inactive", + }); + client.clear(); + }); + + it("actively refreshes an unfocused issue detail keyed by identifier on authenticated board.comment.posted with stripped details", () => { + const client = new QueryClient(); + client.setQueryData(queryKeys.issues.detail("DEM-1"), { + id: "issue-dem-1", + companyId: "company-1", + identifier: "DEM-1", + title: "Demo issue", + }); + const invalidate = vi.spyOn(client, "invalidateQueries"); + + __liveUpdatesTestUtils.invalidateActivityQueries( + client, + "company-1", + { + entityType: "issue", + entityId: "issue-dem-1", + action: "board.comment.posted", + actorType: "user", + actorId: "user-1", + }, + { userId: "user-1", agentId: null }, + { pathname: "/DEM/issues/DEM-1", isForegrounded: false }, + ); + + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.detail("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.activity("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.comments("DEM-1") }); + expect(invalidate).not.toHaveBeenCalledWith({ + queryKey: queryKeys.issues.comments("DEM-1"), + refetchType: "inactive", + }); + client.clear(); + }); + + it("refreshes open issue details, related blockers, and attention views on headless board task events", () => { + const client = new QueryClient(); + client.setQueryData(queryKeys.issues.detail("DEM-1"), { + id: "issue-dem-1", + companyId: "company-1", + identifier: "DEM-1", + title: "Demo blocker", + }); + client.setQueryData(queryKeys.issues.detail("DEM-2"), { + id: "issue-dem-2", + companyId: "company-1", + identifier: "DEM-2", + title: "Blocked downstream task", + blockedBy: [{ id: "issue-dem-1", identifier: "DEM-1" }], + }); + const invalidate = vi.spyOn(client, "invalidateQueries"); + + __liveUpdatesTestUtils.invalidateActivityQueries( + client, + "company-1", + { + entityType: "issue", + entityId: "issue-dem-1", + action: "board.task.completed", + actorType: "user", + actorId: "local-board", + }, + { userId: "local-board", agentId: null }, + { pathname: "/DEM/issues/DEM-1", isForegrounded: false }, + ); + + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.detail("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.activity("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.comments("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.attention("company-1") }); + expect(invalidate).toHaveBeenCalledWith( + expect.objectContaining({ + queryKey: ["issues", "detail"], + predicate: expect.any(Function), + }), + ); + client.clear(); + }); + + it("refreshes open comments, related blockers, and attention views on pc fail, handoff, and dependency changes", () => { + const client = new QueryClient(); + client.setQueryData(queryKeys.issues.detail("DEM-1"), { + id: "issue-dem-1", + companyId: "company-1", + identifier: "DEM-1", + title: "Demo blocker", + }); + client.setQueryData(queryKeys.issues.detail("DEM-2"), { + id: "issue-dem-2", + companyId: "company-1", + identifier: "DEM-2", + title: "Blocked downstream task", + blockedBy: [{ id: "issue-dem-1", identifier: "DEM-1" }], + }); + const invalidate = vi.spyOn(client, "invalidateQueries"); + + for (const action of [ + "board.task.failed", + "board.task.handed_off", + "board.task.dependencies_changed", + ] as const) { + invalidate.mockClear(); + __liveUpdatesTestUtils.invalidateActivityQueries( + client, + "company-1", + { + entityType: "issue", + entityId: "issue-dem-1", + action, + actorType: "user", + actorId: "local-board", + }, + { userId: "local-board", agentId: null }, + { pathname: "/DEM/issues/DEM-1", isForegrounded: false }, + ); + + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.detail("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.issues.comments("DEM-1") }); + expect(invalidate).toHaveBeenCalledWith({ queryKey: queryKeys.attention("company-1") }); + expect(invalidate).toHaveBeenCalledWith( + expect.objectContaining({ + queryKey: ["issues", "detail"], + predicate: expect.any(Function), + }), + ); + } + client.clear(); + }); + it("refreshes visible issue run queries when the displayed run changes status", () => { const invalidations: unknown[] = []; const cache = new Map([ diff --git a/ui/src/context/LiveUpdatesProvider.tsx b/ui/src/context/LiveUpdatesProvider.tsx index 468a74f9c61..2f2321400ec 100644 --- a/ui/src/context/LiveUpdatesProvider.tsx +++ b/ui/src/context/LiveUpdatesProvider.tsx @@ -329,6 +329,28 @@ function resolveIssueQueryRefs( if (detailIssue?.id) refs.add(detailIssue.id); if (detailIssue?.identifier) refs.add(detailIssue.identifier); + // Detail routes normally key on the human identifier (e.g. DEM-1), while + // activity and headless board events carry the canonical UUID (and + // authenticated events omit `details`). Scan cached detail queries so an + // open or unfocused issue page resolves even without a loaded issue list. + if (typeof queryClient.getQueriesData === "function") { + for (const [key, cached] of queryClient.getQueriesData({ + queryKey: ["issues", "detail"], + })) { + if (!cached) continue; + if ( + cached.id === issueId || + (cached.identifier && cached.identifier === issueId) || + (detailsIdentifier && cached.identifier === detailsIdentifier) + ) { + if (cached.id) refs.add(cached.id); + if (cached.identifier) refs.add(cached.identifier); + const keyRef = readString(key[2]); + if (keyRef) refs.add(keyRef); + } + } + } + const listIssue = listIssues?.find((issue) => { if (issue.id === issueId) return true; if (issue.identifier && issue.identifier === issueId) return true; @@ -1542,6 +1564,54 @@ const ISSUE_RELATION_SUMMARY_ACTIVITY_ACTIONS = new Set([ "issue.recovery_generation_replaced", "issue.retry_requested", "issue.scheduled_retry_retry_now", + "board.task.ready", + "board.task.dependencies_changed", + "board.task.completed", + "board.task.failed", + "board.task.blocked", + "board.task.unblocked", + "board.task.cancelled", + "board.task.reopened", + "board.task.claimed", + "board.task.lease_released", + "board.task.lease_expired_requeued", + "board.task.lease_poison_blocked", + "board.task.assigned", + "board.task.handed_off", +]); + +const BOARD_DECISION_PROJECTION_ACTIONS = new Set([ + "board.task.created", + "board.task.ready", + "board.task.claimed", + "board.task.lease_released", + "board.task.lease_expired_requeued", + "board.task.lease_poison_blocked", + "board.task.completed", + "board.task.failed", + "board.task.blocked", + "board.task.unblocked", + "board.task.assigned", + "board.task.handed_off", + "board.task.cancelled", + "board.task.reopened", + "board.task.dependencies_changed", +]); + +// Headless board actions that can persist a comment on the issue without +// emitting a separate `issue.comment_added` event. +const BOARD_COMMENT_PRODUCING_ACTIONS = new Set([ + "board.comment.posted", + "board.task.lease_released", + "board.task.completed", + "board.task.failed", + "board.task.assigned", + "board.task.handed_off", + "board.task.cancelled", + "board.task.reopened", + "board.task.dependencies_changed", + "issue.thread_interaction_created", + "issue.thread_interaction_resolved", ]); /** @@ -1622,7 +1692,9 @@ function invalidateActivityQueries( action === "issue.recovery_action_resolved" || action === "issue.retry_requested" || action === "issue.conversation_session_started" || - (entityType === "issue" && ["issue.created", "issue.updated", "issue.deleted"].includes(action ?? "")); + (entityType === "issue" && + (["issue.created", "issue.updated", "issue.deleted"].includes(action ?? "") || + (!!action && BOARD_DECISION_PROJECTION_ACTIONS.has(action)))); const decisionAudienceChanged = action?.startsWith("issue_access_grant.") || action?.startsWith("resource_membership.") || action?.startsWith("company_membership.") || @@ -1714,7 +1786,11 @@ function invalidateActivityQueries( queryKey: queryKeys.issues.listUnreadTouchedByMe(companyId), }); if (entityId) { + const isHeadlessCommentActivity = + readString(details?.source) === "headless" || + Boolean(readString(details?.actorProfile)); const selfCommentActivity = + !isHeadlessCommentActivity && (action === "issue.comment_added" || (action === "issue.updated" && readString(details?.source) === "comment")) && @@ -1786,8 +1862,18 @@ function invalidateActivityQueries( : undefined; queryClient.invalidateQueries({ queryKey: queryKeys.issues.detail(ref), ...invalidationOptions }); queryClient.invalidateQueries({ queryKey: queryKeys.issues.activity(ref), ...invalidationOptions }); - if (action === "issue.comment_added" || action === "issue.conversation_session_started" || visibleConversation) { - queryClient.invalidateQueries({ queryKey: queryKeys.issues.comments(ref), ...(visibleConversation ? undefined : invalidationOptions) }); + const isBoardCommentAction = + !!action && BOARD_COMMENT_PRODUCING_ACTIONS.has(action); + if ( + action === "issue.comment_added" || + isBoardCommentAction || + action === "issue.conversation_session_started" || + visibleConversation + ) { + queryClient.invalidateQueries({ + queryKey: queryKeys.issues.comments(ref), + ...(visibleConversation || isBoardCommentAction ? undefined : invalidationOptions), + }); } if (action?.startsWith("issue.attachment_") || action?.startsWith("issue.work_product_")) { // These cards are durable API objects, not streamed text. Refresh the