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.

29g: a held push on grok finds its own step by the name grok called (#828)

Oleksandr Ivanchenko35fbe0392c35
diff --git a/src/drivers/activity.rs b/src/drivers/activity.rs
index 1f1c3e52..fbea5574 100644
--- a/src/drivers/activity.rs
+++ b/src/drivers/activity.rs
@@ -111,6 +111,9 @@ struct Refusal {
call: Option<String>,
/// The call's arguments, when kwnt knows them.
args: Option<Value>,
+ /// The name the engine called the tool under, when that isn't `tool`: grok's name for it on
+ /// kwnt's gateway (29g), which grok's step carries (see [`grok_gateway_call`]).
+ called_as: Option<String>,
/// What the step shows instead of the text the bot was told.
words: &'static str,
}
@@ -295,10 +298,25 @@ impl ActivityLog {
/// (its step's id) and `args` its arguments, when kwnt knows them, so a neighbouring call of
/// the same tool that fails for its own reason keeps that reason (28q).
pub fn refused_call(&mut self, tool: &str, call: Option<&str>, args: Option<&Value>, words: &'static str) {
+ self.refused_call_as(tool, None, call, args, words);
+ }
+
+ /// [`refused_call`](Self::refused_call) for a call the engine made under another name,
+ /// `called_as`: grok's name for `tool` on kwnt's gateway (29g). grok sends no id, and its step
+ /// carries that name, not `tool`, so the step is found by it (see [`grok_gateway_call`]).
+ pub fn refused_call_as(
+ &mut self,
+ tool: &str,
+ called_as: Option<&str>,
+ call: Option<&str>,
+ args: Option<&Value>,
+ words: &'static str,
+ ) {
self.refused.push(Refusal {
tool: tool.to_string(),
call: call.filter(|c| !c.is_empty()).map(str::to_string),
args: args.cloned(),
+ called_as: called_as.filter(|n| !n.is_empty()).map(str::to_string),
words,
});
}
@@ -309,19 +327,46 @@ impl ActivityLog {
/// - else one of that tool with the same arguments;
/// - else, only when no other step of that tool is still open, one of that tool.
///
- /// A refusal whose id names another step still open waits for that step.
+ /// A refusal whose id names another step still open waits for that step. A grok step through
+ /// kwnt's gateway is of the tool its refusal was called as, with that call's own arguments (29g).
fn refusal_for(&self, id: &str, tool: Option<&str>, args: Option<&Value>) -> Option<usize> {
if let Some(k) = self.refused.iter().position(|r| r.call.as_deref() == Some(id)) {
return Some(k);
}
let tool = tool?;
- let free = |r: &Refusal| same_tool(&r.tool, tool) && r.call.as_ref().is_none_or(|c| !self.tools.contains_key(c));
- if let Some(k) = self.refused.iter().position(|r| free(r) && r.args.is_some() && r.args.as_ref() == args) {
+ let grok = grok_gateway_call(tool, args);
+ // The arguments to compare with `r`'s when `r` is a refusal of this step's tool
+ let of = |r: &Refusal| {
+ if same_tool(&r.tool, tool) {
+ return Some(args);
+ }
+ match (r.called_as.as_deref(), grok) {
+ (Some(name), Some((called, called_args))) if name == called => Some(called_args),
+ _ => None,
+ }
+ };
+ let free = |r: &Refusal| r.call.as_ref().is_none_or(|c| !self.tools.contains_key(c));
+ let same_args = |r: &Refusal| of(r).is_some_and(|a| r.args.is_some() && r.args.as_ref() == a);
+ if let Some(k) = self.refused.iter().position(|r| free(r) && same_args(r)) {
return Some(k);
}
- let alone = !self.tools.values().any(|&j| self.steps[j].tool.as_deref().is_some_and(|t| same_tool(t, tool)));
+ // Whether open step `j` may be a call of this step's tool: a grok call through the gateway
+ // by the name it was made under, or a `use_tool` whose target can't be read (29g)
+ let same_as = |j: usize| {
+ let Some(t) = self.steps[j].tool.as_deref() else {
+ return false;
+ };
+ match grok {
+ Some((called, _)) => match grok_gateway_call(t, self.open_args.get(&j)) {
+ Some((other, _)) => other == called,
+ None => t == GROK_USE_TOOL,
+ },
+ None => same_tool(t, tool),
+ }
+ };
+ let alone = !self.tools.values().any(|&j| same_as(j));
if alone {
- self.refused.iter().position(free)
+ self.refused.iter().position(|r| free(r) && of(r).is_some())
} else {
None
}
@@ -692,6 +737,28 @@ fn same_tool(a: &str, b: &str) -> bool {
a == b || stands_for(a, b) || stands_for(b, a)
}
+/// grok's tool for calling an MCP tool (`{"tool_name": "<server>__<tool>", "tool_input": {…}}`).
+const GROK_USE_TOOL: &str = "use_tool";
+
+/// The gateway name and arguments of a grok step that called a tool through kwnt's gateway (29g),
+/// as grok 1.0.62 streams it: its own `use_tool` with `tool_name` `kwnt__<name>` and the call's
+/// arguments in `tool_input` (how grok offers MCP tools), or `kwnt__<name>` called directly with
+/// them. `<name>` is the grok name the gateway was called under (`github-create_or_update_file`).
+/// `None` for any other step, and for a `use_tool` that names its call only in a file.
+fn grok_gateway_call<'a>(tool: &'a str, args: Option<&'a Value>) -> Option<(&'a str, Option<&'a Value>)> {
+ let gateway_name = |name: &'a str| {
+ name.strip_prefix(crate::mcp::RESERVED_SERVER_NAME)
+ .and_then(|rest| rest.strip_prefix("__"))
+ .filter(|n| !n.is_empty())
+ };
+ if tool == GROK_USE_TOOL {
+ let args = args?;
+ let name = gateway_name(args.get("tool_name")?.as_str()?)?;
+ return Some((name, args.get("tool_input")));
+ }
+ gateway_name(tool).map(|name| (name, args))
+}
+
/// Whether a tool (`browser_open`, `mcp__kwnt__browser_open`, …) opens a page in the bot's browser.
pub fn opens_page(tool: &str) -> bool {
PAGE_OPEN_TOOLS.contains(&tool.rsplit("__").next().unwrap_or(tool))
@@ -2158,6 +2225,152 @@ mod tests {
}
}
+ /// 29g (Rigel's 29b QA): grok reaches kwnt's gateway under its own names
+ /// (`github-create_or_update_file`) with no id, and its step is `use_tool` with
+ /// `{"tool_name": "kwnt__<that name>", "tool_input": <the call's arguments>}` (or the
+ /// `kwnt__<name>` tool called directly), as a real grok 1.0.62 run streams it. The gate's note
+ /// carries the name the gateway was called under, so the held push still finds its own step.
+ #[test]
+ fn a_held_push_on_grok_finds_its_step_by_the_name_grok_called() {
+ const NOTED: &str = "mcp__github__create_or_update_file";
+ const GROK: &str = "github-create_or_update_file";
+ let gate = "kwant is checking this push of main before it goes (pre-push card pp1). Nothing was pushed.";
+ let own = "409 Conflict: README.md changed on the branch";
+ let other_args = json!({"owner": "o", "repo": "r", "branch": "lin-x", "path": "README.md"});
+ let held_args = json!({"owner": "o", "repo": "r", "branch": "main", "path": "src/lib.rs"});
+ // A grok step for gateway tool `name` with `args`, in each shape grok streams
+ let use_tool = |name: &str, args: &Value| ("use_tool".to_string(), json!({"tool_name": format!("kwnt__{name}"), "tool_input": args}));
+ let direct = |name: &str, args: &Value| (format!("kwnt__{name}"), args.clone());
+ type Shape<'a> = &'a dyn Fn(&str, &Value) -> (String, Value);
+ let shapes: [(&str, Shape); 2] = [("use_tool", &use_tool), ("direct", &direct)];
+ // The same step under `kwant__` instead of grok's server prefix `kwnt__`
+ let kwant_prefix = |(tool, args): (String, Value)| {
+ (tool.replace("kwnt__", "kwant__"), serde_json::from_str::<Value>(&args.to_string().replace("kwnt__", "kwant__")).unwrap())
+ };
+ let open = |log: &mut ActivityLog, id: &str, (tool, args): (String, Value)| {
+ log.observe(&pending(id, &tool, args));
+ };
+ let note = |log: &mut ActivityLog, called_as: &str, args: &Value| {
+ log.refused_call_as(NOTED, Some(called_as), None, Some(args), crate::mcp::PREPUSH_BLOCKED_STEP_DETAIL);
+ };
+ let failed = |id: &str, error: &str| RuntimeEvent::ToolCallCompleted {
+ id: id.into(),
+ result: json!({"status": "error", "error": error}),
+ };
+ let words_of = |log: &mut ActivityLog, ev: RuntimeEvent| {
+ let ended = log.observe(&ev);
+ (ended[0].status, ended[0].detail.clone().unwrap_or_default())
+ };
+ let own_reason = ("failed", own.to_string());
+ let blocked = ("failed", crate::mcp::PREPUSH_BLOCKED_STEP_DETAIL.to_string());
+
+ for (shape, step) in shapes {
+ // (a) The one open step of that tool, a note with no id: the held push gets the words,
+ // by its arguments or as the only one open (grok's failed step says just "failed")
+ for shown in [held_args.clone(), json!({"path": "src/lib.rs"})] {
+ let mut log = ActivityLog::default();
+ open(&mut log, "held", step(GROK, &shown));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("held", "failed")), blocked, "{shape}: the one open");
+ }
+
+ // (b) Two open, a note with no id and equal arguments: only the matching one gets the
+ // words, the other keeps its own 409, in both end orders
+ for held_first in [false, true] {
+ let mut log = ActivityLog::default();
+ open(&mut log, "other", step(GROK, &other_args));
+ open(&mut log, "held", step(GROK, &held_args));
+ note(&mut log, GROK, &held_args);
+ if held_first {
+ assert_eq!(words_of(&mut log, failed("held", gate)), blocked, "{shape}: held ends first");
+ assert_eq!(words_of(&mut log, failed("other", own)), own_reason, "{shape}: held ends first");
+ } else {
+ assert_eq!(words_of(&mut log, failed("other", own)), own_reason, "{shape}: other ends first");
+ assert_eq!(words_of(&mut log, failed("held", gate)), blocked, "{shape}: other ends first");
+ }
+ }
+
+ // Two open with shown arguments that don't match: never a guess between two
+ let mut log = ActivityLog::default();
+ open(&mut log, "other", step(GROK, &other_args));
+ open(&mut log, "held", step(GROK, &json!({"path": "src/lib.rs"})));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("other", own)), own_reason, "{shape}: two open, no guess");
+ assert_eq!(words_of(&mut log, failed("held", gate)), blocked, "{shape}: the one left open");
+
+ // An open grok call of another tool isn't one of this tool: the held one is alone
+ let mut log = ActivityLog::default();
+ open(&mut log, "issue", step("github-create_issue", &other_args));
+ open(&mut log, "held", step(GROK, &json!({"path": "src/lib.rs"})));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("held", gate)), blocked, "{shape}: another tool open");
+ assert_eq!(words_of(&mut log, failed("issue", own)), own_reason, "{shape}: another tool keeps its reason");
+
+ // (c) Each grok name is one tool: `-2` only matches its own step, even with equal
+ // arguments, in both end orders; the plain name's step keeps its own reason
+ for suffixed_first in [false, true] {
+ let mut log = ActivityLog::default();
+ open(&mut log, "plain", step(GROK, &held_args));
+ open(&mut log, "two", step("github-create_or_update_file-2", &held_args));
+ note(&mut log, "github-create_or_update_file-2", &held_args);
+ if suffixed_first {
+ assert_eq!(words_of(&mut log, failed("two", gate)), blocked, "{shape}: -2 ends first");
+ assert_eq!(words_of(&mut log, failed("plain", own)), own_reason, "{shape}: -2 ends first");
+ } else {
+ assert_eq!(words_of(&mut log, failed("plain", own)), own_reason, "{shape}: plain ends first");
+ assert_eq!(words_of(&mut log, failed("two", gate)), blocked, "{shape}: plain ends first");
+ }
+ }
+ // A grok step for another tool, alone, never takes the note; nor one called with only
+ // a prefix, nor the note's name without grok's server prefix
+ for (tool, args) in [
+ step("gitlab-create_or_update_file", &held_args),
+ kwant_prefix(step(GROK, &held_args)),
+ ] {
+ let mut log = ActivityLog::default();
+ open(&mut log, "x", (tool.clone(), args));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("x", own)), own_reason, "{shape}: {tool} is not {GROK}");
+ }
+ }
+
+ // A bare grok name (no server prefix) and a `use_tool` naming its call only in a file
+ // are never taken for the held call; the file one also blocks a guess for another use_tool
+ for (tool, args) in [(GROK.to_string(), held_args.clone()), ("use_tool".to_string(), json!({"file": "/tmp/call.json"}))] {
+ let mut log = ActivityLog::default();
+ open(&mut log, "x", (tool.clone(), args));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("x", own)), own_reason, "{tool} is not the held call");
+ }
+ let mut log = ActivityLog::default();
+ open(&mut log, "file", ("use_tool".to_string(), json!({"file": "/tmp/call.json"})));
+ open(&mut log, "held", use_tool(GROK, &json!({"path": "src/lib.rs"})));
+ note(&mut log, GROK, &held_args);
+ assert_eq!(words_of(&mut log, failed("held", gate)), ("failed", gate.to_string()), "a use_tool of unknown target is open: no guess");
+
+ // A note without the grok name (Codex's, Claude's) never matches a grok step, as before
+ let mut log = ActivityLog::default();
+ open(&mut log, "held", use_tool(GROK, &held_args));
+ log.refused_call(NOTED, None, Some(&held_args), crate::mcp::PREPUSH_BLOCKED_STEP_DETAIL);
+ assert_eq!(words_of(&mut log, failed("held", gate)), ("failed", gate.to_string()), "no grok name on the note");
+
+ // The step shapes themselves
+ assert_eq!(grok_gateway_call("use_tool", Some(&use_tool(GROK, &held_args).1)), Some((GROK, Some(&held_args))));
+ assert_eq!(grok_gateway_call("kwnt__github-create_or_update_file-2", Some(&held_args)), Some(("github-create_or_update_file-2", Some(&held_args))));
+ for (tool, args) in [
+ ("use_tool", json!({"file": "/tmp/call.json"})),
+ ("use_tool", json!({"tool_name": "github__create_issue", "tool_input": {}})),
+ ("use_tool", json!({"tool_name": "kwnt__", "tool_input": {}})),
+ ("kwnt__", json!({})),
+ ("kwnt_github-create_issue", json!({})),
+ ("mcp__kwnt__github__create_or_update_file", json!({})),
+ ("read_file", json!({"path": "a"})),
+ ] {
+ assert_eq!(grok_gateway_call(tool, Some(&args)), None, "{tool} {args}");
+ }
+ assert_eq!(grok_gateway_call("use_tool", None), None);
+ }
+
/// 28k: a denial whose card stopped waiting with no answer (it couldn't stay open, or it was
/// withdrawn) reads "Nobody answered in time", once per card; a Deny still reads as a no.
#[test]
diff --git a/src/mcp/session.rs b/src/mcp/session.rs
index d924bdd7..3cb27659 100644
--- a/src/mcp/session.rs
+++ b/src/mcp/session.rs
@@ -247,16 +247,19 @@ impl McpToolSession {
/// appended to the turn's `tool_calls`. `call_id` is also the engine's id for the call, the one
/// its activity step carries (the API driver's).
pub async fn call(&self, caller: &Caller, call_id: &str, name: &str, raw_arguments: &str) -> CallOutcome {
- self.call_step(caller, call_id, Some(call_id), name, raw_arguments).await
+ self.call_step(caller, call_id, Some(call_id), None, name, raw_arguments).await
}
/// [`call`](Self::call) for a call whose engine id, `step_id`, differs from kwnt's `call_id`
/// (the gateway's), or is unknown: a refusal kwnt notes on its activity step is keyed to it (28q).
+ /// `called_as` is the name the engine called `name` under when that differs (grok's, 29g), so
+ /// the refusal also finds a step that carries that name and no id.
pub async fn call_step(
&self,
caller: &Caller,
call_id: &str,
step_id: Option<&str>,
+ called_as: Option<&str>,
name: &str,
raw_arguments: &str,
) -> CallOutcome {
@@ -318,7 +321,7 @@ impl McpToolSession {
// 28k: a push the gate holds shows "Blocked before push" on its step; the bot
// gets the gate's own text. 28q: on its own step, never on another call of the tool
let held = || {
- crate::server::activity::refused(&caller.turn_id, name, step_id, &arguments, super::PREPUSH_BLOCKED_STEP_DETAIL)
+ crate::server::activity::refused_as(&caller.turn_id, name, called_as, step_id, &arguments, super::PREPUSH_BLOCKED_STEP_DETAIL)
};
match self.ctx.approver.prepush_gate(&req).await {
Err(why) => {
diff --git a/src/server/activity.rs b/src/server/activity.rs
index 46421735..b252b8f1 100644
--- a/src/server/activity.rs
+++ b/src/server/activity.rs
@@ -236,8 +236,21 @@ pub fn approval_waiting(turn_id: &str, ticket: &str, tool_name: &str) {
/// engine's id for the call when kwnt was told it, and `arguments` its arguments, so the words land
/// on that call's step and not on another call of the same tool (28q).
pub fn refused(turn_id: &str, tool_name: &str, call: Option<&str>, arguments: &Value, words: &'static str) {
+ refused_as(turn_id, tool_name, None, call, arguments, words);
+}
+
+/// [`refused`] for a call the engine made under another name, `called_as`: grok's name for
+/// `tool_name` on kwnt's gateway, which grok's step carries instead of an id (29g).
+pub fn refused_as(
+ turn_id: &str,
+ tool_name: &str,
+ called_as: Option<&str>,
+ call: Option<&str>,
+ arguments: &Value,
+ words: &'static str,
+) {
if let Some(turn) = TurnActivity::find(turn_id) {
- turn.with_log(|log| log.refused_call(tool_name, call, Some(arguments), words));
+ turn.with_log(|log| log.refused_call_as(tool_name, called_as, call, Some(arguments), words));
}
}
diff --git a/src/server/mcp_runtime.rs b/src/server/mcp_runtime.rs
index 424da07f..e7af19f7 100644
--- a/src/server/mcp_runtime.rs
+++ b/src/server/mcp_runtime.rs
@@ -1684,10 +1684,12 @@ async fn gateway_rpc(
let tool_name = tools
.canonical_name(naming, name)
.unwrap_or_else(|| name.to_string());
+ // 29g: grok sends no id; its step carries the grok name, which only this map can read
+ let called_as = (naming == GatewayNaming::Grok).then_some(name);
// Dropping the call withdraws its card (or stops the forwarded call) when the turn ends
let (caller, raw_arguments) = (session.caller(), arguments.to_string());
let outcome = tokio::select! {
- outcome = tools.call_step(&caller, &call_id, step_id, &tool_name, &raw_arguments) => outcome,
+ outcome = tools.call_step(&caller, &call_id, step_id, called_as, &tool_name, &raw_arguments) => outcome,
_ = session.ended.cancelled() => {
let (server, tool) = mcp::split_tool_name(&tool_name).unwrap_or(("", name));
session.ctx.record(cancelled_record(&call_id, &tool_name, server, tool, &arguments));