Skip to content

feat(data): introduce binance.market_tape.v2 tape schema - #544

Merged
proerror77 merged 2 commits into
mainfrom
codex/tape-v2-schema
Aug 1, 2026
Merged

feat(data): introduce binance.market_tape.v2 tape schema#544
proerror77 merged 2 commits into
mainfrom
codex/tape-v2-schema

Conversation

@proerror77

@proerror77 proerror77 commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Change contract

Introduce binance.market_tape.v2 in data-pipelines/core: new event families raw_trade (per-symbol trade-id continuity), book_ticker (spot + fstream frame shapes), force_order (USD-M only), version-aware structural checks (v1 = 2 streams/symbol, v2 = declared stream_types list), explicit continuity-summary arms, and byte-identical v1 verification. kline reserved as name only.

Out of scope

Dependency / merge order

Merge after nothing; base of the stack tape-v2-schema ← archiver-v2-streams ← tape-v2-deploy-gates. Old binaries remain fail-closed on v2 tapes (verified); new code accepts v1 (regression suite unchanged and green).

Focused validation

  • 71 hft-data tests green incl. 10+ new v2 positive/negative tests (unknown fields, wrong stream counts, per-family gap detection, v1-manifest-with-stream_types rejection, spot + fstream bookTicker shapes, kline rejection, force_order-on-spot rejection)
  • cargo test -p hft-collector --locked green; clippy clean
  • Code review found + fixed: spot bookTicker frames (no e/E/T) now parse with receive-clock-only bounds; sealed v2 spot tape verifies end-to-end

Rollout / rollback impact

None until #536 lands — the archiver still writes v1. Verifier-only change; rollback = revert.

Issue relationship

Closes #535

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@proerror77, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4fa96f96-67cc-4ee1-b6d8-50567cf2ba72

📥 Commits

Reviewing files that changed from the base of the PR and between 1adcaad and 3b3328e.

📒 Files selected for processing (4)
  • rust_hft/data-pipelines/core/src/binance_lob_replay.rs
  • rust_hft/data-pipelines/core/src/binance_market_tape.rs
  • rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs
  • rust_hft/tools/collector/src/lob_archiver.rs
📝 Walkthrough

Walkthrough

Changes

Market-tape v2

Layer / File(s) Summary
Event contracts and parsers
rust_hft/data-pipelines/core/src/binance_market_tape.rs
Adds schema v2 and parsers for raw_trade, book_ticker, and force_order. Adds payload, clock, stream, decimal, and sequence validation.
Schema-aware artifact validation
rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs
Adds v2 manifest declarations, event fields, stream-count checks, coverage checks, and v2 fixtures while retaining v1 rules.
Cross-segment continuity checks
rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs
Adds raw-trade continuity verification across segments and tests for valid and invalid v2 artifacts.
Collector and replay integration
rust_hft/tools/collector/src/lob_archiver.rs, rust_hft/data-pipelines/core/src/binance_lob_replay.rs
Recognizes supported market-tape schemas in archiving and verifies that non-LOB events do not change replay state.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related issues

  • proerror77/monday issue 536 — Covers collector capture and dispatch for the same bookTicker, raw-trade, and forceOrder streams supported by this validation work.

Possibly related PRs

  • proerror77/monday#104 — Introduces validation contracts that this PR extends for additional Binance event types.
  • proerror77/monday#128 — Modifies related per-symbol aggregate-trade coverage checks in the artifact verifier.
  • proerror77/monday#131 — Extends replay-timeline verification used by the artifact validation path.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the main template sections but omits the required Scope exception section for this large change. Add a Scope exception section with a split plan or inseparability rationale and named reviewer approval.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed All coding requirements from #535 are met: raw_trade and book_ticker validators, force_order support (USD-M only), version-aware checks, cross-segment continuity verification, explicit continuity-summary handling, and v1 backward compatibility.
Out of Scope Changes check ✅ Passed All changes remain in scope: schema implementation, event validators, version-aware checks, and backward compatibility support are directly required by #535. Archiver capture and deployment policies are correctly marked out of scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly identifies the introduction of the Binance market-tape v2 schema, which is the primary change.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Fix failing CI checks
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/tape-v2-schema

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
rust_hft/tools/collector/src/lob_archiver.rs (1)

619-702: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep trade_representation contract-aligned for v2 recovered segments.

