Class: Aidp::Database::Repositories::WatchStateRepository

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

Overview

Repository for watch_state table Replaces watch/*.yml files Stores all watch mode state in a single JSON blob per repository

Instance Attribute Summary collapse

Attributes inherited from Aidp::Database::Repository

#project_dir, #table_name

Instance Method Summary collapse

Constructor Details

#initialize(project_dir: Dir.pwd, repository: nil) ⇒ WatchStateRepository

Returns a new instance of WatchStateRepository.



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

def initialize(project_dir: Dir.pwd, repository: nil)
  super(project_dir: project_dir, table_name: "watch_state")
  @repository = repository
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



17
18
19
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 17

def repository
  @repository
end

Instance Method Details

#auto_pr_cap_reached?(pr_number, cap:) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 223

def auto_pr_cap_reached?(pr_number, cap:)
  auto_pr_iteration_count(pr_number) >= cap
end

#auto_pr_data(pr_number) ⇒ Object



191
192
193
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 191

def auto_pr_data(pr_number)
  auto_prs[pr_number.to_s]
end

#auto_pr_iteration_count(pr_number) ⇒ Object

Auto PR tracking methods



185
186
187
188
189
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 185

def auto_pr_iteration_count(pr_number)
  data = auto_prs[pr_number.to_s]
  return 0 unless data
  data[:iteration] || 0
end

#build_status(issue_number) ⇒ Object

Build tracking methods



63
64
65
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 63

def build_status(issue_number)
  builds[issue_number.to_s] || {}
end

#change_request_data(pr_number) ⇒ Object



158
159
160
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 158

def change_request_data(pr_number)
  change_requests[pr_number.to_s]
end

#change_request_processed?(pr_number) ⇒ Boolean

Change request tracking methods

Returns:

  • (Boolean)


154
155
156
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 154

def change_request_processed?(pr_number)
  change_requests.key?(pr_number.to_s)
end

#ci_fix_completed?(pr_number) ⇒ Boolean

CI fix tracking methods

Returns:

  • (Boolean)


131
132
133
134
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 131

def ci_fix_completed?(pr_number)
  fix_data = ci_fixes[pr_number.to_s]
  !!(fix_data && fix_data[:status] == "completed")
end

#ci_fix_data(pr_number) ⇒ Object



136
137
138
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 136

def ci_fix_data(pr_number)
  ci_fixes[pr_number.to_s]
end

#complete_auto_pr(pr_number, data = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 211

def complete_auto_pr(pr_number, data = {})
  key = pr_number.to_s
  existing = auto_prs[key] || {}

  auto_prs[key] = existing.merge({
    status: "completed",
    completed_at: current_timestamp
  }).merge(symbolize_keys(data))

  save!
end

#detection_comment_posted?(detection_key) ⇒ Boolean

Detection comment tracking

Returns:

  • (Boolean)


229
230
231
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 229

def detection_comment_posted?(detection_key)
  detection_comments.key?(detection_key.to_s)
end

#find_build_by_pr(pr_number) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 89

def find_build_by_pr(pr_number)
  builds.each do |issue_number, data|
    pr_url = data[:pr_url]
    next unless pr_url

    if pr_url.match?(%r{/pull/#{pr_number}\b})
      return {
        issue_number: issue_number.to_i,
        branch: data[:branch],
        workstream: data[:workstream],
        pr_url: pr_url,
        status: data[:status]
      }
    end
  end

  nil
end

#mark_reaction_processed(comment_id, reaction_id) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 305

def mark_reaction_processed(comment_id, reaction_id)
  key = comment_id.to_s
  processed_reactions[key] ||= {reaction_ids: [], last_checked: nil}
  processed_reactions[key][:reaction_ids] << reaction_id unless processed_reactions[key][:reaction_ids].include?(reaction_id)
  processed_reactions[key][:last_checked] = current_timestamp
  save!
end

#plan_data(issue_number) ⇒ Object



32
33
34
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 32

def plan_data(issue_number)
  plans[issue_number.to_s]
end

#plan_iteration_count(issue_number) ⇒ Object



36
37
38
39
40
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 36

def plan_iteration_count(issue_number)
  plan = plans[issue_number.to_s]
  return 0 unless plan
  plan[:iteration] || 1
end

#plan_processed?(issue_number) ⇒ Boolean

Plan tracking methods

Returns:

  • (Boolean)


28
29
30
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 28

def plan_processed?(issue_number)
  plans.key?(issue_number.to_s)
end

#processed_reaction_ids(comment_id) ⇒ Object



299
300
301
302
303
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 299

def processed_reaction_ids(comment_id)
  data = processed_reactions[comment_id.to_s]
  return [] unless data
  data[:reaction_ids] || []
end

#record_auto_pr_iteration(pr_number, data = {}) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 195

def record_auto_pr_iteration(pr_number, data = {})
  key = pr_number.to_s
  existing = auto_prs[key] || {}
  iteration = (existing[:iteration] || 0) + 1

  auto_prs[key] = {
    iteration: iteration,
    last_processed_at: current_timestamp,
    status: data[:status] || "in_progress",
    metadata: symbolize_keys(data[:metadata] || {})
  }.merge(symbolize_keys(data.except(:status, :metadata)))

  save!
  iteration
end

#record_build_status(issue_number, status:, details: {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 67

def record_build_status(issue_number, status:, details: {})
  builds[issue_number.to_s] = {
    status: status,
    updated_at: current_timestamp
  }.merge(symbolize_keys(details))

  save!
end

#record_change_request(pr_number, data) ⇒ Object



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

def record_change_request(pr_number, data)
  change_requests[pr_number.to_s] = {
    status: data[:status],
    timestamp: data[:timestamp] || current_timestamp,
    changes_applied: data[:changes_applied],
    commits: data[:commits],
    reason: data[:reason],
    clarification_count: data[:clarification_count],
    verification_reasons: data[:verification_reasons],
    missing_items: data[:missing_items],
    additional_work: data[:additional_work]
  }.compact

  save!
end

#record_ci_fix(pr_number, data) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 140

def record_ci_fix(pr_number, data)
  ci_fixes[pr_number.to_s] = {
    status: data[:status],
    timestamp: data[:timestamp] || current_timestamp,
    reason: data[:reason],
    root_causes: data[:root_causes],
    fixes_count: data[:fixes_count]
  }.compact

  save!
end

#record_detection_comment(detection_key, timestamp:) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 233

def record_detection_comment(detection_key, timestamp:)
  detection_comments[detection_key.to_s] = {
    timestamp: timestamp,
    posted_at: current_timestamp
  }
  save!
end

#record_plan(issue_number, data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 42

def record_plan(issue_number, data)
  existing_plan = plans[issue_number.to_s]
  iteration = existing_plan ? (existing_plan[:iteration] || 1) + 1 : 1

  plans[issue_number.to_s] = {
    summary: data[:summary],
    tasks: data[:tasks],
    questions: data[:questions],
    comment_body: data[:comment_body],
    comment_hint: data[:comment_hint],
    comment_id: data[:comment_id],
    posted_at: data[:posted_at] || current_timestamp,
    iteration: iteration,
    previous_iteration_at: existing_plan&.dig(:posted_at)
  }.compact

  save!
end

#record_review(pr_number, data) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 118

def record_review(pr_number, data)
  reviews[pr_number.to_s] = {
    timestamp: data[:timestamp] || current_timestamp,
    reviewers: data[:reviewers],
    total_findings: data[:total_findings],
    comment_id: data[:comment_id]
  }.compact

  save!
end

#reset_change_request_state(pr_number) ⇒ Object



178
179
180
181
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 178

def reset_change_request_state(pr_number)
  change_requests.delete(pr_number.to_s)
  save!
end

#review_data(pr_number) ⇒ Object



114
115
116
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 114

def review_data(pr_number)
  reviews[pr_number.to_s]
end

#review_processed?(pr_number) ⇒ Boolean

Review tracking methods

Returns:

  • (Boolean)


110
111
112
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 110

def review_processed?(pr_number)
  reviews.key?(pr_number.to_s)
end

#stateHash

Get or create state for repository

Returns:

  • (Hash)

    State data



22
23
24
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 22

def state
  @state ||= load_or_create_state
end

#track_comment_for_feedback(comment_id:, processor_type:, number:) ⇒ Object



288
289
290
291
292
293
294
295
296
297
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 288

def track_comment_for_feedback(comment_id:, processor_type:, number:)
  key = "#{processor_type}_#{number}"
  feedback_comments[key] = {
    comment_id: comment_id.to_s,
    processor_type: processor_type,
    number: number,
    posted_at: current_timestamp
  }
  save!
end

#tracked_commentsObject

Feedback tracking



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 243

def tracked_comments
  comments = []

  plans.each do |issue_number, data|
    next unless data[:comment_id]
    comments << {
      comment_id: data[:comment_id],
      processor_type: "plan",
      number: issue_number.to_i,
      posted_at: data[:posted_at]
    }
  end

  reviews.each do |pr_number, data|
    next unless data[:comment_id]
    comments << {
      comment_id: data[:comment_id],
      processor_type: "review",
      number: pr_number.to_i,
      posted_at: data[:timestamp]
    }
  end

  builds.each do |issue_number, data|
    next unless data[:comment_id]
    comments << {
      comment_id: data[:comment_id],
      processor_type: "build",
      number: issue_number.to_i,
      posted_at: data[:updated_at]
    }
  end

  feedback_comments.each do |_key, data|
    comments << {
      comment_id: data[:comment_id],
      processor_type: data[:processor_type],
      number: data[:number].to_i,
      posted_at: data[:posted_at]
    }
  end

  comments
end

#workstream_for_issue(issue_number) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aidp/database/repositories/watch_state_repository.rb', line 76

def workstream_for_issue(issue_number)
  data = build_status(issue_number)
  return nil if data.nil? || data.empty?

  {
    issue_number: issue_number.to_i,
    branch: data[:branch],
    workstream: data[:workstream],
    pr_url: data[:pr_url],
    status: data[:status]
  }
end