feat(pending-updates): short-circuit an unchanged fleet on a DB rowversion watermark (#62)

Owner decision (b), 2026-11-09. PendingUpdatesEvaluator re-flattened every
deployed instance on each cache-TTL expiry even when nothing had changed.

- Migration AddConfigGraphRowVersions: a non-concurrency rowversion column
  + index on the 18 configuration tables the evaluation reads (Templates +
  5 member tables, Instances + 4 override/binding tables, DataConnections,
  DeployedConfigSnapshots, Sites, SharedScripts, ExternalSystemDefinitions,
  ExternalSystemMethods, DatabaseConnectionDefinitions). SQL Server only in
  the model; SQLite test contexts are untouched. Script:
  docs/plans/sql/AddConfigGraphRowVersions.sql. Joins the coordinated
  central-pair migration window (#107 item 4).
- IConfigGraphWatermarkReader / ConfigGraphWatermarkReader: ONE query of
  MAX(RowVersion) + COUNT_BIG(*) per table (count catches deletes, which
  raise no rowversion). rowversion is engine-bumped for any writer, so a
  peer central node's edit moves it — not the per-process watermark MG-3
  removed.
- The secret store (outside the DB) gets its own stamp: SHA-256 over
  ListAsync(includeDeleted: true) metadata.
- Evaluator re-serves the last CLEAN snapshot while watermark + secret
  stamp are unchanged; every PendingUpdatesFullEvaluationEvery'th (default
  10, validated >= 1, 1 = off) is full regardless — the safety net for a
  same-KEK re-wrap or an input table missing from the set. Always full when
  the token is unavailable/unreadable, after an evaluation that could not
  compute a site's artifact hash, on Re-check, and after a deploy completion.
  It only re-serves a real earlier answer, never manufactures one.

Tests: SQL Server (real migrated DB) — token stable without writes, moves
for a peer-connection INSERT/UPDATE and a non-max DELETE, moves for a local
EF write, a detached Update still saves (not a concurrency token), and a
guard that every table the evaluator's repository reads touch is covered.
Evaluator — reuse without flatten (re-served answer still shows the stale
instance), watermark move, 4 secret changes, every-Nth, 5 no-token modes,
unclean evaluation not reused, cache Re-check and deploy completion force
full. Mutation-proven: token compare, clean check, cache force flag, row
count, and an uncovered read each turn tests red.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 22:38:55 -05:00
parent 63ec45e2e3
commit a4806576af
20 changed files with 3963 additions and 9 deletions
@@ -0,0 +1,30 @@
namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
/// <summary>
/// Reads a cheap, DATABASE-side change token over every configuration-database table that
/// feeds the pending-updates evaluation — instance staleness (the flatten inputs plus the
/// deployed snapshots) and the per-site artifact revision hash (Gitea #62).
///
/// <para>
/// The token is derived from SQL Server <c>rowversion</c> columns, so it moves on ANY
/// insert or update of those tables — including a write made by the peer central node, a
/// bundle import, a raw <c>ExecuteUpdate</c> or a hand-edited row — without any write path
/// having to remember to bump it. Each table contributes <c>MAX(rowversion)</c> AND its row
/// count, because a delete never raises a rowversion: a removed override or template member
/// is caught by the count. This is deliberately NOT the per-process template-graph
/// watermark: that one never sees a peer-node edit (arch-review 2026-09-09 MG-3).
/// </para>
/// </summary>
public interface IConfigGraphWatermarkReader
{
/// <summary>
/// Reads the current change token in one round trip.
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// An opaque token that is equal across two reads only when none of the covered tables
/// changed in between; <see langword="null"/> when the provider cannot produce one (a
/// non-SQL-Server test provider), in which case callers must not short-circuit.
/// </returns>
Task<string?> ReadAsync(CancellationToken cancellationToken = default);
}