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.

Delegations: a failed or cancelled task can be retried within the minute

Lin (kwant bot)b6ac6c3f2b6b
diff --git a/src/db/threads.rs b/src/db/threads.rs
index 04979ace..14266f6f 100644
--- a/src/db/threads.rs
+++ b/src/db/threads.rs
@@ -336,7 +336,9 @@ impl Database {
.await?)
}
- /// The same task from the same lead to the same specialist, from the same chat, since `since`.
+ /// The same task from the same lead to the same specialist, from the same chat, since `since`,
+ /// that is still open or finished with a result. One that failed, was cancelled or was
+ /// interrupted doesn't count, so retrying it right away is allowed.
#[allow(clippy::too_many_arguments)]
pub async fn find_recent_delegation(
&self,
@@ -351,7 +353,8 @@ impl Database {
Ok(sqlx::query_as::<_, Delegation>(
"SELECT * FROM delegations WHERE lead_kwant_id = ? AND specialist_kwant_id = ? \
AND origin_channel_id IS ? AND origin_kwant_id IS ? AND origin_thread_id IS ? \
- AND task = ? AND created_at >= ? ORDER BY created_at DESC LIMIT 1",
+ AND task = ? AND created_at >= ? AND status IN ('queued', 'running', 'completed') \
+ ORDER BY created_at DESC LIMIT 1",
)
.bind(lead)
.bind(specialist)
diff --git a/src/server/delegation.rs b/src/server/delegation.rs
index 33b2eaed..9903f0e7 100644
--- a/src/server/delegation.rs
+++ b/src/server/delegation.rs
@@ -22,7 +22,8 @@ use serde_json::{json, Value};
pub const MAX_DEPTH: i64 = 2;
/// Unfinished delegations one lead may have at a time.
pub const MAX_OPEN_PER_LEAD: i64 = 5;
-/// An identical delegation (same lead, specialist, chat and task) within this many seconds is refused.
+/// An identical delegation (same lead, specialist, chat and task) within this many seconds is refused,
+/// unless the earlier one failed, was cancelled or was interrupted.
pub const DEDUPE_SECS: i64 = 60;
/// How much of the specialist's reply is copied into the origin chat; the thread keeps it all.
pub const RESULT_CHARS: usize = 8000;
diff --git a/tests/delegation_test.rs b/tests/delegation_test.rs
index a865f8f5..33eddac2 100644
--- a/tests/delegation_test.rs
+++ b/tests/delegation_test.rs
@@ -534,6 +534,36 @@ async fn delegated_tasks_are_framed_and_repeats_are_refused() {
delegate(&env, "Another task REPLYTAG:other").await;
}
+/// Dedupe only covers a repeat of a task that is still open or already answered: a task that was
+/// cancelled (or failed, or interrupted) can be sent again straight away.
+#[tokio::test]
+async fn a_cancelled_task_can_be_retried_within_the_minute() {
+ let env = setup().await;
+ let release = env.release_file("retry");
+ let task = format!("Slow LONGTASK:{} REPLYTAG:retry", release.display());
+ let id = delegate(&env, &task).await["id"].as_str().unwrap().to_string();
+ wait_for("the delegated turn to run", || async { delegation_status(&env, &id).await == "running" }).await;
+ // While it runs, the same task is refused
+ let body = json!({
+ "lead_kwant_id": env.lead,
+ "specialist_kwant_id": env.specialist,
+ "task": task,
+ "kwant_id": env.lead,
+ "continue_lead": false,
+ });
+ let (s, d) = call(&env.app, "POST", "/api/delegations", Some(body.clone())).await;
+ assert_eq!(s, StatusCode::CONFLICT, "{d}");
+ let (s, d) = call(&env.app, "POST", &format!("/api/delegations/{id}/cancel"), None).await;
+ assert_eq!((s, d["status"].as_str()), (StatusCode::OK, Some("cancelled")));
+ // Once cancelled, the same task goes through again
+ let (s, d) = call(&env.app, "POST", "/api/delegations", Some(body)).await;
+ assert!(s.is_success(), "{s} {d}");
+ let again = d["id"].as_str().unwrap().to_string();
+ assert_ne!(again, id);
+ std::fs::write(&release, "go").unwrap();
+ wait_for("the retry to finish", || async { delegation_status(&env, &again).await == "completed" }).await;
+}
+
#[tokio::test]
async fn delegation_tools_enforce_their_rules() {
let env = setup().await;