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.

9.4 KB
Raw Blame
import { it, expect } from 'vitest'
import { createServer } from 'node:http'
import { mkdtemp, mkdir, writeFile, readFile, rm, stat } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { LocalModels } from './localModels.js'

it('discovers, downloads, starts, verifies, stops and restarts across real subprocesses and HTTP', async () => {
  const root = await mkdtemp(join(tmpdir(), 'models-pipeline-'))
  const gridHome = join(root, 'grid-home'), stateDir = join(root, 'state')
  const modelId = 'fixture/Tiny-GGUF'
  let replies = 0, catalogReads = 0
  const server = createServer(async (req, res) => {
    const chunks: Buffer[] = []
    for await (const chunk of req) chunks.push(Buffer.from(chunk))
    // The engine probe's GET /health is the one request with no body, and this fixture serves no
    // engine: parsing it threw outside the test, failing a unit run whose every test had passed.
    const text = Buffer.concat(chunks).toString()
    if (!text) { res.statusCode = 404; res.end(); return }
    const body = JSON.parse(text)
    res.setHeader('content-type', 'application/json')
    if (req.url === '/v1/grid/catalog') {
      expect(req.headers.authorization).toBe('Bearer catalog-fixture')
      // CPU models share system RAM: the catalog gets half of this 16 GiB fixture.
      expect(body.device.usable_bytes).toBe(8 * 1024 ** 3)
      catalogReads++
      res.end(JSON.stringify({ models: [{ repo_id: modelId, runnable: true, task: 'text-generation', format: 'GGUF',
        fit: { version: 'Q4', ctx: 131072 }, versions: [{ version: 'Q4', size_bytes: 64, pull_spec: `${modelId}:tiny.gguf` }] }] }))
    } else {
      expect(req.url).toBe('/v1/chat/completions')
      expect(req.headers.authorization).toBe('Bearer inference-fixture')
      expect(body).toMatchObject({ model: 'tiny.gguf', max_tokens: 8 })
      replies++
      res.end(JSON.stringify({ choices: [{ message: { content: 'ok' } }] }))
    }
  })
  await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
  const address = server.address() as { port: number }
  const base = `http://127.0.0.1:${address.port}`
  let service: LocalModels | undefined
  try {
    await mkdir(gridHome)
    await writeFile(join(gridHome, 'credentials.toml'), `session_token = 'catalog-fixture'\napi_url = '${base}'\n`)
    const executable = join(root, 'grid.mjs')
    await writeFile(executable, `#!${process.execPath}
import { mkdirSync, writeFileSync, appendFileSync, existsSync, rmSync } from 'node:fs';
import { join } from 'node:path';
const home = process.env.GRID_HOME, args = process.argv.slice(2);
const recordDir = join(home, 'run', 'engines', 'fixture-grid');
const record = join(recordDir, 'remote.json');
const registered = join(home, 'registered');
appendFileSync(join(home, 'calls.jsonl'), JSON.stringify(args) + '\\n');
const result = value => console.log(JSON.stringify(value));
if (args[0] === 'device-info') result({ usable_bytes: 16 * 1024 ** 3, device_class: 'cpu', backend: 'cpu', memory: { total_gb: 16 } });
else if (args.includes('sync')) writeFileSync(registered, 'home');
else if (args.includes('ls')) result(existsSync(registered) ? [{ grid: 'home', id: 'fixture-grid' }] : []);
else if (args.includes('engines')) result(existsSync(record) ? [{ node_id: 'fixture-node', online: true, models: ['tiny'] }] : []);
else if (args[0] === 'pull') { mkdirSync(join(home, 'models'), { recursive: true }); process.stderr.write('42%\\r100%\\n'); writeFileSync(join(home, 'models', 'tiny.gguf'), Buffer.alloc(64)); }
else if (args[0] === 'engine') { mkdirSync(join(home, 'bin'), { recursive: true }); writeFileSync(join(home, 'bin', 'llama-server'), '#!/bin/sh\\nexit 0\\n', { mode: 0o700 }); }
else if (args.includes('join')) { if (!existsSync(registered)) process.exit(1); mkdirSync(recordDir, { recursive: true }); writeFileSync(record, JSON.stringify({ node_id: 'fixture-node', engines: [{ models: ['tiny.gguf'] }] })); writeFileSync(join(recordDir, 'remote.heartbeat'), ''); }
else if (args.includes('leave')) { rmSync(record); rmSync(registered); }
else if (args.includes('info') && args.includes('--json')) result({ grid: 'home', status: 'running' });
else if (args.includes('info')) console.log("export OPENAI_BASE_URL='${base}/v1'\\nexport OPENAI_API_KEY='inference-fixture'");
else process.exitCode = 2;
`, { mode: 0o700 })
    // The grid's own answer, as the credential-less reader would give it: the engine is listed while its
    // run record exists (the fake's `join` writes it, `leave` removes it).
    const record = join(gridHome, 'run', 'engines', 'fixture-grid', 'remote.json')
    const inventory = async () => ({ state: 'awake' as const, status: 'running', nodes: existsSync(record) ? [{ node_id: 'fixture-node', online: true, models: ['tiny'] }] : [] })
    const options = { stateDir, processEnv: { GRID_HOME: gridHome, HARNESS_GRID_BIN: executable, PATH: root }, inventory }
    service = new LocalModels(options)
    expect((await service.list('home')).models[0]).toMatchObject({ state: 'available', canStart: true })
    expect(replies).toBe(0)
    await service.act('home', modelId, 'start'); await service.settled()
    expect((await service.list('home')).models[0]).toMatchObject({ state: 'running', canStop: true, operation: { phase: 'done' } })
    expect(replies).toBe(1)
    // A fresh client/service can recover the completed status from disk.
    service = new LocalModels(options)
    expect((await service.list('home')).models[0].operation?.phase).toBe('done')
    await service.act('home', modelId, 'stop'); await service.settled()
    expect((await service.list('home')).models[0].state).toBe('downloaded')
    expect((await stat(join(gridHome, 'models', 'tiny.gguf'))).size).toBe(64)
    await service.act('home', modelId, 'start'); await service.settled()
    expect((await service.list('home')).models[0].state).toBe('running')
    const calls = (await readFile(join(gridHome, 'calls.jsonl'), 'utf8')).trim().split('\n').map(line => JSON.parse(line) as string[])
    expect(calls.filter(args => args[0] === 'pull')).toHaveLength(1)
    expect(calls.filter(args => args.includes('sync'))).toHaveLength(2)
    expect(calls.filter(args => args[0] === 'engine')).toHaveLength(1)
    expect(calls.find(args => args.includes('leave'))).toEqual(['--remote', 'leave', 'home', '--engine', 'tiny.gguf'])
    expect(catalogReads).toBeGreaterThan(1)
    expect(replies).toBe(2)
  } finally {
    await service?.settled()
    server.closeAllConnections()
    await new Promise<void>(resolve => server.close(() => resolve()))
    await rm(root, { recursive: true, force: true })
  }
}, 20_000)

