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



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aidp/harness/state/persistence.rb', line 14

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 Method Details

#clear_stateObject



60
61
62
63
64
65
66
67
68
# File 'lib/aidp/harness/state/persistence.rb', line 60

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)


27
28
29
30
31
32
# File 'lib/aidp/harness/state/persistence.rb', line 27

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



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

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/aidp/harness/state/persistence.rb', line 49

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