Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(hook): skip session-start handoff fetch for agents that ignore stdout (grok)

Djalma Júnior9f3db3f54341
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ed2fb1a5..934b41ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,9 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
scripts and emits the native `ai-memory hook --event … --agent grok` command.
ai-memory entries merge into a dedicated `ai-memory.json`, leaving any
third-party `~/.grok/hooks/*.json` untouched. NOTE: Grok ignores hook stdout on
- `SessionStart`, so capture works but handoff injection does not — recover a
- prior session's handoff via the MCP `memory_handoff_accept` tool. Adds
- `AgentKind::Grok` (`grok` wire tag).
+ `SessionStart`, so capture works but handoff injection does not. Grok's
+ `session-start` therefore skips the handoff fetch entirely — the fetch is
+ destructive (it marks the handoff accepted server-side) and Grok would discard
+ the result, silently losing the handoff; recover a prior session's handoff via
+ the MCP `memory_handoff_accept` tool instead. Adds `AgentKind::Grok` (`grok`
+ wire tag) and the `AgentKind::session_start_injects_handoff` gate.
## [1.0.3] - 2026-06-13
### Added
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 3e82a12e..d28c9832 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -14,6 +14,7 @@ use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Duration;
+use ai_memory_core::AgentKind;
use ai_memory_llm::OidcToken;
use crate::cli::HookArgs;
@@ -138,20 +139,27 @@ pub async fn run(data_dir: Option<PathBuf>, args: HookArgs) -> anyhow::Result<()
// abruptly), then fetch + inject the pending handoff for the resuming agent.
if args.event == "session-start" {
let _ = hook_spool::drain(&spool, &dd, start_drain_budget(), drain_event_timeout()).await;
- let client = build_client();
- let bearer = hook_spool::resolve_bearer(&client, &dd, args.auth_token.as_deref()).await;
- let handoff_url = format!("{base}/handoff?agent={}{qs}", args.agent);
- if let Some(handoff) =
- get_handoff(&client, &handoff_url, bearer.as_deref(), handoff_timeout()).await
- {
- let envelope = serde_json::json!({
- "hookSpecificOutput": {
- "hookEventName": "SessionStart",
- "additionalContext": handoff,
- }
- });
- println!("{envelope}");
- return Ok(());
+ // Only fetch the handoff for agents that inject the session-start
+ // hook's stdout as context. Grok ignores it, so fetching here would
+ // consume the handoff server-side (the GET is destructive) and then
+ // discard the result — silently losing it. Those agents recover the
+ // handoff on demand via the MCP `memory_handoff_accept` tool.
+ if AgentKind::from_wire(&args.agent).session_start_injects_handoff() {
+ let client = build_client();
+ let bearer = hook_spool::resolve_bearer(&client, &dd, args.auth_token.as_deref()).await;
+ let handoff_url = format!("{base}/handoff?agent={}{qs}", args.agent);
+ if let Some(handoff) =
+ get_handoff(&client, &handoff_url, bearer.as_deref(), handoff_timeout()).await
+ {
+ let envelope = serde_json::json!({
+ "hookSpecificOutput": {
+ "hookEventName": "SessionStart",
+ "additionalContext": handoff,
+ }
+ });
+ println!("{envelope}");
+ return Ok(());
+ }
}
}
diff --git a/crates/ai-memory-core/src/ids.rs b/crates/ai-memory-core/src/ids.rs
index 8388d480..4f1e2266 100644
--- a/crates/ai-memory-core/src/ids.rs
+++ b/crates/ai-memory-core/src/ids.rs
@@ -235,6 +235,23 @@ impl AgentKind {
_ => Self::Other,
}
}
+
+ /// Whether this agent injects the native `session-start` hook's stdout
+ /// into the resuming session as context. Agents that consume it return
+ /// `true` (Claude Code reads `hookSpecificOutput.additionalContext`).
+ ///
+ /// Grok ignores hook stdout on `SessionStart` (per Grok's hooks docs:
+ /// "For events like SessionStart or PostToolUse, stdout is ignored"), so
+ /// the native hook must NOT fetch the handoff for it: the fetch is
+ /// **destructive** (the server marks the handoff accepted) and the result
+ /// would be discarded — silently losing the handoff. For such agents the
+ /// handoff stays available on demand via the MCP `memory_handoff_accept`
+ /// tool. Only the native-hook agents (claude-code, grok) reach this code;
+ /// the others inject via their own staged-script / plugin formats.
+ #[must_use]
+ pub fn session_start_injects_handoff(self) -> bool {
+ !matches!(self, Self::Grok)
+ }
}
#[cfg(test)]
@@ -274,6 +291,11 @@ mod tests {
);
// Unknown tags still degrade to Other.
assert_eq!(AgentKind::from_wire("grok-2"), AgentKind::Other);
+ // Grok cannot inject the session-start handoff (ignores hook stdout);
+ // every other agent can.
+ assert!(!AgentKind::Grok.session_start_injects_handoff());
+ assert!(AgentKind::ClaudeCode.session_start_injects_handoff());
+ assert!(AgentKind::Codex.session_start_injects_handoff());
}
#[test]