Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
15.0 KB
Raw Blame
/**
 * applyThreadLoopVerdict (src/core/google/loop-detect.ts) on PGLite:
 * verdict → open_loops rows, reply-driven auto-close, idempotent re-apply,
 * counterparty slug resolution (alias-exact only — never fallback_slugify),
 * and the suppression cache seam.
 *
 * Synthetic data only.
 */
import { describe, expect, test, beforeAll, afterAll, beforeEach } from 'bun:test';

import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { resetPgliteState } from './helpers/reset-pglite.ts';
import {
  applyThreadLoopVerdict,
  __clearSuppressionCacheForTests,
} from '../src/core/google/loop-detect.ts';
import { addSuppression, listOpenLoops } from '../src/core/loops/loops-store.ts';
import { normalizeAlias } from '../src/core/search/alias-normalize.ts';
import type { GmailMessageMeta, GmailThreadData } from '../src/core/google/types.ts';

let engine: PGLiteEngine;

beforeAll(async () => {
  engine = new PGLiteEngine();
  await engine.connect({});
  await engine.initSchema();
});

afterAll(async () => {
  await engine.disconnect();
});

beforeEach(async () => {
  await resetPgliteState(engine);
  __clearSuppressionCacheForTests();
  await engine.executeRaw(
    `INSERT INTO sources (id, name, config) VALUES ('g1', 'g1', '{"kind":"google"}'::jsonb)
     ON CONFLICT (id) DO NOTHING`,
  );
});

const NOW = new Date('2026-08-25T12:00:00Z');
const MY = new Set(['[email protected]']);

let msgSeq = 0;

function msg(spec: {
  from: string;
  to?: string[];
  ageHours: number;
  sent?: boolean;
  body?: string;
  threadId?: string;
}): GmailMessageMeta {
  const internalDateMs = NOW.getTime() - spec.ageHours * 3_600_000;
  msgSeq += 1;
  return {
    id: `18c2f4a9b3d2${(0x2000 + msgSeq).toString(16)}`,
    threadId: spec.threadId ?? '18c2f4a9b3d21e07',
    from: spec.from,
    fromAddress: spec.from.toLowerCase(),
    to: (spec.to ?? []).map((a) => a.toLowerCase()),
    cc: [],
    subject: 'Quarterly plan',
    dateIso: new Date(internalDateMs).toISOString(),
    internalDateMs,
    labelIds: spec.sent ? ['SENT'] : ['INBOX'],
    listUnsubscribe: false,
    bodyText: spec.body ?? 'Can you review the plan?',
  };
}

function thread(messages: GmailMessageMeta[], threadId = '18c2f4a9b3d21e07'): GmailThreadData {
  return { threadId, account: '[email protected]', messages };
}

/** Inbound thread from `from` that owes a reply (48h old, addressed to me). */
function inboundThread(from: string, threadId = '18c2f4a9b3d21e07'): GmailThreadData {
  return thread([msg({ from, to: ['[email protected]'], ageHours: 48, threadId })], threadId);
}

