@@ -31,7 +31,7 @@ use pool::{
3131 AgentPool , ControlSignal , OwnedAgent , PromptContext , PromptOutcome , PromptResult , PromptSource ,
3232 SessionState ,
3333} ;
34- use queue:: { prepend_base_prompt , EventQueue , QueuedEvent , ThreadTags } ;
34+ use queue:: { EventQueue , QueuedEvent , ThreadTags } ;
3535use relay:: { HarnessRelay , RelayEventPublisher } ;
3636use tokio:: sync:: { mpsc, watch} ;
3737use tracing_subscriber:: EnvFilter ;
@@ -695,7 +695,7 @@ fn any_respawn_in_flight(crash_history: &[SlotCircuit]) -> bool {
695695/// Result of a background respawn task.
696696struct RespawnResult {
697697 index : usize ,
698- result : Result < AcpClient > ,
698+ result : Result < ( AcpClient , u32 ) > ,
699699}
700700
701701/// RAII guard that ensures a `RespawnResult` is sent even if the task panics.
@@ -719,7 +719,7 @@ impl RespawnGuard {
719719 /// Send the result and disarm the guard. Uses `try_send` (sync) so there
720720 /// is no await boundary between marking `sent` and actually enqueueing —
721721 /// cancellation cannot slip between the two.
722- fn send ( mut self , result : Result < AcpClient > ) {
722+ fn send ( mut self , result : Result < ( AcpClient , u32 ) > ) {
723723 // Invariant: try_send succeeds because the channel capacity equals the
724724 // slot count, and respawn_in_flight guarantees at most one outstanding
725725 // result per slot. If this ever fails, the channel sizing or the
@@ -839,6 +839,8 @@ async fn tokio_main() -> Result<()> {
839839 match tokio:: time:: timeout ( Duration :: from_secs ( 60 ) , acp. initialize ( ) ) . await {
840840 Ok ( Ok ( init_result) ) => {
841841 tracing:: info!( agent = i, "agent initialized: {init_result}" ) ;
842+ let protocol_version =
843+ init_result[ "protocolVersion" ] . as_u64 ( ) . unwrap_or ( 1 ) as u32 ;
842844 acp. observe (
843845 "agent_initialized" ,
844846 serde_json:: json!( {
@@ -852,6 +854,7 @@ async fn tokio_main() -> Result<()> {
852854 state : SessionState :: default ( ) ,
853855 model_capabilities : None ,
854856 desired_model : config. model . clone ( ) ,
857+ protocol_version,
855858 } ) ) ;
856859 }
857860 Ok ( Err ( e) ) => {
@@ -1279,13 +1282,14 @@ async fn tokio_main() -> Result<()> {
12791282 while let Ok ( rr) = respawn_rx. try_recv ( ) {
12801283 crash_history[ rr. index ] . respawn_in_flight = false ;
12811284 match rr. result {
1282- Ok ( acp) => {
1285+ Ok ( ( acp, protocol_version ) ) => {
12831286 let agent = OwnedAgent {
12841287 index : rr. index ,
12851288 acp,
12861289 state : SessionState :: default ( ) ,
12871290 model_capabilities : None ,
12881291 desired_model : config. model . clone ( ) ,
1292+ protocol_version,
12891293 } ;
12901294 pool. return_agent ( agent) ;
12911295 tracing:: info!( agent = rr. index, "respawn complete" ) ;
@@ -1887,7 +1891,7 @@ async fn tokio_main() -> Result<()> {
18871891 // Drain any respawn results that completed before the abort. Explicitly
18881892 // shut down returned agents instead of relying on AcpClient::Drop.
18891893 while let Ok ( rr) = respawn_rx. try_recv ( ) {
1890- if let Ok ( mut acp) = rr. result {
1894+ if let Ok ( ( mut acp, _ ) ) = rr. result {
18911895 acp. shutdown ( ) . await ;
18921896 tracing:: debug!( agent = rr. index, "reaped respawned agent on shutdown" ) ;
18931897 }
@@ -2429,10 +2433,10 @@ fn dispatch_heartbeat(
24292433 . heartbeat_prompt
24302434 . clone ( )
24312435 . unwrap_or_else ( default_heartbeat_prompt) ;
2432- let prompt_text = match ctx . base_prompt {
2433- Some ( bp ) => prepend_base_prompt ( bp , & prompt_text ) ,
2434- None => prompt_text ,
2435- } ;
2436+ // For legacy agents (protocol_version < 2), prepend base_prompt to the
2437+ // heartbeat user message since they don't receive it via session/new.
2438+ let prompt_text =
2439+ pool :: prepend_base_for_legacy ( agent . protocol_version , ctx . base_prompt , & prompt_text ) ;
24362440 let result_tx = pool. result_tx ( ) ;
24372441 let ctx_clone = Arc :: clone ( ctx) ;
24382442 let agent_index = agent. index ;
@@ -2546,7 +2550,7 @@ async fn spawn_and_init(
25462550 extra_env : & [ ( String , String ) ] ,
25472551 agent_index : usize ,
25482552 observer : Option < observer:: ObserverHandle > ,
2549- ) -> Result < AcpClient > {
2553+ ) -> Result < ( AcpClient , u32 ) > {
25502554 let mut acp = AcpClient :: spawn ( command, args, extra_env)
25512555 . await
25522556 . map_err ( |e| anyhow:: anyhow!( "failed to spawn agent: {e}" ) ) ?;
@@ -2555,14 +2559,15 @@ async fn spawn_and_init(
25552559 match acp. initialize ( ) . await {
25562560 Ok ( init_result) => {
25572561 tracing:: info!( "agent initialized: {init_result}" ) ;
2562+ let protocol_version = init_result[ "protocolVersion" ] . as_u64 ( ) . unwrap_or ( 1 ) as u32 ;
25582563 acp. observe (
25592564 "agent_initialized" ,
25602565 serde_json:: json!( {
25612566 "agentIndex" : agent_index,
25622567 "initializeResult" : init_result,
25632568 } ) ,
25642569 ) ;
2565- Ok ( acp)
2570+ Ok ( ( acp, protocol_version ) )
25662571 }
25672572 Err ( e) => {
25682573 // Explicitly shut down the spawned child to prevent zombie/leak.
@@ -2605,7 +2610,7 @@ async fn run_models(args: ModelsArgs) -> Result<()> {
26052610 // so shutdown() runs on all paths (success, error, timeout).
26062611 let protocol_result = tokio:: time:: timeout ( MODELS_TIMEOUT , async {
26072612 let init = client. initialize ( ) . await ?;
2608- let session = client. session_new_full ( & cwd, vec ! [ ] ) . await ?;
2613+ let session = client. session_new_full ( & cwd, vec ! [ ] , None ) . await ?;
26092614 Ok :: < _ , acp:: AcpError > ( ( init, session) )
26102615 } )
26112616 . await ;
@@ -2764,6 +2769,38 @@ fn build_mcp_servers(config: &Config) -> Vec<McpServer> {
27642769
27652770// ── Tests ─────────────────────────────────────────────────────────────────────
27662771
2772+ #[ cfg( test) ]
2773+ mod heartbeat_base_prompt_tests {
2774+ use super :: * ;
2775+
2776+ // Pins the heartbeat dispatch path (dispatch_heartbeat, ~line 2359): a
2777+ // legacy agent WITH a base_prompt must get [Base] prepended to the
2778+ // heartbeat user message, composed as `[Base]\n{bp}\n\n{prompt}`. This is
2779+ // the second half of the round-2 regression (the first being initial_message).
2780+
2781+ #[ test]
2782+ fn test_heartbeat_legacy_agent_gets_base_prepended ( ) {
2783+ // protocol_version 1 + Some(base_prompt): heartbeat prompt is prefixed
2784+ // with the [Base] section exactly as the legacy session/new path would.
2785+ let prompt = "[System: Heartbeat]\n run feed get" ;
2786+ let composed = pool:: prepend_base_for_legacy ( 1 , Some ( "you are a helpful agent" ) , prompt) ;
2787+ assert_eq ! (
2788+ composed,
2789+ "[Base]\n you are a helpful agent\n \n [System: Heartbeat]\n run feed get"
2790+ ) ;
2791+ assert ! ( composed. starts_with( "[Base]\n you are a helpful agent\n \n " ) ) ;
2792+ }
2793+
2794+ #[ test]
2795+ fn test_heartbeat_modern_agent_omits_base ( ) {
2796+ // protocol_version 2 gets base_prompt via session/new; the heartbeat
2797+ // prompt is sent verbatim.
2798+ let prompt = "[System: Heartbeat]\n run feed get" ;
2799+ let composed = pool:: prepend_base_for_legacy ( 2 , Some ( "you are a helpful agent" ) , prompt) ;
2800+ assert_eq ! ( composed, prompt) ;
2801+ }
2802+ }
2803+
27672804#[ cfg( test) ]
27682805mod owner_control_command_tests {
27692806 use super :: * ;
0 commit comments