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-offs: create the whole round before any of it runs

Lin (kwant bot)1b79979993a7
diff --git a/src/server/delegation.rs b/src/server/delegation.rs
index 96b854ca..f5ebebef 100644
--- a/src/server/delegation.rs
+++ b/src/server/delegation.rs
@@ -159,6 +159,47 @@ pub struct StartRequest {
/// Checks a request, creates the thread and the delegation, and starts the specialist's turn
/// in the background. Returns at once; the error is a message fit for the user or the agent.
pub async fn start(state: &AppState, req: StartRequest) -> Result<Delegation, (StatusCode, String)> {
+ Ok(create(state, req).await?.start(state).await)
+}
+
+/// A delegation [`create`] made whose turn hasn't started yet.
+pub(super) struct Created {
+ pub delegation: Delegation,
+ next: Next,
+}
+
+enum Next {
+ /// The specialist's turn (lead, specialist)
+ Run(Box<(Kwant, Kwant)>),
+ /// Nothing while it waits for this delegation, unless that already ended
+ Wait(String),
+}
+
+impl Created {
+ /// Starts the specialist's turn in the background, or for a task that waits, lets it go on if
+ /// what it waits for ended meanwhile.
+ pub(super) async fn start(self, state: &AppState) -> Delegation {
+ let Created { delegation: d, next } = self;
+ match next {
+ Next::Run(pair) => {
+ let (lead, specialist) = *pair;
+ spawn_run(state, d.clone(), lead, specialist);
+ d
+ }
+ Next::Wait(prereq) => {
+ if !state.db.get_delegation(&prereq).await.ok().flatten().is_some_and(|p| p.is_open()) {
+ release_dependents(state, &prereq).await;
+ }
+ state.db.get_delegation(&d.id).await.ok().flatten().unwrap_or(d)
+ }
+ }
+ }
+}
+
+/// [`start`] without starting the turn: a round of hand-offs creates every delegation before any
+/// runs, so one that finishes at once can't report to the lead while a sibling doesn't exist yet
+/// (see `finished_batch`).
+pub(super) async fn create(state: &AppState, req: StartRequest) -> Result<Created, (StatusCode, String)> {
let bad = |m: &str| Err((StatusCode::BAD_REQUEST, m.to_string()));
if req.lead.id == req.specialist.id {
return bad("A bot can't delegate to itself.");
@@ -326,16 +367,11 @@ pub async fn start(state: &AppState, req: StartRequest) -> Result<Delegation, (S
emit_thread(state, &thread);
emit_delegation(state, &d);
tracing::info!(delegation = %d.id, lead = %req.lead.id, specialist = %req.specialist.id, depth = d.depth, after = ?d.after_delegation_id, "delegation started");
- if let Some(p) = prereq {
- // It may have ended while this one was being set up
- if !state.db.get_delegation(&p.id).await.ok().flatten().is_some_and(|p| p.is_open()) {
- release_dependents(state, &p.id).await;
- }
- return Ok(state.db.get_delegation(&d.id).await.ok().flatten().unwrap_or(d));
- }
-
- spawn_run(state, d.clone(), req.lead, req.specialist);
- Ok(d)
+ let next = match prereq {
+ Some(p) => Next::Wait(p.id),
+ None => Next::Run(Box::new((req.lead, req.specialist))),
+ };
+ Ok(Created { delegation: d, next })
}
/// Starts a delegation's run in the background. A plain function, so the futures of [`run`] and
diff --git a/src/server/routes.rs b/src/server/routes.rs
index f3254238..ae8c8663 100644
--- a/src/server/routes.rs
+++ b/src/server/routes.rs
@@ -1423,6 +1423,7 @@ pub(super) async fn delegate_mentions(
targets.insert(at, k);
}
let mut started: std::collections::HashMap<String, String> = std::collections::HashMap::new();
+ let mut created = Vec::new();
let mut sent = 0;
let user = truncate_chars(user_prompt.trim(), 3000);
let task = match relay {
@@ -1523,15 +1524,21 @@ pub(super) async fn delegate_mentions(
after,
relayed_from: relay.map(|r| r.source.id.clone()),
};
- match super::delegation::start(&scope.state, req).await {
- Ok(d) => {
- started.insert(specialist.id.clone(), d.id);
+ match super::delegation::create(&scope.state, req).await {
+ Ok(c) => {
+ started.insert(specialist.id.clone(), c.delegation.id.clone());
sent += 1;
room -= 1;
+ created.push(c);
}
Err((_, why)) => skipped.push((specialist.name.clone(), why.trim_end_matches('.').to_string())),
}
}
+ // Only now, with the whole round created: a teammate that finished while the next one was
+ // being created resumed the lead without it (CI on 8f41657: the Continue notice left out SEO)
+ for c in created {
+ c.start(&scope.state).await;
+ }
report_skipped(scope, skipped).await;
if !capped.is_empty() {
super::budget::pause_capped(scope, lead, &capped, this_request.len() + sent, text, user_prompt).await;