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.

Keep board notes under load; give a fast-failing hand-off its card

Lin (kwant bot)45c5c23ee27f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c04261fc..db7dc92e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
times per message (was 2), and one message can start up to 16 hand-offs (was 8). Past that, instead of
"Send a new message to go on", the chat shows a notice with a Continue button (or say "continue").
+### Fixed
+- **Board notes aren't lost when kwant is busy.** A task's notes (such as "the hand-off finished;
+ unblock to review its result") could be dropped with "database is locked" while other work was
+ saving at the same moment. A hand-off that fails before the board sees it start now gets its card
+ too, failed so it offers Retry.
+
## [Mac 0.2.0] - 2026-10-28
### Added
diff --git a/src/db/missions.rs b/src/db/missions.rs
index 68ab84fa..fbd9ff7f 100644
--- a/src/db/missions.rs
+++ b/src/db/missions.rs
@@ -1125,9 +1125,21 @@ impl Database {
// Events
pub async fn log_task_event(&self, task_id: &str, kind: &str, actor: &str, detail: &Value) -> Result<(), DbError> {
- let mut tx = self.pool.begin().await?;
- insert_event(&mut tx, task_id, None, kind, actor, None, None, detail).await?;
- tx.commit().await?;
+ // One statement that writes, so it takes SQLite's write lock up front (waiting on
+ // busy_timeout): a transaction that read the mission first failed at once with "database
+ // is locked" when another write landed in between, and the event was lost
+ sqlx::query(
+ "INSERT INTO task_events (task_id, mission_id, kind, actor, from_state, to_state, detail, created_at) \
+ VALUES (?, (SELECT mission_id FROM tasks WHERE id = ?), ?, ?, NULL, NULL, ?, ?)",
+ )
+ .bind(task_id)
+ .bind(task_id)
+ .bind(kind)
+ .bind(actor)
+ .bind(detail.to_string())
+ .bind(Utc::now())
+ .execute(&self.pool)
+ .await?;
Ok(())
}
diff --git a/src/server/missions/listener.rs b/src/server/missions/listener.rs
index 56446137..6eb05483 100644
--- a/src/server/missions/listener.rs
+++ b/src/server/missions/listener.rs
@@ -198,16 +198,25 @@ pub async fn on_delegation(state: &AppState, delegation_id: &str) {
}
let _serial = SERIAL.lock().await;
let Ok(Some(d)) = state.db.get_delegation(delegation_id).await else { return };
- let task = state.db.task_by_source("delegation", &d.id).await.ok().flatten();
+ let mut task = state.db.task_by_source("delegation", &d.id).await.ok().flatten();
let live = matches!(d.status.as_str(), "queued" | "running");
- match (task, d.status.as_str()) {
- (None, _) if live => {
- let Ok(Some(space)) = state.db.space_of_kwant(&d.specialist_kwant_id).await else { return };
- let mission = mission_of_holder(state, &d.lead_kwant_id).await;
- let title = if d.title.trim().is_empty() { d.task.lines().next().unwrap_or("Hand-off").to_string() } else { d.title.clone() };
- let place = (None, None, Some(d.thread_id.clone()));
- start_held(state, &space, mission, &title, &d.task, &d.specialist_kwant_id, "delegation", &d.id, place, &d.thread_id).await;
+ if task.is_none() {
+ // A hand-off that ended before its first update got here (it failed at once) still gets
+ // its card, which then takes the end below
+ if !live && !matches!(d.status.as_str(), "completed" | "failed" | "cancelled" | "interrupted") {
+ return;
+ }
+ let Ok(Some(space)) = state.db.space_of_kwant(&d.specialist_kwant_id).await else { return };
+ let mission = mission_of_holder(state, &d.lead_kwant_id).await;
+ let title = if d.title.trim().is_empty() { d.task.lines().next().unwrap_or("Hand-off").to_string() } else { d.title.clone() };
+ let place = (None, None, Some(d.thread_id.clone()));
+ task = start_held(state, &space, mission, &title, &d.task, &d.specialist_kwant_id, "delegation", &d.id, place, &d.thread_id).await;
+ if live || task.is_none() {
+ return;
}
+ }
+ match (task, d.status.as_str()) {
+ (None, _) => {}
(Some(t), _) if live && t.state == "ready" => hold(state, &t, &d.specialist_kwant_id, &d.thread_id).await,
// Sent back to Ready after the hand-off ended (the person unblocked or retried the card,
// or AutoFix retried it): the board never runs hand-offs itself, so it goes on here
diff --git a/tests/handoff_continue_test.rs b/tests/handoff_continue_test.rs
index 0890718c..c37fe5ab 100644
--- a/tests/handoff_continue_test.rs
+++ b/tests/handoff_continue_test.rs
@@ -316,3 +316,32 @@ async fn a_stuck_ready_hand_off_card_is_carried_on_or_blocked_with_a_reason() {
let t = card(&env, &gone.id).await.unwrap();
assert_eq!((t.state.as_str(), t.blocked_reason.as_deref()), ("blocked", Some(missions::listener::CANT_CARRY_ON)));
}
+
+/// A hand-off that fails before the board sees it running (a fast failure under load) still
+/// gets its card, failed so it offers Retry.
+#[tokio::test]
+async fn a_hand_off_that_failed_before_the_board_saw_it_gets_a_failed_card() {
+ let env = setup().await;
+ 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: "Audit the pricing page".into(),
+ title: "Pricing audit".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, "failed", Some("the CLI exited"), None).await.unwrap());
+ missions::listener::on_delegation(&env.state, &d.id).await;
+ let first = card(&env, &d.id).await.expect("a card");
+ assert_eq!(first.state, "failed");
+ // Seen again (a late duplicate update): the same card, no second one
+ missions::listener::on_delegation(&env.state, &d.id).await;
+ let again = card(&env, &d.id).await.unwrap();
+ assert_eq!((again.id.as_str(), again.state.as_str()), (first.id.as_str(), "failed"));
+}