Skip to content

feat(runtime): emit signed arrival-cost evidence - #787

Merged
proerror77 merged 15 commits into
mainfrom
codex/runtime-arrival-instrumentation
Aug 9, 2026
Merged

proerror77 merged 15 commits into
mainfrom
codex/runtime-arrival-instrumentation

Conversation

@proerror77

@proerror77 proerror77 commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Change contract

Emit signed, PIT-available arrival-price and instrument-market evidence for Market and IOC fills so research can measure real execution latency cost.

Issue relationship

Closes #795

Out of scope

Research verification, derivatives execution, fee-account binding, materialization, deployment activation, and order placement.

Dependencies and merge order

None. Merge before the separate research verifier.

Focused validation

  • cargo test -p hft-engine arrival_quote_binds_ioc_to_the_sequenced_venue_without_replacing_its_limit --locked
  • cargo test -p hft-engine arrival_price_requires_engine_quote_provenance --locked
  • cargo test -p hft-engine conflicting_duplicate_order_invalidates_prior_metadata --locked
  • Counterexamples: missing/current-event-mismatched L2 rejects Market/IOC; caller prices and stale MarketView quotes are never signed as arrival evidence.

Rollout and rollback

Code-only runtime instrumentation; no service restart or trading activation in this PR. Revert the squash commit to remove the added event field and signed metrics.

Scope exception

None.

@proerror77
proerror77 enabled auto-merge (squash) August 9, 2026 21:05
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds optional arrival-price data to order lifecycles and execution events. Market orders capture the resolved executable quote. Execution paths propagate the value. Runtime attribution preserves it across duplicates and records arrival-price slippage.

Changes

Arrival-price instrumentation

Layer / File(s) Summary
Market arrival-price contract and enrichment
rust_hft/market-core/ports/src/events.rs, rust_hft/market-core/engine/src/lib.rs, agent-worktree.yml
Market lifecycles and ExecutionEvent::OrderNew now support optional arrival prices. Market intents record the resolved executable quote.
Execution-event arrival-price propagation
rust_hft/market-core/engine/src/execution_worker.rs, rust_hft/execution-gateway/adapters/adapter-backpack/src/lib.rs, rust_hft/market-core/runtime/src/system_builder/simulated_execution.rs
Execution workers propagate lifecycle arrival prices. Paper and simulated orders emit None. Tests cover provenance and propagation.
Runtime attribution and duplicate handling
rust_hft/apps/live/src/runtime_attribution.rs
Order metadata preserves arrival prices across valid duplicates. Conflicting duplicates are ignored. Fill attribution records arrival slippage, evidence timestamps, and Binance Spot market metrics.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MarketIntentProcessor
  participant ExecutionWorker
  participant ExecutionEvent
  participant RuntimeAttribution

  MarketIntentProcessor->>ExecutionWorker: resolve and store lifecycle arrival_price
  ExecutionWorker->>ExecutionEvent: emit OrderNew with arrival_price
  ExecutionEvent->>RuntimeAttribution: process order event
  RuntimeAttribution->>RuntimeAttribution: preserve metadata and calculate arrival slippage
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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
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 identifies the runtime feature: emitting signed arrival-cost evidence.
Description check ✅ Passed The description covers every template section and provides scope, validation, counterexamples, rollout, rollback, and scope exception details.
✨ 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/runtime-arrival-instrumentation

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

Copy link
Copy Markdown
Owner Author

@codex review exact head ee77203: runtime-only arrival evidence including IOC limits.

proerror77 and others added 2 commits August 9, 2026 21:06
* feat(research): define CEX replay snapshot V2

* fix(research): bind V2 snapshot evidence timelines

* fix(research): keep V1 writer until consumers migrate

* fix(research): harden V2 PIT evidence bounds

* fix(research): bind complete PIT evidence sets

* fix(research): close V2 evidence coverage gaps

* fix(research): bind fee evidence to account

* feat(research): materialize aggregate trade flow

* feat(research): verify real lifecycle latency evidence

* fix(research): bind V2 series identity

* chore: refresh trade feature stack base

* chore: refresh trade feature stack base

* fix(research): bind latency evidence to fee account

* refactor(research): pass prebuilt source lineage

* chore: refresh latency evidence stack base

* test(research): keep aggregate trade ids contiguous

* chore: refresh latency evidence stack base

* fix(research): bind real arrival latency evidence

* fix(research): authenticate runtime instrument market

---------

