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/router.rb,
lib/smith/workflow/parallel.rb,
lib/smith/workflow/pipeline.rb,
lib/smith/workflow/execution.rb,
lib/smith/workflow/durability.rb,
lib/smith/workflow/transition.rb,
lib/smith/workflow/persistence.rb,
lib/smith/workflow/execution_frame.rb,
lib/smith/workflow/nested_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/parallel_execution.rb,
lib/smith/workflow/evaluator_optimizer.rb,
lib/smith/workflow/orchestrator_worker.rb,
lib/smith/workflow/artifact_integration.rb,
lib/smith/workflow/deadline_enforcement.rb,
lib/smith/workflow/guardrail_integration.rb,
lib/smith/workflow/deterministic_execution.rb

Defined Under Namespace

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

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

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.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/smith/workflow.rb', line 190

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.



188
189
190
# File 'lib/smith/workflow.rb', line 188

def last_prepared_input
  @last_prepared_input
end

#ledgerObject (readonly)

Returns the value of attribute ledger.



188
189
190
# File 'lib/smith/workflow.rb', line 188

def ledger
  @ledger
end

#session_messagesObject (readonly)

Returns the value of attribute session_messages.



188
189
190
# File 'lib/smith/workflow.rb', line 188

def session_messages
  @session_messages
end

#stateObject (readonly)

Returns the value of attribute state.



188
189
190
# File 'lib/smith/workflow.rb', line 188

def state
  @state
end

Instance Method Details

#advance!Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/smith/workflow.rb', line 241

def advance!
  max = self.class.max_transitions || DEFAULT_MAX_TRANSITIONS
  raise MaxTransitionsExceeded if @step_count >= max

  transition = resolve_transition
  return if transition.nil?

  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)


276
277
278
# File 'lib/smith/workflow.rb', line 276

def done?
  @state == :done
end

#failed?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/smith/workflow.rb', line 280

def failed?
  @state == :failed
end

#persisted_keysObject



237
238
239
# File 'lib/smith/workflow.rb', line 237

def persisted_keys
  @persisted_keys.dup.freeze
end

#record_persisted_key!(key) ⇒ Object



284
285
286
287
288
# File 'lib/smith/workflow.rb', line 284

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

#run!Object



263
264
265
266
267
268
269
270
# File 'lib/smith/workflow.rb', line 263

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

#terminal?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/smith/workflow.rb', line 272

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