[BUG] Usage counted again on each poll of a batch whose chat a default scope hides
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- Mezza giornata
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, machine-learning
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Ruby
- Stelle
- 4.5k
- Fork
- 543
- Merge medio
- 11h 38m
- PR unite (30g)
- 410
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di crmne/ruby_llm
-
[BUG] MCP over HTTP can't read a JSON reply that starts with a byte order markForse già presa @yorzi l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
crmne/ruby_llm#1879 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[FEATURE] config.price cannot set image pricesForse già presa @alfredszhao-art l’ha presa 2 giorni fa. Apertaenhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 69/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] XLSX date-times on January 1, 1904 lose the dateForse già presa @yorzi l’ha presa oggi. Apertabug
Difficoltà 3/5 Mezza giornata Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] after_fallback reports a fallback as failed when after_message raises on its replyForse già presa @yorzi l’ha presa 2 giorni fa. Apertabug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
crmne/ruby_llm#1861 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] Agent.create resolves the model before applying the agent contextForse già presa @yorzi l’ha presa 2 giorni fa. Apertabug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di crmne/ruby_llm
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
swombat/souls-house#1203 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
we-promise/sure#5924 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
CircuitVerse/CircuitVerse#8342 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runsForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 92/100
erkamyaman/rust-anything#963 ·
I maintainer di solito rispondono entro 1 giorno