@myclaw163/clawclaw-cli 0.8.4 → 0.8.6
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/README.md +4 -3
- package/docs/configuration.md +44 -26
- package/docs/custom-modules/README.md +7 -10
- package/docs/custom-modules/behavior.md +10 -19
- package/docs/custom-modules/perception.md +1 -1
- package/docs/custom-modules/trigger.md +1 -1
- package/docs/runtime/README.md +4 -4
- package/docs/runtime/official-perception-modules.md +2 -2
- package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
- package/package.json +15 -2
- package/scripts/enable_ccl_permissions.cjs +4 -2
- package/scripts/find-hide-spots.py +2 -2
- package/scripts/postinstall.mjs +1 -4
- package/scripts/sync-bundled-skill.mjs +12 -159
- package/skills/clawclaw/SKILL.md +25 -18
- package/skills/clawclaw/references/BEHAVIORS.md +15 -11
- package/skills/clawclaw/references/COMMANDS.md +45 -41
- package/skills/clawclaw/references/HUB.md +25 -38
- package/skills/clawclaw/references/TACTICS.md +1 -1
- package/src/commands/account.ts +58 -14
- package/src/commands/behavior.ts +116 -38
- package/src/commands/hub.ts +110 -153
- package/src/commands/load.ts +138 -33
- package/src/commands/perception.ts +55 -24
- package/src/commands/persona.ts +66 -20
- package/src/commands/skill.ts +12 -48
- package/src/commands/trigger.ts +68 -40
- package/src/lib/account-guidance.ts +54 -9
- package/src/lib/agent-workspace-client.ts +135 -0
- package/src/lib/agent-workspace-runtime.ts +154 -0
- package/src/lib/agent-workspace-skills.ts +177 -0
- package/src/lib/agent-workspace-sync.ts +852 -0
- package/src/lib/gameplay-config.ts +29 -15
- package/src/lib/hub-client.ts +252 -34
- package/src/lib/hub-reminder.ts +24 -11
- package/src/lib/init-command.ts +0 -6
- package/src/lib/load-context.ts +12 -6
- package/src/lib/persona.ts +141 -69
- package/src/lib/server-status.ts +78 -0
- package/src/lib/skill-host.ts +160 -0
- package/src/lib/user-data.ts +197 -4
- package/src/runtime/clawclaw/behavior-read-api.ts +1 -1
- package/src/{strategies/pathfind → runtime/clawclaw/navigation}/astar-path.ts +1 -1
- package/src/{strategies/pathfind → runtime/clawclaw/navigation}/escape-planner.ts +1 -1
- package/src/{strategies → runtime/clawclaw/navigation}/hide-spots.ts +2 -2
- package/src/{strategies → runtime/clawclaw/navigation}/off-route-points.ts +6 -3
- package/src/{strategies/pathfind → runtime/clawclaw/navigation}/walkable-grid.ts +1 -1
- package/src/runtime/clawclaw/owner.ts +63 -19
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +8 -2
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +2 -2
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +1 -1
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +4 -1
- package/src/runtime/clawclaw/plugins/perception/perception-source.ts +11 -5
- package/src/runtime/clawclaw/plugins/perception/replay.ts +0 -2
- package/src/runtime/framework/modules/runtime-module-loader.ts +0 -54
- package/src/sdk/behavior-toolkit/escape.ts +2 -2
- package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +0 -50
- package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +0 -51
- package/personas//346/227/240/344/272/272/350/256/276.md +0 -1
- package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +0 -48
- package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +0 -51
- package/scripts/compare-perception-replays.test.ts +0 -188
- package/scripts/enable_ccl_permissions.test.mjs +0 -163
- package/scripts/enable_monitor.test.mjs +0 -201
- package/scripts/sync-bundled-skill.test.mjs +0 -152
- package/scripts/watch-perception-events.test.ts +0 -43
- package/src/behaviors/official-finite/do-task.ts +0 -211
- package/src/behaviors/official-finite/evade.ts +0 -78
- package/src/behaviors/official-finite/find-player.ts +0 -50
- package/src/behaviors/official-finite/follow-player.ts +0 -70
- package/src/behaviors/official-finite/go-to.ts +0 -69
- package/src/behaviors/official-finite/hide-temporarily.ts +0 -77
- package/src/behaviors/official-finite/hold-position.ts +0 -48
- package/src/behaviors/official-finite/hunt-player.ts +0 -47
- package/src/behaviors/official-finite/index.test.ts +0 -249
- package/src/behaviors/official-finite/index.ts +0 -44
- package/src/behaviors/official-finite/inspect-corpse.ts +0 -64
- package/src/behaviors/official-finite/patrol-area.ts +0 -80
- package/src/behaviors/official-finite/report-corpse.ts +0 -73
- package/src/behaviors/official-finite/seek-kill-opportunity.ts +0 -152
- package/src/behaviors/official-finite/shared.ts +0 -27
- package/src/channel/claude-channel-server.test.ts +0 -202
- package/src/commands/account.test.ts +0 -570
- package/src/commands/behavior.test.ts +0 -158
- package/src/commands/communication.test.ts +0 -282
- package/src/commands/competition.test.ts +0 -116
- package/src/commands/data.test.ts +0 -89
- package/src/commands/events-canonical-state.test.ts +0 -406
- package/src/commands/events.test.ts +0 -58
- package/src/commands/game/index.test.ts +0 -1251
- package/src/commands/game-map.test.ts +0 -69
- package/src/commands/hub.test.ts +0 -124
- package/src/commands/load.test.ts +0 -440
- package/src/commands/perception.test.ts +0 -75
- package/src/commands/setup/hermes.test.ts +0 -96
- package/src/commands/setup/openclaw.test.ts +0 -124
- package/src/commands/trigger.test.ts +0 -140
- package/src/commands/upgrade.test.ts +0 -82
- package/src/lib/account-guidance.test.ts +0 -36
- package/src/lib/auth.test.ts +0 -151
- package/src/lib/behavior-export.test.ts +0 -326
- package/src/lib/behavior-export.ts +0 -537
- package/src/lib/clawclaw-skill-spectate-guidance.test.ts +0 -21
- package/src/lib/custom-tts.test.ts +0 -387
- package/src/lib/diagnostics-config.test.ts +0 -59
- package/src/lib/game-client-tts.test.ts +0 -151
- package/src/lib/game-client.test.ts +0 -250
- package/src/lib/gameplay-config.test.ts +0 -301
- package/src/lib/host-config-patcher.test.ts +0 -130
- package/src/lib/http-transport.test.ts +0 -124
- package/src/lib/hub-client.test.ts +0 -57
- package/src/lib/hub-install.test.ts +0 -135
- package/src/lib/hub-install.ts +0 -185
- package/src/lib/hub-unzip.test.ts +0 -69
- package/src/lib/init-command.test.ts +0 -103
- package/src/lib/load-context.test.ts +0 -62
- package/src/lib/match-state.test.ts +0 -134
- package/src/lib/official-resource-sync.test.ts +0 -199
- package/src/lib/official-resource-sync.ts +0 -221
- package/src/lib/perception-config.test.ts +0 -143
- package/src/lib/perception-export.test.ts +0 -145
- package/src/lib/perception-export.ts +0 -178
- package/src/lib/persona.test.ts +0 -84
- package/src/lib/room.test.ts +0 -88
- package/src/lib/server-registry.test.ts +0 -64
- package/src/lib/service-status.test.ts +0 -81
- package/src/lib/skill-version.test.ts +0 -48
- package/src/lib/trigger-config.test.ts +0 -102
- package/src/lib/trigger-export.test.ts +0 -80
- package/src/lib/trigger-export.ts +0 -210
- package/src/lib/user-data.test.ts +0 -121
- package/src/lib/workspace-argv.test.ts +0 -49
- package/src/pipeline/canonical-event-presentation.test.ts +0 -257
- package/src/pipeline/event-format.test.ts +0 -879
- package/src/pipeline/event-hints.test.ts +0 -337
- package/src/pipeline/game-sync-event-normalization.test.ts +0 -419
- package/src/pipeline/player-projection.test.ts +0 -247
- package/src/runtime/auto-upgrade.test.ts +0 -66
- package/src/runtime/canonical-event-emitter.test.ts +0 -70
- package/src/runtime/canonical-monitor.test.ts +0 -577
- package/src/runtime/clawclaw/agent-event-policy.test.ts +0 -60
- package/src/runtime/clawclaw/behavior-decision.test.ts +0 -43
- package/src/runtime/clawclaw/behavior-input-fields.test.ts +0 -175
- package/src/runtime/clawclaw/behavior-input.test.ts +0 -173
- package/src/runtime/clawclaw/behavior-observation-memory.test.ts +0 -59
- package/src/runtime/clawclaw/behavior-read-api.test.ts +0 -123
- package/src/runtime/clawclaw/notifications.test.ts +0 -47
- package/src/runtime/clawclaw/owner-lock.test.ts +0 -221
- package/src/runtime/clawclaw/owner.test.ts +0 -203
- package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +0 -328
- package/src/runtime/clawclaw/plugins/assembly.test.ts +0 -200
- package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +0 -180
- package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +0 -43
- package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +0 -156
- package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +0 -182
- package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +0 -1094
- package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +0 -174
- package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +0 -195
- package/src/runtime/clawclaw/plugins/behavior/official-goal-behavior.ts +0 -757
- package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +0 -283
- package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +0 -80
- package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +0 -267
- package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +0 -226
- package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +0 -239
- package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +0 -407
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +0 -397
- package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +0 -270
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +0 -166
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +0 -194
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +0 -87
- package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +0 -2012
- package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +0 -214
- package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +0 -347
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +0 -92
- package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +0 -248
- package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +0 -389
- package/src/runtime/clawclaw/plugins/perception/modules/index.ts +0 -8
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +0 -342
- package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +0 -475
- package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +0 -30
- package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +0 -304
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +0 -179
- package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +0 -364
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +0 -81
- package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +0 -200
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +0 -216
- package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +0 -284
- package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +0 -120
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +0 -105
- package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +0 -269
- package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +0 -865
- package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +0 -1878
- package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +0 -278
- package/src/runtime/clawclaw/plugins/perception/replay.test.ts +0 -77
- package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +0 -853
- package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +0 -287
- package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +0 -321
- package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +0 -307
- package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +0 -2646
- package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +0 -212
- package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +0 -548
- package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +0 -169
- package/src/runtime/clawclaw/runtime-data-export.test.ts +0 -249
- package/src/runtime/clawclaw-event-catalog.test.ts +0 -128
- package/src/runtime/framework/action-diagnostics.test.ts +0 -81
- package/src/runtime/framework/capability-registry.test.ts +0 -423
- package/src/runtime/framework/control-coordinator.test.ts +0 -675
- package/src/runtime/framework/events/event-delivery-service.test.ts +0 -84
- package/src/runtime/framework/events/event-ingress.test.ts +0 -203
- package/src/runtime/framework/events/mailbox-event-repositories.test.ts +0 -710
- package/src/runtime/framework/events/sqlite-event-repository.test.ts +0 -802
- package/src/runtime/framework/mailbox/coalescer.test.ts +0 -55
- package/src/runtime/framework/mailbox/identity.test.ts +0 -38
- package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +0 -513
- package/src/runtime/framework/mailbox/notification-projector.test.ts +0 -77
- package/src/runtime/framework/modules/runtime-module-loader.test.ts +0 -435
- package/src/runtime/framework/participation-manager.test.ts +0 -108
- package/src/runtime/framework/plugins/runtime-input-journal.test.ts +0 -179
- package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +0 -2040
- package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +0 -144
- package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +0 -157
- package/src/runtime/framework/state/history/state-history.test.ts +0 -251
- package/src/runtime/framework/state/state-core.test.ts +0 -197
- package/src/runtime/game-sync-protocol.test.ts +0 -123
- package/src/runtime/host-monitor-rate-gate.test.ts +0 -73
- package/src/runtime/mailbox-host-relay.test.ts +0 -293
- package/src/runtime/observability/observability.test.ts +0 -583
- package/src/runtime/owner-control.test.ts +0 -153
- package/src/runtime/parent-process-watch.test.ts +0 -27
- package/src/runtime/pending-event-handoff.test.ts +0 -29
- package/src/runtime/raw-ws-log.test.ts +0 -102
- package/src/runtime/runtime-logger.test.ts +0 -58
- package/src/runtime/runtime-persistence.test.ts +0 -26
- package/src/runtime/runtime.test.ts +0 -1770
- package/src/runtime/ws-client.test.ts +0 -562
- package/src/sdk/action.test.ts +0 -8
- package/src/sdk/behavior-plugin/index.test.ts +0 -74
- package/src/sdk/index.test.ts +0 -45
- package/src/sdk/module-input/index.test.ts +0 -49
- package/src/sdk/perception-plugin/index.test.ts +0 -22
- package/src/sdk/resource-metadata.test.ts +0 -40
- package/src/sdk/trigger-plugin/index.test.ts +0 -41
- package/src/short-command/classifier.test.ts +0 -107
- package/src/short-command/presenter.test.ts +0 -119
- package/src/short-command/runner.test.ts +0 -78
- package/src/short-command/sensitive-data-filter.test.ts +0 -56
- package/src/short-command/transport-response.test.ts +0 -39
- package/src/strategies/avoid-lone.ts +0 -32
- package/src/strategies/avoid-players.ts +0 -43
- package/src/strategies/corpse-patrol.ts +0 -50
- package/src/strategies/crab-sabotage.ts +0 -48
- package/src/strategies/custom-module.test.ts +0 -267
- package/src/strategies/find-player.ts +0 -49
- package/src/strategies/game-utils.test.ts +0 -305
- package/src/strategies/game-utils.ts +0 -927
- package/src/strategies/goals/anchor-linger.ts +0 -77
- package/src/strategies/goals/avoid-lone-top.ts +0 -189
- package/src/strategies/goals/avoid-players-top.test.ts +0 -95
- package/src/strategies/goals/avoid-players-top.ts +0 -122
- package/src/strategies/goals/conversation-goal.ts +0 -51
- package/src/strategies/goals/corpse-patrol-top.ts +0 -120
- package/src/strategies/goals/crab-octopus-reflexes.ts +0 -109
- package/src/strategies/goals/crab-sabotage-top.test.ts +0 -131
- package/src/strategies/goals/crab-sabotage-top.ts +0 -139
- package/src/strategies/goals/emergency-hunt-goal.ts +0 -40
- package/src/strategies/goals/find-player-top.ts +0 -136
- package/src/strategies/goals/flee-players-goal.ts +0 -55
- package/src/strategies/goals/follow-companion-goal.ts +0 -130
- package/src/strategies/goals/goal-manager.ts +0 -41
- package/src/strategies/goals/goal-root-strategy.ts +0 -49
- package/src/strategies/goals/goal.ts +0 -28
- package/src/strategies/goals/hide-top.ts +0 -240
- package/src/strategies/goals/keep-away-goal.ts +0 -243
- package/src/strategies/goals/kill-frenzy-top.ts +0 -90
- package/src/strategies/goals/kill-lone-top.test.ts +0 -96
- package/src/strategies/goals/kill-lone-top.ts +0 -166
- package/src/strategies/goals/kill-target-goal.ts +0 -69
- package/src/strategies/goals/kill-target-top.test.ts +0 -65
- package/src/strategies/goals/kill-target-top.ts +0 -121
- package/src/strategies/goals/leaf-goal.ts +0 -27
- package/src/strategies/goals/linger-corpse-goal.ts +0 -43
- package/src/strategies/goals/lone-kill-core.ts +0 -86
- package/src/strategies/goals/lone-kill-goal.ts +0 -24
- package/src/strategies/goals/lone-kill-task-top.test.ts +0 -84
- package/src/strategies/goals/lone-kill-task-top.ts +0 -133
- package/src/strategies/goals/move-room-goal.ts +0 -102
- package/src/strategies/goals/normal-shrimp-top.test.ts +0 -112
- package/src/strategies/goals/normal-shrimp-top.ts +0 -248
- package/src/strategies/goals/paradise-fish-top.test.ts +0 -203
- package/src/strategies/goals/paradise-fish-top.ts +0 -234
- package/src/strategies/goals/patrol-top.ts +0 -68
- package/src/strategies/goals/report-patrol-top.ts +0 -97
- package/src/strategies/goals/safe-task-goal.ts +0 -130
- package/src/strategies/goals/social-task-top.ts +0 -192
- package/src/strategies/goals/task-kill-report-top.ts +0 -187
- package/src/strategies/goals/task-only-top.test.ts +0 -88
- package/src/strategies/goals/task-only-top.ts +0 -64
- package/src/strategies/goals/task-or-patrol-goal.ts +0 -59
- package/src/strategies/goals/task-report-top.ts +0 -57
- package/src/strategies/goals/wander-task-goal.ts +0 -33
- package/src/strategies/goals/warrior-shrimp-top.test.ts +0 -138
- package/src/strategies/goals/warrior-shrimp-top.ts +0 -293
- package/src/strategies/greeting.ts +0 -60
- package/src/strategies/hide.ts +0 -51
- package/src/strategies/kill-frenzy.ts +0 -32
- package/src/strategies/kill-lone.ts +0 -52
- package/src/strategies/kill-target.ts +0 -48
- package/src/strategies/lone-kill-task.ts +0 -48
- package/src/strategies/meeting-gate.test.ts +0 -59
- package/src/strategies/meeting-gate.ts +0 -28
- package/src/strategies/move-room.ts +0 -46
- package/src/strategies/official-long-behavior.test.ts +0 -89
- package/src/strategies/paradise-fish.ts +0 -70
- package/src/strategies/pathfind/astar-path.test.ts +0 -198
- package/src/strategies/pathfind/escape-planner.test.ts +0 -223
- package/src/strategies/patrol.ts +0 -32
- package/src/strategies/player-targets.test.ts +0 -10
- package/src/strategies/player-targets.ts +0 -18
- package/src/strategies/report-patrol.ts +0 -32
- package/src/strategies/shrimp-memory.ts +0 -87
- package/src/strategies/social-task.ts +0 -67
- package/src/strategies/speech-module.ts +0 -123
- package/src/strategies/task-kill-report.ts +0 -49
- package/src/strategies/task-only.test.ts +0 -138
- package/src/strategies/task-only.ts +0 -34
- package/src/strategies/task-report.ts +0 -50
- package/src/strategies/types.ts +0 -116
- package/src/strategies/warrior-memory.ts +0 -54
- /package/src/{strategies/pathfind → runtime/clawclaw/navigation}/clawclaw-walkable.bin +0 -0
- /package/src/{strategies/pathfind → runtime/clawclaw/navigation}/distance-field.ts +0 -0
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { CurrentStateStore } from './current-state-store.js';
|
|
3
|
-
import { StateFreshnessEvaluator } from './freshness.js';
|
|
4
|
-
import { StateIngress } from './state-ingress.js';
|
|
5
|
-
import { StateProvider } from './state-provider.js';
|
|
6
|
-
import type { RuntimeScope, StateFrameEnvelope } from './types.js';
|
|
7
|
-
|
|
8
|
-
const scope1: RuntimeScope = {
|
|
9
|
-
adapterId: 'test-game',
|
|
10
|
-
accountId: 'account-1',
|
|
11
|
-
sessionId: 'session-1',
|
|
12
|
-
segmentId: 'segment-1',
|
|
13
|
-
segmentSeq: 1,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const scope2: RuntimeScope = { ...scope1, segmentId: 'segment-2', segmentSeq: 2 };
|
|
17
|
-
const scope3: RuntimeScope = { ...scope1, sessionId: 'session-2', segmentId: 'segment-3', segmentSeq: 1 };
|
|
18
|
-
|
|
19
|
-
function frame(
|
|
20
|
-
payload: Record<string, unknown>,
|
|
21
|
-
overrides: Partial<StateFrameEnvelope<Record<string, unknown>>> = {},
|
|
22
|
-
): StateFrameEnvelope<Record<string, unknown>> {
|
|
23
|
-
return {
|
|
24
|
-
scope: scope1,
|
|
25
|
-
connectionId: 'connection-1',
|
|
26
|
-
sourceKey: 'state:1',
|
|
27
|
-
sourceSeq: 1,
|
|
28
|
-
receivedAtMs: 1_000,
|
|
29
|
-
payload,
|
|
30
|
-
...overrides,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
describe('CurrentStateStore', () => {
|
|
35
|
-
it('replaces the complete backend frame and protects it from caller mutation', () => {
|
|
36
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
37
|
-
store.openScope(scope1);
|
|
38
|
-
store.openConnection('connection-1', 900);
|
|
39
|
-
const first = { phase: 'one', removedLater: true, nested: { value: 1 } };
|
|
40
|
-
expect(store.applyBackendState(frame(first)).accepted).toBe(true);
|
|
41
|
-
first.nested.value = 99;
|
|
42
|
-
|
|
43
|
-
expect(store.snapshot().backendState).toEqual({ phase: 'one', removedLater: true, nested: { value: 1 } });
|
|
44
|
-
expect(store.applyBackendState(frame({ phase: 'two' }, { sourceKey: 'state:2', sourceSeq: 2 })).accepted).toBe(true);
|
|
45
|
-
expect(store.snapshot().backendState).toEqual({ phase: 'two' });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('keeps session facts across a segment and clears facts at their declared lifetime', () => {
|
|
49
|
-
const store = new CurrentStateStore();
|
|
50
|
-
store.openScope(scope1);
|
|
51
|
-
store.applyFacts([
|
|
52
|
-
{ key: 'room', value: 'R1', lifetime: 'session', provenance: { sourceType: 'allocation' } },
|
|
53
|
-
{ key: 'round', value: 1, lifetime: 'segment', provenance: { sourceType: 'allocation' } },
|
|
54
|
-
], 100);
|
|
55
|
-
store.openConnection('connection-1');
|
|
56
|
-
store.applyBackendState(frame({ phase: 'playing' }));
|
|
57
|
-
|
|
58
|
-
const nextSegment = store.openScope(scope2);
|
|
59
|
-
expect(nextSegment.backendState).toBeNull();
|
|
60
|
-
expect(nextSegment.globalFacts).toEqual({ room: 'R1' });
|
|
61
|
-
expect(nextSegment.facts.segment).toEqual({});
|
|
62
|
-
|
|
63
|
-
const nextSession = store.openScope(scope3);
|
|
64
|
-
expect(nextSession.globalFacts).toEqual({});
|
|
65
|
-
expect(nextSession.facts.session).toEqual({});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('rejects stale connections, mismatched scopes, and duplicate source sequences', () => {
|
|
69
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
70
|
-
store.openScope(scope1);
|
|
71
|
-
store.openConnection('connection-1');
|
|
72
|
-
expect(store.applyBackendState(frame({ n: 1 })).accepted).toBe(true);
|
|
73
|
-
store.openConnection('connection-2', 2_000);
|
|
74
|
-
|
|
75
|
-
expect(store.applyBackendState(frame({ n: 2 }, { sourceSeq: 2 })).accepted).toBe(false);
|
|
76
|
-
expect(store.applyBackendState(frame({ n: 2 }, {
|
|
77
|
-
connectionId: 'connection-2',
|
|
78
|
-
scope: scope2,
|
|
79
|
-
sourceSeq: 2,
|
|
80
|
-
}))).toMatchObject({ accepted: false, reason: 'scope_mismatch' });
|
|
81
|
-
expect(store.applyBackendState(frame({ n: 2 }, {
|
|
82
|
-
connectionId: 'connection-2',
|
|
83
|
-
sourceSeq: 2,
|
|
84
|
-
})).accepted).toBe(true);
|
|
85
|
-
expect(store.applyBackendState(frame({ n: 3 }, {
|
|
86
|
-
connectionId: 'connection-2',
|
|
87
|
-
sourceKey: 'state:2-replay',
|
|
88
|
-
sourceSeq: 2,
|
|
89
|
-
}))).toMatchObject({ accepted: false, reason: 'duplicate_or_out_of_order' });
|
|
90
|
-
expect(store.snapshot().backendState).toEqual({ n: 2 });
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('accepts an unchanged newer frame and refreshes receive metadata', () => {
|
|
94
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
95
|
-
store.openScope(scope1);
|
|
96
|
-
store.openConnection('connection-1');
|
|
97
|
-
store.applyBackendState(frame({ same: true }, { receivedAtMs: 1_000, sourceSeq: 'revision-a' }));
|
|
98
|
-
store.applyBackendState(frame({ same: true }, {
|
|
99
|
-
sourceKey: 'state:2',
|
|
100
|
-
receivedAtMs: 2_000,
|
|
101
|
-
sourceSeq: 'revision-b',
|
|
102
|
-
}));
|
|
103
|
-
expect(store.snapshot().meta).toMatchObject({
|
|
104
|
-
receiveSeq: 2,
|
|
105
|
-
lastStateReceivedAtMs: 2_000,
|
|
106
|
-
lastSourceSeq: 'revision-b',
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('State freshness and provider', () => {
|
|
112
|
-
it('becomes stale on disconnect and only freshens after the new connection first frame', () => {
|
|
113
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
114
|
-
const ingress = new StateIngress(store);
|
|
115
|
-
const evaluator = new StateFreshnessEvaluator();
|
|
116
|
-
ingress.openScope(scope1);
|
|
117
|
-
ingress.connectionOpened('connection-1', 900);
|
|
118
|
-
ingress.accept(frame({ phase: 'playing' }));
|
|
119
|
-
expect(evaluator.evaluate(store.snapshot(), scope1, 1_100).status).toBe('fresh');
|
|
120
|
-
|
|
121
|
-
ingress.connectionChanged('connection-1', 'disconnected', 1_200);
|
|
122
|
-
expect(evaluator.evaluate(store.snapshot(), scope1, 1_300)).toMatchObject({ status: 'stale', reason: 'connection_disconnected' });
|
|
123
|
-
ingress.connectionOpened('connection-2', 1_400);
|
|
124
|
-
expect(evaluator.evaluate(store.snapshot(), scope1, 1_500)).toMatchObject({ status: 'stale', reason: 'awaiting_first_frame' });
|
|
125
|
-
|
|
126
|
-
ingress.accept(frame({ phase: 'playing', revision: 2 }, {
|
|
127
|
-
connectionId: 'connection-2',
|
|
128
|
-
sourceKey: 'state:2',
|
|
129
|
-
sourceSeq: 2,
|
|
130
|
-
receivedAtMs: 1_600,
|
|
131
|
-
}));
|
|
132
|
-
expect(evaluator.evaluate(store.snapshot(), scope1, 1_700).status).toBe('fresh');
|
|
133
|
-
ingress.connectionChanged('connection-1', 'disconnected', 1_750);
|
|
134
|
-
expect(evaluator.evaluate(store.snapshot(), scope1, 1_760).status).toBe('fresh');
|
|
135
|
-
|
|
136
|
-
ingress.openScope(scope2, 1_800);
|
|
137
|
-
expect(evaluator.evaluate(store.snapshot(), scope2, 1_900)).toMatchObject({ status: 'unavailable', reason: 'state_not_received' });
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('does not use age alone unless the adapter opts into a silence policy', () => {
|
|
141
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
142
|
-
store.openScope(scope1);
|
|
143
|
-
store.openConnection('connection-1', 900);
|
|
144
|
-
store.applyBackendState(frame({ unchanged: true }));
|
|
145
|
-
|
|
146
|
-
expect(new StateFreshnessEvaluator().evaluate(store.snapshot(), scope1, 100_000).status).toBe('fresh');
|
|
147
|
-
expect(new StateFreshnessEvaluator({ maxStateSilenceMs: 10_000 })
|
|
148
|
-
.evaluate(store.snapshot(), scope1, 100_000)).toMatchObject({ status: 'stale', reason: 'state_silence_timeout' });
|
|
149
|
-
expect(new StateFreshnessEvaluator({ maxLivenessSilenceMs: 10_000 })
|
|
150
|
-
.evaluate(store.snapshot(), scope1, 100_000)).toMatchObject({ status: 'stale', reason: 'liveness_timeout' });
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('returns stale state but never returns state for an unavailable scope', () => {
|
|
154
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
155
|
-
store.openScope(scope1);
|
|
156
|
-
store.openConnection('connection-1');
|
|
157
|
-
store.applyBackendState(frame({ phase: 'playing' }));
|
|
158
|
-
store.markConnectionState('connection-1', 'disconnected', 1_100);
|
|
159
|
-
const provider = new StateProvider(store, new StateFreshnessEvaluator(), (snapshot) => snapshot.backendState);
|
|
160
|
-
|
|
161
|
-
expect(provider.read(scope1, 1_200)).toMatchObject({
|
|
162
|
-
state: { phase: 'playing' },
|
|
163
|
-
stateStatus: { status: 'stale' },
|
|
164
|
-
});
|
|
165
|
-
expect(provider.read(scope2, 1_200)).toMatchObject({
|
|
166
|
-
state: null,
|
|
167
|
-
stateStatus: { status: 'unavailable', reason: 'scope_mismatch' },
|
|
168
|
-
stateError: { code: 'STATE_SCOPE_MISMATCH' },
|
|
169
|
-
});
|
|
170
|
-
expect(new StateProvider(null).read(scope1)).toMatchObject({
|
|
171
|
-
state: null,
|
|
172
|
-
stateStatus: { status: 'unavailable', reason: 'owner_unavailable' },
|
|
173
|
-
stateError: { code: 'OWNER_NOT_READY' },
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
describe('StateIngress observer isolation', () => {
|
|
179
|
-
it('updates the whiteboard even when history observers fail', async () => {
|
|
180
|
-
const observerError = vi.fn();
|
|
181
|
-
const observer = {
|
|
182
|
-
onFrameAccepted: vi.fn(() => { throw new Error('history failed'); }),
|
|
183
|
-
onConnectionChanged: vi.fn(async () => { throw new Error('gap failed'); }),
|
|
184
|
-
onObserverError: observerError,
|
|
185
|
-
};
|
|
186
|
-
const store = new CurrentStateStore<Record<string, unknown>>();
|
|
187
|
-
const ingress = new StateIngress(store, [observer]);
|
|
188
|
-
ingress.openScope(scope1);
|
|
189
|
-
ingress.connectionOpened('connection-1');
|
|
190
|
-
const accepted = ingress.accept(frame({ safe: true }));
|
|
191
|
-
await Promise.resolve();
|
|
192
|
-
|
|
193
|
-
expect(accepted.accepted).toBe(true);
|
|
194
|
-
expect(store.snapshot().backendState).toEqual({ safe: true });
|
|
195
|
-
expect(observerError).toHaveBeenCalledTimes(2);
|
|
196
|
-
});
|
|
197
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
applyGameSyncDelta,
|
|
4
|
-
gameSyncUrl,
|
|
5
|
-
parseGameSyncMessage,
|
|
6
|
-
} from './game-sync-protocol.js';
|
|
7
|
-
|
|
8
|
-
describe('GameSync v2 protocol', () => {
|
|
9
|
-
it('parses snapshot and update envelopes', () => {
|
|
10
|
-
expect(parseGameSyncMessage({
|
|
11
|
-
type: 'game.sync.snapshot',
|
|
12
|
-
data: { seq: 1, game_id: 'g', tick: 10, locale: 'zh-CN', state: { phase: 'wandering' } },
|
|
13
|
-
})).toMatchObject({ type: 'game.sync.snapshot', data: { seq: 1, tick: 10, events: [] } });
|
|
14
|
-
expect(parseGameSyncMessage({
|
|
15
|
-
type: 'game.sync.snapshot',
|
|
16
|
-
data: {
|
|
17
|
-
seq: 2,
|
|
18
|
-
game_id: 'g',
|
|
19
|
-
tick: 11,
|
|
20
|
-
locale: 'zh-CN',
|
|
21
|
-
state: { phase: 'wandering' },
|
|
22
|
-
events: [{ event_id: 'started', tick: 0, type: 'game_started' }],
|
|
23
|
-
},
|
|
24
|
-
})).toMatchObject({
|
|
25
|
-
type: 'game.sync.snapshot',
|
|
26
|
-
data: { seq: 2, events: [{ event_id: 'started', tick: 0, type: 'game_started' }] },
|
|
27
|
-
});
|
|
28
|
-
expect(parseGameSyncMessage({
|
|
29
|
-
type: 'game.sync.update',
|
|
30
|
-
data: {
|
|
31
|
-
seq: 2,
|
|
32
|
-
game_id: 'g',
|
|
33
|
-
tick: 11,
|
|
34
|
-
state_delta: { you: { x: 3 } },
|
|
35
|
-
events: [{ event_id: 'e', tick: 11, type: 'move_end' }],
|
|
36
|
-
},
|
|
37
|
-
})).toMatchObject({ type: 'game.sync.update', data: { seq: 2, events: [{ event_id: 'e' }] } });
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('validates snapshot event history with the same contract as update events', () => {
|
|
41
|
-
expect(() => parseGameSyncMessage({
|
|
42
|
-
type: 'game.sync.snapshot',
|
|
43
|
-
data: {
|
|
44
|
-
seq: 1,
|
|
45
|
-
game_id: 'g',
|
|
46
|
-
tick: 10,
|
|
47
|
-
locale: 'zh-CN',
|
|
48
|
-
state: {},
|
|
49
|
-
events: [{ event_id: '', tick: 0, type: 'game_started' }],
|
|
50
|
-
},
|
|
51
|
-
})).toThrow('game.sync.snapshot.data.events[0].event_id');
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('applies keyed, replace, recursive, and semantic-clear rules', () => {
|
|
55
|
-
const next = applyGameSyncDelta({
|
|
56
|
-
phase: 'wandering',
|
|
57
|
-
players: [
|
|
58
|
-
{ seat: 1, name: 'a', known_status: { is_alive: true } },
|
|
59
|
-
{ seat: 2, name: 'b', known_status: {} },
|
|
60
|
-
],
|
|
61
|
-
you: {
|
|
62
|
-
x: 1,
|
|
63
|
-
activity: { kind: 'moving', target_x: 9, remaining_secs: 2 },
|
|
64
|
-
tasks: [
|
|
65
|
-
{ name: 'old', room: 'deck', x: 1, y: 2, status: 'normal' },
|
|
66
|
-
{ name: 'removed', room: 'engine', x: 3, y: 4, status: 'normal' },
|
|
67
|
-
],
|
|
68
|
-
},
|
|
69
|
-
visible_players: [{ player: { seat: 2, name: 'b' }, x: 2, y: 3, room: 'r' }],
|
|
70
|
-
meeting: { active: true, speeches: { 1: 'old' }, current_speaker: { seat: 1, name: 'a' } },
|
|
71
|
-
emergency: { active: true, task_name: 'x' },
|
|
72
|
-
crab_sabotage: { status: 'cooldown_charged', cooldown_remaining_secs: 4 },
|
|
73
|
-
}, {
|
|
74
|
-
players: [{ seat: 2, known_status: { is_alive: false, death_cause: 'killed' } }],
|
|
75
|
-
you: {
|
|
76
|
-
x: 2,
|
|
77
|
-
activity: { kind: 'idle' },
|
|
78
|
-
tasks: [{ name: 'old', room: 'deck', x: 1, y: 2, status: 'completed' }],
|
|
79
|
-
},
|
|
80
|
-
visible_players: [],
|
|
81
|
-
meeting: { active: true, speeches: {} },
|
|
82
|
-
emergency: { active: false },
|
|
83
|
-
crab_sabotage: { status: 'ready' },
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
expect(next.players).toEqual([
|
|
87
|
-
{ seat: 1, name: 'a', known_status: { is_alive: true } },
|
|
88
|
-
{ seat: 2, name: 'b', known_status: { is_alive: false, death_cause: 'killed' } },
|
|
89
|
-
]);
|
|
90
|
-
expect(next.you).toEqual({
|
|
91
|
-
x: 2,
|
|
92
|
-
activity: { kind: 'idle' },
|
|
93
|
-
tasks: [{ name: 'old', room: 'deck', x: 1, y: 2, status: 'completed' }],
|
|
94
|
-
});
|
|
95
|
-
expect(next.visible_players).toEqual([]);
|
|
96
|
-
expect(next.meeting).toMatchObject({ active: true, speeches: {}, current_speaker: { seat: 1 } });
|
|
97
|
-
expect(next.emergency).toEqual({ active: false });
|
|
98
|
-
expect(next.crab_sabotage).toEqual({ status: 'ready' });
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('builds a protocol URL and adds resume only with a retained baseline', () => {
|
|
102
|
-
expect(gameSyncUrl('wss://host/gs/a/api/v1/game/sync'))
|
|
103
|
-
.toBe('wss://host/gs/a/api/v1/game/sync?protocol_version=1');
|
|
104
|
-
expect(gameSyncUrl('wss://host/gs/a/api/v1/game/sync?protocol_version=1', 42))
|
|
105
|
-
.toBe('wss://host/gs/a/api/v1/game/sync?protocol_version=1&resume_seq=42');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('rejects internally inconsistent resume metadata', () => {
|
|
109
|
-
const resume = (data: Record<string, unknown>) => parseGameSyncMessage({
|
|
110
|
-
type: 'game.sync.resume',
|
|
111
|
-
data: { status: 'complete', tick: 20, ...data },
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
expect(() => resume({ from_seq: null, to_seq: 4, replayed_count: 2 }))
|
|
115
|
-
.toThrow(/from_seq must be present/);
|
|
116
|
-
expect(() => resume({ from_seq: 3, to_seq: 4, replayed_count: 0 }))
|
|
117
|
-
.toThrow(/from_seq must be null/);
|
|
118
|
-
expect(() => resume({ from_seq: 3, to_seq: 5, replayed_count: 2 }))
|
|
119
|
-
.toThrow(/replay range is inconsistent/);
|
|
120
|
-
expect(resume({ from_seq: null, to_seq: 4, replayed_count: 0 }))
|
|
121
|
-
.toMatchObject({ data: { from_seq: null, to_seq: 4, replayed_count: 0 } });
|
|
122
|
-
});
|
|
123
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import {
|
|
3
|
-
HostMonitorWriteGate,
|
|
4
|
-
HOST_MONITOR_BUCKET_CAPACITY,
|
|
5
|
-
HOST_MONITOR_BUCKET_REFILL_MS,
|
|
6
|
-
TokenBucket,
|
|
7
|
-
} from './host-monitor-rate-gate.js';
|
|
8
|
-
|
|
9
|
-
describe('TokenBucket', () => {
|
|
10
|
-
it('starts full and allows capacity burst', () => {
|
|
11
|
-
let now = 0;
|
|
12
|
-
const bucket = new TokenBucket({ now: () => now });
|
|
13
|
-
for (let i = 0; i < HOST_MONITOR_BUCKET_CAPACITY; i += 1) {
|
|
14
|
-
expect(bucket.tryConsume()).toBe(true);
|
|
15
|
-
}
|
|
16
|
-
// Tenth token exhausted the burst; next is denied immediately.
|
|
17
|
-
expect(bucket.tryConsume()).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('refills one token every refill interval', () => {
|
|
21
|
-
let now = 0;
|
|
22
|
-
const bucket = new TokenBucket({ now: () => now });
|
|
23
|
-
for (let i = 0; i < HOST_MONITOR_BUCKET_CAPACITY; i += 1) bucket.tryConsume();
|
|
24
|
-
expect(bucket.tryConsume()).toBe(false);
|
|
25
|
-
|
|
26
|
-
now += HOST_MONITOR_BUCKET_REFILL_MS;
|
|
27
|
-
expect(bucket.tryConsume()).toBe(true);
|
|
28
|
-
// Only one token refilled; second is still denied.
|
|
29
|
-
expect(bucket.tryConsume()).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('refill is capped at capacity', () => {
|
|
33
|
-
let now = 0;
|
|
34
|
-
const bucket = new TokenBucket({ now: () => now });
|
|
35
|
-
for (let i = 0; i < HOST_MONITOR_BUCKET_CAPACITY; i += 1) bucket.tryConsume();
|
|
36
|
-
now += HOST_MONITOR_BUCKET_REFILL_MS * 100;
|
|
37
|
-
expect(bucket.current()).toBe(HOST_MONITOR_BUCKET_CAPACITY);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe('HostMonitorWriteGate', () => {
|
|
42
|
-
it('allows the first write immediately', () => {
|
|
43
|
-
const gate = new HostMonitorWriteGate({ now: () => 0 });
|
|
44
|
-
expect(gate.allow()).toBe(true);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('denies writes closer than the minimum interval', () => {
|
|
48
|
-
let now = 0;
|
|
49
|
-
const gate = new HostMonitorWriteGate({ now: () => now, minIntervalMs: 300 });
|
|
50
|
-
expect(gate.allow()).toBe(true);
|
|
51
|
-
// 100ms later: too soon, denied even though bucket has tokens.
|
|
52
|
-
now += 100;
|
|
53
|
-
expect(gate.allow()).toBe(false);
|
|
54
|
-
// After the interval: allowed.
|
|
55
|
-
now += 200;
|
|
56
|
-
expect(gate.allow()).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('denies once the bucket is exhausted regardless of spacing', () => {
|
|
60
|
-
let now = 0;
|
|
61
|
-
const gate = new HostMonitorWriteGate({
|
|
62
|
-
now: () => now,
|
|
63
|
-
minIntervalMs: 0,
|
|
64
|
-
capacity: 2,
|
|
65
|
-
refillEveryMs: HOST_MONITOR_BUCKET_REFILL_MS,
|
|
66
|
-
});
|
|
67
|
-
expect(gate.allow()).toBe(true);
|
|
68
|
-
expect(gate.allow()).toBe(true);
|
|
69
|
-
// Capacity 2 exhausted; spaced but no token.
|
|
70
|
-
now += 1000;
|
|
71
|
-
expect(gate.allow()).toBe(false);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
-
import { createClawclawEventCatalog } from './clawclaw-event-catalog.js';
|
|
6
|
-
import {
|
|
7
|
-
EventIngress,
|
|
8
|
-
MemoryEventRepository,
|
|
9
|
-
type CanonicalEvent,
|
|
10
|
-
} from './framework/events/index.js';
|
|
11
|
-
import { stableMailboxItemId } from './framework/mailbox/index.js';
|
|
12
|
-
import {
|
|
13
|
-
MailboxRelayBroker,
|
|
14
|
-
runStdoutMailboxRelay,
|
|
15
|
-
} from './mailbox-host-relay.js';
|
|
16
|
-
import { HostMonitorWriteGate } from './host-monitor-rate-gate.js';
|
|
17
|
-
import {
|
|
18
|
-
gameStartRuntimePath,
|
|
19
|
-
sendOwnerControlRequest,
|
|
20
|
-
sendOwnerRelayControlRequest,
|
|
21
|
-
startOwnerControlServer,
|
|
22
|
-
type OwnerControlResponse,
|
|
23
|
-
type OwnerControlType,
|
|
24
|
-
} from './owner-control.js';
|
|
25
|
-
|
|
26
|
-
function fixture() {
|
|
27
|
-
const repository = new MemoryEventRepository();
|
|
28
|
-
repository.saveSession({
|
|
29
|
-
sessionId: 's1',
|
|
30
|
-
adapterId: 'clawclaw',
|
|
31
|
-
accountId: 'a1',
|
|
32
|
-
status: 'open',
|
|
33
|
-
isCurrent: true,
|
|
34
|
-
openedAt: new Date().toISOString(),
|
|
35
|
-
});
|
|
36
|
-
repository.saveSegment({
|
|
37
|
-
segmentId: 'g1',
|
|
38
|
-
sessionId: 's1',
|
|
39
|
-
sequence: 1,
|
|
40
|
-
kind: 'game',
|
|
41
|
-
status: 'open',
|
|
42
|
-
isCurrent: true,
|
|
43
|
-
openedAt: new Date().toISOString(),
|
|
44
|
-
});
|
|
45
|
-
return {
|
|
46
|
-
repository,
|
|
47
|
-
ingress: new EventIngress(repository, createClawclawEventCatalog()),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function enqueue(
|
|
52
|
-
repository: MemoryEventRepository,
|
|
53
|
-
event: CanonicalEvent,
|
|
54
|
-
payload: Record<string, unknown>,
|
|
55
|
-
coalesceKey = 'ready:s1:g1:1',
|
|
56
|
-
): void {
|
|
57
|
-
repository.enqueueMailboxItem({
|
|
58
|
-
itemId: stableMailboxItemId({
|
|
59
|
-
consumerId: 'local-agent',
|
|
60
|
-
sessionId: event.sessionId,
|
|
61
|
-
eventId: event.eventId,
|
|
62
|
-
mode: 'short',
|
|
63
|
-
}),
|
|
64
|
-
formatterVersion: 'test.v1',
|
|
65
|
-
consumerId: 'local-agent',
|
|
66
|
-
sessionId: event.sessionId,
|
|
67
|
-
segmentId: event.segmentId,
|
|
68
|
-
eventId: event.eventId,
|
|
69
|
-
eventCommitSeq: event.commitSeq,
|
|
70
|
-
mode: 'short',
|
|
71
|
-
coalesceKey,
|
|
72
|
-
payload,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
describe('Mailbox Relay broker', () => {
|
|
77
|
-
it('claims and acknowledges one coalesced batch', async () => {
|
|
78
|
-
const { repository, ingress } = fixture();
|
|
79
|
-
for (const [index, type] of ['speech', 'speech_your_turn'].entries()) {
|
|
80
|
-
const committed = await ingress.publish({
|
|
81
|
-
type,
|
|
82
|
-
producer: 'backend_push',
|
|
83
|
-
sessionId: 's1',
|
|
84
|
-
segmentId: 'g1',
|
|
85
|
-
source: { sourceId: 'ws', eventId: `remote-${index}` },
|
|
86
|
-
});
|
|
87
|
-
enqueue(repository, committed.event, { event: type });
|
|
88
|
-
}
|
|
89
|
-
const broker = new MailboxRelayBroker(repository, 'short');
|
|
90
|
-
const claimed = broker.handle({
|
|
91
|
-
type: 'mailbox_claim',
|
|
92
|
-
relay_id: 'relay-1',
|
|
93
|
-
mode: 'short',
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(claimed?.claim?.items).toHaveLength(2);
|
|
97
|
-
expect(claimed?.wait_ms).toBe(220);
|
|
98
|
-
const acked = broker.handle({
|
|
99
|
-
type: 'mailbox_ack',
|
|
100
|
-
relay_id: 'relay-1',
|
|
101
|
-
mode: 'short',
|
|
102
|
-
claim_token: claimed?.claim?.claim_token,
|
|
103
|
-
});
|
|
104
|
-
expect(acked).toMatchObject({ ok: true, result: { status: 'accepted' } });
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('drains pending items from an earlier segment in the current session', async () => {
|
|
108
|
-
const { repository, ingress } = fixture();
|
|
109
|
-
const committed = await ingress.publish({
|
|
110
|
-
type: 'speech',
|
|
111
|
-
producer: 'backend_push',
|
|
112
|
-
sessionId: 's1',
|
|
113
|
-
segmentId: 'g1',
|
|
114
|
-
source: { sourceId: 'ws', eventId: 'old-segment-event' },
|
|
115
|
-
});
|
|
116
|
-
enqueue(repository, committed.event, { event: 'speech' });
|
|
117
|
-
repository.saveSegment({
|
|
118
|
-
segmentId: 'room-2',
|
|
119
|
-
sessionId: 's1',
|
|
120
|
-
sequence: 2,
|
|
121
|
-
kind: 'room',
|
|
122
|
-
status: 'open',
|
|
123
|
-
isCurrent: true,
|
|
124
|
-
openedAt: new Date().toISOString(),
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const broker = new MailboxRelayBroker(repository, 'short');
|
|
128
|
-
expect(broker.hasPendingCurrent()).toBe(true);
|
|
129
|
-
const claimed = broker.handle({
|
|
130
|
-
type: 'mailbox_claim',
|
|
131
|
-
relay_id: 'relay-1',
|
|
132
|
-
mode: 'short',
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
expect(claimed).toMatchObject({
|
|
136
|
-
scope: { session_id: 's1', segment_id: 'g1' },
|
|
137
|
-
claim: { items: [{ event_id: committed.event.eventId }] },
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('stdout Host Relay', () => {
|
|
143
|
-
it('composes one Host wake-up from a multi-item claim', async () => {
|
|
144
|
-
const emitted: Record<string, unknown>[] = [];
|
|
145
|
-
let ownerAlive = true;
|
|
146
|
-
const request = vi.fn(async (type: OwnerControlType): Promise<OwnerControlResponse | null> => {
|
|
147
|
-
if (type === 'mailbox_claim') {
|
|
148
|
-
return {
|
|
149
|
-
ok: true,
|
|
150
|
-
claim: {
|
|
151
|
-
claim_token: 'claim-1',
|
|
152
|
-
items: [
|
|
153
|
-
{ payload: { event: 'speech' } },
|
|
154
|
-
{ payload: { event: 'vote' } },
|
|
155
|
-
],
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
ownerAlive = false;
|
|
160
|
-
return { ok: true, result: { status: 'accepted' } };
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
await runStdoutMailboxRelay({
|
|
164
|
-
request,
|
|
165
|
-
emit: async (payload) => { emitted.push(payload); },
|
|
166
|
-
compose: (payloads) => ({ events: payloads.map((payload) => payload.event) }),
|
|
167
|
-
isOwnerAlive: () => ownerAlive,
|
|
168
|
-
mode: 'short',
|
|
169
|
-
relayId: 'relay-test',
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
expect(emitted).toEqual([{ events: ['speech', 'vote'] }]);
|
|
173
|
-
expect(request.mock.calls.map((call) => call[0])).toEqual(['mailbox_claim', 'mailbox_ack']);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('can disconnect from owner IPC while the control server stays alive', async () => {
|
|
177
|
-
const { repository, ingress } = fixture();
|
|
178
|
-
const committed = await ingress.publish({
|
|
179
|
-
type: 'speech_your_turn',
|
|
180
|
-
producer: 'backend_push',
|
|
181
|
-
sessionId: 's1',
|
|
182
|
-
segmentId: 'g1',
|
|
183
|
-
source: { sourceId: 'ws', eventId: 'turn-1' },
|
|
184
|
-
});
|
|
185
|
-
enqueue(repository, committed.event, { events: ['speech_your_turn'] });
|
|
186
|
-
const broker = new MailboxRelayBroker(repository, 'short');
|
|
187
|
-
const stateDir = mkdtempSync(join(tmpdir(), 'ccl-relay-owner-'));
|
|
188
|
-
const server = await startOwnerControlServer(stateDir, (request, context) => {
|
|
189
|
-
if (context.principal === 'host_relay') {
|
|
190
|
-
return broker.handle(request) ?? { ok: false, error: 'mailbox_unavailable' };
|
|
191
|
-
}
|
|
192
|
-
return request.type === 'snapshot'
|
|
193
|
-
? { ok: true, type: 'snapshot', alive: true }
|
|
194
|
-
: { ok: false, error: 'unsupported' };
|
|
195
|
-
});
|
|
196
|
-
try {
|
|
197
|
-
writeFileSync(gameStartRuntimePath(stateDir), JSON.stringify({
|
|
198
|
-
owner_pid: process.pid,
|
|
199
|
-
control: server.control,
|
|
200
|
-
control_token: server.token,
|
|
201
|
-
relay_token: server.relayToken,
|
|
202
|
-
}));
|
|
203
|
-
const controller = new AbortController();
|
|
204
|
-
const emitted: Record<string, unknown>[] = [];
|
|
205
|
-
await runStdoutMailboxRelay({
|
|
206
|
-
request: (type, payload, timeoutMs) => sendOwnerRelayControlRequest(
|
|
207
|
-
stateDir,
|
|
208
|
-
type,
|
|
209
|
-
payload,
|
|
210
|
-
timeoutMs,
|
|
211
|
-
),
|
|
212
|
-
emit: async (payload) => {
|
|
213
|
-
emitted.push(payload);
|
|
214
|
-
controller.abort();
|
|
215
|
-
},
|
|
216
|
-
isOwnerAlive: () => true,
|
|
217
|
-
mode: 'short',
|
|
218
|
-
signal: controller.signal,
|
|
219
|
-
relayId: 'relay-real-ipc',
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
expect(emitted).toEqual([{ events: ['speech_your_turn'] }]);
|
|
223
|
-
await expect(sendOwnerControlRequest(stateDir, 'snapshot')).resolves.toMatchObject({
|
|
224
|
-
ok: true,
|
|
225
|
-
alive: true,
|
|
226
|
-
});
|
|
227
|
-
} finally {
|
|
228
|
-
await server.close();
|
|
229
|
-
rmSync(stateDir, { recursive: true, force: true });
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it('stops quietly while idle without sending lifecycle commands', async () => {
|
|
234
|
-
const controller = new AbortController();
|
|
235
|
-
const requests: OwnerControlType[] = [];
|
|
236
|
-
await runStdoutMailboxRelay({
|
|
237
|
-
request: async (type) => {
|
|
238
|
-
requests.push(type);
|
|
239
|
-
return { ok: true, claim: null, wait_ms: 1 };
|
|
240
|
-
},
|
|
241
|
-
emit: vi.fn(),
|
|
242
|
-
isOwnerAlive: () => true,
|
|
243
|
-
mode: 'short',
|
|
244
|
-
signal: controller.signal,
|
|
245
|
-
sleep: async () => { controller.abort(); },
|
|
246
|
-
});
|
|
247
|
-
expect(requests).toEqual(['mailbox_claim']);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
it('nacks instead of emitting when the Host rate gate denies the write', async () => {
|
|
251
|
-
let now = 0;
|
|
252
|
-
const gate = new HostMonitorWriteGate({ now: () => now, minIntervalMs: 0 });
|
|
253
|
-
// Exhaust the gate's bucket so the write is denied.
|
|
254
|
-
while (gate.allow()) now += 1;
|
|
255
|
-
const emitted: Record<string, unknown>[] = [];
|
|
256
|
-
const requestTypes: string[] = [];
|
|
257
|
-
let alive = true;
|
|
258
|
-
const request = vi.fn(async (type: OwnerControlType): Promise<OwnerControlResponse | null> => {
|
|
259
|
-
requestTypes.push(type);
|
|
260
|
-
if (type === 'mailbox_claim') {
|
|
261
|
-
return {
|
|
262
|
-
ok: true,
|
|
263
|
-
claim: {
|
|
264
|
-
claim_token: 'claim-1',
|
|
265
|
-
items: [{ payload: { event: 'speech' } }],
|
|
266
|
-
},
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
if (type === 'mailbox_nack') {
|
|
270
|
-
alive = false; // end the loop after the gate path
|
|
271
|
-
return { ok: true, result: { status: 'released' } };
|
|
272
|
-
}
|
|
273
|
-
// Ack would only happen on successful emit.
|
|
274
|
-
return { ok: true, result: { status: 'accepted' } };
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
await runStdoutMailboxRelay({
|
|
278
|
-
request,
|
|
279
|
-
emit: async (payload) => { emitted.push(payload); },
|
|
280
|
-
compose: (payloads) => ({ events: payloads.map((payload) => payload.event) }),
|
|
281
|
-
rateGate: gate,
|
|
282
|
-
isOwnerAlive: () => alive,
|
|
283
|
-
mode: 'short',
|
|
284
|
-
relayId: 'relay-gated',
|
|
285
|
-
sleep: async () => {},
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
// The denied write is nack'ed (released back to pending), NOT emitted,
|
|
289
|
-
// and NOT acked — the event stays durable for a later poll or ccl events.
|
|
290
|
-
expect(emitted).toEqual([]);
|
|
291
|
-
expect(requestTypes).toEqual(['mailbox_claim', 'mailbox_nack']);
|
|
292
|
-
});
|
|
293
|
-
});
|