Skip to content

Apply rc.7 simulation cap and floor rules - #5

Merged
degencodebeast merged 7 commits into
mainfrom
analysis/rc7-cap-floor-review
Nov 14, 2026
Merged

degencodebeast merged 7 commits into
mainfrom
analysis/rc7-cap-floor-review

Conversation

@KaluArunsi

@KaluArunsi KaluArunsi commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Implements the rc.7 sell-rule model and Rust comparison vectors, including one shared marked/unmarked floor, marked-cap holds, creator fee share, and integer token-base-unit fill decisions.

Contract comparison

  • analysis/simulation/results/cap-cases.json uses interface 1.0.0-rc.7, contains 42 cases, and has SHA-256 522483dd44113deacf11f3c357e61481aa0124504943e24a497a55a4012eb0c9.
  • Wallet-wide sells use the exact integer balance (all) when the account has no unrelated balance; targeted resells use integer buy output. Fill status compares filled base units with the submitted base-unit amount.
  • The known cap_left_yocto and cap_left_before_yocto differences, within 1e9 yoctoNEAR, are listed in analysis/simulation/METHOD.md.

Verification

  • python3 -m unittest discover -s tests -v — 41 passed.
  • Regenerated checkpoint and full simulation tables, charts, test 11 results, and the rc.7 cap-case JSON.
  • Added a golden byte-for-byte cap-case regeneration test.
  • git diff --check passes.

Merge Danger

Door: Two-way

Blast Radius: Analysis model, generated simulation tables, and findings only.

@degencodebeast

Copy link
Copy Markdown
Owner

Thanks for the rc.7 update. You noted that the Rust tests could not run on your machine, so I ran your 41 cases on the Rust rule engine (integration branch, interface 1.0.0-rc.7). 33 match and 8 do not. None of the 8 shows an engine bug. Five need a fix on your side, and three are questions where I give you the answer to use.

The hash of cap-cases.json matches your PR (fc435c11...), and python3 -m unittest discover -s tests passes here too (32 tests). Regenerating the file gives the same bytes.

Fix in the model (2)

  1. floor-unmarked-first, action 0. The model sells 5M unmarked tokens at block 0. The engine refuses with FC_EARLY_WINDOW. A sell fills the marked part first (INTERFACE.md "A sell fills the marked part first"), and a marked sell in blocks 0 to N is refused (INTERFACE.md, FC_EARLY_WINDOW; ADR 0008). In simulate.py line 195, if block<=self.policy.n: marked=0. skips the marked part, so the sell goes on to the unmarked part. An account that holds marked tokens should get the refusal for the whole sell.
  2. floor-marked-first, action 0. Token counts match, but the model gives limit_reason cap_full and the engine gives floor. The marked branch sets floor (line 204), then the unmarked branch overwrites it (line 223). INTERFACE.md says floor when "the marked part of a sell fills only its floor and the cap stops the rest". Keep the marked part's reason when the unmarked part fills nothing.

Fix in the vectors (3)

  1. marked-allowance-limit action 2 and small-buy-sell-at-full-cap action 1. The sell amount is a few base units above the account's integer balance (relative 5e-16 and 8e-14), so the engine refuses it as more than the balance. These actions sell the whole balance, so please write the amount as "all", as we agreed on PR Add NEAR launch replay and sell-rule simulation analyses #3. With the exact balance, every field matches.
  2. marked-resale-two-account-cap action 2. The resold amount is the model's float output of the earlier buy, a few base units above the integer balance the engine holds. The engine then fills partial with allowance; the model says full. Same fix: sell "all", or carry the integer balance forward.

Questions, with the answer to use (3)

  1. marked-sale-reduces-unmarked-floor, actions 0 and 1. near_out and cap_used differ by 2.5e-6 relative (model 0.108791480633 NEAR, engine 0.108791208791 NEAR). The model prices the marked part and the unmarked part as two swaps (lines 206 and 226), so the fee of the first stays in the pool for the second. The engine prices one sell as one swap. Please use one swap: INTERFACE.md gives one fee_near per sell, "the whole swap fee on this sell".
  2. full-cap-floor-and-second-sell action 2, and cap-left-s-30 actions 1 and 2. The model expects fill none with 0 tokens. The engine sells 16 to 17 base units for about 0 NEAR. The model rounds small values to zero (lines 136 and 144: <=1e-12 and lo<1e-6). The interface has no minimum sell, so please remove these cutoffs, or compare in integer base units.

METHOD.md

  • Say that a sell is priced as one swap (after fix 5).
  • "A large marked sell is held": the interface condition is narrower, marked floor_left 0.
  • "A marked part held by the cap cannot fill through the unmarked floor": ADR 0005 states this now, so cite it.

After these fixes, please export cap-cases.json again (still 1.0.0-rc.7) and push to this PR. I will rerun all 41 cases and post the result. If you can push by 11:00 UTC today (14 Nov), the cross-check can still make this release.

@degencodebeast

Copy link
Copy Markdown
Owner

