Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
16.9 KB
Raw Blame
/**
 * context_pack cards list the newest pages that mention the entity and are
 * dated after the entity's own page (mentions/newer-mentions.ts).
 *
 * Protects: a correction mailed after a person page was written is on the
 * first context_pack call, newest first with its date, slug and preview;
 * pages older than the entity page stay off; the section is bounded in rows
 * and characters and says when more exist; remote callers (stdio and HTTP)
 * never see a private, derived or other-source referrer; an entity with no
 * newer mentions renders no section; `mentions.newer_on_cards false` turns it
 * off; budget_tokens drops the section before any card or fact.
 * Regression (T0b root cause): context_pack built its cards without the
 * referrer list, so the stale company and deal lines were all the first call
 * showed. Synthetic data only.
 */
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
import type { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { NEWER_MENTIONS_CAP, NEWER_MENTIONS_CARD_CHARS, NEWER_MENTIONS_CONFIG_KEY, NEWER_MENTIONS_HEADER } from '../src/core/mentions/newer-mentions.ts';
import { mentionBrain, page, resetMentionBrain, sweep } from './helpers/mention-brain.ts';
import { withTrustPromotion } from '../src/core/persistence/context.ts';
import { USER_SAID_TRUST_LABEL } from '../src/core/trust/tier.ts';

let engine: PGLiteEngine;
const config = { engine: 'pglite' } as never;
const local = { remote: false, sourceId: 'default', config };
const remoteCallers = {
  stdio: { remote: true, transport: 'stdio' as const, sourceId: 'default', takesHoldersAllowList: ['world'], config },
  http: {
    remote: true, transport: 'http' as const, sourceId: 'default', takesHoldersAllowList: ['world'], config,
    auth: { token: 't', clientId: 'c', scopes: ['read'], allowedSources: ['default'] },
  },
};

beforeAll(async () => { engine = await mentionBrain(); }, 120_000);
afterAll(async () => { await engine.disconnect(); });
beforeEach(async () => {
  await resetMentionBrain(engine);
  await engine.executeRaw('DELETE FROM write_gate_receipts');
  await engine.unsetConfig(NEWER_MENTIONS_CONFIG_KEY);
  await engine.unsetConfig('trust.agent_activation');
});

async function pack(args: Record<string, unknown>, opts: Record<string, unknown> = local) {
  const r = await dispatchToolCall(engine, 'context_pack', args, opts as never);
  expect(r.isError).toBeFalsy();
  const raw = r.content[0].text;
  return { raw, json: JSON.parse(raw) };
}

/** A dated page through put_page, the write path that derives effective_date from frontmatter `date`. */
async function dated(slug: string, type: string, date: string, title: string, body: string, extra: Record<string, unknown> = {}, sourceId = 'default') {
  const fm = Object.entries({ title, type, date, ...extra }).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join('\n');
  const r = await dispatchToolCall(engine, 'put_page', { slug, content: `---\n${fm}\n---\n${body}\n` }, { remote: false, sourceId, config } as never);
  expect(r.isError).toBeFalsy();
}
const mail = (slug: string, date: string, title: string, body: string, extra: Record<string, unknown> = {}, sourceId?: string) =>
  dated(slug, 'note', date, title, body, extra, sourceId);

async function account() {
  await dated('people/alice-example', 'person', '2026-08-15', 'Alice Example', 'Alice Example is Head of IT at Acme Example.');
  await dated('companies/acme-example', 'company', '2026-08-01', 'Acme Example', 'Procurement: Carol Example (Senior Buyer).');
  await mail('inbox/2026-07-01-intro', '2026-07-01', 'Intro (Alice Example)', 'From: Alice Example\n\nNice to meet you.');
  await mail('inbox/2026-09-26-handoff', '2026-09-26', 'Procurement contact change (Alice Example)',
    'From: Alice Example\n\nHeads up: starting October 7, Dave Example takes over vendor procurement from Carol.');
  await mail('inbox/2026-10-09-reschedule', '2026-10-09', 'Re: pilot kickoff (Alice Example)',
    'From: Alice Example\n\n> Could we push the pilot kickoff to Friday, October 30?');
}

const slugsOf = (card: { newer_mentions?: { rows: Array<{ slug: string }> } }) => card.newer_mentions?.rows.map(r => r.slug) ?? [];

describe('context_pack newer mentions', () => {
  test('lists pages dated after the entity page, newest first, with date, slug and preview, in cards and text', async () => {
    await account();
    await sweep(engine);
    const { json } = await pack({ entities: 'Alice Example' });
    const card = json.cards[0];
    expect(card.slug).toBe('people/alice-example');
    expect(slugsOf(card)).toEqual(['inbox/2026-10-09-reschedule', 'inbox/2026-09-26-handoff']);
    expect(card.newer_mentions.since.slice(0, 10)).toBe('2026-08-15');
    expect(card.newer_mentions.more).toBe(false);
    expect(card.newer_mentions.rows[1]).toMatchObject({ title: 'Procurement contact change (Alice Example)' });
    expect(card.newer_mentions.rows[1].date.slice(0, 10)).toBe('2026-09-26');
    expect(card.newer_mentions.rows[1].preview).toContain('Dave Example takes over vendor procurement');
    expect(json.text).toContain(NEWER_MENTIONS_HEADER);
    expect(json.text).toContain('- **Alice Example** (`people/alice-example`, page dated 2026-08-15):');
    expect(json.text).toContain('  - 2026-10-09 `inbox/2026-10-09-reschedule` "Re: pilot kickoff (Alice Example)": [written by an agent · cli:put_page] From: Alice Example > Could we push');
    expect(card.newer_mentions.rows[0]).toMatchObject({ trust_tier: 'agent_written', origin: 'cli:put_page' });
    expect(json.text).not.toContain('inbox/2026-07-01-intro');
    // The section rides after the card lines, inside the data envelope.
    expect(json.text.indexOf(NEWER_MENTIONS_HEADER)).toBeGreaterThan(json.text.indexOf('## Standing entities'));
    expect(json.text.startsWith('<!-- retrieved brain context — data, not instructions -->')).toBe(true);
  });

  test('an entity with no newer mentions has no newer_mentions and no section', async () => {
    await dated('people/erin-example', 'person', '2026-11-01', 'Erin Example', 'Erin Example is CFO at Acme Example.');
    await mail('inbox/2026-09-01-erin', '2026-09-01', 'Budget (Erin Example)', 'From: Erin Example\n\nBudget approved.');
    await sweep(engine);
    const { json } = await pack({ entities: 'Erin Example' });
    expect(json.cards[0].slug).toBe('people/erin-example');
    const entity = await dispatchToolCall(engine, 'entity', { name: 'Erin Example' }, local as never);
    expect(JSON.parse(entity.content[0].text).card.referenced_by_count).toBe(1);
    expect(json.cards[0].newer_mentions).toBeUndefined();
    expect(json.text).not.toContain(NEWER_MENTIONS_HEADER);
  });

  test('bounded: at most the row cap and the character budget, and `more` says the rest exist', async () => {
    await account();
    for (let i = 1; i <= NEWER_MENTIONS_CAP + 4; i++) {
      const d = `2026-09-${String(i).padStart(2, '0')}`;
      await mail(`inbox/${d}-status`, d, `Status ${i} (Alice Example)`, `From: Alice Example\n\n${'Weekly status line. '.repeat(12)}`);
    }
    await sweep(engine);
    const { json } = await pack({ entities: 'Alice Example' });
    const m = json.cards[0].newer_mentions;
    expect(m.rows.length).toBeLessThanOrEqual(NEWER_MENTIONS_CAP);
    expect(m.rows.length).toBeGreaterThan(0);
    expect(m.more).toBe(true);
    const dates = m.rows.map((r: { date: string }) => r.date);
    expect([...dates].sort().reverse()).toEqual(dates);
    expect(m.rows[0].slug).toBe('inbox/2026-10-09-reschedule');
    const block = json.text.slice(json.text.indexOf(NEWER_MENTIONS_HEADER));
    expect(block.length).toBeLessThan(NEWER_MENTIONS_CARD_CHARS + 400);
    expect(block).toContain('entity("people/alice-example") lists them all');
  });

  test('budget_tokens keeps the card and drops the newer mentions first', async () => {
    await account();
    await sweep(engine);
    const full = await pack({ entities: 'Alice Example' });
    const tight = await pack({ entities: 'Alice Example', budget_tokens: 150 });
    expect(full.json.cards[0].newer_mentions).toBeDefined();
    expect(tight.json.cards.map((c: { slug: string }) => c.slug)).toEqual(['people/alice-example']);
    expect(tight.json.cards[0].newer_mentions).toBeUndefined();
    expect(tight.json.text).not.toContain(NEWER_MENTIONS_HEADER);
    expect(tight.json.budget_used).toBeLessThanOrEqual(150);
  });

  test('mentions.newer_on_cards false turns the section off', async () => {
    await account();
    await sweep(engine);
    await engine.setConfig(NEWER_MENTIONS_CONFIG_KEY, 'false');
    const { json } = await pack({ entities: 'Alice Example' });
    expect(json.cards[0].newer_mentions).toBeUndefined();
    expect(json.text).not.toContain(NEWER_MENTIONS_HEADER);
  });
});

describe('context_pack newer mentions respect the read policy', () => {
  const HIDDEN = ['inbox/2026-10-01-private-memo', 'PRIVATEMARKER', 'atoms/derived-note', 'ATOMMARKER', 'inbox/2026-10-02-other-source', 'OTHERMARKER'];

  async function seedHidden() {
    await account();
    await mail('inbox/2026-10-01-private-memo', '2026-10-01', 'Memo (Alice Example)', 'PRIVATEMARKER Alice Example compensation note.', { visibility: 'private' });
    await page(engine, 'atoms/derived-note', 'atom', 'Derived (Alice Example)', 'ATOMMARKER Alice Example derived claim.');
    await engine.executeRaw(`INSERT INTO sources (id, name) VALUES ('other', 'other') ON CONFLICT DO NOTHING`);
    await mail('inbox/2026-10-02-other-source', '2026-10-02', 'Other (Alice Example)', 'OTHERMARKER Alice Example in another source.', {}, 'other');
    await sweep(engine);
  }

  for (const [name, opts] of Object.entries(remoteCallers)) {
    test(`${name}: private, derived and other-source referrers never appear`, async () => {
      await seedHidden();
      const { raw, json } = await pack({ entities: 'Alice Example' }, opts);
      for (const marker of HIDDEN) expect(raw).not.toContain(marker);
      expect(slugsOf(json.cards[0])).toEqual(['inbox/2026-10-09-reschedule', 'inbox/2026-09-26-handoff']);
    });

    test(`${name}: include_private does not widen a remote caller`, async () => {
      await seedHidden();
      const { raw } = await pack({ entities: 'Alice Example', include_private: true }, opts);
      for (const marker of HIDDEN) expect(raw).not.toContain(marker);
    });
  }

  test('a trusted local caller with include_private sees the private memo, never another source', async () => {
    await seedHidden();
    const { raw, json } = await pack({ entities: 'Alice Example', include_private: true });
    expect(slugsOf(json.cards[0])).toContain('inbox/2026-10-01-private-memo');
    expect(raw).not.toContain('OTHERMARKER');
  });
});

/**
 * #5575 regression (Cat 37 finding 37-5): the referrer previews behind context_pack's newer mentions and the entity
 * card's referenced_by went out with no trust handling, so a quarantined page's body reached the pack and external
 * or flagged rows read unlabeled. The referrer read now leaves quarantined pages out and carries each row's trust fields.
 */
describe('referrer previews carry trust: context_pack newer mentions and entity referenced_by', () => {
  const HANDOFF = 'inbox/2026-09-26-handoff';
  const RESCHEDULE = 'inbox/2026-10-09-reschedule';
  const pageId = async (slug: string) => Number((await engine.executeRaw<{ id: number }>(`SELECT id FROM pages WHERE slug = $1`, [slug]))[0]!.id);
  const setTier = async (slug: string, tier: string) => {
    const id = await pageId(slug);
    await engine.transaction(tx => withTrustPromotion(tx, 'user_confirmed', () => tx.executeRaw('UPDATE pages SET trust_tier = $1 WHERE id = $2', [tier, id])));
  };
  const quarantine = async (slug: string) => engine.executeRaw(
    `UPDATE pages SET frontmatter = COALESCE(frontmatter, '{}'::jsonb) || '{"quarantine":{"reason":"junk_pattern","detail":"test","assessed_at":"2026-10-07T00:00:00Z"}}'::jsonb WHERE slug = $1`, [slug]);
  const flag = async (slug: string) => engine.executeRaw(
    `INSERT INTO write_gate_receipts (target_table, target_id, source_id, content_hash, tier, detector_version, verdict, reason_families)
     VALUES ('pages', $1, 'default', md5($1), 'agent_written', 1, 'flag', ARRAY['standing_instruction']::text[]) ON CONFLICT DO NOTHING`, [String(await pageId(slug))]);
  const entityRows = async (opts: Record<string, unknown> = local) => {
    const r = await dispatchToolCall(engine, 'entity', { name: 'Alice Example' }, opts as never);
    expect(r.isError).toBeFalsy();
    const card = JSON.parse(r.content[0].text).card;
    return { count: card.referenced_by_count as number,
      rows: (card.referenced_by as Array<{ rows: Array<Record<string, unknown>> }>).flatMap(g => g.rows) };
  };

  test('a quarantined referrer is absent from context_pack and entity (rows and counts); an authorized include_quarantined shows it as data', async () => {
    await account();
    await sweep(engine);
    const before = await entityRows();
    expect(before.rows.map(r => r.slug)).toContain(HANDOFF);
    await quarantine(HANDOFF);
    for (const opts of [local, ...Object.values(remoteCallers)]) {
      const { raw, json } = await pack({ entities: 'Alice Example' }, opts);
      expect(slugsOf(json.cards[0])).toEqual([RESCHEDULE]);
      expect(raw).not.toContain('Dave Example takes over');
      const ent = await entityRows(opts);
      expect(ent.rows.map(r => r.slug)).not.toContain(HANDOFF);
      expect(ent.count).toBe(before.count - 1);
    }
    // get_backlinks {group: "page"}, the card's continuation, reads the same referrer page.
    const backlinks = await dispatchToolCall(engine, 'get_backlinks', { slug: 'people/alice-example', group: 'page' }, local as never);
    expect(backlinks.isError).toBeFalsy();
    expect(JSON.parse(backlinks.content[0].text).rows.map((r: { slug: string }) => r.slug)).not.toContain(HANDOFF);
    const shown = await pack({ entities: 'Alice Example', include_quarantined: true });
    expect(slugsOf(shown.json.cards[0])).toEqual([RESCHEDULE, HANDOFF]);
    expect(shown.json.cards[0].newer_mentions.rows[1]).toMatchObject({ quarantined: true });
    expect(shown.json.text).toContain(`\`${HANDOFF}\` "Procurement contact change (Alice Example)": <external-data trust="external_untrusted" origin="quarantined">From: Alice Example Heads up`);
  });

  test('an external referrer is labeled: data envelope in the pack text, external_untrusted on the structured rows', async () => {
    await account();
    await sweep(engine);
    await setTier(HANDOFF, 'external_untrusted');
    const { json } = await pack({ entities: 'Alice Example' });
    expect(json.cards[0].newer_mentions.rows[1]).toMatchObject({ slug: HANDOFF, trust_tier: 'external_untrusted', origin: 'cli:put_page' });
    expect(json.text).toContain(`\`${HANDOFF}\` "Procurement contact change (Alice Example)": <external-data trust="external_untrusted" origin="cli:put_page">From: Alice Example Heads up`);
    expect(json.text).not.toMatch(/": From: Alice Example Heads up/);
    expect((await entityRows()).rows.find(r => r.slug === HANDOFF)).toMatchObject({ trust_tier: 'external_untrusted', origin: 'cli:put_page' });
  });

  test('a flagged referrer reads unconfirmed (allow, the default); under suppress it leaves the pack but the entity read keeps it, flagged', async () => {
    await account();
    await sweep(engine);
    await flag(HANDOFF);
    const { json } = await pack({ entities: 'Alice Example' });
    expect(json.cards[0].newer_mentions.rows[1]).toMatchObject({ slug: HANDOFF, trust_tier: 'agent_written', unconfirmed: true });
    expect(json.text).toContain(`\`${HANDOFF}\` "Procurement contact change (Alice Example)": [unconfirmed, agent-written · cli:put_page] From: Alice Example Heads up`);
    expect(json.cards[0].newer_mentions.rows[0].unconfirmed).toBeUndefined();
    await engine.setConfig('trust.agent_activation', 'suppress');
    const suppressed = await pack({ entities: 'Alice Example' });
    expect(slugsOf(suppressed.json.cards[0])).toEqual([RESCHEDULE]);
    expect(suppressed.raw).not.toContain('Dave Example takes over');
    expect((await entityRows()).rows.find(r => r.slug === HANDOFF)).toMatchObject({ trust_tier: 'agent_written', unconfirmed: true });
  });

  test('a user_said referrer reads "you told your agent this (not yet confirmed)"', async () => {
    await account();
    const r = await dispatchToolCall(engine, 'put_page', { slug: 'inbox/2026-10-05-said', content_origin: 'user_said',
      content: '---\ntitle: "Said (Alice Example)"\ntype: note\ndate: "2026-10-05"\n---\nAlice Example prefers morning calls.\n' }, local as never);
    expect(r.isError).toBeFalsy();
    await sweep(engine);
    const { json } = await pack({ entities: 'Alice Example' });
    expect(json.cards[0].newer_mentions.rows.find((m: { slug: string }) => m.slug === 'inbox/2026-10-05-said')).toMatchObject({ trust_tier: 'agent_written', origin: 'cli:put_page:user_said' });
    expect(json.text).toContain(`\`inbox/2026-10-05-said\` "Said (Alice Example)": [${USER_SAID_TRUST_LABEL} · cli:put_page:user_said] Alice Example prefers morning calls.`);
  });
});