Skip to content

An open-source, faster Devin alternative

TypeScript

Merge origin/main into change-backing

Brian Reardon7e059d0c5f45
diff --cc cloudflare-workers/api-edge/src/index.ts
index 8fff572b,a30a2363..a2c0989f
--- a/cloudflare-workers/api-edge/src/index.ts
+++ b/cloudflare-workers/api-edge/src/index.ts
@@@ -1502,21 -1338,27 +1449,22 @@@ async function createSandbox(req: Reque
// the pre-sharding DO (holding the fleet's stock at cutover) drains as a
// first-class shard instead of hoarding reservations until TTL. One empty
// shard (404) gets one retry on a different shard before the CP fallback.
+ const claimBody = JSON.stringify({ cell: { cellID: cell.cell_id, baseURL: cell.base_url } });
mark("cap");
- // Sample up to 3 distinct shards (random walk with a random stride
- // coprime-ish to 8) — at full stock the first hit ends it; under sparse
- // stock (small cells, mid-battery drain) 3 samples cut the false-fallback
- // rate from ~(empty/8)² to ~(empty/8)³ for ~10ms per extra probe.
- let box: { id: string; workerID: string; region: string; sandboxDomain: string } | null = null;
- let shard = Math.floor(Math.random() * POOL_STOCK_SHARDS);
- const stride = 1 + Math.floor(Math.random() * (POOL_STOCK_SHARDS - 1));
- for (let attempt = 0; attempt < 3 && !box; attempt++) {
- const r = await poolStockStub(env, cell.cell_id, shard).fetch("https://pool-stock/claim", {
- method: "POST",
- body: claimBody,
- });
- shard = (shard + stride) % POOL_STOCK_SHARDS;
- if (!r.ok) continue;
- box = (await r.json()) as { id: string; workerID: string; region: string; sandboxDomain: string };
- }
- if (box) {
+ // One DO pop. The token was minted in the DO when it filled its stock, so
+ // a hit costs one subrequest and no crypto on this isolate.
+ const claimed = await claimPoolBox(env, cell, caller.orgID);
+ const box = claimed?.box ?? null;
+ if (box && claimed) {
mark("claim");
- const token = await mintSandboxToken(env.SESSION_JWT_SECRET, caller.orgID, box.id, box.workerID);
+ // Telemetry: stock left in the shard that served this claim. `dur` is a
+ // value here, not a duration.
+ phases.push(`stock;dur=${claimed.stock}`);
+ // `claim` above is DO work + network to the DO. This is the DO's half,
+ // so the remainder is the hop — see claimBatch. `doclo` is a colo name,
+ // not a duration; Server-Timing has nowhere else to put it.
+ phases.push(`doin;dur=${claimed.doMs}`, `doclo;desc=${claimed.doColo}`);
+ const token = box.token;
if (sandboxRouteCache.size >= CACHE_MAX) sandboxRouteCache.clear();
sandboxRouteCache.set(box.id, { cellID: cell.cell_id, orgID: caller.orgID });
// Seed the colo-shared route so the first exec — which usually lands on