Class: Aidp::AutoUpdate::CheckpointStore

Inherits:
Object
  • Object
show all
Includes:
SafeDirectory
Defined in:
lib/aidp/auto_update/checkpoint_store.rb

Overview

Repository for persisting and restoring checkpoint state

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SafeDirectory

#safe_mkdir_p

Constructor Details

#initialize(project_dir: Dir.pwd) ⇒ CheckpointStore

Returns a new instance of CheckpointStore.



16
17
18
19
20
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 16

def initialize(project_dir: Dir.pwd)
  @project_dir = project_dir
  original_dir = File.join(project_dir, ".aidp", "checkpoints")
  @checkpoint_dir = safe_mkdir_p(original_dir, component_name: "CheckpointStore")
end

Instance Attribute Details

#checkpoint_dirObject (readonly)

Returns the value of attribute checkpoint_dir.



14
15
16
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 14

def checkpoint_dir
  @checkpoint_dir
end

Instance Method Details

#cleanup_old_checkpoints(max_age_days: 7) ⇒ Integer

Clean up old checkpoints (retention policy)

Parameters:

  • max_age_days (Integer) (defaults to: 7)

    Maximum age to retain

Returns:

  • (Integer)

    Number of checkpoints deleted



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 126

def cleanup_old_checkpoints(max_age_days: 7)
  Aidp.log_info("checkpoint_store", "cleaning_old_checkpoints",
    max_age_days: max_age_days)

  cutoff_time = Time.now - (max_age_days * 24 * 60 * 60)
  deleted_count = 0

  list_checkpoints.each do |checkpoint|
    if checkpoint.created_at < cutoff_time
      if delete_checkpoint(checkpoint.checkpoint_id)
        deleted_count += 1
      end
    end
  end

  Aidp.log_info("checkpoint_store", "cleanup_complete",
    deleted: deleted_count,
    max_age_days: max_age_days)

  deleted_count
rescue => e
  Aidp.log_error("checkpoint_store", "cleanup_failed",
    error: e.message)
  0
end

#delete_checkpoint(checkpoint_id) ⇒ Boolean

Delete checkpoint after successful restoration

Parameters:

  • checkpoint_id (String)

    Checkpoint UUID

Returns:

  • (Boolean)

    Success status



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 101

def delete_checkpoint(checkpoint_id)
  path = checkpoint_path(checkpoint_id)

  unless File.exist?(path)
    Aidp.log_warn("checkpoint_store", "checkpoint_not_found",
      id: checkpoint_id,
      path: path)
    return false
  end

  File.delete(path)

  Aidp.log_info("checkpoint_store", "checkpoint_deleted",
    id: checkpoint_id)
  true
rescue => e
  Aidp.log_error("checkpoint_store", "delete_failed",
    id: checkpoint_id,
    error: e.message)
  false
end

#latest_checkpointCheckpoint?

Find most recent checkpoint for restoration

Returns:

  • (Checkpoint, nil)

    Most recent checkpoint or nil



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 58

def latest_checkpoint
  checkpoints = list_checkpoints

  if checkpoints.empty?
    Aidp.log_debug("checkpoint_store", "no_checkpoints_found")
    return nil
  end

  # Sort by created_at descending
  latest = checkpoints.max_by { |cp| cp.created_at }

  Aidp.log_info("checkpoint_store", "found_latest_checkpoint",
    id: latest.checkpoint_id,
    created_at: latest.created_at.iso8601)

  latest
rescue => e
  Aidp.log_error("checkpoint_store", "latest_checkpoint_failed",
    error: e.message)
  nil
end

#list_checkpointsArray<Checkpoint>

List all checkpoints

Returns:

  • (Array<Checkpoint>)

    All available checkpoints



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 82

def list_checkpoints
  checkpoint_files = Dir.glob(File.join(@checkpoint_dir, "*.json"))

  checkpoints = checkpoint_files.filter_map do |file|
    load_checkpoint_file(file)
  end

  Aidp.log_debug("checkpoint_store", "listed_checkpoints",
    count: checkpoints.size)

  checkpoints
rescue => e
  Aidp.log_error("checkpoint_store", "list_failed", error: e.message)
  []
end

#save_checkpoint(checkpoint) ⇒ Boolean

Save checkpoint atomically

Parameters:

Returns:

  • (Boolean)

    Success status



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aidp/auto_update/checkpoint_store.rb', line 25

def save_checkpoint(checkpoint)
  Aidp.log_info("checkpoint_store", "saving_checkpoint",
    id: checkpoint.checkpoint_id,
    mode: checkpoint.mode)

  unless checkpoint.valid?
    Aidp.log_error("checkpoint_store", "invalid_checkpoint",
      id: checkpoint.checkpoint_id,
      error: "Checksum validation failed")
    return false
  end

  # Write to temp file first
  temp_file = "#{checkpoint_path(checkpoint.checkpoint_id)}.tmp"
  File.write(temp_file, JSON.pretty_generate(checkpoint.to_h))

  # Atomic rename
  File.rename(temp_file, checkpoint_path(checkpoint.checkpoint_id))

  Aidp.log_info("checkpoint_store", "checkpoint_saved",
    id: checkpoint.checkpoint_id,
    path: checkpoint_path(checkpoint.checkpoint_id))
  true
rescue => e
  Aidp.log_error("checkpoint_store", "save_failed",
    id: checkpoint.checkpoint_id,
    error: e.message)
  File.delete(temp_file) if temp_file && File.exist?(temp_file)
  false
end