A platform for community discussion. Free, open, simple.
DEV: AI credits should reset at start of month (#35587)
Keegan George5ef7fcc21d2b
diff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rbindex 779e3557747..2d1d1fa887e 100644--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb@@ -65,7 +65,7 @@ class LlmCreditAllocation < ActiveRecord::Basedef next_reset_atreturn nil if last_reset_at.nil?- last_reset_at + 1.month+ (last_reset_at + 1.month).beginning_of_monthenddef reset_if_needed!@@ -73,7 +73,7 @@ class LlmCreditAllocation < ActiveRecord::Basereloadreturn unless should_reset?- now = Time.current+ now = Time.current.beginning_of_monthupdate!(monthly_used: 0, last_reset_at: now)endend@@ -145,7 +145,7 @@ class LlmCreditAllocation < ActiveRecord::Baseprivatedef set_last_reset_at- self.last_reset_at ||= Time.current+ self.last_reset_at ||= Time.current.beginning_of_monthenddef format_reset_timediff --git a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rbindex 8efb17fbe01..e73b6e814e4 100644--- a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb+++ b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb@@ -4,6 +4,6 @@ Fabricator(:llm_credit_allocation) dollm_modelmonthly_credits 1_000_000monthly_used 0- last_reset_at { Time.current }+ last_reset_at { Time.current.beginning_of_month }soft_limit_percentage 80enddiff --git a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rbindex 2ba3901138b..c92eb320154 100644--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb@@ -42,6 +42,12 @@ RSpec.describe LlmCreditAllocation doallocation.save!expect(allocation.last_reset_at).to be_presentend++ it "sets last_reset_at to beginning of month on create" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ allocation = Fabricate(:llm_credit_allocation, llm_model: llm_model)+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))+ endenddescribe "#credits_remaining" do@@ -219,10 +225,24 @@ RSpec.describe LlmCreditAllocation doenddescribe "#next_reset_at" do- it "returns one month after last_reset_at" do+ it "returns beginning of next month after last_reset_at" dofreeze_timeallocation = Fabricate(:llm_credit_allocation, last_reset_at: Time.current)- expect(allocation.next_reset_at).to eq_time(1.month.from_now)+ expect(allocation.next_reset_at).to eq_time((1.month.from_now).beginning_of_month)+ end++ it "returns first of next month when created mid-month" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ allocation = Fabricate(:llm_credit_allocation)+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))+ expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-11-01 00:00:00"))+ end++ it "returns first of next month when created on first of month" do+ freeze_time(Time.zone.parse("2025-11-01 00:00:00"))+ allocation = Fabricate(:llm_credit_allocation)+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-11-01 00:00:00"))+ expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-12-01 00:00:00"))endend@@ -253,7 +273,7 @@ RSpec.describe LlmCreditAllocation doallocation.reloadexpect(allocation.monthly_used).to eq(0)- expect(allocation.last_reset_at).to be_within(1.second).of(Time.current)+ expect(allocation.last_reset_at).to eq_time(Time.current.beginning_of_month)endend@@ -273,6 +293,23 @@ RSpec.describe LlmCreditAllocation doexpect(allocation.monthly_used).to eq(800)expect(allocation.last_reset_at).to be_within(1.second).of(original_time)end++ it "resets to beginning of current month when triggered mid-month" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ allocation =+ Fabricate(+ :llm_credit_allocation,+ monthly_credits: 1000,+ monthly_used: 800,+ last_reset_at: Time.zone.parse("2025-09-01 00:00:00"),+ )++ allocation.reset_if_needed!++ allocation.reload+ expect(allocation.monthly_used).to eq(0)+ expect(allocation.last_reset_at).to eq_time(Time.zone.parse("2025-10-01 00:00:00"))+ endenddescribe "#deduct_credits!" do