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.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
28i home-claim-other-tools: a true claim about another tool is never called a home change (stacked on #782) (#800)
Oleksandr Ivanchenkoa704b47d93fa
diff --git a/src/server/delivery_check.rs b/src/server/delivery_check.rsindex 408fae09..2a68a76f 100644--- a/src/server/delivery_check.rs+++ b/src/server/delivery_check.rs@@ -31,7 +31,7 @@//! the same plan the prompt's "[What to deliver]" hint renders, read through [`Expect::from_plan`].//! The continuation carries that plan; it never makes a new one from kwant's own prompt.-use super::home_claims::claimed_change;+use super::home_claims::{claimed_change, claimed_home_change};use super::picture_claims::claims_picture;use crate::router::delivery::{DeliveryPlan, Show};use serde_json::Value;@@ -77,6 +77,10 @@ pub struct Seen {/// 27e: what it did with Home Assistant (see [`home_steps_in`]); `None` when it had no Home/// Assistant server.pub home: Option<HomeSeen>,+ /// 28i: a tool step that isn't one of the turn's Home Assistant servers' and can change+ /// something (not a read, see [`other_change_in`]) worked. Then a reply's claim counts as a+ /// home change only when it names something in the home (see [`home_miss`]).+ pub other_changed: bool,}/// 27e: what a turn (or a delivery) did with a Home Assistant server's tools.@@ -127,6 +131,35 @@ fn home_read(tool: &str) -> bool {crate::mcp::home_floor::is_home_read(tool) || ["GetLiveContext", "GetDateTime"].iter().any(|r| tool.ends_with(r))}+/// Tools that only look, past [`super::restart_resume::read_only`]'s reads: kwant's own+/// `web_fetch`, `search` and `browser_read`, opening a page (kwant's own link open is+/// `browser_open`) and moving around in it.+const LOOKS: &[&str] = &[+ "web_fetch", "fetch", "search", "read", "page", "cite", "open", "navigate", "back", "forward", "reload", "scroll", "tab_switch",+ "tab_new", "wait_ms", "hover",+];++/// 28i: whether a tool step that isn't one of `servers`' (the turn's Home Assistant servers) and+/// can change something worked: anything but a read (a web search, a fetch, a file read, a page+/// look). An unknown tool counts as a change, like `echo` or `Bash`; this only narrows which+/// claims are about the home, so a misjudged read leaves the object check in [`home_miss`].+pub fn other_change_in(activity: Option<&Value>, servers: &[String]) -> bool {+ let steps = activity.and_then(|a| a["steps"].as_array()).map(Vec::as_slice).unwrap_or_default();+ steps.iter().any(|step| {+ let Some(tool) = step["tool"].as_str() else { return false };+ let looks = || {+ let bare = tool.rsplit("__").next().unwrap_or(tool).to_ascii_lowercase();+ let bare = bare.trim_start_matches("agent_").trim_start_matches("browser_");+ LOOKS.contains(&bare)+ };+ step["kind"].as_str() == Some("tool")+ && matches!(step["status"].as_str(), Some("ok" | "done"))+ && home_tool(tool, servers).is_none()+ && !super::restart_resume::read_only(tool)+ && !looks()+ })+}+/// 27e: what the turn did with the Home Assistant servers named `servers`, from its saved/// activity (`{"steps": [...]}`). `None` when the turn had none.pub fn home_steps_in(activity: Option<&Value>, servers: &[String]) -> Option<HomeSeen> {@@ -216,6 +249,7 @@ impl Seen {so_far.image_shared |= self.image_shared;so_far.pushed |= self.pushed;so_far.declined |= self.declined;+ so_far.other_changed |= self.other_changed;if let Some(home) = &self.home {so_far.home.get_or_insert_with(HomeSeen::default).join(home);}@@ -231,6 +265,7 @@ impl Seen {failed_step: so_far.failed_step.clone(),declined: so_far.declined,home: so_far.home.clone(),+ other_changed: so_far.other_changed,}}}@@ -446,10 +481,14 @@ pub fn check(reply: &str, expect: Option<&Expect>, continued: bool, seen: &Seen)/// 27e: what `seen`'s Home Assistant steps don't back in `reply`: a claimed change when no/// change step worked (and none waits on, went unanswered on or was declined on a card), and-/// reads that all failed when nothing else worked.+/// reads that all failed when nothing else worked. 28i: when another tool changed something+/// ([`Seen::other_changed`]), only a claim that names something in the home counts+/// ([`claimed_home_change`]): "I closed the issue." after closing it is true.pub fn home_miss(reply: &str, seen: &Seen) -> HomeMiss {let Some(home) = &seen.home else { return HomeMiss::default() };- let claim = (!home.changed && !home.on_card && !seen.declined).then(|| claimed_change(reply)).flatten();+ // 28i: after another tool changed something, only a claim that names the home is about it+ let claimed = |reply: &str| if seen.other_changed { claimed_home_change(reply) } else { claimed_change(reply) };+ let claim = (!home.changed && !home.on_card && !seen.declined).then(|| claimed(reply)).flatten();let read_failed = (home.reads > 0 && !home.read_ok && !home.changed).then(|| home.read_error.clone());HomeMiss { claim, read_failed }}@@ -1107,7 +1146,7 @@ mod tests {fn explain() -> Expect {Expect::default()}- const NOTHING: Seen = Seen { image_shared: false, has_shot: false, pushed: false, ran_tools: false, failed_step: None, declined: false, home: None };+ const NOTHING: Seen = Seen { image_shared: false, has_shot: false, pushed: false, ran_tools: false, failed_step: None, declined: false, home: None, other_changed: false };const SHARED: Seen = Seen { image_shared: true, has_shot: true, ..NOTHING };const SHOT: Seen = Seen { has_shot: true, ..NOTHING };@@ -2115,6 +2154,90 @@ mod tests {assert_eq!(then.home_lines.len(), 2);}+ /// 28i: a turn on a bot with the Home Assistant server "house" and other tools.+ fn mixed(steps: Value) -> Seen {+ let activity = json!({ "steps": steps.clone() });+ let servers = ["house".to_string()];+ Seen { other_changed: other_change_in(Some(&activity), &servers), ..house(steps) }+ }+ fn step(tool: &str, status: &str) -> Value {+ json!({ "kind": "tool", "tool": tool, "status": status, "title": tool })+ }+ const ECHO_OK: &str = "mcp__tools__echo";+ const KITCHEN: &str = "I turned off the kitchen light.";++ /// 28i (Rigel's QA of #782, Finding 1): a true claim after another tool that worked is never+ /// called a home change: no continuation, and no kwant line after one.+ #[test]+ fn a_true_claim_about_another_tool_is_not_a_home_change() {+ for reply in ["I closed the issue.", "I opened the pull request and closed the issue.", "Done: set the label to bug."] {+ for steps in [json!([step(ECHO_OK, "ok")]), json!([step("WebSearch", "ok"), step("Bash", "ok")]), json!([read("ok", "Kitchen light: on"), step(ECHO_OK, "done")])] {+ let seen = mixed(steps.clone());+ assert!(seen.other_changed, "{steps}");+ let v = check(reply, None, false, &seen);+ assert!(v.missing.is_empty() && v.continue_with.is_none() && v.home_lines.is_empty(), "{reply} after {steps}: {v:?}");+ assert!(check(reply, None, true, &seen).home_lines.is_empty(), "{reply} after {steps}");+ }+ }+ // ...in an earlier turn of the same delivery too+ let mut delivery = NOTHING;+ let _ = mixed(json!([step(ECHO_OK, "ok")])).with_earlier(&mut delivery);+ let then = mixed(json!([])).with_earlier(&mut delivery);+ assert!(then.other_changed && check("I closed the issue.", None, true, &then).home_lines.is_empty());+ }++ /// 28i: a claim that names the home is still caught next to other tools, after a read only+ /// (a web search, a fetch, a page) and after another tool that changed something.+ #[test]+ fn a_home_claim_next_to_other_tools_is_still_caught() {+ for steps in [+ json!([]),+ json!([step("WebSearch", "ok")]),+ json!([step("mcp__kwnt__web_search", "ok"), step("web_fetch", "ok"), step("browser_open", "ok")]),+ json!([step("Read", "ok"), step("mcp__kwnt__browser_read", "ok"), step("agent_browser_snapshot", "ok")]),+ json!([read("ok", "Kitchen light: on")]),+ json!([step(ECHO_OK, "failed")]),+ json!([step(ECHO_OK, "ok")]),+ ] {+ let v = check(KITCHEN, None, false, &mixed(steps.clone()));+ assert_eq!(v.missing, [Missing::HomeChange], "{steps}");+ assert!(v.continue_with.as_deref().is_some_and(|p| p.contains("Your reply says \"I turned off the kitchen light.\"")), "{steps}");+ assert_eq!(check(KITCHEN, None, true, &mixed(steps.clone())).home_lines, [HomeLine::Change { tried: false }], "{steps}");+ }+ // The home sentence is the one quoted, after a true one about the other tool+ let v = check("I closed the issue. I turned off the kitchen light.", None, false, &mixed(json!([step(ECHO_OK, "ok")])));+ assert!(v.continue_with.as_deref().is_some_and(|p| p.contains("Your reply says \"I turned off the kitchen light.\"")), "{v:?}");+ // With no tool that changed something, every claim is still checked as before+ for steps in [json!([]), json!([step("WebSearch", "ok")])] {+ assert_eq!(check("I closed the issue.", None, false, &mixed(steps.clone())).missing, [Missing::HomeChange], "{steps}");+ }+ // A Home Assistant change that worked still backs the claim+ assert!(check(KITCHEN, None, false, &mixed(json!([step(ECHO_OK, "ok"), turn_off("ok")]))).missing.is_empty());+ }++ #[test]+ fn other_changes_are_told_from_reads_and_home_steps() {+ let servers = ["house".to_string()];+ let changed = |steps: Value| other_change_in(Some(&json!({ "steps": steps })), &servers);+ for tool in [ECHO_OK, "Bash", "Write", "mcp__github__close_issue", "browser_click", "mcp__kwnt__browser_act"] {+ assert!(changed(json!([step(tool, "ok")])), "{tool}");+ assert!(changed(json!([step(tool, "done")])), "{tool}");+ assert!(!changed(json!([step(tool, "failed")])), "{tool}");+ assert!(!changed(json!([step(tool, "unanswered")])), "{tool}");+ assert!(!changed(json!([{ "kind": "approval", "tool": tool, "status": "ok" }])), "{tool}");+ }+ for tool in [+ "WebSearch", "WebFetch", "Read", "Grep", "Glob", "web_search", "mcp__kwnt__web_fetch", "search", "browser_open",+ "browser_navigate", "mcp__kwnt__browser_read", "browser_page", "agent_browser_snapshot", "browser_screenshot",+ "mcp__house__intent__HassTurnOff", "mcp__kwnt__house__homeassistant__GetLiveContext",+ ] {+ assert!(!changed(json!([step(tool, "ok")])), "{tool}");+ }+ assert!(!changed(json!([])) && !other_change_in(None, &servers));+ // A Home Assistant-only turn never has one, so its checks are #782's+ assert!(!mixed(json!([read("ok", "x"), turn_off("ok"), turn_off("failed")])).other_changed);+ }+/// 27e round 2 (Mira's design check): every sentence the model and the person get is true/// when a Home Assistant step did run: a read that worked, a read that failed (the report's/// case) and a change step that failed.diff --git a/src/server/home_claims.rs b/src/server/home_claims.rsindex 3a8b2266..bcebbf08 100644--- a/src/server/home_claims.rs+++ b/src/server/home_claims.rs@@ -119,6 +119,45 @@ pub fn claimed_change(reply: &str) -> Option<String> {sentences(reply).into_iter().find(|s| sentence_claims(s))}+/// 28i: what a sentence must name to be about the home when the turn also changed something+/// with another tool: a device, a part of one, a room or what Home Assistant plays. "I closed+/// the issue." names none of these; "I closed the garage door." does. Singular, lowercase; a+/// plural ("lights", "switches") counts too.+const HOME_THINGS: &[&str] = &[+ // lights and power+ "light", "lamp", "bulb", "led", "chandelier", "sconce", "switch", "plug", "outlet", "socket", "power",+ // doors and covers+ "door", "lock", "deadbolt", "garage", "gate", "blind", "shade", "curtain", "shutter", "awning", "cover",+ // climate and water+ "thermostat", "heating", "heater", "boiler", "radiator", "ac", "aircon", "conditioner", "air", "climate", "fan",+ "temperature", "humidifier", "dehumidifier", "purifier", "valve", "sprinkler", "irrigation", "water",+ // safety+ "alarm", "siren", "camera", "doorbell", "sensor",+ // appliances+ "oven", "kettle", "dishwasher", "washer", "dryer", "fridge", "freezer", "appliance",+ // rooms and the home+ "porch", "kitchen", "bedroom", "bathroom", "hallway", "hall", "living", "lounge", "garden", "yard", "patio",+ "basement", "attic", "nursery", "room", "home", "house", "scene",+];++/// Whether a lowercase word (or one part of a hyphenated one) is one of [`HOME_THINGS`] or+/// [`PLAYERS`], or a plural of one.+fn home_thing(word: &str) -> bool {+ word.split('-').any(|w| {+ let known = |x: &str| HOME_THINGS.contains(&x) || PLAYERS.contains(&x);+ known(w) || w.strip_suffix('s').is_some_and(known) || w.strip_suffix("es").is_some_and(known)+ })+}++/// 28i: like [`claimed_change`], but only a sentence that also names something in the home+/// ([`HOME_THINGS`]). For a turn where another tool changed something and no Home Assistant+/// step did: there "I closed the issue." is true and about that tool, while "I turned off the+/// kitchen light." is still a claim about the home. A sentence that names nothing ("I've+/// unlocked it.") is left to the other tool there.+pub fn claimed_home_change(reply: &str) -> Option<String> {+ sentences(reply).into_iter().find(|s| sentence_claims(s) && words(s).iter().any(|w| home_thing(&w.text)))+}+#[cfg(test)]mod tests {use super::*;@@ -185,4 +224,35 @@ mod tests {assert_eq!(claimed_change(reply), None, "{reply}");}}++ /// 28i (Rigel's QA of #782, Finding 1): when another tool changed something, only a claim+ /// that names something in the home is about the home.+ #[test]+ fn a_home_claim_names_something_in_the_home() {+ for (reply, first) in [+ ("I closed the issue.", None),+ ("I opened the pull request and closed the issue.", None),+ ("Done: set the label to bug.", None),+ ("I've unlocked it.", None),+ ("The issue is now closed.", None),+ ("I stopped looking.", None),+ ("I closed the issue. I turned off the kitchen light.", Some("I turned off the kitchen light.")),+ ("I turned off the kitchen light.", Some("I turned off the kitchen light.")),+ ("I have closed the garage door.", Some("I have closed the garage door.")),+ ("Unlocking the front door now.", Some("Unlocking the front door now.")),+ ("Turned off Living room lamp.", Some("Turned off Living room lamp.")),+ ("Switched off the lights.", Some("Switched off the lights.")),+ ("Setting the thermostat to 21 degrees.", Some("Setting the thermostat to 21 degrees.")),+ ("Disarming the alarm now.", Some("Disarming the alarm now.")),+ ("I stopped the vacuum.", Some("I stopped the vacuum.")),+ ("Done — the porch light is now off.", Some("Done — the porch light is now off.")),+ ("I turned on the living-room fan.", Some("I turned on the living-room fan.")),+ ("I turned off both switches.", Some("I turned off both switches.")),+ ] {+ assert_eq!(claimed_home_change(reply).as_deref(), first, "{reply}");+ }+ // Never more than claimed_change finds+ assert_eq!(claimed_home_change("The front door is locked."), None);+ assert_eq!(claimed_home_change("I can't unlock the front door."), None);+ }}diff --git a/src/server/routes.rs b/src/server/routes.rsindex 95d8a2fb..8a1eefd2 100644--- a/src/server/routes.rs+++ b/src/server/routes.rs@@ -4035,6 +4035,7 @@ async fn run_kwant_turn_inner(failed_step,declined,home: super::delivery_check::home_steps_in(activity.as_ref(), &home_servers),+ other_changed: super::delivery_check::other_change_in(activity.as_ref(), &home_servers),};// ...counting what the delivery's earlier turns did (F1)let seen = match opts.delivery_seen.as_ref().map(|d| d.lock()) {