@myclaw163/clawclaw-cli 0.7.16 → 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 +1 -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 +285 -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 +388 -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 -2175
- package/src/runtime/clawclaw/plugins/strategy/strategy-plugin.ts +0 -1774
- 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
|
@@ -18,7 +18,11 @@ import type {
|
|
|
18
18
|
NackMailboxClaimResult,
|
|
19
19
|
SuppressMailboxItemsFilter,
|
|
20
20
|
} from '../mailbox/types.js';
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
DeliveryLeaseLostError,
|
|
23
|
+
DeliveryLeaseScopeError,
|
|
24
|
+
type EventRepository,
|
|
25
|
+
} from './event-repository.js';
|
|
22
26
|
import {
|
|
23
27
|
cloneRecord,
|
|
24
28
|
initialDeliveryFor,
|
|
@@ -41,6 +45,7 @@ import {
|
|
|
41
45
|
type MonitorStateTransition,
|
|
42
46
|
type PlaySessionRecord,
|
|
43
47
|
type SourceCheckpointRecord,
|
|
48
|
+
validateDetailDeliveryVia,
|
|
44
49
|
} from './types.js';
|
|
45
50
|
|
|
46
51
|
interface StatementLike {
|
|
@@ -71,6 +76,14 @@ export type SqliteRepositoryOpenResult =
|
|
|
71
76
|
|
|
72
77
|
const require = createRequire(import.meta.url);
|
|
73
78
|
|
|
79
|
+
// These values are part of the persisted delivery ABI. Keep the migration
|
|
80
|
+
// literals stable even if adapter construction moves elsewhere later.
|
|
81
|
+
const LEGACY_AGENT_CONSUMER_ID = 'local-agent';
|
|
82
|
+
const MONITOR_AGENT_CONSUMER_ID = 'local-agent:monitor';
|
|
83
|
+
const CCL_EVENTS_AGENT_CONSUMER_ID = 'local-agent:ccl-events';
|
|
84
|
+
const DESKTOP_AGENT_CONSUMER_ID = 'local-agent:desktop';
|
|
85
|
+
const CLAUDE_CHANNEL_AGENT_CONSUMER_ID = 'local-agent:claude-channel';
|
|
86
|
+
|
|
74
87
|
function loadSqlite(): { ok: true; module: SqliteModuleLike } | { ok: false; unavailable: SqliteUnavailable } {
|
|
75
88
|
try {
|
|
76
89
|
const module = loadNodeSqliteModule<SqliteModuleLike>(require);
|
|
@@ -629,7 +642,13 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
629
642
|
`).all(...params).map((row) => eventFromRow(row));
|
|
630
643
|
}
|
|
631
644
|
|
|
632
|
-
ensureDeliveries(
|
|
645
|
+
ensureDeliveries(
|
|
646
|
+
sessionId: string,
|
|
647
|
+
consumerId: string,
|
|
648
|
+
channels: readonly ConsumerLease['channel'][] = ['monitor', 'details'],
|
|
649
|
+
): void {
|
|
650
|
+
const monitorEnabled = channels.includes('monitor') ? 1 : 0;
|
|
651
|
+
const detailsEnabled = channels.includes('details') ? 1 : 0;
|
|
633
652
|
this.db.prepare(`
|
|
634
653
|
INSERT OR IGNORE INTO event_deliveries(
|
|
635
654
|
event_id, consumer_id, monitor_state, detail_state
|
|
@@ -638,12 +657,12 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
638
657
|
event_id,
|
|
639
658
|
?,
|
|
640
659
|
CASE
|
|
641
|
-
WHEN delivery_class <> 'monitor_and_events' THEN 'not_applicable'
|
|
660
|
+
WHEN ? = 0 OR delivery_class <> 'monitor_and_events' THEN 'not_applicable'
|
|
642
661
|
WHEN p.is_current = 1 THEN 'pending'
|
|
643
662
|
ELSE 'suppressed'
|
|
644
663
|
END,
|
|
645
664
|
CASE
|
|
646
|
-
WHEN delivery_class = 'internal_only' THEN 'not_applicable'
|
|
665
|
+
WHEN ? = 0 OR delivery_class = 'internal_only' THEN 'not_applicable'
|
|
647
666
|
WHEN p.is_current = 1 AND s.is_current = 1 THEN 'pending'
|
|
648
667
|
ELSE 'deferred'
|
|
649
668
|
END
|
|
@@ -651,21 +670,33 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
651
670
|
JOIN event_segments s ON s.segment_id = e.segment_id
|
|
652
671
|
JOIN play_sessions p ON p.session_id = e.session_id
|
|
653
672
|
WHERE e.session_id = ?
|
|
654
|
-
|
|
673
|
+
ON CONFLICT(event_id, consumer_id) DO UPDATE SET
|
|
674
|
+
monitor_state = CASE
|
|
675
|
+
WHEN event_deliveries.monitor_state = 'not_applicable'
|
|
676
|
+
THEN excluded.monitor_state
|
|
677
|
+
ELSE event_deliveries.monitor_state
|
|
678
|
+
END,
|
|
679
|
+
detail_state = CASE
|
|
680
|
+
WHEN event_deliveries.detail_state = 'not_applicable'
|
|
681
|
+
THEN excluded.detail_state
|
|
682
|
+
ELSE event_deliveries.detail_state
|
|
683
|
+
END
|
|
684
|
+
`).run(consumerId, monitorEnabled, detailsEnabled, sessionId);
|
|
655
685
|
}
|
|
656
686
|
|
|
657
687
|
delivery(eventId: string, consumerId: string): EventDeliveryRecord | null {
|
|
658
|
-
const event = this.eventById(eventId);
|
|
659
|
-
if (!event) return null;
|
|
660
|
-
this.insertInitialDelivery(event, consumerId);
|
|
661
688
|
const row = this.db.prepare(`
|
|
662
689
|
SELECT * FROM event_deliveries WHERE event_id = ? AND consumer_id = ?
|
|
663
690
|
`).get(eventId, consumerId);
|
|
664
691
|
return row ? deliveryFromRow(row) : null;
|
|
665
692
|
}
|
|
666
693
|
|
|
667
|
-
listEvents(
|
|
668
|
-
|
|
694
|
+
listEvents(
|
|
695
|
+
filter: EventListFilter,
|
|
696
|
+
consumerId: string,
|
|
697
|
+
channels?: readonly ConsumerLease['channel'][],
|
|
698
|
+
): EventWithDelivery[] {
|
|
699
|
+
this.ensureDeliveries(filter.sessionId, consumerId, channels);
|
|
669
700
|
const clauses = ['e.session_id = ?', 'd.consumer_id = ?'];
|
|
670
701
|
const params: unknown[] = [filter.sessionId, consumerId];
|
|
671
702
|
if (filter.segmentId) {
|
|
@@ -709,7 +740,11 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
709
740
|
}
|
|
710
741
|
|
|
711
742
|
backlogSummary(sessionId: string, consumerId: string): BacklogSummary {
|
|
712
|
-
const rows = this.listEvents(
|
|
743
|
+
const rows = this.listEvents(
|
|
744
|
+
{ sessionId, detailStates: ['deferred'], order: 'asc' },
|
|
745
|
+
consumerId,
|
|
746
|
+
['details'],
|
|
747
|
+
);
|
|
713
748
|
return {
|
|
714
749
|
unreadCount: rows.length,
|
|
715
750
|
segmentCount: new Set(rows.map((row) => row.event.segmentId)).size,
|
|
@@ -720,11 +755,17 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
720
755
|
}
|
|
721
756
|
|
|
722
757
|
transitionDetails(transition: DetailStateTransition): number {
|
|
723
|
-
this.assertLeaseIfPresent(
|
|
758
|
+
this.assertLeaseIfPresent(
|
|
759
|
+
transition.lease,
|
|
760
|
+
transition.consumerId,
|
|
761
|
+
'details',
|
|
762
|
+
transition.nowMs,
|
|
763
|
+
);
|
|
724
764
|
if (transition.eventIds.length === 0 || transition.from.length === 0) return 0;
|
|
725
765
|
if (transition.to === 'delivered' && (!transition.deliveredAt || !transition.deliveryVia)) {
|
|
726
766
|
throw new Error('Delivered event details require deliveredAt and deliveryVia.');
|
|
727
767
|
}
|
|
768
|
+
if (transition.to === 'delivered') validateDetailDeliveryVia(transition.deliveryVia!);
|
|
728
769
|
const params: unknown[] = [
|
|
729
770
|
transition.to,
|
|
730
771
|
transition.to === 'delivered' ? transition.deliveredAt : null,
|
|
@@ -744,7 +785,12 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
744
785
|
}
|
|
745
786
|
|
|
746
787
|
transitionMonitor(transition: MonitorStateTransition): number {
|
|
747
|
-
this.assertLeaseIfPresent(
|
|
788
|
+
this.assertLeaseIfPresent(
|
|
789
|
+
transition.lease,
|
|
790
|
+
transition.consumerId,
|
|
791
|
+
'monitor',
|
|
792
|
+
transition.nowMs,
|
|
793
|
+
);
|
|
748
794
|
if (transition.eventIds.length === 0 || transition.from.length === 0) return 0;
|
|
749
795
|
if (transition.to === 'notified' && !transition.deliveredAt) {
|
|
750
796
|
throw new Error('Notified Monitor events require deliveredAt.');
|
|
@@ -767,7 +813,12 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
767
813
|
}
|
|
768
814
|
|
|
769
815
|
recordMonitorAttempt(attempt: MonitorDeliveryAttempt): boolean {
|
|
770
|
-
this.assertLeaseIfPresent(
|
|
816
|
+
this.assertLeaseIfPresent(
|
|
817
|
+
attempt.lease,
|
|
818
|
+
attempt.consumerId,
|
|
819
|
+
'monitor',
|
|
820
|
+
attempt.nowMs,
|
|
821
|
+
);
|
|
771
822
|
const result = this.db.prepare(`
|
|
772
823
|
UPDATE event_deliveries
|
|
773
824
|
SET monitor_attempt_count = monitor_attempt_count + 1,
|
|
@@ -846,7 +897,7 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
846
897
|
const state = this.mailboxSessionIsCurrent(request.sessionId, request.segmentId)
|
|
847
898
|
? 'pending'
|
|
848
899
|
: 'suppressed';
|
|
849
|
-
this.insertInitialDelivery(event, request.consumerId);
|
|
900
|
+
this.insertInitialDelivery(event, request.consumerId, ['monitor']);
|
|
850
901
|
this.db.prepare(`
|
|
851
902
|
INSERT INTO mailbox_items(
|
|
852
903
|
item_id, schema_version, formatter_version, consumer_id, session_id,
|
|
@@ -1230,7 +1281,7 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
1230
1281
|
monitor_delivered_at TEXT,
|
|
1231
1282
|
detail_state TEXT NOT NULL CHECK(detail_state IN ('not_applicable', 'pending', 'deferred', 'delivered')),
|
|
1232
1283
|
detail_delivered_at TEXT,
|
|
1233
|
-
detail_delivery_via TEXT CHECK(detail_delivery_via IS NULL OR detail_delivery_via IN ('stdout_full', 'mailbox_full', 'ccl_events', 'ccl_events_backlog')),
|
|
1284
|
+
detail_delivery_via TEXT CHECK(detail_delivery_via IS NULL OR detail_delivery_via IN ('stdout_full', 'mailbox_full', 'desktop_ipc', 'claude_channel', 'ccl_events', 'ccl_events_backlog')),
|
|
1234
1285
|
PRIMARY KEY(event_id, consumer_id),
|
|
1235
1286
|
CHECK(
|
|
1236
1287
|
(detail_state = 'delivered' AND detail_delivered_at IS NOT NULL AND detail_delivery_via IS NOT NULL)
|
|
@@ -1257,6 +1308,7 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
1257
1308
|
PRIMARY KEY(consumer_id, channel)
|
|
1258
1309
|
);
|
|
1259
1310
|
`);
|
|
1311
|
+
this.normalizeChannelV1Schema();
|
|
1260
1312
|
this.db.prepare(`
|
|
1261
1313
|
INSERT OR IGNORE INTO schema_migrations(version, applied_at) VALUES (1, ?)
|
|
1262
1314
|
`).run(new Date().toISOString());
|
|
@@ -1446,10 +1498,448 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
1446
1498
|
`).run(new Date().toISOString());
|
|
1447
1499
|
});
|
|
1448
1500
|
}
|
|
1501
|
+
|
|
1502
|
+
const outputAdapterMigration = this.db.prepare(`
|
|
1503
|
+
SELECT version FROM schema_migrations WHERE version = 8
|
|
1504
|
+
`).get();
|
|
1505
|
+
if (!outputAdapterMigration) {
|
|
1506
|
+
this.transaction(() => {
|
|
1507
|
+
const deliverySchema = this.db.prepare(`
|
|
1508
|
+
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'event_deliveries'
|
|
1509
|
+
`).get();
|
|
1510
|
+
const deliverySql = String(deliverySchema?.sql ?? '');
|
|
1511
|
+
if (!deliverySql.includes('desktop_ipc') || !deliverySql.includes('claude_channel')) {
|
|
1512
|
+
this.db.exec(`
|
|
1513
|
+
ALTER TABLE event_deliveries RENAME TO event_deliveries_v7;
|
|
1514
|
+
CREATE TABLE event_deliveries(
|
|
1515
|
+
event_id TEXT NOT NULL REFERENCES events(event_id),
|
|
1516
|
+
consumer_id TEXT NOT NULL,
|
|
1517
|
+
monitor_state TEXT NOT NULL CHECK(monitor_state IN ('not_applicable', 'pending', 'notified', 'suppressed')),
|
|
1518
|
+
monitor_attempt_count INTEGER NOT NULL DEFAULT 0 CHECK(monitor_attempt_count >= 0),
|
|
1519
|
+
monitor_last_attempt_at TEXT,
|
|
1520
|
+
monitor_delivered_at TEXT,
|
|
1521
|
+
detail_state TEXT NOT NULL CHECK(detail_state IN ('not_applicable', 'pending', 'deferred', 'delivered')),
|
|
1522
|
+
detail_delivered_at TEXT,
|
|
1523
|
+
detail_delivery_via TEXT CHECK(detail_delivery_via IS NULL OR detail_delivery_via IN ('stdout_full', 'mailbox_full', 'desktop_ipc', 'claude_channel', 'ccl_events', 'ccl_events_backlog')),
|
|
1524
|
+
PRIMARY KEY(event_id, consumer_id),
|
|
1525
|
+
CHECK(
|
|
1526
|
+
(detail_state = 'delivered' AND detail_delivered_at IS NOT NULL AND detail_delivery_via IS NOT NULL)
|
|
1527
|
+
OR
|
|
1528
|
+
(detail_state <> 'delivered' AND detail_delivered_at IS NULL AND detail_delivery_via IS NULL)
|
|
1529
|
+
)
|
|
1530
|
+
);
|
|
1531
|
+
INSERT INTO event_deliveries(
|
|
1532
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1533
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1534
|
+
detail_delivered_at, detail_delivery_via
|
|
1535
|
+
)
|
|
1536
|
+
SELECT
|
|
1537
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1538
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1539
|
+
detail_delivered_at, detail_delivery_via
|
|
1540
|
+
FROM event_deliveries_v7;
|
|
1541
|
+
DROP TABLE event_deliveries_v7;
|
|
1542
|
+
CREATE INDEX pending_details
|
|
1543
|
+
ON event_deliveries(consumer_id, detail_state, event_id);
|
|
1544
|
+
`);
|
|
1545
|
+
}
|
|
1546
|
+
this.db.prepare(`
|
|
1547
|
+
INSERT INTO schema_migrations(version, applied_at) VALUES (8, ?)
|
|
1548
|
+
`).run(new Date().toISOString());
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
const outputConsumerStateMigration = this.db.prepare(`
|
|
1553
|
+
SELECT version FROM schema_migrations WHERE version = 9
|
|
1554
|
+
`).get();
|
|
1555
|
+
if (!outputConsumerStateMigration) {
|
|
1556
|
+
this.transaction(() => {
|
|
1557
|
+
this.seedDedicatedOutputConsumers();
|
|
1558
|
+
this.db.prepare(`
|
|
1559
|
+
INSERT INTO schema_migrations(version, applied_at) VALUES (9, ?)
|
|
1560
|
+
`).run(new Date().toISOString());
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
const extensibleOutputAdapterMigration = this.db.prepare(`
|
|
1565
|
+
SELECT version FROM schema_migrations WHERE version = 10
|
|
1566
|
+
`).get();
|
|
1567
|
+
if (!extensibleOutputAdapterMigration) {
|
|
1568
|
+
this.transaction(() => {
|
|
1569
|
+
this.db.exec(`
|
|
1570
|
+
ALTER TABLE event_deliveries RENAME TO event_deliveries_v9;
|
|
1571
|
+
CREATE TABLE event_deliveries(
|
|
1572
|
+
event_id TEXT NOT NULL REFERENCES events(event_id),
|
|
1573
|
+
consumer_id TEXT NOT NULL,
|
|
1574
|
+
monitor_state TEXT NOT NULL CHECK(monitor_state IN ('not_applicable', 'pending', 'notified', 'suppressed')),
|
|
1575
|
+
monitor_attempt_count INTEGER NOT NULL DEFAULT 0 CHECK(monitor_attempt_count >= 0),
|
|
1576
|
+
monitor_last_attempt_at TEXT,
|
|
1577
|
+
monitor_delivered_at TEXT,
|
|
1578
|
+
detail_state TEXT NOT NULL CHECK(detail_state IN ('not_applicable', 'pending', 'deferred', 'delivered')),
|
|
1579
|
+
detail_delivered_at TEXT,
|
|
1580
|
+
detail_delivery_via TEXT CHECK(
|
|
1581
|
+
detail_delivery_via IS NULL OR (
|
|
1582
|
+
length(detail_delivery_via) BETWEEN 1 AND 120
|
|
1583
|
+
AND detail_delivery_via = lower(detail_delivery_via)
|
|
1584
|
+
AND substr(detail_delivery_via, 1, 1) GLOB '[a-z0-9]'
|
|
1585
|
+
AND detail_delivery_via NOT GLOB '*[^a-z0-9._-]*'
|
|
1586
|
+
)
|
|
1587
|
+
),
|
|
1588
|
+
PRIMARY KEY(event_id, consumer_id),
|
|
1589
|
+
CHECK(
|
|
1590
|
+
(detail_state = 'delivered' AND detail_delivered_at IS NOT NULL AND detail_delivery_via IS NOT NULL)
|
|
1591
|
+
OR
|
|
1592
|
+
(detail_state <> 'delivered' AND detail_delivered_at IS NULL AND detail_delivery_via IS NULL)
|
|
1593
|
+
)
|
|
1594
|
+
);
|
|
1595
|
+
INSERT INTO event_deliveries(
|
|
1596
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1597
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1598
|
+
detail_delivered_at, detail_delivery_via
|
|
1599
|
+
)
|
|
1600
|
+
SELECT
|
|
1601
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1602
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1603
|
+
detail_delivered_at, detail_delivery_via
|
|
1604
|
+
FROM event_deliveries_v9;
|
|
1605
|
+
UPDATE event_deliveries
|
|
1606
|
+
SET detail_state = 'not_applicable', detail_delivered_at = NULL,
|
|
1607
|
+
detail_delivery_via = NULL
|
|
1608
|
+
WHERE consumer_id = '${MONITOR_AGENT_CONSUMER_ID}';
|
|
1609
|
+
UPDATE event_deliveries
|
|
1610
|
+
SET monitor_state = 'not_applicable', monitor_attempt_count = 0,
|
|
1611
|
+
monitor_last_attempt_at = NULL, monitor_delivered_at = NULL
|
|
1612
|
+
WHERE consumer_id IN (
|
|
1613
|
+
'${CCL_EVENTS_AGENT_CONSUMER_ID}',
|
|
1614
|
+
'${DESKTOP_AGENT_CONSUMER_ID}',
|
|
1615
|
+
'${CLAUDE_CHANNEL_AGENT_CONSUMER_ID}'
|
|
1616
|
+
);
|
|
1617
|
+
DROP TABLE event_deliveries_v9;
|
|
1618
|
+
CREATE INDEX pending_details
|
|
1619
|
+
ON event_deliveries(consumer_id, detail_state, event_id);
|
|
1620
|
+
`);
|
|
1621
|
+
this.db.prepare(`
|
|
1622
|
+
INSERT INTO schema_migrations(version, applied_at) VALUES (10, ?)
|
|
1623
|
+
`).run(new Date().toISOString());
|
|
1624
|
+
});
|
|
1625
|
+
}
|
|
1449
1626
|
}
|
|
1450
1627
|
|
|
1451
|
-
|
|
1452
|
-
|
|
1628
|
+
/**
|
|
1629
|
+
* The Channels release created a divergent schema-v1 ledger: visible Events
|
|
1630
|
+
* used `agent_visible`, delivery rows had no Monitor/via columns, and leases
|
|
1631
|
+
* only admitted the details channel. Normalize that exact shape before the
|
|
1632
|
+
* incremental npm-lineage migrations inspect or copy those columns.
|
|
1633
|
+
*/
|
|
1634
|
+
private normalizeChannelV1Schema(): void {
|
|
1635
|
+
const deliveryColumns = new Set(
|
|
1636
|
+
this.db.prepare('PRAGMA table_info(event_deliveries)')
|
|
1637
|
+
.all()
|
|
1638
|
+
.map((row) => String(row.name)),
|
|
1639
|
+
);
|
|
1640
|
+
if (deliveryColumns.has('monitor_state') || deliveryColumns.has('detail_delivery_via')) {
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
const eventSchema = this.db.prepare(`
|
|
1644
|
+
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'events'
|
|
1645
|
+
`).get();
|
|
1646
|
+
if (!String(eventSchema?.sql ?? '').includes('agent_visible')) return;
|
|
1647
|
+
|
|
1648
|
+
this.db.exec('PRAGMA foreign_keys = OFF');
|
|
1649
|
+
try {
|
|
1650
|
+
this.transaction(() => {
|
|
1651
|
+
this.db.exec(`
|
|
1652
|
+
DROP INDEX IF EXISTS events_by_remote_id;
|
|
1653
|
+
DROP INDEX IF EXISTS events_by_remote_sequence;
|
|
1654
|
+
DROP INDEX IF EXISTS events_by_scope;
|
|
1655
|
+
DROP INDEX IF EXISTS events_by_type;
|
|
1656
|
+
DROP INDEX IF EXISTS events_by_epoch;
|
|
1657
|
+
DROP INDEX IF EXISTS pending_details;
|
|
1658
|
+
|
|
1659
|
+
ALTER TABLE event_deliveries RENAME TO event_deliveries_channel_v1;
|
|
1660
|
+
ALTER TABLE source_checkpoints RENAME TO source_checkpoints_channel_v1;
|
|
1661
|
+
ALTER TABLE consumer_leases RENAME TO consumer_leases_channel_v1;
|
|
1662
|
+
ALTER TABLE events RENAME TO events_channel_v1;
|
|
1663
|
+
|
|
1664
|
+
CREATE TABLE events(
|
|
1665
|
+
commit_seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1666
|
+
event_id TEXT NOT NULL UNIQUE,
|
|
1667
|
+
epoch_seq INTEGER,
|
|
1668
|
+
session_id TEXT NOT NULL REFERENCES play_sessions(session_id),
|
|
1669
|
+
segment_id TEXT NOT NULL REFERENCES event_segments(segment_id),
|
|
1670
|
+
type TEXT NOT NULL,
|
|
1671
|
+
producer TEXT NOT NULL CHECK(producer IN ('backend_push', 'local_synth')),
|
|
1672
|
+
producer_plugin_id TEXT,
|
|
1673
|
+
producer_output_index INTEGER,
|
|
1674
|
+
delivery_class TEXT NOT NULL CHECK(delivery_class IN ('monitor_and_events', 'events_only', 'internal_only')),
|
|
1675
|
+
occurred_at TEXT NOT NULL,
|
|
1676
|
+
received_at TEXT NOT NULL,
|
|
1677
|
+
payload_json TEXT NOT NULL,
|
|
1678
|
+
source_id TEXT NOT NULL,
|
|
1679
|
+
source_event_id TEXT,
|
|
1680
|
+
source_sequence TEXT,
|
|
1681
|
+
connection_id TEXT,
|
|
1682
|
+
replayed INTEGER NOT NULL DEFAULT 0 CHECK(replayed IN (0, 1))
|
|
1683
|
+
);
|
|
1684
|
+
INSERT INTO events(
|
|
1685
|
+
commit_seq, event_id, epoch_seq, session_id, segment_id, type,
|
|
1686
|
+
producer, producer_plugin_id, producer_output_index, delivery_class,
|
|
1687
|
+
occurred_at, received_at, payload_json, source_id, source_event_id,
|
|
1688
|
+
source_sequence, connection_id, replayed
|
|
1689
|
+
)
|
|
1690
|
+
SELECT
|
|
1691
|
+
commit_seq, event_id, epoch_seq, session_id, segment_id, type,
|
|
1692
|
+
producer, producer_plugin_id, producer_output_index,
|
|
1693
|
+
CASE
|
|
1694
|
+
WHEN delivery_class = 'agent_visible' THEN 'monitor_and_events'
|
|
1695
|
+
ELSE 'internal_only'
|
|
1696
|
+
END,
|
|
1697
|
+
occurred_at, received_at, payload_json, source_id, source_event_id,
|
|
1698
|
+
source_sequence, connection_id, replayed
|
|
1699
|
+
FROM events_channel_v1;
|
|
1700
|
+
|
|
1701
|
+
CREATE TABLE event_deliveries(
|
|
1702
|
+
event_id TEXT NOT NULL REFERENCES events(event_id),
|
|
1703
|
+
consumer_id TEXT NOT NULL,
|
|
1704
|
+
monitor_state TEXT NOT NULL CHECK(monitor_state IN ('not_applicable', 'pending', 'notified', 'suppressed')),
|
|
1705
|
+
monitor_attempt_count INTEGER NOT NULL DEFAULT 0 CHECK(monitor_attempt_count >= 0),
|
|
1706
|
+
monitor_last_attempt_at TEXT,
|
|
1707
|
+
monitor_delivered_at TEXT,
|
|
1708
|
+
detail_state TEXT NOT NULL CHECK(detail_state IN ('not_applicable', 'pending', 'deferred', 'delivered')),
|
|
1709
|
+
detail_delivered_at TEXT,
|
|
1710
|
+
detail_delivery_via TEXT CHECK(detail_delivery_via IS NULL OR detail_delivery_via IN ('stdout_full', 'mailbox_full', 'desktop_ipc', 'claude_channel', 'ccl_events', 'ccl_events_backlog')),
|
|
1711
|
+
PRIMARY KEY(event_id, consumer_id),
|
|
1712
|
+
CHECK(
|
|
1713
|
+
(detail_state = 'delivered' AND detail_delivered_at IS NOT NULL AND detail_delivery_via IS NOT NULL)
|
|
1714
|
+
OR
|
|
1715
|
+
(detail_state <> 'delivered' AND detail_delivered_at IS NULL AND detail_delivery_via IS NULL)
|
|
1716
|
+
)
|
|
1717
|
+
);
|
|
1718
|
+
INSERT INTO event_deliveries(
|
|
1719
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1720
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1721
|
+
detail_delivered_at, detail_delivery_via
|
|
1722
|
+
)
|
|
1723
|
+
SELECT
|
|
1724
|
+
d.event_id,
|
|
1725
|
+
d.consumer_id,
|
|
1726
|
+
CASE
|
|
1727
|
+
WHEN e.delivery_class = 'internal_only' THEN 'not_applicable'
|
|
1728
|
+
WHEN p.is_current = 1 THEN 'pending'
|
|
1729
|
+
ELSE 'suppressed'
|
|
1730
|
+
END,
|
|
1731
|
+
0,
|
|
1732
|
+
NULL,
|
|
1733
|
+
NULL,
|
|
1734
|
+
d.detail_state,
|
|
1735
|
+
d.detail_delivered_at,
|
|
1736
|
+
CASE WHEN d.detail_state = 'delivered' THEN 'claude_channel' ELSE NULL END
|
|
1737
|
+
FROM event_deliveries_channel_v1 d
|
|
1738
|
+
JOIN events e ON e.event_id = d.event_id
|
|
1739
|
+
JOIN play_sessions p ON p.session_id = e.session_id;
|
|
1740
|
+
|
|
1741
|
+
CREATE TABLE source_checkpoints(
|
|
1742
|
+
source_id TEXT PRIMARY KEY,
|
|
1743
|
+
checkpoint TEXT NOT NULL,
|
|
1744
|
+
source_sequence TEXT,
|
|
1745
|
+
event_id TEXT NOT NULL REFERENCES events(event_id),
|
|
1746
|
+
committed_at TEXT NOT NULL
|
|
1747
|
+
);
|
|
1748
|
+
INSERT INTO source_checkpoints(
|
|
1749
|
+
source_id, checkpoint, source_sequence, event_id, committed_at
|
|
1750
|
+
)
|
|
1751
|
+
SELECT source_id, checkpoint, source_sequence, event_id, committed_at
|
|
1752
|
+
FROM source_checkpoints_channel_v1;
|
|
1753
|
+
|
|
1754
|
+
CREATE TABLE consumer_leases(
|
|
1755
|
+
consumer_id TEXT NOT NULL,
|
|
1756
|
+
channel TEXT NOT NULL CHECK(channel IN ('monitor', 'details')),
|
|
1757
|
+
owner_id TEXT NOT NULL,
|
|
1758
|
+
expires_at INTEGER NOT NULL,
|
|
1759
|
+
PRIMARY KEY(consumer_id, channel)
|
|
1760
|
+
);
|
|
1761
|
+
|
|
1762
|
+
DROP TABLE event_deliveries_channel_v1;
|
|
1763
|
+
DROP TABLE source_checkpoints_channel_v1;
|
|
1764
|
+
DROP TABLE consumer_leases_channel_v1;
|
|
1765
|
+
DROP TABLE events_channel_v1;
|
|
1766
|
+
|
|
1767
|
+
CREATE UNIQUE INDEX events_by_remote_id
|
|
1768
|
+
ON events(source_id, source_event_id) WHERE source_event_id IS NOT NULL;
|
|
1769
|
+
CREATE UNIQUE INDEX events_by_remote_sequence
|
|
1770
|
+
ON events(source_id, session_id, segment_id, source_sequence)
|
|
1771
|
+
WHERE source_sequence IS NOT NULL;
|
|
1772
|
+
CREATE INDEX events_by_scope
|
|
1773
|
+
ON events(session_id, segment_id, commit_seq);
|
|
1774
|
+
CREATE INDEX events_by_type
|
|
1775
|
+
ON events(session_id, type, commit_seq DESC);
|
|
1776
|
+
CREATE INDEX events_by_epoch
|
|
1777
|
+
ON events(session_id, segment_id, epoch_seq, commit_seq);
|
|
1778
|
+
CREATE INDEX pending_details
|
|
1779
|
+
ON event_deliveries(consumer_id, detail_state, event_id);
|
|
1780
|
+
`);
|
|
1781
|
+
const violations = this.db.prepare('PRAGMA foreign_key_check').all();
|
|
1782
|
+
if (violations.length > 0) {
|
|
1783
|
+
throw new Error('Channel-v1 Event ledger migration violated foreign keys.');
|
|
1784
|
+
}
|
|
1785
|
+
});
|
|
1786
|
+
} finally {
|
|
1787
|
+
this.db.exec('PRAGMA foreign_keys = ON');
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/**
|
|
1792
|
+
* Split the old logical Agent cursor into output-specific cursors without
|
|
1793
|
+
* treating one transport's acknowledgement as proof for another transport.
|
|
1794
|
+
*/
|
|
1795
|
+
private seedDedicatedOutputConsumers(): void {
|
|
1796
|
+
this.db.prepare(`
|
|
1797
|
+
INSERT INTO event_deliveries(
|
|
1798
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1799
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1800
|
+
detail_delivered_at, detail_delivery_via
|
|
1801
|
+
)
|
|
1802
|
+
SELECT
|
|
1803
|
+
event_id, ?, monitor_state, monitor_attempt_count,
|
|
1804
|
+
monitor_last_attempt_at, monitor_delivered_at,
|
|
1805
|
+
'not_applicable', NULL, NULL
|
|
1806
|
+
FROM event_deliveries
|
|
1807
|
+
WHERE consumer_id = ?
|
|
1808
|
+
ON CONFLICT(event_id, consumer_id) DO UPDATE SET
|
|
1809
|
+
monitor_state = CASE
|
|
1810
|
+
WHEN event_deliveries.monitor_state = 'notified' THEN 'notified'
|
|
1811
|
+
WHEN excluded.monitor_state = 'notified' THEN 'notified'
|
|
1812
|
+
WHEN event_deliveries.monitor_state = 'suppressed' THEN 'suppressed'
|
|
1813
|
+
ELSE excluded.monitor_state
|
|
1814
|
+
END,
|
|
1815
|
+
monitor_attempt_count = MAX(
|
|
1816
|
+
event_deliveries.monitor_attempt_count,
|
|
1817
|
+
excluded.monitor_attempt_count
|
|
1818
|
+
),
|
|
1819
|
+
monitor_last_attempt_at = CASE
|
|
1820
|
+
WHEN event_deliveries.monitor_attempt_count >= excluded.monitor_attempt_count
|
|
1821
|
+
THEN event_deliveries.monitor_last_attempt_at
|
|
1822
|
+
ELSE excluded.monitor_last_attempt_at
|
|
1823
|
+
END,
|
|
1824
|
+
monitor_delivered_at = COALESCE(
|
|
1825
|
+
event_deliveries.monitor_delivered_at,
|
|
1826
|
+
excluded.monitor_delivered_at
|
|
1827
|
+
),
|
|
1828
|
+
detail_state = 'not_applicable',
|
|
1829
|
+
detail_delivered_at = NULL,
|
|
1830
|
+
detail_delivery_via = NULL
|
|
1831
|
+
`).run(MONITOR_AGENT_CONSUMER_ID, LEGACY_AGENT_CONSUMER_ID);
|
|
1832
|
+
|
|
1833
|
+
this.seedDedicatedDetailConsumer(
|
|
1834
|
+
CCL_EVENTS_AGENT_CONSUMER_ID,
|
|
1835
|
+
['ccl_events', 'ccl_events_backlog', 'stdout_full'],
|
|
1836
|
+
);
|
|
1837
|
+
this.seedDedicatedDetailConsumer(
|
|
1838
|
+
DESKTOP_AGENT_CONSUMER_ID,
|
|
1839
|
+
['mailbox_full', 'desktop_ipc'],
|
|
1840
|
+
'desktop_ipc',
|
|
1841
|
+
);
|
|
1842
|
+
this.seedDedicatedDetailConsumer(
|
|
1843
|
+
CLAUDE_CHANNEL_AGENT_CONSUMER_ID,
|
|
1844
|
+
['claude_channel'],
|
|
1845
|
+
'claude_channel',
|
|
1846
|
+
);
|
|
1847
|
+
|
|
1848
|
+
// A legacy short item belongs to the old Monitor lane. It cannot be moved
|
|
1849
|
+
// with its claim token; suppress it so a pending :monitor delivery can be
|
|
1850
|
+
// projected into a fresh item owned by the dedicated consumer.
|
|
1851
|
+
this.db.prepare(`
|
|
1852
|
+
UPDATE mailbox_items
|
|
1853
|
+
SET state = 'suppressed', claim_owner = NULL, claim_token = NULL,
|
|
1854
|
+
claim_expires_at = NULL, updated_at = ?
|
|
1855
|
+
WHERE consumer_id = ?
|
|
1856
|
+
AND mode = 'short'
|
|
1857
|
+
AND state IN ('pending', 'claimed')
|
|
1858
|
+
`).run(new Date().toISOString(), LEGACY_AGENT_CONSUMER_ID);
|
|
1859
|
+
|
|
1860
|
+
// Leases are process ownership, not delivery evidence. Never transfer a
|
|
1861
|
+
// pre-upgrade owner token into any dedicated output lane.
|
|
1862
|
+
this.db.prepare(`
|
|
1863
|
+
DELETE FROM consumer_leases WHERE consumer_id = ?
|
|
1864
|
+
`).run(LEGACY_AGENT_CONSUMER_ID);
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
private seedDedicatedDetailConsumer(
|
|
1868
|
+
consumerId: string,
|
|
1869
|
+
evidence: readonly string[],
|
|
1870
|
+
normalizedVia?: string,
|
|
1871
|
+
): void {
|
|
1872
|
+
const evidenceFilter = placeholders(evidence.length);
|
|
1873
|
+
const deliveredEvidence = `
|
|
1874
|
+
legacy.detail_state = 'delivered'
|
|
1875
|
+
AND legacy.detail_delivery_via IN (${evidenceFilter})
|
|
1876
|
+
`;
|
|
1877
|
+
const params: unknown[] = [
|
|
1878
|
+
consumerId,
|
|
1879
|
+
...evidence,
|
|
1880
|
+
...evidence,
|
|
1881
|
+
...evidence,
|
|
1882
|
+
...(normalizedVia ? [normalizedVia] : []),
|
|
1883
|
+
LEGACY_AGENT_CONSUMER_ID,
|
|
1884
|
+
];
|
|
1885
|
+
this.db.prepare(`
|
|
1886
|
+
INSERT INTO event_deliveries(
|
|
1887
|
+
event_id, consumer_id, monitor_state, monitor_attempt_count,
|
|
1888
|
+
monitor_last_attempt_at, monitor_delivered_at, detail_state,
|
|
1889
|
+
detail_delivered_at, detail_delivery_via
|
|
1890
|
+
)
|
|
1891
|
+
SELECT
|
|
1892
|
+
legacy.event_id,
|
|
1893
|
+
?,
|
|
1894
|
+
'not_applicable',
|
|
1895
|
+
0,
|
|
1896
|
+
NULL,
|
|
1897
|
+
NULL,
|
|
1898
|
+
CASE
|
|
1899
|
+
WHEN e.delivery_class = 'internal_only'
|
|
1900
|
+
OR legacy.detail_state = 'not_applicable' THEN 'not_applicable'
|
|
1901
|
+
WHEN ${deliveredEvidence} THEN 'delivered'
|
|
1902
|
+
WHEN p.is_current = 1 AND s.is_current = 1 THEN 'pending'
|
|
1903
|
+
ELSE 'deferred'
|
|
1904
|
+
END,
|
|
1905
|
+
CASE WHEN ${deliveredEvidence} THEN legacy.detail_delivered_at ELSE NULL END,
|
|
1906
|
+
CASE WHEN ${deliveredEvidence}
|
|
1907
|
+
THEN ${normalizedVia ? '?' : 'legacy.detail_delivery_via'}
|
|
1908
|
+
ELSE NULL
|
|
1909
|
+
END
|
|
1910
|
+
FROM event_deliveries legacy
|
|
1911
|
+
JOIN events e ON e.event_id = legacy.event_id
|
|
1912
|
+
JOIN play_sessions p ON p.session_id = e.session_id
|
|
1913
|
+
JOIN event_segments s ON s.segment_id = e.segment_id
|
|
1914
|
+
WHERE legacy.consumer_id = ?
|
|
1915
|
+
ON CONFLICT(event_id, consumer_id) DO UPDATE SET
|
|
1916
|
+
monitor_state = 'not_applicable',
|
|
1917
|
+
monitor_attempt_count = 0,
|
|
1918
|
+
monitor_last_attempt_at = NULL,
|
|
1919
|
+
monitor_delivered_at = NULL,
|
|
1920
|
+
detail_state = CASE
|
|
1921
|
+
WHEN event_deliveries.detail_state = 'delivered' THEN 'delivered'
|
|
1922
|
+
ELSE excluded.detail_state
|
|
1923
|
+
END,
|
|
1924
|
+
detail_delivered_at = CASE
|
|
1925
|
+
WHEN event_deliveries.detail_state = 'delivered'
|
|
1926
|
+
THEN event_deliveries.detail_delivered_at
|
|
1927
|
+
ELSE excluded.detail_delivered_at
|
|
1928
|
+
END,
|
|
1929
|
+
detail_delivery_via = CASE
|
|
1930
|
+
WHEN event_deliveries.detail_state = 'delivered'
|
|
1931
|
+
THEN event_deliveries.detail_delivery_via
|
|
1932
|
+
ELSE excluded.detail_delivery_via
|
|
1933
|
+
END
|
|
1934
|
+
`).run(...params);
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
private insertInitialDelivery(
|
|
1938
|
+
event: CanonicalEvent,
|
|
1939
|
+
consumerId: string,
|
|
1940
|
+
channels?: readonly ConsumerLease['channel'][],
|
|
1941
|
+
): void {
|
|
1942
|
+
const delivery = initialDeliveryFor(event, consumerId, channels);
|
|
1453
1943
|
const scope = this.db.prepare(`
|
|
1454
1944
|
SELECT s.is_current AS segment_is_current, p.is_current AS session_is_current
|
|
1455
1945
|
FROM event_segments s
|
|
@@ -1472,10 +1962,21 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
1472
1962
|
delivery.monitorState = 'suppressed';
|
|
1473
1963
|
}
|
|
1474
1964
|
this.db.prepare(`
|
|
1475
|
-
INSERT
|
|
1965
|
+
INSERT INTO event_deliveries(
|
|
1476
1966
|
event_id, consumer_id, monitor_state, monitor_delivered_at,
|
|
1477
1967
|
detail_state, detail_delivered_at, detail_delivery_via
|
|
1478
1968
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1969
|
+
ON CONFLICT(event_id, consumer_id) DO UPDATE SET
|
|
1970
|
+
monitor_state = CASE
|
|
1971
|
+
WHEN event_deliveries.monitor_state = 'not_applicable'
|
|
1972
|
+
THEN excluded.monitor_state
|
|
1973
|
+
ELSE event_deliveries.monitor_state
|
|
1974
|
+
END,
|
|
1975
|
+
detail_state = CASE
|
|
1976
|
+
WHEN event_deliveries.detail_state = 'not_applicable'
|
|
1977
|
+
THEN excluded.detail_state
|
|
1978
|
+
ELSE event_deliveries.detail_state
|
|
1979
|
+
END
|
|
1479
1980
|
`).run(
|
|
1480
1981
|
delivery.eventId,
|
|
1481
1982
|
delivery.consumerId,
|
|
@@ -1663,8 +2164,17 @@ export class SqliteEventRepository implements EventRepository, MailboxRepository
|
|
|
1663
2164
|
params.push(...values);
|
|
1664
2165
|
}
|
|
1665
2166
|
|
|
1666
|
-
private assertLeaseIfPresent(
|
|
1667
|
-
|
|
2167
|
+
private assertLeaseIfPresent(
|
|
2168
|
+
lease: ConsumerLease | undefined,
|
|
2169
|
+
expectedConsumerId: string,
|
|
2170
|
+
expectedChannel: ConsumerLease['channel'],
|
|
2171
|
+
nowMs?: number,
|
|
2172
|
+
): void {
|
|
2173
|
+
if (!lease) return;
|
|
2174
|
+
if (lease.consumerId !== expectedConsumerId || lease.channel !== expectedChannel) {
|
|
2175
|
+
throw new DeliveryLeaseScopeError(lease, expectedConsumerId, expectedChannel);
|
|
2176
|
+
}
|
|
2177
|
+
if (!this.leaseIsValid(lease, nowMs)) throw new DeliveryLeaseLostError(lease);
|
|
1668
2178
|
}
|
|
1669
2179
|
|
|
1670
2180
|
private transaction<T>(fn: () => T): T {
|