From 13cd4ee4ba207e0a64cb09689e887f61fc01303f Mon Sep 17 00:00:00 2001 From: Matt Voska Date: Sat, 7 Nov 2026 09:50:34 -0300 Subject: [PATCH 1/2] fix(monitor): ignore stale layers above the job total --- CHANGELOG.md | 1 + internal/cmd/monitor.go | 3 +- internal/cmd/monitor_test.go | 145 +++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 internal/cmd/monitor_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b9c970d..ded526f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ JSON field names and exit codes are part of the public API. - `preflight` `single_filament` now fails only when the number of `--slot`s differs from the number of filaments in the plate. Single-filament jobs send the same payload as before. ### Fixed +- `monitor --snapshot-at-layer` ignores layer counts above the reported total, preventing a previous job's stale layer from consuming the snapshot during startup. - The bundled skill command reference now includes `timelapse list` and `timelapse pull`, including latest-video selection and output paths. - Preflight distinguishes present but untyped AMS trays from empty slots and points to `bambu ams set` to configure the physical spool. Unknown detection is reported as unknown; the tray gate and exit codes are unchanged. - `slice --strict` rejects slicer warnings, including floating regions, with exit 11 before publishing new output files. JSON errors include the warnings and slicer log path; slicing without `--strict` still reports warnings in the summary. diff --git a/internal/cmd/monitor.go b/internal/cmd/monitor.go index 3b676d5..02bf41f 100644 --- a/internal/cmd/monitor.go +++ b/internal/cmd/monitor.go @@ -111,7 +111,8 @@ func (c *MonitorCmd) Run(g *Globals) error { } lastBlocking = s.Errors.Blocking lastFaults = faults - if c.SnapshotAtLayer > 0 && !snapTaken && s.State == "RUNNING" && s.Job.Layer >= c.SnapshotAtLayer { + // A new job can report RUNNING before its previous job's layer count resets. + if c.SnapshotAtLayer > 0 && !snapTaken && s.State == "RUNNING" && s.Job.Layer >= c.SnapshotAtLayer && s.Job.Layer <= s.Job.TotalLayers { snapTaken = true // one attempt; a failure is reported, not retried if m, _ := model(p); m.Camera != printer.CameraRTSPS { ev := Event{TS: time.Now().Format(time.RFC3339), Event: "message", Message: "snapshot skipped: camera not supported for " + p.Model} diff --git a/internal/cmd/monitor_test.go b/internal/cmd/monitor_test.go new file mode 100644 index 0000000..07d5fb6 --- /dev/null +++ b/internal/cmd/monitor_test.go @@ -0,0 +1,145 @@ +package cmd + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/voska/bambu/internal/printer" +) + +type monitorSequenceConn struct { + printer.Conn + reports []map[string]any + index int + updates chan struct{} +} + +func (c *monitorSequenceConn) State() map[string]any { + s := c.reports[c.index] + if c.index < len(c.reports)-1 { + c.index++ + } + return s +} + +func (c *monitorSequenceConn) Updates() <-chan struct{} { return c.updates } + +func TestMonitorSnapshotLayer(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("shell fake ffmpeg") + } + report := func(state string, stage, layer, total int) map[string]any { + return map[string]any{ + "gcode_state": state, "stg_cur": stage, "subtask_name": "part", + "layer_num": layer, "total_layer_num": total, + } + } + finish := report("FINISH", 255, 75, 75) + for _, tc := range []struct { + name string + reports []map[string]any + wantLayer int + }{ + { + name: "previous job layer during startup", + reports: []map[string]any{ + report("PREPARE", 255, 319, 319), + report("RUNNING", 255, 319, 75), + report("RUNNING", 255, 0, 75), + report("RUNNING", 16, 0, 75), + report("RUNNING", 0, 1, 75), + report("RUNNING", 0, 2, 75), + report("RUNNING", 0, 3, 75), + finish, + }, + wantLayer: 2, + }, + { + name: "join before layer reset", + reports: []map[string]any{ + report("RUNNING", 255, 319, 75), + report("RUNNING", 0, 2, 75), finish, + }, + wantLayer: 2, + }, + { + name: "total not yet reported", + reports: []map[string]any{ + report("RUNNING", 255, 319, 0), + report("RUNNING", 0, 2, 75), finish, + }, + wantLayer: 2, + }, + { + name: "join active print after target", + reports: []map[string]any{ + report("RUNNING", 0, 10, 75), finish, + }, + wantLayer: 10, + }, + { + name: "last layer is valid", + reports: []map[string]any{ + report("RUNNING", 0, 75, 75), finish, + }, + wantLayer: 75, + }, + { + name: "never reaches a valid layer", + reports: []map[string]any{ + report("RUNNING", 255, 319, 75), finish, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + h := newHarness(t, nil) + ffmpeg := "#!/bin/sh\nfor a; do last=\"$a\"; done\nprintf JPEG > \"$last\"\n" + if err := os.WriteFile(filepath.Join(h.dir, "ffmpeg"), []byte(ffmpeg), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", h.dir) + conn := &monitorSequenceConn{Conn: h.conn, reports: tc.reports, updates: make(chan struct{})} + close(conn.updates) + code := Execute([]string{"monitor", "--snapshot-at-layer", "2", "--snapshot-dir", h.dir, "--timeout", "5s", "--json"}, BuildInfo{}, func(g *Globals) { + g.Keyring = h.kr + g.Dial = func(context.Context, string, string, string) (printer.Conn, error) { return conn, nil } + g.Out.Stdout, g.Out.Stderr = &h.stdout, &h.stderr + }) + if code != 0 { + t.Fatalf("exit %d: %s", code, h.stderr.String()) + } + var snapshots []Event + var final Event + for _, line := range strings.Split(strings.TrimSpace(h.stdout.String()), "\n") { + var ev Event + if err := json.Unmarshal([]byte(line), &ev); err != nil { + t.Fatal(err) + } + if ev.Event == "snapshot" { + snapshots = append(snapshots, ev) + } + final = ev + } + if tc.wantLayer == 0 { + if len(snapshots) != 0 || final.Snapshot != "" { + t.Fatalf("unexpected snapshot: %+v, final %+v", snapshots, final) + } + return + } + if len(snapshots) != 1 || snapshots[0].Layer != tc.wantLayer { + t.Fatalf("want one snapshot at layer %d, got %+v", tc.wantLayer, snapshots) + } + if final.Event != "final" || final.Snapshot != snapshots[0].Snapshot { + t.Fatalf("final lost snapshot: %+v", final) + } + if b, err := os.ReadFile(final.Snapshot); err != nil || string(b) != "JPEG" { + t.Fatalf("snapshot not written: %q, %v", b, err) + } + }) + } +} From ada3a9ef306392de4b8bb06ae917ab35baa86494 Mon Sep 17 00:00:00 2001 From: Matt Voska Date: Sat, 7 Nov 2026 09:54:39 -0300 Subject: [PATCH 2/2] docs(monitor): clarify snapshot layer validation --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ded526f..0895bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ JSON field names and exit codes are part of the public API. - `preflight` `single_filament` now fails only when the number of `--slot`s differs from the number of filaments in the plate. Single-filament jobs send the same payload as before. ### Fixed -- `monitor --snapshot-at-layer` ignores layer counts above the reported total, preventing a previous job's stale layer from consuming the snapshot during startup. +- `monitor --snapshot-at-layer` waits for a positive reported total and ignores layer counts above that total, preventing a previous job's stale layer from consuming the snapshot during startup. Snapshots wait while the total is unknown. - The bundled skill command reference now includes `timelapse list` and `timelapse pull`, including latest-video selection and output paths. - Preflight distinguishes present but untyped AMS trays from empty slots and points to `bambu ams set` to configure the physical spool. Unknown detection is reported as unknown; the tray gate and exit codes are unchanged. - `slice --strict` rejects slicer warnings, including floating regions, with exit 11 before publishing new output files. JSON errors include the warnings and slicer log path; slicing without `--strict` still reports warnings in the summary.