Skip to content

refactor: share the /pnl report path so other callers build the same report [RAI-3092] - #1684

Open
JuaniRios wants to merge 1 commit into
rai-2998-liq-metrics-log-countsfrom
rai-2998-liq-metrics-pnl
Open

JuaniRios wants to merge 1 commit into
rai-2998-liq-metrics-log-countsfrom
rai-2998-liq-metrics-pnl

Conversation

@JuaniRios

@JuaniRios JuaniRios commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Part of RAI-3092.

Moves the body of GET /pnl into one shared function, run_pnl_report, so the PnL metrics task builds the same report the endpoint builds. Item 4 of the RAI-2998 stack, part 1 of 3.

Live effect: none. /pnl returns the same bodies and status codes · Risk: low (a refactor of the /pnl request path) · Ships: on the next bot release

Why

The liq_pnl_* series must be the numbers /pnl gives for the same window. The exporter got them by calling /pnl. If the bot calls the same function, there is nothing to keep in sync.

Decisions

  • run_pnl_report(deps, query, admission) in src/dashboard/pnl/source.rs does what the handler did, in the same order: check the query, bring the ledger current within 10 s, check asOfRowid against the head, take a permit, fetch the Alpaca activities, replay.
  • Only the permit source is a parameter. PnlReportAdmission::with_permits(n) gives a caller its own semaphore, so the metrics task (next PR) never takes one of the 2 live /pnl permits.
  • PnlReportError has 3 variants (report, catch-up timeout, Alpaca). The handler keeps only the mapping to the same 400, 500, 502 and 503 responses as before.

Proof

  • clippy, fmt, and pre-commit clean. The existing /pnl tests pass unchanged.
  • New tests: the route and run_pnl_report return identical JSON for the same query, and each call sees fills recorded before it (each one runs its own ledger catch-up).
  • Full st0x-hedge suite on the top of this 3-PR stack: 4814 passed.
  • Multi-model review loop over the 3 PRs (sol, grok, composer, flash, and opus) converged. Details in the top PR.
  • Not verified: nothing specific to this PR; /pnl is exercised on staging after release.

Merge conditions

Same as the stack below.

@linear-code

linear-code Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

RAI-2998

RAI-3061

RAI-3092

JuaniRios commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add the label add-to-gt-merge-queue to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@JuaniRios

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Team
  • Run ID: f5757728-eb5f-4655-b5a7-65d36bcf653e

📥 Commits

Reviewing files that changed from the base of the PR and between 11fd31c and 136a2d4.


📒 Files selected for processing (3)
  • src/api.rs
  • src/dashboard/pnl/mod.rs
  • src/dashboard/pnl/source.rs

Included review availability: This review used your included allowance. 4 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 6 reviews per hour.



Walkthrough

The /pnl handler now calls run_pnl_report for query validation, ledger catch-up, permit acquisition, Alpaca activity retrieval, and report construction. The shared flow returns distinct errors for report failures, catch-up timeouts, and activity-fetch failures. Tests compare the route output with the shared report and check reports across successive fills.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 136a2

The change extracts the /pnl report flow into a shared function without changing response bodies or status codes. No concrete merge-blocking risk was found.

Pre-merge checks | Passed 4 | Inconclusive 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage Inconclusive Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. (1 skipped: 1… 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 and concisely describes the main change: sharing the /pnl report path so other callers build the same report.
Description check Passed The description directly explains the run_pnl_report refactor, its purpose, behavior, risks, tests, and merge context.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. (1 skipped: 1 too large.)



✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR


  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-log-counts branch from 8d9608f to a8677c5 Compare October 9, 2026 12:53
@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-pnl branch 2 times, most recently from 36276b0 to 7decac8 Compare October 9, 2026 14:41
@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-log-counts branch from a8677c5 to 123d4a8 Compare October 9, 2026 14:41
@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-pnl branch from 7decac8 to c79354b Compare October 9, 2026 17:59
@JuaniRios

Copy link
Copy Markdown
Collaborator Author

@rain-marvin review

@rain-marvin

rain-marvin Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

🔎 Reviewing c79354b, started by @JuaniRios. The review will appear here when it's done.

@rain-marvin rain-marvin Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR moves the body of GET /pnl into one shared function, run_pnl_report. The metrics task in the next PR can then build the same report that the endpoint returns. PnlReportAdmission::with_permits gives a non-HTTP caller its own semaphore, and PnlReportError carries the three failure kinds back to the handler. The handler maps them to the same 400, 500, 502 and 503 responses as before.

Overall read: a clean extraction. Every lane checked the order of the steps (query checks, the 10 s catch-up, the asOfRowid check, the permit, the Alpaca fetch, the replay with Utc::now()) and the error mapping against the old handler, and found no change in status codes, bodies, or permit lifetime. The live route still uses the shared 2-permit admission. The diff adds no business-sensitive data to this public repo. There are two minor comments. The catch-up timeout warning moved into the shared function, so its log target and text changed and the metrics caller upstack logs that error twice. Also, the new 502 and 503 mapping arms have no test.

claude-opus-5-5 · high · 50 min

Comment thread src/dashboard/pnl/source.rs
Comment thread src/api.rs
@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-pnl branch from c79354b to 7542282 Compare October 9, 2026 22:27
@JuaniRios
JuaniRios force-pushed the rai-2998-liq-metrics-log-counts branch from 0eb74db to 045776e Compare October 9, 2026 22:27
@JuaniRios JuaniRios changed the title [RAI-2998] Share the /pnl report path so other callers build the same report refactor: share the /pnl report path so other callers build the same report [RAI-3092] Oct 9, 2026
This was referenced Oct 10, 2026

This branch has not been deployed

No deployments
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