@@ -15,7 +15,11 @@ vi.mock("./rate-limit-cache.js", () => ({
1515 mergeCodexRateLimitsUpdate : mocks . mergeRateLimitUpdate ,
1616} ) ) ;
1717
18- const { ensureCodexAppServerClientRuntime } = await import ( "./client-runtime.js" ) ;
18+ const {
19+ consumeCodexAppServerLiveThread,
20+ ensureCodexAppServerClientRuntime,
21+ retainCodexAppServerLiveThread,
22+ } = await import ( "./client-runtime.js" ) ;
1923
2024describe ( "Codex app-server client runtime" , ( ) => {
2125 const clients : CodexAppServerClient [ ] = [ ] ;
@@ -99,4 +103,77 @@ describe("Codex app-server client runtime", () => {
99103 } ,
100104 } ) ;
101105 } ) ;
106+
107+ it ( "retains and consumes only one subscribed thread per physical client" , async ( ) => {
108+ const harness = createClientHarness ( ) ;
109+ clients . push ( harness . client ) ;
110+
111+ await expect (
112+ retainCodexAppServerLiveThread ( harness . client , "thread-before-runtime" ) ,
113+ ) . resolves . toBeUndefined ( ) ;
114+ ensureCodexAppServerClientRuntime ( harness . client , { agentDir : "/tmp/agent" } ) ;
115+
116+ await expect ( retainCodexAppServerLiveThread ( harness . client , "thread-1" ) ) . resolves . toEqual ( { } ) ;
117+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toBe ( false ) ;
118+ await expect ( retainCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toEqual ( {
119+ previousThreadId : "thread-1" ,
120+ } ) ;
121+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-1" ) ) . resolves . toBe ( false ) ;
122+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toBe ( true ) ;
123+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toBe ( false ) ;
124+ } ) ;
125+
126+ it ( "waits for the old subscription to release before another thread can acquire it" , async ( ) => {
127+ const harness = createClientHarness ( ) ;
128+ clients . push ( harness . client ) ;
129+ ensureCodexAppServerClientRuntime ( harness . client , { agentDir : "/tmp/agent" } ) ;
130+ await retainCodexAppServerLiveThread ( harness . client , "thread-1" ) ;
131+
132+ let finishRelease : ( ( ) => void ) | undefined ;
133+ const previousRelease = new Promise < void > ( ( resolve ) => {
134+ finishRelease = resolve ;
135+ } ) ;
136+ const transition = retainCodexAppServerLiveThread (
137+ harness . client ,
138+ "thread-2" ,
139+ async ( ) => previousRelease ,
140+ ) ;
141+ const oldThreadAcquisition = consumeCodexAppServerLiveThread ( harness . client , "thread-1" ) ;
142+
143+ finishRelease ?.( ) ;
144+ await expect ( transition ) . resolves . toEqual ( { previousThreadId : "thread-1" } ) ;
145+ await expect ( oldThreadAcquisition ) . resolves . toBe ( false ) ;
146+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toBe ( true ) ;
147+ } ) ;
148+
149+ it ( "does not expose either thread after a previous subscription release fails" , async ( ) => {
150+ const harness = createClientHarness ( ) ;
151+ clients . push ( harness . client ) ;
152+ ensureCodexAppServerClientRuntime ( harness . client , { agentDir : "/tmp/agent" } ) ;
153+ await retainCodexAppServerLiveThread ( harness . client , "thread-1" ) ;
154+
155+ await expect (
156+ retainCodexAppServerLiveThread ( harness . client , "thread-2" , async ( ) => {
157+ throw new Error ( "unsubscribe unavailable" ) ;
158+ } ) ,
159+ ) . rejects . toThrow ( "unsubscribe unavailable" ) ;
160+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-1" ) ) . resolves . toBe ( false ) ;
161+ await expect ( consumeCodexAppServerLiveThread ( harness . client , "thread-2" ) ) . resolves . toBe ( false ) ;
162+ } ) ;
163+
164+ it ( "reuses a retained subscription only for its complete configuration fingerprint" , async ( ) => {
165+ const harness = createClientHarness ( ) ;
166+ clients . push ( harness . client ) ;
167+ ensureCodexAppServerClientRuntime ( harness . client , { agentDir : "/tmp/agent" } ) ;
168+
169+ await expect (
170+ retainCodexAppServerLiveThread ( harness . client , "thread-1" , undefined , "config-before" ) ,
171+ ) . resolves . toEqual ( { } ) ;
172+ await expect (
173+ consumeCodexAppServerLiveThread ( harness . client , "thread-1" , "config-after" ) ,
174+ ) . resolves . toBe ( false ) ;
175+ await expect (
176+ consumeCodexAppServerLiveThread ( harness . client , "thread-1" , "config-before" ) ,
177+ ) . resolves . toBe ( true ) ;
178+ } ) ;
102179} ) ;
0 commit comments