@myclaw163/clawclaw-cli 0.8.12 → 0.8.14
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/configuration.md +11 -12
- package/docs/custom-modules/README.md +3 -3
- package/docs/custom-modules/behavior.md +1 -1
- package/docs/custom-modules/perception.md +1 -1
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/official-perception-modules.md +1 -1
- package/package.json +1 -1
- package/scripts/check-skill-command-surface.mjs +1 -0
- package/scripts/sync-bundled-skill.mjs +89 -52
- package/skills/clawclaw/SKILL.md +10 -6
- package/skills/clawclaw/references/BEHAVIORS.md +2 -0
- package/skills/clawclaw/references/CHANNELS.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +11 -4
- package/skills/clawclaw/references/HUB.md +4 -2
- package/skills/clawclaw/references/STREAM.md +4 -2
- package/src/cli.ts +2 -0
- package/src/commands/behavior.ts +49 -8
- package/src/commands/capability.ts +345 -0
- package/src/commands/load.ts +195 -8
- package/src/commands/perception.ts +21 -7
- package/src/commands/persona.ts +6 -0
- package/src/commands/trigger.ts +8 -5
- package/src/lib/agent-workspace-skills.ts +167 -68
- package/src/lib/agent-workspace-sync.ts +20 -31
- package/src/lib/capability-cloud-refresh.ts +313 -0
- package/src/lib/capability-cloud-sync.ts +315 -0
- package/src/lib/capability-discovery.ts +94 -0
- package/src/lib/capability-workspace.ts +291 -0
- package/src/lib/game-client.ts +12 -4
- package/src/lib/hub-client.ts +131 -4
- package/src/lib/persona.ts +1 -24
- package/src/lib/skill-host.ts +63 -0
- package/src/lib/user-data.ts +47 -59
- package/src/lib/zip-store.ts +92 -0
- 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-input.ts +5 -0
- package/src/runtime/clawclaw/owner.ts +12 -12
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +150 -18
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +8 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-decision-history.ts +88 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +173 -11
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +6 -0
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +3 -1
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +5 -1
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +5 -5
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +213 -6
- 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/runtime.ts +31 -3
- package/src/runtime/clawclaw-event-catalog.ts +2 -0
- package/src/runtime/framework/events/event-repository.ts +1 -1
- package/src/runtime/framework/events/memory-event-repository.ts +59 -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/persistence/async-sqlite-persistence-worker.ts +58 -0
- package/src/runtime/framework/persistence/memory-first-persistence.ts +380 -0
- package/src/runtime/framework/plugins/contracts.ts +2 -2
- package/src/runtime/framework/plugins/runtime-input-journal.ts +173 -17
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +23 -11
- package/src/runtime/framework/runtime.ts +57 -4
- package/src/runtime/framework/state/history/contracts.ts +9 -1
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +49 -7
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +16 -0
- package/src/sdk/behavior-plugin/index.ts +5 -24
- package/src/sdk/module-input/index.ts +47 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
2
3
|
import type { GameClient } from '../../../../lib/game-client.js';
|
|
3
4
|
import { ApiError } from '../../../../lib/http-transport.js';
|
|
4
5
|
import type { TransportResponse } from '../../../../short-command/transport-response.js';
|
|
6
|
+
import { actionFailureDiagnosticSeverity } from '../../../framework/action-diagnostics.js';
|
|
5
7
|
import type {
|
|
6
8
|
ActionCaller,
|
|
7
9
|
ActionControlDescriptor,
|
|
@@ -29,6 +31,7 @@ export interface ClawclawActionRequest {
|
|
|
29
31
|
audioFile?: { path: string; contentType: string };
|
|
30
32
|
/** Host-only preconditions for one unified Behavior activation instance. */
|
|
31
33
|
behaviorOriginEpochSeq?: number;
|
|
34
|
+
behaviorOriginTick?: number;
|
|
32
35
|
behaviorActionSafetyGeneration?: number;
|
|
33
36
|
behaviorActivationGeneration?: number;
|
|
34
37
|
}
|
|
@@ -143,6 +146,49 @@ interface AdmittedClawclawAction {
|
|
|
143
146
|
route: 'game' | 'room_chat';
|
|
144
147
|
}
|
|
145
148
|
|
|
149
|
+
interface AutomatedProviderTiming {
|
|
150
|
+
readonly queuedAtMs: number;
|
|
151
|
+
readonly queueDepthAtAdmission: number;
|
|
152
|
+
readonly inFlightAtAdmission: number;
|
|
153
|
+
startedAtMs?: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface AutomatedSubmission {
|
|
157
|
+
readonly settlement: Promise<ActionResult>;
|
|
158
|
+
readonly timing: AutomatedProviderTiming;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function elapsedMs(startedAtMs: number, endedAtMs = performance.now()): number {
|
|
162
|
+
return Math.round(Math.max(0, endedAtMs - startedAtMs) * 1_000) / 1_000;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function automatedDiagnosticAttributes(
|
|
166
|
+
request: ClawclawActionRequest,
|
|
167
|
+
admitted: AdmittedClawclawAction,
|
|
168
|
+
caller: AutomatedActionCaller,
|
|
169
|
+
): Readonly<Record<string, unknown>> {
|
|
170
|
+
return {
|
|
171
|
+
action_id: admitted.actionId,
|
|
172
|
+
action_kind: typeof admitted.action.action === 'string'
|
|
173
|
+
? admitted.action.action
|
|
174
|
+
: 'unknown',
|
|
175
|
+
route: admitted.route,
|
|
176
|
+
caller_kind: caller.kind,
|
|
177
|
+
behavior_activation_id: caller.activationId,
|
|
178
|
+
behavior_activation_generation: caller.activationGeneration,
|
|
179
|
+
behavior_layer: caller.layer,
|
|
180
|
+
...(request.behaviorOriginEpochSeq === undefined
|
|
181
|
+
? {}
|
|
182
|
+
: {
|
|
183
|
+
epoch_seq: request.behaviorOriginEpochSeq,
|
|
184
|
+
origin_epoch_seq: request.behaviorOriginEpochSeq,
|
|
185
|
+
}),
|
|
186
|
+
...(request.behaviorOriginTick === undefined
|
|
187
|
+
? {}
|
|
188
|
+
: { origin_tick: request.behaviorOriginTick }),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
146
192
|
/** Validates authoritative seat targets without consulting names or roster state. */
|
|
147
193
|
function prepareActionTarget(action: Record<string, any>): ActionTargetPreparation {
|
|
148
194
|
if (action.action !== 'kill' && action.action !== 'vote') return { action };
|
|
@@ -218,6 +264,7 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
218
264
|
* gameplay endpoint before an earlier one has started.
|
|
219
265
|
*/
|
|
220
266
|
private readonly automatedProviderStarts = new Map<string, Promise<void>>();
|
|
267
|
+
private readonly automatedProviderQueueDepths = new Map<string, number>();
|
|
221
268
|
|
|
222
269
|
constructor(private readonly client: GameClient) {}
|
|
223
270
|
|
|
@@ -251,6 +298,7 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
251
298
|
this.unregister = undefined;
|
|
252
299
|
this.diagnostic = undefined;
|
|
253
300
|
this.automatedProviderStarts.clear();
|
|
301
|
+
this.automatedProviderQueueDepths.clear();
|
|
254
302
|
}
|
|
255
303
|
|
|
256
304
|
private automatedProviderQueueKey(scope: ScopeRef): string {
|
|
@@ -263,9 +311,17 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
263
311
|
caller: AutomatedActionCaller,
|
|
264
312
|
context: ActionExecutionContext,
|
|
265
313
|
admitted: AdmittedClawclawAction,
|
|
266
|
-
):
|
|
314
|
+
): AutomatedSubmission {
|
|
267
315
|
const queueKey = this.automatedProviderQueueKey(scope);
|
|
268
316
|
const previousStarted = this.automatedProviderStarts.get(queueKey) ?? Promise.resolve();
|
|
317
|
+
const queueDepthAtAdmission = (this.automatedProviderQueueDepths.get(queueKey) ?? 0) + 1;
|
|
318
|
+
this.automatedProviderQueueDepths.set(queueKey, queueDepthAtAdmission);
|
|
319
|
+
const timing: AutomatedProviderTiming = {
|
|
320
|
+
queuedAtMs: performance.now(),
|
|
321
|
+
queueDepthAtAdmission,
|
|
322
|
+
inFlightAtAdmission: this.ownedSettlements.size + 1,
|
|
323
|
+
};
|
|
324
|
+
const diagnostic = this.diagnostic;
|
|
269
325
|
let markStarted!: () => void;
|
|
270
326
|
const started = new Promise<void>((resolve) => { markStarted = resolve; });
|
|
271
327
|
this.automatedProviderStarts.set(queueKey, started);
|
|
@@ -274,9 +330,56 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
274
330
|
this.automatedProviderStarts.delete(queueKey);
|
|
275
331
|
}
|
|
276
332
|
});
|
|
277
|
-
|
|
333
|
+
let queuePositionReleased = false;
|
|
334
|
+
const releaseQueuePosition = (endpointStarted: boolean): void => {
|
|
335
|
+
if (queuePositionReleased) return;
|
|
336
|
+
queuePositionReleased = true;
|
|
337
|
+
const releasedAtMs = performance.now();
|
|
338
|
+
if (endpointStarted) timing.startedAtMs = releasedAtMs;
|
|
339
|
+
const remainingQueueDepth = Math.max(
|
|
340
|
+
0,
|
|
341
|
+
(this.automatedProviderQueueDepths.get(queueKey) ?? 1) - 1,
|
|
342
|
+
);
|
|
343
|
+
if (remainingQueueDepth === 0) this.automatedProviderQueueDepths.delete(queueKey);
|
|
344
|
+
else this.automatedProviderQueueDepths.set(queueKey, remainingQueueDepth);
|
|
345
|
+
this.emitDiagnostic(
|
|
346
|
+
endpointStarted
|
|
347
|
+
? 'runtime.action.provider_started'
|
|
348
|
+
: 'runtime.action.provider_skipped',
|
|
349
|
+
{
|
|
350
|
+
...automatedDiagnosticAttributes(request, admitted, caller),
|
|
351
|
+
queue_wait_ms: elapsedMs(timing.queuedAtMs, releasedAtMs),
|
|
352
|
+
queue_depth_at_admission: timing.queueDepthAtAdmission,
|
|
353
|
+
in_flight_at_admission: timing.inFlightAtAdmission,
|
|
354
|
+
remaining_queue_depth: remainingQueueDepth,
|
|
355
|
+
},
|
|
356
|
+
'debug',
|
|
357
|
+
diagnostic,
|
|
358
|
+
);
|
|
359
|
+
const queueWaitMs = elapsedMs(timing.queuedAtMs, releasedAtMs);
|
|
360
|
+
if (endpointStarted && (queueWaitMs >= 20 || timing.queueDepthAtAdmission >= 2)) {
|
|
361
|
+
this.emitDiagnostic('runtime.action.provider_queue_delayed', {
|
|
362
|
+
...automatedDiagnosticAttributes(request, admitted, caller),
|
|
363
|
+
queue_wait_ms: queueWaitMs,
|
|
364
|
+
queue_depth_at_admission: timing.queueDepthAtAdmission,
|
|
365
|
+
in_flight_at_admission: timing.inFlightAtAdmission,
|
|
366
|
+
remaining_queue_depth: remainingQueueDepth,
|
|
367
|
+
budget_exceeded: queueWaitMs >= 100,
|
|
368
|
+
}, 'warn', diagnostic);
|
|
369
|
+
}
|
|
370
|
+
markStarted();
|
|
371
|
+
};
|
|
372
|
+
const settlement = previousStarted
|
|
278
373
|
.catch(() => {})
|
|
279
|
-
.then(() => this.submitAdmitted(
|
|
374
|
+
.then(() => this.submitAdmitted(
|
|
375
|
+
request,
|
|
376
|
+
scope,
|
|
377
|
+
caller,
|
|
378
|
+
context,
|
|
379
|
+
admitted,
|
|
380
|
+
releaseQueuePosition,
|
|
381
|
+
));
|
|
382
|
+
return { settlement, timing };
|
|
280
383
|
}
|
|
281
384
|
|
|
282
385
|
private automationSafetyRejection(
|
|
@@ -407,7 +510,7 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
407
510
|
if (!isAutomatedCaller(caller)) {
|
|
408
511
|
return this.submitAdmitted(request, scope, caller, context, admitted);
|
|
409
512
|
}
|
|
410
|
-
const
|
|
513
|
+
const queued = this.enqueueAutomatedSubmission(
|
|
411
514
|
request,
|
|
412
515
|
scope,
|
|
413
516
|
caller,
|
|
@@ -415,11 +518,20 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
415
518
|
admitted,
|
|
416
519
|
);
|
|
417
520
|
this.emitDiagnostic('runtime.action.provider_queued', {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
521
|
+
...automatedDiagnosticAttributes(request, admitted, caller),
|
|
522
|
+
queue_depth: queued.timing.queueDepthAtAdmission,
|
|
523
|
+
in_flight: queued.timing.inFlightAtAdmission,
|
|
421
524
|
});
|
|
422
|
-
const ownedSettlement = this.ownSettlement(
|
|
525
|
+
const ownedSettlement = this.ownSettlement(
|
|
526
|
+
scope,
|
|
527
|
+
caller,
|
|
528
|
+
action,
|
|
529
|
+
actionId,
|
|
530
|
+
admitted.route,
|
|
531
|
+
request,
|
|
532
|
+
queued.settlement,
|
|
533
|
+
queued.timing,
|
|
534
|
+
);
|
|
423
535
|
const dispatch: ActionDispatch = {
|
|
424
536
|
receipt: actionResult(scope, actionId, 'success', {
|
|
425
537
|
accepted: true,
|
|
@@ -442,7 +554,7 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
442
554
|
caller: ActionCaller,
|
|
443
555
|
context: ActionExecutionContext,
|
|
444
556
|
admitted: AdmittedClawclawAction,
|
|
445
|
-
markProviderStartedOrSkipped: () => void = () => {},
|
|
557
|
+
markProviderStartedOrSkipped: (endpointStarted: boolean) => void = () => {},
|
|
446
558
|
): Promise<ActionResult> {
|
|
447
559
|
let actionEndpointStarted = false;
|
|
448
560
|
const action = { ...admitted.action };
|
|
@@ -464,12 +576,13 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
464
576
|
request, scope, admitted.actionId, caller, context,
|
|
465
577
|
);
|
|
466
578
|
if (rejected) return rejected;
|
|
579
|
+
const pending = this.client.chatRoomConfirmed(action.text);
|
|
467
580
|
actionEndpointStarted = true;
|
|
468
|
-
markProviderStartedOrSkipped();
|
|
581
|
+
markProviderStartedOrSkipped(true);
|
|
469
582
|
return acknowledgedActionResult(
|
|
470
583
|
scope,
|
|
471
584
|
admitted.actionId,
|
|
472
|
-
await
|
|
585
|
+
await pending,
|
|
473
586
|
);
|
|
474
587
|
}
|
|
475
588
|
const preparedAction = await this.client.prepareActionForConfirmedSubmission(action as any);
|
|
@@ -477,9 +590,10 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
477
590
|
request, scope, admitted.actionId, caller, context,
|
|
478
591
|
);
|
|
479
592
|
if (rejected) return rejected;
|
|
593
|
+
const pending = this.client.submitPreparedActionConfirmed(preparedAction);
|
|
480
594
|
actionEndpointStarted = true;
|
|
481
|
-
markProviderStartedOrSkipped();
|
|
482
|
-
const response = await
|
|
595
|
+
markProviderStartedOrSkipped(true);
|
|
596
|
+
const response = await pending;
|
|
483
597
|
return acknowledgedActionResult(scope, admitted.actionId, response);
|
|
484
598
|
} catch (error) {
|
|
485
599
|
return actionResult(scope, admitted.actionId, 'failure', undefined, backendError(error), {
|
|
@@ -491,7 +605,7 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
491
605
|
} finally {
|
|
492
606
|
// A local rejection or preparation failure is also a resolved queue
|
|
493
607
|
// position: later Decisions may continue without waiting forever.
|
|
494
|
-
markProviderStartedOrSkipped();
|
|
608
|
+
markProviderStartedOrSkipped(false);
|
|
495
609
|
}
|
|
496
610
|
}
|
|
497
611
|
|
|
@@ -500,23 +614,41 @@ export class ClawclawActionPlugin implements RuntimeEpochPlugin<Record<string, a
|
|
|
500
614
|
caller: AutomatedActionCaller,
|
|
501
615
|
action: Record<string, any>,
|
|
502
616
|
actionId: string,
|
|
617
|
+
route: AdmittedClawclawAction['route'],
|
|
618
|
+
request: ClawclawActionRequest,
|
|
503
619
|
settlement: Promise<ActionResult>,
|
|
620
|
+
timing: AutomatedProviderTiming,
|
|
504
621
|
): Promise<ActionResult> {
|
|
505
622
|
// Keep the epoch's diagnostic sink with the owned submission. The plugin
|
|
506
623
|
// may be stopped after the Runtime starts draining, but the final provider
|
|
507
624
|
// result still belongs to this Action dispatch and must remain observable.
|
|
508
625
|
const diagnostic = this.diagnostic;
|
|
509
626
|
const report = (result: ActionResult): ActionResult => {
|
|
627
|
+
const settledAtMs = performance.now();
|
|
510
628
|
const errorCode = actionResultErrorCode(result);
|
|
511
629
|
this.emitDiagnostic('runtime.action.provider_settled', {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
630
|
+
...automatedDiagnosticAttributes(request, {
|
|
631
|
+
actionId,
|
|
632
|
+
action,
|
|
633
|
+
route,
|
|
634
|
+
}, caller),
|
|
515
635
|
status: result.status,
|
|
516
636
|
...(result.source ? { action_source: result.source } : {}),
|
|
517
637
|
...(result.actionOutcome ? { action_outcome: result.actionOutcome } : {}),
|
|
518
638
|
...(errorCode ? { error_code: errorCode } : {}),
|
|
519
|
-
|
|
639
|
+
queue_depth_at_admission: timing.queueDepthAtAdmission,
|
|
640
|
+
in_flight_at_admission: timing.inFlightAtAdmission,
|
|
641
|
+
in_flight_at_settlement: this.ownedSettlements.size,
|
|
642
|
+
queue_wait_ms: timing.startedAtMs === undefined
|
|
643
|
+
? elapsedMs(timing.queuedAtMs, settledAtMs)
|
|
644
|
+
: elapsedMs(timing.queuedAtMs, timing.startedAtMs),
|
|
645
|
+
...(timing.startedAtMs === undefined
|
|
646
|
+
? {}
|
|
647
|
+
: { provider_duration_ms: elapsedMs(timing.startedAtMs, settledAtMs) }),
|
|
648
|
+
total_duration_ms: elapsedMs(timing.queuedAtMs, settledAtMs),
|
|
649
|
+
}, result.status === 'failure'
|
|
650
|
+
? actionFailureDiagnosticSeverity(result)
|
|
651
|
+
: 'debug', diagnostic);
|
|
520
652
|
return result;
|
|
521
653
|
};
|
|
522
654
|
let owned!: Promise<ActionResult>;
|
|
@@ -7,6 +7,7 @@ export function normalizeClawclawBehaviorAction(
|
|
|
7
7
|
behaviorOriginEpochSeq: number,
|
|
8
8
|
behaviorActionSafetyGeneration: number | undefined,
|
|
9
9
|
behaviorActivationGeneration: number,
|
|
10
|
+
behaviorOriginTick?: number,
|
|
10
11
|
): ClawclawActionRequest {
|
|
11
12
|
if (!Number.isInteger(behaviorOriginEpochSeq) || behaviorOriginEpochSeq <= 0) {
|
|
12
13
|
throw new Error('invalid_behavior_action_epoch');
|
|
@@ -20,10 +21,17 @@ export function normalizeClawclawBehaviorAction(
|
|
|
20
21
|
if (!Number.isSafeInteger(behaviorActivationGeneration) || behaviorActivationGeneration <= 0) {
|
|
21
22
|
throw new Error('invalid_behavior_activation_generation');
|
|
22
23
|
}
|
|
24
|
+
if (
|
|
25
|
+
behaviorOriginTick !== undefined
|
|
26
|
+
&& (!Number.isSafeInteger(behaviorOriginTick) || behaviorOriginTick < 0)
|
|
27
|
+
) {
|
|
28
|
+
throw new Error('invalid_behavior_action_tick');
|
|
29
|
+
}
|
|
23
30
|
return {
|
|
24
31
|
route: 'auto',
|
|
25
32
|
action: normalizeClawclawBehaviorActionPayload(value),
|
|
26
33
|
behaviorOriginEpochSeq,
|
|
34
|
+
...(behaviorOriginTick === undefined ? {} : { behaviorOriginTick }),
|
|
27
35
|
behaviorActionSafetyGeneration,
|
|
28
36
|
behaviorActivationGeneration,
|
|
29
37
|
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BehaviorDecisionAction,
|
|
3
|
+
BehaviorDecisionFrame,
|
|
4
|
+
BehaviorDecisionFrameStatus,
|
|
5
|
+
} from '../../../../sdk/module-input/index.js';
|
|
6
|
+
|
|
7
|
+
export const BEHAVIOR_DECISION_HISTORY_LIMIT = 100;
|
|
8
|
+
|
|
9
|
+
interface DecisionFrameStart {
|
|
10
|
+
readonly epochId: number;
|
|
11
|
+
readonly tick: number;
|
|
12
|
+
readonly instanceGeneration: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cloneActions(
|
|
16
|
+
actions: readonly Readonly<BehaviorDecisionAction>[] | undefined,
|
|
17
|
+
): readonly Readonly<BehaviorDecisionAction>[] {
|
|
18
|
+
return Object.freeze((actions ?? []).map((action) => Object.freeze(structuredClone(action))));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** One Owner-owned, activation-local ring shared by concurrent Decision computations. */
|
|
22
|
+
export class BehaviorDecisionHistory {
|
|
23
|
+
private readonly frames: BehaviorDecisionFrame[] = [];
|
|
24
|
+
private cachedSnapshot?: readonly Readonly<BehaviorDecisionFrame>[];
|
|
25
|
+
|
|
26
|
+
begin(start: DecisionFrameStart): Readonly<BehaviorDecisionFrame> {
|
|
27
|
+
const existing = this.frames.find((frame) => frame.epochId === start.epochId);
|
|
28
|
+
if (existing) return existing;
|
|
29
|
+
const frame = Object.freeze({
|
|
30
|
+
...start,
|
|
31
|
+
status: 'pending' as const,
|
|
32
|
+
actions: null,
|
|
33
|
+
});
|
|
34
|
+
this.frames.push(frame);
|
|
35
|
+
if (this.frames.length > BEHAVIOR_DECISION_HISTORY_LIMIT) {
|
|
36
|
+
this.frames.splice(0, this.frames.length - BEHAVIOR_DECISION_HISTORY_LIMIT);
|
|
37
|
+
}
|
|
38
|
+
this.cachedSnapshot = undefined;
|
|
39
|
+
return frame;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
complete(
|
|
43
|
+
epochId: number,
|
|
44
|
+
actions: readonly Readonly<BehaviorDecisionAction>[] | undefined,
|
|
45
|
+
): Readonly<BehaviorDecisionFrame> | undefined {
|
|
46
|
+
return this.resolve(epochId, 'completed', cloneActions(actions));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
notComputed(epochId: number, reason: string): Readonly<BehaviorDecisionFrame> | undefined {
|
|
50
|
+
return this.resolve(epochId, 'not_computed', null, reason);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fail(epochId: number, reason: string): Readonly<BehaviorDecisionFrame> | undefined {
|
|
54
|
+
return this.resolve(epochId, 'failed', null, reason);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
snapshot(): readonly Readonly<BehaviorDecisionFrame>[] {
|
|
58
|
+
this.cachedSnapshot ??= Object.freeze([...this.frames]);
|
|
59
|
+
return this.cachedSnapshot;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
clear(): void {
|
|
63
|
+
this.frames.length = 0;
|
|
64
|
+
this.cachedSnapshot = undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private resolve(
|
|
68
|
+
epochId: number,
|
|
69
|
+
status: Exclude<BehaviorDecisionFrameStatus, 'pending'>,
|
|
70
|
+
actions: readonly Readonly<BehaviorDecisionAction>[] | null,
|
|
71
|
+
reason?: string,
|
|
72
|
+
): Readonly<BehaviorDecisionFrame> | undefined {
|
|
73
|
+
const index = this.frames.findIndex((frame) => frame.epochId === epochId);
|
|
74
|
+
if (index < 0) return undefined;
|
|
75
|
+
const previous = this.frames[index]!;
|
|
76
|
+
const next = Object.freeze({
|
|
77
|
+
epochId: previous.epochId,
|
|
78
|
+
tick: previous.tick,
|
|
79
|
+
instanceGeneration: previous.instanceGeneration,
|
|
80
|
+
status,
|
|
81
|
+
actions,
|
|
82
|
+
...(reason ? { reason } : {}),
|
|
83
|
+
});
|
|
84
|
+
this.frames[index] = next;
|
|
85
|
+
this.cachedSnapshot = undefined;
|
|
86
|
+
return next;
|
|
87
|
+
}
|
|
88
|
+
}
|