@@ -348,6 +348,47 @@ pub fn divergent_agent_command_override(
348348 }
349349}
350350
351+ /// Decide the `agent_command_override` to persist at AGENT UPDATE time.
352+ ///
353+ /// The edit dialog sends `agent_command` as a tri-state string: the empty
354+ /// "inherit from persona" sentinel (clear the pin), or a concrete command
355+ /// (pin). Resolution:
356+ ///
357+ /// - EMPTY / whitespace → the inherit sentinel: always `None` regardless of
358+ /// `harness_override`, so toggling "Inherit runtime from persona" clears the
359+ /// pin.
360+ /// - DELIBERATE OVERRIDE (`harness_override` true, persona linked): the user
361+ /// explicitly picked a runtime/Custom command in the dialog. This is a real
362+ /// pin and is preserved VERBATIM — even when the picked command maps to, or
363+ /// is byte-identical to, the persona's own runtime command. Selecting "Custom
364+ /// command" and saving e.g. `goose` for a goose persona is a deliberate act
365+ /// to freeze the harness against future persona runtime edits; dropping it
366+ /// back to inherit (as [`divergent_agent_command_override`] would) defeats
367+ /// that intent. Unlike the create-time path, there is no byte-identical
368+ /// exception here: at create the command is machine-derived from the persona,
369+ /// so equality means "no user divergence"; at update an equal command reached
370+ /// the force branch only because the user picked Custom, which IS the
371+ /// divergence.
372+ /// - NO OVERRIDE INTENT (`harness_override` false) or NO PERSONA: defer to
373+ /// [`divergent_agent_command_override`], which keeps the persona authoritative
374+ /// and treats a same-runtime restatement as inherit.
375+ pub fn update_time_agent_command_override (
376+ persona_id : Option < & str > ,
377+ personas : & [ crate :: managed_agents:: types:: PersonaRecord ] ,
378+ picked_command : Option < & str > ,
379+ harness_override : bool ,
380+ ) -> Option < String > {
381+ let picked = picked_command
382+ . map ( str:: trim)
383+ . filter ( |value| !value. is_empty ( ) ) ?;
384+
385+ if persona_id. is_some ( ) && harness_override {
386+ return Some ( picked. to_string ( ) ) ;
387+ }
388+
389+ divergent_agent_command_override ( persona_id, personas, Some ( picked) )
390+ }
391+
351392/// Decide the `agent_command_override` to persist at AGENT CREATE time.
352393///
353394/// A persona-backed create receives its harness command from
@@ -729,8 +770,8 @@ mod tests {
729770 use super :: {
730771 classify_runtime, create_time_agent_command_override, default_agent_command,
731772 divergent_agent_command_override, effective_agent_command, find_via_login_shell,
732- managed_agent_avatar_url, normalize_agent_args, BUZZ_AGENT_AVATAR_URL ,
733- CLAUDE_CODE_AVATAR_URL , CODEX_AVATAR_URL , GOOSE_AVATAR_URL ,
773+ managed_agent_avatar_url, normalize_agent_args, update_time_agent_command_override ,
774+ BUZZ_AGENT_AVATAR_URL , CLAUDE_CODE_AVATAR_URL , CODEX_AVATAR_URL , GOOSE_AVATAR_URL ,
734775 } ;
735776 use crate :: managed_agents:: AcpAvailabilityStatus ;
736777
@@ -1120,4 +1161,98 @@ mod tests {
11201161 Some ( "codex-acp" . to_string( ) )
11211162 ) ;
11221163 }
1164+
1165+ #[ test]
1166+ fn update_time_override_preserves_same_runtime_pin_when_overriding ( ) {
1167+ // The bug this fixes: the user picks "Custom command" in the edit
1168+ // dialog and saves `goose` verbatim for a goose persona. That is a
1169+ // deliberate pin (harness_override true) — it must be kept so future
1170+ // persona runtime edits stop propagating, even though it maps to the
1171+ // persona's own runtime. `divergent_agent_command_override` alone would
1172+ // wrongly drop it to `None`.
1173+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "goose" ) ) ] ;
1174+ assert_eq ! (
1175+ update_time_agent_command_override( Some ( "p1" ) , & personas, Some ( "goose" ) , true ) ,
1176+ Some ( "goose" . to_string( ) )
1177+ ) ;
1178+ }
1179+
1180+ #[ test]
1181+ fn update_time_override_preserves_exact_persona_command_when_overriding ( ) {
1182+ // Even when the pick is byte-identical to the persona's own command, an
1183+ // explicit Custom selection (harness_override true) is a deliberate pin
1184+ // and is preserved. This is the core divergence from the create-time
1185+ // contract: at update, equality reached the force branch only because
1186+ // the user picked Custom.
1187+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "claude" ) ) ] ;
1188+ assert_eq ! (
1189+ update_time_agent_command_override(
1190+ Some ( "p1" ) ,
1191+ & personas,
1192+ Some ( "claude-agent-acp" ) ,
1193+ true
1194+ ) ,
1195+ Some ( "claude-agent-acp" . to_string( ) )
1196+ ) ;
1197+ }
1198+
1199+ #[ test]
1200+ fn update_time_override_preserves_alias_pin_when_overriding ( ) {
1201+ // A `claude` persona with an installed `claude-code-acp` alias: picking
1202+ // it as a Custom pin is a deliberate divergence from the primary
1203+ // command and must be preserved when overriding.
1204+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "claude" ) ) ] ;
1205+ assert_eq ! (
1206+ update_time_agent_command_override(
1207+ Some ( "p1" ) ,
1208+ & personas,
1209+ Some ( "claude-code-acp" ) ,
1210+ true
1211+ ) ,
1212+ Some ( "claude-code-acp" . to_string( ) )
1213+ ) ;
1214+ }
1215+
1216+ #[ test]
1217+ fn update_time_override_defers_to_divergent_when_not_overriding ( ) {
1218+ // Without the explicit intent bit (e.g. a name-only edit that still
1219+ // echoes the command), the persona stays authoritative: a same-runtime
1220+ // command inherits, a different runtime pins.
1221+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "goose" ) ) ] ;
1222+ assert_eq ! (
1223+ update_time_agent_command_override( Some ( "p1" ) , & personas, Some ( "goose" ) , false ) ,
1224+ None
1225+ ) ;
1226+ assert_eq ! (
1227+ update_time_agent_command_override( Some ( "p1" ) , & personas, Some ( "codex-acp" ) , false ) ,
1228+ Some ( "codex-acp" . to_string( ) )
1229+ ) ;
1230+ }
1231+
1232+ #[ test]
1233+ fn update_time_override_clears_pin_for_inherit_sentinel ( ) {
1234+ // The empty "Inherit from persona" sentinel always clears the pin,
1235+ // regardless of the override flag.
1236+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "goose" ) ) ] ;
1237+ assert_eq ! (
1238+ update_time_agent_command_override( Some ( "p1" ) , & personas, Some ( " " ) , true ) ,
1239+ None
1240+ ) ;
1241+ assert_eq ! (
1242+ update_time_agent_command_override( Some ( "p1" ) , & personas, None , true ) ,
1243+ None
1244+ ) ;
1245+ }
1246+
1247+ #[ test]
1248+ fn update_time_override_preserves_pin_for_persona_less_agent ( ) {
1249+ // A persona-less agent has no runtime to inherit, so any picked command
1250+ // is a real pin — preserved even without the override flag (mirrors the
1251+ // create-time persona-less contract).
1252+ let personas = vec ! [ persona_with_runtime( "p1" , Some ( "goose" ) ) ] ;
1253+ assert_eq ! (
1254+ update_time_agent_command_override( None , & personas, Some ( "codex-acp" ) , false ) ,
1255+ Some ( "codex-acp" . to_string( ) )
1256+ ) ;
1257+ }
11231258}
0 commit comments