feat: add paper sizing policy example#32
Conversation
📝 WalkthroughWalkthroughThis PR introduces a deterministic paper sizing policy example that demonstrates sizing orders without submission. The implementation includes a ChangesPaper Sizing Policy Example
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
engine/tests/test_examples.py (1)
90-94: ⚡ Quick winAdd an invariant assertion for capped notional vs configured limit.
This test checks one fixture value (
500), but it doesn’t explicitly guard the policy contract that capped output must be<= payload["limits"]["max_notional_usd"]. Adding that assertion makes regressions easier to catch.Proposed test hardening
capped = decisions["capped-btc"] assert capped["action"] == "cap" assert capped["input"]["notional_usd"] == 1200 assert capped["output"]["notional_usd"] == 500 + assert capped["output"]["notional_usd"] <= payload["limits"]["max_notional_usd"]🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@engine/tests/test_examples.py` around lines 90 - 94, Add an invariant assertion that the capped decision respects the configured max notional: after retrieving decisions["capped-btc"] (stored as capped) add an assertion that capped["output"]["notional_usd"] <= payload["limits"]["max_notional_usd"] (or the test's fixture variable that holds the policy payload) so the test enforces the policy contract in addition to the specific numeric check; keep the existing checks for action and values and ensure the payload/fixture variable referenced is in scope.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@examples/paper-sizing-policy/run.py`:
- Around line 53-65: Replace the upward-capable round(...) quantization for
capped_quantity with a downward (floor) quantization to 8 decimal places so the
produced OrderIntent never exceeds limits.max_notional_usd; compute
capped_quantity by floor-quantizing limits.max_notional_usd / intent.price to 8
decimals (e.g., math.floor or Decimal with ROUND_DOWN) and if the resulting
capped_quantity <= 0, reject/return a failure instead of constructing the
SizingDecision/OrderIntent so you don't create an invalid order.
---
Nitpick comments:
In `@engine/tests/test_examples.py`:
- Around line 90-94: Add an invariant assertion that the capped decision
respects the configured max notional: after retrieving decisions["capped-btc"]
(stored as capped) add an assertion that capped["output"]["notional_usd"] <=
payload["limits"]["max_notional_usd"] (or the test's fixture variable that holds
the policy payload) so the test enforces the policy contract in addition to the
specific numeric check; keep the existing checks for action and values and
ensure the payload/fixture variable referenced is in scope.
🪄 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
Run ID: 8d97e140-2618-4e5a-8021-eea7afa22766
📒 Files selected for processing (3)
engine/tests/test_examples.pyexamples/paper-sizing-policy/README.mdexamples/paper-sizing-policy/run.py
| if intent.notional_usd > limits.max_notional_usd: | ||
| capped_quantity = round(limits.max_notional_usd / intent.price, 8) | ||
| return SizingDecision( | ||
| name=name, | ||
| input=intent, | ||
| output=OrderIntent( | ||
| symbol=intent.symbol, | ||
| side=intent.side, | ||
| quantity=capped_quantity, | ||
| price=intent.price, | ||
| confidence=intent.confidence, | ||
| reduce_only=intent.reduce_only, | ||
| ), |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verifies that round(..., 8) can breach the cap.
python - <<'PY'
limit = 500.0
price = 3.0
qty = round(limit / price, 8)
notional = qty * price
print({"qty": qty, "notional": notional, "limit": limit, "breaches_limit": notional > limit})
PYRepository: zero-intel/zero
Length of output: 147
🏁 Script executed:
# Examine the file structure
fd "run.py" | grep -i paperRepository: zero-intel/zero
Length of output: 125
🏁 Script executed:
# Read the file to verify the code and line numbers
cat -n examples/paper-sizing-policy/run.py | head -80Repository: zero-intel/zero
Length of output: 2877
🏁 Script executed:
# Find and read OrderIntent definition to verify validation
fd "models.py" | head -5Repository: zero-intel/zero
Length of output: 92
🏁 Script executed:
# Search for OrderIntent class definition and validation
rg "class OrderIntent" -A 20Repository: zero-intel/zero
Length of output: 1383
🏁 Script executed:
# Verify that the proposed floor-based fix would guarantee limit compliance
python - <<'PY'
import math
# Test case 1: Same as initial verification
limit = 500.0
price = 3.0
raw_quantity = limit / price
capped_quantity = math.floor(raw_quantity * 1e8) / 1e8
notional = capped_quantity * price
print(f"Test 1: limit={limit}, price={price}")
print(f" capped_quantity={capped_quantity}, notional={notional}")
print(f" respects_limit={notional <= limit}")
# Test case 2: Very small limit causing round-to-zero
limit = 0.01
price = 100.0
raw_quantity = limit / price
capped_quantity = math.floor(raw_quantity * 1e8) / 1e8
print(f"\nTest 2: limit={limit}, price={price}")
print(f" capped_quantity={capped_quantity}, would_fail_validation={capped_quantity <= 0}")
# Test case 3: Normal case
limit = 1000.0
price = 50.0
raw_quantity = limit / price
capped_quantity = math.floor(raw_quantity * 1e8) / 1e8
notional = capped_quantity * price
print(f"\nTest 3: limit={limit}, price={price}")
print(f" capped_quantity={capped_quantity}, notional={notional}")
print(f" respects_limit={notional <= limit}")
PYRepository: zero-intel/zero
Length of output: 349
Use downward quantization for cap logic to guarantee the limit is never exceeded.
Line 54 uses round(..., 8), which can round up and produce output.notional_usd > limits.max_notional_usd; it can also round to 0.0 and fail OrderIntent validation. Use floor-quantization and reject when the quantized quantity is non-positive.
Proposed fix
import json
+import math
from dataclasses import dataclass
@@
if intent.notional_usd > limits.max_notional_usd:
- capped_quantity = round(limits.max_notional_usd / intent.price, 8)
+ raw_quantity = limits.max_notional_usd / intent.price
+ capped_quantity = math.floor(raw_quantity * 1e8) / 1e8
+ if capped_quantity <= 0:
+ return SizingDecision(
+ name=name,
+ input=intent,
+ output=None,
+ action="reject",
+ reason="paper limit too small for instrument price/precision",
+ )
return SizingDecision(
name=name,
input=intent,
output=OrderIntent(🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/paper-sizing-policy/run.py` around lines 53 - 65, Replace the
upward-capable round(...) quantization for capped_quantity with a downward
(floor) quantization to 8 decimal places so the produced OrderIntent never
exceeds limits.max_notional_usd; compute capped_quantity by floor-quantizing
limits.max_notional_usd / intent.price to 8 decimals (e.g., math.floor or
Decimal with ROUND_DOWN) and if the resulting capped_quantity <= 0,
reject/return a failure instead of constructing the SizingDecision/OrderIntent
so you don't create an invalid order.
|
Merged. Thank you for adding a deterministic paper sizing policy example with test coverage. This is exactly the kind of paper-first contributor path we want. |
Summary
Adds a deterministic paper sizing policy example under
examples/paper-sizing-policy.The example sizes proposed paper
OrderIntents before execution by:reduce_only=trueintents without capping or rejecting themFixes #27.
Tests
PYTHONDONTWRITEBYTECODE=1 PYTHONPATH="$PWD/engine/src" python3 examples/paper-sizing-policy/run.pyPYTHONDONTWRITEBYTECODE=1 PYTHONPATH="$PWD/engine/src" python3 -m pytest engine/tests/test_examples.py -qscripts/stale_artifact_check.sh --checkgit diff --cached --checkSafety Impact
Does this touch execution, sizing, credentials, authentication, risk gates, or operator commands?
Yes, sizing. This is a paper-only example that never calls
PaperEngine.submit, live APIs, network services, exchange credentials, wallets, or private runtime state. The smoke test covers capped notional, low-confidence rejection, and reduce-only preservation.AI Assistance
Was this change authored or materially assisted by a coding/design agent?
Context packet: issue #27 asks for a deterministic paper sizing policy example under
examples/with smoke coverage for capped size, rejected low-confidence setup, and reduce-only behavior. Safety invariant: the example must be paper-only and must not submit orders or touch live execution, credentials, wallets, network services, or private runtime state.Checklist
Summary by CodeRabbit
New Features
Documentation
Tests