@myclaw163/clawclaw-cli 0.8.13 → 0.8.15
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/skills/clawclaw/SKILL.md +8 -4
- package/skills/clawclaw/references/BEHAVIORS.md +16 -1
- package/skills/clawclaw/references/CHANNELS.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +2 -2
- 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/STREAM.md +4 -2
- package/src/lib/game-client.ts +12 -4
- package/src/pipeline/event-format.ts +5 -0
- package/src/pipeline/event-hints.ts +10 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +2 -0
- package/src/runtime/clawclaw/behavior-decision.ts +4 -1
- package/src/runtime/clawclaw/behavior-input.ts +5 -0
- package/src/runtime/clawclaw/owner.ts +64 -1
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +150 -18
- package/src/runtime/clawclaw/plugins/assembly.ts +4 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +8 -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-decision-history.ts +88 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +3 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +201 -43
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +45 -9
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +170 -8
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +5 -1
- 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 +33 -9
- 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 +327 -23
- 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/source/game-transport.ts +29 -4
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +153 -0
- package/src/runtime/clawclaw/plugins/source/types.ts +1 -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/runtime.ts +31 -3
- package/src/runtime/clawclaw-event-catalog.ts +3 -0
- package/src/runtime/framework/events/event-repository.ts +1 -1
- package/src/runtime/framework/events/memory-event-repository.ts +71 -1
- package/src/runtime/framework/events/sqlite-event-repository.ts +23 -1
- package/src/runtime/framework/mailbox/types.ts +2 -0
- package/src/runtime/framework/modules/runtime-module-output.ts +24 -0
- package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +58 -0
- package/src/runtime/framework/persistence/memory-first-persistence.ts +383 -0
- package/src/runtime/framework/plugins/contracts.ts +5 -2
- package/src/runtime/framework/plugins/runtime-input-journal.ts +185 -17
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +173 -34
- package/src/runtime/framework/runtime.ts +75 -4
- package/src/runtime/framework/state/history/contracts.ts +9 -1
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +59 -7
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +16 -0
- package/src/runtime/observability/redaction.ts +28 -1
- package/src/sdk/behavior-plugin/index.ts +5 -24
- package/src/sdk/module-input/index.ts +47 -0
- package/src/sdk/perception-plugin/index.ts +13 -8
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
PerceptionModuleLoader,
|
|
45
45
|
type PerceptionThreadModuleSpec,
|
|
46
46
|
} from './perception-module-loader.js';
|
|
47
|
+
import { PerceptionReducerThreadPool } from './perception-reducer-thread-pool.js';
|
|
47
48
|
import {
|
|
48
49
|
CLAWCLAW_PERCEPTION_SPATIAL_API,
|
|
49
50
|
CLAWCLAW_PERCEPTION_SPATIAL_FACTS,
|
|
@@ -66,6 +67,8 @@ export interface ClawclawPerceptionConfig {
|
|
|
66
67
|
moduleSources?: readonly ResolvedClawclawPerceptionSource[];
|
|
67
68
|
/** Maximum visual frames replayed into modules during one live Runtime invocation. */
|
|
68
69
|
maxBackfillVisualFrames?: number;
|
|
70
|
+
/** Samples Worker heaps only for explicit full diagnostics. */
|
|
71
|
+
diagnosticsEnabled?: boolean;
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
export interface PerceptionModuleDescriptor extends ClawclawPerceptionManifest {
|
|
@@ -133,6 +136,45 @@ interface PreparedPerceptionFrame {
|
|
|
133
136
|
readonly stored: StoredStateFrame<Record<string, any>>;
|
|
134
137
|
readonly visual: PerceptionFrame | null;
|
|
135
138
|
readonly analyses: readonly PerceptionModuleAnalysis[];
|
|
139
|
+
readonly analysisTiming: PerceptionAnalysisTiming;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface PerceptionAnalysisTiming {
|
|
143
|
+
readonly workerQueueMs: number;
|
|
144
|
+
readonly workerRoundtripMs: number;
|
|
145
|
+
readonly moduleCpuSumMs: number;
|
|
146
|
+
readonly moduleCpuMaxMs: number;
|
|
147
|
+
readonly workerCount: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
interface PerceptionFrameComputationTiming extends PerceptionAnalysisTiming {
|
|
151
|
+
readonly projectionMs: number;
|
|
152
|
+
readonly historyPrepareMs: number;
|
|
153
|
+
readonly updateTotalMs: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface PerceptionFrameTimingWindow {
|
|
157
|
+
readonly startedAtMs: number;
|
|
158
|
+
readonly projectionMs: number[];
|
|
159
|
+
readonly historyPrepareMs: number[];
|
|
160
|
+
readonly workerQueueMs: number[];
|
|
161
|
+
readonly workerRoundtripMs: number[];
|
|
162
|
+
readonly moduleCpuSumMs: number[];
|
|
163
|
+
readonly moduleCpuMaxMs: number[];
|
|
164
|
+
readonly updateTotalMs: number[];
|
|
165
|
+
readonly commitMs: number[];
|
|
166
|
+
readonly totalMs: number[];
|
|
167
|
+
workerCountMax: number;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface PerceptionFrameTimingSample extends PerceptionFrameComputationTiming {
|
|
171
|
+
readonly commitMs: number;
|
|
172
|
+
readonly totalMs: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
interface CachedProjectedFrame {
|
|
176
|
+
readonly stored: Pick<StoredStateFrame<Record<string, any>>, 'frameSeq' | 'connectionId'>;
|
|
177
|
+
readonly visual: PerceptionFrame;
|
|
136
178
|
}
|
|
137
179
|
|
|
138
180
|
interface PerceptionModuleAnalysis {
|
|
@@ -152,6 +194,7 @@ interface PerceptionComputationProposal {
|
|
|
152
194
|
readonly schema: 'clawclaw.perception-computation.v1';
|
|
153
195
|
readonly visual: PerceptionFrame | null;
|
|
154
196
|
readonly analyses: readonly PerceptionModuleAnalysis[];
|
|
197
|
+
readonly timing?: PerceptionFrameComputationTiming;
|
|
155
198
|
}
|
|
156
199
|
|
|
157
200
|
interface PerceptionCheckpoint {
|
|
@@ -270,26 +313,43 @@ export function validatePerceptionManifest(
|
|
|
270
313
|
function createModuleSlots(
|
|
271
314
|
config: ClawclawPerceptionConfig,
|
|
272
315
|
loader: PerceptionModuleLoader,
|
|
273
|
-
):
|
|
316
|
+
): Readonly<{
|
|
317
|
+
modules: PerceptionModuleSlot[];
|
|
318
|
+
reducerPool?: PerceptionReducerThreadPool;
|
|
319
|
+
}> {
|
|
274
320
|
const ids = [...new Set(config.modules ?? DEFAULT_PERCEPTION_MODULES)].sort();
|
|
275
321
|
const sources = new Map(
|
|
276
322
|
(config.moduleSources ?? listClawclawPerceptionSources(config.workspaceDir))
|
|
277
323
|
.map((source) => [source.id, source]),
|
|
278
324
|
);
|
|
279
|
-
|
|
325
|
+
const selected = ids.map((id) => {
|
|
280
326
|
const source = sources.get(id);
|
|
281
327
|
if (!source) throw new Error(`Unknown perception module '${id}'.`);
|
|
282
|
-
|
|
283
|
-
return {
|
|
284
|
-
manifest: validatePerceptionManifest(runner.manifest),
|
|
285
|
-
runner,
|
|
286
|
-
source,
|
|
287
|
-
analysisGeneration: 0,
|
|
288
|
-
analysisHistoryFloorFrameSeq: 0,
|
|
289
|
-
analysisBaselinePending: false,
|
|
290
|
-
reducerReplayFromFrameSeq: undefined,
|
|
291
|
-
};
|
|
328
|
+
return source;
|
|
292
329
|
});
|
|
330
|
+
const reducerPool = selected.length > 0
|
|
331
|
+
? new PerceptionReducerThreadPool({
|
|
332
|
+
modules: selected.map((source) => loader.threadSpec(source)),
|
|
333
|
+
loader: loader.threadConfig(),
|
|
334
|
+
spatialFacts: CLAWCLAW_PERCEPTION_SPATIAL_FACTS satisfies PerceptionSpatialFacts,
|
|
335
|
+
diagnosticsEnabled: config.diagnosticsEnabled === true,
|
|
336
|
+
})
|
|
337
|
+
: undefined;
|
|
338
|
+
return {
|
|
339
|
+
reducerPool,
|
|
340
|
+
modules: selected.map((source) => {
|
|
341
|
+
const runner = new ImportedLocalPerceptionModuleRunner(source, loader, reducerPool);
|
|
342
|
+
return {
|
|
343
|
+
manifest: validatePerceptionManifest(runner.manifest),
|
|
344
|
+
runner,
|
|
345
|
+
source,
|
|
346
|
+
analysisGeneration: 0,
|
|
347
|
+
analysisHistoryFloorFrameSeq: 0,
|
|
348
|
+
analysisBaselinePending: false,
|
|
349
|
+
reducerReplayFromFrameSeq: undefined,
|
|
350
|
+
};
|
|
351
|
+
}),
|
|
352
|
+
};
|
|
293
353
|
}
|
|
294
354
|
|
|
295
355
|
function proposals(value: readonly PerceptionEventProposal[] | void): RuntimeEventProposal[] {
|
|
@@ -311,13 +371,16 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
311
371
|
: 'every_epoch';
|
|
312
372
|
}
|
|
313
373
|
readonly maxInFlight: number;
|
|
374
|
+
readonly maxPendingEpochs = 10;
|
|
314
375
|
readonly recoveryTimeoutMs = DEFAULT_PERCEPTION_RECOVERY_TIMEOUT_MS;
|
|
315
376
|
readonly produces: { readonly events: readonly string[] };
|
|
316
377
|
|
|
317
378
|
private readonly configuredModules: readonly PerceptionModuleSlot[];
|
|
379
|
+
private readonly reducerPool?: PerceptionReducerThreadPool;
|
|
318
380
|
/** Modules that completed every startup stage for the current lifecycle. */
|
|
319
381
|
private modules: readonly PerceptionModuleSlot[];
|
|
320
382
|
private readonly maxBackfillVisualFrames: number;
|
|
383
|
+
private readonly diagnosticsEnabled: boolean;
|
|
321
384
|
private readonly moduleLoader = new PerceptionModuleLoader();
|
|
322
385
|
private analysisPool?: PerceptionAnalysisThreadPool;
|
|
323
386
|
private readonly projector = new ObservationFrameProjector();
|
|
@@ -331,14 +394,18 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
331
394
|
private lastVisualFrame: PerceptionFrame | null = null;
|
|
332
395
|
private lastConnectionId: string | null = null;
|
|
333
396
|
private readonly analysisWindow: PerceptionFrame[] = [];
|
|
397
|
+
/** Bounded projection cache shared by overlapping epoch analyses. */
|
|
398
|
+
private readonly projectedFrameCache = new Map<number, CachedProjectedFrame>();
|
|
334
399
|
private replayFromFrameSeq: number | null = null;
|
|
335
400
|
private readonly moduleFailureReports = new Map<string, {
|
|
336
401
|
lastReportedAtMs: number;
|
|
337
402
|
failureCount: number;
|
|
338
403
|
suppressedCount: number;
|
|
339
404
|
}>();
|
|
405
|
+
private readonly disabledModuleIds = new Set<string>();
|
|
340
406
|
private readonly moduleTimingWindows = new Map<string, PerceptionModuleTimingWindow>();
|
|
341
407
|
private moduleTimingWindowStartedAt = performance.now();
|
|
408
|
+
private frameTimingWindow: PerceptionFrameTimingWindow = this.newFrameTimingWindow();
|
|
342
409
|
private runnersStarted = false;
|
|
343
410
|
private runnersStartTask?: Promise<void>;
|
|
344
411
|
private runnersStartGeneration = 0;
|
|
@@ -356,7 +423,10 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
356
423
|
throw new TypeError('maxBackfillVisualFrames must be an integer between 1 and 10.');
|
|
357
424
|
}
|
|
358
425
|
this.maxBackfillVisualFrames = maxBackfillVisualFrames;
|
|
359
|
-
this.
|
|
426
|
+
this.diagnosticsEnabled = config.diagnosticsEnabled === true;
|
|
427
|
+
const configured = createModuleSlots(config, this.moduleLoader);
|
|
428
|
+
this.configuredModules = configured.modules;
|
|
429
|
+
this.reducerPool = configured.reducerPool;
|
|
360
430
|
// Keep configured descriptors available before the first Runtime start.
|
|
361
431
|
// Once startup settles, this is narrowed to only successfully active modules.
|
|
362
432
|
this.modules = this.configuredModules;
|
|
@@ -405,15 +475,26 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
405
475
|
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
406
476
|
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
407
477
|
): Promise<RuntimeEpochPluginResult> {
|
|
478
|
+
const updateStartedAt = performance.now();
|
|
479
|
+
const projectionStartedAt = performance.now();
|
|
408
480
|
const visual = this.projector.project(input.frame);
|
|
481
|
+
const projectionMs = elapsedMs(projectionStartedAt);
|
|
482
|
+
const historyStartedAt = performance.now();
|
|
409
483
|
const analysisInput = visual && this.shouldAnalyze(input.frame)
|
|
410
484
|
? this.analysisInputFromHistory(input.frame, visual, context)
|
|
411
485
|
: null;
|
|
486
|
+
const historyPrepareMs = elapsedMs(historyStartedAt);
|
|
412
487
|
const prepared = await this.prepareFrame(input.frame, visual, context.signal, analysisInput);
|
|
413
488
|
const proposal: PerceptionComputationProposal = {
|
|
414
489
|
schema: PERCEPTION_COMPUTATION_SCHEMA,
|
|
415
490
|
visual: prepared.visual,
|
|
416
491
|
analyses: prepared.analyses,
|
|
492
|
+
timing: {
|
|
493
|
+
projectionMs,
|
|
494
|
+
historyPrepareMs,
|
|
495
|
+
...prepared.analysisTiming,
|
|
496
|
+
updateTotalMs: elapsedMs(updateStartedAt),
|
|
497
|
+
},
|
|
417
498
|
};
|
|
418
499
|
return {
|
|
419
500
|
outcome: 'processed',
|
|
@@ -428,11 +509,13 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
428
509
|
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
429
510
|
): Promise<RuntimeEpochPluginResult> {
|
|
430
511
|
const proposal = this.computationProposal(computation.proposal);
|
|
512
|
+
const commitStartedAt = performance.now();
|
|
431
513
|
const previousFrameSeq = this.lastFrameSeq;
|
|
432
514
|
const inspected = await this.inspectPreparedFrame({
|
|
433
515
|
stored: input.frame,
|
|
434
516
|
visual: proposal.visual,
|
|
435
517
|
analyses: proposal.analyses,
|
|
518
|
+
analysisTiming: proposal.timing ?? this.emptyAnalysisTiming(),
|
|
436
519
|
}, context.signal, context.diagnostic);
|
|
437
520
|
if (input.frame.frameSeq > previousFrameSeq + 1) {
|
|
438
521
|
context.diagnostic('perception.catchup_frames_dropped', {
|
|
@@ -446,6 +529,14 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
446
529
|
});
|
|
447
530
|
}
|
|
448
531
|
const checkpoint = this.checkpointValue();
|
|
532
|
+
const commitMs = elapsedMs(commitStartedAt);
|
|
533
|
+
if (proposal.timing) {
|
|
534
|
+
this.recordFrameTiming({
|
|
535
|
+
...proposal.timing,
|
|
536
|
+
commitMs,
|
|
537
|
+
totalMs: proposal.timing.updateTotalMs + commitMs,
|
|
538
|
+
}, context.diagnostic);
|
|
539
|
+
}
|
|
449
540
|
return inspected.visual || inspected.proposals.length > 0
|
|
450
541
|
? {
|
|
451
542
|
outcome: 'processed',
|
|
@@ -585,7 +676,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
585
676
|
// Close every module's old slice before it receives the new phase/gap frame.
|
|
586
677
|
if (gap || phaseChanged) {
|
|
587
678
|
const kind: PerceptionBoundaryKind = gap ? 'trajectory_gap' : 'phase_changed';
|
|
588
|
-
emitted.push(...await this.flushModules(kind, 'close', signal, diagnostic));
|
|
679
|
+
emitted.push(...await this.flushModules(kind, 'close', signal, diagnostic, projected));
|
|
589
680
|
await this.resetModules(
|
|
590
681
|
kind,
|
|
591
682
|
signal,
|
|
@@ -634,6 +725,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
634
725
|
this.lastFrameSeq + 1,
|
|
635
726
|
);
|
|
636
727
|
this.flushModuleTimingWindows(context.diagnostic, true);
|
|
728
|
+
this.flushFrameTimingWindow(context.diagnostic, true);
|
|
637
729
|
}
|
|
638
730
|
|
|
639
731
|
stop(reason: 'segment_ending' | 'shutdown'): Promise<void> {
|
|
@@ -652,14 +744,17 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
652
744
|
const analysisPool = this.analysisPool;
|
|
653
745
|
this.analysisPool = undefined;
|
|
654
746
|
await analysisPool?.stop();
|
|
655
|
-
// This includes
|
|
656
|
-
// generation fence prevents a late
|
|
747
|
+
// This includes reducer Workers that are still starting; their own
|
|
748
|
+
// generation fence prevents a late ready result from resurrecting them.
|
|
657
749
|
await Promise.allSettled(this.configuredModules.map(({ runner }) => runner.stop(reason)));
|
|
750
|
+
await this.reducerPool?.stop();
|
|
658
751
|
await this.moduleLoader.stop();
|
|
659
752
|
this.modules = [];
|
|
660
753
|
this.moduleFailureReports.clear();
|
|
754
|
+
this.disabledModuleIds.clear();
|
|
661
755
|
this.moduleTimingWindows.clear();
|
|
662
756
|
this.moduleTimingWindowStartedAt = performance.now();
|
|
757
|
+
this.frameTimingWindow = this.newFrameTimingWindow();
|
|
663
758
|
this.resetProjection();
|
|
664
759
|
}
|
|
665
760
|
|
|
@@ -680,6 +775,48 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
680
775
|
}));
|
|
681
776
|
}
|
|
682
777
|
|
|
778
|
+
diagnosticSnapshot(): Readonly<Record<string, unknown>> {
|
|
779
|
+
const pool = this.analysisPool?.snapshot();
|
|
780
|
+
const reducerPool = this.reducerPool?.snapshot();
|
|
781
|
+
return Object.freeze({
|
|
782
|
+
configured_module_count: this.configuredModules.length,
|
|
783
|
+
active_module_count: this.modules.length,
|
|
784
|
+
active_module_ids: this.modules.map(({ manifest }) => manifest.id),
|
|
785
|
+
analysis_pool: pool
|
|
786
|
+
? {
|
|
787
|
+
state: pool.state,
|
|
788
|
+
min_worker_count: pool.minWorkers,
|
|
789
|
+
max_worker_count: pool.maxWorkers,
|
|
790
|
+
worker_count: pool.workerCount,
|
|
791
|
+
idle_worker_count: pool.idleWorkerCount,
|
|
792
|
+
busy_worker_count: pool.busyWorkerCount,
|
|
793
|
+
queued_batch_count: pool.queuedBatchCount,
|
|
794
|
+
queued_job_count: pool.queuedJobCount,
|
|
795
|
+
in_flight_batch_count: pool.inFlightBatchCount,
|
|
796
|
+
started_worker_count: pool.startedWorkerCount,
|
|
797
|
+
retired_worker_count: pool.retiredWorkerCount,
|
|
798
|
+
worker_heap_used_bytes: pool.workerHeapUsedBytes,
|
|
799
|
+
peak_worker_heap_used_bytes: pool.peakWorkerHeapUsedBytes,
|
|
800
|
+
}
|
|
801
|
+
: null,
|
|
802
|
+
reducer_pool: reducerPool
|
|
803
|
+
? {
|
|
804
|
+
state: reducerPool.state,
|
|
805
|
+
max_worker_count: reducerPool.maxWorkerCount,
|
|
806
|
+
worker_count: reducerPool.workerCount,
|
|
807
|
+
busy_worker_count: reducerPool.busyWorkerCount,
|
|
808
|
+
queued_request_count: reducerPool.queuedRequestCount,
|
|
809
|
+
in_flight_request_count: reducerPool.inFlightRequestCount,
|
|
810
|
+
active_module_count: reducerPool.activeModuleCount,
|
|
811
|
+
unavailable_module_ids: reducerPool.unavailableModuleIds,
|
|
812
|
+
restarted_worker_count: reducerPool.restartedWorkerCount,
|
|
813
|
+
worker_heap_used_bytes: reducerPool.workerHeapUsedBytes,
|
|
814
|
+
peak_worker_heap_used_bytes: reducerPool.peakWorkerHeapUsedBytes,
|
|
815
|
+
}
|
|
816
|
+
: null,
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
|
|
683
820
|
localizedModuleDescriptors(locale: string): ClawclawResolvedResourceMetadata[] {
|
|
684
821
|
return this.modules.map(({ manifest }) => resolveClawclawResourceMetadata(manifest, locale));
|
|
685
822
|
}
|
|
@@ -772,6 +909,18 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
772
909
|
return [];
|
|
773
910
|
}
|
|
774
911
|
if (!analysis || analysis.outcome === 'rejected') {
|
|
912
|
+
const failure = analysis?.outcome === 'rejected' ? analysis.failure : undefined;
|
|
913
|
+
if (
|
|
914
|
+
failure
|
|
915
|
+
&& this.analysisFailureDisablesModule(failure.errorCode)
|
|
916
|
+
&& !this.disabledModuleIds.has(manifest.id)
|
|
917
|
+
) {
|
|
918
|
+
this.disabledModuleIds.add(manifest.id);
|
|
919
|
+
diagnostic?.('perception.module.disabled', {
|
|
920
|
+
module_id: manifest.id,
|
|
921
|
+
reason: failure.errorCode,
|
|
922
|
+
}, 'error');
|
|
923
|
+
}
|
|
775
924
|
await this.invalidateParallelModule(
|
|
776
925
|
module,
|
|
777
926
|
frame.frameSeq + 1,
|
|
@@ -850,7 +999,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
850
999
|
analysisInput: Readonly<PerceptionAnalysisInput> | null = null,
|
|
851
1000
|
): Promise<PreparedPerceptionFrame> {
|
|
852
1001
|
if (!visual || !this.shouldAnalyze(stored)) {
|
|
853
|
-
return { stored, visual, analyses: [] };
|
|
1002
|
+
return { stored, visual, analyses: [], analysisTiming: this.emptyAnalysisTiming() };
|
|
854
1003
|
}
|
|
855
1004
|
const preparedVisual = analysisInput?.frame ?? visual;
|
|
856
1005
|
const analyzerInput = analysisInput ?? this.localAnalysisInput(stored, visual);
|
|
@@ -858,7 +1007,14 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
858
1007
|
module.runner.execution === 'parallel_analysis'
|
|
859
1008
|
&& this.shouldRunParallelModuleForFrame(module, stored.frameSeq)
|
|
860
1009
|
));
|
|
861
|
-
if (parallelModules.length === 0)
|
|
1010
|
+
if (parallelModules.length === 0) {
|
|
1011
|
+
return {
|
|
1012
|
+
stored,
|
|
1013
|
+
visual: preparedVisual,
|
|
1014
|
+
analyses: [],
|
|
1015
|
+
analysisTiming: this.emptyAnalysisTiming(),
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
862
1018
|
const analysisPool = this.analysisPool;
|
|
863
1019
|
if (!analysisPool) throw new Error('perception_analysis_pool_not_started');
|
|
864
1020
|
// Capture generations and module-specific history before submitting one
|
|
@@ -891,7 +1047,19 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
891
1047
|
durationMs: result.durationMs,
|
|
892
1048
|
failure: result.failure,
|
|
893
1049
|
});
|
|
894
|
-
|
|
1050
|
+
const moduleDurations = batch.results.map((result) => result.durationMs);
|
|
1051
|
+
return {
|
|
1052
|
+
stored,
|
|
1053
|
+
visual: preparedVisual,
|
|
1054
|
+
analyses,
|
|
1055
|
+
analysisTiming: {
|
|
1056
|
+
workerQueueMs: batch.queueDurationMs,
|
|
1057
|
+
workerRoundtripMs: Math.max(0, batch.durationMs - batch.queueDurationMs),
|
|
1058
|
+
moduleCpuSumMs: moduleDurations.reduce((sum, duration) => sum + duration, 0),
|
|
1059
|
+
moduleCpuMaxMs: Math.max(0, ...moduleDurations),
|
|
1060
|
+
workerCount: batch.workerCount,
|
|
1061
|
+
},
|
|
1062
|
+
};
|
|
895
1063
|
} catch (error) {
|
|
896
1064
|
const failure = perceptionFailureDetails(error);
|
|
897
1065
|
const durationMs = elapsedMs(startedAt);
|
|
@@ -905,10 +1073,27 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
905
1073
|
durationMs,
|
|
906
1074
|
failure,
|
|
907
1075
|
})),
|
|
1076
|
+
analysisTiming: {
|
|
1077
|
+
workerQueueMs: 0,
|
|
1078
|
+
workerRoundtripMs: durationMs,
|
|
1079
|
+
moduleCpuSumMs: 0,
|
|
1080
|
+
moduleCpuMaxMs: 0,
|
|
1081
|
+
workerCount: analysisPool.snapshot().workerCount,
|
|
1082
|
+
},
|
|
908
1083
|
};
|
|
909
1084
|
}
|
|
910
1085
|
}
|
|
911
1086
|
|
|
1087
|
+
private emptyAnalysisTiming(): PerceptionAnalysisTiming {
|
|
1088
|
+
return {
|
|
1089
|
+
workerQueueMs: 0,
|
|
1090
|
+
workerRoundtripMs: 0,
|
|
1091
|
+
moduleCpuSumMs: 0,
|
|
1092
|
+
moduleCpuMaxMs: 0,
|
|
1093
|
+
workerCount: this.analysisPool?.snapshot().workerCount ?? 0,
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
|
|
912
1097
|
private analysisInputForModule(
|
|
913
1098
|
input: Readonly<PerceptionAnalysisInput>,
|
|
914
1099
|
module: PerceptionModuleSlot,
|
|
@@ -939,7 +1124,14 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
939
1124
|
module: PerceptionModuleSlot,
|
|
940
1125
|
frameSeq: number,
|
|
941
1126
|
): boolean {
|
|
942
|
-
return
|
|
1127
|
+
return !this.disabledModuleIds.has(module.manifest.id)
|
|
1128
|
+
&& frameSeq >= module.analysisHistoryFloorFrameSeq;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
private analysisFailureDisablesModule(errorCode: string): boolean {
|
|
1132
|
+
return errorCode === 'perception_analysis_worker_exited'
|
|
1133
|
+
|| errorCode === 'perception_analysis_worker_memory_invalid'
|
|
1134
|
+
|| errorCode === 'perception_analysis_output_too_large';
|
|
943
1135
|
}
|
|
944
1136
|
|
|
945
1137
|
private analysisInputFromHistory(
|
|
@@ -947,6 +1139,24 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
947
1139
|
currentVisual: PerceptionFrame,
|
|
948
1140
|
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
949
1141
|
): PerceptionAnalysisInput {
|
|
1142
|
+
this.rememberProjectedFrame(stored, currentVisual);
|
|
1143
|
+
const cached = [...this.projectedFrameCache.values()]
|
|
1144
|
+
.filter((value) => value.stored.frameSeq <= stored.frameSeq)
|
|
1145
|
+
.sort((left, right) => left.stored.frameSeq - right.stored.frameSeq)
|
|
1146
|
+
.slice(-(PERCEPTION_ANALYSIS_WINDOW_FRAMES + 1));
|
|
1147
|
+
// The first frame after process recovery deliberately reads durable history
|
|
1148
|
+
// once. Normal overlapping epochs reuse projections already built by the
|
|
1149
|
+
// plugin and avoid re-querying/re-projecting the same ten frames per tick.
|
|
1150
|
+
const cacheIsContinuous = cached.every((value, index) => (
|
|
1151
|
+
index === 0 || value.stored.frameSeq === cached[index - 1]!.stored.frameSeq + 1
|
|
1152
|
+
));
|
|
1153
|
+
if (
|
|
1154
|
+
cached.length > 1
|
|
1155
|
+
&& cacheIsContinuous
|
|
1156
|
+
&& cached.at(-1)?.stored.frameSeq === stored.frameSeq
|
|
1157
|
+
) {
|
|
1158
|
+
return this.enrichAnalysisInput(cached);
|
|
1159
|
+
}
|
|
950
1160
|
const projected = new Map<number, {
|
|
951
1161
|
stored: StoredStateFrame<Record<string, any>>;
|
|
952
1162
|
visual: PerceptionFrame;
|
|
@@ -966,7 +1176,10 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
966
1176
|
const visual = candidate.frameSeq === stored.frameSeq
|
|
967
1177
|
? currentVisual
|
|
968
1178
|
: this.projector.project(candidate);
|
|
969
|
-
if (visual)
|
|
1179
|
+
if (visual) {
|
|
1180
|
+
projected.set(candidate.frameSeq, { stored: candidate, visual });
|
|
1181
|
+
this.rememberProjectedFrame(candidate, visual);
|
|
1182
|
+
}
|
|
970
1183
|
if (projected.size >= PERCEPTION_ANALYSIS_WINDOW_FRAMES + 1) break;
|
|
971
1184
|
}
|
|
972
1185
|
beforeFrameSeq = page.frames.at(-1)!.frameSeq;
|
|
@@ -975,8 +1188,11 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
975
1188
|
if (!projected.has(stored.frameSeq)) {
|
|
976
1189
|
projected.set(stored.frameSeq, { stored, visual: currentVisual });
|
|
977
1190
|
}
|
|
978
|
-
const values = [...projected.values()]
|
|
979
|
-
|
|
1191
|
+
const values = [...projected.values()].sort((left, right) => left.stored.frameSeq - right.stored.frameSeq);
|
|
1192
|
+
return this.enrichAnalysisInput(values);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
private enrichAnalysisInput(values: readonly CachedProjectedFrame[]): PerceptionAnalysisInput {
|
|
980
1196
|
const enriched: PerceptionFrame[] = [];
|
|
981
1197
|
values.forEach((value, index) => {
|
|
982
1198
|
const previous = index > 0 ? values[index - 1] : undefined;
|
|
@@ -1000,6 +1216,18 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1000
1216
|
};
|
|
1001
1217
|
}
|
|
1002
1218
|
|
|
1219
|
+
private rememberProjectedFrame(
|
|
1220
|
+
stored: Pick<StoredStateFrame<Record<string, any>>, 'frameSeq' | 'connectionId'>,
|
|
1221
|
+
visual: PerceptionFrame,
|
|
1222
|
+
): void {
|
|
1223
|
+
this.projectedFrameCache.set(stored.frameSeq, { stored, visual });
|
|
1224
|
+
const maximum = PERCEPTION_ANALYSIS_WINDOW_FRAMES + this.maxInFlight + 2;
|
|
1225
|
+
while (this.projectedFrameCache.size > maximum) {
|
|
1226
|
+
const oldest = Math.min(...this.projectedFrameCache.keys());
|
|
1227
|
+
this.projectedFrameCache.delete(oldest);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1003
1231
|
private localAnalysisInput(
|
|
1004
1232
|
stored: StoredStateFrame<Record<string, any>>,
|
|
1005
1233
|
visual: PerceptionFrame,
|
|
@@ -1072,11 +1300,13 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1072
1300
|
mode: PerceptionBoundary['mode'],
|
|
1073
1301
|
signal: AbortSignal,
|
|
1074
1302
|
diagnostic?: PerceptionDiagnostic,
|
|
1303
|
+
next?: Readonly<PerceptionFrame> | null,
|
|
1075
1304
|
): Promise<RuntimeEventProposal[]> {
|
|
1076
1305
|
const boundary: PerceptionBoundary = Object.freeze({
|
|
1077
1306
|
kind,
|
|
1078
1307
|
mode,
|
|
1079
1308
|
through: this.lastVisualFrame,
|
|
1309
|
+
...(next === undefined ? {} : { next }),
|
|
1080
1310
|
});
|
|
1081
1311
|
const output = await this.collectModuleEvents(
|
|
1082
1312
|
`flush:${kind}`,
|
|
@@ -1214,6 +1444,74 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1214
1444
|
}
|
|
1215
1445
|
}
|
|
1216
1446
|
|
|
1447
|
+
private newFrameTimingWindow(): PerceptionFrameTimingWindow {
|
|
1448
|
+
return {
|
|
1449
|
+
startedAtMs: performance.now(),
|
|
1450
|
+
projectionMs: [],
|
|
1451
|
+
historyPrepareMs: [],
|
|
1452
|
+
workerQueueMs: [],
|
|
1453
|
+
workerRoundtripMs: [],
|
|
1454
|
+
moduleCpuSumMs: [],
|
|
1455
|
+
moduleCpuMaxMs: [],
|
|
1456
|
+
updateTotalMs: [],
|
|
1457
|
+
commitMs: [],
|
|
1458
|
+
totalMs: [],
|
|
1459
|
+
workerCountMax: 0,
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
private recordFrameTiming(
|
|
1464
|
+
sample: PerceptionFrameTimingSample,
|
|
1465
|
+
diagnostic: PerceptionDiagnostic | undefined,
|
|
1466
|
+
): void {
|
|
1467
|
+
const window = this.frameTimingWindow;
|
|
1468
|
+
window.projectionMs.push(sample.projectionMs);
|
|
1469
|
+
window.historyPrepareMs.push(sample.historyPrepareMs);
|
|
1470
|
+
window.workerQueueMs.push(sample.workerQueueMs);
|
|
1471
|
+
window.workerRoundtripMs.push(sample.workerRoundtripMs);
|
|
1472
|
+
window.moduleCpuSumMs.push(sample.moduleCpuSumMs);
|
|
1473
|
+
window.moduleCpuMaxMs.push(sample.moduleCpuMaxMs);
|
|
1474
|
+
window.updateTotalMs.push(sample.updateTotalMs);
|
|
1475
|
+
window.commitMs.push(sample.commitMs);
|
|
1476
|
+
window.totalMs.push(sample.totalMs);
|
|
1477
|
+
window.workerCountMax = Math.max(window.workerCountMax, sample.workerCount);
|
|
1478
|
+
this.flushFrameTimingWindow(diagnostic);
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
private flushFrameTimingWindow(
|
|
1482
|
+
diagnostic: PerceptionDiagnostic | undefined,
|
|
1483
|
+
force = false,
|
|
1484
|
+
): void {
|
|
1485
|
+
const window = this.frameTimingWindow;
|
|
1486
|
+
if (!diagnostic || window.totalMs.length === 0) return;
|
|
1487
|
+
const windowMs = Math.max(0, performance.now() - window.startedAtMs);
|
|
1488
|
+
if (!force && windowMs < PERCEPTION_MODULE_TIMING_WINDOW_MS) return;
|
|
1489
|
+
diagnostic('perception.frame.timing_window', {
|
|
1490
|
+
window_ms: Math.round(windowMs),
|
|
1491
|
+
count: window.totalMs.length,
|
|
1492
|
+
projection_avg_ms: roundedAverage(window.projectionMs),
|
|
1493
|
+
projection_p95_ms: percentile95(window.projectionMs),
|
|
1494
|
+
history_prepare_avg_ms: roundedAverage(window.historyPrepareMs),
|
|
1495
|
+
history_prepare_p95_ms: percentile95(window.historyPrepareMs),
|
|
1496
|
+
worker_queue_avg_ms: roundedAverage(window.workerQueueMs),
|
|
1497
|
+
worker_queue_p95_ms: percentile95(window.workerQueueMs),
|
|
1498
|
+
worker_roundtrip_avg_ms: roundedAverage(window.workerRoundtripMs),
|
|
1499
|
+
worker_roundtrip_p95_ms: percentile95(window.workerRoundtripMs),
|
|
1500
|
+
module_cpu_sum_avg_ms: roundedAverage(window.moduleCpuSumMs),
|
|
1501
|
+
module_cpu_sum_p95_ms: percentile95(window.moduleCpuSumMs),
|
|
1502
|
+
module_cpu_max_p95_ms: percentile95(window.moduleCpuMaxMs),
|
|
1503
|
+
update_total_avg_ms: roundedAverage(window.updateTotalMs),
|
|
1504
|
+
update_total_p95_ms: percentile95(window.updateTotalMs),
|
|
1505
|
+
commit_avg_ms: roundedAverage(window.commitMs),
|
|
1506
|
+
commit_p95_ms: percentile95(window.commitMs),
|
|
1507
|
+
total_avg_ms: roundedAverage(window.totalMs),
|
|
1508
|
+
total_p95_ms: percentile95(window.totalMs),
|
|
1509
|
+
total_max_ms: Math.max(...window.totalMs),
|
|
1510
|
+
worker_count_max: window.workerCountMax,
|
|
1511
|
+
});
|
|
1512
|
+
this.frameTimingWindow = this.newFrameTimingWindow();
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1217
1515
|
private flushModuleTimingWindows(
|
|
1218
1516
|
diagnostic: PerceptionDiagnostic | undefined,
|
|
1219
1517
|
force = false,
|
|
@@ -1337,6 +1635,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1337
1635
|
let analysisPool: PerceptionAnalysisThreadPool | undefined;
|
|
1338
1636
|
try {
|
|
1339
1637
|
const candidates = this.configuredModules;
|
|
1638
|
+
this.disabledModuleIds.clear();
|
|
1340
1639
|
for (const module of candidates) {
|
|
1341
1640
|
module.analysisGeneration = 0;
|
|
1342
1641
|
module.analysisHistoryFloorFrameSeq = 0;
|
|
@@ -1347,6 +1646,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1347
1646
|
CLAWCLAW_PERCEPTION_SPATIAL_FACTS satisfies PerceptionSpatialFacts,
|
|
1348
1647
|
this.api,
|
|
1349
1648
|
signal,
|
|
1649
|
+
diagnostic,
|
|
1350
1650
|
)));
|
|
1351
1651
|
this.assertRunnerLifecycle(generation, signal);
|
|
1352
1652
|
|
|
@@ -1401,6 +1701,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1401
1701
|
loader: this.moduleLoader.threadConfig(),
|
|
1402
1702
|
spatialFacts: CLAWCLAW_PERCEPTION_SPATIAL_FACTS satisfies PerceptionSpatialFacts,
|
|
1403
1703
|
maxWorkers: this.maxInFlight,
|
|
1704
|
+
diagnosticsEnabled: this.diagnosticsEnabled,
|
|
1404
1705
|
diagnostic,
|
|
1405
1706
|
});
|
|
1406
1707
|
analysisPool = currentPool;
|
|
@@ -1480,6 +1781,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1480
1781
|
await analysisPool?.stop();
|
|
1481
1782
|
if (this.analysisPool === analysisPool) this.analysisPool = undefined;
|
|
1482
1783
|
await Promise.allSettled(this.configuredModules.map(({ runner }) => runner.stop('shutdown')));
|
|
1784
|
+
await this.reducerPool?.stop();
|
|
1483
1785
|
if (generation === this.runnerLifecycleGeneration) {
|
|
1484
1786
|
await this.moduleLoader.stop();
|
|
1485
1787
|
this.modules = [];
|
|
@@ -1503,6 +1805,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1503
1805
|
loader: this.moduleLoader.threadConfig(),
|
|
1504
1806
|
spatialFacts: CLAWCLAW_PERCEPTION_SPATIAL_FACTS satisfies PerceptionSpatialFacts,
|
|
1505
1807
|
maxWorkers: 1,
|
|
1808
|
+
diagnosticsEnabled: this.diagnosticsEnabled,
|
|
1506
1809
|
});
|
|
1507
1810
|
this.analysisPool = pool;
|
|
1508
1811
|
let failure: unknown;
|
|
@@ -1549,6 +1852,7 @@ export class ClawclawPerceptionPlugin implements RuntimeEpochPlugin<Record<strin
|
|
|
1549
1852
|
this.lastVisualFrame = null;
|
|
1550
1853
|
this.lastConnectionId = null;
|
|
1551
1854
|
this.analysisWindow.length = 0;
|
|
1855
|
+
this.projectedFrameCache.clear();
|
|
1552
1856
|
this.replayFromFrameSeq = null;
|
|
1553
1857
|
}
|
|
1554
1858
|
|