@myclaw163/clawclaw-cli 0.9.2 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/QUICKSTART_OTHER.md +5 -5
  2. package/README.md +7 -8
  3. package/bin/clawclaw-channel.mjs +2 -2
  4. package/bin/clawclaw-cli.mjs +2 -2
  5. package/docs/configuration.md +25 -99
  6. package/docs/custom-modules/README.md +23 -35
  7. package/docs/custom-modules/api-reference.md +95 -273
  8. package/docs/custom-modules/behavior.md +94 -78
  9. package/docs/custom-modules/examples/auto-escape.ts +5 -5
  10. package/docs/custom-modules/examples/go-to-point.ts +11 -11
  11. package/docs/custom-modules/examples/nearby-reflex.ts +2 -2
  12. package/docs/custom-modules/examples/task-reminder.ts +4 -5
  13. package/docs/custom-modules/perception.md +42 -603
  14. package/docs/custom-modules/skill.md +1 -1
  15. package/docs/desktop-event-push.md +3 -3
  16. package/docs/runtime/README.md +16 -17
  17. package/docs/runtime/agent-notifications.md +31 -12
  18. package/docs/runtime/behavior-runtime.md +47 -46
  19. package/docs/runtime/components-and-boundaries.md +70 -28
  20. package/docs/runtime/events-and-state.md +41 -18
  21. package/docs/runtime/framework-package.md +73 -0
  22. package/docs/runtime/official-perception-modules.md +5 -5
  23. package/docs/runtime/owner-lifecycle-and-observability.md +6 -6
  24. package/docs/runtime/short-commands-and-capabilities.md +8 -9
  25. package/docs/user-guide/README.md +10 -7
  26. package/docs/user-guide/api-reference.md +81 -164
  27. package/docs/user-guide/behavior.md +84 -68
  28. package/docs/user-guide/installation.md +7 -7
  29. package/docs/user-guide/perception.md +42 -586
  30. package/docs/user-guide/skill.md +1 -1
  31. package/package.json +24 -9
  32. package/scripts/check-framework-boundaries.mjs +27 -0
  33. package/scripts/check-skill-command-surface.mjs +8 -13
  34. package/scripts/install-local-runtime.mjs +35 -0
  35. package/scripts/qa-install.mjs +105 -0
  36. package/skills/clawclaw/SKILL.md +49 -49
  37. package/skills/clawclaw/references/BEHAVIORS.md +42 -73
  38. package/skills/clawclaw/references/CHANNELS.md +2 -2
  39. package/skills/clawclaw/references/COMMANDS.md +60 -101
  40. package/skills/clawclaw/references/CUSTOM-BEHAVIOR-DEVELOPMENT.md +94 -78
  41. package/skills/clawclaw/references/CUSTOM-MODULE-API.md +95 -180
  42. package/skills/clawclaw/references/CUSTOM-MODULES.md +24 -24
  43. package/skills/clawclaw/references/CUSTOM-PERCEPTION-DEVELOPMENT.md +42 -608
  44. package/skills/clawclaw/references/CUSTOM-SKILL-DEVELOPMENT.md +1 -1
  45. package/skills/clawclaw/references/HUB.md +1 -1
  46. package/skills/clawclaw/references/STREAM.md +8 -8
  47. package/skills/clawclaw/references/TACTICS.md +1 -1
  48. package/src/channel/claude-channel-ipc-server.ts +1 -0
  49. package/src/cli.ts +4 -2
  50. package/src/commands/behavior.ts +26 -10
  51. package/src/commands/capability.ts +3 -3
  52. package/src/commands/load.ts +3 -2
  53. package/src/commands/perception.ts +5 -0
  54. package/src/commands/strategy.ts +2 -0
  55. package/src/lib/capability-load-diagnostics.ts +3 -3
  56. package/src/lib/perception-config.ts +18 -6
  57. package/src/lib/runtime-cadence-config.ts +16 -0
  58. package/src/lib/workspace-runtime-lock.ts +3 -46
  59. package/src/pipeline/canonical-event-presentation.ts +23 -124
  60. package/src/pipeline/event-format.ts +12 -21
  61. package/src/pipeline/event-hints.ts +6 -6
  62. package/src/pipeline/game-sync-event-normalization.ts +57 -63
  63. package/src/runtime/clawclaw/behavior-decision.ts +7 -3
  64. package/src/runtime/clawclaw/behavior-input.ts +8 -4
  65. package/src/runtime/clawclaw/behavior-observation-memory.ts +3 -2
  66. package/src/runtime/clawclaw/notification-policy.ts +42 -0
  67. package/src/runtime/clawclaw/notification-turn.ts +19 -0
  68. package/src/runtime/clawclaw/observation-frame-projector.ts +3 -3
  69. package/src/runtime/clawclaw/owner.ts +18 -4
  70. package/src/runtime/clawclaw/perception-module-manifest.ts +8 -3
  71. package/src/runtime/clawclaw/perception-strategy-names.ts +17 -0
  72. package/src/runtime/clawclaw/persistence-compatibility.ts +36 -0
  73. package/src/runtime/clawclaw/plugins/action/action-plugin.ts +3 -3
  74. package/src/runtime/clawclaw/plugins/assembly.ts +7 -9
  75. package/src/runtime/clawclaw/plugins/behavior/activation-manager.ts +1 -495
  76. package/src/runtime/clawclaw/plugins/behavior/behavior-analysis-thread-host.ts +17 -190
  77. package/src/runtime/clawclaw/plugins/behavior/behavior-instance-snapshot.ts +1 -123
  78. package/src/runtime/clawclaw/plugins/behavior/behavior-plugin.ts +126 -1779
  79. package/src/runtime/clawclaw/plugins/behavior/behavior-runner.ts +13 -276
  80. package/src/runtime/clawclaw/plugins/behavior/behavior-source.ts +2 -2
  81. package/src/runtime/clawclaw/plugins/behavior/behavior-worker-bridge.ts +8 -0
  82. package/src/runtime/clawclaw/plugins/behavior/control-driver.ts +3 -180
  83. package/src/runtime/clawclaw/plugins/behavior/thread-behavior-executor.ts +13 -972
  84. package/src/runtime/clawclaw/plugins/derived-event/derived-event-plugin.ts +8 -109
  85. package/src/runtime/clawclaw/plugins/derived-event/derived-event-rules.ts +2 -2
  86. package/src/runtime/clawclaw/plugins/derived-event/reminder-plugin.ts +25 -17
  87. package/src/runtime/clawclaw/plugins/derived-event/reminder-runner.ts +1 -0
  88. package/src/runtime/clawclaw/plugins/derived-event/state-change-rules.ts +3 -3
  89. package/src/runtime/clawclaw/plugins/derived-event/task-change-tracker.ts +1 -259
  90. package/src/runtime/clawclaw/plugins/game-query/game-query-plugin.ts +4 -4
  91. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-adapter.ts +9 -97
  92. package/src/runtime/clawclaw/plugins/mailbox/adapters/claude-channel-socket.ts +8 -343
  93. package/src/runtime/clawclaw/plugins/mailbox/agent-mailbox-coordinator.ts +60 -572
  94. package/src/runtime/clawclaw/plugins/mailbox/desktop-event-socket.ts +6 -434
  95. package/src/runtime/clawclaw/plugins/mailbox/mailbox-event-batch-delivery.ts +1 -266
  96. package/src/runtime/clawclaw/plugins/mailbox/mailbox-plugin.ts +6 -161
  97. package/src/runtime/clawclaw/plugins/mailbox/mailbox-presentation.ts +4 -23
  98. package/src/runtime/clawclaw/plugins/mailbox/mailbox-service.ts +49 -752
  99. package/src/runtime/clawclaw/plugins/perception/map-concurrent.ts +1 -0
  100. package/src/runtime/clawclaw/plugins/perception/notification-content.ts +60 -0
  101. package/src/runtime/clawclaw/plugins/perception/perception-module-plugin.ts +119 -297
  102. package/src/runtime/clawclaw/plugins/perception/perception-module-runner.ts +7 -56
  103. package/src/runtime/clawclaw/plugins/perception/perception-module-worker.ts +11 -33
  104. package/src/runtime/clawclaw/plugins/perception/perception-plugin.ts +18 -7
  105. package/src/runtime/clawclaw/plugins/perception/perception-source.ts +12 -6
  106. package/src/runtime/clawclaw/plugins/perception/task-baseline.ts +29 -0
  107. package/src/runtime/clawclaw/plugins/source/source-plugin.ts +6 -0
  108. package/src/runtime/clawclaw/plugins/system-perception/rule-runner.ts +107 -0
  109. package/src/runtime/clawclaw/plugins/system-perception/system-perceptions.ts +107 -0
  110. package/src/runtime/clawclaw/plugins/trigger/trigger-plugin.ts +8 -3
  111. package/src/runtime/clawclaw/reaction-behavior-resolution.ts +1 -23
  112. package/src/runtime/clawclaw/reminder-manifest.ts +5 -2
  113. package/src/runtime/clawclaw/runtime-cadence.ts +24 -0
  114. package/src/runtime/clawclaw/runtime-state.ts +11 -0
  115. package/src/runtime/clawclaw/runtime.ts +8 -0
  116. package/src/runtime/clawclaw/strategy-memory-rpc.ts +1 -63
  117. package/src/runtime/clawclaw/strategy-memory.ts +1 -77
  118. package/src/runtime/clawclaw-event-catalog.ts +12 -12
  119. package/src/runtime/framework/action-diagnostics.ts +3 -83
  120. package/src/runtime/framework/capability-registry.ts +2 -395
  121. package/src/runtime/framework/contracts.ts +2 -144
  122. package/src/runtime/framework/control/control-scheduler.ts +2 -1268
  123. package/src/runtime/framework/control/domain-set.ts +2 -81
  124. package/src/runtime/framework/control/index.ts +1 -3
  125. package/src/runtime/framework/control/types.ts +2 -191
  126. package/src/runtime/framework/control-coordinator.ts +2 -11
  127. package/src/runtime/framework/events/event-catalog.ts +2 -74
  128. package/src/runtime/framework/events/event-delivery-service.ts +2 -200
  129. package/src/runtime/framework/events/event-ingress.ts +2 -99
  130. package/src/runtime/framework/events/event-repository.ts +2 -82
  131. package/src/runtime/framework/events/index.ts +2 -7
  132. package/src/runtime/framework/events/memory-event-repository.ts +2 -1100
  133. package/src/runtime/framework/events/runtime-resume.ts +2 -20
  134. package/src/runtime/framework/events/sqlite-event-repository.ts +5 -2280
  135. package/src/runtime/framework/events/test-helpers.ts +1 -1
  136. package/src/runtime/framework/events/types.ts +2 -312
  137. package/src/runtime/framework/index.ts +3 -7
  138. package/src/runtime/framework/load-node-sqlite.ts +2 -17
  139. package/src/runtime/framework/mailbox/coalescer.ts +2 -83
  140. package/src/runtime/framework/mailbox/host-notification-sink.ts +2 -44
  141. package/src/runtime/framework/mailbox/identity.ts +2 -38
  142. package/src/runtime/framework/mailbox/index.ts +1 -7
  143. package/src/runtime/framework/mailbox/mailbox-dispatcher.ts +2 -282
  144. package/src/runtime/framework/mailbox/mailbox-repository.ts +2 -44
  145. package/src/runtime/framework/mailbox/notification-projector.ts +2 -65
  146. package/src/runtime/framework/mailbox/types.ts +2 -145
  147. package/src/runtime/framework/modules/runtime-module-loader-hooks.mjs +2 -154
  148. package/src/runtime/framework/modules/runtime-module-loader.ts +7 -51
  149. package/src/runtime/framework/modules/runtime-module-output.ts +2 -24
  150. package/src/runtime/framework/modules/runtime-module-worker-bootstrap.mjs +3 -23
  151. package/src/runtime/framework/participation-manager.ts +2 -253
  152. package/src/runtime/framework/persistence/async-sqlite-persistence-worker.ts +2 -77
  153. package/src/runtime/framework/persistence/memory-first-persistence.ts +4 -489
  154. package/src/runtime/framework/plugins/contracts.ts +2 -396
  155. package/src/runtime/framework/plugins/index.ts +1 -4
  156. package/src/runtime/framework/plugins/runtime-event-identity.ts +2 -17
  157. package/src/runtime/framework/plugins/runtime-input-journal.ts +2 -866
  158. package/src/runtime/framework/plugins/runtime-plugin-scheduler.ts +2 -2071
  159. package/src/runtime/framework/runtime.ts +5 -1843
  160. package/src/runtime/framework/serial-task-queue.ts +2 -14
  161. package/src/runtime/framework/state/contracts.ts +2 -49
  162. package/src/runtime/framework/state/current-state-store.ts +2 -184
  163. package/src/runtime/framework/state/durable-agent-state-snapshot.ts +2 -209
  164. package/src/runtime/framework/state/freshness.ts +2 -72
  165. package/src/runtime/framework/state/history/contracts.ts +2 -158
  166. package/src/runtime/framework/state/history/history-writer.ts +2 -194
  167. package/src/runtime/framework/state/history/memory-state-history-repository.ts +2 -230
  168. package/src/runtime/framework/state/history/sqlite-state-history-repository.ts +2 -648
  169. package/src/runtime/framework/state/history/trajectory-projector.ts +2 -209
  170. package/src/runtime/framework/state/index.ts +1 -13
  171. package/src/runtime/framework/state/state-ingress.ts +2 -95
  172. package/src/runtime/framework/state/state-provider.ts +2 -62
  173. package/src/runtime/framework/state/types.ts +2 -155
  174. package/src/runtime/host-monitor-rate-gate.ts +1 -123
  175. package/src/runtime/mailbox-host-relay.ts +1 -326
  176. package/src/runtime/observability/detail-capture.ts +4 -108
  177. package/src/runtime/observability/process-timing.ts +2 -59
  178. package/src/runtime/observability/recorder.ts +2 -238
  179. package/src/runtime/observability/redaction.ts +2 -181
  180. package/src/runtime/observability/reporter.ts +2 -154
  181. package/src/runtime/observability/sinks.ts +2 -135
  182. package/src/runtime/observability/types.ts +2 -92
  183. package/src/runtime/owner-control.ts +45 -320
  184. package/src/runtime/parent-process-watch.ts +1 -51
  185. package/src/runtime/process-identity.ts +1 -72
  186. package/src/sdk/behavior-plugin/index.ts +9 -3
  187. package/src/sdk/index.ts +40 -2
  188. package/src/sdk/module-input/index.ts +5 -0
  189. package/src/sdk/perception-plugin/index.ts +3 -1
  190. package/src/sdk/perception-plugin/module.ts +12 -2
  191. package/src/sdk/perception-plugin/reminder-rules.ts +8 -42
  192. package/src/sdk/perception-plugin/reminder.ts +2 -1
  193. package/src/sdk/perception-plugin/task-change-tracker.ts +258 -0
  194. package/src/sdk/strategy-memory.ts +1 -12
  195. package/src/sdk/strategy-plugin/index.ts +36 -0
  196. package/src/sdk/trigger-plugin/index.ts +1 -0
  197. package/docs/custom-modules/trigger.md +0 -295
