Skip to content

fix(document): a standalone MongoDB seal is never withdrawn after readers see it - #101

Merged
M3gA-Mind merged 1 commit into
tinyhumansai:mainfrom
M3gA-Mind:fix/mongo-seal-truncation-floor
Nov 11, 2026
Merged

M3gA-Mind merged 1 commit into
tinyhumansai:mainfrom
M3gA-Mind:fix/mongo-seal-truncation-floor

Conversation

@M3gA-Mind

Copy link
Copy Markdown
Collaborator

Part of #49, item (a). The design note was agreed with the maintainer side first. Not breaking.

Summary

On a MongoDB deployment without transactions (a standalone mongod), seal inserted its marker, then re-checked the truncation and append epochs, and deleted the marker again if either had moved. In between, every reader (read_window, info, appends refused) already treated the stream as sealed, so a rolled-back seal was briefly visible. Three more standalone flaws sat in the same code:

  • a withdrawn seal's blob was leaked;
  • a delete_stream between the seal's snapshot and its marker insert went unnoticed (no generation re-check), so the marker landed in a dead generation and seal returned Ok;
  • a truncation that found the stream sealed reported Conflict after its segment edits had already run.

Fix (standalone path only)

  1. Truncation floor. A standalone truncate_before publishes floor on the stream head ($max, filtered on the current generation) before it edits any segment. Sealed and live reads clip at the floor (read_window and info), so a truncation is visible at once and the row edits are only reclamation.
  2. No withdrawal for truncation. The seal drops its epoch re-checks: a truncation racing it has raised, or will raise, the floor, and the sealed view is clipped by it.
  3. Appends. The unique (_scope, s, g, o) index arbitrates them. An append that took the seal's offset makes the marker insert fail before it's visible, and an append after the marker sees it and refuses.
  4. After the marker insert, a seal withdraws (deleting its marker by blob key, and the blob) only when:
    • the generation moved (the stream was deleted meanwhile), or
    • a row exists past its length (a newer seal already published, removing the rows this marker would have collided with).
      In both cases the withdrawn marker is never the one readers see.
  5. A truncation that finds the stream sealed after raising the floor returns what it removed: the floor already made it effective.
  6. delete_stream (advance_generation) resets the floor to 0 with the generation bump.

Migration: none. A head without floor reads as 0. Transactional path: unchanged. publish_seal_marker and truncate_transactional keep their single-session guarantees.

Validation

On a Linux build host, merged onto main 6372924:

  • Live replica set: mongodb:: tests, 60 passed, 0 failed, 0 skipped.
  • Live standalone mongod: all standalone-path tests pass. The only failures are the 14 tests that require transactions, which assert transactions_available() or need ConditionalWrites. They fail identically without this change; CI runs a replica set.
  • Revert check: main's seal and truncate logic with only the new test hooks. On a standalone it fails 4 of the new tests:
    • a truncation finished during a seal ("stream was truncated while sealing");
    • the existing linearize test ("stream was sealed while truncating");
    • the append race;
    • delete-during-seal.
      The fifth new test (a floor belongs to its generation) passes on the old code by construction, since there was no floor; it guards the new field.
  • cargo test --workspace --all-features --locked: 859 passed, 0 failed, 1 ignored.
  • clippy -D warnings (0 items), cargo fmt --check, rustdoc -D warnings.

Tests (mongodb/mod_tests.rs, live MongoDB)

  • a_truncation_finished_during_a_standalone_seal_never_withdraws_it: the seal is paused after its snapshot, a truncation completes, and the seal succeeds clipped at the floor.
  • deleting_a_stream_during_a_standalone_seal_withdraws_the_marker_and_its_blob.
  • an_append_racing_a_standalone_seal_takes_the_offset_and_the_seal_conflicts: the blob is deleted.
  • a_standalone_truncation_floor_belongs_to_its_generation.
  • standalone_truncate_and_seal_linearize_without_losing_the_truncated_view: updated. The truncation now returns Ok(1) rather than a conflict after its edits.
  • stale_seal_attempt_cannot_replace_the_newer_published_blob (existing) now also passes on a standalone.
  • transactional_truncation_invalidates_a_seal_snapshot_taken_before_blob_publish (existing) now asserts it runs with transactions, like the other transaction-only tests; on a standalone the floor path clips instead.