market_tape_schema matches MARKET_TAPE_SCHEMA_V2, which accepts raw_trade rows. recover_parts only builds summaries from agg_trade rows, so finalize_segment can emit empty trade_summaries with metadata saying aggregate trades are present and the price surface derives from aggregate trades. Make the recovered metadata content/schema-aware, or prevent raw-trade-only v2 recoveries from reaching this path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust_hft/tools/collector/src/lob_archiver.rs` around lines 619 - 702, Update
finalize_segment’s market-tape metadata construction to reflect the recovered
event content: do not advertise aggregate-trade representation, aggregate-trade
price derivation, or aggregate trade summaries when the segment contains only
raw_trade rows. Use the existing schema/content signals from recover_parts and
preserve the current aggregate-trade metadata only when corresponding agg_trade
data was actually recovered, or reject raw-trade-only MARKET_TAPE_SCHEMA_V2
segments before finalize_segment.
🧹 Nitpick comments (4)
rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs (1)

649-655: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Replace the expect on stream_types with a fail-closed error.

validate_manifest_identity guarantees that a v2 manifest declares stream_types, so the invariant holds for sealed triplets today. The expect still converts any future path that skips identity validation into a panic instead of a verification failure.

Return an error to keep the verifier fail-closed.

♻️ Proposed change
-                            let declared_types = segment
-                                .manifest
-                                .stream_types
-                                .as_ref()
-                                .expect("v2 manifest declares stream types");
+                            let declared_types = segment
+                                .manifest
+                                .stream_types
+                                .as_ref()
+                                .context("v2 market-tape manifest declares no stream types")?;

The same pattern exists at Lines 1063-1066 in validate_stream_coverage_row.

As per coding guidelines: "Missing capability must fail closed until a governed Rust implementation exists."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs` around
lines 649 - 655, Replace the stream_types expect calls in the v2 branches of the
manifest verification logic and validate_stream_coverage_row with fallible error
returns. Preserve the existing validation behavior when stream_types is present,
but propagate a verification error when it is missing instead of panicking.

Source: Coding guidelines

rust_hft/data-pipelines/core/src/binance_market_tape.rs (2)

850-865: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Consider making event_time_ms optional for spot frames.

For spot frames the code stores the receive clock in event_time_ms. A consumer cannot distinguish a venue source clock from a receive-derived value. Only the doc comment records the difference.

An Option<u64> field would make the absence explicit and prevent latency math on a receive-derived value.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust_hft/data-pipelines/core/src/binance_market_tape.rs` around lines 850 -
865, The BookTicker event_time_ms field conflates spot receive time with venue
event time. Change event_time_ms to Option<u64>, return None for spot frames
lacking a source clock, and preserve the existing source timestamp for USD-M
frames; update constructors and consumers to handle the optional value
explicitly.

815-848: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

RawTradeSequenceValidator duplicates AggregateTradeSequenceValidator.

The state shape HashMap<String, (u64, u64, u64)> and the gap/rollback logic match AggregateTradeSequenceValidator at Lines 722-758. Only the id field and the error text differ.

A shared generic validator, parameterized by the id accessor and the family name, would remove the duplicate logic. This is optional for this PR.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust_hft/data-pipelines/core/src/binance_market_tape.rs` around lines 815 -
848, Optionally refactor RawTradeSequenceValidator and
AggregateTradeSequenceValidator into one shared generic sequence validator,
parameterized by the trade ID accessor and family name. Preserve the existing
per-symbol consecutive-ID and source-time rollback checks, overflow handling,
and family-specific error text while making both validators reuse the common
implementation.
rust_hft/tools/collector/src/lob_archiver.rs (1)

1770-1901: 📐 Maintainability & Code Quality | 🔵 Trivial

Consider adding an explicit v2 case to recovery_preserves_legacy_and_current_schema_identity / recovery_quarantines_mixed_or_schema_incompatible_parts.

These tests exercise LEGACY_LOB_TAPE_SCHEMA and RAW_SCHEMA (v1) recovery paths, but there is no case using MARKET_TAPE_SCHEMA_V2 with a raw_trade, book_ticker, or force_order row. Since market_tape_schema and event_type_allowed were generalized specifically to cover v2, a dedicated recovery test would confirm the new schema branch behaves as expected end to end (manifest fields, quarantine behavior).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rust_hft/tools/collector/src/lob_archiver.rs` around lines 1770 - 1901, Add
explicit MARKET_TAPE_SCHEMA_V2 recovery cases to
recovery_preserves_legacy_and_current_schema_identity and
recovery_quarantines_mixed_or_schema_incompatible_parts, using representative
raw_trade, book_ticker, or force_order rows. Assert valid v2 parts recover with
the expected manifest schema and metadata, and invalid or incompatible v2 rows
are quarantined with no manifest produced.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rust_hft/data-pipelines/core/src/binance_market_tape.rs`:
- Around line 875-909: Update from_frame’s market detection to reject frames
that lack the USD-M event identity but contain either source-clock field E or T,
instead of treating them as spot payloads. Add this guard before the spot path
proceeds, while preserving valid spot frames without source clocks and existing
USD-M validation.

