feat(local-model): the coding agent is chosen per session, with Pi and DeepSeek Harness

An installed model knew one coding agent: the one it was installed with.
The launch check never asked about the agent, only about the endpoint
and the model, so nothing stopped a second one. What was missing is the
list, and a way to get an agent that is not on the Mac yet.

The install view gains `harnesses`: every catalogue harness the model in
use can be run with, each with how it stands (`ready`, `installable`,
`installing`, `setup-needed`), the literal install command, a readiness
mark for scripts, and the actions the server accepts for it. `cli` stays
the default agent, and names one that can be started whenever one can,
so a client that reads only `cli` never starts an agent that is gone.

`POST /api/local-model/agent { cli }` installs one more agent beside an
installed model. It is not a job and not an event of the reducer: the
state stays `installed` whatever the installer does, so a failed npm
install never blocks a session on a model that works. Same install slot
and same compiled stock command as the `agent` step, one at a time, the
outcome in memory only.

The catalogue:
- `pi` is no longer gated. Its two open questions were answered by a run
  against a real model (the `name:tag` id reaches the runtime unchanged;
  its first request is 2,096 tokens), and Codeman now passes it the
  window. Its minimum is 65536 like the others: nothing below was run,
  and no model row offers less, so no Mac's pick changes.
- `deepseek` is new. It is a launcher: its install command leaves it
  with nothing to launch, so it is never reported installable (read off
  `discovery.launcherProfile`, not the id) and a Mac is offered it only
  where it was already set up. Its tool round trip was seen on a
  scripted endpoint only; the header says how to gate it again.
- a row may carry `readyMark`; `opencode` has the measured one.

A switch no longer refuses for the agent a model was checked with when
another agent that can run it is on the Mac.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-11 09:53:54 +01:00
1 parent a5a1b6f9a2
commit d22bfaaaaf
14 files changed
+1089 -71

No files matched your search

