@myclaw163/clawclaw-cli 0.8.21 → 0.8.23

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 (31) hide show
  1. package/docs/custom-modules/api-reference.md +2 -0
  2. package/docs/custom-modules/perception.md +25 -0
  3. package/docs/runtime/official-perception-modules.md +23 -0
  4. package/docs/runtime/owner-lifecycle-and-observability.md +6 -2
  5. package/package.json +1 -1
  6. package/skills/clawclaw/references/CUSTOM-MODULE-API.md +2 -0
  7. package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +25 -0
  8. package/src/runtime/clawclaw/agent-event-policy.ts +7 -0
  9. package/src/runtime/clawclaw/owner.ts +16 -10
  10. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +18 -11
  11. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +2 -0
  12. package/src/runtime/clawclaw/plugins/mailbox/adapters/game-sync-perception-report-adapter.ts +10 -1
  13. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +17 -1
  14. package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +5 -2
  15. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +46 -15
  16. package/src/runtime/clawclaw/plugins/perception/perception-module-output.ts +2 -1
  17. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +1 -0
  18. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +4 -0
  19. package/src/runtime/framework/events/event-ingress.ts +1 -0
  20. package/src/runtime/framework/events/memory-event-repository.ts +5 -4
  21. package/src/runtime/framework/events/sqlite-event-repository.ts +36 -9
  22. package/src/runtime/framework/events/types.ts +4 -0
  23. package/src/runtime/framework/mailbox/types.ts +4 -0
  24. package/src/runtime/framework/plugins/contracts.ts +2 -0
  25. package/src/runtime/framework/runtime.ts +1 -0
  26. package/src/runtime/game-sync-ingress-protocol.ts +3 -2
  27. package/src/runtime/game-sync-ingress-worker.ts +153 -38
  28. package/src/runtime/mailbox-host-relay.ts +13 -12
  29. package/src/runtime/threaded-ws-client.ts +17 -9
  30. package/src/runtime/ws-client.ts +18 -8
  31. package/src/sdk/perception-plugin/index.ts +4 -0
@@ -34,8 +34,12 @@ export interface WsClientConfig {
34
34
  resumeState?: GameSyncResumeState;
35
35
  maxPayloadBytes?: number;
36
36
  handshakeTimeoutMs?: number;
37
- /** The isolated ingress may wait for already-delivered Source applications before reconnecting. */
37
+ /** Optional reconnect gate for consumers that cannot retain input independently. */
38
38
  reconnectReady?: () => boolean;
39
+ /** Optional ingress-owned raw cache. Reserve before applying/ACKing a valid message. */
40
+ retainInputMessage?: (raw: string) => boolean;
41
+ /** Release only an incomplete replay; already dispatched frames remain owned by the cache. */
42
+ discardUnappliedMessages?: () => void;
39
43
  diagnostic?: (name: string, attributes: Record<string, unknown>, severity?: 'debug' | 'info' | 'warn') => void;
40
44
  }
41
45
 
