@myclaw163/clawclaw-cli 0.7.15 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QUICKSTART_HERMES.md +1 -1
- package/QUICKSTART_OPENCLAW.md +3 -11
- package/QUICKSTART_OTHER.md +8 -8
- package/README.md +18 -9
- package/bin/clawclaw-channel.mjs +13 -0
- package/docs/configuration.md +86 -33
- package/docs/custom-modules/README.md +148 -0
- package/docs/custom-modules/api-reference.md +767 -0
- package/docs/custom-modules/behavior.md +318 -0
- package/docs/custom-modules/perception.md +305 -0
- package/docs/custom-modules/trigger.md +292 -0
- package/docs/desktop-event-push.md +6 -4
- package/docs/runtime/README.md +28 -10
- package/docs/runtime/agent-notifications.md +48 -29
- package/docs/runtime/behavior-runtime.md +139 -0
- package/docs/runtime/components-and-boundaries.md +89 -173
- package/docs/runtime/events-and-state.md +55 -66
- package/docs/runtime/official-perception-modules.md +17 -9
- package/docs/runtime/owner-lifecycle-and-observability.md +62 -39
- package/docs/runtime/perception-replay.md +6 -6
- package/docs/runtime/short-commands-and-capabilities.md +35 -37
- package/package.json +7 -4
- package/scripts/check-skill-command-surface.mjs +54 -10
- package/scripts/compare-perception-replays.test.ts +10 -10
- package/scripts/compare-perception-replays.ts +14 -10
- package/scripts/replay-perception.ts +3 -3
- package/scripts/watch-perception-events.test.ts +1 -1
- package/skills/clawclaw/SKILL.md +97 -59
- package/skills/clawclaw/references/BEHAVIORS.md +154 -0
- package/skills/clawclaw/references/CHANNELS.md +115 -0
- package/skills/clawclaw/references/CHATTERBOX.md +3 -2
- package/skills/clawclaw/references/COMMANDS.md +108 -58
- package/skills/clawclaw/references/GAME-MECHANICS.md +4 -3
- package/skills/clawclaw/references/HUB.md +27 -23
- package/skills/clawclaw/references/STREAM.md +18 -14
- package/skills/clawclaw/references/TACTICS.md +2 -2
- package/src/behaviors/official-finite/do-task.ts +141 -0
- package/src/behaviors/official-finite/evade.ts +78 -0
- package/src/behaviors/official-finite/find-player.ts +50 -0
- package/src/behaviors/official-finite/follow-player.ts +70 -0
- package/src/behaviors/official-finite/go-to.ts +69 -0
- package/src/behaviors/official-finite/hide-temporarily.ts +77 -0
- package/src/behaviors/official-finite/hold-position.ts +48 -0
- package/src/behaviors/official-finite/hunt-player.ts +47 -0
- package/src/behaviors/official-finite/index.test.ts +202 -0
- package/src/behaviors/official-finite/index.ts +47 -0
- package/src/behaviors/official-finite/inspect-corpse.ts +64 -0
- package/src/behaviors/official-finite/patrol-area.ts +80 -0
- package/src/behaviors/official-finite/report-corpse.ts +73 -0
- package/src/behaviors/official-finite/seek-kill-opportunity.ts +152 -0
- package/src/behaviors/official-finite/shared.ts +27 -0
- package/src/behaviors/official-finite/trigger-alarm.ts +53 -0
- package/src/channel/claude-channel-ipc-server.ts +295 -0
- package/src/channel/claude-channel-ipc.ts +152 -0
- package/src/channel/claude-channel-server.test.ts +202 -0
- package/src/channel/claude-channel-server.ts +221 -0
- package/src/cli.ts +4 -2
- package/src/commands/account.test.ts +17 -17
- package/src/commands/behavior.test.ts +158 -0
- package/src/commands/behavior.ts +443 -0
- package/src/commands/communication.test.ts +12 -0
- package/src/commands/communication.ts +6 -3
- package/src/commands/data.test.ts +1 -1
- package/src/commands/data.ts +1 -1
- package/src/commands/events-canonical-state.test.ts +221 -2
- package/src/commands/events.ts +205 -12
- package/src/commands/game/index.test.ts +172 -3
- package/src/commands/game/index.ts +69 -32
- package/src/commands/hub.test.ts +25 -14
- package/src/commands/hub.ts +15 -19
- package/src/commands/load.test.ts +228 -6
- package/src/commands/load.ts +102 -18
- package/src/commands/memory.ts +1 -1
- package/src/commands/peek.ts +1 -1
- package/src/commands/perception.test.ts +30 -1
- package/src/commands/perception.ts +15 -17
- package/src/commands/trigger.test.ts +140 -0
- package/src/commands/trigger.ts +314 -0
- package/src/lib/account-guidance.test.ts +4 -0
- package/src/lib/account-guidance.ts +1 -1
- package/src/lib/behavior-export.test.ts +282 -0
- package/src/lib/behavior-export.ts +514 -0
- package/src/lib/game-client.test.ts +4 -17
- package/src/lib/game-client.ts +3 -0
- package/src/lib/gameplay-config.test.ts +258 -37
- package/src/lib/gameplay-config.ts +175 -35
- package/src/lib/hub-install.test.ts +19 -41
- package/src/lib/hub-install.ts +10 -38
- package/src/lib/hub-reminder.ts +7 -9
- package/src/lib/init-command.ts +1 -1
- package/src/lib/official-resource-sync.test.ts +199 -0
- package/src/lib/official-resource-sync.ts +221 -0
- package/src/lib/perception-config.test.ts +9 -4
- package/src/lib/perception-config.ts +10 -13
- package/src/lib/perception-export.test.ts +145 -0
- package/src/lib/perception-export.ts +178 -0
- package/src/lib/trigger-config.test.ts +102 -0
- package/src/lib/trigger-config.ts +217 -0
- package/src/lib/user-data.test.ts +23 -6
- package/src/lib/user-data.ts +15 -123
- package/src/pipeline/canonical-event-presentation.test.ts +5 -5
- package/src/pipeline/canonical-event-presentation.ts +13 -5
- package/src/pipeline/event-format.test.ts +106 -3
- package/src/pipeline/event-format.ts +172 -4
- package/src/pipeline/event-hints.test.ts +57 -4
- package/src/pipeline/event-hints.ts +34 -8
- package/src/runtime/canonical-monitor.test.ts +2 -0
- package/src/runtime/canonical-monitor.ts +6 -2
- package/src/runtime/clawclaw/agent-event-policy.test.ts +23 -2
- package/src/runtime/clawclaw/agent-event-policy.ts +12 -5
- package/src/runtime/clawclaw/behavior-decision.test.ts +29 -0
- package/src/runtime/clawclaw/behavior-decision.ts +82 -0
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +175 -0
- package/src/runtime/clawclaw/behavior-input-fields.ts +270 -0
- package/src/runtime/clawclaw/behavior-input.test.ts +151 -0
- package/src/runtime/clawclaw/behavior-input.ts +70 -0
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +59 -0
- package/src/runtime/clawclaw/behavior-observation-memory.ts +142 -0
- package/src/runtime/clawclaw/behavior-read-api.test.ts +123 -0
- package/src/runtime/clawclaw/behavior-read-api.ts +158 -0
- package/src/runtime/clawclaw/{plugins/perception/visual-state-projector.ts → observation-frame-projector.ts} +44 -25
- package/src/runtime/clawclaw/owner.test.ts +187 -0
- package/src/runtime/clawclaw/owner.ts +431 -69
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +304 -0
- package/src/runtime/clawclaw/plugins/action/action-plugin.ts +286 -41
- package/src/runtime/clawclaw/plugins/action/automated-action-normalization.ts +82 -0
- package/src/runtime/clawclaw/plugins/assembly.test.ts +135 -29
- package/src/runtime/clawclaw/plugins/assembly.ts +54 -28
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +468 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +40 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.ts +30 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +152 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +156 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.ts +104 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-manifest.ts +173 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +182 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.ts +223 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-normalization.ts +157 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +848 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +1347 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +212 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +174 -0
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +621 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +195 -0
- package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +180 -0
- package/src/runtime/clawclaw/plugins/behavior/index.ts +11 -0
- package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +766 -0
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +283 -0
- package/src/runtime/clawclaw/plugins/{strategy/thread-strategy-executor.ts → behavior/thread-behavior-executor.ts} +109 -121
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +80 -0
- package/src/runtime/clawclaw/plugins/behavior-input-source.ts +247 -0
- package/src/runtime/clawclaw/plugins/{strategy → game-query}/context-capabilities.ts +1 -1
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +13 -10
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +26 -14
- package/src/runtime/clawclaw/plugins/game-query/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/index.ts +3 -1
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +239 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +100 -0
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +346 -0
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +230 -12
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +118 -90
- package/src/runtime/clawclaw/plugins/mailbox/consumer-ids.ts +5 -0
- package/src/runtime/clawclaw/plugins/mailbox/index.ts +3 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +1 -1
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +4 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +87 -0
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +70 -12
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +94 -11
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +1229 -345
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +479 -35
- package/src/runtime/clawclaw/plugins/perception/contracts.ts +4 -3
- package/src/runtime/clawclaw/plugins/perception/index.ts +1 -1
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +23 -13
- package/src/runtime/clawclaw/plugins/perception/module-runner.ts +11 -9
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +39 -38
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +92 -0
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +248 -0
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +39 -37
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +22 -21
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +15 -9
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +3 -3
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +33 -18
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +22 -16
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +6 -7
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +18 -12
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +14 -10
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +20 -14
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +5 -6
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +49 -34
- package/src/runtime/clawclaw/plugins/perception/official-module-ids.ts +1 -0
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +126 -20
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.ts +24 -3
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-worker.ts +59 -46
- package/src/runtime/clawclaw/plugins/perception/perception-module-loader.ts +27 -5
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +381 -49
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +267 -245
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +98 -0
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +89 -16
- package/src/runtime/clawclaw/plugins/perception/perception-test-fixtures.ts +34 -0
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +27 -15
- package/src/runtime/clawclaw/plugins/perception/replay.ts +10 -5
- package/src/runtime/clawclaw/plugins/perception/shared/math.ts +1 -107
- package/src/runtime/clawclaw/plugins/perception/spatial-facts.ts +7 -10
- package/src/runtime/clawclaw/plugins/plugin-api-version.ts +112 -0
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +389 -559
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +56 -5
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.ts +135 -26
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +249 -16
- package/src/runtime/clawclaw/plugins/source/source-plugin.ts +99 -2
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +212 -0
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.ts +259 -0
- package/src/runtime/clawclaw/plugins/trigger/contracts.ts +97 -0
- package/src/runtime/clawclaw/plugins/trigger/index.ts +4 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-module-loader.ts +118 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +548 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +655 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +169 -0
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.ts +457 -0
- package/src/runtime/clawclaw/runtime.ts +8 -0
- package/src/runtime/clawclaw/spatial-api.ts +76 -0
- package/src/runtime/clawclaw-event-catalog.test.ts +46 -1
- package/src/runtime/clawclaw-event-catalog.ts +40 -3
- package/src/runtime/framework/capability-registry.test.ts +196 -55
- package/src/runtime/framework/capability-registry.ts +99 -17
- package/src/runtime/framework/contracts.ts +30 -4
- package/src/runtime/framework/control/control-scheduler.ts +13 -5
- package/src/runtime/framework/control/types.ts +11 -4
- package/src/runtime/framework/control-coordinator.test.ts +95 -80
- package/src/runtime/framework/events/event-delivery-service.ts +4 -2
- package/src/runtime/framework/events/event-ingress.test.ts +25 -0
- package/src/runtime/framework/events/event-ingress.ts +2 -1
- package/src/runtime/framework/events/event-repository.ts +24 -2
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +124 -8
- package/src/runtime/framework/events/memory-event-repository.ts +92 -28
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +604 -2
- package/src/runtime/framework/events/sqlite-event-repository.ts +531 -21
- package/src/runtime/framework/events/types.ts +24 -8
- package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +38 -1
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +233 -7
- package/src/runtime/framework/modules/runtime-module-loader.ts +69 -4
- package/src/runtime/framework/plugins/contracts.ts +12 -18
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +81 -71
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +38 -20
- package/src/runtime/framework/runtime.ts +20 -23
- package/src/runtime/host-monitor-rate-gate.test.ts +73 -0
- package/src/runtime/host-monitor-rate-gate.ts +123 -0
- package/src/runtime/mailbox-host-relay.test.ts +45 -0
- package/src/runtime/mailbox-host-relay.ts +25 -0
- package/src/runtime/observability/factory.ts +4 -4
- package/src/runtime/observability/observability.test.ts +25 -3
- package/src/runtime/observability/reporter.ts +1 -1
- package/src/runtime/observability/types.ts +2 -2
- package/src/runtime/owner-control.ts +10 -2
- package/src/runtime/runtime-logger.test.ts +1 -1
- package/src/runtime/runtime-logger.ts +4 -6
- package/src/runtime/runtime.test.ts +88 -22
- package/src/sdk/action.ts +2 -2
- package/src/sdk/behavior-plugin/index.test.ts +74 -0
- package/src/sdk/behavior-plugin/index.ts +335 -0
- package/src/sdk/behavior-toolkit/corpse-search.ts +87 -0
- package/src/sdk/behavior-toolkit/escape.ts +40 -0
- package/src/sdk/behavior-toolkit/index.ts +7 -0
- package/src/sdk/behavior-toolkit/kill-flow.ts +299 -0
- package/src/sdk/behavior-toolkit/model.ts +263 -0
- package/src/sdk/behavior-toolkit/move-gate.ts +52 -0
- package/src/sdk/behavior-toolkit/patrol.ts +53 -0
- package/src/sdk/behavior-toolkit/search.ts +102 -0
- package/src/sdk/index.test.ts +45 -0
- package/src/sdk/index.ts +67 -73
- package/src/sdk/module-input/index.test.ts +49 -0
- package/src/sdk/module-input/index.ts +229 -0
- package/src/sdk/perception-plugin/index.test.ts +2 -2
- package/src/sdk/perception-plugin/index.ts +51 -142
- package/src/sdk/perception-plugin/math.ts +107 -0
- package/src/sdk/trigger-plugin/index.test.ts +41 -0
- package/src/sdk/trigger-plugin/index.ts +128 -0
- package/src/strategies/avoid-lone.ts +21 -10
- package/src/strategies/avoid-players.ts +26 -16
- package/src/strategies/corpse-patrol.ts +25 -14
- package/src/strategies/crab-sabotage.ts +20 -9
- package/src/strategies/find-player.ts +33 -21
- package/src/strategies/game-utils.ts +1 -1
- package/src/strategies/goals/crab-sabotage-top.test.ts +145 -0
- package/src/strategies/goals/crab-sabotage-top.ts +32 -69
- package/src/strategies/hide.ts +25 -15
- package/src/strategies/kill-frenzy.ts +21 -10
- package/src/strategies/kill-lone.ts +27 -17
- package/src/strategies/kill-target.ts +33 -22
- package/src/strategies/lone-kill-task.ts +20 -9
- package/src/strategies/move-room.ts +29 -18
- package/src/strategies/official-long-behavior.test.ts +89 -0
- package/src/strategies/paradise-fish.ts +29 -18
- package/src/strategies/pathfind/astar-path.test.ts +127 -2
- package/src/strategies/pathfind/astar-path.ts +136 -26
- package/src/strategies/patrol.ts +21 -10
- package/src/strategies/player-targets.test.ts +10 -0
- package/src/strategies/player-targets.ts +13 -0
- package/src/strategies/report-patrol.ts +21 -10
- package/src/strategies/shrimp-memory.ts +30 -19
- package/src/strategies/social-task.ts +34 -23
- package/src/strategies/task-kill-report.ts +33 -22
- package/src/strategies/task-only.test.ts +99 -85
- package/src/strategies/task-only.ts +23 -12
- package/src/strategies/task-report.ts +25 -14
- package/src/strategies/types.ts +8 -8
- package/src/strategies/warrior-memory.ts +29 -19
- package/docs/runtime/strategy-runtime.md +0 -111
- package/docs//350/207/252/345/256/232/344/271/211/346/204/237/347/237/245.md +0 -502
- package/docs//350/207/252/345/256/232/344/271/211/347/255/226/347/225/245.md +0 -395
- package/skills/clawclaw/references/STRATEGIES.md +0 -76
- package/src/commands/strategy.test.ts +0 -209
- package/src/commands/strategy.ts +0 -242
- package/src/lib/strategy-export.test.ts +0 -406
- package/src/lib/strategy-export.ts +0 -381
- package/src/runtime/clawclaw/plugins/perception/official-modules.ts +0 -31
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.test.ts +0 -177
- package/src/runtime/clawclaw/plugins/strategy/examples/task-only-strategy.ts +0 -191
- package/src/runtime/clawclaw/plugins/strategy/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/strategy/local-strategy-executor.ts +0 -412
- package/src/runtime/clawclaw/plugins/strategy/strategy-action.ts +0 -80
- package/src/runtime/clawclaw/plugins/strategy/strategy-analysis-thread-host.ts +0 -183
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor-types.ts +0 -29
- package/src/runtime/clawclaw/plugins/strategy/strategy-executor.test.ts +0 -693
- package/src/runtime/clawclaw/plugins/strategy/strategy-game-api.ts +0 -57
- package/src/runtime/clawclaw/plugins/strategy/strategy-module-loader.ts +0 -168
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.test.ts +0 -1994
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1579
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.test.ts +0 -153
- package/src/runtime/clawclaw/plugins/strategy/strategy-source.ts +0 -443
- package/src/sdk/strategy-plugin/index.test.ts +0 -958
- package/src/sdk/strategy-plugin/index.ts +0 -1350
|
@@ -4,35 +4,111 @@ import {
|
|
|
4
4
|
import { compactStateForEvents } from '../../../../pipeline/event-format.js';
|
|
5
5
|
import {
|
|
6
6
|
DEFAULT_EVENT_CONSUMER_ID,
|
|
7
|
+
type CanonicalEvent,
|
|
7
8
|
type EventRepository,
|
|
8
9
|
} from '../../../framework/events/index.js';
|
|
9
10
|
import {
|
|
10
11
|
stableMailboxItemId,
|
|
11
12
|
type MailboxRepository,
|
|
12
13
|
} from '../../../framework/mailbox/index.js';
|
|
14
|
+
import type { RuntimeDiagnosticSeverity } from '../../../framework/plugins/contracts.js';
|
|
13
15
|
import type { RuntimeReadyView } from '../../../framework/runtime.js';
|
|
14
|
-
import {
|
|
16
|
+
import { clawclawMonitorPriority } from '../../agent-event-policy.js';
|
|
15
17
|
import { AgentMailboxCoordinator } from './agent-mailbox-coordinator.js';
|
|
18
|
+
import type {
|
|
19
|
+
ClawclawMailboxAdapterDescriptor,
|
|
20
|
+
ClawclawMailboxDriver,
|
|
21
|
+
} from './mailbox-plugin.js';
|
|
16
22
|
import {
|
|
17
23
|
composeClawclawMonitorNotification,
|
|
24
|
+
clawclawEventRequiresDetails,
|
|
18
25
|
monitorItemPayload,
|
|
26
|
+
partitionClawclawMonitorItems,
|
|
19
27
|
} from './mailbox-presentation.js';
|
|
20
28
|
|
|
21
|
-
const MONITOR_FORMATTER_VERSION = 'clawclaw.monitor.
|
|
29
|
+
const MONITOR_FORMATTER_VERSION = 'clawclaw.monitor.v4';
|
|
22
30
|
const CCL_EVENTS_LEASE_TTL_MS = 30_000;
|
|
31
|
+
const MONITOR_OVERFLOW_LEASE_TTL_MS = 30_000;
|
|
32
|
+
const MONITOR_OVERFLOW_TRANSITION_BATCH_SIZE = 200;
|
|
33
|
+
const MONITOR_PROTECTED_PRIORITY = 80;
|
|
34
|
+
const MONITOR_PRIORITY_BYPASS_ITEM_LIMIT = 4;
|
|
35
|
+
export const DEFAULT_MONITOR_PENDING_ITEM_LIMIT = 10;
|
|
36
|
+
/** Rolling admission window for ordinary Monitor output. */
|
|
37
|
+
export const DEFAULT_MONITOR_ADMISSION_WINDOW_MS = 60_000;
|
|
38
|
+
/** Ordinary short items admitted during one rolling window. */
|
|
39
|
+
export const DEFAULT_MONITOR_ORDINARY_WINDOW_ITEM_BUDGET = 12;
|
|
40
|
+
/** Additional ordinary-only backlog wake-ups admitted during one rolling window. */
|
|
41
|
+
export const DEFAULT_MONITOR_BACKLOG_HINT_WINDOW_ITEM_BUDGET = 4;
|
|
42
|
+
/** Minimum spacing between directly presented visual telemetry items. */
|
|
43
|
+
export const DEFAULT_MONITOR_VISUAL_MIN_INTERVAL_MS = 5_000;
|
|
44
|
+
|
|
45
|
+
const MONITOR_TERMINAL_EVENT_TYPES = new Set(['game_over', 'exile_game_over']);
|
|
46
|
+
const MONITOR_NOISY_VISUAL_EVENT_TYPES = new Set([
|
|
47
|
+
'visual.player_moved',
|
|
48
|
+
'visual.player_motion_changed',
|
|
49
|
+
'visual.no_significant_stop',
|
|
50
|
+
'visual.player_area_changed',
|
|
51
|
+
'visual.player_position_changed',
|
|
52
|
+
'visual.player_turned',
|
|
53
|
+
'visual.player_reversed',
|
|
54
|
+
'visual.player_wandering',
|
|
55
|
+
'visual.players_moving_together',
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
function monitorEventMustRemainDirect(event: Pick<CanonicalEvent, 'type'>): boolean {
|
|
59
|
+
return MONITOR_TERMINAL_EVENT_TYPES.has(event.type)
|
|
60
|
+
|| clawclawMonitorPriority(event.type) >= MONITOR_PROTECTED_PRIORITY;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function monitorDirectAdmissionPriority(event: Pick<CanonicalEvent, 'type'>): number {
|
|
64
|
+
return MONITOR_TERMINAL_EVENT_TYPES.has(event.type)
|
|
65
|
+
? Number.MAX_SAFE_INTEGER
|
|
66
|
+
: clawclawMonitorPriority(event.type);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ordinaryMonitorRetentionRank(event: Pick<CanonicalEvent, 'type' | 'commitSeq'>): number {
|
|
70
|
+
// Retain decision/lifecycle facts, then visual transitions, before noisy
|
|
71
|
+
// motion/spatial telemetry so route churn is delegated first under pressure.
|
|
72
|
+
if (monitorEventIsNoisyVisual(event)) return 2;
|
|
73
|
+
return event.type.startsWith('visual.') ? 1 : 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function monitorEventIsNoisyVisual(event: Pick<CanonicalEvent, 'type'>): boolean {
|
|
77
|
+
return MONITOR_NOISY_VISUAL_EVENT_TYPES.has(event.type);
|
|
78
|
+
}
|
|
23
79
|
|
|
24
80
|
type ClawclawMailboxRepository = EventRepository & MailboxRepository;
|
|
25
81
|
|
|
26
82
|
export interface ClawclawMailboxServiceOptions {
|
|
27
83
|
repository: ClawclawMailboxRepository;
|
|
28
84
|
readyView: () => RuntimeReadyView;
|
|
29
|
-
/**
|
|
85
|
+
/** Dedicated ccl-events lane; transport adapters use their own coordinators. */
|
|
30
86
|
agentMailbox?: AgentMailboxCoordinator;
|
|
31
87
|
consumerId?: string;
|
|
32
88
|
/** Disable the legacy short Monitor adapter for this Owner binding. */
|
|
33
89
|
monitorEnabled?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Maximum ordinary pending/claimed short items retained before overflow is
|
|
92
|
+
* delegated to `ccl events`. One additional coalesced backlog signal is
|
|
93
|
+
* permitted while the ordinary queue is full.
|
|
94
|
+
*/
|
|
95
|
+
monitorPendingItemLimit?: number;
|
|
96
|
+
/** Durable ordinary short-item admissions allowed in one rolling window. */
|
|
97
|
+
monitorOrdinaryWindowItemBudget?: number;
|
|
98
|
+
/** Extra ordinary-only backlog hint admissions allowed in one rolling window. */
|
|
99
|
+
monitorBacklogHintWindowItemBudget?: number;
|
|
100
|
+
/** Duration of the rolling ordinary/hint admission window. */
|
|
101
|
+
monitorAdmissionWindowMs?: number;
|
|
102
|
+
/** Minimum interval between direct visual telemetry items. */
|
|
103
|
+
monitorVisualMinIntervalMs?: number;
|
|
104
|
+
/** Details consumer that reads overflow delegated by this Monitor lane. */
|
|
105
|
+
delegatedDetailsConsumerId?: string;
|
|
34
106
|
now?: () => number;
|
|
35
|
-
diagnostic?: (
|
|
107
|
+
diagnostic?: (
|
|
108
|
+
name: string,
|
|
109
|
+
attributes?: Record<string, unknown>,
|
|
110
|
+
severity?: RuntimeDiagnosticSeverity,
|
|
111
|
+
) => void;
|
|
36
112
|
}
|
|
37
113
|
|
|
38
114
|
export interface PrepareClawclawEventsRequest {
|
|
@@ -45,11 +121,6 @@ export interface PreparedClawclawEventsResponse {
|
|
|
45
121
|
deliveryToken?: string;
|
|
46
122
|
}
|
|
47
123
|
|
|
48
|
-
interface MonitorDelayWindow {
|
|
49
|
-
scopeKey: string;
|
|
50
|
-
deadlineMs: number;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
124
|
function unavailableEventsOutput(): Record<string, unknown> {
|
|
54
125
|
return {
|
|
55
126
|
schema: 'ccl.events.v5',
|
|
@@ -68,18 +139,28 @@ function cclEventsCompatibilityError(error: unknown): never {
|
|
|
68
139
|
throw error;
|
|
69
140
|
}
|
|
70
141
|
|
|
71
|
-
/**
|
|
72
|
-
export class ClawclawMailboxService {
|
|
142
|
+
/** Monitor direct-notification and `ccl events` detail adapters over the Agent Mailbox core. */
|
|
143
|
+
export class ClawclawMailboxService implements ClawclawMailboxDriver {
|
|
144
|
+
readonly adapter?: ClawclawMailboxAdapterDescriptor;
|
|
73
145
|
private readonly repository: ClawclawMailboxRepository;
|
|
74
146
|
private readonly readyView: () => RuntimeReadyView;
|
|
75
147
|
private readonly agentMailbox: AgentMailboxCoordinator;
|
|
76
148
|
private readonly ownsAgentMailbox: boolean;
|
|
77
149
|
private readonly consumerId: string;
|
|
78
150
|
private readonly monitorEnabled: boolean;
|
|
151
|
+
private readonly monitorPendingItemLimit: number;
|
|
152
|
+
private readonly monitorOrdinaryWindowItemBudget: number;
|
|
153
|
+
private readonly monitorBacklogHintWindowItemBudget: number;
|
|
154
|
+
private readonly monitorAdmissionWindowMs: number;
|
|
155
|
+
private readonly monitorVisualMinIntervalMs: number;
|
|
156
|
+
private readonly monitorOrdinaryAdmissionTimesMs: number[] = [];
|
|
157
|
+
private readonly monitorBacklogHintAdmissionTimesMs: number[] = [];
|
|
158
|
+
private monitorLastVisualAdmissionAtMs?: number;
|
|
159
|
+
private readonly delegatedDetailsConsumerId: string;
|
|
79
160
|
private readonly now: () => number;
|
|
80
161
|
private readonly diagnostic?: ClawclawMailboxServiceOptions['diagnostic'];
|
|
81
|
-
private delayWindow?: MonitorDelayWindow;
|
|
82
162
|
private lastObservedReadyEpochSeq = 0;
|
|
163
|
+
private backlogHintEventId?: string;
|
|
83
164
|
private stopped = false;
|
|
84
165
|
|
|
85
166
|
constructor(options: ClawclawMailboxServiceOptions) {
|
|
@@ -87,6 +168,46 @@ export class ClawclawMailboxService {
|
|
|
87
168
|
this.readyView = options.readyView;
|
|
88
169
|
this.consumerId = options.consumerId ?? DEFAULT_EVENT_CONSUMER_ID;
|
|
89
170
|
this.monitorEnabled = options.monitorEnabled ?? true;
|
|
171
|
+
this.monitorPendingItemLimit = options.monitorPendingItemLimit
|
|
172
|
+
?? DEFAULT_MONITOR_PENDING_ITEM_LIMIT;
|
|
173
|
+
if (!Number.isInteger(this.monitorPendingItemLimit) || this.monitorPendingItemLimit <= 0) {
|
|
174
|
+
throw new Error('monitor_pending_item_limit_invalid');
|
|
175
|
+
}
|
|
176
|
+
this.monitorOrdinaryWindowItemBudget = options.monitorOrdinaryWindowItemBudget
|
|
177
|
+
?? DEFAULT_MONITOR_ORDINARY_WINDOW_ITEM_BUDGET;
|
|
178
|
+
if (
|
|
179
|
+
!Number.isInteger(this.monitorOrdinaryWindowItemBudget)
|
|
180
|
+
|| this.monitorOrdinaryWindowItemBudget < 0
|
|
181
|
+
) {
|
|
182
|
+
throw new Error('monitor_ordinary_window_item_budget_invalid');
|
|
183
|
+
}
|
|
184
|
+
this.monitorBacklogHintWindowItemBudget = options.monitorBacklogHintWindowItemBudget
|
|
185
|
+
?? DEFAULT_MONITOR_BACKLOG_HINT_WINDOW_ITEM_BUDGET;
|
|
186
|
+
if (
|
|
187
|
+
!Number.isInteger(this.monitorBacklogHintWindowItemBudget)
|
|
188
|
+
|| this.monitorBacklogHintWindowItemBudget < 0
|
|
189
|
+
) {
|
|
190
|
+
throw new Error('monitor_backlog_hint_window_item_budget_invalid');
|
|
191
|
+
}
|
|
192
|
+
this.monitorAdmissionWindowMs = options.monitorAdmissionWindowMs
|
|
193
|
+
?? DEFAULT_MONITOR_ADMISSION_WINDOW_MS;
|
|
194
|
+
if (!Number.isInteger(this.monitorAdmissionWindowMs) || this.monitorAdmissionWindowMs <= 0) {
|
|
195
|
+
throw new Error('monitor_admission_window_ms_invalid');
|
|
196
|
+
}
|
|
197
|
+
this.monitorVisualMinIntervalMs = options.monitorVisualMinIntervalMs
|
|
198
|
+
?? DEFAULT_MONITOR_VISUAL_MIN_INTERVAL_MS;
|
|
199
|
+
if (
|
|
200
|
+
!Number.isInteger(this.monitorVisualMinIntervalMs)
|
|
201
|
+
|| this.monitorVisualMinIntervalMs < 0
|
|
202
|
+
) {
|
|
203
|
+
throw new Error('monitor_visual_min_interval_ms_invalid');
|
|
204
|
+
}
|
|
205
|
+
this.delegatedDetailsConsumerId = options.delegatedDetailsConsumerId ?? this.consumerId;
|
|
206
|
+
this.adapter = this.monitorEnabled ? {
|
|
207
|
+
id: 'clawclaw.mailbox.adapter.monitor',
|
|
208
|
+
acknowledgement: 'explicit',
|
|
209
|
+
consumerId: this.consumerId,
|
|
210
|
+
} : undefined;
|
|
90
211
|
this.now = options.now ?? Date.now;
|
|
91
212
|
this.diagnostic = options.diagnostic;
|
|
92
213
|
this.ownsAgentMailbox = !options.agentMailbox;
|
|
@@ -95,6 +216,8 @@ export class ClawclawMailboxService {
|
|
|
95
216
|
readyView: options.readyView,
|
|
96
217
|
presentEvent: presentCanonicalEventForCclEvents,
|
|
97
218
|
compactState: compactStateForEvents,
|
|
219
|
+
eventFilter: clawclawEventRequiresDetails,
|
|
220
|
+
delegatedMonitorConsumerId: this.consumerId,
|
|
98
221
|
consumerId: this.consumerId,
|
|
99
222
|
now: this.now,
|
|
100
223
|
diagnostic: this.diagnostic,
|
|
@@ -194,12 +317,12 @@ export class ClawclawMailboxService {
|
|
|
194
317
|
}
|
|
195
318
|
|
|
196
319
|
/** Called by the Monitor Driver at its own cadence, never by each epoch. */
|
|
197
|
-
prepareMonitor(
|
|
320
|
+
prepareMonitor(_force = false): number {
|
|
198
321
|
this.assertRunning();
|
|
199
322
|
if (!this.monitorEnabled) return 0;
|
|
200
323
|
const ready = this.captureReadyView();
|
|
201
324
|
if (ready.epochSeq <= 0) return 0;
|
|
202
|
-
|
|
325
|
+
this.repository.ensureDeliveries(ready.scope.sessionId, this.consumerId, ['monitor']);
|
|
203
326
|
const rows = this.repository.listEvents({
|
|
204
327
|
sessionId: ready.scope.sessionId,
|
|
205
328
|
deliveryClasses: ['monitor_and_events'],
|
|
@@ -207,7 +330,7 @@ export class ClawclawMailboxService {
|
|
|
207
330
|
maxCommitSeq: ready.eventWatermark,
|
|
208
331
|
maxEpochSeq: ready.epochSeq,
|
|
209
332
|
order: 'asc',
|
|
210
|
-
}, this.consumerId);
|
|
333
|
+
}, this.consumerId, ['monitor']);
|
|
211
334
|
const unprojected = rows.filter((row) => !this.repository.mailboxItemForEvent(
|
|
212
335
|
this.consumerId,
|
|
213
336
|
row.event.eventId,
|
|
@@ -216,24 +339,177 @@ export class ClawclawMailboxService {
|
|
|
216
339
|
if (unprojected.length === 0) return 0;
|
|
217
340
|
|
|
218
341
|
const nowMs = this.now();
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
342
|
+
this.pruneMonitorAdmissionWindows(nowMs);
|
|
343
|
+
const ordinaryWindowItemsRemaining = Math.max(
|
|
344
|
+
0,
|
|
345
|
+
this.monitorOrdinaryWindowItemBudget - this.monitorOrdinaryAdmissionTimesMs.length,
|
|
346
|
+
);
|
|
347
|
+
const backlogHintWindowItemsRemaining = Math.max(
|
|
348
|
+
0,
|
|
349
|
+
this.monitorBacklogHintWindowItemBudget - this.monitorBacklogHintAdmissionTimesMs.length,
|
|
350
|
+
);
|
|
351
|
+
const visualAdmissionAvailable = this.monitorLastVisualAdmissionAtMs === undefined
|
|
352
|
+
|| nowMs - this.monitorLastVisualAdmissionAtMs >= this.monitorVisualMinIntervalMs;
|
|
353
|
+
const queuedItems = this.repository.peekMailbox<Record<string, unknown>>({
|
|
354
|
+
consumerId: this.consumerId,
|
|
355
|
+
sessionId: ready.scope.sessionId,
|
|
356
|
+
modes: ['short'],
|
|
357
|
+
states: ['pending', 'claimed'],
|
|
358
|
+
order: 'asc',
|
|
359
|
+
limit: this.monitorPendingItemLimit + MONITOR_PRIORITY_BYPASS_ITEM_LIMIT + 1,
|
|
360
|
+
});
|
|
361
|
+
const queuedItemCount = queuedItems.length;
|
|
362
|
+
const delegatedUnreadBefore = this.delegatedUnreadCount(ready);
|
|
363
|
+
if (delegatedUnreadBefore === 0) this.backlogHintEventId = undefined;
|
|
364
|
+
const queuedBacklogHint = queuedItems.find((item) => (
|
|
365
|
+
!!item.payload.backlog && typeof item.payload.backlog === 'object'
|
|
366
|
+
));
|
|
367
|
+
if (queuedBacklogHint) this.backlogHintEventId = queuedBacklogHint.eventId;
|
|
368
|
+
const storedBacklogHint = this.backlogHintEventId
|
|
369
|
+
? this.repository.mailboxItemForEvent(
|
|
370
|
+
this.consumerId,
|
|
371
|
+
this.backlogHintEventId,
|
|
372
|
+
'short',
|
|
373
|
+
)
|
|
374
|
+
: null;
|
|
375
|
+
const backlogHintOutstanding = !!queuedBacklogHint || (
|
|
376
|
+
delegatedUnreadBefore > 0
|
|
377
|
+
&& !!storedBacklogHint
|
|
378
|
+
&& storedBacklogHint.state !== 'suppressed'
|
|
379
|
+
);
|
|
380
|
+
const queuedBaseItemCount = queuedItems.filter((item) => (
|
|
381
|
+
!item.payload.backlog || typeof item.payload.backlog !== 'object'
|
|
382
|
+
)).length;
|
|
383
|
+
const availableBaseItemCount = Math.max(
|
|
384
|
+
0,
|
|
385
|
+
this.monitorPendingItemLimit - queuedBaseItemCount,
|
|
386
|
+
);
|
|
387
|
+
const usedPriorityBypassItemCount = Math.max(
|
|
388
|
+
0,
|
|
389
|
+
queuedBaseItemCount - this.monitorPendingItemLimit,
|
|
390
|
+
);
|
|
391
|
+
const availablePriorityBypassItemCount = Math.max(
|
|
392
|
+
0,
|
|
393
|
+
MONITOR_PRIORITY_BYPASS_ITEM_LIMIT - usedPriorityBypassItemCount,
|
|
394
|
+
);
|
|
395
|
+
const protectedEvents = unprojected
|
|
396
|
+
.filter((row) => monitorEventMustRemainDirect(row.event))
|
|
397
|
+
.sort((left, right) => (
|
|
398
|
+
monitorDirectAdmissionPriority(right.event)
|
|
399
|
+
- monitorDirectAdmissionPriority(left.event)
|
|
400
|
+
|| left.event.commitSeq - right.event.commitSeq
|
|
401
|
+
));
|
|
402
|
+
const protectedCapacity = availableBaseItemCount + availablePriorityBypassItemCount;
|
|
403
|
+
const directProtected = protectedEvents.slice(0, protectedCapacity);
|
|
404
|
+
const remainingBaseItemCount = Math.max(
|
|
405
|
+
0,
|
|
406
|
+
availableBaseItemCount - Math.min(directProtected.length, availableBaseItemCount),
|
|
407
|
+
);
|
|
408
|
+
const allOrdinaryEvents = unprojected
|
|
409
|
+
.filter((row) => !monitorEventMustRemainDirect(row.event));
|
|
410
|
+
const noisyVisualEvents = allOrdinaryEvents
|
|
411
|
+
.filter((row) => monitorEventIsNoisyVisual(row.event))
|
|
412
|
+
.sort((left, right) => left.event.commitSeq - right.event.commitSeq);
|
|
413
|
+
const directVisualCandidate = visualAdmissionAvailable ? noisyVisualEvents.at(-1) : undefined;
|
|
414
|
+
const coalescedVisualOverflow = directVisualCandidate
|
|
415
|
+
? noisyVisualEvents.slice(0, -1)
|
|
416
|
+
: noisyVisualEvents;
|
|
417
|
+
const ordinaryEvents = allOrdinaryEvents
|
|
418
|
+
.filter((row) => !monitorEventIsNoisyVisual(row.event))
|
|
419
|
+
.concat(directVisualCandidate ? [directVisualCandidate] : [])
|
|
420
|
+
.sort((left, right) => (
|
|
421
|
+
ordinaryMonitorRetentionRank(left.event) - ordinaryMonitorRetentionRank(right.event)
|
|
422
|
+
|| left.event.commitSeq - right.event.commitSeq
|
|
423
|
+
));
|
|
424
|
+
const ordinaryDirectAdmissionCount = Math.min(
|
|
425
|
+
remainingBaseItemCount,
|
|
426
|
+
ordinaryWindowItemsRemaining,
|
|
427
|
+
);
|
|
428
|
+
const directOrdinary = ordinaryEvents.slice(0, ordinaryDirectAdmissionCount);
|
|
429
|
+
const ordinaryBudgetEventIds = new Set(
|
|
430
|
+
directOrdinary.map((row) => row.event.eventId),
|
|
431
|
+
);
|
|
432
|
+
let overflow = [
|
|
433
|
+
...ordinaryEvents.slice(ordinaryDirectAdmissionCount),
|
|
434
|
+
...coalescedVisualOverflow,
|
|
435
|
+
].sort((left, right) => left.event.commitSeq - right.event.commitSeq);
|
|
436
|
+
let direct = [...directProtected, ...directOrdinary]
|
|
437
|
+
.sort((left, right) => left.event.commitSeq - right.event.commitSeq);
|
|
438
|
+
let dedicatedBacklogHintEventId: string | undefined;
|
|
439
|
+
// A full ordinary queue still needs one durable signal. Use the first new
|
|
440
|
+
// Event as that signal's factual anchor and allow one separately-budgeted
|
|
441
|
+
// item. The same bounded hint lane becomes available again as its rolling
|
|
442
|
+
// admission window advances after `ccl events` drains an earlier backlog.
|
|
443
|
+
if (
|
|
444
|
+
direct.length === 0
|
|
445
|
+
&& !backlogHintOutstanding
|
|
446
|
+
&& backlogHintWindowItemsRemaining > 0
|
|
447
|
+
&& (
|
|
448
|
+
queuedBaseItemCount >= this.monitorPendingItemLimit
|
|
449
|
+
|| ordinaryWindowItemsRemaining === 0
|
|
450
|
+
)
|
|
451
|
+
&& (overflow.length > 1 || (overflow.length > 0 && delegatedUnreadBefore > 0))
|
|
452
|
+
) {
|
|
453
|
+
const firstNonVisualIndex = overflow.findIndex((row) => (
|
|
454
|
+
!row.event.type.startsWith('visual.')
|
|
455
|
+
));
|
|
456
|
+
const hintAnchorIndex = firstNonVisualIndex >= 0
|
|
457
|
+
? firstNonVisualIndex
|
|
458
|
+
: overflow.length - 1;
|
|
459
|
+
const hintAnchor = overflow[hintAnchorIndex]!;
|
|
460
|
+
direct = [hintAnchor];
|
|
461
|
+
dedicatedBacklogHintEventId = hintAnchor.event.eventId;
|
|
462
|
+
overflow = overflow.filter((_, index) => index !== hintAnchorIndex);
|
|
229
463
|
}
|
|
230
|
-
|
|
464
|
+
if (direct.length === 0 && overflow.length === 0) return 0;
|
|
465
|
+
// With only one newly blocked Event there is no factual anchor plus
|
|
466
|
+
// durable remainder yet. Keep it pending/unprojected until either the
|
|
467
|
+
// ordinary queue gains capacity or a second Event forms a real backlog.
|
|
468
|
+
// A temporally eligible item blocked only by queue capacity remains
|
|
469
|
+
// pending until a second Event can anchor a durable backlog signal. An
|
|
470
|
+
// exhausted rolling window or visual rate limit recovers only with time,
|
|
471
|
+
// so delegate a quiet tail immediately instead of retaining stale output.
|
|
472
|
+
if (
|
|
473
|
+
direct.length === 0
|
|
474
|
+
&& !backlogHintOutstanding
|
|
475
|
+
&& delegatedUnreadBefore === 0
|
|
476
|
+
&& ordinaryWindowItemsRemaining > 0
|
|
477
|
+
&& ordinaryEvents.length > 0
|
|
478
|
+
) return 0;
|
|
231
479
|
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
480
|
+
const coalesceBase = `ready:${ready.scope.sessionId}:${ready.scope.segmentId}:${ready.epochSeq}`;
|
|
481
|
+
const payloads = direct.map((row) => monitorItemPayload(row.event, ready));
|
|
482
|
+
const anticipatedDelegatedUnreadCount = delegatedUnreadBefore + overflow.length;
|
|
483
|
+
const createsBacklogHint = (
|
|
484
|
+
!backlogHintOutstanding
|
|
485
|
+
&& payloads.length > 0
|
|
486
|
+
&& anticipatedDelegatedUnreadCount > 0
|
|
487
|
+
);
|
|
488
|
+
if (createsBacklogHint) {
|
|
489
|
+
payloads[0] = {
|
|
490
|
+
...payloads[0]!,
|
|
491
|
+
backlog: {
|
|
492
|
+
remaining_events: anticipatedDelegatedUnreadCount,
|
|
493
|
+
through_event_commit_seq: ready.eventWatermark,
|
|
494
|
+
command: 'ccl events',
|
|
495
|
+
},
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
const chunks = partitionClawclawMonitorItems(payloads);
|
|
499
|
+
const rowByEventId = new Map(direct.map((row) => [row.event.eventId, row]));
|
|
500
|
+
const entries = chunks.flatMap((chunk, chunkIndex) => {
|
|
501
|
+
const coalesceKey = chunks.length === 1
|
|
502
|
+
? coalesceBase
|
|
503
|
+
: `${coalesceBase}:chunk:${chunkIndex}`;
|
|
504
|
+
return chunk.map((payload) => {
|
|
505
|
+
const row = rowByEventId.get(payload.event_id);
|
|
506
|
+
if (!row) throw new Error(`mailbox_monitor_event_missing:${payload.event_id}`);
|
|
507
|
+
return { coalesceKey, payload, row };
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
const enqueue = (entry: typeof entries[number]) => {
|
|
511
|
+
const event = entry.row.event;
|
|
512
|
+
return this.repository.enqueueMailboxItem<Record<string, unknown>>({
|
|
237
513
|
itemId: stableMailboxItemId({
|
|
238
514
|
consumerId: this.consumerId,
|
|
239
515
|
sessionId: event.sessionId,
|
|
@@ -248,18 +524,122 @@ export class ClawclawMailboxService {
|
|
|
248
524
|
eventId: event.eventId,
|
|
249
525
|
eventCommitSeq: event.commitSeq,
|
|
250
526
|
mode: 'short',
|
|
251
|
-
coalesceKey,
|
|
252
|
-
payload:
|
|
527
|
+
coalesceKey: entry.coalesceKey,
|
|
528
|
+
payload: entry.payload,
|
|
253
529
|
availableAtMs: nowMs,
|
|
254
530
|
createdAt: new Date(nowMs).toISOString(),
|
|
255
531
|
});
|
|
256
|
-
|
|
532
|
+
};
|
|
533
|
+
let inserted = 0;
|
|
534
|
+
const accountInserted = (
|
|
535
|
+
entry: typeof entries[number],
|
|
536
|
+
wasInserted: boolean,
|
|
537
|
+
): void => {
|
|
538
|
+
if (!wasInserted) return;
|
|
539
|
+
inserted += 1;
|
|
540
|
+
if (ordinaryBudgetEventIds.has(entry.row.event.eventId)) {
|
|
541
|
+
this.monitorOrdinaryAdmissionTimesMs.push(nowMs);
|
|
542
|
+
}
|
|
543
|
+
if (entry.row.event.eventId === dedicatedBacklogHintEventId) {
|
|
544
|
+
this.monitorBacklogHintAdmissionTimesMs.push(nowMs);
|
|
545
|
+
}
|
|
546
|
+
if (monitorEventIsNoisyVisual(entry.row.event)) {
|
|
547
|
+
this.monitorLastVisualAdmissionAtMs = nowMs;
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
let persistedHintEntry: typeof entries[number] | undefined;
|
|
551
|
+
if (createsBacklogHint) {
|
|
552
|
+
const hintEntry = entries.find((entry) => (
|
|
553
|
+
!!entry.payload.backlog && typeof entry.payload.backlog === 'object'
|
|
554
|
+
));
|
|
555
|
+
if (!hintEntry) throw new Error('mailbox_monitor_backlog_hint_missing');
|
|
556
|
+
const result = enqueue(hintEntry);
|
|
557
|
+
const persisted = result.inserted || (
|
|
558
|
+
!!result.item.payload.backlog && typeof result.item.payload.backlog === 'object'
|
|
559
|
+
);
|
|
560
|
+
if (!persisted) return 0;
|
|
561
|
+
accountInserted(hintEntry, result.inserted);
|
|
562
|
+
persistedHintEntry = hintEntry;
|
|
563
|
+
this.backlogHintEventId = hintEntry.row.event.eventId;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// The unique hint anchor is now durable (or an older accepted/pending hint
|
|
567
|
+
// is still outstanding), so suppressing the remainder cannot orphan it.
|
|
568
|
+
const delegatedCount = this.delegateMonitorOverflow(
|
|
569
|
+
overflow.map((row) => row.event.eventId),
|
|
570
|
+
nowMs,
|
|
571
|
+
);
|
|
572
|
+
if (createsBacklogHint) {
|
|
573
|
+
const delegatedUnreadCount = this.delegatedUnreadCount(ready);
|
|
574
|
+
this.diagnostic?.('runtime.mailbox.monitor_backlog_delegated', {
|
|
575
|
+
consumer_id: this.consumerId,
|
|
576
|
+
session_id: ready.scope.sessionId,
|
|
577
|
+
segment_id: ready.scope.segmentId,
|
|
578
|
+
ready_epoch_seq: ready.epochSeq,
|
|
579
|
+
event_watermark: ready.eventWatermark,
|
|
580
|
+
pending_item_limit: this.monitorPendingItemLimit,
|
|
581
|
+
queued_item_count: queuedItemCount,
|
|
582
|
+
direct_event_count: direct.length,
|
|
583
|
+
delegated_event_count: delegatedCount,
|
|
584
|
+
unread_delegated_event_count: delegatedUnreadCount,
|
|
585
|
+
}, 'info');
|
|
586
|
+
}
|
|
587
|
+
for (const entry of entries) {
|
|
588
|
+
if (entry === persistedHintEntry) continue;
|
|
589
|
+
const result = enqueue(entry);
|
|
590
|
+
accountInserted(entry, result.inserted);
|
|
591
|
+
}
|
|
592
|
+
if (payloads.length === 0) {
|
|
593
|
+
// This path uses an already durable backlog hint and only delegates new
|
|
594
|
+
// ordinary Events, so there is intentionally no new Mailbox item.
|
|
595
|
+
return 0;
|
|
596
|
+
}
|
|
597
|
+
if (inserted > 0 && chunks.length > 1) {
|
|
598
|
+
const eventTypeCounts: Record<string, number> = {};
|
|
599
|
+
for (const payload of payloads) {
|
|
600
|
+
const type = String(payload.event.type ?? 'event');
|
|
601
|
+
eventTypeCounts[type] = (eventTypeCounts[type] ?? 0) + 1;
|
|
602
|
+
}
|
|
603
|
+
this.diagnostic?.('runtime.mailbox.monitor_batch_split', {
|
|
604
|
+
consumer_id: this.consumerId,
|
|
605
|
+
session_id: ready.scope.sessionId,
|
|
606
|
+
segment_id: ready.scope.segmentId,
|
|
607
|
+
ready_epoch_seq: ready.epochSeq,
|
|
608
|
+
event_watermark: ready.eventWatermark,
|
|
609
|
+
event_count: payloads.length,
|
|
610
|
+
inserted_event_count: inserted,
|
|
611
|
+
notification_count: chunks.length,
|
|
612
|
+
events_per_notification: chunks.map((chunk) => chunk.length),
|
|
613
|
+
notification_char_counts: chunks.map((chunk) => (
|
|
614
|
+
JSON.stringify(composeClawclawMonitorNotification(chunk)).length
|
|
615
|
+
)),
|
|
616
|
+
event_type_counts: eventTypeCounts,
|
|
617
|
+
}, 'info');
|
|
257
618
|
}
|
|
258
619
|
return inserted;
|
|
259
620
|
}
|
|
260
621
|
|
|
261
622
|
composeMonitor(values: readonly Record<string, unknown>[]): Record<string, unknown> {
|
|
262
|
-
|
|
623
|
+
const hasBacklog = values.some((value) => (
|
|
624
|
+
!!value.backlog && typeof value.backlog === 'object'
|
|
625
|
+
));
|
|
626
|
+
if (!hasBacklog) return composeClawclawMonitorNotification(values);
|
|
627
|
+
const ready = this.captureReadyView();
|
|
628
|
+
const remainingEvents = this.delegatedUnreadCount(ready);
|
|
629
|
+
const refreshed = values.map((value) => {
|
|
630
|
+
if (!value.backlog || typeof value.backlog !== 'object') return value;
|
|
631
|
+
const { backlog: _backlog, ...withoutBacklog } = value;
|
|
632
|
+
if (remainingEvents === 0) return withoutBacklog;
|
|
633
|
+
return {
|
|
634
|
+
...withoutBacklog,
|
|
635
|
+
backlog: {
|
|
636
|
+
remaining_events: remainingEvents,
|
|
637
|
+
through_event_commit_seq: ready.eventWatermark,
|
|
638
|
+
command: 'ccl events',
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
});
|
|
642
|
+
return composeClawclawMonitorNotification(refreshed);
|
|
263
643
|
}
|
|
264
644
|
|
|
265
645
|
async flush(): Promise<void> {
|
|
@@ -273,6 +653,70 @@ export class ClawclawMailboxService {
|
|
|
273
653
|
if (this.ownsAgentMailbox) await this.agentMailbox.stop();
|
|
274
654
|
}
|
|
275
655
|
|
|
656
|
+
private delegateMonitorOverflow(eventIds: readonly string[], nowMs: number): number {
|
|
657
|
+
if (eventIds.length === 0) return 0;
|
|
658
|
+
const lease = this.repository.acquireLease({
|
|
659
|
+
consumerId: this.consumerId,
|
|
660
|
+
channel: 'monitor',
|
|
661
|
+
ownerId: `${this.consumerId}:monitor-overflow`,
|
|
662
|
+
ttlMs: MONITOR_OVERFLOW_LEASE_TTL_MS,
|
|
663
|
+
nowMs,
|
|
664
|
+
});
|
|
665
|
+
if (!lease) return 0;
|
|
666
|
+
let changed = 0;
|
|
667
|
+
try {
|
|
668
|
+
for (let offset = 0; offset < eventIds.length; offset += MONITOR_OVERFLOW_TRANSITION_BATCH_SIZE) {
|
|
669
|
+
changed += this.repository.transitionMonitor({
|
|
670
|
+
eventIds: eventIds.slice(offset, offset + MONITOR_OVERFLOW_TRANSITION_BATCH_SIZE),
|
|
671
|
+
consumerId: this.consumerId,
|
|
672
|
+
from: ['pending'],
|
|
673
|
+
to: 'suppressed',
|
|
674
|
+
lease,
|
|
675
|
+
nowMs,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
return changed;
|
|
679
|
+
} finally {
|
|
680
|
+
this.repository.releaseLease(lease);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
private delegatedUnreadCount(ready: RuntimeReadyView): number {
|
|
685
|
+
const suppressed = this.repository.listEvents({
|
|
686
|
+
sessionId: ready.scope.sessionId,
|
|
687
|
+
deliveryClasses: ['monitor_and_events'],
|
|
688
|
+
monitorStates: ['suppressed'],
|
|
689
|
+
maxCommitSeq: ready.eventWatermark,
|
|
690
|
+
maxEpochSeq: ready.epochSeq,
|
|
691
|
+
order: 'asc',
|
|
692
|
+
}, this.consumerId, ['monitor']);
|
|
693
|
+
if (suppressed.length === 0) return 0;
|
|
694
|
+
this.repository.ensureDeliveries(
|
|
695
|
+
ready.scope.sessionId,
|
|
696
|
+
this.delegatedDetailsConsumerId,
|
|
697
|
+
['details'],
|
|
698
|
+
);
|
|
699
|
+
return suppressed.filter((row) => {
|
|
700
|
+
const state = this.repository.delivery(
|
|
701
|
+
row.event.eventId,
|
|
702
|
+
this.delegatedDetailsConsumerId,
|
|
703
|
+
)?.detailState;
|
|
704
|
+
return state === 'pending' || state === 'deferred';
|
|
705
|
+
}).length;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
private pruneMonitorAdmissionWindows(nowMs: number): void {
|
|
709
|
+
const cutoffMs = nowMs - this.monitorAdmissionWindowMs;
|
|
710
|
+
while (
|
|
711
|
+
this.monitorOrdinaryAdmissionTimesMs.length > 0
|
|
712
|
+
&& this.monitorOrdinaryAdmissionTimesMs[0]! <= cutoffMs
|
|
713
|
+
) this.monitorOrdinaryAdmissionTimesMs.shift();
|
|
714
|
+
while (
|
|
715
|
+
this.monitorBacklogHintAdmissionTimesMs.length > 0
|
|
716
|
+
&& this.monitorBacklogHintAdmissionTimesMs[0]! <= cutoffMs
|
|
717
|
+
) this.monitorBacklogHintAdmissionTimesMs.shift();
|
|
718
|
+
}
|
|
719
|
+
|
|
276
720
|
private captureReadyView(readyView?: RuntimeReadyView): RuntimeReadyView {
|
|
277
721
|
const ready = structuredClone(readyView ?? this.readyView());
|
|
278
722
|
if (ready.epochSeq > this.lastObservedReadyEpochSeq) {
|
|
@@ -16,19 +16,20 @@ export type {
|
|
|
16
16
|
PerceptionBoundary,
|
|
17
17
|
PerceptionBoundaryKind,
|
|
18
18
|
PerceptionEventProposal,
|
|
19
|
+
PerceptionFrame,
|
|
19
20
|
PerceptionMapRoom,
|
|
20
21
|
PerceptionSpatialApi,
|
|
21
|
-
PerceptionSpatialFacts,
|
|
22
22
|
PerceptionSubjectScope,
|
|
23
23
|
VisualCorpse,
|
|
24
|
+
VisualObservation,
|
|
24
25
|
VisualPlayer,
|
|
25
26
|
VisualPosition,
|
|
26
|
-
VisualState,
|
|
27
|
-
VisualStateProvenance,
|
|
28
27
|
} from '../../../../sdk/perception-plugin/index.js';
|
|
28
|
+
export type { SpatialFacts as PerceptionSpatialFacts } from '../../spatial-api.js';
|
|
29
29
|
|
|
30
30
|
export type PerceptionModuleId =
|
|
31
31
|
| 'visual.visibility'
|
|
32
|
+
| 'visual.corpse_visibility'
|
|
32
33
|
| 'visual.motion'
|
|
33
34
|
| 'visual.stop'
|
|
34
35
|
| 'visual.spatial'
|
|
@@ -5,4 +5,4 @@ export * from './perception-plugin.js';
|
|
|
5
5
|
export * from './perception-source.js';
|
|
6
6
|
export * from './replay.js';
|
|
7
7
|
export * from './spatial-facts.js';
|
|
8
|
-
export
|
|
8
|
+
export { ObservationFrameProjector } from '../../observation-frame-projector.js';
|