Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
desktop: ship the Control component (bundled CPython + locked wheels) in the installer, set STRATA_NO_ACQUIRE and STRATA_CONTROL_DIR for the host, clean host stop through Control, self-test checks for Control and the no-fetch engine notice
Niko12214736323d8699
diff --git a/.gitignore b/.gitignoreindex 0d1455d9b..2de9d26c7 100644--- a/.gitignore+++ b/.gitignore@@ -75,6 +75,9 @@ node_modules//apps/desktop/src-tauri/target//apps/desktop/src-tauri/resources/node/*!/apps/desktop/src-tauri/resources/node/.gitignore+# the Control component (bundled CPython + wheels + strata_control) is built by apps/desktop/scripts/stage-control.py+/apps/desktop/src-tauri/resources/control/*+!/apps/desktop/src-tauri/resources/control/.gitignore# screenshots and checkpoints from the Mac rebuild live outside git (StrataSplit\evidence-archive)/apps/host/docs/evidence//apps/host/docs/checkpoints/diff --git a/apps/desktop/scripts/stage-control.py b/apps/desktop/scripts/stage-control.pynew file mode 100644index 000000000..33b005606--- /dev/null+++ b/apps/desktop/scripts/stage-control.py@@ -0,0 +1,138 @@+#!/usr/bin/env python3+"""Stage the Control component the installer ships: src-tauri/resources/control.++ python apps/desktop/scripts/stage-control.py # build the terminal bundle, stage it+ python apps/desktop/scripts/stage-control.py --archive X.zip # stage an archive you already built+ python apps/desktop/scripts/stage-control.py --wheelhouse DIR # build with no network (wheels from DIR)+ python apps/desktop/scripts/stage-control.py --check # verify what is staged++What lands in `control/`: the pinned CPython 3.12.15 (python-build-standalone, digest-checked), the hash-locked wheels+(`packaging/locks/runtime-<target>-cp312.txt`, installed with --require-hashes at build time), strata_control,+strata_cli, serve, tools/strata_tokenizer.py + gguf_reader.py, and BUNDLE.json (version, interpreter and lock digests,+the digest of the archive it came from). It is the same bundle as the Terminal archive (packaging/package_terminal.py):+one Python runtime-core for both products. The app then never runs pip, npm, git or a compiler to start or serve.++This script is a BUILD step. It may download the interpreter and the wheels (verified); the app never does.+"""++from __future__ import annotations++import argparse+import hashlib+import json+import shutil+import subprocess+import sys+import tempfile+import zipfile+import tarfile+from pathlib import Path++HERE = Path(__file__).resolve().parent+ROOT = HERE.parents[2]+OUT = HERE.parent / "src-tauri" / "resources" / "control"+sys.path.insert(0, str(ROOT / "packaging"))+import package_terminal as pt # noqa: E402+++def sha256(path: Path) -> str:+ digest = hashlib.sha256()+ with path.open("rb") as stream:+ for block in iter(lambda: stream.read(1 << 20), b""):+ digest.update(block)+ return digest.hexdigest()+++def build_archive(work: Path, wheelhouse: str | None) -> Path:+ command = [sys.executable, str(ROOT / "packaging" / "package_terminal.py"), "--out", str(work)]+ if wheelhouse:+ command += ["--wheelhouse", wheelhouse]+ subprocess.run(command, check=True, cwd=str(ROOT))+ archives = sorted(work.glob("strata-terminal-*.zip")) + sorted(work.glob("strata-terminal-*.tar.gz"))+ if len(archives) != 1:+ raise SystemExit(f"expected one archive in {work}, found {archives}")+ return archives[0]+++def extract(archive: Path, destination: Path) -> Path:+ """Unpack the archive; returns the folder that holds python/ and strata.py."""+ scratch = destination.parent / (destination.name + ".incoming")+ shutil.rmtree(scratch, ignore_errors=True)+ scratch.mkdir(parents=True)+ if archive.name.endswith(".zip"):+ with zipfile.ZipFile(archive) as zf:+ for member in zf.namelist():+ target = (scratch / member).resolve()+ if scratch.resolve() not in target.parents and target != scratch.resolve():+ raise SystemExit(f"unsafe member in {archive.name}: {member}")+ zf.extractall(scratch)+ else:+ with tarfile.open(archive) as tf:+ tf.extractall(scratch, filter="data")+ (inner,) = [p for p in scratch.iterdir() if p.is_dir()]+ return inner+++def write_bundle_record(folder: Path, archive: Path) -> dict:+ manifest_path = archive.with_name(archive.name.rsplit(".", 2 if archive.name.endswith(".tar.gz") else 1)[0] + ".manifest.json")+ manifest = json.loads(manifest_path.read_text(encoding="utf-8")) if manifest_path.exists() else {}+ target = manifest.get("target") or pt.host_target()+ record = {+ "schema_version": 1, "component": "strata-control",+ "version": (folder / "VERSION").read_text(encoding="utf-8").strip(),+ "target": target,+ "python": {"version": pt.PYTHON_VERSION, "build": pt.PBS_RELEASE, "sha256": pt.PYTHON_PINS[target]["sha256"]},+ "lock": manifest.get("lock"),+ "installed": manifest.get("installed"),+ "archive": {"name": archive.name, "sha256": sha256(archive), "bytes": archive.stat().st_size},+ "acquires_at_runtime": False,+ }+ (folder / "BUNDLE.json").write_text(json.dumps(record, indent=1) + "\n", encoding="utf-8")+ return record+++def check(folder: Path) -> int:+ target = pt.host_target()+ python = folder / pt.PYTHON_PINS[target]["python"]+ problems = []+ for need in (python, folder / "strata.py", folder / "strata_control", folder / "serve" / "server.py", folder / "BUNDLE.json"):+ if not need.exists():+ problems.append(f"missing {need}")+ if problems:+ print("\n".join(problems), file=sys.stderr)+ return 1+ env = {"PYTHONNOUSERSITE": "1", "PYTHONDONTWRITEBYTECODE": "1", "STRATA_NO_ACQUIRE": "1", "SYSTEMROOT": __import__("os").environ.get("SYSTEMROOT", "")}+ done = subprocess.run([str(python), str(folder / "strata.py"), "--version"], capture_output=True, text=True, env=env,+ cwd=str(folder), timeout=60)+ print(done.stdout.strip() or done.stderr.strip())+ return done.returncode+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--archive", type=Path, help="stage this terminal archive instead of building one")+ ap.add_argument("--wheelhouse", help="build with the wheels in this folder (no network)")+ ap.add_argument("--out", type=Path, default=OUT)+ ap.add_argument("--check", action="store_true", help="verify the staged component and exit")+ a = ap.parse_args()+ if a.check:+ return check(a.out)+ with tempfile.TemporaryDirectory(prefix="strata-stage-control-") as work:+ archive = a.archive.resolve() if a.archive else build_archive(Path(work), a.wheelhouse)+ keep = [p for p in a.out.iterdir() if p.name == ".gitignore"] if a.out.exists() else []+ if a.out.exists():+ for child in a.out.iterdir():+ if child.name != ".gitignore":+ shutil.rmtree(child, ignore_errors=True) if child.is_dir() else child.unlink()+ a.out.mkdir(parents=True, exist_ok=True)+ inner = extract(archive, a.out)+ record = write_bundle_record(inner, archive)+ for child in inner.iterdir():+ shutil.move(str(child), str(a.out / child.name))+ shutil.rmtree(inner.parent, ignore_errors=True)+ print(f"staged {record['component']} {record['version']} ({record['target']}) into {a.out}")+ return check(a.out)+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/apps/desktop/src-tauri/resources/control/.gitignore b/apps/desktop/src-tauri/resources/control/.gitignorenew file mode 100644index 000000000..cde6039c8--- /dev/null+++ b/apps/desktop/src-tauri/resources/control/.gitignore@@ -0,0 +1,3 @@+# built by apps/desktop/scripts/stage-control.py, never committed+*+!.gitignorediff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rsindex ce8c66247..4d6e1e295 100644--- a/apps/desktop/src-tauri/src/main.rs+++ b/apps/desktop/src-tauri/src/main.rs@@ -30,6 +30,43 @@ struct HostProcess(Mutex<Option<Child>>);/// The port the App Host bound, so the window (and the frontend) can be pointed at it.struct HostPort(Mutex<Option<u16>>);+/// The secret that lets this shell (and nothing else) ask the host for a clean stop.+struct ShutdownToken(Mutex<Option<String>>);++/// 256 random bits as hex, from the standard library's per-process random hasher keys (no extra crate).+fn random_token() -> String {+ use std::collections::hash_map::RandomState;+ use std::hash::{BuildHasher, Hasher};+ (0..4)+ .map(|i| {+ let mut h = RandomState::new().build_hasher();+ h.write_u32(i);+ h.write_u128(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0));+ format!("{:016x}", h.finish())+ })+ .collect()+}++/// The Control component that ships beside the host (`<resources>/control`): the bundled CPython, the locked+/// wheels, strata_control and the API server. None in a development checkout.+fn control_dir(res: &Path) -> Option<PathBuf> {+ let dir = res.join("control");+ let python = if cfg!(windows) { dir.join("python").join("python.exe") } else { dir.join("python").join("bin").join("python3") };+ if python.exists() && dir.join("strata.py").exists() {+ Some(dir)+ } else {+ None+ }+}++/// What every host start gets, packaged or self-test: acquiring is forbidden, Control is where the bundle put it.+fn apply_runtime_policy(cmd: &mut Command, res: &Path) {+ cmd.env("STRATA_NO_ACQUIRE", "1");+ if let Some(dir) = control_dir(res) {+ cmd.env("STRATA_CONTROL_DIR", dir);+ }+}+/// Ask the OS for an unused loopback port.////// The port is released before the host binds it, so there is a small race; the host reports a@@ -139,6 +176,40 @@ fn wait_for_health(Err(format!("the Strata App Host did not become healthy at {url}: {last}"))}+/// A minimal blocking HTTP POST with no body (the shell's one clean-stop request). Returns the status line.+fn http_post(url: &str, headers: &[(&str, &str)]) -> Result<String, String> {+ http_request("POST", url, headers).map(|(status, _)| status)+}++/// GET returning the response body (the self-test reads `/api/control/status`).+fn http_get_body(url: &str) -> Result<(String, String), String> {+ http_request("GET", url, &[])+}++fn http_request(method: &str, url: &str, headers: &[(&str, &str)]) -> Result<(String, String), String> {+ use std::io::{Read, Write};+ let rest = url.strip_prefix("http://").ok_or("only http:// is supported")?;+ let (authority, path) = match rest.find('/') {+ Some(i) => (&rest[..i], &rest[i..]),+ None => (rest, "/"),+ };+ let mut stream = std::net::TcpStream::connect_timeout(+ &authority.parse().map_err(|e| format!("bad authority {authority}: {e}"))?,+ Duration::from_millis(800),+ )+ .map_err(|e| e.to_string())?;+ stream.set_read_timeout(Some(Duration::from_secs(20))).map_err(|e| e.to_string())?;+ let extra: String = headers.iter().map(|(k, v)| format!("{k}: {v}\r\n")).collect();+ stream+ .write_all(format!("{method} {path} HTTP/1.1\r\nHost: {authority}\r\n{extra}Content-Length: 0\r\nConnection: close\r\n\r\n").as_bytes())+ .map_err(|e| e.to_string())?;+ let mut buf = String::new();+ stream.read_to_string(&mut buf).map_err(|e| e.to_string())?;+ let status = buf.lines().next().unwrap_or_default().to_string();+ let body = buf.split_once("\r\n\r\n").map(|(_, b)| b.to_string()).unwrap_or_default();+ Ok((status, body))+}+/// A minimal blocking HTTP GET, so the shell needs no async runtime for one health probe.fn ureq_get_ok(url: &str) -> Result<bool, String> {use std::io::{Read, Write};@@ -203,6 +274,10 @@ fn start_host(app: &tauri::AppHandle) -> Result<u16, String> {.stdin(Stdio::null()).stdout(Stdio::inherit()).stderr(Stdio::inherit());+ apply_runtime_policy(&mut cmd, &res);+ let token = random_token();+ cmd.env("STRATA_SHUTDOWN_TOKEN", &token);+ *app.state::<ShutdownToken>().0.lock().unwrap() = Some(token);hide_console(&mut cmd);// A user with the Strata API worker on a different port sets this; unset means the worker's// documented loopback default.@@ -234,8 +309,23 @@ fn start_host(app: &tauri::AppHandle) -> Result<u16, String> {/// deliberately not touched here: quitting the window must not kill inference another client is/// using.fn stop_host(app: &tauri::AppHandle) {+ let port = app.try_state::<HostPort>().and_then(|s| *s.0.lock().unwrap());+ let token = app.try_state::<ShutdownToken>().and_then(|s| s.0.lock().unwrap().clone());if let Some(state) = app.try_state::<HostProcess>() {if let Some(mut child) = state.0.lock().unwrap().take() {+ // A clean stop first: the host stops, through Control, the engines this app started, then exits.+ // Engines another client started (the Terminal) are not touched. If it does not finish in time the+ // process is ended as before.+ if let (Some(port), Some(token)) = (port, token) {+ let _ = http_post(&format!("http://127.0.0.1:{port}/api/control/shutdown"), &[("x-strata-shutdown", token.as_str())]);+ let deadline = Instant::now() + Duration::from_secs(80);+ while Instant::now() < deadline {+ if let Ok(Some(_)) = child.try_wait() {+ return;+ }+ std::thread::sleep(Duration::from_millis(100));+ }+ }let _ = child.kill();let _ = child.wait();}@@ -277,6 +367,49 @@ fn self_test_path(args: &[String]) -> Option<String> {None}+/// Run `strata.py --version` on the bundle's own interpreter with a scrubbed environment.+fn control_runs(dir: &Path) -> (bool, String) {+ let python = if cfg!(windows) { dir.join("python").join("python.exe") } else { dir.join("python").join("bin").join("python3") };+ let mut cmd = Command::new(&python);+ cmd.arg(dir.join("strata.py")).arg("--version").current_dir(dir)+ .env_remove("PYTHONHOME").env_remove("PYTHONPATH").env_remove("VIRTUAL_ENV")+ .env("PYTHONNOUSERSITE", "1").env("PYTHONDONTWRITEBYTECODE", "1").env("STRATA_NO_ACQUIRE", "1");+ hide_console(&mut cmd);+ match cmd.output() {+ Ok(out) => {+ let text = String::from_utf8_lossy(&out.stdout).trim().to_string();+ let ok = out.status.success() && text.contains("python 3.12") && text.contains(&dir.display().to_string());+ (ok, if text.is_empty() { String::from_utf8_lossy(&out.stderr).trim().to_string() } else { text })+ }+ Err(e) => (false, format!("could not start {}: {e}", python.display())),+ }+}++/// With an empty component folder Control must say there is no engine and name the explicit command, exit 5,+/// and must not create or fetch anything.+fn control_engine_notice(dir: &Path) -> (bool, String) {+ let python = if cfg!(windows) { dir.join("python").join("python.exe") } else { dir.join("python").join("bin").join("python3") };+ let empty = std::env::temp_dir().join(format!("strata-self-test-components-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&empty);+ let mut cmd = Command::new(&python);+ cmd.arg(dir.join("strata.py")).args(["components", "engine", "--backend", "cuda", "--output", "json"])+ .arg("--components-dir").arg(&empty).current_dir(dir)+ .env_remove("PYTHONHOME").env_remove("PYTHONPATH")+ .env("PYTHONNOUSERSITE", "1").env("PYTHONDONTWRITEBYTECODE", "1").env("STRATA_NO_ACQUIRE", "1");+ hide_console(&mut cmd);+ let result = match cmd.output() {+ Ok(out) => {+ let text = String::from_utf8_lossy(&out.stdout).to_string();+ let notice = text.contains("never downloads an engine");+ let created = empty.exists();+ (out.status.code() == Some(5) && notice && !created, format!("exit {:?}; notice={notice}; folder created={created}", out.status.code()))+ }+ Err(e) => (false, e.to_string()),+ };+ let _ = std::fs::remove_dir_all(&empty);+ result+}+/// Start the host from the packaged resources and report what actually happened.fn run_self_test(report_path: &str) -> i32 {let mut steps: Vec<(String, bool, String)> = Vec::new();@@ -348,6 +481,7 @@ fn run_self_test(report_path: &str) -> i32 {.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());+ apply_runtime_policy(&mut cmd, &res);hide_console(&mut cmd);let mut child = match cmd.spawn() {Ok(c) => {@@ -378,6 +512,29 @@ fn run_self_test(report_path: &str) -> i32 {let api_ok = ureq_get_ok(&format!("http://127.0.0.1:{port}/api/health")).unwrap_or(false);record("api-reachable-on-same-origin", api_ok, "GET /api/health".into());+ // The bundled Control component: present, runs on its own interpreter, and the host sees it with acquiring forbidden.+ match control_dir(&res) {+ Some(dir) => {+ record("control-component-present", true, dir.display().to_string());+ let (ok, detail) = control_runs(&dir);+ record("control-runs-on-bundled-python", ok, detail);+ let (ok, detail) = control_engine_notice(&dir);+ record("engine-missing-is-a-notice-not-a-download", ok, detail);+ }+ None => record("control-component-present", false, format!("no control component under {}", res.display())),+ }+ let status = http_get_body(&format!("http://127.0.0.1:{port}/api/control/status"));+ let (status_ok, status_detail) = match &status {+ Ok((line, body)) => {+ let parsed: serde_json::Value = serde_json::from_str(body).unwrap_or(serde_json::Value::Null);+ let forbidden = parsed["acquisition"]["forbidden"] == serde_json::Value::Bool(true);+ let control = parsed["control"]["python"].is_string();+ (line.contains(" 200") && forbidden && control, format!("{line}; acquisition.forbidden={forbidden}; control={control}"))+ }+ Err(e) => (false, e.clone()),+ };+ record("host-sees-control-and-forbids-acquiring", status_ok, status_detail);+let _ = child.kill();let _ = child.wait();@@ -436,6 +593,7 @@ fn main() {.plugin(tauri_plugin_shell::init()).manage(HostProcess(Mutex::new(None))).manage(HostPort(Mutex::new(None)))+ .manage(ShutdownToken(Mutex::new(None))).setup(|app| {let handle = app.handle().clone();let port = start_host(&handle).map_err(|e| {@@ -502,6 +660,28 @@ mod tests {assert_eq!(plain_path(PathBuf::from("/usr/lib/Strata")), PathBuf::from("/usr/lib/Strata"));}+ #[test]+ fn control_is_found_only_when_whole() {+ let res = scratch("control");+ assert!(control_dir(&res).is_none());+ let dir = res.join("control");+ let py = if cfg!(windows) { dir.join("python").join("python.exe") } else { dir.join("python").join("bin").join("python3") };+ std::fs::create_dir_all(py.parent().unwrap()).unwrap();+ std::fs::write(&py, b"").unwrap();+ assert!(control_dir(&res).is_none(), "no strata.py yet");+ std::fs::write(dir.join("strata.py"), b"").unwrap();+ assert_eq!(control_dir(&res), Some(dir));+ let _ = std::fs::remove_dir_all(&res);+ }++ #[test]+ fn shutdown_tokens_are_long_and_different() {+ let (a, b) = (random_token(), random_token());+ assert_eq!(a.len(), 64);+ assert_ne!(a, b);+ assert!(a.chars().all(|c| c.is_ascii_hexdigit()));+ }+#[test]fn a_bare_node_is_not_taken_for_the_windows_runtime() {if !cfg!(windows) {diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.jsonindex a7b03f961..0977bf52f 100644--- a/apps/desktop/src-tauri/tauri.conf.json+++ b/apps/desktop/src-tauri/tauri.conf.json@@ -33,7 +33,8 @@"../../host/dist": "host/dist","../../host/server": "host/server","../../host/package.json": "host/package.json",- "resources/node": "node"+ "resources/node": "node",+ "resources/control": "control"},"macOS": {"minimumSystemVersion": "11.0",