Class: Smith::Workflow

Inherits:
Object
  • Object
show all
Includes:
ArtifactIntegration, BudgetIntegration, DSL, DataVolumePolicy, DeadlineEnforcement, Durability, EventIntegration, Execution, GuardrailIntegration, Persistence, SplitStepPersistence
Defined in:
lib/smith/workflow.rb,
lib/smith/workflow/dsl.rb,
lib/smith/workflow/claim.rb,
lib/smith/workflow/graph.rb,
lib/smith/workflow/router.rb,
lib/smith/workflow/parallel.rb,
lib/smith/workflow/pipeline.rb,
lib/smith/workflow/execution.rb,
lib/smith/workflow/graph_dsl.rb,
lib/smith/workflow/branch_env.rb,
lib/smith/workflow/durability.rb,
lib/smith/workflow/run_result.rb,
lib/smith/workflow/transition.rb,
lib/smith/workflow/persistence.rb,
lib/smith/workflow/usage_entry.rb,
lib/smith/workflow/agent_result.rb,
lib/smith/workflow/graph/report.rb,
lib/smith/workflow/graph/metrics.rb,
lib/smith/workflow/graph/targets.rb,
lib/smith/workflow/execution_frame.rb,
lib/smith/workflow/graph/reference.rb,
lib/smith/workflow/graph/validator.rb,
lib/smith/workflow/retry_execution.rb,
lib/smith/workflow/fanout_execution.rb,
lib/smith/workflow/graph/diagnostic.rb,
lib/smith/workflow/nested_execution.rb,
lib/smith/workflow/worker_execution.rb,
lib/smith/workflow/event_integration.rb,
lib/smith/workflow/budget_integration.rb,
lib/smith/workflow/data_volume_policy.rb,
lib/smith/workflow/deterministic_step.rb,
lib/smith/workflow/graph/reachability.rb,
lib/smith/workflow/optimization_state.rb,
lib/smith/workflow/parallel_execution.rb,
lib/smith/workflow/evaluator_optimizer.rb,
lib/smith/workflow/orchestration_state.rb,
lib/smith/workflow/orchestrator_worker.rb,
lib/smith/workflow/artifact_integration.rb,
lib/smith/workflow/deadline_enforcement.rb,
lib/smith/workflow/graph/fanout_contract.rb,
lib/smith/workflow/guardrail_integration.rb,
lib/smith/workflow/parallel/cancellation.rb,
lib/smith/workflow/graph/contract_helpers.rb,
lib/smith/workflow/split_step_persistence.rb,
lib/smith/workflow/deterministic_execution.rb,
lib/smith/workflow/graph/runtime_readiness.rb,
lib/smith/workflow/graph/state_diagnostics.rb,
lib/smith/workflow/graph/transition_snapshot.rb,
lib/smith/workflow/graph/optimization_contract.rb,
lib/smith/workflow/graph/orchestration_contract.rb,
lib/smith/workflow/graph/transition_diagnostics.rb,
lib/smith/workflow/parallel/cancellation_signal.rb,
lib/smith/workflow/graph/reachability_diagnostics.rb,
lib/smith/workflow/graph/runtime_readiness_report.rb,
lib/smith/workflow/graph/runtime_readiness_metrics.rb,
lib/smith/workflow/split_step_persistence/boundary.rb,
lib/smith/workflow/split_step_persistence/payloads.rb,
lib/smith/workflow/split_step_persistence/execution.rb,
lib/smith/workflow/graph/runtime_binding_diagnostics.rb,
lib/smith/workflow/split_step_persistence/checkpoint.rb,
lib/smith/workflow/graph/nested_readiness_diagnostics.rb,
lib/smith/workflow/split_step_persistence/inheritance.rb,
lib/smith/workflow/split_step_persistence/preparation.rb,
lib/smith/workflow/split_step_persistence/state_snapshot.rb,
lib/smith/workflow/split_step_persistence/checkpoint_state.rb,
lib/smith/workflow/graph/runtime_binding_diagnostic_builder.rb,
lib/smith/workflow/split_step_persistence/preparation_claim.rb,
lib/smith/workflow/split_step_persistence/subclass_boundary.rb,
lib/smith/workflow/split_step_persistence/transition_contract.rb,
lib/smith/workflow/split_step_persistence/preparation_recovery.rb,
lib/smith/workflow/split_step_persistence/transition_contract_freezer.rb,
lib/smith/workflow/split_step_persistence/transition_contract_signature.rb,
lib/smith/workflow/split_step_persistence/transition_contract_structured_values.rb

