@deepagents/context 7.10.1 → 7.11.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.
package/dist/index.js CHANGED
@@ -7594,6 +7594,7 @@ async function deleteFileOverExec(container, path6, signal) {
7594
7594
  }
7595
7595
  return true;
7596
7596
  }
7597
+ var CONTROL_DIRECTORIES = ["/dev/shm", "/tmp"];
7597
7598
  async function collectOutput(process2) {
7598
7599
  const [stdout, stderr, exitCode] = await Promise.all([
7599
7600
  text2(process2.stdout),
@@ -7602,11 +7603,14 @@ async function collectOutput(process2) {
7602
7603
  ]);
7603
7604
  return { stdout, stderr, exitCode };
7604
7605
  }
7605
- function managedCommand(command, controlPath, commandTimeout) {
7606
+ function managedCommand(command, controlFile, commandTimeout) {
7606
7607
  return [
7607
7608
  "set -m",
7608
- `control=${shellQuote(controlPath)}`,
7609
- ': > "$control" || exit 1',
7609
+ "control=",
7610
+ `for dir in ${CONTROL_DIRECTORIES.join(" ")}; do`,
7611
+ ` if { : > "$dir/"${shellQuote(controlFile)}; } 2>/dev/null; then control="$dir/"${shellQuote(controlFile)}; break; fi`,
7612
+ "done",
7613
+ `[ -n "$control" ] || { echo ${shellQuote(`Cannot start the command: neither ${CONTROL_DIRECTORIES.join(" nor ")} is writable for the file that lets the sandbox stop it`)} >&2; exit 1; }`,
7610
7614
  "timer=",
7611
7615
  `trap 'if [ -n "$timer" ]; then kill -TERM "$timer" 2>/dev/null || :; wait "$timer" 2>/dev/null || :; fi; rm -f "$control" "$control.timeout"' EXIT`,
7612
7616
  `bash -lc ${shellQuote(command)} &`,
@@ -7630,13 +7634,16 @@ function managedCommand(command, controlPath, commandTimeout) {
7630
7634
  'exit "$status"'
7631
7635
  ].join("\n");
7632
7636
  }
7633
- async function terminate(container, controlPath, exitCode) {
7637
+ async function terminate(container, controlFile, exitCode) {
7634
7638
  const exited = exitCode.then(
7635
7639
  () => true,
7636
7640
  () => true
7637
7641
  );
7638
7642
  const killGroup = [
7639
- `IFS= read -r pid < ${shellQuote(controlPath)} || exit 0`,
7643
+ `for dir in ${CONTROL_DIRECTORIES.join(" ")}; do`,
7644
+ ` { IFS= read -r pid < "$dir/"${shellQuote(controlFile)}; } 2>/dev/null && break`,
7645
+ " pid=",
7646
+ "done",
7640
7647
  '[ -n "$pid" ] || exit 0',
7641
7648
  "printf found",
7642
7649
  'kill -KILL -- "-$pid" 2>/dev/null || exit 0',
@@ -7679,18 +7686,18 @@ function containerSession(container, errors) {
7679
7686
  async function spawn5(options) {
7680
7687
  const { command, abortSignal, stdin } = options;
7681
7688
  abortSignal?.throwIfAborted();
7682
- const controlPath = `/dev/shm/deepagents-exec-${randomUUID()}.pid`;
7689
+ const controlFile = `deepagents-exec-${randomUUID()}.pid`;
7683
7690
  const process2 = await container.spawn(
7684
7691
  [
7685
7692
  "bash",
7686
7693
  "-c",
7687
- managedCommand(command, controlPath, container.commandTimeout)
7694
+ managedCommand(command, controlFile, container.commandTimeout)
7688
7695
  ],
7689
7696
  { ...scope(options), stdin }
7690
7697
  );
7691
7698
  const control = controlProcess({
7692
7699
  exitCode: process2.exitCode,
7693
- kill: () => terminate(container, controlPath, process2.exitCode),
7700
+ kill: () => terminate(container, controlFile, process2.exitCode),
7694
7701
  abortSignal
7695
7702
  });
7696
7703
  return {
@@ -8794,6 +8801,14 @@ function createReadFileTool({
8794
8801
  });
8795
8802
  }
8796
8803
 
8804
+ // packages/context/src/lib/sandbox/types.ts
8805
+ var SandboxNotFoundError = class extends Error {
8806
+ constructor(sandbox, options) {
8807
+ super(`The sandbox "${sandbox}" no longer exists.`, options);
8808
+ this.name = "SandboxNotFoundError";
8809
+ }
8810
+ };
8811
+
8797
8812
  // packages/context/src/lib/sandbox/bash-tool.ts
8798
8813
  var DEFAULT_MAX_FILES = 1e3;
8799
8814
  var DEFAULT_MAX_OUTPUT_LENGTH = 3e4;
@@ -9522,6 +9537,10 @@ var DockerContainer = class {
9522
9537
  this.#execOnly = layout.execOnly;
9523
9538
  this.#autoRemove = autoRemove;
9524
9539
  }
9540
+ /** A removed container is a sandbox that no longer exists, whichever request finds it. */
9541
+ #notFound(error) {
9542
+ return error instanceof ContainerNotFoundError ? new SandboxNotFoundError(this.name, { cause: error }) : error;
9543
+ }
9525
9544
  #config(argv, { workingDirectory, env = {} }) {
9526
9545
  return {
9527
9546
  Cmd: argv,
@@ -9537,16 +9556,23 @@ var DockerContainer = class {
9537
9556
  );
9538
9557
  return { stdout, stderr, exitCode };
9539
9558
  } catch (error) {
9559
+ if (error instanceof ContainerNotFoundError) {
9560
+ throw this.#notFound(error);
9561
+ }
9540
9562
  if (!(error instanceof DockerError)) throw error;
9541
9563
  return { stdout: "", stderr: error.message, exitCode: 1 };
9542
9564
  }
9543
9565
  }
9544
9566
  async spawn(argv, options) {
9545
- return engineProcess2(
9546
- await this.#container.spawn(this.#config(argv, options), {
9547
- stdin: options.stdin
9548
- })
9549
- );
9567
+ try {
9568
+ return engineProcess2(
9569
+ await this.#container.spawn(this.#config(argv, options), {
9570
+ stdin: options.stdin
9571
+ })
9572
+ );
9573
+ } catch (error) {
9574
+ throw this.#notFound(error);
9575
+ }
9550
9576
  }
9551
9577
  #resolve(path6) {
9552
9578
  return posix3.resolve(this.workdir, path6);
@@ -9567,7 +9593,23 @@ var DockerContainer = class {
9567
9593
  target = posix3.resolve(posix3.dirname(target), stat.linkTarget);
9568
9594
  }
9569
9595
  }
9596
+ async #isLinkOrUnstatable(path6, signal) {
9597
+ try {
9598
+ const stat = await this.#container.stat(path6, { signal });
9599
+ return stat !== void 0 && stat.linkTarget !== "";
9600
+ } catch (error) {
9601
+ if (isUnexplainedFailure(error)) return true;
9602
+ throw this.#notFound(error);
9603
+ }
9604
+ }
9570
9605
  async readFile(path6, signal) {
9606
+ try {
9607
+ return await this.#readFile(path6, signal);
9608
+ } catch (error) {
9609
+ throw this.#notFound(error);
9610
+ }
9611
+ }
9612
+ async #readFile(path6, signal) {
9571
9613
  const target = this.#resolve(path6);
