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(agent-harness): dispatch queued prompts when macrod reattaches a session (#9779)

Wolf Mermelsteindf2759d155e6
diff --git a/crates/agent_harness/AGENTS.md b/crates/agent_harness/AGENTS.md
index e69e2305f6..cb16e7000d 100644
--- a/crates/agent_harness/AGENTS.md
+++ b/crates/agent_harness/AGENTS.md
@@ -73,8 +73,9 @@ 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
+restoring each (`session/load` or `session/resume`) without a prompt, then
+dispatching whatever its durable queue still holds when no turn is running;
+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 user
diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rs
index abad4256a4..374bafb51c 100644
--- a/crates/agent_harness/src/domain/service/deliver.rs
+++ b/crates/agent_harness/src/domain/service/deliver.rs
@@ -115,20 +115,21 @@ where
///
/// 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.
+ /// Answers whether this call attached the session.
#[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<()> {
+ pub(super) async fn reattach(&self, session_id: AgentSessionId) -> Result<bool> {
if self.sessions.attached_mcp_servers(session_id).is_some() {
- return Ok(());
+ return Ok(false);
}
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(());
+ return Ok(false);
}
self.reconnect(session_id).await?;
tracing::Span::current().record("agent.session.reattached", true);
- Ok(())
+ Ok(true)
}
/// Reconcile a prior invocation before deciding whether a batch may run.
diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rs
index 69bebb5f65..45fb326fb1 100644
--- a/crates/agent_harness/src/domain/service/queue.rs
+++ b/crates/agent_harness/src/domain/service/queue.rs
@@ -891,7 +891,12 @@ where
Ok(CommandOutcome::Completed)
}
HarnessCommand::Reattach => {
- self.reattach(session_id).await?;
+ // Prompts queued behind a turn the drop interrupted are
+ // restored with the session; only a turn end or a new prompt
+ // dispatches otherwise, and an idle session gets neither.
+ if self.reattach(session_id).await? && !self.busy.is_pending(session_id) {
+ self.dispatch_next(session_id).await?;
+ }
Ok(CommandOutcome::Completed)
}
HarnessCommand::Delete => {
diff --git a/crates/agent_harness/src/domain/service/test/reattach.rs b/crates/agent_harness/src/domain/service/test/reattach.rs
index 61e3f7db4c..28a505fbf0 100644
--- a/crates/agent_harness/src/domain/service/test/reattach.rs
+++ b/crates/agent_harness/src/domain/service/test/reattach.rs
@@ -274,3 +274,58 @@ async fn a_dial_restores_only_this_harness_tracked_sessions_from_an_agent_that_l
));
assert!(prompts(&agent).is_empty(), "restoring never prompts");
}
+
+#[tokio::test]
+async fn a_redial_delivers_the_prompt_queued_behind_the_turn_the_drop_interrupted() {
+ let ((service, repo, _containers, _announcer, runtimes), turns) =
+ harness_with_signals(PromptContextMock::default(), PromptComposerMock::default());
+ let bot_id = BotId::new_from_uuid(macro_uuid::generate_uuid_v7());
+ let session = native_session(&service, &repo, bot_id).await;
+ let first = ContainerMock::default();
+ runtimes.attach(harness_for_bot(bot_id), first.clone());
+
+ let drive = async {
+ initializes_loading(&first).await;
+ let agent = first.agent();
+ agent.wait_for_requests(2).await;
+ agent.loads_session(LoadSessionResponse::new());
+ };
+ let (result, ()) = tokio::join!(prompt(&service, session, "fix the failing test"), drive);
+ result.expect("the first prompt binds the session");
+ let queued = prompt(&service, session, "and now the docs")
+ .await
+ .expect("a prompt behind a running turn is accepted");
+ assert!(matches!(queued, CommandOutcome::Queued));
+
+ // The connection drops mid-turn: the turn is lost, the queued prompt is
+ // not, and nobody prompts the session again.
+ let published = turns.lifecycle().len();
+ first.disconnects();
+ turns.lifecycle_published(published + 1).await;
+ assert!(matches!(
+ turns.lifecycle().last(),
+ Some(AgentSessionLifecycleEvent::Stopped(_))
+ ));
+
+ let second = ContainerMock::default();
+ runtimes.attach(harness_for_bot(bot_id), second.clone());
+ let drive = async {
+ initializes_loading(&second).await;
+ let agent = second.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
+ );
+
+ assert_eq!(
+ prompts(&second.agent()),
+ [vec![ContentBlock::from("and now the docs")]]
+ );
+}
diff --git a/crates/agent_session/src/domain/session/actors.rs b/crates/agent_session/src/domain/session/actors.rs
index 7bab658ff3..c0e179578c 100644
--- a/crates/agent_session/src/domain/session/actors.rs
+++ b/crates/agent_session/src/domain/session/actors.rs
@@ -323,7 +323,14 @@ where
let result = self
.logs
.present_tool(self.machine.id(), session_id, presentation)
- .await;
+ .await
+ .inspect_err(|error| {
+ tracing::error!(
+ error = ?error,
+ id = %self.machine.id(),
+ "agent session failed to persist a tool presentation"
+ );
+ });
let failed = result.is_err();
let _ = completed.send(result);
if failed {
@@ -508,6 +515,11 @@ where
.await
.unwrap_or_else(|_| Err(AgentSessionError::LogTimedOut(self.machine.id())));
if let Err(error) = result {
+ tracing::error!(
+ error = ?error,
+ id = %self.machine.id(),
+ "agent session failed to persist its shared initialization"
+ );
self.fail_remaining_completions(&mut effects, error);
effects.extend(self.machine.handle(Input::Closed(CloseReason::LogFailed)));
}