Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(ai): make Gemini Flash the only free-plan model (#7621)

Wolf Mermelstein53037da2f11b
diff --git a/.github/workflows/web-app-check-main.yml b/.github/workflows/web-app-check-main.yml
index 31612ad4ee..02af6c9f61 100644
--- a/.github/workflows/web-app-check-main.yml
+++ b/.github/workflows/web-app-check-main.yml
@@ -188,6 +188,10 @@ jobs:
uses: './.github/actions/setup-reqs-web'
with:
playwright: 'true'
+ - name: Build agent-fold WASM for tests
+ run: just build-agent-fold-wasm
+ timeout-minutes: 15
+ working-directory: apps/web
- name: Test
run: bunx vitest
working-directory: apps/web
diff --git a/Cargo.lock b/Cargo.lock
index 12fe80c3bf..2d60478b4e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -459,6 +459,7 @@ dependencies = [
"macro_uuid",
"mcp_client",
"messages",
+ "model_owner",
"notification",
"pipedream_mcp",
"properties",
@@ -526,6 +527,7 @@ dependencies = [
"prompt",
"rig-agent",
"rmcp",
+ "roles_and_permissions",
"schemars 1.2.1",
"serde",
"serde_json",
diff --git a/apps/web/src/features/agents-view/views/NewChatPage.test.tsx b/apps/web/src/features/agents-view/views/NewChatPage.test.tsx
index 3fc5f8ef0c..fb586b2042 100644
--- a/apps/web/src/features/agents-view/views/NewChatPage.test.tsx
+++ b/apps/web/src/features/agents-view/views/NewChatPage.test.tsx
@@ -21,6 +21,7 @@ import { NewChatPage } from './NewChatPage';
const mocks = vi.hoisted(() => ({
touch: false,
+ freePlan: false,
openSettings: vi.fn(),
capabilitiesPending: false,
attachments: [] as InputAttachmentData[],
@@ -126,6 +127,13 @@ vi.mock('@queries/agents/models', () => ({
get data() {
if (!enabled()) throw new Error('Pending resource must not be read');
const harness = target().harness;
+ if (harness === 'in-memory' && mocks.freePlan) {
+ return {
+ status: 'available',
+ currentModel: 'google/gemini-3.8-flash',
+ models: [{ id: 'google/gemini-3.8-flash', name: 'Gemini 3.8 Flash' }],
+ };
+ }
return {
status: 'available',
currentModel: harness === 'cursor' ? 'cursor-default' : 'chat-default',
@@ -262,6 +270,7 @@ describe('agent-led new conversation', () => {
let motionStyles: HTMLStyleElement;
beforeEach(() => {
mocks.capabilitiesPending = false;
+ mocks.freePlan = false;
mocks.touch = false;
mocks.attachments = [];
mocks.recentIds = [MACRO_CODER_BOT_ID];
@@ -356,6 +365,7 @@ describe('agent-led new conversation', () => {
prompt: 'Prompt',
botId: undefined,
repoUrl: undefined,
+ modelOverride: 'chat-default',
});
});
it('selects a coding agent, keeps the draft, and only sends the repository to coding agents', async () => {
@@ -461,7 +471,8 @@ describe('agent-led new conversation', () => {
repoBranch: 'feature/other',
});
});
- it('uses a saved agent without sending a per-session model override', async () => {
+ it('uses Gemini for a free user starting a saved in-memory agent', async () => {
+ mocks.freePlan = true;
const send = page(true, [
{
bot: { id: 'saved-agent', name: 'Reviewer', handle: 'reviewer' },
@@ -475,8 +486,8 @@ describe('agent-led new conversation', () => {
prompt: 'Prompt',
botId: 'saved-agent',
repoUrl: undefined,
+ modelOverride: 'google/gemini-3.8-flash',
});
- expect(send.mock.calls[0][0]).not.toHaveProperty('modelOverride');
});
it.each(['claude-cloud', 'codex-cloud', 'future-runtime'])(
'shows the drawer for a saved %s coding agent without forwarding unsupported repository overrides',
@@ -763,6 +774,27 @@ describe('agent-led new conversation', () => {
'Sonnet 5.5'
);
});
+ it('uses the free catalog default instead of a persisted paid model', async () => {
+ mocks.freePlan = true;
+ mocks.preferredInmemModel = 'anthropic/claude-sonnet-5-5';
+ const send = page();
+ expect(screen.getByRole('button', { name: 'Agent' }).textContent).toBe(
+ 'Gemini 3.8 Flash'
+ );
+ openAgents();
+ await waitFor(() => expect(screen.getByRole('menu')).toBeTruthy());
+ expect(screen.queryByTitle('Sonnet 5.5')).toBeNull();
+ expect(
+ within(screen.getByRole('menu')).getByTitle('Gemini 3.8 Flash')
+ ).toBeTruthy();
+ fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' });
+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));
+ expect(send).toHaveBeenCalledWith(
+ expect.objectContaining({
+ modelOverride: 'google/gemini-3.8-flash',
+ })
+ );
+ });
it('restores the most recently used supported agent', async () => {
mocks.recentIds = [CURSOR_BOT_ID];
page();
diff --git a/apps/web/src/features/agents-view/views/NewChatPage.tsx b/apps/web/src/features/agents-view/views/NewChatPage.tsx
index 7810046ad4..236f38027e 100644
--- a/apps/web/src/features/agents-view/views/NewChatPage.tsx
+++ b/apps/web/src/features/agents-view/views/NewChatPage.tsx
@@ -96,27 +96,20 @@ export function NewChatPage(props: {
const wanted = agentId() ?? recentAgentId() ?? MACRO_PERSONA_ID;
return options().find((agent) => agent.id === wanted) ?? options()[0];
});
- const macro = () => options().find((agent) => agent.id === MACRO_PERSONA_ID);
- const macroCatalog = createComposerModels(macro);
- /** Preferred Macro model when it is still in the live in-memory catalog. */
- const preferredInmemModel = () => {
- const id = preferredInmem.model();
- if (!id) return undefined;
- const catalog = macroCatalog.models();
- // Until discovery returns, keep the stored id so the trigger can label it.
- if (catalog.length === 0) return id;
- return catalog.some((option) => option.id === id) ? id : undefined;
- };
- /**
- * Model shown on the agent control and sent with the next start. Coding
- * agents use a one-shot override; Macro prefers an in-session pick, then
- * the remembered Models choice.
- */
+ const selectedCatalog = createComposerModels(selected);
+ /** In-memory choices come from the owner's catalog; other runtimes keep their rules. */
const composerModelOverride = () => {
- if (selected()?.id === MACRO_PERSONA_ID) {
- return modelOverride() ?? preferredInmemModel();
- }
- return modelOverride();
+ const agent = selected();
+ if (agent?.harness !== 'macro-inmem' && agent?.harness !== 'in-memory')
+ return modelOverride();
+ const preferred =
+ modelOverride() ??
+ (agent.id === MACRO_PERSONA_ID
+ ? preferredInmem.model()
+ : agent.defaultModel);
+ return selectedCatalog.models().some((option) => option.id === preferred)
+ ? preferred
+ : selectedCatalog.currentModel();
};
const capabilityTarget = () => {
const agent = selected();
@@ -125,7 +118,9 @@ export function NewChatPage(props: {
if (harness !== 'in-memory' && harness !== 'cursor') return undefined;
return {
harness,
- model: composerModelOverride() ?? agent?.defaultModel,
+ model:
+ composerModelOverride() ??
+ (harness === 'in-memory' ? undefined : agent?.defaultModel),
} as const;
};
const capabilities = useAgentCapabilitiesQuery(capabilityTarget);
diff --git a/apps/web/src/features/block-fig/queries/fig-comments.test.ts b/apps/web/src/features/block-fig/queries/fig-comments.test.ts
index 0bd945b294..c3d5ba9ca5 100644
--- a/apps/web/src/features/block-fig/queries/fig-comments.test.ts
+++ b/apps/web/src/features/block-fig/queries/fig-comments.test.ts
@@ -1,7 +1,17 @@
import type { Message, MessageListItem } from '@service-storage/messages';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import { figAnchor, figThread } from './fig-comments';
+// These mapping tests do not mount the live comment query/mutation hooks.
+vi.mock('@queries/messages/document-messages', () => ({
+ useMessageRootsQuery: vi.fn(),
+}));
+vi.mock('@queries/messages/mutations', () => ({
+ newMessageId: vi.fn(),
+ usePatchThreadMutation: vi.fn(),
+ useSendMessageMutation: vi.fn(),
+}));
+
const parent = { type: 'document' as const, id: 'design' };
const anchor = { type: 'fig', pageId: '0:1', nodeId: '1:10', x: 4, y: 8 };
diff --git a/apps/web/src/lib/core/component/AI/constant/model.test.ts b/apps/web/src/lib/core/component/AI/constant/model.test.ts
index 7ed1b504c0..b0c5207715 100644
--- a/apps/web/src/lib/core/component/AI/constant/model.test.ts
+++ b/apps/web/src/lib/core/component/AI/constant/model.test.ts
@@ -29,7 +29,8 @@ describe('modelsForPlan / defaultModelForPlan', () => {
it('gives free users only the free model, defaulted to it', () => {
const free = modelsForPlan(false);
expect(free).toEqual([FREE_DEFAULT_MODEL]);
- expect(FREE_DEFAULT_MODEL).toBe(Model.haiku45);
+ expect(FREE_DEFAULT_MODEL).toBe(Model.gemini38Flash);
+ expect(free).not.toContain(Model.haiku45);
expect(defaultModelForPlan(false)).toBe(FREE_DEFAULT_MODEL);
// The premium models are *not* in a free user's selectable set.
expect(free).not.toContain(Model.sonnet55);
@@ -78,6 +79,14 @@ describe('parseModel', () => {
});
describe('alternateProviderModel', () => {
+ it('does not offer a paid fallback when the free provider fails', () => {
+ expect(
+ alternateProviderModel(Model.gemini38Flash, {
+ candidates: modelsForPlan(false),
+ })
+ ).toBeUndefined();
+ });
+
it('always suggests a model from a different provider than the current one', () => {
for (const current of Object.values(Model)) {
const alt = alternateProviderModel(current);
diff --git a/apps/web/src/lib/core/component/AI/constant/model.ts b/apps/web/src/lib/core/component/AI/constant/model.ts
index 2fc6366c96..d76ec17434 100644
--- a/apps/web/src/lib/core/component/AI/constant/model.ts
+++ b/apps/web/src/lib/core/component/AI/constant/model.ts
@@ -57,21 +57,21 @@ export const DEFAULT_MODEL: TModel = Model.sonnet55;
/**
* Default model for free users. Free users aren't entitled to the premium
- * models (which the backend rejects with a 403), so they start on Haiku.
+ * models (which the backend rejects with a 403), so they start on Gemini Flash.
*/
-export const FREE_DEFAULT_MODEL: TModel = Model.haiku45;
+export const FREE_DEFAULT_MODEL: TModel = Model.gemini38Flash;
/** Models a paid user may select. */
export const PAID_MODELS: readonly TModel[] = Object.values(Model);
/**
* Model for database AI: question answering, the database assistant, and chats
- * opened from a database. Paid-only, like every model but {@link FREE_DEFAULT_MODEL}.
+ * opened from a database. Available on both free and paid plans.
*/
export const DATABASE_MODEL: TModel = Model.gemini38Flash;
/**
- * Models a free user may select. Free users only get Haiku
+ * Models a free user may select. Free users only get Gemini Flash
* (`FREE_DEFAULT_MODEL`); every other model is paid-only and shows locked in
* the selector, where selecting one opens the paywall instead of being sent
* and rejected by the backend.
diff --git a/apps/web/src/lib/queries/agents/models.ts b/apps/web/src/lib/queries/agents/models.ts
index c8550e1e80..92f128ca81 100644
--- a/apps/web/src/lib/queries/agents/models.ts
+++ b/apps/web/src/lib/queries/agents/models.ts
@@ -29,8 +29,8 @@ export function buildAgentModelTargets(
return targets;
}
-/** In-memory models are compiled into the harness and do not change at runtime. */
-const IN_MEMORY_MODELS_STALE_TIME = Number.POSITIVE_INFINITY;
+/** The in-memory catalog depends on current plan permissions. */
+const IN_MEMORY_MODELS_STALE_TIME = 60 * 1000;
/**
* Cursor and macrod catalogs can change, but not between two mounts of the
diff --git a/crates/agent_harness/src/domain/capability_discovery.rs b/crates/agent_harness/src/domain/capability_discovery.rs
index d87b37e026..643ae7f945 100644
--- a/crates/agent_harness/src/domain/capability_discovery.rs
+++ b/crates/agent_harness/src/domain/capability_discovery.rs
@@ -164,7 +164,7 @@ impl<Access, InMemory, Cursor, Macrod> AgentCapabilitiesService
for AgentCapabilitiesServiceImpl<Access, InMemory, Cursor, Macrod>
where
Access: HarnessCapabilityAccess,
- InMemory: CapabilityProbe<Target = ()>,
+ InMemory: CapabilityProbe<Target = MacroUserIdStr<'static>>,
Cursor: CapabilityProbe<Target = MacroUserIdStr<'static>>,
Macrod: CapabilityProbe<Target = HarnessId>,
{
@@ -175,7 +175,7 @@ where
) -> Result<AgentCapabilities, DiscoverAgentCapabilitiesError> {
match (request.harness, request.harness_id) {
(CapabilityHarness::InMemory, None) => {
- self.discover(&self.in_memory, &(), request.model.as_deref())
+ self.discover(&self.in_memory, &caller, request.model.as_deref())
.await
}
(CapabilityHarness::Cursor, None) => {
diff --git a/crates/agent_harness/src/domain/capability_discovery/test.rs b/crates/agent_harness/src/domain/capability_discovery/test.rs
index df5a3630b4..ebc8c139b8 100644
--- a/crates/agent_harness/src/domain/capability_discovery/test.rs
+++ b/crates/agent_harness/src/domain/capability_discovery/test.rs
@@ -172,6 +172,7 @@ async fn unsupported_provider_returns_the_supported_response_shape() {
assert_eq!(result, AgentCapabilities::unsupported());
assert_eq!(service.in_memory.calls(), 1);
+ assert_eq!(*service.in_memory.targets.lock().unwrap(), vec![caller()]);
assert_eq!(service.cursor.calls(), 0);
}
diff --git a/crates/agent_harness/src/domain/model_load.rs b/crates/agent_harness/src/domain/model_load.rs
index f93101b0fc..cead249cb8 100644
--- a/crates/agent_harness/src/domain/model_load.rs
+++ b/crates/agent_harness/src/domain/model_load.rs
@@ -132,7 +132,10 @@ pub trait HarnessModelAccess: Send + Sync + 'static {
/// Fresh in-memory model probe.
pub trait InMemoryModelProbe: Send + Sync + 'static {
/// Probe the running in-memory implementation.
- fn probe(&self) -> impl Future<Output = Result<RawModelProbe, ModelProbeError>> + Send;
+ fn probe(
+ &self,
+ caller: &MacroUserIdStr<'static>,
+ ) -> impl Future<Output = Result<RawModelProbe, ModelProbeError>> + Send;
}
/// Fresh Cursor model probe.
@@ -229,7 +232,7 @@ where
None => Ok(RawModelProbe::Unsupported),
},
(ModelHarness::InMemory, None) => {
- tokio::time::timeout(self.timeout, self.in_memory.probe())
+ tokio::time::timeout(self.timeout, self.in_memory.probe(&caller))
.await
.map_err(|_| LoadAgentModelsError::Timeout)?
}
diff --git a/crates/agent_harness/src/domain/model_load/test.rs b/crates/agent_harness/src/domain/model_load/test.rs
index 88fc71a4ee..e076462ec2 100644
--- a/crates/agent_harness/src/domain/model_load/test.rs
+++ b/crates/agent_harness/src/domain/model_load/test.rs
@@ -56,6 +56,7 @@ impl HarnessModelAccess for Access {
}
struct Probe {
+ callers: std::sync::Mutex<Vec<MacroUserIdStr<'static>>>,
calls: AtomicUsize,
result: fn() -> Result<RawModelProbe, ModelProbeError>,
}
@@ -64,6 +65,7 @@ impl Probe {
fn new(result: fn() -> Result<RawModelProbe, ModelProbeError>) -> Self {
Self {
calls: AtomicUsize::new(0),
+ callers: std::sync::Mutex::new(Vec::new()),
result,
}
}
@@ -74,7 +76,11 @@ impl Probe {
}
impl InMemoryModelProbe for Probe {
- async fn probe(&self) -> Result<RawModelProbe, ModelProbeError> {
+ async fn probe(
+ &self,
+ caller: &MacroUserIdStr<'static>,
+ ) -> Result<RawModelProbe, ModelProbeError> {
+ self.callers.lock().unwrap().push(caller.clone());
self.calls.fetch_add(1, Ordering::Relaxed);
(self.result)()
}
@@ -145,6 +151,7 @@ async fn claude_models_are_never_returned_for_in_memory() {
}
assert_eq!(claude.calls(), 1);
assert_eq!(service.in_memory.calls(), 1);
+ assert_eq!(*service.in_memory.callers.lock().unwrap(), vec![caller()]);
}
impl MacrodModelProbe for Probe {
@@ -252,6 +259,7 @@ async fn unsupported_provider_returns_the_supported_response_shape() {
assert_eq!(result, AgentModels::unsupported());
assert_eq!(service.in_memory.calls(), 1);
+ assert_eq!(*service.in_memory.callers.lock().unwrap(), vec![caller()]);
assert_eq!(service.cursor.calls(), 0);
}
diff --git a/crates/agent_inmem/Cargo.toml b/crates/agent_inmem/Cargo.toml
index f7b053604d..3d38a7934e 100644
--- a/crates/agent_inmem/Cargo.toml
+++ b/crates/agent_inmem/Cargo.toml
@@ -28,6 +28,7 @@ futures = { workspace = true }
genai_telemetry = { path = "../genai_telemetry" }
macro_user_id = { path = "../macro_user_id" }
model_owner = { path = "../model_owner" }
+roles_and_permissions = { path = "../roles_and_permissions", default-features = false, features = ["ports"] }
http = { workspace = true }
http-body-util = { workspace = true }
macro_uuid = { path = "../macro_uuid" }
diff --git a/crates/agent_inmem/src/domain/agent.rs b/crates/agent_inmem/src/domain/agent.rs
index a89283b6f7..c4d2769284 100644
--- a/crates/agent_inmem/src/domain/agent.rs
+++ b/crates/agent_inmem/src/domain/agent.rs
@@ -13,6 +13,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
+use crate::domain::model_access::{InMemModelAccess, ModelAccess, ModelAccessError};
use agent::ReasoningEffort;
use agent::types::{AssistantMessagePart, ChatMessage};
use agent::{StreamAccumulator, StreamPart, ToolResponse};
@@ -34,7 +35,7 @@ use agent_client_protocol::{
};
use agent_runtime_protocol::domain::action::MODEL_CONFIG_ID;
use agent_session::domain::model::AgentSessionId;
-use ai_billing::domain::{AiAdmissionError, AiAdmissionService};
+use ai_billing::domain::AiAdmissionService;
use ai_tools::user_tool_review::{
ReviewError, ReviewFieldKind, ReviewForm, ReviewOutcome, ReviewRequest, UserToolReviewer,
};
@@ -120,6 +121,8 @@ pub struct AgentState {
pub engine: Arc<dyn TurnEngine>,
/// Admission for new provider-backed turns, including direct ACP requests.
pub admission: Arc<dyn AiAdmissionService>,
+ /// Current model permissions for the trusted session owner.
+ pub model_access: Arc<dyn InMemModelAccess>,
/// Conversation state, shared with the manager so it survives reattach.
pub store: Arc<SessionStore>,
/// Every outstanding turn's cancellation token - the running turn and any
@@ -243,13 +246,13 @@ impl AgentState {
/// ACP model configuration backed by the engine's supported-model source
/// and this session's current selection.
- fn session_config_options(&self) -> Vec<SessionConfigOption> {
+ fn session_config_options(&self, access: ModelAccess) -> Vec<SessionConfigOption> {
let Some(session) = self.store.get(&self.session_id) else {
return Vec::new();
};
crate::domain::model_options::session_config_options(
&session.model,
- self.engine.supported_models(),
+ &access.models(self.engine.supported_models()),
session.reasoning_effort,
)
}
@@ -553,12 +556,25 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
let state = Arc::clone(&state);
async move |request: NewSessionRequest, responder, connection| {
let state = Arc::clone(&state);
+ let access = match state.model_access.access(&state.owner).await {
+ Ok(access) => access,
+ Err(error) => {
+ return responder.respond_with_error(model_access_error(error));
+ }
+ };
+ if let Some(model) = state
+ .store
+ .get(&state.session_id)
+ .map(|session| session.model.clone())
+ {
+ state.set_model(access.default_model(&model).to_owned());
+ }
let acp_id = SessionId::new(macro_uuid::generate_uuid_v7().to_string());
state.bind_acp_session(acp_id.clone(), false);
state.start_connect_mcp(request.mcp_servers);
let responded = responder.respond(
NewSessionResponse::new(acp_id.clone())
- .config_options(state.session_config_options()),
+ .config_options(state.session_config_options(access)),
);
advertise_commands(&state, &connection, acp_id);
responded
@@ -571,6 +587,19 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
let state = Arc::clone(&state);
async move |request: ResumeSessionRequest, responder, connection| {
let state = Arc::clone(&state);
+ let access = match state.model_access.access(&state.owner).await {
+ Ok(access) => access,
+ Err(error) => {
+ return responder.respond_with_error(model_access_error(error));
+ }
+ };
+ if let Some(model) = state
+ .store
+ .get(&state.session_id)
+ .map(|session| session.model.clone())
+ {
+ state.set_model(access.default_model(&model).to_owned());
+ }
// Kept when the state already belongs to this ACP id -
// either this process served the session, or a cold
// attach replayed the frame log back into it (see
@@ -578,7 +607,8 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
state.bind_acp_session(request.session_id.clone(), true);
state.start_connect_mcp(request.mcp_servers);
let responded = responder.respond(
- ResumeSessionResponse::new().config_options(state.session_config_options()),
+ ResumeSessionResponse::new()
+ .config_options(state.session_config_options(access)),
);
advertise_commands(&state, &connection, request.session_id);
responded
@@ -654,14 +684,7 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
// Admission failure must not tear down resumable state.
let _ = match result {
Ok(stop) => responder.respond(PromptResponse::new(stop)),
- Err(error) => responder.respond_with_error(
- AcpError::new(-32603, error.to_string()).data(
- serde_json::json!({
- "code": error.code(),
- "retryable": error.is_retryable(),
- }),
- ),
- ),
+ Err(error) => responder.respond_with_error(error),
};
Ok(())
}
@@ -680,6 +703,12 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
if let Err(error) = state.expect_session(&request.session_id) {
return responder.respond_with_error(error);
}
+ let access = match state.model_access.access(&state.owner).await {
+ Ok(access) => access,
+ Err(error) => {
+ return responder.respond_with_error(model_access_error(error));
+ }
+ };
let Some(value) = request.value.as_value_id() else {
return responder.respond_with_error(
AcpError::invalid_params().data("the config option takes a value id"),
@@ -696,6 +725,11 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
AcpError::invalid_params().data("unsupported model"),
);
}
+ if !access.allows(&value.to_string()) {
+ return responder.respond_with_error(model_access_error(
+ ModelAccessError::Forbidden,
+ ));
+ }
state.set_model(value.to_string());
}
REASONING_EFFORT_CONFIG_ID => {
@@ -725,7 +759,7 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
}
}
responder.respond(SetSessionConfigOptionResponse::new(
- state.session_config_options(),
+ state.session_config_options(access),
))
}
},
@@ -748,6 +782,15 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr
.await
}
+fn model_access_error(error: ModelAccessError) -> AcpError {
+ let (code, retryable) = match error {
+ ModelAccessError::Forbidden => ("model_access_denied", false),
+ ModelAccessError::Unavailable => ("model_access_unavailable", true),
+ };
+ AcpError::new(-32603, error.to_string())
+ .data(serde_json::json!({ "code": code, "retryable": retryable }))
+}
+
/// Run one turn to completion, streaming updates as they arrive.
async fn run_turn(
state: &AgentState,
@@ -755,7 +798,7 @@ async fn run_turn(
acp_session_id: SessionId,
prompt: UserPrompt,
cancel: CancellationToken,
-) -> Result<StopReason, AiAdmissionError> {
+) -> Result<StopReason, AcpError> {
// Include lock and admission waits in time to first output.
let started = Instant::now();
// Mark every exit (including denial or a dropped connection) complete so
@@ -774,7 +817,11 @@ async fn run_turn(
tokio::select! {
biased;
_ = cancel.cancelled() => return Ok(StopReason::Cancelled),
- result = admit_turn(state.admission.as_ref(), &state.owner) => result?,
+ result = admit_turn(state.admission.as_ref(), &state.owner) => result.map_err(|error|
+ AcpError::new(-32603, error.to_string()).data(serde_json::json!({
+ "code": error.code(), "retryable": error.is_retryable(),
+ }))
+ )?,
}
span.record(
"agent.turn.admission_wait_ms",
@@ -792,6 +839,14 @@ async fn run_turn(
identity,
instructions,
} = state.turn_input(&prompt);
+ let access = tokio::select! {
+ biased;
+ _ = cancel.cancelled() => return Ok(StopReason::Cancelled),
+ result = state.model_access.access(&state.owner) => result.map_err(model_access_error)?,
+ };
+ if !access.allows(&model) {
+ return Err(model_access_error(ModelAccessError::Forbidden));
+ }
let awaiting = Arc::new(AwaitingUser::default());
let requester = user_input_requester(state, connection, acp_session_id.clone(), &awaiting);
let mut parts = state.engine.run_turn(TurnRequest {
diff --git a/crates/agent_inmem/src/domain/agent/test.rs b/crates/agent_inmem/src/domain/agent/test.rs
index 24aaf013f4..c8442da504 100644
--- a/crates/agent_inmem/src/domain/agent/test.rs
+++ b/crates/agent_inmem/src/domain/agent/test.rs
@@ -69,6 +69,21 @@ async fn with_admission<Engine: TurnEngine, Out>(
engine: Arc<Engine>,
admission: Arc<dyn AiAdmissionService>,
scenario: impl AsyncFnOnce(ConnectionTo<Agent>, SessionId, Arc<AgentState>) -> Out,
+) -> (Vec<SessionNotification>, Vec<SessionConfigOption>, Out) {
+ with_model_access(
+ engine,
+ admission,
+ Arc::new(crate::testing::TestModelAccess::paid()),
+ scenario,
+ )
+ .await
+}
+
+async fn with_model_access<Engine: TurnEngine, Out>(
+ engine: Arc<Engine>,
+ admission: Arc<dyn AiAdmissionService>,
+ model_access: Arc<dyn InMemModelAccess>,
+ scenario: impl AsyncFnOnce(ConnectionTo<Agent>, SessionId, Arc<AgentState>) -> Out,
) -> (Vec<SessionNotification>, Vec<SessionConfigOption>, Out) {
let store = Arc::new(SessionStore::new());
let session_id = AgentSessionId::new();
@@ -83,6 +98,7 @@ async fn with_admission<Engine: TurnEngine, Out>(
),
engine,
admission,
+ model_access,
store,
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
@@ -419,7 +435,8 @@ async fn new_session_advertises_the_engine_supported_models() {
.collect::<Vec<_>>(),
vec![
("anthropic/claude-sonnet-5-5", "anthropic/claude-sonnet-5-5"),
- ("other-model", "other-model")
+ ("other-model", "other-model"),
+ (chat::domain::models::FREE_MODEL, "Gemini 3.8 Flash")
]
);
@@ -935,6 +952,7 @@ async fn serve_with_mcp(
),
engine,
admission: Arc::new(DisabledAiAdmissionService),
+ model_access: Arc::new(crate::testing::TestModelAccess::paid()),
store,
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
@@ -990,6 +1008,7 @@ async fn session_new_dials_the_advertised_servers_except_macros_own() {
),
engine: Arc::new(ScriptedEngine::new(Vec::new())),
admission: Arc::new(DisabledAiAdmissionService),
+ model_access: Arc::new(crate::testing::TestModelAccess::paid()),
store,
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
@@ -1078,6 +1097,7 @@ async fn first_prompt_waits_for_background_mcp_connect() {
),
engine: Arc::clone(&engine) as Arc<dyn TurnEngine>,
admission: Arc::new(DisabledAiAdmissionService),
+ model_access: Arc::new(crate::testing::TestModelAccess::paid()),
store,
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
@@ -1181,6 +1201,7 @@ where
),
engine,
admission: Arc::new(DisabledAiAdmissionService),
+ model_access: Arc::new(crate::testing::TestModelAccess::paid()),
store,
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
@@ -1814,3 +1835,108 @@ async fn effort_is_validated_and_model_changes_return_complete_options() {
})
.await;
}
+
+#[tokio::test]
+async fn free_session_defaults_to_gemini_and_rejects_paid_model_changes() {
+ use crate::domain::model_access::ModelAccess;
+ use chat::domain::models::FREE_MODEL;
+ let engine = Arc::new(ScriptedEngine::new(vec![StreamPart::Content("ok".into())]));
+ let access = Arc::new(crate::testing::TestModelAccess::new(ModelAccess::Free));
+ let (_, config, ()) = with_model_access(
+ engine.clone(),
+ Arc::new(DisabledAiAdmissionService),
+ access.clone(),
+ async |connection, session, state| {
+ assert_eq!(
+ state.store.get(&state.session_id).unwrap().model,
+ FREE_MODEL
+ );
+ let error = connection
+ .send_request(SetSessionConfigOptionRequest::new(
+ session.clone(),
+ MODEL_CONFIG_ID,
+ SessionConfigValueId::new("other-model"),
+ ))
+ .block_task()
+ .await
+ .unwrap_err();
+ assert_eq!(
+ serde_json::to_value(error).unwrap()["data"]["code"],
+ "model_access_denied"
+ );
+ connection
+ .send_request(text_prompt(&session, "hello"))
+ .block_task()
+ .await
+ .unwrap();
+ assert_eq!(engine.requests()[0].model, FREE_MODEL);
+ assert!(
+ access
+ .owners
+ .lock()
+ .unwrap()
+ .iter()
+ .all(|owner| owner == &state.owner)
+ );
+ },
+ )
+ .await;
+ let selection = agent_fold::domain::model_selection::model_selection(&config).unwrap();
+ assert_eq!(selection.options.len(), 1);
+}
+
+#[tokio::test]
+async fn downgrade_and_permission_failure_cannot_run_a_paid_model() {
+ use crate::domain::model_access::{ModelAccess, ModelAccessError};
+ for failure in [Ok(ModelAccess::Free), Err(ModelAccessError::Unavailable)] {
+ let engine = Arc::new(ScriptedEngine::new(vec![StreamPart::Content("ok".into())]));
+ let access = Arc::new(crate::testing::TestModelAccess::paid());
+ with_model_access(
+ engine.clone(),
+ Arc::new(DisabledAiAdmissionService),
+ access.clone(),
+ async |connection, session, state| {
+ *access.result.lock().unwrap() = failure;
+ let error = connection
+ .send_request(text_prompt(&session, "blocked"))
+ .block_task()
+ .await
+ .unwrap_err();
+ let code = if failure.is_ok() {
+ "model_access_denied"
+ } else {
+ "model_access_unavailable"
+ };
+ assert_eq!(serde_json::to_value(error).unwrap()["data"]["code"], code);
+ assert!(engine.requests().is_empty());
+ assert!(
+ state
+ .store
+ .get(&state.session_id)
+ .unwrap()
+ .history
+ .is_empty()
+ );
+ if failure.is_ok() {
+ let restored = connection
+ .send_request(ResumeSessionRequest::new(session.clone(), "/"))
+ .block_task()
+ .await
+ .unwrap();
+ let selection = agent_fold::domain::model_selection::model_selection(
+ restored.config_options.as_deref().unwrap(),
+ )
+ .unwrap();
+ assert_eq!(selection.options.len(), 1);
+ connection
+ .send_request(text_prompt(&session, "resumed"))
+ .block_task()
+ .await
+ .unwrap();
+ assert_eq!(engine.requests()[0].model, chat::domain::models::FREE_MODEL);
+ }
+ },
+ )
+ .await;
+ }
+}
diff --git a/crates/agent_inmem/src/domain/mod.rs b/crates/agent_inmem/src/domain/mod.rs
index 3dbfbc3115..ce5d8f94a7 100644
--- a/crates/agent_inmem/src/domain/mod.rs
+++ b/crates/agent_inmem/src/domain/mod.rs
@@ -5,6 +5,7 @@ pub mod admission;
pub mod agent;
pub mod engine;
pub mod mcp;
+pub mod model_access;
/// ACP session configuration generated from the turn engine catalog.
pub mod model_options;
/// Models this runtime advertises, including routed Fireworks and Gemini ids.
diff --git a/crates/agent_inmem/src/domain/model_access.rs b/crates/agent_inmem/src/domain/model_access.rs
new file mode 100644
index 0000000000..18ffd73315
--- /dev/null
+++ b/crates/agent_inmem/src/domain/model_access.rs
@@ -0,0 +1,95 @@
+//! Plan-based model access for the in-memory runtime only.
+
+use async_trait::async_trait;
+use chat::domain::models::FREE_MODEL;
+use model_owner::Owner;
+use roles_and_permissions::domain::{model::PermissionId, port::UserRolesAndPermissionsService};
+
+/// The model entitlement resolved from the trusted session owner.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ModelAccess {
+ /// Only the free model is available.
+ Free,
+ /// Every model supported by the in-memory engine is available.
+ Paid,
+}
+
+impl ModelAccess {
+ /// Whether the plan permits a model; runtime support is checked separately.
+ pub fn allows(self, model: &str) -> bool {
+ self == Self::Paid || model == FREE_MODEL
+ }
+
+ /// Replace an inaccessible persisted/default model when opening a session.
+ pub fn default_model(self, configured: &str) -> &str {
+ if self.allows(configured) {
+ configured
+ } else {
+ FREE_MODEL
+ }
+ }
+
+ /// The supported models this owner may select.
+ pub fn models<'a>(self, supported: &[&'a str]) -> Vec<&'a str> {
+ supported
+ .iter()
+ .copied()
+ .filter(|model| self.allows(model))
+ .collect()
+ }
+}
+
+/// Failure to resolve or enforce model access. Lookup failures fail closed.
+#[derive(Debug, Clone, Copy, thiserror::Error)]
+pub enum ModelAccessError {
+ /// Permissions could not be resolved for a user owner.
+ #[error("unable to load model permissions")]
+ Unavailable,
+ /// The requested model requires a paid plan.
+ #[error("this model requires a paid plan")]
+ Forbidden,
+}
+
+/// Resolves current access at discovery, handshake, model changes, and execution.
+#[async_trait]
+pub trait InMemModelAccess: Send + Sync + 'static {
+ /// Resolve access for a trusted owner, never a client-provided plan.
+ async fn access(&self, owner: &Owner) -> Result<ModelAccess, ModelAccessError>;
+}
+
+/// In-memory model policy backed by the owning permissions domain service.
+pub struct PermissionModelAccess<P> {
+ permissions: P,
+}
+
+impl<P> PermissionModelAccess<P> {
+ /// Construct the policy with the permissions domain service.
+ pub fn new(permissions: P) -> Self {
+ Self { permissions }
+ }
+}
+
+#[async_trait]
+impl<P: UserRolesAndPermissionsService> InMemModelAccess for PermissionModelAccess<P> {
+ async fn access(&self, owner: &Owner) -> Result<ModelAccess, ModelAccessError> {
+ let user = owner.as_user().ok_or(ModelAccessError::Unavailable)?;
+ let permissions = self
+ .permissions
+ .get_user_permissions(user)
+ .await
+ .map_err(|error| {
+ tracing::error!(?error, "unable to load in-memory model permissions");
+ ModelAccessError::Unavailable
+ })?;
+ Ok(
+ if permissions.contains(&PermissionId::ReadProfessionalFeatures) {
+ ModelAccess::Paid
+ } else {
+ ModelAccess::Free
+ },
+ )
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/agent_inmem/src/domain/model_access/test.rs b/crates/agent_inmem/src/domain/model_access/test.rs
new file mode 100644
index 0000000000..582414dd43
--- /dev/null
+++ b/crates/agent_inmem/src/domain/model_access/test.rs
@@ -0,0 +1,90 @@
+use super::*;
+
+#[test]
+fn free_access_is_exact_and_paid_access_preserves_routed_models() {
+ let models = [
+ FREE_MODEL,
+ "anthropic/claude-haiku-4-5",
+ "fireworks/kimi-k3",
+ ];
+ assert_eq!(ModelAccess::Free.models(&models), [FREE_MODEL]);
+ assert_eq!(ModelAccess::Paid.models(&models), models);
+ assert_eq!(ModelAccess::Free.default_model(models[1]), FREE_MODEL);
+ assert_eq!(ModelAccess::Paid.default_model(models[1]), models[1]);
+ assert!(!ModelAccess::Free.allows("gemini-3.8-flash"));
+ assert!(!ModelAccess::Free.allows("unknown/model"));
+}
+
+use macro_user_id::{email::Email, lowercased::Lowercase, user_id::MacroUserIdStr};
+use roles_and_permissions::domain::model::{
+ ProductTier, RoleId, SubscriptionStatus, UserRolesAndPermissionsError,
+};
+use std::collections::HashSet;
+
+#[derive(Clone)]
+struct Permissions(Option<bool>);
+
+impl UserRolesAndPermissionsService for Permissions {
+ async fn get_user_permissions(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ ) -> Result<HashSet<PermissionId>, UserRolesAndPermissionsError> {
+ match self.0 {
+ Some(true) => Ok(HashSet::from([PermissionId::ReadProfessionalFeatures])),
+ Some(false) => Ok(HashSet::new()),
+ None => Err(UserRolesAndPermissionsError::UserDoesNotExist),
+ }
+ }
+ async fn get_user_roles(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ ) -> Result<HashSet<RoleId>, UserRolesAndPermissionsError> {
+ unreachable!()
+ }
+ async fn update_user_roles_and_permissions_for_subscription(
+ &self,
+ _: Email<Lowercase<'_>>,
+ _: SubscriptionStatus,
+ _: ProductTier,
+ ) -> Result<(), UserRolesAndPermissionsError> {
+ unreachable!()
+ }
+ async fn dangerous_upsert_roles_for_user(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ _: non_empty::NonEmpty<&[RoleId]>,
+ ) -> Result<(), UserRolesAndPermissionsError> {
+ unreachable!()
+ }
+ async fn dangerous_remove_roles_from_user(
+ &self,
+ _: &MacroUserIdStr<'_>,
+ _: &non_empty::NonEmpty<&[RoleId]>,
+ ) -> Result<(), UserRolesAndPermissionsError> {
+ unreachable!()
+ }
+}
+
+#[tokio::test]
+async fn permissions_determine_access_and_lookup_errors_fail_closed() {
+ let owner = Owner::User(MacroUserIdStr::try_from_email("[email protected]").unwrap());
+ for (paid, expected) in [(false, ModelAccess::Free), (true, ModelAccess::Paid)] {
+ let policy = PermissionModelAccess::new(Permissions(Some(paid)));
+ assert_eq!(policy.access(&owner).await.unwrap(), expected);
+ }
+ assert!(matches!(
+ PermissionModelAccess::new(Permissions(None))
+ .access(&owner)
+ .await,
+ Err(ModelAccessError::Unavailable)
+ ));
+}
+
+#[tokio::test]
+async fn non_user_owners_cannot_bypass_model_access() {
+ let policy = PermissionModelAccess::new(Permissions(Some(true)));
+ assert!(matches!(
+ policy.access(&Owner::Bot(bot_id::MACRO_NEW_BOT_ID)).await,
+ Err(ModelAccessError::Unavailable)
+ ));
+}
diff --git a/crates/agent_inmem/src/domain/models.rs b/crates/agent_inmem/src/domain/models.rs
index 473199caf9..decc815c42 100644
--- a/crates/agent_inmem/src/domain/models.rs
+++ b/crates/agent_inmem/src/domain/models.rs
@@ -45,7 +45,11 @@ pub fn advertised_models() -> &'static [&'static str] {
.get_or_init(|| {
let mut models = Vec::with_capacity(CHAT_MODELS.len() + ROUTED_MODELS.len());
models.extend_from_slice(CHAT_MODELS);
- models.extend(ROUTED_MODELS.iter().map(|(model, _)| *model));
+ for (model, _) in ROUTED_MODELS {
+ if !models.contains(model) {
+ models.push(model);
+ }
+ }
models
})
.as_slice()
diff --git a/crates/agent_inmem/src/domain/models/test.rs b/crates/agent_inmem/src/domain/models/test.rs
index a4c92cefa9..b0aea8fe2d 100644
--- a/crates/agent_inmem/src/domain/models/test.rs
+++ b/crates/agent_inmem/src/domain/models/test.rs
@@ -4,12 +4,20 @@ use chat::domain::models::CHAT_MODELS;
#[test]
fn advertised_models_keep_chat_then_append_routed() {
let models = advertised_models();
+ assert_eq!(
+ models
+ .iter()
+ .filter(|model| **model == "google/gemini-3.8-flash")
+ .count(),
+ 1
+ );
assert_eq!(&models[..CHAT_MODELS.len()], CHAT_MODELS);
assert_eq!(
&models[CHAT_MODELS.len()..],
ROUTED_MODELS
.iter()
.map(|(model, _)| *model)
+ .filter(|model| !CHAT_MODELS.contains(model))
.collect::<Vec<_>>()
.as_slice()
);
diff --git a/crates/agent_inmem/src/outbound/manager.rs b/crates/agent_inmem/src/outbound/manager.rs
index 9e96b7d9af..475f01ed02 100644
--- a/crates/agent_inmem/src/outbound/manager.rs
+++ b/crates/agent_inmem/src/outbound/manager.rs
@@ -69,6 +69,7 @@ impl Drop for LiveAgent {
pub struct InMemAgentManager {
engine: Arc<dyn TurnEngine>,
admission: Arc<dyn AiAdmissionService>,
+ model_access: Arc<dyn crate::domain::model_access::InMemModelAccess>,
frames: Arc<dyn FrameSource>,
mcp: Arc<dyn DynMcpToolConnector>,
enable_dev_commands: bool,
@@ -91,9 +92,11 @@ impl InMemAgentManager {
engine: Arc<dyn TurnEngine>,
frames: Arc<dyn FrameSource>,
mcp: Arc<dyn DynMcpToolConnector>,
+ model_access: Arc<dyn crate::domain::model_access::InMemModelAccess>,
) -> Self {
Self {
engine,
+ model_access,
admission: Arc::new(DisabledAiAdmissionService),
frames,
mcp,
@@ -176,6 +179,7 @@ impl InMemAgentManager {
owner: facts.owner,
engine: Arc::clone(&self.engine),
admission: Arc::clone(&self.admission),
+ model_access: Arc::clone(&self.model_access),
store: Arc::clone(&self.store),
active_cancel: std::sync::Mutex::new(Vec::new()),
turn_lock: tokio::sync::Mutex::new(()),
diff --git a/crates/agent_inmem/src/outbound/manager/test.rs b/crates/agent_inmem/src/outbound/manager/test.rs
index c3bef63c90..4326abc3ee 100644
--- a/crates/agent_inmem/src/outbound/manager/test.rs
+++ b/crates/agent_inmem/src/outbound/manager/test.rs
@@ -96,6 +96,7 @@ fn manager(repo: &InMemoryAgentSessionRepo, engine: Arc<ScriptedEngine>) -> InMe
engine,
Arc::new(LogFrameSource::new(repo.clone())),
Arc::new(crate::domain::mcp::NoMcpServers),
+ Arc::new(crate::testing::TestModelAccess::paid()),
)
}
@@ -783,6 +784,7 @@ async fn teardown_releases_pooled_mcp_sessions_for_the_egress_token() {
Arc::new(ScriptedEngine::new(Vec::new())),
Arc::new(LogFrameSource::new(repo.clone())),
mcp.clone(),
+ Arc::new(crate::testing::TestModelAccess::paid()),
);
let id = AgentSessionId::new();
let _spawned = manager
diff --git a/crates/agent_inmem/src/testing.rs b/crates/agent_inmem/src/testing.rs
index 26f6b90186..4595b5aa8b 100644
--- a/crates/agent_inmem/src/testing.rs
+++ b/crates/agent_inmem/src/testing.rs
@@ -122,7 +122,11 @@ impl ai_billing::domain::BillingService for UnavailableBilling {
}
/// Models advertised by shared test engines.
-pub(crate) const TEST_MODELS: &[&str] = &["anthropic/claude-sonnet-5-5", "other-model"];
+pub(crate) const TEST_MODELS: &[&str] = &[
+ "anthropic/claude-sonnet-5-5",
+ "other-model",
+ chat::domain::models::FREE_MODEL,
+];
/// An engine that plays back a script of parts for every turn.
pub(crate) struct ScriptedEngine {
@@ -225,3 +229,41 @@ impl TurnEngine for HangingEngine {
receiver
}
}
+
+/// Mutable plan lookup used to exercise upgrades, downgrades, and lookup errors.
+pub(crate) struct TestModelAccess {
+ pub(crate) result: Mutex<
+ Result<
+ crate::domain::model_access::ModelAccess,
+ crate::domain::model_access::ModelAccessError,
+ >,
+ >,
+ pub(crate) owners: Mutex<Vec<model_owner::Owner>>,
+}
+
+impl TestModelAccess {
+ pub(crate) fn new(access: crate::domain::model_access::ModelAccess) -> Self {
+ Self {
+ result: Mutex::new(Ok(access)),
+ owners: Mutex::new(Vec::new()),
+ }
+ }
+
+ pub(crate) fn paid() -> Self {
+ Self::new(crate::domain::model_access::ModelAccess::Paid)
+ }
+}
+
+#[async_trait::async_trait]
+impl crate::domain::model_access::InMemModelAccess for TestModelAccess {
+ async fn access(
+ &self,
+ owner: &model_owner::Owner,
+ ) -> Result<
+ crate::domain::model_access::ModelAccess,
+ crate::domain::model_access::ModelAccessError,
+ > {
+ self.owners.lock().unwrap().push(owner.clone());
+ *self.result.lock().unwrap()
+ }
+}
diff --git a/crates/ai_projections/src/domain/ai_projection_service.rs b/crates/ai_projections/src/domain/ai_projection_service.rs
index 00eeb36f9e..579671ab7f 100644
--- a/crates/ai_projections/src/domain/ai_projection_service.rs
+++ b/crates/ai_projections/src/domain/ai_projection_service.rs
@@ -24,7 +24,7 @@ pub const READ_PROFESSIONAL_FEATURES: &str = "read:professional_features";
/// `provider/model` ids that any user may request, without professional
/// features. Everything else — including an absent model, which resolves to
/// the server default (the smart tier) — requires the permission.
-pub const FREE_TIER_MODELS: &[&str] = &["anthropic/claude-haiku-4-5"];
+pub const FREE_TIER_MODELS: &[&str] = &["google/gemini-3.8-flash"];
/// Whether a requested projection model requires the
/// [`READ_PROFESSIONAL_FEATURES`] permission.
diff --git a/crates/ai_projections/src/domain/ai_projection_service/test.rs b/crates/ai_projections/src/domain/ai_projection_service/test.rs
index 17da5845fc..63ab0dfb9a 100644
--- a/crates/ai_projections/src/domain/ai_projection_service/test.rs
+++ b/crates/ai_projections/src/domain/ai_projection_service/test.rs
@@ -9,6 +9,7 @@ use crate::domain::{
ai_projection_repo::AiProjectionRepository,
ai_projection_service::{
AiProjectionService, AiProjectionServiceImpl, hash_projection_version,
+ requires_professional_features,
},
model::{
AiProjection, AiProjectionError, Expiry, ProjectionStatus, RefreshCadence, TargetType,
@@ -788,3 +789,18 @@ async fn upsert_projection_rejects_empty_id_and_prompt() {
.unwrap_err();
assert!(matches!(err, UpsertProjectionError::BadRequest(_)));
}
+
+#[test]
+fn only_gemini_flash_is_available_without_professional_features() {
+ assert!(!requires_professional_features(Some(
+ "google/gemini-3.8-flash"
+ )));
+ assert!(requires_professional_features(Some(
+ "anthropic/claude-haiku-4-5"
+ )));
+ assert!(requires_professional_features(Some(
+ "anthropic/claude-sonnet-5"
+ )));
+ assert!(requires_professional_features(Some("unknown/model")));
+ assert!(requires_professional_features(None));
+}
diff --git a/crates/ai_projections/src/inbound/axum_router/test.rs b/crates/ai_projections/src/inbound/axum_router/test.rs
index dbe7c3715b..af07a6129c 100644
--- a/crates/ai_projections/src/inbound/axum_router/test.rs
+++ b/crates/ai_projections/src/inbound/axum_router/test.rs
@@ -174,18 +174,18 @@ async fn upsert_projection_forbids_premium_models_without_professional_features(
let app = build_router(false);
let response = app
- .oneshot(post_request_with_model(Some("cerebras/llama-3.3-70b")))
+ .oneshot(post_request_with_model(Some("anthropic/claude-haiku-4-5")))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::FORBIDDEN);
}
#[tokio::test]
-async fn upsert_projection_allows_free_tier_models_without_professional_features() {
+async fn upsert_projection_allows_gemini_flash_without_professional_features() {
let app = build_router(false);
let response = app
- .oneshot(post_request_with_model(Some("anthropic/claude-haiku-4-5")))
+ .oneshot(post_request_with_model(Some("google/gemini-3.8-flash")))
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
diff --git a/crates/chat/src/domain/models/model_access.rs b/crates/chat/src/domain/models/model_access.rs
index 5467953fe8..b869ba1f2f 100644
--- a/crates/chat/src/domain/models/model_access.rs
+++ b/crates/chat/src/domain/models/model_access.rs
@@ -16,10 +16,11 @@ pub const CHAT_MODELS: &[&str] = &[
"openai/gpt-5.6-mini",
"openai/gpt-5.5",
"openai/gpt-5-mini",
+ "google/gemini-3.8-flash",
];
/// The default model for professional (paid) users.
pub const PAID_DEFAULT_MODEL: &str = "anthropic/claude-sonnet-5-5";
/// The only model available to free (non-professional) users.
-pub const FREE_MODEL: &str = "anthropic/claude-haiku-4-5";
+pub const FREE_MODEL: &str = "google/gemini-3.8-flash";
diff --git a/crates/chat/src/domain/service/model_access/test.rs b/crates/chat/src/domain/service/model_access/test.rs
index b9330654fd..8c60eb5cda 100644
--- a/crates/chat/src/domain/service/model_access/test.rs
+++ b/crates/chat/src/domain/service/model_access/test.rs
@@ -3,6 +3,7 @@ use crate::domain::models::CHAT_MODELS;
const SONNET_5_5: &str = "anthropic/claude-sonnet-5-5";
const OPUS_5_5: &str = "anthropic/claude-opus-5-5";
+const GEMINI_FLASH: &str = "google/gemini-3.8-flash";
const HAIKU_4_5: &str = "anthropic/claude-haiku-4-5";
const SONNET_5: &str = "anthropic/claude-sonnet-5";
const OPUS_5: &str = "anthropic/claude-opus-5";
@@ -15,7 +16,10 @@ fn free_user_only_has_the_free_model() {
let svc = ModelAccessServiceImpl;
assert_eq!(svc.best_model(false), FREE_MODEL);
assert!(svc.has_access(false, FREE_MODEL));
- assert!(svc.has_access(false, HAIKU_4_5));
+ assert_eq!(FREE_MODEL, GEMINI_FLASH);
+ assert!(svc.has_access(false, GEMINI_FLASH));
+ assert!(!svc.has_access(false, HAIKU_4_5));
+ assert!(!svc.has_access(false, "unknown/model"));
assert!(!svc.has_access(false, OPUS_5_5));
assert!(!svc.has_access(false, SONNET_5_5));
assert!(!svc.has_access(false, GPT_5_5));
@@ -28,6 +32,7 @@ fn professional_user_has_everything() {
assert!(svc.has_access(true, SONNET_5_5));
assert!(svc.has_access(true, OPUS_5_5));
assert!(svc.has_access(true, HAIKU_4_5));
+ assert!(svc.has_access(true, GEMINI_FLASH));
assert!(svc.has_access(true, GPT_5_5));
assert!(svc.has_access(true, GPT_5_MINI));
}
diff --git a/crates/chat/src/inbound/http/extractors.rs b/crates/chat/src/inbound/http/extractors.rs
index c1b9f65d47..d753a00b68 100644
--- a/crates/chat/src/inbound/http/extractors.rs
+++ b/crates/chat/src/inbound/http/extractors.rs
@@ -33,7 +33,7 @@ impl<P> Clone for UserPermissionsState<P> {
/// Axum extractor resolving the requesting user's model entitlement from their
/// permissions.
///
-/// Free users may use only [`FREE_MODEL`] (Haiku 4.5); professional (paid)
+/// Free users may use only [`FREE_MODEL`] (Gemini Flash); professional (paid)
/// users may use every chat model. Backed by [`ModelAccessServiceImpl`].
///
/// Type parameter `Auth` is the authorization service implementation and `P`
@@ -159,7 +159,7 @@ mod test {
let free = access(&[]);
assert_eq!(free.best_model(), FREE_MODEL);
assert!(free.has_access(FREE_MODEL));
- assert!(free.has_access("anthropic/claude-haiku-4-5"));
+ assert!(!free.has_access("anthropic/claude-haiku-4-5"));
assert!(!free.has_access("anthropic/claude-opus-5-5"));
assert!(!free.has_access("anthropic/claude-sonnet-5-5"));
}
diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.md
index a6f2c7fe73..fe56808b56 100644
--- a/docs/AGENT_GUIDE/ai-chat.md
+++ b/docs/AGENT_GUIDE/ai-chat.md
@@ -721,10 +721,11 @@ Desktop composer and conversation body text use 15px type. Mobile keeps its
existing text sizing.
- Contenteditable composer (placeholder `Ask AI, @mention anything` / `Describe the edit…`).
-- Model picker button showing the current model (e.g. `Haiku 4.5`). Paid plans list
- `Sonnet 5.5`, `Opus 5.5`, `Haiku 4.5`, `GPT-6 Astra`, `GPT-5.6`, `GPT-5.6 mini`;
+- Model picker button showing the current model (e.g. `Gemini 3.8 Flash`). Paid plans list
+ `Sonnet 5.5`, `Opus 5.5`, `Haiku 4.5`, `GPT-6 Astra`, `GPT-5.6`, `GPT-5.6 mini`,
+ and `Gemini 3.8 Flash`;
in dev, heavy models carry a `2.5× usage` / `5× usage` hint.
- On the free plan everything but `Haiku 4.5` is
+ On the free plan everything but `Gemini 3.8 Flash` is
dimmed with a lock and opens the `Smart models are premium` paywall when clicked.
- `Send` button (disabled when empty). While streaming it becomes `Stop generating`.
@@ -1465,3 +1466,19 @@ navigation, so mounting the next surface joins that request instead of warming
another server session.
Empty or failed warm responses do not trigger a refill loop, and mismatched
agent settings leave a usable reservation available for the default agent.
+
+
+### Free-plan models in the new Macro agent
+
+The in-memory Macro runtime uses the session owner's current permissions.
+Free users see only Gemini 3.8 Flash in model discovery and the live session
+picker. New and resumed sessions replace an inaccessible saved model with Gemini.
+A direct request to select a paid model is rejected, and each prompt rechecks
+permissions before inference, including after a plan downgrade. Permission
+lookup failures prevent inference. Paid users retain the full supported catalog.
+Cursor, Claude Cloud, and paired external runtimes keep their own model rules.
+
+To verify, start a Macro conversation as a free user with a previously saved
+paid-model preference: the composer should show Gemini and send that model.
+In a live session, confirm the model options contain only Gemini. Backend tests
+also exercise direct ACP model-change requests, downgrade, and resume.
diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.toml
index f5ccc0b9f4..6434eafc6b 100644
--- a/services/agent_harness_service/Cargo.toml
+++ b/services/agent_harness_service/Cargo.toml
@@ -84,6 +84,7 @@ system_properties = { path = "../../crates/system_properties" }
channels = { path = "../../crates/channels", features = ["ports", "outbound"] }
anyhow = { workspace = true }
macro_user_id = { path = "../../crates/macro_user_id" }
+model_owner = { path = "../../crates/model_owner" }
axum = { workspace = true }
axum-extra = { workspace = true, features = ["typed-header"] }
aws-sdk-secretsmanager = { workspace = true }
diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rs
index a1588290f0..51c24b44d3 100644
--- a/services/agent_harness_service/src/main.rs
+++ b/services/agent_harness_service/src/main.rs
@@ -553,10 +553,24 @@ async fn run() -> anyhow::Result<()> {
// Cold attaches (fresh spawns and post-restart resumes) rebuild
// their model context from the same log every frame lands in.
let frames = Arc::new(LogFrameSource::new(session_repo.clone()));
+ let permissions_repo = roles_and_permissions::outbound::pgpool::MacroDB::new(pool.clone());
+ let inmem_model_access: Arc<dyn agent_inmem::domain::model_access::InMemModelAccess> = Arc::new(
+ agent_inmem::domain::model_access::PermissionModelAccess::new(
+ roles_and_permissions::domain::service::UserRolesAndPermissionsServiceImpl::new(
+ permissions_repo.clone(),
+ permissions_repo,
+ ),
+ ),
+ );
let inmem = InMemRuntime {
- manager: InMemAgentManager::new(Arc::clone(&inmem_model_engine), frames, mcp_connector)
- .with_admission(admission.clone())
- .with_dev_commands(enable_dev_commands),
+ manager: InMemAgentManager::new(
+ Arc::clone(&inmem_model_engine),
+ frames,
+ mcp_connector,
+ Arc::clone(&inmem_model_access),
+ )
+ .with_admission(admission.clone())
+ .with_dev_commands(enable_dev_commands),
};
// The sandbox provider serves every bot but the in-memory one, which the
// router pulls out by bot id before the provider ever sees it.
@@ -1126,6 +1140,7 @@ async fn run() -> anyhow::Result<()> {
InMemoryModels::new(
Some(Arc::clone(&inmem_model_engine)),
config.inmem_model.clone(),
+ Arc::clone(&inmem_model_access),
),
CursorModels::new(cursor_keys.clone(), cursor_api_base_url()),
macrod_models.clone(),
@@ -1138,7 +1153,11 @@ async fn run() -> anyhow::Result<()> {
let model_service = Arc::new(
AgentModelsServiceImpl::new(
VisibleHarnessAccess::new(PgHarnessRepo::new(pool.clone())),
- InMemoryModels::new(Some(inmem_model_engine), config.inmem_model.clone()),
+ InMemoryModels::new(
+ Some(inmem_model_engine),
+ config.inmem_model.clone(),
+ inmem_model_access,
+ ),
CursorModels::new(cursor_keys, cursor_api_base_url()),
macrod_models,
model_probe_timeout,
diff --git a/services/agent_harness_service/src/model_providers/in_memory.rs b/services/agent_harness_service/src/model_providers/in_memory.rs
index aedd031818..1e612e1087 100644
--- a/services/agent_harness_service/src/model_providers/in_memory.rs
+++ b/services/agent_harness_service/src/model_providers/in_memory.rs
@@ -1,6 +1,9 @@
//! In-memory model discovery adapter.
use agent_harness::domain::capability_discovery::{CapabilityProbeError, RawCapabilityProbe};
+use agent_inmem::domain::model_access::InMemModelAccess;
+use macro_user_id::user_id::MacroUserIdStr;
+use model_owner::Owner;
use std::sync::Arc;
use agent_harness::domain::model_load::{InMemoryModelProbe, ModelProbeError, RawModelProbe};
@@ -10,47 +13,69 @@ use agent_inmem::domain::engine::TurnEngine;
pub struct InMemoryModels {
engine: Option<Arc<dyn TurnEngine>>,
current: String,
+ access: Arc<dyn InMemModelAccess>,
}
impl InMemoryModels {
/// Build an adapter over the optional in-memory engine.
- pub fn new(engine: Option<Arc<dyn TurnEngine>>, current: String) -> Self {
- Self { engine, current }
+ pub fn new(
+ engine: Option<Arc<dyn TurnEngine>>,
+ current: String,
+ access: Arc<dyn InMemModelAccess>,
+ ) -> Self {
+ Self {
+ engine,
+ current,
+ access,
+ }
}
}
impl InMemoryModelProbe for InMemoryModels {
- async fn probe(&self) -> Result<RawModelProbe, ModelProbeError> {
+ async fn probe(
+ &self,
+ caller: &MacroUserIdStr<'static>,
+ ) -> Result<RawModelProbe, ModelProbeError> {
let Some(engine) = &self.engine else {
return Ok(RawModelProbe::Unsupported);
};
+ let access = self
+ .access
+ .access(&Owner::User(caller.clone()))
+ .await
+ .map_err(|error| ModelProbeError::Failed(error.to_string()))?;
Ok(RawModelProbe::Options(
agent_inmem::domain::model_options::model_config_options(
- &self.current,
- engine.supported_models(),
+ access.default_model(&self.current),
+ &access.models(engine.supported_models()),
),
))
}
}
impl agent_harness::domain::capability_discovery::CapabilityProbe for InMemoryModels {
- type Target = ();
+ type Target = MacroUserIdStr<'static>;
async fn probe(
&self,
- _: &(),
+ caller: &MacroUserIdStr<'static>,
model: Option<&str>,
) -> Result<RawCapabilityProbe, CapabilityProbeError> {
let Some(engine) = &self.engine else {
return Ok(RawCapabilityProbe::Unsupported);
};
- let current = model.unwrap_or(&self.current);
+ let access = self
+ .access
+ .access(&Owner::User(caller.clone()))
+ .await
+ .map_err(|error| CapabilityProbeError::Failed(error.to_string()))?;
+ let current = access.default_model(model.unwrap_or(&self.current));
if !engine.supported_models().contains(&current) {
return Err(CapabilityProbeError::Failed("unsupported model".to_owned()));
}
Ok(RawCapabilityProbe::Options(
agent_inmem::domain::model_options::model_config_options(
current,
- engine.supported_models(),
+ &access.models(engine.supported_models()),
),
))
}
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main.rs
index d4d51568f6..5820f740ca 100644
--- a/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main.rs
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main.rs
@@ -106,6 +106,7 @@ fn test() -> Job {
.add_step(steps::mount_web_cache_volume(false))
.add_step(steps::setup_nix())
.add_step(steps::setup_reqs_web("Setup", true))
+ .add_step(build_agent_fold_for_tests())
.add_step(run_tests())
.add_step(run_signup_browser_tests())
.add_step(steps::teardown_nix())
@@ -277,6 +278,14 @@ fn run_collaboration_biome() -> Step<Run> {
.working_directory(xtask_paths::repo_dir!("packages/collaboration"))
}
+// Keep cold Rust/WASM compilation outside Vitest’s five-minute setup hook.
+fn build_agent_fold_for_tests() -> Step<Run> {
+ Step::new("Build agent-fold WASM for tests")
+ .run("just build-agent-fold-wasm")
+ .working_directory(xtask_paths::repo_dir!("apps/web"))
+ .timeout_minutes(15u32)
+}
+
fn run_tests() -> Step<Run> {
Step::new("Test")
.run("bunx vitest")
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main/test.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main/test.rs
index ba5f7310ac..51f201764f 100644
--- a/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main/test.rs
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/web_app_check_main/test.rs
@@ -131,7 +131,11 @@ fn test_job_runs_signup_browser_tests_after_vitest() {
.find("name: Cycles Import Check")
.map_or(yaml.len(), |offset| start + offset);
let test_job = &yaml[start..end];
+ let wasm = test_job
+ .find("just build-agent-fold-wasm")
+ .expect("build WASM before the test setup hook");
let vitest = test_job.find("bunx vitest").expect("vitest step");
+ assert!(wasm < vitest, "cold WASM compilation precedes Vitest");
let browser = test_job
.find("just test-signup-browser")
.expect("sign-up browser step");