@myclaw163/clawclaw-cli 0.8.14 → 0.8.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/custom-modules/perception.md +4 -4
- package/docs/runtime/README.md +1 -1
- package/docs/runtime/behavior-runtime.md +1 -1
- package/docs/runtime/official-perception-modules.md +23 -30
- package/docs/runtime/perception-replay.md +1 -1
- package/package.json +1 -1
- package/scripts/sync-bundled-skill.mjs +28 -57
- package/skills/clawclaw/SKILL.md +6 -2
- package/skills/clawclaw/references/BEHAVIORS.md +16 -1
- package/skills/clawclaw/references/COMMANDS.md +1 -1
- package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +310 -0
- package/skills/clawclaw/references/CUSTOM-MODULE-API.md +684 -0
- package/skills/clawclaw/references/CUSTOM-MODULES.md +125 -0
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +331 -0
- package/skills/clawclaw/references/HUB.md +2 -1
- package/src/commands/account.ts +36 -0
- package/src/commands/capability.ts +6 -1
- package/src/commands/load.ts +11 -0
- package/src/lib/agent-workspace-runtime.ts +2 -1
- package/src/lib/agent-workspace-skills.ts +235 -23
- package/src/lib/agent-workspace-sync.ts +17 -5
- package/src/lib/capability-cloud-refresh.ts +10 -1
- package/src/lib/capability-cloud-sync.ts +71 -1
- package/src/lib/capability-workspace.ts +2 -0
- package/src/lib/game-client.ts +10 -0
- package/src/runtime/clawclaw/behavior-decision.ts +4 -1
- package/src/runtime/clawclaw/owner.ts +79 -2
- package/src/runtime/clawclaw/plugins/assembly.ts +4 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +35 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +11 -7
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +3 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +46 -50
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +39 -9
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +167 -7
- package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-perception-report-adapter.ts +107 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +86 -89
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +157 -41
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +28 -4
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +3 -1
- package/src/runtime/clawclaw/plugins/perception/perception-module-output.ts +30 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +126 -17
- package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-pool.ts +660 -0
- package/src/runtime/clawclaw/plugins/perception/perception-reducer-thread-worker.ts +262 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +41 -12
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +252 -20
- package/src/runtime/clawclaw/plugins/trigger/trigger-thread-pool.ts +496 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-thread-worker.ts +164 -0
- package/src/runtime/clawclaw-event-catalog.ts +1 -0
- package/src/runtime/framework/events/memory-event-repository.ts +12 -0
- package/src/runtime/framework/modules/runtime-module-output.ts +24 -0
- package/src/runtime/framework/persistence/memory-first-persistence.ts +3 -0
- package/src/runtime/framework/plugins/contracts.ts +3 -0
- package/src/runtime/framework/plugins/runtime-input-journal.ts +12 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +150 -23
- package/src/runtime/framework/runtime.ts +18 -0
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +10 -0
- package/src/runtime/game-sync-protocol.ts +47 -0
- package/src/runtime/observability/redaction.ts +28 -1
- package/src/runtime/ws-client.ts +7 -0
- package/src/sdk/perception-plugin/index.ts +13 -8
|
@@ -7,6 +7,8 @@ import type { RuntimeDiagnosticSeverity } from '../../../framework/plugins/index
|
|
|
7
7
|
import type {
|
|
8
8
|
BehaviorDecision,
|
|
9
9
|
BehaviorInput,
|
|
10
|
+
BehaviorTiming,
|
|
11
|
+
ClawclawBehaviorManifest,
|
|
10
12
|
} from '../../../../sdk/behavior-plugin/index.js';
|
|
11
13
|
import {
|
|
12
14
|
behaviorSourceUrl,
|
|
@@ -37,6 +39,8 @@ export interface ThreadBehaviorExecutorOptions {
|
|
|
37
39
|
/** How long an expanded, idle pool is retained before returning to two Workers. */
|
|
38
40
|
readonly idleWorkerTimeoutMs?: number;
|
|
39
41
|
readonly maxOldGenerationSizeMb?: number;
|
|
42
|
+
/** Samples Worker heaps only for explicit full diagnostics. */
|
|
43
|
+
readonly diagnosticsEnabled?: boolean;
|
|
40
44
|
readonly diagnostic?: (
|
|
41
45
|
name: string,
|
|
42
46
|
attributes?: Readonly<Record<string, unknown>>,
|
|
@@ -53,6 +57,8 @@ export interface ThreadBehaviorExecutorSnapshot {
|
|
|
53
57
|
readonly inFlightRequestCount: number;
|
|
54
58
|
readonly startedWorkerCount: number;
|
|
55
59
|
readonly retiredWorkerCount: number;
|
|
60
|
+
readonly workerHeapUsedBytes: number;
|
|
61
|
+
readonly peakWorkerHeapUsedBytes: number;
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
interface PendingRequest {
|
|
@@ -78,6 +84,7 @@ interface WorkerSlot {
|
|
|
78
84
|
current?: PendingRequest;
|
|
79
85
|
readyResolve?: () => void;
|
|
80
86
|
readyReject?: (error: Error) => void;
|
|
87
|
+
heapUsedBytes?: number;
|
|
81
88
|
retirement?: Promise<void>;
|
|
82
89
|
}
|
|
83
90
|
|
|
@@ -92,6 +99,10 @@ interface WorkerReadyMessage extends WorkerMessageBase {
|
|
|
92
99
|
readonly type: 'ready';
|
|
93
100
|
readonly behaviorId: string;
|
|
94
101
|
readonly execution: ResolvedClawclawBehaviorSource['execution'];
|
|
102
|
+
readonly manifest?: Readonly<ClawclawBehaviorManifest>;
|
|
103
|
+
readonly input?: Readonly<Record<string, unknown>>;
|
|
104
|
+
readonly timing?: Readonly<BehaviorTiming>;
|
|
105
|
+
readonly heapUsedBytes?: number;
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
interface WorkerResultMessage extends WorkerMessageBase {
|
|
@@ -100,12 +111,14 @@ interface WorkerResultMessage extends WorkerMessageBase {
|
|
|
100
111
|
readonly result: BehaviorDecision;
|
|
101
112
|
readonly queryCount: number;
|
|
102
113
|
readonly durationMs: number;
|
|
114
|
+
readonly heapUsedBytes?: number;
|
|
103
115
|
}
|
|
104
116
|
|
|
105
117
|
interface WorkerErrorMessage extends WorkerMessageBase {
|
|
106
118
|
readonly type: 'error';
|
|
107
119
|
readonly code: string;
|
|
108
120
|
readonly message?: string;
|
|
121
|
+
readonly heapUsedBytes?: number;
|
|
109
122
|
}
|
|
110
123
|
|
|
111
124
|
function asError(value: unknown, fallback: string): Error {
|
|
@@ -129,11 +142,106 @@ function roundedElapsed(startedAt: number): number {
|
|
|
129
142
|
return Math.round(Math.max(0, performance.now() - startedAt) * 1_000) / 1_000;
|
|
130
143
|
}
|
|
131
144
|
|
|
145
|
+
export interface BehaviorThreadPreparation {
|
|
146
|
+
readonly manifest: Readonly<ClawclawBehaviorManifest>;
|
|
147
|
+
readonly input: Readonly<Record<string, unknown>>;
|
|
148
|
+
readonly timing?: Readonly<BehaviorTiming>;
|
|
149
|
+
readonly source: ResolvedClawclawBehaviorSource;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Imports and normalizes a Behavior without evaluating player code in the Owner. */
|
|
153
|
+
export async function prepareBehaviorInWorker(
|
|
154
|
+
source: ResolvedClawclawBehaviorSource,
|
|
155
|
+
input: Readonly<Record<string, unknown>>,
|
|
156
|
+
options: Readonly<{
|
|
157
|
+
maxOldGenerationSizeMb?: number;
|
|
158
|
+
diagnosticsEnabled?: boolean;
|
|
159
|
+
}> = {},
|
|
160
|
+
): Promise<BehaviorThreadPreparation> {
|
|
161
|
+
const pinned = pinBehaviorSource(source);
|
|
162
|
+
const sourceUrl = behaviorSourceUrl(pinned);
|
|
163
|
+
const loader = behaviorThreadLoaderConfig();
|
|
164
|
+
const poolId = randomUUID();
|
|
165
|
+
const worker = new Worker(WORKER_BOOTSTRAP, {
|
|
166
|
+
execArgv: [],
|
|
167
|
+
resourceLimits: {
|
|
168
|
+
maxOldGenerationSizeMb: Math.max(16, Math.min(
|
|
169
|
+
256,
|
|
170
|
+
Math.floor(options.maxOldGenerationSizeMb ?? 64),
|
|
171
|
+
)),
|
|
172
|
+
},
|
|
173
|
+
stdout: true,
|
|
174
|
+
stderr: true,
|
|
175
|
+
workerData: {
|
|
176
|
+
runtimeModuleBootstrap: {
|
|
177
|
+
hostUrl: WORKER_HOST_URL,
|
|
178
|
+
hooksUrl: loader.hooksUrl,
|
|
179
|
+
sdkAliases: loader.sdkAliases,
|
|
180
|
+
esmModuleRoots: [new URL('.', sourceUrl).href],
|
|
181
|
+
},
|
|
182
|
+
poolId,
|
|
183
|
+
slotId: 0,
|
|
184
|
+
source: pinned,
|
|
185
|
+
sourceUrl,
|
|
186
|
+
prepareInput: structuredClone(input),
|
|
187
|
+
diagnosticsEnabled: options.diagnosticsEnabled === true,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
try {
|
|
191
|
+
return await new Promise<BehaviorThreadPreparation>((resolve, reject) => {
|
|
192
|
+
let settled = false;
|
|
193
|
+
const finish = (callback: () => void): void => {
|
|
194
|
+
if (settled) return;
|
|
195
|
+
settled = true;
|
|
196
|
+
clearTimeout(timer);
|
|
197
|
+
callback();
|
|
198
|
+
};
|
|
199
|
+
const timer = setTimeout(() => finish(() => reject(
|
|
200
|
+
new Error('behavior_thread_prepare_timeout'),
|
|
201
|
+
)), START_TIMEOUT_MS);
|
|
202
|
+
timer.unref?.();
|
|
203
|
+
worker.on('message', (value: unknown) => {
|
|
204
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return;
|
|
205
|
+
const message = value as WorkerReadyMessage | WorkerErrorMessage;
|
|
206
|
+
if (message.poolId !== poolId || message.slotId !== 0) return;
|
|
207
|
+
if (message.type === 'error') {
|
|
208
|
+
const detail = message.message?.trim().slice(0, MAX_WORKER_ERROR_MESSAGE_LENGTH);
|
|
209
|
+
finish(() => reject(new Error(detail ? `${message.code}: ${detail}` : message.code)));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (
|
|
213
|
+
message.type !== 'ready'
|
|
214
|
+
|| message.behaviorId !== pinned.id
|
|
215
|
+
|| message.execution !== pinned.execution
|
|
216
|
+
|| !message.manifest
|
|
217
|
+
|| !message.input
|
|
218
|
+
) {
|
|
219
|
+
finish(() => reject(new Error('behavior_thread_prepare_invalid')));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
finish(() => resolve({
|
|
223
|
+
manifest: structuredClone(message.manifest!),
|
|
224
|
+
input: structuredClone(message.input!),
|
|
225
|
+
...(message.timing === undefined ? {} : { timing: structuredClone(message.timing) }),
|
|
226
|
+
source: pinned,
|
|
227
|
+
}));
|
|
228
|
+
});
|
|
229
|
+
worker.once('error', (error) => finish(() => reject(error)));
|
|
230
|
+
worker.once('exit', (code) => {
|
|
231
|
+
if (code !== 0) finish(() => reject(new Error(`behavior_thread_worker_exited:${code}`)));
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
} finally {
|
|
235
|
+
await worker.terminate().catch(() => -1);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
132
239
|
/** Adaptive Worker Thread pool for cross-tick computations. */
|
|
133
240
|
export class ThreadBehaviorExecutor {
|
|
134
241
|
readonly runtimeInstanceId = randomUUID();
|
|
135
242
|
|
|
136
243
|
private readonly maxWorkerCount: number;
|
|
244
|
+
private readonly maxPendingRequestCount: number;
|
|
137
245
|
private readonly minWorkerCount: number;
|
|
138
246
|
private readonly idleWorkerTimeoutMs: number;
|
|
139
247
|
private readonly maxOldGenerationSizeMb: number;
|
|
@@ -151,6 +259,7 @@ export class ThreadBehaviorExecutor {
|
|
|
151
259
|
private nextSlotId = 1;
|
|
152
260
|
private startedWorkerCount = 0;
|
|
153
261
|
private retiredWorkerCount = 0;
|
|
262
|
+
private peakWorkerHeapUsedBytes = 0;
|
|
154
263
|
private desiredWorkerCount: number;
|
|
155
264
|
|
|
156
265
|
private readonly options: ThreadBehaviorExecutorOptions;
|
|
@@ -160,10 +269,10 @@ export class ThreadBehaviorExecutor {
|
|
|
160
269
|
...options,
|
|
161
270
|
source: pinBehaviorSource(options.source),
|
|
162
271
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
272
|
+
const execution = options.source.execution ?? 'parallel';
|
|
273
|
+
const maxWorkerCount = execution === 'serial'
|
|
274
|
+
? 1
|
|
275
|
+
: options.poolSize ?? PARALLEL_BEHAVIOR_THREAD_POOL_SIZE;
|
|
167
276
|
if (!Number.isInteger(maxWorkerCount) || maxWorkerCount <= 0 || maxWorkerCount > MAX_POOL_SIZE) {
|
|
168
277
|
throw new TypeError(`poolSize must be between 1 and ${MAX_POOL_SIZE}.`);
|
|
169
278
|
}
|
|
@@ -172,6 +281,9 @@ export class ThreadBehaviorExecutor {
|
|
|
172
281
|
throw new TypeError('idleWorkerTimeoutMs must be a non-negative finite number.');
|
|
173
282
|
}
|
|
174
283
|
this.maxWorkerCount = maxWorkerCount;
|
|
284
|
+
this.maxPendingRequestCount = execution === 'serial'
|
|
285
|
+
? PARALLEL_BEHAVIOR_THREAD_POOL_SIZE
|
|
286
|
+
: maxWorkerCount;
|
|
175
287
|
this.minWorkerCount = Math.min(INITIAL_WORKER_COUNT, maxWorkerCount);
|
|
176
288
|
this.desiredWorkerCount = this.minWorkerCount;
|
|
177
289
|
this.idleWorkerTimeoutMs = idleWorkerTimeoutMs;
|
|
@@ -208,8 +320,13 @@ export class ThreadBehaviorExecutor {
|
|
|
208
320
|
worker_count: this.slots.size,
|
|
209
321
|
min_worker_count: this.minWorkerCount,
|
|
210
322
|
max_worker_count: this.maxWorkerCount,
|
|
323
|
+
worker_max_old_generation_mb: this.maxOldGenerationSizeMb,
|
|
211
324
|
started_worker_count: this.startedWorkerCount,
|
|
212
325
|
retired_worker_count: this.retiredWorkerCount,
|
|
326
|
+
...(this.options.diagnosticsEnabled === true ? {
|
|
327
|
+
worker_heap_used_bytes: this.currentWorkerHeapUsedBytes(),
|
|
328
|
+
peak_worker_heap_used_bytes: this.peakWorkerHeapUsedBytes,
|
|
329
|
+
} : {}),
|
|
213
330
|
duration_ms: roundedElapsed(startedAt),
|
|
214
331
|
}, 'info');
|
|
215
332
|
}).catch(async (error) => {
|
|
@@ -269,6 +386,10 @@ export class ThreadBehaviorExecutor {
|
|
|
269
386
|
max_worker_count: this.maxWorkerCount,
|
|
270
387
|
started_worker_count: this.startedWorkerCount,
|
|
271
388
|
retired_worker_count: this.retiredWorkerCount,
|
|
389
|
+
...(this.options.diagnosticsEnabled === true ? {
|
|
390
|
+
worker_heap_used_bytes: this.currentWorkerHeapUsedBytes(),
|
|
391
|
+
peak_worker_heap_used_bytes: this.peakWorkerHeapUsedBytes,
|
|
392
|
+
} : {}),
|
|
272
393
|
}, 'info');
|
|
273
394
|
}
|
|
274
395
|
|
|
@@ -283,6 +404,8 @@ export class ThreadBehaviorExecutor {
|
|
|
283
404
|
inFlightRequestCount: this.inFlight.size,
|
|
284
405
|
startedWorkerCount: this.startedWorkerCount,
|
|
285
406
|
retiredWorkerCount: this.retiredWorkerCount,
|
|
407
|
+
workerHeapUsedBytes: this.currentWorkerHeapUsedBytes(),
|
|
408
|
+
peakWorkerHeapUsedBytes: this.peakWorkerHeapUsedBytes,
|
|
286
409
|
};
|
|
287
410
|
}
|
|
288
411
|
|
|
@@ -305,10 +428,9 @@ export class ThreadBehaviorExecutor {
|
|
|
305
428
|
if (signal.aborted) throw abortError(signal);
|
|
306
429
|
await this.awaitStart(signal);
|
|
307
430
|
if (this.state !== 'running') throw new Error('behavior_thread_pool_not_running');
|
|
308
|
-
if (
|
|
309
|
-
throw new Error('
|
|
431
|
+
if (this.inFlight.size >= this.maxPendingRequestCount) {
|
|
432
|
+
throw new Error('behavior_thread_window_full');
|
|
310
433
|
}
|
|
311
|
-
if (this.inFlight.size >= this.maxWorkerCount) throw new Error('behavior_thread_window_full');
|
|
312
434
|
return new Promise<unknown>((resolve, reject) => {
|
|
313
435
|
const request: PendingRequest = {
|
|
314
436
|
requestId: randomUUID(),
|
|
@@ -363,6 +485,7 @@ export class ThreadBehaviorExecutor {
|
|
|
363
485
|
slotId,
|
|
364
486
|
source,
|
|
365
487
|
sourceUrl,
|
|
488
|
+
diagnosticsEnabled: this.options.diagnosticsEnabled === true,
|
|
366
489
|
},
|
|
367
490
|
});
|
|
368
491
|
const slot: WorkerSlot = { id: slotId, worker, state: 'starting' };
|
|
@@ -467,14 +590,22 @@ export class ThreadBehaviorExecutor {
|
|
|
467
590
|
slot.state !== 'starting'
|
|
468
591
|
|| message.behaviorId !== this.options.source.id
|
|
469
592
|
|| message.execution !== this.options.source.execution
|
|
593
|
+
|| (this.options.diagnosticsEnabled === true && (
|
|
594
|
+
!Number.isFinite(message.heapUsedBytes)
|
|
595
|
+
|| Number(message.heapUsedBytes) < 0
|
|
596
|
+
))
|
|
470
597
|
) {
|
|
471
598
|
this.failSlot(slot, new Error('behavior_thread_ready_invalid'));
|
|
472
599
|
return;
|
|
473
600
|
}
|
|
601
|
+
if (typeof message.heapUsedBytes === 'number') this.updateWorkerHeap(slot, message.heapUsedBytes);
|
|
474
602
|
slot.readyResolve?.();
|
|
475
603
|
return;
|
|
476
604
|
}
|
|
477
605
|
if (message.type === 'error') {
|
|
606
|
+
if (Number.isFinite(message.heapUsedBytes) && Number(message.heapUsedBytes) >= 0) {
|
|
607
|
+
this.updateWorkerHeap(slot, Number(message.heapUsedBytes));
|
|
608
|
+
}
|
|
478
609
|
const code = typeof message.code === 'string'
|
|
479
610
|
? message.code
|
|
480
611
|
: 'behavior_thread_worker_failed';
|
|
@@ -500,6 +631,15 @@ export class ThreadBehaviorExecutor {
|
|
|
500
631
|
void this.retireSlot(slot, 'invalid_response');
|
|
501
632
|
return;
|
|
502
633
|
}
|
|
634
|
+
if (this.options.diagnosticsEnabled === true && (
|
|
635
|
+
!Number.isFinite(message.heapUsedBytes)
|
|
636
|
+
|| Number(message.heapUsedBytes) < 0
|
|
637
|
+
)) {
|
|
638
|
+
this.rejectRequest(request, new Error('behavior_thread_worker_memory_invalid'));
|
|
639
|
+
void this.retireSlot(slot, 'invalid_response');
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
if (typeof message.heapUsedBytes === 'number') this.updateWorkerHeap(slot, message.heapUsedBytes);
|
|
503
643
|
slot.current = undefined;
|
|
504
644
|
slot.state = 'idle';
|
|
505
645
|
slot.idleSince = performance.now();
|
|
@@ -529,6 +669,11 @@ export class ThreadBehaviorExecutor {
|
|
|
529
669
|
) / 1_000,
|
|
530
670
|
worker_duration_ms: message.durationMs,
|
|
531
671
|
read_query_count: message.queryCount,
|
|
672
|
+
...(this.options.diagnosticsEnabled === true ? {
|
|
673
|
+
worker_heap_used_bytes: message.heapUsedBytes,
|
|
674
|
+
worker_pool_heap_used_bytes: this.currentWorkerHeapUsedBytes(),
|
|
675
|
+
peak_worker_pool_heap_used_bytes: this.peakWorkerHeapUsedBytes,
|
|
676
|
+
} : {}),
|
|
532
677
|
}, 'debug');
|
|
533
678
|
request.resolve(value);
|
|
534
679
|
this.scheduleIdleScaleDown();
|
|
@@ -768,6 +913,21 @@ export class ThreadBehaviorExecutor {
|
|
|
768
913
|
return [...this.slots].filter((slot) => slot.state !== 'retiring').length;
|
|
769
914
|
}
|
|
770
915
|
|
|
916
|
+
private currentWorkerHeapUsedBytes(): number {
|
|
917
|
+
return [...this.slots].reduce(
|
|
918
|
+
(total, slot) => total + (slot.state === 'retiring' ? 0 : slot.heapUsedBytes ?? 0),
|
|
919
|
+
0,
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
private updateWorkerHeap(slot: WorkerSlot, heapUsedBytes: number): void {
|
|
924
|
+
slot.heapUsedBytes = Math.max(0, heapUsedBytes);
|
|
925
|
+
this.peakWorkerHeapUsedBytes = Math.max(
|
|
926
|
+
this.peakWorkerHeapUsedBytes,
|
|
927
|
+
this.currentWorkerHeapUsedBytes(),
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
|
|
771
931
|
private diagnostic(
|
|
772
932
|
name: string,
|
|
773
933
|
attributes?: Readonly<Record<string, unknown>>,
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type { GameSyncPerceptionReport } from '../../../../game-sync-protocol.js';
|
|
3
|
+
import type { CanonicalEvent } from '../../../../framework/events/index.js';
|
|
4
|
+
import type {
|
|
5
|
+
ClawclawMailboxAdapterDescriptor,
|
|
6
|
+
ClawclawMailboxDriver,
|
|
7
|
+
} from '../mailbox-plugin.js';
|
|
8
|
+
|
|
9
|
+
const GAME_SYNC_PERCEPTION_REPORT_BATCH_SIZE = 20;
|
|
10
|
+
const PERCEPTION_REPORT_UUID_NAMESPACE = Buffer.from(
|
|
11
|
+
'3c45fc594dd54a59a22185085c1096d1',
|
|
12
|
+
'hex',
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
export interface GameSyncPerceptionDescriptor {
|
|
16
|
+
readonly perceptionId: string;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface GameSyncPerceptionReportAdapterOptions {
|
|
21
|
+
readonly send: (reports: readonly GameSyncPerceptionReport[]) => boolean;
|
|
22
|
+
readonly descriptor: (eventType: string) => GameSyncPerceptionDescriptor | undefined;
|
|
23
|
+
readonly producerPluginId: string;
|
|
24
|
+
readonly diagnostic?: (
|
|
25
|
+
name: string,
|
|
26
|
+
attributes?: Readonly<Record<string, unknown>>,
|
|
27
|
+
) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function uuidForEvent(event: CanonicalEvent): string {
|
|
31
|
+
const bytes = createHash('sha1')
|
|
32
|
+
.update(PERCEPTION_REPORT_UUID_NAMESPACE)
|
|
33
|
+
.update(event.sessionId)
|
|
34
|
+
.update('\0')
|
|
35
|
+
.update(event.eventId)
|
|
36
|
+
.digest()
|
|
37
|
+
.subarray(0, 16);
|
|
38
|
+
bytes[6] = (bytes[6]! & 0x0f) | 0x50;
|
|
39
|
+
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
|
|
40
|
+
const hex = bytes.toString('hex');
|
|
41
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function sourceTick(event: CanonicalEvent): number | undefined {
|
|
45
|
+
for (const candidate of [event.payload.to_tick, event.payload.source_tick, event.payload.tick]) {
|
|
46
|
+
if (Number.isSafeInteger(candidate) && Number(candidate) >= 0) return Number(candidate);
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Best-effort Mailbox fan-out to the active GameSync connection. */
|
|
52
|
+
export class GameSyncPerceptionReportAdapter implements ClawclawMailboxDriver {
|
|
53
|
+
readonly adapter: ClawclawMailboxAdapterDescriptor = {
|
|
54
|
+
id: 'clawclaw.mailbox.adapter.game-sync-perception-report',
|
|
55
|
+
acknowledgement: 'none',
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
private stopped = false;
|
|
59
|
+
|
|
60
|
+
constructor(private readonly options: GameSyncPerceptionReportAdapterOptions) {}
|
|
61
|
+
|
|
62
|
+
observeEvents(events: readonly CanonicalEvent[]): void {
|
|
63
|
+
if (this.stopped) return;
|
|
64
|
+
let skipped = 0;
|
|
65
|
+
const reports: GameSyncPerceptionReport[] = [];
|
|
66
|
+
for (const event of events) {
|
|
67
|
+
if (
|
|
68
|
+
event.producerPluginId !== this.options.producerPluginId
|
|
69
|
+
|| event.deliveryClass === 'internal_only'
|
|
70
|
+
) continue;
|
|
71
|
+
const descriptor = this.options.descriptor(event.type);
|
|
72
|
+
const output = typeof event.payload.text === 'string' ? event.payload.text.trim() : '';
|
|
73
|
+
const tick = sourceTick(event);
|
|
74
|
+
if (!descriptor || !descriptor.name.trim() || !output || tick === undefined) {
|
|
75
|
+
skipped += 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
reports.push({
|
|
79
|
+
id: uuidForEvent(event),
|
|
80
|
+
name: descriptor.name.trim(),
|
|
81
|
+
output,
|
|
82
|
+
source_tick: tick,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (skipped > 0) {
|
|
86
|
+
this.options.diagnostic?.('runtime.mailbox.perception_report_skipped', {
|
|
87
|
+
event_count: skipped,
|
|
88
|
+
error_code: 'PERCEPTION_REPORT_FIELDS_MISSING',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
for (let index = 0; index < reports.length; index += GAME_SYNC_PERCEPTION_REPORT_BATCH_SIZE) {
|
|
92
|
+
const batch = reports.slice(index, index + GAME_SYNC_PERCEPTION_REPORT_BATCH_SIZE);
|
|
93
|
+
if (!this.options.send(batch)) {
|
|
94
|
+
this.options.diagnostic?.('runtime.mailbox.perception_report_dropped', {
|
|
95
|
+
event_count: batch.length,
|
|
96
|
+
error_code: 'GAME_SYNC_NOT_CONNECTED',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async flush(): Promise<void> {}
|
|
103
|
+
|
|
104
|
+
async stop(): Promise<void> {
|
|
105
|
+
this.stopped = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './agent-mailbox-coordinator.js';
|
|
2
2
|
export * from './adapters/claude-channel-adapter.js';
|
|
3
3
|
export * from './adapters/claude-channel-socket.js';
|
|
4
|
+
export * from './adapters/game-sync-perception-report-adapter.js';
|
|
4
5
|
export * from './consumer-ids.js';
|
|
5
6
|
export * from './desktop-event-socket.js';
|
|
6
7
|
export * from './mailbox-event-batch-delivery.js';
|