Defined Under Namespace

Modules: ArtifactIntegration, BudgetIntegration, Claim, DSL, DataVolumePolicy, DeadlineEnforcement, DeterministicExecution, Durability, EvaluatorOptimizer, EventIntegration, Execution, FanoutExecution, GuardrailIntegration, NestedExecution, OrchestratorWorker, ParallelExecution, Persistence, RetryExecution, SplitStepPersistence Classes: AgentResult, BranchEnv, DeterministicStep, ExecutionFrame, Graph, OptimizationState, OrchestrationState, Parallel, Pipeline, Router, RunResult, Transition, UsageEntry, WorkerExecution

Constant Summary collapse

DEFAULT_MAX_TRANSITIONS =
100

Constants included from Agent::Lifecycle

Agent::Lifecycle::TRANSIENT_ERRORS

Constants included from DataVolumePolicy

DataVolumePolicy::LIGHTWEIGHT_SCALARS

Constants included from BudgetIntegration

BudgetIntegration::AGENT_DIM_MAP, BudgetIntegration::BUDGET_DIMENSIONS, BudgetIntegration::COST_DIMENSIONS, BudgetIntegration::TOKEN_DIMENSIONS

Constants included from SplitStepPersistence

SplitStepPersistence::NO_SPLIT_TRANSITION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Durability

#advance_persisted!, #clear_persisted!, #clear_step_in_progress!, included, #mark_step_in_progress!, #persist!, #run_persisted!

Methods included from Persistence

#to_state

Methods included from DSL

included

Methods included from SplitStepPersistence

prepended

Methods included from SplitStepPersistence::Checkpoint

#complete_persisted_step!, #persist!

Methods included from SplitStepPersistence::Preparation

#confirm_prepared_step!, #prepare_persisted_step!

Methods included from SplitStepPersistence::StateSnapshot

#to_state

Methods included from SplitStepPersistence::Boundary

#advance_persisted!, #clear_persisted!, #run_persisted!

Constructor Details

#initialize(context: {}, ledger: nil, created_at: nil) ⇒ Workflow

Returns a new instance of Workflow.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/smith/workflow.rb', line 71

def initialize(context: {}, ledger: nil, created_at: nil)
  @state = self.class.initial_state
  @context = context
  @step_count = 0
  @next_transition_name = nil
  @ledger = ledger || build_ledger
  @created_at = created_at || Time.now.utc.iso8601
  @updated_at = @created_at
  @total_cost = 0.0
  @total_tokens = 0
  @outcome = nil
  # Eager init for usage tracking. Both `@usage_mutex` (lazy
  # init at the call site would race across parallel fan-out
  # branches) and the durable per-call/output/failure fields
  # must be present before any agent recording fires.
  # `restore_state` mirrors these inits because `from_state` uses
  # `allocate` and bypasses `initialize` — see persistence.rb.
  @usage_entries = []
  @usage_mutex = Mutex.new
  @last_output = nil
  @last_failed_step = nil
  # Optimistic-locking version. Incremented on each persist!; restored
  # from the persisted payload. Adapters that support store_versioned
  # raise Smith::PersistenceVersionConflict when expected_version
  # doesn't match the stored payload's version (i.e., a concurrent
  # write occurred between this process's restore and persist).
  @persistence_version = 0
  # Digest of the seed_messages produced at construction time.
  # Compared on restore against the live builder's output when
  # seed_validation is :warn or :strict; nil when no seed builder
  # ran or its output was empty.
  @seed_digest = nil
  # Idempotency marker stamped between persist-before-advance and
  # persist-after-advance under idempotency_mode :strict; restored
  # workflows with the marker set raise
  # Smith::StepInProgressOnRestore. Lax mode leaves it false.
  @step_in_progress = false
  # Process-local phase for the split-step persistence API. This is not
  # serialized: strict restore rejects the durable step_in_progress marker
  # before an uncertain step can be resumed.
  @split_step_phase = nil
  @split_step_transition_name = nil
  @split_step_mutex = Mutex.new
  # Set of context keys recorded via deterministic step write_context
  # writes. Used by persist :auto Context mode to compute the
  # persisted-context slice. Seeded from the Context class's
  # also: declaration so explicit input keys round-trip.
  @persisted_keys = ::Set.new(initial_persist_auto_seed)
  @persisted_keys_mutex = Mutex.new
  initialize_tool_result_state
  seed_initial_session_messages
