Module: Smith::Workflow::Persistence

Included in:
Smith::Workflow
Defined in:
lib/smith/workflow/persistence.rb

Instance Method Summary collapse

Instance Method Details

#to_stateObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smith/workflow/persistence.rb', line 6

def to_state
  {
    class: self.class.name,
    state: @state,
    persistence_key: @persistence_key,
    context: persisted_context,
    budget_consumed: ledger_consumed,
    step_count: @step_count,
    execution_namespace: @execution_namespace,
    created_at: @created_at,
    updated_at: @updated_at,
    next_transition_name: @next_transition_name,
    session_messages: @session_messages || [],
    total_cost: @total_cost || 0.0,
    total_tokens: @total_tokens || 0,
    tool_results: @tool_results || [],
    outcome: snapshot_outcome,
    # New durable fields for hadithi billing. All wrapped in
    # snapshot_value so non-JSON-safe runtime values (e.g.
    # custom Hash details on DeterministicStepFailure) get the
    # same deep-copy treatment as context/session_messages/etc.
    usage_entries: snapshot_value((@usage_entries || []).map(&:to_h)),
    last_output: snapshot_value(@last_output),
    last_failed_step: snapshot_value(@last_failed_step),
    # Optimistic-locking version. Adapters that support
    # store_versioned use this to detect concurrent writes; adapters
    # that don't (CacheStore, RailsCache) ignore it.
    persistence_version: @persistence_version || 0,
    # Schema version of the workflow class that wrote this payload.
    # Restore dispatches through migrate_from blocks when the
    # stored value lags the workflow's current
    # persistence_schema_version.
    schema_version: self.class.persistence_schema_version,
    # SHA256 digest of the seed_messages produced at this
    # workflow's construction. Stays stable across persist/restore
    # cycles so seed_validation can detect when the seed builder
    # has changed in code since this workflow was persisted.
    seed_digest: @seed_digest,
    # Step-in-progress idempotency marker. Set true between
    # persist-before-advance and persist-after-advance when the
    # workflow class opts into idempotency_mode :strict. Restore
    # raises Smith::StepInProgressOnRestore if true under strict
    # mode. Lax mode leaves this false and never raises.
    step_in_progress: @step_in_progress || false,
    # Keys recorded via DeterministicStep#write_context. Used by
    # persist :auto Context mode to scope the persisted context
    # slice. Always emitted (sorted for stable diffing) so
    # explicit-mode workflows produce forward-compatible payloads.
    persisted_keys: (@persisted_keys || ::Set.new).to_a.map(&:to_sym).sort
  }
end