describe('applyThreadLoopVerdict', () => {
  test('N7-6: opened_at is the request time, so a backfill ranks a 30-day wait older than a 30-hour one', async () => {
    for (const [tid, age] of [['18c2f4a9b3d21e01', 720], ['18c2f4a9b3d21e02', 30]] as const) {
      await applyThreadLoopVerdict(engine, 'g1', thread([msg({ from: `${tid}@example.com`, to: ['[email protected]'], ageHours: age, threadId: tid })], tid), MY, null, NOW);
    }
    const rows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    const opened = new Map(rows.map((r) => [r.thread_id, Date.parse(r.opened_at)]));
    expect(opened.get('18c2f4a9b3d21e01')).toBe(NOW.getTime() - 720 * 3_600_000);
    expect(opened.get('18c2f4a9b3d21e02')).toBe(NOW.getTime() - 30 * 3_600_000);
  });

  test('N7-6: a re-apply keeps the earlier opened_at and repairs a detection-time one', async () => {
    const t = thread([msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 300 })]);
    await applyThreadLoopVerdict(engine, 'g1', t, MY, null, NOW);
    await engine.executeRaw(`UPDATE open_loops SET opened_at = now()`);
    await applyThreadLoopVerdict(engine, 'g1', t, MY, null, NOW);
    const [row] = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(Date.parse(row.opened_at)).toBe(NOW.getTime() - 300 * 3_600_000);
  });

  test('creates the loop row with dedup thread:<tid>:unanswered_inbound and merges page_slug into evidence', async () => {
    await applyThreadLoopVerdict(
      engine,
      'g1',
      inboundThread('[email protected]'),
      MY,
      'emails/quarterly-plan',
      NOW,
    );
    const rows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(rows).toHaveLength(1);
    const row = rows[0];
    expect(row.dedup_key).toBe('thread:18c2f4a9b3d21e07:unanswered_inbound');
    expect(row.loop_type).toBe('unanswered_inbound');
    expect(row.detector).toBe('deterministic_thread');
    expect(row.counterparty_email).toBe('[email protected]');
    expect(row.thread_id).toBe('18c2f4a9b3d21e07');
    expect(row.page_slug).toBe('emails/quarterly-plan');
    expect(row.evidence).toHaveLength(1);
    expect(row.evidence[0].page_slug).toBe('emails/quarterly-plan');
    expect(row.evidence[0].quote).toBe('Can you review the plan?');
  });

  test('a second apply with a replied thread closes the loop (closed_by reply_detected)', async () => {
    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(1);

    // The thread now ends with MY reply (no '?') — nothing is owed.
    const replied = thread([
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 48 }),
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 1, sent: true, body: 'Done.' }),
    ]);
    await applyThreadLoopVerdict(engine, 'g1', replied, MY, null, NOW);

    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(0);
    const done = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'done' });
    expect(done).toHaveLength(1);
    expect(done[0].closed_by).toBe('reply_detected');
  });

  test('idempotent re-apply does not duplicate; a later re-apply after close reopens', async () => {
    const t = inboundThread('[email protected]');
    await applyThreadLoopVerdict(engine, 'g1', t, MY, null, NOW);
    await applyThreadLoopVerdict(engine, 'g1', t, MY, null, NOW);
    let rows = await listOpenLoops(engine, { sourceIds: ['g1'] });
    expect(rows).toHaveLength(1);
    expect(rows[0].status).toBe('open');
    const id = rows[0].id;

    // Reply closes it...
    const replied = thread([
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 48 }),
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 40, sent: true, body: 'Done.' }),
    ]);
    await applyThreadLoopVerdict(engine, 'g1', replied, MY, null, NOW);
    // ...a replay carrying NO newer activity must NOT reopen — that's the
    // manual-close-revert guard (a label-only history touch re-emits the
    // same spec; reverting a close on it would silently undo `loops done`)...
    await applyThreadLoopVerdict(engine, 'g1', t, MY, null, NOW);
    const closedRows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'done' });
    expect(closedRows).toHaveLength(1);
    expect(closedRows[0].id).toBe(id);
    // ...but a NEW inbound (genuinely newer activity, past the 24h grace)
    // reopens THE SAME row (dedup conflict path).
    const reAsked = thread([
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 48 }),
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 40, sent: true, body: 'Done.' }),
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 30 }),
    ]);
    await applyThreadLoopVerdict(engine, 'g1', reAsked, MY, null, NOW);
    rows = await listOpenLoops(engine, { sourceIds: ['g1'] });
    expect(rows).toHaveLength(1);
    expect(rows[0].id).toBe(id);
    expect(rows[0].status).toBe('open');
    expect(rows[0].closed_at).toBeNull();
  });

  test('outbound → inbound flip closes the outbound loop and opens the inbound one', async () => {
    // I asked 96h ago — waiting on them.
    const asked = thread([
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 96, sent: true, body: 'Any update?' }),
    ]);
    await applyThreadLoopVerdict(engine, 'g1', asked, MY, null, NOW);
    let open = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(open.map((l) => l.loop_type)).toEqual(['unanswered_outbound']);

    // They replied 48h ago — now I owe the reply.
    const theyReplied = thread([
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 96, sent: true, body: 'Any update?' }),
      msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 48 }),
    ]);
    await applyThreadLoopVerdict(engine, 'g1', theyReplied, MY, null, NOW);
    open = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(open.map((l) => l.loop_type)).toEqual(['unanswered_inbound']);
    const done = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'done' });
    expect(done.map((l) => l.loop_type)).toEqual(['unanswered_outbound']);
    expect(done[0].closed_by).toBe('reply_detected');
  });

  test('counterparty_slug resolves via alias-exact when a person page carries the email alias in the same source', async () => {
    await engine.putPage(
      'people/carol-example',
      { title: 'Carol Example', type: 'person', compiled_truth: 'Carol, a founder.' },
      { sourceId: 'g1' },
    );
    await engine.setPageAliases('people/carol-example', 'g1', [
      normalizeAlias('[email protected]'),
    ]);

    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    const rows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(rows).toHaveLength(1);
    expect(rows[0].counterparty_slug).toBe('people/carol-example');
    expect(rows[0].counterparty_email).toBe('[email protected]');
  });

  test('counterparty_slug stays null when resolution would be fallback_slugify only', async () => {
    // No page, no alias for this address — resolveEntitySlugWithSource can
    // only fabricate a slug (fallback_slugify), which must NOT be persisted.
    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    const rows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(rows).toHaveLength(1);
    expect(rows[0].counterparty_slug).toBeNull();
    expect(rows[0].counterparty_email).toBe('[email protected]');
  });

  // #5504 (wave 14 PR4, T8): an exact-email alias is an identity signal, so a page carrying it in exactly
  // one other source resolves and the loop records that source; the loop row itself stays in the
  // connector source. Name matches never cross sources (test/loops-counterparty-null-entity.test.ts).
  test('exact-email alias in a DIFFERENT source resolves and records its source (#5504)', async () => {
    await engine.executeRaw(
      `INSERT INTO sources (id, name) VALUES ('g2', 'g2') ON CONFLICT (id) DO NOTHING`,
    );
    await engine.putPage(
      'people/carol-example',
      { title: 'Carol Example', type: 'person', compiled_truth: 'Carol.' },
      { sourceId: 'g2' },
    );
    await engine.setPageAliases('people/carol-example', 'g2', [
      normalizeAlias('[email protected]'),
    ]);

    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    const rows = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(rows).toHaveLength(1);
    expect(rows[0].source_id).toBe('g1');
    expect(rows[0].counterparty_slug).toBe('people/carol-example');
    expect(rows[0].counterparty_source_id).toBe('g2');
  });

  test('suppressed sender never opens a NEW loop (with the cache seam cleared)', async () => {
    await addSuppression(engine, 'g1', 'sender', '[email protected]');
    __clearSuppressionCacheForTests();

    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'] })).toHaveLength(0);

    // Unsuppressed sender on another thread still opens.
    await applyThreadLoopVerdict(
      engine,
      'g1',
      inboundThread('[email protected]', '18c2f4a9b3d21e08'),
      MY,
      null,
      NOW,
    );
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(1);
  });

  test('suppressed thread never opens a NEW loop', async () => {
    await addSuppression(engine, 'g1', 'thread', '18c2f4a9b3d21e07');
    __clearSuppressionCacheForTests();

    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'] })).toHaveLength(0);
  });

  test('REGRESSION: muting does NOT close an existing open loop (no phantom reply_detected)', async () => {
    // Open a loop while unsuppressed…
    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(1);
    // …then mute the sender and re-apply the SAME still-unanswered thread.
    // The suppressed verdict is empty, but the RAW verdict still holds —
    // closes are decided by the raw verdict, so the loop must stay open
    // ("existing loops keep their state"), not close as 'reply_detected'.
    await addSuppression(engine, 'g1', 'sender', '[email protected]');
    __clearSuppressionCacheForTests();
    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    const open = await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' });
    expect(open).toHaveLength(1);
    expect(open[0].closed_by).toBeNull();
  });

  test('suppression cache seam: without clearing, a stale cached set is served; clearing picks up new mutes', async () => {
    // Prime the cache with the empty suppression set.
    await applyThreadLoopVerdict(engine, 'g1', inboundThread('[email protected]'), MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(1);

    // Mute frank — but the 60s cache still holds the empty set, so a re-apply
    // on a NEW thread still opens (documents the TTL behavior).
    await addSuppression(engine, 'g1', 'sender', '[email protected]');
    await applyThreadLoopVerdict(
      engine,
      'g1',
      inboundThread('[email protected]', '18c2f4a9b3d21e09'),
      MY,
      null,
      NOW,
    );
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(2);

    // Clearing the cache makes the mute effective.
    __clearSuppressionCacheForTests();
    await applyThreadLoopVerdict(
      engine,
      'g1',
      inboundThread('[email protected]', '18c2f4a9b3d21e0a'),
      MY,
      null,
      NOW,
    );
    expect(await listOpenLoops(engine, { sourceIds: ['g1'], status: 'open' })).toHaveLength(2);
  });

  test('a thread with no loop state is a no-op (no rows created, no throw)', async () => {
    // Fresh inbound inside the grace window.
    const fresh = thread([msg({ from: '[email protected]', to: ['[email protected]'], ageHours: 2 })]);
    await applyThreadLoopVerdict(engine, 'g1', fresh, MY, null, NOW);
    expect(await listOpenLoops(engine, { sourceIds: ['g1'] })).toHaveLength(0);
  });
});