poe-code 8.0.1 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cli/commands/harness.js +12 -3
  2. package/dist/cli/commands/harness.js.map +1 -1
  3. package/dist/index.js +1501 -711
  4. package/dist/index.js.map +4 -4
  5. package/dist/metafile.json +1 -1
  6. package/package.json +1 -1
  7. package/packages/safejs/dist/chunks/chunk-4YIDX44G.js +40 -0
  8. package/packages/safejs/dist/chunks/chunk-4YIDX44G.js.map +7 -0
  9. package/packages/safejs/dist/chunks/chunk-55ZK3CR7.js +7802 -0
  10. package/packages/safejs/dist/chunks/chunk-55ZK3CR7.js.map +7 -0
  11. package/packages/safejs/dist/chunks/{chunk-ME5BTAZT.js → chunk-GQBEEFVQ.js} +150 -19
  12. package/packages/safejs/dist/chunks/{chunk-ME5BTAZT.js.map → chunk-GQBEEFVQ.js.map} +4 -4
  13. package/packages/safejs/dist/chunks/chunk-NI4WFFTL.js +5701 -0
  14. package/packages/safejs/dist/chunks/chunk-NI4WFFTL.js.map +7 -0
  15. package/packages/safejs/dist/chunks/inMemory-RLZ4QGDZ.js +48 -0
  16. package/packages/safejs/dist/chunks/inMemory-RLZ4QGDZ.js.map +7 -0
  17. package/packages/safejs/dist/chunks/server-3SNT3RE2.js +12518 -0
  18. package/packages/safejs/dist/chunks/server-3SNT3RE2.js.map +7 -0
  19. package/packages/safejs/dist/chunks/types-BTF37PZ6.js +348 -0
  20. package/packages/safejs/dist/chunks/types-BTF37PZ6.js.map +7 -0
  21. package/packages/safejs/dist/cli.d.ts +2 -0
  22. package/packages/safejs/dist/cli.js +26 -2
  23. package/packages/safejs/dist/cli.js.map +2 -2
  24. package/packages/safejs/dist/core.js +2 -1
  25. package/packages/safejs/dist/index.d.ts +3 -0
  26. package/packages/safejs/dist/index.js +6 -287
  27. package/packages/safejs/dist/index.js.map +4 -4
  28. package/packages/safejs/dist/interp/host-bridge.d.ts +2 -0
  29. package/packages/safejs/dist/interp/host-bridge.js +20 -9
  30. package/packages/safejs/dist/interp/host-call.d.ts +5 -0
  31. package/packages/safejs/dist/interp/host-call.js +48 -3
  32. package/packages/safejs/dist/interp/resources.d.ts +7 -0
  33. package/packages/safejs/dist/interp/resources.js +36 -0
  34. package/packages/safejs/dist/modules/mcp-transport.d.ts +26 -0
  35. package/packages/safejs/dist/modules/mcp-transport.js +259 -0
  36. package/packages/safejs/dist/modules/mcp.d.ts +15 -1
  37. package/packages/safejs/dist/modules/mcp.js +107 -0
  38. package/packages/safejs/dist/run.js +6 -3
  39. package/packages/safejs/dist/snapshot/dump-format.d.ts +1 -1
  40. package/packages/safejs/dist/snapshot/dump-format.js +1 -1
  41. package/packages/safejs/dist/snapshot/replay-data.d.ts +1 -0
  42. package/packages/safejs/dist/snapshot/replay-data.js +3 -1
  43. package/packages/safejs/dist/snapshot/replay-inputs.d.ts +2 -2
  44. package/packages/safejs/dist/snapshot/replay-inputs.js +2 -1
  45. package/packages/safejs/dist/chunks/chunk-AVDRLE3Y.js +0 -3461
  46. package/packages/safejs/dist/chunks/chunk-AVDRLE3Y.js.map +0 -7
