Class: Aidp::Harness::State::Persistence

Inherits:
Object
  • Object
show all
Includes:
SafeDirectory
Defined in:
lib/aidp/harness/state/persistence.rb

Overview

Handles file I/O and persistence for state management

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SafeDirectory

#safe_mkdir_p

Constructor Details

#initialize(project_dir, mode, skip_persistence: false) ⇒ Persistence

Returns a new instance of Persistence.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/aidp/harness/state/persistence.rb', line 17

def initialize(project_dir, mode, skip_persistence: false)
  @project_dir = project_dir
  @mode = mode
  @state_dir = File.join(project_dir, ".aidp", "harness")
  @state_file = File.join(@state_dir, "#{mode}_state.json")
  @lock_file = File.join(@state_dir, "#{mode}_state.lock")
  # Use explicit skip_persistence flag for dependency injection
  # Callers should set skip_persistence: true for test/dry-run scenarios
  @skip_persistence = skip_persistence
  ensure_state_directory
  Aidp.log_debug("state_persistence", "initialized", mode: @mode, skip: @skip_persistence, dir: @state_dir)
end

Instance Attribute Details

#lock_fileObject (readonly)

Expose for testability



15
16
17
# File 'lib/aidp/harness/state/persistence.rb', line 15

def lock_file
  @lock_file
end

#state_fileObject (readonly)

Expose for testability



15
16
17
# File 'lib/aidp/harness/state/persistence.rb', line 15

def state_file
  @state_file
end

Instance Method Details

#clear_stateObject



63
64
65
66
67
68
69
70
71
# File 'lib/aidp/harness/state/persistence.rb', line 63

def clear_state
  return if @skip_persistence

  with_lock do
    Aidp.log_debug("state_persistence", "clear_state.start", file: @state_file)
    File.delete(@state_file) if File.exist?(@state_file)
    Aidp.log_debug("state_persistence", "clear_state.done", file: @state_file)
  end
end

#has_state?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/aidp/harness/state/persistence.rb', line 30

def has_state?
  return false if @skip_persistence
  exists = File.exist?(@state_file)
  Aidp.log_debug("state_persistence", "has_state?", exists: exists, file: @state_file) if exists
  exists
end

#load_stateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aidp/harness/state/persistence.rb', line 37

def load_state
  return {} if @skip_persistence || !has_state?

  with_lock do
    Aidp.log_debug("state_persistence", "load_state.start", file: @state_file)
    content = File.read(@state_file)
    parsed = JSON.parse(content, symbolize_names: true)
    Aidp.log_debug("state_persistence", "load_state.success", keys: parsed.keys.size, file: @state_file)
    parsed
  rescue JSON::ParserError => e
    Aidp.log_warn("state_persistence", "parse_error", error: e.message, file: @state_file)
    {}
  end
end

#save_state(state_data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/aidp/harness/state/persistence.rb', line 52

def save_state(state_data)
  return if @skip_persistence

  with_lock do
    Aidp.log_debug("state_persistence", "save_state.start", keys: state_data.keys.size)
     = (state_data)
    write_atomically()
    Aidp.log_debug("state_persistence", "save_state.written", file: @state_file, size: .keys.size)
  end
end