Skip to content

Commit 06c2dc7

Browse files
xialongleeobviyus
andauthored
fix(agents): default run admissions to static catalog (#123432)
Direct agent-run admissions inherited the general live catalog default, so compaction and exact-model fallback work could rebuild provider catalogs before starting. Default run admission to configured static facts. Replace a matching static owner when an explicit live request follows, while keeping full control-plane catalog reads on their existing generation-owned loader. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 3b609ec commit 06c2dc7

5 files changed

Lines changed: 62 additions & 31 deletions

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,6 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
146146
existing?.needsRefresh &&
147147
!existing.pending &&
148148
(existing.provenance === "run" || existing.provenance === "ephemeral");
149-
const pluginGenerationChanged =
150-
options.pluginGeneration !== undefined &&
151-
!preparedPluginGenerationReusesBase(
152-
existing?.pending ? existing.pendingPluginGeneration : existing?.pluginGeneration,
153-
options.pluginGeneration,
154-
);
155-
if (existing?.pending && pluginGenerationChanged) {
156-
// Do not supersede active discovery. Wait for its owner to settle, then retry against
157-
// the published identity so same-generation callers still coalesce.
158-
await racePromiseWithAbortSignal(
159-
existing.pending.catch(() => undefined),
160-
options.abortSignal,
161-
);
162-
continue;
163-
}
164149
if (
165150
context.getGatewayLifecycleActive() &&
166151
provenance === "run" &&
@@ -197,8 +182,25 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
197182
// has an owner to rebind. Keep ordinary agent runs fail-closed at this ownership boundary.
198183
}
199184
}
185+
// A static owner cannot satisfy explicit live discovery; publish a new exact generation.
186+
const ownerGenerationChanged =
187+
(options.pluginGeneration !== undefined &&
188+
!preparedPluginGenerationReusesBase(
189+
existing?.pending ? existing.pendingPluginGeneration : existing?.pluginGeneration,
190+
options.pluginGeneration,
191+
)) ||
192+
(options.catalogMode === "live" && existing?.catalogMode === "static");
193+
if (existing?.pending && ownerGenerationChanged) {
194+
// Do not supersede active discovery. Wait for its owner to settle, then retry against
195+
// the published identity so same-generation callers still coalesce.
196+
await racePromiseWithAbortSignal(
197+
existing.pending.catch(() => undefined),
198+
options.abortSignal,
199+
);
200+
continue;
201+
}
200202
try {
201-
if (existing?.pending && !pluginGenerationChanged) {
203+
if (existing?.pending && !ownerGenerationChanged) {
202204
// Matching callers lease the immutable generation they joined even if a queued
203205
// mismatched caller publishes the next owner immediately after this one settles.
204206
snapshot = await racePromiseWithAbortSignal(existing.pending, options.abortSignal);
@@ -208,7 +210,7 @@ export async function acquirePreparedModelRuntimeLeaseFromOwners(
208210
owner = existing;
209211
break;
210212
}
211-
if (existing && !staleDynamicOwner && !pluginGenerationChanged) {
213+
if (existing && !staleDynamicOwner && !ownerGenerationChanged) {
212214
snapshot = await racePromiseWithAbortSignal(
213215
context.prepareSnapshot(input),
214216
options.abortSignal,

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,19 @@ describe("prepared model runtime snapshots", () => {
190190
);
191191
});
192192

193-
it("publishes a static turn generation without live catalog discovery", async () => {
193+
it.each([
194+
["omitted options", undefined],
195+
["partial options", { retainIdleRunOwner: true }],
196+
["explicit static mode", { catalogMode: "static" as const }],
197+
])("defaults %s to static turn facts without live catalog discovery", async (_name, options) => {
194198
const lease = await acquireAgentRunPreparedModelRuntime(
195199
{
196200
config: {},
197201
agentId: "default",
198202
agentDir: state.agentDir("static-run-agent"),
199203
workspaceDir: "/tmp/static-run-workspace",
200204
},
201-
{ catalogMode: "static" },
205+
options,
202206
);
203207

204208
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
@@ -223,7 +227,8 @@ describe("prepared model runtime snapshots", () => {
223227
retainIdleRunOwner: true,
224228
});
225229
reusedLease.release();
226-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
230+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
231+
expect(mocks.ensureOpenClawModelsJson).not.toHaveBeenCalled();
227232

228233
const secondInput = {
229234
...firstInput,
@@ -238,7 +243,8 @@ describe("prepared model runtime snapshots", () => {
238243
"prepared model runtime owner was not published",
239244
);
240245
await expect(prepareModelRuntimeSnapshot(secondInput)).resolves.toBe(secondLease.snapshot);
241-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
246+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledTimes(2);
247+
expect(mocks.ensureOpenClawModelsJson).not.toHaveBeenCalled();
242248
});
243249

244250
it("retains an exact dynamic workspace owner after gateway run admission", async () => {
@@ -275,7 +281,8 @@ describe("prepared model runtime snapshots", () => {
275281
});
276282
expect(retainedLease.snapshot).toBe(firstLease.snapshot);
277283
retainedLease.release();
278-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
284+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
285+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
279286
});
280287

281288
it("joins an in-flight dynamic owner publication", async () => {
@@ -284,10 +291,10 @@ describe("prepared model runtime snapshots", () => {
284291
await refreshPreparedModelRuntimeSnapshots(config, { gatewayLifecycle: true });
285292
const finishDynamicGate = createDeferred();
286293
let finishDynamic!: () => void;
287-
mocks.ensureOpenClawModelsJson.mockImplementationOnce(async (_config, targetDir) => {
294+
mocks.prepareStaticCatalog.mockImplementationOnce(async () => {
288295
finishDynamic = () => finishDynamicGate.resolve();
289296
await finishDynamicGate.promise;
290-
return { agentDir: String(targetDir), wrote: false };
297+
return { entries: [] };
291298
});
292299
const input = {
293300
agentId: "default",
@@ -301,15 +308,16 @@ describe("prepared model runtime snapshots", () => {
301308
let secondPending: ReturnType<typeof acquireAgentRunPreparedModelRuntime> | undefined;
302309
try {
303310
firstPending = acquireAgentRunPreparedModelRuntime(input);
304-
await vi.waitFor(() => expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2));
311+
await vi.waitFor(() => expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce());
305312
secondPending = acquireAgentRunPreparedModelRuntime(input);
306313
await Promise.resolve();
307-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
314+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
308315
finishDynamic();
309316
const [first, second] = await Promise.all([firstPending, secondPending]);
310317

311318
expect(second.snapshot).toBe(first.snapshot);
312-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
319+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
320+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
313321
first.release();
314322
second.release();
315323
} finally {
@@ -519,7 +527,8 @@ describe("prepared model runtime snapshots", () => {
519527

520528
expect(lease.snapshot.config).toBe(latestConfig);
521529
expect(lease.snapshot.workspaceDir).toBe("/tmp/dynamic-replacement-workspace");
522-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(3);
530+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
531+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
523532
lease.release();
524533
} finally {
525534
finishReplacementGate.resolve();

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ describe("prepared model runtime owner selection", () => {
247247
expect(mocks.ensureOpenClawModelsJson).not.toHaveBeenCalled();
248248
});
249249

250+
it("replaces a static run owner when an explicit live acquisition follows", async () => {
251+
const input = {
252+
config: {},
253+
agentId: "default",
254+
agentDir: state.agentDir("catalog-mode-upgrade"),
255+
workspaceDir: "/tmp/catalog-mode-upgrade-workspace",
256+
};
257+
const staticLease = await acquireAgentRunPreparedModelRuntime(input);
258+
const liveLease = await acquireAgentRunPreparedModelRuntime(input, { catalogMode: "live" });
259+
260+
expect(liveLease.snapshot).not.toBe(staticLease.snapshot);
261+
expect(mocks.prepareStaticCatalog).toHaveBeenCalledOnce();
262+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
263+
staticLease.release();
264+
liveLease.release();
265+
});
266+
250267
it("rejects unpublished plugin generations while matching pending callers share their owner", async () => {
251268
mocks.configuredAgentIds = ["default"];
252269
const config = {};
@@ -276,13 +293,16 @@ describe("prepared model runtime owner selection", () => {
276293
let matchingPendingA: ReturnType<typeof acquireAgentRunPreparedModelRuntime> | undefined;
277294
try {
278295
pendingA = acquireAgentRunPreparedModelRuntime(input, {
296+
catalogMode: "live",
279297
pluginGeneration: generationA!,
280298
});
281299
await vi.waitFor(() => expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2));
282300
matchingPendingA = acquireAgentRunPreparedModelRuntime(input, {
301+
catalogMode: "live",
283302
pluginGeneration: generationA!,
284303
});
285304
const pendingB = acquireAgentRunPreparedModelRuntime(input, {
305+
catalogMode: "live",
286306
pluginGeneration: generationB,
287307
}).catch((error: unknown) => error);
288308
await Promise.resolve();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ const preparedModelRuntimeLeaseContext = {
290290
prepareSnapshot: prepareModelRuntimeSnapshot,
291291
};
292292

293-
/** Acquires the exact writable workspace generation at agent-run admission. */
293+
/** Acquires a run generation from configured facts; full catalog discovery is explicit. */
294294
export async function acquireAgentRunPreparedModelRuntime(
295295
rawInput: PreparedModelRuntimeInput,
296296
options: {
@@ -305,7 +305,7 @@ export async function acquireAgentRunPreparedModelRuntime(
305305
rawInput,
306306
"run",
307307
preparedModelRuntimeLeaseContext,
308-
options,
308+
{ ...options, catalogMode: options.catalogMode ?? "static" },
309309
);
310310
}
311311

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type PreparedModelRuntimeSnapshot = Readonly<{
4949
metadataSnapshot: PluginMetadataSnapshot;
5050
messageToolCatalog?: PreparedMessageToolCatalog;
5151
mediaCapabilityProviders?: ReturnType<typeof prepareMediaCapabilityProviders>;
52-
/** Registry value owned by this generation; omitted from read-only/static-catalog builds. */
52+
/** Registry value owned by this generation; omitted from read-only builds. */
5353
pluginRegistry?: PluginRegistry;
5454
allowGatewaySubagentBinding: boolean;
5555
/**

0 commit comments

Comments
 (0)