@myclaw163/clawclaw-cli 0.10.7 → 0.10.9

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 (64) hide show
  1. package/README.md +6 -4
  2. package/dist/strategy-worker/runtime/clawclaw/plugins/behavior/host.js +1 -0
  3. package/dist/strategy-worker/runtime/clawclaw/plugins/behavior/sdk-sdk.js +79 -5
  4. package/docs/configuration.md +4 -7
  5. package/docs/custom-modules/README.md +1 -1
  6. package/docs/custom-modules/behavior.md +2 -2
  7. package/docs/custom-modules/perception.md +1 -1
  8. package/docs/custom-modules/skill.md +6 -5
  9. package/docs/desktop-event-push.md +2 -1
  10. package/docs/runtime/README.md +1 -1
  11. package/docs/runtime/agent-notifications.md +1 -1
  12. package/docs/runtime/official-perception-modules.md +2 -2
  13. package/docs/user-guide/README.md +1 -1
  14. package/docs/user-guide/behavior.md +3 -2
  15. package/docs/user-guide/desktop-auth.md +33 -1
  16. package/docs/user-guide/installation.md +7 -6
  17. package/docs/user-guide/perception.md +1 -1
  18. package/docs/user-guide/skill.md +6 -5
  19. package/package.json +1 -1
  20. package/scripts/check-skill-command-surface.mjs +7 -2
  21. package/scripts/enable_ccl_permissions.cjs +4 -0
  22. package/skills/clawclaw/SKILL.md +24 -188
  23. package/skills/clawclaw/references/BEHAVIORS.md +7 -7
  24. package/skills/clawclaw/references/CHANNELS.md +1 -1
  25. package/skills/clawclaw/references/COMMANDS.md +20 -18
  26. package/skills/clawclaw/references/HUB.md +15 -15
  27. package/skills/clawclaw/references/STREAM.md +1 -1
  28. package/skills/clawclaw-creator/SKILL.md +27 -25
  29. package/skills/clawclaw-creator/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +9 -9
  30. package/skills/clawclaw-creator/references/CUSTOM-MODULE-API.md +5 -5
  31. package/skills/clawclaw-creator/references/CUSTOM-MODULES.md +14 -13
  32. package/skills/clawclaw-creator/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +7 -7
  33. package/skills/clawclaw-creator/references/CUSTOM-PERSONA-DEVELOPMENT.md +6 -6
  34. package/skills/clawclaw-creator/references/CUSTOM-SKILL-DEVELOPMENT.md +11 -10
  35. package/skills/clawclaw-creator/references/SYNC-AND-REPORT.md +21 -19
  36. package/skills/clawclaw-start/SKILL.md +49 -0
  37. package/skills/clawclaw-start/references/STARTUP.md +176 -0
  38. package/src/cli.ts +2 -0
  39. package/src/commands/behavior.ts +1 -1
  40. package/src/commands/communication.ts +1 -1
  41. package/src/commands/hub.ts +2 -2
  42. package/src/commands/load.ts +30 -984
  43. package/src/commands/prepare.ts +1002 -0
  44. package/src/commands/skill.ts +1 -1
  45. package/src/lib/account-guidance.ts +5 -3
  46. package/src/lib/game-client.ts +33 -6
  47. package/src/lib/hub-errors.ts +2 -2
  48. package/src/lib/load-context.ts +11 -7
  49. package/src/lib/load-startup-context.ts +85 -0
  50. package/src/lib/load-startup.ts +47 -0
  51. package/src/lib/lobby-host-context.ts +74 -0
  52. package/src/lib/package-skills.ts +1 -1
  53. package/src/lib/participation-status.ts +5 -0
  54. package/src/lib/skill-name-conflicts.ts +1 -1
  55. package/src/pipeline/event-format.ts +2 -0
  56. package/src/pipeline/event-hints.ts +3 -2
  57. package/src/runtime/auto-upgrade.ts +1 -1
  58. package/src/runtime/clawclaw/notification-policy.ts +1 -0
  59. package/src/runtime/clawclaw/owner.ts +2 -7
  60. package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-perception-report-adapter.ts +3 -3
  61. package/src/runtime/clawclaw/plugins/source/room-transport.ts +46 -9
  62. package/src/runtime/clawclaw/plugins/source/source-plugin.ts +18 -4
  63. package/src/runtime/clawclaw/plugins/source/types.ts +2 -0
  64. package/src/runtime/clawclaw-event-catalog.ts +1 -0
