Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(loops): measure grace windows from the oldest unanswered message in the run (N7-1)
garrytan1842c74944ef
diff --git a/docs/guides/open-loops.md b/docs/guides/open-loops.mdindex 2ac87cfae6..82889baaa5 100644--- a/docs/guides/open-loops.md+++ b/docs/guides/open-loops.md@@ -24,6 +24,10 @@ zero LLM, free, always on). For every synced Gmail thread:≥72h → `unanswered_outbound` — *you are waiting on them*.- a reply lands → the loop **closes itself** (`closed_by: reply_detected`).Loops close by state transition, never delete — the audit trail stays.+- "unanswered" is measured from the oldest unanswered message since the+ last turn flip (the first one addressed to you, or your first question),+ so a nudge or follow-up never restarts the clock — a request that waited+ 40h and got a "bumping this" 5h ago opens on the first sync.Precision rules (pinned by a labeled fixture corpus in`test/google-loop-detect.test.ts` — every false-positive class gets adiff --git a/src/core/google/loop-detect.ts b/src/core/google/loop-detect.tsindex db1ba2146a..2d76f52e35 100644--- a/src/core/google/loop-detect.ts+++ b/src/core/google/loop-detect.ts@@ -20,7 +20,9 @@* - outbound without a question mark is FYI, not an ask* - suppressed senders/threads (gbrain loops mute) never open NEW loops;* existing loops keep their state- * - grace windows: inbound 24h, outbound 72h — fresh mail is not a loop yet+ * - grace windows: inbound 24h, outbound 72h — fresh mail is not a loop yet;+ * measured from the oldest unanswered message in the trailing run, so a+ * nudge or follow-up does not restart the clock** Pure verdict function + a thin apply step; the apply step is called from* runGoogleSync per touched thread and must never fail the sync.@@ -128,6 +130,16 @@ export function detectThreadLoop(const threadSuppressed = suppressions?.threads.has(thread.threadId) ?? false;+ // The trailing run: substantive messages since the last turn flip, all on+ // the last speaker's side. Grace windows are measured from the OLDEST+ // message in the run that carries the obligation (an inbound message with+ // me in To:, or my outbound ask), so a fresh nudge or follow-up never+ // restarts the clock on a request that has already waited past the window+ // — the open-lane twin of the close-lane rule that a nudge is not a reply.+ let runStart = substantive.length - 1;+ while (runStart > 0 && isMine(substantive[runStart - 1], myAddresses) === lastIsMine) runStart--;+ const run = substantive.slice(runStart);+if (!lastIsMine) {// ── Last word is theirs: do I owe a reply? ──// List mail never owes a reply.@@ -136,7 +148,8 @@ export function detectThreadLoop(const inTo = last.to.some((a) => myAddresses.has(a));if (!inTo) return { open: [], close };if (threadSuppressed || suppressions?.senders.has(last.fromAddress)) return { open: [], close };- if (ageHours(last.internalDateMs, now) < INBOUND_GRACE_HOURS) return { open: [], close };+ const owedSince = run.find((m) => m.to.some((a) => myAddresses.has(a))) ?? last;+ if (ageHours(owedSince.internalDateMs, now) < INBOUND_GRACE_HOURS) return { open: [], close };return {open: [{@@ -161,7 +174,8 @@ export function detectThreadLoop(if (recipients.length === 0) return { open: [], close };const counterparty = recipients[0];if (threadSuppressed || suppressions?.senders.has(counterparty)) return { open: [], close };- if (ageHours(last.internalDateMs, now) < OUTBOUND_GRACE_HOURS) return { open: [], close };+ const askedSince = run.find((m) => m.bodyText.includes('?')) ?? last;+ if (ageHours(askedSince.internalDateMs, now) < OUTBOUND_GRACE_HOURS) return { open: [], close };return {open: [{diff --git a/test/google-loop-detect.test.ts b/test/google-loop-detect.test.tsindex 29f975f8ad..512c775275 100644--- a/test/google-loop-detect.test.ts+++ b/test/google-loop-detect.test.ts@@ -396,6 +396,58 @@ const CASES: CorpusCase[] = [],expect: null,},+ // ── Grace runs from the oldest unanswered message (gbrain-evals N7-1) ─────+ // First sight of a thread (backfill, sync gap): a fresh nudge must not+ // restart the clock on a request that has waited past the window.+ {+ name: 'request 40h + nudge 5h, first sight → unanswered_inbound (clock starts at the request)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 40, body: 'Can you send the deck?' }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 5, body: 'Bumping this. Any news?' }),+ ],+ expect: { type: 'unanswered_inbound', counterparty: '[email protected]' },+ },+ {+ name: 'my ask 100h + my follow-up 10h, first sight → unanswered_outbound (clock starts at the ask)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 100, sent: true, body: 'Could you confirm the date?' }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 10, sent: true, body: 'Following up on this. Any update?' }),+ ],+ expect: { type: 'unanswered_outbound', counterparty: '[email protected]' },+ },+ {+ name: 'fresh request 10h + nudge 2h → none (the whole run is inside grace)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 10 }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 2 }),+ ],+ expect: null,+ },+ {+ name: 'inbound 40h, my reply 30h, their nudge 5h → none (the run starts after my reply)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 40 }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 30, sent: true, body: 'Will do.' }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 5 }),+ ],+ expect: null,+ },+ {+ name: 'CC-only mail 40h + direct ask 5h → none (the reply is owed from the To: message)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], cc: ['[email protected]'], ageHours: 40 }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 5 }),+ ],+ expect: null,+ },+ {+ name: 'my FYI 100h + my question 10h → none (the ask is fresh)',+ messages: [+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 100, sent: true, body: 'Notes attached.' }),+ msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 10, sent: true, body: 'Can you review?' }),+ ],+ expect: null,+ },];describe('detectThreadLoop precision corpus', () => {