9572
9614
  if (this.#isExecOnly(target)) {
9573
9615
  return readFileOverExec(this, target, signal);
@@ -9601,7 +9643,7 @@ var DockerContainer = class {
9601
9643
  }
9602
9644
  async writeFile(path6, content, signal) {
9603
9645
  const target = this.#resolve(path6);
9604
- if (this.#isExecOnly(target)) {
9646
+ if (this.#isExecOnly(target) || await this.#isLinkOrUnstatable(target, signal)) {
9605
9647
  return writeFileOverExec(this, target, content, signal);
9606
9648
  }
9607
9649
  try {
@@ -9610,6 +9652,7 @@ var DockerContainer = class {
9610
9652
  copyUIDGID: true
9611
9653
  });
9612
9654
  } catch (error) {
9655
+ if (error instanceof ContainerNotFoundError) throw this.#notFound(error);
9613
9656
  throw new Error(`Failed to write file "${path6}": ${messageOf(error)}`, {
9614
9657
  cause: error
9615
9658
  });
@@ -9630,7 +9673,15 @@ var DockerContainer = class {
9630
9673
  try {
9631
9674
  await this.#container.stop();
9632
9675
  if (this.#autoRemove) {
9633
- await this.#container.wait({ condition: "removed" });
9676
+ const { Error: failure } = await this.#container.wait({
9677
+ condition: "removed"
9678
+ });
9679
+ if (failure?.Message) {
9680
+ throw new DockerSandboxError(
9681
+ `Failed to remove container "${this.name}": ${failure.Message}`,
9682
+ this.name
9683
+ );
9684
+ }
9634
9685
  }
9635
9686
  } catch (error) {
9636
9687
  if (error instanceof ContainerNotFoundError) return;
@@ -10991,6 +11042,7 @@ function buildSubcommandRepair(name, subcommands) {
10991
11042
  // packages/context/src/lib/sandbox/virtual-sandbox.ts
10992
11043
  import { createJustBashSandboxSessionFromNativeSandbox } from "@ai-sdk/sandbox-just-bash";
10993
11044
  import {
11045
+ InMemoryFs,
10994
11046
  Sandbox
10995
11047
  } from "just-bash";
10996
11048
  async function createVirtualSandbox({
@@ -11018,6 +11070,54 @@ async function createVirtualSandbox({
11018
11070
  }
11019
11071
  return sandbox;
11020
11072
  }
11073
+ var VirtualSandboxEnvironment = class {
11074
+ #options;
11075
+ #filesystems = /* @__PURE__ */ new Map();
11076
+ constructor(options = {}) {
11077
+ this.#options = options;
11078
+ }
11079
+ async open(chat2) {
11080
+ let fs = this.#filesystems.get(chat2.chatId);
11081
+ if (fs === void 0) {
11082
+ fs = new InMemoryFs();
11083
+ this.#filesystems.set(chat2.chatId, fs);
11084
+ }
11085
+ const sandbox = await createVirtualSandbox({ ...this.#options, fs });
11086
+ let closing;
11087
+ const close = () => closing ??= sandbox.dispose();
11088
+ const live = (operation) => async (...args) => {
11089
+ if (closing !== void 0) {
11090
+ throw new Error(
11091
+ `The sandbox handle of chat "${chat2.chatId}" is closed.`
11092
+ );
11093
+ }
11094
+ if (this.#filesystems.get(chat2.chatId) !== fs) {
11095
+ throw new SandboxNotFoundError(chat2.chatId);
11096
+ }
11097
+ return operation.apply(sandbox, args);
11098
+ };
11099
+ return Object.assign(sandbox, {
11100
+ run: live(sandbox.run),
11101
+ spawn: live(sandbox.spawn),
11102
+ readFile: live(sandbox.readFile),
11103
+ readBinaryFile: live(sandbox.readBinaryFile),
11104
+ readTextFile: live(sandbox.readTextFile),
11105
+ writeFile: live(sandbox.writeFile),
11106
+ writeBinaryFile: live(sandbox.writeBinaryFile),
11107
+ writeTextFile: live(sandbox.writeTextFile),
11108
+ stat: live(sandbox.stat),
11109
+ listDirectory: live(sandbox.listDirectory),
11110
+ deleteFile: live(sandbox.deleteFile),
11111
+ resume: live(async () => {
11112
+ }),
11113
+ close,
11114
+ [Symbol.asyncDispose]: close
11115
+ });
11116
+ }
11117
+ async delete(chat2) {
11118
+ this.#filesystems.delete(chat2.chatId);
11119
+ }
11120
+ };
11021
11121
 
11022
11122
  // packages/context/src/lib/skills/fragments.ts
11023
11123
  import dedent4 from "dedent";
@@ -14481,6 +14581,7 @@ export {
14481
14581
  PostgresStreamStore,
14482
14582
  PostgresTransaction,
14483
14583
  RuntimeStrategy,
14584
+ SandboxNotFoundError,
14484
14585
  SearchQuerySyntaxError,
14485
14586
  SqlServerContextStore,
14486
14587
  SqliteContextStore,
@@ -14491,6 +14592,7 @@ export {
14491
14592
  TomlRenderer,
14492
14593
  ToonRenderer,
14493
14594
  UrlBinaryInstaller,
14595
+ VirtualSandboxEnvironment,
14494
14596
  VolumeCreateError,
14495
14597
  VolumeInspectError,
14496
14598
  VolumePathError,