fix(udpscope): reconstruct lost accumulated bursts from the packet counter

Review found the decoder was estimating something the wire states exactly.
FrameView::counter increments once per update, so a gap of g means g-1 lost
datagrams; reinstating their duration restores the hole precisely, with no
threshold and no dependence on arrival time. The arrival-anchor comparison
survives only as a backstop for what the counter cannot express — a producer
restart, a counter stuck at zero, a wrong declared rate — and can no longer
step a signal's timestamps backwards, which the ring and trigger forbid.

Also from review: guard the time-signal lookup against a frame carrying more
signals than the installed table, and give FrameBuilder a counter parameter.
Leaving it at zero had hidden the counter rules from every test, and made the
hrt-gap test vacuous — under uniform arrivals the hrt path and packetBurst
agree by construction, so it could not tell which branch answered. Its
arrivals now carry zero-mean jitter.

Each new assertion was proven non-vacuous by sabotage: dropping the gap term,
the backward guard, or the hrt branch fails exactly its own test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Martino Ferrari
2026-08-27 20:16:52 +02:00
co-authored by Claude Opus 4.6
parent 892e3eae28
commit 7102412a9f
4 changed files with 186 additions and 78 deletions
+43 -32
View File
@@ -1609,38 +1609,31 @@ TEST(FrameDecoder, AccumulatedScalarSurvivesBurstyDelivery) {
}
}
// ADDED in Task 4 review. The counterweight to the test above: suppressing
// arrival jitter by chaining bursts is only safe while the chain is CHECKED. On
// UDP packets are lost, and an unchecked chain closes the hole silently and
// dates every later sample early for the rest of the run.
TEST(FrameDecoder, AccumulatedScalarResynchronisesAfterLostPackets) {
// ADDED in Task 4 review, together with two siblings. See the shipped
// tests/FrameDecoderTest.cpp for the full set — accSignal()/primeTenBursts()
// helpers plus:
// * AccumulatedScalarReinstatesLostPacketsFromTheCounterGap — counter 10 →
// 111 means 100 lost packets = exactly 1 s; the packet deliberately lands
// 200 ms off that truth so the test fails if the answer comes from arrival.
// * AccumulatedScalarResyncsOnArrivalWhenTheCounterSaysNothing — counter
// stuck at 0, so only the arrival backstop can recover.
// * AccumulatedScalarNeverStepsBackwardsWhenResyncing — a resync that would
// move a signal's timestamps into the past must be given up instead.
// FrameBuilder::build() gained a `counter` parameter for these; leaving it at
// zero, as the original harness did, hides the counter rules entirely.
TEST(FrameDecoder, AccumulatedScalarReinstatesLostPacketsFromTheCounterGap) {
FrameDecoder dec;
SignalMeta m;
m.name = "Acc";
m.typeCode = 9;
m.numRows = 1;
m.samplingRate = 1000.0; /* 10 samples = 10 ms per packet */
dec.setSignals({m});
dec.setSignals({accSignal()});
std::vector<double> ts;
for (int p = 0; p < 10; p++) {
FrameBuilder fb;
fb.addSignal(std::vector<double>(10, 1.0));
const FrameView& f = fb.build(0, 500.0 + p * 0.010, 10);
dec.beginFrame(f);
ASSERT_TRUE(dec.timestamps(f, 0, ts));
}
EXPECT_NEAR(ts[9], 500.090, 1e-9);
primeTenBursts(dec, ts, /*withCounter=*/true);
/* A full second of packets never arrives. The next one lands at 501.100. */
FrameBuilder fb;
fb.addSignal(std::vector<double>(10, 1.0));
const FrameView& f = fb.build(0, 501.100, 10);
const FrameView& f = fb.build(0, 501.300, 10, 111u);
dec.beginFrame(f);
ASSERT_TRUE(dec.timestamps(f, 0, ts));
/* Chaining blindly would put this burst at 500.091..500.100, as though no
* data were missing. */
/* Chaining blindly gives 500.091; anchoring on arrival gives 501.291. */
EXPECT_NEAR(ts[0], 501.091, 1e-9);
EXPECT_NEAR(ts[9], 501.100, 1e-9);
}
@@ -1930,12 +1923,16 @@ bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
* fallback is waiting on.
*
* With a declared rate none of that is needed: the intra-packet step is
* exact, and bursts are contiguous, so the next burst is PREDICTED at
* lastEmittedEnd + dt. The prediction must be checked, not trusted — a pure
* chain silently closes the hole left by a lost datagram and dates every
* later sample early for the rest of the run. So each packet compares the
* prediction against the arrival anchor and abandons it beyond
* kBurstResyncThresholdS. The hrt path below remains for samplingRate == 0. */
* exact and bursts are contiguous, so the next burst chains onto the end of
* the previous one. The one thing a bare chain gets wrong is LOSS — it
* closes the hole a dropped datagram left, dating every later sample early
* for the rest of the run — and the wire already says exactly how much is
* missing: FrameView::counter increments once per update, so a gap of g
* means g-1 lost packets. Reinstating that duration needs no estimate and
* no threshold. The arrival-anchor comparison is only a BACKSTOP for what
* the counter cannot express (producer restart, counter stuck at zero, a
* declared rate that is simply wrong), and it must never move time
* backwards. The hrt path below remains for samplingRate == 0. */
if (d.numElements() == 1u && nElems > 1u) {
const double dtDeclared = (d.samplingRate > 0.0) ? (1.0 / d.samplingRate) : 0.0;
if (d.samplingRate > 0.0) {
@@ -1943,16 +1940,28 @@ bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
wallNow - static_cast<double>(nElems - 1u) * dtDeclared;
double base = arrivalAnchor;
if (st.lastEmittedValid) {
const double predicted = st.lastEmittedEnd + dtDeclared;
/* Unsigned subtraction wraps, so this is right across the
* counter's own 2^32 rollover. */
const uint32_t gap = f.counter - st.lastCounter;
const double lost = (gap > 1u)
? static_cast<double>(gap - 1u) *
static_cast<double>(st.prevAccCount)
: 0.0;
const double predicted = st.lastEmittedEnd + dtDeclared * (1.0 + lost);
if (std::fabs(predicted - arrivalAnchor) <= kBurstResyncThresholdS) {
base = predicted;
}
if (base <= st.lastEmittedEnd) {
base = st.lastEmittedEnd + dtDeclared;
}
}
tsOut.resize(nElems);
for (uint32_t e = 0; e < nElems; e++) {
tsOut[e] = base + static_cast<double>(e) * dtDeclared;
}
st.lastEmittedEnd = tsOut[nElems - 1u];
st.lastCounter = f.counter;
st.prevAccCount = nElems;
st.lastEmittedValid = true;
return true;
}
@@ -2015,7 +2024,9 @@ cd Client/udpscope && cmake --build build -j && ./build/udpscope_tests --gtest_f
Expected: PASS, 9 tests.
If `AccumulatedScalarSurvivesBurstyDelivery` fails, do NOT reach for the hrt fit: with a declared `samplingRate` rule 3 never consults it, precisely because the fit is not ready for the first 32 packets and is itself corrupted by bursty arrivals. Check instead that `lastEmittedEnd`/`lastEmittedValid` are being updated on every emitted burst. The only test that may legitimately fall through to rule 4 early is `AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared`, whose arrivals are uniform, so `packetBurst` is accurate there.
If `AccumulatedScalarSurvivesBurstyDelivery` fails, do NOT reach for the hrt fit: with a declared `samplingRate` rule 3 never consults it, precisely because the fit is not ready for the first 32 packets and — since `HrtRateFit` regresses `hrt` against ARRIVAL time — is itself corrupted by the very bursts it would be asked to survive. Check instead that `lastEmittedEnd`, `lastCounter`, `prevAccCount` and `lastEmittedValid` are updated on every emitted burst.
Note for `AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared`: its arrivals carry zero-mean jitter on purpose. Under UNIFORM arrivals the hrt path and `packetBurst` return the same number by construction (the fit expresses `hrt` in arrival-clock seconds), so the test could not tell which branch answered.
- [ ] **Step 8: Commit**