@@ -1,5 +1,6 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import WebSocket from 'ws';
3
+ import { lobbyHostHeaders, type LobbyHostContext } from '../../../../lib/lobby-host-context.js';
3
4
  import {
4
5
  normalizeRoomSnapshot,
5
6
  type RoomSnapshot,
@@ -20,6 +21,7 @@ export type ClawclawRoomTransportStopReason =
20
21
  | 'reconcile'
21
22
  | 'replaced'
22
23
  | 'fatal'
24
+ | 'normal'
23
25
  | 'error'
24
26
  | 'shutdown';
25
27
 
@@ -32,7 +34,7 @@ export interface ClawclawRoomCloseInfo {
32
34
  reason: string;
33
35
  }
34
36
 
35
- export type ClawclawRoomCloseDecision = 'retry' | 'room_destroyed' | 'maintenance' | 'reconcile' | 'replaced' | 'fatal';
37
+ export type ClawclawRoomCloseDecision = 'retry' | 'room_destroyed' | 'maintenance' | 'reconcile' | 'replaced' | 'fatal' | 'normal';
36
38
 
37
39
  export interface ClawclawRoomTransportFrameMeta {
38
40
  connectionId: string;
@@ -45,6 +47,7 @@ export interface ClawclawRoomTransportFrameMeta {
45
47
  export interface ClawclawRoomTransportOptions {
46
48
  url: string;
47
49
  apiKey: string;
50
+ hostContext?: LobbyHostContext;
48
51
  onOpened(connectionId: string, reconnected: boolean): Promise<void> | void;
49
52
  onLiveness(connectionId: string, atMs: number): void;
50
53
  onConnectionChanged(connectionId: string, state: 'reconnecting' | 'disconnected'): Promise<void> | void;
@@ -67,6 +70,7 @@ export interface ClawclawRoomTransportOptions {
67
70
  heartbeatIntervalMs?: number;
68
71
  pongTimeoutMs?: number;
69
72
  reconnectMs?: number;
73
+ maxHandshakeFailures?: number;
70
74
  initialSnapshotTimeoutMs?: number;
71
75
  /** Maximum time to attach or reattach before the aggregate Source reconciles through Lobby HTTP. */
72
76
  reconcileAfterMs?: number;
@@ -132,11 +136,17 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
132
136
  private openedConnections = 0;
133
137
  private physicalFrameSeq = 0;
134
138
  private gameStartedEmitted = false;
139
+ private replacementReceived = false;
140
+ private handshakeFailures = 0;
141
+ private socketOpened = false;
142
+ private reconnectAttempts = 0;
143
+ private readonly hostHeaders: Record<string, string>;
135
144
  private stopped = false;
136
145
  private resolveOutcome!: (outcome: ClawclawRoomTransportOutcome) => void;
137
146
  private readonly outcomePromise: Promise<ClawclawRoomTransportOutcome>;
138
147
 
139
148
  constructor(private readonly options: ClawclawRoomTransportOptions) {
149
+ this.hostHeaders = options.hostContext ? lobbyHostHeaders(options.hostContext) : {};
140
150
  this.webSocketFactory = options.webSocketFactory ?? ((url, init) => new WebSocket(url, init));
141
151
  this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? 15_000;
142
152
  this.pongTimeoutMs = options.pongTimeoutMs ?? 10_000;
@@ -185,22 +195,25 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
185
195
  const connectionId = randomUUID();
186
196
  this.connectionId = connectionId;
187
197
  this.physicalFrameSeq = 0;
198
+ this.socketOpened = false;
188
199
  let ws: WebSocket;
189
200
  try {
190
201
  ws = this.webSocketFactory(this.options.url, {
191
- headers: { Authorization: `Bearer ${this.options.apiKey}` },
202
+ headers: { Authorization: `Bearer ${this.options.apiKey}`, ...this.hostHeaders },
192
203
  });
193
204
  } catch (error) {
194
205
  this.options.onDiagnostic('runtime.source.connect_failed', {
195
206
  transport: 'room_ws',
196
207
  error: error instanceof Error ? error.message : String(error),
197
208
  });
198
- this.scheduleReconnect();
209
+ if (!this.recordHandshakeFailure()) this.scheduleReconnect();
199
210
  return;
200
211
  }
201
212
  this.ws = ws;
202
213
  ws.once('open', () => {
203
214
  if (this.stopped || this.ws !== ws) return;
215
+ this.socketOpened = true;
216
+ this.handshakeFailures = 0;
204
217
  const reconnected = this.openedConnections > 0;
205
218
  this.openedConnections += 1;
206
219
  this.enqueue(() => this.options.onOpened(connectionId, reconnected));
@@ -246,6 +259,13 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
246
259
  return;
247
260
  }
248
261
  const type = typeof message?.type === 'string' ? message.type : '';
262
+ if (type === 'replaced') {
263
+ this.handleClose(connectionId, ws, {
264
+ code: 4002,
265
+ reason: typeof message.message === 'string' ? message.message : 'Lobby connection replaced',
266
+ });
267
+ return;
268
+ }
249
269
  if (!type) {
250
270
  this.options.onDiagnostic('runtime.input.rejected', { reason: 'room_ws_message_without_type' });
251
271
  return;
@@ -264,8 +284,10 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
264
284
  replay: type === 'room.snapshot',
265
285
  };
266
286
  this.enqueue(async () => {
287
+ if (this.replacementReceived) return;
267
288
  let terminalPhase: 'room_left' | undefined;
268
289
  if (type === 'room.snapshot') {
290
+ this.reconnectAttempts = 0;
269
291
  this.clearSnapshotDeadline();
270
292
  this.clearAttachDeadline();
271
293
  this.latestSnapshot = normalizeRoomSnapshot(data);
@@ -283,6 +305,7 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
283
305
  terminalPhase,
284
306
  meta,
285
307
  });
308
+ if (this.replacementReceived) return;
286
309
  const started = gameStartedFromRoomSnapshot(this.latestSnapshot);
287
310
  if (started && !this.gameStartedEmitted) {
288
311
  this.gameStartedEmitted = true;
@@ -386,12 +409,16 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
386
409
  info: ClawclawRoomCloseInfo,
387
410
  ): void {
388
411
  if (this.stopped || this.ws !== ws || this.connectionId !== connectionId) return;
412
+ if (info.code === 4002) this.replacementReceived = true;
413
+ const handshakeFailed = !this.socketOpened;
389
414
  this.clearTimers();
390
415
  this.clearSocket();
391
416
  this.enqueue(async () => {
392
- await this.options.onConnectionChanged(connectionId, 'reconnecting');
393
- const decision = await this.options.onClosed(connectionId, info);
417
+ const classified = await this.options.onClosed(connectionId, info);
418
+ const decision = this.replacementReceived ? 'replaced' : classified;
419
+ await this.options.onConnectionChanged(connectionId, decision === 'retry' ? 'reconnecting' : 'disconnected');
394
420
  if (decision === 'retry') {
421
+ if (handshakeFailed && this.recordHandshakeFailure()) return;
395
422
  this.options.onDiagnostic('runtime.source.input_gap', {
396
423
  reason: 'room_ws_disconnected',
397
424
  connection_id: connectionId,
@@ -402,21 +429,29 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
402
429
  this.scheduleReconnect();
403
430
  return;
404
431
  }
405
- this.finish({ reason: decision });
432
+ this.finish({ reason: decision, ...(decision === 'fatal'
433
+ ? { error: new Error(`Lobby WebSocket closed (${info.code ?? 'unknown'}): ${info.reason}`) } : {}) });
406
434
  });
407
435
  }
408
436
 
409
437
  private scheduleReconnect(): void {
410
- if (this.stopped || this.reconnectTimer) return;
438
+ if (this.stopped || this.replacementReceived || this.reconnectTimer) return;
411
439
  this.startAttachDeadline();
412
440
  this.clearSocket();
441
+ const delay = Math.min(30_000, this.reconnectMs * 2 ** Math.min(this.reconnectAttempts++, 5));
413
442
  this.reconnectTimer = setTimeout(() => {
414
443
  this.reconnectTimer = undefined;
415
444
  this.openSocket();
416
- }, this.reconnectMs);
445
+ }, delay);
417
446
  this.reconnectTimer.unref?.();
418
447
  }
419
448
 
449
+ private recordHandshakeFailure(): boolean {
450
+ if (++this.handshakeFailures < (this.options.maxHandshakeFailures ?? 3)) return false;
451
+ this.finish({ reason: 'fatal', error: new Error('Lobby WebSocket handshake repeatedly failed; check credentials and host context before retrying.') });
452
+ return true;
453
+ }
454
+
420
455
  private send(message: Record<string, unknown>, ws = this.ws): void {
421
456
  if (!ws || ws !== this.ws || ws.readyState !== WebSocket.OPEN) return;
422
457
  ws.send(JSON.stringify(message));
@@ -424,7 +459,9 @@ export class ClawclawRoomTransport implements ClawclawRoomTransportPort {
424
459
 
425
460
  private enqueue(operation: () => void | Promise<void>): void {
426
461
  if (this.stopped) return;
427
- this.operationTail = this.operationTail.then(operation).catch((error) => {
462
+ this.operationTail = this.operationTail.then(() => {
463
+ if (!this.stopped) return operation();
464
+ }).catch((error) => {
428
465
  this.finish({ reason: 'error', error });
429
466
  });
430
467
  }
@@ -1,4 +1,5 @@
1
1
  import { randomUUID } from 'node:crypto';
2
+ import { isInvalidHostContextError } from '../../../../lib/lobby-host-context.js';
2
3
  import type { AuthProfile } from '../../../../lib/auth.js';
3
4
  import { trainingCharacterId } from '../../../../lib/training-role.js';
4
5
  import {
@@ -683,6 +684,8 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
683
684
  lifecycleEvents: (queue: Record<string, any>) => Record<string, unknown>[],
684
685
  ): Promise<Record<string, any>> {
685
686
  const maintenance = serverServiceStatusFromApiError(originalError);
687
+ // Invalid host declarations did not create a participation; do not adopt an unrelated one.
688
+ if (isInvalidHostContextError(originalError)) throw originalError;
686
689
  if (maintenance) {
687
690
  await this.acceptServiceStatus(maintenance);
688
691
  throw originalError;
@@ -1007,6 +1010,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
1007
1010
  const transport = this.createRoomTransport({
1008
1011
  url: this.client.lobbyAgentWsUrl,
1009
1012
  apiKey: this.profile.apiKey,
1013
+ hostContext: this.client.lobbyHostContext,
1010
1014
  webSocketFactory: this.roomWebSocketFactory,
1011
1015
  onOpened: async (connectionId, reconnected) => {
1012
1016
  this.activeRoomConnectionId = connectionId;
@@ -1054,6 +1058,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
1054
1058
  status: 'allocated',
1055
1059
  mode: roomMode,
1056
1060
  room_code: outcome.roomCode ?? roomCode,
1061
+ participation: queue.participation ?? null,
1057
1062
  ...outcome.event,
1058
1063
  },
1059
1064
  };
@@ -1066,7 +1071,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
1066
1071
  return { kind: 'complete', completion: { reason: 'maintenance' } };
1067
1072
  }
1068
1073
  if (outcome.reason === 'replaced') return { kind: 'complete', completion: { reason: 'replaced' } };
1069
- if (outcome.reason === 'shutdown' || outcome.reason === 'manual') {
1074
+ if (outcome.reason === 'shutdown' || outcome.reason === 'manual' || outcome.reason === 'normal') {
1070
1075
  return { kind: 'complete', completion: { reason: 'stopped' } };
1071
1076
  }
1072
1077
  if (outcome.reason === 'fatal' || outcome.reason === 'error') {
@@ -1780,11 +1785,13 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
1780
1785
  }
1781
1786
 
1782
1787
  private classifyRoomClose(code: number | undefined, reason: string): ClawclawRoomCloseDecision {
1788
+ if (code === 4002) return 'replaced';
1789
+ if (code === 1000) return 'normal';
1783
1790
  if (code === 4005) return 'room_destroyed';
1784
1791
  if (code === 4006 || /service_maintenance/i.test(reason)) return 'maintenance';
1785
- if (code === 4002) return 'replaced';
1786
1792
  if (code === 4001) return 'fatal';
1787
- if (code === 4003) return 'reconcile';
1793
+ if (code === 4003) return 'fatal';
1794
+ if (code === 4004) return 'reconcile';
1788
1795
  if (/not in room|already left/i.test(reason)) return 'reconcile';
1789
1796
  return 'retry';
1790
1797
  }
@@ -1794,6 +1801,13 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
1794
1801
  info: ClawclawRoomCloseInfo,
1795
1802
  ): Promise<ClawclawRoomCloseDecision> {
1796
1803
  const decision = this.classifyRoomClose(info.code, info.reason);
1804
+ if (decision === 'replaced') {
1805
+ this.currentConnection = 'disconnected';
1806
+ await this.submitCurrentState('control', CLAWCLAW_ROOM_WS_SOURCE.sourceId, connectionId, [{
1807
+ type: 'lobby_connection_replaced', route: '/agent/ws', close_code: 4002,
1808
+ reason: info.reason, retryable: false,
1809
+ }]);
1810
+ }
1797
1811
  if (decision === 'room_destroyed') {
1798
1812
  this.requireRuntime().connectionChanged(
1799
1813
  CLAWCLAW_ROOM_WS_SOURCE.sourceId,
@@ -2394,7 +2408,7 @@ implements RuntimeSourcePlugin<Record<string, any>, Record<string, unknown>> {
2394
2408
  events: completion.reason === 'error'
2395
2409
  ? this.localEvents([{
2396
2410
  type: 'error',
2397
- code: 'SOURCE_FAILED',
2411
+ code: isInvalidHostContextError(completion.error) ? 'INVALID_HOST_CONTEXT' : 'SOURCE_FAILED',
2398
2412
  phase: 'runtime',
2399
2413
  message: errorMessage,
2400
2414
  ...(this.participationMode ? { game_mode: this.participationMode } : {}),
@@ -1,4 +1,5 @@
1
1
  import type { QueueMode } from '../../../../lib/competition.js';
2
+ import type { LobbyHostContext } from '../../../../lib/lobby-host-context.js';
2
3
  import type { ClawclawRoleId } from '../../../../lib/gameplay-config.js';
3
4
  import type {
4
5
  CreateRoomOptions,
@@ -16,6 +17,7 @@ import type {
16
17
  } from './room-transport.js';
17
18
 
18
19
  export interface ClawclawSourceClient extends ClawclawGameTransportClient {
20
+ readonly lobbyHostContext?: LobbyHostContext;
19
21
  readonly lobbyAgentWsUrl: string;
20
22
  getQueueStatus(gameType?: string): Promise<any>;
21
23
  getSettlement(): Promise<any>;
@@ -173,6 +173,7 @@ export const CLAWCLAW_LOCAL_AGENT_EVENT_TYPES = [
173
173
  'room_ready_to_start',
174
174
  'room_returning_to_lobby',
175
175
  'room_ping_timeout',
176
+ 'lobby_connection_replaced',
176
177
  'room_destroyed',
177
178
  'strategy_alert',
178
179
  'behavior.started',