anon-pi 0.4.0 → 0.6.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.
package/dist/anon-pi.js CHANGED
@@ -1,34 +1,71 @@
1
1
  // anon-pi: the PURE logic (no process spawning, no interactive I/O) so every
2
2
  // decision is unit-testable. cli.ts wires this to the real filesystem + spawn.
3
3
  //
4
- // What anon-pi does (settled design):
5
- // - ALWAYS seed a per-workdir writable copy of the canonical anon-pi config
6
- // (~/.config/anon-pi/agent) into a per-session dir keyed by the workdir, and
7
- // mount THAT as the container's pi global (PI_CODING_AGENT_DIR). The
8
- // canonical config is only ever READ (at seed time), never mounted, so the
9
- // container cannot mutate it.
10
- // - Mount the workdir separately at /work (pi's cwd; the user's files land on
11
- // the host). A user-supplied /work/.pi/ override is just pi's own
12
- // project-over-global layering; anon-pi neither creates nor requires it.
13
- // - Open exactly ONE direct hole (--allow-direct <ANON_PI_LLM>) so pi can reach
14
- // a local model while all other egress stays forced through the proxy.
15
- // - NEVER auto-populate the canonical seed: if it is absent, error and tell the
16
- // user to populate it (their anon accounts / chosen skills / a valid
17
- // trust.json that trusts /work). anon-pi does not synthesize pi's trust.json.
18
- // - Session identity = the ABSOLUTE workdir path (hashed). Same folder resumes
19
- // the same session config+state; reseed is manual (delete the session dir).
4
+ // The model (machines + projects; see CONTEXT.md + docs/adr/0001):
5
+ // - A MACHINE is an image + a persistent HOST home (`machines/<M>/home`),
6
+ // bind-mounted into the jail at /root. It holds shell config, pi config +
7
+ // extensions, and pi conversations (`~/.pi/agent/sessions/`). The container
8
+ // is disposable; ALL valuable state is in this host home.
9
+ // - A PROJECT is a folder under the PROJECTS ROOT, bind-mounted at /projects,
10
+ // so a project's cwd is /projects/<name>. pi keys a conversation by its
11
+ // launch cwd, so /projects/<name> is the conversation key (per-machine,
12
+ // since it lives in that machine's home).
13
+ // - TWO invariant container mounts, always: /root (the machine home) and
14
+ // /projects (the projects root). `--mount <parent>` adds EXACTLY one more
15
+ // mount at the DISTINCT /work and re-roots cwd there; nothing else changes,
16
+ // so we never remount a running container.
17
+ // - Throwaway (`--rm`) is the DEFAULT; `--keep` leaves the container kept so
18
+ // its filesystem survives (found + resumed by netcage's `netcage.managed`
19
+ // label via `netcage start`). The machine home persists either way.
20
+ // - Open exactly ONE direct hole (--allow-direct <llm>) so pi can reach a
21
+ // local model while ALL other egress stays forced through the socks5h proxy
22
+ // (fail-closed; the proxy is REQUIRED and never guessed).
23
+ // - Seed-if-fresh (marker-guarded, per MACHINE home): on a fresh home, promote
24
+ // the image's /root defaults + pi staging + the generated models.json into
25
+ // the home once, then stamp the marker and never clobber it again.
26
+ //
27
+ // This module holds every DECISION as a pure function (config load + precedence,
28
+ // machine/project resolvers, name validation, the RunPlan argv, the menu
29
+ // choice-list, project usage, the run-vs-start rule, models.json generation,
30
+ // init's proxy detect/verify decisions). cli.ts owns only the impure edges (fs,
31
+ // the interactive TUI, the netcage query, the spawn).
20
32
  import { existsSync } from 'node:fs';
21
33
  import { homedir } from 'node:os';
22
- import { dirname, isAbsolute, join, resolve } from 'node:path';
34
+ import { dirname, join, resolve } from 'node:path';
23
35
  import { fileURLToPath } from 'node:url';
24
- /** The container path the workdir is mounted at (pi's cwd). */
25
- export const CONTAINER_WORKDIR = '/work';
36
+ /**
37
+ * The jail cwd root for the projects-root launch: the projects root is mounted
38
+ * here and a project `<name>` is `/projects/<name>` (pi keys a conversation by
39
+ * its launch cwd, so `/projects/<name>` is the conversation key). This is the
40
+ * machines + projects mount (distinct from `--mount`'s /work).
41
+ */
42
+ export const CONTAINER_PROJECTS_ROOT = '/projects';
43
+ /**
44
+ * The jail cwd root for a `--mount <parent>` launch: the HOST parent is mounted
45
+ * here (kept DISTINCT from /projects so the two roots never collide), and a
46
+ * project `<name>` is `/work/<name>`. See ADR-0001 (`--mount` keeps `/work`).
47
+ */
48
+ export const CONTAINER_MOUNT_ROOT = '/work';
49
+ /**
50
+ * The jail cwd root for a machine (its persistent home, bind-mounted at /root).
51
+ * A machine root has no named subfolders: only the root token `.` (a scratch pi
52
+ * / shell at `~`) is valid. Written as `~` so it reads as "the machine home".
53
+ */
54
+ export const CONTAINER_MACHINE_HOME = '~';
55
+ /**
56
+ * The REAL container path the machine home is bind-mounted at (the source is
57
+ * the host `machineHomeDir`). This is what a shell-at-`~` launch actually cwds
58
+ * into (`-w /root`), distinct from CONTAINER_MACHINE_HOME (`~`), which is the
59
+ * human-readable menu token. It is the parent of CONTAINER_AGENT_DIR
60
+ * (`/root/.pi/agent`); the seed-if-fresh promotes the image's `/root` defaults +
61
+ * pi staging into the mounted home here.
62
+ */
63
+ export const CONTAINER_HOME_ROOT = '/root';
26
64
  /**
27
65
  * The container path pi uses as its config+state home. anon-pi mounts a
28
66
  * PERSISTENT host dir here (Model B), so everything pi writes, sessions,
29
67
  * history, settings (your model choice), `pi install`ed extensions, downloaded
30
- * bin/fd, survives across launches. Statefulness is the default; --ephemeral
31
- * mounts a throwaway dir here instead.
68
+ * bin/fd, survives across launches. Statefulness is the default.
32
69
  */
33
70
  export const CONTAINER_AGENT_DIR = '/root/.pi/agent';
34
71
  /**
@@ -44,10 +81,25 @@ export const CONTAINER_STAGE_DIR = '/opt/anon-pi-seed/agent';
44
81
  * staged defaults. Read-only: the container never writes back to the host seed.
45
82
  */
46
83
  export const CONTAINER_MODELS_SEED = '/anon-pi-seed/models.json';
84
+ /**
85
+ * Where anon-pi mounts the generated settings SEED (the local-model default
86
+ * selection: defaultProvider/defaultModel/enabledModels) read-only, so the
87
+ * first-launch seed can MERGE it into the fresh home's settings.json (never
88
+ * clobbering image-staged packages/extensions).
89
+ */
90
+ export const CONTAINER_SETTINGS_SEED = '/anon-pi-seed/settings.json';
47
91
  /** Marker file written into the agent dir after seeding; holds the seed version. */
48
92
  export const SEED_MARKER = '.anon-pi-seed';
49
- /** The single file the host-side seed carries: pi's model/provider registry. */
93
+ /** The file the host-side seed carries: pi's model/provider registry. */
50
94
  export const MODELS_FILE = 'models.json';
95
+ /** pi's settings file (holds defaultModel/defaultProvider/enabledModels + more). */
96
+ export const SETTINGS_FILE = 'settings.json';
97
+ /**
98
+ * The settings SEED file anon-pi writes next to a machine (the local-model
99
+ * selection fragment). Distinct name so it never collides with a real
100
+ * settings.json; the seed MERGES it into the home's settings on first launch.
101
+ */
102
+ export const SETTINGS_SEED_FILE = 'settings-seed.json';
51
103
  /**
52
104
  * containerRunCmd builds the container command: on a FRESH home (no seed
53
105
  * marker), promote the image's staged defaults + the mounted models.json into
@@ -76,24 +128,760 @@ export const SEED_VERSION = '1';
76
128
  /** A user-facing error whose message is meant to be printed verbatim (no stack). */
77
129
  export class AnonPiError extends Error {
78
130
  }
