Skip to content

Commit 278dcd4

Browse files
committed
perf(gateway): coalesce workspace root preparation
1 parent 4923d34 commit 278dcd4

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

‎src/agents/prepared-model-runtime-lease.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { PreparedModelRuntimeCatalogMode } from "./prepared-model-runtime.t
2323
type PreparedModelRuntimeLeaseContext = {
2424
owners: Map<string, PreparedModelRuntimeOwner>;
2525
agentBuildCompletions: Map<string, Promise<void>>;
26+
workspacePluginRootPresenceResolutions: Map<string, Promise<boolean | undefined>>;
2627
retainedDirectRunOwners: PreparedModelRuntimeOwnerRetention;
2728
retainedGatewayRunOwners: PreparedModelRuntimeOwnerRetention;
2829
getBuildTimeoutMs(): number;
@@ -35,6 +36,26 @@ type PreparedModelRuntimeLeaseContext = {
3536
): Promise<PreparedModelRuntimeSnapshot>;
3637
};
3738

39+
async function resolveCoalescedWorkspacePluginRootPresence(
40+
input: PreparedModelRuntimeInput,
41+
context: PreparedModelRuntimeLeaseContext,
42+
): Promise<boolean | undefined> {
43+
const key = ownerKey(input);
44+
const existing = context.workspacePluginRootPresenceResolutions.get(key);
45+
if (existing) {
46+
return await existing;
47+
}
48+
const pending = resolveWorkspacePluginRootPresence(input);
49+
context.workspacePluginRootPresenceResolutions.set(key, pending);
50+
try {
51+
return await pending;
52+
} finally {
53+
if (context.workspacePluginRootPresenceResolutions.get(key) === pending) {
54+
context.workspacePluginRootPresenceResolutions.delete(key);
55+
}
56+
}
57+
}
58+
3859
export async function resolveWorkspacePluginRootPresence(
3960
input: PreparedModelRuntimeInput,
4061
): Promise<boolean | undefined> {
@@ -85,7 +106,9 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
85106
const workspacePluginRootPresent =
86107
rawInput.workspacePluginRootPresent ??
87108
retainedWorkspacePluginRootPresent ??
88-
(provenance === "run" ? await resolveWorkspacePluginRootPresence(normalizedInput) : undefined);
109+
(provenance === "run"
110+
? await resolveCoalescedWorkspacePluginRootPresence(normalizedInput, context)
111+
: undefined);
89112
let input = normalizePreparedModelRuntimeInput({
90113
...normalizedInput,
91114
...(workspacePluginRootPresent === undefined ? {} : { workspacePluginRootPresent }),

‎src/agents/prepared-model-runtime.lifecycle.test.ts‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,18 @@ describe("prepared model runtime snapshots", () => {
244244
const workspacePluginRoot = path.join(workspaceDir, ".openclaw", "extensions");
245245
const statSpy = vi.spyOn(fsp, "stat");
246246

247-
const firstLease = await acquireAgentRunPreparedModelRuntime({
248-
agentId: "default",
249-
config,
250-
agentDir: "/tmp/unused-agent",
251-
inheritedAuthDir: "/tmp/unused-agent",
252-
workspaceDir,
253-
});
254-
const secondLease = await acquireAgentRunPreparedModelRuntime({
255-
agentId: "default",
256-
config,
257-
agentDir: "/tmp/unused-agent",
258-
inheritedAuthDir: "/tmp/unused-agent",
259-
workspaceDir,
260-
});
247+
const acquireDynamicLease = () =>
248+
acquireAgentRunPreparedModelRuntime({
249+
agentId: "default",
250+
config,
251+
agentDir: "/tmp/unused-agent",
252+
inheritedAuthDir: "/tmp/unused-agent",
253+
workspaceDir,
254+
});
255+
const [firstLease, secondLease] = await Promise.all([
256+
acquireDynamicLease(),
257+
acquireDynamicLease(),
258+
]);
261259

262260
expect(firstLease.snapshot.workspaceDir).toBe(workspaceDir);
263261
expect(secondLease.snapshot).toBe(firstLease.snapshot);

‎src/agents/prepared-model-runtime.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ describe("prepared model runtime snapshots", () => {
896896
const skippedConfig = { agents: { defaults: { model: "openai/gpt-5.4" } } };
897897
const latestConfig = { agents: { defaults: { model: "openai/gpt-5.5" } } };
898898
await refreshPreparedModelRuntimeSnapshots(initialConfig);
899-
let finishLatestBuild!: () => void;
899+
let finishLatestBuild: (() => void) | undefined;
900900
mocks.ensureOpenClawModelsJson.mockImplementationOnce(
901901
async () =>
902902
await new Promise<{ agentDir: string; wrote: boolean }>((resolve) => {
@@ -927,7 +927,8 @@ describe("prepared model runtime snapshots", () => {
927927
Promise.resolve("pending"),
928928
]),
929929
).resolves.toBe("pending");
930-
finishLatestBuild();
930+
await vi.waitFor(() => expect(finishLatestBuild).toEqual(expect.any(Function)));
931+
finishLatestBuild?.();
931932
await latest;
932933
await expect(read).resolves.toMatchObject({ config: latestConfig });
933934
});

‎src/agents/prepared-model-runtime.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ let modelRuntimeBuildTimeoutMs = DEFAULT_MODEL_RUNTIME_BUILD_TIMEOUT_MS;
6464

6565
const owners = new Map<string, PreparedModelRuntimeOwner>();
6666
const agentBuildCompletions = new Map<string, Promise<void>>();
67+
const workspacePluginRootPresenceResolutions = new Map<string, Promise<boolean | undefined>>();
6768
const standaloneActivationTails = new Map<string, Promise<void>>();
6869
const retainedDirectRunOwners = new PreparedModelRuntimeOwnerRetention(1);
6970
const retainedGatewayRunOwners = new PreparedModelRuntimeOwnerRetention(8);
@@ -274,6 +275,7 @@ async function activateStandalonePreparedModelRuntimeNow(
274275
const preparedModelRuntimeLeaseContext = {
275276
owners,
276277
agentBuildCompletions,
278+
workspacePluginRootPresenceResolutions,
277279
retainedDirectRunOwners,
278280
retainedGatewayRunOwners,
279281
getBuildTimeoutMs: () => modelRuntimeBuildTimeoutMs,
@@ -650,6 +652,7 @@ function resetPreparedModelRuntimeSnapshotsForTest(): void {
650652
pendingModelRuntimeReplacement = undefined;
651653
owners.clear();
652654
agentBuildCompletions.clear();
655+
workspacePluginRootPresenceResolutions.clear();
653656
standaloneActivationTails.clear();
654657
retainedGatewayRunOwners.clear(owners);
655658
gatewayLifecycleActive = false;

0 commit comments

Comments
 (0)