@animalabs/agent-framework 0.19.0 → 0.21.0

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 (214) hide show
  1. package/README.md +29 -0
  2. package/dist/src/agent.d.ts +22 -0
  3. package/dist/src/agent.d.ts.map +1 -1
  4. package/dist/src/agent.js +128 -10
  5. package/dist/src/agent.js.map +1 -1
  6. package/dist/src/api/types.d.ts +1 -1
  7. package/dist/src/api/types.d.ts.map +1 -1
  8. package/dist/src/code-execution/py-runner.d.ts +10 -1
  9. package/dist/src/code-execution/py-runner.d.ts.map +1 -1
  10. package/dist/src/code-execution/py-runner.js +27 -2
  11. package/dist/src/code-execution/py-runner.js.map +1 -1
  12. package/dist/src/code-execution/tool-definition.d.ts +21 -0
  13. package/dist/src/code-execution/tool-definition.d.ts.map +1 -1
  14. package/dist/src/code-execution/tool-definition.js +34 -0
  15. package/dist/src/code-execution/tool-definition.js.map +1 -1
  16. package/dist/src/framework.d.ts +153 -2
  17. package/dist/src/framework.d.ts.map +1 -1
  18. package/dist/src/framework.js +1175 -76
  19. package/dist/src/framework.js.map +1 -1
  20. package/dist/src/index.d.ts +2 -0
  21. package/dist/src/index.d.ts.map +1 -1
  22. package/dist/src/index.js +4 -0
  23. package/dist/src/index.js.map +1 -1
  24. package/dist/src/mcpl/capability-grant.d.ts +1 -1
  25. package/dist/src/mcpl/capability-grant.d.ts.map +1 -1
  26. package/dist/src/mcpl/capability-grant.js +49 -5
  27. package/dist/src/mcpl/capability-grant.js.map +1 -1
  28. package/dist/src/mcpl/channel-registry.d.ts +59 -2
  29. package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
  30. package/dist/src/mcpl/channel-registry.js +106 -6
  31. package/dist/src/mcpl/channel-registry.js.map +1 -1
  32. package/dist/src/mcpl/conversation-router.d.ts +8 -6
  33. package/dist/src/mcpl/conversation-router.d.ts.map +1 -1
  34. package/dist/src/mcpl/conversation-router.js +12 -6
  35. package/dist/src/mcpl/conversation-router.js.map +1 -1
  36. package/dist/src/mcpl/index.d.ts +3 -0
  37. package/dist/src/mcpl/index.d.ts.map +1 -1
  38. package/dist/src/mcpl/index.js +3 -0
  39. package/dist/src/mcpl/index.js.map +1 -1
  40. package/dist/src/mcpl/push-coalescer.d.ts +318 -0
  41. package/dist/src/mcpl/push-coalescer.d.ts.map +1 -0
  42. package/dist/src/mcpl/push-coalescer.js +709 -0
  43. package/dist/src/mcpl/push-coalescer.js.map +1 -0
  44. package/dist/src/mcpl/push-handler.d.ts +7 -2
  45. package/dist/src/mcpl/push-handler.d.ts.map +1 -1
  46. package/dist/src/mcpl/push-handler.js +43 -4
  47. package/dist/src/mcpl/push-handler.js.map +1 -1
  48. package/dist/src/mcpl/references.d.ts.map +1 -1
  49. package/dist/src/mcpl/references.js +9 -4
  50. package/dist/src/mcpl/references.js.map +1 -1
  51. package/dist/src/mcpl/server-connection.d.ts +18 -0
  52. package/dist/src/mcpl/server-connection.d.ts.map +1 -1
  53. package/dist/src/mcpl/server-connection.js +44 -1
  54. package/dist/src/mcpl/server-connection.js.map +1 -1
  55. package/dist/src/mcpl/tool-classes.d.ts +71 -0
  56. package/dist/src/mcpl/tool-classes.d.ts.map +1 -0
  57. package/dist/src/mcpl/tool-classes.js +170 -0
  58. package/dist/src/mcpl/tool-classes.js.map +1 -0
  59. package/dist/src/mcpl/tool-glob.d.ts +16 -0
  60. package/dist/src/mcpl/tool-glob.d.ts.map +1 -0
  61. package/dist/src/mcpl/tool-glob.js +41 -0
  62. package/dist/src/mcpl/tool-glob.js.map +1 -0
  63. package/dist/src/mcpl/tool-lifecycle.d.ts +253 -0
  64. package/dist/src/mcpl/tool-lifecycle.d.ts.map +1 -0
  65. package/dist/src/mcpl/tool-lifecycle.js +607 -0
  66. package/dist/src/mcpl/tool-lifecycle.js.map +1 -0
  67. package/dist/src/mcpl/transport.d.ts +15 -0
  68. package/dist/src/mcpl/transport.d.ts.map +1 -1
  69. package/dist/src/mcpl/transport.js +65 -1
  70. package/dist/src/mcpl/transport.js.map +1 -1
  71. package/dist/src/mcpl/types.d.ts +68 -1
  72. package/dist/src/mcpl/types.d.ts.map +1 -1
  73. package/dist/src/mcpl/types.js +6 -0
  74. package/dist/src/mcpl/types.js.map +1 -1
  75. package/dist/src/modules/discord/index.js +1 -1
  76. package/dist/src/modules/discord/index.js.map +1 -1
  77. package/dist/src/modules/history/index.d.ts +85 -0
  78. package/dist/src/modules/history/index.d.ts.map +1 -1
  79. package/dist/src/modules/history/index.js +1019 -24
  80. package/dist/src/modules/history/index.js.map +1 -1
  81. package/dist/src/modules/history/semantic.d.ts +202 -0
  82. package/dist/src/modules/history/semantic.d.ts.map +1 -0
  83. package/dist/src/modules/history/semantic.js +418 -0
  84. package/dist/src/modules/history/semantic.js.map +1 -0
  85. package/dist/src/modules/index.d.ts +2 -1
  86. package/dist/src/modules/index.d.ts.map +1 -1
  87. package/dist/src/modules/index.js.map +1 -1
  88. package/dist/src/tool-result-guard.d.ts +82 -0
  89. package/dist/src/tool-result-guard.d.ts.map +1 -0
  90. package/dist/src/tool-result-guard.js +306 -0
  91. package/dist/src/tool-result-guard.js.map +1 -0
  92. package/dist/src/types/agent.d.ts +14 -0
  93. package/dist/src/types/agent.d.ts.map +1 -1
  94. package/dist/src/types/framework.d.ts +21 -0
  95. package/dist/src/types/framework.d.ts.map +1 -1
  96. package/dist/src/types/trace.d.ts +17 -0
  97. package/dist/src/types/trace.d.ts.map +1 -1
  98. package/dist/test/channel-registry-routing.test.js +59 -0
  99. package/dist/test/channel-registry-routing.test.js.map +1 -1
  100. package/dist/test/code-execution.test.js +139 -0
  101. package/dist/test/code-execution.test.js.map +1 -1
  102. package/dist/test/conversation-router.test.js +33 -19
  103. package/dist/test/conversation-router.test.js.map +1 -1
  104. package/dist/test/conversation-routing.test.js +89 -0
  105. package/dist/test/conversation-routing.test.js.map +1 -1
  106. package/dist/test/helpers/coalescing-fixture.d.ts +72 -0
  107. package/dist/test/helpers/coalescing-fixture.d.ts.map +1 -0
  108. package/dist/test/helpers/coalescing-fixture.js +145 -0
  109. package/dist/test/helpers/coalescing-fixture.js.map +1 -0
  110. package/dist/test/helpers/coalescing-kill-child.d.ts +2 -0
  111. package/dist/test/helpers/coalescing-kill-child.d.ts.map +1 -0
  112. package/dist/test/helpers/coalescing-kill-child.js +24 -0
  113. package/dist/test/helpers/coalescing-kill-child.js.map +1 -0
  114. package/dist/test/history-module-realstore.test.d.ts +2 -0
  115. package/dist/test/history-module-realstore.test.d.ts.map +1 -0
  116. package/dist/test/history-module-realstore.test.js +363 -0
  117. package/dist/test/history-module-realstore.test.js.map +1 -0
  118. package/dist/test/history-module-semantic.test.d.ts +2 -0
  119. package/dist/test/history-module-semantic.test.d.ts.map +1 -0
  120. package/dist/test/history-module-semantic.test.js +213 -0
  121. package/dist/test/history-module-semantic.test.js.map +1 -0
  122. package/dist/test/history-module-ux.test.d.ts +2 -0
  123. package/dist/test/history-module-ux.test.d.ts.map +1 -0
  124. package/dist/test/history-module-ux.test.js +304 -0
  125. package/dist/test/history-module-ux.test.js.map +1 -0
  126. package/dist/test/history-module.test.js +25 -1
  127. package/dist/test/history-module.test.js.map +1 -1
  128. package/dist/test/history-semantic-review-findings.test.d.ts +2 -0
  129. package/dist/test/history-semantic-review-findings.test.d.ts.map +1 -0
  130. package/dist/test/history-semantic-review-findings.test.js +338 -0
  131. package/dist/test/history-semantic-review-findings.test.js.map +1 -0
  132. package/dist/test/history-semantic-sync-converges.test.d.ts +2 -0
  133. package/dist/test/history-semantic-sync-converges.test.d.ts.map +1 -0
  134. package/dist/test/history-semantic-sync-converges.test.js +109 -0
  135. package/dist/test/history-semantic-sync-converges.test.js.map +1 -0
  136. package/dist/test/mcpl-capability-grant.test.js +3 -1
  137. package/dist/test/mcpl-capability-grant.test.js.map +1 -1
  138. package/dist/test/mcpl-child-env.test.d.ts +2 -0
  139. package/dist/test/mcpl-child-env.test.d.ts.map +1 -0
  140. package/dist/test/mcpl-child-env.test.js +158 -0
  141. package/dist/test/mcpl-child-env.test.js.map +1 -0
  142. package/dist/test/mcpl-coalescing.test.d.ts +2 -0
  143. package/dist/test/mcpl-coalescing.test.d.ts.map +1 -0
  144. package/dist/test/mcpl-coalescing.test.js +1050 -0
  145. package/dist/test/mcpl-coalescing.test.js.map +1 -0
  146. package/dist/test/mcpl-list-status.test.js +4 -0
  147. package/dist/test/mcpl-list-status.test.js.map +1 -1
  148. package/dist/test/mcpl-reference-fields.test.d.ts +2 -0
  149. package/dist/test/mcpl-reference-fields.test.d.ts.map +1 -0
  150. package/dist/test/mcpl-reference-fields.test.js +82 -0
  151. package/dist/test/mcpl-reference-fields.test.js.map +1 -0
  152. package/dist/test/mcpl-tool-class-listing.test.d.ts +2 -0
  153. package/dist/test/mcpl-tool-class-listing.test.d.ts.map +1 -0
  154. package/dist/test/mcpl-tool-class-listing.test.js +147 -0
  155. package/dist/test/mcpl-tool-class-listing.test.js.map +1 -0
  156. package/dist/test/mcpl-tool-classes.test.d.ts +7 -0
  157. package/dist/test/mcpl-tool-classes.test.d.ts.map +1 -0
  158. package/dist/test/mcpl-tool-classes.test.js +80 -0
  159. package/dist/test/mcpl-tool-classes.test.js.map +1 -0
  160. package/dist/test/mcpl-tool-lifecycle-e2e.test.d.ts +2 -0
  161. package/dist/test/mcpl-tool-lifecycle-e2e.test.d.ts.map +1 -0
  162. package/dist/test/mcpl-tool-lifecycle-e2e.test.js +237 -0
  163. package/dist/test/mcpl-tool-lifecycle-e2e.test.js.map +1 -0
  164. package/dist/test/mcpl-tool-lifecycle-emitter.test.d.ts +11 -0
  165. package/dist/test/mcpl-tool-lifecycle-emitter.test.d.ts.map +1 -0
  166. package/dist/test/mcpl-tool-lifecycle-emitter.test.js +307 -0
  167. package/dist/test/mcpl-tool-lifecycle-emitter.test.js.map +1 -0
  168. package/dist/test/mcpl-tool-lifecycle.test.d.ts +11 -0
  169. package/dist/test/mcpl-tool-lifecycle.test.d.ts.map +1 -0
  170. package/dist/test/mcpl-tool-lifecycle.test.js +270 -0
  171. package/dist/test/mcpl-tool-lifecycle.test.js.map +1 -0
  172. package/dist/test/silent-heartbeat.test.d.ts +2 -0
  173. package/dist/test/silent-heartbeat.test.d.ts.map +1 -0
  174. package/dist/test/silent-heartbeat.test.js +154 -0
  175. package/dist/test/silent-heartbeat.test.js.map +1 -0
  176. package/dist/test/tool-result-guard.test.d.ts +2 -0
  177. package/dist/test/tool-result-guard.test.d.ts.map +1 -0
  178. package/dist/test/tool-result-guard.test.js +1081 -0
  179. package/dist/test/tool-result-guard.test.js.map +1 -0
  180. package/dist/test/tools-observe-parser-conformance.test.d.ts +2 -0
  181. package/dist/test/tools-observe-parser-conformance.test.d.ts.map +1 -0
  182. package/dist/test/tools-observe-parser-conformance.test.js +129 -0
  183. package/dist/test/tools-observe-parser-conformance.test.js.map +1 -0
  184. package/dist/test/trunk-channel-routing.test.js +67 -0
  185. package/dist/test/trunk-channel-routing.test.js.map +1 -1
  186. package/dist/tsconfig.tsbuildinfo +1 -1
  187. package/package.json +2 -2
  188. package/src/agent.ts +128 -10
  189. package/src/api/types.ts +2 -0
  190. package/src/code-execution/py-runner.ts +34 -2
  191. package/src/code-execution/tool-definition.ts +46 -0
  192. package/src/framework.ts +1207 -80
  193. package/src/index.ts +13 -0
  194. package/src/mcpl/capability-grant.ts +54 -6
  195. package/src/mcpl/channel-registry.ts +115 -7
  196. package/src/mcpl/conversation-router.ts +12 -8
  197. package/src/mcpl/index.ts +29 -0
  198. package/src/mcpl/push-coalescer.ts +821 -0
  199. package/src/mcpl/push-handler.ts +40 -4
  200. package/src/mcpl/references.ts +11 -3
  201. package/src/mcpl/server-connection.ts +48 -2
  202. package/src/mcpl/tool-classes.ts +188 -0
  203. package/src/mcpl/tool-glob.ts +36 -0
  204. package/src/mcpl/tool-lifecycle.ts +745 -0
  205. package/src/mcpl/transport.ts +67 -1
  206. package/src/mcpl/types.ts +71 -1
  207. package/src/modules/discord/index.ts +1 -1
  208. package/src/modules/history/index.ts +1089 -24
  209. package/src/modules/history/semantic.ts +473 -0
  210. package/src/modules/index.ts +2 -1
  211. package/src/tool-result-guard.ts +318 -0
  212. package/src/types/agent.ts +15 -0
  213. package/src/types/framework.ts +23 -0
  214. package/src/types/trace.ts +17 -0
