@infernetprotocol/cli 0.1.22

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 (50) hide show
  1. package/LICENSE +15 -0
  2. package/commands/chat.js +268 -0
  3. package/commands/console.js +229 -0
  4. package/commands/debug.js +222 -0
  5. package/commands/deploy.js +524 -0
  6. package/commands/doctor.js +501 -0
  7. package/commands/firewall.js +39 -0
  8. package/commands/gpu.js +130 -0
  9. package/commands/help.js +54 -0
  10. package/commands/init.js +304 -0
  11. package/commands/login.js +305 -0
  12. package/commands/logs.js +93 -0
  13. package/commands/model.js +359 -0
  14. package/commands/payments.js +92 -0
  15. package/commands/payout.js +128 -0
  16. package/commands/pubkey.js +116 -0
  17. package/commands/register.js +304 -0
  18. package/commands/remove.js +230 -0
  19. package/commands/service.js +255 -0
  20. package/commands/setup.js +869 -0
  21. package/commands/start.js +903 -0
  22. package/commands/stats.js +85 -0
  23. package/commands/status.js +116 -0
  24. package/commands/stop.js +81 -0
  25. package/commands/train.js +563 -0
  26. package/commands/tui.js +285 -0
  27. package/commands/uncensored.js +222 -0
  28. package/commands/update.js +80 -0
  29. package/commands/upgrade.js +2 -0
  30. package/examples/app.js +153 -0
  31. package/index.js +207 -0
  32. package/lib/chat-executor.js +232 -0
  33. package/lib/config.js +148 -0
  34. package/lib/daemonize.js +46 -0
  35. package/lib/firewall.js +190 -0
  36. package/lib/hf-model.js +166 -0
  37. package/lib/identity.js +18 -0
  38. package/lib/ipc.js +114 -0
  39. package/lib/model-fit.js +67 -0
  40. package/lib/model-key.js +52 -0
  41. package/lib/network.js +240 -0
  42. package/lib/nip44.js +130 -0
  43. package/lib/node-client.js +152 -0
  44. package/lib/peers.js +133 -0
  45. package/lib/prompt.js +49 -0
  46. package/lib/remote-chat.js +115 -0
  47. package/lib/route.js +44 -0
  48. package/lib/runtime-config.js +130 -0
  49. package/lib/version.js +32 -0
  50. package/package.json +57 -0
