okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
be885026f
main
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 ) { 20return 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 = []) { 25try { 26const result = spawnSync ( command , [ ...args , '-c' , 'import sys' ], { timeout :15000 , windowsHide :true , stdio :'ignore' }); 27return ! 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 } = {}) { 34const key = JSON . stringify ([ repo , control ?. python ?? null , env . STRATA_PYTHON , env . STRATA_NATIVE_PYTHON , platform ]); 35if ( useCache && cache . has ( key )) return cache . get ( key ); 36const found = (() => { 37const explicit = env . STRATA_PYTHON || env . STRATA_NATIVE_PYTHON ; 38if ( explicit ) return { command :explicit , args :[], source :'environment' }; 39if ( control ?. python ) return { command :control . python , args :[], source :'bundled' }; 40if ( repo ) { 41const venv = venvPython ( repo , platform ); 42if ( exists ( venv )) return { command :venv , args :[], source :'venv' }; 43} 44if ( platform === 'win32' ) { 45if ( starts ( 'py' , [ '-3' ])) return { command :'py' , args :[ '-3' ], source :'py launcher' }; 46if ( starts ( 'python.exe' )) return { command :'python.exe' , args :[], source :'PATH' }; 47return { command :'python.exe' , args :[], source :'fallback' }; 48} 49for ( const brew of [ '/opt/homebrew/bin/python3.11' , '/opt/homebrew/bin/python3.10' ]) { 50if ( exists ( brew )) return { command :brew , args :[], source :'homebrew' }; 51} 52return { command :'python3' , args :[], source :'PATH' }; 53})(); 54// A failed lookup (the fallback) is not cached: the person may install Python and retry. 55if ( useCache && found . source !== 'fallback' ) cache . set ( key , found ); 56return 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 ) { 61return [ `" ${ python . command } "` , ...python . args ]. join ( ' ' ); 62}