Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(remember): refuse a non-UUID request_id on the batch path (#6280)
time-attack55ff96db7505
diff --git a/src/core/remember-batch.ts b/src/core/remember-batch.tsindex a43403d0e5..1a98ad7d46 100644--- a/src/core/remember-batch.ts+++ b/src/core/remember-batch.ts@@ -21,6 +21,7 @@import { createHash, randomUUID } from 'node:crypto';import type { OperationContext } from './operations.ts';import { OperationError } from './ops/contract.ts';+import { isWriteRequestId } from './persistence/types.ts';export const REMEMBER_BATCH_MAX = 20;const ITEM_KEYS = new Set(['fact', 'provenance', 'entity', 'infer_entity', 'kind', 'ttl', 'visibility', 'replaces']);@@ -56,6 +57,10 @@ export async function runRememberBatch(ctx: OperationContext, p: Record<string,throw verbError('invalid_params', 'replaces names one fact, so it cannot apply to a whole batch.','Put replaces on the item it belongs to: items: [{ "fact": "...", "replaces": "<fact_id>" }].');}+ if (p.request_id !== undefined && !isWriteRequestId(p.request_id)) {+ throw verbError('invalid_params', 'request_id must be a UUID.',+ 'Generate a UUID before submitting the write and reuse it for retries of the same request.');+ }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];const normalized = items.map((raw, index) => {@@ -76,7 +81,7 @@ export async function runRememberBatch(ctx: OperationContext, p: Record<string,}}if (ctx.dryRun) return { dry_run: true, action: 'remember', items: normalized.length, protocol_version: 1 };- const requestId = typeof p.request_id === 'string' && p.request_id ? p.request_id : randomUUID();+ const requestId = typeof p.request_id === 'string' ? p.request_id : randomUUID();const results: BatchItemResult[] = [];const hints = new Set<string>();for (const [index, item] of normalized.entries()) {diff --git a/test/core-cli.test.ts b/test/core-cli.test.tsindex a378a7e065..3e03d97910 100644--- a/test/core-cli.test.ts+++ b/test/core-cli.test.ts@@ -165,6 +165,16 @@ describe('remember items[]', () => {expect(after).toBe(n);});+ test('a non-UUID request_id refuses with invalid_params and writes no fact (#6280)', async () => {+ const [{ n }] = await engine.executeRaw<{ n: number }>(`SELECT count(*)::int AS n FROM facts`);+ const r = await call('remember', { request_id: 'not-a-uuid', provenance: 'user said', items: [{ fact: 'Drinks oolong' }] }, true);+ expect(r.isError).toBe(true);+ expect(r.text).toContain('invalid_params');+ expect(r.text).toContain('request_id must be a UUID');+ const [{ n: after }] = await engine.executeRaw<{ n: number }>(`SELECT count(*)::int AS n FROM facts`);+ expect(after).toBe(n);+ });+test('provenance may be given per item instead of at the top level', async () => {const r = await call('remember', { items: [{ fact: 'Runs on Saturdays', provenance: 'user said' }, { fact: 'Uses Hoka shoes', provenance: 'user said' }] }, true);expect({ isError: r.isError, text: r.text }).toMatchObject({ isError: false });diff --git a/test/remember-batch.test.ts b/test/remember-batch.test.tsindex 68f04d4d76..f951ae4461 100644--- a/test/remember-batch.test.ts+++ b/test/remember-batch.test.ts@@ -29,12 +29,37 @@ describe('remember items[] batch', () => {test('saves each item with shared provenance and child request ids', async () => {const { handler, writes } = fakeSingle();- const out = await runRememberBatch(ctx, { request_id: 'r1', provenance: 'session', items: [{ fact: 'a' }, { fact: 'b', provenance: 'own' }] }, handler, ve);+ const rid = '22222222-2222-4222-8222-222222222222';+ const out = await runRememberBatch(ctx, { request_id: rid, provenance: 'session', items: [{ fact: 'a' }, { fact: 'b', provenance: 'own' }] }, handler, ve);expect(out.saved).toBe(2);expect(out.partial).toBe(false);expect(writes.map(w => w.provenance)).toEqual(['session', 'own']);- expect(writes[0].request_id).toBe(childRequestId('r1', 0));- expect(writes[1].request_id).toBe(childRequestId('r1', 1));+ expect(writes[0].request_id).toBe(childRequestId(rid, 0));+ expect(writes[1].request_id).toBe(childRequestId(rid, 1));+ });++ test('a non-UUID request_id refuses the batch before any write (#6280)', async () => {+ for (const request_id of ['not-a-uuid', 'r1', '', 42]) {+ const { handler, writes } = fakeSingle();+ let err: unknown;+ try { await runRememberBatch(ctx, { request_id, provenance: 'p', items: [{ fact: 'a' }] }, handler, ve); } catch (e) { err = e; }+ expect(err).toBeInstanceOf(OperationError);+ expect((err as OperationError).code).toBe('invalid_params');+ expect((err as Error).message).toContain('request_id must be a UUID');+ expect(writes).toHaveLength(0);+ }+ });++ test('a UUID in either case or an omitted request_id is accepted; children keep the caller spelling', async () => {+ const lower = 'abcdefab-cdef-4abc-8def-abcdefabcdef';+ for (const request_id of [lower, lower.toUpperCase()]) {+ const { handler, writes } = fakeSingle();+ await runRememberBatch(ctx, { request_id, provenance: 'p', items: [{ fact: 'a' }] }, handler, ve);+ expect(writes[0].request_id).toBe(childRequestId(request_id, 0));+ }+ const { handler, writes } = fakeSingle();+ await runRememberBatch(ctx, { provenance: 'p', items: [{ fact: 'a' }] }, handler, ve);+ expect(writes).toHaveLength(1);});test('one invalid item refuses the whole batch before any write', async () => {