---

Outside diff comments:
In `@rust_hft/tools/collector/src/lob_archiver.rs`:
- Around line 619-702: Update finalize_segment’s market-tape metadata
construction to reflect the recovered event content: do not advertise
aggregate-trade representation, aggregate-trade price derivation, or aggregate
trade summaries when the segment contains only raw_trade rows. Use the existing
schema/content signals from recover_parts and preserve the current
aggregate-trade metadata only when corresponding agg_trade data was actually
recovered, or reject raw-trade-only MARKET_TAPE_SCHEMA_V2 segments before
finalize_segment.

---

Nitpick comments:
In `@rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs`:
- Around line 649-655: Replace the stream_types expect calls in the v2 branches
of the manifest verification logic and validate_stream_coverage_row with
fallible error returns. Preserve the existing validation behavior when
stream_types is present, but propagate a verification error when it is missing
instead of panicking.

In `@rust_hft/data-pipelines/core/src/binance_market_tape.rs`:
- Around line 850-865: The BookTicker event_time_ms field conflates spot receive
time with venue event time. Change event_time_ms to Option<u64>, return None for
spot frames lacking a source clock, and preserve the existing source timestamp
for USD-M frames; update constructors and consumers to handle the optional value
explicitly.
- Around line 815-848: Optionally refactor RawTradeSequenceValidator and
AggregateTradeSequenceValidator into one shared generic sequence validator,
parameterized by the trade ID accessor and family name. Preserve the existing
per-symbol consecutive-ID and source-time rollback checks, overflow handling,
and family-specific error text while making both validators reuse the common
implementation.

In `@rust_hft/tools/collector/src/lob_archiver.rs`:
- Around line 1770-1901: Add explicit MARKET_TAPE_SCHEMA_V2 recovery cases to
recovery_preserves_legacy_and_current_schema_identity and
recovery_quarantines_mixed_or_schema_incompatible_parts, using representative
raw_trade, book_ticker, or force_order rows. Assert valid v2 parts recover with
the expected manifest schema and metadata, and invalid or incompatible v2 rows
are quarantined with no manifest produced.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9f6905a7-179d-460f-979f-98686fdcb338

📥 Commits

Reviewing files that changed from the base of the PR and between 322372e and 1adcaad.

📒 Files selected for processing (4)
  • rust_hft/data-pipelines/core/src/binance_lob_replay.rs
  • rust_hft/data-pipelines/core/src/binance_market_tape.rs
  • rust_hft/data-pipelines/core/src/binance_market_tape_artifact.rs
  • rust_hft/tools/collector/src/lob_archiver.rs

Comment thread rust_hft/data-pipelines/core/src/binance_market_tape.rs
Sonic Shih added 2 commits August 1, 2026 08:18
Add the v2 market-tape schema with three new validated event families:
raw_trade (per-symbol trade-id continuity), book_ticker and force_order
(clock-bounds only, no sequence guarantee; force_order is USD-M only).
v2 declares its per-symbol stream-type list in the manifest and the
session_start row; websocket_streams and stream_coverage evidence are
checked against that declaration instead of the fixed v1 two-stream
model. kline is reserved as an event type name only.

v1 tapes keep byte-identical verification: v1 manifests must not carry
stream_types, v1 rows reject the new event types, and the legacy stream
count rule is unchanged. A BinanceRawTradeContinuityVerifier parallels
the aggregate-trade cross-segment verifier, and the collector archiver
finalization now matches the market-tape schema version set.

Refs #535
Binance spot bookTicker payloads carry no e/E/T fields (those are USD-M
only), so the strict identity and source-clock checks rejected every
real spot frame. Decide the market from the frame shape: USD-M frames
keep the strict e/E/T validation, spot frames derive the symbol from s
or the combined-stream name and fall back to the receive clock. Both
shapes still fail closed on malformed payloads.

Refs #535
Refs #536
@proerror77
proerror77 force-pushed the codex/tape-v2-schema branch from 1adcaad to 3b3328e Compare August 1, 2026 00:18
@proerror77
proerror77 enabled auto-merge (squash) August 1, 2026 00:19
@proerror77
proerror77 merged commit fe79959 into main Aug 1, 2026
41 checks passed
@proerror77
proerror77 deleted the codex/tape-v2-schema branch August 1, 2026 00:35
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.

feat(data): binance.market_tape.v2 schema with raw_trade/book_ticker/force_order validators

1 participant