Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(memory): remember records the MCP session on the fact (#6086)

time-attackf066faaacedc
diff --git a/src/core/persistence/memory-mutations.ts b/src/core/persistence/memory-mutations.ts
index 1a36dd4480..a262f5ac97 100644
--- a/src/core/persistence/memory-mutations.ts
+++ b/src/core/persistence/memory-mutations.ts
@@ -162,8 +162,11 @@ export async function submitRememberMutation(ctx: OperationContext, params: Reco
const { slug, authority, snapshot, fence, binding, writeThrough } = linked?.target ?? await planRememberTarget(ctx, sourceId, source, entitySlug, null);
const row = await admitWrite(ctx.engine, { principal, operation: 'remember', sourceId, sourceIncarnation: source.incarnation,
slug, pageId: snapshot?.page.id ?? null, requestId, callerIntent,
+ // The transport's session (MCP `_meta.session_id`) is recorded on the fact, as extract_facts records
+ // it, so recall's session_id filter finds single facts too. Identity only — never a trust surface.
intent: { ...callerIntent, entity_slug: entitySlug, fence, valid_from: new Date().toISOString(), valid_until: validUntil?.toISOString() ?? null,
- ...(linked ? { entity_inferred: linked.via } : {}), ...(inference && 'warning' in inference ? { entity_warning: inference.warning } : {}) },
+ ...(linked ? { entity_inferred: linked.via } : {}), ...(inference && 'warning' in inference ? { entity_warning: inference.warning } : {}),
+ session_id: ctx.sessionId ?? null },
authority, worktreeId: writeThrough ? binding?.worktree_id : null, topologyGeneration: writeThrough ? binding?.topology_generation : null });
return writeResponse(await waitForWrite(ctx.engine, row, ctx.config, waitMs ?? ctx.writeWaitMs));
}
diff --git a/src/core/persistence/memory-prepare.ts b/src/core/persistence/memory-prepare.ts
index 458dd9b5d9..6d7acca8d1 100644
--- a/src/core/persistence/memory-prepare.ts
+++ b/src/core/persistence/memory-prepare.ts
@@ -80,7 +80,8 @@ export async function prepareMemoryMutation(engine: BrainEngine, row: WriteReque
const validFrom = new Date(String(p.valid_from));
const context = p.entity_inferred ? appendContextNote(null, inferenceNote(p.entity_inferred as InferredVia)) : undefined;
const fact: NewFact = { ...input, source: String(p.provenance).trim(), valid_from: validFrom, valid_until: validUntil,
- confidence: 1, embedding, embedding_model, ...(context ? { context } : {}) };
+ confidence: 1, embedding, embedding_model, source_session: typeof p.session_id === 'string' ? p.session_id : null,
+ ...(context ? { context } : {}) };
let page: PreparedMutation | undefined;
let rowNum: number | undefined;
if (p.fence === true && snapshot) {
diff --git a/test/remember-session.test.ts b/test/remember-session.test.ts
new file mode 100644
index 0000000000..05e7de8696
--- /dev/null
+++ b/test/remember-session.test.ts
@@ -0,0 +1,71 @@
+/**
+ * `remember` records the transport's session on the fact, as `extract_facts` does, so `recall`'s
+ * `session_id` filter finds single facts too. The session rides MCP `_meta.session_id`: identity only,
+ * never a trust surface, and no new parameter.
+ */
+
+import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { dispatchToolCall } from '../src/mcp/dispatch.ts';
+
+let engine: PGLiteEngine;
+
+const STDIO = { remote: true, transport: 'stdio' as const, sourceId: 'default' };
+
+function parsed(result: { content: Array<{ text: string }> }) {
+ return JSON.parse(result.content[0].text);
+}
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+});
+
+afterAll(async () => {
+ await engine.disconnect();
+});
+
+describe('remember records the MCP session', () => {
+ test('a fact remembered in a session is recalled by that session, and not by another', async () => {
+ const res = await dispatchToolCall(
+ engine,
+ 'remember',
+ { fact: 'the ledger closes on Fridays', provenance: 'test', _meta: { session_id: 'sess-a' } },
+ { ...STDIO },
+ );
+ expect(res.isError ?? false).toBe(false);
+ const { id } = parsed(res);
+
+ const mine = parsed(await dispatchToolCall(engine, 'recall', { session_id: 'sess-a' }, { ...STDIO }));
+ expect(mine.facts.map((f: { fact_id: string }) => f.fact_id)).toContain(id);
+
+ const other = parsed(await dispatchToolCall(engine, 'recall', { session_id: 'sess-b' }, { ...STDIO }));
+ expect(other.facts.map((f: { fact_id: string }) => f.fact_id)).not.toContain(id);
+ });
+
+ test('the transport-resolved session wins over the arguments-level one', async () => {
+ const res = await dispatchToolCall(
+ engine,
+ 'remember',
+ { fact: 'the bank settles at noon', provenance: 'test', _meta: { session_id: 'sess-args' } },
+ { ...STDIO, sessionId: 'sess-transport' },
+ );
+ const { id } = parsed(res);
+ const byTransport = parsed(
+ await dispatchToolCall(engine, 'recall', { session_id: 'sess-transport' }, { ...STDIO }),
+ );
+ expect(byTransport.facts.map((f: { fact_id: string }) => f.fact_id)).toContain(id);
+ });
+
+ test('a fact remembered with no session has none', async () => {
+ const res = await dispatchToolCall(engine, 'remember', { fact: 'payroll runs monthly', provenance: 'test' }, { ...STDIO });
+ const { id } = parsed(res);
+ const any = parsed(await dispatchToolCall(engine, 'recall', { session_id: 'sess-a' }, { ...STDIO }));
+ expect(any.facts.map((f: { fact_id: string }) => f.fact_id)).not.toContain(id);
+ const rows = await engine.executeRaw<{ fact: string; source_session: string | null }>(
+ `SELECT fact, source_session FROM facts WHERE fact = 'payroll runs monthly'`,
+ );
+ expect(rows).toEqual([{ fact: 'payroll runs monthly', source_session: null }]);
+ });
+});