diff --git a/backend/snapstudio_core/variants/README.md b/backend/snapstudio_core/variants/README.md
new file mode 100644
index 00000000..cf676fc9
--- /dev/null
+++ b/backend/snapstudio_core/variants/README.md
@@ -0,0 +1,27 @@
+# Sliced-output comparison
+
+Studio compares existing local sliced outputs; it does not slice or contact a
+printer. Orca/Bambu `Metadata/slice_info.config` XML plate summaries are read
+alongside JSON project settings. The earlier synthetic JSON slice-summary
+representation remains readable.
+
+Usage is aggregated by one-based filament ID across plates. Missing or invalid
+usage on any plate makes the aggregate unknown, rather than presenting a partial
+sum as complete. Missing/invalid plate predictions make total time unknown.
+Repeated filament IDs within a plate are rejected. XML is bounded by the existing
+2 MiB member limit; only UTF-8 (optional BOM) is evaluated, with DTDs/entities
+refused. XML summaries do not establish tool-change counts or physical bindings.
+
+G-code material/time summaries are accepted in header or footer comments. The
+body scan remains limited to 8 MiB, with a separate 256 KiB tail read for large
+files. Tool changes remain unknown when the body scan is truncated. The tail
+does not prove that no tool changes occurred in the unscanned middle. A summary
+outside these windows remains unknown. Slicer-reported numbers are advisory.
+
+Changing either selected file clears the screen and invalidates an outstanding
+comparison, including late errors. Original files are never rewritten. Existing
+snapshot/freshness and validated physical-binding checks remain in force.
+
+Regression evidence uses synthetic XML/JSON archives and G-code, plus UI
+interaction tests. This repair does not claim a new slicer round-trip or any
+hardware verification.
diff --git a/backend/snapstudio_core/variants/comparison.py b/backend/snapstudio_core/variants/comparison.py
index f0261e26..fd63fb05 100644
--- a/backend/snapstudio_core/variants/comparison.py
+++ b/backend/snapstudio_core/variants/comparison.py
@@ -8,12 +8,17 @@
import re
import zipfile
import zlib
+import xml.etree.ElementTree as ET
from snapstudio_core import localpath
from pathlib import Path
from snapstudio_core import paths, preserve
from snapstudio_core.foundation import artifacts, capabilities, topology
+GCODE_SETTINGS = {"layer_height", "wall_loops", "sparse_infill_density", "support_type",
+ "brim_type", "nozzle_diameter", "temperature", "bed_temperature",
+ "print_speed", "filament_type", "filament_colour"}
+
MAX_GCODE_SCAN = 8 * 1024 * 1024
MAX_3MF_ENTRY = 2 * 1024 * 1024
MAX_ZIP_ENTRIES = 2048
@@ -59,15 +64,17 @@ def _read_gcode(path: Path) -> dict:
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")
+ if truncated:
+ text = text.rsplit("\n", 1)[0] if "\n" in text else "" # never parse a metadata value cut at the scan boundary
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
+ time_priority = -1
for line in text.splitlines():
stripped = line.strip()
- if not body_started and stripped.startswith(";"):
+ if stripped.startswith(";"):
match = GCODE_WEIGHT_RE.match(stripped)
if match:
grams = _parse_number_list(match.group(1))
@@ -76,7 +83,11 @@ def _read_gcode(path: Path) -> dict:
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))
+ priority = 2 if "(normal mode)" in stripped.lower() else 0 if "(silent mode)" in stripped.lower() else 1
+ seconds = _parse_time(match.group(1))
+ if seconds is not None and priority > time_priority:
+ parsed["time_seconds"] = seconds
+ time_priority = priority
match = GCODE_TOWER_RE.match(stripped)
if match:
parsed["tower"] = _parse_number_list(match.group(1))
@@ -84,16 +95,11 @@ def _read_gcode(path: Path) -> dict:
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"}:
+ if key in GCODE_SETTINGS:
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))
@@ -101,6 +107,31 @@ def _read_gcode(path: Path) -> dict:
if active is not None and active != tool:
changes += 1
active = tool
+ if truncated:
+ # Read a separate bounded tail for slicer summaries; never use it to
+ # infer complete tool changes across the unscanned middle of the file.
+ with path.open("rb") as handle:
+ handle.seek(max(0, size - 256 * 1024))
+ tail = handle.read(256 * 1024).decode("utf-8", errors="replace")
+ for line in tail.splitlines()[1:]:
+ stripped = line.strip()
+ if stripped.startswith(";") and "=" in stripped:
+ key, value = stripped[1:].split("=", 1)
+ if key.strip() in GCODE_SETTINGS:
+ parsed["settings"][key.strip()] = value.strip()[:300]
+ match = GCODE_WEIGHT_RE.match(line.strip())
+ 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(line.strip())
+ if match:
+ priority = 2 if "(normal mode)" in stripped.lower() else 0 if "(silent mode)" in stripped.lower() else 1
+ seconds = _parse_time(match.group(1))
+ if seconds is not None and priority > time_priority:
+ parsed["time_seconds"] = seconds
+ time_priority = priority
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:
@@ -126,6 +157,54 @@ def _read_3mf(path: Path) -> dict:
raise ValueError("A selected 3MF could not be read as a valid archive.") from None
+def _slice_xml(raw: bytes) -> dict:
+ """Read bounded Orca/Bambu plate summaries, preserving incomplete totals as unknown."""
+ try:
+ text = raw.decode("utf-8-sig")
+ except UnicodeDecodeError:
+ return {}
+ if "\x00" in text or " 256:
+ return {}
+ totals: dict[int, float] = {}
+ grams_known = True
+ times = []
+ for plate in plates:
+ filaments = plate.findall("filament")
+ if not filaments or len(filaments) > 256:
+ grams_known = False
+ seen = set()
+ for filament in filaments:
+ try:
+ index = int(filament.attrib["id"]) - 1
+ amount = float(filament.attrib["used_g"])
+ if not 0 <= index < 256 or index in seen or not _finite_amount(amount):
+ raise ValueError()
+ except (KeyError, ValueError, OverflowError):
+ grams_known = False
+ continue
+ seen.add(index)
+ totals[index] = totals.get(index, 0.0) + amount
+ predictions = [m.get("value") for m in plate.findall("metadata") if m.get("key") == "prediction"]
+ try:
+ seconds = float(predictions[0]) if len(predictions) == 1 else None
+ except (TypeError, ValueError, OverflowError):
+ seconds = None
+ times.append(seconds)
+ result = {}
+ if grams_known and totals and all(_finite_amount(v) for v in totals.values()):
+ result["filament_used_g"] = [totals.get(i, 0.0) for i in range(max(totals) + 1)]
+ if all(_finite_amount(t) for t in times) and _finite_amount(sum(times)):
+ result["time_seconds"] = sum(times)
+ return result
+
+
def _read_3mf_archive(path: Path) -> dict:
settings: dict = {}
slice_info: dict = {}
@@ -145,7 +224,9 @@ def _read_3mf_archive(path: Path) -> dict:
try:
value = json.loads(raw.decode("utf-8"))
except (ValueError, RecursionError):
- continue # readable bytes that are not JSON: the metadata stays unknown
+ if name == "Metadata/slice_info.config":
+ target.update(_slice_xml(raw))
+ continue
if isinstance(value, dict):
target.update(value)
parsed = {"settings": settings, "reported_tool_indices": None, "grams_by_tool": None,
diff --git a/backend/tests/variants/test_real_slicer_metadata.py b/backend/tests/variants/test_real_slicer_metadata.py
new file mode 100644
index 00000000..4f36dd4e
--- /dev/null
+++ b/backend/tests/variants/test_real_slicer_metadata.py
@@ -0,0 +1,68 @@
+import json, zipfile
+from snapstudio_core.variants.comparison import _read_3mf, _read_gcode, MAX_GCODE_SCAN
+
+def archive(tmp_path, text):
+ p=tmp_path/"sliced.3mf"
+ with zipfile.ZipFile(p,"w") as z:
+ z.writestr("Metadata/project_settings.config",json.dumps({"layer_height":"0.2"}))
+ z.writestr("Metadata/slice_info.config",text)
+ return p
+
+def test_xml_slice_info_aggregates_plates_by_filament_id(tmp_path):
+ p=archive(tmp_path,'')
+ r=_read_3mf(p)
+ assert r["grams_by_tool"] == [15.0,1.5]
+ assert r["time_seconds"] == 180
+
+def test_unknown_plate_usage_does_not_become_a_complete_total(tmp_path):
+ p=archive(tmp_path,'')
+ assert _read_3mf(p)["grams_by_tool"] is None
+
+def test_bad_xml_does_not_crash_or_expand_entities(tmp_path):
+ for text in ['', ']>&a;']:
+ r=_read_3mf(archive(tmp_path,text))
+ assert r["grams_by_tool"] is None and r["time_seconds"] is None
+
+def test_metadata_at_end_of_gcode_is_read(tmp_path):
+ p=tmp_path/"sliced.gcode"
+ p.write_text("G28\nT0\nG1 X1\n; filament used [g] = 12.5, 1.5\n; estimated printing time (normal mode) = 1h 1m\n",encoding="utf-8")
+ r=_read_gcode(p)
+ assert r["grams_by_tool"] == [12.5,1.5]
+ assert r["time_seconds"] == 3660
+
+def test_tail_summary_does_not_make_truncated_body_tool_counts_known(tmp_path):
+ p=tmp_path/"large.gcode"
+ with p.open("wb") as f:
+ f.write(b"T0\n")
+ f.write(b"; filler\n"*(MAX_GCODE_SCAN//8+1))
+ f.write(b"; filament used [g] = 4.5\n; estimated printing time (normal mode) = 2m\n")
+ r=_read_gcode(p)
+ assert r["grams_by_tool"] == [4.5]
+ assert r["time_seconds"] == 120
+ assert r["scan"]["tool_changes"] is None
+
+def test_utf16_entity_document_is_not_evaluated(tmp_path):
+ raw = ']>'.encode("utf-16")
+ result = _read_3mf(archive(tmp_path, raw))
+ assert result["time_seconds"] is None
+
+def test_incomplete_time_and_duplicate_filaments_stay_unknown(tmp_path):
+ result = _read_3mf(archive(tmp_path, ''))
+ assert result["grams_by_tool"] is None
+ assert result["time_seconds"] is None
+
+def test_bomless_utf16_entity_documents_are_not_evaluated(tmp_path):
+ xml = ']>'
+ for encoding in ("utf-16-le", "utf-16-be"):
+ assert _read_3mf(archive(tmp_path, xml.encode(encoding)))["time_seconds"] is None
+
+def test_large_footer_keeps_settings_and_prefers_normal_time(tmp_path):
+ p = tmp_path / "large-footer.gcode"
+ with p.open("wb") as f:
+ f.write(b"; filament used [g] = 2\n; estimated printing time (normal mode) = 1m\nT0\n")
+ f.write(b"; filler\n" * (MAX_GCODE_SCAN // 8 + 1))
+ f.write(b"; layer_height = 0.16\n; filament used [g] = bad\n; estimated printing time (silent mode) = 9m\n")
+ result = _read_gcode(p)
+ assert result["settings"]["layer_height"] == "0.16"
+ assert result["grams_by_tool"] == [2]
+ assert result["time_seconds"] == 60
diff --git a/desktop/src/lib/variantsInteraction.test.tsx b/desktop/src/lib/variantsInteraction.test.tsx
new file mode 100644
index 00000000..c6bb93e7
--- /dev/null
+++ b/desktop/src/lib/variantsInteraction.test.tsx
@@ -0,0 +1,39 @@
+// @vitest-environment jsdom
+import React from "react";
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, expect, it, vi } from "vitest";
+import route from "@/routes/ext/variants.route";
+import { compareVariants } from "./variantsApi";
+vi.mock("@/lib/variantsApi", () => ({ compareVariants: vi.fn() }));
+vi.mock("@tauri-apps/plugin-dialog", () => ({ open: vi.fn() }));
+afterEach(() => { cleanup(); vi.clearAllMocks(); });
+const output = { artifact: { artifact_id: "synthetic-artifact" }, freshness: "current", format: "gcode", tools_used: { value: [0], status: "computed" }, tool_changes: { value: 0, status: "computed" }, grams_by_tool: [], time: { value: 60, status: "reported" }, waste: { value: null, status: "unknown" } };
+const report = { outputs: [output, output], stale: false, settings_diff: [], advisory: "old-report" };
+function choose() {
+ render();
+ fireEvent.change(screen.getByLabelText("Output 1 path"), { target: { value: "first.gcode" } });
+ fireEvent.change(screen.getByLabelText("Output 2 path"), { target: { value: "second.gcode" } });
+ fireEvent.click(screen.getByText("Compare outputs"));
+}
+it("discards results for files that have since changed", async () => {
+ let finish!: (r: never) => void;
+ vi.mocked(compareVariants).mockReturnValue(new Promise(resolve => { finish = resolve; }));
+ choose();
+ fireEvent.change(screen.getByLabelText("Output 1 path"), { target: { value: "new.gcode" } });
+ await act(async () => finish(report as never));
+ expect(screen.queryByText("old-report")).toBeNull();
+ expect(screen.getByText("Compare outputs").hasAttribute("disabled")).toBe(false);
+});
+it("discards errors for files that have since changed", async () => {
+ let fail!: (r: Error) => void;
+ vi.mocked(compareVariants).mockReturnValue(new Promise((_, reject) => { fail = reject; }));
+ choose();
+ fireEvent.change(screen.getByLabelText("Output 2 path"), { target: { value: "new.gcode" } });
+ await act(async () => fail(new Error("old-error")));
+ expect(screen.queryByRole("alert")).toBeNull();
+});
+it("renders a completed comparison for unchanged inputs", async () => {
+ vi.mocked(compareVariants).mockResolvedValue(report as never);
+ choose();
+ expect(await screen.findByText("old-report")).toBeTruthy();
+});
diff --git a/desktop/src/routes/ext/variants.route.tsx b/desktop/src/routes/ext/variants.route.tsx
index 02ae9c93..5bb9e485 100644
--- a/desktop/src/routes/ext/variants.route.tsx
+++ b/desktop/src/routes/ext/variants.route.tsx
@@ -1,4 +1,4 @@
-import { useState, type ReactNode } from "react";
+import { useEffect, useRef, useState, type ReactNode } from "react";
import { open } from "@tauri-apps/plugin-dialog";
import { GitCompareArrows } from "lucide-react";
import { Button } from "@/components/ui/button";
@@ -29,11 +29,15 @@ function OutputCard({ output, name }: { output: VariantOutput; name: string }) {
}
function VariantsView() {
+ const revision = useRef(0);
+ useEffect(() => () => { revision.current += 1; }, []);
const [files, setFiles] = useState<[string | null, string | null]>([null, null]);
const [result, setResult] = useState(null);
const [error, setError] = useState(null);
const [busy, setBusy] = useState(false);
function setPath(index: 0 | 1, value: string | null) {
+ revision.current += 1;
+ setBusy(false); setError(null);
setFiles((current) => { const next = [...current] as [string | null, string | null]; next[index] = value; return next; });
setResult(null);
}
@@ -45,10 +49,14 @@ function VariantsView() {
}
async function compare() {
if (!files[0] || !files[1]) return;
+ const request = ++revision.current;
setBusy(true); setError(null); setResult(null);
- try { setResult(await compareVariants({ outputs: [{ path: files[0] }, { path: files[1] }] })); }
- catch (reason) { setError(reason instanceof Error ? reason.message : "The comparison could not be computed."); }
- finally { setBusy(false); }
+ try {
+ const report = await compareVariants({ outputs: [{ path: files[0] }, { path: files[1] }] });
+ if (request === revision.current) setResult(report);
+ } catch (reason) {
+ if (request === revision.current) setError(reason instanceof Error ? reason.message : "The comparison could not be computed.");
+ } finally { if (request === revision.current) setBusy(false); }
}
return