Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test: POST /mcp dispatches in the serving engine's scope; runServeHttp runs in a local scope
garrytan427171bf01d5
diff --git a/test/serve-http-mcp-dispatch-context.serial.test.ts b/test/serve-http-mcp-dispatch-context.serial.test.tsindex 9ec86b9967..5ceb8d856c 100644--- a/test/serve-http-mcp-dispatch-context.serial.test.ts+++ b/test/serve-http-mcp-dispatch-context.serial.test.ts@@ -13,27 +13,33 @@* Why new: outcome tests show remote/auth/surface indirectly; nothing pinned* the literal dispatch context the handler builds.*+ *+ * Value: protects=POST /mcp dispatches inside the serving engine's scope once the process holds an embedded engine; fails_when=withEngineScope is dropped from the /mcp handler;+ * why_new=the entry-point tests call core functions directly; none drove the HTTP handler; seam=none; no_claim=per-engine AI keys (scope identity only; stock PGLite, no providers)+ ** Serial: mock.module wraps the real dispatcher to record its arguments.*/import { afterAll, beforeAll, describe, expect, mock, spyOn, test } from 'bun:test';-import { createHash } from 'node:crypto';+import { createHash, randomUUID } from 'node:crypto';import type { Server } from 'node:http';import express from 'express';import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { openEngine } from '../src/core/engine-open.ts';+import { currentEngineScope } from '../src/core/engine-scope.ts';import { getBrainHotMemoryMeta } from '../src/core/facts/meta-hook.ts';import { operations } from '../src/core/operations.ts';import { filterOpsForSurface } from '../src/mcp/surface.ts';import * as realDispatch from '../src/mcp/dispatch.ts';type DispatchArgs = Parameters<typeof realDispatch.dispatchToolCall>;-const calls: Array<{ name: string; opts: NonNullable<DispatchArgs[3]>; stderrBefore: string[] }> = [];+const calls: Array<{ name: string; opts: NonNullable<DispatchArgs[3]>; stderrBefore: string[]; scope: object | undefined }> = [];const stderrLines: string[] = [];const passthrough = { ...realDispatch };mock.module('../src/mcp/dispatch.ts', () => ({...passthrough,dispatchToolCall: async (...args: DispatchArgs) => {- calls.push({ name: args[1], opts: args[3] ?? {}, stderrBefore: [...stderrLines] });+ calls.push({ name: args[1], opts: args[3] ?? {}, stderrBefore: [...stderrLines], scope: currentEngineScope() });return passthrough.dispatchToolCall(...args);},}));@@ -166,3 +172,21 @@ describe('POST /mcp dispatch context', () => {for (const name of listed) expect(opts.allowedOps!.has(name)).toBe(true);});});++describe('POST /mcp engine scope', () => {+ test('a tool call dispatches in the serving engine\'s scope when the process holds an embedded engine', async () => {+ const embedded = await openEngine({+ embedded_core: true, engine: 'pglite', database: {}, host: { id: randomUUID() },+ embedding: { model: 'openai:text-embedding-3-small', dimensions: 1536 },+ });+ try {+ const { base, token } = await start(undefined, 'scope');+ calls.length = 0;+ await rpc(base, token, 'tools/call', { name: 'whoami', arguments: {} });+ expect(calls.map(c => c.name)).toEqual(['whoami']);+ expect(calls[0].scope).toBe(engine);+ } finally {+ await embedded.disconnect();+ }+ }, 60_000);+});diff --git a/test/serve-http-run-teardown.serial.test.ts b/test/serve-http-run-teardown.serial.test.tsindex e707d7a952..743b55740b 100644--- a/test/serve-http-run-teardown.serial.test.ts+++ b/test/serve-http-run-teardown.serial.test.ts@@ -13,6 +13,10 @@* Why new: the lifecycle tests drive waitForHttpServerLifecycle with fakes;* nothing ran the real runServeHttp teardown.*+ *+ * Value: protects=runServeHttp runs the server inside a local engine scope (the process gateway), not unscoped; fails_when=withLocalScope is removed from runServeHttp;+ * why_new=the teardown test never inspected the scope the server runs in; seam=none; no_claim=gateway selection (scope presence only)+ ** Serial: mutates GBRAIN_HOME, emits SIGINT on the process and reads the* process-global cleanup registry.*/@@ -23,7 +27,9 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { runServeHttp } from '../src/commands/serve-http.ts';+import * as processCleanup from '../src/core/process-cleanup.ts';import { _registeredCleanupCountForTests } from '../src/core/process-cleanup.ts';+import { currentEngineScope } from '../src/core/engine-scope.ts';let engine: PGLiteEngine;let home: string;@@ -73,3 +79,29 @@ test('SIGINT shutdown closes the resolve-IPC socket and deregisters every cleanuerrorSpy.mockRestore();}}, 30_000);++test('runServeHttp runs the server in a local engine scope', async () => {+ const scopes: Array<object | undefined> = [];+ const original = processCleanup.registerCleanup;+ const cleanupSpy = spyOn(processCleanup, 'registerCleanup').mockImplementation(((...args: Parameters<typeof original>) => {+ scopes.push(currentEngineScope());+ return original(...args);+ }) as typeof original);+ const errorSpy = spyOn(console, 'error').mockImplementation(() => {});+ try {+ const baseline = _registeredCleanupCountForTests();+ const done = runServeHttp(engine, { port: 0, tokenTtl: 3600, enableDcr: false });+ for (let i = 0; i < 200 && scopes.length === 0; i++) await Bun.sleep(25);+ expect(scopes.length).toBeGreaterThan(0);+ for (const scope of scopes) {+ expect(scope).toBeDefined();+ expect(scope).not.toBe(engine);+ }+ process.emit('SIGINT');+ await done;+ expect(_registeredCleanupCountForTests()).toBe(baseline);+ } finally {+ cleanupSpy.mockRestore();+ errorSpy.mockRestore();+ }+}, 30_000);