[BUG] Usage counted again on each poll of a batch whose chat a default scope hides
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 半日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
Start with Batch#chats, deleted?, and deliver_once in the Active Record batch implementation, then run bundle exec rspec spec/ruby_llm/active_record/acts_as_batch_spec.rb. The issue identifies three possible semantics and explicitly waits for maintainer guidance on whether hidden chats should receive answers immediately or defer delivery, so confirm that decision before changing code. Done means one provider result creates exactly one RubyLLM::ActiveRecord::Usage row across repeated polls and later visibility changes, with the existing and new batch specs passing.
索引モデルが issue の本文から書いたものです。
説明
Basic checks
- I searched existing issues - this hasn't been reported
- I can reproduce this consistently
- This is a RubyLLM bug, not my application code
What's broken?
A default scope can hide a chat, for example an archived one, while its batch is being collected. Each fresh poller then records the same result as chatless usage. Once the chat is visible again, the next poll adds the answer and records the usage once more, so one provider result becomes several ledger rows. A result RubyLLM can't parse but the provider billed is overcounted the same way.
Batch#chats looks the records up through the default scope, so a hidden chat is nil. deleted? then correctly says the row still exists, and deliver_once yields without leaving a marker. #1757 covers a hard-deleted chat. Its default-scope example checks the transcript, not the usage.
How to reproduce
In spec/ruby_llm/active_record/acts_as_batch_spec.rb on main, add this example to the when a default scope hides the chat context:
it 'accounts for a result once while its chat is hidden' do
batch
hidden_ids << chat.id
allow_any_instance_of(RubyLLM::Providers::Anthropic).to receive(:batch_results) do
[[0, answer('4')]]
end
expect { 2.times { described_class.find(batch_id).refresh.messages } }
.to change(RubyLLM::ActiveRecord::Usage, :count).by(1)
end
Run bundle exec rspec spec/ruby_llm/active_record/acts_as_batch_spec.rb. The provider is stubbed, so it needs no key.
Expected behavior
One usage row for one provider result, however often the batch is collected and whether or not the chat becomes visible again.
What actually happened
expected `RubyLLM::ActiveRecord::Usage.count` to have changed by 1, but was changed by 2
Two polls while hidden and two after it is visible leave three usage rows, two of them without a chat, for both a successful result and a billed failure.
Possible fixes
The fix depends on what a hidden chat should mean, so I'd like your decision before opening a PR. I tried the first two against the batch specs:
- Deliver to the chat through
unscoped. Onlyto_llmwould use the unscoped lookup, and the publicchatsstays scoped. Usage is counted once and linked to the chat, for successes and billed failures. There is no schema change, and the existing batch specs pass. The answer is added while the chat is still hidden. - Defer delivery while the chat is hidden. Nothing is counted twice, but nothing is recorded either until the chat is visible, so a chat that stays hidden never reaches the ledger.
- Record usage on collection and add the answer when the chat is visible. This needs a durable per-slot marker, so probably a new column.
I'd go with 1: the app submitted that chat itself, and it is the smallest change. @crmne, is it fine for an answer to be added to a chat the default scope hides, or should it wait until the chat is visible? I can open a PR for whichever you prefer.
Environment
- Ruby version: 4.0.9, Rails 8.1
- RubyLLM version: 2.2.3, main at c0889f414
- Provider: Anthropic batch methods, stubbed
- OS: macOS arm64
- 主要言語
- Ruby
- スター
- 4.5k
- フォーク
- 543
- 平均マージ
- 11時間 38分
- マージ済み PR(30日)
- 410
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
crmne/ruby_llm のほかの issue
-
[BUG] MCP over HTTP can't read a JSON reply that starts with a byte order mark対応中かも @yorzi が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
crmne/ruby_llm#1879 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[FEATURE] config.price cannot set image prices対応中かも @alfredszhao-art が 3 日前に担当しました。 オープンenhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
メンテナーはふだん 1 日以内に返信
-
[BUG] XLSX date-times on January 1, 1904 lose the date対応中かも @yorzi が今日担当しました。 オープンbug
難易度 3/5 半日 初心者へのやさしさ 35/100
メンテナーはふだん 1 日以内に返信
-
[BUG] after_fallback reports a fallback as failed when after_message raises on its reply対応中かも @yorzi が 2 日前に担当しました。 オープンbug
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
crmne/ruby_llm#1861 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[BUG] Agent.create resolves the model before applying the agent context対応中かも @yorzi が 2 日前に担当しました。 オープンbug
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
swombat/souls-house#1203 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
we-promise/sure#5924 ·
メンテナーはふだん 1 日以内に返信
-
Semantic knowledge base search misses matching answers, because it gets no more than 10 passages対応中かも @yorzi が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
graalvm/homebrew-tap#89 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信