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