@@ -0,0 +1,318 @@
1
+ /**
2
+ * RFC-006 event coalescing (mcpl PR #5, revision 7): replace-if-unread, deferred
3
+ * rendering, and atomic retraction, for both delivery lanes.
4
+ *
5
+ * The design keeps content IN context. A plain occurrence is delivered through
6
+ * the ordinary channel/push path the moment it is admitted, exactly as today;
7
+ * the coalescer only remembers WHERE it landed. A later occurrence of the same
8
+ * subject edits or removes that message in place while it is still unread
9
+ * (above the agent's consumed watermark, not folded by compression), and
10
+ * appends once it has been read. Nothing is staged, journaled or capped: on
11
+ * any uncertainty (restart, branch switch, eviction) the host behaves as an
12
+ * append-only host would, which is what the RFC's principle 4 asks for.
13
+ *
14
+ * Only deferred batches (§5) wait outside context, because a notice has no
15
+ * model-visible content until `push/render` produces it.
16
+ *
17
+ * All host effects go through {@link CoalescerHost}; this class is pure
18
+ * bookkeeping and is unit-tested without a framework.
19
+ */
20
+ import type { McplContentBlock, PushEventResult } from './types.js';
21
+ export declare const PUSH_COALESCING_SUPPORT: {
22
+ readonly pushEvents: true;
23
+ readonly channelsIncoming: true;
24
+ readonly deferred: true;
25
+ readonly channelScopedPush: true;
26
+ readonly retryWindowMs: 3600000;
27
+ };
28
+ export declare const COALESCE_OUTCOMES: readonly ["first", "replaced", "appended", "retracted", "noted", "consumed"];
29
+ export type CoalesceOutcome = typeof COALESCE_OUTCOMES[number];
30
+ export declare class CoalesceError extends Error {
31
+ readonly field: string;
32
+ readonly code: number;
33
+ constructor(field: string, message: string, code?: number);
34
+ }
35
+ /** Where an unread occurrence currently lives. */
36
+ export interface CoalescingPlacement {
37
+ agent: string;
38
+ /** Stored message id in that agent's context manager. */
39
+ messageId?: string;
40
+ /** Entry id in the framework's deferred-write queue (turn-alive deferral). */
41
+ deferredId?: string;
42
+ }
43
+ export interface CoalescingScope {
44
+ kind: 'featureSet' | 'channel';
45
+ id: string;
46
+ }
47
+ /** One admitted occurrence, normalized from either lane. */
48
+ export interface CoalescedOccurrence<E = unknown> {
49
+ serverId: string;
50
+ /** Host-owned binding identity (§3.2): stable across reconnects to the same
51
+ * configured peer, different when the id is reassigned to another one. */
52
+ binding: string;
53
+ /** Declaring feature set (push lane), echoed in push/render. */
54
+ featureSet?: string;
55
+ scope: CoalescingScope;
56
+ key: string;
57
+ eventId: string;
58
+ timestamp: string;
59
+ retract: boolean;
60
+ deferred: boolean;
61
+ initial: boolean;
62
+ data?: unknown;
63
+ tags?: string[];
64
+ /** Wire content (fallback for deferred; the notice for retract). */
65
+ content: McplContentBlock[];
66
+ /** Stable platform identity for channel subjects (§3.1). */
67
+ identity?: {
68
+ messageId?: string;
69
+ author?: {
70
+ id: string;
71
+ name: string;
72
+ };
73
+ threadId?: string;
74
+ };
75
+ /** The event the ordinary path would have queued — the host delivers it. */
76
+ event: E;
77
+ /** Set by the coalescer: deliver into this agent only (§3.2 audience may
78
+ * narrow, never widen — a replacement or notice follows the prior delivery). */
79
+ deliverTo?: string;
80
+ }
81
+ export interface DeferredNotice {
82
+ eventId: string;
83
+ timestamp: string;
84
+ data?: unknown;
85
+ }
86
+ export interface PushRenderParams {
87
+ featureSet: string;
88
+ channelId?: string;
89
+ key: string;
90
+ eventId: string;
91
+ notices: DeferredNotice[];
92
+ dropped: number;
93
+ }
94
+ export interface PushRenderResult {
95
+ content: McplContentBlock[];
96
+ timestamp?: string;
97
+ }
98
+ interface DeferredBatch<E> {
99
+ /** Latest notice; its content is the fallback, its tags/event drive delivery. */
100
+ latest: CoalescedOccurrence<E>;
101
+ notices: DeferredNotice[];
102
+ dropped: number;
103
+ /** Set on a batch restored from a snapshot whose render may have started. */
104
+ noRender?: boolean;
105
+ }
106
+ interface Rendering<E> {
107
+ batch: DeferredBatch<E>;
108
+ cancelled: boolean;
109
+ done: Promise<void>;
110
+ }
111
+ interface SubjectState<E> {
112
+ history: 'none' | 'some' | 'unknown';
113
+ occupant?: {
114
+ eventId: string;
115
+ placement: CoalescingPlacement;
116
+ };
117
+ batch?: DeferredBatch<E>;
118
+ rendering?: Rendering<E>;
119
+ consumedEventId?: string;
120
+ identity?: CoalescedOccurrence['identity'];
121
+ /** Agent that received the last delivery; replacements and notices follow it. */
122
+ audienceAgent?: string;
123
+ touchedAt: number;
124
+ }
125
+ export interface CoalescingSnapshot {
126
+ version: 1;
127
+ receipts: Array<[string, {
128
+ result: PushEventResult;
129
+ at: number;
130
+ }]>;
131
+ subjects: Array<{
132
+ subject: string;
133
+ history: SubjectState<unknown>['history'];
134
+ consumedEventId?: string;
135
+ identity?: CoalescedOccurrence['identity'];
136
+ occupant?: {
137
+ eventId: string;
138
+ placement: CoalescingPlacement;
139
+ };
140
+ audienceAgent?: string;
141
+ batch?: {
142
+ latest: CoalescedOccurrence<unknown>;
143
+ notices: DeferredNotice[];
144
+ dropped: number;
145
+ rendering?: boolean;
146
+ };
147
+ }>;
148
+ }
149
+ /** One receipt, written synchronously at acceptance (bridges the snapshot throttle). */
150
+ export interface CoalescingReceiptRecord {
151
+ key: string;
152
+ result: PushEventResult;
153
+ at: number;
154
+ subject: string;
155
+ /** History the subject was created with, for a subject first seen by this receipt. */
156
+ born?: SubjectState<unknown>['history'];
157
+ /** The subject's pending batch AFTER this acceptance: the accepted work
158
+ * itself (so a crash before the snapshot flush cannot lose it while keeping
159
+ * the receipt that suppresses its retry), or `null` when the acceptance
160
+ * left no batch — a retraction or plain replacement must be recoverable
161
+ * too. Records replay in order, so the last one per subject wins. */
162
+ batch: {
163
+ latest: CoalescedOccurrence<unknown>;
164
+ notices: DeferredNotice[];
165
+ dropped: number;
166
+ } | null;
167
+ }
168
+ export interface CoalescerHost<E> {
169
+ /** True iff the placement still exists and no model request has included it. */
170
+ isUnread(placement: CoalescingPlacement): boolean;
171
+ /** False when the unread occurrence could not be removed (it may still be read). */
172
+ remove(placement: CoalescingPlacement): boolean;
173
+ /** Ordinary delivery of the event; undefined when routing delivered nowhere.
174
+ * A replacement is delivered as a fresh message after the unread prior was
175
+ * removed: it lands where a fresh event lands (§4.1 allows either place). */
176
+ deliver(occurrence: CoalescedOccurrence<E>, materialized?: McplContentBlock[], assemblingFor?: string): Promise<CoalescingPlacement | undefined>;
177
+ /** Queue a wake for a batch that has no model-visible content yet. */
178
+ wakeForBatch(occurrence: CoalescedOccurrence<E>): Promise<void>;
179
+ /** Withdraw unstarted wakes whose sole cause is this subject. */
180
+ cancelWake(subject: string): void;
181
+ /** The batch's audience still holds the authority it was admitted under. */
182
+ authorized(occurrence: CoalescedOccurrence<E>): boolean;
183
+ /** Agents that read this occurrence's delivery target (may spawn a fork). */
184
+ audience(occurrence: CoalescedOccurrence<E>): Promise<string[]>;
185
+ render(occurrence: CoalescedOccurrence<E>, params: PushRenderParams): Promise<PushRenderResult>;
186
+ audit(record: Record<string, unknown>): void;
187
+ /** Persist (throttled by the host); the thunk builds the snapshot lazily. */
188
+ save(snapshot: () => CoalescingSnapshot): void;
189
+ /** Persist the snapshot NOW (a render is about to start: that boundary must
190
+ * be recoverable before the RPC is issued, §3.2). Throws on failure. */
191
+ saveNow?(snapshot: CoalescingSnapshot): void;
192
+ /** Persist one receipt now, before the acceptance is acknowledged. Throws
193
+ * on failure, which fails the acceptance (the producer retries). */
194
+ recordReceipt?(record: CoalescingReceiptRecord): void;
195
+ /** Make every write so far DURABLE (fsync, group-committed by the host).
196
+ * Awaited before an acceptance is acknowledged and before push/render is
197
+ * issued: a kill after either boundary must find the receipt, the
198
+ * accepted work, and the render-start on disk. Rejects on failure. */
199
+ commit?(): Promise<void>;
200
+ /** Recovery: is this occurrence's content in a context (its durable
201
+ * delivery identity — subject + eventId in message metadata — is found
202
+ * there)? A published occurrence is read from recovery on (the watermark
203
+ * restarts at the head), whatever a stale record says about its history. */
204
+ wasPublished?(subject: string, eventId: string): boolean;
205
+ now?(): number;
206
+ }
207
+ export interface CoalescerOptions {
208
+ retryWindowMs?: number;
209
+ /** Base delay before re-waking a batch whose render-start commit failed
210
+ * (doubles per attempt, capped at 60× base). Default 1 s. */
211
+ recoveryBackoffMs?: number;
212
+ maxSubjects?: number;
213
+ maxNotices?: number;
214
+ maxContentBytes?: number;
215
+ maxDataBytes?: number;
216
+ }
217
+ export declare function coalescingSubjectKey(serverId: string, binding: string, scope: CoalescingScope, key: string): string;
218
+ export declare function coalescingReceiptKey(serverId: string, binding: string, eventId: string): string;
219
+ /** Validate wire content before it is stored, rendered or converted. */
220
+ export declare function validateCoalescedContent(content: unknown, maxBytes?: number): asserts content is McplContentBlock[];
221
+ /** Validate the `coalesce` member of either lane (§13). */
222
+ export declare function validateCoalesceMember(c: unknown, lane: 'push' | 'channel', maxDataBytes?: number): asserts c is NonNullable<import('./types.js').PushEventParams['coalesce']>;
223
+ export declare class PushCoalescer<E = unknown> {
224
+ private readonly host;
225
+ private readonly options;
226
+ private readonly subjects;
227
+ /** `committed`: the durability barrier succeeded after this receipt was
228
+ * written. An uncommitted receipt deduplicates effects but is not an
229
+ * acknowledgement until a commit succeeds. */
230
+ private readonly receipts;
231
+ private readonly retryWindowMs;
232
+ private readonly maxSubjects;
233
+ private readonly maxNotices;
234
+ /** Outstanding push/render RPCs per server, including cancelled ones (§10.7). */
235
+ private readonly rendersInFlight;
236
+ /**
237
+ * The ONE critical section. Every state transition — an admission, the
238
+ * freezing of a batch for rendering, the settlement of a render's result —
239
+ * runs under it, awaits included (fork spawn, durability commit, delivery).
240
+ * Only the push/render RPC itself runs outside, so it stays cancellable.
241
+ * An admission therefore never observes a half-done assembly transition,
242
+ * and an assembly never acts on a batch an admission moved on from.
243
+ */
244
+ private serial;
245
+ /** Liveness after a failed freeze: a bounded, backed-off re-wake per
246
+ * subject. `attempt` outlives the armed timer: it counts consecutive
247
+ * failures and resets only on success or disposal of the pending work. */
248
+ private readonly recovery;
249
+ private suspended;
250
+ constructor(host: CoalescerHost<E>, options?: CoalescerOptions);
251
+ private now;
252
+ private locked;
253
+ /** The unread occupant's or batch's placement/identity, for routing continuity. */
254
+ identity(subject: string): CoalescedOccurrence['identity'] | undefined;
255
+ pendingBatches(): number;
256
+ /** §10.7: a server handling push/render must not issue inference/request —
257
+ * including a render whose batch was cancelled while the RPC is outstanding. */
258
+ isRendering(serverId: string): boolean;
259
+ receipt(serverId: string, binding: string, eventId: string): PushEventResult | undefined;
260
+ private receiptEntry;
261
+ /** Acknowledge a receipt: durable first. A receipt whose commit failed is
262
+ * committed now (no effects are repeated); if that fails again, so does
263
+ * the acknowledgement, and the producer retries once more. */
264
+ private acknowledge;
265
+ /**
266
+ * Admit an occurrence that has already passed the lane's ordinary checks and
267
+ * the coalesce-member validation. Returns the wire result, performing the
268
+ * host effects (replace / remove / deliver) synchronously so a create, edit
269
+ * and delete cannot overtake one another.
270
+ */
271
+ accept(occurrence: CoalescedOccurrence<E>): Promise<PushEventResult>;
272
+ private admit;
273
+ private subjectFor;
274
+ /** Re-check the occupant against the host: consumed occupants become history. */
275
+ private refreshOccupant;
276
+ private scheduleRecovery;
277
+ /** Disarm the timer only (a producer retry wakes now; the count stands). */
278
+ private disarmRecovery;
279
+ /** Success or disposal of the pending work: forget the failure series. */
280
+ private clearRecovery;
281
+ private dropBatches;
282
+ private retract;
283
+ private acceptPlain;
284
+ private acceptDeferred;
285
+ /**
286
+ * Render and materialize every pending batch whose audience includes
287
+ * `agentName`. Called by the host at a turn's assembly boundary, before the
288
+ * compile. Bounded by the host's render timeout; a failed or late render
289
+ * materializes the admitted fallback (§5.3).
290
+ */
291
+ assemble(agentName: string): Promise<void>;
292
+ /**
293
+ * Under the lock: decide whether `subject`'s batch renders for `agentName`,
294
+ * freeze it, make the render-start durable, re-check authority, and start
295
+ * the RPC. Returns the render's completion (shared by every assembly that
296
+ * waits on this batch, vector 25) or undefined when nothing was started.
297
+ */
298
+ private freeze;
299
+ private render;
300
+ restore(snapshot: CoalescingSnapshot | null | undefined): void;
301
+ /**
302
+ * A batch restored as RENDERING may already have been published (its
303
+ * result or fallback is a durable context message; only the completion
304
+ * snapshot was lost). Recovery then marks it consumed instead of
305
+ * delivering the fallback a second time.
306
+ */
307
+ private settlePublished;
308
+ /** Apply receipts written since the last snapshot (crash-window bridge). */
309
+ restoreReceipts(records: CoalescingReceiptRecord[]): void;
310
+ snapshot(): CoalescingSnapshot;
311
+ /** Batches whose wake should be re-queued after a restart (their fallback is pending). */
312
+ pendingBatchOccurrences(): CoalescedOccurrence<E>[];
313
+ suspend(): void;
314
+ private persist;
315
+ private prune;
316
+ }
317
+ export {};
318
+ //# sourceMappingURL=push-coalescer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-coalescer.d.ts","sourceRoot":"","sources":["../../../src/mcpl/push-coalescer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAEpE,eAAO,MAAM,uBAAuB;;;;;;CAG1B,CAAC;AAEX,eAAO,MAAM,iBAAiB,8EAA+E,CAAC;AAC9G,MAAM,MAAM,eAAe,GAAG,OAAO,iBAAiB,CAAC,MAAM,CAAC,CAAC;AAI/D,qBAAa,aAAc,SAAQ,KAAK;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM;IAAmB,QAAQ,CAAC,IAAI;gBAA7C,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAW,IAAI,SAAS;CAI5E;AAED,kDAAkD;AAClD,MAAM,WAAW,mBAAmB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAAG,IAAI,EAAE,YAAY,GAAG,SAAS,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE;AAE/E,4DAA4D;AAC5D,MAAM,WAAW,mBAAm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