Module: Harnex::TerminalStatus

Defined in:
lib/harnex/terminal_status.rb

Class Method Summary collapse

Class Method Details

.blank_to_nil(value) ⇒ Object



220
221
222
223
# File 'lib/harnex/terminal_status.rb', line 220

def blank_to_nil(value)
  text = value.to_s
  text.empty? ? nil : text
end

.build_from_history(record, fallback_repo_root) ⇒ Object



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
199
200
201
202
203
# File 'lib/harnex/terminal_status.rb', line 171

def build_from_history(record, fallback_repo_root)
  status = record["status"].to_s
  state =
    case status
    when "completed"
      "completed"
    when "failed", "timeout", "killed"
      "failed"
    else
      "unknown"
    end
  task_complete = record["terminal_event"].to_s == "task_complete"
  task_failed = record["terminal_event"].to_s == "task_failed" || (state == "failed" && !task_complete)
  terminal = state != "unknown"
  {
    "id" => record["id"].to_s,
    "repo_root" => fallback_repo_root,
    "state" => state,
    "process_state" => Harnex.process_state_for(state, terminal: terminal),
    "terminal" => terminal,
    "task_complete" => task_complete,
    "task_failed" => task_failed,
    "done" => Harnex.work_done_for(state, task_complete: task_complete),
    "work_state" => Harnex.work_state_for(state, task_complete: task_complete),
    "outcome_class" => nil,
    "artifact_report_status" => nil,
    "exit" => history_exit(status),
    "exit_code" => nil,
    "started_at" => record["started_at"],
    "ended_at" => record["ended_at"],
    "source" => "dispatch_history"
  }
end

.build_from_summary(record, fallback_repo_root) ⇒ Object



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
# File 'lib/harnex/terminal_status.rb', line 132

def build_from_summary(record, fallback_repo_root)
  meta = record["meta"] || {}
  actual = record["actual"] || {}
  outcome = record["outcome"] || {}
  state = classify_summary_state(actual)
  task_complete = !!actual["task_complete"]
  task_failed = state == "failed" && !task_complete
  terminal = state != "unknown"
  {
    "id" => meta["id"].to_s,
    "repo_root" => meta["repo"] || fallback_repo_root,
    "state" => state,
    "process_state" => Harnex.process_state_for(state, terminal: terminal),
    "terminal" => terminal,
    "task_complete" => task_complete,
    "task_failed" => task_failed,
    "done" => Harnex.work_done_for(state, task_complete: task_complete),
    "work_state" => Harnex.work_state_for(state, task_complete: task_complete),
    "outcome_class" => blank_to_nil(outcome["class"]),
    "artifact_report_status" => blank_to_nil(outcome["report_status"]),
    "exit" => blank_to_nil(actual["exit"]),
    "exit_code" => actual["exit_code"],
    "started_at" => meta["started_at"],
    "ended_at" => meta["ended_at"],
    "source" => "dispatch_end"
  }
end

.classify_summary_state(actual) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/harnex/terminal_status.rb', line 160

def classify_summary_state(actual)
  exit = actual["exit"].to_s
  exit_code = actual["exit_code"]

  return "completed" if exit == "success"
  return "completed" if exit.empty? && exit_code == 0
  return "failed" unless exit.empty? && exit_code.nil?

  "unknown"
end

.history_exit(status) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/harnex/terminal_status.rb', line 205

def history_exit(status)
  case status
  when "completed"
    "success"
  when "timeout"
    "timeout"
  when "killed"
    "killed"
  when "failed"
    "failure"
  else
    nil
  end
end

.history_paths(repo_root) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/harnex/terminal_status.rb', line 51

def history_paths(repo_root)
  local_path = DispatchHistory.path_for(repo_root)
  return [local_path] if File.file?(local_path)

  global_path = DispatchHistory.global_path
  return [global_path] if File.file?(global_path)

  []
rescue StandardError
  []
end

.history_record?(record) ⇒ Boolean

Legacy thin end rows predate record_type.

Returns:

  • (Boolean)


100
101
102
# File 'lib/harnex/terminal_status.rb', line 100

