@myclaw163/clawclaw-cli 0.8.4 → 0.8.5

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.
Files changed (241) hide show
  1. package/README.md +3 -2
  2. package/docs/configuration.md +44 -26
  3. package/docs/custom-modules/README.md +7 -10
  4. package/docs/custom-modules/behavior.md +3 -1
  5. package/docs/custom-modules/perception.md +1 -1
  6. package/docs/custom-modules/trigger.md +1 -1
  7. package/docs/runtime/README.md +4 -4
  8. package/docs/runtime/official-perception-modules.md +2 -2
  9. package/docs/runtime/owner-lifecycle-and-observability.md +1 -1
  10. package/package.json +9 -2
  11. package/scripts/build-official-hub-behaviors.mjs +136 -0
  12. package/scripts/build-official-hub-behaviors.test.mjs +178 -0
  13. package/scripts/enable_ccl_permissions.cjs +4 -2
  14. package/scripts/enable_ccl_permissions.test.mjs +8 -4
  15. package/scripts/postinstall.mjs +1 -4
  16. package/scripts/sync-bundled-skill.mjs +12 -159
  17. package/scripts/sync-bundled-skill.test.mjs +9 -74
  18. package/skills/clawclaw/SKILL.md +14 -9
  19. package/skills/clawclaw/references/BEHAVIORS.md +4 -4
  20. package/skills/clawclaw/references/COMMANDS.md +32 -16
  21. package/skills/clawclaw/references/HUB.md +25 -38
  22. package/src/commands/account.ts +58 -14
  23. package/src/commands/behavior.ts +116 -38
  24. package/src/commands/hub.ts +110 -153
  25. package/src/commands/load.ts +138 -33
  26. package/src/commands/perception.ts +55 -24
  27. package/src/commands/persona.ts +66 -20
  28. package/src/commands/skill.ts +12 -48
  29. package/src/commands/trigger.ts +68 -40
  30. package/src/hub-official-behaviors/kill-lone.ts +138 -0
  31. package/src/hub-official-behaviors/patrol.ts +30 -0
  32. package/src/hub-official-behaviors/shared.ts +125 -0
  33. package/src/hub-official-behaviors/task-only.ts +158 -0
  34. package/src/lib/account-guidance.ts +54 -9
  35. package/src/lib/agent-workspace-client.ts +135 -0
  36. package/src/lib/agent-workspace-runtime.ts +154 -0
  37. package/src/lib/agent-workspace-skills.ts +177 -0
  38. package/src/lib/agent-workspace-sync.ts +852 -0
  39. package/src/lib/gameplay-config.ts +29 -15
  40. package/src/lib/hub-client.ts +252 -34
  41. package/src/lib/hub-reminder.ts +22 -7
  42. package/src/lib/init-command.ts +0 -6
  43. package/src/lib/load-context.ts +12 -6
  44. package/src/lib/persona.ts +141 -69
  45. package/src/lib/server-status.ts +78 -0
  46. package/src/lib/skill-host.ts +160 -0
  47. package/src/lib/user-data.ts +197 -4
  48. package/src/runtime/clawclaw/owner.ts +63 -19
  49. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +8 -2
  50. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +2 -2
  51. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +4 -1
  52. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +11 -5
  53. package/src/runtime/clawclaw/plugins/perception/replay.ts +0 -2
  54. package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +0 -50
  55. package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +0 -51
  56. package/personas//346/227/240/344/272/272/350/256/276.md +0 -1
  57. package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +0 -48
  58. package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +0 -51
  59. package/src/behaviors/official-finite/index.test.ts +0 -249
  60. package/src/channel/claude-channel-server.test.ts +0 -202
  61. package/src/commands/account.test.ts +0 -570
  62. package/src/commands/behavior.test.ts +0 -158
  63. package/src/commands/communication.test.ts +0 -282
  64. package/src/commands/competition.test.ts +0 -116
  65. package/src/commands/data.test.ts +0 -89
  66. package/src/commands/events-canonical-state.test.ts +0 -406
  67. package/src/commands/events.test.ts +0 -58
  68. package/src/commands/game/index.test.ts +0 -1251
  69. package/src/commands/game-map.test.ts +0 -69
  70. package/src/commands/hub.test.ts +0 -124
  71. package/src/commands/load.test.ts +0 -440
  72. package/src/commands/perception.test.ts +0 -75
  73. package/src/commands/setup/hermes.test.ts +0 -96
  74. package/src/commands/setup/openclaw.test.ts +0 -124
  75. package/src/commands/trigger.test.ts +0 -140
  76. package/src/commands/upgrade.test.ts +0 -82
  77. package/src/lib/account-guidance.test.ts +0 -36
  78. package/src/lib/auth.test.ts +0 -151
  79. package/src/lib/behavior-export.test.ts +0 -326
  80. package/src/lib/behavior-export.ts +0 -537
  81. package/src/lib/clawclaw-skill-spectate-guidance.test.ts +0 -21
  82. package/src/lib/custom-tts.test.ts +0 -387
  83. package/src/lib/diagnostics-config.test.ts +0 -59
  84. package/src/lib/game-client-tts.test.ts +0 -151
  85. package/src/lib/game-client.test.ts +0 -250
  86. package/src/lib/gameplay-config.test.ts +0 -301
  87. package/src/lib/host-config-patcher.test.ts +0 -130
  88. package/src/lib/http-transport.test.ts +0 -124
  89. package/src/lib/hub-client.test.ts +0 -57
  90. package/src/lib/hub-install.test.ts +0 -135
  91. package/src/lib/hub-install.ts +0 -185
  92. package/src/lib/hub-unzip.test.ts +0 -69
  93. package/src/lib/init-command.test.ts +0 -103
  94. package/src/lib/load-context.test.ts +0 -62
  95. package/src/lib/match-state.test.ts +0 -134
  96. package/src/lib/official-resource-sync.test.ts +0 -199
  97. package/src/lib/official-resource-sync.ts +0 -221
  98. package/src/lib/perception-config.test.ts +0 -143
  99. package/src/lib/perception-export.test.ts +0 -145
  100. package/src/lib/perception-export.ts +0 -178
  101. package/src/lib/persona.test.ts +0 -84
  102. package/src/lib/room.test.ts +0 -88
  103. package/src/lib/server-registry.test.ts +0 -64
  104. package/src/lib/service-status.test.ts +0 -81
  105. package/src/lib/skill-version.test.ts +0 -48
  106. package/src/lib/trigger-config.test.ts +0 -102
  107. package/src/lib/trigger-export.test.ts +0 -80
  108. package/src/lib/trigger-export.ts +0 -210
  109. package/src/lib/user-data.test.ts +0 -121
  110. package/src/lib/workspace-argv.test.ts +0 -49
  111. package/src/pipeline/canonical-event-presentation.test.ts +0 -257
  112. package/src/pipeline/event-format.test.ts +0 -879
  113. package/src/pipeline/event-hints.test.ts +0 -337
  114. package/src/pipeline/game-sync-event-normalization.test.ts +0 -419
  115. package/src/pipeline/player-projection.test.ts +0 -247
  116. package/src/runtime/auto-upgrade.test.ts +0 -66
  117. package/src/runtime/canonical-event-emitter.test.ts +0 -70
  118. package/src/runtime/canonical-monitor.test.ts +0 -577
  119. package/src/runtime/clawclaw/agent-event-policy.test.ts +0 -60
  120. package/src/runtime/clawclaw/behavior-decision.test.ts +0 -43
  121. package/src/runtime/clawclaw/behavior-input-fields.test.ts +0 -175
  122. package/src/runtime/clawclaw/behavior-input.test.ts +0 -173
  123. package/src/runtime/clawclaw/behavior-observation-memory.test.ts +0 -59
  124. package/src/runtime/clawclaw/behavior-read-api.test.ts +0 -123
  125. package/src/runtime/clawclaw/notifications.test.ts +0 -47
  126. package/src/runtime/clawclaw/owner-lock.test.ts +0 -221
  127. package/src/runtime/clawclaw/owner.test.ts +0 -203
  128. package/src/runtime/clawclaw/plugins/action/action-plugin.test.ts +0 -328
  129. package/src/runtime/clawclaw/plugins/assembly.test.ts +0 -200
  130. package/src/runtime/clawclaw/plugins/behavior/activation-manager.test.ts +0 -180
  131. package/src/runtime/clawclaw/plugins/behavior/behavior-action.test.ts +0 -43
  132. package/src/runtime/clawclaw/plugins/behavior/behavior-catalog.test.ts +0 -156
  133. package/src/runtime/clawclaw/plugins/behavior/behavior-module-loader.test.ts +0 -182
  134. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.test.ts +0 -1094
  135. package/src/runtime/clawclaw/plugins/behavior/behavior-source.test.ts +0 -174
  136. package/src/runtime/clawclaw/plugins/behavior/control-driver.test.ts +0 -195
  137. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.test.ts +0 -283
  138. package/src/runtime/clawclaw/plugins/behavior-input-source.test.ts +0 -80
  139. package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.test.ts +0 -267
  140. package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.test.ts +0 -226
  141. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.test.ts +0 -239
  142. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.test.ts +0 -407
  143. package/src/runtime/clawclaw/plugins/mailbox/desktop-event-push.integration.test.ts +0 -397
  144. package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.test.ts +0 -270
  145. package/src/runtime/clawclaw/plugins/mailbox/mailbox-agent-event-batch.test.ts +0 -166
  146. package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.test.ts +0 -194
  147. package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.test.ts +0 -87
  148. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.test.ts +0 -2012
  149. package/src/runtime/clawclaw/plugins/perception/module-runner.test.ts +0 -214
  150. package/src/runtime/clawclaw/plugins/perception/modules/co-movement-module.ts +0 -347
  151. package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.test.ts +0 -92
  152. package/src/runtime/clawclaw/plugins/perception/modules/corpse-visibility-module.ts +0 -248
  153. package/src/runtime/clawclaw/plugins/perception/modules/follow-module.ts +0 -389
  154. package/src/runtime/clawclaw/plugins/perception/modules/index.ts +0 -8
  155. package/src/runtime/clawclaw/plugins/perception/modules/motion-module.test.ts +0 -342
  156. package/src/runtime/clawclaw/plugins/perception/modules/motion-module.ts +0 -475
  157. package/src/runtime/clawclaw/plugins/perception/modules/parallel-module-test-helpers.ts +0 -30
  158. package/src/runtime/clawclaw/plugins/perception/modules/relation-stability.test.ts +0 -304
  159. package/src/runtime/clawclaw/plugins/perception/modules/route-module.test.ts +0 -179
  160. package/src/runtime/clawclaw/plugins/perception/modules/route-module.ts +0 -364
  161. package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.test.ts +0 -81
  162. package/src/runtime/clawclaw/plugins/perception/modules/spatial-module.ts +0 -200
  163. package/src/runtime/clawclaw/plugins/perception/modules/stop-module.test.ts +0 -216
  164. package/src/runtime/clawclaw/plugins/perception/modules/stop-module.ts +0 -284
  165. package/src/runtime/clawclaw/plugins/perception/modules/temporal-evidence.test.ts +0 -120
  166. package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.test.ts +0 -105
  167. package/src/runtime/clawclaw/plugins/perception/modules/visibility-module.ts +0 -269
  168. package/src/runtime/clawclaw/plugins/perception/perception-analysis-thread-pool.test.ts +0 -865
  169. package/src/runtime/clawclaw/plugins/perception/perception-plugin.test.ts +0 -1878
  170. package/src/runtime/clawclaw/plugins/perception/perception-source.test.ts +0 -278
  171. package/src/runtime/clawclaw/plugins/perception/replay.test.ts +0 -77
  172. package/src/runtime/clawclaw/plugins/runtime-plugins.test.ts +0 -853
  173. package/src/runtime/clawclaw/plugins/source/game-transport.test.ts +0 -287
  174. package/src/runtime/clawclaw/plugins/source/player-change-tracker.test.ts +0 -321
  175. package/src/runtime/clawclaw/plugins/source/room-transport.test.ts +0 -307
  176. package/src/runtime/clawclaw/plugins/source/source-plugin.test.ts +0 -2646
  177. package/src/runtime/clawclaw/plugins/source/task-change-tracker.test.ts +0 -212
  178. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.test.ts +0 -548
  179. package/src/runtime/clawclaw/plugins/trigger/trigger-source.test.ts +0 -169
  180. package/src/runtime/clawclaw/runtime-data-export.test.ts +0 -249
  181. package/src/runtime/clawclaw-event-catalog.test.ts +0 -128
  182. package/src/runtime/framework/action-diagnostics.test.ts +0 -81
  183. package/src/runtime/framework/capability-registry.test.ts +0 -423
  184. package/src/runtime/framework/control-coordinator.test.ts +0 -675
  185. package/src/runtime/framework/events/event-delivery-service.test.ts +0 -84
  186. package/src/runtime/framework/events/event-ingress.test.ts +0 -203
  187. package/src/runtime/framework/events/mailbox-event-repositories.test.ts +0 -710
  188. package/src/runtime/framework/events/sqlite-event-repository.test.ts +0 -802
  189. package/src/runtime/framework/mailbox/coalescer.test.ts +0 -55
  190. package/src/runtime/framework/mailbox/identity.test.ts +0 -38
  191. package/src/runtime/framework/mailbox/mailbox-dispatcher.test.ts +0 -513
  192. package/src/runtime/framework/mailbox/notification-projector.test.ts +0 -77
  193. package/src/runtime/framework/modules/runtime-module-loader.test.ts +0 -435
  194. package/src/runtime/framework/participation-manager.test.ts +0 -108
  195. package/src/runtime/framework/plugins/runtime-input-journal.test.ts +0 -179
  196. package/src/runtime/framework/plugins/runtime-plugin-scheduler.test.ts +0 -2040
  197. package/src/runtime/framework/state/durable-agent-state-snapshot.test.ts +0 -144
  198. package/src/runtime/framework/state/history/sqlite-state-history-repository.test.ts +0 -157
  199. package/src/runtime/framework/state/history/state-history.test.ts +0 -251
  200. package/src/runtime/framework/state/state-core.test.ts +0 -197
  201. package/src/runtime/game-sync-protocol.test.ts +0 -123
  202. package/src/runtime/host-monitor-rate-gate.test.ts +0 -73
  203. package/src/runtime/mailbox-host-relay.test.ts +0 -293
  204. package/src/runtime/observability/observability.test.ts +0 -583
  205. package/src/runtime/owner-control.test.ts +0 -153
  206. package/src/runtime/parent-process-watch.test.ts +0 -27
  207. package/src/runtime/pending-event-handoff.test.ts +0 -29
  208. package/src/runtime/raw-ws-log.test.ts +0 -102
  209. package/src/runtime/runtime-logger.test.ts +0 -58
  210. package/src/runtime/runtime-persistence.test.ts +0 -26
  211. package/src/runtime/runtime.test.ts +0 -1770
  212. package/src/runtime/ws-client.test.ts +0 -562
  213. package/src/sdk/action.test.ts +0 -8
  214. package/src/sdk/behavior-plugin/index.test.ts +0 -74
  215. package/src/sdk/index.test.ts +0 -45
  216. package/src/sdk/module-input/index.test.ts +0 -49
  217. package/src/sdk/perception-plugin/index.test.ts +0 -22
  218. package/src/sdk/resource-metadata.test.ts +0 -40
  219. package/src/sdk/trigger-plugin/index.test.ts +0 -41
  220. package/src/short-command/classifier.test.ts +0 -107
  221. package/src/short-command/presenter.test.ts +0 -119
  222. package/src/short-command/runner.test.ts +0 -78
  223. package/src/short-command/sensitive-data-filter.test.ts +0 -56
  224. package/src/short-command/transport-response.test.ts +0 -39
  225. package/src/strategies/custom-module.test.ts +0 -267
  226. package/src/strategies/game-utils.test.ts +0 -305
  227. package/src/strategies/goals/avoid-players-top.test.ts +0 -95
  228. package/src/strategies/goals/crab-sabotage-top.test.ts +0 -131
  229. package/src/strategies/goals/kill-lone-top.test.ts +0 -96
  230. package/src/strategies/goals/kill-target-top.test.ts +0 -65
  231. package/src/strategies/goals/lone-kill-task-top.test.ts +0 -84
  232. package/src/strategies/goals/normal-shrimp-top.test.ts +0 -112
  233. package/src/strategies/goals/paradise-fish-top.test.ts +0 -203
  234. package/src/strategies/goals/task-only-top.test.ts +0 -88
  235. package/src/strategies/goals/warrior-shrimp-top.test.ts +0 -138
  236. package/src/strategies/meeting-gate.test.ts +0 -59
  237. package/src/strategies/official-long-behavior.test.ts +0 -89
  238. package/src/strategies/pathfind/astar-path.test.ts +0 -198
  239. package/src/strategies/pathfind/escape-planner.test.ts +0 -223
  240. package/src/strategies/player-targets.test.ts +0 -10
  241. package/src/strategies/task-only.test.ts +0 -138
