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.

Routing notes all begin "(kwant routing note:"

Lin (kwant bot)59e669208c65
diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs
index 94ce1c02..232b4a4b 100644
--- a/src/drivers/mod.rs
+++ b/src/drivers/mod.rs
@@ -95,15 +95,21 @@ pub fn drop_current_message(history: &mut Vec<crate::db::models::Message>, promp
let text = last.text.trim();
let is_current = !text.is_empty()
&& prompt.trim_start().strip_prefix(text).is_some_and(|rest| {
- rest.trim().is_empty() || KWANT_PROMPT_SUFFIXES.iter().any(|s| rest.starts_with(s))
+ rest.trim().is_empty()
+ || rest.strip_prefix("\n\n").is_some_and(|block| KWANT_PROMPT_SUFFIXES.iter().any(|s| block.starts_with(s)))
});
if last.sender_role == "user" && is_current {
history.pop();
}
}
-/// Blocks kwant adds after the person's own words in a turn's prompt (attachments, routing note).
-const KWANT_PROMPT_SUFFIXES: [&str; 2] = ["\n\n[Attached files]", "\n\n(kwant routing note:"];
+/// How every note kwant adds to a routed turn's prompt begins (routed, hand-back, hire, team
+/// building). A note that begins any other way makes the person's message show twice.
+pub const ROUTING_NOTE: &str = "(kwant routing note:";
+
+/// Blocks kwant adds after the person's own words in a turn's prompt, after a blank line
+/// (attachments, routing note).
+const KWANT_PROMPT_SUFFIXES: [&str; 2] = ["[Attached files]", ROUTING_NOTE];
/// Frames text another bot wrote, wherever it reaches a bot (history, handoffs, delegated tasks
/// and their results), so it is never mistaken for the person's own words or authority.
@@ -746,8 +752,8 @@ pub fn create_driver_for_kwant(kwant: &Kwant) -> Box<dyn Driver> {
}
#[cfg(test)]
-mod tests {
- fn said(role: &str, text: &str) -> crate::db::models::Message {
+pub(crate) mod tests {
+ pub(crate) fn said(role: &str, text: &str) -> crate::db::models::Message {
crate::db::models::Message {
id: text.into(),
channel_id: None,
diff --git a/src/router/team.rs b/src/router/team.rs
index 03cdf860..0caefadf 100644
--- a/src/router/team.rs
+++ b/src/router/team.rs
@@ -1943,4 +1943,15 @@ mod fit_tests {
assert_eq!(content_words("Is it ok to ship a 3D build at 10?"), ["3d", "build", "ship"]);
assert!(alike(&content_words("Why is CI red on my PR?"), &content_words("CI red again on the PR")));
}
+
+ /// The note follows the person's message in the lead's prompt and begins like every routing
+ /// note, so the message isn't repeated in the lead's history.
+ #[test]
+ fn the_team_building_note_doesnt_repeat_the_message_in_history() {
+ assert!(TEAM_BUILDING_NOTE.starts_with(crate::drivers::ROUTING_NOTE));
+ let asked = "Create a team to launch the podcast";
+ let mut history = vec![crate::drivers::tests::said("user", asked)];
+ crate::drivers::drop_current_message(&mut history, &format!("{asked}\n\n{TEAM_BUILDING_NOTE}"));
+ assert!(history.is_empty(), "the prompt carries the message once");
+ }
}
diff --git a/src/server/concierge.rs b/src/server/concierge.rs
index ab885718..d7eae7fc 100644
--- a/src/server/concierge.rs
+++ b/src/server/concierge.rs
@@ -166,10 +166,11 @@ pub fn similar_ids(text: &str, message_id: &str, recent: &[crate::db::routing::R
/// specialist through the hire policy, which still refuses a role the person turned down.
pub fn hire_note(ids: &[&str]) -> String {
format!(
- "(kwnt routing note: this is request {} of a kind you have answered in the last {SIMILAR_WINDOW_DAYS} days; message \
+ "{} this is request {} of a kind you have answered in the last {SIMILAR_WINDOW_DAYS} days; message \
ids: {}. Answer it first. Then, if one specialist bot would clearly do this kind of request better, call \
propose_hire once with evidence {{\"basis\": \"repeated\", \"similar_requests\": [those ids]}}. Check hire_status \
first and don't propose a role the person turned down. If propose_hire refuses, don't retry.)",
+ crate::drivers::ROUTING_NOTE,
ids.len(),
ids.join(", ")
)
@@ -218,4 +219,16 @@ mod hire_tests {
let recent = vec![ex("m2", "PR review please", false), ex("m1", "Review my PR", false)];
assert_eq!(similar_ids("Can you review this PR?", "m3", &recent), vec!["m1", "m2", "m3"]);
}
+
+ /// The Concierge's turn has the person's message once: its prompt is the message plus this
+ /// note, so history drops the copy (it didn't while the note began "(kwnt").
+ #[test]
+ fn the_hire_note_doesnt_repeat_the_message_in_history() {
+ let asked = "Best street food stalls in Munich this weekend?";
+ let note = hire_note(&["m1", "m2", "m3"]);
+ assert!(note.starts_with(crate::drivers::ROUTING_NOTE), "{note}");
+ let mut history = vec![crate::drivers::tests::said("user", asked)];
+ crate::drivers::drop_current_message(&mut history, &format!("{asked}\n\n{note}"));
+ assert!(history.is_empty(), "the prompt carries the message once");
+ }
}
diff --git a/src/server/rerouting.rs b/src/server/rerouting.rs
index f6313df3..f3596817 100644
--- a/src/server/rerouting.rs
+++ b/src/server/rerouting.rs
@@ -15,6 +15,7 @@ use crate::db::models::{Kwant, MessageExtra, NewMessage};
use crate::db::routing::RouteChoice;
use crate::db::spaces::SpaceId;
use crate::drivers::events::RuntimeEvent;
+use crate::drivers::ROUTING_NOTE;
use crate::router::team::{Hints, Learned, ROUTE_AT};
use axum::{
extract::{Path, State},
@@ -44,7 +45,7 @@ pub(super) fn routed_note(routing: &Value, team_channel: bool) -> Option<String>
// Not "if another bot would do it better": a bot that sees no better teammate (the Concierge is
// made when needed) answers what isn't its area instead of handing it back
Some(format!(
- "(kwant routing note: kwnt picked you for this message from its words; the person didn't choose you. If it isn't \
+ "{ROUTING_NOTE} kwnt picked you for this message from its words; the person didn't choose you. If it isn't \
your area, call {HAND_BACK} with a one-line reason instead of answering: kwnt passes it to a better fit or a \
generalist, even one you can't see here, and tells you if there's no one. If it is your area, answer it.)"
))
@@ -355,10 +356,7 @@ pub(super) async fn hand_back(state: &AppState, from_id: &str, channel_id: &str,
let mut routing = super::routes::routing_meta(&to, &why, members.len() + 1, false, &[], "handback", &msg.id);
routing["fit"] = json!(fit);
routing["handed_back_by"] = json!(from.id);
- let prompt = format!(
- "{}\n\n(kwnt routing note: {} handed this message to you as not their area: {reason}. Answer it.)",
- msg.text, from.name
- );
+ let prompt = handback_prompt(&msg.text, &from.name, reason);
tracing::info!(from = %from.id, to = %to.id, message = %msg.id, "a bot handed a message back");
let (st, k, um) = (state.clone(), to.clone(), msg.id.clone());
let ch = channel.id.clone();
@@ -370,6 +368,11 @@ pub(super) async fn hand_back(state: &AppState, from_id: &str, channel_id: &str,
Ok(to.name)
}
+/// The handed-back turn's prompt: the person's message, then a note saying who handed it on and why.
+fn handback_prompt(text: &str, from: &str, reason: &str) -> String {
+ format!("{text}\n\n{ROUTING_NOTE} {from} handed this message to you as not their area: {reason}. Answer it.)")
+}
+
/// Who takes a handed-back message: the next best member when it fits (at least [`ROUTE_AT`]),
/// else the Concierge (#general, made if needed; anywhere it already exists), else the lead.
/// Never the bot that handed it back.
@@ -457,4 +460,16 @@ mod tests {
crate::drivers::drop_current_message(&mut history, &format!("{asked}\n\n{note}"));
assert!(history.is_empty(), "the prompt carries the message once");
}
+
+ /// The next bot's turn has the handed-back message once: its prompt is the message plus the
+ /// note, so history drops the copy (it didn't while this note began "(kwnt").
+ #[test]
+ fn a_handed_back_message_isnt_repeated_in_history() {
+ let asked = "Plan a lantern parade route for Saturday";
+ let prompt = handback_prompt(asked, "Scriptwriter", "Lantern parades are not book scripts");
+ assert!(prompt.starts_with(&format!("{asked}\n\n{ROUTING_NOTE} Scriptwriter handed")), "{prompt}");
+ let mut history = vec![crate::drivers::tests::said("user", asked)];
+ crate::drivers::drop_current_message(&mut history, &prompt);
+ assert!(history.is_empty(), "the prompt carries the message once");
+ }
}
diff --git a/tests/concierge_test.rs b/tests/concierge_test.rs
index f02805f8..780e8955 100644
--- a/tests/concierge_test.rs
+++ b/tests/concierge_test.rs
@@ -387,8 +387,8 @@ async fn a_hand_back_passes_on_the_message_it_answers_not_the_newest() {
assert_eq!(routing["message_id"], a.as_str(), "A was handed on, not the newer B");
assert!(state.db.handed_back(&a).await.unwrap() && !state.db.handed_back(&b).await.unwrap());
let prompts = std::fs::read_to_string(fake_cli().join("prompts.log")).unwrap_or_default();
- assert!(prompts.contains(&format!("{a_text}\n\n(kwnt routing note: Scriptwriter handed")), "the hand-back turn's prompt is A's text");
- assert!(!prompts.contains("sitemap\n\n(kwnt routing note"), "B was never handed on");
+ assert!(prompts.contains(&format!("{a_text}\n\n(kwant routing note: Scriptwriter handed")), "the hand-back turn's prompt is A's text");
+ assert!(!prompts.contains("sitemap\n\n(kwant routing note"), "B was never handed on");
// Still one hop for A
let token = answering_token(&state, &atlas, &general, Some(&a)).await;