A test-only pause hook (pause_seal_for_test) joins the existing truncate hook.

…ders see it

On a deployment without transactions, seal inserted its marker, then
re-checked the truncation and append epochs and withdrew the marker on a
change. In between, every reader already saw the sealed view, so a
rolled-back seal was briefly visible.

A standalone truncate_before now publishes a truncation floor on the
stream head before it edits any segment, and sealed and live reads clip
at it. A seal no longer has to be withdrawn because of a truncation: the
floor already clips the sealed view. Appends are arbitrated by the
unique (scope, stream, generation, offset) index. After inserting its
marker, a seal withdraws only if the stream was deleted (generation
moved) or a newer seal already published past its length, and neither
withdrawn marker is ever the one readers see.

Also on the standalone path: a withdrawn seal's blob is deleted rather
than leaked; a truncation that finds the stream sealed after raising the
floor reports what it removed instead of a conflict after its edits;
and deleting a stream resets its floor with the generation. The
transactional path is unchanged. Heads without a floor read as zero.

Part of tinyhumansai#49
@coderabbitai

coderabbitai Bot commented Nov 11, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

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: Advanced
  • Run ID: b5e67289-59c4-4aa7-9679-0ddbee0cce59
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@tinysweeper

tinysweeper Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Tiny Sweeper review

Reviewed head: 143367e6bd0cb4000f6382108977b3a87f38f1d4

Verdict: Ready for maintainer review · Priority: none
Coverage: Reviewed 3 of 3 files.
Pull request purpose (title): fix(document): a sta…

Diagnostics and supporting details

Full summary

