@@ -435,12 +435,7 @@ async fn create_session_and_apply_model(
435435 // for the session/new request. Only sent when the agent declares protocol
436436 // version >= 2 (supports systemPrompt); legacy agents ignore it.
437437 let combined_system_prompt: Option < String > = if agent. protocol_version >= 2 {
438- match ( ctx. base_prompt , ctx. system_prompt . as_deref ( ) ) {
439- ( Some ( bp) , Some ( sp) ) => Some ( format ! ( "{}\n \n {sp}" , bp. trim_end( ) ) ) ,
440- ( Some ( bp) , None ) => Some ( bp. trim_end ( ) . to_string ( ) ) ,
441- ( None , Some ( sp) ) => Some ( sp. to_string ( ) ) ,
442- ( None , None ) => None ,
443- }
438+ framed_system_prompt ( ctx. base_prompt , ctx. system_prompt . as_deref ( ) )
444439 } else {
445440 None
446441 } ;
@@ -660,6 +655,26 @@ pub(crate) fn prepend_base_for_legacy(
660655 }
661656}
662657
658+ /// Frame the `session/new` `systemPrompt` so each present prompt carries its own
659+ /// header, keeping the base/persona boundary recoverable downstream.
660+ ///
661+ /// The header framing matches the legacy per-turn path (`queue::base_section`
662+ /// for `[Base]`, `[System]\n{...}` for the persona) so the desktop observer can
663+ /// split the combined value into labeled sub-sections. Each prompt is wrapped
664+ /// only when present, so a persona-only agent yields `[System]\n{persona}`
665+ /// rather than an unlabeled blob that would be mislabeled as `[Base]`.
666+ fn framed_system_prompt ( base_prompt : Option < & str > , system_prompt : Option < & str > ) -> Option < String > {
667+ match ( base_prompt, system_prompt) {
668+ ( Some ( bp) , Some ( sp) ) => Some ( format ! (
669+ "{}\n \n [System]\n {sp}" ,
670+ crate :: queue:: base_section( bp)
671+ ) ) ,
672+ ( Some ( bp) , None ) => Some ( crate :: queue:: base_section ( bp) ) ,
673+ ( None , Some ( sp) ) => Some ( format ! ( "[System]\n {sp}" ) ) ,
674+ ( None , None ) => None ,
675+ }
676+ }
677+
663678/// Core async function spawned for each prompt.
664679///
665680/// Lifecycle:
@@ -2336,6 +2351,36 @@ mod tests {
23362351 assert_eq ! ( composed, "hello channel" ) ;
23372352 }
23382353
2354+ // ── framed_system_prompt tests ───────────────────────────────────────────
2355+ // Pin the session/new systemPrompt framing: each present prompt carries its
2356+ // own header so the desktop observer can split into labeled sub-sections.
2357+
2358+ #[ test]
2359+ fn test_framed_system_prompt_both_present_carries_both_headers ( ) {
2360+ let framed = framed_system_prompt ( Some ( "base text" ) , Some ( "persona text" ) )
2361+ . expect ( "both present yields Some" ) ;
2362+ assert_eq ! ( framed, "[Base]\n base text\n \n [System]\n persona text" ) ;
2363+ }
2364+
2365+ #[ test]
2366+ fn test_framed_system_prompt_base_only_labels_base ( ) {
2367+ let framed = framed_system_prompt ( Some ( "base text" ) , None ) . expect ( "base yields Some" ) ;
2368+ assert_eq ! ( framed, "[Base]\n base text" ) ;
2369+ }
2370+
2371+ #[ test]
2372+ fn test_framed_system_prompt_persona_only_labels_system ( ) {
2373+ // A bare persona would be mislabeled "Base" downstream — it must carry
2374+ // its own [System] header even when no base prompt exists.
2375+ let framed = framed_system_prompt ( None , Some ( "persona text" ) ) . expect ( "persona yields Some" ) ;
2376+ assert_eq ! ( framed, "[System]\n persona text" ) ;
2377+ }
2378+
2379+ #[ test]
2380+ fn test_framed_system_prompt_neither_is_none ( ) {
2381+ assert ! ( framed_system_prompt( None , None ) . is_none( ) ) ;
2382+ }
2383+
23392384 // ── parse_thread_response tests ──────────────────────────────────────────
23402385
23412386 #[ test]
0 commit comments