@copilotkit/runtime 2.0.0-rc.60 → 2.0.0-rc.62

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 (84) hide show
  1. package/dist/agent.cjs +1 -0
  2. package/dist/agent.cjs.map +1 -1
  3. package/dist/agent.d.cts +10 -0
  4. package/dist/agent.d.cts.map +1 -1
  5. package/dist/agent.d.mts +10 -0
  6. package/dist/agent.d.mts.map +1 -1
  7. package/dist/agent.mjs +1 -0
  8. package/dist/agent.mjs.map +1 -1
  9. package/dist/index.cjs +4 -0
  10. package/dist/index.d.cts +4 -3
  11. package/dist/index.d.mts +4 -3
  12. package/dist/index.mjs +3 -2
  13. package/dist/runtime/core/channel-manager.cjs +51 -162
  14. package/dist/runtime/core/channel-manager.cjs.map +1 -1
  15. package/dist/runtime/core/channel-manager.d.cts +1 -1
  16. package/dist/runtime/core/channel-manager.d.cts.map +1 -1
  17. package/dist/runtime/core/channel-manager.d.mts +1 -1
  18. package/dist/runtime/core/channel-manager.d.mts.map +1 -1
  19. package/dist/runtime/core/channel-manager.mjs +51 -162
  20. package/dist/runtime/core/channel-manager.mjs.map +1 -1
  21. package/dist/runtime/core/fetch-cors.cjs +4 -2
  22. package/dist/runtime/core/fetch-cors.cjs.map +1 -1
  23. package/dist/runtime/core/fetch-cors.mjs +4 -2
  24. package/dist/runtime/core/fetch-cors.mjs.map +1 -1
  25. package/dist/runtime/core/package-info.cjs +1 -1
  26. package/dist/runtime/core/package-info.mjs +1 -1
  27. package/dist/runtime/core/runtime.cjs +3 -1
  28. package/dist/runtime/core/runtime.cjs.map +1 -1
  29. package/dist/runtime/core/runtime.d.cts +36 -7
  30. package/dist/runtime/core/runtime.d.cts.map +1 -1
  31. package/dist/runtime/core/runtime.d.mts +36 -7
  32. package/dist/runtime/core/runtime.d.mts.map +1 -1
  33. package/dist/runtime/core/runtime.mjs +3 -1
  34. package/dist/runtime/core/runtime.mjs.map +1 -1
  35. package/dist/runtime/core/thread-run.cjs +459 -0
  36. package/dist/runtime/core/thread-run.cjs.map +1 -0
  37. package/dist/runtime/core/thread-run.d.cts +201 -0
  38. package/dist/runtime/core/thread-run.d.cts.map +1 -0
  39. package/dist/runtime/core/thread-run.d.mts +201 -0
  40. package/dist/runtime/core/thread-run.d.mts.map +1 -0
  41. package/dist/runtime/core/thread-run.mjs +456 -0
  42. package/dist/runtime/core/thread-run.mjs.map +1 -0
  43. package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs +6 -7
  44. package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs.map +1 -1
  45. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.cts.map +1 -1
  46. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.mts.map +1 -1
  47. package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs +6 -7
  48. package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs.map +1 -1
  49. package/dist/runtime/handlers/get-runtime-info.cjs +2 -1
  50. package/dist/runtime/handlers/get-runtime-info.cjs.map +1 -1
  51. package/dist/runtime/handlers/get-runtime-info.mjs +2 -1
  52. package/dist/runtime/handlers/get-runtime-info.mjs.map +1 -1
  53. package/dist/runtime/handlers/intelligence/run.cjs +0 -1
  54. package/dist/runtime/handlers/intelligence/run.cjs.map +1 -1
  55. package/dist/runtime/handlers/intelligence/run.mjs +0 -1
  56. package/dist/runtime/handlers/intelligence/run.mjs.map +1 -1
  57. package/dist/runtime/handlers/intelligence/thread-names.cjs +43 -70
  58. package/dist/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
  59. package/dist/runtime/handlers/intelligence/thread-names.mjs +43 -70
  60. package/dist/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
  61. package/dist/runtime/index.d.cts +3 -2
  62. package/dist/runtime/index.d.mts +3 -2
  63. package/dist/runtime/intelligence-platform/client.cjs +20 -2
  64. package/dist/runtime/intelligence-platform/client.cjs.map +1 -1
  65. package/dist/runtime/intelligence-platform/client.d.cts +13 -0
  66. package/dist/runtime/intelligence-platform/client.d.cts.map +1 -1
  67. package/dist/runtime/intelligence-platform/client.d.mts +13 -0
  68. package/dist/runtime/intelligence-platform/client.d.mts.map +1 -1
  69. package/dist/runtime/intelligence-platform/client.mjs +20 -2
  70. package/dist/runtime/intelligence-platform/client.mjs.map +1 -1
  71. package/dist/runtime/runner/index.d.cts +1 -1
  72. package/dist/runtime/runner/index.d.mts +1 -1
  73. package/dist/runtime/runner/index.mjs +1 -1
  74. package/dist/runtime/runner/intelligence.cjs +107 -23
  75. package/dist/runtime/runner/intelligence.cjs.map +1 -1
  76. package/dist/runtime/runner/intelligence.d.cts +51 -2
  77. package/dist/runtime/runner/intelligence.d.cts.map +1 -1
  78. package/dist/runtime/runner/intelligence.d.mts +51 -2
  79. package/dist/runtime/runner/intelligence.d.mts.map +1 -1
  80. package/dist/runtime/runner/intelligence.mjs +108 -25
  81. package/dist/runtime/runner/intelligence.mjs.map +1 -1
  82. package/dist/v2/index.d.cts +18 -0
  83. package/dist/v2/index.d.mts +18 -0
  84. package/package.json +9 -9
