grok-build-upstream-mirror/crates/codegen/xai-grok-tools/src/normalization.rs

152 lines
6.2 KiB
Rust
Raw Normal View History

//! First-party tool normalization — the `ToolInput`-coupled projection on top
//! of the [`crate::tool_taxonomy`] leaf. Projects the agent's **typed**
//! [`ToolInput`] into the canonical input dict ([`canonical_input`]) and
//! resolves identity from registered metadata ([`tool_identity_of`]). The typed
//! value is the source of truth — robust to serde renames, exhaustiveness-checked.
use crate::registry::types::FinalizedToolset;
use crate::tool_taxonomy::field;
pub use crate::tool_taxonomy::{CanonicalToolMeta, ToolIdentity};
use crate::types::tool_io::ToolInput;
use crate::types::tool_metadata::ToolMetadata;
use serde::Serialize;
/// Resolve [`ToolIdentity`] from a tool's registered metadata.
pub fn tool_identity_of(metadata: &dyn ToolMetadata) -> ToolIdentity {
let kind = metadata.kind();
ToolIdentity {
tool_kind: kind,
namespace: metadata.tool_namespace(),
presentation_name: kind.presentation_name(),
read_only: metadata.is_read_only(),
}
}
/// Resolve `wire_name` in `toolset` and merge the canonical `x.ai/tool` object
/// into `existing` (see [`CanonicalToolMeta::merge_into`]). Returns `existing`
/// unchanged when the tool is unknown (uninitialized MCP, backend-hosted), so
/// markers like `bash_mode`/`backend` are never clobbered. This is the harness
/// entry point for stamping tool-call `_meta`.
pub fn merge_tool_meta(
toolset: &FinalizedToolset,
existing: Option<serde_json::Value>,
wire_name: &str,
parsed: Option<&ToolInput>,
) -> Option<serde_json::Value> {
match toolset.tool_identity(wire_name) {
Some(identity) => {
let meta =
CanonicalToolMeta::new(wire_name, &identity, parsed.and_then(canonical_input));
Some(meta.merge_into(existing))
}
None => existing,
}
}
/// Normalize a read offset to the 1-indexed canonical line. Readers
/// allow negative (from-end) offsets, which have no 1-indexed
/// equivalent and are dropped (consumers read `raw_input`); `0` coalesces to
/// `1`. Shared by [`canonical_input`] and the harness ACP location line so a
/// single tool-call event never exposes two different start lines.
pub fn norm_offset_i64(offset: Option<i64>) -> Option<u64> {
match offset {
Some(o) if o >= 0 => Some(o.max(1) as u64),
_ => None,
}
}
/// Project a tool's **typed** input into the harness-independent `input` dict of
/// the `x.ai/tool` `_meta` object. Equivalent tools across toolsets emit the
/// same keys with the same meaning (a harness may add an extra key).
///
/// Returns `None` for tools with no stable cross-harness shape (MCP / dynamic /
/// codex / hashline / media / control-flow); the caller then omits `input`.
/// Absent optional fields are omitted (never `null`). Bulky payload fields
/// (edit `old_string`/`new_string`, full write contents) are never projected —
/// consumers read them from `raw_input`. Keys come from [`field`]; the match is
/// exhaustive so a new `ToolInput` variant must decide here.
pub fn canonical_input(input: &ToolInput) -> Option<serde_json::Value> {
use serde_json::Value;
/// Required field — omitted on serialization failure (absent fields are
/// contract-covered: consumers fall back to `raw_input`).
fn req(v: impl Serialize) -> Option<Value> {
serde_json::to_value(v).ok()
}
/// Optional field — `None` is dropped, never serialized as `null`.
fn opt<T: Serialize>(v: Option<T>) -> Option<Value> {
v.and_then(|v| serde_json::to_value(v).ok())
}
fn obj(pairs: impl IntoIterator<Item = (&'static str, Option<Value>)>) -> Value {
let mut m = serde_json::Map::new();
for (k, v) in pairs {
if let Some(v) = v {
m.insert(k.to_string(), v);
}
}
Value::Object(m)
}
Some(match input {
ToolInput::ReadFile(r) => obj([
(field::PATH, req(&r.path)),
(field::OFFSET, opt(norm_offset_i64(r.offset))),
(field::LIMIT, opt(r.limit)),
]),
ToolInput::Bash(b) => obj([
(field::COMMAND, req(&b.command)),
(field::DESCRIPTION, req(&b.description)),
]),
ToolInput::SearchReplace(s) => obj([(field::PATH, req(&s.file_path))]),
ToolInput::Write(w) => obj([(field::PATH, req(&w.file_path))]),
ToolInput::ListDir(l) => obj([(field::DIRECTORY, req(&l.target_directory))]),
ToolInput::Grep(g) => obj([
(field::PATTERN, req(&g.pattern)),
(field::PATH, opt(g.path.as_ref())),
]),
ToolInput::TodoWrite(_)
| ToolInput::Skill(_)
| ToolInput::MCPTool(_)
| ToolInput::TaskOutput(_)
| ToolInput::WaitTasks(_)
| ToolInput::KillTask(_)
| ToolInput::Task(_)
| ToolInput::WebSearch(_)
| ToolInput::ImageGen(_)
| ToolInput::ImageEdit(_)
| ToolInput::ImageToVideo(_)
| ToolInput::ReferenceToVideo(_)
| ToolInput::WebFetch(_)
| ToolInput::ApplyPatch(_)
| ToolInput::HashlineEdit(_)
| ToolInput::CodexReadFile(_)
| ToolInput::CodexListDir(_)
| ToolInput::CodexGrepFiles(_)
| ToolInput::MemorySearch(_)
| ToolInput::MemoryGet(_)
| ToolInput::SearchTool(_)
| ToolInput::UseTool(_)
| ToolInput::EnterPlanMode(_)
| ToolInput::ExitPlanMode(_)
| ToolInput::AskUserQuestion(_)
| ToolInput::Lsp(_)
| ToolInput::Monitor(_)
| ToolInput::SchedulerCreate(_)
| ToolInput::SchedulerDelete(_)
| ToolInput::SchedulerList(_)
| ToolInput::UpdateGoal(_)
Synced from monorepo Synced from monorepo Changes: - Shell: accept target response id on rewind execute - Shell: stamp response id on chat user message chunks - Worktree: optional rebuild and stale git registration cleanup in auto-GC - Worktree: kind-aware auto-GC TTLs and config knobs - Worktree: macOS process CWD scan and Unix PID liveness for GC guards - Worktree: automatic throttled GC on startup (Linux age-based; non-Linux dead-only) - Pager: add `[ui].combine_queued_prompts` to batch queued follow-ups - Shell: stop overwriting user skills - Tools: read markdown in `skills/` directories untruncated - `/usage` shows per-session token and dollar usage in the TUI - Security: prompt on environment-dumping `ps` variants - Security: always-safe `kubectl` no longer runs arbitrary kubeconfig credential plugins without permission - Tools: make scheduler deletion durable - Shell: add relocation storage primitives - Shell: give side model calls their own conversation ids - Fix five workflow-runtime bugs (budget, pause, cancel, reconnect) - Security: peel `env -S` / `--split-string` operands in the Bash permission gate (managed deny/ask) - Pager: expose doctor in the TUI - Security: block unauthorized RCE via abused safe commands - Pager idle watcher cue: "1 subagent still running" instead of "watching · 1 subagent" - Security: block `rg --pre` arbitrary code execution in auto-mode - Voice: diagnose silent-mic failures (macOS permission) and add doctor/terminal-setup Voice section - App builder deployer: `allow_forking` and `show_built_with_grok` - Pager: stop stacking duplicate "Worked for" markers on parked turns - Shell: support `max` as a distinct reasoning effort tier - Tools: serialize background `/loop` fires on the whole work unit - Shell: add working-directory relocation state primitives - Proto: `ClientToolResult` and `ChatConfig` client-side tools - Shell: model providers - Chat: select App Builder product on the Build path - Shell: attach author identity to feedback when the deployment opts in - Doctor: fix for SSH wrap setup - Workflow authoring skills: create-workflow and import-claude-workflow docs - Add read-only grok doctor - Sandbox: apply Landlock without a controlling TTY - Pager: recover image paste over grok wrap on headless remotes - Pager: make actions screen-mode aware - Shell: resume sessions when the working directory moves - Pager: centralize terminal diagnostics - Workspace: gate inline shell file access - Pager: centralize terminal probes - Pager: edit minimal prompts in an external editor - Pager: standardize backgrounding on Ctrl+B - Shell: recap rides the parent turn's prompt cache - Tools: add scheduler lifecycle version clock Source-Revision: 0f4d7c91b8b2b408333f6de1e8a76cb8eaa71899
2026-07-21 18:10:23 +00:00
| ToolInput::Workflow(_)
| ToolInput::Dynamic(_) => return None,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(v: serde_json::Value) -> ToolInput {
serde_json::from_value(v).expect("valid ToolInput")
}
#[test]
fn canonical_omits_absent_options_not_null() {
Synced from monorepo Changes: - Non-blocking coding-data sharing upsell banner - Consolidate remediation in Doctor - Auto mode defers fail-closed gate asks to the classifier - Coalesce marketplace list fetches - Allow removing a marketplace source by name - Contain hung git marketplace sources (timeouts, non-blocking refresh, unbrick modal) - Label failed workspace RPCs with error_kind - Drop redundant explicit tonic/prost deps from xai-grok-shell - Report real exit codes for completed background shells - Narrow the date-rollover reminder to date-bearing templates - Wire toolOverrides through the session and agent - Security: Bash(git:*) allowlist matches whole command chain by prefix - Split prompt-trigger telemetry and record classifier provenance - Raise connectors-manager timeout to 60s - Auto classifier honors recorded approvals for repeat actions - Apply doctor fixes in the TUI - Auto-mode classifier timeouts prompt instead of silently denying - Scope subagent completion drains to the owning session - Add the toolOverrides wire types - Set client_identifier=grok-agent-sdk - Accept both spellings of the workspace-teleport kill switch - Persist one-shot occurrence journal - Stop turns that poll the exact same tool call 16x in a row - Copy compaction checkpoint files when forking sessions - Auto-focus permission prompt from scrollback - Esc cancels the running turn in non-vim and minimal modes - List Ctrl+Z undo and redo in keyboard shortcuts - Out-of-process macOS mic capture - Show active auth mode on session-info - Install the npm binary under $GROK_HOME - Remove hover/click dead zones between dashboard items - Route startup warnings to doctor - Document [feedback.user] author identity config - Extend bang command timeout - Close combine-queued edit-hold race - Integrate relocation recovery - Expose privacy notice rollout flag - Break harness discovery ref cycle so connections can idle-evict - Shift/Alt+Enter inserts newline when editing a queued prompt - Gate project Claude permissions on folder trust - Echo response.create.event_id on response.created - Toast when session creation fails from disk full - Add shared test process lifecycle - Enable dynamic workflows by default - Add relocation transaction state machine - Add shared test sandbox - Surface auth failures on model-switch compact - Persist durable scheduler expiry - Confirm before removing extensions-modal items - Re-run compact and prompt after login when compact hit expired auth - Recap sends hosted tools under backend search
2026-07-22 19:18:53 +01:00
let grok = parse(serde_json::json!({"variant":"ReadFile","target_file":"/a"}));
let g = canonical_input(&grok).unwrap();
let keys: Vec<&String> = g.as_object().unwrap().keys().collect();
assert_eq!(
keys,
vec!["path"],
"absent offset/limit must be omitted, not null"
);
}
}