Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 122 additions & 17 deletions atomic-agent/src/hooks/grok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
//! # JSON shape
//!
//! Grok's stdin envelope uses camelCase throughout (`sessionId`, `toolName`,
//! `toolResult`, …). Claude Code compatibility aliases are accepted via
//! serde `alias` so shared scripts still parse. Every event also carries
//! `cwd` / `workspaceRoot` which we surface as repo-root hints when the
//! process cwd is not the workspace.
//! `toolResult`, …) and, for Claude Code compatibility, repeats most
//! top-level keys in snake_case in the same payload (`session_id`,
//! `tool_name`, `tool_response`, …). `hookEventName` and `hook_event_name`
//! even carry different values (`stop` vs `Stop`). Either spelling is
//! accepted, alone or together; see `FIELD_SPELLINGS`. Every event also
//! carries `cwd` / `workspaceRoot` which we surface as repo-root hints when
//! the process cwd is not the workspace.
//!
//! `Stop` fires both on genuine turn completion (`reason: "end_turn"`) and
//! once more at session end (`channel_closed` / `shutdown`). Both map to
Expand All @@ -48,17 +51,43 @@ const ATOMIC_HOOK_PREFIX: &str = "atomic agent hooks grok";

// JSON input types (camelCase primary, snake_case aliases)

/// Spellings Grok may send for one [`CommonInput`] field, preferred first.
///
/// One payload can carry several spellings of the same field, which serde
/// `alias` rejects as a duplicate field, so [`canonicalize_fields`] folds
/// them into the field name before deserializing.
const FIELD_SPELLINGS: &[(&str, &[&str])] = &[
("session_id", &["sessionId", "session_id"]),
("transcript_path", &["transcriptPath", "transcript_path"]),
("workspace_root", &["workspaceRoot", "workspace_root"]),
("tool_name", &["toolName", "tool_name"]),
("tool_use_id", &["toolUseId", "tool_use_id"]),
("tool_input", &["toolInput", "tool_input"]),
(
"tool_result",
&["toolResult", "tool_result", "tool_response"],
),
("stop_hook_active", &["stopHookActive", "stop_hook_active"]),
(
"last_assistant_message",
&["lastAssistantMessage", "last_assistant_message"],
),
("permission_mode", &["permissionMode", "permission_mode"]),
("hook_event_name", &["hookEventName", "hook_event_name"]),
];

