fix(sql): attribute a duplicated key to its own tags; key-value cap gets 2x headroom (R06:S-1 review I-1, m-3)

One stray duplicate row used to fail every tag in a key-value group or none of them,
depending on whether an unrelated key was absent. Duplicated keys are now refused
per key: only that key's tags publish BadConfigurationError and the rest of the group
reads normally. The key-value cap reads up to 2x the distinct keys (+1 to detect
excess), so a few stray duplicates are attributed per key and only a gross excess
(history/events table) fails the whole group. Duplicated-key refusals count as
answered refusals for health. The row-cap, duplicate-key and multi-row warnings now
use the per-plan limiter (m-3); the reader-wide limiter is removed. Unit tests pin
both the all-keys-present and absent-key cases with Good controls; live SQL Server
gate added (23/23).

Refs #744
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 16:32:23 -04:00
parent 8f0046ed05
commit c1b9c7ec5c
8 changed files with 206 additions and 85 deletions
@@ -272,25 +272,59 @@ public sealed class SqlPollReaderTests
snapshots[0].SourceTimestampUtc!.Value.ShouldBe(snapshots[0].ServerTimestampUtc);
}
/// <summary>
/// <b>R06:S-1 review I-1.</b> One stray duplicate row is attributed to ITS key: that key's tags publish
/// <see cref="SqlStatusCodes.BadConfigurationError"/> and every other key in the group reads normally.
/// It used to be last-row-wins Good, or the whole group Bad, depending on whether an UNRELATED key was
/// absent. Both variants are pinned: every other key present, and another key absent. The other keys'
/// Good / BadNoData are the positive controls.
/// </summary>
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ReadAsync_aDuplicatedKey_failsOnlyThatKeysTags_whateverElseIsAbsent(bool withAnAbsentKey)
{
using var fixture = new SqlitePollFixture();
var logger = new RecordingLogger();
fixture.Execute(
$"INSERT INTO {SqlitePollFixture.KeyValueTable} " +
$"({SqlitePollFixture.KeyColumn}, {SqlitePollFixture.ValueColumn}, {SqlitePollFixture.TimestampColumn}) " +
$"VALUES ('{SqlitePollFixture.PresentKey}', 99.0, '2026-07-24T10:00:09Z')");
var tags = new List<SqlTagDefinition>
{
KvTag("Speed", SqlitePollFixture.PresentKey),
KvTag("Speed2", SqlitePollFixture.PresentKey), // a second tag on the duplicated key
KvTag("Pressure", SqlitePollFixture.SecondPresentKey),
};
if (withAnAbsentKey) tags.Add(KvTag("Gone", SqlitePollFixture.AbsentKey));
var reader = Reader(fixture, logger, [.. tags]);
var snapshots = await reader.ReadAsync([.. tags.Select(t => t.Name)], CancellationToken.None);
snapshots[0].StatusCode.ShouldBe(SqlStatusCodes.BadConfigurationError);
snapshots[1].StatusCode.ShouldBe(SqlStatusCodes.BadConfigurationError);
snapshots[2].StatusCode.ShouldBe(SqlStatusCodes.Good);
snapshots[2].Value.ShouldBe(SqlitePollFixture.SecondPresentValue);
if (withAnAbsentKey) snapshots[3].StatusCode.ShouldBe(SqlStatusCodes.BadNoData);
var warning = logger.Entries.ShouldHaveSingleItem();
warning.Message.ShouldContain(SqlitePollFixture.PresentKey);
}
/// <summary>The driver counts a duplicated-key refusal as the database answering, like an over-cap one.</summary>
[Fact]
public async Task ReadAsync_whenASourceViolatesOneRowPerKey_takesTheLastRowDeterministically()
public async Task ReadDetailedAsync_aDuplicatedKey_isReportedAsAnAnsweredRefusal()
{
using var fixture = new SqlitePollFixture();
fixture.Execute(
$"INSERT INTO {SqlitePollFixture.KeyValueTable} " +
$"({SqlitePollFixture.KeyColumn}, {SqlitePollFixture.ValueColumn}, {SqlitePollFixture.TimestampColumn}) " +
$"VALUES ('{SqlitePollFixture.PresentKey}', 99.0, '2026-07-24T10:00:09Z')");
// Two keys, one absent: the duplicate fits inside the row cap (2), so it is not provably a history
// table — the last-row-wins rule still applies there (the cap test below covers the other side).
var reader = Reader(fixture,
KvTag("Speed", SqlitePollFixture.PresentKey), KvTag("Gone", SqlitePollFixture.AbsentKey));
var reader = Reader(fixture, KvTag("Speed", SqlitePollFixture.PresentKey));
var clean = Reader(fixture, KvTag("Pressure", SqlitePollFixture.SecondPresentKey));
var snapshots = await reader.ReadAsync(["Speed", "Gone"], CancellationToken.None);
// Design §3.6: the contract is one row per key; when a source breaks it the reader is at least
// deterministic — last occurrence in reader order — rather than silently arbitrary.
snapshots[0].Value.ShouldBe(99.0);
snapshots[1].StatusCode.ShouldBe(SqlStatusCodes.BadNoData);
(await reader.ReadDetailedAsync(["Speed"], CancellationToken.None)).AnsweredRefusalGroups.ShouldBe(1);
(await clean.ReadDetailedAsync(["Pressure"], CancellationToken.None)).AnsweredRefusalGroups.ShouldBe(0);
}
/// <summary>