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 re-queued after a restart are told the earlier run may have partly happened

Lin (kwant bot)d6571aec33c3
diff --git a/src/server/delegation.rs b/src/server/delegation.rs
index 5fbb3f78..41ae8c44 100644
--- a/src/server/delegation.rs
+++ b/src/server/delegation.rs
@@ -260,13 +260,23 @@ pub async fn start(state: &AppState, req: StartRequest) -> Result<Delegation, (S
let job = state.turns.start_job([d.id.clone(), thread.id.clone()]);
let runner_state = state.clone();
let (lead, specialist, delegation) = (req.lead, req.specialist, d.clone());
- tokio::spawn(async move { run(runner_state, job, delegation, lead, specialist, false).await });
+ tokio::spawn(async move { run(runner_state, job, delegation, lead, specialist, false, false).await });
Ok(d)
}
/// The background part: the specialist's turn in the thread, then the report in the origin chat.
/// `retried`: the run after a usage limit reset, which never waits for one again.
-async fn run(state: AppState, job: super::turns::JobGuard, d: Delegation, lead: Kwant, specialist: Kwant, retried: bool) {
+/// The task's prompt says so when a restart cut an earlier run short (`after_restart`): that run
+/// may have done part of the work already.
+async fn run(
+ state: AppState,
+ job: super::turns::JobGuard,
+ d: Delegation,
+ lead: Kwant,
+ specialist: Kwant,
+ retried: bool,
+ after_restart: bool,
+) {
let scope = match scope_for(&state, &specialist, None, Some(d.thread_id.clone()), &job, d.depth).await {
Ok(s) => s,
Err(why) => return finish(&state, &d, &specialist, Err(why)).await,
@@ -282,10 +292,11 @@ async fn run(state: AppState, job: super::turns::JobGuard, d: Delegation, lead:
});
let prompt = format!(
"{lead} delegated this task to you. Work on it here; when you finish, your reply is posted back to {lead} \
- and the chat it came from, so make it a complete, self-contained answer.\n\nTask: {title}\n\n{task}",
+ and the chat it came from, so make it a complete, self-contained answer.\n\nTask: {title}\n\n{task}{again}",
lead = lead.name,
title = d.title,
task = d.task,
+ again = if after_restart { RESTARTED_NOTE } else { "" },
);
let mut opts = TurnOpts::default();
opts.on_started = Some(on_started);
@@ -322,6 +333,11 @@ async fn run(state: AppState, job: super::turns::JobGuard, d: Delegation, lead:
finish(&state, &d, &specialist, outcome).await;
}
+/// Added to a delegated task that runs again because kwnt restarted in the middle of it.
+pub const RESTARTED_NOTE: &str = "\n\n[kwnt restarted while an earlier run of this task was in progress, so it may have partly run: \
+files may already be written, messages sent or settings changed. Check what is already done before redoing anything, \
+and don't repeat a step that can't be undone (sending, posting, deleting, paying) unless you've confirmed it didn't happen.]";
+
async fn finish(state: &AppState, d: &Delegation, specialist: &Kwant, outcome: Result<String, String>) {
let (status, error) = match &outcome {
Ok(_) => ("completed", None),
@@ -582,7 +598,7 @@ pub async fn interrupt_open(state: &AppState) {
meta("delegation_started", &d, &specialist),
)
.await;
- restart(state, d, false).await;
+ restart(state, d, false, true).await;
}
}
@@ -604,11 +620,12 @@ pub async fn retry_parked(state: &AppState, delegation_id: &str) -> bool {
meta("delegation_started", &d, &specialist),
)
.await;
- restart(state, d, true).await
+ restart(state, d, true, false).await
}
-/// Runs an open delegation's task again in its thread.
-async fn restart(state: &AppState, d: Delegation, retried: bool) -> bool {
+/// Runs an open delegation's task again in its thread. `after_restart`: kwnt restarted during
+/// the earlier run (see [`RESTARTED_NOTE`]); a usage-limit retry ran nothing before it parked.
+async fn restart(state: &AppState, d: Delegation, retried: bool, after_restart: bool) -> bool {
let (Ok(Some(lead)), Ok(Some(specialist))) =
(state.db.get_kwant(&d.lead_kwant_id).await, state.db.get_kwant(&d.specialist_kwant_id).await)
else {
@@ -617,7 +634,7 @@ async fn restart(state: &AppState, d: Delegation, retried: bool) -> bool {
};
let job = state.turns.start_job([d.id.clone(), d.thread_id.clone()]);
tracing::info!(delegation = %d.id, retried, "delegation restarted");
- tokio::spawn(run(state.clone(), job, d, lead, specialist, retried));
+ tokio::spawn(run(state.clone(), job, d, lead, specialist, retried, after_restart));
true
}
diff --git a/tests/delegation_test.rs b/tests/delegation_test.rs
index 65313ae7..33006665 100644
--- a/tests/delegation_test.rs
+++ b/tests/delegation_test.rs
@@ -648,6 +648,11 @@ async fn follow_ups_and_restarts_report_in_the_origin_chat() {
let lead_chat = env.state.db.list_messages(&env.lead, 30).await.unwrap();
assert!(lead_chat.iter().any(|m| m.text.contains("Cut short") && m.text.contains("retrying it once now")));
assert!(lead_chat.iter().any(|m| m.text.contains("answer restarted")));
+ // Its prompt warns that the cut-short run may have done part of the work; a first run's doesn't
+ let rerun = prompts_with("Task: Cut short");
+ assert!(!rerun.is_empty() && rerun.iter().all(|p| p.contains(delegation::RESTARTED_NOTE)), "{rerun:?}");
+ let first = prompts_with("REPLYTAG:two");
+ assert!(!first.is_empty() && first.iter().all(|p| !p.contains("kwnt restarted while")), "{first:?}");
// Only once: one cut short a second time becomes interrupted
let (_, twice) = env.state.db.create_delegation(&left_open("Cut twice")).await.unwrap();