Skip to content

[DB-2273]: Cap stream reads at the last event number they report - #5822

Open
timothycoleman wants to merge 1 commit into
masterfrom
timothycoleman/db-2273-cap-stream-read-at-last-event-number
Open

timothycoleman wants to merge 1 commit into
masterfrom
timothycoleman/db-2273-cap-stream-read-at-last-event-number

Conversation

@timothycoleman

@timothycoleman timothycoleman commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5816

A stream read that ran while an append to the same stream was committing could return events past the last stream position it reported, for example events 0..10 with last_stream_position 8. A client that pages by comparing the two would read again and get an empty page. After this change, every stream read reports a last stream position at or above every event it returns. Events appended while the read runs are left for the next read.

Design decisions

  • Cap the events rather than raise the reported position: raising the last event number to the last event returned mixes two points in time. A page can end partway through an append, so the reported position could be one the stream never had, and the $maxCount lower bound would still come from the older value.
  • $maxAge gap loop left as it was: TryGetNextEntry matches on the stream hash and can land on a colliding stream's entry past the last event number. Capping there would skip the stream's last event, so the first page leaves out the stream's own entries past the last event number instead.

Risk

Low. Only stream reads that overlap an append change: they return fewer events, ending at the last event number they report. Subscriptions weren't affected, since they drop anything at or below their checkpoint. No data format, compatibility or config changes, and the read path gains only a few integer comparisons.

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Nov 26, 2026

Copy link
Copy Markdown

DB-2273

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Stream reads now cap index lookups and results at the cached last event number. New tests cover index entries beyond that number and a $maxAge index gap that reaches a colliding stream. Pull request description guidance also adds a distinction between past bug behavior and current behavior.

Changes: Stream read bounds

Layer / File(s) Summary
Cap stream read ranges
src/KurrentDB.Core/Services/Storage/ReaderIndex/IndexReader.cs
Forward and backward reads cap index ranges at the cached last event number. The $maxAge path also skips entries beyond that limit, including entries reached after seeking across a gap.
Test reads with index entries beyond the cached end
src/KurrentDB.Core.Tests/Services/Storage/ReadIndex/when_reading_a_stream_while_its_index_is_ahead_of_the_cached_last_event_number.cs
Tests verify forward and backward reads for plain and $maxAge streams when the index contains entries beyond the cached last event number.
Test $maxAge reads across a colliding index gap
src/KurrentDB.Core.Tests/Services/Storage/ReadIndex/when_reading_a_max_age_stream_whose_index_gap_leads_to_a_colliding_stream.cs
Tests verify that reads return only the target stream’s last event when a gap leads to a colliding stream, with and without an index append during the read.

Pull request description guidance

Layer / File(s) Summary
Clarify bug-cause descriptions
CLAUDE.md
Guidance allows an explanation of a non-obvious bug cause and asks that it identify the cause as past behavior, distinct from current behavior.

Assessment against linked issues

Objective Addressed Explanation
Keep forward-read events within the reported cached last stream position [#5816] ✅

Out-of-scope changes

Code Change Explanation
Adds guidance for describing non-obvious bug causes in pull request descriptions (CLAUDE.md:144-145) This writing guidance does not address the stream-read consistency objective in issue #5816.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to 3552c

Stream reads that overlap an append now stop at the position they report. No concrete merge-blocking risk was found, and the change is covered by new tests.

Security architecture risk: Minimal · up to 3552c

Stream reads now return only events at or below the position they report. This makes pagination more consistent during concurrent appends without changing who can read a stream or requiring a data migration.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The consistency change applies to existing forward and backward stream-read callers of the shared reader. The production diff changes which events those calls can return, not their authority to select a stream.

Trust Boundaries and Controls

  • observed — A hash-based index match supplies a candidate record location, not proof of stream identity. The reader still checks the stored stream ID against the requested ID before constructing a returned event, including after a $maxAge gap lookup.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

CI annotation context is incomplete: annotation collection unavailable or limited

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: bafe8b78-1c4f-4414-9f55-a0c98b071236
Commits

Reviewing files that changed from the base of the PR and between dc074bf and 3552c72.

Files selected for processing: 4

  • CLAUDE.md
  • src/KurrentDB.Core.Tests/Services/Storage/ReadIndex/when_reading_a_max_age_stream_whose_index_gap_leads_to_a_colliding_stream.cs
  • src/KurrentDB.Core.Tests/Services/Storage/ReadIndex/when_reading_a_stream_while_its_index_is_ahead_of_the_cached_last_event_number.cs
  • src/KurrentDB.Core/Services/Storage/ReaderIndex/IndexReader.cs
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

A stream read that overlapped an append to the same stream could return
events past the last event number it reported (#5816). Before this change,
a read took the stream's last event number from the cache, then read the
requested range from the index. If an append completed in between, the index
already held the new events, so the read returned them along with the older
last event number.

Reads now cap the index range at the last event number they report, so
events appended during the read are left for the next read.

In the $maxAge path, the gap loop still looks for the next index entry
without the cap. TryGetNextEntry matches on the stream hash and can land on
a colliding stream's entry past the last event number, so capping there
would skip the stream's last event. Instead, the first page leaves out the
stream's own entries past the last event number. The cap in the $maxAge
binary search is defensive: it only matters when hashes collide.

We didn't raise the reported last event number to the last event returned
instead, because that mixes two points in time. A page can end partway
through an append, so the reported last event number could be one the
stream never had, and the $maxCount lower bound would still come from the
older last event number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timothycoleman
timothycoleman force-pushed the timothycoleman/db-2273-cap-stream-read-at-last-event-number branch from b3e76d3 to 3552c72 Compare November 26, 2026 16:56
@timothycoleman
timothycoleman marked this pull request as ready for review November 26, 2026 16:56
@timothycoleman
timothycoleman requested review from a team as code owners November 26, 2026 16:56
Copilot AI balanced review requested due to automatic review settings November 26, 2026 16:56

Copilot AI 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.

🟢 Approval recommended

No active review issues block the change to keep stream-read results and pagination metadata consistent during overlapping appends.

No open issues 🎉

What changed in this PR

Fixes #5816 by capping stream reads at their reported last event number, keeping returned events and pagination metadata consistent when reads overlap appends.

Changes:

  • Caps forward, backward, and $maxAge read ranges while preserving hash-collision handling.
  • Adds deterministic regression tests for an index ahead of the cached last event number and $maxAge gaps involving colliding streams.
  • Clarifies how PR descriptions should distinguish previous buggy behavior from current behavior.
File Description
src/​KurrentDB.Core/​Services/​Storage/​ReaderIndex/​IndexReader.cs Caps returned events at the reported last event number across stream-read paths.
src/​KurrentDB.Core.Tests/​Services/​Storage/​ReadIndex/​when_reading_a_stream_while_its_index_is_ahead_of_the_cached_last_event_number.cs Tests read bounds and pagination when the index is ahead of the cache.
src/​KurrentDB.Core.Tests/​Services/​Storage/​ReadIndex/​when_reading_a_max_age_stream_whose_index_gap_leads_to_a_colliding_stream.cs Tests that collision-related gaps preserve the last valid event without returning newer events.
CLAUDE.md Adds wording guidance for explaining pre-fix behavior in PR descriptions.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

Stream read can return events past the last_stream_position it reports when an append commits during the read

2 participants