Skip to content

Commit 1b512a9

Browse files
committed
feat(gpui): add the remote workspace agent type
"Remote workspace (Kubernetes)" agents run each thread in its own Kubernetes Sandbox, which the agent's host starts. Their config is the provider, its config (always sandbox: true, checked with validate_provider_config), the user's environment, the harness, model and instructions, edited in a Settings tab as JSON. A mention by the owner, or by an agent the owner attested, sends Start for the mention's channel and thread, with the mention's created_at as the replay floor so the harness sees it. Watches and timers start nothing. The launch's policy_env follows the desktop's launch block for what this type configures. The type is built only with the voltage-sandbox feature. Signed-off-by: Tee8z <[email protected]>
1 parent 5ed2571 commit 1b512a9

9 files changed

Lines changed: 998 additions & 2 deletions

File tree

‎apps/buzz-gpui/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/buzz-gpui/crates/app/Cargo.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ buzz-identity = { path = "../identity" }
2525
buzz-pair-mobile = { path = "../pair-mobile" }
2626
buzz-relay-client = { path = "../relay" }
2727
buzz-sandbox-host = { path = "../sandbox-host", optional = true }
28+
# The remote workspace type's config checks and recovery codes.
29+
buzz-remote-sessions = { path = "../../../../crates/buzz-remote-sessions", optional = true }
2830
serde_json = "1"
2931
buzz-ui = { path = "../ui" }
3032
buzzodz-plugins.workspace = true
@@ -76,7 +78,7 @@ url = "2"
7678

7779
[features]
7880
# Remote workspaces on Voltage's Kubernetes provider; off for upstream builds.
79-
voltage-sandbox = ["dep:buzz-sandbox-host"]
81+
voltage-sandbox = ["dep:buzz-sandbox-host", "dep:buzz-remote-sessions"]
8082

8183
[dev-dependencies]
8284
# Paused time for timing tests.

