okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
76e51103c
main
1#!/usr/bin/env python3 2"""Check source CLI dispatch without optional terminal or harness packages. 3 4This runs Python with -S against a loopback telemetry fixture. It checks lazy 5client dispatch and early serving failure, not an actual engine or installation. 6""" 7 8from __future__import annotations 9 10import argparse 11import json 12import os 13import platform 14import subprocess 15import sys 16import tempfile 17import threading 18from pathlib import Path 19 20from portable_monitor_smoke import _server 21 22 23ROOT = Path (__file__ ).resolve ().parents [1 ] 24 25 26def _run (* args :str )-> subprocess .CompletedProcess [str ]: 27env = os .environ .copy () 28env .pop ("PYTHONPATH" ,None ) 29env .pop ("PYTHONHOME" ,None ) 30env ["PYTHONNOUSERSITE" ]= "1" 31return subprocess .run ([sys .executable ,"-S" ,* args ],cwd = ROOT ,env = env , 32capture_output = True ,text = True ,timeout = 20 ,check = False ) 33 34 35def _record (result :subprocess .CompletedProcess [str ],expected_exit :int )-> dict : 36if result .returncode != expected_exit or result .stderr .strip (): 37raise RuntimeError (f"CLI exit { result . returncode } , stderr { result . stderr . strip ()!r } " ) 38try : 39value = json .loads (result .stdout ) 40except json .JSONDecodeError as exc : 41raise RuntimeError ("CLI did not write exactly one JSON record" )from exc 42if not isinstance (value ,dict )or value .get ("schema_version" )!= 1 : 43raise RuntimeError ("CLI JSON schema was missing" ) 44return value 45 46 47def smoke ()-> dict : 48probe = _run ("-c" ,"import importlib.util,json,sys; print(json.dumps({" 49"'no_site':sys.flags.no_site,'rich':importlib.util.find_spec('rich') is not None," 50"'textual':importlib.util.find_spec('textual') is not None}))" ) 51if probe .returncode or probe .stderr .strip (): 52raise RuntimeError ("could not establish the no-site-packages test environment" ) 53extras = json .loads (probe .stdout ) 54if extras != {"no_site" :1 ,"rich" :False ,"textual" :False }: 55raise RuntimeError ("optional terminal packages are visible in the test environment" ) 56 57help_result = _run (str (ROOT / "strata.py" ),"--help" ) 58if (help_result .returncode or help_result .stderr .strip ()or 59not all (word in help_result .stdout for word in ("serve" ,"monitor" ,"code" ))): 60raise RuntimeError ("source CLI help failed without optional packages" ) 61 62server ,paths ,_ = _server () 63thread = threading .Thread (target = server .serve_forever ,daemon = True ) 64thread .start () 65try : 66url = f"http://127.0.0.1: { server . server_address [ 1 ] } " 67status = _record (_run (str (ROOT / "strata.py" ),"status" ,"--url" ,url ,"--json" ),0 ) 68once = _record (_run (str (ROOT / "strata.py" ),"monitor" ,"--url" ,url , 69"--once" ,"--output" ,"json" ),0 ) 70for value in (status ,once ): 71if (value .get ("ok" )is not True or 72 (value .get ("snapshot" , {}).get ("instance" )or {}).get ("id" )!= "portable-smoke" or 73 (value .get ("snapshot" , {}).get ("model" )or {}).get ("id" )!= "portable-smoke-model" ): 74raise RuntimeError ("one-shot CLI showed the wrong telemetry fixture" ) 75if paths != ["GET /v1/telemetry" ,"GET /v1/telemetry" ]: 76raise RuntimeError ("one-shot commands sent unexpected HTTP requests" ) 77finally : 78server .shutdown () 79server .server_close () 80thread .join (timeout = 2 ) 81 82with tempfile .TemporaryDirectory (prefix = "strata-minimal-cli-" )as directory : 83missing = str (Path (directory )/ "missing-config.json" ) 84# serve needs the API server's packages; without them strata.py stops in its 85# preflight (exit 5, a plain message, nothing installed) before reading the config. 86serve = _run (str (ROOT / "strata.py" ),"serve" ,"--profile" ,missing , 87"--offline" ,"--output" ,"jsonl" ) 88if (serve .returncode != 5 or serve .stdout .strip ()or 89"missing packages the API server needs" not in serve .stderr or 90"never install anything" not in serve .stderr ): 91raise RuntimeError (f"serve without server packages did not stop in the preflight: " 92f"exit { serve . returncode } , stderr { serve . stderr . strip ()!r } " ) 93code = _record (_run (str (ROOT / "strata.py" ),"code" ,"run" ,"--prompt-file" , 94str (Path (directory )/ "missing-prompt.txt" ),"--policy" ,"review" , 95"--output" ,"jsonl" ),5 ) 96if (code .get ("ok" )is not False or 97 (code .get ("error" )or {}).get ("code" )!= 5 or 98"harness component" not in (code .get ("error" )or {}).get ("message" ,"" )): 99raise RuntimeError ("missing harness did not return actionable exit 5" ) 100 101return {"schema_version" :1 ,"scope" :"source_cli_without_site_packages" , 102"platform" :platform .platform (),"python" :platform .python_version (), 103"optional_packages_visible" : {"rich" :False ,"textual" :False }, 104"commands" : ["--help" ,"status --json" ,"monitor --once --output json" , 105"serve --offline --output jsonl (server packages absent: preflight exit 5)" , 106"code run --output jsonl (missing harness)" ], 107"telemetry_gets" :len (paths ),"non_gets" :0 , 108"actual_engine_started" :False ,"passed" :True } 109 110 111def main ()-> int : 112parser = argparse .ArgumentParser (description = __doc__ ) 113parser .add_argument ("--report" ,type = Path ,required = True ,help = "new absolute JSON report path" ) 114args = parser .parse_args () 115report = args .report .expanduser () 116if not report .is_absolute ()or report .exists (): 117parser .error ("--report must be an absolute new file" ) 118result = smoke () 119report .parent .mkdir (parents = True ,exist_ok = True ) 120descriptor = os .open (report ,os .O_WRONLY | os .O_CREAT | os .O_EXCL ,0o600 ) 121with os .fdopen (descriptor ,"w" ,encoding = "utf-8" )as stream : 122json .dump (result ,stream ,indent = 2 ) 123stream .write ("\n" ) 124json .dumps ({"report" :str (report ),"passed" :result ["passed" ]})) 125return 0 126 127 128if __name__ == "__main__" : 129raise SystemExit (main ())