Pull request purpose (title): fix(document): a standalone MongoDB seal is never withdrawn after readers see it. This pull request, titled "fix(document): a standalone MongoDB seal is never withdrawn after readers see it" (part of #49, item (a), non-breaking), reworks the non-transactional (standalone `mongod`) stream seal and truncate protocol so a seal is never rolled back after readers have already observed it. Previously a standalone `seal` inserted its marker, re-checked truncation and append epochs, and deleted the marker if either had moved — but in between, `read_window`, `info`, and append refusal already treated the stream as sealed. The same code also leaked a withdrawn seal's blob, missed a `delete_stream` occurring between the seal snapshot and marker insert, and let a truncation report `Conflict` after its segment edits had already run. The fix introduces a truncation floor published on the stream head before any segment edit, clips sealed and live reads at that floor, arbitrates appends through the unique `(_scope, s, g, o)` index, and limits post-insert withdrawal to the generation-moved and newer-seal cases — each of which deletes both marker and blob. The transactional path and migration story (a head without `floor` reads as 0) are unchanged.

Review snapshot

Change surface Files Review signal Count
Production 1 Active findings 0
Tests 2 Noted findings 0
Documentation 0 Resolved findings 0
Configuration 0 Pending checks/questions 0

Completeness: Complete
Test assessment: No supported feature-to-test mapping was available; this does not mean tests are absent or passed.

Features

  • Added — Standalone truncation floor published before segment edits: A standalone `truncate_before` raises `floor` on the stream head (generation-filtered `$max` upsert) before touching any row, so the truncation is visible immediately and the subsequent row edits are only reclamation; the upsert's `_id` collision is translated into a conflict indicating the stream was deleted while truncating. (crates/tinystoragedrivers-document/src/mongodb/streams.rs impl MongoStreams {)
  • Modified — Reads clip at the truncation floor: `read_window` starts at the greater of the requested offset and the floor, and `info` clamps both the sealed and live reported base into `floor..=len`, so readers never observe offsets the floor has already truncated away. (crates/tinystoragedrivers-document/src/mongodb/streams.rs impl StreamStore for MongoStreams {)
  • Modified — Standalone seal no longer withdraws on truncation epochs: The epoch re-checks and their rollback are gone; after the marker insert the seal only withdraws when the generation moved or a row exists past its snapshot length, and each withdrawal deletes the marker by blob key and the blob, eliminating the leaked blob. A marker insert that loses the unique-index race also deletes its blob before returning `Conflict`. (crates/tinystoragedrivers-document/src/mongodb/streams.rs impl StreamStore for MongoStreams {)
  • Modified — Sealed and truncating views honor the floor: A successful standalone seal reports a base clipped at the floor, and a truncation that finds the stream sealed after raising the floor returns what it removed instead of reporting `Conflict` after its edits, matching the already-effective floor. (crates/tinystoragedrivers-document/src/mongodb/streams.rs impl StreamStore for MongoStreams {)
  • Modified — Generation advance clears the floor: `advance_generation` resets `floor` to 0 together with the generation bump on both the non-transactional and transactional paths, so a deleted generation's floor cannot clip a newly created stream. (crates/tinystoragedrivers-document/src/mongodb/streams.rs impl MongoStreams {)
  • Added — Test-only seal pause hook: A `#[cfg(test)]` `seal_pause` field with `pause_seal_for_test` and `pause_at_seal_boundary` lets a seal pause after its snapshot and blob upload but before the marker is published, complementing the existing truncate pause hook; `MongoStreams` construction and the unit-test helper initialize the new field to `None`. (crates/tinystoragedrivers-document/src/mongodb/streams.rs pub struct MongoStreams {, crates/tinystoragedrivers-document/src/mongodb/streams.rs impl MongoStreams {, crates/tinystoragedrivers-document/src/mongodb/streams_tests.rs async fn streams_without_server() -> MongoStreams {)

Tests

No supported feature-to-test mapping was produced. Test execution is not inferred.

Findings

No active actionable findings.

Before merge

None.

Agent review details

critique

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Critique lane answered, reviewing 3 of 3 changed files, with no findings.
  • Lane summary: Reviewed 3 files; 0 findings. _The code index is behind this pull request (indexed at `b141c3729fa9`), so retrieved context may be out of date._

security

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Security lane answered, reviewing 3 of 3 changed files, with no findings.
  • Lane summary: Reviewed 3 files; 0 findings. _The code index is behind this pull request (indexed at `b141c3729fa9`), so retrieved context may be out of date._

tests

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Lane summary: This change modifies MongoDB standalone (non-transactional) stream sealing and truncation to use a floor-based protocol: truncation publishes a floor before editing rows, and seal reads clip at the floor, eliminating the need for epoch-based rollback. New tests cover race conditions (truncation-during-seal, delete-during-seal, append-during-seal) and generation-floor isolation. The change is structurally sound and the test coverage is appropriate. _The code index is behind this pull request (indexed at `b141c3729fa9`), so retrieved context may be out of date._

commits

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Commits lane answered with no findings, reporting no sensitive content in committed files.
  • Lane summary: No sensitive content in committed files. _The code index is behind this pull request (indexed at `b141c3729fa9`), so retrieved context may be out of date._

description

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Description lane answered with no findings, judging that the change correctly fixes the standalone seal race via a floor published before segment edits, and that the three additional flaws — leaked blob on withdrawal, delete-during-seal detection, and truncation reporting `Conflict` after edits — are each addressed.
  • Lane summary: The change correctly fixes the standalone MongoDB seal race by introducing a truncation floor that is published before segment edits, inverting the order so readers clip at the floor and a concurrent seal never needs withdrawal. The three additional flaws (leaked blob on withdrawal, delete-during-seal detection via generation check, and truncation reporting Ok instead of Conflict after edits) are each addressed. Tests cover the new invariants and pass live against standalone MongoDB. No reportable defects remain. _The code index is behind this pull request (indexed at `b141c3729fa9`), so retrieved context may be out of date._
Evidence and run details
  • Models: ladder/vectors-oai3, deepseek-v4-flash, deepseek-v4.1-flash
  • Spend: $0.014381
  • Completion usage: incomplete; some provider completion costs are unavailable.
  • Tokens: 425301 input · 19371 output · 52736 cached · 610 embedding
  • Updated: 2026-11-11T12:38:07Z
Head State Pass summary
143367e6bd0c ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-11T12:38:07Z)

tinysweeper 0.1.2

@tinysweeper tinysweeper Bot 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.

tinysweeper found nothing blocking. Approving.

Diagnostics

Total spend: $0.0144

@M3gA-Mind
M3gA-Mind merged commit 5c65f51 into tinyhumansai:main Nov 11, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant