Skip to content

refactor(contract): extract daily-reset check into shared helper with boundary tests#184

Open
leocagli wants to merge 1 commit into
Stellar-Ecosystem:mainfrom
leocagli:refactor/agents-daily-reset-helper
Open

refactor(contract): extract daily-reset check into shared helper with boundary tests#184
leocagli wants to merge 1 commit into
Stellar-Ecosystem:mainfrom
leocagli:refactor/agents-daily-reset-helper

Conversation

@leocagli

Copy link
Copy Markdown
Contributor

Fixes #50

Problem

The daily-spend reset condition (now >= last_reset_ledger + DAY_LEDGERS) was inlined independently in four places — get_policy, check_spending_allowed, record_payment, and update_policy — with no shared abstraction and zero test coverage. Any future change to the reset threshold or semantics (e.g. moving to wall-clock time, adding a grace period) would require updating all four sites with no safety net.

Solution

Extracted helper:

fn is_new_day(now: u64, last_reset_ledger: u64) -> bool {
    now >= last_reset_ledger + DAY_LEDGERS
}

Replaced all four inline occurrences with is_new_day(now, policy.last_reset_ledger) / is_new_day(now, p.last_reset_ledger).

Added #[cfg(test)] module with 5 boundary tests:

Test now Expected
One ledger before threshold last_reset + DAY_LEDGERS - 1 false (no reset)
Exactly at threshold last_reset + DAY_LEDGERS true (reset)
One ledger past threshold last_reset + DAY_LEDGERS + 1 true (reset)
Same ledger as reset last_reset false (no reset)
Multiple days past threshold last_reset + DAY_LEDGERS * 3 true (reset)

🤖 Generated with Claude Code

…d boundary tests

Closes Stellar-Ecosystem#50

get_policy, check_spending_allowed, record_payment, and update_policy each
inlined the same 'now >= last_reset_ledger + DAY_LEDGERS' check independently
with no shared abstraction and no test coverage.

Changes:
- Added is_new_day(now: u64, last_reset_ledger: u64) -> bool free function
  above the contract impl block
- Replaced all four inline occurrences with is_new_day(now, ...)
- Added #[cfg(test)] module with 5 boundary tests:
  - one ledger before threshold (no reset)
  - exactly at threshold (reset)
  - one ledger past threshold (reset)
  - same ledger as reset (no reset)
  - multiple days past threshold (reset)
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@leocagli, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 1 minute and 17 seconds. Learn how PR review limits work.

To continue reviewing without waiting, enable usage-based billing in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 44c63a6a-f66f-4f76-adf5-eede8d02988b

📥 Commits

Reviewing files that changed from the base of the PR and between 6fb6da9 and 763eedc.

📒 Files selected for processing (1)
  • contract/agents/src/lib.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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.

Add unit tests for the agents contract's daily spend reset logic

1 participant