@@ -133,7 +137,7 @@ export class WsClient {
133
137
  private connectionIdValue = '';
134
138
  private lastPongAtValue: string | null = null;
135
139
  private baseline: GameSyncResumeState | null;
136
- /** Last baseline durably accepted by Source, and therefore safe to resume from. */
140
+ /** Last baseline accepted by the consumer (Source or the ingress memory cache). */
137
141
  private committedBaseline: GameSyncResumeState | null;
138
142
  private recovering = false;
139
143
  private awaitingSnapshot = false;
@@ -191,8 +195,8 @@ export class WsClient {
191
195
  }
192
196
 
193
197
  /**
194
- * Advance the resume/ACK watermark only after Source has durably accepted the
195
- * matching frame. Reading a socket frame is not sufficient authority.
198
+ * Advance the resume/ACK watermark after the consumer has accepted the frame.
199
+ * The threaded consumer retains it in memory before ACK; this is not a disk flush.
196
200
  */
197
201
  acknowledge(ackToken: string, immediate = false): void {
198
202
  if (!ackToken) return;
@@ -236,12 +240,13 @@ export class WsClient {
236
240
 
237
241
  private openSocket(): Promise<void> {
238
242
  return new Promise((resolve, reject) => {
239
- // Discard frames that were read but never committed by Source. The
240
- // server must replay them from the durable watermark.
243
+ // Discard input not accepted by the consumer. The threaded consumer
244
+ // keeps ACKed raw input across socket reconnects in its own memory cache.
241
245
  this.baseline = this.committedBaseline
242
246
  ? structuredClone(this.committedBaseline)
243
247
  : null;
244
248
  this.clearPendingApplication();
249
+ this.config.discardUnappliedMessages?.();
245
250
  this.connectionIdValue = randomUUID();
246
251
  this.recovering = this.baseline !== null;
247
252
  this.awaitingSnapshot = false;
@@ -301,7 +306,7 @@ export class WsClient {
301
306
  return;
302
307
  }
303
308
  try {
304
- this.handleMessage(parsed);
309
+ this.handleMessage(parsed, raw);
305
310
  } catch (error) {
306
311
  this.reportProtocolError('invalid_game_sync_message', {
307
312
  error: error instanceof Error ? error.message : String(error),
@@ -329,9 +334,11 @@ export class WsClient {
329
334
  });
330
335
  }
331
336
 
332
- private handleMessage(raw: unknown): void {
337
+ private handleMessage(raw: unknown, wire = JSON.stringify(raw)): void {
333
338
  const message = parseGameSyncMessage(raw);
334
339
  if (message.type === 'game.sync.snapshot') {
340
+ this.config.discardUnappliedMessages?.();
341
+ if (this.config.retainInputMessage?.(wire) === false) return;
335
342
  const wasRecovering = this.recovering;
336
343
  const previousGameId = this.baseline?.gameId;
337
344
  if (previousGameId !== undefined && previousGameId !== message.data.game_id) {
@@ -387,6 +394,7 @@ export class WsClient {
387
394
  this.requestResync('seq_gap', expectedSeq, message.data.seq);
388
395
  return;
389
396
  }
397
+ if (this.config.retainInputMessage?.(wire) === false) return;
390
398
  this.baseline = {
391
399
  ...baseline,
392
400
  seq: message.data.seq,
@@ -433,6 +441,7 @@ export class WsClient {
433
441
  this.requestResync('invalid_resume', baseline ? baseline.seq + 1 : undefined, message.data.to_seq);
434
442
  return;
435
443
  }
444
+ if (this.config.retainInputMessage?.(wire) === false) return;
436
445
  const events = this.recoveryEvents;
437
446
  const replayedEventCount = events.length;
438
447
  this.recoveryEvents = [];
@@ -473,6 +482,7 @@ export class WsClient {
473
482
 
474
483
  private requestResync(reason: string, expectedSeq?: number, receivedSeq?: number): void {
475
484
  if (this.awaitingSnapshot) return;
485
+ this.config.discardUnappliedMessages?.();
476
486
  this.awaitingSnapshot = true;
477
487
  this.recovering = true;
478
488
  this.recoveryStartSeq = this.baseline?.seq ?? null;
@@ -107,6 +107,8 @@ export interface PerceptionBoundary {
107
107
  }
108
108
 
109
109
  export interface PerceptionEventProposal {
110
+ /** Whether this Event may initiate Agent delivery; omitted means true. */
111
+ readonly wakeAgent?: boolean;
110
112
  readonly type: string;
111
113
  /** Stable identity of this semantic slice within the current segment. */
112
114
  readonly dedupeKey: string;
@@ -116,6 +118,8 @@ export interface PerceptionEventProposal {
116
118
  }
117
119
 
118
120
  export interface ClawclawPerceptionManifest {
121
+ /** Whether this Event may initiate Agent delivery; omitted means true. */
122
+ readonly wakeAgent?: boolean;
119
123
  readonly apiVersion: typeof PERCEPTION_PLUGIN_API_VERSION;
120
124
  readonly id: string;
121
125
  readonly name: string;