Skip to content

fix(history): derive household Load from interval source energy - #1741

Merged
frahlg merged 2 commits into
masterfrom
codex/history-source-load-20261018
Oct 18, 2026
Merged

frahlg merged 2 commits into
masterfrom
codex/history-source-load-20261018

Conversation

@frahlg

@frahlg frahlg commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Recorded household Load depended on a valid instantaneous balance, so different meter and charger reporting times could erase otherwise usable history. The app's consumer ledger and the web/day readers also followed different paths.

Core now stores independently qualified source energy in common UTC minutes and balances signed energy over the requested chart, day or price interval. App and web use the same historical Load. Live display, forecast qualification and hardware control keep their current behavior.

  • Keep negative source intervals until the requested interval sums. Complete chart intervals can contain a negative minute; an invalid whole interval stays unknown.
  • Preserve missing sources, writer gaps, process stops, settings changes and hardware identity changes. Distinguish measured zero from missing data.
  • Keep older history and rollback writer periods separate. Do not retrofit old gaps using today's topology.
  • Copy and verify source tables in backup/restore, retain additive source totals, and gate RAM/durable day caches on committed progress. Keep signed V2X energy separate from EV charging cost.

Validation: the original reporting-cadence regression failed before the change. Targeted SQLite, native-history and same-server API regressions pass, including negative-minute cancellation, zero/missing, restart, timestamp origin, method transitions, V2X, query truncation, tariffs, midnight cache, retention and full backup/restore. Full make verify and targeted race checks pass on exact head 01cc8e3d02b5b39c331d016a8db5125579ad38c6, including the merged MCP base. A local OCPP listener timed out while full and race suites overlapped; the unchanged test passed three isolated runs and the full suite then passed alone. GitHub CI is green on the same exact head: 19 successful checks and four intentional skips. No formal GitHub review is claimed; the owner authorized beta fast-track after green CI.

This is receipt-integrated source energy, not an exact correction of device clocks. Normal rollback keeps the source tables in the database, but an older Core's portable backup does not know these new tables; use the matching new helper for exports. No general migration or stable qualification is claimed.

@frahlg
frahlg marked this pull request as ready for review October 18, 2026 19:02
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T19:06:16.886421Z 01cc8e3 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@frahlg
frahlg merged commit f3f6100 into master Oct 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 01cc8e3d02

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +82 to +83
if sourceTables != 0 && sourceTables != 3 {
return errors.New("incomplete history source tables; restore a verified full backup")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Allow interrupted source-schema creation to resume

If the process or host stops during the first upgrade after one or two of the three new source tables have been created, ensureHistorySchema has committed those CREATE TABLE statements separately. On every subsequent startup this pre-schema check sees a partial set and aborts before the remaining CREATE TABLE IF NOT EXISTS statements can run, unnecessarily making Core unrecoverable without restoring a backup. Create the related tables transactionally or distinguish an interrupted additive migration from a genuinely incomplete restored dataset.

AGENTS.md reference: AGENTS.md:L24-L25

Useful? React with 👍 / 👎.

frahlg added a commit that referenced this pull request Oct 20, 2026
* fix(history): bridge short source gaps instead of losing the day's Home use

Since #1741 a Core restart (18-20 s), a source unknown for a few seconds
or a writer pause over 15 s made the whole day's load_wh null, and an
unknown PV also nulled pv_wh.

The source writer now holds an episode that starts at the last fully
known snapshot. If every source is known again with the same topology
within LoadEstimateRecoveryWindow (2 min), the episode is written as an
estimate marked EstimatedLoad: measured sources keep their snapshots,
unknown ones hold the anchor's values. Otherwise it is written exactly
as before. Pending time stays out of totals and expected coverage, and
the day waits for it before closing.

On open Core marks the cursor as stopped instead of deleting it, so a
brief stop is bridged as an estimate and a longer one starts a new
period as before. Older Cores still delete the cursor on open.

Relates to #1750.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(history): forgiving Home use display, strict marker for training

Follows Fredrik's two-layer direction (19 October). Display is
forgiving: an unknown PV, battery or EV source holds its last known
value; the site meter, a writer pause or a restart holds for at most
two minutes; a topology change no longer stops integration. A day's
flow is unknown only when it is unknown or unrecorded for more than a
tenth of the range.

Only two fully measured snapshots a safe tick apart, of one topology
and one process, make a measured interval. Every other interval keeps
EstimatedLoad, and quarter-hour retention now keeps that bit, so the
training layer can skip it.

Replaces the pending episodes of the first version. Closed source-era
days cached under the old rule are recomputed once.

Relates to #1750.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(history): stop holding a source that would make the house negative

A held value that turns the household residual negative cannot be
right: the source changed while unknown, such as a charger that
finished. That interval leaves the held flows unknown instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(history): a failed source leaves only its own flow unknown

historySources set MissingLoad for every reason except invalid_balance.
Telemetry puts a source's own failure there too, such as
"offline:sma:pv" or "stale:easee:ev", so a real snapshot with a silent
inverter or charger voided the whole balance and the source writer
held nothing. Only configuration and duplicate-flow reasons now set
MissingLoad; a failed source is unknown on its own flag.

Found by the #1755 session through historySources.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg deleted the codex/history-source-load-20261018 branch November 11, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant