@enclave-vm/broker 2.19.1 → 2.20.0-rc.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.
package/index.js CHANGED
@@ -21,8 +21,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  Broker: () => Broker,
24
+ BrokerError: () => BrokerError,
25
+ BrokerErrorCode: () => BrokerErrorCode,
24
26
  BrokerSession: () => BrokerSession,
25
27
  CatalogHandler: () => CatalogHandler,
28
+ DEFAULT_SESSION_HISTORY_LIMITS: () => DEFAULT_SESSION_HISTORY_LIMITS,
26
29
  EventFilter: () => EventFilter,
27
30
  OpenApiSource: () => OpenApiSource,
28
31
  OpenApiSpecPoller: () => OpenApiSpecPoller,
@@ -43,8 +46,159 @@ __export(index_exports, {
43
46
  });
44
47
  module.exports = __toCommonJS(index_exports);
45
48
 
49
+ // libs/broker/src/limits.ts
50
+ var import_types = require("@enclave-vm/types");
51
+ var LIMIT_KEYS = Object.keys(import_types.DEFAULT_SESSION_LIMITS);
52
+ var MAX_TIMER_MS = 2 ** 31 - 1;
53
+ function timerDelay(ms) {
54
+ return Math.min(Math.max(ms, 1), MAX_TIMER_MS);
55
+ }
56
+ function mergeSessionLimits(...layers) {
57
+ const merged = {};
58
+ for (const layer of layers) {
59
+ if (layer === void 0 || layer === null) continue;
60
+ if (typeof layer !== "object") {
61
+ throw new TypeError("Session limits must be an object");
62
+ }
63
+ for (const key of LIMIT_KEYS) {
64
+ const value = layer[key];
65
+ if (value !== void 0 && value !== null) merged[key] = value;
66
+ }
67
+ }
68
+ return merged;
69
+ }
70
+ function resolveSessionLimits(limits) {
71
+ const resolved = { ...import_types.DEFAULT_SESSION_LIMITS, ...mergeSessionLimits(limits) };
72
+ for (const key of LIMIT_KEYS) {
73
+ const value = resolved[key];
74
+ if (key === "cancelOnFirstError") {
75
+ if (typeof value !== "boolean") {
76
+ throw new TypeError(`Session limit "${key}" must be a boolean`);
77
+ }
78
+ } else if (typeof value !== "number" || Number.isNaN(value)) {
79
+ throw new TypeError(`Session limit "${key}" must be a number`);
80
+ }
81
+ }
82
+ return resolved;
83
+ }
84
+
85
+ // libs/broker/src/session-history.ts
86
+ var import_core = require("@enclave-vm/core");
87
+ var DEFAULT_SESSION_HISTORY_LIMITS = {
88
+ maxEvents: 1e4,
89
+ maxBytes: 4 * 1024 * 1024
90
+ };
91
+ var UNMEASURED_EVENT_BYTES = 1024;
92
+ function resolveHistoryLimits(limits) {
93
+ const resolved = { ...DEFAULT_SESSION_HISTORY_LIMITS };
94
+ if (limits === void 0 || limits === null) return resolved;
95
+ if (typeof limits !== "object") {
96
+ throw new TypeError("Session history limits must be an object");
97
+ }
98
+ for (const key of ["maxEvents", "maxBytes"]) {
99
+ const value = limits[key];
100
+ if (value === void 0) continue;
101
+ if (typeof value !== "number" || Number.isNaN(value) || value < 0) {
102
+ throw new TypeError(`Session history limit "${key}" must be a non-negative number`);
103
+ }
104
+ resolved[key] = value;
105
+ }
106
+ return resolved;
107
+ }
108
+ var BoundedSessionEmitter = class extends import_core.SessionEmitter {
109
+ limits;
110
+ kept = [];
111
+ keptBytes = 0;
112
+ droppedThrough = 0;
113
+ ended = false;
114
+ /**
115
+ * Size of the event that is emitted next, when the caller knows it already. Used once.
116
+ */
117
+ nextEventSize;
118
+ constructor(config, limits) {
119
+ super(config);
120
+ this.limits = resolveHistoryLimits(limits);
121
+ }
122
+ /**
123
+ * Emit a stream event: keep it within the bounds, then hand it to the listeners.
124
+ */
125
+ emit(event) {
126
+ if (this.ended) return;
127
+ if (event.type === "final") this.ended = true;
128
+ this.keep(event);
129
+ try {
130
+ super.emit(event);
131
+ } finally {
132
+ super.clearHistory();
133
+ }
134
+ }
135
+ /**
136
+ * The kept events, oldest first.
137
+ */
138
+ getEmittedEvents() {
139
+ return this.kept.map((entry) => entry.event);
140
+ }
141
+ /**
142
+ * Drop every kept event.
143
+ */
144
+ clearHistory() {
145
+ super.clearHistory();
146
+ const last = this.kept[this.kept.length - 1];
147
+ if (last) {
148
+ this.droppedThrough = Math.max(this.droppedThrough, last.event.seq);
149
+ }
150
+ this.kept = [];
151
+ this.keptBytes = 0;
152
+ }
153
+ /**
154
+ * The lowest sequence number a replay can start from: every event from there on is kept.
155
+ */
156
+ get firstReplayableSeq() {
157
+ return this.droppedThrough + 1;
158
+ }
159
+ keep(event) {
160
+ let size = this.nextEventSize;
161
+ this.nextEventSize = void 0;
162
+ if (size === void 0) {
163
+ try {
164
+ size = Buffer.byteLength(JSON.stringify(event), "utf8");
165
+ } catch {
166
+ size = UNMEASURED_EVENT_BYTES;
167
+ }
168
+ }
169
+ this.kept.push({ event, size });
170
+ this.keptBytes += size;
171
+ while (this.kept.length > 0 && (this.kept.length > this.limits.maxEvents || this.keptBytes > this.limits.maxBytes)) {
172
+ const oldest = this.kept.shift();
173
+ this.keptBytes -= oldest.size;
174
+ this.droppedThrough = Math.max(this.droppedThrough, oldest.event.seq);
175
+ }
176
+ }
177
+ };
178
+
46
179
  // libs/broker/src/tool-registry.ts
47
180
  var import_zod = require("zod");
