@myclaw163/clawclaw-cli 0.6.112 → 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 +25 -8
- 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.test.ts +2 -23
- package/src/commands/hub.ts +4 -16
- package/src/commands/load.test.ts +10 -22
- 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 -102
- package/src/commands/strategy.ts +175 -103
- 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 +110 -81
- 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 -710
- package/src/strategies/loader.ts +0 -183
- package/src/strategies/new-events-backfill.ts +0 -98
- package/src/strategies/paradise-fish.knowledge.md +0 -19
- package/src/strategies/scanner.ts +0 -238
- 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,928 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import type { MailboxRepository } from '../mailbox/mailbox-repository.js';
|
|
3
|
+
import type {
|
|
4
|
+
AckMailboxClaimRequest,
|
|
5
|
+
AckMailboxClaimResult,
|
|
6
|
+
ClaimMailboxItemsRequest,
|
|
7
|
+
EnqueueMailboxItem,
|
|
8
|
+
EnqueueMailboxResult,
|
|
9
|
+
MailboxClaim,
|
|
10
|
+
MailboxDeliveryMode,
|
|
11
|
+
MailboxItem,
|
|
12
|
+
MailboxPeekFilter,
|
|
13
|
+
NackMailboxClaimRequest,
|
|
14
|
+
NackMailboxClaimResult,
|
|
15
|
+
SuppressMailboxItemsFilter,
|
|
16
|
+
} from '../mailbox/types.js';
|
|
17
|
+
import { DeliveryLeaseLostError, type EventRepository } from './event-repository.js';
|
|
18
|
+
import {
|
|
19
|
+
cloneRecord,
|
|
20
|
+
initialDeliveryFor,
|
|
21
|
+
type AcquireLeaseRequest,
|
|
22
|
+
type BacklogSummary,
|
|
23
|
+
type CanonicalEvent,
|
|
24
|
+
type CommittedEventListFilter,
|
|
25
|
+
type CommitEventRequest,
|
|
26
|
+
type CommitEventResult,
|
|
27
|
+
type ConsumerLease,
|
|
28
|
+
type CurrentEventScope,
|
|
29
|
+
type DetailStateTransition,
|
|
30
|
+
type EventAccountScope,
|
|
31
|
+
type EventDeliveryRecord,
|
|
32
|
+
type EventListFilter,
|
|
33
|
+
type EventSegmentRecord,
|
|
34
|
+
type EventWithDelivery,
|
|
35
|
+
type LatestEventScopeOptions,
|
|
36
|
+
type MonitorDeliveryAttempt,
|
|
37
|
+
type MonitorStateTransition,
|
|
38
|
+
type PlaySessionRecord,
|
|
39
|
+
type SourceCheckpointRecord,
|
|
40
|
+
} from './types.js';
|
|
41
|
+
|
|
42
|
+
function deliveryKey(eventId: string, consumerId: string): string {
|
|
43
|
+
return `${eventId}\u0000${consumerId}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function leaseKey(consumerId: string, channel: ConsumerLease['channel']): string {
|
|
47
|
+
return `${consumerId}\u0000${channel}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function sourceEventKey(sourceId: string, eventId: string): string {
|
|
51
|
+
return `${sourceId}\u0000id\u0000${eventId}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function sourceSequenceKey(
|
|
55
|
+
sourceId: string,
|
|
56
|
+
sessionId: string,
|
|
57
|
+
segmentId: string,
|
|
58
|
+
sequence: string,
|
|
59
|
+
): string {
|
|
60
|
+
return `${sourceId}\u0000session\u0000${sessionId}\u0000segment\u0000${segmentId}\u0000seq\u0000${sequence}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function mailboxUniqueKey(consumerId: string, eventId: string, mode: MailboxDeliveryMode): string {
|
|
64
|
+
return `${consumerId}\u0000${eventId}\u0000${mode}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Fast deterministic repository for tests, embedding, and non-persistent hosts. */
|
|
68
|
+
export class MemoryEventRepository implements EventRepository, MailboxRepository {
|
|
69
|
+
private readonly sessions = new Map<string, PlaySessionRecord>();
|
|
70
|
+
private readonly segments = new Map<string, EventSegmentRecord>();
|
|
71
|
+
private readonly events = new Map<string, CanonicalEvent>();
|
|
72
|
+
private readonly eventOrder: string[] = [];
|
|
73
|
+
private readonly deliveries = new Map<string, EventDeliveryRecord>();
|
|
74
|
+
private readonly checkpoints = new Map<string, SourceCheckpointRecord>();
|
|
75
|
+
private readonly leases = new Map<string, ConsumerLease>();
|
|
76
|
+
private readonly remoteEventIds = new Map<string, string>();
|
|
77
|
+
private readonly remoteSequences = new Map<string, string>();
|
|
78
|
+
private readonly mailboxItems = new Map<string, MailboxItem>();
|
|
79
|
+
private readonly mailboxUniqueItems = new Map<string, string>();
|
|
80
|
+
private nextCommitSeq = 1;
|
|
81
|
+
|
|
82
|
+
saveSession(session: PlaySessionRecord): void {
|
|
83
|
+
const existingSession = this.sessions.get(session.sessionId);
|
|
84
|
+
if (existingSession?.isCurrent && !session.isCurrent) {
|
|
85
|
+
this.retireMailboxScope(
|
|
86
|
+
(item) => item.sessionId === session.sessionId,
|
|
87
|
+
(event) => event.sessionId === session.sessionId,
|
|
88
|
+
session.sealedAt ?? session.openedAt,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (session.isCurrent) {
|
|
92
|
+
const previousSessionIds = new Set(
|
|
93
|
+
[...this.sessions.values()]
|
|
94
|
+
.filter((existing) => (
|
|
95
|
+
existing.isCurrent
|
|
96
|
+
&& existing.adapterId === session.adapterId
|
|
97
|
+
&& existing.accountId === session.accountId
|
|
98
|
+
&& existing.sessionId !== session.sessionId
|
|
99
|
+
))
|
|
100
|
+
.map((existing) => existing.sessionId),
|
|
101
|
+
);
|
|
102
|
+
if (previousSessionIds.size > 0) {
|
|
103
|
+
for (const [itemId, item] of this.mailboxItems) {
|
|
104
|
+
if (previousSessionIds.has(item.sessionId) && item.state === 'pending') {
|
|
105
|
+
this.mailboxItems.set(itemId, {
|
|
106
|
+
...item,
|
|
107
|
+
state: 'suppressed',
|
|
108
|
+
updatedAt: session.openedAt,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const event of this.events.values()) {
|
|
113
|
+
if (!previousSessionIds.has(event.sessionId)) continue;
|
|
114
|
+
for (const [key, delivery] of this.deliveries) {
|
|
115
|
+
if (delivery.eventId !== event.eventId) continue;
|
|
116
|
+
const claimedMailbox = [...this.mailboxItems.values()].some((item) => (
|
|
117
|
+
item.eventId === event.eventId
|
|
118
|
+
&& item.consumerId === delivery.consumerId
|
|
119
|
+
&& item.state === 'claimed'
|
|
120
|
+
));
|
|
121
|
+
this.deliveries.set(key, {
|
|
122
|
+
...delivery,
|
|
123
|
+
...(delivery.detailState === 'pending' ? { detailState: 'deferred' as const } : {}),
|
|
124
|
+
...(delivery.monitorState === 'pending' && !claimedMailbox
|
|
125
|
+
? { monitorState: 'suppressed' as const }
|
|
126
|
+
: {}),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
for (const [id, existing] of this.sessions) {
|
|
132
|
+
if (
|
|
133
|
+
existing.isCurrent
|
|
134
|
+
&& existing.adapterId === session.adapterId
|
|
135
|
+
&& existing.accountId === session.accountId
|
|
136
|
+
&& id !== session.sessionId
|
|
137
|
+
) {
|
|
138
|
+
this.sessions.set(id, { ...existing, isCurrent: false });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this.sessions.set(session.sessionId, cloneRecord(session));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
saveSegment(segment: EventSegmentRecord): void {
|
|
146
|
+
if (!this.sessions.has(segment.sessionId)) {
|
|
147
|
+
throw new Error(`Unknown event session: ${segment.sessionId}`);
|
|
148
|
+
}
|
|
149
|
+
const existingSegment = this.segments.get(segment.segmentId);
|
|
150
|
+
if (existingSegment?.isCurrent && !segment.isCurrent) {
|
|
151
|
+
this.carrySegmentsForward(
|
|
152
|
+
new Set([segment.segmentId]),
|
|
153
|
+
segment.closedAt ?? segment.openedAt,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (segment.isCurrent) {
|
|
157
|
+
const previousSegmentIds = new Set(
|
|
158
|
+
[...this.segments.values()]
|
|
159
|
+
.filter((existing) => (
|
|
160
|
+
existing.sessionId === segment.sessionId
|
|
161
|
+
&& existing.isCurrent
|
|
162
|
+
&& existing.segmentId !== segment.segmentId
|
|
163
|
+
))
|
|
164
|
+
.map((existing) => existing.segmentId),
|
|
165
|
+
);
|
|
166
|
+
if (previousSegmentIds.size > 0) {
|
|
167
|
+
this.carrySegmentsForward(previousSegmentIds, segment.openedAt);
|
|
168
|
+
}
|
|
169
|
+
for (const [id, existing] of this.segments) {
|
|
170
|
+
if (existing.sessionId === segment.sessionId && existing.isCurrent && id !== segment.segmentId) {
|
|
171
|
+
this.segments.set(id, { ...existing, isCurrent: false });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
this.segments.set(segment.segmentId, cloneRecord(segment));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
currentScope(account?: EventAccountScope): CurrentEventScope | null {
|
|
179
|
+
const sessions = [...this.sessions.values()].filter((item) => (
|
|
180
|
+
item.isCurrent
|
|
181
|
+
&& (!account || (item.adapterId === account.adapterId && item.accountId === account.accountId))
|
|
182
|
+
));
|
|
183
|
+
if (sessions.length > 1) {
|
|
184
|
+
throw new Error('Multiple current event sessions exist; provide adapterId and accountId.');
|
|
185
|
+
}
|
|
186
|
+
const session = sessions[0];
|
|
187
|
+
if (!session) return null;
|
|
188
|
+
const segment = [...this.segments.values()].find(
|
|
189
|
+
(item) => item.sessionId === session.sessionId && item.isCurrent,
|
|
190
|
+
);
|
|
191
|
+
if (!segment) return null;
|
|
192
|
+
return { session: cloneRecord(session), segment: cloneRecord(segment) };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
latestScope(account: EventAccountScope, options: LatestEventScopeOptions = {}): CurrentEventScope | null {
|
|
196
|
+
const sessions = [...this.sessions.values()]
|
|
197
|
+
.filter((item) => item.adapterId === account.adapterId && item.accountId === account.accountId)
|
|
198
|
+
.sort((a, b) => {
|
|
199
|
+
if (a.isCurrent !== b.isCurrent) return a.isCurrent ? -1 : 1;
|
|
200
|
+
return b.openedAt.localeCompare(a.openedAt);
|
|
201
|
+
});
|
|
202
|
+
const session = sessions[0];
|
|
203
|
+
if (!session) return null;
|
|
204
|
+
const allowedKinds = options.segmentKinds?.length ? new Set(options.segmentKinds) : null;
|
|
205
|
+
const segments = [...this.segments.values()]
|
|
206
|
+
.filter((item) => item.sessionId === session.sessionId && (!allowedKinds || allowedKinds.has(item.kind)))
|
|
207
|
+
.sort((a, b) => {
|
|
208
|
+
if (a.isCurrent !== b.isCurrent) return a.isCurrent ? -1 : 1;
|
|
209
|
+
return b.sequence - a.sequence;
|
|
210
|
+
});
|
|
211
|
+
const segment = segments[0];
|
|
212
|
+
return segment ? { session: cloneRecord(session), segment: cloneRecord(segment) } : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
commitEvent(request: CommitEventRequest): CommitEventResult {
|
|
216
|
+
const draft = cloneRecord(request.event);
|
|
217
|
+
if (!this.sessions.has(draft.sessionId)) {
|
|
218
|
+
throw new Error(`Unknown event session: ${draft.sessionId}`);
|
|
219
|
+
}
|
|
220
|
+
const segment = this.segments.get(draft.segmentId);
|
|
221
|
+
if (!segment || segment.sessionId !== draft.sessionId) {
|
|
222
|
+
throw new Error(`Unknown event segment ${draft.segmentId} in session ${draft.sessionId}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const duplicateId = draft.sourceEventId
|
|
226
|
+
? this.remoteEventIds.get(sourceEventKey(draft.sourceId, draft.sourceEventId))
|
|
227
|
+
: undefined;
|
|
228
|
+
const duplicateSequence = draft.sourceSequence
|
|
229
|
+
? this.remoteSequences.get(sourceSequenceKey(
|
|
230
|
+
draft.sourceId,
|
|
231
|
+
draft.sessionId,
|
|
232
|
+
draft.segmentId,
|
|
233
|
+
draft.sourceSequence,
|
|
234
|
+
))
|
|
235
|
+
: undefined;
|
|
236
|
+
if (duplicateId && duplicateSequence && duplicateId !== duplicateSequence) {
|
|
237
|
+
throw new Error('Conflicting source event ID and source sequence identities.');
|
|
238
|
+
}
|
|
239
|
+
const duplicateEventId = duplicateId ?? duplicateSequence;
|
|
240
|
+
if (duplicateEventId) {
|
|
241
|
+
const existing = this.events.get(duplicateEventId);
|
|
242
|
+
if (!existing) throw new Error(`Corrupt event identity index: ${duplicateEventId}`);
|
|
243
|
+
for (const consumerId of new Set(request.consumerIds)) {
|
|
244
|
+
const key = deliveryKey(existing.eventId, consumerId);
|
|
245
|
+
if (!this.deliveries.has(key)) {
|
|
246
|
+
this.deliveries.set(key, this.initialDelivery(existing, consumerId));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return { inserted: false, event: cloneRecord(existing) };
|
|
250
|
+
}
|
|
251
|
+
if (this.events.has(draft.eventId)) {
|
|
252
|
+
throw new Error(`Event ID already exists: ${draft.eventId}`);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const event: CanonicalEvent = {
|
|
256
|
+
...draft,
|
|
257
|
+
payload: cloneRecord(draft.payload),
|
|
258
|
+
commitSeq: this.nextCommitSeq++,
|
|
259
|
+
};
|
|
260
|
+
this.events.set(event.eventId, event);
|
|
261
|
+
this.eventOrder.push(event.eventId);
|
|
262
|
+
if (event.sourceEventId) {
|
|
263
|
+
this.remoteEventIds.set(sourceEventKey(event.sourceId, event.sourceEventId), event.eventId);
|
|
264
|
+
}
|
|
265
|
+
if (event.sourceSequence) {
|
|
266
|
+
this.remoteSequences.set(sourceSequenceKey(
|
|
267
|
+
event.sourceId,
|
|
268
|
+
event.sessionId,
|
|
269
|
+
event.segmentId,
|
|
270
|
+
event.sourceSequence,
|
|
271
|
+
), event.eventId);
|
|
272
|
+
}
|
|
273
|
+
for (const consumerId of new Set(request.consumerIds)) {
|
|
274
|
+
this.deliveries.set(
|
|
275
|
+
deliveryKey(event.eventId, consumerId),
|
|
276
|
+
this.initialDelivery(event, consumerId),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
if (request.checkpoint) {
|
|
280
|
+
this.checkpoints.set(request.checkpoint.sourceId, cloneRecord(request.checkpoint));
|
|
281
|
+
}
|
|
282
|
+
return { inserted: true, event: cloneRecord(event) };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
eventById(eventId: string): CanonicalEvent | null {
|
|
286
|
+
const event = this.events.get(eventId);
|
|
287
|
+
return event ? cloneRecord(event) : null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
latestEventByType(sessionId: string, type: string, segmentId?: string): CanonicalEvent | null {
|
|
291
|
+
for (let i = this.eventOrder.length - 1; i >= 0; i -= 1) {
|
|
292
|
+
const event = this.events.get(this.eventOrder[i]);
|
|
293
|
+
if (
|
|
294
|
+
event?.sessionId === sessionId
|
|
295
|
+
&& event.type === type
|
|
296
|
+
&& (!segmentId || event.segmentId === segmentId)
|
|
297
|
+
) return cloneRecord(event);
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
highWaterCommitSeq(sessionId: string, segmentId?: string): number {
|
|
303
|
+
let highWater = 0;
|
|
304
|
+
for (const event of this.events.values()) {
|
|
305
|
+
if (event.sessionId !== sessionId) continue;
|
|
306
|
+
if (segmentId && event.segmentId !== segmentId) continue;
|
|
307
|
+
highWater = Math.max(highWater, event.commitSeq);
|
|
308
|
+
}
|
|
309
|
+
return highWater;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
listCommittedEvents(filter: CommittedEventListFilter): CanonicalEvent[] {
|
|
313
|
+
const types = filter.types ? new Set(filter.types) : null;
|
|
314
|
+
const rows = this.eventOrder
|
|
315
|
+
.map((eventId) => this.events.get(eventId)!)
|
|
316
|
+
.filter((event) => (
|
|
317
|
+
event.sessionId === filter.sessionId
|
|
318
|
+
&& (!filter.segmentId || event.segmentId === filter.segmentId)
|
|
319
|
+
&& (!types || types.has(event.type))
|
|
320
|
+
&& (filter.epochSeq === undefined || event.epochSeq === filter.epochSeq)
|
|
321
|
+
&& (filter.afterCommitSeq === undefined || event.commitSeq > filter.afterCommitSeq)
|
|
322
|
+
&& (filter.maxCommitSeq === undefined || event.commitSeq <= filter.maxCommitSeq)
|
|
323
|
+
&& (filter.maxEpochSeq === undefined
|
|
324
|
+
|| event.epochSeq === undefined
|
|
325
|
+
|| event.epochSeq <= filter.maxEpochSeq)
|
|
326
|
+
));
|
|
327
|
+
rows.sort((a, b) => filter.order === 'desc'
|
|
328
|
+
? b.commitSeq - a.commitSeq
|
|
329
|
+
: a.commitSeq - b.commitSeq);
|
|
330
|
+
const limit = filter.limit === undefined
|
|
331
|
+
? rows.length
|
|
332
|
+
: Math.max(0, Math.trunc(filter.limit));
|
|
333
|
+
return rows.slice(0, limit).map((event) => cloneRecord(event));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
ensureDeliveries(sessionId: string, consumerId: string): void {
|
|
337
|
+
for (const event of this.events.values()) {
|
|
338
|
+
if (event.sessionId !== sessionId) continue;
|
|
339
|
+
const key = deliveryKey(event.eventId, consumerId);
|
|
340
|
+
if (!this.deliveries.has(key)) {
|
|
341
|
+
this.deliveries.set(key, this.initialDelivery(event, consumerId));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
delivery(eventId: string, consumerId: string): EventDeliveryRecord | null {
|
|
347
|
+
const event = this.events.get(eventId);
|
|
348
|
+
if (!event) return null;
|
|
349
|
+
const key = deliveryKey(eventId, consumerId);
|
|
350
|
+
let delivery = this.deliveries.get(key);
|
|
351
|
+
if (!delivery) {
|
|
352
|
+
delivery = this.initialDelivery(event, consumerId);
|
|
353
|
+
this.deliveries.set(key, delivery);
|
|
354
|
+
}
|
|
355
|
+
return cloneRecord(delivery);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
listEvents(filter: EventListFilter, consumerId: string): EventWithDelivery[] {
|
|
359
|
+
this.ensureDeliveries(filter.sessionId, consumerId);
|
|
360
|
+
const typeSet = filter.types ? new Set(filter.types) : null;
|
|
361
|
+
const deliveryClassSet = filter.deliveryClasses ? new Set(filter.deliveryClasses) : null;
|
|
362
|
+
const detailStateSet = filter.detailStates ? new Set(filter.detailStates) : null;
|
|
363
|
+
const monitorStateSet = filter.monitorStates ? new Set(filter.monitorStates) : null;
|
|
364
|
+
|
|
365
|
+
const rows: EventWithDelivery[] = [];
|
|
366
|
+
for (const eventId of this.eventOrder) {
|
|
367
|
+
const event = this.events.get(eventId)!;
|
|
368
|
+
if (event.sessionId !== filter.sessionId) continue;
|
|
369
|
+
if (filter.segmentId && event.segmentId !== filter.segmentId) continue;
|
|
370
|
+
if (filter.excludeSegmentId && event.segmentId === filter.excludeSegmentId) continue;
|
|
371
|
+
if (typeSet && !typeSet.has(event.type)) continue;
|
|
372
|
+
if (deliveryClassSet && !deliveryClassSet.has(event.deliveryClass)) continue;
|
|
373
|
+
if (filter.maxCommitSeq !== undefined && event.commitSeq > filter.maxCommitSeq) continue;
|
|
374
|
+
if (
|
|
375
|
+
filter.maxEpochSeq !== undefined
|
|
376
|
+
&& event.epochSeq !== undefined
|
|
377
|
+
&& event.epochSeq > filter.maxEpochSeq
|
|
378
|
+
) continue;
|
|
379
|
+
const delivery = this.deliveries.get(deliveryKey(eventId, consumerId))!;
|
|
380
|
+
if (detailStateSet && !detailStateSet.has(delivery.detailState)) continue;
|
|
381
|
+
if (monitorStateSet && !monitorStateSet.has(delivery.monitorState)) continue;
|
|
382
|
+
rows.push({ event: cloneRecord(event), delivery: cloneRecord(delivery) });
|
|
383
|
+
}
|
|
384
|
+
rows.sort((a, b) => filter.order === 'desc'
|
|
385
|
+
? b.event.commitSeq - a.event.commitSeq
|
|
386
|
+
: a.event.commitSeq - b.event.commitSeq);
|
|
387
|
+
return rows;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
backlogSummary(sessionId: string, consumerId: string): BacklogSummary {
|
|
391
|
+
const rows = this.listEvents({
|
|
392
|
+
sessionId,
|
|
393
|
+
detailStates: ['deferred'],
|
|
394
|
+
order: 'asc',
|
|
395
|
+
}, consumerId);
|
|
396
|
+
const segments = new Set(rows.map((row) => row.event.segmentId));
|
|
397
|
+
return {
|
|
398
|
+
unreadCount: rows.length,
|
|
399
|
+
segmentCount: segments.size,
|
|
400
|
+
oldestAt: rows[0]?.event.occurredAt,
|
|
401
|
+
newestAt: rows.at(-1)?.event.occurredAt,
|
|
402
|
+
available: rows.length > 0,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
transitionDetails(transition: DetailStateTransition): number {
|
|
407
|
+
this.assertLeaseIfPresent(transition.lease, transition.nowMs);
|
|
408
|
+
const from = new Set(transition.from);
|
|
409
|
+
let changed = 0;
|
|
410
|
+
for (const eventId of transition.eventIds) {
|
|
411
|
+
const key = deliveryKey(eventId, transition.consumerId);
|
|
412
|
+
const current = this.deliveries.get(key);
|
|
413
|
+
if (!current || !from.has(current.detailState)) continue;
|
|
414
|
+
const next: EventDeliveryRecord = {
|
|
415
|
+
...current,
|
|
416
|
+
detailState: transition.to,
|
|
417
|
+
detailDeliveredAt: transition.to === 'delivered' ? transition.deliveredAt : undefined,
|
|
418
|
+
detailDeliveryVia: transition.to === 'delivered' ? transition.deliveryVia : undefined,
|
|
419
|
+
};
|
|
420
|
+
if (transition.to === 'delivered' && (!next.detailDeliveredAt || !next.detailDeliveryVia)) {
|
|
421
|
+
throw new Error('Delivered event details require deliveredAt and deliveryVia.');
|
|
422
|
+
}
|
|
423
|
+
this.deliveries.set(key, next);
|
|
424
|
+
changed += 1;
|
|
425
|
+
}
|
|
426
|
+
return changed;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
transitionMonitor(transition: MonitorStateTransition): number {
|
|
430
|
+
this.assertLeaseIfPresent(transition.lease, transition.nowMs);
|
|
431
|
+
const from = new Set(transition.from);
|
|
432
|
+
let changed = 0;
|
|
433
|
+
for (const eventId of transition.eventIds) {
|
|
434
|
+
const key = deliveryKey(eventId, transition.consumerId);
|
|
435
|
+
const current = this.deliveries.get(key);
|
|
436
|
+
if (!current || !from.has(current.monitorState)) continue;
|
|
437
|
+
if (transition.to === 'notified' && !transition.deliveredAt) {
|
|
438
|
+
throw new Error('Notified Monitor events require deliveredAt.');
|
|
439
|
+
}
|
|
440
|
+
this.deliveries.set(key, {
|
|
441
|
+
...current,
|
|
442
|
+
monitorState: transition.to,
|
|
443
|
+
monitorDeliveredAt: transition.to === 'notified' ? transition.deliveredAt : undefined,
|
|
444
|
+
});
|
|
445
|
+
changed += 1;
|
|
446
|
+
}
|
|
447
|
+
return changed;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
recordMonitorAttempt(attempt: MonitorDeliveryAttempt): boolean {
|
|
451
|
+
this.assertLeaseIfPresent(attempt.lease, attempt.nowMs);
|
|
452
|
+
const key = deliveryKey(attempt.eventId, attempt.consumerId);
|
|
453
|
+
const current = this.deliveries.get(key);
|
|
454
|
+
if (!current || current.monitorState !== 'pending') return false;
|
|
455
|
+
this.deliveries.set(key, {
|
|
456
|
+
...current,
|
|
457
|
+
monitorAttemptCount: current.monitorAttemptCount + 1,
|
|
458
|
+
monitorLastAttemptAt: attempt.attemptedAt,
|
|
459
|
+
});
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
acquireLease(request: AcquireLeaseRequest): ConsumerLease | null {
|
|
464
|
+
const nowMs = request.nowMs ?? Date.now();
|
|
465
|
+
const key = leaseKey(request.consumerId, request.channel);
|
|
466
|
+
const existing = this.leases.get(key);
|
|
467
|
+
if (existing && existing.expiresAt > nowMs && existing.ownerId !== request.ownerId) return null;
|
|
468
|
+
const lease: ConsumerLease = {
|
|
469
|
+
consumerId: request.consumerId,
|
|
470
|
+
channel: request.channel,
|
|
471
|
+
ownerId: request.ownerId,
|
|
472
|
+
expiresAt: nowMs + Math.max(1, request.ttlMs),
|
|
473
|
+
};
|
|
474
|
+
this.leases.set(key, lease);
|
|
475
|
+
return cloneRecord(lease);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
leaseIsValid(lease: ConsumerLease, nowMs = Date.now()): boolean {
|
|
479
|
+
const current = this.leases.get(leaseKey(lease.consumerId, lease.channel));
|
|
480
|
+
return !!current && current.ownerId === lease.ownerId && current.expiresAt > nowMs;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
releaseLease(lease: ConsumerLease): boolean {
|
|
484
|
+
const key = leaseKey(lease.consumerId, lease.channel);
|
|
485
|
+
const current = this.leases.get(key);
|
|
486
|
+
if (!current || current.ownerId !== lease.ownerId) return false;
|
|
487
|
+
this.leases.delete(key);
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
enqueueMailboxItem<T = unknown>(request: EnqueueMailboxItem<T>): EnqueueMailboxResult<T> {
|
|
492
|
+
const uniqueKey = mailboxUniqueKey(request.consumerId, request.eventId, request.mode);
|
|
493
|
+
const existingUniqueId = this.mailboxUniqueItems.get(uniqueKey);
|
|
494
|
+
if (existingUniqueId) {
|
|
495
|
+
return {
|
|
496
|
+
inserted: false,
|
|
497
|
+
item: cloneRecord(this.mailboxItems.get(existingUniqueId) as MailboxItem<T>),
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
const itemIdCollision = this.mailboxItems.get(request.itemId);
|
|
501
|
+
if (itemIdCollision) {
|
|
502
|
+
throw new Error(`Mailbox item ID already exists: ${request.itemId}`);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const event = this.events.get(request.eventId);
|
|
506
|
+
if (!event) throw new Error(`Unknown Mailbox event: ${request.eventId}`);
|
|
507
|
+
if (
|
|
508
|
+
event.sessionId !== request.sessionId
|
|
509
|
+
|| event.segmentId !== request.segmentId
|
|
510
|
+
|| event.commitSeq !== request.eventCommitSeq
|
|
511
|
+
) {
|
|
512
|
+
throw new Error(`Mailbox scope does not match Event ${request.eventId}.`);
|
|
513
|
+
}
|
|
514
|
+
if (event.deliveryClass !== 'monitor_and_events') {
|
|
515
|
+
throw new Error(`Short Mailbox delivery is not applicable to Event ${request.eventId}.`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const nowMs = Date.now();
|
|
519
|
+
const createdAt = request.createdAt ?? new Date(nowMs).toISOString();
|
|
520
|
+
const item: MailboxItem<T> = {
|
|
521
|
+
itemId: request.itemId,
|
|
522
|
+
schemaVersion: request.schemaVersion ?? 1,
|
|
523
|
+
formatterVersion: request.formatterVersion,
|
|
524
|
+
consumerId: request.consumerId,
|
|
525
|
+
sessionId: request.sessionId,
|
|
526
|
+
segmentId: request.segmentId,
|
|
527
|
+
eventId: request.eventId,
|
|
528
|
+
eventCommitSeq: request.eventCommitSeq,
|
|
529
|
+
mode: request.mode,
|
|
530
|
+
coalesceKey: request.coalesceKey,
|
|
531
|
+
payload: cloneRecord(request.payload),
|
|
532
|
+
state: this.mailboxSessionIsCurrent(request.sessionId, request.segmentId)
|
|
533
|
+
? 'pending'
|
|
534
|
+
: 'suppressed',
|
|
535
|
+
attemptCount: 0,
|
|
536
|
+
availableAtMs: request.availableAtMs ?? 0,
|
|
537
|
+
createdAt,
|
|
538
|
+
updatedAt: createdAt,
|
|
539
|
+
};
|
|
540
|
+
this.delivery(event.eventId, request.consumerId);
|
|
541
|
+
this.mailboxItems.set(item.itemId, item as MailboxItem);
|
|
542
|
+
this.mailboxUniqueItems.set(uniqueKey, item.itemId);
|
|
543
|
+
return { inserted: true, item: cloneRecord(item) };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
mailboxItem<T = unknown>(itemId: string): MailboxItem<T> | null {
|
|
547
|
+
const item = this.mailboxItems.get(itemId);
|
|
548
|
+
return item ? cloneRecord(item as MailboxItem<T>) : null;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
mailboxItemForEvent<T = unknown>(
|
|
552
|
+
consumerId: string,
|
|
553
|
+
eventId: string,
|
|
554
|
+
mode: MailboxDeliveryMode,
|
|
555
|
+
): MailboxItem<T> | null {
|
|
556
|
+
const itemId = this.mailboxUniqueItems.get(mailboxUniqueKey(consumerId, eventId, mode));
|
|
557
|
+
return itemId ? this.mailboxItem<T>(itemId) : null;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
peekMailbox<T = unknown>(filter: MailboxPeekFilter): MailboxItem<T>[] {
|
|
561
|
+
const eventIds = filter.eventIds ? new Set(filter.eventIds) : null;
|
|
562
|
+
const modes = filter.modes ? new Set(filter.modes) : null;
|
|
563
|
+
const states = filter.states ? new Set(filter.states) : null;
|
|
564
|
+
const rows = [...this.mailboxItems.values()].filter((item) => (
|
|
565
|
+
item.consumerId === filter.consumerId
|
|
566
|
+
&& (!filter.sessionId || item.sessionId === filter.sessionId)
|
|
567
|
+
&& (!filter.segmentId || item.segmentId === filter.segmentId)
|
|
568
|
+
&& (!eventIds || eventIds.has(item.eventId))
|
|
569
|
+
&& (!modes || modes.has(item.mode))
|
|
570
|
+
&& (!states || states.has(item.state))
|
|
571
|
+
));
|
|
572
|
+
rows.sort((a, b) => {
|
|
573
|
+
const direction = filter.order === 'desc' ? -1 : 1;
|
|
574
|
+
return direction * (
|
|
575
|
+
a.eventCommitSeq === b.eventCommitSeq
|
|
576
|
+
? a.itemId.localeCompare(b.itemId)
|
|
577
|
+
: a.eventCommitSeq - b.eventCommitSeq
|
|
578
|
+
);
|
|
579
|
+
});
|
|
580
|
+
const limit = filter.limit === undefined
|
|
581
|
+
? rows.length
|
|
582
|
+
: Math.max(0, Math.trunc(filter.limit));
|
|
583
|
+
return rows.slice(0, limit).map((item) => cloneRecord(item as MailboxItem<T>));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
claimMailbox<T = unknown>(request: ClaimMailboxItemsRequest): MailboxClaim<T> | null {
|
|
587
|
+
const nowMs = request.nowMs ?? Date.now();
|
|
588
|
+
this.suppressExpiredMailboxClaimsOutsideCurrentSession(request.consumerId, nowMs);
|
|
589
|
+
if (!this.mailboxSessionIsCurrent(request.sessionId, request.segmentId)) return null;
|
|
590
|
+
|
|
591
|
+
const rows = [...this.mailboxItems.values()]
|
|
592
|
+
.filter((item) => (
|
|
593
|
+
item.consumerId === request.consumerId
|
|
594
|
+
&& item.sessionId === request.sessionId
|
|
595
|
+
&& item.segmentId === request.segmentId
|
|
596
|
+
&& item.mode === request.mode
|
|
597
|
+
&& (item.state === 'pending' || item.state === 'claimed')
|
|
598
|
+
))
|
|
599
|
+
.sort((a, b) => (
|
|
600
|
+
a.eventCommitSeq === b.eventCommitSeq
|
|
601
|
+
? a.itemId.localeCompare(b.itemId)
|
|
602
|
+
: a.eventCommitSeq - b.eventCommitSeq
|
|
603
|
+
));
|
|
604
|
+
const activeRows: MailboxItem[] = [];
|
|
605
|
+
for (const item of rows) {
|
|
606
|
+
if (
|
|
607
|
+
(item.state === 'pending' || (item.claimExpiresAtMs ?? 0) <= nowMs)
|
|
608
|
+
&& !this.mailboxItemMatchesDelivery(item)
|
|
609
|
+
) {
|
|
610
|
+
this.mailboxItems.set(item.itemId, {
|
|
611
|
+
...item,
|
|
612
|
+
state: 'suppressed',
|
|
613
|
+
claimOwner: undefined,
|
|
614
|
+
claimToken: undefined,
|
|
615
|
+
claimExpiresAtMs: undefined,
|
|
616
|
+
updatedAt: new Date(nowMs).toISOString(),
|
|
617
|
+
});
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
activeRows.push(item);
|
|
621
|
+
}
|
|
622
|
+
const first = activeRows[0];
|
|
623
|
+
if (!first || !this.mailboxItemIsClaimable(first, nowMs)) return null;
|
|
624
|
+
|
|
625
|
+
const maxItems = Math.max(1, Math.trunc(request.maxItems ?? 1));
|
|
626
|
+
const selected: MailboxItem[] = [];
|
|
627
|
+
for (const item of activeRows) {
|
|
628
|
+
if (selected.length >= maxItems) break;
|
|
629
|
+
if (
|
|
630
|
+
item.coalesceKey !== first.coalesceKey
|
|
631
|
+
|| item.formatterVersion !== first.formatterVersion
|
|
632
|
+
|| !this.mailboxItemIsClaimable(item, nowMs)
|
|
633
|
+
) break;
|
|
634
|
+
selected.push(item);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const claimToken = randomUUID();
|
|
638
|
+
const claimExpiresAtMs = nowMs + Math.max(1, request.leaseTtlMs);
|
|
639
|
+
const attemptedAt = new Date(nowMs).toISOString();
|
|
640
|
+
const claimed = selected.map((item) => {
|
|
641
|
+
const next: MailboxItem = {
|
|
642
|
+
...item,
|
|
643
|
+
state: 'claimed',
|
|
644
|
+
attemptCount: item.attemptCount + 1,
|
|
645
|
+
lastAttemptAt: attemptedAt,
|
|
646
|
+
claimOwner: request.ownerId,
|
|
647
|
+
claimToken,
|
|
648
|
+
claimExpiresAtMs,
|
|
649
|
+
updatedAt: attemptedAt,
|
|
650
|
+
};
|
|
651
|
+
this.mailboxItems.set(next.itemId, next);
|
|
652
|
+
const deliveryKeyValue = deliveryKey(next.eventId, next.consumerId);
|
|
653
|
+
const delivery = this.deliveries.get(deliveryKeyValue);
|
|
654
|
+
if (delivery?.monitorState === 'pending') {
|
|
655
|
+
this.deliveries.set(deliveryKeyValue, {
|
|
656
|
+
...delivery,
|
|
657
|
+
monitorAttemptCount: delivery.monitorAttemptCount + 1,
|
|
658
|
+
monitorLastAttemptAt: attemptedAt,
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
return cloneRecord(next as MailboxItem<T>);
|
|
662
|
+
});
|
|
663
|
+
return {
|
|
664
|
+
ownerId: request.ownerId,
|
|
665
|
+
claimToken,
|
|
666
|
+
claimExpiresAtMs,
|
|
667
|
+
items: claimed,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
ackMailboxClaim(request: AckMailboxClaimRequest): AckMailboxClaimResult {
|
|
672
|
+
const nowMs = request.nowMs ?? Date.now();
|
|
673
|
+
const matches = [...this.mailboxItems.values()].filter((item) => (
|
|
674
|
+
item.claimOwner === request.ownerId && item.claimToken === request.claimToken
|
|
675
|
+
));
|
|
676
|
+
if (matches.length === 0) return { status: 'claim_lost', itemIds: [], eventIds: [] };
|
|
677
|
+
const itemIds = matches.map((item) => item.itemId);
|
|
678
|
+
const eventIds = matches.map((item) => item.eventId);
|
|
679
|
+
if (matches.every((item) => item.state === 'accepted')) {
|
|
680
|
+
return { status: 'already_accepted', itemIds, eventIds };
|
|
681
|
+
}
|
|
682
|
+
if (
|
|
683
|
+
matches.some((item) => item.state !== 'claimed')
|
|
684
|
+
|| matches.some((item) => (item.claimExpiresAtMs ?? 0) <= nowMs)
|
|
685
|
+
) {
|
|
686
|
+
return { status: 'claim_lost', itemIds: [], eventIds: [] };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const acceptedAt = request.acceptedAt ?? new Date(nowMs).toISOString();
|
|
690
|
+
const updatedAt = new Date(nowMs).toISOString();
|
|
691
|
+
for (const item of matches) {
|
|
692
|
+
const next: MailboxItem = {
|
|
693
|
+
...item,
|
|
694
|
+
state: 'accepted',
|
|
695
|
+
acceptedAt,
|
|
696
|
+
acceptanceLevel: request.acceptanceLevel ?? 'sink_emitted',
|
|
697
|
+
receiptId: request.receiptId,
|
|
698
|
+
lastErrorCode: undefined,
|
|
699
|
+
updatedAt,
|
|
700
|
+
};
|
|
701
|
+
this.mailboxItems.set(item.itemId, next);
|
|
702
|
+
const key = deliveryKey(item.eventId, item.consumerId);
|
|
703
|
+
const delivery = this.deliveries.get(key);
|
|
704
|
+
if (!delivery) continue;
|
|
705
|
+
this.deliveries.set(key, {
|
|
706
|
+
...delivery,
|
|
707
|
+
...(delivery.monitorState === 'pending'
|
|
708
|
+
? {
|
|
709
|
+
monitorState: 'notified' as const,
|
|
710
|
+
monitorDeliveredAt: acceptedAt,
|
|
711
|
+
}
|
|
712
|
+
: {}),
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
return { status: 'accepted', itemIds, eventIds };
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
nackMailboxClaim(request: NackMailboxClaimRequest): NackMailboxClaimResult {
|
|
719
|
+
const nowMs = request.nowMs ?? Date.now();
|
|
720
|
+
const matches = [...this.mailboxItems.values()].filter((item) => (
|
|
721
|
+
item.claimOwner === request.ownerId
|
|
722
|
+
&& item.claimToken === request.claimToken
|
|
723
|
+
&& item.state === 'claimed'
|
|
724
|
+
));
|
|
725
|
+
if (
|
|
726
|
+
matches.length === 0
|
|
727
|
+
|| matches.some((item) => (item.claimExpiresAtMs ?? 0) <= nowMs)
|
|
728
|
+
) {
|
|
729
|
+
return { status: 'claim_lost', itemIds: [] };
|
|
730
|
+
}
|
|
731
|
+
const remainsCurrent = matches.every((item) => (
|
|
732
|
+
this.mailboxSessionIsCurrent(item.sessionId, item.segmentId)
|
|
733
|
+
));
|
|
734
|
+
const updatedAt = new Date(nowMs).toISOString();
|
|
735
|
+
for (const item of matches) {
|
|
736
|
+
const next: MailboxItem = {
|
|
737
|
+
...item,
|
|
738
|
+
state: remainsCurrent ? 'pending' : 'suppressed',
|
|
739
|
+
availableAtMs: request.retryAtMs,
|
|
740
|
+
claimOwner: undefined,
|
|
741
|
+
claimToken: undefined,
|
|
742
|
+
claimExpiresAtMs: undefined,
|
|
743
|
+
lastErrorCode: request.errorCode,
|
|
744
|
+
updatedAt,
|
|
745
|
+
};
|
|
746
|
+
this.mailboxItems.set(item.itemId, next);
|
|
747
|
+
if (!remainsCurrent) this.suppressMailboxEventDelivery(item);
|
|
748
|
+
}
|
|
749
|
+
return {
|
|
750
|
+
status: remainsCurrent ? 'released' : 'suppressed',
|
|
751
|
+
itemIds: matches.map((item) => item.itemId),
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
suppressMailboxItems(filter: SuppressMailboxItemsFilter): number {
|
|
756
|
+
const nowMs = filter.nowMs ?? Date.now();
|
|
757
|
+
const eventIds = filter.eventIds ? new Set(filter.eventIds) : null;
|
|
758
|
+
const modes = filter.modes ? new Set(filter.modes) : null;
|
|
759
|
+
const updatedAt = new Date(nowMs).toISOString();
|
|
760
|
+
let changed = 0;
|
|
761
|
+
for (const [itemId, item] of this.mailboxItems) {
|
|
762
|
+
if (
|
|
763
|
+
item.consumerId !== filter.consumerId
|
|
764
|
+
|| (filter.sessionId && item.sessionId !== filter.sessionId)
|
|
765
|
+
|| (filter.segmentId && item.segmentId !== filter.segmentId)
|
|
766
|
+
|| (filter.excludeSegmentId && item.segmentId === filter.excludeSegmentId)
|
|
767
|
+
|| (eventIds && !eventIds.has(item.eventId))
|
|
768
|
+
|| (modes && !modes.has(item.mode))
|
|
769
|
+
) continue;
|
|
770
|
+
const suppressible = item.state === 'pending'
|
|
771
|
+
|| (
|
|
772
|
+
filter.includeExpiredClaims === true
|
|
773
|
+
&& item.state === 'claimed'
|
|
774
|
+
&& (item.claimExpiresAtMs ?? 0) <= nowMs
|
|
775
|
+
);
|
|
776
|
+
if (!suppressible) continue;
|
|
777
|
+
this.mailboxItems.set(itemId, {
|
|
778
|
+
...item,
|
|
779
|
+
state: 'suppressed',
|
|
780
|
+
claimOwner: undefined,
|
|
781
|
+
claimToken: undefined,
|
|
782
|
+
claimExpiresAtMs: undefined,
|
|
783
|
+
updatedAt,
|
|
784
|
+
});
|
|
785
|
+
this.suppressMailboxEventDelivery(item);
|
|
786
|
+
changed += 1;
|
|
787
|
+
}
|
|
788
|
+
return changed;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
sourceCheckpoint(sourceId: string): SourceCheckpointRecord | null {
|
|
792
|
+
const checkpoint = this.checkpoints.get(sourceId);
|
|
793
|
+
return checkpoint ? cloneRecord(checkpoint) : null;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
private initialDelivery(event: CanonicalEvent, consumerId: string): EventDeliveryRecord {
|
|
797
|
+
const delivery = initialDeliveryFor(event, consumerId);
|
|
798
|
+
if (
|
|
799
|
+
delivery.detailState === 'pending'
|
|
800
|
+
&& !this.deliveryScopeIsCurrent(event.sessionId, event.segmentId)
|
|
801
|
+
) {
|
|
802
|
+
delivery.detailState = 'deferred';
|
|
803
|
+
}
|
|
804
|
+
if (
|
|
805
|
+
delivery.monitorState === 'pending'
|
|
806
|
+
&& !this.mailboxSessionIsCurrent(event.sessionId, event.segmentId)
|
|
807
|
+
) {
|
|
808
|
+
delivery.monitorState = 'suppressed';
|
|
809
|
+
}
|
|
810
|
+
return delivery;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
private mailboxSessionIsCurrent(sessionId: string, segmentId: string): boolean {
|
|
814
|
+
const session = this.sessions.get(sessionId);
|
|
815
|
+
const segment = this.segments.get(segmentId);
|
|
816
|
+
return !!session?.isCurrent
|
|
817
|
+
&& !!segment
|
|
818
|
+
&& segment.sessionId === sessionId;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
private deliveryScopeIsCurrent(sessionId: string, segmentId: string): boolean {
|
|
822
|
+
return this.mailboxSessionIsCurrent(sessionId, segmentId)
|
|
823
|
+
&& this.segments.get(segmentId)?.isCurrent === true;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
private carrySegmentsForward(segmentIds: ReadonlySet<string>, updatedAt: string): void {
|
|
827
|
+
for (const [itemId, item] of this.mailboxItems) {
|
|
828
|
+
if (!segmentIds.has(item.segmentId) || item.state !== 'pending') continue;
|
|
829
|
+
this.mailboxItems.set(itemId, { ...item, availableAtMs: 0, updatedAt });
|
|
830
|
+
}
|
|
831
|
+
for (const event of this.events.values()) {
|
|
832
|
+
if (!segmentIds.has(event.segmentId)) continue;
|
|
833
|
+
for (const [key, delivery] of this.deliveries) {
|
|
834
|
+
if (delivery.eventId !== event.eventId || delivery.detailState !== 'pending') continue;
|
|
835
|
+
this.deliveries.set(key, { ...delivery, detailState: 'deferred' });
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
private retireMailboxScope(
|
|
841
|
+
itemMatches: (item: MailboxItem) => boolean,
|
|
842
|
+
eventMatches: (event: CanonicalEvent) => boolean,
|
|
843
|
+
updatedAt: string,
|
|
844
|
+
): void {
|
|
845
|
+
for (const [itemId, item] of this.mailboxItems) {
|
|
846
|
+
if (!itemMatches(item) || item.state !== 'pending') continue;
|
|
847
|
+
this.mailboxItems.set(itemId, {
|
|
848
|
+
...item,
|
|
849
|
+
state: 'suppressed',
|
|
850
|
+
updatedAt,
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
for (const event of this.events.values()) {
|
|
854
|
+
if (!eventMatches(event)) continue;
|
|
855
|
+
for (const [key, delivery] of this.deliveries) {
|
|
856
|
+
if (delivery.eventId !== event.eventId) continue;
|
|
857
|
+
const claimedMailbox = [...this.mailboxItems.values()].some((item) => (
|
|
858
|
+
item.eventId === event.eventId
|
|
859
|
+
&& item.consumerId === delivery.consumerId
|
|
860
|
+
&& item.state === 'claimed'
|
|
861
|
+
));
|
|
862
|
+
this.deliveries.set(key, {
|
|
863
|
+
...delivery,
|
|
864
|
+
...(delivery.detailState === 'pending' ? { detailState: 'deferred' as const } : {}),
|
|
865
|
+
...(delivery.monitorState === 'pending' && !claimedMailbox
|
|
866
|
+
? { monitorState: 'suppressed' as const }
|
|
867
|
+
: {}),
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
private mailboxItemIsClaimable(item: MailboxItem, nowMs: number): boolean {
|
|
874
|
+
return item.state === 'pending'
|
|
875
|
+
? item.availableAtMs <= nowMs
|
|
876
|
+
: item.state === 'claimed' && (item.claimExpiresAtMs ?? 0) <= nowMs;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
private mailboxItemMatchesDelivery(item: MailboxItem): boolean {
|
|
880
|
+
const delivery = this.deliveries.get(deliveryKey(item.eventId, item.consumerId));
|
|
881
|
+
return item.mode === 'short'
|
|
882
|
+
? delivery?.monitorState === 'pending'
|
|
883
|
+
: delivery !== undefined && ['pending', 'deferred'].includes(delivery.detailState);
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
private suppressMailboxEventDelivery(
|
|
887
|
+
item: Pick<MailboxItem, 'eventId' | 'consumerId' | 'sessionId' | 'segmentId'>,
|
|
888
|
+
): void {
|
|
889
|
+
const key = deliveryKey(item.eventId, item.consumerId);
|
|
890
|
+
const delivery = this.deliveries.get(key);
|
|
891
|
+
if (!delivery) return;
|
|
892
|
+
this.deliveries.set(key, {
|
|
893
|
+
...delivery,
|
|
894
|
+
...(delivery.monitorState === 'pending' ? { monitorState: 'suppressed' as const } : {}),
|
|
895
|
+
...(
|
|
896
|
+
!this.deliveryScopeIsCurrent(item.sessionId, item.segmentId)
|
|
897
|
+
&& delivery.detailState === 'pending'
|
|
898
|
+
? { detailState: 'deferred' as const }
|
|
899
|
+
: {}
|
|
900
|
+
),
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
private suppressExpiredMailboxClaimsOutsideCurrentSession(consumerId: string, nowMs: number): void {
|
|
905
|
+
const updatedAt = new Date(nowMs).toISOString();
|
|
906
|
+
for (const [itemId, item] of this.mailboxItems) {
|
|
907
|
+
if (
|
|
908
|
+
item.consumerId !== consumerId
|
|
909
|
+
|| item.state !== 'claimed'
|
|
910
|
+
|| (item.claimExpiresAtMs ?? 0) > nowMs
|
|
911
|
+
|| this.mailboxSessionIsCurrent(item.sessionId, item.segmentId)
|
|
912
|
+
) continue;
|
|
913
|
+
this.mailboxItems.set(itemId, {
|
|
914
|
+
...item,
|
|
915
|
+
state: 'suppressed',
|
|
916
|
+
claimOwner: undefined,
|
|
917
|
+
claimToken: undefined,
|
|
918
|
+
claimExpiresAtMs: undefined,
|
|
919
|
+
updatedAt,
|
|
920
|
+
});
|
|
921
|
+
this.suppressMailboxEventDelivery(item);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
private assertLeaseIfPresent(lease: ConsumerLease | undefined, nowMs?: number): void {
|
|
926
|
+
if (lease && !this.leaseIsValid(lease, nowMs)) throw new DeliveryLeaseLostError(lease);
|
|
927
|
+
}
|
|
928
|
+
}
|