‎apps/buzz-gpui/crates/app/src/agent_plugins/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ mod opened;
88
pub mod owner_relay;
99
pub mod process;
1010
mod provider;
11+
#[cfg(feature = "voltage-sandbox")]
12+
pub mod remote_workspace;
1113
pub(crate) mod tabs;
1214
mod transcript_view;
1315

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
//! The type's config, and the launch a session runs with: the desktop's
2+
//! `build_launch_block_for_policy` for what this type configures.
3+
4+
use buzz_host::api::{Launch, LaunchSpec};
5+
use serde_json::{Value, json};
6+
use std::collections::BTreeMap;
7+
8+
/// The only provider whose sessions belong to threads.
9+
pub const PROVIDER: &str = "kubernetes";
10+
11+
/// A harness the agent's image runs, as the desktop's catalog has it.
12+
pub struct Runtime {
13+
pub id: &'static str,
14+
pub label: &'static str,
15+
/// Found on the image's `PATH`.
16+
command: &'static str,
17+
args: &'static [&'static str],
18+
default_env: &'static [(&'static str, &'static str)],
19+
}
20+
21+
pub const RUNTIMES: &[Runtime] = &[
22+
Runtime {
23+
id: "claude",
24+
label: "Claude Code",
25+
command: "claude-agent-acp",
26+
args: &[],
27+
default_env: &[],
28+
},
29+
Runtime {
30+
id: "codex",
31+
label: "Codex",
32+
command: "codex-acp",
33+
args: &[],
34+
default_env: &[],
35+
},
36+
Runtime {
37+
id: "goose",
38+
label: "Goose",
39+
command: "goose",
40+
args: &["acp"],
41+
default_env: &[("GOOSE_MODE", "auto")],
42+
},
43+
Runtime {
44+
id: "buzz-agent",
45+
label: "Buzz agent",
46+
command: "buzz-agent",
47+
args: &[],
48+
default_env: &[("MCP_HOOK_SERVERS", "*")],
49+
},
50+
];
51+
52+
/// What the type saves for each agent.
53+
#[derive(Clone, Debug, PartialEq)]
54+
pub struct Config {
55+
pub provider: String,
56+
/// The provider's public config; always a thread Sandbox.
57+
pub provider_config: Value,
58+
/// The user's environment for the agent, which wins over the launch's.
59+
pub env: BTreeMap<String, String>,
60+
/// One of [`RUNTIMES`].
61+
pub runtime: String,
62+
/// Empty uses the harness's default.
63+
pub model: String,
64+
pub instructions: String,
65+
pub parallelism: u32,
66+
}
67+
68+
impl Default for Config {
69+
fn default() -> Self {
70+
Self {
71+
provider: PROVIDER.into(),
72+
provider_config: json!({ "sandbox": true }),
73+
env: BTreeMap::new(),
74+
runtime: RUNTIMES[0].id.into(),
75+
model: String::new(),
76+
instructions: String::new(),
77+
parallelism: 1,
78+
}
79+
}
80+
}
81+
82+
impl Config {
83+
/// The saved config; what is missing or malformed is the default.
84+
pub fn read(value: &Value) -> Self {
85+
let default = Self::default();
86+
let text = |name: &str| value.get(name).and_then(Value::as_str).map(str::to_owned);
87+
let mut provider_config = value
88+
.get("provider_config")
89+
.filter(|config| config.is_object())
90+
.cloned()
91+
.unwrap_or(default.provider_config);
92+
provider_config["sandbox"] = Value::Bool(true);
93+
let env = value
94+
.get("env")
95+
.and_then(Value::as_object)
96+
.map(|env| {
97+
env.iter()
98+
.filter_map(|(key, value)| Some((key.clone(), value.as_str()?.to_owned())))
99+
.collect()
100+
})
101+
.unwrap_or_default();
102+
let runtime = text("runtime")
103+
.filter(|id| RUNTIMES.iter().any(|runtime| runtime.id == id))
104+
.unwrap_or(default.runtime);
105+
Self {
106+
provider: text("provider").unwrap_or(default.provider),
107+
provider_config,
108+
env,
109+
runtime,
110+
model: text("model").unwrap_or_default(),
111+
instructions: text("instructions").unwrap_or_default(),
112+
parallelism: value
113+
.get("parallelism")
114+
.and_then(Value::as_u64)
115+
.and_then(|n| u32::try_from(n).ok())
116+
.filter(|n| *n > 0)
117+
.unwrap_or(default.parallelism),
118+
}
119+
}
120+
121+
pub fn value(&self) -> Value {
122+
json!({
123+
"provider": self.provider,
124+
"provider_config": self.provider_config,
125+
"env": self.env,
126+
"runtime": self.runtime,
127+
"model": self.model,
128+
"instructions": self.instructions,
129+
"parallelism": self.parallelism,
130+
})
131+
}
132+
133+
/// Whether the host will accept it, as far as the GUI can tell.
134+
pub fn validate(&self) -> Result<(), String> {
135+
if self.provider != PROVIDER {
136+
return Err(format!(
137+
"Remote workspaces run on the {PROVIDER} provider only."
138+
));
139+
}
140+
buzz_remote_sessions::validate_provider_config(&self.provider_config)?;
141+
if let Some(key) = self.env.keys().find(|key| !env_name(key)) {
142+
return Err(format!("{key:?} is not an environment variable name."));
143+
}
144+
Ok(())
145+
}
146+
147+
fn runtime(&self) -> &'static Runtime {
148+
RUNTIMES
149+
.iter()
150+
.find(|runtime| runtime.id == self.runtime)
151+
.unwrap_or(&RUNTIMES[0])
152+
}
153+
154+
/// What the host launches for agent `name`. `team` is the agent's
155+
/// team section, if it has one.
156+
pub fn spec(&self, name: &str, team: Option<&str>) -> LaunchSpec {
157+
let runtime = self.runtime();
158+
let claude = runtime.id == "claude";
159+
let mut policy_env: BTreeMap<String, String> = runtime
160+
.default_env
161+
.iter()
162+
.map(|(key, value)| (key.to_string(), value.to_string()))
163+
.collect();
164+
let mut set = |key: &str, value: String| policy_env.insert(key.into(), value);
165+
set("BUZZ_ACP_RELAY_OBSERVER", "true".into());
166+
set("BUZZ_ACP_LAZY_POOL", "true".into());
167+
set("BUZZ_ACP_AGENTS", self.parallelism.to_string());
168+
// Each Sandbox is one thread's.
169+
set(SESSION_POLICY, "thread".into());
170+
if !self.instructions.trim().is_empty() {
171+
set("BUZZ_ACP_SYSTEM_PROMPT", self.instructions.clone());
172+
}
173+
if !self.model.trim().is_empty() {
174+
// Claude reads its startup model from ANTHROPIC_MODEL.
175+
let key = if claude {
176+
"ANTHROPIC_MODEL"
177+
} else {
178+
"BUZZ_ACP_MODEL"
179+
};
180+
set(key, self.model.trim().into());
181+
}
182+
let title: String = name.chars().filter(|c| !c.is_control()).collect();
183+
if !title.trim().is_empty() {
184+
set("BUZZ_ACP_SESSION_TITLE", title.trim().into());
185+
set("BUZZ_ACP_DISPLAY_NAME", title.trim().into());
186+
}
187+
if let Some(team) = team.filter(|team| !team.trim().is_empty()) {
188+
set("BUZZ_ACP_TEAM_INSTRUCTIONS", team.into());
189+
}
190+
// The user's environment wins over policy_env, so it may not name
191+
// the session policy, nor for Claude a second startup model.
192+
let env = self
193+
.env
194+
.iter()
195+
.filter(|(key, _)| {
196+
!key.eq_ignore_ascii_case(SESSION_POLICY)
197+
&& !(claude
198+
&& (key.eq_ignore_ascii_case("BUZZ_ACP_MODEL")
199+
|| key.eq_ignore_ascii_case("ANTHROPIC_MODEL")))
200+
})
201+
.map(|(key, value)| (key.clone(), value.clone()))
202+
.collect();
203+
LaunchSpec {
204+
provider: self.provider.clone(),
205+
provider_config_json: self.provider_config.to_string(),
206+
env,
207+
launch: Launch {
208+
command: runtime.command.into(),
209+
args: runtime.args.iter().map(|arg| arg.to_string()).collect(),
210+
env: BTreeMap::new(),
211+
policy_env,
212+
},
213+
name: name.into(),
214+
}
215+
}
216+
}
217+
218+
const SESSION_POLICY: &str = "BUZZ_ACP_SESSION_POLICY";
219+
220+
/// `[A-Za-z_][A-Za-z0-9_]*`, which the provider requires.
221+
fn env_name(key: &str) -> bool {
222+
let mut chars = key.chars();
223+
chars
224+
.next()
225+
.is_some_and(|c| c == '_' || c.is_ascii_alphabetic())
226+
&& chars.all(|c| c == '_' || c.is_ascii_alphanumeric())
227+
}

0 commit comments

Comments
 (0)