@@ -29,6 +29,14 @@ const MAX_LINE_SIZE: usize = 10_000_000; // 10 MB
2929/// Package and binary name used by Buzz's Pi ACP fork.
3030pub ( crate ) const BUZZ_PI_ACP_NAME : & str = "buzz-pi-acp" ;
3131
32+ /// `antigravity-acp` does not advertise session close support. Bound its
33+ /// successful `session/new` lifetime with a worker recycle so resources owned
34+ /// by that adapter cannot grow without a process-level cleanup.
35+ const ANTIGRAVITY_ACP_NAME : & str = "antigravity-acp" ;
36+ /// Leaves headroom below the observed 60 retained sessions at a 256-FD soft
37+ /// limit while bounding one ACP child process to a small, fixed lifetime.
38+ pub ( crate ) const ANTIGRAVITY_ACP_SESSION_RECYCLE_LIMIT : usize = 16 ;
39+
3240/// An MCP server configuration passed to `session/new`.
3341///
3442/// Corresponds to the `McpServerStdio` variant in the ACP schema.
@@ -221,6 +229,13 @@ pub struct AcpClient {
221229 standard_usage : StandardUsageTracker ,
222230 /// Known adapter identity for prompt-response usage mapping.
223231 standard_adapter : Option < StandardAdapterKind > ,
232+ /// Session count at which this process must be replaced after a completed
233+ /// prompt. Set only for adapters with known retained-session resources.
234+ session_recycle_limit : Option < usize > ,
235+ /// Successful `session/new` calls made by this ACP process. This lives on
236+ /// the client rather than SessionState so session invalidation cannot reset
237+ /// the resource bound.
238+ successful_session_creations : usize ,
224239}
225240
226241/// Recursively merge `overlay` into `base`, with `overlay` winning on scalar/shape
@@ -623,6 +638,8 @@ impl AcpClient {
623638 goose_usage : UsageTracker :: default ( ) ,
624639 standard_usage : StandardUsageTracker :: default ( ) ,
625640 standard_adapter,
641+ session_recycle_limit : None ,
642+ successful_session_creations : 0 ,
626643 } )
627644 }
628645
@@ -677,6 +694,13 @@ impl AcpClient {
677694 . pointer ( "/_meta/steering/supported" )
678695 . and_then ( |v| v. as_bool ( ) )
679696 . unwrap_or ( false ) ;
697+ self . session_recycle_limit = result
698+ . get ( "agentInfo" )
699+ . or_else ( || result. get ( "serverInfo" ) )
700+ . and_then ( |info| info. get ( "name" ) )
701+ . and_then ( |name| name. as_str ( ) )
702+ . filter ( |name| name. trim ( ) == ANTIGRAVITY_ACP_NAME )
703+ . map ( |_| ANTIGRAVITY_ACP_SESSION_RECYCLE_LIMIT ) ;
680704 tracing:: debug!( target: "acp::init" , "initialize response: {result}" ) ;
681705 Ok ( result)
682706 }
@@ -743,13 +767,24 @@ impl AcpClient {
743767 . as_str ( )
744768 . ok_or_else ( || AcpError :: Protocol ( "session/new response missing sessionId" . into ( ) ) ) ?
745769 . to_owned ( ) ;
770+ if self . session_recycle_limit . is_some ( ) {
771+ self . successful_session_creations = self . successful_session_creations . saturating_add ( 1 ) ;
772+ }
746773 tracing:: info!( target: "acp::session" , "session created: {session_id}" ) ;
747774 Ok ( SessionNewResponse {
748775 session_id,
749776 raw : result,
750777 } )
751778 }
752779
780+ /// Whether this reusable worker reached its adapter-specific session bound
781+ /// and is idle enough to replace safely.
782+ pub ( crate ) fn should_recycle_after_completed_prompt ( & self ) -> bool {
783+ self . session_recycle_limit
784+ . is_some_and ( |limit| self . successful_session_creations >= limit)
785+ && !self . has_in_flight_prompt ( )
786+ }
787+
753788 /// Send `session/new` and return only the `sessionId` string.
754789 ///
755790 /// Convenience wrapper around [`session_new_full`].
@@ -3094,6 +3129,34 @@ mod tests {
30943129 . expect ( "failed to spawn test script" )
30953130 }
30963131
3132+ async fn initialized_session_counter ( name : & str ) -> AcpClient {
3133+ let script = format ! (
3134+ r#"
3135+ read -r _init
3136+ echo '{{"jsonrpc":"2.0","id":0,"result":{{"protocolVersion":1,"agentInfo":{{"name":"{name}"}}}}}}'
3137+ session=0
3138+ while read -r request; do
3139+ id=$(printf '%s' "$request" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')
3140+ printf '{{"jsonrpc":"2.0","id":%s,"result":{{"sessionId":"session-%s"}}}}\n' "$id" "$session"
3141+ session=$((session + 1))
3142+ done
3143+ "#
3144+ ) ;
3145+ let mut client = spawn_script ( & script) . await ;
3146+ client
3147+ . initialize ( )
3148+ . await
3149+ . expect ( "test adapter initialize should succeed" ) ;
3150+ client
3151+ }
3152+
3153+ async fn create_test_session ( client : & mut AcpClient ) {
3154+ client
3155+ . session_new_full ( "/tmp" , vec ! [ ] , None , None )
3156+ . await
3157+ . expect ( "test session/new should succeed" ) ;
3158+ }
3159+
30973160 #[ cfg( unix) ]
30983161 async fn spawn_named_script ( name : & str , script : & str ) -> ( AcpClient , std:: path:: PathBuf ) {
30993162 use std:: os:: unix:: fs:: PermissionsExt ;
@@ -3555,6 +3618,48 @@ mod tests {
35553618 ) ;
35563619 }
35573620
3621+ #[ tokio:: test]
3622+ async fn session_resource_antigravity_recycles_only_after_16_completed_sessions ( ) {
3623+ let mut client = initialized_session_counter ( ANTIGRAVITY_ACP_NAME ) . await ;
3624+
3625+ for _ in 0 ..( ANTIGRAVITY_ACP_SESSION_RECYCLE_LIMIT - 1 ) {
3626+ create_test_session ( & mut client) . await ;
3627+ assert ! (
3628+ !client. should_recycle_after_completed_prompt( ) ,
3629+ "the worker must remain usable below its session limit"
3630+ ) ;
3631+ }
3632+
3633+ create_test_session ( & mut client) . await ;
3634+ assert ! (
3635+ client. should_recycle_after_completed_prompt( ) ,
3636+ "the worker must recycle after its sixteenth successful session/new"
3637+ ) ;
3638+
3639+ client. last_prompt_id = Some ( 99 ) ;
3640+ assert ! (
3641+ !client. should_recycle_after_completed_prompt( ) ,
3642+ "a recycle must never terminate an active prompt"
3643+ ) ;
3644+ client. last_prompt_id = None ;
3645+ client. shutdown ( ) . await ;
3646+ }
3647+
3648+ #[ tokio:: test]
3649+ async fn session_resource_recycle_limit_does_not_apply_to_other_adapters ( ) {
3650+ let mut client = initialized_session_counter ( "another-acp-adapter" ) . await ;
3651+
3652+ for _ in 0 ..ANTIGRAVITY_ACP_SESSION_RECYCLE_LIMIT {
3653+ create_test_session ( & mut client) . await ;
3654+ }
3655+
3656+ assert ! (
3657+ !client. should_recycle_after_completed_prompt( ) ,
3658+ "only the named adapter receives the bounded recycle policy"
3659+ ) ;
3660+ client. shutdown ( ) . await ;
3661+ }
3662+
35583663 #[ tokio:: test]
35593664 async fn goose_system_prompt_request_uses_set_contract ( ) {
35603665 let script = r#"
0 commit comments