Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-js): scope in-flight cache invalidation to each sandbox
Claire-ovoc1a70e8cd49e
diff --git a/sdks/sandbox/javascript/src/core/endpointCache.ts b/sdks/sandbox/javascript/src/core/endpointCache.tsindex 59445a513..c7e101294 100644--- a/sdks/sandbox/javascript/src/core/endpointCache.ts+++ b/sdks/sandbox/javascript/src/core/endpointCache.ts@@ -30,7 +30,6 @@ export class EndpointCache {private readonly inflight = new Map<string, Promise<Endpoint>>();private readonly maxSize: number;private readonly ttlMs: number;- private generation = 0;constructor(opts: { maxSize?: number; ttlMs?: number } = {}) {this.maxSize = opts.maxSize ?? 1024;@@ -71,7 +70,6 @@ export class EndpointCache {}invalidate(sandboxId: string): void {- this.generation++;const prefix = `${sandboxId}:`;for (const key of [...this.cache.keys()]) {if (key.startsWith(prefix)) {@@ -99,15 +97,12 @@ export class EndpointCache {const existing = this.inflight.get(key);if (existing) return existing.then((ep) => this.cloneEndpoint(ep));- const genBefore = this.generation;const promise = fetcher().then((ep) => {- // Only cache if no invalidation occurred during fetch.- if (this.generation === genBefore) {- this.put(sandboxId, port, useServerProxy, ep);- }- // Only clear our own entry (an invalidate() may have replaced it).+ // Only the current fetch may populate this key or clear its entry.+ // Invalidating another sandbox must not discard this result.if (this.inflight.get(key) === promise) {+ this.put(sandboxId, port, useServerProxy, ep);this.inflight.delete(key);}return this.cloneEndpoint(ep);diff --git a/sdks/sandbox/javascript/tests/audit-regressions.test.mjs b/sdks/sandbox/javascript/tests/audit-regressions.test.mjsindex fdb6815a6..2d6fce375 100644--- a/sdks/sandbox/javascript/tests/audit-regressions.test.mjs+++ b/sdks/sandbox/javascript/tests/audit-regressions.test.mjs@@ -214,7 +214,7 @@ test("stale in-flight completion does not delete a newer fetch entry", async ()}const first = cache.getOrFetch("sbx", 44772, false, fetcher);- // invalidate() drops the first in-flight entry and bumps the generation.+ // invalidate() drops the first in-flight entry.cache.invalidate("sbx");const second = cache.getOrFetch("sbx", 44772, false, fetcher);diff --git a/sdks/sandbox/javascript/tests/endpoint-cache-isolation.test.mjs b/sdks/sandbox/javascript/tests/endpoint-cache-isolation.test.mjsnew file mode 100644index 000000000..943478dcf--- /dev/null+++ b/sdks/sandbox/javascript/tests/endpoint-cache-isolation.test.mjs@@ -0,0 +1,59 @@+// Copyright 2026 The OpenSandbox Authors+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++import assert from "node:assert/strict";+import test from "node:test";+import { EndpointCache } from "../dist/internal.js";++test("invalidating another sandbox does not discard an in-flight cache fill", async () => {+ const cache = new EndpointCache();+ let resolve;+ let calls = 0;+ const fetcher = () => {+ calls++;+ return new Promise((done) => { resolve = done; });+ };+ const pending = cache.getOrFetch("active", 8080, false, fetcher);+ cache.invalidate("unrelated");+ const joined = cache.getOrFetch("active", 8080, false, fetcher);+ resolve({ endpoint: "active:8080", headers: { token: "test" } });+ const [first, second] = await Promise.all([pending, joined]);+ assert.equal(calls, 1);+ assert.deepEqual(first, second);+ first.headers.token = "changed";+ assert.equal(second.headers.token, "test");+ assert.deepEqual(cache.get("active", 8080, false), second);+ const cached = await cache.getOrFetch("active", 8080, false, () => {+ assert.fail("unrelated invalidation must not cause a second request");+ });+ assert.deepEqual(cached, second);+});++for (const staleFails of [false, true]) {+ test(`same-sandbox invalidation fences a stale ${staleFails ? "failure" : "success"}`, async () => {+ const cache = new EndpointCache();+ let finishOld;+ let finishNew;+ const old = cache.getOrFetch("active", 8080, false, () => new Promise((resolve, reject) => {+ finishOld = staleFails ? () => reject(new Error("old fetch failed")) : () => resolve({ endpoint: "old", headers: {} });+ }));+ const oldResult = old.catch((error) => error);+ cache.invalidate("active");+ const replacement = cache.getOrFetch("active", 8080, false, () => new Promise((resolve) => { finishNew = resolve; }));+ finishOld();+ await oldResult;+ assert.equal(cache.get("active", 8080, false), undefined);+ const joined = cache.getOrFetch("active", 8080, false, () => assert.fail("replacement fetch must remain shared"));+ finishNew({ endpoint: "fresh", headers: {} });+ await Promise.all([replacement, joined]);+ assert.equal(cache.get("active", 8080, false).endpoint, "fresh");+ });+}