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.

Local models: a model served on one grid is listed on every Swarm's grid

bitcoinbuilder4e0d4095854f
diff --git a/cli/src/lib/localModels.spec.ts b/cli/src/lib/localModels.spec.ts
index a0404cb9e..4ff7366d1 100644
--- a/cli/src/lib/localModels.spec.ts
+++ b/cli/src/lib/localModels.spec.ts
@@ -412,6 +412,26 @@ describe('local model discovery and lifecycle', () => {
expect((await fresh.list('home')).models[0].operation?.phase).toBe('done')
})
+ it('lists a model this computer served on one grid on every other grid too', async () => {
+ catalogCards = []; serving = true
+ await writeFile(join(home, 'models', 'Small-Q4.gguf'), Buffer.alloc(64))
+ await writeFile(join(records, 'remote.json'), JSON.stringify({ node_id: 'local-node', engines: [{ models: ['Small-Q4.gguf'] }] }))
+ await writeFile(join(records, 'remote.heartbeat'), '')
+ const id = (await service.list('home')).models[0].id
+ await service.act('home', id, 'stop'); await service.settled()
+ // Another Swarm's grid, read by a fresh process: the file is known there as well, and a broken file beside it
+ // reads as nothing.
+ await writeFile(join(stateDir, 'broken.known.json'), '{')
+ // Known on two grids, it is listed once.
+ const known = (await readdir(stateDir)).find(name => name.endsWith('.known.json') && name !== 'broken.known.json')!
+ await writeFile(join(stateDir, 'copy.known.json'), await readFile(join(stateDir, known)))
+ const fresh = modelFixture({ stateDir, processEnv: { GRID_HOME: home }, run, request: request as typeof fetch, inventory })
+ expect((await fresh.list('swarm-0123456789ab')).models.map(m => m.id)).toEqual([id])
+ // With no state folder yet, nothing is known.
+ const blank = modelFixture({ stateDir: join(stateDir, 'none'), processEnv: { GRID_HOME: home }, run, request: request as typeof fetch, inventory })
+ expect((await blank.list('swarm-0123456789ab')).models.map(m => m.id)).toEqual([])
+ })
+
it('reuses an already downloaded fitting quant of the same model', async () => {
catalogCards[0].versions.push({ version: 'Q3', size_bytes: 48, pull_spec: 'org/Small-GGUF:Small-Q3.gguf', urls: ['https://example.test/Small-Q3.gguf'] })
await writeFile(join(home, 'models', 'Small-Q3.gguf'), Buffer.alloc(48))
diff --git a/cli/src/lib/localModels.ts b/cli/src/lib/localModels.ts
index c29dfbafa..5a708c82b 100644
--- a/cli/src/lib/localModels.ts
+++ b/cli/src/lib/localModels.ts
@@ -26,6 +26,24 @@ const rows = (v: unknown): Record<string, any>[] => Array.isArray(v) ? v.map(obj
const str = (v: unknown): string => typeof v === 'string' ? v : ''
const num = (v: unknown): number | undefined => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : undefined
const key = (v: string): string => createHash('sha256').update(v).digest('hex').slice(0, 24)
+
+/** The models a grid's known-models file names, each checked: a malformed row is left out, never forwarded as argv. */
+async function readKnown(path: string): Promise<Candidate[]> {
+ try {
+ return rows(JSON.parse(await readFile(path, 'utf8'))).filter(c =>
+ c.id === `local:${c.file}` && basename(str(c.file)) === c.file && validArg(c.file) &&
+ c.pull === '' && typeof c.name === 'string' && c.name.length < 256 && num(c.size) &&
+ Array.isArray(c.files) && c.files.length === 1 && c.files[0] === c.file).map(({ context: _pinned, ...c }) => ({
+ // A saved `context` is dropped, not carried: older receipts pinned 16K, and a model
+ // restarted from one would come back too small for a coding agent. The engine sizes it.
+ ...c,
+ // Older receipts kept only the displayed alias. Preserve that name
+ // when upgrading, and never forward malformed saved argv values.
+ aliases: (Array.isArray(c.aliases) ? c.aliases : [c.name])
+ .filter((alias: unknown): alias is string => typeof alias === 'string' && validArg(alias)),
+ })) as Candidate[]
+ } catch { return [] }
+}
const cleanName = (v: string): string => basename(v).replace(/\.gguf$/i, '').replace(/-GGUF$/i, '')
// Grid advertises a GGUF filename as a lowercase model name without its extension.
const modelKey = (v: string): string => v.toLowerCase().replace(/\.gguf$/, '')
@@ -572,20 +590,15 @@ export class LocalModels {
const path = join(this.options.stateDir, `${key(grid)}.known.json`)
let known = this.knownByGrid.get(grid)
if (!known) {
- try {
- known = rows(JSON.parse(await readFile(path, 'utf8'))).filter(c =>
- c.id === `local:${c.file}` && basename(str(c.file)) === c.file && validArg(c.file) &&
- c.pull === '' && typeof c.name === 'string' && c.name.length < 256 && num(c.size) &&
- Array.isArray(c.files) && c.files.length === 1 && c.files[0] === c.file).map(({ context: _pinned, ...c }) => ({
- // A saved `context` is dropped, not carried: older receipts pinned 16K, and a model
- // restarted from one would come back too small for a coding agent. The engine sizes it.
- ...c,
- // Older receipts kept only the displayed alias. Preserve that name
- // when upgrading, and never forward malformed saved argv values.
- aliases: (Array.isArray(c.aliases) ? c.aliases : [c.name])
- .filter((alias: unknown): alias is string => typeof alias === 'string' && validArg(alias)),
- })) as Candidate[]
- } catch { known = [] }
+ known = await readKnown(path)
+ // A file this computer served on another grid is as restartable here: each Swarm has a grid of its own, and a
+ // model run for one was missing from the next one's list, offered again as a download (2026-10-24).
+ const others = await readdir(this.options.stateDir).catch(() => [] as string[])
+ for (const name of others.filter(n => n.endsWith('.known.json') && join(this.options.stateDir, n) !== path).sort()) {
+ for (const candidate of await readKnown(join(this.options.stateDir, name))) {
+ if (!known.some(c => c.file === candidate.file)) known.push(candidate)
+ }
+ }
this.knownByGrid.set(grid, known)
}
let changed = false