@@ -31,6 +31,7 @@ import {
3131 resolveActiveMemoryBackendConfig ,
3232} from "./memory-runtime.js" ;
3333import { resetStandaloneMemoryRegistrySlot } from "./memory-runtime.test-support.js" ;
34+ import { hasMemoryRuntime } from "./memory-state.js" ;
3435
3536function createRuntime ( ) {
3637 return {
@@ -95,6 +96,73 @@ describe("memory runtime handles", () => {
9596 } ) ;
9697 } ) ;
9798
99+ it ( "tracks standalone managers without activating config-only lookups and rearms reused handles" , async ( ) => {
100+ const { registry, runtime } = createRegistry ( ) ;
101+ mocks . loadPluginRegistryHandle . mockReturnValue ( registry ) ;
102+
103+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
104+ expect ( resolveActiveMemoryBackendConfig ( { cfg : memoryConfig , agentId : "main" } ) ) . toEqual ( {
105+ backend : "builtin" ,
106+ } ) ;
107+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
108+
109+ await getActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "main" } ) ;
110+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
111+
112+ await closeActiveMemorySearchManagers ( ) ;
113+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
114+
115+ await getActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "main" } ) ;
116+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
117+ expect ( mocks . loadPluginRegistryHandle ) . toHaveBeenCalledTimes ( 1 ) ;
118+
119+ await closeActiveMemorySearchManagers ( ) ;
120+ expect ( runtime . closeAllMemorySearchManagers ) . toHaveBeenCalledTimes ( 2 ) ;
121+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
122+ } ) ;
123+
124+ it ( "retains standalone ownership across workspace replacement and per-agent cleanup" , async ( ) => {
125+ const main = createRegistry ( ) ;
126+ const research = createRegistry ( ) ;
127+ mocks . loadPluginRegistryHandle
128+ . mockReturnValueOnce ( main . registry )
129+ . mockReturnValueOnce ( research . registry ) ;
130+
131+ await getActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "main" } ) ;
132+ await getActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "research" } ) ;
133+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
134+
135+ await closeActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "main" } ) ;
136+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
137+
138+ await closeActiveMemorySearchManagers ( ) ;
139+ expect ( main . runtime . closeAllMemorySearchManagers ) . toHaveBeenCalledTimes ( 1 ) ;
140+ expect ( research . runtime . closeAllMemorySearchManagers ) . toHaveBeenCalledTimes ( 1 ) ;
141+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
142+ } ) ;
143+
144+ it ( "retains standalone cleanup ownership when manager acquisition or teardown fails" , async ( ) => {
145+ const { registry, runtime } = createRegistry ( ) ;
146+ mocks . loadPluginRegistryHandle . mockReturnValue ( registry ) ;
147+ runtime . getMemorySearchManager . mockRejectedValueOnce (
148+ new Error ( "manager initialization failed" ) ,
149+ ) ;
150+
151+ await expect (
152+ getActiveMemorySearchManager ( { cfg : memoryConfig , agentId : "main" } ) ,
153+ ) . rejects . toThrow ( "manager initialization failed" ) ;
154+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
155+
156+ runtime . closeAllMemorySearchManagers . mockRejectedValueOnce (
157+ new Error ( "manager teardown failed" ) ,
158+ ) ;
159+ await expect ( closeActiveMemorySearchManagers ( ) ) . rejects . toThrow ( "manager teardown failed" ) ;
160+ expect ( hasMemoryRuntime ( ) ) . toBe ( true ) ;
161+
162+ await closeActiveMemorySearchManagers ( ) ;
163+ expect ( hasMemoryRuntime ( ) ) . toBe ( false ) ;
164+ } ) ;
165+
98166 it ( "keys the single slot by the requesting agent workspace" , ( ) => {
99167 const main = createRegistry ( ) ;
100168 const research = createRegistry ( ) ;
0 commit comments