@@ -13,6 +13,45 @@ use crate::{
1313
1414use super :: build_deploy_payload;
1515
16+ #[ derive( serde:: Deserialize ) ]
17+ #[ serde( rename_all = "camelCase" , deny_unknown_fields) ]
18+ /// Published channel and canonical thread that own a remote workspace.
19+ pub struct RemoteSessionScope {
20+ channel_id : uuid:: Uuid ,
21+ thread_root : String ,
22+ }
23+
24+ fn bind_sandbox_scope (
25+ config : & mut serde_json:: Value ,
26+ scope : Option < & RemoteSessionScope > ,
27+ agent_pubkey : & str ,
28+ payload : & serde_json:: Value ,
29+ ) -> Result < ( ) , String > {
30+ if config. get ( "sandbox" ) != Some ( & serde_json:: Value :: Bool ( true ) ) {
31+ return Ok ( ( ) ) ;
32+ }
33+ let scope = scope. ok_or ( "Mention this agent in a channel thread to start its workspace" ) ?;
34+ if scope. thread_root . len ( ) != 64
35+ || !scope
36+ . thread_root
37+ . bytes ( )
38+ . all ( |b| b. is_ascii_hexdigit ( ) && !b. is_ascii_uppercase ( ) )
39+ {
40+ return Err ( "Remote workspace requires a canonical thread root" . into ( ) ) ;
41+ }
42+ let owner = payload
43+ . pointer ( "/launch/owner_pubkey" )
44+ . and_then ( |v| v. as_str ( ) )
45+ . ok_or ( "Remote workspace requires a verified owner" ) ?;
46+ config[ "sandbox" ] = serde_json:: json!( {
47+ "channel_id" : scope. channel_id. to_string( ) , "thread_root" : scope. thread_root,
48+ } ) ;
49+ config[ "identity_policy" ] = serde_json:: json!( {
50+ "agent_pubkey" : agent_pubkey, "owner_pubkey" : owner,
51+ } ) ;
52+ Ok ( ( ) )
53+ }
54+
1655/// Deploy an agent to a provider backend. Resolves the binary, calls deploy via
1756/// spawn_blocking, and persists the result (backend_agent_id or last_error).
1857///
@@ -50,6 +89,7 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
5089 expected_relay_url : Option < & str > ,
5190 expected_signer_pubkey : Option < & str > ,
5291 replay_floor_unix : Option < u64 > ,
92+ session_scope : Option < & RemoteSessionScope > ,
5393) -> Result < ( ) , String > {
5494 let deploy_lock = {
5595 let mut locks = state
@@ -66,7 +106,7 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
66106 // The payload may have waited behind another deployment. Rebuild it from
67107 // the current record so the final provider invocation always carries the
68108 // newest saved policy rather than the stale snapshot captured by its caller.
69- let ( provider_id, config, cached_binary_path, mut agent_json) = {
109+ let ( provider_id, mut config, cached_binary_path, mut agent_json) = {
70110 let _store_guard = state
71111 . managed_agents_store_lock
72112 . lock ( )
@@ -91,6 +131,7 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
91131 // Assert the caller's captured scope against THIS payload — the exact
92132 // value invoked below — not the pre-lock snapshot its caller validated.
93133 assert_payload_scope ( & agent_json, expected_relay_url, expected_signer_pubkey) ?;
134+ bind_sandbox_scope ( & mut config, session_scope, pubkey, & agent_json) ?;
94135 // The floor is invocation state, not record state, so the post-lock
95136 // rebuild cannot restore it — inject it into the payload actually invoked.
96137 apply_replay_floor ( & mut agent_json, replay_floor_unix) ;
@@ -262,6 +303,29 @@ fn apply_deploy_result(
262303mod tests {
263304 use super :: * ;
264305
306+ #[ test]
307+ fn sandbox_scope_is_required_and_bound_to_the_rebuilt_owner ( ) {
308+ let scope = RemoteSessionScope {
309+ channel_id : uuid:: Uuid :: new_v4 ( ) ,
310+ thread_root : "a" . repeat ( 64 ) ,
311+ } ;
312+ let mut config = serde_json:: json!( { "sandbox" : true } ) ;
313+ let payload = serde_json:: json!( { "launch" : { "owner_pubkey" : "owner" } } ) ;
314+ assert ! ( bind_sandbox_scope( & mut config, None , "agent" , & payload) . is_err( ) ) ;
315+ assert_eq ! ( config[ "sandbox" ] , true ) ;
316+ bind_sandbox_scope ( & mut config, Some ( & scope) , "agent" , & payload) . unwrap ( ) ;
317+ assert_eq ! ( config[ "sandbox" ] [ "thread_root" ] , scope. thread_root) ;
318+ assert_eq ! ( config[ "identity_policy" ] [ "owner_pubkey" ] , "owner" ) ;
319+ let mut missing_owner = serde_json:: json!( { "sandbox" : true } ) ;
320+ assert ! ( bind_sandbox_scope(
321+ & mut missing_owner,
322+ Some ( & scope) ,
323+ "agent" ,
324+ & serde_json:: json!( { } )
325+ )
326+ . is_err( ) ) ;
327+ }
328+
265329 fn record ( ) -> crate :: managed_agents:: ManagedAgentRecord {
266330 serde_json:: from_value ( serde_json:: json!( {
267331 "pubkey" : "agent" , "name" : "Agent" , "relay_url" : "" , "acp_command" : "" ,
0 commit comments