Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes (a member write's supersession candidates are its own page's, in SQL; a member replaces its own member-page fact by id)
garrytan6d00cedcdccc
diff --git a/src/core/engine-sql/facts.ts b/src/core/engine-sql/facts.tsindex 182eaedf99..64b3eb4704 100644--- a/src/core/engine-sql/facts.ts+++ b/src/core/engine-sql/facts.ts@@ -582,13 +582,14 @@ export async function findCandidateDuplicates(source_id: string,entitySlug: string,factText: string,- opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean },+ opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string },): Promise<FactRow[]> {const k = Math.min(Math.max(opts?.k ?? 5, 1), 20);const speaker = opts?.attributedTo? sqlFragment`AND (attributed_to IS NULL OR attributed_to = ${opts.attributedTo})`: sqlFragment``;- const members = opts?.excludeMemberPages ? sqlFragment` AND NOT ${trustedSql(onMemberPageSql('facts'))}` : sqlFragment``;+ const members = opts?.onlyPage ? sqlFragment` AND source_markdown_slug = ${opts.onlyPage}`+ : opts?.excludeMemberPages ? sqlFragment` AND NOT ${trustedSql(onMemberPageSql('facts'))}` : sqlFragment``;// Validity-lapsed rows are not dedup candidates: a re-stated fact after// its valid_until lapses re-inserts fresh (WP5 read-time TTL honesty).if (opts?.arm === 'keyword') return findKeywordCandidates(exec, source_id, entitySlug, factText, k, opts);@@ -636,7 +637,7 @@ export async function findCandidateDuplicates(*/async function findKeywordCandidates(exec: LegacyUnscopedRead, source_id: string, entitySlug: string, factText: string, k: number,- opts: { embedding?: Float32Array; embeddingModel?: string | null; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean },+ opts: { embedding?: Float32Array; embeddingModel?: string | null; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string },): Promise<FactRow[]> {if (opts.embedding && !opts.embeddingModel) return [];const speaker = opts.attributedTo@@ -656,7 +657,7 @@ async function findKeywordCandidates(AND (f.valid_until IS NULL OR f.valid_until > now())AND f.source != ALL(${AUDIT_ROW_SOURCES}::text[])${comparable}- ${speaker}${opts.excludeMemberPages ? sqlFragment` AND NOT ${trustedSql(onMemberPageSql('f'))}` : sqlFragment``}+ ${speaker}${opts.onlyPage ? sqlFragment` AND f.source_markdown_slug = ${opts.onlyPage}` : opts.excludeMemberPages ? sqlFragment` AND NOT ${trustedSql(onMemberPageSql('f'))}` : sqlFragment``}AND q.q IS NOT NULL AND to_tsvector(${lang}::regconfig, f.fact) @@ q.qORDER BY ts_rank_cd(to_tsvector(${lang}::regconfig, f.fact), q.q) DESC, f.created_at DESC, f.id DESCLIMIT ${k}diff --git a/src/core/engine.ts b/src/core/engine.tsindex 91d0eee968..3f25d4781e 100644--- a/src/core/engine.ts+++ b/src/core/engine.ts@@ -2370,7 +2370,7 @@ export interface BrainEngine {source_id: string,entitySlug: string,factText: string,- opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean },+ opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string },): Promise<FactRow[]>;/**diff --git a/src/core/facts/single-prepare.ts b/src/core/facts/single-prepare.tsindex bd0f3316be..fdd6407c79 100644--- a/src/core/facts/single-prepare.ts+++ b/src/core/facts/single-prepare.ts@@ -108,11 +108,11 @@ export async function readCandidateFusion(engine: BrainEngine): Promise<Candidat* under `interleave` the cosine and keyword arms merged round-robin by fact id* and cut to the same k, so the decision still scores at most k rows.*/-export async function listSupersessionCandidates(engine: BrainEngine, sourceId: string, entitySlug: string, fact: string, embedding: Float32Array, embeddingModel?: string | null, fusion?: CandidateFusion, attributedTo?: FactAttribution | null, excludeMemberPages = false): Promise<FactRow[]> {+export async function listSupersessionCandidates(engine: BrainEngine, sourceId: string, entitySlug: string, fact: string, embedding: Float32Array, embeddingModel?: string | null, fusion?: CandidateFusion, attributedTo?: FactAttribution | null, scope: { excludeMemberPages?: boolean; onlyPage?: string } = {}): Promise<FactRow[]> {const k = SUPERSESSION_CANDIDATE_K;- const cosine = await engine.findCandidateDuplicates(sourceId, entitySlug, fact, { embedding, embeddingModel, k, attributedTo, excludeMemberPages });+ const cosine = await engine.findCandidateDuplicates(sourceId, entitySlug, fact, { embedding, embeddingModel, k, attributedTo, ...scope });if ((fusion ?? await readCandidateFusion(engine)) !== 'interleave') return cosine;- const keyword = await engine.findCandidateDuplicates(sourceId, entitySlug, fact, { embedding, embeddingModel, k, arm: 'keyword', attributedTo, excludeMemberPages });+ const keyword = await engine.findCandidateDuplicates(sourceId, entitySlug, fact, { embedding, embeddingModel, k, arm: 'keyword', attributedTo, ...scope });return interleaveFusion([cosine, keyword], row => row.id, k);}@@ -129,7 +129,8 @@ export async function decideSingleFact(engine: BrainEngine, sourceId: string, inORDER BY id LIMIT 1`, [sourceId, input.entity_slug, input.visibility, input.fact, input.attributed_to ?? null, input.page_slug ?? null]);if (exact) return { status: 'duplicate', candidate: { ...exact, id: Number(exact.id) } };if (embedding && input.entity_slug) {- const candidates = await listSupersessionCandidates(engine, sourceId, input.entity_slug, input.fact, embedding, embeddingModel, undefined, input.attributed_to ?? null, !input.page_slug);+ const candidates = await listSupersessionCandidates(engine, sourceId, input.entity_slug, input.fact, embedding, embeddingModel, undefined, input.attributed_to ?? null,+ input.page_slug ? { onlyPage: input.page_slug } : { excludeMemberPages: true });const metadata = await engine.executeRaw<{ id: number; source_markdown_slug: string | null; row_num: number | null; trust_tier: FactRow['trust_tier'] }>(`SELECT id,source_markdown_slug,row_num,trust_tier FROM facts WHERE source_id=$1 AND id=ANY($2::int[])${opts.excludePrivate && !input.page_slug ? ` AND ${privateProvenanceFilterFragment('facts')}` : ''}`,[sourceId, candidates.map(c => c.id)]);@@ -181,14 +182,16 @@ export async function replacementVisibility(engine: BrainEngine, sourceId: strin*/export async function decideReplacement(engine: BrainEngine, sourceId: string, input: SingleFactIntent, targetId: number,opts: { pageSlug: string; remote: boolean; lock?: boolean; excludePrivate?: boolean; slugPrefixes?: string[] | null }): Promise<FactDecision> {+ // GST-359: a member's write may replace a fact on its own member page (the page this write goes to), private or not.+ const ownPage = input.page_slug && input.page_slug === opts.pageSlug ? input.page_slug : null;const notFound = () => verbError('not_found', `No fact with id "${targetId}" to replace.`,'Pass the fact_id of an active fact from recall (facts[].fact_id) about the same entity, or omit replaces to save a new fact.');const [target] = await engine.executeRaw<FactCandidate & { withdrawn: boolean }>(`SELECT f.*,EXISTS (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 ${privateProvenanceFilterFragment('f')}` : ''}${opts.lock ? ' FOR UPDATE OF f' : ''}`, [targetId, sourceId]);- if (!target || (opts.remote && target.visibility !== 'world')) throw notFound();+ 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]);+ 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();if (target.visibility !== input.visibility) {diff --git a/src/core/pglite-engine.ts b/src/core/pglite-engine.tsindex 15b2da06f1..2512a73d75 100644--- a/src/core/pglite-engine.ts+++ b/src/core/pglite-engine.ts@@ -2502,7 +2502,7 @@ export class PGLiteEngine implements BrainEngine {source_id: string,entitySlug: string,factText: string,- opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: import('./engine.ts').FactAttribution | null; excludeMemberPages?: boolean },+ opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: import('./engine.ts').FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string },): Promise<FactRow[]> {return factsImpl.findCandidateDuplicates(unscopedExecutor(this.engineSql, 'facts: unscoped on master (EO4 inventory)'), source_id, entitySlug, factText, opts);}diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.tsindex 15c8fb0eea..1fbb005565 100644--- a/src/core/postgres-engine.ts+++ b/src/core/postgres-engine.ts@@ -2338,7 +2338,7 @@ export class PostgresEngine implements BrainEngine {source_id: string,entitySlug: string,factText: string,- opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: import('./engine.ts').FactAttribution | null; excludeMemberPages?: boolean },+ opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: 'keyword'; attributedTo?: import('./engine.ts').FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string },): Promise<FactRow[]> {return factsImpl.findCandidateDuplicates(unscopedExecutor(this.engineSql, 'facts: unscoped on master (EO4 inventory)'), source_id, entitySlug, factText, opts);}diff --git a/test/access-member-facts-dedup.test.ts b/test/access-member-facts-dedup.test.tsindex 48dfa7a5e8..3a06e6cbd9 100644--- a/test/access-member-facts-dedup.test.ts+++ b/test/access-member-facts-dedup.test.ts@@ -132,3 +132,56 @@ describe('member-page facts never crowd the dedup candidate cap (pglite, repo-le}}, 180_000);});++const V_OLD = (() => { const v = new Array<number>(DIMS).fill(0); v[7] = 1; v[9] = 0.25; return v; })();++describe('a member updates its own facts whatever other members saved (pglite, repo-less)', () => {+ let engine: BrainEngine;+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });+ const tokens: Record<string, string> = {};+ const remember = async (who: string, params: Record<string, unknown>) => {+ const out = await dispatchToolCall(engine, 'remember', { provenance: 'dm', ...params }, { remote: true, transport: 'http', auth: await provider().verifyAccessToken(tokens[who]!) as unknown as AuthInfo, sourceId: SRC, takesHoldersAllowList: ['world'] });+ const text = out.content.map(c => c.text).join('\n');+ let json: Record<string, any> = {};+ try { json = JSON.parse(text); } catch { /* prose */ }+ return { isError: out.isError === true, text, json };+ };+ const active = async (needle: string) => (await engine.executeRaw<{ id: number | string }>('SELECT id FROM facts WHERE source_id = $1 AND fact LIKE $2 AND expired_at IS NULL', [SRC, '%' + needle + '%'])).map(r => String(r.id));++ beforeAll(async () => {+ configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: DIMS, env: { OPENAI_API_KEY: 'test' } } as never);+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => ({ embeddings: values.map(t => t.includes('canaryoldbob') ? V_OLD : VEC) })) as never);+ engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema();+ __resetPrivateVisibilityCacheForTests();+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);+ for (const id of [...CROWD, 'bob-example']) await addMember(engine, id);+ await engine.setConfig('facts.supersession_thresholds', JSON.stringify({ 'openai:text-embedding-3-small@1536': 0.95 }));+ await importFromContent(engine, ACME, '---\ntype: company\ntitle: Acme Example\n---\n# Acme Example\n\nAcme example builds harbor ledgers.', { sourceId: SRC });+ for (const who of [...CROWD, 'bob-example']) {+ const c = await provider().registerClientManual('own-' + who + '-' + randomUUID().slice(0, 8), ['client_credentials'], 'read write', [], SRC);+ await bindCredential(engine, { kind: 'client', id: c.clientId }, { member: who });+ tokens[who] = (await provider().exchangeClientCredentials(c.clientId, c.clientSecret ?? '')).access_token;+ }+ }, 180_000);++ afterAll(async () => { __setEmbedTransportForTests(null); resetGateway(); await engine.disconnect(); });++ test('five closer facts on other members\' pages never hide the caller\'s own earlier fact from supersession', async () => {+ expect((await remember('bob-example', { fact: 'Acme Example plans a harbor sale canaryoldbob', entity: ACME })).isError).toBe(false);+ for (const member of CROWD) expect((await remember(member, { fact: 'Acme Example plans a harbor sale soon ' + member, entity: ACME })).isError).toBe(false);+ const out = await remember('bob-example', { fact: 'Acme Example plans a harbor sale this quarter canarynewbob', entity: ACME });+ expect(out.isError).toBe(false);+ expect(await active('canaryoldbob')).toEqual([]);+ expect(await active('canarynewbob')).toHaveLength(1);+ }, 120_000);++ test('a member replaces its own fact by id', async () => {+ const saved = await remember('bob-example', { fact: 'Acme Example ships ledgers on Fridays canaryreplaceold', entity: ACME });+ expect(saved.isError).toBe(false);+ const [old] = await active('canaryreplaceold');+ const out = await remember('bob-example', { fact: 'Acme Example ships ledgers on Thursdays canaryreplacenew', entity: ACME, replaces: old });+ expect({ isError: out.isError, text: out.isError ? out.text : '' }).toEqual({ isError: false, text: '' });+ expect(await active('canaryreplaceold')).toEqual([]);+ expect(await active('canaryreplacenew')).toHaveLength(1);+ }, 120_000);+});diff --git a/test/fixtures/goldens/exports/types.json b/test/fixtures/goldens/exports/types.jsonindex 3dc4a95555..82c561c34c 100644--- a/test/fixtures/goldens/exports/types.json+++ b/test/fixtures/goldens/exports/types.json@@ -88,7 +88,7 @@"expireFact: (id: number, opts?: { supersededBy?: number; at?: Date; }) => Promise<boolean>","findAnomalies: (opts: AnomaliesOpts) => Promise<AnomalyResult[]>","findByTitleFuzzy: (name: string, dirPrefix?: string, minSimilarity?: number, sourceId?: string) => Promise<{ slug: string; similarity: number; } | null>",- "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; }) => Promise<FactRow[]>",+ "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string; }) => Promise<FactRow[]>","findDuplicatePage?: ((sourceId: string, opts: { hash: string; frontmatterId?: string | null; excludeSlug?: string; }) => Promise<{ slug: string; id: number; } | null>) | undefined","findDuplicatePages?: ((sourceId: string, inputs: ReadonlyArray<{ hash: string; frontmatterId?: string | null; excludeSlug?: string; }>) => Promise<Array<{ slug: string; id: number; } | null>>) | undefined","findOntologyConflicts: (opts?: PageReadScope & { minConfidence?: number; visibility?: OntologyReadOpts[\"visibility\"]; }) => Promise<OntologyConflict[]>",@@ -1098,7 +1098,7 @@"explainVectorSearch: (embedding: Float32Array, opts?: SearchOpts) => Promise<Record<string, unknown>>","findAnomalies: (opts: AnomaliesOpts) => Promise<AnomalyResult[]>","findByTitleFuzzy: (name: string, dirPrefix?: string, minSimilarity?: number, sourceId?: string) => Promise<{ slug: string; similarity: number; } | null>",- "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; }) => Promise<FactRow[]>",+ "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string; }) => Promise<FactRow[]>","findDuplicatePage: (sourceId: string, opts: { hash: string; frontmatterId?: string | null; excludeSlug?: string; }) => Promise<{ slug: string; id: number; } | null>","findDuplicatePages: (sourceId: string, inputs: Parameters<typeof findDuplicatePages>[2]) => Promise<({ slug: string; id: number; } | null)[]>","findOntologyConflicts: (opts?: PageReadScope & { minConfidence?: number; visibility?: OntologyReadOpts[\"visibility\"]; }) => Promise<OntologyConflict[]>",@@ -3643,7 +3643,7 @@"expireFact: (id: number, opts?: { supersededBy?: number; at?: Date; }) => Promise<boolean>","findAnomalies: (opts: AnomaliesOpts) => Promise<AnomalyResult[]>","findByTitleFuzzy: (name: string, dirPrefix?: string, minSimilarity?: number, sourceId?: string) => Promise<{ slug: string; similarity: number; } | null>",- "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; }) => Promise<FactRow[]>",+ "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string; }) => Promise<FactRow[]>","findDuplicatePage?: ((sourceId: string, opts: { hash: string; frontmatterId?: string | null; excludeSlug?: string; }) => Promise<{ slug: string; id: number; } | null>) | undefined","findDuplicatePages?: ((sourceId: string, inputs: ReadonlyArray<{ hash: string; frontmatterId?: string | null; excludeSlug?: string; }>) => Promise<Array<{ slug: string; id: number; } | null>>) | undefined","findOntologyConflicts: (opts?: PageReadScope & { minConfidence?: number; visibility?: OntologyReadOpts[\"visibility\"]; }) => Promise<OntologyConflict[]>",@@ -6956,7 +6956,7 @@"expireFact: (id: number, opts?: { supersededBy?: number; at?: Date; }) => Promise<boolean>","findAnomalies: (opts: AnomaliesOpts) => Promise<AnomalyResult[]>","findByTitleFuzzy: (name: string, dirPrefix?: string, minSimilarity?: number, sourceId?: string) => Promise<{ slug: string; similarity: number; } | null>",- "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; }) => Promise<FactRow[]>",+ "findCandidateDuplicates: (source_id: string, entitySlug: string, factText: string, opts?: { k?: number; embedding?: Float32Array; embeddingModel?: string | null; arm?: \"keyword\"; attributedTo?: FactAttribution | null; excludeMemberPages?: boolean; onlyPage?: string; }) => Promise<FactRow[]>","findDuplicatePage: (sourceId: string, opts: { hash: string; frontmatterId?: string | null; excludeSlug?: string; }) => Promise<{ slug: string; id: number; } | null>","findDuplicatePages: (sourceId: string, inputs: Parameters<typeof pagesImpl.findDuplicatePages>[2]) => Promise<({ slug: string; id: number; } | null)[]>","findOntologyConflicts: (opts?: PageReadScope & { minConfidence?: number; visibility?: OntologyReadOpts[\"visibility\"]; }) => Promise<OntologyConflict[]>",