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.

away: approvals carry their real turn id, so they link to their chat (batch3)

Lin (kwant bot)1fcf4ca72ba2
diff --git a/src/server/mcp_runtime.rs b/src/server/mcp_runtime.rs
index 870a730d..44a87992 100644
--- a/src/server/mcp_runtime.rs
+++ b/src/server/mcp_runtime.rs
@@ -239,14 +239,17 @@ fn local_base_url(state: &AppState) -> String {
/// behaviour change) otherwise. `cwd` is where the gateway starts stdio servers. Pair with
/// [`finish_turn`].
pub async fn prepare_turn(state: &AppState, kwant: &Kwant, cwd: Option<PathBuf>) -> Option<(McpTurnContext, String)> {
- prepare_turn_with_self(state, kwant, cwd, None).await
+ prepare_turn_with_self(state, kwant, None, cwd, None).await
}
/// [`prepare_turn`], plus the agents' own continuity tools (`search_history`, `memory_*`) when
/// `self_scope` is set. Those are served by kwnt itself and never need an approval card.
+/// `turn_id` is the running turn's id, so its approvals can be traced back to it (e.g. to link
+/// them to their chat); without one the session gets a fresh id.
pub async fn prepare_turn_with_self(
state: &AppState,
kwant: &Kwant,
+ turn_id: Option<String>,
cwd: Option<PathBuf>,
self_scope: Option<super::continuity::SelfScope>,
) -> Option<(McpTurnContext, String)> {
@@ -289,7 +292,8 @@ pub async fn prepare_turn_with_self(
max_tool_iterations: max_tool_iterations(setting),
records: Default::default(),
};
- let session = PermissionSession::new(kwant.id.clone(), kwant.name.clone(), uuid::Uuid::new_v4().to_string(), ctx.clone(), cwd);
+ let turn_id = turn_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
+ let session = PermissionSession::new(kwant.id.clone(), kwant.name.clone(), turn_id, ctx.clone(), cwd);
state.mcp_sessions.lock().await.insert(token.clone(), session);
if let (true, Some(scope)) = (self_tools, self_scope) {
state.self_tools.lock().await.insert(token.clone(), scope);
diff --git a/src/server/routes.rs b/src/server/routes.rs
index 7c59e41b..14eecf2a 100644
--- a/src/server/routes.rs
+++ b/src/server/routes.rs
@@ -1083,6 +1083,7 @@ async fn run_kwant_turn_inner(
let mcp_turn = super::mcp_runtime::prepare_turn_with_self(
state,
target_kwant,
+ Some(tag.turn_id.clone()),
scope.workspace_dir.clone(),
self_scope,
)
diff --git a/tests/away_test.rs b/tests/away_test.rs
index f77d96b5..560d37d2 100644
--- a/tests/away_test.rs
+++ b/tests/away_test.rs
@@ -7,20 +7,21 @@ use axum::{
};
use chrono::{DateTime, Duration, SecondsFormat, Utc};
use kwnt::{
- broker::{ApprovalRequest, PermissionBroker},
+ broker::{ApprovalDecision, ApprovalRequest, PermissionBroker},
config::Config,
db::{
continuity::NewRoutine,
- models::{NewApproval, NewChannel, NewMessage},
+ models::{McpServer, NewApproval, NewChannel, NewMessage},
questions::QuestionPlace,
threads::NewDelegation,
Database,
},
router::LayaRouter,
- server::{create_router, AppState},
+ server::{create_router, mcp_runtime, turns::ActiveTurn, AppState},
tools::ToolRunner,
};
use serde_json::{json, Value};
+use std::collections::BTreeMap;
use sqlx::sqlite::SqlitePoolOptions;
use tempfile::{tempdir, TempDir};
use tower::ServiceExt;
@@ -363,3 +364,89 @@ async fn away_lists_are_capped_at_50_newest_first() {
assert_eq!(v["unseen"]["finished"], 60);
assert_eq!(v["unseen"]["failed"], 60);
}
+
+/// Asks for a tool approval the way claude's permission prompt does, from a live turn in `place`
+/// (`(context_id, thread_id)`), and returns the waiting item's link once it shows up.
+async fn live_approval_link(app: &Router, state: &AppState, kwant: &kwnt::db::models::Kwant, place: (&str, Option<&str>)) -> Value {
+ let turn_id = uuid::Uuid::new_v4().to_string();
+ let (_, token) = mcp_runtime::prepare_turn_with_self(state, kwant, Some(turn_id.clone()), None, None).await.unwrap();
+ state.turns.set_turn(ActiveTurn {
+ turn_id: turn_id.clone(),
+ kwant_id: kwant.id.clone(),
+ context_id: place.0.into(),
+ thread_id: place.1.map(Into::into),
+ state: "running".into(),
+ since: Utc::now(),
+ });
+ let ask = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "approve",
+ "arguments": {"tool_name": "mcp__echo__echo", "input": {"text": "hi"}, "tool_use_id": "tu_1"}}});
+ let req = Request::post(format!("/api/mcp/permission/{token}"))
+ .header("content-type", "application/json")
+ .body(Body::from(ask.to_string()))
+ .unwrap();
+ let prompt = tokio::spawn(app.clone().oneshot(req));
+
+ let mut item = None;
+ for _ in 0..200 {
+ let v = get(app, "/api/away").await;
+ if let Some(a) = v["waiting"].as_array().unwrap().iter().find(|i| i["kind"] == "approval") {
+ item = Some(a.clone());
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ let item = item.expect("the approval never showed up under waiting");
+ assert_eq!(state.db.get_approval(item["id"].as_str().unwrap()).await.unwrap().unwrap().turn_id, turn_id);
+
+ assert!(state.broker.resolve(item["id"].as_str().unwrap(), ApprovalDecision::AllowOnce).await);
+ let resp = prompt.await.unwrap().unwrap();
+ let bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
+ assert!(String::from_utf8_lossy(&bytes).contains(r#"\"behavior\":\"allow\""#), "{}", String::from_utf8_lossy(&bytes));
+ mcp_runtime::finish_turn(state, &token).await;
+ state.turns.end_turn(&turn_id);
+ item["link"].clone()
+}
+
+#[tokio::test]
+async fn live_approvals_link_to_the_chat_their_turn_runs_in() {
+ let (app, state, _tmp) = setup().await;
+ let kwants = state.db.list_kwants().await.unwrap();
+ let (lead, spec) = (kwants[0].clone(), kwants[1].clone());
+ let echo = state
+ .db
+ .create_mcp_server(&McpServer {
+ id: String::new(),
+ name: "echo".into(),
+ transport: "stdio".into(),
+ command: Some("/usr/bin/true".into()),
+ args: vec![],
+ url: None,
+ env: BTreeMap::new(),
+ headers: BTreeMap::new(),
+ enabled: true,
+ timeout_secs: 30,
+ created_at: None,
+ })
+ .await
+ .unwrap();
+ state.db.set_kwant_mcp_servers(&lead.id, std::slice::from_ref(&echo.id)).await.unwrap();
+ let chan = state
+ .db
+ .create_channel(&NewChannel {
+ name: "ops".into(),
+ description: None,
+ lead_kwant_id: lead.id.clone(),
+ member_kwants: vec![lead.id.clone(), spec.id.clone()],
+ routing_mode: "auto".into(),
+ })
+ .await
+ .unwrap();
+ let thread = state.db.create_thread(&lead.id, "Side quest", "chat", None, "user").await.unwrap();
+
+ let link = live_approval_link(&app, &state, &lead, (&chan.id, None)).await;
+ assert_eq!(link, json!({"thread_id": null, "channel_id": chan.id, "direct_kwant_id": null}));
+ let link = live_approval_link(&app, &state, &lead, (&lead.id, Some(&thread.id))).await;
+ assert_eq!(link, json!({"thread_id": thread.id, "channel_id": null, "direct_kwant_id": null}));
+ let link = live_approval_link(&app, &state, &lead, (&lead.id, None)).await;
+ assert_eq!(link, json!({"thread_id": null, "channel_id": null, "direct_kwant_id": lead.id}));
+}
diff --git a/tests/continuity_test.rs b/tests/continuity_test.rs
index 4b1fbbd6..47780787 100644
--- a/tests/continuity_test.rs
+++ b/tests/continuity_test.rs
@@ -662,6 +662,7 @@ async fn self_tools_are_scoped_to_the_turn_and_kwant() {
&state,
k1,
None,
+ None,
Some(SelfScope {
kwant_id: k1.id.clone(),
kwant_name: k1.name.clone(),