@@ -12,8 +12,9 @@ use crate::{
1212 save_managed_agents, start_managed_agent_process, stop_managed_agent_process,
1313 stop_managed_agent_workspace_pair, sync_managed_agent_processes, try_regenerate_nest,
1414 validate_provider_config, BackendKind , CreateManagedAgentRequest ,
15- CreateManagedAgentResponse , ManagedAgentRecord , ManagedAgentSummary , RelayMeshConfig ,
16- DEFAULT_ACP_COMMAND , DEFAULT_AGENT_PARALLELISM , DEFAULT_AGENT_TURN_TIMEOUT_SECONDS ,
15+ CreateManagedAgentResponse , ManagedAgentRecord , ManagedAgentSummary , ProviderFailure ,
16+ RelayMeshConfig , DEFAULT_ACP_COMMAND , DEFAULT_AGENT_PARALLELISM ,
17+ DEFAULT_AGENT_TURN_TIMEOUT_SECONDS ,
1718 } ,
1819 relay:: relay_ws_url_with_override,
1920 util:: now_iso,
@@ -332,6 +333,7 @@ where
332333}
333334
334335pub ( crate ) use provider_deploy:: deploy_to_provider;
336+ use provider_deploy:: { InvocationScope , ProviderAction } ;
335337
336338// Async so the blocking body (disk reads of agent/persona records, per-agent
337339// process-liveness syscalls, and a possible save) runs on Tauri's worker pool
@@ -829,8 +831,10 @@ async fn create_managed_agent_in<R: tauri::Runtime>(
829831 && input. backend != BackendKind :: Local
830832 && !input. backend . is_thread_sandbox ( )
831833 {
832- if let BackendKind :: Provider { ref id, ref config } = input. backend {
833- let agent_json = {
834+ if let BackendKind :: Provider { .. } = input. backend {
835+ {
836+ // Fail create on a payload that cannot be built; deploy
837+ // rebuilds it after its lock.
834838 let _g = state
835839 . managed_agents_store_lock
836840 . lock ( )
@@ -840,15 +844,11 @@ async fn create_managed_agent_in<R: tauri::Runtime>(
840844 . iter ( )
841845 . find ( |r| r. pubkey == pubkey)
842846 . ok_or_else ( || "agent disappeared" . to_string ( ) ) ?;
843- build_deploy_payload ( & app, state, rec) ?
844- } ;
845- match deploy_to_provider (
846- & app, state, & pubkey, id, config, agent_json, None , None , None , None , None ,
847- )
848- . await
849- {
847+ build_deploy_payload ( & app, state, rec) ?;
848+ }
849+ match deploy_to_provider ( & app, state, & pubkey, & Default :: default ( ) , None ) . await {
850850 Ok ( ( ) ) => spawn_error,
851- Err ( e) => Some ( e) ,
851+ Err ( e) => Some ( e. message ) ,
852852 }
853853 } else {
854854 spawn_error
@@ -895,7 +895,7 @@ pub async fn start_managed_agent(
895895 session_scope : Option < provider_deploy:: RemoteSessionScope > ,
896896 app : AppHandle ,
897897 state : State < ' _ , AppState > ,
898- ) -> Result < ManagedAgentSummary , String > {
898+ ) -> Result < ManagedAgentSummary , ProviderFailure > {
899899 // Snapshot the workspace owner pubkey for the legacy auth_tag fallback.
900900 // Read outside the records lock to keep lock ordering simple.
901901 let owner_hex = workspace_owner_hex ( & state) ?;
@@ -926,11 +926,7 @@ pub async fn start_managed_agent(
926926 ) ?;
927927 enum StartTarget {
928928 Local ,
929- Provider {
930- backend : BackendKind ,
931- cached_binary_path : Option < String > ,
932- agent_json : serde_json:: Value ,
933- } ,
929+ Provider { backend : BackendKind } ,
934930 }
935931
936932 // Collect backend info under lock; async preflight/spawn happens below.
@@ -973,33 +969,31 @@ pub async fn start_managed_agent(
973969 let target = if record. backend == BackendKind :: Local {
974970 StartTarget :: Local
975971 } else {
972+ // Fail before the deploy lock on a payload that cannot be
973+ // built; deploy rebuilds it after the lock.
974+ build_deploy_payload ( & app, & state, record) ?;
976975 StartTarget :: Provider {
977976 backend : record. backend . clone ( ) ,
978- cached_binary_path : record. provider_binary_path . clone ( ) ,
979- agent_json : build_deploy_payload ( & app, & state, record) ?,
980977 }
981978 } ;
982979
983980 ( target, reconcile)
984981 } ;
985982
986983 let result = match target {
987- StartTarget :: Local => {
988- start_local_agent_with_preflight (
989- & app,
990- & state,
991- & pubkey,
992- false ,
993- expected_relay_url. as_deref ( ) ,
994- expected_signer_pubkey. as_deref ( ) ,
995- replay_floor_unix,
996- )
997- . await
998- }
984+ StartTarget :: Local => start_local_agent_with_preflight (
985+ & app,
986+ & state,
987+ & pubkey,
988+ false ,
989+ expected_relay_url. as_deref ( ) ,
990+ expected_signer_pubkey. as_deref ( ) ,
991+ replay_floor_unix,
992+ )
993+ . await
994+ . map_err ( ProviderFailure :: from) ,
999995 StartTarget :: Provider {
1000- backend : BackendKind :: Provider { id, config } ,
1001- cached_binary_path,
1002- agent_json,
996+ backend : BackendKind :: Provider { .. } ,
1003997 } => {
1004998 // The caller's captured scope is asserted INSIDE deploy_to_provider
1005999 // against the payload rebuilt after the deploy lock — the exact
@@ -1008,20 +1002,13 @@ pub async fn start_managed_agent(
10081002 // The replay floor rides along so a publish-first mention send's
10091003 // remote harness replays past the already-published message, same
10101004 // as the local spawn path.
1011- deploy_to_provider (
1012- & app,
1013- & state,
1014- & pubkey,
1015- & id,
1016- & config,
1017- agent_json,
1018- cached_binary_path. as_deref ( ) ,
1019- expected_relay_url. as_deref ( ) ,
1020- expected_signer_pubkey. as_deref ( ) ,
1021- replay_floor_unix,
1022- session_scope. as_ref ( ) ,
1023- )
1024- . await ?;
1005+ let scope = InvocationScope {
1006+ expected_relay_url : expected_relay_url. as_deref ( ) ,
1007+ expected_signer_pubkey : expected_signer_pubkey. as_deref ( ) ,
1008+ session : session_scope. as_ref ( ) ,
1009+ action : ProviderAction :: Deploy ,
1010+ } ;
1011+ deploy_to_provider ( & app, & state, & pubkey, & scope, replay_floor_unix) . await ?;
10251012
10261013 // Return updated summary.
10271014 let _store_guard = state
@@ -1037,11 +1024,11 @@ pub async fn start_managed_agent(
10371024 . iter ( )
10381025 . find ( |r| r. pubkey == pubkey)
10391026 . ok_or_else ( || format ! ( "agent {pubkey} not found" ) ) ?;
1040- summarize_from_disk ( & app, record, & runtimes)
1027+ Ok ( summarize_from_disk ( & app, record, & runtimes) ?)
1028+ }
1029+ StartTarget :: Provider { backend } => {
1030+ Err ( format ! ( "agent {pubkey} has unsupported backend kind: {backend:?}" ) . into ( ) )
10411031 }
1042- StartTarget :: Provider { backend, .. } => Err ( format ! (
1043- "agent {pubkey} has unsupported backend kind: {backend:?}"
1044- ) ) ,
10451032 } ;
10461033
10471034 // ── Profile reconciliation (fire-and-forget) ────────────────────────────
@@ -1229,11 +1216,15 @@ pub async fn delete_managed_agent(
12291216mod deploy;
12301217pub ( super ) mod provider_access;
12311218mod provider_deploy;
1219+ mod remote_session;
12321220pub ( super ) use deploy:: build_deploy_payload;
12331221#[ cfg( test) ]
12341222use deploy:: { deploy_payload_json, DeployProjections } ;
12351223#[ cfg( test) ]
12361224use deploy:: { ensure_remote_provider_supported, resolve_deploy_model_provider} ;
1225+ pub use remote_session:: {
1226+ end_remote_agent_session, recover_remote_agent_session, remote_agent_session_supports_lifecycle,
1227+ } ;
12371228
12381229#[ path = "agents_profile.rs" ]
12391230mod profile;
0 commit comments