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