Module: Harnex::ArtifactReport

Defined in:
lib/harnex/artifact_report.rb

Defined Under Namespace

Classes: ValidationResult

Constant Summary collapse

SCHEMA =
"harnex.artifact_report.v1"
MAX_BYTES =
256 * 1024
MAX_ARTIFACTS =
50
MAX_COMMANDS =
50
MAX_CANONICAL_ARTIFACTS =
50
MAX_EVIDENCE_ITEMS =
20
MAX_STRING_LENGTH =
2_000
MAX_DIAGNOSTICS =
100
MAX_CHANGED_PATHS =
200
RECEIPT_VERSION =
1
CLAIM_SEVERITIES =
%w[P1 P2 P3].freeze
COMMAND_OBSERVATION_STATUSES =
%w[observed unsupported].freeze
GIT_OBSERVATION_STATUSES =
%w[observed unavailable].freeze
USAGE_STATUSES =
%w[observed estimated unsupported missing zero].freeze
REPORT_STATUSES =
%w[in_progress pass fail blocked unknown].freeze
VALIDATION_STATUSES =
%w[in_progress pass fail not_run unknown].freeze
OUTCOME_STATUSES =
%w[accepted rejected no_change unknown].freeze
ACCEPTED_OUTCOME_STATUSES =
%w[accepted no_change].freeze

Class Method Summary collapse

Class Method Details

.accepted_final?(result) ⇒ Boolean

Returns:

  • (Boolean)


361
362
363
# File 'lib/harnex/artifact_report.rb', line 361

def accepted_final?(result)
  result.ok && ACCEPTED_OUTCOME_STATUSES.include?(outcome_status(result))
end

.boolean?(value) ⇒ Boolean

Returns:

  • (Boolean)


1134
1135
1136
# File 'lib/harnex/artifact_report.rb', line 1134

def boolean?(value)
  value == true || value == false
end

.boolean_or_nil(value) ⇒ Object



1130
1131
1132
# File 'lib/harnex/artifact_report.rb', line 1130

def boolean_or_nil(value)
  value if boolean?(value)
end

.bounded_string(value) ⇒ Object



1082
1083
1084
1085
1086
1087
# File 'lib/harnex/artifact_report.rb', line 1082

def bounded_string(value)
  text = value.to_s
  return text if text.length <= MAX_STRING_LENGTH

  text[0, MAX_STRING_LENGTH]
end

.bounded_string_or_nil(value) ⇒ Object



1076
1077
1078
1079
1080
# File 'lib/harnex/artifact_report.rb', line 1076

def bounded_string_or_nil(value)
  return nil if value.nil?

  bounded_string(value)
end

.build_observed(id:, session_id:, generated_at:, successful:, outcome_status:, outcome_summary:, git:, commands:, turn:, usage:, claims:, command_observation:) ⇒ Object



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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/harnex/artifact_report.rb', line 255

def build_observed(
  id:, session_id:, generated_at:, successful:, outcome_status:,
  outcome_summary:, git:, commands:, turn:, usage:, claims:,
  command_observation:
)
  observed_commands = compact_commands(commands).select do |entry|
    non_empty_string?(entry["cmd"]) && entry["exit_code"].is_a?(Integer)
  end
  git_payload = compact_observed_git(git)
  turn_payload = compact_observed_turn(turn)
  usage_payload = compact_observed_usage(usage)
  claims_payload = compact_claims(claims)
  validation_status = if observed_commands.empty?
                        "not_run"
                      elsif observed_commands.all? { |entry| entry["exit_code"].zero? }
                        "pass"
                      else
                        "fail"
                      end
  start_sha = git_payload["start_sha"].to_s
  end_sha = git_payload["end_sha"].to_s
  commit_sha = end_sha unless start_sha.empty? || end_sha.empty? || start_sha == end_sha

  report = {
    "schema" => SCHEMA,
    "status" => successful ? "pass" : "fail",
    "canonical_artifacts" => [],
    "outcome" => {
      "status" => outcome_status,
      "summary" => bounded_string(outcome_summary),
      "commit_sha" => commit_sha
    }.compact,
    "validation" => {
      "status" => validation_status,
      "commands" => observed_commands,
      "final_reported" => true
    },
    "artifacts" => [],
    "receipt" => {
      "version" => RECEIPT_VERSION,
      "author" => "harnex",
      "generated_at" => generated_at.respond_to?(:iso8601) ? generated_at.iso8601 : generated_at.to_s,
      "id" => bounded_string(id),
      "session_id" => bounded_string(session_id)
    },
    "observed" => {
      "git" => git_payload,
      "commands" => observed_commands,
      "command_observation" => command_observation.to_s,
      "turn" => turn_payload,
      "usage" => usage_payload
    }
  }
  report["claims"] = claims_payload unless claims_payload.empty?
  fit_observed_report!(report)
end

.build_payload(path, bytes:, sha256:, report:) ⇒ Object



832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/harnex/artifact_report.rb', line 832

def build_payload(path, bytes:, sha256:, report:)
  artifacts = compact_artifacts(report["artifacts"])
  validation = compact_validation(report["validation"])
  outcome = compact_outcome(report["outcome"])
  receipt = compact_receipt(report["receipt"])
  observed = compact_observed(report["observed"])
  claims = compact_claims(report["claims"])
  payload = {
    "artifact_report" => (
      path,
      bytes: bytes,
      sha256: sha256,
      ingest_status: "ok",
      schema: SCHEMA
    ).merge(
      "report_status" => bounded_string_or_nil(report["status"]),
      "canonical_artifacts" => string_array(report["canonical_artifacts"], max_items: MAX_CANONICAL_ARTIFACTS),
      "artifact_count" => artifacts.length,
      "author" => receipt&.dig("author")
    )
  }
  payload["validation"] = validation if validation
  payload["artifacts"] = artifacts unless artifacts.empty?
  payload["outcome"] = outcome if outcome
  payload["receipt"] = receipt if receipt
  payload["observed"] = observed if observed
  payload["claims"] = claims unless claims.empty?
  payload
