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): add Auto model routing and discounted Gemini pricing (#8389)
Wolf Mermelstein28380baba4af
diff --git a/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.json b/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.jsondeleted file mode 100644index 50f5866d56..0000000000--- a/.sqlx/query-353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e.json+++ /dev/null@@ -1,19 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n UPDATE ai_usage\n SET price_per_million_in = $2::real,\n price_per_million_out = $3::real,\n price_per_million_cache_read = $4::real,\n price_per_million_cache_write = $5::real,\n price_per_audio_minute = $6::real,\n total = CASE\n WHEN (audio_seconds IS NULL) <> ($6::real IS NULL) THEN NULL\n WHEN cache_read_input_tokens > 0 AND $4::real IS NULL THEN NULL\n WHEN cache_write_input_tokens > 0 AND $5::real IS NULL THEN NULL\n ELSE (input_tokens::real / 1000000.0::real) * $2::real\n + (output_tokens::real / 1000000.0::real) * $3::real\n + (cache_read_input_tokens::real / 1000000.0::real) * COALESCE($4::real, 0)\n + (cache_write_input_tokens::real / 1000000.0::real) * COALESCE($5::real, 0)\n + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($6::real, 0))::real\n END\n WHERE model = $1\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Text",- "Float4",- "Float4",- "Float4",- "Float4",- "Float4"- ]- },- "nullable": []- },- "hash": "353b008f819185be1bc3f20cbb7429ad9fe4274375707d9ba2ff9347fddb2b4e"-}diff --git a/.sqlx/query-4114e7a1ddbe43f4d6015e1e475164a61c4c4786184ec275a18f7896c684ad84.json b/.sqlx/query-4114e7a1ddbe43f4d6015e1e475164a61c4c4786184ec275a18f7896c684ad84.jsonnew file mode 100644index 0000000000..9642e71e12--- /dev/null+++ b/.sqlx/query-4114e7a1ddbe43f4d6015e1e475164a61c4c4786184ec275a18f7896c684ad84.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "\n UPDATE ai_usage\n SET price_per_million_in = e.price_per_million_in,\n price_per_million_out = e.price_per_million_out,\n price_per_million_cache_read = e.price_per_million_cache_read,\n price_per_million_cache_write = e.price_per_million_cache_write,\n price_per_audio_minute = e.price_per_audio_minute,\n total = CASE\n WHEN (ai_usage.audio_seconds IS NULL) <> (e.price_per_audio_minute IS NULL) THEN NULL\n WHEN ai_usage.cache_read_input_tokens > 0 AND e.price_per_million_cache_read IS NULL THEN NULL\n WHEN ai_usage.cache_write_input_tokens > 0 AND e.price_per_million_cache_write IS NULL THEN NULL\n ELSE (ai_usage.input_tokens::real / 1000000.0::real) * e.price_per_million_in\n + (ai_usage.output_tokens::real / 1000000.0::real) * e.price_per_million_out\n + (ai_usage.cache_read_input_tokens::real / 1000000.0::real) * COALESCE(e.price_per_million_cache_read, 0)\n + (ai_usage.cache_write_input_tokens::real / 1000000.0::real) * COALESCE(e.price_per_million_cache_write, 0)\n + (COALESCE(ai_usage.audio_seconds, 0) / 60.0 * COALESCE(e.price_per_audio_minute, 0))::real\n END\n FROM ai_effective_pricing e\n WHERE ai_usage.model = $1 AND e.model = $1\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "4114e7a1ddbe43f4d6015e1e475164a61c4c4786184ec275a18f7896c684ad84"+}diff --git a/.sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.json b/.sqlx/query-a44e4e8e4235fab4cdf5324429e126fefec0e641b67efab41660eac550c28f42.jsonsimilarity index 59%rename from .sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.jsonrename to .sqlx/query-a44e4e8e4235fab4cdf5324429e126fefec0e641b67efab41660eac550c28f42.jsonindex b158e99e81..88da14d509 100644--- a/.sqlx/query-0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c.json+++ b/.sqlx/query-a44e4e8e4235fab4cdf5324429e126fefec0e641b67efab41660eac550c28f42.json@@ -1,16 +1,16 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n price_per_million_in,\n price_per_million_out,\n price_per_million_cache_read,\n price_per_million_cache_write,\n price_per_audio_minute\n FROM ai_pricing\n WHERE model = $1\n ",+ "query": "\n SELECT\n price_per_million_in AS \"price_per_million_in!\",\n price_per_million_out AS \"price_per_million_out!\",\n price_per_million_cache_read,\n price_per_million_cache_write,\n price_per_audio_minute\n FROM ai_effective_pricing\n WHERE model = $1\n ","describe": {"columns": [{"ordinal": 0,- "name": "price_per_million_in",+ "name": "price_per_million_in!","type_info": "Float4"},{"ordinal": 1,- "name": "price_per_million_out",+ "name": "price_per_million_out!","type_info": "Float4"},{@@ -35,12 +35,12 @@]},"nullable": [- false,- false,+ true,+ true,true,true,true]},- "hash": "0c21b4586c245cfc25a6bbd17ba5e034a0ee2ee3237ded5b1645a58906b3ae6c"+ "hash": "a44e4e8e4235fab4cdf5324429e126fefec0e641b67efab41660eac550c28f42"}diff --git a/Cargo.lock b/Cargo.lockindex d41d7ed289..dea8e47580 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -600,6 +600,7 @@ dependencies = ["bot_id","bytes","chat",+ "cognition","dashmap","feature_flags","futures",@@ -620,6 +621,7 @@ dependencies = ["rig-agent","rmcp","roles_and_permissions",+ "rootcause","schemars 1.2.1","serde","serde_json",diff --git a/apps/web/src/lib/core/component/AI/component/ProviderIcon.test.tsx b/apps/web/src/lib/core/component/AI/component/ProviderIcon.test.tsxindex b67dbb96c1..cce86241e1 100644--- a/apps/web/src/lib/core/component/AI/component/ProviderIcon.test.tsx+++ b/apps/web/src/lib/core/component/AI/component/ProviderIcon.test.tsx@@ -19,8 +19,8 @@ describe('AI provider icons', () => {['gemini-2.5-pro', 'google'],['fireworks/kimi-k3', 'kimi'],['kimi-k3', 'kimi'],- ['fireworks/deepseek-v4-pro-0813', 'deepseek'],- ['deepseek-v4-pro-0813', 'deepseek'],+ ['fireworks/deepseek-v4p1-flash-1017', 'deepseek'],+ ['deepseek-v4p1-flash-1017', 'deepseek'],['fireworks/muse-glimmer-30b', 'muse'],['muse-glimmer-30b', 'muse'],['google/gemini-3.8-flash', 'google'],diff --git a/apps/web/src/lib/core/component/AI/component/input/modelCatalog.test.ts b/apps/web/src/lib/core/component/AI/component/input/modelCatalog.test.tsindex 7303a87b81..3e99c31170 100644--- a/apps/web/src/lib/core/component/AI/component/input/modelCatalog.test.ts+++ b/apps/web/src/lib/core/component/AI/component/input/modelCatalog.test.ts@@ -44,6 +44,18 @@ describe('model catalog sections', () => {);});+ it('leads the suggested choices with Auto', () => {+ const catalog = buildModelCatalog([+ { id: 'anthropic/claude-opus-5-5', label: 'Opus 5.5' },+ { id: 'auto', label: 'Auto' },+ { id: 'fireworks/kimi-k3', label: 'Kimi K3' },+ ]);+ expect(catalog.frontier.map((option) => option.id)).toEqual([+ 'auto',+ 'anthropic/claude-opus-5-5',+ ]);+ });+it('does not invent frontier choices for a restricted catalog', () => {const option = { id: 'custom', label: 'Custom model' };expect(buildModelCatalog([option])).toEqual({@@ -62,8 +74,8 @@ describe('model catalog sections', () => {{ id: 'auto', label: 'Auto' },{ id: 'private-model', label: 'Custom model' },]);+ expect(catalog.frontier.map((option) => option.id)).toEqual(['auto']);expect(catalog.providers.map((group) => group.label)).toEqual([- 'Automatic','OpenAI','Alibaba','Moonshot AI',diff --git a/apps/web/src/lib/core/component/AI/component/input/modelCatalog.ts b/apps/web/src/lib/core/component/AI/component/input/modelCatalog.tsindex bf9c2a3ee1..edce131b99 100644--- a/apps/web/src/lib/core/component/AI/component/input/modelCatalog.ts+++ b/apps/web/src/lib/core/component/AI/component/input/modelCatalog.ts@@ -118,7 +118,7 @@ export function modelProviderLabel(option: CatalogModelOption): string {}/**- * Flagship choices, followed by Gemini. Prefer the base model over effort/speed+ * Auto, then flagship choices, followed by Gemini. Prefer the base model over effort/speed* variants; every other variant remains in its provider's section. Include* suggested choices in provider groups when building the complete catalog.*/@@ -127,6 +127,7 @@ export function buildModelCatalog(includeSuggested = false): ModelCatalog {const frontierPatterns = [+ /^auto$/i,/^(?:claude[ -])?opus[ -]5[.-]5(?:$|[ -])/i,/^(?:claude[ -])?sonnet[ -]5[.-]5(?:$|[ -])/i,/^(?:gpt[ -]\d+(?:\.\d+)?[ -])?sol(?:$|[ -])|^gpt[ -]5\.6(?:[ -]sol)?(?:$|[ -](?:high|medium|low|fast)\b)/i,diff --git a/apps/web/src/lib/core/component/AI/constant/model-label.test.ts b/apps/web/src/lib/core/component/AI/constant/model-label.test.tsindex 915f4dedc2..12de8ea641 100644--- a/apps/web/src/lib/core/component/AI/constant/model-label.test.ts+++ b/apps/web/src/lib/core/component/AI/constant/model-label.test.ts@@ -10,8 +10,8 @@ describe('modelLabel', () => {expect(modelLabel('claude-haiku-4-5')).toBe('Haiku 4.5');expect(modelLabel('fireworks/kimi-k3')).toBe('Kimi K3');expect(modelLabel('kimi-k3')).toBe('Kimi K3');- expect(modelLabel('fireworks/deepseek-v4-pro-0813')).toBe(- 'DeepSeek V4 Pro'+ expect(modelLabel('fireworks/deepseek-v4p1-flash-1017')).toBe(+ 'DeepSeek V4.1 Flash');expect(modelLabel('fireworks/muse-glimmer-30b')).toBe('Muse Glimmer');expect(modelLabel('fireworks/glm-5p3')).toBe('GLM 5.3');diff --git a/apps/web/src/lib/core/component/AI/constant/model-label.ts b/apps/web/src/lib/core/component/AI/constant/model-label.tsindex 7093eaa0ba..2fc70a5851 100644--- a/apps/web/src/lib/core/component/AI/constant/model-label.ts+++ b/apps/web/src/lib/core/component/AI/constant/model-label.ts@@ -19,8 +19,9 @@ import { MODEL_PRETTYNAME, type Model } from './model';* session row may store both resolve to one entry.*/const HARNESS_PRETTYNAME: Record<string, string> = {+ auto: 'Auto','kimi-k3': 'Kimi K3',- 'deepseek-v4-pro-0813': 'DeepSeek V4 Pro',+ 'deepseek-v4p1-flash-1017': 'DeepSeek V4.1 Flash','muse-glimmer-30b': 'Muse Glimmer','glm-5p3': 'GLM 5.3','glm-5p3-flash': 'GLM 5.3 Flash',diff --git a/crates/agent_inmem/AGENTS.md b/crates/agent_inmem/AGENTS.mdindex 2e1f184e85..fbf6860310 100644--- a/crates/agent_inmem/AGENTS.md+++ b/crates/agent_inmem/AGENTS.md@@ -46,6 +46,11 @@ milliseconds instead of waiting on container provisioning.## Gotchas+- `auto` is a picker entry, not a routable id: `RigTurnEngine` asks its+ `AutoModelSelector` (Jev through cognition's named decisions, 1 s budget) per turn and runs+ Opus 5.5 (hard), GPT-6.1 Sol (mid) or Gemini 3.8 Flash (simple, and+ whenever Jev is unavailable). Capability lookups go through+ `models::resolve`.- Keep session-aware `NameSearch`, `ContentSearch`, and `ListEntities` native incode mode; legacy `ReadChat` must not appear in either runtime tool catalog.- Replay tool calls using `_meta.macro.toolName`; ACP titles may be human-readablediff --git a/crates/agent_inmem/Cargo.toml b/crates/agent_inmem/Cargo.tomlindex 9a3c30c432..358a497d97 100644--- a/crates/agent_inmem/Cargo.toml+++ b/crates/agent_inmem/Cargo.toml@@ -24,6 +24,7 @@ async-trait = { workspace = true }axum = { workspace = true }bot_id = { path = "../bot_id" }chat = { path = "../chat", default-features = false }+cognition = { path = "../cognition" }dashmap = { workspace = true }futures = { workspace = true }feature_flags = { path = "../feature_flags" }@@ -54,6 +55,7 @@ url = { workspace = true }workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]+rootcause = { workspace = true }agent_fold = { path = "../folds/agent_fold" }agent_session = { path = "../agent_session", features = ["test-utils"] }bot_id = { path = "../bot_id", features = ["test-utils"] }diff --git a/crates/agent_inmem/src/domain/auto_model.rs b/crates/agent_inmem/src/domain/auto_model.rsnew file mode 100644index 0000000000..d163e8742b--- /dev/null+++ b/crates/agent_inmem/src/domain/auto_model.rs@@ -0,0 +1,141 @@+//! Which model an Auto turn runs on.++use std::{collections::BTreeMap, future::Future, pin::Pin, sync::Arc, time::Duration};++use cognition::domain::{Cognition, DecisionRequest};+use macro_user_id::user_id::MacroUserIdStr;+use tracing::Instrument as _;++use super::models::resolve;++#[cfg(test)]+mod test;++/// The model a prompt needing high intelligence runs on.+pub const HIGH_MODEL: &str = "anthropic/claude-opus-5-5";++/// The model a prompt that is neither hard nor simple runs on.+pub const MID_MODEL: &str = "openai/gpt-6.1-sol";++/// Asked first: a yes runs the turn on [`HIGH_MODEL`].+const HIGH_QUESTION: &str = "Does this request require higher intelligence?";++/// Asked alongside: a yes, without a yes to [`HIGH_QUESTION`], keeps the turn+/// on Auto's default model; a no runs it on [`MID_MODEL`].+const LOW_QUESTION: &str = "Is this request simple enough for a smaller, faster model?";++/// Decision keys for [`HIGH_QUESTION`] and [`LOW_QUESTION`].+const HIGH_KEY: &str = "high";+const LOW_KEY: &str = "simple";++/// How long a turn waits on Jev before running on Auto's default model.+const DECISION_TIMEOUT: Duration = Duration::from_secs(1);++/// Jev's probability at or above which a question counts as yes.+const YES_THRESHOLD: f32 = 0.5;++/// Choose the routable model for one Auto turn from its prompt.+pub trait AutoModelSelector: Send + Sync {+ /// The model id the turn runs on. Never fails: an unavailable classifier+ /// keeps the turn on Auto's default model.+ fn select<'a>(+ &'a self,+ owner: &'a MacroUserIdStr<'static>,+ prompt: &'a str,+ ) -> Pin<Box<dyn Future<Output = &'static str> + Send + 'a>>;+}++/// Auto without a classifier: every turn runs on Auto's default model.+pub struct DefaultAutoModel;++impl AutoModelSelector for DefaultAutoModel {+ fn select<'a>(+ &'a self,+ _: &'a MacroUserIdStr<'static>,+ _: &'a str,+ ) -> Pin<Box<dyn Future<Output = &'static str> + Send + 'a>> {+ Box::pin(async { resolve(super::models::AUTO_MODEL) })+ }+}++/// Asks Jev, through cognition's named decisions, how much intelligence the+/// prompt needs: [`HIGH_MODEL`] for hard prompts, Auto's default for simple+/// ones, [`MID_MODEL`] otherwise.+pub struct JevAutoModel {+ cognition: Arc<dyn Cognition>,+}++impl JevAutoModel {+ /// Compose with the host's cognition client, which meters each decision.+ pub fn new(cognition: Arc<dyn Cognition>) -> Self {+ Self { cognition }+ }+}++impl AutoModelSelector for JevAutoModel {+ fn select<'a>(+ &'a self,+ owner: &'a MacroUserIdStr<'static>,+ prompt: &'a str,+ ) -> Pin<Box<dyn Future<Output = &'static str> + Send + 'a>> {+ Box::pin(async move {+ let span = tracing::info_span!(+ "agent.auto_model",+ agent.auto_model.high = tracing::field::Empty,+ agent.auto_model.low = tracing::field::Empty,+ agent.auto_model.selected = tracing::field::Empty,+ agent.auto_model.outcome = tracing::field::Empty,+ );+ let request = DecisionRequest {+ input: serde_json::json!({ "request": prompt }),+ questions: BTreeMap::from([+ (HIGH_KEY.to_owned(), HIGH_QUESTION.to_owned()),+ (LOW_KEY.to_owned(), LOW_QUESTION.to_owned()),+ ]),+ };+ let answer =+ tokio::time::timeout(DECISION_TIMEOUT, self.cognition.decide(owner, request))+ .instrument(span.clone())+ .await;+ let probabilities = match &answer {+ Ok(Ok(response)) => response+ .probabilities+ .get(HIGH_KEY)+ .zip(response.probabilities.get(LOW_KEY)),+ _ => None,+ };+ let selected = match probabilities {+ Some((&high, &low)) => {+ span.record("agent.auto_model.high", high);+ span.record("agent.auto_model.low", low);+ span.record("agent.auto_model.outcome", "classified");+ if high >= YES_THRESHOLD {+ HIGH_MODEL+ } else if low >= YES_THRESHOLD {+ resolve(super::models::AUTO_MODEL)+ } else {+ MID_MODEL+ }+ }+ None => {+ let outcome = match &answer {+ Err(_) => "timeout",+ Ok(Err(_)) => "provider_error",+ Ok(Ok(_)) => "invalid_response",+ };+ span.record("agent.auto_model.outcome", outcome);+ span.in_scope(|| {+ tracing::warn!(+ outcome,+ error = ?answer.as_ref().ok().and_then(|result| result.as_ref().err()),+ "Auto model classification failed; using the default"+ );+ });+ resolve(super::models::AUTO_MODEL)+ }+ };+ span.record("agent.auto_model.selected", selected);+ selected+ })+ }+}diff --git a/crates/agent_inmem/src/domain/auto_model/test.rs b/crates/agent_inmem/src/domain/auto_model/test.rsnew file mode 100644index 0000000000..3104cd697a--- /dev/null+++ b/crates/agent_inmem/src/domain/auto_model/test.rs@@ -0,0 +1,138 @@+use std::{+ sync::{Arc, Mutex},+ time::Duration,+};++use async_trait::async_trait;+use cognition::domain::{+ Cognition, DecisionRequest, DecisionResponse, GenerateObjectRequest, GenerateObjectResponse,+};+use macro_user_id::user_id::MacroUserIdStr;+use rootcause::Report;+use serde_json::json;++use super::{AutoModelSelector, DefaultAutoModel, HIGH_MODEL, JevAutoModel, MID_MODEL};++const GEMINI: &str = "google/gemini-3.8-flash";++enum Answer {+ Probabilities(&'static [(&'static str, f32)]),+ Fails,+ Hangs,+}++struct FakeCognition {+ answer: Answer,+ seen: Mutex<Vec<DecisionRequest>>,+}++#[async_trait]+impl Cognition for FakeCognition {+ async fn generate_object(+ &self,+ _: &MacroUserIdStr<'static>,+ _: GenerateObjectRequest,+ ) -> Result<GenerateObjectResponse, Report> {+ panic!("choosing a model must not generate text")+ }++ async fn decide(+ &self,+ _: &MacroUserIdStr<'static>,+ request: DecisionRequest,+ ) -> Result<DecisionResponse, Report> {+ self.seen.lock().unwrap().push(request);+ match self.answer {+ Answer::Probabilities(probabilities) => Ok(DecisionResponse {+ probabilities: probabilities+ .iter()+ .map(|(key, probability)| ((*key).to_owned(), *probability))+ .collect(),+ }),+ Answer::Fails => {+ Err(Report::new_sendsync(std::io::Error::other("jev is down")).into_dynamic())+ }+ Answer::Hangs => {+ tokio::time::sleep(Duration::from_secs(60)).await;+ unreachable!("the selector times out first")+ }+ }+ }+}++fn selector(answer: Answer) -> (JevAutoModel, Arc<FakeCognition>) {+ let cognition = Arc::new(FakeCognition {+ answer,+ seen: Mutex::default(),+ });+ (JevAutoModel::new(cognition.clone()), cognition)+}++fn owner() -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email("[email protected]").unwrap()+}++#[tokio::test]+async fn a_hard_prompt_escalates_to_opus() {+ let (selector, cognition) = selector(Answer::Probabilities(&[("high", 0.9), ("simple", 0.6)]));++ let model = selector+ .select(&owner(), "Prove the scheduler cannot deadlock")+ .await;++ assert_eq!(model, HIGH_MODEL);+ assert_eq!(HIGH_MODEL, "anthropic/claude-opus-5-5");+ let seen = cognition.seen.lock().unwrap();+ assert_eq!(+ seen[0].input,+ json!({ "request": "Prove the scheduler cannot deadlock" })+ );+ assert_eq!(+ seen[0].questions.keys().collect::<Vec<_>>(),+ vec!["high", "simple"]+ );+}++#[tokio::test]+async fn a_simple_prompt_stays_on_gemini() {+ let (selector, _) = selector(Answer::Probabilities(&[("high", 0.03), ("simple", 0.98)]));++ assert_eq!(selector.select(&owner(), "hi").await, GEMINI);+}++#[tokio::test]+async fn an_ordinary_prompt_runs_on_sol() {+ let (selector, _) = selector(Answer::Probabilities(&[("high", 0.2), ("simple", 0.3)]));++ assert_eq!(+ selector.select(&owner(), "Summarize this doc").await,+ MID_MODEL+ );+ assert_eq!(MID_MODEL, "openai/gpt-6.1-sol");+}++#[tokio::test]+async fn a_missing_answer_stays_on_gemini() {+ let (selector, _) = selector(Answer::Probabilities(&[("high", 0.9)]));++ assert_eq!(selector.select(&owner(), "hi").await, GEMINI);+}++#[tokio::test]+async fn a_failed_decision_stays_on_gemini() {+ let (selector, _) = selector(Answer::Fails);++ assert_eq!(selector.select(&owner(), "hi").await, GEMINI);+}++#[tokio::test(start_paused = true)]+async fn a_slow_decision_stays_on_gemini() {+ let (selector, _) = selector(Answer::Hangs);++ assert_eq!(selector.select(&owner(), "hi").await, GEMINI);+}++#[tokio::test]+async fn without_cognition_auto_is_gemini() {+ assert_eq!(DefaultAutoModel.select(&owner(), "hi").await, GEMINI);+}diff --git a/crates/agent_inmem/src/domain/mod.rs b/crates/agent_inmem/src/domain/mod.rsindex aaadd74959..48588f9a1e 100644--- a/crates/agent_inmem/src/domain/mod.rs+++ b/crates/agent_inmem/src/domain/mod.rs@@ -3,6 +3,8 @@pub mod admission;pub mod agent;+/// Which model an Auto turn runs on.+pub mod auto_model;pub mod engine;pub mod mcp;pub mod model_access;diff --git a/crates/agent_inmem/src/domain/models.rs b/crates/agent_inmem/src/domain/models.rsindex 54f9a7cf46..191d007b83 100644--- a/crates/agent_inmem/src/domain/models.rs+++ b/crates/agent_inmem/src/domain/models.rs@@ -4,6 +4,20 @@ use std::sync::OnceLock;use chat::domain::models::CHAT_MODELS;+/// Picker entry that lets Macro choose the model. Turns run on+/// [`AUTO_TARGET`]; [`resolve`] maps it before routing.+pub const AUTO_MODEL: &str = "auto";++/// The model an [`AUTO_MODEL`] turn runs on.+const AUTO_TARGET: &str = "google/gemini-3.8-flash";++/// The routable id a turn runs on: [`AUTO_MODEL`] becomes its target, every+/// other id is already routable.+#[must_use]+pub fn resolve(id: &str) -> &str {+ if id == AUTO_MODEL { AUTO_TARGET } else { id }+}+/// Models the in-memory agent offers beyond the closed chat catalog, paired/// with the house name the picker shows.///@@ -12,10 +26,11 @@ use chat::domain::models::CHAT_MODELS;/// `accounts/fireworks/models/<slug>` wire id; other providers send the slug/// verbatim.pub const ROUTED_MODELS: &[(&str, &str)] = &[+ (AUTO_MODEL, "Auto"),("fireworks/kimi-k3", "Kimi K3"),- ("fireworks/deepseek-v4-pro-0813", "DeepSeek V4 Pro"),- ("fireworks/muse-glimmer-30b", "Muse Glimmer"),+ ("fireworks/deepseek-v4p1-flash-1017", "DeepSeek V4.1 Flash"),("fireworks/glm-5p3", "GLM 5.3"),+ ("fireworks/muse-glimmer-30b", "Muse Glimmer"),("fireworks/glm-5p3-flash", "GLM 5.3 Flash"),("fireworks/qwen3p8-max", "Qwen 3.8 Max"),("fireworks/minimax-m3", "MiniMax M3"),@@ -37,13 +52,14 @@ pub fn display_name(id: &str) -> &str {.unwrap_or(id)}-/// Closed chat models plus the routed extras, in picker order.+/// Auto, then the closed chat models, then the routed extras, in picker order.#[must_use]pub fn advertised_models() -> &'static [&'static str] {static MODELS: OnceLock<Vec<&'static str>> = OnceLock::new();MODELS.get_or_init(|| {let mut models = Vec::with_capacity(CHAT_MODELS.len() + ROUTED_MODELS.len());+ models.push(AUTO_MODEL);models.extend_from_slice(CHAT_MODELS);for (model, _) in ROUTED_MODELS {if !models.contains(model) {@@ -65,17 +81,18 @@ pub fn advertised_models() -> &'static [&'static str] {/// Unknown models remain unknown rather than being advertised as text-only.#[must_use]pub fn supports_images(id: &str) -> Option<bool> {+ let id = resolve(id);if CHAT_MODELS.contains(&id) {return Some(true);}match id {"fireworks/kimi-k3"+ | "fireworks/deepseek-v4p1-flash-1017"| "fireworks/muse-glimmer-30b"| "fireworks/glm-5p3-flash"| "fireworks/qwen3p8-max"| "fireworks/minimax-m3" => Some(true),- "fireworks/deepseek-v4-pro-0813"- | "fireworks/glm-5p3"+ "fireworks/glm-5p3"| "fireworks/nemotron-lightning-3p5-30b-a3b"| "cerebras/gpt-oss-120b" => Some(false),_ => None,@@ -93,7 +110,7 @@ pub fn supports_images(id: &str) -> Option<bool> {/// models stay unknown rather than being given a guess.#[must_use]pub fn context_window(id: &str) -> Option<u64> {- Some(match id {+ Some(match resolve(id) {"anthropic/claude-sonnet-5-5" | "anthropic/claude-opus-5-5" => 1_000_000,"anthropic/claude-haiku-4-5" => 200_000,"openai/gpt-6-astra" | "openai/gpt-6.1-sol" | "openai/gpt-5.6" | "openai/gpt-5.5" => {@@ -102,7 +119,7 @@ pub fn context_window(id: &str) -> Option<u64> {"openai/gpt-5.6-mini" | "openai/gpt-5-mini" => 400_000,"google/gemini-3.8-flash" => 1_048_576,"fireworks/kimi-k3"- | "fireworks/deepseek-v4-pro-0813"+ | "fireworks/deepseek-v4p1-flash-1017"| "fireworks/glm-5p3"| "fireworks/glm-5p3-flash" => 1_040_000,"fireworks/minimax-m3" => 512_000,diff --git a/crates/agent_inmem/src/domain/models/test.rs b/crates/agent_inmem/src/domain/models/test.rsindex af01113b1a..67f71bbea1 100644--- a/crates/agent_inmem/src/domain/models/test.rs+++ b/crates/agent_inmem/src/domain/models/test.rs@@ -1,8 +1,8 @@-use super::{ROUTED_MODELS, advertised_models, display_name};+use super::{AUTO_MODEL, ROUTED_MODELS, advertised_models, display_name, resolve};use chat::domain::models::CHAT_MODELS;#[test]-fn advertised_models_keep_chat_then_append_routed() {+fn advertised_models_lead_with_auto_then_chat_then_routed() {let models = advertised_models();assert_eq!(models@@ -11,24 +11,34 @@ fn advertised_models_keep_chat_then_append_routed() {.count(),1);- assert_eq!(&models[..CHAT_MODELS.len()], CHAT_MODELS);+ assert_eq!(models[0], AUTO_MODEL);+ assert_eq!(&models[1..=CHAT_MODELS.len()], CHAT_MODELS);assert_eq!(- &models[CHAT_MODELS.len()..],+ &models[CHAT_MODELS.len() + 1..],ROUTED_MODELS.iter().map(|(model, _)| *model)- .filter(|model| !CHAT_MODELS.contains(model))+ .filter(|model| *model != AUTO_MODEL && !CHAT_MODELS.contains(model)).collect::<Vec<_>>().as_slice());}+#[test]+fn auto_defaults_to_gemini() {+ assert_eq!(resolve(AUTO_MODEL), "google/gemini-3.8-flash");+ assert_eq!(resolve("fireworks/glm-5p3"), "fireworks/glm-5p3");+ assert_eq!(display_name(AUTO_MODEL), "Auto");+ assert_eq!(super::supports_images(AUTO_MODEL), Some(true));+ assert_eq!(super::context_window(AUTO_MODEL), Some(1_048_576));+}+#[test]fn routed_models_have_house_names() {assert_eq!(display_name("fireworks/kimi-k3"), "Kimi K3");assert_eq!(- display_name("fireworks/deepseek-v4-pro-0813"),- "DeepSeek V4 Pro"+ display_name("fireworks/deepseek-v4p1-flash-1017"),+ "DeepSeek V4.1 Flash");assert_eq!(display_name("fireworks/muse-glimmer-30b"), "Muse Glimmer");assert_eq!(display_name("fireworks/glm-5p3"), "GLM 5.3");@@ -52,6 +62,7 @@ fn routed_models_have_house_names() {#[test]fn routed_models_use_known_providers() {for (model, _) in ROUTED_MODELS {+ let model = resolve(model);let provider = model.split('/').next().expect("a provider segment");assert!(matches!(provider, "cerebras" | "fireworks" | "google"),diff --git a/crates/agent_inmem/src/rig_engine.rs b/crates/agent_inmem/src/rig_engine.rsindex b95c5169cd..a44d7299d6 100644--- a/crates/agent_inmem/src/rig_engine.rs+++ b/crates/agent_inmem/src/rig_engine.rs@@ -38,6 +38,7 @@ use sqlx::PgPool;use tokio::sync::mpsc;use tracing::Instrument as _;+use crate::domain::auto_model::{AutoModelSelector, DefaultAutoModel, JevAutoModel};use crate::domain::engine::{AgentIdentity, TurnEngine, TurnRequest};use crate::domain::tool_gate::{NativeToolGate, NativeToolVerdict, UngatedNativeTools};use crate::domain::tool_mode::{StandardTools, ToolMode, ToolModeSelector};@@ -75,6 +76,7 @@ pub struct RigTurnEngine {native_tools: Arc<NativeTools>,code_mode_tools: Arc<NativeTools>,tool_mode: Arc<dyn ToolModeSelector>,+ auto_model: Arc<dyn AutoModelSelector>,tool_context: ToolServiceContext,gate: Arc<dyn NativeToolGate>,}@@ -97,6 +99,10 @@ impl RigTurnEngine {true,)),tool_mode: Arc::new(StandardTools),+ auto_model: match &tool_context.cognition_tool_context.service {+ Some(cognition) => Arc::new(JevAutoModel::new(Arc::clone(cognition))),+ None => Arc::new(DefaultAutoModel),+ },tool_context,gate: Arc::new(UngatedNativeTools),}@@ -272,11 +278,17 @@ impl TurnEngine for RigTurnEngine {let native_tools = Arc::clone(&self.native_tools);let code_mode_tools = Arc::clone(&self.code_mode_tools);let tool_mode = Arc::clone(&self.tool_mode);+ let auto_model = Arc::clone(&self.auto_model);let tool_context = self.tool_context.clone();let gate = Arc::clone(&self.gate);let metering = agent::MeteringContext::current();tokio::spawn(agent::MeteringContext::carry(metering, async move {+ let mut request = request;+ let Some(model) = select_auto_model(auto_model.as_ref(), &request).await else {+ return;+ };+ request.model = model;let Some(selected) = select_tool_mode(tool_mode.as_ref(), &request).await else {return;};@@ -323,6 +335,32 @@ async fn forward_turn_result(}}+// Auto is a picker entry, not a routable id: the turn's prompt decides the+// model it runs on. Every other id passes through.+async fn select_auto_model(+ selector: &dyn AutoModelSelector,+ request: &TurnRequest,+) -> Option<String> {+ if request.model != crate::domain::models::AUTO_MODEL {+ return Some(request.model.clone());+ }+ let Owner::User(owner) = &request.owner else {+ return Some(crate::domain::models::resolve(&request.model).to_owned());+ };+ let prompt = request+ .messages+ .iter()+ .rev()+ .find(|message| message.role == agent::types::Role::User)+ .and_then(|message| message.content.user_message_text())+ .unwrap_or_default();+ tokio::select! {+ biased;+ _ = request.cancel.cancelled() => None,+ model = selector.select(owner, &prompt) => Some(model.to_owned()),+ }+}+// A failed/missing code-mode MCP connection must never leave a turn with only the// small direct set. Summaries neither evaluate flags nor advertise tools.async fn select_tool_mode(diff --git a/crates/ai_usage/AGENTS.md b/crates/ai_usage/AGENTS.mdindex 572f8a62c4..f144c6ca24 100644--- a/crates/ai_usage/AGENTS.md+++ b/crates/ai_usage/AGENTS.md@@ -51,6 +51,10 @@ ledger decisions, plus a flexible admin query API.- `UsageRecorder` (analytics) is best-effort and never fails theoriginating call; `FinancialUsage` (billable) is awaited and fail-closed— don't conflate the two when adding a new recording path.+- Read prices from the `ai_effective_pricing` view, never `ai_pricing`+ directly: it applies `ai_pricing_discount`, a per-model input and output+ percent off (input's also covers cache and audio rates). Gemini 3.8 Flash+ is discounted to DeepSeek V4.1 Flash's $0.30 / $1.20.## Relateddiff --git a/crates/ai_usage/src/outbound/pg_usage_repo.rs b/crates/ai_usage/src/outbound/pg_usage_repo.rsindex eec5187660..8c01c5562e 100644--- a/crates/ai_usage/src/outbound/pg_usage_repo.rs+++ b/crates/ai_usage/src/outbound/pg_usage_repo.rs@@ -116,12 +116,12 @@ impl UsageRepo for PgUsageRepo {let row = sqlx::query!(r#"SELECT- price_per_million_in,- price_per_million_out,+ price_per_million_in AS "price_per_million_in!",+ price_per_million_out AS "price_per_million_out!",price_per_million_cache_read,price_per_million_cache_write,price_per_audio_minute- FROM ai_pricing+ FROM ai_effective_pricingWHERE model = $1"#,normalize_model_id(model),@@ -178,33 +178,30 @@ impl UsageRepo for PgUsageRepo {.execute(&mut *tx).await?;- // Recompute the price of every recorded row for this model.+ // Recompute the price of every recorded row for this model, at its+ // discounted rates.sqlx::query!(r#"UPDATE ai_usage- SET price_per_million_in = $2::real,- price_per_million_out = $3::real,- price_per_million_cache_read = $4::real,- price_per_million_cache_write = $5::real,- price_per_audio_minute = $6::real,+ SET price_per_million_in = e.price_per_million_in,+ price_per_million_out = e.price_per_million_out,+ price_per_million_cache_read = e.price_per_million_cache_read,+ price_per_million_cache_write = e.price_per_million_cache_write,+ price_per_audio_minute = e.price_per_audio_minute,total = CASE- WHEN (audio_seconds IS NULL) <> ($6::real IS NULL) THEN NULL- WHEN cache_read_input_tokens > 0 AND $4::real IS NULL THEN NULL- WHEN cache_write_input_tokens > 0 AND $5::real IS NULL THEN NULL- ELSE (input_tokens::real / 1000000.0::real) * $2::real- + (output_tokens::real / 1000000.0::real) * $3::real- + (cache_read_input_tokens::real / 1000000.0::real) * COALESCE($4::real, 0)- + (cache_write_input_tokens::real / 1000000.0::real) * COALESCE($5::real, 0)- + (COALESCE(audio_seconds, 0) / 60.0 * COALESCE($6::real, 0))::real+ WHEN (ai_usage.audio_seconds IS NULL) <> (e.price_per_audio_minute IS NULL) THEN NULL+ WHEN ai_usage.cache_read_input_tokens > 0 AND e.price_per_million_cache_read IS NULL THEN NULL+ WHEN ai_usage.cache_write_input_tokens > 0 AND e.price_per_million_cache_write IS NULL THEN NULL+ ELSE (ai_usage.input_tokens::real / 1000000.0::real) * e.price_per_million_in+ + (ai_usage.output_tokens::real / 1000000.0::real) * e.price_per_million_out+ + (ai_usage.cache_read_input_tokens::real / 1000000.0::real) * COALESCE(e.price_per_million_cache_read, 0)+ + (ai_usage.cache_write_input_tokens::real / 1000000.0::real) * COALESCE(e.price_per_million_cache_write, 0)+ + (COALESCE(ai_usage.audio_seconds, 0) / 60.0 * COALESCE(e.price_per_audio_minute, 0))::realEND- WHERE model = $1+ FROM ai_effective_pricing e+ WHERE ai_usage.model = $1 AND e.model = $1"#,model,- per_in,- per_out,- per_cache_read,- per_cache_write,- per_audio_minute,).execute(&mut *tx).await?;diff --git a/crates/ai_usage/src/outbound/pg_usage_repo/test.rs b/crates/ai_usage/src/outbound/pg_usage_repo/test.rsindex 56ac2a54db..c6e97e1f3b 100644--- a/crates/ai_usage/src/outbound/pg_usage_repo/test.rs+++ b/crates/ai_usage/src/outbound/pg_usage_repo/test.rs@@ -121,6 +121,71 @@ async fn insert_and_query_roundtrips(pool: PgPool) {assert!((priced.cost.price.as_ref().unwrap().total - 30.0).abs() < 1e-3);}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn gemini_3_8_flash_is_priced_like_deepseek_v4p1_flash(pool: PgPool) {+ let repo = PgUsageRepo::new(pool);++ assert_eq!(+ repo.get_pricing("google/gemini-3.8-flash").await.unwrap(),+ Some(ModelPricing::Tokens {+ input: 0.3,+ output: 1.2,+ cache_read: Some(0.03),+ cache_write: None,+ })+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn set_pricing_recomputes_existing_rows_at_the_discounted_rate(pool: PgPool) {+ let repo = PgUsageRepo::new(pool.clone());+ repo.set_pricing(+ "discounted-model",+ ModelPricing::Tokens {+ input: 2.0,+ output: 8.0,+ cache_read: None,+ cache_write: None,+ },+ )+ .await+ .unwrap();+ sqlx::query!(+ "INSERT INTO ai_pricing_discount (model, input_discount_percent, output_discount_percent) VALUES ('discounted-model', 25, 50)"+ )+ .execute(&pool)+ .await+ .unwrap();+ repo.insert_usage(+ &completion(+ AiFeature::Automation,+ "discounted-model",+ 1_000_000,+ 1_000_000,+ ),+ true,+ )+ .await+ .unwrap();++ repo.set_pricing(+ "discounted-model",+ ModelPricing::Tokens {+ input: 4.0,+ output: 8.0,+ cache_read: None,+ cache_write: None,+ },+ )+ .await+ .unwrap();++ let after = repo.query_usage(&UsageApiParams::default()).await.unwrap();+ let price = after[0].cost.price.as_ref().unwrap();+ // 1M input * $4/1M * 75% + 1M output * $8/1M * 50% = $3 + $4+ assert!((price.total - 7.0).abs() < 1e-3);+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn set_pricing_recomputes_existing_rows(pool: PgPool) {let repo = PgUsageRepo::new(pool);diff --git a/crates/macro_db_client/migrations/20261020233004_ai_pricing_discount.sql b/crates/macro_db_client/migrations/20261020233004_ai_pricing_discount.sqlnew file mode 100644index 0000000000..2c5b8a2fa8--- /dev/null+++ b/crates/macro_db_client/migrations/20261020233004_ai_pricing_discount.sql@@ -0,0 +1,35 @@+-- Per-model discounts on AI pricing. Every price read goes through+-- ai_effective_pricing, which applies a model's input discount to its input,+-- cache and audio rates and its output discount to its output rate, so+-- usage is recorded and recomputed at the discounted price. Usage already+-- recorded keeps its price until set_pricing recomputes the model.+CREATE TABLE ai_pricing_discount (+ model TEXT PRIMARY KEY REFERENCES ai_pricing (model) ON DELETE CASCADE,+ input_discount_percent NUMERIC(5, 2) NOT NULL+ CHECK (input_discount_percent BETWEEN 0 AND 100),+ output_discount_percent NUMERIC(5, 2) NOT NULL+ CHECK (output_discount_percent BETWEEN 0 AND 100),+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()+);++CREATE VIEW ai_effective_pricing AS+SELECT+ p.model,+ (p.price_per_million_in::numeric * f.input)::real AS price_per_million_in,+ (p.price_per_million_out::numeric * f.output)::real AS price_per_million_out,+ (p.price_per_million_cache_read::numeric * f.input)::real AS price_per_million_cache_read,+ (p.price_per_million_cache_write::numeric * f.input)::real AS price_per_million_cache_write,+ (p.price_per_audio_minute::numeric * f.input)::real AS price_per_audio_minute+FROM ai_pricing p+LEFT JOIN ai_pricing_discount d ON d.model = p.model+CROSS JOIN LATERAL (+ SELECT+ 1 - COALESCE(d.input_discount_percent, 0) / 100 AS input,+ 1 - COALESCE(d.output_discount_percent, 0) / 100 AS output+) f;++-- Gemini 3.8 Flash at DeepSeek V4.1 Flash's price: $0.75 -> $0.30 in,+-- $3.75 -> $1.20 out.+INSERT INTO ai_pricing_discount (model, input_discount_percent, output_discount_percent)+SELECT model, 60, 68 FROM ai_pricing WHERE model = 'gemini-3.8-flash'+ON CONFLICT (model) DO NOTHING;diff --git a/crates/macro_db_client/migrations/20261021153141_seed_deepseek_v4p1_flash_pricing.sql b/crates/macro_db_client/migrations/20261021153141_seed_deepseek_v4p1_flash_pricing.sqlnew file mode 100644index 0000000000..df406a25df--- /dev/null+++ b/crates/macro_db_client/migrations/20261021153141_seed_deepseek_v4p1_flash_pricing.sql@@ -0,0 +1,8 @@+-- Seed Fireworks serverless prices (USD per million tokens) for DeepSeek V4.1+-- Flash, which replaces DeepSeek V4 Pro in the Macro agent catalog: Fireworks+-- no longer serves deepseek-v4-pro-0813 serverless. Source:+-- https://fireworks.ai/models (2026-10-20). ON CONFLICT preserves prices+-- managed at runtime through set_pricing.+INSERT INTO ai_pricing (model, price_per_million_in, price_per_million_out) VALUES+ ('deepseek-v4p1-flash-1017', 0.30, 1.20)+ON CONFLICT (model) DO NOTHING;diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.mdindex 7f9d20dac8..794e75b89e 100644--- a/docs/agent-guide/ai-chat.md+++ b/docs/agent-guide/ai-chat.md@@ -384,7 +384,9 @@ the shimmer.action/availability reason. **Create agent** uses the shared rounded pillbutton at the bottom right with a subtle background. Models have readablenames (for example, **Sonnet 5.5**) and provider or model icons aligned with the- agent icons. The in-memory catalog offers the closed Anthropic and OpenAI chat+ agent icons. The in-memory catalog leads with **Auto**, where Jev sorts each+ turn's prompt: hard prompts run on **Opus 5.5**, simple ones on **Gemini 3.8+ Flash**, and the rest on **GPT-6.1 Sol**. It then offers the closed Anthropic and OpenAI chatmodels; Kimi, DeepSeek, Muse, GLM, Qwen, MiniMax, GPT OSS, and Nemotronopen-weight models; and Google's **Gemini 3.8 Flash**. The Anthropic modelsare **Sonnet 5.5**, **Opus 5.5**, and **Haiku 4.5**; older Claude versions