Thanks for the quick fix. I ran all 42 cases from 5165885 on the Rust rule engine again. 33 match and 9 do not. None of the 9 shows an engine bug. Your regenerated cap-cases.json is byte-identical to the committed one, and the 32 Python tests pass.

Fixed: floor-unmarked-first (and the new early-window-refuses-mixed-sell matches with FC_EARLY_WINDOW), floor-marked-first, marked-sale-reduces-unmarked-floor, marked-allowance-limit and small-buy-sell-at-full-cap.

What is left is float rounding in the model. The engine works in whole base units, so the model must compare in base units too.

  1. full-cap-floor-and-second-sell action 2 and cap-left-s-30 actions 1 and 2. Fill and token count now match, but the model gives limit_reason floor and the engine gives cap_full. The unmarked floor_left is already 0, and the 16 to 17 base units fill because they pay 0 NEAR. simulate.py line 269 treats "within 1e-9 tokens of the floor" as "at the floor". The rc.7 note in INTERFACE.md says floor applies when the sell "fills exactly its unmarked floor", so please compare in whole base units.
  2. marked-resale-two-account-cap actions 0 and 3. The model gives cap_full and the engine gives allowance. The whole marked allowance fills, and the cap has room (cap_left 4.76 NEAR before a 1.0 NEAR sell). simulate.py line 271 compares marked_filled, rounded down to a base unit by search_fill, with the unrounded float marked_most, so a gap below one base unit looks like a cap hold. Please round marked_most down to a base unit before the comparison.
  3. marked-resale-two-account-cap action 2 (from the first review). The resale amount is still the float buy output (line 470), 17444253349 base units above what the engine's buy returns. "all" does not fit here, because the account still holds marked tokens. Please compute the buy output in integer base units (floor of the exact result), so the vector carries the amount the account really holds.
  4. cap_left_yocto after a sell that the cap stops: the model gives 38095238 to 95238095 yocto, and the engine gives 0. The cause is float pool.real and cap_used in cap_left (lines 136 to 143). No change is needed from you for this one. On our side we will accept a difference of up to 1e9 yocto (1e-15 NEAR) in cap_left_yocto.

Two text updates:

  • The PR description still shows the old hash. The new cap-cases.json is d0dd6f9a....
  • METHOD.md says "No rule-text/code disagreement remains in the rc.7 comparison cases." Please change it after these fixes, or say which cases still differ.

If you can push by 11:00 UTC, I will run the cases again the same hour.

@degencodebeast

Copy link
Copy Markdown
Owner

Thanks, d5b970d fixes all three items. I ran the 42 cases again: 37 match under the rules from my last comment, and 5 do not. None of the 5 shows an engine bug. The hash in the PR description matches, the regenerated file is byte-identical, and the 32 Python tests pass.

For the record, "match" here means: fill, limit_reason and refusal_code are equal; numeric fields agree within a relative 1e-8; and cap_left_yocto also matches when it differs by up to 1e9 yocto.

One cause for the 5 cases

The buy output is now an integer, but these sells still take their amount from the float balance. The amount is then a few base units above what the account holds.

  1. same-account-round-trip-s-0 and -s-30, action 1. Line 428 sets requested from the float w.tokens, so requested_base is above the integer balance. Engine.sell then sells only the balance (lines 220 and 221), and line 433 sets fill against the larger value. The vector says fill partial with limit_reason null. The engine sells the whole balance with fill full.
  2. post-sell-cap-s-0 and -s-30, action 1. Line 452 sells e.wallet('large').tokens, a float. The amount is 26290165530671860000000000, and the integer balance from action 0 is 26290165530671859785783836. The engine refuses a sell above the balance. With the exact balance, every field matches.
  3. marked-sell-buy-resell, action 2. Line 508 resells bought, a float, so the amount is 900081007290656100000000. The model's integer buy gave 900081007290656069046214, so the model fills partial. Use the integer buy amount here.

Line 513 already does this right: it passes amount_base=e.pool.last_buy_tokens_base. Please do the same in these three places: pass the integer balance or the integer buy amount as amount_base, and set fill against the amount actually sold.

One more point: the model's integer buy and the engine's buy can differ by up to about 1.5e11 base units. A vector that sells the model's exact buy amount works only when the engine bought the same or more, so selling "all" is safer where the account holds nothing else.

METHOD.md

The paragraph on accepted differences names the wrong cases. At d5b970d, cap_left_yocto in full-cap-floor-and-second-sell and cap-left-s-30 matches the engine exactly. The cases that differ within 1e9 yocto are band-sell-above-allowance, post-sell-cap-s-0, post-sell-cap-s-30, band-round-trip-credit, small-buy-price-rise-cap and marked-and-unmarked-old-plus-buy-resell. Please update that list, and the PR description line that points to it. "Only numeric differences remain" will be true after the fix above, not before it.

If you can push by 11:00 UTC, I will run the cases again the same hour.

@degencodebeast

Copy link
Copy Markdown
Owner

7430a6d passes the cross-check. I ran all 42 cases on the Rust rule engine: all 42 match under the rules from my last comment, with fill, limit_reason and refusal_code exact in every case. The hash in the PR description matches (522483dd...), the regenerated file is byte-identical, and the 32 Python tests pass. Thanks for the fast turnaround on all three rounds.

