Skip to content

Commit 4bf9e54

Browse files
committed
feat(desktop): start thread-bound remote sessions from mentions
After a mention is published, pass its channel and canonical thread root to the detached agent start. For a Kubernetes provider with `sandbox: true`, native deploy binds that thread scope and the verified owner after the deployment lock, and refuses to start without a thread. Provider config fields accept bounded, nested JSON objects and arrays. Signed-off-by: Tee8z <[email protected]>
1 parent b03f007 commit 4bf9e54

48 files changed

Lines changed: 1162 additions & 96 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎desktop/src-tauri/src/commands/agents.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,11 @@ async fn create_managed_agent_in<R: tauri::Runtime>(
824824
profile_sync_error =
825825
super::agent_models::flush_managed_agent_policy(&app, state, profile_sync_error).await;
826826

827-
let spawn_error = if input.spawn_after_create && input.backend != BackendKind::Local {
827+
// A thread-bound provider starts only after a mention supplies its scope.
828+
let spawn_error = if input.spawn_after_create
829+
&& input.backend != BackendKind::Local
830+
&& !input.backend.is_thread_sandbox()
831+
{
828832
if let BackendKind::Provider { ref id, ref config } = input.backend {
829833
let agent_json = {
830834
let _g = state
@@ -839,7 +843,7 @@ async fn create_managed_agent_in<R: tauri::Runtime>(
839843
build_deploy_payload(&app, state, rec)?
840844
};
841845
match deploy_to_provider(
842-
&app, state, &pubkey, id, config, agent_json, None, None, None, None,
846+
&app, state, &pubkey, id, config, agent_json, None, None, None, None, None,
843847
)
844848
.await
845849
{
@@ -888,6 +892,7 @@ pub async fn start_managed_agent(
888892
expected_relay_url: Option<String>,
889893
expected_signer_pubkey: Option<String>,
890894
replay_floor_unix: Option<u64>,
895+
session_scope: Option<provider_deploy::RemoteSessionScope>,
891896
app: AppHandle,
892897
state: State<'_, AppState>,
893898
) -> Result<ManagedAgentSummary, String> {
@@ -1014,6 +1019,7 @@ pub async fn start_managed_agent(
10141019
expected_relay_url.as_deref(),
10151020
expected_signer_pubkey.as_deref(),
10161021
replay_floor_unix,
1022+
session_scope.as_ref(),
10171023
)
10181024
.await?;
10191025

@@ -1240,4 +1246,4 @@ use profile::{profile_needs_sync, resolve_legacy_avatar};
12401246
mod admission_tests;
12411247
#[cfg(test)]
12421248
#[path = "agents_tests.rs"]
1243-
mod tests;
1249+
pub(super) mod tests;

‎desktop/src-tauri/src/commands/agents/provider_access.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub(super) fn needs_reconciliation_with_policy(
1616
owner_only_access: bool,
1717
) -> bool {
1818
(owner_only_access || record.provider_policy_pending)
19+
// Thread Sandboxes enforce their owner/thread inside the harness.
20+
// Configuration applies on the next scoped launch, never on app reopen.
21+
&& !record.backend.is_thread_sandbox()
1922
&& record.backend != BackendKind::Local
2023
&& record.backend_agent_id.is_some()
2124
}
@@ -101,6 +104,7 @@ pub(crate) async fn reconcile_on_workspace_apply(
101104
None,
102105
None,
103106
None,
107+
None,
104108
)
105109
.await
106110
{
@@ -148,6 +152,26 @@ mod tests {
148152
record
149153
}
150154

155+
#[test]
156+
fn thread_sandboxes_do_not_redeploy_during_workspace_apply() {
157+
let mut sandbox = record(
158+
BackendKind::Provider {
159+
id: "kubernetes".into(),
160+
config: serde_json::json!({"sandbox":true}),
161+
},
162+
Some("thread-workspace"),
163+
);
164+
sandbox.provider_policy_pending = true;
165+
for owner_only in [false, true] {
166+
assert!(
167+
collect_targets_with(vec![sandbox.clone()], owner_only, |_| {
168+
panic!("an app lifecycle event must not build an unscoped thread deployment")
169+
})
170+
.is_empty()
171+
);
172+
}
173+
}
174+
151175
#[test]
152176
fn upgrade_collects_existing_provider_and_builds_projected_payload() {
153177
let records = vec![

‎desktop/src-tauri/src/commands/agents/provider_deploy.rs‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ use crate::{
1313

1414
use 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(
262310
mod 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

Comments
 (0)