Co-authored-by: Sonic Shih <sonic.shih@mandonothing.com>

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee77203dcf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rust_hft/execution-gateway/adapters/adapter-backpack/src/lib.rs
Comment thread rust_hft/market-core/engine/src/execution_worker.rs Outdated
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 9ea37ed: verify runtime-only diff after stacked research merge was reverted, including Market and IOC arrival capture.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 9ea37edfdb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head b8a8869: verify Binance Spot/USD-M signed fill market identity and focused futures check.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b8a88691bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 74bec42: verify IOC protection limits are excluded from arrival cost and duplicate adapter OrderNew preserves genuine arrival/lifecycle evidence.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 74bec42dfd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…-instrumentation

# Conflicts:
#	agent-worktree.yml
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 57463cf after main integration: verify runtime-only genuine Market arrival evidence, IOC exclusion, and duplicate event preservation.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 57463cffb6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rust_hft/market-core/engine/src/execution_worker.rs Outdated
Comment thread rust_hft/apps/live/src/runtime_attribution.rs Outdated
Comment thread rust_hft/apps/live/src/runtime_attribution.rs Outdated
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head cf40891: verify genuine Market arrival evidence, IOC exclusion, duplicate preservation, and strict-lint-only follow-up.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: cf40891db5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 7739306: verify engine-captured executable quote provenance, conflicting duplicate invalidation, and removal of unreachable USDM runtime claims.

@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/apps/live/src/runtime_attribution.rs (1)

348-390: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Make conflicting duplicate invalidation terminal.

A duplicate with a different arrival_price is accepted because the conflict predicate does not compare it. The new value then overwrites the prior arrival evidence.

After a detected conflict, this code removes metadata but stores no rejected-order marker. A later matching OrderNew can recreate metadata and make subsequent fills attributable again.

Treat a populated, different arrival_price as a conflict. Retain an invalid-order tombstone after any conflict. Add a test with a conflicting event, a later matching duplicate, and a fill.

Also applies to: 1384-1410

🤖 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/apps/live/src/runtime_attribution.rs` around lines 348 - 390, Update
the duplicate-order conflict checks in the shown handler and corresponding flow
around the second occurrence to compare populated, differing arrival_price
values. When any conflict is detected, preserve a rejected-order tombstone in
state so later matching OrderNew events cannot recreate metadata or attribute
fills; add coverage for a conflicting event, a subsequent matching duplicate,
and its fill.
🤖 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/market-core/engine/src/lib.rs`:
- Around line 1658-1674: Update the retain_mut closure in the market-intent
processing block so non-market envelopes preserve their existing
lifecycle.arrival_price. Only assign arrival_price from the enriched
envelope.intent.price when a market intent is successfully accepted; otherwise
leave it unchanged, while retaining the current rejection behavior for failed
market enrichment.

---

Outside diff comments:
In `@rust_hft/apps/live/src/runtime_attribution.rs`:
- Around line 348-390: Update the duplicate-order conflict checks in the shown
handler and corresponding flow around the second occurrence to compare
populated, differing arrival_price values. When any conflict is detected,
preserve a rejected-order tombstone in state so later matching OrderNew events
cannot recreate metadata or attribute fills; add coverage for a conflicting
event, a subsequent matching duplicate, and its fill.
🪄 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: 039bbd59-4527-41b8-8aea-ff5e6f29be30

📥 Commits

Reviewing files that changed from the base of the PR and between fd3afe6 and 7739306.

📒 Files selected for processing (7)
  • agent-worktree.yml
  • rust_hft/apps/live/src/runtime_attribution.rs
  • rust_hft/execution-gateway/adapters/adapter-backpack/src/lib.rs
  • rust_hft/market-core/engine/src/execution_worker.rs
  • rust_hft/market-core/engine/src/lib.rs
  • rust_hft/market-core/ports/src/events.rs
  • rust_hft/market-core/runtime/src/system_builder/simulated_execution.rs

Comment thread rust_hft/market-core/engine/src/lib.rs Outdated
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 3fa1673: verify explicit market quote provenance, duplicate invalidation, reachable Spot-only labeling, and non-market lifecycle preservation.

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head a66d5c3: verify terminal tombstones for conflicting order IDs, conflicting arrival prices, engine quote provenance, and Spot-only reachable labeling.

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head d2d5646: final runtime evidence head; focused test now proves a differing populated arrival price tombstones the ID, blocks a later matching replay, and rejects its fill.

…-instrumentation

# Conflicts:
#	agent-worktree.yml
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 58494e6 after merging current main: final arrival evidence and duplicate tombstone contract.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58494e6124

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rust_hft/market-core/engine/src/lib.rs Outdated
Comment thread rust_hft/market-core/engine/src/lib.rs Outdated
@proerror77