I also ran a code review on the full diff, for the parts the 42 cases do not reach. Nothing blocks the cross-check, but two items change results outside the vectors. Could you fix these in a follow-up commit on this PR, by 11:00 UTC if you can?

Fix

  1. Marked floor reserve (simulate.py lines 178 to 181). floor_tokens now uses reserve_snapshot(hour), the 1 h reserve, for the marked floor. Before this PR it took the reserve from the 24 h marked snapshot (amounts,total,reserve=self.snaps[...]), which is what ADR 0005 and METHOD.md lines 19 and 20 say. A probe with a buy in hour 2 of the window gives a marked floor of 331014 tokens in the model and 480000 under the 24 h rule. No vector has different 1 h and 24 h reserves, so the cross-check cannot see it.
  2. Test 11 scaling (lines 370 to 374). early_exit scales w.marked and w.tokens, but tokens_base and marked_base keep their unscaled values from seed(). sell() then caps with the base values and writes them back, so the 9%, 23% and 41% scaling is lost after the first sell. In results/test-11.csv the tokens left at 41% fell from 22,847,279 to 5,720,878. Please scale the base fields too and export test 11 again.

Tests

  1. Add one golden test: regenerate cap_cases() and compare it with the committed cap-cases.json. A model change that changes the vectors currently passes all 32 tests.
  2. These rules have no test and no vector that fails when the line changes: the rc.7 marked floor that subtracts the unmarked tokens sold (line 182), the small-sell branch (lines 278 to 281), marked tokens leaving first in transfer (line 142), the 4% creator group cap (line 123), the band boundary (line 96), the brake comparison (line 250) and the ceil rounding of marked_out (line 211). One vector or one test each would cover them. If time is short, the first two matter most.

METHOD.md

  1. Test 11 text (lines 67 to 69) should match the fix in item 2.
  2. Lines 32 and 33 say the penalty-free part is "allowance - tokens already sold". The code uses the allowance net of buys (room_base, line 252), and that is the rule. Please change the text.
  3. Lines 88 and 93 refer to "the reviewer". A public reader cannot see this thread, so please state the tolerance as a rule: cap_left_yocto and cap_left_before_yocto may differ by up to 1e9 yocto because pool state is binary floats.
  4. Lines 80 and 87 say the model is "derived from the published rules" and "implemented independently". The sell planner follows the engine's fill plan step by step (the floor, the marked part first, the fill search and the limit_reason order). Please say that plainly, for example: "The sell planner follows the structure of the contract's fill plan; the pool math, the class rules and the test scenarios are written separately." Then the 42/42 match is not read as two independent derivations.

Small notes, no change needed now: cap_used sums over a set of names, so sorting them would make the float sum order fixed (the output was stable across hash seeds in my runs). transfer does not record the 24 h snapshot, unlike the contract, but no vector uses it.

When you push, I will run the 42 cases again and post the result.

@degencodebeast

Copy link
Copy Markdown
Owner

163bc16 fixes everything from my last comment. Thanks for turning it around so early.

What I checked:

  • The 42 cases on the Rust rule engine: all 42 match under the agreed rules, with fill, limit_reason and refusal_code exact in every case. cap-cases.json is unchanged (522483dd..., same as the PR description).
  • The 41 Python tests pass.
  • Marked floor: my probe with a buy in hour 2 now fills 480,000 tokens with limit_reason floor, the 24 h rule.
  • Test 11: the integer balances now scale with the rest. At 41% the tokens left are back to 22,847,279, and test-11.csv matches a fresh export byte for byte.
  • The golden test fails when I change one byte of cap-cases.json.
  • I changed each of the seven rules in a scratch copy, one at a time. The tests catch every change but one.
  • METHOD.md: all four text items are fixed, and the list of cases with cap_left differences matches my run.
  • Tests 1 to 10 regenerate with the same rows as the committed files (only the row order differs), so the floor fix changes no committed result except test 11.

One small nit, not a blocker: the new creator test covers the 4% creator cap on transfers (simulate.py:141), not on buys (simulate.py:123). A change to the buy-side cap still passes every test. One more assertion in that test would cover it, if you have a minute.

I will follow up here about the merge.

@degencodebeast
degencodebeast merged commit 92be4b9 into main Nov 14, 2026
2 checks passed
@degencodebeast

Copy link
Copy Markdown
Owner

Following up on the merge: this PR is in main now (merge commit 92be4b9), so the rc.7 cap and floor rules are what the simulation runs. Thank you for the fast rounds on this one and on #3.

One more ask, if you have time today: #4, the short analysis/FINDINGS.md. The README will link to it as the summary of your replay and simulation results. The scope is in the issue. Please keep it to numbers that rerun from analysis/ with the documented command.

To make it into the submission, I need the PR by 17:00 UTC (18:00 WAT) today, so there is time to review and merge it before our freeze. If today does not work, just say so here and I will close #4 for now. No problem either way.

The creator buy-cap test I mentioned is still optional and not needed for this.

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.

2 participants