Skip to content

feat: add paper sizing policy example#32

Merged
squaeragent merged 1 commit into
zero-intel:mainfrom
txhno:add-paper-sizing-policy-example
May 4, 2026
Merged

feat: add paper sizing policy example#32
squaeragent merged 1 commit into
zero-intel:mainfrom
txhno:add-paper-sizing-policy-example

Conversation

@txhno
Copy link
Copy Markdown
Contributor

@txhno txhno commented May 4, 2026

Summary

Adds a deterministic paper sizing policy example under examples/paper-sizing-policy.

The example sizes proposed paper OrderIntents before execution by:

  • capping risk-increasing notional to a configured paper limit
  • rejecting risk-increasing setups below the confidence threshold
  • preserving reduce_only=true intents without capping or rejecting them

Fixes #27.

Tests

  • PYTHONDONTWRITEBYTECODE=1 PYTHONPATH="$PWD/engine/src" python3 examples/paper-sizing-policy/run.py
  • PYTHONDONTWRITEBYTECODE=1 PYTHONPATH="$PWD/engine/src" python3 -m pytest engine/tests/test_examples.py -q
  • scripts/stale_artifact_check.sh --check
  • git diff --cached --check

Safety 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?

  • No.
  • Yes, partially. I reviewed the diff and tests.
  • Yes, mostly. I reviewed the diff and tests.

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

  • I ran the relevant tests.
  • I updated docs when behavior changed.
  • I did not add real credentials or private runtime data.
  • If AI-assisted, I included the relevant context packet and safety invariant in the PR body.

Summary by CodeRabbit

  • New Features

    • Added a paper sizing policy example with order sizing controls that enforce reduce-only preservation, low-confidence filtering, and notional position caps, with JSON output.
  • Documentation

    • Added README documenting the paper sizing policy example.
  • Tests

    • Added integration test validating the paper sizing policy example.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

📝 Walkthrough

Walkthrough

This PR introduces a deterministic paper sizing policy example that demonstrates sizing orders without submission. The implementation includes a SizingDecision dataclass and size_paper_order() function that applies three control rules: preserving reduce-only orders, rejecting low-confidence setups, and capping risk-increasing notional. A test validates the example runs from the repo root and produces expected JSON output.

Changes

Paper Sizing Policy Example

Layer / File(s) Summary
Data Shape
examples/paper-sizing-policy/run.py
SizingDecision frozen dataclass captures policy decision with name, input/output intents, action, and reason.
Core Logic
examples/paper-sizing-policy/run.py
size_paper_order() applies three rules: preserve reduce-only orders, reject below min_confidence, cap exceeding max_notional_usd.
Serialization
examples/paper-sizing-policy/run.py
order_to_dict() and SizingDecision.to_dict() serialize intents and decisions to JSON-compatible dicts with notional deltas.
Example Execution
examples/paper-sizing-policy/run.py
main() instantiates RiskLimits, constructs candidate OrderIntents, computes decisions, and outputs sorted JSON payload with metadata and results.
Documentation
examples/paper-sizing-policy/README.md
Run instructions, purpose statement, policy guarantees (caps, rejects, preserves), and note on stable JSON output for testing.
Integration Test
engine/tests/test_examples.py
test_paper_sizing_policy_example_runs_from_repo_root() executes the example, parses JSON, and asserts policy name, submission behavior, and three decision outcomes (capped, rejected, preserved).

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit admires the policy so neat,
Paper-sized orders, no exchange to meet!
Confidence checked, notional capped right,
Reduce-only flows preserved in sight! 🐰
Tests ensure correctness—a delightful delight!

🚥 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'feat: add paper sizing policy example' directly and clearly summarizes the main change—adding a new paper sizing policy example to the codebase.
Linked Issues check ✅ Passed All coding requirements from issue #27 are fully met: paper-only example created under examples/, sizing policy caps notional, enforces confidence thresholds, preserves reduce-only intents, includes tests, and avoids network/exchange dependencies.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the paper sizing policy example: new run.py implementation, README documentation, and integration test—no unrelated or out-of-scope modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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)
engine/tests/test_examples.py (1)

90-94: ⚡ Quick win

Add 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

📥 Commits

Reviewing files that changed from the base of the PR and between a24d5d6 and 8730545.

📒 Files selected for processing (3)
  • engine/tests/test_examples.py
  • examples/paper-sizing-policy/README.md
  • examples/paper-sizing-policy/run.py

Comment on lines +53 to +65
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,
),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 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})
PY

Repository: zero-intel/zero

Length of output: 147


🏁 Script executed:

# Examine the file structure
fd "run.py" | grep -i paper

Repository: 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 -80

Repository: zero-intel/zero

Length of output: 2877


🏁 Script executed:

# Find and read OrderIntent definition to verify validation
fd "models.py" | head -5

Repository: zero-intel/zero

Length of output: 92


🏁 Script executed:

# Search for OrderIntent class definition and validation
rg "class OrderIntent" -A 20

Repository: 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}")
PY

Repository: 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.

@squaeragent squaeragent merged commit 06f3259 into zero-intel:main May 4, 2026
1 check passed
@squaeragent
Copy link
Copy Markdown
Contributor

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.

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.

Good First Issue: Add a paper sizing policy example

2 participants