Parcourir la source

Slice fallback to embedded settings only for the file's printer (#3535)

- A 3MF that fails to slice with the picked profiles is retried with
  its embedded settings only when the file was made for the picked
  printer; otherwise the slicer's error is reported
- Tests for same, different and unknown printers, changelog entry
maziggy il y a 1 semaine
Parent
commit
8fc986fa92

+ 1 - 0
CHANGELOG.md

@@ -27,6 +27,7 @@ All notable changes to Bambuddy will be documented in this file.
 - **"Any model" jobs go to the printer with the fewest print hours (#3480, sponsor vote Q4 2026)** — When several printers of a model can take a job, the queue used to pick the first one in its list, so on a farm the same printer got most of the work. It now picks the one with the fewest print hours (the Maintenance counter, including baseline hours entered there), so jobs and wear spread across the farm. Preferred colours still come first, and the queue powers on the least used printer too when it has to switch one on with an Auto Power On plug.
 
 ### Fixed
+- **A failed slice for another printer is reported as failed (#3535, reported by @Jesse-Cachai)** — When the slicer failed on a 3MF with the picked printer, process and filament profiles, Bambuddy retried with the settings saved in the file and reported the slice as completed. Those settings are for the printer the file was made for, so a P1S project sliced for another printer came back as P1S G-code, with a believable print time and filament estimate. The retry now runs only when the file was made for the picked printer. For any other printer, or when either printer can't be told from the file or the profile, the slice fails with the slicer's error.
 - **A user moved out of Administrators is no longer an admin (#3533)** — Users carry an older admin role next to their groups, and either one made them an admin. The account created during setup has both, so moving it from Administrators to Operators left it with every admin right, still showing the Admin badge, and the user settings had no way to remove the role. The groups now decide: the role follows them wherever they change, in the user settings, on the groups, through the API and at LDAP or OIDC sign-in. Removing the last admin from Administrators is refused, as deactivating them already was. On the first start after the update, users who are not in Administrators lose the old admin role. If they are the only admins, they are added to Administrators instead.
 - **Spool assignments survive a printer reconnecting mid-print (#3526, reported by @SaadShaya7)** — After a short network drop, the first report of a reconnecting printer can show a slot with no filament type and colour `00000000`. Bambuddy only recognised an empty colour as a blank slot, so it read this as a different spool and removed the inventory assignment, even during a print. All four assignments on an A1 mini's AMS lite and an A2L's external spool were lost that way and had to be made again. A slot reporting `00000000` with no type now counts as blank: during a print the assignment is kept as a possible runout, otherwise it is unlinked only if the slot stays blank for two minutes. A clear filament, which also reports `00000000` but with its type, is not affected. The support package also no longer contains a virtual printer's serial number: for the A2L, P2S, H2 series and X2D models it appeared in full in the certificate log lines.
 - **The slicer sidecars run as a non-root user again (#3524, reported by @cubic3d)** — Since 1.3.1, both the Bambu Studio and the OrcaSlicer sidecar failed to slice when the container runs as another user than root, as in a Kubernetes pod with `runAsNonRoot`: the log showed `bambu-studio: Permission denied`. The new slicer releases unpack with folders only root can open; the images now open them to every user, and the image build stops if any folder stays closed. Needs the updated sidecar images.

+ 17 - 0
backend/app/api/routes/library.py

@@ -4569,6 +4569,9 @@ async def _run_slicer_with_fallback(
     # orthogonal so this decision doesn't interact with the #1337 build-
     # plate override.
     cross_class_arrange = False
+    # Also read by the embedded-settings fallback below (#3535).
+    source_model: str | None = None
+    target_model: str | None = None
     if is_3mf:
         from backend.app.services.slicer_3mf_convert import (
             extract_source_printer_model,
@@ -4819,6 +4822,20 @@ async def _run_slicer_with_fallback(
                 # user asked to slice as N, which reads as a Bambuddy bug
                 # rather than a slicer failure. Surface the error instead.
                 raise
+            if not source_model or source_model != target_model:
+                # The embedded settings are for the printer the file was
+                # sliced for. On any other target -- or when either side is
+                # unknown -- the fallback hands back G-code for the wrong
+                # printer as a completed slice (#3535). Surface the error.
+                logger.warning(
+                    "Slicer CLI failed on the --load-settings path for %s (%s); no fallback to embedded "
+                    "settings, which are for %s, not %s",
+                    model_filename,
+                    exc,
+                    source_model or "an unknown printer",
+                    target_model or "an unknown printer",
+                )
+                raise
             logger.warning(
                 "Slicer CLI failed on the --load-settings path for %s (%s); retrying with embedded settings",
                 model_filename,

+ 41 - 8
backend/tests/integration/test_library_slice_api.py

@@ -587,14 +587,30 @@ class TestSliceLibraryFile:
 
     @pytest.mark.asyncio
     @pytest.mark.integration
-    async def test_3mf_falls_back_to_embedded_settings_on_cli_failure(
-        self, async_client: AsyncClient, db_session, slice_test_setup
+    @pytest.mark.parametrize(
+        ("source_model", "target_model", "falls_back"),
+        [
+            # Same printer: the file's own settings are a valid slice for it.
+            ("Bambu Lab X1 Carbon", "Bambu Lab X1 Carbon", True),
+            # #3535: another printer, or one side unknown -- the fallback would
+            # hand back G-code for the file's printer as a completed slice.
+            ("Bambu Lab P1S", "Bambu Lab X1 Carbon", False),
+            (None, "Bambu Lab X1 Carbon", False),
+            ("Bambu Lab X1 Carbon", None, False),
+        ],
+    )
+    async def test_3mf_falls_back_to_embedded_settings_on_cli_failure_only_for_its_own_printer(
+        self, async_client: AsyncClient, db_session, slice_test_setup, source_model, target_model, falls_back
     ):
         # When the slicer CLI fails on the --load-settings path (segfault
         # on complex H2D models), Bambuddy retries with no profile triplet
-        # so the CLI uses the file's embedded settings.
+        # so the CLI uses the file's embedded settings -- which describe the
+        # printer the file was sliced for, so only when that is the target.
+        settings = {"prime_tower_brim_width": "-1"}
+        if source_model:
+            settings["printer_model"] = source_model
         src_3mf_path = slice_test_setup["tmp_path"] / "library" / "files" / "complex.3mf"
-        src_3mf_path.write_bytes(_make_3mf_with_settings({"prime_tower_brim_width": "-1"}))
+        src_3mf_path.write_bytes(_make_3mf_with_settings(settings))
         threemf = LibraryFile(
             filename="complex.3mf",
             file_path=str(src_3mf_path.relative_to(slice_test_setup["tmp_path"])),
@@ -602,8 +618,20 @@ class TestSliceLibraryFile:
             file_size=src_3mf_path.stat().st_size,
         )
         db_session.add(threemf)
+        printer_id = slice_test_setup["printer_id"]
+        if target_model:
+            target = LocalPreset(
+                name="# Target printer 0.4 nozzle",
+                preset_type="printer",
+                source="orcaslicer",
+                setting=json.dumps({"name": "Target printer 0.4 nozzle", "printer_model": target_model}),
+            )
+            db_session.add(target)
         await db_session.commit()
         await db_session.refresh(threemf)
+        if target_model:
+            await db_session.refresh(target)
+            printer_id = target.id
 
         call_count = {"n": 0}
 
@@ -632,7 +660,7 @@ class TestSliceLibraryFile:
         response = await async_client.post(
             f"/api/v1/library/files/{threemf.id}/slice",
             json={
-                "printer_preset_id": slice_test_setup["printer_id"],
+                "printer_preset_id": printer_id,
                 "process_preset_id": slice_test_setup["process_id"],
                 "filament_preset_id": slice_test_setup["filament_id"],
             },
@@ -640,9 +668,14 @@ class TestSliceLibraryFile:
         assert response.status_code == 202
 
         final = await _wait_for_job(async_client, response.json()["job_id"])
-        assert final["status"] == "completed", final
-        assert final["result"]["used_embedded_settings"] is True
-        assert call_count["n"] == 2  # primary + fallback retry
+        if falls_back:
+            assert final["status"] == "completed", final
+            assert final["result"]["used_embedded_settings"] is True
+            assert call_count["n"] == 2  # primary + fallback retry
+        else:
+            assert final["status"] == "failed", final
+            assert final["error_status"] == 502
+            assert call_count["n"] == 1  # no retry with another printer's settings
 
     @pytest.mark.asyncio
     @pytest.mark.integration

+ 10 - 0
backend/tests/unit/test_process_changes_recorded_3497.py

@@ -192,6 +192,16 @@ async def _slice(request: SliceRequest, *, model: str = "stl", embedded_fallback
     with (
         patch("backend.app.api.routes.settings.get_setting", new=AsyncMock(side_effect=_setting)),
         patch("backend.app.services.preset_resolver.resolve_preset_ref", new=AsyncMock(side_effect=_resolve)),
+        # The fallback runs only for the file's own printer (#3535): the
+        # source and the target name the same one.
+        patch(
+            "backend.app.services.slicer_3mf_convert.extract_source_printer_model",
+            return_value="X2D" if embedded_fallback else None,
+        ),
+        patch(
+            "backend.app.api.routes.library._resolve_target_printer_model",
+            new=AsyncMock(return_value="X2D" if embedded_fallback else None),
+        ),
         patch.object(slicer_api_module, "SlicerApiService", return_value=service),
         patch.object(slicer_api_module, "get_stall_timeout_seconds", new=AsyncMock(return_value=60.0)),
         patch("backend.app.api.routes.slicer_presets._fetch_cloud_presets", new=cloud_listing),

+ 10 - 0
backend/tests/unit/test_slice_backstop_wiring_2838.py

@@ -97,6 +97,16 @@ async def _run(
 
     with (
         patch("backend.app.api.routes.settings.get_setting", new=AsyncMock(side_effect=_setting)),
+        # The fallback runs only for the file's own printer (#3535): the
+        # source and the target name the same one.
+        patch(
+            "backend.app.services.slicer_3mf_convert.extract_source_printer_model",
+            return_value="X2D" if embedded_fallback else None,
+        ),
+        patch(
+            "backend.app.api.routes.library._resolve_target_printer_model",
+            new=AsyncMock(return_value="X2D" if embedded_fallback else None),
+        ),
         patch(
             "backend.app.services.preset_resolver.resolve_preset_ref",
             new=AsyncMock(return_value=json.dumps({"name": "x", "from": "system", "type": "machine"})),