Class: Aidp::Database::Repositories::PromptArchiveRepository

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

Overview

Repository for prompt_archive table Replaces prompt_archive/*.md files Write-only audit trail of prompts

Instance Attribute Summary

Attributes inherited from Aidp::Database::Repository

#project_dir, #table_name

Instance Method Summary collapse

Constructor Details

#initialize(project_dir: Dir.pwd) ⇒ PromptArchiveRepository

Returns a new instance of PromptArchiveRepository.



12
13
14
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 12

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

Instance Method Details

#archive(step_name:, content:) ⇒ Integer

Archive a prompt

Parameters:

  • step_name (String, nil)

    Step name

  • content (String)

    Prompt content

Returns:

  • (Integer)

    Archive entry ID



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 21

def archive(step_name:, content:)
  now = current_timestamp

  execute(
    insert_sql([:project_dir, :step_name, :content, :archived_at]),
    [project_dir, step_name, content, now]
  )

  id = last_insert_row_id

  Aidp.log_debug("prompt_archive_repository", "archived",
    id: id, step: step_name, size: content.length)

  id
end

#cleanup(days_to_keep: 30) ⇒ Integer

Clear old archives (keep recent N days)

Parameters:

  • days_to_keep (Integer) (defaults to: 30)

    Days of history to retain

Returns:

  • (Integer)

    Number of entries deleted



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 161

def cleanup(days_to_keep: 30)
  threshold = (Time.now - (days_to_keep * 24 * 60 * 60)).strftime("%Y-%m-%d %H:%M:%S")

  count = query_value(
    "SELECT COUNT(*) FROM prompt_archive WHERE project_dir = ? AND archived_at < ?",
    [project_dir, threshold]
  ) || 0

  execute(
    "DELETE FROM prompt_archive WHERE project_dir = ? AND archived_at < ?",
    [project_dir, threshold]
  )

  Aidp.log_debug("prompt_archive_repository", "cleanup",
    deleted: count, threshold: threshold)

  count
end

#find(id) ⇒ Hash?

Get archive entry by ID

Parameters:

  • id (Integer)

    Entry ID

Returns:

  • (Hash, nil)

    Entry or nil



72
73
74
75
76
77
78
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 72

def find(id)
  row = query_one(
    "SELECT * FROM prompt_archive WHERE id = ? AND project_dir = ?",
    [id, project_dir]
  )
  deserialize_entry(row)
end

#latest_for_step(step_name) ⇒ Hash?

Get latest archived prompt for a step

Parameters:

  • step_name (String)

    Step name

Returns:

  • (Hash, nil)

    Latest entry or nil



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

def latest_for_step(step_name)
  row = query_one(
    <<~SQL,
      SELECT * FROM prompt_archive
      WHERE project_dir = ? AND step_name = ?
      ORDER BY archived_at DESC, id DESC
      LIMIT 1
    SQL
    [project_dir, step_name]
  )
  deserialize_entry(row)
end

#recent(limit: 50, step_name: nil) ⇒ Array<Hash>

Get recent archived prompts

Parameters:

  • limit (Integer) (defaults to: 50)

    Maximum entries

  • step_name (String, nil) (defaults to: nil)

    Filter by step

Returns:

  • (Array<Hash>)

    Archive entries



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
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 42

def recent(limit: 50, step_name: nil)
  rows = if step_name
    query(
      <<~SQL,
        SELECT * FROM prompt_archive
        WHERE project_dir = ? AND step_name = ?
        ORDER BY archived_at DESC
        LIMIT ?
      SQL
      [project_dir, step_name, limit]
    )
  else
    query(
      <<~SQL,
        SELECT * FROM prompt_archive
        WHERE project_dir = ?
        ORDER BY archived_at DESC
        LIMIT ?
      SQL
      [project_dir, limit]
    )
  end

  rows.map { |row| deserialize_entry(row) }
end

#search(query_text, limit: 20) ⇒ Array<Hash>

Search archived prompts

Parameters:

  • query_text (String)

    Search text

  • limit (Integer) (defaults to: 20)

    Maximum results

Returns:

  • (Array<Hash>)

    Matching entries



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 143

def search(query_text, limit: 20)
  rows = query(
    <<~SQL,
      SELECT * FROM prompt_archive
      WHERE project_dir = ? AND content LIKE ?
      ORDER BY archived_at DESC
      LIMIT ?
    SQL
    [project_dir, "%#{query_text}%", limit]
  )

  rows.map { |row| deserialize_entry(row) }
end

#statsHash

Get archive stats

Returns:

  • (Hash)

    Statistics



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/aidp/database/repositories/prompt_archive_repository.rb', line 100

def stats
  total = query_value(
    "SELECT COUNT(*) FROM prompt_archive WHERE project_dir = ?",
    [project_dir]
  ) || 0

  by_step_rows = query(
    <<~SQL,
      SELECT step_name, COUNT(*) as count
      FROM prompt_archive
      WHERE project_dir = ?
      GROUP BY step_name
    SQL
    [project_dir]
  )

  by_step = by_step_rows.each_with_object({}) do |row, h|
    h[row["step_name"] || "unknown"] = row["count"]
  end

  first = query_one(
    "SELECT archived_at FROM prompt_archive WHERE project_dir = ? ORDER BY archived_at ASC LIMIT 1",
    [project_dir]
  )

  last = query_one(
    "SELECT archived_at FROM prompt_archive WHERE project_dir = ? ORDER BY archived_at DESC LIMIT 1",
    [project_dir]
  )

  {
    total: total,
    by_step: by_step,
    first_archived_at: first&.dig("archived_at"),
    last_archived_at: last&.dig("archived_at")
  }
end