diff --git a/server/src/services/native-runtime/runtime-asset-retention.test.ts b/server/src/services/native-runtime/runtime-asset-retention.test.ts index 2d8444c0184..313e924b488 100644 --- a/server/src/services/native-runtime/runtime-asset-retention.test.ts +++ b/server/src/services/native-runtime/runtime-asset-retention.test.ts @@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { Db } from "@paperclipai/db"; import { resolvePaperclipInstanceRoot } from "../../home-paths.js"; import { materializeAsset } from "./runtime-context.js"; -import { removeRuntimeAssetTree, runtimeAssetRetention, RUNTIME_ASSET_RETENTION_MS } from "./runtime-asset-retention.js"; +import { removeRuntimeAssetTree, runtimeAssetRetention, RUNTIME_ASSET_RETENTION_MS, withRuntimeAssetUse } from "./runtime-asset-retention.js"; let home: string; const originalHome = process.env.PAPERCLIP_HOME; @@ -54,4 +54,34 @@ describe("runtime asset retention", () => { expect((await fs.stat(asset.rootPath)).isDirectory()).toBe(true); expect(worker.nextRunAt()).toBeNull(); }); + it("refreshes a cold lease once for concurrent turns instead of queueing each on the lock", async () => { + const digest = "a".repeat(64); + const manifests = path.join(resolvePaperclipInstanceRoot(), "runtime-context-assets", "manifests"); + await fs.mkdir(manifests, { recursive: true }); + await fs.writeFile(path.join(manifests, `${digest}.json`), "{}"); + const modes: boolean[] = []; + const use = () => withRuntimeAssetUse(digest, async locked => { + modes.push(locked); + if (locked) await new Promise(resolve => setTimeout(resolve, 50)); + return locked; + }); + const results = await Promise.all(Array.from({ length: 8 }, use)); + expect(modes.filter(Boolean)).toHaveLength(1); + expect(results.filter(Boolean)).toHaveLength(1); + expect(await withRuntimeAssetUse(digest, async locked => locked, true)).toBe(true); + }); + it("lets waiters take the lock themselves when the shared refresh records no lease", async () => { + const digest = "b".repeat(64); + const modes: boolean[] = []; + let first = true; + const use = () => withRuntimeAssetUse(digest, async locked => { + modes.push(locked); + if (first) { first = false; await new Promise(resolve => setTimeout(resolve, 20)); throw new Error("refresh failed"); } + return locked; + }); + const [leader, ...waiters] = await Promise.allSettled(Array.from({ length: 4 }, use)); + expect(leader.status).toBe("rejected"); + expect(waiters).toEqual(Array.from({ length: 3 }, () => ({ status: "fulfilled", value: true }))); + expect(modes).toEqual([true, true, true, true]); + }); }); diff --git a/server/src/services/native-runtime/runtime-asset-retention.ts b/server/src/services/native-runtime/runtime-asset-retention.ts index 2e74881888b..a511a486205 100644 --- a/server/src/services/native-runtime/runtime-asset-retention.ts +++ b/server/src/services/native-runtime/runtime-asset-retention.ts @@ -12,6 +12,7 @@ import { DELIVERY_QUEUES } from "../delivery-work-notifications.js"; export const RUNTIME_ASSET_RETENTION_MS = 30 * 24 * 60 * 60_000; const TOUCH_MS = 60 * 60_000; const touched = new Map(); +const refreshing = new Map>(); const observers = new Set<() => void>(); const root = () => path.join(resolvePaperclipInstanceRoot(), "runtime-context-assets"); function paths(digest: string) { @@ -24,9 +25,27 @@ function paths(digest: string) { * protects the short interval between preparation and durable run pinning. */ export async function withRuntimeAssetUse(digest: string, consume: (locked: boolean) => Promise, mutation = false): Promise { const target = paths(digest); - const last = touched.get(target.manifest); - const fresh = last !== undefined && Date.now() - last < TOUCH_MS; - if (fresh && !mutation) return consume(false); + if (!mutation) { + if (leaseFresh(target.manifest)) return consume(false); + const inflight = refreshing.get(target.manifest); + if (inflight) { + await inflight.catch(() => undefined); + if (leaseFresh(target.manifest)) return consume(false); + } else { + const refresh = useLocked(target, consume, false); + refreshing.set(target.manifest, refresh); + try { return await refresh; } finally { if (refreshing.get(target.manifest) === refresh) refreshing.delete(target.manifest); } + } + } + return useLocked(target, consume, leaseFresh(target.manifest)); +} + +function leaseFresh(manifest: string) { + const last = touched.get(manifest); + return last !== undefined && Date.now() - last < TOUCH_MS; +} + +async function useLocked(target: ReturnType, consume: (locked: boolean) => Promise, fresh: boolean): Promise { await ensureSkillCacheDirectory(path.dirname(target.bundle), true); return withDirectoryPublicationLock(target.bundle, async () => { const result = await consume(true);