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.

perf(agents): send a new session's first prompt without a model round trip (#7657)

Wolf Mermelsteindcdf58157b9d
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 ce4c544d68..feb709abf5 100644
--- a/apps/web/src/features/agents-view/views/NewChatPage.test.tsx
+++ b/apps/web/src/features/agents-view/views/NewChatPage.test.tsx
@@ -429,6 +429,7 @@ describe('agent-led new conversation', () => {
prompt: 'Prompt',
botId: undefined,
repoUrl: undefined,
+ modelOverride: 'chat-default',
});
});
it('offers only coding agents in Code and opens the repository drawer', async () => {
diff --git a/apps/web/src/features/block-agent/context/configure-session-model.ts b/apps/web/src/features/block-agent/context/configure-session-model.ts
index 62c6c31ac5..971a93b43a 100644
--- a/apps/web/src/features/block-agent/context/configure-session-model.ts
+++ b/apps/web/src/features/block-agent/context/configure-session-model.ts
@@ -5,6 +5,45 @@ import {
} from '../state/session-config';
import { confirmSessionControl } from './confirm-session-control';
+/**
+ * Resolve once the runtime has reported its configuration. A new session's
+ * fold may be loaded before the `session/new` reply lands, with nothing yet to
+ * compare a model or effort against.
+ */
+export function sessionConfigReported(
+ session: Pick<AgentSession, 'snapshot' | 'subscribe'>
+): Promise<void> {
+ return new Promise<void>((resolve, reject) => {
+ let finished = false;
+ const finish = (error?: Error) => {
+ if (finished) return;
+ finished = true;
+ clearTimeout(timeout);
+ unsubscribe();
+ if (error) reject(error);
+ else resolve();
+ };
+ const inspect = async () => {
+ try {
+ const snapshot = await session.snapshot();
+ if (snapshot.metadata.configOptions.length > 0) finish();
+ } catch (error) {
+ finish(
+ error instanceof Error
+ ? error
+ : new Error('Could not read the session configuration.')
+ );
+ }
+ };
+ const timeout = setTimeout(
+ () => finish(new Error('The agent did not report its configuration.')),
+ 60_000
+ );
+ const unsubscribe = session.subscribe(() => void inspect());
+ void inspect();
+ });
+}
+
/** Validate against the model's confirmed runtime snapshot before setting effort. */
export async function configureSessionModel(
session: Pick<AgentSession, 'issue' | 'snapshot' | 'subscribe'>,
diff --git a/apps/web/src/features/block-agent/context/pending-session.ts b/apps/web/src/features/block-agent/context/pending-session.ts
index 2fe5d364cf..cb0cc3378e 100644
--- a/apps/web/src/features/block-agent/context/pending-session.ts
+++ b/apps/web/src/features/block-agent/context/pending-session.ts
@@ -41,7 +41,10 @@ import type {
import { type Accessor, createSignal } from 'solid-js';
import { v7 as uuidv7 } from 'uuid';
import { issueSessionAction } from '../queries/issue-session-action';
-import { configureSessionModel } from './configure-session-model';
+import {
+ configureSessionModel,
+ sessionConfigReported,
+} from './configure-session-model';
export type PendingSession = {
/** The session's id, once the create has made it real. */
@@ -190,20 +193,17 @@ export function startPendingSession(
// A warm claim releases its server reservation before creation answers.
replenishWarmAgentSession(result.value.session.ownerId);
void refetchSoupEntity(created, 'agentSession', { created: true });
- // Hold the block in preflight while selected settings are confirmed,
- // then adopt the session before issuing the first prompt so that prompt
- // is folded speculatively while its POST is in flight.
- if (
- options.modelOverride ||
- options.effortOverride ||
- prompt ||
- options.attachments?.length
- ) {
+ // The create already starts the session on `modelOverride`, so only
+ // an effort holds the block in preflight. Then adopt the session before
+ // issuing the first prompt so that prompt is folded speculatively while
+ // its POST is in flight.
+ if (options.effortOverride || prompt || options.attachments?.length) {
const session = AgentSession.acquire(created);
try {
- if (options.modelOverride || options.effortOverride) {
+ if (options.effortOverride) {
await session.load();
trace?.stage('loaded');
+ await sessionConfigReported(session);
await configureSessionModel(
session,
options.modelOverride,
diff --git a/apps/web/src/features/block-agent/context/resolve-session-id.test.ts b/apps/web/src/features/block-agent/context/resolve-session-id.test.ts
index fabfb1533c..085a360b34 100644
--- a/apps/web/src/features/block-agent/context/resolve-session-id.test.ts
+++ b/apps/web/src/features/block-agent/context/resolve-session-id.test.ts
@@ -30,6 +30,8 @@ const create = vi.hoisted(() => ({
release: vi.fn(),
attribution: vi.fn(),
confirmedModel: undefined as string | undefined,
+ configReported: true,
+ notify: undefined as (() => void) | undefined,
}));
vi.mock('@service-agent-harness/client', () => ({
@@ -64,6 +66,7 @@ vi.mock('@core/agent-session/AgentSession', () => ({
let requestId: string | undefined;
let outcome: { kind: string; message?: string } = { kind: 'pending' };
let listener: (() => void) | undefined;
+ create.notify = () => listener?.();
create.confirm = (error) => {
outcome = error
? { kind: 'rejected', message: error }
@@ -93,19 +96,21 @@ vi.mock('@core/agent-session/AgentSession', () => ({
: [],
metadata: {
model: create.confirmedModel,
- configOptions: [
- {
- id: 'effort',
- name: 'Effort',
- category: 'thought_level',
- type: 'select',
- currentValue: 'low',
- options: [
- { value: 'low', name: 'Low' },
- { value: 'ultra', name: 'Ultra' },
- ],
- },
- ],
+ configOptions: create.configReported
+ ? [
+ {
+ id: 'effort',
+ name: 'Effort',
+ category: 'thought_level',
+ type: 'select',
+ currentValue: 'low',
+ options: [
+ { value: 'low', name: 'Low' },
+ { value: 'ultra', name: 'Ultra' },
+ ],
+ },
+ ]
+ : [],
},
}),
subscribe: (callback: () => void) => {
@@ -141,6 +146,7 @@ beforeEach(() => {
create.autoConfirm = true;
create.confirm = undefined;
create.confirmedModel = undefined;
+ create.configReported = true;
});
afterEach(() => {
@@ -256,8 +262,8 @@ describe('an id whose create is in flight', () => {
});
});
- // A model chosen before the session exists is sent at creation and confirmed
- // before the first prompt, so that prompt runs on the requested model.
+ // A model chosen before the session exists is sent at creation, which starts
+ // the runtime on it, so the first prompt goes out without a model change.
it('creates the session on the chosen model', async () => {
create.control.mockResolvedValue({
isErr: () => false,
@@ -284,7 +290,6 @@ describe('an id whose create is in flight', () => {
await flush();
expect(create.control.mock.calls).toEqual([
- [placeholder, { type: 'setModel', model: 'model-2' }],
[placeholder, { type: 'prompt', prompt: 'Fix the tests' }],
]);
expect(resolved.sessionId()).toBe(placeholder);
@@ -315,9 +320,7 @@ describe('an id whose create is in flight', () => {
await flush();
await flush();
- expect(create.control.mock.calls).toEqual([
- [placeholder, { type: 'setModel', model: 'model-2' }],
- ]);
+ expect(create.control).not.toHaveBeenCalled();
expect(resolved.initialInput()).toBe('Ask about Offsite ');
expect(resolved.sessionId()).toBe(placeholder);
dispose();
@@ -352,24 +355,27 @@ describe('an id whose create is in flight', () => {
});
});
- it('shows a model failure without sending the prompt on the wrong model', async () => {
+ it('compares an effort against the runtime configuration, not an empty fold', async () => {
+ create.configReported = false;
+ create.confirmedModel = 'model-2';
create.control.mockResolvedValue({
- isErr: () => true,
- error: [{ code: 'HTTP_ERROR', message: 'Model is unavailable.' }],
+ isErr: () => false,
+ value: { actionId: 'prompt', status: 'sent' },
});
const placeholder = startPendingSession({
- modelOverride: 'missing',
+ modelOverride: 'model-2',
+ effortOverride: { configId: 'effort', value: 'low' },
prompt: 'Hello',
});
- await createRoot(async (dispose) => {
- const resolved = resolveSessionId(() => placeholder);
- create.resolve?.('session-model-error');
- await flush();
- expect(resolved.error()).toBe('Model is unavailable.');
- expect(resolved.pending()).toBe(false);
- expect(create.control).toHaveBeenCalledTimes(1);
- dispose();
- });
+ create.resolve?.();
+ await flush();
+ expect(create.control).not.toHaveBeenCalled();
+ create.configReported = true;
+ create.notify?.();
+ await flush();
+ expect(create.control.mock.calls).toEqual([
+ [placeholder, { type: 'prompt', prompt: 'Hello' }],
+ ]);
});
it('waits for model and effort confirmation before sending the first prompt', async () => {
diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rs
index 6993f2be14..0af7c46a35 100644
--- a/crates/agent_harness/src/domain/model.rs
+++ b/crates/agent_harness/src/domain/model.rs
@@ -195,6 +195,14 @@ impl AgentKind {
!matches!(self, Self::InMemory | Self::ClaudeCloud)
}
+ /// Whether this kind's runtime starts on the session row's model by itself.
+ /// The others are sent a model chosen at creation over ACP, before their
+ /// first prompt.
+ #[must_use]
+ pub const fn starts_on_session_model(self) -> bool {
+ matches!(self, Self::InMemory | Self::Cursor | Self::ClaudeCloud)
+ }
+
/// Whether a deployment provisions this kind's runtimes itself.
///
/// Membership is about who provisions, not whether *this* deployment is
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index b0cb90cd39..e3e94866cc 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -4538,6 +4538,45 @@ async fn a_chosen_model_is_the_session_model_from_creation() {
assert_eq!(session.model, "claude-4.5-sonnet-thinking");
}
+/// A sandboxed coder does not read the session row, so a model chosen at
+/// creation reaches it as the first request after `session/new`, and the web
+/// app need not send one of its own.
+#[tokio::test]
+async fn a_chosen_model_is_selected_on_a_runtime_that_does_not_read_the_row() {
+ let (service, _, containers, _, _) = harness();
+ let open = service.open_managed_session(OpenManagedSession {
+ id: None,
+ repo_url: None,
+ repo_branch: None,
+ owner: model_owner::Owner::User(sender()),
+ instructions: None,
+ model: Some("openai/gpt-5.6".to_owned()),
+ prompt: None,
+ profile: None,
+ });
+ let drive = async {
+ let session = containers.first_spawned().await;
+ let container = containers.container(session).unwrap();
+ complete_session_handshake(&container).await;
+ let agent = container.agent();
+ tokio::time::timeout(
+ std::time::Duration::from_secs(5),
+ agent.wait_for_requests(3),
+ )
+ .await
+ .expect("the runtime was never sent the chosen model");
+ agent.received_requests()
+ };
+ let (opened, requests) = tokio::join!(open, drive);
+ opened.unwrap();
+ let ClientRequest::SetSessionConfigOptionRequest(request) = &requests[2] else {
+ panic!("expected a model selection, got {:?}", requests[2]);
+ };
+ let request = serde_json::to_value(request).unwrap();
+ assert_eq!(request["configId"], "model");
+ assert_eq!(request["value"], "openai/gpt-5.6");
+}
+
/// The agents-view create path names the Cursor bot with no persisted
/// profile. The deployment default harness is the sandboxed coder's
/// `opencode`; Cursor sessions must not inherit it.
diff --git a/crates/agent_harness/src/domain/service/warm.rs b/crates/agent_harness/src/domain/service/warm.rs
index 7a5da22e5b..0c521020f9 100644
--- a/crates/agent_harness/src/domain/service/warm.rs
+++ b/crates/agent_harness/src/domain/service/warm.rs
@@ -94,7 +94,8 @@ where
};
// A caller's pick outranks the persona's: choosing a model on the way
// in is choosing what this session runs on, for its whole life.
- let model = request.model.unwrap_or(model);
+ let requested_model = request.model;
+ let model = requested_model.clone().unwrap_or(model);
let kind = AgentKind::for_session(bot_id, &harness);
let harness = kind.harness_slug().map_or(harness, str::to_owned);
tracing::Span::current().record("agent.harness", &harness);
@@ -295,14 +296,16 @@ where
tracing::info_span!("agent.init.permissions", agent.session.id = %session_id),
)
.await;
+ let attachment = container
+ .mcp_servers(mcp_servers.clone())
+ .permission_policy(permission_policy);
+ let attachment = match requested_model {
+ Some(model) if !kind.starts_on_session_model() => attachment.initial_model(model),
+ _ => attachment,
+ };
self.inner
.sessions
- .attach_session(
- session.id,
- container
- .mcp_servers(mcp_servers.clone())
- .permission_policy(permission_policy),
- )
+ .attach_session(session.id, attachment)
.instrument(tracing::info_span!("agent.init.attach", agent.session.id = %session_id))
.await?;
diff --git a/docs/AGENT_EFFORT.md b/docs/AGENT_EFFORT.md
index 37f678acde..56547fd7e6 100644
--- a/docs/AGENT_EFFORT.md
+++ b/docs/AGENT_EFFORT.md
@@ -49,8 +49,11 @@ Sources checked September 21, 2026:
- Preflight capability cache identity includes the harness and model. A selection
from another target is discarded. External harnesses expose controls after
opening a session; model-specific preflight is not guessed.
-- Startup waits for the correlated ACP acceptance of the model, checks the
- resulting effort choices, then confirms effort before sending the prompt.
+- A model chosen at creation is the session's model from its row. In-memory,
+ Cursor, and Claude cloud runtimes start on it; the harness selects it over ACP
+ for the others and holds the first prompt until the runtime accepts it.
+- An effort chosen at creation waits for the runtime's reported configuration,
+ checks the effort choices, then confirms effort before sending the prompt.
HTTP queue acceptance alone is insufficient. Rejection or timeout stops startup.
- New/resumed sessions and setting changes return the complete configuration.
Clients replace their previous options, including removing unsupported controls.
diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.md
index c5deedb701..2d1775996a 100644
--- a/docs/AGENT_GUIDE/ai-chat.md
+++ b/docs/AGENT_GUIDE/ai-chat.md
@@ -12,8 +12,10 @@ Repeat with the default model and with an explicit model/effort selection.
The first visible output and the first answer text should appear as they arrive;
later chunks can arrive in batches. Reasoning or a tool row should not delay the
first prose. Verify a second prompt and a reload preserve the complete answer.
-Selected model and effort must be confirmed before the first prompt; settings
-already confirmed by the runtime do not need another control request.
+A model selected before the first prompt is part of the create request, and the
+runtime starts on it; no model change appears in the new session's transcript.
+A selected effort is confirmed before the first prompt; settings the runtime
+already reports do not need another control request.
Repeat from a fresh tab using Home, Agents, and a document's Chat action.
Focusing an agent composer prepares its transcript renderer locally; focus alone