end

.claims_path(report_path) ⇒ Object



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

def claims_path(report_path)
  "#{File.expand_path(report_path.to_s)}.claims.json"
end

.compact_artifacts(value) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/harnex/artifact_report.rb', line 1032

def compact_artifacts(value)
  Array(value).first(MAX_ARTIFACTS).filter_map do |entry|
    next unless entry.is_a?(Hash)

    compact = {
      "type" => bounded_string_or_nil(entry["type"]),
      "summary" => bounded_string_or_nil(entry["summary"]),
      "evidence" => string_array(entry["evidence"], max_items: MAX_EVIDENCE_ITEMS),
      "confidence" => finite_float_or_nil(entry["confidence"]),
      "canonical_ref" => bounded_string_or_nil(entry["canonical_ref"])
    }
    compact.delete_if { |_key, item| item.nil? || item == [] }
    compact.empty? ? nil : compact
  end
end

.compact_claims(value) ⇒ Object



972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/harnex/artifact_report.rb', line 972

def compact_claims(value)
  return {} unless value.is_a?(Hash)

  payload = {
    "summary" => bounded_string_or_nil(hash_value(value, "summary")),
    "verdict" => bounded_string_or_nil(hash_value(value, "verdict"))
  }
  findings = hash_value(value, "findings")
  if findings.is_a?(Hash)
    counts = CLAIM_SEVERITIES.each_with_object({}) do |severity, result|
      count = non_negative_integer_or_nil(hash_value(findings, severity))
      result[severity] = count unless count.nil?
    end
    payload["findings"] = counts unless counts.empty?
  end
  payload.delete_if { |_key, item| item.nil? || item == "" }
  payload
end

.compact_commands(value) ⇒ Object



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/harnex/artifact_report.rb', line 1002

def compact_commands(value)
  Array(value).first(MAX_COMMANDS).filter_map do |entry|
    next unless entry.is_a?(Hash)

    compact = {
      "cmd" => bounded_string_or_nil(entry["cmd"]),
      "exit_code" => integer_or_nil(entry["exit_code"]),
      "status" => bounded_string_or_nil(entry["status"]),
      "duration_s" => finite_float_or_nil(entry["duration_s"])
    }
    compact.delete_if { |_key, item| item.nil? }
    compact.empty? ? nil : compact
  end
end

.compact_observed(value) ⇒ Object



904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/harnex/artifact_report.rb', line 904

def compact_observed(value)
  return nil unless value.is_a?(Hash)

  payload = {
    "git" => compact_observed_git(value["git"]),
    "commands" => compact_commands(value["commands"]),
    "commands_truncated" => boolean_or_nil(value["commands_truncated"]),
    "command_observation" => bounded_string_or_nil(value["command_observation"]),
    "turn" => compact_observed_turn(value["turn"]),
    "usage" => compact_observed_usage(value["usage"])
  }
  payload.delete_if { |_key, item| item.nil? }
  payload.empty? ? nil : payload
end

.compact_observed_git(value) ⇒ Object



919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/harnex/artifact_report.rb', line 919

def compact_observed_git(value)
  value = {} unless value.is_a?(Hash)
  status = hash_value(value, "status").to_s
  status = "observed" if status.empty? && !hash_value(value, "start_sha").to_s.empty?
  status = "unavailable" unless GIT_OBSERVATION_STATUSES.include?(status)
  payload = {
    "status" => status,
    "start_sha" => bounded_string_or_nil(hash_value(value, "start_sha")),
    "end_sha" => bounded_string_or_nil(hash_value(value, "end_sha")),
    "branch" => bounded_string_or_nil(hash_value(value, "branch")),
    "changed_paths" => string_array(hash_value(value, "changed_paths"), max_items: MAX_CHANGED_PATHS),
    "loc_added" => non_negative_integer_or_nil(hash_value(value, "loc_added")),
    "loc_removed" => non_negative_integer_or_nil(hash_value(value, "loc_removed")),
    "files_changed" => non_negative_integer_or_nil(hash_value(value, "files_changed")),
    "commits" => non_negative_integer_or_nil(hash_value(value, "commits")),
    "start_dirty" => boolean_or_nil(hash_value(value, "start_dirty")),
    "end_dirty" => boolean_or_nil(hash_value(value, "end_dirty")),
    "worktree_changed" => boolean_or_nil(hash_value(value, "worktree_changed")),
    "changed_paths_truncated" => boolean_or_nil(hash_value(value, "changed_paths_truncated"))
  }
  payload.delete_if { |key, item| item.nil? || (key == "changed_paths" && status == "unavailable") }
  payload
end

.compact_observed_turn(value) ⇒ Object



943
944
945
946
947
948
949
950
951
952
953
954
955
956
# File 'lib/harnex/artifact_report.rb', line 943

def compact_observed_turn(value)
  value = {} unless value.is_a?(Hash)
  payload = {
    "status" => bounded_string_or_nil(hash_value(value, "status")),
    "outcome_class" => bounded_string_or_nil(hash_value(value, "outcome_class")),
    "task_complete" => !!hash_value(value, "task_complete"),
    "task_failed" => !!hash_value(value, "task_failed"),
    "accepted" => !!hash_value(value, "accepted"),
    "exit_code" => integer_or_nil(hash_value(value, "exit_code")),
    "signal" => integer_or_nil(hash_value(value, "signal"))
  }
  payload.delete_if { |_key, item| item.nil? }
  payload
