Class: Smith::Workflow

Inherits:
Object
  • Object
show all
Includes:
ArtifactIntegration, BudgetIntegration, DSL, DataVolumePolicy, DeadlineEnforcement, Durability, EventIntegration, Execution, GuardrailIntegration, Persistence
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/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/graph/runtime_binding_diagnostics.rb,
lib/smith/workflow/graph/nested_readiness_diagnostics.rb,
lib/smith/workflow/graph/runtime_binding_diagnostic_builder.rb

Defined Under Namespace

Modules: ArtifactIntegration, BudgetIntegration, Claim, DSL, DataVolumePolicy, DeadlineEnforcement, DeterministicExecution, Durability, EvaluatorOptimizer, EventIntegration, Execution, FanoutExecution, GuardrailIntegration, NestedExecution, OrchestratorWorker, ParallelExecution, Persistence, RetryExecution 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

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

Constructor Details

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

Returns a new instance of Workflow.



70
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
# File 'lib/smith/workflow.rb', line 70

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
  # 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.



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

def last_prepared_input
  @last_prepared_input
end

#ledgerObject (readonly)

Returns the value of attribute ledger.



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

def ledger
  @ledger
end

#session_messagesObject (readonly)

Returns the value of attribute session_messages.



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

def session_messages
  @session_messages
end

#stateObject (readonly)

Returns the value of attribute state.



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/smith/workflow.rb', line 121

def advance!
  ensure_transition_budget!
  @step_work_started = false

  transition = resolve_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
end

#done?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/smith/workflow.rb', line 157

def done?
  state_named?(:done)
end

#failed?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/smith/workflow.rb', line 161

def failed?
  state_named?(:failed)
end

#persisted_keysObject



117
118
119
# File 'lib/smith/workflow.rb', line 117

def persisted_keys
  @persisted_keys.dup.freeze
end

#record_persisted_key!(key) ⇒ Object



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

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

#run!Object



144
145
146
147
148
149
150
151
# File 'lib/smith/workflow.rb', line 144

def run!
  steps = []
  until terminal?
    step = advance!
    steps << step if step
  end
  build_run_result(steps)
end

#terminal?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/smith/workflow.rb', line 153

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