@@ -1,562 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { WsConnectionState } from '../sdk/types.js';
3
- import type { RawWsMessageSink } from './raw-ws-log.js';
4
- import type { GameSyncResumeState, WsInputFrame, WsProtocolErrorInfo } from './ws-client.js';
5
- import { WsClient } from './ws-client.js';
6
-
7
- const wsMock = vi.hoisted(() => ({
8
- constructed: [] as Array<{ url: string; options: any; instance: any }>,
9
- }));
10
-
11
- vi.mock('ws', () => {
12
- class MockWebSocket {
13
- onopen: ((event: any) => void) | null = null;
14
- onerror: ((event: any) => void) | null = null;
15
- onclose: ((event: any) => void) | null = null;
16
- onmessage: ((event: any) => void) | null = null;
17
- send = vi.fn();
18
- close = vi.fn();
19
- private readonly handlers = new Map<string, Set<(...args: any[]) => void>>();
20
-
21
- constructor(url: string, options: any) {
22
- wsMock.constructed.push({ url, options, instance: this });
23
- }
24
-
25
- on(event: string, handler: (...args: any[]) => void): this {
26
- const handlers = this.handlers.get(event) ?? new Set();
27
- handlers.add(handler);
28
- this.handlers.set(event, handlers);
29
- return this;
30
- }
31
-
32
- emit(event: string, ...args: any[]): void {
33
- for (const handler of this.handlers.get(event) ?? []) handler(...args);
34
- }
35
- }
36
-
37
- return { default: MockWebSocket };
38
- });
39
-
40
- const URL = 'wss://host.example/game-server/api/v1/game/sync';
41
-
42
- function socket(index = wsMock.constructed.length - 1): any {
43
- return wsMock.constructed[index].instance;
44
- }
45
-
46
- function receive(target: any, message: unknown): void {
47
- target.onmessage?.({ data: JSON.stringify(message) });
48
- }
49
-
50
- function sentMessages(target: any): any[] {
51
- return target.send.mock.calls.map(([raw]: [string]) => JSON.parse(raw));
52
- }
53
-
54
- function snapshot(
55
- seq: number,
56
- tick: number,
57
- state: Record<string, any> = { phase: 'wandering', you: { x: 1, y: 2 } },
58
- events: Record<string, any>[] = [],
59
- gameId = 'game-1',
60
- ) {
61
- return {
62
- type: 'game.sync.snapshot',
63
- data: { seq, game_id: gameId, tick, locale: 'zh-CN', state, events },
64
- };
65
- }
66
-
67
- function update(
68
- seq: number,
69
- tick: number,
70
- stateDelta: Record<string, any> = {},
71
- events: Record<string, any>[] = [],
72
- gameId = 'game-1',
73
- ) {
74
- return {
75
- type: 'game.sync.update',
76
- data: {
77
- seq,
78
- game_id: gameId,
79
- tick,
80
- state_delta: stateDelta,
81
- events,
82
- },
83
- };
84
- }
85
-
86
- function event(eventId: string, tick: number, type = 'player_moved') {
87
- return { event_id: eventId, tick, type };
88
- }
89
-
90
- async function connectedClient(options: {
91
- ackIntervalMs?: number;
92
- reconnectMaxMs?: number;
93
- rawMessageSink?: RawWsMessageSink;
94
- resumeState?: GameSyncResumeState;
95
- } = {}): Promise<{ client: WsClient; ws: any }> {
96
- const client = new WsClient({
97
- url: URL,
98
- apiKey: 'claw_secret',
99
- ackIntervalMs: 0,
100
- ...options,
101
- });
102
- const connected = client.connect();
103
- const ws = socket();
104
- ws.onopen?.({});
105
- await connected;
106
- return { client, ws };
107
- }
108
-
109
- describe('WsClient GameSync v2', () => {
110
- beforeEach(() => {
111
- wsMock.constructed.length = 0;
112
- vi.clearAllMocks();
113
- });
114
-
115
- afterEach(() => {
116
- vi.useRealTimers();
117
- });
118
-
119
- it('offers exact raw frames to a non-blocking owner sink without owning its lifecycle', async () => {
120
- const sink: RawWsMessageSink = {
121
- enqueue: vi.fn(() => true),
122
- flush: vi.fn(async () => {}),
123
- close: vi.fn(async () => {}),
124
- };
125
- const { client, ws } = await connectedClient({ rawMessageSink: sink });
126
- const message = snapshot(1, 10, { phase: 'wandering', you: { name: '测试' } });
127
-
128
- receive(ws, message);
129
-
130
- expect(sink.enqueue).toHaveBeenCalledWith(JSON.stringify(message));
131
- expect(sink.close).not.toHaveBeenCalled();
132
- client.disconnect();
133
- });
134
-
135
- it('does not let a raw capture failure block GameSync reduction', async () => {
136
- const sink: RawWsMessageSink = {
137
- enqueue: vi.fn(() => { throw new Error('capture unavailable'); }),
138
- flush: vi.fn(async () => { throw new Error('capture unavailable'); }),
139
- close: vi.fn(async () => {}),
140
- };
141
- const { client, ws } = await connectedClient({ rawMessageSink: sink });
142
- const frames: WsInputFrame[] = [];
143
- client.onInputFrame((frame) => frames.push(frame));
144
-
145
- receive(ws, snapshot(1, 10));
146
-
147
- expect(frames).toHaveLength(1);
148
- client.disconnect();
149
- });
150
-
151
- it('connects to the versioned endpoint and reduces snapshot plus ordered deltas', async () => {
152
- const { client, ws } = await connectedClient();
153
- const frames: WsInputFrame[] = [];
154
- client.onInputFrame((frame) => frames.push(frame));
155
-
156
- expect(wsMock.constructed[0]).toMatchObject({
157
- url: `${URL}?protocol_version=1`,
158
- options: {
159
- headers: {
160
- Authorization: 'Bearer claw_secret',
161
- 'Accept-Language': 'zh-CN',
162
- },
163
- },
164
- });
165
-
166
- receive(ws, snapshot(1, 10, {
167
- phase: 'wandering',
168
- you: { x: 1, y: 2, activity: { kind: 'moving', target_x: 9 } },
169
- players: [{ seat: 2, name: 'p2', known_status: { is_alive: true } }],
170
- visible_players: [{ player: { seat: 2, name: 'p2' }, x: 3, y: 4 }],
171
- }));
172
- client.acknowledge(frames[0].meta.ackToken, true);
173
- receive(ws, update(2, 11, {
174
- you: { x: 5, activity: { kind: 'idle' } },
175
- players: [{ seat: 2, known_status: { is_alive: false } }],
176
- visible_players: [],
177
- }, [event('event-1', 11)]));
178
- client.acknowledge(frames[1].meta.ackToken);
179
-
180
- expect(frames).toHaveLength(2);
181
- expect(frames[0]).toMatchObject({
182
- kind: 'state',
183
- state: { phase: 'wandering', tick: 10, you: { x: 1, y: 2 } },
184
- events: [],
185
- meta: {
186
- origin: 'snapshot',
187
- sourceSeq: 1,
188
- gameId: 'game-1',
189
- domainTick: 10,
190
- replayed: false,
191
- },
192
- });
193
- expect(frames[1]).toMatchObject({
194
- state: {
195
- tick: 11,
196
- you: { x: 5, y: 2, activity: { kind: 'idle' } },
197
- players: [{ seat: 2, name: 'p2', known_status: { is_alive: false } }],
198
- visible_players: [],
199
- },
200
- events: [{ event_id: 'event-1', tick: 11, type: 'player_moved' }],
201
- meta: { origin: 'update', sourceSeq: 2, domainTick: 11, replayed: false },
202
- });
203
- expect(client.resumeState).toMatchObject({ gameId: 'game-1', seq: 2, tick: 11 });
204
- client.disconnect();
205
- });
206
-
207
- it('deduplicates Events by stable event_id without dropping their enclosing State', async () => {
208
- const { client, ws } = await connectedClient();
209
- const frames: WsInputFrame[] = [];
210
- client.onInputFrame((frame) => frames.push(frame));
211
- receive(ws, snapshot(1, 1));
212
- client.acknowledge(frames[0].meta.ackToken, true);
213
- receive(ws, update(2, 2, { phase: 'meeting' }, [event('same-event', 2, 'meeting_started')]));
214
- client.acknowledge(frames[1].meta.ackToken);
215
- receive(ws, update(3, 3, { phase: 'wandering' }, [
216
- { ...event('same-event', 2, 'meeting_started'), ignored_revision: true },
217
- event('new-event', 3, 'meeting_ended'),
218
- ]));
219
-
220
- expect(frames.map((frame) => frame.state.tick)).toEqual([1, 2, 3]);
221
- expect(frames.map((frame) => frame.events.map((item) => item.event_id))).toEqual([
222
- [],
223
- ['same-event'],
224
- ['new-event'],
225
- ]);
226
- client.disconnect();
227
- });
228
-
229
- it('publishes initial snapshot event history once and deduplicates later copies', async () => {
230
- const { client, ws } = await connectedClient();
231
- const frames: WsInputFrame[] = [];
232
- client.onInputFrame((frame) => frames.push(frame));
233
-
234
- receive(ws, snapshot(3, 10, { phase: 'preparing', you: { x: 1, y: 2 } }, [
235
- event('game-started', 0, 'game_started'),
236
- event('role-assigned', 0, 'role_assigned'),
237
- ]));
238
- client.acknowledge(frames[0].meta.ackToken, true);
239
- receive(ws, update(4, 11, { phase: 'wandering' }, [
240
- event('role-assigned', 0, 'role_assigned'),
241
- event('move-end', 11, 'move_end'),
242
- ]));
243
-
244
- expect(frames).toHaveLength(2);
245
- expect(frames[0]).toMatchObject({
246
- state: { phase: 'preparing', tick: 10 },
247
- events: [
248
- { event_id: 'game-started', type: 'game_started' },
249
- { event_id: 'role-assigned', type: 'role_assigned' },
250
- ],
251
- meta: { origin: 'snapshot', sourceSeq: 3, replayed: false },
252
- });
253
- expect(frames[1].events).toEqual([event('move-end', 11, 'move_end')]);
254
- client.disconnect();
255
- });
256
-
257
- it('requests a snapshot resync on a sequence gap and does not advance the baseline', async () => {
258
- const { client, ws } = await connectedClient();
259
- const frames: WsInputFrame[] = [];
260
- const errors: WsProtocolErrorInfo[] = [];
261
- client.onInputFrame((frame) => frames.push(frame));
262
- client.onProtocolError((error) => errors.push(error));
263
- receive(ws, snapshot(5, 50));
264
- client.acknowledge(frames[0].meta.ackToken, true);
265
- ws.send.mockClear();
266
-
267
- receive(ws, update(7, 52, { you: { x: 7 } }));
268
- receive(ws, update(6, 51, { you: { x: 6 } }));
269
-
270
- expect(frames).toHaveLength(1);
271
- expect(client.resumeState).toMatchObject({ seq: 5, tick: 50, state: { you: { x: 1, y: 2 } } });
272
- expect(errors.map((error) => error.reason)).toEqual(['seq_gap', 'update_without_snapshot']);
273
- expect(sentMessages(ws)).toEqual([{
274
- type: 'game.sync.resync',
275
- data: { reason: 'seq_gap', expected_seq: 6, received_seq: 7 },
276
- }]);
277
- client.disconnect();
278
- });
279
-
280
- it('publishes recovery-start before an in-place snapshot fallback', async () => {
281
- const { client, ws } = await connectedClient();
282
- const order: string[] = [];
283
- const frames: WsInputFrame[] = [];
284
- client.onRecoveryStarted((info) => order.push(`recovery:${info.reason}`));
285
- client.onInputFrame((frame) => {
286
- frames.push(frame);
287
- order.push(`frame:${frame.meta.origin}`);
288
- });
289
- receive(ws, snapshot(1, 10));
290
- client.acknowledge(frames[0].meta.ackToken, true);
291
- order.length = 0;
292
-
293
- receive(ws, update(3, 12, { you: { x: 3 } }));
294
- receive(ws, snapshot(4, 13, { phase: 'wandering', you: { x: 4, y: 2 } }));
295
-
296
- expect(order).toEqual(['recovery:seq_gap', 'frame:snapshot']);
297
- expect(frames.at(-1)?.meta).toMatchObject({ snapshotFallback: true });
298
- client.disconnect();
299
- });
300
-
301
- it('aggregates replayed updates and publishes one atomic resume frame', async () => {
302
- vi.useFakeTimers();
303
- const { client, ws: firstSocket } = await connectedClient({ reconnectMaxMs: 1_000 });
304
- const frames: WsInputFrame[] = [];
305
- client.onInputFrame((frame) => frames.push(frame));
306
- receive(firstSocket, snapshot(1, 10));
307
- client.acknowledge(frames[0].meta.ackToken, true);
308
- receive(firstSocket, update(2, 11, { you: { x: 2 } }, [event('seen-before', 11)]));
309
- client.acknowledge(frames[1].meta.ackToken);
310
- frames.length = 0;
311
-
312
- firstSocket.onclose?.({ code: 1006, reason: 'network', wasClean: false });
313
- expect(client.state).toBe(WsConnectionState.Reconnecting);
314
- await vi.advanceTimersByTimeAsync(1_000);
315
- expect(wsMock.constructed).toHaveLength(2);
316
- expect(wsMock.constructed[1].url).toBe(`${URL}?protocol_version=1&resume_seq=2`);
317
- const resumedSocket = socket(1);
318
- resumedSocket.onopen?.({});
319
- await Promise.resolve();
320
-
321
- receive(resumedSocket, update(3, 12, { you: { x: 3 } }, [
322
- event('seen-before', 11),
323
- event('replayed-1', 12),
324
- ]));
325
- receive(resumedSocket, update(4, 13, { you: { x: 4 } }, [event('replayed-2', 13)]));
326
- expect(frames).toEqual([]);
327
- receive(resumedSocket, {
328
- type: 'game.sync.resume',
329
- data: {
330
- status: 'complete',
331
- from_seq: 3,
332
- to_seq: 4,
333
- replayed_count: 2,
334
- tick: 20,
335
- },
336
- });
337
-
338
- expect(frames).toHaveLength(1);
339
- expect(frames[0]).toMatchObject({
340
- state: { tick: 20, you: { x: 4, y: 2 } },
341
- events: [
342
- { event_id: 'replayed-1', tick: 12 },
343
- { event_id: 'replayed-2', tick: 13 },
344
- ],
345
- meta: {
346
- origin: 'resume',
347
- sourceSeq: 4,
348
- domainTick: 20,
349
- replayed: true,
350
- replayedEventCount: 2,
351
- },
352
- });
353
- expect(sentMessages(resumedSocket)).not.toContainEqual({
354
- type: 'game.sync.ack', data: { seq: 4 },
355
- });
356
- client.acknowledge(frames[0].meta.ackToken, true);
357
- expect(sentMessages(resumedSocket)).toContainEqual({ type: 'game.sync.ack', data: { seq: 4 } });
358
- client.disconnect();
359
- });
360
-
361
- it('ignores a delayed ACK token from the previous physical connection', async () => {
362
- vi.useFakeTimers();
363
- const { client, ws: firstSocket } = await connectedClient({ reconnectMaxMs: 1_000 });
364
- const frames: WsInputFrame[] = [];
365
- client.onInputFrame((frame) => frames.push(frame));
366
- receive(firstSocket, snapshot(1, 10));
367
- client.acknowledge(frames[0].meta.ackToken, true);
368
- receive(firstSocket, update(2, 11, { you: { x: 2 } }, [event('late-event', 11)]));
369
- const staleToken = frames[1].meta.ackToken;
370
-
371
- firstSocket.onclose?.({ code: 1006, reason: 'network', wasClean: false });
372
- await vi.advanceTimersByTimeAsync(1_000);
373
- const resumedSocket = socket(1);
374
- resumedSocket.onopen?.({});
375
- await Promise.resolve();
376
- frames.length = 0;
377
- receive(resumedSocket, update(2, 11, { you: { x: 2 } }, [event('late-event', 11)]));
378
- receive(resumedSocket, {
379
- type: 'game.sync.resume',
380
- data: {
381
- status: 'complete',
382
- from_seq: 2,
383
- to_seq: 2,
384
- replayed_count: 1,
385
- tick: 11,
386
- },
387
- });
388
- const currentToken = frames[0].meta.ackToken;
389
- expect(currentToken).not.toBe(staleToken);
390
-
391
- client.acknowledge(staleToken, true);
392
- expect(client.resumeState).toMatchObject({ seq: 1, tick: 10 });
393
- expect(sentMessages(resumedSocket)).not.toContainEqual({
394
- type: 'game.sync.ack', data: { seq: 2 },
395
- });
396
-
397
- client.acknowledge(currentToken, true);
398
- expect(client.resumeState).toMatchObject({ seq: 2, tick: 11 });
399
- expect(sentMessages(resumedSocket)).toContainEqual({
400
- type: 'game.sync.ack', data: { seq: 2 },
401
- });
402
- client.disconnect();
403
- });
404
-
405
- it('accepts a full snapshot fallback when resume history is unavailable', async () => {
406
- const resumeState: GameSyncResumeState = {
407
- gameId: 'game-1',
408
- seq: 8,
409
- tick: 80,
410
- locale: 'en-US',
411
- state: { phase: 'wandering', you: { x: 8 } },
412
- };
413
- const { client, ws } = await connectedClient({ resumeState });
414
- const frames: WsInputFrame[] = [];
415
- client.onInputFrame((frame) => frames.push(frame));
416
-
417
- expect(wsMock.constructed[0]).toMatchObject({
418
- url: `${URL}?protocol_version=1&resume_seq=8`,
419
- options: { headers: { 'Accept-Language': 'en-US' } },
420
- });
421
- receive(ws, snapshot(20, 100, { phase: 'meeting', you: { x: 100 } }));
422
- client.acknowledge(frames[0].meta.ackToken, true);
423
-
424
- expect(frames).toMatchObject([{
425
- state: { phase: 'meeting', tick: 100, you: { x: 100 } },
426
- events: [],
427
- meta: {
428
- origin: 'snapshot',
429
- sourceSeq: 20,
430
- replayed: false,
431
- snapshotFallback: true,
432
- },
433
- }]);
434
- expect(client.resumeState).toMatchObject({ seq: 20, tick: 100 });
435
- client.disconnect();
436
- });
437
-
438
- it('rejects a resume barrier that does not match the frames actually replayed', async () => {
439
- const resumeState: GameSyncResumeState = {
440
- gameId: 'game-1',
441
- seq: 2,
442
- tick: 11,
443
- locale: 'zh-CN',
444
- state: { phase: 'wandering', you: { x: 2 } },
445
- };
446
- const { client, ws } = await connectedClient({ resumeState });
447
- const frames: WsInputFrame[] = [];
448
- const errors: WsProtocolErrorInfo[] = [];
449
- client.onInputFrame((frame) => frames.push(frame));
450
- client.onProtocolError((error) => errors.push(error));
451
-
452
- receive(ws, update(3, 12, { you: { x: 3 } }));
453
- receive(ws, {
454
- type: 'game.sync.resume',
455
- data: {
456
- status: 'complete',
457
- from_seq: 3,
458
- to_seq: 4,
459
- replayed_count: 2,
460
- tick: 12,
461
- },
462
- });
463
-
464
- expect(frames).toEqual([]);
465
- expect(errors.map((error) => error.reason)).toEqual(['invalid_resume']);
466
- expect(sentMessages(ws)).toContainEqual({
467
- type: 'game.sync.resync',
468
- data: { reason: 'invalid_resume', expected_seq: 4, received_seq: 4 },
469
- });
470
- client.disconnect();
471
- });
472
-
473
- it('rejects a resume barrier whose tick moves backwards', async () => {
474
- const resumeState: GameSyncResumeState = {
475
- gameId: 'game-1',
476
- seq: 2,
477
- tick: 11,
478
- locale: 'zh-CN',
479
- state: { phase: 'wandering', you: { x: 2 } },
480
- };
481
- const { client, ws } = await connectedClient({ resumeState });
482
- const errors: WsProtocolErrorInfo[] = [];
483
- client.onProtocolError((error) => errors.push(error));
484
- receive(ws, update(3, 12));
485
- receive(ws, {
486
- type: 'game.sync.resume',
487
- data: {
488
- status: 'complete',
489
- from_seq: 3,
490
- to_seq: 3,
491
- replayed_count: 1,
492
- tick: 10,
493
- },
494
- });
495
-
496
- expect(errors.map((error) => error.reason)).toEqual(['invalid_resume']);
497
- client.disconnect();
498
- });
499
-
500
- it('ACKs snapshots immediately and coalesces later update ACKs on the timer', async () => {
501
- vi.useFakeTimers();
502
- const { client, ws } = await connectedClient({ ackIntervalMs: 50 });
503
- const frames: WsInputFrame[] = [];
504
- client.onInputFrame((frame) => frames.push(frame));
505
- receive(ws, snapshot(1, 1));
506
- expect(sentMessages(ws)).toEqual([]);
507
- client.acknowledge(frames[0].meta.ackToken, true);
508
- receive(ws, update(2, 2, { you: { x: 2 } }));
509
- client.acknowledge(frames[1].meta.ackToken);
510
- receive(ws, update(3, 3, { you: { x: 3 } }));
511
- client.acknowledge(frames[2].meta.ackToken);
512
-
513
- expect(sentMessages(ws)).toEqual([{
514
- type: 'game.sync.ack',
515
- data: { seq: 1 },
516
- }]);
517
- await vi.advanceTimersByTimeAsync(50);
518
- expect(sentMessages(ws)).toEqual([
519
- { type: 'game.sync.ack', data: { seq: 1 } },
520
- { type: 'game.sync.ack', data: { seq: 3 } },
521
- ]);
522
- await vi.advanceTimersByTimeAsync(50);
523
- expect(sentMessages(ws)).toHaveLength(2);
524
- client.disconnect();
525
- });
526
-
527
- it('treats close code 4002 as terminal and never schedules a reconnect', async () => {
528
- vi.useFakeTimers();
529
- const { client, ws } = await connectedClient();
530
- const closes: Record<string, any>[] = [];
531
- client.onDisconnect((info) => closes.push(info));
532
-
533
- ws.onclose?.({ code: 4002, reason: 'game_not_found', wasClean: true });
534
- expect(client.state).toBe(WsConnectionState.Disconnected);
535
- expect(closes).toEqual([{ code: 4002, reason: 'game_not_found', wasClean: true }]);
536
- await vi.advanceTimersByTimeAsync(60_000);
537
- expect(wsMock.constructed).toHaveLength(1);
538
- client.disconnect();
539
- });
540
-
541
- it('treats protocol close code 1002 as terminal and never schedules a reconnect', async () => {
542
- vi.useFakeTimers();
543
- const { client, ws } = await connectedClient();
544
-
545
- ws.onclose?.({ code: 1002, reason: 'INVALID_GAME_SYNC_MESSAGE', wasClean: true });
546
- expect(client.state).toBe(WsConnectionState.Disconnected);
547
- await vi.advanceTimersByTimeAsync(60_000);
548
- expect(wsMock.constructed).toHaveLength(1);
549
- client.disconnect();
550
- });
551
-
552
- it('treats origin/dev NO_ACTIVE_GAME close code 4000 as terminal', async () => {
553
- vi.useFakeTimers();
554
- const { client, ws } = await connectedClient();
555
-
556
- ws.onclose?.({ code: 4000, reason: 'NO_ACTIVE_GAME', wasClean: true });
557
- expect(client.state).toBe(WsConnectionState.Disconnected);
558
- await vi.advanceTimersByTimeAsync(60_000);
559
- expect(wsMock.constructed).toHaveLength(1);
560
- client.disconnect();
561
- });
562
- });
@@ -1,8 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { Action } from './action.js';
3
-
4
- describe('Action compatibility shims', () => {
5
- it('rejects the removed triggerAlarm helper locally with migration guidance', () => {
6
- expect(() => Action.triggerAlarm()).toThrow('trigger_alarm_removed_use_task');
7
- });
8
- });
@@ -1,74 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { BEHAVIOR_PLUGIN_API_VERSION, ClawclawBehavior } from './index.js';
3
- import type {
4
- BehaviorInput,
5
- BehaviorDecision,
6
- BehaviorReadApi,
7
- ClawclawBehaviorManifest,
8
- GameAction,
9
- } from './index.js';
10
-
11
- describe('Behavior authoring contract', () => {
12
- it('uses one typed effect-only Decision for continuous and finite Behavior', () => {
13
- const action = { action: 'kill', target_seat: 3 } satisfies GameAction;
14
- const continuous = {
15
- actions: [action],
16
- status: { activity: 'hunting', summary: '追踪 3 号玩家' },
17
- events: [{ type: 'target_selected', payload: { seat: 3 } }],
18
- } satisfies BehaviorDecision;
19
- const finite = {
20
- ...continuous,
21
- finish: { outcome: 'completed', reason: 'target_reached' },
22
- } satisfies BehaviorDecision;
23
-
24
- expect(finite.actions).toEqual([action]);
25
- expect(finite.finish.outcome).toBe('completed');
26
- });
27
-
28
- it('keeps every callback capability read-only by construction', () => {
29
- const keys: readonly (keyof BehaviorReadApi)[] = ['spatial', 'navigation', 'observation'];
30
- expect(keys).toEqual(['spatial', 'navigation', 'observation']);
31
- });
32
-
33
- it('keeps meeting votes outside the Behavior Action contract', () => {
34
- // @ts-expect-error vote remains a direct Agent/User command handled by ActionPlugin.
35
- const vote = { action: 'vote', target_seat: 3 } satisfies GameAction;
36
- expect(vote.action).toBe('vote');
37
- });
38
-
39
- it('uses one class and manifest for long, short, and reflex Activations', async () => {
40
- class ExampleBehavior extends ClawclawBehavior<{ readonly target: string }> {
41
- static override readonly manifest = {
42
- apiVersion: BEHAVIOR_PLUGIN_API_VERSION,
43
- id: 'example',
44
- name: 'Example',
45
- description: 'One author surface for every activation layer.',
46
- inputs: [{
47
- id: 'target',
48
- type: 'string',
49
- description: 'Semantic target.',
50
- required: true,
51
- }],
52
- runtime: { defaultMs: 5_000, deadlineOutcome: 'failed' },
53
- } as const satisfies ClawclawBehaviorManifest;
54
-
55
- override decide(input: BehaviorInput<{ readonly target: string }>) {
56
- return {
57
- status: { summary: `going to ${input.input.target}` },
58
- finish: { outcome: 'completed' as const, reason: 'done' },
59
- };
60
- }
61
- }
62
-
63
- expect(ExampleBehavior.manifest.apiVersion).toBe(3);
64
- const decision = await new ExampleBehavior().decide({
65
- epochId: 1,
66
- scope: { sessionId: 's', segmentId: 'g' },
67
- nowMs: 100,
68
- gameState: { game_id: 'game', tick: 1 },
69
- events: [],
70
- input: { target: 'engine' },
71
- });
72
- expect(decision.finish?.reason).toBe('done');
73
- });
74
- });
@@ -1,45 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import * as sdk from './index.js';
3
-
4
- function internalOfficialAlgorithmTypesStayPrivate(
5
- // @ts-expect-error Package-internal official algorithms are not part of the root SDK.
6
- _strategy?: import('./index.js').Strategy,
7
- // @ts-expect-error Package-internal official algorithm context is not public.
8
- _context?: import('./index.js').StrategyContext,
9
- // @ts-expect-error Package-internal official algorithm modules are not public.
10
- _module?: import('./index.js').CustomModule,
11
- ): void {}
12
- void internalOfficialAlgorithmTypesStayPrivate;
13
-
14
- function removedStrategyAndIntentSurfacesStayPrivate(
15
- // @ts-expect-error Strategy authoring was replaced by ClawclawBehavior.
16
- _strategy?: import('./index.js').ClawclawStrategy,
17
- // @ts-expect-error Intent authoring was replaced by ClawclawBehavior.
18
- _intent?: import('./index.js').ClawclawIntent,
19
- ): void {}
20
- void removedStrategyAndIntentSurfacesStayPrivate;
21
-
22
- describe('root SDK surface', () => {
23
- it('publishes the public Behavior and Trigger authoring classes', () => {
24
- expect(sdk).toHaveProperty('Action');
25
- expect(sdk).toHaveProperty('GameClient');
26
- expect(sdk).toHaveProperty('ClawclawBehavior');
27
- expect(sdk).toHaveProperty('BEHAVIOR_PLUGIN_API_VERSION', 3);
28
- expect(sdk).toHaveProperty('ClawclawTrigger');
29
- expect(sdk).toHaveProperty('TRIGGER_PLUGIN_API_VERSION', 4);
30
- expect(sdk).toHaveProperty('ClawclawPerception');
31
- expect(sdk).toHaveProperty('ClawclawParallelPerception');
32
- expect(sdk).toHaveProperty('PERCEPTION_PLUGIN_API_VERSION', 2);
33
- expect(sdk).not.toHaveProperty('ClawclawStrategy');
34
- expect(sdk).not.toHaveProperty('ClawclawIntent');
35
- });
36
-
37
- it('does not publish the package-internal official algorithm framework', () => {
38
- expect(sdk).not.toHaveProperty('ClawclawGoalStrategy');
39
- expect(sdk).not.toHaveProperty('OfficialGoalBehavior');
40
- expect(sdk).not.toHaveProperty('GoalRootStrategy');
41
- expect(sdk).not.toHaveProperty('PatrolState');
42
- expect(sdk).not.toHaveProperty('SpeechModule');
43
- expect(sdk).not.toHaveProperty('GreetingTracker');
44
- });
45
- });