@@ -1,266 +1 @@
1
- import type { CanonicalEvent } from '../../../framework/events/index.js';
2
- import type {
3
- AgentMailboxBatch,
4
- PreparedAgentMailboxBatch,
5
- } from './agent-mailbox-coordinator.js';
6
- import type { ClawclawMailboxAdapterDescriptor } from './mailbox-plugin.js';
7
-
8
- const DEFAULT_RETRY_BASE_MS = 250;
9
- const DEFAULT_RETRY_MAX_MS = 5_000;
10
-
11
- export interface MailboxEventBatchSource {
12
- prepare(): PreparedAgentMailboxBatch;
13
- renew(deliveryToken: string): boolean;
14
- ack(deliveryToken: string): boolean;
15
- release(deliveryToken: string): boolean;
16
- }
17
-
18
- export interface MailboxEventBatchEnvelope {
19
- /** Stable for this durable preparation and required on the application ACK. */
20
- deliveryId: string;
21
- batch: AgentMailboxBatch;
22
- }
23
-
24
- export interface MailboxEventBatchAcceptance {
25
- acceptedAt: string;
26
- receiptId?: string;
27
- }
28
-
29
- /** A transport adapter only hands a prepared batch to a Host input queue. */
30
- export interface MailboxEventBatchSink {
31
- deliver(
32
- envelope: MailboxEventBatchEnvelope,
33
- signal: AbortSignal,
34
- ): Promise<MailboxEventBatchAcceptance>;
35
- close(): Promise<void>;
36
- }
37
-
38
- export interface MailboxEventBatchPumpOptions {
39
- source: MailboxEventBatchSource;
40
- sink: MailboxEventBatchSink;
41
- adapter?: ClawclawMailboxAdapterDescriptor;
42
- now?: () => number;
43
- diagnostic?: (name: string, attributes?: Record<string, unknown>) => void;
44
- }
45
-
46
- interface PreparedPumpDelivery {
47
- deliveryToken: string;
48
- batch: AgentMailboxBatch;
49
- readyEpochSeq: number;
50
- generation: number;
51
- acceptance?: MailboxEventBatchAcceptance;
52
- }
53
-
54
- function errorMessage(error: unknown): string {
55
- return error instanceof Error ? error.message : String(error);
56
- }
57
-
58
- function retryDelay(attempt: number): number {
59
- const exponent = Math.max(0, Math.min(8, attempt - 1));
60
- return Math.min(DEFAULT_RETRY_MAX_MS, DEFAULT_RETRY_BASE_MS * (2 ** exponent));
61
- }
62
-
63
- /**
64
- * Coordinates the durable Mailbox detail lane with the Host sink selected for
65
- * this Owner. Desktop IPC and Channels are sibling adapters over this boundary;
66
- * the pump knows neither their wire formats nor Monitor presentation.
67
- */
68
- export class MailboxEventBatchPump {
69
- readonly adapter?: ClawclawMailboxAdapterDescriptor;
70
- private readonly source: MailboxEventBatchSource;
71
- private readonly sink: MailboxEventBatchSink;
72
- private readonly now: () => number;
73
- private readonly diagnostic?: MailboxEventBatchPumpOptions['diagnostic'];
74
-
75
- private stopped = false;
76
- private pending = false;
77
- private generation = 0;
78
- private retryAttempt = 0;
79
- private timer: ReturnType<typeof setTimeout> | undefined;
80
- private timerAtMs: number | undefined;
81
- private pumpRequested = false;
82
- private pumpPromise: Promise<void> | undefined;
83
- private activeAbort: AbortController | undefined;
84
- private preparedDelivery: PreparedPumpDelivery | undefined;
85
-
86
- constructor(options: MailboxEventBatchPumpOptions) {
87
- this.adapter = options.adapter;
88
- this.source = options.source;
89
- this.sink = options.sink;
90
- this.now = options.now ?? Date.now;
91
- this.diagnostic = options.diagnostic;
92
- }
93
-
94
- /** Called by the Mailbox Plugin after committed Agent-visible Events appear. */
95
- observeEvents(events: readonly CanonicalEvent[]): void {
96
- if (this.stopped) return;
97
- const visible = events.filter((event) => event.deliveryClass !== 'internal_only');
98
- if (visible.length === 0) return;
99
- this.pending = true;
100
- this.generation += 1;
101
- this.clearTimer();
102
- this.requestPump();
103
- }
104
-
105
- /** Shortens a persisted retry after the Host transport reconnects. */
106
- retryNow(): void {
107
- if (this.stopped || !this.pending || this.retryAttempt === 0) return;
108
- this.clearTimer();
109
- this.scheduleAt(this.now());
110
- }
111
-
112
- /** Kicks delivery without making Runtime lifecycle flush wait for Host ACK. */
113
- async flush(): Promise<void> {
114
- if (this.stopped) {
115
- return;
116
- }
117
- this.pending = true;
118
- this.generation += 1;
119
- this.clearTimer();
120
- this.requestPump();
121
- }
122
-
123
- /** Waits only for the currently runnable delivery attempt at an Owner boundary. */
124
- async drain(): Promise<void> {
125
- await this.flush();
126
- while (this.pumpPromise) await this.pumpPromise;
127
- }
128
-
129
- async stop(): Promise<void> {
130
- if (this.stopped) {
131
- await this.pumpPromise;
132
- return;
133
- }
134
- this.stopped = true;
135
- this.pending = false;
136
- this.pumpRequested = false;
137
- this.clearTimer();
138
- this.activeAbort?.abort(new Error('mailbox_event_batch_pump_stopped'));
139
- await this.pumpPromise;
140
- this.releasePreparedDelivery();
141
- await this.sink.close();
142
- }
143
-
144
- private requestPump(): void {
145
- if (this.stopped) return;
146
- this.pumpRequested = true;
147
- if (this.pumpPromise) return;
148
- this.pumpPromise = this.pump()
149
- .catch((error) => {
150
- this.diagnostic?.('runtime.mailbox.event_batch_pump_failed', {
151
- error: errorMessage(error),
152
- });
153
- })
154
- .finally(() => {
155
- this.pumpPromise = undefined;
156
- if (this.pumpRequested && !this.stopped) this.requestPump();
157
- });
158
- }
159
-
160
- private async pump(): Promise<void> {
161
- this.pumpRequested = false;
162
- if (!this.pending || this.stopped) return;
163
-
164
- try {
165
- let prepared = this.preparedDelivery;
166
- if (prepared) {
167
- if (!this.source.renew(prepared.deliveryToken)) {
168
- this.releasePreparedDelivery();
169
- throw new Error('mailbox_event_batch_lease_lost');
170
- }
171
- } else {
172
- const selected = this.source.prepare();
173
- const deliveryToken = selected.deliveryToken;
174
- if (!deliveryToken || selected.batch.events.length === 0) {
175
- if (deliveryToken) this.source.release(deliveryToken);
176
- if (selected.nextAttemptAtMs !== undefined) {
177
- this.scheduleAt(selected.nextAttemptAtMs);
178
- return;
179
- }
180
- if (!this.pumpRequested) {
181
- this.pending = false;
182
- this.retryAttempt = 0;
183
- } else {
184
- this.scheduleAt(this.now());
185
- }
186
- return;
187
- }
188
- prepared = {
189
- deliveryToken,
190
- batch: selected.batch,
191
- readyEpochSeq: selected.readyEpochSeq,
192
- generation: this.generation,
193
- };
194
- this.preparedDelivery = prepared;
195
- }
196
-
197
- if (!prepared.acceptance) {
198
- const controller = new AbortController();
199
- this.activeAbort = controller;
200
- prepared.acceptance = await this.sink.deliver({
201
- deliveryId: prepared.deliveryToken,
202
- batch: prepared.batch,
203
- }, controller.signal);
204
- this.activeAbort = undefined;
205
- }
206
- if (!this.source.ack(prepared.deliveryToken)) {
207
- throw new Error('mailbox_event_batch_ack_lost');
208
- }
209
- this.preparedDelivery = undefined;
210
- this.diagnostic?.('runtime.mailbox.event_batch_accepted', {
211
- delivery_id: prepared.deliveryToken,
212
- event_count: prepared.batch.events.length,
213
- ready_epoch_seq: prepared.readyEpochSeq,
214
- accepted_at: prepared.acceptance.acceptedAt,
215
- ...(prepared.acceptance.receiptId ? { receipt_id: prepared.acceptance.receiptId } : {}),
216
- });
217
- this.retryAttempt = 0;
218
- if (this.generation === prepared.generation) {
219
- this.pending = false;
220
- } else {
221
- this.scheduleAt(this.now());
222
- }
223
- } catch (error) {
224
- this.activeAbort = undefined;
225
- if (this.stopped) {
226
- this.releasePreparedDelivery();
227
- return;
228
- }
229
- this.pending = true;
230
- this.retryAttempt += 1;
231
- const delayMs = retryDelay(this.retryAttempt);
232
- this.diagnostic?.('runtime.mailbox.event_batch_retry', {
233
- attempt: this.retryAttempt,
234
- retry_in_ms: delayMs,
235
- error: errorMessage(error),
236
- });
237
- this.scheduleAt(this.now() + delayMs);
238
- }
239
- }
240
-
241
- private scheduleAt(atMs: number): void {
242
- if (this.stopped) return;
243
- if (this.timer && this.timerAtMs !== undefined && this.timerAtMs <= atMs) return;
244
- this.clearTimer();
245
- this.timerAtMs = atMs;
246
- const timer = setTimeout(() => {
247
- this.timer = undefined;
248
- this.timerAtMs = undefined;
249
- this.requestPump();
250
- }, Math.max(0, atMs - this.now()));
251
- timer.unref?.();
252
- this.timer = timer;
253
- }
254
-
255
- private clearTimer(): void {
256
- if (this.timer) clearTimeout(this.timer);
257
- this.timer = undefined;
258
- this.timerAtMs = undefined;
259
- }
260
-
261
- private releasePreparedDelivery(): void {
262
- const prepared = this.preparedDelivery;
263
- this.preparedDelivery = undefined;
264
- if (prepared) this.source.release(prepared.deliveryToken);
265
- }
266
- }
1
+ export * from '@myclaw163/game-agent-runtime/mailbox/mailbox-event-batch-delivery.js';
@@ -1,163 +1,8 @@
1
- import type {
2
- RuntimeEpochFlushReason,
3
- RuntimeEpochPlugin,
4
- RuntimeEpochPluginContext,
5
- RuntimeEpochPluginInput,
6
- RuntimeEpochPluginResult,
7
- } from '../../../framework/plugins/index.js';
8
- import type { CanonicalEvent } from '../../../framework/events/index.js';
9
-
1
+ import { MailboxPlugin, type MailboxPluginOptions } from '@myclaw163/game-agent-runtime/mailbox/mailbox-plugin.js';
2
+ export { CompositeMailboxDriver as CompositeClawclawMailboxDriver } from '@myclaw163/game-agent-runtime/mailbox/mailbox-plugin.js';
3
+ export type { MailboxDriver as ClawclawMailboxDriver, MailboxAcknowledgement as ClawclawMailboxAcknowledgement, MailboxAdapterDescriptor as ClawclawMailboxAdapterDescriptor } from '@myclaw163/game-agent-runtime/mailbox/mailbox-plugin.js';
10
4
  export const CLAWCLAW_MAILBOX_PLUGIN_ID = 'clawclaw.mailbox';
