Order Ledger Review
A review-and-harden task on a small Rust money ledger: an assistant generated it, with orders, discounts, refunds and a running total, and its six tests pass. Eight hazards hide in four families (money, validation, errors, accounting), and nothing fails until you try the orders and refunds the tests never use.
On this page
The codebase
The ledger records orders and refunds in integer cents: it prices each line, applies the order's discount, keeps a running total, and applies refunds against an order's balance.
The six visible tests use clean orders only, so they pass on the shipped code. Verification after Submit adds the hidden tests, one file per family.
The assistant explains the code and Rust on request and makes the changes you direct, but it does not audit the crate for you and volunteers no defect.
Where things live
src/lib.rsThe public types, the Ledger and its errors. The signatures are fixed.src/money.rsPrice text into cents, a line's amount and the order's discount.src/validate.rsThe limits on a line, an order and a refund.src/ledger.rsStores orders and refund ids, applies refunds, keeps the running total.src/candidate_tests.rsYour tests. Test runs them after the visible tests in tests/, which are read-only.Rules that shape every fix
- Fix the crate in place: no rewrite, no new crate, the public API and the supplied tests unchanged.
- Rust standard library only.
- A rejected order or refund changes nothing, and no input panics.
How to work the review
- Read each module against the task's rules and ask what it does with the orders and refunds the tests never use: a discount that rounds, a huge line, a zero or negative value, a malformed price, a refund above the balance, a repeated id.
- Say what breaks and which rule it breaks before you ask for a fix.
- Ask for your own tests first, naming each case with the result the rules expect, and press Test to watch them fail.
- Name the failing test, then say how each fix should work and point to the rule it enforces.
- Press Test again after the last change, then Submit.
Review the generated ledger
What the ticket asks
An assistant generated an order ledger from the task's rules and wrote six tests for it, and they pass. Review the crate before it ships: fix, in src/, whatever does not meet the rules or would misbehave on real orders.
Keep the public API and the supplied tests as they are. The assistant explains code and makes the changes you direct, but it will not find the defects for you.
Where to look
src/money.rsPrice text into cents, a line's amount and the discount: check them against the rounding and exactness rules.src/validate.rsOnly the upper limits are checked, and the discount not at all.src/ledger.rsWhen the running total moves, what a refund is compared with, and whether a refund id is remembered.src/lib.rsThe errors each rule names, and the signatures you must keep.What the rules pin down
Where the generated crate goes wrong
- The discount is rounded per line in floating-point dollars, and a line's amount is multiplied in 32 bits with unchecked sums, so totals drift by a cent and large orders overflow.
- Only the upper limits are checked and the discount is never checked, and a refund is compared with the order's total instead of its balance.
- The price parse unwraps a floating-point parse, so a comma or a word panics and an exponent is accepted, and a refund changes the running total before its order is found.
- An order changes the running total before it is validated and adds its subtotal, not its total, and refund ids are never stored, so the same refund applies twice.
The fix
- Round the discount once per order on the integer subtotal, half-up, and keep every amount in 64-bit integers with checked arithmetic.
- Parse price text by hand into cents and reject anything outside the format, without a panic.
- Check both limits on every value and compare a refund with the balance.
- Change the running total only once an order or a refund is accepted, add the total after the discount, and remember applied refund ids.
src/money.rsThe discount is rounded once per order, half-up, in integer cents, without leaving 64 bits.-pub fn discount_cents(line_amounts: &[i64], discount_bps: u32) -> i64 {- let rate = f64::from(discount_bps) / 10_000.0;- line_amounts- .iter()- .map(|¢s| {- let line_discount = cents as f64 / 100.0 * rate;- (line_discount * 100.0).round() as i64- })- .sum()+pub fn discount_cents(subtotal_cents: i64, discount_bps: u32) -> Result<i64, LedgerError> {+ let bps = i64::from(discount_bps);+ let (blocks, remainder) = (subtotal_cents / 10_000, subtotal_cents % 10_000);+ blocks+ .checked_mul(bps)+ .and_then(|whole| whole.checked_add((remainder * bps + 5_000) / 10_000))+ .ok_or(LedgerError::Overflow) }src/money.rsPrice text is parsed digit by digit into cents; anything else is an invalid price.- let value: f64 = text.trim().parse().unwrap();- let cents = (value * 100.0).round();+ let (whole, fraction) = unsigned.split_once('.').unwrap_or((unsigned, "00"));+ let digits = |part: &str| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit());+ if !digits(whole) || fraction.len() != 2 || !digits(fraction) {+ return Err(malformed());+ }+ let mut cents: i64 = 0;+ for byte in whole.bytes().chain(fraction.bytes()) {+ cents = cents+ .checked_mul(10)+ .and_then(|cents| cents.checked_add(i64::from(byte - b'0')))+ .ok_or(LedgerError::Overflow)?;+ }src/ledger.rsAn order is checked first and adds its total after the discount only once it is accepted.- let subtotal_cents: i64 = line_amounts.iter().sum();- self.total_cents += subtotal_cents;-+ if self.orders.contains_key(&order.id) {+ return Err(LedgerError::DuplicateOrder(order.id));+ } validate::order(&order)?;+ let discount_cents = money::discount_cents(subtotal_cents, order.discount_bps)?;+ let total_cents = subtotal_cents - discount_cents;+ let ledger_total = self.total_cents.checked_add(total_cents).ok_or(LedgerError::Overflow)?;+ self.orders.insert(order.id, summary.clone());+ self.total_cents = ledger_total;src/ledger.rsA refund is checked against the balance and its id first; the total moves only when it is accepted.+ if self.refund_ids.contains(&refund.id) {+ return Err(LedgerError::DuplicateRefund(refund.id));+ } validate::refund_amount(refund.amount_cents)?;- self.total_cents -= refund.amount_cents;- if refund.amount_cents > summary.total_cents {+ let available_cents = summary.total_cents - summary.refunded_cents;+ if refund.amount_cents > available_cents { summary.refunded_cents += refund.amount_cents;+ self.total_cents -= refund.amount_cents;+ self.refund_ids.insert(refund.id);Tests that prove it
- Three lines of 1.00 at 3,333 basis points, and the largest exact line.
- A price with a comma, an exponent and a sign, through the ledger and through the price parse.
- A zero and a negative quantity, a negative price, a discount over the limit, and a refund above the balance.
- A rejected order and an unknown refund leaving the total alone, a discounted order adding its total, and the same refund id twice.
The conversation
Only your side is shown: the assistant reads, writes and runs between your messages. Each chip is a rubric item the step earns.
DirectionPrompt qualityVerificationCorrectnessCode quality
- Press Test. Your own tests run after the six visible ones, and the first of them fails on the shipped code.
- Verification: Checks own, 3 points
- Press Test. The discount-limit test still fails: validation is the next request.
- Press Test. One test left: a price with a comma must be rejected with its line's SKU.
- Press Test. All 22 tests pass after the last change, your 16 included.
- Verification: Run after last change, 5 points
- Submit. The stage closes. Scope, the hidden tests and code quality are judged on the final code.
- Direction: Scope, 5 points
- Correctness: Hidden tests, 20 points
- Code quality: Code quality, 10 points
Where the stage lands
Measured when this conversation was played on the platform. A+ needs 92, S needs 95.