@myclaw163/clawclaw-cli 0.6.111 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QUICKSTART.md +46 -0
- package/QUICKSTART_HERMES.md +134 -0
- package/QUICKSTART_OPENCLAW.md +134 -0
- package/QUICKSTART_OTHER.md +134 -0
- package/README.md +23 -398
- package/bin/clawclaw-cli.mjs +9 -1
- package/docs/configuration.md +197 -0
- package/docs/runtime/README.md +52 -0
- package/docs/runtime/agent-notifications.md +55 -0
- package/docs/runtime/components-and-boundaries.md +213 -0
- package/docs/runtime/events-and-state.md +105 -0
- package/docs/runtime/owner-lifecycle-and-observability.md +69 -0
- package/docs/runtime/perception-replay.md +51 -0
- package/docs/runtime/short-commands-and-capabilities.md +55 -0
- package/docs/runtime/strategy-runtime.md +111 -0
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +488 -0
- package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +395 -0
- package/package.json +24 -7
- package/scripts/check-skill-command-surface.mjs +16 -7
- package/scripts/compare-perception-replays.test.ts +188 -0
- package/scripts/compare-perception-replays.ts +589 -0
- package/scripts/generate-spatial-facts.py +246 -0
- package/scripts/package-readme.mjs +48 -0
- package/scripts/replay-perception.ts +347 -0
- package/scripts/sync-frontend-map.mjs +195 -0
- package/scripts/watch-perception-events.test.ts +43 -0
- package/scripts/watch-perception-events.ts +162 -0
- package/skills/clawclaw/SKILL.md +120 -64
- package/skills/clawclaw/references/CHATTERBOX.md +2 -4
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +95 -61
- package/skills/clawclaw/references/GAME-MECHANICS.md +18 -8
- package/skills/clawclaw/references/HUB.md +5 -5
- package/skills/clawclaw/references/STRATEGIES.md +34 -17
- package/skills/clawclaw/references/STREAM.md +82 -46
- package/src/assets/clawclaw-map.json +1692 -0
- package/src/assets/clawclaw-spatial-facts.json +1291 -0
- package/src/cli.ts +21 -47
- package/src/commands/account.test.ts +229 -50
- package/src/commands/account.ts +456 -223
- package/src/commands/communication.test.ts +259 -0
- package/src/commands/communication.ts +391 -0
- package/src/commands/competition.test.ts +116 -0
- package/src/commands/competition.ts +50 -28
- package/src/commands/data.test.ts +57 -1
- package/src/commands/data.ts +79 -4
- package/src/commands/events-canonical-state.test.ts +187 -0
- package/src/commands/events.test.ts +50 -136
- package/src/commands/events.ts +175 -241
- package/src/commands/game/index.test.ts +510 -0
- package/src/commands/game/index.ts +556 -0
- package/src/commands/game-map.test.ts +40 -3
- package/src/commands/hub.ts +2 -9
- package/src/commands/load.test.ts +12 -2
- package/src/commands/load.ts +15 -2
- package/src/commands/peek.ts +2 -3
- package/src/commands/perception.test.ts +46 -0
- package/src/commands/perception.ts +92 -0
- package/src/commands/setup/codex.ts +0 -1
- package/src/commands/strategy.test.ts +139 -83
- package/src/commands/strategy.ts +175 -125
- package/src/commands/upgrade.ts +0 -21
- package/src/lib/auth.test.ts +35 -0
- package/src/lib/auth.ts +45 -6
- package/src/lib/diagnostics-config.test.ts +59 -0
- package/src/lib/diagnostics-config.ts +76 -0
- package/src/lib/game-client.test.ts +166 -0
- package/src/lib/game-client.ts +316 -87
- package/src/lib/game-context.ts +29 -0
- package/src/lib/gameplay-config.test.ts +66 -0
- package/src/lib/gameplay-config.ts +151 -0
- package/src/lib/http-transport.test.ts +63 -0
- package/src/lib/http-transport.ts +76 -10
- package/src/lib/init-command.test.ts +25 -1
- package/src/lib/init-command.ts +66 -1
- package/src/lib/participation-leave.ts +78 -0
- package/src/lib/perception-config.test.ts +138 -0
- package/src/lib/perception-config.ts +101 -0
- package/src/lib/room.test.ts +14 -0
- package/src/lib/room.ts +28 -2
- package/src/lib/server-registry.test.ts +15 -5
- package/src/lib/server-registry.ts +9 -10
- package/src/lib/strategy-export.test.ts +123 -44
- package/src/lib/strategy-export.ts +111 -101
- package/src/lib/user-data.test.ts +2 -0
- package/src/lib/user-data.ts +17 -16
- package/src/pipeline/canonical-event-presentation.test.ts +195 -0
- package/src/pipeline/canonical-event-presentation.ts +71 -0
- package/src/pipeline/event-format.test.ts +357 -59
- package/src/pipeline/event-format.ts +365 -80
- package/src/pipeline/event-hints.test.ts +241 -0
- package/src/pipeline/event-hints.ts +202 -51
- package/src/pipeline/game-sync-event-normalization.test.ts +239 -0
- package/src/pipeline/game-sync-event-normalization.ts +215 -0
- package/src/pipeline/player-projection.test.ts +164 -102
- package/src/pipeline/player-projection.ts +194 -235
- package/src/runtime/canonical-event-emitter.test.ts +70 -0
- package/src/runtime/canonical-event-emitter.ts +78 -0
- package/src/runtime/canonical-monitor.test.ts +575 -0
- package/src/runtime/canonical-monitor.ts +388 -0
- package/src/runtime/clawclaw/agent-event-policy.test.ts +27 -0
- package/src/runtime/clawclaw/agent-event-policy.ts +35 -0
- package/src/runtime/clawclaw/index.ts +4 -0
- package/src/runtime/clawclaw/notifications.test.ts +47 -0
- package/src/runtime/clawclaw/notifications.ts +65 -0
- package/src/runtime/clawclaw/owner-lock.test.ts +221 -0
- package/src/runtime/clawclaw/owner.ts +945 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +299 -0
- package/src/runtime/clawclaw/plugins/action/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +81 -0
- package/src/runtime/clawclaw/plugins/assembly.ts +116 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +150 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +100 -0
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +162 -0
- package/src/runtime/clawclaw/plugins/derived-event/index.ts +2 -0
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +223 -0
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +236 -0
- package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/index.ts +7 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +77 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +91 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1008 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +475 -0
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +32 -0
- package/src/runtime/clawclaw/plugins/perception/index.ts +8 -0
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +204 -0
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +290 -0
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +324 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +365 -0
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +7 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +341 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +447 -0
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +30 -0
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +289 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +180 -0
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +329 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +82 -0
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +172 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +212 -0
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +256 -0
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +121 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +106 -0
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +225 -0
- package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +11 -0
- package/src/runtime/clawclaw/plugins/perception/official-modules.ts +31 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +759 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +707 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +215 -0
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +61 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +1516 -0
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +1529 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +119 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +244 -0
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +65 -0
- package/src/runtime/clawclaw/plugins/perception/replay.ts +86 -0
- package/src/runtime/clawclaw/plugins/perception/shared/math.ts +107 -0
- package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +114 -0
- package/src/runtime/clawclaw/plugins/perception/visual-state-projector.ts +98 -0
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +1019 -0
- package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +287 -0
- package/src/runtime/clawclaw/plugins/source/game-transport.ts +281 -0
- package/src/runtime/clawclaw/plugins/source/index.ts +5 -0
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +270 -0
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +151 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +307 -0
- package/src/runtime/clawclaw/plugins/source/room-transport.ts +480 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +1909 -0
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +2055 -0
- package/src/runtime/clawclaw/plugins/source/source-transports.ts +30 -0
- package/src/runtime/clawclaw/plugins/source/types.ts +79 -0
- package/src/runtime/clawclaw/plugins/strategy/context-capabilities.ts +5 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +177 -0
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +185 -0
- package/src/runtime/clawclaw/plugins/strategy/index.ts +8 -0
- package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +412 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +73 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +183 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +25 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +693 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +57 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +164 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +1936 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +1557 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +131 -0
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +413 -0
- package/src/runtime/clawclaw/plugins/strategy/thread-strategy-executor.ts +792 -0
- package/src/runtime/clawclaw/runtime-data-export.test.ts +249 -0
- package/src/runtime/clawclaw/runtime-data-export.ts +402 -0
- package/src/runtime/clawclaw/runtime-state.ts +98 -0
- package/src/runtime/clawclaw/runtime.ts +133 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +74 -0
- package/src/runtime/clawclaw-event-catalog.ts +266 -0
- package/src/runtime/clawclaw-position.ts +4 -0
- package/src/runtime/clawclaw-room-state.ts +51 -0
- package/src/runtime/framework/action-diagnostics.test.ts +81 -0
- package/src/runtime/framework/action-diagnostics.ts +146 -0
- package/src/runtime/framework/capability-registry.test.ts +282 -0
- package/src/runtime/framework/capability-registry.ts +283 -0
- package/src/runtime/framework/contracts.ts +118 -0
- package/src/runtime/framework/control/control-scheduler.ts +1260 -0
- package/src/runtime/framework/control/domain-set.ts +81 -0
- package/src/runtime/framework/control/index.ts +3 -0
- package/src/runtime/framework/control/types.ts +184 -0
- package/src/runtime/framework/control-coordinator.test.ts +660 -0
- package/src/runtime/framework/control-coordinator.ts +11 -0
- package/src/runtime/framework/events/event-catalog.ts +74 -0
- package/src/runtime/framework/events/event-delivery-service.test.ts +84 -0
- package/src/runtime/framework/events/event-delivery-service.ts +198 -0
- package/src/runtime/framework/events/event-ingress.test.ts +178 -0
- package/src/runtime/framework/events/event-ingress.ts +97 -0
- package/src/runtime/framework/events/event-repository.ts +59 -0
- package/src/runtime/framework/events/index.ts +7 -0
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +594 -0
- package/src/runtime/framework/events/memory-event-repository.ts +928 -0
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +200 -0
- package/src/runtime/framework/events/sqlite-event-repository.ts +1681 -0
- package/src/runtime/framework/events/test-helpers.ts +78 -0
- package/src/runtime/framework/events/types.ts +286 -0
- package/src/runtime/framework/index.ts +7 -0
- package/src/runtime/framework/load-node-sqlite.ts +17 -0
- package/src/runtime/framework/mailbox/coalescer.test.ts +55 -0
- package/src/runtime/framework/mailbox/coalescer.ts +83 -0
- package/src/runtime/framework/mailbox/host-notification-sink.ts +44 -0
- package/src/runtime/framework/mailbox/identity.test.ts +38 -0
- package/src/runtime/framework/mailbox/identity.ts +38 -0
- package/src/runtime/framework/mailbox/index.ts +7 -0
- package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +513 -0
- package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +282 -0
- package/src/runtime/framework/mailbox/mailbox-repository.ts +44 -0
- package/src/runtime/framework/mailbox/notification-projector.test.ts +77 -0
- package/src/runtime/framework/mailbox/notification-projector.ts +65 -0
- package/src/runtime/framework/mailbox/types.ts +139 -0
- package/src/runtime/framework/modules/runtime-module-loader-hooks.d.mts +19 -0
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +117 -0
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +209 -0
- package/src/runtime/framework/modules/runtime-module-loader.ts +71 -0
- package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +23 -0
- package/src/runtime/framework/participation-manager.test.ts +108 -0
- package/src/runtime/framework/participation-manager.ts +244 -0
- package/src/runtime/framework/plugins/contracts.ts +382 -0
- package/src/runtime/framework/plugins/index.ts +4 -0
- package/src/runtime/framework/plugins/runtime-event-identity.ts +17 -0
- package/src/runtime/framework/plugins/runtime-input-journal.test.ts +179 -0
- package/src/runtime/framework/plugins/runtime-input-journal.ts +693 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +1994 -0
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +1860 -0
- package/src/runtime/framework/runtime.ts +1756 -0
- package/src/runtime/framework/serial-task-queue.ts +14 -0
- package/src/runtime/framework/state/contracts.ts +49 -0
- package/src/runtime/framework/state/current-state-store.ts +184 -0
- package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +144 -0
- package/src/runtime/framework/state/durable-agent-state-snapshot.ts +209 -0
- package/src/runtime/framework/state/freshness.ts +72 -0
- package/src/runtime/framework/state/history/contracts.ts +150 -0
- package/src/runtime/framework/state/history/history-writer.ts +194 -0
- package/src/runtime/framework/state/history/memory-state-history-repository.ts +178 -0
- package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +157 -0
- package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +629 -0
- package/src/runtime/framework/state/history/state-history.test.ts +251 -0
- package/src/runtime/framework/state/history/trajectory-projector.ts +209 -0
- package/src/runtime/framework/state/index.ts +13 -0
- package/src/runtime/framework/state/state-core.test.ts +197 -0
- package/src/runtime/framework/state/state-ingress.ts +95 -0
- package/src/runtime/framework/state/state-provider.ts +62 -0
- package/src/runtime/framework/state/types.ts +155 -0
- package/src/runtime/game-sync-protocol.test.ts +123 -0
- package/src/runtime/game-sync-protocol.ts +261 -0
- package/src/runtime/mailbox-host-relay.test.ts +248 -0
- package/src/runtime/mailbox-host-relay.ts +300 -0
- package/src/runtime/observability/factory.ts +188 -0
- package/src/runtime/observability/index.ts +6 -0
- package/src/runtime/observability/observability.test.ts +561 -0
- package/src/runtime/observability/recorder.ts +209 -0
- package/src/runtime/observability/redaction.ts +154 -0
- package/src/runtime/observability/reporter.ts +154 -0
- package/src/runtime/observability/sinks.ts +135 -0
- package/src/runtime/observability/types.ts +92 -0
- package/src/runtime/owner-control.test.ts +153 -0
- package/src/runtime/owner-control.ts +206 -42
- package/src/runtime/parent-process-watch.test.ts +27 -0
- package/src/runtime/parent-process-watch.ts +51 -0
- package/src/runtime/pending-event-handoff.test.ts +29 -0
- package/src/runtime/pending-event-handoff.ts +61 -0
- package/src/runtime/raw-ws-log.test.ts +86 -17
- package/src/runtime/raw-ws-log.ts +230 -21
- package/src/runtime/runtime-logger.test.ts +58 -0
- package/src/runtime/runtime-logger.ts +10 -13
- package/src/runtime/runtime-persistence.test.ts +26 -0
- package/src/runtime/runtime.test.ts +1704 -0
- package/src/runtime/ws-client.test.ts +496 -103
- package/src/runtime/ws-client.ts +546 -166
- package/src/sdk/action.ts +3 -1
- package/src/sdk/index.ts +4 -19
- package/src/sdk/perception-plugin/index.test.ts +22 -0
- package/src/sdk/perception-plugin/index.ts +285 -0
- package/src/sdk/strategy-plugin/index.test.ts +952 -0
- package/src/sdk/strategy-plugin/index.ts +1324 -0
- package/src/sdk/types.ts +3 -1
- package/src/short-command/classifier.test.ts +107 -0
- package/src/short-command/classifier.ts +169 -0
- package/src/short-command/index.ts +7 -0
- package/src/short-command/presenter.test.ts +119 -0
- package/src/short-command/presenter.ts +308 -0
- package/src/short-command/process-io.ts +13 -0
- package/src/short-command/response-contract.ts +58 -0
- package/src/short-command/runner.test.ts +78 -0
- package/src/short-command/runner.ts +164 -0
- package/src/short-command/sensitive-data-filter.test.ts +56 -0
- package/src/short-command/sensitive-data-filter.ts +123 -0
- package/src/short-command/transport-response.test.ts +39 -0
- package/src/short-command/transport-response.ts +89 -0
- package/src/short-command/types.ts +92 -0
- package/src/strategies/avoid-lone.ts +18 -11
- package/src/strategies/avoid-players.ts +29 -13
- package/src/strategies/corpse-patrol.ts +29 -11
- package/src/strategies/crab-sabotage.ts +31 -18
- package/src/strategies/custom-module.test.ts +3 -5
- package/src/strategies/find-player.ts +34 -15
- package/src/strategies/game-utils.test.ts +42 -0
- package/src/strategies/game-utils.ts +143 -53
- package/src/strategies/goals/avoid-lone-top.ts +22 -1
- package/src/strategies/goals/avoid-players-top.test.ts +13 -0
- package/src/strategies/goals/avoid-players-top.ts +6 -5
- package/src/strategies/goals/corpse-patrol-top.ts +9 -2
- package/src/strategies/goals/crab-octopus-reflexes.ts +6 -2
- package/src/strategies/goals/crab-sabotage-top.ts +7 -1
- package/src/strategies/goals/emergency-hunt-goal.ts +15 -3
- package/src/strategies/goals/find-player-top.ts +47 -3
- package/src/strategies/goals/flee-players-goal.ts +2 -0
- package/src/strategies/goals/follow-companion-goal.ts +27 -3
- package/src/strategies/goals/hide-top.ts +47 -5
- package/src/strategies/goals/keep-away-goal.ts +24 -2
- package/src/strategies/goals/kill-frenzy-top.ts +12 -2
- package/src/strategies/goals/kill-lone-top.test.ts +72 -0
- package/src/strategies/goals/kill-lone-top.ts +8 -6
- package/src/strategies/goals/kill-target-goal.ts +16 -7
- package/src/strategies/goals/kill-target-top.ts +16 -2
- package/src/strategies/goals/linger-corpse-goal.ts +11 -3
- package/src/strategies/goals/lone-kill-core.ts +4 -2
- package/src/strategies/goals/move-room-goal.ts +46 -4
- package/src/strategies/goals/normal-shrimp-top.test.ts +16 -0
- package/src/strategies/goals/normal-shrimp-top.ts +18 -18
- package/src/strategies/goals/paradise-fish-top.test.ts +78 -0
- package/src/strategies/goals/paradise-fish-top.ts +16 -6
- package/src/strategies/goals/patrol-top.ts +14 -3
- package/src/strategies/goals/report-patrol-top.ts +14 -3
- package/src/strategies/goals/safe-task-goal.ts +32 -4
- package/src/strategies/goals/social-task-top.ts +39 -8
- package/src/strategies/goals/task-kill-report-top.ts +33 -10
- package/src/strategies/goals/task-only-top.test.ts +89 -0
- package/src/strategies/goals/task-only-top.ts +9 -2
- package/src/strategies/goals/task-or-patrol-goal.ts +21 -3
- package/src/strategies/goals/warrior-shrimp-top.test.ts +75 -23
- package/src/strategies/goals/warrior-shrimp-top.ts +34 -13
- package/src/strategies/greeting.ts +10 -3
- package/src/strategies/hide-spots.ts +4 -0
- package/src/strategies/hide.ts +35 -20
- package/src/strategies/kill-frenzy.ts +18 -12
- package/src/strategies/kill-lone.ts +43 -12
- package/src/strategies/kill-target.ts +34 -17
- package/src/strategies/lone-kill-task.ts +31 -18
- package/src/strategies/move-room.ts +32 -15
- package/src/strategies/off-route-points.ts +12 -4
- package/src/strategies/paradise-fish.ts +49 -14
- package/src/strategies/pathfind/astar-path.test.ts +73 -0
- package/src/strategies/pathfind/astar-path.ts +196 -0
- package/src/strategies/pathfind/escape-planner.test.ts +27 -1
- package/src/strategies/pathfind/escape-planner.ts +108 -0
- package/src/strategies/patrol.ts +18 -11
- package/src/strategies/report-patrol.ts +18 -11
- package/src/strategies/shrimp-memory.ts +66 -14
- package/src/strategies/social-task.ts +49 -45
- package/src/strategies/task-kill-report.ts +34 -16
- package/src/strategies/task-only.test.ts +124 -0
- package/src/strategies/task-only.ts +20 -11
- package/src/strategies/task-report.ts +29 -11
- package/src/strategies/types.ts +29 -62
- package/src/strategies/warrior-memory.ts +44 -14
- package/scripts/check_monitor.test.mjs +0 -212
- package/skills/clawclaw/references/KNOWLEDGE.md +0 -42
- package/src/commands/do.test.ts +0 -117
- package/src/commands/do.ts +0 -195
- package/src/commands/game-monitor-status.test.ts +0 -58
- package/src/commands/game-start-plan.test.ts +0 -356
- package/src/commands/game.ts +0 -2046
- package/src/commands/history-player.test.ts +0 -105
- package/src/commands/history.ts +0 -573
- package/src/commands/knowledge.test.ts +0 -13
- package/src/commands/knowledge.ts +0 -176
- package/src/commands/meeting-history.test.ts +0 -106
- package/src/commands/state.ts +0 -46
- package/src/commands/watch.test.ts +0 -1313
- package/src/commands/watch.ts +0 -824
- package/src/lib/knowledge-store.test.ts +0 -170
- package/src/lib/knowledge-store.ts +0 -369
- package/src/perception/player-history-store.test.ts +0 -177
- package/src/perception/player-history-store.ts +0 -262
- package/src/pipeline/event-store.test.ts +0 -28
- package/src/pipeline/event-store.ts +0 -193
- package/src/pipeline/pipeline.ts +0 -35
- package/src/runtime/event-daemon.test.ts +0 -268
- package/src/runtime/event-daemon.ts +0 -531
- package/src/runtime/room-lobby-runtime.test.ts +0 -95
- package/src/runtime/room-lobby-runtime.ts +0 -367
- package/src/strategies/avoid-players.knowledge.md +0 -19
- package/src/strategies/kill-lone.knowledge.md +0 -17
- package/src/strategies/knowledge-contract.ts +0 -85
- package/src/strategies/loader.test.ts +0 -678
- package/src/strategies/loader.ts +0 -212
- package/src/strategies/new-events-backfill.ts +0 -98
- package/src/strategies/paradise-fish.knowledge.md +0 -19
- package/src/strategies/shrimp-memory.knowledge.md +0 -19
- package/src/strategies/social-task.test.ts +0 -28
- package/src/strategies/spawn.ts +0 -82
- package/src/strategies/strategy-loop.test.ts +0 -15
- package/src/strategies/strategy-loop.ts +0 -748
- package/src/strategies/warrior-memory.knowledge.md +0 -21
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventCatalogEntry,
|
|
3
|
+
EventDeliveryClass,
|
|
4
|
+
EventProducer,
|
|
5
|
+
} from './types.js';
|
|
6
|
+
|
|
7
|
+
export class EventProducerMismatchError extends Error {
|
|
8
|
+
constructor(
|
|
9
|
+
readonly eventType: string,
|
|
10
|
+
readonly expected: EventProducer,
|
|
11
|
+
readonly actual: EventProducer,
|
|
12
|
+
) {
|
|
13
|
+
super(`Event "${eventType}" must be produced by ${expected}, not ${actual}.`);
|
|
14
|
+
this.name = 'EventProducerMismatchError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export interface EventCatalogOptions {
|
|
18
|
+
unknownDeliveryClass?: EventDeliveryClass;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Game-specific policy registry. The Event Core remains unaware of concrete game
|
|
23
|
+
* event names; a game adapter owns the entries and can extend them at startup.
|
|
24
|
+
*/
|
|
25
|
+
export class EventCatalog {
|
|
26
|
+
private readonly entries = new Map<string, EventCatalogEntry>();
|
|
27
|
+
private readonly unknownDeliveryClass: EventDeliveryClass;
|
|
28
|
+
|
|
29
|
+
constructor(entries: Iterable<EventCatalogEntry> = [], opts: EventCatalogOptions = {}) {
|
|
30
|
+
this.unknownDeliveryClass = opts.unknownDeliveryClass ?? 'internal_only';
|
|
31
|
+
for (const entry of entries) this.register(entry);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
register(entry: EventCatalogEntry): this {
|
|
35
|
+
const existing = this.entries.get(entry.type);
|
|
36
|
+
if (existing && existing.producer !== entry.producer) {
|
|
37
|
+
throw new EventProducerMismatchError(entry.type, existing.producer, entry.producer);
|
|
38
|
+
}
|
|
39
|
+
this.entries.set(entry.type, { ...entry });
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get(type: string): EventCatalogEntry | undefined {
|
|
44
|
+
const entry = this.entries.get(type);
|
|
45
|
+
return entry ? { ...entry } : undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
resolve(type: string, producer: EventProducer): EventCatalogEntry {
|
|
49
|
+
const entry = this.entries.get(type);
|
|
50
|
+
if (!entry) {
|
|
51
|
+
return { type, producer, deliveryClass: this.unknownDeliveryClass };
|
|
52
|
+
}
|
|
53
|
+
if (entry.producer !== producer) {
|
|
54
|
+
throw new EventProducerMismatchError(type, entry.producer, producer);
|
|
55
|
+
}
|
|
56
|
+
return { ...entry };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
monitorAndEventsTypes(): Set<string> {
|
|
60
|
+
return new Set(
|
|
61
|
+
[...this.entries.values()]
|
|
62
|
+
.filter((entry) => entry.deliveryClass === 'monitor_and_events')
|
|
63
|
+
.map((entry) => entry.type),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
eventsOnlyTypes(): Set<string> {
|
|
68
|
+
return new Set(
|
|
69
|
+
[...this.entries.values()]
|
|
70
|
+
.filter((entry) => entry.deliveryClass === 'events_only')
|
|
71
|
+
.map((entry) => entry.type),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { DeliveryLeaseLostError } from './event-repository.js';
|
|
3
|
+
import { EventDeliveryService } from './event-delivery-service.js';
|
|
4
|
+
import { EventIngress } from './event-ingress.js';
|
|
5
|
+
import { MemoryEventRepository } from './memory-event-repository.js';
|
|
6
|
+
import { publishText, seedScope, testCatalog } from './test-helpers.js';
|
|
7
|
+
|
|
8
|
+
describe('EventDeliveryService', () => {
|
|
9
|
+
it('tracks monitor and detail delivery independently', async () => {
|
|
10
|
+
const repository = new MemoryEventRepository();
|
|
11
|
+
seedScope(repository);
|
|
12
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
13
|
+
await publishText(ingress, { eventId: 'event-1', sequence: 1 });
|
|
14
|
+
const service = new EventDeliveryService(repository);
|
|
15
|
+
|
|
16
|
+
const monitor = service.prepareMonitor('event-1', { ownerId: 'monitor', nowMs: 100 });
|
|
17
|
+
expect(monitor?.delivery).toMatchObject({
|
|
18
|
+
monitorState: 'pending',
|
|
19
|
+
monitorAttemptCount: 1,
|
|
20
|
+
});
|
|
21
|
+
expect(monitor?.ack('2026-07-15T00:01:00.000Z', 100)).toBe(true);
|
|
22
|
+
expect(repository.delivery('event-1', 'local-agent')).toMatchObject({
|
|
23
|
+
monitorState: 'notified',
|
|
24
|
+
detailState: 'pending',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const details = service.prepareDetails(['event-1'], 'stdout_full', {
|
|
28
|
+
ownerId: 'details',
|
|
29
|
+
nowMs: 101,
|
|
30
|
+
});
|
|
31
|
+
expect(details?.ack('2026-07-15T00:02:00.000Z', 101)).toBe(true);
|
|
32
|
+
expect(repository.delivery('event-1', 'local-agent')).toMatchObject({
|
|
33
|
+
monitorState: 'notified',
|
|
34
|
+
detailState: 'delivered',
|
|
35
|
+
detailDeliveryVia: 'stdout_full',
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('does not allow a second live owner and permits takeover after lease expiry', () => {
|
|
40
|
+
const repository = new MemoryEventRepository();
|
|
41
|
+
const first = repository.acquireLease({
|
|
42
|
+
consumerId: 'agent',
|
|
43
|
+
channel: 'details',
|
|
44
|
+
ownerId: 'owner-a',
|
|
45
|
+
ttlMs: 10,
|
|
46
|
+
nowMs: 100,
|
|
47
|
+
});
|
|
48
|
+
expect(first).not.toBeNull();
|
|
49
|
+
expect(repository.acquireLease({
|
|
50
|
+
consumerId: 'agent',
|
|
51
|
+
channel: 'details',
|
|
52
|
+
ownerId: 'owner-b',
|
|
53
|
+
ttlMs: 10,
|
|
54
|
+
nowMs: 105,
|
|
55
|
+
})).toBeNull();
|
|
56
|
+
|
|
57
|
+
const second = repository.acquireLease({
|
|
58
|
+
consumerId: 'agent',
|
|
59
|
+
channel: 'details',
|
|
60
|
+
ownerId: 'owner-b',
|
|
61
|
+
ttlMs: 10,
|
|
62
|
+
nowMs: 111,
|
|
63
|
+
});
|
|
64
|
+
expect(second).not.toBeNull();
|
|
65
|
+
expect(repository.leaseIsValid(first!, 111)).toBe(false);
|
|
66
|
+
expect(repository.leaseIsValid(second!, 111)).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects acknowledgement with an expired lease', async () => {
|
|
70
|
+
const repository = new MemoryEventRepository();
|
|
71
|
+
seedScope(repository);
|
|
72
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
73
|
+
await publishText(ingress, { eventId: 'event-1', sequence: 1 });
|
|
74
|
+
const service = new EventDeliveryService(repository);
|
|
75
|
+
const prepared = service.prepareDetails(['event-1'], 'ccl_events', {
|
|
76
|
+
ownerId: 'short-lease',
|
|
77
|
+
leaseTtlMs: 10,
|
|
78
|
+
nowMs: 100,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(() => prepared?.ack('2026-07-15T00:01:00.000Z', 111)).toThrow(DeliveryLeaseLostError);
|
|
82
|
+
expect(repository.delivery('event-1', 'local-agent')?.detailState).toBe('pending');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import type { EventRepository } from './event-repository.js';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_EVENT_CONSUMER_ID,
|
|
5
|
+
type CanonicalEvent,
|
|
6
|
+
type ConsumerLease,
|
|
7
|
+
type DetailDeliveryVia,
|
|
8
|
+
type EventDeliveryRecord,
|
|
9
|
+
} from './types.js';
|
|
10
|
+
|
|
11
|
+
export interface PrepareDeliveryOptions {
|
|
12
|
+
consumerId?: string;
|
|
13
|
+
ownerId?: string;
|
|
14
|
+
leaseTtlMs?: number;
|
|
15
|
+
nowMs?: number;
|
|
16
|
+
/** False only for a deliberate suppression; sink delivery defaults to true. */
|
|
17
|
+
recordAttempt?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PreparedMonitorDelivery {
|
|
21
|
+
event: CanonicalEvent;
|
|
22
|
+
delivery: EventDeliveryRecord;
|
|
23
|
+
lease: ConsumerLease;
|
|
24
|
+
/** Call only after the monitor text has been flushed successfully. */
|
|
25
|
+
ack(deliveredAt?: string, nowMs?: number): boolean;
|
|
26
|
+
suppress(nowMs?: number): boolean;
|
|
27
|
+
release(): boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PreparedDetailDelivery {
|
|
31
|
+
events: CanonicalEvent[];
|
|
32
|
+
lease: ConsumerLease;
|
|
33
|
+
/** Call only after every returned detail has been flushed successfully. */
|
|
34
|
+
ack(deliveredAt?: string, nowMs?: number): boolean;
|
|
35
|
+
release(): boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Coordinates push deliveries without coupling transport sinks to SQLite. */
|
|
39
|
+
export class EventDeliveryService {
|
|
40
|
+
constructor(private readonly repository: EventRepository) {}
|
|
41
|
+
|
|
42
|
+
prepareMonitor(
|
|
43
|
+
eventId: string,
|
|
44
|
+
opts: PrepareDeliveryOptions = {},
|
|
45
|
+
): PreparedMonitorDelivery | null {
|
|
46
|
+
const event = this.repository.eventById(eventId);
|
|
47
|
+
if (!event) return null;
|
|
48
|
+
const consumerId = opts.consumerId ?? DEFAULT_EVENT_CONSUMER_ID;
|
|
49
|
+
this.repository.ensureDeliveries(event.sessionId, consumerId);
|
|
50
|
+
const delivery = this.repository.delivery(eventId, consumerId);
|
|
51
|
+
if (!delivery || delivery.monitorState !== 'pending') return null;
|
|
52
|
+
const lease = this.repository.acquireLease({
|
|
53
|
+
consumerId,
|
|
54
|
+
channel: 'monitor',
|
|
55
|
+
ownerId: opts.ownerId ?? randomUUID(),
|
|
56
|
+
ttlMs: opts.leaseTtlMs ?? 30_000,
|
|
57
|
+
nowMs: opts.nowMs,
|
|
58
|
+
});
|
|
59
|
+
if (!lease) return null;
|
|
60
|
+
|
|
61
|
+
// State may change between the optimistic read and lease acquisition.
|
|
62
|
+
// Re-read under the lease so two sinks cannot both prepare the same push.
|
|
63
|
+
const leasedDelivery = this.repository.delivery(eventId, consumerId);
|
|
64
|
+
if (!leasedDelivery || leasedDelivery.monitorState !== 'pending') {
|
|
65
|
+
this.repository.releaseLease(lease);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
let attemptedDelivery = leasedDelivery;
|
|
69
|
+
if (opts.recordAttempt !== false) {
|
|
70
|
+
// Persist the attempt before invoking the sink. If the owner crashes
|
|
71
|
+
// between this point and acknowledgement, a new owner can distinguish a
|
|
72
|
+
// failed/in-flight delivery from history that was never sent.
|
|
73
|
+
const attemptedAt = new Date().toISOString();
|
|
74
|
+
if (!this.repository.recordMonitorAttempt({
|
|
75
|
+
eventId,
|
|
76
|
+
consumerId,
|
|
77
|
+
attemptedAt,
|
|
78
|
+
lease,
|
|
79
|
+
nowMs: opts.nowMs,
|
|
80
|
+
})) {
|
|
81
|
+
this.repository.releaseLease(lease);
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const recorded = this.repository.delivery(eventId, consumerId);
|
|
85
|
+
if (!recorded) {
|
|
86
|
+
this.repository.releaseLease(lease);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
attemptedDelivery = recorded;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let closed = false;
|
|
93
|
+
const close = (): boolean => {
|
|
94
|
+
if (closed) return false;
|
|
95
|
+
closed = true;
|
|
96
|
+
return this.repository.releaseLease(lease);
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
event,
|
|
100
|
+
delivery: attemptedDelivery,
|
|
101
|
+
lease,
|
|
102
|
+
ack: (deliveredAt = new Date().toISOString(), nowMs?: number): boolean => {
|
|
103
|
+
if (closed) return false;
|
|
104
|
+
const changed = this.repository.transitionMonitor({
|
|
105
|
+
eventIds: [eventId],
|
|
106
|
+
consumerId,
|
|
107
|
+
from: ['pending'],
|
|
108
|
+
to: 'notified',
|
|
109
|
+
deliveredAt,
|
|
110
|
+
lease,
|
|
111
|
+
nowMs,
|
|
112
|
+
});
|
|
113
|
+
close();
|
|
114
|
+
return changed === 1;
|
|
115
|
+
},
|
|
116
|
+
suppress: (nowMs?: number): boolean => {
|
|
117
|
+
if (closed) return false;
|
|
118
|
+
const changed = this.repository.transitionMonitor({
|
|
119
|
+
eventIds: [eventId],
|
|
120
|
+
consumerId,
|
|
121
|
+
from: ['pending'],
|
|
122
|
+
to: 'suppressed',
|
|
123
|
+
lease,
|
|
124
|
+
nowMs,
|
|
125
|
+
});
|
|
126
|
+
close();
|
|
127
|
+
return changed === 1;
|
|
128
|
+
},
|
|
129
|
+
release: close,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
prepareDetails(
|
|
134
|
+
eventIds: string[],
|
|
135
|
+
via: DetailDeliveryVia,
|
|
136
|
+
opts: PrepareDeliveryOptions = {},
|
|
137
|
+
): PreparedDetailDelivery | null {
|
|
138
|
+
const events = eventIds
|
|
139
|
+
.map((eventId) => this.repository.eventById(eventId))
|
|
140
|
+
.filter((event): event is CanonicalEvent => event !== null);
|
|
141
|
+
if (events.length === 0) return null;
|
|
142
|
+
const sessionId = events[0].sessionId;
|
|
143
|
+
if (events.some((event) => event.sessionId !== sessionId)) {
|
|
144
|
+
throw new Error('One detail delivery cannot span multiple sessions.');
|
|
145
|
+
}
|
|
146
|
+
const consumerId = opts.consumerId ?? DEFAULT_EVENT_CONSUMER_ID;
|
|
147
|
+
this.repository.ensureDeliveries(sessionId, consumerId);
|
|
148
|
+
const pending = events.filter((event) => {
|
|
149
|
+
const state = this.repository.delivery(event.eventId, consumerId)?.detailState;
|
|
150
|
+
return state === 'pending' || state === 'deferred';
|
|
151
|
+
});
|
|
152
|
+
if (pending.length === 0) return null;
|
|
153
|
+
const lease = this.repository.acquireLease({
|
|
154
|
+
consumerId,
|
|
155
|
+
channel: 'details',
|
|
156
|
+
ownerId: opts.ownerId ?? randomUUID(),
|
|
157
|
+
ttlMs: opts.leaseTtlMs ?? 30_000,
|
|
158
|
+
nowMs: opts.nowMs,
|
|
159
|
+
});
|
|
160
|
+
if (!lease) return null;
|
|
161
|
+
|
|
162
|
+
const leasedPending = pending.filter((event) => {
|
|
163
|
+
const state = this.repository.delivery(event.eventId, consumerId)?.detailState;
|
|
164
|
+
return state === 'pending' || state === 'deferred';
|
|
165
|
+
});
|
|
166
|
+
if (leasedPending.length === 0) {
|
|
167
|
+
this.repository.releaseLease(lease);
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let closed = false;
|
|
172
|
+
const close = (): boolean => {
|
|
173
|
+
if (closed) return false;
|
|
174
|
+
closed = true;
|
|
175
|
+
return this.repository.releaseLease(lease);
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
events: leasedPending,
|
|
179
|
+
lease,
|
|
180
|
+
ack: (deliveredAt = new Date().toISOString(), nowMs?: number): boolean => {
|
|
181
|
+
if (closed) return false;
|
|
182
|
+
const changed = this.repository.transitionDetails({
|
|
183
|
+
eventIds: leasedPending.map((event) => event.eventId),
|
|
184
|
+
consumerId,
|
|
185
|
+
from: ['pending', 'deferred'],
|
|
186
|
+
to: 'delivered',
|
|
187
|
+
deliveredAt,
|
|
188
|
+
deliveryVia: via,
|
|
189
|
+
lease,
|
|
190
|
+
nowMs,
|
|
191
|
+
});
|
|
192
|
+
close();
|
|
193
|
+
return changed === leasedPending.length;
|
|
194
|
+
},
|
|
195
|
+
release: close,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { EventProducerMismatchError } from './event-catalog.js';
|
|
3
|
+
import { EventIngress } from './event-ingress.js';
|
|
4
|
+
import { MemoryEventRepository } from './memory-event-repository.js';
|
|
5
|
+
import { seedScope, testCatalog, TEST_TIME } from './test-helpers.js';
|
|
6
|
+
|
|
7
|
+
describe('EventIngress', () => {
|
|
8
|
+
it('deduplicates remote identities after the durable commit', async () => {
|
|
9
|
+
const repository = new MemoryEventRepository();
|
|
10
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
11
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
12
|
+
const first = await ingress.publish({
|
|
13
|
+
eventId: 'local-event-1',
|
|
14
|
+
type: 'speech',
|
|
15
|
+
producer: 'backend_push',
|
|
16
|
+
sessionId,
|
|
17
|
+
segmentId,
|
|
18
|
+
receivedAt: TEST_TIME,
|
|
19
|
+
payload: { text: 'hello' },
|
|
20
|
+
source: {
|
|
21
|
+
sourceId: 'game-stream',
|
|
22
|
+
eventId: 'remote-event-7',
|
|
23
|
+
sequence: 7,
|
|
24
|
+
checkpoint: 'resume-at-7',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
const duplicateById = await ingress.publish({
|
|
28
|
+
eventId: 'local-event-2',
|
|
29
|
+
type: 'speech',
|
|
30
|
+
producer: 'backend_push',
|
|
31
|
+
sessionId,
|
|
32
|
+
segmentId,
|
|
33
|
+
payload: { text: 'retry by id' },
|
|
34
|
+
source: { sourceId: 'game-stream', eventId: 'remote-event-7', sequence: 8 },
|
|
35
|
+
});
|
|
36
|
+
const duplicateBySequence = await ingress.publish({
|
|
37
|
+
eventId: 'local-event-3',
|
|
38
|
+
type: 'speech',
|
|
39
|
+
producer: 'backend_push',
|
|
40
|
+
sessionId,
|
|
41
|
+
segmentId,
|
|
42
|
+
payload: { text: 'retry by sequence' },
|
|
43
|
+
source: { sourceId: 'game-stream', eventId: 'another-id', sequence: 7 },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(first.inserted).toBe(true);
|
|
47
|
+
expect(duplicateById).toMatchObject({ inserted: false, event: { eventId: 'local-event-1' } });
|
|
48
|
+
expect(duplicateBySequence).toMatchObject({ inserted: false, event: { eventId: 'local-event-1' } });
|
|
49
|
+
expect(repository.sourceCheckpoint('game-stream')).toMatchObject({
|
|
50
|
+
checkpoint: 'resume-at-7',
|
|
51
|
+
eventId: 'local-event-1',
|
|
52
|
+
sourceSequence: '7',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('stores an immutable copy and strips backend presentation fields', async () => {
|
|
57
|
+
const repository = new MemoryEventRepository();
|
|
58
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
59
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
60
|
+
const payload = {
|
|
61
|
+
text: 'fact',
|
|
62
|
+
hint: 'backend-owned hint',
|
|
63
|
+
next_step: 'backend-owned next step',
|
|
64
|
+
notice: 'backend-owned notice',
|
|
65
|
+
nested: { still: 'a fact' },
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const result = await ingress.publish({
|
|
69
|
+
eventId: 'immutable-event',
|
|
70
|
+
type: 'speech',
|
|
71
|
+
producer: 'backend_push',
|
|
72
|
+
sessionId,
|
|
73
|
+
segmentId,
|
|
74
|
+
payload,
|
|
75
|
+
source: { sourceId: 'game-stream', eventId: 'immutable-remote' },
|
|
76
|
+
});
|
|
77
|
+
payload.text = 'mutated input';
|
|
78
|
+
payload.nested.still = 'mutated nested input';
|
|
79
|
+
result.event.payload.text = 'mutated result';
|
|
80
|
+
|
|
81
|
+
expect(repository.eventById('immutable-event')?.payload).toEqual({
|
|
82
|
+
text: 'fact',
|
|
83
|
+
nested: { still: 'a fact' },
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('also strips legacy presentation fields from local synthesized facts', async () => {
|
|
88
|
+
const repository = new MemoryEventRepository();
|
|
89
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
90
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
91
|
+
const result = await ingress.publish({
|
|
92
|
+
eventId: 'local-fact',
|
|
93
|
+
type: 'client_notice',
|
|
94
|
+
producer: 'local_synth',
|
|
95
|
+
sessionId,
|
|
96
|
+
segmentId,
|
|
97
|
+
payload: {
|
|
98
|
+
reason: 'scope_changed',
|
|
99
|
+
hint: 'old hint',
|
|
100
|
+
next_step: 'old next step',
|
|
101
|
+
notice: 'old notice',
|
|
102
|
+
},
|
|
103
|
+
source: { sourceId: 'local-runtime' },
|
|
104
|
+
});
|
|
105
|
+
expect(result.event.payload).toEqual({ reason: 'scope_changed' });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('allows a transport sequence to restart in a new segment', async () => {
|
|
109
|
+
const repository = new MemoryEventRepository();
|
|
110
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
111
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
112
|
+
const first = await ingress.publish({
|
|
113
|
+
eventId: 'segment-1-event',
|
|
114
|
+
type: 'speech',
|
|
115
|
+
producer: 'backend_push',
|
|
116
|
+
sessionId,
|
|
117
|
+
segmentId,
|
|
118
|
+
source: { sourceId: 'udp-broadcast', sequence: 1 },
|
|
119
|
+
});
|
|
120
|
+
repository.saveSegment({
|
|
121
|
+
segmentId: 'segment-2',
|
|
122
|
+
sessionId,
|
|
123
|
+
sequence: 2,
|
|
124
|
+
kind: 'reconnect',
|
|
125
|
+
status: 'open',
|
|
126
|
+
isCurrent: true,
|
|
127
|
+
openedAt: TEST_TIME,
|
|
128
|
+
});
|
|
129
|
+
const second = await ingress.publish({
|
|
130
|
+
eventId: 'segment-2-event',
|
|
131
|
+
type: 'speech',
|
|
132
|
+
producer: 'backend_push',
|
|
133
|
+
sessionId,
|
|
134
|
+
segmentId: 'segment-2',
|
|
135
|
+
source: { sourceId: 'udp-broadcast', sequence: 1 },
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(first.inserted).toBe(true);
|
|
139
|
+
expect(second.inserted).toBe(true);
|
|
140
|
+
expect(repository.delivery('segment-1-event', 'local-agent')).toMatchObject({
|
|
141
|
+
detailState: 'deferred',
|
|
142
|
+
monitorState: 'pending',
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('does not advance a checkpoint when commit fails', async () => {
|
|
147
|
+
const repository = new MemoryEventRepository();
|
|
148
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
149
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
150
|
+
vi.spyOn(repository, 'commitEvent').mockImplementation(() => {
|
|
151
|
+
throw new Error('disk full');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
await expect(ingress.publish({
|
|
155
|
+
type: 'speech',
|
|
156
|
+
producer: 'backend_push',
|
|
157
|
+
sessionId,
|
|
158
|
+
segmentId,
|
|
159
|
+
payload: { text: 'not committed' },
|
|
160
|
+
source: { sourceId: 'game-stream', sequence: 9, checkpoint: 'resume-at-9' },
|
|
161
|
+
})).rejects.toThrow('disk full');
|
|
162
|
+
expect(repository.sourceCheckpoint('game-stream')).toBeNull();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('rejects a producer that does not own a registered event type', async () => {
|
|
166
|
+
const repository = new MemoryEventRepository();
|
|
167
|
+
const { sessionId, segmentId } = seedScope(repository);
|
|
168
|
+
const ingress = new EventIngress(repository, testCatalog());
|
|
169
|
+
|
|
170
|
+
await expect(ingress.publish({
|
|
171
|
+
type: 'speech',
|
|
172
|
+
producer: 'local_synth',
|
|
173
|
+
sessionId,
|
|
174
|
+
segmentId,
|
|
175
|
+
source: { sourceId: 'client' },
|
|
176
|
+
})).rejects.toBeInstanceOf(EventProducerMismatchError);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { EventCatalog } from './event-catalog.js';
|
|
3
|
+
import type { EventRepository } from './event-repository.js';
|
|
4
|
+
import {
|
|
5
|
+
cloneRecord,
|
|
6
|
+
DEFAULT_EVENT_CONSUMER_ID,
|
|
7
|
+
type CanonicalEventDraft,
|
|
8
|
+
type CommitEventResult,
|
|
9
|
+
type PublishEventInput,
|
|
10
|
+
type SourceCheckpointRecord,
|
|
11
|
+
} from './types.js';
|
|
12
|
+
|
|
13
|
+
export interface EventIngressOptions {
|
|
14
|
+
consumerIds?: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizeSourceSequence(sequence: string | number | undefined): string | undefined {
|
|
18
|
+
if (sequence === undefined) return undefined;
|
|
19
|
+
return String(sequence);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function factsOnlyPayload(input: PublishEventInput): Record<string, unknown> {
|
|
23
|
+
const payload = cloneRecord(input.payload ?? {});
|
|
24
|
+
// These legacy presentation fields are not Event facts. Strip them at the
|
|
25
|
+
// canonical write boundary; the ClawClaw Mailbox may later add its own
|
|
26
|
+
// optional `hint` while projecting an Event for an Agent.
|
|
27
|
+
delete payload.hint;
|
|
28
|
+
delete payload.next_step;
|
|
29
|
+
delete payload.notice;
|
|
30
|
+
return payload;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The sole write entrance for canonical game events. */
|
|
34
|
+
export class EventIngress {
|
|
35
|
+
private readonly consumerIds: string[];
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly repository: EventRepository,
|
|
39
|
+
private readonly catalog: EventCatalog,
|
|
40
|
+
opts: EventIngressOptions = {},
|
|
41
|
+
) {
|
|
42
|
+
this.consumerIds = [...new Set(opts.consumerIds ?? [DEFAULT_EVENT_CONSUMER_ID])];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async publish(input: PublishEventInput): Promise<CommitEventResult> {
|
|
46
|
+
const prepared = this.prepare(input);
|
|
47
|
+
return this.repository.commitEvent({
|
|
48
|
+
event: prepared.event,
|
|
49
|
+
consumerIds: this.consumerIds,
|
|
50
|
+
checkpoint: prepared.checkpoint,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Build the canonical immutable draft without making it externally visible. */
|
|
55
|
+
prepare(input: PublishEventInput): {
|
|
56
|
+
event: CanonicalEventDraft;
|
|
57
|
+
checkpoint?: SourceCheckpointRecord;
|
|
58
|
+
} {
|
|
59
|
+
const policy = this.catalog.resolve(input.type, input.producer);
|
|
60
|
+
const receivedAt = input.receivedAt ?? new Date().toISOString();
|
|
61
|
+
const eventId = input.eventId ?? randomUUID();
|
|
62
|
+
const draft: CanonicalEventDraft = {
|
|
63
|
+
...(input.epochSeq === undefined ? {} : { epochSeq: input.epochSeq }),
|
|
64
|
+
eventId,
|
|
65
|
+
sessionId: input.sessionId,
|
|
66
|
+
segmentId: input.segmentId,
|
|
67
|
+
type: input.type,
|
|
68
|
+
producer: input.producer,
|
|
69
|
+
...(input.producerPluginId === undefined
|
|
70
|
+
? {}
|
|
71
|
+
: { producerPluginId: input.producerPluginId }),
|
|
72
|
+
...(input.producerOutputIndex === undefined
|
|
73
|
+
? {}
|
|
74
|
+
: { producerOutputIndex: input.producerOutputIndex }),
|
|
75
|
+
deliveryClass: policy.deliveryClass,
|
|
76
|
+
occurredAt: input.occurredAt ?? receivedAt,
|
|
77
|
+
receivedAt,
|
|
78
|
+
payload: factsOnlyPayload(input),
|
|
79
|
+
sourceId: input.source.sourceId,
|
|
80
|
+
sourceEventId: input.source.eventId,
|
|
81
|
+
sourceSequence: normalizeSourceSequence(input.source.sequence),
|
|
82
|
+
connectionId: input.source.connectionId,
|
|
83
|
+
replayed: input.source.replayed === true,
|
|
84
|
+
};
|
|
85
|
+
const checkpoint: SourceCheckpointRecord | undefined = input.source.checkpoint
|
|
86
|
+
? {
|
|
87
|
+
sourceId: input.source.sourceId,
|
|
88
|
+
checkpoint: input.source.checkpoint,
|
|
89
|
+
sourceSequence: draft.sourceSequence,
|
|
90
|
+
eventId,
|
|
91
|
+
committedAt: receivedAt,
|
|
92
|
+
}
|
|
93
|
+
: undefined;
|
|
94
|
+
|
|
95
|
+
return { event: draft, ...(checkpoint ? { checkpoint } : {}) };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AcquireLeaseRequest,
|
|
3
|
+
BacklogSummary,
|
|
4
|
+
CanonicalEvent,
|
|
5
|
+
CommittedEventListFilter,
|
|
6
|
+
CommitEventRequest,
|
|
7
|
+
CommitEventResult,
|
|
8
|
+
ConsumerLease,
|
|
9
|
+
CurrentEventScope,
|
|
10
|
+
DetailStateTransition,
|
|
11
|
+
EventAccountScope,
|
|
12
|
+
EventDeliveryRecord,
|
|
13
|
+
EventListFilter,
|
|
14
|
+
EventSegmentRecord,
|
|
15
|
+
EventWithDelivery,
|
|
16
|
+
LatestEventScopeOptions,
|
|
17
|
+
MonitorDeliveryAttempt,
|
|
18
|
+
MonitorStateTransition,
|
|
19
|
+
PlaySessionRecord,
|
|
20
|
+
SourceCheckpointRecord,
|
|
21
|
+
} from './types.js';
|
|
22
|
+
|
|
23
|
+
export interface EventRepository {
|
|
24
|
+
saveSession(session: PlaySessionRecord): void;
|
|
25
|
+
saveSegment(segment: EventSegmentRecord): void;
|
|
26
|
+
currentScope(account?: EventAccountScope): CurrentEventScope | null;
|
|
27
|
+
/** Current session/segment when possible, otherwise the newest durable scope. */
|
|
28
|
+
latestScope(account: EventAccountScope, options?: LatestEventScopeOptions): CurrentEventScope | null;
|
|
29
|
+
|
|
30
|
+
commitEvent(request: CommitEventRequest): CommitEventResult;
|
|
31
|
+
eventById(eventId: string): CanonicalEvent | null;
|
|
32
|
+
latestEventByType(sessionId: string, type: string, segmentId?: string): CanonicalEvent | null;
|
|
33
|
+
highWaterCommitSeq(sessionId: string, segmentId?: string): number;
|
|
34
|
+
/** Lists committed facts without creating or mutating a consumer delivery row. */
|
|
35
|
+
listCommittedEvents(filter: CommittedEventListFilter): CanonicalEvent[];
|
|
36
|
+
|
|
37
|
+
ensureDeliveries(sessionId: string, consumerId: string): void;
|
|
38
|
+
delivery(eventId: string, consumerId: string): EventDeliveryRecord | null;
|
|
39
|
+
listEvents(filter: EventListFilter, consumerId: string): EventWithDelivery[];
|
|
40
|
+
backlogSummary(sessionId: string, consumerId: string): BacklogSummary;
|
|
41
|
+
|
|
42
|
+
transitionDetails(transition: DetailStateTransition): number;
|
|
43
|
+
transitionMonitor(transition: MonitorStateTransition): number;
|
|
44
|
+
recordMonitorAttempt(attempt: MonitorDeliveryAttempt): boolean;
|
|
45
|
+
|
|
46
|
+
acquireLease(request: AcquireLeaseRequest): ConsumerLease | null;
|
|
47
|
+
leaseIsValid(lease: ConsumerLease, nowMs?: number): boolean;
|
|
48
|
+
releaseLease(lease: ConsumerLease): boolean;
|
|
49
|
+
|
|
50
|
+
sourceCheckpoint(sourceId: string): SourceCheckpointRecord | null;
|
|
51
|
+
close?(): void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class DeliveryLeaseLostError extends Error {
|
|
55
|
+
constructor(readonly lease: ConsumerLease) {
|
|
56
|
+
super(`The ${lease.channel} delivery lease for ${lease.consumerId} is no longer owned by ${lease.ownerId}.`);
|
|
57
|
+
this.name = 'DeliveryLeaseLostError';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './event-catalog.js';
|
|
2
|
+
export * from './event-delivery-service.js';
|
|
3
|
+
export * from './event-ingress.js';
|
|
4
|
+
export * from './event-repository.js';
|
|
5
|
+
export * from './memory-event-repository.js';
|
|
6
|
+
export * from './sqlite-event-repository.js';
|
|
7
|
+
export * from './types.js';
|