Skip to content

fix(ladder): convert amend ceiling amounts by the amended tranche's basis - #969

Merged
cristim merged 1 commit into
mainfrom
fix/ladder-amend-ceiling-basis
Oct 16, 2026
Merged

cristim merged 1 commit into
mainfrom
fix/ladder-amend-ceiling-basis

Conversation

@cristim

@cristim cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member

Summary

The ladder amend ceiling now converts the amended tranche's old and new amounts into on-demand-equivalent (OD) dollars using that tranche's own amount_basis and commitment_rate, and compares against the OD ceiling exactly in big.Rat.

Why

The ceiling is LEAST(target, baseline*coverage) - existing, which is in OD. Once tranches are stored in commitment dollars (after the go bump, cloud-commitments-go#349), comparing a raw commitment amount against it would let an amend raise exceed target coverage by 1/rate. The independent plan review found this. Fixing it before any commitment writer exists keeps main safe.

Changes

Verification

  • Fail-before: on origin/main, raising a rate-0.5 commitment row beyond the OD ceiling is allowed ("expected error, got nil"). This PR refuses it.
  • Further tests:
    • the raise is allowed when its OD is within the ceiling;
    • NULL-basis amend behaves as today;
    • checkLadderRunTotal on a single-basis run.
  • Mutation: dropping the old/new conversion is killed by TestLadderAmendCeiling_RaiseOnCommitmentRowNetsInOnDemand.
  • go build ./... and go vet -tags integration ./... are clean.
  • -run 'LadderAmend|LadderNetting' integration tests pass against a native local PostgreSQL 17 on loopback, using the same overlay as fix(ladder): net in-flight tranches per row basis in on-demand-equivalent dollars #968.
  • Local golangci-lint could not run (toolchain mismatch), so CI lint is authoritative.

Stack: base is #968 (fix/ladder-basis-aware-netting). This PR retargets to main after #968 merges. Refs #799, refs LeanerCloud/cloud-commitments-go#349.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

This review includes 4 billable files and costs up to $1.00.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 55 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 64 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: LeanerCloud/cloud-commitments-platform/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 5a7406ba-0966-4364-b788-91aad35c1ea7
📥 Commits

Reviewing files that changed from the base of the PR and between f064294 and 2952deb.

📒 Files selected for processing: 4
  • internal/config/store_postgres_ladder_amend.go
  • internal/config/store_postgres_ladder_amend_ceiling.go
  • internal/config/store_postgres_ladder_basis_integration_test.go
  • known_issues/05_config_store_postgres.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Automated review of 2c390a1: no blocking findings and CI is green. Ready to merge.
The amend ceiling now converts the amended row's old and new amounts by that row's own basis and rate, using the existing exact-rational helpers. I found no correctness, security or scope problems, and CI is green. The three new integration tests cover commitment-basis raises, NULL-basis rows and run totals.

  • (optional) internal/config/store_postgres_ladder_amend_ceiling.go:64: The ceiling is still rounded to micro-dollars through float64 (toLadderMicros) while the committed total is now exact. This is harmless, since the ceiling is already truncated to 6 decimals in SQL, but reading it as a numeric string would be cleaner.
  • (optional) known_issues/05_config_store_postgres.md:225: The new known-issue entry is unrelated to the netting fix, though it is a reasonable record of the NULL-basis assumption. Check that it references the actual tracking issue number.

Posted by a read-only review sweep. It stays silent on this commit and re-reviews only after a new push.

@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Independent review verdict (ladder stack #968 → #969 → #972): CLEAN

Reviewer: fresh Claude agent (Sonnet, high effort), not the author, cold brief, covering each PR's own diff and the whole stack against main. The PR bodies were treated as claims. Coordinator: claude-cc-reviewer.

Blocking findings: none.

Non-blocking notes (follow-ups before the go bump):

What was attacked and how (local macOS):

Gate: labels are set on each PR (they reference #799, with no closing issue). CI must be green at each head. Merge order is #968, then #969 rebased onto main, then #972 rebased onto main. Each rebase needs an empty git range-diff of the PR's own commits to keep this verdict.

@cristim
cristim force-pushed the fix/ladder-basis-aware-netting branch from 201b877 to f4a286a Compare October 16, 2026 12:08
@cristim
cristim force-pushed the fix/ladder-amend-ceiling-basis branch from 2c390a1 to 6cc1b90 Compare October 16, 2026 12:08
@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Independent review at 2c390a1

Verdict: no actionable findings in the code of this PR (it inherits the lint blocker and test-strength gaps from #968, reviewed at 201b877, which must land first).

Observations

  1. Info: the raise test $1::numeric > $2::numeric is native, which is correct because old and new are the same row (same basis and rate, monotonic). Equal amounts pass without a ceiling check, as before.
  2. Info: checkLadderRunTotal stays native; correct for a single-basis run, which is what writers produce. Tested on a commitment-basis run. A mixed-basis run would need a conversion; no writer produces one.
  3. Info: stale comment names settledLadderCommit in store_postgres_ladder_lock.go (ladderSettleAnchor doc) and store_postgres_ladder_settled_integration_test.go:24; the function is now settledLadderRows.

Attacked

  • Amend NULL handling: assumeOnDemandIfUnknown applies only when both basis and rate are NULL; a rate without basis stays fail-loud (unit test). Only the amend path passes assumeOD=true; netting passes false. Mutant that disables the assumption is killed; mutant that applies it in netting is killed.
  • Conversion: old and new amounts both convert by the amended row's own basis and rate; mutants leaving the old or the new amount unconverted are both killed, as is flipping the old-amount sign.
  • Ceiling comparison: exact big.Rat against the ceiling in micros; mutant > to >= killed (a test amends to exactly the ceiling and passes). Raise detection > to >= killed. Dropping settled rows from the amend sum killed.
  • Overflow guard checks committed, old OD and new OD individually against 1e12 before subtraction; rates are bounded to (0,1] so no divide-by-zero.
  • Transaction and locking: queries run on the same tx as the amend, under the config lock; the in-flight read precedes the settled read as in planning; cancelOverdueLadderTranchesTx still runs first with the same now.
  • Legacy behaviour: for six-digit amounts the exact Rat comparison equals the old micros comparison.
  • Scope: 4 files, +85/-30, within the 400-line cap. gocyclo max 10.

Mutation probes

Killed: NULL assumption disabled; netting assumes NULL; old amount unconverted; new amount unconverted; ceiling >=; raise >=; drop settled in amend; old-OD sign. No survivor in this PR's own code. (Survivors from #968 are listed there.)

Local verification (native local PostgreSQL 17 integration with synthetic data)

At this SHA in a fresh detached scratch worktree: GOWORK=off go build ./... OK; go vet -tags integration ./... OK; unit tests for config, server, mocks, analytics pass; -tags integration -run Ladder for internal/config, internal/purchase, internal/server pass on my own native PostgreSQL 17 cluster with a go test -overlay helper stub. Not real-account verification. CI lint is authoritative.

@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Rebase + delta: verdict carried to the new heads

The stack was rebased onto main (picks up #960) by claude-cc-impl.

Range-diffs against the reviewed heads:

#968: 1: 201b877d = 1: f838030c fix(ladder): net in-flight tranches per row basis ...
      -: -------- > 2: f4a286af style(ladder): keep the unparam suppression on the builder func line
#969: 1: 2c390a18 = 1: 6cc1b901
#972: 1: c0ae02f3 = 1: 57c3dd56

Delta review of f4a286a (same independent reviewer): CLEAN.

  • The commit is +2/-2 in internal/config/store_postgres_ladder_lock_integration_test.go. It moves //nolint:unparam // matches LadderPlanBuilder onto the func-literal line, where unparam reports. That is justified, because the closure must keep the LadderPlanBuilder signature.
  • git diff f838030c f4a286af shows nothing else changed.
  • golangci-lint v2.10.1 (the CI pin) with --build-tags integration ./internal/config/... reports 0 issues.

The CLEAN stack verdict above therefore covers #968 f4a286a, #969 6cc1b90 and #972 57c3dd5. Merge order is #968, then #969, then #972, each when its CI is green. (claude-cc-reviewer)

@cristim
cristim changed the base branch from fix/ladder-basis-aware-netting to main October 16, 2026 12:39
…wn basis

The config-wide amend ceiling is in on-demand-equivalent dollars, but the
amended tranche's old and new amounts were subtracted and added as stored
numbers. A raise on a commitment-basis row (rate 0.5) could therefore pass
while its on-demand equivalent exceeded the ceiling. Old and new amounts now
convert by that row's basis and rate, and the comparison is exact in
big.Rat. A NULL basis (rows from before 000121, left unknown by #886) is
assumed on-demand-equivalent on this path, as before; netting still fails
loud. Records the open risk that a future executor could claim a legacy
on-demand row as commitment dollars.

Refs #799
Refs LeanerCloud/cloud-commitments-go#349

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cristim
cristim force-pushed the fix/ladder-amend-ceiling-basis branch from 6cc1b90 to 2952deb Compare October 16, 2026 12:39
@cristim

cristim commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

Retargeted to main after #968 merged (f064294) and rebased with git rebase --onto origin/main f4a286af. git range-diff: 1: 6cc1b901 = 1: 2952deb0, so the commit is identical. go build ./... and go vet ./internal/... pass on the rebased tree. The CLEAN verdict carries over to 2952deb. Merge waits on CI at this head. (claude-cc-reviewer)

@cristim
cristim merged commit 06c5109 into main Oct 16, 2026
25 checks passed
@cristim
cristim deleted the fix/ladder-amend-ceiling-basis branch October 16, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/l Weeks impact/few Limited audience priority/p2 Backlog-worthy severity/medium Moderate harm triaged Item has been triaged type/bug Defect urgency/eventually No deadline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant