@dreamlake/dreamlake-cli 0.111.1 → 0.112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/skills.js +1 -1
- package/dist/cli/agents/index.js +20 -20
- package/dist/cli/agents/manage.js +1 -1
- package/dist/cli/artifact/index.js +5 -5
- package/dist/cli/artifact/meta.js +1 -1
- package/dist/cli/auth/commands.d.ts +2 -2
- package/dist/cli/auth/commands.js +9 -9
- package/dist/cli/auth/device-flow.js +2 -2
- package/dist/cli/auth/materialized.d.ts +1 -1
- package/dist/cli/auth/materialized.js +1 -1
- package/dist/cli/compat/http.js +2 -2
- package/dist/cli/compat/presignedTransfer.js +2 -2
- package/dist/cli/config.js +2 -2
- package/dist/cli/connection/index.js +8 -8
- package/dist/cli/connection/oauth.js +3 -3
- package/dist/cli/connection/write.js +6 -6
- package/dist/cli/create/index.js +2 -2
- package/dist/cli/declare/index.js +14 -14
- package/dist/cli/delete/index.js +4 -4
- package/dist/cli/download/index.js +3 -3
- package/dist/cli/env/compose.js +1 -1
- package/dist/cli/env/index.js +9 -9
- package/dist/cli/experiments/index.js +2 -2
- package/dist/cli/experiments/native-commands.js +1 -1
- package/dist/cli/experiments/native-run-data.js +1 -1
- package/dist/cli/graphql.js +1 -1
- package/dist/cli/help-examples.generated.js +253 -253
- package/dist/cli/hosts/bootstrap-source.d.ts +1 -1
- package/dist/cli/hosts/bootstrap-source.js +1 -1
- package/dist/cli/hosts/inventory.js +2 -2
- package/dist/cli/hosts/picker.js +2 -2
- package/dist/cli/hosts/ssh.js +1 -1
- package/dist/cli/hosts/upgrade.d.ts +2 -2
- package/dist/cli/hosts/upgrade.js +3 -3
- package/dist/cli/index.js +7 -3
- package/dist/cli/lakeshore/connections.js +1 -1
- package/dist/cli/lakeshore/index.d.ts +1 -1
- package/dist/cli/lakeshore/index.js +4 -4
- package/dist/cli/launch/index.js +10 -10
- package/dist/cli/library/add.js +4 -4
- package/dist/cli/library/cache.js +1 -1
- package/dist/cli/library/discover.js +1 -1
- package/dist/cli/library/freshness.js +1 -1
- package/dist/cli/library/index.js +1 -1
- package/dist/cli/library/info.js +1 -1
- package/dist/cli/library/manifest.js +1 -1
- package/dist/cli/library/pull.js +1 -1
- package/dist/cli/library/push.js +2 -2
- package/dist/cli/library/rm.js +2 -2
- package/dist/cli/library/search.js +2 -2
- package/dist/cli/library/stat.js +1 -1
- package/dist/cli/library/tools.js +1 -1
- package/dist/cli/library/wire.js +1 -1
- package/dist/cli/list/index.js +4 -4
- package/dist/cli/memos/document.d.ts +1 -1
- package/dist/cli/memos/document.js +1 -1
- package/dist/cli/memos/edit-commands.d.ts +2 -2
- package/dist/cli/memos/edit-commands.js +2 -2
- package/dist/cli/memos/files-commands.d.ts +1 -1
- package/dist/cli/memos/files-commands.js +2 -2
- package/dist/cli/memos/index.js +7 -7
- package/dist/cli/memos/media-commands.js +1 -1
- package/dist/cli/memos/move-command.js +2 -2
- package/dist/cli/memos/rm-command.d.ts +1 -1
- package/dist/cli/memos/rm-command.js +2 -2
- package/dist/cli/namespace.js +2 -2
- package/dist/cli/nymph/index.js +4 -4
- package/dist/cli/ownership/index.js +1 -1
- package/dist/cli/ownership/metadata-transfer.js +2 -2
- package/dist/cli/page/index.js +5 -5
- package/dist/cli/pipeline/index.js +5 -5
- package/dist/cli/project/attachments.js +1 -1
- package/dist/cli/project/grants.js +1 -1
- package/dist/cli/project/index.js +1 -1
- package/dist/cli/project/move.js +3 -3
- package/dist/cli/project/stream.js +2 -2
- package/dist/cli/provider/dreamdir.js +2 -2
- package/dist/cli/provider/index.js +18 -18
- package/dist/cli/provider/lifecycle.js +14 -14
- package/dist/cli/python.js +4 -4
- package/dist/cli/queues/index.js +4 -4
- package/dist/cli/queues/slurm.js +2 -2
- package/dist/cli/resources.js +2 -2
- package/dist/cli/run/index.js +1 -1
- package/dist/cli/runconfig/index.js +3 -3
- package/dist/cli/runnable/bound.js +3 -3
- package/dist/cli/runnable/index.js +14 -14
- package/dist/cli/runnable/path.d.ts +1 -1
- package/dist/cli/runnable/path.js +1 -1
- package/dist/cli/self-update/alias-migration.d.ts +11 -0
- package/dist/cli/self-update/alias-migration.js +56 -0
- package/dist/cli/self-update/index.d.ts +1 -1
- package/dist/cli/self-update/index.js +5 -5
- package/dist/cli/self-update/install.js +2 -2
- package/dist/cli/self-update/update.d.ts +1 -1
- package/dist/cli/self-update/update.js +7 -7
- package/dist/cli/session-affiliation.js +1 -1
- package/dist/cli/sessions/control.d.ts +1 -1
- package/dist/cli/sessions/control.js +2 -2
- package/dist/cli/sessions/index.js +5 -5
- package/dist/cli/sessions/live.js +1 -1
- package/dist/cli/sessions/runner/serve.js +1 -1
- package/dist/cli/sessions/wrap.d.ts +2 -2
- package/dist/cli/sessions/wrap.js +8 -8
- package/dist/cli/skill/index.js +22 -22
- package/dist/cli/skill/prefs.js +1 -1
- package/dist/cli/skill/read.js +3 -3
- package/dist/cli/skill/registry.js +2 -2
- package/dist/cli/snapshot/index.js +9 -9
- package/dist/cli/source/access.js +3 -3
- package/dist/cli/source/control.js +8 -8
- package/dist/cli/source/delete.js +6 -6
- package/dist/cli/source/index.js +14 -14
- package/dist/cli/source/share.js +8 -8
- package/dist/cli/source/upload.d.ts +1 -1
- package/dist/cli/source/upload.js +2 -2
- package/dist/cli/source/write.js +2 -2
- package/dist/cli/spawn.js +1 -1
- package/dist/cli/tasks/index.js +1 -1
- package/dist/cli/update/index.js +2 -2
- package/dist/cli/upload/index.js +3 -3
- package/dist/cli/upload/multipart.js +1 -1
- package/dist/cli/vault/index.js +1 -1
- package/dist/cli/vault/sshSync.js +1 -1
- package/dist/cli/version.d.ts +1 -1
- package/dist/cli/version.js +1 -1
- package/dist/cli/workflow/index.d.ts +1 -1
- package/dist/cli/workflow/index.js +6 -6
- package/package.json +10 -10
|
@@ -1 +1 @@
|
|
|
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
|
+
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 (`dl 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: `dl 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 dl 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, 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
|
+
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 (`dl 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: `dl 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 dl 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";
|
|
@@ -175,12 +175,12 @@ export function hostRows(hosts) {
|
|
|
175
175
|
export function formatHostMatches(result, matches, color, command = "ssh", limit = HOST_MATCH_LIMIT) {
|
|
176
176
|
const quoted = JSON.stringify(matches.keyword);
|
|
177
177
|
if (!matches.hosts.length)
|
|
178
|
-
return `No hosts match ${quoted} in namespace ${result.namespace}. Run
|
|
178
|
+
return `No hosts match ${quoted} in namespace ${result.namespace}. Run dl hosts list --all to see every host.`;
|
|
179
179
|
const shown = matches.hosts.slice(0, limit);
|
|
180
180
|
const rows = hostRows(shown);
|
|
181
181
|
const decorate = (value, row, column) => color && row > 0 && column < 2 ? highlightMatches(value, matches.keyword) : value;
|
|
182
182
|
const more = matches.hosts.length > shown.length ? ` Showing ${shown.length}; narrow the keyword to see the rest.` : "";
|
|
183
|
-
const next = matches.hosts.length === 1 ? `Connect with:
|
|
183
|
+
const next = matches.hosts.length === 1 ? `Connect with: dl ${command} ${cell(matches.hosts[0].name)}` : "Connect with the full NAME or ID.";
|
|
184
184
|
return [`Hosts matching ${quoted} in namespace ${result.namespace}:`, table(rows, decorate),
|
|
185
185
|
`${matches.hosts.length} of ${result.total} hosts match.${more} ${next}`].join("\n");
|
|
186
186
|
}
|
package/dist/cli/hosts/picker.js
CHANGED
|
@@ -11,7 +11,7 @@ export function renderPicker(inventory, selector, command, state, color, limit =
|
|
|
11
11
|
const paint = (text, code) => color ? `${code}${text}${RESET}` : text;
|
|
12
12
|
const lines = [
|
|
13
13
|
paint(`Host ${JSON.stringify(selector)} not found. ↑↓ move · Tab complete · Enter connect · Esc cancel`, DIM),
|
|
14
|
-
`>
|
|
14
|
+
`> dl ${command} ${state.query}${paint("▏", DIM)}`,
|
|
15
15
|
];
|
|
16
16
|
if (!matches.hosts.length) {
|
|
17
17
|
lines.push(`No hosts match ${JSON.stringify(matches.keyword)} in namespace ${inventory.namespace}. Keep typing or press Esc.`);
|
|
@@ -92,7 +92,7 @@ export function pickHost(inventory, selector, command, io) {
|
|
|
92
92
|
io.input.pause();
|
|
93
93
|
clear();
|
|
94
94
|
if (picked !== undefined)
|
|
95
|
-
io.output.write(`
|
|
95
|
+
io.output.write(`dl ${command} ${picked}\n`);
|
|
96
96
|
resolve(picked);
|
|
97
97
|
};
|
|
98
98
|
const onData = (chunk) => {
|
package/dist/cli/hosts/ssh.js
CHANGED
|
@@ -63,7 +63,7 @@ export async function chooseSshTarget(targets, selector, opts, argv, choose) {
|
|
|
63
63
|
const options = [opts.namespace ? ['--namespace', opts.namespace] : [],
|
|
64
64
|
opts.duration ? ['--duration', opts.duration] : [], opts.timeout ? ['--timeout', opts.timeout] : []].flat();
|
|
65
65
|
const rows = targets.map(t => {
|
|
66
|
-
const command = ['
|
|
66
|
+
const command = ['dl', argv === undefined ? 'ssh' : 'exec', selector, ...options,
|
|
67
67
|
'--enrollment', t.enrollmentId, ...(argv === undefined ? [] : ['--', ...argv])];
|
|
68
68
|
return ` ${label(t)}\n ${command.map(quote).join(' ')}`;
|
|
69
69
|
});
|
|
@@ -3,7 +3,7 @@ import { hostStatus } from "./enroll.js";
|
|
|
3
3
|
import { listHosts, resolveHostNamespace } from "./inventory.js";
|
|
4
4
|
/**
|
|
5
5
|
* Host-side upgrade, verification and rollback run the enrollment bootstrap
|
|
6
|
-
* (bootstrap.py) over `
|
|
6
|
+
* (bootstrap.py) over `dl exec`. The program and its JSON payload travel
|
|
7
7
|
* on stdin, so the remote command stays a short fixed loader.
|
|
8
8
|
*/
|
|
9
9
|
export declare const BOOTSTRAP_LOADER: string;
|
|
@@ -20,7 +20,7 @@ export interface ExecReply {
|
|
|
20
20
|
result?: Record<string, any>;
|
|
21
21
|
}
|
|
22
22
|
export type HostExec = (target: ExecTarget, payload: Record<string, unknown>, timeoutSeconds: number) => Promise<ExecReply>;
|
|
23
|
-
/** Runs one bootstrap action through the nymph SSH relay (the `
|
|
23
|
+
/** Runs one bootstrap action through the nymph SSH relay (the `dl exec` path). */
|
|
24
24
|
export declare const execOverNymph: HostExec;
|
|
25
25
|
export type FleetAction = "upgrade" | "rollback";
|
|
26
26
|
export interface FleetOptions {
|
|
@@ -9,7 +9,7 @@ import { hostConnection, listHosts, resolveHostNamespace } from "./inventory.js"
|
|
|
9
9
|
import { defaultSshIO, runHostSsh } from "./ssh.js";
|
|
10
10
|
/**
|
|
11
11
|
* Host-side upgrade, verification and rollback run the enrollment bootstrap
|
|
12
|
-
* (bootstrap.py) over `
|
|
12
|
+
* (bootstrap.py) over `dl exec`. The program and its JSON payload travel
|
|
13
13
|
* on stdin, so the remote command stays a short fixed loader.
|
|
14
14
|
*/
|
|
15
15
|
export const BOOTSTRAP_LOADER = [
|
|
@@ -36,7 +36,7 @@ export function parseBootstrapOutput(text) {
|
|
|
36
36
|
}
|
|
37
37
|
return undefined;
|
|
38
38
|
}
|
|
39
|
-
/** Runs one bootstrap action through the nymph SSH relay (the `
|
|
39
|
+
/** Runs one bootstrap action through the nymph SSH relay (the `dl exec` path). */
|
|
40
40
|
export const execOverNymph = async (target, payload, timeoutSeconds) => {
|
|
41
41
|
const chunks = [];
|
|
42
42
|
let bytes = 0;
|
|
@@ -134,7 +134,7 @@ function failure(base, code, error, extra = {}) {
|
|
|
134
134
|
return { ...base, ...extra, status: "failed", code, error };
|
|
135
135
|
}
|
|
136
136
|
const HOST_ERRORS = {
|
|
137
|
-
service_not_installed: "no nymph service from `
|
|
137
|
+
service_not_installed: "no nymph service from `dl hosts enroll` was found for this host name",
|
|
138
138
|
nymph_binary_unmanaged: "the service binary is not a user-owned regular file in a writable directory (package-managed or root-owned); upgrade it with its installer",
|
|
139
139
|
nymph_binary_missing: "the service binary is missing",
|
|
140
140
|
upgrade_in_progress: "another upgrade, verification or rollback holds this host's lock",
|
package/dist/cli/index.js
CHANGED
|
@@ -57,6 +57,7 @@ import { registerLibraryCommand } from "./library/index.js";
|
|
|
57
57
|
import { registerSkillCommand } from "./skill/index.js";
|
|
58
58
|
import { maybeCheckForUpdates, registerDoctorCommand, registerInstallCommand, } from "./self-update/index.js";
|
|
59
59
|
import { VERSION } from "./version.js";
|
|
60
|
+
import { migrateCommandAlias } from "./self-update/alias-migration.js";
|
|
60
61
|
import { passwordAskpass } from "./vault/passwordProbe.js";
|
|
61
62
|
async function main(argv) {
|
|
62
63
|
if (argv[2] === '--internal-telemetry') {
|
|
@@ -81,6 +82,9 @@ async function main(argv) {
|
|
|
81
82
|
// Throttled, detached, and a no-op unless this is a native install. Kick
|
|
82
83
|
// it off before parsing so the check overlaps whatever the user came to do.
|
|
83
84
|
if (!inventory && startupCommand !== "telemetry") {
|
|
85
|
+
const aliasWarning = migrateCommandAlias();
|
|
86
|
+
if (aliasWarning && process.stderr.isTTY)
|
|
87
|
+
process.stderr.write(`${aliasWarning}\n`);
|
|
84
88
|
maybeCheckForUpdates(argv);
|
|
85
89
|
maybeRemindStaleSkills(argv);
|
|
86
90
|
}
|
|
@@ -105,7 +109,7 @@ async function main(argv) {
|
|
|
105
109
|
}
|
|
106
110
|
const program = new Command();
|
|
107
111
|
program
|
|
108
|
-
.name("
|
|
112
|
+
.name("dl")
|
|
109
113
|
.description("DreamLake CLI — manage data, memos, project access, and scoped Vault entries.")
|
|
110
114
|
.version(VERSION, "-v, --version", "print the version and exit")
|
|
111
115
|
.showHelpAfterError('(Add --help to see options and examples.)');
|
|
@@ -163,7 +167,7 @@ async function main(argv) {
|
|
|
163
167
|
});
|
|
164
168
|
// ─── auth env (switch between logged-in environments) ────────────
|
|
165
169
|
//
|
|
166
|
-
// This lived at top-level `
|
|
170
|
+
// This lived at top-level `dl env` until MuJoCo environments took
|
|
167
171
|
// that name (clean cutover, no alias) — docs/pages/environments records
|
|
168
172
|
// the rename.
|
|
169
173
|
const auth = program
|
|
@@ -219,7 +223,7 @@ async function main(argv) {
|
|
|
219
223
|
//
|
|
220
224
|
// The only two commands here that talk to the LAKESHORE CONTROL
|
|
221
225
|
// PLANE rather than dreamlake-server: --server/$LAKESHORE_URL, no
|
|
222
|
-
// `
|
|
226
|
+
// `dl login`. They drive the Python SDK's bridge as a child
|
|
223
227
|
// process rather than reimplementing git-archive, presigning, dirty
|
|
224
228
|
// snapshots and the msgpack envelope in TypeScript — see python.ts.
|
|
225
229
|
registerSnapshotCommand(program);
|
|
@@ -22,7 +22,7 @@ const input = {
|
|
|
22
22
|
async function context(opts) {
|
|
23
23
|
const token = resolveToken();
|
|
24
24
|
if (!token)
|
|
25
|
-
throw new Error("not authenticated — run '
|
|
25
|
+
throw new Error("not authenticated — run 'dl login' first");
|
|
26
26
|
const remote = resolveRemote();
|
|
27
27
|
const ns = await resolveNamespace(opts.namespace, { remote, token });
|
|
28
28
|
if (!ns)
|
|
@@ -49,7 +49,7 @@ export interface ResourceCreateOpts {
|
|
|
49
49
|
/**
|
|
50
50
|
* Inline JSON or @file.json, assigned to the row's generic `metadata` column.
|
|
51
51
|
*
|
|
52
|
-
* It exists so `
|
|
52
|
+
* It exists so `dl provider create/update` can carry
|
|
53
53
|
* `metadata.lifecycle` — {vendor, template, templateVersion, dreamDir,
|
|
54
54
|
* scaffoldedAt} — so a teammate who clones the repo, or an agent on another
|
|
55
55
|
* machine, can see which template a provider came from. `metadata` was
|