Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

0.9 restore preview: send verified_at (the approval a rollback undoes) (#515)

Oleksandr Ivanchenko1bdd44db28c9
diff --git a/src/server/missions/checkpoints.rs b/src/server/missions/checkpoints.rs
index e9c203f6..bdc5ec6e 100644
--- a/src/server/missions/checkpoints.rs
+++ b/src/server/missions/checkpoints.rs
@@ -1452,6 +1452,7 @@ pub async fn preview(State(state): State<AppState>, RawQuery(q): RawQuery, Path(
}
out["via"] = json!(via);
out["step_down"] = super::flight_school::step_down_preview(&state.db, &task).await;
+ out["verified_at"] = super::flight_school::approved_at(&state.db, &task).await;
Json(out).into_response()
}
Err(e) => plan_err(e),
diff --git a/src/server/missions/flight_school.rs b/src/server/missions/flight_school.rs
index b2d9b3e9..eef55559 100644
--- a/src/server/missions/flight_school.rs
+++ b/src/server/missions/flight_school.rs
@@ -462,15 +462,32 @@ pub fn one_down(tier: &str) -> Option<&'static str> {
/// owner down once per key, however many forks or restores undo that approval. Approved means
/// verified, or a passed `person` check (contract §5.1); read it before a rollback moves the task.
pub async fn approval(db: &Database, task: &Task) -> Option<String> {
+ approval_with_time(db, task).await.map(|(key, _)| key)
+}
+
+/// [`approval`]'s key with the time of that approval: the task's `finished_at` when verified, else
+/// the passed `person` check's `finished_at`. The restore preview sends the time as `verified_at`.
+pub async fn approval_with_time(db: &Database, task: &Task) -> Option<(String, Option<chrono::DateTime<chrono::Utc>>)> {
if task.state == "verified" {
- return Some(format!("verified@{}", task.finished_at.map(|t| t.to_rfc3339()).unwrap_or_default()));
+ let at = task.finished_at;
+ return Some((format!("verified@{}", at.map(|t| t.to_rfc3339()).unwrap_or_default()), at));
}
let checks = db.list_checks(&task.id).await.unwrap_or_default();
checks
.iter()
.filter(|c| c.kind == "person" && c.status == "passed")
.max_by_key(|c| c.finished_at)
- .map(|c| format!("check:{}@{}", c.id, c.finished_at.map(|t| t.to_rfc3339()).unwrap_or_default()))
+ .map(|c| (format!("check:{}@{}", c.id, c.finished_at.map(|t| t.to_rfc3339()).unwrap_or_default()), c.finished_at))
+}
+
+/// When the approval a rollback of `task` would undo was given (the [`approval_with_time`] time),
+/// as the restore preview's `verified_at`; null when it isn't approved. It stays set after the
+/// approval's one step-down was used: the task was still approved at that time.
+pub async fn approved_at(db: &Database, task: &Task) -> Value {
+ match approval_with_time(db, task).await {
+ Some((_, Some(at))) => json!(at),
+ _ => Value::Null,
+ }
}
/// The owner, class and tiers a rollback of `task` would drop between, if it drops at all.
diff --git a/tests/mission_checkpoints_test.rs b/tests/mission_checkpoints_test.rs
index c3fbe41a..651d155d 100644
--- a/tests/mission_checkpoints_test.rs
+++ b/tests/mission_checkpoints_test.rs
@@ -1108,6 +1108,62 @@ async fn concurrent_forks_of_one_verification_step_the_owner_down_once() {
assert!(e.state.db.stepped_down_for(&ids[0], "verified@other").await.unwrap());
}
+/// #503 follow-up (Ada, Nov 11): the restore preview sends `verified_at`, the time of the approval
+/// a rollback would undo, the same approval the R8b step-down is keyed on: the task's `finished_at`
+/// when verified, else the passed `person` check's `finished_at`; null when it isn't approved. It
+/// stays set after that approval's one step-down was used.
+#[tokio::test]
+async fn the_restore_preview_says_when_the_approval_it_would_undo_was_given() {
+ let at = |v: &Value| v.as_str().map(|s| chrono::DateTime::parse_from_rfc3339(s).unwrap_or_else(|e| panic!("{s}: {e}")).with_timezone(&chrono::Utc));
+ let e = setup().await;
+
+ // Verified: the time it was verified
+ let plan = json!([{"title": "A", "owner": e.bot.id, "checks": [{"kind": "file_exists", "spec": {"path": "a.txt"}}]}]);
+ let (mid, ids) = mission(&e, &e.bot.id, plan).await;
+ run(&e, &mid, &ids[0], &[("a.txt", Some("v1"))]).await;
+ let t = task(&e, &ids[0]).await;
+ assert_eq!(t["task"]["state"], "verified", "{t}");
+ let verified = at(&t["task"]["finished_at"]).expect("a verified task has finished_at");
+ let cid = id(of_kind(&checkpoints(&e, &ids[0]).await, "before_run")[0]);
+ let p = preview(&e, &ids[0], &cid).await;
+ assert_eq!(at(&p["verified_at"]), Some(verified), "{p}");
+ assert!(!p["step_down"].is_null(), "{p}");
+
+ // After the approval's one step-down: no drop left, but it was still approved then
+ let uri = format!("/api/tasks/{}/fork", ids[0]);
+ let (s, r, _) = send(&e.app, "POST", &uri, None, Some("k-verified-at"), Some(json!({"from_checkpoint": cid, "preview_sha256": p["preview_sha256"]}))).await;
+ assert_eq!(s, StatusCode::OK, "{r}");
+ assert!(!r["stepped_down"].is_null(), "{r}");
+ let again = preview(&e, &ids[0], &cid).await;
+ assert!(again["step_down"].is_null(), "{again}");
+ assert_eq!(at(&again["verified_at"]), Some(verified), "{again}");
+
+ // Not approved: null
+ let (_mid, tid, _dir) = cli_mission(&e).await;
+ assert_ne!(state_of(&e, &tid).await, "verified");
+ let cid = id(of_kind(&checkpoints(&e, &tid).await, "before_run")[0]);
+ let p = preview(&e, &tid, &cid).await;
+ assert!(p.as_object().unwrap().contains_key("verified_at"), "sent, as null: {p}");
+ assert!(p["verified_at"].is_null(), "{p}");
+ assert!(p["step_down"].is_null(), "{p}");
+
+ // Approved by a passed person check: that check's time
+ let check = kwnt::db::missions::NewCheck {
+ task_id: tid.clone(),
+ kind: "person".into(),
+ spec: json!({}),
+ required: true,
+ approved: true,
+ requested_by: "person".into(),
+ approved_by: None,
+ };
+ let c = e.state.db.add_check(&check).await.unwrap();
+ e.state.db.finish_check(&c.id, "passed", None, "person", None).await.unwrap();
+ let checked = e.state.db.list_checks(&tid).await.unwrap().into_iter().find(|x| x.id == c.id).unwrap().finished_at.expect("a finished check has finished_at");
+ let p = preview(&e, &tid, &cid).await;
+ assert_eq!(at(&p["verified_at"]), Some(checked), "{p}");
+}
+
// V2: the verifier is on unless the person saved "off", and a handed-in task waits in review on
// its hold until a pass verdict or the person's Verify. Every other test here turns it off in
// setup, because they are about checkpoints, not the verifier.