it('cancels a download that hangs: the real grid process ends, and what it wrote goes', async () => {
  const root = await mkdtemp(join(tmpdir(), 'models-cancel-'))
  const gridHome = join(root, 'grid-home'), stateDir = join(root, 'state')
  const modelId = 'fixture/Tiny-GGUF'
  const server = createServer(async (req, res) => {
    for await (const _ of req) { /* drain */ }
    res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify({ models: [{ repo_id: modelId, runnable: true, task: 'text-generation', format: 'GGUF',
      fit: { version: 'Q4', ctx: 131072 }, versions: [{ version: 'Q4', size_bytes: 64, pull_spec: `${modelId}:tiny.gguf` }] }] }))
  })
  await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
  const base = `http://127.0.0.1:${(server.address() as { port: number }).port}`
  let service: LocalModels | undefined
  try {
    await mkdir(join(gridHome, 'models'), { recursive: true })
    await writeFile(join(gridHome, 'credentials.toml'), `session_token = 'catalog-fixture'\napi_url = '${base}'\n`)
    const executable = join(root, 'grid.mjs')
    // `grid pull` as it hung when its connection stopped sending: a `.part` written, then nothing, forever.
    await writeFile(executable, `#!${process.execPath}
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
const home = process.env.GRID_HOME, args = process.argv.slice(2);
if (args[0] === 'device-info') console.log(JSON.stringify({ usable_bytes: 16 * 1024 ** 3, device_class: 'cpu', backend: 'cpu', memory: { total_gb: 16 } }));
else if (args.includes('ls')) console.log(JSON.stringify([{ grid: 'home', id: 'fixture-grid' }]));
else if (args[0] === 'pull') { writeFileSync(join(home, 'models', 'tiny.gguf.part'), Buffer.alloc(24)); writeFileSync(join(home, 'pulling'), String(process.pid)); setInterval(() => {}, 1000); }
else process.exitCode = 2;
`, { mode: 0o700 })
    const inventory = async () => ({ state: 'awake' as const, status: 'running', nodes: [] })
    service = new LocalModels({ stateDir, processEnv: { GRID_HOME: gridHome, HARNESS_GRID_BIN: executable, PATH: root }, inventory })
    await service.act('home', modelId, 'download')
    const pulling = join(gridHome, 'pulling')
    for (let i = 0; i < 100 && !existsSync(pulling); i++) await new Promise(resolve => setTimeout(resolve, 50))
    const pid = Number(await readFile(pulling, 'utf8'))
    await service.act('home', modelId, 'stop'); await service.settled()
    expect(() => process.kill(pid, 0)).toThrow()
    expect(existsSync(join(gridHome, 'models', 'tiny.gguf.part'))).toBe(false)
    expect((await service.list('home', true)).models[0]).toMatchObject({ state: 'available', operation: { action: 'stop', phase: 'done' } })
  } finally {
    await service?.settled()
    server.closeAllConnections()
    await new Promise<void>(resolve => server.close(() => resolve()))
    await rm(root, { recursive: true, force: true })
  }
}, 20_000)