@myclaw163/clawclaw-cli 0.7.15 → 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 +3 -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 +286 -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 +389 -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 -1994
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1579
- 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
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import { ClawclawBehaviorCatalog } from './behavior-catalog.js';
|
|
6
|
+
import { listClawclawBehaviorSources } from './behavior-source.js';
|
|
7
|
+
|
|
8
|
+
const roots: string[] = [];
|
|
9
|
+
|
|
10
|
+
function createCatalogFixture(): {
|
|
11
|
+
root: string;
|
|
12
|
+
catalog: ClawclawBehaviorCatalog;
|
|
13
|
+
markerA: string;
|
|
14
|
+
markerB: string;
|
|
15
|
+
} {
|
|
16
|
+
const root = mkdtempSync(join(tmpdir(), 'clawclaw-behavior-catalog-'));
|
|
17
|
+
roots.push(root);
|
|
18
|
+
const suffix = root.replace(/[^a-z0-9]/gi, '_');
|
|
19
|
+
const markerA = `__behavior_lazy_a_${suffix}`;
|
|
20
|
+
const markerB = `__behavior_lazy_b_${suffix}`;
|
|
21
|
+
writeFileSync(join(root, 'a.ts'), `
|
|
22
|
+
import { ClawclawBehavior } from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
23
|
+
globalThis.${markerA} = (globalThis.${markerA} ?? 0) + 1;
|
|
24
|
+
export default class LazyA extends ClawclawBehavior {
|
|
25
|
+
static readonly manifest = {
|
|
26
|
+
apiVersion: 3,
|
|
27
|
+
id: 'lazy_a',
|
|
28
|
+
name: 'Lazy A',
|
|
29
|
+
description: 'Selected lazy module.',
|
|
30
|
+
inputs: [{
|
|
31
|
+
id: 'label',
|
|
32
|
+
type: 'string',
|
|
33
|
+
description: 'Label.',
|
|
34
|
+
default: ' ready ',
|
|
35
|
+
}, {
|
|
36
|
+
id: 'mode',
|
|
37
|
+
type: 'enum',
|
|
38
|
+
description: 'Mode.',
|
|
39
|
+
values: ['safe', 'fast'],
|
|
40
|
+
default: 'safe',
|
|
41
|
+
}],
|
|
42
|
+
runtime: { defaultMs: 5_000, controlGraceMs: 250 },
|
|
43
|
+
} as const;
|
|
44
|
+
override normalize(input, api) {
|
|
45
|
+
return {
|
|
46
|
+
label: api.string(input.label, 'label'),
|
|
47
|
+
mode: api.choice(input.mode, 'mode', ['safe', 'fast']),
|
|
48
|
+
...api.timing(input),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
override decide() {}
|
|
52
|
+
}
|
|
53
|
+
`);
|
|
54
|
+
writeFileSync(join(root, 'b.ts'), `
|
|
55
|
+
import { ClawclawBehavior } from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
56
|
+
globalThis.${markerB} = (globalThis.${markerB} ?? 0) + 1;
|
|
57
|
+
export default class LazyB extends ClawclawBehavior {
|
|
58
|
+
static readonly manifest = {
|
|
59
|
+
apiVersion: 3,
|
|
60
|
+
id: 'lazy_b',
|
|
61
|
+
name: 'Lazy B',
|
|
62
|
+
description: 'Unselected lazy module.',
|
|
63
|
+
inputs: [],
|
|
64
|
+
} as const;
|
|
65
|
+
override decide() {}
|
|
66
|
+
}
|
|
67
|
+
`);
|
|
68
|
+
return {
|
|
69
|
+
root,
|
|
70
|
+
catalog: new ClawclawBehaviorCatalog(listClawclawBehaviorSources(root)),
|
|
71
|
+
markerA,
|
|
72
|
+
markerB,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('ClawclawBehaviorCatalog', () => {
|
|
81
|
+
it('keeps list/has static and imports only the selected module on prepare', async () => {
|
|
82
|
+
const { catalog, markerA, markerB } = createCatalogFixture();
|
|
83
|
+
const globals = globalThis as Record<string, unknown>;
|
|
84
|
+
|
|
85
|
+
expect(catalog.has('lazy_a')).toBe(true);
|
|
86
|
+
expect(catalog.list().map(({ id, loadable }) => ({ id, loadable }))).toEqual([
|
|
87
|
+
{ id: 'lazy_a', loadable: true },
|
|
88
|
+
{ id: 'lazy_b', loadable: true },
|
|
89
|
+
]);
|
|
90
|
+
expect(globals[markerA]).toBeUndefined();
|
|
91
|
+
expect(globals[markerB]).toBeUndefined();
|
|
92
|
+
|
|
93
|
+
const prepared = await catalog.prepare('lazy_a', {});
|
|
94
|
+
expect(prepared.input).toEqual({
|
|
95
|
+
label: 'ready',
|
|
96
|
+
mode: 'safe',
|
|
97
|
+
timeout_ms: 5_000,
|
|
98
|
+
});
|
|
99
|
+
expect(prepared.timing).toEqual({ timeout_ms: 5_000 });
|
|
100
|
+
expect(Object.isFrozen(prepared.input)).toBe(true);
|
|
101
|
+
expect(globals[markerA]).toBe(1);
|
|
102
|
+
expect(globals[markerB]).toBeUndefined();
|
|
103
|
+
|
|
104
|
+
await catalog.prepare('lazy_a', { label: 'again', duration_ms: 2_000 });
|
|
105
|
+
expect(globals[markerA]).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('validates schema, timing, and normalized values before Activation', async () => {
|
|
109
|
+
const { catalog } = createCatalogFixture();
|
|
110
|
+
await expect(catalog.prepare('lazy_a', { unknown: true }))
|
|
111
|
+
.rejects.toThrow('behavior_input_field_unknown:unknown');
|
|
112
|
+
await expect(catalog.prepare('lazy_a', { mode: 'unsafe' }))
|
|
113
|
+
.rejects.toThrow('behavior_input_value_enum_invalid:mode');
|
|
114
|
+
await expect(catalog.prepare('lazy_a', {
|
|
115
|
+
timeout_ms: 2_000,
|
|
116
|
+
duration_ms: 2_000,
|
|
117
|
+
})).rejects.toThrow('behavior_runtime_ms_ambiguous');
|
|
118
|
+
await expect(catalog.prepare('lazy_a', { timeout_ms: 999 }))
|
|
119
|
+
.rejects.toThrow('behavior_runtime_ms_invalid');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('reports incompatible static entries without evaluating them and rejects duplicates', async () => {
|
|
123
|
+
const { root, catalog } = createCatalogFixture();
|
|
124
|
+
const legacyMarker = `__legacy_catalog_${root.replace(/[^a-z0-9]/gi, '_')}`;
|
|
125
|
+
writeFileSync(join(root, 'legacy.ts'), `
|
|
126
|
+
import { ClawclawBehavior } from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
127
|
+
globalThis.${legacyMarker} = true;
|
|
128
|
+
export default class Legacy extends ClawclawBehavior {
|
|
129
|
+
static readonly manifest = {
|
|
130
|
+
apiVersion: 2,
|
|
131
|
+
id: 'legacy',
|
|
132
|
+
name: 'Legacy',
|
|
133
|
+
description: 'Unsupported API.',
|
|
134
|
+
inputs: [],
|
|
135
|
+
} as const;
|
|
136
|
+
override decide() {}
|
|
137
|
+
}
|
|
138
|
+
`);
|
|
139
|
+
const sources = listClawclawBehaviorSources(root);
|
|
140
|
+
const withLegacy = new ClawclawBehaviorCatalog(sources);
|
|
141
|
+
expect(withLegacy.list().find(({ id }) => id === 'legacy')).toMatchObject({
|
|
142
|
+
apiVersion: 2,
|
|
143
|
+
expectedApiVersion: 3,
|
|
144
|
+
loadable: false,
|
|
145
|
+
compatibilityError: 'behavior_api_version_unsupported:expected=3:received=2',
|
|
146
|
+
});
|
|
147
|
+
await expect(withLegacy.load('legacy'))
|
|
148
|
+
.rejects.toThrow('behavior_api_version_unsupported:expected=3:received=2');
|
|
149
|
+
expect((globalThis as Record<string, unknown>)[legacyMarker]).toBeUndefined();
|
|
150
|
+
|
|
151
|
+
const [first] = listClawclawBehaviorSources(root);
|
|
152
|
+
expect(() => new ClawclawBehaviorCatalog([first, first]))
|
|
153
|
+
.toThrow(`Duplicate behavior module id '${first.id}'.`);
|
|
154
|
+
expect(catalog.has('legacy')).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { BEHAVIOR_PLUGIN_API_VERSION } from '../../../../sdk/behavior-plugin/index.js';
|
|
2
|
+
import { pluginApiVersionDiagnostic } from '../plugin-api-version.js';
|
|
3
|
+
import {
|
|
4
|
+
loadClawclawBehaviorModule,
|
|
5
|
+
type LoadedClawclawBehaviorModule,
|
|
6
|
+
} from './behavior-module-loader.js';
|
|
7
|
+
import {
|
|
8
|
+
prepareClawclawBehavior,
|
|
9
|
+
type PreparedClawclawBehavior,
|
|
10
|
+
} from './behavior-normalization.js';
|
|
11
|
+
import {
|
|
12
|
+
behaviorSourceCompatibilityError,
|
|
13
|
+
type ResolvedClawclawBehaviorSource,
|
|
14
|
+
} from './behavior-source.js';
|
|
15
|
+
|
|
16
|
+
export interface ClawclawBehaviorCatalogEntry {
|
|
17
|
+
readonly apiVersion: number | null;
|
|
18
|
+
readonly expectedApiVersion: typeof BEHAVIOR_PLUGIN_API_VERSION;
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly description: string;
|
|
22
|
+
readonly localizations?: ResolvedClawclawBehaviorSource['localizations'];
|
|
23
|
+
readonly inputs: ResolvedClawclawBehaviorSource['inputs'];
|
|
24
|
+
readonly execution: ResolvedClawclawBehaviorSource['execution'];
|
|
25
|
+
readonly everyTicks: number;
|
|
26
|
+
readonly produces?: readonly string[];
|
|
27
|
+
readonly runtime?: ResolvedClawclawBehaviorSource['runtime'];
|
|
28
|
+
readonly provenance?: ResolvedClawclawBehaviorSource['provenance'];
|
|
29
|
+
readonly loadable: boolean;
|
|
30
|
+
readonly compatibilityError?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function catalogEntry(source: ResolvedClawclawBehaviorSource): ClawclawBehaviorCatalogEntry {
|
|
34
|
+
const apiError = source.apiVersion === BEHAVIOR_PLUGIN_API_VERSION
|
|
35
|
+
? undefined
|
|
36
|
+
: pluginApiVersionDiagnostic(
|
|
37
|
+
'behavior_api_version_unsupported',
|
|
38
|
+
BEHAVIOR_PLUGIN_API_VERSION,
|
|
39
|
+
source.apiVersion,
|
|
40
|
+
);
|
|
41
|
+
const compatibilityError = apiError ?? behaviorSourceCompatibilityError(source);
|
|
42
|
+
return structuredClone({
|
|
43
|
+
apiVersion: source.apiVersion ?? null,
|
|
44
|
+
expectedApiVersion: BEHAVIOR_PLUGIN_API_VERSION,
|
|
45
|
+
id: source.id,
|
|
46
|
+
name: source.name,
|
|
47
|
+
description: source.description,
|
|
48
|
+
...(source.localizations === undefined ? {} : { localizations: source.localizations }),
|
|
49
|
+
inputs: source.inputs,
|
|
50
|
+
execution: source.execution,
|
|
51
|
+
everyTicks: source.everyTicks,
|
|
52
|
+
...(source.produces === undefined ? {} : { produces: source.produces }),
|
|
53
|
+
...(source.runtime === undefined ? {} : { runtime: source.runtime }),
|
|
54
|
+
...(source.provenance === undefined ? {} : { provenance: source.provenance }),
|
|
55
|
+
loadable: compatibilityError === undefined,
|
|
56
|
+
...(compatibilityError === undefined ? {} : { compatibilityError }),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A static catalog: discovered modules are imported only by load()/prepare(). */
|
|
61
|
+
export class ClawclawBehaviorCatalog {
|
|
62
|
+
private readonly sources = new Map<string, ResolvedClawclawBehaviorSource>();
|
|
63
|
+
private readonly loaded = new Map<string, Promise<LoadedClawclawBehaviorModule>>();
|
|
64
|
+
|
|
65
|
+
constructor(sources: readonly ResolvedClawclawBehaviorSource[]) {
|
|
66
|
+
for (const source of sources) {
|
|
67
|
+
if (this.sources.has(source.id)) {
|
|
68
|
+
throw new Error(`Duplicate behavior module id '${source.id}'.`);
|
|
69
|
+
}
|
|
70
|
+
this.sources.set(source.id, source);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
list(): readonly ClawclawBehaviorCatalogEntry[] {
|
|
75
|
+
return [...this.sources.values()].map(catalogEntry);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
has(id: string): boolean {
|
|
79
|
+
return this.sources.has(id);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
load(id: string): Promise<LoadedClawclawBehaviorModule> {
|
|
83
|
+
const source = this.sources.get(id);
|
|
84
|
+
if (!source) return Promise.reject(new Error(`Unknown behavior module '${id}'.`));
|
|
85
|
+
let loading = this.loaded.get(id);
|
|
86
|
+
if (!loading) {
|
|
87
|
+
loading = loadClawclawBehaviorModule(source);
|
|
88
|
+
this.loaded.set(id, loading);
|
|
89
|
+
}
|
|
90
|
+
return loading;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async prepare(id: string, input: unknown): Promise<PreparedClawclawBehavior> {
|
|
94
|
+
const source = this.sources.get(id);
|
|
95
|
+
if (!source) throw new Error(`Unknown behavior module '${id}'.`);
|
|
96
|
+
const loaded = await this.load(id);
|
|
97
|
+
return prepareClawclawBehavior(
|
|
98
|
+
loaded.BehaviorClass,
|
|
99
|
+
loaded.manifest,
|
|
100
|
+
input,
|
|
101
|
+
loaded.source ?? source,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BEHAVIOR_PLUGIN_API_VERSION,
|
|
3
|
+
hasValidClawclawResourceLocalizations,
|
|
4
|
+
type BehaviorRuntimeDefaults,
|
|
5
|
+
type ClawclawBehaviorManifest,
|
|
6
|
+
} from '../../../../sdk/behavior-plugin/index.js';
|
|
7
|
+
import { isClawclawFixedEventType } from '../../../clawclaw-event-catalog.js';
|
|
8
|
+
import { normalizeBehaviorInputFields } from '../../behavior-input-fields.js';
|
|
9
|
+
import { assertPluginApiVersion } from '../plugin-api-version.js';
|
|
10
|
+
|
|
11
|
+
const EVENT_TYPE = /^[a-z][a-z0-9_.-]*$/;
|
|
12
|
+
const BEHAVIOR_ID = /^[a-z0-9][a-z0-9._-]*$/;
|
|
13
|
+
const MANIFEST_FIELDS = new Set([
|
|
14
|
+
'apiVersion',
|
|
15
|
+
'id',
|
|
16
|
+
'name',
|
|
17
|
+
'description',
|
|
18
|
+
'localizations',
|
|
19
|
+
'inputs',
|
|
20
|
+
'execution',
|
|
21
|
+
'everyTicks',
|
|
22
|
+
'produces',
|
|
23
|
+
'runtime',
|
|
24
|
+
]);
|
|
25
|
+
const RUNTIME_FIELDS = new Set(['defaultMs', 'deadlineOutcome', 'controlGraceMs']);
|
|
26
|
+
|
|
27
|
+
function own(value: object, key: string): boolean {
|
|
28
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function deepFreezeBehaviorValue<T>(value: T): T {
|
|
32
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value;
|
|
33
|
+
Object.freeze(value);
|
|
34
|
+
for (const child of Object.values(value as Record<string, unknown>)) {
|
|
35
|
+
deepFreezeBehaviorValue(child);
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function behaviorProducesDiagnostic(
|
|
41
|
+
produces: unknown,
|
|
42
|
+
options: Readonly<{ allowReservedProduces?: readonly string[] }> = {},
|
|
43
|
+
): string | undefined {
|
|
44
|
+
if (!Array.isArray(produces)) return 'behavior_produces_invalid';
|
|
45
|
+
const seen = new Set<string>();
|
|
46
|
+
const allowedReserved = new Set(options.allowReservedProduces ?? []);
|
|
47
|
+
for (const candidate of produces) {
|
|
48
|
+
if (typeof candidate !== 'string' || !EVENT_TYPE.test(candidate)) {
|
|
49
|
+
return `behavior_produces_invalid:${String(candidate)}`;
|
|
50
|
+
}
|
|
51
|
+
if (seen.has(candidate)) return `behavior_produces_duplicate:${candidate}`;
|
|
52
|
+
seen.add(candidate);
|
|
53
|
+
if (isClawclawFixedEventType(candidate) && !allowedReserved.has(candidate)) {
|
|
54
|
+
return `behavior_produces_reserved:${candidate}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function normalizeBehaviorRuntimeDefaults(
|
|
61
|
+
value: unknown,
|
|
62
|
+
): Readonly<BehaviorRuntimeDefaults> {
|
|
63
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
64
|
+
throw new Error('behavior_runtime_invalid');
|
|
65
|
+
}
|
|
66
|
+
const runtime = value as Readonly<Record<string, unknown>>;
|
|
67
|
+
const unknownField = Object.keys(runtime).find((field) => !RUNTIME_FIELDS.has(field));
|
|
68
|
+
if (unknownField) throw new Error(`behavior_runtime_field_unknown:${unknownField}`);
|
|
69
|
+
if (!Number.isSafeInteger(runtime.defaultMs)
|
|
70
|
+
|| (runtime.defaultMs as number) < 1_000
|
|
71
|
+
|| (runtime.defaultMs as number) > 120_000) {
|
|
72
|
+
throw new Error('behavior_runtime_default_invalid');
|
|
73
|
+
}
|
|
74
|
+
if (
|
|
75
|
+
runtime.deadlineOutcome !== undefined
|
|
76
|
+
&& runtime.deadlineOutcome !== 'completed'
|
|
77
|
+
&& runtime.deadlineOutcome !== 'failed'
|
|
78
|
+
) throw new Error('behavior_runtime_deadline_outcome_invalid');
|
|
79
|
+
if (
|
|
80
|
+
runtime.controlGraceMs !== undefined
|
|
81
|
+
&& (
|
|
82
|
+
!Number.isSafeInteger(runtime.controlGraceMs)
|
|
83
|
+
|| (runtime.controlGraceMs as number) < 0
|
|
84
|
+
|| (runtime.controlGraceMs as number) > 30_000
|
|
85
|
+
)
|
|
86
|
+
) throw new Error('behavior_runtime_control_grace_invalid');
|
|
87
|
+
|
|
88
|
+
return deepFreezeBehaviorValue({
|
|
89
|
+
defaultMs: runtime.defaultMs as number,
|
|
90
|
+
...(runtime.deadlineOutcome === undefined
|
|
91
|
+
? {}
|
|
92
|
+
: { deadlineOutcome: runtime.deadlineOutcome as 'completed' | 'failed' }),
|
|
93
|
+
...(runtime.controlGraceMs === undefined
|
|
94
|
+
? {}
|
|
95
|
+
: { controlGraceMs: runtime.controlGraceMs as number }),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function nonEmpty(value: unknown): value is string {
|
|
100
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Validates, detaches, canonicalizes, and deeply freezes a loaded manifest. */
|
|
104
|
+
export function normalizeClawclawBehaviorManifest(
|
|
105
|
+
value: unknown,
|
|
106
|
+
options: Readonly<{ allowReservedProduces?: readonly string[] }> = {},
|
|
107
|
+
): Readonly<ClawclawBehaviorManifest> {
|
|
108
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
109
|
+
throw new Error('behavior_manifest_missing');
|
|
110
|
+
}
|
|
111
|
+
const manifest = value as Readonly<Record<string, unknown>>;
|
|
112
|
+
const unknownField = Object.keys(manifest).find((field) => !MANIFEST_FIELDS.has(field));
|
|
113
|
+
if (unknownField) throw new Error(`behavior_manifest_field_unknown:${unknownField}`);
|
|
114
|
+
assertPluginApiVersion(
|
|
115
|
+
'behavior_manifest_api_version_unsupported',
|
|
116
|
+
BEHAVIOR_PLUGIN_API_VERSION,
|
|
117
|
+
typeof manifest.apiVersion === 'number' ? manifest.apiVersion : undefined,
|
|
118
|
+
);
|
|
119
|
+
const id = nonEmpty(manifest.id) ? manifest.id.trim() : '';
|
|
120
|
+
if (!BEHAVIOR_ID.test(id)) throw new Error('behavior_id_invalid');
|
|
121
|
+
if (!nonEmpty(manifest.name)) throw new Error('behavior_name_invalid');
|
|
122
|
+
if (!nonEmpty(manifest.description)) throw new Error('behavior_description_invalid');
|
|
123
|
+
if (!hasValidClawclawResourceLocalizations(
|
|
124
|
+
manifest.localizations as ClawclawBehaviorManifest['localizations'],
|
|
125
|
+
)) throw new Error('behavior_localizations_invalid');
|
|
126
|
+
if (!own(manifest, 'inputs')) throw new Error('behavior_inputs_missing');
|
|
127
|
+
const inputs = normalizeBehaviorInputFields(manifest.inputs);
|
|
128
|
+
const reserved = inputs.find(({ id: inputId }) => (
|
|
129
|
+
inputId === 'timeout_ms' || inputId === 'duration_ms'
|
|
130
|
+
));
|
|
131
|
+
if (reserved) throw new Error(`behavior_input_id_reserved:${reserved.id}`);
|
|
132
|
+
if (
|
|
133
|
+
manifest.execution !== undefined
|
|
134
|
+
&& manifest.execution !== 'parallel'
|
|
135
|
+
&& manifest.execution !== 'serial'
|
|
136
|
+
) throw new Error('behavior_execution_invalid');
|
|
137
|
+
const everyTicks = manifest.everyTicks ?? 1;
|
|
138
|
+
if (!Number.isSafeInteger(everyTicks) || (everyTicks as number) <= 0) {
|
|
139
|
+
throw new Error('behavior_every_ticks_invalid');
|
|
140
|
+
}
|
|
141
|
+
if (manifest.produces !== undefined) {
|
|
142
|
+
const diagnostic = behaviorProducesDiagnostic(manifest.produces, options);
|
|
143
|
+
if (diagnostic) throw new Error(diagnostic);
|
|
144
|
+
}
|
|
145
|
+
const runtime = manifest.runtime === undefined
|
|
146
|
+
? undefined
|
|
147
|
+
: normalizeBehaviorRuntimeDefaults(manifest.runtime);
|
|
148
|
+
|
|
149
|
+
let localizations: ClawclawBehaviorManifest['localizations'];
|
|
150
|
+
try {
|
|
151
|
+
localizations = manifest.localizations === undefined
|
|
152
|
+
? undefined
|
|
153
|
+
: structuredClone(manifest.localizations) as ClawclawBehaviorManifest['localizations'];
|
|
154
|
+
} catch {
|
|
155
|
+
throw new Error('behavior_localizations_not_cloneable');
|
|
156
|
+
}
|
|
157
|
+
return deepFreezeBehaviorValue({
|
|
158
|
+
apiVersion: BEHAVIOR_PLUGIN_API_VERSION,
|
|
159
|
+
id,
|
|
160
|
+
name: (manifest.name as string).trim(),
|
|
161
|
+
description: (manifest.description as string).trim(),
|
|
162
|
+
...(localizations === undefined ? {} : { localizations }),
|
|
163
|
+
inputs,
|
|
164
|
+
...(manifest.execution === undefined
|
|
165
|
+
? {}
|
|
166
|
+
: { execution: manifest.execution as 'parallel' | 'serial' }),
|
|
167
|
+
...(manifest.everyTicks === undefined ? {} : { everyTicks: everyTicks as number }),
|
|
168
|
+
...(manifest.produces === undefined
|
|
169
|
+
? {}
|
|
170
|
+
: { produces: [...manifest.produces as readonly string[]] }),
|
|
171
|
+
...(runtime === undefined ? {} : { runtime }),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import { ClawclawBehavior } from '../../../../sdk/behavior-plugin/index.js';
|
|
6
|
+
import {
|
|
7
|
+
loadClawclawBehaviorModule,
|
|
8
|
+
validateClawclawBehaviorModule,
|
|
9
|
+
} from './behavior-module-loader.js';
|
|
10
|
+
import { listClawclawBehaviorSources } from './behavior-source.js';
|
|
11
|
+
|
|
12
|
+
const roots: string[] = [];
|
|
13
|
+
|
|
14
|
+
function fixture(id = 'loaded_behavior'): { root: string; path: string } {
|
|
15
|
+
const root = mkdtempSync(join(tmpdir(), 'clawclaw-behavior-loader-'));
|
|
16
|
+
roots.push(root);
|
|
17
|
+
const path = join(root, `${id}.ts`);
|
|
18
|
+
writeFileSync(path, `
|
|
19
|
+
import { ClawclawBehavior } from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
20
|
+
export default class LoadedBehavior extends ClawclawBehavior {
|
|
21
|
+
static override readonly manifest = {
|
|
22
|
+
apiVersion: 3,
|
|
23
|
+
id: '${id}',
|
|
24
|
+
name: 'Loaded behavior',
|
|
25
|
+
description: 'Loader parity fixture.',
|
|
26
|
+
inputs: [{
|
|
27
|
+
id: 'target',
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: 'Target.',
|
|
30
|
+
required: true,
|
|
31
|
+
}],
|
|
32
|
+
execution: 'serial',
|
|
33
|
+
everyTicks: 2,
|
|
34
|
+
produces: ['custom.loaded'],
|
|
35
|
+
runtime: { defaultMs: 4_000, deadlineOutcome: 'completed' },
|
|
36
|
+
} as const;
|
|
37
|
+
override normalize(input) {
|
|
38
|
+
return { target: input.target };
|
|
39
|
+
}
|
|
40
|
+
override decide() {}
|
|
41
|
+
}
|
|
42
|
+
`);
|
|
43
|
+
return { root, path };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('Behavior module loader', () => {
|
|
51
|
+
it('loads one API v3 class and returns an immutable canonical manifest', async () => {
|
|
52
|
+
const { root } = fixture();
|
|
53
|
+
const [source] = listClawclawBehaviorSources(root);
|
|
54
|
+
const loaded = await loadClawclawBehaviorModule(source);
|
|
55
|
+
|
|
56
|
+
expect(typeof loaded.BehaviorClass.prototype.decide).toBe('function');
|
|
57
|
+
expect(loaded.manifest).toMatchObject({
|
|
58
|
+
id: 'loaded_behavior',
|
|
59
|
+
execution: 'serial',
|
|
60
|
+
everyTicks: 2,
|
|
61
|
+
runtime: { defaultMs: 4_000, deadlineOutcome: 'completed' },
|
|
62
|
+
});
|
|
63
|
+
expect(Object.isFrozen(loaded.manifest)).toBe(true);
|
|
64
|
+
expect(Object.isFrozen(loaded.manifest.inputs)).toBe(true);
|
|
65
|
+
expect(Object.isFrozen(loaded.manifest.runtime)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('rejects every scanner/loader parity drift after import', async () => {
|
|
69
|
+
const { root } = fixture('parity_behavior');
|
|
70
|
+
const [source] = listClawclawBehaviorSources(root);
|
|
71
|
+
await expect(loadClawclawBehaviorModule({
|
|
72
|
+
...source,
|
|
73
|
+
runtime: { defaultMs: 5_000, deadlineOutcome: 'completed' },
|
|
74
|
+
})).rejects.toThrow('behavior_runtime_source_mismatch');
|
|
75
|
+
|
|
76
|
+
const loaded = await loadClawclawBehaviorModule(source);
|
|
77
|
+
const loadedModule = { default: loaded.BehaviorClass };
|
|
78
|
+
expect(() => validateClawclawBehaviorModule(loadedModule, {
|
|
79
|
+
...source,
|
|
80
|
+
execution: 'parallel',
|
|
81
|
+
})).toThrow('behavior_execution_source_mismatch');
|
|
82
|
+
expect(() => validateClawclawBehaviorModule(loadedModule, {
|
|
83
|
+
...source,
|
|
84
|
+
inputs: [],
|
|
85
|
+
})).toThrow('behavior_inputs_source_mismatch');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('rejects structural classes, invalid runtime declarations, and escaped paths', async () => {
|
|
89
|
+
class MissingDecision {
|
|
90
|
+
static readonly manifest = {
|
|
91
|
+
apiVersion: 3,
|
|
92
|
+
id: 'structural',
|
|
93
|
+
name: 'Structural',
|
|
94
|
+
description: 'Does not extend the public base.',
|
|
95
|
+
inputs: [],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
expect(() => validateClawclawBehaviorModule({ default: MissingDecision }))
|
|
99
|
+
.toThrow('behavior_decide_missing');
|
|
100
|
+
|
|
101
|
+
class InvalidRuntime extends ClawclawBehavior {
|
|
102
|
+
static override readonly manifest: any = {
|
|
103
|
+
apiVersion: 3,
|
|
104
|
+
id: 'invalid_runtime',
|
|
105
|
+
name: 'Invalid runtime',
|
|
106
|
+
description: 'Runtime budget exceeds the contract.',
|
|
107
|
+
inputs: [],
|
|
108
|
+
runtime: { defaultMs: 999 },
|
|
109
|
+
};
|
|
110
|
+
override decide() {}
|
|
111
|
+
}
|
|
112
|
+
expect(() => validateClawclawBehaviorModule({ default: InvalidRuntime }))
|
|
113
|
+
.toThrow('behavior_runtime_default_invalid');
|
|
114
|
+
|
|
115
|
+
const first = fixture('inside');
|
|
116
|
+
const second = fixture('outside');
|
|
117
|
+
const [outside] = listClawclawBehaviorSources(second.root);
|
|
118
|
+
await expect(loadClawclawBehaviorModule({
|
|
119
|
+
...outside,
|
|
120
|
+
sourceRoot: first.root,
|
|
121
|
+
})).rejects.toThrow("Behavior 'outside' source is outside its root.");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('allows fixed Events only through an explicit Host-owned source grant', async () => {
|
|
125
|
+
const { root, path } = fixture('official_event');
|
|
126
|
+
const original = String.raw`
|
|
127
|
+
import { ClawclawBehavior } from '@myclaw163/clawclaw-cli/behavior-plugin';
|
|
128
|
+
export default class OfficialEvent extends ClawclawBehavior {
|
|
129
|
+
static readonly manifest = {
|
|
130
|
+
apiVersion: 3,
|
|
131
|
+
id: 'official_event',
|
|
132
|
+
name: 'Official event',
|
|
133
|
+
description: 'Host-authenticated fixed Event fixture.',
|
|
134
|
+
inputs: [],
|
|
135
|
+
produces: ['strategy_alert'],
|
|
136
|
+
} as const;
|
|
137
|
+
override decide() {}
|
|
138
|
+
}
|
|
139
|
+
`;
|
|
140
|
+
writeFileSync(path, original);
|
|
141
|
+
const [source] = listClawclawBehaviorSources(root);
|
|
142
|
+
expect(source.compatibilityError).toBe('behavior_produces_reserved:strategy_alert');
|
|
143
|
+
await expect(loadClawclawBehaviorModule(source))
|
|
144
|
+
.rejects.toThrow('behavior_produces_reserved:strategy_alert');
|
|
145
|
+
await expect(loadClawclawBehaviorModule({
|
|
146
|
+
...source,
|
|
147
|
+
allowReservedProduces: ['strategy_alert'],
|
|
148
|
+
})).resolves.toMatchObject({
|
|
149
|
+
manifest: { produces: ['strategy_alert'] },
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('rejects unknown manifest and runtime fields after import', () => {
|
|
154
|
+
class UnknownManifestField extends ClawclawBehavior {
|
|
155
|
+
static override readonly manifest: any = {
|
|
156
|
+
apiVersion: 3,
|
|
157
|
+
id: 'unknown_manifest',
|
|
158
|
+
name: 'Unknown manifest',
|
|
159
|
+
description: 'Contains a removed field.',
|
|
160
|
+
inputs: [],
|
|
161
|
+
defaultRuntimeMs: 5_000,
|
|
162
|
+
};
|
|
163
|
+
override decide() {}
|
|
164
|
+
}
|
|
165
|
+
expect(() => validateClawclawBehaviorModule({ default: UnknownManifestField }))
|
|
166
|
+
.toThrow('behavior_manifest_field_unknown:defaultRuntimeMs');
|
|
167
|
+
|
|
168
|
+
class UnknownRuntimeField extends ClawclawBehavior {
|
|
169
|
+
static override readonly manifest: any = {
|
|
170
|
+
apiVersion: 3,
|
|
171
|
+
id: 'unknown_runtime',
|
|
172
|
+
name: 'Unknown runtime',
|
|
173
|
+
description: 'Contains an unknown runtime field.',
|
|
174
|
+
inputs: [],
|
|
175
|
+
runtime: { defaultMs: 5_000, retry: true },
|
|
176
|
+
};
|
|
177
|
+
override decide() {}
|
|
178
|
+
}
|
|
179
|
+
expect(() => validateClawclawBehaviorModule({ default: UnknownRuntimeField }))
|
|
180
|
+
.toThrow('behavior_runtime_field_unknown:retry');
|
|
181
|
+
});
|
|
182
|
+
});
|