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
chore(agents): durable queues (#6960)
Wolf Mermelstein1ea2c09ee9b3
diff --git a/.sqlx/query-17973bb78d8247cf21ac13fbbde7faa2d777f5619aa2ecda22d8da96ba8fc713.json b/.sqlx/query-17973bb78d8247cf21ac13fbbde7faa2d777f5619aa2ecda22d8da96ba8fc713.jsonnew file mode 100644index 0000000000..1d6feffa86--- /dev/null+++ b/.sqlx/query-17973bb78d8247cf21ac13fbbde7faa2d777f5619aa2ecda22d8da96ba8fc713.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n DELETE FROM agent_session_queue\n WHERE agent_session_id = $1\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "17973bb78d8247cf21ac13fbbde7faa2d777f5619aa2ecda22d8da96ba8fc713"+}diff --git a/.sqlx/query-4492d9a9c974bebbe0dbec33bc0355775de6574f06b55f00c15c19af4d5e7ab2.json b/.sqlx/query-4492d9a9c974bebbe0dbec33bc0355775de6574f06b55f00c15c19af4d5e7ab2.jsonnew file mode 100644index 0000000000..ebb3ee8290--- /dev/null+++ b/.sqlx/query-4492d9a9c974bebbe0dbec33bc0355775de6574f06b55f00c15c19af4d5e7ab2.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT entries AS \"entries: Json<Vec<StoredQueuedAction>>\"\n FROM agent_session_queue\n WHERE agent_session_id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "entries: Json<Vec<StoredQueuedAction>>",+ "type_info": "Jsonb"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "4492d9a9c974bebbe0dbec33bc0355775de6574f06b55f00c15c19af4d5e7ab2"+}diff --git a/.sqlx/query-472628032919f5ea7bb596cf5c4fd729bdbd32924fa927df0deb215f8b85981a.json b/.sqlx/query-472628032919f5ea7bb596cf5c4fd729bdbd32924fa927df0deb215f8b85981a.jsonnew file mode 100644index 0000000000..d1aa76ad25--- /dev/null+++ b/.sqlx/query-472628032919f5ea7bb596cf5c4fd729bdbd32924fa927df0deb215f8b85981a.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT count(*) AS \"count!\"\n FROM agent_session_queue\n WHERE agent_session_id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ null+ ]+ },+ "hash": "472628032919f5ea7bb596cf5c4fd729bdbd32924fa927df0deb215f8b85981a"+}diff --git a/.sqlx/query-6841edc7fe147eb0ea2a5a9bad80a4dd77ccde7a15dc9c0c7be19d2e5111c806.json b/.sqlx/query-6841edc7fe147eb0ea2a5a9bad80a4dd77ccde7a15dc9c0c7be19d2e5111c806.jsonnew file mode 100644index 0000000000..dc5943990d--- /dev/null+++ b/.sqlx/query-6841edc7fe147eb0ea2a5a9bad80a4dd77ccde7a15dc9c0c7be19d2e5111c806.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n INSERT INTO agent_session_queue (agent_session_id, entries)\n VALUES ($1, $2)\n ON CONFLICT (agent_session_id) DO UPDATE SET\n entries = EXCLUDED.entries,\n updated_at = now()\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Jsonb"+ ]+ },+ "nullable": []+ },+ "hash": "6841edc7fe147eb0ea2a5a9bad80a4dd77ccde7a15dc9c0c7be19d2e5111c806"+}diff --git a/crates/agent_harness/src/domain/queue.rs b/crates/agent_harness/src/domain/queue.rsindex adee30a100..fecf4a3427 100644--- a/crates/agent_harness/src/domain/queue.rs+++ b/crates/agent_harness/src/domain/queue.rs@@ -10,9 +10,11 @@//! accepted, so the reply sits on the follow-up rather than after the//! cancelled turn or behind work queued earlier.//!-//! In-memory on purpose. The queue lives beside the session's live actor on-//! the replica that manages it, and dies with the process - a restart loses-//! whatever was waiting, exactly like it always lost an in-flight turn.+//! The live replica keeps this working copy beside the session's actor so+//! edits and dispatch stay serialized on the command worker. Every mutation+//! is written through to the session store as it happens; resume (and the+//! first local command after a restart) restores that row. An in-flight+//! turn is still process-local.//!//! Callers mutate this only from the session's command worker, which is what//! serializes an edit against the dispatch that might be claiming the same@@ -24,6 +26,7 @@ use agent_fold::domain::model::TurnId;use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};use agent_session::domain::events::{InFlightTurnSummary, TurnSummary};use agent_session::domain::model::AgentSessionId;+use agent_session::domain::model::StoredQueuedAction;use agent_session::domain::ports::QueuedControl;use chrono::{DateTime, Utc};use dashmap::DashMap;@@ -125,6 +128,36 @@ impl From<&QueuedEntry> for QueuedControl {}}+impl QueuedEntry {+ /// The durable form written to the session store.+ pub fn to_stored(&self) -> anyhow::Result<StoredQueuedAction> {+ Ok(StoredQueuedAction {+ action_id: self.action_id,+ action: self.action.clone(),+ actor: self.actor.clone(),+ created_at: self.created_at,+ announce: self+ .announce+ .as_ref()+ .map(serde_json::to_value)+ .transpose()?,+ announced_message_id: self.announced,+ })+ }++ /// Rebuild the working-copy entry from a persisted row.+ pub fn from_stored(stored: StoredQueuedAction) -> anyhow::Result<Self> {+ Ok(Self {+ action_id: stored.action_id,+ action: stored.action,+ actor: stored.actor,+ announce: stored.announce.map(serde_json::from_value).transpose()?,+ announced: stored.announced_message_id,+ created_at: stored.created_at,+ })+ }+}+/// Why the queue refused a mutation.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum QueueError {@@ -272,6 +305,24 @@ impl SessionQueues {self.queues.remove(&session);}+ /// Everything waiting, as the working-copy entries.+ #[must_use]+ pub fn snapshot(&self, session: AgentSessionId) -> Vec<QueuedEntry> {+ self.queues+ .get(&session)+ .map(|queue| queue.iter().cloned().collect())+ .unwrap_or_default()+ }++ /// Replace a session's working copy, used when hydrating from the store.+ pub fn replace(&self, session: AgentSessionId, entries: Vec<QueuedEntry>) {+ if entries.is_empty() {+ self.queues.remove(&session);+ } else {+ self.queues.insert(session, entries.into());+ }+ }+/// Remove a waiting entry.pub fn remove(&self,diff --git a/crates/agent_harness/src/domain/queue/test.rs b/crates/agent_harness/src/domain/queue/test.rsindex f742f82fce..66f8a6682b 100644--- a/crates/agent_harness/src/domain/queue/test.rs+++ b/crates/agent_harness/src/domain/queue/test.rs@@ -223,6 +223,35 @@ fn enqueue_front_puts_the_entry_ahead_of_waiting_work() {);}+#[test]+fn a_snapshot_round_trips_through_the_durable_shape() {+ let session = AgentSessionId::TEST_A;+ let mut entry = prompt_entry("keep me");+ entry.announce = Some(AnnounceOrigin {+ parent: messages::domain::models::MessageParent::Channel(Uuid::from_u128(0xf0)),+ thread_id: Uuid::from_u128(0xf1),+ message_id: Uuid::from_u128(0xf2),+ });+ entry.announced = Some(Uuid::from_u128(0xf3));+ let queues = SessionQueues::new();+ queues.enqueue(session, entry.clone()).unwrap();++ let stored = queues.snapshot(session)[0].to_stored().unwrap();+ let restored = QueuedEntry::from_stored(stored).unwrap();+ assert_eq!(restored.action_id, entry.action_id);+ assert_eq!(prompt_text(&restored.action), "keep me");+ assert_eq!(restored.announce, entry.announce);+ assert_eq!(restored.announced, entry.announced);++ let other = SessionQueues::new();+ other.replace(session, vec![restored]);+ assert_eq!(other.list(session).len(), 1);+ assert_eq!(+ prompt_text(&other.claim_next(session).unwrap().action),+ "keep me"+ );+}+#[test]fn the_cap_refuses_the_overflowing_entry() {let queues = SessionQueues::new();diff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rsindex 3cf3c7125e..cbd0d3e301 100644--- a/crates/agent_harness/src/domain/service.rs+++ b/crates/agent_harness/src/domain/service.rs@@ -39,6 +39,7 @@ use agent_session::domain::service::AgentSessionService;use agent_session::domain::session::PermissionPolicy;use bot_id::BotId;use dashmap::DashMap;+use dashmap::DashSet;use dashmap::mapref::entry::Entry;use macro_user_id::user_id::MacroUserIdStr;use tokio::sync::{mpsc, oneshot};@@ -134,8 +135,12 @@ struct AgentHarnessInner<coding_agents: Box<dyn ErasedCodingAgentSource>,defaults: HarnessDefaults,/// Turn-occupying actions waiting for their session's running turn to- /// end. In-memory beside the live actors this replica manages.+ /// end. In-memory working copy; the session store is the durable source.queues: SessionQueues,+ /// Sessions whose durable queue has been loaded into [`Self::queues`]+ /// in this process. A restart starts empty; resume and the first local+ /// command restore from the session store.+ hydrated: DashSet<AgentSessionId>,/// The sessions with a command admitted and not yet resolved, and which/// turn it opened once dispatch names one. Marked the moment a/// turn-occupying action is admitted (queue.rs's `enqueue_then_dispatch`),@@ -324,6 +329,7 @@ wherecoding_agents: Box::new(coding_agents),defaults: defaults.into(),queues: SessionQueues::new(),+ hydrated: DashSet::new(),busy: pending,lifecycle_publisher,mentions,diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rsindex 3d1fe196a5..d2a92b69e8 100644--- a/crates/agent_harness/src/domain/service/deliver.rs+++ b/crates/agent_harness/src/domain/service/deliver.rs@@ -92,6 +92,10 @@ where.permission_policy(permission_policy),).await?;+ // The sandbox is back; take waiting prompts from the store+ // so a restart cannot drop what was queued while this+ // replica was gone.+ self.restore_queue(session_id).await?;} else {// An external runtime is not ours to start - only its// operator can dial - but a bot whose runtime is already@@ -131,6 +135,7 @@ where.mcp_servers(vec![egress.sandbox.internal_mcp_server()]),).await?;+ self.restore_queue(session_id).await?;}self.sessions.send_action(session_id, actor, action, id)diff --git a/crates/agent_harness/src/domain/service/lifecycle.rs b/crates/agent_harness/src/domain/service/lifecycle.rsindex f3bacda82a..d4f9959576 100644--- a/crates/agent_harness/src/domain/service/lifecycle.rs+++ b/crates/agent_harness/src/domain/service/lifecycle.rs@@ -103,14 +103,20 @@ where})}- /// A local read on purpose: the queue lives beside the session's live- /// actor, and this replica answers for what it holds. A reader landing on- /// a non-managing replica sees an empty queue rather than an error.+ /// The durable queue, so any replica can answer. Waiting actions live in+ /// the session store; the managing replica's in-memory copy is a cache.async fn queued_controls(&self,id: AgentSessionId,) -> agent_session::domain::error::Result<Vec<QueuedControl>> {- Ok(self.inner.queues.list(id))+ Ok(self+ .inner+ .sessions+ .list_queued_actions(id)+ .await?+ .iter()+ .map(QueuedControl::from)+ .collect())}async fn edit_queued_control(@@ -350,6 +356,7 @@ where.permission_policy(permission_policy),).await?;+ self.restore_queue(session_id).await?;}}self.sessions.set_sandbox_size(session_id, size).await?;diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rsindex a56f396a09..016d71902a 100644--- a/crates/agent_harness/src/domain/service/queue.rs+++ b/crates/agent_harness/src/domain/service/queue.rs@@ -2,11 +2,13 @@//! command at a time, and routing to the replica that holds the session.use agent_fold::domain::model::{StopReason, TurnSignal};+use agent_session::domain::error::AgentSessionError;use agent_session::domain::events::{AgentSessionLifecycleEvent, InputReceivedMetadata, SessionDeletedMetadata,SessionSettledMetadata, SessionStoppedMetadata, TurnEndedMetadata, TurnStartedMetadata,WaitingForInputMetadata,};+use agent_session::domain::model::StoredQueuedAction;use super::*;@@ -278,6 +280,12 @@ wheresession_id: AgentSessionId,command: HarnessCommand,) -> Result<CommandOutcome> {+ // Persist-as-we-go needs a current working copy before the first+ // mutation, or a restart would overwrite the store with an empty+ // queue. Open creates the row and has nothing to restore.+ if !matches!(command, HarnessCommand::Open(_)) {+ self.ensure_hydrated(session_id).await?;+ }match &command {HarnessCommand::Open(open)if AgentKind::of(open.bot_id) == AgentKind::SandboxedCoder@@ -349,11 +357,13 @@ where.edit_prompt(session_id, action_id, prompt, actor),session_id,)?;+ self.persist_or_rollback(session_id).await?;self.publish_queue(session_id).await;Ok(CommandOutcome::Completed)}HarnessCommand::RemoveQueued { action_id, .. } => {queue_result(self.queues.remove(session_id, action_id), session_id)?;+ self.persist_or_rollback(session_id).await?;self.publish_queue(session_id).await;Ok(CommandOutcome::Completed)}@@ -515,6 +525,7 @@ where// empty snapshot is the viewers' goodbye.self.busy.clear(session_id);self.queues.drop_session(session_id);+ self.hydrated.remove(&session_id);self.publish_queue(session_id).await;match identity {Ok(identity) => {@@ -593,6 +604,7 @@ whereself.queues.enqueue(session_id, entry)};queue_result(enqueued, session_id)?;+ self.persist_or_rollback(session_id).await?;// Mentions are a fact about the prompt, not the turn: published as// soon as the prompt is accepted, whether it dispatches now or waits.if let Some(prompt) = prompt {@@ -682,6 +694,84 @@ where.mark_announced(session_id, action_id, announced.message_id),session_id,)?;+ self.persist_or_rollback(session_id).await?;+ }+ Ok(())+ }++ /// Write the working copy through to the session store.+ async fn write_queue(&self, session_id: AgentSessionId) -> Result<()> {+ let stored = self+ .queues+ .snapshot(session_id)+ .iter()+ .map(QueuedEntry::to_stored)+ .collect::<anyhow::Result<Vec<StoredQueuedAction>>>()+ .map_err(AgentSessionError::Unknown)?;+ self.sessions+ .replace_queued_actions(session_id, &stored)+ .await?;+ Ok(())+ }++ /// Persist after a working-copy mutation. A failed write reloads the last+ /// good row so this process does not keep a queue the store never saw.+ async fn persist_or_rollback(&self, session_id: AgentSessionId) -> Result<()> {+ if let Err(error) = self.write_queue(session_id).await {+ if let Err(reload) = self.reload_queue(session_id).await {+ tracing::error!(+ error = ?reload,+ %session_id,+ "failed to reload the agent session queue after a persist error"+ );+ }+ return Err(error);+ }+ Ok(())+ }++ /// Put a claimed entry back and persist. A persist failure here loses the+ /// in-flight item on the next restart — the same as losing the turn mark.+ async fn requeue_claimed(&self, session_id: AgentSessionId, entry: QueuedEntry) {+ self.queues.requeue_front(session_id, entry);+ if let Err(error) = self.write_queue(session_id).await {+ tracing::error!(+ error = ?error,+ %session_id,+ "failed to persist a requeued agent session action"+ );+ }+ }++ /// Replace the working copy from the session store.+ async fn reload_queue(&self, session_id: AgentSessionId) -> Result<()> {+ let stored = self.sessions.list_queued_actions(session_id).await?;+ let entries = stored+ .into_iter()+ .map(QueuedEntry::from_stored)+ .collect::<anyhow::Result<Vec<QueuedEntry>>>()+ .map_err(AgentSessionError::Unknown)?;+ self.queues.replace(session_id, entries);+ Ok(())+ }++ /// Restore the working copy from the session store and mark it current.+ /// Resume always does this so waiting prompts survive a replica restart.+ pub(super) async fn restore_queue(&self, session_id: AgentSessionId) -> Result<()> {+ self.reload_queue(session_id).await?;+ self.hydrated.insert(session_id);+ Ok(())+ }++ /// Load the durable queue the first time this process handles the session,+ /// so a later persist cannot wipe waiting actions this replica never saw.+ async fn ensure_hydrated(&self, session_id: AgentSessionId) -> Result<()> {+ if self.hydrated.contains(&session_id) {+ return Ok(());+ }+ if let Err(error) = self.restore_queue(session_id).await {+ self.hydrated.remove(&session_id);+ return Err(error);}Ok(())}@@ -727,6 +817,13 @@ wherelet Some(mut entry) = self.queues.claim_next(session_id) else {return Ok(Dispatch::QueueEmpty);};+ // Persist the remaining queue before any fallible work: a crash after+ // this leaves the claimed entry as in-flight (lost, like the turn+ // mark) and keeps every still-waiting action.+ if let Err(error) = self.write_queue(session_id).await {+ self.queues.requeue_front(session_id, entry);+ return Err(error);+ }// Compose a copy: the queued entry stays raw so a retry still edits// and re-composes the user's text, and the chip (below) still shows@@ -736,7 +833,7 @@ where.compose_action(&mut composed, entry.actor.as_ref(), entry.announce.as_ref()).await{- self.queues.requeue_front(session_id, entry);+ self.requeue_claimed(session_id, entry).await;return Err(error);}@@ -746,7 +843,7 @@ wherelet prompted_message_id = match self.sessions.next_prompt_message_id(session_id).await {Ok(message_id) => message_id,Err(error) => {- self.queues.requeue_front(session_id, entry);+ self.requeue_claimed(session_id, entry).await;return Err(error.into());}};@@ -764,7 +861,7 @@ where{Ok(announcement) => announcement,Err(error) => {- self.queues.requeue_front(session_id, entry);+ self.requeue_claimed(session_id, entry).await;return Err(error);}};@@ -772,7 +869,7 @@ wherematch self.announcer.announce(announcement).await {Ok(announced) => entry.announced = Some(announced.message_id),Err(error) => {- self.queues.requeue_front(session_id, entry);+ self.requeue_claimed(session_id, entry).await;return Err(error);}}@@ -808,7 +905,7 @@ whereOk(Dispatch::Dispatched)}Err(error) => {- self.queues.requeue_front(session_id, entry);+ self.requeue_claimed(session_id, entry).await;Err(error)}}diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rsindex e31460eb1a..1c6a366bd0 100644--- a/crates/agent_harness/src/domain/service/test.rs+++ b/crates/agent_harness/src/domain/service/test.rs@@ -27,6 +27,7 @@ use agent_session::PROTOCOL_VERSION;use agent_session::domain::events::AgentSessionLifecycleEvent;use agent_session::domain::model::{AgentMcpServers, AgentSessionId, CreateAgentSessionParams, Message, SandboxSize,+ StoredQueuedAction,};use agent_session::domain::ports::{AgentSessionLogRepo as _, AgentSessionNotificationRecipient as _, AgentSessionRepo as _,@@ -525,6 +526,45 @@ fn harness() -> TestBench {harness_with_context(PromptContextMock::default())}+/// A second replica on the same durable store — a process restart.+fn harness_sharing_repo(repo: InMemoryAgentSessionRepo) -> TestHarness {+ let turn_observer = Arc::new(agent_session::domain::ports::LateBoundTurnObserver::new());+ let lifecycle = RecordingLifecyclePublisher::new();+ let runtimes = RuntimeRegistry::new();+ let service = AgentHarnessService::new(+ AgentSessionServiceImpl::new(+ repo.clone(),+ FoldedMessageService::new(repo),+ NoOpRealtime,+ NoOpAgentSessionNameGenerator,+ turn_observer.clone(),+ Arc::new(lifecycle),+ 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+}+fn context_prompt(original: &str) -> String {format!("composed: {original}")}@@ -1930,6 +1970,102 @@ async fn a_prompt_during_a_running_turn_queues_and_dispatches_when_it_ends() {);}+#[tokio::test]+async fn a_queued_prompt_survives_a_replica_restart() {+ let ((service, repo, containers, _announcer, _runtimes), _turns) =+ harness_with_signals(PromptContextMock::default(), PromptComposerMock::default());+ let id = AgentSessionId::new();+ let _container = session_with_a_running_turn(&service, &containers, id).await;++ let accepted = service+ .control_event(+ id,+ ControlEvent {+ action: AgentAction::prompt("remember me after restart"),+ action_id: None,+ actor: Some(sender()),+ },+ )+ .await+ .expect("a mid-turn prompt is accepted");+ assert_eq!(+ accepted.disposition,+ agent_session::domain::ports::ControlDisposition::Queued+ );++ // The first replica's in-memory copy is gone; the store is not.+ drop(service);+ let restarted = harness_sharing_repo(repo);+ let queued = restarted+ .queued_controls(id)+ .await+ .expect("the durable queue is readable");+ assert_eq!(queued.len(), 1);+ assert_eq!(queued[0].action_id, accepted.action_id);+ assert_eq!(+ queued[0].action,+ AgentAction::prompt("remember me after restart")+ );+}++#[tokio::test]+async fn a_resume_restores_the_persisted_queue() {+ let (service, repo, containers, _announcer, _runtimes) = harness();+ let id = disconnected_session(&repo, &containers).await;+ let waiting_id = AgentActionId::mint();+ repo.replace_queued_actions(+ id,+ &[StoredQueuedAction {+ action_id: waiting_id,+ action: AgentAction::prompt("remember me"),+ actor: Some(staff_sender()),+ created_at: chrono::Utc::now(),+ announce: None,+ announced_message_id: None,+ }],+ )+ .await+ .expect("persist a waiting prompt");++ let prompted = service.control_event(+ id,+ ControlEvent {+ action: AgentAction::prompt("wake up"),+ action_id: None,+ actor: Some(staff_sender()),+ },+ );+ let drive_resume = async {+ loop {+ if containers.resumed() == 1 {+ break;+ }+ tokio::task::yield_now().await;+ }+ let resumed = containers+ .container(id)+ .expect("the resumed container is findable");+ complete_resume(&resumed).await;+ resumed.agent().wait_for_requests(3).await;+ resumed+ };+ let (result, resumed) = tokio::join!(prompted, drive_resume);+ result.expect("resume should deliver the restored prompt");++ assert_eq!(+ prompts(&resumed.agent()),+ [vec![ContentBlock::from("remember me")]],+ "the prompt persisted before disconnect dispatches first"+ );+ let queued = service+ .queued_controls(id)+ .await+ .expect("the follow-up stays queued");+ assert_eq!(queued.len(), 1);+ assert_eq!(queued[0].action, AgentAction::prompt("wake up"));+ assert_ne!(queued[0].action_id, waiting_id);+}+#[tokio::test]async fn a_stop_cancels_the_turn_and_the_queue_keeps_draining() {let ((service, _repo, containers, _announcer, _runtimes), _turns) =diff --git a/crates/agent_harness/src/outbound/cursor/manager/test.rs b/crates/agent_harness/src/outbound/cursor/manager/test.rsindex 64364f134f..605e7492cc 100644--- a/crates/agent_harness/src/outbound/cursor/manager/test.rs+++ b/crates/agent_harness/src/outbound/cursor/manager/test.rs@@ -198,6 +198,21 @@ impl AgentSessionRepo for StubSessions {) -> SessionResult<()> {unimplemented!("resizing is the harness service's job")}++ async fn list_queued_actions(+ &self,+ _id: AgentSessionId,+ ) -> SessionResult<Vec<agent_session::domain::model::StoredQueuedAction>> {+ unimplemented!("the manager never reads the queue")+ }++ async fn replace_queued_actions(+ &self,+ _id: AgentSessionId,+ _entries: &[agent_session::domain::model::StoredQueuedAction],+ ) -> SessionResult<()> {+ unimplemented!("the manager never writes the queue")+ }}/// A fake Cursor API: create/get/archive/stream, canned. Returns its base url,diff --git a/crates/agent_harness/src/outbound/routing/test.rs b/crates/agent_harness/src/outbound/routing/test.rsindex 4e344d0749..12d0c8c741 100644--- a/crates/agent_harness/src/outbound/routing/test.rs+++ b/crates/agent_harness/src/outbound/routing/test.rs@@ -250,6 +250,21 @@ impl AgentSessionRepo for FixedBotSessions {) -> SessionResult<()> {unimplemented!("resizing is the harness service's job")}++ async fn list_queued_actions(+ &self,+ _id: AgentSessionId,+ ) -> SessionResult<Vec<agent_session::domain::model::StoredQueuedAction>> {+ unimplemented!("the router never reads the queue")+ }++ async fn replace_queued_actions(+ &self,+ _id: AgentSessionId,+ _entries: &[agent_session::domain::model::StoredQueuedAction],+ ) -> SessionResult<()> {+ unimplemented!("the router never writes the queue")+ }}fn spawn_for(kind: AgentKind) -> SpawnContainer {diff --git a/crates/agent_session/src/domain/model.rs b/crates/agent_session/src/domain/model.rsindex eef4ed633b..389e6cbc75 100644--- a/crates/agent_session/src/domain/model.rs+++ b/crates/agent_session/src/domain/model.rs@@ -336,6 +336,43 @@ pub struct SessionBot {pub avatar_url: Option<String>,}+/// One waiting action as the session store records it.+///+/// The harness keeps an in-memory working copy beside the live actor; this+/// is the durable form a restart or a reader on another replica consults.+/// `announce` is the harness's channel/document origin, stored as JSON so+/// this crate does not depend on harness types.+#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct StoredQueuedAction {+ /// The id the action was accepted under.+ pub action_id: agent_runtime_protocol::domain::action::AgentActionId,+ /// What will be delivered - a prompt's text is the raw user text.+ pub action: agent_runtime_protocol::domain::action::AgentAction,+ /// The user who queued it, absent when a bot acted on nobody's behalf.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub actor: Option<MacroUserIdStr<'static>>,+ /// When it was accepted.+ pub created_at: DateTime<Utc>,+ /// Harness announce origin, opaque to this crate.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub announce: Option<serde_json::Value>,+ /// Chip message id, once posted, so a retry does not announce twice.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub announced_message_id: Option<Uuid>,+}++impl From<&StoredQueuedAction> for super::ports::QueuedControl {+ fn from(stored: &StoredQueuedAction) -> Self {+ Self {+ action_id: stored.action_id,+ action: stored.action.clone(),+ actor: stored.actor.clone(),+ created_at: stored.created_at,+ }+ }+}+/// One action waiting in a session's queue.////// Clients deserialize this, so both derives are used.diff --git a/crates/agent_session/src/domain/ports.rs b/crates/agent_session/src/domain/ports.rsindex 5da74d7e44..15f2943840 100644--- a/crates/agent_session/src/domain/ports.rs+++ b/crates/agent_session/src/domain/ports.rs@@ -485,6 +485,19 @@ pub trait AgentSessionRepo: Send + Sync + 'static {/// Delete an agent session by id.fn delete(&self, id: AgentSessionId) -> impl Future<Output = Result<()>> + Send;++ /// The session's waiting actions, oldest first. Missing row is empty.+ fn list_queued_actions(+ &self,+ id: AgentSessionId,+ ) -> impl Future<Output = Result<Vec<StoredQueuedAction>>> + Send;++ /// Replace the session's waiting actions. An empty slice deletes the row.+ fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> impl Future<Output = Result<()>> + Send;}/// The durable record of which provider-side agent an externally-serveddiff --git a/crates/agent_session/src/domain/service.rs b/crates/agent_session/src/domain/service.rsindex a6cdd51c2b..e3a6204fd1 100644--- a/crates/agent_session/src/domain/service.rs+++ b/crates/agent_session/src/domain/service.rs@@ -60,8 +60,8 @@ use super::model::{AgentSession, AgentSessionId, AgentSessionLog, AgentSessionPreview, AgentSessionRenamed,AuthorKind, ClaimOutcome, CreateAgentSessionParams, LogAppended, MAX_AGENT_SESSION_NAME_CHARS,MAX_PREVIEW_SESSION_IDS, Message, MessageId, ReplicaId, SandboxSize, SessionClaim, SessionLog,- SessionManagement, SessionPreviewCandidate, StoredAgentSessionLog, ThreadSession,- cursor_run_checkpoint,+ SessionManagement, SessionPreviewCandidate, StoredAgentSessionLog, StoredQueuedAction,+ ThreadSession, cursor_run_checkpoint,};use super::ports::{AgentConnector, AgentSessionLifecyclePublisher, AgentSessionLogRepo, AgentSessionLogWriter,@@ -271,6 +271,19 @@ pub trait AgentSessionService: Send + Sync + 'static {event: AgentSessionQueueChanged,) -> impl Future<Output = Result<()>> + Send;+ /// The session's waiting actions, oldest first. Missing row is empty.+ fn list_queued_actions(+ &self,+ id: AgentSessionId,+ ) -> impl Future<Output = Result<Vec<StoredQueuedAction>>> + Send;++ /// Replace the session's waiting actions. An empty slice deletes the row.+ fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> impl Future<Output = Result<()>> + Send;+/// Persist the sandbox size this session is running at.fn set_sandbox_size(&self,@@ -927,6 +940,18 @@ whereOk(self.realtime.publish_queue_changed(event).await?)}+ async fn list_queued_actions(&self, id: AgentSessionId) -> Result<Vec<StoredQueuedAction>> {+ self.repo.list_queued_actions(id).await+ }++ async fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> Result<()> {+ self.repo.replace_queued_actions(id, entries).await+ }+async fn set_sandbox_size(&self, id: AgentSessionId, size: SandboxSize) -> Result<()> {self.repo.set_sandbox_size(id, size).await}@@ -1598,6 +1623,18 @@ whereasync fn delete(&self, id: AgentSessionId) -> Result<()> {self.repo.delete(id).await}++ async fn list_queued_actions(&self, id: AgentSessionId) -> Result<Vec<StoredQueuedAction>> {+ self.repo.list_queued_actions(id).await+ }++ async fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> Result<()> {+ self.repo.replace_queued_actions(id, entries).await+ }}impl<R, Rt> LiveSessionLogWriter<R, Rt>diff --git a/crates/agent_session/src/domain/service/test.rs b/crates/agent_session/src/domain/service/test.rsindex c4e51dfc1b..259ff30b36 100644--- a/crates/agent_session/src/domain/service/test.rs+++ b/crates/agent_session/src/domain/service/test.rs@@ -592,6 +592,21 @@ impl AgentSessionRepo for BlockingPromptLogs {async fn delete(&self, id: AgentSessionId) -> Result<()> {self.repo.delete(id).await}++ async fn list_queued_actions(+ &self,+ id: AgentSessionId,+ ) -> Result<Vec<crate::domain::model::StoredQueuedAction>> {+ self.repo.list_queued_actions(id).await+ }++ async fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[crate::domain::model::StoredQueuedAction],+ ) -> Result<()> {+ self.repo.replace_queued_actions(id, entries).await+ }}/// Pure delegation: the lease semantics under test live in the shareddiff --git a/crates/agent_session/src/outbound/postgres.rs b/crates/agent_session/src/outbound/postgres.rsindex ea5ca331e1..138e0d5675 100644--- a/crates/agent_session/src/outbound/postgres.rs+++ b/crates/agent_session/src/outbound/postgres.rs@@ -10,6 +10,7 @@ use sqlx::types::Json;mod test;mod pull_request;+mod queue;mod sharing;mod turn_state;mod working_branch;@@ -19,7 +20,7 @@ use crate::domain::model::{AgentMcpServers, AgentSession, AgentSessionId, AgentSessionLog, AgentSessionPreviewData,ClaimOutcome, CreateAgentSessionParams, ExternalSession, ManagerFence, Message, ReplicaAddress,ReplicaId, SandboxSize, SessionBot, SessionClaim, SessionManager, SessionPreviewCandidate,- SessionStatus, StoredAgentSessionLog, ThreadSession, cursor_run_checkpoint,+ SessionStatus, StoredAgentSessionLog, StoredQueuedAction, ThreadSession, cursor_run_checkpoint,};use crate::domain::ports::{AgentSessionLogRepo, AgentSessionRepo, ExternalSessionRepo, REPLICA_STALE_AFTER,@@ -903,6 +904,18 @@ impl AgentSessionRepo for PgAgentSessionRepo {Ok(())}+ async fn list_queued_actions(&self, id: AgentSessionId) -> Result<Vec<StoredQueuedAction>> {+ self.load_queued_actions(id).await+ }++ async fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> Result<()> {+ self.store_queued_actions(id, entries).await+ }+async fn delete(&self, id: AgentSessionId) -> Result<()> {let mut transaction = self.pooldiff --git a/crates/agent_session/src/outbound/postgres/queue.rs b/crates/agent_session/src/outbound/postgres/queue.rsnew file mode 100644index 0000000000..bafedea2e2--- /dev/null+++ b/crates/agent_session/src/outbound/postgres/queue.rs@@ -0,0 +1,61 @@+//! Durable waiting actions for a session.++use super::*;+use crate::domain::model::StoredQueuedAction;+use sqlx::types::Json;++impl PgAgentSessionRepo {+ pub(super) async fn load_queued_actions(+ &self,+ id: AgentSessionId,+ ) -> Result<Vec<StoredQueuedAction>> {+ let row = sqlx::query!(+ r#"+ SELECT entries AS "entries: Json<Vec<StoredQueuedAction>>"+ FROM agent_session_queue+ WHERE agent_session_id = $1+ "#,+ id.as_uuid(),+ )+ .fetch_optional(&self.pool)+ .await+ .context("list agent session queue")?;+ Ok(row.map(|row| row.entries.0).unwrap_or_default())+ }++ pub(super) async fn store_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> Result<()> {+ if entries.is_empty() {+ sqlx::query!(+ r#"+ DELETE FROM agent_session_queue+ WHERE agent_session_id = $1+ "#,+ id.as_uuid(),+ )+ .execute(&self.pool)+ .await+ .context("clear agent session queue")?;+ return Ok(());+ }++ sqlx::query!(+ r#"+ INSERT INTO agent_session_queue (agent_session_id, entries)+ VALUES ($1, $2)+ ON CONFLICT (agent_session_id) DO UPDATE SET+ entries = EXCLUDED.entries,+ updated_at = now()+ "#,+ id.as_uuid(),+ Json(entries) as _,+ )+ .execute(&self.pool)+ .await+ .context("replace agent session queue")?;+ Ok(())+ }+}diff --git a/crates/agent_session/src/outbound/postgres/test.rs b/crates/agent_session/src/outbound/postgres/test.rsindex c1bcfd5000..11eb949778 100644--- a/crates/agent_session/src/outbound/postgres/test.rs+++ b/crates/agent_session/src/outbound/postgres/test.rs@@ -1,4 +1,5 @@use super::*;+mod queue;mod search;mod user_cleanup;mod working_branch;diff --git a/crates/agent_session/src/outbound/postgres/test/queue.rs b/crates/agent_session/src/outbound/postgres/test/queue.rsnew file mode 100644index 0000000000..45fd5b2a73--- /dev/null+++ b/crates/agent_session/src/outbound/postgres/test/queue.rs@@ -0,0 +1,110 @@+use super::*;+use crate::domain::model::StoredQueuedAction;+use crate::domain::ports::AgentSessionRepo;+use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};+use macro_db_migrator::MACRO_DB_MIGRATIONS;++fn stored_prompt(text: &str) -> StoredQueuedAction {+ StoredQueuedAction {+ action_id: AgentActionId::mint(),+ action: AgentAction::prompt(text),+ actor: Some(user_id(OWNER)),+ created_at: chrono::Utc::now(),+ announce: Some(serde_json::json!({+ "parent": { "type": "channel", "id": "00000000-0000-0000-0000-0000000000f0" },+ "threadId": "00000000-0000-0000-0000-0000000000f1",+ "messageId": "00000000-0000-0000-0000-0000000000f2"+ })),+ announced_message_id: None,+ }+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn replace_lists_oldest_first_and_empty_deletes_the_row(pool: PgPool) {+ let repo = PgAgentSessionRepo::new(pool.clone());+ let bot_id = create_test_bot(&pool).await;+ let session = create_session(&repo, new_session(bot_id, None, None)).await;++ let first = stored_prompt("first");+ let second = stored_prompt("second");+ repo.replace_queued_actions(session.id, &[first.clone(), second.clone()])+ .await+ .expect("persist queue");++ let listed = repo+ .list_queued_actions(session.id)+ .await+ .expect("list queue");+ assert_eq!(+ listed+ .iter()+ .map(|entry| entry.action_id)+ .collect::<Vec<_>>(),+ [first.action_id, second.action_id]+ );+ assert_eq!(listed[0].action, AgentAction::prompt("first"));+ assert_eq!(listed[0].announce, first.announce);++ repo.replace_queued_actions(session.id, &[])+ .await+ .expect("clear queue");+ assert!(+ repo.list_queued_actions(session.id)+ .await+ .expect("list empty queue")+ .is_empty()+ );++ let remaining = sqlx::query_scalar!(+ r#"+ SELECT count(*) AS "count!"+ FROM agent_session_queue+ WHERE agent_session_id = $1+ "#,+ session.id.as_uuid(),+ )+ .fetch_one(&pool)+ .await+ .expect("count queue rows");+ assert_eq!(remaining, 0);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn deleting_a_session_cascades_its_queue(pool: PgPool) {+ let repo = PgAgentSessionRepo::new(pool.clone());+ let bot_id = create_test_bot(&pool).await;+ let session = create_session(&repo, new_session(bot_id, None, None)).await;+ repo.replace_queued_actions(session.id, &[stored_prompt("remember me")])+ .await+ .expect("persist queue");++ AgentSessionRepo::delete(&repo, session.id)+ .await+ .expect("delete session");++ let remaining = sqlx::query_scalar!(+ r#"+ SELECT count(*) AS "count!"+ FROM agent_session_queue+ WHERE agent_session_id = $1+ "#,+ session.id.as_uuid(),+ )+ .fetch_one(&pool)+ .await+ .expect("count queue rows");+ assert_eq!(remaining, 0);+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_session_without_a_row_lists_empty(pool: PgPool) {+ let repo = PgAgentSessionRepo::new(pool.clone());+ let bot_id = create_test_bot(&pool).await;+ let session = create_session(&repo, new_session(bot_id, None, None)).await;+ assert!(+ repo.list_queued_actions(session.id)+ .await+ .expect("list missing queue")+ .is_empty()+ );+}diff --git a/crates/agent_session/src/testing.rs b/crates/agent_session/src/testing.rsindex b0233d0bfb..24a5489856 100644--- a/crates/agent_session/src/testing.rs+++ b/crates/agent_session/src/testing.rs@@ -10,7 +10,8 @@ use crate::domain::model::{AgentMcpServers, AgentSession, AgentSessionId, AgentSessionLog, AgentSessionPreviewData,ClaimOutcome, CreateAgentSessionParams, DEFAULT_AGENT_SESSION_NAME, LogAppended, ManagerFence,ReplicaAddress, ReplicaId, SandboxSize, SessionBot, SessionClaim, SessionManager,- SessionPreviewCandidate, SessionStatus, StoredAgentSessionLog, ThreadSession,+ SessionPreviewCandidate, SessionStatus, StoredAgentSessionLog, StoredQueuedAction,+ ThreadSession,};use crate::domain::ports::{AgentSessionLifecyclePublisher, AgentSessionLogRepo, AgentSessionRealtime, AgentSessionRepo,@@ -59,6 +60,8 @@ pub struct InMemoryAgentSessionRepo {/// Session -> lease, mirroring the lease columns: release clears the/// holder and leaves the counter.leases: Arc<Mutex<HashMap<AgentSessionId, Lease>>>,+ /// Session -> waiting actions, mirroring `agent_session_queue`.+ queues: Arc<Mutex<HashMap<AgentSessionId, Vec<StoredQueuedAction>>>>,}impl InMemoryAgentSessionRepo {@@ -444,6 +447,37 @@ impl AgentSessionRepo for InMemoryAgentSessionRepo {.lock().expect("in-memory log store is not poisoned").remove(&id);+ self.queues+ .lock()+ .expect("in-memory queue store is not poisoned")+ .remove(&id);+ Ok(())+ }++ async fn list_queued_actions(&self, id: AgentSessionId) -> Result<Vec<StoredQueuedAction>> {+ Ok(self+ .queues+ .lock()+ .expect("in-memory queue store is not poisoned")+ .get(&id)+ .cloned()+ .unwrap_or_default())+ }++ async fn replace_queued_actions(+ &self,+ id: AgentSessionId,+ entries: &[StoredQueuedAction],+ ) -> Result<()> {+ let mut queues = self+ .queues+ .lock()+ .expect("in-memory queue store is not poisoned");+ if entries.is_empty() {+ queues.remove(&id);+ } else {+ queues.insert(id, entries.to_vec());+ }Ok(())}}diff --git a/crates/macro_db_client/migrations/20260925001120_persist_agent_session_queue.sql b/crates/macro_db_client/migrations/20260925001120_persist_agent_session_queue.sqlnew file mode 100644index 0000000000..ea60aa9274--- /dev/null+++ b/crates/macro_db_client/migrations/20260925001120_persist_agent_session_queue.sql@@ -0,0 +1,12 @@+-- Waiting turn-occupying actions for a session, oldest first.+--+-- One row per session, replaced on every queue mutation. The live replica+-- still holds an in-memory working copy; this is what a restart or a reader+-- on another replica consults. Cascade: a deleted session's queue goes with+-- it. Empty queues delete the row rather than storing `[]`.+CREATE TABLE agent_session_queue (+ agent_session_id UUID PRIMARY KEY REFERENCES agent_session(id) ON DELETE CASCADE,+ -- agent_session::domain::model::StoredQueuedAction, oldest first.+ entries JSONB NOT NULL DEFAULT '[]'::jsonb,+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now()+);diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.mdindex 013120175e..d366a5a280 100644--- a/docs/AGENT_GUIDE/ai-chat.md+++ b/docs/AGENT_GUIDE/ai-chat.md@@ -748,7 +748,9 @@ must stay hidden; subsequent live messages must still appear.- Sending is never blocked by a running turn. A prompt sent mid-turn is queued**server-side** and dispatches automatically when the current turn ends, one per turn.The queue holds at most 50 entries; past that a send is refused with an error rather- than queued.+ than queued. Waiting prompts are written through to the session store as they are+ accepted, edited, or removed, and are restored when the session resumes after a+ harness restart — they must not disappear if the managing replica drains.- Queued prompts render as a list between the transcript and the input, newest at thetop — the prompt about to be sent sits at the bottom, immediately above the input.Each row shows a `Queued` label (with `by {user}` when someone else queued it —