@myclaw163/clawclaw-cli 0.9.2 → 0.10.2
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/QUICKSTART.md +1 -1
- package/QUICKSTART_OTHER.md +13 -12
- package/README.md +7 -8
- package/bin/clawclaw-channel.mjs +2 -2
- package/bin/clawclaw-cli.mjs +2 -2
- package/docs/configuration.md +27 -101
- package/docs/custom-modules/README.md +23 -35
- package/docs/custom-modules/api-reference.md +95 -273
- package/docs/custom-modules/behavior.md +94 -78
- package/docs/custom-modules/examples/auto-escape.ts +5 -5
- package/docs/custom-modules/examples/go-to-point.ts +11 -11
- package/docs/custom-modules/examples/nearby-reflex.ts +2 -2
- package/docs/custom-modules/examples/task-reminder.ts +4 -5
- package/docs/custom-modules/perception.md +42 -603
- package/docs/custom-modules/skill.md +12 -10
- package/docs/desktop-event-push.md +3 -3
- package/docs/runtime/README.md +16 -17
- package/docs/runtime/agent-notifications.md +42 -12
- package/docs/runtime/behavior-runtime.md +47 -46
- package/docs/runtime/components-and-boundaries.md +70 -28
- package/docs/runtime/events-and-state.md +41 -18
- package/docs/runtime/framework-package.md +73 -0
- package/docs/runtime/official-perception-modules.md +5 -5
- package/docs/runtime/owner-lifecycle-and-observability.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +8 -9
- package/docs/user-guide/README.md +10 -7
- package/docs/user-guide/api-reference.md +81 -164
- package/docs/user-guide/behavior.md +84 -68
- package/docs/user-guide/installation.md +7 -7
- package/docs/user-guide/perception.md +42 -586
- package/docs/user-guide/skill.md +12 -10
- package/package.json +24 -9
- package/scripts/check-framework-boundaries.mjs +27 -0
- package/scripts/check-skill-command-surface.mjs +20 -23
- package/scripts/enable_ccl_permissions.cjs +4 -0
- package/scripts/install-local-runtime.mjs +35 -0
- package/scripts/postinstall.mjs +1 -1
- package/scripts/qa-install.mjs +105 -0
- package/scripts/sync-bundled-skill.mjs +35 -28
- package/skills/clawclaw/SKILL.md +53 -57
- package/skills/clawclaw/references/BEHAVIORS.md +43 -83
- package/skills/clawclaw/references/CHANNELS.md +2 -2
- package/skills/clawclaw/references/COMMANDS.md +62 -103
- package/skills/clawclaw/references/HUB.md +2 -2
- package/skills/clawclaw/references/STREAM.md +8 -8
- package/skills/clawclaw/references/TACTICS.md +1 -1
- package/skills/clawclaw-creator/SKILL.md +82 -0
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +95 -79
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-MODULE-API.md +95 -180
- package/skills/{clawclaw → clawclaw-creator}/references/CUSTOM-MODULES.md +26 -24
- package/skills/clawclaw-creator/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +126 -0
- package/skills/clawclaw-creator/references/CUSTOM-PERSONA-DEVELOPMENT.md +29 -0
- package/skills/clawclaw-creator/references/CUSTOM-SKILL-DEVELOPMENT.md +69 -0
- package/skills/clawclaw-creator/references/SYNC-AND-REPORT.md +36 -0
- package/src/channel/claude-channel-ipc-server.ts +1 -0
- package/src/cli.ts +4 -2
- package/src/commands/behavior.ts +26 -10
- package/src/commands/capability.ts +17 -14
- package/src/commands/load.ts +5 -3
- package/src/commands/perception.ts +5 -0
- package/src/commands/skill.ts +34 -29
- package/src/commands/strategy.ts +2 -0
- package/src/commands/upgrade.ts +2 -2
- package/src/lib/agent-workspace-skills.ts +20 -13
- package/src/lib/capability-cloud-sync.ts +2 -1
- package/src/lib/capability-discovery.ts +2 -1
- package/src/lib/capability-load-diagnostics.ts +3 -3
- package/src/lib/capability-workspace.ts +2 -1
- package/src/lib/game-client.ts +7 -1
- package/src/lib/package-skills.ts +4 -0
- package/src/lib/perception-config.ts +18 -6
- package/src/lib/runtime-cadence-config.ts +16 -0
- package/src/lib/skill-host.ts +1 -1
- package/src/lib/workspace-runtime-lock.ts +3 -46
- package/src/pipeline/canonical-event-presentation.ts +23 -124
- package/src/pipeline/event-format.ts +12 -21
- package/src/pipeline/event-hints.ts +6 -6
- package/src/pipeline/game-sync-event-normalization.ts +57 -63
- package/src/runtime/clawclaw/behavior-decision.ts +7 -3
- package/src/runtime/clawclaw/behavior-input.ts +8 -4
- package/src/runtime/clawclaw/behavior-observation-memory.ts +3 -2
- package/src/runtime/clawclaw/notification-policy.ts +42 -0
- package/src/runtime/clawclaw/notification-turn.ts +19 -0
- package/src/runtime/clawclaw/observation-frame-projector.ts +3 -3
- package/src/runtime/clawclaw/owner.ts +47 -9
- package/src/runtime/clawclaw/perception-module-manifest.ts +8 -3
- package/src/runtime/clawclaw/perception-strategy-names.ts +17 -0
- package/src/runtime/clawclaw/persistence-compatibility.ts +36 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +3 -3
- package/src/runtime/clawclaw/plugins/assembly.ts +7 -9
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -495
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +17 -190
- package/src/runtime/clawclaw/plugins/behavior/behavior-instance-snapshot.ts +1 -123
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +126 -1779
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +13 -276
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-worker-bridge.ts +8 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +3 -180
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +13 -972
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +8 -109
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +2 -2
- package/src/runtime/clawclaw/plugins/derived-event/reminder-plugin.ts +27 -17
- package/src/runtime/clawclaw/plugins/derived-event/reminder-runner.ts +1 -0
- package/src/runtime/clawclaw/plugins/derived-event/state-change-rules.ts +3 -3
- package/src/runtime/clawclaw/plugins/derived-event/task-change-tracker.ts +1 -259
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +4 -4
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +9 -97
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +8 -343
- package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-perception-report-adapter.ts +15 -20
- package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-runtime-report-adapter.ts +59 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +60 -572
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +1 -266
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +6 -161
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +4 -23
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +49 -752
- package/src/runtime/clawclaw/plugins/perception/map-concurrent.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/notification-content.ts +60 -0
- package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +125 -297
- package/src/runtime/clawclaw/plugins/perception/perception-module-runner.ts +7 -56
- package/src/runtime/clawclaw/plugins/perception/perception-module-worker.ts +11 -33
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +25 -10
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +12 -6
- package/src/runtime/clawclaw/plugins/perception/report-content.ts +50 -0
- package/src/runtime/clawclaw/plugins/perception/task-baseline.ts +29 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +6 -0
- package/src/runtime/clawclaw/plugins/system-perception/rule-runner.ts +107 -0
- package/src/runtime/clawclaw/plugins/system-perception/system-perceptions.ts +107 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +8 -3
- package/src/runtime/clawclaw/reaction-behavior-resolution.ts +1 -23
- package/src/runtime/clawclaw/reminder-manifest.ts +5 -2
- package/src/runtime/clawclaw/runtime-cadence.ts +24 -0
- package/src/runtime/clawclaw/runtime-state.ts +11 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/strategy-memory-rpc.ts +1 -63
- package/src/runtime/clawclaw/strategy-memory.ts +1 -77
- package/src/runtime/clawclaw-event-catalog.ts +12 -12
- package/src/runtime/framework/action-diagnostics.ts +3 -83
- package/src/runtime/framework/capability-registry.ts +2 -395
- package/src/runtime/framework/contracts.ts +2 -144
- package/src/runtime/framework/control/control-scheduler.ts +2 -1268
- package/src/runtime/framework/control/domain-set.ts +2 -81
- package/src/runtime/framework/control/index.ts +1 -3
- package/src/runtime/framework/control/types.ts +2 -191
- package/src/runtime/framework/control-coordinator.ts +2 -11
- package/src/runtime/framework/events/event-catalog.ts +2 -74
- package/src/runtime/framework/events/event-delivery-service.ts +2 -200
- package/src/runtime/framework/events/event-ingress.ts +2 -99
- package/src/runtime/framework/events/event-repository.ts +2 -82
- package/src/runtime/framework/events/index.ts +2 -7
- package/src/runtime/framework/events/memory-event-repository.ts +2 -1100
- package/src/runtime/framework/events/runtime-resume.ts +2 -20
- package/src/runtime/framework/events/sqlite-event-repository.ts +5 -2280
- package/src/runtime/framework/events/test-helpers.ts +1 -1
- package/src/runtime/framework/events/types.ts +2 -312
- package/src/runtime/framework/index.ts +3 -7
- package/src/runtime/framework/load-node-sqlite.ts +2 -17
- package/src/runtime/framework/mailbox/coalescer.ts +2 -83
- package/src/runtime/framework/mailbox/host-notification-sink.ts +2 -44
- package/src/runtime/framework/mailbox/identity.ts +2 -38
- package/src/runtime/framework/mailbox/index.ts +1 -7
- package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +2 -282
- package/src/runtime/framework/mailbox/mailbox-repository.ts +2 -44
- package/src/runtime/framework/mailbox/notification-projector.ts +2 -65
- package/src/runtime/framework/mailbox/types.ts +2 -145
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +2 -154
- package/src/runtime/framework/modules/runtime-module-loader.ts +7 -51
- package/src/runtime/framework/modules/runtime-module-output.ts +2 -24
- package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +3 -23
- package/src/runtime/framework/participation-manager.ts +2 -253
- package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +2 -77
- package/src/runtime/framework/persistence/memory-first-persistence.ts +4 -489
- package/src/runtime/framework/plugins/contracts.ts +2 -396
- package/src/runtime/framework/plugins/index.ts +1 -4
- package/src/runtime/framework/plugins/runtime-event-identity.ts +2 -17
- package/src/runtime/framework/plugins/runtime-input-journal.ts +2 -866
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +2 -2071
- package/src/runtime/framework/runtime.ts +5 -1843
- package/src/runtime/framework/serial-task-queue.ts +2 -14
- package/src/runtime/framework/state/contracts.ts +2 -49
- package/src/runtime/framework/state/current-state-store.ts +2 -184
- package/src/runtime/framework/state/durable-agent-state-snapshot.ts +2 -209
- package/src/runtime/framework/state/freshness.ts +2 -72
- package/src/runtime/framework/state/history/contracts.ts +2 -158
- package/src/runtime/framework/state/history/history-writer.ts +2 -194
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +2 -230
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +2 -648
- package/src/runtime/framework/state/history/trajectory-projector.ts +2 -209
- package/src/runtime/framework/state/index.ts +1 -13
- package/src/runtime/framework/state/state-ingress.ts +2 -95
- package/src/runtime/framework/state/state-provider.ts +2 -62
- package/src/runtime/framework/state/types.ts +2 -155
- package/src/runtime/game-sync-ingress-protocol.ts +3 -2
- package/src/runtime/game-sync-ingress-worker.ts +7 -1
- package/src/runtime/game-sync-protocol.ts +64 -4
- package/src/runtime/host-monitor-rate-gate.ts +1 -123
- package/src/runtime/mailbox-host-relay.ts +1 -326
- package/src/runtime/observability/detail-capture.ts +4 -108
- package/src/runtime/observability/process-timing.ts +2 -59
- package/src/runtime/observability/recorder.ts +2 -238
- package/src/runtime/observability/redaction.ts +2 -181
- package/src/runtime/observability/reporter.ts +2 -154
- package/src/runtime/observability/sinks.ts +2 -135
- package/src/runtime/observability/types.ts +2 -92
- package/src/runtime/owner-control.ts +45 -320
- package/src/runtime/parent-process-watch.ts +1 -51
- package/src/runtime/process-identity.ts +1 -72
- package/src/runtime/threaded-ws-client.ts +11 -2
- package/src/runtime/ws-client.ts +6 -0
- package/src/sdk/behavior-plugin/index.ts +9 -3
- package/src/sdk/index.ts +40 -2
- package/src/sdk/module-input/index.ts +5 -0
- package/src/sdk/perception-plugin/index.ts +3 -1
- package/src/sdk/perception-plugin/module.ts +12 -2
- package/src/sdk/perception-plugin/reminder-rules.ts +8 -42
- package/src/sdk/perception-plugin/reminder.ts +2 -1
- package/src/sdk/perception-plugin/task-change-tracker.ts +258 -0
- package/src/sdk/strategy-memory.ts +1 -12
- package/src/sdk/strategy-plugin/index.ts +36 -0
- package/src/sdk/trigger-plugin/index.ts +1 -0
- package/docs/custom-modules/trigger.md +0 -295
- package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +0 -692
- package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +0 -67
|
@@ -1,197 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
BehaviorInput,
|
|
5
|
-
ClawclawBehavior,
|
|
6
|
-
} from '../../../../sdk/behavior-plugin/index.js';
|
|
1
|
+
import { runStrategyWorker } from '@myclaw163/game-agent-runtime/strategy/strategy-worker-host.js';
|
|
2
|
+
import type { BehaviorInput, BehaviorDecision, BehaviorReadApi } from '../../../../sdk/behavior-plugin/index.js';
|
|
7
3
|
import { createBehaviorReadApi } from '../../behavior-read-api.js';
|
|
8
4
|
import { normalizeBehaviorDecision } from '../../behavior-decision.js';
|
|
9
|
-
import {
|
|
10
|
-
resolveBehaviorSource,
|
|
11
|
-
validateClawclawBehaviorModule,
|
|
12
|
-
} from './behavior-module-loader.js';
|
|
13
|
-
import { deepFreezeBehaviorValue } from './behavior-manifest.js';
|
|
5
|
+
import { resolveBehaviorSource, validateClawclawBehaviorModule } from './behavior-module-loader.js';
|
|
14
6
|
import { prepareClawclawBehavior } from './behavior-normalization.js';
|
|
15
7
|
import type { ResolvedClawclawBehaviorSource } from './behavior-source.js';
|
|
16
8
|
import type { ClawclawBehaviorObservationSnapshot } from '../../behavior-observation-memory.js';
|
|
17
|
-
import { assertRuntimeModuleOutputSize } from '../../../framework/modules/runtime-module-output.js';
|
|
18
|
-
import { captureBehaviorInstance, restoreBehaviorInstance, type BehaviorInstanceSnapshot } from './behavior-instance-snapshot.js';
|
|
19
|
-
import { StrategyMemoryRpcClient } from '../../strategy-memory-rpc.js';
|
|
20
9
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
readonly operation: 'decide';
|
|
36
|
-
readonly frame: BehaviorInput;
|
|
37
|
-
readonly observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface BehaviorThreadStopRequest {
|
|
41
|
-
readonly type: 'stop';
|
|
42
|
-
readonly poolId: string;
|
|
43
|
-
readonly requestId: string;
|
|
44
|
-
readonly reason: 'switched' | 'scope_ended' | 'shutdown';
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function errorMessage(error: unknown): string {
|
|
48
|
-
return error instanceof Error ? error.message : String(error);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function errorCode(error: unknown, fallback: string): string {
|
|
52
|
-
const explicit = error && typeof error === 'object' && typeof (error as any).code === 'string'
|
|
53
|
-
? (error as any).code.trim()
|
|
54
|
-
: '';
|
|
55
|
-
if (/^[A-Za-z][A-Za-z0-9_.-]{0,119}$/.test(explicit)) return explicit;
|
|
56
|
-
const prefix = /^([A-Za-z][A-Za-z0-9_.-]{0,119})(?::|$)/.exec(errorMessage(error).trim())?.[1];
|
|
57
|
-
return prefix ?? fallback;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const port = parentPort;
|
|
61
|
-
if (!port) throw new Error('behavior_thread_parent_missing');
|
|
62
|
-
const data = workerData as Partial<BehaviorThreadWorkerData> | undefined;
|
|
63
|
-
if (
|
|
64
|
-
!data
|
|
65
|
-
|| typeof data.poolId !== 'string'
|
|
66
|
-
|| typeof data.slotId !== 'number'
|
|
67
|
-
|| !data.source
|
|
68
|
-
|| typeof data.sourceUrl !== 'string'
|
|
69
|
-
) throw new Error('behavior_thread_worker_data_invalid');
|
|
70
|
-
|
|
71
|
-
function send(value: Readonly<Record<string, unknown>>): void {
|
|
72
|
-
port!.postMessage({ ...value, poolId: data!.poolId!, slotId: data!.slotId! });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function diagnosticMemory(): Readonly<Record<string, number>> {
|
|
76
|
-
return data?.diagnosticsEnabled === true
|
|
77
|
-
? { heapUsedBytes: process.memoryUsage().heapUsed }
|
|
78
|
-
: {};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const memoryClient = new StrategyMemoryRpcClient(send);
|
|
82
|
-
let loaded: ReturnType<typeof validateClawclawBehaviorModule>;
|
|
83
|
-
let execution: ResolvedClawclawBehaviorSource['execution'];
|
|
84
|
-
let preparation: ReturnType<typeof prepareClawclawBehavior> | undefined;
|
|
85
|
-
let serialInstance: ClawclawBehavior | undefined;
|
|
86
|
-
try {
|
|
87
|
-
const behaviorModule = await import(data.sourceUrl) as Record<string, unknown>;
|
|
88
|
-
// The parent validates immediately before spawn; repeat after the complete
|
|
89
|
-
// module graph has loaded so an edit in that narrow window cannot enter the pool.
|
|
90
|
-
resolveBehaviorSource(data.source);
|
|
91
|
-
loaded = validateClawclawBehaviorModule(behaviorModule, data.source);
|
|
92
|
-
execution = loaded.manifest.execution ?? 'parallel';
|
|
93
|
-
if (data.prepareInput !== undefined) {
|
|
94
|
-
preparation = prepareClawclawBehavior(
|
|
95
|
-
loaded.BehaviorClass,
|
|
96
|
-
loaded.manifest,
|
|
97
|
-
data.prepareInput,
|
|
98
|
-
loaded.source,
|
|
99
|
-
);
|
|
100
|
-
assertRuntimeModuleOutputSize(preparation.input, 'behavior_normalize_output');
|
|
101
|
-
}
|
|
102
|
-
if (execution === 'serial' && data.prepareInput === undefined) {
|
|
103
|
-
serialInstance = new loaded.BehaviorClass();
|
|
104
|
-
}
|
|
105
|
-
} catch (error) {
|
|
106
|
-
send({
|
|
107
|
-
type: 'error',
|
|
108
|
-
code: errorCode(error, 'BEHAVIOR_THREAD_START_FAILED'),
|
|
109
|
-
message: errorMessage(error),
|
|
110
|
-
});
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function run(request: BehaviorThreadRunRequest | BehaviorThreadStopRequest): Promise<void> {
|
|
115
|
-
if (
|
|
116
|
-
!request
|
|
117
|
-
|| typeof request !== 'object'
|
|
118
|
-
|| request.poolId !== data!.poolId
|
|
119
|
-
|| typeof request.requestId !== 'string'
|
|
120
|
-
) throw new Error('behavior_thread_request_invalid');
|
|
121
|
-
if (request.type === 'stop') {
|
|
122
|
-
send({ type: 'stopped', requestId: request.requestId });
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
if (request.type !== 'run' || request.operation !== 'decide') {
|
|
126
|
-
throw new Error('behavior_thread_request_invalid');
|
|
127
|
-
}
|
|
128
|
-
if (!request.frame || typeof request.frame !== 'object') {
|
|
129
|
-
throw new Error('behavior_thread_frame_invalid');
|
|
130
|
-
}
|
|
131
|
-
const startedAt = performance.now();
|
|
132
|
-
let queryCount = 0;
|
|
133
|
-
let invocationActive = true;
|
|
134
|
-
const input = deepFreezeBehaviorValue(structuredClone(request.frame));
|
|
135
|
-
// Cross-tick computations are isolated. The module and constructor are
|
|
136
|
-
// preloaded once per slot, but no mutable behavior `this` state crosses
|
|
137
|
-
// frame boundaries or becomes dependent on random slot assignment.
|
|
138
|
-
const instance: ClawclawBehavior = execution === 'serial'
|
|
139
|
-
? serialInstance ??= new loaded.BehaviorClass()
|
|
140
|
-
: new loaded.BehaviorClass();
|
|
141
|
-
if (!instance || typeof instance.decide !== 'function') {
|
|
142
|
-
throw new Error('behavior_thread_instance_invalid');
|
|
143
|
-
}
|
|
144
|
-
if (loaded.manifest.stateMode === 'snapshot' && request.instanceSnapshot) {
|
|
145
|
-
restoreBehaviorInstance(instance, request.instanceSnapshot);
|
|
146
|
-
}
|
|
147
|
-
const api = createBehaviorReadApi(input, {
|
|
148
|
-
memory: memoryClient.bind(request.requestId, () => invocationActive),
|
|
149
|
-
onQuery: () => { queryCount += 1; },
|
|
150
|
-
isActive: () => invocationActive,
|
|
151
|
-
...(request.observationSnapshot === undefined
|
|
152
|
-
? {}
|
|
153
|
-
: { observationSnapshot: request.observationSnapshot }),
|
|
154
|
-
});
|
|
155
|
-
try {
|
|
156
|
-
const result = normalizeBehaviorDecision(await instance.decide(input, api), { finite: false });
|
|
157
|
-
const instanceSnapshot = loaded.manifest.stateMode === 'snapshot' ? captureBehaviorInstance(instance) : undefined;
|
|
158
|
-
if (instanceSnapshot) assertRuntimeModuleOutputSize({ result, instanceSnapshot }, 'behavior_snapshot_output');
|
|
159
|
-
send({
|
|
160
|
-
type: 'result',
|
|
161
|
-
requestId: request.requestId,
|
|
162
|
-
result,
|
|
163
|
-
...(instanceSnapshot === undefined ? {} : { instanceSnapshot }),
|
|
164
|
-
queryCount,
|
|
165
|
-
durationMs: Math.round((performance.now() - startedAt) * 1_000) / 1_000,
|
|
166
|
-
...diagnosticMemory(),
|
|
167
|
-
});
|
|
168
|
-
} finally {
|
|
169
|
-
invocationActive = false;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
let operationTail = Promise.resolve();
|
|
174
|
-
port.on('message', (request: BehaviorThreadRunRequest | BehaviorThreadStopRequest) => {
|
|
175
|
-
if (memoryClient.receive(request)) return;
|
|
176
|
-
operationTail = operationTail.then(() => run(request)).catch((error) => {
|
|
177
|
-
send({
|
|
178
|
-
type: 'error',
|
|
179
|
-
...(typeof request?.requestId === 'string' ? { requestId: request.requestId } : {}),
|
|
180
|
-
code: errorCode(error, 'BEHAVIOR_THREAD_OPERATION_FAILED'),
|
|
181
|
-
message: errorMessage(error),
|
|
182
|
-
...diagnosticMemory(),
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
send({
|
|
188
|
-
type: 'ready',
|
|
189
|
-
behaviorId: data.source.id,
|
|
190
|
-
execution,
|
|
191
|
-
...(preparation === undefined ? {} : {
|
|
192
|
-
manifest: preparation.manifest,
|
|
193
|
-
input: preparation.input,
|
|
194
|
-
timing: preparation.timing,
|
|
195
|
-
}),
|
|
196
|
-
...diagnosticMemory(),
|
|
10
|
+
// The framework owns Worker protocol, instances, snapshots and memory RPC.
|
|
11
|
+
// This entry point only adapts the game SDK and verifies the pinned source.
|
|
12
|
+
await runStrategyWorker<ResolvedClawclawBehaviorSource, BehaviorInput, BehaviorDecision, BehaviorReadApi, ClawclawBehaviorObservationSnapshot>({
|
|
13
|
+
async load(data) {
|
|
14
|
+
const module = await import(data.sourceUrl) as Record<string, unknown>;
|
|
15
|
+
resolveBehaviorSource(data.source);
|
|
16
|
+
const loaded = validateClawclawBehaviorModule(module, data.source);
|
|
17
|
+
return { execution: loaded.manifest.execution ?? 'parallel', stateMode: loaded.manifest.stateMode,
|
|
18
|
+
create: () => new loaded.BehaviorClass(),
|
|
19
|
+
prepare: input => prepareClawclawBehavior(loaded.BehaviorClass, loaded.manifest, input, loaded.source),
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
createApi: createBehaviorReadApi,
|
|
23
|
+
normalize: value => normalizeBehaviorDecision(value, {finite:false}) as BehaviorDecision,
|
|
197
24
|
});
|
|
@@ -1,123 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
type Value = null | undefined | boolean | number | string | bigint | { ref: number };
|
|
4
|
-
interface Node {
|
|
5
|
-
kind: 'object' | 'array' | 'map' | 'set' | 'date';
|
|
6
|
-
className?: string;
|
|
7
|
-
nullPrototype?: boolean;
|
|
8
|
-
entries?: Array<[string, Value]>;
|
|
9
|
-
items?: Value[];
|
|
10
|
-
pairs?: Array<[Value, Value]>;
|
|
11
|
-
time?: number;
|
|
12
|
-
}
|
|
13
|
-
export interface BehaviorInstanceSnapshot { readonly nodes: readonly Node[]; }
|
|
14
|
-
export const MAX_BEHAVIOR_SNAPSHOT_BYTES = 1024 * 1024;
|
|
15
|
-
|
|
16
|
-
/** One activation's completed computation state; never an Action receipt or commit checkpoint. */
|
|
17
|
-
export class BehaviorSnapshotStore {
|
|
18
|
-
generation = 0;
|
|
19
|
-
latest?: { epochId: number; value: BehaviorInstanceSnapshot };
|
|
20
|
-
publish(epochId: number, value: BehaviorInstanceSnapshot, generation: number): boolean {
|
|
21
|
-
if (generation !== this.generation || (this.latest && epochId <= this.latest.epochId)) return false;
|
|
22
|
-
this.latest = { epochId, value: structuredClone(value) };
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
reset(): void { this.generation++; this.latest = undefined; }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function unsupported(reason: string): never { throw new Error(`behavior_snapshot_unsupported:${reason}`); }
|
|
29
|
-
function className(value: object): string | undefined {
|
|
30
|
-
const proto = Object.getPrototypeOf(value);
|
|
31
|
-
if (proto === null || proto === Object.prototype) return undefined;
|
|
32
|
-
const constructor = Object.getOwnPropertyDescriptor(proto, 'constructor')?.value;
|
|
33
|
-
return typeof constructor === 'function' ? constructor.name : unsupported('prototype');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Capture data only. Methods stay on fresh constructor-created instances in each Worker. */
|
|
37
|
-
export function captureBehaviorInstance(instance: object): BehaviorInstanceSnapshot {
|
|
38
|
-
const nodes: Node[] = [];
|
|
39
|
-
const seen = new Map<object, number>();
|
|
40
|
-
let stringBytes = 0;
|
|
41
|
-
function encode(value: unknown, depth: number): Value {
|
|
42
|
-
if (depth > 128) unsupported('depth');
|
|
43
|
-
if (typeof value === 'string') {
|
|
44
|
-
stringBytes += Buffer.byteLength(value);
|
|
45
|
-
if (stringBytes > MAX_BEHAVIOR_SNAPSHOT_BYTES) throw new Error('behavior_snapshot_too_large');
|
|
46
|
-
}
|
|
47
|
-
if (value === null || value === undefined || ['boolean', 'number', 'string', 'bigint'].includes(typeof value)) return value as Value;
|
|
48
|
-
if (typeof value !== 'object') unsupported(typeof value);
|
|
49
|
-
const object = value as object;
|
|
50
|
-
const prior = seen.get(object);
|
|
51
|
-
if (prior !== undefined) return { ref: prior };
|
|
52
|
-
if (nodes.length >= 10_000) throw new Error('behavior_snapshot_too_large');
|
|
53
|
-
const ref = nodes.length;
|
|
54
|
-
const node: Node = { kind: 'object' };
|
|
55
|
-
seen.set(object, ref); nodes.push(node);
|
|
56
|
-
if (Array.isArray(object)) {
|
|
57
|
-
node.kind = 'array'; node.items = object.map(v => encode(v, depth + 1));
|
|
58
|
-
} else if (object instanceof Map) {
|
|
59
|
-
node.kind = 'map'; node.pairs = [...object].map(([k, v]) => [encode(k, depth + 1), encode(v, depth + 1)]);
|
|
60
|
-
} else if (object instanceof Set) {
|
|
61
|
-
node.kind = 'set'; node.items = [...object].map(v => encode(v, depth + 1));
|
|
62
|
-
} else if (object instanceof Date) {
|
|
63
|
-
node.kind = 'date'; node.time = object.getTime();
|
|
64
|
-
} else {
|
|
65
|
-
node.className = className(object);
|
|
66
|
-
node.nullPrototype = Object.getPrototypeOf(object) === null;
|
|
67
|
-
node.entries = [];
|
|
68
|
-
for (const key of Reflect.ownKeys(object)) {
|
|
69
|
-
if (typeof key !== 'string') unsupported('symbol_key');
|
|
70
|
-
const descriptor = Object.getOwnPropertyDescriptor(object, key)!;
|
|
71
|
-
if (!('value' in descriptor)) unsupported('accessor');
|
|
72
|
-
node.entries.push([key, encode(descriptor.value, depth + 1)]);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return { ref };
|
|
76
|
-
}
|
|
77
|
-
encode(instance, 0);
|
|
78
|
-
const snapshot = { nodes };
|
|
79
|
-
if (serialize(snapshot).byteLength > MAX_BEHAVIOR_SNAPSHOT_BYTES) throw new Error('behavior_snapshot_too_large');
|
|
80
|
-
return snapshot;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Custom helper classes must be created by the constructor so their methods are retained. */
|
|
84
|
-
export function restoreBehaviorInstance(instance: object, snapshot: BehaviorInstanceSnapshot): void {
|
|
85
|
-
if (!Array.isArray(snapshot.nodes) || snapshot.nodes.length === 0
|
|
86
|
-
|| snapshot.nodes.length > 10_000 || serialize(snapshot).byteLength > MAX_BEHAVIOR_SNAPSHOT_BYTES) {
|
|
87
|
-
throw new Error('behavior_snapshot_invalid');
|
|
88
|
-
}
|
|
89
|
-
const restored = new Map<number, any>();
|
|
90
|
-
function decode(value: Value, seed?: any): any {
|
|
91
|
-
if (value === null || typeof value !== 'object') return value;
|
|
92
|
-
if (restored.has(value.ref)) return restored.get(value.ref);
|
|
93
|
-
const node = snapshot.nodes[value.ref];
|
|
94
|
-
if (!node) throw new Error('behavior_snapshot_reference_invalid');
|
|
95
|
-
let result: any;
|
|
96
|
-
if (node.kind === 'array') result = [];
|
|
97
|
-
else if (node.kind === 'map') result = new Map();
|
|
98
|
-
else if (node.kind === 'set') result = new Set();
|
|
99
|
-
else if (node.kind === 'date') result = new Date(node.time!);
|
|
100
|
-
else if (node.className) {
|
|
101
|
-
if (!seed || typeof seed !== 'object' || className(seed) !== node.className) unsupported(`class_template:${node.className}`);
|
|
102
|
-
result = seed;
|
|
103
|
-
} else result = node.nullPrototype ? Object.create(null) : {};
|
|
104
|
-
restored.set(value.ref, result);
|
|
105
|
-
if (node.kind === 'object') {
|
|
106
|
-
const keys = new Set(node.entries!.map(([key]) => key));
|
|
107
|
-
for (const key of Object.getOwnPropertyNames(result)) if (!keys.has(key)) {
|
|
108
|
-
if (!Reflect.deleteProperty(result, key)) unsupported('nonconfigurable_field');
|
|
109
|
-
}
|
|
110
|
-
for (const [key, entry] of node.entries!) {
|
|
111
|
-
const initial = Object.getOwnPropertyDescriptor(result, key);
|
|
112
|
-
const decoded = decode(entry, initial && 'value' in initial ? initial.value : undefined);
|
|
113
|
-
Object.defineProperty(result, key, { value: decoded, enumerable: true, writable: true, configurable: true });
|
|
114
|
-
}
|
|
115
|
-
} else if (node.kind === 'array') node.items!.forEach((v, i) => { result[i] = decode(v, seed?.[i]); });
|
|
116
|
-
else if (node.kind === 'set') for (const v of node.items!) result.add(decode(v));
|
|
117
|
-
else if (node.kind === 'map') for (const [k, v] of node.pairs!) {
|
|
118
|
-
const key = decode(k); result.set(key, decode(v, seed instanceof Map ? seed.get(key) : undefined));
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
decode({ ref: 0 }, instance);
|
|
123
|
-
}
|
|
1
|
+
export * from '@myclaw163/game-agent-runtime/strategy/behavior-instance-snapshot.js';
|