nimblebrain 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/nimblebrain.js +133 -111
  2. package/package.json +1 -1
@@ -6,7 +6,7 @@ import { writeSync } from "node:fs";
6
6
  // src/deps.ts
7
7
  import { spawn } from "node:child_process";
8
8
  import { constants } from "node:fs";
9
- import { access, chmod, mkdir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
9
+ import { access, chmod, mkdir, readFile, rename, rm, rmdir, writeFile } from "node:fs/promises";
10
10
  import { createServer } from "node:net";
11
11
  import { homedir } from "node:os";
12
12
  import { delimiter, dirname, join, resolve } from "node:path";
@@ -96,8 +96,17 @@ function realDeps() {
96
96
  },
97
97
  async writeText(path, text, mode = 420) {
98
98
  await mkdir(dirname(path), { recursive: true, mode: 448 });
99
- await writeFile(path, text, { mode });
100
- await chmod(path, mode);
99
+ const tmp = `${path}.${process.pid}.tmp`;
100
+ try {
101
+ await writeFile(tmp, text, { mode });
102
+ await chmod(tmp, mode);
103
+ await rename(tmp, path);
104
+ } catch (err) {
105
+ await rm(tmp, { force: true }).catch(() => {
106
+ return;
107
+ });
108
+ throw err;
109
+ }
101
110
  },
102
111
  async remove(path) {
103
112
  try {
@@ -220,11 +229,12 @@ var ERRORS = {
220
229
  LOW_MEMORY: "Give Docker at least 2 GB of memory in its settings, then run this again.",
221
230
  PORT_IN_USE: "Stop whatever holds the port, or pass --port with a free one.",
222
231
  NOT_INSTALLED: "Run `npx nimblebrain` to install it.",
223
- NOT_OURS: "Another install holds this name or its data. Remove it yourself, or choose a --home whose folder has another name.",
232
+ NOT_OURS: "This folder holds files the CLI did not write. Remove them yourself, or choose another --home.",
233
+ INSTALL_INCOMPLETE: "Run `npx nimblebrain` to finish installing it, or `npx nimblebrain uninstall` to remove it.",
224
234
  NETWORK: "Check the network connection, then run the same command again.",
225
- HEALTH_TIMEOUT: "Run `npx nimblebrain logs` and look at the last error line.",
235
+ HEALTH_TIMEOUT: "Run `npx nimblebrain logs` and look at the last error line, then run the command again; `npx nimblebrain` finishes an install whose first start did not.",
226
236
  AGENT_UNSUPPORTED: "Pass --agent claude-code, cursor, codex or none.",
227
- START_FAILED: "Run `npx nimblebrain logs` and look at the last error line, then run this again.",
237
+ START_FAILED: "Run `npx nimblebrain logs` and look at the last error line, then run the command again; `npx nimblebrain` finishes an install whose first start did not.",
228
238
  NEEDS_TERMINAL: "A person runs this in their own terminal; it asks for input no agent should give.",
229
239
  CANCELLED: "Nothing was changed. Run the command again to go ahead.",
230
240
  RUNTIME_UNSUPPORTED: "Pass a newer runtime with --runtime, or leave it out to install the newest release.",
@@ -251,6 +261,9 @@ var FIX_VARIANTS = {
251
261
  mac_brew: "Install Docker Desktop with `brew install --cask docker`, open it once from Applications, then run this again.",
252
262
  mac: "Install Docker Desktop (https://docs.docker.com/desktop/setup/install/mac-install/), open it once, then run this again."
253
263
  },
264
+ DOCKER_NOT_RUNNING: {
265
+ colima: "Start Colima with `colima start`, then run this again."
266
+ },
254
267
  COMPOSE_MISSING: {
255
268
  brew: "Install Compose with `brew install docker-compose`, then run this again."
256
269
  },
@@ -518,13 +531,13 @@ var FLAG_SPECS = {
518
531
  port: {
519
532
  type: "string",
520
533
  takes: "n",
521
- description: `Local port (default ${DEFAULT_PORT})`,
534
+ description: `Local port (default ${DEFAULT_PORT}; for doctor, the install's own port)`,
522
535
  commands: ["init", "doctor"]
523
536
  },
524
537
  home: {
525
538
  type: "string",
526
539
  takes: "dir",
527
- description: "Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME); a home with another folder name is a separate install"
540
+ description: "Where the install lives (default ~/.nimblebrain-local, or NIMBLEBRAIN_HOME); each home is a separate install"
528
541
  },
529
542
  provider: {
530
543
  type: "string",
@@ -581,8 +594,8 @@ var options = Object.fromEntries(Object.entries(FLAG_SPECS).map(([name, f]) => [
581
594
  function parse(argv) {
582
595
  const { values, positionals } = parseStrict(argv);
583
596
  const json = values.json === true;
584
- const port = values.port === undefined ? DEFAULT_PORT : Number(values.port);
585
- if (!Number.isInteger(port) || port < 1 || port > 65535) {
597
+ const port = values.port === undefined ? undefined : Number(values.port);
598
+ if (port !== undefined && (!Number.isInteger(port) || port < 1 || port > 65535)) {
586
599
  throw new CliError("USAGE", `--port must be a port number, not "${values.port}".`);
587
600
  }
588
601
  let agent;
@@ -669,15 +682,13 @@ function wantsJson(argv) {
669
682
  }
670
683
 
671
684
  // src/compose.ts
685
+ import { randomBytes } from "node:crypto";
672
686
  import { basename, join as join2 } from "node:path";
673
- var DEFAULT_PROJECT = "nimblebrain-local";
674
- function projectFor(home) {
687
+ function newProject(home) {
675
688
  const slug = basename(home).toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z0-9]+/, "").replace(/[^a-z0-9]+$/, "");
676
- if (!slug)
677
- return "nimblebrain";
678
- return slug.startsWith("nimblebrain") ? slug : `nimblebrain-${slug}`;
689
+ const name = !slug ? "nimblebrain" : slug.startsWith("nimblebrain") ? slug : `nimblebrain-${slug}`;
690
+ return `${name}-${randomBytes(3).toString("hex")}`;
679
691
  }
680
- var volumeOf = (project) => `${project}_workspace`;
681
692
  var RAW = "https://raw.githubusercontent.com/NimbleBrainInc/nimblebrain";
682
693
  async function fetchCompose(deps, tag) {
683
694
  const res = await deps.getText(`${RAW}/${tag}/docker-compose.yml`, 15000);
@@ -762,11 +773,26 @@ async function compose(deps, home, args, timeoutMs = 120000) {
762
773
  timeoutMs
763
774
  });
764
775
  }
776
+ async function claimWritten(deps, home) {
777
+ return await deps.readText(paths(home).env) !== undefined;
778
+ }
779
+ async function runningHere(deps, home) {
780
+ if (!await claimWritten(deps, home))
781
+ return false;
782
+ const res = await compose(deps, home, ["ps", "-q"], 30000);
783
+ return res.code === 0 && res.stdout.trim() !== "";
784
+ }
785
+ function daemonVariant(text) {
786
+ return /\/\.colima\/default\//.test(text) ? "colima" : undefined;
787
+ }
765
788
  function startError(res) {
766
789
  const text = `${res.stderr}
767
790
  ${res.stdout}`;
768
791
  const last = text.trim().split(`
769
792
  `).filter((l) => l.trim()).pop() ?? "Compose failed.";
793
+ if (/cannot connect to the docker daemon|failed to connect to the docker api/i.test(text)) {
794
+ return new CliError("DOCKER_NOT_RUNNING", last, {}, daemonVariant(text));
795
+ }
770
796
  if (/no such host|timeout|TLS handshake|connection refused|network is unreachable/i.test(text)) {
771
797
  return new CliError("NETWORK", last);
772
798
  }
@@ -851,7 +877,7 @@ async function checkDaemon(deps, log) {
851
877
  ${info.stdout}`)) {
852
878
  log.fail("docker-daemon", "DOCKER_PERMISSION", "This user may not use Docker.");
853
879
  }
854
- log.fail("docker-daemon", "DOCKER_NOT_RUNNING", "Docker is installed but not running.");
880
+ log.fail("docker-daemon", "DOCKER_NOT_RUNNING", "Docker is installed but not running.", daemonVariant(info.stderr));
855
881
  }
856
882
  log.pass("docker-daemon", "running");
857
883
  return parseInfo(info.stdout);
@@ -902,6 +928,7 @@ function gib(bytes) {
902
928
  // src/answers.ts
903
929
  var STATE = {
904
930
  version: "The NimbleBrain runtime release installed, without the leading v",
931
+ phase: "starting until the install's first start answers health, else ready; npx nimblebrain finishes a starting install, and only down, logs, uninstall, status and doctor act on one",
905
932
  cli_version: "This CLI's version; it moves independently of the runtime",
906
933
  health: "ok when NimbleBrain answers, else down",
907
934
  url: "Where the web UI and API answer, on 127.0.0.1",
@@ -1178,7 +1205,7 @@ function doNotTrack(deps) {
1178
1205
  return deps.env.DO_NOT_TRACK === "1";
1179
1206
  }
1180
1207
  function lockedHere(deps) {
1181
- if (doNotTrack(deps) || deps.env.NB_TELEMETRY_DISABLED === "1")
1208
+ if (doNotTrack(deps))
1182
1209
  return "do_not_track";
1183
1210
  if (!telemetryKey(deps))
1184
1211
  return "no_destination";
@@ -1334,7 +1361,7 @@ async function setRuntimeTelemetry(deps, url, consent) {
1334
1361
  // package.json
1335
1362
  var package_default = {
1336
1363
  name: "nimblebrain",
1337
- version: "0.7.1",
1364
+ version: "0.8.0",
1338
1365
  description: "Install, run and connect NimbleBrain, the open-source runtime for agents and MCP apps, on this machine.",
1339
1366
  license: "Apache-2.0",
1340
1367
  type: "module",
@@ -1368,7 +1395,7 @@ async function readInstall(deps, home) {
1368
1395
  return;
1369
1396
  try {
1370
1397
  const state = JSON.parse(text);
1371
- if (typeof state.version !== "string" || typeof state.port !== "number" || typeof state.project !== "string") {
1398
+ if (typeof state?.version !== "string" || typeof state.port !== "number" || typeof state.project !== "string" || state.phase !== "starting" && state.phase !== "ready") {
1372
1399
  return;
1373
1400
  }
1374
1401
  return state;
@@ -1376,13 +1403,24 @@ async function readInstall(deps, home) {
1376
1403
  return;
1377
1404
  }
1378
1405
  }
1379
- async function status(deps, home) {
1406
+ async function requireInstall(deps, home) {
1380
1407
  const install = await readInstall(deps, home);
1381
1408
  if (!install)
1382
1409
  throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
1410
+ return install;
1411
+ }
1412
+ async function requireReady(deps, home) {
1413
+ const install = await requireInstall(deps, home);
1414
+ if (install.phase !== "ready") {
1415
+ throw new CliError("INSTALL_INCOMPLETE", `The NimbleBrain install in ${home} has not finished installing.`);
1416
+ }
1417
+ return install;
1418
+ }
1419
+ async function status(deps, home) {
1420
+ const install = await requireInstall(deps, home);
1383
1421
  const url = installUrl(install);
1384
1422
  const up = await isUp(deps, url);
1385
- const workspaces = up ? await readWorkspaces(deps, url) : [];
1423
+ const workspaces = up && install.phase === "ready" ? await readWorkspaces(deps, url) : [];
1386
1424
  const composeText = await deps.readText(join5(home, "docker-compose.yml")) ?? "";
1387
1425
  const vars = keyVars(composeText);
1388
1426
  const envText = await deps.readText(join5(home, ".env")) ?? "";
@@ -1390,6 +1428,7 @@ async function status(deps, home) {
1390
1428
  return {
1391
1429
  ok: true,
1392
1430
  version: install.version,
1431
+ phase: install.phase,
1393
1432
  cli_version: VERSION,
1394
1433
  health: up ? "ok" : "down",
1395
1434
  url,
@@ -1441,9 +1480,7 @@ async function openBrowser(deps, url) {
1441
1480
  }
1442
1481
  async function open(ctx) {
1443
1482
  const { deps, home } = ctx;
1444
- const install = await readInstall(deps, home);
1445
- if (!install)
1446
- throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
1483
+ const install = await requireReady(deps, home);
1447
1484
  const url = installUrl(install);
1448
1485
  if (!await isUp(deps, url))
1449
1486
  throw new CliError("NOT_RUNNING", "NimbleBrain is not running.", { url });
@@ -1530,15 +1567,16 @@ function confirmOllamaStart(ctx, unasked) {
1530
1567
  }
1531
1568
  async function init(ctx) {
1532
1569
  const { deps, flags, home } = ctx;
1533
- if (await readInstall(deps, home))
1570
+ const record = await readInstall(deps, home);
1571
+ if (record?.phase === "ready")
1534
1572
  return { ...await status(deps, home), already: true };
1535
- const consent = await chooseConsent(ctx);
1573
+ const consent = await chooseConsent(ctx, record);
1536
1574
  const id = await foreignFile(deps, home) ? undefined : await installId(deps, home);
1537
1575
  const rec = recorder2(deps, ctx.version, id ?? await reportId(deps, home));
1538
1576
  const begun = Date.now();
1539
1577
  rec.record("cli.init_started", { agent: flags.agent ?? null, interactive: deps.interactive });
1540
1578
  try {
1541
- return await openWhenDone(ctx, await install(ctx, consent, rec, begun));
1579
+ return await openWhenDone(ctx, await install(ctx, record, consent, rec, begun));
1542
1580
  } catch (err) {
1543
1581
  if (!(err instanceof CliError))
1544
1582
  throw err;
@@ -1560,11 +1598,17 @@ async function settleFailure(rec, home, consent, err) {
1560
1598
  rec.record(check ? "cli.doctor_failed" : "cli.init_failed", { error: err.code, check });
1561
1599
  await rec.settle(home, consent);
1562
1600
  }
1563
- async function chooseConsent(ctx) {
1601
+ async function chooseConsent(ctx, record) {
1564
1602
  if (lockedHere(ctx.deps))
1565
1603
  return "off";
1566
1604
  if (ctx.flags.telemetry)
1567
1605
  return ctx.flags.telemetry;
1606
+ if (record) {
1607
+ const env = await ctx.deps.readText(paths(ctx.home).env) ?? "";
1608
+ const held = env.match(/^NB_TELEMETRY_CONSENT=(on|off)\r?$/m)?.[1];
1609
+ if (held === "on" || held === "off")
1610
+ return held;
1611
+ }
1568
1612
  if (!asks(ctx))
1569
1613
  return "undecided";
1570
1614
  const consent = parseAnswer(await ctx.deps.prompt(`${QUESTION} [Y/n] `));
@@ -1572,30 +1616,29 @@ async function chooseConsent(ctx) {
1572
1616
  ctx.deps.say(consent === "on" ? `Sending anonymous usage data. Turn it off any time: ${cli} telemetry off` : `No usage data will be sent. Turn it on any time: ${cli} telemetry on`);
1573
1617
  return consent;
1574
1618
  }
1575
- async function install(ctx, consent, rec, begun) {
1619
+ async function install(ctx, record, consent, rec, begun) {
1576
1620
  const { deps, flags, home } = ctx;
1577
- await doctor(deps, flags.port);
1578
- const project = await guardOurs(deps, home);
1579
- const tag = await runtimeTag(deps, flags.runtime);
1621
+ const port = flags.port ?? record?.port ?? DEFAULT_PORT;
1622
+ const ours = record?.port === port && await runningHere(deps, home);
1623
+ await doctor(deps, port, ours);
1624
+ if (!record)
1625
+ await refuseForeign(deps, home);
1626
+ const tag = record && !flags.runtime ? `v${record.version}` : await runtimeTag(deps, flags.runtime);
1580
1627
  const composeText = await fetchCompose(deps, tag);
1581
- await confirmStart(ctx, tag);
1582
- await writeInstallFiles(deps, home, project, tag, flags.port, composeText, envValues(deps, lockedHere(deps) ? undefined : rec.id, consent, asksConsent(composeText)));
1583
- const envText = await deps.readText(paths(home).env) ?? "";
1584
- const hasModel = hasKey(envText, keyVars(composeText)) || localModelOf(envText) !== undefined;
1585
- const local = flags.localModel ?? (!hasModel && asks(ctx) && takesLocalModel(composeText) ? await chooseModelSource(deps) : undefined);
1586
- let ollamaStarted;
1587
- if (local)
1588
- ollamaStarted = (await useLocalModel(ctx, composeText, local)).started;
1589
- else if (!hasModel && asks(ctx))
1590
- await enterKey(ctx, false);
1628
+ await confirmStart(ctx, tag, port);
1629
+ const state = {
1630
+ project: record?.project ?? newProject(home),
1631
+ port,
1632
+ version: tag.replace(/^v/, ""),
1633
+ phase: "starting"
1634
+ };
1635
+ await writeState(deps, home, state);
1636
+ await writeInstallFiles(deps, home, state.project, tag, port, composeText, envValues(deps, lockedHere(deps) ? undefined : rec.id, consent, asksConsent(composeText)));
1637
+ const ollamaStarted = await prepareModel2(ctx, composeText);
1591
1638
  return carryingStarted(ollamaStarted, async () => {
1592
1639
  deps.say(`Starting NimbleBrain ${tag}. The first start downloads about 2 GB of images.`);
1593
- await start(deps, home, flags.port);
1594
- const state = {
1595
- version: tag.replace(/^v/, ""),
1596
- port: flags.port,
1597
- project
1598
- };
1640
+ await start(deps, home, port);
1641
+ state.phase = "ready";
1599
1642
  await writeState(deps, home, state);
1600
1643
  const manual = await connectChosenAgent(ctx, state);
1601
1644
  if (state.agent)
@@ -1616,10 +1659,10 @@ async function install(ctx, consent, rec, begun) {
1616
1659
  return { ...result };
1617
1660
  });
1618
1661
  }
1619
- async function confirmStart(ctx, tag) {
1662
+ async function confirmStart(ctx, tag, port) {
1620
1663
  if (!asks(ctx))
1621
1664
  return;
1622
- const answer = await ctx.deps.prompt(`Start NimbleBrain ${tag} at http://127.0.0.1:${ctx.flags.port}? [Y/n] `);
1665
+ const answer = await ctx.deps.prompt(`Start NimbleBrain ${tag} at http://127.0.0.1:${port}? [Y/n] `);
1623
1666
  if (/^n/i.test(answer))
1624
1667
  throw new CliError("CANCELLED", "Nothing was installed.");
1625
1668
  }
@@ -1641,7 +1684,7 @@ async function connectChosenAgent(ctx, state) {
1641
1684
  }
1642
1685
  async function up(ctx) {
1643
1686
  const { deps, home } = ctx;
1644
- const install = await requireInstall(ctx);
1687
+ const install = await requireReady(ctx.deps, home);
1645
1688
  const local = localModelOf(await deps.readText(paths(home).env) ?? "");
1646
1689
  const ollama = local ? await ensureOllama(deps, confirmOllamaStart(ctx, true)).catch((err) => {
1647
1690
  if (err instanceof CliError)
@@ -1660,14 +1703,18 @@ async function up(ctx) {
1660
1703
  }
1661
1704
  var lowerFirst = (s) => s.charAt(0).toLowerCase() + s.slice(1);
1662
1705
  async function down(ctx) {
1663
- await requireInstall(ctx);
1706
+ await requireInstall(ctx.deps, ctx.home);
1707
+ if (!await claimWritten(ctx.deps, ctx.home))
1708
+ return { ok: true, health: "down" };
1664
1709
  const res = await compose(ctx.deps, ctx.home, ["down"]);
1665
1710
  if (res.code !== 0)
1666
1711
  throw startError(res);
1667
1712
  return { ok: true, health: "down" };
1668
1713
  }
1669
1714
  async function logs(ctx) {
1670
- await requireInstall(ctx);
1715
+ await requireInstall(ctx.deps, ctx.home);
1716
+ if (!await claimWritten(ctx.deps, ctx.home))
1717
+ return { ok: true, lines: [] };
1671
1718
  const res = await compose(ctx.deps, ctx.home, [
1672
1719
  "logs",
1673
1720
  "--no-color",
@@ -1682,7 +1729,7 @@ async function logs(ctx) {
1682
1729
  }
1683
1730
  async function update(ctx) {
1684
1731
  const { deps, home } = ctx;
1685
- const install = await requireInstall(ctx);
1732
+ const install = await requireReady(deps, home);
1686
1733
  const tag = await runtimeTag(deps, ctx.flags.runtime);
1687
1734
  const from = `v${install.version}`;
1688
1735
  const order = compareVersions(tag, from);
@@ -1711,7 +1758,7 @@ async function refreshAgentSkill(deps, install) {
1711
1758
  }
1712
1759
  async function uninstall(ctx) {
1713
1760
  const { deps, flags, home } = ctx;
1714
- const install = await requireInstall(ctx);
1761
+ const install = await requireInstall(deps, home);
1715
1762
  if (!flags.yes) {
1716
1763
  if (!deps.interactive || flags.json) {
1717
1764
  throw new CliError("CANCELLED", "Removing deletes every conversation; pass --yes to confirm.");
@@ -1720,9 +1767,11 @@ async function uninstall(ctx) {
1720
1767
  if (!/^y/i.test(answer))
1721
1768
  throw new CliError("CANCELLED", "Nothing was removed.");
1722
1769
  }
1723
- const res = await compose(deps, home, ["down", "-v", "--rmi", "all"], START_TIMEOUT_MS);
1724
- if (res.code !== 0)
1725
- throw startError(res);
1770
+ if (await claimWritten(deps, home)) {
1771
+ const res = await compose(deps, home, ["down", "-v", "--rmi", "all"], START_TIMEOUT_MS);
1772
+ if (res.code !== 0)
1773
+ throw startError(res);
1774
+ }
1726
1775
  if (install.agent === "claude-code")
1727
1776
  await deps.exec("claude", ["mcp", "remove", "-s", "user", SERVER_NAME]);
1728
1777
  if (install.agent === "codex")
@@ -1734,14 +1783,14 @@ async function uninstall(ctx) {
1734
1783
  }
1735
1784
  async function telemetry(ctx) {
1736
1785
  const { deps, home } = ctx;
1737
- const install = await requireInstall(ctx);
1786
+ const install = await requireReady(deps, home);
1738
1787
  const url = `http://127.0.0.1:${install.port}`;
1739
1788
  if (ctx.arg !== "on" && ctx.arg !== "off") {
1740
1789
  return { ok: true, telemetry: (await status(deps, home)).telemetry };
1741
1790
  }
1742
1791
  const here = lockedHere(deps);
1743
1792
  if (here) {
1744
- throw new CliError("TELEMETRY_LOCKED", here === "do_not_track" ? "DO_NOT_TRACK (or NB_TELEMETRY_DISABLED) is set, so telemetry stays off." : "This CLI has nowhere to send telemetry.");
1793
+ throw new CliError("TELEMETRY_LOCKED", here === "do_not_track" ? "DO_NOT_TRACK is set, so telemetry stays off." : "This CLI has nowhere to send telemetry.");
1745
1794
  }
1746
1795
  const rt = await runtimeTelemetry(deps, url);
1747
1796
  if (!rt)
@@ -1754,10 +1803,10 @@ async function telemetry(ctx) {
1754
1803
  return { ok: true, telemetry: field(ctx.arg, invocation(deps, home)) };
1755
1804
  }
1756
1805
  async function key(ctx) {
1806
+ await requireReady(ctx.deps, ctx.home);
1757
1807
  if (!ctx.deps.interactive) {
1758
1808
  throw new CliError("NEEDS_TERMINAL", "`key` asks for a model key at a hidden prompt.");
1759
1809
  }
1760
- await requireInstall(ctx);
1761
1810
  return enterKey(ctx, true);
1762
1811
  }
1763
1812
  async function enterKey(ctx, restart) {
@@ -1780,12 +1829,12 @@ async function enterKey(ctx, restart) {
1780
1829
  await deps.writeText(p.env, setEnvLines(await deps.readText(p.env) ?? "", { [variable]: value }), 384);
1781
1830
  if (restart) {
1782
1831
  const install = await readInstall(deps, home);
1783
- await start(deps, home, install?.port ?? flags.port);
1832
+ await start(deps, home, install?.port ?? DEFAULT_PORT);
1784
1833
  }
1785
1834
  return { ok: true, provider, model_key: "set" };
1786
1835
  }
1787
1836
  async function localModel(ctx) {
1788
- const install = await requireInstall(ctx);
1837
+ const install = await requireReady(ctx.deps, ctx.home);
1789
1838
  const composeText = await ctx.deps.readText(paths(ctx.home).compose) ?? "";
1790
1839
  const model = ctx.arg ?? ctx.flags.localModel ?? DEFAULT_LOCAL_MODEL;
1791
1840
  const { name, started } = await useLocalModel(ctx, composeText, model);
@@ -1858,45 +1907,31 @@ async function writeInstallFiles(deps, home, project, tag, port, composeText, ex
1858
1907
  });
1859
1908
  await deps.writeText(p.env, env, 384);
1860
1909
  }
1910
+ async function prepareModel2(ctx, composeText) {
1911
+ const { deps, flags, home } = ctx;
1912
+ const envText = await deps.readText(paths(home).env) ?? "";
1913
+ const saved = localModelOf(envText) !== undefined;
1914
+ const hasModel = saved || hasKey(envText, keyVars(composeText));
1915
+ const local = flags.localModel ?? (!hasModel && asks(ctx) && takesLocalModel(composeText) ? await chooseModelSource(deps) : undefined);
1916
+ if (local)
1917
+ return (await useLocalModel(ctx, composeText, local)).started;
1918
+ if (saved)
1919
+ return (await ensureOllama(deps, confirmOllamaStart(ctx, true))).started;
1920
+ if (!hasModel && asks(ctx))
1921
+ await enterKey(ctx, false);
1922
+ return;
1923
+ }
1861
1924
  async function writeState(deps, home, state) {
1862
1925
  await deps.writeText(paths(home).install, `${JSON.stringify(state, null, 2)}
1863
1926
  `);
1864
1927
  }
1865
- async function requireInstall({ deps, home }) {
1866
- const install = await readInstall(deps, home);
1867
- if (!install)
1868
- throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
1869
- return install;
1870
- }
1871
- async function guardOurs(deps, home) {
1872
- const project = projectFor(home);
1873
- const marked = await markedProject(deps, home);
1874
- if (marked)
1875
- return marked;
1928
+ async function refuseForeign(deps, home) {
1876
1929
  const file = await foreignFile(deps, home);
1877
1930
  if (file)
1878
1931
  throw new CliError("NOT_OURS", `${file} already exists and this CLI did not write it.`);
1879
- const projects = await composeExec(deps, ["ls", "-a", "--format", "json"]);
1880
- const taken = projects.code === 0 && listsProject(projects.stdout, project);
1881
- const volumes = await deps.exec("docker", [
1882
- "volume",
1883
- "ls",
1884
- "-q",
1885
- "--filter",
1886
- `name=^${volumeOf(project)}$`
1887
- ]);
1888
- if (taken || volumes.stdout.trim() !== "") {
1889
- throw new CliError("NOT_OURS", `Docker already has a "${project}" project or volume that this install did not make.`);
1890
- }
1891
- return project;
1892
- }
1893
- async function markedProject(deps, home) {
1894
- const env = await deps.readText(paths(home).env);
1895
- const marked = env?.match(/^COMPOSE_PROJECT_NAME=(.*?)\r?$/m)?.[1];
1896
- return marked === projectFor(home) || marked === DEFAULT_PROJECT ? marked : undefined;
1897
1932
  }
1898
1933
  async function foreignFile(deps, home) {
1899
- if (await markedProject(deps, home))
1934
+ if (await readInstall(deps, home))
1900
1935
  return;
1901
1936
  for (const file of Object.values(paths(home))) {
1902
1937
  if (await deps.readText(file) !== undefined)
@@ -1904,14 +1939,6 @@ async function foreignFile(deps, home) {
1904
1939
  }
1905
1940
  return;
1906
1941
  }
1907
- function listsProject(stdout, project) {
1908
- try {
1909
- const list = JSON.parse(stdout);
1910
- return Array.isArray(list) && list.some((p) => p?.Name === project);
1911
- } catch {
1912
- return false;
1913
- }
1914
- }
1915
1942
 
1916
1943
  // src/mcp.ts
1917
1944
  async function mcp(ctx) {
@@ -1935,9 +1962,7 @@ var ADD_TIMEOUT_MS = 60000;
1935
1962
  var CALL_TIMEOUT_MS = 15000;
1936
1963
  async function reach(ctx, adding) {
1937
1964
  const { deps, home, flags } = ctx;
1938
- const install = await readInstall(deps, home);
1939
- if (!install)
1940
- throw new CliError("NOT_INSTALLED", `No NimbleBrain install in ${home}.`);
1965
+ const install = await requireReady(deps, home);
1941
1966
  if (adding && !hostLoopback(await deps.readText(paths(home).compose) ?? "")) {
1942
1967
  throw new CliError("UPDATE_REQUIRED", `NimbleBrain ${install.version} cannot reach a server on this machine.`, { version: install.version });
1943
1968
  }
@@ -2147,9 +2172,10 @@ async function run(argv, deps) {
2147
2172
  switch (command) {
2148
2173
  case "doctor": {
2149
2174
  const install = await readInstall(deps, home);
2150
- const ours = install !== undefined && install.port === flags.port && await isHealthy(deps, flags.port);
2175
+ const port = flags.port ?? install?.port ?? DEFAULT_PORT;
2176
+ const ours = install?.port === port && await runningHere(deps, home);
2151
2177
  const startOllama = confirmOllamaStart({ deps, flags }, false);
2152
- result = { ...await doctorInstall(deps, home, flags.port, ours, startOllama) };
2178
+ result = { ...await doctorInstall(deps, home, port, ours, startOllama) };
2153
2179
  break;
2154
2180
  }
2155
2181
  case "status":
@@ -2222,10 +2248,6 @@ async function doctorInstall(deps, home, port, ours, startOllama) {
2222
2248
  throw err.with({ checks: [...machine.checks, ...local] });
2223
2249
  }
2224
2250
  }
2225
- async function isHealthy(deps, port) {
2226
- const res = await deps.getJson(`http://127.0.0.1:${port}/v1/health`);
2227
- return res?.status === 200;
2228
- }
2229
2251
  function toFailure(err) {
2230
2252
  if (err instanceof CliError)
2231
2253
  return failure(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nimblebrain",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "Install, run and connect NimbleBrain, the open-source runtime for agents and MCP apps, on this machine.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",