-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathartifact_persistence.rs
More file actions
546 lines (516 loc) · 18.3 KB
/
Copy pathartifact_persistence.rs
File metadata and controls
546 lines (516 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
118
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
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
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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
472
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
511
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
//! Bench artifact persistence.
//!
//! Boundary: the bench command owns run lifecycle and result presentation; this
//! module owns persisting bench scenario/run artifacts into the observation
//! store — resolving artifact file pointers (including shared-state and
//! preserved-invocation paths), recording file/directory/URL artifacts,
//! rewriting artifact links back onto the results, and recording budget
//! findings. It operates on the core `ActiveObservation`, not command types.
use std::collections::BTreeMap;
use std::fs;
use std::path::{Component, Path, PathBuf};
use crate::bench::{
BenchArtifact, BenchDiagnostic, BenchDiagnosticSource, BenchResults, BenchRunWorkflowResult,
};
use homeboy_core::artifacts as artifact_links;
use homeboy_core::engine::run_dir::{self, RunDir};
use homeboy_core::observation::{finding_records_from_budget, ActiveObservation};
use homeboy_lifecycle_contract::ArtifactRecord;
/// Persist every bench artifact referenced by `workflow`'s results into the
/// observation store, rewrite the results file with the recorded links, record
/// the standard bench run-dir artifacts, and record budget findings.
pub fn record_bench_observation_artifacts(
observation: &ActiveObservation,
workflow: &mut BenchRunWorkflowResult,
run_dir: &RunDir,
) -> bool {
if let Some(results) = workflow.results.as_mut() {
workflow
.diagnostics
.extend(persist_bench_result_artifact_paths(
observation,
results,
run_dir,
));
rewrite_bench_results_file(results, run_dir);
}
record_if_exists(
observation,
"bench_results",
run_dir.step_file(run_dir::files::BENCH_RESULTS),
);
record_if_exists(
observation,
"resource_summary",
run_dir.step_file(run_dir::files::RESOURCE_SUMMARY),
);
record_memory_timeline_artifacts(observation, run_dir);
record_failure_artifacts(observation, workflow, run_dir);
let Some(results) = workflow.results.as_ref() else {
return true;
};
observation.record_findings(&finding_records_from_budget(
observation.run_id(),
&results.budget_findings,
));
all_bench_artifacts_are_promoted(results)
}
/// Persist diagnostics emitted by failed nested workloads before their temporary
/// invocation directories are removed. The artifact roots are generic runner
/// output, not workload-specific paths.
fn record_failure_artifacts(
observation: &ActiveObservation,
workflow: &BenchRunWorkflowResult,
run_dir: &RunDir,
) {
let Some(failure) = workflow.failure.as_ref() else {
return;
};
let stderr_path = run_dir.step_file("bench-failure-stderr.txt");
if fs::write(&stderr_path, &failure.stderr_tail).is_ok() {
let _ = observation.store().record_artifact_with_metadata(
observation.run_id(),
"bench_failure_stderr",
&stderr_path,
serde_json::json!({
"failure_diagnostic": true,
"failure_diagnostic_rank": 0,
"source": "bench",
}),
);
}
for root in [
run_dir.path().join("artifacts"),
run_dir.path().join("invocations"),
] {
record_failure_artifact_tree(observation, &root);
}
}
fn record_failure_artifact_tree(observation: &ActiveObservation, path: &Path) {
let Ok(entries) = fs::read_dir(path) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
record_failure_artifact_tree(observation, &path);
continue;
}
if !path.is_file() {
continue;
}
let name = path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
let diagnostic = name.contains("diagnostic") || name.contains("failure");
let _ = observation.store().record_artifact_with_metadata(
observation.run_id(),
if diagnostic {
"bench_failure_diagnostic"
} else {
"bench_failure_artifact"
},
&path,
serde_json::json!({
"failure_diagnostic": diagnostic,
"failure_diagnostic_rank": diagnostic.then(|| path.components().count()).unwrap_or_default(),
"source": "bench_nested_workload",
"original_path": path,
}),
);
}
}
fn all_bench_artifacts_are_promoted(results: &BenchResults) -> bool {
results.scenarios.iter().all(|scenario| {
scenario
.artifacts
.values()
.chain(
scenario
.runs
.iter()
.flatten()
.flat_map(|run| run.artifacts.values()),
)
.filter(|artifact| artifact.required_durable)
.all(|artifact| artifact.observation_artifact_id.is_some())
})
}
/// Record an artifact when the file at `path` exists.
pub fn record_if_exists(observation: &ActiveObservation, kind: &str, path: PathBuf) {
observation.record_artifact_if_file(kind, &path);
}
/// Record any `bench-memory-timeline` JSON/CSV artifacts found in the run dir.
pub fn record_memory_timeline_artifacts(observation: &ActiveObservation, run_dir: &RunDir) {
let Ok(entries) = fs::read_dir(run_dir.path()) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
continue;
};
if name.starts_with("bench-memory-timeline")
&& (name.ends_with(".json") || name.ends_with(".csv"))
{
observation.record_artifact_if_file("bench_memory_timeline", &path);
}
}
}
fn persist_bench_result_artifact_paths(
observation: &ActiveObservation,
results: &mut BenchResults,
run_dir: &RunDir,
) -> Vec<BenchDiagnostic> {
let mut diagnostics = Vec::new();
let shared_state = results
.run_metadata
.as_ref()
.and_then(|metadata| metadata.shared_state.as_deref());
for scenario in &mut results.scenarios {
for (name, artifact) in &mut scenario.artifacts {
if let Some(diagnostic) = persist_bench_artifact(
observation,
&scenario.id,
None,
name,
artifact,
run_dir,
shared_state,
) {
diagnostics.push(diagnostic);
}
}
if let Some(runs) = &mut scenario.runs {
for (run_index, run) in runs.iter_mut().enumerate() {
for (name, artifact) in &mut run.artifacts {
if let Some(diagnostic) = persist_bench_artifact(
observation,
&scenario.id,
Some(run_index),
name,
artifact,
run_dir,
shared_state,
) {
diagnostics.push(diagnostic);
}
}
}
}
}
diagnostics
}
fn persist_bench_artifact(
observation: &ActiveObservation,
scenario_id: &str,
run_index: Option<usize>,
name: &str,
artifact: &mut BenchArtifact,
run_dir: &RunDir,
shared_state: Option<&str>,
) -> Option<BenchDiagnostic> {
// Results can be retried or hydrated from a prior run. Only this attempt's
// persisted record may satisfy the current durability declaration.
artifact.observation_artifact_id = None;
let metadata = bench_artifact_metadata(scenario_id, run_index, name, artifact);
let original_path = artifact.path.clone();
if let Some(original_path) = original_path.as_deref() {
if bench_artifact_path_is_blocked(original_path) {
return Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_artifact_path_blocked",
format!("blocked bench artifact path `{original_path}` for `{name}`"),
serde_json::json!({ "path": original_path }),
));
}
}
let Some(path) = resolve_bench_artifact_file_pointer(
original_path.as_deref(),
artifact.url.as_deref(),
run_dir,
shared_state,
) else {
if let Some(url) = artifact.url.clone() {
if !artifact.required_durable {
return match observation.store().record_url_artifact_with_metadata(
observation.run_id(),
artifact.kind.as_deref().unwrap_or(name),
&url,
metadata,
) {
Ok(record) => apply_recorded_bench_artifact_links(
scenario_id,
run_index,
name,
artifact,
&record,
),
Err(error) => Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_artifact_url_record_failed",
format!("failed to record bench URL artifact `{name}`: {error}"),
serde_json::json!({ "url": url }),
)),
};
}
return Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_artifact_durable_source_missing",
format!(
"bench artifact `{name}` has only URL source `{url}`; reviewer-facing artifacts require a local file or directory for durable promotion"
),
serde_json::json!({ "url": url }),
));
}
let original_path = original_path?;
return Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_artifact_path_missing",
format!("bench artifact path for `{name}` was not found: {original_path}"),
serde_json::json!({
"path": original_path,
"resolved_path": resolve_bench_artifact_path(&original_path, run_dir, shared_state).to_string_lossy().to_string(),
}),
));
};
let record = if path.is_file() {
observation.store().record_artifact_with_metadata(
observation.run_id(),
"bench_artifact",
&path,
metadata,
)
} else if path.is_dir() {
observation.store().record_directory_artifact_with_metadata(
observation.run_id(),
"bench_artifact",
&path,
metadata,
)
} else {
unreachable!("resolved bench artifact file pointers must be files or directories");
};
match record {
Ok(record) => {
// The source URL can be a tunnel or runner-local address. Keep it
// in the persisted provenance metadata, but never present it as a
// reviewer-facing reference after durable promotion.
artifact.url = None;
artifact.path = Some(record.path.clone());
apply_recorded_bench_artifact_links(scenario_id, run_index, name, artifact, &record)
}
Err(error) => Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_artifact_record_failed",
format!("failed to persist bench artifact `{name}`: {error}"),
serde_json::json!({
"path": original_path,
"resolved_path": path.to_string_lossy().to_string(),
}),
)),
}
}
/// Attach the recorded artifact's observation id, public URL, and validated
/// viewer links back onto the bench artifact, returning a diagnostic when the
/// public URL is unreachable.
pub fn apply_recorded_bench_artifact_links(
scenario_id: &str,
run_index: Option<usize>,
name: &str,
artifact: &mut BenchArtifact,
record: &ArtifactRecord,
) -> Option<BenchDiagnostic> {
artifact.observation_artifact_id = Some(record.id.clone());
let public_url = artifact_links::public_artifact_url(record)?;
if artifact_links::public_artifact_url_is_reachable_or_legacy(record) {
artifact.public_url = Some(public_url.clone());
artifact.viewer_refs.viewer_links =
artifact_links::cached_validated_viewer_links(record, &public_url);
artifact.viewer_refs.viewer_url = artifact
.viewer_refs
.viewer_links
.first()
.map(|link| link.url.clone());
None
} else {
let validation = record
.metadata_json
.get("public_url_validation")
.expect("unreachable new artifact has validation metadata");
let error = validation
.get("error")
.and_then(serde_json::Value::as_str)
.unwrap_or("public artifact URL was not reachable");
Some(bench_artifact_diagnostic(
scenario_id,
run_index,
name,
"bench_public_artifact_url_unreachable",
format!(
"public artifact URL for bench artifact `{name}` is not reachable; viewer links were not published: {error}"
),
serde_json::json!({
"url": validation.get("url").cloned().unwrap_or(serde_json::Value::Null),
"status_code": validation.get("status_code").cloned().unwrap_or(serde_json::Value::Null),
"error": validation.get("error").cloned().unwrap_or(serde_json::Value::Null),
}),
))
}
}
fn bench_artifact_metadata(
scenario_id: &str,
run_index: Option<usize>,
name: &str,
artifact: &BenchArtifact,
) -> serde_json::Value {
serde_json::json!({
"source": "bench",
"scenario_id": scenario_id,
"run_index": run_index,
"name": name,
"kind": artifact.kind,
"label": artifact.label,
"original_path": artifact.path,
"url": artifact.url,
"viewer": artifact.viewer,
})
}
fn bench_artifact_path_is_blocked(path: &str) -> bool {
Path::new(path)
.components()
.any(|component| matches!(component, Component::ParentDir))
}
fn bench_artifact_diagnostic(
scenario_id: &str,
run_index: Option<usize>,
name: &str,
class: &str,
message: String,
metadata: serde_json::Value,
) -> BenchDiagnostic {
let mut diagnostic_metadata = BTreeMap::new();
diagnostic_metadata.insert("artifact_name".to_string(), serde_json::json!(name));
if let Some(object) = metadata.as_object() {
for (key, value) in object {
diagnostic_metadata.insert(key.clone(), value.clone());
}
}
BenchDiagnostic {
class: class.to_string(),
message: Some(message),
severity: None,
source: Some(match run_index {
Some(run_index) => BenchDiagnosticSource::ScenarioRun {
scenario_id: scenario_id.to_string(),
run_index,
},
None => BenchDiagnosticSource::Scenario {
scenario_id: scenario_id.to_string(),
},
}),
metadata: diagnostic_metadata,
}
}
fn rewrite_bench_results_file(results: &BenchResults, run_dir: &RunDir) {
let Ok(json) = serde_json::to_vec_pretty(results) else {
return;
};
let _ = fs::write(run_dir.step_file(run_dir::files::BENCH_RESULTS), json);
}
fn resolve_bench_artifact_path(
path: &str,
run_dir: &RunDir,
shared_state: Option<&str>,
) -> PathBuf {
let artifact_path = PathBuf::from(path);
if artifact_path.exists() {
return artifact_path;
}
if artifact_path.is_absolute() {
if let Some(shared_state_path) = resolve_shared_state_artifact(&artifact_path, shared_state)
{
return shared_state_path;
}
if let Some(preserved_path) = resolve_preserved_invocation_artifact(&artifact_path, run_dir)
{
return preserved_path;
}
return artifact_path;
}
let run_dir_path = run_dir.path().join(path);
if run_dir_path.exists() {
return run_dir_path;
}
artifact_path
}
fn resolve_bench_artifact_file_pointer(
path: Option<&str>,
url: Option<&str>,
run_dir: &RunDir,
shared_state: Option<&str>,
) -> Option<PathBuf> {
if let Some(path) = path {
let resolved = resolve_bench_artifact_path(path, run_dir, shared_state);
if resolved.is_file() || resolved.is_dir() {
return Some(resolved);
}
}
let filename = url.and_then(artifact_filename_from_url)?;
[
run_dir.path().join("artifacts").join(&filename),
run_dir.path().join(&filename),
]
.into_iter()
.find(|candidate| candidate.is_file() || candidate.is_dir())
}
fn artifact_filename_from_url(url: &str) -> Option<String> {
let parsed = reqwest::Url::parse(url).ok()?;
parsed
.path_segments()?
.next_back()
.filter(|segment| !segment.is_empty() && !bench_artifact_path_is_blocked(segment))
.map(str::to_string)
}
fn resolve_shared_state_artifact(path: &Path, shared_state: Option<&str>) -> Option<PathBuf> {
let shared_state = shared_state?;
let relative = path.strip_prefix("/bench-shared-state").ok()?;
let candidate = Path::new(shared_state).join(relative);
if candidate.exists() {
return Some(candidate);
}
None
}
fn resolve_preserved_invocation_artifact(path: &Path, run_dir: &RunDir) -> Option<PathBuf> {
let mut components = path.components().peekable();
while let Some(component) = components.next() {
let name = component.as_os_str().to_string_lossy();
let Some(short_id) = name.strip_suffix(".a") else {
continue;
};
let mut preserved = run_dir
.path()
.join("invocations")
.join(format!("inv-{short_id}"))
.join("artifacts");
for rest in components {
preserved.push(rest.as_os_str());
}
if preserved.exists() {
return Some(preserved);
}
return None;
}
None
}