@@ -0,0 +1,869 @@
1
+ /**
2
+ * `infernet setup` — environment bootstrap.
3
+ *
4
+ * Detects what's installed (Node, Ollama, models), offers to install or
5
+ * pull what's missing, and writes engine preferences to the config so
6
+ * the daemon and `infernet chat` use them by default. Interactive by
7
+ * default; pass --yes (or set INFERNET_NONINTERACTIVE=1) for CI.
8
+ *
9
+ * Things that need root (Ollama install, systemd) are NOT executed —
10
+ * we print the exact command. The CLI runs unprivileged on purpose.
11
+ *
12
+ * Usage:
13
+ * infernet setup # interactive
14
+ * infernet setup --yes --model qwen2.5:7b # non-interactive
15
+ * infernet setup --skip-pull # don't pull a model
16
+ */
17
+
18
+ import { execFile, spawn } from "node:child_process";
19
+ import { promisify } from "node:util";
20
+ import { fileURLToPath } from "node:url";
21
+ import { dirname, join } from "node:path";
22
+ import { loadConfig, saveConfig, getConfigPath, fixConfigPermissions } from "../lib/config.js";
23
+ import { checkFits } from "../lib/model-fit.js";
24
+ import { nodeTokenLogin } from "./login.js";
25
+ import { question } from "../lib/prompt.js";
26
+ import { applyFirewallRule, detectFirewall, describeFirewallHowTo } from "../lib/firewall.js";
27
+ import { DEFAULT_P2P_PORT, resolveP2pPort } from "../lib/network.js";
28
+ import {
29
+ detectGpus, formatGpuLine,
30
+ detectCpus, detectHost, formatCpuLine,
31
+ detectInterconnects, formatInterconnectSummary,
32
+ lastDetectionDiagnostics
33
+ } from "@infernetprotocol/gpu";
34
+ import { isDaemonAlive } from "../lib/ipc.js";
35
+ import { createNodeClient } from "../lib/node-client.js";
36
+
37
+ const pExec = promisify(execFile);
38
+
39
+ const HELP = `infernet setup — bootstrap node environment
40
+
41
+ Usage:
42
+ infernet setup [flags]
43
+
44
+ Flags:
45
+ --confirm, --yes Auto-confirm every prompt. Use for unattended runs.
46
+ --model <name> Pre-select model to pull (e.g. qwen2.5:7b).
47
+ --skip-pull Skip the model-pull step.
48
+ --backend <kind> Pin engine backend (ollama | mojo | stub). Default: ollama.
49
+ --host <url> Override Ollama host. Default: http://localhost:11434.
50
+ --port <n> P2P port to open in the firewall. Default: ${DEFAULT_P2P_PORT}.
51
+ --no-firewall Skip the firewall step entirely.
52
+ --skip-identity Don't chain into \`infernet init\` if identity is missing.
53
+ --skip-register Don't chain into \`infernet register\` if not registered.
54
+ --skip-daemon Don't offer to start the daemon.
55
+ -h, --help Show this help.
56
+
57
+ By default, every step that wants to install or change something on
58
+ your system asks for confirmation first. Pass --confirm (or --yes) to
59
+ skip the prompts.
60
+ `;
61
+
62
+ /**
63
+ * Each entry's `size_gb` is the rough on-disk size of the GGUF file.
64
+ * The actual working-set (RAM/VRAM the model occupies during inference)
65
+ * is ~1.2× that for a Q4_K_M quant + KV cache. The model picker uses
66
+ * this to decide whether a model fits the host.
67
+ */
68
+ const MODEL_SUGGESTIONS = [
69
+ { name: "qwen2.5:0.5b", size_gb: 0.4, size: "≈400 MB", note: "smoke test, runs on CPU" },
70
+ { name: "qwen2.5:3b", size_gb: 2.0, size: "≈2 GB", note: "fits 6 GB+ GPU / 8 GB+ RAM" },
71
+ { name: "qwen2.5:7b", size_gb: 4.4, size: "≈4.4 GB", note: "fits 8 GB+ GPU / 12 GB+ RAM" },
72
+ { name: "qwen2.5:14b", size_gb: 9.0, size: "≈9 GB", note: "fits 16 GB+ GPU / 24 GB+ RAM" },
73
+ { name: "qwen2.5:32b", size_gb: 20.0, size: "≈20 GB", note: "fits 24 GB+ GPU / 40 GB+ RAM" },
74
+ { name: "qwen2.5:72b", size_gb: 40.0, size: "≈40 GB", note: "fits 48 GB+ GPU / 80 GB+ RAM" }
75
+ ];
76
+
77
+ // modelFits wraps checkFits: maps ok→fits and passes through unknown sizes.
78
+ export function modelFits({ size_gb, vram_gb, ram_gb }) {
79
+ if (!Number.isFinite(size_gb) || size_gb <= 0) {
80
+ return { fits: true, mode: "unknown", have_gb: 0, ceiling_gb: null };
81
+ }
82
+ const r = checkFits({ size_gb, vram_gb, ram_gb });
83
+ return { ...r, fits: r.ok };
84
+ }
85
+
86
+ function ok(msg) { process.stdout.write(` ✓ ${msg}\n`); }
87
+ function warn(msg) { process.stdout.write(` ! ${msg}\n`); }
88
+ function fail(msg) { process.stdout.write(` ✗ ${msg}\n`); }
89
+ function step(n, total, label) {
90
+ process.stdout.write(`\n[${n}/${total}] ${label}\n`);
91
+ }
92
+
93
+ async function detectNode() {
94
+ const v = process.versions.node;
95
+ const major = Number.parseInt(v.split(".")[0], 10);
96
+ if (major >= 18) {
97
+ ok(`Node.js v${v}`);
98
+ return true;
99
+ }
100
+ fail(`Node.js v${v} — need 18+. Upgrade: https://nodejs.org`);
101
+ return false;
102
+ }
103
+
104
+ async function detectOllama(host, { quiet = false } = {}) {
105
+ try {
106
+ const ctrl = new AbortController();
107
+ const t = setTimeout(() => ctrl.abort(), 2000);
108
+ const res = await fetch(new URL("/api/tags", host), { signal: ctrl.signal });
109
+ clearTimeout(t);
110
+ if (res.ok) {
111
+ const body = await res.json().catch(() => ({}));
112
+ const count = Array.isArray(body.models) ? body.models.length : 0;
113
+ if (!quiet) {
114
+ ok(`Ollama running at ${host} (${count} model${count === 1 ? "" : "s"} pulled)`);
115
+ }
116
+ return { running: true, models: body.models ?? [] };
117
+ }
118
+ } catch {
119
+ // fall through
120
+ }
121
+ // Reachable check failed — try `ollama --version` to distinguish
122
+ // not-installed vs not-running.
123
+ try {
124
+ const { stdout } = await pExec("ollama", ["--version"], { timeout: 2000 });
125
+ return { running: false, models: [], version: stdout.trim() };
126
+ } catch {
127
+ return { running: false, models: [], notInstalled: true };
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Prompt the user to run a command, then run it with inherited stdio
133
+ * (so sudo prompts and command output appear inline). Skipped if `yes`
134
+ * is true. Returns true if the command ran and exited 0; false if the
135
+ * user declined or the command failed.
136
+ */
137
+ async function confirmRun({ label, command, yes }) {
138
+ process.stdout.write(` Will run: ${command}\n`);
139
+ if (!yes) {
140
+ const ans = await question(` ${label}?`, { default: "y" });
141
+ if (!ans.toLowerCase().startsWith("y")) {
142
+ process.stdout.write(" skipped\n");
143
+ return false;
144
+ }
145
+ }
146
+ return new Promise((resolve) => {
147
+ const child = spawn("sh", ["-c", command], { stdio: "inherit" });
148
+ child.on("exit", (code) => {
149
+ if (code === 0) {
150
+ ok("done");
151
+ resolve(true);
152
+ } else {
153
+ fail(`command exited ${code}`);
154
+ resolve(false);
155
+ }
156
+ });
157
+ child.on("error", (err) => {
158
+ fail(`could not run: ${err?.message ?? err}`);
159
+ resolve(false);
160
+ });
161
+ });
162
+ }
163
+
164
+ async function startOllama({ yes }) {
165
+ // Linux: usually has the ollama systemd unit from the installer.
166
+ // macOS: usually opens the Ollama.app. Fall back to `ollama serve &`.
167
+ const isMac = process.platform === "darwin";
168
+ const cmd = isMac
169
+ ? "open -a Ollama || (ollama serve > /tmp/ollama.log 2>&1 &)"
170
+ : "sudo systemctl start ollama || (ollama serve > /tmp/ollama.log 2>&1 &)";
171
+ return confirmRun({
172
+ label: "Start Ollama now",
173
+ command: cmd,
174
+ yes
175
+ });
176
+ }
177
+
178
+ async function installOllama({ yes }) {
179
+ return confirmRun({
180
+ label: "Install Ollama now (will sudo)",
181
+ command: "curl -fsSL https://ollama.com/install.sh | sh",
182
+ yes
183
+ });
184
+ }
185
+
186
+ /**
187
+ * Spawn another `infernet` subcommand as a child process with stdio
188
+ * inherited, so the user sees the same prompts they'd see if they ran
189
+ * it themselves. Resolves with the child's exit code.
190
+ */
191
+ function runSubcommand(subcommand, extraArgs = []) {
192
+ const here = dirname(fileURLToPath(import.meta.url));
193
+ const cliEntry = join(here, "..", "index.js");
194
+ return new Promise((resolve, reject) => {
195
+ const child = spawn(process.execPath, [cliEntry, subcommand, ...extraArgs], {
196
+ stdio: "inherit"
197
+ });
198
+ child.on("exit", (code) => resolve(code ?? 1));
199
+ child.on("error", reject);
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Fetch the control-plane row for this node, or null. Network/auth
205
+ * errors → null so we don't block on a transient outage. Note that
206
+ * /api/v1/node/me wraps the row as `{ row: <actual> }`, so we have to
207
+ * unwrap before deciding whether registration succeeded.
208
+ */
209
+ async function fetchSelfRow(config) {
210
+ if (!config?.controlPlane?.url || !config?.node?.publicKey) return null;
211
+ try {
212
+ const client = createNodeClient({
213
+ url: config.controlPlane.url,
214
+ publicKey: config.node.publicKey,
215
+ privateKey: config.node.privateKey,
216
+ role: config.node.role,
217
+ timeoutMs: 5000
218
+ });
219
+ const resp = await client.me();
220
+ return resp?.row ?? null;
221
+ } catch {
222
+ return null;
223
+ }
224
+ }
225
+
226
+ async function isRegistered(config) {
227
+ return (await fetchSelfRow(config)) !== null;
228
+ }
229
+
230
+ /**
231
+ * After `infernet start` we want to know the daemon's heartbeats are
232
+ * actually landing on the control plane — not just that the local IPC
233
+ * socket is alive. Poll /me until last_seen is recent, or give up.
234
+ */
235
+ /**
236
+ * Probe the control plane for whether the node's advertised
237
+ * (address, port) is reachable from the public Internet. Prints a
238
+ * concrete port-forwarding cookbook on failure — the typical
239
+ * residential router case.
240
+ *
241
+ * The control-plane-mediated chat path doesn't actually need inbound
242
+ * (daemon polls/posts outbound), so unreachable is a soft warning.
243
+ * But IPIP-0002 P2P chat and libp2p discovery DO need it, so we
244
+ * surface this clearly.
245
+ */
246
+ async function reportReachability(row, config, port) {
247
+ const address = row?.address ?? null;
248
+ const advertisedPort = Number.isFinite(row?.port) ? row.port : port;
249
+ if (!address || !Number.isFinite(advertisedPort)) {
250
+ process.stdout.write(" reachability: skipped (no address/port advertised — fine for outbound-only setups)\n");
251
+ return;
252
+ }
253
+ const baseUrl = config?.controlPlane?.url;
254
+ if (!baseUrl) return;
255
+
256
+ process.stdout.write(` probing inbound reachability at ${address}:${advertisedPort}...\n`);
257
+ let probe;
258
+ try {
259
+ const url = new URL("/api/probe", baseUrl);
260
+ url.searchParams.set("host", address);
261
+ url.searchParams.set("port", String(advertisedPort));
262
+ const res = await fetch(url, { signal: AbortSignal.timeout?.(8000) });
263
+ probe = await res.json();
264
+ } catch (err) {
265
+ warn(` probe failed to run: ${err?.message ?? err}`);
266
+ return;
267
+ }
268
+
269
+ if (probe?.reachable) {
270
+ ok(`inbound reachable from the public Internet (${probe.elapsed_ms}ms)`);
271
+ return;
272
+ }
273
+
274
+ warn(`inbound NOT reachable: ${probe?.error ?? "unknown"}`);
275
+ process.stdout.write(
276
+ "\n This means: control-plane-routed chat jobs still work (your daemon's\n" +
277
+ " outbound polling doesn't need inbound connectivity), but direct P2P\n" +
278
+ " features can't reach you. Other operators won't see your node as a\n" +
279
+ " dial-able peer.\n"
280
+ );
281
+ process.stdout.write("\n How to punch a hole in your firewall (port forwarding):\n");
282
+ process.stdout.write(" 1. Open your router admin page. Common defaults:\n");
283
+ process.stdout.write(" http://192.168.1.1 (Linksys, Asus, TP-Link, Netgear)\n");
284
+ process.stdout.write(" http://192.168.0.1 (D-Link, Belkin, some Netgear)\n");
285
+ process.stdout.write(" http://10.0.0.1 (Comcast/Xfinity, ATT)\n");
286
+ process.stdout.write(" Or run `ip route | awk '/default/ {print $3}'` to see your gateway.\n");
287
+ process.stdout.write(" 2. Find the section called \"Port Forwarding\", \"Virtual Servers\",\n");
288
+ process.stdout.write(" \"NAT/Gaming\", or similar.\n");
289
+ process.stdout.write(" 3. Add a rule:\n");
290
+ process.stdout.write(` External port: ${advertisedPort} (TCP)\n`);
291
+ process.stdout.write(` Internal IP: <this machine's LAN IP — usually 192.168.x.x>\n`);
292
+ process.stdout.write(` Internal port: ${advertisedPort} (TCP)\n`);
293
+ process.stdout.write(" 4. Save / apply. Some routers reboot.\n");
294
+ process.stdout.write(" 5. Re-run `infernet setup` to re-probe (or wait ~10 min and the\n");
295
+ process.stdout.write(" daemon will re-check on its own).\n");
296
+ process.stdout.write("\n Or, if you don't want to expose your home IP at all, run\n");
297
+ process.stdout.write(" `infernet start --no-advertise` so the daemon doesn't broadcast its\n");
298
+ process.stdout.write(" address. You'll still get jobs via the control plane.\n");
299
+ }
300
+
301
+ async function waitForFreshHeartbeat(config, { timeoutMs = 45000, freshMs = 60000 } = {}) {
302
+ const deadline = Date.now() + timeoutMs;
303
+ let lastRow = null;
304
+ while (Date.now() < deadline) {
305
+ const row = await fetchSelfRow(config);
306
+ if (row) {
307
+ lastRow = row;
308
+ const lastSeen = row.last_seen ? Date.parse(row.last_seen) : 0;
309
+ const fresh = Number.isFinite(lastSeen) && Date.now() - lastSeen < freshMs;
310
+ if (fresh && row.status === "available") return { ok: true, row };
311
+ }
312
+ await new Promise((r) => setTimeout(r, 3000));
313
+ }
314
+ return { ok: false, row: lastRow };
315
+ }
316
+
317
+ async function pullModel(name) {
318
+ process.stdout.write(`\n → pulling ${name} via the ollama CLI (this may take a while)...\n\n`);
319
+ try {
320
+ const child = spawn("ollama", ["pull", name], {
321
+ stdio: ["ignore", "inherit", "inherit"]
322
+ });
323
+ await new Promise((resolve, reject) => {
324
+ child.on("exit", (code) => {
325
+ if (code === 0) resolve();
326
+ else reject(new Error(`ollama pull exited code ${code}`));
327
+ });
328
+ child.on("error", reject);
329
+ });
330
+ process.stdout.write("\n");
331
+ ok(`ollama pull ${name} completed`);
332
+ return true;
333
+ } catch (err) {
334
+ fail(`pull failed: ${err?.message ?? err}`);
335
+ return false;
336
+ }
337
+ }
338
+
339
+ async function verifyModelPulled(host, name) {
340
+ try {
341
+ const ctrl = new AbortController();
342
+ const t = setTimeout(() => ctrl.abort(), 3000);
343
+ const res = await fetch(new URL("/api/tags", host), { signal: ctrl.signal });
344
+ clearTimeout(t);
345
+ if (!res.ok) return false;
346
+ const body = await res.json();
347
+ const have = (body.models ?? []).some((m) => (m.name ?? m.model) === name);
348
+ return have;
349
+ } catch {
350
+ return false;
351
+ }
352
+ }
353
+
354
+ async function chooseModel(installed, opts) {
355
+ const { vramGb, ramGb } = opts.capacity ?? { vramGb: 0, ramGb: 0 };
356
+
357
+ // Pre-evaluate fit so we can both annotate the menu AND auto-pick a
358
+ // sensible default for `--yes` mode.
359
+ const evaluated = MODEL_SUGGESTIONS.map((m) => {
360
+ const f = checkFits({ size_gb: m.size_gb, vram_gb: vramGb, ram_gb: ramGb });
361
+ return { ...m, fits: f.ok, mode: f.mode, ceiling_gb: f.ceiling_gb };
362
+ });
363
+ const fittingOnly = evaluated.filter((m) => m.fits);
364
+ const isCpuOnly = !(Number.isFinite(vramGb) && vramGb > 0);
365
+ // CPU-only: default to smallest model — large models are impractically
366
+ // slow on CPU and will lock the machine. GPU: largest fitting model.
367
+ const defaultModel = isCpuOnly
368
+ ? MODEL_SUGGESTIONS[0].name // qwen2.5:0.5b
369
+ : fittingOnly.length > 0
370
+ ? fittingOnly[fittingOnly.length - 1].name
371
+ : MODEL_SUGGESTIONS[0].name;
372
+
373
+ if (opts.preselected) {
374
+ const m = evaluated.find((x) => x.name === opts.preselected);
375
+ if (m && !m.fits) {
376
+ if (opts.yes) {
377
+ // In non-interactive mode, fall back to the auto-detected default
378
+ // rather than pulling a model that will OOM or crawl on CPU.
379
+ warn(`${opts.preselected} won't fit on this host — falling back to ${defaultModel}`);
380
+ return defaultModel;
381
+ }
382
+ warn(`preselected ${opts.preselected} (${m.size}) likely won't fit (${m.mode} ceiling ≈ ${m.ceiling_gb} GB)`);
383
+ warn(" Pulling anyway because --model overrides the fit check. Inference may OOM.");
384
+ }
385
+ return opts.preselected;
386
+ }
387
+ if (opts.yes) return defaultModel;
388
+
389
+ process.stdout.write("\n Which model should this node serve?\n");
390
+ evaluated.forEach((m, i) => {
391
+ const installedFlag = installed.includes(m.name) ? " [pulled]" : "";
392
+ const fitFlag = m.fits
393
+ ? ""
394
+ : ` [✗ won't fit on this host — ${m.mode} ceiling ≈ ${m.ceiling_gb} GB]`;
395
+ process.stdout.write(
396
+ ` ${i + 1}) ${m.name.padEnd(16)} ${m.size.padEnd(10)} ${m.note}${installedFlag}${fitFlag}\n`
397
+ );
398
+ });
399
+ process.stdout.write(` ${MODEL_SUGGESTIONS.length + 1}) other (enter manually)\n`);
400
+ const defaultIdx = MODEL_SUGGESTIONS.findIndex((m) => m.name === defaultModel) + 1;
401
+ const ans = await question(" Choice", { default: String(defaultIdx) });
402
+ const n = Number.parseInt(ans, 10);
403
+ if (n >= 1 && n <= MODEL_SUGGESTIONS.length) {
404
+ const picked = evaluated[n - 1];
405
+ if (!picked.fits) {
406
+ const cont = await question(
407
+ ` ${picked.name} likely won't fit (${picked.mode} ceiling ≈ ${picked.ceiling_gb} GB). Pull anyway?`,
408
+ { default: "n" }
409
+ );
410
+ if (!cont.toLowerCase().startsWith("y")) {
411
+ process.stdout.write(" Aborting model pick. Re-run setup and choose a smaller model.\n");
412
+ return null;
413
+ }
414
+ }
415
+ return picked.name;
416
+ }
417
+ if (n === MODEL_SUGGESTIONS.length + 1) {
418
+ const custom = await question(" Model name (e.g. mistral:7b)", {});
419
+ return custom || null;
420
+ }
421
+ return defaultModel;
422
+ }
423
+
424
+ export default async function setup(args) {
425
+ if (args.has("help") || args.has("h")) {
426
+ process.stdout.write(HELP);
427
+ return 0;
428
+ }
429
+
430
+ const yes =
431
+ args.has("yes") ||
432
+ args.has("confirm") ||
433
+ process.env.INFERNET_NONINTERACTIVE === "1";
434
+ const skipPull = args.has("skip-pull");
435
+ const skipFirewall = args.has("no-firewall");
436
+ const skipIdentity = args.has("skip-identity");
437
+ const skipRegister = args.has("skip-register");
438
+ const skipDaemon = args.has("skip-daemon") || args.has("no-start");
439
+ const backend = String(args.get("backend") ?? "ollama");
440
+ const host = String(args.get("host") ?? process.env.OLLAMA_HOST ?? "http://localhost:11434");
441
+ const preselectedModel = args.get("model") ? String(args.get("model")) : null;
442
+
443
+ const existing = (await loadConfig()) ?? {};
444
+ const portArg = args.get("port");
445
+ const port = Number.parseInt(portArg ?? "", 10) || resolveP2pPort(existing);
446
+
447
+ // Fix config file permissions on every setup run — self-heals nodes
448
+ // where the file was created with wrong mode (666 instead of 600).
449
+ await fixConfigPermissions();
450
+
451
+ process.stdout.write("\nInfernet setup — checking your environment\n");
452
+
453
+ // Step count: hardware (1) + node (1) + ollama+model (0|2) + firewall (0|1)
454
+ // + logrotate (0|1) + config (1) + identity (0|1) + registration (0|1)
455
+ // + daemon (0|1) + login (0|1).
456
+ // Track the actual maximum so the printed counter never goes [7/6].
457
+ const isProviderRole = (existing?.node?.role ?? "provider") === "provider";
458
+ let total = 2; // hardware + node.js
459
+ if (backend === "ollama") total += 2; // ollama + model
460
+ if (!skipFirewall && process.platform === "linux") total += 1;
461
+ if (process.platform === "linux") total += 1; // logrotate
462
+ total += 1; // config
463
+ if (!skipIdentity) total += 1;
464
+ if (!skipRegister && isProviderRole) total += 1;
465
+ if (!skipDaemon && isProviderRole) total += 1;
466
+ total += 1; // login (always offered)
467
+ let n = 0;
468
+
469
+ // ---- Hardware (always; first so the operator sees what setup sees) ----
470
+ n += 1;
471
+ step(n, total, "Hardware");
472
+ const hostInfo = detectHost();
473
+ process.stdout.write(` platform: ${hostInfo.platform}/${hostInfo.arch}\n`);
474
+ process.stdout.write(` ram: ${(hostInfo.total_ram_mb / 1024).toFixed(1)} GB total\n`);
475
+ const cpuList = detectCpus();
476
+ if (cpuList.length === 0) {
477
+ warn("no CPUs detected (this is unusual)");
478
+ } else {
479
+ ok(`${cpuList.length} CPU group${cpuList.length === 1 ? "" : "s"} (${hostInfo.cpu_count} logical cores)`);
480
+ for (const c of cpuList) {
481
+ process.stdout.write(` · ${formatCpuLine(c)}\n`);
482
+ }
483
+ }
484
+ let detectedGpus = [];
485
+ try {
486
+ detectedGpus = await detectGpus();
487
+ } catch (err) {
488
+ warn(`gpu detection error: ${err?.message ?? err}`);
489
+ }
490
+ if (detectedGpus.length === 0) {
491
+ warn("no GPUs detected — this node will register as CPU-only");
492
+ // Surface per-vendor "why" so an operator with hardware that
493
+ // SHOULD be detected (e.g. a desktop with an Nvidia card but
494
+ // no driver installed) gets an actionable hint instead of a
495
+ // mystery "CPU-only" verdict.
496
+ const diag = lastDetectionDiagnostics();
497
+ const lines = Object.entries(diag).map(([k, v]) => `${k}: ${v}`);
498
+ if (lines.length > 0) {
499
+ process.stdout.write("\n Why detection came up empty:\n");
500
+ for (const line of lines) process.stdout.write(` · ${line}\n`);
501
+ process.stdout.write(
502
+ "\n If you have hardware that should be detected:\n" +
503
+ " NVIDIA → install drivers + nvidia-smi (https://www.nvidia.com/Download/index.aspx)\n" +
504
+ " AMD → install ROCm (https://rocm.docs.amd.com/en/latest/install/install.html)\n" +
505
+ " Apple → on macOS only — Linux can't read Apple Silicon GPUs\n" +
506
+ " Intel → integrated GPUs aren't queried (Arc/Iris support is roadmap)\n" +
507
+ " Then re-run `infernet setup`.\n"
508
+ );
509
+ }
510
+ } else {
511
+ ok(`${detectedGpus.length} GPU${detectedGpus.length === 1 ? "" : "s"}`);
512
+ for (const g of detectedGpus) {
513
+ process.stdout.write(` · ${formatGpuLine(g)}\n`);
514
+ }
515
+ // If we fell through to lspci, every entry has source: 'lspci' —
516
+ // we know the hardware is present but can't query its specs.
517
+ // Tell the operator how to upgrade detection.
518
+ const lspciOnly = detectedGpus.every((g) => g.source === "lspci");
519
+ if (lspciOnly) {
520
+ process.stdout.write(
521
+ "\n Detected via lspci only (vendor tooling missing).\n" +
522
+ " VRAM / utilization / temperature won't be reported until you install\n" +
523
+ " the right CLI for your card (nvidia-smi for NVIDIA, rocm-smi for AMD).\n"
524
+ );
525
+ }
526
+ }
527
+
528
+ // Interconnects — NVLink between GPUs, InfiniBand for multi-node training.
529
+ let interconnects = { nvlink: { available: false, links: [] }, infiniband: { available: false, devices: [] }, rdma_capable: false };
530
+ try {
531
+ interconnects = await detectInterconnects();
532
+ } catch (err) {
533
+ warn(`interconnect detection error: ${err?.message ?? err}`);
534
+ }
535
+ process.stdout.write(` interconnect: ${formatInterconnectSummary(interconnects)}\n`);
536
+ if (interconnects.nvlink.available) {
537
+ const links = interconnects.nvlink.links;
538
+ const preview = links.slice(0, 6).map((l) => `GPU${l.from}↔GPU${l.to} ${l.kind}`).join(", ");
539
+ process.stdout.write(` · NVLink topology: ${interconnects.nvlink.topology}${links.length > 6 ? ` — ${preview}, …` : ` — ${preview}`}\n`);
540
+ }
541
+ if (interconnects.infiniband.available) {
542
+ const active = interconnects.infiniband.devices.filter((d) => d.state === "active");
543
+ for (const d of active.slice(0, 6)) {
544
+ const rate = d.rate ? ` ${d.rate}` : "";
545
+ const ll = d.link_layer ? ` ${d.link_layer}` : "";
546
+ process.stdout.write(` · IB ${d.name}/p${d.port}${rate}${ll} (active)\n`);
547
+ }
548
+ }
549
+
550
+ n += 1;
551
+ step(n, total, "Node.js");
552
+ const nodeOk = await detectNode();
553
+ if (!nodeOk) return 1;
554
+
555
+ let ollamaState = { running: false, models: [] };
556
+ let chosenModel = null;
557
+
558
+ if (backend === "ollama") {
559
+ n += 1;
560
+ step(n, total, `Ollama (${host})`);
561
+ ollamaState = await detectOllama(host);
562
+
563
+ if (ollamaState.notInstalled) {
564
+ warn("Ollama not installed");
565
+ const installed = await installOllama({ yes });
566
+ if (!installed) {
567
+ process.stdout.write(
568
+ "\n Re-run `infernet setup` after Ollama is installed.\n"
569
+ );
570
+ return 1;
571
+ }
572
+ // Re-probe — the installer usually starts the daemon on Linux,
573
+ // but not always on macOS.
574
+ ollamaState = await detectOllama(host, { quiet: true });
575
+ }
576
+
577
+ if (!ollamaState.running) {
578
+ warn(
579
+ ollamaState.version
580
+ ? `Ollama installed (${ollamaState.version}) but not reachable at ${host}`
581
+ : `Ollama installed but not reachable at ${host}`
582
+ );
583
+ const started = await startOllama({ yes });
584
+ if (started) {
585
+ // Give the daemon a beat to bind, then re-probe.
586
+ await new Promise((r) => setTimeout(r, 1500));
587
+ ollamaState = await detectOllama(host, { quiet: true });
588
+ }
589
+ if (!ollamaState.running) {
590
+ process.stdout.write(
591
+ "\n Could not bring Ollama up. Start it manually, then re-run `infernet setup`.\n"
592
+ );
593
+ return 1;
594
+ }
595
+ }
596
+ ok(`Ollama running at ${host} (${ollamaState.models?.length ?? 0} model${(ollamaState.models?.length ?? 0) === 1 ? "" : "s"} pulled)`);
597
+
598
+ n += 1;
599
+ step(n, total, "Model");
600
+ const installedNames = (ollamaState.models ?? []).map((m) => m.name ?? m.model);
601
+ if (skipPull) {
602
+ warn("--skip-pull set; not pulling any model");
603
+ chosenModel = preselectedModel ?? installedNames[0] ?? null;
604
+ } else if (installedNames.length > 0 && !preselectedModel && yes) {
605
+ // Non-interactive + already have something — keep what's there.
606
+ chosenModel = installedNames[0];
607
+ ok(`using already-installed ${chosenModel}`);
608
+ } else {
609
+ // Compute host capacity (in GB) for the model fit-check. Sum
610
+ // VRAM across all detected GPUs (multi-GPU = tensor parallel
611
+ // via Ollama). RAM falls back to the host total when no GPU
612
+ // info is available.
613
+ const vramGb = detectedGpus.reduce(
614
+ (a, g) => a + (Number.isFinite(g.vram_mb) ? g.vram_mb / 1024 : 0),
615
+ 0
616
+ );
617
+ const ramGb = hostInfo.total_ram_mb / 1024;
618
+ chosenModel = await chooseModel(installedNames, {
619
+ yes,
620
+ preselected: preselectedModel,
621
+ capacity: { vramGb, ramGb }
622
+ });
623
+ if (!chosenModel) {
624
+ fail("no model selected — aborting");
625
+ return 1;
626
+ }
627
+ ok(`selected model: ${chosenModel}`);
628
+ if (!installedNames.includes(chosenModel)) {
629
+ const okPull = await pullModel(chosenModel);
630
+ if (!okPull) return 1;
631
+ } else {
632
+ ok(`${chosenModel} already pulled — skipping download`);
633
+ }
634
+ // Always verify, regardless of whether we just pulled or skipped.
635
+ const ready = await verifyModelPulled(host, chosenModel);
636
+ if (!ready) {
637
+ fail(`${chosenModel} not visible to ollama after pull — check \`ollama list\``);
638
+ return 1;
639
+ }
640
+ ok(`verified: ollama can serve ${chosenModel}`);
641
+ }
642
+ }
643
+
644
+ if (!skipFirewall && process.platform === "linux") {
645
+ n += 1;
646
+ step(n, total, `Firewall (port ${port}/tcp)`);
647
+ const detected = detectFirewall();
648
+ if (detected.length === 0) {
649
+ warn("no firewall manager detected (ufw / firewalld / nftables / iptables)");
650
+ warn(" if you have one, open the port manually");
651
+ } else {
652
+ const tool = detected[0];
653
+ const { lines } = describeFirewallHowTo(port);
654
+ process.stdout.write(` Detected: ${tool}\n`);
655
+ for (const line of lines.slice(0, 4)) process.stdout.write(` ${line}\n`);
656
+
657
+ let proceed = yes;
658
+ if (!yes) {
659
+ const ans = await question(` Apply now (will sudo)?`, { default: "y" });
660
+ proceed = ans.toLowerCase().startsWith("y");
661
+ }
662
+ if (proceed) {
663
+ try {
664
+ const result = await applyFirewallRule(port, { tool });
665
+ if (result.applied) ok(`firewall rule applied via ${result.tool}`);
666
+ else warn(result.reason ?? "skipped");
667
+ } catch (err) {
668
+ fail(`firewall rule failed: ${err?.message ?? err}`);
669
+ warn(" re-run later or apply the command manually");
670
+ }
671
+ } else {
672
+ process.stdout.write(" skipped — run `infernet firewall` to print the commands\n");
673
+ }
674
+ }
675
+ }
676
+
677
+ // Logrotate — install /etc/logrotate.d/infernet on Linux if writable.
678
+ if (process.platform === "linux") {
679
+ n += 1;
680
+ step(n, total, "Log rotation");
681
+ const logrotatePath = "/etc/logrotate.d/infernet";
682
+ const logrotateConf = `/var/log/infernet/*.log {
683
+ daily
684
+ rotate 14
685
+ compress
686
+ delaycompress
687
+ missingok
688
+ notifempty
689
+ copytruncate
690
+ create 0640 root root
691
+ }
692
+ `;
693
+ try {
694
+ await fs.writeFile(logrotatePath, logrotateConf, { mode: 0o644 });
695
+ ok(`logrotate config written to ${logrotatePath} (daily, 14 days)`);
696
+ } catch {
697
+ warn(`could not write ${logrotatePath} (not root?) — logs won't rotate automatically`);
698
+ warn(` run as root or manually add the config: https://infernetprotocol.com/book/02-node-operators/monitoring`);
699
+ }
700
+ }
701
+
702
+ n += 1;
703
+ step(n, total, "Config");
704
+ const merged = {
705
+ ...existing,
706
+ engine: {
707
+ ...(existing.engine ?? {}),
708
+ backend,
709
+ ...(host ? { ollamaHost: host } : {}),
710
+ ...(chosenModel ? { model: chosenModel } : {})
711
+ }
712
+ };
713
+ await saveConfig(merged);
714
+ ok(`saved to ${getConfigPath()}`);
715
+ process.stdout.write(` engine.backend: ${merged.engine.backend}\n`);
716
+ if (merged.engine.ollamaHost) {
717
+ process.stdout.write(` engine.ollamaHost: ${merged.engine.ollamaHost}\n`);
718
+ }
719
+ if (merged.engine.model) {
720
+ process.stdout.write(` engine.model: ${merged.engine.model}\n`);
721
+ }
722
+
723
+ // ---- Identity (calls `infernet init` if needed) ----
724
+ let configAfterId = await loadConfig();
725
+ if (!skipIdentity) {
726
+ n += 1;
727
+ step(n, total, "Identity & control plane");
728
+ const hasKey = !!(configAfterId?.node?.publicKey && configAfterId?.node?.privateKey);
729
+ const hasUrl = !!configAfterId?.controlPlane?.url;
730
+ if (hasKey && hasUrl) {
731
+ ok(`identity ready: ${configAfterId.node.publicKey.slice(0, 12)}…, role=${configAfterId.node.role ?? "?"}`);
732
+ ok(`control plane: ${configAfterId.controlPlane.url}`);
733
+ } else {
734
+ warn(hasKey || hasUrl ? "partial identity — finishing init" : "no identity yet — running init");
735
+ const code = await runSubcommand("init", yes ? ["--yes"] : []);
736
+ if (code !== 0) {
737
+ fail(`init exited ${code}`);
738
+ return code;
739
+ }
740
+ configAfterId = await loadConfig();
741
+ ok(`identity: ${configAfterId?.node?.publicKey?.slice(0, 12) ?? "?"}…`);
742
+ }
743
+ }
744
+
745
+ const role = configAfterId?.node?.role ?? "provider";
746
+ const isProvider = role === "provider";
747
+
748
+ // ---- Registration (always re-runs; register is an upsert, and the
749
+ // payload shape — specs.cpu, specs.interconnects, etc. — evolves,
750
+ // so re-registering keeps the row current with the running CLI). ----
751
+ if (!skipRegister && isProvider && configAfterId) {
752
+ n += 1;
753
+ step(n, total, "Provider registration");
754
+ const code = await runSubcommand("register", []);
755
+ if (code !== 0) {
756
+ fail(`register exited ${code}`);
757
+ warn(" re-run `infernet register` once the control plane is reachable");
758
+ } else {
759
+ ok("registered (specs synced)");
760
+ }
761
+ }
762
+
763
+ // ---- Daemon (always restart so it picks up current CLI code; providers only) ----
764
+ if (!skipDaemon && isProvider && configAfterId) {
765
+ n += 1;
766
+ step(n, total, "Daemon");
767
+ const wasAlive = await isDaemonAlive(500);
768
+ if (wasAlive) {
769
+ process.stdout.write(" daemon was running — restarting so it picks up current CLI code\n");
770
+ try {
771
+ await runSubcommand("stop", []);
772
+ } catch {
773
+ // best-effort
774
+ }
775
+ // Give the OS a moment to release the IPC socket / pidfile.
776
+ await new Promise((r) => setTimeout(r, 800));
777
+ }
778
+ const startCode = await runSubcommand("start", []);
779
+ if (startCode !== 0) {
780
+ fail(`start exited ${startCode}`);
781
+ warn(" check `infernet logs` and `infernet status`");
782
+ } else {
783
+ // Poll up to 15 s — ESM cold-start can take a while on slow machines.
784
+ let aliveNow = false;
785
+ for (let i = 0; i < 30 && !aliveNow; i++) {
786
+ await new Promise((r) => setTimeout(r, 500));
787
+ aliveNow = await isDaemonAlive(500);
788
+ }
789
+ if (aliveNow) ok(wasAlive ? "daemon restarted" : "daemon started");
790
+ else warn("daemon spawned but not yet responding — check `infernet logs`");
791
+ }
792
+
793
+ // Heartbeat verification — alive locally isn't enough; the control
794
+ // plane needs to see a fresh last_seen before chat will route to us.
795
+ if (await isDaemonAlive(500)) {
796
+ process.stdout.write(" verifying heartbeat reaches control plane (up to 45s)...\n");
797
+ const res = await waitForFreshHeartbeat(configAfterId);
798
+ if (res.ok) {
799
+ ok(`heartbeat ok — last_seen ${res.row.last_seen}, status=${res.row.status}`);
800
+ await reportReachability(res.row, configAfterId, port);
801
+ } else if (res.row) {
802
+ warn(
803
+ `daemon alive locally but control plane shows stale state ` +
804
+ `(last_seen=${res.row.last_seen ?? "never"}, status=${res.row.status ?? "?"}). ` +
805
+ `Check 'infernet logs' for heartbeat errors.`
806
+ );
807
+ } else {
808
+ warn(
809
+ `control plane has no row for this pubkey. Either registration ` +
810
+ `didn't complete, or the daemon is using a different identity. ` +
811
+ `Try 'infernet register' then 'infernet doctor'.`
812
+ );
813
+ }
814
+ }
815
+ }
816
+
817
+ // ---- Login (offer to chain into device-code flow; auto-links pubkey on success) ----
818
+ n += 1;
819
+ step(n, total, "Sign-in");
820
+ const alreadySignedIn = !!(configAfterId?.auth?.bearerToken);
821
+ let didLogin = false;
822
+ if (alreadySignedIn) {
823
+ ok(`signed in as ${configAfterId.auth.email ?? configAfterId.auth.userId}`);
824
+ } else {
825
+ // Try autonomous login using the node's Nostr keypair — no browser needed.
826
+ // Falls back to device-code (browser) flow only if the node isn't registered yet.
827
+ const autoCode = await nodeTokenLogin(configAfterId ?? {});
828
+ if (autoCode === 0) {
829
+ const fresh = await loadConfig();
830
+ ok(`signed in as ${fresh?.auth?.email ?? fresh?.auth?.userId ?? "node"} (auto)`);
831
+ didLogin = true;
832
+ } else if (yes) {
833
+ process.stdout.write(" auto-login failed — node may not be registered yet. Run `infernet login` when ready.\n");
834
+ } else {
835
+ process.stdout.write(" Sign in to view your dashboard, manage payouts, and submit paid jobs.\n");
836
+ const ans = await question(" Sign in now (opens browser)?", { default: "y" });
837
+ if (ans.toLowerCase().startsWith("y")) {
838
+ const code = await runSubcommand("login", []);
839
+ if (code !== 0) warn(" login did not complete — re-run `infernet login` later");
840
+ else { ok("signed in"); didLogin = true; }
841
+ } else {
842
+ process.stdout.write(" skipped — run `infernet login` later\n");
843
+ }
844
+ }
845
+ }
846
+
847
+ // ---- Pubkey link (only needed when already-signed-in: a fresh login
848
+ // already auto-links via login.js → autoLinkPubkey). Without this
849
+ // step, /dashboard couldn't tell which providers belong to a user
850
+ // who signed in before they ran `infernet init`. ----
851
+ if (alreadySignedIn && !didLogin && isProvider && configAfterId?.node?.publicKey) {
852
+ const code = await runSubcommand("pubkey", ["link"]);
853
+ if (code !== 0) warn(" pubkey link did not complete — re-run `infernet pubkey link` manually");
854
+ }
855
+
856
+ process.stdout.write("\n");
857
+ ok("setup complete");
858
+ process.stdout.write("\nTry it:\n");
859
+ if (chosenModel && configAfterId?.controlPlane?.url) {
860
+ process.stdout.write(` infernet chat "what is 2+2?" # P2P\n`);
861
+ process.stdout.write(` infernet chat --local "what is 2+2?" # local engine\n`);
862
+ } else if (chosenModel) {
863
+ process.stdout.write(` infernet chat "what is 2+2?"\n`);
864
+ } else {
865
+ process.stdout.write(` infernet chat --backend stub "smoke test"\n`);
866
+ }
867
+ process.stdout.write(` infernet doctor # full health check\n`);
868
+ return 0;
869
+ }