@enclave-vm/broker 2.19.2 → 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/README.md CHANGED
@@ -159,16 +159,16 @@ The routes answer the preflight (`OPTIONS`) themselves, and a browser sends it w
159
159
 
160
160
  `limits` (see `SessionLimits` in `@enclave-vm/types`) bound every session of a broker. A session created in process may name its own value for a limit; a session created over HTTP may only lower `maxToolCalls` and the lifetime.
161
161
 
162
- | Limit | Default | What it bounds |
163
- | --------------------- | -------- | -------------------------------------------------------------------------------------------------------- |
164
- | `maxToolCalls` | 50 | Tool calls of one session |
165
- | `toolTimeoutMs` | 30000 | The run time of the script (it is passed to the sandbox as its timeout) |
166
- | `perToolDeadlineMs` | 30000 | One tool call: the handler's `signal` is aborted. A tool's own `config.timeout` applies when it is lower |
167
- | `deadlineMs` | 0 (none) | The whole session, counted from its creation |
168
- | `sessionTtlMs` | 60000 | How long a session stays registered; a session past it is cancelled at the next cleanup |
169
- | `maxToolResultBytes` | 5 MB | A tool result, as serialized JSON; a larger one fails the call with `TOOL_RESULT_TOO_LARGE` |
170
- | `heartbeatIntervalMs` | 15000 | Interval of `heartbeat` events |
171
- | `cancelOnFirstError` | false | Whether the first failed tool call cancels the session |
162
+ | Limit | Default | What it bounds |
163
+ | --------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
164
+ | `maxToolCalls` | 50 | Tool calls of one session |
165
+ | `toolTimeoutMs` | 30000 | The run time of the script (it is passed to the sandbox as its timeout) |
166
+ | `perToolDeadlineMs` | 30000 | One tool call: the handler's `signal` is aborted. A tool's own `config.timeout` applies when it is lower |
167
+ | `deadlineMs` | 0 (none) | The whole session, counted from its creation |
168
+ | `sessionTtlMs` | 60000 | How long a session lives: a session still running when it is reached is cancelled then (its tool calls get the abort signal), and an idle one is removed at the next cleanup |
169
+ | `maxToolResultBytes` | 5 MB | A tool result, as serialized JSON; a larger one fails the call with `TOOL_RESULT_TOO_LARGE` |
170
+ | `heartbeatIntervalMs` | 15000 | Interval of `heartbeat` events |
171
+ | `cancelOnFirstError` | false | Whether the first failed tool call cancels the session |
172
172
 
173
173
  `maxStdoutBytes` is not used: the broker emits no `stdout` events.
174
174
 
@@ -236,6 +236,8 @@ A script decides the arguments of a generated tool, so whatever an operation of
236
236
 
237
237
  Secrets are resolved per tool and handed to the handler only; they are not part of any event. Tool arguments and the result of the script are part of the events: do not put a secret into them, and do not return one from a tool.
238
238
 
239
+ The message and code of an exception a handler throws are passed on to the script, and to the final event when the script does not catch it, with the value of each secret the handler was given replaced by `[REDACTED]`. Only the value as it is is recognised, not an encoded or partial form of it: a handler does not put a secret into an error.
240
+
239
241
  ## Related packages
240
242
 
241
243
  | Package | Description |
@@ -44,6 +44,8 @@ export interface BrokerSessionConfig {
44
44
  * one is given here, and is then not reachable over routes that identify their callers.
45
45
  */
46
46
  owner?: string;
47
+ /** Optional creation identifier used by the HTTP routes for conditional cancellation. */
48
+ instanceId?: string;
47
49
  }
48
50
  /**
49
51
  * Broker Session
@@ -59,18 +61,28 @@ export declare class BrokerSession {
59
61
  * The caller this session belongs to, if any.
60
62
  */
61
63
  readonly owner?: string;
64
+ /** Creation identifier supplied by the HTTP client, if any. */
65
+ readonly instanceId?: string;
62
66
  private readonly emitter;
63
67
  private readonly toolRegistry;
64
68
  private readonly enclave;
65
69
  private readonly abortController;
66
70
  private readonly limits;
67
71
  private readonly checkResultSize;
72
+ /** The payload limit of the sandbox's string bridge; undefined when the bridge is not used. */
73
+ private readonly bridgePayloadBytes;
74
+ /**
75
+ * The size from which the sandbox's sidecar holds a string result as a reference (measured by
76
+ * the bridge after that); undefined without a sidecar.
77
+ */
78
+ private readonly sidecarExtractionBytes;
68
79
  private _state;
69
80
  private executionPromise;
70
81
  private toolCallCount;
71
82
  private stdoutBytes;
72
83
  private heartbeatInterval;
73
84
  private deadlineTimer;
85
+ private lifetimeTimer;
74
86
  private _deadlineExceeded;
75
87
  private readonly partialErrors;
76
88
  constructor(toolRegistry: ToolRegistry, config?: BrokerSessionConfig);
