Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(remember): a top-level entity is the default subject of every batch item (#7432)

garrytan62d52b74394f
diff --git a/docs/architecture/key-files/core-memory.md b/docs/architecture/key-files/core-memory.md
index e1f41f70f2..d02aa116c3 100644
--- a/docs/architecture/key-files/core-memory.md
+++ b/docs/architecture/key-files/core-memory.md
@@ -53,11 +53,13 @@ notice). User guide: [core memory](../../guides/core-memory.md).
hook and OpenClaw `assemble()`.
- `src/core/remember-batch.ts`: `remember` with `items[]` (all-or-none
validation, deterministic child request ids, per-item status; #7248: an
- item still publishing is `pending` with its `target`, not `failed`).
+ item still publishing is `pending` with its `target`, not `failed`; #7432:
+ a top-level `entity` is the default subject of every item that sets none).
- `src/commands/core.ts` (`gbrain core`), `src/commands/doctor/checks/core-memory.ts`
(`core_memory`), schema `src/core/core-memory-schema.ts` + migration v213.
- Tests: `test/core-memory.test.ts`, `test/core-guard.test.ts`,
`test/core-cli.test.ts`, `test/compile-context-core.serial.test.ts`,
`test/context-pressure.test.ts`, `test/remember-batch.test.ts`,
+ `test/remember-batch-entity-7432.test.ts`,
`test/trust-cache-invalidation.test.ts` (core lane after trust transitions),
`test/e2e/core-memory-locks-postgres.test.ts`.
diff --git a/docs/guides/core-memory.md b/docs/guides/core-memory.md
index f0c7ccd132..2b578828a9 100644
--- a/docs/guides/core-memory.md
+++ b/docs/guides/core-memory.md
@@ -120,8 +120,9 @@ Two pieces cover that:
- `remember` takes `items`: up to 20 facts in one call, each with the usual
fields (`fact`, optional `entity`, `kind`, `ttl`, `visibility`,
- `provenance`). Every item is validated before anything is written; each item
- is then saved as its own write and reported with its own status, and
+ `provenance`); top-level `entity`, `kind`, `ttl`, `visibility` and
+ `provenance` are the defaults for items that omit them. Every item is
+ validated before anything is written; each item is then saved as its own write and reported with its own status, and
`partial: true` marks a batch where some items failed. Replaying the same
`request_id` replays the same per-item outcomes.
- When a session's context is about 80% full, or sooner when it is growing
diff --git a/docs/protocol/MEMORY_VERBS_v1.md b/docs/protocol/MEMORY_VERBS_v1.md
index a64e696b60..7b82c8e83b 100644
--- a/docs/protocol/MEMORY_VERBS_v1.md
+++ b/docs/protocol/MEMORY_VERBS_v1.md
@@ -386,9 +386,9 @@ Optional response fields; clients must ignore any they do not know.
`items` (1 to 20) replaces `fact` for a batch, typically right before
context compaction. Each item is a fact string or an object with `fact` plus
optional `entity`, `kind`, `ttl`, `visibility`, `provenance`,
-`infer_entity` and `replaces`; top-level `provenance`,
-`kind`, `ttl`, `visibility` and `infer_entity` are the defaults, so top-level
-`provenance` is optional when every item carries its own. `replaces` names one
+`infer_entity` and `replaces`; top-level `provenance`, `entity`,
+`kind`, `ttl`, `visibility` and `infer_entity` are the defaults for items that
+omit them, so top-level `provenance` is optional when every item carries its own. `replaces` names one
fact, so it goes on its item; passing both `fact` and `items`, or a top-level
`replaces` with `items`, is `invalid_params`.
diff --git a/llms-full.txt b/llms-full.txt
index 5e42576b32..15b0fbc1ba 100644
--- a/llms-full.txt
+++ b/llms-full.txt
@@ -9245,9 +9245,9 @@ Optional response fields; clients must ignore any they do not know.
`items` (1 to 20) replaces `fact` for a batch, typically right before
context compaction. Each item is a fact string or an object with `fact` plus
optional `entity`, `kind`, `ttl`, `visibility`, `provenance`,
-`infer_entity` and `replaces`; top-level `provenance`,
-`kind`, `ttl`, `visibility` and `infer_entity` are the defaults, so top-level
-`provenance` is optional when every item carries its own. `replaces` names one
+`infer_entity` and `replaces`; top-level `provenance`, `entity`,
+`kind`, `ttl`, `visibility` and `infer_entity` are the defaults for items that
+omit them, so top-level `provenance` is optional when every item carries its own. `replaces` names one
fact, so it goes on its item; passing both `fact` and `items`, or a top-level
`replaces` with `items`, is `invalid_params`.
diff --git a/src/core/remember-batch.ts b/src/core/remember-batch.ts
index 1448ebbc6e..e742a8c127 100644
--- a/src/core/remember-batch.ts
+++ b/src/core/remember-batch.ts
@@ -61,7 +61,8 @@ export async function runRememberBatch(ctx: OperationContext, p: Record<string,
// #6280: an unusable request_id refuses the batch before any write (F6 accepts a UUID or a printable client id).
if (p.request_id !== undefined) parseWriteRequestId(p.request_id);
const shared: Record<string, unknown> = {};
- for (const key of ['provenance', 'source_id', 'kind', 'ttl', 'visibility', 'infer_entity']) if (p[key] !== undefined) shared[key] = p[key];
+ // #7432: a top-level entity is every item's default subject, like provenance; an item's own entity wins.
+ for (const key of ['provenance', 'source_id', 'entity', 'kind', 'ttl', 'visibility', 'infer_entity']) if (p[key] !== undefined) shared[key] = p[key];
const normalized = items.map((raw, index) => {
// A bare string is the common shorthand for one fact.
if (typeof raw === 'string') return { ...shared, fact: raw };
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index 98ba735b01..eb7f861202 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -390,6 +390,7 @@ test/recall-page-fill.test.ts
test/receipt-compaction-newest-sync-gbra130.test.ts
test/reconcile-clears-stalled-hold-7364.test.ts
test/remediation-manual-only.test.ts
+test/remember-batch-entity-7432.test.ts
test/repair-apply-pending-6543.test.ts
test/repair-database-stop-gbra133.test.ts
test/repair-embedding-effects.test.ts
diff --git a/test/remember-batch-entity-7432.test.ts b/test/remember-batch-entity-7432.test.ts
new file mode 100644
index 0000000000..6f81d4973d
--- /dev/null
+++ b/test/remember-batch-entity-7432.test.ts
@@ -0,0 +1,71 @@
+/**
+ * #7432: a top-level `entity` on a `remember` items batch is the default
+ * subject for every item that names none, like top-level provenance, kind,
+ * ttl and visibility; an item's own `entity` wins.
+ *
+ * Fails when: the batch drops the top-level entity, so the items save with
+ * `entity_slug: null` and a `NO_ENTITY` warning.
+ * The facts name nobody, so entity inference cannot fill the subject in.
+ * Seams: none; the real remember operation on an unmanaged brain, keyless.
+ * PGLite always, Postgres when DATABASE_URL is set (testBackends).
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import type { BrainEngine } from '../src/core/engine.ts';
+import type { OperationContext } from '../src/core/operations.ts';
+import { operationsByName } from '../src/core/operations.ts';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { configureGateway, resetGateway } from '../src/core/ai/gateway.ts';
+import { LEGACY_EMBEDDING_CONFIG } from './helpers/legacy-embedding-config.ts';
+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
+
+const SHARED = 'people/alice-example';
+const OWN = 'people/bob-example';
+
+beforeAll(() => { configureGateway({ ...LEGACY_EMBEDDING_CONFIG, env: {} }); });
+afterAll(() => { resetGateway(); });
+
+for (const backend of testBackends()) {
+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;
+
+ describe(`remember items with a top-level entity (${backend})`, () => {
+ let engine: BrainEngine;
+ let close: () => Promise<void>;
+ beforeAll(async () => {
+ if (databaseUrl) {
+ ({ engine, close } = await isolatedPersistencePostgres(databaseUrl));
+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');
+ } else {
+ const pglite = new PGLiteEngine();
+ await pglite.connect({});
+ await pglite.initSchema();
+ engine = pglite;
+ close = () => pglite.disconnect();
+ }
+ await engine.putPage(SHARED, { type: 'person', title: 'Alice Example', compiled_truth: '# Alice Example' });
+ await engine.putPage(OWN, { type: 'person', title: 'Bob Example', compiled_truth: '# Bob Example' });
+ }, 120_000);
+ afterAll(async () => { await close?.(); });
+
+ test('items without an entity inherit it; an item with its own entity keeps it', async () => {
+ const ctx = { engine, config: {}, logger: { info() {}, warn() {}, error() {} }, dryRun: false, remote: false, sourceId: 'default' } as unknown as OperationContext;
+ const out = await operationsByName.remember!.handler(ctx, {
+ entity: SHARED,
+ provenance: 'synthetic session',
+ items: [
+ { fact: 'Prefers written agendas.' },
+ 'Ships the synthetic report every Friday.',
+ { fact: 'Reviews the synthetic budget.', entity: OWN },
+ ],
+ }) as { saved: number; failed: number; items: Array<{ status: string; entity_slug?: string | null; warnings?: string[] }> };
+
+ expect(out.failed).toBe(0);
+ expect(out.saved).toBe(3);
+ expect(out.items.map(item => item.entity_slug)).toEqual([SHARED, SHARED, OWN]);
+ expect(out.items.flatMap(item => item.warnings ?? [])).not.toContain('NO_ENTITY');
+ const rows = await engine.executeRaw<{ fact: string; entity_slug: string | null }>(
+ 'SELECT fact, entity_slug FROM facts WHERE expired_at IS NULL ORDER BY id');
+ expect(rows.map(row => row.entity_slug)).toEqual([SHARED, SHARED, OWN]);
+ }, 120_000);
+ });
+}
diff --git a/test/remember-batch.test.ts b/test/remember-batch.test.ts
index dedd6ef707..7a2905d388 100644
--- a/test/remember-batch.test.ts
+++ b/test/remember-batch.test.ts
@@ -38,6 +38,22 @@ describe('remember items[] batch', () => {
expect(writes[1].request_id).toBe(childRequestId(rid, 1));
});
+ test("a top-level entity reaches validation and admission; an item's own entity wins (#7432)", async () => {
+ const validated: unknown[] = [];
+ const admitted: unknown[] = [];
+ await runRememberBatch(ctx, {
+ entity: 'people/alice-example',
+ provenance: 'session',
+ items: [{ fact: 'Prefers tea' }, 'Ships weekly', { fact: 'Prefers coffee', entity: 'people/bob-example' }, { fact: 'An unscoped note', entity: null }],
+ }, async (c, p) => {
+ (c.dryRun ? validated : admitted).push(p.entity);
+ return { status: 'saved' };
+ }, ve);
+ const expected = ['people/alice-example', 'people/alice-example', 'people/bob-example', null];
+ expect(validated).toEqual(expected);
+ expect(admitted).toEqual(expected);
+ });
+
test('an unusable request_id refuses the batch before any write (#6280)', async () => {
for (const request_id of ['', 42, null, 'has space', 'x'.repeat(129)]) {
const { handler, writes } = fakeSingle();