| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
use crate::domain::{RunId, Version}; |
| 19 |
use anyhow::Result; |
| 20 |
use chrono::Utc; |
| 21 |
use serde::Serialize; |
| 22 |
use sqlx::{Row, SqlitePool}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[derive(Debug, Clone, Copy)] |
| 27 |
pub enum Phase { |
| 28 |
Fetching, |
| 29 |
Compiling, |
| 30 |
Staging, |
| 31 |
Gating, |
| 32 |
} |
| 33 |
|
| 34 |
impl Phase { |
| 35 |
pub fn as_str(self) -> &'static str { |
| 36 |
match self { |
| 37 |
Phase::Fetching => "fetching", |
| 38 |
Phase::Compiling => "compiling", |
| 39 |
Phase::Staging => "staging", |
| 40 |
Phase::Gating => "gating", |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
pub async fn create(pool: &SqlitePool, sha: &str) -> Result<RunId> { |
| 47 |
let id: i64 = sqlx::query_scalar( |
| 48 |
"INSERT INTO build_runs (sha, phase, result, started_at) |
| 49 |
VALUES (?, 'queued', 'building', ?) RETURNING id", |
| 50 |
) |
| 51 |
.bind(sha) |
| 52 |
.bind(Utc::now().to_rfc3339()) |
| 53 |
.fetch_one(pool) |
| 54 |
.await?; |
| 55 |
Ok(RunId(id)) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
pub async fn set_phase(pool: &SqlitePool, run_id: RunId, phase: Phase) -> Result<()> { |
| 61 |
sqlx::query("UPDATE build_runs SET phase = ? WHERE id = ? AND result = 'building'") |
| 62 |
.bind(phase.as_str()) |
| 63 |
.bind(run_id.0) |
| 64 |
.execute(pool) |
| 65 |
.await?; |
| 66 |
Ok(()) |
| 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 |
pub async fn advance_tier( |
| 101 |
pool: &SqlitePool, |
| 102 |
tier: &str, |
| 103 |
version: &Version, |
| 104 |
build_id: Option<i64>, |
| 105 |
) -> Result<(), sqlx::Error> { |
| 106 |
let now = Utc::now().to_rfc3339(); |
| 107 |
sqlx::query( |
| 108 |
"UPDATE tier_state |
| 109 |
SET previous_version = current_version, |
| 110 |
current_version = ?, |
| 111 |
previous_build_id = current_build_id, |
| 112 |
current_build_id = ?, |
| 113 |
burn_in_started_at = ?, |
| 114 |
advanced_at = ? |
| 115 |
WHERE tier = ?", |
| 116 |
) |
| 117 |
.bind(version) |
| 118 |
.bind(build_id) |
| 119 |
.bind(&now) |
| 120 |
.bind(&now) |
| 121 |
.bind(tier) |
| 122 |
.execute(pool) |
| 123 |
.await?; |
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pub async fn set_version(pool: &SqlitePool, run_id: RunId, version: &Version) -> Result<()> { |
| 129 |
sqlx::query("UPDATE build_runs SET version = ? WHERE id = ? AND result = 'building'") |
| 130 |
.bind(version.to_string()) |
| 131 |
.bind(run_id.0) |
| 132 |
.execute(pool) |
| 133 |
.await?; |
| 134 |
Ok(()) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
pub async fn set_identity( |
| 143 |
pool: &SqlitePool, |
| 144 |
run_id: RunId, |
| 145 |
bundle_digest: &str, |
| 146 |
staged_path: &str, |
| 147 |
) -> Result<()> { |
| 148 |
sqlx::query( |
| 149 |
"UPDATE build_runs SET bundle_digest = ?, staged_path = ? |
| 150 |
WHERE id = ? AND result = 'building'", |
| 151 |
) |
| 152 |
.bind(bundle_digest) |
| 153 |
.bind(staged_path) |
| 154 |
.bind(run_id.0) |
| 155 |
.execute(pool) |
| 156 |
.await?; |
| 157 |
Ok(()) |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
pub async fn mark_passed(pool: &SqlitePool, run_id: RunId) -> Result<()> { |
| 162 |
sqlx::query( |
| 163 |
"UPDATE build_runs SET result = 'passed', phase = 'done', finished_at = ? |
| 164 |
WHERE id = ? AND result = 'building'", |
| 165 |
) |
| 166 |
.bind(Utc::now().to_rfc3339()) |
| 167 |
.bind(run_id.0) |
| 168 |
.execute(pool) |
| 169 |
.await?; |
| 170 |
Ok(()) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
pub async fn mark_failed(pool: &SqlitePool, run_id: RunId, summary: &str) -> Result<()> { |
| 177 |
|
| 178 |
|
| 179 |
let summary: String = summary.chars().take(600).collect(); |
| 180 |
sqlx::query( |
| 181 |
"UPDATE build_runs SET result = 'failed', phase = 'done', failure_summary = ?, finished_at = ? |
| 182 |
WHERE id = ? AND result = 'building'", |
| 183 |
) |
| 184 |
.bind(&summary) |
| 185 |
.bind(Utc::now().to_rfc3339()) |
| 186 |
.bind(run_id.0) |
| 187 |
.execute(pool) |
| 188 |
.await?; |
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
pub async fn mark_aborted(pool: &SqlitePool, run_id: RunId) -> Result<()> { |
| 194 |
sqlx::query( |
| 195 |
"UPDATE build_runs SET result = 'aborted', phase = 'done', |
| 196 |
failure_summary = 'superseded by a newer /rebuild', finished_at = ? |
| 197 |
WHERE id = ? AND result = 'building'", |
| 198 |
) |
| 199 |
.bind(Utc::now().to_rfc3339()) |
| 200 |
.bind(run_id.0) |
| 201 |
.execute(pool) |
| 202 |
.await?; |
| 203 |
Ok(()) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
pub async fn recover_orphaned_running(pool: &SqlitePool) -> Result<u64> { |
| 213 |
let res = sqlx::query( |
| 214 |
"UPDATE build_runs SET result = 'aborted', phase = 'done', |
| 215 |
failure_summary = 'daemon restarted mid-build', finished_at = ? |
| 216 |
WHERE result = 'building'", |
| 217 |
) |
| 218 |
.bind(Utc::now().to_rfc3339()) |
| 219 |
.execute(pool) |
| 220 |
.await?; |
| 221 |
Ok(res.rows_affected()) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
#[derive(Debug, Serialize)] |
| 226 |
pub struct RunGateView { |
| 227 |
pub kind: String, |
| 228 |
|
| 229 |
pub status: Option<String>, |
| 230 |
|
| 231 |
pub log_ref: Option<String>, |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
#[derive(Debug, Serialize)] |
| 236 |
pub struct RunView { |
| 237 |
pub run_id: i64, |
| 238 |
pub sha: String, |
| 239 |
pub version: Option<String>, |
| 240 |
pub phase: String, |
| 241 |
|
| 242 |
pub result: String, |
| 243 |
pub started_at: String, |
| 244 |
pub finished_at: Option<String>, |
| 245 |
|
| 246 |
|
| 247 |
pub failure_summary: Option<String>, |
| 248 |
|
| 249 |
|
| 250 |
pub gates: Vec<RunGateView>, |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
pub async fn get(pool: &SqlitePool, run_id: RunId) -> Result<Option<RunView>> { |
| 255 |
let Some(row) = sqlx::query( |
| 256 |
"SELECT id, sha, version, phase, result, started_at, finished_at, failure_summary |
| 257 |
FROM build_runs WHERE id = ?", |
| 258 |
) |
| 259 |
.bind(run_id.0) |
| 260 |
.fetch_optional(pool) |
| 261 |
.await? |
| 262 |
else { |
| 263 |
return Ok(None); |
| 264 |
}; |
| 265 |
|
| 266 |
let version: Option<String> = row.get("version"); |
| 267 |
|
| 268 |
|
| 269 |
let gates: Vec<RunGateView> = if let Some(ver) = version.as_deref() { |
| 270 |
sqlx::query( |
| 271 |
"SELECT gate_kind, status, log_ref |
| 272 |
FROM gate_runs g |
| 273 |
WHERE tier = 'host' AND version = ?1 |
| 274 |
AND id = (SELECT MAX(id) FROM gate_runs |
| 275 |
WHERE tier = 'host' AND version = ?1 AND gate_kind = g.gate_kind) |
| 276 |
ORDER BY gate_kind", |
| 277 |
) |
| 278 |
.bind(ver) |
| 279 |
.fetch_all(pool) |
| 280 |
.await? |
| 281 |
.into_iter() |
| 282 |
.map(|gr| RunGateView { |
| 283 |
kind: gr.get("gate_kind"), |
| 284 |
status: gr.get("status"), |
| 285 |
log_ref: gr.get("log_ref"), |
| 286 |
}) |
| 287 |
.collect() |
| 288 |
} else { |
| 289 |
Vec::new() |
| 290 |
}; |
| 291 |
|
| 292 |
Ok(Some(RunView { |
| 293 |
run_id: row.get("id"), |
| 294 |
sha: row.get("sha"), |
| 295 |
version, |
| 296 |
phase: row.get("phase"), |
| 297 |
result: row.get("result"), |
| 298 |
started_at: row.get("started_at"), |
| 299 |
finished_at: row.get("finished_at"), |
| 300 |
failure_summary: row.get("failure_summary"), |
| 301 |
gates, |
| 302 |
})) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
#[derive(Debug, Serialize)] |
| 307 |
pub struct BuildSummary { |
| 308 |
pub run_id: i64, |
| 309 |
pub sha: String, |
| 310 |
pub version: Option<String>, |
| 311 |
pub phase: String, |
| 312 |
pub result: String, |
| 313 |
pub failure_summary: Option<String>, |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
pub elapsed_s: i64, |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
pub async fn latest_summary(pool: &SqlitePool) -> Result<Option<BuildSummary>> { |
| 322 |
let Some(row) = sqlx::query( |
| 323 |
"SELECT id, sha, version, phase, result, failure_summary, started_at, finished_at |
| 324 |
FROM build_runs ORDER BY id DESC LIMIT 1", |
| 325 |
) |
| 326 |
.fetch_optional(pool) |
| 327 |
.await? |
| 328 |
else { |
| 329 |
return Ok(None); |
| 330 |
}; |
| 331 |
let started_at: String = row.get("started_at"); |
| 332 |
let finished_at: Option<String> = row.get("finished_at"); |
| 333 |
Ok(Some(BuildSummary { |
| 334 |
run_id: row.get("id"), |
| 335 |
sha: row.get("sha"), |
| 336 |
version: row.get("version"), |
| 337 |
phase: row.get("phase"), |
| 338 |
result: row.get("result"), |
| 339 |
failure_summary: row.get("failure_summary"), |
| 340 |
elapsed_s: elapsed_seconds(&started_at, finished_at.as_deref()), |
| 341 |
})) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
fn elapsed_seconds(started_at: &str, finished_at: Option<&str>) -> i64 { |
| 347 |
let Ok(start) = chrono::DateTime::parse_from_rfc3339(started_at) else { |
| 348 |
return 0; |
| 349 |
}; |
| 350 |
let end = match finished_at { |
| 351 |
Some(f) => chrono::DateTime::parse_from_rfc3339(f) |
| 352 |
.map_or_else(|_| Utc::now(), |d| d.with_timezone(&Utc)), |
| 353 |
None => Utc::now(), |
| 354 |
}; |
| 355 |
(end - start.with_timezone(&Utc)).num_seconds().max(0) |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
pub async fn first_failed_gate_summary(pool: &SqlitePool, version: &Version) -> Option<String> { |
| 363 |
let row = sqlx::query( |
| 364 |
"SELECT gate_kind, outcome_json FROM gate_runs |
| 365 |
WHERE tier = 'host' AND version = ? AND status = 'failed' |
| 366 |
ORDER BY id ASC LIMIT 1", |
| 367 |
) |
| 368 |
.bind(version.to_string()) |
| 369 |
.fetch_optional(pool) |
| 370 |
.await |
| 371 |
.ok() |
| 372 |
.flatten()?; |
| 373 |
let kind: String = row.get("gate_kind"); |
| 374 |
let outcome_json: Option<String> = row.get("outcome_json"); |
| 375 |
let summary = outcome_json |
| 376 |
.and_then(|s| serde_json::from_str::<crate::outcome::GateOutcome>(&s).ok()) |
| 377 |
.map_or_else( |
| 378 |
|| "gate failed".to_string(), |
| 379 |
|o| match o.status { |
| 380 |
crate::outcome::GateStatus::Failed { failure } => failure.summary(), |
| 381 |
other => format!("{other:?}"), |
| 382 |
}, |
| 383 |
); |
| 384 |
Some(format!("{kind}: {summary}")) |
| 385 |
} |
| 386 |
|
| 387 |
#[cfg(test)] |
| 388 |
mod tests { |
| 389 |
use super::*; |
| 390 |
use sqlx::sqlite::SqlitePoolOptions; |
| 391 |
|
| 392 |
async fn pool() -> SqlitePool { |
| 393 |
let pool = SqlitePoolOptions::new() |
| 394 |
.max_connections(1) |
| 395 |
.connect("sqlite::memory:") |
| 396 |
.await |
| 397 |
.unwrap(); |
| 398 |
crate::db::migrate(&pool).await.unwrap(); |
| 399 |
pool |
| 400 |
} |
| 401 |
|
| 402 |
#[tokio::test] |
| 403 |
async fn create_then_get_roundtrips_building() { |
| 404 |
let pool = pool().await; |
| 405 |
let id = create(&pool, "abc1234").await.unwrap(); |
| 406 |
let v = get(&pool, id).await.unwrap().expect("run exists"); |
| 407 |
assert_eq!(v.sha, "abc1234"); |
| 408 |
assert_eq!(v.result, "building"); |
| 409 |
assert_eq!(v.phase, "queued"); |
| 410 |
assert!(v.version.is_none()); |
| 411 |
assert!(v.gates.is_empty()); |
| 412 |
assert!(v.failure_summary.is_none()); |
| 413 |
} |
| 414 |
|
| 415 |
#[tokio::test] |
| 416 |
async fn recover_orphaned_running_settles_building_runs() { |
| 417 |
let pool = pool().await; |
| 418 |
|
| 419 |
|
| 420 |
let run_a = create(&pool, "aaaaaaa").await.unwrap(); |
| 421 |
let run_b = create(&pool, "bbbbbbb").await.unwrap(); |
| 422 |
let run_c = create(&pool, "ccccccc").await.unwrap(); |
| 423 |
mark_passed(&pool, run_c).await.unwrap(); |
| 424 |
|
| 425 |
let reconciled = recover_orphaned_running(&pool).await.unwrap(); |
| 426 |
assert_eq!(reconciled, 2, "both 'building' runs reconciled"); |
| 427 |
|
| 428 |
for id in [run_a, run_b] { |
| 429 |
let rec = get(&pool, id).await.unwrap().unwrap(); |
| 430 |
assert_eq!(rec.result, "aborted"); |
| 431 |
assert_eq!(rec.phase, "done"); |
| 432 |
assert!(rec.finished_at.is_some()); |
| 433 |
assert_eq!( |
| 434 |
rec.failure_summary.as_deref(), |
| 435 |
Some("daemon restarted mid-build") |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
assert_eq!(get(&pool, run_c).await.unwrap().unwrap().result, "passed"); |
| 440 |
|
| 441 |
|
| 442 |
assert_eq!(recover_orphaned_running(&pool).await.unwrap(), 0); |
| 443 |
} |
| 444 |
|
| 445 |
#[tokio::test] |
| 446 |
async fn phase_and_version_advance_then_pass() { |
| 447 |
let pool = pool().await; |
| 448 |
let id = create(&pool, "abc1234").await.unwrap(); |
| 449 |
set_phase(&pool, id, Phase::Compiling).await.unwrap(); |
| 450 |
let ver: Version = "0.10.2".parse().unwrap(); |
| 451 |
set_version(&pool, id, &ver).await.unwrap(); |
| 452 |
mark_passed(&pool, id).await.unwrap(); |
| 453 |
|
| 454 |
let v = get(&pool, id).await.unwrap().unwrap(); |
| 455 |
assert_eq!(v.result, "passed"); |
| 456 |
assert_eq!(v.phase, "done"); |
| 457 |
assert_eq!(v.version.as_deref(), Some("0.10.2")); |
| 458 |
assert!(v.finished_at.is_some()); |
| 459 |
} |
| 460 |
|
| 461 |
#[tokio::test] |
| 462 |
async fn first_terminal_write_wins() { |
| 463 |
let pool = pool().await; |
| 464 |
let id = create(&pool, "abc1234").await.unwrap(); |
| 465 |
mark_failed(&pool, id, "error[E0063]: missing field user_pages_host") |
| 466 |
.await |
| 467 |
.unwrap(); |
| 468 |
|
| 469 |
|
| 470 |
mark_passed(&pool, id).await.unwrap(); |
| 471 |
|
| 472 |
mark_failed(&pool, id, "something else").await.unwrap(); |
| 473 |
|
| 474 |
let v = get(&pool, id).await.unwrap().unwrap(); |
| 475 |
assert_eq!(v.result, "failed"); |
| 476 |
assert_eq!( |
| 477 |
v.failure_summary.as_deref(), |
| 478 |
Some("error[E0063]: missing field user_pages_host") |
| 479 |
); |
| 480 |
} |
| 481 |
|
| 482 |
#[tokio::test] |
| 483 |
async fn phase_write_after_terminal_is_noop() { |
| 484 |
let pool = pool().await; |
| 485 |
let id = create(&pool, "abc1234").await.unwrap(); |
| 486 |
mark_passed(&pool, id).await.unwrap(); |
| 487 |
set_phase(&pool, id, Phase::Gating).await.unwrap(); |
| 488 |
let v = get(&pool, id).await.unwrap().unwrap(); |
| 489 |
assert_eq!( |
| 490 |
v.phase, "done", |
| 491 |
"a late phase write must not move a finished run" |
| 492 |
); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn elapsed_seconds_uses_finished_when_present() { |
| 497 |
|
| 498 |
let s = elapsed_seconds("2026-06-13T00:00:00Z", Some("2026-06-13T00:02:05Z")); |
| 499 |
assert_eq!(s, 125); |
| 500 |
|
| 501 |
assert_eq!(elapsed_seconds("not-a-date", None), 0); |
| 502 |
} |
| 503 |
|
| 504 |
#[tokio::test] |
| 505 |
async fn latest_summary_reports_most_recent_run() { |
| 506 |
let pool = pool().await; |
| 507 |
assert!(latest_summary(&pool).await.unwrap().is_none()); |
| 508 |
let _old = create(&pool, "old1234").await.unwrap(); |
| 509 |
let new = create(&pool, "new5678").await.unwrap(); |
| 510 |
set_phase(&pool, new, Phase::Compiling).await.unwrap(); |
| 511 |
let sum = latest_summary(&pool).await.unwrap().expect("a run exists"); |
| 512 |
assert_eq!(sum.run_id, new.0); |
| 513 |
assert_eq!(sum.sha, "new5678"); |
| 514 |
assert_eq!(sum.phase, "compiling"); |
| 515 |
assert_eq!(sum.result, "building"); |
| 516 |
} |
| 517 |
|
| 518 |
#[tokio::test] |
| 519 |
async fn get_unknown_id_is_none() { |
| 520 |
let pool = pool().await; |
| 521 |
assert!(get(&pool, RunId(999)).await.unwrap().is_none()); |
| 522 |
} |
| 523 |
|
| 524 |
#[tokio::test] |
| 525 |
async fn failure_summary_is_bounded() { |
| 526 |
let pool = pool().await; |
| 527 |
let id = create(&pool, "abc1234").await.unwrap(); |
| 528 |
mark_failed(&pool, id, &"x".repeat(5_000)).await.unwrap(); |
| 529 |
let v = get(&pool, id).await.unwrap().unwrap(); |
| 530 |
assert!(v.failure_summary.unwrap().len() <= 600); |
| 531 |
} |
| 532 |
} |
| 533 |
|