Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): fail closed on email outreach draft missing accountId (#2820)

[._.]/ Adam Eivy85729030321c
diff --git a/server/services/tribeOutreach.js b/server/services/tribeOutreach.js
index ee93bbdcf7..431807af8a 100644
--- a/server/services/tribeOutreach.js
+++ b/server/services/tribeOutreach.js
@@ -435,6 +435,16 @@ async function generateOutreachDraftImpl({
instructions = '',
useVoice,
} = {}) {
+ // Fail closed (#2820): when threadId is THE selective grounding key (email — no
+ // chatGuid/conversationId), it's only unique within one account. Without accountId
+ // the grounding query would be account-unscoped and could merge same-valued
+ // threadIds across accounts (wrong conversation grounded/reused). A detected Gmail
+ // thread always carries accountId; a legacy-queued/older-client/direct request
+ // that omits it is refused rather than grounded ambiguously.
+ if (threadId && !chatGuid && !conversationId && !accountId) {
+ throw new ServerError('An email outreach draft requires accountId to disambiguate the thread — reload the outreach list', { status: 400, code: 'ACCOUNT_ID_REQUIRED' });
+ }
+
const [{ getPerson }, { listEvents }, { generateReplyBody }, { createDraft, listDrafts }] = await Promise.all([
import('./tribe.js'),
import('./humanActivity.js'),
diff --git a/server/services/tribeOutreach.test.js b/server/services/tribeOutreach.test.js
index 3ba46a6d55..49259725e3 100644
--- a/server/services/tribeOutreach.test.js
+++ b/server/services/tribeOutreach.test.js
@@ -431,6 +431,31 @@ describe('generateOutreachDraft', () => {
expect(opts.templateOverride).not.toMatch(/professional reply to this email/i);
});
+ it('fails closed (400) for an email thread keyed only by threadId with no accountId (#2820)', async () => {
+ // A legacy-queued / older-client / direct request omits accountId — grounding by
+ // an account-unscoped threadId could merge accounts, so refuse rather than guess.
+ await expect(generateOutreachDraft({
+ personId: 'p1', source: 'gmail', threadId: 't-ambiguous', lastInboundAt: '2026-07-15T00:00:00.000Z',
+ })).rejects.toMatchObject({ status: 400, code: 'ACCOUNT_ID_REQUIRED' });
+ expect(generateReplyBody).not.toHaveBeenCalled();
+ });
+
+ it('allows an email thread when accountId disambiguates it (#2820)', async () => {
+ listEvents.mockResolvedValue([
+ { kind: 'message.received', accountId: 'acct-a', happenedAt: '2026-07-15T00:00:00.000Z', summary: 'proposal?', metadata: { threadId: 't-9' } },
+ ]);
+ getPerson.mockResolvedValue({ id: 'p1', name: 'Alex', phones: [], emails: ['[email protected]'] });
+ const result = await generateOutreachDraft({
+ personId: 'p1', source: 'gmail', accountId: 'acct-a', threadId: 't-9', lastInboundAt: '2026-07-15T00:00:00.000Z',
+ });
+ expect(generateReplyBody).toHaveBeenCalled();
+ // The grounding query is scoped to the account.
+ expect(listEvents).toHaveBeenCalledWith(expect.objectContaining({ accountId: 'acct-a', threadId: 't-9' }));
+ // The stored draft key is account-namespaced.
+ expect(createDraft).toHaveBeenCalledWith(expect.objectContaining({ conversationKey: 'thread:acct-a:t-9' }));
+ expect(result.draft).toBeDefined();
+ });
+
it('prefers the chat handle over a person email in the review-only recipient', async () => {
getPerson.mockResolvedValue({ id: 'p1', name: 'Alex', phones: [], emails: ['[email protected]'] });
listEvents.mockResolvedValue([