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
refactor(harness): a bot's sessions either have a repository or do not (#6460)
Wolf Mermelsteinc2172a358d0a
diff --git a/crates/agent_egress/src/domain/model/test.rs b/crates/agent_egress/src/domain/model/test.rsindex d979a9f356..dcb65d134d 100644--- a/crates/agent_egress/src/domain/model/test.rs+++ b/crates/agent_egress/src/domain/model/test.rs@@ -231,6 +231,33 @@ fn a_repo_slug_rejects_rather_than_repairs() {}}+/// A configured URL is read into a slug only when it names a repository and+/// nothing else: this is the one reading of `repo_url`, and the harness mints+/// a session's token against whatever it says.+#[test]+fn reads_the_repository_out_of_a_configured_url() {+ for url in [+ "https://github.com/macro-inc/macro",+ "https://github.com/macro-inc/macro/",+ "https://github.com/macro-inc/macro.git",+ ] {+ let repo = RepoSlug::parse_github_url(url).expect(url);+ assert_eq!(repo.to_string(), "macro-inc/macro", "for {url}");+ }++ for url in [+ "",+ "not a url",+ "https://github.com",+ "https://github.com/macro-inc",+ "https://gitlab.com/macro-inc/macro",+ "https://github.com.evil.example/macro-inc/macro",+ "https://github.com/macro-inc/macro/tree/main",+ ] {+ assert_eq!(RepoSlug::parse_github_url(url), None, "accepted {url}");+ }+}+/// The allowlist is the whole point: anything not one of the three smart-HTTP/// routes - the dumb protocol's object endpoints most of all - is not a target/// this crate can name.diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rsindex 07297c3a97..1095d4f887 100644--- a/crates/agent_harness/src/domain/model.rs+++ b/crates/agent_harness/src/domain/model.rs@@ -1,7 +1,7 @@//! Commands and values used by the harness domain.use agent_client_protocol::schema::v1::{HttpHeader, McpServer as AcpMcpServer, McpServerHttp};-use agent_egress::domain::model::McpServerSlug;+use agent_egress::domain::model::{McpServerSlug, RepoSlug};use agent_fold::domain::model::TurnSignal;use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};use agent_session::domain::model::{AgentMcpServers, AgentSessionId, MessageId, SandboxSize};@@ -500,6 +500,36 @@ impl std::fmt::Debug for SandboxEgress {}}+/// The repository a deployment's sessions work in, valid by construction.+///+/// Held as the URL a session's row carries, not as the [`RepoSlug`] it was+/// read as: the row is what the egress proxy re-reads to decide which+/// repository a sandbox's git traffic may reach, so the URL is the value+/// that has to survive. Parsing is what makes it a repository rather than a+/// string - a URL that names no repository could only fail later, at a clone+/// nobody is watching - and the parse is the proxy's own.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SessionRepository(String);++impl SessionRepository {+ /// Read a configured GitHub URL as the repository it names.+ ///+ /// [`None`] for a URL that names no repository. That is a deployment+ /// misconfiguration, and the composition root is where it should be+ /// refused: every session this deployment would go on to open carries it.+ #[must_use]+ pub fn parse(repository_url: &str) -> Option<Self> {+ RepoSlug::parse_github_url(repository_url)?;+ Some(Self(repository_url.to_owned()))+ }++ /// The URL, as a session's row carries it.+ #[must_use]+ pub fn as_str(&self) -> &str {+ &self.0+ }+}+/// Session-row values that remain deployment configuration for now.#[derive(Debug, Clone)]pub struct SessionDefaults {@@ -513,8 +543,16 @@ pub struct SessionDefaults {pub model: String,/// Harness slug, e.g. `opencode`.pub harness: String,- /// Repository sessions run against.- pub repo_url: String,+ /// Repository this bot's sessions open against, or [`None`] for a bot+ /// whose sessions do not learn one until they run.+ ///+ /// A Codex cloud session is the latter: it works in whatever repository+ /// its cloud environment holds, which is not known until the environment+ /// resolves, and the row is written then (see+ /// `CodexRuntime::resolve_target`). Seeding the row with a deployment+ /// default would make it briefly claim a repository the session will+ /// never touch.+ pub repo_url: Option<SessionRepository>,}/// Session defaults for every bot a deployment answers for.@@ -581,3 +619,26 @@ impl From<SessionDefaults> for HarnessDefaults {Self::new(default)}}++#[cfg(test)]+mod test {+ use super::SessionRepository;++ /// The URL survives verbatim - it is what a session's row carries, and+ /// what the egress proxy re-reads - and one that names no repository is+ /// refused rather than repaired. The shapes themselves are the parser's+ /// own tests, in `agent_egress`.+ #[test]+ fn a_session_repository_keeps_the_url_it_was_read_from() {+ let repository =+ SessionRepository::parse("https://github.com/macro-inc/macro.git").expect("a repo");+ assert_eq!(+ repository.as_str(),+ "https://github.com/macro-inc/macro.git"+ );++ for url in ["", "https://github.com/macro-inc", "not a url"] {+ assert_eq!(SessionRepository::parse(url), None, "accepted {url}");+ }+ }+}diff --git a/crates/agent_harness/src/domain/ports.rs b/crates/agent_harness/src/domain/ports.rsindex a58341e93e..d144f5860c 100644--- a/crates/agent_harness/src/domain/ports.rs+++ b/crates/agent_harness/src/domain/ports.rs@@ -283,17 +283,13 @@ pub trait SandboxEgressProvisioner: Send + Sync + 'static {/// advertised; under [`AgentMcpServers::Selected`] exactly the listed/// apps are, connected or not.///- /// `repo_url` is [`None`] for a session that clones nothing from this- /// deployment's repository - a Codex cloud session works in the cloud- /// environment's own checkout - and the deployment's configured URL- /// otherwise, which is validated here so a session whose git traffic- /// could never resolve fails at provisioning rather than at the agent's- /// first clone.+ /// The session's repository is not named here: nothing about minting a+ /// token depends on it, and the URL a session carries is read as a+ /// repository once, where it is configured.fn provision(&self,session: AgentSessionId,owner: &MacroUserIdStr<'static>,- repo_url: Option<&str>,selection: &AgentMcpServers,) -> impl Future<Output = Result<ProvisionedEgress>> + Send;diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rsindex 2399692d2d..ae39ce16da 100644--- a/crates/agent_harness/src/domain/service/deliver.rs+++ b/crates/agent_harness/src/domain/service/deliver.rs@@ -97,7 +97,6 @@ where.provision(session_id,&session.owner_id,- session.repo_url.as_deref(),&AgentMcpServers::Selected {servers: Vec::new(),},diff --git a/crates/agent_harness/src/domain/service/open.rs b/crates/agent_harness/src/domain/service/open.rsindex 51f64ff7b4..45653c6c81 100644--- a/crates/agent_harness/src/domain/service/open.rs+++ b/crates/agent_harness/src/domain/service/open.rs@@ -157,10 +157,6 @@ where};}let defaults = self.inner.defaults.for_bot(bot_id);- // A Codex cloud session clones nothing here: it works in its cloud- // environment's own checkout, so this deployment's repository URL is- // neither stored on the row nor validated at provisioning.- let repo_url = (kind != AgentKind::CodexCloud).then(|| defaults.repo_url.clone());let sandbox_size = self.inner.sessions@@ -173,12 +169,7 @@ wherelet egress = self.inner.egress- .provision(- session_id,- &request.owner,- repo_url.as_deref(),- &mcp_servers,- )+ .provision(session_id, &request.owner, &mcp_servers).await.map_err(into_session_error)?;let session = self@@ -192,7 +183,13 @@ whereoriginating_message_id: None,model,harness,- repo_url,+ // Whatever this bot's sessions work in: the deployment's+ // repository, or nothing for a bot whose sessions work+ // somewhere this deployment does not name.+ repo_url: defaults+ .repo_url+ .as_ref()+ .map(|repo| repo.as_str().to_owned()),// Managed sandboxes run in the path baked into their image.workspace: agent_session::MANAGED_CONTAINER_WORKSPACE.to_owned(),sandbox_size,@@ -340,9 +337,6 @@ where} = command;tracing::Span::current().record("agent.session.id", tracing::field::display(session_id));let defaults = self.defaults.for_bot(bot_id);- // See the request path: a Codex cloud session has no repository URL- // of this deployment's to clone, store, or validate.- let repo_url = (runtime.kind != AgentKind::CodexCloud).then(|| defaults.repo_url.clone());let sandbox_size = self.sessions.user_sandbox_size(&origin.sender).await?;// Provisioned before the session exists, because the row is what makes@@ -352,12 +346,7 @@ where// credentials, so there is nowhere else it could correctly come from.let egress = self.egress- .provision(- session_id,- &origin.sender,- repo_url.as_deref(),- &runtime.mcp_servers,- )+ .provision(session_id, &origin.sender, &runtime.mcp_servers).await?;let session = self@@ -370,7 +359,10 @@ whereoriginating_message_id: Some(origin.message_id),model: runtime.model.clone(),harness: runtime.harness.clone(),- repo_url,+ repo_url: defaults+ .repo_url+ .as_ref()+ .map(|repo| repo.as_str().to_owned()),// Managed sandboxes run in the path baked into their image.workspace: agent_session::MANAGED_CONTAINER_WORKSPACE.to_owned(),sandbox_size,diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rsindex c8250e134e..ed270d9994 100644--- a/crates/agent_harness/src/domain/service/test.rs+++ b/crates/agent_harness/src/domain/service/test.rs@@ -39,7 +39,7 @@ use crate::domain::error::HarnessError;use crate::domain::model::{AgentKind, AgentRuntimeConfig, AnnounceOrigin, CommandOutcome, DeliverAction, HarnessCommand,HarnessDefaults, MentionOrigin, OpenSession, PriorChannelMessage, SessionDefaults,- SpawnContainer,+ SessionRepository, SpawnContainer,};use crate::domain::ports::{AgentPromptComposer, ChannelPromptContext, ContainerManager as _, NoPeers,@@ -357,7 +357,7 @@ fn harness_with_mentions(bot_id: BotId::TEST_A,model: "claude".to_owned(),harness: "opencode".to_owned(),- repo_url: "https://github.com/macro-inc/macro".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),}).with_bot(bot_id::CODEX_BOT_ID,@@ -365,7 +365,7 @@ fn harness_with_mentions(bot_id: bot_id::CODEX_BOT_ID,model: String::new(),harness: "codex-cloud".into(),- repo_url: String::new(),+ repo_url: None,},),lifecycle.clone(),@@ -1895,7 +1895,7 @@ async fn a_managed_session_opens_as_the_managed_default_bot() {bot_id: BotId::TEST_A,model: "claude".to_owned(),harness: "opencode".to_owned(),- repo_url: "https://github.com/macro-inc/macro".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),}).with_bot(inmem_bot,@@ -1903,7 +1903,7 @@ async fn a_managed_session_opens_as_the_managed_default_bot() {bot_id: inmem_bot,model: "fast-model".to_owned(),harness: "macro-inmem".to_owned(),- repo_url: "https://github.com/macro-inc/macro".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),},).with_managed_bot(inmem_bot),@@ -2353,7 +2353,7 @@ async fn commands_for_a_peer_managed_session_forward_through_redis() {bot_id: BotId::TEST_A,model: "claude".to_owned(),harness: "opencode".to_owned(),- repo_url: "https://github.com/macro-inc/macro".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),},NoopLifecyclePublisher,crate::domain::pending::PendingCommands::new(),@@ -2404,7 +2404,7 @@ async fn unmanaged_external_session_forwards_to_its_remote_harness() {bot_id: BotId::TEST_A,model: "claude".to_owned(),harness: "opencode".to_owned(),- repo_url: "https://github.com/macro-inc/macro".to_owned(),+ repo_url: SessionRepository::parse("https://github.com/macro-inc/macro"),},NoopLifecyclePublisher,crate::domain::pending::PendingCommands::new(),diff --git a/crates/agent_harness/src/outbound/egress.rs b/crates/agent_harness/src/outbound/egress.rsindex 8728972555..a1a8682a3c 100644--- a/crates/agent_harness/src/outbound/egress.rs+++ b/crates/agent_harness/src/outbound/egress.rs@@ -9,11 +9,10 @@//! each end reads at runtime as "every request from every sandbox is//! unauthenticated".-use agent_egress::domain::model::{McpServerSlug, RepoSlug, SessionToken};+use agent_egress::domain::model::{McpServerSlug, SessionToken};use macro_user_id::user_id::MacroUserIdStr;use pipedream_mcp::domain::ports::ConnectionStore;use std::sync::Arc;-use url::Url;use crate::domain::error::{HarnessError, Result};use crate::domain::model::{ProvisionedEgress, SandboxEgress};@@ -23,11 +22,6 @@ use agent_session::domain::model::{AgentMcpServers, AgentSessionId};#[cfg(test)]mod test;-/// The only host a configured repository URL may name. Validating the-/// deployment's own configuration, not a request: a URL pointing somewhere else-/// would mint a session token for a repository the proxy cannot reach.-const GITHUB_HOST: &str = "github.com";-/// Mints session tokens and gathers the MCP servers a sandbox may dial.pub struct EgressProvisioner<Connections> {connections: Arc<Connections>,@@ -126,17 +120,8 @@ where&self,session: AgentSessionId,owner: &MacroUserIdStr<'static>,- repo_url: Option<&str>,selection: &AgentMcpServers,) -> Result<ProvisionedEgress> {- // Validated even though nothing here uses it: it is the deployment's- // repository URL, and a session whose git traffic could never resolve- // should fail at provisioning rather than at the agent's first clone.- // A session that clones nothing has no URL to validate.- if let Some(repo_url) = repo_url {- repo_slug(repo_url)?;- }-let token = SessionToken::mint();Ok(ProvisionedEgress {@@ -163,34 +148,3 @@ where})}}--/// The repository a configured GitHub URL names.-///-/// Deliberately narrow: it must be `https://github.com/<owner>/<name>` and-/// nothing else. This reads the deployment's own configuration, so a URL that-/// is nearly right - a different host, an extra path segment - is a mistake-/// worth failing on rather than guessing at, and the answer decides which-/// repository a session's credential will be minted for.-fn repo_slug(repo_url: &str) -> Result<RepoSlug> {- let unusable = || {- HarnessError::Egress(rootcause::report!(- "configured repository url does not name a github repository"- ))- };-- let url = Url::parse(repo_url).map_err(|_| unusable())?;- if url.host_str() != Some(GITHUB_HOST) {- return Err(unusable());- }-- let mut segments = url.path_segments().ok_or_else(unusable)?;- let owner = segments.next().ok_or_else(unusable)?;- let name = segments.next().ok_or_else(unusable)?;- // Anything after the repository name is not part of it. A trailing empty- // segment is just a trailing slash.- if segments.any(|segment| !segment.is_empty()) {- return Err(unusable());- }-- RepoSlug::parse(owner, name.trim_end_matches(".git")).ok_or_else(unusable)-}diff --git a/crates/agent_harness/src/outbound/egress/test.rs b/crates/agent_harness/src/outbound/egress/test.rsindex 45fe764851..99d1d35be2 100644--- a/crates/agent_harness/src/outbound/egress/test.rs+++ b/crates/agent_harness/src/outbound/egress/test.rs@@ -5,33 +5,6 @@ fn slug(name: &str) -> McpServerSlug {McpServerSlug::parse(name).expect("a valid app slug")}-#[test]-fn reads_the_repository_out_of_a_configured_url() {- for url in [- "https://github.com/macro-inc/macro",- "https://github.com/macro-inc/macro/",- "https://github.com/macro-inc/macro.git",- ] {- let repo = repo_slug(url).expect(url);- assert_eq!(repo.to_string(), "macro-inc/macro", "for {url}");- }-}--#[test]-fn refuses_a_url_that_does_not_name_a_repository() {- for url in [- "",- "not a url",- "https://github.com",- "https://github.com/macro-inc",- "https://gitlab.com/macro-inc/macro",- "https://github.com.evil.example/macro-inc/macro",- "https://github.com/macro-inc/macro/tree/main",- ] {- assert!(repo_slug(url).is_err(), "accepted {url}");- }-}-struct FixedConnections(Vec<pipedream_mcp::domain::models::PipedreamConnection>);impl ConnectionStore for FixedConnections {@@ -97,7 +70,6 @@ async fn lists_enabled_app_slugs_verbatim() {.provision(AgentSessionId::new(),&MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),- Some("https://github.com/macro-inc/macro"),&AgentMcpServers::OwnerConnections,).await@@ -112,30 +84,6 @@ async fn lists_enabled_app_slugs_verbatim() {assert_eq!(slugs, ["linear", "google_sheets"]);}-/// A session that clones nothing from this deployment provisions without a-/// repository URL: a Codex cloud session works in its cloud environment's own-/// checkout, and there is no configured URL to hold it to.-#[tokio::test]-async fn provisions_a_session_that_has_no_repository_url() {- let provisioner = EgressProvisioner::new(- Arc::new(FixedConnections(vec![connection("linear", true)])),- "https://egress.macro.com",- );-- let provisioned = provisioner- .provision(- AgentSessionId::new(),- &MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),- None,- &selected(&[]),- )- .await- .expect("provisioned");-- assert!(provisioned.sandbox.mcp_servers.is_empty());- assert!(!provisioned.session_token_hash.is_empty());-}-/// `restore` rebuilds the same environment around a token that already/// exists: nothing is minted, and the server list is read fresh.#[tokio::test]@@ -193,7 +141,6 @@ async fn a_selected_list_is_advertised_regardless_of_connections() {.provision(AgentSessionId::new(),&MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),- Some("https://github.com/macro-inc/macro"),&selected(&["notion", "linear", "Not A Slug!"]),).await@@ -236,7 +183,6 @@ async fn an_empty_selection_offers_nothing_of_the_owners() {.provision(AgentSessionId::new(),&MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),- Some("https://github.com/macro-inc/macro"),&selected(&[]),).awaitdiff --git a/crates/agent_harness/src/testing/helpers/egress.rs b/crates/agent_harness/src/testing/helpers/egress.rsindex 6bbe22a382..a1d156d28e 100644--- a/crates/agent_harness/src/testing/helpers/egress.rs+++ b/crates/agent_harness/src/testing/helpers/egress.rs@@ -9,9 +9,9 @@ use crate::domain::error::Result;use crate::domain::model::{ProvisionedEgress, SandboxEgress};use crate::domain::ports::SandboxEgressProvisioner;-/// One recorded provisioning: session, owner, repository URL, and the MCP-/// selection it was asked to advertise.-pub type RecordedProvisioning = (AgentSessionId, String, Option<String>, AgentMcpServers);+/// One recorded provisioning: session, owner, and the MCP selection it was+/// asked to advertise.+pub type RecordedProvisioning = (AgentSessionId, String, AgentMcpServers);/// A [`SandboxEgressProvisioner`] that records who it was asked for and hands/// back a fixed environment. Cloning shares one record.@@ -27,8 +27,8 @@ impl EgressProvisionerMock {Self::default()}- /// Every provisioning recorded, as session, owner, repository URL, and- /// the MCP selection it was asked to advertise.+ /// Every provisioning recorded, as session, owner, and the MCP selection+ /// it was asked to advertise.#[must_use]pub fn provisioned(&self) -> Vec<RecordedProvisioning> {self.provisioned@@ -43,18 +43,12 @@ impl SandboxEgressProvisioner for EgressProvisionerMock {&self,session: AgentSessionId,owner: &MacroUserIdStr<'static>,- repo_url: Option<&str>,selection: &AgentMcpServers,) -> Result<ProvisionedEgress> {self.provisioned.lock().expect("egress mock lock should not be poisoned")- .push((- session,- owner.to_string(),- repo_url.map(str::to_owned),- selection.clone(),- ));+ .push((session, owner.to_string(), selection.clone()));Ok(ProvisionedEgress {sandbox: test_egress(),diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex 55d2318c39..a3bd31cb7a 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -32,6 +32,7 @@ use agent_egress::outbound::session_authority::StoredTokenSessionAuthority;use agent_fold::domain::service::FoldedMessageService;use agent_harness::domain::model::{AgentKind, AgentRuntimeConfig, HarnessCommand, HarnessDefaults, SessionDefaults,+ SessionRepository,};use agent_harness::domain::model_load::AgentModelsServiceImpl;use agent_harness::domain::ports::AgentRuntimeDirectory as _;@@ -628,11 +629,21 @@ async fn run() -> anyhow::Result<()> {let runtimes = RuntimeRegistry::with_presence(Arc::new(PgHarnessPresence::new(pool.clone())));let redis = redis::Client::open(config.redis_uri.as_ref()).context("failed to create the runtime command Redis client")?;+ // 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+ // and every session failing to open.+ let repo_url = SessionRepository::parse(&config.harness_repo_url).with_context(|| {+ format!(+ "HARNESS_REPO_URL does not name a github repository: {}",+ config.harness_repo_url+ )+ })?;let defaults = HarnessDefaults::new(SessionDefaults {bot_id,model: config.harness_model.clone(),harness: config.harness_slug.clone(),- repo_url: config.harness_repo_url.clone(),+ repo_url: Some(repo_url.clone()),}).with_bot(inmem_bot,@@ -642,7 +653,7 @@ async fn run() -> anyhow::Result<()> {harness: config.inmem_harness_slug.clone(),// Stamped but unused: the in-process agent has no// workspace to clone anything into.- repo_url: config.harness_repo_url.clone(),+ repo_url: Some(repo_url),},).with_bot(@@ -651,7 +662,11 @@ async fn run() -> anyhow::Result<()> {bot_id: bot_id::CODEX_BOT_ID,model: String::new(),harness: "codex-cloud".into(),- repo_url: String::new(),+ // A Codex cloud session works in whatever repository its cloud+ // environment holds, and records it on the row once that+ // environment resolves. Nothing to seed it with here, and this+ // deployment's own repository would be the wrong guess.+ repo_url: None,},)// Sessions nothing names a bot for (the create menu's) run in-process;