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

オープン
#1,876 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rails, ruby

調査の方向性

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 の本文から書いたものです。

説明

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
主要言語
Ruby
スター
4.5k
フォーク
543
平均マージ
11時間 38分
マージ済み PR(30日)
410

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

crmne/ruby_llm のほかの issue

crmne/ruby_llm の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。