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
feat(agent-trigger): route thread replies to agents with Jev (#8680)
Eric Hayes151745e19436
diff --git a/Cargo.lock b/Cargo.lockindex 6fac79a84f..e2cc3813b6 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -917,6 +917,7 @@ dependencies = ["document_sub_type","entity_access","initiative",+ "jev","lexical_client","macro_db_migrator","macro_event_broker",@@ -964,6 +965,7 @@ dependencies = ["entity_registry","entity_registry_db_utils","initiative",+ "jev","kafka_util","lexical_client","macro_config",diff --git a/crates/agent_trigger/AGENTS.md b/crates/agent_trigger/AGENTS.mdindex 112cdb03cc..6016a75e1f 100644--- a/crates/agent_trigger/AGENTS.md+++ b/crates/agent_trigger/AGENTS.md@@ -19,8 +19,9 @@ session that results.that were not on the mentioning message.- `src/domain/task_assignment.rs` / `src/domain/project_assignment.rs`:non-message trigger paths.-- `src/outbound/fast_model_judge.rs`: the Haiku-class implicit-trigger- classifier.+- `src/outbound/jev_judge.rs`: the Jev implicit-trigger judge — one yes/no+ question per live agent in the thread; the domain routes to the single+ agent at or above the threshold.## Scope@@ -32,7 +33,7 @@ session that results.## Boundaries- Depends on: `agent_session`, `ai_billing` (admission, `admission`- feature), `messages`, `bots`.+ feature), `messages`, `bots`, `jev` (`outbound`).- Used by: `agent_trigger_service` (its primary host) and `agent_harness`(routes triggers it receives via `agent_harness`'s `trigger_router`).@@ -40,7 +41,7 @@ session that results.- Add a trigger source: `src/domain/sources.rs` → `src/domain/service.rs`(admission + dispatch) → `src/domain/context.rs` if it needs new context.-- Change implicit-reply detection: `src/outbound/fast_model_judge.rs` →+- Change implicit-reply detection: `src/outbound/jev_judge.rs` →`src/outbound/lexical_explicit_reply_extractor.rs`.## Checks@@ -49,6 +50,10 @@ session that results.## Gotchas+- Implicit replies need `TYPESAFE_API_KEY` in the host (`agent_trigger_service`,+ `agent_harness_service`); while it is unset only mentions and explicit+ replies reach agents. Image captions still use the fast vision model,+ since Jev reads text only.- A new trigger variant must also be taught to `crates/webhook` ingestionand the SSE fan-out, or it will be silently dropped end to end.- Attributed bot messages (`MessagePostedMetadata::invoking_user`) triggerdiff --git a/crates/agent_trigger/Cargo.toml b/crates/agent_trigger/Cargo.tomlindex e587db3fe9..6b845b6370 100644--- a/crates/agent_trigger/Cargo.toml+++ b/crates/agent_trigger/Cargo.toml@@ -7,11 +7,11 @@ version = "0.1.0"[features]# Keep billing infrastructure out of domain-event-only consumers.admission = ["dep:ai_billing"]-# Lexical / fast-model adapters. Off by default: `lexical_client` pulls+# Lexical / Jev / fast-model adapters. Off by default: `lexical_client` pulls# models_search → models_soup → notification → rdkafka, and the self-hosted# daemon only needs domain events from this crate. Services that construct# the adapters enable this; do not add it to coding_agent_worker.-outbound = ["admission", "dep:agent", "dep:ai_usage", "dep:call", "dep:document_storage_service_client", "dep:document_sub_type", "dep:lexical_client", "dep:sqlx", "dep:static_file", "channels/ports"]+outbound = ["admission", "dep:agent", "dep:ai_usage", "dep:call", "dep:document_storage_service_client", "dep:document_sub_type", "dep:jev", "dep:lexical_client", "dep:sqlx", "dep:static_file", "channels/ports"][dependencies]ai_billing = { path = "../ai_billing", optional = true }@@ -22,6 +22,7 @@ anyhow = { workspace = true }bot_id = { path = "../bot_id" }document_storage_service_client = { path = "../document_storage_service_client", optional = true }document_sub_type = { path = "../document_sub_type", optional = true }+jev = { path = "../jev", optional = true }entity_access = { path = "../entity_access", default-features = false, features = ["ports"] }bots = { path = "../bots", default-features = false, features = ["ports"] }lexical_client = { path = "../lexical_client", optional = true }diff --git a/crates/agent_trigger/src/domain/service.rs b/crates/agent_trigger/src/domain/service.rsindex bcfc0af099..58f3908036 100644--- a/crates/agent_trigger/src/domain/service.rs+++ b/crates/agent_trigger/src/domain/service.rs@@ -38,9 +38,13 @@ const WINDOW_RADIUS: usize = 4;/// Ceiling on transcript length, keeping the most recent messages. A judgement/// about the newest message rarely turns on what happened dozens of messages-/// ago, and the fast model reads a bounded prompt.+/// ago, and the judge reads a bounded input.const TRANSCRIPT_CAP: usize = 40;+/// The judge's probability at or above which a message counts as addressed to+/// an agent.+const ADDRESSED_THRESHOLD: f32 = 0.5;+/// Bot facts required to decide whether a mention may start an agent session.#[cfg_attr(test, mockall::automock)]pub trait AgentBotLookup: Send + Sync + 'static {@@ -152,18 +156,39 @@ pub trait ThreadHistory: Send + Sync + 'static {) -> impl Future<Output = Result<Vec<ThreadMessage>>> + Send;}-/// Judges whether an unmentioned message in a session's thread is addressed-/// to the agent.+/// Judges which, if any, of the live agents in a thread an unmentioned+/// message is addressed to.#[cfg_attr(test, mockall::automock)]pub trait ImplicitTriggerJudge: Send + Sync + 'static {- /// Whether the message reads as directed at the session's agent, read- /// against `transcript` - the thread around the agent's participation in- /// it, empty when the thread could not be read.- fn is_addressed_to_agent(+ /// For each of the `agents` live in the thread, the probability from 0 to+ /// 1 that the message is directed at it, in order. `transcript` is the+ /// thread around the agents' participation, labelling them `agent 1` to+ /// `agent {agents}`; empty when the thread could not be read.+ fn addressee_probabilities(&self,posted: &MessagePostedMetadata,transcript: &str,- ) -> impl Future<Output = Result<bool>> + Send;+ agents: usize,+ ) -> impl Future<Output = Result<Vec<f32>>> + Send;+}++/// For hosts without a judge: no unmentioned message is routed by inference.+impl<J: ImplicitTriggerJudge> ImplicitTriggerJudge for Option<J> {+ async fn addressee_probabilities(+ &self,+ posted: &MessagePostedMetadata,+ transcript: &str,+ agents: usize,+ ) -> Result<Vec<f32>> {+ match self {+ Some(judge) => {+ judge+ .addressee_probabilities(posted, transcript, agents)+ .await+ }+ None => Ok(vec![0.0; agents]),+ }+ }}/// Looks up the session context for a message and evaluates its trigger rule.@@ -534,12 +559,10 @@ where/// Evaluates an unmentioned message against the sessions rooted at its/// thread: an explicit reply that targets a live session's bot, or that/// session's originating message, is forwarded outright. Anything else- /// only when the judge reads it as addressed to the agent.+ /// only when the judge reads it as addressed to exactly one live agent.///- /// An extracted reply names who and which message it answers, so it can- /// pick among several live agents. The inferred path still only fires when- /// exactly one agent is live; picking among several by recency would route- /// on nothing the author meant.+ /// When the judge reads the message as addressed to several agents, it+ /// goes to none: picking one would route on nothing the author meant.////// Extractor and judge failures are treated as "no" rather than propagated:/// implicit triggering is best-effort, and an outage must not wedge the@@ -579,20 +602,9 @@ where)));}- let session = match candidates.as_slice() {- [] => return Ok(None),- [session] => session.clone(),- sessions => {- log_no_event(- posted,- None,- NoEventReason::AmbiguousAgentSessions {- candidates: sessions.len(),- },- );- return Ok(None);- }- };+ if candidates.is_empty() {+ return Ok(None);+ }// The judge may caption attachments before classifying. Admit the whole// optional operation before entering that port, never retrying through a@@ -603,25 +615,37 @@ wherereturn Ok(None);}- if self- .is_addressed_to_agent(posted, &self.transcript(posted, invocation, &session).await)- .await- {- return Ok(Some(thread_event(- session,+ let transcript = self.transcript(posted, invocation, &candidates).await;+ let probabilities = self+ .addressee_probabilities(posted, &transcript, candidates.len())+ .await;+ match addressed_candidate(&probabilities) {+ Addressee::One(index) => Ok(Some(thread_event(+ candidates.swap_remove(index),ThreadMessageKind::Inferred,posted,- )));+ ))),+ Addressee::None => {+ log_no_event(+ posted,+ None,+ NoEventReason::NotAddressedToAgent {+ candidates: candidates.len(),+ },+ );+ Ok(None)+ }+ Addressee::Several => {+ log_no_event(+ posted,+ None,+ NoEventReason::AmbiguousAgentSessions {+ candidates: candidates.len(),+ },+ );+ Ok(None)+ }}-- log_no_event(- posted,- None,- NoEventReason::NotAddressedToAgent {- session_id: session.id,- },- );- Ok(None)}/// The live session the extracted reply-target names: either its bot, or@@ -649,9 +673,9 @@ wheresession_targeted_by_reply(candidates, &reply).cloned()}- /// The thread around the points where the agent took part: its own- /// messages, and the message being judged, each with [`WINDOW_RADIUS`]- /// messages of surrounding conversation.+ /// The thread around the points where the candidate agents took part:+ /// their own messages, and the message being judged, each with+ /// [`WINDOW_RADIUS`] messages of surrounding conversation.////// An unreadable thread yields an empty transcript rather than an error, so/// the judge still rules on the message itself instead of the whole path@@ -660,7 +684,7 @@ where&self,posted: &MessagePostedMetadata,invocation: &AuthorizedInvocation,- session: &AgentSession,+ candidates: &[AgentSession],) -> String {let messages = match self.history.thread_messages(invocation).await {Ok(messages) => messages,@@ -669,7 +693,7 @@ wherereturn String::new();}};- let agent = session.bot_id.into_storage_id();+ let agents: Vec<BotId> = candidates.iter().map(|session| session.bot_id).collect();let anchors: Vec<Uuid> = messages.iter().filter(|message| {@@ -677,29 +701,66 @@ where|| message.sender.as_bot()- .is_some_and(|bot| bot.as_ref() == agent.as_ref())+ .is_some_and(|bot| agents.contains(&bot.bot_id()))}).map(|message| message.id).collect();render_transcript(&thread_window(&messages, &anchors, WINDOW_RADIUS, TRANSCRIPT_CAP),- session.bot_id,+ &agents,)}- async fn is_addressed_to_agent(+ /// The judge's probabilities, one per candidate; a failure or a malformed+ /// answer reads as addressed to nobody.+ async fn addressee_probabilities(&self,posted: &MessagePostedMetadata,transcript: &str,- ) -> bool {- self.judge- .is_addressed_to_agent(posted, transcript)+ agents: usize,+ ) -> Vec<f32> {+ match self+ .judge+ .addressee_probabilities(posted, transcript, agents).await- .inspect_err(|error| {+ {+ Ok(probabilities) if probabilities.len() == agents => probabilities,+ Ok(probabilities) => {+ tracing::warn!(+ expected = agents,+ answered = probabilities.len(),+ "implicit trigger judge answered for the wrong number of agents; treating as not addressed"+ );+ Vec::new()+ }+ Err(error) => {tracing::warn!(error = ?error, "implicit trigger judge failed; treating as not addressed");- })- .unwrap_or(false)+ Vec::new()+ }+ }+ }+}++/// Which candidates the judge read a message as addressed to.+#[derive(Debug, PartialEq, Eq)]+enum Addressee {+ None,+ One(usize),+ Several,+}++/// The single candidate whose probability reaches [`ADDRESSED_THRESHOLD`].+fn addressed_candidate(probabilities: &[f32]) -> Addressee {+ let mut addressed = probabilities+ .iter()+ .enumerate()+ .filter(|(_, probability)| **probability >= ADDRESSED_THRESHOLD)+ .map(|(index, _)| index);+ match (addressed.next(), addressed.next()) {+ (None, _) => Addressee::None,+ (Some(index), None) => Addressee::One(index),+ (Some(_), Some(_)) => Addressee::Several,}}diff --git a/crates/agent_trigger/src/domain/service/test.rs b/crates/agent_trigger/src/domain/service/test.rsindex 4d8569f382..dac38363df 100644--- a/crates/agent_trigger/src/domain/service/test.rs+++ b/crates/agent_trigger/src/domain/service/test.rs@@ -674,12 +674,12 @@ fn reply_to_user_message(target_message_id: Uuid) -> ExtractedExplicitReply {}}-fn judge_saying(result: Result<bool>) -> MockImplicitTriggerJudge {+fn judge_saying(result: Result<Vec<f32>>) -> MockImplicitTriggerJudge {let mut judge = MockImplicitTriggerJudge::new();judge- .expect_is_addressed_to_agent()+ .expect_addressee_probabilities().once()- .return_once(move |_, _| Box::pin(async move { result }));+ .return_once(move |_, _, _| Box::pin(async move { result }));judge}@@ -687,11 +687,11 @@ fn judge_saying(result: Result<bool>) -> MockImplicitTriggerJudge {fn judge_expecting(transcript: &'static str) -> MockImplicitTriggerJudge {let mut judge = MockImplicitTriggerJudge::new();judge- .expect_is_addressed_to_agent()+ .expect_addressee_probabilities().once()- .return_once(move |_, given| {+ .return_once(move |_, given, _| {assert_eq!(given, transcript);- Box::pin(async { Ok(true) })+ Box::pin(async { Ok(vec![0.9]) })});judge}@@ -731,7 +731,7 @@ async fn a_message_the_judge_reads_as_addressed_triggers_as_inferred() {sessions,agent_bots(),extractor(Ok(None)),- judge_saying(Ok(true)),+ judge_saying(Ok(vec![0.9])),);let events = service.evaluate(&posted).await.expect("evaluate message");@@ -747,7 +747,7 @@ async fn a_message_addressed_to_nobody_yields_nothing() {sessions,agent_bots(),extractor(Ok(None)),- judge_saying(Ok(false)),+ judge_saying(Ok(vec![0.1])),);assert!(@@ -825,18 +825,38 @@ async fn implicit_triggering_skips_sessions_of_agentless_bots() {assert_eq!(metadata.bot_id, BotId::TEST_B);}-#[tokio::test]-async fn two_live_agents_in_a_thread_yield_nothing() {- let posted = message(vec![]);- let sessions = implicit_sessions(vec![+fn two_live_sessions() -> MockAgentSessionRepo {+ implicit_sessions(vec![thread_session(AgentSessionId::TEST_A, BotId::TEST_A),thread_session(AgentSessionId::TEST_B, BotId::TEST_B),- ]);+ ])+}++#[tokio::test]+async fn the_judge_picks_which_of_two_live_agents_a_message_is_for() {+ let posted = message(vec![]);let service = service(- sessions,+ two_live_sessions(),agent_bots(),extractor(Ok(None)),- MockImplicitTriggerJudge::new(),+ judge_saying(Ok(vec![0.2, 0.8])),+ );++ let events = service.evaluate(&posted).await.expect("evaluate message");+ let metadata = existing_channel_metadata(&events);+ assert_eq!(metadata.session_id, AgentSessionId::TEST_B);+ assert_eq!(metadata.bot_id, BotId::TEST_B);+ assert_eq!(metadata.kind, ThreadMessageKind::Inferred);+}++#[tokio::test]+async fn a_message_the_judge_reads_as_for_both_live_agents_yields_nothing() {+ let posted = message(vec![]);+ let service = service(+ two_live_sessions(),+ agent_bots(),+ extractor(Ok(None)),+ judge_saying(Ok(vec![0.7, 0.8])),);assert!(@@ -848,6 +868,62 @@ async fn two_live_agents_in_a_thread_yield_nothing() {);}+#[tokio::test]+async fn a_judge_answering_for_the_wrong_number_of_agents_yields_nothing() {+ let posted = message(vec![]);+ let service = service(+ two_live_sessions(),+ agent_bots(),+ extractor(Ok(None)),+ judge_saying(Ok(vec![0.9])),+ );++ assert!(+ service+ .evaluate(&posted)+ .await+ .expect("evaluate message")+ .is_empty()+ );+}++#[tokio::test]+async fn the_judge_reads_every_live_agent_numbered_in_the_thread() {+ let posted = message(vec![]);+ let history = thread_of(vec![+ thread_message(0, ChannelSender::new_from_bot(BotId::TEST_A), "on it"),+ thread_message(1, ChannelSender::new_from_bot(BotId::TEST_B), "me too"),+ thread_message(2, ChannelSender::new_from_user(user()), "hello"),+ ]);+ let mut judge = MockImplicitTriggerJudge::new();+ judge+ .expect_addressee_probabilities()+ .once()+ .return_once(|_, transcript, agents| {+ assert_eq!(agents, 2);+ assert_eq!(+ transcript,+ "[agent 1] on it\n\+ [agent 2] me too\n\+ [user macro|[email protected]] hello\n"+ );+ Box::pin(async { Ok(vec![0.9, 0.1]) })+ });+ let service = service_reading(+ two_live_sessions(),+ agent_bots(),+ extractor(Ok(None)),+ judge,+ history,+ );++ let events = service.evaluate(&posted).await.expect("evaluate message");+ assert_eq!(+ existing_channel_metadata(&events).session_id,+ AgentSessionId::TEST_A+ );+}+#[tokio::test]async fn a_failing_extractor_falls_through_to_the_judge() {let posted = message(vec![]);@@ -858,7 +934,7 @@ async fn a_failing_extractor_falls_through_to_the_judge() {extractor(Err(AgentSessionError::Unknown(anyhow::anyhow!("lexical service unavailable")))),- judge_saying(Ok(true)),+ judge_saying(Ok(vec![0.9])),);let events = service.evaluate(&posted).await.expect("evaluate message");@@ -907,7 +983,7 @@ async fn the_judge_reads_the_thread_around_the_agent() {extractor(Ok(None)),judge_expecting("[user macro|[email protected]] unrelated chatter\n\- [agent] on it\n\+ [agent 1] on it\n\[user macro|[email protected]] hello\n",),history,@@ -1000,7 +1076,7 @@ async fn an_explicit_reply_to_another_user_falls_through_to_the_judge() {sessions,agent_bots(),extractor(Ok(Some(reply_to_user()))),- judge_saying(Ok(true)),+ judge_saying(Ok(vec![0.9])),);let events = service.evaluate(&posted).await.expect("evaluate message");@@ -1032,7 +1108,7 @@ async fn an_explicit_reply_to_one_of_two_live_agents_routes_to_that_agent() {}#[tokio::test]-async fn an_explicit_reply_to_neither_of_two_live_agents_yields_nothing() {+async fn an_explicit_reply_to_neither_of_two_live_agents_is_left_to_the_judge() {let posted = message(vec![]);let sessions = implicit_sessions(vec![thread_session(AgentSessionId::TEST_A, BotId::TEST_A),@@ -1042,7 +1118,7 @@ async fn an_explicit_reply_to_neither_of_two_live_agents_yields_nothing() {sessions,agent_bots(),extractor(Ok(Some(reply_to_user()))),- MockImplicitTriggerJudge::new(),+ judge_saying(Ok(vec![0.1, 0.1])),);assert!(@@ -1055,7 +1131,7 @@ async fn an_explicit_reply_to_neither_of_two_live_agents_yields_nothing() {}#[tokio::test]-async fn an_explicit_reply_to_a_shared_originating_message_yields_nothing() {+async fn an_explicit_reply_to_a_shared_originating_message_is_left_to_the_judge() {let posted = message(vec![]);let sessions = implicit_sessions(vec![thread_session(AgentSessionId::TEST_A, BotId::TEST_A),@@ -1065,7 +1141,7 @@ async fn an_explicit_reply_to_a_shared_originating_message_yields_nothing() {sessions,agent_bots(),extractor(Ok(Some(reply_to_user_message(Uuid::from_u128(3))))),- MockImplicitTriggerJudge::new(),+ judge_saying(Ok(vec![0.1, 0.1])),);assert!(@@ -1499,7 +1575,7 @@ async fn an_agents_unmentioned_follow_up_reaches_another_agents_session() {for (replies, judge, kind) in [(extractor(Ok(None)),- judge_saying(Ok(true)),+ judge_saying(Ok(vec![0.9])),ThreadMessageKind::Inferred,),(diff --git a/crates/agent_trigger/src/domain/service/test/admission.rs b/crates/agent_trigger/src/domain/service/test/admission.rsindex e58420c029..abf312c2e7 100644--- a/crates/agent_trigger/src/domain/service/test/admission.rs+++ b/crates/agent_trigger/src/domain/service/test/admission.rs@@ -112,7 +112,7 @@ async fn explicit_mentions_and_reply_targets_route_without_admission() {#[tokio::test]async fn admitted_inference_uses_the_sender_and_does_not_retry_a_failed_judge() {for outcome in [- Ok(true),+ Ok(vec![0.9]),Err(AgentSessionError::Unknown(anyhow::anyhow!("provider failed"))),diff --git a/crates/agent_trigger/src/domain/thread_window.rs b/crates/agent_trigger/src/domain/thread_window.rsindex 77a7dec0a5..0c08c97765 100644--- a/crates/agent_trigger/src/domain/thread_window.rs+++ b/crates/agent_trigger/src/domain/thread_window.rs@@ -87,15 +87,15 @@ pub fn thread_window<'a>(entries}-/// Renders a transcript for a judge reading the thread, labelling `agent`'s own-/// messages so it can tell which participant it is deciding about.+/// Renders a transcript for a judge reading the thread, labelling each of+/// `agents`' own messages `agent N` (1-based, in `agents` order) so it can tell+/// which participants it is deciding about.///-/// Other bots keep their raw ids: a second bot in the thread is a participant-/// like any other, and mislabelling it as the agent would invite the judge to-/// answer about the wrong one.+/// Other bots keep their raw ids: a bot that is not a candidate is a+/// participant like any other, and labelling it as an agent would invite the+/// judge to route to the wrong one.#[must_use]-pub fn render_transcript(entries: &[TranscriptEntry<'_>], agent: BotId) -> String {- let agent = agent.into_storage_id();+pub fn render_transcript(entries: &[TranscriptEntry<'_>], agents: &[BotId]) -> String {let mut rendered = String::new();for entry in entries {match entry {@@ -105,8 +105,12 @@ pub fn render_transcript(entries: &[TranscriptEntry<'_>], agent: BotId) -> Strin}TranscriptEntry::Message(message) => {let speaker = match (message.sender.as_bot(), message.sender.as_user()) {- (Some(bot), _) if bot.as_ref() == agent.as_ref() => "agent".to_owned(),- (Some(bot), _) => format!("bot {bot}"),+ (Some(bot), _) => {+ match agents.iter().position(|agent| *agent == bot.bot_id()) {+ Some(index) => format!("agent {}", index + 1),+ None => format!("bot {bot}"),+ }+ }(None, Some(user)) => format!("user {user}"),// A sender is one or the other; a future third kind should// still appear in the transcript rather than vanish.diff --git a/crates/agent_trigger/src/domain/thread_window/test.rs b/crates/agent_trigger/src/domain/thread_window/test.rsindex c359750b42..44b75183d1 100644--- a/crates/agent_trigger/src/domain/thread_window/test.rs+++ b/crates/agent_trigger/src/domain/thread_window/test.rs@@ -136,27 +136,44 @@ fn a_rendered_transcript_marks_the_agents_own_messages() {let rendered = render_transcript(&thread_window(&messages, &anchors(&[1]), 2, 60),- BotId::TEST_A,+ &[BotId::TEST_A],);assert_eq!(rendered,format!("[user macro|[email protected]] message 0\n\- [agent] message 1\n\+ [agent 1] message 1\n\[bot {}] message 2\n",BotId::TEST_B.into_storage_id()));}+#[test]+fn a_rendered_transcript_numbers_each_candidate_agent() {+ let messages = bot_thread();++ let rendered = render_transcript(+ &thread_window(&messages, &anchors(&[1]), 2, 60),+ &[BotId::TEST_B, BotId::TEST_A],+ );++ assert_eq!(+ rendered,+ "[user macro|[email protected]] message 0\n\+ [agent 2] message 1\n\+ [agent 1] message 2\n"+ );+}+#[test]fn a_rendered_transcript_says_where_messages_were_dropped() {let messages = thread(20);let rendered = render_transcript(&thread_window(&messages, &anchors(&[15]), 1, 60),- BotId::TEST_A,+ &[BotId::TEST_A],);assert!(@@ -171,7 +188,7 @@ fn a_single_dropped_message_reads_as_one() {let rendered = render_transcript(&thread_window(&messages, &anchors(&[1]), 0, 60),- BotId::TEST_A,+ &[BotId::TEST_A],);assert!(diff --git a/crates/agent_trigger/src/domain/yield_event.rs b/crates/agent_trigger/src/domain/yield_event.rsindex cde9c62a71..3d26c2f78f 100644--- a/crates/agent_trigger/src/domain/yield_event.rs+++ b/crates/agent_trigger/src/domain/yield_event.rs@@ -36,14 +36,15 @@ pub enum NoEventReason {/// Session already evaluated for this message.session_id: AgentSessionId,},- /// The message sat in a session's thread without a mention, and neither- /// an explicit reply to the agent nor the model judged it addressed.+ /// The message sat in a thread with live agent sessions without a+ /// mention, and neither an explicit reply nor the judge addressed it to+ /// any of their agents.NotAddressedToAgent {- /// Session whose thread carried the message.- session_id: AgentSessionId,+ /// How many agent-backed sessions the thread carried.+ candidates: usize,},- /// Several agents are live in the thread and nothing said which one the- /// message was for.+ /// Several agents are live in the thread and the judge read the message+ /// as addressed to more than one of them.AmbiguousAgentSessions {/// How many agent-backed sessions the thread carried.candidates: usize,diff --git a/crates/agent_trigger/src/outbound/fast_model_judge.rs b/crates/agent_trigger/src/outbound/fast_model_judge.rsdeleted file mode 100644index cf0e47f8db..0000000000--- a/crates/agent_trigger/src/outbound/fast_model_judge.rs+++ /dev/null@@ -1,135 +0,0 @@-//! Implicit-trigger judgement backed by the fast agent model.--#[cfg(test)]-mod test;--use std::sync::Arc;--use agent::structured_output::{DynamicSchema, dynamic_structured_completion};-use agent::{Message, PredefinedModel};-use agent_session::domain::error::Result;-use ai_usage::{AiFeature, UsageContext, UsageRecorder};-use messages::domain::events::MessagePostedMetadata;-use serde::Deserialize;-use serde_json::json;--use crate::domain::service::ImplicitTriggerJudge;--use super::image_caption::{ImageCaptioner, append_image_blurbs, blurbs_for_attachments};--static SYSTEM_PROMPT: &str = "\-You decide whether a channel message is addressed to an AI coding agent.--The message was posted, without mentioning anyone, in a thread where an AI \-coding agent has an open session: the agent was asked to do work earlier in \-the thread and posts its progress there. Decide whether this new message is \-directed at that agent - a follow-up instruction, question, correction, or \-feedback the agent should act on - or is conversation between the people in \-the thread.--Return true only when the message reads as something its author expects the \-agent to respond to. Return false when it is commentary about the agent or \-its work addressed to other people, or unrelated discussion.--You are given the thread around the agent's part in it, as lines of \-'[speaker] message' where the agent's own messages are marked '[agent]'. Some \-messages may be hidden; judge on what you are shown.--Attached images are not shown to you. Each one is written into the message \-as <this is an image of ...>, a description of the picture rather than words \-the author typed. Treat that description as what the image shows. \-<this is an image> means a picture was attached but could not be described. \-Those descriptions appear on the message to judge; the thread transcript \-does not repeat them.";--#[derive(Debug, Deserialize)]-struct JudgeOutput {- addressed_to_agent: bool,- #[expect(dead_code, reason = "the model reasons better when asked to explain")]- reason: String,-}--/// Judges implicit triggers with [`PredefinedModel::Fast`], recording token-/// usage against the user the message invokes for.-pub struct FastModelTriggerJudge {- model: PredefinedModel,- recorder: Arc<dyn UsageRecorder>,- images: Arc<dyn ImageCaptioner>,-}--impl FastModelTriggerJudge {- /// Creates a judge using the fast agent model.- ///- /// `images` turns attached pictures into the `<this is an image of ...>`- /// blurbs the text-only model reads. The forwarded message is unchanged.- pub fn new(recorder: Arc<dyn UsageRecorder>, images: impl ImageCaptioner + 'static) -> Self {- Self {- model: PredefinedModel::Fast,- recorder,- images: Arc::new(images),- }- }-}--impl ImplicitTriggerJudge for FastModelTriggerJudge {- async fn is_addressed_to_agent(- &self,- posted: &MessagePostedMetadata,- transcript: &str,- ) -> Result<bool> {- let Some(user) = posted.invoking_user() else {- return Ok(false);- };- let schema = DynamicSchema {- name: "ImplicitTriggerJudgeOutput".to_string(),- description: Some(- "Judgement for whether a thread message is addressed to the agent.".to_string(),- ),- schema: json!({- "type": "object",- "additionalProperties": false,- "required": ["addressed_to_agent", "reason"],- "properties": {- "addressed_to_agent": {- "type": "boolean",- "description": "True only if the message is directed at the agent."- },- "reason": {- "type": "string",- "description": "A concise reason for the judgement."- }- }- }),- };- let ctx = UsageContext::new(AiFeature::Automation, user);-- let blurbs =- blurbs_for_attachments(self.images.as_ref(), &posted.attachments, ctx.clone()).await;- let content = append_image_blurbs(&posted.content, &blurbs);- let prompt = judge_user_prompt(transcript, &content);-- let value = dynamic_structured_completion(- self.model,- SYSTEM_PROMPT,- vec![Message::user(prompt)],- schema,- self.recorder.as_ref(),- ctx,- )- .await?;-- let output: JudgeOutput = serde_json::from_value(value).map_err(|error| {- anyhow::anyhow!("implicit trigger judge returned malformed output: {error}")- })?;- Ok(output.addressed_to_agent)- }-}--/// The user prompt the judge scores. Image blurbs are already part of `content`.-pub(crate) fn judge_user_prompt(transcript: &str, content: &str) -> String {- if transcript.is_empty() {- format!("The message to judge:\n{content}")- } else {- format!("The thread so far:\n{transcript}\nThe message to judge:\n{content}")- }-}diff --git a/crates/agent_trigger/src/outbound/fast_model_judge/test.rs b/crates/agent_trigger/src/outbound/fast_model_judge/test.rsdeleted file mode 100644index 3eb912ee8f..0000000000--- a/crates/agent_trigger/src/outbound/fast_model_judge/test.rs+++ /dev/null@@ -1,57 +0,0 @@-use super::*;--struct UnexpectedCaptioner;--#[async_trait::async_trait]-impl ImageCaptioner for UnexpectedCaptioner {- async fn caption(&self, _file_id: &str, _ctx: UsageContext) -> Option<String> {- panic!("a missing real caller must never reach image captioning");- }-}--#[tokio::test]-async fn a_non_user_sender_is_not_replaced_with_system_billing_identity() {- use chrono::Utc;- use macro_uuid::Uuid;- use messages::domain::{events::MessageEventAttachment, models::MessageParent};-- let judge =- FastModelTriggerJudge::new(Arc::new(ai_usage::NoOpUsageRecorder), UnexpectedCaptioner);- let posted = MessagePostedMetadata {- parent: MessageParent::Channel(Uuid::from_u128(1)),- message_id: Uuid::from_u128(2),- thread_id: Some(Uuid::from_u128(3)),- root_id: Uuid::from_u128(3),- sender: channel_sender::ChannelSender::new_from_bot(bot_id::MACRO_AI_BOT_ID),- triggered_by: None,- content: "describe this".to_owned(),- mentions: vec![],- attachments: vec![MessageEventAttachment {- attachment_id: Uuid::from_u128(4),- entity_type: "static_image".to_owned(),- entity_id: "image-to-caption".to_owned(),- created_at: Utc::now(),- }],- created_at: Utc::now(),- };- assert!(!judge.is_addressed_to_agent(&posted, "").await.unwrap());-}--#[test]-fn the_judge_is_told_what_an_image_blurb_means() {- assert!(SYSTEM_PROMPT.contains("<this is an image of ...>"));- assert!(SYSTEM_PROMPT.contains("<this is an image>"));-}--#[test]-fn an_image_blurb_is_part_of_the_message_being_judged() {- let content = "see this\n<this is an image of a frog>";- assert_eq!(- judge_user_prompt("", content),- "The message to judge:\nsee this\n<this is an image of a frog>"- );- assert_eq!(- judge_user_prompt("[agent] on it\n", "see this"),- "The thread so far:\n[agent] on it\n\nThe message to judge:\nsee this"- );-}diff --git a/crates/agent_trigger/src/outbound/jev_judge.rs b/crates/agent_trigger/src/outbound/jev_judge.rsnew file mode 100644index 0000000000..3083aaae12--- /dev/null+++ b/crates/agent_trigger/src/outbound/jev_judge.rs@@ -0,0 +1,101 @@+//! Implicit-trigger judgement backed by the Jev yes/no classifier.++#[cfg(test)]+mod test;++use std::sync::Arc;++use agent_session::domain::error::Result;+use ai_usage::{AiFeature, UsageContext};+use jev::domain::{YesNoClassifier, YesNoQuestion};+use messages::domain::events::MessagePostedMetadata;+use serde_json::{Value, json};++use crate::domain::service::ImplicitTriggerJudge;++use super::image_caption::{ImageCaptioner, append_image_blurbs, blurbs_for_attachments};++/// What the classifier is looking at, sent alongside the thread and message.+const SITUATION: &str = "\+A message was posted, without mentioning anyone, in a channel thread where AI \+coding agents have open sessions: each was asked to do work earlier in the \+thread and posts its progress there. `thread` is the conversation around the \+agents' part in it, as lines of '[speaker] message'; each agent's own messages \+are marked '[agent N]'. Some messages may be hidden. `message` is the new \+message. Attached images are not shown: each is written into the message as \+<this is an image of ...>, a description of the picture rather than words the \+author typed, or <this is an image> when it could not be described.";++/// Judges implicit triggers with Jev, one yes/no question per live agent,+/// metering usage against the user the message invokes for.+pub struct JevTriggerJudge<C> {+ classifier: Arc<C>,+ images: Arc<dyn ImageCaptioner>,+}++impl<C: YesNoClassifier> JevTriggerJudge<C> {+ /// Creates a judge asking `classifier`.+ ///+ /// `images` turns attached pictures into the `<this is an image of ...>`+ /// blurbs the text-only classifier reads. The forwarded message is+ /// unchanged.+ pub fn new(classifier: Arc<C>, images: impl ImageCaptioner + 'static) -> Self {+ Self {+ classifier,+ images: Arc::new(images),+ }+ }+}++impl<C: YesNoClassifier> ImplicitTriggerJudge for JevTriggerJudge<C> {+ async fn addressee_probabilities(+ &self,+ posted: &MessagePostedMetadata,+ transcript: &str,+ agents: usize,+ ) -> Result<Vec<f32>> {+ let Some(user) = posted.invoking_user() else {+ return Ok(vec![0.0; agents]);+ };+ let ctx = UsageContext::new(AiFeature::Automation, user);++ let blurbs =+ blurbs_for_attachments(self.images.as_ref(), &posted.attachments, ctx.clone()).await;+ let content = append_image_blurbs(&posted.content, &blurbs);+ let questions = addressee_questions(agents)?;++ let probabilities = self+ .classifier+ .classify(ctx, &judge_input(transcript, &content), &questions)+ .await+ .map_err(|error| anyhow::anyhow!("implicit trigger judge failed: {error}"))?;+ Ok(probabilities+ .into_iter()+ .map(|probability| probability.get())+ .collect())+ }+}++/// The JSON the classifier reads. Image blurbs are already part of `content`.+pub(crate) fn judge_input(transcript: &str, content: &str) -> Value {+ json!({+ "situation": SITUATION,+ "thread": transcript,+ "message": content,+ })+}++/// One question per agent, asking whether `message` is directed at it.+pub(crate) fn addressee_questions(agents: usize) -> Result<Vec<YesNoQuestion>> {+ (1..=agents)+ .map(|agent| {+ YesNoQuestion::try_from(format!(+ "Is `message` directed at agent {agent}: a follow-up instruction, question, \+ correction, or feedback its author expects agent {agent} to respond to, \+ rather than conversation between the people in the thread, commentary \+ about an agent addressed to others, or unrelated discussion?"+ ))+ .map_err(|error| anyhow::anyhow!("invalid addressee question: {error}").into())+ })+ .collect()+}diff --git a/crates/agent_trigger/src/outbound/jev_judge/test.rs b/crates/agent_trigger/src/outbound/jev_judge/test.rsnew file mode 100644index 0000000000..475c6cbfd0--- /dev/null+++ b/crates/agent_trigger/src/outbound/jev_judge/test.rs@@ -0,0 +1,153 @@+use std::sync::Mutex;++use chrono::Utc;+use jev::domain::{JevError, Probability};+use macro_uuid::Uuid;+use messages::domain::{events::MessageEventAttachment, models::MessageParent};++use super::*;++struct UnexpectedCaptioner;++#[async_trait::async_trait]+impl ImageCaptioner for UnexpectedCaptioner {+ async fn caption(&self, _file_id: &str, _ctx: UsageContext) -> Option<String> {+ panic!("a missing real caller must never reach image captioning");+ }+}++/// Answers with fixed probabilities and remembers what it was asked.+struct FakeClassifier {+ answer: std::result::Result<Vec<f32>, JevError>,+ asked: Mutex<Option<(Value, usize)>>,+}++impl FakeClassifier {+ fn answering(answer: std::result::Result<Vec<f32>, JevError>) -> Arc<Self> {+ Arc::new(Self {+ answer,+ asked: Mutex::new(None),+ })+ }+}++impl YesNoClassifier for FakeClassifier {+ async fn classify(+ &self,+ _usage: UsageContext,+ input: &Value,+ questions: &[YesNoQuestion],+ ) -> std::result::Result<Vec<Probability>, JevError> {+ *self.asked.lock().expect("fake lock") = Some((input.clone(), questions.len()));+ self.answer.clone().map(|answer| {+ answer+ .into_iter()+ .map(|value| Probability::new(value).expect("valid probability"))+ .collect()+ })+ }+}++fn posted(sender: channel_sender::ChannelSender<'static>) -> MessagePostedMetadata {+ MessagePostedMetadata {+ parent: MessageParent::Channel(Uuid::from_u128(1)),+ message_id: Uuid::from_u128(2),+ thread_id: Some(Uuid::from_u128(3)),+ root_id: Uuid::from_u128(3),+ sender,+ triggered_by: None,+ content: "can you also fix the tests".to_owned(),+ mentions: vec![],+ attachments: vec![],+ created_at: Utc::now(),+ }+}++fn user_sender() -> channel_sender::ChannelSender<'static> {+ channel_sender::ChannelSender::new_from_user(+ macro_user_id::user_id::MacroUserIdStr::try_from_email("[email protected]")+ .expect("valid user id"),+ )+}++#[tokio::test]+async fn a_non_user_sender_is_not_replaced_with_system_billing_identity() {+ let classifier = FakeClassifier::answering(Ok(vec![1.0]));+ let judge = JevTriggerJudge::new(classifier.clone(), UnexpectedCaptioner);+ let mut posted = posted(channel_sender::ChannelSender::new_from_bot(+ bot_id::MACRO_AI_BOT_ID,+ ));+ posted.attachments = vec![MessageEventAttachment {+ attachment_id: Uuid::from_u128(4),+ entity_type: "static_image".to_owned(),+ entity_id: "image-to-caption".to_owned(),+ created_at: Utc::now(),+ }];++ assert_eq!(+ judge+ .addressee_probabilities(&posted, "", 1)+ .await+ .expect("judge"),+ vec![0.0]+ );+ assert!(classifier.asked.lock().expect("fake lock").is_none());+}++#[tokio::test]+async fn asks_one_question_per_agent_about_the_thread_and_message() {+ let classifier = FakeClassifier::answering(Ok(vec![0.1, 0.9]));+ let judge = JevTriggerJudge::new(classifier.clone(), UnexpectedCaptioner);++ let probabilities = judge+ .addressee_probabilities(&posted(user_sender()), "[agent 2] on it\n", 2)+ .await+ .expect("judge");++ assert_eq!(probabilities, vec![0.1, 0.9]);+ let (input, questions) = classifier+ .asked+ .lock()+ .expect("fake lock")+ .clone()+ .expect("classifier was asked");+ assert_eq!(questions, 2);+ assert_eq!(input["thread"], "[agent 2] on it\n");+ assert_eq!(input["message"], "can you also fix the tests");+}++#[tokio::test]+async fn a_classifier_failure_is_an_error() {+ let classifier = FakeClassifier::answering(Err(JevError::Unavailable));+ let judge = JevTriggerJudge::new(classifier, UnexpectedCaptioner);++ assert!(+ judge+ .addressee_probabilities(&posted(user_sender()), "", 1)+ .await+ .is_err()+ );+}++#[test]+fn every_addressee_question_names_its_agent_and_fits_the_classifier() {+ let questions = addressee_questions(jev::domain::MAX_QUESTIONS).expect("valid questions");++ assert_eq!(questions.len(), jev::domain::MAX_QUESTIONS);+ assert!(questions[0].as_str().contains("agent 1"));+ assert!(questions[31].as_str().contains("agent 32"));+}++#[test]+fn the_judge_is_told_what_an_image_blurb_means() {+ assert!(SITUATION.contains("<this is an image of ...>"));+ assert!(SITUATION.contains("<this is an image>"));+}++#[test]+fn an_image_blurb_is_part_of_the_message_being_judged() {+ let input = judge_input("", "see this\n<this is an image of a frog>");++ assert_eq!(input["message"], "see this\n<this is an image of a frog>");+ assert_eq!(input["thread"], "");+}diff --git a/crates/agent_trigger/src/outbound/mod.rs b/crates/agent_trigger/src/outbound/mod.rsindex 06376e2ef7..ac22357594 100644--- a/crates/agent_trigger/src/outbound/mod.rs+++ b/crates/agent_trigger/src/outbound/mod.rs@@ -4,8 +4,8 @@ mod bot_repo_agent_lookup;mod channel_type_lookup;mod discussion_context;mod dss_task_assignment_context;-mod fast_model_judge;mod image_caption;+mod jev_judge;mod lexical_explicit_reply_extractor;mod message_thread_history;mod people_directory;@@ -14,8 +14,8 @@ pub use bot_repo_agent_lookup::BotRepoAgentLookup;pub use channel_type_lookup::ChannelRepoTypeLookup;pub use discussion_context::{MessageDiscussionReader, ParentNameReader};pub use dss_task_assignment_context::DssTaskAssignmentContext;-pub use fast_model_judge::FastModelTriggerJudge;pub use image_caption::{ImageCaptioner, VisionImageCaptioner};+pub use jev_judge::JevTriggerJudge;pub use lexical_explicit_reply_extractor::LexicalExplicitReplyExtractor;pub use message_thread_history::MessageThreadHistory;pub use people_directory::PgPeopleDirectory;diff --git a/crates/jev/AGENTS.md b/crates/jev/AGENTS.mdindex 5b3441bd8c..e187f9b7b8 100644--- a/crates/jev/AGENTS.md+++ b/crates/jev/AGENTS.md@@ -33,7 +33,8 @@ composer's Auto mode.- Used by: `scheduled_action` (trigger conditions),`document_cognition_service` (the `/decisions` endpoint), `agent_harness`(Auto mode choice, Clef Flash), `agent_session` (whether to retitle a- session after a turn, TypeSafe).+ session after a turn, TypeSafe), `agent_trigger` (routing unmentioned+ thread replies to agent sessions).## Checksdiff --git a/docs/runbooks/ai-billing-rollout.md b/docs/runbooks/ai-billing-rollout.mdindex 4b03e0af7a..7d6e758140 100644--- a/docs/runbooks/ai-billing-rollout.md+++ b/docs/runbooks/ai-billing-rollout.md@@ -99,7 +99,7 @@ regressions, not live-provider or deployment verification.| In-memory harness sessions | `services/agent_harness_service/src/main.rs` settling recorder passed into the common context → `RigTurnEngine`; trusted `TurnRequest.owner`, AgentSession feature | Captured scope carried into spawned turn; AgentLoop binds immutable owner scope; Rig attempts → journal, final analytics retained | `agent` spawned scope/cancellation tests; `agent_inmem` package tests. Non-user owners remain rejected by existing behavior || Harness session naming | The harness's settling recorder supplied to `HaikuAgentSessionNameGenerator` in harness `main.rs`; session owner, ChatRename feature/entity | One-shot model attempts → journal (feature classification unchanged) | Existing name-generator/host tests plus shared completion tests; not covered merely by common-context injection || Harness repository selection | The harness's settling recorder passed to `CursorContainerManager` in harness `main.rs`; repository chooser's trusted owner, AgentRepositoryChoice feature | Rust selection completion attempts → journal | Shared completion tests and host tests. Does not cover Cursor's external agent runtime |-| Harness trigger classifier + image captions | The harness's settling recorder, handed to `services/agent_harness_service/src/trigger.rs` and passed to `FastModelTriggerJudge` and `VisionImageCaptioner`; authenticated message sender, Automation feature | Structured/image HTTP attempts → journal before existing errors are handled | `agent_trigger` caption tests + `agent` image-budget/cancellation/evidence tests. A non-user sender is not replaced with a system billing identity |+| Harness trigger classifier + image captions | The harness's settling recorder, handed to `services/agent_harness_service/src/trigger.rs` and passed to the Jev classifier behind `JevTriggerJudge` and to `VisionImageCaptioner`; authenticated message sender, Automation feature | Jev/image HTTP attempts → journal before existing errors are handled | `agent_trigger` caption tests + `agent` image-budget/cancellation/evidence tests. A non-user sender is not replaced with a system billing identity || Standalone trigger classifier + captions | Same explicit injection in `services/agent_trigger_service/src/main.rs` | Same journal attempt boundary, separate from harness and DCS | Standalone service and shared trigger tests. Caption timeout/error-to-None behavior retained; optional inference skips legacy quota refusal without AI fallback || DSS channel responder | `services/document_storage_service/src/main.rs` builds common context; responder binds requesting user, ChannelBot feature | AgentLoop HTTP attempts, native tools and subagents → journal; legacy aggregate retained | Shared agent/tool tests, DSS package tests || DSS channel classifier | Separate configured recorder supplied to `FastModelTriggerClassifier` in DSS `main.rs`; requesting user, ChannelBot | One-shot classification attempts → journal | Shared completion/structured tests. This is distinct from the responder's common context |diff --git a/docs/runbooks/ai-quota-enforcement.md b/docs/runbooks/ai-quota-enforcement.mdindex f1ddf5161b..47565d0285 100644--- a/docs/runbooks/ai-quota-enforcement.md+++ b/docs/runbooks/ai-quota-enforcement.md@@ -320,7 +320,7 @@ requests settlement). Admission itself never records usage or calls settlement.| Notion page imports / Import | [import admission](../../crates/import/src/domain/service/admission.rs), [execution/rechecks](../../crates/import/src/domain/service.rs) | DCS main, wrapped recorder | [delayed/deterministic cases](../../crates/import/src/domain/service/test/admission.rs), [HTTP](../../crates/import/src/inbound/axum_router/test.rs) || Channel responder / ChannelBot | [bot service](../../crates/channel_bots/src/domain/service.rs) | [DSS main](../../services/document_storage_service/src/main.rs), common context T | [response refusal](../../crates/channel_bots/src/domain/service/tests.rs) || Optional channel classification / ChannelBot | [trigger detector](../../crates/channel_bots/src/domain/trigger_detector.rs) | DSS main, independent R for classifier | [explicit routing and inference skip](../../crates/channel_bots/src/domain/trigger_detector/tests.rs) |-| Trigger inference and image captions / Automation | [trigger service](../../crates/agent_trigger/src/domain/service.rs) | [standalone trigger main](../../services/agent_trigger_service/src/main.rs) R; [harness trigger](../../services/agent_harness_service/src/trigger.rs) receives the harness's settling recorder from harness main | [classification/caption refusal](../../crates/agent_trigger/src/domain/service/test/admission.rs), [no system fallback](../../crates/agent_trigger/src/outbound/fast_model_judge/test.rs) |+| Trigger inference and image captions / Automation | [trigger service](../../crates/agent_trigger/src/domain/service.rs) | [standalone trigger main](../../services/agent_trigger_service/src/main.rs) R; [harness trigger](../../services/agent_harness_service/src/trigger.rs) receives the harness's settling recorder from harness main | [classification/caption refusal](../../crates/agent_trigger/src/domain/service/test/admission.rs), [no system fallback](../../crates/agent_trigger/src/outbound/jev_judge/test.rs) || Managed session ingress/dispatch/queue / AgentSession | [harness admission](../../crates/agent_harness/src/domain/service/admission.rs) | [harness main](../../services/agent_harness_service/src/main.rs): one `pg_settling_recorder` shared with T for in-memory; managed sandbox inference has **no `ai_usage` producer** | [owner, authorization, runtime funding, restart, queue rejection/outage](../../crates/agent_harness/src/domain/service/test/quota.rs), [HTTP](../../crates/agent_session/src/inbound/axum_router/test.rs), [event shape](../../crates/agent_session/src/domain/events/test.rs) || Direct in-memory ACP/provider commands / AgentSession | [admit_turn](../../crates/agent_inmem/src/domain/admission.rs), [agent execution](../../crates/agent_inmem/src/domain/agent.rs) | Harness main → manager → `RigTurnEngine`, the harness's settling recorder passed into T | [direct ACP, queue, cancel, history](../../crates/agent_inmem/src/domain/agent/test.rs), [manager](../../crates/agent_inmem/src/outbound/manager/test.rs) || Session naming / ChatRename | [name generator gate](../../crates/agent_session/src/domain/name_generation.rs) | Harness main, the same settling recorder for `HaikuAgentSessionNameGenerator` (exempt, so never counted or settled) | [fallback without provider calls](../../crates/agent_session/src/domain/name_generation/test.rs) |diff --git a/services/agent_harness_service/src/config.rs b/services/agent_harness_service/src/config.rsindex c3b9b3fa3c..3f789da878 100644--- a/services/agent_harness_service/src/config.rs+++ b/services/agent_harness_service/src/config.rs@@ -75,7 +75,8 @@ fn default_pipedream_environment() -> String {#[derive(macro_config::MacroConfig)]#[serde(rename_all = "SCREAMING_SNAKE_CASE")]pub struct Config {- /// Optional TypeSafe key for Command K classification.+ /// Optional TypeSafe key for Command K classification and for routing+ /// unmentioned thread replies to agents.pub typesafe_api_key: jev::outbound::TypesafeApiKey,/// Optional Workers AI account for Clairic extraction and Auto mode's Clef Flash.pub cloudflare_account_id: universal_input::outbound::clairic::CloudflareAccountId,diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex fe23fc1069..1f87a4fdf4 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -1772,6 +1772,14 @@ async fn run() -> anyhow::Result<()> {}});+ let trigger_classifier = config+ .typesafe_api_key+ .value()+ .map(jev::outbound::SystemOneJev::typesafe)+ .transpose()+ .context("invalid TYPESAFE_API_KEY")?+ .map(|provider| Arc::new(jev::domain::JevClassifier::new(provider, recorder.clone())));+// Keep trigger generation in this deployment while retaining Kafka as the// boundary between channel events and harness commands.let mut trigger = tokio::spawn(trigger::supervise(@@ -1780,6 +1788,7 @@ async fn run() -> anyhow::Result<()> {config.internal_api_key.clone(),config.document_storage_service_auth_key.clone(),trigger::TriggerServices {+ classifier: trigger_classifier,recorder,admission,messages: message_service,diff --git a/services/agent_harness_service/src/trigger.rs b/services/agent_harness_service/src/trigger.rsindex 8cb600ac0e..d3dad35734 100644--- a/services/agent_harness_service/src/trigger.rs+++ b/services/agent_harness_service/src/trigger.rs@@ -10,7 +10,7 @@ use agent_trigger::domain::task_assignment::{ProjectTaskAssignmentContext, TaskAssignmentContext, process_task_assignment,};use agent_trigger::outbound::{- BotRepoAgentLookup, ChannelRepoTypeLookup, DssTaskAssignmentContext, FastModelTriggerJudge,+ BotRepoAgentLookup, ChannelRepoTypeLookup, DssTaskAssignmentContext, JevTriggerJudge,LexicalExplicitReplyExtractor, MessageDiscussionReader, MessageThreadHistory, ParentNameReader,PgPeopleDirectory, VisionImageCaptioner,};@@ -59,7 +59,7 @@ type Trigger = AgentTriggerService<BotRepoAgentLookup<PgBotsRepo>,BotRepoAgentLookup<PgBotsRepo>,LexicalExplicitReplyExtractor,- FastModelTriggerJudge,+ Option<JevTriggerJudge<TriggerClassifier>>,MessageThreadHistory<entity_access::domain::service::EntityAccessServiceImpl<entity_access::outbound::PgAccessRepository,@@ -96,11 +96,18 @@ fn commit_message<M: MacroEventCollection + 'static>(.map_err(|error| anyhow::anyhow!("failed to commit trigger event offset: {error:?}"))}+/// The Jev classifier behind implicit trigger judgement.+pub type TriggerClassifier = jev::domain::JevClassifier<jev::outbound::SystemOneJev>;+/// Shared domain services retained across trigger-consumer restarts.#[derive(Clone)]pub struct TriggerServices {/// Recorder configured with the host's quota policy.pub recorder: Arc<dyn ai_usage::UsageRecorder>,+ /// Classifier judging which live agent an unmentioned thread reply is+ /// for; `None` when `TYPESAFE_API_KEY` is unset, so only mentions and+ /// explicit replies reach agents.+ pub classifier: Option<Arc<TriggerClassifier>>,/// Admission gate for Macro-funded inference.pub admission: Arc<dyn ai_billing::AiAdmissionService>,/// Message service used to announce task assignments.@@ -140,6 +147,7 @@ async fn run() -> anyhow::Result<()> {let TriggerServices {recorder,+ classifier,admission,messages,} = services;@@ -194,7 +202,7 @@ async fn run();let images = VisionImageCaptioner::new(static_file::outbound::CdnStaticFileRepo::new(StaticFileServiceUrl::new()?.to_string()),- recorder.clone(),+ recorder,);let trigger = AgentTriggerService::new(PgAgentSessionRepo::new(@@ -205,7 +213,7 @@ async fn run(BotRepoAgentLookup::new(PgBotsRepo::new(pool.clone())),BotRepoAgentLookup::new(PgBotsRepo::new(pool.clone())),LexicalExplicitReplyExtractor::new(lexical),- FastModelTriggerJudge::new(recorder, images),+ classifier.map(|classifier| JevTriggerJudge::new(classifier, images)),MessageThreadHistory::new(message_reader,entity_access::domain::service::EntityAccessServiceImpl::new(diff --git a/services/agent_trigger_service/Cargo.toml b/services/agent_trigger_service/Cargo.tomlindex 1f4958e1f2..fefedfb5a9 100644--- a/services/agent_trigger_service/Cargo.toml+++ b/services/agent_trigger_service/Cargo.toml@@ -17,6 +17,7 @@ crm = { path = "../../crates/crm", default-features = false, features = ["lookupcall = { path = "../../crates/call", default-features = false, features = ["outbound"] }agent_session = { path = "../../crates/agent_session" }agent_trigger = { path = "../../crates/agent_trigger", features = ["outbound"] }+jev = { path = "../../crates/jev", features = ["outbound"] }ai_usage = { path = "../../crates/ai_usage" }anyhow = { workspace = true }bots = { path = "../../crates/bots", default-features = false, features = ["outbound", "ports"] }diff --git a/services/agent_trigger_service/src/config.rs b/services/agent_trigger_service/src/config.rsindex 9624f07afb..c0170271a0 100644--- a/services/agent_trigger_service/src/config.rs+++ b/services/agent_trigger_service/src/config.rs@@ -36,6 +36,10 @@ pub struct Config {pub internal_api_key: String,/// Key required by document storage's internal endpoints.pub document_storage_service_auth_key: String,+ /// TypeSafe credential for the Jev classifier that routes unmentioned+ /// thread replies to agents. Optional: while unset, only mentions and+ /// explicit replies reach agents.+ pub typesafe_api_key: jev::outbound::TypesafeApiKey,}impl Config {diff --git a/services/agent_trigger_service/src/main.rs b/services/agent_trigger_service/src/main.rsindex 1200a88a35..c01c4f00b0 100644--- a/services/agent_trigger_service/src/main.rs+++ b/services/agent_trigger_service/src/main.rs@@ -16,7 +16,7 @@ use agent_trigger::domain::task_assignment::{ProjectTaskAssignmentContext, TaskAssignmentContext, process_task_assignment,};use agent_trigger::outbound::{- BotRepoAgentLookup, ChannelRepoTypeLookup, DssTaskAssignmentContext, FastModelTriggerJudge,+ BotRepoAgentLookup, ChannelRepoTypeLookup, DssTaskAssignmentContext, JevTriggerJudge,LexicalExplicitReplyExtractor, MessageDiscussionReader, MessageThreadHistory, ParentNameReader,PgPeopleDirectory, VisionImageCaptioner,};@@ -30,6 +30,8 @@ use entity_access::domain::{ports::EntityAccessService, service::EntityAccessSeruse entity_access::outbound::PgAccessRepository;use entity_registry::OwnerGrantPolicy;use entity_registry_db_utils::OwnedEntityRegistrar;+use jev::domain::JevClassifier;+use jev::outbound::SystemOneJev;use kafka_util::{GroupName, InitialOffset, KafkaEventConsumer, consumer_span, record_span_error};use lexical_client::LexicalClient;use macro_entrypoint::{MacroEntrypoint, shutdown_signal};@@ -70,7 +72,7 @@ type Trigger = AgentTriggerService<BotRepoAgentLookup<PgBotsRepo>,BotRepoAgentLookup<PgBotsRepo>,LexicalExplicitReplyExtractor,- FastModelTriggerJudge,+ Option<JevTriggerJudge<JevClassifier<SystemOneJev>>>,MessageThreadHistory<entity_access::domain::service::EntityAccessServiceImpl<entity_access::outbound::PgAccessRepository,@@ -197,6 +199,13 @@ async fn run() -> anyhow::Result<()> {static_file::outbound::CdnStaticFileRepo::new(StaticFileServiceUrl::new()?.to_string()),recorder.clone(),);+ let classifier = config+ .typesafe_api_key+ .value()+ .map(SystemOneJev::typesafe)+ .transpose()+ .context("invalid TYPESAFE_API_KEY")?+ .map(|provider| Arc::new(JevClassifier::new(provider, recorder)));let trigger = AgentTriggerService::new(PgAgentSessionRepo::new(pool.clone(),@@ -206,7 +215,7 @@ async fn run() -> anyhow::Result<()> {BotRepoAgentLookup::new(PgBotsRepo::new(pool.clone())),BotRepoAgentLookup::new(PgBotsRepo::new(pool.clone())),LexicalExplicitReplyExtractor::new(lexical),- FastModelTriggerJudge::new(recorder, images),+ classifier.map(|classifier| JevTriggerJudge::new(classifier, images)),MessageThreadHistory::new(message_reader,entity_access::domain::service::EntityAccessServiceImpl::new(