@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
|
@@ -11,14 +11,18 @@ import {
|
|
|
11
11
|
validateClawclawBehaviorModule,
|
|
12
12
|
} from './behavior-module-loader.js';
|
|
13
13
|
import { deepFreezeBehaviorValue } from './behavior-manifest.js';
|
|
14
|
+
import { prepareClawclawBehavior } from './behavior-normalization.js';
|
|
14
15
|
import type { ResolvedClawclawBehaviorSource } from './behavior-source.js';
|
|
15
16
|
import type { ClawclawBehaviorObservationSnapshot } from '../../behavior-observation-memory.js';
|
|
17
|
+
import { assertRuntimeModuleOutputSize } from '../../../framework/modules/runtime-module-output.js';
|
|
16
18
|
|
|
17
19
|
interface BehaviorThreadWorkerData {
|
|
18
20
|
readonly poolId: string;
|
|
19
21
|
readonly slotId: number;
|
|
20
22
|
readonly source: ResolvedClawclawBehaviorSource;
|
|
21
23
|
readonly sourceUrl: string;
|
|
24
|
+
readonly diagnosticsEnabled?: boolean;
|
|
25
|
+
readonly prepareInput?: Readonly<Record<string, unknown>>;
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
interface BehaviorThreadRunRequest {
|
|
@@ -65,8 +69,16 @@ function send(value: Readonly<Record<string, unknown>>): void {
|
|
|
65
69
|
port!.postMessage({ ...value, poolId: data!.poolId!, slotId: data!.slotId! });
|
|
66
70
|
}
|
|
67
71
|
|
|
72
|
+
function diagnosticMemory(): Readonly<Record<string, number>> {
|
|
73
|
+
return data?.diagnosticsEnabled === true
|
|
74
|
+
? { heapUsedBytes: process.memoryUsage().heapUsed }
|
|
75
|
+
: {};
|
|
76
|
+
}
|
|
77
|
+
|
|
68
78
|
let loaded: ReturnType<typeof validateClawclawBehaviorModule>;
|
|
69
79
|
let execution: ResolvedClawclawBehaviorSource['execution'];
|
|
80
|
+
let preparation: ReturnType<typeof prepareClawclawBehavior> | undefined;
|
|
81
|
+
let serialInstance: ClawclawBehavior | undefined;
|
|
70
82
|
try {
|
|
71
83
|
const behaviorModule = await import(data.sourceUrl) as Record<string, unknown>;
|
|
72
84
|
// The parent validates immediately before spawn; repeat after the complete
|
|
@@ -74,6 +86,18 @@ try {
|
|
|
74
86
|
resolveBehaviorSource(data.source);
|
|
75
87
|
loaded = validateClawclawBehaviorModule(behaviorModule, data.source);
|
|
76
88
|
execution = loaded.manifest.execution ?? 'parallel';
|
|
89
|
+
if (data.prepareInput !== undefined) {
|
|
90
|
+
preparation = prepareClawclawBehavior(
|
|
91
|
+
loaded.BehaviorClass,
|
|
92
|
+
loaded.manifest,
|
|
93
|
+
data.prepareInput,
|
|
94
|
+
loaded.source,
|
|
95
|
+
);
|
|
96
|
+
assertRuntimeModuleOutputSize(preparation.input, 'behavior_normalize_output');
|
|
97
|
+
}
|
|
98
|
+
if (execution === 'serial' && data.prepareInput === undefined) {
|
|
99
|
+
serialInstance = new loaded.BehaviorClass();
|
|
100
|
+
}
|
|
77
101
|
} catch (error) {
|
|
78
102
|
send({
|
|
79
103
|
type: 'error',
|
|
@@ -107,7 +131,9 @@ async function run(request: BehaviorThreadRunRequest | BehaviorThreadStopRequest
|
|
|
107
131
|
// Cross-tick computations are isolated. The module and constructor are
|
|
108
132
|
// preloaded once per slot, but no mutable behavior `this` state crosses
|
|
109
133
|
// frame boundaries or becomes dependent on random slot assignment.
|
|
110
|
-
const instance: ClawclawBehavior =
|
|
134
|
+
const instance: ClawclawBehavior = execution === 'serial'
|
|
135
|
+
? serialInstance ??= new loaded.BehaviorClass()
|
|
136
|
+
: new loaded.BehaviorClass();
|
|
111
137
|
if (!instance || typeof instance.decide !== 'function') {
|
|
112
138
|
throw new Error('behavior_thread_instance_invalid');
|
|
113
139
|
}
|
|
@@ -119,7 +145,6 @@ async function run(request: BehaviorThreadRunRequest | BehaviorThreadStopRequest
|
|
|
119
145
|
: { observationSnapshot: request.observationSnapshot }),
|
|
120
146
|
});
|
|
121
147
|
try {
|
|
122
|
-
if (execution !== 'parallel') throw new Error('behavior_thread_decide_not_available');
|
|
123
148
|
const result = normalizeBehaviorDecision(await instance.decide(input, api), { finite: false });
|
|
124
149
|
send({
|
|
125
150
|
type: 'result',
|
|
@@ -127,6 +152,7 @@ async function run(request: BehaviorThreadRunRequest | BehaviorThreadStopRequest
|
|
|
127
152
|
result,
|
|
128
153
|
queryCount,
|
|
129
154
|
durationMs: Math.round((performance.now() - startedAt) * 1_000) / 1_000,
|
|
155
|
+
...diagnosticMemory(),
|
|
130
156
|
});
|
|
131
157
|
} finally {
|
|
132
158
|
invocationActive = false;
|
|
@@ -141,6 +167,7 @@ port.on('message', (request: BehaviorThreadRunRequest | BehaviorThreadStopReques
|
|
|
141
167
|
...(typeof request?.requestId === 'string' ? { requestId: request.requestId } : {}),
|
|
142
168
|
code: errorCode(error, 'BEHAVIOR_THREAD_OPERATION_FAILED'),
|
|
143
169
|
message: errorMessage(error),
|
|
170
|
+
...diagnosticMemory(),
|
|
144
171
|
});
|
|
145
172
|
});
|
|
146
173
|
});
|
|
@@ -149,4 +176,10 @@ send({
|
|
|
149
176
|
type: 'ready',
|
|
150
177
|
behaviorId: data.source.id,
|
|
151
178
|
execution,
|
|
179
|
+
...(preparation === undefined ? {} : {
|
|
180
|
+
manifest: preparation.manifest,
|
|
181
|
+
input: preparation.input,
|
|
182
|
+
timing: preparation.timing,
|
|
183
|
+
}),
|
|
184
|
+
...diagnosticMemory(),
|
|
152
185
|
});
|
|
@@ -5,13 +5,14 @@ import {
|
|
|
5
5
|
type LoadedClawclawBehaviorModule,
|
|
6
6
|
} from './behavior-module-loader.js';
|
|
7
7
|
import {
|
|
8
|
-
prepareClawclawBehavior,
|
|
9
8
|
type PreparedClawclawBehavior,
|
|
10
9
|
} from './behavior-normalization.js';
|
|
11
10
|
import {
|
|
12
11
|
behaviorSourceCompatibilityError,
|
|
13
12
|
type ResolvedClawclawBehaviorSource,
|
|
14
13
|
} from './behavior-source.js';
|
|
14
|
+
import { prepareBehaviorInWorker } from './thread-behavior-executor.js';
|
|
15
|
+
import { deepFreezeBehaviorValue } from './behavior-manifest.js';
|
|
15
16
|
|
|
16
17
|
export interface ClawclawBehaviorCatalogEntry {
|
|
17
18
|
readonly apiVersion: number | null;
|
|
@@ -93,12 +94,15 @@ export class ClawclawBehaviorCatalog {
|
|
|
93
94
|
async prepare(id: string, input: unknown): Promise<PreparedClawclawBehavior> {
|
|
94
95
|
const source = this.sources.get(id);
|
|
95
96
|
if (!source) throw new Error(`Unknown behavior module '${id}'.`);
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
loaded.manifest,
|
|
100
|
-
input,
|
|
101
|
-
loaded.source ?? source,
|
|
97
|
+
const prepared = await prepareBehaviorInWorker(
|
|
98
|
+
source,
|
|
99
|
+
input as Readonly<Record<string, unknown>>,
|
|
102
100
|
);
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
manifest: deepFreezeBehaviorValue(structuredClone(prepared.manifest)),
|
|
103
|
+
input: deepFreezeBehaviorValue(structuredClone(prepared.input)),
|
|
104
|
+
source: prepared.source,
|
|
105
|
+
...(prepared.timing === undefined ? {} : { timing: prepared.timing }),
|
|
106
|
+
});
|
|
103
107
|
}
|
|
104
108
|
}
|
|
@@ -13,8 +13,9 @@ const MIN_RUNTIME_MS = 1_000;
|
|
|
13
13
|
const MAX_RUNTIME_MS = 120_000;
|
|
14
14
|
|
|
15
15
|
export interface PreparedClawclawBehavior {
|
|
16
|
-
|
|
17
|
-
readonly
|
|
16
|
+
/** Owner-local instance exists only for explicitly serial trusted execution. */
|
|
17
|
+
readonly module?: ClawclawBehavior;
|
|
18
|
+
readonly createModule?: () => ClawclawBehavior;
|
|
18
19
|
readonly manifest: Readonly<ClawclawBehaviorManifest>;
|
|
19
20
|
readonly input: Readonly<Record<string, unknown>>;
|
|
20
21
|
/** Static source identity required only by isolated parallel execution. */
|
|
@@ -116,6 +116,7 @@ export interface ClawclawBehaviorPluginOptions {
|
|
|
116
116
|
readonly timeoutMs?: number;
|
|
117
117
|
readonly decisionTimeoutMs?: number;
|
|
118
118
|
readonly catchUp?: RuntimeEpochCatchUp;
|
|
119
|
+
readonly diagnosticsEnabled?: boolean;
|
|
119
120
|
readonly now?: () => number;
|
|
120
121
|
readonly createActivationId?: () => string;
|
|
121
122
|
}
|
|
@@ -279,6 +280,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
279
280
|
readonly capabilities = { uses: [CLAWCLAW_ACTION_CAPABILITY] } as const;
|
|
280
281
|
readonly catchUp: RuntimeEpochCatchUp;
|
|
281
282
|
readonly maxInFlight = 10;
|
|
283
|
+
readonly maxPendingEpochs = 10;
|
|
282
284
|
readonly timeoutMs: number;
|
|
283
285
|
readonly failurePolicy = 'failed_completion' as const;
|
|
284
286
|
|
|
@@ -287,6 +289,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
287
289
|
private readonly now: () => number;
|
|
288
290
|
private readonly createActivationId: () => string;
|
|
289
291
|
private readonly decisionTimeoutMs: number;
|
|
292
|
+
private readonly diagnosticsEnabled: boolean;
|
|
290
293
|
private readonly observationMemory = new ClawclawBehaviorObservationMemory();
|
|
291
294
|
private readonly runtimes = new Map<string, ActivationRuntime>();
|
|
292
295
|
private readonly delegatedShorts = new Map<string, Readonly<DelegatedShort>>();
|
|
@@ -323,6 +326,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
323
326
|
this.now = options.now ?? Date.now;
|
|
324
327
|
this.createActivationId = options.createActivationId ?? randomUUID;
|
|
325
328
|
this.decisionTimeoutMs = options.decisionTimeoutMs ?? DEFAULT_DECISION_TIMEOUT_MS;
|
|
329
|
+
this.diagnosticsEnabled = options.diagnosticsEnabled === true;
|
|
326
330
|
this.after = [...(options.after ?? [
|
|
327
331
|
CLAWCLAW_ACTION_PLUGIN_ID,
|
|
328
332
|
CLAWCLAW_DERIVED_EVENT_PLUGIN_ID,
|
|
@@ -330,7 +334,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
330
334
|
CLAWCLAW_PERCEPTION_PLUGIN_ID,
|
|
331
335
|
CLAWCLAW_TRIGGER_PLUGIN_ID,
|
|
332
336
|
])];
|
|
333
|
-
this.catchUp = options.catchUp ?? '
|
|
337
|
+
this.catchUp = options.catchUp ?? 'every_epoch';
|
|
334
338
|
this.timeoutMs = options.timeoutMs ?? Math.max(this.decisionTimeoutMs + 1_000, 5_000);
|
|
335
339
|
const eventTypes = new Set([
|
|
336
340
|
...CLAWCLAW_BEHAVIOR_LIFECYCLE_EVENT_TYPES,
|
|
@@ -363,6 +367,23 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
363
367
|
});
|
|
364
368
|
}
|
|
365
369
|
|
|
370
|
+
diagnosticSnapshot(): Readonly<Record<string, unknown>> {
|
|
371
|
+
const status = this.status();
|
|
372
|
+
return Object.freeze({
|
|
373
|
+
running: status.running,
|
|
374
|
+
current_activation_id: status.current?.activationId ?? null,
|
|
375
|
+
current_behavior_id: status.current?.behaviorId ?? null,
|
|
376
|
+
current_layer: status.current?.layer ?? null,
|
|
377
|
+
runtime_count: this.runtimes.size,
|
|
378
|
+
runtimes: [...this.runtimes.values()].map((runtime) => ({
|
|
379
|
+
activation_id: runtime.activationId,
|
|
380
|
+
behavior_id: runtime.behaviorId,
|
|
381
|
+
instance_generation: runtime.instanceGeneration ?? null,
|
|
382
|
+
runner: runtime.runner?.diagnosticSnapshot() ?? null,
|
|
383
|
+
})),
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
|
|
366
387
|
setIdleMs(idleMs: number): Promise<void> {
|
|
367
388
|
return this.enqueueControl(async () => {
|
|
368
389
|
await this.applyTransitions(this.manager.setIdleMs(idleMs, this.now()));
|
|
@@ -609,20 +630,11 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
609
630
|
return noDecision('cadence_skip', 'cadence_skip');
|
|
610
631
|
}
|
|
611
632
|
const observation = this.observationMemory.capture(frame);
|
|
612
|
-
if (runtime.runner.execution === 'serial') {
|
|
613
|
-
return {
|
|
614
|
-
outcome: 'processed',
|
|
615
|
-
proposal: {
|
|
616
|
-
...base,
|
|
617
|
-
kind: 'serial',
|
|
618
|
-
eventIds: eventIds(frame),
|
|
619
|
-
observationRevision: observation.revision,
|
|
620
|
-
} satisfies BehaviorComputationProposal,
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
633
|
try {
|
|
624
634
|
const decision = await this.withDecisionTimeout(
|
|
625
|
-
signal => runtime.runner!.
|
|
635
|
+
signal => runtime.runner!.execution === 'serial'
|
|
636
|
+
? runtime.runner!.decideSerial(frame, signal, observation)
|
|
637
|
+
: runtime.runner!.decideParallel(frame, signal, observation),
|
|
626
638
|
context.signal,
|
|
627
639
|
current.activationId,
|
|
628
640
|
);
|
|
@@ -646,7 +658,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
646
658
|
outcome: 'processed',
|
|
647
659
|
proposal: {
|
|
648
660
|
...base,
|
|
649
|
-
kind:
|
|
661
|
+
kind: runtime.runner.execution,
|
|
650
662
|
eventIds: eventIds(frame),
|
|
651
663
|
observationRevision: observation.revision,
|
|
652
664
|
decision,
|
|
@@ -860,41 +872,20 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
860
872
|
|
|
861
873
|
const observation = this.observationMemory.snapshot(frame.epochId)
|
|
862
874
|
?? this.observationMemory.capture(frame);
|
|
863
|
-
|
|
864
|
-
const
|
|
865
|
-
|| !sameStrings(computed.eventIds ?? [], eventIds(frame))
|
|
875
|
+
const decision = computed.decision;
|
|
876
|
+
const proposalIsStale = !sameStrings(computed.eventIds ?? [], eventIds(frame))
|
|
866
877
|
|| computed.observationRevision !== observation.revision;
|
|
867
|
-
if (
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
const failure = errorCode(error, 'BEHAVIOR_DECIDE_FAILED');
|
|
879
|
-
const resolved = runtime.decisionHistory.fail(frame.epochId, failure);
|
|
880
|
-
if (resolved) {
|
|
881
|
-
const history = runtime.decisionHistory.snapshot();
|
|
882
|
-
context.diagnostic('behavior.decision_history.frame_resolved', {
|
|
883
|
-
behavior_id: current.behaviorId,
|
|
884
|
-
activation_id: current.activationId,
|
|
885
|
-
instance_generation: current.instanceGeneration,
|
|
886
|
-
epoch_id: frame.epochId,
|
|
887
|
-
tick: resolved.tick,
|
|
888
|
-
status: resolved.status,
|
|
889
|
-
reason: failure,
|
|
890
|
-
action_count: 0,
|
|
891
|
-
retained_frame_count: history.length,
|
|
892
|
-
pending_frame_count: history.filter((entry) => entry.status === 'pending').length,
|
|
893
|
-
}, 'debug');
|
|
894
|
-
}
|
|
895
|
-
await this.failCurrent(current, failure, now);
|
|
896
|
-
return this.output(cursor);
|
|
897
|
-
}
|
|
878
|
+
if (proposalIsStale) {
|
|
879
|
+
context.diagnostic('behavior.decision.stale_before_commit', {
|
|
880
|
+
behavior_id: current.behaviorId,
|
|
881
|
+
activation_id: current.activationId,
|
|
882
|
+
instance_generation: current.instanceGeneration,
|
|
883
|
+
epoch_id: frame.epochId,
|
|
884
|
+
tick: frame.gameState.tick,
|
|
885
|
+
event_input_changed: !sameStrings(computed.eventIds ?? [], eventIds(frame)),
|
|
886
|
+
observation_revision_changed: computed.observationRevision !== observation.revision,
|
|
887
|
+
}, 'debug');
|
|
888
|
+
return this.output(undefined);
|
|
898
889
|
}
|
|
899
890
|
if (!decision || !this.manager.isCurrent(current.activationId, current.instanceGeneration)) {
|
|
900
891
|
return this.output(undefined);
|
|
@@ -1253,7 +1244,9 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
1253
1244
|
prepared = this.fallbackPreparedUsed
|
|
1254
1245
|
? Object.freeze({
|
|
1255
1246
|
...this.fallbackPrepared,
|
|
1256
|
-
|
|
1247
|
+
...(this.fallbackPrepared.createModule
|
|
1248
|
+
? { module: this.fallbackPrepared.createModule() }
|
|
1249
|
+
: {}),
|
|
1257
1250
|
})
|
|
1258
1251
|
: this.fallbackPrepared;
|
|
1259
1252
|
this.fallbackPreparedUsed = true;
|
|
@@ -1274,7 +1267,9 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
1274
1267
|
? runtime.prepared
|
|
1275
1268
|
: Object.freeze({
|
|
1276
1269
|
...runtime.prepared,
|
|
1277
|
-
|
|
1270
|
+
...(runtime.prepared.createModule
|
|
1271
|
+
? { module: runtime.prepared.createModule() }
|
|
1272
|
+
: {}),
|
|
1278
1273
|
});
|
|
1279
1274
|
runtime.runner = new ClawclawBehaviorRunner({
|
|
1280
1275
|
behaviorId: activation.behaviorId,
|
|
@@ -1282,6 +1277,7 @@ implements RuntimeEpochPlugin<Record<string, any>>, BehaviorReflexActivationDriv
|
|
|
1282
1277
|
instanceGeneration: activation.instanceGeneration,
|
|
1283
1278
|
prepared,
|
|
1284
1279
|
diagnostic: this.diagnostic,
|
|
1280
|
+
diagnosticsEnabled: this.diagnosticsEnabled,
|
|
1285
1281
|
isActive: () => this.manager.isCurrent(
|
|
1286
1282
|
activation.activationId,
|
|
1287
1283
|
activation.instanceGeneration,
|
|
@@ -22,6 +22,7 @@ export interface ClawclawBehaviorRunnerOptions {
|
|
|
22
22
|
severity?: RuntimeDiagnosticSeverity,
|
|
23
23
|
) => void;
|
|
24
24
|
readonly isActive: () => boolean;
|
|
25
|
+
readonly diagnosticsEnabled?: boolean;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
function errorCode(error: unknown): string {
|
|
@@ -47,7 +48,7 @@ export class ClawclawBehaviorRunner {
|
|
|
47
48
|
readonly instanceGeneration: number;
|
|
48
49
|
readonly prepared: PreparedClawclawBehavior;
|
|
49
50
|
|
|
50
|
-
private serialModule
|
|
51
|
+
private serialModule?: ClawclawBehavior;
|
|
51
52
|
private serialTail = Promise.resolve();
|
|
52
53
|
private readonly parallelLifecycle = new AbortController();
|
|
53
54
|
private parallelExecutor?: ThreadBehaviorExecutor;
|
|
@@ -71,12 +72,40 @@ export class ClawclawBehaviorRunner {
|
|
|
71
72
|
return this.prepared.manifest.everyTicks ?? 1;
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
diagnosticSnapshot(): Readonly<Record<string, unknown>> {
|
|
76
|
+
const pool = this.parallelExecutor?.snapshot();
|
|
77
|
+
return Object.freeze({
|
|
78
|
+
execution: this.execution,
|
|
79
|
+
every_ticks: this.everyTicks,
|
|
80
|
+
stopped: this.stopped,
|
|
81
|
+
worker_pool: pool
|
|
82
|
+
? {
|
|
83
|
+
state: pool.state,
|
|
84
|
+
worker_count: pool.workerCount,
|
|
85
|
+
idle_worker_count: pool.idleWorkerCount,
|
|
86
|
+
busy_worker_count: pool.busyWorkerCount,
|
|
87
|
+
queued_request_count: pool.queuedRequestCount,
|
|
88
|
+
in_flight_request_count: pool.inFlightRequestCount,
|
|
89
|
+
started_worker_count: pool.startedWorkerCount,
|
|
90
|
+
retired_worker_count: pool.retiredWorkerCount,
|
|
91
|
+
worker_heap_used_bytes: pool.workerHeapUsedBytes,
|
|
92
|
+
peak_worker_heap_used_bytes: pool.peakWorkerHeapUsedBytes,
|
|
93
|
+
}
|
|
94
|
+
: null,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
74
98
|
decideSerial(
|
|
75
99
|
input: Readonly<BehaviorInput>,
|
|
76
100
|
signal: AbortSignal,
|
|
77
101
|
observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>,
|
|
78
102
|
): Promise<BehaviorDecision> {
|
|
79
|
-
|
|
103
|
+
if (this.prepared.source) {
|
|
104
|
+
return this.enqueue(() => this.invokeThread(input, signal, observationSnapshot));
|
|
105
|
+
}
|
|
106
|
+
const module = this.serialModule;
|
|
107
|
+
if (!module) return Promise.reject(new Error('behavior_serial_module_missing'));
|
|
108
|
+
return this.enqueue(() => this.invoke(module, input, signal, observationSnapshot));
|
|
80
109
|
}
|
|
81
110
|
|
|
82
111
|
decideParallel(
|
|
@@ -84,7 +113,7 @@ export class ClawclawBehaviorRunner {
|
|
|
84
113
|
signal: AbortSignal,
|
|
85
114
|
observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>,
|
|
86
115
|
): Promise<BehaviorDecision> {
|
|
87
|
-
return this.
|
|
116
|
+
return this.invokeThread(input, signal, observationSnapshot);
|
|
88
117
|
}
|
|
89
118
|
|
|
90
119
|
stop(reason: 'switched' | 'scope_ended' | 'shutdown' = 'switched'): Promise<void> {
|
|
@@ -98,7 +127,7 @@ export class ClawclawBehaviorRunner {
|
|
|
98
127
|
return this.parallelStop;
|
|
99
128
|
}
|
|
100
129
|
|
|
101
|
-
private async
|
|
130
|
+
private async invokeThread(
|
|
102
131
|
input: Readonly<BehaviorInput>,
|
|
103
132
|
signal: AbortSignal,
|
|
104
133
|
observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>,
|
|
@@ -106,7 +135,7 @@ export class ClawclawBehaviorRunner {
|
|
|
106
135
|
if (this.stopped) throw new Error('behavior_runner_stopped');
|
|
107
136
|
if (signal.aborted) throw abortError(signal);
|
|
108
137
|
if (!this.options.isActive()) throw new Error('behavior_activation_superseded');
|
|
109
|
-
const executor = this.
|
|
138
|
+
const executor = this.ensureThreadExecutor();
|
|
110
139
|
await this.parallelReady;
|
|
111
140
|
if (this.stopped) throw new Error('behavior_runner_stopped');
|
|
112
141
|
if (signal.aborted) throw abortError(signal);
|
|
@@ -117,16 +146,17 @@ export class ClawclawBehaviorRunner {
|
|
|
117
146
|
return decision;
|
|
118
147
|
}
|
|
119
148
|
|
|
120
|
-
private
|
|
149
|
+
private ensureThreadExecutor(): ThreadBehaviorExecutor {
|
|
121
150
|
if (this.parallelExecutor) return this.parallelExecutor;
|
|
122
151
|
const source = this.prepared.source;
|
|
123
|
-
if (!source) throw new Error('
|
|
124
|
-
if (source.id !== this.behaviorId || source.execution !==
|
|
125
|
-
throw new Error('
|
|
152
|
+
if (!source) throw new Error('behavior_thread_source_missing');
|
|
153
|
+
if (source.id !== this.behaviorId || source.execution !== this.execution) {
|
|
154
|
+
throw new Error('behavior_thread_source_invalid');
|
|
126
155
|
}
|
|
127
156
|
const executor = new ThreadBehaviorExecutor({
|
|
128
157
|
source,
|
|
129
158
|
diagnostic: this.options.diagnostic,
|
|
159
|
+
diagnosticsEnabled: this.options.diagnosticsEnabled,
|
|
130
160
|
});
|
|
131
161
|
this.parallelExecutor = executor;
|
|
132
162
|
this.parallelReady = executor.start(this.parallelLifecycle.signal);
|