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 Gmail reply detection closed when sent coverage is incomplete

[._.]/ Adam Eivy14de458cf141
diff --git a/server/services/messageGmailSync.js b/server/services/messageGmailSync.js
index 49a2051ad5..be05725a62 100644
--- a/server/services/messageGmailSync.js
+++ b/server/services/messageGmailSync.js
@@ -251,6 +251,7 @@ export async function syncGmail(account, cache, io, options = {}) {
// Step 2: Fetch full message details in parallel batches
const messages = [];
const BATCH_SIZE = 10;
+ let detailFetchFailures = 0;
for (let i = 0; i < messageIds.length; i += BATCH_SIZE) {
io?.emit('messages:sync:progress', { accountId: account.id, current: i, total: messageIds.length });
@@ -262,7 +263,7 @@ export async function syncGmail(account, cache, io, options = {}) {
));
for (let j = 0; j < results.length; j++) {
- if (!results[j]) continue;
+ if (!results[j]) { detailFetchFailures++; continue; }
const data = results[j].data;
const { id: gmailId, threadId: gmailThreadId } = batch[j];
const headers = data.payload?.headers || [];
@@ -319,8 +320,22 @@ export async function syncGmail(account, cache, io, options = {}) {
io.emit('messages:sync:message', { accountId: account.id, messages: inboxMessages });
}
+ // Sent-coverage fail-closed also covers a dropped detail fetch (#2820): a
+ // `users.messages.get` that failed above was skipped, but `sentTruncated` only
+ // reflects LIST pagination — so a dropped message (possibly the user's reply)
+ // would otherwise be certified as full coverage. We can't read a failed fetch's
+ // SENT label to know whether the dropped message was a reply, so when the account
+ // is ingesting sent mail we fail closed on ANY detail-fetch failure: the outreach
+ // detector pauses this account for the scan rather than nudging on incomplete
+ // reply evidence. Self-heals on the next clean sync. (When ingestSent is off,
+ // reply detection isn't trusted for this account anyway, so don't over-signal.)
+ const sentCoveragePartial = sentTruncated || (ingestSent && detailFetchFailures > 0);
+ if (ingestSent && detailFetchFailures > 0 && !sentTruncated) {
+ console.warn(`📧 Gmail: ${detailFetchFailures} message detail fetch(es) failed for ${account.email} — reply detection paused for this account this sync (incomplete coverage)`);
+ }
+
console.log(`📧 Gmail API sync complete: ${inboxMessages.length} inbox, ${sentMessages.length} sent (activity-only)`);
- return { messages: inboxMessages, sentMessages, sentTruncated, status: 'success', syncMethod: 'api' };
+ return { messages: inboxMessages, sentMessages, sentTruncated: sentCoveragePartial, status: 'success', syncMethod: 'api' };
}
/**
diff --git a/server/services/messageSync.js b/server/services/messageSync.js
index e1e58e6441..3b2e066c93 100644
--- a/server/services/messageSync.js
+++ b/server/services/messageSync.js
@@ -258,8 +258,11 @@ export async function syncAccount(accountId, io, options = {}) {
// cache each sync is a no-op for already-recorded messages. Machine-local.
// Sent mail (activity-only, #2796) is fed IN ADDITION to the cached inbox mail
// so it records `message.sent` events without ever entering the inbox cache.
- await recordMessageActivity(account, [...cache.messages, ...sentMessages]).catch((err) =>
- console.error(`🗓️ Activity ingest failed for account ${accountId}: ${err.message}`));
+ let activityIngestFailed = false;
+ await recordMessageActivity(account, [...cache.messages, ...sentMessages]).catch((err) => {
+ activityIngestFailed = true;
+ console.error(`🗓️ Activity ingest failed for account ${accountId}: ${err.message}`);
+ });
// Reply-detection watermark (#2796): stamp when a Gmail account with sent-ingest
// enabled completes a successful sync, so the outreach detector only trusts an
@@ -267,9 +270,13 @@ export async function syncAccount(accountId, io, options = {}) {
// account default-on at upgrade (or after an OAuth/sync failure) would be trusted
// before any reply evidence exists, producing false "unanswered" nudges.
if (providerStatus === 'success' && account.type === 'gmail' && account.syncConfig?.ingestSent !== false) {
- // `partial` fails the account closed for the outreach detector when the sent
- // window truncated at its ceiling this sync (#2820) — incomplete reply evidence.
- await markSentIngested(accountId, { partial: sentTruncated }).catch((err) =>
+ // `partial` fails the account closed for the outreach detector when reply
+ // evidence for this sync is incomplete: either the sent window truncated at
+ // its ceiling (#2820) OR activity ingestion itself failed — in which case the
+ // freshly-fetched `message.sent` rows never reached the timeline, so trusting a
+ // full watermark (or a stale recent one) would let a replied thread look
+ // unanswered. Stamping partial keeps the account fail-closed until a clean sync.
+ await markSentIngested(accountId, { partial: sentTruncated || activityIngestFailed }).catch((err) =>
console.error(`🤝 Sent-ingest watermark failed for account ${accountId}: ${err.message}`));
}