Class: Aidp::Database::Repositories::CheckpointRepository

Inherits:
Aidp::Database::Repository show all
Defined in:
lib/aidp/database/repositories/checkpoint_repository.rb

Overview

Repository for checkpoint and checkpoint_history tables Replaces checkpoint.yml and checkpoint_history.jsonl

Instance Attribute Summary

Attributes inherited from Aidp::Database::Repository

#project_dir, #table_name

Instance Method Summary collapse

Constructor Details

#initialize(project_dir: Dir.pwd) ⇒ CheckpointRepository

Returns a new instance of CheckpointRepository.



11
12
13
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 11

def initialize(project_dir: Dir.pwd)
  super(project_dir: project_dir, table_name: "checkpoints")
end

Instance Method Details

#append_history(step_name:, iteration: nil, status: nil, timestamp: nil, metrics: {}) ⇒ Object

Append checkpoint to history

Parameters:

  • step_name (String)
  • iteration (Integer) (defaults to: nil)
  • status (String) (defaults to: nil)
  • timestamp (String, nil) (defaults to: nil)
  • metrics (Hash) (defaults to: {})


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 102

def append_history(step_name:, iteration: nil, status: nil, timestamp: nil, metrics: {})
  execute(
    <<~SQL,
      INSERT INTO checkpoint_history (project_dir, step_name, step_index, status, timestamp, metadata)
      VALUES (?, ?, ?, ?, ?, ?)
    SQL
    [
      project_dir,
      step_name,
      iteration,
      status,
      timestamp || current_timestamp,
      serialize_json(metrics)
    ]
  )
  Aidp.log_debug("checkpoint_repository", "history_appended", step: step_name)
end

#clearObject

Clear all checkpoint data for project



139
140
141
142
143
144
145
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 139

def clear
  transaction do
    execute("DELETE FROM checkpoints WHERE project_dir = ?", [project_dir])
    execute("DELETE FROM checkpoint_history WHERE project_dir = ?", [project_dir])
  end
  Aidp.log_debug("checkpoint_repository", "cleared", project_dir: project_dir)
end

#current_checkpointHash?

Get current checkpoint for project

Returns:

  • (Hash, nil)

    Current checkpoint or nil



85
86
87
88
89
90
91
92
93
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 85

def current_checkpoint
  row = query_one(
    "SELECT * FROM checkpoints WHERE project_dir = ? ORDER BY updated_at DESC LIMIT 1",
    [project_dir]
  )
  return nil unless row

  deserialize_checkpoint(row)
end

#history(limit: 100) ⇒ Array<Hash>

Get checkpoint history

Parameters:

  • limit (Integer) (defaults to: 100)

    Maximum entries to return

Returns:

  • (Array<Hash>)

    Checkpoint history entries



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 124

def history(limit: 100)
  rows = query(
    <<~SQL,
      SELECT * FROM checkpoint_history
      WHERE project_dir = ?
      ORDER BY timestamp DESC, id DESC
      LIMIT ?
    SQL
    [project_dir, limit]
  )

  rows.map { |row| deserialize_history_entry(row) }.reverse
end

#save_checkpoint(step_name:, iteration: nil, status: nil, run_loop_started_at: nil, metrics: {}) ⇒ Integer

Save or update current checkpoint

Parameters:

  • step_name (String)
  • iteration (Integer) (defaults to: nil)
  • status (String) (defaults to: nil)
  • run_loop_started_at (String, nil) (defaults to: nil)
  • metrics (Hash) (defaults to: {})

Returns:

  • (Integer)

    Checkpoint ID



23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aidp/database/repositories/checkpoint_repository.rb', line 23

def save_checkpoint(step_name:, iteration: nil, status: nil, run_loop_started_at: nil, metrics: {})
  data = {
    step_name: step_name,
    iteration: iteration,
    status: status,
    run_loop_started_at: run_loop_started_at,
    metrics: metrics
  }
  existing = current_checkpoint
  now = current_timestamp

  if existing
    existing_id = existing[:id]
    execute(
      <<~SQL,
        UPDATE checkpoints SET
          step_name = ?,
          step_index = ?,
          status = ?,
          run_loop_started_at = ?,
          metadata = ?,
          updated_at = ?
        WHERE id = ?
      SQL
      [
        data[:step_name],
        data[:iteration],
        data[:status],
        data[:run_loop_started_at],
        serialize_json(data[:metrics]),
        now,
        existing_id
      ]
    )
    Aidp.log_debug("checkpoint_repository", "updated", id: existing_id)
    existing_id
  else
    execute(
      insert_sql([
        :project_dir, :step_name, :step_index, :status,
        :run_loop_started_at, :metadata, :created_at, :updated_at
      ]),
      [
        project_dir,
        data[:step_name],
        data[:iteration],
        data[:status],
        data[:run_loop_started_at],
        serialize_json(data[:metrics]),
        now,
        now
      ]
    )
    id = last_insert_row_id
    Aidp.log_debug("checkpoint_repository", "created", id: id)
    id
  end
end