Artifact 4a29b35a035f0b7c2b7cd9e4bca9a7919eeeb9ffedd3123a7d890b61a8c3ce04:
- File
db/scan.go
— part of check-in
[168e20fc91]
at
2026-07-19 16:30:58
on branch trunk
— fix: make Timeline pagination cursor an exact total order (#62)
* fix: make Timeline pagination cursor an exact total order
#54 (merged as 95cbbb7) split Repo.Timeline into a repository-wide enumeration and Repo.Ancestry, the original first-parent walk under an honest name. Its pagination cursor predicate used a tolerance window around the caller-supplied Before/After pair to work around a lossy julian<->time.Time round trip:
mtime < (C - eps) OR (mtime <= (C + eps) AND rid < After)
This reintroduces exactly the defect the composite cursor existed to prevent, and makes it worse: rows within eps of the boundary that are not real ties get filtered by rid instead of tie-broken by it, which can skip a row, duplicate another, or fail to advance the cursor at all — unlike canonical fossil's own bare-timestamp slop, which repeats rows but still advances. A pagination test using only exact-tie and one-hour-apart fixtures could not exercise the 0 < delta < eps range where the defect lives, so it stayed green.
Fix: make the predicate an exact total order, with rid as a true tie-break only at exact mtime equality:
mtime < C OR (mtime = C AND rid < After)
and stop deriving C from a time.Time altogether. TimelineOpts.After is now an opaque Cursor (internal/fsltype.Cursor, re-exported as libfossil.Cursor): obtained only from a LogEntry the library already produced, carrying the exact float64 mtime read off that row rather than one reconstructed from a rounded timestamp. A cursor built from parts can silently drift from the row it claims to follow; a cursor that can only ever be handed back cannot.
That exactness depends on the row's scanned mtime being faithful to begin with, which surfaced a second bug: under the ncruces/WASM sqlite driver, event.mtime scans as a time.Time via the driver's own internal REAL conversion, carrying ~13us of noise that db.ScanJulianDay's time.Time branch then truncated (via UnixMilli()) into a full missed millisecond — independent of and in addition to the JulianToTime truncation bug #54 already fixed on the read-back side. Fixed db.timeToJulian to round to the nearest millisecond, the same class of fix, at the point where the driver-specific noise actually enters.
Also tightened the regression coverage: - Pagination tests now use a fixed loop bound of exactly ceil(n/pageSize) pages rather than an open "while non-empty" loop, so a non-advancing cursor fails the test immediately instead of hanging it. - Added a sub-millisecond-boundary fixture (events 3ms apart, well inside the old epsilon window) with rid order deliberately decoupled from mtime order — a monotonic fixture cannot expose this class of bug, since rid and mtime need not move together in general (backdated commits, imports). - TestJulianRoundTrip now asserts exact equality for values originating from TimeToJulian instead of a *1ms tolerance, which would have passed under both the old truncating JulianToTime and the current rounding one and so was not a regression guard for that fix.
No change to Ancestry, event-type filtering, NULL tolerance, or either loop bound; all were already correct.
* fix: assert pagination termination; mark Cursor change breaking
Both pagination tests (the pre-existing ones and the sub-millisecond regression) ran exactly ceil(n/pageSize) pages and asserted each event appeared once, but neither ever called Timeline one more time to confirm the next page comes back empty. The headline symptom of the epsilon-cursor defect these tests exist to catch was non-termination — a caller paginating until empty never stops — and that specific failure mode was going unguarded: a cursor that enumerates every row correctly but never signals exhaustion would have passed all four tests as they stood.
Added one more Timeline call after the last expected page in all four tests, asserting it returns zero entries. Verified this is not vacuous: reverted the tie-break to <= (non-strict) and confirmed the existing per-event count checks catch it; separately, made Timeline phantom-return one row only on an already-exhausted call (isolated from the real per-page counts, which stayed correct in that experiment) and confirmed only the new final-page assertion fails — proving it exercises a real, non-redundant path, not one already covered by the count checks. Both experiments reverted before commit.
Also corrected the CHANGELOG bullet describing the Cursor redesign: it described the new Cursor type but never stated that TimelineOpts.Before (time.Time) and TimelineOpts.After (FslID) were deleted outright, not deprecated. A reader diffing releases would have read it as an addition rather than a second compile-breaking change on top of #54. (user: [email protected], size: 2603) [annotate] [blame] [check-ins using]
package db import "time" // julianEpoch is the Julian Day Number for the Unix epoch (1970-01-01 12:00:00 UTC). const julianEpoch = 2440587.5 // ScanJulianDay converts a scanned mtime value to a float64 Julian Day Number. // SQLite drivers return mtime differently: modernc returns float64, // ncruces returns time.Time for DATETIME/TIMESTAMP/DATE columns. // Scan the column into an `any` variable, then pass it here. func ScanJulianDay(v any) (float64, bool) { switch v := v.(type) { case float64: return v, true case time.Time: return timeToJulian(v), true case int64: return float64(v), true default: return 0, false } } // ScanTime converts a scanned mtime value to time.Time. // Same driver-compatibility rationale as ScanJulianDay. func ScanTime(v any) (time.Time, bool) { switch v := v.(type) { case time.Time: return v, true case float64: return julianToTime(v), true case int64: return julianToTime(float64(v)), true default: return time.Time{}, false } } // ScanInt converts a scanned value to int. // SQLite drivers differ on BOOLEAN columns: modernc returns int64, // ncruces returns bool. Scan the column into an `any` variable, then pass it here. func ScanInt(v any) (int, bool) { switch v := v.(type) { case int64: return int(v), true case int: return v, true case bool: if v { return 1, true } return 0, true case float64: return int(v), true default: return 0, false } } func julianToTime(jd float64) time.Time { millis := int64((jd - julianEpoch) * 86400.0 * 1000.0) return time.UnixMilli(millis).UTC() } // timeToJulian converts a driver-scanned time.Time back to a julian day // float64. Rounds to the nearest millisecond rather than truncating: the // ncruces/WASM sqlite driver hands back DATETIME columns as a time.Time // via its own internal REAL conversion, which carries sub-millisecond // noise (observed ~13us) around the true millisecond-aligned instant every // mtime in this codebase is written as. A plain UnixMilli() truncates that // noise into a full missed millisecond when it lands just below the true // value; rounding recovers the exact intended millisecond, which is what // makes ScanJulianDay(scannedTime) reproduce the same float64 bit-for-bit // as the value TimeToJulian originally wrote, across both drivers. This // matters beyond cosmetics: Timeline's pagination cursor is built from // exactly this value and depends on it matching the stored row exactly. func timeToJulian(t time.Time) float64 { return julianEpoch + float64(t.Round(time.Millisecond).UTC().UnixMilli())/(86400.0*1000.0) }