core.turn_checkpoint module
Durable, atomic whole-turn and tool-call checkpoints.
The inbound and outbound Redis Streams are at-least-once transports. This module journals the canonical model transcript and every logical tool effect in one Redis Cluster-safe hash so a reclaimed turn resumes instead of regenerating provider decisions or repeating effects.
Every mutation is one single-key Lua EVAL. Large or structured values are
msgpack encoded, zlib compressed, and base64 framed before entering the hash.
The readable scalar fields exist only for Lua compare-and-set transitions.
- class core.turn_checkpoint.ToolCallAction(*values)
-
Outcome of a tool-slot claim or state transition.
- ACQUIRED = 'ACQUIRED'
- RECLAIMED = 'RECLAIMED'
- BUSY = 'BUSY'
- BEGUN = 'BEGUN'
- ALREADY_EXECUTING = 'ALREADY_EXECUTING'
- RENEWED = 'RENEWED'
- ABANDONED = 'ABANDONED'
- COMPLETED = 'COMPLETED'
- CACHED = 'CACHED'
- AMBIGUOUS = 'AMBIGUOUS'
- CONFLICT = 'CONFLICT'
- STALE_OWNER = 'STALE_OWNER'
- LEASE_EXPIRED = 'LEASE_EXPIRED'
- INVALID_STATE = 'INVALID_STATE'
- MISSING = 'MISSING'
- SCHEMA_CONFLICT = 'SCHEMA_CONFLICT'
- class core.turn_checkpoint.ToolCallDecision(action, state, round_index, slot_index, operation_id, provider_call_id, tool_name, raw_arguments, owner_token, fence, replay_safe, result=None, payload_fingerprint='')
Bases:
objectCanonical state observed after a tool-slot operation.
- Parameters:
- action: ToolCallAction
- state: ToolCallState | None
- class core.turn_checkpoint.ToolCallState(*values)
-
Durable states for one logical tool slot.
- CLAIMED = 'CLAIMED'
- EXECUTING = 'EXECUTING'
- COMPLETED = 'COMPLETED'
- AMBIGUOUS = 'AMBIGUOUS'
- exception core.turn_checkpoint.TurnCheckpointBusy
Bases:
TurnCheckpointErrorAnother fenced worker currently owns the logical turn or tool call.
- exception core.turn_checkpoint.TurnCheckpointConflict
Bases:
TurnCheckpointErrorThe logical turn/slot already contains different canonical input.
- exception core.turn_checkpoint.TurnCheckpointError
Bases:
RuntimeErrorBase class for an unusable or invalid durable turn checkpoint.
- class core.turn_checkpoint.TurnCheckpointSession(redis, trace_id, scope='primary', ttl_seconds=604800, claim_lease_seconds=30, execution_lease_seconds=900, proof_deadline_ms=0, lifecycle=None)
Bases:
objectRedis-backed journal for one trace/scope pair.
- Parameters:
- property redis: _AsyncRedis
Binary-safe Redis client shared by checkpoint sidecar stores.
- async load()
Load the canonical turn snapshot, or
Nonebeforestart.- Return type:
- async reconcile_lifecycle(snapshot=None)
Project the durable primary-turn phase into the transport lifecycle.
The turn checkpoint remains the recovery authority. Lifecycle writes happen only after the corresponding checkpoint mutation commits, so a crash can leave the projection behind but can never move it ahead of durable proof. Replaying this method is safe because lifecycle transitions are predecessor-checked and same-state idempotent.
COMPLETEDcheckpoint state remains projected atDELIVEREDrather than moving the lifecycle directly to a terminal state. The inbound consumer owns the final single-slot Lua operation that combinesXACKwith theCOMPLETEDtransition and active-index removal.- Return type:
- Parameters:
snapshot (TurnSnapshot | None)
- async start(messages, tool_names, runtime)
Create the canonical base transcript, or recover the first writer.
- async reserve_assistant(round_index, response)
Persist a provider decision before any effect and return canonical data.
- async get_assistant(round_index)
Return a previously reserved provider decision without mutation.
- logical_operation_id(round_index, slot_index)
Derive a stable effect ID using no provider-generated identity.
- async claim_tool(round_index, slot_index, provider_call_id, tool_name, raw_arguments, operation_id, replay_safe=False, owner_token=None)
Claim a logical tool slot, reclaiming only under safe lease rules.
- async begin_tool(round_index, slot_index, owner_token, fence)
Cross the effect barrier for the current fenced owner.
- Return type:
- Parameters:
- async renew_tool(round_index, slot_index, owner_token, fence)
Renew an executing slot while preserving its owner/fence.
- Return type:
- Parameters:
- async abandon_tool(round_index, slot_index, owner_token, fence)
Release an executing slot whose call provably caused no effects.
Only the live fenced owner may release, and only from
EXECUTING. The slot returns toCLAIMEDwith an expired lease so the next attempt reclaims it through the ordinary fenced path. Callers must be certain no effect was possible — a failure that merely might have reached the provider has to keep the fail-closed lease-expiry route toAMBIGUOUS.- Return type:
- Parameters:
- async complete_tool(round_index, slot_index, owner_token, fence, result_record)
Save the exact model-facing/audit result before inference resumes.
- async get_tool(round_index, slot_index)
Read a logical tool slot without changing leases or ownership.
- Return type:
- Parameters:
- async commit_round(round_index, messages, runtime)
Commit the post-tool transcript/runtime and return canonical state.
- async save_final_model(content)
First-writer reserve the terminal provider content.
- async reserve_final_delivery(reply_message_key, text, delivery=None)
Reserve delivery identity, exact text, and canonical presentation.
- async mark_delivered(reply_message_key, message_id)
Record successful deterministic delivery using the reserved key.
- Return type:
- Parameters:
- class core.turn_checkpoint.TurnPhase(*values)
-
Durable progress phases for a whole model turn.
- STARTED = 'STARTED'
- ASSISTANT_RESERVED = 'ASSISTANT_RESERVED'
- ROUND_COMMITTED = 'ROUND_COMMITTED'
- FINAL_MODEL_SAVED = 'FINAL_MODEL_SAVED'
- DELIVERY_RESERVED = 'DELIVERY_RESERVED'
- DELIVERED = 'DELIVERED'
- COMPLETED = 'COMPLETED'
- class core.turn_checkpoint.TurnSnapshot(phase, messages, tool_names, runtime, final_model=None, final_text=None, final_delivery=None, reply_message_key=None, sent_message_id=None, completed=False, current_round=-1, revision=0, input_fingerprint='', assistant_responses=<factory>)
Bases:
objectDecoded canonical whole-turn state returned to inference recovery.