@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,1260 @@
|
|
|
1
|
+
import type { ScopeRef } from '../contracts.js';
|
|
2
|
+
import {
|
|
3
|
+
cloneSymbolicDomains,
|
|
4
|
+
containsAllDomains,
|
|
5
|
+
containsDomain,
|
|
6
|
+
domainsOverlap,
|
|
7
|
+
emptySymbolicDomains,
|
|
8
|
+
normalizeDomains,
|
|
9
|
+
normalizeNames,
|
|
10
|
+
requestDomains,
|
|
11
|
+
symbolicHasAny,
|
|
12
|
+
symbolicList,
|
|
13
|
+
WILDCARD_DOMAIN,
|
|
14
|
+
type SymbolicDomains,
|
|
15
|
+
} from './domain-set.js';
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_CONTROLLER_PRIORITY,
|
|
18
|
+
type AwaitControlOptions,
|
|
19
|
+
type AwaitControlResult,
|
|
20
|
+
type ControlClock,
|
|
21
|
+
type ControlDeferred,
|
|
22
|
+
type ControlExecutionContext,
|
|
23
|
+
type ControllerBlockerSnapshot,
|
|
24
|
+
type ControllerHandoff,
|
|
25
|
+
type ControllerLifecycle,
|
|
26
|
+
type ControllerSnapshot,
|
|
27
|
+
type ControllerSpec,
|
|
28
|
+
type ControlRunResult,
|
|
29
|
+
type ControlSnapshot,
|
|
30
|
+
type ReleaseControllerOptions,
|
|
31
|
+
type RunControlledAction,
|
|
32
|
+
type TouchControllerOptions,
|
|
33
|
+
} from './types.js';
|
|
34
|
+
|
|
35
|
+
const DEFAULT_ACTIVITY_LEASE_MS = 15_000;
|
|
36
|
+
const DEFAULT_HANDOFF_WAIT_MS = 250;
|
|
37
|
+
const MAX_RELEASED_TOMBSTONES = 32;
|
|
38
|
+
|
|
39
|
+
const realClock: ControlClock = {
|
|
40
|
+
now: () => Date.now(),
|
|
41
|
+
sleep: (milliseconds) => new Promise((resolve) => {
|
|
42
|
+
const timer = setTimeout(resolve, milliseconds);
|
|
43
|
+
// An abandoned wait after a controller change must not keep the owner
|
|
44
|
+
// process alive until an old lease/deadline timer elapses.
|
|
45
|
+
timer.unref?.();
|
|
46
|
+
}),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export interface ControlSchedulerOptions {
|
|
50
|
+
clock?: ControlClock;
|
|
51
|
+
defaultActivityLeaseMs?: number;
|
|
52
|
+
defaultHandoffWaitMs?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface DomainClaim {
|
|
56
|
+
expiresAtMs?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface ControllerRecord {
|
|
60
|
+
spec: Omit<ControllerSpec, 'domains' | 'mutexGroups' | 'priority' | 'holdMode' | 'lifecycle'> & {
|
|
61
|
+
domains: string[];
|
|
62
|
+
mutexGroups: string[];
|
|
63
|
+
priority: number;
|
|
64
|
+
holdMode: 'all_or_nothing' | 'per_resource';
|
|
65
|
+
lifecycle: ControllerLifecycle;
|
|
66
|
+
};
|
|
67
|
+
sequence: number;
|
|
68
|
+
epoch: number;
|
|
69
|
+
/** Changes only when an admitted permit must become stale. */
|
|
70
|
+
authorityEpoch: number;
|
|
71
|
+
registeredAtMs: number;
|
|
72
|
+
lastTouchedAtMs: number;
|
|
73
|
+
claims: Map<string, DomainClaim>;
|
|
74
|
+
excludedDomains: Set<string>;
|
|
75
|
+
ready: boolean;
|
|
76
|
+
releasedAtMs?: number;
|
|
77
|
+
releaseReason?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface EffectiveController {
|
|
81
|
+
record: ControllerRecord;
|
|
82
|
+
claim: SymbolicDomains;
|
|
83
|
+
grant: SymbolicDomains;
|
|
84
|
+
held: SymbolicDomains;
|
|
85
|
+
blockers: Map<string, ControllerBlockerSnapshot>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface InFlightOperation {
|
|
89
|
+
operationId: number;
|
|
90
|
+
controllerId: string;
|
|
91
|
+
controllerEpoch: number;
|
|
92
|
+
scope: ScopeRef;
|
|
93
|
+
scopeKey: string;
|
|
94
|
+
domains: string[];
|
|
95
|
+
domainSet: SymbolicDomains;
|
|
96
|
+
mutexGroups: string[];
|
|
97
|
+
priority: number;
|
|
98
|
+
sequence: number;
|
|
99
|
+
submittedAtMs: number;
|
|
100
|
+
settled: Promise<unknown>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type OperationWaitResult = 'settled' | 'timeout' | 'interrupted';
|
|
104
|
+
|
|
105
|
+
function scopeKey(scope: ScopeRef): string {
|
|
106
|
+
return JSON.stringify([scope.adapterId, scope.accountId, scope.sessionId, scope.segmentId]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function sameScope(left: ScopeRef, right: ScopeRef): boolean {
|
|
110
|
+
return scopeKey(left) === scopeKey(right);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function cloneScope(scope: ScopeRef): ScopeRef {
|
|
114
|
+
return { ...scope };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function validateDuration(name: string, value: number): number {
|
|
118
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
119
|
+
throw new TypeError(`${name} must be a positive finite number.`);
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function validateBoundedWait(name: string, value: number): number {
|
|
125
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
126
|
+
throw new TypeError(`${name} must be a non-negative finite number.`);
|
|
127
|
+
}
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function lifecycleFor(spec: ControllerSpec): ControllerLifecycle {
|
|
132
|
+
if (spec.lifecycle) return spec.lifecycle;
|
|
133
|
+
if (spec.kind === 'agent' || spec.kind === 'user') return 'activity_lease';
|
|
134
|
+
if (spec.kind === 'task') return 'finite';
|
|
135
|
+
return 'persistent';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function comparePrecedence(left: ControllerRecord, right: ControllerRecord): number {
|
|
139
|
+
return right.spec.priority - left.spec.priority || left.sequence - right.sequence;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function hasSharedMutex(left: ControllerRecord, right: ControllerRecord): boolean {
|
|
143
|
+
return left.spec.mutexGroups.some((group) => right.spec.mutexGroups.includes(group));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function blockerSnapshot(
|
|
147
|
+
blocker: EffectiveController,
|
|
148
|
+
domains: string[],
|
|
149
|
+
mutexGroups: string[] = [],
|
|
150
|
+
): ControllerBlockerSnapshot {
|
|
151
|
+
return {
|
|
152
|
+
controllerId: blocker.record.spec.controllerId,
|
|
153
|
+
domains: [...domains],
|
|
154
|
+
mutexGroups: [...mutexGroups],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Controller-oriented control arbitration.
|
|
160
|
+
*
|
|
161
|
+
* Actions are never queued while their controller is held. A high-priority
|
|
162
|
+
* takeover may only wait briefly for a conflicting provider call that was
|
|
163
|
+
* already submitted before the takeover became effective.
|
|
164
|
+
*/
|
|
165
|
+
export class ControlScheduler {
|
|
166
|
+
private readonly clock: ControlClock;
|
|
167
|
+
private readonly defaultActivityLeaseMs: number;
|
|
168
|
+
private readonly defaultHandoffWaitMs: number;
|
|
169
|
+
private readonly controllers = new Map<string, ControllerRecord>();
|
|
170
|
+
private readonly inFlight = new Map<number, InFlightOperation>();
|
|
171
|
+
private readonly changeWaiters = new Set<() => boolean | void>();
|
|
172
|
+
private activeScope?: ScopeRef;
|
|
173
|
+
private activeScopeKey?: string;
|
|
174
|
+
private scopeEpoch = 0;
|
|
175
|
+
private version = 0;
|
|
176
|
+
private controllerSequence = 0;
|
|
177
|
+
private operationSequence = 0;
|
|
178
|
+
private stopping = false;
|
|
179
|
+
|
|
180
|
+
constructor(options: ControlSchedulerOptions = {}) {
|
|
181
|
+
this.clock = options.clock ?? realClock;
|
|
182
|
+
this.defaultActivityLeaseMs = validateDuration(
|
|
183
|
+
'defaultActivityLeaseMs',
|
|
184
|
+
options.defaultActivityLeaseMs ?? DEFAULT_ACTIVITY_LEASE_MS,
|
|
185
|
+
);
|
|
186
|
+
this.defaultHandoffWaitMs = validateBoundedWait(
|
|
187
|
+
'defaultHandoffWaitMs',
|
|
188
|
+
options.defaultHandoffWaitMs ?? DEFAULT_HANDOFF_WAIT_MS,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
activateScope(scope: ScopeRef): void {
|
|
193
|
+
if (this.stopping) return;
|
|
194
|
+
const nextKey = scopeKey(scope);
|
|
195
|
+
if (nextKey === this.activeScopeKey) return;
|
|
196
|
+
this.activeScope = cloneScope(scope);
|
|
197
|
+
this.activeScopeKey = nextKey;
|
|
198
|
+
this.scopeEpoch += 1;
|
|
199
|
+
const now = this.clock.now();
|
|
200
|
+
for (const [controllerId, controller] of this.controllers) {
|
|
201
|
+
if (scopeKey(controller.spec.scope) === nextKey) continue;
|
|
202
|
+
if (controller.releasedAtMs === undefined) {
|
|
203
|
+
controller.releasedAtMs = now;
|
|
204
|
+
controller.releaseReason = 'scope_changed';
|
|
205
|
+
controller.ready = false;
|
|
206
|
+
controller.epoch += 1;
|
|
207
|
+
controller.authorityEpoch += 1;
|
|
208
|
+
}
|
|
209
|
+
// The scheduler is live ownership state, not the audit log. In-flight
|
|
210
|
+
// operations retain their own immutable fence data and old waiters are
|
|
211
|
+
// invalidated by object identity, so old-scope registrations can go now.
|
|
212
|
+
this.controllers.delete(controllerId);
|
|
213
|
+
}
|
|
214
|
+
this.pruneReleasedControllers();
|
|
215
|
+
this.changed();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
registerController(spec: ControllerSpec): ControllerSnapshot {
|
|
219
|
+
const now = this.clock.now();
|
|
220
|
+
this.sweepExpired(now);
|
|
221
|
+
const previous = this.controllers.get(spec.controllerId);
|
|
222
|
+
if (previous && previous.releasedAtMs === undefined) {
|
|
223
|
+
throw new Error(`Control controller already exists: ${spec.controllerId}`);
|
|
224
|
+
}
|
|
225
|
+
const domains = normalizeDomains(spec.domains);
|
|
226
|
+
const lifecycle = lifecycleFor(spec);
|
|
227
|
+
const activityLeaseMs = spec.activityLeaseMs === undefined
|
|
228
|
+
? this.defaultActivityLeaseMs
|
|
229
|
+
: validateDuration('activityLeaseMs', spec.activityLeaseMs);
|
|
230
|
+
if (!Number.isFinite(spec.priority ?? DEFAULT_CONTROLLER_PRIORITY[spec.kind])) {
|
|
231
|
+
throw new TypeError('Controller priority must be finite.');
|
|
232
|
+
}
|
|
233
|
+
let deadlineAtMs = spec.deadlineAtMs;
|
|
234
|
+
if (spec.maxRuntimeMs !== undefined) {
|
|
235
|
+
validateDuration('maxRuntimeMs', spec.maxRuntimeMs);
|
|
236
|
+
if (deadlineAtMs !== undefined) {
|
|
237
|
+
throw new TypeError('Use deadlineAtMs or maxRuntimeMs, not both.');
|
|
238
|
+
}
|
|
239
|
+
deadlineAtMs = now + spec.maxRuntimeMs;
|
|
240
|
+
}
|
|
241
|
+
if (lifecycle === 'finite') {
|
|
242
|
+
if (deadlineAtMs === undefined || !Number.isFinite(deadlineAtMs) || deadlineAtMs <= now) {
|
|
243
|
+
throw new TypeError('A finite controller requires a future deadlineAtMs or maxRuntimeMs.');
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const record: ControllerRecord = {
|
|
247
|
+
spec: {
|
|
248
|
+
...spec,
|
|
249
|
+
scope: cloneScope(spec.scope),
|
|
250
|
+
domains,
|
|
251
|
+
mutexGroups: normalizeNames(spec.mutexGroups),
|
|
252
|
+
priority: spec.priority ?? DEFAULT_CONTROLLER_PRIORITY[spec.kind],
|
|
253
|
+
holdMode: spec.holdMode ?? 'all_or_nothing',
|
|
254
|
+
lifecycle,
|
|
255
|
+
activityLeaseMs,
|
|
256
|
+
deadlineAtMs,
|
|
257
|
+
},
|
|
258
|
+
sequence: this.controllerSequence++,
|
|
259
|
+
epoch: 1,
|
|
260
|
+
authorityEpoch: 1,
|
|
261
|
+
registeredAtMs: now,
|
|
262
|
+
lastTouchedAtMs: now,
|
|
263
|
+
claims: new Map(),
|
|
264
|
+
excludedDomains: new Set(),
|
|
265
|
+
ready: spec.ready ?? false,
|
|
266
|
+
};
|
|
267
|
+
if (this.activeScopeKey && scopeKey(spec.scope) !== this.activeScopeKey) {
|
|
268
|
+
record.releasedAtMs = now;
|
|
269
|
+
record.releaseReason = 'scope_changed';
|
|
270
|
+
record.ready = false;
|
|
271
|
+
record.epoch += 1;
|
|
272
|
+
}
|
|
273
|
+
if (lifecycle !== 'activity_lease') {
|
|
274
|
+
for (const domain of domains) {
|
|
275
|
+
record.claims.set(domain, lifecycle === 'finite' ? { expiresAtMs: deadlineAtMs } : {});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
this.controllers.set(spec.controllerId, record);
|
|
279
|
+
if (!this.activeScopeKey) this.activateScope(spec.scope);
|
|
280
|
+
this.changed();
|
|
281
|
+
return this.controllerSnapshot(record, this.allocate());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
ensureController(spec: ControllerSpec): ControllerSnapshot {
|
|
285
|
+
this.sweepExpired(this.clock.now());
|
|
286
|
+
const existing = this.controllers.get(spec.controllerId);
|
|
287
|
+
if (!existing) return this.registerController(spec);
|
|
288
|
+
if (existing.releasedAtMs !== undefined) return this.registerController(spec);
|
|
289
|
+
if (!sameScope(existing.spec.scope, spec.scope)) {
|
|
290
|
+
throw new Error(`Controller ${spec.controllerId} belongs to another scope.`);
|
|
291
|
+
}
|
|
292
|
+
return this.controllerSnapshot(existing, this.allocate());
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
setReady(controllerId: string, ready = true): ControllerSnapshot | undefined {
|
|
296
|
+
const record = this.controllers.get(controllerId);
|
|
297
|
+
if (!record || record.releasedAtMs !== undefined) return undefined;
|
|
298
|
+
if (record.ready !== ready) {
|
|
299
|
+
record.ready = ready;
|
|
300
|
+
record.epoch += 1;
|
|
301
|
+
if (!ready) record.authorityEpoch += 1;
|
|
302
|
+
record.lastTouchedAtMs = this.clock.now();
|
|
303
|
+
this.changed();
|
|
304
|
+
}
|
|
305
|
+
return this.controllerSnapshot(record, this.allocate());
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
touch(controllerId: string, options: TouchControllerOptions = {}): ControllerSnapshot | undefined {
|
|
309
|
+
const record = this.controllers.get(controllerId);
|
|
310
|
+
if (!record || record.releasedAtMs !== undefined) return undefined;
|
|
311
|
+
const now = this.clock.now();
|
|
312
|
+
this.sweepExpired(now);
|
|
313
|
+
if (record.releasedAtMs !== undefined) return this.controllerSnapshot(record, this.allocate());
|
|
314
|
+
const domains = normalizeDomains(options.domains ?? record.spec.domains);
|
|
315
|
+
const leaseMs = options.activityLeaseMs === undefined
|
|
316
|
+
? record.spec.activityLeaseMs ?? this.defaultActivityLeaseMs
|
|
317
|
+
: validateDuration('activityLeaseMs', options.activityLeaseMs);
|
|
318
|
+
this.applyTouch(record, domains, now, leaseMs);
|
|
319
|
+
record.lastTouchedAtMs = now;
|
|
320
|
+
record.epoch += 1;
|
|
321
|
+
this.changed();
|
|
322
|
+
return this.controllerSnapshot(record, this.allocate());
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
release(controllerId: string, options: ReleaseControllerOptions = {}): ControllerSnapshot | undefined {
|
|
326
|
+
const record = this.controllers.get(controllerId);
|
|
327
|
+
if (!record) return undefined;
|
|
328
|
+
const now = this.clock.now();
|
|
329
|
+
if (!options.domains) {
|
|
330
|
+
if (record.releasedAtMs === undefined) {
|
|
331
|
+
record.releasedAtMs = now;
|
|
332
|
+
record.releaseReason = options.reason ?? 'released';
|
|
333
|
+
record.ready = false;
|
|
334
|
+
record.epoch += 1;
|
|
335
|
+
record.authorityEpoch += 1;
|
|
336
|
+
this.changed();
|
|
337
|
+
}
|
|
338
|
+
const snapshot = this.controllerSnapshot(record, this.allocate());
|
|
339
|
+
this.pruneReleasedControllers();
|
|
340
|
+
return snapshot;
|
|
341
|
+
}
|
|
342
|
+
if (record.releasedAtMs !== undefined) return this.controllerSnapshot(record, this.allocate());
|
|
343
|
+
const domains = normalizeDomains(options.domains);
|
|
344
|
+
for (const domain of domains) this.releaseDomain(record, domain);
|
|
345
|
+
record.lastTouchedAtMs = now;
|
|
346
|
+
record.epoch += 1;
|
|
347
|
+
record.authorityEpoch += 1;
|
|
348
|
+
this.changed();
|
|
349
|
+
return this.controllerSnapshot(record, this.allocate());
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
handoff(request: ControllerHandoff): {
|
|
353
|
+
from: ControllerSnapshot;
|
|
354
|
+
to: ControllerSnapshot;
|
|
355
|
+
} {
|
|
356
|
+
const source = this.controllers.get(request.fromControllerId);
|
|
357
|
+
const target = this.controllers.get(request.toControllerId);
|
|
358
|
+
if (!source || !target) throw new Error('Both handoff controllers must be registered.');
|
|
359
|
+
const now = this.clock.now();
|
|
360
|
+
this.sweepExpired(now);
|
|
361
|
+
if (!sameScope(source.spec.scope, target.spec.scope)) {
|
|
362
|
+
throw new Error('Control handoff cannot cross scopes.');
|
|
363
|
+
}
|
|
364
|
+
if (source.releasedAtMs !== undefined || target.releasedAtMs !== undefined) {
|
|
365
|
+
throw new Error('A released controller cannot participate in a handoff.');
|
|
366
|
+
}
|
|
367
|
+
const domains = normalizeDomains(request.domains);
|
|
368
|
+
const sourceBefore = {
|
|
369
|
+
claims: new Map([...source.claims].map(([domain, claim]) => [domain, { ...claim }])),
|
|
370
|
+
excludedDomains: new Set(source.excludedDomains),
|
|
371
|
+
ready: source.ready,
|
|
372
|
+
lastTouchedAtMs: source.lastTouchedAtMs,
|
|
373
|
+
};
|
|
374
|
+
const targetBefore = {
|
|
375
|
+
claims: new Map([...target.claims].map(([domain, claim]) => [domain, { ...claim }])),
|
|
376
|
+
excludedDomains: new Set(target.excludedDomains),
|
|
377
|
+
ready: target.ready,
|
|
378
|
+
lastTouchedAtMs: target.lastTouchedAtMs,
|
|
379
|
+
};
|
|
380
|
+
for (const domain of domains) {
|
|
381
|
+
if (!this.isDeclaredDomain(source, domain) || !this.isDeclaredDomain(target, domain)) {
|
|
382
|
+
throw new Error(`Both controllers must declare handoff domain: ${domain}`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
for (const domain of domains) {
|
|
386
|
+
this.releaseDomain(source, domain);
|
|
387
|
+
}
|
|
388
|
+
this.applyTouch(
|
|
389
|
+
target,
|
|
390
|
+
domains,
|
|
391
|
+
now,
|
|
392
|
+
target.spec.activityLeaseMs ?? this.defaultActivityLeaseMs,
|
|
393
|
+
);
|
|
394
|
+
// Handoff is the atomic READY boundary. A not-ready reservation would
|
|
395
|
+
// release the source without leaving an eligible controller.
|
|
396
|
+
target.ready = true;
|
|
397
|
+
source.lastTouchedAtMs = now;
|
|
398
|
+
target.lastTouchedAtMs = now;
|
|
399
|
+
const provisional = this.allocate(domains);
|
|
400
|
+
const targetControl = provisional.get(target.spec.controllerId);
|
|
401
|
+
if (!targetControl || !containsAllDomains(targetControl.grant, domains)) {
|
|
402
|
+
source.claims = sourceBefore.claims;
|
|
403
|
+
source.excludedDomains = sourceBefore.excludedDomains;
|
|
404
|
+
source.ready = sourceBefore.ready;
|
|
405
|
+
source.lastTouchedAtMs = sourceBefore.lastTouchedAtMs;
|
|
406
|
+
target.claims = targetBefore.claims;
|
|
407
|
+
target.excludedDomains = targetBefore.excludedDomains;
|
|
408
|
+
target.ready = targetBefore.ready;
|
|
409
|
+
target.lastTouchedAtMs = targetBefore.lastTouchedAtMs;
|
|
410
|
+
throw new Error('control_handoff_target_held');
|
|
411
|
+
}
|
|
412
|
+
source.epoch += 1;
|
|
413
|
+
source.authorityEpoch += 1;
|
|
414
|
+
target.epoch += 1;
|
|
415
|
+
this.changed();
|
|
416
|
+
const allocation = this.allocate();
|
|
417
|
+
return {
|
|
418
|
+
from: this.controllerSnapshot(source, allocation),
|
|
419
|
+
to: this.controllerSnapshot(target, allocation),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async runAction<T>(request: RunControlledAction<T>): Promise<ControlRunResult<T>> {
|
|
424
|
+
const domains = request.domains.length === 0 ? [] : normalizeDomains(request.domains);
|
|
425
|
+
if (domains.length === 0) return this.runUncontrolledAction(request, domains);
|
|
426
|
+
let admission = this.admit(request.controllerId, request.scope, domains, {
|
|
427
|
+
touchActivityLease: request.touchActivityLease ?? true,
|
|
428
|
+
});
|
|
429
|
+
if (admission.status === 'deferred') return admission;
|
|
430
|
+
|
|
431
|
+
const capturedScopeEpoch = this.scopeEpoch;
|
|
432
|
+
const capturedController = admission.controller;
|
|
433
|
+
const capturedControllerEpoch = capturedController.authorityEpoch;
|
|
434
|
+
const conflicts = this.conflictingInFlight(admission.controller, request.scope, domains);
|
|
435
|
+
const waitable: InFlightOperation[] = [];
|
|
436
|
+
const notWaitable: InFlightOperation[] = [];
|
|
437
|
+
for (const conflict of conflicts) {
|
|
438
|
+
if (conflict.scopeKey !== this.activeScopeKey
|
|
439
|
+
|| this.precedes(admission.controller, conflict)) {
|
|
440
|
+
waitable.push(conflict);
|
|
441
|
+
} else {
|
|
442
|
+
notWaitable.push(conflict);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (notWaitable.length > 0) {
|
|
446
|
+
return this.deferred('CONTROL_BUSY', request.controllerId, domains, 'provider_in_flight', true,
|
|
447
|
+
notWaitable.map((operation) => operation.controllerId));
|
|
448
|
+
}
|
|
449
|
+
if (waitable.length > 0) {
|
|
450
|
+
const waitMs = request.handoffWaitMs === undefined
|
|
451
|
+
? this.defaultHandoffWaitMs
|
|
452
|
+
: validateBoundedWait('handoffWaitMs', request.handoffWaitMs);
|
|
453
|
+
const waitResult = await this.waitForOperations(waitable, waitMs, () => (
|
|
454
|
+
this.stopping
|
|
455
|
+
|| capturedScopeEpoch !== this.scopeEpoch
|
|
456
|
+
|| this.controllers.get(request.controllerId) !== capturedController
|
|
457
|
+
|| capturedController.authorityEpoch !== capturedControllerEpoch
|
|
458
|
+
));
|
|
459
|
+
if (this.stopping) {
|
|
460
|
+
return this.deferred('CONTROL_STOPPING', request.controllerId, domains, 'scheduler_stopping', false);
|
|
461
|
+
}
|
|
462
|
+
if (capturedScopeEpoch !== this.scopeEpoch || scopeKey(request.scope) !== this.activeScopeKey) {
|
|
463
|
+
return this.deferred('CONTROL_SCOPE_CHANGED', request.controllerId, domains, 'scope_changed', false);
|
|
464
|
+
}
|
|
465
|
+
this.sweepExpired(this.clock.now());
|
|
466
|
+
const current = this.controllers.get(request.controllerId);
|
|
467
|
+
if (current !== capturedController) {
|
|
468
|
+
return this.deferred(
|
|
469
|
+
'CONTROL_RELEASED', request.controllerId, domains, 'controller_changed', false,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
if (current.releasedAtMs !== undefined) {
|
|
473
|
+
return this.deferred(
|
|
474
|
+
current.releaseReason === 'scope_changed'
|
|
475
|
+
? 'CONTROL_SCOPE_CHANGED'
|
|
476
|
+
: 'CONTROL_RELEASED',
|
|
477
|
+
request.controllerId,
|
|
478
|
+
domains,
|
|
479
|
+
current.releaseReason ?? 'released',
|
|
480
|
+
false,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
if (current.authorityEpoch !== capturedControllerEpoch) {
|
|
484
|
+
return this.deferred(
|
|
485
|
+
'CONTROL_RELEASED', request.controllerId, domains, 'authority_changed', false,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
if (waitResult !== 'settled') {
|
|
489
|
+
return this.deferred('CONTROL_BUSY', request.controllerId, domains, 'handoff_timeout', true,
|
|
490
|
+
waitable.map((operation) => operation.controllerId));
|
|
491
|
+
}
|
|
492
|
+
admission = this.admit(request.controllerId, request.scope, domains, {
|
|
493
|
+
touchActivityLease: false,
|
|
494
|
+
});
|
|
495
|
+
if (admission.status === 'deferred') return admission;
|
|
496
|
+
if (this.conflictingInFlight(admission.controller, request.scope, domains).length > 0) {
|
|
497
|
+
return this.deferred('CONTROL_BUSY', request.controllerId, domains, 'provider_race', true);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Install the submission fence before invoking user/provider code. This
|
|
502
|
+
// prevents a synchronous re-entrant Action from bypassing arbitration.
|
|
503
|
+
const operationId = this.operationSequence++;
|
|
504
|
+
let markSettled!: () => void;
|
|
505
|
+
const settled = new Promise<void>((resolve) => { markSettled = resolve; });
|
|
506
|
+
const operation: InFlightOperation = {
|
|
507
|
+
operationId,
|
|
508
|
+
controllerId: request.controllerId,
|
|
509
|
+
controllerEpoch: admission.controller.authorityEpoch,
|
|
510
|
+
scope: cloneScope(request.scope),
|
|
511
|
+
scopeKey: scopeKey(request.scope),
|
|
512
|
+
domains,
|
|
513
|
+
domainSet: requestDomains(domains),
|
|
514
|
+
mutexGroups: [...admission.controller.spec.mutexGroups],
|
|
515
|
+
priority: admission.controller.spec.priority,
|
|
516
|
+
sequence: admission.controller.sequence,
|
|
517
|
+
submittedAtMs: this.clock.now(),
|
|
518
|
+
settled,
|
|
519
|
+
};
|
|
520
|
+
this.inFlight.set(operationId, operation);
|
|
521
|
+
this.changed();
|
|
522
|
+
let providerPromise: Promise<T>;
|
|
523
|
+
try {
|
|
524
|
+
providerPromise = Promise.resolve(request.execute(
|
|
525
|
+
this.executionContext(admission.controller, request.scope, domains),
|
|
526
|
+
));
|
|
527
|
+
} catch (error) {
|
|
528
|
+
providerPromise = Promise.reject(error);
|
|
529
|
+
}
|
|
530
|
+
try {
|
|
531
|
+
const value = await providerPromise;
|
|
532
|
+
return { status: 'executed', controllerId: request.controllerId, domains, value };
|
|
533
|
+
} finally {
|
|
534
|
+
markSettled();
|
|
535
|
+
this.inFlight.delete(operationId);
|
|
536
|
+
this.changed();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
submit<T>(request: RunControlledAction<T>): Promise<ControlRunResult<T>> {
|
|
541
|
+
return this.runAction(request);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
async awaitControl(
|
|
545
|
+
controllerId: string,
|
|
546
|
+
scope: ScopeRef,
|
|
547
|
+
domainsInput: string[],
|
|
548
|
+
options: AwaitControlOptions = {},
|
|
549
|
+
): Promise<AwaitControlResult> {
|
|
550
|
+
const domains = normalizeDomains(domainsInput);
|
|
551
|
+
const lifecycle = this.controllers.get(controllerId);
|
|
552
|
+
const startedAt = this.clock.now();
|
|
553
|
+
const timeoutMs = options.timeoutMs ?? Number.POSITIVE_INFINITY;
|
|
554
|
+
if (timeoutMs < 0 || Number.isNaN(timeoutMs)) throw new TypeError('timeoutMs must not be negative.');
|
|
555
|
+
while (true) {
|
|
556
|
+
if (options.signal?.aborted) {
|
|
557
|
+
return this.deferred(
|
|
558
|
+
'CONTROL_CANCELLED', controllerId, domains, 'await_aborted', false,
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
if (this.stopping) {
|
|
562
|
+
return this.deferred(
|
|
563
|
+
'CONTROL_STOPPING', controllerId, domains, 'scheduler_stopping', false,
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
if (scopeKey(scope) !== this.activeScopeKey) {
|
|
567
|
+
return this.deferred(
|
|
568
|
+
'CONTROL_SCOPE_CHANGED', controllerId, domains, 'scope_changed', false,
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
if (this.controllers.get(controllerId) !== lifecycle) {
|
|
572
|
+
return this.deferred(
|
|
573
|
+
'CONTROL_RELEASED', controllerId, domains, 'controller_changed', false,
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
const admission = this.admit(controllerId, scope, domains, { touchActivityLease: false });
|
|
577
|
+
const conflicts = admission.status === 'deferred'
|
|
578
|
+
? []
|
|
579
|
+
: this.conflictingInFlight(admission.controller, scope, domains);
|
|
580
|
+
if (admission.status !== 'deferred' && conflicts.length === 0) {
|
|
581
|
+
return { status: 'granted', controllerId, domains };
|
|
582
|
+
}
|
|
583
|
+
const unavailable = admission.status === 'deferred'
|
|
584
|
+
? admission
|
|
585
|
+
: this.deferred(
|
|
586
|
+
'CONTROL_BUSY', controllerId, domains, 'provider_in_flight', true,
|
|
587
|
+
conflicts.map((operation) => operation.controllerId),
|
|
588
|
+
);
|
|
589
|
+
if (!unavailable.retryable) return unavailable;
|
|
590
|
+
const elapsed = this.clock.now() - startedAt;
|
|
591
|
+
if (elapsed >= timeoutMs) {
|
|
592
|
+
return this.deferred('CONTROL_BUSY', controllerId, domains, 'await_timeout', true,
|
|
593
|
+
unavailable.blockedBy);
|
|
594
|
+
}
|
|
595
|
+
const remaining = timeoutMs - elapsed;
|
|
596
|
+
const untilTransition = this.nextTransitionDelay();
|
|
597
|
+
const sleepMs = Math.max(0, Math.min(remaining, untilTransition ?? remaining));
|
|
598
|
+
await this.waitForChange(Number.isFinite(sleepMs) ? sleepMs : undefined, options.signal);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
snapshot(): ControlSnapshot {
|
|
603
|
+
this.sweepExpired(this.clock.now());
|
|
604
|
+
const allocation = this.allocate();
|
|
605
|
+
return {
|
|
606
|
+
activeScope: this.activeScope ? cloneScope(this.activeScope) : undefined,
|
|
607
|
+
scopeEpoch: this.scopeEpoch,
|
|
608
|
+
version: this.version,
|
|
609
|
+
stopping: this.stopping,
|
|
610
|
+
nowMs: this.clock.now(),
|
|
611
|
+
controllers: [...this.controllers.values()]
|
|
612
|
+
.sort((left, right) => left.sequence - right.sequence)
|
|
613
|
+
.map((record) => this.controllerSnapshot(record, allocation)),
|
|
614
|
+
inFlight: [...this.inFlight.values()].map((operation) => ({
|
|
615
|
+
operationId: operation.operationId,
|
|
616
|
+
controllerId: operation.controllerId,
|
|
617
|
+
scope: cloneScope(operation.scope),
|
|
618
|
+
domains: [...operation.domains],
|
|
619
|
+
submittedAtMs: operation.submittedAtMs,
|
|
620
|
+
})),
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
beginStopping(): void {
|
|
625
|
+
if (this.stopping) return;
|
|
626
|
+
this.stopping = true;
|
|
627
|
+
this.changed();
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async waitForIdle(): Promise<void> {
|
|
631
|
+
while (this.inFlight.size > 0) {
|
|
632
|
+
await Promise.all([...this.inFlight.values()].map((operation) => operation.settled));
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
private admit(
|
|
637
|
+
controllerId: string,
|
|
638
|
+
scope: ScopeRef,
|
|
639
|
+
domains: string[],
|
|
640
|
+
options: { touchActivityLease: boolean },
|
|
641
|
+
): { status: 'admitted'; controller: ControllerRecord } | ControlDeferred {
|
|
642
|
+
if (this.stopping) {
|
|
643
|
+
return this.deferred('CONTROL_STOPPING', controllerId, domains, 'scheduler_stopping', false);
|
|
644
|
+
}
|
|
645
|
+
if (scopeKey(scope) !== this.activeScopeKey) {
|
|
646
|
+
return this.deferred('CONTROL_SCOPE_CHANGED', controllerId, domains, 'scope_changed', false);
|
|
647
|
+
}
|
|
648
|
+
const record = this.controllers.get(controllerId);
|
|
649
|
+
if (!record) {
|
|
650
|
+
return this.deferred('CONTROL_UNKNOWN_CONTROLLER', controllerId, domains, 'unknown_controller', false);
|
|
651
|
+
}
|
|
652
|
+
this.sweepExpired(this.clock.now());
|
|
653
|
+
if (record.releasedAtMs !== undefined) {
|
|
654
|
+
const code = record.releaseReason === 'scope_changed'
|
|
655
|
+
? 'CONTROL_SCOPE_CHANGED' as const
|
|
656
|
+
: 'CONTROL_RELEASED' as const;
|
|
657
|
+
return this.deferred(code, controllerId, domains, record.releaseReason ?? 'released', false);
|
|
658
|
+
}
|
|
659
|
+
if (!record.ready) {
|
|
660
|
+
return this.deferred('CONTROL_NOT_READY', controllerId, domains, 'controller_not_ready', true);
|
|
661
|
+
}
|
|
662
|
+
for (const domain of domains) {
|
|
663
|
+
if (!this.isDeclaredDomain(record, domain)) {
|
|
664
|
+
return this.deferred('CONTROL_HELD', controllerId, domains, 'domain_not_declared', false);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
let previousClaims: Map<string, DomainClaim> | undefined;
|
|
668
|
+
let previousExcluded: Set<string> | undefined;
|
|
669
|
+
let previousTouchedAt: number | undefined;
|
|
670
|
+
if (options.touchActivityLease && record.spec.lifecycle === 'activity_lease') {
|
|
671
|
+
previousClaims = new Map(
|
|
672
|
+
[...record.claims].map(([domain, claim]) => [domain, { ...claim }]),
|
|
673
|
+
);
|
|
674
|
+
previousExcluded = new Set(record.excludedDomains);
|
|
675
|
+
previousTouchedAt = record.lastTouchedAtMs;
|
|
676
|
+
this.applyTouch(
|
|
677
|
+
record,
|
|
678
|
+
domains,
|
|
679
|
+
this.clock.now(),
|
|
680
|
+
record.spec.activityLeaseMs ?? this.defaultActivityLeaseMs,
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
const allocation = this.allocate(domains);
|
|
684
|
+
const effective = allocation.get(controllerId);
|
|
685
|
+
if (!effective || !containsAllDomains(effective.grant, domains)) {
|
|
686
|
+
if (previousClaims && previousExcluded && previousTouchedAt !== undefined) {
|
|
687
|
+
record.claims = previousClaims;
|
|
688
|
+
record.excludedDomains = previousExcluded;
|
|
689
|
+
record.lastTouchedAtMs = previousTouchedAt;
|
|
690
|
+
}
|
|
691
|
+
return this.deferred(
|
|
692
|
+
'CONTROL_HELD',
|
|
693
|
+
controllerId,
|
|
694
|
+
domains,
|
|
695
|
+
'controller_held',
|
|
696
|
+
true,
|
|
697
|
+
effective ? [...effective.blockers.keys()] : undefined,
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
if (previousClaims) {
|
|
701
|
+
record.lastTouchedAtMs = this.clock.now();
|
|
702
|
+
record.epoch += 1;
|
|
703
|
+
this.changed();
|
|
704
|
+
}
|
|
705
|
+
return { status: 'admitted', controller: record };
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
private async runUncontrolledAction<T>(
|
|
709
|
+
request: RunControlledAction<T>,
|
|
710
|
+
domains: string[],
|
|
711
|
+
): Promise<ControlRunResult<T>> {
|
|
712
|
+
if (this.stopping) {
|
|
713
|
+
return this.deferred(
|
|
714
|
+
'CONTROL_STOPPING', request.controllerId, domains, 'scheduler_stopping', false,
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
if (scopeKey(request.scope) !== this.activeScopeKey) {
|
|
718
|
+
return this.deferred(
|
|
719
|
+
'CONTROL_SCOPE_CHANGED', request.controllerId, domains, 'scope_changed', false,
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
this.sweepExpired(this.clock.now());
|
|
723
|
+
const record = this.controllers.get(request.controllerId);
|
|
724
|
+
if (!record) {
|
|
725
|
+
return this.deferred(
|
|
726
|
+
'CONTROL_UNKNOWN_CONTROLLER', request.controllerId, domains, 'unknown_controller', false,
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
if (record.releasedAtMs !== undefined) {
|
|
730
|
+
return this.deferred(
|
|
731
|
+
record.releaseReason === 'scope_changed' ? 'CONTROL_SCOPE_CHANGED' : 'CONTROL_RELEASED',
|
|
732
|
+
request.controllerId,
|
|
733
|
+
domains,
|
|
734
|
+
record.releaseReason ?? 'released',
|
|
735
|
+
false,
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
if (!record.ready) {
|
|
739
|
+
return this.deferred(
|
|
740
|
+
'CONTROL_NOT_READY', request.controllerId, domains, 'controller_not_ready', true,
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
const capturedScopeEpoch = this.scopeEpoch;
|
|
744
|
+
const capturedController = record;
|
|
745
|
+
const capturedControllerEpoch = record.authorityEpoch;
|
|
746
|
+
const oldScopeOperations = [...this.inFlight.values()].filter(
|
|
747
|
+
(operation) => operation.scopeKey !== this.activeScopeKey,
|
|
748
|
+
);
|
|
749
|
+
if (oldScopeOperations.length > 0) {
|
|
750
|
+
const waitMs = request.handoffWaitMs === undefined
|
|
751
|
+
? this.defaultHandoffWaitMs
|
|
752
|
+
: validateBoundedWait('handoffWaitMs', request.handoffWaitMs);
|
|
753
|
+
const waitResult = await this.waitForOperations(oldScopeOperations, waitMs, () => (
|
|
754
|
+
this.stopping
|
|
755
|
+
|| capturedScopeEpoch !== this.scopeEpoch
|
|
756
|
+
|| this.controllers.get(request.controllerId) !== capturedController
|
|
757
|
+
|| capturedController.authorityEpoch !== capturedControllerEpoch
|
|
758
|
+
));
|
|
759
|
+
if (this.stopping) {
|
|
760
|
+
return this.deferred(
|
|
761
|
+
'CONTROL_STOPPING', request.controllerId, domains, 'scheduler_stopping', false,
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
if (capturedScopeEpoch !== this.scopeEpoch || scopeKey(request.scope) !== this.activeScopeKey) {
|
|
765
|
+
return this.deferred(
|
|
766
|
+
'CONTROL_SCOPE_CHANGED', request.controllerId, domains, 'scope_changed', false,
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
this.sweepExpired(this.clock.now());
|
|
770
|
+
const current = this.controllers.get(request.controllerId);
|
|
771
|
+
if (current !== capturedController) {
|
|
772
|
+
return this.deferred(
|
|
773
|
+
'CONTROL_RELEASED', request.controllerId, domains, 'controller_changed', false,
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
if (current.releasedAtMs !== undefined) {
|
|
777
|
+
return this.deferred(
|
|
778
|
+
current.releaseReason === 'scope_changed'
|
|
779
|
+
? 'CONTROL_SCOPE_CHANGED'
|
|
780
|
+
: 'CONTROL_RELEASED',
|
|
781
|
+
request.controllerId,
|
|
782
|
+
domains,
|
|
783
|
+
current.releaseReason ?? 'released',
|
|
784
|
+
false,
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
if (!current.ready) {
|
|
788
|
+
return this.deferred(
|
|
789
|
+
'CONTROL_NOT_READY', request.controllerId, domains, 'controller_not_ready', true,
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
if (current.authorityEpoch !== capturedControllerEpoch) {
|
|
793
|
+
return this.deferred(
|
|
794
|
+
'CONTROL_RELEASED', request.controllerId, domains, 'authority_changed', false,
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
if (waitResult !== 'settled') {
|
|
798
|
+
return this.deferred(
|
|
799
|
+
'CONTROL_BUSY',
|
|
800
|
+
request.controllerId,
|
|
801
|
+
domains,
|
|
802
|
+
'handoff_timeout',
|
|
803
|
+
true,
|
|
804
|
+
oldScopeOperations.map((operation) => operation.controllerId),
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const operationId = this.operationSequence++;
|
|
810
|
+
let markSettled!: () => void;
|
|
811
|
+
const settled = new Promise<void>((resolve) => { markSettled = resolve; });
|
|
812
|
+
this.inFlight.set(operationId, {
|
|
813
|
+
operationId,
|
|
814
|
+
controllerId: request.controllerId,
|
|
815
|
+
controllerEpoch: record.authorityEpoch,
|
|
816
|
+
scope: cloneScope(request.scope),
|
|
817
|
+
scopeKey: scopeKey(request.scope),
|
|
818
|
+
domains,
|
|
819
|
+
domainSet: requestDomains(domains),
|
|
820
|
+
mutexGroups: [],
|
|
821
|
+
priority: record.spec.priority,
|
|
822
|
+
sequence: record.sequence,
|
|
823
|
+
submittedAtMs: this.clock.now(),
|
|
824
|
+
settled,
|
|
825
|
+
});
|
|
826
|
+
this.changed();
|
|
827
|
+
let providerPromise: Promise<T>;
|
|
828
|
+
try {
|
|
829
|
+
providerPromise = Promise.resolve(request.execute(
|
|
830
|
+
this.executionContext(record, request.scope, domains),
|
|
831
|
+
));
|
|
832
|
+
} catch (error) {
|
|
833
|
+
providerPromise = Promise.reject(error);
|
|
834
|
+
}
|
|
835
|
+
try {
|
|
836
|
+
const value = await providerPromise;
|
|
837
|
+
return { status: 'executed', controllerId: request.controllerId, domains, value };
|
|
838
|
+
} finally {
|
|
839
|
+
markSettled();
|
|
840
|
+
this.inFlight.delete(operationId);
|
|
841
|
+
this.changed();
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
private allocate(extraDomains: string[] = []): Map<string, EffectiveController> {
|
|
846
|
+
const now = this.clock.now();
|
|
847
|
+
this.sweepExpired(now);
|
|
848
|
+
const knownDomains = new Set(extraDomains.filter((domain) => domain !== WILDCARD_DOMAIN));
|
|
849
|
+
for (const record of this.controllers.values()) {
|
|
850
|
+
for (const domain of record.claims.keys()) if (domain !== WILDCARD_DOMAIN) knownDomains.add(domain);
|
|
851
|
+
for (const domain of record.excludedDomains) knownDomains.add(domain);
|
|
852
|
+
}
|
|
853
|
+
const active = [...this.controllers.values()]
|
|
854
|
+
.filter((record) => record.ready
|
|
855
|
+
&& record.releasedAtMs === undefined
|
|
856
|
+
&& scopeKey(record.spec.scope) === this.activeScopeKey)
|
|
857
|
+
.sort(comparePrecedence);
|
|
858
|
+
const result = new Map<string, EffectiveController>();
|
|
859
|
+
const granted: EffectiveController[] = [];
|
|
860
|
+
for (const record of active) {
|
|
861
|
+
const claim = this.claimsFor(record);
|
|
862
|
+
const grant = emptySymbolicDomains();
|
|
863
|
+
const held = emptySymbolicDomains();
|
|
864
|
+
const blockers = new Map<string, ControllerBlockerSnapshot>();
|
|
865
|
+
const mutexBlockers = granted.filter((higher) => hasSharedMutex(record, higher.record));
|
|
866
|
+
if (mutexBlockers.length > 0) {
|
|
867
|
+
Object.assign(held, cloneSymbolicDomains(claim));
|
|
868
|
+
for (const higher of mutexBlockers) {
|
|
869
|
+
blockers.set(higher.record.spec.controllerId, blockerSnapshot(
|
|
870
|
+
higher,
|
|
871
|
+
[],
|
|
872
|
+
record.spec.mutexGroups.filter((group) => higher.record.spec.mutexGroups.includes(group)),
|
|
873
|
+
));
|
|
874
|
+
}
|
|
875
|
+
} else if (record.spec.holdMode === 'all_or_nothing') {
|
|
876
|
+
const domainBlockers = granted.filter((higher) => domainsOverlap(claim, higher.grant));
|
|
877
|
+
if (domainBlockers.length > 0) {
|
|
878
|
+
Object.assign(held, cloneSymbolicDomains(claim));
|
|
879
|
+
for (const higher of domainBlockers) {
|
|
880
|
+
blockers.set(higher.record.spec.controllerId, blockerSnapshot(higher, symbolicList(higher.grant)));
|
|
881
|
+
}
|
|
882
|
+
} else {
|
|
883
|
+
Object.assign(grant, cloneSymbolicDomains(claim));
|
|
884
|
+
}
|
|
885
|
+
} else {
|
|
886
|
+
for (const domain of claim.exact) {
|
|
887
|
+
const higher = granted.find((candidate) => containsDomain(candidate.grant, domain));
|
|
888
|
+
if (higher) {
|
|
889
|
+
held.exact.add(domain);
|
|
890
|
+
blockers.set(higher.record.spec.controllerId, blockerSnapshot(higher, [domain]));
|
|
891
|
+
} else {
|
|
892
|
+
grant.exact.add(domain);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
if (claim.wildcard) {
|
|
896
|
+
const higherWildcard = granted.find((candidate) => candidate.grant.wildcard);
|
|
897
|
+
if (!higherWildcard) {
|
|
898
|
+
grant.wildcard = true;
|
|
899
|
+
grant.excluded = new Set(claim.excluded);
|
|
900
|
+
for (const domain of knownDomains) {
|
|
901
|
+
const higher = granted.find((candidate) => containsDomain(candidate.grant, domain));
|
|
902
|
+
if (!higher) continue;
|
|
903
|
+
grant.excluded.add(domain);
|
|
904
|
+
held.exact.add(domain);
|
|
905
|
+
blockers.set(higher.record.spec.controllerId, blockerSnapshot(higher, [domain]));
|
|
906
|
+
}
|
|
907
|
+
} else {
|
|
908
|
+
held.wildcard = true;
|
|
909
|
+
blockers.set(higherWildcard.record.spec.controllerId,
|
|
910
|
+
blockerSnapshot(higherWildcard, [WILDCARD_DOMAIN]));
|
|
911
|
+
// A higher wildcard may have explicit holes that this wildcard can own.
|
|
912
|
+
for (const domain of knownDomains) {
|
|
913
|
+
if (containsDomain(claim, domain)
|
|
914
|
+
&& !granted.some((candidate) => containsDomain(candidate.grant, domain))) {
|
|
915
|
+
grant.exact.add(domain);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
const effective = { record, claim, grant, held, blockers };
|
|
922
|
+
result.set(record.spec.controllerId, effective);
|
|
923
|
+
if (symbolicHasAny(grant)) granted.push(effective);
|
|
924
|
+
}
|
|
925
|
+
return result;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Build a revalidation guard tied to the exact controller record admitted
|
|
930
|
+
* for this attempt. Object identity closes the release/re-register (ABA)
|
|
931
|
+
* hole without treating an ordinary lease touch as authority revocation.
|
|
932
|
+
*/
|
|
933
|
+
private executionContext(
|
|
934
|
+
capturedController: ControllerRecord,
|
|
935
|
+
scope: ScopeRef,
|
|
936
|
+
domains: string[],
|
|
937
|
+
): ControlExecutionContext {
|
|
938
|
+
const capturedScopeEpoch = this.scopeEpoch;
|
|
939
|
+
const capturedAuthorityEpoch = capturedController.authorityEpoch;
|
|
940
|
+
const controllerId = capturedController.spec.controllerId;
|
|
941
|
+
return {
|
|
942
|
+
revalidate: () => {
|
|
943
|
+
if (this.stopping) {
|
|
944
|
+
return this.deferred(
|
|
945
|
+
'CONTROL_STOPPING', controllerId, domains, 'scheduler_stopping', false,
|
|
946
|
+
);
|
|
947
|
+
}
|
|
948
|
+
if (capturedScopeEpoch !== this.scopeEpoch || scopeKey(scope) !== this.activeScopeKey) {
|
|
949
|
+
return this.deferred(
|
|
950
|
+
'CONTROL_SCOPE_CHANGED', controllerId, domains, 'scope_changed', false,
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
this.sweepExpired(this.clock.now());
|
|
954
|
+
const current = this.controllers.get(controllerId);
|
|
955
|
+
if (current !== capturedController) {
|
|
956
|
+
return this.deferred(
|
|
957
|
+
'CONTROL_RELEASED', controllerId, domains, 'controller_changed', false,
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
if (current.authorityEpoch !== capturedAuthorityEpoch) {
|
|
961
|
+
return this.deferred(
|
|
962
|
+
'CONTROL_RELEASED', controllerId, domains, 'authority_changed', false,
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
if (current.releasedAtMs !== undefined) {
|
|
966
|
+
return this.deferred(
|
|
967
|
+
current.releaseReason === 'scope_changed'
|
|
968
|
+
? 'CONTROL_SCOPE_CHANGED'
|
|
969
|
+
: 'CONTROL_RELEASED',
|
|
970
|
+
controllerId,
|
|
971
|
+
domains,
|
|
972
|
+
current.releaseReason ?? 'released',
|
|
973
|
+
false,
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
if (!current.ready) {
|
|
977
|
+
return this.deferred(
|
|
978
|
+
'CONTROL_NOT_READY', controllerId, domains, 'controller_not_ready', true,
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
// A no-control Action still needs lifecycle and scope validation, but
|
|
982
|
+
// intentionally participates in no domain or mutex arbitration.
|
|
983
|
+
if (domains.length === 0) return undefined;
|
|
984
|
+
const effective = this.allocate(domains).get(controllerId);
|
|
985
|
+
if (!effective || !containsAllDomains(effective.grant, domains)) {
|
|
986
|
+
return this.deferred(
|
|
987
|
+
'CONTROL_HELD',
|
|
988
|
+
controllerId,
|
|
989
|
+
domains,
|
|
990
|
+
'controller_held',
|
|
991
|
+
true,
|
|
992
|
+
effective ? [...effective.blockers.keys()] : undefined,
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
return undefined;
|
|
996
|
+
},
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
private claimsFor(record: ControllerRecord): SymbolicDomains {
|
|
1001
|
+
const claims = emptySymbolicDomains();
|
|
1002
|
+
for (const domain of record.claims.keys()) {
|
|
1003
|
+
if (domain === WILDCARD_DOMAIN) claims.wildcard = true;
|
|
1004
|
+
else claims.exact.add(domain);
|
|
1005
|
+
}
|
|
1006
|
+
claims.excluded = new Set(record.excludedDomains);
|
|
1007
|
+
return claims;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
private controllerSnapshot(
|
|
1011
|
+
record: ControllerRecord,
|
|
1012
|
+
allocation: Map<string, EffectiveController>,
|
|
1013
|
+
): ControllerSnapshot {
|
|
1014
|
+
const effective = allocation.get(record.spec.controllerId);
|
|
1015
|
+
const claim = effective?.claim ?? this.claimsFor(record);
|
|
1016
|
+
const grant = effective?.grant ?? emptySymbolicDomains();
|
|
1017
|
+
const held = effective?.held ?? emptySymbolicDomains();
|
|
1018
|
+
let state: ControllerSnapshot['state'];
|
|
1019
|
+
if (record.releasedAtMs !== undefined) {
|
|
1020
|
+
state = record.releaseReason === 'deadline_expired' ? 'expired' : 'released';
|
|
1021
|
+
} else if (!record.ready) {
|
|
1022
|
+
state = 'registered';
|
|
1023
|
+
} else if (!symbolicHasAny(claim)) {
|
|
1024
|
+
state = 'idle';
|
|
1025
|
+
} else if (!symbolicHasAny(grant)) {
|
|
1026
|
+
state = 'held';
|
|
1027
|
+
} else if (symbolicHasAny(held)) {
|
|
1028
|
+
state = 'partially_active';
|
|
1029
|
+
} else {
|
|
1030
|
+
state = 'active';
|
|
1031
|
+
}
|
|
1032
|
+
const expiries = [...record.claims.values()]
|
|
1033
|
+
.map((entry) => entry.expiresAtMs)
|
|
1034
|
+
.filter((value): value is number => value !== undefined);
|
|
1035
|
+
return {
|
|
1036
|
+
controllerId: record.spec.controllerId,
|
|
1037
|
+
kind: record.spec.kind,
|
|
1038
|
+
scope: cloneScope(record.spec.scope),
|
|
1039
|
+
priority: record.spec.priority,
|
|
1040
|
+
sequence: record.sequence,
|
|
1041
|
+
epoch: record.epoch,
|
|
1042
|
+
lifecycle: record.spec.lifecycle,
|
|
1043
|
+
state,
|
|
1044
|
+
ready: record.ready,
|
|
1045
|
+
holdMode: record.spec.holdMode,
|
|
1046
|
+
declaredDomains: [...record.spec.domains],
|
|
1047
|
+
claimedDomains: symbolicList(claim),
|
|
1048
|
+
grantedDomains: symbolicList(grant),
|
|
1049
|
+
heldDomains: symbolicList(held),
|
|
1050
|
+
excludedDomains: [...record.excludedDomains].sort(),
|
|
1051
|
+
mutexGroups: [...record.spec.mutexGroups],
|
|
1052
|
+
blockers: effective ? [...effective.blockers.values()] : [],
|
|
1053
|
+
registeredAtMs: record.registeredAtMs,
|
|
1054
|
+
lastTouchedAtMs: record.lastTouchedAtMs,
|
|
1055
|
+
nextExpiryAtMs: expiries.length > 0 ? Math.min(...expiries) : undefined,
|
|
1056
|
+
deadlineAtMs: record.spec.deadlineAtMs,
|
|
1057
|
+
releasedAtMs: record.releasedAtMs,
|
|
1058
|
+
releaseReason: record.releaseReason,
|
|
1059
|
+
metadata: record.spec.metadata,
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
private sweepExpired(now: number): void {
|
|
1064
|
+
let changed = false;
|
|
1065
|
+
for (const record of this.controllers.values()) {
|
|
1066
|
+
if (record.releasedAtMs !== undefined) continue;
|
|
1067
|
+
if (record.spec.lifecycle === 'finite'
|
|
1068
|
+
&& record.spec.deadlineAtMs !== undefined
|
|
1069
|
+
&& now >= record.spec.deadlineAtMs) {
|
|
1070
|
+
record.releasedAtMs = record.spec.deadlineAtMs;
|
|
1071
|
+
record.releaseReason = 'deadline_expired';
|
|
1072
|
+
record.ready = false;
|
|
1073
|
+
record.epoch += 1;
|
|
1074
|
+
record.authorityEpoch += 1;
|
|
1075
|
+
changed = true;
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
for (const [domain, claim] of record.claims) {
|
|
1079
|
+
if (claim.expiresAtMs !== undefined && now >= claim.expiresAtMs) {
|
|
1080
|
+
record.claims.delete(domain);
|
|
1081
|
+
record.epoch += 1;
|
|
1082
|
+
record.authorityEpoch += 1;
|
|
1083
|
+
changed = true;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
if (changed) {
|
|
1088
|
+
this.pruneReleasedControllers();
|
|
1089
|
+
this.changed();
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
private pruneReleasedControllers(): void {
|
|
1094
|
+
const released = [...this.controllers.entries()]
|
|
1095
|
+
.filter(([, record]) => record.releasedAtMs !== undefined)
|
|
1096
|
+
.sort(([, left], [, right]) => (
|
|
1097
|
+
(left.releasedAtMs ?? 0) - (right.releasedAtMs ?? 0)
|
|
1098
|
+
|| left.sequence - right.sequence
|
|
1099
|
+
));
|
|
1100
|
+
for (const [controllerId] of released.slice(
|
|
1101
|
+
0,
|
|
1102
|
+
Math.max(0, released.length - MAX_RELEASED_TOMBSTONES),
|
|
1103
|
+
)) {
|
|
1104
|
+
this.controllers.delete(controllerId);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
private isDeclaredDomain(record: ControllerRecord, domain: string): boolean {
|
|
1109
|
+
return domain === WILDCARD_DOMAIN
|
|
1110
|
+
? record.spec.domains.includes(WILDCARD_DOMAIN)
|
|
1111
|
+
: record.spec.domains.includes(domain) || record.spec.domains.includes(WILDCARD_DOMAIN);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
private releaseDomain(record: ControllerRecord, domain: string): void {
|
|
1115
|
+
if (domain === WILDCARD_DOMAIN) {
|
|
1116
|
+
record.claims.clear();
|
|
1117
|
+
record.excludedDomains.clear();
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
record.claims.delete(domain);
|
|
1121
|
+
if (record.claims.has(WILDCARD_DOMAIN)) record.excludedDomains.add(domain);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
private applyTouch(
|
|
1125
|
+
record: ControllerRecord,
|
|
1126
|
+
domains: string[],
|
|
1127
|
+
now: number,
|
|
1128
|
+
activityLeaseMs: number,
|
|
1129
|
+
): void {
|
|
1130
|
+
for (const domain of domains) {
|
|
1131
|
+
if (!this.isDeclaredDomain(record, domain)) {
|
|
1132
|
+
throw new Error(
|
|
1133
|
+
`Controller ${record.spec.controllerId} did not declare control domain: ${domain}`,
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
if (domain === WILDCARD_DOMAIN) record.excludedDomains.clear();
|
|
1137
|
+
else record.excludedDomains.delete(domain);
|
|
1138
|
+
record.claims.set(domain, record.spec.lifecycle === 'activity_lease'
|
|
1139
|
+
? { expiresAtMs: now + activityLeaseMs }
|
|
1140
|
+
: record.spec.lifecycle === 'finite'
|
|
1141
|
+
? { expiresAtMs: record.spec.deadlineAtMs }
|
|
1142
|
+
: {});
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
private conflictingInFlight(
|
|
1147
|
+
controller: ControllerRecord,
|
|
1148
|
+
scope: ScopeRef,
|
|
1149
|
+
domains: string[],
|
|
1150
|
+
): InFlightOperation[] {
|
|
1151
|
+
const requested = requestDomains(domains);
|
|
1152
|
+
return [...this.inFlight.values()].filter((operation) => {
|
|
1153
|
+
// Scope transition is a global barrier: no provider from two logical
|
|
1154
|
+
// segments may overlap, even when their named domains differ.
|
|
1155
|
+
if (operation.scopeKey !== scopeKey(scope)) return true;
|
|
1156
|
+
return domainsOverlap(requested, operation.domainSet)
|
|
1157
|
+
|| (operation.sequence !== controller.sequence
|
|
1158
|
+
&& controller.spec.mutexGroups.some((group) => operation.mutexGroups.includes(group)));
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
private precedes(controller: ControllerRecord, operation: InFlightOperation): boolean {
|
|
1163
|
+
return controller.spec.priority > operation.priority
|
|
1164
|
+
|| (controller.spec.priority === operation.priority
|
|
1165
|
+
&& controller.sequence < operation.sequence);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
private async waitForOperations(
|
|
1169
|
+
operations: InFlightOperation[],
|
|
1170
|
+
timeoutMs: number,
|
|
1171
|
+
shouldInterrupt: () => boolean,
|
|
1172
|
+
): Promise<OperationWaitResult> {
|
|
1173
|
+
if (operations.length === 0) return 'settled';
|
|
1174
|
+
if (shouldInterrupt()) return 'interrupted';
|
|
1175
|
+
if (timeoutMs <= 0) return 'timeout';
|
|
1176
|
+
|
|
1177
|
+
let resolveInterrupt!: () => void;
|
|
1178
|
+
const interrupted = new Promise<void>((resolve) => { resolveInterrupt = resolve; });
|
|
1179
|
+
const waiter = (): boolean => {
|
|
1180
|
+
if (!shouldInterrupt()) return false;
|
|
1181
|
+
resolveInterrupt();
|
|
1182
|
+
return true;
|
|
1183
|
+
};
|
|
1184
|
+
this.changeWaiters.add(waiter);
|
|
1185
|
+
try {
|
|
1186
|
+
return await Promise.race([
|
|
1187
|
+
Promise.all(operations.map((operation) => operation.settled)).then(() => 'settled' as const),
|
|
1188
|
+
this.clock.sleep(timeoutMs).then(() => 'timeout' as const),
|
|
1189
|
+
interrupted.then(() => 'interrupted' as const),
|
|
1190
|
+
]);
|
|
1191
|
+
} finally {
|
|
1192
|
+
this.changeWaiters.delete(waiter);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
private nextTransitionDelay(): number | undefined {
|
|
1197
|
+
const now = this.clock.now();
|
|
1198
|
+
let next: number | undefined;
|
|
1199
|
+
for (const record of this.controllers.values()) {
|
|
1200
|
+
if (record.releasedAtMs !== undefined) continue;
|
|
1201
|
+
if (record.spec.deadlineAtMs !== undefined && record.spec.deadlineAtMs > now) {
|
|
1202
|
+
next = Math.min(next ?? Number.POSITIVE_INFINITY, record.spec.deadlineAtMs);
|
|
1203
|
+
}
|
|
1204
|
+
for (const claim of record.claims.values()) {
|
|
1205
|
+
if (claim.expiresAtMs !== undefined && claim.expiresAtMs > now) {
|
|
1206
|
+
next = Math.min(next ?? Number.POSITIVE_INFINITY, claim.expiresAtMs);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return next === undefined ? undefined : next - now;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
private async waitForChange(milliseconds?: number, signal?: AbortSignal): Promise<void> {
|
|
1214
|
+
if (signal?.aborted) return;
|
|
1215
|
+
let resolveChange!: () => void;
|
|
1216
|
+
const changed = new Promise<void>((resolve) => { resolveChange = resolve; });
|
|
1217
|
+
this.changeWaiters.add(resolveChange);
|
|
1218
|
+
let abortHandler: (() => void) | undefined;
|
|
1219
|
+
const aborted = new Promise<void>((resolve) => {
|
|
1220
|
+
abortHandler = resolve;
|
|
1221
|
+
signal?.addEventListener('abort', abortHandler, { once: true });
|
|
1222
|
+
});
|
|
1223
|
+
const waits: Promise<void>[] = [changed, aborted];
|
|
1224
|
+
if (milliseconds !== undefined) waits.push(this.clock.sleep(milliseconds));
|
|
1225
|
+
try {
|
|
1226
|
+
await Promise.race(waits);
|
|
1227
|
+
} finally {
|
|
1228
|
+
this.changeWaiters.delete(resolveChange);
|
|
1229
|
+
if (abortHandler) signal?.removeEventListener('abort', abortHandler);
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
private changed(): void {
|
|
1234
|
+
this.version += 1;
|
|
1235
|
+
for (const waiter of [...this.changeWaiters]) {
|
|
1236
|
+
if (waiter() !== false) this.changeWaiters.delete(waiter);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
private deferred(
|
|
1241
|
+
code: ControlDeferred['code'],
|
|
1242
|
+
controllerId: string,
|
|
1243
|
+
domains: string[],
|
|
1244
|
+
reason: string,
|
|
1245
|
+
retryable: boolean,
|
|
1246
|
+
blockedBy?: string[],
|
|
1247
|
+
): ControlDeferred {
|
|
1248
|
+
return {
|
|
1249
|
+
status: 'deferred',
|
|
1250
|
+
code,
|
|
1251
|
+
controllerId,
|
|
1252
|
+
domains: [...domains],
|
|
1253
|
+
retryable,
|
|
1254
|
+
reason,
|
|
1255
|
+
...(blockedBy && blockedBy.length > 0
|
|
1256
|
+
? { blockedBy: [...new Set(blockedBy)].sort() }
|
|
1257
|
+
: {}),
|
|
1258
|
+
};
|
|
1259
|
+
}
|
|
1260
|
+
}
|