end

.compact_observed_usage(value) ⇒ Object



958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/harnex/artifact_report.rb', line 958

def compact_observed_usage(value)
  value = {} unless value.is_a?(Hash)
  payload = {}
  %w[status cost_source cost_price_as_of].each do |key|
    payload[key] = bounded_string_or_nil(hash_value(value, key))
  end
  payload["cost_usd"] = finite_float_or_nil(hash_value(value, "cost_usd"))
  %w[input_tokens output_tokens cached_input_tokens reasoning_tokens total_tokens].each do |key|
    payload[key] = non_negative_integer_or_nil(hash_value(value, key))
  end
  payload.delete_if { |_key, item| item.nil? }
  payload
end

.compact_outcome(value) ⇒ Object



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/harnex/artifact_report.rb', line 1017

def compact_outcome(value)
  return nil unless value.is_a?(Hash)

  status = value["status"].to_s
  return nil unless OUTCOME_STATUSES.include?(status)

  payload = {
    "status" => status,
    "summary" => bounded_string_or_nil(value["summary"]),
    "commit_sha" => bounded_string_or_nil(value["commit_sha"])
  }
  payload.delete_if { |_key, item| item.nil? }
  payload
end

.compact_receipt(value) ⇒ Object



890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'lib/harnex/artifact_report.rb', line 890

def compact_receipt(value)
  return nil unless value.is_a?(Hash)

  payload = {
    "version" => integer_or_nil(value["version"]),
    "author" => bounded_string_or_nil(value["author"]),
    "generated_at" => bounded_string_or_nil(value["generated_at"]),
    "id" => bounded_string_or_nil(value["id"]),
    "session_id" => bounded_string_or_nil(value["session_id"])
  }
  payload.delete_if { |_key, item| item.nil? }
  payload.empty? ? nil : payload
end

.compact_validation(value) ⇒ Object



991
992
993
994
995
996
997
998
999
1000
# File 'lib/harnex/artifact_report.rb', line 991

def compact_validation(value)
  return nil unless value.is_a?(Hash)

  payload = {}
  payload["status"] = bounded_string_or_nil(value["status"])
  payload["commands"] = compact_commands(value["commands"])
  payload["final_reported"] = !!value["final_reported"] if value.key?("final_reported")
  payload.delete_if { |_key, item| item.nil? || item == [] }
  payload.empty? ? nil : payload
end

.default_path(repo_root:, id:, session_id:) ⇒ Object

Default receipts live outside the checkout so proof generation cannot become part of the Git delta it is recording. An explicit --artifact-report path still wins for callers that need a stable path.



211
212
213
214
215
216
217
218
219
# File 'lib/harnex/artifact_report.rb', line 211

def default_path(repo_root:, id:, session_id:)
  slug = Harnex.id_key(id)
  slug = Digest::SHA256.hexdigest(id.to_s)[0, 12] if slug.empty?
  File.join(
    Harnex::STATE_DIR,
    "receipts",
    "#{Harnex.repo_key(repo_root)}-#{slug}-#{session_id}.json"
  )
end

.diagnostic(code, path, message) ⇒ Object



1061
1062
1063
1064
1065
1066
1067
# File 'lib/harnex/artifact_report.rb', line 1061

def diagnostic(code, path, message)
  {
    "code" => code,
    "path" => path,
    "message" => bounded_string(message)
  }
end

.extract_claims(path) ⇒ Object

Claims are deliberately advisory. Malformed, stale, or oversized claims are ignored by Session and can never invalidate observed proof.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/harnex/artifact_report.rb', line 227

def extract_claims(path)
  claims_path = File.expand_path(path.to_s)
  return {} unless File.file?(claims_path)
  return {} if File.size(claims_path) > MAX_BYTES

  document = JSON.parse(File.read(claims_path, mode: "rb"))
  return {} unless document.is_a?(Hash)

  source = document["claims"].is_a?(Hash) ? document["claims"] : document
  claims = compact_claims(source)

  # Compatibility bridge for old worker-authored v1 reports. Their
  # outcome is retained only as an untrusted verdict/summary claim; it no
  # longer participates in completion acceptance.
  if claims.empty? && document["schema"] == SCHEMA
    outcome = document["outcome"]
    if outcome.is_a?(Hash)
      claims = compact_claims(
        "summary" => outcome["summary"],
        "verdict" => outcome["status"]
      )
    end
  end
  claims
rescue StandardError
  {}
end

.file_sha256(path) ⇒ Object



1138
1139
1140
1141
1142
1143
1144
1145
# File 'lib/harnex/artifact_report.rb', line 1138

def file_sha256(path)
  digest = Digest::SHA256.new
  File.open(path, "rb") do |file|
    buffer = +""
    digest.update(buffer) while file.read(16 * 1024, buffer)
  end
  digest.hexdigest
end

.fingerprint(path) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/harnex/artifact_report.rb', line 365

def fingerprint(path)
  report_path = File.expand_path(path.to_s)
  return nil unless File.file?(report_path)

  stat = File.stat(report_path)
  {
    "bytes" => stat.size,
    "sha256" => file_sha256(report_path),
    "mtime_ns" => (stat.mtime.to_r * 1_000_000_000).to_i,
    "ctime_ns" => (stat.ctime.to_r * 1_000_000_000).to_i,
    "inode" => stat.ino
  }
rescue StandardError
  nil
end

.finite_float_or_nil(value) ⇒ Object



1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/harnex/artifact_report.rb', line 1109

def finite_float_or_nil(value)
  return nil if value.nil?

  number = Float(value)
  number.finite? ? number : nil
