class RubyLLM::AgUI::Rails::ActiveRecordStore
The durable run store: run identity in a table instead of a process.
The in-process store is correct for one process only. A host that runs more than one Puma worker, or that restarts, needs the reservation, the interrupts and the terminal status to outlive the worker that produced them, which is what this store is for.
Ordering is by row id, so โthe latest run on the threadโ means the run reserved last, not the run whose clock reading is largest. Two requests that arrive in the same millisecond still have an order.
RubyLLM::AgUI.configure { |config| config.run_store = ActiveRecordStore.new }
Public Instance Methods
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 40 def find(scope:, thread_id:, run_id:) row_for(scope, thread_id, run_id)&.as_run_record end
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 59 def find_interrupt(scope:, thread_id:, interrupt_id:) raise InvalidInputError, 'interrupt_id is required.' if blank?(interrupt_id) row_for_interrupt(scope, thread_id, interrupt_id)&.as_run_record&.interrupt(interrupt_id) end
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 48 def latest_interrupted(scope:, thread_id:) interrupted_rows(scope, thread_id).last&.as_run_record end
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 44 def latest_run(scope:, thread_id:) thread_rows(scope, thread_id).last&.as_run_record end
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 67 def redeem_interrupt(scope:, thread_id:, interrupt_id:, decision:, now:) unless Interrupt::DECISIONS.include?(decision) raise ConfigurationError, "decision must be one of #{Interrupt::DECISIONS.join(', ')}" end retry_stale do row = row_for_interrupt(scope, thread_id, interrupt_id) raise ResumeRejectedError, "no interrupt #{interrupt_id}" if row.nil? redeem(row.as_run_record, interrupt_id, decision, now) end end
Records decision with a conditional update. Competing writers reload and revalidate, so only one can redeem the approval.
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 29 def reserve(scope:, thread_id:, run_id:, digest:) scope_key(scope) record = RunRecord.new( scope: scope, thread_id: thread_id, run_id: run_id, digest: digest, created_at: now ) Record.from_run_record(record).save! Reservation.new(record: record, status: Reservation::CREATED) rescue ::ActiveRecord::RecordNotUnique attach(row_for(scope, thread_id, run_id), digest) end
Claims (scope, thread_id, run_id) for digest.
The unique index on the identity is the real guard: two workers that insert at the same time cannot both win, and the loser attaches to the row the winner wrote instead of executing the turn a second time.
Source
# File lib/ruby_llm/ag_ui/rails/active_record_store.rb, line 52 def save(record) scope_key(record.scope) row = row_for(record.scope, record.thread_id, record.run_id) updated = next_record(row&.as_run_record, record) row.write_run_record(updated) end