[BUG] Usage counted again on each poll of a batch whose chat a default scope hides

Aperta
#1,876 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

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
Stack tecnologico
rails, ruby

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

bug
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:

  1. Deliver to the chat through unscoped. Only to_llm would use the unscoped lookup, and the public chats stays 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.
  2. 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.
  3. 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di crmne/ruby_llm

Tutte le issue di crmne/ruby_llm

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.