Reconnects and Run Identity
Reserve each run once and report its recorded outcome after a reconnect.
After reading this guide, you will know:
- How the gem reserves
(scope, threadId, runId)for one input digest. - How retries and reconnects differ from duplicate execution.
- Which store to use for one process or several workers.
Reuse a Run Identity
The store reserves a run before any provider work. It keys the record by authenticated scope, thread id, and run id, then compares the canonical digest of the accepted input.
| Request | Result |
|---|---|
| New identity | Reserve and execute the run. |
| Same identity and same input while running | Refuse a second live stream. |
| Same identity and same input after completion | Return the recorded start, optional state snapshot, and terminal event. |
| Same identity with different input | Raise a run conflict. |
Reconnect does not replay text or tool deltas. Replaying deltas could display text twice or repeat a side effect. The host can publish a state snapshot for a finished run, but the gem does not reconstruct conversation history.
The same rule applies to a completed approval-resume run. Resend its exact
input, including resume, with the original run id to receive the recorded
outcome without applying the decision again. A new run id that tries to answer
an already answered interrupt is refused. Changing the input under an existing
run id is a conflict.
Choose a Store
MemoryStore is suitable for tests and a single process. Its records disappear on restart and are not shared between workers. The Rails adapter’s ActiveRecordStore persists records and redeems interrupts with atomic conditional updates.
The store boundary is RubyLLM::AgUI::Store::Base. Configure a compatible store globally or pass it to Run.new. For multi-process deployments, the database must enforce unique run identity and serialize interrupt redemption.
Store writes check RunRecord#revision and refuse stale snapshots. A run whose
worker died remains non-terminal until the host confirms the worker stopped
and explicitly recovers it. See
Worker Recovery before changing a
stuck record; deleting its reservation can repeat unknown tool effects.
Next Steps
- Configuration - set the default store.
- Rails Integration - install the durable ActiveRecord store.
- Protocol Coverage - inspect reconnect events.
Keep Request and Record Values Read-only
Parsed input, run identity, and JSON snapshots are isolated from the objects
supplied by the caller. Nested strings, arrays, hashes, and parsed SDK message
models are frozen. Editing them raises FrozenError; it cannot change the
reserved digest or bypass a store revision check. Constructors copy JSON values
rather than freezing the host’s original state. Keep snapshots JSON-compatible.
Create a new input with Input.parse for a new request. Use RunRecord
transitions and retain store.save’s returned record when updating storage.
After Run#stream returns a recorded outcome, Run#record exposes that same
record, including on a reconnect. Reading it never executes the chat.