neurastream 0.2.65 → 0.2.66
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.
- package/README.md +31 -0
- package/package.json +1 -1
- package/src/adapters/index.ts +7 -0
- package/src/adapters/omp/adapter.ts +1 -0
- package/src/adapters/omp/doctor.ts +4 -2
- package/src/cli/config-set.ts +56 -0
- package/src/cli/dispatch.ts +8 -0
- package/src/cli/init.ts +6 -4
- package/src/cli/setup.ts +327 -0
- package/src/cli/ui.ts +4 -2
- package/src/core/config-edit.ts +111 -0
- package/src/core/pages-setup.ts +6 -21
- package/src/core/setup-detect.ts +120 -0
- package/src/core/tailscale.ts +20 -0
- package/src/core/ui-setup.ts +52 -64
- package/templates/config.yml +127 -128
package/README.md
CHANGED
|
@@ -58,8 +58,16 @@ omp -p "say hi" # plain omp: no neurastream hook, no guard-log line
|
|
|
58
58
|
|
|
59
59
|
## Install
|
|
60
60
|
|
|
61
|
+
`neurastream setup` is the one-command path: it detects this machine (harness, Tailscale, machine, secrets), asks the settings in groups with the detected values as defaults, installs the adapter, runs `doctor`, and prints `Next: neurastream main` when it passes. `--yes` takes the detected settings without asking.
|
|
62
|
+
|
|
61
63
|
```sh
|
|
62
64
|
bun add -g neurastream
|
|
65
|
+
neurastream setup
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The manual path, if you want each step yourself:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
63
71
|
neurastream init
|
|
64
72
|
neurastream adapter install omp
|
|
65
73
|
neurastream doctor
|
|
@@ -250,6 +258,29 @@ watch:
|
|
|
250
258
|
interval_min: 15 # minutes between watcher runs
|
|
251
259
|
```
|
|
252
260
|
|
|
261
|
+
## Several Mains
|
|
262
|
+
|
|
263
|
+
You can run more than one Main at a time on the same queue. Each one claims the plans it works on, and no other Main can touch a claimed plan while the claim is live. Start each with its own name:
|
|
264
|
+
|
|
265
|
+
```sh
|
|
266
|
+
neurastream main --name a # terminal 1
|
|
267
|
+
neurastream main --name b # terminal 2
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Names match `[a-z0-9-]{1,24}`; plain `neurastream main` is named `main`. A second launcher with the same name refuses (`Main a is already running`) — the running lock under `<queue_state>/mains/<id>/` is freed by the kernel when the launcher dies, so a crash leaves nothing to clean up.
|
|
271
|
+
|
|
272
|
+
What each Main keeps for itself, under `<queue_state>/mains/<id>/`:
|
|
273
|
+
|
|
274
|
+
- its claims: a spawn on a plan claims it for this Main first, and another Main's live claim refuses the launch with `plan <file> is held by Main <id> since HH:MM` (a Main's own plan edit, tick, close or push is gated the same way) — two Mains racing for one plan get exactly one winner;
|
|
275
|
+
- its owner-inbox cursor (`inbox-cursor`): a message you send starting `@<id> ` goes only to that Main, anything else reaches all running Mains, and `@<id>` naming a Main that is not running is refused (`Main b is not running; running: a`) before anything is stored;
|
|
276
|
+
- its rollover notes (`rollover/`) and edit-window state (`edit-window.json`).
|
|
277
|
+
|
|
278
|
+
Who holds what: the queue summary, `neurastream main-start` and the phone page flag every plan `held by <id> since HH:MM` or `free`, and queue cards carry the holder chip. `neurastream claim list` prints the live claims, `neurastream claim next` claims the highest free plan by the queue's own order, and `neurastream claim release <plan>` frees your Main's own claim.
|
|
279
|
+
|
|
280
|
+
The 30-minute rule: a claim lives by its heartbeat — the adapter beats every claim its Main holds once a minute while that session lives. When a Main dies (crash, kill, closed terminal), the beats stop and a claim whose last beat is `queue.claim_ttl_min` (default 30) minutes old is free; the next Main to spawn on that plan takes it. A session that shuts down releases nothing — the TTL is what frees a dead Main's claims (closing a plan the normal way, with `queue-close`, frees the closer's own claim). To free another Main's live claim sooner there is `neurastream claim release <plan> --force` — refused for every agent by the guard; that is the owner's move, in a terminal.
|
|
281
|
+
|
|
282
|
+
Claim heartbeats are not the job heartbeats of the watch section above. A job heartbeat says a cron job ran (`<queue_state>/heartbeats/<name>.json`, stale per `watch.expect`); a claim heartbeat is per-Main liveness recorded in the claim itself, under `<queue_state>/claims/` — one JSON file per plan, its `beat` refreshed every minute. Claims are never wired into `watch`.
|
|
283
|
+
|
|
253
284
|
## Secrets never reach the chat
|
|
254
285
|
|
|
255
286
|
A value the machine holds is hidden before the model, the chat and the session log see it (`src/core/secret-redact.ts`, plan 20261025_2204, incident I-20261025-197). Detection is by **content**, not by file name — the old name list never knew about `~/.npmrc`.
|
package/package.json
CHANGED
package/src/adapters/index.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { ompAdapter } from "./omp/adapter.ts";
|
|
|
11
11
|
|
|
12
12
|
export interface HarnessAdapter {
|
|
13
13
|
name: string;
|
|
14
|
+
/** The command the harness runs; setup checks it is on PATH. */
|
|
15
|
+
bin: string;
|
|
14
16
|
/** Session reader: the runs and the main session (docs/adapter.md). */
|
|
15
17
|
runSource(cfg: Config): RunSource;
|
|
16
18
|
/** Completed subagent task-results announced in the newest main session file. */
|
|
@@ -53,3 +55,8 @@ export function getAdapter(cfg?: Pick<Config, "harness">): HarnessAdapter {
|
|
|
53
55
|
if (!adapter) throw new Error(`unknown harness "${name}"; known: ${Object.keys(ADAPTERS).join(", ")}`);
|
|
54
56
|
return adapter;
|
|
55
57
|
}
|
|
58
|
+
|
|
59
|
+
/** Every registered harness with the command it runs (setup checks those are on PATH). */
|
|
60
|
+
export function harnessBins(): Pick<HarnessAdapter, "name" | "bin">[] {
|
|
61
|
+
return Object.values(ADAPTERS);
|
|
62
|
+
}
|
|
@@ -10,6 +10,7 @@ import { dumpModel, dumpParts, dumpPromptTokens, ompDumps, ompScoreTraces, ompTr
|
|
|
10
10
|
|
|
11
11
|
export const ompAdapter: HarnessAdapter = {
|
|
12
12
|
name: "omp",
|
|
13
|
+
bin: "omp",
|
|
13
14
|
runSource: defaultRunSource,
|
|
14
15
|
completedResults: defaultCompletedResults,
|
|
15
16
|
scoreTraces: ompScoreTraces,
|
|
@@ -220,9 +220,11 @@ export function doctorReport(home: string, env: NodeJS.ProcessEnv = process.env,
|
|
|
220
220
|
// The owner page (plan 20261024_2230): when it is switched on, the login must be set and
|
|
221
221
|
// tailscale must answer, or the unit will start and refuse, or serve nobody.
|
|
222
222
|
if (cfg.ui.enabled) {
|
|
223
|
+
const state = tailscale.state();
|
|
224
|
+
if (state === "running") ok.push("tailscale: ok");
|
|
225
|
+
else problems.push(`tailscale: ${state === "missing" ? "missing" : "logged out"}`);
|
|
223
226
|
if (!cfg.ui.owner_login) problems.push("ui.enabled but ui.owner_login is empty");
|
|
224
|
-
else if (
|
|
225
|
-
else ok.push(`ui owner page: login ${cfg.ui.owner_login}`);
|
|
227
|
+
else if (state === "running") ok.push(`ui owner page: login ${cfg.ui.owner_login}`);
|
|
226
228
|
}
|
|
227
229
|
// One index (20261024_1400_one_index): repo-directory lines in the main index are the old
|
|
228
230
|
// two-level queue; the plans they hold are invisible to queue-check until migrate folds them.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// `neurastream config set|get|list <key> [value]` (plan 20261028_2356 P1b): the non-interactive door
|
|
2
|
+
// to the generic config editor (src/core/config-edit.ts). Keys are the leaf paths of
|
|
3
|
+
// templates/config.yml, so a typo is refused and never written. A set writes through the same
|
|
4
|
+
// line-based editor and is refused, with the file untouched, when the result would not load.
|
|
5
|
+
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { dirname } from "node:path";
|
|
9
|
+
import { checkEdited, leafKeys, setConfigKeys, valueAt, yamlValue } from "../core/config-edit.ts";
|
|
10
|
+
import { TEMPLATE_PATH, configFile, loadConfig } from "../core/config.ts";
|
|
11
|
+
|
|
12
|
+
const VALID_KEYS = leafKeys(Bun.YAML.parse(readFileSync(TEMPLATE_PATH, "utf8")));
|
|
13
|
+
|
|
14
|
+
export function runConfigKeys(args: string[], home: string = homedir()): number {
|
|
15
|
+
const [verb, key, raw] = args;
|
|
16
|
+
try {
|
|
17
|
+
if (verb === "list") {
|
|
18
|
+
const cfg = loadConfig({ home });
|
|
19
|
+
for (const k of VALID_KEYS) {
|
|
20
|
+
const v = valueAt(cfg, k);
|
|
21
|
+
console.log(`${k} = ${Array.isArray(v) ? JSON.stringify(v) : String(v)}`);
|
|
22
|
+
}
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
if (key === undefined || !VALID_KEYS.includes(key)) {
|
|
26
|
+
console.error(`neurastream config ${verb ?? ""}: unknown key ${key ?? "(none)"}; valid keys: ${VALID_KEYS.join(", ")}`);
|
|
27
|
+
return 1;
|
|
28
|
+
}
|
|
29
|
+
if (verb === "get") {
|
|
30
|
+
const v = valueAt(loadConfig({ home }), key);
|
|
31
|
+
console.log(Array.isArray(v) ? JSON.stringify(v) : String(v));
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
if (verb !== "set") {
|
|
35
|
+
console.error("usage: neurastream config set|get|list [key] [value]");
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
const parsed: unknown = raw === undefined ? null : Bun.YAML.parse(raw);
|
|
39
|
+
if (parsed === null || parsed === undefined || (typeof parsed === "object" && !Array.isArray(parsed))) {
|
|
40
|
+
console.error(`neurastream config set ${key}: give a scalar or a list (e.g. 7788, true, '["a", "b"]')`);
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
// Lists in flow style on one line ([a, b]); scalars as JSON, which is valid YAML.
|
|
44
|
+
const text = yamlValue(parsed);
|
|
45
|
+
const file = configFile(home);
|
|
46
|
+
const original = existsSync(file) ? readFileSync(file, "utf8") : null;
|
|
47
|
+
const edited = setConfigKeys(original, { [key]: text });
|
|
48
|
+
if (!checkEdited(edited, home, (line) => console.error(line))) return 1;
|
|
49
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
50
|
+
writeFileSync(file, edited);
|
|
51
|
+
return 0;
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
54
|
+
return 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/cli/dispatch.ts
CHANGED
|
@@ -15,11 +15,13 @@ import { applyQueueEnv } from "../core/queue/env.ts";
|
|
|
15
15
|
import { getAdapter } from "../adapters/index.ts";
|
|
16
16
|
import { GUARD_USAGE, runGuard } from "./guard.ts";
|
|
17
17
|
import { runInit } from "./init.ts";
|
|
18
|
+
import { realSetupDeps, runSetup } from "./setup.ts";
|
|
18
19
|
import { PORTED, runMain, runPorted } from "./commands.ts";
|
|
19
20
|
import { runMetrics } from "./metrics.ts";
|
|
20
21
|
import { runIncident } from "./incident.ts";
|
|
21
22
|
import { runTraces } from "./traces.ts";
|
|
22
23
|
import { runUi } from "./ui.ts";
|
|
24
|
+
import { runConfigKeys } from "./config-set.ts";
|
|
23
25
|
import { runDiagram } from "./diagram.ts";
|
|
24
26
|
import { runNotes } from "./notes.ts";
|
|
25
27
|
import { runPages } from "./pages.ts";
|
|
@@ -63,6 +65,9 @@ export function helpText(): string {
|
|
|
63
65
|
" create config, index, notes, blockers, queue template, state dirs",
|
|
64
66
|
" (--agent-dir: keep an existing index/notes/blockers dir)",
|
|
65
67
|
" (--allow a,b: subagents allowed to spawn; on a terminal init asks instead)",
|
|
68
|
+
" neurastream setup [--yes]",
|
|
69
|
+
" guided first run: detect this machine, ask in groups, install, doctor",
|
|
70
|
+
" (--yes: take the detected settings without asking)",
|
|
66
71
|
" neurastream adapter install omp",
|
|
67
72
|
" link the omp extension under this HOME",
|
|
68
73
|
" neurastream uninstall [--purge] [--yes]",
|
|
@@ -84,6 +89,7 @@ export function helpText(): string {
|
|
|
84
89
|
" move a listed plan to rank N; the index file itself is 0444,",
|
|
85
90
|
" these two commands are the only agent writes to it",
|
|
86
91
|
" neurastream config [--json] print the merged config (defaults + file + env)",
|
|
92
|
+
" neurastream config set|get|list <key> [value] read or write one key (ui.port, paths.work_roots, ...)",
|
|
87
93
|
" neurastream guard <event> --json",
|
|
88
94
|
` (${GUARD_USAGE.slice(GUARD_USAGE.indexOf("(") + 1, -1)})`,
|
|
89
95
|
" neurastream main [args] run omp under the launcher (automatic session rollover)",
|
|
@@ -137,6 +143,8 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
137
143
|
if (cmd === "notes") return runNotes(args);
|
|
138
144
|
if (cmd === "diagram") return runDiagram(args);
|
|
139
145
|
if (cmd === "adapter") return getAdapter().install(args);
|
|
146
|
+
if (cmd === "setup") return runSetup(args, realSetupDeps(async () => runNotify(["test"])));
|
|
147
|
+
if (cmd === "config" && ["set", "get", "list"].includes(args[0] ?? "")) return runConfigKeys(args);
|
|
140
148
|
if (cmd === "config") {
|
|
141
149
|
try {
|
|
142
150
|
const cfg = loadConfig();
|
package/src/cli/init.ts
CHANGED
|
@@ -71,9 +71,11 @@ function configText(home: string, invoked: string, agentDir?: string): string {
|
|
|
71
71
|
text = text.replace(AGENT_DIR_KEYS, (_m, key: string) => `${key}${yamlQuote(agent).slice(0, -1)}`);
|
|
72
72
|
}
|
|
73
73
|
const protectedPaths = [...installedBins(home, invoked), `${agent}/notes/`, `${agent}/blockers/`, `${agent}/queue/`];
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
const items = protectedPaths.map(p => ` - ${yamlQuote(p)}`);
|
|
75
|
+
const placeholder = /^ protected: \[\].*$/m;
|
|
76
|
+
if (!placeholder.test(text)) throw new Error("templates/config.yml has no protected placeholder");
|
|
77
|
+
// The header stays a bare ` protected:`: install, uninstall and the guards read it in that form.
|
|
78
|
+
return text.replace(placeholder, [" protected:", ...items].join("\n"));
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
function putFile(dest: string, body: string, mode: number, created: string[], unchanged: string[]): void {
|
|
@@ -162,7 +164,7 @@ export function printInit(result: InitResult): void {
|
|
|
162
164
|
}
|
|
163
165
|
|
|
164
166
|
/** One line from stdin, read synchronously; a terminal hands over a line at a time. */
|
|
165
|
-
function askLine(prompt: string): string {
|
|
167
|
+
export function askLine(prompt: string): string {
|
|
166
168
|
process.stdout.write(prompt);
|
|
167
169
|
const buf = Buffer.alloc(4096);
|
|
168
170
|
const n = readSync(0, buf, 0, buf.length, null);
|
package/src/cli/setup.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// `neurastream setup [--yes]` (plan 20261028_2356 P4): the guided first run. It shows what this
|
|
2
|
+
// machine has (src/core/setup-detect.ts), lays out the home (initHome), asks in groups, writes each
|
|
3
|
+
// group's answers through the one config editor (src/core/config-edit.ts), then runs the adapter
|
|
4
|
+
// install and doctor. Every answer is proven to load before it is kept; --yes takes the proposals
|
|
5
|
+
// without asking. SetupDeps carries the terminal, the seams, and the install/doctor runs, so tests
|
|
6
|
+
// drive the whole flow without a real terminal, systemctl or Tailscale.
|
|
7
|
+
|
|
8
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { getAdapter, harnessBins } from "../adapters/index.ts";
|
|
11
|
+
import { TEMPLATE_PATH, configFile } from "../core/config.ts";
|
|
12
|
+
import { checkEdited, leafKeys, setConfigKeys, valueAt, yamlValue } from "../core/config-edit.ts";
|
|
13
|
+
import { pagesDoctor } from "../core/pages-doctor.ts";
|
|
14
|
+
import { detect, realSeams, type SetupFacts, type SetupSeams } from "../core/setup-detect.ts";
|
|
15
|
+
import { invalidSlugs } from "../core/subagents.ts";
|
|
16
|
+
import { TAILSCALE_MISSING, TAILSCALE_STOPPED } from "../core/ui-setup.ts";
|
|
17
|
+
import { askLine, initHome } from "./init.ts";
|
|
18
|
+
|
|
19
|
+
export interface SetupDeps {
|
|
20
|
+
home: string;
|
|
21
|
+
env: NodeJS.ProcessEnv;
|
|
22
|
+
invoked: string;
|
|
23
|
+
terminal: boolean;
|
|
24
|
+
ask: (question: string) => string;
|
|
25
|
+
seams: SetupSeams;
|
|
26
|
+
install: () => number;
|
|
27
|
+
doctor: () => number;
|
|
28
|
+
say: (line: string) => void;
|
|
29
|
+
// `neurastream notify test`, injected by dispatch (setup.ts does not import dispatch.ts).
|
|
30
|
+
notifyTest: () => Promise<number>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The production deps: the real home, terminal, PATH, Tailscale, adapter install and doctor. */
|
|
34
|
+
export function realSetupDeps(notifyTest: () => Promise<number>): SetupDeps {
|
|
35
|
+
const home = homedir();
|
|
36
|
+
return {
|
|
37
|
+
home,
|
|
38
|
+
env: process.env,
|
|
39
|
+
invoked: process.argv[1] ?? "",
|
|
40
|
+
terminal: process.stdin.isTTY === true,
|
|
41
|
+
ask: askLine,
|
|
42
|
+
seams: realSeams(harnessBins(), home, process.env),
|
|
43
|
+
install: () => getAdapter().install(["install", getAdapter().name]),
|
|
44
|
+
doctor: () => {
|
|
45
|
+
for (const warning of pagesDoctor(home)) console.warn(`warning: ${warning}`);
|
|
46
|
+
return getAdapter().doctor();
|
|
47
|
+
},
|
|
48
|
+
say: (line) => console.log(line),
|
|
49
|
+
notifyTest,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Trailing `# comment` of each leaf key, by dotted path. The indentation stack gives a key's path
|
|
54
|
+
// from its parents' keys, so a nested key never takes a neighbour's comment.
|
|
55
|
+
export function templateComments(text: string): Record<string, string> {
|
|
56
|
+
const out: Record<string, string> = {};
|
|
57
|
+
const stack: { indent: number; key: string }[] = [];
|
|
58
|
+
for (const line of text.split("\n")) {
|
|
59
|
+
const m = /^( *)([A-Za-z_][\w-]*):(.*)$/.exec(line);
|
|
60
|
+
if (!m) continue;
|
|
61
|
+
const indent = m[1]!.length;
|
|
62
|
+
while (stack.length > 0 && stack[stack.length - 1]!.indent >= indent) stack.pop();
|
|
63
|
+
stack.push({ indent, key: m[2]! });
|
|
64
|
+
const comment = /#\s*(.*)$/.exec(m[3]!)?.[1]?.trim() ?? "";
|
|
65
|
+
if (comment !== "") out[stack.map((s) => s.key).join(".")] = comment;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const TEMPLATE_TEXT = readFileSync(TEMPLATE_PATH, "utf8");
|
|
71
|
+
const TEMPLATE = Bun.YAML.parse(TEMPLATE_TEXT);
|
|
72
|
+
const COMMENTS = templateComments(TEMPLATE_TEXT);
|
|
73
|
+
const VALID_KEYS = leafKeys(TEMPLATE);
|
|
74
|
+
const TIERS = ["strict", "opinionated", "full"];
|
|
75
|
+
|
|
76
|
+
// The asked groups, in order. Keys outside them form the advanced walk.
|
|
77
|
+
const GROUPS: { name: string; keys: string[] }[] = [
|
|
78
|
+
{ name: "Paths", keys: ["paths.work_roots"] },
|
|
79
|
+
{ name: "Harness", keys: ["harness"] },
|
|
80
|
+
{ name: "Autonomy", keys: ["autonomy.tier", "autonomy.strict_edit_limit", "autonomy.go_minutes", "autonomy.go_phrase"] },
|
|
81
|
+
{ name: "Phone page", keys: ["ui.enabled", "ui.owner_login"] },
|
|
82
|
+
{ name: "Static pages", keys: ["pages.enabled", "pages.port", "pages.dir"] },
|
|
83
|
+
{ name: "Notify", keys: ["notify.discord_webhook_file", "notify.slack_webhook_file"] },
|
|
84
|
+
{ name: "Secrets", keys: ["secrets.redact", "secrets.known_value_files", "secrets.proxy"] },
|
|
85
|
+
{ name: "Machine", keys: ["machine.load_over_cores", "machine.avail_min_mb", "machine.disk_min_gb"] },
|
|
86
|
+
{ name: "Session rollover", keys: ["rollover.enabled", "rollover.max_session_mb", "rollover.exit_grace_s"] },
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
type Kind = "bool" | "num" | "list" | "str";
|
|
90
|
+
interface Change {
|
|
91
|
+
key: string;
|
|
92
|
+
old: unknown;
|
|
93
|
+
next: unknown;
|
|
94
|
+
}
|
|
95
|
+
interface Ctx {
|
|
96
|
+
deps: SetupDeps;
|
|
97
|
+
facts: SetupFacts;
|
|
98
|
+
fresh: boolean;
|
|
99
|
+
file: string;
|
|
100
|
+
text: string;
|
|
101
|
+
changed: Change[];
|
|
102
|
+
// none: ask every key. group: accept proposals until this group ends (s). all: accept every proposal (q, --yes).
|
|
103
|
+
auto: "none" | "group" | "all";
|
|
104
|
+
// set when the Notify group was asked and a webhook file is set; runSetup then offers the test.
|
|
105
|
+
testOffer: boolean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function runSetup(args: string[], deps: SetupDeps): Promise<number> {
|
|
109
|
+
const yes = args.includes("--yes");
|
|
110
|
+
if (!yes && !deps.terminal) {
|
|
111
|
+
deps.say("neurastream setup needs a terminal or --yes");
|
|
112
|
+
return 2;
|
|
113
|
+
}
|
|
114
|
+
const facts = detect(deps.seams);
|
|
115
|
+
printFacts(deps, facts);
|
|
116
|
+
const ctx: Ctx = {
|
|
117
|
+
deps,
|
|
118
|
+
facts,
|
|
119
|
+
fresh: facts.current === null,
|
|
120
|
+
file: configFile(deps.home),
|
|
121
|
+
text: "",
|
|
122
|
+
changed: [],
|
|
123
|
+
auto: yes ? "all" : "none",
|
|
124
|
+
testOffer: false,
|
|
125
|
+
};
|
|
126
|
+
const allow = yes ? [] : askSubagents(deps);
|
|
127
|
+
const made = initHome(deps.home, { invoked: deps.invoked, env: deps.env, allow: allow.length > 0 ? allow : undefined });
|
|
128
|
+
deps.say(`layout: created ${made.created.length}, kept ${made.unchanged.length}`);
|
|
129
|
+
ctx.text = readFileSync(ctx.file, "utf8");
|
|
130
|
+
for (const group of GROUPS) {
|
|
131
|
+
if (runGroup(ctx, group.name, group.keys) !== 0) return 1;
|
|
132
|
+
}
|
|
133
|
+
if (ctx.auto === "none") {
|
|
134
|
+
const advanced = VALID_KEYS.filter((k) => !GROUPS.some((g) => g.keys.includes(k)));
|
|
135
|
+
const open = deps.ask(`Show the ${advanced.length} advanced settings? [y/N]: `).trim();
|
|
136
|
+
if (/^y(es)?$/i.test(open) && runGroup(ctx, "Advanced", advanced) !== 0) return 1;
|
|
137
|
+
}
|
|
138
|
+
if (ctx.testOffer) await offerTest(ctx);
|
|
139
|
+
const installed = deps.install();
|
|
140
|
+
if (installed !== 0) {
|
|
141
|
+
deps.say("adapter install failed");
|
|
142
|
+
return installed;
|
|
143
|
+
}
|
|
144
|
+
const doctor = deps.doctor();
|
|
145
|
+
printChanges(deps, ctx.changed);
|
|
146
|
+
if (doctor === 0) deps.say("Next: neurastream main");
|
|
147
|
+
return doctor;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function printFacts(deps: SetupDeps, facts: SetupFacts): void {
|
|
151
|
+
deps.say("Detected on this machine:");
|
|
152
|
+
deps.say(` work roots: ${facts.workRoots.join(", ") || "none"}`);
|
|
153
|
+
deps.say(` harnesses on PATH: ${facts.harnesses.join(", ") || "none"}`);
|
|
154
|
+
deps.say(` tailscale: ${facts.tailscale.state}`);
|
|
155
|
+
deps.say(` secretgate: ${facts.secretgate ? "on PATH" : "not on PATH"}`);
|
|
156
|
+
deps.say(` machine: ${facts.machine.cores} cores, ${facts.machine.ramMb} MB RAM, ${facts.machine.diskFreeGb} GB free disk`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function askSubagents(deps: SetupDeps): string[] {
|
|
160
|
+
for (;;) {
|
|
161
|
+
const names = deps.ask("Subagents allowed to spawn (comma-separated names, empty = none): ")
|
|
162
|
+
.split(",")
|
|
163
|
+
.map((s) => s.trim())
|
|
164
|
+
.filter((s) => s !== "");
|
|
165
|
+
const bad = invalidSlugs(names);
|
|
166
|
+
if (bad.length === 0) return names;
|
|
167
|
+
deps.say(` not a valid name: ${bad.join(", ")}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function printChanges(deps: SetupDeps, changed: Change[]): void {
|
|
172
|
+
if (changed.length === 0) {
|
|
173
|
+
deps.say("No settings changed.");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
deps.say("Changed:");
|
|
177
|
+
for (const c of changed) deps.say(` ${c.key}: ${JSON.stringify(c.old)} → ${JSON.stringify(c.next)}`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Asks one group, then writes the changed keys once through the editor. Returns non-zero only
|
|
181
|
+
// when the edited file would not load (nothing is written then). A Notify group the owner was
|
|
182
|
+
// asked, with a webhook file set, sets ctx.testOffer so runSetup offers the test.
|
|
183
|
+
function runGroup(ctx: Ctx, name: string, keys: string[]): number {
|
|
184
|
+
ctx.deps.say(`\n${name}`);
|
|
185
|
+
const now = Bun.YAML.parse(ctx.text);
|
|
186
|
+
const picks: Record<string, unknown> = {};
|
|
187
|
+
const asking = ctx.auto === "none";
|
|
188
|
+
for (const key of keys) {
|
|
189
|
+
if (key === "ui.owner_login" && picks["ui.enabled"] !== true) continue;
|
|
190
|
+
const proposed = proposal(ctx, key, valueAt(now, key));
|
|
191
|
+
let value = askKey(ctx, key, kindOf(key), proposed);
|
|
192
|
+
if (key === "ui.enabled" && value === true && !phoneReady(ctx)) value = false;
|
|
193
|
+
picks[key] = value;
|
|
194
|
+
}
|
|
195
|
+
if (asking && Object.keys(picks).some((key) => key.startsWith("notify.") && picks[key] !== "")) {
|
|
196
|
+
ctx.testOffer = true;
|
|
197
|
+
}
|
|
198
|
+
if (ctx.auto === "group") ctx.auto = "none";
|
|
199
|
+
const changes: Record<string, string> = {};
|
|
200
|
+
for (const key of Object.keys(picks)) {
|
|
201
|
+
const old = valueAt(now, key);
|
|
202
|
+
if (JSON.stringify(old) === JSON.stringify(picks[key])) continue;
|
|
203
|
+
changes[key] = yamlValue(picks[key]);
|
|
204
|
+
ctx.changed.push({ key, old, next: picks[key] });
|
|
205
|
+
}
|
|
206
|
+
if (Object.keys(changes).length === 0) return 0;
|
|
207
|
+
const edited = setConfigKeys(ctx.text, changes);
|
|
208
|
+
if (!checkEdited(edited, ctx.deps.home, (line) => ctx.deps.say(line))) return 1;
|
|
209
|
+
writeFileSync(ctx.file, edited);
|
|
210
|
+
ctx.text = edited;
|
|
211
|
+
return 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// The notify test offer. A yes runs `neurastream notify test` through the injected seam. A failed
|
|
215
|
+
// test is printed by that command and setup goes on: the webhook is the owner's to fix.
|
|
216
|
+
async function offerTest(ctx: Ctx): Promise<void> {
|
|
217
|
+
const answer = ctx.deps.ask("send a test message now? [y/N]: ").trim();
|
|
218
|
+
if (/^y(es)?$/i.test(answer)) await ctx.deps.notifyTest();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// The value offered for Enter. A fresh home offers what this machine has; a re-run offers the
|
|
222
|
+
// current config, so a setting the owner already chose never moves on its own.
|
|
223
|
+
function proposal(ctx: Ctx, key: string, current: unknown): unknown {
|
|
224
|
+
if (!ctx.fresh) return current;
|
|
225
|
+
const fromFacts: Record<string, unknown> = {
|
|
226
|
+
"paths.work_roots": ctx.facts.workRoots,
|
|
227
|
+
harness: ctx.facts.harnesses[0],
|
|
228
|
+
"machine.load_over_cores": ctx.facts.proposed.load_over_cores,
|
|
229
|
+
"machine.avail_min_mb": ctx.facts.proposed.avail_min_mb,
|
|
230
|
+
"machine.disk_min_gb": ctx.facts.proposed.disk_min_gb,
|
|
231
|
+
};
|
|
232
|
+
return fromFacts[key] ?? current;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function kindOf(key: string): Kind {
|
|
236
|
+
const t = valueAt(TEMPLATE, key);
|
|
237
|
+
if (typeof t === "boolean") return "bool";
|
|
238
|
+
if (typeof t === "number") return "num";
|
|
239
|
+
if (Array.isArray(t)) return "list";
|
|
240
|
+
return "str";
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function askKey(ctx: Ctx, key: string, kind: Kind, proposed: unknown): unknown {
|
|
244
|
+
if (ctx.auto !== "none") return proposed;
|
|
245
|
+
const comment = COMMENTS[key] ?? "";
|
|
246
|
+
for (;;) {
|
|
247
|
+
const raw = ctx.deps
|
|
248
|
+
.ask(`${key}: ${comment} [${yamlValue(proposed)}] (Enter keeps; s keeps the rest of this group; q keeps the rest): `)
|
|
249
|
+
.trim();
|
|
250
|
+
if (raw === "") return proposed;
|
|
251
|
+
if (raw === "s" || raw === "q") {
|
|
252
|
+
ctx.auto = raw === "s" ? "group" : "all";
|
|
253
|
+
return proposed;
|
|
254
|
+
}
|
|
255
|
+
const parsed = parseAnswer(kind, raw);
|
|
256
|
+
if ("error" in parsed) {
|
|
257
|
+
ctx.deps.say(` ${parsed.error}`);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const problem = validate(ctx, key, parsed.value);
|
|
261
|
+
if (problem !== null) {
|
|
262
|
+
ctx.deps.say(` ${problem}`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
return parsed.value;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseAnswer(kind: Kind, raw: string): { value: unknown } | { error: string } {
|
|
270
|
+
if (kind === "bool") {
|
|
271
|
+
if (/^(true|yes|y|on)$/i.test(raw)) return { value: true };
|
|
272
|
+
if (/^(false|no|n|off)$/i.test(raw)) return { value: false };
|
|
273
|
+
return { error: "answer yes or no" };
|
|
274
|
+
}
|
|
275
|
+
if (kind === "num") {
|
|
276
|
+
const n = Number(raw);
|
|
277
|
+
return Number.isFinite(n) ? { value: n } : { error: "answer a number" };
|
|
278
|
+
}
|
|
279
|
+
if (kind === "list") {
|
|
280
|
+
if (!raw.startsWith("[")) return { value: raw.split(",").map((s) => s.trim()).filter((s) => s !== "") };
|
|
281
|
+
try {
|
|
282
|
+
const list: unknown = Bun.YAML.parse(raw);
|
|
283
|
+
if (Array.isArray(list) && list.every((x) => typeof x === "string")) return { value: list };
|
|
284
|
+
} catch {
|
|
285
|
+
// not YAML: reported below
|
|
286
|
+
}
|
|
287
|
+
return { error: "answer a list of strings, e.g. [\"a\", \"b\"] or a, b" };
|
|
288
|
+
}
|
|
289
|
+
return { value: raw };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Why an answer is refused, or null. The file must load with the answer in place (checkEdited),
|
|
293
|
+
// and the named keys must hold values the setup understands.
|
|
294
|
+
function validate(ctx: Ctx, key: string, value: unknown): string | null {
|
|
295
|
+
const problem = rule(ctx, key, value);
|
|
296
|
+
if (problem !== null) return problem;
|
|
297
|
+
const messages: string[] = [];
|
|
298
|
+
const candidate = setConfigKeys(ctx.text, { [key]: yamlValue(value) });
|
|
299
|
+
return checkEdited(candidate, ctx.deps.home, (line) => messages.push(line)) ? null : messages.join("\n");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function rule(ctx: Ctx, key: string, value: unknown): string | null {
|
|
303
|
+
if (key === "autonomy.tier" && !TIERS.includes(String(value))) return `autonomy.tier must be ${TIERS.join(", ")}`;
|
|
304
|
+
if (key === "harness" && !ctx.deps.seams.harnesses.some((h) => h.name === value)) {
|
|
305
|
+
return `harness must be one of: ${ctx.deps.seams.harnesses.map((h) => h.name).join(", ")}`;
|
|
306
|
+
}
|
|
307
|
+
if (key === "secrets.proxy" && value === "secretgate" && !ctx.facts.secretgate) return "secretgate is not on PATH";
|
|
308
|
+
if (key === "secrets.proxy" && value !== "none" && value !== "secretgate") return "secrets.proxy must be none or secretgate";
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// The phone page needs a running tailnet. Without one the page stays off and the setup says why.
|
|
313
|
+
function phoneReady(ctx: Ctx): boolean {
|
|
314
|
+
const tailscale = ctx.deps.seams.tailscale;
|
|
315
|
+
const state = tailscale.state();
|
|
316
|
+
if (state === "missing") {
|
|
317
|
+
ctx.deps.say(TAILSCALE_MISSING);
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
if (state === "stopped") {
|
|
321
|
+
ctx.deps.say(TAILSCALE_STOPPED);
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
const logins = tailscale.logins() ?? [];
|
|
325
|
+
if (logins.length > 0) ctx.deps.say(` logins on this tailnet: ${logins.join(", ")}`);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
package/src/cli/ui.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { beat, errorLine } from "../core/heartbeat.ts";
|
|
|
10
10
|
import { startOwnerUi, type OwnerUiOptions, type OwnerUiServer } from "../core/owner-ui.ts";
|
|
11
11
|
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
12
12
|
import { realTailscale, type Tailscale } from "../core/tailscale.ts";
|
|
13
|
-
import { uiOff, uiOn, type Runner } from "../core/ui-setup.ts";
|
|
13
|
+
import { systemTerminal, uiOff, uiOn, type Runner } from "../core/ui-setup.ts";
|
|
14
14
|
|
|
15
15
|
export interface UiDeps {
|
|
16
16
|
/** The merged config. Default: loadConfig(). */
|
|
@@ -56,7 +56,9 @@ function runUiSetup(args: string[], deps: UiDeps): number {
|
|
|
56
56
|
console.error("neurastream ui on: --login <tailscale login> is required — e.g. neurastream ui on --login me@example.com");
|
|
57
57
|
return 1;
|
|
58
58
|
}
|
|
59
|
-
|
|
59
|
+
// A question only on a terminal: scripted runs get the message and exit 1, no prompt.
|
|
60
|
+
const terminal = process.stdin.isTTY && process.stdout.isTTY ? systemTerminal : undefined;
|
|
61
|
+
return uiOn({ login, port, terminal, ...setup });
|
|
60
62
|
}
|
|
61
63
|
return uiOff(setup);
|
|
62
64
|
}
|