@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
|
@@ -1,2175 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import type {
|
|
3
|
-
RuntimeEpochPluginContext,
|
|
4
|
-
RuntimeEpochPluginInput,
|
|
5
|
-
} from '../../../framework/plugins/index.js';
|
|
6
|
-
import type { CanonicalEvent } from '../../../framework/events/types.js';
|
|
7
|
-
import type { StoredStateFrame } from '../../../framework/state/history/contracts.js';
|
|
8
|
-
import { MemoryStateHistoryRepository } from '../../../framework/state/history/memory-state-history-repository.js';
|
|
9
|
-
import type {
|
|
10
|
-
StrategyFrame,
|
|
11
|
-
StrategyResult,
|
|
12
|
-
} from '../../../../sdk/strategy-plugin/index.js';
|
|
13
|
-
import {
|
|
14
|
-
ClawclawStrategyPlugin,
|
|
15
|
-
type ClawclawStrategyExecutor,
|
|
16
|
-
} from './strategy-plugin.js';
|
|
17
|
-
|
|
18
|
-
const scope = {
|
|
19
|
-
adapterId: 'clawclaw',
|
|
20
|
-
accountId: 'account',
|
|
21
|
-
sessionId: 'session',
|
|
22
|
-
segmentId: 'game',
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
interface InputOptions {
|
|
26
|
-
readonly domainTick?: number;
|
|
27
|
-
readonly stateTick?: number;
|
|
28
|
-
readonly pendingEvents?: readonly CanonicalEvent[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function input(
|
|
32
|
-
epochSeq: number,
|
|
33
|
-
options: InputOptions = {},
|
|
34
|
-
): RuntimeEpochPluginInput<Record<string, any>> {
|
|
35
|
-
const domainTick = options.domainTick ?? epochSeq;
|
|
36
|
-
const stateTick = options.stateTick ?? domainTick;
|
|
37
|
-
const gameState = {
|
|
38
|
-
phase: 'wandering',
|
|
39
|
-
tick: stateTick,
|
|
40
|
-
you: { name: 'Observer', x: 0, y: 0, is_alive: true },
|
|
41
|
-
};
|
|
42
|
-
const state = {
|
|
43
|
-
schema: 'clawclaw.runtime-state.v1',
|
|
44
|
-
action_safety_generation: 1,
|
|
45
|
-
stage: 'game',
|
|
46
|
-
transport: 'game_ws',
|
|
47
|
-
connection: 'connected',
|
|
48
|
-
cause: 'game_snapshot',
|
|
49
|
-
game_snapshot: { status: 'fresh', state: gameState },
|
|
50
|
-
};
|
|
51
|
-
const frame: StoredStateFrame<Record<string, any>> = {
|
|
52
|
-
frameSeq: epochSeq,
|
|
53
|
-
epochSeq,
|
|
54
|
-
scope,
|
|
55
|
-
sourceId: 'test',
|
|
56
|
-
connectionId: 'ws',
|
|
57
|
-
sourceKey: `state:${epochSeq}`,
|
|
58
|
-
backendTick: domainTick,
|
|
59
|
-
receivedAtMs: epochSeq * 100,
|
|
60
|
-
state,
|
|
61
|
-
};
|
|
62
|
-
return {
|
|
63
|
-
epoch: {
|
|
64
|
-
epochSeq,
|
|
65
|
-
scope,
|
|
66
|
-
sourceId: 'test',
|
|
67
|
-
connectionId: 'ws',
|
|
68
|
-
sourceKey: `state:${epochSeq}`,
|
|
69
|
-
domainTick,
|
|
70
|
-
state,
|
|
71
|
-
events: [],
|
|
72
|
-
completeness: 'complete',
|
|
73
|
-
receivedAt: new Date(frame.receivedAtMs).toISOString(),
|
|
74
|
-
},
|
|
75
|
-
frame,
|
|
76
|
-
currentState: {
|
|
77
|
-
state: gameState,
|
|
78
|
-
stateStatus: {
|
|
79
|
-
status: 'fresh',
|
|
80
|
-
semantics: 'latest_received',
|
|
81
|
-
source: 'owner_memory',
|
|
82
|
-
scope,
|
|
83
|
-
connectionId: 'ws',
|
|
84
|
-
connectionState: 'connected',
|
|
85
|
-
receivedAtMs: frame.receivedAtMs,
|
|
86
|
-
ageMs: 0,
|
|
87
|
-
backendTick: domainTick,
|
|
88
|
-
reason: null,
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
previousState: null,
|
|
92
|
-
epochEvents: [],
|
|
93
|
-
pendingEvents: options.pendingEvents ?? [],
|
|
94
|
-
upstreamStates: {},
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function event(commitSeq: number, type = `event_${commitSeq}`): CanonicalEvent {
|
|
99
|
-
return {
|
|
100
|
-
commitSeq,
|
|
101
|
-
epochSeq: commitSeq,
|
|
102
|
-
eventId: `event-${commitSeq}`,
|
|
103
|
-
sessionId: scope.sessionId,
|
|
104
|
-
segmentId: scope.segmentId,
|
|
105
|
-
type,
|
|
106
|
-
producer: 'backend_push',
|
|
107
|
-
deliveryClass: 'monitor_and_events',
|
|
108
|
-
occurredAt: new Date(commitSeq * 100).toISOString(),
|
|
109
|
-
receivedAt: new Date(commitSeq * 100).toISOString(),
|
|
110
|
-
payload: { commitSeq },
|
|
111
|
-
sourceId: 'test',
|
|
112
|
-
replayed: false,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function context(
|
|
117
|
-
diagnostic: RuntimeEpochPluginContext<Record<string, any>>['diagnostic'],
|
|
118
|
-
): RuntimeEpochPluginContext<Record<string, any>> {
|
|
119
|
-
const history = new MemoryStateHistoryRepository<unknown, Record<string, any>>();
|
|
120
|
-
history.openScope(scope);
|
|
121
|
-
return {
|
|
122
|
-
scope,
|
|
123
|
-
stateHistory: history,
|
|
124
|
-
currentSegment: () => ({ kind: 'game', identity: 'game' }),
|
|
125
|
-
checkpoint: () => undefined,
|
|
126
|
-
progress: () => ({
|
|
127
|
-
pluginId: 'clawclaw.strategy',
|
|
128
|
-
completedThroughEpochSeq: 0,
|
|
129
|
-
eventCursor: 0,
|
|
130
|
-
updatedAt: new Date(0).toISOString(),
|
|
131
|
-
}),
|
|
132
|
-
capabilities: {
|
|
133
|
-
provideObservation: () => { throw new Error('unused'); },
|
|
134
|
-
provideAction: () => { throw new Error('unused'); },
|
|
135
|
-
queryObservation: () => Promise.reject(new Error('unused')),
|
|
136
|
-
performAction: () => Promise.reject(new Error('unused')),
|
|
137
|
-
releaseActionCaller: () => {},
|
|
138
|
-
},
|
|
139
|
-
diagnostic,
|
|
140
|
-
signal: new AbortController().signal,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function actionContext(actions: Array<{ epoch: number; action: Record<string, unknown> }>) {
|
|
145
|
-
const base = context(() => {});
|
|
146
|
-
let activeEpoch = 0;
|
|
147
|
-
return {
|
|
148
|
-
forEpoch(epoch: number): RuntimeEpochPluginContext<Record<string, any>> {
|
|
149
|
-
activeEpoch = epoch;
|
|
150
|
-
return {
|
|
151
|
-
...base,
|
|
152
|
-
capabilities: {
|
|
153
|
-
...base.capabilities,
|
|
154
|
-
performAction: async (_capability, action) => {
|
|
155
|
-
actions.push({ epoch: activeEpoch, action: action as Record<string, unknown> });
|
|
156
|
-
return {
|
|
157
|
-
scope,
|
|
158
|
-
actionId: `action-${actions.length}`,
|
|
159
|
-
status: 'success' as const,
|
|
160
|
-
source: 'backend' as const,
|
|
161
|
-
actionOutcome: 'confirmed' as const,
|
|
162
|
-
};
|
|
163
|
-
},
|
|
164
|
-
},
|
|
165
|
-
};
|
|
166
|
-
},
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function successfulStrategyReportResult() {
|
|
171
|
-
return {
|
|
172
|
-
scope,
|
|
173
|
-
actionId: 'strategy-report-action',
|
|
174
|
-
status: 'success' as const,
|
|
175
|
-
source: 'backend' as const,
|
|
176
|
-
actionOutcome: 'confirmed' as const,
|
|
177
|
-
value: { success: true, data: { ok: true } },
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function reportingPlugin(): ClawclawStrategyPlugin {
|
|
182
|
-
return new ClawclawStrategyPlugin({
|
|
183
|
-
executorFactory: () => ({
|
|
184
|
-
runtimeInstanceId: 'strategy-reporting-test',
|
|
185
|
-
isAlive: true,
|
|
186
|
-
async start() {},
|
|
187
|
-
async reset() {},
|
|
188
|
-
async update() { return {}; },
|
|
189
|
-
async stop() {},
|
|
190
|
-
}),
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function run(
|
|
195
|
-
plugin: ClawclawStrategyPlugin,
|
|
196
|
-
pluginInput: RuntimeEpochPluginInput<Record<string, any>>,
|
|
197
|
-
pluginContext: RuntimeEpochPluginContext<Record<string, any>>,
|
|
198
|
-
) {
|
|
199
|
-
const computation = await plugin.update(pluginInput, pluginContext);
|
|
200
|
-
return plugin.commit(pluginInput, computation, pluginContext);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
describe('ClawClaw Strategy plugin scheduling', () => {
|
|
204
|
-
it('accepts numeric player_seats controls and rejects string seats or names', () => {
|
|
205
|
-
const plugin = new ClawclawStrategyPlugin();
|
|
206
|
-
plugin.select({
|
|
207
|
-
id: 'seat-controls',
|
|
208
|
-
execution: 'parallel_analyze',
|
|
209
|
-
sourcePath: 'unused',
|
|
210
|
-
sourceRoot: 'unused',
|
|
211
|
-
controls: [{
|
|
212
|
-
id: 'targets',
|
|
213
|
-
type: 'player_seats',
|
|
214
|
-
description: 'Target seats.',
|
|
215
|
-
}],
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
expect(() => plugin.control({ targets: ['6'] })).toThrow('invalid_strategy_control:targets');
|
|
219
|
-
expect(() => plugin.control({ targets: ['木桩机器虾'] })).toThrow('invalid_strategy_control:targets');
|
|
220
|
-
expect(() => plugin.control({ targets: [0, 11] })).toThrow('invalid_strategy_control:targets');
|
|
221
|
-
|
|
222
|
-
const accepted = plugin.control({ targets: [6] });
|
|
223
|
-
void accepted.catch(() => {});
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('exposes localized metadata only after a strategy becomes active', async () => {
|
|
227
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
228
|
-
executorFactory: () => ({
|
|
229
|
-
runtimeInstanceId: 'localized-metadata-test',
|
|
230
|
-
isAlive: true,
|
|
231
|
-
async start() {},
|
|
232
|
-
async reset() {},
|
|
233
|
-
async update() { return {}; },
|
|
234
|
-
async stop() {},
|
|
235
|
-
}),
|
|
236
|
-
});
|
|
237
|
-
plugin.select({
|
|
238
|
-
id: 'test-strategy',
|
|
239
|
-
name: 'Test strategy',
|
|
240
|
-
description: 'Runs a test strategy.',
|
|
241
|
-
localizations: {
|
|
242
|
-
'zh-CN': {
|
|
243
|
-
name: '测试策略',
|
|
244
|
-
description: '运行测试策略。',
|
|
245
|
-
},
|
|
246
|
-
},
|
|
247
|
-
sourcePath: 'unused',
|
|
248
|
-
sourceRoot: 'unused',
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
expect(plugin.activeResourceDescriptor('zh-CN')).toBeNull();
|
|
252
|
-
await run(plugin, input(1), context(() => {}));
|
|
253
|
-
expect(plugin.activeResourceDescriptor('zh-CN')).toEqual({
|
|
254
|
-
id: 'test-strategy',
|
|
255
|
-
name: '测试策略',
|
|
256
|
-
description: '运行测试策略。',
|
|
257
|
-
});
|
|
258
|
-
expect(plugin.activeResourceDescriptor('en-US')?.name).toBe('Test strategy');
|
|
259
|
-
await plugin.stop('shutdown');
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
it('reports the effective strategy asynchronously and suppresses repeats after the exact ack', async () => {
|
|
263
|
-
vi.useFakeTimers();
|
|
264
|
-
try {
|
|
265
|
-
let resolveReport!: (value: ReturnType<typeof successfulStrategyReportResult>) => void;
|
|
266
|
-
const pendingReport = new Promise<ReturnType<typeof successfulStrategyReportResult>>((resolve) => {
|
|
267
|
-
resolveReport = resolve;
|
|
268
|
-
});
|
|
269
|
-
const performAction = vi.fn(() => pendingReport);
|
|
270
|
-
const baseContext = context(() => {});
|
|
271
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
272
|
-
...baseContext,
|
|
273
|
-
capabilities: {
|
|
274
|
-
...baseContext.capabilities,
|
|
275
|
-
performAction: performAction as RuntimeEpochPluginContext<Record<string, any>>[
|
|
276
|
-
'capabilities'
|
|
277
|
-
]['performAction'],
|
|
278
|
-
},
|
|
279
|
-
};
|
|
280
|
-
const plugin = reportingPlugin();
|
|
281
|
-
plugin.select({
|
|
282
|
-
id: 'task-only',
|
|
283
|
-
name: 'Task only',
|
|
284
|
-
description: 'Complete assigned tasks.',
|
|
285
|
-
localizations: {
|
|
286
|
-
'zh-CN': { name: '纯任务', description: '专心完成分配的任务。' },
|
|
287
|
-
},
|
|
288
|
-
sourcePath: 'unused',
|
|
289
|
-
sourceRoot: 'unused',
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
await expect(run(plugin, input(1), pluginContext)).resolves.toMatchObject({
|
|
293
|
-
outcome: 'processed',
|
|
294
|
-
});
|
|
295
|
-
expect(performAction).not.toHaveBeenCalled();
|
|
296
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
297
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
298
|
-
expect(performAction).toHaveBeenCalledWith('clawclaw.action', {
|
|
299
|
-
route: 'game',
|
|
300
|
-
action: {
|
|
301
|
-
action: 'strategy',
|
|
302
|
-
name: '纯任务',
|
|
303
|
-
description: '专心完成分配的任务。',
|
|
304
|
-
},
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
resolveReport(successfulStrategyReportResult());
|
|
308
|
-
await Promise.resolve();
|
|
309
|
-
await Promise.resolve();
|
|
310
|
-
await run(plugin, input(2), pluginContext);
|
|
311
|
-
await vi.advanceTimersByTimeAsync(5_000);
|
|
312
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
313
|
-
await plugin.stop('shutdown');
|
|
314
|
-
} finally {
|
|
315
|
-
vi.useRealTimers();
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it('retries an incorrect strategy report response once per second until acknowledged', async () => {
|
|
320
|
-
vi.useFakeTimers();
|
|
321
|
-
try {
|
|
322
|
-
const performAction = vi.fn()
|
|
323
|
-
.mockResolvedValueOnce({
|
|
324
|
-
...successfulStrategyReportResult(),
|
|
325
|
-
value: { success: true, data: { ok: false } },
|
|
326
|
-
})
|
|
327
|
-
.mockResolvedValue(successfulStrategyReportResult());
|
|
328
|
-
const baseContext = context(() => {});
|
|
329
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
330
|
-
...baseContext,
|
|
331
|
-
capabilities: {
|
|
332
|
-
...baseContext.capabilities,
|
|
333
|
-
performAction: performAction as RuntimeEpochPluginContext<Record<string, any>>[
|
|
334
|
-
'capabilities'
|
|
335
|
-
]['performAction'],
|
|
336
|
-
},
|
|
337
|
-
};
|
|
338
|
-
const plugin = reportingPlugin();
|
|
339
|
-
plugin.select({
|
|
340
|
-
id: 'patrol',
|
|
341
|
-
name: '巡逻',
|
|
342
|
-
description: '按房间顺序巡逻。',
|
|
343
|
-
sourcePath: 'unused',
|
|
344
|
-
sourceRoot: 'unused',
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
await run(plugin, input(1), pluginContext);
|
|
348
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
349
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
350
|
-
await vi.advanceTimersByTimeAsync(999);
|
|
351
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
352
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
353
|
-
expect(performAction).toHaveBeenCalledTimes(2);
|
|
354
|
-
await vi.advanceTimersByTimeAsync(5_000);
|
|
355
|
-
expect(performAction).toHaveBeenCalledTimes(2);
|
|
356
|
-
await plugin.stop('shutdown');
|
|
357
|
-
} finally {
|
|
358
|
-
vi.useRealTimers();
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
it('does not let an old in-flight acknowledgement confirm a newly selected strategy', async () => {
|
|
363
|
-
vi.useFakeTimers();
|
|
364
|
-
try {
|
|
365
|
-
let resolveFirst!: (value: ReturnType<typeof successfulStrategyReportResult>) => void;
|
|
366
|
-
const first = new Promise<ReturnType<typeof successfulStrategyReportResult>>((resolve) => {
|
|
367
|
-
resolveFirst = resolve;
|
|
368
|
-
});
|
|
369
|
-
const performAction = vi.fn()
|
|
370
|
-
.mockImplementationOnce(() => first)
|
|
371
|
-
.mockResolvedValue(successfulStrategyReportResult());
|
|
372
|
-
const baseContext = context(() => {});
|
|
373
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
374
|
-
...baseContext,
|
|
375
|
-
capabilities: {
|
|
376
|
-
...baseContext.capabilities,
|
|
377
|
-
performAction: performAction as RuntimeEpochPluginContext<Record<string, any>>[
|
|
378
|
-
'capabilities'
|
|
379
|
-
]['performAction'],
|
|
380
|
-
},
|
|
381
|
-
};
|
|
382
|
-
const plugin = reportingPlugin();
|
|
383
|
-
plugin.select({
|
|
384
|
-
id: 'first',
|
|
385
|
-
name: '策略甲',
|
|
386
|
-
description: '策略甲描述。',
|
|
387
|
-
sourcePath: 'unused',
|
|
388
|
-
sourceRoot: 'unused',
|
|
389
|
-
});
|
|
390
|
-
await run(plugin, input(1), pluginContext);
|
|
391
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
392
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
393
|
-
|
|
394
|
-
plugin.select({
|
|
395
|
-
id: 'second',
|
|
396
|
-
name: '策略乙',
|
|
397
|
-
description: '策略乙描述。',
|
|
398
|
-
sourcePath: 'unused',
|
|
399
|
-
sourceRoot: 'unused',
|
|
400
|
-
});
|
|
401
|
-
await run(plugin, input(2), pluginContext);
|
|
402
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
403
|
-
expect(performAction).toHaveBeenCalledTimes(1);
|
|
404
|
-
|
|
405
|
-
resolveFirst(successfulStrategyReportResult());
|
|
406
|
-
await Promise.resolve();
|
|
407
|
-
await Promise.resolve();
|
|
408
|
-
await vi.advanceTimersByTimeAsync(0);
|
|
409
|
-
expect(performAction).toHaveBeenCalledTimes(2);
|
|
410
|
-
expect(performAction.mock.calls[1]?.[1]).toMatchObject({
|
|
411
|
-
action: { action: 'strategy', name: '策略乙', description: '策略乙描述。' },
|
|
412
|
-
});
|
|
413
|
-
await plugin.stop('shutdown');
|
|
414
|
-
} finally {
|
|
415
|
-
vi.useRealTimers();
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
it('releases each Strategy activation, including a same-id parameter generation', async () => {
|
|
420
|
-
const releases: Array<{ capability: string; id: string; reason?: string }> = [];
|
|
421
|
-
const baseContext = context(() => {});
|
|
422
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
423
|
-
...baseContext,
|
|
424
|
-
capabilities: {
|
|
425
|
-
...baseContext.capabilities,
|
|
426
|
-
releaseActionCaller: (capability, caller, reason) => {
|
|
427
|
-
releases.push({ capability, id: caller.id, reason });
|
|
428
|
-
},
|
|
429
|
-
},
|
|
430
|
-
};
|
|
431
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
432
|
-
executorFactory: () => {
|
|
433
|
-
let alive = false;
|
|
434
|
-
return {
|
|
435
|
-
runtimeInstanceId: 'selection-release-test',
|
|
436
|
-
get isAlive() { return alive; },
|
|
437
|
-
async start() { alive = true; },
|
|
438
|
-
async reset() {},
|
|
439
|
-
async update() { return {}; },
|
|
440
|
-
async stop() { alive = false; },
|
|
441
|
-
};
|
|
442
|
-
},
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
plugin.select({ id: 'task-report', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
446
|
-
await run(plugin, input(1, { domainTick: 1 }), pluginContext);
|
|
447
|
-
plugin.select({
|
|
448
|
-
id: 'task-report',
|
|
449
|
-
sourcePath: 'unused',
|
|
450
|
-
sourceRoot: 'unused',
|
|
451
|
-
parameters: { greeting: 'same-controller' },
|
|
452
|
-
});
|
|
453
|
-
await run(plugin, input(2, { domainTick: 6 }), pluginContext);
|
|
454
|
-
expect(releases).toEqual([{
|
|
455
|
-
capability: 'clawclaw.action',
|
|
456
|
-
id: 'task-report@activation-1',
|
|
457
|
-
reason: 'strategy_switched',
|
|
458
|
-
}]);
|
|
459
|
-
|
|
460
|
-
plugin.select({ id: 'task-only', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
461
|
-
await run(plugin, input(3, { domainTick: 11 }), pluginContext);
|
|
462
|
-
expect(releases).toEqual([
|
|
463
|
-
{
|
|
464
|
-
capability: 'clawclaw.action',
|
|
465
|
-
id: 'task-report@activation-1',
|
|
466
|
-
reason: 'strategy_switched',
|
|
467
|
-
},
|
|
468
|
-
{
|
|
469
|
-
capability: 'clawclaw.action',
|
|
470
|
-
id: 'task-report@activation-2',
|
|
471
|
-
reason: 'strategy_switched',
|
|
472
|
-
},
|
|
473
|
-
]);
|
|
474
|
-
await plugin.stop('shutdown');
|
|
475
|
-
});
|
|
476
|
-
|
|
477
|
-
it('routes persistent executor diagnostics to the epoch that is currently reducing', async () => {
|
|
478
|
-
const seen: string[] = [];
|
|
479
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
480
|
-
executorFactory: (_selection, host) => {
|
|
481
|
-
let alive = false;
|
|
482
|
-
return {
|
|
483
|
-
runtimeInstanceId: 'diagnostic-context-test',
|
|
484
|
-
get isAlive() { return alive; },
|
|
485
|
-
async start() { alive = true; },
|
|
486
|
-
async reset() {},
|
|
487
|
-
async update(frame) {
|
|
488
|
-
host.diagnostic('strategy.test.current_epoch', { epoch_id: frame.epochId });
|
|
489
|
-
return {};
|
|
490
|
-
},
|
|
491
|
-
async stop() { alive = false; },
|
|
492
|
-
};
|
|
493
|
-
},
|
|
494
|
-
});
|
|
495
|
-
plugin.select({ id: 'legacy-diagnostics', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
496
|
-
const firstContext = context((name) => seen.push(`epoch-1:${name}`));
|
|
497
|
-
const secondContext = context((name) => seen.push(`epoch-2:${name}`));
|
|
498
|
-
|
|
499
|
-
await run(plugin, input(1, { domainTick: 1 }), firstContext);
|
|
500
|
-
await run(plugin, input(2, { domainTick: 6 }), secondContext);
|
|
501
|
-
|
|
502
|
-
expect(seen.filter(item => item.endsWith('strategy.test.current_epoch'))).toEqual([
|
|
503
|
-
'epoch-1:strategy.test.current_epoch',
|
|
504
|
-
'epoch-2:strategy.test.current_epoch',
|
|
505
|
-
]);
|
|
506
|
-
await plugin.stop('shutdown');
|
|
507
|
-
});
|
|
508
|
-
|
|
509
|
-
it('rejects a late Action callback from an older same-id Strategy activation', async () => {
|
|
510
|
-
let firstActivationAction: ((action: Record<string, unknown>) => Promise<any>) | undefined;
|
|
511
|
-
let sameIdActivation = 0;
|
|
512
|
-
const acknowledgements: any[] = [];
|
|
513
|
-
const admittedCallerIds: string[] = [];
|
|
514
|
-
const baseContext = context(() => {});
|
|
515
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
516
|
-
...baseContext,
|
|
517
|
-
capabilities: {
|
|
518
|
-
...baseContext.capabilities,
|
|
519
|
-
performAction: async (_capability, _action, caller) => {
|
|
520
|
-
admittedCallerIds.push(caller?.id ?? 'missing');
|
|
521
|
-
return {
|
|
522
|
-
scope,
|
|
523
|
-
actionId: `action-${admittedCallerIds.length}`,
|
|
524
|
-
status: 'success' as const,
|
|
525
|
-
source: 'backend' as const,
|
|
526
|
-
actionOutcome: 'confirmed' as const,
|
|
527
|
-
};
|
|
528
|
-
},
|
|
529
|
-
},
|
|
530
|
-
};
|
|
531
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
532
|
-
executorFactory: (selection, host) => {
|
|
533
|
-
let alive = false;
|
|
534
|
-
const activation = selection.id === 'same-id' ? ++sameIdActivation : 0;
|
|
535
|
-
if (activation === 1) firstActivationAction = host.action;
|
|
536
|
-
return {
|
|
537
|
-
runtimeInstanceId: `${selection.id}-${activation}`,
|
|
538
|
-
get isAlive() { return alive; },
|
|
539
|
-
async start() { alive = true; },
|
|
540
|
-
async reset() {},
|
|
541
|
-
async update() {
|
|
542
|
-
if (activation === 2) {
|
|
543
|
-
acknowledgements.push(await firstActivationAction!({ action: 'move', target: 'engine' }));
|
|
544
|
-
acknowledgements.push(await host.action({ action: 'move', target: 'engine' }));
|
|
545
|
-
}
|
|
546
|
-
return {};
|
|
547
|
-
},
|
|
548
|
-
async stop() { alive = false; },
|
|
549
|
-
};
|
|
550
|
-
},
|
|
551
|
-
});
|
|
552
|
-
|
|
553
|
-
plugin.select({ id: 'same-id', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
554
|
-
await run(plugin, input(1, { domainTick: 1 }), pluginContext);
|
|
555
|
-
plugin.select({ id: 'middle', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
556
|
-
await run(plugin, input(2, { domainTick: 6 }), pluginContext);
|
|
557
|
-
plugin.select({ id: 'same-id', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
558
|
-
await run(plugin, input(3, { domainTick: 11 }), pluginContext);
|
|
559
|
-
|
|
560
|
-
expect(acknowledgements).toEqual([
|
|
561
|
-
expect.objectContaining({
|
|
562
|
-
status: 'deferred',
|
|
563
|
-
actionOutcome: 'not_submitted',
|
|
564
|
-
error: expect.objectContaining({ code: 'STRATEGY_GENERATION_SUPERSEDED' }),
|
|
565
|
-
}),
|
|
566
|
-
expect.objectContaining({ status: 'success', actionOutcome: 'confirmed' }),
|
|
567
|
-
]);
|
|
568
|
-
expect(admittedCallerIds).toEqual(['same-id@activation-3']);
|
|
569
|
-
await plugin.stop('shutdown');
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
it('advertises parallel scheduler capacity only for a parallel selection', async () => {
|
|
573
|
-
let alive = false;
|
|
574
|
-
const executor: ClawclawStrategyExecutor = {
|
|
575
|
-
runtimeInstanceId: 'capacity-instance',
|
|
576
|
-
get isAlive() { return alive; },
|
|
577
|
-
async start() { alive = true; },
|
|
578
|
-
async reset() {},
|
|
579
|
-
async update() { return {}; },
|
|
580
|
-
async stop() { alive = false; },
|
|
581
|
-
};
|
|
582
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
583
|
-
executorFactory: () => executor,
|
|
584
|
-
defaultSelectionForRole: () => ({
|
|
585
|
-
id: 'parallel-default',
|
|
586
|
-
execution: 'parallel_compute',
|
|
587
|
-
sourcePath: 'unused',
|
|
588
|
-
sourceRoot: 'unused',
|
|
589
|
-
}),
|
|
590
|
-
});
|
|
591
|
-
expect(plugin.maxInFlight).toBe(1);
|
|
592
|
-
|
|
593
|
-
const roleInput = input(1);
|
|
594
|
-
roleInput.epoch.state.game_snapshot.state.you.role = 'shrimp_generic';
|
|
595
|
-
await plugin.update(roleInput, context(() => {}));
|
|
596
|
-
expect(plugin.maxInFlight).toBe(10);
|
|
597
|
-
expect(plugin.status()).toMatchObject({
|
|
598
|
-
pending: 'parallel-default',
|
|
599
|
-
running: true,
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
plugin.select({ id: 'legacy', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
603
|
-
expect(plugin.maxInFlight).toBe(1);
|
|
604
|
-
await plugin.stop('shutdown');
|
|
605
|
-
});
|
|
606
|
-
|
|
607
|
-
it('rebuilds a parallel executor after its first start fails', async () => {
|
|
608
|
-
const stopped: string[] = [];
|
|
609
|
-
let factoryCalls = 0;
|
|
610
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
611
|
-
executorFactory: () => {
|
|
612
|
-
factoryCalls += 1;
|
|
613
|
-
const attempt = factoryCalls;
|
|
614
|
-
let alive = false;
|
|
615
|
-
return {
|
|
616
|
-
runtimeInstanceId: `start-attempt-${attempt}`,
|
|
617
|
-
get isAlive() { return alive; },
|
|
618
|
-
async start() {
|
|
619
|
-
if (attempt === 1) throw new Error('first_start_failed');
|
|
620
|
-
alive = true;
|
|
621
|
-
},
|
|
622
|
-
async reset() {},
|
|
623
|
-
async update() { return {}; },
|
|
624
|
-
async stop(reason) { alive = false; stopped.push(`${attempt}:${reason}`); },
|
|
625
|
-
} satisfies ClawclawStrategyExecutor;
|
|
626
|
-
},
|
|
627
|
-
});
|
|
628
|
-
plugin.select({
|
|
629
|
-
id: 'parallel-restart',
|
|
630
|
-
execution: 'parallel_compute',
|
|
631
|
-
sourcePath: 'unused',
|
|
632
|
-
sourceRoot: 'unused',
|
|
633
|
-
});
|
|
634
|
-
const pluginContext = context(() => {});
|
|
635
|
-
|
|
636
|
-
await expect(plugin.update(input(1, { domainTick: 100 }), pluginContext))
|
|
637
|
-
.rejects.toThrow('first_start_failed');
|
|
638
|
-
expect(stopped).toEqual(['1:switched']);
|
|
639
|
-
await expect(plugin.update(input(2, { domainTick: 105 }), pluginContext))
|
|
640
|
-
.resolves.toMatchObject({ outcome: 'processed' });
|
|
641
|
-
expect(factoryCalls).toBe(2);
|
|
642
|
-
expect(plugin.status().running).toBe(true);
|
|
643
|
-
await plugin.stop('shutdown');
|
|
644
|
-
});
|
|
645
|
-
|
|
646
|
-
it('fully retires the previous parallel pool before starting its replacement', async () => {
|
|
647
|
-
let activePools = 0;
|
|
648
|
-
let maximumActivePools = 0;
|
|
649
|
-
let releaseOldStop!: () => void;
|
|
650
|
-
let markOldStopStarted!: () => void;
|
|
651
|
-
const oldStopStarted = new Promise<void>((resolve) => { markOldStopStarted = resolve; });
|
|
652
|
-
const oldMayStop = new Promise<void>((resolve) => { releaseOldStop = resolve; });
|
|
653
|
-
const starts: string[] = [];
|
|
654
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
655
|
-
executorFactory: (selection) => {
|
|
656
|
-
let alive = false;
|
|
657
|
-
return {
|
|
658
|
-
runtimeInstanceId: `pool-${selection.id}`,
|
|
659
|
-
get isAlive() { return alive; },
|
|
660
|
-
async start(_parameters, signal) {
|
|
661
|
-
if (signal.aborted) throw signal.reason;
|
|
662
|
-
alive = true;
|
|
663
|
-
activePools += 1;
|
|
664
|
-
maximumActivePools = Math.max(maximumActivePools, activePools);
|
|
665
|
-
starts.push(selection.id);
|
|
666
|
-
},
|
|
667
|
-
async reset() {},
|
|
668
|
-
async update() { return {}; },
|
|
669
|
-
async stop() {
|
|
670
|
-
if (!alive) return;
|
|
671
|
-
if (selection.id === 'parallel-a') {
|
|
672
|
-
markOldStopStarted();
|
|
673
|
-
await oldMayStop;
|
|
674
|
-
}
|
|
675
|
-
alive = false;
|
|
676
|
-
activePools -= 1;
|
|
677
|
-
},
|
|
678
|
-
} satisfies ClawclawStrategyExecutor;
|
|
679
|
-
},
|
|
680
|
-
});
|
|
681
|
-
const pluginContext = context(() => {});
|
|
682
|
-
plugin.select({
|
|
683
|
-
id: 'parallel-a',
|
|
684
|
-
execution: 'parallel_compute',
|
|
685
|
-
sourcePath: 'unused',
|
|
686
|
-
sourceRoot: 'unused',
|
|
687
|
-
});
|
|
688
|
-
await run(plugin, input(1, { domainTick: 100 }), pluginContext);
|
|
689
|
-
|
|
690
|
-
plugin.select({
|
|
691
|
-
id: 'parallel-b',
|
|
692
|
-
execution: 'parallel_compute',
|
|
693
|
-
sourcePath: 'unused',
|
|
694
|
-
sourceRoot: 'unused',
|
|
695
|
-
});
|
|
696
|
-
const replacementInput = input(2, { domainTick: 105 });
|
|
697
|
-
const replacement = plugin.update(replacementInput, pluginContext);
|
|
698
|
-
await oldStopStarted;
|
|
699
|
-
expect(starts).toEqual(['parallel-a']);
|
|
700
|
-
expect(activePools).toBe(1);
|
|
701
|
-
expect(maximumActivePools).toBe(1);
|
|
702
|
-
|
|
703
|
-
releaseOldStop();
|
|
704
|
-
const replacementComputation = await replacement;
|
|
705
|
-
await plugin.commit(replacementInput, replacementComputation, pluginContext);
|
|
706
|
-
expect(starts).toEqual(['parallel-a', 'parallel-b']);
|
|
707
|
-
expect(activePools).toBe(1);
|
|
708
|
-
expect(maximumActivePools).toBe(1);
|
|
709
|
-
|
|
710
|
-
await plugin.stop('shutdown');
|
|
711
|
-
expect(activePools).toBe(0);
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it('waits for a superseded pool transition when the plugin stops', async () => {
|
|
715
|
-
let activePools = 0;
|
|
716
|
-
let releaseOldStop!: () => void;
|
|
717
|
-
let markOldStopStarted!: () => void;
|
|
718
|
-
const oldStopStarted = new Promise<void>((resolve) => { markOldStopStarted = resolve; });
|
|
719
|
-
const oldMayStop = new Promise<void>((resolve) => { releaseOldStop = resolve; });
|
|
720
|
-
const starts: string[] = [];
|
|
721
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
722
|
-
executorFactory: (selection) => {
|
|
723
|
-
let alive = false;
|
|
724
|
-
return {
|
|
725
|
-
runtimeInstanceId: `stop-transition-${selection.id}`,
|
|
726
|
-
get isAlive() { return alive; },
|
|
727
|
-
async start(_parameters, signal) {
|
|
728
|
-
if (signal.aborted) throw signal.reason;
|
|
729
|
-
alive = true;
|
|
730
|
-
activePools += 1;
|
|
731
|
-
starts.push(selection.id);
|
|
732
|
-
},
|
|
733
|
-
async reset() {},
|
|
734
|
-
async update() { return {}; },
|
|
735
|
-
async stop() {
|
|
736
|
-
if (!alive) return;
|
|
737
|
-
if (selection.id === 'parallel-old') {
|
|
738
|
-
markOldStopStarted();
|
|
739
|
-
await oldMayStop;
|
|
740
|
-
}
|
|
741
|
-
alive = false;
|
|
742
|
-
activePools -= 1;
|
|
743
|
-
},
|
|
744
|
-
} satisfies ClawclawStrategyExecutor;
|
|
745
|
-
},
|
|
746
|
-
});
|
|
747
|
-
const pluginContext = context(() => {});
|
|
748
|
-
plugin.select({
|
|
749
|
-
id: 'parallel-old',
|
|
750
|
-
execution: 'parallel_compute',
|
|
751
|
-
sourcePath: 'unused',
|
|
752
|
-
sourceRoot: 'unused',
|
|
753
|
-
});
|
|
754
|
-
await run(plugin, input(1, { domainTick: 100 }), pluginContext);
|
|
755
|
-
|
|
756
|
-
plugin.select({
|
|
757
|
-
id: 'parallel-never-started',
|
|
758
|
-
execution: 'parallel_compute',
|
|
759
|
-
sourcePath: 'unused',
|
|
760
|
-
sourceRoot: 'unused',
|
|
761
|
-
});
|
|
762
|
-
const replacement = plugin.update(input(2, { domainTick: 105 }), pluginContext);
|
|
763
|
-
await oldStopStarted;
|
|
764
|
-
let stopCompleted = false;
|
|
765
|
-
const stopping = plugin.stop('shutdown').then(() => { stopCompleted = true; });
|
|
766
|
-
await Promise.resolve();
|
|
767
|
-
expect(stopCompleted).toBe(false);
|
|
768
|
-
|
|
769
|
-
releaseOldStop();
|
|
770
|
-
await stopping;
|
|
771
|
-
await expect(replacement).rejects.toThrow('strategy_parallel_executor_stopped:shutdown');
|
|
772
|
-
expect(activePools).toBe(0);
|
|
773
|
-
expect(starts).toEqual(['parallel-old']);
|
|
774
|
-
});
|
|
775
|
-
|
|
776
|
-
it('keeps shared parallel startup alive when the first epoch is cancelled', async () => {
|
|
777
|
-
let resolveStart!: () => void;
|
|
778
|
-
const mayStart = new Promise<void>((resolve) => { resolveStart = resolve; });
|
|
779
|
-
let lifecycleSignal: AbortSignal | undefined;
|
|
780
|
-
let alive = false;
|
|
781
|
-
let factoryCalls = 0;
|
|
782
|
-
const frames: number[] = [];
|
|
783
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
784
|
-
executorFactory: () => {
|
|
785
|
-
factoryCalls += 1;
|
|
786
|
-
return {
|
|
787
|
-
runtimeInstanceId: 'slow-parallel-start',
|
|
788
|
-
get isAlive() { return alive; },
|
|
789
|
-
async start(_parameters, signal) {
|
|
790
|
-
lifecycleSignal = signal;
|
|
791
|
-
await mayStart;
|
|
792
|
-
if (signal.aborted) throw signal.reason;
|
|
793
|
-
alive = true;
|
|
794
|
-
},
|
|
795
|
-
async reset() {},
|
|
796
|
-
async update(frame) {
|
|
797
|
-
frames.push(frame.epochId);
|
|
798
|
-
return {};
|
|
799
|
-
},
|
|
800
|
-
async stop() { alive = false; },
|
|
801
|
-
} satisfies ClawclawStrategyExecutor;
|
|
802
|
-
},
|
|
803
|
-
});
|
|
804
|
-
plugin.select({
|
|
805
|
-
id: 'slow-parallel-start',
|
|
806
|
-
execution: 'parallel_compute',
|
|
807
|
-
sourcePath: 'unused',
|
|
808
|
-
sourceRoot: 'unused',
|
|
809
|
-
});
|
|
810
|
-
const firstController = new AbortController();
|
|
811
|
-
const secondController = new AbortController();
|
|
812
|
-
const firstContext = { ...context(() => {}), signal: firstController.signal };
|
|
813
|
-
const secondContext = { ...context(() => {}), signal: secondController.signal };
|
|
814
|
-
|
|
815
|
-
const first = plugin.update(input(1, { domainTick: 100 }), firstContext);
|
|
816
|
-
await vi.waitFor(() => expect(lifecycleSignal).toBeDefined());
|
|
817
|
-
const second = plugin.update(input(2, { domainTick: 105 }), secondContext);
|
|
818
|
-
firstController.abort(new Error('first_epoch_cancelled'));
|
|
819
|
-
await expect(first).rejects.toThrow('first_epoch_cancelled');
|
|
820
|
-
expect(lifecycleSignal?.aborted).toBe(false);
|
|
821
|
-
resolveStart();
|
|
822
|
-
await expect(second).resolves.toMatchObject({ outcome: 'processed' });
|
|
823
|
-
expect(factoryCalls).toBe(1);
|
|
824
|
-
expect(frames).toEqual([2]);
|
|
825
|
-
await plugin.stop('shutdown');
|
|
826
|
-
});
|
|
827
|
-
|
|
828
|
-
it('selects the configured role default before the first wandering update', async () => {
|
|
829
|
-
const frames: number[] = [];
|
|
830
|
-
let alive = false;
|
|
831
|
-
const executor: ClawclawStrategyExecutor = {
|
|
832
|
-
runtimeInstanceId: 'default-strategy-instance',
|
|
833
|
-
get isAlive() { return alive; },
|
|
834
|
-
async start() { alive = true; },
|
|
835
|
-
async reset() {},
|
|
836
|
-
async update(frame) { frames.push(frame.epochId); return {}; },
|
|
837
|
-
async stop() { alive = false; },
|
|
838
|
-
};
|
|
839
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
840
|
-
executorFactory: () => executor,
|
|
841
|
-
defaultSelectionForRole: (role) => ({
|
|
842
|
-
id: `default-${role}`,
|
|
843
|
-
sourcePath: 'unused',
|
|
844
|
-
sourceRoot: 'unused',
|
|
845
|
-
}),
|
|
846
|
-
});
|
|
847
|
-
const roleInput = input(1);
|
|
848
|
-
roleInput.epoch.state.game_snapshot.state.you.role = 'shrimp_generic';
|
|
849
|
-
|
|
850
|
-
await run(plugin, roleInput, context(() => {}));
|
|
851
|
-
|
|
852
|
-
expect(plugin.status()).toEqual({
|
|
853
|
-
selected: 'default-shrimp_generic',
|
|
854
|
-
running: true,
|
|
855
|
-
});
|
|
856
|
-
expect(frames).toEqual([1]);
|
|
857
|
-
});
|
|
858
|
-
|
|
859
|
-
it('resets stale private execution state before updating the latest epoch after a gap', async () => {
|
|
860
|
-
const frames: number[] = [];
|
|
861
|
-
let resets = 0;
|
|
862
|
-
let alive = false;
|
|
863
|
-
const executor: ClawclawStrategyExecutor = {
|
|
864
|
-
runtimeInstanceId: 'test-strategy-instance',
|
|
865
|
-
get isAlive() { return alive; },
|
|
866
|
-
async start() { alive = true; },
|
|
867
|
-
async reset() { resets += 1; },
|
|
868
|
-
async update(frame: StrategyFrame<Record<string, unknown>>) {
|
|
869
|
-
frames.push(frame.epochId);
|
|
870
|
-
return {};
|
|
871
|
-
},
|
|
872
|
-
async stop() { alive = false; },
|
|
873
|
-
};
|
|
874
|
-
const diagnostics: Array<{ name: string; attributes?: Readonly<Record<string, unknown>> }> = [];
|
|
875
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
876
|
-
plugin.select({ id: 'test-strategy', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
877
|
-
const pluginContext = context((name, attributes) => diagnostics.push({ name, attributes }));
|
|
878
|
-
|
|
879
|
-
await run(plugin, input(1), pluginContext);
|
|
880
|
-
await run(plugin, input(10), pluginContext);
|
|
881
|
-
|
|
882
|
-
expect(frames).toEqual([1, 10]);
|
|
883
|
-
expect(resets).toBe(1);
|
|
884
|
-
expect(diagnostics).toContainEqual(expect.objectContaining({
|
|
885
|
-
name: 'strategy.catchup_gap',
|
|
886
|
-
attributes: expect.objectContaining({
|
|
887
|
-
skipped_from_epoch_seq: 2,
|
|
888
|
-
skipped_through_epoch_seq: 9,
|
|
889
|
-
skipped_epoch_count: 8,
|
|
890
|
-
}),
|
|
891
|
-
}));
|
|
892
|
-
});
|
|
893
|
-
|
|
894
|
-
it('uses every distinct backend domain tick and keeps one executor', async () => {
|
|
895
|
-
const frames: Array<{ epochId: number; tick?: number; stateTick?: number }> = [];
|
|
896
|
-
let factoryCalls = 0;
|
|
897
|
-
let starts = 0;
|
|
898
|
-
let alive = false;
|
|
899
|
-
const executor: ClawclawStrategyExecutor = {
|
|
900
|
-
runtimeInstanceId: 'persistent-strategy-instance',
|
|
901
|
-
get isAlive() { return alive; },
|
|
902
|
-
async start() { starts += 1; alive = true; },
|
|
903
|
-
async reset() {},
|
|
904
|
-
async update(frame) {
|
|
905
|
-
frames.push({
|
|
906
|
-
epochId: frame.epochId,
|
|
907
|
-
tick: frame.tick,
|
|
908
|
-
stateTick: typeof frame.state?.tick === 'number' ? frame.state.tick : undefined,
|
|
909
|
-
});
|
|
910
|
-
return {};
|
|
911
|
-
},
|
|
912
|
-
async stop() { alive = false; },
|
|
913
|
-
};
|
|
914
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
915
|
-
executorFactory: () => {
|
|
916
|
-
factoryCalls += 1;
|
|
917
|
-
return executor;
|
|
918
|
-
},
|
|
919
|
-
});
|
|
920
|
-
plugin.select({ id: 'persistent', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
921
|
-
const pluginContext = context(() => {});
|
|
922
|
-
|
|
923
|
-
const first = await run(plugin, input(1, { domainTick: 100, stateTick: 999 }), pluginContext);
|
|
924
|
-
const second = await run(plugin, input(2, { domainTick: 101 }), pluginContext);
|
|
925
|
-
const third = await run(plugin, input(3, { domainTick: 104 }), pluginContext);
|
|
926
|
-
const duplicate = await run(plugin, input(4, { domainTick: 104 }), pluginContext);
|
|
927
|
-
const fifth = await run(plugin, input(5, { domainTick: 105 }), pluginContext);
|
|
928
|
-
const stale = await run(plugin, input(6, { domainTick: 99 }), pluginContext);
|
|
929
|
-
const next = await run(plugin, input(7, { domainTick: 106 }), pluginContext);
|
|
930
|
-
|
|
931
|
-
expect(first.outcome).toBe('processed');
|
|
932
|
-
expect(second.outcome).toBe('processed');
|
|
933
|
-
expect(third.outcome).toBe('processed');
|
|
934
|
-
expect(duplicate).toEqual({ outcome: 'noop', reason: 'strategy_update_interval' });
|
|
935
|
-
expect(fifth.outcome).toBe('processed');
|
|
936
|
-
expect(stale).toEqual({ outcome: 'noop', reason: 'strategy_update_interval' });
|
|
937
|
-
expect(next.outcome).toBe('processed');
|
|
938
|
-
expect(frames).toEqual([
|
|
939
|
-
{ epochId: 1, tick: 100, stateTick: 100 },
|
|
940
|
-
{ epochId: 2, tick: 101, stateTick: 101 },
|
|
941
|
-
{ epochId: 3, tick: 104, stateTick: 104 },
|
|
942
|
-
{ epochId: 5, tick: 105, stateTick: 105 },
|
|
943
|
-
{ epochId: 7, tick: 106, stateTick: 106 },
|
|
944
|
-
]);
|
|
945
|
-
expect(factoryCalls).toBe(1);
|
|
946
|
-
expect(starts).toBe(1);
|
|
947
|
-
});
|
|
948
|
-
|
|
949
|
-
it('serves dynamic tasks and players directly from the Strategy frame', async () => {
|
|
950
|
-
let alive = false;
|
|
951
|
-
const observed: unknown[] = [];
|
|
952
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
953
|
-
executorFactory: (_selection, host) => ({
|
|
954
|
-
runtimeInstanceId: 'frame-query-instance',
|
|
955
|
-
get isAlive() { return alive; },
|
|
956
|
-
async start() { alive = true; },
|
|
957
|
-
async reset() {},
|
|
958
|
-
async update(frame) {
|
|
959
|
-
observed.push(
|
|
960
|
-
await host.gameQuery({ kind: 'tasks' }, frame),
|
|
961
|
-
await host.gameQuery({ kind: 'map' }, frame),
|
|
962
|
-
);
|
|
963
|
-
return {};
|
|
964
|
-
},
|
|
965
|
-
async stop() { alive = false; },
|
|
966
|
-
}),
|
|
967
|
-
});
|
|
968
|
-
plugin.select({ id: 'frame-query', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
969
|
-
const pluginInput = input(1);
|
|
970
|
-
pluginInput.epoch.state.game_snapshot.state.you.tasks = [{
|
|
971
|
-
name: 'repair', room: 'engine', status: 'normal', x: 3, y: 4,
|
|
972
|
-
}];
|
|
973
|
-
pluginInput.epoch.state.game_snapshot.state.players = [{ name: 'player-2', seat: 2 }];
|
|
974
|
-
(pluginInput.currentState.state as Record<string, any>).you.tasks = [{
|
|
975
|
-
name: 'repair', room: 'engine', status: 'normal', x: 3, y: 4,
|
|
976
|
-
}];
|
|
977
|
-
(pluginInput.currentState.state as Record<string, any>).players = [{ name: 'player-2', seat: 2 }];
|
|
978
|
-
|
|
979
|
-
await run(plugin, pluginInput, context(() => {}));
|
|
980
|
-
|
|
981
|
-
expect(observed[0]).toEqual({
|
|
982
|
-
status: 'success',
|
|
983
|
-
value: [{ name: 'repair', room: 'engine', status: 'normal', x: 3, y: 4 }],
|
|
984
|
-
});
|
|
985
|
-
expect(observed[1]).toMatchObject({
|
|
986
|
-
status: 'success',
|
|
987
|
-
value: {
|
|
988
|
-
all_players: [{ name: 'player-2', seat: 2 }],
|
|
989
|
-
your_tasks: [{ name: 'repair', room: 'engine', status: 'normal', x: 3, y: 4 }],
|
|
990
|
-
},
|
|
991
|
-
});
|
|
992
|
-
await plugin.stop('shutdown');
|
|
993
|
-
});
|
|
994
|
-
|
|
995
|
-
it('consumes Events on every new backend tick but not on a same-tick revision', async () => {
|
|
996
|
-
const eventIdsByFrame: string[][] = [];
|
|
997
|
-
let alive = false;
|
|
998
|
-
const executor: ClawclawStrategyExecutor = {
|
|
999
|
-
runtimeInstanceId: 'event-accumulation-instance',
|
|
1000
|
-
get isAlive() { return alive; },
|
|
1001
|
-
async start() { alive = true; },
|
|
1002
|
-
async reset() {},
|
|
1003
|
-
async update(frame) {
|
|
1004
|
-
eventIdsByFrame.push(frame.events.map((item) => item.eventId));
|
|
1005
|
-
return {};
|
|
1006
|
-
},
|
|
1007
|
-
async stop() { alive = false; },
|
|
1008
|
-
};
|
|
1009
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1010
|
-
plugin.select({ id: 'events', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
1011
|
-
const pluginContext = context(() => {});
|
|
1012
|
-
const firstEvent = event(1);
|
|
1013
|
-
const secondEvent = event(2);
|
|
1014
|
-
const thirdEvent = event(3);
|
|
1015
|
-
const lateSameTickEvent = event(4);
|
|
1016
|
-
|
|
1017
|
-
await run(plugin, input(1, { domainTick: 200 }), pluginContext);
|
|
1018
|
-
const processed201 = await run(plugin, input(2, {
|
|
1019
|
-
domainTick: 201,
|
|
1020
|
-
pendingEvents: [firstEvent],
|
|
1021
|
-
}), pluginContext);
|
|
1022
|
-
const processed202 = await run(plugin, input(3, {
|
|
1023
|
-
domainTick: 202,
|
|
1024
|
-
pendingEvents: [secondEvent],
|
|
1025
|
-
}), pluginContext);
|
|
1026
|
-
const processed205 = await run(plugin, input(4, {
|
|
1027
|
-
domainTick: 205,
|
|
1028
|
-
pendingEvents: [thirdEvent],
|
|
1029
|
-
}), pluginContext);
|
|
1030
|
-
const duplicate205 = await run(plugin, input(5, {
|
|
1031
|
-
domainTick: 205,
|
|
1032
|
-
pendingEvents: [lateSameTickEvent],
|
|
1033
|
-
}), pluginContext);
|
|
1034
|
-
const processed210 = await run(plugin, input(6, {
|
|
1035
|
-
domainTick: 210,
|
|
1036
|
-
pendingEvents: [lateSameTickEvent],
|
|
1037
|
-
}), pluginContext);
|
|
1038
|
-
|
|
1039
|
-
expect(processed201.consumeEventsThroughCommitSeq).toBe(1);
|
|
1040
|
-
expect(processed202.consumeEventsThroughCommitSeq).toBe(2);
|
|
1041
|
-
expect(duplicate205.consumeEventsThroughCommitSeq).toBeUndefined();
|
|
1042
|
-
expect(processed205.consumeEventsThroughCommitSeq).toBe(3);
|
|
1043
|
-
expect(processed210.consumeEventsThroughCommitSeq).toBe(4);
|
|
1044
|
-
expect(eventIdsByFrame).toEqual([
|
|
1045
|
-
[],
|
|
1046
|
-
['event-1'],
|
|
1047
|
-
['event-2'],
|
|
1048
|
-
['event-3'],
|
|
1049
|
-
['event-4'],
|
|
1050
|
-
]);
|
|
1051
|
-
});
|
|
1052
|
-
|
|
1053
|
-
it('fences selected noops by generation while preserving no-selection backlog disposal', async () => {
|
|
1054
|
-
let alive = false;
|
|
1055
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1056
|
-
runtimeInstanceId: 'noop-generation-instance',
|
|
1057
|
-
get isAlive() { return alive; },
|
|
1058
|
-
async start() { alive = true; },
|
|
1059
|
-
async reset() {},
|
|
1060
|
-
async update() { return {}; },
|
|
1061
|
-
async stop() { alive = false; },
|
|
1062
|
-
};
|
|
1063
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1064
|
-
const pluginContext = context(() => {});
|
|
1065
|
-
plugin.select({ id: 'first', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
1066
|
-
await run(plugin, input(1), pluginContext);
|
|
1067
|
-
const meetingInput = input(2, { pendingEvents: [event(1)] });
|
|
1068
|
-
meetingInput.epoch.state.stage = 'meeting';
|
|
1069
|
-
meetingInput.epoch.state.game_snapshot.state.phase = 'meeting';
|
|
1070
|
-
const staleNoop = await plugin.update(meetingInput, pluginContext);
|
|
1071
|
-
plugin.select({ id: 'second', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
1072
|
-
|
|
1073
|
-
await expect(plugin.commit(meetingInput, staleNoop, pluginContext)).resolves.toEqual({
|
|
1074
|
-
outcome: 'noop',
|
|
1075
|
-
reason: 'strategy_computation_superseded',
|
|
1076
|
-
});
|
|
1077
|
-
expect((plugin as unknown as { wanderingResetRequired: boolean }).wanderingResetRequired)
|
|
1078
|
-
.toBe(false);
|
|
1079
|
-
|
|
1080
|
-
const initiallyEmpty = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1081
|
-
const backlogInput = input(3, { pendingEvents: [event(2)] });
|
|
1082
|
-
const noSelectionNoop = await initiallyEmpty.update(backlogInput, pluginContext);
|
|
1083
|
-
initiallyEmpty.select({ id: 'later', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
1084
|
-
await expect(initiallyEmpty.commit(backlogInput, noSelectionNoop, pluginContext)).resolves.toEqual({
|
|
1085
|
-
outcome: 'noop',
|
|
1086
|
-
reason: 'strategy_not_selected',
|
|
1087
|
-
consumeEventsThroughCommitSeq: 2,
|
|
1088
|
-
});
|
|
1089
|
-
await plugin.stop('shutdown');
|
|
1090
|
-
await initiallyEmpty.stop('shutdown');
|
|
1091
|
-
});
|
|
1092
|
-
|
|
1093
|
-
it('deduplicates same-tick execution without suppressing later-tick output Events', async () => {
|
|
1094
|
-
let alive = false;
|
|
1095
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1096
|
-
runtimeInstanceId: 'event-output-instance',
|
|
1097
|
-
get isAlive() { return alive; },
|
|
1098
|
-
async start() { alive = true; },
|
|
1099
|
-
async reset() {},
|
|
1100
|
-
async update() {
|
|
1101
|
-
return { events: [{ type: 'strategy_alert', payload: { message: 'test' } }] };
|
|
1102
|
-
},
|
|
1103
|
-
async stop() { alive = false; },
|
|
1104
|
-
};
|
|
1105
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1106
|
-
plugin.select({ id: 'output-events', sourcePath: 'unused', sourceRoot: 'unused' });
|
|
1107
|
-
const pluginContext = context(() => {});
|
|
1108
|
-
|
|
1109
|
-
const tick300 = await run(plugin, input(1, { domainTick: 300 }), pluginContext);
|
|
1110
|
-
const duplicate300 = await run(plugin, input(10, { domainTick: 300 }), pluginContext);
|
|
1111
|
-
const tick305 = await run(plugin, input(11, { domainTick: 305 }), pluginContext);
|
|
1112
|
-
|
|
1113
|
-
expect(tick300.events?.[0]?.dedupeKey).toBe('output-events:300:strategy_alert:0');
|
|
1114
|
-
expect(duplicate300.events).toBeUndefined();
|
|
1115
|
-
expect(tick305.events?.[0]?.dedupeKey).toBe('output-events:305:strategy_alert:0');
|
|
1116
|
-
});
|
|
1117
|
-
|
|
1118
|
-
it('hosts the active strategy and effective named controls in strategy.status', async () => {
|
|
1119
|
-
let alive = false;
|
|
1120
|
-
let effective = { greetings: ['hello'] } as Record<string, unknown>;
|
|
1121
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1122
|
-
runtimeInstanceId: 'semantic-status-instance',
|
|
1123
|
-
get isAlive() { return alive; },
|
|
1124
|
-
async start(parameters) { effective = { ...parameters }; alive = true; },
|
|
1125
|
-
async applyControl(update) {
|
|
1126
|
-
effective = { ...effective, ...update.values };
|
|
1127
|
-
return {
|
|
1128
|
-
accepted: true,
|
|
1129
|
-
appliedRevision: update.revision,
|
|
1130
|
-
effectiveValues: effective,
|
|
1131
|
-
};
|
|
1132
|
-
},
|
|
1133
|
-
async reset() {},
|
|
1134
|
-
async update() {
|
|
1135
|
-
return {
|
|
1136
|
-
states: [{
|
|
1137
|
-
topic: 'strategy.status',
|
|
1138
|
-
value: { activity: 'waiting', summary: '等待下一次观察。' },
|
|
1139
|
-
}],
|
|
1140
|
-
};
|
|
1141
|
-
},
|
|
1142
|
-
async stop() { alive = false; },
|
|
1143
|
-
};
|
|
1144
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1145
|
-
plugin.select({
|
|
1146
|
-
id: 'task-report',
|
|
1147
|
-
sourcePath: 'unused',
|
|
1148
|
-
sourceRoot: 'unused',
|
|
1149
|
-
parameters: { greetings: ['hello'] },
|
|
1150
|
-
});
|
|
1151
|
-
const pluginContext = context(() => {});
|
|
1152
|
-
|
|
1153
|
-
const initial = await run(plugin, input(1, { domainTick: 400 }), pluginContext);
|
|
1154
|
-
expect(initial.states).toEqual([{
|
|
1155
|
-
topic: 'strategy.status',
|
|
1156
|
-
value: {
|
|
1157
|
-
activity: 'waiting',
|
|
1158
|
-
summary: '等待下一次观察。',
|
|
1159
|
-
strategy: 'task-report',
|
|
1160
|
-
parameters: { greetings: ['hello'] },
|
|
1161
|
-
},
|
|
1162
|
-
}]);
|
|
1163
|
-
|
|
1164
|
-
const controlled = plugin.control({ greetings: ['你好'] });
|
|
1165
|
-
const updated = await run(plugin, input(2, { domainTick: 405 }), pluginContext);
|
|
1166
|
-
await expect(controlled).resolves.toMatchObject({
|
|
1167
|
-
accepted: true,
|
|
1168
|
-
effectiveValues: { greetings: ['你好'] },
|
|
1169
|
-
});
|
|
1170
|
-
expect(updated.states?.[0]?.value).toMatchObject({
|
|
1171
|
-
strategy: 'task-report',
|
|
1172
|
-
parameters: { greetings: ['你好'] },
|
|
1173
|
-
});
|
|
1174
|
-
});
|
|
1175
|
-
|
|
1176
|
-
it('allows parallel computations to finish out of order but commits Actions, State, Events and cursors in epoch order', async () => {
|
|
1177
|
-
const completions = new Map<number, (result: StrategyResult) => void>();
|
|
1178
|
-
let alive = false;
|
|
1179
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1180
|
-
runtimeInstanceId: 'parallel-order-instance',
|
|
1181
|
-
get isAlive() { return alive; },
|
|
1182
|
-
async start() { alive = true; },
|
|
1183
|
-
async reset() {},
|
|
1184
|
-
update(frame) {
|
|
1185
|
-
return new Promise((resolve) => completions.set(frame.epochId, resolve));
|
|
1186
|
-
},
|
|
1187
|
-
async stop() { alive = false; },
|
|
1188
|
-
};
|
|
1189
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1190
|
-
plugin.select({
|
|
1191
|
-
id: 'parallel-order',
|
|
1192
|
-
execution: 'parallel_compute',
|
|
1193
|
-
sourcePath: 'unused',
|
|
1194
|
-
sourceRoot: 'unused',
|
|
1195
|
-
});
|
|
1196
|
-
const firstInput = input(1, { domainTick: 100, pendingEvents: [event(1)] });
|
|
1197
|
-
const secondInput = input(2, { domainTick: 105, pendingEvents: [event(1), event(2)] });
|
|
1198
|
-
const actions: Array<{ epoch: number; action: Record<string, unknown> }> = [];
|
|
1199
|
-
const contexts = actionContext(actions);
|
|
1200
|
-
const firstContext = contexts.forEpoch(1);
|
|
1201
|
-
const secondContext = contexts.forEpoch(2);
|
|
1202
|
-
|
|
1203
|
-
const firstPending = plugin.update(firstInput, firstContext);
|
|
1204
|
-
const secondPending = plugin.update(secondInput, secondContext);
|
|
1205
|
-
await vi.waitFor(() => expect([...completions.keys()]).toEqual([1, 2]));
|
|
1206
|
-
completions.get(2)!({
|
|
1207
|
-
actions: [{ action: { action: 'move', target_x: 2, target_y: 2 }, dedupeKey: 'move-2' }],
|
|
1208
|
-
states: [{ topic: 'strategy.status', value: { activity: 'moving', summary: 'epoch 2' } }],
|
|
1209
|
-
events: [{ type: 'strategy_alert', dedupeKey: 'alert-2', payload: { epoch: 2 } }],
|
|
1210
|
-
});
|
|
1211
|
-
const secondComputation = await secondPending;
|
|
1212
|
-
let firstCompleted = false;
|
|
1213
|
-
void firstPending.then(() => { firstCompleted = true; });
|
|
1214
|
-
await Promise.resolve();
|
|
1215
|
-
expect(firstCompleted).toBe(false);
|
|
1216
|
-
completions.get(1)!({
|
|
1217
|
-
actions: [{ action: { action: 'move', target_x: 1, target_y: 1 }, dedupeKey: 'move-1' }],
|
|
1218
|
-
states: [{ topic: 'strategy.status', value: { activity: 'moving', summary: 'epoch 1' } }],
|
|
1219
|
-
events: [{ type: 'strategy_alert', dedupeKey: 'alert-1', payload: { epoch: 1 } }],
|
|
1220
|
-
});
|
|
1221
|
-
const firstComputation = await firstPending;
|
|
1222
|
-
|
|
1223
|
-
expect(firstComputation.consumeEventsThroughCommitSeq).toBeUndefined();
|
|
1224
|
-
expect(secondComputation.consumeEventsThroughCommitSeq).toBeUndefined();
|
|
1225
|
-
const firstCommitted = await plugin.commit(firstInput, firstComputation, firstContext);
|
|
1226
|
-
const secondCommitted = await plugin.commit(secondInput, secondComputation, secondContext);
|
|
1227
|
-
|
|
1228
|
-
expect(actions.map((entry) => entry.action)).toEqual([
|
|
1229
|
-
{
|
|
1230
|
-
route: 'auto',
|
|
1231
|
-
action: { action: 'move', target_x: 1, target_y: 1 },
|
|
1232
|
-
strategyOriginEpochSeq: 1,
|
|
1233
|
-
strategyActionSafetyGeneration: 1,
|
|
1234
|
-
},
|
|
1235
|
-
{
|
|
1236
|
-
route: 'auto',
|
|
1237
|
-
action: { action: 'move', target_x: 2, target_y: 2 },
|
|
1238
|
-
strategyOriginEpochSeq: 2,
|
|
1239
|
-
strategyActionSafetyGeneration: 1,
|
|
1240
|
-
},
|
|
1241
|
-
]);
|
|
1242
|
-
expect(firstCommitted.states?.[0]?.value).toMatchObject({ summary: 'epoch 1' });
|
|
1243
|
-
expect(secondCommitted.states?.[0]?.value).toMatchObject({ summary: 'epoch 2' });
|
|
1244
|
-
expect(firstCommitted.events?.[0]?.dedupeKey).toBe('alert-1');
|
|
1245
|
-
expect(secondCommitted.events?.[0]?.dedupeKey).toBe('alert-2');
|
|
1246
|
-
expect(firstCommitted.consumeEventsThroughCommitSeq).toBe(1);
|
|
1247
|
-
expect(secondCommitted.consumeEventsThroughCommitSeq).toBe(2);
|
|
1248
|
-
});
|
|
1249
|
-
|
|
1250
|
-
it('analyzes hybrid epochs concurrently and reduces their snapshots only in committed order', async () => {
|
|
1251
|
-
const completions = new Map<number, (analysis: unknown) => void>();
|
|
1252
|
-
const reduced: Array<{ epochId: number; analysis: unknown }> = [];
|
|
1253
|
-
let parallelAlive = false;
|
|
1254
|
-
let reducerAlive = false;
|
|
1255
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1256
|
-
executorFactory: (_selection, _host, purpose) => purpose === 'parallel'
|
|
1257
|
-
? {
|
|
1258
|
-
runtimeInstanceId: 'hybrid-analysis-pool',
|
|
1259
|
-
get isAlive() { return parallelAlive; },
|
|
1260
|
-
async start() { parallelAlive = true; },
|
|
1261
|
-
async reset() {},
|
|
1262
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1263
|
-
analyze(frame) {
|
|
1264
|
-
return new Promise(resolve => completions.set(frame.epochId, resolve));
|
|
1265
|
-
},
|
|
1266
|
-
async stop() { parallelAlive = false; },
|
|
1267
|
-
}
|
|
1268
|
-
: {
|
|
1269
|
-
runtimeInstanceId: 'hybrid-reducer',
|
|
1270
|
-
get isAlive() { return reducerAlive; },
|
|
1271
|
-
async start() { reducerAlive = true; },
|
|
1272
|
-
async reset() {},
|
|
1273
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1274
|
-
async reduce(frame, analysis) {
|
|
1275
|
-
reduced.push({ epochId: frame.epochId, analysis });
|
|
1276
|
-
return {
|
|
1277
|
-
states: [{
|
|
1278
|
-
topic: 'strategy.status',
|
|
1279
|
-
value: { activity: 'idle', summary: `epoch ${frame.epochId}` },
|
|
1280
|
-
}],
|
|
1281
|
-
};
|
|
1282
|
-
},
|
|
1283
|
-
async stop() { reducerAlive = false; },
|
|
1284
|
-
},
|
|
1285
|
-
});
|
|
1286
|
-
plugin.select({
|
|
1287
|
-
id: 'hybrid-order',
|
|
1288
|
-
execution: 'parallel_analyze',
|
|
1289
|
-
sourcePath: 'unused',
|
|
1290
|
-
sourceRoot: 'unused',
|
|
1291
|
-
});
|
|
1292
|
-
const firstInput = input(1, { domainTick: 100 });
|
|
1293
|
-
const secondInput = input(2, { domainTick: 105 });
|
|
1294
|
-
const pluginContext = context(() => {});
|
|
1295
|
-
|
|
1296
|
-
const firstPending = plugin.update(firstInput, pluginContext);
|
|
1297
|
-
const secondPending = plugin.update(secondInput, pluginContext);
|
|
1298
|
-
await vi.waitFor(() => expect([...completions.keys()]).toEqual([1, 2]));
|
|
1299
|
-
completions.get(2)!({ sourceEpoch: 2 });
|
|
1300
|
-
const secondComputation = await secondPending;
|
|
1301
|
-
expect(reduced).toEqual([]);
|
|
1302
|
-
completions.get(1)!({ sourceEpoch: 1 });
|
|
1303
|
-
const firstComputation = await firstPending;
|
|
1304
|
-
|
|
1305
|
-
await plugin.commit(firstInput, firstComputation, pluginContext);
|
|
1306
|
-
await plugin.commit(secondInput, secondComputation, pluginContext);
|
|
1307
|
-
|
|
1308
|
-
expect(reduced).toEqual([
|
|
1309
|
-
{ epochId: 1, analysis: { sourceEpoch: 1 } },
|
|
1310
|
-
{ epochId: 2, analysis: { sourceEpoch: 2 } },
|
|
1311
|
-
]);
|
|
1312
|
-
await plugin.stop('shutdown');
|
|
1313
|
-
});
|
|
1314
|
-
|
|
1315
|
-
it('applies hybrid controls to the ordered reducer and discards older parameter analyses', async () => {
|
|
1316
|
-
const analyzed: Array<{ epochId: number; greeting: unknown; style: unknown }> = [];
|
|
1317
|
-
const reduced: Array<{ epochId: number; greeting: unknown; style: unknown }> = [];
|
|
1318
|
-
const controls: Array<Readonly<Record<string, unknown>>> = [];
|
|
1319
|
-
let parallelPool = 0;
|
|
1320
|
-
let parallelAlive = false;
|
|
1321
|
-
let reducerAlive = false;
|
|
1322
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1323
|
-
executorFactory: (_selection, _host, purpose) => purpose === 'parallel'
|
|
1324
|
-
? {
|
|
1325
|
-
runtimeInstanceId: `hybrid-control-pool-${++parallelPool}`,
|
|
1326
|
-
get isAlive() { return parallelAlive; },
|
|
1327
|
-
async start() { parallelAlive = true; },
|
|
1328
|
-
async reset() {},
|
|
1329
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1330
|
-
async analyze(frame) {
|
|
1331
|
-
const analysis = {
|
|
1332
|
-
epochId: frame.epochId,
|
|
1333
|
-
greeting: frame.parameters.greeting,
|
|
1334
|
-
style: frame.parameters.style,
|
|
1335
|
-
};
|
|
1336
|
-
analyzed.push(analysis);
|
|
1337
|
-
return analysis;
|
|
1338
|
-
},
|
|
1339
|
-
async stop() { parallelAlive = false; },
|
|
1340
|
-
}
|
|
1341
|
-
: {
|
|
1342
|
-
runtimeInstanceId: 'hybrid-control-reducer',
|
|
1343
|
-
get isAlive() { return reducerAlive; },
|
|
1344
|
-
async start() { reducerAlive = true; },
|
|
1345
|
-
async reset() {},
|
|
1346
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1347
|
-
async applyControl(update) {
|
|
1348
|
-
controls.push(update.values);
|
|
1349
|
-
return {
|
|
1350
|
-
accepted: true,
|
|
1351
|
-
appliedRevision: update.revision,
|
|
1352
|
-
effectiveValues: { greeting: update.values.greeting },
|
|
1353
|
-
};
|
|
1354
|
-
},
|
|
1355
|
-
async reduce(frame, analysis) {
|
|
1356
|
-
reduced.push({
|
|
1357
|
-
epochId: frame.epochId,
|
|
1358
|
-
greeting: (analysis as { greeting: unknown }).greeting,
|
|
1359
|
-
style: (analysis as { style: unknown }).style,
|
|
1360
|
-
});
|
|
1361
|
-
return {};
|
|
1362
|
-
},
|
|
1363
|
-
async stop() { reducerAlive = false; },
|
|
1364
|
-
},
|
|
1365
|
-
});
|
|
1366
|
-
plugin.select({
|
|
1367
|
-
id: 'hybrid-controls',
|
|
1368
|
-
execution: 'parallel_analyze',
|
|
1369
|
-
controls: [{ id: 'greeting', type: 'string', description: 'Greeting.' }],
|
|
1370
|
-
parameters: { greeting: 'old', style: 'friendly' },
|
|
1371
|
-
sourcePath: 'unused',
|
|
1372
|
-
sourceRoot: 'unused',
|
|
1373
|
-
});
|
|
1374
|
-
|
|
1375
|
-
await run(plugin, input(1, { domainTick: 100 }), context(() => {}));
|
|
1376
|
-
const secondInput = input(2, { domainTick: 101 });
|
|
1377
|
-
const secondComputation = await plugin.update(secondInput, context(() => {}));
|
|
1378
|
-
const controlled = plugin.control({ greeting: 'new' });
|
|
1379
|
-
const secondCommitted = await plugin.commit(secondInput, secondComputation, context(() => {}));
|
|
1380
|
-
|
|
1381
|
-
expect(secondCommitted).toMatchObject({
|
|
1382
|
-
outcome: 'noop',
|
|
1383
|
-
reason: 'strategy_parameters_changed_before_reduce',
|
|
1384
|
-
});
|
|
1385
|
-
await expect(controlled).resolves.toMatchObject({
|
|
1386
|
-
accepted: true,
|
|
1387
|
-
effectiveValues: { greeting: 'new', style: 'friendly' },
|
|
1388
|
-
appliedEpochId: 2,
|
|
1389
|
-
});
|
|
1390
|
-
await run(plugin, input(3, { domainTick: 102 }), context(() => {}));
|
|
1391
|
-
const fourthInput = input(4, { domainTick: 103 });
|
|
1392
|
-
const fourthComputation = await plugin.update(fourthInput, context(() => {}));
|
|
1393
|
-
const unchanged = plugin.control({ greeting: 'new' });
|
|
1394
|
-
const fourthCommitted = await plugin.commit(
|
|
1395
|
-
fourthInput,
|
|
1396
|
-
fourthComputation,
|
|
1397
|
-
context(() => {}),
|
|
1398
|
-
);
|
|
1399
|
-
expect(fourthCommitted.outcome).toBe('processed');
|
|
1400
|
-
await expect(unchanged).resolves.toMatchObject({
|
|
1401
|
-
accepted: true,
|
|
1402
|
-
effectiveValues: { greeting: 'new', style: 'friendly' },
|
|
1403
|
-
appliedEpochId: 4,
|
|
1404
|
-
});
|
|
1405
|
-
|
|
1406
|
-
expect(analyzed).toEqual([
|
|
1407
|
-
{ epochId: 1, greeting: 'old', style: 'friendly' },
|
|
1408
|
-
{ epochId: 2, greeting: 'old', style: 'friendly' },
|
|
1409
|
-
{ epochId: 3, greeting: 'new', style: 'friendly' },
|
|
1410
|
-
{ epochId: 4, greeting: 'new', style: 'friendly' },
|
|
1411
|
-
]);
|
|
1412
|
-
expect(reduced).toEqual([
|
|
1413
|
-
{ epochId: 1, greeting: 'old', style: 'friendly' },
|
|
1414
|
-
{ epochId: 3, greeting: 'new', style: 'friendly' },
|
|
1415
|
-
{ epochId: 4, greeting: 'new', style: 'friendly' },
|
|
1416
|
-
]);
|
|
1417
|
-
expect(controls).toEqual([{ greeting: 'new' }, { greeting: 'new' }]);
|
|
1418
|
-
expect(parallelPool).toBe(2);
|
|
1419
|
-
await plugin.stop('shutdown');
|
|
1420
|
-
});
|
|
1421
|
-
|
|
1422
|
-
it('discards a pre-boundary hybrid analysis before its reducer can mutate or submit an Action', async () => {
|
|
1423
|
-
let resolveFirst: ((analysis: unknown) => void) | undefined;
|
|
1424
|
-
let parallelAlive = false;
|
|
1425
|
-
let reducerAlive = false;
|
|
1426
|
-
const reduced: number[] = [];
|
|
1427
|
-
const actions: Array<{ epoch: number; action: Record<string, unknown> }> = [];
|
|
1428
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1429
|
-
executorFactory: (_selection, host, purpose) => purpose === 'parallel'
|
|
1430
|
-
? {
|
|
1431
|
-
runtimeInstanceId: 'hybrid-barrier-analysis',
|
|
1432
|
-
get isAlive() { return parallelAlive; },
|
|
1433
|
-
async start() { parallelAlive = true; },
|
|
1434
|
-
async reset() {},
|
|
1435
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1436
|
-
analyze(frame) {
|
|
1437
|
-
if (frame.epochId !== 1) return Promise.resolve({ sourceEpoch: frame.epochId });
|
|
1438
|
-
return new Promise(resolve => { resolveFirst = resolve; });
|
|
1439
|
-
},
|
|
1440
|
-
async stop() { parallelAlive = false; },
|
|
1441
|
-
}
|
|
1442
|
-
: {
|
|
1443
|
-
runtimeInstanceId: 'hybrid-barrier-reducer',
|
|
1444
|
-
get isAlive() { return reducerAlive; },
|
|
1445
|
-
async start() { reducerAlive = true; },
|
|
1446
|
-
async reset() {},
|
|
1447
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1448
|
-
async reduce(frame) {
|
|
1449
|
-
reduced.push(frame.epochId);
|
|
1450
|
-
await host.action({ action: 'task' });
|
|
1451
|
-
return {};
|
|
1452
|
-
},
|
|
1453
|
-
async stop() { reducerAlive = false; },
|
|
1454
|
-
},
|
|
1455
|
-
});
|
|
1456
|
-
plugin.select({
|
|
1457
|
-
id: 'hybrid-barrier',
|
|
1458
|
-
execution: 'parallel_analyze',
|
|
1459
|
-
sourcePath: 'unused',
|
|
1460
|
-
sourceRoot: 'unused',
|
|
1461
|
-
});
|
|
1462
|
-
const contexts = actionContext(actions);
|
|
1463
|
-
const firstInput = input(1, { domainTick: 100 });
|
|
1464
|
-
const firstContext = contexts.forEpoch(1);
|
|
1465
|
-
const firstPending = plugin.update(firstInput, firstContext);
|
|
1466
|
-
await vi.waitFor(() => expect(resolveFirst).toBeTypeOf('function'));
|
|
1467
|
-
|
|
1468
|
-
const meetingInput = input(2, { domainTick: 101 });
|
|
1469
|
-
meetingInput.epoch.state.stage = 'meeting';
|
|
1470
|
-
meetingInput.epoch.state.game_snapshot.state.phase = 'meeting';
|
|
1471
|
-
await plugin.update(meetingInput, contexts.forEpoch(2));
|
|
1472
|
-
|
|
1473
|
-
// A newer wandering frame makes the latest state actionable again, but it
|
|
1474
|
-
// must not revive analysis computed before the meeting boundary.
|
|
1475
|
-
await plugin.update(input(3, { domainTick: 105 }), contexts.forEpoch(3));
|
|
1476
|
-
resolveFirst!({ sourceEpoch: 1 });
|
|
1477
|
-
const firstComputation = await firstPending;
|
|
1478
|
-
|
|
1479
|
-
await expect(plugin.commit(firstInput, firstComputation, firstContext)).resolves.toEqual({
|
|
1480
|
-
outcome: 'noop',
|
|
1481
|
-
reason: 'strategy_action_barrier_advanced',
|
|
1482
|
-
});
|
|
1483
|
-
expect(reduced).toEqual([]);
|
|
1484
|
-
expect(actions).toEqual([]);
|
|
1485
|
-
await plugin.stop('shutdown');
|
|
1486
|
-
});
|
|
1487
|
-
|
|
1488
|
-
it('rebases a hybrid reducer frame on the ordered commit Event cursor', async () => {
|
|
1489
|
-
const analyzed: Array<{ epochId: number; eventIds: string[] }> = [];
|
|
1490
|
-
const reduced: Array<{
|
|
1491
|
-
epochId: number;
|
|
1492
|
-
frameEventIds: string[];
|
|
1493
|
-
analyzedEventIds: string[];
|
|
1494
|
-
}> = [];
|
|
1495
|
-
let parallelAlive = false;
|
|
1496
|
-
let reducerAlive = false;
|
|
1497
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1498
|
-
executorFactory: (_selection, _host, purpose) => purpose === 'parallel'
|
|
1499
|
-
? {
|
|
1500
|
-
runtimeInstanceId: 'hybrid-event-analysis',
|
|
1501
|
-
get isAlive() { return parallelAlive; },
|
|
1502
|
-
async start() { parallelAlive = true; },
|
|
1503
|
-
async reset() {},
|
|
1504
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1505
|
-
async analyze(frame) {
|
|
1506
|
-
const eventIds = frame.events.map(item => item.eventId);
|
|
1507
|
-
analyzed.push({ epochId: frame.epochId, eventIds });
|
|
1508
|
-
return { eventIds };
|
|
1509
|
-
},
|
|
1510
|
-
async stop() { parallelAlive = false; },
|
|
1511
|
-
}
|
|
1512
|
-
: {
|
|
1513
|
-
runtimeInstanceId: 'hybrid-event-reducer',
|
|
1514
|
-
get isAlive() { return reducerAlive; },
|
|
1515
|
-
async start() { reducerAlive = true; },
|
|
1516
|
-
async reset() {},
|
|
1517
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1518
|
-
async reduce(frame, analysis) {
|
|
1519
|
-
reduced.push({
|
|
1520
|
-
epochId: frame.epochId,
|
|
1521
|
-
frameEventIds: frame.events.map(item => item.eventId),
|
|
1522
|
-
analyzedEventIds: (analysis as { eventIds: string[] }).eventIds,
|
|
1523
|
-
});
|
|
1524
|
-
return {};
|
|
1525
|
-
},
|
|
1526
|
-
async stop() { reducerAlive = false; },
|
|
1527
|
-
},
|
|
1528
|
-
});
|
|
1529
|
-
plugin.select({
|
|
1530
|
-
id: 'hybrid-event-rebase',
|
|
1531
|
-
execution: 'parallel_analyze',
|
|
1532
|
-
sourcePath: 'unused',
|
|
1533
|
-
sourceRoot: 'unused',
|
|
1534
|
-
});
|
|
1535
|
-
const pluginContext = context(() => {});
|
|
1536
|
-
const firstEvent = event(1);
|
|
1537
|
-
const secondEvent = event(2);
|
|
1538
|
-
const firstUpdateInput = input(1, {
|
|
1539
|
-
domainTick: 100,
|
|
1540
|
-
pendingEvents: [firstEvent],
|
|
1541
|
-
});
|
|
1542
|
-
const secondUpdateInput = input(2, {
|
|
1543
|
-
domainTick: 101,
|
|
1544
|
-
pendingEvents: [firstEvent, secondEvent],
|
|
1545
|
-
});
|
|
1546
|
-
const [firstComputation, secondComputation] = await Promise.all([
|
|
1547
|
-
plugin.update(firstUpdateInput, pluginContext),
|
|
1548
|
-
plugin.update(secondUpdateInput, pluginContext),
|
|
1549
|
-
]);
|
|
1550
|
-
|
|
1551
|
-
const firstCommitted = await plugin.commit(firstUpdateInput, firstComputation, pluginContext);
|
|
1552
|
-
// The Runtime scheduler reconstructs commit input after epoch 1 advances
|
|
1553
|
-
// the cursor, so epoch 2 now contains only the still-unconsumed Event.
|
|
1554
|
-
const secondCommitInput = input(2, {
|
|
1555
|
-
domainTick: 101,
|
|
1556
|
-
pendingEvents: [secondEvent],
|
|
1557
|
-
});
|
|
1558
|
-
const secondCommitted = await plugin.commit(
|
|
1559
|
-
secondCommitInput,
|
|
1560
|
-
secondComputation,
|
|
1561
|
-
pluginContext,
|
|
1562
|
-
);
|
|
1563
|
-
|
|
1564
|
-
expect(analyzed).toEqual([
|
|
1565
|
-
{ epochId: 1, eventIds: ['event-1'] },
|
|
1566
|
-
{ epochId: 2, eventIds: ['event-1', 'event-2'] },
|
|
1567
|
-
]);
|
|
1568
|
-
expect(reduced).toEqual([
|
|
1569
|
-
{ epochId: 1, frameEventIds: ['event-1'], analyzedEventIds: ['event-1'] },
|
|
1570
|
-
{ epochId: 2, frameEventIds: ['event-2'], analyzedEventIds: ['event-1', 'event-2'] },
|
|
1571
|
-
]);
|
|
1572
|
-
expect(firstCommitted.consumeEventsThroughCommitSeq).toBe(1);
|
|
1573
|
-
expect(secondCommitted.consumeEventsThroughCommitSeq).toBe(2);
|
|
1574
|
-
await plugin.stop('shutdown');
|
|
1575
|
-
});
|
|
1576
|
-
|
|
1577
|
-
it('does not let a dropped hybrid analysis mutate the persistent reducer', async () => {
|
|
1578
|
-
const reduced: number[] = [];
|
|
1579
|
-
const resets: number[] = [];
|
|
1580
|
-
let parallelAlive = false;
|
|
1581
|
-
let reducerAlive = false;
|
|
1582
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1583
|
-
executorFactory: (_selection, _host, purpose) => purpose === 'parallel'
|
|
1584
|
-
? {
|
|
1585
|
-
runtimeInstanceId: 'hybrid-drop-analysis',
|
|
1586
|
-
get isAlive() { return parallelAlive; },
|
|
1587
|
-
async start() { parallelAlive = true; },
|
|
1588
|
-
async reset() {},
|
|
1589
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1590
|
-
async analyze(frame) { return { sourceEpoch: frame.epochId }; },
|
|
1591
|
-
async stop() { parallelAlive = false; },
|
|
1592
|
-
}
|
|
1593
|
-
: {
|
|
1594
|
-
runtimeInstanceId: 'hybrid-drop-reducer',
|
|
1595
|
-
get isAlive() { return reducerAlive; },
|
|
1596
|
-
async start() { reducerAlive = true; },
|
|
1597
|
-
async reset() { resets.push(reduced.at(-1) ?? 0); },
|
|
1598
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1599
|
-
async reduce(frame) { reduced.push(frame.epochId); return {}; },
|
|
1600
|
-
async stop() { reducerAlive = false; },
|
|
1601
|
-
},
|
|
1602
|
-
});
|
|
1603
|
-
plugin.select({
|
|
1604
|
-
id: 'hybrid-drop',
|
|
1605
|
-
execution: 'parallel_analyze',
|
|
1606
|
-
sourcePath: 'unused',
|
|
1607
|
-
sourceRoot: 'unused',
|
|
1608
|
-
});
|
|
1609
|
-
const pluginContext = context(() => {});
|
|
1610
|
-
const firstInput = input(1, { domainTick: 100 });
|
|
1611
|
-
await plugin.commit(firstInput, await plugin.update(firstInput, pluginContext), pluginContext);
|
|
1612
|
-
|
|
1613
|
-
// Epoch 2 finished analysis but is intentionally dropped by the host's
|
|
1614
|
-
// catch-up policy. Only the later committed snapshot may reach reduce().
|
|
1615
|
-
await plugin.update(input(2, { domainTick: 105 }), pluginContext);
|
|
1616
|
-
const latestInput = input(10, { domainTick: 150 });
|
|
1617
|
-
await plugin.commit(latestInput, await plugin.update(latestInput, pluginContext), pluginContext);
|
|
1618
|
-
|
|
1619
|
-
expect(reduced).toEqual([1, 10]);
|
|
1620
|
-
expect(resets).toEqual([1]);
|
|
1621
|
-
await plugin.stop('shutdown');
|
|
1622
|
-
});
|
|
1623
|
-
|
|
1624
|
-
it('replaces a hybrid reducer after either an internal failure or invalid output', async () => {
|
|
1625
|
-
let parallelAlive = false;
|
|
1626
|
-
let serialFactoryCalls = 0;
|
|
1627
|
-
const stopped: string[] = [];
|
|
1628
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1629
|
-
executorFactory: (_selection, _host, purpose) => {
|
|
1630
|
-
if (purpose === 'parallel') {
|
|
1631
|
-
return {
|
|
1632
|
-
runtimeInstanceId: 'hybrid-failure-analysis',
|
|
1633
|
-
get isAlive() { return parallelAlive; },
|
|
1634
|
-
async start() { parallelAlive = true; },
|
|
1635
|
-
async reset() {},
|
|
1636
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1637
|
-
async analyze(frame) { return { sourceEpoch: frame.epochId }; },
|
|
1638
|
-
async stop() { parallelAlive = false; },
|
|
1639
|
-
};
|
|
1640
|
-
}
|
|
1641
|
-
serialFactoryCalls += 1;
|
|
1642
|
-
const instance = serialFactoryCalls;
|
|
1643
|
-
let alive = false;
|
|
1644
|
-
let dirtyState = 0;
|
|
1645
|
-
return {
|
|
1646
|
-
runtimeInstanceId: `hybrid-failure-reducer-${instance}`,
|
|
1647
|
-
get isAlive() { return alive; },
|
|
1648
|
-
async start() { alive = true; },
|
|
1649
|
-
async reset() {},
|
|
1650
|
-
async update() { throw new Error('hybrid_update_must_not_run'); },
|
|
1651
|
-
async reduce(frame) {
|
|
1652
|
-
dirtyState += 1;
|
|
1653
|
-
if (instance === 1) throw new Error('mutated_then_failed');
|
|
1654
|
-
if (instance === 2) {
|
|
1655
|
-
return {
|
|
1656
|
-
states: [{
|
|
1657
|
-
topic: 'undeclared.state',
|
|
1658
|
-
value: { dirtyState },
|
|
1659
|
-
}],
|
|
1660
|
-
};
|
|
1661
|
-
}
|
|
1662
|
-
return {
|
|
1663
|
-
states: [{
|
|
1664
|
-
topic: 'strategy.status',
|
|
1665
|
-
value: { activity: 'idle', summary: `epoch ${frame.epochId}; state ${dirtyState}` },
|
|
1666
|
-
}],
|
|
1667
|
-
};
|
|
1668
|
-
},
|
|
1669
|
-
async stop(reason) { alive = false; stopped.push(`${instance}:${reason}`); },
|
|
1670
|
-
};
|
|
1671
|
-
},
|
|
1672
|
-
});
|
|
1673
|
-
plugin.select({
|
|
1674
|
-
id: 'hybrid-failure',
|
|
1675
|
-
execution: 'parallel_analyze',
|
|
1676
|
-
sourcePath: 'unused',
|
|
1677
|
-
sourceRoot: 'unused',
|
|
1678
|
-
});
|
|
1679
|
-
const pluginContext = context(() => {});
|
|
1680
|
-
const firstInput = input(1, { domainTick: 100 });
|
|
1681
|
-
await expect(plugin.commit(
|
|
1682
|
-
firstInput,
|
|
1683
|
-
await plugin.update(firstInput, pluginContext),
|
|
1684
|
-
pluginContext,
|
|
1685
|
-
)).rejects.toThrow('mutated_then_failed');
|
|
1686
|
-
|
|
1687
|
-
const secondInput = input(2, { domainTick: 105 });
|
|
1688
|
-
await expect(plugin.commit(
|
|
1689
|
-
secondInput,
|
|
1690
|
-
await plugin.update(secondInput, pluginContext),
|
|
1691
|
-
pluginContext,
|
|
1692
|
-
)).rejects.toThrow('strategy_state_output_invalid');
|
|
1693
|
-
|
|
1694
|
-
const thirdInput = input(3, { domainTick: 110 });
|
|
1695
|
-
const third = await plugin.commit(
|
|
1696
|
-
thirdInput,
|
|
1697
|
-
await plugin.update(thirdInput, pluginContext),
|
|
1698
|
-
pluginContext,
|
|
1699
|
-
);
|
|
1700
|
-
expect(serialFactoryCalls).toBe(3);
|
|
1701
|
-
expect(stopped).toEqual(expect.arrayContaining(['1:switched', '2:switched']));
|
|
1702
|
-
expect(third.states?.[0]?.value).toMatchObject({ summary: 'epoch 3; state 1' });
|
|
1703
|
-
await plugin.stop('shutdown');
|
|
1704
|
-
});
|
|
1705
|
-
|
|
1706
|
-
it('routes hybrid analysis diagnostics with the correct execution mode to each epoch', async () => {
|
|
1707
|
-
let alive = false;
|
|
1708
|
-
const seen: Array<{
|
|
1709
|
-
context: string;
|
|
1710
|
-
name: string;
|
|
1711
|
-
execution: unknown;
|
|
1712
|
-
}> = [];
|
|
1713
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
1714
|
-
executorFactory: (_selection, host) => ({
|
|
1715
|
-
runtimeInstanceId: 'parallel-diagnostic-instance',
|
|
1716
|
-
get isAlive() { return alive; },
|
|
1717
|
-
async start() { alive = true; },
|
|
1718
|
-
async reset() {},
|
|
1719
|
-
async update() { return {}; },
|
|
1720
|
-
async analyze(frame) {
|
|
1721
|
-
host.diagnostic('strategy.analyze.completed', { epoch_id: frame.epochId });
|
|
1722
|
-
return { epochId: frame.epochId };
|
|
1723
|
-
},
|
|
1724
|
-
async reduce() { return {}; },
|
|
1725
|
-
async stop() { alive = false; },
|
|
1726
|
-
}),
|
|
1727
|
-
});
|
|
1728
|
-
plugin.select({
|
|
1729
|
-
id: 'parallel-diagnostics',
|
|
1730
|
-
execution: 'parallel_analyze',
|
|
1731
|
-
sourcePath: 'unused',
|
|
1732
|
-
sourceRoot: 'unused',
|
|
1733
|
-
});
|
|
1734
|
-
const firstContext = context((name, attributes) => seen.push({
|
|
1735
|
-
context: 'epoch-1',
|
|
1736
|
-
name,
|
|
1737
|
-
execution: attributes?.execution,
|
|
1738
|
-
}));
|
|
1739
|
-
const secondContext = context((name, attributes) => seen.push({
|
|
1740
|
-
context: 'epoch-2',
|
|
1741
|
-
name,
|
|
1742
|
-
execution: attributes?.execution,
|
|
1743
|
-
}));
|
|
1744
|
-
|
|
1745
|
-
await plugin.update(input(1, { domainTick: 100 }), firstContext);
|
|
1746
|
-
await plugin.update(input(2, { domainTick: 105 }), secondContext);
|
|
1747
|
-
|
|
1748
|
-
expect(seen).toEqual([
|
|
1749
|
-
{
|
|
1750
|
-
context: 'epoch-1',
|
|
1751
|
-
name: 'strategy.analyze.completed',
|
|
1752
|
-
execution: 'parallel_analyze',
|
|
1753
|
-
},
|
|
1754
|
-
{
|
|
1755
|
-
context: 'epoch-2',
|
|
1756
|
-
name: 'strategy.analyze.completed',
|
|
1757
|
-
execution: 'parallel_analyze',
|
|
1758
|
-
},
|
|
1759
|
-
]);
|
|
1760
|
-
await plugin.stop('shutdown');
|
|
1761
|
-
});
|
|
1762
|
-
|
|
1763
|
-
it('drops State, Event, and cursor output when the first Action is rejected before submission', async () => {
|
|
1764
|
-
let alive = false;
|
|
1765
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1766
|
-
runtimeInstanceId: 'stale-provider-instance',
|
|
1767
|
-
get isAlive() { return alive; },
|
|
1768
|
-
async start() { alive = true; },
|
|
1769
|
-
async reset() {},
|
|
1770
|
-
async update() {
|
|
1771
|
-
return {
|
|
1772
|
-
actions: [{
|
|
1773
|
-
action: { action: 'move', target_x: 10, target_y: 20 },
|
|
1774
|
-
dedupeKey: 'stale-move',
|
|
1775
|
-
}],
|
|
1776
|
-
states: [{
|
|
1777
|
-
topic: 'strategy.status',
|
|
1778
|
-
value: { activity: 'moving', summary: 'stale movement' },
|
|
1779
|
-
}],
|
|
1780
|
-
events: [{
|
|
1781
|
-
type: 'strategy_alert',
|
|
1782
|
-
dedupeKey: 'stale-alert',
|
|
1783
|
-
payload: { message: 'stale movement' },
|
|
1784
|
-
}],
|
|
1785
|
-
};
|
|
1786
|
-
},
|
|
1787
|
-
async stop() { alive = false; },
|
|
1788
|
-
};
|
|
1789
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1790
|
-
plugin.select({
|
|
1791
|
-
id: 'stale-provider',
|
|
1792
|
-
execution: 'parallel_compute',
|
|
1793
|
-
sourcePath: 'unused',
|
|
1794
|
-
sourceRoot: 'unused',
|
|
1795
|
-
});
|
|
1796
|
-
const pluginInput = input(1, { pendingEvents: [event(1)] });
|
|
1797
|
-
const base = context(() => {});
|
|
1798
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
1799
|
-
...base,
|
|
1800
|
-
capabilities: {
|
|
1801
|
-
...base.capabilities,
|
|
1802
|
-
performAction: async () => ({
|
|
1803
|
-
scope,
|
|
1804
|
-
actionId: 'stale-action',
|
|
1805
|
-
status: 'failure',
|
|
1806
|
-
source: 'local',
|
|
1807
|
-
actionOutcome: 'not_submitted',
|
|
1808
|
-
error: {
|
|
1809
|
-
error: {
|
|
1810
|
-
code: 'STALE_STRATEGY_ACTION',
|
|
1811
|
-
message: 'The Action crossed a safety boundary.',
|
|
1812
|
-
},
|
|
1813
|
-
},
|
|
1814
|
-
}),
|
|
1815
|
-
},
|
|
1816
|
-
};
|
|
1817
|
-
|
|
1818
|
-
const computation = await plugin.update(pluginInput, pluginContext);
|
|
1819
|
-
await expect(plugin.commit(pluginInput, computation, pluginContext)).resolves.toEqual({
|
|
1820
|
-
outcome: 'noop',
|
|
1821
|
-
reason: 'strategy_action_safety_superseded',
|
|
1822
|
-
});
|
|
1823
|
-
});
|
|
1824
|
-
|
|
1825
|
-
it('keeps an ordered commit atomic when switch and meeting arrive during its first Action', async () => {
|
|
1826
|
-
let alive = false;
|
|
1827
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1828
|
-
runtimeInstanceId: 'mid-action-boundary-instance',
|
|
1829
|
-
get isAlive() { return alive; },
|
|
1830
|
-
async start() { alive = true; },
|
|
1831
|
-
async reset() {},
|
|
1832
|
-
async update() {
|
|
1833
|
-
return {
|
|
1834
|
-
actions: [
|
|
1835
|
-
{ action: { action: 'move', target_x: 1, target_y: 1 }, dedupeKey: 'first' },
|
|
1836
|
-
{ action: { action: 'speech', text: 'later' }, dedupeKey: 'second' },
|
|
1837
|
-
{ action: { action: 'move', target_x: 3, target_y: 3 }, dedupeKey: 'third' },
|
|
1838
|
-
],
|
|
1839
|
-
states: [{
|
|
1840
|
-
topic: 'strategy.status',
|
|
1841
|
-
value: { activity: 'moving', summary: 'first action committed' },
|
|
1842
|
-
}],
|
|
1843
|
-
events: [{
|
|
1844
|
-
type: 'strategy_alert',
|
|
1845
|
-
dedupeKey: 'mid-action-alert',
|
|
1846
|
-
payload: { message: 'first action committed' },
|
|
1847
|
-
}],
|
|
1848
|
-
};
|
|
1849
|
-
},
|
|
1850
|
-
async stop() { alive = false; },
|
|
1851
|
-
};
|
|
1852
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1853
|
-
plugin.select({
|
|
1854
|
-
id: 'mid-action-boundary',
|
|
1855
|
-
execution: 'parallel_compute',
|
|
1856
|
-
sourcePath: 'unused',
|
|
1857
|
-
sourceRoot: 'unused',
|
|
1858
|
-
});
|
|
1859
|
-
const pluginInput = input(1, { pendingEvents: [event(1)] });
|
|
1860
|
-
const attempts: Record<string, unknown>[] = [];
|
|
1861
|
-
const submitted: Record<string, unknown>[] = [];
|
|
1862
|
-
const diagnostics: Array<{ name: string; attributes?: Readonly<Record<string, unknown>> }> = [];
|
|
1863
|
-
let markFirstStarted!: () => void;
|
|
1864
|
-
const firstStarted = new Promise<void>((resolve) => { markFirstStarted = resolve; });
|
|
1865
|
-
let resolveFirst!: (value: any) => void;
|
|
1866
|
-
const base = context((name, attributes) => diagnostics.push({ name, attributes }));
|
|
1867
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
1868
|
-
...base,
|
|
1869
|
-
capabilities: {
|
|
1870
|
-
...base.capabilities,
|
|
1871
|
-
performAction: async (_capability, action) => {
|
|
1872
|
-
const request = action as Record<string, unknown>;
|
|
1873
|
-
attempts.push(request);
|
|
1874
|
-
if (attempts.length === 1) {
|
|
1875
|
-
submitted.push(request);
|
|
1876
|
-
markFirstStarted();
|
|
1877
|
-
return new Promise((resolve) => { resolveFirst = resolve; });
|
|
1878
|
-
}
|
|
1879
|
-
return {
|
|
1880
|
-
scope,
|
|
1881
|
-
actionId: 'stale-later-action',
|
|
1882
|
-
status: 'failure' as const,
|
|
1883
|
-
source: 'local' as const,
|
|
1884
|
-
actionOutcome: 'not_submitted' as const,
|
|
1885
|
-
error: {
|
|
1886
|
-
error: {
|
|
1887
|
-
code: 'STALE_STRATEGY_ACTION',
|
|
1888
|
-
message: 'The Action crossed a safety boundary.',
|
|
1889
|
-
},
|
|
1890
|
-
},
|
|
1891
|
-
};
|
|
1892
|
-
},
|
|
1893
|
-
},
|
|
1894
|
-
};
|
|
1895
|
-
|
|
1896
|
-
const computation = await plugin.update(pluginInput, pluginContext);
|
|
1897
|
-
const committing = plugin.commit(pluginInput, computation, pluginContext);
|
|
1898
|
-
await firstStarted;
|
|
1899
|
-
plugin.select({
|
|
1900
|
-
id: 'replacement',
|
|
1901
|
-
execution: 'parallel_compute',
|
|
1902
|
-
sourcePath: 'unused',
|
|
1903
|
-
sourceRoot: 'unused',
|
|
1904
|
-
});
|
|
1905
|
-
const meetingInput = input(2);
|
|
1906
|
-
meetingInput.epoch.state.action_safety_generation = 2;
|
|
1907
|
-
meetingInput.epoch.state.stage = 'meeting';
|
|
1908
|
-
meetingInput.epoch.state.game_snapshot.state.phase = 'meeting';
|
|
1909
|
-
await plugin.update(meetingInput, pluginContext);
|
|
1910
|
-
resolveFirst({
|
|
1911
|
-
scope,
|
|
1912
|
-
actionId: 'first-action',
|
|
1913
|
-
status: 'success',
|
|
1914
|
-
source: 'backend',
|
|
1915
|
-
actionOutcome: 'confirmed',
|
|
1916
|
-
});
|
|
1917
|
-
|
|
1918
|
-
await expect(committing).resolves.toMatchObject({
|
|
1919
|
-
outcome: 'processed',
|
|
1920
|
-
states: [{
|
|
1921
|
-
topic: 'strategy.status',
|
|
1922
|
-
value: { strategy: 'mid-action-boundary', summary: 'first action committed' },
|
|
1923
|
-
}],
|
|
1924
|
-
events: [{ type: 'strategy_alert', dedupeKey: 'mid-action-alert' }],
|
|
1925
|
-
consumeEventsThroughCommitSeq: 1,
|
|
1926
|
-
});
|
|
1927
|
-
expect(attempts).toHaveLength(2);
|
|
1928
|
-
expect(submitted).toHaveLength(1);
|
|
1929
|
-
expect(diagnostics).toContainEqual({
|
|
1930
|
-
name: 'strategy.action.batch_truncated',
|
|
1931
|
-
attributes: expect.objectContaining({
|
|
1932
|
-
reason: 'strategy_action_safety_superseded',
|
|
1933
|
-
rejected_action_index: 1,
|
|
1934
|
-
skipped_action_count: 1,
|
|
1935
|
-
}),
|
|
1936
|
-
});
|
|
1937
|
-
await plugin.stop('shutdown');
|
|
1938
|
-
});
|
|
1939
|
-
|
|
1940
|
-
it('validates every output before submitting any proposed Action', async () => {
|
|
1941
|
-
let alive = false;
|
|
1942
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1943
|
-
runtimeInstanceId: 'invalid-output-instance',
|
|
1944
|
-
get isAlive() { return alive; },
|
|
1945
|
-
async start() { alive = true; },
|
|
1946
|
-
async reset() {},
|
|
1947
|
-
async update() {
|
|
1948
|
-
return {
|
|
1949
|
-
actions: [{ action: { action: 'move', target_x: 1, target_y: 2 } }],
|
|
1950
|
-
states: [{ topic: 'undeclared.state', value: { invalid: true } }],
|
|
1951
|
-
};
|
|
1952
|
-
},
|
|
1953
|
-
async stop() { alive = false; },
|
|
1954
|
-
};
|
|
1955
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1956
|
-
plugin.select({
|
|
1957
|
-
id: 'invalid-output',
|
|
1958
|
-
execution: 'parallel_compute',
|
|
1959
|
-
sourcePath: 'unused',
|
|
1960
|
-
sourceRoot: 'unused',
|
|
1961
|
-
});
|
|
1962
|
-
const actions: Array<{ epoch: number; action: Record<string, unknown> }> = [];
|
|
1963
|
-
const pluginContext = actionContext(actions).forEpoch(1);
|
|
1964
|
-
const pluginInput = input(1);
|
|
1965
|
-
|
|
1966
|
-
const computation = await plugin.update(pluginInput, pluginContext);
|
|
1967
|
-
await expect(plugin.commit(pluginInput, computation, pluginContext)).rejects.toThrow(
|
|
1968
|
-
'strategy_state_output_invalid',
|
|
1969
|
-
);
|
|
1970
|
-
expect(actions).toEqual([]);
|
|
1971
|
-
});
|
|
1972
|
-
|
|
1973
|
-
it('drops a stale Action after a newer meeting or death State but not after an events-query boundary', async () => {
|
|
1974
|
-
const completions: Array<(result: StrategyResult) => void> = [];
|
|
1975
|
-
let alive = false;
|
|
1976
|
-
const executor: ClawclawStrategyExecutor = {
|
|
1977
|
-
runtimeInstanceId: 'parallel-barrier-instance',
|
|
1978
|
-
get isAlive() { return alive; },
|
|
1979
|
-
async start() { alive = true; },
|
|
1980
|
-
async reset() {},
|
|
1981
|
-
update() { return new Promise((resolve) => completions.push(resolve)); },
|
|
1982
|
-
async stop() { alive = false; },
|
|
1983
|
-
};
|
|
1984
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
1985
|
-
plugin.select({
|
|
1986
|
-
id: 'parallel-barrier',
|
|
1987
|
-
execution: 'parallel_compute',
|
|
1988
|
-
sourcePath: 'unused',
|
|
1989
|
-
sourceRoot: 'unused',
|
|
1990
|
-
});
|
|
1991
|
-
const actions: Array<{ epoch: number; action: Record<string, unknown> }> = [];
|
|
1992
|
-
const contexts = actionContext(actions);
|
|
1993
|
-
|
|
1994
|
-
const firstInput = input(1, { domainTick: 200 });
|
|
1995
|
-
const firstContext = contexts.forEpoch(1);
|
|
1996
|
-
const firstPending = plugin.update(firstInput, firstContext);
|
|
1997
|
-
await vi.waitFor(() => expect(completions).toHaveLength(1));
|
|
1998
|
-
const queryInput = input(2, { domainTick: 201 });
|
|
1999
|
-
queryInput.epoch.state.cause = 'agent_events_query';
|
|
2000
|
-
queryInput.epoch.state.game_snapshot = { status: 'not_updated' };
|
|
2001
|
-
await plugin.update(queryInput, contexts.forEpoch(2));
|
|
2002
|
-
completions.shift()!({
|
|
2003
|
-
actions: [{ action: { action: 'move', target_x: 1, target_y: 1 }, dedupeKey: 'before-query' }],
|
|
2004
|
-
});
|
|
2005
|
-
const firstComputation = await firstPending;
|
|
2006
|
-
await plugin.commit(firstInput, firstComputation, firstContext);
|
|
2007
|
-
expect(actions).toHaveLength(1);
|
|
2008
|
-
|
|
2009
|
-
const secondFresh = input(3, { domainTick: 205 });
|
|
2010
|
-
const secondFreshContext = contexts.forEpoch(3);
|
|
2011
|
-
const secondPending = plugin.update(secondFresh, secondFreshContext);
|
|
2012
|
-
await vi.waitFor(() => expect(completions).toHaveLength(1));
|
|
2013
|
-
const meetingInput = input(4, { domainTick: 206 });
|
|
2014
|
-
meetingInput.epoch.state.stage = 'meeting';
|
|
2015
|
-
meetingInput.epoch.state.game_snapshot.state.phase = 'meeting';
|
|
2016
|
-
await plugin.update(meetingInput, contexts.forEpoch(4));
|
|
2017
|
-
completions.shift()!({
|
|
2018
|
-
actions: [{ action: { action: 'move', target_x: 2, target_y: 2 }, dedupeKey: 'before-meeting' }],
|
|
2019
|
-
states: [{ topic: 'strategy.status', value: { activity: 'moving', summary: 'stale meeting move' } }],
|
|
2020
|
-
events: [{ type: 'strategy_alert', payload: { message: 'stale meeting move' } }],
|
|
2021
|
-
});
|
|
2022
|
-
await expect(plugin.commit(secondFresh, await secondPending, secondFreshContext)).resolves.toEqual({
|
|
2023
|
-
outcome: 'noop',
|
|
2024
|
-
reason: 'strategy_action_barrier_advanced',
|
|
2025
|
-
});
|
|
2026
|
-
expect(actions).toHaveLength(1);
|
|
2027
|
-
|
|
2028
|
-
const afterMeeting = input(5, { domainTick: 210 });
|
|
2029
|
-
const afterMeetingContext = contexts.forEpoch(5);
|
|
2030
|
-
const deathPending = plugin.update(afterMeeting, afterMeetingContext);
|
|
2031
|
-
await vi.waitFor(() => expect(completions).toHaveLength(1));
|
|
2032
|
-
const deathInput = input(6, { domainTick: 211 });
|
|
2033
|
-
deathInput.epoch.state.game_snapshot.state.you.is_alive = false;
|
|
2034
|
-
const deathBoundaryPending = plugin.update(deathInput, contexts.forEpoch(6));
|
|
2035
|
-
await vi.waitFor(() => expect(completions).toHaveLength(2));
|
|
2036
|
-
completions.shift()!({
|
|
2037
|
-
actions: [{ action: { action: 'move', target_x: 3, target_y: 3 }, dedupeKey: 'before-death' }],
|
|
2038
|
-
});
|
|
2039
|
-
await plugin.commit(afterMeeting, await deathPending, afterMeetingContext);
|
|
2040
|
-
expect(actions).toHaveLength(1);
|
|
2041
|
-
completions.shift()!({});
|
|
2042
|
-
await deathBoundaryPending;
|
|
2043
|
-
});
|
|
2044
|
-
|
|
2045
|
-
it('deduplicates repeated parallel Action proposals at ordered commit', async () => {
|
|
2046
|
-
let alive = false;
|
|
2047
|
-
const executor: ClawclawStrategyExecutor = {
|
|
2048
|
-
runtimeInstanceId: 'parallel-dedupe-instance',
|
|
2049
|
-
get isAlive() { return alive; },
|
|
2050
|
-
async start() { alive = true; },
|
|
2051
|
-
async reset() {},
|
|
2052
|
-
async update() {
|
|
2053
|
-
return {
|
|
2054
|
-
actions: [{ action: { action: 'speech', text: 'hello' }, dedupeKey: 'same-greeting' }],
|
|
2055
|
-
};
|
|
2056
|
-
},
|
|
2057
|
-
async stop() { alive = false; },
|
|
2058
|
-
};
|
|
2059
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
2060
|
-
plugin.select({
|
|
2061
|
-
id: 'parallel-dedupe',
|
|
2062
|
-
execution: 'parallel_compute',
|
|
2063
|
-
sourcePath: 'unused',
|
|
2064
|
-
sourceRoot: 'unused',
|
|
2065
|
-
});
|
|
2066
|
-
const actions: Array<{ epoch: number; action: Record<string, unknown> }> = [];
|
|
2067
|
-
const contexts = actionContext(actions);
|
|
2068
|
-
const firstInput = input(1, { domainTick: 300 });
|
|
2069
|
-
const secondInput = input(2, { domainTick: 305 });
|
|
2070
|
-
const firstContext = contexts.forEpoch(1);
|
|
2071
|
-
const secondContext = contexts.forEpoch(2);
|
|
2072
|
-
const [first, second] = await Promise.all([
|
|
2073
|
-
plugin.update(firstInput, firstContext),
|
|
2074
|
-
plugin.update(secondInput, secondContext),
|
|
2075
|
-
]);
|
|
2076
|
-
await plugin.commit(firstInput, first, firstContext);
|
|
2077
|
-
await plugin.commit(secondInput, second, secondContext);
|
|
2078
|
-
expect(actions.map((entry) => entry.action)).toEqual([{
|
|
2079
|
-
route: 'auto',
|
|
2080
|
-
action: { action: 'speech', text: 'hello' },
|
|
2081
|
-
strategyOriginEpochSeq: 1,
|
|
2082
|
-
strategyActionSafetyGeneration: 1,
|
|
2083
|
-
}]);
|
|
2084
|
-
});
|
|
2085
|
-
|
|
2086
|
-
it('allows a stable Action dedupe key to retry after a deferred submission', async () => {
|
|
2087
|
-
let alive = false;
|
|
2088
|
-
const executor: ClawclawStrategyExecutor = {
|
|
2089
|
-
runtimeInstanceId: 'parallel-action-retry',
|
|
2090
|
-
get isAlive() { return alive; },
|
|
2091
|
-
async start() { alive = true; },
|
|
2092
|
-
async reset() {},
|
|
2093
|
-
async update() {
|
|
2094
|
-
return {
|
|
2095
|
-
actions: [{ action: { action: 'move', target_x: 5, target_y: 6 }, dedupeKey: 'stable-move' }],
|
|
2096
|
-
};
|
|
2097
|
-
},
|
|
2098
|
-
async stop() { alive = false; },
|
|
2099
|
-
};
|
|
2100
|
-
const plugin = new ClawclawStrategyPlugin({ executorFactory: () => executor });
|
|
2101
|
-
plugin.select({
|
|
2102
|
-
id: 'parallel-action-retry',
|
|
2103
|
-
execution: 'parallel_compute',
|
|
2104
|
-
sourcePath: 'unused',
|
|
2105
|
-
sourceRoot: 'unused',
|
|
2106
|
-
});
|
|
2107
|
-
const baseContext = context(() => {});
|
|
2108
|
-
let attempts = 0;
|
|
2109
|
-
const pluginContext: RuntimeEpochPluginContext<Record<string, any>> = {
|
|
2110
|
-
...baseContext,
|
|
2111
|
-
capabilities: {
|
|
2112
|
-
...baseContext.capabilities,
|
|
2113
|
-
performAction: async () => {
|
|
2114
|
-
attempts += 1;
|
|
2115
|
-
return {
|
|
2116
|
-
scope,
|
|
2117
|
-
actionId: `attempt-${attempts}`,
|
|
2118
|
-
status: attempts === 1 ? 'deferred' as const : 'success' as const,
|
|
2119
|
-
source: 'local' as const,
|
|
2120
|
-
actionOutcome: attempts === 1 ? 'not_submitted' as const : 'confirmed' as const,
|
|
2121
|
-
};
|
|
2122
|
-
},
|
|
2123
|
-
},
|
|
2124
|
-
};
|
|
2125
|
-
|
|
2126
|
-
await run(plugin, input(1, { domainTick: 300 }), pluginContext);
|
|
2127
|
-
await run(plugin, input(2, { domainTick: 305 }), pluginContext);
|
|
2128
|
-
await run(plugin, input(3, { domainTick: 310 }), pluginContext);
|
|
2129
|
-
expect(attempts).toBe(2);
|
|
2130
|
-
});
|
|
2131
|
-
|
|
2132
|
-
it('freezes validated parallel controls before the next computation starts', async () => {
|
|
2133
|
-
const frames: StrategyFrame<Record<string, unknown>>[] = [];
|
|
2134
|
-
const executors: ClawclawStrategyExecutor[] = [];
|
|
2135
|
-
const plugin = new ClawclawStrategyPlugin({
|
|
2136
|
-
executorFactory: () => {
|
|
2137
|
-
let alive = false;
|
|
2138
|
-
const executor: ClawclawStrategyExecutor = {
|
|
2139
|
-
runtimeInstanceId: `parallel-control-${executors.length}`,
|
|
2140
|
-
get isAlive() { return alive; },
|
|
2141
|
-
async start() { alive = true; },
|
|
2142
|
-
async reset() {},
|
|
2143
|
-
async update(frame) { frames.push(frame); return {}; },
|
|
2144
|
-
async stop() { alive = false; },
|
|
2145
|
-
};
|
|
2146
|
-
executors.push(executor);
|
|
2147
|
-
return executor;
|
|
2148
|
-
},
|
|
2149
|
-
});
|
|
2150
|
-
plugin.select({
|
|
2151
|
-
id: 'parallel-control',
|
|
2152
|
-
execution: 'parallel_compute',
|
|
2153
|
-
controls: [{ id: 'greeting', type: 'string', description: 'Greeting.' }],
|
|
2154
|
-
parameters: { greeting: 'old' },
|
|
2155
|
-
sourcePath: 'unused',
|
|
2156
|
-
sourceRoot: 'unused',
|
|
2157
|
-
});
|
|
2158
|
-
const pluginContext = context(() => {});
|
|
2159
|
-
const firstInput = input(1, { domainTick: 400 });
|
|
2160
|
-
await run(plugin, firstInput, pluginContext);
|
|
2161
|
-
|
|
2162
|
-
const controlled = plugin.control({ greeting: 'new' });
|
|
2163
|
-
const secondInput = input(2, { domainTick: 405 });
|
|
2164
|
-
await run(plugin, secondInput, pluginContext);
|
|
2165
|
-
await expect(controlled).resolves.toMatchObject({
|
|
2166
|
-
accepted: true,
|
|
2167
|
-
effectiveValues: { greeting: 'new' },
|
|
2168
|
-
appliedEpochId: 2,
|
|
2169
|
-
});
|
|
2170
|
-
expect(frames.map((frame) => frame.parameters)).toEqual([
|
|
2171
|
-
{ greeting: 'old' },
|
|
2172
|
-
{ greeting: 'new' },
|
|
2173
|
-
]);
|
|
2174
|
-
});
|
|
2175
|
-
});
|