A platform for community discussion. Free, open, simple.
DEV: Apply credit system on daily schedule instead of monthly (#36066)
Keegan George6d9a176bfd7f
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rbindex 5f6f6b40a08..1b2aa402dc7 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb@@ -174,7 +174,7 @@ module DiscourseAiallocation = params[:ai_llm][:llm_credit_allocation]{- monthly_credits: allocation[:monthly_credits].to_i,+ daily_credits: allocation[:daily_credits].to_i,soft_limit_percentage: allocation[:soft_limit_percentage].to_i,}enddiff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rbindex f7c87ccfc82..3809be0b79f 100644--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb@@ -12,13 +12,17 @@ class LlmCreditAllocation < ActiveRecord::Baseendend+ DAILY_USAGE_RETENTION_DAYS = 90+belongs_to :llm_model- # TODO: Remove once 20251105174003_remove_old_credit_columns_from_llm_credit_allocations has been promoted to pre-deploy- self.ignored_columns = %w[monthly_used last_reset_at]+ # TODO: Remove once both column-dropping migrations have been promoted to pre-deploy:+ # - 20251105174003 (drops monthly_used, last_reset_at)+ # - 20251117000003 (drops monthly_credits, monthly_usage)+ self.ignored_columns = %w[monthly_used last_reset_at monthly_credits monthly_usage]validates :llm_model_id, presence: true, uniqueness: true- validates :monthly_credits, presence: true, numericality: { only_integer: true, greater_than: 0 }+ validates :daily_credits, presence: true, numericality: { only_integer: true, greater_than: 0 }validates :soft_limit_percentage,presence: true,numericality: {@@ -27,31 +31,31 @@ class LlmCreditAllocation < ActiveRecord::Baseless_than_or_equal_to: 100,}- def current_month_key- Time.current.strftime("%Y-%m")+ def current_day_key+ Time.current.utc.strftime("%Y-%m-%d")end- def monthly_used- monthly_usage[current_month_key].to_i+ def daily_used+ daily_usage[current_day_key].to_iend- def monthly_used=(value)- month_key = current_month_key- self.monthly_usage = (monthly_usage || {}).merge(month_key => value.to_i)+ def daily_used=(value)+ day_key = current_day_key+ self.daily_usage = (daily_usage || {}).merge(day_key => value.to_i)enddef credits_remaining- [0, monthly_credits - monthly_used].max+ [0, daily_credits - daily_used].maxenddef percentage_used- return 0 if monthly_credits.zero?- [(monthly_used.to_f / monthly_credits * 100).round(2), 100].min+ return 0 if daily_credits.zero?+ [(daily_used.to_f / daily_credits * 100).round(2), 100].minenddef percentage_remaining- return 100.0 if monthly_credits.zero?- [(credits_remaining.to_f / monthly_credits * 100).round(2), 0].max+ return 100.0 if daily_credits.zero?+ [(credits_remaining.to_f / daily_credits * 100).round(2), 0].maxenddef soft_limit_reached?@@ -63,7 +67,7 @@ class LlmCreditAllocation < ActiveRecord::Baseenddef hard_limit_reached?- monthly_used >= monthly_credits+ daily_used >= daily_creditsenddef hard_limit_remaining_reached?@@ -71,15 +75,15 @@ class LlmCreditAllocation < ActiveRecord::Baseenddef next_reset_at- Time.current.next_month.beginning_of_month+ Time.current.utc.tomorrow.beginning_of_dayenddef deduct_credits!(credits)with_lock doreload- month_key = current_month_key- self.monthly_usage = monthly_usage.merge(month_key => monthly_used + credits)- cleanup_old_months!+ day_key = current_day_key+ self.daily_usage = daily_usage.merge(day_key => daily_used + credits)+ cleanup_old_days!save!endend@@ -136,9 +140,9 @@ class LlmCreditAllocation < ActiveRecord::Baseprivate- def cleanup_old_months!- cutoff = 6.months.ago.beginning_of_month.strftime("%Y-%m")- self.monthly_usage = monthly_usage.select { |k, _| k >= cutoff }+ def cleanup_old_days!+ cutoff = DAILY_USAGE_RETENTION_DAYS.days.ago.beginning_of_day.utc.strftime("%Y-%m-%d")+ self.daily_usage = daily_usage.select { |k, _| k >= cutoff }enddef format_reset_time@@ -152,8 +156,8 @@ end# Table name: llm_credit_allocations## id :bigint not null, primary key-# monthly_credits :bigint not null-# monthly_usage :jsonb not null+# daily_credits :bigint default(0), not null+# daily_usage :jsonb not null# soft_limit_percentage :integer default(80), not null# created_at :datetime not null# updated_at :datetime not nulldiff --git a/plugins/discourse-ai/app/serializers/llm_credit_allocation_serializer.rb b/plugins/discourse-ai/app/serializers/llm_credit_allocation_serializer.rbindex 7d5db19f007..b99c12764c5 100644--- a/plugins/discourse-ai/app/serializers/llm_credit_allocation_serializer.rb+++ b/plugins/discourse-ai/app/serializers/llm_credit_allocation_serializer.rb@@ -2,8 +2,8 @@class LlmCreditAllocationSerializer < ApplicationSerializerattributes :id,- :monthly_credits,- :monthly_used,+ :daily_credits,+ :daily_used,:credits_remaining,:percentage_used,:percentage_remaining,diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjsindex f4ec1d6a519..7152b068abe 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-credit-bar.gjs@@ -9,7 +9,7 @@ import { i18n } from "discourse-i18n";* Component to display credit allocation remaining as a horizontal progress bar** @component AiCreditBar- * @param {Object} allocation - LlmCreditAllocation object with monthly_credits, credits_remaining, percentage_remaining, soft_limit_reached, hard_limit_reached, next_reset_at+ * @param {Object} allocation - LlmCreditAllocation object with daily_credits, credits_remaining, percentage_remaining, soft_limit_reached, hard_limit_reached, next_reset_at* @param {Boolean} showTooltip - Whether to show tooltip on hover (default: true)*/export default class AiCreditBar extends Component {@@ -27,7 +27,7 @@ export default class AiCreditBar extends Component {get barText() {return i18n("discourse_ai.llms.credit_allocation.credits_remaining", {remaining: number(this.args.allocation.credits_remaining),- total: number(this.args.allocation.monthly_credits),+ total: number(this.args.allocation.daily_credits),percentage: this.args.allocation.percentage_remaining,});}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex d298a8c6180..ba8d6faef37 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -659,7 +659,7 @@ en:max_usages_required: "Must be set if max tokens is not set"credit_allocation:credits_remaining: "%{remaining} of %{total} credits remaining (%{percentage}%)"- soft_limit_warning: "This model has %{percentage}% of AI credits remaining this month"+ soft_limit_warning: "This model has %{percentage}% of AI credits remaining today"hard_limit_warning: "You have run out of AI credits for this model. AI features using this model will be unavailable until %{reset_date}"next_reset: "Credits reset on %{time}"usage:diff --git a/plugins/discourse-ai/db/migrate/20251117000001_add_daily_credits_to_llm_credit_allocations.rb b/plugins/discourse-ai/db/migrate/20251117000001_add_daily_credits_to_llm_credit_allocations.rbnew file mode 100644index 00000000000..97bd695b79d--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20251117000001_add_daily_credits_to_llm_credit_allocations.rb@@ -0,0 +1,20 @@+# frozen_string_literal: true++class AddDailyCreditsToLlmCreditAllocations < ActiveRecord::Migration[8.0]+ def up+ return if column_exists?(:llm_credit_allocations, :daily_credits)++ add_column :llm_credit_allocations, :daily_credits, :bigint, null: false, default: 0++ execute <<~SQL+ UPDATE llm_credit_allocations+ SET daily_credits = GREATEST(COALESCE(monthly_credits / 30, 0), 0)+ SQL+ end++ def down+ if column_exists?(:llm_credit_allocations, :daily_credits)+ remove_column :llm_credit_allocations, :daily_credits+ end+ end+enddiff --git a/plugins/discourse-ai/db/migrate/20251117000002_add_daily_usage_to_llm_credit_allocations.rb b/plugins/discourse-ai/db/migrate/20251117000002_add_daily_usage_to_llm_credit_allocations.rbnew file mode 100644index 00000000000..7c443128b7d--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20251117000002_add_daily_usage_to_llm_credit_allocations.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++class AddDailyUsageToLlmCreditAllocations < ActiveRecord::Migration[8.0]+ def up+ return if column_exists?(:llm_credit_allocations, :daily_usage)++ add_column :llm_credit_allocations, :daily_usage, :jsonb, default: {}, null: false++ # Migrate existing monthly_usage data to daily_usage+ # For past months: assign to last day of that month+ # For current month: assign to current date (to avoid future dates)+ # This preserves historical totals while transitioning to daily tracking+ execute <<~SQL+ UPDATE llm_credit_allocations+ SET daily_usage = (+ SELECT jsonb_object_agg(+ CASE+ WHEN (month_date || '-01')::date >= date_trunc('month', CURRENT_TIMESTAMP)::date+ THEN to_char(CURRENT_DATE, 'YYYY-MM-DD')+ ELSE to_char((month_date || '-01')::date + interval '1 month' - interval '1 day', 'YYYY-MM-DD')+ END,+ month_usage::integer+ )+ FROM jsonb_each_text(monthly_usage) AS t(month_date, month_usage)+ WHERE month_usage::integer > 0+ )+ WHERE monthly_usage IS NOT NULL+ AND monthly_usage != '{}'::jsonb+ SQL+ end++ def down+ if column_exists?(:llm_credit_allocations, :daily_usage)+ remove_column :llm_credit_allocations, :daily_usage+ end+ end+enddiff --git a/plugins/discourse-ai/db/post_migrate/20251117000003_drop_monthly_credit_columns.rb b/plugins/discourse-ai/db/post_migrate/20251117000003_drop_monthly_credit_columns.rbnew file mode 100644index 00000000000..15ea74eb4eb--- /dev/null+++ b/plugins/discourse-ai/db/post_migrate/20251117000003_drop_monthly_credit_columns.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++class DropMonthlyCreditColumns < ActiveRecord::Migration[8.0]+ DROPPED_COLUMNS = { llm_credit_allocations: %i[monthly_credits monthly_usage] }++ def up+ DROPPED_COLUMNS.each { |table, columns| Migration::ColumnDropper.execute_drop(table, columns) }+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rbindex e7789fb060b..cdd6ea718cc 100644--- a/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb+++ b/plugins/discourse-ai/spec/fabricators/llm_credit_allocation_fabricator.rb@@ -2,16 +2,16 @@Fabricator(:llm_credit_allocation) dollm_model- monthly_credits 1_000_000- monthly_usage { {} }+ daily_credits 33_334+ daily_usage { {} }soft_limit_percentage 80- transient :monthly_used+ transient :daily_usedafter_build do |allocation, transients|- if transients[:monthly_used]- month_key = Time.current.strftime("%Y-%m")- allocation.monthly_usage = { month_key => transients[:monthly_used] }+ if transients[:daily_used]+ day_key = Time.current.utc.strftime("%Y-%m-%d")+ allocation.daily_usage = { day_key => transients[:daily_used] }endendenddiff --git a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rbindex 4c410a3c22b..13191e08297 100644--- a/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb+++ b/plugins/discourse-ai/spec/models/llm_credit_allocation_spec.rb@@ -6,26 +6,26 @@ RSpec.describe LlmCreditAllocation dodescribe "validations" doit "requires llm_model_id" do- allocation = LlmCreditAllocation.new(monthly_credits: 1000)+ allocation = LlmCreditAllocation.new(daily_credits: 1000)expect(allocation).not_to be_validexpect(allocation.errors[:llm_model_id]).to be_presentendit "requires unique llm_model_id" doFabricate(:llm_credit_allocation, llm_model: llm_model)- allocation = LlmCreditAllocation.new(llm_model: llm_model, monthly_credits: 1000)+ allocation = LlmCreditAllocation.new(llm_model: llm_model, daily_credits: 1000)expect(allocation).not_to be_validexpect(allocation.errors[:llm_model_id]).to be_presentend- it "requires monthly_credits to be positive" do- allocation = LlmCreditAllocation.new(llm_model: llm_model, monthly_credits: 0)+ it "requires daily_credits to be positive" do+ allocation = LlmCreditAllocation.new(llm_model: llm_model, daily_credits: 0)expect(allocation).not_to be_valid- expect(allocation.errors[:monthly_credits]).to be_present+ expect(allocation.errors[:daily_credits]).to be_presentendit "requires soft_limit_percentage between 0 and 100" do- allocation = LlmCreditAllocation.new(llm_model: llm_model, monthly_credits: 1000)+ allocation = LlmCreditAllocation.new(llm_model: llm_model, daily_credits: 1000)allocation.soft_limit_percentage = 101expect(allocation).not_to be_valid@@ -36,62 +36,62 @@ RSpec.describe LlmCreditAllocation doexpect(allocation).to be_validend- it "initializes with empty monthly_usage on create" do+ it "initializes with empty daily_usage on create" doallocation = Fabricate(:llm_credit_allocation, llm_model: llm_model)- expect(allocation.monthly_usage).to eq({})- expect(allocation.monthly_used).to eq(0)+ expect(allocation.daily_usage).to eq({})+ expect(allocation.daily_used).to eq(0)endenddescribe "#credits_remaining" doit "returns remaining credits" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 300)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 300)expect(allocation.credits_remaining).to eq(700)endit "returns 0 when credits are exhausted" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1200)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1200)expect(allocation.credits_remaining).to eq(0)endenddescribe "#percentage_used" doit "calculates percentage correctly" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 250)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 250)expect(allocation.percentage_used).to eq(25.0)endit "caps at 100%" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1500)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1500)expect(allocation.percentage_used).to eq(100.0)end- it "returns 0 when monthly_credits is 0" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 0)- allocation.monthly_credits = 0+ it "returns 0 when daily_credits is 0" do+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 0)+ allocation.daily_credits = 0expect(allocation.percentage_used).to eq(0)endenddescribe "#percentage_remaining" doit "calculates percentage correctly" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 250)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 250)expect(allocation.percentage_remaining).to eq(75.0)endit "floors at 0%" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1500)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1500)expect(allocation.percentage_remaining).to eq(0.0)end- it "returns 100.0 when monthly_credits is 0" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 0)- allocation.monthly_credits = 0+ it "returns 100.0 when daily_credits is 0" do+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 0)+ allocation.daily_credits = 0expect(allocation.percentage_remaining).to eq(100.0)endit "returns 100.0 when no credits used" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 0)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 0)expect(allocation.percentage_remaining).to eq(100.0)endend@@ -101,8 +101,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 800,+ daily_credits: 1000,+ daily_used: 800,soft_limit_percentage: 80,)expect(allocation.soft_limit_remaining_reached?).to be true@@ -112,8 +112,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 900,+ daily_credits: 1000,+ daily_used: 900,soft_limit_percentage: 80,)expect(allocation.soft_limit_remaining_reached?).to be true@@ -123,8 +123,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 700,+ daily_credits: 1000,+ daily_used: 700,soft_limit_percentage: 80,)expect(allocation.soft_limit_remaining_reached?).to be false@@ -133,34 +133,34 @@ RSpec.describe LlmCreditAllocation dodescribe "#hard_limit_remaining_reached?" doit "returns true when credits_remaining is 0" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1000)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1000)expect(allocation.hard_limit_remaining_reached?).to be trueendit "returns true when credits_remaining is negative" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1200)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1200)expect(allocation.hard_limit_remaining_reached?).to be trueendit "returns false when credits_remaining is positive" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 999)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 999)expect(allocation.hard_limit_remaining_reached?).to be falseendenddescribe "#credits_available?" doit "returns true when credits are available" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 500)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 500)expect(allocation.credits_available?).to be trueendit "returns false when hard limit is reached" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1000)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1000)expect(allocation.credits_available?).to be falseendit "returns false when hard limit is exceeded" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1100)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1100)expect(allocation.credits_available?).to be falseendend@@ -170,8 +170,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 800,+ daily_credits: 1000,+ daily_used: 800,soft_limit_percentage: 80,)expect(allocation.soft_limit_reached?).to be true@@ -181,8 +181,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 900,+ daily_credits: 1000,+ daily_used: 900,soft_limit_percentage: 80,)expect(allocation.soft_limit_reached?).to be true@@ -192,8 +192,8 @@ RSpec.describe LlmCreditAllocation doallocation =Fabricate(:llm_credit_allocation,- monthly_credits: 1000,- monthly_used: 700,+ daily_credits: 1000,+ daily_used: 700,soft_limit_percentage: 80,)expect(allocation.soft_limit_reached?).to be false@@ -201,107 +201,107 @@ RSpec.describe LlmCreditAllocation doenddescribe "#hard_limit_reached?" do- it "returns true when monthly_used equals monthly_credits" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1000)+ it "returns true when daily_used equals daily_credits" do+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1000)expect(allocation.hard_limit_reached?).to be trueend- it "returns true when monthly_used exceeds monthly_credits" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1200)+ it "returns true when daily_used exceeds daily_credits" do+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1200)expect(allocation.hard_limit_reached?).to be trueendit "returns false when below limit" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 999)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 999)expect(allocation.hard_limit_reached?).to be falseendenddescribe "#next_reset_at" do- it "returns first of next month" do- freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ it "returns tomorrow at UTC midnight" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))allocation = Fabricate(:llm_credit_allocation)- expect(allocation.next_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-10-16 00:00:00 UTC"))end- it "returns first of next month when on last day of month" do- freeze_time(Time.zone.parse("2025-10-31 23:59:00"))+ it "returns tomorrow at UTC midnight when near end of day" do+ freeze_time(Time.zone.parse("2025-10-15 23:59:00 UTC"))allocation = Fabricate(:llm_credit_allocation)- expect(allocation.next_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-10-16 00:00:00 UTC"))end- it "returns first of next month when on first of month" do- freeze_time(Time.zone.parse("2025-11-01 00:00:00"))+ it "returns tomorrow at UTC midnight when at start of day" do+ freeze_time(Time.zone.parse("2025-10-15 00:00:00 UTC"))allocation = Fabricate(:llm_credit_allocation)- expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-12-01 00:00:00"))+ expect(allocation.next_reset_at).to eq_time(Time.zone.parse("2025-10-16 00:00:00 UTC"))endend- describe "month transitions" do- it "automatically resets usage to 0 when month changes" do- freeze_time(Time.zone.parse("2025-10-15 14:30:00"))- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 800)- expect(allocation.monthly_used).to eq(800)+ describe "day transitions" do+ it "automatically resets usage to 0 when day changes" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 800)+ expect(allocation.daily_used).to eq(800)- freeze_time(Time.zone.parse("2025-11-05 10:00:00"))+ freeze_time(Time.zone.parse("2025-10-16 10:00:00 UTC"))allocation.reload- expect(allocation.monthly_used).to eq(0)+ expect(allocation.daily_used).to eq(0)end- it "preserves previous month's data" do- freeze_time(Time.zone.parse("2025-10-15 14:30:00"))- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 800)+ it "preserves previous day's data" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 800)- freeze_time(Time.zone.parse("2025-11-05 10:00:00"))+ freeze_time(Time.zone.parse("2025-10-16 10:00:00 UTC"))allocation.deduct_credits!(200)allocation.reload- expect(allocation.monthly_used).to eq(200)- expect(allocation.monthly_usage["2025-10"]).to eq(800)+ expect(allocation.daily_used).to eq(200)+ expect(allocation.daily_usage["2025-10-15"]).to eq(800)end- it "cleans up old months beyond 6 months" do+ it "cleans up old days beyond 90 days" doallocation = Fabricate(:llm_credit_allocation)- 8.times do |i|- month_key = (i + 1).months.ago.strftime("%Y-%m")- allocation.monthly_usage[month_key] = 1000 * (i + 1)+ 100.times do |i|+ day_key = (i + 1).days.ago.beginning_of_day.utc.strftime("%Y-%m-%d")+ allocation.daily_usage[day_key] = 100 * (i + 1)endallocation.save!allocation.deduct_credits!(100)allocation.reload- expect(allocation.monthly_usage.keys.size).to be <= 7+ expect(allocation.daily_usage.keys.size).to be <= 91endenddescribe "#deduct_credits!" do- it "increments monthly_used" do- allocation = Fabricate(:llm_credit_allocation, monthly_used: 100)+ it "increments daily_used" do+ allocation = Fabricate(:llm_credit_allocation, daily_used: 100)allocation.deduct_credits!(50)allocation.reload- expect(allocation.monthly_used).to eq(150)+ expect(allocation.daily_used).to eq(150)endenddescribe "#check_credits!" doit "raises error when hard limit reached" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1000)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1000)expect { allocation.check_credits! }.to raise_error(LlmCreditAllocation::CreditLimitExceeded)endit "does not raise error when below limit" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 500)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 500)expect { allocation.check_credits! }.not_to raise_errorendit "attaches allocation to raised exception" do- allocation = Fabricate(:llm_credit_allocation, monthly_credits: 1000, monthly_used: 1000)+ allocation = Fabricate(:llm_credit_allocation, daily_credits: 1000, daily_used: 1000)beginallocation.check_credits!@@ -329,22 +329,12 @@ RSpec.describe LlmCreditAllocation doendit "returns true when credits are available" do- Fabricate(- :llm_credit_allocation,- llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 500,- )+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 500)expect(LlmCreditAllocation.credits_available?(llm_model)).to be trueendit "returns false when hard limit reached" do- Fabricate(- :llm_credit_allocation,- llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,- )+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)expect(LlmCreditAllocation.credits_available?(llm_model)).to be falseend@@ -354,8 +344,8 @@ RSpec.describe LlmCreditAllocation doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,+ daily_credits: 1000,+ daily_used: 1000,)freeze_time(Time.zone.parse("2025-11-05 10:00:00"))@@ -363,7 +353,7 @@ RSpec.describe LlmCreditAllocation doallocation.reloadexpect(result).to be true- expect(allocation.monthly_used).to eq(0)+ expect(allocation.daily_used).to eq(0)endenddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rbindex bbb37cffdcb..df5f9a302e9 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb@@ -18,8 +18,8 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 850,+ daily_credits: 1000,+ daily_used: 850,soft_limit_percentage: 80,)@@ -30,8 +30,8 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,+ daily_credits: 1000,+ daily_used: 1000,soft_limit_percentage: 80,)@@ -42,31 +42,31 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1200,+ daily_credits: 1000,+ daily_used: 1200,soft_limit_percentage: 80,)expect(check).to have_a_problem.with_priority("high").with_target(llm_model.id)end- it "does not report problem when previous month exceeded limit but current month is new" do- freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ it "does not report problem when previous day exceeded limit but current day is new" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))allocation =Fabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,+ daily_credits: 1000,+ daily_used: 1000,soft_limit_percentage: 80,)- freeze_time(Time.zone.parse("2025-11-05 10:00:00"))+ freeze_time(Time.zone.parse("2025-10-16 10:00:00 UTC"))expect(check).to be_chill_about_itallocation.reload- expect(allocation.monthly_used).to eq(0)+ expect(allocation.daily_used).to eq(0)endit "skips non-seeded models" do@@ -74,8 +74,8 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doFabricate(:llm_credit_allocation,llm_model: non_seeded,- monthly_credits: 1000,- monthly_used: 1000,+ daily_credits: 1000,+ daily_used: 1000,)expect(check).to be_chill_about_it@@ -83,12 +83,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doit "returns no problems when discourse_ai is disabled" doSiteSetting.discourse_ai_enabled = false- Fabricate(- :llm_credit_allocation,- llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,- )+ Fabricate(:llm_credit_allocation, llm_model: llm_model, daily_credits: 1000, daily_used: 1000)expect(check).to be_chill_about_itenddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rbindex 430bde4169b..3948463f766 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb@@ -18,8 +18,8 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 700,+ daily_credits: 1000,+ daily_used: 700,soft_limit_percentage: 80,)@@ -30,8 +30,8 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 850,+ daily_credits: 1000,+ daily_used: 850,soft_limit_percentage: 80,)@@ -42,31 +42,31 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 1000,+ daily_credits: 1000,+ daily_used: 1000,soft_limit_percentage: 80,)expect(check).to be_chill_about_itend- it "does not report problem when previous month exceeded limit but current month is new" do- freeze_time(Time.zone.parse("2025-10-15 14:30:00"))+ it "does not report problem when previous day exceeded limit but current day is new" do+ freeze_time(Time.zone.parse("2025-10-15 14:30:00 UTC"))allocation =Fabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 850,+ daily_credits: 1000,+ daily_used: 850,soft_limit_percentage: 80,)- freeze_time(Time.zone.parse("2025-11-05 10:00:00"))+ freeze_time(Time.zone.parse("2025-10-16 10:00:00 UTC"))expect(check).to be_chill_about_itallocation.reload- expect(allocation.monthly_used).to eq(0)+ expect(allocation.daily_used).to eq(0)endit "skips non-seeded models" do@@ -74,8 +74,8 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doFabricate(:llm_credit_allocation,llm_model: non_seeded,- monthly_credits: 1000,- monthly_used: 850,+ daily_credits: 1000,+ daily_used: 850,soft_limit_percentage: 80,)@@ -87,8 +87,8 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doFabricate(:llm_credit_allocation,llm_model: llm_model,- monthly_credits: 1000,- monthly_used: 850,+ daily_credits: 1000,+ daily_used: 850,soft_limit_percentage: 80,)