@deepagents/experimental 7.13.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.
@@ -4389,6 +4389,10 @@ import { SandboxNotFoundError } from "@deepagents/context";
4389
4389
  var ConversationSandboxes = class {
4390
4390
  // One memoized acquisition for each declaration and conversation.
4391
4391
  #held = /* @__PURE__ */ new Map();
4392
+ // The check of a held acquisition that is in flight, which every caller
4393
+ // shares: a caller's own check could still be running on a handle that
4394
+ // another caller's check found gone and disposed.
4395
+ #checks = /* @__PURE__ */ new WeakMap();
4392
4396
  acquire(declaration, context) {
4393
4397
  const key = JSON.stringify([
4394
4398
  declaration.name,
@@ -4396,21 +4400,33 @@ var ConversationSandboxes = class {
4396
4400
  context.userId
4397
4401
  ]);
4398
4402
  const held = this.#held.get(key);
4399
- if (held !== void 0)
4400
- return this.#existing(key, held, declaration, context);
4401
- const acquiring = declaration.sandbox(context).catch((error) => {
4403
+ if (held === void 0)
4404
+ return this.#hold(key, declaration.sandbox(context));
4405
+ let check = this.#checks.get(held);
4406
+ if (check === void 0) {
4407
+ const checking = this.#check(key, held, declaration, context).finally(
4408
+ () => this.#checks.delete(held)
4409
+ );
4410
+ check = () => checking;
4411
+ this.#checks.set(held, check);
4412
+ }
4413
+ return check();
4414
+ }
4415
+ // A failed acquisition is forgotten, so the next turn tries again.
4416
+ #hold(key, acquiring) {
4417
+ const held = acquiring.catch((error) => {
4402
4418
  this.#held.delete(key);
4403
4419
  throw error;
4404
4420
  });
4405
- this.#held.set(key, () => acquiring);
4406
- return acquiring;
4421
+ this.#held.set(key, () => held);
4422
+ return held;
4407
4423
  }
4408
4424
  /**
4409
4425
  * Checked before the turn, not left to its first command: the turn's tools
4410
4426
  * are bound to this handle, so a sandbox found gone mid-turn fails the
4411
4427
  * model's tool call instead of acquiring a new sandbox.
4412
4428
  */
4413
- async #existing(key, held, declaration, context) {
4429
+ async #check(key, held, declaration, context) {
4414
4430
  const handle = await held();
4415
4431
  try {
4416
4432
  await handle.sandbox.run({ command: "true" });
@@ -4418,11 +4434,10 @@ var ConversationSandboxes = class {
4418
4434
  } catch (error) {
4419
4435
  if (!(error instanceof SandboxNotFoundError)) throw error;
4420
4436
  }
4421
- if (this.#held.get(key) === held) {
4422
- this.#held.delete(key);
4423
- await handle.sandbox.dispose();
4424
- }
4425
- return this.acquire(declaration, context);
4437
+ return this.#hold(
4438
+ key,
4439
+ handle.sandbox.dispose().then(() => declaration.sandbox(context))
4440
+ );
4426
4441
  }
4427
4442
  async [Symbol.asyncDispose]() {
4428
4443
  await using handles = new AsyncDisposableStack();