181
+ var REDACTED_SECRET = "[REDACTED]";
182
+ function redactSecrets(text, secretValues) {
183
+ if (typeof text !== "string") return text;
184
+ const values = [...new Set(secretValues)].filter((value) => typeof value === "string" && value.length > 0);
185
+ if (!values.some((value) => text.includes(value))) return text;
186
+ values.sort((a, b) => b.length - a.length);
187
+ let redacted = "";
188
+ let i = 0;
189
+ scan: while (i < text.length) {
190
+ for (const value of values) {
191
+ if (text.startsWith(value, i)) {
192
+ redacted += REDACTED_SECRET;
193
+ i += value.length;
194
+ continue scan;
195
+ }
196
+ }
197
+ redacted += text[i];
198
+ i++;
199
+ }
200
+ return redacted;
201
+ }
48
202
  var ToolRegistry = class {
49
203
  tools;
50
204
  secretStore;
@@ -109,7 +263,7 @@ var ToolRegistry = class {
109
263
  * Get tool configurations for all registered tools
110
264
  */
111
265
  getConfigs() {
112
- const configs = {};
266
+ const configs = /* @__PURE__ */ Object.create(null);
113
267
  for (const [name, tool] of this.tools) {
114
268
  if (tool.definition.config) {
115
269
  configs[name] = tool.definition.config;
@@ -222,6 +376,7 @@ var ToolRegistry = class {
222
376
  durationMs: Date.now() - startTime
223
377
  };
224
378
  }
379
+ const secretValues = Object.values(secrets);
225
380
  try {
226
381
  const result = await tool.definition.handler(validation.validatedArgs, {
227
382
  ...context,
@@ -237,8 +392,8 @@ var ToolRegistry = class {
237
392
  return {
238
393
  success: false,
239
394
  error: {
240
- code: err.code ?? "EXECUTION_ERROR",
241
- message: err.message
395
+ code: redactSecrets(err.code ?? "EXECUTION_ERROR", secretValues),
396
+ message: redactSecrets(err.message, secretValues)
242
397
  },
243
398
  durationMs: Date.now() - startTime
244
399
  };
@@ -268,39 +423,95 @@ function createToolRegistry() {
268
423
  }
269
424
 
270
425
  // libs/broker/src/broker-session.ts
271
- var import_types = require("@enclave-vm/types");
272
- var import_core = require("@enclave-vm/core");
426
+ var import_types2 = require("@enclave-vm/types");
427
+ var import_core2 = require("@enclave-vm/core");
428
+ var DEFAULT_BRIDGE_PAYLOAD_BYTES = 5 * 1024 * 1024;
429
+ function isPlainData(value) {
430
+ const pending = [value];
431
+ const seen = /* @__PURE__ */ new Set();
432
+ while (pending.length > 0) {
433
+ const current = pending.pop();
434
+ if (current === null || typeof current !== "object") continue;
435
+ if (seen.has(current)) continue;
436
+ seen.add(current);
437
+ if (Array.isArray(current)) {
438
+ const length = current.length;
439
+ for (let index = 0; index < length; index++) pending.push(current[index]);
440
+ continue;
441
+ }
442
+ const prototype = Object.getPrototypeOf(current);
443
+ if (prototype !== Object.prototype && prototype !== null) return false;
444
+ for (const key of Object.keys(current)) {
445
+ if (key === "__proto__" || key === "constructor") return false;
446
+ pending.push(current[key]);
447
+ }
448
+ }
449
+ return true;
450
+ }
451
+ var BRIDGE_RESPONSE_ENVELOPE_BYTES = JSON.stringify({ v: 1, ok: true, value: 0 }).length - 1;
273
452
  var BrokerSession = class {
274
453
  sessionId;
275
454
  createdAt;
276
455
  expiresAt;
456
+ /**
457
+ * The caller this session belongs to, if any.
458
+ */
459
+ owner;
460
+ /** Creation identifier supplied by the HTTP client, if any. */
461
+ instanceId;
277
462
  emitter;
278
463
  toolRegistry;
279
464
  enclave;
280
465
  abortController;
281
466
  limits;
467
+ checkResultSize;
468
+ /** The payload limit of the sandbox's string bridge; undefined when the bridge is not used. */
469
+ bridgePayloadBytes;
470
+ /**
471
+ * The size from which the sandbox's sidecar holds a string result as a reference (measured by
472
+ * the bridge after that); undefined without a sidecar.
473
+ */
474
+ sidecarExtractionBytes;
282
475
  _state = "starting";
283
476
  executionPromise = null;
284
477
  toolCallCount = 0;
285
478
  stdoutBytes = 0;
286
479
  heartbeatInterval = null;
287
480
  deadlineTimer = null;
481
+ lifetimeTimer = null;
288
482
  _deadlineExceeded = false;
289
483
  partialErrors = [];
290
484
  constructor(toolRegistry, config = {}) {
291
- this.sessionId = config.sessionId ?? (0, import_types.generateSessionId)();
485
+ this.sessionId = config.sessionId ?? (0, import_types2.generateSessionId)();
486
+ if (config.instanceId !== void 0 && (typeof config.instanceId !== "string" || !/^[A-Za-z0-9._:~-]{1,128}$/.test(config.instanceId))) {
487
+ throw new TypeError("Session instance id must be a short header-safe string");
488
+ }
489
+ this.instanceId = config.instanceId;
490
+ if (config.owner !== void 0) {
491
+ if (typeof config.owner !== "string" || config.owner.length === 0) {
492
+ throw new TypeError("Session owner must be a non-empty string");
493
+ }
494
+ this.owner = config.owner;
495
+ }
292
496
  this.createdAt = Date.now();
293
497
  this.toolRegistry = toolRegistry;
294
498
  this.abortController = new AbortController();
295
- this.limits = {
296
- ...import_types.DEFAULT_SESSION_LIMITS,
297
- ...config.limits
298
- };
499
+ this.limits = resolveSessionLimits(config.limits);
500
+ const bridge = config.enclaveConfig?.toolBridge;
501
+ this.checkResultSize = true;
502
+ this.bridgePayloadBytes = (bridge?.mode ?? "string") === "string" ? bridge?.maxPayloadBytes ?? DEFAULT_BRIDGE_PAYLOAD_BYTES : void 0;
503
+ const sidecar = config.enclaveConfig?.sidecar;
504
+ this.sidecarExtractionBytes = sidecar?.enabled ? sidecar.extractionThreshold ?? import_core2.REFERENCE_CONFIGS[config.enclaveConfig?.securityLevel ?? "STANDARD"].extractionThreshold : void 0;
299
505
  this.expiresAt = this.createdAt + this.limits.sessionTtlMs;
300
- this.emitter = (0, import_core.createSessionEmitter)(this.sessionId, {
301
- expiresAt: new Date(this.expiresAt)
302
- });
303
- this.enclave = new import_core.Enclave({
506
+ this.emitter = new BoundedSessionEmitter(
507
+ {
508
+ sessionId: this.sessionId,
509
+ cancelUrl: `/sessions/${this.sessionId}/cancel`,
510
+ expiresAt: new Date(this.expiresAt).toISOString()
511
+ },
512
+ config.history
513
+ );
514
+ this.enclave = new import_core2.Enclave({
304
515
  ...config.enclaveConfig,
305
516
  timeout: this.limits.toolTimeoutMs,
306
517
  maxToolCalls: this.limits.maxToolCalls
@@ -331,11 +542,19 @@ var BrokerSession = class {
331
542
  return this.emitter.on(handler);
332
543
  }
333
544
  /**
334
- * Get all emitted events
545
+ * Get the events the session keeps: all of them, or the most recent ones once a bound of
546
+ * the history was reached (see `firstReplayableSeq`)
335
547
  */
336
548
  getEvents() {
337
549
  return this.emitter.getEmittedEvents();
338
550
  }
551
+ /**
552
+ * The lowest sequence number from which every event is still kept. It is 1 until the history
553
+ * reaches a bound; from then on, events below it cannot be replayed.
554
+ */
555
+ get firstReplayableSeq() {
556
+ return this.emitter.firstReplayableSeq;
557
+ }
339
558
  /**
340
559
  * Execute code in this session
341
560
  *
@@ -347,13 +566,16 @@ var BrokerSession = class {
347
566
  throw new Error("Session is already executing");
348
567
  }
349
568
  this.emitter.emitSessionInit();
569
+ if (this.executionPromise) {
570
+ return this.executionPromise;
571
+ }
350
572
  const heartbeatMs = this.limits.heartbeatIntervalMs;
351
573
  if (heartbeatMs > 0) {
352
574
  this.heartbeatInterval = setInterval(() => {
353
575
  if (!this.isTerminal()) {
354
576
  this.emitter.emitHeartbeat();
355
577
  }
356
- }, heartbeatMs);
578
+ }, timerDelay(heartbeatMs));
357
579
  }
358
580
  const deadlineMs = this.limits.deadlineMs;
359
581
  if (deadlineMs > 0) {
@@ -362,14 +584,14 @@ var BrokerSession = class {
362
584
  this.deadlineTimer = setTimeout(() => {
363
585
  this._deadlineExceeded = true;
364
586
  this.cancel(`Deadline exceeded: ${deadlineMs}ms budget`);
365
- }, remaining);
587
+ }, timerDelay(remaining));
366
588
  } else {
367
589
  this._deadlineExceeded = true;
368
590
  this.stopHeartbeat();
369
591
  this._state = "failed";
370
592
  const elapsed = Date.now() - this.createdAt;
371
593
  this.emitter.emit(
372
- this.makeCustomEvent(import_types.EventType.DeadlineExceeded, { elapsedMs: elapsed, budgetMs: deadlineMs })
594
+ this.makeCustomEvent(import_types2.EventType.DeadlineExceeded, { elapsedMs: elapsed, budgetMs: deadlineMs })
373
595
  );
374
596
  this.emitter.emitFinalError(
375
597
  { code: "DEADLINE_EXCEEDED", message: "Deadline exceeded before execution" },
@@ -395,6 +617,13 @@ var BrokerSession = class {
395
617
  return finalResult;
396
618
  }
397
619
  }
620
+ const lifetimeLeft = this.expiresAt - Date.now();
621
+ this.lifetimeTimer = setTimeout(
622
+ () => {
623
+ if (!this.isTerminal()) this.cancel("Session lifetime exceeded");
624
+ },
625
+ timerDelay(Math.max(0, lifetimeLeft))
626
+ );
398
627
  this._state = "running";
399
628
  this.executionPromise = this.runExecution(code);
400
629
  return this.executionPromise;
@@ -406,12 +635,21 @@ var BrokerSession = class {
406
635
  const startTime = Date.now();
407
636
  let result;
408
637
  try {
409
- const enclaveResult = await this.enclave.run(code, async (toolName, args) => {
410
- if (this.abortController.signal.aborted) {
411
- throw new Error("Session cancelled");
638
+ const enclaveResult = await this.enclave.run(
639
+ code,
640
+ async (toolName, args) => {
641
+ if (this.abortController.signal.aborted) {
642
+ throw new Error("Session cancelled");
643
+ }
644
+ return this.executeTool(toolName, args);
645
+ },
646
+ {
647
+ maxToolResultBytes: this.checkResultSize && Number.isFinite(this.limits.maxToolResultBytes) && this.limits.maxToolResultBytes > 0 ? this.limits.maxToolResultBytes : void 0,
648
+ onToolBridgeFailure: () => {
649
+ if (this.limits.cancelOnFirstError) this.abortController.abort();
650
+ }
412
651
  }
413
- return this.executeTool(toolName, args);
414
- });
652
+ );
415
653
  const endTime = Date.now();
416
654
  const stats = this.buildStats(startTime, endTime);
417
655
  const eventStats = {
@@ -422,7 +660,7 @@ var BrokerSession = class {
422
660
  if (this.isTerminal()) {
423
661
  if (this._deadlineExceeded) {
424
662
  this.emitter.emit(
425
- this.makeCustomEvent(import_types.EventType.DeadlineExceeded, {
663
+ this.makeCustomEvent(import_types2.EventType.DeadlineExceeded, {
426
664
  elapsedMs: Date.now() - this.createdAt,
427
665
  budgetMs: this.limits.deadlineMs
428
666
  })
@@ -477,7 +715,7 @@ var BrokerSession = class {
477
715
  };
478
716
  if (this._deadlineExceeded) {
479
717
  this.emitter.emit(
480
- this.makeCustomEvent(import_types.EventType.DeadlineExceeded, {
718
+ this.makeCustomEvent(import_types2.EventType.DeadlineExceeded, {
481
719
  elapsedMs: Date.now() - this.createdAt,
482
720
  budgetMs: this.limits.deadlineMs
483
721
  })
@@ -513,7 +751,7 @@ var BrokerSession = class {
513
751
  */
514
752
  emitToolProgress(callId, phase, elapsedMs, bytesReceived, totalBytes) {
515
753
  this.emitter.emit(
516
- this.makeCustomEvent(import_types.EventType.ToolProgress, {
754
+ this.makeCustomEvent(import_types2.EventType.ToolProgress, {
517
755
  callId,
518
756
  phase,
519
757
  elapsedMs,
@@ -530,7 +768,7 @@ var BrokerSession = class {
530
768
  this.partialErrors.push(error);
531
769
  }
532
770
  this.emitter.emit(
533
- this.makeCustomEvent(import_types.EventType.PartialResult, {
771
+ this.makeCustomEvent(import_types2.EventType.PartialResult, {
534
772
  path,
535
773
  data,
536
774
  error,
@@ -548,7 +786,7 @@ var BrokerSession = class {
548
786
  * Execute a tool call directly
549
787
  */
550
788
  async executeTool(toolName, args) {
551
- const callId = (0, import_types.generateCallId)();
789
+ const callId = (0, import_types2.generateCallId)();
552
790
  const toolStartTime = Date.now();
553
791
  if (this.limits.deadlineMs > 0) {
554
792
  const elapsed = Date.now() - this.createdAt;
@@ -560,10 +798,16 @@ var BrokerSession = class {
560
798
  throw err;
561
799
  }
562
800
  }
563
- this.emitter.emitToolCall(callId, toolName, args);
801
+ const snapshot = snapshotArguments(args);
802
+ this.emitter.nextEventSize = snapshot.size + TOOL_CALL_EVENT_OVERHEAD_BYTES;
803
+ this.emitter.emitToolCall(callId, toolName, snapshot.copy);
564
804
  this.toolCallCount++;
565
805
  this.emitToolProgress(callId, "connecting", 0);
566
806
  let toolTimeout = this.limits.perToolDeadlineMs;
807
+ const ownTimeout = this.toolRegistry.get(toolName)?.config?.timeout;
808
+ if (typeof ownTimeout === "number" && ownTimeout > 0) {
809
+ toolTimeout = toolTimeout > 0 ? Math.min(toolTimeout, ownTimeout) : ownTimeout;
810
+ }
567
811
  if (this.limits.deadlineMs > 0) {
568
812
  const remaining = this.limits.deadlineMs - (Date.now() - this.createdAt);
569
813
  toolTimeout = Math.min(toolTimeout, remaining);
@@ -571,7 +815,7 @@ var BrokerSession = class {
571
815
  const toolAbort = new AbortController();
572
816
  let toolTimer = null;
573
817
  if (toolTimeout > 0) {
574
- toolTimer = setTimeout(() => toolAbort.abort(), toolTimeout);
818
+ toolTimer = setTimeout(() => toolAbort.abort(), timerDelay(toolTimeout));
575
819
  }
576
820
  const onSessionAbort = () => toolAbort.abort();
577
821
  this.abortController.signal.addEventListener("abort", onSessionAbort);
@@ -593,6 +837,12 @@ var BrokerSession = class {
593
837
  if (toolTimer) clearTimeout(toolTimer);
594
838
  this.abortController.signal.removeEventListener("abort", onSessionAbort);
595
839
  }
840
+ if (result.success) {
841
+ const refusal = this.resultRefusal(result.value);
842
+ if (refusal) {
843
+ result = { success: false, error: refusal, durationMs: result.durationMs };
844
+ }
845
+ }
596
846
  this.emitter.emitToolResultApplied(callId);
597
847
  if (result.success) {
598
848
  return result.value;
@@ -605,12 +855,41 @@ var BrokerSession = class {
605
855
  throw error;
606
856
  }
607
857
  }
858
+ /**
859
+ * Why a tool result is refused, or undefined: over `limits.maxToolResultBytes`, or over the
860
+ * payload limit of the sandbox's string bridge (with the bytes of its response envelope),
861
+ * measured as serialized JSON.
862
+ */
863
+ resultRefusal(value) {
864
+ const limit = this.limits.maxToolResultBytes;
865
+ const checkOwn = this.checkResultSize && limit > 0 && limit !== Infinity;
866
+ if (!checkOwn && this.bridgePayloadBytes === void 0) return void 0;
867
+ let serialized;
868
+ try {
869
+ serialized = JSON.stringify(value);
870
+ } catch {
871
+ return void 0;
872
+ }
873
+ if (serialized === void 0) return void 0;
874
+ const bytes = Buffer.byteLength(serialized, "utf8");
875
+ if (checkOwn && bytes > limit) {
876
+ return { code: "TOOL_RESULT_TOO_LARGE", message: `Tool result exceeds the limit of ${limit} bytes` };
877
+ }
878
+ const heldBySidecar = typeof value === "string" && this.sidecarExtractionBytes !== void 0 && Buffer.byteLength(value, "utf8") >= this.sidecarExtractionBytes;
879
+ if (!heldBySidecar && this.bridgePayloadBytes !== void 0 && bytes + BRIDGE_RESPONSE_ENVELOPE_BYTES > this.bridgePayloadBytes && isPlainData(value)) {
880
+ return {
881
+ code: "TOOL_BRIDGE_RESPONSE_TOO_LARGE",
882
+ message: `Tool response exceeds maximum size (${this.bridgePayloadBytes} bytes)`
883
+ };
884
+ }
885
+ return void 0;
886
+ }
608
887
  /**
609
888
  * Create a custom event with proper base fields and unique sequence number.
610
889
  */
611
890
  makeCustomEvent(type, payload) {
612
891
  return {
613
- protocolVersion: import_types.PROTOCOL_VERSION,
892
+ protocolVersion: import_types2.PROTOCOL_VERSION,
614
893
  sessionId: this.sessionId,
615
894
  seq: this.emitter.nextSeq(),
616
895
  type,
@@ -720,6 +999,10 @@ var BrokerSession = class {
720
999
  clearTimeout(this.deadlineTimer);
721
1000
  this.deadlineTimer = null;
722
1001
  }
1002
+ if (this.lifetimeTimer) {
1003
+ clearTimeout(this.lifetimeTimer);
1004
+ this.lifetimeTimer = null;
1005
+ }
723
1006
  }
724
1007
  /**
725
1008
  * Dispose of the session and its resources
@@ -731,11 +1014,44 @@ var BrokerSession = class {
731
1014
  this.enclave.dispose();
732
1015
  }
733
1016
  };
1017
+ var TOOL_CALL_EVENT_OVERHEAD_BYTES = 256;
1018
+ function snapshotArguments(args) {
1019
+ try {
1020
+ const serialized = JSON.stringify(args);
1021
+ if (serialized !== void 0) {
1022
+ return {
1023
+ copy: JSON.parse(serialized),
1024
+ size: Buffer.byteLength(serialized, "utf8")
1025
+ };
1026
+ }
1027
+ } catch {
1028
+ }
1029
+ return { copy: {}, size: 2 };
1030
+ }
734
1031
  function createBrokerSession(toolRegistry, config) {
735
1032
  return new BrokerSession(toolRegistry, config);
736
1033
  }
737
1034
 
1035
+ // libs/broker/src/errors.ts
1036
+ var BrokerErrorCode = {
1037
+ /** The session id is not a usable id */
1038
+ InvalidSessionId: "INVALID_SESSION_ID",
1039
+ /** A session with this id is registered */
1040
+ SessionExists: "SESSION_EXISTS",
1041
+ /** The broker holds as many sessions as `maxSessions` allows */
1042
+ MaxSessionsReached: "MAX_SESSIONS_REACHED"
1043
+ };
1044
+ var BrokerError = class extends Error {
1045
+ code;
1046
+ constructor(code, message) {
1047
+ super(message);
1048
+ this.name = "BrokerError";
1049
+ this.code = code;
1050
+ }
1051
+ };
1052
+
738
1053
  // libs/broker/src/session-manager.ts
1054
+ var MAX_SESSION_ID_LENGTH = 256;
739
1055
  var SessionManager = class {
740
1056
  sessions;
741
1057
  toolRegistry;
@@ -755,21 +1071,40 @@ var SessionManager = class {
755
1071
  /**
756
1072
  * Create a new session
757
1073
  *
758
- * @throws Error if max sessions reached or manager is disposed
1074
+ * A session id names one session for as long as that session is registered: an id that is in
1075
+ * use is refused.
1076
+ *
1077
+ * @throws BrokerError if the id is not usable or is in use, or if max sessions is reached
1078
+ * @throws Error if the manager is disposed
759
1079
  */
760
1080
  create(config) {
761
1081
  if (this.disposed) {
762
1082
  throw new Error("Session manager is disposed");
763
1083
  }
1084
+ const requestedId = config?.sessionId;
1085
+ if (requestedId !== void 0) {
1086
+ if (typeof requestedId !== "string" || requestedId.length === 0 || requestedId.length > MAX_SESSION_ID_LENGTH) {
1087
+ throw new BrokerError(BrokerErrorCode.InvalidSessionId, "Session id must be a non-empty string");
1088
+ }
1089
+ if (this.sessions.has(requestedId)) {
1090
+ throw new BrokerError(BrokerErrorCode.SessionExists, "A session with this id already exists");
1091
+ }
1092
+ }
764
1093
  if (this.sessions.size >= this.config.maxSessions) {
765
- throw new Error(
1094
+ throw new BrokerError(
1095
+ BrokerErrorCode.MaxSessionsReached,
766
1096
  `Maximum sessions reached (${this.config.maxSessions}). Wait for existing sessions to complete or increase maxSessions.`
767
1097
  );
768
1098
  }
769
- const sessionConfig = {
770
- ...this.config.defaultSessionConfig,
771
- ...config
772
- };
1099
+ const defaults = this.config.defaultSessionConfig;
1100
+ const sessionConfig = { ...defaults };
1101
+ for (const [key, value] of Object.entries(config ?? {})) {
1102
+ if (value !== void 0) {
1103
+ sessionConfig[key] = value;
1104
+ }
1105
+ }
1106
+ sessionConfig.instanceId = config?.instanceId;
1107
+ sessionConfig.limits = mergeSessionLimits(defaults.limits, config?.limits);
773
1108
  const session = createBrokerSession(this.toolRegistry, sessionConfig);
774
1109
  this.sessions.set(session.sessionId, session);
775
1110
  return {
@@ -798,7 +1133,8 @@ var SessionManager = class {
798
1133
  state: session.state,
799
1134
  createdAt: session.createdAt,
800
1135
  expiresAt: session.expiresAt,
801
- isExpired: session.isExpired()
1136
+ isExpired: session.isExpired(),
1137
+ ...session.owner !== void 0 && { owner: session.owner }
802
1138
  }));
803
1139
  }
804
1140
  /**
@@ -810,14 +1146,14 @@ var SessionManager = class {
810
1146
  /**
811
1147
  * Terminate a session
812
1148
  */
813
- async terminate(sessionId, reason) {
1149
+ async terminate(sessionId, reason, expectedSession) {
814
1150
  const session = this.sessions.get(sessionId);
815
- if (!session) {
1151
+ if (!session || expectedSession !== void 0 && session !== expectedSession) {
816
1152
  return false;
817
1153
  }
818
1154
  await session.cancel(reason ?? "Terminated by session manager");
819
1155
  session.dispose();
820
- this.sessions.delete(sessionId);
1156
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
821
1157
  return true;
822
1158
  }
823
1159
  /**
@@ -836,7 +1172,7 @@ var SessionManager = class {
836
1172
  return result;
837
1173
  } finally {
838
1174
  session.dispose();
839
- this.sessions.delete(sessionId);
1175
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
840
1176
  }
841
1177
  }
842
1178
  /**
@@ -858,6 +1194,10 @@ var SessionManager = class {
858
1194
  let cleaned = 0;
859
1195
  for (const [sessionId, session] of this.sessions) {
860
1196
  if (session.isExpired() || session.isTerminal()) {
1197
+ if (!session.isTerminal()) {
1198
+ session.cancel("Session expired").catch(() => {
1199
+ });
1200
+ }
861
1201
  session.dispose();
862
1202
  this.sessions.delete(sessionId);
863
1203
  cleaned++;
@@ -875,6 +1215,7 @@ var SessionManager = class {
875
1215
  this.cleanupInterval = setInterval(() => {
876
1216
  this.cleanup();
877
1217
  }, this.config.cleanupIntervalMs);
1218
+ this.cleanupInterval.unref?.();
878
1219
  }
879
1220
  /**
880
1221
  * Stop automatic cleanup
@@ -924,253 +1265,195 @@ async function sha256Hex(data) {
924
1265
  return Array.from(hashArray).map((b) => b.toString(16).padStart(2, "0")).join("");
925
1266
  }
926
1267
 
927
- // libs/broker/src/openapi/openapi-spec-poller.ts
928
- var DEFAULT_RETRY = {
929
- maxRetries: 3,
930
- initialDelayMs: 1e3,
931
- maxDelayMs: 1e4,
932
- backoffMultiplier: 2
1268
+ // libs/broker/src/openapi/http-utils.ts
1269
+ var MAX_REDIRECTS = 5;
1270
+ var REDIRECT_STATUS = [301, 302, 303, 307, 308];
1271
+ var ResponseTooLargeError = class extends Error {
1272
+ code = "RESPONSE_TOO_LARGE";
1273
+ constructor(maxBytes) {
1274
+ super(`Response body exceeds the limit of ${maxBytes} bytes`);
1275
+ this.name = "ResponseTooLargeError";
1276
+ }
933
1277
  };
934
- var OpenApiSpecPoller = class extends import_events.EventEmitter {
935
- config;
936
- intervalTimer = null;
937
- lastHash = null;
938
- lastEtag = null;
939
- lastModified = null;
940
- consecutiveFailures = 0;
941
- isPolling = false;
942
- wasUnhealthy = false;
943
- /** Per-run abort controller — aborted on stop(), recreated on start() */
944
- _runAbort = new AbortController();
945
- _fetchController = null;
946
- _activePoll = null;
947
- constructor(config) {
948
- super();
949
- this.config = {
950
- url: config.url,
951
- intervalMs: config.intervalMs ?? 6e4,
952
- fetchTimeoutMs: config.fetchTimeoutMs ?? 1e4,
953
- changeDetection: config.changeDetection ?? "auto",
954
- unhealthyThreshold: config.unhealthyThreshold ?? 3,
955
- retry: { ...DEFAULT_RETRY, ...config.retry },
956
- headers: config.headers ?? {}
957
- };
1278
+ function sameOrigin(a, b) {
1279
+ try {
1280
+ return new URL(a).origin === new URL(b).origin;
1281
+ } catch {
1282
+ return false;
958
1283
  }
959
- /**
960
- * Start polling for changes.
961
- */
962
- start() {
963
- if (this.intervalTimer) return;
964
- this._runAbort = new AbortController();
965
- const runSignal = this._runAbort.signal;
966
- const pendingPoll = this._activePoll;
967
- if (pendingPoll) {
968
- pendingPoll.finally(() => {
969
- if (!runSignal.aborted) {
970
- this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
971
- }
972
- });
973
- } else {
974
- this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
1284
+ }
1285
+ async function discard(response) {
1286
+ try {
1287
+ await response.body?.cancel();
1288
+ } catch {
1289
+ }
1290
+ }
1291
+ async function readBodyText(response, maxBytes, truncate = false) {
1292
+ if (!truncate) {
1293
+ const declared = Number(response.headers.get("content-length"));
1294
+ if (Number.isFinite(declared) && declared > maxBytes) {
1295
+ await discard(response);
1296
+ throw new ResponseTooLargeError(maxBytes);
1297
+ }
1298
+ }
1299
+ if (!response.body) {
1300
+ return "";
1301
+ }
1302
+ const reader = response.body.getReader();
1303
+ const decoder = new TextDecoder();
1304
+ let total = 0;
1305
+ let text = "";
1306
+ for (; ; ) {
1307
+ const { done, value } = await reader.read();
1308
+ if (done) break;
1309
+ total += value.byteLength;
1310
+ if (total > maxBytes) {
1311
+ await reader.cancel().catch(() => void 0);
1312
+ if (!truncate) {
1313
+ throw new ResponseTooLargeError(maxBytes);
1314
+ }
1315
+ text += decoder.decode(value.subarray(0, value.byteLength - (total - maxBytes)), { stream: true });
1316
+ break;
975
1317
  }
976
- this.intervalTimer = setInterval(() => {
977
- this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
978
- }, this.config.intervalMs);
1318
+ text += decoder.decode(value, { stream: true });
979
1319
  }
980
- /**
981
- * Stop polling.
982
- */
983
- stop() {
984
- this._runAbort.abort();
985
- if (this._fetchController) {
986
- this._fetchController.abort();
987
- this._fetchController = null;
1320
+ return text + decoder.decode();
1321
+ }
1322
+ async function fetchOriginBound(url, request) {
1323
+ const origin = new URL(url).origin;
1324
+ let current = url;
1325
+ let method = request.method;
1326
+ let body = request.body;
1327
+ const portable = request.portableHeaders ?? {};
1328
+ let own = { ...request.headers, ...portable };
1329
+ let headers = own;
1330
+ for (let hop = 0; ; hop++) {
1331
+ const response = await fetch(current, { method, headers, body, signal: request.signal, redirect: "manual" });
1332
+ const location = REDIRECT_STATUS.includes(response.status) ? response.headers.get("location") : null;
1333
+ if (!location) {
1334
+ return response;
1335
+ }
1336
+ await discard(response);
1337
+ if (hop >= MAX_REDIRECTS) {
1338
+ throw new Error(`Too many redirects (more than ${MAX_REDIRECTS})`);
1339
+ }
1340
+ let next;
1341
+ try {
1342
+ next = new URL(location, current);
1343
+ } catch {
1344
+ throw new Error("Redirect to an invalid URL");
988
1345
  }
989
- if (this.intervalTimer) {
990
- clearInterval(this.intervalTimer);
991
- this.intervalTimer = null;
1346
+ if (next.protocol !== "http:" && next.protocol !== "https:") {
1347
+ throw new Error("Redirect to an unsupported URL");
1348
+ }
1349
+ if (response.status === 303 && method !== "GET" && method !== "HEAD" || (response.status === 301 || response.status === 302) && method === "POST") {
1350
+ method = "GET";
1351
+ body = void 0;
1352
+ headers = withoutContentType(headers);
1353
+ own = withoutContentType(own);
992
1354
  }
1355
+ if (next.origin !== origin) {
1356
+ headers = { ...body === void 0 ? {} : onlyContentType(headers), ...portable };
1357
+ } else if (request.restoreOnOrigin) {
1358
+ headers = own;
1359
+ }
1360
+ current = next.href;
993
1361
  }
994
- /**
995
- * Get the current content hash.
996
- */
997
- getHash() {
998
- return this.lastHash;
1362
+ }
1363
+ function withoutContentType(headers) {
1364
+ const kept = {};
1365
+ for (const [name, value] of Object.entries(headers)) {
1366
+ if (name.toLowerCase() !== "content-type") kept[name] = value;
999
1367
  }
1000
- /**
1001
- * Get the number of consecutive failures.
1002
- */
1003
- getConsecutiveFailures() {
1004
- return this.consecutiveFailures;
1368
+ return kept;
1369
+ }
1370
+ function onlyContentType(headers) {
1371
+ const kept = {};
1372
+ for (const [name, value] of Object.entries(headers)) {
1373
+ if (name.toLowerCase() === "content-type") kept[name] = value;
1374
+ }
1375
+ return kept;
1376
+ }
1377
+
1378
+ // libs/broker/src/openapi/openapi-tool-loader.ts
1379
+ var import_zod2 = require("zod");
1380
+ var DEFAULT_MAX_RESPONSE_BYTES = 10 * 1024 * 1024;
1381
+ var DEFAULT_MAX_SPEC_BYTES = 32 * 1024 * 1024;
1382
+ var DEFAULT_SPEC_TIMEOUT_MS = 3e4;
1383
+ var MAX_ERROR_BODY_BYTES = 4096;
1384
+ var OpenApiToolLoader = class _OpenApiToolLoader {
1385
+ tools = [];
1386
+ toolNames = /* @__PURE__ */ new Set();
1387
+ specHash;
1388
+ options;
1389
+ auth;
1390
+ constructor(spec, hash, options = {}, auth) {
1391
+ this.options = options;
1392
+ this.auth = auth;
1393
+ this.specHash = hash;
1394
+ this.loadFromSpec(spec);
1005
1395
  }
1006
1396
  /**
1007
- * Perform a single poll cycle.
1397
+ * Create a loader from a URL.
1398
+ *
1399
+ * `auth` and `options.headers` belong to the upstream API. They go with the request for the
1400
+ * specification only when the specification is on the origin of `options.baseUrl`, or when
1401
+ * no base URL is given: the specification then decides where the tools send their requests,
1402
+ * so its host is trusted with the credential either way. What the specification host itself
1403
+ * needs goes into `options.specHeaders`.
1008
1404
  */
1009
- async poll() {
1010
- return this.pollWithSignal(this._runAbort.signal);
1011
- }
1012
- async pollWithSignal(runSignal) {
1013
- if (this.isPolling) return;
1014
- this.isPolling = true;
1405
+ static async fromURL(url, options, auth) {
1406
+ const headers = {};
1407
+ if (!options?.baseUrl || sameOrigin(url, options.baseUrl)) {
1408
+ for (const [name, value] of Object.entries(options?.headers ?? {})) {
1409
+ setHeader(headers, name, value);
1410
+ }
1411
+ _OpenApiToolLoader.applyAuth(headers, auth);
1412
+ }
1413
+ for (const [name, value] of Object.entries(options?.specHeaders ?? {})) {
1414
+ setHeader(headers, name, value);
1415
+ }
1416
+ const controller = new AbortController();
1417
+ const timer = setTimeout(
1418
+ () => controller.abort(),
1419
+ timerDelay(positive(options?.specTimeoutMs) ?? DEFAULT_SPEC_TIMEOUT_MS)
1420
+ );
1421
+ let parsed;
1015
1422
  try {
1016
- await this.fetchWithRetry(runSignal);
1423
+ const response = await fetchOriginBound(url, { method: "GET", headers, signal: controller.signal });
1424
+ if (!response.ok) {
1425
+ await response.body?.cancel().catch(() => void 0);
1426
+ throw new Error(`Failed to fetch OpenAPI spec from ${url}: ${response.status}`);
1427
+ }
1428
+ const text = await readBodyText(response, positive(options?.maxSpecBytes) ?? DEFAULT_MAX_SPEC_BYTES);
1429
+ parsed = JSON.parse(text);
1430
+ } catch (error) {
1431
+ if (controller.signal.aborted) {
1432
+ throw new Error(`Timed out fetching OpenAPI spec from ${url}`);
1433
+ }
1434
+ throw error;
1017
1435
  } finally {
1018
- this.isPolling = false;
1019
- this._activePoll = null;
1436
+ clearTimeout(timer);
1437
+ }
1438
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
1439
+ throw new Error(`The OpenAPI spec from ${url} is not a JSON object`);
1440
+ }
1441
+ const spec = parsed;
1442
+ const hash = await sha256Hex(JSON.stringify(spec));
1443
+ let resolvedBaseUrl = options?.baseUrl;
1444
+ if (!resolvedBaseUrl) {
1445
+ const servers = spec["servers"];
1446
+ if (servers?.[0]?.url) {
1447
+ try {
1448
+ resolvedBaseUrl = new URL(servers[0].url, url).origin + new URL(servers[0].url, url).pathname;
1449
+ } catch {
1450
+ resolvedBaseUrl = new URL(url).origin;
1451
+ }
1452
+ } else {
1453
+ resolvedBaseUrl = new URL(url).origin;
1454
+ }
1020
1455
  }
1021
- }
1022
- async fetchWithRetry(runSignal) {
1023
- const { maxRetries, initialDelayMs, maxDelayMs, backoffMultiplier } = this.config.retry;
1024
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
1025
- if (runSignal.aborted) return;
1026
- try {
1027
- await this.doFetch(runSignal);
1028
- if (runSignal.aborted) return;
1029
- if (this.consecutiveFailures > 0) {
1030
- this.consecutiveFailures = 0;
1031
- if (this.wasUnhealthy) {
1032
- this.wasUnhealthy = false;
1033
- this.emit("recovered");
1034
- }
1035
- }
1036
- return;
1037
- } catch (error) {
1038
- if (runSignal.aborted) return;
1039
- if (attempt === maxRetries) {
1040
- this.consecutiveFailures++;
1041
- this.emit("pollError", error instanceof Error ? error : new Error(String(error)));
1042
- if (this.consecutiveFailures >= this.config.unhealthyThreshold && !this.wasUnhealthy) {
1043
- this.wasUnhealthy = true;
1044
- this.emit("unhealthy", this.consecutiveFailures);
1045
- }
1046
- return;
1047
- }
1048
- const delay = Math.min(initialDelayMs * Math.pow(backoffMultiplier, attempt), maxDelayMs);
1049
- await new Promise((resolve) => {
1050
- const onAbort = () => {
1051
- clearTimeout(timer);
1052
- resolve();
1053
- };
1054
- const timer = setTimeout(() => {
1055
- runSignal.removeEventListener("abort", onAbort);
1056
- resolve();
1057
- }, delay);
1058
- runSignal.addEventListener("abort", onAbort, { once: true });
1059
- });
1060
- }
1061
- }
1062
- }
1063
- async doFetch(runSignal) {
1064
- if (runSignal.aborted) return;
1065
- const headers = { ...this.config.headers };
1066
- if (this.config.changeDetection !== "content-hash") {
1067
- if (this.lastEtag) {
1068
- headers["If-None-Match"] = this.lastEtag;
1069
- }
1070
- if (this.lastModified) {
1071
- headers["If-Modified-Since"] = this.lastModified;
1072
- }
1073
- }
1074
- const controller = new AbortController();
1075
- this._fetchController = controller;
1076
- const timeout = setTimeout(() => controller.abort(), this.config.fetchTimeoutMs);
1077
- try {
1078
- const response = await fetch(this.config.url, {
1079
- headers,
1080
- signal: controller.signal
1081
- });
1082
- if (runSignal.aborted) return;
1083
- if (response.status === 304) {
1084
- this.emit("unchanged");
1085
- return;
1086
- }
1087
- if (!response.ok) {
1088
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1089
- }
1090
- const etag = response.headers.get("etag");
1091
- const lastModified = response.headers.get("last-modified");
1092
- const body = await response.text();
1093
- if (runSignal.aborted) return;
1094
- const hash = await sha256Hex(body);
1095
- if (runSignal.aborted) return;
1096
- if (this.lastHash && this.lastHash === hash) {
1097
- this.emit("unchanged");
1098
- if (etag) this.lastEtag = etag;
1099
- if (lastModified) this.lastModified = lastModified;
1100
- return;
1101
- }
1102
- this.emit("changed", body, hash);
1103
- this.lastHash = hash;
1104
- if (etag) this.lastEtag = etag;
1105
- if (lastModified) this.lastModified = lastModified;
1106
- } finally {
1107
- clearTimeout(timeout);
1108
- if (this._fetchController === controller) {
1109
- this._fetchController = null;
1110
- }
1111
- }
1112
- }
1113
- /**
1114
- * Dispose the poller and release resources.
1115
- */
1116
- dispose() {
1117
- this.stop();
1118
- this.removeAllListeners();
1119
- }
1120
- };
1121
-
1122
- // libs/broker/src/openapi/openapi-tool-loader.ts
1123
- var import_zod2 = require("zod");
1124
- var OpenApiToolLoader = class _OpenApiToolLoader {
1125
- tools = [];
1126
- toolNames = /* @__PURE__ */ new Set();
1127
- specHash;
1128
- options;
1129
- auth;
1130
- constructor(spec, hash, options = {}, auth) {
1131
- this.options = options;
1132
- this.auth = auth;
1133
- this.specHash = hash;
1134
- this.loadFromSpec(spec);
1135
- }
1136
- /**
1137
- * Create a loader from a URL.
1138
- */
1139
- static async fromURL(url, options, auth) {
1140
- const headers = { ...options?.headers };
1141
- if (auth) {
1142
- switch (auth.type) {
1143
- case "bearer":
1144
- if (auth.token) headers["Authorization"] = `Bearer ${auth.token}`;
1145
- break;
1146
- case "api-key":
1147
- if (auth.token) headers[auth.header ?? "X-API-Key"] = auth.token;
1148
- break;
1149
- case "basic":
1150
- if (auth.token) headers["Authorization"] = `Basic ${auth.token}`;
1151
- break;
1152
- }
1153
- }
1154
- const response = await fetch(url, { headers });
1155
- if (!response.ok) {
1156
- throw new Error(`Failed to fetch OpenAPI spec from ${url}: ${response.status}`);
1157
- }
1158
- const spec = await response.json();
1159
- const hash = await sha256Hex(JSON.stringify(spec));
1160
- let resolvedBaseUrl = options?.baseUrl;
1161
- if (!resolvedBaseUrl) {
1162
- const servers = spec["servers"];
1163
- if (servers?.[0]?.url) {
1164
- try {
1165
- resolvedBaseUrl = new URL(servers[0].url, url).origin + new URL(servers[0].url, url).pathname;
1166
- } catch {
1167
- resolvedBaseUrl = new URL(url).origin;
1168
- }
1169
- } else {
1170
- resolvedBaseUrl = new URL(url).origin;
1171
- }
1172
- }
1173
- return new _OpenApiToolLoader(spec, hash, { ...options, baseUrl: resolvedBaseUrl }, auth);
1456
+ return new _OpenApiToolLoader(spec, hash, { ...options, baseUrl: resolvedBaseUrl }, auth);
1174
1457
  }
1175
1458
  /**
1176
1459
  * Create a loader from a spec object.
@@ -1198,6 +1481,23 @@ var OpenApiToolLoader = class _OpenApiToolLoader {
1198
1481
  auth
1199
1482
  );
1200
1483
  }
1484
+ /**
1485
+ * Put the upstream credential into a set of request headers.
1486
+ */
1487
+ static applyAuth(headers, auth) {
1488
+ if (!auth?.token) return;
1489
+ switch (auth.type) {
1490
+ case "bearer":
1491
+ setHeader(headers, "Authorization", `Bearer ${auth.token}`);
1492
+ break;
1493
+ case "api-key":
1494
+ setHeader(headers, auth.header ?? "X-API-Key", auth.token);
1495
+ break;
1496
+ case "basic":
1497
+ setHeader(headers, "Authorization", `Basic ${auth.token}`);
1498
+ break;
1499
+ }
1500
+ }
1201
1501
  /**
1202
1502
  * Get all loaded tool definitions.
1203
1503
  */
@@ -1390,19 +1690,46 @@ var OpenApiToolLoader = class _OpenApiToolLoader {
1390
1690
  createHandler(op, baseUrl, bodyMediaType) {
1391
1691
  const auth = this.auth;
1392
1692
  const headers = this.options.headers ?? {};
1693
+ const maxResponseBytes = positive(this.options.maxResponseBytes) ?? DEFAULT_MAX_RESPONSE_BYTES;
1694
+ const pathParams = (op.parameters ?? []).filter((param) => param.in === "path");
1393
1695
  return async (args, context) => {
1394
1696
  const params = args;
1697
+ const template = op.path.startsWith("/") ? op.path : "/" + op.path;
1698
+ const path = template.split("/").map((segment) => {
1699
+ if (!segment.includes("{")) return segment;
1700
+ let filled = segment;
1701
+ for (const param of pathParams) {
1702
+ const value = params[param.name];
1703
+ if (value === void 0) continue;
1704
+ filled = filled.split(`{${param.name}}`).join(encodeURIComponent(_OpenApiToolLoader.serializeParam(value)));
1705
+ }
1706
+ const plain = filled.replace(/%2e/gi, ".");
1707
+ if (plain === "" || plain === "." || plain === "..") {
1708
+ throw invalidPathParameter(op);
1709
+ }
1710
+ return filled;
1711
+ }).join("/");
1395
1712
  let normalizedBase = baseUrl;
1396
1713
  while (normalizedBase.endsWith("/")) normalizedBase = normalizedBase.slice(0, -1);
1397
- let url = `${normalizedBase}${op.path.startsWith("/") ? op.path : "/" + op.path}`;
1714
+ let url = `${normalizedBase}${path}`;
1715
+ let basePath;
1716
+ let requestPath;
1717
+ try {
1718
+ basePath = new URL(normalizedBase).pathname.replace(/\/+$/, "");
1719
+ requestPath = new URL(url).pathname;
1720
+ } catch {
1721
+ throw new Error(`Invalid request URL for ${op.method.toUpperCase()} ${op.path}`);
1722
+ }
1723
+ const expectedPath = basePath + path.split(/[?#]/)[0];
1724
+ if (requestPath.split("/").length !== expectedPath.split("/").length) {
1725
+ throw invalidPathParameter(op);
1726
+ }
1398
1727
  const queryParams = {};
1399
1728
  if (op.parameters) {
1400
1729
  for (const param of op.parameters) {
1401
1730
  const value = params[param.name];
1402
1731
  if (value === void 0) continue;
1403
- if (param.in === "path") {
1404
- url = url.replace(`{${param.name}}`, encodeURIComponent(_OpenApiToolLoader.serializeParam(value)));
1405
- } else if (param.in === "query") {
1732
+ if (param.in === "query") {
1406
1733
  queryParams[param.name] = _OpenApiToolLoader.serializeParam(value);
1407
1734
  }
1408
1735
  }
@@ -1414,181 +1741,385 @@ var OpenApiToolLoader = class _OpenApiToolLoader {
1414
1741
  const hasBody = (["post", "put", "patch", "delete"].includes(op.method) || op.requestBody != null) && Object.prototype.hasOwnProperty.call(params, "body") && params["body"] != null;
1415
1742
  const resolvedMediaType = bodyMediaType ?? "application/json";
1416
1743
  const isMultipart = resolvedMediaType === "multipart/form-data";
1417
- const requestHeaders = {
1418
- // Omit Content-Type for multipart/form-data to let the runtime set the boundary
1419
- ...hasBody && !isMultipart && { "Content-Type": resolvedMediaType },
1420
- ...headers
1421
- };
1422
- if (auth) {
1423
- switch (auth.type) {
1424
- case "bearer":
1425
- if (auth.token) requestHeaders["Authorization"] = `Bearer ${auth.token}`;
1426
- break;
1427
- case "api-key":
1428
- if (auth.token) requestHeaders[auth.header ?? "X-API-Key"] = auth.token;
1429
- break;
1430
- case "basic":
1431
- if (auth.token) requestHeaders["Authorization"] = `Basic ${auth.token}`;
1432
- break;
1433
- }
1434
- }
1435
- const protectedHeaders = /* @__PURE__ */ new Set();
1436
- if (auth) {
1437
- if (auth.type === "bearer" || auth.type === "basic") protectedHeaders.add("authorization");
1438
- if (auth.type === "api-key") protectedHeaders.add((auth.header ?? "X-API-Key").toLowerCase());
1744
+ const requestHeaders = {};
1745
+ if (hasBody && !isMultipart) {
1746
+ requestHeaders["Content-Type"] = resolvedMediaType;
1439
1747
  }
1440
1748
  if (op.parameters) {
1441
1749
  for (const param of op.parameters) {
1442
1750
  if (param.in === "header" && params[param.name] !== void 0) {
1443
- if (protectedHeaders.has(param.name.toLowerCase())) continue;
1444
- requestHeaders[param.name] = _OpenApiToolLoader.serializeParam(params[param.name]);
1751
+ setHeader(requestHeaders, param.name, _OpenApiToolLoader.serializeParam(params[param.name]));
1445
1752
  }
1446
1753
  }
1447
1754
  }
1448
- const fetchOptions = {
1449
- method: op.method.toUpperCase(),
1450
- headers: requestHeaders,
1451
- signal: context.signal
1452
- };
1755
+ for (const [name, value] of Object.entries(headers)) {
1756
+ setHeader(requestHeaders, name, value);
1757
+ }
1758
+ _OpenApiToolLoader.applyAuth(requestHeaders, auth);
1759
+ let body;
1453
1760
  if (hasBody) {
1454
1761
  if (_OpenApiToolLoader.isJsonContentType(resolvedMediaType)) {
1455
- fetchOptions.body = JSON.stringify(params["body"]);
1762
+ body = JSON.stringify(params["body"]);
1456
1763
  } else if (resolvedMediaType === "application/x-www-form-urlencoded") {
1457
- fetchOptions.body = new URLSearchParams(params["body"]).toString();
1764
+ body = new URLSearchParams(params["body"]).toString();
1458
1765
  } else if (resolvedMediaType === "multipart/form-data") {
1459
1766
  const formData = new FormData();
1460
1767
  const bodyObj = params["body"];
1461
1768
  for (const [key, value] of Object.entries(bodyObj)) {
1462
1769
  formData.append(key, value instanceof Blob ? value : String(value));
1463
1770
  }
1464
- fetchOptions.body = formData;
1771
+ body = formData;
1465
1772
  } else if (resolvedMediaType.startsWith("text/")) {
1466
- fetchOptions.body = String(params["body"]);
1773
+ body = String(params["body"]);
1467
1774
  } else {
1468
- fetchOptions.body = JSON.stringify(params["body"]);
1775
+ body = JSON.stringify(params["body"]);
1469
1776
  }
1470
1777
  }
1471
- const response = await fetch(url, fetchOptions);
1778
+ const response = await fetchOriginBound(url, {
1779
+ method: op.method.toUpperCase(),
1780
+ headers: requestHeaders,
1781
+ body,
1782
+ signal: context.signal
1783
+ });
1472
1784
  if (!response.ok) {
1473
1785
  const contentType2 = response.headers.get("content-type") ?? "";
1474
- let body;
1786
+ let errorBody;
1475
1787
  try {
1476
- body = _OpenApiToolLoader.isJsonContentType(contentType2) ? JSON.stringify(await response.json()) : await response.text();
1788
+ errorBody = await readBodyText(response, MAX_ERROR_BODY_BYTES, true);
1789
+ if (_OpenApiToolLoader.isJsonContentType(contentType2)) {
1790
+ try {
1791
+ errorBody = JSON.stringify(JSON.parse(errorBody));
1792
+ } catch {
1793
+ }
1794
+ }
1477
1795
  } catch {
1478
- body = "";
1796
+ errorBody = "";
1479
1797
  }
1480
- throw new Error(`HTTP ${response.status} ${response.statusText}: ${body}`);
1798
+ throw new Error(`HTTP ${response.status} ${response.statusText.slice(0, 128)}: ${errorBody}`);
1481
1799
  }
1482
1800
  const contentType = response.headers.get("content-type") ?? "";
1801
+ const text = await readBodyText(response, maxResponseBytes);
1483
1802
  if (_OpenApiToolLoader.isJsonContentType(contentType)) {
1484
- return response.json();
1803
+ return JSON.parse(text);
1485
1804
  }
1486
- return response.text();
1805
+ return text;
1487
1806
  };
1488
1807
  }
1489
1808
  };
1809
+ function positive(value) {
1810
+ return typeof value === "number" && value > 0 ? value : void 0;
1811
+ }
1812
+ function setHeader(headers, name, value) {
1813
+ const lower = name.toLowerCase();
1814
+ for (const existing of Object.keys(headers)) {
1815
+ if (existing.toLowerCase() === lower) delete headers[existing];
1816
+ }
1817
+ headers[name] = value;
1818
+ }
1819
+ function invalidPathParameter(op) {
1820
+ const error = new Error(`Invalid path parameter for ${op.method.toUpperCase()} ${op.path}`);
1821
+ error.code = "INVALID_PATH_PARAMETER";
1822
+ return error;
1823
+ }
1490
1824
 
1491
- // libs/broker/src/openapi/openapi-source.ts
1492
- var OpenApiSource = class extends import_events2.EventEmitter {
1493
- toolRegistry;
1825
+ // libs/broker/src/openapi/openapi-spec-poller.ts
1826
+ var DEFAULT_RETRY = {
1827
+ maxRetries: 3,
1828
+ initialDelayMs: 1e3,
1829
+ maxDelayMs: 1e4,
1830
+ backoffMultiplier: 2
1831
+ };
1832
+ var OpenApiSpecPoller = class extends import_events.EventEmitter {
1494
1833
  config;
1495
- poller = null;
1496
- previousToolNames = /* @__PURE__ */ new Set();
1497
- lastUpdate = "";
1498
- specHash = "";
1499
- health = "unknown";
1500
- disposed = false;
1501
- _syncLock = Promise.resolve();
1502
- constructor(toolRegistry, config) {
1834
+ intervalTimer = null;
1835
+ lastHash = null;
1836
+ lastEtag = null;
1837
+ lastModified = null;
1838
+ consecutiveFailures = 0;
1839
+ isPolling = false;
1840
+ wasUnhealthy = false;
1841
+ /** Per-run abort controller — aborted on stop(), recreated on start() */
1842
+ _runAbort = new AbortController();
1843
+ _fetchController = null;
1844
+ _activePoll = null;
1845
+ constructor(config) {
1503
1846
  super();
1504
- this.toolRegistry = toolRegistry;
1505
- this.config = config;
1847
+ this.config = {
1848
+ url: config.url,
1849
+ intervalMs: config.intervalMs ?? 6e4,
1850
+ fetchTimeoutMs: config.fetchTimeoutMs ?? 1e4,
1851
+ changeDetection: config.changeDetection ?? "auto",
1852
+ unhealthyThreshold: config.unhealthyThreshold ?? 3,
1853
+ retry: { ...DEFAULT_RETRY, ...config.retry },
1854
+ headers: config.headers ?? {},
1855
+ maxSpecBytes: typeof config.maxSpecBytes === "number" && config.maxSpecBytes > 0 ? config.maxSpecBytes : DEFAULT_MAX_SPEC_BYTES
1856
+ };
1506
1857
  }
1507
1858
  /**
1508
- * Initial load + start polling.
1859
+ * Start polling for changes.
1509
1860
  */
1510
- async start() {
1511
- if (this.disposed) throw new Error("OpenApiSource is disposed");
1512
- await this.refresh();
1513
- this.poller = new OpenApiSpecPoller(this.config);
1514
- this.poller.on("changed", (spec) => {
1515
- this._syncLock = this._syncLock.then(async () => {
1516
- try {
1517
- const parsed = JSON.parse(spec);
1518
- await this.syncTools(parsed);
1519
- this.health = "healthy";
1520
- } catch (error) {
1521
- this.emit("error", error instanceof Error ? error : new Error(String(error)));
1861
+ start() {
1862
+ if (this.intervalTimer) return;
1863
+ this._runAbort = new AbortController();
1864
+ const runSignal = this._runAbort.signal;
1865
+ const pendingPoll = this._activePoll;
1866
+ if (pendingPoll) {
1867
+ pendingPoll.finally(() => {
1868
+ if (!runSignal.aborted) {
1869
+ this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
1522
1870
  }
1523
1871
  });
1524
- });
1525
- this.poller.on("unhealthy", (failures) => {
1526
- this.health = "unhealthy";
1527
- this.emit("unhealthy", failures);
1528
- });
1529
- this.poller.on("recovered", () => {
1530
- this.health = "healthy";
1531
- this.emit("recovered");
1532
- });
1533
- this.poller.on("pollError", (error) => {
1534
- this.emit("error", error);
1535
- });
1536
- this.poller.start();
1872
+ } else {
1873
+ this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
1874
+ }
1875
+ this.intervalTimer = setInterval(() => {
1876
+ this._activePoll = this.pollWithSignal(runSignal).catch(() => void 0);
1877
+ }, this.config.intervalMs);
1537
1878
  }
1538
1879
  /**
1539
1880
  * Stop polling.
1540
1881
  */
1541
1882
  stop() {
1542
- if (this.poller) {
1543
- this.poller.stop();
1883
+ this._runAbort.abort();
1884
+ if (this._fetchController) {
1885
+ this._fetchController.abort();
1886
+ this._fetchController = null;
1887
+ }
1888
+ if (this.intervalTimer) {
1889
+ clearInterval(this.intervalTimer);
1890
+ this.intervalTimer = null;
1544
1891
  }
1545
1892
  }
1546
1893
  /**
1547
- * Manual refresh (for webhook triggers).
1894
+ * Get the current content hash.
1548
1895
  */
1549
- async refresh() {
1550
- if (this.disposed) throw new Error("OpenApiSource is disposed");
1551
- const loader = await OpenApiToolLoader.fromURL(
1552
- this.config.url,
1553
- {
1554
- ...this.config.loaderOptions,
1555
- baseUrl: this.config.baseUrl,
1556
- sourceName: this.config.name
1557
- },
1558
- this.config.auth
1559
- );
1560
- const tools = loader.getTools();
1561
- const newToolNames = loader.getToolNames();
1562
- const oldToolNames = this.previousToolNames;
1563
- for (const tool of tools) {
1564
- if (this.toolRegistry.has(tool.name)) {
1565
- this.toolRegistry.replace(tool);
1566
- } else {
1567
- this.toolRegistry.register(tool);
1568
- }
1569
- }
1570
- for (const name of oldToolNames) {
1571
- if (!newToolNames.has(name)) {
1572
- this.toolRegistry.unregister(name);
1896
+ getHash() {
1897
+ return this.lastHash;
1898
+ }
1899
+ /**
1900
+ * Get the number of consecutive failures.
1901
+ */
1902
+ getConsecutiveFailures() {
1903
+ return this.consecutiveFailures;
1904
+ }
1905
+ /**
1906
+ * Perform a single poll cycle.
1907
+ */
1908
+ async poll() {
1909
+ return this.pollWithSignal(this._runAbort.signal);
1910
+ }
1911
+ async pollWithSignal(runSignal) {
1912
+ if (this.isPolling) return;
1913
+ this.isPolling = true;
1914
+ try {
1915
+ await this.fetchWithRetry(runSignal);
1916
+ } finally {
1917
+ this.isPolling = false;
1918
+ this._activePoll = null;
1919
+ }
1920
+ }
1921
+ async fetchWithRetry(runSignal) {
1922
+ const { maxRetries, initialDelayMs, maxDelayMs, backoffMultiplier } = this.config.retry;
1923
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
1924
+ if (runSignal.aborted) return;
1925
+ try {
1926
+ await this.doFetch(runSignal);
1927
+ if (runSignal.aborted) return;
1928
+ if (this.consecutiveFailures > 0) {
1929
+ this.consecutiveFailures = 0;
1930
+ if (this.wasUnhealthy) {
1931
+ this.wasUnhealthy = false;
1932
+ this.emit("recovered");
1933
+ }
1934
+ }
1935
+ return;
1936
+ } catch (error) {
1937
+ if (runSignal.aborted) return;
1938
+ if (attempt === maxRetries) {
1939
+ this.consecutiveFailures++;
1940
+ this.emit("pollError", error instanceof Error ? error : new Error(String(error)));
1941
+ if (this.consecutiveFailures >= this.config.unhealthyThreshold && !this.wasUnhealthy) {
1942
+ this.wasUnhealthy = true;
1943
+ this.emit("unhealthy", this.consecutiveFailures);
1944
+ }
1945
+ return;
1946
+ }
1947
+ const delay = Math.min(initialDelayMs * Math.pow(backoffMultiplier, attempt), maxDelayMs);
1948
+ await new Promise((resolve) => {
1949
+ const onAbort = () => {
1950
+ clearTimeout(timer);
1951
+ resolve();
1952
+ };
1953
+ const timer = setTimeout(() => {
1954
+ runSignal.removeEventListener("abort", onAbort);
1955
+ resolve();
1956
+ }, delay);
1957
+ runSignal.addEventListener("abort", onAbort, { once: true });
1958
+ });
1573
1959
  }
1574
1960
  }
1575
- this.previousToolNames = newToolNames;
1576
- this.specHash = loader.getSpecHash();
1577
- this.lastUpdate = (/* @__PURE__ */ new Date()).toISOString();
1578
- this.health = "healthy";
1579
- const added = [...newToolNames].filter((n) => !oldToolNames.has(n));
1580
- const removed = [...oldToolNames].filter((n) => !newToolNames.has(n));
1581
- this.emit("toolsUpdated", {
1582
- added,
1583
- removed,
1584
- updated: [...newToolNames].filter((n) => oldToolNames.has(n))
1585
- });
1586
- if (added.length > 0 || removed.length > 0) {
1587
- this.emit("catalogChanged", {
1588
- version: this.specHash,
1589
- addedActions: added,
1590
- removedActions: removed
1961
+ }
1962
+ async doFetch(runSignal) {
1963
+ if (runSignal.aborted) return;
1964
+ const headers = { ...this.config.headers };
1965
+ const validators = {};
1966
+ if (this.config.changeDetection !== "content-hash") {
1967
+ if (this.lastEtag) {
1968
+ validators["If-None-Match"] = this.lastEtag;
1969
+ }
1970
+ if (this.lastModified) {
1971
+ validators["If-Modified-Since"] = this.lastModified;
1972
+ }
1973
+ }
1974
+ const controller = new AbortController();
1975
+ this._fetchController = controller;
1976
+ const timeout = setTimeout(() => controller.abort(), this.config.fetchTimeoutMs);
1977
+ try {
1978
+ const response = await fetchOriginBound(this.config.url, {
1979
+ method: "GET",
1980
+ headers,
1981
+ portableHeaders: validators,
1982
+ // A redirect that comes back to the specification's origin gets its headers again
1983
+ restoreOnOrigin: true,
1984
+ signal: controller.signal
1591
1985
  });
1986
+ if (runSignal.aborted) return;
1987
+ if (response.status === 304) {
1988
+ this.emit("unchanged");
1989
+ return;
1990
+ }
1991
+ if (!response.ok) {
1992
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1993
+ }
1994
+ const etag = response.headers.get("etag");
1995
+ const lastModified = response.headers.get("last-modified");
1996
+ const body = await readBodyText(response, this.config.maxSpecBytes);
1997
+ if (runSignal.aborted) return;
1998
+ const hash = await sha256Hex(body);
1999
+ if (runSignal.aborted) return;
2000
+ if (this.lastHash && this.lastHash === hash) {
2001
+ this.emit("unchanged");
2002
+ if (etag) this.lastEtag = etag;
2003
+ if (lastModified) this.lastModified = lastModified;
2004
+ return;
2005
+ }
2006
+ this.emit("changed", body, hash);
2007
+ this.lastHash = hash;
2008
+ if (etag) this.lastEtag = etag;
2009
+ if (lastModified) this.lastModified = lastModified;
2010
+ } finally {
2011
+ clearTimeout(timeout);
2012
+ if (this._fetchController === controller) {
2013
+ this._fetchController = null;
2014
+ }
2015
+ }
2016
+ }
2017
+ /**
2018
+ * Dispose the poller and release resources.
2019
+ */
2020
+ dispose() {
2021
+ this.stop();
2022
+ this.removeAllListeners();
2023
+ }
2024
+ };
2025
+
2026
+ // libs/broker/src/openapi/openapi-source.ts
2027
+ var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
2028
+ var OpenApiSource = class extends import_events2.EventEmitter {
2029
+ toolRegistry;
2030
+ config;
2031
+ poller = null;
2032
+ previousToolNames = /* @__PURE__ */ new Set();
2033
+ lastUpdate = "";
2034
+ specHash = "";
2035
+ loaded = false;
2036
+ syncFailed = false;
2037
+ pollFailing = false;
2038
+ lastError;
2039
+ disposed = false;
2040
+ _syncLock = Promise.resolve();
2041
+ constructor(toolRegistry, config) {
2042
+ super();
2043
+ this.toolRegistry = toolRegistry;
2044
+ this.config = config;
2045
+ }
2046
+ /**
2047
+ * Initial load + start polling.
2048
+ *
2049
+ * A failure while polling (a specification that cannot be fetched, parsed or applied) is
2050
+ * reported with the `error` event when it has a listener, and always through `getStats()`:
2051
+ * `pollHealth` becomes `unhealthy` and `lastError` holds the message. The tools of the last
2052
+ * specification that was applied stay registered.
2053
+ */
2054
+ async start() {
2055
+ if (this.disposed) throw new Error("OpenApiSource is disposed");
2056
+ await this.refresh();
2057
+ this.poller = new OpenApiSpecPoller(this.config);
2058
+ this.poller.on("changed", (spec) => {
2059
+ this._syncLock = this._syncLock.then(async () => {
2060
+ if (this.disposed) return;
2061
+ const parsed = JSON.parse(spec);
2062
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
2063
+ throw new Error("The OpenAPI spec is not a JSON object");
2064
+ }
2065
+ await this.syncTools(parsed);
2066
+ }).catch((error) => {
2067
+ this.syncFailed = true;
2068
+ this.reportError(error);
2069
+ });
2070
+ });
2071
+ this.poller.on("unhealthy", (failures) => {
2072
+ this.pollFailing = true;
2073
+ this.notify("unhealthy", failures);
2074
+ });
2075
+ this.poller.on("recovered", () => {
2076
+ this.pollFailing = false;
2077
+ this.notify("recovered");
2078
+ });
2079
+ this.poller.on("pollError", (error) => {
2080
+ this.reportError(error);
2081
+ });
2082
+ this.poller.on("unchanged", () => {
2083
+ if (!this.syncFailed) this.lastError = void 0;
2084
+ });
2085
+ this.poller.start();
2086
+ }
2087
+ /**
2088
+ * Stop polling.
2089
+ */
2090
+ stop() {
2091
+ if (this.poller) {
2092
+ this.poller.stop();
2093
+ }
2094
+ }
2095
+ /**
2096
+ * Manual refresh (for webhook triggers).
2097
+ *
2098
+ * @throws when the specification cannot be fetched or applied; the registered tools are
2099
+ * then what they were before
2100
+ */
2101
+ async refresh() {
2102
+ if (this.disposed) throw new Error("OpenApiSource is disposed");
2103
+ try {
2104
+ const loader = await OpenApiToolLoader.fromURL(
2105
+ this.config.url,
2106
+ {
2107
+ // The request for the specification is the one the poller sends: same headers,
2108
+ // same timeout, same size limit.
2109
+ specHeaders: this.config.headers,
2110
+ specTimeoutMs: this.config.fetchTimeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS,
2111
+ maxSpecBytes: this.config.maxSpecBytes,
2112
+ ...this.config.loaderOptions,
2113
+ baseUrl: this.config.baseUrl,
2114
+ sourceName: this.config.name
2115
+ },
2116
+ this.config.auth
2117
+ );
2118
+ this.applyTools(loader);
2119
+ } catch (error) {
2120
+ this.syncFailed = true;
2121
+ this.lastError = error instanceof Error ? error : new Error(String(error));
2122
+ throw error;
1592
2123
  }
1593
2124
  }
1594
2125
  /**
@@ -1604,21 +2135,32 @@ var OpenApiSource = class extends import_events2.EventEmitter {
1604
2135
  },
1605
2136
  this.config.auth
1606
2137
  );
2138
+ this.applyTools(loader);
2139
+ }
2140
+ /**
2141
+ * Replace the tools of this source with the tools of a loaded specification, in one step:
2142
+ * either every tool of the new specification is registered and every tool that is gone is
2143
+ * removed, or nothing changes. A tool this source does not manage is never replaced.
2144
+ */
2145
+ applyTools(loader) {
1607
2146
  const newTools = loader.getTools();
1608
2147
  const newToolNames = loader.getToolNames();
2148
+ const oldToolNames = this.previousToolNames;
2149
+ for (const tool of newTools) {
2150
+ if (!oldToolNames.has(tool.name) && this.toolRegistry.has(tool.name)) {
2151
+ throw new Error(
2152
+ `Tool "${tool.name}" is already registered and is not managed by OpenAPI source "${this.config.name}"`
2153
+ );
2154
+ }
2155
+ }
1609
2156
  const added = [];
1610
2157
  const removed = [];
1611
2158
  const updated = [];
1612
2159
  for (const tool of newTools) {
1613
- if (this.previousToolNames.has(tool.name)) {
1614
- this.toolRegistry.replace(tool);
1615
- updated.push(tool.name);
1616
- } else {
1617
- this.toolRegistry.register(tool);
1618
- added.push(tool.name);
1619
- }
2160
+ this.toolRegistry.replace(tool);
2161
+ (oldToolNames.has(tool.name) ? updated : added).push(tool.name);
1620
2162
  }
1621
- for (const name of this.previousToolNames) {
2163
+ for (const name of oldToolNames) {
1622
2164
  if (!newToolNames.has(name)) {
1623
2165
  this.toolRegistry.unregister(name);
1624
2166
  removed.push(name);
@@ -1627,16 +2169,39 @@ var OpenApiSource = class extends import_events2.EventEmitter {
1627
2169
  this.previousToolNames = newToolNames;
1628
2170
  this.specHash = loader.getSpecHash();
1629
2171
  this.lastUpdate = (/* @__PURE__ */ new Date()).toISOString();
2172
+ this.loaded = true;
2173
+ this.syncFailed = false;
2174
+ this.lastError = void 0;
1630
2175
  const event = { added, removed, updated };
1631
- this.emit("toolsUpdated", event);
2176
+ this.notify("toolsUpdated", event);
1632
2177
  if (added.length > 0 || removed.length > 0) {
1633
- this.emit("catalogChanged", {
2178
+ this.notify("catalogChanged", {
1634
2179
  version: this.specHash,
1635
2180
  addedActions: added,
1636
2181
  removedActions: removed
1637
2182
  });
1638
2183
  }
1639
2184
  }
2185
+ /**
2186
+ * Record a failure and tell the `error` listeners, if there are any. Without a listener the
2187
+ * failure is kept for `getStats()`; it is never raised from here.
2188
+ */
2189
+ reportError(error) {
2190
+ this.lastError = error instanceof Error ? error : new Error(String(error));
2191
+ if (this.listenerCount("error") > 0) {
2192
+ this.notify("error", this.lastError);
2193
+ }
2194
+ }
2195
+ /**
2196
+ * Emit an event. A listener that throws does not stop the source.
2197
+ */
2198
+ notify(event, ...args) {
2199
+ if (event === "error" && this.listenerCount("error") === 0) return;
2200
+ try {
2201
+ this.emit(event, ...args);
2202
+ } catch {
2203
+ }
2204
+ }
1640
2205
  /**
1641
2206
  * Get the set of tool names managed by this source.
1642
2207
  */
@@ -1647,11 +2212,13 @@ var OpenApiSource = class extends import_events2.EventEmitter {
1647
2212
  * Get source statistics.
1648
2213
  */
1649
2214
  getStats() {
2215
+ const failing = this.syncFailed || this.pollFailing;
1650
2216
  return {
1651
2217
  toolCount: this.previousToolNames.size,
1652
2218
  lastUpdate: this.lastUpdate,
1653
2219
  specHash: this.specHash,
1654
- pollHealth: this.health
2220
+ pollHealth: failing ? "unhealthy" : this.loaded ? "healthy" : "unknown",
2221
+ ...this.lastError && { lastError: this.lastError.message }
1655
2222
  };
1656
2223
  }
1657
2224
  /**
@@ -1676,23 +2243,44 @@ var OpenApiSource = class extends import_events2.EventEmitter {
1676
2243
  };
1677
2244
 
1678
2245
  // libs/broker/src/broker.ts
2246
+ var KNOWN_CONFIG_KEYS = ["sessions", "enclave", "limits", "history"];
1679
2247
  var Broker = class {
1680
2248
  toolRegistry;
1681
2249
  sessionManager;
1682
2250
  config;
1683
2251
  openApiSources = [];
2252
+ defaultLimits;
1684
2253
  disposed = false;
1685
2254
  constructor(config = {}) {
2255
+ for (const key of Object.keys(config)) {
2256
+ if (!KNOWN_CONFIG_KEYS.includes(key)) {
2257
+ console.warn(
2258
+ `[@enclave-vm/broker] Broker: unknown configuration key ${JSON.stringify(key.slice(0, 64))} is ignored`
2259
+ );
2260
+ }
2261
+ }
1686
2262
  this.config = config;
1687
2263
  this.toolRegistry = createToolRegistry();
2264
+ const sessionDefaults = config.sessions?.defaultSessionConfig ?? {};
2265
+ const limits = mergeSessionLimits(sessionDefaults.limits, config.limits);
2266
+ this.defaultLimits = Object.freeze(resolveSessionLimits(limits));
1688
2267
  this.sessionManager = createSessionManager(this.toolRegistry, {
1689
2268
  ...config.sessions,
1690
2269
  defaultSessionConfig: {
1691
- limits: config.limits,
1692
- enclaveConfig: config.enclave
2270
+ ...sessionDefaults,
2271
+ limits,
2272
+ enclaveConfig: config.enclave ?? sessionDefaults.enclaveConfig,
2273
+ history: resolveHistoryLimits({ ...sessionDefaults.history, ...config.history })
1693
2274
  }
1694
2275
  });
1695
2276
  }
2277
+ /**
2278
+ * The limits of a session that names none: the broker's configuration over the package
2279
+ * defaults. The HTTP routes use them as the ceiling of what a request may ask for.
2280
+ */
2281
+ get sessionLimits() {
2282
+ return this.defaultLimits;
2283
+ }
1696
2284
  // ============================================================================
1697
2285
  // Tool Registration
1698
2286
  // ============================================================================
@@ -1871,8 +2459,8 @@ var Broker = class {
1871
2459
  /**
1872
2460
  * Terminate a session
1873
2461
  */
1874
- async terminateSession(sessionId, reason) {
1875
- return this.sessionManager.terminate(sessionId, reason);
2462
+ async terminateSession(sessionId, reason, expectedSession) {
2463
+ return this.sessionManager.terminate(sessionId, reason, expectedSession);
1876
2464
  }
1877
2465
  // ============================================================================
1878
2466
  // Quick Execution
@@ -1959,41 +2547,72 @@ function createBroker(config) {
1959
2547
  return new Broker(config);
1960
2548
  }
1961
2549
 
2550
+ // libs/broker/src/http/session-handler.ts
2551
+ var import_types5 = require("@enclave-vm/types");
2552
+
1962
2553
  // libs/broker/src/event-filter.ts
1963
- var import_types2 = require("@enclave-vm/types");
2554
+ var import_node_vm = require("node:vm");
2555
+ var import_types3 = require("@enclave-vm/types");
1964
2556
  var import_minimatch = require("minimatch");
1965
- var MAX_REGEX_INPUT_LENGTH = 1e4;
1966
- var MAX_PATTERN_LENGTH_FOR_REDOS_CHECK = 500;
1967
- var REDOS_DETECTION_PATTERNS = {
1968
- /** Check for nested quantifiers: (a+)+ or (a*)* */
1969
- nestedQuantifiers: /\([^)]{0,100}[*+{][^)]{0,100}\)[*+{]/,
1970
- /** Check for alternation with overlapping patterns: (a|ab)* */
1971
- alternationOverlap: /\([^|]{0,100}\|[^)]{0,100}\)[*+{]/,
1972
- /** Check for repeated groups with quantifiers: (a+)+ */
1973
- repeatedGroups: /\([^)]{0,100}[*+][^)]{0,100}\)[*+]/
1974
- };
1975
- function isPotentiallyVulnerableRegex(pattern) {
1976
- const safePattern = pattern.length > MAX_PATTERN_LENGTH_FOR_REDOS_CHECK ? pattern.slice(0, MAX_PATTERN_LENGTH_FOR_REDOS_CHECK) : pattern;
2557
+ var DEFAULT_EVALUATION_TIMEOUT_MS = 100;
2558
+ var MAX_EVALUATION_TIMEOUT_MS = 1e4;
2559
+ var MAX_EVALUATION_ATTEMPTS = 5;
2560
+ var STARVED_CPU_SHARE = 0.5;
2561
+ function cpuTimeMs() {
2562
+ const perThread = process.threadCpuUsage;
2563
+ const usage = typeof perThread === "function" ? perThread.call(process) : process.cpuUsage();
2564
+ return (usage.user + usage.system) / 1e3;
2565
+ }
2566
+ var ANY = 0;
2567
+ var ONE = 1;
2568
+ var boundedRealm = {};
2569
+ var boundedScript;
2570
+ var insideBounded = false;
2571
+ function runBounded(fn, timeoutMs) {
2572
+ if (insideBounded) {
2573
+ return { done: true, value: fn() };
2574
+ }
2575
+ if (!boundedScript) {
2576
+ (0, import_node_vm.createContext)(boundedRealm);
2577
+ boundedScript = new import_node_vm.Script("run()");
2578
+ }
2579
+ boundedRealm.run = fn;
2580
+ insideBounded = true;
1977
2581
  try {
1978
- if (REDOS_DETECTION_PATTERNS.nestedQuantifiers.test(safePattern)) return true;
1979
- if (REDOS_DETECTION_PATTERNS.alternationOverlap.test(safePattern)) return true;
1980
- if (REDOS_DETECTION_PATTERNS.repeatedGroups.test(safePattern)) return true;
1981
- } catch {
1982
- return false;
2582
+ return { done: true, value: boundedScript.runInContext(boundedRealm, { timeout: timeoutMs }) };
2583
+ } catch (error) {
2584
+ if (error?.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") {
2585
+ return { done: false };
2586
+ }
2587
+ throw error;
2588
+ } finally {
2589
+ insideBounded = false;
2590
+ boundedRealm.run = void 0;
1983
2591
  }
1984
- return false;
1985
2592
  }
1986
2593
  var EventFilter = class {
1987
- config;
2594
+ exclude;
1988
2595
  alwaysAllow;
1989
- compiledRegexes;
2596
+ rules;
2597
+ timeoutMs;
2598
+ budgetMs;
2599
+ expandGlobs;
1990
2600
  onError;
2601
+ spentMs = 0;
1991
2602
  constructor(options) {
1992
- this.config = options.config;
2603
+ const config = options.config;
2604
+ if (typeof config !== "object" || config === null || !Array.isArray(config.rules)) {
2605
+ throw new TypeError('EventFilter: the configuration needs a "rules" array');
2606
+ }
1993
2607
  this.onError = options.onError;
1994
- this.alwaysAllow = new Set(options.config.alwaysAllow ?? import_types2.DEFAULT_ALWAYS_ALLOW);
1995
- this.compiledRegexes = /* @__PURE__ */ new Map();
1996
- this.compilePatterns();
2608
+ this.exclude = config.mode !== import_types3.FilterMode.Include;
2609
+ this.alwaysAllow = new Set(config.alwaysAllow ?? import_types3.DEFAULT_ALWAYS_ALLOW);
2610
+ this.expandGlobs = options.expandGlobs ?? true;
2611
+ const timeout = config.regexTimeoutMs;
2612
+ this.timeoutMs = typeof timeout === "number" && timeout > 0 ? Math.min(Math.max(Math.ceil(timeout), 1), MAX_EVALUATION_TIMEOUT_MS) : DEFAULT_EVALUATION_TIMEOUT_MS;
2613
+ const budget = options.budgetMs;
2614
+ this.budgetMs = typeof budget === "number" && budget >= 0 ? budget : void 0;
2615
+ this.rules = config.rules.map((rule) => this.compileRule(rule));
1997
2616
  }
1998
2617
  /**
1999
2618
  * Check if an event should be sent to the client.
@@ -2002,12 +2621,30 @@ var EventFilter = class {
2002
2621
  if (this.alwaysAllow.has(event.type)) {
2003
2622
  return true;
2004
2623
  }
2005
- const matches = this.matchesAnyRule(event);
2006
- if (this.config.mode === import_types2.FilterMode.Include) {
2007
- return matches;
2008
- } else {
2009
- return !matches;
2624
+ const subjects = /* @__PURE__ */ new Map();
2625
+ const send = this.decide(event, subjects);
2626
+ if (this.onError) {
2627
+ for (const subject of subjects.values()) {
2628
+ if (subject.error) this.onError(subject.error, event);
2629
+ }
2630
+ }
2631
+ return send;
2632
+ }
2633
+ /**
2634
+ * Decide one event that is not always allowed.
2635
+ */
2636
+ decide(event, subjects) {
2637
+ let matches = this.matchesAnyRule(event, subjects, false);
2638
+ if (matches === void 0) {
2639
+ if (this.budgetUsedUp()) {
2640
+ return false;
2641
+ }
2642
+ matches = this.evaluateWithinLimit(event, subjects);
2643
+ if (matches === void 0) {
2644
+ return false;
2645
+ }
2010
2646
  }
2647
+ return this.exclude ? !matches : matches;
2011
2648
  }
2012
2649
  /**
2013
2650
  * Filter an array of events.
@@ -2016,165 +2653,308 @@ var EventFilter = class {
2016
2653
  return events.filter((event) => this.shouldSend(event));
2017
2654
  }
2018
2655
  /**
2019
- * Check if event matches any filter rule.
2656
+ * Whether the budget of this filter is used up.
2020
2657
  */
2021
- matchesAnyRule(event) {
2022
- return this.config.rules.some((rule) => this.matchesRule(event, rule));
2658
+ budgetUsedUp() {
2659
+ return this.budgetMs !== void 0 && this.spentMs >= this.budgetMs;
2023
2660
  }
2024
2661
  /**
2025
- * Check if event matches a specific rule.
2026
- */
2027
- matchesRule(event, rule) {
2028
- const typeMatch = rule.types ? this.matchesTypeFilter(event, rule.types) : true;
2029
- const contentMatch = rule.content ? this.matchesContentFilter(event, rule.content) : true;
2030
- if (rule.types && rule.content) {
2031
- return typeMatch && contentMatch;
2662
+ * Evaluate all rules, slow patterns included, under the time limit. Returns undefined when
2663
+ * the evaluation did not finish.
2664
+ *
2665
+ * The limit is a limit on elapsed time. When it is reached although the thread got little
2666
+ * processor time meanwhile (the machine was busy with something else), the evaluation is
2667
+ * tried again, a bounded number of times, so that a busy machine does not drop events. An
2668
+ * evaluation that used its time on a processor is not tried again.
2669
+ */
2670
+ evaluateWithinLimit(event, subjects) {
2671
+ for (let attempt = 1; ; attempt++) {
2672
+ let limit = this.timeoutMs;
2673
+ if (this.budgetMs !== void 0) {
2674
+ limit = Math.max(1, Math.min(limit, Math.ceil(this.budgetMs - this.spentMs)));
2675
+ }
2676
+ const cpuBefore = cpuTimeMs();
2677
+ const outcome = runBounded(() => {
2678
+ const started = cpuTimeMs();
2679
+ const matches = this.matchesAnyRule(event, subjects, true) === true;
2680
+ return { matches, used: cpuTimeMs() - started };
2681
+ }, limit);
2682
+ if (outcome.done) {
2683
+ this.spentMs += outcome.value.used;
2684
+ return outcome.value.matches;
2685
+ }
2686
+ this.recompileGlobs();
2687
+ const cpuMs = cpuTimeMs() - cpuBefore;
2688
+ const starved = cpuMs < limit * STARVED_CPU_SHARE;
2689
+ this.spentMs += starved ? cpuMs : limit;
2690
+ if (starved && attempt < MAX_EVALUATION_ATTEMPTS && !this.budgetUsedUp()) {
2691
+ continue;
2692
+ }
2693
+ this.onError?.(new Error("Event filter evaluation exceeded its time limit"), event);
2694
+ return void 0;
2032
2695
  }
2033
- return rule.types ? typeMatch : contentMatch;
2034
2696
  }
2035
2697
  /**
2036
- * Check if event type matches filter.
2698
+ * Check if event matches any filter rule. With `slow` false, patterns that need the time
2699
+ * limit are put off and the answer is undefined when it depends on one of them.
2037
2700
  */
2038
- matchesTypeFilter(event, filter) {
2039
- return filter.types.includes(event.type);
2701
+ matchesAnyRule(event, subjects, slow) {
2702
+ let putOff = false;
2703
+ for (const rule of this.rules) {
2704
+ const match = this.matchesRule(event, rule, subjects, slow);
2705
+ if (match === true) return true;
2706
+ if (match === void 0) putOff = true;
2707
+ }
2708
+ return putOff ? void 0 : false;
2040
2709
  }
2041
2710
  /**
2042
- * Check if event content matches filter.
2711
+ * Check if event matches a specific rule. If the rule has both a type filter and a content
2712
+ * filter, both must match.
2043
2713
  */
2044
- matchesContentFilter(event, filter) {
2045
- const matchMode = filter.match ?? "any";
2046
- if (matchMode === "all") {
2047
- return filter.patterns.every((pattern) => this.matchesPattern(event, pattern));
2048
- } else {
2049
- return filter.patterns.some((pattern) => this.matchesPattern(event, pattern));
2714
+ matchesRule(event, rule, subjects, slow) {
2715
+ if (rule.types && !rule.types.has(event.type)) {
2716
+ return false;
2717
+ }
2718
+ if (!rule.patterns) {
2719
+ return true;
2050
2720
  }
2721
+ let putOff = false;
2722
+ for (const pattern of rule.patterns) {
2723
+ const match = this.matchesPattern(event, pattern, subjects, slow);
2724
+ if (match === void 0) {
2725
+ putOff = true;
2726
+ } else if (match !== rule.matchAll) {
2727
+ return match;
2728
+ }
2729
+ }
2730
+ return putOff ? void 0 : rule.matchAll;
2051
2731
  }
2052
2732
  /**
2053
2733
  * Check if event matches a content pattern.
2054
2734
  */
2055
- matchesPattern(event, pattern) {
2056
- const value = this.getFieldValue(event, pattern.field);
2057
- if (value === void 0) {
2058
- return false;
2735
+ matchesPattern(event, pattern, subjects, slow) {
2736
+ const subject = this.subjectOf(event, pattern, subjects);
2737
+ if (subject.failed) {
2738
+ return this.notEvaluable();
2059
2739
  }
2060
- const stringValue = this.stringify(value);
2061
- const patternValue = pattern.caseInsensitive ? pattern.pattern.toLowerCase() : pattern.pattern;
2062
- const matchValue = pattern.caseInsensitive ? stringValue.toLowerCase() : stringValue;
2063
- try {
2064
- switch (pattern.type) {
2065
- case import_types2.PatternType.Exact:
2066
- return matchValue === patternValue;
2067
- case import_types2.PatternType.Prefix:
2068
- return matchValue.startsWith(patternValue);
2069
- case import_types2.PatternType.Regex:
2070
- return this.matchRegex(pattern.pattern, matchValue, pattern.caseInsensitive);
2071
- case import_types2.PatternType.Glob:
2072
- return (0, import_minimatch.minimatch)(matchValue, patternValue, {
2073
- nocase: pattern.caseInsensitive
2074
- });
2075
- default:
2076
- return false;
2077
- }
2078
- } catch (error) {
2079
- this.onError?.(error, event);
2740
+ if (subject.text === void 0) {
2080
2741
  return false;
2081
2742
  }
2743
+ switch (pattern.type) {
2744
+ case import_types3.PatternType.Exact:
2745
+ return this.textOf(subject, pattern) === pattern.text;
2746
+ case import_types3.PatternType.Prefix:
2747
+ return this.textOf(subject, pattern).startsWith(pattern.text);
2748
+ case import_types3.PatternType.Regex:
2749
+ if (!pattern.regex) return this.notEvaluable();
2750
+ if (!slow) return void 0;
2751
+ return pattern.regex.test(subject.text);
2752
+ case import_types3.PatternType.Glob:
2753
+ if (!pattern.glob) return this.notEvaluable();
2754
+ if (!slow) return void 0;
2755
+ if (pattern.glob.match(subject.text)) return true;
2756
+ return pattern.wildcard ? matchesWildcard(this.textOf(subject, pattern), pattern.wildcard) : false;
2757
+ default:
2758
+ return this.notEvaluable();
2759
+ }
2082
2760
  }
2083
2761
  /**
2084
- * Get field value from event using dot notation.
2762
+ * What a pattern that cannot be evaluated counts as: a match when matching keeps an event
2763
+ * back (exclude), no match when matching lets it through (include).
2085
2764
  */
2086
- getFieldValue(event, field) {
2087
- if (!field) {
2088
- return event.payload;
2089
- }
2090
- const parts = field.split(".");
2091
- let value = event;
2092
- for (const part of parts) {
2093
- if (value === null || value === void 0) {
2094
- return void 0;
2095
- }
2096
- if (typeof value !== "object") {
2097
- return void 0;
2098
- }
2099
- value = value[part];
2100
- }
2101
- return value;
2765
+ notEvaluable() {
2766
+ return this.exclude;
2767
+ }
2768
+ /**
2769
+ * The subject text in the case the pattern compares in.
2770
+ */
2771
+ textOf(subject, pattern) {
2772
+ if (!pattern.caseInsensitive) return subject.text;
2773
+ if (subject.lower === void 0) subject.lower = subject.text.toLowerCase();
2774
+ return subject.lower;
2102
2775
  }
2103
2776
  /**
2104
- * Stringify a value for matching.
2777
+ * The text of the field a pattern reads, computed once per event.
2105
2778
  */
2106
- stringify(value) {
2779
+ subjectOf(event, pattern, subjects) {
2780
+ let subject = subjects.get(pattern.key);
2781
+ if (subject) return subject;
2782
+ subject = { failed: false };
2783
+ const value = pattern.path ? readField(event, pattern.path) : event.payload;
2107
2784
  if (typeof value === "string") {
2108
- return value;
2109
- }
2110
- if (value === null || value === void 0) {
2111
- return "";
2785
+ subject.text = value;
2786
+ } else if (value === null) {
2787
+ subject.text = "";
2788
+ } else if (value !== void 0) {
2789
+ try {
2790
+ subject.text = JSON.stringify(value) ?? "";
2791
+ } catch (error) {
2792
+ subject.failed = true;
2793
+ subject.error = error;
2794
+ }
2112
2795
  }
2113
- return JSON.stringify(value);
2796
+ subjects.set(pattern.key, subject);
2797
+ return subject;
2114
2798
  }
2115
2799
  /**
2116
- * Match against a compiled regex with ReDoS protection.
2117
- * Enforces input length limits to prevent catastrophic backtracking.
2800
+ * Compile one rule.
2118
2801
  */
2119
- matchRegex(pattern, value, caseInsensitive) {
2120
- if (value.length > MAX_REGEX_INPUT_LENGTH) {
2121
- return false;
2802
+ compileRule(rule) {
2803
+ const compiled = { matchAll: false };
2804
+ if (typeof rule !== "object" || rule === null) {
2805
+ throw new TypeError("EventFilter: a rule must be an object");
2122
2806
  }
2123
- const cacheKey = `${pattern}:${caseInsensitive ? "i" : ""}`;
2124
- const compiled = this.compiledRegexes.get(cacheKey);
2125
- if (!compiled) {
2126
- try {
2127
- const flags = caseInsensitive ? "i" : "";
2128
- return new RegExp(pattern, flags).test(value);
2129
- } catch {
2130
- return false;
2807
+ if (rule.types) {
2808
+ if (!Array.isArray(rule.types.types)) {
2809
+ throw new TypeError('EventFilter: a type filter needs a "types" array');
2131
2810
  }
2811
+ compiled.types = new Set(rule.types.types);
2132
2812
  }
2813
+ if (rule.content) {
2814
+ const content = rule.content;
2815
+ if (!Array.isArray(content.patterns)) {
2816
+ throw new TypeError('EventFilter: a content filter needs a "patterns" array');
2817
+ }
2818
+ compiled.patterns = content.patterns.map((pattern) => this.compilePattern(pattern));
2819
+ compiled.matchAll = content.match === "all";
2820
+ }
2821
+ return compiled;
2822
+ }
2823
+ /**
2824
+ * Compile a glob. Returns null when the pattern does not compile.
2825
+ */
2826
+ compileGlob(source, caseInsensitive) {
2133
2827
  try {
2134
- return compiled.regex.test(value);
2828
+ return new import_minimatch.Minimatch(source, {
2829
+ nocase: caseInsensitive,
2830
+ nobrace: !this.expandGlobs,
2831
+ noext: !this.expandGlobs,
2832
+ // An exclude glob is matched as text: also against a leading dot.
2833
+ dot: this.exclude
2834
+ });
2135
2835
  } catch {
2136
- return false;
2836
+ return null;
2137
2837
  }
2138
2838
  }
2139
2839
  /**
2140
- * Pre-compile all regex patterns for efficiency.
2141
- * Includes ReDoS vulnerability detection and logging.
2840
+ * Compile every glob of this filter again.
2142
2841
  */
2143
- compilePatterns() {
2144
- for (const rule of this.config.rules) {
2145
- if (!rule.content?.patterns) continue;
2146
- for (const pattern of rule.content.patterns) {
2147
- if (pattern.type !== import_types2.PatternType.Regex) continue;
2148
- const cacheKey = `${pattern.pattern}:${pattern.caseInsensitive ? "i" : ""}`;
2149
- if (this.compiledRegexes.has(cacheKey)) continue;
2150
- if (isPotentiallyVulnerableRegex(pattern.pattern)) {
2151
- console.warn(
2152
- `[EventFilter] Potentially vulnerable regex pattern detected: ${pattern.pattern}. Input length will be limited to ${MAX_REGEX_INPUT_LENGTH} characters for protection.`
2153
- );
2154
- }
2155
- try {
2156
- const flags = pattern.caseInsensitive ? "i" : "";
2157
- const regex = new RegExp(pattern.pattern, flags);
2158
- this.compiledRegexes.set(cacheKey, { regex });
2159
- } catch (error) {
2160
- console.error(`Invalid regex pattern: ${pattern.pattern}`, error);
2842
+ recompileGlobs() {
2843
+ for (const rule of this.rules) {
2844
+ for (const pattern of rule.patterns ?? []) {
2845
+ if (pattern.glob) {
2846
+ pattern.glob = this.compileGlob(pattern.source, pattern.caseInsensitive);
2161
2847
  }
2162
2848
  }
2163
2849
  }
2164
2850
  }
2851
+ /**
2852
+ * Compile one content pattern. A pattern that does not compile is kept as "cannot be
2853
+ * evaluated"; its text is not logged.
2854
+ */
2855
+ compilePattern(pattern) {
2856
+ if (typeof pattern !== "object" || pattern === null || typeof pattern.pattern !== "string") {
2857
+ throw new TypeError('EventFilter: a content pattern needs a "pattern" string');
2858
+ }
2859
+ const caseInsensitive = pattern.caseInsensitive === true;
2860
+ const field2 = typeof pattern.field === "string" && pattern.field.length > 0 ? pattern.field : void 0;
2861
+ const compiled = {
2862
+ type: pattern.type,
2863
+ path: field2?.split("."),
2864
+ key: field2 ?? "",
2865
+ caseInsensitive,
2866
+ text: caseInsensitive ? pattern.pattern.toLowerCase() : pattern.pattern,
2867
+ source: pattern.pattern
2868
+ };
2869
+ if (pattern.type === import_types3.PatternType.Regex) {
2870
+ try {
2871
+ compiled.regex = new RegExp(pattern.pattern, caseInsensitive ? "i" : "");
2872
+ } catch {
2873
+ compiled.regex = null;
2874
+ }
2875
+ } else if (pattern.type === import_types3.PatternType.Glob) {
2876
+ compiled.glob = this.compileGlob(pattern.pattern, caseInsensitive);
2877
+ if (this.exclude) {
2878
+ compiled.wildcard = compileWildcard(compiled.text);
2879
+ }
2880
+ }
2881
+ return compiled;
2882
+ }
2165
2883
  };
2884
+ function readField(event, path) {
2885
+ let value = event;
2886
+ for (const part of path) {
2887
+ if (value === null || typeof value !== "object") {
2888
+ return void 0;
2889
+ }
2890
+ if (!Object.prototype.hasOwnProperty.call(value, part)) {
2891
+ return void 0;
2892
+ }
2893
+ value = value[part];
2894
+ }
2895
+ return value;
2896
+ }
2897
+ function compileWildcard(pattern) {
2898
+ const tokens = [];
2899
+ for (let i = 0; i < pattern.length; i++) {
2900
+ const char = pattern[i];
2901
+ if (char === "*") {
2902
+ if (tokens[tokens.length - 1] !== ANY) tokens.push(ANY);
2903
+ } else if (char === "?") {
2904
+ tokens.push(ONE);
2905
+ } else if (char === "\\" && i + 1 < pattern.length) {
2906
+ tokens.push(pattern[++i]);
2907
+ } else {
2908
+ tokens.push(char);
2909
+ }
2910
+ }
2911
+ const inner = tokens.slice(1, -1);
2912
+ if (tokens.length >= 3 && tokens[0] === ANY && tokens[tokens.length - 1] === ANY && inner.every((token) => typeof token === "string")) {
2913
+ return { contains: inner.join("") };
2914
+ }
2915
+ return { tokens };
2916
+ }
2917
+ function matchesWildcard(text, wildcard) {
2918
+ if ("contains" in wildcard) {
2919
+ return text.includes(wildcard.contains);
2920
+ }
2921
+ const tokens = wildcard.tokens;
2922
+ let t = 0;
2923
+ let p = 0;
2924
+ let star = -1;
2925
+ let mark = 0;
2926
+ while (t < text.length) {
2927
+ const token = tokens[p];
2928
+ if (p < tokens.length && (token === ONE || token === text[t])) {
2929
+ t++;
2930
+ p++;
2931
+ } else if (p < tokens.length && token === ANY) {
2932
+ star = p;
2933
+ mark = t;
2934
+ p++;
2935
+ } else if (star !== -1) {
2936
+ p = star + 1;
2937
+ mark++;
2938
+ t = mark;
2939
+ } else {
2940
+ return false;
2941
+ }
2942
+ }
2943
+ while (p < tokens.length && tokens[p] === ANY) p++;
2944
+ return p === tokens.length;
2945
+ }
2166
2946
  function createEventFilter(options) {
2167
2947
  return new EventFilter(options);
2168
2948
  }
2169
2949
  function createTypeFilter(mode, types) {
2170
2950
  return {
2171
- mode: mode === "include" ? import_types2.FilterMode.Include : import_types2.FilterMode.Exclude,
2951
+ mode: mode === "include" ? import_types3.FilterMode.Include : import_types3.FilterMode.Exclude,
2172
2952
  rules: [{ types: { types } }]
2173
2953
  };
2174
2954
  }
2175
- function createContentFilter(mode, pattern, patternType = import_types2.PatternType.Regex) {
2955
+ function createContentFilter(mode, pattern, patternType = import_types3.PatternType.Regex) {
2176
2956
  return {
2177
- mode: mode === "include" ? import_types2.FilterMode.Include : import_types2.FilterMode.Exclude,
2957
+ mode: mode === "include" ? import_types3.FilterMode.Include : import_types3.FilterMode.Exclude,
2178
2958
  rules: [
2179
2959
  {
2180
2960
  content: {
@@ -2185,15 +2965,280 @@ function createContentFilter(mode, pattern, patternType = import_types2.PatternT
2185
2965
  };
2186
2966
  }
2187
2967
 
2968
+ // libs/broker/src/http/request-validation.ts
2969
+ var import_types4 = require("@enclave-vm/types");
2970
+ var REQUEST_FILTER_LIMITS = {
2971
+ /** Rules per filter */
2972
+ maxRules: 16,
2973
+ /** Content patterns per filter, over all rules */
2974
+ maxPatterns: 32,
2975
+ /** Characters per pattern */
2976
+ maxPatternLength: 256,
2977
+ /** Characters per field path */
2978
+ maxFieldLength: 128,
2979
+ /** Event types per type filter and per always-allow list */
2980
+ maxTypes: 32,
2981
+ /** Characters per event type */
2982
+ maxTypeLength: 64,
2983
+ /** Characters of the JSON text in the `filter` query parameter */
2984
+ maxQueryLength: 8192,
2985
+ /** Time limit for the evaluation of one event, in milliseconds */
2986
+ evaluationTimeoutMs: 100,
2987
+ /** Time all evaluations of one filter may take together, in milliseconds */
2988
+ budgetMs: 500
2989
+ };
2990
+ var SESSION_ID_PATTERN = /^s_[A-Za-z0-9._:~-]{1,128}$/;
2991
+ function isRequestSessionId(value) {
2992
+ return typeof value === "string" && SESSION_ID_PATTERN.test(value);
2993
+ }
2994
+ var invalid = (message, code = "INVALID_REQUEST") => ({
2995
+ ok: false,
2996
+ code,
2997
+ message
2998
+ });
2999
+ function isPlainObject(value) {
3000
+ return typeof value === "object" && value !== null && !Array.isArray(value);
3001
+ }
3002
+ function field(object, key) {
3003
+ if (!Object.prototype.hasOwnProperty.call(object, key)) return void 0;
3004
+ const value = object[key];
3005
+ return value === null ? void 0 : value;
3006
+ }
3007
+ function isPositiveInteger(value) {
3008
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
3009
+ }
3010
+ var LIMIT_FIELDS = ["maxExecutionMs", "maxToolCalls", "heartbeatIntervalMs"];
3011
+ function parseCreateSessionRequest(body) {
3012
+ if (!isPlainObject(body)) {
3013
+ return invalid('Missing or invalid "code" field');
3014
+ }
3015
+ const code = field(body, "code");
3016
+ if (typeof code !== "string") {
3017
+ return invalid('Missing or invalid "code" field');
3018
+ }
3019
+ const sessionId = field(body, "sessionId");
3020
+ if (sessionId !== void 0 && !isRequestSessionId(sessionId)) {
3021
+ return invalid('Invalid "sessionId" field');
3022
+ }
3023
+ let filter;
3024
+ const rawFilter = field(body, "filter");
3025
+ if (rawFilter !== void 0) {
3026
+ const parsedFilter = parseFilterConfig(rawFilter);
3027
+ if (!parsedFilter.ok) return parsedFilter;
3028
+ filter = parsedFilter.value;
3029
+ }
3030
+ const config = {};
3031
+ const rawConfig = field(body, "config");
3032
+ if (rawConfig !== void 0) {
3033
+ if (!isPlainObject(rawConfig)) {
3034
+ return invalid('Invalid "config" field: expected an object');
3035
+ }
3036
+ for (const key of LIMIT_FIELDS) {
3037
+ const value = field(rawConfig, key);
3038
+ if (value === void 0) continue;
3039
+ if (!isPositiveInteger(value)) {
3040
+ return invalid(`Invalid "config.${key}" field: expected a positive integer`);
3041
+ }
3042
+ config[key] = value;
3043
+ }
3044
+ }
3045
+ const encryption = field(body, "encryption");
3046
+ if (encryption !== void 0) {
3047
+ if (!isPlainObject(encryption)) {
3048
+ return invalid('Invalid "encryption" field: expected an object');
3049
+ }
3050
+ if (field(encryption, "mode") === "required") {
3051
+ return invalid("Encrypted sessions are not supported by this broker", "ENCRYPTION_NOT_SUPPORTED");
3052
+ }
3053
+ }
3054
+ return { ok: true, value: { code, sessionId, config, filter } };
3055
+ }
3056
+ var invalidFilter = (message) => invalid(`Invalid filter configuration: ${message}`, "INVALID_FILTER");
3057
+ var PATTERN_TYPES = Object.values(import_types4.PatternType);
3058
+ function parseTypeList(value) {
3059
+ if (!Array.isArray(value) || value.length > REQUEST_FILTER_LIMITS.maxTypes) return void 0;
3060
+ for (const entry of value) {
3061
+ if (typeof entry !== "string" || entry.length === 0 || entry.length > REQUEST_FILTER_LIMITS.maxTypeLength) {
3062
+ return void 0;
3063
+ }
3064
+ }
3065
+ return [...value];
3066
+ }
3067
+ function parseFilterConfig(value) {
3068
+ if (!isPlainObject(value)) {
3069
+ return invalidFilter("expected an object");
3070
+ }
3071
+ const mode = field(value, "mode");
3072
+ if (mode !== import_types4.FilterMode.Include && mode !== import_types4.FilterMode.Exclude) {
3073
+ return invalidFilter('"mode" must be "include" or "exclude"');
3074
+ }
3075
+ const rawRules = field(value, "rules");
3076
+ if (!Array.isArray(rawRules) || rawRules.length > REQUEST_FILTER_LIMITS.maxRules) {
3077
+ return invalidFilter(`"rules" must be a list of at most ${REQUEST_FILTER_LIMITS.maxRules} rules`);
3078
+ }
3079
+ const rules = [];
3080
+ let patternCount = 0;
3081
+ for (const rawRule of rawRules) {
3082
+ if (!isPlainObject(rawRule)) {
3083
+ return invalidFilter("a rule must be an object");
3084
+ }
3085
+ const rule = {};
3086
+ const rawTypes = field(rawRule, "types");
3087
+ if (rawTypes !== void 0) {
3088
+ const types = isPlainObject(rawTypes) ? parseTypeList(field(rawTypes, "types")) : void 0;
3089
+ if (!types) {
3090
+ return invalidFilter(`"types" must hold a list of at most ${REQUEST_FILTER_LIMITS.maxTypes} event types`);
3091
+ }
3092
+ rule.types = { types };
3093
+ }
3094
+ const rawContent = field(rawRule, "content");
3095
+ if (rawContent !== void 0) {
3096
+ if (!isPlainObject(rawContent)) {
3097
+ return invalidFilter('"content" must be an object');
3098
+ }
3099
+ const rawPatterns = field(rawContent, "patterns");
3100
+ if (!Array.isArray(rawPatterns)) {
3101
+ return invalidFilter('"content.patterns" must be a list');
3102
+ }
3103
+ patternCount += rawPatterns.length;
3104
+ if (patternCount > REQUEST_FILTER_LIMITS.maxPatterns) {
3105
+ return invalidFilter(`a filter may hold at most ${REQUEST_FILTER_LIMITS.maxPatterns} patterns`);
3106
+ }
3107
+ const patterns = [];
3108
+ for (const rawPattern of rawPatterns) {
3109
+ const pattern = parseContentPattern(rawPattern);
3110
+ if (!pattern.ok) return pattern;
3111
+ patterns.push(pattern.value);
3112
+ }
3113
+ const match = field(rawContent, "match");
3114
+ if (match !== void 0 && match !== "any" && match !== "all") {
3115
+ return invalidFilter('"content.match" must be "any" or "all"');
3116
+ }
3117
+ rule.content = match === void 0 ? { patterns } : { patterns, match };
3118
+ }
3119
+ rules.push(rule);
3120
+ }
3121
+ const config = { mode, rules, regexTimeoutMs: REQUEST_FILTER_LIMITS.evaluationTimeoutMs };
3122
+ const rawAlwaysAllow = field(value, "alwaysAllow");
3123
+ if (rawAlwaysAllow !== void 0) {
3124
+ const alwaysAllow = parseTypeList(rawAlwaysAllow);
3125
+ if (!alwaysAllow) {
3126
+ return invalidFilter(`"alwaysAllow" must be a list of at most ${REQUEST_FILTER_LIMITS.maxTypes} event types`);
3127
+ }
3128
+ config.alwaysAllow = alwaysAllow;
3129
+ }
3130
+ const rawTimeout = field(value, "regexTimeoutMs");
3131
+ if (rawTimeout !== void 0) {
3132
+ if (!isPositiveInteger(rawTimeout)) {
3133
+ return invalidFilter('"regexTimeoutMs" must be a positive integer');
3134
+ }
3135
+ config.regexTimeoutMs = Math.min(rawTimeout, REQUEST_FILTER_LIMITS.evaluationTimeoutMs);
3136
+ }
3137
+ return { ok: true, value: config };
3138
+ }
3139
+ function parseContentPattern(value) {
3140
+ if (!isPlainObject(value)) {
3141
+ return invalidFilter("a pattern must be an object");
3142
+ }
3143
+ const type = field(value, "type");
3144
+ if (typeof type !== "string" || !PATTERN_TYPES.includes(type)) {
3145
+ return invalidFilter('"type" of a pattern must be "exact", "prefix", "regex" or "glob"');
3146
+ }
3147
+ const text = field(value, "pattern");
3148
+ if (typeof text !== "string" || text.length > REQUEST_FILTER_LIMITS.maxPatternLength) {
3149
+ return invalidFilter(`"pattern" must be a string of at most ${REQUEST_FILTER_LIMITS.maxPatternLength} characters`);
3150
+ }
3151
+ const pattern = { type, pattern: text };
3152
+ const path = field(value, "field");
3153
+ if (path !== void 0) {
3154
+ if (typeof path !== "string" || path.length === 0 || path.length > REQUEST_FILTER_LIMITS.maxFieldLength) {
3155
+ return invalidFilter(`"field" must be a string of at most ${REQUEST_FILTER_LIMITS.maxFieldLength} characters`);
3156
+ }
3157
+ pattern.field = path;
3158
+ }
3159
+ const caseInsensitive = field(value, "caseInsensitive");
3160
+ if (caseInsensitive !== void 0) {
3161
+ if (typeof caseInsensitive !== "boolean") {
3162
+ return invalidFilter('"caseInsensitive" must be a boolean');
3163
+ }
3164
+ pattern.caseInsensitive = caseInsensitive;
3165
+ }
3166
+ if (type === import_types4.PatternType.Regex) {
3167
+ try {
3168
+ new RegExp(text, pattern.caseInsensitive ? "i" : "");
3169
+ } catch {
3170
+ return invalidFilter('a "regex" pattern must be a valid regular expression');
3171
+ }
3172
+ }
3173
+ return { ok: true, value: pattern };
3174
+ }
3175
+ function parseFilterParameter(value) {
3176
+ if (typeof value !== "string" || value.length > REQUEST_FILTER_LIMITS.maxQueryLength) {
3177
+ return invalidFilter(`expected JSON text of at most ${REQUEST_FILTER_LIMITS.maxQueryLength} characters`);
3178
+ }
3179
+ let parsed;
3180
+ try {
3181
+ parsed = JSON.parse(value);
3182
+ } catch {
3183
+ return invalidFilter("expected JSON text");
3184
+ }
3185
+ return parseFilterConfig(parsed);
3186
+ }
3187
+
2188
3188
  // libs/broker/src/http/session-handler.ts
3189
+ var CORS_REQUEST_HEADERS = ["Content-Type", "Accept", "Last-Event-ID", "Authorization", "X-Session-Instance-ID"];
3190
+ var SESSION_INSTANCE_ID = /^[A-Za-z0-9._:~-]{1,128}$/;
3191
+ var HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
3192
+ function corsRequestHeaders(configured) {
3193
+ if (configured !== void 0 && !Array.isArray(configured)) {
3194
+ throw new TypeError('SessionHandler: "allowedHeaders" must be an array of header names');
3195
+ }
3196
+ const names = [...CORS_REQUEST_HEADERS];
3197
+ for (const name of configured ?? []) {
3198
+ if (typeof name !== "string" || !HEADER_NAME.test(name)) {
3199
+ throw new TypeError('SessionHandler: "allowedHeaders" must be an array of header names');
3200
+ }
3201
+ if (!names.some((known) => known.toLowerCase() === name.toLowerCase())) {
3202
+ names.push(name);
3203
+ }
3204
+ }
3205
+ return names.join(", ");
3206
+ }
3207
+ var MIN_REQUEST_HEARTBEAT_MS = 1e3;
3208
+ var MAX_REQUEST_HEARTBEAT_MS = 3e5;
3209
+ var MAX_CALLER_LENGTH = 1024;
2189
3210
  var SessionHandler = class {
2190
3211
  broker;
2191
3212
  cors;
2192
3213
  allowedOrigins;
3214
+ allowedRequestHeaders;
3215
+ identify;
3216
+ sessionList;
3217
+ maxStreamsPerSession;
3218
+ maxBufferedBytes;
3219
+ streams = /* @__PURE__ */ new WeakMap();
2193
3220
  constructor(config) {
2194
3221
  this.broker = config.broker;
2195
3222
  this.cors = config.cors ?? true;
2196
3223
  this.allowedOrigins = config.allowedOrigins ?? ["*"];
3224
+ this.allowedRequestHeaders = corsRequestHeaders(config.allowedHeaders);
3225
+ if (config.identify !== void 0 && typeof config.identify !== "function") {
3226
+ throw new TypeError('SessionHandler: "identify" must be a function');
3227
+ }
3228
+ this.identify = config.identify;
3229
+ this.sessionList = config.sessionList ?? true;
3230
+ this.maxStreamsPerSession = positiveNumber(config.maxStreamsPerSession, "maxStreamsPerSession", 8);
3231
+ this.maxBufferedBytes = positiveNumber(config.maxBufferedBytes, "maxBufferedBytes", 8 * 1024 * 1024);
3232
+ warnUnknownKeys("SessionHandler", config, [
3233
+ "broker",
3234
+ "cors",
3235
+ "allowedOrigins",
3236
+ "allowedHeaders",
3237
+ "identify",
3238
+ "sessionList",
3239
+ "maxStreamsPerSession",
3240
+ "maxBufferedBytes"
3241
+ ]);
2197
3242
  }
2198
3243
  /**
2199
3244
  * Get route definitions for registration with HTTP frameworks
@@ -2205,9 +3250,11 @@ var SessionHandler = class {
2205
3250
  { method: "GET", path: "/sessions/:sessionId", handler: this.getSession.bind(this) },
2206
3251
  { method: "GET", path: "/sessions/:sessionId/stream", handler: this.streamSession.bind(this) },
2207
3252
  { method: "DELETE", path: "/sessions/:sessionId", handler: this.terminateSession.bind(this) },
3253
+ { method: "POST", path: "/sessions/:sessionId/cancel", handler: this.terminateSession.bind(this) },
2208
3254
  { method: "OPTIONS", path: "/sessions", handler: this.handleCors.bind(this) },
2209
3255
  { method: "OPTIONS", path: "/sessions/:sessionId", handler: this.handleCors.bind(this) },
2210
- { method: "OPTIONS", path: "/sessions/:sessionId/stream", handler: this.handleCors.bind(this) }
3256
+ { method: "OPTIONS", path: "/sessions/:sessionId/stream", handler: this.handleCors.bind(this) },
3257
+ { method: "OPTIONS", path: "/sessions/:sessionId/cancel", handler: this.handleCors.bind(this) }
2211
3258
  ];
2212
3259
  }
2213
3260
  /**
@@ -2225,7 +3272,7 @@ var SessionHandler = class {
2225
3272
  return;
2226
3273
  }
2227
3274
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
2228
- res.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Last-Event-ID");
3275
+ res.setHeader("Access-Control-Allow-Headers", this.allowedRequestHeaders);
2229
3276
  res.setHeader("Access-Control-Max-Age", "86400");
2230
3277
  }
2231
3278
  /**
@@ -2236,14 +3283,81 @@ var SessionHandler = class {
2236
3283
  res.status(204).end();
2237
3284
  }
2238
3285
  /**
2239
- * List all active sessions
3286
+ * Who the caller of a request is.
3287
+ *
3288
+ * Without an `identify` hook there is no caller identity and every request passes. With one,
3289
+ * a request without an identity is answered with 401 here and `undefined` is returned.
3290
+ */
3291
+ async callerOf(req, res) {
3292
+ if (!this.identify) {
3293
+ return { caller: void 0 };
3294
+ }
3295
+ let caller;
3296
+ try {
3297
+ caller = await this.identify(req);
3298
+ } catch {
3299
+ caller = void 0;
3300
+ }
3301
+ if (typeof caller !== "string" || caller.length === 0 || caller.length > MAX_CALLER_LENGTH) {
3302
+ this.sendError(res, 401, "UNAUTHORIZED", "The caller of this request could not be identified");
3303
+ return void 0;
3304
+ }
3305
+ return { caller };
3306
+ }
3307
+ /**
3308
+ * Whether a caller may reach a session: always without an `identify` hook, otherwise only the
3309
+ * caller that owns it. A session without an owner belongs to no caller.
3310
+ */
3311
+ mayReach(session, caller) {
3312
+ if (!this.identify) return true;
3313
+ return caller !== void 0 && session.owner === caller;
3314
+ }
3315
+ /**
3316
+ * The session a request names, if it exists and its caller may reach it. Otherwise the answer
3317
+ * has been sent (401, 400 or 404) and `undefined` is returned. A session of another caller is
3318
+ * answered exactly like an id that does not exist.
3319
+ */
3320
+ async sessionOf(req, res) {
3321
+ const identity = await this.callerOf(req, res);
3322
+ if (!identity) return void 0;
3323
+ const sessionId = req.params["sessionId"];
3324
+ if (typeof sessionId !== "string" || sessionId.length === 0) {
3325
+ this.sendError(res, 400, "INVALID_REQUEST", "Missing sessionId");
3326
+ return void 0;
3327
+ }
3328
+ const session = this.broker.getSession(sessionId);
3329
+ if (!session || !this.mayReach(session, identity.caller)) {
3330
+ this.sendError(res, 404, "NOT_FOUND", "Session not found");
3331
+ return void 0;
3332
+ }
3333
+ return session;
3334
+ }
3335
+ /** Optional identifier for one creation, sent again on a conditional stop. */
3336
+ instanceIdOf(req, res) {
3337
+ const values = Object.entries(req.headers).filter(([name]) => name.toLowerCase() === "x-session-instance-id");
3338
+ const value = values[0]?.[1];
3339
+ if (values.length === 0) return {};
3340
+ if (values.length !== 1 || typeof value !== "string" || !SESSION_INSTANCE_ID.test(value)) {
3341
+ this.sendError(res, 400, "INVALID_REQUEST", "Invalid X-Session-Instance-ID header");
3342
+ return void 0;
3343
+ }
3344
+ return { value };
3345
+ }
3346
+ /**
3347
+ * List sessions: all of them, or the caller's own when callers are identified
2240
3348
  *
2241
3349
  * GET /sessions
2242
3350
  */
2243
3351
  async listSessions(req, res) {
2244
3352
  this.addCorsHeaders(req, res);
3353
+ if (!this.sessionList) {
3354
+ this.sendError(res, 404, "NOT_FOUND", "Not found");
3355
+ return;
3356
+ }
3357
+ const identity = await this.callerOf(req, res);
3358
+ if (!identity) return;
2245
3359
  try {
2246
- const sessions = this.broker.listSessions();
3360
+ const sessions = this.broker.listSessions().filter((info) => this.mayReach(info, identity.caller));
2247
3361
  const response = {
2248
3362
  sessions: sessions.map((info) => this.sessionInfoToResponse(info)),
2249
3363
  total: sessions.length
@@ -2263,67 +3377,99 @@ var SessionHandler = class {
2263
3377
  */
2264
3378
  async createSession(req, res) {
2265
3379
  this.addCorsHeaders(req, res);
2266
- const body = req.body;
2267
- if (!body || typeof body.code !== "string") {
2268
- this.sendError(res, 400, "INVALID_REQUEST", 'Missing or invalid "code" field');
3380
+ const identity = await this.callerOf(req, res);
3381
+ if (!identity) return;
3382
+ const instanceId = this.instanceIdOf(req, res);
3383
+ if (!instanceId) return;
3384
+ const parsed = parseCreateSessionRequest(req.body);
3385
+ if (!parsed.ok) {
3386
+ this.sendError(res, 400, parsed.code, parsed.message);
2269
3387
  return;
2270
3388
  }
3389
+ const { code, sessionId, config, filter } = parsed.value;
2271
3390
  if (this.broker.isDisposed) {
2272
3391
  this.sendError(res, 503, "SERVICE_UNAVAILABLE", "Broker is disposed");
2273
3392
  return;
2274
3393
  }
3394
+ let session;
3395
+ let eventFilter;
2275
3396
  try {
2276
- const limits = {};
2277
- if (body.config?.maxExecutionMs !== void 0) {
2278
- limits["sessionTtlMs"] = body.config.maxExecutionMs;
2279
- }
2280
- if (body.config?.maxToolCalls !== void 0) {
2281
- limits["maxToolCalls"] = body.config.maxToolCalls;
3397
+ const limits = this.boundLimits(config);
3398
+ if (filter) {
3399
+ eventFilter = this.requestFilter(filter);
2282
3400
  }
2283
- if (body.config?.heartbeatIntervalMs !== void 0) {
2284
- limits["heartbeatIntervalMs"] = body.config.heartbeatIntervalMs;
2285
- }
2286
- let eventFilter;
2287
- if (body.filter) {
2288
- eventFilter = createEventFilter({ config: body.filter });
3401
+ session = this.broker.createSession({ sessionId, limits, owner: identity.caller, instanceId: instanceId.value });
3402
+ } catch (error) {
3403
+ this.sendCreateFailure(res, error);
3404
+ return;
3405
+ }
3406
+ const release = this.acquireStream(session);
3407
+ let unsubscribe = () => void 0;
3408
+ let cleanedUp = false;
3409
+ let dropped = false;
3410
+ const cleanup = (reason = "Client disconnected") => {
3411
+ if (cleanedUp) return;
3412
+ cleanedUp = true;
3413
+ unsubscribe();
3414
+ release?.();
3415
+ if (!session.isTerminal()) {
3416
+ session.cancel(reason).catch(() => {
3417
+ });
2289
3418
  }
2290
- const session = this.broker.createSession({
2291
- sessionId: body.sessionId,
2292
- limits: Object.keys(limits).length > 0 ? limits : void 0
2293
- });
3419
+ };
3420
+ try {
2294
3421
  res.setHeader("Content-Type", "application/x-ndjson");
2295
3422
  res.setHeader("Cache-Control", "no-cache");
2296
3423
  res.setHeader("Connection", "keep-alive");
2297
3424
  res.setHeader("X-Session-ID", session.sessionId);
2298
- const unsubscribe = session.onEvent((event) => {
3425
+ unsubscribe = session.onEvent((event) => {
3426
+ if (dropped) return;
2299
3427
  if (eventFilter && !eventFilter.shouldSend(event)) {
2300
3428
  return;
2301
3429
  }
2302
- this.writeEvent(res, event);
2303
- });
2304
- let cleanedUp = false;
2305
- const cleanup = () => {
2306
- if (cleanedUp) return;
2307
- cleanedUp = true;
2308
- unsubscribe();
2309
- if (!session.isTerminal()) {
2310
- session.cancel("Client disconnected").catch(() => {
2311
- });
3430
+ if (!this.writeEvent(res, event)) {
3431
+ dropped = true;
3432
+ cleanup("Client is not reading its stream");
3433
+ this.dropReader(res);
2312
3434
  }
2313
- };
3435
+ });
2314
3436
  if (req.signal) {
2315
- req.signal.addEventListener("abort", cleanup, { once: true });
3437
+ req.signal.addEventListener("abort", () => cleanup(), { once: true });
3438
+ if (req.signal.aborted) cleanup();
2316
3439
  }
2317
- try {
2318
- await session.execute(body.code);
2319
- } catch {
2320
- } finally {
2321
- cleanup();
3440
+ } catch {
3441
+ cleanup();
3442
+ await this.broker.terminateSession(session.sessionId, void 0, session).catch(() => false);
3443
+ this.sendError(res, 500, "SESSION_CREATE_ERROR", "Failed to create session");
3444
+ return;
3445
+ }
3446
+ try {
3447
+ await session.execute(code);
3448
+ } catch {
3449
+ } finally {
3450
+ cleanup();
3451
+ if (!dropped) {
2322
3452
  res.end();
2323
3453
  }
2324
- } catch (error) {
2325
- const message = error instanceof Error ? error.message : "Unknown error";
2326
- this.sendError(res, 500, "SESSION_CREATE_ERROR", message);
3454
+ }
3455
+ }
3456
+ /**
3457
+ * Answer a failed session creation with a code and a fixed text.
3458
+ */
3459
+ sendCreateFailure(res, error) {
3460
+ const code = error instanceof BrokerError ? error.code : void 0;
3461
+ switch (code) {
3462
+ case BrokerErrorCode.SessionExists:
3463
+ this.sendError(res, 409, "SESSION_EXISTS", "A session with this id already exists");
3464
+ return;
3465
+ case BrokerErrorCode.InvalidSessionId:
3466
+ this.sendError(res, 400, "INVALID_REQUEST", 'Invalid "sessionId" field');
3467
+ return;
3468
+ case BrokerErrorCode.MaxSessionsReached:
3469
+ this.sendError(res, 503, "MAX_SESSIONS_REACHED", "The broker has reached its session limit");
3470
+ return;
3471
+ default:
3472
+ this.sendError(res, 500, "SESSION_CREATE_ERROR", "Failed to create session");
2327
3473
  }
2328
3474
  }
2329
3475
  /**
@@ -2333,16 +3479,8 @@ var SessionHandler = class {
2333
3479
  */
2334
3480
  async getSession(req, res) {
2335
3481
  this.addCorsHeaders(req, res);
2336
- const sessionId = req.params["sessionId"];
2337
- if (!sessionId) {
2338
- this.sendError(res, 400, "INVALID_REQUEST", "Missing sessionId");
2339
- return;
2340
- }
2341
- const session = this.broker.getSession(sessionId);
2342
- if (!session) {
2343
- this.sendError(res, 404, "NOT_FOUND", `Session ${sessionId} not found`);
2344
- return;
2345
- }
3482
+ const session = await this.sessionOf(req, res);
3483
+ if (!session) return;
2346
3484
  res.status(200).json(this.sessionToInfo(session));
2347
3485
  }
2348
3486
  /**
@@ -2352,90 +3490,146 @@ var SessionHandler = class {
2352
3490
  */
2353
3491
  async streamSession(req, res) {
2354
3492
  this.addCorsHeaders(req, res);
2355
- const sessionId = req.params["sessionId"];
2356
- if (!sessionId) {
2357
- this.sendError(res, 400, "INVALID_REQUEST", "Missing sessionId");
2358
- return;
2359
- }
2360
- const session = this.broker.getSession(sessionId);
2361
- if (!session) {
2362
- this.sendError(res, 404, "NOT_FOUND", `Session ${sessionId} not found`);
2363
- return;
2364
- }
2365
- const parsedFromSeq = parseInt(req.query["fromSeq"] ?? "0", 10);
2366
- const fromSeq = Number.isFinite(parsedFromSeq) && parsedFromSeq >= 0 ? parsedFromSeq : 0;
3493
+ const session = await this.sessionOf(req, res);
3494
+ if (!session) return;
3495
+ const rawFromSeq = req.query["fromSeq"];
3496
+ const parsedFromSeq = typeof rawFromSeq === "string" ? parseInt(rawFromSeq, 10) : 0;
3497
+ const fromSeq = Number.isSafeInteger(parsedFromSeq) && parsedFromSeq >= 0 ? parsedFromSeq : 0;
2367
3498
  let eventFilter;
2368
- if (req.query["filter"]) {
3499
+ const rawFilter = req.query["filter"];
3500
+ if (rawFilter !== void 0 && rawFilter !== "") {
3501
+ const parsedFilter = parseFilterParameter(rawFilter);
3502
+ if (!parsedFilter.ok) {
3503
+ this.sendError(res, 400, parsedFilter.code, parsedFilter.message);
3504
+ return;
3505
+ }
2369
3506
  try {
2370
- const filterConfig = JSON.parse(req.query["filter"]);
2371
- eventFilter = createEventFilter({ config: filterConfig });
3507
+ eventFilter = this.requestFilter(parsedFilter.value);
2372
3508
  } catch {
2373
3509
  this.sendError(res, 400, "INVALID_FILTER", "Invalid filter configuration");
2374
3510
  return;
2375
3511
  }
2376
3512
  }
2377
- res.setHeader("Content-Type", "application/x-ndjson");
2378
- res.setHeader("Cache-Control", "no-cache");
2379
- res.setHeader("Connection", "keep-alive");
2380
- res.setHeader("X-Session-ID", session.sessionId);
2381
- const pastEvents = session.getEvents();
2382
- for (const event of pastEvents) {
2383
- if (event.seq >= fromSeq) {
2384
- if (eventFilter && !eventFilter.shouldSend(event)) {
2385
- continue;
2386
- }
2387
- this.writeEvent(res, event);
2388
- }
3513
+ const firstReplayable = session.firstReplayableSeq;
3514
+ if (firstReplayable > 1 && fromSeq < firstReplayable) {
3515
+ this.sendError(res, 410, "EVENTS_GONE", "The requested events are no longer kept for this session");
3516
+ return;
2389
3517
  }
2390
- if (session.isTerminal()) {
2391
- res.end();
3518
+ const release = this.acquireStream(session);
3519
+ if (!release) {
3520
+ this.sendError(res, 429, "TOO_MANY_STREAMS", "This session has too many open streams");
2392
3521
  return;
2393
3522
  }
2394
- const unsubscribe = session.onEvent((event) => {
2395
- if (event.seq >= fromSeq) {
2396
- if (eventFilter && !eventFilter.shouldSend(event)) {
2397
- if (event.type === "final") {
2398
- unsubscribe();
2399
- res.end();
3523
+ let unsubscribe = () => void 0;
3524
+ let closed = false;
3525
+ const close = (drop = false) => {
3526
+ if (closed) return;
3527
+ closed = true;
3528
+ unsubscribe();
3529
+ release();
3530
+ if (drop) {
3531
+ this.dropReader(res);
3532
+ } else {
3533
+ res.end();
3534
+ }
3535
+ };
3536
+ try {
3537
+ res.setHeader("Content-Type", "application/x-ndjson");
3538
+ res.setHeader("Cache-Control", "no-cache");
3539
+ res.setHeader("Connection", "keep-alive");
3540
+ res.setHeader("X-Session-ID", session.sessionId);
3541
+ const pastEvents = session.getEvents();
3542
+ for (const event of pastEvents) {
3543
+ if (event.seq >= fromSeq) {
3544
+ if (eventFilter && !eventFilter.shouldSend(event)) {
3545
+ continue;
3546
+ }
3547
+ if (!this.writeEvent(res, event)) {
3548
+ close(true);
3549
+ return;
2400
3550
  }
2401
- return;
2402
3551
  }
2403
- this.writeEvent(res, event);
2404
3552
  }
2405
- if (event.type === "final") {
2406
- unsubscribe();
2407
- res.end();
3553
+ if (session.isTerminal()) {
3554
+ close();
3555
+ return;
2408
3556
  }
2409
- });
2410
- if (req.signal) {
2411
- req.signal.addEventListener(
2412
- "abort",
2413
- () => {
2414
- unsubscribe();
2415
- },
2416
- { once: true }
2417
- );
3557
+ unsubscribe = session.onEvent((event) => {
3558
+ if (closed) return;
3559
+ if (event.seq >= fromSeq && (!eventFilter || eventFilter.shouldSend(event))) {
3560
+ if (!this.writeEvent(res, event)) {
3561
+ close(true);
3562
+ return;
3563
+ }
3564
+ }
3565
+ if (event.type === "final") {
3566
+ close();
3567
+ }
3568
+ });
3569
+ if (req.signal) {
3570
+ req.signal.addEventListener("abort", () => close(), { once: true });
3571
+ if (req.signal.aborted) close();
3572
+ }
3573
+ } catch (error) {
3574
+ close(true);
3575
+ throw error;
2418
3576
  }
2419
3577
  }
2420
3578
  /**
2421
3579
  * Terminate a session
2422
3580
  *
2423
3581
  * DELETE /sessions/:sessionId
3582
+ * POST /sessions/:sessionId/cancel (the URL a session announces in its first event)
2424
3583
  */
2425
3584
  async terminateSession(req, res) {
2426
3585
  this.addCorsHeaders(req, res);
2427
- const sessionId = req.params["sessionId"];
2428
- if (!sessionId) {
2429
- this.sendError(res, 400, "INVALID_REQUEST", "Missing sessionId");
3586
+ const session = await this.sessionOf(req, res);
3587
+ if (!session) return;
3588
+ const instanceId = this.instanceIdOf(req, res);
3589
+ if (!instanceId) return;
3590
+ if (instanceId.value !== void 0 && session.instanceId === void 0) {
3591
+ this.sendError(res, 409, "SESSION_INSTANCE_UNAVAILABLE", "Session has no creation identifier");
3592
+ return;
3593
+ }
3594
+ if (session.instanceId !== instanceId.value) {
3595
+ this.sendError(res, 412, "SESSION_INSTANCE_MISMATCH", "Session was replaced");
2430
3596
  return;
2431
3597
  }
2432
- const success = await this.broker.terminateSession(sessionId);
3598
+ const sessionId = session.sessionId;
3599
+ const success = await this.broker.terminateSession(sessionId, void 0, session);
2433
3600
  if (!success) {
2434
- this.sendError(res, 404, "NOT_FOUND", `Session ${sessionId} not found`);
3601
+ this.sendError(res, 404, "NOT_FOUND", "Session not found");
2435
3602
  return;
2436
3603
  }
2437
3604
  res.status(200).json({ success: true, sessionId });
2438
3605
  }
3606
+ /**
3607
+ * The filter for a configuration that arrived in a request and passed validation. It gets a
3608
+ * budget for the whole stream, and its globs are matched without expansion.
3609
+ */
3610
+ requestFilter(config) {
3611
+ return createEventFilter({ config, budgetMs: REQUEST_FILTER_LIMITS.budgetMs, expandGlobs: false });
3612
+ }
3613
+ /**
3614
+ * The limits a request gets: what it asks for, never above what the broker is configured
3615
+ * with. A request may lower a limit; it cannot raise one.
3616
+ */
3617
+ boundLimits(requested) {
3618
+ const ceiling = this.broker.sessionLimits ?? import_types5.DEFAULT_SESSION_LIMITS;
3619
+ const limits = {};
3620
+ if (requested.maxExecutionMs !== void 0) {
3621
+ limits.sessionTtlMs = Math.min(requested.maxExecutionMs, ceiling.sessionTtlMs);
3622
+ }
3623
+ if (requested.maxToolCalls !== void 0) {
3624
+ limits.maxToolCalls = Math.min(requested.maxToolCalls, ceiling.maxToolCalls);
3625
+ }
3626
+ if (requested.heartbeatIntervalMs !== void 0 && ceiling.heartbeatIntervalMs > 0) {
3627
+ const shortest = Math.min(MIN_REQUEST_HEARTBEAT_MS, ceiling.heartbeatIntervalMs);
3628
+ const longest = Math.max(MAX_REQUEST_HEARTBEAT_MS, ceiling.heartbeatIntervalMs);
3629
+ limits.heartbeatIntervalMs = Math.min(Math.max(requested.heartbeatIntervalMs, shortest), longest);
3630
+ }
3631
+ return limits;
3632
+ }
2439
3633
  /**
2440
3634
  * Convert BrokerSession to info response
2441
3635
  */
@@ -2469,13 +3663,51 @@ var SessionHandler = class {
2469
3663
  };
2470
3664
  }
2471
3665
  /**
2472
- * Write a stream event as NDJSON
3666
+ * Count one more stream of a session. Returns the function that gives the place back, or
3667
+ * undefined when the session has as many streams as `maxStreamsPerSession` allows.
3668
+ */
3669
+ acquireStream(session) {
3670
+ const open = this.streams.get(session) ?? 0;
3671
+ if (open >= this.maxStreamsPerSession) {
3672
+ return void 0;
3673
+ }
3674
+ this.streams.set(session, open + 1);
3675
+ let released = false;
3676
+ return () => {
3677
+ if (released) return;
3678
+ released = true;
3679
+ this.streams.set(session, Math.max(0, (this.streams.get(session) ?? 1) - 1));
3680
+ };
3681
+ }
3682
+ /**
3683
+ * Write a stream event as NDJSON. Returns false when the reader of this response has fallen
3684
+ * behind by more than `maxBufferedBytes`: it must be dropped, nothing more is written to it.
2473
3685
  */
2474
3686
  writeEvent(res, event) {
2475
- res.write(JSON.stringify(event) + "\n");
3687
+ let line;
3688
+ try {
3689
+ line = JSON.stringify(event);
3690
+ } catch {
3691
+ return true;
3692
+ }
3693
+ res.write(line + "\n");
2476
3694
  if (res.flush) {
2477
3695
  res.flush();
2478
3696
  }
3697
+ return !res.bufferedBytes || res.bufferedBytes() <= this.maxBufferedBytes;
3698
+ }
3699
+ /**
3700
+ * Close the connection of a reader that does not take what it is sent.
3701
+ */
3702
+ dropReader(res) {
3703
+ try {
3704
+ if (res.destroy) {
3705
+ res.destroy();
3706
+ } else {
3707
+ res.end();
3708
+ }
3709
+ } catch {
3710
+ }
2479
3711
  }
2480
3712
  /**
2481
3713
  * Send an error response
@@ -2485,14 +3717,36 @@ var SessionHandler = class {
2485
3717
  res.status(status).json(error);
2486
3718
  }
2487
3719
  };
3720
+ function positiveNumber(value, name, fallback) {
3721
+ if (value === void 0) return fallback;
3722
+ if (typeof value !== "number" || Number.isNaN(value) || value <= 0) {
3723
+ throw new TypeError(`SessionHandler: "${name}" must be a number above zero`);
3724
+ }
3725
+ return value;
3726
+ }
3727
+ function warnUnknownKeys(owner, config, known) {
3728
+ for (const key of Object.keys(config)) {
3729
+ if (!known.includes(key)) {
3730
+ console.warn(
3731
+ `[@enclave-vm/broker] ${owner}: unknown configuration key ${JSON.stringify(key.slice(0, 64))} is ignored`
3732
+ );
3733
+ }
3734
+ }
3735
+ }
2488
3736
  function createSessionHandler(config) {
2489
3737
  return new SessionHandler(config);
2490
3738
  }
2491
3739
 
2492
3740
  // libs/broker/src/http/express-adapter.ts
2493
- function adaptRequest(req) {
3741
+ function adaptRequest(req, res) {
2494
3742
  const abortController = new AbortController();
2495
- if (req.on && req.socket) {
3743
+ if (res?.on) {
3744
+ res.on("close", () => {
3745
+ if (!res.writableFinished) {
3746
+ abortController.abort();
3747
+ }
3748
+ });
3749
+ } else if (req.on && req.socket) {
2496
3750
  req.on("close", () => {
2497
3751
  if (req.socket?.destroyed) {
2498
3752
  abortController.abort();
@@ -2506,7 +3760,8 @@ function adaptRequest(req) {
2506
3760
  query: req.query,
2507
3761
  body: req.body,
2508
3762
  headers: req.headers,
2509
- signal: abortController.signal
3763
+ signal: abortController.signal,
3764
+ raw: req
2510
3765
  };
2511
3766
  }
2512
3767
  function adaptResponse(res) {
@@ -2523,11 +3778,21 @@ function adaptResponse(res) {
2523
3778
  return this;
2524
3779
  },
2525
3780
  write(data) {
2526
- res.write(data);
3781
+ return res.write(data);
2527
3782
  },
2528
3783
  end() {
2529
3784
  res.end();
2530
3785
  },
3786
+ bufferedBytes() {
3787
+ return res.writableLength ?? 0;
3788
+ },
3789
+ destroy() {
3790
+ if (res.destroy) {
3791
+ res.destroy();
3792
+ } else {
3793
+ res.end();
3794
+ }
3795
+ },
2531
3796
  flush() {
2532
3797
  if (res.flushHeaders) {
2533
3798
  res.flushHeaders();
@@ -2546,7 +3811,7 @@ function createExpressRouter(handler) {
2546
3811
  method,
2547
3812
  path: route.path,
2548
3813
  handler: (req, res) => {
2549
- const brokerReq = adaptRequest(req);
3814
+ const brokerReq = adaptRequest(req, res);
2550
3815
  const brokerRes = adaptResponse(res);
2551
3816
  route.handler(brokerReq, brokerRes).catch((error) => {
2552
3817
  console.error("Route handler error:", error);
@@ -2582,7 +3847,7 @@ function registerExpressRoutes(router, handler) {
2582
3847
  for (const route of handler.getRoutes()) {
2583
3848
  const method = route.method.toLowerCase();
2584
3849
  const expressHandler = (req, res) => {
2585
- const brokerReq = adaptRequest(req);
3850
+ const brokerReq = adaptRequest(req, res);
2586
3851
  const brokerRes = adaptResponse(res);
2587
3852
  route.handler(brokerReq, brokerRes).catch((error) => {
2588
3853
  console.error("Route handler error:", error);
@@ -2666,8 +3931,11 @@ var CatalogHandler = class {
2666
3931
  // Annotate the CommonJS export names for ESM import in node:
2667
3932
  0 && (module.exports = {
2668
3933
  Broker,
3934
+ BrokerError,
3935
+ BrokerErrorCode,
2669
3936
  BrokerSession,
2670
3937
  CatalogHandler,
3938
+ DEFAULT_SESSION_HISTORY_LIMITS,
2671
3939
  EventFilter,
2672
3940
  OpenApiSource,
2673
3941
  OpenApiSpecPoller,