11
-
12
- export type ClawclawMailboxAcknowledgement =
13
- | 'explicit'
14
- | 'on_delivery'
15
- | 'none';
16
-
17
- export type ClawclawMailboxAdapterDescriptor =
18
- | {
19
- id: string;
20
- acknowledgement: 'explicit' | 'on_delivery';
21
- /** Durable delivery state is isolated by Adapter consumer. */
22
- consumerId: string;
23
- }
24
- | {
25
- id: string;
26
- acknowledgement: 'none';
27
- /** Best-effort outputs never own or advance a durable consumer cursor. */
28
- consumerId?: never;
29
- };
30
-
31
- /** Delivery I/O runs independently from epoch cadence. */
32
- export interface ClawclawMailboxDriver {
33
- /** Optional for internal components; public transport adapters should declare it. */
34
- readonly adapter?: ClawclawMailboxAdapterDescriptor;
35
- observeEvents?(events: readonly CanonicalEvent[]): void;
36
- flush(): Promise<void>;
37
- stop(): Promise<void>;
38
- }
39
-
40
- /** Keeps the Mailbox Plugin single-owner while composing independent adapters. */
41
- export class CompositeClawclawMailboxDriver implements ClawclawMailboxDriver {
42
- constructor(private readonly drivers: readonly ClawclawMailboxDriver[]) {
43
- const consumers = new Map<string, string>();
44
- for (const driver of drivers) {
45
- const adapter = driver.adapter;
46
- if (!adapter) continue;
47
- if (adapter.acknowledgement === 'none') {
48
- if (typeof (adapter as { consumerId?: unknown }).consumerId === 'string') {
49
- throw new Error(`No-ACK Mailbox adapter '${adapter.id}' cannot own a consumerId.`);
50
- }
51
- continue;
52
- }
53
- if (!adapter.consumerId.trim()) {
54
- throw new Error(`ACK Mailbox adapter '${adapter.id}' requires a non-empty consumerId.`);
55
- }
56
- const existing = consumers.get(adapter.consumerId);
57
- if (existing) {
58
- throw new Error(
59
- `Mailbox adapters '${existing}' and '${adapter.id}' share consumerId '${adapter.consumerId}'.`,
60
- );
61
- }
62
- consumers.set(adapter.consumerId, adapter.id);
63
- }
64
- }
65
-
66
- observeEvents(events: readonly CanonicalEvent[]): void {
67
- let firstReliableError: unknown;
68
- for (const driver of this.drivers) {
69
- try {
70
- driver.observeEvents?.(events);
71
- } catch (error) {
72
- if (driver.adapter?.acknowledgement !== 'none') firstReliableError ??= error;
73
- }
74
- }
75
- if (firstReliableError) throw firstReliableError;
76
- }
77
-
78
- async flush(): Promise<void> {
79
- const results = await Promise.allSettled(this.drivers.map((driver) => (
80
- Promise.resolve().then(() => driver.flush())
81
- )));
82
- const failed = results.findIndex((result, index) => (
83
- result.status === 'rejected'
84
- && this.drivers[index]?.adapter?.acknowledgement !== 'none'
85
- ));
86
- if (failed >= 0) throw (results[failed] as PromiseRejectedResult).reason;
87
- }
88
-
89
- async stop(): Promise<void> {
90
- const drivers = [...this.drivers].reverse();
91
- const results = await Promise.allSettled(drivers.map((driver) => (
92
- Promise.resolve().then(() => driver.stop())
93
- )));
94
- const failed = results.findIndex((result, index) => (
95
- result.status === 'rejected'
96
- && drivers[index]?.adapter?.acknowledgement !== 'none'
97
- ));
98
- if (failed >= 0) throw (results[failed] as PromiseRejectedResult).reason;
99
- }
100
- }
101
-
102
- export interface ClawclawMailboxPluginOptions {
103
- after?: readonly string[];
104
- timeoutMs?: number;
105
- }
106
-
107
- /** Durable Agent delivery is an ordinary Event-consuming Epoch Plugin. */
108
- export class ClawclawMailboxPlugin implements RuntimeEpochPlugin<Record<string, any>> {
109
- readonly kind = 'epoch' as const;
110
- readonly id = CLAWCLAW_MAILBOX_PLUGIN_ID;
111
- readonly version = '1';
112
- readonly consumes = { events: ['*'] } as const;
113
- readonly after: readonly string[];
114
- readonly timeoutMs: number;
115
-
116
- private driver?: ClawclawMailboxDriver;
117
- private stopped = false;
118
-
119
- constructor(options: ClawclawMailboxPluginOptions = {}) {
120
- this.after = [...(options.after ?? [])];
121
- this.timeoutMs = options.timeoutMs ?? 5_000;
122
- }
123
-
124
- attachDriver(
125
- driver: ClawclawMailboxDriver,
126
- ): void {
127
- if (this.stopped) throw new Error('Clawclaw Mailbox Plugin is stopped.');
128
- if (this.driver && this.driver !== driver) {
129
- throw new Error('Clawclaw Mailbox Plugin already has a delivery driver.');
130
- }
131
- this.driver = driver;
132
- }
133
-
134
- async update(
135
- input: RuntimeEpochPluginInput<Record<string, any>>,
136
- ): Promise<RuntimeEpochPluginResult> {
137
- const eventCursor = input.pendingEvents.at(-1)?.commitSeq;
138
- if (eventCursor !== undefined) this.driver?.observeEvents?.(input.pendingEvents);
139
- return {
140
- outcome: eventCursor === undefined ? 'noop' : 'processed',
141
- ...(eventCursor === undefined ? { reason: 'no_pending_events' } : {}),
142
- ...(eventCursor === undefined ? {} : { consumeEventsThroughCommitSeq: eventCursor }),
143
- };
144
- }
145
-
146
- async flush(
147
- _reason: RuntimeEpochFlushReason,
148
- _context: RuntimeEpochPluginContext<Record<string, any>>,
149
- ): Promise<void> {
150
- if (!this.driver) return;
151
- await this.driver.flush();
152
- }
153
-
154
- async stop(reason: 'segment_ending' | 'shutdown'): Promise<void> {
155
- if (!this.driver) return;
156
- if (reason === 'segment_ending') {
157
- await this.driver.flush();
158
- return;
159
- }
160
- this.stopped = true;
161
- await this.driver.stop();
162
- }
5
+ export type ClawclawMailboxPluginOptions = Omit<MailboxPluginOptions, 'id'>;
6
+ export class ClawclawMailboxPlugin extends MailboxPlugin {
7
+ constructor(options: ClawclawMailboxPluginOptions = {}) { super({ ...options, id: CLAWCLAW_MAILBOX_PLUGIN_ID }); }
163
8
  }
@@ -1,5 +1,4 @@
1
- import { presentCanonicalEvent } from '../../../../pipeline/canonical-event-presentation.js';
2
- import { formatEventMessage } from '../../../../pipeline/event-format.js';
1
+ import { presentCanonicalEvent, preparedMonitorText } from '../../../../pipeline/canonical-event-presentation.js';
3
2
  import {
4
3
  fitShortMonitorPayload,
5
4
  } from '../../../canonical-monitor.js';
@@ -59,28 +58,12 @@ function truncate(value: string, maxChars: number): string {
59
58
  return `${value.slice(0, Math.max(0, maxChars - 1))}…`;
60
59
  }
61
60
 
62
- function fullMonitorMessage(
63
- event: Record<string, any>,
64
- state: Record<string, any> | null | undefined,
65
- ): string {
66
- const type = String(event.type ?? 'event');
67
- if (type.startsWith('visual.')) {
68
- const content = typeof event.text === 'string' ? event.text.trim() : '';
69
- return `${type} | ${content}`.trimEnd();
70
- }
71
- return formatEventMessage(event, {
72
- summary: state,
73
- maxTextLength: Number.MAX_SAFE_INTEGER,
74
- });
75
- }
76
-
77
61
  export function clawclawEventRequiresDetails(
78
62
  event: CanonicalEvent,
79
63
  state?: Record<string, any> | null,
80
64
  ): boolean {
81
65
  if (CLAWCLAW_DETAIL_EVENT_TYPES.has(event.type)) return true;
82
- const presented = presentCanonicalEvent(event, state);
83
- return fullMonitorMessage(presented, state).length > CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS;
66
+ return preparedMonitorText(event).length > CLAWCLAW_MONITOR_INLINE_MESSAGE_MAX_CHARS;
84
67
  }
85
68
 
86
69
  export function monitorItemPayload(
@@ -88,7 +71,7 @@ export function monitorItemPayload(
88
71
  ready: RuntimeReadyView,
89
72
  ): ClawclawMonitorItemPayload {
90
73
  const presented = presentCanonicalEvent(event, ready.state.state);
91
- const fullMessage = fullMonitorMessage(presented, ready.state.state);
74
+ const fullMessage = preparedMonitorText(event);
92
75
  return {
93
76
  schema: 'clawclaw.monitor-item.v1',
94
77
  monitor_priority: clawclawMonitorEventPriority(event),
@@ -147,9 +130,7 @@ export function composeClawclawMonitorNotification(
147
130
  details_required: detailsRequired.length > 0 ? detailsRequired : undefined,
148
131
  backlog,
149
132
  guidance: backlog
150
- ? types.includes('speech_your_turn')
151
- ? `Submit ccl speak now before any query or narration, then run ccl events to inspect the ${backlog.remaining_events} delegated backlog Events.`
152
- : `Monitor delegated ${backlog.remaining_events} additional Events to the durable backlog. Run ccl events now before continuing.`
133
+ ? `Monitor delegated ${backlog.remaining_events} additional Events to the durable backlog. Read them with ccl events when appropriate.`
153
134
  : undefined,
154
135
  truncated: backlog ? true : undefined,
155
136
  }));