rescue ArgumentError, TypeError
  nil
end

.finite_non_negative_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


1122
1123
1124
# File 'lib/harnex/artifact_report.rb', line 1122

def finite_non_negative_number?(value)
  finite_number?(value) && value >= 0
end

.finite_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


1118
1119
1120
# File 'lib/harnex/artifact_report.rb', line 1118

def finite_number?(value)
  value.is_a?(Numeric) && value.finite?
end

.fit_observed_report!(report) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/harnex/artifact_report.rb', line 312

def fit_observed_report!(report)
  loop do
    return report if JSON.pretty_generate(report).bytesize + 1 <= MAX_BYTES

    commands = report.dig("observed", "commands")
    if commands.is_a?(Array) && !commands.empty?
      commands.pop
      report.dig("observed")["commands_truncated"] = true
      next
    end

    paths = report.dig("observed", "git", "changed_paths")
    if paths.is_a?(Array) && !paths.empty?
      paths.pop
      report.dig("observed", "git")["changed_paths_truncated"] = true
      next
    end

    raise ArgumentError, "generated artifact receipt exceeds the #{MAX_BYTES}-byte limit"
  end
end

.harness_receipt?(report) ⇒ Boolean

Returns:

  • (Boolean)


731
732
733
# File 'lib/harnex/artifact_report.rb', line 731

def harness_receipt?(report)
  report.dig("receipt", "author") == "harnex"
end

.hash_value(hash, key) ⇒ Object



1102
1103
1104
1105
1106
1107
# File 'lib/harnex/artifact_report.rb', line 1102

def hash_value(hash, key)
  return nil unless hash.is_a?(Hash)
  return hash[key] if hash.key?(key)

  hash[key.to_sym]
end

.ingest(path) ⇒ Object



119
120
121
122
123
124
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
# File 'lib/harnex/artifact_report.rb', line 119

def ingest(path)
  result = validate(path)
  case result.status
  when "missing"
    missing(result.path)
  when "oversized"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "oversized",
      warning: "artifact report is #{result.bytes} bytes; max is #{MAX_BYTES} bytes",
      diagnostics: result.diagnostics
    )
  when "malformed"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "malformed",
      warning: "malformed artifact report JSON",
      diagnostics: result.diagnostics
    )
  when "unsupported_schema"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "unsupported_schema",
      schema: result.schema,
      warning: "unsupported artifact report schema; expected #{SCHEMA}",
      diagnostics: result.diagnostics
    )
  when "error"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "error",
      warning: "artifact report ingest failed",
      diagnostics: result.diagnostics
    )
  else
    payload = build_payload(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      report: result.report
    )
    unless result.ok
       = payload.fetch("artifact_report")
      ["ingest_status"] = "invalid"
      ["warning"] = "artifact report does not conform to #{SCHEMA}"
      ["diagnostics"] = result.diagnostics
    end
    payload
  end
end

.initialize_file(path, force: false) ⇒ Object

Raises:

  • (ArgumentError)


178
179
180
181
182
183
184
185
186
187
188
# File 'lib/harnex/artifact_report.rb', line 178

def initialize_file(path, force: false)
  report_path = File.expand_path(path.to_s)
  raise ArgumentError, "artifact report path is required" if path.to_s.strip.empty?
  if File.exist?(report_path) && !force
    raise ArgumentError, "artifact report already exists: #{report_path} (use --force to replace it)"
  end

  FileUtils.mkdir_p(File.dirname(report_path))
  File.write(report_path, JSON.pretty_generate(template) + "\n")
  report_path
end

.integer_or_nil(value) ⇒ Object



1089
1090
1091
1092
1093
1094
1095
# File 'lib/harnex/artifact_report.rb', line 1089

def integer_or_nil(value)
  return nil if value.nil?

  Integer(value)
rescue ArgumentError, TypeError
  nil
end

.metadata(path, bytes:, sha256:, ingest_status:, schema: nil) ⇒ Object



880
881
882
883
884
885
886
887
888
# File 'lib/harnex/artifact_report.rb', line 880

def (path, bytes:, sha256:, ingest_status:, schema: nil)
  {
    "path" => path.to_s,
    "bytes" => bytes,
    "sha256" => sha256,
    "ingest_status" => ingest_status,
    "schema" => schema
  }
end

.missing(path) ⇒ Object



862
863
864
865
866
867
868
869
870
871
# File 'lib/harnex/artifact_report.rb', line 862

def missing(path)
  warning(
    path,
    bytes: nil,
    sha256: nil,
    ingest_status: "missing",
    warning: "artifact report not found",
    diagnostics: [diagnostic("report_missing", "$", "artifact report file does not exist")]
  )
end

.non_empty_string?(value) ⇒ Boolean

Returns:

  • (Boolean)


1126
1127
1128
# File 'lib/harnex/artifact_report.rb', line 1126

def non_empty_string?(value)
  value.is_a?(String) && !value.empty? && value.length <= MAX_STRING_LENGTH
end

.non_negative_integer_or_nil(value) ⇒ Object



1097
1098
1099
1100
# File 'lib/harnex/artifact_report.rb', line 1097

def non_negative_integer_or_nil(value)
  integer = integer_or_nil(value)
  integer if integer && integer >= 0
end

.outcome_status(result) ⇒ Object



381
382
383
384
385
386
387
# File 'lib/harnex/artifact_report.rb', line 381

def outcome_status(result)
  report = result.respond_to?(:report) ? result.report : result
  return nil unless report.is_a?(Hash)

  outcome = report["outcome"]
  outcome.is_a?(Hash) ? outcome["status"] : nil
end

.safe_file_sha256(path) ⇒ Object