+49 -12
View File
@@ -13,10 +13,11 @@
* computed from that model's architecture. test/local-model-catalog.test.ts recomputes every
* computed figure from a table of architectures and refuses a row that is not in it, so a
* new row needs its model's config.json read, not a figure borrowed from a neighbour.
* - A harness is a ROW (`{ cli, minContext, rank, gated?, slowFirstTurn? }`). The `cli` is a CLI
* registry id as data: no code may compare it with a literal. Whether that CLI exists and can
* take a custom model is checked against the registry at call time, in
* src/local-model/harness-facts.ts, never here.
* - A harness is a ROW (`{ cli, minContext, rank, gated?, slowFirstTurn?, readyMark? }`). The
* `cli` is a CLI registry id as data: no code may compare it with a literal. Whether that CLI
* exists, can take a custom model and can be installed from here is checked against the
* registry at call time, in src/local-model/harness-facts.ts, never here. HARNESSES below
* says what was measured for each row, and where.
* - The schema checks more than fields. Across rows it requires: unique model ids and ranks,
* unique harness `cli`s and ranks, `contexts` largest first, a `tested` entry naming a harness
* row, and `chips` most specific first.
@@ -56,14 +57,45 @@
* | | | faster than the run without it |
* | `tested` | our own run of the model with a harness on a real Mac | one entry, with its measured speed |
* | `minMacosMajor` | the runtime's documentation (macOS 14) | yes, by the facts review |
* | harness `minContext` | 65536: the runtime's documentation for OpenCode ("64k or | 65536 yes. 32768 no, which is why |
* | | higher"), and OpenCode v1.19.5's own fixed prompt (about | that row is gated |
* | | 8,000 to 10,000 tokens) and compaction rule: at 32768 it | |
* | harness `minContext` | 65536: the runtime's documentation for OpenCode ("64k or | 65536 yes. Nothing was run below it, |
* | | higher"), and OpenCode v1.19.5's own fixed prompt (about | and no model row offers less, so |
* | | 8,000 to 10,000 tokens) and compaction rule: at 32768 it | every row says 65536 (HARNESSES) |
* | | leaves 14,000 to 16,000 tokens, less than one default | |
* | | 2,000-line file read | |
* | `chips` | the bandwidth column of | yes, read at the source for this |
* | | https://github.com/ggml-org/llama.cpp/discussions/4167 | file |
*
* HARNESSES. What each harness row rests on. The runs of 2026-11-10 were made on Linux through
* Codeman's own custom model launch, against a real model (Ollama 0.13.0, a 1.7B model on a CPU)
* where it says REAL, and against a scripted endpoint that replays that runtime's stream of a
* tool call where it says SCRIPTED. A scripted run proves the harness's side of the protocol,
* never what a model does with its prompt. None of them ran on a Mac or on the pinned runtime.
*
* | Harness | First request (fixed prompt) | Tool round trip | `readyMark` |
* |------------|-----------------------------------|----------------------------------------|--------------------------------------|
* | `opencode` | 8,115 tokens, 11 tools (v1.19.5; | SCRIPTED (v1.19.5). REAL in our own | `┃`: the first painted frame holds |
* | | more with user-level skills) | run on a Mac, see the `tested` entry | the composer; a prompt sent at the |
* | | | | mark was submitted, 2 of 2 launches |
* | `claude` | about 36,000 tokens, which is | not run again for this table (the row | none here: the CLI registry's |
* | | `slowFirstTurn` | shipped in 1.50.0) | `composerReadyMark` stands |
* | `pi` | 2,096 tokens, 4 tools (v1.5.2, | REAL (v1.5.2): tool call, tool result, | none: its composer rules are painted |
* | | the runtime's own count) | final answer, the file on disk | before it takes Enter (2 of 2 |
* | | | | prompts sent at the rule stayed |
* | | | | unsent), and it draws nothing that |
* | | | | is a contract once it does |
* | `deepseek` | about 7,650 tokens, 25 tools | SCRIPTED only (0.1.1-rc.2 with dsh-TUI | none here: the CLI registry's `❯` |
* | | (0.1.1-rc.2) | 0.10.0-beta.1, not today's versions). | stands; a prompt sent at it was |
* | | | REAL: the launch and the model name | submitted, 2 of 2 launches |
* | | | reached the runtime, no turn finished | |
*
* Every one of them sent the `name:tag` model id unchanged. `pi` was gated until 2026-11-10 for
* two open questions, both answered by its run: the id (above), and the window, which Codeman
* now passes to it (src/custom-model-injection.ts; without it pi takes an unknown model for a
* 128,000-token one). `deepseek` is a LAUNCHER: its install command leaves it with nothing to
* launch, so src/local-model/harness-facts.ts never reports it installable and a Mac is offered
* it only where it was already set up. It is offered on a scripted run and one unfinished real
* one: gate the row again (`gated: true`) should a run on a Mac not finish a turn.
*
* MEMORY. The need is `bytes + heldBytesPerToken x context + overheadBytes + peakExtraBytes`
* (src/local-model/recommend.ts). The two runners spend memory differently, so a row's three
* figures are filled by the rule of its runner:
@@ -222,6 +254,8 @@ const LocalHarnessEntrySchema = z
rank: z.number().int(),
gated: z.boolean().optional(),
slowFirstTurn: z.boolean().optional(),
// One literal a script waits for: no white space, so the terminal's own spacing cannot split it.
readyMark: z.string().min(1).max(64).regex(/^\S+$/).optional(),
})
.strict();
@@ -528,13 +562,16 @@ export const STOCK_LOCAL_MODEL_CATALOG: LocalModelCatalog = {
tested: [],
},
],
// The higher rank wins when a model can run with more than one harness.
// The higher rank wins when a model can run with more than one harness. What each row rests
// on is the HARNESSES table of the file header.
harnesses: [
{ cli: 'opencode', minContext: 65536, rank: 3 },
{ cli: 'opencode', minContext: 65536, rank: 4, readyMark: '┃' },
// Sends a fixed prompt of about 36,000 tokens, so the first answer takes minutes.
{ cli: 'claude', minContext: 65536, rank: 2, slowFirstTurn: true },
// Gated: neither its 32768 minimum nor its handling of a `name:tag` model id is verified.
{ cli: 'pi', minContext: 32768, rank: 1, gated: true },
{ cli: 'claude', minContext: 65536, rank: 3, slowFirstTurn: true },
// The smallest first request of the four (2,096 tokens). No `readyMark`: see HARNESSES.
{ cli: 'pi', minContext: 65536, rank: 2 },
// A launcher: offered only where it is already set up, never installed from here.
{ cli: 'deepseek', minContext: 65536, rank: 1 },
],
// Memory bandwidth in GB/s, most specific first. Only chips that shipped and are in the source
// table are listed. Where one name covers two bandwidths (M3 Max 300 or 400, M4 Max 410 or 546,
+13 -5
View File
@@ -9,14 +9,18 @@
* mechanism (`capabilities.customModelInjection.kind` is `unsupported`);
* - reports `label` from the registry entry and `installed` from the same probe the page
* render and `GET /api/clis` use (`utils/cli-installed-probes.ts`), asked once per build;
* - reports `installable` only for a STOCK CLI that is not installed and whose install
* command for this platform needs no administrator rights (`installCommandNeedsAdmin()`),
* and then carries that command as `installCommand`, so the consent screen can print the
* - reports `installable` only for a STOCK CLI that is not installed, whose install command
* for this platform needs no administrator rights (`installCommandNeedsAdmin()`) and which
* is not a launcher (`discovery.launcherProfile`: its install command leaves it with nothing
* to launch, and what it launches is third-party code with a consent of its own in the Run
* menu), and then carries that command as `installCommand`, so the consent screen can print the
* literal text. The command is the COMPILED stock one (`stockInstallCommand()`), never the
* one a `clis.json` override supplies: it is exactly what the install job would run, and
* an override file must not be able to change that;
* - supplies `acceptsModelId` (`cliAcceptsCustomModelId()`), the test a custom model launch
* applies to the model id before anything is written.
* applies to the model id before anything is written;
* - passes on the registry's `capabilities.composerReadyMark` as `readyMark` when it holds no
* white space (a script waits for it as one literal).
*
* Rules:
* - No CLI id is compared with a literal here or anywhere: the id comes from the catalogue
@@ -156,19 +160,23 @@ export async function buildHarnessFacts(
}
let installCommand: string | undefined;
if (!installed && entry.stock) {
// A launcher (`discovery.launcherProfile`) is not startable after its install command: it
// still needs something to launch, which that command does not install.
if (!installed && entry.stock && entry.discovery.launcherProfile === undefined) {
const command = resolved.installCommandFor(entry);
if (command !== undefined && command.trim() !== '' && !installCommandNeedsAdmin(command)) {
installCommand = command;
}
}
const registryMark = entry.capabilities.composerReadyMark;
facts.push({
cli: row.cli,
label: entry.label,
installed,
installable: installCommand !== undefined,
...(installCommand !== undefined ? { installCommand } : {}),
...(registryMark !== undefined && !/\s/.test(registryMark) ? { readyMark: registryMark } : {}),
acceptsModelId: (modelId: string) => cliAcceptsCustomModelId(entry, modelId),
});
}
+280 -29
View File
@@ -33,7 +33,7 @@
* | `installed` | the runtime, the private home, the model | stays `installed`; the | refused (409) | start a session, start or stop |
* | | in use and every other model of the | runtime is started again | | the runtime, install (add a |
* | | library | when it was not stopped | | model), use, delete a model, |
* | | | | | remove |
* | | | | | install a coding agent, remove |
*
* The step table. Every step is IDEMPOTENT, so Resume and "Try again" re-enter the step the
* job stopped in; they first make sure the runtime runs when the step needs it.
@@ -96,6 +96,16 @@
* server's own total.
* - No CLI id is compared with a literal here: the coding agent is a catalogue row plus a
* `HarnessFact`.
* - The coding agent is chosen PER SESSION. The state file's `cli` is the agent of the job
* and, once installed, the default one; the view's `harnesses` lists every catalogue harness
* the model in use can be run with (`buildHarnessRows()`). The launch check
* (`launchRefusalMessage()`) asks about the endpoint and the model, never about the agent.
* - Installing one more coding agent beside an installed model (`installAgent()`,
* `POST /api/local-model/agent`) is NOT a job and not an event of the reducer: the state
* stays `installed` whatever the installer does, so a failed `npm install` never blocks a
* session on a model that works. It runs in the background through the same install slot
* and the same stock command as the `agent` step, one at a time, and what it leaves behind
* is in memory only (`LocalModelHarnessRow.status` `installing`, or its `error`).
*
* The service (`createLocalModelService()`), beyond the reducer:
* - Verbs run one at a time on one queue, so two installs sent together cannot both find the
@@ -161,6 +171,7 @@ import {
type HardwareFacts,
type HarnessFact,
type LocalModelAction,
type LocalModelAgentRequest,
type LocalModelCatalog,
type LocalModelConflict,
type LocalModelDeleteRequest,
@@ -172,6 +183,8 @@ import {
type LocalModelInstallState,
type LocalModelInstallStep,
type LocalModelInstallView,
type LocalModelHarnessRow,
type LocalModelHarnessStatus,
type LocalModelLibraryAction,
type LocalModelLibraryEntry,
type LocalModelLibraryRow,
@@ -261,8 +274,9 @@ export interface InstallStateRow {
* Every action a client may take in this state. The view's `actions` is this list narrowed
* by what is live (`allowedActions()`): `start` only while the runtime is down, `stop` and
* `start-session` only while it runs, `start-session` only when nothing blocks a session,
* `use` and `delete-model` only while a row of the library offers them, and in `idle`
* `remove` only while the model store still holds something.
* `use` and `delete-model` only while a row of the library offers them, `install-agent` only
* while a row of `harnesses` offers it, and in `idle` `remove` only while the model store
* still holds something.
*/
actions: readonly LocalModelAction[];
}
@@ -312,7 +326,7 @@ export const INSTALL_STATE_TABLE: Readonly<Record<LocalModelInstallState, Instal
'Stays installed. The runtime is started again unless the person had stopped it, and the digest is read again: a changed digest or a port taken by another program shows as an error on the view and blocks a session, the state stays installed.',
afterCancel: null,
cancel: 'Refused: no job is running.',
actions: ['start-session', 'start', 'stop', 'install', 'use', 'delete-model', 'remove'],
actions: ['start-session', 'start', 'stop', 'install', 'use', 'delete-model', 'install-agent', 'remove'],
},
};
@@ -695,6 +709,13 @@ export interface InstallLiveFacts {
storeBytes?: number;
/** The catalogue's label per model id, for a model the runtime lists and the state file does not know. */
labels?: Readonly<Record<string, string>>;
/**
* The coding agents of the model in use (`buildHarnessRows()`), without their
* `start-session` action: the view adds it once it knows whether a session can be started.
* Absent while nothing is installed, and when the catalogue or the CLI registry could not be
* read: the view then lists none.
*/
harnesses?: readonly LocalModelHarnessRow[];
}
/** An error code with its sentence, its slots and its one next step. Pure. */
@@ -819,6 +840,91 @@ export function buildLibraryRows(state: LocalModelStateFile, live: InstallLiveFa
return rows;
}
/** What the service remembers of installs of a coding agent beside an installed model. In memory only. */
export interface AgentInstallMemory {
/** The CLI id of the agent whose installer runs right now. */
running?: string;
/** The agents whose last install failed, by CLI id, with the end of what the installer printed. */
failed?: Readonly<Record<string, { detail?: string }>>;
}
/**
* The coding agents the model in use can be run with: every catalogue harness that is not
* gated, has a fact, works at the model's context and accepts its name. The default agent
* first, then the higher catalogue rank. The default is the agent of the installation
* (`state.cli`) while it can be started, else the first one that can, so a client that names
* no agent never starts one that is gone. Rows carry no `start-session` action yet: whether a
* session can be started at all is the view's knowledge (`buildInstallView()`). Empty unless
* the state is `installed`. Pure, and it compares no CLI id with a literal.
*/
export function buildHarnessRows(
state: LocalModelStateFile,
catalog: Pick<LocalModelCatalog, 'harnesses'>,
facts: readonly HarnessFact[],
memory: AgentInstallMemory = {}
): LocalModelHarnessRow[] {
if (state.state !== 'installed' || !state.model) return [];
const model = state.model;
const ranked: Array<{ rank: number; row: LocalModelHarnessRow }> = [];
for (const entry of catalog.harnesses) {
if (entry.gated) continue;
const fact = facts.find((candidate) => candidate.cli === entry.cli);
if (!fact || model.context < entry.minContext) continue;
let accepts = false;
try {
accepts = fact.acceptsModelId(model.id);
} catch {
accepts = false;
}
if (!accepts) continue;
const installing = !fact.installed && memory.running === entry.cli;
const status: LocalModelHarnessStatus = fact.installed
? 'ready'
: installing
? 'installing'
: fact.installable
? 'installable'
: 'setup-needed';
const failure = status === 'installable' ? memory.failed?.[entry.cli] : undefined;
const readyMark = entry.readyMark ?? fact.readyMark;
ranked.push({
rank: entry.rank,
row: {
cli: entry.cli,
cliLabel: fact.label,
default: false,
status,
...((status === 'installable' || status === 'installing') && fact.installCommand !== undefined
? { installCommand: fact.installCommand }
: {}),
...(entry.slowFirstTurn ? { slowFirstTurn: true } : {}),
...(readyMark !== undefined ? { readyMark } : {}),
...(failure
? {
error: {
message: { text: LOCAL_MODEL_INSTALL_LABELS.errHarnessFailed, vars: { cli: fact.label } },
...(failure.detail ? { detail: failure.detail } : {}),
},
}
: {}),
// One installer at a time: while one runs, no other agent is offered for install.
actions: status === 'installable' && memory.running === undefined ? ['install'] : [],
},
});
}
ranked.sort((a, b) => b.rank - a.rank);
const rows = ranked.map((entry) => entry.row);
const chosen = state.cli?.id;
const fallback =
rows.find((row) => row.cli === chosen && row.status === 'ready') ??
rows.find((row) => row.status === 'ready') ??
rows.find((row) => row.cli === chosen);
if (!fallback) return rows;
fallback.default = true;
return [fallback, ...rows.filter((row) => row !== fallback)];
}
/**
* The actions the server accepts right now: the state row's list narrowed by what is live.
* `rows` is `buildLibraryRows()` of the same two arguments, for a caller that has it. Pure.
@@ -833,12 +939,18 @@ export function allowedActions(
return actions.filter((action) => {
if (action === 'use') return rows.some((row) => row.actions.includes('use'));
if (action === 'delete-model') return rows.some((row) => row.actions.includes('delete'));
if (action === 'install-agent') return (live.harnesses ?? []).some((row) => row.actions.includes('install'));
// From idle a Remove only clears what an earlier one kept: offered while there is something.
if (state.state === 'idle') return action !== 'remove' || (live.storeBytes ?? 0) > 0;
if (state.state !== 'installed') return true;
if (action === 'start') return !live.running;
if (action === 'stop') return live.running;
if (action === 'start-session') return live.running && !blocked;
if (action === 'start-session') {
// With the agents known, a session needs one that can be started. Unknown (the registry
// could not be read) is not "none": the launch itself says what is missing then.
const anAgent = live.harnesses === undefined || live.harnesses.some((row) => row.status === 'ready');
return live.running && !blocked && anAgent;
}
return true;
});
}
@@ -881,6 +993,7 @@ export function buildInstallView(state: LocalModelStateFile, live: InstallLiveFa
folder: live.folder,
library,
...(live.storeBytes !== undefined ? { store: { bytes: live.storeBytes, gb: formatGb(live.storeBytes) } } : {}),
harnesses: [],
actions: allowedActions(state, live, library),
};
if (state.state === 'idle') return view;
@@ -907,6 +1020,18 @@ export function buildInstallView(state: LocalModelStateFile, live: InstallLiveFa
if (state.connection) view.endpointId = state.connection.endpointId;
const blocker = installedBlocker(state, live);
if (blocker) view.error = blocker;
// An agent that can be started offers a session exactly when the model does.
const canStart = view.actions.includes('start-session');
view.harnesses = (live.harnesses ?? []).map((row) =>
row.status === 'ready' && canStart ? { ...row, actions: ['start-session', ...row.actions] } : { ...row }
);
// The default agent is the one a client that names none starts: never one that is gone
// while another can be started.
const preferred = view.harnesses.find((row) => row.default);
if (preferred) {
view.cli = preferred.cli;
view.cliLabel = preferred.cliLabel;
}
return view;
}
@@ -1053,8 +1178,12 @@ export interface LocalModelServiceDeps {
/** The install as the routes and the server see it. One per process. */
export interface LocalModelService {
/** The `install` object of the GET. Never throws. */
view(): Promise<LocalModelInstallView>;
/**
* The `install` object of the GET. Never throws. `harnessFacts`: the facts a caller already
* built for the same answer (the GET builds them for the recommendation), so they are not
* built twice; without them the service builds its own.
*/
view(options?: { harnessFacts?: readonly HarnessFact[] }): Promise<LocalModelInstallView>;
/**
* Whether a session may be started on the saved endpoint `endpointId` right now. `undefined`:
* yes, which is also the answer for every endpoint that is not this feature's. Otherwise the
@@ -1105,6 +1234,14 @@ export interface LocalModelService {
* runtime is down.
*/
deleteLibraryModel(request: LocalModelDeleteRequest, actor?: LocalModelActor): Promise<LocalModelVerbResult>;
/**
* `POST /api/local-model/agent`: install one more coding agent beside an installed model.
* Answers once the installer is STARTED; it runs on in the server, and the view's
* `harnesses` says how it stands. Not a job: the state stays `installed` and sessions go on.
* The agent must be a row of `harnesses` that offers `install`; one that can already be
* started answers with the view as it is. One installer at a time (409 `job-running`).
*/
installAgent(request: LocalModelAgentRequest, actor: LocalModelActor): Promise<LocalModelVerbResult>;
/** `POST /api/local-model/cancel`. Answers once the step in flight was interrupted. */
cancel(): Promise<LocalModelVerbResult>;
/**
@@ -1148,7 +1285,7 @@ export interface LocalModelService {
shutdown(): Promise<void>;
/** What the runtime supervisor reports through `onChange`: the pid of a child the moment it is spawned, a new pid after a restart, the budget line. Stored, never thrown. */
onRuntimeChange(status: RuntimeStatus): void;
/** Resolves once no job is in flight. For `shutdown()` and for tests. */
/** Resolves once no job is in flight and no coding agent is being installed. For `shutdown()` and for tests. */
idle(): Promise<void>;
}
@@ -1260,6 +1397,13 @@ class LocalModelServiceImpl implements LocalModelService {
/** What the model store last took on disk, and when. */
private storeCache: { at: number; bytes: number } | undefined;
private storeRefresh: Promise<void> | undefined;
/**
* The install of one more coding agent beside an installed model, while its installer runs.
* Not a job: the state file never hears of it, and the state stays `installed`.
*/
private agentInstall: { cli: string; controller: AbortController; promise: Promise<void> } | undefined;
/** The agents whose last such install failed, with the end of what the installer printed. Forgotten at a restart. */
private readonly agentFailures = new Map<string, { detail?: string }>();
constructor(deps: Partial<LocalModelServiceDeps>) {
const pin = deps.pin ?? OLLAMA_RUNTIME_PIN;
@@ -1730,8 +1874,30 @@ class LocalModelServiceImpl implements LocalModelService {
// ---------- The view ----------
private async liveFacts(state: LocalModelStateFile): Promise<InstallLiveFacts> {
/** What `buildHarnessRows()` is told about agent installs: the one that runs, and the ones that failed. */
private agentMemory(): AgentInstallMemory {
return {
...(this.agentInstall ? { running: this.agentInstall.cli } : {}),
...(this.agentFailures.size > 0 ? { failed: Object.fromEntries(this.agentFailures) } : {}),
};
}
private async liveFacts(
state: LocalModelStateFile,
harnessFacts?: readonly HarnessFact[]
): Promise<InstallLiveFacts> {
const status = this.deps.supervisor.status();
let harnesses: LocalModelHarnessRow[] | undefined;
if (state.state === 'installed') {
try {
const catalog = this.deps.getCatalog();
const facts = harnessFacts ?? (await this.deps.buildHarnessFacts(catalog));
harnesses = buildHarnessRows(state, catalog, facts, this.agentMemory());
} catch (err) {
// Unknown, not "none": the view then lists no agent and still offers a session.
console.warn(`[local-model] The coding agents could not be listed: ${getErrorMessage(err)}`);
}
}
let developerTools: LocalModelDeveloperTools = 'unknown';
try {
developerTools = await this.deps.checkDeveloperTools();
@@ -1767,10 +1933,11 @@ class LocalModelServiceImpl implements LocalModelService {
...(tags ? { tags } : {}),
...(this.storeCache ? { storeBytes: this.storeCache.bytes } : {}),
...(labels ? { labels } : {}),
...(harnesses ? { harnesses } : {}),
};
}
async view(): Promise<LocalModelInstallView> {
async view(options: { harnessFacts?: readonly HarnessFact[] } = {}): Promise<LocalModelInstallView> {
try {
await this.recovered();
let state = await this.currentState();
@@ -1793,7 +1960,7 @@ class LocalModelServiceImpl implements LocalModelService {
void this.refreshDigest(state).catch(() => {});
}
await this.freshenListings(state);
return buildInstallView(state, await this.liveFacts(state));
return buildInstallView(state, await this.liveFacts(state, options.harnessFacts));
} catch (err) {
console.warn(`[local-model] Could not build the install view: ${getErrorMessage(err)}`);
return buildInstallView(IDLE_LOCAL_MODEL_STATE, {
@@ -1988,9 +2155,11 @@ class LocalModelServiceImpl implements LocalModelService {
const open = this.sessionsOpen(state, liveSessionNames);
if (open) return open;
// The coding agent of the switch: the one this model was checked with, else the one of
// the state, whichever is on this Mac now. A job that stopped while it installed another
// agent leaves the state naming one that may not be there.
// The coding agent of the switch, which becomes the default one: the one this model was
// checked with, else the one of the state, whichever is on this Mac now. A job that
// stopped while it installed another agent leaves the state naming one that may not be
// there. With neither, any agent the model can be run with that is on this Mac: the
// agent is chosen per session, so a switch is not refused for one that was uninstalled.
const agents = [entry.cli, state.cli].filter(
(cli, index, all): cli is { id: string; label: string } =>
cli !== undefined && all.findIndex((other) => other?.id === cli.id) === index
@@ -2002,6 +2171,22 @@ class LocalModelServiceImpl implements LocalModelService {
break;
}
}
if (!cli) {
try {
const catalog = this.deps.getCatalog();
const afterSwitch: LocalModelStateFile = {
schema: 1,
state: 'installed',
model: { id: entry.id, label: entry.label, context: entry.context },
};
const ready = buildHarnessRows(afterSwitch, catalog, await this.deps.buildHarnessFacts(catalog)).find(
(row) => row.status === 'ready'
);
if (ready) cli = { id: ready.cli, label: ready.cliLabel };
} catch {
/* no other agent is known then, and the switch is refused below */
}
}
if (!cli) {
return {
ok: false,
@@ -2031,6 +2216,54 @@ class LocalModelServiceImpl implements LocalModelService {
});
}
installAgent(request: LocalModelAgentRequest, actor: LocalModelActor): Promise<LocalModelVerbResult> {
return this.exclusive(async () => {
await this.settle();
const state = await this.currentState();
if (state.state === 'installing') return conflict('job-running');
if (!INSTALL_STATE_TABLE[state.state].actions.includes('install-agent')) return conflict('wrong-state');
// One installer at a time, whichever agent it is for.
if (this.agentInstall) return conflict('job-running');
// Checked against what the server itself lists right now, before anything runs.
const catalog = this.deps.getCatalog();
const facts = await this.deps.buildHarnessFacts(catalog);
const row = buildHarnessRows(state, catalog, facts).find((candidate) => candidate.cli === request.cli);
if (!row) {
return { ok: false, status: 400, message: { text: LOCAL_MODEL_INSTALL_LABELS.refusedAgentNotOffered } };
}
// Already there (a second click, or it was installed by hand meanwhile): nothing to do.
if (row.status === 'ready') return this.answer();
if (row.status !== 'installable') {
return {
ok: false,
status: 400,
message: { text: LOCAL_MODEL_INSTALL_LABELS.refusedAgentNotInstallable, vars: { cli: row.cliLabel } },
};
}
this.agentFailures.delete(row.cli);
const controller = new AbortController();
const cli = row.cli;
const run = {
cli,
controller,
promise: this.installCodingAgent(cli, actor, controller.signal)
.then((outcome) => {
if (outcome.kind === 'fail') this.agentFailures.set(cli, { detail: outcome.detail });
})
.catch((err: unknown) => {
this.agentFailures.set(cli, { detail: getErrorMessage(err) });
})
.finally(() => {
if (this.agentInstall === run) this.agentInstall = undefined;
}),
};
this.agentInstall = run;
return this.answer();
});
}
deleteLibraryModel(request: LocalModelDeleteRequest, actor?: LocalModelActor): Promise<LocalModelVerbResult> {
return this.exclusive(async () => {
await this.settle();
@@ -2372,6 +2605,8 @@ class LocalModelServiceImpl implements LocalModelService {
const job = this.job;
// The state file keeps saying `installing`: the next start reads that as a paused job.
job?.controller.abort();
// An agent installer beside an installed model: its process group is ended, nothing is recorded.
this.agentInstall?.controller.abort();
// The runtime is signalled at once, not after the job let go: the server may exit soon.
const stopped = Promise.resolve()
.then(() => this.deps.supervisor.stop())
@@ -2385,7 +2620,9 @@ class LocalModelServiceImpl implements LocalModelService {
}
async idle(): Promise<void> {
while (this.job) await this.job.promise.catch(() => {});
while (this.job || this.agentInstall) {
await (this.job ?? this.agentInstall)?.promise.catch(() => {});
}
}
// ---------- The job ----------
@@ -2741,24 +2978,38 @@ class LocalModelServiceImpl implements LocalModelService {
signal: AbortSignal
): Promise<StepOutcome> {
if (!state.cli) return { kind: 'fail', code: 'internal', detail: 'The job has no coding agent.' };
const cli = state.cli.id;
const vars = this.errorVars('harness-failed', state);
const outcome = await this.installCodingAgent(state.cli.id, actor, signal);
if (outcome.kind !== 'fail') return outcome;
return {
kind: 'fail',
code: 'harness-failed',
vars: this.errorVars('harness-failed', state),
detail: outcome.detail,
};
}
/**
* Installs one coding agent with its STOCK command, through the install slot it shares with
* the CLI management routes. What the `agent` step of a job and `installAgent()` both run.
* `done` also when the agent is already there; `fail` carries what to show as the detail.
*/
private async installCodingAgent(
cli: string,
actor: LocalModelActor,
signal: AbortSignal
): Promise<{ kind: 'done' } | { kind: 'cancelled' } | { kind: 'fail'; detail: string }> {
// A resumed job, or an installer that finished before a restart: the binary may be there.
if (await this.deps.recheckCliInstalled(cli)) return DONE;
if (signal.aborted) return CANCELLED;
if (await this.deps.recheckCliInstalled(cli)) return { kind: 'done' };
if (signal.aborted) return { kind: 'cancelled' };
// Only a coding agent of the catalogue, and only its STOCK install command.
if (!this.deps.getCatalog().harnesses.some((row) => row.cli === cli)) {
return { kind: 'fail', code: 'harness-failed', vars, detail: 'Not a coding agent of the local model catalogue.' };
return { kind: 'fail', detail: 'Not a coding agent of the local model catalogue.' };
}
const command = this.deps.stockInstallCommand(cli);
if (!command) {
return { kind: 'fail', code: 'harness-failed', vars, detail: 'No install command for this platform.' };
}
if (!command) return { kind: 'fail', detail: 'No install command for this platform.' };
const release = this.deps.tryBeginCliInstall(cli);
if (!release) {
return { kind: 'fail', code: 'harness-failed', vars, detail: 'Another install of this coding agent is running.' };
}
if (!release) return { kind: 'fail', detail: 'Another install of this coding agent is running.' };
let result: CliInstallResult;
try {
result = await this.deps.runInstallCommand(command, { signal });
@@ -2783,13 +3034,13 @@ class LocalModelServiceImpl implements LocalModelService {
// Even a failed or interrupted install may have left a binary behind: the cached lookups
// are forgotten either way, and what counts is whether the binary is found now.
const installed = await this.deps.recheckCliInstalled(cli);
if (result.aborted || signal.aborted) return CANCELLED;
if (installed) return DONE;
if (result.aborted || signal.aborted) return { kind: 'cancelled' };
if (installed) return { kind: 'done' };
const detail = result.timedOut
? `The installer was stopped after ${Math.round(CLI_INSTALL_TIMEOUT_MS / 1000)} seconds.`
: result.output.slice(-INSTALLER_DETAIL_CHARS).trim() ||
(result.code === 0 ? 'The installer finished, but the program was not found afterwards.' : 'No output.');
return { kind: 'fail', code: 'harness-failed', vars, detail };
return { kind: 'fail', detail };
}
private async stepConnect(state: LocalModelStateFile, signal: AbortSignal): Promise<StepOutcome> {
+86 -5
View File
@@ -211,6 +211,8 @@ export type LocalModelNextStep = 'retry' | 'other-port' | 'next-model' | 'start'
* - `use`: `POST /api/local-model/use`. Listed only while a row of `library` offers it.
* - `delete-model`: `DELETE /api/local-model/models`. Listed only while a row of `library`
* offers it.
* - `install-agent`: `POST /api/local-model/agent`. Listed only while a row of `harnesses`
* offers it.
*/
export type LocalModelAction =
| 'install'
@@ -221,7 +223,8 @@ export type LocalModelAction =
| 'remove'
| 'start-session'
| 'use'
| 'delete-model';
| 'delete-model'
| 'install-agent';
/** Whether the Mac has the developer tools (git, Python) a coding agent calls. */
export type LocalModelDeveloperTools =
@@ -301,7 +304,7 @@ export const LOCAL_MODEL_INSTALL_LABELS = {
refusedNotInLibrary: 'This model is not one of the checked models on this Mac.',
refusedNotOnThisMac: 'This model is not on this Mac.',
deleteFailed: '{model} could not be deleted from this Mac.',
refusedAgentMissing: '{cli} is not installed on this Mac. Install this model again to choose a coding agent.',
refusedAgentMissing: '{cli} is not installed on this Mac. Install a coding agent in the local model window first.',
endpointManaged:
'This endpoint belongs to the model Codeman runs on this Mac. Change or remove it in the local model window.',
endpointIdReserved: 'This endpoint id is reserved for the model Codeman runs on this Mac. Pick another id.',
@@ -461,8 +464,9 @@ export interface LocalModelLoadedFacts {
* | `port` | | once chosen | once chosen | once chosen | yes |
* | `loaded`, `endpointId` | | | | | yes |
*
* `running`, `developerTools`, `runtime`, `folder`, `library` and `actions` are always
* present. `store` is present once the model store was measured.
* `running`, `developerTools`, `runtime`, `folder`, `library`, `harnesses` and `actions` are
* always present. `store` is present once the model store was measured. `harnesses` is empty
* in every state but `installed`.
*/
export interface LocalModelInstallView {
state: LocalModelInstallState;
@@ -473,7 +477,11 @@ export interface LocalModelInstallView {
progress?: LocalModelProgress;
error?: LocalModelInstallError;
model?: LocalModelInstalledModel;
/** The coding agent: a CLI registry id, as data. */
/**
* The coding agent of the job, and on an installed model the DEFAULT one: the agent it was
* installed with, which a client that names no agent starts. A CLI registry id, as data.
* `harnesses` lists every agent a session can be started with.
*/
cli?: string;
/** The coding agent's display label ('OpenCode'). */
cliLabel?: string;
@@ -498,10 +506,74 @@ export interface LocalModelInstallView {
library: LocalModelLibraryRow[];
/** What the model store takes on disk right now. Present in every state once it was measured. */
store?: LocalModelStoreView;
/**
* The coding agents a session on the model in use can be started with: every catalogue
* harness that works at the model's context and accepts its name, the default one (`cli`)
* first, then by catalogue rank. The agent is chosen per session, so a row is not a setting.
* Empty in every state but `installed`.
*/
harnesses: LocalModelHarnessRow[];
/** What the server accepts right now, see `LocalModelAction`. */
actions: LocalModelAction[];
}
/**
* How a coding agent of `harnesses` stands on this Mac.
* - `ready`: it can be started (the same answer the Run menu gets).
* - `installable`: it is not on this Mac, and `POST /api/local-model/agent` installs it with
* `installCommand`.
* - `installing`: that install is running. It runs beside the model: nothing about the model
* in use changes, and a session with another agent can be started meanwhile.
* - `setup-needed`: it is not startable and one command does not make it so (a launcher
* without a profile, or a command that needs an administrator). Its own setup is in the Run
* menu or in a terminal; nothing is offered here.
*/
export type LocalModelHarnessStatus = 'ready' | 'installable' | 'installing' | 'setup-needed';
/** What can be done with one row of `harnesses`: start a session with it, or `POST /api/local-model/agent`. */
export type LocalModelHarnessAction = 'start-session' | 'install';
/** One coding agent the model in use can be run with. Everything a client needs is here, so it compares no CLI id. */
export interface LocalModelHarnessRow {
/** The CLI registry id, as data: the `mode` of a session started with it, and the `cli` of `POST /api/local-model/agent`. */
cli: string;
/** The registry's display label ('OpenCode'). */
cliLabel: string;
/** True on the row `LocalModelInstallView.cli` names. */
default: boolean;
status: LocalModelHarnessStatus;
/**
* The literal command `POST /api/local-model/agent` would run: the STOCK command of the CLI
* registry for this platform. Present exactly when `status` is `installable` or `installing`.
* The consent prints it as it is.
*/
installCommand?: string;
/** The harness row's `slowFirstTurn`: the first answer takes minutes. */
slowFirstTurn?: boolean;
/**
* A literal with no white space the agent's composer draws once it takes input
* (`LocalHarnessEntry.readyMark`, else the CLI registry's `composerReadyMark`): what a script
* waits for on `GET /api/sessions/:id/wait-output` before it sends the first prompt. Absent
* for an agent with no measured mark.
*/
readyMark?: string;
/**
* The last install of this agent through `POST /api/local-model/agent` failed, and it is
* still not on this Mac. Kept in memory only: a restart of the server forgets it.
*/
error?: LocalModelAgentError;
/** What the server accepts for this row right now. */
actions: LocalModelHarnessAction[];
}
/** Why an install of a coding agent from an installed model failed. */
export interface LocalModelAgentError {
/** `errHarnessFailed` with the agent's label. */
message: LocalModelInstallMessage;
/** The end of what the installer printed, as it is (English or the tool's own language). */
detail?: string;
}
/**
* How a model of the library stands.
* - `ready`: checked by Codeman, and the runtime (when it was asked) still lists it with the
@@ -612,6 +684,15 @@ export interface LocalModelDeleteRequest {
modelId: string;
}
/**
* Body of `POST /api/local-model/agent`: a coding agent of `harnesses` to install beside an
* installed model. The click that sends it is the consent to the row's `installCommand`.
*/
export interface LocalModelAgentRequest {
/** `LocalModelHarnessRow.cli` of a row whose `actions` list `install`. */
cli: string;
}
/** The `data` of a 400 the service names: its sentence, for the window to print in its own language. */
export interface LocalModelRefusal {
message: LocalModelInstallMessage;
+22 -2
View File
@@ -207,6 +207,15 @@ export interface LocalHarnessEntry {
gated?: boolean;
/** True: the first answer takes minutes, because the harness sends a very large fixed prompt. */
slowFirstTurn?: boolean;
/**
* A literal with no white space that this harness's composer draws once it takes input, and
* nothing before it does: what a script waits for before it sends the first prompt
* (`GET /api/sessions/:id/wait-output`). Only a mark that was MEASURED on a session started
* on a custom model goes here. Absent: the CLI registry's own
* `capabilities.composerReadyMark` is used when it declares one, and a harness with neither
* has no mark.
*/
readyMark?: string;
}
/**
@@ -248,9 +257,18 @@ export interface HarnessFact {
cli: string;
/** The registry's display label ('OpenCode'). */
label: string;
/** The CLI's binary was found on this machine. */
/**
* The CLI can be started on this machine: the same answer the Run menu gets. For a launcher
* that starts nothing by itself (`discovery.launcherProfile` in the CLI registry) that is its
* binary AND something for it to launch.
*/
installed: boolean;
/** Not installed, but the registry has an install command for this platform that needs no administrator rights. */
/**
* Not installed, but ONE command makes it startable: the registry has an install command for
* this platform that needs no administrator rights, and the CLI is not a launcher. A launcher
* needs a second install the command does not make (a profile, which is third-party code
* with a consent of its own), so it is never installable from here.
*/
installable: boolean;
/**
* The literal install command the install job would run for this CLI: the STOCK
@@ -259,6 +277,8 @@ export interface HarnessFact {
* (`CandidateHarness.installCommand`).
*/
installCommand?: string;
/** The registry's `capabilities.composerReadyMark`, when the CLI declares one and it holds no white space. */
readyMark?: string;
/** Whether this CLI can be launched with that model id (its command-line pattern accepts it). */
acceptsModelId: (modelId: string) => boolean;
}
+2 -2
View File
@@ -1628,8 +1628,8 @@
'此 Mac 目前只使用 {model}。请先在本地模型窗口中切换模型。',
'This model is not one of the checked models on this Mac.': '该模型不在此 Mac 上已检查的模型之列。',
'This model is not on this Mac.': '此 Mac 上没有这个模型。',
'{cli} is not installed on this Mac. Install this model again to choose a coding agent.':
'此 Mac 上没有安装 {cli}。请重新安装此模型,以便选择编程智能体。',
'{cli} is not installed on this Mac. Install a coding agent in the local model window first.':
'此 Mac 上没有安装 {cli}。请先在本地模型窗口中安装一个编程智能体。',
'{model} could not be deleted from this Mac.': '无法从此 Mac 删除 {model}。',
// What the endpoint routes answer for the record of the model on this Mac (shown as a toast).
'This endpoint belongs to the model Codeman runs on this Mac. Change or remove it in the local model window.':
+14 -1
View File
@@ -12,6 +12,7 @@
* | `POST /api/local-model/runtime` | `LocalModelRuntimeRequest` | the install view; 409 |
* | `DELETE /api/local-model` | `LocalModelRemoveRequest` | the install view; 409 |
* | `DELETE /api/local-model/models` | `LocalModelDeleteRequest` | the install view; 400, 409, 500 |
* | `POST /api/local-model/agent` | `LocalModelAgentRequest` | the install view; 400, 409 |
*
* The GET is built from the hardware probe, the catalogue, the harness facts and the pure
* `recommend()`, plus the `install` object of the install service
@@ -55,6 +56,10 @@
* - `DELETE /api/local-model/models` takes a model NAME in its body, never a path: the schema
* admits only `name:tag`, and the service deletes only a name of its own listing, through
* the runtime.
* - `POST /api/local-model/agent` installs one more coding agent beside an installed model.
* Its body names a CLI id only: the service takes it only when it is a row of the view's
* `harnesses` that offers `install`, and runs the compiled stock command, never text from
* the request. It looks at no session: nothing about the model in use changes.
* - Every dependency is injected (`LocalModelRouteDeps`), so a route test spawns nothing and
* does not need a Mac: it passes its own hardware, harness facts and service.
*/
@@ -97,6 +102,7 @@ import {
} from '../../local-model/install-job.js';
import { recommend } from '../../local-model/recommend.js';
import {
LocalModelAgentSchema,
LocalModelDeleteSchema,
LocalModelInstallSchema,
LocalModelRemoveSchema,
@@ -261,7 +267,7 @@ export function registerLocalModelRoutes(
// The measured budget once the runtime reported it, and the runtime download that an
// install would still add: what turns the estimate into the figures of the consent.
const options = await service.recommendOptions();
const install = await service.view();
const install = await service.view({ harnessFacts });
// A row that is already a checked model on this Mac says so: choosing it is a switch.
const recommendation = markLibraryCandidates(
resolved.recommend(hardware, catalog, harnessFacts, options),
@@ -334,4 +340,11 @@ export function registerLocalModelRoutes(
const body = parseBody(LocalModelDeleteSchema, req.body);
return runVerb(reply, () => resolved.service().deleteLibraryModel(body, actorOf(req)));
});
app.post('/api/local-model/agent', async (req, reply): Promise<LocalModelVerbResponse> => {
const denied = adminOnly(req, reply);
if (denied) return denied;
const body = parseBody(LocalModelAgentSchema, req.body);
return runVerb(reply, () => resolved.service().installAgent(body, actorOf(req)));
});
}
+13 -4
View File
@@ -2481,14 +2481,20 @@ const localModelId = z
.max(200)
.regex(/^[a-z0-9][a-z0-9._-]*:[a-z0-9][a-z0-9._-]*$/, 'Invalid model id');
/**
* A coding agent as a CLI registry id. A request-shape check only: the real gate is the
* service's own lookup of the id among the catalogue harnesses it offers right now.
*/
const localModelCli = z
.string()
.max(64)
.regex(/^[a-z0-9][a-z0-9_-]*$/, 'Invalid coding agent id');
/** POST /api/local-model/install: the row the person chose and the size they confirmed. */
export const LocalModelInstallSchema = z
.object({
modelId: localModelId,
cli: z
.string()
.max(64)
.regex(/^[a-z0-9][a-z0-9_-]*$/, 'Invalid coding agent id'),
cli: localModelCli,
context: z.number().int().positive().max(MAX_DISCOVERED_CONTEXT_TOKENS),
acknowledgedBytes: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
})
@@ -2511,6 +2517,9 @@ export const LocalModelUseSchema = z.object({ modelId: localModelId }).strict();
/** DELETE /api/local-model/models: one model of the store, by its name. Never a path. */
export const LocalModelDeleteSchema = z.object({ modelId: localModelId }).strict();
/** POST /api/local-model/agent: one more coding agent to install beside an installed model. */
export const LocalModelAgentSchema = z.object({ cli: localModelCli }).strict();
/**
* A shell-safe bare word, mirroring `config/cli-registry/schema.ts`'s own `shellToken` —
* duplicated rather than imported, since the REAL safety boundary for anything built from