@@ -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+ /// Replace a thread Sandbox's `sandbox: true` request with the published
25+ /// thread and the rebuilt payload's verified owner. Callers decide eligibility
26+ /// with [`BackendKind::is_thread_sandbox`] on the rebuilt record.
27+ fn bind_sandbox_scope (
28+ config : & mut serde_json:: Value ,
29+ scope : Option < & RemoteSessionScope > ,
30+ agent_pubkey : & str ,
31+ payload : & serde_json:: Value ,
32+ ) -> Result < ( ) , String > {
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,7 +89,12 @@ 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 > {
94+ // One lock per agent, not per thread: it serializes the record rebuild,
95+ // provider call, and result write. A thread Sandbox cold start holds it
96+ // through the provider's Pod waits (120s bound each), so starts for the
97+ // same agent's other threads queue behind it.
5498 let deploy_lock = {
5599 let mut locks = state
56100 . provider_deploy_locks
@@ -66,7 +110,7 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
66110 // The payload may have waited behind another deployment. Rebuild it from
67111 // the current record so the final provider invocation always carries the
68112 // newest saved policy rather than the stale snapshot captured by its caller.
69- let ( provider_id, config, cached_binary_path, mut agent_json) = {
113+ let ( provider_id, mut config, thread_sandbox , cached_binary_path, mut agent_json) = {
70114 let _store_guard = state
71115 . managed_agents_store_lock
72116 . lock ( )
@@ -83,6 +127,7 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
83127 (
84128 provider_id,
85129 config,
130+ record. backend . is_thread_sandbox ( ) ,
86131 record. provider_binary_path . clone ( ) ,
87132 build_deploy_payload ( app, state, record) ?,
88133 )
@@ -91,6 +136,9 @@ pub(crate) async fn deploy_to_provider<R: tauri::Runtime>(
91136 // Assert the caller's captured scope against THIS payload — the exact
92137 // value invoked below — not the pre-lock snapshot its caller validated.
93138 assert_payload_scope ( & agent_json, expected_relay_url, expected_signer_pubkey) ?;
139+ if thread_sandbox {
140+ bind_sandbox_scope ( & mut config, session_scope, pubkey, & agent_json) ?;
141+ }
94142 // The floor is invocation state, not record state, so the post-lock
95143 // rebuild cannot restore it — inject it into the payload actually invoked.
96144 apply_replay_floor ( & mut agent_json, replay_floor_unix) ;
@@ -262,6 +310,29 @@ fn apply_deploy_result(
262310mod tests {
263311 use super :: * ;
264312
313+ #[ test]
314+ fn sandbox_scope_is_required_and_bound_to_the_rebuilt_owner ( ) {
315+ let scope = RemoteSessionScope {
316+ channel_id : uuid:: Uuid :: new_v4 ( ) ,
317+ thread_root : "a" . repeat ( 64 ) ,
318+ } ;
319+ let mut config = serde_json:: json!( { "sandbox" : true } ) ;
320+ let payload = serde_json:: json!( { "launch" : { "owner_pubkey" : "owner" } } ) ;
321+ assert ! ( bind_sandbox_scope( & mut config, None , "agent" , & payload) . is_err( ) ) ;
322+ assert_eq ! ( config[ "sandbox" ] , true ) ;
323+ bind_sandbox_scope ( & mut config, Some ( & scope) , "agent" , & payload) . unwrap ( ) ;
324+ assert_eq ! ( config[ "sandbox" ] [ "thread_root" ] , scope. thread_root) ;
325+ assert_eq ! ( config[ "identity_policy" ] [ "owner_pubkey" ] , "owner" ) ;
326+ let mut missing_owner = serde_json:: json!( { "sandbox" : true } ) ;
327+ assert ! ( bind_sandbox_scope(
328+ & mut missing_owner,
329+ Some ( & scope) ,
330+ "agent" ,
331+ & serde_json:: json!( { } )
332+ )
333+ . is_err( ) ) ;
334+ }
335+
265336 fn record ( ) -> crate :: managed_agents:: ManagedAgentRecord {
266337 serde_json:: from_value ( serde_json:: json!( {
267338 "pubkey" : "agent" , "name" : "Agent" , "relay_url" : "" , "acp_command" : "" ,
0 commit comments