1153
1154
1155
1156
1157
# File 'lib/harnex/artifact_report.rb', line 1153

def safe_file_sha256(path)
  file_sha256(path) if File.file?(path)
rescue StandardError
  nil
end

.safe_file_size(path) ⇒ Object



1147
1148
1149
1150
1151
# File 'lib/harnex/artifact_report.rb', line 1147

def safe_file_size(path)
  File.size(path) if File.file?(path)
rescue StandardError
  nil
end

.string_array(value, max_items:) ⇒ Object



1069
1070
1071
1072
1073
1074
# File 'lib/harnex/artifact_report.rb', line 1069

def string_array(value, max_items:)
  Array(value).first(max_items).filter_map do |item|
    text = bounded_string_or_nil(item)
    text unless text.nil? || text.empty?
  end
end

.templateObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/harnex/artifact_report.rb', line 190

def template
  {
    "schema" => SCHEMA,
    "status" => "in_progress",
    "canonical_artifacts" => [],
    "outcome" => {
      "status" => "unknown",
      "summary" => ""
    },
    "validation" => {
      "status" => "not_run",
      "commands" => [],
      "final_reported" => false
    },
    "artifacts" => []
  }
end

.validate(path, final: false) ⇒ Object



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
81
82
83
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
# File 'lib/harnex/artifact_report.rb', line 48

def validate(path, final: false)
  report_path = File.expand_path(path.to_s)
  return validation_failure(
    report_path,
    status: "missing",
    diagnostics: [diagnostic("report_missing", "$", "artifact report file does not exist")]
  ) unless File.file?(report_path)

  bytes = File.size(report_path)
  sha256 = file_sha256(report_path)
  if bytes > MAX_BYTES
    return validation_failure(
      report_path,
      status: "oversized",
      bytes: bytes,
      sha256: sha256,
      diagnostics: [
        diagnostic("report_oversized", "$", "artifact report exceeds the #{MAX_BYTES}-byte limit")
      ]
    )
  end

  parsed = JSON.parse(File.read(report_path, mode: "rb"))
  unless parsed.is_a?(Hash)
    return validation_failure(
      report_path,
      status: "malformed",
      bytes: bytes,
      sha256: sha256,
      diagnostics: [diagnostic("object_required", "$", "artifact report must be a JSON object")]
    )
  end

  schema = parsed["schema"].is_a?(String) ? bounded_string(parsed["schema"]) : nil
  diagnostics = validate_document(parsed, final: final)
  status = if parsed["schema"] != SCHEMA
             "unsupported_schema"
           elsif diagnostics.empty?
             "valid"
           else
             "invalid"
           end

  ValidationResult.new(
    ok: diagnostics.empty?,
    status: status,
    path: report_path,
    bytes: bytes,
    sha256: sha256,
    schema: schema,
    diagnostics: diagnostics,
    report: parsed
  )
rescue JSON::ParserError
  validation_failure(
    report_path,
    status: "malformed",
    bytes: safe_file_size(report_path),
    sha256: safe_file_sha256(report_path),
    diagnostics: [diagnostic("json_invalid", "$", "artifact report is not valid JSON")]
  )
rescue StandardError
  validation_failure(
    report_path,
    status: "error",
    bytes: safe_file_size(report_path),
    sha256: safe_file_sha256(report_path),
    diagnostics: [diagnostic("read_error", "$", "artifact report could not be read")]
  )
end

.validate_artifacts(value, diagnostics) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/harnex/artifact_report.rb', line 512

def validate_artifacts(value, diagnostics)
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", "$.artifacts", "artifacts must be an array")
    return
  end
  if value.length > MAX_ARTIFACTS
    diagnostics << diagnostic(
      "too_many_items",
      "$.artifacts",
      "artifacts may contain at most #{MAX_ARTIFACTS} entries"
    )
  end

  value.first(MAX_ARTIFACTS).each_with_index do |entry, index|
    path = "$.artifacts[#{index}]"
    unless entry.is_a?(Hash)
      diagnostics << diagnostic("object_required", path, "artifact must be an object")
      next
    end

    validate_required_string(entry, "type", "#{path}.type", diagnostics)
    validate_required_string(entry, "summary", "#{path}.summary", diagnostics)
    validate_string_array_value(
      entry["evidence"],
      "#{path}.evidence",
      max_items: MAX_EVIDENCE_ITEMS,
      diagnostics: diagnostics,
      required: true
    )
    confidence = entry["confidence"]
    unless finite_number?(confidence) && confidence >= 0.0 && confidence <= 1.0
      diagnostics << diagnostic(
        "range",
        "#{path}.confidence",
        "artifact confidence must be a finite number from 0 through 1"
      )
    end
    validate_optional_string(entry, "canonical_ref", "#{path}.canonical_ref", diagnostics)
  end
end

.validate_commands(value, diagnostics, base_path: "$.validation.commands") ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/harnex/artifact_report.rb', line 473

def validate_commands(value, diagnostics, base_path: "$.validation.commands")
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", base_path, "#{base_path.delete_prefix('$.')} must be an array")
    return
  end
  if value.length > MAX_COMMANDS
    diagnostics << diagnostic(
      "too_many_items",
      base_path,
      "#{base_path.delete_prefix('$.')} may contain at most #{MAX_COMMANDS} entries"
    )
  end

  value.first(MAX_COMMANDS).each_with_index do |entry, index|
    path = "#{base_path}[#{index}]"
    unless entry.is_a?(Hash)
      diagnostics << diagnostic("object_required", path, "validation command must be an object")
      next
    end

    validate_required_string(entry, "cmd", "#{path}.cmd", diagnostics)
    unless entry["exit_code"].is_a?(Integer)
      diagnostics << diagnostic(
        "integer_required",
        "#{path}.exit_code",
        "validation command exit_code must be an integer"
      )
    end
    validate_optional_string(entry, "status", "#{path}.status", diagnostics)
    if entry.key?("duration_s") && !finite_non_negative_number?(entry["duration_s"])
      diagnostics << diagnostic(
        "number_required",
        "#{path}.duration_s",
        "validation command duration_s must be a finite non-negative number"
      )
    end
  end
