Class: Ace::Hitl::Organisms::HitlManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/hitl/organisms/hitl_manager.rb

Defined Under Namespace

Classes: AmbiguousReferenceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir: nil, config: nil, scope_resolver: nil, resume_dispatcher: nil) ⇒ HitlManager

Returns a new instance of HitlManager.



32
33
34
35
36
37
38
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 32

def initialize(root_dir: nil, config: nil, scope_resolver: nil, resume_dispatcher: nil)
  @config = config || load_config
  @configured_root_setting = @config.dig("hitl", "root_dir") || Molecules::HitlConfigLoader::DEFAULT_ROOT_DIR
  @root_dir = root_dir || resolve_root_dir
  @scope_resolver = scope_resolver || Molecules::WorktreeScopeResolver.new
  @resume_dispatcher = resume_dispatcher || Molecules::ResumeDispatcher.new
end

Instance Attribute Details

#last_folder_counts ⇒ Object (readonly)

Returns the value of attribute last_folder_counts.



30
31
32
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 30

def last_folder_counts
  @last_folder_counts
end

#last_list_total ⇒ Object (readonly)

Returns the value of attribute last_list_total.



30
31
32
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 30

def last_list_total
  @last_list_total
end

#root_dir ⇒ Object (readonly)

Returns the value of attribute root_dir.



30
31
32
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 30

def root_dir
  @root_dir
end

Instance Method Details

#create(title, **options) ⇒ Object



40
41
42
43
44
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 40

def create(title, **options)
  ensure_root_dir
  creator = Molecules::HitlCreator.new(root_dir: @root_dir, config: @config)
  creator.create(title, **options)
end

#dispatch_resume(ref, scope: nil, now: Time.now.utc) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 200

def dispatch_resume(ref, scope: nil, now: Time.now.utc)
  current = show(ref, scope: scope)
  return {status: :not_found} unless current

  event = current[:event]
  answer = event.answer.to_s
  return {status: :no_answer, event: event} if answer.strip.empty?

  if waiter_active?(event, now: now)
    return {status: :waiter_active, event: event}
  end

  result = @resume_dispatcher.dispatch(event: event, answer: answer, now: now)
  unless result.success?
    update(event.id,
      set: {
        "resume_dispatch_status" => "failed",
        "resume_dispatch_attempted_at" => now.iso8601,
        "resume_dispatch_error" => result.error
      },
      scope: scope
    )
    return {status: :failed, event: event, error: result.error}
  end

  update(event.id,
    set: {
      "resume_dispatch_status" => "dispatched",
      "resume_dispatch_attempted_at" => now.iso8601,
      "resumed_at" => now.iso8601,
      "resumed_by" => result.details,
      "waiter_state" => "resumed",
      "resume_dispatch_error" => nil
    },
    scope: scope
  )

  note = "Work resumed at #{now.iso8601} via #{result.mode} (#{result.details})."
  append_resume_note(event.id, note, scope: scope)
  archived = update(event.id, move_to: "archive", scope: scope)

  {status: :dispatched, event: archived, mode: result.mode, details: result.details}
end

#list(status: nil, kind: nil, in_folder: "next", tags: [], scope: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 72

def list(status: nil, kind: nil, in_folder: "next", tags: [], scope: nil)
  effective_scope = @scope_resolver.effective_scope(scope)
  scan_results = scan_results_for_scope(effective_scope, in_folder: in_folder)
  events = load_events(scan_results)

  events = events.select { |event| event.status == status } if status
  events = events.select { |event| event.kind == kind } if kind
  events = filter_by_tags(events, tags) if tags.any?

  events
end

#show(ref, scope: nil) ⇒ Object



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
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 46

def show(ref, scope: nil)
  effective_scope = @scope_resolver.effective_scope(scope)
  roots = hitl_roots_for_scope(effective_scope)
  current_root = current_hitl_root

  resolved = resolve_from_roots(ref, roots, strict_ambiguity: true)

  fallback_used = false
  if resolved.nil? && scope.nil? && effective_scope == "current"
    resolved = resolve_from_roots(ref, hitl_roots_for_scope("all"), strict_ambiguity: true)
    effective_scope = "all" if resolved
    fallback_used = !resolved.nil?
  end

  return nil unless resolved

  {
    event: resolved[:event],
    effective_scope: effective_scope,
    fallback_used: fallback_used,
    resolved_hitl_root: resolved[:hitl_root],
    resolved_worktree_root: resolved[:worktree_root],
    resolved_outside_current: !current_root.nil? && resolved[:hitl_root] != current_root
  }
