Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* The eligibility gate in front of loops_extract
* (src/core/google/loops-extract.ts:loopExtractionEligibility).
*
* Table-driven, pure, no engine. Every row is a STRUCTURAL shape — Gmail
* labels, List-Unsubscribe, calendar parts, who wrote the message. There are
* deliberately no sender, domain, subject or body assertions anywhere in this
* file: the gate must never grow a vendor list.
*
* Synthetic data only.
*/
import { describe, expect, test } from 'bun:test';
import {
loopExtractionEligibility,
type ExtractEligibility,
} from '../src/core/google/loops-extract.ts';
import type { GmailMessageMeta, GmailThreadData } from '../src/core/google/types.ts';
const MY = new Set(['[email protected]']);
let seq = 0;
interface MsgSpec {
from: string;
to?: string[];
labels?: string[];
listUnsub?: boolean;
calendarMethod?: string | null;
autoSubmitted?: boolean;
body?: string;
subject?: string;
}
function msg(spec: MsgSpec): GmailMessageMeta {
seq += 1;
return {
id: `18c2f4a9b3d2${(0x2000 + seq).toString(16)}`,
threadId: 'thread-1',
from: spec.from,
fromAddress: spec.from.toLowerCase(),
to: (spec.to ?? ['[email protected]']).map((a) => a.toLowerCase()),
cc: [],
subject: spec.subject ?? 'A subject',
dateIso: '2026-08-20T10:00:00.000Z',
internalDateMs: Date.parse('2026-08-20T10:00:00.000Z'),
labelIds: spec.labels ?? ['INBOX'],
listUnsubscribe: spec.listUnsub ?? false,
calendarMethod: spec.calendarMethod ?? null,
...(spec.autoSubmitted !== undefined ? { autoSubmitted: spec.autoSubmitted } : {}),
bodyText: spec.body ?? 'Some body text.',
};
}
const thread = (messages: GmailMessageMeta[]): GmailThreadData => ({
threadId: 'thread-1',
account: '[email protected]',
messages,
});
interface Row {
name: string;
messages: GmailMessageMeta[];
eligible: boolean;
reason: ExtractEligibility['reason'];
}
const ROWS: Row[] = [
{
name: 'ordinary human correspondence is eligible',
messages: [msg({ from: '[email protected]' })],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'SPAM is never eligible',
messages: [msg({ from: '[email protected]', labels: ['SPAM'] })],
eligible: false,
reason: 'spam_or_trash',
},
{
name: 'TRASH is never eligible',
messages: [msg({ from: '[email protected]', labels: ['TRASH'] })],
eligible: false,
reason: 'spam_or_trash',
},
{
name: 'SPAM beats an owner message — deleted mail is not an obligation',
messages: [
msg({ from: '[email protected]', labels: ['SPAM'] }),
msg({ from: '[email protected]', labels: ['SENT', 'SPAM'] }),
],
eligible: false,
reason: 'spam_or_trash',
},
{
name: 'a pure no-reply/notification thread is not eligible',
messages: [msg({ from: '[email protected]' })],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: 'a pure calendar-notice thread is not eligible',
messages: [msg({ from: '[email protected]', calendarMethod: 'REQUEST' })],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: '#5586: a tracker thread marked Auto-Submitted (no List-Unsubscribe) is not eligible',
messages: [msg({ from: '[email protected]', autoSubmitted: true }), msg({ from: '[email protected]', autoSubmitted: true })],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: '#5586: my own auto-reply is not owner participation',
messages: [msg({ from: '[email protected]', autoSubmitted: true }), msg({ from: '[email protected]', labels: ['SENT'], autoSubmitted: true })],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: '#5586: a human message beside an auto-submitted one keeps the thread eligible',
messages: [msg({ from: '[email protected]', autoSubmitted: true }), msg({ from: '[email protected]', autoSubmitted: false })],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'a human "Notification: ..." thread with no calendar part is eligible',
messages: [msg({ from: '[email protected]', subject: 'Notification: your invoice is ready' })],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'an unrecognised MIME method does not make a thread a calendar notice',
messages: [msg({ from: '[email protected]', calendarMethod: 'BOGUS' })],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'CATEGORY_PROMOTIONS without an owner message is not eligible',
messages: [msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_PROMOTIONS'] })],
eligible: false,
reason: 'bulk_category',
},
{
name: 'CATEGORY_SOCIAL without an owner message is not eligible',
messages: [msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_SOCIAL'] })],
eligible: false,
reason: 'bulk_category',
},
{
name: 'CATEGORY_FORUMS without an owner message is not eligible',
messages: [msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_FORUMS'] })],
eligible: false,
reason: 'bulk_category',
},
{
name: 'CATEGORY_UPDATES is NOT excluded — invoices and documents live there',
messages: [msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_UPDATES'] })],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'List-Unsubscribe bulk mail the owner never answered is not eligible',
messages: [msg({ from: '[email protected]', listUnsub: true })],
eligible: false,
reason: 'list_mail',
},
{
name: 'a SENT message keeps a promotions thread eligible',
messages: [
msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_PROMOTIONS'] }),
msg({ from: '[email protected]', labels: ['SENT'], to: ['[email protected]'] }),
],
eligible: true,
reason: 'owner_participated',
},
{
name: 'a SENT message keeps a List-Unsubscribe thread eligible',
messages: [
msg({ from: '[email protected]', listUnsub: true }),
msg({ from: '[email protected]', labels: ['SENT'], to: ['[email protected]'] }),
],
eligible: true,
reason: 'owner_participated',
},
{
name: 'owner recognised by address even without the SENT label',
messages: [
msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_SOCIAL'] }),
msg({ from: '[email protected]', to: ['[email protected]'] }),
],
eligible: true,
reason: 'owner_participated',
},
{
name: 'mixed thread: one bulk message, one real human message → eligible',
messages: [
msg({ from: '[email protected]', listUnsub: true }),
msg({ from: '[email protected]' }),
],
eligible: true,
reason: 'human_correspondence',
},
{
name: 'an empty thread is not eligible',
messages: [],
eligible: false,
reason: 'no_substantive_messages',
},
// Ship-review fix: owner_participated used to be evaluated BEFORE the
// calendar/noise filter, so an invitation the owner merely RSVP'd to (the
// "Accepted:" notice Calendar sends on the owner's behalf — SENT label,
// METHOD:REPLY) was eligible and paid for a model call. The owner must have
// written a SUBSTANTIVE message; a pure-calendar exchange is never one.
{
name: "an invitation the owner RSVP'd to (Accepted: reply, SENT, METHOD:REPLY) is still pure calendar mail",
messages: [
msg({ from: '[email protected]', calendarMethod: 'REQUEST', subject: 'Invitation: Sync @ Tue' }),
msg({
from: '[email protected]',
to: ['[email protected]'],
labels: ['SENT'],
calendarMethod: 'REPLY',
subject: 'Accepted: Sync @ Tue',
}),
],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: "an owner RSVP recognised by ADDRESS (no SENT label) is still pure calendar mail",
messages: [
msg({ from: '[email protected]', calendarMethod: 'REQUEST', subject: 'Invitation: Sync @ Tue' }),
msg({ from: '[email protected]', to: ['[email protected]'], calendarMethod: 'REPLY', subject: 'Accepted: Sync @ Tue' }),
],
eligible: false,
reason: 'no_substantive_messages',
},
{
name: "the owner's REAL reply to an invitation (no calendar part) keeps the thread eligible",
messages: [
msg({ from: '[email protected]', calendarMethod: 'REQUEST', subject: 'Invitation: Sync @ Tue' }),
msg({
from: '[email protected]',
to: ['[email protected]'],
labels: ['SENT'],
subject: 'Re: Invitation: Sync @ Tue',
body: 'Can we push to Wednesday? I will send the agenda tonight.',
}),
],
eligible: true,
reason: 'owner_participated',
},
{
name: 'an owner RSVP does not rescue a bulk thread either — the owner wrote nothing substantive',
messages: [
msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_PROMOTIONS'] }),
msg({ from: '[email protected]', labels: ['SENT'], calendarMethod: 'REPLY', subject: 'Accepted: Webinar' }),
],
eligible: false,
reason: 'bulk_category',
},
];
describe('loopExtractionEligibility', () => {
for (const row of ROWS) {
test(row.name, () => {
const v = loopExtractionEligibility(thread(row.messages), MY);
expect(v.eligible).toBe(row.eligible);
expect(v.reason).toBe(row.reason);
});
}
test('a SENT message alone is enough with no myAddresses supplied', () => {
// The gate must work from Gmail labels alone — callers that cannot
// resolve the owner's alias set still get the owner override.
const v = loopExtractionEligibility(
thread([
msg({ from: '[email protected]', labels: ['INBOX', 'CATEGORY_PROMOTIONS'] }),
msg({ from: '[email protected]', labels: ['SENT'] }),
]),
);
expect(v.eligible).toBe(true);
expect(v.reason).toBe('owner_participated');
});
});