Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): a response_too_large reply to a write carries its request_id and receipt (#6989)
garrytan7210501e5896
diff --git a/src/core/ops/contract.ts b/src/core/ops/contract.tsindex f82e1b2c8b..d66329f48d 100644--- a/src/core/ops/contract.ts+++ b/src/core/ops/contract.ts@@ -79,6 +79,8 @@ export class OperationError extends Error {public protocolVersion?: number;public writeRequest?: WriteReceipt;public writeError?: WriteErrorCode;+ /** #6989: the request ID of a write that may have committed although its reply was not delivered (`response_too_large`); never invented for a read. */+ public requestId?: string;/** Agent contract v1 (A1): which cause, when one code covers several. */public reason?: string;/** Agent contract v1: why it happened, for the agent to explain and weigh. */@@ -151,6 +153,7 @@ export class OperationError extends Error {detail: this.detail,protocol_version: this.protocolVersion,...(this.writeRequest ? { write_request: publicWriteReceipt(this.writeRequest) } : {}),+ ...(this.requestId ? { request_id: this.requestId } : {}),...(this.writeError ? { write_error: this.writeError } : {}),...(this.reason !== undefined ? { reason: this.reason } : {}),...(this.why !== undefined ? { why: this.why } : {}),diff --git a/src/core/persistence/ipc.ts b/src/core/persistence/ipc.tsindex ef2a01405d..2e3e7a7a97 100644--- a/src/core/persistence/ipc.ts+++ b/src/core/persistence/ipc.ts@@ -7,7 +7,7 @@ import { readFix } from '../ops/op-fix.ts';import type { Action } from '../agent-output.ts';import type { ProjectionBacklog } from '../page-state/projections.ts';import { resolveSocketPathForConfig, socketHasLiveListener } from '../context/resolve-ipc.ts';-import { isWriteErrorCode, isWriteReceipt, isWriteRequestId, publicWriteReceipt } from './types.ts';+import { isWriteErrorCode, isWriteReceipt, isWriteRequestId, publicWriteReceipt, type WriteReceipt } from './types.ts';import { isPersistenceAdminExtension, isPersistenceAdminOperation, PERSISTENCE_ADMIN_EXTENSIONS, PERSISTENCE_ADMIN_OPERATIONS, type PersistenceAdminExtension, type PersistenceAdminOperation } from './admin-contract.ts';import { claimLocalIpcBinding, isWindowsIpcPipe, prepareLocalIpcPath } from '../context/ipc-path.ts';@@ -231,6 +231,7 @@ export async function startPersistenceIpcServer(chunks = [];void (async () => {let admitted = false;+ let mutation: { requestId: string; receipt?: WriteReceipt } | undefined;try {const request: unknown = JSON.parse(payload);if (record(request) && exactKeys(request, ['version', 'kind']) && request.version === 1 && request.kind === 'capabilities') {@@ -267,9 +268,17 @@ export async function startPersistenceIpcServer(if (request.kind === 'administration' && !provider.administer) throw opError('unavailable', 'This owner does not support local administration.','The running persistence owner was started without local administration (an older release or a non-CLI owner), so nothing ran. Restart the owner on this gbrain release, or stop it and run the administration command from the gbrain CLI on this host.',{ fix: ownerVersionsFix() });+ if (request.kind === 'operation' && isPersistenceIpcMutation(request.operation)) mutation = { requestId: request.params.request_id as string };const result = request.kind === 'administration' ? await provider.administer!(request) : await provider.dispatch(request);+ if (mutation && record(result) && isWriteReceipt(result.write_request)) mutation.receipt = result.write_request;if (!socket.destroyed) socket.end(responseFrame({ version: 1, ok: true, result }));} catch (error) {+ // #6989: a write whose reply is too large to frame may have committed; the error carries its request ID (and+ // receipt, when the result held one) so the advice to inspect it is actionable. A read gets none.+ if (mutation && error instanceof OperationError && error.code === 'response_too_large') {+ error.requestId = mutation.requestId;+ if (mutation.receipt) error.writeRequest = publicWriteReceipt(mutation.receipt);+ }if (!socket.destroyed) socket.end(responseFrame({ version: 1, ok: false, error: publicError(error) }));} finally {if (admitted) active--;@@ -342,6 +351,7 @@ function remoteOperationError(value: unknown): OperationError {if (typeof value.detail === 'string') error.detail = value.detail;if (typeof value.protocol_version === 'number') error.protocolVersion = value.protocol_version;if (isWriteReceipt(value.write_request)) error.writeRequest = publicWriteReceipt(value.write_request);+ if (isWriteRequestId(value.request_id)) error.requestId = value.request_id;if (isWriteErrorCode(value.write_error)) error.writeError = value.write_error;return error;}@@ -384,7 +394,12 @@ async function exchange(socketPath: string, request: unknown, timeoutMs: number,const value: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));if (!record(value) || value.version !== 1 || typeof value.ok !== 'boolean') { lost(); return; }if (value.ok) settle(undefined, value.result);- else settle(remoteOperationError(value.error));+ else {+ const error = remoteOperationError(value.error);+ // #6989: an owner that predates the server-side ID still answers a mutation's oversized reply without it.+ if (requestId && error.code === 'response_too_large' && !error.requestId) error.requestId = requestId;+ settle(error);+ }} catch { lost(); }});});diff --git a/test/persistence-ipc-response-too-large-6989.test.ts b/test/persistence-ipc-response-too-large-6989.test.tsnew file mode 100644index 0000000000..b24287089d--- /dev/null+++ b/test/persistence-ipc-response-too-large-6989.test.ts@@ -0,0 +1,94 @@+/**+ * D-24 (GBRA-131, #6989): a persistence IPC reply too large to frame keeps the write's request ID.+ *+ * Protects: when the owner dispatched a mutation and its result exceeds the local frame limit, the+ * `response_too_large` error the client receives names the mutation's request_id (and its receipt when the result+ * held one), so the advice "inspect it by its request_id" is actionable; the client also fills the ID in when an+ * older owner omits it. The error stays an error (no success is claimed). A read's oversized reply carries no request ID.+ * Fails when: the owner's catch frames the generic error and drops the ID the client generated (local-client.ts+ * allocates it), leaving advice that requires an ID absent from the reply.+ * Seams: a real IPC listener with a stub dispatch returning a committed receipt plus an oversized payload.+ */+import { afterEach, describe, expect, test } from 'bun:test';+import net from 'node:net';+import { once } from 'node:events';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { OperationError } from '../src/core/ops/contract.ts';+import { PERSISTENCE_IPC_MAX_BYTES, requestPersistenceOperation, startPersistenceIpcServer, type PersistenceIpcBinding,+ type PersistenceIpcRequest } from '../src/core/persistence/ipc.ts';++const BRAIN = '10000000-0000-4000-8000-000000000001';+const ID = '20000000-0000-4000-8000-000000000069';+const REGISTRATION = { id: '30000000-0000-4000-8000-000000000001', credential: 'a'.repeat(64), lane: 'cli' as const };+const dirs: string[] = [];+const bindings: PersistenceIpcBinding[] = [];+const servers: net.Server[] = [];++function socketPath() {+ const dir = mkdtempSync(join(tmpdir(), 'gb-ipc-6989-'));+ dirs.push(dir);+ return join(dir, 'write.sock');+}+const request = (operation: string, params: Record<string, unknown>): PersistenceIpcRequest =>+ ({ version: 1, kind: 'operation', brain_id: BRAIN, operation, params, registration: REGISTRATION, routing: { source: null, cwd: tmpdir() } }) as PersistenceIpcRequest;+const receipt = { request_id: ID, state: 'committed', retry_after_ms: null };+const oversized = { write_request: receipt, page: { compiled_truth: 'x'.repeat(PERSISTENCE_IPC_MAX_BYTES) } };++async function bind(path: string, dispatch: (request: PersistenceIpcRequest) => Promise<unknown>) {+ const binding = await startPersistenceIpcServer(path, { brainId: BRAIN, dispatch });+ expect(binding).not.toBeNull();+ bindings.push(binding!);+}+const rejection = (promise: Promise<unknown>) => promise.then(() => null, (error: unknown) => error);++afterEach(async () => {+ for (const binding of bindings.splice(0)) {+ const closed = binding.server.listening ? once(binding.server, 'close') : Promise.resolve();+ binding.close();+ await closed;+ }+ for (const server of servers.splice(0)) if (server.listening) { const closed = once(server, 'close'); server.close(); await closed; }+ for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });+});++describe('D-24 persistence IPC response_too_large keeps the write identity (#6989)', () => {+ test('a mutation whose result is too large to frame: the error names its request_id and receipt', async () => {+ const path = socketPath();+ await bind(path, async () => oversized);+ const error = await rejection(requestPersistenceOperation(path, request('put_page', { request_id: ID, slug: 'notes/acme-example', content: 'x' })));+ expect(error).toBeInstanceOf(OperationError);+ const wire = (error as OperationError).toJSON() as Record<string, unknown>;+ expect({ code: wire.code, request_id: wire.request_id, receipt: (wire.write_request as { request_id?: string } | undefined)?.request_id })+ .toEqual({ code: 'response_too_large', request_id: ID, receipt: ID });+ });++ test('a mutation result with no receipt still carries the request_id', async () => {+ const path = socketPath();+ await bind(path, async () => ({ page: oversized.page }));+ const error = await rejection(requestPersistenceOperation(path, request('put_page', { request_id: ID, slug: 'notes/acme-example', content: 'x' })));+ const wire = (error as OperationError).toJSON() as Record<string, unknown>;+ expect({ code: wire.code, request_id: wire.request_id, receipt: wire.write_request }).toEqual({ code: 'response_too_large', request_id: ID, receipt: undefined });+ });++ test('a read whose result is too large carries no invented request identity', async () => {+ const path = socketPath();+ await bind(path, async () => oversized);+ const error = await rejection(requestPersistenceOperation(path, request('get_page', { slug: 'notes/acme-example' })));+ const wire = (error as OperationError).toJSON() as Record<string, unknown>;+ expect({ code: wire.code, request_id: wire.request_id, receipt: wire.write_request }).toEqual({ code: 'response_too_large', request_id: undefined, receipt: undefined });+ });++ test('an older owner that omits the ID: the client fills in the request_id it sent', async () => {+ const path = socketPath();+ const server = net.createServer(socket => {+ socket.once('data', () => socket.end(JSON.stringify({ version: 1, ok: false, error: { error: 'response_too_large', message: 'Persistence response exceeds the local transport limit.' } }) + '\n'));+ });+ servers.push(server);+ server.listen(path);+ await once(server, 'listening');+ const error = await rejection(requestPersistenceOperation(path, request('put_page', { request_id: ID, slug: 'notes/acme-example', content: 'x' })));+ expect(((error as OperationError).toJSON() as Record<string, unknown>).request_id).toBe(ID);+ });+});