okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-s3: Control bundled in the installer, local engine components, no runtime acquisitionbe885026f

main
3.2 KiB62 linesraw
1/**
2 * The one place the App Host decides which Python to run. Every spawn of Python goes through it
3 * (tuning, Time-skip, the revise command, the native MCP client, setup/calibration jobs, the native server).
4 *
5 * Order:
6 *   1. STRATA_PYTHON, then STRATA_NATIVE_PYTHON      (an explicit choice is never second-guessed)
7 *   1b. the Control component's own interpreter, when the app ships one (control-runtime.mjs findControl):
8 *      the installed app never looks at the user's Python
9 *   2. the repository's own venv: .venv\Scripts\python.exe on Windows, .venv/bin/python elsewhere
10 *   3. Windows: `py -3` (the launcher), then python.exe; each one must actually start
11 *      (the Microsoft Store alias in WindowsApps exists on PATH but only opens the Store)
12 *      POSIX: Homebrew 3.11 / 3.10, then python3, then python
13 * It returns { command, args } so `py -3` can be spawned as spawn(command, [...args, script]).
14 */
15import { existsSync } from 'node:fs';
16import { spawnSync } from 'node:child_process';
17import { join } from 'node:path';
18
19export function venvPython(repo, platform = process.platform) {
20  return join(repo, '.venv', platform === 'win32' ? 'Scripts' : 'bin', platform === 'win32' ? 'python.exe' : 'python');
21}
22
23/** Does this interpreter start? A bare name is looked up on PATH by the spawn itself. */
24export function pythonStarts(command, args = []) {
25  try {
26    const result = spawnSync(command, [...args, '-c', 'import sys'], { timeout: 15000, windowsHide: true, stdio: 'ignore' });
27    return !result.error && result.status === 0;
28  } catch { return false; }
29}
30
31const cache = new Map();
32
33export function resolvePython({ repo = null, control = null, env = process.env, platform = process.platform, exists = existsSync, starts = pythonStarts, useCache = true } = {}) {
34  const key = JSON.stringify([repo, control?.python ?? null, env.STRATA_PYTHON, env.STRATA_NATIVE_PYTHON, platform]);
35  if (useCache && cache.has(key)) return cache.get(key);
36  const found = (() => {
37    const explicit = env.STRATA_PYTHON || env.STRATA_NATIVE_PYTHON;
38    if (explicit) return { command: explicit, args: [], source: 'environment' };
39    if (control?.python) return { command: control.python, args: [], source: 'bundled' };
40    if (repo) {
41      const venv = venvPython(repo, platform);
42      if (exists(venv)) return { command: venv, args: [], source: 'venv' };
43    }
44    if (platform === 'win32') {
45      if (starts('py', ['-3'])) return { command: 'py', args: ['-3'], source: 'py launcher' };
46      if (starts('python.exe')) return { command: 'python.exe', args: [], source: 'PATH' };
47      return { command: 'python.exe', args: [], source: 'fallback' };
48    }
49    for (const brew of ['/opt/homebrew/bin/python3.11', '/opt/homebrew/bin/python3.10']) {
50      if (exists(brew)) return { command: brew, args: [], source: 'homebrew' };
51    }
52    return { command: 'python3', args: [], source: 'PATH' };
53  })();
54  // A failed lookup (the fallback) is not cached: the person may install Python and retry.
55  if (useCache && found.source !== 'fallback') cache.set(key, found);
56  return found;
57}
58
59/** A shell-ready command prefix such as `"py" -3`, for the few places that run a command string. */
60export function shellPrefix(python) {
61  return [`"${python.command}"`, ...python.args].join(' ');
62}