79
- /** Resolve the anon-pi home dir (holds the seed). */
131
+ /**
132
+ * The verbatim guidance printed when no proxy is supplied. Kept as a single
133
+ * source so the fail-closed path (resolveProxy) emits byte-identical
134
+ * copy-pasteable guidance. The proxy is REQUIRED and never guessed: it is what
135
+ * anonymizes egress (fail-closed is the anonymity invariant).
136
+ */
137
+ export const PROXY_REQUIRED_MESSAGE = 'anon-pi: set ANON_PI_PROXY to your socks5h proxy. anon-pi has no default:\n' +
138
+ 'the proxy is what makes the session anonymous, so it is never guessed.\n' +
139
+ '\n' +
140
+ 'Pick the one you run (copy-paste), then re-run anon-pi:\n' +
141
+ '\n' +
142
+ '# Tor (system tor / Tor Browser bundle default port)\n' +
143
+ 'export ANON_PI_PROXY=socks5h://127.0.0.1:9050\n' +
144
+ '\n' +
145
+ '# wireproxy -> a WireGuard VPN (Mullvad, Proton, ...); use YOUR configured\n' +
146
+ '# [Socks5] BindAddress port (1080 in wireproxy examples):\n' +
147
+ 'export ANON_PI_PROXY=socks5h://127.0.0.1:1080\n' +
148
+ '\n' +
149
+ '# an SSH dynamic-forward (ssh -D 1080 host) or any other socks5h endpoint\n' +
150
+ 'export ANON_PI_PROXY=socks5h://127.0.0.1:1080\n' +
151
+ '\n' +
152
+ 'Only socks5h:// is accepted (plain socks5:// resolves DNS locally and leaks).';
153
+ /**
154
+ * Resolve the anon-pi home dir: the dedicated, browsable workspace folder
155
+ * (`~/.anon-pi/`, NOT under `~/.config`), holding config.json, machines/<M>/,
156
+ * and the default global projects root. Overridable via ANON_PI_HOME.
157
+ */
80
158
  export function resolveAnonPiHome(env) {
81
159
  if (env.anonPiHome)
82
160
  return resolve(env.anonPiHome);
83
- const base = env.xdgConfigHome && env.xdgConfigHome.trim() !== ''
84
- ? env.xdgConfigHome
85
- : join(env.home, '.config');
86
- return join(base, 'anon-pi');
161
+ return join(env.home, '.anon-pi');
162
+ }
163
+ /** A machine's directory: <home>/machines/<name> (holds machine.json + home/). */
164
+ export function machineDir(env, name) {
165
+ return join(resolveAnonPiHome(env), 'machines', name);
166
+ }
167
+ /** A machine's persistent HOST home: <home>/machines/<name>/home (bind-mounted at /root). */
168
+ export function machineHomeDir(env, name) {
169
+ return join(machineDir(env, name), 'home');
170
+ }
171
+ /** A machine's machine.json path: <home>/machines/<name>/machine.json. */
172
+ export function machineJsonPath(env, name) {
173
+ return join(machineDir(env, name), 'machine.json');
174
+ }
175
+ /** The sessions dirname pi keeps its per-cwd conversation dirs under (in the agent dir). */
176
+ export const SESSIONS_DIRNAME = 'sessions';
177
+ /**
178
+ * A machine's HOST pi agent dir: the host side of the container's
179
+ * CONTAINER_AGENT_DIR (`/root/.pi/agent`, since the home is bind-mounted at
180
+ * /root). i.e. <machineHome>/.pi/agent. Where pi's config + sessions live.
181
+ */
182
+ export function machineAgentDir(env, name) {
183
+ return join(machineHomeDir(env, name), '.pi', 'agent');
184
+ }
185
+ /**
186
+ * A machine's HOST pi sessions dir: <machineAgentDir>/sessions. Each per-cwd
187
+ * conversation is a slug-named subdir here (projectSessionSlug for a project).
188
+ */
189
+ export function machineSessionsDir(env, name) {
190
+ return join(machineAgentDir(env, name), SESSIONS_DIRNAME);
191
+ }
192
+ /**
193
+ * The HOST session dir a given project's conversation occupies in a given
194
+ * machine's home: <machineSessionsDir>/<projectSessionSlug>. Because the slug is
195
+ * MACHINE-INVARIANT (pi keys by the `/projects/<name>` cwd, identical on every
196
+ * machine), the SAME shared project has this dir in each machine that used it.
197
+ * Validates the project name (rejecting traversal) via projectSessionSlug.
198
+ */
199
+ export function machineProjectSessionDir(env, machine, project) {
200
+ return join(machineSessionsDir(env, machine), projectSessionSlug(project));
201
+ }
202
+ /** The built-in default global projects root: <home>/projects. */
203
+ export function builtinProjectsRoot(env) {
204
+ return join(resolveAnonPiHome(env), 'projects');
205
+ }
206
+ /**
207
+ * PURE: resolve the affected path for `--delete-home <machine>`: the machine's
208
+ * HOME dir only (config + convos + shell env), NOT the whole machine dir, so the
209
+ * image pin (machine.json) survives a re-seed. Validates the machine name
210
+ * (rejecting traversal) via machineHomeDir's join being under a validated name;
211
+ * we validate explicitly here so the plan itself is a safe single segment.
212
+ */
213
+ export function resolveDeleteHome(env, machine) {
214
+ validateName(machine, 'machine');
215
+ return { machine, home: machineHomeDir(env, machine) };
216
+ }
217
+ /**
218
+ * PURE: resolve the affected paths for `--delete-project <project>`: the
219
+ * project's files under the RESOLVED projects root, plus that project's session
220
+ * dir in each SUPPLIED machine home (the machine-invariant slug). Validates the
221
+ * project name (rejecting traversal) so both the folder join and every session
222
+ * join stay inside their roots. The homes are NOT targeted (only the per-project
223
+ * slug dir inside each), matching the prd behaviour table.
224
+ */
225
+ export function resolveDeleteProject(args) {
226
+ const { env, project, projectsRoot, machines } = args;
227
+ validateName(project, 'project');
228
+ return {
229
+ project,
230
+ folder: projectHostDir(projectsRoot, project),
231
+ sessions: machines.map((m) => machineProjectSessionDir(env, m, project)),
232
+ };
233
+ }
234
+ // --- Name validation + the "." root token ------------------------------------
235
+ /**
236
+ * The project token meaning "the root itself": cwd `/projects` (projects root),
237
+ * `/work` (`--mount`), or `~` (a machine home). It is NOT a valid machine or
238
+ * project name (validateName rejects it) so a folder can never shadow it.
239
+ */
240
+ export const ROOT_TOKEN = '.';
241
+ /**
242
+ * Reserved names that a machine/project may NOT take (case-sensitive). Kept
243
+ * DELIBERATELY minimal: only the two structural path tokens. `.` is the root
244
+ * token (see ROOT_TOKEN); `..` is parent-traversal. Both are also rejected by
245
+ * the leading-dot / `..` structural checks below, but are listed here so the
246
+ * reserved-name concept is explicit and extendable. `--mount`'s `/work` is a
247
+ * CONTAINER path, not a name in this namespace, so it needs no reservation.
248
+ */
249
+ export const RESERVED_NAMES = ['.', '..'];
250
+ /**
251
+ * PURE: validate a machine or project name as a safe single path segment, and
252
+ * return it unchanged on success. Rejects (with AnonPiError):
253
+ * - empty
254
+ * - a path separator `/` or `\`, or a colon `:`
255
+ * - the traversal token `..` (and any leading dot, incl. `.`)
256
+ * - any whitespace
257
+ * - a reserved name (RESERVED_NAMES)
258
+ * A valid name is thus a single folder segment safe to join under the projects
259
+ * root or the machines dir with no traversal or drive/scheme surprises.
260
+ */
261
+ export function validateName(name, kind) {
262
+ const bad = (why) => {
263
+ throw new AnonPiError(`anon-pi: invalid ${kind} name ${JSON.stringify(name)}: ${why}. ` +
264
+ `A ${kind} name must be a single folder segment (no / \\ : whitespace, ` +
265
+ `no leading dot, not "..").`);
266
+ };
267
+ if (name === '')
268
+ return bad('it is empty');
269
+ if (/[/\\:]/.test(name))
270
+ return bad('it contains / \\ or :');
271
+ if (/\s/.test(name))
272
+ return bad('it contains whitespace');
273
+ if (name.startsWith('.'))
274
+ return bad('it starts with a dot');
275
+ if (name === '..')
276
+ return bad('it is the parent-traversal token');
277
+ if (RESERVED_NAMES.includes(name))
278
+ return bad('it is a reserved name');
279
+ return name;
280
+ }
281
+ /**
282
+ * PURE: map a validated project `<name>` to its host folder under the resolved
283
+ * projects root (the parent from resolveProjectsRoot / a `--mount` parent).
284
+ * Validates the name (rejecting traversal) so the join stays inside the root.
285
+ */
286
+ export function projectHostDir(projectsRoot, name) {
287
+ return join(projectsRoot, validateName(name, 'project'));
288
+ }
289
+ /**
290
+ * PURE: the jail cwd for a validated project `<name>`: `/projects/<name>`. This
291
+ * is pi's conversation key (pi keys a session by its launch cwd). Validates the
292
+ * name. For the `--mount` root use resolveCwd('mount', name) (=> /work/<name>).
293
+ */
294
+ export function projectContainerCwd(name) {
295
+ return `${CONTAINER_PROJECTS_ROOT}/${validateName(name, 'project')}`;
296
+ }
297
+ /** True iff `token` is exactly the root token `.` ("the root itself"). */
298
+ export function isRootToken(token) {
299
+ return token === ROOT_TOKEN;
300
+ }
301
+ /** PURE: the jail cwd of a root itself: /projects, /work (mount), or ~ (machine). */
302
+ export function rootCwd(kind) {
303
+ switch (kind) {
304
+ case 'projects':
305
+ return CONTAINER_PROJECTS_ROOT;
306
+ case 'mount':
307
+ return CONTAINER_MOUNT_ROOT;
308
+ case 'machine':
309
+ return CONTAINER_MACHINE_HOME;
310
+ }
311
+ }
312
+ /**
313
+ * PURE: resolve a launch's jail cwd UNIFORMLY from a `token` and its root kind.
314
+ * The root token `.` means "the root itself" (rootCwd) in every context; any
315
+ * other token is a project name resolved to `<root>/<name>` (validated). A
316
+ * machine root has no named subfolders (projects live at /projects or /work,
317
+ * never under the machine home), so a non-`.` token for a machine is rejected.
318
+ * This is the one seam so `anon-pi --mount <p> .` and a menu "here" entry agree.
319
+ */
320
+ export function resolveCwd(kind, token) {
321
+ if (isRootToken(token))
322
+ return rootCwd(kind);
323
+ if (kind === 'machine') {
324
+ throw new AnonPiError(`anon-pi: a machine root takes only "${ROOT_TOKEN}" (the machine home ${CONTAINER_MACHINE_HOME}), ` +
325
+ `not a named project ${JSON.stringify(token)}. Projects live under /projects or /work.`);
326
+ }
327
+ return `${rootCwd(kind)}/${validateName(token, 'project')}`;
328
+ }
329
+ /** Pick a string field from a parsed-JSON object, or undefined if absent/non-string. */
330
+ function strField(o, key) {
331
+ if (!o || typeof o !== 'object')
332
+ return undefined;
333
+ const v = o[key];
334
+ return typeof v === 'string' ? v : undefined;
335
+ }
336
+ /**
337
+ * PURE: parse an already-JSON-decoded config.json value into an AnonPiConfig,
338
+ * keeping only the known string fields (defensive against a hand-edited file).
339
+ * Tolerates undefined/null/partial input (an absent config is `{}`).
340
+ */
341
+ export function parseConfigJson(raw) {
342
+ const out = {};
343
+ const proxy = strField(raw, 'proxy');
344
+ if (proxy !== undefined)
345
+ out.proxy = proxy;
346
+ const llm = strField(raw, 'llm');
347
+ if (llm !== undefined)
348
+ out.llm = llm;
349
+ const defaultMachine = strField(raw, 'defaultMachine');
350
+ if (defaultMachine !== undefined)
351
+ out.defaultMachine = defaultMachine;
352
+ const projects = strField(raw, 'projects');
353
+ if (projects !== undefined)
354
+ out.projects = projects;
355
+ return out;
356
+ }
357
+ /**
358
+ * PURE: parse an already-JSON-decoded machine.json value into a MachineConfig.
359
+ * Tolerates undefined/null/partial input (an absent machine.json is `{}`).
360
+ */
361
+ export function parseMachineJson(raw) {
362
+ const out = {};
363
+ const image = strField(raw, 'image');
364
+ if (image !== undefined)
365
+ out.image = image;
366
+ const projects = strField(raw, 'projects');
367
+ if (projects !== undefined)
368
+ out.projects = projects;
369
+ return out;
370
+ }
371
+ /** A non-empty (after-trim) string, or undefined. */
372
+ function nonEmpty(v) {
373
+ return v && v.trim() !== '' ? v.trim() : undefined;
374
+ }
375
+ /**
376
+ * PURE: resolve the projects root (the host dir mounted at /projects) with the
377
+ * decided precedence, highest first:
378
+ * --mount (CLI) > env ANON_PI_PROJECTS > machine.json.projects >
379
+ * config.json.projects > built-in <home>/projects
380
+ * This task delivers the config/env/machine layers; `mountParent` is the
381
+ * documented top slot the later --mount CLI task threads in (pass the resolved
382
+ * host parent). A relative override is resolved to an absolute path.
383
+ */
384
+ export function resolveProjectsRoot(args) {
385
+ const { env, config, machine, mountParent } = args;
386
+ const pick = nonEmpty(mountParent) ??
387
+ nonEmpty(env.projects) ??
388
+ nonEmpty(machine?.projects) ??
389
+ nonEmpty(config?.projects);
390
+ if (pick !== undefined)
391
+ return resolve(pick);
392
+ return builtinProjectsRoot(env);
393
+ }
394
+ /**
395
+ * PURE: resolve the proxy with env-over-config precedence, REQUIRED /
396
+ * fail-closed. Throws AnonPiError with the verbatim PROXY_REQUIRED_MESSAGE when
397
+ * neither env nor config supplies a non-empty proxy (never a guessed default:
398
+ * fail-closed is the anonymity invariant).
399
+ */
400
+ export function resolveProxy(args) {
401
+ const pick = nonEmpty(args.env.proxy) ?? nonEmpty(args.config?.proxy);
402
+ if (pick === undefined)
403
+ throw new AnonPiError(PROXY_REQUIRED_MESSAGE);
404
+ return pick;
405
+ }
406
+ /**
407
+ * PURE: resolve the local-model direct target with env-over-config precedence.
408
+ * Unlike the proxy this is NOT fail-closed here (a launch with no local model
409
+ * is a later decision); returns undefined when neither supplies one.
410
+ */
411
+ export function resolveLlm(args) {
412
+ return nonEmpty(args.env.llmDirect) ?? nonEmpty(args.config?.llm);
413
+ }
414
+ // --- Grammar A: the pure argv -> ParsedLaunch parser -------------------------
415
+ //
416
+ // A bare positional is a PROJECT; `-m` picks the machine. The CLI (cli.ts)
417
+ // combines the ParsedLaunch with config/machine reads (proxy, llm, image, home,
418
+ // projects root) into a LaunchIntent and runs resolveRunPlan. Kept PURE (argv
419
+ // in -> struct out, or AnonPiError) so parsing + the reserved-name guard are
420
+ // unit-testable; the CLI stays thin I/O.
421
+ /** The machine bare `anon-pi` launches when no `-m` and no config default. */
422
+ export const DEFAULT_MACHINE = 'default';
423
+ /**
424
+ * PURE: parse grammar A into a ParsedLaunch. Consumes the anon-pi flags
425
+ * (`-m <machine>`, `--shell`, `--mount <parent>`, `--keep`/`--rm`) LEFT of the
426
+ * project positional; the FIRST bare positional is the project (`.` allowed as
427
+ * the root token). In pi mode every token AFTER the project is forwarded to pi
428
+ * verbatim (so `anon-pi recon -p '...'` works) — anon-pi flags must come before
429
+ * the project. In shell/menu mode a stray extra positional is an error (bash has
430
+ * no forwarded-args grammar; the menu takes no project).
431
+ *
432
+ * Validates the project name and the `-m` machine name via validateName (the
433
+ * reserved-name guard); `--mount <parent>` is a HOST path in its own namespace,
434
+ * distinct from the project-name namespace (NAME vs `--mount` exclusivity), so
435
+ * it is NOT name-validated here. Throws AnonPiError for an unknown option, a
436
+ * missing `-m`/`--mount` argument, a contradictory `--keep --rm`, or a bad name.
437
+ */
438
+ export function parseLaunchArgs(args) {
439
+ let machine = DEFAULT_MACHINE;
440
+ let machineSet = false;
441
+ let shell = false;
442
+ let mountParent;
443
+ let keepSeen = false;
444
+ let rmSeen = false;
445
+ let project;
446
+ let piArgs;
447
+ const fail = (msg) => {
448
+ throw new AnonPiError(`anon-pi: ${msg}\nRun \`anon-pi --help\`.`);
449
+ };
450
+ let i = 0;
451
+ for (; i < args.length; i++) {
452
+ const a = args[i];
453
+ if (a === '-m' || a === '--machine') {
454
+ const v = args[++i];
455
+ if (v === undefined)
456
+ fail(`${a} needs a machine name`);
457
+ machine = validateName(v, 'machine');
458
+ machineSet = true;
459
+ continue;
460
+ }
461
+ if (a === '--shell') {
462
+ shell = true;
463
+ continue;
464
+ }
465
+ if (a === '--mount') {
466
+ const v = args[++i];
467
+ if (v === undefined)
468
+ fail('--mount needs a HOST parent path');
469
+ mountParent = v;
470
+ continue;
471
+ }
472
+ if (a === '--keep') {
473
+ keepSeen = true;
474
+ continue;
475
+ }
476
+ if (a === '--rm') {
477
+ rmSeen = true;
478
+ continue;
479
+ }
480
+ if (a === '.') {
481
+ // the root token is a valid project positional (not a name).
482
+ project = ROOT_TOKEN;
483
+ i++;
484
+ break;
485
+ }
486
+ if (a.startsWith('-')) {
487
+ fail(`unknown option: ${a}`);
488
+ }
489
+ // the first bare positional is the project.
490
+ project = validateName(a, 'project');
491
+ i++;
492
+ break;
493
+ }
494
+ if (keepSeen && rmSeen) {
495
+ fail('--keep and --rm are contradictory (pick one; --rm is the default)');
496
+ }
497
+ // tokens remaining after the project.
498
+ const rest = args.slice(i);
499
+ if (shell) {
500
+ if (rest.length > 0) {
501
+ fail(`--shell takes at most one project, got extra: ${rest.join(' ')} ` +
502
+ '(a shell forwards no args; run pi from inside it instead)');
503
+ }
504
+ return {
505
+ mode: 'shell',
506
+ machine,
507
+ machineExplicit: machineSet,
508
+ project,
509
+ mountParent,
510
+ keep: keepSeen,
511
+ };
512
+ }
513
+ if (project === undefined) {
514
+ // no project + no --shell: the menu (bare, or -m/--mount with no project).
515
+ if (rest.length > 0)
516
+ fail(`unexpected argument: ${rest[0]}`);
517
+ return {
518
+ mode: 'menu',
519
+ machine,
520
+ machineExplicit: machineSet,
521
+ project: undefined,
522
+ mountParent,
523
+ keep: keepSeen,
524
+ };
525
+ }
526
+ // pi mode: every token after the project is forwarded to pi verbatim.
527
+ if (rest.length > 0)
528
+ piArgs = rest.slice();
529
+ return {
530
+ mode: 'pi',
531
+ machine,
532
+ machineExplicit: machineSet,
533
+ project,
534
+ mountParent,
535
+ keep: keepSeen,
536
+ piArgs,
537
+ };
538
+ }
539
+ /**
540
+ * PURE: resolve a LaunchIntent into a LaunchPlan, composing the netcage argv for
541
+ * every mode. Never spawns, never touches the filesystem: `homeFresh` reports
542
+ * whether the machine home has been seeded (so `fresh` is known) and is the only
543
+ * capability injected.
544
+ *
545
+ * Invariants held on EVERY composed argv:
546
+ * - the two mounts <home>:/root and <projectsRoot>:/projects, always;
547
+ * - --mount adds EXACTLY <parent>:/work and re-roots cwd, nothing else;
548
+ * - --proxy <p> + exactly one --allow-direct <llm> (forced egress, fail-closed);
549
+ * - --rm by default, omitted only under --keep.
550
+ *
551
+ * Throws AnonPiError (a plan is NEVER produced) when the image, the machine
552
+ * home, the proxy, or the direct-hole llm is missing.
553
+ */
554
+ export function resolveRunPlan(intent, homeFresh) {
555
+ const { machine, mode, projectsRoot, project, mountParent } = intent;
556
+ // Forced egress FIRST, on every path incl. the menu marker: a plan can never
557
+ // be produced without the proxy + the one direct hole (fail-closed).
558
+ const proxy = nonEmpty(intent.proxy);
559
+ if (proxy === undefined)
560
+ throw new AnonPiError(PROXY_REQUIRED_MESSAGE);
561
+ const llm = nonEmpty(intent.llmDirect);
562
+ if (llm === undefined) {
563
+ throw new AnonPiError('anon-pi: no local-model direct target: set ANON_PI_LLM (or config.llm) to the ' +
564
+ 'RFC1918/link-local IP[:port] of the local model. It is the ONE direct hole; ' +
565
+ 'all other egress stays forced through the proxy.');
566
+ }
567
+ if (nonEmpty(machine.image) === undefined) {
568
+ throw new AnonPiError(`anon-pi: machine ${JSON.stringify(machine.name)} has no image. Set one with ` +
569
+ '`anon-pi machine set-image` or in its machine.json.');
570
+ }
571
+ if (nonEmpty(machine.home) === undefined) {
572
+ throw new AnonPiError(`anon-pi: machine ${JSON.stringify(machine.name)} has no resolved home dir.`);
573
+ }
574
+ // Bare launch: defer to the host-side menu; compose no argv yet (but the
575
+ // forced-egress checks above have already run, so a menu is never a way to
576
+ // slip past the proxy requirement).
577
+ if (mode === 'menu') {
578
+ return { kind: 'menu', machine };
579
+ }
580
+ const mounted = nonEmpty(mountParent) !== undefined;
581
+ // Which root the cwd resolves under: /work when --mount, else /projects.
582
+ const rootKind = mounted ? 'mount' : 'projects';
583
+ // cwd: shell with no project sits at the machine home (/root); otherwise the
584
+ // project token (a name or `.`) resolves under the active root uniformly.
585
+ const cwd = project === undefined ? CONTAINER_HOME_ROOT : resolveCwd(rootKind, project);
586
+ const fresh = homeFresh(machine.home);
587
+ const seedVersion = intent.seedVersion ?? SEED_VERSION;
588
+ const directTarget = hostPortKey(llm);
589
+ const modelsSeed = nonEmpty(intent.modelsSeed);
590
+ // Interactive modes (interactive pi, shell) need a TTY; a HEADLESS pi run
591
+ // (`<project> <pi-args…>`) must work WITHOUT one, so `-it` is omitted there
592
+ // (podman fails to allocate a TTY on a non-tty stdin). The CLI's broader
593
+ // no-TTY discipline (erroring when an interactive mode has no TTY) is a later
594
+ // task; here the argv simply omits -it for the one headless shape.
595
+ const headless = mode === 'pi' && !!intent.piArgs && intent.piArgs.length > 0;
596
+ const netcageArgs = ['run'];
597
+ // --rm by DEFAULT (throwaway); --keep leaves the container kept.
598
+ if (intent.keep !== true)
599
+ netcageArgs.push('--rm');
600
+ // Forced egress: the proxy + the ONE direct hole. Never omitted.
601
+ netcageArgs.push('--proxy', proxy, '--allow-direct', directTarget);
602
+ if (!headless)
603
+ netcageArgs.push('-it');
604
+ // The TWO invariant mounts, ALWAYS.
605
+ netcageArgs.push('-v', `${machine.home}:${CONTAINER_HOME_ROOT}`);
606
+ netcageArgs.push('-v', `${projectsRoot}:${CONTAINER_PROJECTS_ROOT}`);
607
+ // --mount adds EXACTLY the one parent mount at /work (distinct from /projects,
608
+ // so the two roots never collide). Nothing else changes.
609
+ if (mounted) {
610
+ netcageArgs.push('-v', `${mountParent}:${CONTAINER_MOUNT_ROOT}`);
611
+ }
612
+ // The generated models.json read-only for the first-launch seed, when present.
613
+ if (modelsSeed !== undefined) {
614
+ netcageArgs.push('-v', `${modelsSeed}:${CONTAINER_MODELS_SEED}:ro`);
615
+ }
616
+ // The generated settings SEED (the local-model default selection) read-only,
617
+ // when present; the seed-if-fresh MERGES it into the home's settings.json.
618
+ const settingsSeed = nonEmpty(intent.settingsSeed);
619
+ if (settingsSeed !== undefined) {
620
+ netcageArgs.push('-v', `${settingsSeed}:${CONTAINER_SETTINGS_SEED}:ro`);
621
+ }
622
+ // The jail cwd.
623
+ netcageArgs.push('-w', cwd);
624
+ // The image, then the command: a marker-guarded seed-if-fresh then the tool.
625
+ // pi (with forwarded args) for pi mode; bash for a shell. The seed shape is
626
+ // containerRunCmd re-pointed at the machine home (/root), so a fresh machine
627
+ // home gets the image's staged defaults + models.json once.
628
+ netcageArgs.push(machine.image);
629
+ if (mode === 'shell') {
630
+ // A jailed bash: seed-if-fresh (so a fresh home still gets .bashrc etc.),
631
+ // then exec bash.
632
+ netcageArgs.push('sh', '-c', containerSeedThen(seedVersion, 'exec bash'));
633
+ }
634
+ else if (intent.piArgs && intent.piArgs.length > 0) {
635
+ // Forward args: seed-if-fresh, then exec pi with the args. The args are the
636
+ // shell's positional argv ($@) so they are forwarded verbatim (no re-quote).
637
+ netcageArgs.push('sh', '-c', containerSeedThen(seedVersion, 'exec pi "$@"'), 'pi', ...intent.piArgs);
638
+ }
639
+ else {
640
+ // Interactive pi: seed-if-fresh, then exec pi.
641
+ netcageArgs.push('sh', '-c', containerSeedThen(seedVersion, 'exec pi'));
642
+ }
643
+ return { kind: 'launch', machine, cwd, fresh, netcageArgs };
644
+ }
645
+ /**
646
+ * The marker-guarded seed-if-fresh prefix (reused across pi/bash), followed by
647
+ * the given exec. On a FRESH machine home (no `.anon-pi-seed` marker under
648
+ * /root/.pi/agent) it promotes the image's staged pi defaults
649
+ * (/opt/anon-pi-seed/agent) + the mounted models.json into the home and stamps
650
+ * the marker; on a seeded home it does nothing. Then it runs `exec`. This is
651
+ * `containerRunCmd`'s shape (already /root-pointed), generalised over the tool.
652
+ */
653
+ function containerSeedThen(seedVersion, exec) {
654
+ const agent = CONTAINER_AGENT_DIR;
655
+ const marker = `${agent}/${SEED_MARKER}`;
656
+ const settings = `${agent}/${SETTINGS_FILE}`;
657
+ // Merge the settings SEED (the local-model default selection) into the home's
658
+ // settings.json, overwriting ONLY the three selection keys so any staged
659
+ // packages/extensions survive. Done with a node one-liner (pi is a node app,
660
+ // so node is on PATH). The seed path + target are shell-quoted single args.
661
+ const mergeSettings = `{ [ -f "${CONTAINER_SETTINGS_SEED}" ] && node -e '` +
662
+ `const fs=require("fs");` +
663
+ `const seed=JSON.parse(fs.readFileSync(process.argv[1],"utf8"));` +
664
+ `let cur={};try{cur=JSON.parse(fs.readFileSync(process.argv[2],"utf8"))}catch(e){}` +
665
+ `cur.defaultProvider=seed.defaultProvider;cur.defaultModel=seed.defaultModel;cur.enabledModels=seed.enabledModels;` +
666
+ `fs.writeFileSync(process.argv[2],JSON.stringify(cur,null,"\\t")+"\\n")` +
667
+ `' "${CONTAINER_SETTINGS_SEED}" "${settings}" || true; }`;
668
+ return (`mkdir -p "${agent}" && ` +
669
+ `if [ ! -f "${marker}" ]; then ` +
670
+ `{ [ -d "${CONTAINER_STAGE_DIR}" ] && cp -a "${CONTAINER_STAGE_DIR}/." "${agent}/" || true; } && ` +
671
+ `{ [ -f "${CONTAINER_MODELS_SEED}" ] && cp "${CONTAINER_MODELS_SEED}" "${agent}/${MODELS_FILE}" || true; } && ` +
672
+ `${mergeSettings} && ` +
673
+ `printf '%s\\n' "${seedVersion}" > "${marker}"; ` +
674
+ `fi && ` +
675
+ `${exec}`);
676
+ }
677
+ /**
678
+ * PURE: the launch-identity match key for a kept container, derived ENTIRELY
679
+ * from the (machine, projects-root, project) identity (ADR-0002). It is what
680
+ * decides whether an existing kept `netcage.managed` container IS the one a
681
+ * `--keep` launch should resume.
682
+ *
683
+ * The fields, and why each is load-bearing:
684
+ * - `machine.name`: a kept container mounts THIS machine's home at /root; a
685
+ * same-project container on another machine is a different environment.
686
+ * - `projectsRoot`: the host dir mounted at /projects; two launches with the
687
+ * same project name but different roots are different working trees.
688
+ * - `mountParent` (or '' when absent): `--mount` re-roots into a DIFFERENT
689
+ * host parent at /work, so a `--mount` launch is a distinct identity from
690
+ * the projects-root launch of the same name.
691
+ * - the resolved container `cwd`: this already encodes the project token
692
+ * (`/projects/<p>`, `/work/<p>`, `.` -> a root, or /root for a bare shell)
693
+ * AND which root it sits under, so it is pi's conversation key too. Using
694
+ * the cwd keeps the container identity aligned with the conversation the
695
+ * kept container hosts.
696
+ *
697
+ * DELIBERATELY EXCLUDED (not part of identity): `--keep`/`--rm` (the throwaway
698
+ * choice for THIS run), the proxy + the direct-hole llm (forced-egress inputs),
699
+ * forwarded pi args, and the seed. Two launches that differ only in those must
700
+ * resolve to the SAME kept container.
701
+ *
702
+ * The key is a single opaque string (a `\n`-joined, field-tagged record) so the
703
+ * CLI can stamp it verbatim onto a netcage label and match on string equality;
704
+ * its internal shape is not a contract (compare only keys this function makes).
705
+ */
706
+ export function keptContainerKey(intent) {
707
+ const { machine, projectsRoot, project, mountParent } = intent;
708
+ const mounted = nonEmpty(mountParent) !== undefined;
709
+ const rootKind = mounted ? 'mount' : 'projects';
710
+ // The same cwd resolution resolveRunPlan uses, so the key names the exact
711
+ // container a matching launch would run in (its conversation key).
712
+ const cwd = project === undefined ? CONTAINER_HOME_ROOT : resolveCwd(rootKind, project);
713
+ return [
714
+ `machine=${machine.name}`,
715
+ `projectsRoot=${projectsRoot}`,
716
+ `mountParent=${nonEmpty(mountParent) ?? ''}`,
717
+ `cwd=${cwd}`,
718
+ ].join('\n');
719
+ }
720
+ /**
721
+ * PURE: decide run-vs-start for a launch given a SUPPLIED listing of kept
722
+ * `netcage.managed` containers (the CLI's netcage query result).
723
+ *
724
+ * - `--rm` (throwaway, `intent.keep !== true`): ALWAYS a fresh `run`. The
725
+ * listing is NOT consulted (a throwaway launch never resumes a kept box).
726
+ * - `--keep`: a kept container whose `key` equals this launch's
727
+ * keptContainerKey is present -> `start` it (by its `ref`); else -> `run`
728
+ * (resolveRunPlan leaves it kept because `--keep` omits `--rm`).
729
+ *
730
+ * Never spawns, never queries netcage: the listing is injected, so the whole
731
+ * decision is a pure function of (intent, listing).
732
+ */
733
+ export function resolveRunVsStart(intent, kept) {
734
+ // Throwaway short-circuit: a `--rm` launch is always a fresh run and never
735
+ // consults the listing (it must not resume a kept container).
736
+ if (intent.keep !== true)
737
+ return { action: 'run' };
738
+ const want = keptContainerKey(intent);
739
+ const match = kept.find((c) => c.key === want);
740
+ return match ? { action: 'start', ref: match.ref } : { action: 'run' };
741
+ }
742
+ // --- The bare-launch menu: choice-list + per-machine project-usage record ----
743
+ //
744
+ // anon-pi's bare launch shows a HOST-side arrow-key menu of a machine's
745
+ // projects BEFORE any jail runs. This module owns only the PURE data the menu
746
+ // renders; the CLI reads the real dirs (the projects root + each machine home's
747
+ // sessions dir) and renders the raw-mode TUI (the cli-bare-launch-menu-tui
748
+ // task). Everything here takes SUPPLIED listings so it stays unit-testable.
749
+ //
750
+ // Conversations are per-machine (each machine's home keeps its own pi
751
+ // sessions), but project FILES are global (the same folder is shared across
752
+ // machines). pi keys a session by its launch cwd, so a project used on a machine
753
+ // leaves a session dir at machines/<M>/home/.pi/agent/sessions/<slug>/, where
754
+ // <slug> is pi's cwd convention over /projects/<name> (projectSessionSlug),
755
+ // machine-invariant. "Used on" is therefore DERIVED from which machine homes
756
+ // contain that session dir - no marker file.
757
+ /**
758
+ * PURE: the pi session-dir slug for a project, i.e. pathSlug of its jail cwd
759
+ * `/projects/<name>`. Because the cwd is the SAME on every machine (files are
760
+ * global, the projects root is mounted at /projects everywhere), this slug is
761
+ * MACHINE-INVARIANT: the same shared project is recognised in each machine's
762
+ * sessions dir. Validates the name (rejecting traversal) as projectContainerCwd
763
+ * does. e.g. `alpha` -> `--projects-alpha--`.
764
+ */
765
+ export function projectSessionSlug(name) {
766
+ return pathSlug(projectContainerCwd(name));
767
+ }
768
+ /**
769
+ * PURE: build the menu choice-list from a SUPPLIED projects-root listing (the
770
+ * CLI's real `readdir` of the projects root). Entries that are not folder-safe
771
+ * project names (dotfiles like `.git`, `..`, path-separator names, whitespace,
772
+ * reserved tokens) are DROPPED silently: they can never be a valid project
773
+ * launch (validateName would reject them), and the `.` root is the separate
774
+ * `here` entry, not a listed project. The surviving names are sorted
775
+ * case-insensitively so the menu order is stable regardless of dir-read order.
776
+ *
777
+ * `canNew` / `canShell` default TRUE (both affordances are always offered
778
+ * today); they are fields so a later policy can gate them without a signature
779
+ * change. An empty projects root still offers here / new / shell.
780
+ */
781
+ export function buildMenuChoiceList(args) {
782
+ const projects = args.projects.filter(isProjectName).sort((a, b) => {
783
+ const la = a.toLowerCase();
784
+ const lb = b.toLowerCase();
785
+ if (la < lb)
786
+ return -1;
787
+ if (la > lb)
788
+ return 1;
789
+ // Case-insensitive ties keep a deterministic order via the raw compare.
790
+ return a < b ? -1 : a > b ? 1 : 0;
791
+ });
792
+ return {
793
+ projects,
794
+ here: ROOT_TOKEN,
795
+ canNew: args.canNew ?? true,
796
+ canShell: args.canShell ?? true,
797
+ };
798
+ }
799
+ /** True iff `name` is a folder-safe project name (validateName would accept it). */
800
+ function isProjectName(name) {
801
+ try {
802
+ validateName(name, 'project');
803
+ return true;
804
+ }
805
+ catch {
806
+ return false;
807
+ }
87
808
  }
