Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(macrod): reattach native sessions after macrod reconnects (#9225)
Wolf Mermelsteind140e0667e37
diff --git a/.sqlx/query-03c32e58e85365fdb5339194dc3c24a2fdc0c8dc6b0a4b7574e024db739193ed.json b/.sqlx/query-03c32e58e85365fdb5339194dc3c24a2fdc0c8dc6b0a4b7574e024db739193ed.jsonnew file mode 100644index 0000000000..36638b390c--- /dev/null+++ b/.sqlx/query-03c32e58e85365fdb5339194dc3c24a2fdc0c8dc6b0a4b7574e024db739193ed.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT s.id\n FROM agent_session s\n JOIN agent_configs ac ON ac.bot_id = s.bot_id\n JOIN harnesses h ON h.id = ac.harness_id\n WHERE ac.harness_id = $1\n AND h.deleted_at IS NULL\n AND s.acp_session_id = ANY($2)\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "TextArray"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "03c32e58e85365fdb5339194dc3c24a2fdc0c8dc6b0a4b7574e024db739193ed"+}diff --git a/crates/agent_harness/AGENTS.md b/crates/agent_harness/AGENTS.mdindex 0c4e612b5e..d073a1feda 100644--- a/crates/agent_harness/AGENTS.md+++ b/crates/agent_harness/AGENTS.md@@ -69,6 +69,14 @@ Remember successful announcements. After 24 hours, recover replies without apersisted completion at most hourly, preserving late answers after lost callbacks.Persisted completions bypass this backoff; direct completion handling is immediate.+External runtimes bind sessions lazily. On each dial `RuntimeRegistry` has the+connection run `initialize` (shared with every session through its handshake)+and ACP `session/list`, and `AgentHarnessService::reattach_live_sessions` runs+`HarnessCommand::Reattach` for the listed sessions bound to that harness,+restoring each (`session/load` or `session/resume`) without a prompt; attached,+archived and managed sessions are skipped. macrod's herdr-acp lists native+sessions whose Herdr agent still runs.+Durable system events use the `agent_session` outbox, distinct from the userpush notifications planned in `domain/notifications.rs`. The serialized sessionworker drains waking batches after a turn; quiet batches join a real prompt.diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rsindex 3dd8bd42fc..a2d8655f35 100644--- a/crates/agent_harness/src/domain/model.rs+++ b/crates/agent_harness/src/domain/model.rs@@ -630,6 +630,10 @@ pub enum HarnessCommand {SetSandboxSize(SandboxSize),/// Release a session's live resources and delete it.Delete,+ /// Load an external session whose agent is still running on a runtime+ /// that dialed in again, so its local activity reaches Macro without a+ /// prompt. Internal: the runtime registry asks for these on each dial.+ Reattach,}/// What executing a command did with it, beyond succeeding.diff --git a/crates/agent_harness/src/domain/ports.rs b/crates/agent_harness/src/domain/ports.rsindex dea99b66a8..99b712efff 100644--- a/crates/agent_harness/src/domain/ports.rs+++ b/crates/agent_harness/src/domain/ports.rs@@ -5,6 +5,7 @@ use std::pin::Pin;use std::sync::Arc;use crate::domain::model::PermissionPolicyConfig;+use agent_client_protocol::schema::v1::SessionId;use agent_session::domain::connection::RuntimeAttachment;use agent_session::domain::model::{AgentMcpServers, AgentSessionId, SandboxSize};use agent_session::domain::ports::AgentConnector;@@ -103,6 +104,15 @@ pub trait HarnessBindings: Send + Sync + 'static {&self,bot: BotId,) -> impl Future<Output = anyhow::Result<Option<HarnessId>>> + Send;++ /// The sessions holding one of `acp_sessions` whose bot is bound to+ /// `harness` now. Scoping by binding is the authorization: a runtime+ /// naming another harness's ACP sessions reattaches nothing.+ fn sessions_on_harness(+ &self,+ harness: HarnessId,+ acp_sessions: &[SessionId],+ ) -> impl Future<Output = anyhow::Result<Vec<AgentSessionId>>> + Send;}/// Loads facts for the domain to resolve a bot's permission policy.@@ -356,8 +366,8 @@ impl<T: HeldToolCallObserver + ?Sized> HeldToolCallObserver for Arc<T> {/// A self-hosted runtime dials once and carries every session its bot is/// serving, so binding a session to a connection happens when work arrives for/// it rather than when the runtime dials. That is what keeps a reconnect cheap:-/// sessions nobody is prompting are never restored at all, and the one being-/// prompted restores itself on the way to being prompted.+/// only sessions the runtime reports as still running are restored when it+/// dials, and any other one restores itself on the way to being prompted./// Only binding: taking a dialed-in socket into the registry is the inbound/// adapter's business, and the type it hands over is not the type a session/// talks through.@@ -386,6 +396,14 @@ pub trait RuntimeConnections: Send + Sync + 'static {/// Whether this process currently holds `harness`'s physical runtime socket.fn is_connected(&self, harness: HarnessId) -> bool;++ /// The sessions a runtime dialing in for `harness` reported as live, as+ /// [`HarnessBindings::sessions_on_harness`] resolves them.+ fn sessions_on_harness(+ &self,+ harness: HarnessId,+ acp_sessions: &[SessionId],+ ) -> impl Future<Output = anyhow::Result<Vec<AgentSessionId>>> + Send;}/// Mints the one secret a sandbox is given, and the config that points it atdiff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rsindex 00cca0635b..84901bedb7 100644--- a/crates/agent_harness/src/domain/service.rs+++ b/crates/agent_harness/src/domain/service.rs@@ -684,6 +684,58 @@ whereself.enqueue(session_id, command, false)}+ /// Load the sessions a runtime that dialed in again still serves.+ ///+ /// Only sessions whose bot is bound to `harness` are touched, each on its+ /// own command worker, so a reattach waits behind whatever that session+ /// is already doing. One session failing never stops the others.+ #[tracing::instrument(+ name = "agent.runtime.reattach",+ skip_all,+ fields(+ %harness,+ agent.runtime.live_sessions = acp_sessions.len(),+ agent.runtime.reattach_sessions = tracing::field::Empty,+ agent.runtime.reattach_failures = tracing::field::Empty,+ )+ )]+ pub async fn reattach_live_sessions(+ &self,+ harness: harness_id::HarnessId,+ acp_sessions: Vec<agent_client_protocol::schema::v1::SessionId>,+ cause: tracing::Span,+ ) {+ tracing::Span::current().follows_from(&cause);+ let sessions = match self+ .inner+ .runtimes+ .sessions_on_harness(harness, &acp_sessions)+ .await+ {+ Ok(sessions) => sessions,+ Err(error) => {+ tracing::error!(error = ?error, "could not resolve a runtime's live sessions");+ return;+ }+ };+ let span = tracing::Span::current();+ span.record("agent.runtime.reattach_sessions", sessions.len());+ let results = futures::future::join_all(+ sessions+ .iter()+ .map(|session_id| self.execute(*session_id, HarnessCommand::Reattach)),+ )+ .await;+ let mut failures = 0;+ for (session_id, result) in sessions.iter().zip(results) {+ if let Err(error) = result {+ failures += 1;+ tracing::warn!(error = ?error, %session_id, "could not reattach a live session");+ }+ }+ span.record("agent.runtime.reattach_failures", failures);+ }+/// Post the announcement for a prompt an external runtime delivers.////// The follow-up-mention half of the external split: the runtime sendsdiff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rsindex 59ae17af58..2d4bbb1617 100644--- a/crates/agent_harness/src/domain/service/deliver.rs+++ b/crates/agent_harness/src/domain/service/deliver.rs@@ -111,6 +111,26 @@ whereOk(session)}+ /// Load a live external session onto its runtime's new connection.+ ///+ /// Safe to repeat: a session already attached here keeps its actor, and+ /// an archived or managed one is never started by a runtime's dial.+ #[tracing::instrument(err, skip(self), fields(agent.session.id = %session_id, agent.session.reattached = false))]+ pub(super) async fn reattach(&self, session_id: AgentSessionId) -> Result<()> {+ if self.sessions.attached_mcp_servers(session_id).is_some() {+ return Ok(());+ }+ let session = self.sessions.get_session(session_id).await?;+ if session.is_archived()+ || AgentKind::for_session(session.bot_id, &session.harness) != AgentKind::External+ {+ return Ok(());+ }+ self.reconnect(session_id).await?;+ tracing::Span::current().record("agent.session.reattached", true);+ Ok(())+ }+/// Reconcile a prior invocation before deciding whether a batch may run.async fn recover_batch(&self,diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rsindex d7239d1de9..bf9b265234 100644--- a/crates/agent_harness/src/domain/service/queue.rs+++ b/crates/agent_harness/src/domain/service/queue.rs@@ -439,6 +439,7 @@ where| HarnessCommand::ToolApproval(_)| HarnessCommand::PresentTool { .. }| HarnessCommand::SessionStopped { .. }+ | HarnessCommand::Reattach| HarnessCommand::Delete => {}HarnessCommand::SetSandboxSize(_) => {if self.sessions.get_session(session_id).await?.is_archived() {@@ -889,6 +890,10 @@ whereself.apply_sandbox_size(session_id, size).await?;Ok(CommandOutcome::Completed)}+ HarnessCommand::Reattach => {+ self.reattach(session_id).await?;+ Ok(CommandOutcome::Completed)+ }HarnessCommand::Delete => {self.notification_finished.lock()diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rsindex 84d9dcda44..2b0064e4dd 100644--- a/crates/agent_harness/src/domain/service/test.rs+++ b/crates/agent_harness/src/domain/service/test.rs@@ -304,6 +304,18 @@ impl crate::domain::ports::HarnessBindings for MirrorBindings {async fn harness_for(&self, bot: BotId) -> anyhow::Result<Option<harness_id::HarnessId>> {Ok(Some(harness_for_bot(bot)))}++ /// Mirrored too: an ACP session id that is a session's own id names it.+ async fn sessions_on_harness(+ &self,+ _harness: harness_id::HarnessId,+ acp_sessions: &[agent_client_protocol::schema::v1::SessionId],+ ) -> anyhow::Result<Vec<AgentSessionId>> {+ Ok(acp_sessions+ .iter()+ .filter_map(|acp| acp.to_string().parse().ok())+ .collect())+ }}/// No agent has said anything about permissions: every bot runs under its@@ -4600,6 +4612,7 @@ async fn codex_channel_mention_provisions_egress_without_advertising_mcp() {assert!(request.mcp_servers.is_empty());}+mod reattach;mod reopen;/// The owner's reachable repositories, as the GitHub App would list them.diff --git a/crates/agent_harness/src/domain/service/test/reattach.rs b/crates/agent_harness/src/domain/service/test/reattach.rsnew file mode 100644index 0000000000..61e3f7db4c--- /dev/null+++ b/crates/agent_harness/src/domain/service/test/reattach.rs@@ -0,0 +1,276 @@+//! A runtime that dials in again reports the sessions its agents still serve,+//! and the harness loads them without anyone prompting.++use super::*;+use agent_client_protocol::schema::v1::LoadSessionResponse;++/// A session the runtime had opened before its connection dropped. Its ACP+/// session id is its own id, which is how [`MirrorBindings`] resolves it.+async fn native_session(+ service: &TestHarness,+ repo: &InMemoryAgentSessionRepo,+ bot_id: BotId,+) -> AgentSessionId {+ let mut request = open_external_request("/srv/agent");+ request.bot_id = bot_id;+ let session = service.open_external_session(request).await.unwrap();+ repo.set_acp_session_id(session.id, SessionId::new(session.id.to_string()))+ .await+ .unwrap();+ session.id+}++/// Initialize the connection for an agent that can load sessions.+async fn initializes_loading(runtime: &ContainerMock) {+ let agent = runtime.agent();+ runtime.sends_ready();+ agent.wait_for_requests(1).await;+ agent.completes_initialize(+ InitializeResponse::new(PROTOCOL_VERSION)+ .agent_capabilities(AgentCapabilities::new().load_session(true)),+ );+}++/// The ACP sessions the harness asked this agent to load, in order.+fn loaded(agent: &FakeAgent) -> Vec<String> {+ agent+ .received_requests()+ .into_iter()+ .filter_map(|request| match request {+ ClientRequest::LoadSessionRequest(load) => Some(load.session_id.to_string()),+ _ => None,+ })+ .collect()+}++#[tokio::test]+async fn a_redial_loads_a_live_session_without_a_prompt() {+ let (service, repo, containers, _announcer, runtimes) = harness();+ let bot_id = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());+ let session = native_session(&service, &repo, bot_id).await;+ let runtime = ContainerMock::default();+ runtimes.attach(harness_for_bot(bot_id), runtime.clone());++ let drive = async {+ initializes_loading(&runtime).await;+ let agent = runtime.agent();+ agent.wait_for_requests(2).await;+ agent.loads_session(LoadSessionResponse::new());+ };+ tokio::join!(+ service.reattach_live_sessions(+ harness_for_bot(bot_id),+ vec![SessionId::new(session.to_string())],+ tracing::Span::none(),+ ),+ drive+ );++ let agent = runtime.agent();+ assert_eq!(loaded(&agent), [session.to_string()]);+ assert!(prompts(&agent).is_empty(), "reattaching never prompts");+ assert_eq!(containers.spawned(), 0);+ assert_eq!(containers.resumed(), 0);++ // The runtime reports the same session again, as a retry would: the+ // attached session keeps its actor and nothing reaches the agent.+ service+ .reattach_live_sessions(+ harness_for_bot(bot_id),+ vec![SessionId::new(session.to_string())],+ tracing::Span::none(),+ )+ .await;+ assert_eq!(agent.received_requests().len(), 2);+}++#[tokio::test]+async fn a_redial_loads_every_live_session_but_not_archived_or_unknown_ones() {+ let (service, repo, _containers, _announcer, runtimes) = harness();+ let bot_id = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());+ let first = native_session(&service, &repo, bot_id).await;+ let second = native_session(&service, &repo, bot_id).await;+ let archived = native_session(&service, &repo, bot_id).await;+ agent_session::domain::ports::AgentSessionRepo::set_archived(&repo, archived, true)+ .await+ .unwrap();+ let runtime = ContainerMock::default();+ runtimes.attach(harness_for_bot(bot_id), runtime.clone());++ let drive = async {+ initializes_loading(&runtime).await;+ let agent = runtime.agent();+ agent.wait_for_requests(3).await;+ for frame in agent.received_frames() {+ if let RawJsonRpcMessage::Request(request) = frame+ && request.method.as_ref() == "session/load"+ {+ agent.sends_reply(request.id, LoadSessionResponse::new());+ }+ }+ };+ tokio::join!(+ service.reattach_live_sessions(+ harness_for_bot(bot_id),+ vec![+ SessionId::new(first.to_string()),+ SessionId::new(second.to_string()),+ SessionId::new(archived.to_string()),+ SessionId::new(AgentSessionId::new().to_string()),+ ],+ tracing::Span::none(),+ ),+ drive+ );++ let mut loaded = loaded(&runtime.agent());+ loaded.sort();+ let mut expected = vec![first.to_string(), second.to_string()];+ expected.sort();+ assert_eq!(loaded, expected);+}++#[tokio::test]+async fn a_live_session_on_a_runtime_that_is_gone_is_left_for_the_next_dial() {+ let (service, repo, _containers, _announcer, _runtimes) = harness();+ let bot_id = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());+ let session = native_session(&service, &repo, bot_id).await;++ // The report raced the connection's end: nothing is attached and the+ // session stays reattachable when the runtime dials again.+ service+ .reattach_live_sessions(+ harness_for_bot(bot_id),+ vec![SessionId::new(session.to_string())],+ tracing::Span::none(),+ )+ .await;+ assert!(+ agent_session::domain::service::AgentSessionService::attached_mcp_servers(+ &service.inner.sessions,+ session+ )+ .is_none()+ );+}++/// A harness whose runtime registry lists each dial's sessions, as in+/// production, with the reports the service would consume.+fn listing_harness() -> (+ TestHarness,+ InMemoryAgentSessionRepo,+ Arc<RuntimeRegistry<ContainerSender>>,+ mpsc::UnboundedReceiver<crate::outbound::runtime_registry::LiveRuntimeSessions>,+) {+ let repo = InMemoryAgentSessionRepo::new();+ let turn_observer = Arc::new(agent_session::domain::ports::LateBoundTurnObserver::new());+ let (live_sessions, reports) = mpsc::unbounded_channel();+ let runtimes = RuntimeRegistry::reporting_live_sessions(live_sessions);+ let service = AgentHarnessService::new(+ None,+ AgentSessionServiceImpl::new(+ repo.clone(),+ FoldedMessageService::new(repo.clone()),+ NoOpRealtime,+ NoOpAgentSessionNameGenerator,+ turn_observer.clone(),+ Arc::new(RecordingLifecyclePublisher::new()),+ ReplicaId::mint(),+ ),+ MockContainerManager::new(),+ AnnouncerMock::new(),+ TestConnections::new(MirrorBindings, Arc::clone(&runtimes)),+ PromptContextMock::default(),+ PromptComposerMock::default(),+ EgressProvisionerMock::new(),+ NoPeers,+ KindDefaultPolicies,+ HarnessDefaultCodingAgents,+ HarnessDefaults::new(SessionDefaults {+ bot_id: BotId::TEST_A,+ model: "claude".to_owned(),+ harness: "opencode".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),+ }),+ RecordingLifecyclePublisher::new(),+ crate::domain::pending::PendingCommands::new(),+ PromptMentionsMock::new(),+ NotifierMock::new(),+ );+ turn_observer.bind(NoOpTurnObserver);+ (service, repo, runtimes, reports)+}++/// An adapter that lists every session it has ever stored, the way+/// `@agentclientprotocol/claude-agent-acp` 0.73.0 answers `session/list`:+/// Macro's sessions on this harness, archived ones, another harness's, ones+/// Macro never created, and ids that are not Macro's at all. Only the live,+/// unarchived sessions bound to the dialing harness are restored.+#[tokio::test]+async fn a_dial_restores_only_this_harness_tracked_sessions_from_an_agent_that_lists_everything() {+ let (service, repo, runtimes, mut reports) = listing_harness();+ let bot_id = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());+ let other_bot = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());+ let tracked = native_session(&service, &repo, bot_id).await;+ let archived = native_session(&service, &repo, bot_id).await;+ agent_session::domain::ports::AgentSessionRepo::set_archived(&repo, archived, true)+ .await+ .unwrap();+ let elsewhere = native_session(&service, &repo, other_bot).await;+ let untracked = AgentSessionId::new();+ let runtime = ContainerMock::default();+ runtimes.attach(harness_for_bot(bot_id), runtime.clone());+ runtime.sends_ready();++ let agent = runtime.agent();+ agent.wait_for_requests(1).await;+ agent.completes_initialize(+ InitializeResponse::new(PROTOCOL_VERSION).agent_capabilities(+ AgentCapabilities::new()+ .load_session(true)+ .session_capabilities(+ SessionCapabilities::new()+ .resume(SessionResumeCapabilities::new())+ .list(agent_client_protocol::schema::v1::SessionListCapabilities::new()),+ ),+ ),+ );+ agent.wait_for_requests(2).await;+ let Some(RawJsonRpcMessage::Request(list)) = agent.received_frames().into_iter().nth(1) else {+ panic!("expected session/list after initialize");+ };+ assert_eq!(list.method.as_ref(), "session/list");+ agent.sends_reply(+ list.id,+ serde_json::json!({"sessions": [+ {"sessionId": tracked.to_string(), "cwd": "/srv/agent", "title": "Fix the build", "updatedAt": "2026-11-02T10:00:00Z"},+ {"sessionId": archived.to_string(), "cwd": "/srv/agent", "title": "Old work", "updatedAt": "2026-10-01T10:00:00Z"},+ {"sessionId": elsewhere.to_string(), "cwd": "/srv/agent", "title": "Other laptop", "updatedAt": "2026-11-01T10:00:00Z"},+ {"sessionId": untracked.to_string(), "cwd": "/home/operator", "title": "Started in a terminal", "updatedAt": "2026-11-02T09:00:00Z"},+ {"sessionId": "claude-local-7f3a", "cwd": "/home/operator/scratch", "title": "Not Macro's", "updatedAt": "2026-10-20T09:00:00Z"},+ ]}),+ );+ let report = reports.recv().await.expect("the dial reports its listing");+ assert_eq!(report.acp_sessions.len(), 5);++ let drive = async {+ agent.wait_for_requests(3).await;+ agent.resumes_session(ResumeSessionResponse::new());+ };+ tokio::join!(+ service.reattach_live_sessions(report.harness, report.acp_sessions, report.span),+ drive+ );++ let requests = agent.received_requests();+ assert_eq!(+ requests.len(),+ 3,+ "only one session is restored: {requests:?}"+ );+ assert!(matches!(+ &requests[2],+ ClientRequest::ResumeSessionRequest(request) if request.session_id.to_string() == tracked.to_string()+ ));+ assert!(prompts(&agent).is_empty(), "restoring never prompts");+}diff --git a/crates/agent_harness/src/inbound/runtime_gateway.rs b/crates/agent_harness/src/inbound/runtime_gateway.rsindex d171c9825b..6eff648188 100644--- a/crates/agent_harness/src/inbound/runtime_gateway.rs+++ b/crates/agent_harness/src/inbound/runtime_gateway.rs@@ -9,8 +9,9 @@//! socket and one harness process serve every session of every agent bound to//! the harness. Which sessions those are is not something the dial declares://! the runtime dials once, and sessions are bound to the connection as work-//! arrives for them. A session nobody is prompting costs nothing, and one-//! prompted after a reconnect restores itself on the way to being prompted.+//! arrives for them. The registry initializes each dial and calls ACP+//! `session/list`, and the harness loads the sessions listed; any other+//! session prompted after a reconnect restores itself on the way.//!//! The token is the whole gate: a valid, unrevoked credential on a live//! (undeleted) harness is exactly what the authorizer verifies, so a revokeddiff --git a/crates/agent_harness/src/outbound/runtime_registry.rs b/crates/agent_harness/src/outbound/runtime_registry.rsindex c6f5e34828..9025abb4d5 100644--- a/crates/agent_harness/src/outbound/runtime_registry.rs+++ b/crates/agent_harness/src/outbound/runtime_registry.rs@@ -10,6 +10,9 @@//! - across many bots - share one harness process.use std::sync::Arc;+use std::time::Duration;++use agent_client_protocol::schema::v1::SessionId;use agent_runtime_protocol::domain::ports::{Transport, TransportSender};use agent_runtime_protocol::domain::schema::v0::{ToRuntimeMessage, ToServerMessage};@@ -18,16 +21,32 @@ use agent_session::domain::model::AgentSessionId;use bot_id::BotId;use dashmap::DashMap;use harness_id::HarnessId;+use tokio::sync::mpsc;use crate::domain::ports::{HarnessBindings, HarnessPresence, RuntimeConnections};#[cfg(test)]mod test;+/// How long a dialed-in runtime has to list the sessions it still serves.+const LIVE_SESSIONS_TIMEOUT: Duration = Duration::from_secs(30);++/// ACP sessions a runtime that just dialed in for `harness` still serves.+#[derive(Debug, Clone)]+pub struct LiveRuntimeSessions {+ /// The harness the runtime dialed in for.+ pub harness: HarnessId,+ /// Its agents' live ACP sessions.+ pub acp_sessions: Vec<SessionId>,+ /// The span that asked, for reattaching to follow from.+ pub span: tracing::Span,+}+/// Every harness's live runtime connection.pub struct RuntimeRegistry<Sender> {connections: DashMap<HarnessId, Arc<RuntimeConnection<Sender>>>,presence: Option<Arc<dyn HarnessPresence>>,+ live_sessions: Option<mpsc::UnboundedSender<LiveRuntimeSessions>>,}impl<Sender> Default for RuntimeRegistry<Sender> {@@ -35,6 +54,7 @@ impl<Sender> Default for RuntimeRegistry<Sender> {Self {connections: DashMap::new(),presence: None,+ live_sessions: None,}}}@@ -49,12 +69,29 @@ whereArc::new(Self::default())}- /// An empty registry that reports attach/detach to `presence`.+ /// An empty registry that reports attach/detach to `presence` and the+ /// sessions each dialed-in runtime still serves to `live_sessions`.#[must_use]- pub fn with_presence(presence: Arc<dyn HarnessPresence>) -> Arc<Self> {+ pub fn with_presence(+ presence: Arc<dyn HarnessPresence>,+ live_sessions: mpsc::UnboundedSender<LiveRuntimeSessions>,+ ) -> Arc<Self> {Arc::new(Self {connections: DashMap::new(),presence: Some(presence),+ live_sessions: Some(live_sessions),+ })+ }++ /// An empty registry that reports the sessions each dialed-in runtime+ /// still serves to `live_sessions`.+ #[must_use]+ pub fn reporting_live_sessions(+ live_sessions: mpsc::UnboundedSender<LiveRuntimeSessions>,+ ) -> Arc<Self> {+ Arc::new(Self {+ live_sessions: Some(live_sessions),+ ..Self::default()})}@@ -92,7 +129,9 @@ where/// Take over as `harness`'s connection, displacing whatever it had.////// Last dial wins: a runtime that redials has necessarily lost its old- /// socket, and the sessions it was carrying rebind as they are next used.+ /// socket. Sessions whose agents outlived it are reported to+ /// `live_sessions` to be loaded again; the rest rebind as they are next+ /// used.pub fn attach<Carrier>(self: &Arc<Self>, harness: HarnessId, carrier: Carrier)whereCarrier: Transport<ToRuntimeMessage, ToServerMessage, Sender = Sender>,@@ -109,6 +148,13 @@ whereif let Some(presence) = &self.presence {tokio::spawn(Arc::clone(presence).connected(harness));}+ if let Some(live_sessions) = &self.live_sessions {+ tokio::spawn(report_live_sessions(+ harness,+ Arc::clone(&connection),+ live_sessions.clone(),+ ));+ }self.watch_for_close(harness, connection);}@@ -155,6 +201,45 @@ where}}+/// Initialize a new connection and ask which sessions its runtime serves.+///+/// An agent that does not advertise `session/list` lists none; an error+/// listing is not worth more than a debug line either.+#[tracing::instrument(+ name = "agent.runtime.live_sessions",+ skip_all,+ fields(%harness, agent.runtime.live_sessions = tracing::field::Empty)+)]+async fn report_live_sessions<Sender>(+ harness: HarnessId,+ connection: Arc<RuntimeConnection<Sender>>,+ live_sessions: mpsc::UnboundedSender<LiveRuntimeSessions>,+) where+ Sender: TransportSender<ToRuntimeMessage>,+{+ let acp_sessions =+ match tokio::time::timeout(LIVE_SESSIONS_TIMEOUT, connection.list_sessions()).await {+ Ok(Ok(acp_sessions)) => acp_sessions,+ Ok(Err(error)) => {+ tracing::debug!(error = %error, "the runtime listed no live sessions");+ return;+ }+ Err(_) => {+ tracing::warn!("the runtime did not list its live sessions in time");+ return;+ }+ };+ tracing::Span::current().record("agent.runtime.live_sessions", acp_sessions.len());+ if acp_sessions.is_empty() {+ return;+ }+ let _ = live_sessions.send(LiveRuntimeSessions {+ harness,+ acp_sessions,+ span: tracing::Span::current(),+ });+}+/// [`RuntimeConnections`] over the harness-keyed registry.////// The domain binds sessions by bot; this resolves the bot's *current*@@ -207,4 +292,14 @@ wherefn is_connected(&self, harness: HarnessId) -> bool {self.registry.is_connected(harness)}++ async fn sessions_on_harness(+ &self,+ harness: HarnessId,+ acp_sessions: &[SessionId],+ ) -> anyhow::Result<Vec<AgentSessionId>> {+ self.bindings+ .sessions_on_harness(harness, acp_sessions)+ .await+ }}diff --git a/crates/agent_harness/src/outbound/runtime_registry/test.rs b/crates/agent_harness/src/outbound/runtime_registry/test.rsindex a5e7bb3486..27978644f9 100644--- a/crates/agent_harness/src/outbound/runtime_registry/test.rs+++ b/crates/agent_harness/src/outbound/runtime_registry/test.rs@@ -27,6 +27,14 @@ impl HarnessBindings for Arc<FakeBindings> {async fn harness_for(&self, bot: BotId) -> anyhow::Result<Option<HarnessId>> {Ok(self.bindings.lock().unwrap().get(&bot).copied())}++ async fn sessions_on_harness(+ &self,+ _harness: HarnessId,+ _acp_sessions: &[SessionId],+ ) -> anyhow::Result<Vec<AgentSessionId>> {+ Ok(Vec::new())+ }}fn connections(@@ -238,8 +246,10 @@ async fn attach_and_close_report_presence() {}let presence = Arc::new(RecordingPresence::default());- let registry: Arc<RuntimeRegistry<ContainerSender>> =- RuntimeRegistry::with_presence(Arc::clone(&presence) as Arc<dyn HarnessPresence>);+ let registry: Arc<RuntimeRegistry<ContainerSender>> = RuntimeRegistry::with_presence(+ Arc::clone(&presence) as Arc<dyn HarnessPresence>,+ mpsc::unbounded_channel().0,+ );let runtime = ContainerMock::default();registry.attach(HarnessId::TEST_A, runtime.clone());let connection = registry@@ -264,3 +274,48 @@ async fn attach_and_close_report_presence() {assert!(events.contains(&(HarnessId::TEST_A, "connected")));assert!(events.contains(&(HarnessId::TEST_A, "disconnected")));}++#[tokio::test]+async fn a_dial_initializes_and_reports_the_sessions_its_runtime_lists() {+ let (live_sessions, mut reports) = mpsc::unbounded_channel();+ let registry = RuntimeRegistry::<ContainerSender>::reporting_live_sessions(live_sessions);+ let runtime = ContainerMock::default();+ registry.attach(HarnessId::TEST_A, runtime.clone());+ runtime.sends_ready();++ let agent = runtime.agent();+ agent.wait_for_requests(1).await;+ agent.completes_initialize(+ agent_client_protocol::schema::v1::InitializeResponse::new(agent_session::PROTOCOL_VERSION)+ .agent_capabilities(+ agent_client_protocol::schema::v1::AgentCapabilities::new()+ .load_session(true)+ .session_capabilities(+ agent_client_protocol::schema::v1::SessionCapabilities::new().list(+ agent_client_protocol::schema::v1::SessionListCapabilities::new(),+ ),+ ),+ ),+ );+ agent.wait_for_requests(2).await;+ let Some(agent_client_protocol::RawJsonRpcMessage::Request(list)) =+ agent.received_frames().into_iter().nth(1)+ else {+ panic!("expected session/list after initialize");+ };+ assert_eq!(list.method.as_ref(), "session/list");+ agent.sends_reply(+ list.id,+ serde_json::json!({"sessions": [+ {"sessionId": "acp-1", "cwd": "/srv"},+ {"sessionId": "acp-2", "cwd": "/srv"},+ ]}),+ );++ let report = reports.recv().await.expect("the dial reports");+ assert_eq!(report.harness, HarnessId::TEST_A);+ assert_eq!(+ report.acp_sessions,+ [SessionId::from("acp-1"), SessionId::from("acp-2")]+ );+}diff --git a/crates/agent_harness/src/testing/helpers/agent.rs b/crates/agent_harness/src/testing/helpers/agent.rsindex 20de05d6e7..7b1e55ff68 100644--- a/crates/agent_harness/src/testing/helpers/agent.rs+++ b/crates/agent_harness/src/testing/helpers/agent.rs@@ -309,6 +309,15 @@ impl FakeAgent {);progress.opening = Some(request.id.clone());}+ // The connection lists sessions right after it initializes.+ ClientRequest::ListSessionsRequest(_) => {+ assert_ne!(+ progress.stage,+ Stage::Fresh,+ "harness sent {} before initialize completed",+ request.method+ );+ }ClientRequest::PromptRequest(_) => {assert_eq!(progress.stage,diff --git a/crates/agent_session/src/domain/connection.rs b/crates/agent_session/src/domain/connection.rsindex afdee7be7e..ad9128e82e 100644--- a/crates/agent_session/src/domain/connection.rs+++ b/crates/agent_session/src/domain/connection.rs@@ -18,12 +18,14 @@//! session-scoped request or notification matches the session holding that ACP//! session id, and system events belong to the connection itself.-use std::collections::HashMap;-use std::sync::atomic::{AtomicBool, Ordering};+use std::collections::{HashMap, HashSet};use std::sync::{Arc, OnceLock};-use agent_client_protocol::RawJsonRpcMessage;-use agent_client_protocol::schema::v1::{McpServer, RequestId, Response, SessionId};+use agent_client_protocol::schema::v1::{+ InitializeResponse, ListSessionsRequest, ListSessionsResponse, McpServer, RequestId, Response,+ SessionId,+};+use agent_client_protocol::{JsonRpcMessage, RawJsonRpcMessage};use agent_runtime_protocol::domain::ports::{Transport, TransportError, TransportReceiver, TransportSender,};@@ -31,11 +33,13 @@ use agent_runtime_protocol::domain::schema::v0::{AcpMessage, ModelProbeResult, SystemEvent, ToRuntimeMessage, ToServerMessage,};use dashmap::DashMap;-use tokio::sync::{Mutex, mpsc, watch};+use tokio::sync::{Mutex, mpsc, oneshot, watch};use tokio_util::sync::CancellationToken;use crate::domain::model::{AgentSessionId, SessionClaim};-use crate::domain::session::{HandshakeStatus, PermissionPolicy};+use crate::domain::session::{+ HandshakeStatus, InitializationContext, PermissionPolicy, SessionRestoreSupport,+};#[cfg(test)]mod test;@@ -59,6 +63,8 @@ pub(crate) enum Routed {Connection,/// A connection-level model probe response, never session traffic.Probe,+ /// The answer to a request the connection itself sent.+ Answer,/// Nothing owns it. Kept as its own answer rather than folded into/// `Connection` so it can be counted and logged as the anomaly it is.Orphan,@@ -70,6 +76,7 @@ pub(crate) enum Routed {#[derive(Debug, Default)]pub(crate) struct Routes {requests: HashMap<RequestId, AgentSessionId>,+ connection_requests: HashSet<RequestId>,acp_sessions: HashMap<SessionId, AgentSessionId>,}@@ -79,6 +86,11 @@ impl Routes {self.requests.insert(request, session);}+ /// Remember that the connection itself is awaiting an answer to `request`.+ pub(crate) fn expect_connection_response(&mut self, request: RequestId) {+ self.connection_requests.insert(request);+ }+/// Remember that `acp_session` is `session`'s session on the agent.pub(crate) fn bind_acp_session(&mut self, acp_session: SessionId, session: AgentSessionId) {self.acp_sessions.insert(acp_session, session);@@ -104,10 +116,13 @@ impl Routes {};if let Some(id) = frame.response_id() {- return match self.requests.remove(id) {- Some(session) => Routed::Session(session),- None => Routed::Orphan,- };+ if let Some(session) = self.requests.remove(id) {+ return Routed::Session(session);+ }+ if self.connection_requests.remove(id) {+ return Routed::Answer;+ }+ return Routed::Orphan;}match acp_session_of(frame).and_then(|acp| self.acp_sessions.get(&acp)) {@@ -260,12 +275,15 @@ pub struct RuntimeConnection<Sender> {/// sessions that need it mostly do not exist yet: a runtime dials, says/// ready, and only then does someone mention the bot. A session binding/// afterwards has to be able to learn it happened.- runtime_ready: AtomicBool,+ runtime_ready: watch::Sender<bool>,bound: Bound,/// Answers to model probes, broadcast rather than correlated: every probe/// on this connection asks the same parameterless question, so any answer/// serves any waiter.probe_answers: ProbeAnswers,+ /// Waiters for answers to requests the connection itself sent: its+ /// `initialize` and `session/list`.+ answers: DashMap<RequestId, oneshot::Sender<RawJsonRpcMessage>>,routes: Mutex<Routes>,router: OnceLock<tokio::task::AbortHandle>,/// Cancelled once this connection's transport has ended.@@ -295,9 +313,10 @@ wherelet connection = Arc::new(Self {outbound,handshake,- runtime_ready: AtomicBool::new(false),+ runtime_ready: watch::channel(false).0,bound: DashMap::new(),probe_answers: watch::channel(None).0,+ answers: DashMap::new(),routes: Mutex::new(Routes::default()),router: OnceLock::new(),closed: CancellationToken::new(),@@ -310,6 +329,7 @@ where/// Stop serving this connection: it has been displaced by a newer dial.pub fn evict(&self) {self.bound.clear();+ self.answers.clear();// Nothing will answer a probe on a connection that is no longer served.let _ = self.probe_answers.send(None);if let Some(router) = self.router.get() {@@ -364,6 +384,174 @@ where}}+ /// Initialize the connection and list the ACP sessions its agent serves.+ ///+ /// Asked of every new connection: agents that outlived the previous one+ /// keep working, and nothing else would load their sessions until someone+ /// prompted them. Waits for the runtime to report ready, then runs the+ /// connection's one `initialize` and publishes it through the handshake+ /// every session shares - or, when a session already claimed it, waits+ /// for that one. An agent that does not advertise+ /// `sessionCapabilities.list` lists nothing. The caller owns the deadline.+ pub async fn list_sessions(&self) -> Result<Vec<SessionId>, ListSessionsError> {+ let mut ready = self.runtime_ready.subscribe();+ tokio::select! {+ ready = ready.wait_for(|ready| *ready) => {+ ready.map_err(|_| ListSessionsError::Closed)?;+ }+ () = self.closed.cancelled() => return Err(ListSessionsError::Closed),+ }+ let initialized = if self.claim_handshake() {+ let mut claim = HandshakeClaim {+ connection: self,+ held: true,+ };+ let initialized = self.initialize().await?;+ claim.held = false;+ initialized+ } else {+ self.shared_initialize().await?+ };+ if initialized+ .agent_capabilities+ .session_capabilities+ .list+ .is_none()+ {+ return Ok(Vec::new());+ }+ let mut sessions = Vec::new();+ let mut cursor = None;+ for _ in 0..MAX_SESSION_LIST_PAGES {+ let (method, params) = ListSessionsRequest::new()+ .cursor(cursor)+ .to_untyped_message()+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))?+ .into_parts();+ let (_, result) = self.request(method, params).await?;+ let page = serde_json::from_value::<ListSessionsResponse>(result)+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))?;+ sessions.extend(page.sessions.into_iter().map(|session| session.session_id));+ match page.next_cursor {+ Some(next) => cursor = Some(next),+ None => return Ok(sessions),+ }+ }+ Err(ListSessionsError::Runtime(+ "session/list did not end its pages".to_owned(),+ ))+ }++ /// Send this connection's `initialize` and publish its answer to every+ /// session on the connection, as a session that initialized would.+ async fn initialize(&self) -> Result<InitializeResponse, ListSessionsError> {+ let id = connection_request_id();+ let request = crate::domain::session::initialize_request(id.clone())+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))?;+ let (response, result) = self.send_request(id, request.clone()).await?;+ let initialized = serde_json::from_value::<InitializeResponse>(result)+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))?;+ self.handshake+ .send_replace(HandshakeStatus::ReadyWithContext {+ restore: SessionRestoreSupport::of(&initialized),+ context: Arc::new(InitializationContext {+ request: ToRuntimeMessage::Acp(AcpMessage(request)),+ response: ToServerMessage::Acp(AcpMessage(response)),+ }),+ });+ Ok(initialized)+ }++ /// The `initialize` answer a session on this connection received.+ async fn shared_initialize(&self) -> Result<InitializeResponse, ListSessionsError> {+ let mut handshake = self.handshake.subscribe();+ let context = tokio::select! {+ status = handshake.wait_for(|status| matches!(status, HandshakeStatus::ReadyWithContext { .. })) => {+ let status = status.map_err(|_| ListSessionsError::Closed)?;+ let HandshakeStatus::ReadyWithContext { context, .. } = &*status else {+ unreachable!("waited for a ready handshake")+ };+ Arc::clone(context)+ }+ () = self.closed.cancelled() => return Err(ListSessionsError::Closed),+ };+ let ToServerMessage::Acp(AcpMessage(RawJsonRpcMessage::Response(Response::Result {+ result,+ ..+ }))) = &context.response+ else {+ return Err(ListSessionsError::Runtime(+ "the shared handshake has no initialize result".to_owned(),+ ));+ };+ serde_json::from_value::<InitializeResponse>(result.clone())+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))+ }++ /// Send a request of the connection's own and await its result.+ async fn request(+ &self,+ method: String,+ params: serde_json::Value,+ ) -> Result<(RawJsonRpcMessage, serde_json::Value), ListSessionsError> {+ let id = connection_request_id();+ let request = RawJsonRpcMessage::request(method, params, id.clone())+ .map_err(|error| ListSessionsError::Runtime(error.to_string()))?;+ self.send_request(id, request).await+ }++ async fn send_request(+ &self,+ id: RequestId,+ request: RawJsonRpcMessage,+ ) -> Result<(RawJsonRpcMessage, serde_json::Value), ListSessionsError> {+ let (answered, answer) = oneshot::channel();+ self.answers.insert(id.clone(), answered);+ self.routes+ .lock()+ .await+ .expect_connection_response(id.clone());+ if let Err(error) = self+ .outbound+ .send(ToRuntimeMessage::Acp(AcpMessage(request)))+ .await+ {+ self.answers.remove(&id);+ return Err(ListSessionsError::Transport(error));+ }+ let answer = tokio::select! {+ answer = answer => answer.map_err(|_| ListSessionsError::Closed)?,+ () = self.closed.cancelled() => return Err(ListSessionsError::Closed),+ };+ match &answer {+ RawJsonRpcMessage::Response(Response::Result { result, .. }) => {+ let result = result.clone();+ Ok((answer, result))+ }+ RawJsonRpcMessage::Response(Response::Error { error, .. }) => {+ Err(ListSessionsError::Runtime(error.message.clone()))+ }+ _ => Err(ListSessionsError::Runtime("not a response".to_owned())),+ }+ }++ /// Give a handshake this connection claimed but did not finish back to+ /// the sessions: the first one bound takes it over, as on readiness.+ fn release_handshake(&self) {+ self.handshake.send_replace(HandshakeStatus::Pending);+ let waiting = self.bound.iter().next().map(|entry| entry.value().clone());+ if let Some(inbound) = waiting+ && self.claim_handshake()+ && inbound+ .try_send(ToServerMessage::Event {+ event: SystemEvent::AcpReady,+ })+ .is_err()+ {+ tracing::warn!("a session could not take over this connection's handshake");+ }+ }+/// The gate every session on this connection shares.pub fn handshake(&self) -> watch::Sender<HandshakeStatus> {self.handshake.clone()@@ -468,6 +656,7 @@ whereRouted::Session(session) => self.deliver(session, message).await,Routed::Connection => self.on_connection_message(message).await,Routed::Probe => self.answer_probes(message),+ Routed::Answer => self.answer(message),Routed::Orphan => {tracing::warn!(frame = ?message,@@ -477,9 +666,22 @@ where}}self.bound.clear();+ self.answers.clear();self.closed.cancel();}+ /// Hand an answer to the connection request waiting for it.+ fn answer(&self, message: ToServerMessage) {+ let ToServerMessage::Acp(AcpMessage(frame)) = message else {+ tracing::error!("dropping a connection answer that is not an ACP response");+ return;+ };+ let Some((_, waiter)) = frame.response_id().and_then(|id| self.answers.remove(id)) else {+ return;+ };+ let _ = waiter.send(frame);+ }+/// Hand one answer to every probe waiting on this connection. An answer/// nobody is waiting for is simply the last one, and is dropped.fn answer_probes(&self, message: ToServerMessage) {@@ -517,7 +719,7 @@ wherereturn;}- self.runtime_ready.store(true, Ordering::Release);+ self.runtime_ready.send_replace(true);let waiting = self.bound.iter()@@ -538,7 +740,7 @@ where/// The move from `Pending` to `InFlight` is the claim itself, so two/// sessions binding at once cannot both decide they are the one.fn claim_handshake(&self) -> bool {- if !self.runtime_ready.load(Ordering::Acquire) {+ if !*self.runtime_ready.borrow() {return false;}let mut claimed = false;@@ -567,6 +769,52 @@ where}}+/// Why a runtime could not list the sessions it serves.+#[derive(Debug, thiserror::Error)]+#[non_exhaustive]+pub enum ListSessionsError {+ /// Sending a request over the runtime transport failed.+ #[error("could not ask the runtime for its sessions: {0}")]+ Transport(#[source] TransportError),+ /// The runtime connection closed before answering.+ #[error("runtime connection closed before listing its sessions")]+ Closed,+ /// The runtime refused or answered something unreadable.+ #[error("runtime could not list its sessions: {0}")]+ Runtime(String),+}++/// Pages of `session/list` read before an agent is assumed to loop.+const MAX_SESSION_LIST_PAGES: usize = 100;++fn connection_request_id() -> RequestId {+ RequestId::Str(format!(+ "macro-connection-{}",+ macro_uuid::generate_uuid_v7()+ ))+}++/// A handshake [`RuntimeConnection::list_sessions`] claimed; given back to+/// the sessions if the initialize it started never finishes.+struct HandshakeClaim<'connection, Sender>+where+ Sender: TransportSender<ToRuntimeMessage>,+{+ connection: &'connection RuntimeConnection<Sender>,+ held: bool,+}++impl<Sender> Drop for HandshakeClaim<'_, Sender>+where+ Sender: TransportSender<ToRuntimeMessage>,+{+ fn drop(&mut self) {+ if self.held {+ self.connection.release_handshake();+ }+ }+}+/// A connection-level model probe failure.#[derive(Debug, thiserror::Error)]#[non_exhaustive]diff --git a/crates/agent_session/src/domain/connection/test.rs b/crates/agent_session/src/domain/connection/test.rsindex 727809ab39..af23ed88dc 100644--- a/crates/agent_session/src/domain/connection/test.rs+++ b/crates/agent_session/src/domain/connection/test.rs@@ -4,6 +4,7 @@//! transcript, so "which session owns this" is a correctness question about//! that session's durable history.+use std::sync::atomic::{AtomicBool, Ordering};use std::time::Duration;use super::*;@@ -164,6 +165,20 @@ fn model_probe_responses_belong_to_the_connection_waiters() {);}+#[test]+fn an_answer_to_the_connection_is_neither_a_session_frame_nor_an_orphan() {+ let mut routes = Routes::default();+ routes.expect_connection_response(RequestId::Str("macro-live-sessions-1".to_owned()));++ let answer = response(+ "macro-live-sessions-1",+ serde_json::json!({"sessionIds": []}),+ );+ assert_eq!(routes.route(&answer), Routed::Answer);+ // Answered once: a second reply to the same request has no owner.+ assert_eq!(routes.route(&answer), Routed::Orphan);+}+#[test]fn a_finished_session_stops_owning_anything() {let mut routes = Routes::default();@@ -484,3 +499,222 @@ async fn pending_model_probe_ends_when_the_connection_closes() {Err(ModelProbeError::Closed)));}++fn sent_request(+ message: Option<ToRuntimeMessage>,+) -> agent_client_protocol::schema::v1::Request<agent_client_protocol::RawJsonRpcParams> {+ let Some(ToRuntimeMessage::Acp(AcpMessage(RawJsonRpcMessage::Request(request)))) = message+ else {+ panic!("expected an ACP request, got {message:?}");+ };+ request+}++fn answer(+ request: &agent_client_protocol::schema::v1::Request<agent_client_protocol::RawJsonRpcParams>,+ result: serde_json::Value,+) -> ToServerMessage {+ ToServerMessage::Acp(AcpMessage(RawJsonRpcMessage::response(+ request.id.clone(),+ Ok(result),+ )))+}++fn listing_initialize() -> serde_json::Value {+ serde_json::json!({+ "protocolVersion": 1,+ "agentCapabilities": {"loadSession": true, "sessionCapabilities": {"list": {}}},+ })+}++#[tokio::test]+async fn a_dialed_runtime_is_initialized_once_and_lists_every_page_of_its_sessions() {+ let (carrier, mut runtime) = Channel::duplex();+ let connection = RuntimeConnection::connect(carrier);+ let listing = {+ let connection = Arc::clone(&connection);+ tokio::spawn(async move { connection.list_sessions().await })+ };+ runtime.tx.send(ready()).expect("ready should send");++ let initialize = sent_request(runtime.rx.recv().await);+ assert_eq!(initialize.method.as_ref(), "initialize");+ runtime+ .tx+ .send(answer(&initialize, listing_initialize()))+ .expect("initialize answer should send");+ let first = sent_request(runtime.rx.recv().await);+ assert_eq!(first.method.as_ref(), "session/list");+ runtime+ .tx+ .send(answer(+ &first,+ serde_json::json!({"sessions": [{"sessionId": "acp-1", "cwd": "/srv"}], "nextCursor": "page-2"}),+ ))+ .expect("first page should send");+ let second = sent_request(runtime.rx.recv().await);+ assert_eq!(+ serde_json::to_value(&second.params).unwrap()["cursor"],+ "page-2"+ );+ runtime+ .tx+ .send(answer(+ &second,+ serde_json::json!({"sessions": [{"sessionId": "acp-2", "cwd": "/srv"}]}),+ ))+ .expect("second page should send");++ assert_eq!(+ listing+ .await+ .expect("list task")+ .expect("the runtime listed"),+ [SessionId::from("acp-1"), SessionId::from("acp-2")]+ );+ // Sessions binding afterwards reuse this handshake instead of sending+ // their own initialize.+ assert!(matches!(+ &*connection.handshake().borrow(),+ HandshakeStatus::ReadyWithContext { restore, .. } if restore.load && !restore.resume+ ));+ let attachment = connection.bind(AgentSessionId::TEST_A).await;+ let (_session_outbound, mut session_inbound) = attachment.connector.split();+ assert!(+ tokio::time::timeout(Duration::from_millis(20), session_inbound.recv())+ .await+ .is_err(),+ "a session must not be handed a handshake the connection already ran"+ );+}++#[tokio::test]+async fn an_agent_that_cannot_list_sessions_lists_none() {+ let (carrier, mut runtime) = Channel::duplex();+ let connection = RuntimeConnection::connect(carrier);+ let listing = {+ let connection = Arc::clone(&connection);+ tokio::spawn(async move { connection.list_sessions().await })+ };+ runtime.tx.send(ready()).expect("ready should send");+ let initialize = sent_request(runtime.rx.recv().await);+ runtime+ .tx+ .send(answer(+ &initialize,+ serde_json::json!({"protocolVersion": 1, "agentCapabilities": {"loadSession": true}}),+ ))+ .expect("initialize answer should send");++ assert!(+ listing+ .await+ .expect("list task")+ .expect("not listing is no failure")+ .is_empty()+ );+ assert!(runtime.rx.try_recv().is_err(), "no session/list was sent");+}++#[tokio::test]+async fn a_handshake_a_session_runs_is_reused_for_listing() {+ let (carrier, mut runtime) = Channel::duplex();+ let connection = RuntimeConnection::connect(carrier);+ let attachment = connection.bind(AgentSessionId::TEST_A).await;+ let (_session_outbound, mut session_inbound) = attachment.connector.split();+ runtime.tx.send(ready()).expect("ready should send");+ assert!(matches!(+ session_inbound.recv().await,+ Some(ToServerMessage::Event {+ event: SystemEvent::AcpReady+ })+ ));+ let listing = {+ let connection = Arc::clone(&connection);+ tokio::spawn(async move { connection.list_sessions().await })+ };++ // The session's initialize finishes and is published the way its actor+ // publishes it.+ let initialize = RawJsonRpcMessage::request(+ "initialize".to_owned(),+ serde_json::json!({"protocolVersion": 1}),+ RequestId::Str("agent_session:0".to_owned()),+ )+ .expect("a serializable request");+ connection+ .handshake()+ .send_replace(HandshakeStatus::ReadyWithContext {+ restore: SessionRestoreSupport {+ resume: false,+ load: true,+ },+ context: Arc::new(InitializationContext {+ request: ToRuntimeMessage::Acp(AcpMessage(initialize)),+ response: response("agent_session:0", listing_initialize()),+ }),+ });++ let list = sent_request(runtime.rx.recv().await);+ assert_eq!(list.method.as_ref(), "session/list");+ runtime+ .tx+ .send(answer(+ &list,+ serde_json::json!({"sessions": [{"sessionId": "acp-1", "cwd": "/srv"}]}),+ ))+ .expect("list answer should send");+ assert_eq!(+ listing+ .await+ .expect("list task")+ .expect("the runtime listed"),+ [SessionId::from("acp-1")]+ );+}++#[tokio::test]+async fn an_abandoned_initialize_hands_the_handshake_to_a_waiting_session() {+ let (carrier, mut runtime) = Channel::duplex();+ let connection = RuntimeConnection::connect(carrier);+ let listing = {+ let connection = Arc::clone(&connection);+ tokio::spawn(async move { connection.list_sessions().await })+ };+ runtime.tx.send(ready()).expect("ready should send");+ sent_request(runtime.rx.recv().await);+ // Bound while the connection's own initialize is unanswered: it waits.+ let attachment = connection.bind(AgentSessionId::TEST_A).await;+ let (_session_outbound, mut session_inbound) = attachment.connector.split();++ // The caller's deadline passes and the listing is dropped.+ listing.abort();+ assert!(listing.await.unwrap_err().is_cancelled());++ assert!(matches!(+ session_inbound.recv().await,+ Some(ToServerMessage::Event {+ event: SystemEvent::AcpReady+ })+ ));+ assert!(matches!(+ &*connection.handshake().borrow(),+ HandshakeStatus::InFlight+ ));+}++#[tokio::test]+async fn a_closed_connection_ends_the_listing() {+ let (carrier, runtime) = Channel::duplex();+ let connection = RuntimeConnection::connect(carrier);+ let listing = {+ let connection = Arc::clone(&connection);+ tokio::spawn(async move { connection.list_sessions().await })+ };+ drop(runtime);++ assert!(matches!(+ listing.await.expect("list task"),+ Err(ListSessionsError::Closed)+ ));+}diff --git a/crates/agent_session/src/domain/session/mod.rs b/crates/agent_session/src/domain/session/mod.rsindex cfdb035343..affdcc9213 100644--- a/crates/agent_session/src/domain/session/mod.rs+++ b/crates/agent_session/src/domain/session/mod.rs@@ -12,6 +12,7 @@ mod types;mod tests;pub use session::SessionMachine;+pub(crate) use session::initialize_request;pub use types::{CloseReason, Effect, HandshakeStatus, InitializationContext, Input, PermissionPolicy,RuntimeStatus, SessionRestoreSupport, StopReason,diff --git a/crates/agent_session/src/domain/session/session.rs b/crates/agent_session/src/domain/session/session.rsindex 0ed67a9949..6a989aab51 100644--- a/crates/agent_session/src/domain/session/session.rs+++ b/crates/agent_session/src/domain/session/session.rs@@ -535,14 +535,7 @@ impl<Token> SessionMachine<Token> {}};- let restore = SessionRestoreSupport {- resume: response- .agent_capabilities- .session_capabilities- .resume- .is_some(),- load: response.agent_capabilities.load_session,- };+ let restore = SessionRestoreSupport::of(&response);// Announced before this session opens: every other session on this// connection needs the same answer, and only this machine was told it.effects.push(Effect::Initialized { restore });@@ -801,23 +794,8 @@ impl<Token> SessionMachine<Token> {fn build_initialize_request(&mut self,) -> std::result::Result<(RawJsonRpcMessage, RequestId), agent_client_protocol::Error> {- // Both elicitation modes are advertised: the session page renders- // forms and opens URLs after consent. Agents that check (they must)- // will only ask once this says they may - so this line must never- // ship ahead of `hold_or_refuse_elicitation`, or every agent that- // asks hangs on a request nothing answers.- let capabilities = ClientCapabilities::new().elicitation(- ElicitationCapabilities::new()- .form(ElicitationFormCapabilities::new())- .url(ElicitationUrlCapabilities::new()),- );- let (method, params) = InitializeRequest::new(PROTOCOL_VERSION)- .client_capabilities(capabilities)- .to_untyped_message()?- .into_parts();let request_id = self.next_id();- let request = RawJsonRpcMessage::request(method, params, request_id.clone())?;- Ok((request, request_id))+ Ok((initialize_request(request_id.clone())?, request_id))}fn build_new_session_request(@@ -1201,3 +1179,24 @@ impl<Token> SessionMachine<Token> {id}}++/// The one `initialize` a connection sends, whoever sends it.+pub(crate) fn initialize_request(+ request_id: RequestId,+) -> std::result::Result<RawJsonRpcMessage, agent_client_protocol::Error> {+ // Both elicitation modes are advertised: the session page renders+ // forms and opens URLs after consent. Agents that check (they must)+ // will only ask once this says they may - so this line must never+ // ship ahead of `hold_or_refuse_elicitation`, or every agent that+ // asks hangs on a request nothing answers.+ let capabilities = ClientCapabilities::new().elicitation(+ ElicitationCapabilities::new()+ .form(ElicitationFormCapabilities::new())+ .url(ElicitationUrlCapabilities::new()),+ );+ let (method, params) = InitializeRequest::new(PROTOCOL_VERSION)+ .client_capabilities(capabilities)+ .to_untyped_message()?+ .into_parts();+ RawJsonRpcMessage::request(method, params, request_id)+}diff --git a/crates/agent_session/src/domain/session/types.rs b/crates/agent_session/src/domain/session/types.rsindex 814cd6049a..44da700ecc 100644--- a/crates/agent_session/src/domain/session/types.rs+++ b/crates/agent_session/src/domain/session/types.rs@@ -105,6 +105,20 @@ pub struct SessionRestoreSupport {pub load: bool,}+impl SessionRestoreSupport {+ /// What an agent's `initialize` answer offers.+ pub fn of(response: &agent_client_protocol::schema::v1::InitializeResponse) -> Self {+ Self {+ resume: response+ .agent_capabilities+ .session_capabilities+ .resume+ .is_some(),+ load: response.agent_capabilities.load_session,+ }+ }+}+/// Where the sessions on one connection learn that its ACP handshake is done.////// Exactly one session runs `initialize` per connection; the rest wait fordiff --git a/crates/coding_agent_worker/README.md b/crates/coding_agent_worker/README.mdindex 462e97fb7e..64b77d3672 100644--- a/crates/coding_agent_worker/README.md+++ b/crates/coding_agent_worker/README.md@@ -22,6 +22,12 @@ agent resumes its native session on the next prompt. Socket errors do not createreplacement agents. Changing the configured provider does not convert existingsessions into sessions for the other provider.+After macrod restarts or its runtime connection reconnects, the harness service+initializes the new connection and calls ACP `session/list`. herdr-acp lists+the sessions whose Herdr agent still runs, and the service loads each one. That+replays local history Macro missed and resumes forwarding without a Macro+prompt. Agents closed in Herdr are not reopened.+Closing a session's agent in Herdr (its tab or pane, by you or the agent) isreported to Macro once with the ACP extension notification `_session/closed`.Only Herdr's explicit `agent_not_found` answer counts, checked every fewdiff --git a/crates/coding_agent_worker/src/herdr/acp_agent.rs b/crates/coding_agent_worker/src/herdr/acp_agent.rsindex eeb317e97f..299af6b1d3 100644--- a/crates/coding_agent_worker/src/herdr/acp_agent.rs+++ b/crates/coding_agent_worker/src/herdr/acp_agent.rs@@ -34,6 +34,9 @@ use super::cli::{AgentLaunch, HerdrCli, HerdrError};use super::hub::title_from;use super::store::{Record, Store, write_private};use super::tail::Cursor;+use agent_client_protocol::schema::v1::{+ ListSessionsRequest, ListSessionsResponse, SessionId, SessionInfo,+};use agent_fold::domain::transcript::{Fold, LogEvent};use claude_fold::ClaudeLog;use codex_fold::CodexLog;@@ -51,6 +54,8 @@ use permission::{OpenTool, track_open_tool};pub(crate) const SUBCOMMAND: &str = "herdr-acp";const MODEL_CONFIG_ID: &str = "model";+/// Attempts to read one native agent's state while listing live sessions.+const LIVE_SESSION_LOOKUPS: usize = 3;const DEFAULT_MODEL: &str = "default";const CLAUDE_MODELS: &[(&str, &str)] = &[(DEFAULT_MODEL, "Default (Claude Code's choice)"),@@ -457,11 +462,18 @@ impl Adapter {"protocolVersion": 1,"agentCapabilities": {"loadSession": true,+ "sessionCapabilities": {"list": {}},"promptCapabilities": {"image": false, "audio": false, "embeddedContext": false},},"authMethods": [],"agentInfo": {"name": "macrod-herdr", "version": env!("CARGO_PKG_VERSION")},})),+ "session/list" => {+ let request = serde_json::from_value::<ListSessionsRequest>(params)+ .map_err(|error| RpcError::invalid(error.to_string()))?;+ serde_json::to_value(self.list_sessions(request.cwd.as_deref()).await)+ .map_err(|error| RpcError::internal(error.to_string()))+ }"session/new" => self.new_session(¶ms),"session/load" => self.load_session(¶ms).await,"session/set_config_option" => {@@ -492,6 +504,47 @@ impl Adapter {}}+ /// `session/list`: stored sessions whose Herdr agent still runs, oldest+ /// first, in `cwd` when given. Read-only: the client's following+ /// `session/load` attaches them. A closed agent is not live, so it is+ /// never reopened; other lookup failures are retried before a session is+ /// left out.+ async fn list_sessions(&self, cwd: Option<&Path>) -> ListSessionsResponse {+ use futures::StreamExt as _;+ let Some(herdr) = &self.herdr else {+ return ListSessionsResponse::new(Vec::new());+ };+ let mut records: Vec<Record> = futures::stream::iter(+ self.store.list().into_iter().filter(|record| {+ record.kind == self.options.kind && cwd.is_none_or(|cwd| record.cwd == cwd)+ }),+ )+ .map(|record| async move {+ let name = agent_name(&record.id);+ for _ in 0..LIVE_SESSION_LOOKUPS {+ match herdr.agent_info(&name).await {+ Ok(_) => return Some(record),+ Err(error) if error.missing_agent() => return None,+ Err(error) => {+ tracing::warn!(%name, %error, "could not tell whether a native agent still runs");+ }+ }+ }+ None+ })+ .buffer_unordered(8)+ .filter_map(std::future::ready)+ .collect()+ .await;+ records.sort_by(|a, b| a.started.cmp(&b.started).then_with(|| a.id.cmp(&b.id)));+ ListSessionsResponse::new(+ records+ .into_iter()+ .map(|record| SessionInfo::new(SessionId::new(record.id), record.cwd))+ .collect(),+ )+ }+fn new_session(self: &Arc<Self>, params: &Value) -> Result<Value, RpcError> {let id = uuid::Uuid::new_v4().to_string();let cwd = paramsdiff --git a/crates/coding_agent_worker/src/herdr/acp_agent/fixtures/live_herdr.sh b/crates/coding_agent_worker/src/herdr/acp_agent/fixtures/live_herdr.shnew file mode 100644index 0000000000..6a9ae95e23--- /dev/null+++ b/crates/coding_agent_worker/src/herdr/acp_agent/fixtures/live_herdr.sh@@ -0,0 +1,18 @@+#!/bin/sh+# The first lookup of macro-222222222222 fails without saying the agent is+# gone, as a herdr that is briefly unavailable would.+flaked="$(dirname "$0")/flaked"+case "$1 $2 $3" in+ "agent get macro-222222222222")+ if [ ! -e "$flaked" ]; then+ : > "$flaked"+ printf '%s\n' 'herdr: server not responding' >&2+ exit 1+ fi+ printf '%s\n' '{"result":{"agent":{"agent_status":"idle","state_change_seq":1}}}' ;;+ "agent get macro-111111111111" | "agent get macro-444444444444")+ printf '%s\n' '{"result":{"agent":{"agent_status":"idle","state_change_seq":1}}}' ;;+ *)+ printf '%s\n' '{"error":{"code":"agent_not_found"}}' >&2+ exit 1 ;;+esacdiff --git a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs b/crates/coding_agent_worker/src/herdr/acp_agent/test.rsindex 891c40a428..7f5f263561 100644--- a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs+++ b/crates/coding_agent_worker/src/herdr/acp_agent/test.rs@@ -829,6 +829,88 @@ async fn a_prompt_dismisses_the_exit_confirmation_a_second_ctrl_c_left() {adapter.shutdown.cancel();}+#[tokio::test]+async fn session_list_names_stored_sessions_whose_herdr_agent_still_runs() {+ use std::os::unix::fs::PermissionsExt as _;+ let root = tempfile::tempdir().unwrap();+ let script = root.path().join("herdr");+ std::fs::write(&script, include_str!("fixtures/live_herdr.sh")).unwrap();+ std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap();+ let store = Store(root.path().to_owned());+ for (id, kind, started) in [+ ("11111111-1111-4111-8111-111111111111", TuiAgent::Claude, 2),+ ("22222222-2222-4222-8222-222222222222", TuiAgent::Claude, 1),+ ("33333333-3333-4333-8333-333333333333", TuiAgent::Claude, 0),+ ("44444444-4444-4444-8444-444444444444", TuiAgent::Codex, 0),+ ] {+ store+ .save(&Record {+ version: 1,+ id: id.to_owned(),+ kind,+ cwd: root.path().to_owned(),+ model: "sonnet".into(),+ native_id: None,+ transcript: None,+ started,+ })+ .unwrap();+ }+ let (mut adapter, mut output) = test_adapter(root.path());+ Arc::get_mut(&mut adapter).unwrap().herdr = Some(HerdrCli::new(script, None));++ adapter+ .respond_to_request(json!("macro-connection-1"), "session/list", json!({}))+ .await;++ assert_eq!(+ output.try_recv().unwrap(),+ json!({+ "jsonrpc": "2.0",+ "id": "macro-connection-1",+ "result": {"sessions": [+ {"sessionId": "22222222-2222-4222-8222-222222222222", "cwd": root.path()},+ {"sessionId": "11111111-1111-4111-8111-111111111111", "cwd": root.path()},+ ]},+ })+ );+ assert!(output.try_recv().is_err());+ assert!(lock(&adapter.sessions).is_empty());+ assert!(+ root.path().join("flaked").exists(),+ "a lookup that failed without naming the agent missing was retried"+ );+}++#[tokio::test]+async fn initialize_advertises_session_list() {+ let root = tempfile::tempdir().unwrap();+ let (adapter, _) = test_adapter(root.path());++ let initialized = adapter.request("initialize", json!({})).await.unwrap();++ assert_eq!(+ initialized["agentCapabilities"]["sessionCapabilities"]["list"],+ json!({})+ );+}++#[tokio::test]+async fn session_list_is_empty_without_herdr() {+ let root = tempfile::tempdir().unwrap();+ save_transcript(root.path(), &uuid::Uuid::now_v7().to_string());+ let (adapter, mut output) = test_adapter(root.path());++ adapter+ .respond_to_request(json!(1), "session/list", json!({}))+ .await;++ assert_eq!(+ output.try_recv().unwrap(),+ json!({"jsonrpc": "2.0", "id": 1, "result": {"sessions": []}})+ );+}+#[tokio::test]async fn a_closed_native_agent_is_reported_once_and_other_herdr_failures_never() {use std::os::unix::fs::PermissionsExt as _;diff --git a/crates/coding_agent_worker/src/herdr/store.rs b/crates/coding_agent_worker/src/herdr/store.rsindex 013e14320a..e75ab4fab5 100644--- a/crates/coding_agent_worker/src/herdr/store.rs+++ b/crates/coding_agent_worker/src/herdr/store.rs@@ -46,6 +46,23 @@ impl Store {}Ok(record)}++ /// Every valid record; unreadable or mismatched files are skipped.+ pub fn list(&self) -> Vec<Record> {+ let Ok(entries) = std::fs::read_dir(self.0.join("sessions")) else {+ return Vec::new();+ };+ entries+ .flatten()+ .filter_map(|entry| {+ let path = entry.path();+ if path.extension()? != "json" {+ return None;+ }+ self.load(path.file_stem()?.to_str()?).ok()+ })+ .collect()+ }}pub(crate) fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {diff --git a/crates/coding_agent_worker/src/herdr/store/test.rs b/crates/coding_agent_worker/src/herdr/store/test.rsindex 4fb24792cb..4b966ee451 100644--- a/crates/coding_agent_worker/src/herdr/store/test.rs+++ b/crates/coding_agent_worker/src/herdr/store/test.rs@@ -28,3 +28,44 @@ fn records_survive_a_new_store_without_exposing_credentials() {);assert!(reopened.load("../other").is_err());}++#[test]+fn list_returns_every_valid_record_and_skips_the_rest() {+ let root = tempfile::tempdir().unwrap();+ let store = Store(root.path().to_owned());+ assert!(store.list().is_empty());+ let id = uuid::Uuid::now_v7().to_string();+ store+ .save(&Record {+ version: 1,+ id: id.clone(),+ kind: TuiAgent::Codex,+ cwd: PathBuf::from("/repo"),+ model: "gpt-5".into(),+ native_id: None,+ transcript: None,+ started: 1,+ })+ .unwrap();+ let sessions = root.path().join("sessions");+ std::fs::write(sessions.join("garbage.json"), "{").unwrap();+ std::fs::write(+ sessions.join(format!("{}.json", uuid::Uuid::now_v7())),+ serde_json::to_vec(&Record {+ version: 1,+ id: uuid::Uuid::now_v7().to_string(),+ kind: TuiAgent::Codex,+ cwd: PathBuf::from("/repo"),+ model: "gpt-5".into(),+ native_id: None,+ transcript: None,+ started: 2,+ })+ .unwrap(),+ )+ .unwrap();+ std::fs::write(sessions.join("notes.txt"), "").unwrap();+ let listed = store.list();+ assert_eq!(listed.len(), 1);+ assert_eq!(listed[0].id, id);+}diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.tomlindex 0931edf682..073e6602a0 100644--- a/services/agent_harness_service/Cargo.toml+++ b/services/agent_harness_service/Cargo.toml@@ -29,6 +29,7 @@ universal_input = { path = "../../crates/universal_input" }models_ai_projection = { path = "../../crates/models_ai_projection" }activity = { path = "../../crates/activity", default-features = false }agent = { path = "../../crates/agent", features = ["debug-messages"] }+agent-client-protocol = { workspace = true }agent_changes = { path = "../../crates/agent_changes" }agent_code_mode = { path = "../../crates/agent_code_mode" }agent_egress = { path = "../../crates/agent_egress" }@@ -205,7 +206,6 @@ workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }[dev-dependencies]agent = { path = "../../crates/agent" }-agent-client-protocol = { workspace = true }agent_session = { path = "../../crates/agent_session", features = ["test-utils"] }ai_tools = { path = "../../crates/ai_tools", features = ["test-support"] }async-trait = { workspace = true }diff --git a/services/agent_harness_service/src/harness_bindings.rs b/services/agent_harness_service/src/harness_bindings.rsindex 2e16f32478..dbda857a7a 100644--- a/services/agent_harness_service/src/harness_bindings.rs+++ b/services/agent_harness_service/src/harness_bindings.rs@@ -9,11 +9,16 @@ use std::future::Future;use std::pin::Pin;use std::sync::Arc;+use agent_client_protocol::schema::v1::SessionId;use agent_harness::domain::ports::{HarnessBindings, HarnessPresence};+use agent_session::domain::model::AgentSessionId;use bot_id::BotId;use harness_id::HarnessId;use sqlx::PgPool;+#[cfg(test)]+mod test;+/// [`HarnessBindings`] over the `agent_configs` table.#[derive(Clone)]pub struct PgHarnessBindings {@@ -43,6 +48,34 @@ impl HarnessBindings for PgHarnessBindings {Ok(harness_id.flatten().map(HarnessId::new_from_uuid))}++ async fn sessions_on_harness(+ &self,+ harness: HarnessId,+ acp_sessions: &[SessionId],+ ) -> anyhow::Result<Vec<AgentSessionId>> {+ let acp_sessions: Vec<String> = acp_sessions+ .iter()+ .map(|session| session.to_string())+ .collect();+ let ids = sqlx::query_scalar!(+ r#"+ SELECT s.id+ FROM agent_session s+ JOIN agent_configs ac ON ac.bot_id = s.bot_id+ JOIN harnesses h ON h.id = ac.harness_id+ WHERE ac.harness_id = $1+ AND h.deleted_at IS NULL+ AND s.acp_session_id = ANY($2)+ "#,+ harness.as_uuid(),+ &acp_sessions,+ )+ .fetch_all(&self.pool)+ .await?;++ Ok(ids.into_iter().map(AgentSessionId::new_from_uuid).collect())+ }}/// [`HarnessPresence`] over the `harnesses` table.diff --git a/services/agent_harness_service/src/harness_bindings/test.rs b/services/agent_harness_service/src/harness_bindings/test.rsnew file mode 100644index 0000000000..021355c765--- /dev/null+++ b/services/agent_harness_service/src/harness_bindings/test.rs@@ -0,0 +1,115 @@+use super::*;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use sqlx::types::Uuid;++const OWNER: &str = "macro|[email protected]";++async fn insert_harness(pool: &PgPool) -> HarnessId {+ let id = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ r#"+ INSERT INTO harnesses (id, kind, name, owner_user_id, created_by)+ VALUES ($1, 'macrod', 'Laptop', $2, $2)+ "#,+ id,+ OWNER,+ )+ .execute(pool)+ .await+ .unwrap();+ HarnessId::new_from_uuid(id)+}++async fn insert_bot(pool: &PgPool, harness: HarnessId) -> Uuid {+ let id = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ r#"+ INSERT INTO bots (id, kind, name, handle, owner_user_id)+ VALUES ($1, 'owned', 'Native agent', $2, $3)+ "#,+ id,+ format!("native-{id}"),+ OWNER,+ )+ .execute(pool)+ .await+ .unwrap();+ sqlx::query!(+ r#"+ INSERT INTO agent_configs+ (bot_id, instructions, harness, default_model, channel_scope, is_coding, harness_id)+ VALUES ($1, '', 'macrod', 'default', 'all', true, $2)+ "#,+ id,+ harness.as_uuid(),+ )+ .execute(pool)+ .await+ .unwrap();+ id+}++async fn insert_session(pool: &PgPool, bot: Uuid, acp_session_id: &str) -> AgentSessionId {+ let id = macro_uuid::generate_uuid_v7();+ sqlx::query!(+ r#"+ INSERT INTO agent_session+ (id, owner_id, bot_id, model, harness, workspace, for_warming, acp_session_id)+ VALUES ($1, $2, $3, 'default', 'macrod', '/srv/agent', false, $4)+ "#,+ id,+ OWNER,+ bot,+ acp_session_id,+ )+ .execute(pool)+ .await+ .unwrap();+ AgentSessionId::new_from_uuid(id)+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn live_sessions_resolve_only_on_the_harness_their_bot_is_bound_to(pool: PgPool) {+ let laptop = insert_harness(&pool).await;+ let other = insert_harness(&pool).await;+ let laptop_bot = insert_bot(&pool, laptop).await;+ let other_bot = insert_bot(&pool, other).await;+ let mine = insert_session(&pool, laptop_bot, "acp-mine").await;+ insert_session(&pool, other_bot, "acp-theirs").await;+ insert_session(&pool, laptop_bot, "acp-not-reported").await;++ let found = PgHarnessBindings::new(pool.clone())+ .sessions_on_harness(+ laptop,+ &[+ SessionId::new("acp-mine"),+ SessionId::new("acp-theirs"),+ SessionId::new("acp-unknown"),+ ],+ )+ .await+ .unwrap();++ assert_eq!(found, [mine]);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_deleted_harness_reattaches_nothing(pool: PgPool) {+ let laptop = insert_harness(&pool).await;+ let bot = insert_bot(&pool, laptop).await;+ insert_session(&pool, bot, "acp-mine").await;+ sqlx::query!(+ "UPDATE harnesses SET deleted_at = now() WHERE id = $1",+ laptop.as_uuid()+ )+ .execute(&pool)+ .await+ .unwrap();++ let found = PgHarnessBindings::new(pool.clone())+ .sessions_on_harness(laptop, &[SessionId::new("acp-mine")])+ .await+ .unwrap();++ assert!(found.is_empty());+}diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex aaac5ebfd7..7fd16626d5 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -1164,7 +1164,13 @@ async fn run() -> anyhow::Result<()> {// bound to it. Held here because the gateway puts dialed-in sockets into// it and the harness takes sessions out of it. Attach/detach is mirrored// to the harnesses table so the settings page can show connection state.- let runtimes = RuntimeRegistry::with_presence(Arc::new(PgHarnessPresence::new(pool.clone())));+ // Each dial also reports which sessions its runtime still serves, which+ // the harness loads once it exists below.+ let (live_sessions, mut live_session_reports) = tokio::sync::mpsc::unbounded_channel();+ let runtimes = RuntimeRegistry::with_presence(+ Arc::new(PgHarnessPresence::new(pool.clone())),+ live_sessions,+ );// Read once, here, rather than at every session this deployment opens: a// URL that names no repository is a misconfiguration of the deployment,// and refusing it at startup is the difference between one loud failure@@ -1307,6 +1313,17 @@ async fn run() -> anyhow::Result<()> {reviews_cell.set(reviews.clone()).map_err(|_| anyhow::anyhow!("Review service was initialized twice"))?;+ let reattaching = Arc::clone(&harness);+ tokio::spawn(async move {+ while let Some(live) = live_session_reports.recv().await {+ let harness = Arc::clone(&reattaching);+ tokio::spawn(async move {+ harness+ .reattach_live_sessions(live.harness, live.acp_sessions, live.span)+ .await;+ });+ }+ });let warm_cleanup = Arc::clone(&harness);let session_visibility_repo = session_repo.clone();tokio::spawn(async move {@@ -2020,6 +2037,7 @@ async fn run() -> anyhow::Result<()> {| HarnessCommand::ToolApproval(_)| HarnessCommand::PresentTool { .. }| HarnessCommand::SessionStopped { .. }+ | HarnessCommand::Reattach| HarnessCommand::DrainNotifications => "agent_trigger.unexpected",};tracing::Span::current().record("macro.event.type", event_type);