@@ -16919,7 +16919,7 @@ function serializeArguments(value, serializeValue) {
16919
16919
 
16920
16920
  // packages/safejs/src/snapshot/dump-format.ts
16921
16921
  var DUMP_FORMAT_VERSION = 1;
16922
- var EXECUTION_SEMANTICS = "jobs-v4";
16922
+ var EXECUTION_SEMANTICS = "jobs-v5";
16923
16923
  var SKIP_VALUE = /* @__PURE__ */ Symbol("SafeJS.skip-dump-value");
16924
16924
  function serializeSafeJSSnapshot(snapshot) {
16925
16925
  const replayError = Object.getOwnPropertyDescriptor(snapshot, "replayError");
@@ -20360,7 +20360,7 @@ function decodeReplayData(input, options = {}) {
20360
20360
  restored.set(id, capability);
20361
20361
  return capability;
20362
20362
  }
20363
- return createSandboxClosure({
20363
+ const copy = createSandboxClosure({
20364
20364
  ...capability,
20365
20365
  boundTarget: capability.boundTarget,
20366
20366
  cancellationSignal: capability.cancellationSignal,
@@ -20374,6 +20374,8 @@ function decodeReplayData(input, options = {}) {
20374
20374
  return properties;
20375
20375
  }
20376
20376
  });
20377
+ options.onCapabilityRestored?.(capability, copy);
20378
+ return copy;
20377
20379
  }
20378
20380
  if (kind === "map") {
20379
20381
  const result2 = createSandboxMap();
@@ -20492,7 +20494,12 @@ var HostCallJournal = class {
20492
20494
  this.budget = budget;
20493
20495
  this.recordedReplay = replay !== void 0;
20494
20496
  if (replay !== void 0) {
20495
- const replayRecords = restoreReplayCalls(replay, this.encodedOutcomes, this.callbackSizes);
20497
+ const replayRecords = restoreReplayCalls(
20498
+ replay,
20499
+ this.encodedOutcomes,
20500
+ this.callbackSizes,
20501
+ this.requiredHostCapabilities
20502
+ );
20496
20503
  const restoredRunId = replayRecords[0]?.runId ?? records[0]?.runId ?? randomUUID();
20497
20504
  validateRestoredRecords(records, restoredRunId, sourceHash);
20498
20505
  for (const record2 of records) {
@@ -20544,8 +20551,10 @@ var HostCallJournal = class {
20544
20551
  retainedSize = 0;
20545
20552
  outcomeSizes = /* @__PURE__ */ new Map();
20546
20553
  capabilities = /* @__PURE__ */ new Map();
20554
+ requiredHostCapabilities = /* @__PURE__ */ new Set();
20547
20555
  capabilityIds = /* @__PURE__ */ new WeakMap();
20548
20556
  nativeClosures = /* @__PURE__ */ new WeakMap();
20557
+ hostSources = /* @__PURE__ */ new WeakMap();
20549
20558
  encodedOutcomes = /* @__PURE__ */ new Map();
20550
20559
  callbackSizes = /* @__PURE__ */ new Map();
20551
20560
  completedCallbackOwners = /* @__PURE__ */ new Set();
@@ -20698,6 +20707,30 @@ var HostCallJournal = class {
20698
20707
  if (!this.capabilityIds.has(closure)) this.capabilityIds.set(closure, identity);
20699
20708
  this.nativeClosures.set(native, closure);
20700
20709
  }
20710
+ registerHostCapability(path, closure, native) {
20711
+ const identity = `host:${path}`;
20712
+ const existing = this.capabilities.get(identity);
20713
+ if (existing !== void 0 && existing !== closure)
20714
+ throw new TypeError(`Conflicting replay capability '${identity}'.`);
20715
+ this.capabilities.set(identity, closure);
20716
+ if (!this.capabilityIds.has(closure)) this.capabilityIds.set(closure, identity);
20717
+ if (!this.nativeClosures.has(native)) this.nativeClosures.set(native, closure);
20718
+ this.hostSources.set(closure, native);
20719
+ }
20720
+ rebindHostCapability(original, restored) {
20721
+ const identity = this.capabilityIds.get(original);
20722
+ const source = this.hostSources.get(original);
20723
+ if (identity === void 0 || source === void 0) return;
20724
+ this.capabilities.set(identity, restored);
20725
+ this.capabilityIds.set(restored, identity);
20726
+ this.nativeClosures.set(source, restored);
20727
+ this.hostSources.set(restored, source);
20728
+ }
20729
+ validateHostCapabilities() {
20730
+ for (const identity of this.requiredHostCapabilities) {
20731
+ if (!this.capabilities.has(identity)) throw new UnresolvedReplayCapabilityError(identity);
20732
+ }
20733
+ }
20701
20734
  waitForCapability(id) {
20702
20735
  if (this.capabilities.has(id)) return Promise.resolve();
20703
20736
  const owner = id.slice(0, id.lastIndexOf("/function/"));
@@ -20808,7 +20841,7 @@ var HostCallJournal = class {
20808
20841
  function copyOutcome(outcome) {
20809
20842
  return outcome.status === "fulfilled" ? { status: "fulfilled", value: deepCopyToSandbox(outcome.value) } : { status: "rejected", reason: deepCopyToSandbox(outcome.reason) };
20810
20843
  }
20811
- function restoreReplayCalls(input, encodedOutcomes, callbackSizes) {
20844
+ function restoreReplayCalls(input, encodedOutcomes, callbackSizes, requiredHostCapabilities = /* @__PURE__ */ new Set()) {
20812
20845
  validateSnapshotData(input);
20813
20846
  if (input === null || typeof input !== "object" || !("version" in input) || input.version !== 1 || !("calls" in input) || !Array.isArray(input.calls)) {
20814
20847
  throw new TypeError("Invalid host call replay header.");
@@ -20829,7 +20862,23 @@ function restoreReplayCalls(input, encodedOutcomes, callbackSizes) {
20829
20862
  );
20830
20863
  }
20831
20864
  }
20832
- const resolveCapability = (id) => capabilities.get(id);
20865
+ const resolveCapability = (id) => {
20866
+ if (!capabilities.has(id) && id.startsWith("host:")) {
20867
+ const path = JSON.parse(id.slice(5));
20868
+ if (!Array.isArray(path) || path.length < 2 || path.some((value) => typeof value !== "string"))
20869
+ throw new TypeError("Invalid host capability identity.");
20870
+ requiredHostCapabilities.add(id);
20871
+ capabilities.set(
20872
+ id,
20873
+ createSandboxClosure({
20874
+ call: () => {
20875
+ throw new TypeError("Host capability has not been reconstructed.");
20876
+ }
20877
+ })
20878
+ );
20879
+ }
20880
+ return capabilities.get(id);
20881
+ };
20833
20882
  return input.calls.map((entry, index) => {
20834
20883
  if (entry === null || typeof entry !== "object" || Array.isArray(entry))
20835
20884
  throw new TypeError("Invalid replay call.");
@@ -21785,18 +21834,25 @@ function readHostOperationPolicy(operation) {
21785
21834
  return hostOperationPolicies.get(operation);
21786
21835
  }
21787
21836
  function wrapCallerInjectedBindings(bindings, options) {
21837
+ options = { ...options, registerCapabilities: true };
21788
21838
  const state = { seen: /* @__PURE__ */ new WeakMap() };
21789
21839
  const copied = Object.fromEntries(
21790
21840
  Object.entries(bindings).map(([name, value]) => [
21791
21841
  name,
21792
- typeof value === "function" ? wrapCallerInjectedFunction(name, value, options, state) : copyHostValueToSandbox(value, [], { ...options, operation: name }, state, "<root>")
21842
+ typeof value === "function" ? wrapCallerInjectedFunction(name, value, { ...options, capabilityPath: [name] }, state) : copyHostValueToSandbox(
21843
+ value,
21844
+ [],
21845
+ { ...options, operation: name, capabilityPath: [name] },
21846
+ state,
21847
+ "<root>"
21848
+ )
21793
21849
  ])
21794
21850
  );
21795
21851
  options.budget.provisionDataUsage(measureSandboxData(Object.values(copied)));
21796
21852
  return copied;
21797
21853
  }
21798
21854
  function wrapCallerInjectedFunction(name, value, options, state) {
21799
- const existing = state.seen.get(value);
21855
+ const existing = state.seen.get(value) ?? options.hostCalls?.nativeClosures.get(value);
21800
21856
  if (existing !== void 0) return existing;
21801
21857
  const bindingName = name === "default" && value.name.length > 0 ? value.name : name;
21802
21858
  const callable = value;
@@ -21911,6 +21967,16 @@ function wrapCallerInjectedFunction(name, value, options, state) {
21911
21967
  name: bindingName,
21912
21968
  properties: (closure) => {
21913
21969
  state.seen.set(value, closure);
21970
+ if (options.registerCapabilities) {
21971
+ options.hostCalls?.registerHostCapability(
21972
+ JSON.stringify([
21973
+ options.moduleId ?? "<bindings>",
21974
+ ...options.capabilityPath ?? [bindingName]
21975
+ ]),
21976
+ closure,
21977
+ value
21978
+ );
21979
+ }
21914
21980
  return copyFunctionProperties(callable, [], options, state, options.operation ?? bindingName) ?? {};
21915
21981
  }
21916
21982
  });
@@ -22081,7 +22147,7 @@ function copyHostResultToSandbox(result, stackFrames, options) {
22081
22147
  const value = copyHostValueToSandbox(
22082
22148
  result,
22083
22149
  stackFrames,
22084
- options,
22150
+ { ...options, registerCapabilities: false },
22085
22151
  {
22086
22152
  seen: /* @__PURE__ */ new WeakMap()
22087
22153
  },
@@ -22381,7 +22447,7 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22381
22447
  copy[index] = copyHostValueToSandbox(
22382
22448
  descriptor.value,
22383
22449
  stackFrames,
22384
- options,
22450
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], String(index)] },
22385
22451
  state,
22386
22452
  `${path}[${index}]`
22387
22453
  );
@@ -22397,9 +22463,22 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22397
22463
  state.seen.set(value, copy);
22398
22464
  budget.allocateCollectionEntries(value.size);
22399
22465
  for (const [key, entry] of value) {
22466
+ const ordinal = copy.entries.size;
22400
22467
  copy.entries.set(
22401
- copyHostValueToSandbox(key, stackFrames, options, state, `${path}.<key>`),
22402
- copyHostValueToSandbox(entry, stackFrames, options, state, `${path}.<value>`)
22468
+ copyHostValueToSandbox(
22469
+ key,
22470
+ stackFrames,
22471
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], `key:${ordinal}`] },
22472
+ state,
22473
+ `${path}.<key>`
22474
+ ),
22475
+ copyHostValueToSandbox(
22476
+ entry,
22477
+ stackFrames,
22478
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], `value:${ordinal}`] },
22479
+ state,
22480
+ `${path}.<value>`
22481
+ )
22403
22482
  );
22404
22483
  }
22405
22484
  return copy;
@@ -22414,7 +22493,16 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22414
22493
  budget.allocateCollectionEntries(value.size);
22415
22494
  for (const entry of value) {
22416
22495
  copy.values.add(
22417
- copyHostValueToSandbox(entry, stackFrames, options, state, `${path}.<value>`)
22496
+ copyHostValueToSandbox(
22497
+ entry,
22498
+ stackFrames,
22499
+ {
22500
+ ...options,
22501
+ capabilityPath: [...options.capabilityPath ?? [], String(copy.values.size)]
22502
+ },
22503
+ state,
22504
+ `${path}.<value>`
22505
+ )
22418
22506
  );
22419
22507
  }
22420
22508
  return copy;
@@ -22442,7 +22530,7 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22442
22530
  value: copyHostValueToSandbox(
22443
22531
  descriptor.value,
22444
22532
  stackFrames,
22445
- options,
22533
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], key] },
22446
22534
  state,
22447
22535
  joinPath2(path, key)
22448
22536
  )
@@ -22515,7 +22603,7 @@ function copyFunctionProperties(callable, stackFrames, options, state, path) {
22515
22603
  properties[key] = copyHostValueToSandbox(
22516
22604
  descriptor.value,
22517
22605
  stackFrames,
22518
- options,
22606
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], key] },
22519
22607
  state,
22520
22608
  joinPath2(path, key)
22521
22609
  );
@@ -22895,6 +22983,41 @@ function isLockedFileError(error) {
22895
22983
  return code !== void 0 && LOCKED_FILE_ERROR_CODES.has(code);
22896
22984
  }
22897
22985
 
22986
+ // packages/safejs/src/interp/resources.ts
22987
+ import { AsyncLocalStorage as AsyncLocalStorage5 } from "node:async_hooks";
22988
+ var runResources = new AsyncLocalStorage5();
22989
+ async function withRunResources(signal, execute) {
22990
+ const controller = new AbortController();
22991
+ const cancel = () => controller.abort(signal?.reason);
22992
+ const cleanups = /* @__PURE__ */ new Set();
22993
+ const resources = {
22994
+ signal: controller.signal,
22995
+ add(close) {
22996
+ cleanups.add(close);
22997
+ }
22998
+ };
22999
+ signal?.addEventListener("abort", cancel, { once: true });
23000
+ if (signal?.aborted) cancel();
23001
+ let result;
23002
+ let failure;
23003
+ let errors = [];
23004
+ try {
23005
+ result = await runResources.run(resources, execute);
23006
+ } catch (error) {
23007
+ failure = { reason: error };
23008
+ } finally {
23009
+ signal?.removeEventListener("abort", cancel);
23010
+ controller.abort(new Error("SafeJS run finished."));
23011
+ const outcomes = await Promise.allSettled(
23012
+ [...cleanups].map((close) => Promise.resolve().then(close))
23013
+ );
23014
+ errors = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
23015
+ }
23016
+ if (failure !== void 0) throw failure.reason;
23017
+ if (errors.length > 0) throw new AggregateError(errors, "SafeJS resource cleanup failed.");
23018
+ return result;
23019
+ }
23020
+
22898
23021
  // packages/safejs/src/interp/globals/console-json.ts
22899
23022
  function createConsoleJsonGlobals(options) {
22900
23023
  const sink = options.sink ?? console;
@@ -28068,7 +28191,7 @@ var UnsnapshotableValueError = class extends Error {
28068
28191
 
28069
28192
  // packages/safejs/src/snapshot/replay-inputs.ts
28070
28193
  var validationPromise = createSandboxPromise(Promise.resolve(void 0));
28071
- function prepareReplayInputs(current, saved, preparePromise) {
28194
+ function prepareReplayInputs(current, saved, preparePromise, onCapabilityRestored) {
28072
28195
  const identities = /* @__PURE__ */ new WeakMap();
28073
28196
  const capabilities = /* @__PURE__ */ new Map();
28074
28197
  const promises = /* @__PURE__ */ new Map();
@@ -28139,6 +28262,7 @@ function prepareReplayInputs(current, saved, preparePromise) {
28139
28262
  assertReplayInputShape(validated);
28140
28263
  const restored = decodeReplayData(saved, {
28141
28264
  resolveCapability,
28265
+ onCapabilityRestored,
28142
28266
  resolvePromise: (id) => {
28143
28267
  const value = readCapability(id);
28144
28268
  if (!promises.has(id) && preparePromise !== void 0)
@@ -28176,7 +28300,7 @@ function run(source, options = {}) {
28176
28300
  const lifecycle = { hostCallbackDepth: 0 };
28177
28301
  const dumpController = createDumpController(lifecycle);
28178
28302
  const promiseTracker = createSandboxPromiseRejectionTracker();
28179
- const result = withSandboxPromiseRejectionTracker(promiseTracker, async () => {
28303
+ const execute = async () => {
28180
28304
  const promiseReplay = new PromiseReplay(options.snapshot?.promiseReplay);
28181
28305
  return promiseReplayContext.run(promiseReplay, async () => {
28182
28306
  const deactivateOtelSink = activateOtelSink(options.otelSink);
@@ -28276,8 +28400,10 @@ function run(source, options = {}) {
28276
28400
  if (!isSandboxClosure(binding))
28277
28401
  throw new TypeError("Invalid initial promise operation.");
28278
28402
  return binding.call([]);
28279
- }
28403
+ },
28404
+ hostCalls.rebindHostCapability.bind(hostCalls)
28280
28405
  );
28406
+ hostCalls.validateHostCapabilities();
28281
28407
  budget.setRetainedDataUsage(
28282
28408
  initialInputs,
28283
28409
  measureSandboxData(
@@ -28456,7 +28582,11 @@ function run(source, options = {}) {
28456
28582
  deactivateOtelSink();
28457
28583
  }
28458
28584
  });
28459
- });
28585
+ };
28586
+ const result = withRunResources(
28587
+ options.signal,
28588
+ () => withSandboxPromiseRejectionTracker(promiseTracker, execute)
28589
+ );
28460
28590
  return attachDumpController(result, dumpController);
28461
28591
  }
28462
28592
  async function throwIfReturnedPromiseRejected(result) {
@@ -28636,6 +28766,7 @@ export {
28636
28766
  parse,
28637
28767
  parseModule,
28638
28768
  lint,
28769
+ runResources,
28639
28770
  noopOtelSink,
28640
28771
  bindOtelSpan,
28641
28772
  activateOtelSpan,
@@ -28664,4 +28795,4 @@ export {
28664
28795
  FileSnapshotBackend,
28665
28796
  run
28666
28797
  };
28667
- //# sourceMappingURL=chunk-ME5BTAZT.js.map
28798
+ //# sourceMappingURL=chunk-GQBEEFVQ.js.map