88
809
  /**
89
- * The CANONICAL host seed dir holding models.json (written by `anon-pi import`).
90
- * Mounted read-only so the first-launch seed can copy models.json into a fresh
91
- * persistent home. Workdir-independent (import does not need a workdir).
810
+ * PURE: derive the per-machine project-usage record from SUPPLIED session-dir
811
+ * presence (no marker file). For each supplied project, in the SUPPLIED order,
812
+ * it reports which machines' homes contain that project's (machine-invariant)
813
+ * session slug, and whether the CURRENT machine is new for it.
814
+ *
815
+ * The project ORDER is preserved (the caller orders the menu, e.g. via
816
+ * buildMenuChoiceList); only the per-project `machines` list is sorted, so the
817
+ * "used on" annotation is stable. Validates each project name (rejecting
818
+ * traversal) via projectSessionSlug.
92
819
  */
93
- export function resolveConfigSeed(env) {
94
- if (env.configSeed)
95
- return resolve(env.configSeed);
96
- return join(resolveAnonPiHome(env), 'agent');
820
+ export function deriveProjectUsage(args) {
821
+ const { projects, currentMachine, sessions } = args;
822
+ const machineNames = Object.keys(sessions);
823
+ return projects.map((project) => {
824
+ const slug = projectSessionSlug(project);
825
+ const machines = machineNames
826
+ .filter((m) => (sessions[m] ?? []).includes(slug))
827
+ .sort();
828
+ const currentMachineIsNew = !(sessions[currentMachine] ?? []).includes(slug);
829
+ return { project, machines, currentMachineIsNew };
830
+ });
831
+ }
832
+ /** The fixed labels for the non-project affordances (one source, so the TUI + its test agree). */
833
+ export const MENU_HERE_LABEL = '. (here: a scratch pi at the root)';
834
+ export const MENU_NEW_LABEL = '+ new project\u2026';
835
+ export const MENU_SHELL_LABEL = 'shell (a jailed bash on this machine)';
836
+ /**
837
+ * PURE: render ONE project row's annotation from its usage record. Files are
838
+ * global but conversations are per-machine, so the row tells the user where a
839
+ * conversation for this project already lives (`used on: <machines>`) and
840
+ * whether the CURRENT machine has none yet (`new here`). An unused project on a
841
+ * fresh machine is just `new here` (no machine list). This is the whole
842
+ * user-visible surface of the derived usage record, kept pure + testable.
843
+ */
844
+ export function formatProjectAnnotation(usage) {
845
+ const parts = [];
846
+ if (usage.machines.length > 0) {
847
+ parts.push(`used on: ${usage.machines.join(', ')}`);
848
+ }
849
+ if (usage.currentMachineIsNew)
850
+ parts.push('new here');
851
+ return parts.length > 0 ? ` (${parts.join('; ')})` : '';
852
+ }
853
+ /**
854
+ * PURE: assemble the ordered, labelled, selectable menu rows from the choice-
855
+ * list + the per-project usage record. The order is: the projects (in the
856
+ * choice-list's stable sorted order), then the `.` "here" scratch entry, then
857
+ * `+ new project\u2026` (when `canNew`), then `shell` (when `canShell`). Each
858
+ * project row's label carries its used-on / new-here annotation
859
+ * (formatProjectAnnotation). This holds ALL the menu's logic (order + wording)
860
+ * so the raw-mode selector only renders these rows and dispatches the picked
861
+ * one by its `kind`/`project`.
862
+ *
863
+ * The `usage` list is expected to be keyed to `choiceList.projects` (same order,
864
+ * as deriveProjectUsage produces from the choice-list's projects); a project
865
+ * with no matching usage entry gets a bare, unannotated row rather than erroring.
866
+ */
867
+ export function buildMenuEntries(args) {
868
+ const { choiceList, usage } = args;
869
+ const byProject = new Map(usage.map((u) => [u.project, u]));
870
+ const entries = choiceList.projects.map((project) => {
871
+ const u = byProject.get(project);
872
+ const annotation = u ? formatProjectAnnotation(u) : '';
873
+ return { kind: 'project', project, label: `${project}${annotation}` };
874
+ });
875
+ entries.push({
876
+ kind: 'here',
877
+ project: choiceList.here,
878
+ label: MENU_HERE_LABEL,
879
+ });
880
+ if (choiceList.canNew)
881
+ entries.push({ kind: 'new', label: MENU_NEW_LABEL });
882
+ if (choiceList.canShell)
883
+ entries.push({ kind: 'shell', label: MENU_SHELL_LABEL });
884
+ return entries;
97
885
  }
98
886
  /**
99
887
  * Encode an absolute path into a directory name using pi's OWN convention (see
@@ -104,14 +892,6 @@ export function resolveConfigSeed(env) {
104
892
  export function pathSlug(absPath) {
105
893
  return `--${absPath.replace(/^[/\\]/, '').replace(/[/\\:]/g, '-')}--`;
106
894
  }
107
- /**
108
- * The persistent per-workdir state dir on the host (mounted at the container's
109
- * ~/.pi/agent). Keyed by the workdir via pi's path-slug convention:
110
- * <anonPiHome>/state/<slug>/agent
111
- */
112
- export function stateAgentDir(env, absWorkdir) {
113
- return join(resolveAnonPiHome(env), 'state', pathSlug(absWorkdir), 'agent');
114
- }
115
895
  /**
116
896
  * Normalise a proxy-less host:port key from an ANON_PI_LLM value or a provider
117
897
  * baseUrl, so `192.168.1.150:8080` matches `http://192.168.1.150:8080/v1`.
@@ -126,182 +906,476 @@ export function hostPortKey(value) {
126
906
  v = v.replace(/^[^@]*@/, ''); // drop any user:pass@
127
907
  return v.toLowerCase();
128
908
  }
129
- /** apiKey values that are NOT real secrets (safe to carry into the seed). */
130
- const BENIGN_API_KEYS = new Set(['', 'none', 'ollama', 'no-key', 'local']);
131
909
  /**
132
- * PURE: given a parsed host models.json and the ANON_PI_LLM value, select the
133
- * provider whose baseUrl points at that host:port and return a barebones
134
- * models.json carrying ONLY that provider (verbatim, with its models). Throws
135
- * AnonPiError if nothing matches. Carries no other provider (so etherplay /
136
- * google / paid API keys never enter the seed).
910
+ * The provider key anon-pi gives the single local provider it generates. A
911
+ * neutral, host-agnostic name (matches the CONTEXT glossary's "local model"):
912
+ * it carries NO host identity, unlike the old `import` path which kept the
913
+ * host's own provider key.
914
+ */
915
+ export const LOCAL_PROVIDER_NAME = 'local';
916
+ /**
917
+ * The pi `api` dialect the generated local provider speaks. Local model servers
918
+ * (llama.cpp, ollama, LM Studio, vLLM, ...) are overwhelmingly OpenAI-compatible
919
+ * and serve the completions API under `/v1`, so this is the safe default for an
920
+ * endpoint captured by `init` (there is no host models.json to copy a dialect
921
+ * from anymore). See the ## Decisions note in the done record.
922
+ */
923
+ export const LOCAL_PROVIDER_API = 'openai-completions';
924
+ /**
925
+ * A benign, non-secret apiKey for the local provider (a LAN model rarely needs a
926
+ * real key). It is one of the values pi never flags as a real secret.
927
+ */
928
+ export const LOCAL_PROVIDER_API_KEY = 'none';
929
+ /**
930
+ * apiKey values that are NOT real secrets (safe to carry into the anonymized
931
+ * seed verbatim). Anything else is treated as a REAL secret: `init` refuses to
932
+ * seed it (which would put a host credential into the anon home) unless the
933
+ * operator passes `--force-allow-local-llm-api-key`.
934
+ */
935
+ export const BENIGN_API_KEYS = new Set([
936
+ '',
937
+ 'none',
938
+ 'ollama',
939
+ 'no-key',
940
+ 'nokey',
941
+ 'local',
942
+ 'dummy',
943
+ 'sk-no-key-required',
944
+ ]);
945
+ /** PURE: whether an apiKey looks like a REAL secret (i.e. not in the benign set). */
946
+ export function apiKeyLooksReal(apiKey) {
947
+ if (apiKey === undefined)
948
+ return false;
949
+ return !BENIGN_API_KEYS.has(apiKey.trim().toLowerCase());
950
+ }
951
+ /**
952
+ * PURE: turn a discovered model `id` into a minimal-but-valid pi model entry.
953
+ * `name` defaults to the id; a LAN model is free, so every cost is 0.
137
954
  */
138
- export function pickProviderForLlm(hostModels, llmDirect) {
955
+ export function localModelEntry(id) {
956
+ return {
957
+ id,
958
+ name: id,
959
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
960
+ };
961
+ }
962
+ /**
963
+ * PURE: extract the model ids from a parsed OpenAI-compatible `/v1/models`
964
+ * response (`{ data: [{ id }, ...] }`, as llama.cpp / vLLM / LM Studio serve).
965
+ * Tolerates a bare array, a `models` key, missing/garbage input (returns []), so
966
+ * `init` can feed whatever the endpoint returned straight in.
967
+ */
968
+ export function parseModelsListing(raw) {
969
+ const rows = Array.isArray(raw)
970
+ ? raw
971
+ : raw && typeof raw === 'object'
972
+ ? (raw.data ??
973
+ raw.models ??
974
+ [])
975
+ : [];
976
+ if (!Array.isArray(rows))
977
+ return [];
978
+ const ids = [];
979
+ for (const r of rows) {
980
+ if (typeof r === 'string') {
981
+ if (r.trim() !== '')
982
+ ids.push(r.trim());
983
+ }
984
+ else if (r && typeof r === 'object') {
985
+ const id = r.id;
986
+ if (typeof id === 'string' && id.trim() !== '')
987
+ ids.push(id.trim());
988
+ }
989
+ }
990
+ return ids;
991
+ }
992
+ /**
993
+ * PURE: find, in a parsed host `~/.pi/agent/models.json`, the provider whose
994
+ * `baseUrl` points at `llmEndpoint` (matched via hostPortKey), and return ONLY
995
+ * that provider's models + apiKey. This is the anonymity-critical scoping: the
996
+ * ONLY provider considered is the one served by the `--allow-direct` endpoint,
997
+ * so no other provider (etherplay/google/a paid API) — and no other provider's
998
+ * key — can ever enter the seed. Returns undefined when no provider matches.
999
+ *
1000
+ * The `--allow-direct` target and this match both go through hostPortKey, so a
1001
+ * URL / ip:port / bare-ip host config all match the same endpoint.
1002
+ */
1003
+ export function pickLocalProviderModels(hostModels, llmEndpoint) {
139
1004
  const providers = hostModels.providers ?? {};
140
- const want = hostPortKey(llmDirect);
141
- const matches = [];
142
- for (const [name, p] of Object.entries(providers)) {
1005
+ const want = hostPortKey(llmEndpoint);
1006
+ for (const p of Object.values(providers)) {
143
1007
  if (!p || typeof p !== 'object' || !p.baseUrl)
144
1008
  continue;
145
- if (hostPortKey(p.baseUrl) === want)
146
- matches.push(name);
147
- }
148
- if (matches.length === 0) {
149
- const known = Object.entries(providers)
150
- .filter(([, p]) => p && p.baseUrl)
151
- .map(([n, p]) => ` ${n}: ${p.baseUrl}`)
152
- .join('\n');
153
- throw new AnonPiError(`anon-pi import: no provider in your host models.json points at ANON_PI_LLM (${want}).\n` +
154
- (known
155
- ? `Providers found:\n${known}\n`
156
- : 'No providers with a baseUrl were found.\n') +
157
- 'Set ANON_PI_LLM to the host:port of a provider above, or add that provider to pi first.');
158
- }
159
- const name = matches[0];
160
- const provider = providers[name];
161
- const key = (provider.apiKey ?? '').trim().toLowerCase();
162
- const apiKeyLooksReal = !BENIGN_API_KEYS.has(key);
1009
+ if (hostPortKey(p.baseUrl) !== want)
1010
+ continue;
1011
+ const models = [];
1012
+ for (const m of p.models ?? []) {
1013
+ if (m && typeof m === 'object') {
1014
+ const id = m.id;
1015
+ if (typeof id === 'string' && id.trim() !== '') {
1016
+ models.push({ ...m, id: id.trim() });
1017
+ }
1018
+ }
1019
+ else if (typeof m === 'string' && m.trim() !== '') {
1020
+ models.push(localModelEntry(m.trim()));
1021
+ }
1022
+ }
1023
+ return {
1024
+ models,
1025
+ apiKey: p.apiKey,
1026
+ apiKeyLooksReal: apiKeyLooksReal(p.apiKey),
1027
+ };
1028
+ }
1029
+ return undefined;
1030
+ }
1031
+ /**
1032
+ * PURE: merge the host-config models (rich, `configured: true`) with the
1033
+ * endpoint's live `/v1/models` ids (`configured: false` for any the host did not
1034
+ * already carry), into ONE deduped, sorted candidate list. Host config wins on
1035
+ * an id present in both (it has the real config). Every candidate here is served
1036
+ * by the endpoint, so every one is `--allow-direct`-reachable; the merge just
1037
+ * unions "what you already configured" with "what the server also offers".
1038
+ */
1039
+ export function mergeModelSources(hostModels, serverIds) {
1040
+ const byId = new Map();
1041
+ for (const m of hostModels) {
1042
+ const id = m.id.trim();
1043
+ if (id === '')
1044
+ continue;
1045
+ byId.set(id, { id, configured: true, entry: { ...m, id } });
1046
+ }
1047
+ for (const raw of serverIds) {
1048
+ const id = raw.trim();
1049
+ if (id === '' || byId.has(id))
1050
+ continue;
1051
+ byId.set(id, { id, configured: false, entry: localModelEntry(id) });
1052
+ }
1053
+ return Array.from(byId.values()).sort((a, b) => a.id.localeCompare(b.id));
1054
+ }
1055
+ /**
1056
+ * PURE: synthesize a pi `models.json` for the local provider from an endpoint
1057
+ * and the CHOSEN model entries. It normalises the endpoint with hostPortKey and
1058
+ * returns a models.json carrying exactly ONE provider (named LOCAL_PROVIDER_NAME
1059
+ * — a neutral name, no host fingerprint) pointed at that endpoint.
1060
+ *
1061
+ * `apiKey` defaults to the benign LOCAL_PROVIDER_API_KEY. A caller may pass the
1062
+ * host provider's real key ONLY under an explicit force flag; the benign/real
1063
+ * decision (and the refusal) lives in `init`, not here — this pure function just
1064
+ * writes what it is given.
1065
+ *
1066
+ * Accepts either full model entries (from the host config) or bare id strings
1067
+ * (which it turns into minimal entries). Empty models => a provider pointed at
1068
+ * the endpoint with no pickable model (the degraded fallback).
1069
+ */
1070
+ export function generateModelsJson(llmEndpoint, models = [], apiKey = LOCAL_PROVIDER_API_KEY) {
1071
+ const hostPort = hostPortKey(llmEndpoint);
1072
+ const entries = [];
1073
+ const seen = new Set();
1074
+ for (const m of models) {
1075
+ const entry = typeof m === 'string' ? localModelEntry(m.trim()) : m;
1076
+ const id = entry.id.trim();
1077
+ if (id === '' || seen.has(id))
1078
+ continue;
1079
+ seen.add(id);
1080
+ entries.push({ ...entry, id });
1081
+ }
1082
+ entries.sort((a, b) => a.id.localeCompare(b.id));
1083
+ const provider = {
1084
+ api: LOCAL_PROVIDER_API,
1085
+ apiKey,
1086
+ baseUrl: `http://${hostPort}/v1`,
1087
+ models: entries,
1088
+ };
1089
+ return { providers: { [LOCAL_PROVIDER_NAME]: provider } };
1090
+ }
1091
+ /**
1092
+ * PURE: the model-selection settings.json fragment for the seeded local
1093
+ * provider: `defaultProvider` = LOCAL_PROVIDER_NAME, `defaultModel` = the chosen
1094
+ * default id, `enabledModels` = `local/<id>` for each imported model (pi's
1095
+ * `<provider>/<id>` convention). The caller MERGES this into any existing
1096
+ * settings so image-staged settings (packages/extensions) are preserved.
1097
+ */
1098
+ export function generateModelSelection(modelIds, defaultId) {
1099
+ const ids = Array.from(new Set(modelIds.map((m) => m.trim()).filter((m) => m !== ''))).sort((a, b) => a.localeCompare(b));
163
1100
  return {
164
- name,
165
- models: { providers: { [name]: provider } },
166
- apiKeyLooksReal,
1101
+ defaultProvider: LOCAL_PROVIDER_NAME,
1102
+ defaultModel: defaultId.trim(),
1103
+ enabledModels: ids.map((id) => `${LOCAL_PROVIDER_NAME}/${id}`),
167
1104
  };
168
1105
  }
169
1106
  /**
170
- * The default host models.json path `import` reads FROM. Overridable via
171
- * ANON_PI_SOURCE_MODELS; defaults to the real pi config (~/.pi/agent/models.json
172
- * under the container-less host HOME, or PI_CODING_AGENT_DIR if the user set it).
1107
+ * PURE: shallow-merge the local-model selection into an existing (parsed)
1108
+ * settings.json object, returning the merged object. Only the three selection
1109
+ * keys are overwritten; every other key the user/image had (packages,
1110
+ * extensions, thinking level, ...) is preserved. `existing` undefined/garbage is
1111
+ * treated as `{}`.
173
1112
  */
174
- export function resolveSourceModelsPath(env) {
175
- if (env.sourceModels && env.sourceModels.trim() !== '') {
176
- return resolve(env.sourceModels);
177
- }
1113
+ export function mergeModelSelection(existing, selection) {
1114
+ const base = existing && typeof existing === 'object'
1115
+ ? { ...existing }
1116
+ : {};
1117
+ base.defaultProvider = selection.defaultProvider;
1118
+ base.defaultModel = selection.defaultModel;
1119
+ base.enabledModels = selection.enabledModels;
1120
+ return base;
1121
+ }
1122
+ /**
1123
+ * The host `~/.pi/agent/models.json` path `init` reads the matching local
1124
+ * provider from. Uses the container-less host HOME (or PI_CODING_AGENT_DIR when
1125
+ * the user relocated pi's agent dir). This is READ-ONLY (init copies only the
1126
+ * ONE matching provider's models); it is never written.
1127
+ */
1128
+ export function resolveHostModelsPath(env) {
178
1129
  const agentDir = env.piAgentDir && env.piAgentDir.trim() !== ''
179
1130
  ? env.piAgentDir
180
1131
  : join(env.home, '.pi', 'agent');
181
1132
  return join(agentDir, MODELS_FILE);
182
1133
  }
1134
+ // --- `anon-pi init` onboarding: the PURE proxy detect/verify DECISIONS --------
1135
+ //
1136
+ // `anon-pi init` onboards HONESTLY (this is an anonymity tool): its proxy step
1137
+ // presents EVIDENCE only (open ports, a real SOCKS5 handshake, a real `netcage
1138
+ // verify` exit IP) plus WEAK process hints. It MUST NEVER claim/label the exit
1139
+ // provider: a SOCKS proxy does not announce Mullvad/Proton/NordVPN/etc, so a
1140
+ // provider label would be a DANGEROUS LIE. This module owns the pure decisions
1141
+ // (handshake interpretation, the findings-without-labels formatter, the weak
1142
+ // hint wording, the verify exit-IP parse); the socket probes, the `netcage
1143
+ // verify` / `podman build` spawns, and the prompts are cli.ts's thin I/O.
183
1144
  /**
184
- * Build the run plan from the environment + the (optional) workdir arg. PURE: it
185
- * resolves paths and composes the netcage argv, performing NO filesystem writes
186
- * or spawns. It THROWS AnonPiError for the required inputs (image, llm, proxy).
1145
+ * The default SOCKS ports `init` probes, each with a WEAK, structural hint (the
1146
+ * conventional tool that DEFAULTS to that port). The hint names a local tool a
1147
+ * port is CONVENTIONALLY used by, NOT the exit provider: `9050`/`9150` are Tor's
1148
+ * own listeners (Tor IS the tool, so naming it is honest), `1080` is the generic
1149
+ * SOCKS default (wireproxy / `ssh -D` / other), which is why its hint stays
1150
+ * provider-agnostic ("wireproxy / ssh -D / generic"): behind a `1080` wireproxy
1151
+ * could be ANY WireGuard VPN, and we never guess which. See the ADR / Decisions.
1152
+ */
1153
+ export const DEFAULT_SOCKS_PROBE_PORTS = [
1154
+ { port: 9050, hint: 'Tor default (system tor)' },
1155
+ { port: 9150, hint: 'Tor Browser default' },
1156
+ { port: 1080, hint: 'generic SOCKS (wireproxy / ssh -D)' },
1157
+ ];
1158
+ /**
1159
+ * The SOCKS5 method-selection greeting `init` sends to CONFIRM a port really
1160
+ * speaks SOCKS5 (RFC 1928 §3): version 5, one method offered, `0x00`
1161
+ * (no-authentication). A real SOCKS5 server replies with two bytes
1162
+ * `[0x05, <method>]`; anything else is not SOCKS5. Exposed as a constant so the
1163
+ * probe I/O and the handshake test send byte-identical bytes.
1164
+ */
1165
+ export const SOCKS5_METHOD_SELECTOR = [0x05, 0x01, 0x00];
1166
+ /**
1167
+ * PURE: interpret a SOCKS5 method-selection REPLY (the bytes read back after
1168
+ * sending SOCKS5_METHOD_SELECTOR). A valid reply is EXACTLY the two bytes
1169
+ * `[0x05, <method>]` where `<method> != 0xff` (0xff = "no acceptable methods",
1170
+ * i.e. the server IS SOCKS5 but rejected no-auth; that is still a SOCKS5 server,
1171
+ * but for a bare no-auth probe we treat it as a soft failure so the finding does
1172
+ * not imply the port is usable no-auth). Any non-5 first byte, a short reply, or
1173
+ * an empty reply is NOT SOCKS5.
187
1174
  *
188
- * Statefulness (Model B): a persistent per-workdir host dir is mounted at the
189
- * container's ~/.pi/agent, so pi's sessions/history/settings/extensions persist.
190
- * First-launch seed (Model C): when that home is FRESH, the container run
191
- * command promotes the image's staged defaults + the imported models.json into
192
- * it and stamps a marker; thereafter pi OWNS the home and nothing is clobbered.
1175
+ * Reply in -> verdict out; the socket read is cli.ts's job. The reason strings
1176
+ * are deliberately structural ("no reply", "not SOCKS5") and NEVER name a
1177
+ * provider.
1178
+ */
1179
+ export function interpretSocks5Handshake(reply) {
1180
+ const bytes = Array.from(reply);
1181
+ if (bytes.length === 0)
1182
+ return { socks5: false, reason: 'no reply' };
1183
+ if (bytes.length < 2)
1184
+ return { socks5: false, reason: 'short reply' };
1185
+ if (bytes[0] !== 0x05)
1186
+ return { socks5: false, reason: 'not SOCKS5' };
1187
+ const method = bytes[1];
1188
+ if (method === 0xff) {
1189
+ return { socks5: false, reason: 'SOCKS5 but no acceptable auth method' };
1190
+ }
1191
+ return { socks5: true, method };
1192
+ }
1193
+ /**
1194
+ * PURE: map an observed local process name to a WEAK, hedged hint, or undefined
1195
+ * when we have nothing honest to say. The ONLY confident mapping is `tor` ->
1196
+ * "likely Tor", because Tor is a LOCAL tool that runs its OWN SOCKS listener (so
1197
+ * seeing `tor` is real evidence the port is Tor). We do NOT map anything to an
1198
+ * EXIT provider (Mullvad/Proton/...): a `wireproxy` process only tells us the
1199
+ * SOCKS front-end, never which VPN sits behind it, so its hint stays
1200
+ * provider-agnostic. Every returned hint is HEDGED ("likely", "-> a SOCKS
1201
+ * front-end") and never states the exit provider.
1202
+ */
1203
+ export function processHint(processName) {
1204
+ const name = processName.trim().toLowerCase();
1205
+ if (name === '')
1206
+ return undefined;
1207
+ if (name === 'tor') {
1208
+ return {
1209
+ process: processName,
1210
+ hint: 'a `tor` process is running -> likely Tor',
1211
+ };
1212
+ }
1213
+ if (name === 'wireproxy') {
1214
+ return {
1215
+ process: processName,
1216
+ // A SOCKS front-end for SOME WireGuard VPN; we NEVER guess which one.
1217
+ hint: 'a `wireproxy` process is running -> a SOCKS front-end for a ' +
1218
+ 'WireGuard VPN (which one is not observable here)',
1219
+ };
1220
+ }
1221
+ return undefined;
1222
+ }
1223
+ /**
1224
+ * The set of substrings a findings line must NEVER contain: known exit-provider
1225
+ * / VPN brand names. This is the machine-checkable half of the never-label rule
1226
+ * (a test asserts formatProxyFindings' output contains NONE of these for any
1227
+ * input). It is not exhaustive of every brand, but it pins the obvious ones so a
1228
+ * regression that starts labelling providers is caught. `tor` is NOT here: Tor
1229
+ * is the LOCAL tool we legitimately hint at, not an opaque exit provider.
1230
+ */
1231
+ export const FORBIDDEN_PROVIDER_LABELS = [
1232
+ 'mullvad',
1233
+ 'proton',
1234
+ 'nordvpn',
1235
+ 'nord vpn',
1236
+ 'expressvpn',
1237
+ 'express vpn',
1238
+ 'surfshark',
1239
+ 'ivpn',
1240
+ 'pia',
1241
+ 'private internet access',
1242
+ 'cyberghost',
1243
+ 'windscribe',
1244
+ ];
1245
+ /**
1246
+ * PURE: format the probe findings into the human-readable block `init` shows
1247
+ * before asking the user to CHOOSE a proxy. It renders EVIDENCE ONLY: for each
1248
+ * candidate, the `host:port`, whether it is open, the SOCKS5 handshake verdict,
1249
+ * and the structural PORT hint. It NEVER emits an exit-provider label (a SOCKS
1250
+ * proxy does not announce its provider; a false label is a dangerous lie). The
1251
+ * `## Decisions` note + a test assert the output never contains a
1252
+ * FORBIDDEN_PROVIDER_LABELS substring for any input.
193
1253
  *
194
- * `modelsSeedExists` reports whether the canonical import models.json exists (so
195
- * it is mounted for the seed); `stateExists` reports whether this workdir's
196
- * state home already exists (so `fresh` is known).
1254
+ * `processNote` is the HOST-WIDE weak process hint (a running `tor`/`wireproxy`
1255
+ * LOCAL process), shown ONCE as a general note rather than glued onto every port
1256
+ * line: the observation is host-wide, not per-port, so repeating it on each
1257
+ * candidate (including closed ports the process is unrelated to) reads as noise.
1258
+ * A per-finding `processHint`, if still set, is also honoured inline for
1259
+ * backward compatibility, but `init` now passes the host-wide note instead.
197
1260
  *
198
- * --ephemeral mounts NO writable state: pi writes to the container's own
199
- * filesystem, which netcage runs with `--rm`, so it is destroyed when the
200
- * container exits. Nothing writable ever touches a host path; there is no
201
- * cleanup and no leftover-on-crash. (The read-only models.json seed is still
202
- * mounted; it is a single file anon-pi never writes to.)
203
- */
204
- export function buildRunPlan(env, workdirArg, modelsSeedExists, stateExists) {
205
- if (!env.image || env.image.trim() === '') {
206
- // dockerfilePath is injected (cli.ts resolves the shipped Dockerfile.pi via
207
- // import.meta.url; tests pass a fixed path). Every command is emitted
208
- // flush-left so it copy-pastes cleanly: an indented heredoc would bake
209
- // leading spaces into the Dockerfile and break the EOF terminator, so we
210
- // point at the shipped file instead of printing a heredoc.
211
- const df = env.dockerfilePath ?? 'Dockerfile.pi';
212
- const wv = env.webveilDockerfilePath ?? 'examples/Dockerfile.pi-webveil';
213
- throw new AnonPiError('anon-pi: set ANON_PI_IMAGE to a container image that has `pi` on its PATH.\n' +
214
- '\n' +
215
- 'No image yet? A ready Dockerfile.pi ships with anon-pi (it installs the\n' +
216
- 'official @earendil-works/pi-coding-agent). Build it and point at it:\n' +
217
- '\n' +
218
- `podman build -t localhost/anon-pi-pi:latest -f "${df}" "$(dirname "${df}")"\n` +
219
- 'export ANON_PI_IMAGE=localhost/anon-pi-pi:latest\n' +
220
- '\n' +
221
- 'Or the fuller example with the pi-webveil extension + a local SearXNG\n' +
222
- '(anonymized web search):\n' +
223
- '\n' +
224
- `podman build -t localhost/anon-pi-webveil:latest -f "${wv}" "$(dirname "${wv}")"\n` +
225
- 'export ANON_PI_IMAGE=localhost/anon-pi-webveil:latest\n' +
226
- '\n' +
227
- 'See the README (Providing a pi image) for details and a community-image note.');
228
- }
229
- if (!env.llmDirect || env.llmDirect.trim() === '') {
230
- throw new AnonPiError('anon-pi: set ANON_PI_LLM to the RFC1918/link-local IP[:port] of the local model pi should reach directly (e.g. ANON_PI_LLM=192.168.1.150:8080). All other egress stays forced through the proxy.');
231
- }
232
- if (!env.proxy || env.proxy.trim() === '') {
233
- // No default: this is an anonymity tool, so the proxy is REQUIRED and never
234
- // guessed (mirrors netcage, which fails closed without --proxy). A silent
235
- // default would anonymize through the wrong endpoint, or fail deep in the
236
- // jail with a confusing DNS error, if the guessed proxy is not actually up.
237
- throw new AnonPiError('anon-pi: set ANON_PI_PROXY to your socks5h proxy. anon-pi has no default:\n' +
238
- 'the proxy is what makes the session anonymous, so it is never guessed.\n' +
239
- '\n' +
240
- 'Pick the one you run (copy-paste), then re-run anon-pi:\n' +
241
- '\n' +
242
- '# Tor (system tor / Tor Browser bundle default port)\n' +
243
- 'export ANON_PI_PROXY=socks5h://127.0.0.1:9050\n' +
244
- '\n' +
245
- '# wireproxy -> a WireGuard VPN (Mullvad, Proton, ...); use YOUR configured\n' +
246
- '# [Socks5] BindAddress port (1080 in wireproxy examples):\n' +
247
- 'export ANON_PI_PROXY=socks5h://127.0.0.1:1080\n' +
248
- '\n' +
249
- '# an SSH dynamic-forward (ssh -D 1080 host) or any other socks5h endpoint\n' +
250
- 'export ANON_PI_PROXY=socks5h://127.0.0.1:1080\n' +
251
- '\n' +
252
- 'Only socks5h:// is accepted (plain socks5:// resolves DNS locally and leaks).');
253
- }
254
- const home = env.home;
255
- if (!home || home.trim() === '') {
256
- throw new AnonPiError('anon-pi: could not resolve HOME.');
257
- }
258
- const raw = workdirArg && workdirArg.trim() !== '' ? workdirArg : process.cwd();
259
- const workdir = isAbsolute(raw) ? raw : resolve(raw);
260
- // Persistent per-workdir state home, unless --ephemeral (no writable mount).
261
- const ephemeral = env.ephemeral === true;
262
- const stateDir = ephemeral ? '' : stateAgentDir(env, workdir);
263
- // Ephemeral home is always fresh (the container's throwaway layer); a
264
- // persistent home is fresh iff its dir is absent.
265
- const fresh = ephemeral ? true : !stateExists(stateDir);
266
- // The canonical imported models.json is mounted (read-only) for the seed only
267
- // when it exists; pi can also start with no models and you add them in-session.
268
- const modelsSeed = join(resolveConfigSeed(env), MODELS_FILE);
269
- const haveModelsSeed = modelsSeedExists(modelsSeed);
270
- const proxy = env.proxy.trim();
271
- // netcage's --allow-direct wants a bare IP[:port]/CIDR (no scheme/path), but a
272
- // user naturally sets ANON_PI_LLM to a URL (http://192.168.1.150:8080). Strip
273
- // it to host:port with the same helper `import` uses to match providers, so a
274
- // URL, an ip:port, or a bare ip all work.
275
- const directTarget = hostPortKey(env.llmDirect);
276
- const seedVersion = env.seedVersion ?? SEED_VERSION;
277
- const netcageArgs = [
278
- 'run',
279
- '--proxy',
280
- proxy,
281
- '--allow-direct',
282
- directTarget,
283
- '-it',
284
- '-v',
285
- workdir, // netcage defaults a target-less -v to /work and cwd to /work
286
- ];
287
- // Persistent mode ONLY: mount the per-workdir state home at ~/.pi/agent
288
- // (Model B). --ephemeral mounts nothing writable: pi writes to the container's
289
- // own --rm layer, gone on exit, no host state.
290
- if (!ephemeral) {
291
- netcageArgs.push('-v', `${stateDir}:${CONTAINER_AGENT_DIR}`);
292
- }
293
- // Mount the imported models.json read-only for the first-launch seed, if any.
294
- if (haveModelsSeed) {
295
- netcageArgs.push('-v', `${modelsSeed}:${CONTAINER_MODELS_SEED}:ro`);
1261
+ * Findings in -> display string out; the socket probes are cli.ts's job.
1262
+ */
1263
+ export function formatProxyFindings(findings, processNote) {
1264
+ if (findings.length === 0) {
1265
+ return 'No SOCKS ports responded on the probed set. Enter your proxy as host:port.';
296
1266
  }
297
- netcageArgs.push(env.image, 'sh', '-c', containerRunCmd(seedVersion));
298
- return {
299
- workdir,
300
- stateDir,
301
- configSeed: haveModelsSeed ? modelsSeed : '',
302
- fresh,
303
- netcageArgs,
304
- };
1267
+ const lines = [];
1268
+ for (const f of findings) {
1269
+ const where = `${f.host}:${f.port}`;
1270
+ let status;
1271
+ if (!f.open) {
1272
+ status = 'closed (no TCP connection)';
1273
+ }
1274
+ else if (f.handshake && f.handshake.socks5) {
1275
+ status = 'open, SOCKS5 handshake OK';
1276
+ }
1277
+ else if (f.handshake && !f.handshake.socks5) {
1278
+ status = `open, but NOT SOCKS5 (${f.handshake.reason})`;
1279
+ }
1280
+ else {
1281
+ status = 'open';
1282
+ }
1283
+ const hints = [];
1284
+ if (f.portHint)
1285
+ hints.push(f.portHint);
1286
+ if (f.processHint)
1287
+ hints.push(f.processHint);
1288
+ const hintStr = hints.length > 0 ? ` [${hints.join('; ')}]` : '';
1289
+ lines.push(`${where}: ${status}${hintStr}`);
1290
+ }
1291
+ // The host-wide process observation, shown ONCE (not per port). It is a weak
1292
+ // LOCAL hint, never an exit-provider label.
1293
+ if (processNote && processNote.trim() !== '') {
1294
+ lines.push(`Note: ${processNote.trim()}`);
1295
+ }
1296
+ lines.push('These are EVIDENCE only (open ports + a real SOCKS5 handshake). A SOCKS ' +
1297
+ 'proxy does not announce its exit provider, so none is claimed here; the ' +
1298
+ '`netcage verify` step below shows the real exit IP as proof.');
1299
+ return lines.join('\n');
1300
+ }
1301
+ /**
1302
+ * PURE: the `socks5h://<host:port>` URL `init` hands to `netcage verify` and
1303
+ * writes into config.json. Only socks5h:// is accepted downstream (plain
1304
+ * socks5:// resolves DNS locally and leaks), so `init` always emits socks5h.
1305
+ * A value that already carries a scheme is normalised to its host:port first
1306
+ * (via hostPortKey) so `socks5h://socks5h://...` can never be produced.
1307
+ */
1308
+ export function socks5hUrl(hostPort) {
1309
+ return `socks5h://${hostPortKey(hostPort)}`;
1310
+ }
1311
+ /**
1312
+ * PURE: extract the exit IP `netcage verify` reported from its combined output.
1313
+ * `netcage verify` prints the jail's forced-egress exit IP (an IPv4/IPv6 line)
1314
+ * as PROOF the egress leaves via the proxy (not the host IP). We scan the output
1315
+ * for the first plausible IP literal and return it; undefined if none is found
1316
+ * (the caller then shows the raw output and lets the user judge). This is a
1317
+ * best-effort PARSE of another tool's text, kept pure + tested so a format tweak
1318
+ * is caught by a unit test, not only in the field.
1319
+ */
1320
+ export function parseVerifyExitIp(output) {
1321
+ // IPv4 first (the common case: ipify returns an IPv4 for most exits).
1322
+ const v4 = output.match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/);
1323
+ if (v4) {
1324
+ const ip = v4[0];
1325
+ if (ip.split('.').every((o) => Number(o) <= 255))
1326
+ return ip;
1327
+ }
1328
+ // IPv6 (a loose match: at least two groups and a colon-run), best-effort.
1329
+ const v6 = output.match(/\b(?:[0-9a-fA-F]{0,4}:){2,}[0-9a-fA-F]{0,4}\b/);
1330
+ if (v6 && v6[0].includes('::'))
1331
+ return v6[0];
1332
+ if (v6 && v6[0].split(':').filter(Boolean).length >= 3)
1333
+ return v6[0];
1334
+ return undefined;
1335
+ }
1336
+ /**
1337
+ * PURE: the ordered image-menu entries `init` shows. `[1]` basic pi
1338
+ * (Dockerfile.pi), `[2]` pi + webveil/SearXNG (examples/Dockerfile.pi-webveil),
1339
+ * `[3]` an existing image ref, `[4]` skip. A single source so the prompt and its
1340
+ * test agree on the order + wording.
1341
+ */
1342
+ export function initImageMenu() {
1343
+ return [
1344
+ { choice: 'basic', label: 'basic pi (build the shipped Dockerfile.pi)' },
1345
+ {
1346
+ choice: 'webveil',
1347
+ label: 'pi + webveil/SearXNG (build the shipped examples/Dockerfile.pi-webveil)',
1348
+ },
1349
+ { choice: 'existing', label: 'an existing image ref (I already have one)' },
1350
+ {
1351
+ choice: 'skip',
1352
+ label: 'skip (create the machine imageless; pin it later)',
1353
+ },
1354
+ ];
1355
+ }
1356
+ /**
1357
+ * PURE: build the `config.json` body `init` writes, keeping only the non-empty
1358
+ * fields (a skipped image / llm is simply omitted, never written as ""). Emits
1359
+ * pretty-printed JSON (tab indent, trailing newline) matching
1360
+ * serializeMachineJson, so a browsed ~/.anon-pi/config.json reads cleanly. The
1361
+ * proxy is REQUIRED (init only reaches here after a verified proxy), so it is
1362
+ * always present; llm / defaultMachine / projects are included when set.
1363
+ */
1364
+ export function serializeConfigJson(config) {
1365
+ const out = {};
1366
+ const proxy = nonEmpty(config.proxy);
1367
+ if (proxy !== undefined)
1368
+ out.proxy = proxy;
1369
+ const llm = nonEmpty(config.llm);
1370
+ if (llm !== undefined)
1371
+ out.llm = llm;
1372
+ const defaultMachine = nonEmpty(config.defaultMachine);
1373
+ if (defaultMachine !== undefined)
1374
+ out.defaultMachine = defaultMachine;
1375
+ const projects = nonEmpty(config.projects);
1376
+ if (projects !== undefined)
1377
+ out.projects = projects;
1378
+ return JSON.stringify(out, null, '\t') + '\n';
305
1379
  }
306
1380
  /**
307
1381
  * Absolute path to the Dockerfile.pi that ships with anon-pi, resolved from this
@@ -337,82 +1411,185 @@ function shippedFile(rel) {
337
1411
  }
338
1412
  return undefined;
339
1413
  }
1414
+ /**
1415
+ * PURE: parse the tokens AFTER `machine` into a MachineCommand. Validates the
1416
+ * machine name via validateName (the reserved-name / traversal guard) so the CLI
1417
+ * only ever joins a safe segment under the machines dir. Throws AnonPiError
1418
+ * (printed verbatim, exit 1) for an unknown/missing verb, a missing or extra
1419
+ * positional, an unknown flag, or a bad name.
1420
+ *
1421
+ * The grammar is deliberately small and flag-light (mirrors the launch grammar's
1422
+ * `--yes` / `--image` shape): `--image <ref>` on create, `--yes` on rm; no other
1423
+ * flags. This keeps `machine` a thin, predictable dispatch surface.
1424
+ */
1425
+ export function parseMachineArgs(args) {
1426
+ const fail = (msg) => {
1427
+ throw new AnonPiError(`anon-pi: ${msg}\nRun \`anon-pi machine --help\` or \`anon-pi --help\`.`);
1428
+ };
1429
+ const verb = args[0];
1430
+ if (verb === undefined) {
1431
+ fail('`machine` needs a subcommand: create | list | set-image | rm');
1432
+ }
1433
+ const rest = args.slice(1);
1434
+ if (verb === 'list') {
1435
+ if (rest.length > 0)
1436
+ fail(`machine list takes no arguments, got: ${rest.join(' ')}`);
1437
+ return { verb: 'list' };
1438
+ }
1439
+ if (verb === 'create') {
1440
+ let name;
1441
+ let image;
1442
+ for (let i = 0; i < rest.length; i++) {
1443
+ const a = rest[i];
1444
+ if (a === '--image') {
1445
+ const v = rest[++i];
1446
+ if (v === undefined)
1447
+ fail('--image needs an image ref');
1448
+ image = v;
1449
+ continue;
1450
+ }
1451
+ if (a.startsWith('-'))
1452
+ fail(`unknown option: ${a}`);
1453
+ if (name !== undefined)
1454
+ fail(`machine create takes one name, got extra: ${a}`);
1455
+ name = validateName(a, 'machine');
1456
+ }
1457
+ if (name === undefined)
1458
+ fail('machine create needs a <name>');
1459
+ return { verb: 'create', name: name, image: nonEmpty(image) };
1460
+ }
1461
+ if (verb === 'set-image') {
1462
+ let name;
1463
+ let image;
1464
+ for (const a of rest) {
1465
+ if (a.startsWith('-'))
1466
+ fail(`unknown option: ${a}`);
1467
+ if (name === undefined) {
1468
+ name = validateName(a, 'machine');
1469
+ }
1470
+ else if (image === undefined) {
1471
+ image = a;
1472
+ }
1473
+ else {
1474
+ fail(`machine set-image takes <name> <ref>, got extra: ${a}`);
1475
+ }
1476
+ }
1477
+ if (name === undefined)
1478
+ fail('machine set-image needs a <name> and an <image-ref>');
1479
+ if (nonEmpty(image) === undefined)
1480
+ fail('machine set-image needs an <image-ref>');
1481
+ return {
1482
+ verb: 'set-image',
1483
+ name: name,
1484
+ image: image.trim(),
1485
+ };
1486
+ }
1487
+ if (verb === 'rm') {
1488
+ let name;
1489
+ let yes = false;
1490
+ for (const a of rest) {
1491
+ if (a === '--yes' || a === '-y') {
1492
+ yes = true;
1493
+ continue;
1494
+ }
1495
+ if (a.startsWith('-'))
1496
+ fail(`unknown option: ${a}`);
1497
+ if (name !== undefined)
1498
+ fail(`machine rm takes one name, got extra: ${a}`);
1499
+ name = validateName(a, 'machine');
1500
+ }
1501
+ if (name === undefined)
1502
+ fail('machine rm needs a <name>');
1503
+ return { verb: 'rm', name: name, yes };
1504
+ }
1505
+ return fail(`unknown machine subcommand: ${verb} (create | list | set-image | rm)`);
1506
+ }
1507
+ /**
1508
+ * PURE: the JSON body a machine.json carries, given the pinned image (and an
1509
+ * optional per-machine projects override, preserved on a re-pin). A single
1510
+ * source so create + set-image write byte-identical, pretty-printed JSON (tab
1511
+ * indent, trailing newline) that reads cleanly when the user browses
1512
+ * ~/.anon-pi/machines/<M>/machine.json.
1513
+ */
1514
+ export function serializeMachineJson(config) {
1515
+ const out = {};
1516
+ if (nonEmpty(config.image) !== undefined)
1517
+ out.image = config.image.trim();
1518
+ if (nonEmpty(config.projects) !== undefined)
1519
+ out.projects = config.projects.trim();
1520
+ return JSON.stringify(out, null, '\t') + '\n';
1521
+ }
1522
+ /**
1523
+ * PURE: the compatibility WARNING `machine set-image` prints after re-pinning
1524
+ * the image. Re-pinning does NOT reseed or touch the home: the home's pi
1525
+ * extensions / downloaded bin were built against the OLD image, so a mismatched
1526
+ * new image may misbehave. The message tells the user the two remedies (re-run
1527
+ * `pi install` inside the machine, or delete the home to reseed) WITHOUT doing
1528
+ * either automatically. See the ## Decisions note (set-image warning wording).
1529
+ */
1530
+ export function setImageWarning(name, oldImage, newImage) {
1531
+ const from = oldImage === undefined ? '(none)' : oldImage;
1532
+ return (`anon-pi: re-pinned machine ${JSON.stringify(name)} image ${from} -> ${newImage}.\n` +
1533
+ 'WARNING: the home was NOT reseeded. Its pi extensions and downloaded tools\n' +
1534
+ 'were built for the old image; if they misbehave on the new one, re-run\n' +
1535
+ '`pi install` inside the machine, or delete + reseed the home with\n' +
1536
+ `\`anon-pi --delete-home ${name}\` (then relaunch to seed fresh).`);
1537
+ }
340
1538
  /** Read the AnonPiEnv from a process env map (kept separate so tests inject one). */
341
1539
  export function envFromProcess(penv) {
342
1540
  return {
343
1541
  home: penv.HOME ?? homedir(),
344
1542
  proxy: penv.ANON_PI_PROXY,
345
1543
  anonPiHome: penv.ANON_PI_HOME,
346
- configSeed: penv.ANON_PI_CONFIG,
1544
+ projects: penv.ANON_PI_PROJECTS,
347
1545
  image: penv.ANON_PI_IMAGE,
348
1546
  llmDirect: penv.ANON_PI_LLM,
349
1547
  xdgConfigHome: penv.XDG_CONFIG_HOME,
1548
+ piAgentDir: penv.PI_CODING_AGENT_DIR,
350
1549
  dockerfilePath: shippedDockerfilePath(),
351
1550
  webveilDockerfilePath: shippedWebveilDockerfilePath(),
352
- sourceModels: penv.ANON_PI_SOURCE_MODELS,
353
- piAgentDir: penv.PI_CODING_AGENT_DIR,
354
- ephemeral: isTruthy(penv.ANON_PI_EPHEMERAL),
355
1551
  };
356
1552
  }
357
- /** Whether an env-var string is set to a truthy value (1/true/yes, any case). */
358
- function isTruthy(v) {
359
- if (!v)
360
- return false;
361
- const s = v.trim().toLowerCase();
362
- return s === '1' || s === 'true' || s === 'yes' || s === 'on';
363
- }
364
1553
  /** The --help text (kept here so it is covered by the same module). */
365
- export const HELP = `anon-pi - launch pi inside a netcage (anonymized egress + one direct local model)
1554
+ export const HELP = `anon-pi - run pi on anonymized, jailed machines (netcage: forced egress + one direct local model)
366
1555
 
367
1556
  USAGE
368
- anon-pi [WORKDIR] launch pi jailed, working in WORKDIR (default: cwd)
369
- anon-pi import seed models.json from your local model
370
-
371
- WORKDIR the host folder pi works in (mounted at ${CONTAINER_WORKDIR}; pi's cwd). Files pi
372
- writes there land on the host.
373
-
374
- WHAT IT DOES
375
- Runs pi inside netcage with all web/DNS egress forced through the socks5h
376
- proxy (fail-closed) and ONE direct hole to your local model (ANON_PI_LLM).
377
-
378
- STATEFUL by default: a persistent per-workdir home
379
- (<ANON_PI_HOME>/state/<workdir>/agent) is mounted at the container's
380
- ~/.pi/agent, so your conversations, history, settings (model choice), and any
381
- extensions you \`pi install\` persist across launches. Re-running in the same
382
- folder resumes it. On a FRESH home, the image's staged defaults (extensions,
383
- trust) and your imported models.json are seeded in once; after that pi owns the
384
- home and nothing is overwritten. Requires \`netcage\`.
1557
+ anon-pi MENU: pick a project (pi), a shell, or a new project
1558
+ anon-pi <project> pi in the project (${CONTAINER_PROJECTS_ROOT}/<project>); exit pi -> host
1559
+ anon-pi <project> <pi-args…> forward args to pi (headless/one-shot; no TTY needed)
1560
+ anon-pi --shell [<project>] a jailed bash (at ~, or cd'd into <project>) - the project-hopper
1561
+ anon-pi -m <machine> [<p>] the same, on <machine> (its own image + home + conversations)
1562
+ anon-pi --mount <parent> [<p>] root at a HOST parent folder instead of the projects root
1563
+ anon-pi init onboard: verify your proxy, capture your local model, pick an image
1564
+ anon-pi machine … manage machines (create / list / set-image / rm)
1565
+ anon-pi --delete-home [<m>] delete a machine's home (config + convos); keep its image pin + files
1566
+ anon-pi --delete-project <p> delete a project's files + its per-machine sessions; keep the homes
385
1567
 
386
- --ephemeral (or ANON_PI_EPHEMERAL=1): mount NO writable state; pi writes to the
387
- container's own --rm layer, gone on exit. Nothing writable touches the host,
388
- no cleanup, no leftover-on-crash.
1568
+ <project> a folder under the projects root (mounted at ${CONTAINER_PROJECTS_ROOT}; pi's cwd). \`.\` means
1569
+ the root itself (a scratch pi at ${CONTAINER_PROJECTS_ROOT}, ${CONTAINER_MOUNT_ROOT} for --mount, or ~).
389
1570
 
390
- --fresh: delete this workdir's persistent state home first, so the (possibly
391
- rebuilt) image's defaults + your imported models.json are re-seeded. Use it
392
- after rebuilding your image to pick up new extensions/config.
1571
+ [--rm] throwaway container this run (the DEFAULT; deleted on exit).
1572
+ [--keep] leave the container KEPT so its filesystem survives (apt install,
1573
+ quit, re-enter). anon-pi finds it by netcage's managed label and
1574
+ \`netcage start\`s it on re-entry.
393
1575
 
394
- import
395
- Reads your host ~/.pi/agent/models.json, picks the provider whose baseUrl
396
- serves ANON_PI_LLM, and writes JUST that provider to the canonical seed
397
- (<ANON_PI_CONFIG>/models.json). No other provider's API keys, no sessions, no
398
- identity. It SEEDS a fresh home; models you later add inside pi persist and are
399
- never clobbered. Re-run with --force to overwrite the canonical seed.
1576
+ WHAT IT DOES
1577
+ Runs pi inside netcage with all web/DNS egress forced through the socks5h proxy
1578
+ (fail-closed) and ONE direct hole to your local model (ANON_PI_LLM). A MACHINE
1579
+ is an image + a persistent HOST home (bind-mounted at ${CONTAINER_HOME_ROOT}) holding your pi
1580
+ config, extensions, and conversations; the container is disposable, so \`--rm\`
1581
+ loses nothing. Files (projects) are global by default; conversations are
1582
+ per-machine. On a FRESH machine home the image's staged defaults + your
1583
+ models.json are seeded in once; after that pi owns the home. Requires \`netcage\`.
400
1584
 
401
1585
  ENVIRONMENT
402
- ANON_PI_IMAGE (required for run) image with \`pi\` on PATH. No image yet?
403
- Running anon-pi without it prints a ready-to-build
404
- Dockerfile.pi recipe; see the README (Providing a pi image).
405
- ANON_PI_LLM (required) RFC1918/link-local IP[:port] of the local model
406
1586
  ANON_PI_PROXY (required) socks5h URL of your proxy (Tor/wireproxy/ssh -D).
407
1587
  No default: the proxy is what anonymizes, so it is never guessed.
408
- ANON_PI_EPHEMERAL set to 1 for a throwaway (non-persistent) session
409
- ANON_PI_HOME anon-pi home (default $XDG_CONFIG_HOME/anon-pi or ~/.config/anon-pi)
410
- ANON_PI_CONFIG canonical seed dir holding models.json (default <ANON_PI_HOME>/agent)
411
- ANON_PI_SOURCE_MODELS (import) host models.json to read (default ~/.pi/agent/models.json)
412
-
413
- RESET A SESSION
414
- anon-pi --fresh [WORKDIR] drop the session home and re-seed on this launch.
415
- Or delete it by hand: rm -rf <ANON_PI_HOME>/state/<workdir-slug>/agent
1588
+ ANON_PI_LLM (required) RFC1918/link-local IP[:port] of the local model
1589
+ ANON_PI_IMAGE image with \`pi\` on PATH, used when a machine has no image set.
1590
+ No image yet? See the README (Providing a pi image).
1591
+ ANON_PI_HOME anon-pi workspace dir (default ~/.anon-pi; NOT under ~/.config)
1592
+ ANON_PI_PROJECTS projects root override (host dir mounted at ${CONTAINER_PROJECTS_ROOT})
416
1593
 
417
1594
  PLATFORM
418
1595
  Linux only (via netcage's netns/nft jail). On macOS/Windows it works only