Skip to content

fix(collector): deduplicate tape trade rows during upload scan - #746

Merged
proerror77 merged 3 commits into
mainfrom
codex/polymarket-upload-dedup-scan
Aug 8, 2026
Merged

proerror77 merged 3 commits into
mainfrom
codex/polymarket-upload-dedup-scan

Conversation

@proerror77

@proerror77 proerror77 commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Change contract

The upload scanner (scan_tape_with_identity_at) hard-failed any tape containing a duplicate polymarket_trade record_id. But crash recovery legitimately re-emits already-recorded trades: state migration reopens markets without a completion proof (completion_migration_reopens_v2_markets_without_a_proof), the market re-finalizes, and its full trade snapshot is emitted again. During the 2026-08-08 incident every tape written after a collector restart carried hundreds of content-identical re-emissions (measured: 565 in the active tape, more in closed tapes), so the upload chain hard-wedged — no tape could ever be archived.

A record id is a SHA-256 over the complete trade identity (tx hash, condition, asset, side, timestamp, wallet, size, price, outcome index), so a repeated row is lossless to deduplicate. This PR mirrors the collector-side #743 fix at the upload layer: duplicates are counted in the scan manifest as duplicate_trade_rows evidence instead of refusing the segment. The set-based completion digest (trade_record_ids_sha256 over the id set) is unaffected because dedup does not change the set. Genuinely malformed rows (bad price/side/size, missing fields) still fail closed.

Issue relationship

None

Out of scope

  • The raw-ops shadow gate redesign (900s observation vs 1800s trade finalization lag) — separate decision pending with the user.
  • The new artifact build + host upload-chain restart with this fix (follows merge).

Dependencies and merge order

None

Focused validation

  • Test rejects_duplicate_trade_record_ids replaced by duplicate_trade_record_ids_are_deduplicated_into_evidence: a tape with a repeated trade id now scans successfully with duplicate_trade_rows == 1, row counts preserved.
  • cargo test -p hft-collector --locked polymarket_upload — 87 passed, 0 failed; clippy -D warnings clean.
  • Host evidence: upload of repaired tape 230054 failed "line 615214: duplicate polymarket_trade record_id" despite all duplicate rows being byte-level identical re-emissions (verified by content comparison: only bookkeeping timestamps differ).

Rollout and rollback

No production impact at merge. After merge the next ACR artifact carries the fix; the upload chain (currently drained manually) resumes against the remaining dup-carrying tapes. Rollback is revert — tapes keep accumulating, which is the pre-change state.

Scope exception

None

Summary by CodeRabbit

  • Bug Fixes
    • Duplicate Polymarket trade records are now accepted and automatically deduplicated.
    • Tapes no longer fail validation when duplicate trade rows are encountered.
    • Manifests now report the number of duplicate trade rows detected.

The upload scanner hard-failed any tape containing a duplicate
polymarket_trade record_id. Crash recovery legitimately re-emits
already-recorded trades when a market re-finalizes (state migration
reopens markets without a completion proof), so every tape written after
a collector restart can carry hundreds of content-identical re-emissions
and the whole upload chain wedges behind them.

A record id is a SHA-256 over the complete trade identity, so a repeated
row is lossless to deduplicate. Count them in the scan manifest as
duplicate_trade_rows evidence instead of refusing the segment, mirroring
the collector-side fix in #743. Genuinely malformed rows still fail
closed.
@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.

@proerror77
proerror77 enabled auto-merge (squash) August 8, 2026 01:00
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The tape scanner now accepts duplicate polymarket_trade rows, deduplicates them by record ID, counts them in the manifest, and verifies the behavior through an updated test.

Changes

Polymarket trade deduplication

Layer / File(s) Summary
Scanner handling, manifest evidence, and validation
rust_hft/tools/collector/src/polymarket_upload.rs
The scanner retains the first record ID, counts duplicate rows, continues validation, and writes duplicate_trade_rows to the manifest. The test verifies successful scanning, one duplicate row, and two retained trade events.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes deduplication of tape trade rows during upload scanning.
Description check ✅ Passed The description includes all required template sections and provides clear behavior, rationale, validation, rollout, and scope details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/polymarket-upload-dedup-scan

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

🧹 Nitpick comments (1)
rust_hft/tools/collector/src/polymarket_upload.rs (1)

4517-4520: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Extend the regression test for the remaining trade contracts.

This test proves duplicate acceptance and row counts. Add a case that preserves the unique-set trade_count and trade_record_ids_sha256 completion evidence. Add another case where a repeated row fails canonical validation. These cases protect fail-closed validation and set-based completion semantics.

🤖 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/polymarket_upload.rs` around lines 4517 - 4520,
Extend the regression test around scan_tape to assert the unique-set completion
fields trade_count and trade_record_ids_sha256 for the existing accepted trades.
Add a fixture case with a repeated trade row that fails canonical validation,
and assert the scan rejects it rather than accepting it. Preserve the current
duplicate_trade_rows, event_types, and events assertions.
🤖 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/tools/collector/src/polymarket_upload.rs`:
- Around line 1766-1769: Update the manifest construction near
duplicate_trade_rows so the duplicate count is recorded under the canonical
quality field, or remove the unused duplicate-count emission entirely. Eliminate
any duplicate_record_ids manifest payload that is always exported as 0, while
preserving the actual duplicate_trade_rows tracking.

---

Nitpick comments:
In `@rust_hft/tools/collector/src/polymarket_upload.rs`:
- Around line 4517-4520: Extend the regression test around scan_tape to assert
the unique-set completion fields trade_count and trade_record_ids_sha256 for the
existing accepted trades. Add a fixture case with a repeated trade row that
fails canonical validation, and assert the scan rejects it rather than accepting
it. Preserve the current duplicate_trade_rows, event_types, and events
assertions.
🪄 Autofix

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: 69bc8158-91ed-4f7f-8df1-ca4b10d8881b

📥 Commits

Reviewing files that changed from the base of the PR and between 176b096 and a18259c.

📒 Files selected for processing (1)
  • rust_hft/tools/collector/src/polymarket_upload.rs

Comment thread rust_hft/tools/collector/src/polymarket_upload.rs
@proerror77
proerror77 merged commit 1f7e740 into main Aug 8, 2026
45 checks passed
@proerror77
proerror77 deleted the codex/polymarket-upload-dedup-scan branch August 8, 2026 05:36
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.

1 participant