diff --git a/backend/snapstudio_api/routes_assemblies.py b/backend/snapstudio_api/routes_assemblies.py new file mode 100644 index 00000000..5f396332 --- /dev/null +++ b/backend/snapstudio_api/routes_assemblies.py @@ -0,0 +1,129 @@ +"""Routes of the Assemblies track: plan a multi-tile project against shared spools. + + POST /assemblies/evaluate {assembly, supply?, now?} -> {report} + POST /assemblies/save {assembly, id?, expected_revision?} -> {id, revision, assembly} + POST /assemblies/list {} -> {assemblies} + POST /assemblies/get {id} -> {id, revision, assembly} + POST /assemblies/delete {id} -> {deleted} + +Everything here is local arithmetic and local files: no network request of its own, no printer, no +model file is written (file-path checks are best-effort, see snapstudio_core/localpath.py). The library is only read (to say whether a tile's project is still there and unchanged). +The verdicts are advisory. +""" +from __future__ import annotations + +import hashlib +import os +from snapstudio_core import localpath +import sqlite3 + +from snapstudio_core import paths +from snapstudio_core.assemblies import evaluate as ev +from snapstudio_core.assemblies import store +from snapstudio_core.assemblies.model import AssemblyInputError, read_assembly +from snapstudio_core.foundation.decode import is_finite_number + +from . import ext_routes +from . import request_validation as rv + +#: A library file larger than this is not re-hashed inside a request; its tile says "unverified". +MAX_HASH_BYTES = 256 * 1024 * 1024 + + +def _refused(exc: AssemblyInputError) -> rv.ValidationError: + return rv.ValidationError(str(exc)) + + +def _id(data: dict) -> int: + value = data.get("id") + if isinstance(value, bool) or not isinstance(value, int) or value < 1: + raise rv.ValidationError("Missing or invalid 'id'") + return value + + +def _now(data: dict): + value = data.get("now") + if value is None: + return None + if not is_finite_number(value) or value < 0: + raise rv.ValidationError("Invalid 'now'") + return value + + +def _sha256_of(path: str) -> str | None: + if not localpath.is_local_regular_file(path, max_bytes=MAX_HASH_BYTES): + return None + try: + if os.path.getsize(path) > MAX_HASH_BYTES: + return None + digest = hashlib.sha256() + with open(path, "rb") as fh: + for chunk in iter(lambda: fh.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + except OSError: + return None + + +def library_lookup(project_id: int) -> dict | None: + """The library row for `project_id`, read-only: {sha256} of its source file, or None when it is gone. + + Opens `library.db` read-only and never creates it; a missing library means no project is in it. + """ + db = os.path.join(paths.data_dir(create=False), "library.db") + if not os.path.isfile(db): + return None + uri = "file:" + db.replace("\\", "/").replace("?", "%3f").replace("#", "%23") + "?mode=ro" + try: + conn = sqlite3.connect(uri, uri=True, timeout=2) + try: + row = conn.execute("SELECT source_path FROM projects WHERE id=?", (project_id,)).fetchone() + finally: + conn.close() + except sqlite3.Error: + return {"sha256": None} # a locked or unfamiliar library leaves the tile unverified + if row is None: + return None + return {"sha256": _sha256_of(row[0]) if isinstance(row[0], str) else None} + + +@ext_routes.route("/assemblies/evaluate") +def evaluate(data: dict) -> tuple[int, dict]: + try: + assembly = read_assembly(data.get("assembly")) + now = _now(data) + supply = ev.read_supply(data.get("supply"), now) + except AssemblyInputError as exc: + raise _refused(exc) from None + lookup = library_lookup if any(t.library is not None for t in assembly.tiles) else None + return 200, {"report": ev.evaluate(assembly, supply, now=now, lookup=lookup)} + + +@ext_routes.route("/assemblies/save") +def save(data: dict) -> tuple[int, dict]: + expected = data.get("expected_revision") + if expected is not None and (isinstance(expected, bool) or not isinstance(expected, int) or expected < 0): + raise rv.ValidationError("Invalid 'expected_revision'") + assembly_id = _id(data) if data.get("id") is not None else None + try: + return 200, store.save(data.get("assembly"), assembly_id, expected) + except AssemblyInputError as exc: + raise _refused(exc) from None + + +@ext_routes.route("/assemblies/list") +def list_saved(data: dict) -> tuple[int, dict]: + return 200, {"assemblies": store.list_saved()} + + +@ext_routes.route("/assemblies/get") +def get(data: dict) -> tuple[int, dict]: + try: + return 200, store.get(_id(data)) + except AssemblyInputError as exc: + raise _refused(exc) from None + + +@ext_routes.route("/assemblies/delete") +def delete(data: dict) -> tuple[int, dict]: + return 200, {"deleted": store.delete(_id(data))} diff --git a/backend/snapstudio_api/routes_calibration.py b/backend/snapstudio_api/routes_calibration.py new file mode 100644 index 00000000..41958f35 --- /dev/null +++ b/backend/snapstudio_api/routes_calibration.py @@ -0,0 +1,80 @@ +"""Palette calibration routes. All work is local parsing, comparison and persistence.""" +from __future__ import annotations + +import re + +from snapstudio_core import paths +from snapstudio_core.calibration import palette as palette_reader +from snapstudio_core.calibration import service +from snapstudio_core.foundation.decode import Decoded + +from . import ext_routes +from . import request_validation as rv + +_COLOR = re.compile(r"^#[0-9A-Fa-f]{6}$") + + +def _decoded_dict(decoded: Decoded) -> dict: + return decoded.to_dict() + + +def _colors(data: dict) -> dict[str, str]: + raw = data.get("physical_colors", {}) + if not isinstance(raw, dict) or len(raw) > 256: + raise rv.ValidationError("Invalid 'physical_colors'") + result = {} + for key, value in raw.items(): + if not str(key).isdigit() or not isinstance(value, str) or not _COLOR.fullmatch(value): + raise rv.ValidationError("Invalid 'physical_colors'") + result[str(key)] = value.upper() + return result + + +def _owned(data: dict) -> list[dict]: + rows = data.get("owned_filaments", []) + if not isinstance(rows, list) or len(rows) > 100: + raise rv.ValidationError("Invalid 'owned_filaments'") + result = [] + for row in rows: + if not isinstance(row, dict): + raise rv.ValidationError("Invalid 'owned_filaments'") + color = row.get("color") + label = row.get("label", "Owned filament") + if not isinstance(color, str) or not _COLOR.fullmatch(color) or not isinstance(label, str) or len(label) > 80: + raise rv.ValidationError("Invalid 'owned_filaments'") + result.append({"color": color.upper(), "label": label}) + return result + + +@ext_routes.route("/calibration/evaluate") +def evaluate(data: dict) -> tuple[int, dict]: + source = data.get("source") + if source not in ("ditherforge_txt", "kromacut_json"): + raise rv.ValidationError("Invalid 'source'") + content = data.get("content") + if not isinstance(content, str) or len(content.encode("utf-8")) > 512_000: + raise rv.ValidationError("Invalid 'content'") + decoded = palette_reader.parse_palette(source, content) + if decoded.value is None: + return 200, {"status": "unknown", "decoded": _decoded_dict(decoded), "result": None} + family = data.get("family") + if family not in ("frontlit", "backlit"): + raise rv.ValidationError("Invalid 'family'") + material = rv.require_bounded_str(data, "material", 32) if "material" in data else "PLA" + if not material.strip(): + raise rv.ValidationError("Material must not be empty") + basis = service.basis_document(family, rv.require_positive_float(data, "layer_height_mm"), + rv.optional_positive_float(data, "nozzle_mm", 0.4), + material, + source=source, model=rv.optional_bounded_str(data, "model", 128), + first_layer_height_mm=rv.require_positive_float(data, "first_layer_height_mm") + if "first_layer_height_mm" in data else None) + palette = decoded.value.to_dict() + result = service.evaluate(palette, basis, _colors(data), _owned(data)) + saved = service.save_run(basis, palette, result, data_dir=paths.data_dir()) + return 200, {"status": "evaluated", "decoded": _decoded_dict(decoded), "result": saved} + + +@ext_routes.route("/calibration/history") +def get_history(data: dict) -> tuple[int, dict]: + return 200, {"runs": service.history(data_dir=paths.data_dir())} diff --git a/backend/snapstudio_api/routes_creative.py b/backend/snapstudio_api/routes_creative.py new file mode 100644 index 00000000..35ca7fb4 --- /dev/null +++ b/backend/snapstudio_api/routes_creative.py @@ -0,0 +1,180 @@ +"""Routes of the Creative track: inspect one local creative project (3MF) and say what Studio read. + + POST /creative/inspect {path, format?} -> {origin, understood, passed_through, could_not_interpret, + missing, diagnostics, recipes, source, evidence, advisory} + +The handler opens the file for reading only. It never writes, renames or deletes anything, never +slices, never contacts a printer and makes no network request of its own (the local-path check is +best-effort, see snapstudio_core/localpath.py). `source` carries the SHA-256 of the +file before the readers ran and again afterwards, so the response itself shows the original is +unchanged. Recipes are simulated: `execution_supported` is False and nothing here runs them. An +unrecognised generator is reported as origin "unknown", never guessed. +""" +from __future__ import annotations + +import hashlib +import os + +from snapstudio_core import localpath +from snapstudio_core.creative import container, readers + +from . import ext_routes +from . import request_validation as rv + +#: Same ceiling the 3MF reader enforces; a larger file is refused before it is read. +MAX_FILE_BYTES = container.MAX_ARCHIVE_BYTES +MAX_PATH_CHARS = 1024 + +EVIDENCE = "fixture-tested; fixtures are hand-written to published shapes, not captures" +ADVISORY = ("Advisory read of one project file. Studio does not slice, does not run recipes and " + "does not change the file. What it could not interpret is listed, not guessed.") + +#: format hint -> reader. "auto" picks by the keys the project settings contain. +_READERS = { + "forcaslicer": readers.read_forca, + "lixnix": readers.read_lixnix, + "fullspectrum": readers.read_fullspectrum, + "imagemap": readers.read_imagemap, + "lumina": readers.read_lumina, +} +_FORMATS = ("auto",) + tuple(_READERS) + +_ERROR_TEXT = { + "bad_zip": "That file is not a readable 3MF archive.", + "archive_too_large": "That file is larger than the inspector reads.", + "too_many_members": "That archive holds more parts than the inspector reads.", + "unsafe_member_name": "That archive has a part name that is not safe to read.", + "duplicate_member": "That archive lists the same part name twice.", + "member_too_large": "A part inside that archive is larger than the inspector reads.", + "archive_expanded_too_large": "That archive expands to more data than the inspector reads.", + "compression_ratio_exceeded": "A part inside that archive expands far more than a normal 3MF.", + "bad_zip_member": "A part inside that archive could not be read.", + "truncated_member": "A part inside that archive is truncated.", + "missing_model": "That archive has no 3D model part, so it is not a 3MF project.", + "unsafe_xml": "The model part uses XML features the inspector does not read.", + "bad_xml": "The model part is not valid XML.", + "unsupported_3mf_version": "That 3MF uses a core namespace the inspector does not read.", + "bad_project_settings": "The project settings part is not a JSON object.", +} + + +def _path(data: dict) -> str: + raw = rv.require_path_string(data, "path") + if len(raw) > MAX_PATH_CHARS or "\x00" in raw: + raise rv.ValidationError("Invalid 'path'") + return raw + + +def _format(data: dict) -> str: + value = data.get("format", "auto") + if not isinstance(value, str) or value not in _FORMATS: + raise rv.ValidationError("Invalid 'format'") + return value + + +def _read_source(path: str) -> bytes: + """The file bytes, after checking it is a regular file under the size cap. Read-only open.""" + try: + if localpath.is_network_or_device_path(path) or not localpath.is_local_regular_file(path): + raise rv.ValidationError("That path is not a local file Studio can read.") + if os.path.getsize(path) > MAX_FILE_BYTES: + raise rv.ValidationError("That file is larger than the inspector reads.") + with open(path, "rb") as fh: + blob = fh.read(MAX_FILE_BYTES + 1) + except rv.ValidationError: + raise + except OSError: + raise rv.ValidationError("That file could not be opened.") from None + if len(blob) > MAX_FILE_BYTES: + raise rv.ValidationError("That file is larger than the inspector reads.") + return blob + + +def _detect(settings: dict) -> list[str]: + """Formats whose published keys are present. Several can match; none means unknown.""" + keys = set(settings) + found = [] + if keys & readers._FORCA_KEYS: + found.append("forcaslicer") + if keys & readers._LIX_KEYS: + found.append("lixnix") + if keys & readers._FULLSPECTRUM_KEYS: + found.append("fullspectrum") + return found + + +def _wrap(exc: container.ContainerError) -> rv.ValidationError: + code = str(exc) + return rv.ValidationError(_ERROR_TEXT.get(code, "That file could not be read as a 3MF project.")) + + +@ext_routes.route("/creative/inspect") +def inspect(data: dict) -> tuple[int, dict]: + path = _path(data) + hint = _format(data) + blob = _read_source(path) + before = hashlib.sha256(blob).hexdigest() + try: + manifest = container.read_3mf(blob) + except container.ContainerError as exc: + raise _wrap(exc) from None + + settings = manifest.project_settings or {} + names = [hint] if hint != "auto" else _detect(settings) + projects = [] + try: + for name in names: + projects.append(_READERS[name](blob)) + except container.ContainerError as exc: + raise _wrap(exc) from None + + if not projects: + # Unknown generator: container identity only. Every part is preserved, nothing is interpreted. + passed = [{"name": m.name, "sha256": m.sha256} for m in manifest.passed_through] + passed += [{"name": k, "sha256": readers._digest_obj({k: v})["sha256"]} for k, v in sorted(settings.items())] + origin = {"generator": "unknown", "status": "unknown", "candidates": []} + understood = [p for p in manifest.understood_parts] + unknown: list = [] + missing: list = [] + diagnostics = ["generator_not_recognized"] + recipes: list = [] + slots = None + else: + status = "detected" if len(projects) == 1 else "ambiguous" + origin = {"generator": projects[0].source_format if len(projects) == 1 else "unknown", + "status": status if hint == "auto" else "chosen", + "candidates": [p.source_format for p in projects]} + understood = sorted({k for p in projects for k in p.understood}) + seen = set() + passed = [] + for p in projects: + for item in p.passed_through: + key = (item["name"], item["sha256"]) + if key not in seen: + seen.add(key) + passed.append(item) + # A key one reader understood is not passed through by another reading of the same file. + passed = [i for i in passed if i["name"] not in understood] + unknown = sorted({u for p in projects for u in p.unknown}) + missing = sorted({m for p in projects for m in p.missing}) + diagnostics = [d for p in projects for d in p.diagnostics] + recipes = [r.to_dict() for p in projects for r in p.recipes] + slots = next((p.physical_slots_needed for p in projects if p.recipes), None) + + after = _read_source(path) + after_hash = hashlib.sha256(after).hexdigest() + return 200, { + "origin": origin, + "understood": list(understood), + "passed_through": passed, + "could_not_interpret": list(unknown), + "missing": list(missing), + "diagnostics": list(diagnostics), + "recipes": recipes, + "physical_slots_needed": slots, + "source": {"name": os.path.basename(path), "size_bytes": len(blob), + "sha256_before": before, "sha256_after": after_hash, + "unchanged": before == after_hash}, + "evidence": EVIDENCE, + "advisory": ADVISORY, + } diff --git a/backend/snapstudio_api/routes_passport.py b/backend/snapstudio_api/routes_passport.py new file mode 100644 index 00000000..dc33eeb0 --- /dev/null +++ b/backend/snapstudio_api/routes_passport.py @@ -0,0 +1,85 @@ +"""Routes of the Passport track: build, import and verify a job passport. + + POST /passport/build {entries:[{path, role, parents?}], recipes?, material_mappings?} + -> {passport, paths, diagnostics, losses} + POST /passport/import {passport, base_dir?, paths?} -> {passport, parentage, verification, ...} + POST /passport/verify {passport, base_dir?, paths?} -> {verification} + +Files are only read (hashed). The routes make no network request of their own and never touch a +printer; a path on a mapped network drive, or one redirected as described in +docs/internal/PASSPORT.md ("Path checks are not a sandbox"), is read through the operating system. +A chain built here is `declared` by the user; this version does not see a prepare or slice happen. +`paths` in a response maps artifact ids to the local files they were read from, for the caller's +own later verify call; the passport itself never contains a path. +""" +from __future__ import annotations + +from snapstudio_core import localpath +from snapstudio_core.passport import build_passport, import_passport, verify_passport +from snapstudio_core.passport.model import MAX_ARTIFACTS, decode_passport + +from . import ext_routes +from . import request_validation as rv + +MAX_PATH_CHARS = 1024 +MAX_PATHS = 2 * MAX_ARTIFACTS + + +def _entries(data: dict) -> list: + raw = data.get("entries") + if not isinstance(raw, list) or not 1 <= len(raw) <= MAX_ARTIFACTS: + raise rv.ValidationError("Invalid 'entries'") + out = [] + for item in raw: + if not isinstance(item, dict): + raise rv.ValidationError("Invalid 'entries'") + out.append({"path": rv.require_bounded_str(item, "path", MAX_PATH_CHARS), + "role": rv.require_bounded_str(item, "role", 16), + "parents": item.get("parents"), "producer": item.get("producer")}) + return out + + +def _location_hints(data: dict) -> tuple: + base_dir = rv.optional_bounded_str(data, "base_dir", MAX_PATH_CHARS) + if base_dir is not None and not localpath.is_local_directory(base_dir): + raise rv.ValidationError("Invalid 'base_dir'") + raw = data.get("paths") + if raw is None: + return base_dir or None, {} + if not isinstance(raw, dict) or len(raw) > MAX_PATHS or not all( + isinstance(k, str) and 0 < len(k) <= 128 and isinstance(v, str) and 0 < len(v) <= MAX_PATH_CHARS + for k, v in raw.items()): + raise rv.ValidationError("Invalid 'paths'") + return base_dir or None, dict(raw) + + +def _document(data: dict): + doc = data.get("passport") + if not isinstance(doc, dict): + raise rv.ValidationError("Invalid 'passport'") + return doc + + +@ext_routes.route("/passport/build") +def build(data: dict) -> tuple[int, dict]: + result = build_passport(_entries(data), recipes=data.get("recipes"), + material_mappings=data.get("material_mappings")) + body = result.to_dict() + body["paths"] = result.paths + return 200, body + + +@ext_routes.route("/passport/import") +def import_(data: dict) -> tuple[int, dict]: + base_dir, paths = _location_hints(data) + return 200, import_passport(_document(data), base_dir=base_dir, paths=paths) + + +@ext_routes.route("/passport/verify") +def verify(data: dict) -> tuple[int, dict]: + base_dir, paths = _location_hints(data) + decoded = decode_passport(_document(data)) + if decoded.value is None: + return 200, {"verification": None, "diagnostics": [d.to_dict() for d in decoded.diagnostics]} + return 200, {"verification": verify_passport(decoded.value, base_dir=base_dir, paths=paths), + "diagnostics": [d.to_dict() for d in decoded.diagnostics]} diff --git a/backend/snapstudio_api/routes_recovery.py b/backend/snapstudio_api/routes_recovery.py new file mode 100644 index 00000000..da00ffd2 --- /dev/null +++ b/backend/snapstudio_api/routes_recovery.py @@ -0,0 +1,75 @@ +"""Routes for guarded monitoring context and local workspace recovery.""" +from __future__ import annotations + +import time + +from snapstudio_core import monitoring_context, workspace_recovery +from snapstudio_core import moonraker +from snapstudio_core.errors import SnapStudioError +from snapstudio_core.foundation import store +from snapstudio_core.foundation.observations import to_iso + +from . import ext_routes, request_validation as rv + + +@ext_routes.route("/monitoring/read") +def monitoring_read(data: dict) -> tuple[int, dict]: + host = rv.require_bounded_str(data, "host", 255) + port = rv.require_port(data, "port", 7125) + previous = data.get("previous_state") + if previous is not None and (not isinstance(previous, str) or len(previous) > 64): + raise rv.ValidationError("Invalid 'previous_state'") + try: + result = monitoring_context.read(host, port) + except moonraker.InvalidHost as exc: + raise rv.ValidationError(str(exc)) from exc + if previous is not None: + result["reconciliation"] = monitoring_context.reconcile(previous, result) + return 200, result + + +@ext_routes.route("/monitoring/history") +def monitoring_history(data: dict) -> tuple[int, dict]: + host = rv.require_bounded_str(data, "host", 255) + port = rv.require_port(data, "port", 7125) + try: + return 200, monitoring_context.read_history(host, port) + except moonraker.InvalidHost as exc: + raise rv.ValidationError(str(exc)) from exc + + +@ext_routes.route("/recovery/checkpoint") +def recovery_checkpoint(data: dict) -> tuple[int, dict]: + raw = data.get("workspace", {}) + if not isinstance(raw, dict): + raise rv.ValidationError("Invalid 'workspace'") + observations = data.get("observations", []) + files = data.get("files", []) + jobs = data.get("jobs", []) + if any(not isinstance(value, list) or len(value) > 1000 for value in (observations, files, jobs)): + raise rv.ValidationError("Invalid checkpoint content") + expected = data.get("expected_revision") + if expected is not None and (isinstance(expected, bool) or not isinstance(expected, int)): + raise rv.ValidationError("Invalid 'expected_revision'") + try: + result = workspace_recovery.checkpoint({"workspace": raw, "observations": observations, + "files": files, "jobs": jobs, + "checkpointed_at": to_iso(time.time())}, + expected_revision=expected) + except store.StoreLockError: + raise SnapStudioError("Another Studio process is saving a checkpoint. Try again shortly.") from None + except (store.RevisionConflict, store.StoreVersionError): + raise # dispatch owns these refusal texts + except store.StoreError: + raise SnapStudioError("The checkpoint could not be saved.") from None + return 200, result + + +@ext_routes.route("/recovery/restore") +def recovery_restore(data: dict) -> tuple[int, dict]: + try: + return 200, workspace_recovery.restore() + except store.StoreVersionError: + raise + except store.StoreError: + raise SnapStudioError("The saved checkpoint could not be read.") from None diff --git a/backend/snapstudio_api/routes_variants.py b/backend/snapstudio_api/routes_variants.py new file mode 100644 index 00000000..d28ace47 --- /dev/null +++ b/backend/snapstudio_api/routes_variants.py @@ -0,0 +1,17 @@ +"""Read-only routes for comparing two sliced outputs.""" +from __future__ import annotations + +from snapstudio_core.variants import compare_outputs + +from . import ext_routes +from . import request_validation as rv + + +@ext_routes.route("/variants/compare") +def compare(data: dict) -> tuple[int, dict]: + """Compare exactly two user-selected local outputs.""" + try: + return 200, compare_outputs(data) + except ValueError as exc: + # Unusable input is the caller's to fix: a plain sentence, never the internal-error response. + raise rv.ValidationError(str(exc)) from exc diff --git a/backend/snapstudio_core/assemblies/__init__.py b/backend/snapstudio_core/assemblies/__init__.py new file mode 100644 index 00000000..41afa7a7 --- /dev/null +++ b/backend/snapstudio_core/assemblies/__init__.py @@ -0,0 +1,4 @@ +"""Assembly planning: a multi-tile / multi-part project, and what it needs from shared spools. + +Pure data and arithmetic. Nothing here touches the network, a printer, or an original file. +""" diff --git a/backend/snapstudio_core/assemblies/evaluate.py b/backend/snapstudio_core/assemblies/evaluate.py new file mode 100644 index 00000000..e97770ad --- /dev/null +++ b/backend/snapstudio_core/assemblies/evaluate.py @@ -0,0 +1,281 @@ +"""Cumulative material need of an assembly against shared spools. Advisory. + +Foundation-aware on purpose. Supply is an `Observation` of a spool's remaining grams, and what it +may be leaned on for comes from `observations.freshness` (`usable_for_loadout`). The legacy +`material_plan` sufficiency helper takes plain quantities with no lifetime, so it is not used +here. + +Rules the result follows: + * grams that are not stated stay unknown; they are never counted as zero; + * a spool is one `SpoolRef` (or an alias group), counted once however many slots and tiles use it; + * a slot with no role, an ambiguous role, or a role with no spool binding has no known physical + consumption, so the verdict cannot be "enough"; + * a stale reading is reported as stale: neither a shortage nor "enough". +""" +from __future__ import annotations + +import time +from typing import Any, Callable + +from snapstudio_core.foundation import observations as obs_mod +from snapstudio_core.foundation.decode import is_finite_number +from snapstudio_core.foundation.materials import SpoolRef, same_spool + +from .model import Assembly, AssemblyInputError, grams_or_none, spool_ref + +MAX_SUPPLY = 64 +SCALE_TOLERANCE = 0.001 + +ENOUGH, SHORTAGE, STALE, UNKNOWN = "enough", "shortage", "stale", "unknown" +ADVISORY = ("Advisory only: this adds up the grams the tiles state and compares them with the last stated " + "remaining weight of each shared spool. It does not measure the spool and does not promise a print.") + + +# -- supply ---------------------------------------------------------------------- +def read_supply(raw: Any, now: Any = None) -> list: + """Request supply entries -> [(SpoolRef, Observation)]. Bad shapes are refused; bad times are not: + an unreadable or missing time becomes an unknown/unusable freshness, which the verdict reports.""" + if raw is None: + return [] + if not isinstance(raw, list) or len(raw) > MAX_SUPPLY: + raise AssemblyInputError("supply must be a list of spool readings.") + received = obs_mod.to_iso(time.time() if now is None else float(now)) + out = [] + for i, item in enumerate(raw): + if not isinstance(item, dict): + raise AssemblyInputError("A supply reading must be an object.") + ref = spool_ref(item.get("spool"), "A supply reading's spool") + grams = grams_or_none(item.get("remaining_g"), "remaining_g") + observed = item.get("observed_at") + if observed is not None and (isinstance(observed, bool) or not isinstance(observed, (str, int, float))): + raise AssemblyInputError("observed_at must be a time or null.") + obs = obs_mod.Observation( + observation_id=f"supply:{i}", subject={"kind": "spool", "id": ref.provider_spool_id}, + field="remaining_g", value=grams, source={"provider_instance_id": ref.provider_instance_id, + "kind": "live_query", "source_version": None, "snapshot_id": None}, + observed_at=observed, received_at=received, ttl_s=item.get("ttl_s"), + ageing_ok=bool(item.get("ageing_ok", False)), historical=bool(item.get("historical", False))) + out.append((ref, obs)) + return out + + +# -- tile checks ----------------------------------------------------------------- +LibraryLookup = Callable[[int], "dict | None"] + + +def library_status(tile, lookup: LibraryLookup | None) -> dict: + """linked | changed | missing | unverified | unlinked. The library is asked by project id; no foreign key.""" + ref = tile.library + if ref is None: + return {"status": "unlinked", "detail": "No library project is recorded for this tile."} + if lookup is None: + return {"status": "unverified", "detail": "The library was not checked."} + try: + found = lookup(ref.project_id) + except Exception: # an unreadable library is "not checked", never a crash + return {"status": "unverified", "detail": "The library could not be read."} + if found is None: + return {"status": "missing", "detail": "That library project is no longer in the library."} + current = found.get("sha256") + if ref.sha256 is None or current is None: + return {"status": "unverified", "detail": "The library project exists but its contents were not compared."} + if current.lower() != ref.sha256: + return {"status": "changed", "detail": "The library project's file differs from the one recorded for this tile."} + return {"status": "linked", "detail": "The library project matches the recorded checksum."} + + +def seam_scale(assembly: Assembly) -> dict: + """Tiles that meet at seams should be printed at one scale. Unknown scales stay unknown.""" + known = [(t.tile_id, t.scale) for t in assembly.tiles if t.scale is not None] + unknown = [t.tile_id for t in assembly.tiles if t.scale is None] + mismatch = False + if len(known) > 1: + values = [s for _, s in known] + mismatch = max(values) / min(values) - 1.0 > SCALE_TOLERANCE + if mismatch: + status = "mismatch" + elif unknown: + status = "unknown" + else: + status = "consistent" + return {"status": status, "known": [{"tile_id": t, "scale": s} for t, s in known], "unknown": unknown} + + +def role_summary(assembly: Assembly) -> list: + out = [] + names = sorted({r for t in assembly.tiles for r in t.roles} | set(assembly.bindings)) + for role in names: + tiles = [t for t in assembly.tiles if role in t.roles] + plates = sorted({t.plate for t in tiles if t.plate is not None}) + binding = assembly.bindings.get(role) + out.append({"role": role, "tiles": [t.tile_id for t in tiles], "plates": plates, + "shared_across_plates": len(plates) > 1, + "spool": binding.to_dict() if binding else None}) + return out + + +def parts_report(assembly: Assembly) -> dict: + covered = {t.part_id for t in assembly.tiles if t.part_id} + listed = {p["part_id"] for p in assembly.parts} + return {"missing": [p for p in assembly.parts if p["part_id"] not in covered], + "unlisted_tiles": [t.tile_id for t in assembly.tiles if t.part_id and t.part_id not in listed]} + + +# -- consumption ----------------------------------------------------------------- +def _rows(assembly: Assembly) -> tuple[list, list]: + """Per tile slot: grams (None unknown), the role it serves and the spool bound to that role. + + Returns (rows, unresolved). A tile slot is a slot the tile names a role for or has a gram entry for. + """ + rows, unresolved = [], [] + for tile in assembly.tiles: + by_slot: dict = {} + for role, slot in tile.roles.items(): + by_slot.setdefault(slot, []).append(role) + for slot in sorted(set(by_slot) | set(tile.usage), key=lambda s: int(s.split(":")[1])): + grams = tile.usage.get(slot) + roles = by_slot.get(slot, []) + spool, why = None, None + if len(roles) != 1: + why = "no_role" if not roles else "ambiguous_role" + elif roles[0] not in assembly.bindings: + why = "no_binding" + else: + spool = assembly.bindings[roles[0]] + row = {"tile_id": tile.tile_id, "slot": slot, "role": roles[0] if len(roles) == 1 else None, + "grams": grams, "spool": spool, "unresolved": why} + rows.append(row) + if why and grams != 0.0: + unresolved.append({"tile_id": tile.tile_id, "slot": slot, "grams": grams, "reason": why, + "roles": roles}) + return rows, unresolved + + +def _group_key(ref: SpoolRef, groups: list, aliases: list) -> int: + for i, members in enumerate(groups): + if any(same_spool(ref, m, aliases) for m in members): + members.append(ref) + return i + groups.append([ref]) + return len(groups) - 1 + + +def _supply_for(members: list, supply: list, aliases: list) -> list: + return [(ref, ob) for ref, ob in supply if any(same_spool(ref, m, aliases) for m in members)] + + +def _supply_state(matches: list, now: Any) -> dict: + """{state: usable|stale|unknown, remaining_g, reason, freshness}.""" + if not matches: + return {"state": UNKNOWN, "remaining_g": None, "reason": "No remaining-weight reading was given for this spool.", + "freshness": None} + values = {ob.value for _, ob in matches} + if len(values) > 1: + return {"state": UNKNOWN, "remaining_g": None, "freshness": None, + "reason": "Readings for this spool disagree, so none of them is used."} + ref, ob = matches[0] + fresh = obs_mod.freshness(ob, now) + grams = ob.value + base = {"remaining_g": grams if is_finite_number(grams) else None, "freshness": fresh} + if grams is None: + return {**base, "state": UNKNOWN, "reason": "The reading states no remaining weight."} + if fresh["usable_for_loadout"]: + return {**base, "state": "usable", "reason": fresh["reason"]} + if fresh["state"] in (obs_mod.STALE, obs_mod.AGEING) or ob.historical: + return {**base, "state": STALE, "reason": fresh["reason"]} + return {**base, "state": UNKNOWN, "reason": fresh["reason"]} + + +def _spool_verdict(contribs: list, supply: dict) -> dict: + """contribs: [{tile_id, grams (known sum) | None, unknown}] in tile order.""" + known = sum(c["grams"] or 0.0 for c in contribs) + unknown_tiles = [c["tile_id"] for c in contribs if c["unknown"]] + out = {"need_known_g": round(known, 3), "unknown_tiles": unknown_tiles, "need_complete": not unknown_tiles} + state = supply["state"] + if state == STALE: + return {**out, "verdict": STALE, "starving_tiles": [], "shortfall_g": None, + "reason": "The last remaining-weight reading is too old to rely on: " + supply["reason"]} + if state == UNKNOWN: + return {**out, "verdict": UNKNOWN, "starving_tiles": [], "shortfall_g": None, "reason": supply["reason"]} + remaining = supply["remaining_g"] + if known > remaining + 1e-9: + running, starving = 0.0, [] + for c in contribs: + running += c["grams"] or 0.0 + if running > remaining + 1e-9 or starving: + starving.append(c["tile_id"]) + note = " The stated need is only a lower bound: some tiles state no grams." if unknown_tiles else "" + return {**out, "verdict": SHORTAGE, "starving_tiles": starving, + "shortfall_g": round(known - remaining, 3), + "reason": f"The tiles state {known:g} g against {remaining:g} g left.{note}"} + if unknown_tiles: + return {**out, "verdict": UNKNOWN, "starving_tiles": [], "shortfall_g": None, + "reason": "Some tiles state no grams for this spool, so the total need is not known."} + return {**out, "verdict": ENOUGH, "starving_tiles": [], "shortfall_g": None, + "reason": f"The tiles state {known:g} g against {remaining:g} g left."} + + +def evaluate(assembly: Assembly, supply: list | None = None, *, now: Any = None, + lookup: LibraryLookup | None = None) -> dict: + """The advisory report: per-tile need, per-spool verdict, overall verdict, and every unknown named.""" + supply = supply or [] + rows, unresolved = _rows(assembly) + + groups: list = [] + per_group: dict = {} + for row in rows: + if row["spool"] is None: + continue + g = _group_key(row["spool"], groups, assembly.aliases) + per_group.setdefault(g, []).append(row) + + spools = [] + for g, members in enumerate(groups): + contribs = [] + for tile in assembly.tiles: + mine = [r for r in per_group.get(g, []) if r["tile_id"] == tile.tile_id] + if not mine: + continue + unknown = any(r["grams"] is None for r in mine) + contribs.append({"tile_id": tile.tile_id, "grams": sum(r["grams"] or 0.0 for r in mine), "unknown": unknown, + "roles": sorted({r["role"] for r in mine})}) + state = _supply_state(_supply_for(members, supply, assembly.aliases), now) + verdict = _spool_verdict(contribs, state) + spools.append({"spool": members[0].to_dict(), "same_spool_as": [m.to_dict() for m in members[1:]], + "roles": sorted({r["role"] for r in per_group.get(g, [])}), + "remaining_g": state["remaining_g"], "supply_state": state["state"], + "freshness": state["freshness"], "tiles": contribs, **verdict}) + + link = {t.tile_id: library_status(t, lookup) for t in assembly.tiles} + tiles = [] + for tile in assembly.tiles: + trows = [r for r in rows if r["tile_id"] == tile.tile_id] + known = [r["grams"] for r in trows if r["grams"] is not None] + unknown_slots = [r["slot"] for r in trows if r["grams"] is None] + tiles.append({ + "tile_id": tile.tile_id, "name": tile.name, "plate": tile.plate, "part_id": tile.part_id, + "usage_source": tile.usage_source, "scale": tile.scale, "library": link[tile.tile_id], + "attribution": dict(tile.attribution), + "slots": [{"slot": r["slot"], "role": r["role"], "grams": r["grams"], + "spool": r["spool"].to_dict() if r["spool"] else None, "unresolved": r["unresolved"]} + for r in trows], + "need_known_g": round(sum(known), 3), + # No slot at all means no stated figure: the need is unknown, not zero. + "need_complete": bool(trows) and not unknown_slots, + "unknown_slots": unknown_slots, + }) + + verdicts = [s["verdict"] for s in spools] + if SHORTAGE in verdicts: + overall = SHORTAGE + elif STALE in verdicts: + overall = STALE + elif not spools or UNKNOWN in verdicts or unresolved or any(not t["need_complete"] for t in tiles): + overall = UNKNOWN + else: + overall = ENOUGH + + return {"name": assembly.name, "advisory": ADVISORY, "verdict": overall, + "tiles": tiles, "spools": spools, "unresolved": unresolved, + "roles": role_summary(assembly), "parts": parts_report(assembly), "seam_scale": seam_scale(assembly), + "attribution": [dict(a) for a in assembly.attribution]} diff --git a/backend/snapstudio_core/assemblies/model.py b/backend/snapstudio_core/assemblies/model.py new file mode 100644 index 00000000..3d8e55c2 --- /dev/null +++ b/backend/snapstudio_core/assemblies/model.py @@ -0,0 +1,289 @@ +"""The assembly document: tiles, shared roles, spool bindings and an attribution manifest. + +An assembly is user-defined input, so every field is validated and bounded here. A bad +document raises `AssemblyInputError` (the route turns that into a 400); nothing else in +this module raises on data. Grams are `float | None` and None means "not stated": it is +never turned into zero. + +Slots are 0-based `slot:n` ids (foundation topology). An Orca project's 1-based filament +number and a G-code `T` both land on the same id. +""" +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from typing import Any + +from snapstudio_core.foundation import topology +from snapstudio_core.foundation.decode import is_finite_number +from snapstudio_core.foundation.materials import SpoolAlias, SpoolRef + +SCHEMA_VERSION = "assembly/1" + +MAX_TILES = 64 +MAX_PARTS = 64 +MAX_ROLES = 32 +MAX_ATTRIBUTION = 64 +MAX_SLOTS = 64 +MAX_TEXT = 200 +MAX_GRAMS = 1_000_000.0 + +_ROLE_RE = re.compile(r"^[A-Za-z0-9_][A-Za-z0-9_ \-]{0,39}$") +_ID_RE = re.compile(r"^[A-Za-z0-9_][A-Za-z0-9_.\-]{0,63}$") +_SHA_RE = re.compile(r"^[0-9a-f]{64}$") +_SLOT_RE = re.compile(r"^slot:(\d{1,3})$") + + +class AssemblyInputError(ValueError): + """The assembly document is not usable. The message is safe to show to the user.""" + + +def _obj(value: Any, what: str) -> dict: + if not isinstance(value, dict): + raise AssemblyInputError(f"{what} must be an object.") + return value + + +def _list(value: Any, what: str, limit: int) -> list: + if value is None: + return [] + if not isinstance(value, list): + raise AssemblyInputError(f"{what} must be a list.") + if len(value) > limit: + raise AssemblyInputError(f"{what} has more than {limit} entries.") + return value + + +def _text(value: Any, what: str, *, required: bool = False) -> str | None: + if value is None or value == "": + if required: + raise AssemblyInputError(f"{what} is required.") + return None + if not isinstance(value, str) or len(value) > MAX_TEXT: + raise AssemblyInputError(f"{what} must be text of at most {MAX_TEXT} characters.") + return value.strip() or None + + +def _ident(value: Any, what: str) -> str: + if not isinstance(value, str) or not _ID_RE.match(value): + raise AssemblyInputError(f"{what} must be 1-64 letters, digits, dots, dashes or underscores.") + return value + + +def _role(value: Any, what: str) -> str: + if not isinstance(value, str) or not _ROLE_RE.match(value): + raise AssemblyInputError(f"{what} must be 1-40 letters, digits, spaces, dashes or underscores.") + return value + + +def grams_or_none(value: Any, what: str) -> float | None: + """A gram figure: finite, non-negative, bounded. null stays null; anything else unusable is refused.""" + if value is None: + return None + if not is_finite_number(value) or value < 0 or value > MAX_GRAMS: + raise AssemblyInputError(f"{what} must be a number from 0 up, or null when it is not known.") + return float(value) + + +def slot_key(value: Any, what: str) -> str: + match = _SLOT_RE.match(value) if isinstance(value, str) else None + if not match or int(match.group(1)) >= MAX_SLOTS: + raise AssemblyInputError(f"{what} must look like slot:0 (slots count from 0).") + return topology.slot_id(int(match.group(1))) + + +def spool_ref(value: Any, what: str) -> SpoolRef: + d = _obj(value, what) + try: + return SpoolRef(d.get("provider_instance_id"), d.get("provider_spool_id")) + except ValueError: + raise AssemblyInputError(f"{what} needs a provider_instance_id and a provider_spool_id.") from None + + +@dataclass(frozen=True) +class LibraryRef: + """Cross-reference to a library project: its id and the sha256 the user recorded. No foreign key.""" + project_id: int + sha256: str | None + + def to_dict(self) -> dict: + return {"project_id": self.project_id, "sha256": self.sha256} + + +@dataclass +class Tile: + tile_id: str + name: str + plate: str | None = None + part_id: str | None = None + library: LibraryRef | None = None + roles: dict = field(default_factory=dict) # role -> slot id + usage: dict = field(default_factory=dict) # slot id -> grams | None + usage_source: str | None = None # declared | slot_usage | gcode_facts | None + scale: float | None = None + attribution: dict = field(default_factory=dict) + + def to_dict(self) -> dict: + return {"tile_id": self.tile_id, "name": self.name, "plate": self.plate, "part_id": self.part_id, + "library": self.library.to_dict() if self.library else None, + "roles": dict(self.roles), "usage": dict(self.usage), "usage_source": self.usage_source, + "scale": self.scale, "attribution": dict(self.attribution)} + + +@dataclass +class Assembly: + name: str + tiles: list = field(default_factory=list) + parts: list = field(default_factory=list) # [{part_id, name}] + bindings: dict = field(default_factory=dict) # role -> SpoolRef + aliases: list = field(default_factory=list) # [SpoolAlias] + attribution: list = field(default_factory=list) # retained as written: [{item, author, license, source}] + + def to_dict(self) -> dict: + return {"schema_version": SCHEMA_VERSION, "name": self.name, + "tiles": [t.to_dict() for t in self.tiles], "parts": [dict(p) for p in self.parts], + "bindings": {role: ref.to_dict() for role, ref in self.bindings.items()}, + "aliases": [a.to_dict() for a in self.aliases], + "attribution": [dict(a) for a in self.attribution]} + + +# -- gram sources ---------------------------------------------------------------- +def usage_from_slot_usage(slot_usage: Any) -> dict: + """`project_compare.slot_usage` output (slots numbered from 1) -> {slot:n: grams | None}. + + Only `sliced_g` counts as grams. A slot the project references but that carries no slicer + figure is None (unknown), never 0. + """ + slots = _obj(_obj(slot_usage, "slot_usage").get("slots"), "slot_usage.slots") + if len(slots) > MAX_SLOTS: + raise AssemblyInputError("slot_usage has too many slots.") + out: dict = {} + for key, entry in slots.items(): + try: + number = int(key) + except (TypeError, ValueError): + raise AssemblyInputError("slot_usage slots must be numbered from 1.") from None + index = topology.normalise_index("orca_filament", number) + if index is None: + raise AssemblyInputError("slot_usage slots must be numbered from 1.") + out[topology.slot_id(index)] = grams_or_none(_obj(entry, "A slot_usage entry").get("sliced_g"), "sliced_g") + return out + + +def usage_from_gcode_facts(facts: Any) -> dict: + """`gcode.read_facts` output (tools numbered from 0) -> {slot:n: grams | None}. + + A file Studio could not read (`available` false) states nothing: empty usage, every + figure unknown. + """ + facts = _obj(facts, "gcode_facts") + if not facts.get("available"): + return {} + out: dict = {} + for entry in _list(facts.get("slots"), "gcode_facts.slots", MAX_SLOTS): + entry = _obj(entry, "A gcode_facts slot") + tool = topology.normalise_index("gcode_t", entry.get("tool")) + if tool is None: + raise AssemblyInputError("gcode_facts slots need a tool number from 0.") + out[topology.slot_id(tool)] = grams_or_none(entry.get("grams"), "grams") + return out + + +# -- reading --------------------------------------------------------------------- +def _read_tile(raw: Any) -> Tile: + d = _obj(raw, "A tile") + tile = Tile(tile_id=_ident(d.get("tile_id"), "tile_id"), + name=_text(d.get("name"), "The tile name", required=True) or "") + plate = d.get("plate") + if plate is not None and (isinstance(plate, bool) or not isinstance(plate, (str, int))): + raise AssemblyInputError("plate must be text or a number.") + tile.plate = None if plate in (None, "") else str(plate)[:MAX_TEXT] + part_id = d.get("part_id") + tile.part_id = None if part_id in (None, "") else _ident(part_id, "part_id") + + library = d.get("library") + if library is not None: + lib = _obj(library, "library") + pid = lib.get("project_id") + if isinstance(pid, bool) or not isinstance(pid, int) or pid < 0: + raise AssemblyInputError("library.project_id must be a whole number.") + sha = lib.get("sha256") + if sha is not None and (not isinstance(sha, str) or not _SHA_RE.match(sha.lower())): + raise AssemblyInputError("library.sha256 must be 64 hex characters or null.") + tile.library = LibraryRef(pid, sha.lower() if sha else None) + + roles = _obj(d.get("roles") or {}, "roles") + if len(roles) > MAX_ROLES: + raise AssemblyInputError("A tile has too many roles.") + for role, slot in roles.items(): + tile.roles[_role(role, "A role name")] = slot_key(slot, f"Role {role}") + + sources = [k for k in ("usage", "slot_usage", "gcode_facts") if d.get(k) is not None] + if len(sources) > 1: + raise AssemblyInputError("Give a tile one source of gram figures: usage, slot_usage or gcode_facts.") + if sources == ["usage"]: + for slot, grams in _obj(d["usage"], "usage").items(): + tile.usage[slot_key(slot, "A usage key")] = grams_or_none(grams, "A gram figure") + tile.usage_source = "declared" + elif sources == ["slot_usage"]: + tile.usage, tile.usage_source = usage_from_slot_usage(d["slot_usage"]), "slot_usage" + elif sources == ["gcode_facts"]: + tile.usage, tile.usage_source = usage_from_gcode_facts(d["gcode_facts"]), "gcode_facts" + + scale = d.get("scale") + if scale is not None: + if not is_finite_number(scale) or scale <= 0 or scale > 1000: + raise AssemblyInputError("scale must be a positive number, or null when it is not known.") + tile.scale = float(scale) + + for key, value in _obj(d.get("attribution") or {}, "attribution").items(): + if not isinstance(key, str) or len(key) > 40: + raise AssemblyInputError("An attribution key must be short text.") + tile.attribution[key] = _text(value, f"Attribution {key}") + return tile + + +def read_assembly(raw: Any) -> Assembly: + d = _obj(raw, "The assembly") + stamp = d.get("schema_version") + if stamp is not None and stamp != SCHEMA_VERSION: + raise AssemblyInputError("This assembly was written in a format this version of Studio does not read.") + asm = Assembly(name=_text(d.get("name"), "The assembly name", required=True) or "") + seen: set = set() + for item in _list(d.get("tiles"), "tiles", MAX_TILES): + tile = _read_tile(item) + if tile.tile_id in seen: + raise AssemblyInputError("Two tiles share the same tile_id.") + seen.add(tile.tile_id) + asm.tiles.append(tile) + part_ids: set = set() + for item in _list(d.get("parts"), "parts", MAX_PARTS): + p = _obj(item, "A part") + pid = _ident(p.get("part_id"), "part_id") + if pid in part_ids: + raise AssemblyInputError("Two parts share the same part_id.") + part_ids.add(pid) + asm.parts.append({"part_id": pid, "name": _text(p.get("name"), "The part name") or pid}) + bindings = _obj(d.get("bindings") or {}, "bindings") + if len(bindings) > MAX_ROLES: + raise AssemblyInputError("There are too many bindings.") + for role, ref in bindings.items(): + asm.bindings[_role(role, "A binding role")] = spool_ref(ref, f"The binding for {role}") + for item in _list(d.get("aliases"), "aliases", 32): + a = _obj(item, "An alias") + refs = [spool_ref(r, "An alias reference") for r in _list(a.get("refs"), "alias refs", 8)] + evidence = _text(a.get("evidence"), "The alias evidence", required=True) or "" + declared_by = _text(a.get("declared_by"), "The alias declared_by", required=True) or "" + try: + asm.aliases.append(SpoolAlias(tuple(refs), evidence, declared_by)) + except ValueError: + raise AssemblyInputError("An alias needs at least two spool references.") from None + for item in _list(d.get("attribution"), "attribution", MAX_ATTRIBUTION): + a = _obj(item, "An attribution entry") + entry = {} + for key, value in a.items(): + if not isinstance(key, str) or len(key) > 40: + raise AssemblyInputError("An attribution key must be short text.") + entry[key] = _text(value, f"Attribution {key}") + asm.attribution.append(entry) + return asm diff --git a/backend/snapstudio_core/assemblies/store.py b/backend/snapstudio_core/assemblies/store.py new file mode 100644 index 00000000..06959a50 --- /dev/null +++ b/backend/snapstudio_core/assemblies/store.py @@ -0,0 +1,121 @@ +"""`assemblies.db`: saved assembly definitions, through `foundation.store`. + +Only definitions are saved. Spool readings are time-sensitive, so supply is supplied with each +evaluation and never stored. `tile_refs` records which library project (id + sha256) each tile +points at so "which assemblies use this project" is one query; it has no foreign key into the +library, which is a different database that the user can change or delete from. +""" +from __future__ import annotations + +import datetime +import json +from typing import Any + +from snapstudio_core.foundation import store as fstore + +from .model import Assembly, AssemblyInputError, read_assembly + +STORE_NAME = "assemblies" +VERSION = 1 +MAX_SAVED = 500 + +_SCHEMA = """ +CREATE TABLE IF NOT EXISTS assemblies ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + doc TEXT NOT NULL, + revision INTEGER NOT NULL, + updated_at TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS tile_refs ( + assembly_id INTEGER NOT NULL, + tile_id TEXT NOT NULL, + library_project_id INTEGER NOT NULL, + library_sha256 TEXT, + PRIMARY KEY (assembly_id, tile_id) +); +CREATE INDEX IF NOT EXISTS tile_refs_by_project ON tile_refs (library_project_id); +""" + + +class NotFound(AssemblyInputError): + """No saved assembly has that id.""" + + +def _now() -> str: + return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +def _open(): + return fstore.open_store(STORE_NAME, _SCHEMA, VERSION) + + +def _refs(assembly: Assembly) -> list: + return [(t.tile_id, t.library.project_id, t.library.sha256) for t in assembly.tiles if t.library is not None] + + +def save(raw: Any, assembly_id: int | None = None, expected_revision: int | None = None) -> dict: + """Validate and store `raw`. New when `assembly_id` is None. An update must name the revision it + was read at; a different stored revision is a `RevisionConflict` and nothing is written.""" + assembly = read_assembly(raw) + doc = json.dumps(assembly.to_dict(), sort_keys=True, separators=(",", ":")) + with _open() as conn: + with fstore.transaction(conn): + if assembly_id is None: + if conn.execute("SELECT COUNT(*) FROM assemblies").fetchone()[0] >= MAX_SAVED: + raise AssemblyInputError("There are too many saved assemblies. Delete one first.") + cur = conn.execute("INSERT INTO assemblies (name, doc, revision, updated_at) VALUES (?,?,?,?)", + (assembly.name, doc, 1, _now())) + assembly_id, revision = int(cur.lastrowid), 1 + else: + row = conn.execute("SELECT revision FROM assemblies WHERE id=?", (assembly_id,)).fetchone() + if row is None: + raise NotFound("That assembly is not saved.") + if expected_revision is None or row[0] != expected_revision: + raise fstore.RevisionConflict("the document changed since it was read") + revision = row[0] + 1 + conn.execute("UPDATE assemblies SET name=?, doc=?, revision=?, updated_at=? WHERE id=?", + (assembly.name, doc, revision, _now(), assembly_id)) + conn.execute("DELETE FROM tile_refs WHERE assembly_id=?", (assembly_id,)) + conn.executemany("INSERT INTO tile_refs VALUES (?,?,?,?)", + [(assembly_id, t, p, s) for t, p, s in _refs(assembly)]) + return {"id": assembly_id, "revision": revision, "assembly": assembly.to_dict()} + + +def get(assembly_id: int) -> dict: + with _open() as conn: + row = conn.execute("SELECT id, doc, revision, updated_at FROM assemblies WHERE id=?", + (assembly_id,)).fetchone() + if row is None: + raise NotFound("That assembly is not saved.") + return {"id": row[0], "revision": row[2], "updated_at": row[3], "assembly": json.loads(row[1])} + + +def list_saved() -> list: + with _open() as conn: + rows = conn.execute("SELECT id, name, doc, revision, updated_at FROM assemblies " + "ORDER BY updated_at DESC, id DESC").fetchall() + return [{"id": r[0], "name": r[1], "tiles": len(json.loads(r[2]).get("tiles", [])), + "revision": r[3], "updated_at": r[4]} for r in rows] + + +def delete(assembly_id: int) -> bool: + """Remove a saved definition. Never touches a library project or any model file.""" + with _open() as conn: + with fstore.transaction(conn): + conn.execute("DELETE FROM tile_refs WHERE assembly_id=?", (assembly_id,)) + cur = conn.execute("DELETE FROM assemblies WHERE id=?", (assembly_id,)) + return cur.rowcount > 0 + + +def assemblies_for_project(project_id: int, sha256: str | None = None) -> list: + """Saved assemblies with a tile that points at this library project (and checksum, when given).""" + sql = ("SELECT r.assembly_id, a.name, r.tile_id, r.library_sha256 FROM tile_refs r " + "JOIN assemblies a ON a.id = r.assembly_id WHERE r.library_project_id=?") + args: list = [project_id] + if sha256 is not None: + sql += " AND r.library_sha256=?" + args.append(sha256.lower()) + with _open() as conn: + rows = conn.execute(sql + " ORDER BY r.assembly_id, r.tile_id", args).fetchall() + return [{"assembly_id": r[0], "name": r[1], "tile_id": r[2], "sha256": r[3]} for r in rows] diff --git a/backend/snapstudio_core/calibration/README.md b/backend/snapstudio_core/calibration/README.md new file mode 100644 index 00000000..4b3364d3 --- /dev/null +++ b/backend/snapstudio_core/calibration/README.md @@ -0,0 +1,44 @@ +# Palette calibration input and history + +DitherForge TXT accepts `#RRGGBB 1.9 Label`, a label without TD, and the +`td=1.9` form. Lines starting with `# `, `;` or `//` are comments. Missing TD +is explicitly nominal (1.0), never a measurement. Invalid or conflicting TD +is not replaced with a nominal value. The earlier Studio `td Label` syntax +remains readable for compatibility. + +Kromacut v2 `.kpal` files are JSON objects with `version: 2`, `colors`, +optional `colorNames` and optional `disabledColors` indices. Indices remain +in their original positions. Disabled colours are shown but receive no +physical reading or owned-filament suggestion. The earlier Studio `palette` +row representation remains readable; it is not the upstream export format. +Palette-library arrays and unsupported schema versions are not evaluated. + +The screen accepts TXT, JSON and KPAL. Physical observations and owned spool +colours start empty. Changing the palette or the calibration basis clears +observations and invalidates an in-flight result. File reads also have an +identity guard, so an older import cannot replace a later selection. + +The visible basis includes family, layer height, first-layer height, nozzle, +material, palette source and an optional user-entered model/revision label. +These fields participate in the basis hash. The label is not a verified model +file identity. Omitted first-layer/model/source data on older history remains +unknown. This changes no database schema: extended basis data stays in the +existing saved comparison payload. History is local, newest first, up to 50; +viewing it never reapplies old readings to current input. + +The results are advisory RGB comparisons. No printer, spectrometer or +physical calibration is performed. Tests use synthetic fixtures conforming +to pinned upstream contracts, rather than captured physical readings. + +Contract references: +- [DitherForge README](https://github.com/rtwfroody/ditherforge/blob/4d3552a434e8084d9b5fe573959a3493325be29f/README.md) +- [Kromacut palette manager](https://github.com/vycdev/Kromacut/blob/94874755426ee7f33b3684a5544e7fab0e2e73f7/src/lib/paletteManager.ts) + + +Review follow-up: every explicit TD token is checked, repeated td= assignments +are refused, and leading/fallback values must agree. A blank material is refused; +only an omitted material retains the legacy PLA default. Blank model labels become +unknown. The expanded basis/palette documents change their hashes: pre-change +identities are not reproduced, and old saved comparisons are left untouched. +A numeric leading label such as `#FF0000 2 Tone` is interpreted as TD 2 by the +documented upstream grammar, rather than a label beginning with the number 2. diff --git a/backend/snapstudio_core/calibration/__init__.py b/backend/snapstudio_core/calibration/__init__.py new file mode 100644 index 00000000..0ba71cb7 --- /dev/null +++ b/backend/snapstudio_core/calibration/__init__.py @@ -0,0 +1 @@ +"""Read-only palette calibration and advisory comparison helpers.""" diff --git a/backend/snapstudio_core/calibration/palette.py b/backend/snapstudio_core/calibration/palette.py new file mode 100644 index 00000000..5fa733f7 --- /dev/null +++ b/backend/snapstudio_core/calibration/palette.py @@ -0,0 +1,167 @@ +"""Palette decoding for DitherForge TXT and Kromacut palette JSON v2.""" +from __future__ import annotations + +import re +from dataclasses import dataclass +from typing import Any + +from snapstudio_core.foundation.decode import Decoded, Diagnostic, ERROR, decode_document, diag, parse_json + +_COLOR = re.compile(r"^#?([0-9a-fA-F]{6})$") +_DITHER = re.compile(r"^\s*#?([0-9a-fA-F]{6})(?:\s+(.*?))?\s*$", re.IGNORECASE) + + +@dataclass(frozen=True) +class PaletteEntry: + label: str + color: str + td: float + td_provenance: str + enabled: bool = True + + def to_dict(self) -> dict: + return {"label": self.label, "color": self.color, "td": self.td, + "td_provenance": self.td_provenance, "enabled": self.enabled} + + +@dataclass +class Palette: + source: str + entries: list[PaletteEntry] + + def to_dict(self) -> dict: + return {"schema_version": "calibration_palette/1", "source": self.source, + "entries": [entry.to_dict() for entry in self.entries]} + + +def _color(value: Any) -> str | None: + if not isinstance(value, str): + return None + match = _COLOR.fullmatch(value.strip()) + return f"#{match.group(1).upper()}" if match else None + + +def parse_ditherforge(text: str) -> Decoded[Palette]: + """Parse documented DitherForge rows; unmeasured TD is explicitly derived.""" + if not isinstance(text, str): + return Decoded(None, None, [diag("wrong_type", "text", ERROR)], False) + entries: list[PaletteEntry] = [] + diagnostics: list[Diagnostic] = [] + for number, line in enumerate(text.splitlines(), 1): + stripped = line.strip() + if not stripped or stripped.startswith(";") or stripped.startswith("//") or stripped.startswith("# "): + continue + match = _DITHER.match(stripped) + if not match: + diagnostics.append(diag("invalid_palette_line", f"line:{number}", ERROR)) + continue + color, remainder = match.groups() + remainder = remainder or "" + assignments = list(re.finditer(r"\btd\s*=\s*(\S*)", remainder, re.IGNORECASE)) + values = [item.group(1) for item in assignments] + label = re.sub(r"\btd\s*=\s*\S*", "", remainder, flags=re.IGNORECASE).strip() + leading_td = re.match(r"^td(?:\s+([0-9.eE+-]+))?(?:\s+|$)(.*)$", label, re.IGNORECASE) + numeric = re.match(r"^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)(?:\s+|$)(.*)$", label) + if leading_td: + if leading_td.group(1) is not None: + values.append(leading_td.group(1)) + label = leading_td.group(2) + elif numeric: + values.append(numeric.group(1)) + label = numeric.group(2) + try: + numbers = [float(value) for value in values] + except ValueError: + numbers = [float("nan")] + if len(assignments) > 1 or any(not (0 < value < float("inf")) for value in numbers) or len(set(numbers)) > 1: + diagnostics.append(diag("invalid_or_conflicting_td", f"line:{number}", ERROR)) + continue + td = numbers[0] if numbers else 1.0 + td_text = values[0] if values else None + entries.append(PaletteEntry((label or f"Color {len(entries) + 1}").strip(), + f"#{color.upper()}", td, + "provided" if td_text is not None else "derived")) + if not entries and not diagnostics: + diagnostics.append(diag("empty_palette", "", ERROR)) + value = Palette("ditherforge_txt", entries) if not any(d.severity == ERROR for d in diagnostics) else None + return Decoded("calibration_palette/1", value, diagnostics, value is not None, + raw=None if value else text) + + +def _build_kromacut(reader) -> Palette: + # Kromacut v2 exports parallel arrays in a .kpal JSON object. Keep every + # original index, including disabled colours, so readings cannot shift rows. + if "colors" in reader.raw: + colors = reader.raw["colors"] + names = reader.raw.get("colorNames", []) + disabled = reader.raw.get("disabledColors", []) + if not isinstance(colors, list) or not colors or len(colors) > 256: + reader.error("invalid_colors", "colors") + return Palette("kromacut_json_v2", []) + if not isinstance(names, list) or len(names) > len(colors) or any(not isinstance(n, str) or len(n) > 256 for n in names): + reader.error("invalid_color_names", "colorNames") + return Palette("kromacut_json_v2", []) + if not isinstance(disabled, list) or any(type(i) is not int or not 0 <= i < len(colors) for i in disabled): + reader.error("invalid_disabled_colors", "disabledColors") + return Palette("kromacut_json_v2", []) + entries = [] + for index, raw_color in enumerate(colors): + color = _color(raw_color) + if color is None: + reader.error("invalid_color", f"colors[{index}]") + continue + label = names[index].strip() if index < len(names) else "" + entries.append(PaletteEntry(label or f"Color {index + 1}", color, 1.0, "derived", index not in disabled)) + return Palette("kromacut_json_v2", entries) + # Retain the earlier Studio row representation for saved fixtures. + rows = reader.items("palette", required=True) + if not rows: + reader.error("empty_palette", "palette") + entries: list[PaletteEntry] = [] + for index, row in enumerate(rows): + item = reader.child(row, f"palette[{index}]") + color = _color(item.raw.get("color", item.raw.get("hex"))) + if color is None: + item.error("invalid_color", "color") + continue + label = item.string("label", required=False, default=None) or f"Color {index + 1}" + td_value = item.raw.get("td") + if td_value is None: + td = 1.0 + provenance = "derived" + elif isinstance(td_value, bool) or not isinstance(td_value, (int, float)) or not (0 < td_value < float("inf")): + item.error("invalid_td", "td") + continue + else: + td = float(td_value) + provenance = "provided" + entries.append(PaletteEntry(label, color, td, provenance)) + return Palette("kromacut_json_v2", entries) + + +def parse_kromacut(raw: Any) -> Decoded[Palette]: + """Decode version 2 only; preserve unsupported input without evaluating it.""" + parsed = parse_json(raw) if isinstance(raw, (str, bytes, bytearray)) else None + if parsed is not None: + if parsed.value is None: + return Decoded(None, None, parsed.diagnostics, False, raw=parsed.raw) + raw = parsed.value + if not isinstance(raw, dict): + return Decoded(None, None, [diag("not_an_object", "", ERROR)], False, raw=raw) + version = raw.get("version", raw.get("schema_version")) + if version not in (2, "2", "kromacut_palette/2"): + return Decoded(str(version) if version is not None else None, None, + [diag("unknown_schema_version" if version is not None else "missing_schema_version", + "version", ERROR)], False, raw=raw) + normalized = {**raw, "schema_version": "kromacut_palette/2"} + if "palette" not in normalized and isinstance(normalized.get("entries"), list): + normalized["palette"] = normalized["entries"] + return decode_document(normalized, "kromacut_palette/2", _build_kromacut) + + +def parse_palette(source: str, content: str) -> Decoded[Palette]: + if source == "ditherforge_txt": + return parse_ditherforge(content) + if source == "kromacut_json": + return parse_kromacut(content) + return Decoded(None, None, [diag("unknown_palette_source", "source", ERROR)], False) diff --git a/backend/snapstudio_core/calibration/service.py b/backend/snapstudio_core/calibration/service.py new file mode 100644 index 00000000..0bfccacd --- /dev/null +++ b/backend/snapstudio_core/calibration/service.py @@ -0,0 +1,103 @@ +"""Calibration basis identity, palette comparison and local history.""" +from __future__ import annotations + +import json +import math +import time +from typing import Any + +from snapstudio_core import material_plan +from snapstudio_core.foundation.decode import content_hash, revision_envelope +from snapstudio_core.foundation.store import open_store + +_SCHEMA = """ +CREATE TABLE IF NOT EXISTS calibration_basis ( + calibration_basis_id TEXT PRIMARY KEY, + family TEXT NOT NULL, + layer_height_mm REAL NOT NULL, + nozzle_mm REAL NOT NULL, + material TEXT NOT NULL, + created_at REAL NOT NULL +); +CREATE TABLE IF NOT EXISTS calibration_runs ( + run_id TEXT PRIMARY KEY, + calibration_basis_id TEXT NOT NULL, + inputs_revision TEXT NOT NULL, + created_at REAL NOT NULL, + payload_json TEXT NOT NULL, + FOREIGN KEY(calibration_basis_id) REFERENCES calibration_basis(calibration_basis_id) +); +""" + + +def basis_document(family: str, layer_height_mm: float, nozzle_mm: float, material: str, + *, source: str | None = None, model: str | None = None, + first_layer_height_mm: float | None = None) -> dict: + if family not in ("frontlit", "backlit"): + raise ValueError("family must be frontlit or backlit") + values = (layer_height_mm, nozzle_mm) + (() if first_layer_height_mm is None else (first_layer_height_mm,)) + if not isinstance(material, str) or not material.strip() or len(material) > 32: + raise ValueError("material must be a non-empty name of at most 32 characters") + if source not in (None, "ditherforge_txt", "kromacut_json"): + raise ValueError("unknown palette source") + if model is not None and (not isinstance(model, str) or len(model) > 128): + raise ValueError("model must be at most 128 characters") + if any(isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v) or v <= 0 for v in values): + raise ValueError("layer height and nozzle must be positive finite numbers") + normalized = {"family": family, "layer_height_mm": round(float(layer_height_mm), 4), + "nozzle_mm": round(float(nozzle_mm), 4), "material": material.strip().upper()} + normalized.update(source=source, model=(model.strip() or None) if model else None, + first_layer_height_mm=round(float(first_layer_height_mm), 4) if first_layer_height_mm is not None else None) + return {**normalized, "calibration_basis_id": "calibration-basis:" + content_hash(normalized).split(":", 1)[1]} + + +def evaluate(palette: dict, basis: dict, physical_colors: dict[str, str], owned_filaments: list[dict]) -> dict: + entries = [] + for index, item in enumerate(palette["entries"]): + enabled = item.get("enabled", True) + physical = physical_colors.get(str(index)) if enabled else None + alternatives = [] + for spool in owned_filaments: + color = spool.get("color") if isinstance(spool, dict) else None + distance = material_plan.colour_distance(item["color"], color) + if distance is not None: + alternatives.append({"label": str(spool.get("label") or "Owned filament"), + "color": color, "distance": round(distance, 1), + "advisory": True}) + alternatives.sort(key=lambda alt: alt["distance"]) + entries.append({"label": item["label"], "simulated_color": item["color"], + "physical_color": physical, "physical_distance": material_plan.colour_distance( + item["color"], physical), "td": item["td"], + "td_provenance": item["td_provenance"], "enabled": enabled, "alternatives": alternatives[:3] if enabled else []}) + return {"calibration_basis": basis, "entries": entries, + "advisory": "Palette colours are simulated targets; physical readings are separate observations. Owned-filament matches are advisory."} + + +def save_run(basis: dict, palette: dict, result: dict, data_dir: str | None = None) -> dict: + inputs_revision = revision_envelope({"calibration_basis_id": basis["calibration_basis_id"], + "palette_hash": content_hash(palette), + # physical readings and owned filaments change the comparison, so they + # change the run identity; an earlier result is never overwritten + "comparison_hash": content_hash(result["entries"])}) + run_id = inputs_revision["revision"] + payload = {**result, "inputs_revision": inputs_revision} + with open_store("calibration", _SCHEMA, 1, data_dir=data_dir) as conn: + conn.execute("BEGIN IMMEDIATE") # the store connection is autocommit; keep both rows together + try: + conn.execute("INSERT OR IGNORE INTO calibration_basis VALUES (?, ?, ?, ?, ?, ?)", + (basis["calibration_basis_id"], basis["family"], basis["layer_height_mm"], + basis["nozzle_mm"], basis["material"], time.time())) + conn.execute("INSERT OR IGNORE INTO calibration_runs VALUES (?, ?, ?, ?, ?)", + (run_id, basis["calibration_basis_id"], inputs_revision["revision"], time.time(), + json.dumps(payload, separators=(",", ":")))) + conn.execute("COMMIT") + except BaseException: + conn.execute("ROLLBACK") + raise + return {"run_id": run_id, **payload} + + +def history(data_dir: str | None = None) -> list[dict]: + with open_store("calibration", _SCHEMA, 1, data_dir=data_dir) as conn: + rows = conn.execute("SELECT payload_json FROM calibration_runs ORDER BY created_at DESC, run_id DESC LIMIT 50").fetchall() + return [json.loads(row[0]) for row in rows] diff --git a/backend/snapstudio_core/creative/__init__.py b/backend/snapstudio_core/creative/__init__.py new file mode 100644 index 00000000..f4fec220 --- /dev/null +++ b/backend/snapstudio_core/creative/__init__.py @@ -0,0 +1,9 @@ +"""Read-only creative-project inspection for documented project shapes.""" + +from .container import ContainerError, ThreeMFManifest, read_3mf +from .readers import CreativeProject, read_forca, read_fullspectrum, read_imagemap, read_lixnix, read_lumina + +__all__ = [ + "ContainerError", "ThreeMFManifest", "read_3mf", "CreativeProject", + "read_forca", "read_fullspectrum", "read_imagemap", "read_lixnix", "read_lumina", +] diff --git a/backend/snapstudio_core/creative/container.py b/backend/snapstudio_core/creative/container.py new file mode 100644 index 00000000..b285ae0a --- /dev/null +++ b/backend/snapstudio_core/creative/container.py @@ -0,0 +1,142 @@ +"""Bounded, read-only 3MF container inspection. + +Unknown members are represented only by their archive names and SHA-256 digests; +their payloads are never interpreted or rewritten. +""" +from __future__ import annotations + +import hashlib +import io +import posixpath +import zipfile +import zlib +from dataclasses import dataclass +from xml.etree import ElementTree + +CORE_NS = "http://schemas.microsoft.com/3dmanufacturing/core/2015/02" +MAX_ARCHIVE_BYTES = 128 * 1024 * 1024 +MAX_MEMBERS = 4096 +MAX_MEMBER_BYTES = 16 * 1024 * 1024 +MAX_TOTAL_BYTES = 64 * 1024 * 1024 +MAX_COMPRESSION_RATIO = 200 + + +class ContainerError(ValueError): + """The archive is malformed, unsafe, or exceeds reader limits.""" + + +@dataclass(frozen=True) +class MemberDigest: + name: str + sha256: str + + +@dataclass(frozen=True) +class ThreeMFManifest: + members: tuple[MemberDigest, ...] + understood_parts: tuple[str, ...] + passed_through: tuple[MemberDigest, ...] + model_xml: bytes + project_settings: dict | None + + def to_dict(self) -> dict: + """Serialize member identity only; original archive payloads stay untouched.""" + return {"schema_version": "creative_3mf_manifest/1", + "members": [{"name": item.name, "sha256": item.sha256} for item in self.members], + "understood_parts": list(self.understood_parts), + "passed_through": [{"name": item.name, "sha256": item.sha256} for item in self.passed_through]} + + +def _safe_name(name: str) -> str: + if not name or "\\" in name or name.startswith("/") or "\x00" in name: + raise ContainerError("unsafe_member_name") + if ".." in name.split("/"): + raise ContainerError("unsafe_member_name") + normalized = posixpath.normpath(name) + if normalized in (".", "..") or normalized.startswith("../") or ":" in normalized.split("/")[0]: + raise ContainerError("unsafe_member_name") + return normalized + + +def _check_model(xml: bytes) -> None: + # UTF-8 only. A byte-substring DOCTYPE check is bypassed by UTF-16/UTF-32 (NUL-interleaved), which expat + # auto-detects and then expands entities from, so the text is decoded first and anything else is refused. + try: + text = xml.decode("utf-8-sig") + except UnicodeDecodeError as exc: + raise ContainerError("bad_xml") from exc + if "\x00" in text: + raise ContainerError("unsafe_xml") + upper = text.upper() + if " ThreeMFManifest: + """Validate a small 3MF ZIP and return an immutable identity manifest. + + Container shape reference: captured FullSpectrum packaging notes list the + 3MF ZIP members and Metadata config paths at lines 13-38 and 74-76. + """ + if not isinstance(data, bytes): + raise TypeError("data must be bytes") + if len(data) > MAX_ARCHIVE_BYTES: + raise ContainerError("archive_too_large") + try: + archive = zipfile.ZipFile(io.BytesIO(data), "r") + except (zipfile.BadZipFile, OSError) as exc: + raise ContainerError("bad_zip") from exc + with archive: + infos = archive.infolist() + if len(infos) > MAX_MEMBERS: + raise ContainerError("too_many_members") + names: set[str] = set() + total = 0 + blobs: dict[str, bytes] = {} + for info in infos: + name = _safe_name(info.filename) + if name in names: + raise ContainerError("duplicate_member") + names.add(name) + if info.is_dir(): + continue + if info.file_size > MAX_MEMBER_BYTES: + raise ContainerError("member_too_large") + total += info.file_size + if total > MAX_TOTAL_BYTES: + raise ContainerError("archive_expanded_too_large") + if info.file_size and (info.compress_size == 0 or info.file_size / info.compress_size > MAX_COMPRESSION_RATIO): + raise ContainerError("compression_ratio_exceeded") + try: + blob = archive.read(info) + except (RuntimeError, zipfile.BadZipFile, OSError, NotImplementedError, zlib.error) as exc: + raise ContainerError("bad_zip_member") from exc + if len(blob) != info.file_size: + raise ContainerError("truncated_member") + blobs[name] = blob + model = blobs.get("3D/3dmodel.model") + if model is None: + raise ContainerError("missing_model") + _check_model(model) + settings = None + if "Metadata/project_settings.config" in blobs: + import json + try: + parsed = json.loads(blobs["Metadata/project_settings.config"]) + except (UnicodeDecodeError, json.JSONDecodeError, RecursionError) as exc: + raise ContainerError("bad_project_settings") from exc + if not isinstance(parsed, dict): + raise ContainerError("bad_project_settings") + settings = parsed + understood = ["3D/3dmodel.model"] + if settings is not None: + understood.append("Metadata/project_settings.config") + records = tuple(MemberDigest(n, hashlib.sha256(blobs[n]).hexdigest()) for n in sorted(blobs)) + passed = tuple(rec for rec in records if rec.name not in understood) + return ThreeMFManifest(records, tuple(understood), passed, model, settings) diff --git a/backend/snapstudio_core/creative/loss_report.py b/backend/snapstudio_core/creative/loss_report.py new file mode 100644 index 00000000..70950f5b --- /dev/null +++ b/backend/snapstudio_core/creative/loss_report.py @@ -0,0 +1,11 @@ +"""Loss reports for creative projects; reports are advisory and read-only.""" +from __future__ import annotations + +from .readers import CreativeProject + + +def document(project: CreativeProject) -> dict: + """Return per-format understood, passed-through and unknown fields.""" + report = project.loss_report() + report["evidence"] = "fixture-tested; fixtures are hand-written to published shapes, not captures" + return report diff --git a/backend/snapstudio_core/creative/readers.py b/backend/snapstudio_core/creative/readers.py new file mode 100644 index 00000000..b7545105 --- /dev/null +++ b/backend/snapstudio_core/creative/readers.py @@ -0,0 +1,194 @@ +"""Version-specific readers for documented creative project settings. + +Each reader is conservative: it extracts only published fields and leaves all +other project settings and archive members opaque. +""" +from __future__ import annotations + +import hashlib +import json +from dataclasses import dataclass, field +from typing import Any + +from snapstudio_core.foundation.recipes import RecipeComponent, VirtualRecipe + +from .container import ThreeMFManifest, read_3mf + +_FORCA_KEYS = frozenset({ + "fos_filament_nozzle", "fos_filament_mms_slot", "fos_physical_nozzle_diameter", + "fos_mms_topology", "fos_mms_unit_count", "fos_mms_head_ace", "mms_system", + "mms_host", "color_patch_loops", "color_patch_enabled", +}) +_LIX_KEYS = frozenset({"extruder_layer_height", "extruder_layer_height_exact"}) +_FULLSPECTRUM_KEYS = frozenset({"mixed_filament_definitions"}) + + +@dataclass +class CreativeProject: + source_format: str + artifact_identity: dict[str, Any] + understood: dict[str, Any] = field(default_factory=dict) + passed_through: tuple[dict[str, str], ...] = () + unknown: tuple[str, ...] = () + missing: tuple[str, ...] = () + diagnostics: tuple[str, ...] = () + recipes: tuple[VirtualRecipe, ...] = () + physical_slots_needed: int | None = None + source_component_ids: tuple[int, ...] = () + simulated: bool = True + + def loss_report(self) -> dict[str, Any]: + return {"source_format": self.source_format, "understood": sorted(self.understood), + "passed_through": list(self.passed_through), "unknown": list(self.unknown), + "missing": list(self.missing), "diagnostics": list(self.diagnostics)} + + +def _settings(source: bytes | dict) -> tuple[dict, ThreeMFManifest | None]: + if isinstance(source, dict): + return source, None + manifest = read_3mf(source) + return manifest.project_settings or {}, manifest + + +def _digest_obj(raw: dict) -> dict[str, Any]: + encoded = json.dumps(raw, sort_keys=True, separators=(",", ":")).encode() + return {"sha256": hashlib.sha256(encoded).hexdigest(), "size_bytes": len(encoded)} + + +def _reader(name: str, settings: dict, known: frozenset[str], *, require: tuple[str, ...] = (), + manifest: ThreeMFManifest | None = None) -> CreativeProject: + found = {k: v for k, v in settings.items() if k in known} + unknown = tuple(sorted(k for k in settings if k.startswith(("fos_", "extruder_layer_height", "mixed_filament_")) and k not in known)) + passthrough = tuple({"name": k, "sha256": _digest_obj({k: v})["sha256"]} for k, v in sorted(settings.items()) if k not in known) + if manifest is not None: + archive_parts = tuple({"name": member.name, "sha256": member.sha256} + for member in manifest.passed_through) + passthrough += archive_parts + missing = tuple(k for k in require if k not in settings) + return CreativeProject(name, _digest_obj(settings), found, passthrough, unknown, missing) + + +def read_forca(source: bytes | dict) -> CreativeProject: + """Read published FOrca extension keys only. + + Shape references: saved device-research entry FOrcaSlicer lines 134-150; + source blob receipt `fos_config` (PrintConfig.cpp lines 2175-2185, + 4067-4087 and 4664-4725). nozzle_diameter is deliberately not treated + as physical because the research records its per-filament slicing meaning. + """ + settings, manifest = _settings(source) + return _reader("forcaslicer", settings, _FORCA_KEYS, + require=("fos_physical_nozzle_diameter",), manifest=manifest) + + +def read_lixnix(source: bytes | dict) -> CreativeProject: + """Read the published LixNix per-extruder height settings. + + Shape references: saved device-research entry Full Multi-Nozzle/LixNix + lines 155-168; source blob receipts `lix_config` (PrintConfig.cpp lines + 5600-5622) and `lix_plan` (Slicing.hpp lines 212-289). + """ + settings, manifest = _settings(source) + result = _reader("lixnix", settings, _LIX_KEYS, + require=("extruder_layer_height", "extruder_layer_height_exact"), manifest=manifest) + heights = settings.get("extruder_layer_height") + if settings.get("extruder_layer_height_exact") is True and (not isinstance(heights, list) or not heights): + result.diagnostics = ("exact_flag_without_layer_height_values",) + return result + + +def _parse_fullspectrum(raw: Any) -> tuple[tuple[VirtualRecipe, ...], tuple[int, ...], bool]: + if not isinstance(raw, str) or not raw: + return (), (), isinstance(raw, str) + recipes = [] + slots: set[int] = set() + valid = True + for index, row in enumerate(raw.split(";")): + if not row: + continue + parts = row.split(",") + if len(parts) < 6 or any(not token.isdigit() for token in parts[:6]): + valid = False + continue + a, b = int(parts[0]), int(parts[1]) + if (a <= 0 or b <= 0 or parts[2] not in ("0", "1") or parts[3] not in ("0", "1") + or not 0 <= int(parts[4]) <= 100): + valid = False + continue + source_ids = [a, b] + for token in parts[6:]: + if token == "g": + continue + if token.startswith("g"): + gradient_ids = token[1:] + if not gradient_ids.isdigit() or "0" in gradient_ids: + valid = False + continue + source_ids.extend(int(digit) for digit in gradient_ids) + # The row names source component ids. Ratios are not asserted by this + # compact row grammar; preserve them as unknown. + unique_ids = tuple(dict.fromkeys(source_ids)) + slots.update(unique_ids) + components = [RecipeComponent(f"project:1:{n}", None, "unknown") for n in unique_ids] + stable_ids = [token[1:] for token in parts[6:] if token.startswith("u") and token[1:].isdigit()] + recipe_id = f"fullspectrum:{stable_ids[0]}" if len(stable_ids) == 1 else f"fullspectrum:row-{index}" + recipes.append(VirtualRecipe(recipe_id, {"name": "FullSpectrum", "version": "0.9.12"}, + "fullspectrum", components, False)) + return tuple(recipes), tuple(sorted(slots)), valid + + +def read_fullspectrum(source: bytes | dict) -> CreativeProject: + """Read the documented FullSpectrum mixed-row field into simulated recipes. + + Shape references: saved workflow-research entry `xipit-fullspectrum-ui` + lines 232-272 and the captured v0.9.12 packaging notes lines 74-76, + 162-211. The draft vnext schema is intentionally not read. + """ + settings, manifest = _settings(source) + result = _reader("fullspectrum", settings, _FULLSPECTRUM_KEYS, + require=("mixed_filament_definitions",), manifest=manifest) + raw = settings.get("mixed_filament_definitions") + result.recipes, result.source_component_ids, valid = _parse_fullspectrum(raw) + result.physical_slots_needed = len(result.source_component_ids) if valid else None + if raw is not None and (not isinstance(raw, str) or not valid): + result.unknown = tuple(sorted((*result.unknown, "mixed_filament_definitions"))) + result.understood.pop("mixed_filament_definitions", None) + result.diagnostics = ("unrecognized_fullspectrum_row_shape",) + return result + + +def read_imagemap(data: bytes) -> CreativeProject: + """Preserve ImageMap content while reporting its untraced serialized shape. + + Shape references: saved creative-research entry OrcaSlicer-ImageMap lines + 138-156. It explicitly records that the exact 3MF carrier remains untraced; + this reader therefore makes no field-level compatibility claim. + """ + manifest = read_3mf(data) + identity = {"size_bytes": len(data), "sha256": hashlib.sha256(data).hexdigest()} + passed = tuple({"name": item.name, "sha256": item.sha256} for item in manifest.members) + return CreativeProject("imagemap", identity, {}, passed, + ("ImageMap serialized metadata carrier and zone shape",), + diagnostics=("shape_not_established_in_research",)) + + +def read_lumina(data: bytes, *, file_id: str | None = None) -> CreativeProject: + """Return identity for a Lumina-produced 3MF without interpreting its settings. + + Shape references: saved creative-research entry Lumina Layers lines 80-105 + and source receipt `creative/Lumina-Layers/api__routers__converter.py` + lines 192-268, 469, 609, 730, 832 (generated files retrieved by file id). + """ + manifest = read_3mf(data) + name = "lumina:3mf" + identity = {"file_id": file_id, "artifact_name": name, "size_bytes": len(data), + "sha256": hashlib.sha256(data).hexdigest()} + passed = tuple({"name": item.name, "sha256": item.sha256} for item in manifest.members) + return CreativeProject("lumina", identity, {"3mf_artifact_identity": identity}, passed, + ("Lumina generation provenance",)) + + +def read_project_settings(data: bytes) -> tuple[ThreeMFManifest, dict]: + """Convenience seam used by 3MF format adapters.""" + manifest = read_3mf(data) + return manifest, manifest.project_settings or {} diff --git a/backend/snapstudio_core/localpath.py b/backend/snapstudio_core/localpath.py new file mode 100644 index 00000000..c53bb1c5 --- /dev/null +++ b/backend/snapstudio_core/localpath.py @@ -0,0 +1,113 @@ +"""One guard for every user-supplied file path the Studio+ tracks open. + +Studio is meant to read local files only. A network (UNC) path in any slash style, a Windows device path, a symlink or +junction that points at one of those, and anything that is not a regular file under the size cap is refused +before the engine opens, hashes or lists it. The checks are lexical first; the link walk then looks at each path +component with `lstat` (which never follows the component being examined) and reads a link's target text with +`readlink`, so a link to a network share is rejected without the engine ever connecting to it. + +These are best-effort checks, not a filesystem sandbox or an absolute no-network guarantee: the check and the +later open are separate steps, so a process that can modify the path or its ancestors in between can redirect +the operation. Callers that write (`passport export -o`) document that limit for their own output. + +Known limit: a mapped network drive letter looks like a local volume to the operating system, so it is allowed. +That is the user's own storage, chosen by them; Studio does not look behind a drive letter. On Linux and macOS a +mounted network filesystem looks local in the same way, and only UNC-style strings and link targets are refused. +""" +from __future__ import annotations + +import os +import stat + +MAX_LOCAL_FILE_BYTES = 2 * 1024 ** 3 +_MAX_LINK_DEPTH = 8 +# Windows reparse tags that redirect a path elsewhere: symlink and junction. Cloud placeholders are not links. +_REDIRECTING_REPARSE_TAGS = {0xA000000C, 0xA0000003} + + +def is_network_or_device_path(path: str) -> bool: + """UNC shares and the `\\\\?\\` / `\\\\.\\` device namespaces, in any mix of slash styles.""" + return isinstance(path, str) and path.replace("/", "\\").startswith("\\\\") + + +def _plain_drive_target(target: str) -> str: + r"""Windows readlink returns a link to C:\x as \\?\C:\x; that is a local drive, not a device namespace. + + The UNC form (\\?\UNC\...), the \\.\ form and volume-GUID forms keep their prefix and stay refused.""" + if len(target) >= 7 and target[:4] == "\\\\?\\" and "A" <= target[4].upper() <= "Z" and target[5:7] == ":\\": + return target[4:] + return target + + +def _is_redirect(info: os.stat_result) -> bool: + if stat.S_ISLNK(info.st_mode): + return True + return getattr(info, "st_reparse_tag", 0) in _REDIRECTING_REPARSE_TAGS + + +def _reaches_network(path: str, depth: int = 0) -> bool: + """True when the path, or any link on the way to it, names a network or device location.""" + if is_network_or_device_path(path): + return True + if depth > _MAX_LINK_DEPTH: + return True # too many links to follow safely: refuse + absolute = os.path.abspath(path) # no filesystem access + if is_network_or_device_path(absolute): + return True + drive, rest = os.path.splitdrive(absolute) + current = drive + os.sep + for part in [piece for piece in rest.split(os.sep) if piece]: + current = os.path.join(current, part) + try: + info = os.lstat(current) + except FileNotFoundError: + return False # nothing further exists to follow + except (OSError, ValueError): + return True + if _is_redirect(info): + try: + target = os.readlink(current) + except (OSError, ValueError): + return True + target = _plain_drive_target(target) + if not os.path.isabs(target) and not is_network_or_device_path(target): + target = os.path.join(os.path.dirname(current), target) + if _reaches_network(target, depth + 1): + return True + return False + + +def is_local_regular_file(path: object, *, max_bytes: int = MAX_LOCAL_FILE_BYTES) -> bool: + """True only for a regular file on a local volume no larger than `max_bytes`. Never raises.""" + if not isinstance(path, str) or not path or "\x00" in path or _reaches_network(path): + return False + try: + info = os.stat(path) + except (OSError, ValueError): + return False + return stat.S_ISREG(info.st_mode) and info.st_size <= max_bytes + + +def is_local_directory(path: object) -> bool: + """True only for an existing directory that is not a network or device location. Never raises.""" + if not isinstance(path, str) or not path or "\x00" in path or _reaches_network(path): + return False + try: + return stat.S_ISDIR(os.stat(path).st_mode) + except (OSError, ValueError): + return False + + +def usable_path(path: str | None, *, max_bytes: int = MAX_LOCAL_FILE_BYTES) -> str | None: + """`path` unless opening it could leave the machine or hash an oversize file; None means treat as unverified. + + A missing path is returned as is, so callers still report it as missing. + """ + if not isinstance(path, str) or not path or "\x00" in path or _reaches_network(path): + return None + try: + if os.path.isfile(path) and os.path.getsize(path) > max_bytes: + return None + except (OSError, ValueError): + return None + return path diff --git a/backend/snapstudio_core/monitoring_context/__init__.py b/backend/snapstudio_core/monitoring_context/__init__.py new file mode 100644 index 00000000..8e4852dc --- /dev/null +++ b/backend/snapstudio_core/monitoring_context/__init__.py @@ -0,0 +1,90 @@ +"""Bounded, read-only monitoring context from configured local Moonraker hosts. + +Two GETs only, both through `foundation.netguard.local_request`: `/printer/objects/query` (state) +and `/server/history/list` (last job). There is no websocket: "events" are derived by comparing +polled statuses, so a transition that happens between two polls is never seen, only inferred. +Nothing here sends a control request. A filename match is never proof of which artifact runs. +""" + +from __future__ import annotations + +import json + +from snapstudio_core import moonraker +from snapstudio_core.foundation import netguard + +READ_TIMEOUT_S = 1.5 +MAX_RESPONSE_BYTES = 256 * 1024 +ASSOCIATION_NOTE = "Filename match is unverified; Studio has not confirmed the local artifact." +# Moonraker camera context lives behind endpoints this module does not read. +CAMERA_UNKNOWN = {"state": "unknown", "reason": "camera_context_not_read"} + + +def _origin(host: str, port: int) -> str: + checked_host = moonraker.validate_host(host) + if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535: + raise ValueError("invalid port") + return f"http://{checked_host}:{port}" + + +def _get(origin: str, path_and_query: str, resolver) -> dict | None: + result = netguard.local_request("GET", origin + path_and_query, origin=origin, + timeout=READ_TIMEOUT_S, max_bytes=MAX_RESPONSE_BYTES, + resolver=resolver) + if not 200 <= result.status < 300: + return None + try: + data = json.loads(result.body.decode("utf-8")) + except (ValueError, UnicodeDecodeError): + return None + return data if isinstance(data, dict) else None + + +def read(host: str, port: int = moonraker.DEFAULT_PORT, *, resolver=None) -> dict: + """One bounded status GET. Unsupported or absent fields stay unknown.""" + origin = _origin(host, port) + data = _get(origin, "/printer/objects/query?print_stats&virtual_sdcard", resolver) + if data is None: + return {"status": "unavailable", "reason": "invalid_reply", "camera": dict(CAMERA_UNKNOWN)} + result = data.get("result") + values = result.get("status") if isinstance(result, dict) else None + stats = values.get("print_stats") if isinstance(values, dict) else None + filename = stats.get("filename") if isinstance(stats, dict) else None + state = stats.get("state") if isinstance(stats, dict) else None + filename = filename if isinstance(filename, str) and filename else None + return {"status": "ok", "print_state": state if isinstance(state, str) else "unknown", + "filename": filename, "association": "unverified" if filename else "unknown", + "camera": dict(CAMERA_UNKNOWN), "note": ASSOCIATION_NOTE} + + +def read_history(host: str, port: int = moonraker.DEFAULT_PORT, *, resolver=None) -> dict: + """One bounded GET of the most recent job record. Never more than one entry is kept.""" + origin = _origin(host, port) + data = _get(origin, "/server/history/list?limit=1&order=desc", resolver) + if data is None: + return {"status": "unavailable", "reason": "invalid_reply"} + result = data.get("result") + jobs = result.get("jobs") if isinstance(result, dict) else None + job = jobs[0] if isinstance(jobs, list) and jobs and isinstance(jobs[0], dict) else None + if job is None: + return {"status": "ok", "last_job": None} + name, outcome = job.get("filename"), job.get("status") + name = name if isinstance(name, str) and name else None + return {"status": "ok", "association": "unverified" if name else "unknown", + "last_job": {"filename": name, "outcome": outcome if isinstance(outcome, str) else "unknown"}, + "note": ASSOCIATION_NOTE} + + +def reconcile(previous_state: str | None, current: dict) -> dict: + """Derive events from polling: compare the last state a caller saw with a fresh status. + + The fresh status is the truth. When the state differs, the states in between (if any) are + unknown, so the result says so instead of inventing a sequence. + """ + state = current.get("print_state", "unknown") if current.get("status") == "ok" else "unknown" + events = [] + if isinstance(previous_state, str) and previous_state != state: + events.append({"kind": "state_changed", "from": previous_state, "to": state, "source": "poll"}) + return {"state": state, "events": events, "source": "polling", + "intermediate_states_known": False, + "reconciled_from_status": True} diff --git a/backend/snapstudio_core/passport/__init__.py b/backend/snapstudio_core/passport/__init__.py new file mode 100644 index 00000000..ed79d194 --- /dev/null +++ b/backend/snapstudio_core/passport/__init__.py @@ -0,0 +1,45 @@ +"""Job passport: the derivation chain of a job and what travelled with it. + + build_passport(entries) files -> passport (declared chain by default) + import_passport(raw) passport JSON -> normalised passport, always declared + verify_passport(passport) are the files still the bytes the passport recorded? + +Evidence: fixture-tested only. Studio does not watch a prepare or a slice happen, so a chain +is declared by whoever states it; verification checks bytes, not who made them. +""" +from __future__ import annotations + +from typing import Any + +from .build import BuildResult, build_passport +from .model import SCHEMA_VERSION, Passport, decode_passport +from .verify import verify_passport + +__all__ = ["BuildResult", "Passport", "SCHEMA_VERSION", "build_passport", "decode_passport", + "import_passport", "verify_passport"] + + +def import_passport(raw: Any, *, base_dir: str | None = None, paths: dict | None = None) -> dict: + """Decode `raw` and, when `base_dir` or `paths` say where the files are, verify it. + + Never raises. `passport` is None when the document cannot be used (see `diagnostics`). + """ + decoded = decode_passport(raw) + out: dict = {"passport": None, "diagnostics": [d.to_dict() for d in decoded.diagnostics], + "losses": [], "parentage": [], "verification": None} + passport = decoded.value + if passport is None: + return out + out["passport"] = passport.to_dict() + out["losses"] = list(passport.losses) + if base_dir or paths: + report = verify_passport(passport, base_dir=base_dir, paths=paths) + out["verification"] = report + out["parentage"] = report["derivations"] + else: + by_id = {a.artifact_id: a.name for a in passport.artifacts} + out["parentage"] = [{"output": by_id[d.output], "inputs": [{"kind": i["kind"], "name": by_id[i["ref"]]} + for i in d.inputs], + "producer": dict(d.producer), "witness": d.witness, "declared_by": d.declared_by, + "declared_mismatch": None, "status": "unverified"} for d in passport.derivations] + return out diff --git a/backend/snapstudio_core/passport/build.py b/backend/snapstudio_core/passport/build.py new file mode 100644 index 00000000..57ce596b --- /dev/null +++ b/backend/snapstudio_core/passport/build.py @@ -0,0 +1,118 @@ +"""Build a passport from files the user names. Files are only read, never written. + +`entries` are ordered. Each is `{path, role, parents?, producer?}`: `parents` are indices of +EARLIER entries this one was made from (default: the entry before it, except for an original, +which has none). Because a parent must come earlier the chain cannot loop. + +The chain is stated by the caller, so it is recorded `declared` (by "user") unless a Studio +step that actually ran the transformation passes `witness="local"`. The routes and the CLI +never do: this version does not observe a prepare or a slice happening. +""" +from __future__ import annotations + +import os +from snapstudio_core import localpath +from dataclasses import dataclass, field +from typing import Any + +from snapstudio_core.foundation.artifacts import ArtifactRef, Derivation +from snapstudio_core.foundation.decode import ERROR, NOTE, WARNING, Diagnostic, diag + +from .model import ( + CHAIN_ROLES, MAX_ARTIFACTS, Passport, compute_passport_id, hash_file, retain_mappings, retain_recipes, + utc_now_iso, +) + +DECLARED_BY = "user" +DEFAULT_PRODUCER = {"tool": "unspecified", "version": None} + + +@dataclass +class BuildResult: + passport: Passport | None + diagnostics: list = field(default_factory=list) + #: artifact_id -> the path it was read from. Local only: never part of the passport. + paths: dict = field(default_factory=dict) + + def to_dict(self) -> dict: + return {"passport": self.passport.to_dict() if self.passport else None, + "diagnostics": [d.to_dict() for d in self.diagnostics], + "losses": list(self.passport.losses) if self.passport else []} + + +def _fail(code: str, where: str) -> BuildResult: + return BuildResult(None, [diag(code, where, ERROR)]) + + +def build_passport(entries: Any, *, recipes: Any = None, material_mappings: Any = None, + witness: str = "declared", created_at: str | None = None) -> BuildResult: + """Never raises on bad input; `passport` is None when an error diagnostic says why.""" + if witness not in ("declared", "local"): + raise ValueError(f"unknown witness {witness!r}") + if not isinstance(entries, list) or not entries: + return _fail("no_entries", "entries") + if len(entries) > MAX_ARTIFACTS: + return _fail("too_many_entries", "entries") + diagnostics: list[Diagnostic] = [] + refs: list = [] # one ArtifactRef per entry (entries with identical bytes share the first) + paths: dict = {} + for i, entry in enumerate(entries): + where = f"entries[{i}]" + if not isinstance(entry, dict) or not isinstance(entry.get("path"), str) or not entry["path"].strip(): + return _fail("bad_entry", where) + role = entry.get("role") + if role not in CHAIN_ROLES: + return _fail("bad_role", f"{where}.role") + path = entry["path"] + try: + if localpath.is_network_or_device_path(path) or localpath.usable_path(path) is None: + return _fail("file_unreadable", f"{where}.path") + if not os.path.isfile(path): + return _fail("file_missing", f"{where}.path") + sha, size = hash_file(path) + ref = ArtifactRef.create(sha, size, role, os.path.basename(path)) + except OSError: + return _fail("file_unreadable", f"{where}.path") + refs.append(ref) + paths.setdefault(ref.artifact_id, os.path.abspath(path)) + artifacts: list = [] + for i, ref in enumerate(refs): + if any(a.artifact_id == ref.artifact_id for a in artifacts): + diagnostics.append(diag("identical_bytes", f"entries[{i}]", NOTE)) + else: + artifacts.append(ref) + derivations: list = [] + for i, entry in enumerate(entries): + where = f"entries[{i}]" + parents = entry.get("parents") + if parents is None: + parents = [] if entry["role"] == "original" or i == 0 else [i - 1] + if (not isinstance(parents, list) or not all(isinstance(p, int) and not isinstance(p, bool) + and 0 <= p < i for p in parents) + or len(set(parents)) != len(parents)): + return _fail("bad_parents", f"{where}.parents") + if not parents: + continue + inputs = [{"kind": "settings" if entries[p]["role"] == "settings" else "artifact", + "ref": refs[p].artifact_id} for p in parents] + if any(inp["ref"] == refs[i].artifact_id for inp in inputs): + diagnostics.append(diag("identical_to_parent", where, WARNING)) # same bytes: nothing was derived + continue + producer = entry.get("producer") if isinstance(entry.get("producer"), dict) else DEFAULT_PRODUCER + tool = producer.get("tool") + version = producer.get("version") + if not isinstance(tool, str) or not tool.strip() or len(tool) > 64 \ + or (version is not None and (not isinstance(version, str) or len(version) > 64)): + return _fail("bad_producer", f"{where}.producer") + derivation = Derivation.create( + refs[i].artifact_id, inputs, {"tool": tool, "version": version}, witness=witness, + declared_by=DECLARED_BY if witness == "declared" else None) + if all(d.derivation_id != derivation.derivation_id for d in derivations): + derivations.append(derivation) + kept_recipes, recipe_losses, recipe_diags = retain_recipes(recipes) + kept_mappings, mapping_losses = retain_mappings(material_mappings) + diagnostics.extend(recipe_diags) + passport = Passport(compute_passport_id(artifacts, derivations, kept_recipes, kept_mappings), + created_at or utc_now_iso(), artifacts, derivations, kept_recipes, kept_mappings, + recipe_losses + mapping_losses) + return BuildResult(passport, diagnostics, paths) diff --git a/backend/snapstudio_core/passport/model.py b/backend/snapstudio_core/passport/model.py new file mode 100644 index 00000000..852e1035 --- /dev/null +++ b/backend/snapstudio_core/passport/model.py @@ -0,0 +1,250 @@ +"""The passport document: which files a job was made from, and what was kept or lost. + +A passport records the derivation chain original -> prepared -> sliced (or any generated +artifact) as `foundation.artifacts` byte identities and derivations, plus the recipes and +material mappings that travelled with the job. It never contains a local path: names only. + +Who vouches for a link matters. A chain the user states when building a passport, and any +chain read from a passport file, is `declared`; only a derivation Studio itself witnessed +could be `local`, and importing can never produce one. Anything that cannot be carried over +is listed in `losses`, never dropped silently. +""" +from __future__ import annotations + +import dataclasses +import datetime +import hashlib +import re +from dataclasses import dataclass, field +from typing import Any + +from snapstudio_core.foundation import recipes as _recipes +from snapstudio_core.foundation.artifacts import ArtifactRef, Derivation +from snapstudio_core.foundation.decode import ( + ERROR, NOTE, WARNING, Decoded, Diagnostic, Reader, canonical_json, decode_document, diag, +) + +SCHEMA_VERSION = "passport/1" +PRODUCER = {"tool": "snapstudio-passport", "version": None} + +#: Roles a job chain may carry. Others in foundation.artifacts.ROLES belong to other tracks. +CHAIN_ROLES = ("original", "prepared", "generated", "sliced", "settings") + +MAX_ARTIFACTS = 64 +MAX_DERIVATIONS = 128 +MAX_RECIPES = 64 +MAX_MAPPINGS = 64 +MAX_TEXT = 64 + +_SLOT_REF_RE = re.compile(r"^(slot:\d{1,3}|project:[01]:\d{1,3})$") +_COLOUR_RE = re.compile(r"^#[0-9A-Fa-f]{6}$") +_MAPPING_KEYS = ("slot_ref", "material", "colour", "spool") + + +def utc_now_iso() -> str: + return datetime.datetime.now(datetime.timezone.utc).isoformat().replace("+00:00", "Z") + + +def hash_file(path: str) -> tuple[str, int]: + """sha256 and size of a file, read in chunks. Raises OSError; the caller reports it.""" + digest, total = hashlib.sha256(), 0 + with open(path, "rb") as handle: + while True: + chunk = handle.read(1024 * 1024) + if not chunk: + return digest.hexdigest(), total + digest.update(chunk) + total += len(chunk) + + +def loss(kind: str, ident: Any, reason: str) -> dict: + return {"kind": kind, "id": str(ident)[:MAX_TEXT * 2], "reason": reason} + + +@dataclass +class Passport: + passport_id: str + created_at: str + artifacts: list + derivations: list + recipes: list = field(default_factory=list) + material_mappings: list = field(default_factory=list) + losses: list = field(default_factory=list) + + def to_dict(self) -> dict: + return {"schema_version": SCHEMA_VERSION, "passport_id": self.passport_id, + "created_at": self.created_at, "producer": dict(PRODUCER), + "artifacts": [a.to_dict() for a in self.artifacts], + "derivations": [d.to_dict() for d in self.derivations], + "recipes": [r.to_dict() for r in self.recipes], + "material_mappings": [dict(m) for m in self.material_mappings], + "losses": [dict(item) for item in self.losses]} + + +def compute_passport_id(artifacts: list, derivations: list, recipes: list, mappings: list) -> str: + """Content-derived: the same chain, recipes and mappings always give the same id.""" + body = {"artifacts": sorted(a.artifact_id for a in artifacts), + "derivations": sorted(d.derivation_id for d in derivations), + "recipes": sorted((r.to_dict() for r in recipes), key=lambda r: r["recipe_id"]), + "mappings": sorted((dict(m) for m in mappings), key=lambda m: m["slot_ref"])} + return "pp:" + hashlib.sha256(canonical_json(body).encode("utf-8")).hexdigest()[:40] + + +# -- recipes and mappings: retained, or reported lost --------------------------------- +def retain_recipes(raws: Any) -> tuple[list, list, list]: + """(recipes, losses, diagnostics). A recipe that cannot be decoded is reported lost, never hidden.""" + kept: list = [] + losses: list = [] + diagnostics: list[Diagnostic] = [] + if raws is None: + return kept, losses, diagnostics + if not isinstance(raws, list): + return kept, [loss("recipe", "recipes", "not_a_list")], [diag("recipes_not_a_list", "recipes", ERROR)] + if len(raws) > MAX_RECIPES: + return kept, [loss("recipe", "recipes", "too_many")], [diag("too_many_recipes", "recipes", ERROR)] + seen: set = set() + for i, raw in enumerate(raws): + ident = raw.get("recipe_id") if isinstance(raw, dict) and isinstance(raw.get("recipe_id"), str) else f"#{i}" + decoded = _recipes.VirtualRecipe.from_dict(raw if isinstance(raw, dict) else None) + if decoded.value is None: + code = decoded.diagnostics[0].code if decoded.diagnostics else "undecodable" + losses.append(loss("recipe", ident, code)) + continue + if decoded.value.recipe_id in seen: + losses.append(loss("recipe", ident, "duplicate")) + continue + seen.add(decoded.value.recipe_id) + recipe, found = _recipes.assess(decoded.value) + diagnostics.extend(diag(d.code, f"recipes[{i}].{d.field}", d.severity) for d in found) + if recipe.execution_supported is not False: + # A passport only records a recipe; whatever execution claim came with it is not Studio's to repeat. + diagnostics.append(diag("execution_claim_ignored", f"recipes[{i}].execution_supported", NOTE)) + recipe = dataclasses.replace(recipe, execution_supported=False) + kept.append(recipe) + return kept, losses, diagnostics + + +def retain_mappings(raws: Any) -> tuple[list, list]: + """(mappings, losses). Only slot_ref, material, colour and spool are kept; a dropped field is listed.""" + kept: list = [] + losses: list = [] + if raws is None: + return kept, losses + if not isinstance(raws, list): + return kept, [loss("material_mapping", "material_mappings", "not_a_list")] + if len(raws) > MAX_MAPPINGS: + return kept, [loss("material_mapping", "material_mappings", "too_many")] + seen: set = set() + for i, raw in enumerate(raws): + ident = raw.get("slot_ref") if isinstance(raw, dict) and isinstance(raw.get("slot_ref"), str) else f"#{i}" + if not isinstance(raw, dict) or not isinstance(raw.get("slot_ref"), str) \ + or not _SLOT_REF_RE.match(raw["slot_ref"]): + losses.append(loss("material_mapping", ident, "bad_slot_ref")) + continue + if raw["slot_ref"] in seen: + losses.append(loss("material_mapping", ident, "duplicate")) + continue + entry: dict = {"slot_ref": raw["slot_ref"]} + material, colour, spool = raw.get("material"), raw.get("colour"), raw.get("spool") + if isinstance(material, str) and 0 < len(material.strip()) <= MAX_TEXT: + entry["material"] = material.strip() + elif material is not None: + losses.append(loss("material_mapping", ident, "bad_material")) + if isinstance(colour, str) and _COLOUR_RE.match(colour): + entry["colour"] = colour.upper() + elif colour is not None: + losses.append(loss("material_mapping", ident, "bad_colour")) + if isinstance(spool, dict) and all(isinstance(spool.get(k), str) and 0 < len(spool[k]) <= 128 + for k in ("provider_instance_id", "provider_spool_id")): + entry["spool"] = {"provider_instance_id": spool["provider_instance_id"], + "provider_spool_id": spool["provider_spool_id"]} + elif spool is not None: + losses.append(loss("material_mapping", ident, "bad_spool")) + for key in raw: + if key not in _MAPPING_KEYS: + losses.append(loss("material_mapping", ident, f"field_not_retained:{str(key)[:32]}")) + seen.add(raw["slot_ref"]) + kept.append(entry) + return kept, losses + + +# -- decoding an imported passport ---------------------------------------------------- +def decode_passport(raw: Any) -> Decoded[Passport]: + """Decode a passport from a file or request. Never raises. + + Every derivation comes back `declared`; locators are discarded (a foreign path is neither + trusted nor read); an artifact or derivation that cannot be decoded, or that refers to + something not in the passport, is dropped and listed in `losses`. + """ + return decode_document(raw, SCHEMA_VERSION, _build_passport) + + +def _prefixed(source: list, prefix: str, sink: list) -> None: + """Child diagnostics, re-pathed; the child is dropped on error so they only warn here.""" + for d in source: + sink.append(diag(d.code, f"{prefix}.{d.field}".rstrip("."), WARNING)) + + +def _item_id(item: Any, key: str, index: int) -> str: + return str(item.get(key)) if isinstance(item, dict) and isinstance(item.get(key), str) else f"#{index}" + + +def _build_passport(r: Reader) -> Passport: + for name, cap in (("artifacts", MAX_ARTIFACTS), ("derivations", MAX_DERIVATIONS), + ("recipes", MAX_RECIPES), ("material_mappings", MAX_MAPPINGS)): + if isinstance(r.raw.get(name), list) and len(r.raw[name]) > cap: + r.error(f"too_many_{name}", name) + if any(d.severity == ERROR for d in r.diagnostics): + return Passport("", "", [], []) + losses: list = [] + artifacts: list = [] + for i, item in enumerate(r.items("artifacts", required=True)): + decoded = ArtifactRef.from_dict(item) + if decoded.value is None: + _prefixed(decoded.diagnostics, f"artifacts[{i}]", r.diagnostics) + reason = decoded.diagnostics[0].code if decoded.diagnostics else "undecodable" + losses.append(loss("artifact", _item_id(item, "artifact_id", i), reason)) + continue + ref = decoded.value + if ref.role not in CHAIN_ROLES: + losses.append(loss("artifact", ref.artifact_id, "role_not_supported")) + continue + if ref.locator is not None: + r.note("locator_dropped", f"artifacts[{i}].locator") + ref.locator = None + if any(a.artifact_id == ref.artifact_id for a in artifacts): + losses.append(loss("artifact", ref.artifact_id, "duplicate")) + continue + artifacts.append(ref) + if not artifacts: + r.error("no_artifacts", "artifacts") + known = {a.artifact_id for a in artifacts} + derivations: list = [] + for i, item in enumerate(r.items("derivations", required=True)): + decoded = Derivation.from_dict(item, imported=True) + _prefixed(decoded.diagnostics, f"derivations[{i}]", r.diagnostics) + if decoded.value is None: + code = decoded.diagnostics[0].code if decoded.diagnostics else "undecodable" + losses.append(loss("derivation", _item_id(item, "derivation_id", i), code)) + continue + drv = decoded.value + if drv.output not in known or any(inp["ref"] not in known for inp in drv.inputs): + losses.append(loss("derivation", drv.derivation_id, "dangling_ref")) + continue + if any(d.derivation_id == drv.derivation_id for d in derivations): + losses.append(loss("derivation", drv.derivation_id, "duplicate")) + continue + derivations.append(drv) + recipes, recipe_losses, recipe_diags = retain_recipes(r.raw.get("recipes")) + mappings, mapping_losses = retain_mappings(r.raw.get("material_mappings")) + r.diagnostics.extend(diag(d.code, d.field, WARNING) for d in recipe_diags) + recorded = r.raw.get("losses") + carried = ([loss(str(x.get("kind"))[:32], x.get("id"), str(x.get("reason"))[:64]) + for x in recorded if isinstance(x, dict)][:MAX_ARTIFACTS] if isinstance(recorded, list) else []) + created = r.string("created_at", required=False) or "" + new_id = compute_passport_id(artifacts, derivations, recipes, mappings) + declared_id = r.string("passport_id", required=False) + if declared_id and declared_id != new_id: + r.warn("passport_id_recomputed", "passport_id") + return Passport(new_id, created, artifacts, derivations, recipes, mappings, + carried + losses + recipe_losses + mapping_losses) diff --git a/backend/snapstudio_core/passport/verify.py b/backend/snapstudio_core/passport/verify.py new file mode 100644 index 00000000..504c91e0 --- /dev/null +++ b/backend/snapstudio_core/passport/verify.py @@ -0,0 +1,120 @@ +"""Check a passport against the files that exist now. Files are only read. + +For each artifact: do the bytes on disk still match (`same_bytes`)? Then the chain: a changed +file invalidates itself and everything derived from it, because the record "B was made from A" +no longer describes the A that exists. A file that cannot be found or read makes its lineage +`unverified`, never `intact`. `intact` means the bytes match what the passport recorded; for a +`declared` link it is still the declarer's word, which the output says. + +A declared derivation whose input exists locally with different bytes is flagged +`declared_mismatch`. It is never promoted to `local`. +""" +from __future__ import annotations + +import os +from snapstudio_core import localpath +from typing import Any + +from snapstudio_core.foundation.artifacts import CHANGED, MISSING, SAME, UNREADABLE, UNVERIFIED, same_bytes + +from .model import Passport + +INTACT, INVALIDATED = "intact", "invalidated" +_BAD = (MISSING, UNREADABLE, UNVERIFIED) + + +def _resolve(artifact: Any, names: dict, paths: dict, base_dir: str | None) -> str | None: + """The local file for an artifact: an explicit path, else a unique file name inside `base_dir`.""" + explicit = paths.get(artifact.artifact_id) or (paths.get(artifact.name) if names[artifact.name] == 1 else None) + if explicit: + return localpath.usable_path(explicit) + if base_dir and names[artifact.name] == 1 and _is_bare_file_name(artifact.name): + return localpath.usable_path(os.path.join(base_dir, artifact.name)) + return None + + +def _is_bare_file_name(name: str) -> bool: + """A single file name, whichever OS wrote the passport: no separator of either style, no drive, no dot-dot.""" + return bool(name) and name not in (".", "..") and not any(ch in name for ch in ("/", "\\", ":", "\x00")) + + +def _ancestors(artifact_id: str, parents: dict) -> list: + """Every artifact the given one was derived from, nearest first. Cycle-safe (declared data may loop).""" + seen: list = [] + queue = list(parents.get(artifact_id, [])) + while queue: + current = queue.pop(0) + if current == artifact_id or current in seen: + continue + seen.append(current) + queue.extend(parents.get(current, [])) + return seen + + +def verify_passport(passport: Passport, *, base_dir: str | None = None, paths: dict | None = None) -> dict: + paths = paths or {} + by_id = {a.artifact_id: a for a in passport.artifacts} + names: dict = {} + for artifact in passport.artifacts: + names[artifact.name] = names.get(artifact.name, 0) + 1 + state = {a.artifact_id: same_bytes(a, _resolve(a, names, paths, base_dir)) for a in passport.artifacts} + parents: dict = {} + for drv in passport.derivations: + parents.setdefault(drv.output, []).extend(i["ref"] for i in drv.inputs) + + artifact_rows = [] + for artifact in passport.artifacts: + aid = artifact.artifact_id + reasons: list = [] + if state[aid] == CHANGED: + reasons.append({"code": "bytes_changed", "artifact": artifact.name}) + for anc in _ancestors(aid, parents): + if state[anc] == CHANGED: + reasons.append({"code": "ancestor_changed", "artifact": by_id[anc].name}) + if reasons: + chain = INVALIDATED + else: + if state[aid] in _BAD: + reasons.append({"code": f"bytes_{state[aid]}", "artifact": artifact.name}) + for anc in _ancestors(aid, parents): + if state[anc] in _BAD: + reasons.append({"code": f"ancestor_{state[anc]}", "artifact": by_id[anc].name}) + chain = UNVERIFIED if reasons else INTACT + artifact_rows.append({"artifact_id": aid, "name": artifact.name, "role": artifact.role, + "size_bytes": artifact.bytes.size_bytes, "sha256": artifact.bytes.sha256, + "bytes": state[aid], "chain": chain, "reasons": reasons}) + + derivation_rows = [] + for drv in passport.derivations: + involved = [drv.output] + [i["ref"] for i in drv.inputs] + if any(state[x] == CHANGED for x in involved): + status = INVALIDATED + elif any(state[x] != SAME for x in involved): + status = UNVERIFIED + else: + status = INTACT + input_states = [state[i["ref"]] for i in drv.inputs] + if drv.witness == "declared": + mismatch: bool | None = True if CHANGED in input_states else (False if all( + s == SAME for s in input_states) else None) + else: + mismatch = False + derivation_rows.append({ + "derivation_id": drv.derivation_id, "output": by_id[drv.output].name, + "inputs": [{"kind": i["kind"], "name": by_id[i["ref"]].name} for i in drv.inputs], + "producer": dict(drv.producer), "witness": drv.witness, "declared_by": drv.declared_by, + "declared_mismatch": mismatch, "status": status}) + + def names_of(chain: str) -> list: + return [r["name"] for r in artifact_rows if r["chain"] == chain] + + summary = {"all_intact": bool(artifact_rows) and all(r["chain"] == INTACT for r in artifact_rows), + "intact": names_of(INTACT), "invalidated": names_of(INVALIDATED), + "unverified": names_of(UNVERIFIED), + "declared_mismatch": [d["output"] for d in derivation_rows if d["declared_mismatch"]]} + return {"passport_id": passport.passport_id, "artifacts": artifact_rows, "derivations": derivation_rows, + "recipes_retained": [r.recipe_id for r in passport.recipes], + "material_mappings_retained": [m["slot_ref"] for m in passport.material_mappings], + "losses": list(passport.losses), "summary": summary, + "note": ("A match means the bytes are the same as the passport recorded. A declared link is the " + "declarer's statement; Studio did not watch the file being made.")} diff --git a/backend/snapstudio_core/passport_cli.py b/backend/snapstudio_core/passport_cli.py new file mode 100644 index 00000000..0967f6d9 --- /dev/null +++ b/backend/snapstudio_core/passport_cli.py @@ -0,0 +1,183 @@ +"""`u1convert passport export | import | verify` - the connector contract for other programs. + +Contract (see docs/internal/PASSPORT.md): + +* stdout is exactly ONE top-level JSON object and nothing else, in every outcome Studio handles. + Studio does not promise that taking the text between the last "{" and "}" works: string + values (file names, for one) may contain braces. Parse the whole of stdout. +* exit 0 = the command did what it was asked and the JSON says `"ok": true`; + exit 1 = a handled failure or a negative answer, still with a JSON object (`"ok": false`). +* a path that does not exist is rejected by the command-line layer before any of this runs: + exit 2, usage text on stderr, NO JSON. A caller must treat absent or invalid JSON as failure. + +Nothing here writes next to or over an input: `export -o` refuses an existing file and any +path that is one of the inputs. + +Publication: `export -o` pins the output directory once (a held descriptor on POSIX; a handle that +forbids rename/delete on Windows) and writes through that pin (snapstudio_core.safe_publish), so swapping the +directory or an ancestor after the check cannot redirect the write. It is still not a filesystem +sandbox: someone who can already write inside the pinned directory can create or remove entries there, +and the UNC/device/link refusals are checks on the pinned path, not an absolute no-network guarantee. +See docs/internal/PASSPORT.md. +""" +from __future__ import annotations + +import json +import os +from functools import wraps +from typing import Any + +import click + +from snapstudio_core import localpath +from snapstudio_core.safe_publish import UnsafeOutput, publish_new_file +from snapstudio_core.passport import build_passport, import_passport, verify_passport +from snapstudio_core.passport.model import decode_passport + +MAX_PASSPORT_BYTES = 2 * 1024 * 1024 +MAX_SIDE_BYTES = 1024 * 1024 + + +class _LocalPath(click.Path): + """click.Path that refuses network and device locations before it touches the filesystem.""" + + def convert(self, value, param, ctx): + text = os.fspath(value) if isinstance(value, (str, os.PathLike)) else "" + if localpath._reaches_network(text): + self.fail("Studio reads local files only.", param, ctx) + return super().convert(value, param, ctx) + + +_FILE = _LocalPath(exists=True, dir_okay=False) +_DIR = _LocalPath(exists=True, file_okay=False) + + +def _emit(obj: dict, code: int) -> None: + click.echo(json.dumps(obj, indent=2, ensure_ascii=True)) + raise SystemExit(code) + + +def _error(code: str, message: str) -> None: + _emit({"ok": False, "error": {"code": code, "message": message}}, 1) + + +def _handled(fn): + """Any failure Studio did not foresee is still one JSON object and exit 1, never a traceback.""" + @wraps(fn) + def wrapper(*args, **kwargs): + try: + return fn(*args, **kwargs) + except (SystemExit, click.ClickException, click.exceptions.Exit): + raise + except Exception: # noqa: BLE001 - the contract is "always JSON" + _error("internal", "The passport command failed unexpectedly.") + return wrapper + + +def _read_json(path: str, limit: int, what: str) -> Any: + if not localpath.is_local_regular_file(path, max_bytes=limit): + _error("unreadable", f"The {what} file could not be read.") + try: + with open(path, "rb") as handle: + data = handle.read(limit + 1) + except OSError: + _error("unreadable", f"The {what} file could not be read.") + if len(data) > limit: + _error("too_large", f"The {what} file is too large.") + try: + return json.loads(data.decode("utf-8")) + except (ValueError, UnicodeDecodeError, RecursionError): + _error("malformed_json", f"The {what} file is not valid JSON.") + + +def _locations(pairs: tuple) -> dict: + out = {} + for item in pairs: + name, sep, path = item.partition("=") + if not sep or not name or not path: + _error("bad_path_option", "--path takes NAME=PATH.") + out[name] = path + return out + + +@click.group() +def passport(): + """Job passport: record, import and check which files a job came from.""" + + +@passport.command("export") +@click.option("--original", type=_FILE, default=None) +@click.option("--prepared", type=_FILE, default=None) +@click.option("--sliced", type=_FILE, default=None) +@click.option("--recipes", "recipes_file", type=_FILE, default=None, help="JSON list of virtual_recipe/1 documents") +@click.option("--mappings", "mappings_file", type=_FILE, default=None, help="JSON list of material mappings") +@click.option("-o", "--out", default=None, help="write the passport here (refuses an existing file)") +@_handled +def export_cmd(original, prepared, sliced, recipes_file, mappings_file, out): + chain = [(role, path) for role, path in (("original", original), ("prepared", prepared), ("sliced", sliced)) + if path] + if not chain: + _error("no_files", "Give at least one of --original, --prepared, --sliced.") + recipes = _read_json(recipes_file, MAX_SIDE_BYTES, "recipes") if recipes_file else None + mappings = _read_json(mappings_file, MAX_SIDE_BYTES, "mappings") if mappings_file else None + built = build_passport([{"path": p, "role": r, "producer": {"tool": "user"}} for r, p in chain], + recipes=recipes, material_mappings=mappings) + if built.passport is None: + _emit({"ok": False, "error": {"code": built.diagnostics[0].code, "message": "The passport could not be built."}, + "diagnostics": [d.to_dict() for d in built.diagnostics]}, 1) + document = built.passport.to_dict() + written = None + if out: + # The raw text is checked as well as its absolute form: on POSIX abspath would turn a backslash UNC + # string into a harmless-looking relative name, so the guard must see what the user typed. + if localpath._reaches_network(out): + _error("bad_output", "Studio writes local files only.") + if out.endswith(("/", os.sep)) or os.path.isdir(out): + _error("bad_output", "The output must be a file name, not a folder.") + target = os.path.abspath(out) + if any(os.path.normcase(target) == os.path.normcase(os.path.abspath(p)) for _r, p in chain): + _error("output_is_input", "The output path is one of the input files.") + if os.path.exists(target): + _error("output_exists", "The output file already exists.") + try: + publish_new_file(target, json.dumps(document, indent=2).encode("utf-8")) + except FileExistsError: + _error("output_exists", "The output file already exists.") + except UnsafeOutput: + _error("bad_output", "The output directory could not be verified as a local folder.") + except OSError: + _error("write_failed", "The passport could not be written.") + written = os.path.basename(target) + _emit({"ok": True, "passport_id": document["passport_id"], "written": written, "passport": document, + "diagnostics": [d.to_dict() for d in built.diagnostics], "losses": document["losses"]}, 0) + + +def _load_passport(path: str) -> Any: + return _read_json(path, MAX_PASSPORT_BYTES, "passport") + + +@passport.command("import") +@click.argument("file", type=_FILE) +@click.option("--base-dir", type=_DIR, default=None) +@click.option("--path", "paths", multiple=True, help="NAME=PATH of a local copy of an artifact") +@_handled +def import_cmd(file, base_dir, paths): + result = import_passport(_load_passport(file), base_dir=base_dir, paths=_locations(paths)) + ok = result["passport"] is not None + _emit({"ok": ok, **result}, 0 if ok else 1) + + +@passport.command("verify") +@click.argument("file", type=_FILE) +@click.option("--base-dir", type=_DIR, default=None) +@click.option("--path", "paths", multiple=True, help="NAME=PATH of a local copy of an artifact") +@_handled +def verify_cmd(file, base_dir, paths): + decoded = decode_passport(_load_passport(file)) + if decoded.value is None: + _emit({"ok": False, "error": {"code": "unusable_passport", "message": "The passport could not be read."}, + "diagnostics": [d.to_dict() for d in decoded.diagnostics]}, 1) + report = verify_passport(decoded.value, base_dir=base_dir, paths=_locations(paths)) + ok = report["summary"]["all_intact"] + _emit({"ok": ok, "verification": report, "diagnostics": [d.to_dict() for d in decoded.diagnostics]}, + 0 if ok else 1) diff --git a/backend/snapstudio_core/safe_publish.py b/backend/snapstudio_core/safe_publish.py new file mode 100644 index 00000000..9b72d711 --- /dev/null +++ b/backend/snapstudio_core/safe_publish.py @@ -0,0 +1,226 @@ +"""Publish a new local file without a validate-then-write race on the output directory. + +`publish_new_file(target, data)` writes `data` to `target` only if `target` does not exist, and +never replaces anything. The directory that will hold the file is opened ONCE ("pinned") and every +later operation goes through that pin, so a later swap of the directory, or of any ancestor, cannot +redirect the write: + +* POSIX: the directory is held as a file descriptor and the temporary file, the no-replace link and + the cleanup are all `dir_fd`-relative. The pinned directory's real path is checked for + network/device locations after the pin, from the descriptor (`/proc/self/fd`, or `F_GETPATH` on macOS). +* Windows: the directory is opened with a share mode that omits FILE_SHARE_DELETE, so the directory + and its ancestors cannot be renamed, replaced or removed while the handle is held. The final path + is read from the handle and checked for network/device locations. A junction cannot be renamed in, but + it can be set in place on an empty directory, so the final name is created exclusively with sharing + off, and the file HANDLE's own final path must equal the verified directory plus the name; a mismatch + discards the empty file before any content is written. + +Remaining limits (stated, not hidden): this is not a sandbox. Anyone who can already write inside the +pinned directory can still create or remove entries there, and a platform that can neither report the +pinned directory's path nor pin it is refused (`UnsafeOutput`) rather than written best-effort. +""" +from __future__ import annotations + +import os +import secrets +import sys + +from snapstudio_core import localpath + + +class UnsafeOutput(Exception): + """The output location could not be pinned and verified as a local directory.""" + + +def _after_pin() -> None: + """Seam for tests: runs after the directory is pinned and verified, before any file is created.""" + + +def _before_create() -> None: + """Seam for tests (Windows): runs after the pinned directory's reparse re-check, immediately before the create.""" + + +def _check_name(name: str) -> None: + separators = ("/", "\\") if sys.platform == "win32" else ("/",) + if not name or name in (".", "..") or "\x00" in name or any(sep in name for sep in separators): + raise UnsafeOutput("the output must be a plain file name") + + +def _tmp_name() -> str: + """A bounded temporary name that does not depend on the destination name (NAME_MAX safe).""" + return f".snapstudio-{secrets.token_hex(8)}.tmp" + + +def publish_new_file(target: str, data: bytes, mode: int = 0o600) -> None: + """Create `target` with `data`; FileExistsError if it exists; UnsafeOutput if not pinnable/local.""" + absolute = os.path.abspath(target) + parent, name = os.path.split(absolute) + _check_name(name) + if sys.platform == "win32": + _publish_windows(parent, name, data) + else: + _publish_posix(parent, name, data, mode) + + +# ---------------------------------------------------------------- POSIX + +def _pinned_path_posix(dfd: int) -> str: + if sys.platform.startswith("linux"): + try: + resolved = os.readlink(f"/proc/self/fd/{dfd}") # fails closed when /proc is unavailable + except OSError: + raise UnsafeOutput("this system cannot report the output directory's real path") from None + if not os.path.isabs(resolved) or resolved.endswith(" (deleted)"): + raise UnsafeOutput("the output directory's real path could not be verified") + return resolved + try: + import fcntl + raw = fcntl.fcntl(dfd, getattr(fcntl, "F_GETPATH"), b"\0" * 1024) + return os.fsdecode(raw.split(b"\0", 1)[0]) + except (ImportError, AttributeError, OSError): + raise UnsafeOutput("this platform cannot report the output directory's real path") from None + + +def _publish_posix(parent: str, name: str, data: bytes, mode: int) -> None: + flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_CLOEXEC", 0) + try: + dfd = os.open(parent, flags) + except OSError as error: + raise UnsafeOutput("the output directory could not be opened") from error + tmp = None + try: + real = _pinned_path_posix(dfd) + if not real or localpath._reaches_network(real): + raise UnsafeOutput("the output directory is not a local directory") + _after_pin() + tmp = _tmp_name() + fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0), mode, dir_fd=dfd) + with os.fdopen(fd, "wb") as handle: + handle.write(data) + os.link(tmp, name, src_dir_fd=dfd, dst_dir_fd=dfd, follow_symlinks=False) # refuses an existing name + finally: + if tmp is not None: + try: + os.unlink(tmp, dir_fd=dfd) + except OSError: + pass + os.close(dfd) + + +# ---------------------------------------------------------------- Windows + +def _publish_windows(parent: str, name: str, data: bytes) -> None: + """Pin the directory, then create the FINAL name exclusively and verify where it really landed. + + Holding the directory handle (no FILE_SHARE_DELETE) stops a rename or delete of the directory and its + ancestors, but an actor with write access to an EMPTY directory can still turn it into a junction in + place. So the file handle itself is checked: after the exclusive create (CREATE_NEW, share mode none, so + nobody can read a partial file) its final path must sit directly inside the pinned directory's path; + otherwise the just-created empty file is deleted through the handle and nothing is written. The residual + exposure is an empty, immediately removed file in the redirected location; no passport content is written. + """ + import ctypes + from ctypes import wintypes + + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) + kernel32.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID, + wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE] + kernel32.CreateFileW.restype = wintypes.HANDLE + kernel32.GetFinalPathNameByHandleW.argtypes = [wintypes.HANDLE, wintypes.LPWSTR, wintypes.DWORD, wintypes.DWORD] + kernel32.GetFinalPathNameByHandleW.restype = wintypes.DWORD + kernel32.GetFileInformationByHandle.argtypes = [wintypes.HANDLE, wintypes.LPVOID] + kernel32.GetFileInformationByHandle.restype = wintypes.BOOL + kernel32.SetFileInformationByHandle.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD] + kernel32.SetFileInformationByHandle.restype = wintypes.BOOL + kernel32.WriteFile.argtypes = [wintypes.HANDLE, wintypes.LPCVOID, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD), + wintypes.LPVOID] + kernel32.WriteFile.restype = wintypes.BOOL + kernel32.CloseHandle.argtypes = [wintypes.HANDLE] + + FILE_LIST_DIRECTORY = 0x0001 + GENERIC_WRITE = 0x40000000 + DELETE = 0x00010000 + SHARE_READ_WRITE = 0x1 | 0x2 # deliberately NOT FILE_SHARE_DELETE (0x4) + OPEN_EXISTING, CREATE_NEW = 3, 1 + FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 + FILE_ATTRIBUTE_NORMAL = 0x80 + FILE_ATTRIBUTE_REPARSE_POINT = 0x400 + FILE_DISPOSITION_INFO = 4 + ERROR_FILE_EXISTS, ERROR_ALREADY_EXISTS = 80, 183 + invalid = wintypes.HANDLE(-1).value + + class FILETIME(ctypes.Structure): + _fields_ = [("lo", wintypes.DWORD), ("hi", wintypes.DWORD)] + + class BY_HANDLE_FILE_INFORMATION(ctypes.Structure): + _fields_ = [("attributes", wintypes.DWORD), ("created", FILETIME), ("accessed", FILETIME), + ("written", FILETIME), ("volume", wintypes.DWORD), ("size_hi", wintypes.DWORD), + ("size_lo", wintypes.DWORD), ("links", wintypes.DWORD), ("index_hi", wintypes.DWORD), + ("index_lo", wintypes.DWORD)] + + def final_path(h) -> str: + buffer = ctypes.create_unicode_buffer(32768) + length = kernel32.GetFinalPathNameByHandleW(h, buffer, len(buffer), 0) + if not length or length >= len(buffer): + raise UnsafeOutput("a real path could not be read") + text = buffer.value + if text.upper().startswith("\\\\?\\UNC\\"): + raise UnsafeOutput("the output location is not local") + if text.startswith("\\\\?\\") and len(text) > 6 and text[5] == ":": + text = text[4:] # strip the extended-length prefix from a local drive path + return text + + def is_reparse(h) -> bool: + info = BY_HANDLE_FILE_INFORMATION() + if not kernel32.GetFileInformationByHandle(h, ctypes.byref(info)): + raise UnsafeOutput("the output directory could not be inspected") + return bool(info.attributes & FILE_ATTRIBUTE_REPARSE_POINT) + + directory = kernel32.CreateFileW(parent, FILE_LIST_DIRECTORY, SHARE_READ_WRITE, None, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, None) + if directory in (None, invalid): + raise UnsafeOutput("the output directory could not be opened") + out = None + try: + real = final_path(directory) + if localpath._reaches_network(real): + raise UnsafeOutput("the output directory is not a local directory") + _after_pin() + if is_reparse(directory): # turned into a link in place after the pin + raise UnsafeOutput("the output directory was redirected") + destination = os.path.join(real, name) + _before_create() + out = kernel32.CreateFileW(destination, GENERIC_WRITE | DELETE, 0, None, CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, None) + if out in (None, invalid): + if ctypes.get_last_error() in (ERROR_FILE_EXISTS, ERROR_ALREADY_EXISTS): + raise FileExistsError(destination) + raise OSError(ctypes.get_last_error(), "the passport could not be created") + + def discard(reason): + flag = wintypes.DWORD(1) + if not kernel32.SetFileInformationByHandle(out, FILE_DISPOSITION_INFO, ctypes.byref(flag), + ctypes.sizeof(flag)): + raise UnsafeOutput("the output could not be verified and the file it created could not be " + "removed") from reason + + try: + landed = final_path(out) + # Both strings are canonical on-disk spellings (GetFinalPathNameByHandle), so compare them exactly: + # case-folding would accept a junction to a distinct directory on a case-sensitive volume. + if os.path.normpath(landed) != os.path.normpath(destination) or localpath._reaches_network(landed): + raise UnsafeOutput("the output was redirected away from the verified directory") + view = memoryview(data) + while len(view): + written = wintypes.DWORD(0) + chunk = bytes(view[: 1 << 20]) + if not kernel32.WriteFile(out, chunk, len(chunk), ctypes.byref(written), None): + raise OSError(ctypes.get_last_error(), "the passport could not be written") + view = view[written.value:] + except BaseException as error: + discard(error) + raise + finally: + if out not in (None, invalid): + kernel32.CloseHandle(out) + kernel32.CloseHandle(directory) diff --git a/backend/snapstudio_core/variants/__init__.py b/backend/snapstudio_core/variants/__init__.py new file mode 100644 index 00000000..87162858 --- /dev/null +++ b/backend/snapstudio_core/variants/__init__.py @@ -0,0 +1,5 @@ +"""Advisory comparison of two user-selected sliced outputs.""" + +from .comparison import compare_outputs + +__all__ = ["compare_outputs"] diff --git a/backend/snapstudio_core/variants/comparison.py b/backend/snapstudio_core/variants/comparison.py new file mode 100644 index 00000000..f0261e26 --- /dev/null +++ b/backend/snapstudio_core/variants/comparison.py @@ -0,0 +1,291 @@ +"""Bounded, local-only comparison of sliced 3MF and G-code outputs.""" +from __future__ import annotations + +import json +import lzma +import math +import os +import re +import zipfile +import zlib +from snapstudio_core import localpath +from pathlib import Path + +from snapstudio_core import paths, preserve +from snapstudio_core.foundation import artifacts, capabilities, topology + +MAX_GCODE_SCAN = 8 * 1024 * 1024 +MAX_3MF_ENTRY = 2 * 1024 * 1024 +MAX_ZIP_ENTRIES = 2048 +TOOL_RE = re.compile(r"^\s*T(\d+)\s*(?:;.*)?$", re.IGNORECASE) +GCODE_WEIGHT_RE = re.compile(r"^;\s*filament used \[g\]\s*=\s*(.*)$", re.IGNORECASE) +GCODE_TIME_RE = re.compile(r"^;\s*estimated printing time(?: \([^)]*\))?\s*=\s*(.+)$", re.IGNORECASE) +GCODE_TOWER_RE = re.compile(r"^;\s*(?:total )?(?:wipe tower|purge tower|flush tower)\s*(?:used|volume)?\s*=\s*(.+)$", re.IGNORECASE) + + +def _parse_number_list(raw: str) -> list[float] | None: + try: + values = [float(v.strip()) for v in raw.split(",")] + except ValueError: + return None + return values if values and all(v >= 0 and v < float("inf") for v in values) else None + + +def _parse_time(raw: str) -> int | None: + total = 0 + found = False + for number, unit in re.findall(r"(\d+)\s*(d|h|m|s)", raw.lower()): + total += int(number) * {"d": 86400, "h": 3600, "m": 60, "s": 1}[unit] + found = True + return total if found else None + + +def _safe_path(path: object) -> Path: + if not isinstance(path, str) or not path.strip() or len(path) > 4096: + raise ValueError("Each output needs a local file path.") + if not localpath.is_local_regular_file(str(Path(path).expanduser())): + raise ValueError("A selected output could not be read as a local file.") + candidate = Path(path).expanduser() + if not candidate.is_file(): + raise ValueError("A selected output could not be read as a file.") + if candidate.suffix.lower() not in (".3mf", ".gcode", ".g", ".gco"): + raise ValueError("Choose a sliced 3MF or G-code output.") + return candidate + + +def _read_gcode(path: Path) -> dict: + size = path.stat().st_size + with path.open("rb") as handle: + raw = handle.read(MAX_GCODE_SCAN + 1) + truncated = size > MAX_GCODE_SCAN or len(raw) > MAX_GCODE_SCAN + text = raw[:MAX_GCODE_SCAN].decode("utf-8", errors="replace") + parsed: dict = {"settings": {}, "reported_tool_indices": None, "grams_by_tool": None, + "time_seconds": None, "tower": None} + body_started = False + active: int | None = None + used: set[int] = set() + changes = 0 + for line in text.splitlines(): + stripped = line.strip() + if not body_started and stripped.startswith(";"): + match = GCODE_WEIGHT_RE.match(stripped) + if match: + grams = _parse_number_list(match.group(1)) + if grams is not None: + parsed["grams_by_tool"] = grams + parsed["reported_tool_indices"] = [i for i, amount in enumerate(grams) if amount > 0] + match = GCODE_TIME_RE.match(stripped) + if match: + parsed["time_seconds"] = _parse_time(match.group(1)) + match = GCODE_TOWER_RE.match(stripped) + if match: + parsed["tower"] = _parse_number_list(match.group(1)) + parsed["tower"] = parsed["tower"][0] if parsed["tower"] and len(parsed["tower"]) == 1 else None + if "=" in stripped: + key, value = stripped[1:].split("=", 1) + key = key.strip() + if key in {"layer_height", "wall_loops", "sparse_infill_density", "support_type", + "brim_type", "nozzle_diameter", "temperature", "bed_temperature", + "print_speed", "filament_type", "filament_colour"}: + parsed["settings"][key] = value.strip()[:300] + if "EXECUTABLE_BLOCK_START" in stripped: + body_started = True + continue + if stripped.startswith(";") or not stripped: + continue + body_started = True + match = TOOL_RE.match(stripped) + if match: + tool = int(match.group(1)) + used.add(tool) + if active is not None and active != tool: + changes += 1 + active = tool + parsed["scan"] = {"truncated": truncated, "tools_used": sorted(used) if used and not truncated else None, + "tool_changes": changes if not truncated else None} + if parsed["reported_tool_indices"] is None and used: + parsed["reported_tool_indices"] = sorted(used) + return parsed + + +def _finite_amount(value: object) -> bool: + """A non-negative, finite number that converts to float. Overflowing integers and infinities are not amounts.""" + if isinstance(value, bool) or not isinstance(value, (int, float)): + return False + try: + number = float(value) + except OverflowError: + return False + return math.isfinite(number) and number >= 0 + + +def _read_3mf(path: Path) -> dict: + try: + return _read_3mf_archive(path) + except (zipfile.BadZipFile, NotImplementedError, RuntimeError, zlib.error, EOFError, lzma.LZMAError, OSError): + raise ValueError("A selected 3MF could not be read as a valid archive.") from None + + +def _read_3mf_archive(path: Path) -> dict: + settings: dict = {} + slice_info: dict = {} + with zipfile.ZipFile(path) as archive: + infos = archive.infolist() + if len(infos) > MAX_ZIP_ENTRIES: + raise ValueError("This 3MF contains too many entries to inspect safely.") + for name, target in (("Metadata/project_settings.config", settings), + ("Metadata/slice_info.config", slice_info)): + try: + info = archive.getinfo(name) + except KeyError: + continue + if info.file_size > MAX_3MF_ENTRY: + continue + raw = archive.read(info) # a corrupt or encrypted member is an archive error: the caller maps it to 400 + try: + value = json.loads(raw.decode("utf-8")) + except (ValueError, RecursionError): + continue # readable bytes that are not JSON: the metadata stays unknown + if isinstance(value, dict): + target.update(value) + parsed = {"settings": settings, "reported_tool_indices": None, "grams_by_tool": None, + "time_seconds": None, "tower": None, + "scan": {"truncated": False, "tools_used": None, "tool_changes": None}} + for key in ("filament_used_g", "filament_used_g_by_tool"): + values = slice_info.get(key) + if isinstance(values, list) and all(_finite_amount(v) for v in values): + parsed["grams_by_tool"] = [float(v) for v in values] + parsed["reported_tool_indices"] = [i for i, amount in enumerate(values) if amount > 0] + break + seconds = slice_info.get("estimated_time_s", slice_info.get("time_seconds")) + if _finite_amount(seconds): + parsed["time_seconds"] = float(seconds) + tools = slice_info.get("tools_used") + if isinstance(tools, list) and all(isinstance(v, int) and not isinstance(v, bool) and 0 <= v <= 31 for v in tools): + parsed["scan"]["tools_used"] = sorted(set(tools)) + return parsed + + +def _load(path: Path, snapshots: str) -> dict: + parsed = _read_3mf(path) if path.suffix.lower() == ".3mf" else _read_gcode(path) + snap = artifacts.snapshot(str(path), "sliced", snapshot_dir=snapshots, data_dir=paths.data_dir()) + if snap.value is None: + raise ValueError("Studio could not create a byte identity for a selected output.") + copy_path = artifacts.snapshot_copy_path(snap.value, snapshots) + if copy_path: + parsed = _read_3mf(Path(copy_path)) if path.suffix.lower() == ".3mf" else _read_gcode(Path(copy_path)) + ref = snap.value.to_dict() + ref["locator"] = None + current = artifacts.same_bytes(snap.value, str(path)) == artifacts.SAME + return {"ref": snap.value, "public_ref": ref, "parsed": parsed, "path": str(path), + "status": "current" if current else "stale"} + + +def _grams_by_source(entry: dict) -> list[dict]: + grams = entry["parsed"].get("grams_by_tool") + if not isinstance(grams, list): + return [] + bindings = entry.get("physical_bindings") + result = [] + for index, amount in enumerate(grams): + if amount <= 0: + continue + binding = bindings.get(str(index)) if isinstance(bindings, dict) else None + source = _verified_physical_source(binding, index) + result.append({"logical_tool": topology.denormalise_index("orca_filament", index), "grams": amount, + "physical_source": source, + "basis": "physical source" if source else "logical tool (physical unknown)"}) + return result + + +def _verified_physical_source(binding: object, index: int) -> str | None: + """Resolve only a validated direct slot-to-head topology binding.""" + if not isinstance(binding, dict) or not isinstance(binding.get("topology"), dict): + return None + slot_id = topology.slot_id(index) + decoded = topology.Topology.from_dict(binding["topology"]) + if decoded.value is None or any(d.severity == "error" for d in decoded.diagnostics): + return None + value = decoded.value + if not value.complete or any(d.severity == "error" for d in topology.validate(value)): + return None + targets = {b.target for b in value.bindings if b.slot_id == slot_id and b.target is not None} + slot = next((item for item in value.slots if item.id == slot_id), None) + if slot is not None and slot.binding is not None: + targets.add(slot.binding) + if len(targets) != 1: + return None + target = next(iter(targets)) + if not target.startswith("head:"): + return None + head = next((item for item in value.heads if item.id == target), None) + return f"head {head.index + 1}" if head is not None else None + + +def _requirements(raw: object) -> list: + if raw is None: + return [] + if not isinstance(raw, list) or len(raw) > 64: + raise ValueError("Requirements must be a list of at most 64 entries.") + result = [] + for item in raw: + if not isinstance(item, dict) or not isinstance(item.get("key"), str) or not item["key"]: + raise ValueError("Each requirement needs a capability key.") + params = item.get("params", {}) + if not isinstance(params, dict): + raise ValueError("Requirement parameters must be an object.") + result.append(capabilities.Requirement(item["key"], params, source="variant comparison")) + return result + + +def compare_outputs(data: dict) -> dict: + """Compare two selected files; computed values remain advisory evidence.""" + outputs = data.get("outputs") + if not isinstance(outputs, list) or len(outputs) != 2 or any(not isinstance(row, dict) for row in outputs): + raise ValueError("Choose exactly two sliced outputs to compare.") + snapshots = os.path.join(paths.data_dir(), "variants", "snapshots") + left, right = (_load(_safe_path(row.get("path")), snapshots) for row in outputs) + for entry, raw in zip((left, right), outputs): + entry["physical_bindings"] = raw.get("physical_bindings") + stale = left["status"] == "stale" or right["status"] == "stale" + tools_a, tools_b = left["parsed"]["scan"]["tools_used"], right["parsed"]["scan"]["tools_used"] + tools_known = isinstance(tools_a, list) and isinstance(tools_b, list) + tools_union = sorted(set(tools_a or []) | set(tools_b or [])) if tools_known else None + requirements: list[dict] = [] + raw_reqs = data.get("requirements") + if raw_reqs is not None: + reqs = _requirements(raw_reqs) + capability_sets = data.get("capability_sets") + if capability_sets is None: + capability_sets = [data.get("capability_set"), data.get("capability_set")] + decoded = [capabilities.CapabilitySet.from_dict(raw) for raw in capability_sets] \ + if isinstance(capability_sets, list) and len(capability_sets) == 2 else [] + if len(decoded) != 2 or any(item.value is None for item in decoded): + requirements = [{"key": req.key, "result": "unknown", "reason": "Capability evidence was not usable."} + for req in reqs] + elif decoded[0].value.to_dict() != decoded[1].value.to_dict(): + requirements = [{"key": req.key, "result": "unknown", "reason": "The capability evidence basis changed between outputs."} + for req in reqs] + else: + requirements = capabilities.evaluation_to_dict(capabilities.evaluate(reqs, decoded[0].value)) + + def present(entry: dict) -> dict: + parsed = entry["parsed"] + scan = parsed["scan"] + value = scan["tool_changes"] + seconds = parsed.get("time_seconds") + return { + "artifact": entry["public_ref"], "freshness": entry["status"], + "format": Path(entry["path"]).suffix.lower().lstrip("."), + "tools_used": {"value": scan["tools_used"], "status": "computed" if scan["tools_used"] is not None else "unknown"}, + "tool_changes": {"value": value, "status": "computed" if value is not None else "unknown"}, + "grams_by_tool": _grams_by_source(entry), + "time": {"value": seconds, "unit": "seconds", "status": "reported" if seconds is not None else "unknown"}, + "waste": {"value": parsed.get("tower") if scan["tools_used"] is not None else None, + "status": "reported" if parsed.get("tower") is not None and scan["tools_used"] is not None else "unknown"}, + } + settings_diff = preserve.config_diff(left["parsed"].get("settings", {}), right["parsed"].get("settings", {})) + return {"outputs": [present(left), present(right)], "stale": stale, + "tools_union": {"value": tools_union, "status": "computed" if tools_known else "unknown"}, + "settings_diff": settings_diff, "requirements": requirements, + "advisory": "Comparison is advisory. Computed tool changes come from a bounded G-code scan; unknown means Studio did not verify the value."} diff --git a/backend/snapstudio_core/workspace_recovery/__init__.py b/backend/snapstudio_core/workspace_recovery/__init__.py new file mode 100644 index 00000000..9dd32302 --- /dev/null +++ b/backend/snapstudio_core/workspace_recovery/__init__.py @@ -0,0 +1,160 @@ +"""Checkpoint and restore local workspace context. + +The checkpoint is one revision-checked `recovery.json` written through `foundation.store`. Restore +never replays work and never trusts what it reads: observations come back historical, files are +re-hashed against their recorded identity, jobs that were in flight come back `interrupted`. +The old desktop job record (`store/job.ts`) is imported one way into this document and never +written back, so the two restore mechanisms cannot overwrite each other. +""" + +from __future__ import annotations + +import hashlib +import os +from snapstudio_core import localpath +import re +from copy import deepcopy +from typing import Any + +from snapstudio_core.foundation import artifacts, observations, store + +STORE_NAME = "recovery" +SCHEMA = "recovery" +VERSION = 1 +_SECRET_KEY = re.compile(r"api[_-]?key|token|authorization|password|secret", re.IGNORECASE) +_MAX_DEPTH = 8 +_ROLES = ("original", "prepared", "sliced") +MAX_FINGERPRINT_BYTES = 512 * 1024 * 1024 # larger files are tracked by name only + + +def _scrub(value: Any, depth: int = 0) -> Any: + """Copy `value` without any field that looks like a credential; bounded depth.""" + if depth > _MAX_DEPTH: + return None + if isinstance(value, dict): + return {k: _scrub(v, depth + 1) for k, v in value.items() + if isinstance(k, str) and not _SECRET_KEY.search(k)} + if isinstance(value, list): + return [_scrub(v, depth + 1) for v in value] + return value + + +def _fingerprint(item: dict) -> dict: + """Track a local file by path: record its bytes identity now, read-only. Unreadable stays unverified.""" + path = item.get("path") + name = item.get("name") if isinstance(item.get("name"), str) else None + if not isinstance(path, str) or not path: + return {"name": name or "Unknown file", "path": None} + entry = {"name": name or os.path.basename(path) or "Unknown file", "path": path} + if isinstance(item.get("artifact"), dict): + entry["artifact"] = item["artifact"] + return entry + if not localpath.is_local_regular_file(path, max_bytes=MAX_FINGERPRINT_BYTES): + return entry # network/device/non-regular/oversize: never opened + try: + if os.path.getsize(path) > MAX_FINGERPRINT_BYTES: + return entry + digest, size = hashlib.sha256(), 0 + with open(path, "rb") as handle: + for chunk in iter(lambda: handle.read(1024 * 1024), b""): + digest.update(chunk) + size += len(chunk) + entry["artifact"] = artifacts.ArtifactRef.create(digest.hexdigest(), size, "original", + entry["name"]).to_dict() + except OSError: + pass + return entry + + +def _import_legacy(record: Any) -> dict | None: + """Translate an old `job/1` record into the recovery shape. Association stays unverified.""" + if not isinstance(record, dict) or not isinstance(record.get("original"), dict): + return None + return {"schema": "legacy-job-import/1", "saved_at": record.get("savedAt"), + "files": [{"role": role, "name": item.get("name", "Unknown file"), + "path": item.get("path"), "fingerprint": _scrub(item.get("fingerprint")), + "association": "unverified"} + for role in _ROLES if isinstance((item := record.get(role)), dict)]} + + +def _legacy_job(data_dir: str | None) -> dict | None: + """Read the old job store once, without renaming or writing anything.""" + legacy = store.read_json("job", schema="job", version=1, data_dir=data_dir, rename_damaged=False) + doc = legacy.value + if not isinstance(doc, dict) or doc.get("missing"): + return None + return _import_legacy(doc.get("record", doc)) + + +def _read_recovery(data_dir: str | None): + current = store.read_json(STORE_NAME, schema=SCHEMA, version=VERSION, data_dir=data_dir) + if current.value is None: + if any(d.code == "newer_schema_version" for d in current.diagnostics): + raise store.StoreVersionError("recovery document was written by a newer version") + raise store.StoreError("recovery document is unreadable") + return current + + +def checkpoint(payload: dict, *, data_dir: str | None = None, + lock_wait_s: float = store.LOCK_WAIT_S, expected_revision: int | None = None) -> dict: + """Revision-checked checkpoint; paths stay local and the UI exposes names only. + + A caller that holds the revision it last saw passes it as expected_revision; a mismatch is refused. + Without one, the check only covers the read-then-write window inside this call (last writer wins).""" + current = _read_recovery(data_dir) + if expected_revision is not None and expected_revision != current.value["revision"]: + raise store.RevisionConflict("checkpoint revision is stale") + jobs = [item for item in payload.get("jobs", []) if isinstance(item, dict)] + legacy = next((_import_legacy(item["legacy_record"]) for item in jobs + if isinstance(item.get("legacy_record"), dict)), None) + imported = legacy or _legacy_job(data_dir) + job_records = [item for item in jobs if isinstance(item.get("job_id"), str)] + body = {"workspace": _scrub(payload.get("workspace", {})), + "observations": _scrub(deepcopy(payload.get("observations", []))), + "files": [_fingerprint(f) for f in _scrub(payload.get("files", [])) if isinstance(f, dict)], + "jobs": _scrub(job_records), + "legacy_job": imported, "checkpointed_at": payload.get("checkpointed_at")} + revision = store.write_json(STORE_NAME, body, expected_revision=current.value["revision"], + schema=SCHEMA, version=VERSION, data_dir=data_dir, + lock_wait_s=lock_wait_s) + return {"revision": revision, "checkpointed_at": body["checkpointed_at"], + "workspace": body["workspace"], "file_count": len(body["files"]), + "observation_count": len(body["observations"]), "job_count": len(body["jobs"])} + + +def restore(*, data_dir: str | None = None) -> dict: + """Preview of what a checkpoint holds. Read-only: nothing is applied or replayed.""" + current = _read_recovery(data_dir) + if current.value.get("missing"): + return {"missing_checkpoint": True, "revision": 0, "workspace": {}, "observations": [], + "files": [], "jobs": [], "legacy_job": _legacy_job(data_dir), + "fresh_read_required": True} + doc = deepcopy(current.value) + docs = [] + for raw in doc.get("observations", []): + decoded = observations.Observation.from_dict(raw) + if decoded.value is None: + continue + historical = observations.as_historical(decoded.value) + docs.append({**historical.to_dict(), "usable_for_loadout": False}) + files = [] + for item in doc.get("files", []): + if not isinstance(item, dict): + continue + ref_decoded = artifacts.ArtifactRef.from_dict(item.get("artifact")) + state = ("unverified" if ref_decoded.value is None + else artifacts.same_bytes(ref_decoded.value, localpath.usable_path(item.get("path")))) + files.append({"name": item.get("name", "Unknown file"), "state": state, + "association": "unverified"}) + jobs = [] + for job in doc.get("jobs", []): + if isinstance(job, dict): + state = job.get("state") + interrupted = state in ("queued", "running", "cancel_requested") + jobs.append({"job_id": job.get("job_id"), + "state": "interrupted" if interrupted else state, "replayed": False}) + return {"missing_checkpoint": False, "revision": doc.get("revision", 0), + "checkpointed_at": doc.get("checkpointed_at"), "workspace": doc.get("workspace", {}), + "observations": docs, "files": files, "jobs": jobs, + "legacy_job": doc.get("legacy_job") or _legacy_job(data_dir), + "fresh_read_required": True} diff --git a/backend/tests/assemblies/__init__.py b/backend/tests/assemblies/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/tests/assemblies/test_assemblies.py b/backend/tests/assemblies/test_assemblies.py new file mode 100644 index 00000000..fc9a8952 --- /dev/null +++ b/backend/tests/assemblies/test_assemblies.py @@ -0,0 +1,475 @@ +"""P5 assemblies: PLAN_V43 section 6 P5 tests 1-10, common C3/C7 and guard G1/G2-style checks. + +Fixture-tested: every input here is synthetic (see tests/fixtures/assemblies/PROVENANCE.md). +""" +from __future__ import annotations + +import copy +import hashlib +import json +import re +import sqlite3 +from pathlib import Path + +import pytest + +from snapstudio_api import ext_routes, routes_assemblies +from snapstudio_core import paths +from snapstudio_core.assemblies import evaluate as ev +from snapstudio_core.assemblies import store +from snapstudio_core.assemblies.model import (AssemblyInputError, read_assembly, usage_from_gcode_facts, + usage_from_slot_usage) +from snapstudio_core.foundation.store import RevisionConflict + +HERE = Path(__file__).parent +FIXTURES = HERE.parent / "fixtures" / "assemblies" +NOW = 1_800_000_000.0 + +ROAD = {"provider_instance_id": "synthetic", "provider_spool_id": "road-1"} +WATER = {"provider_instance_id": "synthetic", "provider_spool_id": "water-1"} + + +def fixture(name: str): + return json.loads((FIXTURES / name).read_text(encoding="utf-8")) + + +def six_tile() -> dict: + return fixture("six_tile.json") + + +def reading(spool, grams, age=10, ttl=600, **extra): + return {"spool": spool, "remaining_g": grams, "observed_at": NOW - age, "ttl_s": ttl, **extra} + + +def run(doc, supply, lookup=None): + asm = read_assembly(doc) + return ev.evaluate(asm, ev.read_supply(supply, NOW), now=NOW, lookup=lookup) + + +def spool_row(report, spool): + return next(s for s in report["spools"] if s["spool"] == spool) + + +# -- 1. six tiles share roles across plates --------------------------------------- +def test_1_six_tiles_share_road_and_water_roles_across_plates(): + report = run(six_tile(), [reading(ROAD, 500), reading(WATER, 500)]) + assert len(report["tiles"]) == 6 + roles = {r["role"]: r for r in report["roles"]} + assert set(roles) == {"road", "water"} + for role in roles.values(): + assert len(role["tiles"]) == 6 + assert role["plates"] == ["1", "2", "3"] + assert role["shared_across_plates"] is True + # The same role sits on different slots on different plates; the spool is still counted once. + assert spool_row(report, ROAD)["need_known_g"] == 240.0 + assert spool_row(report, WATER)["need_known_g"] == 150.0 + + +# -- 2. cumulative grams, 1-based and 0-based sources, never double counted -------- +def test_2_slot_usage_is_one_based_and_gcode_tools_are_zero_based_and_both_land_on_the_same_slot(): + from_project = usage_from_slot_usage(fixture("slot_usage_tile.json")) + from_gcode = usage_from_gcode_facts(fixture("gcode_facts_tile.json")) + assert from_project == from_gcode == {"slot:0": 12.5, "slot:1": None, "slot:2": 4.0} + + +def test_2_one_spool_behind_two_roles_or_two_slots_is_counted_once(): + doc = six_tile() + doc["bindings"]["water"] = dict(ROAD) # both roles on one physical spool + report = run(doc, [reading(ROAD, 1000)]) + assert len(report["spools"]) == 1 + assert report["spools"][0]["need_known_g"] == 390.0 + assert sorted(report["spools"][0]["roles"]) == ["road", "water"] + per_tile = {t["tile_id"]: t["grams"] for t in report["spools"][0]["tiles"]} + assert per_tile == {f"t{i}": 65.0 for i in range(1, 7)} + + +def test_2_an_alias_merges_two_providers_spools_and_no_alias_keeps_them_apart(): + doc = six_tile() + doc["bindings"]["water"] = {"provider_instance_id": "other", "provider_spool_id": "road-1"} + assert len(run(doc, [])["spools"]) == 2 + doc["aliases"] = [{"refs": [ROAD, doc["bindings"]["water"]], "evidence": "user label", "declared_by": "user"}] + merged = run(doc, [reading(ROAD, 1000)]) + assert len(merged["spools"]) == 1 and merged["spools"][0]["need_known_g"] == 390.0 + + +def test_2_slot_usage_and_gcode_facts_feed_a_tile_through_the_document(): + doc = six_tile() + doc["tiles"][0].pop("usage") + doc["tiles"][0]["slot_usage"] = fixture("slot_usage_tile.json") + doc["tiles"][1].pop("usage") + doc["tiles"][1]["gcode_facts"] = fixture("gcode_facts_tile.json") + asm = read_assembly(doc) + assert asm.tiles[0].usage == asm.tiles[1].usage + assert (asm.tiles[0].usage_source, asm.tiles[1].usage_source) == ("slot_usage", "gcode_facts") + doc["tiles"][0]["usage"] = {"slot:0": 1} + with pytest.raises(AssemblyInputError): + read_assembly(doc) # two gram sources on one tile + + +# -- 3. a job without grams stays unknown ------------------------------------------ +def test_3_a_tile_without_grams_stays_unknown_and_is_not_counted_as_zero(): + doc = six_tile() + doc["tiles"][4].pop("usage") + doc["tiles"][5]["usage"] = {"slot:0": 40, "slot:1": None} + report = run(doc, [reading(ROAD, 1000), reading(WATER, 1000)]) + t5, t6 = report["tiles"][4], report["tiles"][5] + assert t5["need_complete"] is False and t5["unknown_slots"] == ["slot:0", "slot:1"] + assert t6["unknown_slots"] == ["slot:1"] and t6["need_known_g"] == 40.0 + assert report["verdict"] == "unknown" + assert spool_row(report, ROAD)["unknown_tiles"] == ["t5"] + assert spool_row(report, ROAD)["verdict"] == "unknown" + + +def test_3_an_unreadable_gcode_file_states_nothing(): + assert usage_from_gcode_facts({"available": False, "error": "that file does not exist"}) == {} + with pytest.raises(AssemblyInputError): + usage_from_gcode_facts({"available": True, "slots": [{"tool": -1, "grams": 3}]}) + + +@pytest.mark.parametrize("bad", [-1, float("nan"), float("inf"), "12", True, 1e12]) +def test_3_unusable_gram_figures_are_refused_not_coerced(bad): + doc = six_tile() + doc["tiles"][0]["usage"]["slot:0"] = bad + with pytest.raises(AssemblyInputError): + read_assembly(doc) + + +# -- 4. missing part listed; deleted library project is "missing" ------------------ +def test_4_a_part_with_no_tile_is_listed_as_missing(): + report = run(six_tile(), []) + assert [p["part_id"] for p in report["parts"]["missing"]] == ["pier"] + doc = six_tile() + doc["tiles"][0]["part_id"] = "ghost" + report = run(doc, []) + assert "quay" in [p["part_id"] for p in report["parts"]["missing"]] + assert report["parts"]["unlisted_tiles"] == ["t1"] + + +def test_4_library_cross_reference_by_project_id_and_sha256(): + sha = "1" * 64 + ok = run(six_tile(), [], lookup=lambda pid: {"sha256": sha})["tiles"][0]["library"]["status"] + gone = run(six_tile(), [], lookup=lambda pid: None)["tiles"][0]["library"]["status"] + changed = run(six_tile(), [], lookup=lambda pid: {"sha256": "2" * 64})["tiles"][0]["library"]["status"] + unread = run(six_tile(), [], lookup=lambda pid: {"sha256": None})["tiles"][0]["library"]["status"] + unchecked = run(six_tile(), [])["tiles"][0]["library"]["status"] + assert (ok, gone, changed, unread, unchecked) == ("linked", "missing", "changed", "unverified", "unverified") + assert run(six_tile(), [])["tiles"][1]["library"]["status"] == "unlinked" + + +def test_4_a_deleted_library_project_reads_missing_through_the_route(tmp_path): + real = tmp_path / "model.3mf" + real.write_bytes(b"synthetic bytes") + digest = hashlib.sha256(b"synthetic bytes").hexdigest() + db = Path(paths.data_dir()) / "library.db" + conn = sqlite3.connect(db) + conn.execute("CREATE TABLE projects (id INTEGER PRIMARY KEY, name TEXT, source_path TEXT)") + conn.execute("INSERT INTO projects VALUES (11, 'x', ?)", (str(real),)) + conn.commit() + conn.close() + doc = six_tile() + doc["tiles"][0]["library"]["sha256"] = digest + status, body = routes_assemblies.evaluate({"assembly": doc}) + assert status == 200 and body["report"]["tiles"][0]["library"]["status"] == "linked" + real.write_bytes(b"changed") + assert routes_assemblies.evaluate({"assembly": doc})[1]["report"]["tiles"][0]["library"]["status"] == "changed" + conn = sqlite3.connect(db) + conn.execute("DELETE FROM projects") + conn.commit() + conn.close() + assert routes_assemblies.evaluate({"assembly": doc})[1]["report"]["tiles"][0]["library"]["status"] == "missing" + assert real.read_bytes() == b"changed" # the model file itself is never touched + + +def test_4_no_library_database_is_never_created_by_an_evaluation(): + routes_assemblies.evaluate({"assembly": six_tile()}) + assert not (Path(paths.data_dir()) / "library.db").exists() + + +# -- 5. seam scale ------------------------------------------------------------------- +def test_5_seam_scale_mismatch_is_flagged_and_unknown_scale_stays_unknown(): + assert run(six_tile(), [])["seam_scale"]["status"] == "consistent" + doc = six_tile() + doc["tiles"][2]["scale"] = 95 + assert run(doc, [])["seam_scale"]["status"] == "mismatch" + doc = six_tile() + doc["tiles"][2]["scale"] = None + result = run(doc, [])["seam_scale"] + assert result["status"] == "unknown" and result["unknown"] == ["t3"] + doc["tiles"][3]["scale"] = 120 + assert run(doc, [])["seam_scale"]["status"] == "mismatch" + + +# -- 6. attribution manifest retained --------------------------------------------------- +def test_6_attribution_is_retained_through_evaluate_save_and_load(): + doc = six_tile() + report = run(doc, []) + assert report["attribution"] == doc["attribution"] + assert report["tiles"][0]["attribution"] == {"author": "Example Maker"} + saved = store.save(doc) + loaded = store.get(saved["id"])["assembly"] + assert loaded["attribution"] == doc["attribution"] + assert loaded["tiles"][0]["attribution"] == {"author": "Example Maker"} + + +# -- 7. shortage / unknown / stale --------------------------------------------------------- +def test_7_a_shared_spool_smaller_than_the_cumulative_need_is_a_shortage_naming_spool_and_tiles(): + report = run(six_tile(), [reading(ROAD, 150), reading(WATER, 500)]) + road = spool_row(report, ROAD) + assert report["verdict"] == "shortage" + assert road["verdict"] == "shortage" and road["spool"] == ROAD + assert road["starving_tiles"] == ["t4", "t5", "t6"] + assert road["shortfall_g"] == 90.0 + assert spool_row(report, WATER)["verdict"] == "enough" + + +def test_7_enough_when_supply_covers_every_tile(): + report = run(six_tile(), [reading(ROAD, 240), reading(WATER, 150)]) + assert report["verdict"] == "enough" + assert all(s["verdict"] == "enough" for s in report["spools"]) + + +def test_7_missing_or_null_supply_is_unknown_not_enough_and_not_shortage(): + report = run(six_tile(), [reading(WATER, 500)]) + assert spool_row(report, ROAD)["verdict"] == "unknown" and report["verdict"] == "unknown" + report = run(six_tile(), [reading(ROAD, None), reading(WATER, 500)]) + assert spool_row(report, ROAD)["verdict"] == "unknown" + report = run(six_tile(), [reading(ROAD, 500, ttl=0), reading(WATER, 500)]) + assert spool_row(report, ROAD)["verdict"] == "unknown" + no_time = {"spool": ROAD, "remaining_g": 10, "ttl_s": 600} + assert spool_row(run(six_tile(), [no_time, reading(WATER, 500)]), ROAD)["verdict"] == "unknown" + + +def test_7_stale_supply_is_stale_even_when_the_figure_is_small_or_large(): + for grams in (10, 5000): + report = run(six_tile(), [reading(ROAD, grams, age=5000, ttl=600), reading(WATER, 500)]) + row = spool_row(report, ROAD) + assert row["verdict"] == "stale" and row["starving_tiles"] == [] + assert report["verdict"] == "stale" + + +def test_7_a_restored_reading_and_an_ageing_reading_are_not_loadout_evidence(): + historical = run(six_tile(), [reading(ROAD, 5000, historical=True), reading(WATER, 500)]) + assert spool_row(historical, ROAD)["verdict"] == "stale" + ageing = run(six_tile(), [reading(ROAD, 5000, age=400, ttl=600), reading(WATER, 500)]) + assert spool_row(ageing, ROAD)["verdict"] == "stale" + allowed = run(six_tile(), [reading(ROAD, 5000, age=400, ttl=600, ageing_ok=True), reading(WATER, 500)]) + assert spool_row(allowed, ROAD)["verdict"] == "enough" + + +def test_7_conflicting_readings_for_one_spool_are_not_averaged_or_picked(): + report = run(six_tile(), [reading(ROAD, 500), reading(ROAD, 100), reading(WATER, 500)]) + assert spool_row(report, ROAD)["verdict"] == "unknown" + + +def test_7_a_known_overrun_is_a_shortage_even_when_other_tiles_state_no_grams(): + doc = six_tile() + doc["tiles"][5].pop("usage") + report = run(doc, [reading(ROAD, 100), reading(WATER, 500)]) + road = spool_row(report, ROAD) + assert road["verdict"] == "shortage" and road["unknown_tiles"] == ["t6"] + assert "lower bound" in road["reason"] + + +# -- 8. foundation-aware, legacy sufficiency never used ------------------------------------------- +def _source_files(): + root = Path(ev.__file__).parent + return [*root.glob("*.py"), Path(routes_assemblies.__file__)] + + +def test_8_material_plan_sufficiency_is_never_referenced(): + for path in _source_files(): + text = path.read_text(encoding="utf-8") + assert "_sufficiency" not in text, path + assert not re.search(r"\bmaterial_plan\b\s*(import|\.)|import\s+material_plan|from\s+\.+\s*material_plan", text), path + assert not re.search(r"\bproject_materials\b|\breadiness\b", text), path + + +def test_8_supply_goes_through_observation_freshness(monkeypatch): + seen = [] + real = ev.obs_mod.freshness + monkeypatch.setattr(ev.obs_mod, "freshness", lambda o, now=None: (seen.append(o.observation_id), real(o, now))[1]) + run(six_tile(), [reading(ROAD, 500), reading(WATER, 500)]) + assert sorted(seen) == ["supply:0", "supply:1"] + + +# -- 9. unresolved slot-to-spool binding --------------------------------------------------------------- +def test_9_a_role_without_a_binding_leaves_physical_consumption_unknown(): + doc = six_tile() + del doc["bindings"]["water"] + report = run(doc, [reading(ROAD, 5000), reading(WATER, 5000)]) + assert report["verdict"] == "unknown" + assert {u["reason"] for u in report["unresolved"]} == {"no_binding"} + assert len(report["unresolved"]) == 6 + assert [s["spool"] for s in report["spools"]] == [ROAD] + + +def test_9_a_slot_with_grams_and_no_role_or_two_roles_is_unresolved(): + doc = six_tile() + doc["tiles"][0]["usage"]["slot:3"] = 10 + doc["tiles"][1]["roles"]["extra"] = "slot:0" + report = run(doc, [reading(ROAD, 5000), reading(WATER, 5000)]) + reasons = {(u["tile_id"], u["slot"]): u["reason"] for u in report["unresolved"]} + assert reasons == {("t1", "slot:3"): "no_role", ("t2", "slot:0"): "ambiguous_role"} + assert report["verdict"] == "unknown" + + +def test_9_a_known_zero_on_an_unresolved_slot_does_not_block(): + doc = six_tile() + doc["tiles"][0]["usage"]["slot:3"] = 0 + report = run(doc, [reading(ROAD, 5000), reading(WATER, 5000)]) + assert report["unresolved"] == [] and report["verdict"] == "enough" + + +# -- 10. assemblies.db through the store, cross-reference without a foreign key ----------------------------- +def test_10_assemblies_db_lives_in_the_foundation_store_and_has_no_foreign_key(): + saved = store.save(six_tile()) + db = Path(paths.data_dir()) / "store" / "assemblies.db" + assert db.is_file() and saved["revision"] == 1 + conn = sqlite3.connect(db) + try: + for table in ("assemblies", "tile_refs"): + assert conn.execute(f"PRAGMA foreign_key_list({table})").fetchall() == [] + assert conn.execute("SELECT library_project_id, library_sha256 FROM tile_refs").fetchall() == [(11, "1" * 64)] + assert conn.execute("PRAGMA user_version").fetchone()[0] == store.VERSION + finally: + conn.close() + assert [a["assembly_id"] for a in store.assemblies_for_project(11)] == [saved["id"]] + assert store.assemblies_for_project(11, "2" * 64) == [] + assert store.assemblies_for_project(12) == [] + + +def test_10_update_needs_the_revision_it_was_read_at_and_a_stale_one_changes_nothing(): + first = store.save(six_tile()) + doc = six_tile() + doc["name"] = "Renamed" + second = store.save(doc, first["id"], first["revision"]) + assert second["revision"] == 2 + with pytest.raises(RevisionConflict): + store.save(six_tile(), first["id"], first["revision"]) + with pytest.raises(RevisionConflict): + store.save(six_tile(), first["id"], None) + assert store.get(first["id"])["assembly"]["name"] == "Renamed" + with pytest.raises(store.NotFound): + store.save(six_tile(), 999, 1) + + +def test_10_deleting_a_saved_assembly_removes_its_refs_only(): + saved = store.save(six_tile()) + assert store.delete(saved["id"]) is True and store.delete(saved["id"]) is False + assert store.list_saved() == [] and store.assemblies_for_project(11) == [] + + +def test_10_a_newer_database_is_refused_untouched(): + store.save(six_tile()) + db = Path(paths.data_dir()) / "store" / "assemblies.db" + conn = sqlite3.connect(db) + conn.execute(f"PRAGMA user_version = {store.VERSION + 1}") + conn.close() + from snapstudio_core.foundation.store import StoreVersionError + with pytest.raises(StoreVersionError): + store.list_saved() + conn = sqlite3.connect(db) + assert conn.execute("SELECT COUNT(*) FROM assemblies").fetchone()[0] == 1 + conn.close() + + +# -- routes: shapes through the seam ----------------------------------------------------------------------- +class _Handler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(path, data): + handler = _Handler() + assert ext_routes.dispatch(handler, path, data) is True + return handler.sent[-1] + + +def test_routes_are_registered_under_the_assemblies_prefix(): + for path in ("evaluate", "save", "list", "get", "delete"): + assert f"/assemblies/{path}" in ext_routes.ROUTES + + +def test_route_evaluate_returns_an_advisory_report(): + status, body = call("/assemblies/evaluate", {"assembly": six_tile(), "now": NOW, + "supply": [reading(ROAD, 150), reading(WATER, 500)]}) + assert status == 200 + assert body["report"]["verdict"] == "shortage" + assert body["report"]["advisory"].startswith("Advisory only") + + +@pytest.mark.parametrize("data", [ + {}, {"assembly": []}, {"assembly": {"name": ""}}, {"assembly": {"name": "x", "tiles": "no"}}, + {"assembly": {"name": "x"}, "supply": "no"}, {"assembly": {"name": "x"}, "now": "soon"}, + {"assembly": {"name": "x"}, "supply": [{"spool": {"provider_instance_id": "p"}, "remaining_g": 1}]}, + {"assembly": {"name": "x", "schema_version": "assembly/9"}}, +]) +def test_route_evaluate_refuses_bad_input_with_400(data): + status, body = call("/assemblies/evaluate", data) + assert status == 400 and body["error"] == "invalid" and "Traceback" not in body["message"] + + +def test_route_save_get_list_delete_round_trip_and_error_shapes(): + status, saved = call("/assemblies/save", {"assembly": six_tile()}) + assert status == 200 and saved["revision"] == 1 + assert call("/assemblies/list", {})[1]["assemblies"][0]["tiles"] == 6 + assert call("/assemblies/get", {"id": saved["id"]})[1]["assembly"]["name"] == six_tile()["name"] + status, body = call("/assemblies/save", {"assembly": six_tile(), "id": saved["id"], "expected_revision": 7}) + assert status == 422 and body["error"] == "refusal" + assert call("/assemblies/get", {"id": 999})[0] == 400 + assert call("/assemblies/get", {"id": "1"})[0] == 400 + assert call("/assemblies/delete", {"id": saved["id"]})[1] == {"deleted": True} + + +def test_route_responses_never_echo_a_credential_like_header_value(): + doc = six_tile() + doc["attribution"].append({"item": "x", "author": "SENTINEL-KEY-9f3a"}) + status, body = call("/assemblies/evaluate", {"assembly": {**doc, "tiles": "bad"}}) + assert "SENTINEL-KEY-9f3a" not in json.dumps(body) + + +# -- guards: source greps over the package ----------------------------------------------------------------------- +def test_g1_the_package_has_no_network_or_printer_control_references(): + banned = ("urlopen", "requests", "http.client", "socket", "moonraker", "netguard", "pause", "resume", "cancel") + for path in _source_files(): + text = path.read_text(encoding="utf-8") + for word in banned: + assert not re.search(rf"\b{re.escape(word)}\b", text), (path.name, word) + + +def test_g3_the_package_never_counts_heads_from_slot_or_nozzle_counts(): + for path in _source_files(): + text = path.read_text(encoding="utf-8") + assert "slot_count" not in text and "len(nozzles)" not in text, path + + +def test_c7_no_overclaiming_words_in_package_files(): + banned = ["simul" + "taneous", "guaran" + "teed", "100" + "%"] + files = _source_files() + list(FIXTURES.glob("*")) + [HERE.parents[2] / "docs" / "internal" / "ASSEMBLIES.md"] + for path in files: + if path.is_file(): + text = path.read_text(encoding="utf-8").lower() + for word in banned: + assert word not in text, (path.name, word) + + +def test_c3_the_fixture_directory_has_provenance(): + text = (FIXTURES / "PROVENANCE.md").read_text(encoding="utf-8") + assert "synthetic" in text and "documentation-derived" in text + + +def test_the_document_round_trips_unchanged_and_bounds_hold(): + doc = six_tile() + once = read_assembly(doc).to_dict() + assert read_assembly(once).to_dict() == once + big = copy.deepcopy(doc) + big["tiles"] = [{"tile_id": f"x{i}", "name": "n"} for i in range(65)] + with pytest.raises(AssemblyInputError): + read_assembly(big) + dup = copy.deepcopy(doc) + dup["tiles"][1]["tile_id"] = "t1" + with pytest.raises(AssemblyInputError): + read_assembly(dup) diff --git a/backend/tests/calibration/test_calibration.py b/backend/tests/calibration/test_calibration.py new file mode 100644 index 00000000..d8534d46 --- /dev/null +++ b/backend/tests/calibration/test_calibration.py @@ -0,0 +1,160 @@ +"""P4 calibration acceptance tests; evidence label: fixture-tested.""" +from __future__ import annotations + +import json +import sqlite3 +from pathlib import Path + +import pytest + +from snapstudio_api import ext_routes +from snapstudio_api import routes_calibration # noqa: F401 (registers routes) +from snapstudio_core.calibration import palette, service +from snapstudio_core.foundation.store import StoreVersionError, open_store + +FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" / "calibration" + + +def test_1_ditherforge_rows_parse_both_td_forms_and_derive_missing_td(): + decoded = palette.parse_ditherforge((FIXTURES / "ditherforge-sample.txt").read_text()) + assert decoded.valid_for_evaluation + assert [entry.label for entry in decoded.value.entries] == ["Vermilion", "Blue", "Ochre"] + assert decoded.value.entries[0].td == 0.8 and decoded.value.entries[0].td_provenance == "provided" + assert decoded.value.entries[1].td == 1.0 and decoded.value.entries[1].td_provenance == "derived" + + +def test_2_kromacut_v2_is_decoded_and_other_versions_remain_unknown(): + accepted = palette.parse_kromacut((FIXTURES / "kromacut-v2.json").read_text()) + unknown_raw = json.loads((FIXTURES / "kromacut-v3-unknown.json").read_text()) + rejected = palette.parse_kromacut(unknown_raw) + assert accepted.valid_for_evaluation and accepted.value.source == "kromacut_json_v2" + assert accepted.value.entries[1].td_provenance == "derived" + assert rejected.value is None and not rejected.valid_for_evaluation + assert rejected.raw == unknown_raw + + +def test_3_frontlit_basis_identity_never_merges_with_another_family(): + front = service.basis_document("frontlit", 0.2, 0.4, "PLA") + back = service.basis_document("backlit", 0.2, 0.4, "PLA") + assert front["calibration_basis_id"] != back["calibration_basis_id"] + with pytest.raises(ValueError): + service.basis_document("other", 0.2, 0.4, "PLA") + + +def test_4_layer_height_change_gets_new_basis_and_keeps_prior_run(tmp_path): + parsed = palette.parse_ditherforge("#112233 td Ink").value.to_dict() + first_basis = service.basis_document("frontlit", 0.2, 0.4, "PLA") + second_basis = service.basis_document("frontlit", 0.16, 0.4, "PLA") + first = service.save_run(first_basis, parsed, service.evaluate(parsed, first_basis, {}, []), str(tmp_path)) + second = service.save_run(second_basis, parsed, service.evaluate(parsed, second_basis, {}, []), str(tmp_path)) + history = service.history(str(tmp_path)) + assert first["calibration_basis"]["calibration_basis_id"] != second["calibration_basis"]["calibration_basis_id"] + assert first["inputs_revision"]["revision"] != second["inputs_revision"]["revision"] + assert {row["calibration_basis"]["calibration_basis_id"] for row in history} == { + first_basis["calibration_basis_id"], second_basis["calibration_basis_id"]} + + +def test_5_simulated_palette_and_physical_reading_stay_distinct(): + parsed = palette.parse_ditherforge("#112233 td Ink").value.to_dict() + basis = service.basis_document("frontlit", 0.2, 0.4, "PLA") + result = service.evaluate(parsed, basis, {"0": "#102030"}, []) + row = result["entries"][0] + assert row["simulated_color"] == "#112233" and row["physical_color"] == "#102030" + assert row["physical_distance"] is not None + + +def test_6_owned_filament_alternatives_use_advisory_colour_distance(): + parsed = palette.parse_ditherforge("#112233 td Ink").value.to_dict() + basis = service.basis_document("frontlit", 0.2, 0.4, "PLA") + result = service.evaluate(parsed, basis, {}, [{"label": "Near", "color": "#112234"}, + {"label": "Far", "color": "#FFFFFF"}]) + alternatives = result["entries"][0]["alternatives"] + assert alternatives[0]["label"] == "Near" and alternatives[0]["distance"] < alternatives[1]["distance"] + assert all(item["advisory"] for item in alternatives) + + +def test_7_calibration_store_refuses_newer_schema_without_replacing_rows(tmp_path): + (tmp_path / "store").mkdir() + conn = sqlite3.connect(tmp_path / "store" / "calibration.db") + if conn.execute("PRAGMA user_version").fetchone()[0] < 2: + conn.execute("CREATE TABLE IF NOT EXISTS preserved (value TEXT)") + conn.execute("DELETE FROM preserved") + conn.execute("INSERT INTO preserved VALUES ('synthetic')") + conn.execute("PRAGMA user_version = 2") + conn.commit() + conn.close() + with pytest.raises(StoreVersionError): + with open_store("calibration", "CREATE TABLE replacement (value TEXT);", 1, str(tmp_path)): + pass + conn = sqlite3.connect(tmp_path / "store" / "calibration.db") + try: + assert conn.execute("SELECT value FROM preserved").fetchone()[0] == "synthetic" + assert conn.execute("SELECT name FROM sqlite_master WHERE name='replacement'").fetchone() is None + finally: + conn.close() + + +def test_7b_failed_store_migration_rolls_back_schema_and_rows(tmp_path): + data_dir = tmp_path + with open_store("calibration", "CREATE TABLE preserved (value TEXT);", 1, str(data_dir)) as conn: + if conn.execute("SELECT COUNT(*) FROM preserved").fetchone()[0] == 0: + conn.execute("INSERT INTO preserved VALUES ('synthetic')") + with pytest.raises(sqlite3.OperationalError): + with open_store("calibration", [ + (1, "CREATE TABLE IF NOT EXISTS preserved (value TEXT);"), + (2, "ALTER TABLE preserved ADD COLUMN added TEXT; SELECT * FROM missing_table;"), + ], 2, str(data_dir)): + pass + conn = sqlite3.connect(data_dir / "store" / "calibration.db") + try: + assert conn.execute("PRAGMA user_version").fetchone()[0] == 1 + assert conn.execute("SELECT value FROM preserved").fetchone()[0] == "synthetic" + assert "added" not in {row[1] for row in conn.execute("PRAGMA table_info(preserved)")} + finally: + conn.close() + + +def test_8_basis_id_is_returned_and_included_in_derivation_revision(): + parsed = palette.parse_ditherforge("#112233 td Ink").value.to_dict() + basis = service.basis_document("frontlit", 0.2, 0.4, "PLA") + saved = service.save_run(basis, parsed, service.evaluate(parsed, basis, {}, [])) + assert saved["calibration_basis"]["calibration_basis_id"] == basis["calibration_basis_id"] + assert saved["inputs_revision"]["calibration_basis_id"] == basis["calibration_basis_id"] + + +def test_route_evaluates_palette_and_saves_local_revision(tmp_path, monkeypatch): + monkeypatch.setenv("SNAPSTUDIO_DATA_DIR", str(tmp_path)) + handler = type("Handler", (), {"_send": lambda self, code, body: self.sent.append((code, body))})() + handler.sent = [] + assert ext_routes.dispatch(handler, "/calibration/evaluate", { + "source": "ditherforge_txt", "content": "#112233 Ink", "family": "frontlit", "layer_height_mm": 0.2, + "physical_colors": {"0": "#102030"}, "owned_filaments": [{"label": "Owned red", "color": "#112234"}], + }) + code, body = handler.sent[0] + assert code == 200 and body["status"] == "evaluated" + assert body["result"]["entries"][0]["physical_color"] == "#102030" + assert body["result"]["entries"][0]["alternatives"][0]["label"] == "Owned red" + assert service.history(str(tmp_path)) + + +def test_routes_do_not_echo_or_persist_a_sentinel_credential(tmp_path, monkeypatch, caplog): + sentinel = "SENTINEL-KEY-9f3a" + monkeypatch.setenv("SNAPSTUDIO_DATA_DIR", str(tmp_path)) + request = {"source": "ditherforge_txt", "content": "#112233 Ink", "family": "frontlit", + "layer_height_mm": 0.2, "api_key": sentinel} + for path, body in (("/calibration/evaluate", request), ("/calibration/history", {"api_key": sentinel})): + handler = type("Handler", (), {"_send": lambda self, code, response: self.sent.append((code, response))})() + handler.sent = [] + ext_routes.dispatch(handler, path, body) + assert sentinel not in json.dumps(handler.sent) + assert sentinel not in caplog.text + assert all(sentinel not in item.read_bytes().decode("utf-8", "ignore") + for item in tmp_path.rglob("*") if item.is_file()) + + +def test_route_returns_unknown_for_unsupported_kromacut_version(): + handler = type("Handler", (), {"_send": lambda self, code, body: self.sent.append((code, body))})() + handler.sent = [] + ext_routes.dispatch(handler, "/calibration/evaluate", {"source": "kromacut_json", "content": '{"version":9}'}) + code, body = handler.sent[0] + assert code == 200 and body["status"] == "unknown" and body["result"] is None diff --git a/backend/tests/calibration/test_real_palette_formats.py b/backend/tests/calibration/test_real_palette_formats.py new file mode 100644 index 00000000..ca115192 --- /dev/null +++ b/backend/tests/calibration/test_real_palette_formats.py @@ -0,0 +1,73 @@ +import json +import pytest +from snapstudio_core.calibration import palette, service + +def test_upstream_ditherforge_numeric_td_and_comments(): + result = palette.parse_ditherforge("# Collection comment\n#FF0000 1.9 Red\n#00FF00 0.4 Green\n#0000FF Blue") + assert result.valid_for_evaluation + assert [(e.label,e.td,e.td_provenance) for e in result.value.entries] == [ + ("Red",1.9,"provided"),("Green",0.4,"provided"),("Blue",1.0,"derived")] + +@pytest.mark.parametrize("td", ["0", "-1", "1e999"]) +def test_numeric_td_must_be_positive_finite(td): + assert palette.parse_ditherforge(f"#FF0000 {td} Red").value is None + +def test_kromacut_exported_v2_colors_names_and_disabled_indices(): + raw = {"version":2,"name":"Synthetic palette","colors":["#FF0000","#00FF00"], + "colorNames":["Red","Green"],"disabledColors":[1],"createdAt":1,"updatedAt":1} + result=palette.parse_kromacut(json.dumps(raw)) + assert result.valid_for_evaluation + assert [e.color for e in result.value.entries] == ["#FF0000","#00FF00"] + assert [e.label for e in result.value.entries] == ["Red","Green"] + assert [e.enabled for e in result.value.entries] == [True,False] + assert all(e.td_provenance=="derived" for e in result.value.entries) + +@pytest.mark.parametrize("changes", [ + {"colors":["not-a-colour"]},{"disabledColors":[9]},{"disabledColors":[True]}, + {"colorNames":"not-an-array"}]) +def test_invalid_kpal_metadata_is_refused(changes): + raw={"version":2,"colors":["#FF0000"],"colorNames":["Red"],"disabledColors":[]} + assert palette.parse_kromacut({**raw,**changes}).value is None + +def test_basis_distinguishes_generator_model_and_first_layer(): + common=("frontlit",0.2,0.4,"PLA") + a=service.basis_document(*common,source="ditherforge_txt",model="swatch-A",first_layer_height_mm=0.2) + for changes in [dict(source="kromacut_json"),dict(model="swatch-B"),dict(first_layer_height_mm=0.3)]: + b=service.basis_document(*common,**{**dict(source="ditherforge_txt",model="swatch-A",first_layer_height_mm=0.2),**changes}) + assert a["calibration_basis_id"] != b["calibration_basis_id"] + +def test_disabled_color_keeps_its_index_without_claiming_a_physical_reading(): + from snapstudio_core.calibration.service import evaluate + parsed = palette.parse_kromacut({"version": 2, "colors": ["#112233", "#445566"], "disabledColors": [0]}).value.to_dict() + result = evaluate(parsed, service.basis_document("frontlit", .2, .4, "PLA"), {"0": "#aaaaaa", "1": "#bbbbbb"}, []) + assert result["entries"][0]["enabled"] is False + assert result["entries"][0]["physical_color"] is None + assert result["entries"][1]["physical_color"] == "#bbbbbb" + +def test_route_persists_basis_identity_and_real_kpal_rows(tmp_path, monkeypatch): + from snapstudio_api.routes_calibration import evaluate, get_history + import json + monkeypatch.setenv("SNAPSTUDIO_DATA_DIR", str(tmp_path)) + code, body = evaluate({"source": "kromacut_json", "content": json.dumps({"version": 2, "colors": ["#123456"]}), + "family": "backlit", "layer_height_mm": .16, "nozzle_mm": .6, "material": "PETG", "model": "synthetic-swatch-2", "first_layer_height_mm": .24}) + assert code == 200 and body["status"] == "evaluated" + basis = body["result"]["calibration_basis"] + assert basis["source"] == "kromacut_json" and basis["model"] == "synthetic-swatch-2" + assert basis["first_layer_height_mm"] == .24 and basis["nozzle_mm"] == .6 + assert get_history({})[1]["runs"][0]["calibration_basis"] == basis + +@pytest.mark.parametrize("row", ["#FF0000 td=oops Red", "#FF0000 td=1.2junk Red", "#FF0000 1.9 Red td=1..2", "#FF0000 1.9 Red td=2"]) +def test_invalid_or_conflicting_td_does_not_fall_back_to_nominal(row): + assert palette.parse_ditherforge(row).value is None + +@pytest.mark.parametrize("row", ["#FF0000 1.9 Red td=1.9 td=2", "#FF0000 td=1.9 Red td=oops", "#FF0000 td 1.9 Red td=2", "#FF0000 td=1.9 td=1.9"]) +def test_all_explicit_td_values_are_validated(row): + assert palette.parse_ditherforge(row).value is None + +@pytest.mark.parametrize("material", ["", " "]) +def test_empty_material_is_not_silently_replaced_with_pla(tmp_path, monkeypatch, material): + from snapstudio_api.routes_calibration import evaluate + from snapstudio_api.request_validation import ValidationError + monkeypatch.setenv("SNAPSTUDIO_DATA_DIR", str(tmp_path)) + with pytest.raises(ValidationError): + evaluate({"source":"ditherforge_txt", "content":"#FF0000 Red", "family":"frontlit", "layer_height_mm":.2, "material": material}) diff --git a/backend/tests/creative/__init__.py b/backend/tests/creative/__init__.py new file mode 100644 index 00000000..b620388b --- /dev/null +++ b/backend/tests/creative/__init__.py @@ -0,0 +1 @@ +"""Creative reader contract tests.""" diff --git a/backend/tests/creative/test_container_xml_hardening.py b/backend/tests/creative/test_container_xml_hardening.py new file mode 100644 index 00000000..bc7360ce --- /dev/null +++ b/backend/tests/creative/test_container_xml_hardening.py @@ -0,0 +1,44 @@ +"""The 3MF model XML guard refuses entity/DTD content in every encoding, not only plain UTF-8. + +Regression for the UTF-16 bypass found in the #140 aggregate review: a byte-substring check misses NUL-interleaved +DOCTYPE/ENTITY, and expat auto-detects UTF-16 and expands the entities. +""" +from __future__ import annotations + +import pytest + +from snapstudio_core.creative.container import ContainerError, read_3mf +from fixtures.creative.zip_factory import make_3mf, model_xml + +BOMB = ( + ']>' + '&b;' +) + + +@pytest.mark.parametrize("encoding", ["utf-8", "utf-16-le", "utf-16-be", "utf-16", "utf-32-le", "utf-32-be", "utf-32"]) +def test_entity_documents_are_refused_in_every_encoding(encoding): + with pytest.raises(ContainerError) as info: + read_3mf(make_3mf(model=BOMB.encode(encoding))) + assert info.value.args[0] in ("unsafe_xml", "bad_xml") + + +@pytest.mark.parametrize("encoding", ["utf-16-le", "utf-16-be", "utf-32-le"]) +def test_a_bom_less_wide_encoding_is_unsafe_not_parsed(encoding): + plain = model_xml().decode("utf-8") if isinstance(model_xml(), bytes) else model_xml() + with pytest.raises(ContainerError) as info: + read_3mf(make_3mf(model=plain.encode(encoding))) + assert info.value.args[0] == "unsafe_xml" + + +def test_lowercase_doctype_is_refused(): + lowered = BOMB.replace("DOCTYPE", "doctype").replace("ENTITY", "entity") + with pytest.raises(ContainerError) as info: + read_3mf(make_3mf(model=lowered.encode("utf-8"))) + assert info.value.args[0] == "unsafe_xml" + + +def test_a_plain_utf8_model_with_a_bom_is_still_read(): + plain = model_xml() + raw = plain if isinstance(plain, bytes) else plain.encode("utf-8") + assert read_3mf(make_3mf(model=bytes([0xEF, 0xBB, 0xBF]) + raw)) is not None diff --git a/backend/tests/creative/test_creative_reader_contract_shapes.py b/backend/tests/creative/test_creative_reader_contract_shapes.py new file mode 100644 index 00000000..0f1d2fae --- /dev/null +++ b/backend/tests/creative/test_creative_reader_contract_shapes.py @@ -0,0 +1,137 @@ +"""Contract tests for the exact published key allowlists and safe 3MF reading.""" +from __future__ import annotations + +import zipfile + +import pytest + +from snapstudio_core.creative.container import ContainerError, read_3mf +from snapstudio_core.creative.readers import ( + _FORCA_KEYS, _FULLSPECTRUM_KEYS, _LIX_KEYS, + read_forca, read_fullspectrum, read_imagemap, read_lixnix, read_lumina, +) +from fixtures.creative.zip_factory import make_3mf, model_xml + + +def test_forca_fixture_published_key_contract() -> None: + """Research lines device-research.json:134-150 and fos_config receipts name these keys.""" + config = {key: [0.4] for key in _FORCA_KEYS} + config["fos_mms_topology"] = "opaque documented key" + project = read_forca(config) + assert set(project.understood) == set(_FORCA_KEYS) + assert project.missing == () + changed = read_forca({**config, "fos_unpublished_guess": True}) + assert "fos_unpublished_guess" in changed.unknown + archive_read = read_forca(make_3mf(settings={"fos_physical_nozzle_diameter": [0.4], "other": "kept"}, + members={"Metadata/vendor/opaque.bin": b"opaque"})) + assert "Metadata/vendor/opaque.bin" in {item["name"] for item in archive_read.passed_through} + assert "other" in {item["name"] for item in archive_read.passed_through} + + +def test_lixnix_fixture_published_key_contract() -> None: + """Research lines device-research.json:155-168 and lix_config receipts name these keys.""" + config = {"extruder_layer_height": [0.1, 0.2], "extruder_layer_height_exact": True} + project = read_lixnix(config) + assert set(project.understood) == set(_LIX_KEYS) + assert read_lixnix({"extruder_layer_height": [0.1]}).missing == ("extruder_layer_height_exact",) + assert "extruder_layer_height_unpublished" in read_lixnix( + {**config, "extruder_layer_height_unpublished": [0.3]} + ).unknown + + +def test_fullspectrum_published_mixed_rows_become_simulated_virtual_recipes() -> None: + """Packaging notes:162-211, and workflow-research xipit entry:232-272.""" + project = read_fullspectrum({"mixed_filament_definitions": "1,2,1,1,50,0,g,w,m2,z0,xa0,xb0,d0,o0,u17"}) + assert set(project.understood) == set(_FULLSPECTRUM_KEYS) + assert project.physical_slots_needed == 2 + assert project.source_component_ids == (1, 2) + assert project.simulated is True + assert project.recipes[0].recipe_id == "fullspectrum:17" + assert all(recipe.simulated is True for recipe in project.recipes) + assert all(recipe.execution_supported is False for recipe in project.recipes) + assert all(component.slot_ref.startswith("project:1:") for recipe in project.recipes for component in recipe.components) + gradient = read_fullspectrum({"mixed_filament_definitions": "1,2,1,1,50,0,g123,w50/25/25,m2,z0,xa0,xb0,d0,o0,u18"}) + assert gradient.source_component_ids == (1, 2, 3) + assert gradient.physical_slots_needed == 3 + assert read_fullspectrum({"mixed_filament_definitions": {"draft": True}}).unknown == ( + "mixed_filament_definitions", + ) + assert read_fullspectrum({}).missing == ("mixed_filament_definitions",) + + +def test_3mf_manifest_hashes_unknown_members_without_rewriting() -> None: + extension = b"opaque extension bytes\x00" + data = make_3mf(members={"Metadata/vendor/extension.bin": extension}) + manifest = read_3mf(data) + passthrough = next(part for part in manifest.passed_through if part.name == "Metadata/vendor/extension.bin") + import hashlib + assert passthrough.sha256 == hashlib.sha256(extension).hexdigest() + assert "Metadata/vendor/extension.bin" not in manifest.understood_parts + assert {"name": "Metadata/vendor/extension.bin", "sha256": passthrough.sha256} in manifest.to_dict()["passed_through"] + + +def test_imagemap_shape_stays_unknown_but_archive_identity_is_available() -> None: + """Research says serialized ImageMap 3MF carrier is not established (creative-research:138-156).""" + result = read_imagemap(make_3mf(members={"Metadata/image-zones.bin": b"zones"})) + assert result.unknown == ("ImageMap serialized metadata carrier and zone shape",) + assert result.diagnostics == ("shape_not_established_in_research",) + + +def test_lumina_reader_reports_only_3mf_artifact_identity() -> None: + result = read_lumina(make_3mf(settings={"private_generator_state": "opaque"}), file_id="opaque-id") + assert set(result.understood) == {"3mf_artifact_identity"} + assert result.artifact_identity["file_id"] == "opaque-id" + assert result.unknown == ("Lumina generation provenance",) + + +@pytest.mark.parametrize("payload", [make_3mf()[:-12], b"not a zip"]) +def test_truncated_or_invalid_zip_is_rejected(payload: bytes) -> None: + with pytest.raises(ContainerError): + read_3mf(payload) + + +def test_expansion_cap_rejects_large_member() -> None: + data = make_3mf(members={"Metadata/large.bin": b"x" * (16 * 1024 * 1024 + 1)}, compression=zipfile.ZIP_STORED) + with pytest.raises(ContainerError, match="member_too_large"): + read_3mf(data) + + +@pytest.mark.parametrize("name", ["../escape", "Metadata/../../escape", "/absolute", "C:/drive"]) +def test_path_traversal_member_names_are_rejected(name: str) -> None: + with pytest.raises(ContainerError, match="unsafe_member_name"): + read_3mf(make_3mf(members={name: b"x"})) + + +def test_duplicate_member_is_rejected() -> None: + with pytest.warns(UserWarning): + data = make_3mf(duplicate=("Metadata/repeat.bin", b"one", b"two")) + with pytest.raises(ContainerError, match="duplicate_member"): + read_3mf(data) + + +def test_bad_xml_and_wrong_3mf_namespace_are_rejected() -> None: + with pytest.raises(ContainerError, match="bad_xml"): + read_3mf(make_3mf(model=b"")) + with pytest.raises(ContainerError, match="unsupported_3mf_version"): + read_3mf(make_3mf(model=model_xml(namespace="urn:unsupported:3mf:2"))) + + +def test_missing_model_is_rejected() -> None: + output = __import__("io").BytesIO() + with zipfile.ZipFile(output, "w") as archive: + archive.writestr("Metadata/project_settings.config", b"{}") + with pytest.raises(ContainerError, match="missing_model"): + read_3mf(output.getvalue()) + + +def test_missing_required_forca_key_and_contradictory_nozzle_semantics_stay_explicit() -> None: + project = read_forca({"nozzle_diameter": [0.4], "fos_filament_nozzle": [0.6]}) + assert project.missing == ("fos_physical_nozzle_diameter",) + assert "nozzle_diameter" not in project.understood + assert "nozzle_diameter" in {item["name"] for item in project.passed_through} + assert project.unknown == () + + +def test_lixnix_exact_flag_with_empty_vector_is_reported_as_contradictory() -> None: + project = read_lixnix({"extruder_layer_height": [], "extruder_layer_height_exact": True}) + assert project.diagnostics == ("exact_flag_without_layer_height_values",) diff --git a/backend/tests/fixtures/assemblies/PROVENANCE.md b/backend/tests/fixtures/assemblies/PROVENANCE.md new file mode 100644 index 00000000..6ee002ce --- /dev/null +++ b/backend/tests/fixtures/assemblies/PROVENANCE.md @@ -0,0 +1,14 @@ +# Assembly fixtures: provenance + +Every file here is synthetic and documentation-derived. None is a capture from a real printer, +spool provider, library or model file. + +- `six_tile.json`: a six-tile road-and-water diorama invented for these tests (three plates, two tiles + each, two shared roles). Names, ids and gram figures are made up. The spool ids belong to a provider + called `synthetic`; no such service exists. +- `slot_usage_tile.json`: shaped like the output of `snapstudio_core.project_compare.slot_usage` + (slots numbered from 1, `sliced_g` per slot) as documented in that module. Hand-written. +- `gcode_facts_tile.json`: shaped like the `slots` list of `snapstudio_core.gcode.read_facts` + (tools numbered from 0, `grams` per slot). Hand-written. + +Evidence label: fixture-tested. diff --git a/backend/tests/fixtures/assemblies/gcode_facts_tile.json b/backend/tests/fixtures/assemblies/gcode_facts_tile.json new file mode 100644 index 00000000..f8de29db --- /dev/null +++ b/backend/tests/fixtures/assemblies/gcode_facts_tile.json @@ -0,0 +1,8 @@ +{ + "available": true, + "slots": [ + {"tool": 0, "used": true, "grams": 12.5, "mm": 4100.0}, + {"tool": 1, "used": null, "grams": null, "mm": null}, + {"tool": 2, "used": true, "grams": 4.0, "mm": 1300.0} + ] +} diff --git a/backend/tests/fixtures/assemblies/six_tile.json b/backend/tests/fixtures/assemblies/six_tile.json new file mode 100644 index 00000000..cb14b983 --- /dev/null +++ b/backend/tests/fixtures/assemblies/six_tile.json @@ -0,0 +1,35 @@ +{ + "name": "Harbour diorama (synthetic)", + "parts": [ + {"part_id": "quay", "name": "Quay"}, + {"part_id": "road", "name": "Road"}, + {"part_id": "bridge", "name": "Bridge"}, + {"part_id": "lighthouse", "name": "Lighthouse"}, + {"part_id": "harbour", "name": "Harbour"}, + {"part_id": "beach", "name": "Beach"}, + {"part_id": "pier", "name": "Pier"} + ], + "bindings": { + "road": {"provider_instance_id": "synthetic", "provider_spool_id": "road-1"}, + "water": {"provider_instance_id": "synthetic", "provider_spool_id": "water-1"} + }, + "attribution": [ + {"item": "Harbour tile set", "author": "Example Maker", "license": "CC BY 4.0", "source": "example.invalid/harbour"} + ], + "tiles": [ + {"tile_id": "t1", "name": "Tile 1", "plate": 1, "part_id": "quay", "scale": 100, + "library": {"project_id": 11, "sha256": "1111111111111111111111111111111111111111111111111111111111111111"}, + "roles": {"road": "slot:0", "water": "slot:1"}, "usage": {"slot:0": 40, "slot:1": 25}, + "attribution": {"author": "Example Maker"}}, + {"tile_id": "t2", "name": "Tile 2", "plate": 1, "part_id": "road", "scale": 100, + "roles": {"road": "slot:0", "water": "slot:1"}, "usage": {"slot:0": 40, "slot:1": 25}}, + {"tile_id": "t3", "name": "Tile 3", "plate": 2, "part_id": "bridge", "scale": 100, + "roles": {"road": "slot:1", "water": "slot:0"}, "usage": {"slot:0": 25, "slot:1": 40}}, + {"tile_id": "t4", "name": "Tile 4", "plate": 2, "part_id": "lighthouse", "scale": 100, + "roles": {"road": "slot:0", "water": "slot:1"}, "usage": {"slot:0": 40, "slot:1": 25}}, + {"tile_id": "t5", "name": "Tile 5", "plate": 3, "part_id": "harbour", "scale": 100, + "roles": {"road": "slot:0", "water": "slot:1"}, "usage": {"slot:0": 40, "slot:1": 25}}, + {"tile_id": "t6", "name": "Tile 6", "plate": 3, "part_id": "beach", "scale": 100, + "roles": {"road": "slot:0", "water": "slot:1"}, "usage": {"slot:0": 40, "slot:1": 25}} + ] +} diff --git a/backend/tests/fixtures/assemblies/slot_usage_tile.json b/backend/tests/fixtures/assemblies/slot_usage_tile.json new file mode 100644 index 00000000..451de9b8 --- /dev/null +++ b/backend/tests/fixtures/assemblies/slot_usage_tile.json @@ -0,0 +1,9 @@ +{ + "slots": { + "1": {"object_extruder": [], "part_extruder": [], "painted": false, "colour_changes": [], "process_roles": [], "sliced_g": 12.5, "referenced_by": ["sliced_usage"], "verdict": "referenced"}, + "2": {"object_extruder": [], "part_extruder": [], "painted": false, "colour_changes": [], "process_roles": [], "sliced_g": null, "referenced_by": ["object_extruder"], "verdict": "referenced"}, + "3": {"object_extruder": [], "part_extruder": [], "painted": false, "colour_changes": [], "process_roles": [], "sliced_g": 4.0, "referenced_by": ["sliced_usage"], "verdict": "referenced"} + }, + "painting": {"present": false, "complete": true, "truncated": false}, + "object_list_readable": true +} diff --git a/backend/tests/fixtures/calibration/PROVENANCE.md b/backend/tests/fixtures/calibration/PROVENANCE.md new file mode 100644 index 00000000..b1dd778f --- /dev/null +++ b/backend/tests/fixtures/calibration/PROVENANCE.md @@ -0,0 +1,3 @@ +# Calibration fixture provenance + +These palettes are synthetic examples created for parser and UI verification. Their field shapes follow the package's documented input contract; they are not copied from real user projects or hardware measurements. diff --git a/backend/tests/fixtures/calibration/ditherforge-sample.txt b/backend/tests/fixtures/calibration/ditherforge-sample.txt new file mode 100644 index 00000000..9a76b802 --- /dev/null +++ b/backend/tests/fixtures/calibration/ditherforge-sample.txt @@ -0,0 +1,3 @@ +#E94B35 td=0.8 Vermilion +#2878B8 td Blue +#F2C14E Ochre td=1.1 diff --git a/backend/tests/fixtures/calibration/kromacut-v2.json b/backend/tests/fixtures/calibration/kromacut-v2.json new file mode 100644 index 00000000..2b393cd0 --- /dev/null +++ b/backend/tests/fixtures/calibration/kromacut-v2.json @@ -0,0 +1 @@ +{"version":2,"palette":[{"label":"Vermilion","color":"#E94B35","td":0.8},{"label":"Blue","color":"#2878B8"}]} diff --git a/backend/tests/fixtures/calibration/kromacut-v3-unknown.json b/backend/tests/fixtures/calibration/kromacut-v3-unknown.json new file mode 100644 index 00000000..94d2c057 --- /dev/null +++ b/backend/tests/fixtures/calibration/kromacut-v3-unknown.json @@ -0,0 +1 @@ +{"version":3,"palette":[{"label":"Future color","color":"#123456"}]} diff --git a/backend/tests/fixtures/creative/__init__.py b/backend/tests/fixtures/creative/__init__.py new file mode 100644 index 00000000..915afb46 --- /dev/null +++ b/backend/tests/fixtures/creative/__init__.py @@ -0,0 +1 @@ +"""In-memory creative project test fixtures; no captured projects are stored.""" diff --git a/backend/tests/fixtures/creative/zip_factory.py b/backend/tests/fixtures/creative/zip_factory.py new file mode 100644 index 00000000..1e492aaf --- /dev/null +++ b/backend/tests/fixtures/creative/zip_factory.py @@ -0,0 +1,29 @@ +"""Small synthetic 3MF ZIP builder used by creative reader contract tests.""" +from __future__ import annotations + +import io +import json +import zipfile + +CORE = "http://schemas.microsoft.com/3dmanufacturing/core/2015/02" + + +def model_xml(*, namespace: str = CORE) -> bytes: + return f''.encode() + + +def make_3mf(*, settings: dict | None = None, members: dict[str, bytes] | None = None, + model: bytes | None = None, duplicate: tuple[str, bytes, bytes] | None = None, + compression: int = zipfile.ZIP_DEFLATED) -> bytes: + output = io.BytesIO() + with zipfile.ZipFile(output, "w", compression=compression) as archive: + archive.writestr("3D/3dmodel.model", model if model is not None else model_xml()) + if settings is not None: + archive.writestr("Metadata/project_settings.config", json.dumps(settings).encode()) + for name, value in (members or {}).items(): + archive.writestr(name, value) + if duplicate is not None: + name, first, second = duplicate + archive.writestr(name, first) + archive.writestr(name, second) + return output.getvalue() diff --git a/backend/tests/fixtures/monitoring_context/PROVENANCE.md b/backend/tests/fixtures/monitoring_context/PROVENANCE.md new file mode 100644 index 00000000..30d9b4a3 --- /dev/null +++ b/backend/tests/fixtures/monitoring_context/PROVENANCE.md @@ -0,0 +1 @@ +Synthetic fixture provenance: Moonraker response fields are documentation-derived; all values and filenames are invented. No live printer or host was used. diff --git a/backend/tests/fixtures/passport/PROVENANCE.md b/backend/tests/fixtures/passport/PROVENANCE.md new file mode 100644 index 00000000..6ea27d26 --- /dev/null +++ b/backend/tests/fixtures/passport/PROVENANCE.md @@ -0,0 +1,12 @@ +# Passport fixtures: provenance + +Synthetic and hand-written. None of these files is a capture from a real printer, a real +project, a real slicer run or a real third-party tool. Evidence label: **fixture-tested**. + +| File | What it is | +|---|---| +| `recipes.json` | Three hand-written `virtual_recipe/1` documents: one usable, one with a schema version this build does not know (reported lost), one that references itself (kept, `execution_supported=false`). Shape follows `snapstudio_core/foundation/recipes.py`. | +| `mappings.json` | Four hand-written material mappings: one complete, one carrying a field that is not retained, one with an invalid slot reference, one duplicate. Provider and spool ids are made up. | + +The artifact files used by the tests (original, prepared, sliced stand-ins) are small byte +strings written into a temporary directory by the tests themselves. They are not 3MF or G-code. diff --git a/backend/tests/fixtures/passport/mappings.json b/backend/tests/fixtures/passport/mappings.json new file mode 100644 index 00000000..6cff51d4 --- /dev/null +++ b/backend/tests/fixtures/passport/mappings.json @@ -0,0 +1,7 @@ +[ + {"slot_ref": "slot:0", "material": "PLA", "colour": "#ff8800", + "spool": {"provider_instance_id": "synthetic-provider", "provider_spool_id": "42"}}, + {"slot_ref": "project:1:2", "material": "PETG", "colour": "#00AAFF", "note": "not a retained field"}, + {"slot_ref": "banana", "material": "PLA"}, + {"slot_ref": "slot:0", "material": "ABS"} +] diff --git a/backend/tests/fixtures/passport/recipes.json b/backend/tests/fixtures/passport/recipes.json new file mode 100644 index 00000000..0bd0ca91 --- /dev/null +++ b/backend/tests/fixtures/passport/recipes.json @@ -0,0 +1,12 @@ +[ + {"schema_version": "virtual_recipe/1", "recipe_id": "synthetic:mix-a", + "generator": {"name": "synthetic-generator", "version": "1"}, "kind": "custom", + "components": [{"slot_ref": "slot:0", "ratio": 0.6, "ratio_kind": "mass"}, + {"slot_ref": "slot:1", "ratio": 0.4, "ratio_kind": "mass"}], + "simulated": true, "execution_supported": null}, + {"schema_version": "virtual_recipe/9", "recipe_id": "synthetic:from-the-future", + "generator": {"name": "synthetic-generator", "version": "9"}, "kind": "custom", "components": []}, + {"schema_version": "virtual_recipe/1", "recipe_id": "synthetic:loops", + "generator": {"name": "synthetic-generator", "version": "1"}, "kind": "custom", + "components": [{"slot_ref": "recipe:synthetic:loops", "ratio": 1, "ratio_kind": "mass"}]} +] diff --git a/backend/tests/fixtures/variants/PROVENANCE.md b/backend/tests/fixtures/variants/PROVENANCE.md new file mode 100644 index 00000000..cf2f4754 --- /dev/null +++ b/backend/tests/fixtures/variants/PROVENANCE.md @@ -0,0 +1,4 @@ +# Variants fixtures + +All files in this directory are synthetic, authored for package tests. They do not +represent a real printer, user, model, or slicer run. Evidence label: fixture-tested. diff --git a/backend/tests/fixtures/variants/left.gcode b/backend/tests/fixtures/variants/left.gcode new file mode 100644 index 00000000..4ac9823a --- /dev/null +++ b/backend/tests/fixtures/variants/left.gcode @@ -0,0 +1,12 @@ +; generated by synthetic fixture +; layer_height = 0.20 +; filament used [g] = 2.5, 1.25 +; estimated printing time (normal mode) = 1h 2m +; total wipe tower used = 3.4 +; EXECUTABLE_BLOCK_START +T0 +G1 X1 +T1 +G1 X2 +T0 +G1 X3 diff --git a/backend/tests/fixtures/variants/right.gcode b/backend/tests/fixtures/variants/right.gcode new file mode 100644 index 00000000..9fb6ee1e --- /dev/null +++ b/backend/tests/fixtures/variants/right.gcode @@ -0,0 +1,8 @@ +; generated by synthetic fixture +; layer_height = 0.24 +; filament used [g] = 1.5, 4.0 +; EXECUTABLE_BLOCK_START +T0 +G1 X1 +T1 +G1 X2 diff --git a/backend/tests/fixtures/variants/unknown.gcode b/backend/tests/fixtures/variants/unknown.gcode new file mode 100644 index 00000000..cedadf3c --- /dev/null +++ b/backend/tests/fixtures/variants/unknown.gcode @@ -0,0 +1,4 @@ +; generated by synthetic fixture +; EXECUTABLE_BLOCK_START +G1 X0 +G1 Y1 diff --git a/backend/tests/monitoring_context/test_monitoring.py b/backend/tests/monitoring_context/test_monitoring.py new file mode 100644 index 00000000..826633eb --- /dev/null +++ b/backend/tests/monitoring_context/test_monitoring.py @@ -0,0 +1,86 @@ +"""P8 monitoring tests 3-7 against an in-process 127.0.0.1 mock. Evidence label: mock-service-tested.""" +import json + +import pytest + +from snapstudio_core import monitoring_context as mc +from tests.mocks.hosts.host_mocks import MockHost + +STATUS = {"result": {"status": {"print_stats": {"filename": "demo.3mf", "state": "printing"}}}} +HISTORY = {"result": {"jobs": [{"filename": "demo.3mf", "status": "completed"}]}} + + +@pytest.fixture +def host(): + mock = MockHost({"/printer/objects/query": (200, STATUS), "/server/history/list": (200, HISTORY)}).start() + yield mock + mock.stop() + + +def port_of(mock): + return mock.server_address[1] + + +def test_4_reads_are_two_bounded_gets_and_zero_control_requests(host): + state = mc.read("127.0.0.1", port_of(host)) + last = mc.read_history("127.0.0.1", port_of(host)) + assert state["print_state"] == "printing" and last["last_job"]["outcome"] == "completed" + assert sorted(host.paths()) == ["/printer/objects/query", "/server/history/list"] + assert {r["method"] for r in host.requests} == {"GET"} + control = ("/printer/print/", "/printer/emergency_stop", "/printer/gcode/script", "/printer/restart") + assert not [p for p in host.paths() if p.startswith(control)] + + +def test_7_filename_match_is_unverified_and_never_claims_printing_this_file(host): + result = mc.read("127.0.0.1", port_of(host)) + assert result["association"] == "unverified" + assert "printing this file" not in json.dumps(result).lower() + assert mc.read_history("127.0.0.1", port_of(host))["association"] == "unverified" + + +def test_unsupported_fields_remain_unknown(): + mock = MockHost({"/printer/objects/query": (200, {"result": {"status": {}}})}).start() + try: + result = mc.read("127.0.0.1", port_of(mock)) + finally: + mock.stop() + assert result["print_state"] == "unknown" and result["association"] == "unknown" + assert result["filename"] is None + + +def test_6_camera_is_unknown_not_inferred(host): + assert mc.read("127.0.0.1", port_of(host))["camera"]["state"] == "unknown" + + +def test_invalid_reply_is_unavailable_not_an_exception(): + mock = MockHost({"/printer/objects/query": (200, ["not", "an", "object"])}).start() + try: + result = mc.read("127.0.0.1", port_of(mock)) + finally: + mock.stop() + assert result["status"] == "unavailable" and result["camera"]["state"] == "unknown" + + +def test_5_missed_events_reconcile_from_current_status(host): + # A caller last saw "paused"; the printer is now "printing". The transition path is unknown. + current = mc.read("127.0.0.1", port_of(host)) + rec = mc.reconcile("paused", current) + assert rec["state"] == "printing" and rec["reconciled_from_status"] is True + assert rec["events"] == [{"kind": "state_changed", "from": "paused", "to": "printing", "source": "poll"}] + assert rec["source"] == "polling" and rec["intermediate_states_known"] is False + assert mc.reconcile("printing", current)["events"] == [] + assert mc.reconcile(None, current)["events"] == [] + assert mc.reconcile("printing", {"status": "unavailable"})["state"] == "unknown" + + +def test_connection_failure_is_a_refusal_not_a_hang(): + from snapstudio_core.foundation import netguard + with pytest.raises(netguard.NetguardRefused): + mc.read("8.8.8.8", 7125) # public address: refused before any dial + + +def test_invalid_port_rejected(): + with pytest.raises(ValueError): + mc.read("127.0.0.1", 0) + with pytest.raises(ValueError): + mc.read("127.0.0.1", True) diff --git a/backend/tests/passport/__init__.py b/backend/tests/passport/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/tests/passport/synthetic_client.py b/backend/tests/passport/synthetic_client.py new file mode 100644 index 00000000..7a865f33 --- /dev/null +++ b/backend/tests/passport/synthetic_client.py @@ -0,0 +1,82 @@ +"""Two synthetic clients of the `u1convert` command line. Neither is a real third-party program. + +`CliClient` behaves the way an external program that shells out to Studio does: it runs the +command, parses stdout, and treats anything it cannot parse as a failure. `ManifestClient` +keeps passports nested inside its own manifest file and round-trips them through Studio. +""" +from __future__ import annotations + +import json +import os +import subprocess +import sys + +# The passport group is attached here, in the child interpreter, until `u1convert/cli.py` +# registers it itself (a two-line change that is outside this package's file scope). +_WIRED = ("from u1convert.cli import cli; from snapstudio_core.passport_cli import passport; " + "cli.add_command(passport); cli()") + + +def run_cli(*args, timeout=120): + exe = os.environ.get("SNAPSTUDIO_CLI_EXE") + cmd = [exe] if exe else [sys.executable, "-c", _WIRED] + return subprocess.run([*cmd, *args], capture_output=True, text=True, timeout=timeout) + + +def parse_strict(stdout: str): + """Studio's contract: the whole of stdout is one JSON object. None when it is not.""" + try: + value = json.loads(stdout) + except ValueError: + return None + return value if isinstance(value, dict) else None + + +def parse_last_brace(stdout: str): + """What a naive caller does: take the text from the last "{" to the last "}". NOT guaranteed by Studio.""" + text = stdout.strip() + start, end = text.rfind("{"), text.rfind("}") + if not 0 <= start < end: + return None + try: + value = json.loads(text[start:end + 1]) + except ValueError: + return None + return value if isinstance(value, dict) else None + + +class CliClient: + """Asks Studio about a file. Absent or invalid JSON is a failure, never a success.""" + + def doctor(self, path: str) -> dict: + proc = run_cli("doctor", path, "--json") + data = parse_strict(proc.stdout) + if data is None: + return {"ok": False, "reason": f"no usable JSON (exit {proc.returncode})", "exit": proc.returncode} + return {"ok": True, "exit": proc.returncode, "verdict": data.get("verdict"), "score": data.get("score"), + "keys": sorted(data)} + + def passport_verify(self, passport_file: str, base_dir: str) -> dict: + proc = run_cli("passport", "verify", passport_file, "--base-dir", base_dir) + data = parse_strict(proc.stdout) + if data is None: + return {"ok": False, "reason": "no usable JSON", "exit": proc.returncode} + return {"ok": data.get("ok") is True and proc.returncode == 0, "exit": proc.returncode, "json": data} + + +class ManifestClient: + """A generic tool whose manifest nests Studio passports: {"manifest": 1, "jobs": [{"name", "passport"}]}.""" + + def pack(self, jobs: dict) -> dict: + return {"manifest": 1, "jobs": [{"name": name, "passport": doc} for name, doc in sorted(jobs.items())]} + + def unpack_through_studio(self, manifest: dict, workdir: str) -> list: + out = [] + for job in manifest["jobs"]: + target = os.path.join(workdir, f"{job['name']}.passport.json") + with open(target, "w", encoding="utf-8") as handle: + json.dump(job["passport"], handle) + proc = run_cli("passport", "import", target) + data = parse_strict(proc.stdout) + out.append({"name": job["name"], "exit": proc.returncode, "json": data}) + return out diff --git a/backend/tests/passport/test_export_publication.py b/backend/tests/passport/test_export_publication.py new file mode 100644 index 00000000..4d77acf6 --- /dev/null +++ b/backend/tests/passport/test_export_publication.py @@ -0,0 +1,284 @@ +"""R4-F1: `passport export -o` must publish into the directory it validated, even if that directory +(or an ancestor) is swapped between the check and the write. Evidence label: fixture-tested. + +The swap is simulated deterministically. Both the old write path (`tempfile.mkstemp` in the validated +directory) and the new one (`safe_publish._after_pin`, after the directory is pinned) trigger it exactly +once, immediately before the first file is created. The directory swap is a rename of the validated +directory plus a rename of a decoy into its place; on Windows the new code's pinned handle makes the +first rename fail, which is the protection under test. +""" +from __future__ import annotations + +import json +import os +import tempfile + +import pytest +from click.testing import CliRunner + +from snapstudio_core import passport_cli + +try: + from snapstudio_core import safe_publish +except ImportError: # the pre-fix tree: let the behavioural assertion fail instead + safe_publish = None + + +def _run(*args): + return CliRunner().invoke(passport_cli.passport, list(args)) + + +@pytest.fixture +def layout(tmp_path): + victim = tmp_path / "victim" + decoy = tmp_path / "decoy" + victim.mkdir() + decoy.mkdir() + (victim / "orig.marker").write_text("victim") + (decoy / "decoy.marker").write_text("decoy") + source = tmp_path / "part.3mf" + source.write_bytes(b"synthetic original") + return tmp_path, victim, decoy, source + + +def _install_swap(monkeypatch, victim, decoy): + moved = victim.with_name("victim_moved") + state = {"armed": True, "swapped": False} + + def swap(): + if not state["armed"]: + return + state["armed"] = False + try: + os.rename(victim, moved) + os.rename(decoy, victim) + state["swapped"] = True + except OSError: + pass # a pinned directory refuses to be renamed (Windows) + + real_mkstemp = tempfile.mkstemp + + def mkstemp(*args, **kwargs): + swap() + return real_mkstemp(*args, **kwargs) + + monkeypatch.setattr(tempfile, "mkstemp", mkstemp) + if safe_publish is not None: + monkeypatch.setattr(safe_publish, "_after_pin", swap) + return state, moved + + +def test_a_directory_swapped_after_validation_does_not_redirect_the_passport(layout, monkeypatch): + tmp_path, victim, decoy, source = layout + state, moved = _install_swap(monkeypatch, victim, decoy) + out = victim / "job.passport.json" + + result = _run("export", "--original", str(source), "-o", str(out)) + + assert result.exit_code == 0, result.output + assert json.loads(result.output)["written"] == "job.passport.json" + holders = [d for d in (victim, moved) if (d / "job.passport.json").exists()] + assert len(holders) == 1 + assert (holders[0] / "orig.marker").exists(), "the passport landed in the swapped-in directory" + assert not [p for p in (victim, moved) if (p / "decoy.marker").exists() and (p / "job.passport.json").exists()] + assert not list(holders[0].glob("*.tmp")) and not list(holders[0].glob(".*.tmp")) + + +def test_export_writes_a_new_file_and_refuses_an_existing_one(tmp_path): + source = tmp_path / "part.3mf" + source.write_bytes(b"x") + out = tmp_path / "p.json" + first = _run("export", "--original", str(source), "-o", str(out)) + assert first.exit_code == 0 and json.loads(out.read_text(encoding="utf-8"))["passport_id"] + before = out.read_bytes() + second = _run("export", "--original", str(source), "-o", str(out)) + assert second.exit_code == 1 and json.loads(second.output)["error"]["code"] == "output_exists" + assert out.read_bytes() == before + assert sorted(p.name for p in tmp_path.iterdir()) == ["p.json", "part.3mf"] + + +@pytest.mark.skipif(safe_publish is None, reason="needs safe_publish") +def test_publish_new_file_never_replaces_and_leaves_no_temporary(tmp_path): + target = tmp_path / "a.json" + safe_publish.publish_new_file(str(target), b"one") + with pytest.raises(FileExistsError): + safe_publish.publish_new_file(str(target), b"two") + assert target.read_bytes() == b"one" + assert [p.name for p in tmp_path.iterdir()] == ["a.json"] + + +@pytest.mark.skipif(safe_publish is None, reason="needs safe_publish") +@pytest.mark.parametrize("name", ["", "..", "a/b"]) +def test_publish_refuses_a_name_that_is_not_a_plain_file_name(tmp_path, name): + with pytest.raises(safe_publish.UnsafeOutput): + safe_publish._check_name(name) + + +@pytest.mark.skipif(safe_publish is None, reason="needs safe_publish") +def test_a_backslash_is_a_separator_only_on_windows(): + if os.name == "nt": + with pytest.raises(safe_publish.UnsafeOutput): + safe_publish._check_name("a\\b") + else: + safe_publish._check_name("a\\b") # an ordinary POSIX file name + + +@pytest.mark.skipif(safe_publish is None, reason="needs safe_publish") +def test_a_directory_that_resolves_to_a_network_location_is_refused(tmp_path, monkeypatch): + monkeypatch.setattr(safe_publish.localpath, "_reaches_network", lambda path, depth=0: True) + with pytest.raises(safe_publish.UnsafeOutput): + safe_publish.publish_new_file(str(tmp_path / "a.json"), b"x") + assert list(tmp_path.iterdir()) == [] + + +@pytest.mark.skipif(safe_publish is None, reason="needs safe_publish") +def test_a_missing_output_directory_is_refused_not_created(tmp_path): + with pytest.raises(safe_publish.UnsafeOutput): + safe_publish.publish_new_file(str(tmp_path / "nope" / "a.json"), b"x") + assert not (tmp_path / "nope").exists() + + +# ------------------------------------------------------------ in-place link on an empty directory + +def _make_junction_in_place(directory, target) -> bool: + """Turn the EMPTY `directory` into a junction to `target` without renaming it (Windows only).""" + import ctypes + import struct + from ctypes import wintypes + + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) + kernel32.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID, + wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE] + kernel32.CreateFileW.restype = wintypes.HANDLE + kernel32.DeviceIoControl.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.LPVOID, wintypes.DWORD, + wintypes.LPVOID, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD), + wintypes.LPVOID] + kernel32.DeviceIoControl.restype = wintypes.BOOL + h = kernel32.CreateFileW(str(directory), 0x40000000, 0x7, None, 3, 0x02000000 | 0x00200000, None) + if h in (None, wintypes.HANDLE(-1).value): + return False + try: + substitute = ("\\??\\" + str(target)).encode("utf-16-le") + printable = str(target).encode("utf-16-le") + path_buffer = substitute + b"\0\0" + printable + b"\0\0" + header = struct.pack("= {"identical_bytes"} + multi = build_passport([{"path": files["original.3mf"], "role": "original"}, + {"path": files["prepared.3mf"], "role": "settings"}, + {"path": files["sliced.gcode"], "role": "sliced", "parents": [0, 1]}]).passport + sliced = multi.derivations[-1] + assert sorted(i["kind"] for i in sliced.inputs) == ["artifact", "settings"] + + +@pytest.mark.parametrize("entries, code", [ + ([], "no_entries"), ("x", "no_entries"), ([{"path": "x"}], "bad_role"), ([5], "bad_entry"), + ([{"path": "definitely-not-here.bin", "role": "original"}], "file_missing"), +]) +def test_build_refuses_bad_input_without_raising(entries, code): + result = build_passport(entries) + assert result.passport is None and result.diagnostics[0].code == code + + +def test_build_refuses_bad_parents(files): + for parents in ([1], [0, 0], [-1], ["a"], "0", [True]): + entries = [{"path": files["original.3mf"], "role": "original"}, + {"path": files["prepared.3mf"], "role": "prepared", "parents": parents}] + assert build_passport(entries).diagnostics[0].code == "bad_parents", parents + + +def test_2_changed_bytes_invalidate_the_file_and_everything_derived_from_it(files): + passport = build_passport(chain_entries(files)).passport + ok = verify_passport(passport, paths={"original.3mf": files["original.3mf"], + "prepared.3mf": files["prepared.3mf"], "sliced.gcode": files["sliced.gcode"]}) + assert ok["summary"]["all_intact"] is True + pathlib.Path(files["original.3mf"]).write_bytes(b"synthetic original bytes") # same bytes: still intact + assert verify_passport(passport, base_dir=str(pathlib.Path(files["original.3mf"]).parent))["summary"]["all_intact"] + # a same-size replacement is a change + pathlib.Path(files["original.3mf"]).write_bytes(b"synthetic original BYTES") + bad = verify_passport(passport, base_dir=str(pathlib.Path(files["original.3mf"]).parent)) + rows = {r["name"]: r for r in bad["artifacts"]} + assert rows["original.3mf"]["bytes"] == "changed" and rows["original.3mf"]["chain"] == "invalidated" + assert rows["prepared.3mf"]["bytes"] == "same" and rows["prepared.3mf"]["chain"] == "invalidated" + assert {"code": "ancestor_changed", "artifact": "original.3mf"} in rows["sliced.gcode"]["reasons"] + assert bad["summary"]["all_intact"] is False + status = {d["output"]: d["status"] for d in bad["derivations"]} + assert status == {"prepared.3mf": "invalidated", "sliced.gcode": "intact"} # a link is judged on its own bytes + + +def test_2_missing_or_unlocated_files_are_unverified_never_intact(files): + passport = build_passport(chain_entries(files)).passport + nothing = verify_passport(passport) # no base_dir, no paths + assert {r["chain"] for r in nothing["artifacts"]} == {"unverified"} + assert {r["bytes"] for r in nothing["artifacts"]} == {"unverified"} + assert nothing["summary"]["all_intact"] is False + pathlib.Path(files["prepared.3mf"]).unlink() + gone = verify_passport(passport, base_dir=str(pathlib.Path(files["original.3mf"]).parent)) + rows = {r["name"]: r for r in gone["artifacts"]} + assert rows["prepared.3mf"]["bytes"] == "missing" and rows["prepared.3mf"]["chain"] == "unverified" + assert rows["original.3mf"]["chain"] == "intact" + assert rows["sliced.gcode"]["chain"] == "unverified" # its parent cannot be checked + + +def test_2_a_name_that_is_not_a_file_name_is_never_joined_to_base_dir(files, tmp_path): + doc = build_passport(chain_entries(files)).passport.to_dict() + doc["artifacts"][0]["name"] = "..\\original.3mf" + imported = decode_passport(doc).value + report = verify_passport(imported, base_dir=str(tmp_path)) + assert report["artifacts"][0]["bytes"] == "unverified" + + +def test_3_recipes_and_mappings_are_retained_or_reported_lost(files): + recipes = json.loads((FIXTURES / "recipes.json").read_text(encoding="utf-8")) + mappings = json.loads((FIXTURES / "mappings.json").read_text(encoding="utf-8")) + result = build_passport(chain_entries(files), recipes=recipes, material_mappings=mappings) + passport = result.passport + assert [r.recipe_id for r in passport.recipes] == ["synthetic:mix-a", "synthetic:loops"] + loops = {r.recipe_id: r for r in passport.recipes}["synthetic:loops"] + assert loops.execution_supported is False # kept, never executable + assert [m["slot_ref"] for m in passport.material_mappings] == ["slot:0", "project:1:2"] + assert passport.material_mappings[0]["colour"] == "#FF8800" + reasons = {(l["kind"], l["id"]): l["reason"] for l in passport.losses} + assert reasons[("recipe", "synthetic:from-the-future")] == "unknown_schema_version" + assert reasons[("material_mapping", "project:1:2")] == "field_not_retained:note" + assert reasons[("material_mapping", "banana")] == "bad_slot_ref" + assert reasons[("material_mapping", "slot:0")] == "duplicate" + # the losses survive an export/import round trip and nothing new appears or disappears + again = decode_passport(json.loads(json.dumps(passport.to_dict()))).value + assert [r.recipe_id for r in again.recipes] == ["synthetic:mix-a", "synthetic:loops"] + assert again.material_mappings == passport.material_mappings + assert sorted(map(json.dumps, again.losses)) == sorted(map(json.dumps, passport.losses)) + assert again.passport_id == passport.passport_id + + +def test_3_unusable_recipe_and_mapping_lists_are_reported_not_raised(files): + result = build_passport(chain_entries(files), recipes="nope", material_mappings={"a": 1}) + kinds = {(l["kind"], l["reason"]) for l in result.passport.losses} + assert kinds == {("recipe", "not_a_list"), ("material_mapping", "not_a_list")} + + +def foreign_passport(files, *, claim="local"): + """A passport as another machine or a hand edit could have made it.""" + doc = build_passport(chain_entries(files)).passport.to_dict() + for d in doc["derivations"]: + d["witness"] = claim + d["declared_by"] = None if claim == "local" else "someone-else" + for a in doc["artifacts"]: + a["locator"] = {"path": "/mnt/vault/someone/secret/original.3mf", "mtime_ns": 1} + return doc + + +def test_4_imported_parentage_is_declared_and_a_local_claim_is_downgraded(files): + result = import_passport(foreign_passport(files)) + assert result["passport"] is not None + assert {p["witness"] for p in result["parentage"]} == {"declared"} + assert any(d["code"] == "witness_downgraded" for d in result["diagnostics"]) + assert all(a["locator"] is None for a in result["passport"]["artifacts"]) # a foreign path is dropped + assert "someone" not in json.dumps(result["passport"]) + assert {p["declared_mismatch"] for p in result["parentage"]} == {None} # not checked without files + + +def test_4_a_false_declared_parent_is_flagged_and_never_promoted(files, tmp_path): + doc = foreign_passport(files, claim="declared") + other = tmp_path / "elsewhere" + other.mkdir() + (other / "original.3mf").write_bytes(b"a different file with the same name") # exists locally, other sha + (other / "prepared.3mf").write_bytes(pathlib.Path(files["prepared.3mf"]).read_bytes()) + (other / "sliced.gcode").write_bytes(pathlib.Path(files["sliced.gcode"]).read_bytes()) + result = import_passport(doc, base_dir=str(other)) + by_output = {p["output"]: p for p in result["parentage"]} + assert by_output["prepared.3mf"]["declared_mismatch"] is True + assert by_output["sliced.gcode"]["declared_mismatch"] is False + assert {p["witness"] for p in result["parentage"]} == {"declared"} # never promoted to local + assert result["verification"]["summary"]["declared_mismatch"] == ["prepared.3mf"] + assert result["verification"]["summary"]["all_intact"] is False + + +def test_4_an_imported_derivation_id_is_never_trusted(files): + doc = foreign_passport(files, claim="declared") + doc["derivations"][0]["derivation_id"] = "drv:" + "0" * 40 + imported = decode_passport(doc).value + assert imported.derivations[0].derivation_id != "drv:" + "0" * 40 + assert imported.derivations[0].derivation_id == Derivation.compute_id( + imported.derivations[0].output, imported.derivations[0].inputs, imported.derivations[0].producer, + "declared", "someone-else") + + +def test_import_never_raises_on_hostile_documents(files): + base = build_passport(chain_entries(files)).passport.to_dict() + cases = [None, 5, "not json", "{", [], {}, {"schema_version": "passport/9"}, {"schema_version": "passport/1"}, + {**base, "artifacts": "x"}, {**base, "artifacts": [None, 3, {}]}, {**base, "derivations": [[]]}, + {**base, "artifacts": [base["artifacts"][0]] * 100}] + for raw in cases: + result = import_passport(raw) + assert result["passport"] is None or isinstance(result["passport"], dict) + assert import_passport({"schema_version": "passport/9"})["passport"] is None + assert import_passport({**base, "artifacts": [base["artifacts"][0]] * 100})["passport"] is None + + +def test_a_looping_declared_chain_is_cycle_safe(files): + doc = foreign_passport(files, claim="declared") + a, b = doc["artifacts"][0]["artifact_id"], doc["artifacts"][1]["artifact_id"] + loops = [] + for out, inp in ((a, b), (b, a)): + loops.append({"schema_version": "derivation/1", "output": out, "inputs": [{"kind": "artifact", "ref": inp}], + "producer": {"tool": "x", "version": None}, "witness": "declared", "declared_by": "y"}) + doc["derivations"] = loops + result = import_passport(doc, base_dir=str(pathlib.Path(files["original.3mf"]).parent)) + assert result["verification"] is not None # terminated; no hang or recursion error + + +def test_a_derivation_that_points_outside_the_passport_is_dropped_and_listed(files): + doc = foreign_passport(files, claim="declared") + doc["derivations"][0]["inputs"][0]["ref"] = "sha256:" + "a" * 64 + ":1" + result = import_passport(doc) + assert ("derivation", "dangling_ref") in {(l["kind"], l["reason"]) for l in result["losses"]} + assert len(result["parentage"]) == 1 diff --git a/backend/tests/passport/test_passport_cli.py b/backend/tests/passport/test_passport_cli.py new file mode 100644 index 00000000..686d7958 --- /dev/null +++ b/backend/tests/passport/test_passport_cli.py @@ -0,0 +1,189 @@ +"""The connector contract through the real command line (plan P7 tests 5-9). Evidence label: fixture-tested. + +Every case runs the CLI in a subprocess. The passport group is attached by the child interpreter +(see synthetic_client._WIRED); `test_u1convert_registers_passport` pins the registration itself. +""" +from __future__ import annotations + +import hashlib +import json +import pathlib + +import pytest + +from tests.passport.synthetic_client import CliClient, ManifestClient, parse_last_brace, parse_strict, run_cli +from tests.test_external_cli_contract import CUBE + +FIXTURES = pathlib.Path(__file__).resolve().parents[1] / "fixtures" / "passport" + + +def sha(path) -> str: + return hashlib.sha256(pathlib.Path(path).read_bytes()).hexdigest() + + +@pytest.fixture +def cube(tmp_path): + p = tmp_path / "authored_cube.stl" + p.write_text(CUBE) + return str(p) + + +@pytest.fixture +def job(tmp_path): + """Original, prepared and sliced stand-ins whose names contain braces, on purpose.""" + d = tmp_path / "job" + d.mkdir() + names = ("part{a}.3mf", "part{a}_u1}.3mf", "part{a}{.gcode") + for i, name in enumerate(names): + (d / name).write_bytes(f"synthetic file {i}".encode()) + return d, names + + +def export(d, names, *extra): + out = d / "job.passport.json" + proc = run_cli("passport", "export", "--original", str(d / names[0]), "--prepared", str(d / names[1]), + "--sliced", str(d / names[2]), "-o", str(out), *extra) + return proc, out + + +def test_5_doctor_json_is_exactly_one_object_with_a_score_key(cube): + proc = run_cli("doctor", cube, "--json") + data = parse_strict(proc.stdout) + assert data is not None, proc.stdout # json.loads of ALL of stdout, nothing after it + assert "score" in data and "verdict" in data + assert proc.stdout.strip().endswith("}") and proc.stdout.count("\n{") == 0 + client = CliClient().doctor(cube) + assert client["ok"] and "score" in client["keys"] and client["exit"] in (0, 1) + + +def test_5_last_brace_parsing_is_not_guaranteed_by_studio(job): + d, names = job + proc, out = export(d, names) + assert proc.returncode == 0, proc.stderr + verify = run_cli("passport", "verify", str(out), "--base-dir", str(d)) + assert verify.returncode == 0 + strict = parse_strict(verify.stdout) + assert strict is not None and strict["ok"] is True + names_in_output = [row["name"] for row in strict["verification"]["artifacts"]] + assert names_in_output == list(names) # string values do contain "{" and "}" + # A caller that cuts at the last "{" gets a fragment inside a string value, or nothing. Studio's + # contract is "one object, nothing else on stdout", so the strict parse is the only supported one. + assert parse_last_brace(verify.stdout) != strict + + +def test_6_exit_codes_are_0_or_1_with_json(cube, job): + d, names = job + ok = run_cli("passport", "verify", str(export(d, names)[1]), "--base-dir", str(d)) + assert ok.returncode == 0 and parse_strict(ok.stdout)["ok"] is True + (d / names[0]).write_bytes(b"changed on disk") + bad = run_cli("passport", "verify", str(d / "job.passport.json"), "--base-dir", str(d)) + assert bad.returncode == 1 + data = parse_strict(bad.stdout) + assert data["ok"] is False and data["verification"]["summary"]["invalidated"] + garbage = d / "garbage.json" + garbage.write_text("{ not json") + handled = run_cli("passport", "import", str(garbage)) + assert handled.returncode == 1 and parse_strict(handled.stdout)["error"]["code"] == "malformed_json" + unusable = d / "future.json" + unusable.write_text(json.dumps({"schema_version": "passport/9"})) + refused = run_cli("passport", "verify", str(unusable)) + assert refused.returncode == 1 and parse_strict(refused.stdout)["error"]["code"] == "unusable_passport" + nofiles = run_cli("passport", "export") + assert nofiles.returncode == 1 and parse_strict(nofiles.stdout)["error"]["code"] == "no_files" + + +def test_6_a_missing_file_is_click_exit_2_with_no_json_and_a_client_calls_it_a_failure(tmp_path): + missing = str(tmp_path / "nope.3mf") + for args in (("doctor", missing, "--json"), ("passport", "verify", missing), ("passport", "import", missing), + ("passport", "export", "--original", missing)): + proc = run_cli(*args) + assert proc.returncode == 2, args + assert proc.stdout.strip() == "", (args, proc.stdout) # no JSON at all + assert parse_strict(proc.stdout) is None + result = CliClient().doctor(missing) + assert result["ok"] is False and result["exit"] == 2 # absent JSON is failure, never success + + +def test_6_invalid_json_is_a_failure_not_a_success(): + for text in ("", "not json", "[1, 2]", "{ truncated", '{"a": 1} trailing'): + assert parse_strict(text) is None + + +def test_7_repair_writes_a_new_file_and_leaves_the_original_unchanged(cube, tmp_path): + before = sha(cube) + out = tmp_path / "prepared_u1.3mf" + proc = run_cli("repair", cube, "--mode", "u1", "-o", str(out)) + assert out.exists() and out.stat().st_size > 0, (proc.returncode, proc.stdout[-200:], proc.stderr[-200:]) + assert sha(cube) == before + # the output of `repair` goes straight into a passport as the prepared step + exported = tmp_path / "from_repair.passport.json" + made = run_cli("passport", "export", "--original", cube, "--prepared", str(out), "-o", str(exported)) + assert made.returncode == 0 and exported.exists() + assert sha(cube) == before + assert run_cli("passport", "verify", str(exported), "--base-dir", str(tmp_path)).returncode == 0 + + +def test_8_export_import_verify_round_trip_and_refusals(job): + d, names = job + before = {n: sha(d / n) for n in names} + proc, out = export(d, names) + data = parse_strict(proc.stdout) + assert proc.returncode == 0 and data["ok"] and data["written"] == "job.passport.json" + assert json.loads(out.read_text(encoding="utf-8"))["passport_id"] == data["passport_id"] + assert str(d) not in out.read_text(encoding="utf-8") # no local path in the file + imported = parse_strict(run_cli("passport", "import", str(out), "--base-dir", str(d)).stdout) + assert imported["ok"] and imported["passport"]["passport_id"] == data["passport_id"] + assert {p["witness"] for p in imported["parentage"]} == {"declared"} + assert imported["verification"]["summary"]["all_intact"] is True + again = run_cli("passport", "export", "--original", str(d / names[0]), "-o", str(out)) + assert again.returncode == 1 and parse_strict(again.stdout)["error"]["code"] == "output_exists" + clobber = run_cli("passport", "export", "--original", str(d / names[0]), "-o", str(d / names[0])) + assert clobber.returncode == 1 and parse_strict(clobber.stdout)["error"]["code"] == "output_is_input" + assert {n: sha(d / n) for n in names} == before # inputs untouched + located = run_cli("passport", "verify", str(out), "--path", f"{names[0]}={d / names[0]}") + assert parse_strict(located.stdout)["verification"]["summary"]["unverified"] # others not located + + +def test_u1convert_registers_passport(): + from u1convert.cli import cli + if "passport" not in cli.commands: + pytest.skip("u1convert/cli.py does not register the passport group yet (two-line change, out of this package's scope)") + proc = run_cli("passport", "--help") + assert proc.returncode == 0 and "export" in proc.stdout + + +def test_9_a_generic_manifest_client_round_trips_nested_passports(job, tmp_path): + d, names = job + recipes = tmp_path / "recipes.json" + recipes.write_text((FIXTURES / "recipes.json").read_text(encoding="utf-8"), encoding="utf-8") + mappings = FIXTURES / "mappings.json" + out = d / "with_recipes.passport.json" + proc = run_cli("passport", "export", "--original", str(d / names[0]), "--prepared", str(d / names[1]), + "--recipes", str(recipes), "--mappings", str(mappings), "-o", str(out)) + exported = parse_strict(proc.stdout) + assert proc.returncode == 0, proc.stdout + client = ManifestClient() + manifest = client.pack({"job-b": json.loads(out.read_text(encoding="utf-8")), "job-a": exported["passport"]}) + assert [j["name"] for j in manifest["jobs"]] == ["job-a", "job-b"] + results = client.unpack_through_studio(manifest, str(tmp_path)) + assert [r["exit"] for r in results] == [0, 0] + for r in results: + kept = r["json"]["passport"] + assert [x["recipe_id"] for x in kept["recipes"]] == ["synthetic:mix-a", "synthetic:loops"] + assert [m["slot_ref"] for m in kept["material_mappings"]] == ["slot:0", "project:1:2"] + reasons = {(l["kind"], l["id"]): l["reason"] for l in r["json"]["losses"]} + assert reasons[("recipe", "synthetic:from-the-future")] == "unknown_schema_version" # explicit loss + assert reasons[("material_mapping", "banana")] == "bad_slot_ref" + # a manifest passport whose recipes were damaged in transit: the loss is reported, not hidden + broken = json.loads(out.read_text(encoding="utf-8")) + broken["recipes"][0]["components"] = "x" + again = client.unpack_through_studio(client.pack({"job-c": broken}), str(tmp_path))[0] + assert again["exit"] == 0 + assert ("recipe", "synthetic:mix-a") in {(l["kind"], l["id"]) for l in again["json"]["losses"]} + + +def test_10_the_contract_document_states_the_rules(): + text = (pathlib.Path(__file__).resolve().parents[3] / "docs" / "internal" / "PASSPORT.md").read_text(encoding="utf-8") + for needle in ("exactly one top-level JSON object", "exit 2", "no JSON", "rfind", "declared", "exit 0", "exit 1", + "fixture-tested"): + assert needle in text, needle diff --git a/backend/tests/passport/test_passport_routes.py b/backend/tests/passport/test_passport_routes.py new file mode 100644 index 00000000..b509bcef --- /dev/null +++ b/backend/tests/passport/test_passport_routes.py @@ -0,0 +1,140 @@ +"""Route level (/passport/build, /passport/import, /passport/verify) and package guards. + +Plan common tests C3, C7 and guards G1, G2, G3 as they apply to this package. +Evidence label: fixture-tested. +""" +from __future__ import annotations + +import json +import pathlib +import re + +import pytest + +from snapstudio_api import ext_routes, routes_passport # noqa: F401 (registers the routes) + +ROOT = pathlib.Path(__file__).resolve().parents[2] +PACKAGE_FILES = (sorted((ROOT / "snapstudio_core" / "passport").glob("*.py")) + + [ROOT / "snapstudio_core" / "passport_cli.py", ROOT / "snapstudio_api" / "routes_passport.py"]) + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(path, data): + h = FakeHandler() + assert ext_routes.dispatch(h, path, data) is True + assert len(h.sent) == 1 + return h.sent[0] + + +@pytest.fixture +def files(tmp_path): + out = {} + for name, body in (("a.3mf", b"synthetic a"), ("b.3mf", b"synthetic b"), ("c.gcode", b"synthetic c")): + (tmp_path / name).write_bytes(body) + out[name] = str(tmp_path / name) + return out + + +def entries(files): + return [{"path": files["a.3mf"], "role": "original"}, {"path": files["b.3mf"], "role": "prepared"}, + {"path": files["c.gcode"], "role": "sliced"}] + + +def test_routes_are_registered_under_passport_only(): + names = {"/passport/build", "/passport/import", "/passport/verify"} + assert names <= set(ext_routes.ROUTES) + assert all(p.startswith("/passport") for p, fn in ext_routes.ROUTES.items() if fn.__module__.endswith("routes_passport")) + + +def test_build_then_verify_then_a_change_invalidates(files): + code, built = call("/passport/build", {"entries": entries(files)}) + assert code == 200 and built["passport"]["schema_version"] == "passport/1" + assert set(built["paths"].values()) == set(files.values()) + assert files["a.3mf"] not in json.dumps(built["passport"]) # paths stay out of the document + code, ok = call("/passport/verify", {"passport": built["passport"], "paths": built["paths"]}) + assert code == 200 and ok["verification"]["summary"]["all_intact"] is True + pathlib.Path(files["a.3mf"]).write_bytes(b"synthetic A") + code, bad = call("/passport/verify", {"passport": built["passport"], "paths": built["paths"]}) + summary = bad["verification"]["summary"] + assert summary["all_intact"] is False and summary["invalidated"] == ["a.3mf", "b.3mf", "c.gcode"] + + +def test_build_reports_a_missing_file_as_a_diagnostic_not_a_crash(files): + body = entries(files) + body[1]["path"] = body[1]["path"] + ".missing" + code, out = call("/passport/build", {"entries": body}) + assert code == 200 and out["passport"] is None and out["diagnostics"][0]["code"] == "file_missing" + + +def test_import_marks_everything_declared_and_flags_a_false_parent(files, tmp_path): + _c, built = call("/passport/build", {"entries": entries(files)}) + doc = json.loads(json.dumps(built["passport"])) + for d in doc["derivations"]: + d["witness"] = "local" + d["declared_by"] = None + other = tmp_path / "other" + other.mkdir() + (other / "a.3mf").write_bytes(b"not the same") + (other / "b.3mf").write_bytes(pathlib.Path(files["b.3mf"]).read_bytes()) + code, res = call("/passport/import", {"passport": doc, "base_dir": str(other)}) + assert code == 200 + assert {p["witness"] for p in res["parentage"]} == {"declared"} + assert [p["output"] for p in res["parentage"] if p["declared_mismatch"]] == ["b.3mf"] + + +@pytest.mark.parametrize("path, body", [ + ("/passport/build", {}), ("/passport/build", {"entries": []}), ("/passport/build", {"entries": [1]}), + ("/passport/build", {"entries": [{"path": 5, "role": "original"}]}), + ("/passport/verify", {}), ("/passport/verify", {"passport": []}), + ("/passport/verify", {"passport": {}, "paths": {"a": 1}}), + ("/passport/verify", {"passport": {}, "base_dir": "no-such-folder-here"}), + ("/passport/import", {"passport": "text"}), +]) +def test_bad_requests_are_400(path, body): + code, out = call(path, body) + assert code == 400 and out["error"] == "invalid" + + +def test_an_unusable_passport_is_a_200_with_no_verification(): + code, out = call("/passport/verify", {"passport": {"schema_version": "passport/9"}}) + assert code == 200 and out["verification"] is None and out["diagnostics"][0]["code"] == "unknown_schema_version" + + +def test_a_credential_in_the_request_is_not_echoed_anywhere(files): + code, out = call("/passport/build", {"entries": entries(files), "api_key": "SENTINEL-KEY-9f3a"}) + assert code == 200 and "SENTINEL-KEY-9f3a" not in json.dumps(out) + + +# -- package guards ---------------------------------------------------------------------- +def test_c3_every_fixture_dir_has_provenance(): + assert (ROOT / "tests" / "fixtures" / "passport" / "PROVENANCE.md").is_file() + + +def test_g1_no_network_or_printer_control_in_package_sources(): + banned = re.compile(r"urlopen|\brequests\b|http\.client|socket\.create_connection|moonraker\._get|" + r"moonraker\.(status|history|pause|resume|cancel|start)|netguard|import socket") + for path in PACKAGE_FILES: + text = path.read_text(encoding="utf-8") + assert not banned.search(text), path.name + + +def test_g3_no_head_count_from_slot_count(): + for path in PACKAGE_FILES: + text = path.read_text(encoding="utf-8") + assert "slot_count" not in text and "len(nozzles)" not in text, path.name + + +def test_c7_no_banned_claim_words_in_package_files(): + paths = PACKAGE_FILES + [ROOT.parent / "docs" / "internal" / "PASSPORT.md"] + paths += list((ROOT / "tests" / "fixtures" / "passport").iterdir()) + for path in paths: + text = path.read_text(encoding="utf-8").lower() + for word in ("simultaneous", "guaranteed", "100%"): + assert word not in text, (path.name, word) diff --git a/backend/tests/recovery/test_recovery.py b/backend/tests/recovery/test_recovery.py new file mode 100644 index 00000000..eadfb189 --- /dev/null +++ b/backend/tests/recovery/test_recovery.py @@ -0,0 +1,220 @@ +"""P8 recovery tests 1, 2, 8, 9, 10 against the real store under a throwaway data dir. + +Evidence label: fixture-tested. All names and bytes are synthetic. +""" +import hashlib +import json +import os +import subprocess +import sys + +import pytest + +from snapstudio_core import workspace_recovery as recovery +from snapstudio_core.foundation import artifacts, store +from snapstudio_core.foundation.observations import Observation + + +def ref_for(data: bytes, name: str): + return artifacts.ArtifactRef.create(hashlib.sha256(data).hexdigest(), len(data), "original", name) + + +def obs(): + return Observation("o1", {"kind": "file", "id": "part"}, "state", "printing", + {"provider_instance_id": "fixture", "kind": "live_query", "snapshot_id": "s1"}, + "2026-10-31T12:00:00Z", "2026-10-31T12:00:00Z", 60) + + +def test_1_10_restore_marks_historical_and_interrupts_jobs_without_replay(tmp_path): + d = str(tmp_path) + recovery.checkpoint({"workspace": {"name": "demo"}, "observations": [obs().to_dict()], "files": [], + "jobs": [{"job_id": "job-1", "state": "running"}, + {"job_id": "job-2", "state": "succeeded"}], + "checkpointed_at": "2026-10-31T12:00:00Z"}, data_dir=d) + restored = recovery.restore(data_dir=d) + assert restored["observations"][0]["historical"] is True + assert restored["observations"][0]["usable_for_loadout"] is False + assert restored["fresh_read_required"] is True + states = {j["job_id"]: j["state"] for j in restored["jobs"]} + assert states == {"job-1": "interrupted", "job-2": "succeeded"} + assert all(j["replayed"] is False for j in restored["jobs"]) + + +def test_2_missing_changed_and_size_equal_replacement_detected(tmp_path): + d = str(tmp_path) + same, changed, swapped, gone = (tmp_path / n for n in ("same.3mf", "changed.3mf", "swapped.3mf", "gone.3mf")) + for path in (same, changed, swapped, gone): + path.write_bytes(b"AAAAAAAA") + files = [{"name": p.name, "path": str(p), "artifact": ref_for(b"AAAAAAAA", p.name).to_dict()} + for p in (same, changed, swapped, gone)] + recovery.checkpoint({"files": files}, data_dir=d) + changed.write_bytes(b"AAAAAAAAXX") + swapped.write_bytes(b"BBBBBBBB") # same size, different bytes + gone.unlink() + restored = recovery.restore(data_dir=d) + state = {f["name"]: f["state"] for f in restored["files"]} + assert state == {"same.3mf": "same", "changed.3mf": "changed", "swapped.3mf": "changed", + "gone.3mf": "missing"} + assert all(f["association"] == "unverified" for f in restored["files"]) + assert "path" not in restored["files"][0] # the UI sees names only + + +def test_missing_checkpoint_is_reported_not_invented(tmp_path): + restored = recovery.restore(data_dir=str(tmp_path)) + assert restored["missing_checkpoint"] is True and restored["fresh_read_required"] is True + assert restored["observations"] == [] and restored["files"] == [] + + +def test_8_legacy_record_imported_one_way_and_both_mechanisms_coexist(tmp_path): + d = str(tmp_path) + legacy_doc = {"schema_version": "job/1", "revision": 3, "record": { + "savedAt": "2026-10-30T12:00:00Z", "original": {"name": "legacy.3mf", "path": "synthetic/legacy.3mf"}}} + legacy_path = os.path.join(store.store_dir(d), "job.json") + with open(legacy_path, "w", encoding="utf-8") as handle: + json.dump(legacy_doc, handle) + before = open(legacy_path, "rb").read() + # Legacy present, no recovery document yet: restore still shows it and writes nothing. + first = recovery.restore(data_dir=d) + assert first["legacy_job"]["files"][0]["name"] == "legacy.3mf" + assert first["legacy_job"]["files"][0]["association"] == "unverified" + assert not os.path.exists(os.path.join(store.store_dir(d), "recovery.json")) + # Both present: checkpoint imports it into recovery.json, the legacy file is untouched. + recovery.checkpoint({"workspace": {"name": "demo"}}, data_dir=d) + assert open(legacy_path, "rb").read() == before + assert recovery.restore(data_dir=d)["legacy_job"]["files"][0]["name"] == "legacy.3mf" + # A browser-held record sent with the checkpoint is imported too, with no legacy store file. + d2 = str(tmp_path / "other") + recovery.checkpoint({"jobs": [{"legacy_record": {"savedAt": "x", "original": {"name": "ls.3mf"}, + "sliced": {"name": "ls.gcode"}}}]}, data_dir=d2) + roles = [f["role"] for f in recovery.restore(data_dir=d2)["legacy_job"]["files"]] + assert roles == ["original", "sliced"] + + +def test_damaged_legacy_file_is_not_renamed_by_restore(tmp_path): + d = str(tmp_path) + legacy_path = os.path.join(store.store_dir(d), "job.json") + with open(legacy_path, "wb") as handle: + handle.write(b"{not json") + assert recovery.restore(data_dir=d)["legacy_job"] is None + assert os.path.exists(legacy_path) + + +def test_secret_like_fields_are_not_persisted(tmp_path): + d = str(tmp_path) + recovery.checkpoint({"workspace": {"name": "demo", "apiKey": "SENTINEL-KEY-9f3a", + "nested": {"Authorization": "SENTINEL-KEY-9f3a", "ok": 1}}}, data_dir=d) + raw = open(os.path.join(store.store_dir(d), "recovery.json"), encoding="utf-8").read() + assert "SENTINEL-KEY-9f3a" not in raw and '"ok":1' in raw + + +def test_newer_schema_document_is_left_untouched(tmp_path): + d = str(tmp_path) + path = os.path.join(store.store_dir(d), "recovery.json") + newer = json.dumps({"schema_version": "recovery/99", "revision": 5}).encode() + with open(path, "wb") as handle: + handle.write(newer) + with pytest.raises(store.StoreVersionError): + recovery.checkpoint({}, data_dir=d) + assert open(path, "rb").read() == newer + + +# ------------------------------------------------------------------ test 9: crash and lock safety +CHILD_CRASH = ( + "import os, sys\n" + "from snapstudio_core.foundation import store\n" + "from snapstudio_core import workspace_recovery as r\n" + "store._replace = lambda *a, **k: os._exit(7)\n" # die after the temp file is written, before the swap + "r.checkpoint({'workspace': {'name': 'NEW'}}, data_dir=sys.argv[1])\n" +) +CHILD_LOCKED = ( + "import sys\n" + "from snapstudio_core.foundation import store\n" + "from snapstudio_core import workspace_recovery as r\n" + "try:\n" + " r.checkpoint({'workspace': {'name': 'OTHER'}}, data_dir=sys.argv[1], lock_wait_s=0.3)\n" + "except store.StoreLockError:\n" + " sys.exit(42)\n" + "sys.exit(0)\n" +) + + +def run_child(code, data_dir): + env = dict(os.environ) + here = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + env["PYTHONPATH"] = here + os.pathsep + env.get("PYTHONPATH", "") + return subprocess.run([sys.executable, "-c", code, data_dir], env=env, timeout=60, + capture_output=True).returncode + + +def test_9_killed_writer_leaves_the_old_document_whole(tmp_path): + d = str(tmp_path) + recovery.checkpoint({"workspace": {"name": "OLD"}}, data_dir=d) + assert run_child(CHILD_CRASH, d) == 7 + restored = recovery.restore(data_dir=d) # parses cleanly: old, never partial + assert restored["workspace"] == {"name": "OLD"} and restored["revision"] == 1 + recovery.checkpoint({"workspace": {"name": "NEW"}}, data_dir=d) # and a later save still works + assert recovery.restore(data_dir=d)["workspace"] == {"name": "NEW"} + + +def test_9_second_process_writer_is_refused_by_the_lock(tmp_path): + d = str(tmp_path) + recovery.checkpoint({"workspace": {"name": "OLD"}}, data_dir=d) + with store.document_lock("recovery", data_dir=d): + assert run_child(CHILD_LOCKED, d) == 42 + assert recovery.restore(data_dir=d)["workspace"] == {"name": "OLD"} + + +def test_9_stale_revision_is_refused(tmp_path, monkeypatch): + d = str(tmp_path) + recovery.checkpoint({"workspace": {"name": "A"}}, data_dir=d) + real = store.write_json + + def racing(name, doc, *, expected_revision, **kw): # another writer lands between read and write + real(name, {"workspace": {"name": "OTHER"}}, expected_revision=expected_revision, **kw) + return real(name, doc, expected_revision=expected_revision, **kw) + + monkeypatch.setattr(recovery.store, "write_json", racing) + with pytest.raises(store.RevisionConflict): + recovery.checkpoint({"workspace": {"name": "B"}}, data_dir=d) + assert recovery.restore(data_dir=d)["workspace"] == {"name": "OTHER"} + + +def test_2_path_only_files_are_fingerprinted_at_checkpoint(tmp_path): + d = str(tmp_path) + part, absent = tmp_path / "part.3mf", tmp_path / "never-existed.3mf" + part.write_bytes(b"synthetic-bytes") + recovery.checkpoint({"files": [{"path": str(part)}, {"path": str(absent)}]}, data_dir=d) + assert {f["name"]: f["state"] for f in recovery.restore(data_dir=d)["files"]} == { + "part.3mf": "same", "never-existed.3mf": "unverified"} + part.write_bytes(b"synthetic-bytez") # same size, one byte different + assert recovery.restore(data_dir=d)["files"][0]["state"] == "changed" + + +def test_stale_client_revision_is_refused_and_current_one_accepted(tmp_path): + d = str(tmp_path) + first = recovery.checkpoint({"workspace": {"name": "A"}}, data_dir=d)["revision"] + recovery.checkpoint({"workspace": {"name": "B"}}, data_dir=d, expected_revision=first) + with pytest.raises(store.RevisionConflict): + recovery.checkpoint({"workspace": {"name": "C"}}, data_dir=d, expected_revision=first) + assert recovery.restore(data_dir=d)["workspace"] == {"name": "B"} + + +UNC = r"\\host\share\part.3mf" + + +def test_unc_and_directory_paths_are_never_opened_or_hashed(tmp_path, monkeypatch): + opened = [] + real_open = open + monkeypatch.setattr("builtins.open", lambda *a, **k: (opened.append(a[0]), real_open(*a, **k))[1]) + entry = recovery._fingerprint({"path": UNC}) + assert "artifact" not in entry + assert "artifact" not in recovery._fingerprint({"path": str(tmp_path)}) + assert not any(str(p) == UNC for p in opened) + + +def test_secret_like_keys_in_observations_are_not_persisted(tmp_path): + d = str(tmp_path) + recovery.checkpoint({"workspace": {}, "observations": [{"id": "o", "Authorization": "SENTINEL-KEY-9f3a"}]}, + data_dir=d) + raw = open(os.path.join(store.store_dir(d), "recovery.json"), encoding="utf-8").read() + assert "SENTINEL-KEY-9f3a" not in raw diff --git a/backend/tests/recovery/test_routes.py b/backend/tests/recovery/test_routes.py new file mode 100644 index 00000000..a74fcadf --- /dev/null +++ b/backend/tests/recovery/test_routes.py @@ -0,0 +1,101 @@ +"""Recovery and monitoring routes through the real HTTP server. Evidence label: mock-service-tested.""" +import json +import threading +import urllib.error +import urllib.request + +import pytest + +from snapstudio_api import ext_routes +from snapstudio_api.server import build_server +from tests.mocks.hosts.host_mocks import MockHost + +SENTINEL = "SENTINEL-KEY-9f3a" +STATUS = {"result": {"status": {"print_stats": {"filename": "demo.3mf", "state": "printing"}}}} + + +@pytest.fixture +def api(): + httpd, token = build_server(port=0) + thread = threading.Thread(target=httpd.serve_forever, daemon=True) + thread.start() + port = httpd.server_address[1] + + def post(path, body): + req = urllib.request.Request(f"http://127.0.0.1:{port}{path}", data=json.dumps(body).encode(), + method="POST", headers={"X-Auth-Token": token, "X-Api-Key": SENTINEL, + "Content-Type": "application/json"}) + try: + with urllib.request.urlopen(req, timeout=30) as r: + return r.status, r.read().decode() + except urllib.error.HTTPError as e: + return e.code, e.read().decode() + + yield post + httpd.shutdown() + httpd.server_close() + + +def test_routes_register_within_owned_prefixes(): + ext_routes.load_all() + for path in ("/monitoring/read", "/monitoring/history", "/recovery/checkpoint", "/recovery/restore"): + assert path in ext_routes.ROUTES + + +def test_checkpoint_then_restore_over_http_and_no_secret_echo(api): + status, text = api("/recovery/restore", {}) + assert status == 200 and json.loads(text)["missing_checkpoint"] is True + status, text = api("/recovery/checkpoint", {"workspace": {"name": "demo", "token": SENTINEL}, + "jobs": [{"job_id": "j1", "state": "running"}]}) + assert status == 200 and SENTINEL not in text + status, text = api("/recovery/restore", {}) + body = json.loads(text) + assert status == 200 and SENTINEL not in text + assert body["jobs"][0]["state"] == "interrupted" and body["fresh_read_required"] is True + + +def test_checkpoint_rejects_malformed_content(api): + assert api("/recovery/checkpoint", {"workspace": []})[0] == 400 + assert api("/recovery/checkpoint", {"files": "no"})[0] == 400 + + +def test_monitoring_route_reads_mock_with_reconciliation_and_no_control(api): + mock = MockHost({"/printer/objects/query": (200, STATUS)}).start() + try: + status, text = api("/monitoring/read", {"host": "127.0.0.1", "port": mock.server_address[1], + "previous_state": "paused"}) + requests = list(mock.requests) + finally: + mock.stop() + body = json.loads(text) + assert status == 200 and SENTINEL not in text + assert body["association"] == "unverified" and body["camera"]["state"] == "unknown" + assert body["reconciliation"]["events"][0]["to"] == "printing" + assert [(r["method"], r["path"]) for r in requests] == [("GET", "/printer/objects/query")] + assert "x-api-key" not in requests[0]["headers"] # the app's own header is never forwarded + + +def test_monitoring_route_refuses_public_host_and_bad_input(api): + assert api("/monitoring/read", {"host": "8.8.8.8"})[0] == 422 + assert api("/monitoring/read", {"host": "127.0.0.1", "previous_state": 5})[0] == 400 + assert api("/monitoring/history", {})[0] == 400 + + +def test_malformed_monitoring_host_is_a_400_not_a_500(api): + for host in ("evil.com/path", "user@host", "http://x"): + code, _ = api("/monitoring/read", {"host": host}) + assert code == 400, host + code, _ = api("/monitoring/history", {"host": host}) + assert code == 400, host + + +def test_checkpoint_with_stale_expected_revision_is_refused(api): + code, body = api("/recovery/checkpoint", {"workspace": {"project": "a"}}) + assert code == 200 + revision = json.loads(body)["revision"] + code, _ = api("/recovery/checkpoint", {"workspace": {"project": "b"}, "expected_revision": revision}) + assert code == 200 + code, _ = api("/recovery/checkpoint", {"workspace": {"project": "c"}, "expected_revision": revision}) + assert code == 422 + code, _ = api("/recovery/checkpoint", {"workspace": {}, "expected_revision": "x"}) + assert code == 400 diff --git a/backend/tests/test_localpath_links.py b/backend/tests/test_localpath_links.py new file mode 100644 index 00000000..a9c40c03 --- /dev/null +++ b/backend/tests/test_localpath_links.py @@ -0,0 +1,56 @@ +"""The link walk in localpath, exercised with simulated links so it runs on accounts that cannot create symlinks. + +Evidence label: fixture-tested. +""" +from __future__ import annotations + +import os +import stat + +from snapstudio_core import localpath + +UNC = "\\\\attacker-host\\share\\p.3mf" + + +class _Link: + st_mode = stat.S_IFLNK + + +def _simulate(monkeypatch, link: str, target: str): + real_lstat = os.lstat + + def fake_lstat(path, *a, **k): + return _Link() if path == link else real_lstat(path, *a, **k) + + def fake_readlink(path, *a, **k): + if path == link: + return target + raise OSError("not a link") + + monkeypatch.setattr(os, "lstat", fake_lstat) + monkeypatch.setattr(os, "readlink", fake_readlink) + + +def test_link_to_a_network_path_is_refused(monkeypatch, tmp_path): + link = str(tmp_path / "linked.3mf") + _simulate(monkeypatch, link, UNC) + assert localpath._reaches_network(link) is True + assert localpath.usable_path(link) is None + + +def test_link_to_a_mixed_separator_network_path_is_refused(monkeypatch, tmp_path): + link = str(tmp_path / "linked.3mf") + _simulate(monkeypatch, link, "/\\attacker-host/share/p.3mf") + assert localpath._reaches_network(link) is True + + +def test_link_to_a_local_path_is_followed_and_allowed(monkeypatch, tmp_path): + link = str(tmp_path / "linked.3mf") + _simulate(monkeypatch, link, str(tmp_path / "real.3mf")) + assert localpath._reaches_network(link) is False + + +def test_a_link_loop_is_refused(monkeypatch, tmp_path): + link = str(tmp_path / "loop.3mf") + _simulate(monkeypatch, link, link) + assert localpath._reaches_network(link) is True diff --git a/backend/tests/test_review_fixes.py b/backend/tests/test_review_fixes.py new file mode 100644 index 00000000..56e8d4f1 --- /dev/null +++ b/backend/tests/test_review_fixes.py @@ -0,0 +1,147 @@ +"""Regression tests for the Opus+Sol result review of the Phase 2 portfolio candidate. + +Evidence label: fixture-tested. Every file here is synthetic and lives in tmp_path. +""" +from __future__ import annotations + +import json +import os +import sqlite3 +from pathlib import Path + +import pytest + +from snapstudio_api import ext_routes, routes_creative, routes_variants # noqa: F401 (imports register routes) +from snapstudio_core import localpath +from snapstudio_core.calibration import palette, service +from snapstudio_core.passport import build_passport + +UNC = r"\\attacker-host\share\project.3mf" +DEVICE = r"\\?\C:\project.3mf" + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(path, data): + h = FakeHandler() + assert ext_routes.dispatch(h, path, data) is True + return h.sent[0] + + +@pytest.fixture +def no_filesystem_probe(monkeypatch): + """Fail the test if a network or device path reaches any open/stat/listing call.""" + def guarded(real): + def wrapper(*a, **k): + if a and isinstance(a[0], str) and localpath.is_network_or_device_path(a[0]): + raise AssertionError("a network or device path reached the filesystem") + return real(*a, **k) + return wrapper + monkeypatch.setattr(os, "stat", guarded(os.stat)) + monkeypatch.setattr("builtins.open", guarded(open)) + + +@pytest.mark.parametrize("path", [UNC, DEVICE, "//attacker-host/share/p.3mf"]) +def test_network_and_device_paths_are_refused_before_any_filesystem_access(path): + assert localpath.is_network_or_device_path(path) + assert localpath.is_local_regular_file(path) is False + assert localpath.usable_path(path) is None + + +def test_creative_route_refuses_unc_with_a_400(no_filesystem_probe): + code, body = call("/creative/inspect", {"path": UNC}) + assert code == 400 and body["error"] == "invalid" + + +def test_variants_route_refuses_unc_with_a_400(no_filesystem_probe): + code, body = call("/variants/compare", {"outputs": [{"path": UNC}, {"path": UNC}]}) + assert code == 400 and body["error"] == "invalid" + + +def test_passport_build_refuses_unc_as_unreadable(no_filesystem_probe): + built = build_passport([{"path": UNC, "role": "original"}]) + assert built.passport is None + assert built.diagnostics[0].code == "file_unreadable" + + +def test_directory_and_missing_paths_are_not_local_regular_files(tmp_path): + assert localpath.is_local_regular_file(str(tmp_path)) is False + assert localpath.is_local_regular_file(str(tmp_path / "nope.3mf")) is False + assert localpath.usable_path(str(tmp_path / "nope.3mf")) == str(tmp_path / "nope.3mf") # missing stays missing + + +def test_oversize_file_is_not_a_usable_path(tmp_path): + big = tmp_path / "big.bin" + big.write_bytes(b"x" * 64) + assert localpath.is_local_regular_file(str(big), max_bytes=10) is False + assert localpath.usable_path(str(big), max_bytes=10) is None + + +@pytest.mark.parametrize("flag", [True, None, "omitted"]) +def test_passport_never_repeats_an_execution_claim(tmp_path, flag): + original = tmp_path / "a.3mf" + original.write_bytes(b"synthetic") + recipe = {"schema_version": "virtual_recipe/1", "recipe_id": "synthetic:mix", + "generator": {"name": "synthetic", "version": "1"}, "kind": "custom", + "components": [{"slot_ref": "slot:0", "ratio": 1, "ratio_kind": "mass"}], "simulated": True} + if flag != "omitted": + recipe["execution_supported"] = flag + built = build_passport([{"path": str(original), "role": "original"}], recipes=[recipe]) + assert built.passport is not None + document = built.passport.to_dict() + assert [r["execution_supported"] for r in document["recipes"]] == [False] + assert all(r["simulated"] is True for r in document["recipes"]) + + +def test_repeated_calibration_with_different_readings_keeps_both_runs(tmp_path): + parsed = palette.parse_ditherforge("#112233 td Ink").value.to_dict() + basis = service.basis_document("frontlit", 0.2, 0.4, "PLA") + d = str(tmp_path) + first = service.save_run(basis, parsed, service.evaluate(parsed, basis, {"0": "#102030"}, []), data_dir=d) + second = service.save_run(basis, parsed, service.evaluate(parsed, basis, {"0": "#FFFFFF"}, []), data_dir=d) + assert first["run_id"] != second["run_id"] + assert first["inputs_revision"]["revision"] != second["inputs_revision"]["revision"] + assert len(service.history(data_dir=d)) == 2 + again = service.save_run(basis, parsed, service.evaluate(parsed, basis, {"0": "#102030"}, []), data_dir=d) + assert again["run_id"] == first["run_id"] and len(service.history(data_dir=d)) == 2 + + +def test_export_refuses_a_destination_that_appears_before_publication(tmp_path, monkeypatch): + from click.testing import CliRunner + + from snapstudio_core import passport_cli + + original = tmp_path / "a.3mf" + original.write_bytes(b"synthetic") + target = tmp_path / "out.passport.json" + if os.name == "nt": + # Windows creates the final name exclusively through the pinned directory: the destination appears + # after the pin and before the create. + from snapstudio_core import safe_publish + + def appears(): + target.write_text("someone else's file", encoding="utf-8") + + monkeypatch.setattr(safe_publish, "_after_pin", appears) + else: + # POSIX publishes with a dir_fd-relative no-replace link. + real = os.link + + def racing(src, dst, *a, **k): + fd = os.open(dst, os.O_WRONLY | os.O_CREAT, 0o600, dir_fd=k["dst_dir_fd"]) + os.write(fd, "someone else's file".encode("utf-8")) + os.close(fd) + return real(src, dst, *a, **k) + + monkeypatch.setattr(os, "link", racing) + result = CliRunner().invoke(passport_cli.passport, ["export", "--original", str(original), "-o", str(target)]) + assert result.exit_code == 1 + assert json.loads(result.output)["error"]["code"] == "output_exists" + assert target.read_text(encoding="utf-8") == "someone else's file" + assert not list(tmp_path.glob("*.tmp")) and not list(tmp_path.glob(".*.tmp")) diff --git a/backend/tests/test_review_fixes_r2.py b/backend/tests/test_review_fixes_r2.py new file mode 100644 index 00000000..228f02fd --- /dev/null +++ b/backend/tests/test_review_fixes_r2.py @@ -0,0 +1,142 @@ +"""Regression tests for round 2 of the Opus+Sol result review of the Phase 2 portfolio candidate. + +Evidence label: fixture-tested. Every file here is synthetic and lives in tmp_path. +""" +from __future__ import annotations + +import json +import os +import zipfile +from pathlib import Path + +import pytest + +from snapstudio_api import ext_routes, routes_passport, routes_variants # noqa: F401 (imports register routes) +from snapstudio_core import localpath +from snapstudio_core.variants import compare_outputs + +FIXTURES = Path(__file__).resolve().parent / "fixtures" / "variants" +MIXED = ["\\/attacker-host/share/p.3mf", "/\\attacker-host\\share\\p.3mf", "\\\\attacker-host\\share\\p.3mf"] + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(path, data): + h = FakeHandler() + assert ext_routes.dispatch(h, path, data) is True + return h.sent[0] + + +@pytest.fixture +def no_probe(monkeypatch): + """Any filesystem call on a network-looking path fails the test, whichever os / os.path function makes it.""" + def guard(real): + def wrapper(*a, **k): + if a and isinstance(a[0], str) and "attacker-host" in a[0]: + raise AssertionError(f"{real.__name__} probed a network path") + return real(*a, **k) + return wrapper + for owner, name in ((os, "stat"), (os, "lstat"), (os, "readlink"), (os.path, "isdir"), (os.path, "isfile"), + (os.path, "exists"), (os.path, "realpath"), (os.path, "getsize"), (os, "listdir")): + monkeypatch.setattr(owner, name, guard(getattr(owner, name))) + monkeypatch.setattr("builtins.open", guard(open)) + + +@pytest.mark.parametrize("path", MIXED) +def test_mixed_separator_unc_forms_are_network_paths(path): + assert localpath.is_network_or_device_path(path) + + +@pytest.mark.parametrize("path", MIXED) +def test_guard_helpers_never_probe_a_network_path(no_probe, path): + assert localpath.is_local_regular_file(path) is False + assert localpath.is_local_directory(path) is False + assert localpath.usable_path(path) is None + + +def test_passport_routes_refuse_a_unc_base_dir_without_probing(no_probe): + for route in ("/passport/verify", "/passport/import"): + code, body = call(route, {"passport": {}, "base_dir": MIXED[0]}) + assert code == 400, route + + +def test_passport_cli_refuses_unc_paths_before_click_probes_them(no_probe): + from click.testing import CliRunner + + from snapstudio_core import passport_cli + + runner = CliRunner() + for args in (["export", "--original", MIXED[2]], ["verify", MIXED[1]], ["verify", "x.json", "--base-dir", MIXED[0]]): + result = runner.invoke(passport_cli.passport, args) + assert result.exit_code == 2, args + assert "local files only" in result.output + + +def test_a_symlink_to_a_network_path_is_refused_without_following_it(tmp_path, monkeypatch, no_probe): + link = tmp_path / "linked.3mf" + try: + os.symlink(MIXED[2], link) + except (OSError, NotImplementedError): + pytest.skip("this account cannot create symlinks") + assert localpath.is_local_regular_file(str(link)) is False + assert localpath.usable_path(str(link)) is None + + +def test_a_symlink_to_a_local_file_is_still_usable(tmp_path): + real = tmp_path / "real.3mf" + real.write_bytes(b"synthetic") + link = tmp_path / "linked.3mf" + try: + os.symlink(real, link) + except (OSError, NotImplementedError): + pytest.skip("this account cannot create symlinks") + assert localpath.is_local_regular_file(str(link)) is True + + +def _3mf_with_member(tmp_path, name, payload, *, corrupt=False): + path = tmp_path / "out.3mf" + with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as archive: + archive.writestr("3D/3dmodel.model", "") + archive.writestr(name, payload) + if corrupt: + raw = bytearray(path.read_bytes()) + marker = raw.find(b"Metadata/") + # flip a byte inside the deflate stream of the member (after its local header) + header_end = marker + len(name) + 2 + raw[header_end + 2] ^= 0xFF + path.write_bytes(bytes(raw)) + return path + + +def test_corrupt_recognised_member_is_a_400_not_silently_unknown(tmp_path): + path = _3mf_with_member(tmp_path, "Metadata/project_settings.config", json.dumps({"layer_height": "0.2"}) * 20, + corrupt=True) + code, body = call("/variants/compare", {"outputs": [{"path": str(path)}, {"path": str(FIXTURES / "right.gcode")}]}) + assert code == 400 and body["error"] == "invalid" + + +@pytest.mark.parametrize("bad", ["1" + "0" * 400, "1e999", -1]) +def test_overflowing_or_infinite_numbers_are_unknown_not_a_500(tmp_path, bad): + payload = '{"estimated_time_s": %s, "filament_used_g": [%s]}' % (bad, bad) + path = _3mf_with_member(tmp_path, "Metadata/slice_info.config", payload) + report = compare_outputs({"outputs": [{"path": str(path)}, {"path": str(FIXTURES / "right.gcode")}]}) + first = report["outputs"][0] + assert first["time"]["status"] == "unknown" if "time" in first else True + assert json.dumps(report) # no inf/nan leaked into the response + + +def test_deeply_nested_project_settings_is_a_400_for_creative(tmp_path): + from snapstudio_api import routes_creative # noqa: F401 + + path = tmp_path / "deep.3mf" + with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as archive: + archive.writestr("3D/3dmodel.model", "") + archive.writestr("Metadata/project_settings.config", "[" * 200000 + "]" * 200000) + code, body = call("/creative/inspect", {"path": str(path)}) + assert code == 400 diff --git a/backend/tests/test_review_fixes_r3.py b/backend/tests/test_review_fixes_r3.py new file mode 100644 index 00000000..5f29d619 --- /dev/null +++ b/backend/tests/test_review_fixes_r3.py @@ -0,0 +1,120 @@ +"""Regression tests for round 3 of the Opus+Sol result review of the Phase 2 portfolio candidate. + +Evidence label: fixture-tested. Every file here is synthetic and lives in tmp_path. +""" +from __future__ import annotations + +import json +import lzma +import os +import stat +import zipfile +from pathlib import Path + +import pytest +from click.testing import CliRunner + +from snapstudio_api import ext_routes +from snapstudio_core import localpath, passport_cli + +FIXTURES = Path(__file__).resolve().parent / "fixtures" / "variants" +UNC_OUT = "\\\\attacker-host\\share\\out.passport.json" +MIXED_OUT = "/\\attacker-host/share/out.passport.json" +DEVICE_OUT = "\\\\.\\C:\\out.passport.json" + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(path, data): + h = FakeHandler() + assert ext_routes.dispatch(h, path, data) is True + return h.sent[0] + + +def test_windows_readlink_form_of_a_local_drive_is_plain_but_network_forms_stay_refused(): + assert localpath._plain_drive_target("\\\\?\\C:\\models") == "C:\\models" + assert localpath._plain_drive_target("\\\\?\\UNC\\host\\share\\x") == "\\\\?\\UNC\\host\\share\\x" + assert localpath._plain_drive_target("\\\\.\\C:\\x") == "\\\\.\\C:\\x" + assert localpath._plain_drive_target("\\\\?\\Volume{1234}\\x") == "\\\\?\\Volume{1234}\\x" + assert localpath._plain_drive_target("C:\\plain") == "C:\\plain" + + +class _Link: + st_mode = stat.S_IFLNK + + +def _simulate(monkeypatch, link: str, target: str): + real_lstat = os.lstat + monkeypatch.setattr(os, "lstat", lambda p, *a, **k: _Link() if p == link else real_lstat(p, *a, **k)) + monkeypatch.setattr(os, "readlink", lambda p, *a, **k: target if p == link else (_ for _ in ()).throw(OSError())) + + +@pytest.mark.skipif(os.name != "nt", reason="the readlink form being simulated is Windows-specific") +def test_junction_to_a_local_drive_as_windows_reports_it_is_not_refused(monkeypatch, tmp_path): + link = str(tmp_path / "models") + _simulate(monkeypatch, link, "\\\\?\\" + str(tmp_path / "real")) + assert localpath._reaches_network(link) is False + + +def test_junction_to_a_unc_share_as_windows_reports_it_is_refused(monkeypatch, tmp_path): + link = str(tmp_path / "models") + _simulate(monkeypatch, link, "\\\\?\\UNC\\attacker-host\\share\\x") + assert localpath._reaches_network(link) is True + + +@pytest.fixture +def no_probe(monkeypatch): + def guard(real): + def wrapper(*a, **k): + if a and isinstance(a[0], str) and "attacker-host" in a[0]: + raise AssertionError(f"{real.__name__} probed a network path") + return real(*a, **k) + return wrapper + for owner, name in ((os, "stat"), (os, "lstat"), (os.path, "exists"), (os.path, "isdir"), (os.path, "isfile"), + (os, "link"), (os, "rename")): + monkeypatch.setattr(owner, name, guard(getattr(owner, name))) + import tempfile + monkeypatch.setattr(tempfile, "mkstemp", lambda *a, **k: (_ for _ in ()).throw( + AssertionError("mkstemp reached") if "attacker-host" in str(k.get("dir", "")) else OSError())) + monkeypatch.setattr("builtins.open", guard(open)) + + +@pytest.mark.parametrize("out", [UNC_OUT, MIXED_OUT, DEVICE_OUT]) +def test_export_output_on_a_network_or_device_path_is_refused_before_any_probe(no_probe, tmp_path, out): + original = tmp_path / "a.3mf" + original.write_bytes(b"synthetic") + result = CliRunner().invoke(passport_cli.passport, ["export", "--original", str(original), "-o", out]) + assert result.exit_code == 1 + assert json.loads(result.output)["error"]["code"] == "bad_output" + + +def test_export_output_below_a_link_to_a_share_is_refused(monkeypatch, tmp_path): + original = tmp_path / "a.3mf" + original.write_bytes(b"synthetic") + link = str(tmp_path / "outdir") + _simulate(monkeypatch, link, "\\\\attacker-host\\share\\dir") + result = CliRunner().invoke(passport_cli.passport, + ["export", "--original", str(original), "-o", os.path.join(link, "p.json")]) + assert result.exit_code == 1 + assert json.loads(result.output)["error"]["code"] == "bad_output" + + +@pytest.mark.parametrize("error", [EOFError("truncated"), lzma.LZMAError("corrupt")]) +def test_truncated_or_corrupt_compressed_member_is_a_400(monkeypatch, tmp_path, error): + path = tmp_path / "out.3mf" + with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as archive: + archive.writestr("3D/3dmodel.model", "") + archive.writestr("Metadata/slice_info.config", json.dumps({"estimated_time_s": 5})) + + def broken_read(self, *a, **k): + raise error + + monkeypatch.setattr(zipfile.ZipFile, "read", broken_read) + code, body = call("/variants/compare", {"outputs": [{"path": str(path)}, {"path": str(FIXTURES / "right.gcode")}]}) + assert code == 400 and body["error"] == "invalid" diff --git a/backend/tests/test_routes_creative.py b/backend/tests/test_routes_creative.py new file mode 100644 index 00000000..a4e15e60 --- /dev/null +++ b/backend/tests/test_routes_creative.py @@ -0,0 +1,167 @@ +"""Route level: /creative/inspect (read-only inspection of a local 3MF). + +Evidence label: fixture-tested. Archives are synthetic, built in memory by the checked-in zip factory. +""" +from __future__ import annotations + +import hashlib +import json +import sys +from pathlib import Path + +import pytest + +from snapstudio_api import ext_routes, routes_creative + +sys.path.insert(0, str(Path(__file__).resolve().parent / "fixtures" / "creative")) +from zip_factory import make_3mf # noqa: E402 + +ROW = "1,2,1,0,50,0,u7" + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(data): + h = FakeHandler() + assert ext_routes.dispatch(h, "/creative/inspect", data) is True + assert len(h.sent) == 1 + return h.sent[0] + + +def write(tmp_path, blob, name="project.3mf"): + p = tmp_path / name + p.write_bytes(blob) + return str(p) + + +def test_route_registered_under_creative_only(): + assert "/creative/inspect" in ext_routes.ROUTES + assert all(p.startswith("/creative") for p, fn in ext_routes.ROUTES.items() + if fn.__module__.endswith("routes_creative")) + + +def test_registered_in_a_fresh_interpreter(): + import subprocess + code = ("from snapstudio_api import ext_routes; " + "print('/creative/inspect' in ext_routes.ROUTES)") + out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, + cwd=str(Path(__file__).resolve().parents[1]), timeout=60) + assert out.stdout.strip() == "True", out.stderr + + +def test_fullspectrum_happy_path_with_recipes_and_untouched_original(tmp_path): + blob = make_3mf(settings={"mixed_filament_definitions": ROW, "layer_height": "0.2"}, + members={"Metadata/plate_1.png": b"opaque-bytes"}) + path = write(tmp_path, blob) + code, body = call({"path": path}) + assert code == 200 + assert body["origin"]["generator"] == "fullspectrum" and body["origin"]["status"] == "detected" + assert "mixed_filament_definitions" in body["understood"] + names = {i["name"] for i in body["passed_through"]} + assert {"layer_height", "Metadata/plate_1.png"} <= names + assert body["recipes"] and all(r["simulated"] is True and r["execution_supported"] is False + for r in body["recipes"]) + assert body["physical_slots_needed"] == 2 + src = body["source"] + assert src["sha256_before"] == src["sha256_after"] == hashlib.sha256(blob).hexdigest() + assert src["unchanged"] is True and src["name"] == "project.3mf" + assert str(tmp_path) not in json.dumps(body) + assert Path(path).read_bytes() == blob + + +def test_forca_reports_missing_and_unknown_keys(tmp_path): + blob = make_3mf(settings={"fos_mms_unit_count": 2, "fos_future_key": 1, "nozzle_diameter": ["0.4"]}) + code, body = call({"path": write(tmp_path, blob)}) + assert code == 200 and body["origin"]["generator"] == "forcaslicer" + assert "fos_physical_nozzle_diameter" in body["missing"] + assert "fos_future_key" in body["could_not_interpret"] + assert "nozzle_diameter" in {i["name"] for i in body["passed_through"]} + + +def test_unknown_generator_is_origin_unknown(tmp_path): + blob = make_3mf(settings={"layer_height": "0.2"}, members={"Metadata/x.bin": b"zz"}) + code, body = call({"path": write(tmp_path, blob)}) + assert code == 200 + assert body["origin"] == {"generator": "unknown", "status": "unknown", "candidates": []} + assert "generator_not_recognized" in body["diagnostics"] + assert body["recipes"] == [] + assert {"layer_height", "Metadata/x.bin"} <= {i["name"] for i in body["passed_through"]} + assert body["source"]["unchanged"] is True + + +def test_ambiguous_generators_are_not_guessed(tmp_path): + blob = make_3mf(settings={"fos_mms_unit_count": 2, "extruder_layer_height": [0.1]}) + code, body = call({"path": write(tmp_path, blob)}) + assert code == 200 + assert body["origin"]["generator"] == "unknown" and body["origin"]["status"] == "ambiguous" + assert set(body["origin"]["candidates"]) == {"forcaslicer", "lixnix"} + + +def test_format_hint_is_honoured_and_validated(tmp_path): + path = write(tmp_path, make_3mf(settings={"layer_height": "0.2"})) + code, body = call({"path": path, "format": "lumina"}) + assert code == 200 and body["origin"]["generator"] == "lumina" and body["origin"]["status"] == "chosen" + assert call({"path": path, "format": "nope"})[0] == 400 + + +def test_missing_file_is_400(tmp_path): + code, body = call({"path": str(tmp_path / "absent.3mf")}) + assert code == 400 and body["error"] == "invalid" + + +def test_directory_is_400(tmp_path): + assert call({"path": str(tmp_path)})[0] == 400 + + +def test_missing_or_bad_path_field_is_400(): + assert call({})[0] == 400 + assert call({"path": 5})[0] == 400 + assert call({"path": "a\x00b"})[0] == 400 + assert call({"path": "x" * 2000})[0] == 400 + + +def test_oversized_file_is_refused_unread(tmp_path, monkeypatch): + path = write(tmp_path, make_3mf(settings={"layer_height": "0.2"})) + monkeypatch.setattr(routes_creative, "MAX_FILE_BYTES", 10) + code, body = call({"path": path}) + assert code == 400 and "larger" in body["message"] + + +def test_non_zip_is_400_and_untouched(tmp_path): + blob = b"this is not a zip archive" + path = write(tmp_path, blob, "notes.3mf") + code, body = call({"path": path}) + assert code == 400 and "not a readable 3MF" in body["message"] + assert Path(path).read_bytes() == blob + + +def test_zip_without_model_is_400(tmp_path): + import io + import zipfile + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w") as z: + z.writestr("readme.txt", "hi") + code, body = call({"path": write(tmp_path, buf.getvalue())}) + assert code == 400 and "no 3D model" in body["message"] + + +def test_original_bytes_and_mtime_unchanged_and_no_siblings(tmp_path): + blob = make_3mf(settings={"mixed_filament_definitions": ROW}) + path = write(tmp_path, blob) + before = Path(path).stat().st_mtime_ns + call({"path": path}) + assert Path(path).read_bytes() == blob + assert Path(path).stat().st_mtime_ns == before + assert [p.name for p in tmp_path.iterdir()] == ["project.3mf"] + + +def test_copy_has_no_overclaims(): + text = (routes_creative.ADVISORY + routes_creative.EVIDENCE).lower() + for banned in ("guarantee", "100%", "simultaneous", "ready", "coming soon"): + assert banned not in text diff --git a/backend/tests/variants/__init__.py b/backend/tests/variants/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/tests/variants/test_routes_variants.py b/backend/tests/variants/test_routes_variants.py new file mode 100644 index 00000000..295f7857 --- /dev/null +++ b/backend/tests/variants/test_routes_variants.py @@ -0,0 +1,50 @@ +"""Route level: /variants/compare answers bad input with a plain 400, never the internal-error 500. + +Evidence label: fixture-tested. +""" +from __future__ import annotations + +from pathlib import Path + +from snapstudio_api import ext_routes, routes_variants # noqa: F401 (import registers the route) + +FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" / "variants" + + +class FakeHandler: + def __init__(self): + self.sent = [] + + def _send(self, code, obj): + self.sent.append((code, obj)) + + +def call(data): + h = FakeHandler() + assert ext_routes.dispatch(h, "/variants/compare", data) is True + assert len(h.sent) == 1 + return h.sent[0] + + +def test_empty_body_is_a_400_with_a_plain_sentence(): + code, body = call({}) + assert code == 400 + assert body["error"] == "invalid" + assert "exactly two" in body["message"] + + +def test_missing_file_is_a_400_not_a_500(): + code, body = call({"outputs": [{"path": str(FIXTURES / "nope.gcode")}, {"path": str(FIXTURES / "right.gcode")}]}) + assert code == 400 and body["error"] == "invalid" + + +def test_two_fixture_outputs_compare(): + code, body = call({"outputs": [{"path": str(FIXTURES / "left.gcode")}, {"path": str(FIXTURES / "right.gcode")}]}) + assert code == 200 and len(body["outputs"]) == 2 + + +def test_garbage_3mf_is_a_400_not_a_500(tmp_path): + bad = tmp_path / "broken.3mf" + bad.write_bytes(b"this is not a zip archive") + code, body = call({"outputs": [{"path": str(bad)}, {"path": str(FIXTURES / "right.gcode")}]}) + assert code == 400 and body["error"] == "invalid" diff --git a/backend/tests/variants/test_variants_comparison.py b/backend/tests/variants/test_variants_comparison.py new file mode 100644 index 00000000..bcef92d5 --- /dev/null +++ b/backend/tests/variants/test_variants_comparison.py @@ -0,0 +1,112 @@ +from __future__ import annotations + +from pathlib import Path + +from snapstudio_core.foundation import capabilities +from snapstudio_core.foundation import topology +from snapstudio_core.variants.comparison import MAX_GCODE_SCAN, compare_outputs + +FIXTURES = Path(__file__).parents[1] / "fixtures" / "variants" + + +def compare(a: Path, b: Path, **extra): + return compare_outputs({"outputs": [{"path": str(a)}, {"path": str(b)}], **extra}) + + +def test_two_outputs_have_byte_backed_artifact_refs(): + report = compare(FIXTURES / "left.gcode", FIXTURES / "right.gcode") + assert all(row["artifact"]["artifact_id"].startswith("sha256:") for row in report["outputs"]) + assert all(row["artifact"]["locator"] is None for row in report["outputs"]) + + +def test_grams_are_logical_until_a_binding_is_verified(): + rows = compare(FIXTURES / "left.gcode", FIXTURES / "right.gcode")["outputs"][0]["grams_by_tool"] + assert rows[0]["logical_tool"] == 1 + assert rows[0]["basis"] == "logical tool (physical unknown)" + setup = { + "schema_version": "topology/1", "snapshot_id": "synthetic", "received_at": "2026-10-31T00:00:00Z", + "heads": [{"id": "head:0", "index": 0, "nozzle_mm": None, "evidence": []}], + "units": [], "slots": [{"id": "slot:0", "index": 0, "binding": "head:0", "nozzle_mm": None}], + "bindings": [], "complete": True, "evidence": [], "diagnostics": [], + } + assert topology.Topology.from_dict(setup).value is not None + bound = compare_outputs({"outputs": [ + {"path": str(FIXTURES / "left.gcode"), "physical_bindings": {"0": {"topology": setup}}}, + {"path": str(FIXTURES / "right.gcode")}, + ]})["outputs"][0]["grams_by_tool"] + assert bound[0]["physical_source"] == "head 1" + assert bound[0]["basis"] == "physical source" + + +def test_gcode_zero_based_tools_and_project_one_based_slots_normalise(): + report = compare(FIXTURES / "left.gcode", FIXTURES / "right.gcode") + assert report["outputs"][0]["tools_used"]["value"] == [0, 1] + assert [row["logical_tool"] for row in report["outputs"][0]["grams_by_tool"]] == [1, 2] + + +def test_tool_change_count_is_computed_from_bounded_gcode_body(): + output = compare(FIXTURES / "left.gcode", FIXTURES / "right.gcode")["outputs"][0] + assert output["tool_changes"] == {"value": 2, "status": "computed"} + + +def test_unknown_tool_usage_keeps_waste_unknown(): + output = compare(FIXTURES / "unknown.gcode", FIXTURES / "right.gcode")["outputs"][0] + assert output["tools_used"]["status"] == "unknown" + assert output["waste"]["status"] == "unknown" + + +def test_missing_time_stays_unknown(): + output = compare(FIXTURES / "unknown.gcode", FIXTURES / "right.gcode")["outputs"][0] + assert output["time"] == {"value": None, "unit": "seconds", "status": "unknown"} + + +def test_changed_bytes_are_marked_stale(tmp_path): + changed = tmp_path / "changed.gcode" + changed.write_text((FIXTURES / "left.gcode").read_text() + "; changed\n", encoding="utf-8") + # Compare against a snapshot candidate then mutate its source during artifact admission. + from snapstudio_core.foundation import artifacts + original = artifacts.snapshot + + def snapshot_then_mutate(path, *args, **kwargs): + result = original(path, *args, **kwargs) + if Path(path) == changed: + Path(path).write_text(Path(path).read_text(encoding="utf-8") + "; mutation\n", encoding="utf-8") + return result + + artifacts.snapshot = snapshot_then_mutate + try: + report = compare_outputs({"outputs": [{"path": str(changed)}, {"path": str(FIXTURES / "right.gcode")}]}) + finally: + artifacts.snapshot = original + assert report["stale"] is True + assert report["outputs"][0]["freshness"] == "stale" + + +def test_settings_diff_uses_preserve_config_diff(): + report = compare(FIXTURES / "left.gcode", FIXTURES / "right.gcode") + assert {"key": "layer_height", "old": "0.20", "new": "0.24", "removed": False} in report["settings_diff"] + + +def test_requirements_use_foundation_capability_evaluator_and_basis_changes_unknown(): + evidence = capabilities.CapabilitySet() + evidence.add(capabilities.Capability("supports_material_x", "supported")) + other_basis = capabilities.CapabilitySet() + other_basis.add(capabilities.Capability("supports_material_x", "unsupported")) + report = compare_outputs({"outputs": [{"path": str(FIXTURES / "left.gcode")}, + {"path": str(FIXTURES / "right.gcode")}], + "requirements": [{"key": "supports_material_x"}], + "capability_sets": [evidence.to_dict(), other_basis.to_dict()]}) + assert report["requirements"][0]["result"] == "unknown" + same_basis = compare_outputs({"outputs": [{"path": str(FIXTURES / "left.gcode")}, + {"path": str(FIXTURES / "left.gcode")}], + "requirements": [{"key": "supports_material_x"}], + "capability_set": evidence.to_dict()}) + assert same_basis["requirements"][0]["result"] == "satisfied" + + +def test_gcode_scan_limit_is_bounded(tmp_path): + text = ";\n" + "G1 X0\n" * ((MAX_GCODE_SCAN // 6) + 100) + path = tmp_path / "oversize.gcode" + path.write_text(text, encoding="utf-8") + report = compare(path, FIXTURES / "right.gcode") + assert report["outputs"][0]["tool_changes"] == {"value": None, "status": "unknown"} diff --git a/backend/u1convert/cli.py b/backend/u1convert/cli.py index 91234b96..c6a9d6ab 100644 --- a/backend/u1convert/cli.py +++ b/backend/u1convert/cli.py @@ -360,5 +360,10 @@ def selfcheck_cmd(sample, as_json): raise SystemExit(1) +from snapstudio_core.passport_cli import passport # noqa: E402 + +cli.add_command(passport) + + if __name__ == "__main__": # `python -m u1convert.cli` without installing the package cli() diff --git a/desktop/src/components/creative/CreativeReportView.tsx b/desktop/src/components/creative/CreativeReportView.tsx new file mode 100644 index 00000000..8ed1d3f5 --- /dev/null +++ b/desktop/src/components/creative/CreativeReportView.tsx @@ -0,0 +1,68 @@ +import type { ReactNode } from "react"; +import { originSentence, type CreativeInspectResponse } from "@/lib/creativeApi"; + +function Section({ title, note, children }: { title: string; note: string; children: ReactNode }) { + return ( +
+

{title}

+

{note}

+ {children} +
+ ); +} + +function NameList({ items, empty }: { items: string[]; empty: string }) { + if (items.length === 0) return

{empty}

; + return
    {items.map((i) =>
  • {i}
  • )}
; +} + +/** What Studio read from one project file: understood, kept untouched, could not interpret. Advisory only. */ +export default function CreativeReportView({ report }: { report: CreativeInspectResponse }) { + const { source } = report; + return ( +
+
+

{originSentence(report.origin)}

+

{report.advisory}

+

Evidence: fixture-tested. The readers are tested against hand-written files shaped like the published formats, not against captured projects.

+
+
+
+ +
+
+ `${p.name} (${p.sha256.slice(0, 8)})`)} empty="Nothing else in this file." /> +
+
+ `${m} (expected, not found)`), + ...report.diagnostics.map((d) => `note: ${d}`)]} empty="Nothing was left uninterpreted." /> +
+
+
+ {report.recipes.length === 0 ?

No recipes in this file.

: ( +
    + {report.recipes.map((r) => ( +
  • + {r.recipe_id}: {r.components.length} source slots + {" "}(ratios not stated). Simulated{r.execution_supported === false ? "; execution not supported" : ""}. +
  • + ))} +
+ )} + {report.physical_slots_needed !== null && ( +

Distinct project slots referenced: {report.physical_slots_needed}. This is not a statement about your loaded filament.

+ )} +
+
+

+ {source.unchanged + ? "Original unchanged: the file fingerprint is identical before and after inspection." + : "The file fingerprint differs between the two reads. Something outside Studio changed the file while it was being inspected."} +

+

+ {source.name}, {source.size_bytes} bytes. before {source.sha256_before.slice(0, 16)}, after {source.sha256_after.slice(0, 16)} +

+
+
+ ); +} diff --git a/desktop/src/components/creative/CreativeView.tsx b/desktop/src/components/creative/CreativeView.tsx new file mode 100644 index 00000000..ed9d3737 --- /dev/null +++ b/desktop/src/components/creative/CreativeView.tsx @@ -0,0 +1,52 @@ +import { useEffect, useRef, useState } from "react"; +import { Palette } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { PageHeader } from "@/components/ui/layout"; +import { CREATIVE_FORMATS, inspectCreative, type CreativeFormat, type CreativeInspectResponse } from "@/lib/creativeApi"; +import CreativeReportView from "./CreativeReportView"; + +/** Inspect one local creative project file. The path is sent to the local Studio service only, which + * opens the file for reading, never writes it, and never slices or contacts a printer. */ +export default function CreativeView() { + const [path, setPath] = useState(""); + const [format, setFormat] = useState("auto"); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(null); + const [report, setReport] = useState(null); + const alive = useRef(true); + useEffect(() => { + alive.current = true; + return () => { alive.current = false; }; + }, []); + + const run = async () => { + // A failed inspection must not leave the previous file's report under the new path. + setBusy(true); setError(null); setReport(null); + try { + const res = await inspectCreative(path, format); + if (alive.current) setReport(res); + } catch (e) { + if (alive.current) setError(e instanceof Error ? e.message : "The inspection failed."); + } finally { + if (alive.current) setBusy(false); + } + }; + + const field = "h-9 rounded-md border border-border bg-card px-3 text-sm outline-none focus-visible:ring-2 focus-visible:ring-primary/50"; + return ( +
+ +
+ setPath(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter" && !busy && path.trim() !== "") void run(); }} /> + + +
+

Studio does not guess the generator: when the settings do not identify it, the origin is reported as unknown.

+ {error &&

{error}

} + {report && } +
+ ); +} diff --git a/desktop/src/components/creative/creative.test.tsx b/desktop/src/components/creative/creative.test.tsx new file mode 100644 index 00000000..955c3ed3 --- /dev/null +++ b/desktop/src/components/creative/creative.test.tsx @@ -0,0 +1,91 @@ +import { describe, expect, it } from "vitest"; +import { renderToStaticMarkup } from "react-dom/server"; +import CreativeReportView from "./CreativeReportView"; +import CreativeView from "./CreativeView"; +import creativeRoute from "@/routes/ext/creative.route"; +import creativeMeta from "@/routes/ext/creative.meta"; +import type { CreativeInspectResponse } from "@/lib/creativeApi"; + +const HASH = "a".repeat(64); + +function report(over: Partial = {}): CreativeInspectResponse { + return { + origin: { generator: "fullspectrum", status: "detected", candidates: ["fullspectrum"] }, + understood: ["mixed_filament_definitions"], + passed_through: [{ name: "layer_height", sha256: HASH }], + could_not_interpret: ["fos_future_key"], + missing: ["fos_physical_nozzle_diameter"], + diagnostics: ["unrecognized_fullspectrum_row_shape"], + recipes: [{ recipe_id: "fullspectrum:7", generator: { name: "FullSpectrum", version: "0.9.12" }, kind: "fullspectrum", + components: [{ source: "project:1:1" }, { source: "project:1:2" }], simulated: true, execution_supported: false }], + physical_slots_needed: 2, + source: { name: "synthetic.3mf", size_bytes: 1234, sha256_before: HASH, sha256_after: HASH, unchanged: true }, + evidence: "fixture-tested", advisory: "Advisory read of one project file.", + ...over, + }; +} + +describe("creative route wiring", () => { + it("route default export has the { path, Component } shape and meta agrees", () => { + expect(Object.keys(creativeRoute).sort()).toEqual(["Component", "path"]); + expect(typeof creativeRoute.Component).toBe("function"); + expect(creativeRoute.path).toBe("/creative"); + expect(creativeMeta.path).toBe(creativeRoute.path); + expect(creativeMeta.group).toBe("Studio+"); + expect(creativeMeta.label).toBe("Creative import"); + }); +}); + +describe("creative report view", () => { + const html = renderToStaticMarkup(); + + it("shows understood, untouched and could-not-interpret buckets", () => { + expect(html).toContain("mixed_filament_definitions"); + expect(html).toContain("layer_height (aaaaaaaa)"); + expect(html).toContain("fos_future_key"); + expect(html).toContain("fos_physical_nozzle_diameter (expected, not found)"); + expect(html).toContain("note: unrecognized_fullspectrum_row_shape"); + }); + + it("labels recipes as simulated, not executed, ratios not stated", () => { + expect(html).toContain("fullspectrum:7"); + expect(html).toContain("ratios not stated"); + expect(html).toContain("Simulated; execution not supported"); + expect(html).toContain("not a statement about your loaded filament"); + }); + + it("shows the unchanged-original hash line and the evidence label", () => { + expect(html).toContain("Original unchanged"); + expect(html).toContain("before aaaaaaaaaaaaaaaa, after aaaaaaaaaaaaaaaa"); + expect(html).toContain("fixture-tested"); + }); + + it("reports a changed fingerprint instead of claiming it is unchanged", () => { + const changed = renderToStaticMarkup(); + expect(changed).not.toContain("Original unchanged"); + expect(changed).toContain("differs between the two reads"); + }); + + it("unknown origin stays unknown", () => { + const h = renderToStaticMarkup(); + expect(h).toContain("Origin unknown"); + expect(h).toContain("No recipes in this file."); + }); + + it("has no overclaiming or readiness wording", () => { + const all = (html + renderToStaticMarkup()).toLowerCase(); + for (const banned of ["ready", "coming soon", "guarantee", "100%", "simultaneous", "print success", "printer doctor"]) { + expect(all).not.toContain(banned); + } + }); +}); + +describe("creative view", () => { + it("renders the path input, format choice and a disabled inspect button", () => { + const html = renderToStaticMarkup(); + expect(html).toContain("Project file path"); + expect(html).toContain("Detect from settings"); + expect(html).toMatch(/]*disabled[^>]*>Inspect project/); + }); +}); diff --git a/desktop/src/lib/assemblies/AssembliesView.tsx b/desktop/src/lib/assemblies/AssembliesView.tsx new file mode 100644 index 00000000..3a68c655 --- /dev/null +++ b/desktop/src/lib/assemblies/AssembliesView.tsx @@ -0,0 +1,104 @@ +import { useEffect, useRef, useState } from "react"; +import { Boxes } from "lucide-react"; +import { Button } from "@/components/ui/button"; +import { PageHeader } from "@/components/ui/layout"; +import { evaluateAssembly, getAssembly, listAssemblies, parseJsonBox, saveAssembly } from "./api"; +import AssemblyReportView from "./AssemblyReportView"; +import { SAMPLE_ASSEMBLY, sampleSupply } from "./sample"; +import type { AssemblyReport, SavedAssembly } from "./types"; + +/** Plan a multi-tile project against shared spools. The assembly and the spool readings are typed here; + * nothing is sent anywhere except the local Studio service, and the verdict is advisory. */ +export default function AssembliesView() { + const [assemblyText, setAssemblyText] = useState(""); + const [supplyText, setSupplyText] = useState(""); + const [report, setReport] = useState(null); + const [error, setError] = useState(null); + const [notice, setNotice] = useState(null); + const [busy, setBusy] = useState(false); + const [saved, setSaved] = useState([]); + const [current, setCurrent] = useState<{ id: number; revision: number } | null>(null); + const alive = useRef(true); + + const refresh = () => listAssemblies().then((r) => { if (alive.current) setSaved(r.assemblies); }).catch(() => undefined); + useEffect(() => { + alive.current = true; + void refresh(); + return () => { alive.current = false; }; + }, []); + + const loadSample = () => { + setAssemblyText(JSON.stringify(SAMPLE_ASSEMBLY, null, 2)); + setSupplyText(JSON.stringify(sampleSupply(Math.floor(Date.now() / 1000)), null, 2)); + setReport(null); setError(null); setNotice(null); setCurrent(null); + }; + + const run = async (action: () => Promise) => { + setBusy(true); setError(null); setNotice(null); + try { + await action(); + } catch (e) { + if (alive.current) setError(e instanceof Error ? e.message : "That did not work."); + } finally { + if (alive.current) setBusy(false); + } + }; + + const evaluate = () => run(async () => { + const assembly = parseJsonBox(assemblyText, "The assembly"); + const supply = supplyText.trim() === "" ? [] : parseJsonBox(supplyText, "The spool readings"); + setReport(null); + const res = await evaluateAssembly(assembly, supply); + if (alive.current) setReport(res.report); + }); + + const save = () => run(async () => { + const assembly = parseJsonBox(assemblyText, "The assembly"); + const res = current ? await saveAssembly(assembly, current.id, current.revision) : await saveAssembly(assembly); + if (!alive.current) return; + setCurrent({ id: res.id, revision: res.revision }); + setNotice("Saved. Spool readings are not saved; add them again when you check."); + await refresh(); + }); + + const open = (id: number) => run(async () => { + const res = await getAssembly(id); + if (!alive.current) return; + setAssemblyText(JSON.stringify(res.assembly, null, 2)); + setCurrent({ id: res.id, revision: res.revision }); + setReport(null); + }); + + const box = "min-h-[16rem] w-full rounded-md border border-border bg-card p-3 font-mono text-xs outline-none focus-visible:ring-2 focus-visible:ring-primary/50"; + return ( +
+ +
+ + + + {saved.length > 0 && ( + + )} +
+
+