From 4067ef70e2fde72c6f4218b471ab141e4852e143 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 22 Nov 2026 10:18:02 +0100 Subject: [PATCH 1/3] fix(control): stop one late poll from reading as lost readings The control journal samples at its ticker's time, and GET /api/status takes its clock before it waits for the controller's lock. A battery poll the store took in between was newer than that clock, so the verdict called the reading stale, and a stale reading became readings_lost (no_contact, alarm) at once. On a Sungrow hybrid polled once a second this gave about 23 one-sample alarms a day. Judge each device at the latest of the caller's clock and the store's own stamps on what it read. Count readings as lost once no measured power has come for 30 s, or 2 min for a charger, the same grace a new order gets, bounded by the driver's last good poll so cached values from a driver the watchdog gave up on do not hold off the alarm. Dispatch and the stale site meter stop are unchanged. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Fredrik Ahlgren --- .changeset/readings-lost-sustained-gap.md | 17 ++ go/internal/api/api_control_feedback.go | 42 ++++- go/internal/api/api_control_feedback_test.go | 10 +- .../api/api_control_readings_lost_test.go | 165 ++++++++++++++++++ 4 files changed, 226 insertions(+), 8 deletions(-) create mode 100644 .changeset/readings-lost-sustained-gap.md create mode 100644 go/internal/api/api_control_readings_lost_test.go diff --git a/.changeset/readings-lost-sustained-gap.md b/.changeset/readings-lost-sustained-gap.md new file mode 100644 index 000000000..2e420e319 --- /dev/null +++ b/.changeset/readings-lost-sustained-gap.md @@ -0,0 +1,17 @@ +--- +"ftw": patch +--- + +Stop one late poll from reading as "Lost control". + +A home box with a Sungrow hybrid logged 341 "Lost control" alarms for its +battery in two weeks, each one 5 s journal sample long. The journal samples +at its timer's time, and the status page takes its clock before it waits +for the controller. A battery poll stored in between looked newer than the +clock, so Core called the reading stale, and a stale reading became lost +readings at once. Core now judges a device at the latest of its own clock +and the store's stamps on what it read. Readings count as lost once no +measured power has come for 30 s, or 2 min for a charger, the same grace a +new order gets. A reading over a minute old, or a driver the watchdog gave +up on, is still lost at once. Dispatch and the stale site meter stop are +unchanged. diff --git a/go/internal/api/api_control_feedback.go b/go/internal/api/api_control_feedback.go index a99924e23..58150d9ca 100644 --- a/go/internal/api/api_control_feedback.go +++ b/go/internal/api/api_control_feedback.go @@ -178,6 +178,7 @@ func (s *Server) controlFeedback(now time.Time) []ControlFeedback { for _, kind := range []telemetry.DerType{telemetry.DerBattery, telemetry.DerEV, telemetry.DerPV, telemetry.DerV2X} { for _, rd := range s.deps.Tel.ReadingsByType(kind) { cmd, commanded := s.deps.Tel.CommandEvidence(rd.Driver, kind.String()) + now := rowClock(now, rd.UpdatedAt, cmd.LastObservation) _, loadpointOwned := lps[rd.Driver] manualBattery := held && kind == telemetry.DerBattery && (hold.Driver == "" || hold.Driver == rd.Driver) // Measurements inform the site comparison, but only controlled @@ -372,7 +373,7 @@ func (s *Server) controlFeedback(now time.Time) []ControlFeedback { f.SiteDeltaW, f.DeviceDeltaW, f.SiteBeforeW, f.SiteAfterW = nil, nil, nil, nil f.SiteBeforeAtMs, f.SiteAfterAtMs = 0, 0 } - f.VerificationLost = controlVerificationLost(f, cmd, now) + f.VerificationLost = controlVerificationLost(f, cmd, lastHeard(cmd, s.deps.Tel.DriverHealth(rd.Driver)), now) if f.VerificationLost { f.Reason = "readings_lost" } @@ -482,7 +483,11 @@ func evAwayIdle(lp loadpoint.State, requestedW *float64) bool { return !lp.PluggedIn && !lp.ManualActive && (requestedW == nil || *requestedW <= 0) } -func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, now time.Time) bool { +// controlVerificationLost reports readings lost once no measured power has +// come for the grace a new order gets: 30 s, or 2 min for a charger, whose +// cloud may re-read once a minute. One odd poll is not lost control. heard +// is when the box last heard the device (see lastHeard). +func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, heard, now time.Time) bool { // Failed or released calls have their own status. They do not by themselves // mean that a previously measured device stopped supplying power readings. if cmd.Result != "accepted" || cmd.LastMeasuredAt.IsZero() || f.VerificationTier != nil && *f.VerificationTier >= 1 { @@ -492,16 +497,43 @@ func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, n case "observe_only", "disabled", "device_control", "not_connected", "idle", "no_command": return false } - if f.Reason == "telemetry_stale" { - return true - } grace := 30 * time.Second if f.Kind == "ev" { grace = 2 * time.Minute } + // Stale readings say nothing about the order, only about the device. + if f.Reason == "telemetry_stale" { + return now.Sub(heard) >= grace + } return !cmd.Since.IsZero() && now.Sub(cmd.Since) >= grace && now.Sub(cmd.LastMeasuredAt) >= grace } +// lastHeard is when the box last measured the device's power, but no later +// than its driver's last good poll: a driver the watchdog gave up on may go +// on storing cached values, and those are not news from the device. +func lastHeard(cmd telemetry.CommandEvidence, h *telemetry.DriverHealth) time.Time { + heard := cmd.LastMeasuredAt + if h != nil && h.LastSuccess != nil && h.LastSuccess.Before(heard) { + heard = *h.LastSuccess + } + return heard +} + +// rowClock judges one device at the latest of the caller's clock and the +// store's own stamps on what it read. The journal samples at its ticker's +// time, and GET /api/status takes its clock before it waits for the +// controller's lock; a driver can store a poll in between. The store stamps +// with the box's clock, so such a reading is the newest there is, not one +// from the future. The result never runs ahead of the present. +func rowClock(now time.Time, stamps ...time.Time) time.Time { + for _, t := range stamps { + if t.After(now) { + now = t + } + } + return now +} + // evMismatchWait is how long a charger's fresh readings must disagree with // the orders in force before it is not following. evOverdrawAlarm is how // much longer one that draws more than ordered may go on before the warning diff --git a/go/internal/api/api_control_feedback_test.go b/go/internal/api/api_control_feedback_test.go index bf694dd83..af98dd3da 100644 --- a/go/internal/api/api_control_feedback_test.go +++ b/go/internal/api/api_control_feedback_test.go @@ -138,7 +138,11 @@ func TestControlVerificationLossAlarm(t *testing.T) { {"startup", "waiting_response", "battery", &zero, 0, time.Minute, false}, {"new command settling", "waiting_response", "battery", &zero, time.Minute, 5 * time.Second, false}, {"lost measured response", "waiting_response", "battery", &zero, time.Minute, time.Minute, true}, - {"device offline", "telemetry_stale", "battery", &zero, 20 * time.Second, time.Minute, true}, + {"one stale sample", "telemetry_stale", "battery", &zero, 5 * time.Second, time.Minute, false}, + {"stale under a new order", "telemetry_stale", "battery", &zero, 40 * time.Second, 5 * time.Second, true}, + {"device offline", "telemetry_stale", "battery", &zero, 40 * time.Second, time.Minute, true}, + {"charger stale within its grace", "telemetry_stale", "ev", &zero, time.Minute, 3 * time.Minute, false}, + {"charger offline", "telemetry_stale", "ev", &zero, 3 * time.Minute, 3 * time.Minute, true}, {"EV still settling", "waiting_response", "ev", &zero, time.Minute, time.Minute, false}, {"EV lost response", "response_unknown", "ev", &zero, 3 * time.Minute, 3 * time.Minute, true}, {"unplugged", "not_connected", "ev", &zero, 3 * time.Minute, 3 * time.Minute, false}, @@ -151,7 +155,7 @@ func TestControlVerificationLossAlarm(t *testing.T) { cmd.LastMeasuredAt = now.Add(-tc.verified) } f := ControlFeedback{Reason: tc.reason, Kind: tc.kind, VerificationTier: tc.tier} - if got := controlVerificationLost(f, cmd, now); got != tc.want { + if got := controlVerificationLost(f, cmd, cmd.LastMeasuredAt, now); got != tc.want { t.Fatalf("alarm=%v want %v", got, tc.want) } }) @@ -247,7 +251,7 @@ func TestFailedCallIsNotLostMeasurement(t *testing.T) { now := time.Now() for _, result := range []string{"failed", "unconfirmed", "default_failed", "pending", "released"} { cmd := telemetry.CommandEvidence{Result: result, Since: now.Add(-time.Minute), LastMeasuredAt: now.Add(-time.Minute)} - if controlVerificationLost(ControlFeedback{Reason: "command_failed", ActualW: watts(-4400)}, cmd, now) { + if controlVerificationLost(ControlFeedback{Reason: "command_failed", ActualW: watts(-4400)}, cmd, cmd.LastMeasuredAt, now) { t.Fatalf("%s call was relabelled as lost measurements", result) } } diff --git a/go/internal/api/api_control_readings_lost_test.go b/go/internal/api/api_control_readings_lost_test.go new file mode 100644 index 000000000..3e219807f --- /dev/null +++ b/go/internal/api/api_control_readings_lost_test.go @@ -0,0 +1,165 @@ +package api + +import ( + "fmt" + "testing" + "testing/synctest" + "time" + + "github.com/srcfl/ftw/go/internal/telemetry" +) + +// Field record, 8-22 Nov 2026, a Sungrow SH hybrid polled once a second: the +// control journal held 341 readings_lost alarms for the battery, each one +// 5 s journal sample long, about 23 a day, and none longer than 40 s. One +// gap in what a single sample sees is not lost readings. Each case below +// breaks one poll of a battery that follows its order; no sample may then +// alarm, and the verdict must come back. +func TestOnePollGapIsNotReadingsLost(t *testing.T) { + for _, tc := range []struct { + name string + // gap stores the broken poll and returns the clock the sample uses. + gap func(h *hybridTrace) time.Time + }{ + // The journal samples at its ticker's time, and GET /api/status takes + // its clock before it waits for the controller's lock. A poll the + // store takes in between is newer than that clock, not stale. + {"poll stored after the sample's clock", func(h *hybridTrace) time.Time { + now := time.Now() + time.Sleep(time.Millisecond) + h.poll() + return now + }}, + {"failed read, nothing stored", func(h *hybridTrace) time.Time { + h.tel.RecordDriverError("hybrid", "modbus: i/o timeout") + return time.Now() + }}, + {"power not available once", func(h *hybridTrace) time.Time { + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(`{"control_power_available":false}`)) + return time.Now() + }}, + {"power older than its max age once", func(h *hybridTrace) time.Time { + at := time.Now().Add(-61 * time.Second).Format(time.RFC3339Nano) + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(fmt.Sprintf(`{"setpoint_w":%g,"power_observed_at":%q}`, h.cmdW, at))) + return time.Now() + }}, + {"driver says offline once", func(h *hybridTrace) time.Time { + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(fmt.Sprintf(`{"setpoint_w":%g,"is_online":false}`, h.cmdW))) + return time.Now() + }}, + } { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + h := newHybridTrace() + h.houseW, h.batW = 600, -3000 + h.command(-3000) + for range 90 { + time.Sleep(time.Second) + h.poll() + } + if f := h.battery(time.Now()); f.Status != "following" { + t.Fatalf("set-up battery not following: %s/%s", f.Status, f.Reason) + } + time.Sleep(time.Second) + at := tc.gap(h) + seen := []ControlFeedback{h.battery(at)} + for range 30 { + time.Sleep(time.Second) + h.poll() + seen = append(seen, h.battery(time.Now())) + } + for i, f := range seen { + if f.Reason == "readings_lost" || f.Severity == "alarm" || f.VerificationLost { + t.Fatalf("sample %d after one broken poll: %s/%s/%s", i, f.Status, f.Reason, f.Severity) + } + } + if f := seen[len(seen)-1]; f.Status != "following" || f.Reason != "power_observed" { + t.Fatalf("verdict did not come back: %s/%s", f.Status, f.Reason) + } + }) + }) + } +} + +// Readings that stay away are still lost, 30 s after the last measured power, +// the grace a new order gets. +func TestSustainedGapIsReadingsLost(t *testing.T) { + for _, tc := range []struct { + name string + // poll stores what the driver sends each second while the gap lasts. + poll func(h *hybridTrace) + // lostBy is when the alarm must stand; earlier samples must not alarm. + lostBy time.Duration + }{ + {"driver stores nothing", func(h *hybridTrace) {}, 31 * time.Second}, + // The watchdog gives up after 60 s without a good poll. Cached values + // the driver keeps storing do not hold off the alarm after that. + {"driver stores cached values only", func(h *hybridTrace) { h.poll() }, 61 * time.Second}, + } { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + h := newHybridTrace() + h.houseW, h.batW = 600, -3000 + h.command(-3000) + for range 90 { + time.Sleep(time.Second) + h.poll() + h.tel.RecordDriverSuccess("hybrid") + } + start := time.Now() + for time.Since(start) < 70*time.Second { + time.Sleep(time.Second) + tc.poll(h) + h.tel.WatchdogScan(time.Minute) + f, gap := h.battery(time.Now()), time.Since(start) + if gap < 30*time.Second && (f.Severity == "alarm" || f.Reason == "readings_lost") { + t.Fatalf("%v into the gap: %s/%s/%s", gap, f.Status, f.Reason, f.Severity) + } + if gap >= tc.lostBy && (f.Reason != "readings_lost" || f.Status != "no_contact" || f.Severity != "alarm") { + t.Fatalf("%v into the gap: %s/%s/%s, want no_contact/readings_lost/alarm", gap, f.Status, f.Reason, f.Severity) + } + } + }) + }) + } +} + +// Easee's cloud sends op_mode 0 when it has not heard from the charger, and +// then stops vouching for the last power value. One such poll is a warning; +// two minutes of them, a charger's grace, are lost readings. +func TestCloudChargerOfflineIsLostAfterItsGrace(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + e := newEVTrace(7000) + for range 24 { + e.tick(7000, "plan") + } + if f := e.verdict(t); f.Status != "following" { + t.Fatalf("set-up charger not following: %s/%s", f.Status, f.Reason) + } + start := time.Now() + for time.Since(start) < 3*time.Minute { + c := e.tel.BeginCommand("easee", []byte(`{"action":"ev_set_current","power_w":7000}`), time.Now()) + e.tel.CompleteCommand(c, "accepted") + e.tel.Update("easee", telemetry.DerEV, 7000, nil, []byte(fmt.Sprintf( + `{"connected":true,"is_online":false,"control_power_observed_at":%q,"power_max_age_s":180}`, e.changedAt.Format(time.RFC3339Nano)))) + f, gap := e.verdict(t), time.Since(start) + if gap < 110*time.Second && (f.Reason != "telemetry_stale" || f.Severity != "warning") { + t.Fatalf("%v offline: %s/%s/%s, want telemetry_stale warning", gap, f.Status, f.Reason, f.Severity) + } + if gap >= 2*time.Minute+5*time.Second && (f.Reason != "readings_lost" || f.Severity != "alarm") { + t.Fatalf("%v offline: %s/%s/%s, want readings_lost alarm", gap, f.Status, f.Reason, f.Severity) + } + time.Sleep(5 * time.Second) + } + }) +} + +// battery is the battery row of the verdict at now. +func (h *hybridTrace) battery(now time.Time) ControlFeedback { + for _, f := range h.srv.controlFeedback(now) { + if f.Kind == "battery" { + return f + } + } + return ControlFeedback{} +} From cb93468dbac7602a4cbd0dcfd8e6126c655fd48c Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 22 Nov 2026 10:56:31 +0100 Subject: [PATCH 2/3] fix(control): give stale readings the common lost-readings grace Drop the last-good-poll cap: every emit records a good poll, so it never differed from the last measured power in production. Stale readings now use the rule every other lost-proof path uses: lost once both the last measured power and the current order step are older than the grace. Measured proof restarts its 10 s window after a reading without power or a new step of 500 W or more, so it can lag healthy readings; counting from the step keeps one late poll under a stepping target from alarming. Add repeated one-poll gap tests and rename the new-step test row. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Fredrik Ahlgren --- .changeset/readings-lost-sustained-gap.md | 13 ++-- go/internal/api/api_control_feedback.go | 27 ++----- go/internal/api/api_control_feedback_test.go | 6 +- .../api/api_control_readings_lost_test.go | 73 ++++++++++++++++++- 4 files changed, 86 insertions(+), 33 deletions(-) diff --git a/.changeset/readings-lost-sustained-gap.md b/.changeset/readings-lost-sustained-gap.md index 2e420e319..499243e82 100644 --- a/.changeset/readings-lost-sustained-gap.md +++ b/.changeset/readings-lost-sustained-gap.md @@ -9,9 +9,10 @@ battery in two weeks, each one 5 s journal sample long. The journal samples at its timer's time, and the status page takes its clock before it waits for the controller. A battery poll stored in between looked newer than the clock, so Core called the reading stale, and a stale reading became lost -readings at once. Core now judges a device at the latest of its own clock -and the store's stamps on what it read. Readings count as lost once no -measured power has come for 30 s, or 2 min for a charger, the same grace a -new order gets. A reading over a minute old, or a driver the watchdog gave -up on, is still lost at once. Dispatch and the stale site meter stop are -unchanged. +readings at once. A charger that said it was offline for one poll did the +same. Core now judges a device at the latest of its own clock and the +store's stamps on what it read. Readings count as lost once neither +measured power nor a new order has come for 30 s, or 2 min for a charger, +the same grace a new order gets. A reading over a minute old, or a driver +the watchdog gave up on, is still lost at once. Dispatch and the stale site +meter stop are unchanged. diff --git a/go/internal/api/api_control_feedback.go b/go/internal/api/api_control_feedback.go index 58150d9ca..16842e3b8 100644 --- a/go/internal/api/api_control_feedback.go +++ b/go/internal/api/api_control_feedback.go @@ -373,7 +373,7 @@ func (s *Server) controlFeedback(now time.Time) []ControlFeedback { f.SiteDeltaW, f.DeviceDeltaW, f.SiteBeforeW, f.SiteAfterW = nil, nil, nil, nil f.SiteBeforeAtMs, f.SiteAfterAtMs = 0, 0 } - f.VerificationLost = controlVerificationLost(f, cmd, lastHeard(cmd, s.deps.Tel.DriverHealth(rd.Driver)), now) + f.VerificationLost = controlVerificationLost(f, cmd, now) if f.VerificationLost { f.Reason = "readings_lost" } @@ -483,11 +483,11 @@ func evAwayIdle(lp loadpoint.State, requestedW *float64) bool { return !lp.PluggedIn && !lp.ManualActive && (requestedW == nil || *requestedW <= 0) } -// controlVerificationLost reports readings lost once no measured power has -// come for the grace a new order gets: 30 s, or 2 min for a charger, whose -// cloud may re-read once a minute. One odd poll is not lost control. heard -// is when the box last heard the device (see lastHeard). -func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, heard, now time.Time) bool { +// controlVerificationLost reports readings lost once neither measured power +// nor a new order has come for the grace a new order gets: 30 s, or 2 min +// for a charger, whose cloud may re-read once a minute. Stale readings get +// the same grace as missing power: one odd poll is not lost control. +func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, now time.Time) bool { // Failed or released calls have their own status. They do not by themselves // mean that a previously measured device stopped supplying power readings. if cmd.Result != "accepted" || cmd.LastMeasuredAt.IsZero() || f.VerificationTier != nil && *f.VerificationTier >= 1 { @@ -501,24 +501,9 @@ func controlVerificationLost(f ControlFeedback, cmd telemetry.CommandEvidence, h if f.Kind == "ev" { grace = 2 * time.Minute } - // Stale readings say nothing about the order, only about the device. - if f.Reason == "telemetry_stale" { - return now.Sub(heard) >= grace - } return !cmd.Since.IsZero() && now.Sub(cmd.Since) >= grace && now.Sub(cmd.LastMeasuredAt) >= grace } -// lastHeard is when the box last measured the device's power, but no later -// than its driver's last good poll: a driver the watchdog gave up on may go -// on storing cached values, and those are not news from the device. -func lastHeard(cmd telemetry.CommandEvidence, h *telemetry.DriverHealth) time.Time { - heard := cmd.LastMeasuredAt - if h != nil && h.LastSuccess != nil && h.LastSuccess.Before(heard) { - heard = *h.LastSuccess - } - return heard -} - // rowClock judges one device at the latest of the caller's clock and the // store's own stamps on what it read. The journal samples at its ticker's // time, and GET /api/status takes its clock before it waits for the diff --git a/go/internal/api/api_control_feedback_test.go b/go/internal/api/api_control_feedback_test.go index af98dd3da..a1094f795 100644 --- a/go/internal/api/api_control_feedback_test.go +++ b/go/internal/api/api_control_feedback_test.go @@ -139,7 +139,7 @@ func TestControlVerificationLossAlarm(t *testing.T) { {"new command settling", "waiting_response", "battery", &zero, time.Minute, 5 * time.Second, false}, {"lost measured response", "waiting_response", "battery", &zero, time.Minute, time.Minute, true}, {"one stale sample", "telemetry_stale", "battery", &zero, 5 * time.Second, time.Minute, false}, - {"stale under a new order", "telemetry_stale", "battery", &zero, 40 * time.Second, 5 * time.Second, true}, + {"stale just after a new step waits for its grace", "telemetry_stale", "battery", &zero, 40 * time.Second, 5 * time.Second, false}, {"device offline", "telemetry_stale", "battery", &zero, 40 * time.Second, time.Minute, true}, {"charger stale within its grace", "telemetry_stale", "ev", &zero, time.Minute, 3 * time.Minute, false}, {"charger offline", "telemetry_stale", "ev", &zero, 3 * time.Minute, 3 * time.Minute, true}, @@ -155,7 +155,7 @@ func TestControlVerificationLossAlarm(t *testing.T) { cmd.LastMeasuredAt = now.Add(-tc.verified) } f := ControlFeedback{Reason: tc.reason, Kind: tc.kind, VerificationTier: tc.tier} - if got := controlVerificationLost(f, cmd, cmd.LastMeasuredAt, now); got != tc.want { + if got := controlVerificationLost(f, cmd, now); got != tc.want { t.Fatalf("alarm=%v want %v", got, tc.want) } }) @@ -251,7 +251,7 @@ func TestFailedCallIsNotLostMeasurement(t *testing.T) { now := time.Now() for _, result := range []string{"failed", "unconfirmed", "default_failed", "pending", "released"} { cmd := telemetry.CommandEvidence{Result: result, Since: now.Add(-time.Minute), LastMeasuredAt: now.Add(-time.Minute)} - if controlVerificationLost(ControlFeedback{Reason: "command_failed", ActualW: watts(-4400)}, cmd, cmd.LastMeasuredAt, now) { + if controlVerificationLost(ControlFeedback{Reason: "command_failed", ActualW: watts(-4400)}, cmd, now) { t.Fatalf("%s call was relabelled as lost measurements", result) } } diff --git a/go/internal/api/api_control_readings_lost_test.go b/go/internal/api/api_control_readings_lost_test.go index 3e219807f..3b0cad427 100644 --- a/go/internal/api/api_control_readings_lost_test.go +++ b/go/internal/api/api_control_readings_lost_test.go @@ -92,9 +92,13 @@ func TestSustainedGapIsReadingsLost(t *testing.T) { lostBy time.Duration }{ {"driver stores nothing", func(h *hybridTrace) {}, 31 * time.Second}, - // The watchdog gives up after 60 s without a good poll. Cached values - // the driver keeps storing do not hold off the alarm after that. - {"driver stores cached values only", func(h *hybridTrace) { h.poll() }, 61 * time.Second}, + // Every emit counts as a good poll (drivers.HostEnv.emitTelemetry), + // so the watchdog never gives up on this one; its readings stop + // being measurements, and that is lost after the same grace. + {"driver says offline on every poll", func(h *hybridTrace) { + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(`{"control_power_available":false,"is_online":false}`)) + h.tel.RecordDriverSuccess("hybrid") + }, 31 * time.Second}, } { t.Run(tc.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { @@ -154,6 +158,69 @@ func TestCloudChargerOfflineIsLostAfterItsGrace(t *testing.T) { }) } +// Measured proof needs 10 s of distinct fresh readings, and a reading +// without power, or a new step of 500 W or more, starts that window again. +// So the last measured proof can lag well behind healthy readings. Gaps that +// keep coming, alone or under a target that steps every few seconds, are +// still not lost readings. +func TestRepeatedOnePollGapsAreNotReadingsLost(t *testing.T) { + gaps := []func(h *hybridTrace) time.Time{ + func(h *hybridTrace) time.Time { now := time.Now(); time.Sleep(time.Millisecond); h.poll(); return now }, + func(h *hybridTrace) time.Time { + h.tel.RecordDriverError("hybrid", "modbus: i/o timeout") + return time.Now() + }, + func(h *hybridTrace) time.Time { + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(`{"control_power_available":false}`)) + return time.Now() + }, + func(h *hybridTrace) time.Time { + h.tel.Update("hybrid", telemetry.DerBattery, h.batW, nil, []byte(fmt.Sprintf(`{"setpoint_w":%g,"is_online":false}`, h.cmdW))) + return time.Now() + }, + } + for _, tc := range []struct { + name string + every, step time.Duration + }{ + {"a gap every 25 s", 25 * time.Second, 0}, + {"a gap every 8 s", 8 * time.Second, 0}, + {"a gap every 12 s under 600 W steps every 4 s", 12 * time.Second, 4 * time.Second}, + } { + t.Run(tc.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + h := newHybridTrace() + h.houseW, h.batW = 600, -3000 + h.command(-3000) + for range 90 { + time.Sleep(time.Second) + h.poll() + } + for sec := 1; sec <= 5*60; sec++ { + time.Sleep(time.Second) + if tc.step > 0 && sec%int(tc.step.Seconds()) == 0 { + target := -3000.0 + if h.cmdW == -3000 { + target = -3600 + } + h.command(target) + h.batW = target + } + at := time.Now() + if sec%int(tc.every.Seconds()) == 0 { + at = gaps[(sec/int(tc.every.Seconds()))%len(gaps)](h) + } else { + h.poll() + } + if f := h.battery(at); f.Reason == "readings_lost" || f.Severity == "alarm" { + t.Fatalf("%d s: %s/%s/%s", sec, f.Status, f.Reason, f.Severity) + } + } + }) + }) + } +} + // battery is the battery row of the verdict at now. func (h *hybridTrace) battery(now time.Time) ControlFeedback { for _, f := range h.srv.controlFeedback(now) { From fff49a183628ed397340d85b97d61c6fbb920039 Mon Sep 17 00:00:00 2001 From: Fredrik Ahlgren Date: Sun, 22 Nov 2026 11:06:42 +0100 Subject: [PATCH 3/3] test(control): show readings lost waits for steps on a dead driver While Core keeps stepping a dead driver's order by 600 W every 10 s, each step restarts the 30 s wait, so readings_lost comes 30 s after the last step. The verdict says no contact (telemetry_stale, warning) the whole time. In the field the watchdog hands the battery back to its own control after 60 s, so the steps stop. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Fredrik Ahlgren --- .../api/api_control_readings_lost_test.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/go/internal/api/api_control_readings_lost_test.go b/go/internal/api/api_control_readings_lost_test.go index 3b0cad427..eb9915d77 100644 --- a/go/internal/api/api_control_readings_lost_test.go +++ b/go/internal/api/api_control_readings_lost_test.go @@ -221,6 +221,54 @@ func TestRepeatedOnePollGapsAreNotReadingsLost(t *testing.T) { } } +// A driver that has died while Core keeps stepping its order by 600 W +// every 10 s. Readings lost waits for 30 s without a new step as well as +// without measured power, so it is late here by design: each step restarts +// the response wait. The stale readings still show as no contact, a +// warning, the whole time, and the alarm comes 30 s after the steps stop. +// In the field the 60 s watchdog hands such a battery back to its own +// control, so the steps stop. +func TestDeadDriverUnderSteppingOrders(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + h := newHybridTrace() + h.houseW, h.batW = 600, -3000 + h.command(-3000) + for range 90 { + time.Sleep(time.Second) + h.poll() + } + died := time.Now() + for sec := 1; sec <= 4*60; sec++ { + time.Sleep(time.Second) + if sec%10 == 0 { + target := -3000.0 + if h.cmdW == -3000 { + target = -3600 + } + h.command(target) + } + f, gap := h.battery(time.Now()), time.Since(died) + if f.Severity == "alarm" || f.Reason == "readings_lost" { + t.Fatalf("%v after the driver died, under steps: %s/%s/%s", gap, f.Status, f.Reason, f.Severity) + } + if gap > 61*time.Second && (f.Status != "no_contact" || f.Reason != "telemetry_stale" || f.Severity != "warning") { + t.Fatalf("%v after the driver died: %s/%s/%s, want no_contact/telemetry_stale/warning", gap, f.Status, f.Reason, f.Severity) + } + } + stopped := time.Now() + for time.Since(stopped) < 40*time.Second { + time.Sleep(time.Second) + f, quiet := h.battery(time.Now()), time.Since(stopped) + if quiet < 30*time.Second && f.Reason != "telemetry_stale" { + t.Fatalf("%v after the last step: %s/%s/%s", quiet, f.Status, f.Reason, f.Severity) + } + if quiet >= 30*time.Second && (f.Reason != "readings_lost" || f.Severity != "alarm") { + t.Fatalf("%v after the last step: %s/%s/%s, want readings_lost alarm", quiet, f.Status, f.Reason, f.Severity) + } + } + }) +} + // battery is the battery row of the verdict at now. func (h *hybridTrace) battery(now time.Time) ControlFeedback { for _, f := range h.srv.controlFeedback(now) {