/// Fields arrive under their own name: [`canonicalize_fields`] has already
/// folded every spelling in [`FIELD_SPELLINGS`].
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct CommonInput {
// Grok primary: camelCase. Aliases accept Claude-style snake_case scripts.
#[serde(default, alias = "sessionId")]
#[serde(default)]
session_id: Option<String>,
#[serde(default, alias = "transcriptPath")]
#[serde(default)]
transcript_path: Option<String>,
#[serde(default)]
cwd: Option<String>,
#[serde(default, alias = "workspaceRoot")]
#[serde(default)]
workspace_root: Option<String>,
#[serde(default)]
model: Option<String>,
Expand All @@ -68,26 +97,26 @@ struct CommonInput {
source: Option<String>,
#[serde(default)]
prompt: Option<String>,
#[serde(default, alias = "toolName")]
#[serde(default)]
tool_name: Option<String>,
#[serde(default, alias = "toolUseId")]
#[serde(default)]
tool_use_id: Option<String>,
#[serde(default, alias = "toolInput")]
#[serde(default)]
tool_input: Option<Value>,
#[serde(default, alias = "toolResult", alias = "tool_response")]
#[serde(default)]
tool_result: Option<Value>,
/// Stop reason: `"end_turn"`, `"channel_closed"`, `"shutdown"`, …
#[serde(default)]
reason: Option<String>,
#[serde(default, alias = "stopHookActive")]
#[serde(default)]
stop_hook_active: Option<bool>,
#[serde(default, alias = "lastAssistantMessage")]
#[serde(default)]
last_assistant_message: Option<String>,
#[serde(default)]
timestamp: Option<String>,
#[serde(default, alias = "permissionMode")]
#[serde(default)]
permission_mode: Option<String>,
#[serde(default, alias = "hookEventName")]
#[serde(default)]
hook_event_name: Option<String>,
}

Expand Down Expand Up @@ -171,7 +200,7 @@ impl AgentHook for GrokHook {
fn parse_event(&self, hook_type: HookType, input: &[u8]) -> AgentResult<TurnEvent> {
let raw_json = self.parse_json(hook_type, input)?;
let timestamp = TimestampFormat::Rfc3339.parse(self.name(), hook_type, &raw_json)?;
let parsed = self.parse_common(hook_type, raw_json.clone())?;
let parsed = self.parse_common(hook_type, canonicalize_fields(&raw_json))?;

let mut raw_json = with_xai_provider(raw_json);
if hook_type == HookType::TurnEnd {
Expand Down Expand Up @@ -296,6 +325,34 @@ impl AgentHook for GrokHook {

// Normalization helpers

/// Rewrite the envelope so each [`CommonInput`] field appears once, under
/// its own name.
///
/// The first spelling in [`FIELD_SPELLINGS`] with a non-null value wins.
/// Every other key passes through unchanged.
fn canonicalize_fields(raw: &Value) -> Value {
let Some(obj) = raw.as_object() else {
return raw.clone();
};
let is_spelling = |key: &str| {
FIELD_SPELLINGS
.iter()
.any(|(_, spellings)| spellings.contains(&key))
};
let untouched = obj
.iter()
.filter(|(key, _)| !is_spelling(key))
.map(|(key, value)| (key.clone(), value.clone()));
let folded = FIELD_SPELLINGS.iter().filter_map(|(field, spellings)| {
spellings
.iter()
.filter_map(|spelling| obj.get(*spelling))
.find(|value| !value.is_null())
.map(|value| (field.to_string(), value.clone()))
});
Value::Object(untouched.chain(folded).collect())
}

/// Strip Grok harness wrappers from a user prompt.
///
/// Grok's agent runtime delivers the human message inside an XML-ish envelope:
Expand Down Expand Up @@ -634,6 +691,54 @@ mod tests {
assert_eq!(raw.get("provider").and_then(Value::as_str), Some("xai"));
}

// Real payloads carrying both spellings of every field are covered in
// `tests/grok_real_payloads.rs`.
#[test]
fn test_parse_prefers_camel_case_when_spellings_disagree() {
let hook = make_hook();
let input = br#"{"sessionId": "camel", "session_id": "snake"}"#;
let event = hook.parse_event(HookType::SessionStart, input).unwrap();
assert_eq!(event.session_id, "camel");
}

#[test]
fn test_parse_skips_a_null_spelling() {
let hook = make_hook();
let input = br#"{"sessionId": null, "session_id": "snake"}"#;
let event = hook.parse_event(HookType::SessionStart, input).unwrap();
assert_eq!(event.session_id, "snake");
}

#[test]
fn test_parse_every_spelling_null() {
let hook = make_hook();
let input = br#"{"sessionId": null, "session_id": null}"#;
let event = hook.parse_event(HookType::SessionStart, input).unwrap();
assert!(event.session_id.starts_with("grok-"));
}

#[test]
fn test_parse_tool_result_under_every_spelling() {
let hook = make_hook();
let input = br#"{
"sessionId": "s1",
"toolResult": "camel",
"tool_result": "sdk",
"tool_response": "claude"
}"#;
let event = hook.parse_event(HookType::PostToolUse, input).unwrap();
let raw = event.raw_json.unwrap();
assert_eq!(
raw.get("tool_output").and_then(Value::as_str),
Some("camel")
);
// The envelope reaches downstream as Grok sent it.
assert_eq!(
raw.get("tool_response").and_then(Value::as_str),
Some("claude")
);
}

#[test]
fn test_parse_empty_input() {
let hook = make_hook();
Expand Down
54 changes: 54 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/post_tool_use.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"hookEventName": "post_tool_use",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:10:58.231423+00:00",
"transcriptPath": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permissionMode": "default",
"toolName": "read_file",
"toolUseId": "call-e15f7982-e832-4e29-a8a6-0ea59a23b34b-0",
"toolInput": {
"target_file": "/Users/you/project/a.txt",
"limit": 5
},
"toolResult": {
"type": "ReadFile",
"FileContent": {
"content": "1→first line\n",
"content_concise": "1→first line\n",
"absolute_path": "/Users/you/project/a.txt",
"offset": null,
"limit": 5,
"raw_output": "first line\n",
"total_lines": 1
}
},
"toolInputTruncated": false,
"toolResultTruncated": false,
"durationMs": 5,
"isBackgrounded": false,
"hook_event_name": "PostToolUse",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"transcript_path": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permission_mode": "default",
"tool_name": "read_file",
"tool_input": {
"target_file": "/Users/you/project/a.txt",
"limit": 5
},
"tool_response": {
"type": "ReadFile",
"FileContent": {
"content": "1→first line\n",
"content_concise": "1→first line\n",
"absolute_path": "/Users/you/project/a.txt",
"offset": null,
"limit": 5,
"raw_output": "first line\n",
"total_lines": 1
}
},
"tool_use_id": "call-e15f7982-e832-4e29-a8a6-0ea59a23b34b-0",
"duration_ms": 5
}
26 changes: 26 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/pre_tool_use.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"hookEventName": "pre_tool_use",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:10:58.171140+00:00",
"transcriptPath": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permissionMode": "default",
"toolName": "read_file",
"toolUseId": "call-e15f7982-e832-4e29-a8a6-0ea59a23b34b-0",
"toolInput": {
"target_file": "/Users/you/project/a.txt",
"limit": 5
},
"toolInputTruncated": false,
"hook_event_name": "PreToolUse",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"transcript_path": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permission_mode": "default",
"tool_name": "read_file",
"tool_input": {
"target_file": "/Users/you/project/a.txt",
"limit": 5
},
"tool_use_id": "call-e15f7982-e832-4e29-a8a6-0ea59a23b34b-0"
}
14 changes: 14 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/session_end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"hookEventName": "session_end",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:11:00.474405+00:00",
"transcriptPath": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permissionMode": "default",
"reason": "shutdown",
"hook_event_name": "SessionEnd",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"transcript_path": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permission_mode": "default"
}
12 changes: 12 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/session_start.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"hookEventName": "session_start",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:10:52.790226+00:00",
"permissionMode": "default",
"source": "new",
"hook_event_name": "SessionStart",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"permission_mode": "default"
}
21 changes: 21 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/stop_end_turn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"hookEventName": "stop",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:11:00.132019+00:00",
"transcriptPath": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"promptId": "521d4615-47f7-4ad8-adde-307836fe26fc",
"permissionMode": "default",
"reason": "end_turn",
"stopHookActive": false,
"lastAssistantMessage": "The first line of `a.txt` is:\n\n```\nfirst line\n```",
"backgroundTasks": [],
"sessionCrons": [],
"hook_event_name": "Stop",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"transcript_path": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permission_mode": "default",
"stop_hook_active": false,
"last_assistant_message": "The first line of `a.txt` is:\n\n```\nfirst line\n```"
}
16 changes: 16 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/stop_shutdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hookEventName": "stop",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:11:00.476518+00:00",
"transcriptPath": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permissionMode": "default",
"reason": "shutdown",
"stopHookActive": false,
"hook_event_name": "Stop",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"transcript_path": "/Users/you/.grok/sessions/%2FUsers%2Fyou%2Fproject/01a1d576-264a-7530-a25e-d209bd34a860/updates.jsonl",
"permission_mode": "default",
"stop_hook_active": false
}
13 changes: 13 additions & 0 deletions atomic-agent/tests/fixtures/grok-1.0.77/user_prompt_submit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"hookEventName": "user_prompt_submit",
"sessionId": "01a1d576-264a-7530-a25e-d209bd34a860",
"cwd": "/Users/you/project",
"workspaceRoot": "/Users/you/project",
"timestamp": "2026-11-13T15:10:52.842761+00:00",
"promptId": "521d4615-47f7-4ad8-adde-307836fe26fc",
"permissionMode": "default",
"prompt": "Read the file a.txt and tell me its first line. Do not modify anything.",
"hook_event_name": "UserPromptSubmit",
"session_id": "01a1d576-264a-7530-a25e-d209bd34a860",
"permission_mode": "default"
}
Loading
Loading