@@ -21,6 +21,45 @@ interface IntelligenceAgentRunnerOptions {
21
21
  heartbeatIntervalMs?: number;
22
22
  /** Where the runner writes its logs. Default: the console. */
23
23
  logger?: CopilotKitRuntimeLogger;
24
+ /**
25
+ * Called each time Intelligence acknowledges a batch of runner events.
26
+ * Use it to watch the delivery queue: a growing `pendingEvents` or
27
+ * `oldestPendingAgeMs` means events are produced faster than Intelligence
28
+ * accepts them. Errors thrown by the callback are logged and ignored.
29
+ */
30
+ onEventDeliveryStats?: (stats: RunnerEventDeliveryStats) => void;
31
+ /**
32
+ * Most events one run may hold while they wait for Intelligence to
33
+ * acknowledge them. A run whose agent outpaces Intelligence long enough to
34
+ * exceed it fails with `RunnerEventQueueFullError` instead of growing the
35
+ * queue without bound. The default is far above what a healthy gateway
36
+ * leaves pending; a stalled one fails the run on the durability deadline
37
+ * first.
38
+ * @default 50_000
39
+ */
40
+ maxPendingEvents?: number;
41
+ }
42
+ /**
43
+ * A run produced events faster than Intelligence acknowledged them until
44
+ * `maxPendingEvents` were waiting. The run is failed and its agent aborted.
45
+ */
46
+ declare class RunnerEventQueueFullError extends Error {
47
+ readonly threadId: string;
48
+ readonly runId: string;
49
+ readonly maxPendingEvents: number;
50
+ constructor(threadId: string, runId: string, maxPendingEvents: number);
51
+ }
52
+ interface RunnerEventDeliveryStats {
53
+ threadId: string;
54
+ runId: string;
55
+ /** Events in the batch Intelligence just acknowledged. */
56
+ acknowledgedEvents: number;
57
+ /** Time from sending the batch to its acknowledgement. */
58
+ ackLatencyMs: number;
59
+ /** Events still waiting for acknowledgement. */
60
+ pendingEvents: number;
61
+ /** Age of the oldest event still waiting, or 0 when none is. */
62
+ oldestPendingAgeMs: number;
24
63
  }