end

#update(ref, set: {}, add: {}, remove: {}, move_to: nil, answer: nil, scope: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 84

def update(ref, set: {}, add: {}, remove: {}, move_to: nil, answer: nil, scope: nil)
  resolved = resolve_for_mutation(ref, scope: scope)
  return nil unless resolved

  event = resolved[:event]
  hitl_root = resolved[:hitl_root] || @root_dir

  has_field_updates = [set, add, remove].any? { |h| h && !h.empty? }
  if has_field_updates && !answer.nil?
    apply_field_and_answer_updates(event.file_path, set: set, add: add, remove: remove, answer: answer)
  elsif has_field_updates
    Ace::Support::Items::Molecules::FieldUpdater.update(
      event.file_path,
      set: set,
      add: add,
      remove: remove
    )
  end

  apply_answer_update(event.file_path, answer) unless answer.nil?

  current_path = event.path
  current_special = event.special_folder
  if move_to
    mover = Ace::Support::Items::Molecules::FolderMover.new(hitl_root)
    new_path = if Ace::Support::Items::Atoms::SpecialFolderDetector.move_to_root?(move_to)
      mover.move_to_root(event)
    else
      mover.move(event, to: move_to, date: parse_archive_date(event))
    end
    current_path = new_path
    current_special = Ace::Support::Items::Atoms::SpecialFolderDetector.detect_in_path(
      new_path,
      root: hitl_root
    )
  end

  loader = Molecules::HitlLoader.new
  loader.load(current_path, id: event.id, special_folder: current_special)
end

#wait_for_answer(ref, scope: nil, poll_every: 600, timeout: 14_400, waiter: {}, now_proc: nil, sleeper: nil, lab_observer: nil) ⇒ Object



125
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ace/hitl/organisms/hitl_manager.rb', line 125

def wait_for_answer(ref, scope: nil, poll_every: 600, timeout: 14_400, waiter: {}, now_proc: nil, sleeper: nil, lab_observer: nil)
  poll_every = normalize_poll_seconds(poll_every)
  timeout = normalize_timeout_seconds(timeout)
  now_proc ||= -> { Time.now.utc }
  sleeper ||= ->(seconds) { sleep(seconds) }

  started_at = now_proc.call
  deadline = started_at + timeout
  waiter_session_id = waiter[:session_id].to_s.strip
  waiter_provider = waiter[:provider].to_s.strip

  loop do
    current = show(ref, scope: scope)
    return {status: :not_found} unless current

    event = current[:event]
    now = now_proc.call
    refresh_waiter_lease(
      event,
      now: now,
      deadline: deadline,
      poll_every: poll_every,
      waiter_session_id: waiter_session_id,
      waiter_provider: waiter_provider,
      scope: scope
    )

    observer = lab_observer_for(lab_observer)
    lab_snapshot = observe_lab_state(event, observer: observer, scope: scope)
    lab_state = observer.effective_state(lab_snapshot)

    # An effect-declaring request keeps waiting until the callback
    # verdict (callback-ok / callback-escalated) is visible; answer
    # delivery alone never ends the wait. When the projection is
    # unreadable there is nothing to hold on, so the wait falls
    # back to the event/lifecycle behavior (timeout stays the
    # backstop).
    hold_for_effect = effect_declared?(event) && lab_snapshot &&
      !observer.terminal?(lab_snapshot, effect_declared: true)

    if event.answered? && !hold_for_effect
      update(event.id,
        set: {
          "waiter_state" => "answered",
          "waiter_last_seen_at" => now.iso8601
        },
        scope: scope
      )
      refreshed = show(event.id, scope: scope)&.dig(:event) || event
      return {status: :answered, event: refreshed, lab_state: lab_state}
    end

    if observer.terminal?(lab_snapshot, effect_declared: effect_declared?(event))
      update(
        event.id,
        set: {
          "waiter_state" => "lab_delivered",
          "waiter_last_seen_at" => now.iso8601,
          "lab_request_state" => lab_state
        },
        scope: scope
      )
      return {status: :lab_delivered, event: event, lab_state: lab_state}
    end

    if now >= deadline
      update(event.id, set: {"waiter_state" => "timed_out"}, scope: scope)
      return {status: :timeout, event: event, lab_state: lab_state}
    end

    sleep_seconds = [poll_every, (deadline - now).ceil].min
    sleeper.call(sleep_seconds) if sleep_seconds.positive?
  end
end