@deepagents/experimental 7.12.0 → 7.13.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/dist/index.js CHANGED
@@ -4753,6 +4753,10 @@ import { SandboxNotFoundError } from "@deepagents/context";
4753
4753
  var ConversationSandboxes = class {
4754
4754
  // One memoized acquisition for each declaration and conversation.
4755
4755
  #held = /* @__PURE__ */ new Map();
4756
+ // The check of a held acquisition that is in flight, which every caller
4757
+ // shares: a caller's own check could still be running on a handle that
4758
+ // another caller's check found gone and disposed.
4759
+ #checks = /* @__PURE__ */ new WeakMap();
4756
4760
  acquire(declaration, context) {
4757
4761
  const key = JSON.stringify([
4758
4762
  declaration.name,
@@ -4760,21 +4764,33 @@ var ConversationSandboxes = class {
4760
4764
  context.userId
4761
4765
  ]);
4762
4766
  const held = this.#held.get(key);
4763
- if (held !== void 0)
4764
- return this.#existing(key, held, declaration, context);
4765
- const acquiring = declaration.sandbox(context).catch((error) => {
4767
+ if (held === void 0)
4768
+ return this.#hold(key, declaration.sandbox(context));
4769
+ let check = this.#checks.get(held);
4770
+ if (check === void 0) {
4771
+ const checking = this.#check(key, held, declaration, context).finally(
4772
+ () => this.#checks.delete(held)
4773
+ );
4774
+ check = () => checking;
4775
+ this.#checks.set(held, check);
4776
+ }
4777
+ return check();
4778
+ }
4779
+ // A failed acquisition is forgotten, so the next turn tries again.
4780
+ #hold(key, acquiring) {
4781
+ const held = acquiring.catch((error) => {
4766
4782
  this.#held.delete(key);
4767
4783
  throw error;
4768
4784
  });
4769
- this.#held.set(key, () => acquiring);
4770
- return acquiring;
4785
+ this.#held.set(key, () => held);
4786
+ return held;
4771
4787
  }
4772
4788
  /**
4773
4789
  * Checked before the turn, not left to its first command: the turn's tools
4774
4790
  * are bound to this handle, so a sandbox found gone mid-turn fails the
4775
4791
  * model's tool call instead of acquiring a new sandbox.
4776
4792
  */
4777
- async #existing(key, held, declaration, context) {
4793
+ async #check(key, held, declaration, context) {
4778
4794
  const handle = await held();
4779
4795
  try {
4780
4796
  await handle.sandbox.run({ command: "true" });
@@ -4782,11 +4798,10 @@ var ConversationSandboxes = class {
4782
4798
  } catch (error) {
4783
4799
  if (!(error instanceof SandboxNotFoundError)) throw error;
4784
4800
  }
4785
- if (this.#held.get(key) === held) {
4786
- this.#held.delete(key);
4787
- await handle.sandbox.dispose();
4788
- }
4789
- return this.acquire(declaration, context);
4801
+ return this.#hold(
4802
+ key,
4803
+ handle.sandbox.dispose().then(() => declaration.sandbox(context))
4804
+ );
4790
4805
  }
4791
4806
  async [Symbol.asyncDispose]() {
4792
4807
  await using handles = new AsyncDisposableStack();