25
64
  interface RunnerStartupBoundary {
26
65
  events: Observable<BaseEvent>;
@@ -79,9 +118,19 @@ declare class IntelligenceAgentRunner extends AgentRunner {
79
118
  stop(request: AgentRunnerStopRequest): Promise<boolean | undefined>;
80
119
  private stopRun;
81
120
  private executeAgentRun;
82
- /** Queue one immutable event payload until Redis-backed gateway acknowledgement. */
121
+ /**
122
+ * Queue one immutable event payload until Redis-backed gateway acknowledgement.
123
+ *
124
+ * `pushCanonicalEvent` is the only producer and stamps a fresh, increasing
125
+ * `cpki_event_seq` on every event it queues, so the map's insertion order is
126
+ * sequence order. Batches are therefore read from the front of the map
127
+ * instead of sorting the whole queue on every event.
128
+ */
83
129
  private queueRunnerEvent;
130
+ /** The oldest `limit` pending events, in sequence order. */
131
+ private oldestPendingEvents;
84
132
  private pushPendingEventBatch;
133
+ private reportEventDelivery;
85
134
  private handlePendingEventFailure;
86
135
  private schedulePendingEventRetry;
87
136
  private clearPendingEventRetry;
@@ -111,5 +160,5 @@ declare class IntelligenceAgentRunner extends AgentRunner {
111
160
  private removeThread;
112
161
  }
113
162
  //#endregion
114
- export { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS };
163
+ export { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerEventDeliveryStats, RunnerEventQueueFullError, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS };
115
164
  //# sourceMappingURL=intelligence.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"intelligence.d.cts","names":[],"sources":["../../../src/runtime/runner/intelligence.ts"],"mappings":";;;;;;UAqBiB,8BAAA;EAA8B;EAE7C,GAAA;EAcgC;EAZhC,SAAA;EAAA;EAEA,cAAA;EAEA;EAAA,WAAA;EAQA;;;;AAGF;EALE,mBAAA;;EAEA,MAAA,GAAS,uBAAA;AAAA;AAAA,UAGM,qBAAA;EACf,MAAA,EAAQ,UAAA,CAAW,SAAA;EACnB,OAAA,EAAS,OAAA;AAAA;;;;;;cAqDE,uBAAA;AAAA,cAEA,uBAAA,SAAgC,WAAA;EAAA,QACnC,OAAA;EAAA,QACA,OAAA;EAAA,iBACS,MAAA;cAEL,OAAA,EAAS,8BAAA;EALV;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAA,QAuCH,YAAA;EAAA,QAiBA,wBAAA;EAAA,QAqBA,0BAAA;EAmBA;EAAA,QARA,yBAAA;EAAA,QAQA,mBAAA;EA4BR,GAAA,CAAI,OAAA,EAAS,qBAAA,GAAwB,UAAA,CAAW,SAAA;EAIhD,sBAAA,CACE,OAAA,EAAS,qBAAA,GACR,qBAAA;EAAA,QAiBK,mBAAA;EAoPR,OAAA,CAAQ,OAAA,EAAS,yBAAA,GAA4B,UAAA,CAAW,SAAA;EA4CxD,SAAA,CAAU,OAAA,EAAS,2BAAA,GAA8B,OAAA;EAlT/C;EAwTF,IAAA,CAAK,OAAA,EAAS,sBAAA,GAAyB,OAAA;EAAA,QAI/B,OAAA;EAAA,QAwEM,eAAA;EA9HG;EAAA,QAmQT,gBAAA;EAAA,QAkBA,qBAAA;EAAA,QAqDA,yBAAA;EAAA,QAgCA,yBAAA;EAAA,QA0BA,sBAAA;EAAA,QAOA,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QAqBA,0BAAA;EAqDL;EAAA,QA1CK,yBAAA;EAAA,QAQA,mBAAA;EA4BR,GAAA,CAAI,OAAA,EAAS,qBAAA,GAAwB,UAAA,CAAW,SAAA;EAIhD,sBAAA,CACE,OAAA,EAAS,qBAAA,GACR,qBAAA;EAAA,QAiBK,mBAAA;EAsPR,OAAA,CAAQ,OAAA,EAAS,yBAAA,GAA4B,UAAA,CAAW,SAAA;EA4CxD,SAAA,CAAU,OAAA,EAAS,2BAAA,GAA8B,OAAA;EAA9B;EAMnB,IAAA,CAAK,OAAA,EAAS,sBAAA,GAAyB,OAAA;EAAA,QAI/B,OAAA;EAAA,QAwEM,eAAA;EA5EA;;;;;;;;EAAA,QAuNN,gBAAA;EAkLA;EAAA,QA1HA,mBAAA;EAAA,QA0BA,qBAAA;EAAA,QA+DA,mBAAA;EAAA,QAiCA,yBAAA;EAAA,QAgCA,yBAAA;EAAA,QA0BA,sBAAA;EAAA,QAOA,yBAAA;EAAA,QAwBA,sBAAA;EAAA,QAOA,sBAAA;EAAA,QAOA,mBAAA;EAAA,QAgCA,sBAAA;EAAA,QAiBA,qBAAA;EAAA,QA8BA,mBAAA;EAAA,QAgBA,aAAA;EAAA,QAKA,cAAA;EAAA,QAKA,wBAAA;EAAA,QAWA,oBAAA;EAAA,QAWA,uBAAA;EAAA,QAQA,qBAAA;EAAA,QA4BA,UAAA;EAAA,QAaA,2BAAA;EAAA,QAeA,oBAAA;;;;;;;;UAWA,YAAA;AAAA"}
@@ -21,6 +21,45 @@ interface IntelligenceAgentRunnerOptions {
21
21
  heartbeatIntervalMs?: number;
22
22
  /** Where the runner writes its logs. Default: the console. */
23
23
  logger?: CopilotKitRuntimeLogger;
24
+ /**
25
+ * Called each time Intelligence acknowledges a batch of runner events.
26
+ * Use it to watch the delivery queue: a growing `pendingEvents` or
27
+ * `oldestPendingAgeMs` means events are produced faster than Intelligence
28
+ * accepts them. Errors thrown by the callback are logged and ignored.
29
+ */
30
+ onEventDeliveryStats?: (stats: RunnerEventDeliveryStats) => void;
31
+ /**
32
+ * Most events one run may hold while they wait for Intelligence to
33
+ * acknowledge them. A run whose agent outpaces Intelligence long enough to
34
+ * exceed it fails with `RunnerEventQueueFullError` instead of growing the
35
+ * queue without bound. The default is far above what a healthy gateway
36
+ * leaves pending; a stalled one fails the run on the durability deadline
37
+ * first.
38
+ * @default 50_000
39
+ */
40
+ maxPendingEvents?: number;
41
+ }
42
+ /**
43
+ * A run produced events faster than Intelligence acknowledged them until
44
+ * `maxPendingEvents` were waiting. The run is failed and its agent aborted.
45
+ */
46
+ declare class RunnerEventQueueFullError extends Error {
47
+ readonly threadId: string;
48
+ readonly runId: string;
49
+ readonly maxPendingEvents: number;
50
+ constructor(threadId: string, runId: string, maxPendingEvents: number);
51
+ }
52
+ interface RunnerEventDeliveryStats {
53
+ threadId: string;
54
+ runId: string;
55
+ /** Events in the batch Intelligence just acknowledged. */
56
+ acknowledgedEvents: number;
57
+ /** Time from sending the batch to its acknowledgement. */
58
+ ackLatencyMs: number;
59
+ /** Events still waiting for acknowledgement. */
60
+ pendingEvents: number;
61
+ /** Age of the oldest event still waiting, or 0 when none is. */
62
+ oldestPendingAgeMs: number;
24
63
  }
25
64
  interface RunnerStartupBoundary {
26
65
  events: Observable<BaseEvent>;
@@ -79,9 +118,19 @@ declare class IntelligenceAgentRunner extends AgentRunner {
79
118
  stop(request: AgentRunnerStopRequest): Promise<boolean | undefined>;
80
119
  private stopRun;
81
120
  private executeAgentRun;
82
- /** Queue one immutable event payload until Redis-backed gateway acknowledgement. */
121
+ /**
122
+ * Queue one immutable event payload until Redis-backed gateway acknowledgement.
123
+ *
124
+ * `pushCanonicalEvent` is the only producer and stamps a fresh, increasing
125
+ * `cpki_event_seq` on every event it queues, so the map's insertion order is
126
+ * sequence order. Batches are therefore read from the front of the map
127
+ * instead of sorting the whole queue on every event.
128
+ */
83
129
  private queueRunnerEvent;
130
+ /** The oldest `limit` pending events, in sequence order. */
131
+ private oldestPendingEvents;
84
132
  private pushPendingEventBatch;
133
+ private reportEventDelivery;
85
134
  private handlePendingEventFailure;
86
135
  private schedulePendingEventRetry;
87
136
  private clearPendingEventRetry;
@@ -111,5 +160,5 @@ declare class IntelligenceAgentRunner extends AgentRunner {
111
160
  private removeThread;
112
161
  }
113
162
  //#endregion
114
- export { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS };
163
+ export { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerEventDeliveryStats, RunnerEventQueueFullError, RunnerStartupBoundary, STOP_RECORD_DEADLINE_MS };
115
164
  //# sourceMappingURL=intelligence.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"intelligence.d.mts","names":[],"sources":["../../../src/runtime/runner/intelligence.ts"],"mappings":";;;;;;UAqBiB,8BAAA;EAA8B;EAE7C,GAAA;EAcgC;EAZhC,SAAA;EAAA;EAEA,cAAA;EAEA;EAAA,WAAA;EAQA;;;;AAGF;EALE,mBAAA;;EAEA,MAAA,GAAS,uBAAA;AAAA;AAAA,UAGM,qBAAA;EACf,MAAA,EAAQ,UAAA,CAAW,SAAA;EACnB,OAAA,EAAS,OAAA;AAAA;;;;;;cAqDE,uBAAA;AAAA,cAEA,uBAAA,SAAgC,WAAA;EAAA,QACnC,OAAA;EAAA,QACA,OAAA;EAAA,iBACS,MAAA;cAEL,OAAA,EAAS,8BAAA;EALV;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAA,QAuCH,YAAA;EAAA,QAiBA,wBAAA;EAAA,QAqBA,0BAAA;EAmBA;EAAA,QARA,yBAAA;EAAA,QAQA,mBAAA;EA4BR,GAAA,CAAI,OAAA,EAAS,qBAAA,GAAwB,UAAA,CAAW,SAAA;EAIhD,sBAAA,CACE,OAAA,EAAS,qBAAA,GACR,qBAAA;EAAA,QAiBK,mBAAA;EAoPR,OAAA,CAAQ,OAAA,EAAS,yBAAA,GAA4B,UAAA,CAAW,SAAA;EA4CxD,SAAA,CAA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
  import { AgentRunner, ThreadAlreadyRunningError } from "./agent-runner.mjs";
3
- import { AG_UI_CHANNEL_EVENT, finalizeRunEvents, phoenixExponentialBackoff, stripIntelligenceRoutingFields } from "@copilotkit/shared";
3
+ import { AG_UI_CHANNEL_EVENT, createRunEventFinalizer, phoenixExponentialBackoff, stripIntelligenceRoutingFields } from "@copilotkit/shared";
4
4
  import { Socket } from "phoenix";
5
5
  import { Observable } from "rxjs";
6
6
  import { EventType } from "@ag-ui/client";
7
7
 
8
8
  //#region src/runtime/runner/intelligence.ts
9
+ /**
10
+ * A run produced events faster than Intelligence acknowledged them until
11
+ * `maxPendingEvents` were waiting. The run is failed and its agent aborted.
12
+ */
13
+ var RunnerEventQueueFullError = class extends Error {
14
+ constructor(threadId, runId, maxPendingEvents) {
15
+ super(`Run ${runId} on thread ${threadId} has ${maxPendingEvents} events waiting for Intelligence to acknowledge them, the most a run may hold. The agent produces events faster than Intelligence accepts them.`);
16
+ this.name = "RunnerEventQueueFullError";
17
+ this.threadId = threadId;
18
+ this.runId = runId;
19
+ this.maxPendingEvents = maxPendingEvents;
20
+ }
21
+ };
9
22
  const MAX_CONSECUTIVE_SOCKET_ERRORS = 5;
10
23
  const EVENT_RETRY_BASE_MS = 100;
11
24
  const EVENT_RETRY_MAX_MS = 2e3;
@@ -13,6 +26,7 @@ const RUNNER_EVENT_BATCH_CAPABILITY = "runner_event_batch_v1";
13
26
  const MAX_RUNNER_EVENT_BATCH_SIZE = 32;
14
27
  const RUNNER_EVENT_BATCH_FLUSH_MS = 5;
15
28
  const EVENT_DURABILITY_DEADLINE_MS = 6e4;
29
+ const DEFAULT_MAX_PENDING_EVENTS = 5e4;
16
30
  /**
17
31
  * How long a runtime-side stop waits for Intelligence to record it before the
18
32
  * terminal event is sent anyway. The agent is aborted at once; only the
@@ -153,7 +167,8 @@ var IntelligenceAgentRunner = class extends AgentRunner {
153
167
  isRunning: true,
154
168
  stopRequested: false,
155
169
  agent,
156
- currentEvents: [],
170
+ finalizer: createRunEventFinalizer(),
171
+ runErrorEmitted: false,
157
172
  nextEventSeq: 1,
158
173
  hasRunStarted: false,
159
174
  hasJoined: false,
@@ -167,6 +182,9 @@ var IntelligenceAgentRunner = class extends AgentRunner {
167
182
  ...request.recordStop ? { recordStop: request.recordStop } : {},
168
183
  stopRecorded: null,
169
184
  pendingEvents: /* @__PURE__ */ new Map(),
185
+ durableProgressAt: 0,
186
+ lastQueuedEventSeq: 0,
187
+ pendingEventsOutOfOrder: false,
170
188
  activeEventBatch: null,
171
189
  nextEventPushAttempt: 0,
172
190
  eventRetryTimer: null,
@@ -256,7 +274,6 @@ var IntelligenceAgentRunner = class extends AgentRunner {
256
274
  code: "CHANNEL_JOIN_ERROR"
257
275
  };
258
276
  observer.next(errorEvent);
259
- state.currentEvents.push(errorEvent);
260
277
  this.removeThread(threadId, state);
261
278
  startupBoundary?.rejectStartup(error);
262
279
  observer.complete();
@@ -270,7 +287,6 @@ var IntelligenceAgentRunner = class extends AgentRunner {
270
287
  code: "CHANNEL_JOIN_TIMEOUT"
271
288
  };
272
289
  observer.next(errorEvent);
273
- state.currentEvents.push(errorEvent);
274
290
  this.removeThread(threadId, state);
275
291
  startupBoundary?.rejectStartup(error);
276
292
  observer.complete();
@@ -349,12 +365,12 @@ var IntelligenceAgentRunner = class extends AgentRunner {
349
365
  });
350
366
  }
351
367
  async executeAgentRun(request, state, threadId, notifyHandler) {
352
- const { currentEvents } = state;
353
368
  const pushCanonicalEvent = (event) => {
354
369
  if (!this.isCurrentThreadState(threadId, state)) return;
355
370
  const canonicalEvent = this.stampRunnerMetadata(this.stampCanonicalRunOwnership(this.withoutAgentEventIdentity(event), request), state);
356
- currentEvents.push(canonicalEvent);
371
+ state.finalizer.observe(canonicalEvent);
357
372
  if (canonicalEvent.type === EventType.RUN_STARTED) state.hasRunStarted = true;
373
+ if (canonicalEvent.type === EventType.RUN_ERROR) state.runErrorEmitted = true;
358
374
  this.queueRunnerEvent(this.createRunnerEventPayload(canonicalEvent, request, state), state);
359
375
  if (canonicalEvent.type === EventType.RUN_STARTED || canonicalEvent.type === EventType.RUN_ERROR) notifyHandler(canonicalEvent);
360
376
  };
@@ -399,7 +415,7 @@ var IntelligenceAgentRunner = class extends AgentRunner {
399
415
  } catch (error) {
400
416
  if (state.stopRequested || !this.isCurrentThreadState(threadId, state)) return;
401
417
  ensureRunStarted();
402
- if (!currentEvents.find((event) => event.type === EventType.RUN_ERROR)) pushCanonicalEvent({
418
+ if (!state.runErrorEmitted) pushCanonicalEvent({
403
419
  type: EventType.RUN_ERROR,
404
420
  message: error instanceof Error ? error.message : String(error)
405
421
  });
@@ -407,7 +423,7 @@ var IntelligenceAgentRunner = class extends AgentRunner {
407
423
  if (state.stopRecorded) await state.stopRecorded;
408
424
  if (!this.isCurrentThreadState(threadId, state)) return;
409
425
  ensureRunStarted();
410
- const appended = finalizeRunEvents(currentEvents, {
426
+ const appended = state.finalizer.finalize({
411
427
  stopRequested: state.stopRequested,
412
428
  protocolVersion: request.input.protocolVersion
413
429
  });
@@ -416,20 +432,61 @@ var IntelligenceAgentRunner = class extends AgentRunner {
416
432
  this.completeWhenDurable(threadId, state);
417
433
  }
418
434
  }
419
- /** Queue one immutable event payload until Redis-backed gateway acknowledgement. */
435
+ /**
436
+ * Queue one immutable event payload until Redis-backed gateway acknowledgement.
437
+ *
438
+ * `pushCanonicalEvent` is the only producer and stamps a fresh, increasing
439
+ * `cpki_event_seq` on every event it queues, so the map's insertion order is
440
+ * sequence order. Batches are therefore read from the front of the map
441
+ * instead of sorting the whole queue on every event.
442
+ */
420
443
  queueRunnerEvent(payload, state) {
421
444
  if (!this.isCurrentThreadState(state.threadId, state)) return;
422
445
  const eventId = this.runnerEventId(payload);
423
- if (!state.pendingEvents.has(eventId)) state.pendingEvents.set(eventId, {
424
- payload: structuredClone(payload),
425
- queuedAt: Date.now()
426
- });
427
- this.scheduleEventDeadline(state);
446
+ if (!state.pendingEvents.has(eventId)) {
447
+ const maxPendingEvents = this.options.maxPendingEvents ?? DEFAULT_MAX_PENDING_EVENTS;
448
+ if (state.pendingEvents.size >= maxPendingEvents) {
449
+ this.failThread(state.threadId, state, new RunnerEventQueueFullError(state.threadId, state.runId, maxPendingEvents));
450
+ return;
451
+ }
452
+ const queuedAt = Date.now();
453
+ const wasEmpty = state.pendingEvents.size === 0;
454
+ const eventSeq = this.runnerEventSeq(payload);
455
+ if (eventSeq < state.lastQueuedEventSeq && !state.pendingEventsOutOfOrder) {
456
+ state.pendingEventsOutOfOrder = true;
457
+ this.logger.warn({
458
+ threadId: state.threadId,
459
+ runId: state.runId,
460
+ eventSeq,
461
+ lastQueuedEventSeq: state.lastQueuedEventSeq
462
+ }, "Runner event queued out of sequence order; sorting pending events");
463
+ }
464
+ state.lastQueuedEventSeq = Math.max(state.lastQueuedEventSeq, eventSeq);
465
+ state.pendingEvents.set(eventId, {
466
+ payload: structuredClone(payload),
467
+ queuedAt
468
+ });
469
+ if (wasEmpty) {
470
+ state.durableProgressAt = queuedAt;
471
+ this.scheduleEventDeadline(state);
472
+ }
473
+ }
428
474
  this.replayPendingEvents(state);
429
475
  }
476
+ /** The oldest `limit` pending events, in sequence order. */
477
+ oldestPendingEvents(state, limit) {
478
+ if (state.pendingEventsOutOfOrder) return [...state.pendingEvents.entries()].sort(([, left], [, right]) => this.runnerEventSeq(left.payload) - this.runnerEventSeq(right.payload)).slice(0, limit);
479
+ const oldest = [];
480
+ for (const entry of state.pendingEvents) {
481
+ if (oldest.length >= limit) break;
482
+ oldest.push(entry);
483
+ }
484
+ return oldest;
485
+ }
430
486
  pushPendingEventBatch(eventIds, events, state) {
431
487
  if (!this.isCurrentThreadState(state.threadId, state) || this.failIfEventDeadlineExceeded(state) || state.channel.state !== "joined" || !state.socket.isConnected()) return;
432
488
  const attempt = ++state.nextEventPushAttempt;
489
+ const sentAt = Date.now();
433
490
  state.activeEventBatch = {
434
491
  eventIds,
435
492
  attempt
@@ -440,9 +497,12 @@ var IntelligenceAgentRunner = class extends AgentRunner {
440
497
  if (!this.isCurrentThreadState(state.threadId, state) || state.activeEventBatch?.attempt !== attempt) return;
441
498
  if (this.failIfEventDeadlineExceeded(state)) return;
442
499
  for (const eventId of eventIds) state.pendingEvents.delete(eventId);
500
+ state.durableProgressAt = Date.now();
501
+ this.reportEventDelivery(state, eventIds.length, state.durableProgressAt - sentAt);
443
502
  state.activeEventBatch = null;
444
503
  state.eventRetryAttempt = 0;
445
504
  if (state.pendingEvents.size === 0) {
505
+ state.pendingEventsOutOfOrder = false;
446
506
  this.clearPendingEventRetry(state);
447
507
  this.clearPendingEventFlush(state);
448
508
  }
@@ -451,6 +511,31 @@ var IntelligenceAgentRunner = class extends AgentRunner {
451
511
  this.replayPendingEvents(state);
452
512
  }).receive("error", (response) => this.handlePendingEventFailure(state, attempt, response)).receive("timeout", () => this.handlePendingEventFailure(state, attempt));
453
513
  }
514
+ reportEventDelivery(state, acknowledgedEvents, ackLatencyMs) {
515
+ const onStats = this.options.onEventDeliveryStats;
516
+ if (!onStats) return;
517
+ const pending = this.oldestPendingEvents(state, 1);
518
+ const logFailure = (error) => {
519
+ this.logger.warn({
520
+ err: error,
521
+ threadId: state.threadId,
522
+ runId: state.runId
523
+ }, "onEventDeliveryStats callback failed");
524
+ };
525
+ try {
526
+ const result = onStats({
527
+ threadId: state.threadId,
528
+ runId: state.runId,
529
+ acknowledgedEvents,
530
+ ackLatencyMs,
531
+ pendingEvents: state.pendingEvents.size,
532
+ oldestPendingAgeMs: pending.length > 0 ? Date.now() - pending[0][1].queuedAt : 0
533
+ });
534
+ if (result instanceof Promise) result.catch(logFailure);
535
+ } catch (error) {
536
+ logFailure(error);
537
+ }
538
+ }
454
539
  handlePendingEventFailure(state, attempt, response) {
455
540
  if (!this.isCurrentThreadState(state.threadId, state) || state.activeEventBatch?.attempt !== attempt) return;
456
541
  if (this.failIfEventDeadlineExceeded(state)) return;
@@ -479,8 +564,8 @@ var IntelligenceAgentRunner = class extends AgentRunner {
479
564
  }
480
565
  schedulePendingEventFlush(state) {
481
566
  if (!this.isCurrentThreadState(state.threadId, state) || state.pendingEvents.size === 0 || state.activeEventBatch !== null || state.eventFlushTimer !== null) return;
482
- const oldestQueuedAt = Math.min(...[...state.pendingEvents.values()].map((event) => event.queuedAt));
483
- const delay = Math.max(0, oldestQueuedAt + RUNNER_EVENT_BATCH_FLUSH_MS - Date.now());
567
+ const [[, oldest]] = this.oldestPendingEvents(state, 1);
568
+ const delay = Math.max(0, oldest.queuedAt + RUNNER_EVENT_BATCH_FLUSH_MS - Date.now());
484
569
  state.eventFlushTimer = setTimeout(() => {
485
570
  state.eventFlushTimer = null;
486
571
  if (!this.isCurrentThreadState(state.threadId, state)) return;
@@ -501,12 +586,11 @@ var IntelligenceAgentRunner = class extends AgentRunner {
501
586
  }
502
587
  replayPendingEvents(state) {
503
588
  if (!this.isCurrentThreadState(state.threadId, state) || state.activeEventBatch !== null) return;
504
- const pendingEvents = [...state.pendingEvents.entries()].sort(([, left], [, right]) => this.runnerEventSeq(left.payload) - this.runnerEventSeq(right.payload));
505
- if (pendingEvents.length === 0) return;
589
+ if (state.pendingEvents.size === 0) return;
506
590
  const batchSize = state.supportsRunnerEventBatch ? MAX_RUNNER_EVENT_BATCH_SIZE : 1;
507
- if (!state.supportsRunnerEventBatch || pendingEvents.length >= batchSize) {
591
+ if (!state.supportsRunnerEventBatch || state.pendingEvents.size >= batchSize) {
508
592
  this.clearPendingEventFlush(state);
509
- const batch = pendingEvents.slice(0, batchSize);
593
+ const batch = this.oldestPendingEvents(state, batchSize);
510
594
  this.pushPendingEventBatch(batch.map(([eventId]) => eventId), batch.map(([, event]) => event), state);
511
595
  return;
512
596
  }
@@ -514,7 +598,7 @@ var IntelligenceAgentRunner = class extends AgentRunner {
514
598
  }
515
599
  flushPendingEventBatch(state) {
516
600
  if (!this.isCurrentThreadState(state.threadId, state) || state.activeEventBatch !== null || state.pendingEvents.size === 0) return;
517
- const batch = [...state.pendingEvents.entries()].sort(([, left], [, right]) => this.runnerEventSeq(left.payload) - this.runnerEventSeq(right.payload)).slice(0, MAX_RUNNER_EVENT_BATCH_SIZE);
601
+ const batch = this.oldestPendingEvents(state, MAX_RUNNER_EVENT_BATCH_SIZE);
518
602
  this.pushPendingEventBatch(batch.map(([eventId]) => eventId), batch.map(([, event]) => event), state);
519
603
  }
520
604
  retryActiveEventBatch(state) {
@@ -564,7 +648,7 @@ var IntelligenceAgentRunner = class extends AgentRunner {
564
648
  state.eventDeadlineTimer = null;
565
649
  }
566
650
  if (!this.isCurrentThreadState(state.threadId, state) || state.pendingEvents.size === 0) return;
567
- const deadline = Math.min(...[...state.pendingEvents.values()].map((event) => event.queuedAt)) + EVENT_DURABILITY_DEADLINE_MS;
651
+ const deadline = state.durableProgressAt + EVENT_DURABILITY_DEADLINE_MS;
568
652
  state.eventDeadlineTimer = setTimeout(() => {
569
653
  state.eventDeadlineTimer = null;
570
654
  if (!this.isCurrentThreadState(state.threadId, state)) return;
@@ -581,8 +665,7 @@ var IntelligenceAgentRunner = class extends AgentRunner {
581
665
  }
582
666
  failIfEventDeadlineExceeded(state) {
583
667
  if (state.pendingEvents.size === 0) return false;
584
- const oldestQueuedAt = Math.min(...[...state.pendingEvents.values()].map((event) => event.queuedAt));
585
- if (Date.now() < oldestQueuedAt + EVENT_DURABILITY_DEADLINE_MS) return false;
668
+ if (Date.now() < state.durableProgressAt + EVENT_DURABILITY_DEADLINE_MS) return false;
586
669
  this.failThread(state.threadId, state, /* @__PURE__ */ new Error("Timed out trying to durably deliver runner events"));
587
670
  return true;
588
671
  }
@@ -657,5 +740,5 @@ function recordStopWithin(state, deadlineMs, logger) {
657
740
  }
658
741
 
659
742
  //#endregion
660
- export { IntelligenceAgentRunner, STOP_RECORD_DEADLINE_MS };
743
+ export { IntelligenceAgentRunner, RunnerEventQueueFullError, STOP_RECORD_DEADLINE_MS };
661
744
  //# sourceMappingURL=intelligence.mjs.map