end

.validate_document(report, final: false) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/harnex/artifact_report.rb', line 389

def validate_document(report, final: false)
  diagnostics = []
  validate_schema(report, diagnostics)
  validate_optional_enum(report, "status", REPORT_STATUSES, diagnostics)
  validate_string_array_field(
    report,
    "canonical_artifacts",
    max_items: MAX_CANONICAL_ARTIFACTS,
    diagnostics: diagnostics
  )
  validate_outcome(report["outcome"], diagnostics) if report.key?("outcome")
  validate_validation(report["validation"], diagnostics) if report.key?("validation")
  validate_artifacts(report["artifacts"], diagnostics) if report.key?("artifacts")
  validate_receipt(report["receipt"], diagnostics) if report.key?("receipt")
  validate_observed(report["observed"], diagnostics) if report.key?("observed")
  # `claims` is intentionally not part of validity. Session emits only a
  # bounded sanitized subset, while malformed worker claims are ignored.
  validate_final_contract(report, diagnostics) if final
  diagnostics.first(MAX_DIAGNOSTICS)
end

.validate_final_contract(report, diagnostics) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/harnex/artifact_report.rb', line 660

def validate_final_contract(report, diagnostics)
  if harness_receipt?(report)
    validate_observed_final_contract(report, diagnostics)
    return
  end

  unless report["status"] == "pass"
    diagnostics << diagnostic("final_status", "$.status", "final report status must be pass")
  end

  outcome = report["outcome"]
  unless outcome.is_a?(Hash)
    diagnostics << diagnostic("required", "$.outcome", "final report requires an outcome object")
  else
    unless ACCEPTED_OUTCOME_STATUSES.include?(outcome["status"])
      diagnostics << diagnostic(
        "outcome_not_accepted",
        "$.outcome.status",
        "final outcome must be accepted or no_change"
      )
    end
    unless non_empty_string?(outcome["summary"])
      diagnostics << diagnostic(
        "required",
        "$.outcome.summary",
        "final outcome requires a non-empty summary"
      )
    end
  end

  validation = report["validation"]
  unless validation.is_a?(Hash)
    diagnostics << diagnostic("required", "$.validation", "final report requires a validation object")
    return
  end

  expected_statuses = outcome.is_a?(Hash) && outcome["status"] == "no_change" ? %w[pass not_run] : %w[pass]
  unless expected_statuses.include?(validation["status"])
    diagnostics << diagnostic(
      "final_validation_status",
      "$.validation.status",
      "final validation.status must be #{expected_statuses.join(' or ')}"
    )
  end
  unless validation["commands"].is_a?(Array)
    diagnostics << diagnostic(
      "required",
      "$.validation.commands",
      "final report requires a validation.commands array"
    )
  end
  unless validation["final_reported"] == true
    diagnostics << diagnostic(
      "final_reported",
      "$.validation.final_reported",
      "final report requires validation.final_reported=true"
    )
  end

  Array(validation["commands"]).first(MAX_COMMANDS).each_with_index do |entry, index|
    next unless entry.is_a?(Hash) && entry["exit_code"].is_a?(Integer)
    next if entry["exit_code"].zero?

    diagnostics << diagnostic(
      "command_failed",
      "$.validation.commands[#{index}].exit_code",
      "accepted final reports require validation command exit_code=0"
    )
  end
end

.validate_observed(value, diagnostics) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/harnex/artifact_report.rb', line 575