Copy link
Copy Markdown
Owner Author

Exact-head review requested for 258c540. IOC now captures the side-specific executable quote without replacing its protection limit; unbound Market/IOC intents bind to the event-sequenced venue, and the unsafe any-venue fallback is removed. Focused engine tests pass. Please review this exact head.

…-instrumentation

# Conflicts:
#	agent-worktree.yml
@proerror77

Copy link
Copy Markdown
Owner Author

Exact-head review requested for afc533b after integrating current main (including merged #791). The arrival-quote fix remains unchanged from 258c540: IOC quote capture preserves its protection limit, unbound intents bind to the event-sequenced venue, and no any-venue quote can be signed. Please review this exact head.

@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head afc533b. Focus on IOC executable arrival quote capture, venue binding, and preservation of the protection limit.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Side::Buy => latest_market_view
.get_best_ask_for_venue(&key)
.map(|(price, _)| price),

P1 Badge Reject stale fallback quotes before signing arrival evidence

When the event-specific book is unavailable or does not match, a venue-bound IOC uses this MarketView fallback without checking the snapshot timestamp. In the engine path inspected, AggregationEngine::cleanup_stale_data has no caller, so a disconnected venue's top-of-book can remain indefinitely and be recorded as the executable arrival quote while other events continue triggering orders. Validate the selected snapshot's freshness against the capture boundary before carrying it into signed fill evidence.

AGENTS.md reference: AGENTS.md:L65-L66

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread rust_hft/market-core/engine/src/lib.rs Outdated
@proerror77

Copy link
Copy Markdown
Owner Author

@codex review exact head 2135b0e. Market and IOC now require the current event-sequenced same-venue L2 executable quote; no cached MarketView fallback remains, and IOC protection limits are preserved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 2135b0e469

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@proerror77
proerror77 merged commit 0bd446d into main Aug 9, 2026
46 of 47 checks passed
@proerror77
proerror77 deleted the codex/runtime-arrival-instrumentation branch August 9, 2026 23:36
proerror77 pushed a commit that referenced this pull request Aug 10, 2026
Materialize verified Binance Spot/USD-M market tapes into the V3 outer
materialization carrying CexReplaySnapshotV2: PIT instrument rules,
account-bound maker/taker fees, independent funding and OI timelines,
aggregate-trade flow, and signed runtime lifecycle latency evidence.

Rebased onto origin/main be9e746, which already contains the signed
arrival-cost evidence (#787), its verification (#788), fee-account
binding (#789), and fee snapshot timers (#793). Refs #794.
proerror77 added a commit that referenced this pull request Aug 10, 2026
* feat(research): publish verified CEX ResearchSnapshot V2

Materialize verified Binance Spot/USD-M market tapes into the V3 outer
materialization carrying CexReplaySnapshotV2: PIT instrument rules,
account-bound maker/taker fees, independent funding and OI timelines,
aggregate-trade flow, and signed runtime lifecycle latency evidence.

Rebased onto origin/main be9e746, which already contains the signed
arrival-cost evidence (#787), its verification (#788), fee-account
binding (#789), and fee snapshot timers (#793). Refs #794.

* fix(research): close lob-pit-materializer review threads

- Delegate runtime latency verification to the hardened
  alpha_domain::runtime_latency_evidence verifier instead of a drifted
  collector-local copy: event IDs are deduplicated before filtering
  (conflicting duplicates fail closed), every fill must carry an
  authenticated instrument_market_{market} identity, and USD-M fails
  closed until a derivatives execution path exists.
- Republish verified fee and reference evidence triplets into the
  materialization artifact directory under content-addressed names so
  the digests recorded in the snapshot resolve to immutable bytes.
- Restructure the test fixture per market: the happy path is Spot
  (signed LiveSmall latency evidence exists only for Spot), a new test
  proves USD-M materialization fails closed, and the aggregate-trade
  assertions now match the (previous, current] bucket semantics.

* fix(research): anchor latency cohort and holding-interval funding

- Publish the complete digest-anchored runtime feedback log and the
  trusted-key document into the artifact directory under their content
  digests, so independent readers can reverify every signed lifecycle
  event and no resolvable-evidence gap remains for the key document.
- Charge USD-M funding settlements over each row's actual
  (current, future] holding interval, matching how the evaluator
  applies funding_bps to the held position; a settlement that precedes
  the holding interval is no longer charged and multi-bucket horizons
  collect every settlement.

---------

Co-authored-by: Sonic Shih <sonic.shih@mandonothing.com>
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.

Capture executable arrival-price provenance

1 participant