Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

feat(mcp): readiness and real worker status on gbrain://capabilities; whoami readiness + verified scopes (F2)

garrytanf7174fb39bb4
diff --git a/src/core/harness/capabilities.ts b/src/core/harness/capabilities.ts
index 4847723194..36337a7acc 100644
--- a/src/core/harness/capabilities.ts
+++ b/src/core/harness/capabilities.ts
@@ -61,7 +61,12 @@ export async function resolveAuthCapabilities(auth: AuthInfo, engine: BrainEngin
const visibleOperations = filterOpsForSurface(operations.filter(op => !op.localOnly), surface).filter(op =>
operationScopesAllowed(auth.scopes, op)
&& opAllowedForBoundClient(auth, op) && !disabled.has(op.name)).map(op => op.name);
- return describeAuthCapabilities(auth, { surface, visibleOperations, delegatedTools: grantCatalog().delegateToolNames });
+ // F2: config-plane readiness in the HTTP view (probed entries are host posture, never sent over HTTP).
+ const { configReadiness, readinessHttpView } = await import('../readiness.ts');
+ return {
+ ...describeAuthCapabilities(auth, { surface, visibleOperations, delegatedTools: grantCatalog().delegateToolNames }),
+ readiness: readinessHttpView(configReadiness(config, { transport: 'http' }).entries),
+ };
}
/** No credentials or private inventories. Uses the already authenticated grant
diff --git a/src/core/ops/sources.ts b/src/core/ops/sources.ts
index c3679a88e5..0d20f5b17c 100644
--- a/src/core/ops/sources.ts
+++ b/src/core/ops/sources.ts
@@ -34,14 +34,27 @@ const whoami: Operation = {
// where code conditionally trusted on `scopes.includes('admin')` instead
// of `ctx.remote === false`. Empty scopes array forces clients to
// special-case `transport: 'local'` explicitly.
+ // F2: config-plane readiness (sync, in-memory config) rides every shape.
+ const { configReadiness, readinessHttpView } = await import('../readiness.ts');
+ const readiness = (transport: 'cli' | 'stdio' | 'http') => {
+ const entries = configReadiness(ctx.config, { transport }).entries;
+ return transport === 'http' ? readinessHttpView(entries) : entries;
+ };
if (ctx.remote === false) {
- return { transport: 'local', scopes: [] };
+ return { transport: 'local', scopes: [], readiness: readiness('cli') };
}
// #1061: stdio MCP is remote/untrusted by design but has no per-token
// auth (local pipe) — a known transport, not a bug. Report it instead of
- // throwing. Empty scopes: nothing here may be used to gate anything.
+ // throwing. Scopes are the verified stdio registration's grant (the same
+ // scopes gbrain://capabilities reports and dispatch enforces); [] without one.
if (!ctx.auth && ctx.transport === 'stdio') {
- return { transport: 'stdio', scopes: [] };
+ const { readLocalWriter, verifyLocalWriter } = await import('../persistence/identity.ts');
+ let scopes: readonly string[] = [];
+ try {
+ const verified = await verifyLocalWriter(ctx.engine, await readLocalWriter(ctx.engine, 'stdio'));
+ if (verified.remote) scopes = verified.grant.scopes;
+ } catch { /* no registration: no scopes */ }
+ return { transport: 'stdio', scopes, readiness: readiness('stdio') };
}
if (!ctx.auth) {
throw new OperationError(
@@ -66,6 +79,7 @@ const whoami: Operation = {
token_name: ctx.auth.clientName ?? ctx.auth.clientId,
scopes: ctx.auth.scopes,
expires_at: null,
+ readiness: readiness('http'),
};
},
cliHints: { name: 'whoami' },
diff --git a/src/mcp/initialize-context.ts b/src/mcp/initialize-context.ts
index d8d1cf2371..7c943f79a6 100644
--- a/src/mcp/initialize-context.ts
+++ b/src/mcp/initialize-context.ts
@@ -10,7 +10,7 @@ import type { GBrainConfig } from '../core/config.ts';
import type { AuthInfo, Operation } from '../core/operations.ts';
import { opAllowedForBoundClient } from '../core/operations.ts';
import { isCallable, publishGatesFromDisabled } from '../core/ops/callable.ts';
-import { configReadiness, readinessHttpView, readinessTail, type ReadinessCache, type ReadinessEntry } from '../core/readiness.ts';
+import { configReadiness, probedReadiness, readinessHttpView, readinessTail, type ReadinessCache, type ReadinessEntry } from '../core/readiness.ts';
import { isEngineDegraded } from '../core/degraded-marker.ts';
import { disabledOpsForPublishGates } from './publish-gates.ts';
import type { InstructionTools } from './instructions.ts';
@@ -45,3 +45,20 @@ export async function httpInstructionTools(
&& opAllowedForBoundClient(opts.auth, op)).map(op => op.name));
return { callable: n => names.has(n), readiness: await instructionReadiness(engine, config, 'http', opts.cache) };
}
+
+/**
+ * F2: `gbrain://capabilities` on stdio — every readiness entry (config plane +
+ * probed tier, 60 s cache) and the real worker status from the worker probe
+ * (it replaces the old constant `unknown`).
+ */
+export async function stdioCapabilityReadiness(engine: BrainEngine, config: GBrainConfig | null | undefined): Promise<{
+ readiness: ReadinessEntry[]; worker: { status: string; reason?: string; why?: string };
+}> {
+ if (!config) return { readiness: [], worker: { status: 'unknown', reason: 'not_configured' } };
+ const readiness = [...configReadiness(config, { transport: 'stdio' }).entries];
+ try {
+ if (!isEngineDegraded(engine)) readiness.push(...await probedReadiness(engine));
+ } catch { /* the probed tier is best-effort */ }
+ const w = readiness.find(e => e.capability === 'worker');
+ return { readiness, worker: w ? { status: w.state, reason: w.reason, why: w.why } : { status: 'unknown', reason: isEngineDegraded(engine) ? 'engine_unreachable' : 'probe_failed' } };
+}
diff --git a/src/mcp/server.ts b/src/mcp/server.ts
index 93f9db9160..83e74aef2a 100644
--- a/src/mcp/server.ts
+++ b/src/mcp/server.ts
@@ -18,7 +18,7 @@ import { gcSessionContextState } from '../core/context/session-state.ts';
import { bindResolveIpcForServe } from './resolve-ipc-binding.ts';
import { createPersistenceIpcProvider, residentPersistenceConfig } from '../core/persistence/provider.ts';
import { installInstructionsResolver, resolveMcpInstructions } from './instructions.ts';
-import { instructionReadiness } from './initialize-context.ts';
+import { instructionReadiness, stdioCapabilityReadiness } from './initialize-context.ts';
import { STATUS_TOOL_DEF, STATUS_TOOL_NAME, attemptStatusRecovery, statusHeadline, statusInstructionLine, statusModeErrorResult, statusModeOf, statusPayload, statusToolResult } from './status-mode.ts';
import { installCapabilitiesResource, mcpAdministrationGuidance } from './capabilities.ts';
import { createSkillResources } from './skill-resources.ts';
@@ -288,7 +288,8 @@ export async function startMcpServer(engine: BrainEngine, opts: { surface?: McpS
administration: mcpAdministrationGuidance(),
shared_skills: { protocol_version: 2, catalog: available.includes('list_skills') && available.includes('get_skill'),
can_join: available.includes('join_brain'), can_edit: available.includes('put_skill') && available.includes('delete_skill'), native_activation: 'unverified' },
- worker: { status: 'unknown' }, note: 'This local MCP pipe has no OAuth profile; agent-facing operation restrictions still apply.' };
+ ...await stdioCapabilityReadiness(engine, config),
+ note: 'This local MCP pipe has no OAuth profile; agent-facing operation restrictions still apply.' };
}, createSkillResources(engine, async () => {
const scope = await resolveMcpStdioSourceScope(engine);
return { remote: true, transport: 'stdio', sourceId: scope.sourceId,
diff --git a/test/mcp-capabilities-readiness.test.ts b/test/mcp-capabilities-readiness.test.ts
new file mode 100644
index 0000000000..9d8204c522
--- /dev/null
+++ b/test/mcp-capabilities-readiness.test.ts
@@ -0,0 +1,61 @@
+/**
+ * F2 (agent operator contract v1): `gbrain://capabilities` carries readiness
+ * and the real worker status on stdio; whoami returns config-plane readiness
+ * and the verified stdio scopes; the HTTP view drops host posture and paths.
+ */
+import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { resetPgliteState } from './helpers/reset-pglite.ts';
+import { operations, type OperationContext } from '../src/core/operations.ts';
+import { stdioCapabilityReadiness } from '../src/mcp/initialize-context.ts';
+import type { GBrainConfig } from '../src/core/config.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+let engine: PGLiteEngine;
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+});
+afterAll(async () => { await engine.disconnect(); });
+beforeEach(async () => { await resetPgliteState(engine); });
+
+const whoami = operations.find(o => o.name === 'whoami')!;
+const config = { engine: 'pglite', database_path: '/home/someone/.gbrain/brain.pglite' } as unknown as GBrainConfig;
+const NO_KEYS = { OPENAI_API_KEY: undefined, ANTHROPIC_API_KEY: undefined, VOYAGE_API_KEY: undefined };
+
+function ctx(over: Partial<OperationContext>): OperationContext {
+ return { engine: engine as any, config, logger: console as any, dryRun: false, remote: true, sourceId: 'default', ...over };
+}
+
+describe('F2 readiness on capabilities and whoami', () => {
+ test('stdio capabilities: readiness entries + a probed worker status (no pending jobs)', async () => {
+ await withEnv(NO_KEYS, async () => {
+ const out = await stdioCapabilityReadiness(engine as any, config);
+ expect(out.readiness.some(e => e.capability === 'embeddings')).toBe(true);
+ expect(out.readiness.some(e => e.capability === 'worker' && e.tier === 'probed')).toBe(true);
+ expect(out.worker).toMatchObject({ status: 'ok', reason: 'no_pending_jobs' });
+ });
+ });
+
+ test('whoami on stdio: verified scopes (none without a registration) + config-plane readiness', async () => {
+ await withEnv(NO_KEYS, async () => {
+ const r = await whoami.handler(ctx({ transport: 'stdio' }), {}) as { transport: string; scopes: string[]; readiness: Array<{ capability: string; tier: string }> };
+ expect(r.transport).toBe('stdio');
+ expect(r.scopes).toEqual([]);
+ expect(r.readiness.length).toBeGreaterThan(0);
+ expect(r.readiness.every(e => e.tier === 'config')).toBe(true);
+ });
+ });
+
+ test('whoami over HTTP: the redacted view (no http-invisible entries, no local paths)', async () => {
+ await withEnv(NO_KEYS, async () => {
+ const auth = { token: 't', clientId: 'legacy-token', clientName: 'legacy-token', scopes: ['read'], principalKind: 'legacy' } as any;
+ const r = await whoami.handler(ctx({ transport: 'http' as any, auth }), {}) as { readiness: Array<{ capability: string; http_visible: boolean }> };
+ expect(r.readiness.length).toBeGreaterThan(0);
+ expect(r.readiness.every(e => e.http_visible)).toBe(true);
+ expect(r.readiness.some(e => e.capability === 'writeback')).toBe(false);
+ expect(JSON.stringify(r.readiness)).not.toContain('/home/someone');
+ });
+ });
+});
diff --git a/test/whoami.test.ts b/test/whoami.test.ts
index 69a9dd7dc2..844b9c17f9 100644
--- a/test/whoami.test.ts
+++ b/test/whoami.test.ts
@@ -162,7 +162,9 @@ describe('whoami op contract', () => {
expiresAt: 999999999,
};
const result = (await whoami.handler(ctxWith({ remote: true, auth }), {})) as any;
- expect(result).toEqual({ transport: 'legacy', token_name: 'gbrain_cl_lookalike', scopes: ['read'], expires_at: null });
+ // Agent contract v1 (F2): additive config-plane `readiness`.
+ expect(result).toMatchObject({ transport: 'legacy', token_name: 'gbrain_cl_lookalike', scopes: ['read'], expires_at: null });
+ expect(Array.isArray(result.readiness)).toBe(true);
});
test('legacy transport (token name as clientId, no gbrain_cl_ prefix)', async () => {