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.

Hand-off review: fix two races behind handoff_review_test flakes

Lin (kwant bot)107266956cb9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 41788402..01a56ee9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -57,6 +57,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
page; screen readers read it as "Made in Germany with love".
### Fixed
+- **A hand-off card the lead reviewed no longer loses its fixed result.** When the lead's review sent
+ work back and the lead's earlier results turn ran late, that turn put the card back in Review while the
+ teammate was still fixing it, and the fix's result was never attached. The lead's turn now also makes a
+ hand-off's card if the board hasn't seen it yet, so the lead reviews it right away instead of minutes
+ later. Both races made `handoff_review_test` fail now and then on CI.
- **Truth Ledger: outside facts can't be laundered or erased.** A bot editing a note that ever came
from outside text, or re-adding its exact text after deleting it, lands it as proposed again, even
after you accepted an earlier version. Only you can take a fact out of "outside", and your decision
diff --git a/src/server/missions/listener.rs b/src/server/missions/listener.rs
index 7ad94354..3031688d 100644
--- a/src/server/missions/listener.rs
+++ b/src/server/missions/listener.rs
@@ -234,6 +234,12 @@ pub fn kick_handoff(state: &AppState, task: &Task) {
tokio::spawn(async move { on_delegation(&state, &id).await });
}
+/// [`on_delegation`] boxed, for [`lead_review_note`]: a send-back runs the hand-off, whose end
+/// resumes the lead, whose turn comes back to [`lead_review_note`].
+fn on_delegation_boxed<'a>(state: &'a AppState, delegation_id: &'a str) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
+ Box::pin(on_delegation(state, delegation_id))
+}
+
pub async fn on_delegation(state: &AppState, delegation_id: &str) {
if !super::enabled(state).await {
return;
@@ -402,13 +408,22 @@ pub async fn lead_review_note(state: &AppState, d: &crate::db::threads::Delegati
if !super::enabled(state).await || d.status != "completed" {
return None;
}
+ // The board may not have seen the hand-off at all yet (its listener lags behind the run): the
+ // card is made now, or the lead's turn would leave it out and nothing would ask until the sweep
+ if state.db.task_by_source("delegation", &d.id).await.ok().flatten().is_none() {
+ on_delegation_boxed(state, &d.id).await;
+ }
// The board may not have seen the end yet: its card goes to review first. Not through
// [`on_delegation`], whose send-back runs the hand-off, whose end comes back here.
let t = {
let _serial = SERIAL.lock().await;
+ // `d` is from when the batch ended; a review may have sent it back since. Only the end
+ // that is still current puts the card in review, or a late turn would push the card of a
+ // fix still running to review, and the fix's own result would then never be attached.
+ let d = state.db.get_delegation(&d.id).await.ok().flatten().filter(|now| now.status == "completed" && now.ended_at == d.ended_at)?;
let t = state.db.task_by_source("delegation", &d.id).await.ok().flatten()?;
if matches!(t.state.as_str(), "working" | "needs_you") {
- attach_result(state, &t, d).await;
+ attach_result(state, &t, &d).await;
finish(state, &t, "review", "the hand-off finished").await;
}
state.db.get_task(&t.id).await.ok().flatten()?
diff --git a/tests/handoff_review_test.rs b/tests/handoff_review_test.rs
index c534973d..c9c42c9a 100644
--- a/tests/handoff_review_test.rs
+++ b/tests/handoff_review_test.rs
@@ -485,3 +485,64 @@ async fn a_batch_of_two_cards_gets_one_prompt_and_one_reminder() {
assert_eq!(review_asks_for(t), 2, "no card was asked about on its own");
}
}
+
+/// A hand-off made in the database, finished, that no board event has seen yet (no card).
+async fn ended_unseen(env: &Env, task: &str, title: &str) -> kwnt::db::threads::Delegation {
+ let new = kwnt::db::threads::NewDelegation {
+ lead_kwant_id: env.lead.clone(),
+ specialist_kwant_id: env.specialist.clone(),
+ origin_channel_id: None,
+ origin_kwant_id: Some(env.lead.clone()),
+ origin_thread_id: None,
+ team_id: None,
+ task: task.into(),
+ title: title.into(),
+ continue_lead: true,
+ depth: 1,
+ follow_up_at: None,
+ };
+ let (_, d) = env.state.db.create_delegation(&new).await.unwrap();
+ assert!(env.state.db.finish_delegation(&d.id, "completed", None, None).await.unwrap());
+ env.state.db.get_delegation(&d.id).await.unwrap().unwrap()
+}
+
+/// The lead's results turn can run before the board's listener has seen the hand-off (a slow
+/// listener; the batch test timed out on CI for this): its card is made then, in review with its
+/// result, and the lead is asked about it in that turn instead of only by the sweep minutes later.
+#[tokio::test]
+async fn the_lead_is_asked_about_a_card_the_board_had_not_seen_yet() {
+ let env = setup().await;
+ let d = ended_unseen(&env, "Index the archive UNSEENMARK", "Archive index").await;
+ assert!(card(&env, &d.id).await.is_none(), "no event made its card");
+ let note = missions::listener::lead_review_note(&env.state, &d, "Specialist").await;
+ let t = card(&env, &d.id).await.expect("the results turn made the card");
+ assert!(note.is_some_and(|n| n.contains(&t.id)), "the lead is asked about it in this turn");
+ assert_eq!(t.state, "review");
+ assert_eq!(result_evidence(&env, &t.id).await.len(), 1, "with its result");
+ assert_eq!(asks_logged(&env, &t.id).await, 1);
+}
+
+/// A results turn that runs late, after the lead's review already sent the work back, carries
+/// the hand-off as it was when it ended. It must not push the card of the fix still running to
+/// review: the fix's own result would then never be attached (CI, #170: one result, not two).
+#[tokio::test]
+async fn a_late_results_turn_leaves_the_card_of_a_running_fix_alone() {
+ let env = setup().await;
+ let ended = ended_unseen(&env, "Fix the importer STALEMARK", "Importer fix").await;
+ missions::listener::on_delegation(&env.state, &ended.id).await;
+ assert_eq!(card_state(&env, &ended.id).await, "review");
+ let t = card(&env, &ended.id).await.unwrap();
+ let ev = result_evidence(&env, &t.id).await;
+ let release = tempfile::tempdir().unwrap().keep().join("fixed");
+ let (out, err) = review(&env, &env.lead, &t.id, "fail", &ev, &format!("Handle empty rows LONGTASK:{} ", release.display())).await;
+ assert!(!err, "{out}");
+ wait_for("the fix to run", || async { status_of(&env, &ended.id).await == "running" && card_state(&env, &ended.id).await == "working" }).await;
+
+ let note = missions::listener::lead_review_note(&env.state, &ended, "Specialist").await;
+ assert!(note.is_none(), "the old end isn't up for review any more");
+ assert_eq!(card_state(&env, &ended.id).await, "working", "the fix's card stays with the fix");
+
+ std::fs::write(&release, "go").unwrap();
+ wait_for("the fixed result in review", || async { card_state(&env, &ended.id).await == "review" }).await;
+ assert_eq!(result_evidence(&env, &t.id).await.len(), 2, "the fix's result is attached too");
+}