end

Instance Attribute Details

#last_prepared_inputObject (readonly)

Returns the value of attribute last_prepared_input.



69
70
71
# File 'lib/smith/workflow.rb', line 69

def last_prepared_input
  @last_prepared_input
end

#ledgerObject (readonly)

Returns the value of attribute ledger.



69
70
71
# File 'lib/smith/workflow.rb', line 69

def ledger
  @ledger
end

#session_messagesObject (readonly)

Returns the value of attribute session_messages.



69
70
71
# File 'lib/smith/workflow.rb', line 69

def session_messages
  @session_messages
end

#stateObject (readonly)

Returns the value of attribute state.



69
70
71
# File 'lib/smith/workflow.rb', line 69

def state
  @state
end

Class Method Details

.graphObject



5
6
7
8
9
10
11
12
# File 'lib/smith/workflow/graph_dsl.rb', line 5

def self.graph
  Graph.new(
    workflow_class: self,
    initial_state: initial_state,
    states: @states || [],
    transitions: @transitions || {}
  )
end

.runtime_readinessObject



18
19
20
# File 'lib/smith/workflow/graph_dsl.rb', line 18

def self.runtime_readiness
  graph.runtime_readiness
end

.validate_graphObject



14
15
16
# File 'lib/smith/workflow/graph_dsl.rb', line 14

def self.validate_graph
  graph.validate
end

Instance Method Details

#advance!Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/smith/workflow.rb', line 128

def advance!
  advance_claim = SplitStepPersistence.instance_method(:claim_split_step_advance!).bind_call(self)
  ensure_transition_budget!
  @step_work_started = false

  transition = SplitStepPersistence.instance_method(:resolve_split_step_advance_transition).bind_call(self)
  transition = resolve_transition if transition.equal?(SplitStepPersistence::NO_SPLIT_TRANSITION)
  return if transition.nil?

  @step_work_started = true
  step_result = execute_step(transition)
  @step_count += 1
  @updated_at = Time.now.utc.iso8601
  record_step_snapshot(step_result)
  step_result
rescue UnresolvedTransitionError => e
  origin_state = @state
  @outcome = nil
  raise unless route_to_fail_state!

  step_result = { transition: e.requested_name, from: origin_state, to: @state, error: e }
  record_step_snapshot(step_result)
  step_result
ensure
  SplitStepPersistence.instance_method(:release_split_step_advance!).bind_call(self, advance_claim) if advance_claim
end

#done?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/smith/workflow.rb', line 169

def done?
  state_named?(:done)
end

#failed?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/smith/workflow.rb', line 173

def failed?
  state_named?(:failed)
end

#persisted_keysObject



124
125
126
# File 'lib/smith/workflow.rb', line 124

def persisted_keys
  @persisted_keys.dup.freeze
end

#record_persisted_key!(key) ⇒ Object



177
178
179
180
181
# File 'lib/smith/workflow.rb', line 177

def record_persisted_key!(key)
  @persisted_keys_mutex.synchronize do
    @persisted_keys << key.to_sym
  end
end

#run!Object



155
156
157
158
159
160
161
162
163
# File 'lib/smith/workflow.rb', line 155

def run!
  SplitStepPersistence.instance_method(:ensure_split_step_execution_allowed!).bind_call(self)
  steps = []
  until terminal?
    step = advance!
    steps << step if step
  end
  build_run_result(steps)
end

#terminal?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/smith/workflow.rb', line 165

def terminal?
  self.class.transitions_from(@state).empty? && @next_transition_name.nil?
end