Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(route): ask the backend which agent a task belongs to

0xdiego25207402b8ed44
diff --git a/cli/src/cable/cableHost.ts b/cli/src/cable/cableHost.ts
index 4998df852..a3630f570 100644
--- a/cli/src/cable/cableHost.ts
+++ b/cli/src/cable/cableHost.ts
@@ -682,8 +682,15 @@ export class DaemonCableHost implements CableHost {
// THE QUESTION, not the answer. A recap summarises what the agent replied — "1945." is a correct
// recap and a useless routing signal, and the next question about the same conversation matches
- // nothing in it. The ask carries the topic, so it wins when there is one; the recap stands in for
- // turns recorded before this was stored, and for agents on other machines.
+ // nothing in it.
+ //
+ // TWO FIELDS, because two routers read this. The backend is sent `prompts` and is told plainly
+ // when an agent has none; the local ladder below it still reads `recentSummary`, where a recap
+ // standing in for a missing ask is the best it has. Mixing the two into one field is what let a
+ // summary of the agent's own replies reach a prompt that called them the person's questions.
+ prompts: (await this.recentSummaries(a.id))
+ .map((r) => (r.ask || '').replace(/\s+/g, ' ').trim())
+ .filter(Boolean),
recentSummary: (await this.recentSummaries(a.id))
.map((r) => r.ask || r.recap || r.text || '')
.filter(Boolean)
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 4e99c1bda..9ced8d4f5 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -209,8 +209,6 @@ let cableHostRef: DaemonCableHost | null = null
* Fifteen is the owner's number; the ordering that decides WHICH fifteen is in onRouteTask.
*/
const ROUTE_MAX_CANDIDATES = 15
-/** Each recent turn, cut to this before it enters the classifier's prompt. See onRouteTask. */
-const ROUTE_RECAP_CHARS = 60
/** What ⌘K gives the classifier before the name matcher answers instead. */
const ROUTE_CLASSIFY_APP_MS = 20_000
/**
@@ -2945,24 +2943,24 @@ async function runForeground(session: AuthSession): Promise<void> {
name: agent.name,
engine: agent.engine,
machine: agent.machine,
- // EACH TURN CUT TO 60 CHARS before it goes anywhere near the prompt.
+ // THE PERSON'S OWN QUESTIONS, AND NOTHING ELSE.
//
- // Nothing trimmed these: the 120-char cut further down is for the PICKER, which the person reads,
- // and the classifier was being handed three recaps at full length per agent. That is what turned a
- // list of ten into a 12-second timeout while eight had answered in nine — the agents cost little,
- // their recent work cost a lot. Sixty characters still separates "webhook retry fix" from
- // "round 3 standings", which is all the router is being asked to tell apart.
- // THE QUESTION FIRST, and the recap only when there is no question on record.
+ // This used to be `turn.ask || turn.recap || turn.text`, cut to sixty characters and joined
+ // into one blob — under a prompt heading that told the model every word of it was something
+ // the person had asked. For any agent with no recorded question that was false: it was a
+ // summary of what the AGENT REPLIED. Measured on this desk, "which year did the second world
+ // war end" summarised to "1945." — an answer, labelled as a question, handed to a model asked
+ // to recognise a topic. An agent with nothing on record now sends an empty list and is
+ // described honestly in the prompt.
//
- // A recap describes what the AGENT REPLIED. Measured on this desk: "which year did the second
- // world war end" recapped to "1945." — correct, and carrying not one word the next question
- // about the same conversation could match. The router was being handed answers and asked to
- // recognise topics. The ask is the topic, costs the same sixty characters, and needs no extra
- // room in a prompt that already times out often enough to matter.
- recentSummary: (await host.recentSummaries(agent.id))
- .map((turn) => (turn.ask || turn.recap || turn.text || '').replace(/\s+/g, ' ').trim().slice(0, ROUTE_RECAP_CHARS))
- .filter(Boolean)
- .join(' · '),
+ // UNCUT, too. Sixty characters was chosen when fifteen agents each carried three recaps at
+ // full length and the prompt timed out; a real machine has four to eight agents, and cutting
+ // a Vietnamese sentence at sixty takes the object with it — which is the topic. The bound that
+ // matters now lives at the two ends: ASK_MAX_CHARS where the question is recorded, and the
+ // endpoint's own per-prompt ceiling.
+ prompts: (await host.recentSummaries(agent.id))
+ .map((turn) => (turn.ask || '').replace(/\s+/g, ' ').trim())
+ .filter(Boolean),
})))
// 20s, not the shared 12s: this path answers a person watching a spinner in their own window, and
// it is under nobody else's deadline — the app's rpc waits longer still. The dial and the web keep
diff --git a/cli/src/device/deviceFleet.ts b/cli/src/device/deviceFleet.ts
index 282c6af8b..bb389f102 100644
--- a/cli/src/device/deviceFleet.ts
+++ b/cli/src/device/deviceFleet.ts
@@ -245,7 +245,15 @@ export class DeviceFleet implements MachineFleet {
const events = Array.isArray(res.events) ? res.events : []
const turns = events.map((e) => {
const r = (e ?? {}) as Record<string, unknown>
- return { recap: typeof r.recap === 'string' ? r.recap : '', text: typeof r.text === 'string' ? r.text : '' }
+ // `ask` travels too. The wire has always carried it — commander.recent() puts it on every
+ // turn — and dropping it here left every agent on a REMOTE machine with no questions on
+ // record, so the router ranked them on their name alone while local agents were ranked on
+ // what the person had actually said to them.
+ return {
+ recap: typeof r.recap === 'string' ? r.recap : '',
+ text: typeof r.text === 'string' ? r.text : '',
+ ask: typeof r.ask === 'string' ? r.ask : '',
+ }
})
this.recapCache.set(key, turns)
diff --git a/cli/src/lib/commander.ts b/cli/src/lib/commander.ts
index 124b64c77..6311e27a8 100644
--- a/cli/src/lib/commander.ts
+++ b/cli/src/lib/commander.ts
@@ -209,9 +209,21 @@ const RECENT_TURNS = 3
*/
const FULL_TEXT_MAX_BYTES = 8192
-/** How much of the user's own words is kept per turn. Long enough for the topic, short enough that three
- * of them per agent still fit a router prompt that already carries fifteen agents. */
-const ASK_MAX_CHARS = 200
+/**
+ * How much of the user's own words is kept per turn.
+ *
+ * Raised 200 → 1000 when the router stopped cutting these again on the way out. Two hundred was set
+ * against a worst case that does not happen — fifteen agents each carrying three questions — while a
+ * real machine has four to eight, and the cost of the old number was paid on every long question by
+ * losing its second half.
+ *
+ * Still bounded, and the bound is not about prompt size: somebody pasting a stack trace or a file at
+ * an agent must not put the whole thing on disk here and then into a routing prompt.
+ *
+ * Only affects questions recorded from now on — `summaries-asks.json` keeps what it already holds at
+ * the old length.
+ */
+const ASK_MAX_CHARS = 1000
/**
* Truncate to a byte budget without splitting a character.
diff --git a/cli/src/lib/routeApi.spec.ts b/cli/src/lib/routeApi.spec.ts
new file mode 100644
index 000000000..2133e665a
--- /dev/null
+++ b/cli/src/lib/routeApi.spec.ts
@@ -0,0 +1,158 @@
+// The backend router client.
+//
+// Two things are pinned hard here. First, that EVERY failure comes back as null rather than throwing:
+// this sits in front of a fallback ladder, and a router that can throw turns a backend hiccup into a
+// lost turn. Second, that a 404 is one of those failures — the CLI self-updates from GCS while the
+// backend ships on its own tags, so a machine will hold this build before the endpoint exists, and
+// that machine must route exactly as it did yesterday.
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
+
+const readAuthSession = vi.hoisted(() => vi.fn())
+const accessToken = vi.hoisted(() => vi.fn())
+vi.mock('./authSession.js', () => ({
+ readAuthSession,
+ AuthSessionManager: class {
+ accessToken = accessToken
+ },
+}))
+
+import { rankAgents, routeApiBase, ROUTE_API_PATH } from './routeApi.js'
+
+const agents = [
+ { id: 'a1', name: 'Phones', prompts: ['iphone 17 gia bao nhieu'] },
+ { id: 'a2', name: 'Frontend', prompts: [] },
+]
+
+const ask = (over: Partial<Parameters<typeof rankAgents>[0]> = {}) =>
+ rankAgents({ task: 'so sanh iphone vs samsung', agents, budgetMs: 12_000, ...over })
+
+/** One backend reply in the `{success, data}` envelope every route uses. */
+const replies = (ranking: unknown, status = 200) =>
+ vi.fn(async (_url: string, _init?: RequestInit) =>
+ new Response(JSON.stringify({ success: status === 200, data: { ranking } }), {
+ status,
+ headers: { 'content-type': 'application/json' },
+ }))
+
+/** The RequestInit of the nth fetch this test made. */
+const sent = (spy: ReturnType<typeof replies>, at = 0): RequestInit => spy.mock.calls[at][1] as RequestInit
+
+describe('rankAgents', () => {
+ beforeEach(() => {
+ readAuthSession.mockReturnValue({ accessToken: 'raw', autonomousEnv: 'prod', computerId: 'c1' })
+ accessToken.mockResolvedValue('fresh-token')
+ })
+ afterEach(() => vi.restoreAllMocks())
+
+ it('returns the ranking', async () => {
+ vi.stubGlobal('fetch', replies([
+ { agentId: 'a1', score: 0.91, reason: 'phones' },
+ { agentId: 'a2', score: 0.12, reason: 'no' },
+ ]))
+ expect(await ask()).toEqual([
+ { agentId: 'a1', score: 0.91, reason: 'phones' },
+ { agentId: 'a2', score: 0.12, reason: 'no' },
+ ])
+ })
+
+ it('sends the REFRESHED token, not the one on disk', async () => {
+ // A daemon that has been up for a week is holding an expired access token in its session file.
+ const spy = replies([{ agentId: 'a1', score: 1 }])
+ vi.stubGlobal('fetch', spy)
+ await ask()
+ const headers = sent(spy).headers as Record<string, string>
+ expect(headers.authorization).toBe('Bearer fresh-token')
+ expect(headers['x-autonomous-env']).toBe('prod')
+ })
+
+ it('sends the prompts as sent, empty list included', async () => {
+ const spy = replies([{ agentId: 'a1', score: 1 }])
+ vi.stubGlobal('fetch', spy)
+ await ask()
+ const body = JSON.parse(sent(spy).body as string)
+ expect(body.agents).toEqual(agents)
+ expect(body.task).toBe('so sanh iphone vs samsung')
+ })
+
+ it('asks the backend for LESS time than the caller has', async () => {
+ // Equal deadlines make it a coin toss which fires first, and if the caller's wins the fallback
+ // ladder never runs at all.
+ const spy = replies([{ agentId: 'a1', score: 1 }])
+ vi.stubGlobal('fetch', spy)
+ await ask({ budgetMs: 12_000 })
+ const body = JSON.parse(sent(spy).body as string)
+ expect(body.budgetMs).toBeLessThan(12_000)
+ })
+
+ it('passes continuity only when it was given', async () => {
+ const spy = replies([{ agentId: 'a1', score: 1 }])
+ vi.stubGlobal('fetch', spy)
+ await ask({ continuity: { agentId: 'a2', agoMs: 4_000 } })
+ expect(JSON.parse(sent(spy).body as string).continuity)
+ .toEqual({ agentId: 'a2', agoMs: 4_000 })
+ vi.stubGlobal('fetch', spy)
+ await ask()
+ expect(JSON.parse(sent(spy, 1).body as string).continuity).toBeUndefined()
+ })
+
+ it('is null on 404 — the endpoint may simply not be deployed yet', async () => {
+ vi.stubGlobal('fetch', replies(null, 404))
+ expect(await ask()).toBeNull()
+ })
+
+ it('is null on 500', async () => {
+ vi.stubGlobal('fetch', replies(null, 500))
+ expect(await ask()).toBeNull()
+ })
+
+ it('is null when the machine is offline, and does not throw', async () => {
+ vi.stubGlobal('fetch', vi.fn(async () => { throw new Error('ENOTFOUND') }))
+ await expect(ask()).resolves.toBeNull()
+ })
+
+ it('is null when nobody is signed in', async () => {
+ readAuthSession.mockReturnValue(null)
+ const spy = replies([{ agentId: 'a1', score: 1 }])
+ vi.stubGlobal('fetch', spy)
+ expect(await ask()).toBeNull()
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('is null when the token cannot be refreshed', async () => {
+ accessToken.mockRejectedValue(new Error('refresh refused'))
+ vi.stubGlobal('fetch', replies([{ agentId: 'a1', score: 1 }]))
+ await expect(ask()).resolves.toBeNull()
+ })
+
+ it('drops a row naming an agent this daemon does not have', async () => {
+ // The endpoint filters too, but THIS process is the one that will dispatch on the answer.
+ vi.stubGlobal('fetch', replies([
+ { agentId: 'ghost', score: 0.99 },
+ { agentId: 'a2', score: 0.4 },
+ ]))
+ expect(await ask()).toEqual([{ agentId: 'a2', score: 0.4, reason: '' }])
+ })
+
+ it('is null when every row was dropped', async () => {
+ vi.stubGlobal('fetch', replies([{ agentId: 'ghost', score: 0.99 }]))
+ expect(await ask()).toBeNull()
+ })
+
+ it('is null when the body is not a ranking at all', async () => {
+ vi.stubGlobal('fetch', vi.fn(async () => new Response('not json', { status: 200 })))
+ expect(await ask()).toBeNull()
+ })
+
+ it('clamps a score outside 0..1 rather than trusting it', async () => {
+ vi.stubGlobal('fetch', replies([{ agentId: 'a1', score: 7 }]))
+ expect((await ask())?.[0].score).toBe(1)
+ })
+})
+
+describe('routeApiBase', () => {
+ it('is the websocket host over https', () => {
+ expect(routeApiBase()).toMatch(/^https?:\/\//)
+ expect(routeApiBase()).not.toMatch(/\/$/)
+ })
+ it('names the agreed path', () => expect(ROUTE_API_PATH).toBe('/api/route/agents'))
+})
diff --git a/cli/src/lib/routeApi.ts b/cli/src/lib/routeApi.ts
new file mode 100644
index 000000000..ae641e6cf
--- /dev/null
+++ b/cli/src/lib/routeApi.ts
@@ -0,0 +1,151 @@
+/**
+ * Ask the backend which agent a task belongs to.
+ *
+ * WHY THE BACKEND. The router's prompt used to be built here, which made every wording change a
+ * release: bump, build, upload to GCS, then wait for each machine to self-update. A prompt that is
+ * still being corrected cannot converge on that loop, and this one is still being corrected — the
+ * owner's complaint that started this work was that it suggests the wrong agent too often. On the
+ * backend it changes with a deploy.
+ *
+ * WHAT IS SENT. The task, and for each agent its NAME and the person's own last three questions to
+ * it. Nothing else: no recap of what the agent replied, no machine name, no engine. The old prompt
+ * carried a summary of the AGENT'S ANSWERS under a heading claiming they were the person's questions,
+ * which is the single most likely reason it misrouted with confidence.
+ *
+ * WHAT COMES BACK. A RANKING — up to five agents, each with a score on one absolute scale — not a
+ * winner plus some runners-up. The pick is simply its first row.
+ *
+ * NULL, NEVER A THROW. Every failure here (no session, no network, a 5xx, an unusable body) means the
+ * same thing to the caller: this route did not answer, use the ladder below it. A router that can
+ * throw would turn a backend hiccup into a lost turn.
+ */
+import { env } from '../config/env.js'
+import { readAuthSession, AuthSessionManager } from './authSession.js'
+
+/** One row of the answer. */
+export interface RankedAgent {
+ agentId: string
+ /** 0..1 on the endpoint's absolute scale — comparable across rows and against the app's threshold. */
+ score: number
+ reason: string
+}
+
+/** What the endpoint is told about one agent. */
+export interface RouteApiAgent {
+ id: string
+ name: string
+ /** The person's own last questions to this agent, newest first. EMPTY for a fresh agent. */
+ prompts: string[]
+}
+
+export interface RouteApiOptions {
+ task: string
+ agents: RouteApiAgent[]
+ /**
+ * How long the CALLER can wait. Passed through so the endpoint never outlives the person watching:
+ * a spoken task has 12s and a typed one 20s, and an answer that arrives after the caller gave up is
+ * the same as no answer while still costing a model call.
+ */
+ budgetMs: number
+ /** Who the previous task went to, and how long ago. Omitted when there was none, or it is stale. */
+ continuity?: { agentId: string; agoMs: number }
+ signal?: AbortSignal
+}
+
+export const ROUTE_API_PATH = '/api/route/agents'
+
+/** Same derivation `backendHttpBase()` uses in cli.ts — the WS host over https. */
+export function routeApiBase(): string {
+ return env.BACKEND_WS_URL.replace(/\/$/, '').replace(/^wss:/, 'https:').replace(/^ws:/, 'http:')
+}
+
+/**
+ * A small margin so the HTTP call gives up before the caller's own clock does.
+ *
+ * Without it the two deadlines are equal and which one fires first is a coin toss — and if the
+ * caller's wins, the fallback ladder never runs at all.
+ */
+const ROUND_TRIP_MARGIN_MS = 1_500
+
+/**
+ * The ranking, or null when this route could not produce one.
+ *
+ * The daemon signs in as a PERSON — `~/.harness/auth/session.json` holds an SSO access token, and it
+ * holds no machine api key at all — so this presents the same Bearer credential the control-plane
+ * calls in cli.ts do, refreshed through AuthSessionManager rather than read raw: a daemon that has
+ * been up for a week is holding an expired token otherwise.
+ */
+export async function rankAgents(options: RouteApiOptions): Promise<RankedAgent[] | null> {
+ const session = readAuthSession()
+ if (!session) return null
+
+ let accessToken: string
+ try {
+ accessToken = await new AuthSessionManager(routeApiBase()).accessToken()
+ } catch {
+ // Signed out, or the refresh was refused. Not this router's problem to report.
+ return null
+ }
+
+ const budget = Math.max(1_000, options.budgetMs - ROUND_TRIP_MARGIN_MS)
+ const controller = new AbortController()
+ const timer = setTimeout(() => controller.abort(), options.budgetMs)
+ const onCallerAbort = () => controller.abort()
+ options.signal?.addEventListener('abort', onCallerAbort, { once: true })
+
+ try {
+ const res = await fetch(`${routeApiBase()}${ROUTE_API_PATH}`, {
+ method: 'POST',
+ headers: {
+ 'content-type': 'application/json',
+ authorization: `Bearer ${accessToken}`,
+ 'x-autonomous-env': session.autonomousEnv,
+ },
+ body: JSON.stringify({
+ task: options.task,
+ agents: options.agents,
+ budgetMs: budget,
+ ...(options.continuity ? { continuity: options.continuity } : {}),
+ }),
+ signal: controller.signal,
+ })
+ if (!res.ok) {
+ // 404 is the ROLLOUT case and must stay quiet-ish: the CLI self-updates from GCS while the
+ // backend ships on its own tags, so a machine can hold this build for days before the endpoint
+ // exists. It falls through to the ladder exactly like any other failure.
+ console.log(`[voice-route] backend said ${res.status} → falling through`)
+ return null
+ }
+ const payload = (await res.json()) as { data?: { ranking?: unknown } } | null
+ const rows = payload?.data?.ranking
+ if (!Array.isArray(rows)) return null
+
+ // Validated again on the way in. The endpoint already filters against the ids it was offered, but
+ // this process is the one that will DISPATCH on the answer, and a row naming an agent this daemon
+ // does not have would send a person's words nowhere.
+ const known = new Set(options.agents.map((agent) => agent.id))
+ const seen = new Set<string>()
+ const ranking: RankedAgent[] = []
+ for (const row of rows) {
+ if (!row || typeof row !== 'object') continue
+ const entry = row as Record<string, unknown>
+ const agentId = typeof entry.agentId === 'string' ? entry.agentId : ''
+ if (!agentId || !known.has(agentId) || seen.has(agentId)) continue
+ const score = typeof entry.score === 'number' ? entry.score : Number(entry.score)
+ if (!Number.isFinite(score)) continue
+ seen.add(agentId)
+ ranking.push({
+ agentId,
+ score: Math.max(0, Math.min(1, score)),
+ reason: typeof entry.reason === 'string' ? entry.reason.slice(0, 120) : '',
+ })
+ }
+ return ranking.length > 0 ? ranking : null
+ } catch {
+ // A timeout, an offline machine, a body that is not JSON. All the same answer.
+ return null
+ } finally {
+ clearTimeout(timer)
+ options.signal?.removeEventListener('abort', onCallerAbort)
+ }
+}
diff --git a/cli/src/lib/voiceRouter.spec.ts b/cli/src/lib/voiceRouter.spec.ts
index 4331b41e3..6adf89442 100644
--- a/cli/src/lib/voiceRouter.spec.ts
+++ b/cli/src/lib/voiceRouter.spec.ts
@@ -23,6 +23,14 @@ vi.mock('./openrouter.js', () => ({
openRouterComplete: (...args: unknown[]) => openRouterComplete(...args),
}))
+// The backend router: it answers first when it can, so every existing test below depends on this
+// mock returning null — which is exactly what a machine with no session, or an undeployed endpoint,
+// does in the field.
+type RankArgs = Parameters<typeof import('./routeApi.js').rankAgents>[0]
+type RankResult = Awaited<ReturnType<typeof import('./routeApi.js').rankAgents>>
+const rankAgents = vi.fn(async (_options: RankArgs): Promise<RankResult> => null)
+vi.mock('./routeApi.js', () => ({ rankAgents: (options: RankArgs) => rankAgents(options) }))
+
// Import AFTER the mock is registered.
const { buildRouterPrompt, parseRouteOutput, routeVoiceTask, pickAgentHeuristic, chooseRouterEngine, routerModelFor, ROUTE_SCORED_ROWS, CONTINUITY_WINDOW_MS } = await import('./voiceRouter.js')
type RouterAgent = import('./voiceRouter.js').RouterAgent
@@ -232,7 +240,52 @@ describe('buildRouterPrompt', () => {
})
describe('routeVoiceTask', () => {
- beforeEach(() => { runRouterOneShot.mockReset(); runGrokOneShot.mockReset(); routerImpl = async () => ({ text: '', sessionId: null }) })
+ beforeEach(() => {
+ runRouterOneShot.mockReset(); runGrokOneShot.mockReset()
+ rankAgents.mockReset(); rankAgents.mockResolvedValue(null)
+ routerImpl = async () => ({ text: '', sessionId: null })
+ })
+
+ it('takes the backend ranking when there is one, and asks no engine', async () => {
+ rankAgents.mockResolvedValue([
+ { agentId: '2', score: 0.92, reason: 'auth' },
+ { agentId: '1', score: 0.31, reason: 'frontend' },
+ ] as never)
+ const d = await routeVoiceTask('refresh tokens are expiring early', AGENTS)
+ expect(d).toMatchObject({ agentId: '2', confidence: 0.92, reason: 'auth', via: 'backend' })
+ // The ranking IS the answer: the pick is its first row and the rest become the picker's scores.
+ expect(d.scores).toEqual([{ agentId: '1', confidence: 0.31 }])
+ expect(runRouterOneShot).not.toHaveBeenCalled()
+ expect(openRouterComplete).not.toHaveBeenCalled()
+ })
+
+ it('sends each agent its own questions, and an empty list for one with none', async () => {
+ rankAgents.mockResolvedValue([{ agentId: '1', score: 0.9, reason: '' }] as never)
+ await routeVoiceTask('dark mode', [
+ { id: '1', name: 'Frontend', prompts: ['make the settings page dark'] },
+ { id: '2', name: 'Fresh' },
+ ])
+ expect(rankAgents.mock.calls[0][0]).toMatchObject({
+ agents: [
+ { id: '1', name: 'Frontend', prompts: ['make the settings page dark'] },
+ { id: '2', name: 'Fresh', prompts: [] },
+ ],
+ })
+ })
+
+ it('falls through to the ladder when the backend cannot answer', async () => {
+ // The rollout case: this build reaches a machine before the endpoint is deployed.
+ rankAgents.mockResolvedValue(null)
+ routerImpl = async () => ({ text: '{"agentId":"2","confidence":0.88,"reason":"auth"}', sessionId: null })
+ const d = await routeVoiceTask('jwt refresh', AGENTS)
+ expect(d).toMatchObject({ agentId: '2', confidence: 0.88 })
+ expect(runRouterOneShot).toHaveBeenCalled()
+ })
+
+ it('does not ask the backend at all for a single agent', async () => {
+ await routeVoiceTask('anything', [{ id: '7', name: 'Solo' }])
+ expect(rankAgents).not.toHaveBeenCalled()
+ })
it('returns needNewAgent for an empty machine WITHOUT calling the LLM', async () => {
const d = await routeVoiceTask('anything', [])
diff --git a/cli/src/lib/voiceRouter.ts b/cli/src/lib/voiceRouter.ts
index 90691faf7..9f1eded4a 100644
--- a/cli/src/lib/voiceRouter.ts
+++ b/cli/src/lib/voiceRouter.ts
@@ -8,6 +8,7 @@ import { join } from 'path'
import { env } from '../config/env.js'
import { runGrokOneShot, runRouterOneShot, configureRouterOneShot, setRouterOneShotDeviceConnected, shutdownRouterOneShot } from './oneshot.js'
import { openRouterComplete, resolveOpenRouterKey } from './openrouter.js'
+import { rankAgents } from './routeApi.js'
export interface RouterAgent {
id: string
@@ -18,8 +19,15 @@ export interface RouterAgent {
/** 'ori' when this agent's CLI is pointed at OpenRouter — see the direct-call path in routeVoiceTask. */
gateway?: 'ori' | null
/** Recaps of the agent's last few completed turns (up to 3), joined — a broader, less skewed picture
- * of what it's working on than a single turn. */
+ * of what it's working on than a single turn.
+ *
+ * ONLY the local fallback ladder reads this now. The backend router is sent [prompts] instead: a
+ * recap describes what the AGENT REPLIED, and the old prompt presented those replies under a
+ * heading claiming they were the person's questions. */
recentSummary?: string
+ /** The person's OWN last questions to this agent, newest first, uncut. Empty for a fresh agent —
+ * and empty is sent as empty, never backfilled with a recap of the agent's answers. */
+ prompts?: string[]
/** The machine it runs on. The candidate list spans every machine the owner has, so this is the only
* thing that separates two agents with the same name on two computers — and it is how a spoken "the
* one on the mac mini" can be answered at all. */
@@ -394,6 +402,31 @@ export async function routeVoiceTask(
return logDecision('only-agent', agents, { agentId: agents[0].id, confidence: 1, reason: 'only agent in machine', needNewAgent: false })
}
+ // THE BACKEND FIRST — it owns the prompt now, so a wording fix ships with a deploy instead of with a
+ // CLI release that every machine has to self-update into. It is also the only path that works on a
+ // machine with no model credential of its own, which is most of them: those used to fall straight
+ // through to name matching, whose confidence is capped at 0.4 and can therefore never dispatch.
+ //
+ // Everything below stays as the ladder beneath it. A backend that is down, or too old to have this
+ // endpoint, must not cost anybody a turn.
+ const ranked = await rankAgents({
+ task: transcript,
+ agents: agents.map((agent) => ({ id: agent.id, name: agent.name, prompts: agent.prompts ?? [] })),
+ budgetMs: timeoutMs,
+ ...(continuity && continuity.agoMs <= CONTINUITY_WINDOW_MS ? { continuity } : {}),
+ ...(signal ? { signal } : {}),
+ })
+ if (ranked && ranked.length > 0) {
+ const [top, ...rest] = ranked
+ return logDecision('backend', agents, {
+ agentId: top.agentId,
+ confidence: top.score,
+ reason: top.reason,
+ needNewAgent: false,
+ scores: rest.slice(0, ROUTE_SCORED_ROWS - 1).map((row) => ({ agentId: row.agentId, confidence: row.score })),
+ })
+ }
+
const prompt = buildRouterPrompt(transcript, agents, continuity)
// ONE DIRECT API CALL WHENEVER THERE IS A KEY TO MAKE IT WITH — not only on machines running gateway