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.

bot_create obeys the hire policy and turned-down kinds

Lin (kwant bot)2af07a4e83db
diff --git a/src/server/bot_actions.rs b/src/server/bot_actions.rs
index 4926d4fa..75ed02e2 100644
--- a/src/server/bot_actions.rs
+++ b/src/server/bot_actions.rs
@@ -160,7 +160,8 @@ pub fn tools() -> Vec<Value> {
}),
json!({
"name": "bot_create",
- "description": "Add a new bot to your space. It applies at once and the person sees a card with Undo.",
+ "description": "Add a new bot the person asked for in this conversation. It applies at once and the person sees a card \
+ with Undo. To suggest a bot they didn't ask for, use propose_hire instead.",
"inputSchema": {
"type": "object",
"properties": {
@@ -171,8 +172,9 @@ pub fn tools() -> Vec<Value> {
"model": {"type": "string", "description": "\"auto\" (default), an engine (claude, codex, grok, ollama, custom) or engine/model, from bot_list"},
"profile_id": {"type": "string", "description": "Optional account (from bot_list) for that engine"},
"avatar": {"type": "string", "enum": AVATARS},
+ "requested_by_person": {"type": "boolean", "description": "true only when the person asked for this bot in this conversation"},
},
- "required": ["name", "title", "instructions"],
+ "required": ["name", "title", "instructions", "requested_by_person"],
},
}),
json!({
@@ -1113,6 +1115,15 @@ impl Ctx<'_> {
let (Some(name), Some(title), Some(instructions)) = (arg(args, "name"), arg(args, "title"), arg(args, "instructions")) else {
return fail("name, title and instructions are required");
};
+ if args["requested_by_person"].as_bool() != Some(true) {
+ return fail(
+ "Nothing was created: bot_create is only for a bot the person asked for in this conversation (set \
+ requested_by_person). To suggest a new bot yourself, use propose_hire; the space's hire policy decides.",
+ );
+ }
+ if let Some(why) = super::hires::create_clash(self.state, &self.space, name, title).await {
+ return fail(format!("Nothing was created. {why}"));
+ }
let spec = json!({
"name": name,
"title": title,
diff --git a/src/server/hires.rs b/src/server/hires.rs
index 1d0902b5..73fa31b5 100644
--- a/src/server/hires.rs
+++ b/src/server/hires.rs
@@ -246,6 +246,27 @@ fn clash(name: &str, key: &str, bots: &[crate::db::models::Kwant], denials: &[Hi
None
}
+/// Why `bot_create` can't add this bot right away, if it can't: the same name, role, pending and
+/// turned-down checks as a hire proposal. A kind of bot the person turned down only comes back
+/// through `propose_hire`, so the person confirms it on a card.
+pub(super) async fn create_clash(state: &AppState, space: &SpaceId, name: &str, role: &str) -> Option<String> {
+ let key = role_key(role);
+ let name = name.trim_start_matches('@');
+ let denials = state.db.active_denials(space).await.unwrap_or_default();
+ if let Some(d) = denials.iter().find(|d| d.name.eq_ignore_ascii_case(name) || similar_roles(&d.role_key, &key)) {
+ return Some(format!(
+ "The person turned down hiring {} ({}) in this space on {}. If they asked for it again, propose it with propose_hire \
+ (requested_by_person true) so they confirm it on a card.",
+ d.name,
+ d.role,
+ d.resolved_at.map(|t| t.format("%Y-%m-%d").to_string()).unwrap_or_default()
+ ));
+ }
+ let bots = state.db.list_kwants(space).await.unwrap_or_default();
+ let pending = state.db.list_hires(space, Some("pending")).await.unwrap_or_default();
+ clash(name, &key, &bots, &[], &pending, true)
+}
+
async fn propose(state: &AppState, scope: &SelfScope, space: &SpaceId, args: &Value) -> ToolResult {
if let Err(e) = super::spaces::ensure_active(state, space).await {
let _ = e;
diff --git a/tests/approvals_off_test.rs b/tests/approvals_off_test.rs
index 667f0841..bd1db558 100644
--- a/tests/approvals_off_test.rs
+++ b/tests/approvals_off_test.rs
@@ -322,7 +322,7 @@ async fn only_the_person_changes_approvals() {
assert!(err, "{text}");
let (text, err) = self_tool(&e, &token, "bot_update", json!({"bot": e.bot.id, "approvals": "off"})).await;
assert!(err && text.contains("approvals"), "{text}");
- let (text, err) = self_tool(&e, &token, "bot_create", json!({"name": "Free", "title": "t", "instructions": "i", "approvals": "off"})).await;
+ let (text, err) = self_tool(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Free", "title": "t", "instructions": "i", "approvals": "off"})).await;
assert!(err && text.contains("approvals"), "{text}");
let (text, err) = self_tool(&e, &token, "space_update", json!({"name": "X", "approvals": "off"})).await;
assert!(err && text.contains("approvals"), "{text}");
diff --git a/tests/bot_actions_test.rs b/tests/bot_actions_test.rs
index 679a8dfe..64eda95e 100644
--- a/tests/bot_actions_test.rs
+++ b/tests/bot_actions_test.rs
@@ -266,13 +266,13 @@ async fn bots_are_created_and_updated_and_the_person_undoes_it() {
let origin = TurnOrigin { direct_kwant_id: Some(e.bot.id.clone()), ..Default::default() };
let token = turn_in(&e, &e.bot, origin, "t-bots").await;
- refused(&e, &token, "bot_create", json!({"name": "Two words", "title": "x", "instructions": "x"})).await;
- let text = refused(&e, &token, "bot_create", json!({"name": "Chef", "title": "x", "instructions": "x", "model": "nonsense-model"})).await;
+ refused(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Two words", "title": "x", "instructions": "x"})).await;
+ let text = refused(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Chef", "title": "x", "instructions": "x", "model": "nonsense-model"})).await;
assert!(text.contains("Unknown model"), "{text}");
- let text = refused(&e, &token, "bot_create", json!({"name": e.other.name, "title": "x", "instructions": "x"})).await;
- assert!(text.contains("not created"), "{text}");
+ let text = refused(&e, &token, "bot_create", json!({"requested_by_person": true, "name": e.other.name, "title": "x", "instructions": "x"})).await;
+ assert!(text.contains("already a bot called"), "{text}");
- let text = ok(&e, &token, "bot_create", json!({"name": "Chef", "title": "Recipe helper",
+ let text = ok(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Chef", "title": "Recipe helper",
"instructions": "You suggest recipes from what is in the fridge.", "model": "claude", "avatar": "chef"})).await;
assert!(text.contains("Created bot Chef"), "{text}");
let chef = e.state.db.list_kwants(&SpaceId::default_space()).await.unwrap().into_iter().find(|k| k.name == "Chef").expect("Chef");
@@ -326,7 +326,7 @@ async fn bots_are_created_and_updated_and_the_person_undoes_it() {
async fn deleting_a_bot_waits_for_the_person() {
let e = setup().await;
let token = turn(&e, "t-del").await;
- ok(&e, &token, "bot_create", json!({"name": "Temp", "title": "t", "instructions": "t"})).await;
+ ok(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Temp", "title": "t", "instructions": "t"})).await;
let temp = e.state.db.list_kwants(&SpaceId::default_space()).await.unwrap().into_iter().find(|k| k.name == "Temp").unwrap();
let text = ok(&e, &token, "bot_delete", json!({"bot": "Temp", "reason": "not needed"})).await;
assert!(text.contains("confirm"), "{text}");
@@ -408,7 +408,7 @@ async fn what_stays_with_the_person_is_refused_with_where_to_do_it() {
// MCP env/header values and keys can't ride along on a bot
let text = refused(&e, &token, "bot_update", json!({"bot": e.bot.name, "env": {"GITHUB_TOKEN": "x"}})).await;
assert!(text.contains("Tools") && text.contains("mcp_suggest"), "{text}");
- let text = refused(&e, &token, "bot_create", json!({"name": "Spy", "title": "x", "instructions": "x", "api_key": "k"})).await;
+ let text = refused(&e, &token, "bot_create", json!({"requested_by_person": true, "name": "Spy", "title": "x", "instructions": "x", "api_key": "k"})).await;
assert!(text.contains("secret"), "{text}");
// No tool resolves approvals, answers questions, installs MCP, signs in or picks the computer
@@ -512,7 +512,7 @@ async fn card_text_follows_the_decision_and_monthly_schedules_read_naturally() {
// Confirmed and declined requests read that way too
for (name, route, lead) in [("Temp1", "confirm", "Confirmed: "), ("Temp2", "decline", "Declined: ")] {
- ok(&e, &token, "bot_create", json!({"name": name, "title": "t", "instructions": "t"})).await;
+ ok(&e, &token, "bot_create", json!({"requested_by_person": true, "name": name, "title": "t", "instructions": "t"})).await;
ok(&e, &token, "bot_delete", json!({"bot": name, "reason": "not needed"})).await;
let req = last_action(&e).await;
let card = find(messages(&e, &e.general.id).await, card_of(&req));
diff --git a/tests/hires_test.rs b/tests/hires_test.rs
index c3f095c8..3212b197 100644
--- a/tests/hires_test.rs
+++ b/tests/hires_test.rs
@@ -528,3 +528,41 @@ async fn a_team_is_one_proposal_on_one_card() {
assert!(bot_named(&e, "spc_default", "Gopher").await.is_none());
assert_eq!(e.state.db.list_kwants(&space).await.unwrap().len(), before);
}
+
+#[tokio::test]
+async fn bot_create_obeys_the_hire_policy_and_turned_down_kinds() {
+ let e = setup().await;
+ let token = turn(&e, "t-bc1").await;
+ let create = |name: &str, role: &str, requested: bool| {
+ json!({"name": name, "title": role, "instructions": format!("You are {name}."), "requested_by_person": requested})
+ };
+
+ // A bot the person didn't ask for goes through propose_hire, not bot_create
+ for args in [create("Gardener", "Garden helper", false), json!({"name": "Gardener", "title": "Garden helper", "instructions": "x"})] {
+ let (text, err) = tool(&e, &token, "bot_create", args).await;
+ assert!(err && text.contains("propose_hire"), "{text}");
+ }
+ assert!(bot_named(&e, "spc_default", "Gardener").await.is_none());
+
+ // A kind of bot already waiting for the person can't be created around the card
+ ok(&e, &token, specialist("Chef", "Recipe planner")).await;
+ let (text, err) = tool(&e, &token, "bot_create", create("Cook", "recipes planner", true)).await;
+ assert!(err && text.contains("already waiting"), "{text}");
+ assert!(bot_named(&e, "spc_default", "Cook").await.is_none());
+
+ // A turned-down kind stays out of bot_create, even with "always accept"; it goes back on a card
+ let h = latest(&e).await;
+ let (status, _) = call(&e.app, "POST", &format!("/api/hires/{}/deny", h["id"].as_str().unwrap()), None, None).await;
+ assert_eq!(status, StatusCode::OK);
+ let (_, _) = call(&e.app, "PUT", "/api/spaces/spc_default/hire-policy", None, Some(json!({"policy": "auto"}))).await;
+ for (name, role) in [("Menu", "Weekly recipe planner"), ("Chef", "Kitchen helper")] {
+ let (text, err) = tool(&e, &token, "bot_create", create(name, role, true)).await;
+ assert!(err && text.contains("turned down") && text.contains("propose_hire"), "{name}: {text}");
+ assert!(bot_named(&e, "spc_default", name).await.is_none(), "{name}");
+ }
+
+ // What the person asked for, of a new kind, is created at once
+ let (text, err) = tool(&e, &token, "bot_create", create("Gardener", "Garden helper", true)).await;
+ assert!(!err, "{text}");
+ assert!(bot_named(&e, "spc_default", "Gardener").await.is_some());
+}