@opengeni/runtime 2.4.2-canary.0 → 2.4.2-canary.2

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.
@@ -14376,9 +14376,10 @@ function notifyDurableOpOwnershipTransferred(opId) {
14376
14376
  // src/sandbox/provider-command-session.ts
14377
14377
  import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
14378
14378
  var admission = new AsyncLocalStorage2();
14379
+ var MAX_PROVIDER_COMMAND_HANDLE = 2147483647;
14379
14380
  function withProviderCommandHandle(handle, fn) {
14380
14381
  if (handle === void 0) return fn();
14381
- if (!Number.isSafeInteger(handle) || handle <= 0 || handle > 2147483647)
14382
+ if (!Number.isSafeInteger(handle) || handle <= 0 || handle > MAX_PROVIDER_COMMAND_HANDLE)
14382
14383
  throw new Error("Invalid admitted provider command handle");
14383
14384
  return admission.run(handle, fn);
14384
14385
  }
@@ -20393,10 +20394,26 @@ function sameExecution(a, b) {
20393
20394
  function installModalCommandSession(session, control) {
20394
20395
  markTypedExecHandleLoss(session);
20395
20396
  const originalExec = session.execCommand?.bind(session);
20397
+ const originalWrite = session.writeStdin?.bind(session);
20396
20398
  const cancelLegacyStart = session.cancelPendingExecCommand?.bind(session);
20397
20399
  const pendingStarts = /* @__PURE__ */ new Set();
20398
20400
  const entries = /* @__PURE__ */ new Map();
20401
+ const setupHandles = /* @__PURE__ */ new Map();
20402
+ let nextSetupHandle = MAX_PROVIDER_COMMAND_HANDLE + 1;
20399
20403
  const receipts = /* @__PURE__ */ new Map();
20404
+ const setupPage = (raw, handle, sdkHandle) => {
20405
+ const banner = parseExecResponseBanner(raw);
20406
+ if (banner.kind === "exited") {
20407
+ setupHandles.delete(handle);
20408
+ return raw;
20409
+ }
20410
+ if (banner.kind !== "running" || banner.sessionId !== sdkHandle)
20411
+ throw new ModalProcessObservationUnavailableError(handle);
20412
+ return raw.replace(
20413
+ /^Process running with session ID \d+(?=\r?$)/mu,
20414
+ `Process running with session ID ${handle}`
20415
+ );
20416
+ };
20400
20417
  const formatPage = (handle, page, maxOutputTokens) => {
20401
20418
  const result = [
20402
20419
  `Provider output receipt: ${randomUUID5()}`,
@@ -20425,7 +20442,13 @@ function installModalCommandSession(session, control) {
20425
20442
  const handle = admittedProviderCommandHandle();
20426
20443
  if (handle === void 0) {
20427
20444
  if (!originalExec) throw new Error("Modal command requires mutation admission");
20428
- return originalExec(args);
20445
+ if (!Number.isSafeInteger(nextSetupHandle)) throw new Error("Modal setup handles exhausted");
20446
+ const setupHandle = nextSetupHandle++;
20447
+ const raw = await originalExec(args);
20448
+ const banner = parseExecResponseBanner(raw);
20449
+ if (banner.kind !== "running") return raw;
20450
+ setupHandles.set(setupHandle, banner.sessionId);
20451
+ return setupPage(raw, setupHandle, banner.sessionId);
20429
20452
  }
20430
20453
  if (entries.has(handle)) throw new Error("Modal command handle is already bound");
20431
20454
  const cancellation = new AbortController();
@@ -20442,7 +20465,11 @@ function installModalCommandSession(session, control) {
20442
20465
  cancellation.signal
20443
20466
  );
20444
20467
  } catch {
20445
- return formatPage(handle, { command: entry.command, chunks: [], exitCode: null });
20468
+ return formatPage(handle, {
20469
+ command: entry.command,
20470
+ chunks: [],
20471
+ exitCode: null
20472
+ });
20446
20473
  }
20447
20474
  } finally {
20448
20475
  pendingStarts.delete(cancellation);
@@ -20460,7 +20487,7 @@ function installModalCommandSession(session, control) {
20460
20487
  return entry ? structuredClone(entry.command) : null;
20461
20488
  };
20462
20489
  session.bindProviderCommand = (handle, command, persistence) => {
20463
- if (!Number.isSafeInteger(handle) || handle <= 0)
20490
+ if (!Number.isSafeInteger(handle) || handle <= 0 || handle > MAX_PROVIDER_COMMAND_HANDLE)
20464
20491
  throw new Error("Invalid retained Modal command handle");
20465
20492
  const existing = entries.get(handle);
20466
20493
  if (existing && !sameExecution(existing.command, command))
@@ -20481,6 +20508,11 @@ function installModalCommandSession(session, control) {
20481
20508
  receipts.delete(result);
20482
20509
  };
20483
20510
  session.writeStdin = async (args) => {
20511
+ const sdkHandle = setupHandles.get(args.sessionId);
20512
+ if (sdkHandle !== void 0 && originalWrite) {
20513
+ const raw = await originalWrite({ ...args, sessionId: sdkHandle });
20514
+ return setupPage(raw, args.sessionId, sdkHandle);
20515
+ }
20484
20516
  const entry = entries.get(args.sessionId);
20485
20517
  if (!entry?.persistence)
20486
20518
  throw new ModalProcessObservationUnavailableError(args.sessionId, {
@@ -20556,9 +20588,9 @@ function decodePage(prior, input, terminal) {
20556
20588
  };
20557
20589
  }
20558
20590
  var ModalCommandControl = class _ModalCommandControl {
20559
- constructor(client, sandboxId, root, environment = {}) {
20591
+ constructor(client, sandboxIdentity, root, environment = {}) {
20560
20592
  this.client = client;
20561
- this.sandboxId = sandboxId;
20593
+ this.sandboxIdentity = sandboxIdentity;
20562
20594
  this.root = root;
20563
20595
  this.environment = environment;
20564
20596
  }
@@ -20567,16 +20599,19 @@ var ModalCommandControl = class _ModalCommandControl {
20567
20599
  throw new Error("Modal command control requires the verified 0.9.0 SDK contract");
20568
20600
  return new _ModalCommandControl(commandControlPlane(client), sandboxId, root, environment);
20569
20601
  }
20602
+ get sandboxId() {
20603
+ return typeof this.sandboxIdentity === "function" ? this.sandboxIdentity() : this.sandboxIdentity;
20604
+ }
20570
20605
  async start(args, signal) {
20571
20606
  signal?.throwIfAborted();
20572
20607
  const workdir = posix2.resolve(this.root, args.workdir ?? this.root);
20573
20608
  if (workdir !== this.root && !workdir.startsWith(`${this.root.replace(/\/$/u, "")}/`))
20574
20609
  throw new Error("Command workdir is outside the sandbox workspace");
20575
- const task = await this.client.sandboxGetTaskId(
20576
- { sandboxId: this.sandboxId },
20577
- signal ? { signal } : void 0
20578
- );
20610
+ const sandboxId = this.sandboxId;
20611
+ const task = await this.client.sandboxGetTaskId({ sandboxId }, signal ? { signal } : void 0);
20579
20612
  if (!task.taskId || task.taskResult) throw new Error("Modal command task is unavailable");
20613
+ if (sandboxId !== this.sandboxId)
20614
+ throw new Error("Modal sandbox changed during command preparation");
20580
20615
  const login = args.shell ? args.login ?? true : false;
20581
20616
  let command = [args.shell ?? "/bin/sh", login ? "-lc" : "-c", args.cmd];
20582
20617
  if (args.runAs) {
@@ -20623,7 +20658,7 @@ var ModalCommandControl = class _ModalCommandControl {
20623
20658
  if (!result.execId) throw new Error("Modal command start returned no execution identity");
20624
20659
  return {
20625
20660
  kind: "modal-control-v1",
20626
- sandboxId: this.sandboxId,
20661
+ sandboxId,
20627
20662
  taskId: task.taskId,
20628
20663
  execId: result.execId,
20629
20664
  ...args.tty ? { pty: true } : {},
@@ -21038,7 +21073,11 @@ function installOpenGeniModalSnapshotPolicy(session) {
21038
21073
  profile: mutable.modal.profile,
21039
21074
  ...mutable.modal.logger ? { logger: mutable.modal.logger } : {}
21040
21075
  },
21041
- mutable.state.sandboxId,
21076
+ () => {
21077
+ const sandboxId = mutable.state?.sandboxId;
21078
+ if (!sandboxId) throw new Error("Modal session sandbox identity is unavailable");
21079
+ return sandboxId;
21080
+ },
21042
21081
  mutable.state.manifest.root,
21043
21082
  () => mutable.state?.environment ?? {}
21044
21083
  )
@@ -36083,4 +36122,4 @@ export {
36083
36122
  collectSandboxEnvironment2 as collectSandboxEnvironment,
36084
36123
  parseExposedPorts2 as parseExposedPorts
36085
36124
  };
36086
- //# sourceMappingURL=chunk-72SCUP4B.js.map
36125
+ //# sourceMappingURL=chunk-WATBACLT.js.map