Skip to content

fix(polymarket): sanitize legacy state at Rust handoff - #528

Merged
proerror77 merged 3 commits into
mainfrom
codex/polymarket-legacy-state-handoff-527
Jul 31, 2026
Merged

fix(polymarket): sanitize legacy state at Rust handoff#528
proerror77 merged 3 commits into
mainfrom
codex/polymarket-legacy-state-handoff-527

Conversation

@proerror77

@proerror77 proerror77 commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Change contract

At a stopped legacy Python-to-Rust Polymarket collector transition, atomically remove only the four legacy transient failure-clock fields from collector-state.json, while preserving all durable market, completion, settlement, and trade-deduplication state and binding the before/after state to cutover evidence.

This is a small, already-diagnosed runtime defect, so a separate PRD is unnecessary; the linked runtime issue is the authoritative behavior contract.

Out of scope

  • Rust stale-health thresholds and retry policy
  • Python collector/uploader behavior and HTTP 429 rate limiting
  • Gate duration, parity, or health-policy semantics
  • Research, snapshots, evaluator/MCTS, and research issue 235
  • Any production mutation in this PR

Dependency or merge order

None. Base is exact 9384b0e71dc0435c98c4d01cb215b279baa11f1e.

Focused validation

  • RED proof: legacy transient clocks survive into Rust state and are immediately judged against Rust's 180-second stale window.
  • deployment/aliyun/test-polymarket-raw-ops-control-plane.sh
  • bash -n on the Gate, cutover, and focused test scripts
  • shellcheck on the same three files
  • git diff --check
  • Counterexamples cover malformed JSON, non-object market state, wrong mode, wrong owner, symlink state, Rust-to-Rust no-op behavior, transition order, durable-state preservation, snapshot SHA, and cutover evidence binding.

Rollout / rollback impact

Rollout remains fail-closed. Gate and cutover both require the direct state file to be owned by hftcollector:hftcollector, mode 0640, under the trusted 0750 spool. The cutover applies the transform only after the legacy process is stopped and before Rust starts. Automatic rollback retains the transformed durable state and the exact pre-handoff snapshot remains in immutable cutover evidence; it never restores a stale whole-state snapshot over newer durable writes.

Issue relationship

Refs #527

@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: 43 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: d5ca2806-5afa-49ce-bcff-cc08cacbbfd9

📥 Commits

Reviewing files that changed from the base of the PR and between 74169ec and 5d2501a.

📒 Files selected for processing (3)
  • deployment/aliyun/polymarket-raw-ops-cutover.sh
  • deployment/aliyun/polymarket-raw-ops-shadow-gate.sh
  • deployment/aliyun/test-polymarket-raw-ops-control-plane.sh
📝 Walkthrough

Walkthrough

The cutover control plane now validates legacy Python collector state, removes transient failure fields during Python-to-Rust promotion, atomically installs the transformed state, and records hashes, metadata, and counts in cutover evidence. Contract tests cover valid, invalid, and Rust-to-Rust paths.

Changes

Legacy state handoff

Layer / File(s) Summary
Handoff preflight validation
deployment/aliyun/polymarket-raw-ops-shadow-gate.sh
The shadow gate validates ownership, permissions, file type, and JSON structure for Python baselines. Rust baselines bypass the validation.
Atomic state transformation and evidence
deployment/aliyun/polymarket-raw-ops-cutover.sh
Cutover snapshots and transforms legacy state, preserves durable fields, atomically replaces the state file, and publishes handoff metadata in cutover.json.
Handoff contract and ordering tests
deployment/aliyun/test-polymarket-raw-ops-control-plane.sh
Tests cover validation parity, state transformation, rejection cases, operation ordering, and evidence binding.

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

Possibly related issues

Possibly related PRs

Sequence Diagram(s)

sequenceDiagram
  participant ShadowGate
  participant Cutover
  participant LegacyState
  participant RustCollector
  participant CutoverEvidence
  ShadowGate->>LegacyState: validate ownership, mode, type, and JSON schema
  ShadowGate->>Cutover: allow validated promotion
  Cutover->>LegacyState: snapshot and remove transient failure fields
  LegacyState-->>Cutover: return hashes, counts, and metadata
  Cutover->>RustCollector: restart after handoff
  Cutover->>CutoverEvidence: record handoff result
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title clearly describes sanitizing legacy state during the Rust handoff.
Description check ✅ Passed The description covers the change, issue, scope, dependencies, validation, and rollout; only the low-risk scope-exception section is missing.
✨ 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/polymarket-legacy-state-handoff-527

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.

