@myclaw163/clawclaw-cli 0.7.16 → 0.8.1
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_HERMES.md +1 -1
- package/QUICKSTART_OPENCLAW.md +3 -11
- package/QUICKSTART_OTHER.md +8 -8
- package/README.md +18 -9
- package/bin/clawclaw-channel.mjs +13 -0
- package/docs/configuration.md +86 -33
- package/docs/custom-modules/README.md +148 -0
- package/docs/custom-modules/api-reference.md +767 -0
- package/docs/custom-modules/behavior.md +318 -0
- package/docs/custom-modules/perception.md +305 -0
- package/docs/custom-modules/trigger.md +292 -0
- package/docs/desktop-event-push.md +6 -4
- package/docs/runtime/README.md +28 -10
- package/docs/runtime/agent-notifications.md +48 -29
- package/docs/runtime/behavior-runtime.md +139 -0
- package/docs/runtime/components-and-boundaries.md +89 -173
- package/docs/runtime/events-and-state.md +55 -66
- package/docs/runtime/official-perception-modules.md +17 -9
- package/docs/runtime/owner-lifecycle-and-observability.md +62 -39
- package/docs/runtime/perception-replay.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +35 -37
- package/package.json +7 -4
- package/scripts/check-skill-command-surface.mjs +54 -10
- package/scripts/compare-perception-replays.test.ts +10 -10
- package/scripts/compare-perception-replays.ts +14 -10
- package/scripts/replay-perception.ts +3 -3
- package/scripts/watch-perception-events.test.ts +1 -1
- package/skills/clawclaw/SKILL.md +97 -59
- package/skills/clawclaw/references/BEHAVIORS.md +154 -0
- package/skills/clawclaw/references/CHANNELS.md +115 -0
- package/skills/clawclaw/references/CHATTERBOX.md +3 -2
- package/skills/clawclaw/references/COMMANDS.md +108 -58
- package/skills/clawclaw/references/GAME-MECHANICS.md +4 -3
- package/skills/clawclaw/references/HUB.md +27 -23
- package/skills/clawclaw/references/STREAM.md +18 -14
- package/skills/clawclaw/references/TACTICS.md +2 -2
- package/src/behaviors/official-finite/do-task.ts +141 -0
- package/src/behaviors/official-finite/evade.ts +78 -0
- package/src/behaviors/official-finite/find-player.ts +50 -0
- package/src/behaviors/official-finite/follow-player.ts +70 -0
- package/src/behaviors/official-finite/go-to.ts +69 -0
- package/src/behaviors/official-finite/hide-temporarily.ts +77 -0
- package/src/behaviors/official-finite/hold-position.ts +48 -0
- package/src/behaviors/official-finite/hunt-player.ts +47 -0
- package/src/behaviors/official-finite/index.test.ts +202 -0
- package/src/behaviors/official-finite/index.ts +47 -0
- package/src/behaviors/official-finite/inspect-corpse.ts +64 -0
- package/src/behaviors/official-finite/patrol-area.ts +80 -0
- package/src/behaviors/official-finite/report-corpse.ts +73 -0
- package/src/behaviors/official-finite/seek-kill-opportunity.ts +152 -0
- package/src/behaviors/official-finite/shared.ts +27 -0
- package/src/behaviors/official-finite/trigger-alarm.ts +53 -0
- package/src/channel/claude-channel-ipc-server.ts +295 -0
- package/src/channel/claude-channel-ipc.ts +152 -0
- package/src/channel/claude-channel-server.test.ts +202 -0
- package/src/channel/claude-channel-server.ts +221 -0
- package/src/cli.ts +4 -2
- package/src/commands/account.test.ts +17 -17
- package/src/commands/behavior.test.ts +158 -0
- package/src/commands/behavior.ts +443 -0
- package/src/commands/communication.test.ts +12 -0
- package/src/commands/communication.ts +6 -3
- package/src/commands/data.test.ts +1 -1
- package/src/commands/data.ts +1 -1
- package/src/commands/events-canonical-state.test.ts +221 -2
- package/src/commands/events.ts +205 -12
- package/src/commands/game/index.test.ts +172 -3
- package/src/commands/game/index.ts +69 -32
- package/src/commands/hub.test.ts +25 -14
- package/src/commands/hub.ts +15 -19
- package/src/commands/load.test.ts +228 -6
- package/src/commands/load.ts +102 -18
- package/src/commands/memory.ts +1 -1
- package/src/commands/peek.ts +1 -1
- package/src/commands/perception.test.ts +30 -1
- package/src/commands/perception.ts +15 -17
- package/src/commands/trigger.test.ts +140 -0
- package/src/commands/trigger.ts +314 -0
- package/src/lib/account-guidance.test.ts +4 -0
- package/src/lib/account-guidance.ts +1 -1
- package/src/lib/behavior-export.test.ts +282 -0
- package/src/lib/behavior-export.ts +514 -0
- package/src/lib/game-client.test.ts +4 -17
- package/src/lib/game-client.ts +1 -0
- package/src/lib/gameplay-config.test.ts +258 -37
- package/src/lib/gameplay-config.ts +175 -35
- package/src/lib/hub-install.test.ts +19 -41
- package/src/lib/hub-install.ts +10 -38
- package/src/lib/hub-reminder.ts +7 -9
- package/src/lib/init-command.ts +1 -1
- package/src/lib/official-resource-sync.test.ts +199 -0
- package/src/lib/official-resource-sync.ts +221 -0
- package/src/lib/perception-config.test.ts +9 -4
- package/src/lib/perception-config.ts +10 -13
- package/src/lib/perception-export.test.ts +145 -0
- package/src/lib/perception-export.ts +178 -0
- package/src/lib/trigger-config.test.ts +102 -0
- package/src/lib/trigger-config.ts +217 -0
- package/src/lib/user-data.test.ts +23 -6
- package/src/lib/user-data.ts +15 -123
- package/src/pipeline/canonical-event-presentation.test.ts +5 -5
- package/src/pipeline/canonical-event-presentation.ts +13 -5
- package/src/pipeline/event-format.test.ts +106 -3
- package/src/pipeline/event-format.ts +172 -4
- package/src/pipeline/event-hints.test.ts +57 -4
- package/src/pipeline/event-hints.ts +34 -8
- package/src/runtime/canonical-monitor.test.ts +2 -0
- package/src/runtime/canonical-monitor.ts +6 -2
- package/src/runtime/clawclaw/agent-event-policy.test.ts +23 -2
- package/src/runtime/clawclaw/agent-event-policy.ts +12 -5
- package/src/runtime/clawclaw/behavior-decision.test.ts +29 -0
- package/src/runtime/clawclaw/behavior-decision.ts +82 -0
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +175 -0
- package/src/runtime/clawclaw/behavior-input-fields.ts +270 -0
- package/src/runtime/clawclaw/behavior-input.test.ts +151 -0
- package/src/runtime/clawclaw/behavior-input.ts +70 -0
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +59 -0
- package/src/runtime/clawclaw/behavior-observation-memory.ts +142 -0
- package/src/runtime/clawclaw/behavior-read-api.test.ts +123 -0
- package/src/runtime/clawclaw/behavior-read-api.ts +158 -0
- package/src/runtime/clawclaw/{plugins/perception/visual-state-projector.ts → observation-frame-projector.ts} +44 -25
- package/src/runtime/clawclaw/owner.test.ts +187 -0
- package/src/runtime/clawclaw/owner.ts +431 -69
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +304 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +285 -41
- package/src/runtime/clawclaw/plugins/action/automated-action-normalization.ts +82 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +135 -29
- package/src/runtime/clawclaw/plugins/assembly.ts +54 -28
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +468 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +40 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +30 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +152 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +156 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +104 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-manifest.ts +173 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +182 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.ts +223 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +157 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +848 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +1347 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +212 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +174 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +621 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +195 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/index.ts +11 -0
- package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +766 -0
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +283 -0
- package/src/runtime/clawclaw/plugins/{strategy/thread-strategy-executor.ts → behavior/thread-behavior-executor.ts} +109 -121
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +80 -0
- package/src/runtime/clawclaw/plugins/behavior-input-source.ts +247 -0
- package/src/runtime/clawclaw/plugins/{strategy → game-query}/context-capabilities.ts +1 -1
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +13 -10
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +26 -14
- package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/index.ts +3 -1
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +239 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +100 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +346 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +230 -12
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +118 -90
- package/src/runtime/clawclaw/plugins/mailbox/consumer-ids.ts +5 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +1 -1
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +4 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +87 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +70 -12
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +94 -11
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1229 -345
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +479 -35
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -3
- package/src/runtime/clawclaw/plugins/perception/index.ts +1 -1
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +23 -13
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +11 -9
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +39 -38
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +92 -0
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +248 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +39 -37
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +22 -21
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +15 -9
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +3 -3
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +33 -18
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +22 -16
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +18 -12
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +14 -10
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +20 -14
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +49 -34
- package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +126 -20
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +24 -3
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +59 -46
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +27 -5
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +381 -49
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +267 -245
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +98 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +89 -16
- package/src/runtime/clawclaw/plugins/perception/perception-test-fixtures.ts +34 -0
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +27 -15
- package/src/runtime/clawclaw/plugins/perception/replay.ts +10 -5
- package/src/runtime/clawclaw/plugins/perception/shared/math.ts +1 -107
- package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +7 -10
- package/src/runtime/clawclaw/plugins/plugin-api-version.ts +112 -0
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +388 -559
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +56 -5
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +135 -26
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +249 -16
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +99 -2
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +212 -0
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.ts +259 -0
- package/src/runtime/clawclaw/plugins/trigger/contracts.ts +97 -0
- package/src/runtime/clawclaw/plugins/trigger/index.ts +4 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +118 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +548 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +655 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +169 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.ts +457 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/spatial-api.ts +76 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +46 -1
- package/src/runtime/clawclaw-event-catalog.ts +40 -3
- package/src/runtime/framework/capability-registry.test.ts +196 -55
- package/src/runtime/framework/capability-registry.ts +99 -17
- package/src/runtime/framework/contracts.ts +30 -4
- package/src/runtime/framework/control/control-scheduler.ts +13 -5
- package/src/runtime/framework/control/types.ts +11 -4
- package/src/runtime/framework/control-coordinator.test.ts +95 -80
- package/src/runtime/framework/events/event-delivery-service.ts +4 -2
- package/src/runtime/framework/events/event-ingress.test.ts +25 -0
- package/src/runtime/framework/events/event-ingress.ts +2 -1
- package/src/runtime/framework/events/event-repository.ts +24 -2
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +124 -8
- package/src/runtime/framework/events/memory-event-repository.ts +92 -28
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +604 -2
- package/src/runtime/framework/events/sqlite-event-repository.ts +531 -21
- package/src/runtime/framework/events/types.ts +24 -8
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +38 -1
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +233 -7
- package/src/runtime/framework/modules/runtime-module-loader.ts +69 -4
- package/src/runtime/framework/plugins/contracts.ts +12 -18
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +81 -71
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +38 -20
- package/src/runtime/framework/runtime.ts +20 -23
- package/src/runtime/host-monitor-rate-gate.test.ts +73 -0
- package/src/runtime/host-monitor-rate-gate.ts +123 -0
- package/src/runtime/mailbox-host-relay.test.ts +45 -0
- package/src/runtime/mailbox-host-relay.ts +25 -0
- package/src/runtime/observability/factory.ts +4 -4
- package/src/runtime/observability/observability.test.ts +25 -3
- package/src/runtime/observability/reporter.ts +1 -1
- package/src/runtime/observability/types.ts +2 -2
- package/src/runtime/owner-control.ts +10 -2
- package/src/runtime/runtime-logger.test.ts +1 -1
- package/src/runtime/runtime-logger.ts +4 -6
- package/src/runtime/runtime.test.ts +88 -22
- package/src/sdk/action.ts +2 -2
- package/src/sdk/behavior-plugin/index.test.ts +74 -0
- package/src/sdk/behavior-plugin/index.ts +335 -0
- package/src/sdk/behavior-toolkit/corpse-search.ts +87 -0
- package/src/sdk/behavior-toolkit/escape.ts +40 -0
- package/src/sdk/behavior-toolkit/index.ts +7 -0
- package/src/sdk/behavior-toolkit/kill-flow.ts +299 -0
- package/src/sdk/behavior-toolkit/model.ts +263 -0
- package/src/sdk/behavior-toolkit/move-gate.ts +52 -0
- package/src/sdk/behavior-toolkit/patrol.ts +53 -0
- package/src/sdk/behavior-toolkit/search.ts +102 -0
- package/src/sdk/index.test.ts +45 -0
- package/src/sdk/index.ts +67 -73
- package/src/sdk/module-input/index.test.ts +49 -0
- package/src/sdk/module-input/index.ts +229 -0
- package/src/sdk/perception-plugin/index.test.ts +2 -2
- package/src/sdk/perception-plugin/index.ts +51 -142
- package/src/sdk/perception-plugin/math.ts +107 -0
- package/src/sdk/trigger-plugin/index.test.ts +41 -0
- package/src/sdk/trigger-plugin/index.ts +128 -0
- package/src/strategies/avoid-lone.ts +21 -10
- package/src/strategies/avoid-players.ts +26 -16
- package/src/strategies/corpse-patrol.ts +25 -14
- package/src/strategies/crab-sabotage.ts +20 -9
- package/src/strategies/find-player.ts +33 -21
- package/src/strategies/game-utils.ts +1 -1
- package/src/strategies/goals/crab-sabotage-top.test.ts +145 -0
- package/src/strategies/goals/crab-sabotage-top.ts +32 -69
- package/src/strategies/hide.ts +25 -15
- package/src/strategies/kill-frenzy.ts +21 -10
- package/src/strategies/kill-lone.ts +27 -17
- package/src/strategies/kill-target.ts +33 -22
- package/src/strategies/lone-kill-task.ts +20 -9
- package/src/strategies/move-room.ts +29 -18
- package/src/strategies/official-long-behavior.test.ts +89 -0
- package/src/strategies/paradise-fish.ts +29 -18
- package/src/strategies/pathfind/astar-path.test.ts +127 -2
- package/src/strategies/pathfind/astar-path.ts +136 -26
- package/src/strategies/patrol.ts +21 -10
- package/src/strategies/player-targets.test.ts +10 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +21 -10
- package/src/strategies/shrimp-memory.ts +30 -19
- package/src/strategies/social-task.ts +34 -23
- package/src/strategies/task-kill-report.ts +33 -22
- package/src/strategies/task-only.test.ts +99 -85
- package/src/strategies/task-only.ts +23 -12
- package/src/strategies/task-report.ts +25 -14
- package/src/strategies/types.ts +8 -8
- package/src/strategies/warrior-memory.ts +29 -19
- package/docs/runtime/strategy-runtime.md +0 -111
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +0 -502
- package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +0 -395
- package/skills/clawclaw/references/STRATEGIES.md +0 -76
- package/src/commands/strategy.test.ts +0 -209
- package/src/commands/strategy.ts +0 -242
- package/src/lib/strategy-export.test.ts +0 -406
- package/src/lib/strategy-export.ts +0 -381
- package/src/runtime/clawclaw/plugins/perception/official-modules.ts +0 -31
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +0 -177
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +0 -191
- package/src/runtime/clawclaw/plugins/strategy/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +0 -412
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +0 -80
- package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +0 -183
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +0 -29
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +0 -693
- package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +0 -57
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +0 -168
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +0 -2175
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1774
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +0 -153
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +0 -443
- package/src/sdk/strategy-plugin/index.test.ts +0 -958
- package/src/sdk/strategy-plugin/index.ts +0 -1350
|
@@ -5,17 +5,19 @@ import { performance } from 'node:perf_hooks';
|
|
|
5
5
|
import { Worker } from 'node:worker_threads';
|
|
6
6
|
import type { RuntimeDiagnosticSeverity } from '../../../framework/plugins/index.js';
|
|
7
7
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from '../../../../sdk/
|
|
8
|
+
BehaviorDecision,
|
|
9
|
+
BehaviorInput,
|
|
10
|
+
} from '../../../../sdk/behavior-plugin/index.js';
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
behaviorSourceUrl,
|
|
13
|
+
behaviorThreadLoaderConfig,
|
|
14
|
+
pinBehaviorSource,
|
|
15
|
+
resolveBehaviorSource,
|
|
16
|
+
} from './behavior-module-loader.js';
|
|
17
|
+
import type { ResolvedClawclawBehaviorSource } from './behavior-source.js';
|
|
18
|
+
import type { ClawclawBehaviorObservationSnapshot } from '../../behavior-observation-memory.js';
|
|
19
|
+
|
|
20
|
+
export const PARALLEL_BEHAVIOR_THREAD_POOL_SIZE = 10;
|
|
19
21
|
const INITIAL_WORKER_COUNT = 2;
|
|
20
22
|
const MAX_POOL_SIZE = 10;
|
|
21
23
|
const IDLE_SCALE_DOWN_MS = 30_000;
|
|
@@ -26,10 +28,10 @@ const REPLACEMENT_RETRY_MAX_MS = 5_000;
|
|
|
26
28
|
const WORKER_BOOTSTRAP = realpathSync(fileURLToPath(
|
|
27
29
|
new URL('../../../framework/modules/runtime-module-worker-bootstrap.mjs', import.meta.url),
|
|
28
30
|
));
|
|
29
|
-
const WORKER_HOST_URL = new URL('./
|
|
31
|
+
const WORKER_HOST_URL = new URL('./behavior-analysis-thread-host.ts', import.meta.url).href;
|
|
30
32
|
|
|
31
|
-
export interface
|
|
32
|
-
readonly source:
|
|
33
|
+
export interface ThreadBehaviorExecutorOptions {
|
|
34
|
+
readonly source: ResolvedClawclawBehaviorSource;
|
|
33
35
|
/** Maximum Worker count and in-flight request window. Tests may reduce it. */
|
|
34
36
|
readonly poolSize?: number;
|
|
35
37
|
/** How long an expanded, idle pool is retained before returning to two Workers. */
|
|
@@ -42,7 +44,7 @@ export interface ThreadStrategyExecutorOptions {
|
|
|
42
44
|
) => void;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
export interface
|
|
47
|
+
export interface ThreadBehaviorExecutorSnapshot {
|
|
46
48
|
readonly state: 'new' | 'starting' | 'running' | 'stopping' | 'stopped';
|
|
47
49
|
readonly workerCount: number;
|
|
48
50
|
readonly idleWorkerCount: number;
|
|
@@ -55,8 +57,9 @@ export interface ThreadStrategyExecutorSnapshot {
|
|
|
55
57
|
|
|
56
58
|
interface PendingRequest {
|
|
57
59
|
readonly requestId: string;
|
|
58
|
-
readonly operation: '
|
|
59
|
-
readonly frame:
|
|
60
|
+
readonly operation: 'decide';
|
|
61
|
+
readonly frame: BehaviorInput;
|
|
62
|
+
readonly observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>;
|
|
60
63
|
readonly signal: AbortSignal;
|
|
61
64
|
readonly enqueuedAt: number;
|
|
62
65
|
readonly resolve: (value: unknown) => void;
|
|
@@ -87,22 +90,14 @@ interface WorkerMessageBase {
|
|
|
87
90
|
|
|
88
91
|
interface WorkerReadyMessage extends WorkerMessageBase {
|
|
89
92
|
readonly type: 'ready';
|
|
90
|
-
readonly
|
|
91
|
-
readonly execution:
|
|
93
|
+
readonly behaviorId: string;
|
|
94
|
+
readonly execution: ResolvedClawclawBehaviorSource['execution'];
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
interface WorkerResultMessage extends WorkerMessageBase {
|
|
95
98
|
readonly type: 'result';
|
|
96
99
|
readonly requestId: string;
|
|
97
|
-
readonly result:
|
|
98
|
-
readonly queryCount: number;
|
|
99
|
-
readonly durationMs: number;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
interface WorkerAnalysisMessage extends WorkerMessageBase {
|
|
103
|
-
readonly type: 'analysis';
|
|
104
|
-
readonly requestId: string;
|
|
105
|
-
readonly analysis: unknown;
|
|
100
|
+
readonly result: BehaviorDecision;
|
|
106
101
|
readonly queryCount: number;
|
|
107
102
|
readonly durationMs: number;
|
|
108
103
|
}
|
|
@@ -118,7 +113,7 @@ function asError(value: unknown, fallback: string): Error {
|
|
|
118
113
|
}
|
|
119
114
|
|
|
120
115
|
function abortError(signal: AbortSignal): Error {
|
|
121
|
-
return asError(signal.reason, '
|
|
116
|
+
return asError(signal.reason, 'behavior_thread_request_aborted');
|
|
122
117
|
}
|
|
123
118
|
|
|
124
119
|
function errorCode(error: unknown, fallback: string): string {
|
|
@@ -135,14 +130,14 @@ function roundedElapsed(startedAt: number): number {
|
|
|
135
130
|
}
|
|
136
131
|
|
|
137
132
|
/** Adaptive Worker Thread pool for cross-tick computations. */
|
|
138
|
-
export class
|
|
133
|
+
export class ThreadBehaviorExecutor {
|
|
139
134
|
readonly runtimeInstanceId = randomUUID();
|
|
140
135
|
|
|
141
136
|
private readonly maxWorkerCount: number;
|
|
142
137
|
private readonly minWorkerCount: number;
|
|
143
138
|
private readonly idleWorkerTimeoutMs: number;
|
|
144
139
|
private readonly maxOldGenerationSizeMb: number;
|
|
145
|
-
private state:
|
|
140
|
+
private state: ThreadBehaviorExecutorSnapshot['state'] = 'new';
|
|
146
141
|
private readonly slots = new Set<WorkerSlot>();
|
|
147
142
|
private readonly queue: PendingRequest[] = [];
|
|
148
143
|
private readonly inFlight = new Set<PendingRequest>();
|
|
@@ -153,17 +148,22 @@ export class ThreadStrategyExecutor {
|
|
|
153
148
|
private replacementRetryTimer?: ReturnType<typeof setTimeout>;
|
|
154
149
|
private idleScaleDownTimer?: ReturnType<typeof setTimeout>;
|
|
155
150
|
private replacementFailureCount = 0;
|
|
156
|
-
private parameters: Readonly<Record<string, unknown>> = {};
|
|
157
151
|
private nextSlotId = 1;
|
|
158
152
|
private startedWorkerCount = 0;
|
|
159
153
|
private retiredWorkerCount = 0;
|
|
160
154
|
private desiredWorkerCount: number;
|
|
161
155
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
156
|
+
private readonly options: ThreadBehaviorExecutorOptions;
|
|
157
|
+
|
|
158
|
+
constructor(options: ThreadBehaviorExecutorOptions) {
|
|
159
|
+
this.options = Object.freeze({
|
|
160
|
+
...options,
|
|
161
|
+
source: pinBehaviorSource(options.source),
|
|
162
|
+
});
|
|
163
|
+
if ((options.source.execution ?? 'parallel') !== 'parallel') {
|
|
164
|
+
throw new Error('behavior_thread_execution_invalid');
|
|
165
165
|
}
|
|
166
|
-
const maxWorkerCount = options.poolSize ??
|
|
166
|
+
const maxWorkerCount = options.poolSize ?? PARALLEL_BEHAVIOR_THREAD_POOL_SIZE;
|
|
167
167
|
if (!Number.isInteger(maxWorkerCount) || maxWorkerCount <= 0 || maxWorkerCount > MAX_POOL_SIZE) {
|
|
168
168
|
throw new TypeError(`poolSize must be between 1 and ${MAX_POOL_SIZE}.`);
|
|
169
169
|
}
|
|
@@ -187,26 +187,22 @@ export class ThreadStrategyExecutor {
|
|
|
187
187
|
return this.state === 'running';
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
-
start(
|
|
191
|
-
parameters: Readonly<Record<string, unknown>>,
|
|
192
|
-
signal: AbortSignal,
|
|
193
|
-
): Promise<void> {
|
|
190
|
+
start(signal: AbortSignal): Promise<void> {
|
|
194
191
|
if (this.state === 'running') return Promise.resolve();
|
|
195
192
|
if (this.state === 'starting') return this.awaitStart(signal);
|
|
196
193
|
if (this.state === 'stopping' || this.state === 'stopped') {
|
|
197
|
-
return Promise.reject(new Error('
|
|
194
|
+
return Promise.reject(new Error('behavior_thread_pool_stopped'));
|
|
198
195
|
}
|
|
199
196
|
if (signal.aborted) return Promise.reject(abortError(signal));
|
|
200
197
|
this.state = 'starting';
|
|
201
|
-
this.parameters = structuredClone(parameters);
|
|
202
198
|
const startedAt = performance.now();
|
|
203
199
|
this.startTask = Promise.all(
|
|
204
200
|
Array.from({ length: this.minWorkerCount }, () => this.spawnSlot('initial')),
|
|
205
201
|
).then(() => {
|
|
206
|
-
if (this.state !== 'starting') throw new Error('
|
|
202
|
+
if (this.state !== 'starting') throw new Error('behavior_thread_pool_stopped');
|
|
207
203
|
this.state = 'running';
|
|
208
|
-
this.diagnostic('
|
|
209
|
-
|
|
204
|
+
this.diagnostic('behavior.thread_pool.started', {
|
|
205
|
+
behavior_id: this.options.source.id,
|
|
210
206
|
execution: this.options.source.execution,
|
|
211
207
|
runtime_instance_id: this.runtimeInstanceId,
|
|
212
208
|
worker_count: this.slots.size,
|
|
@@ -228,22 +224,16 @@ export class ThreadStrategyExecutor {
|
|
|
228
224
|
}
|
|
229
225
|
|
|
230
226
|
update(
|
|
231
|
-
frame:
|
|
232
|
-
signal: AbortSignal,
|
|
233
|
-
): Promise<StrategyResult> {
|
|
234
|
-
return this.submit('update', frame, signal) as Promise<StrategyResult>;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
analyze(
|
|
238
|
-
frame: StrategyFrame<Record<string, unknown>>,
|
|
227
|
+
frame: BehaviorInput,
|
|
239
228
|
signal: AbortSignal,
|
|
240
|
-
|
|
241
|
-
|
|
229
|
+
observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>,
|
|
230
|
+
): Promise<BehaviorDecision> {
|
|
231
|
+
return this.submit('decide', frame, signal, observationSnapshot) as Promise<BehaviorDecision>;
|
|
242
232
|
}
|
|
243
233
|
|
|
244
234
|
async reset(_signal: AbortSignal): Promise<void> {
|
|
245
|
-
//
|
|
246
|
-
// cached by each slot
|
|
235
|
+
// Parallel Behavior instances are already fresh per input. Only the
|
|
236
|
+
// module/Class is cached by each slot.
|
|
247
237
|
}
|
|
248
238
|
|
|
249
239
|
stop(reason: 'switched' | 'scope_ended' | 'shutdown'): Promise<void> {
|
|
@@ -264,15 +254,15 @@ export class ThreadStrategyExecutor {
|
|
|
264
254
|
this.replacementRetryTimer = undefined;
|
|
265
255
|
}
|
|
266
256
|
this.cancelIdleScaleDown();
|
|
267
|
-
const error = new Error('
|
|
257
|
+
const error = new Error('behavior_thread_pool_stopped');
|
|
268
258
|
for (const request of [...this.inFlight]) this.rejectRequest(request, error);
|
|
269
259
|
await Promise.allSettled([...this.slots].map((slot) => this.retireSlot(slot, reason)));
|
|
270
260
|
await Promise.allSettled([...this.spawnTasks]);
|
|
271
261
|
await this.startTask?.catch(() => {});
|
|
272
262
|
await Promise.allSettled([...this.slots].map((slot) => this.retireSlot(slot, reason)));
|
|
273
263
|
this.state = 'stopped';
|
|
274
|
-
this.diagnostic('
|
|
275
|
-
|
|
264
|
+
this.diagnostic('behavior.thread_pool.stopped', {
|
|
265
|
+
behavior_id: this.options.source.id,
|
|
276
266
|
runtime_instance_id: this.runtimeInstanceId,
|
|
277
267
|
reason,
|
|
278
268
|
min_worker_count: this.minWorkerCount,
|
|
@@ -282,7 +272,7 @@ export class ThreadStrategyExecutor {
|
|
|
282
272
|
}, 'info');
|
|
283
273
|
}
|
|
284
274
|
|
|
285
|
-
snapshot():
|
|
275
|
+
snapshot(): ThreadBehaviorExecutorSnapshot {
|
|
286
276
|
const slots = [...this.slots];
|
|
287
277
|
return {
|
|
288
278
|
state: this.state,
|
|
@@ -307,25 +297,26 @@ export class ThreadStrategyExecutor {
|
|
|
307
297
|
}
|
|
308
298
|
|
|
309
299
|
private async submit(
|
|
310
|
-
operation: '
|
|
311
|
-
frame:
|
|
300
|
+
operation: 'decide',
|
|
301
|
+
frame: BehaviorInput,
|
|
312
302
|
signal: AbortSignal,
|
|
303
|
+
observationSnapshot?: Readonly<ClawclawBehaviorObservationSnapshot>,
|
|
313
304
|
): Promise<unknown> {
|
|
314
305
|
if (signal.aborted) throw abortError(signal);
|
|
315
306
|
await this.awaitStart(signal);
|
|
316
|
-
if (this.state !== 'running') throw new Error('
|
|
317
|
-
if (
|
|
318
|
-
throw new Error('
|
|
319
|
-
}
|
|
320
|
-
if (operation === 'analyze' && this.options.source.execution !== 'parallel_analyze') {
|
|
321
|
-
throw new Error('strategy_thread_analyze_not_available');
|
|
307
|
+
if (this.state !== 'running') throw new Error('behavior_thread_pool_not_running');
|
|
308
|
+
if ((this.options.source.execution ?? 'parallel') !== 'parallel') {
|
|
309
|
+
throw new Error('behavior_thread_decide_not_available');
|
|
322
310
|
}
|
|
323
|
-
if (this.inFlight.size >= this.maxWorkerCount) throw new Error('
|
|
311
|
+
if (this.inFlight.size >= this.maxWorkerCount) throw new Error('behavior_thread_window_full');
|
|
324
312
|
return new Promise<unknown>((resolve, reject) => {
|
|
325
313
|
const request: PendingRequest = {
|
|
326
314
|
requestId: randomUUID(),
|
|
327
315
|
operation,
|
|
328
316
|
frame: structuredClone(frame),
|
|
317
|
+
...(observationSnapshot === undefined
|
|
318
|
+
? {}
|
|
319
|
+
: { observationSnapshot: structuredClone(observationSnapshot) }),
|
|
329
320
|
signal,
|
|
330
321
|
enqueuedAt: performance.now(),
|
|
331
322
|
resolve,
|
|
@@ -336,13 +327,13 @@ export class ThreadStrategyExecutor {
|
|
|
336
327
|
signal.addEventListener('abort', request.onAbort, { once: true });
|
|
337
328
|
this.inFlight.add(request);
|
|
338
329
|
this.queue.push(request);
|
|
339
|
-
this.diagnostic(`
|
|
340
|
-
|
|
330
|
+
this.diagnostic(`behavior.${operation}.started`, {
|
|
331
|
+
behavior_id: this.options.source.id,
|
|
341
332
|
executor: 'worker_thread',
|
|
342
333
|
runtime_instance_id: this.runtimeInstanceId,
|
|
343
334
|
request_id: request.requestId,
|
|
344
335
|
epoch_id: frame.epochId,
|
|
345
|
-
...(frame.tick === undefined ? {} : { tick: frame.tick }),
|
|
336
|
+
...(frame.gameState.tick === undefined ? {} : { tick: frame.gameState.tick }),
|
|
346
337
|
}, 'debug');
|
|
347
338
|
this.pump();
|
|
348
339
|
this.growForPressure();
|
|
@@ -350,10 +341,10 @@ export class ThreadStrategyExecutor {
|
|
|
350
341
|
}
|
|
351
342
|
|
|
352
343
|
private async spawnSlot(reason: 'initial' | 'queue_pressure' | 'replacement'): Promise<void> {
|
|
353
|
-
const source =
|
|
354
|
-
const sourceUrl =
|
|
344
|
+
const source = resolveBehaviorSource(this.options.source);
|
|
345
|
+
const sourceUrl = behaviorSourceUrl(source);
|
|
355
346
|
const slotId = this.nextSlotId++;
|
|
356
|
-
const loader =
|
|
347
|
+
const loader = behaviorThreadLoaderConfig();
|
|
357
348
|
const worker = new Worker(WORKER_BOOTSTRAP, {
|
|
358
349
|
execArgv: [],
|
|
359
350
|
resourceLimits: { maxOldGenerationSizeMb: this.maxOldGenerationSizeMb },
|
|
@@ -370,26 +361,25 @@ export class ThreadStrategyExecutor {
|
|
|
370
361
|
slotId,
|
|
371
362
|
source,
|
|
372
363
|
sourceUrl,
|
|
373
|
-
parameters: this.parameters,
|
|
374
364
|
},
|
|
375
365
|
});
|
|
376
366
|
const slot: WorkerSlot = { id: slotId, worker, state: 'starting' };
|
|
377
367
|
this.slots.add(slot);
|
|
378
368
|
worker.stdout?.on('data', (chunk: Buffer) => this.diagnostic(
|
|
379
|
-
'
|
|
380
|
-
{
|
|
369
|
+
'behavior.thread.stdout',
|
|
370
|
+
{ behavior_id: source.id, slot_id: slot.id, bytes: chunk.length },
|
|
381
371
|
'debug',
|
|
382
372
|
));
|
|
383
373
|
worker.stderr?.on('data', (chunk: Buffer) => this.diagnostic(
|
|
384
|
-
'
|
|
385
|
-
{
|
|
374
|
+
'behavior.thread.stderr',
|
|
375
|
+
{ behavior_id: source.id, slot_id: slot.id, bytes: chunk.length },
|
|
386
376
|
'debug',
|
|
387
377
|
));
|
|
388
378
|
worker.on('message', (message: unknown) => this.handleMessage(slot, message));
|
|
389
379
|
worker.once('error', (error) => this.failSlot(slot, error));
|
|
390
380
|
worker.once('exit', (code) => {
|
|
391
381
|
if (slot.state !== 'retiring') {
|
|
392
|
-
this.failSlot(slot, new Error(`
|
|
382
|
+
this.failSlot(slot, new Error(`behavior_thread_worker_exited:${code}`));
|
|
393
383
|
}
|
|
394
384
|
});
|
|
395
385
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -397,17 +387,17 @@ export class ThreadStrategyExecutor {
|
|
|
397
387
|
await new Promise<void>((resolve, reject) => {
|
|
398
388
|
slot.readyResolve = resolve;
|
|
399
389
|
slot.readyReject = reject;
|
|
400
|
-
timer = setTimeout(() => reject(new Error('
|
|
390
|
+
timer = setTimeout(() => reject(new Error('behavior_thread_worker_start_timeout')), START_TIMEOUT_MS);
|
|
401
391
|
timer.unref?.();
|
|
402
392
|
});
|
|
403
393
|
if (this.state !== 'starting' && this.state !== 'running') {
|
|
404
|
-
throw new Error('
|
|
394
|
+
throw new Error('behavior_thread_pool_stopped');
|
|
405
395
|
}
|
|
406
396
|
slot.state = 'idle';
|
|
407
397
|
slot.idleSince = performance.now();
|
|
408
398
|
this.startedWorkerCount += 1;
|
|
409
|
-
this.diagnostic('
|
|
410
|
-
|
|
399
|
+
this.diagnostic('behavior.thread.started', {
|
|
400
|
+
behavior_id: source.id,
|
|
411
401
|
runtime_instance_id: this.runtimeInstanceId,
|
|
412
402
|
slot_id: slot.id,
|
|
413
403
|
worker_count: this.activeWorkerCount(),
|
|
@@ -447,9 +437,12 @@ export class ThreadStrategyExecutor {
|
|
|
447
437
|
requestId: request.requestId,
|
|
448
438
|
operation: request.operation,
|
|
449
439
|
frame: request.frame,
|
|
440
|
+
...(request.observationSnapshot === undefined
|
|
441
|
+
? {}
|
|
442
|
+
: { observationSnapshot: request.observationSnapshot }),
|
|
450
443
|
});
|
|
451
444
|
} catch (error) {
|
|
452
|
-
this.rejectRequest(request, asError(error, '
|
|
445
|
+
this.rejectRequest(request, asError(error, 'behavior_thread_submit_failed'));
|
|
453
446
|
void this.retireSlot(slot, 'submit_failed');
|
|
454
447
|
}
|
|
455
448
|
}
|
|
@@ -457,24 +450,23 @@ export class ThreadStrategyExecutor {
|
|
|
457
450
|
|
|
458
451
|
private handleMessage(slot: WorkerSlot, value: unknown): void {
|
|
459
452
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
460
|
-
this.failSlot(slot, new Error('
|
|
453
|
+
this.failSlot(slot, new Error('behavior_thread_protocol_invalid'));
|
|
461
454
|
return;
|
|
462
455
|
}
|
|
463
456
|
const message = value as WorkerReadyMessage
|
|
464
457
|
| WorkerResultMessage
|
|
465
|
-
| WorkerAnalysisMessage
|
|
466
458
|
| WorkerErrorMessage;
|
|
467
459
|
if (message.poolId !== this.runtimeInstanceId || message.slotId !== slot.id) {
|
|
468
|
-
this.failSlot(slot, new Error('
|
|
460
|
+
this.failSlot(slot, new Error('behavior_thread_protocol_identity_mismatch'));
|
|
469
461
|
return;
|
|
470
462
|
}
|
|
471
463
|
if (message.type === 'ready') {
|
|
472
464
|
if (
|
|
473
465
|
slot.state !== 'starting'
|
|
474
|
-
|| message.
|
|
466
|
+
|| message.behaviorId !== this.options.source.id
|
|
475
467
|
|| message.execution !== this.options.source.execution
|
|
476
468
|
) {
|
|
477
|
-
this.failSlot(slot, new Error('
|
|
469
|
+
this.failSlot(slot, new Error('behavior_thread_ready_invalid'));
|
|
478
470
|
return;
|
|
479
471
|
}
|
|
480
472
|
slot.readyResolve?.();
|
|
@@ -483,7 +475,7 @@ export class ThreadStrategyExecutor {
|
|
|
483
475
|
if (message.type === 'error') {
|
|
484
476
|
const code = typeof message.code === 'string'
|
|
485
477
|
? message.code
|
|
486
|
-
: '
|
|
478
|
+
: 'behavior_thread_worker_failed';
|
|
487
479
|
const detail = typeof message.message === 'string'
|
|
488
480
|
? message.message.trim().slice(0, MAX_WORKER_ERROR_MESSAGE_LENGTH)
|
|
489
481
|
: '';
|
|
@@ -498,14 +490,11 @@ export class ThreadStrategyExecutor {
|
|
|
498
490
|
}
|
|
499
491
|
const request = slot.current;
|
|
500
492
|
if (!request || message.requestId !== request.requestId) {
|
|
501
|
-
this.failSlot(slot, new Error('
|
|
493
|
+
this.failSlot(slot, new Error('behavior_thread_request_mismatch'));
|
|
502
494
|
return;
|
|
503
495
|
}
|
|
504
|
-
if (
|
|
505
|
-
(request
|
|
506
|
-
|| (request.operation === 'analyze' && message.type !== 'analysis')
|
|
507
|
-
) {
|
|
508
|
-
this.rejectRequest(request, new Error('strategy_thread_response_invalid'));
|
|
496
|
+
if (message.type !== 'result') {
|
|
497
|
+
this.rejectRequest(request, new Error('behavior_thread_response_invalid'));
|
|
509
498
|
void this.retireSlot(slot, 'invalid_response');
|
|
510
499
|
return;
|
|
511
500
|
}
|
|
@@ -513,22 +502,21 @@ export class ThreadStrategyExecutor {
|
|
|
513
502
|
slot.state = 'idle';
|
|
514
503
|
slot.idleSince = performance.now();
|
|
515
504
|
request.slot = undefined;
|
|
516
|
-
|
|
517
|
-
this.resolveRequest(request, structuredClone(valueResult), message);
|
|
505
|
+
this.resolveRequest(request, structuredClone(message.result), message);
|
|
518
506
|
this.pump();
|
|
519
507
|
}
|
|
520
508
|
|
|
521
509
|
private resolveRequest(
|
|
522
510
|
request: PendingRequest,
|
|
523
511
|
value: unknown,
|
|
524
|
-
message: WorkerResultMessage
|
|
512
|
+
message: WorkerResultMessage,
|
|
525
513
|
): void {
|
|
526
514
|
if (request.settled) return;
|
|
527
515
|
request.settled = true;
|
|
528
516
|
request.signal.removeEventListener('abort', request.onAbort);
|
|
529
517
|
this.inFlight.delete(request);
|
|
530
|
-
this.diagnostic(`
|
|
531
|
-
|
|
518
|
+
this.diagnostic(`behavior.${request.operation}.completed`, {
|
|
519
|
+
behavior_id: this.options.source.id,
|
|
532
520
|
executor: 'worker_thread',
|
|
533
521
|
runtime_instance_id: this.runtimeInstanceId,
|
|
534
522
|
request_id: request.requestId,
|
|
@@ -538,7 +526,7 @@ export class ThreadStrategyExecutor {
|
|
|
538
526
|
Math.max(0, (request.assignedAt ?? performance.now()) - request.enqueuedAt) * 1_000,
|
|
539
527
|
) / 1_000,
|
|
540
528
|
worker_duration_ms: message.durationMs,
|
|
541
|
-
|
|
529
|
+
read_query_count: message.queryCount,
|
|
542
530
|
}, 'debug');
|
|
543
531
|
request.resolve(value);
|
|
544
532
|
this.scheduleIdleScaleDown();
|
|
@@ -555,17 +543,17 @@ export class ThreadStrategyExecutor {
|
|
|
555
543
|
request.slot = undefined;
|
|
556
544
|
this.diagnostic(
|
|
557
545
|
request.signal.aborted
|
|
558
|
-
? `
|
|
559
|
-
: `
|
|
546
|
+
? `behavior.${request.operation}.cancelled`
|
|
547
|
+
: `behavior.${request.operation}.failed`,
|
|
560
548
|
{
|
|
561
|
-
|
|
549
|
+
behavior_id: this.options.source.id,
|
|
562
550
|
executor: 'worker_thread',
|
|
563
551
|
runtime_instance_id: this.runtimeInstanceId,
|
|
564
552
|
request_id: request.requestId,
|
|
565
553
|
epoch_id: request.frame.epochId,
|
|
566
554
|
duration_ms: roundedElapsed(request.enqueuedAt),
|
|
567
555
|
aborted: request.signal.aborted,
|
|
568
|
-
error_code: errorCode(error, `
|
|
556
|
+
error_code: errorCode(error, `BEHAVIOR_${request.operation.toUpperCase()}_FAILED`),
|
|
569
557
|
},
|
|
570
558
|
request.signal.aborted ? 'debug' : 'error',
|
|
571
559
|
);
|
|
@@ -577,7 +565,7 @@ export class ThreadStrategyExecutor {
|
|
|
577
565
|
if (request.settled) return;
|
|
578
566
|
const slot = request.slot;
|
|
579
567
|
this.rejectRequest(request, abortError(request.signal));
|
|
580
|
-
// Synchronous
|
|
568
|
+
// Synchronous behavior code cannot be interrupted inside one isolate.
|
|
581
569
|
// Replace only its slot; computations in the other Workers remain in flight.
|
|
582
570
|
if (slot) void this.retireSlot(slot, 'request_aborted');
|
|
583
571
|
}
|
|
@@ -590,16 +578,16 @@ export class ThreadStrategyExecutor {
|
|
|
590
578
|
|
|
591
579
|
private retireSlot(slot: WorkerSlot, reason: string): Promise<void> {
|
|
592
580
|
if (slot.retirement) return slot.retirement;
|
|
593
|
-
slot.readyReject?.(new Error(`
|
|
581
|
+
slot.readyReject?.(new Error(`behavior_thread_worker_retired:${reason}`));
|
|
594
582
|
slot.state = 'retiring';
|
|
595
583
|
if (slot.current) {
|
|
596
|
-
this.rejectRequest(slot.current, new Error(`
|
|
584
|
+
this.rejectRequest(slot.current, new Error(`behavior_thread_worker_retired:${reason}`));
|
|
597
585
|
}
|
|
598
586
|
const retirement = Promise.resolve(slot.worker.terminate()).catch(() => -1).then(() => {
|
|
599
587
|
this.slots.delete(slot);
|
|
600
588
|
this.retiredWorkerCount += 1;
|
|
601
|
-
this.diagnostic('
|
|
602
|
-
|
|
589
|
+
this.diagnostic('behavior.thread.retired', {
|
|
590
|
+
behavior_id: this.options.source.id,
|
|
603
591
|
runtime_instance_id: this.runtimeInstanceId,
|
|
604
592
|
slot_id: slot.id,
|
|
605
593
|
reason,
|
|
@@ -633,10 +621,10 @@ export class ThreadStrategyExecutor {
|
|
|
633
621
|
REPLACEMENT_RETRY_MAX_MS,
|
|
634
622
|
REPLACEMENT_RETRY_BASE_MS * (2 ** Math.min(10, this.replacementFailureCount - 1)),
|
|
635
623
|
);
|
|
636
|
-
this.diagnostic('
|
|
637
|
-
|
|
624
|
+
this.diagnostic('behavior.thread.replacement_failed', {
|
|
625
|
+
behavior_id: this.options.source.id,
|
|
638
626
|
runtime_instance_id: this.runtimeInstanceId,
|
|
639
|
-
error_code: errorCode(error, '
|
|
627
|
+
error_code: errorCode(error, 'BEHAVIOR_THREAD_REPLACEMENT_FAILED'),
|
|
640
628
|
reason,
|
|
641
629
|
retry_delay_ms: retryDelayMs,
|
|
642
630
|
}, 'error');
|
|
@@ -665,8 +653,8 @@ export class ThreadStrategyExecutor {
|
|
|
665
653
|
if (target <= this.desiredWorkerCount) return;
|
|
666
654
|
const previous = this.desiredWorkerCount;
|
|
667
655
|
this.desiredWorkerCount = target;
|
|
668
|
-
this.diagnostic('
|
|
669
|
-
|
|
656
|
+
this.diagnostic('behavior.thread_pool.scale_requested', {
|
|
657
|
+
behavior_id: this.options.source.id,
|
|
670
658
|
runtime_instance_id: this.runtimeInstanceId,
|
|
671
659
|
trigger: 'queue_pressure',
|
|
672
660
|
previous_worker_count: previous,
|
|
@@ -700,10 +688,10 @@ export class ThreadStrategyExecutor {
|
|
|
700
688
|
const retiredWorkerCount = this.trimIdleWorkers(demand);
|
|
701
689
|
this.diagnostic(
|
|
702
690
|
retiredWorkerCount > 0 || previous !== demand
|
|
703
|
-
? '
|
|
704
|
-
: '
|
|
691
|
+
? 'behavior.thread_pool.scale_requested'
|
|
692
|
+
: 'behavior.thread_pool.scale_deferred',
|
|
705
693
|
{
|
|
706
|
-
|
|
694
|
+
behavior_id: this.options.source.id,
|
|
707
695
|
runtime_instance_id: this.runtimeInstanceId,
|
|
708
696
|
trigger: retiredWorkerCount > 0 ? 'idle' : 'current_demand',
|
|
709
697
|
previous_worker_count: previous,
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
hasTopLevelManifestProperty,
|
|
4
|
+
parseLiteralBehaviorInputFields,
|
|
5
|
+
} from './behavior-input-source.js';
|
|
6
|
+
|
|
7
|
+
describe('parseLiteralBehaviorInputFields', () => {
|
|
8
|
+
it('parses a strict top-level literal without evaluating TypeScript', () => {
|
|
9
|
+
const result = parseLiteralBehaviorInputFields(`{
|
|
10
|
+
apiVersion: 2,
|
|
11
|
+
localizations: { 'zh-CN': { description: 'inputs: not a field' } },
|
|
12
|
+
inputs: [
|
|
13
|
+
{ id: 'name', type: 'string', description: 'Name', default: 'A\\nB' },
|
|
14
|
+
{ id: 'count', type: 'number', description: 'Count', min: -1.5, max: 1e2 },
|
|
15
|
+
{ id: 'enabled', type: 'boolean', description: 'Enabled', default: false },
|
|
16
|
+
{
|
|
17
|
+
id: 'mode', type: 'enum', description: 'Mode',
|
|
18
|
+
values: ['safe', 'fast'] as const, default: 'safe',
|
|
19
|
+
},
|
|
20
|
+
{ id: 'tags', type: 'string_list', description: 'Tags', maxItems: 2, default: [] },
|
|
21
|
+
{ id: 'target', type: 'player_seat', description: 'Target', required: true },
|
|
22
|
+
{ id: 'allies', type: 'player_seats', description: 'Allies', maxItems: 4 },
|
|
23
|
+
] as const,
|
|
24
|
+
produces: [],
|
|
25
|
+
}`);
|
|
26
|
+
|
|
27
|
+
expect(result.status).toBe('valid');
|
|
28
|
+
if (result.status !== 'valid') return;
|
|
29
|
+
expect(result.inputs).toHaveLength(7);
|
|
30
|
+
expect(result.inputs[0]).toMatchObject({ id: 'name', default: 'A\nB' });
|
|
31
|
+
expect(result.inputs[1]).toMatchObject({ min: -1.5, max: 100 });
|
|
32
|
+
expect(result.inputs[5]).toMatchObject({ type: 'player_seat', required: true });
|
|
33
|
+
expect(Object.isFrozen(result.inputs)).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('distinguishes missing, dynamic, malformed, and schema-invalid declarations', () => {
|
|
37
|
+
expect(parseLiteralBehaviorInputFields('{ apiVersion: 2 }'))
|
|
38
|
+
.toEqual({ status: 'missing' });
|
|
39
|
+
expect(parseLiteralBehaviorInputFields('{ inputs: getInputs() }'))
|
|
40
|
+
.toEqual({ status: 'invalid', diagnostic: 'behavior_inputs_not_literal' });
|
|
41
|
+
expect(parseLiteralBehaviorInputFields(`{
|
|
42
|
+
inputs: [{ id: 'mode', type: 'enum', description: 'Mode', values: MODES }],
|
|
43
|
+
}`)).toEqual({ status: 'invalid', diagnostic: 'behavior_inputs_not_literal' });
|
|
44
|
+
expect(parseLiteralBehaviorInputFields(`{
|
|
45
|
+
inputs: [{ id: 'count', type: 'number', description: 'Count', min: 3, max: 2 }],
|
|
46
|
+
}`)).toEqual({
|
|
47
|
+
status: 'invalid',
|
|
48
|
+
diagnostic: 'behavior_input_number_bounds_invalid:count',
|
|
49
|
+
});
|
|
50
|
+
expect(parseLiteralBehaviorInputFields(`{
|
|
51
|
+
inputs: [{ id: 'target', type: 'player_seat', description: 'Target', required: true, default: 2 }],
|
|
52
|
+
}`)).toEqual({
|
|
53
|
+
status: 'invalid',
|
|
54
|
+
diagnostic: 'behavior_input_required_default_conflict:target',
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('rejects template interpolation and arithmetic instead of evaluating them', () => {
|
|
59
|
+
expect(parseLiteralBehaviorInputFields(`{
|
|
60
|
+
inputs: [{ id: 'name', type: 'string', description: 'Name', default: \`player-\${seat}\` }],
|
|
61
|
+
}`)).toEqual({ status: 'invalid', diagnostic: 'behavior_inputs_not_literal' });
|
|
62
|
+
expect(parseLiteralBehaviorInputFields(`{
|
|
63
|
+
inputs: [{ id: 'count', type: 'number', description: 'Count', default: 1 + 2 }],
|
|
64
|
+
}`)).toEqual({ status: 'invalid', diagnostic: 'behavior_inputs_literal_invalid' });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('detects only real top-level properties', () => {
|
|
68
|
+
expect(hasTopLevelManifestProperty(`{
|
|
69
|
+
description: 'mentions controls: without declaring it',
|
|
70
|
+
localizations: { 'zh-CN': { controls: [] } },
|
|
71
|
+
// controls: []
|
|
72
|
+
inputs: [],
|
|
73
|
+
}`, 'controls')).toBe(false);
|
|
74
|
+
expect(hasTopLevelManifestProperty(`{
|
|
75
|
+
description: 'legacy manifest',
|
|
76
|
+
'controls': [],
|
|
77
|
+
inputs: [],
|
|
78
|
+
}`, 'controls')).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
});
|