Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(chronicle): disambiguate same-day event slugs
garrytan6b9cdfd96d88
diff --git a/scripts/e2e-test-map.ts b/scripts/e2e-test-map.tsindex 722f3dd6f3..af1615cd0b 100644--- a/scripts/e2e-test-map.ts+++ b/scripts/e2e-test-map.ts@@ -501,6 +501,7 @@ export const E2E_TEST_MAP: Record<string, string[]> = {"test/e2e/chronicle-auto-decision-postgres.test.ts","test/e2e/chronicle-auto-phase-postgres.test.ts","test/e2e/chronicle-event-privacy-postgres.test.ts",+ "test/e2e/chronicle-event-identity-postgres.test.ts","test/e2e/chronicle-surfaces-postgres.test.ts",],// #5876: doctor auto_chronicle + the chronicle advisor read the ledger rollups.diff --git a/src/core/chronicle/contract.ts b/src/core/chronicle/contract.tsindex c40966ee16..73739e7530 100644--- a/src/core/chronicle/contract.ts+++ b/src/core/chronicle/contract.ts@@ -22,8 +22,13 @@import { CHRONICLE_RUN_NOW_ARGV, type ChronicleReasonCode } from './reasons.ts';import type { Action } from '../agent-output.ts';-/** Bump to re-extract every page once (the ledger key includes it). */-export const CHRONICLE_EXTRACTOR_VERSION = 1;+/**+ * The ledger key includes it, so a bump makes already-extracted content new again: the phase's+ * discovery re-decides pages changed since activation (unmanaged brains judge the recent ones under+ * the daily limit; managed brains record `no_write_decision`) and backfill re-queues the rest on request.+ * 2: same-day event slug disambiguation (event-identity.ts) and ended-invite projection (invite-projection.ts).+ */+export const CHRONICLE_EXTRACTOR_VERSION = 2;/** Ledger table and its columns (migration `chronicle_page_state`). */export const CHRONICLE_LEDGER_TABLE = 'chronicle_page_state';diff --git a/src/core/chronicle/event-identity.ts b/src/core/chronicle/event-identity.tsnew file mode 100644index 0000000000..42706d17a4--- /dev/null+++ b/src/core/chronicle/event-identity.ts@@ -0,0 +1,95 @@+/**+ * Life Chronicle event identity: same-day disambiguation.+ *+ * `buildChronicleEvent` slugs an event `life/events/<day>-<hash8>` with the+ * hash over who, what and the depth page. Two proposals of one depth page+ * that share who, what and day (two "Call with alice-example" on one day)+ * get the same base slug. Publication resolves each such group here:+ *+ * - An event's discriminator is its instant (`when`, normalized), `where`+ * and `kind`, the fields a group's members can differ in.+ * - One member keeps the bare base slug; every other distinct member gets+ * `<base>-<hash6>`, the hash over its discriminator. Identical copies+ * beyond the first get `-2`, `-3`.+ * - Assignment reads the pages already at the candidate slugs, so it+ * survives reordering, corrections and re-runs: the member whose+ * discriminator the base page holds keeps the base slug, a member whose+ * suffixed slug already exists (live or retired) keeps it, and only then+ * does the earliest remaining member (by instant, then discriminator)+ * take a free base slug. A page extracted before disambiguation keeps+ * its base slug.+ *+ * Ownership, operator protection and retirement stay with publish.ts: an+ * assigned slug is written only when the extractor owns it.+ */+import type { BrainEngine } from '../engine.ts';+import { computeContentHash } from '../ingestion/types.ts';+import type { BuiltChronicleEvent } from './publish.ts';++interface EventFields { when?: unknown; where?: unknown; kind?: unknown }++function normalizedWhen(when: unknown): string {+ const text = typeof when === 'string' ? when : when instanceof Date ? when.toISOString() : '';+ const at = Date.parse(text);+ return Number.isNaN(at) ? text : new Date(at).toISOString();+}++/** The fields that tell apart events sharing who, what, depth and day. */+function eventDiscriminator(event: EventFields | null | undefined): string {+ return JSON.stringify([normalizedWhen(event?.when), typeof event?.where === 'string' ? event.where : '', String(event?.kind ?? '')]);+}++function suffixedEventSlug(base: string, discriminator: string): string {+ return `${base}-${computeContentHash(discriminator).slice(0, 6)}`;+}++function discriminatorOf(ev: BuiltChronicleEvent): string {+ return eventDiscriminator(ev.frontmatter.event as EventFields);+}++/**+ * Assign final slugs to one generation. `existing` maps a slug already in the+ * brain (any state) to the discriminator of the event it holds.+ */+export function assignChronicleEventSlugs(events: BuiltChronicleEvent[], existing: Map<string, string>): BuiltChronicleEvent[] {+ const keys = events.map(discriminatorOf);+ const instants = keys.map((key) => Date.parse(JSON.parse(key)[0]) || 0);+ const order = events.map((_, i) => i).sort((a, b) => instants[a] - instants[b] || keys[a].localeCompare(keys[b]));+ const groups = new Map<string, number[]>();+ for (const i of order) groups.set(events[i].slug, [...groups.get(events[i].slug) ?? [], i]);++ const assigned: string[] = [];+ for (const [base, members] of groups) {+ const distinct = [...new Set(members.map((i) => keys[i]))];+ const slugOf = new Map<string, string>();+ const holder = distinct.find((key) => existing.get(base) === key);+ if (holder !== undefined) slugOf.set(holder, base);+ for (const key of distinct) {+ if (!slugOf.has(key) && existing.has(suffixedEventSlug(base, key))) slugOf.set(key, suffixedEventSlug(base, key));+ }+ for (const key of distinct) {+ if (slugOf.has(key)) continue;+ slugOf.set(key, [...slugOf.values()].includes(base) ? suffixedEventSlug(base, key) : base);+ }+ const copies = new Map<string, number>();+ for (const i of members) {+ const slug = slugOf.get(keys[i])!;+ const n = (copies.get(slug) ?? 0) + 1;+ copies.set(slug, n);+ assigned[i] = n === 1 ? slug : `${slug}-${n}`;+ }+ }+ return events.map((ev, i) => (assigned[i] === ev.slug ? ev : { ...ev, slug: assigned[i] }));+}++/** Read the pages at every candidate slug of the generation, then assign. */+export async function resolveChronicleEventSlugs(engine: BrainEngine, sourceId: string,+ events: BuiltChronicleEvent[]): Promise<BuiltChronicleEvent[]> {+ if (events.length === 0) return events;+ const candidates = new Set(events.flatMap((ev) => [ev.slug, suffixedEventSlug(ev.slug, discriminatorOf(ev))]));+ const rows = await engine.executeRaw<{ slug: string; when: string | null; where: string | null; kind: string | null }>(+ `SELECT slug, frontmatter->'event'->>'when' AS "when", frontmatter->'event'->>'where' AS "where", frontmatter->'event'->>'kind' AS kind+ FROM pages WHERE source_id=$1 AND slug = ANY($2::text[])`,+ [sourceId, [...candidates]]);+ return assignChronicleEventSlugs(events, new Map(rows.map((r) => [r.slug, eventDiscriminator(r)])));+}diff --git a/src/core/chronicle/publish.ts b/src/core/chronicle/publish.tsindex dcf7f8b778..4b5515bf5d 100644--- a/src/core/chronicle/publish.ts+++ b/src/core/chronicle/publish.ts@@ -40,6 +40,7 @@ import { maintenancePreflight, submitDatabaseMaintenanceIntent, type Maintenanceimport { preparePageMutation } from '../persistence/page-prepare.ts';import { effectiveVisibility, type Visibility } from '../search/private-visibility.ts';import type { ChronicleEventProposal } from './extract-events.ts';+import { resolveChronicleEventSlugs } from './event-identity.ts';export const CHRONICLE_RETIRED_BY = 'life-chronicle';export const CHRONICLE_EVENT_INTENT = 'managed_maintenance_chronicle_event';@@ -94,7 +95,7 @@ export interface BuiltChronicleEvent {summary: string;}-/** Event identity is content-addressed on (who, what, depth): a re-run upserts the same page. */+/** Event identity is content-addressed on (who, what, depth): a re-run upserts the same page. Same-day collisions are resolved at publication (event-identity.ts). */export function buildChronicleEvent(ev: ChronicleEventProposal, ctx: {depthSlug: string; attendees: string[]; effectiveDate: string | null; tz: string; visibility: Visibility; depthHash: string;isoDay: (when: string, tz: string) => string; normalizeKind: (kind: string) => string;@@ -173,8 +174,9 @@ export async function publishChronicleGeneration(engine: BrainEngine, opts: {const abort = () => { if (opts.signal?.aborted) { const e = new Error('aborted'); e.name = 'AbortError'; throw e; } };const broken = await depthPinBroken(engine, sourceId, pin, opts.decisionRequestId);if (broken) return { ...result, superseded: broken };+ const events = await resolveChronicleEventSlugs(engine, sourceId, opts.events);- for (const ev of opts.events) {+ for (const ev of events) {abort();const existing = await engine.readPageSnapshot(ev.slug, { sourceId, includeDeleted: true });const verdict = judgeTarget(existing, owned);@@ -210,7 +212,7 @@ export async function publishChronicleGeneration(engine: BrainEngine, opts: {}}- const keep = new Set(opts.events.map((e) => e.slug));+ const keep = new Set(events.map((e) => e.slug));const stale = await engine.executeRaw<{ slug: string; revision: string; content_hash: string }>(`SELECT slug, knowledge_revision::text AS revision, content_hash FROM pagesWHERE source_id=$1 AND type='event' AND deleted_at IS NULL AND frontmatter->'event'->>'depth'=$2diff --git a/test/chronicle-event-identity.test.ts b/test/chronicle-event-identity.test.tsnew file mode 100644index 0000000000..f6fbe5b01f--- /dev/null+++ b/test/chronicle-event-identity.test.ts@@ -0,0 +1,188 @@+/**+ * Life Chronicle same-day event identity (event-identity.ts).+ *+ * Protects: two distinct proposals that share who, what and day on one depth+ * page publish as two events; the slug set does not depend on proposal order;+ * a correction leaves the other events' slugs alone; a page extracted before+ * disambiguation keeps its event slug when an extractor-version bump+ * reprocesses it; the day is the brain timezone's day.+ * Fails when: a collision collapses two events into one page, reordering or+ * a correction moves an unrelated event to another slug, or reprocessing a+ * legacy page retires and recreates its existing event.+ * Existing coverage: chronicle-auto-phase pins reconciliation with distinct+ * summaries only; nothing exercised two proposals with one base slug.+ * Seams: injected judge (`runPhaseChronicle({ judge })`).+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import type { OperationContext } from '../src/core/ops/contract.ts';+import { operationsByName } from '../src/core/operations.ts';+import { runPhaseChronicle } from '../src/core/cycle/chronicle.ts';+import { runChronicleBackfill } from '../src/core/chronicle/backfill.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { buildChronicleEvents, type ChronicleEventProposal, type ChronicleJudge } from '../src/core/chronicle/extract-events.ts';+import { assignChronicleEventSlugs } from '../src/core/chronicle/event-identity.ts';+import { managedBrain, type ManagedBrain } from './helpers/managed-brain.ts';+import { requirePostgresTestDatabase } from './helpers/test-backends.ts';++const databaseUrl = process.env.GBRAIN_TEST_BACKEND === 'postgres' ? requirePostgresTestDatabase() : undefined;+const brain = (run: (b: ManagedBrain) => Promise<void>) => managedBrain(run, { databaseUrl });++const today = new Date().toISOString().slice(0, 10);+const BODY = 'Alice and Bob reviewed the launch plan and agreed on the next steps for the beta. '.repeat(3);+const meeting = (body = BODY) => `---\ntype: meeting\ntitle: Day notes\ndate: ${today}\n---\n\n${body}`;+const call = (when: string, extra: Partial<ChronicleEventProposal> = {}): ChronicleEventProposal =>+ ({ when, who: ['people/alice-example'], what: 'Call with alice-example', kind: 'call', ...extra });+const MORNING = call(`${today}T09:00:00Z`);+const AFTERNOON = call(`${today}T16:00:00Z`);++async function put(ctx: OperationContext, slug: string, content: string) {+ const current = await ctx.engine.readPageSnapshot(slug, { sourceId: 'default' });+ await operationsByName.put_page.handler(ctx, { slug, content, ...(current ? { expected_revision: current.revision } : {}) });+}+async function settle(engine: BrainEngine) {+ await engine.executeRaw("UPDATE chronicle_page_state SET next_attempt_at=now()-interval '1 second', decided_at=now()-interval '1 hour' WHERE state='pending'");+}+async function judged(engine: BrainEngine, events: ChronicleEventProposal[]) {+ await settle(engine);+ let calls = 0;+ const judge: ChronicleJudge = async () => { calls++; return { events }; };+ const r = await runPhaseChronicle(engine, { judge });+ return { calls, details: r.details };+}+async function eventPages(engine: BrainEngine) {+ return engine.executeRaw<{ id: number; slug: string; when: string; what: string; deleted: boolean }>(+ `SELECT id, slug, frontmatter->'event'->>'when' AS "when", frontmatter->'event'->>'what' AS what, deleted_at IS NOT NULL AS deleted+ FROM pages WHERE type='event' ORDER BY slug`);+}+const live = async (engine: BrainEngine) => (await eventPages(engine)).filter((e) => !e.deleted);+const at = (whenIso: string) => new Date(whenIso).toISOString();++function slugsFor(proposals: ChronicleEventProposal[], tz = 'UTC', existing = new Map<string, string>()) {+ const ctx = { input: {} as never, effectiveDate: today, attendees: [] };+ const built = buildChronicleEvents(proposals, ctx, { slug: 'meetings/day', visibility: 'world', contentHash: 'h' }, tz);+ return { legacy: built.map((e) => e.slug), assigned: assignChronicleEventSlugs(built, existing).map((e) => e.slug) };+}++describe('assignment (no database)', () => {+ test('collision count on a proposal fixture: legacy slugs vs disambiguated slugs', () => {+ const fixture: ChronicleEventProposal[] = [+ MORNING, AFTERNOON,+ call(`${today}T11:00:00Z`, { what: 'Standup with the beta team', who: ['people/bob-example'], kind: 'meeting' }),+ call(`${today}T15:00:00Z`, { what: 'Standup with the beta team', who: ['people/bob-example'], kind: 'meeting', where: 'Room 2' }),+ call(`${today}T15:00:00Z`, { what: 'Standup with the beta team', who: ['people/bob-example'], kind: 'meeting', where: 'Room 3' }),+ call(`${today}T12:00:00Z`, { what: 'Lunch with carol-example', who: ['people/carol-example'], kind: 'meal' }),+ ];+ const { legacy, assigned } = slugsFor(fixture);+ const collisions = (slugs: string[]) => slugs.length - new Set(slugs).size;+ expect({ proposals: fixture.length, before: collisions(legacy), after: collisions(assigned) }).toEqual({ proposals: 6, before: 3, after: 0 });+ // Every group keeps its legacy base slug for exactly one member.+ expect(new Set(assigned.filter((s) => legacy.includes(s))).size).toBe(new Set(legacy).size);+ });++ test('reordered proposals produce the same slug set, and the earliest keeps the base slug', () => {+ const forward = slugsFor([MORNING, AFTERNOON]);+ const reverse = slugsFor([AFTERNOON, MORNING]);+ expect(forward.assigned[0]).toBe(forward.legacy[0]);+ expect(forward.assigned[1]).toMatch(new RegExp(`^${forward.legacy[0]}-[0-9a-f]{6}$`));+ expect(reverse.assigned).toEqual([forward.assigned[1], forward.assigned[0]]);+ });++ test('identical copies keep their count with an ordinal suffix', () => {+ const { legacy, assigned } = slugsFor([MORNING, MORNING]);+ expect(assigned).toEqual([legacy[0], `${legacy[0]}-2`]);+ });++ test('timezone boundary: the day is the brain timezone\'s day', () => {+ const lateNight = call('2026-10-01T23:30:00-07:00');+ const nextMorning = call('2026-10-02T09:00:00-07:00');+ const pacific = slugsFor([lateNight, nextMorning], 'America/Los_Angeles');+ expect(pacific.assigned.map((s) => s.slice('life/events/'.length, 'life/events/'.length + 10))).toEqual(['2026-10-01', '2026-10-02']);+ expect(pacific.assigned).toEqual(pacific.legacy);+ // In UTC both instants fall on 2026-10-02 and share a base slug; they still publish as two events.+ const utc = slugsFor([lateNight, nextMorning], 'UTC');+ expect(new Set(utc.legacy).size).toBe(1);+ expect(new Set(utc.assigned).size).toBe(2);+ });+});++describe('publication through the chronicle phase', () => {+ test('two same-day calls publish as two events; reordering changes nothing; a correction moves only the corrected event', () => brain(async ({ engine, ctx }) => {+ await put(ctx, 'meetings/day', meeting());+ expect((await judged(engine, [MORNING, AFTERNOON])).details).toMatchObject({ events_written: 2 });+ const first = await live(engine);+ expect(first.map((e) => e.when).map(at).sort()).toEqual([at(MORNING.when), at(AFTERNOON.when)]);++ await put(ctx, 'meetings/day', meeting(`${BODY} Reordered.`));+ expect((await judged(engine, [AFTERNOON, MORNING])).details).toMatchObject({ events_written: 2, events_retired: 0 });+ expect(await live(engine)).toEqual(first);++ // The morning call's summary is corrected: it leaves the group, and the afternoon call keeps its slug.+ await put(ctx, 'meetings/day', meeting(`${BODY} Corrected.`));+ const corrected = { ...MORNING, what: 'Call with alice-example about the beta' };+ expect((await judged(engine, [corrected, AFTERNOON])).details).toMatchObject({ events_written: 2, events_retired: 1 });+ const after = await live(engine);+ const afternoon = first.find((e) => at(e.when) === at(AFTERNOON.when))!;+ expect(after.find((e) => e.what === AFTERNOON.what)).toEqual(afternoon);+ expect(after.map((e) => e.what).sort()).toEqual([AFTERNOON.what, corrected.what]);+ }), 120_000);++ test('a legacy page reprocessed after the extractor-version bump keeps its event slug', () => brain(async ({ engine, ctx }) => {+ // Before disambiguation, both calls landed on one base slug and the last proposal won it.+ await put(ctx, 'meetings/day', meeting());+ await judged(engine, [AFTERNOON]);+ const [legacy] = await live(engine);+ await engine.executeRaw('UPDATE chronicle_page_state SET extractor_version=1');++ // A managed brain leaves the re-decision to backfill; the run then judges the page once more.+ const queued = await runChronicleBackfill(engine, { yes: true });+ expect(queued).toMatchObject({ queued: 1, already_done: 0 });+ const run = await judged(engine, [MORNING, AFTERNOON]);+ expect(run.calls).toBe(1);+ expect(run.details).toMatchObject({ events_written: 2, events_retired: 0 });+ const events = await eventPages(engine);+ expect(events.every((e) => !e.deleted)).toBe(true);+ expect(events.find((e) => e.slug === legacy.slug)).toEqual(legacy);+ const separated = events.find((e) => e.slug !== legacy.slug)!;+ expect(separated.slug).toMatch(new RegExp(`^${legacy.slug}-[0-9a-f]{6}$`));+ expect(at(separated.when)).toBe(at(MORNING.when));+ }), 120_000);++ test('an operator-edited colliding event stays the operator\'s; its sibling still publishes', () => brain(async ({ engine, ctx }) => {+ await put(ctx, 'meetings/day', meeting());+ await judged(engine, [MORNING, AFTERNOON]);+ const before = await live(engine);+ const morning = before.find((e) => at(e.when) === at(MORNING.when))!;+ const page = (await engine.readPageSnapshot(morning.slug, { sourceId: 'default' }))!;+ const { serializePageToMarkdown } = await import('../src/core/markdown.ts');+ await operationsByName.put_page.handler(ctx, { slug: morning.slug, expected_revision: page.revision,+ content: serializePageToMarkdown({ ...page.page, compiled_truth: 'Call with alice-example (the operator confirmed it).' }, page.tags) });+ const edited = (await engine.readPageSnapshot(morning.slug, { sourceId: 'default' }))!;+ await put(ctx, 'meetings/day', meeting(`${BODY} Again.`));+ await judged(engine, [AFTERNOON, MORNING]);+ const after = (await engine.readPageSnapshot(morning.slug, { sourceId: 'default' }))!;+ expect(after.page.content_hash).toBe(edited.page.content_hash);+ expect((await live(engine)).map((e) => e.slug)).toEqual(before.map((e) => e.slug));+ }), 120_000);+});++describe.skipIf(databaseUrl !== undefined)('unmanaged brain', () => {+ let engine: PGLiteEngine;+ beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 120_000);+ afterAll(async () => { await engine.disconnect(); });++ test('an extractor-version bump re-queues an already-extracted page automatically', async () => {+ await engine.setConfig('chronicle.auto_settle_seconds', '0');+ await engine.setConfig('chronicle.activated_at', new Date(Date.now() - 60_000).toISOString());+ await engine.putPage('meetings/day', { type: 'meeting', title: 'Day notes', compiled_truth: BODY, frontmatter: { date: today } });+ expect((await judged(engine, [AFTERNOON])).calls).toBe(1);+ const [legacy] = await live(engine);+ expect((await judged(engine, [MORNING, AFTERNOON])).calls).toBe(0);+ await engine.executeRaw('UPDATE chronicle_page_state SET extractor_version=1');+ const run = await judged(engine, [MORNING, AFTERNOON]);+ expect(run.calls).toBe(1);+ const events = await live(engine);+ expect(events).toHaveLength(2);+ expect(events.find((e) => e.slug === legacy.slug)).toEqual(legacy);+ }, 120_000);+});diff --git a/test/e2e/chronicle-event-identity-postgres.test.ts b/test/e2e/chronicle-event-identity-postgres.test.tsnew file mode 100644index 0000000000..2a04a10f93--- /dev/null+++ b/test/e2e/chronicle-event-identity-postgres.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../chronicle-event-identity.test.ts'));