|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import datetime as dt |
| 4 | +import json |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +from pathlib import Path |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +PORTABLE_LAYOUT_VERSION = "sourceos.portable-ai/v1alpha1" |
| 11 | + |
| 12 | +PORTABLE_PROFILES: dict[str, dict[str, Any]] = { |
| 13 | + "tiny-router": {"displayName": "Tiny Router Kit", "minimumFreeGb": 8, "recommendedFreeGb": 16}, |
| 14 | + "laptop-safe": {"displayName": "Laptop-safe Portable AI Kit", "minimumFreeGb": 16, "recommendedFreeGb": 32}, |
| 15 | + "office-local": {"displayName": "Office-local Portable AI Kit", "minimumFreeGb": 32, "recommendedFreeGb": 64}, |
| 16 | + "code-local": {"displayName": "Code-local Portable AI Kit", "minimumFreeGb": 32, "recommendedFreeGb": 64}, |
| 17 | + "field-kit": {"displayName": "Field Operator Portable AI Kit", "minimumFreeGb": 64, "recommendedFreeGb": 128}, |
| 18 | + "byom-gguf": {"displayName": "Bring-your-own GGUF Portable Kit", "minimumFreeGb": 8, "recommendedFreeGb": 64}, |
| 19 | +} |
| 20 | + |
| 21 | +PORTABLE_DIRS = ["manifests", "models/blobs", "cache", "state/routes", "evidence/preflight", "evidence/materialization", "tmp"] |
| 22 | + |
| 23 | + |
| 24 | +def _now() -> str: |
| 25 | + return dt.datetime.now(dt.timezone.utc).isoformat() |
| 26 | + |
| 27 | + |
| 28 | +def _print(payload: dict[str, Any]) -> int: |
| 29 | + print(json.dumps(payload, indent=2, sort_keys=True)) |
| 30 | + return 0 |
| 31 | + |
| 32 | + |
| 33 | +def _root(value: str) -> Path: |
| 34 | + return Path(value).expanduser().resolve() |
| 35 | + |
| 36 | + |
| 37 | +def _disk(path: Path) -> dict[str, float | None]: |
| 38 | + probe = path if path.exists() else path.parent |
| 39 | + try: |
| 40 | + total, used, free = shutil.disk_usage(probe) |
| 41 | + except FileNotFoundError: |
| 42 | + return {"totalGb": None, "usedGb": None, "freeGb": None} |
| 43 | + gb = 1024 ** 3 |
| 44 | + return {"totalGb": round(total / gb, 2), "usedGb": round(used / gb, 2), "freeGb": round(free / gb, 2)} |
| 45 | + |
| 46 | + |
| 47 | +def profiles(_args) -> int: |
| 48 | + return _print({"type": "PortableAIProfiles", "apiVersion": PORTABLE_LAYOUT_VERSION, "profiles": PORTABLE_PROFILES}) |
| 49 | + |
| 50 | + |
| 51 | +def preflight(args) -> int: |
| 52 | + target = _root(args.target_root) |
| 53 | + profile_name = getattr(args, "profile", "laptop-safe") |
| 54 | + profile = PORTABLE_PROFILES[profile_name] |
| 55 | + disk = _disk(target) |
| 56 | + parent = target if target.exists() else target.parent |
| 57 | + writable = parent.exists() and os.access(parent, os.W_OK) |
| 58 | + failures: list[str] = [] |
| 59 | + warnings: list[str] = [] |
| 60 | + free_gb = disk.get("freeGb") |
| 61 | + if not writable: |
| 62 | + failures.append("target parent is not writable") |
| 63 | + if free_gb is not None and free_gb < profile["minimumFreeGb"]: |
| 64 | + failures.append("free space below profile minimum") |
| 65 | + elif free_gb is not None and free_gb < profile["recommendedFreeGb"]: |
| 66 | + warnings.append("free space below profile recommendation") |
| 67 | + return _print({ |
| 68 | + "type": "PortablePreflightEvidence", |
| 69 | + "apiVersion": PORTABLE_LAYOUT_VERSION, |
| 70 | + "capturedAt": _now(), |
| 71 | + "targetRoot": str(target), |
| 72 | + "profile": profile_name, |
| 73 | + "disk": disk, |
| 74 | + "writable": writable, |
| 75 | + "failures": failures, |
| 76 | + "warnings": warnings, |
| 77 | + "decision": "fail" if failures else "warn" if warnings else "pass", |
| 78 | + }) |
| 79 | + |
| 80 | + |
| 81 | +def prepare(args) -> int: |
| 82 | + target = _root(args.target_root) |
| 83 | + profile_name = args.profile |
| 84 | + return _print({ |
| 85 | + "type": "PortablePreparePlan", |
| 86 | + "apiVersion": PORTABLE_LAYOUT_VERSION, |
| 87 | + "capturedAt": _now(), |
| 88 | + "targetRoot": str(target), |
| 89 | + "profile": profile_name, |
| 90 | + "wouldCreateDirectories": [str(target / rel) for rel in PORTABLE_DIRS], |
| 91 | + "wouldWriteManifest": str(target / "manifests" / "portable-ai-root.json"), |
| 92 | + "wouldStartProvider": False, |
| 93 | + "wouldFetchRemoteModels": False, |
| 94 | + }) |
| 95 | + |
| 96 | + |
| 97 | +def inspect(args) -> int: |
| 98 | + target = _root(args.target_root) |
| 99 | + return _print({"type": "PortableAIInspect", "apiVersion": PORTABLE_LAYOUT_VERSION, "targetRoot": str(target), "exists": target.exists()}) |
| 100 | + |
| 101 | + |
| 102 | +def evidence_inspect(args) -> int: |
| 103 | + path = Path(args.path).expanduser() |
| 104 | + payload = json.loads(path.read_text(encoding="utf-8")) |
| 105 | + return _print({"path": str(path), "type": payload.get("type"), "apiVersion": payload.get("apiVersion")}) |
0 commit comments