@proerror77
proerror77 enabled auto-merge (squash) July 31, 2026 02:00

@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)
deployment/aliyun/test-polymarket-raw-ops-control-plane.sh (1)

1664-1697: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add rejection cases for a wrong group, a non-integer trade_seen value, and an unknown baseline mode.

The rejection tests cover malformed JSON, a non-object market, mode 660, owner root, and a symlink. Three branches of the shared admission contract stay untested:

  • group == hftcollector on line 204 of deployment/aliyun/polymarket-raw-ops-cutover.sh. state_group never changes from hftcollector.
  • type == "number" and floor == . for trade_seen values.
  • The legacy_python || rust_release allow-list, which must reject any other mode.

Each case costs one assertion and protects the fail-closed admission contract from a silent regression.

♻️ Proposed additional rejection cases
   state_owner=hftcollector
+  state_group=root
+  if verify_legacy_state_handoff_preflight legacy_python "$state"; then
+    printf 'legacy-state handoff admitted a foreign-group state file\n' >&2
+    exit 1
+  fi
+  state_group=hftcollector
+  printf '{"markets":{},"trade_seen":{"condition-a":{"id-a":1.5}}}\n' >"$state"
+  if verify_legacy_state_handoff_preflight legacy_python "$state"; then
+    printf 'legacy-state handoff admitted a non-integer trade sequence\n' >&2
+    exit 1
+  fi
+  printf '{"markets":{},"trade_seen":{}}\n' >"$state"
+  if verify_legacy_state_handoff_preflight unknown_mode "$state"; then
+    printf 'legacy-state handoff admitted an unknown baseline mode\n' >&2
+    exit 1
+  fi
   rm "$state"
   ln -s state-target.json "$state"
🤖 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 `@deployment/aliyun/test-polymarket-raw-ops-control-plane.sh` around lines 1664
- 1697, Add rejection assertions in the legacy-state preflight test flow around
verify_legacy_state_handoff_preflight: temporarily set state_group to a
non-hftcollector value and expect rejection, write state JSON with a non-integer
trade_seen value and expect rejection, and invoke the preflight with an unknown
baseline mode and expect rejection. Restore shared test state as needed so the
existing rust_release allow-list check remains valid.
🤖 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 `@deployment/aliyun/test-polymarket-raw-ops-control-plane.sh`:
- Around line 1709-1722: Guard cutover_stop_collector_line with the same
positive-integer regex used for the handoff line variables before the numeric
ordering comparisons in the cutover validation block. Add a targeted
counterexample test that removes or fails to match the collector stop command
and verifies the check exits nonzero, while preserving the existing fail-closed
ordering behavior.

---

Nitpick comments:
In `@deployment/aliyun/test-polymarket-raw-ops-control-plane.sh`:
- Around line 1664-1697: Add rejection assertions in the legacy-state preflight
test flow around verify_legacy_state_handoff_preflight: temporarily set
state_group to a non-hftcollector value and expect rejection, write state JSON
with a non-integer trade_seen value and expect rejection, and invoke the
preflight with an unknown baseline mode and expect rejection. Restore shared
test state as needed so the existing rust_release allow-list check remains
valid.
🪄 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: 205fd178-f014-4cda-9c4b-8105b88057ca

📥 Commits

Reviewing files that changed from the base of the PR and between 9384b0e and 74169ec.

📒 Files selected for processing (3)
  • deployment/aliyun/polymarket-raw-ops-cutover.sh
  • deployment/aliyun/polymarket-raw-ops-shadow-gate.sh
  • deployment/aliyun/test-polymarket-raw-ops-control-plane.sh

Comment thread deployment/aliyun/test-polymarket-raw-ops-control-plane.sh
@proerror77
proerror77 merged commit 20d8500 into main Jul 31, 2026
41 checks passed
@proerror77
proerror77 deleted the codex/polymarket-legacy-state-handoff-527 branch July 31, 2026 02:24
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