Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (decideReplacement binds its own-page parameter only when the clause uses it; memory_write's withdrawal preflight skips facts already admitted, so a key still replays its receipt)
garrytan022a89a5d61a
diff --git a/src/core/facts/single-prepare.ts b/src/core/facts/single-prepare.tsindex fdd6407c79..407686c4d6 100644--- a/src/core/facts/single-prepare.ts+++ b/src/core/facts/single-prepare.ts@@ -190,7 +190,8 @@ export async function decideReplacement(engine: BrainEngine, sourceId: string, iEXISTS (SELECT 1 FROM fact_withdrawals w WHERE w.source_id=f.source_id AND w.visibility=f.visibilityAND w.fact_hash IN (gbrain_fact_fingerprint(f.fact),gbrain_fact_fingerprint_v1(f.fact))AND ${withdrawalCoversFactSql('w', 'f')}) AS withdrawn- FROM facts f WHERE f.id=$1 AND f.source_id=$2${opts.excludePrivate ? ` AND (f.source_markdown_slug IS NOT DISTINCT FROM $3 OR ${privateProvenanceFilterFragment('f')})` : ''}${opts.lock ? ' FOR UPDATE OF f' : ''}`, [targetId, sourceId, ownPage]);+ FROM facts f WHERE f.id=$1 AND f.source_id=$2${!opts.excludePrivate ? '' : ownPage ? ` AND (f.source_markdown_slug = $3 OR ${privateProvenanceFilterFragment('f')})` : ` AND ${privateProvenanceFilterFragment('f')}`}${opts.lock ? ' FOR UPDATE OF f' : ''}`,+ opts.excludePrivate && ownPage ? [targetId, sourceId, ownPage] : [targetId, sourceId]);if (!target || (opts.remote && target.visibility !== 'world' && !(ownPage && target.source_markdown_slug === ownPage))) throw notFound();// #5913 [R2]: a slug-bound writer learns nothing about a fact outside its prefixes; the answer is the missing-id one.if (opts.slugPrefixes && [target.entity_slug, target.source_markdown_slug].some(slug => slug && !slugUnderBoundPrefixes(opts.slugPrefixes!, slug))) throw notFound();diff --git a/src/core/ops/qm-memory.ts b/src/core/ops/qm-memory.tsindex f277372ea7..2f9245b8ab 100644--- a/src/core/ops/qm-memory.ts+++ b/src/core/ops/qm-memory.ts@@ -239,8 +239,12 @@ const memory_write: Operation = {}}const { assertFactNotWithdrawn } = await import('../facts/single-prepare.ts');+ const { requestPrincipalForContext } = await import('../persistence/page-mutations.ts');+ const { getWriteRequest } = await import('../persistence/journal.ts');const excludePrivate = await resolveExcludePrivatePages(ctx.engine, ctx.remote);+ const principal = await requestPrincipalForContext(ctx);for (const [index, item] of parsed.entries()) {+ if (await getWriteRequest(ctx.engine, principal, items[index]!.request_id)) continue;const intent = { fact: item.fact as string, kind: (item.kind ?? 'fact') as never, visibility: 'private' as const, entity_slug: null, page_slug: memberPageSlug(actor.member) };try { await assertFactNotWithdrawn(ctx.engine, ctx.sourceId ?? 'default', intent, { excludePrivate }); } catch (e) {if (e instanceof OperationError) e.message = `facts[${index}]: ${e.message} Nothing was saved.`;diff --git a/test/qm-memory-tools.test.ts b/test/qm-memory-tools.test.tsindex 61556abdb5..8217949089 100644--- a/test/qm-memory-tools.test.ts+++ b/test/qm-memory-tools.test.ts@@ -437,6 +437,8 @@ for (const kind of testBackends()) {const b = await write(bob, [{ fact: claim, provenance: 'turn 11' }]);const [aliceId] = a.structured!.fact_ids;const [bobId] = b.structured!.fact_ids;+ const replayKey = randomUUID();+ const replayed = await write(alice, [{ fact: 'Alice moved the harbor sync canaryreplayforget', provenance: 'turn 11' }], replayKey);const [worldRow] = await factRows('canaryworld');const active = async (id: string) => (await engine.executeRaw<{ expired_at: unknown }>('SELECT expired_at FROM facts WHERE id = $1', [Number(id)]))[0]?.expired_at === null;for (const [op, id] of [['forget', bobId], ['forget_fact', bobId], ['forget', String(worldRow!.id)], ['forget_fact', String(worldRow!.id)]] as const) {@@ -448,6 +450,9 @@ for (const kind of testBackends()) {expect(await active(String(worldRow!.id))).toBe(true);const forgot = await call(alice, 'forget', { id: aliceId, reason: 'moved back' });expect(forgot.isError).toBe(false);+ expect((await call(alice, 'forget', { id: replayed.structured!.fact_ids[0] })).isError).toBe(false);+ const replay = await write(alice, [{ fact: 'Alice moved the harbor sync canaryreplayforget', provenance: 'turn 11' }], replayKey);+ expect({ isError: replay.isError, fact_ids: replay.structured?.fact_ids }).toEqual({ isError: false, fact_ids: replayed.structured!.fact_ids });expect(await active(aliceId!)).toBe(false);expect(await active(bobId!)).toBe(true);const [ledger] = await engine.executeRaw<{ subject: string }>('SELECT subject FROM fact_withdrawals WHERE source_id = $1 AND fact_hash = gbrain_fact_fingerprint($2)', [SRC, claim]);