def validate_observed(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.observed", "observed must be an object")
    return
  end

  git = value["git"]
  unless git.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.observed.git", "observed.git must be an object")
  else
    unless GIT_OBSERVATION_STATUSES.include?(git["status"])
      diagnostics << diagnostic("enum", "$.observed.git.status", "observed.git.status must be observed or unavailable")
    end
    %w[start_sha end_sha branch].each do |key|
      validate_optional_string(git, key, "$.observed.git.#{key}", diagnostics)
    end
    if git["status"] == "observed"
      %w[start_sha end_sha].each do |key|
        unless git[key].is_a?(String) && git[key].match?(/\A(?:[0-9a-f]{40}|[0-9a-f]{64})\z/)
          diagnostics << diagnostic("git_sha", "$.observed.git.#{key}", "observed.git.#{key} must be a full hexadecimal Git SHA")
        end
      end
    end
    validate_string_array_value(
      git["changed_paths"],
      "$.observed.git.changed_paths",
      max_items: MAX_CHANGED_PATHS,
      diagnostics: diagnostics,
      required: true
    ) if git["status"] == "observed" || git.key?("changed_paths")
    %w[loc_added loc_removed files_changed commits].each do |key|
      if git["status"] == "observed" && !git.key?(key)
        diagnostics << diagnostic("required", "$.observed.git.#{key}", "observed.git.#{key} is required when Git is observed")
        next
      end
      next unless git.key?(key)
      next if git[key].is_a?(Integer) && git[key] >= 0

      diagnostics << diagnostic("integer_required", "$.observed.git.#{key}", "observed.git.#{key} must be a non-negative integer")
    end
    %w[start_dirty end_dirty worktree_changed changed_paths_truncated].each do |key|
      next unless git.key?(key)
      next if boolean?(git[key])

      diagnostics << diagnostic("boolean_required", "$.observed.git.#{key}", "observed.git.#{key} must be true or false")
    end
  end

  validate_commands(value["commands"], diagnostics, base_path: "$.observed.commands")
  if value.key?("commands_truncated") && !boolean?(value["commands_truncated"])
    diagnostics << diagnostic("boolean_required", "$.observed.commands_truncated", "observed.commands_truncated must be true or false")
  end
  unless COMMAND_OBSERVATION_STATUSES.include?(value["command_observation"])
    diagnostics << diagnostic(
      "enum",
      "$.observed.command_observation",
      "observed.command_observation must be observed or unsupported"
    )
  end

  turn = value["turn"]
  unless turn.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.observed.turn", "observed.turn must be an object")
  else
    validate_required_string(turn, "status", "$.observed.turn.status", diagnostics)
    %w[task_complete task_failed accepted].each do |key|
      next if boolean?(turn[key])

      diagnostics << diagnostic("boolean_required", "$.observed.turn.#{key}", "observed.turn.#{key} must be true or false")
    end
    validate_optional_string(turn, "outcome_class", "$.observed.turn.outcome_class", diagnostics)
    if turn.key?("exit_code") && !turn["exit_code"].is_a?(Integer)
      diagnostics << diagnostic("integer_required", "$.observed.turn.exit_code", "observed.turn.exit_code must be an integer")
    end
  end

  unless value["usage"].is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.observed.usage", "observed.usage must be an object")
  else
    unless USAGE_STATUSES.include?(value.dig("usage", "status"))
      diagnostics << diagnostic("enum", "$.observed.usage.status", "observed.usage.status must be observed, estimated, unsupported, missing, or zero")
    end
  end
end

.validate_observed_final_contract(report, diagnostics) ⇒ Object

Harness receipts prove that the receipt itself is complete and that Harnex accepted the observed terminal state. Individual command failures remain factual telemetry: they are not treated as worker-authored gates, because exploratory failures can precede a successful final turn.



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/harnex/artifact_report.rb', line 739

def validate_observed_final_contract(report, diagnostics)
  unless report["status"] == "pass"
    diagnostics << diagnostic("final_status", "$.status", "final receipt status must be pass")
  end

  outcome = report["outcome"]
  unless outcome.is_a?(Hash) && ACCEPTED_OUTCOME_STATUSES.include?(outcome["status"])
    diagnostics << diagnostic("outcome_not_accepted", "$.outcome.status", "final receipt outcome must be accepted or no_change")
  end
  unless outcome.is_a?(Hash) && non_empty_string?(outcome["summary"])
    diagnostics << diagnostic("required", "$.outcome.summary", "final receipt requires a non-empty summary")
  end

  validation = report["validation"]
  unless validation.is_a?(Hash) && validation["final_reported"] == true
    diagnostics << diagnostic("final_reported", "$.validation.final_reported", "final receipt requires validation.final_reported=true")
  end

  turn = report.dig("observed", "turn")
  unless turn.is_a?(Hash) && turn["accepted"] == true
    diagnostics << diagnostic("observed_not_accepted", "$.observed.turn.accepted", "final receipt requires an accepted observed turn")
  end

  return unless outcome.is_a?(Hash) && outcome["status"] == "no_change"

  git = report.dig("observed", "git")
  no_change = git.is_a?(Hash) &&
    git["status"] == "observed" &&
    git["commits"].to_i.zero? &&
    Array(git["changed_paths"]).empty? &&
    !git["start_sha"].to_s.empty? &&
    git["start_sha"].to_s == git["end_sha"].to_s
  unless no_change
    diagnostics << diagnostic(
      "no_change_unobserved",
      "$.observed.git",
      "no_change requires an observed zero-delta Git state"
    )
  end
end

.validate_optional_enum(report, key, values, diagnostics) ⇒ Object



419
420
421
422
423
424
425
426
# File 'lib/harnex/artifact_report.rb', line 419

def validate_optional_enum(report, key, values, diagnostics)
  return unless report.key?(key)

  value = report[key]
  unless value.is_a?(String) && values.include?(value)
    diagnostics << diagnostic("enum", "$.#{key}", "#{key} must be one of #{values.join(', ')}")
  end
end

.validate_optional_string(hash, key, path, diagnostics) ⇒ Object



821
822
823
824
825
826
827
828
829
830
# File 'lib/harnex/artifact_report.rb', line 821

def validate_optional_string(hash, key, path, diagnostics)
  return unless hash.key?(key)
  return if hash[key].is_a?(String) && hash[key].length <= MAX_STRING_LENGTH

  diagnostics << diagnostic(
    "string_required",
    path,
    "#{path.delete_prefix('$.')} must be a string no longer than #{MAX_STRING_LENGTH} characters"
  )
end

.validate_outcome(value, diagnostics) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/harnex/artifact_report.rb', line 428

def validate_outcome(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.outcome", "outcome must be an object")
    return
  end

  status = value["status"]
  unless status.is_a?(String) && OUTCOME_STATUSES.include?(status)
    diagnostics << diagnostic(
      "enum",
      "$.outcome.status",
      "outcome.status must be one of #{OUTCOME_STATUSES.join(', ')}"
    )
  end
  validate_optional_string(value, "summary", "$.outcome.summary", diagnostics)
  validate_optional_string(value, "commit_sha", "$.outcome.commit_sha", diagnostics)
end

.validate_receipt(value, diagnostics) ⇒ Object



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/harnex/artifact_report.rb', line 553

def validate_receipt(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.receipt", "receipt must be an object")
    return
  end

  unless value["version"] == RECEIPT_VERSION
    diagnostics << diagnostic("receipt_version", "$.receipt.version", "receipt.version must equal #{RECEIPT_VERSION}")
  end
  unless value["author"] == "harnex"
    diagnostics << diagnostic("receipt_author", "$.receipt.author", "receipt.author must equal harnex")
  end
  validate_required_string(value, "generated_at", "$.receipt.generated_at", diagnostics)
  begin
    Time.iso8601(value["generated_at"].to_s)
  rescue ArgumentError
    diagnostics << diagnostic("timestamp_required", "$.receipt.generated_at", "receipt.generated_at must be ISO 8601")
  end
  validate_required_string(value, "id", "$.receipt.id", diagnostics)
  validate_required_string(value, "session_id", "$.receipt.session_id", diagnostics)
end

.validate_required_string(hash, key, path, diagnostics) ⇒ Object



815
816
817
818
819
# File 'lib/harnex/artifact_report.rb', line 815

def validate_required_string(hash, key, path, diagnostics)
  return if non_empty_string?(hash[key])

  diagnostics << diagnostic("string_required", path, "#{path.delete_prefix('$.')} must be a non-empty string")
end

.validate_schema(report, diagnostics) ⇒ Object



410
411
412
413
414
415
416
417
# File 'lib/harnex/artifact_report.rb', line 410

def validate_schema(report, diagnostics)
  value = report["schema"]
  if !value.is_a?(String) || value.empty?
    diagnostics << diagnostic("schema_required", "$.schema", "schema must be a non-empty string")
  elsif value != SCHEMA
    diagnostics << diagnostic("schema_unsupported", "$.schema", "schema must equal #{SCHEMA}")
  end
end

.validate_string_array_field(report, key, max_items:, diagnostics:) ⇒ Object



780
781
782
783
784
785
786
787
788
789
790
# File 'lib/harnex/artifact_report.rb', line 780

def validate_string_array_field(report, key, max_items:, diagnostics:)
  return unless report.key?(key)

  validate_string_array_value(
    report[key],
    "$.#{key}",
    max_items: max_items,
    diagnostics: diagnostics,
    required: true
  )
end

.validate_string_array_value(value, path, max_items:, diagnostics:, required:) ⇒ Object



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/harnex/artifact_report.rb', line 792

def validate_string_array_value(value, path, max_items:, diagnostics:, required:)
  if value.nil? && !required
    return
  end
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", path, "#{path.delete_prefix('$.')} must be an array")
    return
  end
  if value.length > max_items
    diagnostics << diagnostic("too_many_items", path, "#{path.delete_prefix('$.')} may contain at most #{max_items} entries")
  end

  value.first(max_items).each_with_index do |item, index|
    next if non_empty_string?(item)

    diagnostics << diagnostic(
      "string_required",
      "#{path}[#{index}]",
      "#{path.delete_prefix('$.')} entries must be non-empty strings"
    )
  end
end

.validate_validation(value, diagnostics) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/harnex/artifact_report.rb', line 446

def validate_validation(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.validation", "validation must be an object")
    return
  end

  if value.key?("status")
    status = value["status"]
    unless status.is_a?(String) && VALIDATION_STATUSES.include?(status)
      diagnostics << diagnostic(
        "enum",
        "$.validation.status",
        "validation.status must be one of #{VALIDATION_STATUSES.join(', ')}"
      )
    end
  end

  validate_commands(value["commands"], diagnostics) if value.key?("commands")
  if value.key?("final_reported") && !boolean?(value["final_reported"])
    diagnostics << diagnostic(
      "boolean_required",
      "$.validation.final_reported",
      "validation.final_reported must be true or false"
    )
  end
end

.validation_failure(path, status:, diagnostics:, bytes: nil, sha256: nil) ⇒ Object



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'lib/harnex/artifact_report.rb', line 1048

def validation_failure(path, status:, diagnostics:, bytes: nil, sha256: nil)
  ValidationResult.new(
    ok: false,
    status: status,
    path: path,
    bytes: bytes,
    sha256: sha256,
    schema: nil,
    diagnostics: diagnostics.first(MAX_DIAGNOSTICS),
    report: nil
  )
end

.warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil, diagnostics: nil) ⇒ Object



873
874
875
876
877
878
# File 'lib/harnex/artifact_report.rb', line 873

def warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil, diagnostics: nil)
  report = (path, bytes: bytes, sha256: sha256, ingest_status: ingest_status, schema: schema)
  report["warning"] = bounded_string(warning)
  report["diagnostics"] = Array(diagnostics).first(MAX_DIAGNOSTICS) unless Array(diagnostics).empty?
  { "artifact_report" => report }
end

.write_observed(path, **attributes) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/harnex/artifact_report.rb', line 334

def write_observed(path, **attributes)
  report_path = File.expand_path(path.to_s)
  report = build_observed(**attributes)
  diagnostics = validate_document(report)
  unless diagnostics.empty?
    raise ArgumentError, "generated artifact receipt is invalid: #{diagnostics.first.fetch('path')}"
  end

  FileUtils.mkdir_p(File.dirname(report_path))
  temporary_path = "#{report_path}.tmp.#{Process.pid}.#{Thread.current.object_id}"
  File.open(temporary_path, "wb", 0o644) do |file|
    file.write(JSON.pretty_generate(report))
    file.write("\n")
    file.flush
    file.fsync
  end
  File.rename(temporary_path, report_path)

  result = validate(report_path, final: !!attributes[:successful])
  if attributes[:successful] && !accepted_final?(result)
    raise ArgumentError, "generated artifact receipt did not satisfy final validation"
  end
  result
ensure
  FileUtils.rm_f(temporary_path) if defined?(temporary_path) && temporary_path
end