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.

29s: the reason a person gives with Deny shows in Details (#849)

Oleksandr Ivanchenkocf95902e81be
diff --git a/src/drivers/activity.rs b/src/drivers/activity.rs
index fbea5574..0bf6b0f4 100644
--- a/src/drivers/activity.rs
+++ b/src/drivers/activity.rs
@@ -427,12 +427,23 @@ impl ActivityLog {
/// Ends the waiting step for `ref_id` with `status` ("ok", "done", "declined", "stopped", or
/// "unanswered" for an approval card the turn stopped waiting for, still open for a late answer).
pub fn settle(&mut self, ref_id: &str, status: &'static str) -> Option<Step> {
+ self.settle_with(ref_id, status, None)
+ }
+
+ /// [`settle`](Self::settle), with the reason the person gave with a Deny as the step's detail
+ /// (29s, [`crate::mcp::said_why_detail`]). The reason is redacted first, then cut so the whole
+ /// detail fits [`MAX_DETAIL`] and a cut reason still ends `…”`. An `unanswered` step keeps its
+ /// own detail.
+ pub fn settle_with(&mut self, ref_id: &str, status: &'static str, said_why: Option<&str>) -> Option<Step> {
let i = self
.steps
.iter()
.rposition(|s| s.ref_id.as_deref() == Some(ref_id) && s.is_open())?;
if status == "unanswered" {
self.steps[i].detail = Some(crate::mcp::UNANSWERED_STEP_DETAIL.to_string());
+ } else if let Some(why) = said_why {
+ let room = MAX_DETAIL.saturating_sub(crate::mcp::said_why_detail("").chars().count());
+ self.steps[i].detail = Some(crate::mcp::said_why_detail(&cut(&self.redact(why), room)));
}
Some(self.end(i, status))
}
@@ -1867,6 +1878,39 @@ mod tests {
}
}
+ /// 29s: a card settled with the person's words keeps them as its detail, redacted and cut like
+ /// any detail; the paired tool step still reads "You said no; nothing ran". A plain settle adds
+ /// none, and an `unanswered` card keeps its own detail.
+ #[test]
+ fn a_card_settled_with_the_persons_words_keeps_them_redacted() {
+ let mut log = ActivityLog::default();
+ log.wait("approval", "t1", "Waiting for your approval: Running `git push --force`", Some("Bash"));
+ let card = log.settle_with("t1", "declined", Some("push it with ghp_abcdefghijklmnopqrstuvwxyz0123 instead")).unwrap();
+ assert_eq!(card.detail.as_deref(), Some("You said why: \u{201c}push it with [redacted] instead\u{201d}"));
+ log.observe(&pending("a", "Bash", json!({"command": "git push --force"})));
+ let ended = log.observe(&denied("a"));
+ assert_eq!((ended[0].status, ended[0].detail.as_deref()), ("declined", Some(crate::mcp::DENIED_STEP_DETAIL)));
+ // A 500-char reason: cut so the whole detail fits MAX_DETAIL, and the quote still closes
+ log.wait("approval", "t2", "Waiting for your approval: Running a command", Some("Bash"));
+ let long = "é".repeat(crate::mcp::DENY_REASON_MAX);
+ let detail = log.settle_with("t2", "declined", Some(&long)).unwrap().detail.unwrap();
+ assert_eq!(detail.chars().count(), MAX_DETAIL, "{detail}");
+ assert!(detail.starts_with("You said why: \u{201c}é") && detail.ends_with("é…\u{201d}"), "{detail}");
+ // A short one isn't cut
+ log.wait("approval", "t6", "Waiting for your approval: Running a command", Some("Bash"));
+ let short = log.settle_with("t6", "declined", Some("use staging")).unwrap().detail;
+ assert_eq!(short.as_deref(), Some("You said why: \u{201c}use staging\u{201d}"));
+ // A plain Deny and an Allow: no detail
+ log.wait("approval", "t3", "Waiting for your approval: Running a command", Some("Bash"));
+ assert_eq!(log.settle("t3", "declined").unwrap().detail, None);
+ log.wait("approval", "t4", "Waiting for your approval: Running a command", Some("Bash"));
+ assert_eq!(log.settle_with("t4", "ok", None).unwrap().detail, None);
+ // An unanswered card keeps its own words
+ log.wait("approval", "t5", "Waiting for your approval: Running a command", Some("Bash"));
+ let late = log.settle_with("t5", "unanswered", Some("ignored")).unwrap();
+ assert_eq!(late.detail.as_deref(), Some(crate::mcp::UNANSWERED_STEP_DETAIL));
+ }
+
/// 28b: a Deny on the card ends the tool step `declined`, in the person's words, whether the
/// card or the tool step started first.
#[test]
diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs
index 786eca3a..a8cd850b 100644
--- a/src/mcp/mod.rs
+++ b/src/mcp/mod.rs
@@ -490,6 +490,14 @@ If what they said points to another way, you may try that; it will ask them agai
)
}
+/// 29s: the detail of the card's approval step after a Deny whose reason the call heard, so the
+/// person (and a teammate reading the run) can see later what they told the bot. The tool step
+/// keeps [`DENIED_STEP_DETAIL`]. The activity log redacts the reason, then cuts it so this fits
+/// its detail length with the closing quote kept.
+pub fn said_why_detail(reason: &str) -> String {
+ format!("You said why: \u{201c}{reason}\u{201d}")
+}
+
tokio::task_local! {
/// 29o: where [`tell_deny_reason`] leaves the person's reason for [`hear_deny_reason`].
static DENY_REASON: Arc<Mutex<Option<String>>>;
@@ -847,6 +855,7 @@ If what they said points to another way, you may try that; it will ask them agai
/// 29o round 2: a Deny with a reason, as the claude CLI reports it back (an error carrying the
/// permission prompt's message), still ends the tool step `declined` in the person's words,
/// paired with its card; the reason and the rest of the bot's text stay out of the step.
+ /// 29s: the reason shows once, on the card's approval step, as the person's own words.
#[test]
fn a_deny_with_a_reason_still_ends_the_step_declined() {
use crate::drivers::activity::ActivityLog;
@@ -857,7 +866,7 @@ If what they said points to another way, you may try that; it will ask them agai
for text in [with_no_false_claims(&said), said.clone()] {
let mut log = ActivityLog::default();
log.wait("approval", "t1", "Waiting for your approval: Running `git push --force`", Some("Bash"));
- log.settle("t1", "declined");
+ log.settle_with("t1", "declined", Some("push without --force"));
log.observe(&RuntimeEvent::ToolCallPending {
id: "a".into(),
name: "Bash".into(),
@@ -871,8 +880,16 @@ If what they said points to another way, you may try that; it will ask them agai
(ended[0].kind, ended[0].status, ended[0].detail.as_deref()),
("tool", "declined", Some(DENIED_STEP_DETAIL))
);
+ let details: Vec<_> = log.steps().iter().map(|s| (s.kind, s.status, s.detail.as_deref())).collect();
+ assert_eq!(
+ details,
+ vec![
+ ("approval", "declined", Some("You said why: \u{201c}push without --force\u{201d}")),
+ ("tool", "declined", Some(DENIED_STEP_DETAIL)),
+ ]
+ );
let shown = serde_json::to_string(log.steps()).unwrap();
- assert!(!shown.contains("without --force") && !shown.contains("They said why"), "{shown}");
+ assert!(!shown.contains("They said why") && !shown.contains("Don't make this same call"), "{shown}");
}
}
diff --git a/src/server/activity.rs b/src/server/activity.rs
index b252b8f1..3162d513 100644
--- a/src/server/activity.rs
+++ b/src/server/activity.rs
@@ -105,7 +105,13 @@ impl TurnActivity {
}
pub fn settle(&self, ref_id: &str, status: &'static str) {
- let step = self.with_log(|log| log.settle(ref_id, status));
+ self.settle_with(ref_id, status, None);
+ }
+
+ /// [`settle`](Self::settle) with the reason the person gave with a Deny as the step's detail
+ /// (29s), redacted and cut ([`ActivityLog::settle_with`]).
+ pub fn settle_with(&self, ref_id: &str, status: &'static str, said_why: Option<&str>) {
+ let step = self.with_log(|log| log.settle_with(ref_id, status, said_why));
self.send(step);
}
@@ -256,8 +262,14 @@ pub fn refused_as(
/// An approval card for `turn_id` was answered ("ok" allowed, "declined") or stopped waiting.
pub fn approval_settled(turn_id: &str, ticket: &str, status: &'static str) {
+ approval_settled_with(turn_id, ticket, status, None);
+}
+
+/// [`approval_settled`], with a detail on the card's step: 29s, the reason the person gave with
+/// a Deny that the waiting call heard, shown as [`crate::mcp::said_why_detail`], redacted and cut.
+pub fn approval_settled_with(turn_id: &str, ticket: &str, status: &'static str, said_why: Option<&str>) {
if let Some(turn) = TurnActivity::find(turn_id) {
- turn.settle(ticket, status);
+ turn.settle_with(ticket, status, said_why);
}
}
diff --git a/src/server/mcp_runtime.rs b/src/server/mcp_runtime.rs
index 36b24778..5a3cf900 100644
--- a/src/server/mcp_runtime.rs
+++ b/src/server/mcp_runtime.rs
@@ -557,8 +557,17 @@ impl BrokerApprover {
let decision = tokio::time::timeout(self.timeout, rx).await;
guard.id = None;
super::scheduler::approval_wait(&self.events, &req.kwant_id, &chat_key, None);
+ // 29o: a Deny's reason goes to the call listening for it (`hear_deny_reason`). 29s: taken
+ // before the card's step settles, so a heard reason is that step's detail when it ends
+ let heard_reason = match &decision {
+ Ok(Ok(ApprovalDecision::Deny)) => self
+ .broker
+ .take_deny_reason(&ticket)
+ .filter(|why| mcp::tell_deny_reason(why.clone())),
+ _ => None,
+ };
if let Some(settled) = card_settles(&decision) {
- super::activity::approval_settled(&req.turn_id, &ticket, settled);
+ super::activity::approval_settled_with(&req.turn_id, &ticket, settled, heard_reason.as_deref());
}
let outcome = match decision {
Ok(Ok(ApprovalDecision::AllowOnce)) => ApprovalOutcome::Allowed,
@@ -580,8 +589,7 @@ impl BrokerApprover {
Ok(Ok(ApprovalDecision::Deny)) => {
// A reason nobody is listening for (no caller scoped by `hear_deny_reason`) means
// the bot hears the plain DENIED_TEXT, so the no is remembered as a plain one
- let heard = self.broker.take_deny_reason(&ticket).is_some_and(mcp::tell_deny_reason);
- if !heard {
+ if heard_reason.is_none() {
self.broker.declined.record(&req.kwant_id, &self.conversation, &families, &ticket);
}
ApprovalOutcome::Denied
@@ -2281,6 +2289,54 @@ If what they said points to another way, you may try that; it will ask them agai
assert_eq!(e.state.broker.deny_reasons_held(), 0, "nothing left behind");
}
+ /// 29s: the reason a person gives with Deny shows in Details: once the waiting call hears it,
+ /// the card's approval step ends `declined` with `You said why: “…”`, redacted (a token in the
+ /// reason never shows). A reason nobody heard, a plain Deny and an Allow add nothing.
+ #[tokio::test]
+ async fn a_heard_deny_reason_is_the_cards_detail_in_details() {
+ use crate::drivers::events::TurnTag;
+ use crate::server::activity::TurnActivity;
+ let e = setup().await;
+ let a = approver(&e, &e.general, Duration::from_secs(10));
+ let tag = TurnTag { turn_id: "t-29s".into(), kwant_id: e.bot.id.clone(), context_id: "c".into(), thread_id: None };
+ let turn = TurnActivity::start(broadcast::channel(64).0, tag);
+ let on_turn = |command: &str| ToolApprovalRequest { turn_id: "t-29s".into(), ..bash(&e.bot, command) };
+ let token = "ghp_abcdefghijklmnopqrstuvwxyz0123";
+ let reason: &'static str = Box::leak(format!("push without --force, my token {token} is fine").into_boxed_str());
+ let (out, _, heard) = asked(&e, &a, on_turn("git push --force origin main"), why(reason), true).await;
+ assert_eq!((out, heard.as_deref()), (ApprovalOutcome::Denied, Some(reason)), "the call hears it whole");
+ let (out, _) = ask_unheard(&e, &a, on_turn("rm -rf build"), why("keep the build folder")).await;
+ assert_eq!(out, ApprovalOutcome::Denied);
+ let (out, _) = ask(&e, &a, on_turn("gh pr merge 7"), DENY).await;
+ assert_eq!(out, ApprovalOutcome::Denied);
+ let (out, _) = ask(&e, &a, on_turn("npm run build"), ALLOW).await;
+ assert_eq!(out, ApprovalOutcome::Allowed);
+ let saved = turn.finish().unwrap();
+ let cards: Vec<(String, Option<String>)> = saved["steps"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .filter(|s| s["kind"] == "approval")
+ .map(|s| (s["status"].as_str().unwrap().to_string(), s["detail"].as_str().map(str::to_string)))
+ .collect();
+ assert_eq!(
+ cards,
+ vec![
+ (
+ "declined".to_string(),
+ Some("You said why: \u{201c}push without --force, my token [redacted] is fine\u{201d}".to_string())
+ ),
+ ("declined".to_string(), None),
+ ("declined".to_string(), None),
+ ("ok".to_string(), None),
+ ],
+ "{saved}"
+ );
+ assert!(!saved.to_string().contains(token), "{saved}");
+ assert!(!saved.to_string().contains("keep the build folder"), "an unheard reason shows nothing new");
+ assert_eq!(e.state.broker.deny_reasons_held(), 0, "nothing left behind");
+ }
+
/// 29o (d): a late Deny with a reason (its turn stopped waiting) answers as a plain late Deny
/// does, and keeps nothing for a call that will never come.
#[tokio::test]