Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refactor(mcp): write-receipt capacity notices in one helper; dispatchToolCall back under 300 lines after the master merge (#6696)

garrytan965aab6aa720
diff --git a/src/core/persistence/capacity-snapshot.ts b/src/core/persistence/capacity-snapshot.ts
index ade1f6e2fa..2fc0b4702f 100644
--- a/src/core/persistence/capacity-snapshot.ts
+++ b/src/core/persistence/capacity-snapshot.ts
@@ -13,7 +13,7 @@
* clears and passes it anew.
*/
import type { Notice } from '../agent-output.ts';
-import type { SqlEngine } from './model.ts';
+import { principalKey, type Principal, type SqlEngine } from './model.ts';
import { BACKGROUND_PRODUCERS, readBackgroundPauseShare } from './admission-origin.ts';
import { readCapacityForecast } from './capacity-forecast.ts';
@@ -112,3 +112,9 @@ export function __resetCapacityNoticesForTests(): void {
lowShownAt.clear();
pausedShown.clear();
}
+
+/** #6696 (A4): a write receipt's capacity notices, throttled hourly per scope; a remote caller sees only the brain and its own principal. Never throws. */
+export async function writeReceiptCapacityNotices(engine: SqlEngine, ctx: { remote?: boolean; auth?: { principal?: Principal } }): Promise<Notice[]> {
+ const scopes = ctx.remote === false ? undefined : ['brain', ...(ctx.auth?.principal ? [principalKey(ctx.auth.principal)] : [])];
+ return capacityNotices(engine, { throttle: true, scopes }).catch(() => []);
+}
diff --git a/src/mcp/dispatch.ts b/src/mcp/dispatch.ts
index 77b5489655..bccc0565d1 100644
--- a/src/mcp/dispatch.ts
+++ b/src/mcp/dispatch.ts
@@ -6,8 +6,7 @@
* + missing-context bugs; this module exists to prevent that recurring.
*/
-import { capacityNotices } from '../core/persistence/capacity-snapshot.ts';
-import { principalKey } from '../core/persistence/model.ts';
+import { writeReceiptCapacityNotices } from '../core/persistence/capacity-snapshot.ts';
import { affectsRecall } from '../core/types.ts';
import type { BrainEngine } from '../core/engine.ts';
import { operations, OperationError, enforceBoundClientOpAllowList, opError } from '../core/operations.ts';
@@ -1064,11 +1063,9 @@ export async function dispatchToolCall(
return op.handler(ctx, safeParams);
})
: await op.handler(ctx, safeParams);
- // Hot memory built before a write (forget, remember, …) is never served after it, on every transport.
- if (op.mutating) invalidateHotMemoryForEngine(engine);
- // #6696 (A4): a write's receipt carries the capacity notices, from the 60 s snapshot and at most once an hour per scope.
- if (op.mutating) notices.push(...await capacityNotices(engine, { throttle: true,
- scopes: ctx.remote === false ? undefined : ['brain', ...(ctx.auth?.principal ? [principalKey(ctx.auth.principal)] : [])] }).catch(() => []));
+ // Hot memory built before a write (forget, remember, …) is never served after it, on every transport;
+ // #6696 (A4): the write's receipt carries the capacity notices (60 s snapshot, at most hourly per scope).
+ if (op.mutating) { invalidateHotMemoryForEngine(engine); notices.push(...await writeReceiptCapacityNotices(engine, ctx)); }
// [E4] verb success metrics: budget drops + entity hit/miss when present.
{
const r = result as { dropped_count?: number; found?: boolean; status?: string } | null;