feat: say why a restore failed, and add the user_schemas reset mode

- the agent reports the client's primary ERROR/FATAL line with a failed restore step; the hub keeps it
  as failure_reason (administrators only), logs it and audits restore.failed. psql runs with
  VERBOSITY=terse and DETAIL/CONTEXT/statement text is dropped so row data never travels
- reset mode user_schemas empties every non-system schema in the restore transaction, so multi-schema
  databases restore without DROP DATABASE; proven against a real PostgreSQL server

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 12:29:48 +07:00
parent fb277249f7
commit 43709aeeab
9 changed files with 565 additions and 31 deletions

View File

@@ -412,6 +412,10 @@ impl Lab {
assert_eq!(status, StatusCode::NO_CONTENT);
}
fn audit_log(&self) -> Vec<(String, String)> {
self.state.db.audit_log_events()
}
fn restore_jobs(&self) -> i64 {
self.scalar("SELECT COUNT(*) FROM restore_jobs")
}
@@ -1518,3 +1522,125 @@ async fn only_an_administrator_can_change_a_profile() {
StatusCode::UNAUTHORIZED
);
}
// ---- why a restore failed ----
impl Lab {
async fn report_with_reason(
&self,
job: &str,
token: &str,
progress: &str,
reason: &str,
) -> StatusCode {
self.agent_json(
"POST",
&format!("/api/v1/wormhole/restore/{job}/report"),
&[
("x-tessera-restore-token", token.to_string()),
("x-tessera-failure-reason", reason.to_string()),
],
json!(progress),
)
.await
.0
}
}
#[tokio::test]
async fn a_failed_restore_says_why_to_administrators_only() {
let lab = fresh().await;
let (qa, _) = lab.refresher("qa");
let run = lab.start(&qa, "why").await.1["id"]
.as_str()
.unwrap()
.to_string();
lab.capture(&Lab::production_identity(), false)
.await
.unwrap();
lab.tick().await;
let (job, token) = lab.restore_until_ready().await.unwrap();
assert_eq!(lab.apply(&job, &token).await, StatusCode::NO_CONTENT);
let reason = "psql failed (exit status: 3): ERROR: schema \"drizzle\" already exists";
assert_eq!(
lab.report_with_reason(&job, &token, "apply_failed", reason)
.await,
StatusCode::NO_CONTENT
);
let (status, view) = lab
.send(
&lab.admin(),
"GET",
&format!("/api/v1/restore-jobs/{job}"),
None,
)
.await;
assert_eq!(status, StatusCode::OK);
assert_eq!(view["phase"], "apply_failed");
// whitespace is normalised on the way in (psql pads "ERROR: ")
assert_eq!(
view["failure_reason"],
"psql failed (exit status: 3): ERROR: schema \"drizzle\" already exists"
);
assert!(lab
.audit_log()
.iter()
.any(|(event, detail)| event == "restore.failed" && detail.contains("drizzle")));
// the person who started the refresh sees that it failed, never the database's own words
lab.tick().await;
let (_, seen) = lab
.send(&qa, "GET", &format!("/api/v1/refresh-runs/{run}"), None)
.await;
assert_eq!(seen["status"], "recovery_required", "{seen}");
assert!(
!seen.to_string().contains("drizzle") && seen.get("failure_reason").is_none(),
"{seen}"
);
}
#[tokio::test]
async fn a_failure_reason_is_only_kept_for_failures_and_is_cleaned_and_bounded() {
let lab = fresh().await;
lab.start(&lab.admin(), "clean").await;
lab.capture(&Lab::production_identity(), false)
.await
.unwrap();
lab.tick().await;
let (job, token) = lab.restore_until_ready().await.unwrap();
// a heartbeat that carries a reason does not create one
assert_eq!(
lab.report_with_reason(&job, &token, "heartbeat", "ignored")
.await,
StatusCode::NO_CONTENT
);
let admin = lab.admin();
let uri = format!("/api/v1/restore-jobs/{job}");
assert!(lab
.send(&admin, "GET", &uri, None)
.await
.1
.get("failure_reason")
.is_none());
assert_eq!(lab.apply(&job, &token).await, StatusCode::NO_CONTENT);
let long = format!("ERROR:\t{}\tend", "x".repeat(2000));
assert_eq!(
lab.report_with_reason(&job, &token, "apply_failed", &long)
.await,
StatusCode::NO_CONTENT
);
let reason = lab.send(&admin, "GET", &uri, None).await.1["failure_reason"]
.as_str()
.unwrap()
.to_string();
assert!(
reason.len() <= 300 && reason.starts_with("ERROR: xxx"),
"{}",
reason.len()
);
assert!(
reason.chars().all(|c| c.is_ascii() && !c.is_control()),
"{reason:?}"
);
}