Class: Roast::Workflow::FileStateRepository

Inherits:
StateRepository show all
Defined in:
lib/roast/workflow/file_state_repository.rb

Overview

File-based implementation of StateRepository Handles state persistence to the filesystem in a thread-safe manner

Instance Method Summary collapse

Constructor Details

#initialize(session_manager = SessionManager.new) ⇒ FileStateRepository

Returns a new instance of FileStateRepository.



8
9
10
11
12
# File 'lib/roast/workflow/file_state_repository.rb', line 8

def initialize(session_manager = SessionManager.new)
  super()
  @state_mutex = Mutex.new
  @session_manager = session_manager
end

Instance Method Details

#load_state_before_step(workflow, step_name, timestamp: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/roast/workflow/file_state_repository.rb', line 33

def load_state_before_step(workflow, step_name, timestamp: nil)
  session_dir = @session_manager.find_session_directory(workflow.session_name, workflow.file, timestamp)
  return false unless session_dir

  step_files = find_step_files(session_dir)
  return false if step_files.empty?

  target_index = find_step_before(step_files, step_name)

  if target_index.nil?
    $stderr.puts "No suitable state found for step #{step_name} - no prior steps found in session."
    return false
  end

  if target_index < 0
    $stderr.puts "No state before step #{step_name} (it may be the first step)"
    return false
  end

  state_file = step_files[target_index]
  state_data = load_state_file(state_file)

  # Extract the loaded step name for diagnostics
  loaded_step = File.basename(state_file).split("_", 3)[2].sub(/\.json$/, "")
  $stderr.puts "Found state from step: #{loaded_step} (will replay from here to #{step_name})"

  # If no timestamp provided and workflow has no session, copy states to new session
  should_copy = !timestamp && workflow.session_timestamp.nil?

  copy_states_to_new_session(workflow, session_dir, step_files[0..target_index]) if should_copy
  state_data
end

#save_final_output(workflow, output_content) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/roast/workflow/file_state_repository.rb', line 66

def save_final_output(workflow, output_content)
  return if output_content.empty?

  session_dir = @session_manager.ensure_session_directory(
    workflow.object_id,
    workflow.session_name,
    workflow.file,
    timestamp: workflow.session_timestamp,
  )
  output_file = File.join(session_dir, "final_output.txt")
  File.write(output_file, output_content)
  output_file
rescue => e
  $stderr.puts "Failed to save final output: #{e.message}"
  nil
end

#save_state(workflow, step_name, state_data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roast/workflow/file_state_repository.rb', line 14

def save_state(workflow, step_name, state_data)
  @state_mutex.synchronize do
    # If workflow doesn't have a timestamp, let the session manager create one
    workflow.session_timestamp ||= @session_manager.create_new_session(workflow.object_id)

    session_dir = @session_manager.ensure_session_directory(
      workflow.object_id,
      workflow.session_name,
      workflow.file,
      timestamp: workflow.session_timestamp,
    )
    step_file = File.join(session_dir, format_step_filename(state_data[:order], step_name))
    FileUtils.mkdir_p(File.dirname(step_file))
    File.write(step_file, JSON.pretty_generate(state_data))
  end
rescue => e
  $stderr.puts "Failed to save state for step #{step_name}: #{e.message}"
end