@@ -11,6 +11,7 @@ import path from "node:path";
1111import { beforeEach , describe , expect , it , vi } from "vitest" ;
1212import { retainLegacyDefaultAgentId } from "../config/legacy.default-agent-owner.js" ;
1313import { createEmptyPluginRegistry } from "../plugins/registry-empty.js" ;
14+ import { withPreparedModelRuntimePluginGenerationScope } from "./prepared-model-runtime-generation-scope.js" ;
1415import {
1516 acquireAgentRunPreparedModelRuntime ,
1617 acquireReadOnlyPreparedModelRuntime ,
@@ -246,7 +247,7 @@ describe("prepared model runtime owner selection", () => {
246247 expect ( mocks . ensureOpenClawModelsJson ) . not . toHaveBeenCalled ( ) ;
247248 } ) ;
248249
249- it ( "sequences pending owners by their explicit plugin generation " , async ( ) => {
250+ it ( "rejects unpublished plugin generations while matching pending callers share their owner " , async ( ) => {
250251 mocks . configuredAgentIds = [ "default" ] ;
251252 const config = { } ;
252253 await refreshPreparedModelRuntimeSnapshots ( config , { gatewayLifecycle : true } ) ;
@@ -281,24 +282,170 @@ describe("prepared model runtime owner selection", () => {
281282 } ) ;
282283 const pendingB = acquireAgentRunPreparedModelRuntime ( input , {
283284 pluginGeneration : generationB ,
284- } ) ;
285+ } ) . catch ( ( error : unknown ) => error ) ;
285286 await Promise . resolve ( ) ;
286287 expect ( mocks . ensureOpenClawModelsJson ) . toHaveBeenCalledTimes ( 2 ) ;
287288 finishGenerationA ( ) ;
288- const [ leaseA , matchingLeaseA , leaseB ] = await Promise . all ( [
289+ const [ leaseA , matchingLeaseA , rejectedGeneration ] = await Promise . all ( [
289290 pendingA ,
290291 matchingPendingA ,
291292 pendingB ,
292293 ] ) ;
293294
294295 expect ( matchingLeaseA . snapshot ) . toBe ( leaseA . snapshot ) ;
295- expect ( leaseB . snapshot ) . not . toBe ( leaseA . snapshot ) ;
296+ expect ( rejectedGeneration ) . toEqual (
297+ expect . objectContaining ( { message : expect . stringContaining ( "superseded" ) } ) ,
298+ ) ;
296299 expect ( leaseA . snapshot . metadataSnapshot ) . toBe ( generationA ! . pluginMetadataSnapshot ) ;
297- expect ( leaseB . snapshot . metadataSnapshot ) . toBe ( generationB . pluginMetadataSnapshot ) ;
300+ await expect ( prepareModelRuntimeSnapshot ( input ) ) . resolves . toBe ( leaseA . snapshot ) ;
298301 leaseA . release ( ) ;
299302 matchingLeaseA . release ( ) ;
300- await expect ( prepareModelRuntimeSnapshot ( input ) ) . resolves . toBe ( leaseB . snapshot ) ;
301- leaseB . release ( ) ;
303+ } ) ;
304+
305+ it ( "keeps a committed gateway owner current when an admitted turn resumes on its older generation" , async ( ) => {
306+ mocks . configuredAgentIds = [ "default" ] ;
307+ const defaults = {
308+ model : "openai/gpt-5" ,
309+ models : {
310+ "openai/gpt-5" : { params : { transport : "sse" as const , openaiWsWarmup : false } } ,
311+ } ,
312+ } ;
313+ const previousConfig = {
314+ agents : { defaults } ,
315+ messages : { responsePrefix : "previous" } ,
316+ } ;
317+ const committedConfig = {
318+ agents : { defaults } ,
319+ messages : { responsePrefix : "committed" } ,
320+ } ;
321+ const publicationOptions = {
322+ allowGatewaySubagentBinding : true ,
323+ catalogMode : "static" as const ,
324+ gatewayLifecycle : true ,
325+ } ;
326+ const runInput = ( config : typeof previousConfig ) => ( {
327+ agentId : "default" ,
328+ agentDir : "/tmp/unused-agent" ,
329+ allowGatewaySubagentBinding : true ,
330+ config,
331+ runtimePluginSelections : [ { provider : "openai" , modelId : "gpt-5" , runtime : "openclaw" } ] ,
332+ workspaceDir : "/tmp/unused-workspace" ,
333+ } ) ;
334+
335+ await refreshPreparedModelRuntimeSnapshots ( previousConfig , publicationOptions ) ;
336+ const admitted = await loadPublishedGatewayReplyDispatchRuntime ( { agentId : "default" } ) ;
337+ const previousSnapshot = getPreparedModelRuntimeSnapshot ( runInput ( previousConfig ) ) ;
338+ expect ( admitted ?. pluginGeneration ) . toBeDefined ( ) ;
339+ expect ( previousSnapshot ) . toBeDefined ( ) ;
340+
341+ const outerLease = await acquireAgentRunPreparedModelRuntime ( runInput ( previousConfig ) , {
342+ catalogMode : "static" ,
343+ pluginGeneration : admitted ! . pluginGeneration ,
344+ } ) ;
345+ expect ( outerLease . snapshot ) . toBe ( previousSnapshot ) ;
346+ let outerLeaseActive = true ;
347+
348+ await refreshPreparedModelRuntimeSnapshots ( committedConfig , publicationOptions ) ;
349+ const committedSnapshot = getPreparedModelRuntimeSnapshot ( runInput ( committedConfig ) ) ;
350+ const registryLoads = mocks . loadAgentRuntimePluginRegistryHandle . mock . calls . length ;
351+ expect ( committedSnapshot ) . toBeDefined ( ) ;
352+ expect ( committedSnapshot ) . not . toBe ( previousSnapshot ) ;
353+ let releaseDetachedAdmission ! : ( ) => void ;
354+ const detachedAdmissionGate = new Promise < void > ( ( resolve ) => {
355+ releaseDetachedAdmission = resolve ;
356+ } ) ;
357+ let detachedAdmission ! : Promise < unknown > ;
358+
359+ try {
360+ await withPreparedModelRuntimePluginGenerationScope (
361+ admitted ! . pluginGeneration ,
362+ async ( ) => {
363+ const resumed = await withPreparedModelRuntimePluginGenerationScope (
364+ admitted ! . pluginGeneration ,
365+ async ( ) =>
366+ await acquireAgentRunPreparedModelRuntime ( runInput ( previousConfig ) , {
367+ catalogMode : "static" ,
368+ pluginGeneration : admitted ! . pluginGeneration ,
369+ } ) ,
370+ ) ;
371+
372+ expect ( resumed . snapshot ) . toBe ( previousSnapshot ) ;
373+ expect ( getPreparedModelRuntimeSnapshot ( runInput ( committedConfig ) ) ) . toBe (
374+ committedSnapshot ,
375+ ) ;
376+ expect ( mocks . loadAgentRuntimePluginRegistryHandle ) . toHaveBeenCalledTimes ( registryLoads ) ;
377+ resumed . release ( ) ;
378+ const clonedConfigLease = await acquireAgentRunPreparedModelRuntime (
379+ runInput ( structuredClone ( previousConfig ) ) ,
380+ { pluginGeneration : admitted ! . pluginGeneration } ,
381+ ) ;
382+ expect ( clonedConfigLease . snapshot ) . toBe ( previousSnapshot ) ;
383+ clonedConfigLease . release ( ) ;
384+ await expect (
385+ acquireAgentRunPreparedModelRuntime (
386+ { ...runInput ( previousConfig ) , workspaceDir : "/tmp/different-workspace" } ,
387+ { pluginGeneration : admitted ! . pluginGeneration } ,
388+ ) ,
389+ ) . rejects . toThrow ( "plugin generation was superseded" ) ;
390+ await expect (
391+ acquireAgentRunPreparedModelRuntime ( runInput ( previousConfig ) , {
392+ pluginGeneration : { ...admitted ! . pluginGeneration } ,
393+ } ) ,
394+ ) . rejects . toThrow ( "plugin generation was superseded" ) ;
395+ detachedAdmission = detachedAdmissionGate . then ( async ( ) =>
396+ acquireAgentRunPreparedModelRuntime ( runInput ( previousConfig ) , {
397+ pluginGeneration : admitted ! . pluginGeneration ,
398+ } ) ,
399+ ) ;
400+ } ,
401+ ( ) => ( outerLeaseActive ? outerLease . snapshot : undefined ) ,
402+ ) ;
403+ } finally {
404+ outerLeaseActive = false ;
405+ outerLease . release ( ) ;
406+ }
407+ releaseDetachedAdmission ( ) ;
408+ await expect ( detachedAdmission ) . rejects . toThrow ( "plugin generation was superseded" ) ;
409+
410+ await expect (
411+ acquireAgentRunPreparedModelRuntime ( runInput ( previousConfig ) , {
412+ pluginGeneration : admitted ! . pluginGeneration ,
413+ } ) ,
414+ ) . rejects . toThrow ( "plugin generation was superseded" ) ;
415+ expect ( getPreparedModelRuntimeSnapshot ( runInput ( committedConfig ) ) ) . toBe ( committedSnapshot ) ;
416+
417+ const next = await acquireAgentRunPreparedModelRuntime ( runInput ( committedConfig ) ) ;
418+ expect ( next . snapshot ) . toBe ( committedSnapshot ) ;
419+ next . release ( ) ;
420+ } ) ;
421+
422+ it ( "does not let a stale pinned generation replace an owner awaiting auth refresh" , async ( ) => {
423+ mocks . configuredAgentIds = [ "default" ] ;
424+ const config = { } ;
425+ await refreshPreparedModelRuntimeSnapshots ( config , { gatewayLifecycle : true } ) ;
426+ const admitted = await loadPublishedGatewayReplyDispatchRuntime ( { agentId : "default" } ) ;
427+ expect ( admitted ) . toBeDefined ( ) ;
428+
429+ mocks . mutationListener ?.( { agentDir : "/tmp/unused-agent" , affectsInheritedStores : false } ) ;
430+
431+ await expect (
432+ acquireAgentRunPreparedModelRuntime (
433+ {
434+ agentId : "default" ,
435+ agentDir : "/tmp/unused-agent" ,
436+ config,
437+ workspaceDir : "/tmp/unused-workspace" ,
438+ } ,
439+ { pluginGeneration : { ...admitted ! . pluginGeneration } } ,
440+ ) ,
441+ ) . rejects . toThrow ( "plugin generation was superseded" ) ;
442+
443+ await vi . waitFor ( async ( ) => {
444+ expect ( mocks . ensureOpenClawModelsJson ) . toHaveBeenCalledTimes ( 2 ) ;
445+ await expect (
446+ loadPublishedGatewayReplyDispatchRuntime ( { agentId : "default" } ) ,
447+ ) . resolves . toMatchObject ( { config } ) ;
448+ } ) ;
302449 } ) ;
303450
304451 it ( "bounds retained gateway run owners while reusing recent selections" , async ( ) => {
0 commit comments