Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(hot-memory): read floor, activation partition, trust labels and generation revalidation (#5575 ENG-11, CEO-20, DX-10)
garrytan3fea1a9de998
diff --git a/src/core/context/turn-context.ts b/src/core/context/turn-context.tsindex c18843a2d7..f2d50a6cb0 100644--- a/src/core/context/turn-context.ts+++ b/src/core/context/turn-context.ts@@ -87,6 +87,10 @@ export interface TurnContextFact {/** Who asserted the claim; rendered so an assistant suggestion never reads as the user's own claim. */attributed_to?: 'user' | 'assistant' | 'other' | null;confidence: number;+ /** #5575: the fact's stored trust tier (eligibility/labels.ts trustFields); absent from older serves. */+ trust_tier?: import('../trust/tier.ts').TrustTier;+ /** #5575: the short, server-stamped write origin (e.g. `mcp:remember`, `legacy`). */+ origin?: string;}/** One page in a `delta` result (updated after the cursor). */@@ -105,6 +109,13 @@ export interface TurnContextResult {* requests; absent from older serves and from every other trigger.*/core?: { text: string; revision: string; chars_used: number; chars_limit: number; truncated: boolean };+ /**+ * #5575 ENG-11, coreOnly requests: the core block's trust cache identity+ * (eligibility/generation.ts). `unchanged` answers a request whose+ * `coreIdentity` still matches, without `core`. Absent when the serve+ * cannot read it (the client then does not memoize the block).+ */+ coreTrust?: { identity: string; unchanged: boolean };/** Context-pressure gate (pressure.ts), serve-sourced; an older serve omits it and no notice fires. */pressure?: import('./pressure.ts').PressureGate;/** Reflex pointers that survived suppression + budget. */diff --git a/src/core/facts/meta-hook.ts b/src/core/facts/meta-hook.tsindex 903f62bd13..3c9d3f865b 100644--- a/src/core/facts/meta-hook.ts+++ b/src/core/facts/meta-hook.ts@@ -12,6 +12,18 @@* dispatch on every MCP transport), and every hit revalidates the* source's withdrawal-ledger watermark, so a forget committed by any* process is never served from the cache.+ * - #5575 ENG-11: the key also folds the effective read eligibility (floor+ * and activation control), and every hit revalidates the brain's trust+ * policy generation (eligibility/generation.ts), read before the rows.+ * A tier change, quarantine transition, floor or `trust.%` config change+ * or purge in any process makes the next call rebuild. When the+ * generation cannot be read, or the rebuild fails, nothing is delivered;+ * a cached payload is never served in its place.+ * - Facts are read through the 'hot_memory' proactive eligibility (floor,+ * quarantined pages, needs_rederive in SQL; activation control by+ * eligibility/activation.ts after ranking), and each carries its+ * `trust_tier` and `origin`. The payload's `suppressed` field counts what+ * activation control withheld, never its content (DX-10).* - Cap at top-K facts per response so the injection stays lean.** Both stdio and HTTP MCP transports pass this hook into dispatchToolCall@@ -22,6 +34,10 @@ import type { OperationContext } from './../operations.ts';import type { FactRow } from './../engine.ts';import { effectiveConfidence } from './decay.ts';import { collapseHotFacts } from './capture-dedup.ts';+import { proactiveEligibility } from '../eligibility/registry.ts';+import { partitionForActivation, suppressionSummary } from '../eligibility/activation.ts';+import { trustFields } from '../eligibility/labels.ts';+import { eligibilityCacheField, readTrustGeneration } from '../eligibility/generation.ts';const DEFAULT_TTL_MS = 30_000;const DEFAULT_TOP_K = 10;@@ -40,6 +56,8 @@ interface CacheEntry {expiresAt: number;payload: Record<string, unknown> | undefined;withdrawals?: string;+ /** The trust policy generation the payload was built under (ENG-11). */+ trustGeneration?: string;}const _cache = new Map<string, CacheEntry>();@@ -126,20 +144,31 @@ export async function getBrainHotMemoryMeta(// the same source id / tier / session must never share hot-memory payloads// — a cached entry from brain A served to brain B's caller is a cross-brain// fact leak through the cache, not through any query.- const cacheKey = `${engineCacheField(ctx.engine)}::${encodeCacheField(sourceId)}::${tier}::${encodeCacheField(sessionId ?? '_')}::${allowListHash}`;-const ttl = Math.max(1000, opts.ttlMs ?? DEFAULT_TTL_MS);const topK = Math.max(1, Math.min(opts.topK ?? DEFAULT_TOP_K, 25));- // The watermark is read before the rows, so a build that raced a forget- // stores the older watermark and the next hit rebuilds.+ // The watermark and the trust generation are read before the rows, so a+ // build that raced a forget or a trust transition stores the older value+ // and the next hit rebuilds. The eligibility is read after the generation:+ // a `trust.%` config change bumps it.const generation = _generations.get(ctx.engine) ?? 0;const withdrawals = await withdrawalWatermark(ctx.engine, sourceId);+ let trustGeneration: string;+ let policy: Awaited<ReturnType<typeof proactiveEligibility>>;+ try {+ trustGeneration = await readTrustGeneration(ctx.engine);+ policy = await proactiveEligibility(ctx, 'hot_memory');+ } catch {+ return undefined;+ }+ // The effective eligibility is part of the key: two connections with+ // different floors never share an entry.+ const cacheKey = `${engineCacheField(ctx.engine)}::${encodeCacheField(sourceId)}::${tier}::${encodeCacheField(sessionId ?? '_')}::${allowListHash}::${encodeCacheField(eligibilityCacheField(policy))}`;// Cache hit?const cached = _cache.get(cacheKey);if (cached) {- if (cached.expiresAt > Date.now() && cached.withdrawals === withdrawals) return cached.payload;+ if (cached.expiresAt > Date.now() && cached.withdrawals === withdrawals && cached.trustGeneration === trustGeneration) return cached.payload;// Expired or withdrawn since: evict BEFORE the rebuild. If the rebuild// below throws (the caller's try/catch absorbs it), the dead entry must// not linger — with remote-influencable keys, lingering corpses defeat@@ -147,24 +176,28 @@ export async function getBrainHotMemoryMeta(_cache.delete(cacheKey);}const store = (entry: CacheEntry) => {- if ((_generations.get(ctx.engine) ?? 0) === generation) cacheSet(cacheKey, { ...entry, withdrawals });+ if ((_generations.get(ctx.engine) ?? 0) === generation) cacheSet(cacheKey, { ...entry, withdrawals, trustGeneration });};// Build a fresh payload. Visibility tier: remote → world-only;// local → all rows.const visibility = ctx.remote === false ? undefined : ['world'] as ('world' | 'private')[];+ // Floor, quarantined pages and needs_rederive apply in SQL; activation+ // control runs after ranking so the withheld count can be reported.+ const eligibility = { ...policy, suppressFlagged: false };+// #5888 V2: over-fetch so collapsing duplicates still fills topK.let fetched: FactRow[] = [];if (sessionId) {fetched = await ctx.engine.listFactsBySession(sourceId, sessionId, {- activeOnly: true, limit: topK * 3, visibility, fingerprint: true,+ activeOnly: true, limit: topK * 3, visibility, fingerprint: true, eligibility,});}// If no session-scoped rows, fall back to recent across the source.if (fetched.length === 0) {fetched = await ctx.engine.listFactsSince(sourceId, new Date(Date.now() - 24 * 60 * 60 * 1000), {- activeOnly: true, limit: topK * 3, visibility, fingerprint: true,+ activeOnly: true, limit: topK * 3, visibility, fingerprint: true, eligibility,});}if (fetched.length === 0) {@@ -174,15 +207,22 @@ export async function getBrainHotMemoryMeta(// One representative per (fingerprint, entity) group, then sort by// effective confidence (decayed) before truncating.- let rows = await collapseHotFacts(ctx.engine, sourceId, fetched);+ const collapsed = await collapseHotFacts(ctx.engine, sourceId, fetched);const now = new Date();- rows.sort((a, b) => effectiveConfidence(b, now) - effectiveConfidence(a, now));- rows = rows.slice(0, topK);+ collapsed.sort((a, b) => effectiveConfidence(b, now) - effectiveConfidence(a, now));+ const { kept, withheld } = await partitionForActivation(ctx.engine, collapsed, r => ({ table: 'facts', id: r.id }), policy);+ const rows = kept.slice(0, topK).map(k => k.item);+ const suppressed = suppressionSummary(withheld);+ if (rows.length === 0 && !suppressed) {+ store({ expiresAt: Date.now() + ttl, payload: undefined });+ return undefined;+ }const payload = {brain_hot_memory: {source_id: sourceId,session_id: sessionId,+ ...(suppressed ? { suppressed } : {}),facts: rows.map(r => ({id: r.id,fact: r.fact,@@ -202,6 +242,7 @@ export async function getBrainHotMemoryMeta(context: r.context ?? null,confidence: Number(effectiveConfidence(r, now).toFixed(3)),...(r.attributed_to ? { attributed_to: r.attributed_to } : {}),+ ...trustFields(r.trust_tier, r.write_origin),})),},};diff --git a/test/facts-meta-cache.test.ts b/test/facts-meta-cache.test.tsindex 0930e42e16..d5c4886993 100644--- a/test/facts-meta-cache.test.ts+++ b/test/facts-meta-cache.test.ts@@ -189,9 +189,19 @@ describe('meta-hook cache', () => {});describe('meta-hook cache hygiene (bounded, expired-entry eviction)', () => {+ /**+ * Raw SQL for engine stubs: hot memory reads the withdrawal watermark, the+ * trust policy generation and each fact's activation verdict (#5575 ENG-11).+ */+ const stubRaw = async (sql: string, params?: unknown[]) =>+ sql.includes('trust_policy_state') ? [{ generation: '0' }]+ : sql.includes('fact_withdrawals') ? [{ n: 0, at: null }]+ : sql.includes('AS suppressed') ? ((params?.[0] as number[]) ?? []).map(id => ({ id, trust_tier: 'unknown', suppressed: false }))+ : [];/** Engine stub with no facts — every call takes the payload:undefined cache path. */function emptyEngine(): BrainEngine {return {+ executeRaw: stubRaw,listFactsBySession: async () => [],listFactsSince: async () => [],} as unknown as BrainEngine;@@ -272,6 +282,7 @@ describe('meta-hook cache hygiene (bounded, expired-entry eviction)', () => {function countingEngine(validUntil: Date): { engine: BrainEngine; calls: () => number } {let n = 0;const engine = {+ executeRaw: stubRaw,listFactsBySession: async () => [],listFactsSince: async () => { n++; return [factRow(validUntil)]; },} as unknown as BrainEngine;diff --git a/test/request-meta-session.test.ts b/test/request-meta-session.test.tsindex 695bba3b41..49cbbb14d1 100644--- a/test/request-meta-session.test.ts+++ b/test/request-meta-session.test.ts@@ -49,7 +49,7 @@ describe('per-session hot-memory cache stays bounded under remote session ids',test('2000 random remote session ids leave at most HOT_MEMORY_CACHE_MAX_ENTRIES entries', async () => {__resetHotMemoryCacheForTests();const engine = {- executeRaw: async () => [{ n: 0, at: null }],+ executeRaw: async () => [{ n: 0, at: null, generation: '0' }],listFactsBySession: async () => [],listFactsSince: async () => [],};