def history_record?(record)
  record["schema_version"] == 1 && record.key?("status")
end

.history_time(record) ⇒ Object



122
123
124
# File 'lib/harnex/terminal_status.rb', line 122

def history_time(record)
  parse_time(record["ended_at"]) || parse_time(record["started_at"]) || Time.at(0)
end

.newer_history?(candidate, current) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/harnex/terminal_status.rb', line 111

def newer_history?(candidate, current)
  return false unless candidate
  return true unless current

  history_time(candidate) >= history_time(current)
end

.newer_summary?(candidate, current) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
# File 'lib/harnex/terminal_status.rb', line 104

def newer_summary?(candidate, current)
  return false unless candidate
  return true unless current

  summary_time(candidate) >= summary_time(current)
end

.parse_time(value) ⇒ Object



126
127
128
129
130
# File 'lib/harnex/terminal_status.rb', line 126

def parse_time(value)
  Time.iso8601(value.to_s)
rescue ArgumentError
  nil
end

.resolve(id:, repo_root: Dir.pwd) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/harnex/terminal_status.rb', line 8

def resolve(id:, repo_root: Dir.pwd)
  normalized_id = Harnex.normalize_id(id)
  root = File.expand_path(repo_root.to_s.empty? ? Dir.pwd : repo_root)

  latest_summary = nil
  latest_history = nil

  # The canonical dispatch stream is the only source. Rows a prior
  # release mirrored elsewhere are never read back, so a leftover mirror
  # file on disk cannot resolve status from stale data.
  history_paths(root).each do |path|
    summary, history = scan_dispatch_path(path, normalized_id)
    latest_summary = summary if newer_summary?(summary, latest_summary)
    latest_history = history if newer_history?(history, latest_history)
  end

  return build_from_summary(latest_summary, root) if latest_summary
  return build_from_history(latest_history, root) if latest_history

  nil
end

.scan_dispatch_path(path, id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/harnex/terminal_status.rb', line 63

def scan_dispatch_path(path, id)
  summary_record = nil
  history_record = nil

  File.foreach(path) do |line|
    record = JSON.parse(line)
    next unless record.is_a?(Hash)
    next if DispatchHistory.start_record?(record)

    # Branch on record_type first: a v2 end row carries both the thin
    # envelope and the rich sections, so it resolves as summary and
    # history in one shot — never double-counted via the legacy
    # duck-types below, which stay for pre-v2 files.
    if record["record_type"] == "dispatch_end"
      next unless record["id"].to_s == id

      history_record = record
      summary_record = record if summary_record?(record)
    elsif summary_record?(record) && record.dig("meta", "id").to_s == id
      summary_record = record
    elsif history_record?(record) && record["id"].to_s == id
      history_record = record
    end
  rescue JSON::ParserError
    next
  end

  [summary_record, history_record]
rescue Errno::ENOENT
  [nil, nil]
end

.summary_record?(record) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/harnex/terminal_status.rb', line 95

def summary_record?(record)
  record["meta"].is_a?(Hash) && record["actual"].is_a?(Hash)
end

.summary_time(record) ⇒ Object



118
119
120
# File 'lib/harnex/terminal_status.rb', line 118

def summary_time(record)
  parse_time(record.dig("meta", "ended_at")) || parse_time(record.dig("meta", "started_at")) || Time.at(0)
end

.unknown(id:, repo_root: Dir.pwd) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/harnex/terminal_status.rb', line 30

def unknown(id:, repo_root: Dir.pwd)
  {
    "id" => Harnex.normalize_id(id),
    "repo_root" => File.expand_path(repo_root.to_s.empty? ? Dir.pwd : repo_root),
    "state" => "unknown",
    "process_state" => "unknown",
    "terminal" => false,
    "task_complete" => false,
    "task_failed" => false,
    "done" => false,
    "work_state" => "unknown",
    "outcome_class" => nil,
    "artifact_report_status" => nil,
    "exit" => nil,
    "exit_code" => nil,
    "started_at" => nil,
    "ended_at" => nil,
    "source" => "none"
  }
end