@dreamlake/dreamlake-cli 0.94.0 → 0.96.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.
@@ -10,6 +10,7 @@ export declare function slugify(text: string): string;
10
10
  export declare function detectKind(file: string, override?: string): string | null;
11
11
  export interface ArtifactPushArgs extends DreamlakeOpts {
12
12
  file: string;
13
+ entry?: string;
13
14
  title?: string;
14
15
  kind?: string;
15
16
  artifactId?: string;
@@ -10,6 +10,7 @@ import { registerShareCommands } from "../share.js";
10
10
  import { readFileSync, statSync } from "node:fs";
11
11
  import path from "node:path";
12
12
  import { uploadIncremental, MAX_SIZE } from "./incremental.js";
13
+ import { packPages } from "./pages.js";
13
14
  import { metaError, putArtifactCover, readCoverFile, registerArtifactMetaCommand } from "./meta.js";
14
15
  // ../prompt.js, NOT @inquirer/prompts directly. This file used to import the
15
16
  // inquirer prompt straight, which bypassed prompt.ts's `!process.stdin.isTTY`
@@ -85,21 +86,30 @@ function shellQuote(value) {
85
86
  export async function runDreamlakeArtifactPush(args, deps = {}) {
86
87
  const incremental = !args.full && args.incremental !== false;
87
88
  let content;
89
+ let directory = false;
88
90
  try {
89
91
  const stat = statSync(args.file);
90
- if (!stat.isFile())
91
- throw new Error("not a file");
92
+ directory = stat.isDirectory();
93
+ if (!directory && !stat.isFile())
94
+ throw new Error("not a file or directory");
95
+ if (!directory && args.entry !== undefined)
96
+ throw new Error("--entry requires a directory");
97
+ if (directory && args.kind && args.kind !== "html")
98
+ throw new Error("page directories require --kind html");
92
99
  if (incremental && stat.size > MAX_SIZE) {
93
100
  fail("incremental artifacts are limited to 512 MiB");
94
101
  return 1;
95
102
  }
96
- content = readFileSync(args.file);
103
+ content = directory ? packPages(args.file, args.entry) : readFileSync(args.file);
97
104
  }
98
- catch {
99
- fail(`file not found: ${args.file}`);
105
+ catch (error) {
106
+ const missing = error.code === 'ENOENT';
107
+ fail(missing
108
+ ? `file not found: ${args.file}`
109
+ : `cannot read artifact '${args.file}': ${error instanceof Error ? error.message : String(error)}`);
100
110
  return 1;
101
111
  }
102
- const kind = detectKind(args.file, args.kind);
112
+ const kind = directory ? "html" : detectKind(args.file, args.kind);
103
113
  if (!kind) {
104
114
  fail(`cannot determine kind for '${path.extname(args.file)}'. ` +
105
115
  `use --kind (${[...KINDS].sort().join(", ")})`);
@@ -109,7 +119,9 @@ export async function runDreamlakeArtifactPush(args, deps = {}) {
109
119
  fail(`--visibility must be public or private (got '${args.visibility}')`);
110
120
  return 1;
111
121
  }
112
- const defaultTitle = path.basename(args.file, path.extname(args.file));
122
+ const defaultTitle = directory
123
+ ? path.basename(path.resolve(args.file))
124
+ : path.basename(args.file, path.extname(args.file));
113
125
  const artifactId = args.artifactId ?? slugify(args.title ?? defaultTitle);
114
126
  // Read the cover before any network call so a bad file fails fast.
115
127
  let coverBytes = null;
@@ -360,7 +372,8 @@ export function registerArtifactCommand(program) {
360
372
  withConnFlags(artifact
361
373
  .command("push")
362
374
  .description("push a renderable artifact — a new version of --id (written straight to storage)")
363
- .argument("<file>", "file to push (kind auto-detected from the extension)")
375
+ .argument("<file>", "file or self-contained HTML page directory to push")
376
+ .option("--entry <path>", "entry page for a directory (default: index.html)")
364
377
  .option("--title <title>", "human title (default: file stem)")
365
378
  .option("--kind <kind>", "html, react, markdown, svg, code, mermaid (default: auto)")
366
379
  .option("--id <artifact-id>", "stable artifact id for versioning (default: slug of title)")
@@ -0,0 +1,2 @@
1
+ /** Collect one complete snapshot before acquiring credentials or writing objects. */
2
+ export declare function packPages(directory: string, entry?: string): Buffer;
@@ -0,0 +1,45 @@
1
+ import { lstatSync, readdirSync, readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { encodePages, MAX_PAGES, MAX_PAGES_BYTES, validPagePath } from './pagesFormat.js';
4
+ /** Collect one complete snapshot before acquiring credentials or writing objects. */
5
+ export function packPages(directory, entry = 'index.html') {
6
+ if (!validPagePath(entry))
7
+ throw new Error('entry must be a relative .html/.htm path');
8
+ const pages = [];
9
+ let bytes = 0;
10
+ let visited = 0;
11
+ function walk(relative, depth) {
12
+ if (depth > 16)
13
+ throw new Error('artifact page directories are limited to 16 levels');
14
+ for (const name of readdirSync(path.join(directory, relative)).sort()) {
15
+ if (++visited > 1024)
16
+ throw new Error('artifact page directories are limited to 1024 entries');
17
+ const file = path.join(directory, relative, name);
18
+ const stat = lstatSync(file);
19
+ if (stat.isSymbolicLink())
20
+ throw new Error(`symlinks are not supported: ${file}`);
21
+ // Do not accidentally publish repository metadata, secrets or editor files.
22
+ if (name.startsWith('.'))
23
+ continue;
24
+ const pagePath = relative ? `${relative}/${name}` : name;
25
+ if (stat.isDirectory()) {
26
+ walk(pagePath, depth + 1);
27
+ continue;
28
+ }
29
+ if (!stat.isFile() || !validPagePath(pagePath))
30
+ throw new Error(`expected self-contained HTML pages; unsupported file: ${pagePath}`);
31
+ if (pages.length >= MAX_PAGES)
32
+ throw new Error('artifact bundles are limited to 128 pages');
33
+ bytes += stat.size;
34
+ if (bytes > MAX_PAGES_BYTES)
35
+ throw new Error('artifact pages exceed 32 MiB');
36
+ const data = readFileSync(file);
37
+ const content = new TextDecoder('utf-8', { fatal: true }).decode(data);
38
+ pages.push({ path: pagePath, content });
39
+ }
40
+ }
41
+ if (!lstatSync(directory).isDirectory())
42
+ throw new Error('expected a page directory (not a symlink)');
43
+ walk('', 0);
44
+ return Buffer.from(encodePages({ entry, pages }));
45
+ }
@@ -0,0 +1,16 @@
1
+ export declare const PAGES_FAMILY = "<!--DREAMLAKE-ARTIFACT-PAGES/";
2
+ export declare const PAGES_PREFIX = "<!--DREAMLAKE-ARTIFACT-PAGES/1-->\n<script type=\"application/json\" id=\"dreamlake-artifact-pages\">";
3
+ export declare const PAGES_SUFFIX = "</script>\n<p>This multi-page artifact requires an updated DreamLake viewer.</p>";
4
+ export declare const MAX_PAGES = 128;
5
+ export declare const MAX_PAGES_BYTES: number;
6
+ export interface HtmlPages {
7
+ entry: string;
8
+ pages: {
9
+ path: string;
10
+ content: string;
11
+ }[];
12
+ }
13
+ export declare function validPagePath(path: unknown): path is string;
14
+ export declare function validatePages(value: unknown): HtmlPages;
15
+ export declare function encodePages(value: HtmlPages): string;
16
+ export declare function decodePages(content: string): HtmlPages | null;
@@ -0,0 +1,50 @@
1
+ // Wire contract shared with dreamlake-ai/frame/pages.ts.
2
+ // Keep the format/limits in sync; the envelope is inert HTML in older viewers.
3
+ export const PAGES_FAMILY = '<!--DREAMLAKE-ARTIFACT-PAGES/';
4
+ export const PAGES_PREFIX = `${PAGES_FAMILY}1-->\n<script type="application/json" id="dreamlake-artifact-pages">`;
5
+ export const PAGES_SUFFIX = '</script>\n<p>This multi-page artifact requires an updated DreamLake viewer.</p>';
6
+ export const MAX_PAGES = 128;
7
+ export const MAX_PAGES_BYTES = 32 * 1024 * 1024;
8
+ export function validPagePath(path) {
9
+ return typeof path === 'string' && path.length <= 512 &&
10
+ path === path.normalize('NFC') && !/[\\%?:#\u0000-\u0020\u007f]/.test(path) &&
11
+ path.split('/').every(part => !!part && part !== '.' && part !== '..') &&
12
+ /\.html?$/i.test(path);
13
+ }
14
+ export function validatePages(value) {
15
+ const bundle = value;
16
+ if (!bundle || !validPagePath(bundle.entry) || !Array.isArray(bundle.pages) ||
17
+ !bundle.pages.length || bundle.pages.length > MAX_PAGES)
18
+ throw new Error('Invalid artifact page bundle');
19
+ const paths = new Set();
20
+ let size = 0;
21
+ for (const page of bundle.pages) {
22
+ if (!page || !validPagePath(page.path) || typeof page.content !== 'string' ||
23
+ paths.has(page.path.toLowerCase()))
24
+ throw new Error('Invalid or duplicate artifact page');
25
+ paths.add(page.path.toLowerCase());
26
+ size += new TextEncoder().encode(page.content).length;
27
+ if (size > MAX_PAGES_BYTES)
28
+ throw new Error('Artifact pages exceed 32 MiB');
29
+ }
30
+ if (!bundle.pages.some(page => page.path === bundle.entry))
31
+ throw new Error('Artifact entry page is missing');
32
+ return bundle;
33
+ }
34
+ export function encodePages(value) {
35
+ validatePages(value);
36
+ const json = JSON.stringify(value).replace(/</g, '\\u003c').replace(/>/g, '\\u003e').replace(/&/g, '\\u0026');
37
+ const content = PAGES_PREFIX + json + PAGES_SUFFIX;
38
+ if (new TextEncoder().encode(content).length > MAX_PAGES_BYTES)
39
+ throw new Error('Encoded artifact pages exceed 32 MiB');
40
+ return content;
41
+ }
42
+ export function decodePages(content) {
43
+ if (!content.startsWith(PAGES_FAMILY))
44
+ return null;
45
+ if (!content.startsWith(PAGES_PREFIX) || !content.endsWith(PAGES_SUFFIX))
46
+ throw new Error('Unsupported or malformed artifact page bundle');
47
+ if (content.length > MAX_PAGES_BYTES || new TextEncoder().encode(content).length > MAX_PAGES_BYTES)
48
+ throw new Error('Encoded artifact pages exceed 32 MiB');
49
+ return validatePages(JSON.parse(content.slice(PAGES_PREFIX.length, -PAGES_SUFFIX.length)));
50
+ }
@@ -4,7 +4,7 @@ export const helpExamples = {
4
4
  "agents show": "dreamlake agents show my_repo.tri --namespace my-workspace",
5
5
  "agents edit": "dreamlake agents edit my_repo.tri --namespace my-workspace\n# Browser form: follow the localhost URL printed by the command.\ndreamlake agents edit my_repo.tri --namespace my-workspace --web",
6
6
  "telemetry": "dreamlake telemetry disable --quiet\ndreamlake telemetry status\ndreamlake telemetry enable --quiet",
7
- "artifact push": "dreamlake artifact push ./dashboard.html --id q1-dashboard\n# Edit the file, then repeat the same command to upload only new chunks.\n\n# Compatibility with older self-hosted viewers:\ndreamlake artifact push ./dashboard.html --id q1-dashboard --full",
7
+ "artifact push": "dreamlake artifact push ./dashboard.html --id q1-dashboard\n# Edit the file, then repeat the same command to upload only new chunks.\n\n# Compatibility with older self-hosted viewers:\ndreamlake artifact push ./dashboard.html --id q1-dashboard --full\n\n# Multiple self-contained HTML pages (requires an updated viewer):\ndreamlake artifact push ./report --entry index.html --id quarterly-report",
8
8
  "artifact meta set": "# At push time:\ndreamlake artifact push ./report.html --id q1-dashboard \\\n --description \"Revenue, churn and pipeline for the quarter\" \\\n --cover ./cover.png\n\n# Or after the fact, for an artifact that already exists:\ndreamlake artifact meta set q1-dashboard --description \"Revenue, churn and pipeline\"\ndreamlake artifact meta set q1-dashboard --cover ./cover.png\ndreamlake artifact meta show q1-dashboard\n\n# Back to the automatic description / the generated cover:\ndreamlake artifact meta set q1-dashboard --clear-description --clear-cover",
9
9
  "artifact share": "# Find the dashboard you already uploaded; no new version is needed.\ndreamlake artifact list\nARTIFACT=\"q1-dashboard\" # Replace with the id from list.\ndreamlake artifact share get \"$ARTIFACT\"\n\n# Give signed-in recipients read access, then verify the returned link.\ndreamlake artifact share create \"$ARTIFACT\"\ndreamlake artifact share get \"$ARTIFACT\"\n\n# When link access is no longer needed, revoke it and verify.\ndreamlake artifact share revoke \"$ARTIFACT\"\ndreamlake artifact share get \"$ARTIFACT\"",
10
10
  "artifact visibility": "ARTIFACT=\"q1-dashboard\" # Your existing artifact id.\ndreamlake artifact visibility \"$ARTIFACT\" public\ndreamlake artifact visibility \"$ARTIFACT\" private",
@@ -43,6 +43,8 @@ export const helpExamples = {
43
43
  "hosts ssh": "dreamlake hosts ssh geyang/dev/worker --duration 1800",
44
44
  "exec": "dreamlake exec geyang/dev/worker -- uname -a\ndreamlake exec geyang/dev/worker --timeout 30 -- sh -c 'printf \"%s\\n\" \"$HOME\"'",
45
45
  "hosts exec": "dreamlake hosts exec geyang/dev/worker -- python3 -c 'print(\"hello\")'",
46
+ "hosts upgrade": "dreamlake hosts upgrade geyang/dev/worker --nymph-version 0.1.32 --dry-run\ndreamlake hosts upgrade geyang/dev/worker --nymph-version 0.1.32\ndreamlake hosts upgrade 'geyang/lab/*' --nymph-version 0.1.32 --concurrency 4 --max-failures 2\ndreamlake hosts upgrade --all --namespace geyang --nymph-version 0.1.32 --json",
47
+ "hosts rollback": "dreamlake hosts rollback geyang/dev/worker\ndreamlake hosts rollback geyang/dev/worker --upgrade-id 20261029-001 --json",
46
48
  "infra list": "dreamlake infra list --namespace fortyfive --project dreamlake-infra --json",
47
49
  "infra status": "dreamlake infra status da8e49cf-1b37-446f-9494-9620faf3192d --json",
48
50
  "infra connect": "dreamlake infra connect da8e49cf-1b37-446f-9494-9620faf3192d",
@@ -81,11 +83,11 @@ export const helpExamples = {
81
83
  "notes insert": "dreamlake notes insert --note \"$NOTE_ID\" --ind 0 --text 'Review: ' \\\n --base-revision \"$BASE\" --json\n# ACK is the revision returned by the insertion receipt.\ndreamlake notes read \"$NOTE_ID\" --at \"$ACK\"",
82
84
  "notes delete": "dreamlake notes delete 'obsolete paragraph' --note \"$NOTE_ID\" \\\n --base-revision \"$BASE\" --json\n# Pin verification to the receipt's acknowledged revision.\ndreamlake notes read \"$NOTE_ID\" --at \"$ACK\"",
83
85
  "notes write": "dreamlake notes read \"$NOTE_ID\" --json > baseline.json\nBASE=$(jq -er .revision baseline.json)\njq -jr .content baseline.json > draft.md\n# Edit draft.md without changing the saved original baseline.\ndreamlake notes write \"$NOTE_ID\" --file draft.md --base-revision \"$BASE\" --json\n# ACK is the successful write receipt's revision.\ndreamlake notes read \"$NOTE_ID\" --at \"$ACK\"",
84
- "notes": "# Find a note and read it directly.\ndreamlake notes search \"release plan\"\ndreamlake notes read release-plan\n\n# Preview a unique wording change, apply it, then read back.\ndreamlake notes replace \"Draft checklist\" --text \"Ready for review\" --note release-plan --dry-run\ndreamlake notes replace \"Draft checklist\" --text \"Ready for review\" --note release-plan\ndreamlake notes read release-plan\n\n# Create a private note from a Markdown file.\ndreamlake notes create \"Release plan\" --file release-plan.md",
86
+ "notes": "# Find a note and read it directly.\ndreamlake notes search \"release plan\"\ndreamlake notes read release-plan\n\n# Preview a unique wording change, apply it, then read back.\ndreamlake notes replace \"Draft checklist\" --text \"Ready for review\" --note release-plan --dry-run\ndreamlake notes replace \"Draft checklist\" --text \"Ready for review\" --note release-plan\ndreamlake notes read release-plan\n\n# Create a private note from a Markdown file.\ndreamlake notes create \"Release plan\" --file release-plan.md\n\n# Cross-interface design (unreleased target APIs; verify installed --help):\n# https://docs.dreamlake.ai/notes/api/",
85
87
  "notes list": "dreamlake notes list --limit 10\ndreamlake notes list --shared\ndreamlake notes list --namespace acme",
86
88
  "notes search": "dreamlake notes search \"release plan\"\ndreamlake notes search deploy --namespace acme --json",
87
89
  "notes create": "dreamlake notes create \"Release plan\" --text \"Draft checklist\"\nprintf '# Release plan\\n' | dreamlake notes create \"Release plan\" --file - --json",
88
- "notes read": "dreamlake notes read release-plan\ndreamlake notes read release-plan --since \"10 minutes ago\" --format diff\n# Reuse a stable agent identity for this task.\nexport DREAMLAKE_AGENT_ID=release-reviewer-a\nexport DREAMLAKE_AGENT_NAME=\"Release reviewer A\"\ndreamlake notes read release-plan --linger\n# Ctrl-C stops following.\n\nExample notifications:\n+ @alice joined\n+ Reviewer (agent) joined\n* Reviewer (agent) read the note\n* Reviewer (agent) edited the note\n* @alice selected \"## The center\"\n- @alice left",
90
+ "notes read": "dreamlake notes read release-plan\n# Inspect source with generated address hints; do not write the hints back.\ndreamlake notes read release-plan --view markdown\n# Preserve exact source plus revision/hash for a programmatic edit baseline.\ndreamlake notes read release-plan --json > baseline.json\ndreamlake notes read release-plan --since \"10 minutes ago\" --format diff\n# Detailed read design (unreleased target syntax):\n# https://docs.dreamlake.ai/notes/api/reading/\n# Reuse a stable agent identity for this task.\nexport DREAMLAKE_AGENT_ID=release-reviewer-a\nexport DREAMLAKE_AGENT_NAME=\"Release reviewer A\"\ndreamlake notes read release-plan --linger\n# Ctrl-C stops following.\n\nExample notifications:\n+ @alice joined\n+ Reviewer (agent) joined\n* Reviewer (agent) read the note\n* Reviewer (agent) edited the note\n* @alice selected \"## The center\"\n- @alice left",
89
91
  "notes projects": "dreamlake notes projects 000000000000000000000001 --namespace acme",
90
92
  "notes move": "# Standalone cross-namespace Note transfer is unavailable (exit 3).\ndreamlake notes move NOTE_ID --to-namespace my-org --project shared-research --dry-run\n# Add a project association within the note's CURRENT workspace.\ndreamlake notes move NOTE_ID --to-namespace current-workspace --project research --dry-run\n# Apply only an executable same-workspace preview.\ndreamlake notes move NOTE_ID --to-namespace current-workspace --project research\n# Relocate the note to an existing folder of one project (CLI 0.87.0+).\ndreamlake notes move NOTE_ID --namespace acme --project research --folder reports/2026 --dry-run",
91
93
  "notes diff": "NOTE_ID=release-plan\ndreamlake notes read \"$NOTE_ID\" --json > baseline.json\nHASH=$(jq -er .hash baseline.json)\ndreamlake notes diff \"$NOTE_ID\" --since \"$HASH\" --format diff",
@@ -190,6 +192,7 @@ export const helpExamples = {
190
192
  "skill remove": "dreamlake skill remove dreamlake-notes\ndreamlake skill remove dreamlake-notes --agent codex --global --json",
191
193
  "vault cloud import": "# No --file: list standard candidate paths without opening their contents.\ndreamlake vault cloud import --provider aws -p alice/aws/dev\n# Explicit selections: preview names, sizes and profile metadata, without upload.\ndreamlake vault cloud import --provider aws -p alice/aws/dev \\\n --file \"$HOME/.aws/config\" --file \"$HOME/.aws/credentials\"\n# Repeat the selection with --apply to upload.\ndreamlake vault cloud import --provider aws -p alice/aws/dev \\\n --file \"$HOME/.aws/config\" --file \"$HOME/.aws/credentials\" --apply\ndreamlake vault cloud import --provider gcp -p alice/gcp/dev \\\n --file /private/config/service-account.json --apply",
192
194
  "vault cloud restore": "# Preview checks metadata and destination without retrieving the secret.\ndreamlake vault cloud restore -n alice/aws/dev/credentials \\\n --to '~/.aws/credentials'\n# Write only after explicit --apply; existing files additionally need --overwrite.\ndreamlake vault cloud restore -n alice/aws/dev/credentials \\\n --to '~/.aws/credentials' --apply",
195
+ "vault add": "# Keep a 90-day registry token for exactly as long as the registry honors it.\npbpaste | dreamlake vault add -p alice -n cli/npm/access-token --stdin --expires-in 90d --env NPM_TOKEN\n# An exact UTC timestamp works the same way.\npbpaste | dreamlake vault add -p alice -n cli/npm/access-token --stdin --expires-at 2027-01-24T00:00:00Z\n# Replace a value and clear its expiry, pinning the current revision.\npbpaste | dreamlake vault add -p alice -n cli/npm/access-token --stdin --if-match 2 --no-expiry",
193
196
  "vault list": "# Interactive on a terminal; JSON when piped or redirected.\ndreamlake vault list -p alice\n# Always emit metadata JSON.\ndreamlake vault list -p alice --to-json",
194
197
  "vault delete": "# Use a disposable entry and its current metadata revision.\ndreamlake vault --scope org:000000000000000000000001 delete \\\n --name acme/test-entry --if-match 1",
195
198
  "vault terraform backend": "chmod 600 ./backend-reference.json\ndreamlake vault terraform backend --file ./backend-reference.json --name alice/terraform/aws-demo\n# Upload only after explicitly selecting the file and destination.\ndreamlake vault terraform backend --file ./backend-reference.json --name alice/terraform/aws-demo --apply --request-id aws-demo-backend-001",
@@ -39,8 +39,12 @@ export async function hostRequest(path, method = "GET", body, connection) {
39
39
  const data = await result.json().catch(() => ({}));
40
40
  const operationId = typeof data.operationId === "string" && /^[a-f0-9]{24}$/i.test(data.operationId) ? data.operationId : undefined;
41
41
  const retryAt = typeof data.retryAt === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(data.retryAt) && Number.isFinite(Date.parse(data.retryAt)) ? data.retryAt : undefined;
42
- const code = typeof data.code === "string" && SAFE_POWER_CODES.has(data.code) ? data.code : undefined;
43
- throw new HostApiError(`Host API request failed (HTTP ${result.status})${code ? `: ${code}` : ""}`, result.status, operationId, retryAt, code);
42
+ // A busy control plane refused the write before running it, so the caller may resubmit after Retry-After.
43
+ const busy = result.status === 503 && data.error === "controlplane_busy";
44
+ const code = busy ? "controlplane_busy" : typeof data.code === "string" && SAFE_POWER_CODES.has(data.code) ? data.code : undefined;
45
+ const retryAfter = Number(result.headers.get("retry-after"));
46
+ const busyRetryAt = busy && Number.isFinite(retryAfter) && retryAfter >= 0 ? new Date(Date.now() + retryAfter * 1000).toISOString() : undefined;
47
+ throw new HostApiError(`Host API request failed (HTTP ${result.status})${code ? `: ${code}` : ""}`, result.status, operationId, retryAt ?? busyRetryAt, code);
44
48
  }
45
49
  try {
46
50
  return await result.json();
@@ -1 +1 @@
1
- export declare const BOOTSTRAP_PYTHON = "import base64, fcntl, pwd, hashlib, json, os, pathlib, shutil, subprocess, sys, tempfile, re, urllib.request, plistlib\n\ndef run(args, **kwargs):\n return subprocess.run(args, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)\ndef prerequisite_error(code):\n print(json.dumps({'code': code}))\n sys.exit(1)\ndef openssl_binary():\n # Apple's bundled LibreSSL does not reliably support Ed25519 DER keys.\n candidates = ([str(pathlib.Path.home() / '.local/bin/openssl'),\n '/opt/homebrew/opt/openssl@3/bin/openssl',\n '/usr/local/opt/openssl@3/bin/openssl'] if sys.platform == 'darwin' else [])\n candidates.append(shutil.which('openssl'))\n for binary in candidates:\n if not binary or not os.access(binary, os.X_OK):\n continue\n try:\n result = run([binary, 'pkey', '-inform', 'DER', '-pubout', '-outform', 'DER'],\n input=bytes.fromhex('302e020100300506032b657004220420') + bytes(32)).stdout\n if len(result) == 44 and result[:12] == bytes.fromhex('302a300506032b6570032100'):\n return binary\n except Exception:\n pass\n prerequisite_error('openssl_ed25519_required')\n\ndef check_prerequisites():\n openssl = openssl_binary()\n if sys.platform == 'darwin':\n if os.getuid() == 0:\n prerequisite_error('macos_user_required')\n try:\n run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception:\n prerequisite_error('launchd_gui_unavailable')\n return openssl\n if sys.platform != 'linux':\n prerequisite_error('platform_unsupported')\n try:\n run(['systemctl', '--user', 'show-environment'])\n except Exception:\n prerequisite_error('systemd_user_unavailable')\n try:\n linger = run(['loginctl', 'show-user', pwd.getpwuid(os.getuid()).pw_name, '-p', 'Linger', '--value']).stdout.strip()\n except Exception:\n prerequisite_error('linger_unavailable')\n if linger != b'yes':\n prerequisite_error('linger_required')\n return openssl\ndef write(path, value):\n fd, tmp = tempfile.mkstemp(dir=str(path.parent))\n try:\n os.fchmod(fd, 0o600)\n with os.fdopen(fd, 'w') as f: f.write(value)\n os.replace(tmp, path)\n finally:\n if os.path.exists(tmp): os.unlink(tmp)\ndef nymph_binary(p, home):\n binary = shutil.which('nymph')\n if not binary:\n bindir = home / '.local/bin'\n bindir.mkdir(parents=True, exist_ok=True)\n binary = str(bindir / 'nymph')\n if not pathlib.Path(binary).exists():\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # Published immutable release directories use v-prefixed tags.\n version = p.get('version', 'latest')\n if re.fullmatch(r'\\d+\\.\\d+\\.\\d+(?:-[A-Za-z0-9.-]+)?', version):\n version = 'v' + version\n run(['bash','-s','--','--dest',str(bindir),'--version',version], input=installer)\n return binary\n\ndef main():\n p = json.load(sys.stdin)\n # Check before identity creation or requesting an enrollment grant.\n openssl = check_prerequisites()\n home = pathlib.Path.home()\n root = home / '.local/state/dreamlake/hosts' / hashlib.sha256(p['name'].encode()).hexdigest()\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n os.chmod(root, 0o700)\n with open(root / 'lock', 'a') as lock:\n fcntl.flock(lock, fcntl.LOCK_EX)\n if p.get('managedAccess'):\n binary = nymph_binary(p, home)\n try: run([binary, 'access', '--help'])\n except Exception: prerequisite_error('nymph_access_version_required')\n if (root / 'nymph.toml').exists() and not (root / 'access.json').exists():\n prerequisite_error('nymph_access_migration_required')\n key = root / 'identity.key'\n if not key.exists(): write(key, base64.urlsafe_b64encode(os.urandom(32)).decode().rstrip('='))\n os.chmod(key, 0o600)\n raw = base64.urlsafe_b64decode(key.read_text().strip() + '==')\n if len(raw) != 32: raise RuntimeError('identity format')\n public = run([openssl,'pkey','-inform','DER','-pubout','-outform','DER'], input=bytes.fromhex('302e020100300506032b657004220420') + raw).stdout\n if len(public) != 44 or public[:12] != bytes.fromhex('302a300506032b6570032100'): raise RuntimeError('public format')\n public_key = base64.urlsafe_b64encode(public[12:]).decode().rstrip('=')\n if p['action'] == 'probe':\n print(json.dumps({'unixUser':pwd.getpwuid(os.getuid()).pw_name,'publicKey':public_key}))\n return\n if public_key != p['publicKey']: raise RuntimeError('identity changed')\n # Never fall back to a privileged system unit or an unsupervised daemon.\n binary = nymph_binary(p, home)\n web_terminal = p.get('webTerminal', True)\n if not isinstance(web_terminal, bool): raise RuntimeError('invalid web terminal option')\n sshd = '/usr/sbin/sshd'\n if web_terminal:\n if not os.access(sshd, os.X_OK): prerequisite_error('sshd_required')\n try: run([sshd, '-V'])\n except Exception: prerequisite_error('sshd_required')\n version = run([binary, '--version']).stdout.decode('utf8', errors='replace')\n match = re.search(r'\\b(\\d+)\\.(\\d+)\\.(\\d+)\\b', version)\n if not match or tuple(map(int, match.groups())) < (0, 1, 11):\n prerequisite_error('nymph_ssh_version_required')\n if sys.platform == 'linux':\n privilege_dir = pathlib.Path('/run/sshd')\n if not privilege_dir.is_dir() or privilege_dir.stat().st_uid != 0 or privilege_dir.stat().st_mode & 0o022:\n prerequisite_error('sshd_privilege_directory_required')\n (root / 'ssh').mkdir(exist_ok=True, mode=0o700)\n os.chmod(root / 'ssh', 0o700)\n grant = root / 'enroll-token'\n if p.get('token'): write(grant, p['token'])\n q = json.dumps\n config = '\\n'.join([\n '[server]', 'url = ' + q(p['controlPlaneUrl']), 'namespace = ' + q(p['namespace']),\n '[daemon]', 'machine_id = ' + q(p['machineId']), 'label = ' + q(p['name']),\n '[runtime]', 'keep_alive_s = -1', 'workdir = ' + q(str(root / 'work')), 'runners = [\"process\"]', 'default_runner = \"process\"',\n '[identity]', 'enabled = true', 'key_file = ' + q(str(key)), 'enroll_token_file = ' + q(str(grant)),\n ]) + '\\n'\n if p.get('managedAccess'):\n access = root / 'access.json'\n if not access.exists():\n write(access, json.dumps({'version': 1, 'scope': {'server': p['controlPlaneUrl'], 'namespace': p['namespace'], 'machine_id': p['machineId']}, 'permissions': {'upload': False, 'command_execution': False}}))\n config = 'access_file = ' + q(str(access)) + '\\n' + config\n if web_terminal:\n config += '\\n'.join(['[ssh]', 'enabled = true', 'state_dir = ' + q(str(root / 'ssh')),\n 'sshd = ' + q(sshd), 'max_duration_s = 3600']) + '\\n'\n (root / 'work').mkdir(exist_ok=True)\n config_file = root / 'nymph.toml'\n changed = not config_file.exists() or config_file.read_text() != config\n reload_marker = root / 'launchd-reload-required'\n if sys.platform == 'darwin' and changed:\n write(reload_marker, 'pending')\n write(config_file, config)\n service_path = str(home / '.local/bin') + (':/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' if sys.platform == 'darwin' else ':/usr/local/bin:/usr/bin:/bin')\n if sys.platform == 'darwin':\n label = 'ai.dreamlake.host.' + hashlib.sha256(p['name'].encode()).hexdigest()[:24]\n domain = 'gui/' + str(os.getuid())\n target = domain + '/' + label\n agents = home / 'Library/LaunchAgents'\n agents.mkdir(parents=True, exist_ok=True)\n agent_file = agents / (label + '.plist')\n agent = plistlib.dumps({\n 'Label': label,\n 'ProgramArguments': [binary, '--config', str(config_file)],\n 'EnvironmentVariables': {'PATH': service_path, 'HOME': str(home)},\n 'WorkingDirectory': str(root / 'work'),\n 'RunAtLoad': True,\n 'KeepAlive': True,\n 'ThrottleInterval': 3,\n # Daemon output can contain sensitive workloads: do not persist it.\n 'StandardOutPath': '/dev/null',\n 'StandardErrorPath': '/dev/null',\n }, sort_keys=True).decode('utf8')\n changed = changed or not agent_file.exists() or agent_file.read_text() != agent or reload_marker.exists()\n if changed:\n write(reload_marker, 'pending')\n # Unload before replacing a loaded definition. Never ignore bootout failure.\n loaded = subprocess.run(['launchctl', 'print', target], stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL).returncode == 0\n if loaded and changed:\n run(['launchctl', 'bootout', target])\n loaded = False\n write(agent_file, agent)\n run(['launchctl', 'enable', target])\n if not loaded:\n run(['launchctl', 'bootstrap', domain, str(agent_file)])\n # No -k: replay must not kill a healthy daemon or its active workloads.\n run(['launchctl', 'kickstart', target])\n run(['launchctl', 'print', target])\n if reload_marker.exists():\n reload_marker.unlink()\n else:\n # JSON string escaping covers quoted systemd arguments; percent must be escaped separately.\n unit_quote = lambda s: q(s.replace('%','%%'))\n unit_name = 'dreamlake-host-' + hashlib.sha256(p['name'].encode()).hexdigest()[:24] + '.service'\n unit_dir = home / '.config/systemd/user'\n unit_dir.mkdir(parents=True, exist_ok=True)\n unit = '[Unit]\\nDescription=DreamLake nymph host\\n[Service]\\nEnvironment=' + unit_quote('PATH=' + service_path) + '\\nExecStart=' + unit_quote(binary) + ' --config ' + unit_quote(str(config_file)) + '\\nRestart=on-failure\\nRestartSec=3\\n[Install]\\nWantedBy=default.target\\n'\n unit_file = unit_dir / unit_name\n changed = changed or not unit_file.exists() or unit_file.read_text() != unit\n write(unit_file, unit)\n run(['systemctl','--user','daemon-reload'])\n run(['systemctl','--user','enable',unit_name])\n run(['systemctl','--user','restart' if changed else 'start',unit_name])\n print(json.dumps({'started':True}))\ntry:\n main()\nexcept Exception:\n # Never forward child stdout/stderr or payloads: these can contain grants.\n print(json.dumps({'error':'Remote bootstrap failed. Verify Python 3, OpenSSL, the platform user-service manager, OpenSSL Ed25519 support, and nymph installation access.'}))\n sys.exit(1)\n";
1
+ export declare const BOOTSTRAP_PYTHON = "import base64, fcntl, pwd, hashlib, json, os, pathlib, shutil, stat, subprocess, sys, tempfile, time, re, urllib.request, plistlib\n\ndef run(args, **kwargs):\n return subprocess.run(args, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)\ndef prerequisite_error(code):\n print(json.dumps({'code': code}))\n sys.exit(1)\ndef openssl_binary():\n # Apple's bundled LibreSSL does not reliably support Ed25519 DER keys.\n candidates = ([str(pathlib.Path.home() / '.local/bin/openssl'),\n '/opt/homebrew/opt/openssl@3/bin/openssl',\n '/usr/local/opt/openssl@3/bin/openssl'] if sys.platform == 'darwin' else [])\n candidates.append(shutil.which('openssl'))\n for binary in candidates:\n if not binary or not os.access(binary, os.X_OK):\n continue\n try:\n result = run([binary, 'pkey', '-inform', 'DER', '-pubout', '-outform', 'DER'],\n input=bytes.fromhex('302e020100300506032b657004220420') + bytes(32)).stdout\n if len(result) == 44 and result[:12] == bytes.fromhex('302a300506032b6570032100'):\n return binary\n except Exception:\n pass\n prerequisite_error('openssl_ed25519_required')\n\ndef check_prerequisites():\n openssl = openssl_binary()\n if sys.platform == 'darwin':\n if os.getuid() == 0:\n prerequisite_error('macos_user_required')\n try:\n run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception:\n prerequisite_error('launchd_gui_unavailable')\n return openssl\n if sys.platform != 'linux':\n prerequisite_error('platform_unsupported')\n try:\n run(['systemctl', '--user', 'show-environment'])\n except Exception:\n prerequisite_error('systemd_user_unavailable')\n try:\n linger = run(['loginctl', 'show-user', pwd.getpwuid(os.getuid()).pw_name, '-p', 'Linger', '--value']).stdout.strip()\n except Exception:\n prerequisite_error('linger_unavailable')\n if linger != b'yes':\n prerequisite_error('linger_required')\n return openssl\ndef write(path, value):\n fd, tmp = tempfile.mkstemp(dir=str(path.parent))\n try:\n os.fchmod(fd, 0o600)\n with os.fdopen(fd, 'w') as f: f.write(value)\n os.replace(tmp, path)\n finally:\n if os.path.exists(tmp): os.unlink(tmp)\ndef nymph_binary(p, home):\n binary = shutil.which('nymph')\n if not binary:\n bindir = home / '.local/bin'\n bindir.mkdir(parents=True, exist_ok=True)\n binary = str(bindir / 'nymph')\n if not pathlib.Path(binary).exists():\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # Published immutable release directories use v-prefixed tags.\n version = p.get('version', 'latest')\n if re.fullmatch(r'\\d+\\.\\d+\\.\\d+(?:-[A-Za-z0-9.-]+)?', version):\n version = 'v' + version\n run(['bash','-s','--','--dest',str(bindir),'--version',version], input=installer)\n return binary\n\ndef service_names(name):\n digest = hashlib.sha256(name.encode()).hexdigest()[:24]\n return 'ai.dreamlake.host.' + digest, 'dreamlake-host-' + digest + '.service'\n\n# ---- Nymph upgrade, verification and rollback (`dreamlake hosts upgrade`) ----\n# Every action records itself in ~/.local/share/dreamlake/fleet-upgrades/<YYYYMMDD-NNN>/.\n# The restart that activates a new binary runs once, later, outside the session\n# that requested it: `dreamlake exec` sessions are relayed by nymph itself.\nUPGRADE_TOOL = 'dreamlake-hosts-upgrade'\nUPGRADE_ACTIONS = ('upgrade', 'rollback', 'verify', 'inspect')\nRESTART_GRACE = 90\nVERSION = r'\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?'\n\ndef fail(code, **extra):\n extra['code'] = code\n print(json.dumps(extra))\n sys.exit(1)\ndef sha256_file(path):\n digest = hashlib.sha256()\n with open(str(path), 'rb') as f:\n for chunk in iter(lambda: f.read(1 << 20), b''): digest.update(chunk)\n return digest.hexdigest()\ndef binary_version(binary):\n try: out = run([str(binary), '--version']).stdout.decode('utf8', errors='replace')\n except Exception: return None\n match = re.search(r'(?<![0-9.])(' + VERSION + ')', out)\n return match.group(1) if match else None\ndef bounded(value, default, low, high, code):\n value = default if value is None else value\n if not isinstance(value, int) or isinstance(value, bool) or not low <= value <= high: fail(code)\n return value\n\ndef leftover_restart_jobs(home, remove=True):\n \"\"\"Same rule as src/cli/hosts/leftover-jobs.ts (dreamlake-cli#407): only a submitted\n ai.dreamlake.refresh.* job running this user's fleet-upgrades/<id>/restart.py, which\n the 2026-10 fleet upgrade left behind and launchd re-ran forever. Nothing else.\"\"\"\n if sys.platform != 'darwin': return []\n script = re.compile(r'^\\s*' + re.escape(str(home)) + r'/\\.local/share/dreamlake/fleet-upgrades/[A-Za-z0-9._-]+/restart\\.py$', re.M)\n found = []\n try: listing = run(['launchctl', 'list']).stdout.decode('utf8', errors='replace')\n except Exception: return found\n for line in listing.split('\\n'):\n fields = line.split()\n label = fields[2] if len(fields) > 2 else ''\n if not re.fullmatch(r'ai\\.dreamlake\\.refresh\\.[A-Za-z0-9]+', label): continue\n try:\n detail = run(['launchctl', 'print', 'gui/%d/%s' % (os.getuid(), label)]).stdout.decode('utf8', errors='replace')\n if not re.search(r'^\\s*type = Submitted$', detail, re.M) or not script.search(detail): continue\n if remove: run(['launchctl', 'remove', label])\n found.append(label)\n except Exception:\n pass\n return found\n\ndef service_prerequisites():\n if sys.platform == 'darwin':\n if os.getuid() == 0: prerequisite_error('macos_user_required')\n try: run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception: prerequisite_error('launchd_gui_unavailable')\n elif sys.platform == 'linux':\n try: run(['systemctl', '--user', 'show-environment'])\n except Exception: prerequisite_error('systemd_user_unavailable')\n if not shutil.which('systemd-run'): prerequisite_error('systemd_run_required')\n else:\n prerequisite_error('platform_unsupported')\n\ndef installed_service(name, home):\n \"\"\"The service and binary that enrollment installed; never a guess from PATH.\"\"\"\n label, unit = service_names(name)\n if sys.platform == 'darwin':\n agent = home / 'Library/LaunchAgents' / (label + '.plist')\n if not agent.is_file(): fail('service_not_installed')\n with open(str(agent), 'rb') as f: binary = plistlib.load(f)['ProgramArguments'][0]\n service = {'kind': 'launchd', 'target': 'gui/%d/%s' % (os.getuid(), label)}\n else:\n unit_file = home / '.config/systemd/user' / unit\n if not unit_file.is_file(): fail('service_not_installed')\n lines = [l for l in unit_file.read_text().splitlines() if l.startswith('ExecStart=')]\n if len(lines) != 1: fail('service_not_installed')\n binary = json.JSONDecoder().raw_decode(lines[0][len('ExecStart='):])[0].replace('%%', '%')\n service = {'kind': 'systemd', 'unit': unit}\n service['binary'] = binary\n return service\n\ndef check_binary(binary):\n # Swap only a regular file this user owns in a directory it can write; never a\n # package-managed symlink or a root-owned install that would need sudo.\n try: st = os.lstat(str(binary))\n except OSError: fail('nymph_binary_missing', binary=str(binary))\n if not binary.is_absolute() or not stat.S_ISREG(st.st_mode) or st.st_uid != os.getuid() or not os.access(str(binary.parent), os.W_OK):\n fail('nymph_binary_unmanaged', binary=str(binary))\n\ndef service_state(service):\n if service['kind'] == 'launchd':\n result = subprocess.run(['launchctl', 'print', service['target']], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n if result.returncode != 0: return {'loaded': False, 'pid': None, 'runs': None}\n text = result.stdout.decode('utf8', errors='replace')\n pid, runs = re.search(r'^\\tpid = (\\d+)$', text, re.M), re.search(r'^\\truns = (\\d+)$', text, re.M)\n return {'loaded': True, 'pid': int(pid.group(1)) if pid else None, 'runs': int(runs.group(1)) if runs else None}\n result = subprocess.run(['systemctl', '--user', 'show', service['unit'], '-p', 'MainPID', '-p', 'InvocationID', '-p', 'NRestarts', '-p', 'ActiveState'],\n stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n props = dict(l.split('=', 1) for l in result.stdout.decode('utf8', errors='replace').splitlines() if '=' in l)\n pid = int(props.get('MainPID') or 0) or None\n return {'loaded': props.get('ActiveState') == 'active', 'pid': pid, 'invocation': props.get('InvocationID') or None,\n 'autoRestarts': int(props.get('NRestarts') or 0)}\n\ndef process_started_at(pid):\n try: out = run(['ps', '-o', 'etime=', '-p', str(pid)]).stdout.decode().strip()\n except Exception: return None\n match = re.fullmatch(r'(?:(\\d+)-)?(?:(\\d+):)?(\\d+):(\\d+)', out)\n if not match: return None\n days, hours, minutes, seconds = (int(x or 0) for x in match.groups())\n return time.time() - (((days * 24 + hours) * 60 + minutes) * 60 + seconds)\n\ndef running_current(state, binary):\n \"\"\"Whether the service process was started from the binary now on disk.\"\"\"\n pid = state.get('pid')\n if not pid: return False\n if sys.platform == 'linux':\n try:\n if os.readlink('/proc/%d/exe' % pid).endswith(' (deleted)'): return False\n except OSError: pass\n started = process_started_at(pid)\n # etime is whole (possibly rounded) seconds: demand a 2 s margin. An ambiguous\n # answer only costs one extra, verified restart.\n return started is not None and started - 2 >= os.stat(str(binary)).st_mtime\ndef verified_current(fleet, service, sha, pid):\n \"\"\"A recorded verification proves this PID came from the restart onto this binary.\"\"\"\n for d, data in reversed(receipts(fleet)):\n if data.get('binary') != service['binary'] or data.get('status') != 'restart_scheduled': continue\n try: result = json.loads((d / 'verification.json').read_text())\n except Exception: return False\n return data.get('sha256') == sha and result.get('status') == 'verified' and bool(pid) and (result.get('pid') or {}).get('after') == pid\n return False\n\ndef fleet_root(home):\n root = home / '.local/share/dreamlake/fleet-upgrades'\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n return root\ndef receipts(fleet):\n \"\"\"Receipts written by this tool, oldest first. Ad-hoc 2026-10 receipts are ignored.\"\"\"\n found = []\n for d in sorted(fleet.iterdir()):\n if not re.fullmatch(r'\\d{8}-\\d{3}', d.name) or not (d / 'receipt.json').is_file(): continue\n try: data = json.loads((d / 'receipt.json').read_text())\n except Exception: continue\n if isinstance(data, dict) and data.get('tool') == UPGRADE_TOOL: found.append((d, data))\n return found\ndef select_receipt(fleet, service, upgrade_id=None, rollback=False):\n for d, data in reversed(receipts(fleet)):\n if upgrade_id is not None and d.name != upgrade_id: continue\n if data.get('binary') != service['binary'] or data.get('status') != 'restart_scheduled': continue\n if rollback and not (d / 'previous-nymph').is_file(): continue\n return d, data\n fail('receipt_not_found')\ndef pending_restart(fleet):\n now = time.time()\n for d, data in receipts(fleet):\n at = (data.get('restart') or {}).get('at')\n if at and not (d / 'restart.started').exists() and now < at + RESTART_GRACE: return d.name\ndef new_action_dir(fleet):\n day = time.strftime('%Y%m%d')\n taken = [int(d.name[9:]) for d in fleet.iterdir() if re.fullmatch(day + r'-\\d{3}', d.name)]\n for n in range(max(taken + [0]) + 1, 1000):\n d = fleet / ('%s-%03d' % (day, n))\n try:\n d.mkdir(mode=0o700)\n return d\n except FileExistsError:\n continue\n raise RuntimeError('upgrade ids exhausted')\n\ndef swap_binary(source, binary, expected):\n \"\"\"Atomic replace in the binary's own directory; the old path never disappears.\"\"\"\n mode = stat.S_IMODE(os.stat(str(binary)).st_mode) | 0o500\n tmp = binary.parent / ('.nymph-upgrade-' + os.urandom(6).hex())\n try:\n shutil.copyfile(str(source), str(tmp))\n os.chmod(str(tmp), mode)\n with open(str(tmp), 'rb') as f: os.fsync(f.fileno())\n if sha256_file(tmp) != expected: raise RuntimeError('staged binary changed')\n os.replace(str(tmp), str(binary))\n finally:\n if tmp.exists(): tmp.unlink()\n\ndef shell_quote(value):\n return \"'\" + str(value).replace(\"'\", \"'\\\\''\") + \"'\"\ndef schedule_restart(service, d, delay):\n \"\"\"One delayed restart that outlives this session and can run at most once.\n restart.sh creates restart.started with noclobber before restarting; any second\n execution, for any reason, finds the marker and exits without restarting.\"\"\"\n launchctl, systemctl = shutil.which('launchctl'), shutil.which('systemctl')\n if service['kind'] == 'launchd':\n if not launchctl: raise RuntimeError('launchctl missing')\n command = [launchctl, 'kickstart', '-k', service['target']]\n read_pid = \"%s print %s | awk '/^\\\\tpid = / {print $3; exit}'\" % (shell_quote(launchctl), shell_quote(service['target']))\n else:\n if not systemctl: raise RuntimeError('systemctl missing')\n command = [systemctl, '--user', 'restart', service['unit']]\n read_pid = \"%s --user show %s -p MainPID | awk -F= '$1 == \\\"MainPID\\\" {print $2; exit}'\" % (shell_quote(systemctl), shell_quote(service['unit']))\n script = d / 'restart.sh'\n # restart.pid is the service PID right after this restart; verification requires the\n # final PID to equal it, which proves no later restart happened.\n write(script, '\\n'.join([\n '# One-shot nymph restart for %s; written by dreamlake hosts upgrade.' % d.name,\n 'if ! (set -C; : > %s) 2>/dev/null; then exit 0; fi' % shell_quote(d / 'restart.started'),\n 'date +%%s >> %s' % shell_quote(d / 'restart.log'),\n ' '.join(shell_quote(a) for a in command),\n 'status=$?',\n 'for attempt in 1 2 3 4 5 6 7 8 9 10; do pid=$(%s); [ -n \"$pid\" ] && [ \"$pid\" != 0 ] && break; sleep 1; done' % read_pid,\n 'echo \"$pid\" > %s' % shell_quote(d / 'restart.pid'),\n 'echo \"$status\" > %s' % shell_quote(d / 'restart.exit'), '']))\n at = time.time() + delay\n if service['kind'] == 'launchd':\n # A plain process in its own session (setsid): launchd never registers it, so\n # nothing can respawn it (no `launchctl submit`, no KeepAlive), and outside\n # nymph's process group neither the SSH session ending nor nymph's teardown\n # signals it before it runs.\n child = subprocess.Popen(['/bin/sh', '-c', 'sleep %d; exec /bin/sh %s' % (delay, shell_quote(script))],\n stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,\n close_fds=True, start_new_session=True, cwd='/')\n return {'mechanism': 'detached-process', 'pid': child.pid, 'at': at, 'delaySeconds': delay}\n # A transient timer in the user manager, outside nymph's cgroup (a child of nymph\n # would be killed by its own restart). No Restart= and an OnActiveSec timer that\n # unloads after elapsing: systemd starts the service once and then forgets both.\n unit = 'dreamlake-nymph-restart-%s-%s' % (d.name, os.urandom(3).hex())\n run([shutil.which('systemd-run'), '--user', '--quiet', '--collect', '--unit=' + unit, '--on-active=%ds' % delay,\n '--timer-property=AccuracySec=1s', '--timer-property=RemainAfterElapse=no', '/bin/sh', str(script)])\n return {'mechanism': 'systemd-run-timer', 'unit': unit, 'at': at, 'delaySeconds': delay}\n\ndef helper_finished(restart):\n if restart.get('mechanism') == 'detached-process':\n try: os.kill(restart['pid'], 0)\n except ProcessLookupError: return True\n except OSError: return False\n try: command = run(['ps', '-o', 'command=', '-p', str(restart['pid'])]).stdout.decode('utf8', errors='replace')\n except Exception: return True\n return 'restart.sh' not in command\n for suffix in ('.timer', '.service'):\n result = subprocess.run(['systemctl', '--user', 'show', restart['unit'] + suffix, '-p', 'ActiveState'],\n stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n if result.stdout.decode().strip() in ('ActiveState=active', 'ActiveState=activating'): return False\n return True\n\ndef apply_binary(fleet, service, binary, source, sha, receipt, delay):\n \"\"\"Keep the previous binary, swap atomically, record, then schedule the one restart.\"\"\"\n d = receipt['dir']\n shutil.copyfile(str(source), str(d / 'nymph'))\n shutil.copyfile(str(binary), str(d / 'previous-nymph'))\n for kept in ('nymph', 'previous-nymph'): os.chmod(str(d / kept), 0o700)\n if sha256_file(d / 'nymph') != sha or sha256_file(d / 'previous-nymph') != receipt['previousSha256']:\n raise RuntimeError('copied binary changed')\n del receipt['dir']\n receipt['status'] = 'swapping'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n swap_binary(d / 'nymph', binary, sha)\n try:\n receipt['restart'] = schedule_restart(service, d, delay)\n except Exception:\n swap_binary(d / 'previous-nymph', binary, receipt['previousSha256'])\n receipt['status'] = 'reverted'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n fail('restart_schedule_failed', upgradeId=d.name)\n receipt['status'] = 'restart_scheduled'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n return d\n\ndef restart_only(fleet, service, binary, receipt, delay):\n d = receipt.pop('dir')\n receipt['status'] = 'restart_scheduled'\n receipt['restart'] = schedule_restart(service, d, delay)\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n return d\n\ndef scheduled(d, receipt, removed):\n return {'status': 'restart_scheduled', 'action': receipt['action'], 'upgradeId': d.name, 'version': receipt['version'],\n 'previousVersion': receipt.get('previousVersion'), 'sha256': receipt['sha256'], 'previousSha256': receipt.get('previousSha256'),\n 'restart': {k: receipt['restart'][k] for k in ('mechanism', 'at', 'delaySeconds')}, 'before': receipt['before'],\n 'leftoverJobsRemoved': removed}\n\ndef verify(fleet, service, binary, p):\n d, receipt = select_receipt(fleet, service, p.get('upgradeId'))\n stable = bounded(p.get('stableSeconds'), 15, 1, 300, 'invalid_stable_seconds')\n restart, before = receipt.get('restart') or {}, receipt.get('before') or {}\n deadline = (restart.get('at') or 0) + RESTART_GRACE\n while time.time() < deadline and not (d / 'restart.exit').exists(): time.sleep(0.5)\n state = service_state(service)\n while time.time() < deadline and (not state.get('pid') or state.get('pid') == before.get('pid')):\n time.sleep(1)\n state = service_state(service)\n samples, end = [state], time.time() + stable\n while time.time() < end:\n time.sleep(1)\n samples.append(service_state(service))\n after = samples[-1]\n log = d / 'restart.log'\n helper_runs = len(log.read_text().split()) if log.exists() else 0\n exit_status = (d / 'restart.exit').read_text().strip() if (d / 'restart.exit').exists() else None\n restart_pid = (d / 'restart.pid').read_text().strip() if (d / 'restart.pid').exists() else ''\n checks = {\n 'restartRan': (d / 'restart.started').exists(),\n 'restartRuns': helper_runs,\n 'restartExit': int(exit_status) if exit_status and exit_status.isdigit() else None,\n 'pidChanged': bool(after.get('pid')) and after.get('pid') != before.get('pid'),\n 'pidStable': len({s.get('pid') for s in samples}) == 1 and bool(after.get('pid')),\n 'restartPid': int(restart_pid) if restart_pid.isdigit() else None,\n 'helperFinished': helper_finished(restart) if restart else False,\n 'binarySha256': sha256_file(binary) == receipt['sha256'],\n 'version': binary_version(binary),\n # Informational: the PID chain (pidFromRestart) is the proof; ps start times are coarse.\n 'processCurrent': running_current(after, binary),\n 'leftoverJobsRemoved': leftover_restart_jobs(pathlib.Path.home()),\n }\n if service['kind'] == 'launchd' and before.get('runs') is not None and after.get('runs') is not None:\n checks['serviceStarts'] = after['runs'] - before['runs']\n if service['kind'] == 'systemd':\n checks['invocationChanged'] = bool(after.get('invocation')) and after.get('invocation') != before.get('invocation')\n # An explicit systemctl restart resets NRestarts. Only the new\n # activation's count applies; subtracting the previous activation can\n # reject a healthy restart or hide new crashes behind an old count.\n checks['autoRestarts'] = after.get('autoRestarts', 0)\n failures = [k for k, ok in [\n ('restartRan', checks['restartRan']), ('restartRuns', helper_runs == 1), ('restartExit', checks['restartExit'] == 0),\n ('pidChanged', checks['pidChanged']), ('pidStable', checks['pidStable']),\n ('pidFromRestart', checks['restartPid'] is not None and checks['restartPid'] == after.get('pid')),\n ('helperFinished', checks['helperFinished']),\n ('binarySha256', checks['binarySha256']), ('version', checks['version'] == receipt['version']),\n ('serviceStarts', checks.get('serviceStarts', 1) == 1),\n ('invocationChanged', checks.get('invocationChanged', True)), ('autoRestarts', checks.get('autoRestarts', 0) == 0)] if not ok]\n result = {'status': 'failed' if failures else 'verified', 'action': receipt['action'], 'upgradeId': d.name,\n 'version': checks['version'], 'expectedVersion': receipt['version'],\n 'pid': {'before': before.get('pid'), 'after': after.get('pid')}, 'restarts': helper_runs,\n 'stableSeconds': stable, 'checks': checks, 'failures': failures}\n write(d / 'verification.json', json.dumps(result, sort_keys=True))\n print(json.dumps(result))\n if failures: sys.exit(1)\n\ndef inspect(fleet, service, binary, p):\n state = service_state(service)\n version = binary_version(binary)\n wanted = p.get('version')\n sha = sha256_file(binary)\n current = verified_current(fleet, service, sha, state.get('pid')) or running_current(state, binary)\n pending = pending_restart(fleet)\n rollback = next((d.name for d, data in reversed(receipts(fleet)) if data.get('binary') == service['binary']\n and data.get('status') == 'restart_scheduled' and (d / 'previous-nymph').is_file()), None)\n plan = None\n if isinstance(wanted, str):\n wanted = wanted[1:] if wanted.startswith('v') else wanted\n plan = 'blocked:restart_pending' if pending else 'current' if version == wanted and current else 'restart' if version == wanted else 'upgrade'\n print(json.dumps({'status': 'inspected', 'service': {k: v for k, v in service.items() if k != 'binary'}, 'binary': service['binary'],\n 'version': version, 'sha256': sha, 'pid': state.get('pid'), 'processCurrent': current,\n 'pendingRestart': pending, 'rollbackAvailable': rollback, 'plan': plan,\n 'leftoverJobs': leftover_restart_jobs(pathlib.Path.home(), remove=False)}))\n\ndef upgrade_main(p):\n action = p['action']\n if not isinstance(p.get('name'), str) or not p['name']: fail('invalid_request')\n service_prerequisites()\n home = pathlib.Path.home()\n fleet = fleet_root(home)\n service = installed_service(p['name'], home)\n binary = pathlib.Path(service['binary'])\n check_binary(binary)\n if action == 'inspect': return inspect(fleet, service, binary, p)\n with open(str(fleet / '.lock'), 'a') as lock:\n try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)\n except OSError: fail('upgrade_in_progress')\n if action == 'verify': return verify(fleet, service, binary, p)\n delay = bounded(p.get('restartDelaySeconds'), 15, 1, 600, 'invalid_restart_delay')\n removed = leftover_restart_jobs(home)\n pending = pending_restart(fleet)\n if pending: fail('restart_pending', upgradeId=pending)\n current_sha, current_version = sha256_file(binary), binary_version(binary)\n state = service_state(service)\n base = {'tool': UPGRADE_TOOL, 'schema': 1, 'name': p['name'], 'binary': service['binary'],\n 'service': {k: v for k, v in service.items() if k != 'binary'}, 'previousVersion': current_version,\n 'previousSha256': current_sha, 'before': state, 'leftoverJobsRemoved': removed, 'at': time.time()}\n if action == 'rollback':\n source_dir, source = select_receipt(fleet, service, p.get('upgradeId'), rollback=True)\n if current_sha != source.get('sha256'): fail('rollback_receipt_mismatch', upgradeId=source_dir.name)\n previous = source_dir / 'previous-nymph'\n if sha256_file(previous) != source.get('previousSha256'): fail('rollback_binary_corrupt', upgradeId=source_dir.name)\n receipt = dict(base, action='rollback', rollbackOf=source_dir.name, version=source.get('previousVersion') or binary_version(previous),\n sha256=source['previousSha256'], dir=new_action_dir(fleet))\n d = apply_binary(fleet, service, binary, previous, receipt['sha256'], receipt, delay)\n print(json.dumps(scheduled(d, receipt, removed)))\n return\n version = p.get('version')\n if not isinstance(version, str) or not re.fullmatch('v?' + VERSION, version): fail('invalid_version')\n version = version[1:] if version.startswith('v') else version\n if current_version == version:\n if verified_current(fleet, service, current_sha, state.get('pid')) or running_current(state, binary):\n print(json.dumps({'status': 'current', 'version': version, 'sha256': current_sha, 'pid': state.get('pid'),\n 'leftoverJobsRemoved': removed}))\n return\n # Already swapped but the service still runs an older process: restart only.\n receipt = dict(base, action='restart', version=version, sha256=current_sha, dir=new_action_dir(fleet))\n d = restart_only(fleet, service, binary, receipt, delay)\n print(json.dumps(scheduled(d, receipt, removed)))\n return\n d = new_action_dir(fleet)\n try:\n stage = d / 'stage'\n stage.mkdir(mode=0o700)\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # The official installer downloads the archive and verifies its published SHA-256.\n run(['bash', '-s', '--', '--dest', str(stage), '--version', 'v' + version], input=installer)\n staged = stage / 'nymph'\n if not staged.is_file(): fail('installer_failed')\n found = binary_version(staged)\n if found != version: fail('installer_version_mismatch', expected=version, found=found)\n sha = sha256_file(staged)\n receipt = dict(base, action='upgrade', version=version, sha256=sha, dir=d)\n apply_binary(fleet, service, binary, staged, sha, receipt, delay)\n shutil.rmtree(str(stage), ignore_errors=True)\n except BaseException:\n if not (d / 'receipt.json').exists(): shutil.rmtree(str(d), ignore_errors=True)\n raise\n print(json.dumps(scheduled(d, receipt, removed)))\n\ndef main():\n p = json.load(sys.stdin)\n if p.get('action') in UPGRADE_ACTIONS:\n try:\n return upgrade_main(p)\n except SystemExit:\n raise\n except Exception:\n fail('upgrade_failed')\n # Check before identity creation or requesting an enrollment grant.\n openssl = check_prerequisites()\n home = pathlib.Path.home()\n root = home / '.local/state/dreamlake/hosts' / hashlib.sha256(p['name'].encode()).hexdigest()\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n os.chmod(root, 0o700)\n with open(root / 'lock', 'a') as lock:\n fcntl.flock(lock, fcntl.LOCK_EX)\n if p.get('managedAccess'):\n binary = nymph_binary(p, home)\n try: run([binary, 'access', '--help'])\n except Exception: prerequisite_error('nymph_access_version_required')\n if (root / 'nymph.toml').exists() and not (root / 'access.json').exists():\n prerequisite_error('nymph_access_migration_required')\n key = root / 'identity.key'\n if not key.exists(): write(key, base64.urlsafe_b64encode(os.urandom(32)).decode().rstrip('='))\n os.chmod(key, 0o600)\n raw = base64.urlsafe_b64decode(key.read_text().strip() + '==')\n if len(raw) != 32: raise RuntimeError('identity format')\n public = run([openssl,'pkey','-inform','DER','-pubout','-outform','DER'], input=bytes.fromhex('302e020100300506032b657004220420') + raw).stdout\n if len(public) != 44 or public[:12] != bytes.fromhex('302a300506032b6570032100'): raise RuntimeError('public format')\n public_key = base64.urlsafe_b64encode(public[12:]).decode().rstrip('=')\n if p['action'] == 'probe':\n print(json.dumps({'unixUser':pwd.getpwuid(os.getuid()).pw_name,'publicKey':public_key}))\n return\n if public_key != p['publicKey']: raise RuntimeError('identity changed')\n # Never fall back to a privileged system unit or an unsupervised daemon.\n binary = nymph_binary(p, home)\n web_terminal = p.get('webTerminal', True)\n if not isinstance(web_terminal, bool): raise RuntimeError('invalid web terminal option')\n sshd = '/usr/sbin/sshd'\n if web_terminal:\n if not os.access(sshd, os.X_OK): prerequisite_error('sshd_required')\n try: run([sshd, '-V'])\n except Exception: prerequisite_error('sshd_required')\n version = run([binary, '--version']).stdout.decode('utf8', errors='replace')\n match = re.search(r'\\b(\\d+)\\.(\\d+)\\.(\\d+)\\b', version)\n if not match or tuple(map(int, match.groups())) < (0, 1, 11):\n prerequisite_error('nymph_ssh_version_required')\n if sys.platform == 'linux':\n privilege_dir = pathlib.Path('/run/sshd')\n if not privilege_dir.is_dir() or privilege_dir.stat().st_uid != 0 or privilege_dir.stat().st_mode & 0o022:\n prerequisite_error('sshd_privilege_directory_required')\n (root / 'ssh').mkdir(exist_ok=True, mode=0o700)\n os.chmod(root / 'ssh', 0o700)\n grant = root / 'enroll-token'\n if p.get('token'): write(grant, p['token'])\n q = json.dumps\n config = '\\n'.join([\n '[server]', 'url = ' + q(p['controlPlaneUrl']), 'namespace = ' + q(p['namespace']),\n '[daemon]', 'machine_id = ' + q(p['machineId']), 'label = ' + q(p['name']),\n '[runtime]', 'keep_alive_s = -1', 'workdir = ' + q(str(root / 'work')), 'runners = [\"process\"]', 'default_runner = \"process\"',\n '[identity]', 'enabled = true', 'key_file = ' + q(str(key)), 'enroll_token_file = ' + q(str(grant)),\n ]) + '\\n'\n if p.get('managedAccess'):\n access = root / 'access.json'\n if not access.exists():\n write(access, json.dumps({'version': 1, 'scope': {'server': p['controlPlaneUrl'], 'namespace': p['namespace'], 'machine_id': p['machineId']}, 'permissions': {'upload': False, 'command_execution': False}}))\n config = 'access_file = ' + q(str(access)) + '\\n' + config\n if web_terminal:\n config += '\\n'.join(['[ssh]', 'enabled = true', 'state_dir = ' + q(str(root / 'ssh')),\n 'sshd = ' + q(sshd), 'max_duration_s = 3600']) + '\\n'\n (root / 'work').mkdir(exist_ok=True)\n config_file = root / 'nymph.toml'\n changed = not config_file.exists() or config_file.read_text() != config\n reload_marker = root / 'launchd-reload-required'\n if sys.platform == 'darwin' and changed:\n write(reload_marker, 'pending')\n write(config_file, config)\n service_path = str(home / '.local/bin') + (':/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' if sys.platform == 'darwin' else ':/usr/local/bin:/usr/bin:/bin')\n if sys.platform == 'darwin':\n label = service_names(p['name'])[0]\n domain = 'gui/' + str(os.getuid())\n target = domain + '/' + label\n agents = home / 'Library/LaunchAgents'\n agents.mkdir(parents=True, exist_ok=True)\n agent_file = agents / (label + '.plist')\n agent = plistlib.dumps({\n 'Label': label,\n 'ProgramArguments': [binary, '--config', str(config_file)],\n 'EnvironmentVariables': {'PATH': service_path, 'HOME': str(home)},\n 'WorkingDirectory': str(root / 'work'),\n 'RunAtLoad': True,\n 'KeepAlive': True,\n 'ThrottleInterval': 3,\n # Daemon output can contain sensitive workloads: do not persist it.\n 'StandardOutPath': '/dev/null',\n 'StandardErrorPath': '/dev/null',\n }, sort_keys=True).decode('utf8')\n changed = changed or not agent_file.exists() or agent_file.read_text() != agent or reload_marker.exists()\n if changed:\n write(reload_marker, 'pending')\n # Unload before replacing a loaded definition. Never ignore bootout failure.\n loaded = subprocess.run(['launchctl', 'print', target], stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL).returncode == 0\n if loaded and changed:\n run(['launchctl', 'bootout', target])\n loaded = False\n write(agent_file, agent)\n run(['launchctl', 'enable', target])\n if not loaded:\n run(['launchctl', 'bootstrap', domain, str(agent_file)])\n # No -k: replay must not kill a healthy daemon or its active workloads.\n run(['launchctl', 'kickstart', target])\n run(['launchctl', 'print', target])\n if reload_marker.exists():\n reload_marker.unlink()\n else:\n # JSON string escaping covers quoted systemd arguments; percent must be escaped separately.\n unit_quote = lambda s: q(s.replace('%','%%'))\n unit_name = service_names(p['name'])[1]\n unit_dir = home / '.config/systemd/user'\n unit_dir.mkdir(parents=True, exist_ok=True)\n unit = '[Unit]\\nDescription=DreamLake nymph host\\n[Service]\\nEnvironment=' + unit_quote('PATH=' + service_path) + '\\nExecStart=' + unit_quote(binary) + ' --config ' + unit_quote(str(config_file)) + '\\nRestart=on-failure\\nRestartSec=3\\n[Install]\\nWantedBy=default.target\\n'\n unit_file = unit_dir / unit_name\n changed = changed or not unit_file.exists() or unit_file.read_text() != unit\n write(unit_file, unit)\n run(['systemctl','--user','daemon-reload'])\n run(['systemctl','--user','enable',unit_name])\n run(['systemctl','--user','restart' if changed else 'start',unit_name])\n print(json.dumps({'started':True}))\ntry:\n main()\nexcept Exception:\n # Never forward child stdout/stderr or payloads: these can contain grants.\n print(json.dumps({'error':'Remote bootstrap failed. Verify Python 3, OpenSSL, the platform user-service manager, OpenSSL Ed25519 support, and nymph installation access.'}))\n sys.exit(1)\n";
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/gen-host-bootstrap.mjs from bootstrap.py.
2
- export const BOOTSTRAP_PYTHON = "import base64, fcntl, pwd, hashlib, json, os, pathlib, shutil, subprocess, sys, tempfile, re, urllib.request, plistlib\n\ndef run(args, **kwargs):\n return subprocess.run(args, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)\ndef prerequisite_error(code):\n print(json.dumps({'code': code}))\n sys.exit(1)\ndef openssl_binary():\n # Apple's bundled LibreSSL does not reliably support Ed25519 DER keys.\n candidates = ([str(pathlib.Path.home() / '.local/bin/openssl'),\n '/opt/homebrew/opt/openssl@3/bin/openssl',\n '/usr/local/opt/openssl@3/bin/openssl'] if sys.platform == 'darwin' else [])\n candidates.append(shutil.which('openssl'))\n for binary in candidates:\n if not binary or not os.access(binary, os.X_OK):\n continue\n try:\n result = run([binary, 'pkey', '-inform', 'DER', '-pubout', '-outform', 'DER'],\n input=bytes.fromhex('302e020100300506032b657004220420') + bytes(32)).stdout\n if len(result) == 44 and result[:12] == bytes.fromhex('302a300506032b6570032100'):\n return binary\n except Exception:\n pass\n prerequisite_error('openssl_ed25519_required')\n\ndef check_prerequisites():\n openssl = openssl_binary()\n if sys.platform == 'darwin':\n if os.getuid() == 0:\n prerequisite_error('macos_user_required')\n try:\n run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception:\n prerequisite_error('launchd_gui_unavailable')\n return openssl\n if sys.platform != 'linux':\n prerequisite_error('platform_unsupported')\n try:\n run(['systemctl', '--user', 'show-environment'])\n except Exception:\n prerequisite_error('systemd_user_unavailable')\n try:\n linger = run(['loginctl', 'show-user', pwd.getpwuid(os.getuid()).pw_name, '-p', 'Linger', '--value']).stdout.strip()\n except Exception:\n prerequisite_error('linger_unavailable')\n if linger != b'yes':\n prerequisite_error('linger_required')\n return openssl\ndef write(path, value):\n fd, tmp = tempfile.mkstemp(dir=str(path.parent))\n try:\n os.fchmod(fd, 0o600)\n with os.fdopen(fd, 'w') as f: f.write(value)\n os.replace(tmp, path)\n finally:\n if os.path.exists(tmp): os.unlink(tmp)\ndef nymph_binary(p, home):\n binary = shutil.which('nymph')\n if not binary:\n bindir = home / '.local/bin'\n bindir.mkdir(parents=True, exist_ok=True)\n binary = str(bindir / 'nymph')\n if not pathlib.Path(binary).exists():\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # Published immutable release directories use v-prefixed tags.\n version = p.get('version', 'latest')\n if re.fullmatch(r'\\d+\\.\\d+\\.\\d+(?:-[A-Za-z0-9.-]+)?', version):\n version = 'v' + version\n run(['bash','-s','--','--dest',str(bindir),'--version',version], input=installer)\n return binary\n\ndef main():\n p = json.load(sys.stdin)\n # Check before identity creation or requesting an enrollment grant.\n openssl = check_prerequisites()\n home = pathlib.Path.home()\n root = home / '.local/state/dreamlake/hosts' / hashlib.sha256(p['name'].encode()).hexdigest()\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n os.chmod(root, 0o700)\n with open(root / 'lock', 'a') as lock:\n fcntl.flock(lock, fcntl.LOCK_EX)\n if p.get('managedAccess'):\n binary = nymph_binary(p, home)\n try: run([binary, 'access', '--help'])\n except Exception: prerequisite_error('nymph_access_version_required')\n if (root / 'nymph.toml').exists() and not (root / 'access.json').exists():\n prerequisite_error('nymph_access_migration_required')\n key = root / 'identity.key'\n if not key.exists(): write(key, base64.urlsafe_b64encode(os.urandom(32)).decode().rstrip('='))\n os.chmod(key, 0o600)\n raw = base64.urlsafe_b64decode(key.read_text().strip() + '==')\n if len(raw) != 32: raise RuntimeError('identity format')\n public = run([openssl,'pkey','-inform','DER','-pubout','-outform','DER'], input=bytes.fromhex('302e020100300506032b657004220420') + raw).stdout\n if len(public) != 44 or public[:12] != bytes.fromhex('302a300506032b6570032100'): raise RuntimeError('public format')\n public_key = base64.urlsafe_b64encode(public[12:]).decode().rstrip('=')\n if p['action'] == 'probe':\n print(json.dumps({'unixUser':pwd.getpwuid(os.getuid()).pw_name,'publicKey':public_key}))\n return\n if public_key != p['publicKey']: raise RuntimeError('identity changed')\n # Never fall back to a privileged system unit or an unsupervised daemon.\n binary = nymph_binary(p, home)\n web_terminal = p.get('webTerminal', True)\n if not isinstance(web_terminal, bool): raise RuntimeError('invalid web terminal option')\n sshd = '/usr/sbin/sshd'\n if web_terminal:\n if not os.access(sshd, os.X_OK): prerequisite_error('sshd_required')\n try: run([sshd, '-V'])\n except Exception: prerequisite_error('sshd_required')\n version = run([binary, '--version']).stdout.decode('utf8', errors='replace')\n match = re.search(r'\\b(\\d+)\\.(\\d+)\\.(\\d+)\\b', version)\n if not match or tuple(map(int, match.groups())) < (0, 1, 11):\n prerequisite_error('nymph_ssh_version_required')\n if sys.platform == 'linux':\n privilege_dir = pathlib.Path('/run/sshd')\n if not privilege_dir.is_dir() or privilege_dir.stat().st_uid != 0 or privilege_dir.stat().st_mode & 0o022:\n prerequisite_error('sshd_privilege_directory_required')\n (root / 'ssh').mkdir(exist_ok=True, mode=0o700)\n os.chmod(root / 'ssh', 0o700)\n grant = root / 'enroll-token'\n if p.get('token'): write(grant, p['token'])\n q = json.dumps\n config = '\\n'.join([\n '[server]', 'url = ' + q(p['controlPlaneUrl']), 'namespace = ' + q(p['namespace']),\n '[daemon]', 'machine_id = ' + q(p['machineId']), 'label = ' + q(p['name']),\n '[runtime]', 'keep_alive_s = -1', 'workdir = ' + q(str(root / 'work')), 'runners = [\"process\"]', 'default_runner = \"process\"',\n '[identity]', 'enabled = true', 'key_file = ' + q(str(key)), 'enroll_token_file = ' + q(str(grant)),\n ]) + '\\n'\n if p.get('managedAccess'):\n access = root / 'access.json'\n if not access.exists():\n write(access, json.dumps({'version': 1, 'scope': {'server': p['controlPlaneUrl'], 'namespace': p['namespace'], 'machine_id': p['machineId']}, 'permissions': {'upload': False, 'command_execution': False}}))\n config = 'access_file = ' + q(str(access)) + '\\n' + config\n if web_terminal:\n config += '\\n'.join(['[ssh]', 'enabled = true', 'state_dir = ' + q(str(root / 'ssh')),\n 'sshd = ' + q(sshd), 'max_duration_s = 3600']) + '\\n'\n (root / 'work').mkdir(exist_ok=True)\n config_file = root / 'nymph.toml'\n changed = not config_file.exists() or config_file.read_text() != config\n reload_marker = root / 'launchd-reload-required'\n if sys.platform == 'darwin' and changed:\n write(reload_marker, 'pending')\n write(config_file, config)\n service_path = str(home / '.local/bin') + (':/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' if sys.platform == 'darwin' else ':/usr/local/bin:/usr/bin:/bin')\n if sys.platform == 'darwin':\n label = 'ai.dreamlake.host.' + hashlib.sha256(p['name'].encode()).hexdigest()[:24]\n domain = 'gui/' + str(os.getuid())\n target = domain + '/' + label\n agents = home / 'Library/LaunchAgents'\n agents.mkdir(parents=True, exist_ok=True)\n agent_file = agents / (label + '.plist')\n agent = plistlib.dumps({\n 'Label': label,\n 'ProgramArguments': [binary, '--config', str(config_file)],\n 'EnvironmentVariables': {'PATH': service_path, 'HOME': str(home)},\n 'WorkingDirectory': str(root / 'work'),\n 'RunAtLoad': True,\n 'KeepAlive': True,\n 'ThrottleInterval': 3,\n # Daemon output can contain sensitive workloads: do not persist it.\n 'StandardOutPath': '/dev/null',\n 'StandardErrorPath': '/dev/null',\n }, sort_keys=True).decode('utf8')\n changed = changed or not agent_file.exists() or agent_file.read_text() != agent or reload_marker.exists()\n if changed:\n write(reload_marker, 'pending')\n # Unload before replacing a loaded definition. Never ignore bootout failure.\n loaded = subprocess.run(['launchctl', 'print', target], stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL).returncode == 0\n if loaded and changed:\n run(['launchctl', 'bootout', target])\n loaded = False\n write(agent_file, agent)\n run(['launchctl', 'enable', target])\n if not loaded:\n run(['launchctl', 'bootstrap', domain, str(agent_file)])\n # No -k: replay must not kill a healthy daemon or its active workloads.\n run(['launchctl', 'kickstart', target])\n run(['launchctl', 'print', target])\n if reload_marker.exists():\n reload_marker.unlink()\n else:\n # JSON string escaping covers quoted systemd arguments; percent must be escaped separately.\n unit_quote = lambda s: q(s.replace('%','%%'))\n unit_name = 'dreamlake-host-' + hashlib.sha256(p['name'].encode()).hexdigest()[:24] + '.service'\n unit_dir = home / '.config/systemd/user'\n unit_dir.mkdir(parents=True, exist_ok=True)\n unit = '[Unit]\\nDescription=DreamLake nymph host\\n[Service]\\nEnvironment=' + unit_quote('PATH=' + service_path) + '\\nExecStart=' + unit_quote(binary) + ' --config ' + unit_quote(str(config_file)) + '\\nRestart=on-failure\\nRestartSec=3\\n[Install]\\nWantedBy=default.target\\n'\n unit_file = unit_dir / unit_name\n changed = changed or not unit_file.exists() or unit_file.read_text() != unit\n write(unit_file, unit)\n run(['systemctl','--user','daemon-reload'])\n run(['systemctl','--user','enable',unit_name])\n run(['systemctl','--user','restart' if changed else 'start',unit_name])\n print(json.dumps({'started':True}))\ntry:\n main()\nexcept Exception:\n # Never forward child stdout/stderr or payloads: these can contain grants.\n print(json.dumps({'error':'Remote bootstrap failed. Verify Python 3, OpenSSL, the platform user-service manager, OpenSSL Ed25519 support, and nymph installation access.'}))\n sys.exit(1)\n";
2
+ export const BOOTSTRAP_PYTHON = "import base64, fcntl, pwd, hashlib, json, os, pathlib, shutil, stat, subprocess, sys, tempfile, time, re, urllib.request, plistlib\n\ndef run(args, **kwargs):\n return subprocess.run(args, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)\ndef prerequisite_error(code):\n print(json.dumps({'code': code}))\n sys.exit(1)\ndef openssl_binary():\n # Apple's bundled LibreSSL does not reliably support Ed25519 DER keys.\n candidates = ([str(pathlib.Path.home() / '.local/bin/openssl'),\n '/opt/homebrew/opt/openssl@3/bin/openssl',\n '/usr/local/opt/openssl@3/bin/openssl'] if sys.platform == 'darwin' else [])\n candidates.append(shutil.which('openssl'))\n for binary in candidates:\n if not binary or not os.access(binary, os.X_OK):\n continue\n try:\n result = run([binary, 'pkey', '-inform', 'DER', '-pubout', '-outform', 'DER'],\n input=bytes.fromhex('302e020100300506032b657004220420') + bytes(32)).stdout\n if len(result) == 44 and result[:12] == bytes.fromhex('302a300506032b6570032100'):\n return binary\n except Exception:\n pass\n prerequisite_error('openssl_ed25519_required')\n\ndef check_prerequisites():\n openssl = openssl_binary()\n if sys.platform == 'darwin':\n if os.getuid() == 0:\n prerequisite_error('macos_user_required')\n try:\n run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception:\n prerequisite_error('launchd_gui_unavailable')\n return openssl\n if sys.platform != 'linux':\n prerequisite_error('platform_unsupported')\n try:\n run(['systemctl', '--user', 'show-environment'])\n except Exception:\n prerequisite_error('systemd_user_unavailable')\n try:\n linger = run(['loginctl', 'show-user', pwd.getpwuid(os.getuid()).pw_name, '-p', 'Linger', '--value']).stdout.strip()\n except Exception:\n prerequisite_error('linger_unavailable')\n if linger != b'yes':\n prerequisite_error('linger_required')\n return openssl\ndef write(path, value):\n fd, tmp = tempfile.mkstemp(dir=str(path.parent))\n try:\n os.fchmod(fd, 0o600)\n with os.fdopen(fd, 'w') as f: f.write(value)\n os.replace(tmp, path)\n finally:\n if os.path.exists(tmp): os.unlink(tmp)\ndef nymph_binary(p, home):\n binary = shutil.which('nymph')\n if not binary:\n bindir = home / '.local/bin'\n bindir.mkdir(parents=True, exist_ok=True)\n binary = str(bindir / 'nymph')\n if not pathlib.Path(binary).exists():\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # Published immutable release directories use v-prefixed tags.\n version = p.get('version', 'latest')\n if re.fullmatch(r'\\d+\\.\\d+\\.\\d+(?:-[A-Za-z0-9.-]+)?', version):\n version = 'v' + version\n run(['bash','-s','--','--dest',str(bindir),'--version',version], input=installer)\n return binary\n\ndef service_names(name):\n digest = hashlib.sha256(name.encode()).hexdigest()[:24]\n return 'ai.dreamlake.host.' + digest, 'dreamlake-host-' + digest + '.service'\n\n# ---- Nymph upgrade, verification and rollback (`dreamlake hosts upgrade`) ----\n# Every action records itself in ~/.local/share/dreamlake/fleet-upgrades/<YYYYMMDD-NNN>/.\n# The restart that activates a new binary runs once, later, outside the session\n# that requested it: `dreamlake exec` sessions are relayed by nymph itself.\nUPGRADE_TOOL = 'dreamlake-hosts-upgrade'\nUPGRADE_ACTIONS = ('upgrade', 'rollback', 'verify', 'inspect')\nRESTART_GRACE = 90\nVERSION = r'\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?'\n\ndef fail(code, **extra):\n extra['code'] = code\n print(json.dumps(extra))\n sys.exit(1)\ndef sha256_file(path):\n digest = hashlib.sha256()\n with open(str(path), 'rb') as f:\n for chunk in iter(lambda: f.read(1 << 20), b''): digest.update(chunk)\n return digest.hexdigest()\ndef binary_version(binary):\n try: out = run([str(binary), '--version']).stdout.decode('utf8', errors='replace')\n except Exception: return None\n match = re.search(r'(?<![0-9.])(' + VERSION + ')', out)\n return match.group(1) if match else None\ndef bounded(value, default, low, high, code):\n value = default if value is None else value\n if not isinstance(value, int) or isinstance(value, bool) or not low <= value <= high: fail(code)\n return value\n\ndef leftover_restart_jobs(home, remove=True):\n \"\"\"Same rule as src/cli/hosts/leftover-jobs.ts (dreamlake-cli#407): only a submitted\n ai.dreamlake.refresh.* job running this user's fleet-upgrades/<id>/restart.py, which\n the 2026-10 fleet upgrade left behind and launchd re-ran forever. Nothing else.\"\"\"\n if sys.platform != 'darwin': return []\n script = re.compile(r'^\\s*' + re.escape(str(home)) + r'/\\.local/share/dreamlake/fleet-upgrades/[A-Za-z0-9._-]+/restart\\.py$', re.M)\n found = []\n try: listing = run(['launchctl', 'list']).stdout.decode('utf8', errors='replace')\n except Exception: return found\n for line in listing.split('\\n'):\n fields = line.split()\n label = fields[2] if len(fields) > 2 else ''\n if not re.fullmatch(r'ai\\.dreamlake\\.refresh\\.[A-Za-z0-9]+', label): continue\n try:\n detail = run(['launchctl', 'print', 'gui/%d/%s' % (os.getuid(), label)]).stdout.decode('utf8', errors='replace')\n if not re.search(r'^\\s*type = Submitted$', detail, re.M) or not script.search(detail): continue\n if remove: run(['launchctl', 'remove', label])\n found.append(label)\n except Exception:\n pass\n return found\n\ndef service_prerequisites():\n if sys.platform == 'darwin':\n if os.getuid() == 0: prerequisite_error('macos_user_required')\n try: run(['launchctl', 'print', 'gui/' + str(os.getuid())])\n except Exception: prerequisite_error('launchd_gui_unavailable')\n elif sys.platform == 'linux':\n try: run(['systemctl', '--user', 'show-environment'])\n except Exception: prerequisite_error('systemd_user_unavailable')\n if not shutil.which('systemd-run'): prerequisite_error('systemd_run_required')\n else:\n prerequisite_error('platform_unsupported')\n\ndef installed_service(name, home):\n \"\"\"The service and binary that enrollment installed; never a guess from PATH.\"\"\"\n label, unit = service_names(name)\n if sys.platform == 'darwin':\n agent = home / 'Library/LaunchAgents' / (label + '.plist')\n if not agent.is_file(): fail('service_not_installed')\n with open(str(agent), 'rb') as f: binary = plistlib.load(f)['ProgramArguments'][0]\n service = {'kind': 'launchd', 'target': 'gui/%d/%s' % (os.getuid(), label)}\n else:\n unit_file = home / '.config/systemd/user' / unit\n if not unit_file.is_file(): fail('service_not_installed')\n lines = [l for l in unit_file.read_text().splitlines() if l.startswith('ExecStart=')]\n if len(lines) != 1: fail('service_not_installed')\n binary = json.JSONDecoder().raw_decode(lines[0][len('ExecStart='):])[0].replace('%%', '%')\n service = {'kind': 'systemd', 'unit': unit}\n service['binary'] = binary\n return service\n\ndef check_binary(binary):\n # Swap only a regular file this user owns in a directory it can write; never a\n # package-managed symlink or a root-owned install that would need sudo.\n try: st = os.lstat(str(binary))\n except OSError: fail('nymph_binary_missing', binary=str(binary))\n if not binary.is_absolute() or not stat.S_ISREG(st.st_mode) or st.st_uid != os.getuid() or not os.access(str(binary.parent), os.W_OK):\n fail('nymph_binary_unmanaged', binary=str(binary))\n\ndef service_state(service):\n if service['kind'] == 'launchd':\n result = subprocess.run(['launchctl', 'print', service['target']], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n if result.returncode != 0: return {'loaded': False, 'pid': None, 'runs': None}\n text = result.stdout.decode('utf8', errors='replace')\n pid, runs = re.search(r'^\\tpid = (\\d+)$', text, re.M), re.search(r'^\\truns = (\\d+)$', text, re.M)\n return {'loaded': True, 'pid': int(pid.group(1)) if pid else None, 'runs': int(runs.group(1)) if runs else None}\n result = subprocess.run(['systemctl', '--user', 'show', service['unit'], '-p', 'MainPID', '-p', 'InvocationID', '-p', 'NRestarts', '-p', 'ActiveState'],\n stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n props = dict(l.split('=', 1) for l in result.stdout.decode('utf8', errors='replace').splitlines() if '=' in l)\n pid = int(props.get('MainPID') or 0) or None\n return {'loaded': props.get('ActiveState') == 'active', 'pid': pid, 'invocation': props.get('InvocationID') or None,\n 'autoRestarts': int(props.get('NRestarts') or 0)}\n\ndef process_started_at(pid):\n try: out = run(['ps', '-o', 'etime=', '-p', str(pid)]).stdout.decode().strip()\n except Exception: return None\n match = re.fullmatch(r'(?:(\\d+)-)?(?:(\\d+):)?(\\d+):(\\d+)', out)\n if not match: return None\n days, hours, minutes, seconds = (int(x or 0) for x in match.groups())\n return time.time() - (((days * 24 + hours) * 60 + minutes) * 60 + seconds)\n\ndef running_current(state, binary):\n \"\"\"Whether the service process was started from the binary now on disk.\"\"\"\n pid = state.get('pid')\n if not pid: return False\n if sys.platform == 'linux':\n try:\n if os.readlink('/proc/%d/exe' % pid).endswith(' (deleted)'): return False\n except OSError: pass\n started = process_started_at(pid)\n # etime is whole (possibly rounded) seconds: demand a 2 s margin. An ambiguous\n # answer only costs one extra, verified restart.\n return started is not None and started - 2 >= os.stat(str(binary)).st_mtime\ndef verified_current(fleet, service, sha, pid):\n \"\"\"A recorded verification proves this PID came from the restart onto this binary.\"\"\"\n for d, data in reversed(receipts(fleet)):\n if data.get('binary') != service['binary'] or data.get('status') != 'restart_scheduled': continue\n try: result = json.loads((d / 'verification.json').read_text())\n except Exception: return False\n return data.get('sha256') == sha and result.get('status') == 'verified' and bool(pid) and (result.get('pid') or {}).get('after') == pid\n return False\n\ndef fleet_root(home):\n root = home / '.local/share/dreamlake/fleet-upgrades'\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n return root\ndef receipts(fleet):\n \"\"\"Receipts written by this tool, oldest first. Ad-hoc 2026-10 receipts are ignored.\"\"\"\n found = []\n for d in sorted(fleet.iterdir()):\n if not re.fullmatch(r'\\d{8}-\\d{3}', d.name) or not (d / 'receipt.json').is_file(): continue\n try: data = json.loads((d / 'receipt.json').read_text())\n except Exception: continue\n if isinstance(data, dict) and data.get('tool') == UPGRADE_TOOL: found.append((d, data))\n return found\ndef select_receipt(fleet, service, upgrade_id=None, rollback=False):\n for d, data in reversed(receipts(fleet)):\n if upgrade_id is not None and d.name != upgrade_id: continue\n if data.get('binary') != service['binary'] or data.get('status') != 'restart_scheduled': continue\n if rollback and not (d / 'previous-nymph').is_file(): continue\n return d, data\n fail('receipt_not_found')\ndef pending_restart(fleet):\n now = time.time()\n for d, data in receipts(fleet):\n at = (data.get('restart') or {}).get('at')\n if at and not (d / 'restart.started').exists() and now < at + RESTART_GRACE: return d.name\ndef new_action_dir(fleet):\n day = time.strftime('%Y%m%d')\n taken = [int(d.name[9:]) for d in fleet.iterdir() if re.fullmatch(day + r'-\\d{3}', d.name)]\n for n in range(max(taken + [0]) + 1, 1000):\n d = fleet / ('%s-%03d' % (day, n))\n try:\n d.mkdir(mode=0o700)\n return d\n except FileExistsError:\n continue\n raise RuntimeError('upgrade ids exhausted')\n\ndef swap_binary(source, binary, expected):\n \"\"\"Atomic replace in the binary's own directory; the old path never disappears.\"\"\"\n mode = stat.S_IMODE(os.stat(str(binary)).st_mode) | 0o500\n tmp = binary.parent / ('.nymph-upgrade-' + os.urandom(6).hex())\n try:\n shutil.copyfile(str(source), str(tmp))\n os.chmod(str(tmp), mode)\n with open(str(tmp), 'rb') as f: os.fsync(f.fileno())\n if sha256_file(tmp) != expected: raise RuntimeError('staged binary changed')\n os.replace(str(tmp), str(binary))\n finally:\n if tmp.exists(): tmp.unlink()\n\ndef shell_quote(value):\n return \"'\" + str(value).replace(\"'\", \"'\\\\''\") + \"'\"\ndef schedule_restart(service, d, delay):\n \"\"\"One delayed restart that outlives this session and can run at most once.\n restart.sh creates restart.started with noclobber before restarting; any second\n execution, for any reason, finds the marker and exits without restarting.\"\"\"\n launchctl, systemctl = shutil.which('launchctl'), shutil.which('systemctl')\n if service['kind'] == 'launchd':\n if not launchctl: raise RuntimeError('launchctl missing')\n command = [launchctl, 'kickstart', '-k', service['target']]\n read_pid = \"%s print %s | awk '/^\\\\tpid = / {print $3; exit}'\" % (shell_quote(launchctl), shell_quote(service['target']))\n else:\n if not systemctl: raise RuntimeError('systemctl missing')\n command = [systemctl, '--user', 'restart', service['unit']]\n read_pid = \"%s --user show %s -p MainPID | awk -F= '$1 == \\\"MainPID\\\" {print $2; exit}'\" % (shell_quote(systemctl), shell_quote(service['unit']))\n script = d / 'restart.sh'\n # restart.pid is the service PID right after this restart; verification requires the\n # final PID to equal it, which proves no later restart happened.\n write(script, '\\n'.join([\n '# One-shot nymph restart for %s; written by dreamlake hosts upgrade.' % d.name,\n 'if ! (set -C; : > %s) 2>/dev/null; then exit 0; fi' % shell_quote(d / 'restart.started'),\n 'date +%%s >> %s' % shell_quote(d / 'restart.log'),\n ' '.join(shell_quote(a) for a in command),\n 'status=$?',\n 'for attempt in 1 2 3 4 5 6 7 8 9 10; do pid=$(%s); [ -n \"$pid\" ] && [ \"$pid\" != 0 ] && break; sleep 1; done' % read_pid,\n 'echo \"$pid\" > %s' % shell_quote(d / 'restart.pid'),\n 'echo \"$status\" > %s' % shell_quote(d / 'restart.exit'), '']))\n at = time.time() + delay\n if service['kind'] == 'launchd':\n # A plain process in its own session (setsid): launchd never registers it, so\n # nothing can respawn it (no `launchctl submit`, no KeepAlive), and outside\n # nymph's process group neither the SSH session ending nor nymph's teardown\n # signals it before it runs.\n child = subprocess.Popen(['/bin/sh', '-c', 'sleep %d; exec /bin/sh %s' % (delay, shell_quote(script))],\n stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,\n close_fds=True, start_new_session=True, cwd='/')\n return {'mechanism': 'detached-process', 'pid': child.pid, 'at': at, 'delaySeconds': delay}\n # A transient timer in the user manager, outside nymph's cgroup (a child of nymph\n # would be killed by its own restart). No Restart= and an OnActiveSec timer that\n # unloads after elapsing: systemd starts the service once and then forgets both.\n unit = 'dreamlake-nymph-restart-%s-%s' % (d.name, os.urandom(3).hex())\n run([shutil.which('systemd-run'), '--user', '--quiet', '--collect', '--unit=' + unit, '--on-active=%ds' % delay,\n '--timer-property=AccuracySec=1s', '--timer-property=RemainAfterElapse=no', '/bin/sh', str(script)])\n return {'mechanism': 'systemd-run-timer', 'unit': unit, 'at': at, 'delaySeconds': delay}\n\ndef helper_finished(restart):\n if restart.get('mechanism') == 'detached-process':\n try: os.kill(restart['pid'], 0)\n except ProcessLookupError: return True\n except OSError: return False\n try: command = run(['ps', '-o', 'command=', '-p', str(restart['pid'])]).stdout.decode('utf8', errors='replace')\n except Exception: return True\n return 'restart.sh' not in command\n for suffix in ('.timer', '.service'):\n result = subprocess.run(['systemctl', '--user', 'show', restart['unit'] + suffix, '-p', 'ActiveState'],\n stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n if result.stdout.decode().strip() in ('ActiveState=active', 'ActiveState=activating'): return False\n return True\n\ndef apply_binary(fleet, service, binary, source, sha, receipt, delay):\n \"\"\"Keep the previous binary, swap atomically, record, then schedule the one restart.\"\"\"\n d = receipt['dir']\n shutil.copyfile(str(source), str(d / 'nymph'))\n shutil.copyfile(str(binary), str(d / 'previous-nymph'))\n for kept in ('nymph', 'previous-nymph'): os.chmod(str(d / kept), 0o700)\n if sha256_file(d / 'nymph') != sha or sha256_file(d / 'previous-nymph') != receipt['previousSha256']:\n raise RuntimeError('copied binary changed')\n del receipt['dir']\n receipt['status'] = 'swapping'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n swap_binary(d / 'nymph', binary, sha)\n try:\n receipt['restart'] = schedule_restart(service, d, delay)\n except Exception:\n swap_binary(d / 'previous-nymph', binary, receipt['previousSha256'])\n receipt['status'] = 'reverted'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n fail('restart_schedule_failed', upgradeId=d.name)\n receipt['status'] = 'restart_scheduled'\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n return d\n\ndef restart_only(fleet, service, binary, receipt, delay):\n d = receipt.pop('dir')\n receipt['status'] = 'restart_scheduled'\n receipt['restart'] = schedule_restart(service, d, delay)\n write(d / 'receipt.json', json.dumps(receipt, sort_keys=True))\n return d\n\ndef scheduled(d, receipt, removed):\n return {'status': 'restart_scheduled', 'action': receipt['action'], 'upgradeId': d.name, 'version': receipt['version'],\n 'previousVersion': receipt.get('previousVersion'), 'sha256': receipt['sha256'], 'previousSha256': receipt.get('previousSha256'),\n 'restart': {k: receipt['restart'][k] for k in ('mechanism', 'at', 'delaySeconds')}, 'before': receipt['before'],\n 'leftoverJobsRemoved': removed}\n\ndef verify(fleet, service, binary, p):\n d, receipt = select_receipt(fleet, service, p.get('upgradeId'))\n stable = bounded(p.get('stableSeconds'), 15, 1, 300, 'invalid_stable_seconds')\n restart, before = receipt.get('restart') or {}, receipt.get('before') or {}\n deadline = (restart.get('at') or 0) + RESTART_GRACE\n while time.time() < deadline and not (d / 'restart.exit').exists(): time.sleep(0.5)\n state = service_state(service)\n while time.time() < deadline and (not state.get('pid') or state.get('pid') == before.get('pid')):\n time.sleep(1)\n state = service_state(service)\n samples, end = [state], time.time() + stable\n while time.time() < end:\n time.sleep(1)\n samples.append(service_state(service))\n after = samples[-1]\n log = d / 'restart.log'\n helper_runs = len(log.read_text().split()) if log.exists() else 0\n exit_status = (d / 'restart.exit').read_text().strip() if (d / 'restart.exit').exists() else None\n restart_pid = (d / 'restart.pid').read_text().strip() if (d / 'restart.pid').exists() else ''\n checks = {\n 'restartRan': (d / 'restart.started').exists(),\n 'restartRuns': helper_runs,\n 'restartExit': int(exit_status) if exit_status and exit_status.isdigit() else None,\n 'pidChanged': bool(after.get('pid')) and after.get('pid') != before.get('pid'),\n 'pidStable': len({s.get('pid') for s in samples}) == 1 and bool(after.get('pid')),\n 'restartPid': int(restart_pid) if restart_pid.isdigit() else None,\n 'helperFinished': helper_finished(restart) if restart else False,\n 'binarySha256': sha256_file(binary) == receipt['sha256'],\n 'version': binary_version(binary),\n # Informational: the PID chain (pidFromRestart) is the proof; ps start times are coarse.\n 'processCurrent': running_current(after, binary),\n 'leftoverJobsRemoved': leftover_restart_jobs(pathlib.Path.home()),\n }\n if service['kind'] == 'launchd' and before.get('runs') is not None and after.get('runs') is not None:\n checks['serviceStarts'] = after['runs'] - before['runs']\n if service['kind'] == 'systemd':\n checks['invocationChanged'] = bool(after.get('invocation')) and after.get('invocation') != before.get('invocation')\n # An explicit systemctl restart resets NRestarts. Only the new\n # activation's count applies; subtracting the previous activation can\n # reject a healthy restart or hide new crashes behind an old count.\n checks['autoRestarts'] = after.get('autoRestarts', 0)\n failures = [k for k, ok in [\n ('restartRan', checks['restartRan']), ('restartRuns', helper_runs == 1), ('restartExit', checks['restartExit'] == 0),\n ('pidChanged', checks['pidChanged']), ('pidStable', checks['pidStable']),\n ('pidFromRestart', checks['restartPid'] is not None and checks['restartPid'] == after.get('pid')),\n ('helperFinished', checks['helperFinished']),\n ('binarySha256', checks['binarySha256']), ('version', checks['version'] == receipt['version']),\n ('serviceStarts', checks.get('serviceStarts', 1) == 1),\n ('invocationChanged', checks.get('invocationChanged', True)), ('autoRestarts', checks.get('autoRestarts', 0) == 0)] if not ok]\n result = {'status': 'failed' if failures else 'verified', 'action': receipt['action'], 'upgradeId': d.name,\n 'version': checks['version'], 'expectedVersion': receipt['version'],\n 'pid': {'before': before.get('pid'), 'after': after.get('pid')}, 'restarts': helper_runs,\n 'stableSeconds': stable, 'checks': checks, 'failures': failures}\n write(d / 'verification.json', json.dumps(result, sort_keys=True))\n print(json.dumps(result))\n if failures: sys.exit(1)\n\ndef inspect(fleet, service, binary, p):\n state = service_state(service)\n version = binary_version(binary)\n wanted = p.get('version')\n sha = sha256_file(binary)\n current = verified_current(fleet, service, sha, state.get('pid')) or running_current(state, binary)\n pending = pending_restart(fleet)\n rollback = next((d.name for d, data in reversed(receipts(fleet)) if data.get('binary') == service['binary']\n and data.get('status') == 'restart_scheduled' and (d / 'previous-nymph').is_file()), None)\n plan = None\n if isinstance(wanted, str):\n wanted = wanted[1:] if wanted.startswith('v') else wanted\n plan = 'blocked:restart_pending' if pending else 'current' if version == wanted and current else 'restart' if version == wanted else 'upgrade'\n print(json.dumps({'status': 'inspected', 'service': {k: v for k, v in service.items() if k != 'binary'}, 'binary': service['binary'],\n 'version': version, 'sha256': sha, 'pid': state.get('pid'), 'processCurrent': current,\n 'pendingRestart': pending, 'rollbackAvailable': rollback, 'plan': plan,\n 'leftoverJobs': leftover_restart_jobs(pathlib.Path.home(), remove=False)}))\n\ndef upgrade_main(p):\n action = p['action']\n if not isinstance(p.get('name'), str) or not p['name']: fail('invalid_request')\n service_prerequisites()\n home = pathlib.Path.home()\n fleet = fleet_root(home)\n service = installed_service(p['name'], home)\n binary = pathlib.Path(service['binary'])\n check_binary(binary)\n if action == 'inspect': return inspect(fleet, service, binary, p)\n with open(str(fleet / '.lock'), 'a') as lock:\n try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)\n except OSError: fail('upgrade_in_progress')\n if action == 'verify': return verify(fleet, service, binary, p)\n delay = bounded(p.get('restartDelaySeconds'), 15, 1, 600, 'invalid_restart_delay')\n removed = leftover_restart_jobs(home)\n pending = pending_restart(fleet)\n if pending: fail('restart_pending', upgradeId=pending)\n current_sha, current_version = sha256_file(binary), binary_version(binary)\n state = service_state(service)\n base = {'tool': UPGRADE_TOOL, 'schema': 1, 'name': p['name'], 'binary': service['binary'],\n 'service': {k: v for k, v in service.items() if k != 'binary'}, 'previousVersion': current_version,\n 'previousSha256': current_sha, 'before': state, 'leftoverJobsRemoved': removed, 'at': time.time()}\n if action == 'rollback':\n source_dir, source = select_receipt(fleet, service, p.get('upgradeId'), rollback=True)\n if current_sha != source.get('sha256'): fail('rollback_receipt_mismatch', upgradeId=source_dir.name)\n previous = source_dir / 'previous-nymph'\n if sha256_file(previous) != source.get('previousSha256'): fail('rollback_binary_corrupt', upgradeId=source_dir.name)\n receipt = dict(base, action='rollback', rollbackOf=source_dir.name, version=source.get('previousVersion') or binary_version(previous),\n sha256=source['previousSha256'], dir=new_action_dir(fleet))\n d = apply_binary(fleet, service, binary, previous, receipt['sha256'], receipt, delay)\n print(json.dumps(scheduled(d, receipt, removed)))\n return\n version = p.get('version')\n if not isinstance(version, str) or not re.fullmatch('v?' + VERSION, version): fail('invalid_version')\n version = version[1:] if version.startswith('v') else version\n if current_version == version:\n if verified_current(fleet, service, current_sha, state.get('pid')) or running_current(state, binary):\n print(json.dumps({'status': 'current', 'version': version, 'sha256': current_sha, 'pid': state.get('pid'),\n 'leftoverJobsRemoved': removed}))\n return\n # Already swapped but the service still runs an older process: restart only.\n receipt = dict(base, action='restart', version=version, sha256=current_sha, dir=new_action_dir(fleet))\n d = restart_only(fleet, service, binary, receipt, delay)\n print(json.dumps(scheduled(d, receipt, removed)))\n return\n d = new_action_dir(fleet)\n try:\n stage = d / 'stage'\n stage.mkdir(mode=0o700)\n with urllib.request.urlopen('https://dreamlake.ai/dreamlake/nymph/install.sh', timeout=30) as response:\n installer = response.read()\n # The official installer downloads the archive and verifies its published SHA-256.\n run(['bash', '-s', '--', '--dest', str(stage), '--version', 'v' + version], input=installer)\n staged = stage / 'nymph'\n if not staged.is_file(): fail('installer_failed')\n found = binary_version(staged)\n if found != version: fail('installer_version_mismatch', expected=version, found=found)\n sha = sha256_file(staged)\n receipt = dict(base, action='upgrade', version=version, sha256=sha, dir=d)\n apply_binary(fleet, service, binary, staged, sha, receipt, delay)\n shutil.rmtree(str(stage), ignore_errors=True)\n except BaseException:\n if not (d / 'receipt.json').exists(): shutil.rmtree(str(d), ignore_errors=True)\n raise\n print(json.dumps(scheduled(d, receipt, removed)))\n\ndef main():\n p = json.load(sys.stdin)\n if p.get('action') in UPGRADE_ACTIONS:\n try:\n return upgrade_main(p)\n except SystemExit:\n raise\n except Exception:\n fail('upgrade_failed')\n # Check before identity creation or requesting an enrollment grant.\n openssl = check_prerequisites()\n home = pathlib.Path.home()\n root = home / '.local/state/dreamlake/hosts' / hashlib.sha256(p['name'].encode()).hexdigest()\n root.mkdir(parents=True, exist_ok=True, mode=0o700)\n os.chmod(root, 0o700)\n with open(root / 'lock', 'a') as lock:\n fcntl.flock(lock, fcntl.LOCK_EX)\n if p.get('managedAccess'):\n binary = nymph_binary(p, home)\n try: run([binary, 'access', '--help'])\n except Exception: prerequisite_error('nymph_access_version_required')\n if (root / 'nymph.toml').exists() and not (root / 'access.json').exists():\n prerequisite_error('nymph_access_migration_required')\n key = root / 'identity.key'\n if not key.exists(): write(key, base64.urlsafe_b64encode(os.urandom(32)).decode().rstrip('='))\n os.chmod(key, 0o600)\n raw = base64.urlsafe_b64decode(key.read_text().strip() + '==')\n if len(raw) != 32: raise RuntimeError('identity format')\n public = run([openssl,'pkey','-inform','DER','-pubout','-outform','DER'], input=bytes.fromhex('302e020100300506032b657004220420') + raw).stdout\n if len(public) != 44 or public[:12] != bytes.fromhex('302a300506032b6570032100'): raise RuntimeError('public format')\n public_key = base64.urlsafe_b64encode(public[12:]).decode().rstrip('=')\n if p['action'] == 'probe':\n print(json.dumps({'unixUser':pwd.getpwuid(os.getuid()).pw_name,'publicKey':public_key}))\n return\n if public_key != p['publicKey']: raise RuntimeError('identity changed')\n # Never fall back to a privileged system unit or an unsupervised daemon.\n binary = nymph_binary(p, home)\n web_terminal = p.get('webTerminal', True)\n if not isinstance(web_terminal, bool): raise RuntimeError('invalid web terminal option')\n sshd = '/usr/sbin/sshd'\n if web_terminal:\n if not os.access(sshd, os.X_OK): prerequisite_error('sshd_required')\n try: run([sshd, '-V'])\n except Exception: prerequisite_error('sshd_required')\n version = run([binary, '--version']).stdout.decode('utf8', errors='replace')\n match = re.search(r'\\b(\\d+)\\.(\\d+)\\.(\\d+)\\b', version)\n if not match or tuple(map(int, match.groups())) < (0, 1, 11):\n prerequisite_error('nymph_ssh_version_required')\n if sys.platform == 'linux':\n privilege_dir = pathlib.Path('/run/sshd')\n if not privilege_dir.is_dir() or privilege_dir.stat().st_uid != 0 or privilege_dir.stat().st_mode & 0o022:\n prerequisite_error('sshd_privilege_directory_required')\n (root / 'ssh').mkdir(exist_ok=True, mode=0o700)\n os.chmod(root / 'ssh', 0o700)\n grant = root / 'enroll-token'\n if p.get('token'): write(grant, p['token'])\n q = json.dumps\n config = '\\n'.join([\n '[server]', 'url = ' + q(p['controlPlaneUrl']), 'namespace = ' + q(p['namespace']),\n '[daemon]', 'machine_id = ' + q(p['machineId']), 'label = ' + q(p['name']),\n '[runtime]', 'keep_alive_s = -1', 'workdir = ' + q(str(root / 'work')), 'runners = [\"process\"]', 'default_runner = \"process\"',\n '[identity]', 'enabled = true', 'key_file = ' + q(str(key)), 'enroll_token_file = ' + q(str(grant)),\n ]) + '\\n'\n if p.get('managedAccess'):\n access = root / 'access.json'\n if not access.exists():\n write(access, json.dumps({'version': 1, 'scope': {'server': p['controlPlaneUrl'], 'namespace': p['namespace'], 'machine_id': p['machineId']}, 'permissions': {'upload': False, 'command_execution': False}}))\n config = 'access_file = ' + q(str(access)) + '\\n' + config\n if web_terminal:\n config += '\\n'.join(['[ssh]', 'enabled = true', 'state_dir = ' + q(str(root / 'ssh')),\n 'sshd = ' + q(sshd), 'max_duration_s = 3600']) + '\\n'\n (root / 'work').mkdir(exist_ok=True)\n config_file = root / 'nymph.toml'\n changed = not config_file.exists() or config_file.read_text() != config\n reload_marker = root / 'launchd-reload-required'\n if sys.platform == 'darwin' and changed:\n write(reload_marker, 'pending')\n write(config_file, config)\n service_path = str(home / '.local/bin') + (':/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' if sys.platform == 'darwin' else ':/usr/local/bin:/usr/bin:/bin')\n if sys.platform == 'darwin':\n label = service_names(p['name'])[0]\n domain = 'gui/' + str(os.getuid())\n target = domain + '/' + label\n agents = home / 'Library/LaunchAgents'\n agents.mkdir(parents=True, exist_ok=True)\n agent_file = agents / (label + '.plist')\n agent = plistlib.dumps({\n 'Label': label,\n 'ProgramArguments': [binary, '--config', str(config_file)],\n 'EnvironmentVariables': {'PATH': service_path, 'HOME': str(home)},\n 'WorkingDirectory': str(root / 'work'),\n 'RunAtLoad': True,\n 'KeepAlive': True,\n 'ThrottleInterval': 3,\n # Daemon output can contain sensitive workloads: do not persist it.\n 'StandardOutPath': '/dev/null',\n 'StandardErrorPath': '/dev/null',\n }, sort_keys=True).decode('utf8')\n changed = changed or not agent_file.exists() or agent_file.read_text() != agent or reload_marker.exists()\n if changed:\n write(reload_marker, 'pending')\n # Unload before replacing a loaded definition. Never ignore bootout failure.\n loaded = subprocess.run(['launchctl', 'print', target], stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL).returncode == 0\n if loaded and changed:\n run(['launchctl', 'bootout', target])\n loaded = False\n write(agent_file, agent)\n run(['launchctl', 'enable', target])\n if not loaded:\n run(['launchctl', 'bootstrap', domain, str(agent_file)])\n # No -k: replay must not kill a healthy daemon or its active workloads.\n run(['launchctl', 'kickstart', target])\n run(['launchctl', 'print', target])\n if reload_marker.exists():\n reload_marker.unlink()\n else:\n # JSON string escaping covers quoted systemd arguments; percent must be escaped separately.\n unit_quote = lambda s: q(s.replace('%','%%'))\n unit_name = service_names(p['name'])[1]\n unit_dir = home / '.config/systemd/user'\n unit_dir.mkdir(parents=True, exist_ok=True)\n unit = '[Unit]\\nDescription=DreamLake nymph host\\n[Service]\\nEnvironment=' + unit_quote('PATH=' + service_path) + '\\nExecStart=' + unit_quote(binary) + ' --config ' + unit_quote(str(config_file)) + '\\nRestart=on-failure\\nRestartSec=3\\n[Install]\\nWantedBy=default.target\\n'\n unit_file = unit_dir / unit_name\n changed = changed or not unit_file.exists() or unit_file.read_text() != unit\n write(unit_file, unit)\n run(['systemctl','--user','daemon-reload'])\n run(['systemctl','--user','enable',unit_name])\n run(['systemctl','--user','restart' if changed else 'start',unit_name])\n print(json.dumps({'started':True}))\ntry:\n main()\nexcept Exception:\n # Never forward child stdout/stderr or payloads: these can contain grants.\n print(json.dumps({'error':'Remote bootstrap failed. Verify Python 3, OpenSSL, the platform user-service manager, OpenSSL Ed25519 support, and nymph installation access.'}))\n sys.exit(1)\n";
@@ -2,6 +2,7 @@ import { collectionList } from "../collection-list.js";
2
2
  import { inspectHostCatalog, changeHostCatalog, formatHostCatalog } from "./catalog.js";
3
3
  import { registerHostLifecycleCommands } from "./lifecycle.js";
4
4
  import { registerHostSshCommands } from "./ssh.js";
5
+ import { registerHostUpgradeCommands } from "./upgrade.js";
5
6
  import { resolveRemote, resolveToken } from '../config.js';
6
7
  import { saveCredentials } from './credentials.js';
7
8
  import { enrollHost, hostStatus } from "./enroll.js";
@@ -276,4 +277,5 @@ export function registerHostsCommand(program) {
276
277
  });
277
278
  registerHostSshCommands(group, program);
278
279
  registerHostLifecycleCommands(group);
280
+ registerHostUpgradeCommands(group);
279
281
  }
@@ -0,0 +1,10 @@
1
+ /** Runs launchctl and returns stdout; throws on a non-zero exit. */
2
+ export type Launchctl = (args: string[]) => string;
3
+ /**
4
+ * The 2026-10-21 fleet upgrade registered a one-shot nymph restart with
5
+ * `launchctl submit`, which launchd keeps alive: the job re-ran forever and
6
+ * restarted nymph every ~15s. Remove only jobs that match it exactly — a
7
+ * submitted `ai.dreamlake.refresh.*` job running this user's
8
+ * `fleet-upgrades/<id>/restart.py` — and never touch anything else.
9
+ */
10
+ export declare function removeLeftoverRestartJobs(run?: Launchctl, home?: string, platform?: NodeJS.Platform, uid?: number | undefined): string[];