Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

dream: a pattern page drops a seat its reflections no longer earn (#4618)

garrytana97e077f07e3
diff --git a/src/core/cycle/dream-provenance.ts b/src/core/cycle/dream-provenance.ts
index abfd51d763..411572b7bc 100644
--- a/src/core/cycle/dream-provenance.ts
+++ b/src/core/cycle/dream-provenance.ts
@@ -5,11 +5,13 @@ import { throwIfAborted } from '../abort-check.ts';
* Dream-provenance DB stamp (#2569): marks pages a dream child wrote with
* `dream_generated` and a stable first cycle date. Shared by synthesize and
* patterns (#5733); a page that existed before the child's first write to it
- * is not stamped. `seat` (#4618) credits the capturing agent seat.
+ * is not stamped. `seat` (#4618) credits the capturing agent seat; `seat: null`
+ * removes a seat the page no longer earns (a pattern whose reflections no
+ * longer share one).
*/
export async function stampDreamProvenance(
engine: BrainEngine,
- refs: Array<{ slug: string; source_id: string; raw_source?: string; seat?: string; raw_trace_exempt_reason?: string; first_write_at?: Date }>,
+ refs: Array<{ slug: string; source_id: string; raw_source?: string; seat?: string | null; raw_trace_exempt_reason?: string; first_write_at?: Date }>,
cycleDate: string,
signal?: AbortSignal,
): Promise<void> {
@@ -23,8 +25,8 @@ export async function stampDreamProvenance(
await executeRawJsonb(
engine,
`UPDATE pages
- SET frontmatter = COALESCE(frontmatter, '{}'::jsonb)
- || $5::jsonb
+ SET frontmatter = (COALESCE(frontmatter, '{}'::jsonb) - CASE WHEN $5::boolean THEN 'seat' ELSE '' END)
+ || $6::jsonb
|| jsonb_build_object(
'dream_cycle_date',
COALESCE(NULLIF(frontmatter->>'dream_created_cycle_date', ''), NULLIF(frontmatter->>'dream_cycle_date', ''), $3),
@@ -38,7 +40,7 @@ export async function stampDreamProvenance(
// C-8: a page that existed before the child's first write to it is
// not dream output; stamping it would hide it from extract_facts
// and transcript discovery forever.
- [slug, source_id, cycleDate, first_write_at?.toISOString() ?? null],
+ [slug, source_id, cycleDate, first_write_at?.toISOString() ?? null, seat === null],
// #1978 raw-source persistence: record the transcript path the
// synthesis was derived from, so `gbrain doctor` (raw_provenance
// check) can verify every generated page carries a raw trace.
diff --git a/src/core/cycle/patterns.ts b/src/core/cycle/patterns.ts
index cc5ed84bcf..6d73614f17 100644
--- a/src/core/cycle/patterns.ts
+++ b/src/core/cycle/patterns.ts
@@ -636,10 +636,11 @@ When done, briefly list the pattern slugs you wrote/updated in your final messag
async function stampPatternOutputs(engine: BrainEngine, maintenance: MaintenanceAuthority | null,
refs: Array<{ slug: string; source_id: string }>, cycleDate: string, sourceSlugPrefix: string, seat: string | undefined, signal?: AbortSignal): Promise<void> {
const reason = `derived from reflections under ${sourceSlugPrefix}/; raw traces live on the cited reflection pages`;
- if (!maintenance) return stampDreamProvenance(engine, refs.map(ref => ({ ...ref, raw_trace_exempt_reason: reason, ...(seat ? { seat } : {}) })), cycleDate, signal);
+ // A pattern earns a seat only while its reflections share one, so a pattern without one drops a seat an earlier run stamped.
+ if (!maintenance) return stampDreamProvenance(engine, refs.map(ref => ({ ...ref, raw_trace_exempt_reason: reason, seat: seat ?? null })), cycleDate, signal);
for (const ref of refs) {
throwIfAborted(signal, '[dream] patterns provenance');
- await stampMaintenancePage(engine, maintenance, ref.slug, cycleDate, undefined, reason, seat);
+ await stampMaintenancePage(engine, maintenance, ref.slug, cycleDate, undefined, reason, seat ?? null);
}
}
diff --git a/src/core/persistence/prepared-maintenance.ts b/src/core/persistence/prepared-maintenance.ts
index a9ecc5327f..0364607399 100644
--- a/src/core/persistence/prepared-maintenance.ts
+++ b/src/core/persistence/prepared-maintenance.ts
@@ -165,13 +165,14 @@ export async function submitDatabaseMaintenanceIntent(engine: BrainEngine, autho
}
export async function stampMaintenancePage(engine: BrainEngine, authority: MaintenanceAuthority, slug: string,
- cycleDate: string, rawSource?: string, rawTraceExemptReason?: string, seat?: string): Promise<void> {
+ cycleDate: string, rawSource?: string, rawTraceExemptReason?: string, seat?: string | null): Promise<void> {
const snapshot = await engine.readPageSnapshot(slug, { sourceId: authority.writer.sourceId });
if (!snapshot) throw opError('page_not_found', 'A maintenance output page disappeared.',
`Output page ${slug} in '${authority.writer.sourceId}' no longer exists, so it was not stamped. Confirm with the command in fix, then run maintenance again to regenerate it if it is still wanted.`,
{ fix: readFix('Shows whether the page exists in this source now.', { argv: ['gbrain', 'get', '--source', authority.writer.sourceId, '--', slug] }) });
const firstDate = snapshot.page.frontmatter.dream_created_cycle_date || snapshot.page.frontmatter.dream_cycle_date || cycleDate;
- const page = { ...snapshot.page, frontmatter: { ...snapshot.page.frontmatter, dream_generated: true,
+ const { seat: _staleSeat, ...kept } = snapshot.page.frontmatter;
+ const page = { ...snapshot.page, frontmatter: { ...(seat === null ? kept : snapshot.page.frontmatter), dream_generated: true,
dream_cycle_date: firstDate, dream_created_cycle_date: firstDate, ...(rawSource ? { raw_source: rawSource } : {}),
...(rawTraceExemptReason ? { raw_trace_exempt: true, raw_trace_exempt_reason: rawTraceExemptReason } : {}),
...(seat ? { seat } : {}) } };
diff --git a/test/session-seat-synthesize.test.ts b/test/session-seat-synthesize.test.ts
index 853ef917d6..1af82a8cbc 100644
--- a/test/session-seat-synthesize.test.ts
+++ b/test/session-seat-synthesize.test.ts
@@ -161,8 +161,11 @@ for (const managed of [true, false]) describe(`${managed ? 'managed' : 'unmanage
});
}, 120_000);
- test('patterns credit a seat only when every input reflection shares it', async () => {
- for (const seats of [['alice-desk', 'alice-desk', 'alice-desk'], ['alice-desk', 'bob-desk', 'alice-desk']]) {
+ // A model updating an existing pattern page copies its frontmatter, seat included; once the reflections
+ // that earned the seat age out of the window, the rewrite must not keep crediting it.
+ test('patterns credit a seat only when every input reflection shares it, and drop one they no longer earn', async () => {
+ for (const [seats, carried] of [[['alice-desk', 'alice-desk', 'alice-desk'], ''], [['alice-desk', 'bob-desk', 'alice-desk'], ''],
+ [['alice-desk', 'bob-desk', 'alice-desk'], 'seat: alice-desk\n']] as const) {
await fixture(managed, async ({ engine, sourceId, root }) => {
await engine.setConfig('agent.use_gateway_loop', 'true');
const ctx = pageCtx(engine, sourceId);
@@ -175,7 +178,7 @@ for (const managed of [true, false]) describe(`${managed ? 'managed' : 'unmanage
calls++;
const write = calls === 1;
return { text: write ? '' : 'Saved the pattern.', blocks: write ? [{ type: 'tool-call', toolCallId: 'pattern-write', toolName: 'brain_put_page', input: {
- slug: 'wiki/personal/patterns/durability', content: '---\ntitle: Durability pattern\ntype: note\n---\nA recurring theme in [[wiki/personal/reflections/example-0]].',
+ slug: 'wiki/personal/patterns/durability', content: `---\ntitle: Durability pattern\ntype: note\n${carried}---\nA recurring theme in [[wiki/personal/reflections/example-0]].`,
} }] : [{ type: 'text', text: 'Saved the pattern.' }], stopReason: write ? 'tool_calls' : 'end', usage, model: opts.model!, providerId: 'anthropic' };
});
const result = await runPhasePatterns(engine, { brainDir: root, sourceId, dryRun: false, once: true, cycleDate: '2026-09-20' });