@@ -128,9 +140,11 @@ export declare class BrokerSession {
128
140
  */
129
141
  private executeTool;
130
142
  /**
131
- * Whether a tool result is within `limits.maxToolResultBytes`, measured as serialized JSON.
143
+ * Why a tool result is refused, or undefined: over `limits.maxToolResultBytes`, or over the
144
+ * payload limit of the sandbox's string bridge (with the bytes of its response envelope),
145
+ * measured as serialized JSON.
132
146
  */
133
- private resultWithinLimit;
147
+ private resultRefusal;
134
148
  /**
135
149
  * Create a custom event with proper base fields and unique sequence number.
136
150
  */
package/broker.d.ts CHANGED
@@ -214,7 +214,7 @@ export declare class Broker {
214
214
  /**
215
215
  * Terminate a session
216
216
  */
217
- terminateSession(sessionId: SessionId, reason?: string): Promise<boolean>;
217
+ terminateSession(sessionId: SessionId, reason?: string, expectedSession?: BrokerSession): Promise<boolean>;
218
218
  /**
219
219
  * Execute code and wait for result
220
220
  *
package/esm/index.mjs CHANGED
@@ -62,6 +62,7 @@ var BoundedSessionEmitter = class extends SessionEmitter {
62
62
  kept = [];
63
63
  keptBytes = 0;
64
64
  droppedThrough = 0;
65
+ ended = false;
65
66
  /**
66
67
  * Size of the event that is emitted next, when the caller knows it already. Used once.
67
68
  */
@@ -74,6 +75,8 @@ var BoundedSessionEmitter = class extends SessionEmitter {
74
75
  * Emit a stream event: keep it within the bounds, then hand it to the listeners.
75
76
  */
76
77
  emit(event) {
78
+ if (this.ended) return;
79
+ if (event.type === "final") this.ended = true;
77
80
  this.keep(event);
78
81
  try {
79
82
  super.emit(event);
@@ -110,7 +113,7 @@ var BoundedSessionEmitter = class extends SessionEmitter {
110
113
  this.nextEventSize = void 0;
111
114
  if (size === void 0) {
112
115
  try {
113
- size = JSON.stringify(event).length;
116
+ size = Buffer.byteLength(JSON.stringify(event), "utf8");
114
117
  } catch {
115
118
  size = UNMEASURED_EVENT_BYTES;
116
119
  }
@@ -127,6 +130,27 @@ var BoundedSessionEmitter = class extends SessionEmitter {
127
130
 
128
131
  // libs/broker/src/tool-registry.ts
129
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
+ }
130
154
  var ToolRegistry = class {
131
155
  tools;
132
156
  secretStore;
@@ -304,6 +328,7 @@ var ToolRegistry = class {
304
328
  durationMs: Date.now() - startTime
305
329
  };
306
330
  }
331
+ const secretValues = Object.values(secrets);
307
332
  try {
308
333
  const result = await tool.definition.handler(validation.validatedArgs, {
309
334
  ...context,
@@ -319,8 +344,8 @@ var ToolRegistry = class {
319
344
  return {
320
345
  success: false,
321
346
  error: {
322
- code: err.code ?? "EXECUTION_ERROR",
323
- message: err.message
347
+ code: redactSecrets(err.code ?? "EXECUTION_ERROR", secretValues),
348
+ message: redactSecrets(err.message, secretValues)
324
349
  },
325
350
  durationMs: Date.now() - startTime
326
351
  };
@@ -351,8 +376,31 @@ function createToolRegistry() {
351
376
 
352
377
  // libs/broker/src/broker-session.ts
353
378
  import { generateSessionId, generateCallId, EventType, PROTOCOL_VERSION } from "@enclave-vm/types";
354
- import { Enclave } from "@enclave-vm/core";
379
+ import { Enclave, REFERENCE_CONFIGS } from "@enclave-vm/core";
355
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;
356
404
  var BrokerSession = class {
357
405
  sessionId;
358
406
  createdAt;
@@ -361,22 +409,36 @@ var BrokerSession = class {
361
409
  * The caller this session belongs to, if any.
362
410
  */
363
411
  owner;
412
+ /** Creation identifier supplied by the HTTP client, if any. */
413
+ instanceId;
364
414
  emitter;
365
415
  toolRegistry;
366
416
  enclave;
367
417
  abortController;
368
418
  limits;
369
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;
370
427
  _state = "starting";
371
428
  executionPromise = null;
372
429
  toolCallCount = 0;
373
430
  stdoutBytes = 0;
374
431
  heartbeatInterval = null;
375
432
  deadlineTimer = null;
433
+ lifetimeTimer = null;
376
434
  _deadlineExceeded = false;
377
435
  partialErrors = [];
378
436
  constructor(toolRegistry, config = {}) {
379
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;
380
442
  if (config.owner !== void 0) {
381
443
  if (typeof config.owner !== "string" || config.owner.length === 0) {
382
444
  throw new TypeError("Session owner must be a non-empty string");
@@ -388,7 +450,10 @@ var BrokerSession = class {
388
450
  this.abortController = new AbortController();
389
451
  this.limits = resolveSessionLimits(config.limits);
390
452
  const bridge = config.enclaveConfig?.toolBridge;
391
- this.checkResultSize = (bridge?.mode ?? "string") !== "string" || this.limits.maxToolResultBytes < (bridge?.maxPayloadBytes ?? DEFAULT_BRIDGE_PAYLOAD_BYTES);
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;
392
457
  this.expiresAt = this.createdAt + this.limits.sessionTtlMs;
393
458
  this.emitter = new BoundedSessionEmitter(
394
459
  {
@@ -504,6 +569,13 @@ var BrokerSession = class {
504
569
  return finalResult;
505
570
  }
506
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
+ );
507
579
  this._state = "running";
508
580
  this.executionPromise = this.runExecution(code);
509
581
  return this.executionPromise;
@@ -515,12 +587,21 @@ var BrokerSession = class {
515
587
  const startTime = Date.now();
516
588
  let result;
517
589
  try {
518
- const enclaveResult = await this.enclave.run(code, async (toolName, args) => {
519
- if (this.abortController.signal.aborted) {
520
- 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
+ }
521
603
  }
522
- return this.executeTool(toolName, args);
523
- });
604
+ );
524
605
  const endTime = Date.now();
525
606
  const stats = this.buildStats(startTime, endTime);
526
607
  const eventStats = {
@@ -708,15 +789,11 @@ var BrokerSession = class {
708
789
  if (toolTimer) clearTimeout(toolTimer);
709
790
  this.abortController.signal.removeEventListener("abort", onSessionAbort);
710
791
  }
711
- if (result.success && !this.resultWithinLimit(result.value)) {
712
- result = {
713
- success: false,
714
- error: {
715
- code: "TOOL_RESULT_TOO_LARGE",
716
- message: `Tool result exceeds the limit of ${this.limits.maxToolResultBytes} bytes`
717
- },
718
- durationMs: result.durationMs
719
- };
792
+ if (result.success) {
793
+ const refusal = this.resultRefusal(result.value);
794
+ if (refusal) {
795
+ result = { success: false, error: refusal, durationMs: result.durationMs };
796
+ }
720
797
  }
721
798
  this.emitter.emitToolResultApplied(callId);
722
799
  if (result.success) {
@@ -731,19 +808,33 @@ var BrokerSession = class {
731
808
  }
732
809
  }
733
810
  /**
734
- * Whether a tool result is within `limits.maxToolResultBytes`, measured as serialized JSON.
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.
735
814
  */
736
- resultWithinLimit(value) {
737
- if (!this.checkResultSize) return true;
815
+ resultRefusal(value) {
738
816
  const limit = this.limits.maxToolResultBytes;
739
- if (!(limit > 0) || limit === Infinity) return true;
817
+ const checkOwn = this.checkResultSize && limit > 0 && limit !== Infinity;
818
+ if (!checkOwn && this.bridgePayloadBytes === void 0) return void 0;
740
819
  let serialized;
741
820
  try {
742
821
  serialized = JSON.stringify(value);
743
822
  } catch {
744
- return true;
823
+ return void 0;
745
824
  }
746
- return serialized === void 0 || Buffer.byteLength(serialized, "utf8") <= limit;
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;
747
838
  }
748
839
  /**
749
840
  * Create a custom event with proper base fields and unique sequence number.
@@ -860,6 +951,10 @@ var BrokerSession = class {
860
951
  clearTimeout(this.deadlineTimer);
861
952
  this.deadlineTimer = null;
862
953
  }
954
+ if (this.lifetimeTimer) {
955
+ clearTimeout(this.lifetimeTimer);
956
+ this.lifetimeTimer = null;
957
+ }
863
958
  }
864
959
  /**
865
960
  * Dispose of the session and its resources
@@ -876,7 +971,10 @@ function snapshotArguments(args) {
876
971
  try {
877
972
  const serialized = JSON.stringify(args);
878
973
  if (serialized !== void 0) {
879
- return { copy: JSON.parse(serialized), size: serialized.length };
974
+ return {
975
+ copy: JSON.parse(serialized),
976
+ size: Buffer.byteLength(serialized, "utf8")
977
+ };
880
978
  }
881
979
  } catch {
882
980
  }
@@ -957,6 +1055,7 @@ var SessionManager = class {
957
1055
  sessionConfig[key] = value;
958
1056
  }
959
1057
  }
1058
+ sessionConfig.instanceId = config?.instanceId;
960
1059
  sessionConfig.limits = mergeSessionLimits(defaults.limits, config?.limits);
961
1060
  const session = createBrokerSession(this.toolRegistry, sessionConfig);
962
1061
  this.sessions.set(session.sessionId, session);
@@ -999,14 +1098,14 @@ var SessionManager = class {
999
1098
  /**
1000
1099
  * Terminate a session
1001
1100
  */
1002
- async terminate(sessionId, reason) {
1101
+ async terminate(sessionId, reason, expectedSession) {
1003
1102
  const session = this.sessions.get(sessionId);
1004
- if (!session) {
1103
+ if (!session || expectedSession !== void 0 && session !== expectedSession) {
1005
1104
  return false;
1006
1105
  }
1007
1106
  await session.cancel(reason ?? "Terminated by session manager");
1008
1107
  session.dispose();
1009
- this.sessions.delete(sessionId);
1108
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
1010
1109
  return true;
1011
1110
  }
1012
1111
  /**
@@ -1025,7 +1124,7 @@ var SessionManager = class {
1025
1124
  return result;
1026
1125
  } finally {
1027
1126
  session.dispose();
1028
- this.sessions.delete(sessionId);
1127
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
1029
1128
  }
1030
1129
  }
1031
1130
  /**
@@ -1177,7 +1276,9 @@ async function fetchOriginBound(url, request) {
1177
1276
  let current = url;
1178
1277
  let method = request.method;
1179
1278
  let body = request.body;
1180
- let headers = request.headers;
1279
+ const portable = request.portableHeaders ?? {};
1280
+ let own = { ...request.headers, ...portable };
1281
+ let headers = own;
1181
1282
  for (let hop = 0; ; hop++) {
1182
1283
  const response = await fetch(current, { method, headers, body, signal: request.signal, redirect: "manual" });
1183
1284
  const location = REDIRECT_STATUS.includes(response.status) ? response.headers.get("location") : null;
@@ -1201,9 +1302,12 @@ async function fetchOriginBound(url, request) {
1201
1302
  method = "GET";
1202
1303
  body = void 0;
1203
1304
  headers = withoutContentType(headers);
1305
+ own = withoutContentType(own);
1204
1306
  }
1205
1307
  if (next.origin !== origin) {
1206
- headers = body === void 0 ? {} : onlyContentType(headers);
1308
+ headers = { ...body === void 0 ? {} : onlyContentType(headers), ...portable };
1309
+ } else if (request.restoreOnOrigin) {
1310
+ headers = own;
1207
1311
  }
1208
1312
  current = next.href;
1209
1313
  }
@@ -1810,20 +1914,25 @@ var OpenApiSpecPoller = class extends EventEmitter {
1810
1914
  async doFetch(runSignal) {
1811
1915
  if (runSignal.aborted) return;
1812
1916
  const headers = { ...this.config.headers };
1917
+ const validators = {};
1813
1918
  if (this.config.changeDetection !== "content-hash") {
1814
1919
  if (this.lastEtag) {
1815
- headers["If-None-Match"] = this.lastEtag;
1920
+ validators["If-None-Match"] = this.lastEtag;
1816
1921
  }
1817
1922
  if (this.lastModified) {
1818
- headers["If-Modified-Since"] = this.lastModified;
1923
+ validators["If-Modified-Since"] = this.lastModified;
1819
1924
  }
1820
1925
  }
1821
1926
  const controller = new AbortController();
1822
1927
  this._fetchController = controller;
1823
1928
  const timeout = setTimeout(() => controller.abort(), this.config.fetchTimeoutMs);
1824
1929
  try {
1825
- const response = await fetch(this.config.url, {
1930
+ const response = await fetchOriginBound(this.config.url, {
1931
+ method: "GET",
1826
1932
  headers,
1933
+ portableHeaders: validators,
1934
+ // A redirect that comes back to the specification's origin gets its headers again
1935
+ restoreOnOrigin: true,
1827
1936
  signal: controller.signal
1828
1937
  });
1829
1938
  if (runSignal.aborted) return;
@@ -2302,8 +2411,8 @@ var Broker = class {
2302
2411
  /**
2303
2412
  * Terminate a session
2304
2413
  */
2305
- async terminateSession(sessionId, reason) {
2306
- return this.sessionManager.terminate(sessionId, reason);
2414
+ async terminateSession(sessionId, reason, expectedSession) {
2415
+ return this.sessionManager.terminate(sessionId, reason, expectedSession);
2307
2416
  }
2308
2417
  // ============================================================================
2309
2418
  // Quick Execution
@@ -3029,7 +3138,8 @@ function parseFilterParameter(value) {
3029
3138
  }
3030
3139
 
3031
3140
  // libs/broker/src/http/session-handler.ts
3032
- var CORS_REQUEST_HEADERS = ["Content-Type", "Accept", "Last-Event-ID", "Authorization"];
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}$/;
3033
3143
  var HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
3034
3144
  function corsRequestHeaders(configured) {
3035
3145
  if (configured !== void 0 && !Array.isArray(configured)) {
@@ -3174,6 +3284,17 @@ var SessionHandler = class {
3174
3284
  }
3175
3285
  return session;
3176
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
+ }
3177
3298
  /**
3178
3299
  * List sessions: all of them, or the caller's own when callers are identified
3179
3300
  *
@@ -3210,6 +3331,8 @@ var SessionHandler = class {
3210
3331
  this.addCorsHeaders(req, res);
3211
3332
  const identity = await this.callerOf(req, res);
3212
3333
  if (!identity) return;
3334
+ const instanceId = this.instanceIdOf(req, res);
3335
+ if (!instanceId) return;
3213
3336
  const parsed = parseCreateSessionRequest(req.body);
3214
3337
  if (!parsed.ok) {
3215
3338
  this.sendError(res, 400, parsed.code, parsed.message);
@@ -3227,7 +3350,7 @@ var SessionHandler = class {
3227
3350
  if (filter) {
3228
3351
  eventFilter = this.requestFilter(filter);
3229
3352
  }
3230
- session = this.broker.createSession({ sessionId, limits, owner: identity.caller });
3353
+ session = this.broker.createSession({ sessionId, limits, owner: identity.caller, instanceId: instanceId.value });
3231
3354
  } catch (error) {
3232
3355
  this.sendCreateFailure(res, error);
3233
3356
  return;
@@ -3268,7 +3391,7 @@ var SessionHandler = class {
3268
3391
  }
3269
3392
  } catch {
3270
3393
  cleanup();
3271
- await this.broker.terminateSession(session.sessionId).catch(() => false);
3394
+ await this.broker.terminateSession(session.sessionId, void 0, session).catch(() => false);
3272
3395
  this.sendError(res, 500, "SESSION_CREATE_ERROR", "Failed to create session");
3273
3396
  return;
3274
3397
  }
@@ -3414,8 +3537,18 @@ var SessionHandler = class {
3414
3537
  this.addCorsHeaders(req, res);
3415
3538
  const session = await this.sessionOf(req, res);
3416
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");
3548
+ return;
3549
+ }
3417
3550
  const sessionId = session.sessionId;
3418
- const success = await this.broker.terminateSession(sessionId);
3551
+ const success = await this.broker.terminateSession(sessionId, void 0, session);
3419
3552
  if (!success) {
3420
3553
  this.sendError(res, 404, "NOT_FOUND", "Session not found");
3421
3554
  return;
@@ -127,6 +127,8 @@ export declare class SessionHandler {
127
127
  * answered exactly like an id that does not exist.
128
128
  */
129
129
  private sessionOf;
130
+ /** Optional identifier for one creation, sent again on a conditional stop. */
131
+ private instanceIdOf;
130
132
  /**
131
133
  * List sessions: all of them, or the caller's own when callers are identified
132
134
  *
package/index.js CHANGED
@@ -110,6 +110,7 @@ var BoundedSessionEmitter = class extends import_core.SessionEmitter {
110
110
  kept = [];
111
111
  keptBytes = 0;
112
112
  droppedThrough = 0;
113
+ ended = false;
113
114
  /**
114
115
  * Size of the event that is emitted next, when the caller knows it already. Used once.
115
116
  */
@@ -122,6 +123,8 @@ var BoundedSessionEmitter = class extends import_core.SessionEmitter {
122
123
  * Emit a stream event: keep it within the bounds, then hand it to the listeners.
123
124
  */
124
125
  emit(event) {
126
+ if (this.ended) return;
127
+ if (event.type === "final") this.ended = true;
125
128
  this.keep(event);
126
129
  try {
127
130
  super.emit(event);
@@ -158,7 +161,7 @@ var BoundedSessionEmitter = class extends import_core.SessionEmitter {
158
161
  this.nextEventSize = void 0;
159
162
  if (size === void 0) {
160
163
  try {
161
- size = JSON.stringify(event).length;
164
+ size = Buffer.byteLength(JSON.stringify(event), "utf8");
162
165
  } catch {
163
166
  size = UNMEASURED_EVENT_BYTES;
164
167
  }
@@ -175,6 +178,27 @@ var BoundedSessionEmitter = class extends import_core.SessionEmitter {
175
178
 
176
179
  // libs/broker/src/tool-registry.ts
177
180
  var import_zod = require("zod");
181
+ var REDACTED_SECRET = "[REDACTED]";
182
+ function redactSecrets(text, secretValues) {
183
+ if (typeof text !== "string") return text;
184
+ const values = [...new Set(secretValues)].filter((value) => typeof value === "string" && value.length > 0);
185
+ if (!values.some((value) => text.includes(value))) return text;
186
+ values.sort((a, b) => b.length - a.length);
187
+ let redacted = "";
188
+ let i = 0;
189
+ scan: while (i < text.length) {
190
+ for (const value of values) {
191
+ if (text.startsWith(value, i)) {
192
+ redacted += REDACTED_SECRET;
193
+ i += value.length;
194
+ continue scan;
195
+ }
196
+ }
197
+ redacted += text[i];
198
+ i++;
199
+ }
200
+ return redacted;
201
+ }
178
202
  var ToolRegistry = class {
179
203
  tools;
180
204
  secretStore;
@@ -352,6 +376,7 @@ var ToolRegistry = class {
352
376
  durationMs: Date.now() - startTime
353
377
  };
354
378
  }
379
+ const secretValues = Object.values(secrets);
355
380
  try {
356
381
  const result = await tool.definition.handler(validation.validatedArgs, {
357
382
  ...context,
@@ -367,8 +392,8 @@ var ToolRegistry = class {
367
392
  return {
368
393
  success: false,
369
394
  error: {
370
- code: err.code ?? "EXECUTION_ERROR",
371
- message: err.message
395
+ code: redactSecrets(err.code ?? "EXECUTION_ERROR", secretValues),
396
+ message: redactSecrets(err.message, secretValues)
372
397
  },
373
398
  durationMs: Date.now() - startTime
374
399
  };
@@ -401,6 +426,29 @@ function createToolRegistry() {
401
426
  var import_types2 = require("@enclave-vm/types");
402
427
  var import_core2 = require("@enclave-vm/core");
403
428
  var DEFAULT_BRIDGE_PAYLOAD_BYTES = 5 * 1024 * 1024;
429
+ function isPlainData(value) {
430
+ const pending = [value];
431
+ const seen = /* @__PURE__ */ new Set();
432
+ while (pending.length > 0) {
433
+ const current = pending.pop();
434
+ if (current === null || typeof current !== "object") continue;
435
+ if (seen.has(current)) continue;
436
+ seen.add(current);
437
+ if (Array.isArray(current)) {
438
+ const length = current.length;
439
+ for (let index = 0; index < length; index++) pending.push(current[index]);
440
+ continue;
441
+ }
442
+ const prototype = Object.getPrototypeOf(current);
443
+ if (prototype !== Object.prototype && prototype !== null) return false;
444
+ for (const key of Object.keys(current)) {
445
+ if (key === "__proto__" || key === "constructor") return false;
446
+ pending.push(current[key]);
447
+ }
448
+ }
449
+ return true;
450
+ }
451
+ var BRIDGE_RESPONSE_ENVELOPE_BYTES = JSON.stringify({ v: 1, ok: true, value: 0 }).length - 1;
404
452
  var BrokerSession = class {
405
453
  sessionId;
406
454
  createdAt;
@@ -409,22 +457,36 @@ var BrokerSession = class {
409
457
  * The caller this session belongs to, if any.
410
458
  */
411
459
  owner;
460
+ /** Creation identifier supplied by the HTTP client, if any. */
461
+ instanceId;
412
462
  emitter;
413
463
  toolRegistry;
414
464
  enclave;
415
465
  abortController;
416
466
  limits;
417
467
  checkResultSize;
468
+ /** The payload limit of the sandbox's string bridge; undefined when the bridge is not used. */
469
+ bridgePayloadBytes;
470
+ /**
471
+ * The size from which the sandbox's sidecar holds a string result as a reference (measured by
472
+ * the bridge after that); undefined without a sidecar.
473
+ */
474
+ sidecarExtractionBytes;
418
475
  _state = "starting";
419
476
  executionPromise = null;
420
477
  toolCallCount = 0;
421
478
  stdoutBytes = 0;
422
479
  heartbeatInterval = null;
423
480
  deadlineTimer = null;
481
+ lifetimeTimer = null;
424
482
  _deadlineExceeded = false;
425
483
  partialErrors = [];
426
484
  constructor(toolRegistry, config = {}) {
427
485
  this.sessionId = config.sessionId ?? (0, import_types2.generateSessionId)();
486
+ if (config.instanceId !== void 0 && (typeof config.instanceId !== "string" || !/^[A-Za-z0-9._:~-]{1,128}$/.test(config.instanceId))) {
487
+ throw new TypeError("Session instance id must be a short header-safe string");
488
+ }
489
+ this.instanceId = config.instanceId;
428
490
  if (config.owner !== void 0) {
429
491
  if (typeof config.owner !== "string" || config.owner.length === 0) {
430
492
  throw new TypeError("Session owner must be a non-empty string");
@@ -436,7 +498,10 @@ var BrokerSession = class {
436
498
  this.abortController = new AbortController();
437
499
  this.limits = resolveSessionLimits(config.limits);
438
500
  const bridge = config.enclaveConfig?.toolBridge;
439
- this.checkResultSize = (bridge?.mode ?? "string") !== "string" || this.limits.maxToolResultBytes < (bridge?.maxPayloadBytes ?? DEFAULT_BRIDGE_PAYLOAD_BYTES);
501
+ this.checkResultSize = true;
502
+ this.bridgePayloadBytes = (bridge?.mode ?? "string") === "string" ? bridge?.maxPayloadBytes ?? DEFAULT_BRIDGE_PAYLOAD_BYTES : void 0;
503
+ const sidecar = config.enclaveConfig?.sidecar;
504
+ this.sidecarExtractionBytes = sidecar?.enabled ? sidecar.extractionThreshold ?? import_core2.REFERENCE_CONFIGS[config.enclaveConfig?.securityLevel ?? "STANDARD"].extractionThreshold : void 0;
440
505
  this.expiresAt = this.createdAt + this.limits.sessionTtlMs;
441
506
  this.emitter = new BoundedSessionEmitter(
442
507
  {
@@ -552,6 +617,13 @@ var BrokerSession = class {
552
617
  return finalResult;
553
618
  }
554
619
  }
620
+ const lifetimeLeft = this.expiresAt - Date.now();
621
+ this.lifetimeTimer = setTimeout(
622
+ () => {
623
+ if (!this.isTerminal()) this.cancel("Session lifetime exceeded");
624
+ },
625
+ timerDelay(Math.max(0, lifetimeLeft))
626
+ );
555
627
  this._state = "running";
556
628
  this.executionPromise = this.runExecution(code);
557
629
  return this.executionPromise;
@@ -563,12 +635,21 @@ var BrokerSession = class {
563
635
  const startTime = Date.now();
564
636
  let result;
565
637
  try {
566
- const enclaveResult = await this.enclave.run(code, async (toolName, args) => {
567
- if (this.abortController.signal.aborted) {
568
- throw new Error("Session cancelled");
638
+ const enclaveResult = await this.enclave.run(
639
+ code,
640
+ async (toolName, args) => {
641
+ if (this.abortController.signal.aborted) {
642
+ throw new Error("Session cancelled");
643
+ }
644
+ return this.executeTool(toolName, args);
645
+ },
646
+ {
647
+ maxToolResultBytes: this.checkResultSize && Number.isFinite(this.limits.maxToolResultBytes) && this.limits.maxToolResultBytes > 0 ? this.limits.maxToolResultBytes : void 0,
648
+ onToolBridgeFailure: () => {
649
+ if (this.limits.cancelOnFirstError) this.abortController.abort();
650
+ }
569
651
  }
570
- return this.executeTool(toolName, args);
571
- });
652
+ );
572
653
  const endTime = Date.now();
573
654
  const stats = this.buildStats(startTime, endTime);
574
655
  const eventStats = {
@@ -756,15 +837,11 @@ var BrokerSession = class {
756
837
  if (toolTimer) clearTimeout(toolTimer);
757
838
  this.abortController.signal.removeEventListener("abort", onSessionAbort);
758
839
  }
759
- if (result.success && !this.resultWithinLimit(result.value)) {
760
- result = {
761
- success: false,
762
- error: {
763
- code: "TOOL_RESULT_TOO_LARGE",
764
- message: `Tool result exceeds the limit of ${this.limits.maxToolResultBytes} bytes`
765
- },
766
- durationMs: result.durationMs
767
- };
840
+ if (result.success) {
841
+ const refusal = this.resultRefusal(result.value);
842
+ if (refusal) {
843
+ result = { success: false, error: refusal, durationMs: result.durationMs };
844
+ }
768
845
  }
769
846
  this.emitter.emitToolResultApplied(callId);
770
847
  if (result.success) {
@@ -779,19 +856,33 @@ var BrokerSession = class {
779
856
  }
780
857
  }
781
858
  /**
782
- * Whether a tool result is within `limits.maxToolResultBytes`, measured as serialized JSON.
859
+ * Why a tool result is refused, or undefined: over `limits.maxToolResultBytes`, or over the
860
+ * payload limit of the sandbox's string bridge (with the bytes of its response envelope),
861
+ * measured as serialized JSON.
783
862
  */
784
- resultWithinLimit(value) {
785
- if (!this.checkResultSize) return true;
863
+ resultRefusal(value) {
786
864
  const limit = this.limits.maxToolResultBytes;
787
- if (!(limit > 0) || limit === Infinity) return true;
865
+ const checkOwn = this.checkResultSize && limit > 0 && limit !== Infinity;
866
+ if (!checkOwn && this.bridgePayloadBytes === void 0) return void 0;
788
867
  let serialized;
789
868
  try {
790
869
  serialized = JSON.stringify(value);
791
870
  } catch {
792
- return true;
871
+ return void 0;
793
872
  }
794
- return serialized === void 0 || Buffer.byteLength(serialized, "utf8") <= limit;
873
+ if (serialized === void 0) return void 0;
874
+ const bytes = Buffer.byteLength(serialized, "utf8");
875
+ if (checkOwn && bytes > limit) {
876
+ return { code: "TOOL_RESULT_TOO_LARGE", message: `Tool result exceeds the limit of ${limit} bytes` };
877
+ }
878
+ const heldBySidecar = typeof value === "string" && this.sidecarExtractionBytes !== void 0 && Buffer.byteLength(value, "utf8") >= this.sidecarExtractionBytes;
879
+ if (!heldBySidecar && this.bridgePayloadBytes !== void 0 && bytes + BRIDGE_RESPONSE_ENVELOPE_BYTES > this.bridgePayloadBytes && isPlainData(value)) {
880
+ return {
881
+ code: "TOOL_BRIDGE_RESPONSE_TOO_LARGE",
882
+ message: `Tool response exceeds maximum size (${this.bridgePayloadBytes} bytes)`
883
+ };
884
+ }
885
+ return void 0;
795
886
  }
796
887
  /**
797
888
  * Create a custom event with proper base fields and unique sequence number.
@@ -908,6 +999,10 @@ var BrokerSession = class {
908
999
  clearTimeout(this.deadlineTimer);
909
1000
  this.deadlineTimer = null;
910
1001
  }
1002
+ if (this.lifetimeTimer) {
1003
+ clearTimeout(this.lifetimeTimer);
1004
+ this.lifetimeTimer = null;
1005
+ }
911
1006
  }
912
1007
  /**
913
1008
  * Dispose of the session and its resources
@@ -924,7 +1019,10 @@ function snapshotArguments(args) {
924
1019
  try {
925
1020
  const serialized = JSON.stringify(args);
926
1021
  if (serialized !== void 0) {
927
- return { copy: JSON.parse(serialized), size: serialized.length };
1022
+ return {
1023
+ copy: JSON.parse(serialized),
1024
+ size: Buffer.byteLength(serialized, "utf8")
1025
+ };
928
1026
  }
929
1027
  } catch {
930
1028
  }
@@ -1005,6 +1103,7 @@ var SessionManager = class {
1005
1103
  sessionConfig[key] = value;
1006
1104
  }
1007
1105
  }
1106
+ sessionConfig.instanceId = config?.instanceId;
1008
1107
  sessionConfig.limits = mergeSessionLimits(defaults.limits, config?.limits);
1009
1108
  const session = createBrokerSession(this.toolRegistry, sessionConfig);
1010
1109
  this.sessions.set(session.sessionId, session);
@@ -1047,14 +1146,14 @@ var SessionManager = class {
1047
1146
  /**
1048
1147
  * Terminate a session
1049
1148
  */
1050
- async terminate(sessionId, reason) {
1149
+ async terminate(sessionId, reason, expectedSession) {
1051
1150
  const session = this.sessions.get(sessionId);
1052
- if (!session) {
1151
+ if (!session || expectedSession !== void 0 && session !== expectedSession) {
1053
1152
  return false;
1054
1153
  }
1055
1154
  await session.cancel(reason ?? "Terminated by session manager");
1056
1155
  session.dispose();
1057
- this.sessions.delete(sessionId);
1156
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
1058
1157
  return true;
1059
1158
  }
1060
1159
  /**
@@ -1073,7 +1172,7 @@ var SessionManager = class {
1073
1172
  return result;
1074
1173
  } finally {
1075
1174
  session.dispose();
1076
- this.sessions.delete(sessionId);
1175
+ if (this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
1077
1176
  }
1078
1177
  }
1079
1178
  /**
@@ -1225,7 +1324,9 @@ async function fetchOriginBound(url, request) {
1225
1324
  let current = url;
1226
1325
  let method = request.method;
1227
1326
  let body = request.body;
1228
- let headers = request.headers;
1327
+ const portable = request.portableHeaders ?? {};
1328
+ let own = { ...request.headers, ...portable };
1329
+ let headers = own;
1229
1330
  for (let hop = 0; ; hop++) {
1230
1331
  const response = await fetch(current, { method, headers, body, signal: request.signal, redirect: "manual" });
1231
1332
  const location = REDIRECT_STATUS.includes(response.status) ? response.headers.get("location") : null;
@@ -1249,9 +1350,12 @@ async function fetchOriginBound(url, request) {
1249
1350
  method = "GET";
1250
1351
  body = void 0;
1251
1352
  headers = withoutContentType(headers);
1353
+ own = withoutContentType(own);
1252
1354
  }
1253
1355
  if (next.origin !== origin) {
1254
- headers = body === void 0 ? {} : onlyContentType(headers);
1356
+ headers = { ...body === void 0 ? {} : onlyContentType(headers), ...portable };
1357
+ } else if (request.restoreOnOrigin) {
1358
+ headers = own;
1255
1359
  }
1256
1360
  current = next.href;
1257
1361
  }
@@ -1858,20 +1962,25 @@ var OpenApiSpecPoller = class extends import_events.EventEmitter {
1858
1962
  async doFetch(runSignal) {
1859
1963
  if (runSignal.aborted) return;
1860
1964
  const headers = { ...this.config.headers };
1965
+ const validators = {};
1861
1966
  if (this.config.changeDetection !== "content-hash") {
1862
1967
  if (this.lastEtag) {
1863
- headers["If-None-Match"] = this.lastEtag;
1968
+ validators["If-None-Match"] = this.lastEtag;
1864
1969
  }
1865
1970
  if (this.lastModified) {
1866
- headers["If-Modified-Since"] = this.lastModified;
1971
+ validators["If-Modified-Since"] = this.lastModified;
1867
1972
  }
1868
1973
  }
1869
1974
  const controller = new AbortController();
1870
1975
  this._fetchController = controller;
1871
1976
  const timeout = setTimeout(() => controller.abort(), this.config.fetchTimeoutMs);
1872
1977
  try {
1873
- const response = await fetch(this.config.url, {
1978
+ const response = await fetchOriginBound(this.config.url, {
1979
+ method: "GET",
1874
1980
  headers,
1981
+ portableHeaders: validators,
1982
+ // A redirect that comes back to the specification's origin gets its headers again
1983
+ restoreOnOrigin: true,
1875
1984
  signal: controller.signal
1876
1985
  });
1877
1986
  if (runSignal.aborted) return;
@@ -2350,8 +2459,8 @@ var Broker = class {
2350
2459
  /**
2351
2460
  * Terminate a session
2352
2461
  */
2353
- async terminateSession(sessionId, reason) {
2354
- return this.sessionManager.terminate(sessionId, reason);
2462
+ async terminateSession(sessionId, reason, expectedSession) {
2463
+ return this.sessionManager.terminate(sessionId, reason, expectedSession);
2355
2464
  }
2356
2465
  // ============================================================================
2357
2466
  // Quick Execution
@@ -3077,7 +3186,8 @@ function parseFilterParameter(value) {
3077
3186
  }
3078
3187
 
3079
3188
  // libs/broker/src/http/session-handler.ts
3080
- var CORS_REQUEST_HEADERS = ["Content-Type", "Accept", "Last-Event-ID", "Authorization"];
3189
+ var CORS_REQUEST_HEADERS = ["Content-Type", "Accept", "Last-Event-ID", "Authorization", "X-Session-Instance-ID"];
3190
+ var SESSION_INSTANCE_ID = /^[A-Za-z0-9._:~-]{1,128}$/;
3081
3191
  var HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
3082
3192
  function corsRequestHeaders(configured) {
3083
3193
  if (configured !== void 0 && !Array.isArray(configured)) {
@@ -3222,6 +3332,17 @@ var SessionHandler = class {
3222
3332
  }
3223
3333
  return session;
3224
3334
  }
3335
+ /** Optional identifier for one creation, sent again on a conditional stop. */
3336
+ instanceIdOf(req, res) {
3337
+ const values = Object.entries(req.headers).filter(([name]) => name.toLowerCase() === "x-session-instance-id");
3338
+ const value = values[0]?.[1];
3339
+ if (values.length === 0) return {};
3340
+ if (values.length !== 1 || typeof value !== "string" || !SESSION_INSTANCE_ID.test(value)) {
3341
+ this.sendError(res, 400, "INVALID_REQUEST", "Invalid X-Session-Instance-ID header");
3342
+ return void 0;
3343
+ }
3344
+ return { value };
3345
+ }
3225
3346
  /**
3226
3347
  * List sessions: all of them, or the caller's own when callers are identified
3227
3348
  *
@@ -3258,6 +3379,8 @@ var SessionHandler = class {
3258
3379
  this.addCorsHeaders(req, res);
3259
3380
  const identity = await this.callerOf(req, res);
3260
3381
  if (!identity) return;
3382
+ const instanceId = this.instanceIdOf(req, res);
3383
+ if (!instanceId) return;
3261
3384
  const parsed = parseCreateSessionRequest(req.body);
3262
3385
  if (!parsed.ok) {
3263
3386
  this.sendError(res, 400, parsed.code, parsed.message);
@@ -3275,7 +3398,7 @@ var SessionHandler = class {
3275
3398
  if (filter) {
3276
3399
  eventFilter = this.requestFilter(filter);
3277
3400
  }
3278
- session = this.broker.createSession({ sessionId, limits, owner: identity.caller });
3401
+ session = this.broker.createSession({ sessionId, limits, owner: identity.caller, instanceId: instanceId.value });
3279
3402
  } catch (error) {
3280
3403
  this.sendCreateFailure(res, error);
3281
3404
  return;
@@ -3316,7 +3439,7 @@ var SessionHandler = class {
3316
3439
  }
3317
3440
  } catch {
3318
3441
  cleanup();
3319
- await this.broker.terminateSession(session.sessionId).catch(() => false);
3442
+ await this.broker.terminateSession(session.sessionId, void 0, session).catch(() => false);
3320
3443
  this.sendError(res, 500, "SESSION_CREATE_ERROR", "Failed to create session");
3321
3444
  return;
3322
3445
  }
@@ -3462,8 +3585,18 @@ var SessionHandler = class {
3462
3585
  this.addCorsHeaders(req, res);
3463
3586
  const session = await this.sessionOf(req, res);
3464
3587
  if (!session) return;
3588
+ const instanceId = this.instanceIdOf(req, res);
3589
+ if (!instanceId) return;
3590
+ if (instanceId.value !== void 0 && session.instanceId === void 0) {
3591
+ this.sendError(res, 409, "SESSION_INSTANCE_UNAVAILABLE", "Session has no creation identifier");
3592
+ return;
3593
+ }
3594
+ if (session.instanceId !== instanceId.value) {
3595
+ this.sendError(res, 412, "SESSION_INSTANCE_MISMATCH", "Session was replaced");
3596
+ return;
3597
+ }
3465
3598
  const sessionId = session.sessionId;
3466
- const success = await this.broker.terminateSession(sessionId);
3599
+ const success = await this.broker.terminateSession(sessionId, void 0, session);
3467
3600
  if (!success) {
3468
3601
  this.sendError(res, 404, "NOT_FOUND", "Session not found");
3469
3602
  return;
@@ -30,6 +30,17 @@ export declare function readBodyText(response: Response, maxBytes: number, trunc
30
30
  export interface OriginBoundRequest {
31
31
  method: string;
32
32
  headers: Record<string, string>;
33
+ /**
34
+ * Headers that are no credential and follow a redirect to any origin, such as the validators of
35
+ * a conditional request (`If-None-Match`, `If-Modified-Since`). Sent with every request.
36
+ */
37
+ portableHeaders?: Record<string, string>;
38
+ /**
39
+ * Send the configured headers again when a redirect comes back to the request's own origin
40
+ * after another (`A → B → A`). Off by default: what B chooses on A is then sent with A's
41
+ * headers.
42
+ */
43
+ restoreOnOrigin?: boolean;
33
44
  body?: BodyInit;
34
45
  signal?: AbortSignal;
35
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enclave-vm/broker",
3
- "version": "2.19.2",
3
+ "version": "2.20.0-rc.1",
4
4
  "description": "Tool broker and session management for the EnclaveJS streaming runtime",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://github.com/agentfront/enclave",
@@ -34,9 +34,9 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@enclave-vm/core": "2.19.2",
38
- "@enclave-vm/stream": "2.19.2",
39
- "@enclave-vm/types": "2.19.2",
37
+ "@enclave-vm/core": "2.20.0-rc.1",
38
+ "@enclave-vm/stream": "2.20.0-rc.1",
39
+ "@enclave-vm/types": "2.20.0-rc.1",
40
40
  "minimatch": "^11.0.1",
41
41
  "zod": "^4.6.6"
42
42
  }
@@ -43,6 +43,7 @@ export declare class BoundedSessionEmitter extends SessionEmitter {
43
43
  private kept;
44
44
  private keptBytes;
45
45
  private droppedThrough;
46
+ private ended;
46
47
  /**
47
48
  * Size of the event that is emitted next, when the caller knows it already. Used once.
48
49
  */
@@ -95,7 +95,7 @@ export declare class SessionManager {
95
95
  /**
96
96
  * Terminate a session
97
97
  */
98
- terminate(sessionId: SessionId, reason?: string): Promise<boolean>;
98
+ terminate(sessionId: SessionId, reason?: string, expectedSession?: BrokerSession): Promise<boolean>;
99
99
  /**
100
100
  * Execute code in a new session and wait for completion
101
101
  *