Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
community(agent-inbox): mirror thread participation narrowing in send alignment gate
Genercb05af16fb40
diff --git a/src/shared/src/db/queries/community/agent-inbox.ts b/src/shared/src/db/queries/community/agent-inbox.tsindex 773b3b537..151129c81 100644--- a/src/shared/src/db/queries/community/agent-inbox.ts+++ b/src/shared/src/db/queries/community/agent-inbox.ts@@ -639,8 +639,16 @@ export async function listUnreadMessagesForAgent(* for already-stuck bots: the gate stops counting messages the pull never hands* over.*- * Visibility (channel roster / thread participation) is the caller's job — the- * send route membership-gates the resolved channel before calling this.+ * Thread/forum-post participation narrowing: the pull's allowed set+ * (`listAgentAllowedChannelIds`) additionally drops thread/forum_post channels+ * the bot holds no `relation='notify'` row on — those are never delivered. The+ * gate MUST mirror that same narrowing (via the SAME `listParticipatingThreadIds`+ * predicate, not a re-derived one) or it counts backlog in a non-participated+ * thread that the pull will never hand over — the exact "not aligned: N unread"+ * deadlock a bot hit trying to post into a thread it hadn't engaged. This drops+ * ONLY true spectators: a bot @mentioned into a thread gets a `notify` row on the+ * send hot path (`addThreadParticipants`, source=mention), so its owed mention+ * still counts as deliverable — the narrowing is not-more-not-less than the pull.*/export async function hasDeliverableUnreadForAgentScope(db: Database,@@ -648,6 +656,21 @@ export async function hasDeliverableUnreadForAgentScope(channelId: string,seen: number): Promise<boolean> {+ // Participation narrowing, single-channel form of `listAgentAllowedChannelIds`'s+ // set-wide step: a thread/forum_post the bot doesn't participate in is never+ // deliverable, so its backlog must not register as unread here. Short-circuits+ // before the deliverable scan — cheaper for the common spectator case too.+ const typeRows = await db+ .select({ type: communityChannel.type })+ .from(communityChannel)+ .where(eq(communityChannel.id, channelId))+ .limit(1);+ const type = typeRows[0]?.type;+ if (isThread(type) || isForumPost(type)) {+ const participating = await listParticipatingThreadIds(db, [channelId], botUserId);+ if (participating.length === 0) return false;+ }+const rows = await db.select({ seq: communityMessage.seq }).from(communityMessage)diff --git a/src/shared/test/queries/community-agent-inbox.test.ts b/src/shared/test/queries/community-agent-inbox.test.tsindex 5a839374c..72f4736e0 100644--- a/src/shared/test/queries/community-agent-inbox.test.ts+++ b/src/shared/test/queries/community-agent-inbox.test.ts@@ -835,22 +835,55 @@ describe("listMessagesBySeq", () => {});describe("hasDeliverableUnreadForAgentScope", () => {+ // Call order (see the query's body):+ // 1. channel-type lookup (participation-narrowing pre-check)+ // 2. `listParticipatingThreadIds` — ONLY when (1) is thread/forum_post+ // 3. the deliverable-unread existence scan (limit 1)+ // For a plain (non-thread) channel, (2) is skipped: (1)=type, (2)=scan.it("returns true when a deliverable message beyond `seen` exists", async () => {- const db = createSequentialDb([[{ seq: 7 }]]);+ const db = createSequentialDb([[{ type: "text" }], [{ seq: 7 }]]);const result = await agentInbox.hasDeliverableUnreadForAgentScope(db, "bot_1", "c1", 3);expect(result).toBe(true);});it("returns false when nothing is deliverable beyond `seen` (pre-join backlog / own / already read)", async () => {- const db = createSequentialDb([[]]);+ const db = createSequentialDb([[{ type: "text" }], []]);const result = await agentInbox.hasDeliverableUnreadForAgentScope(db, "bot_1", "c1", 0);expect(result).toBe(false);});it("probes with limit(1) — existence check, not a full scan", async () => {- const db = createSequentialDb([[]]);+ const db = createSequentialDb([[{ type: "text" }], []]);await agentInbox.hasDeliverableUnreadForAgentScope(db, "bot_1", "c1", 0);- const chainResult = db.select.mock.results[0]!.value;+ // The scan is the 2nd select for a non-thread channel (type lookup is 1st).+ const chainResult = db.select.mock.results[1]!.value;expect(chainResult.limit).toHaveBeenCalledWith(1);});++ // The deadlock this fix closes: a thread the bot doesn't participate in has+ // unread backlog the pull will NEVER deliver, so the gate must NOT count it+ // (else send is wedged on "not aligned" forever). Participation narrowing+ // returns false BEFORE the deliverable scan — mirroring the pull's allowed set.+ it("returns false for a non-participated thread even with unread beyond `seen` (never-drop deadlock fix)", async () => {+ // (1) type=thread → (2) listParticipatingThreadIds returns [] (not a+ // participant). Short-circuits false; the deliverable scan never runs.+ const db = createSequentialDb([[{ type: "thread" }], []]);+ const result = await agentInbox.hasDeliverableUnreadForAgentScope(db, "bot_1", "thread_x", 0);+ expect(result).toBe(false);+ // Only two selects: type lookup + participation. No scan (would be a 3rd).+ expect(db.select).toHaveBeenCalledTimes(2);+ });++ // The other side of the coin (Aigneis's never-drop invariant): a bot+ // @mentioned into a thread gets a `relation='notify'` row on the send hot+ // path, so it IS a participant — its owed message must still count as+ // deliverable. Narrowing is not-more-not-less than the pull.+ it("returns true for a participated thread with a deliverable message (mention isn't dropped)", async () => {+ // (1) type=thread → (2) participation returns the channel id (notify row+ // exists) → (3) the deliverable scan finds an unread message.+ const db = createSequentialDb([[{ type: "thread" }], [{ channelId: "thread_x" }], [{ seq: 4 }]]);+ const result = await agentInbox.hasDeliverableUnreadForAgentScope(db, "bot_1", "thread_x", 0);+ expect(result).toBe(true);+ expect(db.select).toHaveBeenCalledTimes(3);+ });});