@@ -9,8 +9,12 @@ import type { ProcessSession } from "./bash-process-registry.js";
99import {
1010 addSession ,
1111 appendOutput ,
12+ createSessionSlug ,
13+ deleteSession ,
1214 drainSession ,
15+ getActiveBackgroundExecSessionCount ,
1316 listFinishedSessions ,
17+ listRunningSessions ,
1418 markBackgrounded ,
1519 markExited ,
1620 resetProcessRegistryForTests ,
@@ -19,6 +23,14 @@ import {
1923} from "./bash-process-registry.js" ;
2024import { createProcessSessionFixture } from "./bash-process-registry.test-helpers.js" ;
2125
26+ const randomMocks = vi . hoisted ( ( ) => ( {
27+ generateSecureInt : vi . fn ( ( ) => 0 ) ,
28+ } ) ) ;
29+
30+ vi . mock ( "../infra/secure-random.js" , ( ) => ( {
31+ generateSecureInt : randomMocks . generateSecureInt ,
32+ } ) ) ;
33+
2234describe ( "bash process registry" , ( ) => {
2335 function createRegistrySession ( params : {
2436 id ?: string ;
@@ -37,6 +49,8 @@ describe("bash process registry", () => {
3749 }
3850
3951 beforeEach ( ( ) => {
52+ randomMocks . generateSecureInt . mockReset ( ) ;
53+ randomMocks . generateSecureInt . mockReturnValue ( 0 ) ;
4054 resetProcessRegistryForTests ( ) ;
4155 } ) ;
4256
@@ -173,6 +187,79 @@ describe("bash process registry", () => {
173187 ] ) ;
174188 } ) ;
175189
190+ it ( "tracks only live backgrounded sessions" , ( ) => {
191+ const session = createRegistrySession ( {
192+ maxOutputChars : 100 ,
193+ pendingMaxOutputChars : 30_000 ,
194+ backgrounded : false ,
195+ } ) ;
196+
197+ addSession ( session ) ;
198+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 0 ) ;
199+
200+ markBackgrounded ( session ) ;
201+ markBackgrounded ( session ) ;
202+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 1 ) ;
203+
204+ markExited ( session , 0 , null , "completed" ) ;
205+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 0 ) ;
206+
207+ markBackgrounded ( session ) ;
208+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 0 ) ;
209+ } ) ;
210+
211+ it ( "keeps a hidden background session active until its process exits" , ( ) => {
212+ const session = createRegistrySession ( {
213+ id : "hidden-until-exit" ,
214+ maxOutputChars : 100 ,
215+ pendingMaxOutputChars : 30_000 ,
216+ backgrounded : false ,
217+ } ) ;
218+
219+ addSession ( session ) ;
220+ markBackgrounded ( session ) ;
221+ deleteSession ( session . id ) ;
222+
223+ expect ( listRunningSessions ( ) ) . toHaveLength ( 0 ) ;
224+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 1 ) ;
225+
226+ markExited ( session , null , "SIGTERM" , "killed" ) ;
227+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 0 ) ;
228+ } ) ;
229+
230+ it ( "keeps a hidden active session id reserved until exit" , ( ) => {
231+ const session = createRegistrySession ( {
232+ id : "amber-atlas" ,
233+ maxOutputChars : 100 ,
234+ pendingMaxOutputChars : 30_000 ,
235+ backgrounded : false ,
236+ } ) ;
237+
238+ addSession ( session ) ;
239+ markBackgrounded ( session ) ;
240+ deleteSession ( session . id ) ;
241+ expect ( createSessionSlug ( ) ) . toBe ( "amber-atlas-2" ) ;
242+
243+ session . backgrounded = false ;
244+ markExited ( session , 0 , null , "completed" ) ;
245+ expect ( createSessionSlug ( ) ) . toBe ( "amber-atlas" ) ;
246+ } ) ;
247+
248+ it ( "clears background activity in the test reset" , ( ) => {
249+ const session = createRegistrySession ( {
250+ maxOutputChars : 100 ,
251+ pendingMaxOutputChars : 30_000 ,
252+ backgrounded : false ,
253+ } ) ;
254+
255+ addSession ( session ) ;
256+ markBackgrounded ( session ) ;
257+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 1 ) ;
258+
259+ resetProcessRegistryForTests ( ) ;
260+ expect ( getActiveBackgroundExecSessionCount ( ) ) . toBe ( 0 ) ;
261+ } ) ;
262+
176263 it ( "clamps a zero retention TTL to one minute" , ( ) => {
177264 vi . useFakeTimers ( ) ;
178265 try {
0 commit comments