Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(macrod): connect before agent creation and preserve harness selection (#6546)

gbirmanac4c91409e72
diff --git a/apps/web/src/features/settings/Agents.test.tsx b/apps/web/src/features/settings/Agents.test.tsx
index d08d06bffc..f5c762f791 100644
--- a/apps/web/src/features/settings/Agents.test.tsx
+++ b/apps/web/src/features/settings/Agents.test.tsx
@@ -4,7 +4,9 @@
import { Model } from '@core/component/AI/constant/model';
import { useAgentModelsQueries } from '@queries/agents/models';
+import { useHarnessesQuery } from '@queries/harnesses/harnesses';
import type { LoadAgentModelsResponse } from '@service-agent-harness/generated/schemas';
+import type { Harness } from '@service-storage/client';
import {
fireEvent,
render,
@@ -155,7 +157,7 @@ const harnessMocks = vi.hoisted(() => ({
}));
vi.mock('@queries/harnesses/harnesses', () => ({
- useHarnessesQuery: () => harnessMocks.query,
+ useHarnessesQuery: vi.fn(() => harnessMocks.query),
}));
vi.mock('@queries/agents/agents', () => ({
@@ -307,7 +309,7 @@ const MACROD_HARNESS = {
updated_at: '2026-08-27T12:00:00Z',
connected: true,
last_connected_at: '2026-08-27T12:34:00Z',
-};
+} satisfies Harness;
describe('Agents', () => {
it.each([false, true])(
@@ -992,6 +994,69 @@ describe('Agents', () => {
).toBeTruthy();
});
+ it('preserves the selected harness and submission when the harness list refetches', async () => {
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const queryKey = ['refreshed-harnesses'];
+ const fetchHarnesses = vi.fn(
+ async (): Promise<Harness[]> => [{ ...MACROD_HARNESS }]
+ );
+ vi.mocked(useHarnessesQuery).mockImplementationOnce(() =>
+ useQuery(() => ({
+ queryKey,
+ queryFn: fetchHarnesses,
+ initialData: [MACROD_HARNESS],
+ }))
+ );
+ modelMocks.queries[`macrod:${MACROD_HARNESS.id}`] = successfulModels([
+ { id: 'codex-model', name: 'Codex model' },
+ ]);
+ const view = render(() => (
+ <QueryClientProvider client={client}>
+ <Suspense>
+ <Agents />
+ </Suspense>
+ </QueryClientProvider>
+ ));
+ await waitFor(() => expect(fetchHarnesses).toHaveBeenCalledOnce());
+ fireEvent.click(screen.getByRole('button', { name: 'Create agent' }));
+ const dialog = screen.getByRole('dialog');
+ const harness = within(dialog).getByRole('combobox', { name: 'Harness' });
+ await waitFor(() =>
+ expect(
+ within(harness).getByRole('option', { name: 'Dev box' })
+ ).toBeTruthy()
+ );
+ fireEvent.change(harness, { target: { value: MACROD_HARNESS.id } });
+ fireEvent.input(within(dialog).getByLabelText('Name'), {
+ target: { value: 'Coding agent' },
+ });
+
+ for (let refresh = 0; refresh < 2; refresh++) {
+ await client.refetchQueries({ queryKey });
+ expect(harness).toHaveProperty('value', MACROD_HARNESS.id);
+ expect(
+ within(dialog).getByRole('combobox', { name: 'Default model' })
+ ).toHaveProperty('value', 'codex-model');
+ }
+ expect(fetchHarnesses).toHaveBeenCalledTimes(3);
+ fireEvent.click(
+ within(dialog).getByRole('button', { name: 'Create agent' })
+ );
+ await waitFor(() =>
+ expect(agentMocks.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ harness: 'macrod',
+ harnessId: MACROD_HARNESS.id,
+ defaultModel: 'codex-model',
+ })
+ )
+ );
+ view.unmount();
+ client.clear();
+ });
+
it('keeps loading, error, and unsupported states independent per harness', () => {
cursorMocks.status.data = {
registered: true,
@@ -1195,6 +1260,54 @@ describe('Agents', () => {
).toBeTruthy();
});
+ it('keeps a saved Claude harness visible when model discovery fails', async () => {
+ modelMocks.queries['claude-cloud:'] = {
+ isPending: false,
+ isError: true,
+ isSuccess: false,
+ refetch: vi.fn(),
+ };
+ agentMocks.query.data = [
+ {
+ bot: {
+ id: 'agent-1',
+ kind: 'owned',
+ owner: { type: 'user', user_id: 'macro|[email protected]' },
+ name: 'Bug fixer',
+ handle: 'bug-fixer',
+ has_agent: true,
+ created_at: '2026-08-27T12:00:00Z',
+ updated_at: '2026-08-27T12:00:00Z',
+ },
+ instructions: 'Fix the root cause.',
+ harness: 'claude-cloud',
+ default_model: 'saved-claude-model',
+ channel_scope: 'all',
+ channel_ids: [],
+ },
+ ];
+
+ render(() => <Agents />);
+ fireEvent.click(screen.getByRole('button', { name: 'Edit Bug fixer' }));
+
+ const dialog = screen.getByRole('dialog');
+ expect(within(dialog).getByLabelText('Harness')).toHaveProperty(
+ 'value',
+ 'claude-cloud'
+ );
+ fireEvent.click(
+ within(dialog).getByRole('button', { name: 'Save changes' })
+ );
+ await waitFor(() => {
+ expect(agentMocks.update).toHaveBeenCalledWith(
+ expect.objectContaining({
+ harness: 'claude-cloud',
+ defaultModel: 'saved-claude-model',
+ })
+ );
+ });
+ });
+
it('preselects the bound macrod harness when editing', () => {
harnessMocks.query.data = [MACROD_HARNESS];
modelMocks.queries[`macrod:${MACROD_HARNESS.id}`] = successfulModels([
diff --git a/apps/web/src/features/settings/Agents.tsx b/apps/web/src/features/settings/Agents.tsx
index a93803872e..0ad68794e4 100644
--- a/apps/web/src/features/settings/Agents.tsx
+++ b/apps/web/src/features/settings/Agents.tsx
@@ -866,7 +866,6 @@ function AgentDialog(props: {
<span class="text-xs font-medium text-ink">Harness</span>
<select
class="settings-input w-full"
- value={harnessId()}
onChange={(event) =>
handleHarnessChange(event.currentTarget.value)
}
@@ -881,7 +880,12 @@ function AgentDialog(props: {
)}
>
{(harness) => (
- <option value={harness.id}>{harness.name}</option>
+ <option
+ value={harness.id}
+ selected={harness.id === harnessId()}
+ >
+ {harness.name}
+ </option>
)}
</For>
</select>
diff --git a/crates/agent_runtime_protocol/src/domain/connection.rs b/crates/agent_runtime_protocol/src/domain/connection.rs
index e9e155e1cb..97fb256eee 100644
--- a/crates/agent_runtime_protocol/src/domain/connection.rs
+++ b/crates/agent_runtime_protocol/src/domain/connection.rs
@@ -98,7 +98,7 @@ impl Drop for ServerConnection {
/// Agent Runtime-side access to one logical service connection.
pub struct RuntimeConnection {
outbound: UnboundedSender<ToServerMessage>,
- driver: tokio::task::AbortHandle,
+ driver: tokio::task::JoinHandle<()>,
}
impl RuntimeConnection {
@@ -131,11 +131,18 @@ impl RuntimeConnection {
outbound.clone(),
acp_driver,
Arc::new(model_probes),
- ))
- .abort_handle();
+ ));
(Self { outbound, driver }, acp)
}
+ /// Wait for the service transport to close, independently of the ACP peer.
+ /// Callers can cancel an idle ACP subprocess when the service goes away.
+ pub async fn closed(&mut self) {
+ if !self.driver.is_finished() {
+ let _ = (&mut self.driver).await;
+ }
+ }
+
/// Send a system event notification to the Agent Service.
pub fn system_event(&self, event: SystemEvent) -> Result<(), ConnectionError> {
self.outbound
diff --git a/crates/agent_runtime_protocol/src/domain/connection/test.rs b/crates/agent_runtime_protocol/src/domain/connection/test.rs
index 71c7d52bb0..5e554f5842 100644
--- a/crates/agent_runtime_protocol/src/domain/connection/test.rs
+++ b/crates/agent_runtime_protocol/src/domain/connection/test.rs
@@ -277,3 +277,15 @@ async fn runtime_model_probes_fan_out_without_serializing() {
));
}
}
+
+#[tokio::test]
+async fn runtime_closes_when_transport_drops_even_with_an_idle_acp_peer() {
+ let (server_channel, runtime_channel) = Channel::duplex();
+ let (mut runtime, _idle_acp) = RuntimeConnection::connect(runtime_channel);
+ drop(server_channel);
+
+ timeout(Duration::from_secs(1), runtime.closed())
+ .await
+ .expect("transport closure must be observable without ACP traffic");
+ runtime.closed().await;
+}
diff --git a/crates/coding_agent_worker/Cargo.toml b/crates/coding_agent_worker/Cargo.toml
index fd0fafc629..851bf581f8 100644
--- a/crates/coding_agent_worker/Cargo.toml
+++ b/crates/coding_agent_worker/Cargo.toml
@@ -63,4 +63,4 @@ channels = { path = "../channels", default-features = false }
chrono = { workspace = true }
harness_id = { path = "../harness_id", features = ["test-utils"] }
tempfile = { workspace = true }
-tokio = { workspace = true, features = ["io-util", "net"] }
+tokio = { workspace = true, features = ["io-util", "net", "test-util"] }
diff --git a/crates/coding_agent_worker/config.example.toml b/crates/coding_agent_worker/config.example.toml
index ac6b9231dc..76f2b3a5db 100644
--- a/crates/coding_agent_worker/config.example.toml
+++ b/crates/coding_agent_worker/config.example.toml
@@ -6,7 +6,7 @@
[macro]
# Base URL of the agent-harness service. Its ws(s) twin hosts the runtime
-# gateway the daemon dials per session.
+# gateway the daemon connects to on startup.
api_url = "http://localhost:50009/agent-harness"
# Base URL of the storage service (harness pairing + live event stream).
storage_url = "http://localhost:50009/dss"
diff --git a/crates/coding_agent_worker/src/config.rs b/crates/coding_agent_worker/src/config.rs
index 47a61725e6..1bf4a4444b 100644
--- a/crates/coding_agent_worker/src/config.rs
+++ b/crates/coding_agent_worker/src/config.rs
@@ -1,5 +1,5 @@
//! The daemon's one input: a TOML file describing the Macro deployment it
-//! streams from and the harness it runs per session. See
+//! streams from and the harness it runs. See
//! `config.example.toml` at the crate root.
//!
//! Pairing (press `p` in the control panel) adds the harness credential to
@@ -30,7 +30,7 @@ pub struct Config {
#[serde(default)]
#[expect(dead_code, reason = "accepted only so existing configs still parse")]
server: Option<LegacyServer>,
- /// The harness process spawned per session.
+ /// The harness process shared by every session.
pub harness: Harness,
/// The workspace every session runs against.
pub workspace: Workspace,
@@ -87,7 +87,7 @@ pub struct MacroApi {
}
impl MacroApi {
- /// The dial-in URL for a session on this deployment's runtime gateway:
+ /// The dial-in URL for this deployment's runtime gateway:
/// the API base with a websocket scheme.
pub fn gateway_url(&self) -> String {
let base = self.api_url.trim_end_matches('/');
@@ -145,7 +145,7 @@ struct LegacyServer {
signing_secret: Option<String>,
}
-/// The harness process spawned per session. Generic on purpose: any binary
+/// The harness process shared by every session. Generic on purpose: any binary
/// speaking ACP over stdio fits here - opencode, claude, hermes - so a new
/// harness is a config change, not code.
#[derive(Debug, Clone, Deserialize)]
diff --git a/crates/coding_agent_worker/src/daemon.rs b/crates/coding_agent_worker/src/daemon.rs
index ec3834ce14..5f27252be9 100644
--- a/crates/coding_agent_worker/src/daemon.rs
+++ b/crates/coding_agent_worker/src/daemon.rs
@@ -33,7 +33,7 @@ pub struct Daemon {
}
impl Daemon {
- /// Start listening for agent triggers in a background task.
+ /// Connect the runtime and listen for agent triggers in background tasks.
///
/// Returns once the client is built; the serving itself runs until
/// [`Daemon::stop`] or the task fails.
@@ -53,7 +53,7 @@ impl Daemon {
let cancel = CancellationToken::new();
let client = EventStreamClient::new(&config.macro_api, &credentials);
let api = HarnessApi::new(&config.macro_api, &credentials);
- let runtime = Runtime::new(
+ let runtime = Runtime::start(
&config.macro_api,
&credentials,
config.harness.clone(),
@@ -105,9 +105,8 @@ impl Daemon {
/// Stop serving without waiting for an in-flight network request or trigger.
///
- /// A live harness bridge (the WebSocket + spawned harness process) is not
- /// torn down here; it dies with the process, and a restarted daemon's
- /// fresh dial displaces it at the gateway.
+ /// Dropping the dispatcher also stops its runtime supervisor, WebSocket,
+ /// and harness process, including when the daemon is restarted to re-pair.
pub async fn stop(self) {
self.cancel.cancel();
self.task.abort();
diff --git a/crates/coding_agent_worker/src/daemon/test.rs b/crates/coding_agent_worker/src/daemon/test.rs
index e5d0bc4275..e882166bca 100644
--- a/crates/coding_agent_worker/src/daemon/test.rs
+++ b/crates/coding_agent_worker/src/daemon/test.rs
@@ -11,3 +11,101 @@ async fn stop_does_not_wait_for_in_flight_work() {
.await
.expect("daemon shutdown should abort in-flight work");
}
+
+#[tokio::test]
+async fn startup_without_agents_serves_model_probes_and_stop_closes_the_runtime() {
+ use agent_runtime_protocol::domain::schema::v0::{
+ ModelProbeResult, SystemEvent, ToRuntimeMessage, ToServerMessage,
+ };
+ use futures::{SinkExt as _, StreamExt as _};
+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
+ use tokio::net::TcpListener;
+ use tokio_tungstenite::tungstenite::Message;
+
+ let gateway = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let storage = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let mut config: Config = toml::from_str(include_str!("../../config.example.toml")).unwrap();
+ config.macro_api.api_url = format!("http://{}", gateway.local_addr().unwrap());
+ config.macro_api.storage_url = format!("http://{}", storage.local_addr().unwrap());
+ config.workspace.path = std::env::current_dir().unwrap();
+ config.harness.command = "python3".to_owned();
+ config.harness.args = vec!["-c".to_owned(), include_str!("test_acp.py").to_owned()];
+ let credentials = HarnessCredentials {
+ harness_id: harness_id::HarnessId::TEST_A,
+ token: "mhns_test".to_owned(),
+ scope: crate::config::HarnessScope::User,
+ };
+ let storage_task = tokio::spawn(async move {
+ let (mut socket, _) = storage.accept().await.unwrap();
+ let mut request = [0; 4096];
+ let read = socket.read(&mut request).await.unwrap();
+ assert!(String::from_utf8_lossy(&request[..read]).contains("/harnesses/me/agents"));
+ socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\nConnection: close\r\n\r\n[]").await.unwrap();
+ });
+ let daemon = Daemon::start(config, credentials, Path::new("macrod.toml"))
+ .await
+ .unwrap();
+ let (socket, _) = tokio::time::timeout(Duration::from_secs(2), gateway.accept())
+ .await
+ .expect("the runtime must connect before an agent exists")
+ .unwrap();
+ let mut socket = tokio_tungstenite::accept_hdr_async(
+ socket,
+ |request: &tokio_tungstenite::tungstenite::handshake::server::Request, response| {
+ assert_eq!(request.uri().path(), "/runtime/ws");
+ assert_eq!(request.headers()["x-macro-harness-token"], "mhns_test");
+ Ok(response)
+ },
+ )
+ .await
+ .unwrap();
+ storage_task.await.unwrap();
+ socket
+ .send(Message::Text(
+ serde_json::to_string(&ToRuntimeMessage::ModelProbeRequest)
+ .unwrap()
+ .into(),
+ ))
+ .await
+ .unwrap();
+ tokio::time::timeout(Duration::from_secs(3), async {
+ let mut ready = false;
+ loop {
+ if let Message::Text(text) = socket.next().await.unwrap().unwrap() {
+ match serde_json::from_str::<ToServerMessage>(&text).unwrap() {
+ ToServerMessage::Event {
+ event: SystemEvent::AcpReady,
+ } => ready = true,
+ ToServerMessage::ModelProbeResponse { result } => {
+ assert!(
+ ready,
+ "the bridge must announce itself before answering probes"
+ );
+ let ModelProbeResult::Available { config_options } = result else {
+ panic!("model discovery failed: {result:?}");
+ };
+ let options = serde_json::to_value(config_options).unwrap();
+ assert_eq!(options[0]["currentValue"], "test-model");
+ break;
+ }
+ message => panic!("unexpected runtime message: {message:?}"),
+ }
+ }
+ }
+ })
+ .await
+ .expect("model discovery must work before any trigger");
+
+ daemon.stop().await;
+ tokio::time::timeout(Duration::from_secs(2), async {
+ while let Some(Ok(_)) = socket.next().await {}
+ })
+ .await
+ .expect("stopping must close the runtime socket");
+ assert!(
+ tokio::time::timeout(Duration::from_millis(1100), gateway.accept())
+ .await
+ .is_err(),
+ "stopped daemon must not reconnect"
+ );
+}
diff --git a/crates/coding_agent_worker/src/daemon/test_acp.py b/crates/coding_agent_worker/src/daemon/test_acp.py
new file mode 100644
index 0000000000..e773f4d061
--- /dev/null
+++ b/crates/coding_agent_worker/src/daemon/test_acp.py
@@ -0,0 +1,23 @@
+"""Minimal ACP peer: discovery succeeds, and any prompt is a test failure."""
+
+import json
+import sys
+
+for line in sys.stdin:
+ request = json.loads(line)
+ if request["method"] == "initialize":
+ result = {"protocolVersion": 1, "agentCapabilities": {}}
+ elif request["method"] == "session/new":
+ result = {
+ "sessionId": "probe",
+ "configOptions": [{
+ "id": "model",
+ "name": "Model",
+ "type": "select",
+ "currentValue": "test-model",
+ "options": [{"value": "test-model", "name": "Test model"}],
+ }],
+ }
+ else:
+ raise AssertionError(f"unexpected method: {request['method']}")
+ print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": result}), flush=True)
diff --git a/crates/coding_agent_worker/src/harness.rs b/crates/coding_agent_worker/src/harness.rs
index 1f12c1ecf2..05c8d897ff 100644
--- a/crates/coding_agent_worker/src/harness.rs
+++ b/crates/coding_agent_worker/src/harness.rs
@@ -37,7 +37,7 @@ pub async fn bridge(
let probes = HarnessModelProbes {
process: probe_process(harness, cwd),
};
- let (runtime, acp) = RuntimeConnection::connect_with_model_probe_handler(channel, probes);
+ let (mut runtime, acp) = RuntimeConnection::connect_with_model_probe_handler(channel, probes);
let agent = AcpAgent::new(AcpAgentConfig::new(&harness.command).args(harness.args.clone()))
// The wire tap: every ndjson line crossing the child's stdio, plus
@@ -50,7 +50,10 @@ pub async fn bridge(
.system_event(SystemEvent::AcpReady)
.map_err(BridgeError::Announce)?;
- let outcome = ConnectTo::<Client>::connect_to(agent, acp).await;
+ let outcome = tokio::select! {
+ outcome = ConnectTo::<Client>::connect_to(agent, acp) => outcome,
+ () = runtime.closed() => Ok(()),
+ };
// Best effort: a transport that has already failed cannot carry news of
// its own failure, and that is not itself worth failing the worker over.
diff --git a/crates/coding_agent_worker/src/main.rs b/crates/coding_agent_worker/src/main.rs
index fea22627a8..5295d21302 100644
--- a/crates/coding_agent_worker/src/main.rs
+++ b/crates/coding_agent_worker/src/main.rs
@@ -9,12 +9,11 @@
//!
//! A first run starts unpaired: the panel offers pairing (press `p`), the
//! user approves the printed code in the web app, and the minted harness
-//! credential is embedded in the sensitive `macrod.toml`. Once paired, each
-//! `agent_trigger.new` delivery opens a session over the harness service's
-//! API, dials its runtime gateway, spawns the configured harness in ACP mode,
-//! bridges its stdio to the websocket, and forwards the mention as the first
-//! prompt; `agent_trigger.existing` deliveries forward follow-up messages,
-//! redialing first when the bridge is gone.
+//! credential is embedded in the sensitive `macrod.toml`. Once paired, the
+//! daemon connects to the runtime gateway and bridges the configured ACP
+//! harness, making model discovery available before any agents exist. Each
+//! `agent_trigger.new` delivery opens a session and forwards the first prompt;
+//! `agent_trigger.existing` deliveries forward follow-up messages.
mod config;
mod daemon;
diff --git a/crates/coding_agent_worker/src/runtime.rs b/crates/coding_agent_worker/src/runtime.rs
index efdc798a5d..44eb2c8bd2 100644
--- a/crates/coding_agent_worker/src/runtime.rs
+++ b/crates/coding_agent_worker/src/runtime.rs
@@ -1,210 +1,133 @@
-//! This daemon's one connection to the harness service, and the one harness
-//! process behind it.
-//!
-//! A daemon serves a single registered harness, and a harness's runtime is a
-//! single connection: ACP initializes per connection and tags every
-//! session-scoped message with a `sessionId`, so one socket and one harness
-//! process carry every session of every agent bound to this harness. The
-//! service decides which sessions those are - it binds a session when work
-//! arrives for it - so nothing here is per-session or per-bot at all.
-//!
-//! What that buys: a harness starts once per daemon rather than once per
-//! session, so only the first mention after boot pays for a cold agent.
+//! The daemon's persistent connection to the harness service and the ACP
+//! process behind it. Connecting when the daemon starts lets the service probe
+//! models before the first agent is created. One connection carries every
+//! session of every agent bound to this harness.
use std::path::{Path, PathBuf};
-use std::sync::Arc;
-use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
-use agent_runtime_protocol::domain::connection::RuntimeChannel;
-use tokio_retry::RetryIf;
-use tokio_retry::strategy::{ExponentialBackoff, FixedInterval};
+use tokio::sync::watch;
+use tokio::time::Instant;
+use tokio_retry::strategy::ExponentialBackoff;
use tokio_tungstenite::tungstenite;
+use tokio_util::task::AbortOnDropHandle;
use crate::config::{Harness, HarnessCredentials, MacroApi};
use crate::harness;
use crate::outbound::link;
-/// A dial races the delivery that asked for it, so it retries quickly and
-/// briefly: a proxy or service mid restart is worth waiting out, a longer
-/// outage is better answered by failing the delivery and being redelivered.
-const DIAL_ATTEMPTS: usize = 3;
-const DIAL_RETRY_DELAY: Duration = Duration::from_millis(500);
+#[cfg(test)]
+mod test;
-/// How many times a dropped connection is rebuilt before the daemon waits for
-/// its next delivery to try again. Bounded because a bridge that fails the
-/// instant it starts - a harness binary that is missing, say - would otherwise
-/// rebuild itself forever. Nobody is waiting on these, so they back off.
-const REBUILD_ATTEMPTS: usize = 4;
+const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
+const MAX_RECONNECT_DELAY: Duration = Duration::from_secs(30);
/// The daemon's connection to its harness's sessions.
///
-/// At most one is live. The flag is the claim on it: a delivery that finds it
-/// unset dials, and the task serving the connection clears it on the way out,
-/// so "set" always means "somebody is serving, or about to be".
+/// Owns one connection supervisor, independent of trigger delivery. Dropping
+/// the runtime aborts the supervisor and drops its bridge, which closes the
+/// socket and terminates the ACP process group.
pub struct Runtime {
- gateway_url: String,
- token: String,
- harness: Harness,
- cwd: PathBuf,
- live: Arc<AtomicBool>,
+ connected: watch::Receiver<bool>,
+ _task: AbortOnDropHandle<()>,
}
impl Runtime {
- /// A runtime that dials with the given credentials and spawns the given
- /// harness.
- pub fn new(
+ /// Start connecting with the given credentials and keep the harness
+ /// available for model discovery and agent sessions until dropped.
+ pub fn start(
macro_api: &MacroApi,
credentials: &HarnessCredentials,
harness: Harness,
cwd: &Path,
) -> Self {
- Self {
- gateway_url: macro_api.gateway_url(),
- token: credentials.token.clone(),
+ let (connected_tx, connected) = watch::channel(false);
+ let task = tokio::spawn(serve(
+ macro_api.gateway_url(),
+ credentials.token.clone(),
harness,
- cwd: cwd.to_owned(),
- live: Arc::new(AtomicBool::new(false)),
+ cwd.to_owned(),
+ connected_tx,
+ ));
+ Self {
+ connected,
+ _task: AbortOnDropHandle::new(task),
}
}
- /// Ensure this bot's runtime is connected, dialing if it is not. Returns
- /// once the dial has succeeded (or was unnecessary), with the harness
- /// bridged in a background task.
+ /// Wait briefly for the supervisor to connect before dispatching a prompt.
+ /// Callers never open a second connection while it is reconnecting.
pub async fn ensure_connected(&self) -> Result<(), tungstenite::Error> {
- if self
- .live
- .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
- .is_err()
- {
- return Ok(());
+ let mut connected = self.connected.clone();
+ match tokio::time::timeout(CONNECT_TIMEOUT, connected.wait_for(|ready| *ready)).await {
+ Ok(Ok(_)) => Ok(()),
+ Ok(Err(_)) => Err(tungstenite::Error::ConnectionClosed),
+ Err(_) => Err(tungstenite::Error::Io(std::io::Error::new(
+ std::io::ErrorKind::TimedOut,
+ "the runtime gateway did not connect in time",
+ ))),
}
-
- let channel = match dial(&self.token, &self.gateway_url, dial_strategy()).await {
- Ok(channel) => channel,
- Err(error) => {
- self.live.store(false, Ordering::Release);
- return Err(error);
- }
- };
-
- tokio::spawn(serve(
- self.gateway_url.clone(),
- channel,
- self.token.clone(),
- self.harness.clone(),
- self.cwd.clone(),
- Arc::clone(&self.live),
- ));
- Ok(())
}
}
-/// Bridge the harness until the connection ends for good, rebuilding through
-/// failures that look transient. Holds the claim for its whole life - including
-/// across rebuilds, so a reconnecting runtime is never raced by a second one -
-/// and releases it on the way out.
+/// Reconnect even after a clean close or a long outage: model discovery must
+/// recover without requiring an agent mention. Authentication failures stop
+/// the supervisor until the user re-pairs or restarts the daemon.
async fn serve(
gateway_url: String,
- channel: RuntimeChannel,
token: String,
harness: Harness,
cwd: PathBuf,
- live: Arc<AtomicBool>,
+ connected: watch::Sender<bool>,
) {
- tracing::info!("harness bridge starting");
- // A bridge that ends cleanly is a runtime that is done being asked for
- // anything; the next delivery dials again.
- match harness::bridge(&harness, &cwd, channel).await {
- Ok(()) => tracing::info!("harness bridge ended"),
- Err(error) => {
- tracing::warn!(error = ?error, "harness bridge ended with an error");
- rebuild(&gateway_url, &token, &harness, &cwd).await;
+ let mut backoff = reconnect_strategy();
+ loop {
+ match tokio::time::timeout(CONNECT_TIMEOUT, link::dial(&gateway_url, &token)).await {
+ Ok(Ok(channel)) => {
+ connected.send_replace(true);
+ tracing::info!("harness bridge starting");
+ let started = Instant::now();
+ match harness::bridge(&harness, &cwd, channel).await {
+ Ok(()) => tracing::info!("harness bridge ended; reconnecting"),
+ Err(error) => {
+ tracing::warn!(error = ?error, "harness bridge ended; reconnecting");
+ }
+ }
+ connected.send_replace(false);
+ if started.elapsed() >= MAX_RECONNECT_DELAY {
+ backoff = reconnect_strategy();
+ }
+ }
+ Ok(Err(error)) => {
+ if !worth_redialing(&error) {
+ tracing::error!(error = ?error, "runtime connection refused; check configuration or re-pair");
+ return;
+ }
+ tracing::warn!(error = ?error, "runtime connection failed; reconnecting");
+ }
+ Err(_) => tracing::warn!("runtime connection timed out; reconnecting"),
}
+ tokio::time::sleep(backoff.next().unwrap_or(MAX_RECONNECT_DELAY)).await;
}
- live.store(false, Ordering::Release);
}
-/// Dial and serve again, as one retried operation: ending cleanly stops it, as
-/// does a gateway verdict no retry can change.
-async fn rebuild(gateway_url: &str, token: &str, harness: &Harness, cwd: &Path) {
- let outcome = RetryIf::start(
- rebuild_strategy(),
- || async {
- let channel = dial(token, gateway_url, dial_strategy())
- .await
- .map_err(ServeError::Dial)?;
- tracing::info!("harness bridge restarting");
- harness::bridge(harness, cwd, channel)
- .await
- .map_err(ServeError::Bridge)
- },
- worth_rebuilding,
- )
- .await;
-
- match outcome {
- Ok(()) => tracing::info!("harness bridge ended"),
- Err(error) => tracing::warn!(
- error = ?error,
- "could not keep a harness bridge up; waiting for the next delivery"
- ),
- }
-}
-
-/// Why serving the runtime stopped.
-#[derive(Debug, thiserror::Error)]
-enum ServeError {
- #[error(transparent)]
- Dial(tungstenite::Error),
- #[error(transparent)]
- Bridge(harness::BridgeError),
-}
-
-/// Whether rebuilding could plausibly go better. A failed bridge says nothing
-/// about the next one, so only the gateway's own verdict stops this.
-fn worth_rebuilding(error: &ServeError) -> bool {
- match error {
- ServeError::Dial(error) => worth_redialing(error),
- ServeError::Bridge(_) => true,
- }
-}
-
-/// Dial the gateway, retrying on the failures a retry can fix.
-async fn dial(
- token: &str,
- gateway_url: &str,
- strategy: impl IntoIterator<Item = Duration>,
-) -> Result<RuntimeChannel, tungstenite::Error> {
- RetryIf::start(strategy, || link::dial(gateway_url, token), worth_redialing).await
-}
-
-/// Whether dialing again could plausibly answer differently. A 4xx is the
-/// gateway's verdict on this harness - credentials refused or revoked - and
-/// asking again only repeats it.
+/// Retry transport failures, server errors, and temporary HTTP refusals.
+/// Other 4xx responses require an operator to fix configuration or credentials.
fn worth_redialing(error: &tungstenite::Error) -> bool {
- if let tungstenite::Error::Http(response) = error
- && response.status().is_client_error()
- {
- if response.status() == tungstenite::http::StatusCode::UNAUTHORIZED
- || response.status() == tungstenite::http::StatusCode::FORBIDDEN
- {
- tracing::error!(
- status = %response.status(),
- "the gateway refused this harness's credentials; press p to re-pair"
- );
- }
+ if matches!(error, tungstenite::Error::Url(_)) {
return false;
}
- true
-}
-
-fn dial_strategy() -> impl Iterator<Item = Duration> {
- FixedInterval::new(DIAL_RETRY_DELAY).take(DIAL_ATTEMPTS - 1)
+ let tungstenite::Error::Http(response) = error else {
+ return true;
+ };
+ let status = response.status();
+ !status.is_client_error()
+ || status == tungstenite::http::StatusCode::REQUEST_TIMEOUT
+ || status == tungstenite::http::StatusCode::TOO_MANY_REQUESTS
}
-fn rebuild_strategy() -> impl Iterator<Item = Duration> {
+fn reconnect_strategy() -> ExponentialBackoff {
ExponentialBackoff::from_millis(2)
.factor(500)
- .take(REBUILD_ATTEMPTS - 1)
+ .max_delay(MAX_RECONNECT_DELAY)
}
diff --git a/crates/coding_agent_worker/src/runtime/test.rs b/crates/coding_agent_worker/src/runtime/test.rs
new file mode 100644
index 0000000000..00c41906aa
--- /dev/null
+++ b/crates/coding_agent_worker/src/runtime/test.rs
@@ -0,0 +1,135 @@
+use super::*;
+use crate::config::HarnessScope;
+use futures::StreamExt as _;
+use harness_id::HarnessId;
+use tokio::net::TcpListener;
+use tokio_tungstenite::accept_async;
+
+fn start_runtime(listener: &TcpListener) -> Runtime {
+ Runtime::start(
+ &MacroApi {
+ api_url: format!("http://{}", listener.local_addr().unwrap()),
+ storage_url: "http://unused".to_owned(),
+ web_url: "http://unused".to_owned(),
+ },
+ &HarnessCredentials {
+ harness_id: HarnessId::TEST_A,
+ token: "mhns_test".to_owned(),
+ scope: HarnessScope::User,
+ },
+ Harness {
+ command: "cat".to_owned(),
+ args: Vec::new(),
+ },
+ Path::new("/"),
+ )
+}
+
+#[tokio::test]
+async fn retries_transient_failure_and_closed_connection_without_triggers() {
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let runtime = start_runtime(&listener);
+ let (socket, _) = listener.accept().await.unwrap();
+ let _ = tokio_tungstenite::accept_hdr_async(
+ socket,
+ |_: &tungstenite::handshake::server::Request, _| {
+ Err(tungstenite::http::Response::builder()
+ .status(429)
+ .body(None)
+ .unwrap())
+ },
+ )
+ .await;
+
+ let (socket, _) = tokio::time::timeout(Duration::from_secs(3), listener.accept())
+ .await
+ .expect("retry startup without an agent trigger")
+ .unwrap();
+ let mut socket = accept_async(socket).await.unwrap();
+ runtime.ensure_connected().await.unwrap();
+ // Concurrent deliveries must share the existing connection.
+ let (first, second) = tokio::join!(runtime.ensure_connected(), runtime.ensure_connected());
+ first.unwrap();
+ second.unwrap();
+ assert!(
+ tokio::time::timeout(Duration::from_millis(50), listener.accept())
+ .await
+ .is_err()
+ );
+
+ socket.close(None).await.unwrap();
+ while socket.next().await.is_some() {}
+ let (socket, _) = tokio::time::timeout(Duration::from_secs(5), listener.accept())
+ .await
+ .expect("reconnect after the gateway closes while idle")
+ .unwrap();
+ let _socket = accept_async(socket).await.unwrap();
+ runtime.ensure_connected().await.unwrap();
+}
+
+#[tokio::test]
+async fn refused_credentials_stop_reconnecting() {
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let runtime = start_runtime(&listener);
+ let (socket, _) = listener.accept().await.unwrap();
+ let _ = tokio_tungstenite::accept_hdr_async(
+ socket,
+ |_: &tungstenite::handshake::server::Request, _| {
+ Err(tungstenite::http::Response::builder()
+ .status(401)
+ .body(None)
+ .unwrap())
+ },
+ )
+ .await;
+
+ let error = tokio::time::timeout(Duration::from_secs(1), runtime.ensure_connected())
+ .await
+ .expect("permanent refusal should finish without waiting for a retry")
+ .unwrap_err();
+ assert!(matches!(error, tungstenite::Error::ConnectionClosed));
+ assert!(runtime._task.is_finished());
+}
+
+#[tokio::test(start_paused = true)]
+async fn dispatch_does_not_wait_forever_for_a_connection() {
+ let (_sender, connected) = watch::channel(false);
+ let runtime = Runtime {
+ connected,
+ _task: AbortOnDropHandle::new(tokio::spawn(std::future::pending())),
+ };
+ let error = runtime.ensure_connected().await.unwrap_err();
+ assert!(
+ matches!(error, tungstenite::Error::Io(error) if error.kind() == std::io::ErrorKind::TimedOut)
+ );
+}
+
+#[test]
+fn retry_policy_distinguishes_temporary_refusals_from_invalid_credentials() {
+ for (status, retry) in [
+ (401, false),
+ (403, false),
+ (404, false),
+ (408, true),
+ (429, true),
+ (503, true),
+ ] {
+ let error = tungstenite::Error::Http(Box::new(
+ tungstenite::http::Response::builder()
+ .status(status)
+ .body(None)
+ .unwrap(),
+ ));
+ assert_eq!(worth_redialing(&error), retry, "HTTP {status}");
+ }
+ assert!(worth_redialing(&tungstenite::Error::ConnectionClosed));
+ assert!(worth_redialing(&tungstenite::Error::Io(
+ std::io::ErrorKind::ConnectionRefused.into(),
+ )));
+ for error in [
+ tungstenite::error::UrlError::UnsupportedUrlScheme,
+ tungstenite::error::UrlError::UnableToConnect("invalid credential header".to_owned()),
+ ] {
+ assert!(!worth_redialing(&tungstenite::Error::Url(error)));
+ }
+}
diff --git a/crates/coding_agent_worker/src/tui/ui/pages/overview.rs b/crates/coding_agent_worker/src/tui/ui/pages/overview.rs
index 9e80cfa39e..d345898d33 100644
--- a/crates/coding_agent_worker/src/tui/ui/pages/overview.rs
+++ b/crates/coding_agent_worker/src/tui/ui/pages/overview.rs
@@ -47,7 +47,7 @@ pub(super) fn render(frame: &mut Frame, app: &App, area: Rect) {
match &app.harness_process {
Some((pid, name)) => Span::raw(format!("{name} · pid {pid}")),
None => Span::styled(
- "not spawned - starts on the first trigger",
+ "not running - check connection and Logs",
Style::new().fg(DIM).italic(),
),
},
@@ -56,7 +56,7 @@ pub(super) fn render(frame: &mut Frame, app: &App, area: Rect) {
lines.push(Line::raw(""));
lines.push(Line::styled(
if app.serving() {
- "Serving from this window; the dot turns green once a session dials in."
+ "Serving from this window; the dot turns green when the runtime connects."
} else {
"The daemon is not running; check the Logs tab for why."
},
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index edd1f84a03..306ec5e1bc 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -708,7 +708,9 @@ but unconnected app gets a tool result saying so, and the agent's reply renders
`Agents` → `Create agent` (or edit an existing agent) opens runtime selectors.
The model list is loaded live and independently for In-memory, connected Cursor, and every
-registered macrod harness. A harness can show `Loading models…`, an unsupported message, or
+registered macrod harness. The selected harness stays selected when the list refreshes.
+A paired macrod connects on startup, so models can load before any agents are bound.
+A harness can show `Loading models…`, an unsupported message, or
a retryable error without hiding the other harnesses. Editing preserves a saved model that
is no longer offered and labels it `saved, unavailable`. A macrod with no responding runtime
can remain loading until the 10-second discovery timeout; use Retry after reconnecting it.