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.

harnessd: the models service answers the apps' models requests (#776)

Dee782639e57449
diff --git a/cli/e2e/services.e2e.ts b/cli/e2e/services.e2e.ts
index 76efef685..98fa9c991 100644
--- a/cli/e2e/services.e2e.ts
+++ b/cli/e2e/services.e2e.ts
@@ -53,6 +53,12 @@ describe('a failing service never takes the core down', () => {
for (const type of ['dsh_list', 'dsh_install', 'dsh_update', 'dsh_remove']) {
expect(await client.request(type, { id: 'acme/thing' }), type).toMatchObject({ error: 'SERVICE_UNAVAILABLE', service: 'store', retryable: false })
}
+ for (const type of ['grid_models_list', 'models_list', 'grid_fleet_models_list', 'grid_fleet_model_download', 'grid_fleet_model_start', 'grid_fleet_model_stop']) {
+ expect(await client.request(type, { modelId: 'org/Model-GGUF' }), type).toMatchObject({ error: 'SERVICE_UNAVAILABLE', service: 'models', retryable: false })
+ }
+ // The Model Manager's grid commands are still the socket's, and so is their handshake: models being
+ // off does not tell the Grid harness to update Harness.
+ expect(await client.request('grid_fleet_capabilities', {})).toMatchObject({ protocol: 1, thinkingControl: true })
await daemon.restart()
const again = await LocalClient.connect(daemon)
@@ -142,6 +148,21 @@ describe('a failing service never takes the core down', () => {
client.close()
})
+ it('models answers from its own service; one of its requests failing leaves its others and the core alone', async () => {
+ daemon = await IsolatedDaemon.create({ env: { HARNESSD_TEST_FAULTS: 'models.grid_models_list' } })
+ onTestFailed(() => { console.log(`---- daemon log\n${daemon?.log().split('\n').slice(-80).join('\n')}`) })
+ await daemon.start()
+ const client = await LocalClient.connect(daemon)
+ expect(await client.request('grid_models_list', {})).toMatchObject({ error: 'SERVICE_FAILED', service: 'models' })
+ expect(daemon.log()).toContain('[services] models.grid_models_list failed · injected fault: models.grid_models_list')
+ // Its other requests are still its own answers: with no agent here, no Model/Effort choices.
+ expect(await client.request('models_list', {})).toMatchObject({ models: [] })
+ expect(await client.request('grid_fleet_capabilities', {})).toMatchObject({ protocol: 1 })
+ expect((await client.request('agents_list', {})).agents).toEqual([])
+ expect(daemon.coresStarted()).toBe(1)
+ client.close()
+ })
+
it('the store lists what is installed here and what the registry offers', async () => {
daemon = await IsolatedDaemon.create()
await daemon.start()
diff --git a/cli/src/architecture.spec.ts b/cli/src/architecture.spec.ts
index b71e55488..057c23a17 100644
--- a/cli/src/architecture.spec.ts
+++ b/cli/src/architecture.spec.ts
@@ -62,7 +62,7 @@ function runForegroundLines(): number {
* the code belongs in a module or a service.
*/
const RUN_FOREGROUND_BUDGET = 2_690
-const BACKEND_SOCKET_BUDGET = 3_160
+const BACKEND_SOCKET_BUDGET = 2_975
/** Exceptions, each with its reason. Keep this short. */
const SERVICE_MAY_IMPORT: Record<string, string> = {
diff --git a/cli/src/backendSocket.gridReads.spec.ts b/cli/src/backendSocket.gridReads.spec.ts
index 9c1bd2ccf..d18f8b551 100644
--- a/cli/src/backendSocket.gridReads.spec.ts
+++ b/cli/src/backendSocket.gridReads.spec.ts
@@ -1,7 +1,8 @@
/**
* The daemon's grid RPCs as a desktop drives them — `grid_models_list`, the Model Manager's
* `grid_fleet_models_list` and `agent_retarget` — against a fake relay that records every request it
- * receives, and a fake `grid` first on PATH (grid-reads-without-waking issue 02).
+ * receives, and a fake `grid` first on PATH (grid-reads-without-waking issue 02). The first two are
+ * the models service's (services/models.ts), answered through a service host as the daemon runs it.
*
* The relay is the spy: nothing here recomputes what the daemon sends, it reads what arrived.
*/
@@ -12,9 +13,13 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { BackendSocket } from './backendSocket.js'
import { env } from './config/env.js'
+import { emptyPorts, MODELS_FALLBACKS } from './core/api.js'
+import { createServiceHost } from './core/serviceHost.js'
import { fakeGridAnswers, installFakeGrid, type FakeGrid, type FakeGridPlan } from './lib/__fixtures__/fakeGrid.js'
import { clearGridMcpUrlCache } from './lib/gridMcpUrl.js'
import { observeMachineList, resetGridModels, type GridModelsService } from './lib/gridModels.js'
+import { MODELS_REQUESTS, startModels } from './services/models.js'
+import { fakeCore } from './testing/fakeCore.js'
const OWN = 'mine', OWN_ID = 'net-own'
const OVERVIEW = '/relay/v1/grid/overview'
@@ -79,7 +84,8 @@ beforeEach(async () => {
previousData = env.ADAPTER_DATA_DIR
env.ADAPTER_DATA_DIR = join(root, 'data')
clock = Date.parse('2026-09-24T10:00:00Z')
- // Before the socket, which subscribes to the service it finds for its `grid_models_changed` push.
+ // Before the socket and the models service, which subscribe to the service they find: the socket for its
+ // `grid_models_changed` push, models for the agents' frames.
service = resetGridModels({
now: () => clock, dataDir: () => join(root, 'data'), gridHome: () => gridHome, email: () => null,
// An explicit wake's 3 s pauses move the clock instead of the test.
@@ -88,6 +94,13 @@ beforeEach(async () => {
grid = installFakeGrid(plan())
socket = new BackendSocket('token')
socket.setHarnessGridName(OWN)
+ const models = createServiceHost(emptyPorts(), { log: () => {} })
+ models.start('models', startModels, fakeCore({
+ dataDir: join(root, 'data'),
+ account: { privateGridName: () => socket.privateGridName(), machineName: () => socket.machineName() },
+ clients: { gridModelsChanged: () => { void socket.pushGridModels() } },
+ }), MODELS_FALLBACKS, MODELS_REQUESTS)
+ socket.serviceRouter = (type, payload, asker, reply) => models.route(type, payload, asker, reply)
frames = []
socket.registerLocalClient('local:grid-reads', { sendFrame: (frame) => { frames.push(frame as typeof frames[number]); return true }, sendBinary: () => true })
})
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.ts
index 680e89c94..e393ef785 100644
--- a/cli/src/backendSocket.spec.ts
+++ b/cli/src/backendSocket.spec.ts
@@ -5,9 +5,12 @@ import { readFileSync, mkdirSync, mkdtempSync, writeFileSync, rmSync } from 'fs'
import { homedir, tmpdir } from 'os'
import { join } from 'path'
import { fileURLToPath } from 'url'
-import { AGENT_OPENED_THROTTLE_MS, BackendSocket, compactRuntimePickerModels, deviceAgentListItem, deviceAgentRow } from './backendSocket.js'
+import { AGENT_OPENED_THROTTLE_MS, BackendSocket, deviceAgentListItem, deviceAgentRow } from './backendSocket.js'
import { grokHistoryPage } from './core/transcripts/history.js'
-import { ServiceUnavailableError } from './core/serviceHost.js'
+import { emptyPorts, MODELS_FALLBACKS } from './core/api.js'
+import { createServiceHost, ServiceUnavailableError } from './core/serviceHost.js'
+import { MODELS_REQUESTS, startModels } from './services/models.js'
+import { fakeCore } from './testing/fakeCore.js'
import { AuthSessionError, type AuthSessionManager } from './lib/authSession.js'
import { WS_IDLE_DEADLINE_MS as IDLE_DEADLINE_MS } from './lib/wsLiveness.js'
import { TerminalStreamManager } from './lib/terminalStreamManager.js'
@@ -186,24 +189,10 @@ describe('reviewed permanent deletion RPCs', () => {
describe('local model lifecycle RPCs', () => {
afterEach(() => vi.restoreAllMocks())
- it.each(['grid_fleet_models_list', 'grid_fleet_model_download', 'grid_fleet_model_start', 'grid_fleet_model_stop'])('dispatches %s and returns its correlated result', async type => {
- const socket = new BackendSocket('fixture')
- socket.setHarnessGridName('home')
- const frames: any[] = []
- socket.registerLocalClient('local:models', { sendFrame: frame => { frames.push(frame); return true }, sendBinary: () => true })
- const list = vi.spyOn(LocalModels.prototype, 'list').mockResolvedValue({ models: [], busy: false, observedAt: 'fixture' })
- const act = vi.spyOn(LocalModels.prototype, 'act').mockResolvedValue({ error: 'fixture refusal' })
- socket.handleLocalFrame('local:models', { type, payload: { requestId: 'models-rpc', modelId: 'fixture/model', refresh: true } })
- await vi.waitFor(() => expect(frames.some(frame => frame.type === `${type}_result`)).toBe(true))
- expect(frames.find(frame => frame.type === `${type}_result`).payload.requestId).toBe('models-rpc')
- if (type === 'grid_fleet_models_list') expect(list).toHaveBeenCalledWith('home', true)
- else expect(act).toHaveBeenCalledWith('home', 'fixture/model', type.endsWith('download') ? 'download' : type.endsWith('start') ? 'start' : 'stop')
- await socket.stop()
- })
-
it('a slow catalog never blocks terminal or agent inventory, and errors stay redacted', async () => {
const socket = new BackendSocket('fixture')
socket.setHarnessGridName('home')
+ serveModels(socket)
const frames: any[] = []
socket.registerLocalClient('local:models', { sendFrame: frame => { frames.push(frame); return true }, sendBinary: () => true })
let reject!: (cause: Error) => void
@@ -221,10 +210,13 @@ describe('local model lifecycle RPCs', () => {
it('rejects unencrypted remote lifecycle requests before reaching the model service', async () => {
const socket = new BackendSocket('fixture')
+ const models = serveModels(socket)
+ const routed = vi.spyOn(models, 'route')
const act = vi.spyOn(LocalModels.prototype, 'act')
for (const type of ['grid_fleet_model_download', 'grid_fleet_model_start', 'grid_fleet_model_stop']) {
await (socket as any).dispatchDown({ type, payload: { requestId: 'unsafe', modelId: 'fixture/model' } }, 'remote')
}
+ expect(routed).not.toHaveBeenCalled()
expect(act).not.toHaveBeenCalled()
await socket.stop()
})
@@ -553,6 +545,21 @@ function sealedDown(socket: BackendSocket, connId: string, type: string, payload
return { t: 'down', connId, frame: { type, payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'fixture', clear: payload } } } }
}
+/** The models service (services/models.ts) answering the socket's models requests through a service host,
+ * as the daemon runs it: its core reads the grid name and this machine's name off the socket. */
+function serveModels(socket: BackendSocket, over: Parameters<typeof fakeCore>[0] = {}) {
+ const host = createServiceHost(emptyPorts(), { log: () => {} })
+ const core = fakeCore({
+ dataDir: process.env.ADAPTER_DATA_DIR,
+ ...over,
+ account: { privateGridName: () => socket.privateGridName(), machineName: () => socket.machineName(), ...over.account },
+ clients: { gridModelsChanged: () => { void socket.pushGridModels() }, ...over.clients },
+ })
+ host.start('models', startModels, core, MODELS_FALLBACKS, MODELS_REQUESTS)
+ socket.serviceRouter = (type, payload, asker, reply) => host.route(type, payload, asker, reply)
+ return host
+}
+
describe('BackendSocket outbound queue', () => {
afterEach(() => {
wsMock.instances.length = 0
@@ -775,9 +782,9 @@ describe('BackendSocket outbound queue', () => {
it('serves the opaque runtime catalog through the existing models_list RPC', async () => {
const socket = new BackendSocket('token')
- socket.runtimeModelsProvider = async () => [
+ serveModels(socket, { agents: { runtimeModels: async () => [
{ id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'GPT-5.6 Sol / High' },
- ]
+ ] } })
socket.connect()
const ws = wsMock.instances[0]
ws.open()
@@ -1191,7 +1198,7 @@ describe('BackendSocket outbound queue', () => {
{ id: 'runtime-v1:s1:codex:o3@medium', displayName: 'o3 / Medium' },
{ id: 'runtime-v1:s1:codex:o3@auto', displayName: 'o3 / Auto' },
])
- socket.runtimeModelsProvider = provider
+ serveModels(socket, { agents: { runtimeModels: provider } })
socket.connect()
const ws = wsMock.instances[0]
ws.open()
@@ -1232,7 +1239,7 @@ describe('BackendSocket outbound queue', () => {
const provider = vi.fn(async () => [
{ id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'sensitive' },
])
- socket.runtimeModelsProvider = provider
+ serveModels(socket, { agents: { runtimeModels: provider } })
socket.connect()
const ws = wsMock.instances[0]
ws.open()
@@ -1254,9 +1261,9 @@ describe('BackendSocket outbound queue', () => {
it('serves authenticated local RPCs in cleartext without weakening cloud E2EE', async () => {
const socket = new BackendSocket('token')
- socket.runtimeModelsProvider = async () => [
+ serveModels(socket, { agents: { runtimeModels: async () => [
{ id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'Sol / High' },
- ]
+ ] } })
const frames: Array<Record<string, unknown>> = []
expect(socket.registerLocalClient('local:test', {
sendFrame: (frame) => { frames.push(frame); return true },
@@ -1297,7 +1304,7 @@ describe('BackendSocket outbound queue', () => {
() => new Promise(resolve => { finish = resolve }),
)
const socket = new BackendSocket('token')
- socket.runtimeModelsProvider = async () => []
+ serveModels(socket)
const frames: Array<Record<string, unknown>> = []
socket.registerLocalClient('local:stats', {
sendFrame: frame => { frames.push(frame); return true }, sendBinary: () => true,
@@ -1351,7 +1358,7 @@ describe('BackendSocket outbound queue', () => {
const socket = new BackendSocket('token')
let finish!: (value: { sampledAt: string; agents: [] }) => void
socket.harnessResourcesReader = vi.fn(() => new Promise<{ sampledAt: string; agents: [] }>(resolve => { finish = resolve }))
- socket.runtimeModelsProvider = async () => []
+ serveModels(socket)
const frames: Array<Record<string, unknown>> = []
socket.registerLocalClient('local:monitor', {
sendFrame: frame => { frames.push(frame); return true }, sendBinary: () => true,
@@ -1387,10 +1394,10 @@ describe('BackendSocket outbound queue', () => {
it('never holds a connection\'s next request for a service still answering one', async () => {
const socket = new BackendSocket('token')
- socket.runtimeModelsProvider = async () => []
+ const models = serveModels(socket)
let answerCatalog!: (result: Record<string, unknown>) => void
- socket.serviceRouter = (type, _payload, _asker, reply) => {
- if (type !== 'dsh_list') return false
+ socket.serviceRouter = (type, payload, asker, reply) => {
+ if (type !== 'dsh_list') return models.route(type, payload, asker, reply)
answerCatalog = reply
return true
}
@@ -2501,43 +2508,6 @@ describe('agent_restart RPC', () => {
})
})
-describe('compact runtime picker catalog', () => {
- const models = [
- { id: 'runtime-v1:s1:codex:gpt-5.6-sol@auto', displayName: 'Sol / Auto' },
- { id: 'runtime-v1:s1:codex:gpt-5.6-sol@medium', displayName: 'Sol / Medium' },
- { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'Sol / High' },
- { id: 'runtime-v1:s1:codex:o3@high', displayName: 'o3 / High' },
- { id: 'runtime-v1:s2:claude:sonnet@high', displayName: 'Sonnet / High' },
- ]
-
- it('returns only explicit efforts for the selected session model', () => {
- expect(compactRuntimePickerModels(
- models,
- 's1',
- 'effort',
- 'runtime-v1:s1:codex:gpt-5.6-sol@medium',
- )).toEqual([
- { id: 'runtime-v1:s1:codex:gpt-5.6-sol@medium' },
- { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high' },
- ])
- })
-
- it('caps the device model list and keeps the running model in it', () => {
- // Devin publishes 72 models; a 49-row wheel already tripped the device's task watchdog once.
- const many = Array.from({ length: 40 }, (_, i) => ({
- id: `runtime-v1:s1:devin:model-${i}@auto`,
- displayName: `Model ${i}`,
- }))
- const capped = compactRuntimePickerModels(many, 's1', 'model', 'runtime-v1:s1:devin:model-39@auto')
-
- expect(capped).toHaveLength(24)
- // The model the agent is running would have fallen off the end of the catalog order.
- expect(capped[0]).toEqual({ id: 'runtime-v1:s1:devin:model-39@auto' })
- // The web asks without a picker mode and still gets the whole catalog.
- expect(compactRuntimePickerModels(many, 's1', undefined, null)).toHaveLength(40)
- })
-})
-
describe('device agent list contract', () => {
it('keeps the engine discriminator while trimming web-only fields', () => {
expect(deviceAgentListItem({ id: 's1', name: 'Codex agent', engine: 'codex', userId: 'secret' })).toEqual({
@@ -2841,6 +2811,7 @@ describe('grid_models_list says whether this machine has a grid CLI', () => {
async function listModels(): Promise<Record<string, unknown> | undefined> {
const socket = new BackendSocket('token')
+ serveModels(socket)
socket.connect()
const ws = wsMock.instances[0]
ws.open()
@@ -2866,20 +2837,14 @@ describe('grid is set up on demand — by an act, never by a read', () => {
const READY: GridAttachResult = { status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: '', ownGrid: 'created' }
- /** A daemon whose grid set-up and local models are stubs, asked over a local frame. */
+ /** A daemon whose grid set-up is a stub, asked over a local frame. The models picker's Set up and a
+ * local model's Get and Use are the models service's (services/models.spec.ts); a Grid harness command
+ * and a move onto a grid model are the socket's. */
function daemon(ready: GridAttachResult = READY) {
const socket = new BackendSocket('token')
socket.deriveGridName = async () => 'kelvin-1a2b3c4d'
- let setUp = false
- const ensureGrid = vi.fn(async (_request?: { ownGrid?: boolean }) => {
- if (ready.status === 'signed-in' || ready.status === 'converged') setUp = true
- return ready
- })
+ const ensureGrid = vi.fn(async (_request?: { ownGrid?: boolean }) => ready)
socket.ensureGrid = ensureGrid
- socket.gridSetUp = () => setUp
- const list = vi.fn(async () => ({ models: [], observedAt: 'now', busy: false }))
- const act = vi.fn(async () => ({}))
- Object.assign(socket as unknown as Record<string, unknown>, { localModels: { list, act } })
socket.onRetargetAgent = async () => ({ ok: true })
const frames: Array<Record<string, unknown>> = []
socket.registerLocalClient('local:grid', { sendFrame: (frame) => { frames.push(frame); return true }, sendBinary: () => true })
@@ -2893,55 +2858,9 @@ describe('grid is set up on demand — by an act, never by a read', () => {
await vi.waitFor(() => expect(answer()).toBeDefined())
return answer()!
}
- return { socket, ensureGrid, list, act, ask, done: async () => { await socket.unregisterLocalClient('local:grid'); await socket.stop() } }
+ return { socket, ensureGrid, ask, done: async () => { await socket.unregisterLocalClient('local:grid'); await socket.stop() } }
}
- it('the list read a picker polls sets nothing up, and says when it is needed', async () => {
- const d = daemon()
- const answer = await d.ask('grid_fleet_models_list')
- expect(d.ensureGrid).not.toHaveBeenCalled()
- expect(answer).toMatchObject({ gridSetupNeeded: true })
- await d.done()
- })
-
- it("Set up — a list read carrying `setup` — signs grid in with the account's own grid, then answers", async () => {
- const d = daemon()
- const answer = await d.ask('grid_fleet_models_list', { setup: true })
- expect(d.ensureGrid).toHaveBeenCalledExactlyOnceWith({ ownGrid: true })
- expect(answer).not.toHaveProperty('gridSetupNeeded')
- expect(answer).not.toHaveProperty('gridSetupError')
- // Read fresh: the catalog was unreachable a moment ago.
- expect(d.list).toHaveBeenCalledWith('kelvin-1a2b3c4d', true)
- await d.done()
- })
-
- it("a Get signs grid in; a Use also makes sure of the account's grid; a Stop does neither", async () => {
- const d = daemon()
- await d.ask('grid_fleet_model_download', { modelId: 'org/Model-GGUF' })
- await d.ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })
- await d.ask('grid_fleet_model_stop', { modelId: 'org/Model-GGUF' })
- expect(d.ensureGrid.mock.calls).toEqual([[{ ownGrid: false }], [{ ownGrid: true }]])
- expect(d.act.mock.calls.map((call) => (call as unknown[])[2])).toEqual(['download', 'start', 'stop'])
- await d.done()
- })
-
- it('a set-up grid refused is said, and the act waiting on it does not run', async () => {
- const d = daemon({ status: 'handoff-failed', name: 'kelvin-1a2b3c4d', detail: 'grid is too old for --harness' })
- expect(await d.ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })).toMatchObject({ error: 'grid is too old for --harness' })
- expect(d.act).not.toHaveBeenCalled()
- expect(await d.ask('grid_fleet_models_list', { setup: true }))
- .toMatchObject({ gridSetupNeeded: true, gridSetupError: 'grid is too old for --harness' })
- await d.done()
- })
-
- it("an account grid that could not be made fails a Use, not a Get", async () => {
- const d = daemon({ status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: 'free plan: one grid per account', ownGrid: 'failed' })
- expect(await d.ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })).toMatchObject({ error: 'free plan: one grid per account' })
- await d.ask('grid_fleet_model_download', { modelId: 'org/Model-GGUF' })
- expect(d.act.mock.calls.map((call) => (call as unknown[])[2])).toEqual(['download'])
- await d.done()
- })
-
it('a Grid harness command runs against grid as it stands — its viewer asks on its own, so it never sets grid up', async () => {
const d = daemon()
const run = vi.fn(async () => ({ ok: true, code: 0, stdout: '[]', stderr: '', error: null }))
@@ -2977,6 +2896,7 @@ describe('the connect burst with no network', () => {
const socket = new BackendSocket('token')
socket.deriveGridName = () => new Promise<null>(() => {}) // a grid read that never lands
socket.accountUsageReader = () => new Promise(() => {}) // a vendor that never answers
+ serveModels(socket)
const frames: Array<Record<string, unknown>> = []
socket.registerLocalClient('local:burst', { sendFrame: (frame) => { frames.push(frame); return true }, sendBinary: () => true })
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index c55992b95..fb9e64c5d 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -36,20 +36,17 @@ import { VERSION } from './version.js'
import { registry, projectDisplayName, type RegisteredSession } from './lib/registry.js'
import { AgentStopError } from './lib/stopAgentService.js'
import type { CloseAgentService, CloseMode } from './lib/closeAgentService.js'
-import { MODEL_MANAGER_ID, isHiddenBuiltin } from './dsh/builtins.js'
+import { isHiddenBuiltin } from './dsh/builtins.js'
import { ENGINES, PROCESS_ENGINES, isTerminalEngine, type AgentEngine, type ProcessEngine } from './engines/types.js'
import { listDir } from './lib/fsBrowse.js'
import { linkCodexProfile, listCodexProfiles } from './lib/codexProfiles.js'
import { gridCliPresence } from './lib/gridExec.js'
import { GridFleetRpc, GRID_FLEET_PROTOCOL, GRID_FLEET_MAX_TIMEOUT_MS, parseGridFleetRequest } from './lib/gridFleetRpc.js'
-import { LocalModels } from './lib/localModels.js'
-import { appEngineOps, scanAppModels } from './lib/appModels.js'
import { ApiConnectionError, ApiConnections, apiConnectionsRequest } from './lib/apiConnections.js'
import { apiModelsRequest, rememberSavedApis, resolveApiTarget } from './lib/apiModels.js'
-import { gridCapableEngines, isApiLaunch, parseGridLaunchOverride, type GridLaunchOverride } from './lib/gridLaunch.js'
-import {
- forgetGridModels, gridInventory, listAllGridModels, onGridModelsChanged, presentGridSections, retargetPrewarm, type GridSection,
-} from './lib/gridModels.js'
+import { isApiLaunch, parseGridLaunchOverride, type GridLaunchOverride } from './lib/gridLaunch.js'
+import { listAllGridModels, onGridModelsChanged, retargetPrewarm } from './lib/gridModels.js'
+import { gridModelsPayload } from './lib/gridModelsPayload.js'
import { resolveGridTarget } from './lib/gridTarget.js'
import type { GridAttachResult } from './lib/gridAttach.js'
import { parseNewAgentModel, resolveNewAgentModel } from './lib/newAgentModel.js'
@@ -112,7 +109,6 @@ import { tmuxPaneInfo } from './lib/tmux.js'
import { DEVICE_RECENT_SAFE_FRAME_BYTES, fitRecentReplyPayloadForDevice } from './lib/deviceRecentTrim.js'
import { shouldReplayCommander } from './lib/commanderReplay.js'
import { RuntimeProfileControlError, type RuntimeProfileErrorCode } from './lib/runtimeProfileController.js'
-import { parseRuntimeProfile, type RuntimeModelOption } from './lib/runtimeProfile.js'
import { sid, preview, logFrame } from './lib/log.js'
import {
TerminalP2pResponderPool,
@@ -257,106 +253,9 @@ export function deviceAgentRow(raw: unknown): boolean {
return !isTerminalEngine(typeof o.engine === 'string' ? o.engine : undefined)
}
-/**
- * How many models the DEVICE picker may receive. It has room for 48 (`models[48]` in ui_habitat.c) and
- * is handed half of that, so the list it draws is never one it was not built for; the web picker is
- * unbounded and still gets everything.
- */
-const DEVICE_PICKER_MAX_MODELS = 24
-
-export function compactRuntimePickerModels(
- models: RuntimeModelOption[],
- sessionId: string | undefined,
- pickerMode: unknown,
- selectedModel: unknown,
-): Array<{ id: string }> {
- const compact = models.map(({ id }) => ({ id }))
- if ((pickerMode !== 'model' && pickerMode !== 'effort') || !sessionId) return compact
-
- const profiles = models.flatMap((item) => {
- const profile = parseRuntimeProfile(item.id)
- return profile?.sessionId === sessionId ? [{ item, profile }] : []
- })
- const selected = parseRuntimeProfile(selectedModel)
- const current = selected?.sessionId === sessionId ? selected : null
-
- if (pickerMode === 'effort') {
- if (!current) return []
- const seen = new Set<string>()
- return profiles.flatMap(({ item, profile }) => {
- if (profile.model !== current.model || profile.effort === 'auto' || seen.has(profile.effort)) return []
- seen.add(profile.effort)
- return [{ id: item.id }]
- })
- }
-
- const byModel = new Map<string, typeof profiles>()
- for (const entry of profiles) {
- const group = byModel.get(entry.profile.model) ?? []
- group.push(entry)
- byModel.set(entry.profile.model, group)
- }
- const rows = [...byModel.values()].map((group) => {
- const target = group.find(({ profile }) => current && profile.effort === current.effort)
- ?? group.find(({ profile }) => profile.effort === 'auto')
- ?? group[0]
- return { id: target.item.id, model: target.profile.model }
- })
- // Top N only. The picker is a scroll wheel on a 1.9" round screen, and a 49-row one was enough to stall
- // the device's the device UI task into a task-watchdog reset; devin alone publishes 72
- // models. The catalog arrives in the engine's own order — its curated/most-used first — so "top" is that
- // order, with the model the agent is RUNNING pinned in front so the list can never hide it.
- const ordered = current
- ? [...rows].sort((a, b) => Number(b.model === current.model) - Number(a.model === current.model))
- : rows
- return ordered.slice(0, DEVICE_PICKER_MAX_MODELS).map(({ id }) => ({ id }))
-}
-
-/** Sections one `grid_models_list` may ask to wake — a person presses one "Show models" at a time. */
-const MAX_WAKES_PER_ASK = 8
-
-/** What `grid_models_list` answers and `grid_models_changed` pushes — one shape, so a window parses both
- * with one reader. `gridName` and `models` keep naming the own grid alone, for an app that predates
- * `grids`; each section's `state`, `seenAt`, `lastKnownAge` and `wakeOutcome` are additive, and a row's
- * offline label is `unavailable` for a window that asked for row state, its node text for one that did
- * not (`presentGridSections`). */
-function gridModelsPayload(gridName: string | null, sections: GridSection[], rowState: boolean): Record<string, unknown> {
- const grids = presentGridSections(sections, { rowState })
- return {
- gridName,
- models: grids.find((g) => g.own)?.models ?? [],
- grids,
- // Which engines a Local model can be offered to at all. Static per CLI version — it is
- // the set of launch contracts in `gridLaunch.ts` — and answered here, beside the list,
- // so the picker can say "Cursor runs only on its own login" instead of offering a row
- // whose retarget the daemon would refuse. An older app ignores the field; an older
- // daemon omits it, which the app reads as "offer everything", as before.
- localModelEngines: gridCapableEngines(),
- supportsModelLaunch: true,
- // Whether this MACHINE has a `grid` to run at all — `managed`, `path` or `missing` —
- // as distinct from `gridName`, which is about the account. The Local model dialog was
- // gating on the account alone and starting an agent whose second step is `grid`; this
- // is what lets it, and the picker, say so first. An older app ignores the field.
- gridCli: gridCliPresence(),
- }
-}
-
export class BackendSocket {
harnessDevices: HarnessDevicesService | null = null
private readonly gridFleet = new GridFleetRpc()
- // The Model Manager reads the grid it runs on through the same credential-less reader as every picker
- // (never `grid engines`, which carries the grid credential and so wakes a sleeping grid on every tick),
- // and a start or stop it finishes makes every list read again — pushed to the window when it changes.
- private readonly localModels = new LocalModels({
- stateDir: join(env.ADAPTER_DATA_DIR, 'local-models'),
- machineName: () => this.machineDisplayName,
- inventory: gridInventory,
- onChanged: () => { forgetGridModels(); void this.pushGridModels() },
- // Models Ollama, LM Studio and llama.cpp downloaded here, found by the Model Manager's own scan (the
- // bundled harness), so the picker and that harness agree on what is here and what starts it.
- appModels: () => scanAppModels({ node: process.execPath, packageDir: installedDsh(MODEL_MANAGER_ID)?.realDir ?? null, env: process.env }),
- appEngines: appEngineOps(process.env),
- })
/** This machine's name as Harness shows it (Machines), from the backend's `machine_meta`. Null
* until the first one arrives. */
private machineDisplayName: string | null = null
@@ -685,18 +584,14 @@ export class BackendSocket {
Promise<{ file: string | null; gitRepo: boolean; cwd: string; degraded: string[] }>) | null = null
monitorActivityProvider: ((sessionId: string) => MonitorActivity) | null = null
private readonly monitorCompletions = new MonitorCompletions()
- /** Runtime Model/Effort integration, wired by cli.ts for registered tmux sessions. */
- runtimeModelsProvider: ((sessionId?: string) => Promise<RuntimeModelOption[]>) | null = null
/** Answers `usage_read` — this machine's own agent-account usage (lib/accountUsage.ts). A field
* rather than a direct call so a spec answers it without a real home, Keychain or network. */
accountUsageReader: () => Promise<AccountUsageReading[]> = readAccountUsage
harnessResourcesReader = createHarnessResourcesReader(() => registry.advertised())
harnessStorageReader = createHarnessStorageReader()
- /** The grid listing currently out, shared by every `grid_models_list` for the same own grid
- * that lands meanwhile. */
- private gridModelsInFlight: { gridName: string | null; grids: ReturnType<typeof listAllGridModels> } | null = null
/** The windows on this computer that draw row state (`grid_models_list` with `rowState: true`) and so
- * are pushed labels as `unavailable` rather than in the node text. */
+ * are pushed labels as `unavailable` rather than in the node text. Kept here, by connection: the
+ * models service answers the list, and a request reaches it without its connection. */
private readonly rowStateWindows = new Set<string>()
/** Receives `theme_set` — the desktop's pane colours, to become this machine's tmux
* `window-style` (lib/hostTheme.ts). Wired by cli.ts; null answers with UNSUPPORTED. */
@@ -912,12 +807,10 @@ export class BackendSocket {
deriveGridName: () => Promise<string | null> = deriveHarnessGridName
/** Have grid ready for a grid feature the person is using now (`lib/gridAttach.ts`): `grid` installed,
* signed in as this account with its harness token, and — `ownGrid` — the account's own grid there.
- * Asked by acts only (Set up, Get, Use, a move onto a grid model, a Model Manager command), never by
- * a read. Set by `cli.ts`; null (tests) reads grid as it stands. */
+ * Asked by acts only (here a move onto a grid model, and making a Model Manager; the models service
+ * asks its own for Set up, Get and Use), never by a read. Set by `cli.ts`; null (tests) reads grid as
+ * it stands. */
ensureGrid: ((request?: { ownGrid?: boolean }) => Promise<GridAttachResult>) | null = null
- /** Offline, cheap: is there a `grid` here holding a sign-in? Answers the list read's
- * `gridSetupNeeded`. Null (tests) reads as set up. */
- gridSetUp: (() => boolean) | null = null
/** Set the account's private grid name from the reconcile that just confirmed it, so the RPCs
* answer with it at once rather than waiting for the next `machine_meta` (`lib/gridAttach.ts`). */
@@ -929,13 +822,15 @@ export class BackendSocket {
/** This machine's name as the Machines list shows it — what a model it serves is labelled with. */
machineName(): string | null { return this.machineDisplayName }
- /** The account's private grid, resolved the way the models RPC resolves it — for a harness
- * workspace that must be told which grid is "yours" rather than work it out or ask. */
+ /** The account's private grid, resolved the way the models RPC resolves it — for the models service,
+ * and for a harness workspace that must be told which grid is "yours" rather than work it out or ask. */
privateGridName(): Promise<string | null> { return this.resolveGridName() }
/** `grid_models_changed` to the windows on this computer: the same payload `grid_models_list` answers,
- * built from the pictures as they stand — no read is started to build it, so a push never causes one. */
- private async pushGridModels(): Promise<void> {
+ * built from the pictures as they stand — no read is started to build it, so a push never causes one.
+ * On a background read's change, and on the models service's word (a local model started or stopped,
+ * grid set up). */
+ async pushGridModels(): Promise<void> {
if (this.closed || this.localClients.size === 0) return
try {
const gridName = await this.resolveGridName()
@@ -1962,6 +1857,10 @@ export class BackendSocket {
// SERVICE_UNAVAILABLE while it is off. Never waited on in line: the next frame is not held for it.
// Who asked is established here, after the gates above, and the service trusts only that.
const asker: Asker = { local, owner: local || this.e2ee.sessionRole(connId) === 'web' }
+ // A window that draws row state asks for the models list with `rowState`, and the list's changes are
+ // pushed to it in that form (`pushGridModels`). Noted here, by connection: the models service answers
+ // the list, and a request reaches it without its connection.
+ if (type === 'grid_models_list' && payload.rowState === true && this.localClients.has(connId)) this.rowStateWindows.add(connId)
if (this.serviceRouter?.(type, payload, asker, (result) => reply(type, requestId, result))) return
try {
@@ -1978,35 +1877,13 @@ export class BackendSocket {
.then(resources => reply(type, requestId, { ...resources }))
.catch(() => reply(type, requestId, { error: 'UNAVAILABLE' }))
return
- case 'grid_fleet_models_list':
- case 'grid_fleet_model_download':
- case 'grid_fleet_model_start':
- case 'grid_fleet_model_stop': {
- // A daemon-owned operation survives panel closure and a lost reply.
- // Keep hardware/catalog/network reads off the ordered terminal queue.
- //
- // Grid is set up here only for an ACT: the picker's Set up (a list read carrying `setup`),
- // a Get, a Use. The list read the app polls while a picker is open never sets anything up —
- // it says whether it is needed (`gridSetupNeeded`). A Get needs a sign-in (the catalog is
- // grid's); a Use serves on the account's own grid, and so does the Set up that offers it.
- void (async () => {
- const list = type === 'grid_fleet_models_list'
- const setup = list ? payload.setup === true : type !== 'grid_fleet_model_stop'
- const notReady = setup ? await this.gridNotReady(type !== 'grid_fleet_model_download') : null
- const grid = await this.resolveGridName()
- if (list) {
- const snapshot = await this.localModels.list(grid, payload.refresh === true || setup)
- const needed = this.gridSetUp ? !this.gridSetUp() : false
- if (setup && !notReady) void this.pushGridModels()
- return { ...snapshot, ...(needed ? { gridSetupNeeded: true } : {}), ...(notReady ? { gridSetupError: notReady } : {}) }
- }
- if (notReady) return { error: notReady }
- return this.localModels.act(grid, payload.modelId, type === 'grid_fleet_model_download' ? 'download' : type === 'grid_fleet_model_start' ? 'start' : 'stop')
- })()
- .then(result => reply(type, requestId, { ...result }))
- .catch(() => reply(type, requestId, { error: 'Models are unavailable. Try again.' }))
- return
- }
+ // The Model Manager's grid commands stay here while the rest of models answers from its service
+ // (services/models.ts). A command is a job of the connection that started it, keyed by that
+ // connection, and a cancel stops only that connection's own (lib/gridFleetRpc.ts); a request the
+ // service answers knows who asked (`Asker`) but not over which connection. `grid_fleet_capabilities`
+ // is the commands' handshake (their protocol, longest timeout and thinking control): the Grid
+ // harness sends `grid_fleet_run` only once it answers protocol 1, and reads anything else as
+ // "update Harness", so it stays beside them rather than go off with the models service.
case 'grid_fleet_capabilities':
reply(type, requestId, { protocol: GRID_FLEET_PROTOCOL, gridCli: gridCliPresence(), maxTimeoutMs: GRID_FLEET_MAX_TIMEOUT_MS, thinkingControl: true })
return
@@ -2128,70 +2005,6 @@ export class BackendSocket {
reply(type, requestId, this.historyProvider ? await this.historyProvider(payload) : { error: 'UNSUPPORTED' })
return
- case 'grid_models_list': {
- // Every grid this computer is signed into, in sections, the account's own first. `gridName`
- // and `models` keep naming the own grid alone, for an app that predates `grids`.
- //
- // DETACHED from this connection's ordered RPC chain, like `engines_probe`: it waits on a
- // grid reconcile (up to 6s), then a `grid ls` spawn and — for a grid this daemon has never
- // read — up to 4s of its first read (`gridModels.ts`); every other grid answers from its picture.
- // The desktop asks for it in the same breath as `terminal_capabilities` and `agents_list`
- // on every connect, and awaited here it held both behind it — with no network, past the
- // app's 10s request timeout, on which the app forces a reconnect and asks all three again.
- // Measured 2026-09-18, wifi off, daemon restarted: every local RPC timed out for as long
- // as the backend stayed unreachable; the terminal on the SAME computer sat on "offline"
- // until the wifi came back. Request ids make the reply safe to land out of order.
- //
- // One computation at a time: detached, a second ask that lands while the first is still
- // out (the app re-asks on every connect) would spawn another `grid ls` for the same answer.
- // Later askers share the one in flight; each grid's reads are single-flight in the service.
- //
- // `rowState: true` — a window that draws row state gets offline labels as `unavailable` (and
- // its pushes in that form); `wake: [name]` — a person pressed "Show models" / "Wake now", and
- // the answer (with those sections "waking") comes back at once while the wake runs behind it
- // (grid-reads-without-waking issue 03). A wake never joins a listing already out: that one
- // was built before the wake began, and would not say "waking".
- const rowState = payload.rowState === true
- if (rowState && this.localClients.has(connId)) this.rowStateWindows.add(connId)
- const wake = Array.isArray(payload.wake)
- ? payload.wake.filter((name): name is string => typeof name === 'string' && !!name.trim()).map((name) => name.trim()).slice(0, MAX_WAKES_PER_ASK)
- : []
- void (async () => {
- const gridName = await this.resolveGridName()
- const inFlight = this.gridModelsInFlight
- const listing = wake.length
- ? listAllGridModels(gridName, { wake })
- : inFlight && inFlight.gridName === gridName
- ? inFlight.grids
- : (this.gridModelsInFlight = {
- gridName,
- grids: listAllGridModels(gridName).finally(() => {
- if (this.gridModelsInFlight?.gridName === gridName) this.gridModelsInFlight = null
- }),
- }).grids
- reply(type, requestId, gridModelsPayload(gridName, await listing, rowState))
- })().catch(() => reply(type, requestId, { error: 'GRID_MODELS_FAILED' }))
- return
- }
-
-
- case 'models_list': {
- const sessionId = typeof payload.agentId === 'string' && payload.agentId
- ? payload.agentId
- : undefined
- const models = this.runtimeModelsProvider
- ? await this.runtimeModelsProvider(sessionId)
- : [{ id: 'default', displayName: 'Remote CLI default' }]
- reply(type, requestId, {
- // The device derives labels from the opaque runtime-v1 id. Omitting the duplicate
- // displayName keeps the encrypted picker response below its 16 KiB decrypt cap.
- models: payload.compact === true
- ? compactRuntimePickerModels(models, sessionId, payload.pickerMode, payload.selectedModel)
- : models,
- })
- return
- }
-
case 'remote_terminal_handoff': {
// `harness remote`, typed INSIDE one of this machine's terminal tiles, opened a terminal on
// another machine and asks the window showing the tile to swap it over: the tile it was
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 23b6d08e6..842f6b042 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -171,7 +171,7 @@ import { runViewersService } from './services/viewersProcess.js'
import { SEARCH_REQUESTS, startSearch } from './services/search.js'
import { STORE_REQUESTS, startStore } from './services/store.js'
import { startViewers } from './services/viewers.js'
-import { startModels } from './services/models.js'
+import { MODELS_REQUESTS, startModels } from './services/models.js'
import { startWorkspaces } from './services/workspaces.js'
import { describeMasterStatus, readStatusFile, runMaster } from './harnessd/master.js'
import { CORE_EXIT_STOP, CORE_EXIT_UPDATE } from './harnessd/protocol.js'
@@ -221,7 +221,7 @@ import { agentFrame, lastActivityAt, type AgentFrame } from './lib/agentFrame.js
import { agentTokenUsage } from './lib/agentTokenUsage.js'
import { DeviceResultJournal } from './lib/autonomous-device/resultJournal.js'
import { adaptSlashCommand } from './lib/goalCommand.js'
-import { RuntimeProfileManager } from './lib/runtimeProfile.js'
+import { RuntimeProfileManager, type RuntimeModelOption } from './lib/runtimeProfile.js'
import { RuntimeProfileController } from './lib/runtimeProfileController.js'
// Before ANY child is spawned: on Linux an absent locale makes tmux and ps mangle their output,
// which silently costs the daemon every pane it would have discovered. See lib/childLocale.ts.
@@ -1762,6 +1762,13 @@ async function runForeground(session: AuthSession | null): Promise<void> {
}
syncRecapPool()
const runtimeProfiles = new RuntimeProfileManager()
+ // An agent's Model/Effort choices, or every live agent's: what `models_list` answers (services/models.ts)
+ // and the dial's picker reads, so neither can show a catalog the machine would not honour.
+ const runtimeModels = (agentId?: string): Promise<RuntimeModelOption[]> => {
+ if (!agentId) return runtimeProfiles.modelsForSessions(registry.list())
+ const session = registry.resolve(agentId)
+ return session ? runtimeProfiles.modelsForSession(session) : Promise.resolve([])
+ }
// NB: hooks are installed AFTER the hook server binds (below), with the port it actually got — the
// server may fall back to a free port if env.PORT is taken, and the hooks must point at the real one.
@@ -1794,11 +1801,15 @@ async function runForeground(session: AuthSession | null): Promise<void> {
externalSessions,
openSessions,
syncSession,
+ runtimeModels,
viewerChanged: (agentId) => {
backendRef?.viewerForwarder.refresh(agentId)
backendRef?.interactiveViewers.refresh(agentId)
},
gridNamed: (name) => backendRef?.setHarnessGridName(name),
+ gridModelsChanged: () => { void backendRef?.pushGridModels() },
+ privateGridName: async () => (await backendRef?.privateGridName()) ?? null,
+ machineName: () => backendRef?.machineName() ?? null,
dshInstallStatus: (status) => backendRef?.send({ type: 'dsh_install_status', payload: status }),
// The backend mints and remembers the account's grid name; this CLI holds neither the account's
// email nor its id. An older backend (no route) answers nothing, which the grid reconcile treats as
@@ -1894,15 +1905,13 @@ async function runForeground(session: AuthSession | null): Promise<void> {
ensureBundledCoreHarnesses()
- // Models: grid access, the model pictures on agents' frames, the keystroke prewarm (services/models.ts).
- serviceHost.start('models', startModels, coreApi, MODELS_FALLBACKS)
+ // Models: grid access, the model pictures on agents' frames, the keystroke prewarm, and the models
+ // requests the apps send (services/models.ts).
+ serviceHost.start('models', startModels, coreApi, MODELS_FALLBACKS, MODELS_REQUESTS)
const models = ports.models
backend.ensureGrid = models ? (request) => models.ensure(request) : null
- // Offline, for every list read: is there a `grid` here holding a sign-in? What decides whether the
- // picker offers local and shared models or a Set up row.
- backend.gridSetUp = models ? () => models.setUp() : null
// Models switched off: the socket reads grid as it stands, as it does with no models at all.
- serviceHost.onOff('models', () => { backend.ensureGrid = null; backend.gridSetUp = null })
+ serviceHost.onOff('models', () => { backend.ensureGrid = null })
/**
* Is ANY device surface watching this machine?
@@ -2241,11 +2250,6 @@ async function runForeground(session: AuthSession | null): Promise<void> {
const agentIdFor = (sessionId: string): string => registry.bySession(sessionId)?.agentId ?? sessionId
- backend.runtimeModelsProvider = (sessionId) => {
- if (!sessionId) return runtimeProfiles.modelsForSessions(registry.list())
- const session = registry.resolve(sessionId)
- return session ? runtimeProfiles.modelsForSession(session) : Promise.resolve([])
- }
backend.runtimeProfileProvider = (session) => runtimeProfiles.selectedModel(session)
backend.dshFrameProvider = (s) => ports.viewers?.frameContext(s) ?? null
// `harness remote` names the tile it was typed in by its tmux pane; the registry knows whose it is.
@@ -4032,7 +4036,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {
},
// The same provider the web and the WiFi device read, so the dial's picker cannot show a different
// catalog from the one the machine will actually honour.
- listModels: async (agentId) => (await backend.runtimeModelsProvider?.(agentId)) ?? [],
+ listModels: runtimeModels,
// Both of these are LOCAL-ONLY on purpose (backend.sendLocal, not backend.send): they describe a hand
// at this desk, not a change in what the machine is doing, and the cloud web audience may be sitting
// at another computer entirely.
diff --git a/cli/src/core/api.spec.ts b/cli/src/core/api.spec.ts
index ebc60908e..dff6f774e 100644
--- a/cli/src/core/api.spec.ts
+++ b/cli/src/core/api.spec.ts
@@ -19,11 +19,15 @@ describe('the core API services stand on', () => {
externalSessions: { list: vi.fn(), scan: vi.fn() } as unknown as CoreApiDeps['externalSessions'],
openSessions: { known: vi.fn(), fresh: vi.fn() } as unknown as CoreApiDeps['openSessions'],
syncSession: vi.fn(),
+ runtimeModels: vi.fn(async () => []),
viewerChanged: vi.fn(),
gridNamed: vi.fn(),
+ gridModelsChanged: vi.fn(),
dshInstallStatus: vi.fn(),
mintGridName: vi.fn(async () => 'grid-1'),
accessToken: vi.fn(async () => 'token'),
+ privateGridName: vi.fn(async () => 'grid-1'),
+ machineName: vi.fn(() => 'Studio'),
}
const core = createCoreApi(deps)
expect(core.dataDir).toBe('/data')
@@ -37,12 +41,16 @@ describe('the core API services stand on', () => {
expect(core.agents.byAgent('gone')).toBeUndefined()
expect(core.agents.terminalAvailable('live')).toBe(true)
expect(core.agents.sync).toBe(deps.syncSession)
+ expect(core.agents.runtimeModels).toBe(deps.runtimeModels)
expect(core.clients.viewerChanged).toBe(deps.viewerChanged)
expect(core.clients.dshInstallStatus).toBe(deps.dshInstallStatus)
expect(core.agents.advertised().map((s) => s.agentId)).toEqual(['live'])
expect(core.clients.gridNamed).toBe(deps.gridNamed)
+ expect(core.clients.gridModelsChanged).toBe(deps.gridModelsChanged)
expect(core.account.mintGridName).toBe(deps.mintGridName)
expect(core.account.accessToken).toBe(deps.accessToken)
+ expect(core.account.privateGridName).toBe(deps.privateGridName)
+ expect(core.account.machineName).toBe(deps.machineName)
})
it('falls back, when the teams fail, to an undo that has nothing to undo', () => {
diff --git a/cli/src/core/api.ts b/cli/src/core/api.ts
index c07209141..1d25db068 100644
--- a/cli/src/core/api.ts
+++ b/cli/src/core/api.ts
@@ -15,6 +15,7 @@ import type { GridAccess } from '../lib/gridAttach.js'
import type { AgentGridTarget } from '../lib/gridModels.js'
import type { LiveEvent } from '../lib/normalize.js'
import { projectDisplayName, type registry, type RegisteredSession } from '../lib/registry.js'
+import type { RuntimeModelOption } from '../lib/runtimeProfile.js'
import type { ExternalSessions, OpenSessions } from '../lib/sessionSearch/external.js'
import type { SessionSearchIndex } from '../lib/sessionSearch/indexer.js'
import type { StoppedAgentStore } from '../lib/stoppedAgents.js'
@@ -39,6 +40,9 @@ export interface CoreApi {
terminalAvailable(agentId: string): boolean
/** Send the agent's frame to the apps again. */
sync(session: RegisteredSession): void
+ /** The Model/Effort choices an agent's engine offers (opaque `runtime-v1` ids): one agent's, or
+ * every live agent's when none is named. */
+ runtimeModels(agentId?: string): Promise<RuntimeModelOption[]>
}
transcripts: {
/** How to read a conversation its engine keeps in a database instead of a transcript file;
@@ -57,12 +61,21 @@ export interface CoreApi {
mintGridName(): Promise<string | null>
/** This machine's harness access token, for handing a sign-in to grid. Rejects when signed out. */
accessToken(): Promise<string>
+ /** The account's private grid: the backend's word when it gave one, else what this machine works
+ * out (`lib/gridDerive.ts`); null when it has none. */
+ privateGridName(): Promise<string | null>
+ /** This machine's name as the account's Machines list shows it (the backend's `machine_meta`); null
+ * until the first one lands. */
+ machineName(): string | null
}
clients: {
/** An agent's viewer moved: the windows' viewer panes forward to the new one. */
viewerChanged(agentId: string): void
/** The account's grid has a name: the models picker answers with it at once. */
gridNamed(name: string): void
+ /** What the models picker lists may have changed (a local model started or stopped, grid set up):
+ * the windows on this computer are pushed the list again (`grid_models_changed`). */
+ gridModelsChanged(): void
/** A harness being installed or updated moved on (`dsh_install_status`): the apps show it in the
* create dialog. */
dshInstallStatus(status: Record<string, unknown>): void
@@ -176,16 +189,20 @@ export interface CoreApiDeps {
externalSessions: CoreApi['external']['sessions']
openSessions: CoreApi['external']['open']
syncSession: CoreApi['agents']['sync']
+ runtimeModels: CoreApi['agents']['runtimeModels']
viewerChanged: CoreApi['clients']['viewerChanged']
gridNamed: CoreApi['clients']['gridNamed']
+ gridModelsChanged: CoreApi['clients']['gridModelsChanged']
dshInstallStatus: CoreApi['clients']['dshInstallStatus']
mintGridName: CoreApi['account']['mintGridName']
accessToken: CoreApi['account']['accessToken']
+ privateGridName: CoreApi['account']['privateGridName']
+ machineName: CoreApi['account']['machineName']
}
export function createCoreApi({
- dataDir, registry, stoppedAgents, databaseHistory, externalSessions, openSessions, syncSession, viewerChanged,
- gridNamed, dshInstallStatus, mintGridName, accessToken,
+ dataDir, registry, stoppedAgents, databaseHistory, externalSessions, openSessions, syncSession, runtimeModels, viewerChanged,
+ gridNamed, gridModelsChanged, dshInstallStatus, mintGridName, accessToken, privateGridName, machineName,
}: CoreApiDeps): CoreApi {
return {
dataDir,
@@ -197,10 +214,11 @@ export function createCoreApi({
advertised: () => registry.advertised(),
terminalAvailable: (agentId) => registry.terminalAvailable(agentId),
sync: syncSession,
+ runtimeModels,
},
transcripts: { databaseHistory },
external: { sessions: externalSessions, open: openSessions },
- account: { mintGridName, accessToken },
- clients: { viewerChanged, gridNamed, dshInstallStatus },
+ account: { mintGridName, accessToken, privateGridName, machineName },
+ clients: { viewerChanged, gridNamed, gridModelsChanged, dshInstallStatus },
}
}
diff --git a/cli/src/lib/gridModelsPayload.ts b/cli/src/lib/gridModelsPayload.ts
new file mode 100644
index 000000000..994f4a14a
--- /dev/null
+++ b/cli/src/lib/gridModelsPayload.ts
@@ -0,0 +1,33 @@
+/**
+ * What `grid_models_list` answers (the models service, services/models.ts) and `grid_models_changed`
+ * pushes (the socket, to the windows on this computer) — one shape, so a window parses both with one
+ * reader. A pure function of the sections, here so the two build it the same way.
+ */
+import { gridCliPresence } from './gridExec.js'
+import { gridCapableEngines } from './gridLaunch.js'
+import { presentGridSections, type GridSection } from './gridModels.js'
+
+/** `gridName` and `models` keep naming the own grid alone, for an app that predates `grids`; each
+ * section's `state`, `seenAt`, `lastKnownAge` and `wakeOutcome` are additive, and a row's offline label
+ * is `unavailable` for a window that asked for row state, its node text for one that did not
+ * (`presentGridSections`). */
+export function gridModelsPayload(gridName: string | null, sections: GridSection[], rowState: boolean): Record<string, unknown> {
+ const grids = presentGridSections(sections, { rowState })
+ return {
+ gridName,
+ models: grids.find((g) => g.own)?.models ?? [],
+ grids,
+ // Which engines a Local model can be offered to at all. Static per CLI version — it is
+ // the set of launch contracts in `gridLaunch.ts` — and answered here, beside the list,
+ // so the picker can say "Cursor runs only on its own login" instead of offering a row
+ // whose retarget the daemon would refuse. An older app ignores the field; an older
+ // daemon omits it, which the app reads as "offer everything", as before.
+ localModelEngines: gridCapableEngines(),
+ supportsModelLaunch: true,
+ // Whether this MACHINE has a `grid` to run at all — `managed`, `path` or `missing` —
+ // as distinct from `gridName`, which is about the account. The Local model dialog was
+ // gating on the account alone and starting an agent whose second step is `grid`; this
+ // is what lets it, and the picker, say so first. An older app ignores the field.
+ gridCli: gridCliPresence(),
+ }
+}
diff --git a/cli/src/services/models.spec.ts b/cli/src/services/models.spec.ts
index f7dbb6a24..fc72e8b85 100644
--- a/cli/src/services/models.spec.ts
+++ b/cli/src/services/models.spec.ts
@@ -1,19 +1,28 @@
+import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import { emptyPorts, type ModelsPort } from '../core/api.js'
-import { createGridAccess, gridNamesLocal, reconcileGridAttach } from '../lib/gridAttach.js'
+import { emptyPorts, MODELS_FALLBACKS, type ModelsPort } from '../core/api.js'
+import { createServiceHost } from '../core/serviceHost.js'
+import { installedDsh } from '../dsh/installed.js'
+import { appEngineOps, scanAppModels } from '../lib/appModels.js'
+import { createGridAccess, gridNamesLocal, reconcileGridAttach, type GridAttachResult } from '../lib/gridAttach.js'
import { resetGridDeriveMemo, signedInGridEmail } from '../lib/gridDerive.js'
import { ensureHarnessGrid } from '../lib/gridEnsure.js'
import { gridAvailable } from '../lib/gridExec.js'
import { handOffToGrid } from '../lib/gridHandoff.js'
+import { gridCapableEngines } from '../lib/gridLaunch.js'
import { ensureGridInstalled } from '../lib/gridInstall.js'
import { clearGridMcpUrlCache } from '../lib/gridMcpUrl.js'
-import { forgetGridModels, gridAnnotation, keystrokePrewarm, onGridModelsChanged, warmGridModels } from '../lib/gridModels.js'
+import {
+ forgetGridModels, gridAnnotation, gridInventory, keystrokePrewarm, listAllGridModels, onGridModelsChanged, presentGridSections, warmGridModels,
+ type GridSection,
+} from '../lib/gridModels.js'
import type { RegisteredSession } from '../lib/registry.js'
+import type { RuntimeModelOption } from '../lib/runtimeProfile.js'
import { ensureManagedGrid } from '../lib/runtimeInstall.js'
import { fakeCore } from '../testing/fakeCore.js'
-import { startModels } from './models.js'
+import { compactRuntimePickerModels, MODELS_REQUESTS, startModels } from './models.js'
-const access = vi.hoisted(() => ({ ensure: vi.fn(async () => ({ status: 'converged', name: 'grid-1', detail: 'ok' })) }))
+const access = vi.hoisted(() => ({ ensure: vi.fn(async (_request?: { ownGrid?: boolean }): Promise<GridAttachResult> => ({ status: 'converged', name: 'grid-1', detail: 'ok' })) }))
vi.mock('../lib/gridAttach.js', () => ({
createGridAccess: vi.fn(() => access),
gridNamesLocal: vi.fn(() => ['grid-1']),
@@ -21,30 +30,54 @@ vi.mock('../lib/gridAttach.js', () => ({
}))
vi.mock('../lib/gridDerive.js', () => ({ resetGridDeriveMemo: vi.fn(), signedInGridEmail: vi.fn(() => '[email protected]') }))
vi.mock('../lib/gridEnsure.js', () => ({ ensureHarnessGrid: vi.fn(async () => 'converged') }))
-vi.mock('../lib/gridExec.js', () => ({ gridAvailable: vi.fn(() => true) }))
+vi.mock('../lib/gridExec.js', () => ({ gridAvailable: vi.fn(() => true), gridCliPresence: vi.fn(() => 'managed') }))
vi.mock('../lib/gridHandoff.js', () => ({ handOffToGrid: vi.fn(async () => ({ ok: true })) }))
vi.mock('../lib/gridInstall.js', () => ({ ensureGridInstalled: vi.fn(async () => ({ status: 'present', message: '' })) }))
vi.mock('../lib/gridMcpUrl.js', () => ({ clearGridMcpUrlCache: vi.fn() }))
vi.mock('../lib/gridModels.js', () => ({
forgetGridModels: vi.fn(),
gridAnnotation: vi.fn((grid: { state: string }) => ({ state: grid.state })),
+ gridInventory: vi.fn(),
keystrokePrewarm: vi.fn(async () => 'started'),
+ listAllGridModels: vi.fn(async () => []),
onGridModelsChanged: vi.fn(),
+ presentGridSections: vi.fn((sections: unknown[]) => sections),
warmGridModels: vi.fn(async () => {}),
}))
vi.mock('../lib/runtimeInstall.js', () => ({ ensureManagedGrid: vi.fn(async () => {}) }))
+vi.mock('../lib/appModels.js', () => ({ appEngineOps: vi.fn(() => ({ engines: 'app' })), scanAppModels: vi.fn(async () => []) }))
+vi.mock('../dsh/installed.js', async (importOriginal) => ({
+ ...await importOriginal<typeof import('../dsh/installed.js')>(),
+ installedDsh: vi.fn(() => ({ realDir: '/dsh/model-manager' })),
+}))
+/** The Model Manager's local models: what the service built it with, and its list and act. */
+const local = vi.hoisted(() => ({
+ options: {} as Record<string, any>,
+ list: vi.fn(async (_grid: string | null, _force?: boolean): Promise<Record<string, unknown>> => ({ models: [], busy: false, observedAt: 'now' })),
+ act: vi.fn(async (_grid: string | null, _modelId: unknown, _action: string): Promise<Record<string, unknown>> => ({})),
+}))
+vi.mock('../lib/localModels.js', () => ({
+ LocalModels: class {
+ constructor(options: Record<string, unknown>) { local.options = options }
+ list = local.list
+ act = local.act
+ },
+}))
type AttemptDeps = Parameters<typeof reconcileGridAttach>[0]
+type Over = Parameters<typeof fakeCore>[0]
const onGrid = (agentId: string, state: string) => ({ agentId, grid: { state } }) as unknown as RegisteredSession
-function setup(advertised: RegisteredSession[] = []) {
+function setup(advertised: RegisteredSession[] = [], over: Over = {}) {
const list = [...advertised]
- const core = fakeCore({ agents: { advertised: vi.fn(() => list), sync: vi.fn() } })
+ const core = fakeCore({ ...over, agents: { advertised: vi.fn(() => list), sync: vi.fn(), ...over.agents } })
const ports = emptyPorts()
- startModels(core, ports)
+ const requests = startModels(core, ports)
const options = vi.mocked(createGridAccess).mock.calls.at(-1)![0]
const changed = vi.mocked(onGridModelsChanged).mock.calls.at(-1)![0]
- return { core, list, port: ports.models as ModelsPort, options, changed }
+ /** Ask one request as a local window would. */
+ const ask = async (type: string, payload: Record<string, unknown> = {}) => await requests[type]!(payload, { local: true, owner: true })
+ return { core, list, port: ports.models as ModelsPort, options, changed, requests, ask }
}
/** The reconcile deps the access attempt builds, for one attempt. */
@@ -55,6 +88,17 @@ async function attemptDeps(options: ReturnType<typeof setup>['options']): Promis
return deps
}
+const section = (name: string, own: boolean, models: Array<{ id: string; node?: string }>) =>
+ ({ name, type: 'permissioned-public', own, models, state: 'awake', seenAt: null, lastKnownAge: null }) as unknown as GridSection
+
+/** A promise and the hands that settle it. */
+function deferred<T>() {
+ let resolve!: (value: T) => void
+ let reject!: (error: unknown) => void
+ const promise = new Promise<T>((done, fail) => { resolve = done; reject = fail })
+ return { promise, resolve, reject }
+}
+
describe('the models service', () => {
beforeEach(() => { vi.spyOn(console, 'log').mockImplementation(() => {}) })
afterEach(() => { vi.restoreAllMocks(); vi.clearAllMocks() })
@@ -172,4 +216,337 @@ describe('the models service', () => {
expect(clearGridMcpUrlCache).toHaveBeenCalledTimes(1)
})
})
+
+ describe('the requests it answers for the apps', () => {
+ it('answers exactly the requests it declares, and while it is off the host answers them unavailable', async () => {
+ expect(Object.keys(setup().requests).sort()).toEqual([...MODELS_REQUESTS].sort())
+ // Not the Model Manager's grid commands, whose jobs belong to the connection that started them, nor
+ // their handshake, which the Grid harness reads as "update Harness" when it does not answer.
+ for (const socketOwn of ['grid_fleet_run', 'grid_fleet_cancel', 'grid_fleet_capabilities']) expect(MODELS_REQUESTS).not.toContain(socketOwn)
+
+ const on = createServiceHost(emptyPorts(), { log: () => {} })
+ on.start('models', startModels, fakeCore(), MODELS_FALLBACKS, MODELS_REQUESTS)
+ expect(on.isOff('models')).toBe(false)
+ const off = createServiceHost(emptyPorts(), { log: () => {}, faults: new Set(['models']) })
+ off.start('models', startModels, fakeCore(), MODELS_FALLBACKS, MODELS_REQUESTS)
+ for (const type of MODELS_REQUESTS) {
+ const reply = vi.fn()
+ expect(off.route(type, { requestId: 'r' }, { local: true, owner: true }, reply)).toBe(true)
+ expect(reply).toHaveBeenCalledWith({ error: 'SERVICE_UNAVAILABLE', service: 'models', retryable: false })
+ }
+ })
+
+ describe('grid_models_list', () => {
+ it("answers every grid in sections, the account's own first, in the shape the push has", async () => {
+ const sections = [section('mine', true, [{ id: 'Small-Q4', node: 'mac' }]), section('team', false, [{ id: 'Big', node: 'studio' }])]
+ vi.mocked(listAllGridModels).mockResolvedValueOnce(sections)
+ const { core, ask } = setup([], { account: { privateGridName: vi.fn(async () => 'mine') } })
+
+ expect(await ask('grid_models_list')).toEqual({
+ gridName: 'mine',
+ models: [{ id: 'Small-Q4', node: 'mac' }],
+ grids: sections,
+ localModelEngines: gridCapableEngines(),
+ supportsModelLaunch: true,
+ gridCli: 'managed',
+ })
+ expect(core.account.privateGridName).toHaveBeenCalled()
+ expect(listAllGridModels).toHaveBeenCalledExactlyOnceWith('mine')
+ // A window that did not ask for row state reads the offline label in the node text.
+ expect(presentGridSections).toHaveBeenCalledWith(sections, { rowState: false })
+ })
+
+ it('labels rows for a window that draws row state, and names no models when no grid is its own', async () => {
+ const sections = [section('team', false, [{ id: 'Big' }])]
+ vi.mocked(listAllGridModels).mockResolvedValueOnce(sections)
+ const { ask } = setup()
+ expect(await ask('grid_models_list', { rowState: true })).toMatchObject({ gridName: null, models: [], grids: sections })
+ expect(presentGridSections).toHaveBeenCalledWith(sections, { rowState: true })
+ })
+
+ it('shares the one listing out with every ask for the same grid that lands meanwhile, and starts afresh after it', async () => {
+ const first = deferred<GridSection[]>()
+ vi.mocked(listAllGridModels).mockReturnValueOnce(first.promise)
+ const { ask } = setup([], { account: { privateGridName: vi.fn(async () => 'mine') } })
+ const a = ask('grid_models_list')
+ const b = ask('grid_models_list', { rowState: true })
+ await vi.waitFor(() => expect(listAllGridModels).toHaveBeenCalledTimes(1))
+ first.resolve([section('mine', true, [{ id: 'm' }])])
+ expect(await a).toMatchObject({ models: [{ id: 'm' }] })
+ expect(await b).toMatchObject({ models: [{ id: 'm' }] })
+ expect(listAllGridModels).toHaveBeenCalledTimes(1)
+ await ask('grid_models_list')
+ expect(listAllGridModels).toHaveBeenCalledTimes(2)
+ })
+
+ it('never shares a listing of another grid, and an older listing landing late leaves the newer one out', async () => {
+ const names = ['a', 'b', 'b']
+ const ofA = deferred<GridSection[]>()
+ const ofB = deferred<GridSection[]>()
+ vi.mocked(listAllGridModels).mockReturnValueOnce(ofA.promise).mockReturnValueOnce(ofB.promise)
+ const { ask } = setup([], { account: { privateGridName: vi.fn(async () => names.shift()!) } })
+ const a = ask('grid_models_list')
+ await vi.waitFor(() => expect(listAllGridModels).toHaveBeenCalledTimes(1))
+ const b = ask('grid_models_list')
+ await vi.waitFor(() => expect(listAllGridModels).toHaveBeenCalledTimes(2))
+ expect(listAllGridModels).toHaveBeenLastCalledWith('b')
+ ofA.resolve([section('a', true, [{ id: 'from-a' }])])
+ expect(await a).toMatchObject({ gridName: 'a', models: [{ id: 'from-a' }] })
+ // b's listing is still the one out: a third ask for b joins it rather than starting another.
+ const again = ask('grid_models_list')
+ ofB.resolve([section('b', true, [{ id: 'from-b' }])])
+ expect(await b).toMatchObject({ gridName: 'b', models: [{ id: 'from-b' }] })
+ expect(await again).toMatchObject({ gridName: 'b', models: [{ id: 'from-b' }] })
+ expect(listAllGridModels).toHaveBeenCalledTimes(2)
+ })
+
+ it('a person\'s wake goes to the grids at once, never joins a listing already out, and wakes at most eight', async () => {
+ const out = deferred<GridSection[]>()
+ vi.mocked(listAllGridModels).mockReturnValueOnce(out.promise)
+ const { ask } = setup([], { account: { privateGridName: vi.fn(async () => 'mine') } })
+ const listing = ask('grid_models_list')
+ await vi.waitFor(() => expect(listAllGridModels).toHaveBeenCalledTimes(1))
+
+ await ask('grid_models_list', { wake: [' mine ', '', ' ', 7, 'team'] })
+ expect(listAllGridModels).toHaveBeenLastCalledWith('mine', { wake: ['mine', 'team'] })
+ const many = Array.from({ length: 10 }, (_, i) => `grid-${i}`)
+ await ask('grid_models_list', { wake: many })
+ expect(listAllGridModels).toHaveBeenLastCalledWith('mine', { wake: many.slice(0, 8) })
+ expect(listAllGridModels).toHaveBeenCalledTimes(3)
+ out.resolve([])
+ await listing
+ })
+
+ it('a listing that fails is answered GRID_MODELS_FAILED, and so is a grid name that cannot be read', async () => {
+ vi.mocked(listAllGridModels).mockRejectedValueOnce(new Error('grid ls exited 1'))
+ const failing = setup([], { account: { privateGridName: vi.fn(async () => 'mine') } })
+ expect(await failing.ask('grid_models_list')).toEqual({ error: 'GRID_MODELS_FAILED' })
+ // The failed listing is not kept for the next ask.
+ expect(await failing.ask('grid_models_list')).toMatchObject({ gridName: 'mine' })
+ const nameless = setup([], { account: { privateGridName: vi.fn(async () => { throw new Error('no grid') }) } })
+ expect(await nameless.ask('grid_models_list')).toEqual({ error: 'GRID_MODELS_FAILED' })
+ })
+ })
+
+ describe('models_list', () => {
+ const catalog: RuntimeModelOption[] = [
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'GPT-5.6 Sol / High' },
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@auto', displayName: 'GPT-5.6 Sol / Auto' },
+ { id: 'runtime-v1:s1:codex:o3@medium', displayName: 'o3 / Medium' },
+ { id: 'runtime-v1:s1:codex:o3@auto', displayName: 'o3 / Auto' },
+ ]
+
+ it('answers the Model/Effort choices of every live agent, or of the one it names', async () => {
+ const runtimeModels = vi.fn(async (_agentId?: string) => catalog)
+ const { ask } = setup([], { agents: { runtimeModels } })
+ expect(await ask('models_list')).toEqual({ models: catalog })
+ expect(await ask('models_list', { agentId: '' })).toEqual({ models: catalog })
+ expect(await ask('models_list', { agentId: 's1' })).toEqual({ models: catalog })
+ expect(runtimeModels.mock.calls).toEqual([[undefined], [undefined], ['s1']])
+ })
+
+ it('answers the device only ids, at most what its picker draws, filtered to the picker it shows', async () => {
+ const runtimeModels = vi.fn(async () => catalog)
+ const { ask } = setup([], { agents: { runtimeModels } })
+ expect(await ask('models_list', { agentId: 's1', compact: true, pickerMode: 'model', selectedModel: 'runtime-v1:s1:codex:gpt-5.6-sol@high' }))
+ .toEqual({ models: [{ id: 'runtime-v1:s1:codex:gpt-5.6-sol@high' }, { id: 'runtime-v1:s1:codex:o3@auto' }] })
+ expect(await ask('models_list', { compact: true })).toEqual({ models: catalog.map(({ id }) => ({ id })) })
+ })
+
+ it('a catalog that cannot be read is logged and answered INTERNAL, as the socket did', async () => {
+ const error = vi.spyOn(console, 'error').mockImplementation(() => {})
+ const failure = new Error('catalog unreadable')
+ const { ask } = setup([], { agents: { runtimeModels: vi.fn(async () => { throw failure }) } })
+ expect(await ask('models_list', { agentId: 's1' })).toEqual({ error: 'INTERNAL' })
+ expect(error).toHaveBeenCalledWith('[backend] dispatch models_list failed:', failure)
+ })
+ })
+
+ describe("the Model Manager's local models", () => {
+ const READY: GridAttachResult = { status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: '', ownGrid: 'created' }
+ const named = { account: { privateGridName: vi.fn(async () => 'kelvin-1a2b3c4d') } }
+
+ it('the list read a picker polls sets nothing up, and says when it is needed', async () => {
+ vi.mocked(signedInGridEmail).mockReturnValue(null)
+ try {
+ const { core, ask } = setup([], named)
+ expect(await ask('grid_fleet_models_list')).toEqual({ models: [], busy: false, observedAt: 'now', gridSetupNeeded: true })
+ expect(access.ensure).not.toHaveBeenCalled()
+ expect(local.list).toHaveBeenCalledWith('kelvin-1a2b3c4d', false)
+ expect(core.clients.gridModelsChanged).not.toHaveBeenCalled()
+ // Asked to, it reads afresh; still nothing set up.
+ await ask('grid_fleet_models_list', { refresh: true })
+ expect(local.list).toHaveBeenLastCalledWith('kelvin-1a2b3c4d', true)
+ expect(access.ensure).not.toHaveBeenCalled()
+ } finally {
+ vi.mocked(signedInGridEmail).mockReturnValue('[email protected]')
+ }
+ })
+
+ it("Set up — a list read carrying `setup` — signs grid in with the account's own grid, answers, and tells the windows", async () => {
+ access.ensure.mockResolvedValueOnce(READY)
+ const { core, ask } = setup([], named)
+ const answer = await ask('grid_fleet_models_list', { setup: true })
+ expect(access.ensure).toHaveBeenCalledExactlyOnceWith({ ownGrid: true })
+ expect(answer).not.toHaveProperty('gridSetupNeeded')
+ expect(answer).not.toHaveProperty('gridSetupError')
+ // Read fresh: the catalog was unreachable a moment ago.
+ expect(local.list).toHaveBeenCalledWith('kelvin-1a2b3c4d', true)
+ expect(core.clients.gridModelsChanged).toHaveBeenCalledOnce()
+ })
+
+ it("a Get signs grid in; a Use also makes sure of the account's grid; a Stop does neither", async () => {
+ access.ensure.mockResolvedValue(READY)
+ const { ask } = setup([], named)
+ await ask('grid_fleet_model_download', { modelId: 'org/Model-GGUF' })
+ await ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })
+ await ask('grid_fleet_model_stop', { modelId: 'org/Model-GGUF' })
+ expect(access.ensure.mock.calls).toEqual([[{ ownGrid: false }], [{ ownGrid: true }]])
+ expect(local.act.mock.calls).toEqual([
+ ['kelvin-1a2b3c4d', 'org/Model-GGUF', 'download'],
+ ['kelvin-1a2b3c4d', 'org/Model-GGUF', 'start'],
+ ['kelvin-1a2b3c4d', 'org/Model-GGUF', 'stop'],
+ ])
+ access.ensure.mockReset()
+ })
+
+ it('answers what the act answers, as it is', async () => {
+ local.act.mockResolvedValueOnce({ operation: { action: 'start', modelId: 'org/Model-GGUF' } })
+ local.act.mockResolvedValueOnce({ error: 'fixture refusal' })
+ const { ask } = setup([], named)
+ expect(await ask('grid_fleet_model_stop', { modelId: 'org/Model-GGUF' })).toEqual({ operation: { action: 'start', modelId: 'org/Model-GGUF' } })
+ expect(await ask('grid_fleet_model_stop', { modelId: 'org/Model-GGUF' })).toEqual({ error: 'fixture refusal' })
+ })
+
+ it('a set-up grid refused is said, and the act waiting on it does not run', async () => {
+ access.ensure.mockResolvedValue({ status: 'handoff-failed', name: 'kelvin-1a2b3c4d', detail: 'grid is too old for --harness' })
+ vi.mocked(signedInGridEmail).mockReturnValue(null)
+ try {
+ const { core, ask } = setup([], named)
+ expect(await ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })).toEqual({ error: 'grid is too old for --harness' })
+ expect(local.act).not.toHaveBeenCalled()
+ expect(await ask('grid_fleet_models_list', { setup: true }))
+ .toMatchObject({ gridSetupNeeded: true, gridSetupError: 'grid is too old for --harness' })
+ expect(core.clients.gridModelsChanged).not.toHaveBeenCalled()
+ } finally {
+ access.ensure.mockReset()
+ vi.mocked(signedInGridEmail).mockReturnValue('[email protected]')
+ }
+ })
+
+ it('an account grid that could not be made fails a Use, not a Get', async () => {
+ access.ensure.mockResolvedValue({ status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: 'free plan: one grid per account', ownGrid: 'failed' })
+ const { ask } = setup([], named)
+ expect(await ask('grid_fleet_model_start', { modelId: 'org/Model-GGUF' })).toEqual({ error: 'free plan: one grid per account' })
+ await ask('grid_fleet_model_download', { modelId: 'org/Model-GGUF' })
+ expect(local.act.mock.calls.map((call) => call[2])).toEqual(['download'])
+ access.ensure.mockReset()
+ })
+
+ it('says why in its own words when grid gives none, and takes a grid already there or adopted as ready', async () => {
+ const { ask } = setup([], named)
+ access.ensure.mockResolvedValueOnce({ status: 'no-cli', name: null, detail: '' })
+ expect(await ask('grid_fleet_model_download', { modelId: 'm' })).toEqual({ error: 'Grid could not be set up on this computer. Try again.' })
+ access.ensure.mockResolvedValueOnce({ status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: '', ownGrid: 'failed' })
+ expect(await ask('grid_fleet_model_start', { modelId: 'm' })).toEqual({ error: 'Your grid could not be created. Try again.' })
+ for (const ownGrid of ['existed', 'adopted'] as const) {
+ access.ensure.mockResolvedValueOnce({ status: 'converged', name: 'kelvin-1a2b3c4d', detail: '', ownGrid })
+ expect(await ask('grid_fleet_model_start', { modelId: 'm' })).toEqual({})
+ }
+ // A sign-in alone, with nothing said about the account's grid, is ready for a Use too.
+ access.ensure.mockResolvedValueOnce({ status: 'signed-in', name: 'kelvin-1a2b3c4d', detail: '' })
+ expect(await ask('grid_fleet_model_start', { modelId: 'm' })).toEqual({})
+ expect(local.act).toHaveBeenCalledTimes(3)
+ })
+
+ it('a failure is answered with one sentence, never with what failed', async () => {
+ local.list.mockRejectedValueOnce(new Error('private-token'))
+ local.act.mockRejectedValueOnce(new Error('private-token'))
+ const { ask } = setup([], named)
+ const list = await ask('grid_fleet_models_list')
+ const stop = await ask('grid_fleet_model_stop', { modelId: 'm' })
+ expect(list).toEqual({ error: 'Models are unavailable. Try again.' })
+ expect(stop).toEqual({ error: 'Models are unavailable. Try again.' })
+ expect(JSON.stringify([list, stop])).not.toContain('private-token')
+ })
+
+ it('is built on this machine: its folder, its name, the inventory read without a credential, and the apps that hold models', async () => {
+ const { core, ask } = setup([], { dataDir: '/data/here', account: { machineName: vi.fn(() => 'Studio') } })
+ expect(local.options.stateDir).toBe(join('/data/here', 'local-models'))
+ expect(local.options.machineName()).toBe('Studio')
+ expect(core.account.machineName).toHaveBeenCalled()
+ expect(local.options.inventory).toBe(gridInventory)
+ expect(appEngineOps).toHaveBeenCalledWith(process.env)
+ expect(local.options.appEngines).toEqual({ engines: 'app' })
+ await local.options.appModels()
+ expect(installedDsh).toHaveBeenCalledWith('autonomous/autonomous-grid')
+ expect(scanAppModels).toHaveBeenLastCalledWith({ node: process.execPath, packageDir: '/dsh/model-manager', env: process.env })
+ vi.mocked(installedDsh).mockReturnValueOnce(undefined)
+ await local.options.appModels()
+ expect(scanAppModels).toHaveBeenLastCalledWith({ node: process.execPath, packageDir: null, env: process.env })
+ // A start or stop finished: every list reads again, and the windows are told.
+ local.options.onChanged()
+ expect(forgetGridModels).toHaveBeenCalledOnce()
+ expect(core.clients.gridModelsChanged).toHaveBeenCalledOnce()
+ expect(await ask('grid_fleet_models_list')).toMatchObject({ models: [] })
+ })
+ })
+ })
+})
+
+describe('the device picker\'s compact catalog', () => {
+ const models = [
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@auto', displayName: 'Sol / Auto' },
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@medium', displayName: 'Sol / Medium' },
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high', displayName: 'Sol / High' },
+ { id: 'runtime-v1:s1:codex:o3@high', displayName: 'o3 / High' },
+ { id: 'runtime-v1:s2:claude:sonnet@high', displayName: 'Sonnet / High' },
+ ]
+
+ it('returns only explicit efforts for the selected session model', () => {
+ expect(compactRuntimePickerModels(
+ models,
+ 's1',
+ 'effort',
+ 'runtime-v1:s1:codex:gpt-5.6-sol@medium',
+ )).toEqual([
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@medium' },
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high' },
+ ])
+ // Each effort once, though the catalog repeats it.
+ expect(compactRuntimePickerModels([...models, models[2]!], 's1', 'effort', 'runtime-v1:s1:codex:gpt-5.6-sol@high'))
+ .toEqual([{ id: 'runtime-v1:s1:codex:gpt-5.6-sol@medium' }, { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high' }])
+ // No effort to show for a model the agent is not known to run.
+ expect(compactRuntimePickerModels(models, 's1', 'effort', 'runtime-v1:s2:claude:sonnet@high')).toEqual([])
+ expect(compactRuntimePickerModels(models, 's1', 'effort', null)).toEqual([])
+ })
+
+ it('lists each of the agent\'s models once, at the effort it runs, else Auto, else the first', () => {
+ expect(compactRuntimePickerModels(models, 's1', 'model', 'runtime-v1:s1:codex:o3@high')).toEqual([
+ // The running model first; the others at the running effort when they have it.
+ { id: 'runtime-v1:s1:codex:o3@high' },
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@high' },
+ ])
+ expect(compactRuntimePickerModels(models, 's1', 'model', null)).toEqual([
+ { id: 'runtime-v1:s1:codex:gpt-5.6-sol@auto' },
+ { id: 'runtime-v1:s1:codex:o3@high' },
+ ])
+ // Without the agent named there is nothing to filter by: every id, as the web gets them.
+ expect(compactRuntimePickerModels(models, undefined, 'model', null)).toEqual(models.map(({ id }) => ({ id })))
+ })
+
+ it('caps the device model list and keeps the running model in it', () => {
+ // Devin publishes 72 models; a 49-row wheel already tripped the device's task watchdog once.
+ const many = Array.from({ length: 40 }, (_, i) => ({
+ id: `runtime-v1:s1:devin:model-${i}@auto`,
+ displayName: `Model ${i}`,
+ }))
+ const capped = compactRuntimePickerModels(many, 's1', 'model', 'runtime-v1:s1:devin:model-39@auto')
+
+ expect(capped).toHaveLength(24)
+ // The model the agent is running would have fallen off the end of the catalog order.
+ expect(capped[0]).toEqual({ id: 'runtime-v1:s1:devin:model-39@auto' })
+ // The web asks without a picker mode and still gets the whole catalog.
+ expect(compactRuntimePickerModels(many, 's1', undefined, null)).toHaveLength(40)
+ })
})
diff --git a/cli/src/services/models.ts b/cli/src/services/models.ts
index cba4fc9f9..40a900995 100644
--- a/cli/src/services/models.ts
+++ b/cli/src/services/models.ts
@@ -1,12 +1,18 @@
/**
* Models: grid, how Harness runs local AI models with Codex, Claude Code and the other engines
* (docs/design/2026-10-03-harnessd.md, "Models"). Grid access on first use, the model pictures an
- * agent's frame carries, and starting a sleeping grid while someone types to its agent.
+ * agent's frame carries, starting a sleeping grid while someone types to its agent, and what the apps
+ * ask about models: the picker's list, an agent's Model/Effort choices and the Model Manager's local
+ * models.
*
- * A service on the core boundary (step 13): it reads the core only through `CoreApi`, and the core
- * reaches it only through `ports.models`.
+ * A service on the core boundary (step 13): it reads the core only through `CoreApi`, the core
+ * reaches it only through `ports.models`, and the apps through the requests it answers.
*/
-import type { CoreApi, CorePorts } from '../core/api.js'
+import { join } from 'node:path'
+import type { CoreApi, CorePorts, ModelsPort, ServiceRequest, ServiceRequests } from '../core/api.js'
+import { MODEL_MANAGER_ID } from '../dsh/builtins.js'
+import { installedDsh } from '../dsh/installed.js'
+import { appEngineOps, scanAppModels } from '../lib/appModels.js'
import { createGridAccess, gridNamesLocal, reconcileGridAttach } from '../lib/gridAttach.js'
import { resetGridDeriveMemo, signedInGridEmail } from '../lib/gridDerive.js'
import { ensureHarnessGrid } from '../lib/gridEnsure.js'
@@ -14,10 +20,29 @@ import { gridAvailable } from '../lib/gridExec.js'
import { handOffToGrid } from '../lib/gridHandoff.js'
import { ensureGridInstalled } from '../lib/gridInstall.js'
import { clearGridMcpUrlCache } from '../lib/gridMcpUrl.js'
-import { forgetGridModels, gridAnnotation, keystrokePrewarm, onGridModelsChanged, warmGridModels } from '../lib/gridModels.js'
+import {
+ forgetGridModels, gridAnnotation, gridInventory, keystrokePrewarm, listAllGridModels, onGridModelsChanged, warmGridModels, type GridSection,
+} from '../lib/gridModels.js'
+import { gridModelsPayload } from '../lib/gridModelsPayload.js'
+import { LocalModels } from '../lib/localModels.js'
+import { parseRuntimeProfile, type RuntimeModelOption } from '../lib/runtimeProfile.js'
import { ensureManagedGrid } from '../lib/runtimeInstall.js'
-export function startModels(core: CoreApi, ports: CorePorts): void {
+/**
+ * The requests models answers for the apps.
+ *
+ * The Model Manager's grid commands, `grid_fleet_run` and `grid_fleet_cancel`, are still the socket's:
+ * a command is a job of the connection that started it, and a cancel stops only that connection's job
+ * (`lib/gridFleetRpc.ts`), while a request answered here knows who asked but not over which connection.
+ * Their handshake, `grid_fleet_capabilities`, stays beside them: the Grid harness runs a command only
+ * after it, and reads an answer without its protocol as "update Harness".
+ */
+export const MODELS_REQUESTS = [
+ 'grid_models_list', 'models_list',
+ 'grid_fleet_models_list', 'grid_fleet_model_download', 'grid_fleet_model_start', 'grid_fleet_model_stop',
+] as const
+
+export function startModels(core: CoreApi, ports: CorePorts): ServiceRequests {
// Grid is an add-on (`lib/gridAttach.ts`): nothing on this path installs `grid`, signs this machine in
// to it or creates a grid. The first grid feature a person uses — the models picker's Set up, a local
// model's Get or Use, an agent moved onto a grid model, the Model Manager — asks `ports.models.ensure`,
@@ -74,15 +99,217 @@ export function startModels(core: CoreApi, ports: CorePorts): void {
// frame carries its grid's state and note, and a keystroke can start its grid, before any window asks
// for the list (after a self-update, a phone may be the only one typing).
void warmGridModels().catch(() => {})
- ports.models = {
+ const grid: Pick<ModelsPort, 'ensure' | 'setUp'> = {
ensure: (request) => gridAccess.ensure(request),
// Offline, for every list read: is there a `grid` here holding a sign-in? What decides whether the
// picker offers local and shared models or a Set up row.
setUp: () => gridAvailable() && signedInGridEmail() !== null,
+ }
+ ports.models = {
+ ...grid,
// The keystroke prewarm (grid-reads-without-waking issue 03): typing into a pane whose agent runs on
// a sleeping grid starts that grid while the person types.
- prewarm: (grid) => { void keystrokePrewarm(grid).catch(() => {}) },
+ prewarm: (target) => { void keystrokePrewarm(target).catch(() => {}) },
// The web-tools cache lives exactly as long as the sign-in.
signedOut: () => clearGridMcpUrlCache(),
}
+ return modelsRequests(core, grid)
+}
+
+/** Sections one `grid_models_list` may ask to wake — a person presses one "Show models" at a time. */
+const MAX_WAKES_PER_ASK = 8
+
+type LocalModelRequest = 'grid_fleet_models_list' | 'grid_fleet_model_download' | 'grid_fleet_model_start' | 'grid_fleet_model_stop'
+
+/**
+ * What the apps ask about models, on grid as this service holds it. Each request waits on grid, the
+ * network or a model's process; the host replies when it is done and never holds the asking connection's
+ * next request behind it (core/serviceHost.ts `route`), which is why the socket ran them detached when it
+ * answered them itself. A failure is answered as the socket answered it, sentences included: the apps
+ * show them.
+ */
+function modelsRequests(core: CoreApi, grid: Pick<ModelsPort, 'ensure' | 'setUp'>): ServiceRequests {
+ // The Model Manager reads the grid it runs on through the same credential-less reader as every picker
+ // (never `grid engines`, which carries the grid credential and so wakes a sleeping grid on every tick),
+ // and a start or stop it finishes makes every list read again — pushed to the window when it changes.
+ const localModels = new LocalModels({
+ stateDir: join(core.dataDir, 'local-models'),
+ machineName: () => core.account.machineName(),
+ inventory: gridInventory,
+ onChanged: () => { forgetGridModels(); core.clients.gridModelsChanged() },
+ // Models Ollama, LM Studio and llama.cpp downloaded here, found by the Model Manager's own scan (the
+ // bundled harness), so the picker and that harness agree on what is here and what starts it.
+ appModels: () => scanAppModels({ node: process.execPath, packageDir: installedDsh(MODEL_MANAGER_ID)?.realDir ?? null, env: process.env }),
+ appEngines: appEngineOps(process.env),
+ })
+ /** The grid listing currently out, shared by every `grid_models_list` for the same own grid that lands
+ * meanwhile. */
+ let listing: { gridName: string | null; grids: Promise<GridSection[]> } | null = null
+
+ /**
+ * Grid set up for an act, or the sentence saying why it could not be: null when it is ready. The
+ * socket says the same for a move onto a grid model (`agent_retarget`), which it still answers.
+ */
+ const notReady = async (ownGrid: boolean): Promise<string | null> => {
+ const ready = await grid.ensure({ ownGrid })
+ if (ready.status !== 'converged' && ready.status !== 'signed-in') {
+ return ready.detail || 'Grid could not be set up on this computer. Try again.'
+ }
+ if (ownGrid && ready.ownGrid && !['created', 'existed', 'adopted'].includes(ready.ownGrid)) {
+ return ready.detail || 'Your grid could not be created. Try again.'
+ }
+ return null
+ }
+
+ // A daemon-owned operation survives panel closure and a lost reply. Its hardware, catalog and network
+ // reads stay off the connection's ordered queue: the host answers when they are done.
+ //
+ // Grid is set up here only for an ACT: the picker's Set up (a list read carrying `setup`), a Get, a
+ // Use. The list read the app polls while a picker is open never sets anything up — it says whether it
+ // is needed (`gridSetupNeeded`). A Get needs a sign-in (the catalog is grid's); a Use serves on the
+ // account's own grid, and so does the Set up that offers it.
+ const localModel = (type: LocalModelRequest): ServiceRequest => async (payload) => {
+ try {
+ const list = type === 'grid_fleet_models_list'
+ const setup = list ? payload.setup === true : type !== 'grid_fleet_model_stop'
+ const unready = setup ? await notReady(type !== 'grid_fleet_model_download') : null
+ const gridName = await core.account.privateGridName()
+ if (list) {
+ const snapshot = await localModels.list(gridName, payload.refresh === true || setup)
+ const needed = !grid.setUp()
+ if (setup && !unready) core.clients.gridModelsChanged()
+ return { ...snapshot, ...(needed ? { gridSetupNeeded: true } : {}), ...(unready ? { gridSetupError: unready } : {}) }
+ }
+ if (unready) return { error: unready }
+ return { ...await localModels.act(gridName, payload.modelId, type === 'grid_fleet_model_download' ? 'download' : type === 'grid_fleet_model_start' ? 'start' : 'stop') }
+ } catch {
+ return { error: 'Models are unavailable. Try again.' }
+ }
+ }
+
+ return {
+ grid_models_list: async (payload) => {
+ // Every grid this computer is signed into, in sections, the account's own first. `gridName`
+ // and `models` keep naming the own grid alone, for an app that predates `grids`.
+ //
+ // Never held in line: it waits on a grid reconcile (up to 6s), then a `grid ls` spawn and — for a
+ // grid this daemon has never read — up to 4s of its first read (`gridModels.ts`); every other grid
+ // answers from its picture. The desktop asks for it in the same breath as `terminal_capabilities`
+ // and `agents_list` on every connect, and awaited in line it held both behind it — with no
+ // network, past the app's 10s request timeout, on which the app forces a reconnect and asks all
+ // three again. Measured 2026-09-18, wifi off, daemon restarted: every local RPC timed out for as
+ // long as the backend stayed unreachable; the terminal on the SAME computer sat on "offline" until
+ // the wifi came back. Request ids make the reply safe to land out of order.
+ //
+ // One computation at a time: a second ask that lands while the first is still out (the app re-asks
+ // on every connect) would spawn another `grid ls` for the same answer. Later askers share the one
+ // in flight; each grid's reads are single-flight in `gridModels.ts`.
+ //
+ // `rowState: true` — a window that draws row state gets offline labels as `unavailable` (and the
+ // socket pushes it the list's changes in that form); `wake: [name]` — a person pressed "Show
+ // models" / "Wake now", and the answer (with those sections "waking") comes back at once while the
+ // wake runs behind it (grid-reads-without-waking issue 03). A wake never joins a listing already
+ // out: that one was built before the wake began, and would not say "waking".
+ const rowState = payload.rowState === true
+ const wake = Array.isArray(payload.wake)
+ ? payload.wake.filter((name): name is string => typeof name === 'string' && !!name.trim()).map((name) => name.trim()).slice(0, MAX_WAKES_PER_ASK)
+ : []
+ try {
+ const gridName = await core.account.privateGridName()
+ const inFlight = listing
+ const grids = wake.length
+ ? listAllGridModels(gridName, { wake })
+ : inFlight && inFlight.gridName === gridName
+ ? inFlight.grids
+ : (listing = {
+ gridName,
+ grids: listAllGridModels(gridName).finally(() => {
+ if (listing?.gridName === gridName) listing = null
+ }),
+ }).grids
+ return gridModelsPayload(gridName, await grids, rowState)
+ } catch {
+ return { error: 'GRID_MODELS_FAILED' }
+ }
+ },
+
+ // Runtime Model/Effort: the choices an agent's engine offers, or every live agent's.
+ models_list: async (payload) => {
+ const sessionId = typeof payload.agentId === 'string' && payload.agentId ? payload.agentId : undefined
+ try {
+ const models = await core.agents.runtimeModels(sessionId)
+ return {
+ // The device derives labels from the opaque runtime-v1 id. Omitting the duplicate
+ // displayName keeps the encrypted picker response below its 16 KiB decrypt cap.
+ models: payload.compact === true
+ ? compactRuntimePickerModels(models, sessionId, payload.pickerMode, payload.selectedModel)
+ : models,
+ }
+ } catch (error) {
+ // Logged and answered as the socket did while this was a case of its own.
+ console.error('[backend] dispatch models_list failed:', error)
+ return { error: 'INTERNAL' }
+ }
+ },
+
+ grid_fleet_models_list: localModel('grid_fleet_models_list'),
+ grid_fleet_model_download: localModel('grid_fleet_model_download'),
+ grid_fleet_model_start: localModel('grid_fleet_model_start'),
+ grid_fleet_model_stop: localModel('grid_fleet_model_stop'),
+ }
+}
+
+/**
+ * How many models the DEVICE picker may receive. It has room for 48 (`models[48]` in ui_habitat.c) and
+ * is handed half of that, so the list it draws is never one it was not built for; the web picker is
+ * unbounded and still gets everything.
+ */
+const DEVICE_PICKER_MAX_MODELS = 24
+
+export function compactRuntimePickerModels(
+ models: RuntimeModelOption[],
+ sessionId: string | undefined,
+ pickerMode: unknown,
+ selectedModel: unknown,
+): Array<{ id: string }> {
+ const compact = models.map(({ id }) => ({ id }))
+ if ((pickerMode !== 'model' && pickerMode !== 'effort') || !sessionId) return compact
+
+ const profiles = models.flatMap((item) => {
+ const profile = parseRuntimeProfile(item.id)
+ return profile?.sessionId === sessionId ? [{ item, profile }] : []
+ })
+ const selected = parseRuntimeProfile(selectedModel)
+ const current = selected?.sessionId === sessionId ? selected : null
+
+ if (pickerMode === 'effort') {
+ if (!current) return []
+ const seen = new Set<string>()
+ return profiles.flatMap(({ item, profile }) => {
+ if (profile.model !== current.model || profile.effort === 'auto' || seen.has(profile.effort)) return []
+ seen.add(profile.effort)
+ return [{ id: item.id }]
+ })
+ }
+
+ const byModel = new Map<string, typeof profiles>()
+ for (const entry of profiles) {
+ const group = byModel.get(entry.profile.model) ?? []
+ group.push(entry)
+ byModel.set(entry.profile.model, group)
+ }
+ const rows = [...byModel.values()].map((group) => {
+ const target = group.find(({ profile }) => current && profile.effort === current.effort)
+ ?? group.find(({ profile }) => profile.effort === 'auto')
+ ?? group[0]
+ return { id: target.item.id, model: target.profile.model }
+ })
+ // Top N only. The picker is a scroll wheel on a 1.9" round screen, and a 49-row one was enough to stall
+ // the device's the device UI task into a task-watchdog reset; devin alone publishes 72
+ // models. The catalog arrives in the engine's own order — its curated/most-used first — so "top" is that
+ // order, with the model the agent is RUNNING pinned in front so the list can never hide it.
+ const ordered = current
+ ? [...rows].sort((a, b) => Number(b.model === current.model) - Number(a.model === current.model))
+ : rows
+ return ordered.slice(0, DEVICE_PICKER_MAX_MODELS).map(({ id }) => ({ id }))
}
diff --git a/cli/src/services/searchProcess.spec.ts b/cli/src/services/searchProcess.spec.ts
index 4098f3728..ddd967d3d 100644
--- a/cli/src/services/searchProcess.spec.ts
+++ b/cli/src/services/searchProcess.spec.ts
@@ -121,12 +121,16 @@ describe('search in its own process', () => {
expect(api.agents.advertised()).toEqual([])
expect(api.agents.terminalAvailable('live')).toBe(false)
api.agents.sync(live as never)
+ await expect(api.agents.runtimeModels('live')).resolves.toEqual([])
expect(api.transcripts.databaseHistory({ engine: 'claude' } as never)).toBeUndefined()
expect(api.external.sessions.list()).toEqual([])
await expect(api.account.mintGridName()).resolves.toBeNull()
await expect(api.account.accessToken()).rejects.toThrow('search holds no credential')
+ await expect(api.account.privateGridName()).resolves.toBeNull()
+ expect(api.account.machineName()).toBeNull()
api.clients.viewerChanged('live')
api.clients.gridNamed('grid')
+ api.clients.gridModelsChanged()
api.clients.dshInstallStatus({ phase: 'clone' })
})
diff --git a/cli/src/services/searchProcess.ts b/cli/src/services/searchProcess.ts
index 8468b733e..f86cbbfb5 100644
--- a/cli/src/services/searchProcess.ts
+++ b/cli/src/services/searchProcess.ts
@@ -48,15 +48,21 @@ export function searchCoreApi(
advertised: () => [],
terminalAvailable: () => false,
sync: () => {},
+ runtimeModels: async () => [],
},
transcripts: { databaseHistory },
external: {
sessions: new ExternalSessions({ providers, excluded: [dataDir], log: console.warn }),
open: new OpenSessions({ providers, log: console.warn }),
},
- // Search holds no credential and talks to no window: these are never asked of it.
- account: { mintGridName: async () => null, accessToken: () => Promise.reject(new Error('search holds no credential')) },
- clients: { viewerChanged: () => {}, gridNamed: () => {}, dshInstallStatus: () => {} },
+ // Search holds no credential, runs no model and talks to no window: these are never asked of it.
+ account: {
+ mintGridName: async () => null,
+ accessToken: () => Promise.reject(new Error('search holds no credential')),
+ privateGridName: async () => null,
+ machineName: () => null,
+ },
+ clients: { viewerChanged: () => {}, gridNamed: () => {}, gridModelsChanged: () => {}, dshInstallStatus: () => {} },
}
}
diff --git a/cli/src/services/viewersProcess.spec.ts b/cli/src/services/viewersProcess.spec.ts
index 2cc3c413e..2c8e54441 100644
--- a/cli/src/services/viewersProcess.spec.ts
+++ b/cli/src/services/viewersProcess.spec.ts
@@ -229,7 +229,11 @@ describe('the viewers in their own process', () => {
await expect(api.external.open.fresh()).resolves.toEqual(new Map())
await expect(api.account.mintGridName()).resolves.toBeNull()
await expect(api.account.accessToken()).rejects.toThrow('the viewers hold no credential')
+ await expect(api.account.privateGridName()).resolves.toBeNull()
+ expect(api.account.machineName()).toBeNull()
+ await expect(api.agents.runtimeModels()).resolves.toEqual([])
api.clients.gridNamed('grid')
+ api.clients.gridModelsChanged()
api.clients.dshInstallStatus({ phase: 'clone' })
})
diff --git a/cli/src/services/viewersProcess.ts b/cli/src/services/viewersProcess.ts
index 499379ea0..a020a9cdf 100644
--- a/cli/src/services/viewersProcess.ts
+++ b/cli/src/services/viewersProcess.ts
@@ -58,14 +58,20 @@ export function viewersCoreApi(dataDir: string, sessions: ReadonlyMap<string, Re
advertised: () => [],
terminalAvailable: () => true,
sync: (session) => tell(session.agentId),
+ runtimeModels: async () => [],
},
transcripts: { databaseHistory: () => undefined },
external: {
sessions: { list: () => [], scan: async () => [] },
open: { known: () => new Map(), fresh: async () => new Map() },
},
- account: { mintGridName: async () => null, accessToken: () => Promise.reject(new Error('the viewers hold no credential')) },
- clients: { viewerChanged: tell, gridNamed: () => {}, dshInstallStatus: () => {} },
+ account: {
+ mintGridName: async () => null,
+ accessToken: () => Promise.reject(new Error('the viewers hold no credential')),
+ privateGridName: async () => null,
+ machineName: () => null,
+ },
+ clients: { viewerChanged: tell, gridNamed: () => {}, gridModelsChanged: () => {}, dshInstallStatus: () => {} },
}
}
diff --git a/cli/src/testing/fakeCore.ts b/cli/src/testing/fakeCore.ts
index c696b2c0a..9c7a87a77 100644
--- a/cli/src/testing/fakeCore.ts
+++ b/cli/src/testing/fakeCore.ts
@@ -19,6 +19,7 @@ export function fakeCore(over: Overrides = {}): CoreApi {
advertised: vi.fn(() => []),
terminalAvailable: vi.fn(() => false),
sync: vi.fn(),
+ runtimeModels: vi.fn(async () => []),
...over.agents,
},
transcripts: { databaseHistory: vi.fn(() => undefined), ...over.transcripts },
@@ -27,7 +28,13 @@ export function fakeCore(over: Overrides = {}): CoreApi {
open: { known: vi.fn(() => new Map()), fresh: vi.fn(async () => new Map()) },
...over.external,
},
- account: { mintGridName: vi.fn(async () => null), accessToken: vi.fn(async () => 'token'), ...over.account },
- clients: { viewerChanged: vi.fn(), gridNamed: vi.fn(), dshInstallStatus: vi.fn(), ...over.clients },
+ account: {
+ mintGridName: vi.fn(async () => null),
+ accessToken: vi.fn(async () => 'token'),
+ privateGridName: vi.fn(async () => null),
+ machineName: vi.fn(() => null),
+ ...over.account,
+ },
+ clients: { viewerChanged: vi.fn(), gridNamed: vi.fn(), gridModelsChanged: vi.fn(), dshInstallStatus: vi.fn(), ...over.clients },
}
}
diff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.md
index faebe7f83..d081500d1 100644
--- a/docs/design/2026-10-03-harnessd.md
+++ b/docs/design/2026-10-03-harnessd.md
@@ -490,6 +490,11 @@ Done so far:
members only the apps called, and the store's `dsh_list`, `dsh_install`, `dsh_update` and
`dsh_remove` (`services/store.ts`, a service with no port). Five `BackendSocket` slots went with
them.
+ - Then models' `grid_models_list`, `models_list` and the Model Manager's `grid_fleet_models_list`,
+ `_download`, `_start` and `_stop` (`services/models.ts`), with two more slots. A request reaches a
+ service without its connection, so the grid commands (`grid_fleet_run`, `grid_fleet_cancel`), each
+ its connection's job, stay in the socket with their handshake (`grid_fleet_capabilities`), as does
+ the `grid_models_changed` push, sent to each window in the form it asked for.
- Adding a request to a feature now touches only that feature's service.
- **A floor under every engine's whole reads** (lib/transcriptTail.ts `tailFileCapped`). Claude Code
and Codex read pages. Every other engine still read its transcript whole, in three places that run