@myclaw163/clawclaw-cli 0.8.14 → 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 +5 -1
- package/skills/clawclaw/references/BEHAVIORS.md +16 -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/src/runtime/clawclaw/behavior-decision.ts +4 -1
- package/src/runtime/clawclaw/owner.ts +64 -1
- 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/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 +114 -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/observability/redaction.ts +28 -1
- package/src/sdk/perception-plugin/index.ts +13 -8
|
@@ -34,6 +34,8 @@ import {
|
|
|
34
34
|
} from './contracts.js';
|
|
35
35
|
import { TriggerModuleLoader } from './trigger-module-loader.js';
|
|
36
36
|
import type { ResolvedClawclawTriggerSource } from './trigger-source.js';
|
|
37
|
+
import { assertRuntimeModuleOutputSize } from '../../../framework/modules/runtime-module-output.js';
|
|
38
|
+
import { TriggerThreadPool } from './trigger-thread-pool.js';
|
|
37
39
|
|
|
38
40
|
const TRIGGER_COMPUTATION_SCHEMA = 1;
|
|
39
41
|
const TRIGGER_CHECKPOINT_SCHEMA = 1;
|
|
@@ -80,6 +82,9 @@ export interface ClawclawTriggerPluginOptions {
|
|
|
80
82
|
readonly triggers?: readonly ClawclawTriggerRegistration[];
|
|
81
83
|
readonly loader?: TriggerModuleLoader;
|
|
82
84
|
readonly driver?: BehaviorReflexActivationDriver;
|
|
85
|
+
readonly diagnosticsEnabled?: boolean;
|
|
86
|
+
/** Tests and embedded trusted hosts may retain the direct executor. */
|
|
87
|
+
readonly threaded?: boolean;
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
function record(value: unknown): Record<string, unknown> | undefined {
|
|
@@ -105,11 +110,13 @@ function normalizeMatch(value: unknown): TriggerMatch | undefined {
|
|
|
105
110
|
if (source.behaviorInput !== undefined && !record(source.behaviorInput)) {
|
|
106
111
|
throw new Error('trigger_match_behavior_input_invalid');
|
|
107
112
|
}
|
|
108
|
-
|
|
113
|
+
const normalized = Object.freeze(structuredClone({
|
|
109
114
|
reason: source.reason.trim(),
|
|
110
115
|
...(source.evidence === undefined ? {} : { evidence: source.evidence }),
|
|
111
116
|
...(source.behaviorInput === undefined ? {} : { behaviorInput: source.behaviorInput }),
|
|
112
117
|
})) as TriggerMatch;
|
|
118
|
+
assertRuntimeModuleOutputSize(normalized, 'trigger_output');
|
|
119
|
+
return normalized;
|
|
113
120
|
}
|
|
114
121
|
|
|
115
122
|
function normalizeRegistration(value: ClawclawTriggerRegistration): EffectiveTriggerConfig {
|
|
@@ -233,8 +240,11 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
233
240
|
readonly version = String(TRIGGER_PLUGIN_API_VERSION);
|
|
234
241
|
readonly after: readonly string[];
|
|
235
242
|
readonly consumes = { events: ['*'] } as const;
|
|
236
|
-
readonly catchUp = '
|
|
237
|
-
|
|
243
|
+
readonly catchUp = 'every_epoch' as const;
|
|
244
|
+
get maxInFlight(): number {
|
|
245
|
+
return this.threaded ? 10 : 1;
|
|
246
|
+
}
|
|
247
|
+
readonly maxPendingEpochs = 10;
|
|
238
248
|
readonly timeoutMs = DEFAULT_TRIGGER_PLUGIN_TIMEOUT_MS;
|
|
239
249
|
readonly failurePolicy = 'failed_completion' as const;
|
|
240
250
|
|
|
@@ -245,6 +255,12 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
245
255
|
private readonly states = new Map<string, TriggerRuntimeState>();
|
|
246
256
|
private readonly observationMemory = new ClawclawBehaviorObservationMemory();
|
|
247
257
|
private readonly projector = new ObservationFrameProjector();
|
|
258
|
+
private readonly threaded: boolean;
|
|
259
|
+
private readonly diagnosticsEnabled: boolean;
|
|
260
|
+
private threadPool?: TriggerThreadPool;
|
|
261
|
+
private threadPoolStartTask?: Promise<void>;
|
|
262
|
+
private threadPoolFailed = false;
|
|
263
|
+
private pendingThreadResetReason?: TriggerResetReason;
|
|
248
264
|
private driver?: BehaviorReflexActivationDriver;
|
|
249
265
|
private configGeneration = 1;
|
|
250
266
|
private started = false;
|
|
@@ -258,6 +274,8 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
258
274
|
this.registrations = normalizeRegistrations(options.triggers ?? []);
|
|
259
275
|
this.loader = options.loader ?? new TriggerModuleLoader();
|
|
260
276
|
this.driver = options.driver;
|
|
277
|
+
this.threaded = options.threaded ?? options.loader === undefined;
|
|
278
|
+
this.diagnosticsEnabled = options.diagnosticsEnabled === true;
|
|
261
279
|
}
|
|
262
280
|
|
|
263
281
|
attachDriver(driver: BehaviorReflexActivationDriver): void {
|
|
@@ -275,6 +293,11 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
275
293
|
this.states.clear();
|
|
276
294
|
this.observationMemory.clear();
|
|
277
295
|
this.clearFrameBoundary();
|
|
296
|
+
void this.threadPool?.stop();
|
|
297
|
+
this.threadPool = undefined;
|
|
298
|
+
this.threadPoolStartTask = undefined;
|
|
299
|
+
this.threadPoolFailed = false;
|
|
300
|
+
this.pendingThreadResetReason = undefined;
|
|
278
301
|
this.loader.stop();
|
|
279
302
|
}
|
|
280
303
|
|
|
@@ -298,9 +321,32 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
298
321
|
}));
|
|
299
322
|
}
|
|
300
323
|
|
|
324
|
+
diagnosticSnapshot(): Readonly<Record<string, unknown>> {
|
|
325
|
+
const pool = this.threadPool?.snapshot();
|
|
326
|
+
return Object.freeze({
|
|
327
|
+
threaded: this.threaded,
|
|
328
|
+
configured_trigger_count: this.registrations.size,
|
|
329
|
+
enabled_trigger_count: [...this.registrations.values()].filter(({ enabled }) => enabled).length,
|
|
330
|
+
active_trigger_count: this.threaded ? pool?.workerCount ?? 0 : this.slots.size,
|
|
331
|
+
thread_pool_failed: this.threadPoolFailed,
|
|
332
|
+
worker_pool: pool
|
|
333
|
+
? {
|
|
334
|
+
state: pool.state,
|
|
335
|
+
worker_count: pool.workerCount,
|
|
336
|
+
busy_worker_count: pool.busyWorkerCount,
|
|
337
|
+
queued_request_count: pool.queuedRequestCount,
|
|
338
|
+
in_flight_request_count: pool.inFlightRequestCount,
|
|
339
|
+
worker_heap_used_bytes: pool.workerHeapUsedBytes,
|
|
340
|
+
peak_worker_heap_used_bytes: pool.peakWorkerHeapUsedBytes,
|
|
341
|
+
}
|
|
342
|
+
: null,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
301
346
|
async start(context: RuntimeEpochPluginContext<Record<string, any>>): Promise<void> {
|
|
302
347
|
this.started = true;
|
|
303
|
-
await this.
|
|
348
|
+
if (this.threaded) await this.ensureThreadPool(context);
|
|
349
|
+
else await this.ensureModules(context);
|
|
304
350
|
}
|
|
305
351
|
|
|
306
352
|
recover(context: RuntimeEpochPluginContext<Record<string, any>>): void {
|
|
@@ -326,6 +372,7 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
326
372
|
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
327
373
|
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
328
374
|
): Promise<RuntimeEpochPluginResult> {
|
|
375
|
+
if (this.threaded) return this.updateThreaded(input, context);
|
|
329
376
|
await this.ensureModules(context);
|
|
330
377
|
const generation = this.configGeneration;
|
|
331
378
|
const eventCursor = input.pendingEvents.at(-1)?.commitSeq;
|
|
@@ -343,7 +390,7 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
343
390
|
|| emptyInput.gameState.you?.is_alive === false
|
|
344
391
|
) {
|
|
345
392
|
if (this.lastVisualTick !== null) {
|
|
346
|
-
await this.
|
|
393
|
+
await this.requestModuleReset('phase_changed', context);
|
|
347
394
|
this.clearFrameBoundary();
|
|
348
395
|
}
|
|
349
396
|
return {
|
|
@@ -362,7 +409,7 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
362
409
|
const projected = this.projector.project(input.frame);
|
|
363
410
|
if (!projected) {
|
|
364
411
|
if (this.lastVisualTick !== null) {
|
|
365
|
-
await this.
|
|
412
|
+
await this.requestModuleReset('trajectory_gap', context);
|
|
366
413
|
this.clearFrameBoundary();
|
|
367
414
|
}
|
|
368
415
|
return {
|
|
@@ -395,7 +442,7 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
395
442
|
});
|
|
396
443
|
const gap = this.lastVisualTick !== null && !phaseChanged && !continuousFromPrevious;
|
|
397
444
|
if (gap || phaseChanged) {
|
|
398
|
-
await this.
|
|
445
|
+
await this.requestModuleReset(gap ? 'trajectory_gap' : 'phase_changed', context);
|
|
399
446
|
}
|
|
400
447
|
this.lastFrameSeq = input.frame.frameSeq;
|
|
401
448
|
this.lastVisualTick = frame.tick;
|
|
@@ -403,38 +450,40 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
403
450
|
this.lifecyclePhase = lifecycle;
|
|
404
451
|
|
|
405
452
|
const observationSnapshot = this.observationMemory.capture(emptyInput);
|
|
406
|
-
const
|
|
453
|
+
const evaluators = [...this.slots.values()].sort((left, right) => (
|
|
407
454
|
left.config.source.id.localeCompare(right.config.source.id)
|
|
408
455
|
));
|
|
409
|
-
const
|
|
410
|
-
const
|
|
456
|
+
const evaluationsPending = evaluators.map(async (evaluator): Promise<ClawclawTriggerEvaluation> => {
|
|
457
|
+
const { config } = evaluator;
|
|
458
|
+
const triggerInput = triggerInputFor(frame, emptyInput, config);
|
|
411
459
|
const readInput = behaviorInputForTrigger(frame, emptyInput, triggerInput);
|
|
412
460
|
const isActive = (): boolean => (
|
|
413
461
|
generation === this.configGeneration
|
|
414
|
-
&& this.slots.get(
|
|
462
|
+
&& this.slots.get(config.source.id) === evaluator
|
|
415
463
|
);
|
|
416
464
|
try {
|
|
417
|
-
const value = await
|
|
465
|
+
const value = await evaluator.module.evaluate(
|
|
418
466
|
triggerInput,
|
|
419
467
|
createBehaviorReadApi(readInput, { isActive, observationSnapshot }),
|
|
420
468
|
);
|
|
421
|
-
if (!isActive()) return { triggerId:
|
|
469
|
+
if (!isActive()) return { triggerId: config.source.id, outcome: 'error' };
|
|
422
470
|
const match = normalizeMatch(value);
|
|
423
471
|
return match
|
|
424
|
-
? { triggerId:
|
|
425
|
-
: { triggerId:
|
|
472
|
+
? { triggerId: config.source.id, outcome: 'match', match }
|
|
473
|
+
: { triggerId: config.source.id, outcome: 'clear' };
|
|
426
474
|
} catch (error) {
|
|
427
475
|
context.diagnostic('trigger.module.failed', {
|
|
428
|
-
trigger_id:
|
|
476
|
+
trigger_id: config.source.id,
|
|
429
477
|
operation: 'evaluate',
|
|
430
478
|
error_code: diagnosticErrorCode(error),
|
|
431
479
|
}, 'error');
|
|
432
|
-
return { triggerId:
|
|
480
|
+
return { triggerId: config.source.id, outcome: 'error' };
|
|
433
481
|
}
|
|
434
|
-
})
|
|
482
|
+
});
|
|
483
|
+
const settled = await Promise.allSettled(evaluationsPending);
|
|
435
484
|
const evaluations = settled.map((result, index): ClawclawTriggerEvaluation => {
|
|
436
485
|
if (result.status === 'fulfilled') return result.value;
|
|
437
|
-
const triggerId =
|
|
486
|
+
const triggerId = evaluators[index]!.config.source.id;
|
|
438
487
|
context.diagnostic('trigger.module.failed', {
|
|
439
488
|
trigger_id: triggerId,
|
|
440
489
|
operation: 'evaluate',
|
|
@@ -458,6 +507,122 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
458
507
|
};
|
|
459
508
|
}
|
|
460
509
|
|
|
510
|
+
private async updateThreaded(
|
|
511
|
+
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
512
|
+
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
513
|
+
): Promise<RuntimeEpochPluginResult> {
|
|
514
|
+
await this.ensureThreadPool(context);
|
|
515
|
+
const generation = this.configGeneration;
|
|
516
|
+
const eventCursor = input.pendingEvents.at(-1)?.commitSeq;
|
|
517
|
+
const triggerVisibleInput = {
|
|
518
|
+
...input,
|
|
519
|
+
pendingEvents: input.pendingEvents.filter((event) => event.producer === 'backend_push'),
|
|
520
|
+
} satisfies RuntimeEpochPluginInput<Record<string, any>>;
|
|
521
|
+
const emptyInput = createBehaviorInput(triggerVisibleInput, Object.freeze({}));
|
|
522
|
+
const baseProposal = (nowMs: number, evaluations: readonly ClawclawTriggerEvaluation[]) => ({
|
|
523
|
+
schema: TRIGGER_COMPUTATION_SCHEMA,
|
|
524
|
+
configGeneration: generation,
|
|
525
|
+
epochId: input.epoch.epochSeq,
|
|
526
|
+
nowMs,
|
|
527
|
+
...(eventCursor === undefined ? {} : { eventCursor }),
|
|
528
|
+
evaluations,
|
|
529
|
+
} satisfies TriggerComputationProposal);
|
|
530
|
+
if (
|
|
531
|
+
!emptyInput
|
|
532
|
+
|| emptyInput.gameState.phase !== 'wandering'
|
|
533
|
+
|| emptyInput.gameState.you?.is_alive === false
|
|
534
|
+
) {
|
|
535
|
+
return {
|
|
536
|
+
outcome: 'processed',
|
|
537
|
+
reason: 'trigger_ineligible_state',
|
|
538
|
+
proposal: baseProposal(
|
|
539
|
+
input.frame.observedAtMs ?? input.frame.receivedAtMs,
|
|
540
|
+
[],
|
|
541
|
+
),
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
const projected = this.projector.project(input.frame);
|
|
545
|
+
if (!projected) {
|
|
546
|
+
return {
|
|
547
|
+
outcome: 'processed',
|
|
548
|
+
reason: 'trigger_observation_unavailable',
|
|
549
|
+
proposal: baseProposal(emptyInput.nowMs, []),
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const previousStored = context.stateHistory.queryStateFrames({
|
|
554
|
+
scope: context.scope,
|
|
555
|
+
toEpochSeq: input.epoch.epochSeq - 1,
|
|
556
|
+
order: 'desc',
|
|
557
|
+
limit: 1,
|
|
558
|
+
}).frames[0];
|
|
559
|
+
const previous = previousStored ? this.projector.project(previousStored) : null;
|
|
560
|
+
const tickDelta = previous ? projected.tick - previous.tick : null;
|
|
561
|
+
const phaseChanged = !!previous
|
|
562
|
+
&& triggerLifecycle(previous) !== triggerLifecycle(projected);
|
|
563
|
+
const continuousFromPrevious = !!previous
|
|
564
|
+
&& !!previousStored
|
|
565
|
+
&& previousStored.frameSeq + 1 === input.frame.frameSeq
|
|
566
|
+
&& previousStored.connectionId === input.frame.connectionId
|
|
567
|
+
&& !phaseChanged
|
|
568
|
+
&& tickDelta !== null
|
|
569
|
+
&& tickDelta >= 0
|
|
570
|
+
&& tickDelta <= 3;
|
|
571
|
+
const frame: Readonly<ObservationFrame> = Object.freeze({
|
|
572
|
+
...projected,
|
|
573
|
+
tickDelta,
|
|
574
|
+
continuousFromPrevious,
|
|
575
|
+
});
|
|
576
|
+
const resetReason: TriggerResetReason | undefined = phaseChanged
|
|
577
|
+
? 'phase_changed'
|
|
578
|
+
: previousStored && !continuousFromPrevious ? 'trajectory_gap' : undefined;
|
|
579
|
+
const configs = [...this.registrations.values()]
|
|
580
|
+
.filter(({ enabled }) => enabled)
|
|
581
|
+
.sort((left, right) => left.source.id.localeCompare(right.source.id));
|
|
582
|
+
const settled = await Promise.allSettled(configs.map(async (config): Promise<ClawclawTriggerEvaluation> => {
|
|
583
|
+
const triggerInput = triggerInputFor(frame, emptyInput, config);
|
|
584
|
+
const readInput = behaviorInputForTrigger(frame, emptyInput, triggerInput);
|
|
585
|
+
try {
|
|
586
|
+
if (!this.threadPool) throw new Error('trigger_thread_pool_unavailable');
|
|
587
|
+
const value = await this.threadPool.evaluate({
|
|
588
|
+
epochId: input.epoch.epochSeq,
|
|
589
|
+
triggerId: config.source.id,
|
|
590
|
+
input: triggerInput,
|
|
591
|
+
readInput,
|
|
592
|
+
...(resetReason === undefined ? {} : { resetReason }),
|
|
593
|
+
}, context.signal);
|
|
594
|
+
const match = normalizeMatch(value);
|
|
595
|
+
return match
|
|
596
|
+
? { triggerId: config.source.id, outcome: 'match', match }
|
|
597
|
+
: { triggerId: config.source.id, outcome: 'clear' };
|
|
598
|
+
} catch (error) {
|
|
599
|
+
context.diagnostic('trigger.module.failed', {
|
|
600
|
+
trigger_id: config.source.id,
|
|
601
|
+
operation: 'evaluate',
|
|
602
|
+
error_code: diagnosticErrorCode(error),
|
|
603
|
+
}, context.signal.aborted ? 'debug' : 'error');
|
|
604
|
+
return { triggerId: config.source.id, outcome: 'error' };
|
|
605
|
+
}
|
|
606
|
+
}));
|
|
607
|
+
const evaluations = settled.map((result, index): ClawclawTriggerEvaluation => {
|
|
608
|
+
if (result.status === 'fulfilled') return result.value;
|
|
609
|
+
const triggerId = configs[index]!.source.id;
|
|
610
|
+
context.diagnostic('trigger.module.failed', {
|
|
611
|
+
trigger_id: triggerId,
|
|
612
|
+
operation: 'evaluate',
|
|
613
|
+
error_code: diagnosticErrorCode(result.reason),
|
|
614
|
+
}, context.signal.aborted ? 'debug' : 'error');
|
|
615
|
+
return { triggerId, outcome: 'error' };
|
|
616
|
+
});
|
|
617
|
+
return {
|
|
618
|
+
outcome: 'processed',
|
|
619
|
+
reason: evaluations.some((entry) => entry.outcome === 'match')
|
|
620
|
+
? 'trigger_match_computed'
|
|
621
|
+
: 'trigger_no_match',
|
|
622
|
+
proposal: baseProposal(frame.timeMs, evaluations),
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
461
626
|
async commit(
|
|
462
627
|
input: RuntimeEpochPluginInput<Record<string, any>>,
|
|
463
628
|
computation: Readonly<RuntimeEpochPluginResult>,
|
|
@@ -578,7 +743,8 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
578
743
|
}
|
|
579
744
|
|
|
580
745
|
async stop(reason: 'segment_ending' | 'shutdown'): Promise<void> {
|
|
581
|
-
await this.
|
|
746
|
+
if (this.threaded) await this.threadPool?.stop();
|
|
747
|
+
else await this.resetModules(reason, undefined);
|
|
582
748
|
this.started = false;
|
|
583
749
|
this.configGeneration += 1;
|
|
584
750
|
this.slots.clear();
|
|
@@ -586,9 +752,75 @@ export class ClawclawTriggerPlugin implements RuntimeEpochPlugin<Record<string,
|
|
|
586
752
|
this.states.clear();
|
|
587
753
|
this.observationMemory.clear();
|
|
588
754
|
this.clearFrameBoundary();
|
|
755
|
+
this.threadPool = undefined;
|
|
756
|
+
this.threadPoolStartTask = undefined;
|
|
757
|
+
this.threadPoolFailed = false;
|
|
758
|
+
this.pendingThreadResetReason = undefined;
|
|
589
759
|
this.loader.stop();
|
|
590
760
|
}
|
|
591
761
|
|
|
762
|
+
private async ensureThreadPool(
|
|
763
|
+
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
764
|
+
): Promise<void> {
|
|
765
|
+
if (!this.started || this.threadPool || this.threadPoolFailed) return;
|
|
766
|
+
if (this.threadPoolStartTask) return this.threadPoolStartTask;
|
|
767
|
+
const generation = this.configGeneration;
|
|
768
|
+
let task!: Promise<void>;
|
|
769
|
+
task = this.startThreadPool(context, generation).finally(() => {
|
|
770
|
+
if (this.threadPoolStartTask === task) this.threadPoolStartTask = undefined;
|
|
771
|
+
});
|
|
772
|
+
this.threadPoolStartTask = task;
|
|
773
|
+
return task;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
private async startThreadPool(
|
|
777
|
+
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
778
|
+
generation: number,
|
|
779
|
+
): Promise<void> {
|
|
780
|
+
const enabled = [...this.registrations.values()].filter(({ enabled }) => enabled);
|
|
781
|
+
if (enabled.length === 0) return;
|
|
782
|
+
let pool: TriggerThreadPool | undefined;
|
|
783
|
+
try {
|
|
784
|
+
pool = new TriggerThreadPool({
|
|
785
|
+
modules: enabled.map(({ source }) => this.loader.threadSpec(source)),
|
|
786
|
+
loader: this.loader.threadConfig(),
|
|
787
|
+
diagnosticsEnabled: this.diagnosticsEnabled,
|
|
788
|
+
diagnostic: context.diagnostic,
|
|
789
|
+
});
|
|
790
|
+
await pool.start();
|
|
791
|
+
if (!this.started || generation !== this.configGeneration) {
|
|
792
|
+
await pool.stop();
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
this.threadPool = pool;
|
|
796
|
+
} catch (error) {
|
|
797
|
+
if (!this.started || generation !== this.configGeneration) {
|
|
798
|
+
await pool?.stop().catch(() => {});
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
this.threadPoolFailed = true;
|
|
802
|
+
await pool?.stop().catch(() => {});
|
|
803
|
+
for (const config of enabled) {
|
|
804
|
+
context.diagnostic('trigger.module.failed', {
|
|
805
|
+
trigger_id: config.source.id,
|
|
806
|
+
operation: 'worker_start',
|
|
807
|
+
error_code: diagnosticErrorCode(error),
|
|
808
|
+
}, 'error');
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
private async requestModuleReset(
|
|
814
|
+
reason: TriggerResetReason,
|
|
815
|
+
context: RuntimeEpochPluginContext<Record<string, any>>,
|
|
816
|
+
): Promise<void> {
|
|
817
|
+
if (this.threaded) {
|
|
818
|
+
this.pendingThreadResetReason = reason;
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
await this.resetModules(reason, context);
|
|
822
|
+
}
|
|
823
|
+
|
|
592
824
|
private async ensureModules(context: RuntimeEpochPluginContext<Record<string, any>>): Promise<void> {
|
|
593
825
|
if (!this.started) return;
|
|
594
826
|
const enabled = [...this.registrations.values()].filter((entry) => entry.enabled);
|