Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(cli-socket): GBRAIN_DEBUG names a missing socket and a warm hit
garrytan3b56b2a8a2dc
diff --git a/docs/guides/cli-socket.md b/docs/guides/cli-socket.mdindex 47b9705ae8..3d098af8e1 100644--- a/docs/guides/cli-socket.md+++ b/docs/guides/cli-socket.md@@ -106,7 +106,9 @@ The CLI falls back to its normal path, silently, when:- the CLI targets a mount brain (`--brain <mount>` and the other mount tiers).`GBRAIN_DEBUG=1` prints one `[cli-socket] cold path: <reason>` line on stderr-whenever a call falls back. `GBRAIN_CLI_SOCKET=0` makes a CLI call skip the+whenever a call falls back, including when no socket exists (`no socket at+<path>`, with how to start one), and one `[cli-socket] warm: <command>+answered by the serve socket at <path>` line when the serve answers. `GBRAIN_CLI_SOCKET=0` makes a CLI call skip thesocket entirely, for comparisons and benchmarks.## Securitydiff --git a/src/cli/cli-socket-client.ts b/src/cli/cli-socket-client.tsindex 3a7b4431be..1768eb6119 100644--- a/src/cli/cli-socket-client.ts+++ b/src/cli/cli-socket-client.ts@@ -28,6 +28,10 @@ function debug(reason: string): void {if (process.env.GBRAIN_DEBUG === '1') process.stderr.write(`[cli-socket] cold path: ${reason}\n`);}+function debugWarm(command: string, path: string): void {+ if (process.env.GBRAIN_DEBUG === '1') process.stderr.write(`[cli-socket] warm: ${command} answered by the serve socket at ${path}\n`);+}+type Exchange = { frame: ResultFrame } | { reason: string };/**@@ -204,15 +208,16 @@ export async function renderOp(command: string, op: import('../core/operations.texport async function maybeServeFromCliSocket(command: string, args: string[]): Promise<boolean> {if (!CLI_SOCKET_COMMANDS.includes(command) || process.env.GBRAIN_CLI_SOCKET === '0') return false;const config = loadConfig();- if (!config || config.engine !== 'postgres' || !config.database_url || isThinClient(config)) return false;+ if (!config || config.engine !== 'postgres' || !config.database_url || isThinClient(config)) { debug('not a local postgres brain'); return false; }const cliOpts = getCliOptions();- if (cliOpts.explain) return false;+ if (cliOpts.explain) { debug('--explain runs locally'); return false; }const home = configDir();const path = cliSocketPath(home);const problem = connectProblem(path, cliSocketDir(home));- if (problem) { if (problem !== 'missing') debug(`socket refused before connect (${problem})`); return false; }+ if (problem === 'missing') { debug(`no socket at ${path}; start the serve with gbrain serve --cli-socket or set serve.cli_socket true`); return false; }+ if (problem) { debug(`socket refused before connect (${problem})`); return false; }let brainId: string;- try { brainId = resolveBrainId(cliOpts.brain); } catch { return false; }+ try { brainId = resolveBrainId(cliOpts.brain); } catch { debug('brain did not resolve'); return false; }if (brainId !== 'host') { debug(`mount brain ${brainId}`); return false; }const base = {@@ -235,6 +240,7 @@ export async function maybeServeFromCliSocket(command: string, args: string[]):if ('reason' in outcome) { debug(outcome.reason); return false; }if (outcome.frame.kind === 'fallback') { debug(outcome.frame.reason); return false; }const frame = outcome.frame;+ debugWarm(command, path);await noteSource(frame);if (frame.kind === 'error' && !recall.isOneShotRecall(flags)) { await replay(frame); throw reviveOperationError(frame.error); }const { writeCliNotices } = await import('../core/interop-notices.ts');@@ -254,5 +260,6 @@ export async function maybeServeFromCliSocket(command: string, args: string[]):const outcome = await exchangeCliSocket(path, { ...base, kind: 'op', op: SOCKET_OPS[prepared.op.name]!, params: prepared.params }, resultTimeoutMs);if ('reason' in outcome) { debug(outcome.reason); return false; }if (outcome.frame.kind === 'fallback') { debug(outcome.frame.reason); return false; }+ debugWarm(command, path);return renderOp(command, prepared.op, prepared.params, outcome.frame);}diff --git a/test/e2e/cli-socket-postgres.test.ts b/test/e2e/cli-socket-postgres.test.tsindex c39dfca669..e92dfce767 100644--- a/test/e2e/cli-socket-postgres.test.ts+++ b/test/e2e/cli-socket-postgres.test.ts@@ -132,6 +132,14 @@ describe.skipIf(!DATABASE_URL)('warm CLI socket (postgres)', () => {await stopServe();}, 90_000);+ test('without a socket, GBRAIN_DEBUG names the missing socket and the read runs cold', async () => {+ expect(existsSync(socketPath)).toBe(false);+ const run = await cli(READS.search!, { GBRAIN_DEBUG: '1' });+ expect(run.stderr).toContain(`[cli-socket] cold path: no socket at ${socketPath}; start the serve with gbrain serve --cli-socket or set serve.cli_socket true`);+ expect(run.stderr).not.toContain('[cli-socket] warm:');+ expect({ stdout: run.stdout, exitCode: run.exitCode }).toEqual({ stdout: cold.get('search')!.stdout, exitCode: cold.get('search')!.exitCode });+ }, 90_000);+test('serve.cli_socket true opens it without the flag (read from the connect-time config merge)', async () => {expect((await cli(['config', 'set', 'serve.cli_socket', 'true'])).exitCode).toBe(0);try {@@ -161,8 +169,10 @@ describe.skipIf(!DATABASE_URL)('warm CLI socket (postgres)', () => {for (const [name, args] of Object.entries(READS)) {const warm = await cli(args, { GBRAIN_DEBUG: '1' });expect(warm.stderr, `${name} took the cold path`).not.toContain('[cli-socket] cold path');+ expect(warm.stderr).toContain(`[cli-socket] warm: ${args[0]} answered by the serve socket at ${socketPath}\n`);const expected = cold.get(name)!;- expect({ name, ...warm }).toEqual({ name, ...expected });+ const output = { ...warm, stderr: warm.stderr.replace(`[cli-socket] warm: ${args[0]} answered by the serve socket at ${socketPath}\n`, '') };+ expect({ name, ...output }).toEqual({ name, ...expected });}expect(cold.get('get missing')!.exitCode).not.toBe(0);}, 180_000);