Skip to content

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

Rust

fix(hooks): fit a Grok recap into the room a handoff leaves

Giovanni Coutinho7800873860bb
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index f078b094..93b574d0 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -555,6 +555,18 @@ fn write_success_response<W: std::io::Write>(
const GROK_ADDITIONAL_CONTEXT_CHARS: usize = 10_000;
+/// Grok carries the handoff and the recap in one `additionalContext`, clipped
+/// at [`GROK_ADDITIONAL_CONTEXT_CHARS`]. The recap is asked to fit the room
+/// the handoff leaves, so the clip never cuts its closing fence. `None` when
+/// that room is under the smallest recap the server renders: the recap then
+/// waits for the next tool result, which has the whole field to itself.
+fn grok_recap_room(handoff_chars: usize) -> Option<usize> {
+ const JOIN: usize = "\n\n".len();
+ GROK_ADDITIONAL_CONTEXT_CHARS
+ .checked_sub(handoff_chars + JOIN)
+ .filter(|room| *room >= ai_memory_hooks::RECAP_BUDGET_MIN)
+}
+
fn grok_post_tool_handoff_envelope(handoff: &str) -> serde_json::Value {
serde_json::json!({
"hookSpecificOutput": {
@@ -1334,8 +1346,16 @@ where
parts.push(handoff);
}
}
- if wants_recap && let Some(session_id) = canonical_session_id.as_deref() {
- let url = recap_url(base, &args.agent, session_id, None);
+ // With no handoff the server's default budget already fits.
+ let recap_budget = match parts.first() {
+ Some(handoff) => grok_recap_room(handoff.chars().count()).map(Some),
+ None => Some(None),
+ };
+ if wants_recap
+ && let Some(session_id) = canonical_session_id.as_deref()
+ && let Some(budget) = recap_budget
+ {
+ let url = recap_url(base, &args.agent, session_id, budget);
if let Some(recap) =
get_handoff(&client, &url, bearer.as_deref(), handoff_timeout()).await
{
@@ -7301,6 +7321,80 @@ mod tests {
assert!(recap_requests(&seen).is_empty(), "{seen:?}");
}
+ #[tokio::test]
+ async fn grok_recap_sharing_a_handoff_asks_for_the_room_left() {
+ let tmp = tempfile::tempdir().unwrap();
+ let data_dir = tmp.path().join("data");
+ write_recap_marker(&data_dir, Duration::from_secs(5));
+ // The fake server answers both GETs with this 3,000-char body.
+ let body: &'static str = Box::leak("H".repeat(3_000).into_boxed_str());
+ let (base, mut requests) = serve_requests("200 OK", body).await;
+ let stdout = run_hook(
+ &data_dir,
+ grok_hook_args("post-tool-use", &base),
+ grok_tool_payload(tmp.path()),
+ )
+ .await;
+ let envelope: serde_json::Value = serde_json::from_slice(stdout.trim_ascii()).unwrap();
+ let context = envelope["hookSpecificOutput"]["additionalContext"]
+ .as_str()
+ .unwrap();
+ assert_eq!(context.chars().count(), 3_000 + 2 + 3_000);
+ let seen = all_requests(&mut requests).await;
+ let recap = recap_requests(&seen);
+ assert_eq!(recap.len(), 1, "{seen:?}");
+ // 10,000 minus the handoff and the blank line joining them.
+ assert!(recap[0].contains("budget=6998"), "{}", recap[0]);
+ }
+
+ #[tokio::test]
+ async fn grok_recap_waits_for_the_next_tool_when_a_handoff_leaves_no_room() {
+ let tmp = tempfile::tempdir().unwrap();
+ let data_dir = tmp.path().join("data");
+ write_recap_marker(&data_dir, Duration::from_secs(5));
+ let body: &'static str = Box::leak("H".repeat(9_000).into_boxed_str());
+ let (base, mut requests) = serve_requests("200 OK", body).await;
+ let stdout = run_hook(
+ &data_dir,
+ grok_hook_args("post-tool-use", &base),
+ grok_tool_payload(tmp.path()),
+ )
+ .await;
+ let envelope: serde_json::Value = serde_json::from_slice(stdout.trim_ascii()).unwrap();
+ assert_eq!(
+ envelope["hookSpecificOutput"]["additionalContext"]
+ .as_str()
+ .unwrap()
+ .chars()
+ .count(),
+ 9_000,
+ "only the handoff"
+ );
+ let seen = all_requests(&mut requests).await;
+ assert!(recap_requests(&seen).is_empty(), "{seen:?}");
+ assert!(
+ data_dir
+ .join("recap-pending")
+ .join("grok-session")
+ .is_file(),
+ "the recap must stay pending"
+ );
+
+ // The next tool result has the whole context to itself.
+ let stdout = run_hook(
+ &data_dir,
+ grok_hook_args("post-tool-use", &base),
+ grok_tool_payload(tmp.path()),
+ )
+ .await;
+ assert!(!stdout.is_empty());
+ let seen = all_requests(&mut requests).await;
+ let recap = recap_requests(&seen);
+ assert_eq!(recap.len(), 1, "{seen:?}");
+ assert!(!recap[0].contains("budget="), "{}", recap[0]);
+ assert!(!data_dir.join("recap-pending").join("grok-session").exists());
+ }
+
#[tokio::test]
async fn grok_stale_recap_marker_is_ignored() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/ai-memory-hooks/src/lib.rs b/crates/ai-memory-hooks/src/lib.rs
index 6aaa7ad2..33ff39ac 100644
--- a/crates/ai-memory-hooks/src/lib.rs
+++ b/crates/ai-memory-hooks/src/lib.rs
@@ -58,6 +58,7 @@ pub use payload::{
USER_PROMPT_EXCERPT_MAX_BYTES, agent_from_payload, cap_lifecycle_body_for_client,
hook_payload_context, user_prompt_text,
};
+pub use recap::RECAP_BUDGET_MIN;
pub use router::{
DEFAULT_HOOK_INGEST_MAX_IN_FLIGHT, DEFAULT_INGEST_GATE_MAX_ENTRIES,
DEFAULT_PROJECT_CACHE_MAX_ENTRIES, HookState, IngestGates, IngestRateLimiter, ProjectCache,
diff --git a/crates/ai-memory-hooks/src/recap.rs b/crates/ai-memory-hooks/src/recap.rs
index 9ba29737..ef848b44 100644
--- a/crates/ai-memory-hooks/src/recap.rs
+++ b/crates/ai-memory-hooks/src/recap.rs
@@ -157,7 +157,8 @@ async fn build_recap(
/// Default recap size: under the 10,000-char cap Claude Code and Grok put on
/// one `additionalContext` field.
pub(crate) const RECAP_BUDGET_DEFAULT: usize = 8_000;
-const RECAP_BUDGET_MIN: usize = 2_000;
+/// The smallest recap `/recap` renders: a smaller `budget` is raised to it.
+pub const RECAP_BUDGET_MIN: usize = 2_000;
const RECAP_BUDGET_MAX: usize = 9_500;
/// The last reply and the handoffs each keep this much room when present,
/// so a long run of prompts cannot crowd them out.
diff --git a/docs/usage.md b/docs/usage.md
index 61b2cac4..30022281 100644
--- a/docs/usage.md
+++ b/docs/usage.md
@@ -192,7 +192,7 @@ agent, which then runs it without a recap. Prompts that do not start with
| Claude Code | With that prompt (`UserPromptSubmit` `additionalContext`, up to 8,000 chars) |
| Codex | With that prompt (developer context, up to 6,000 chars to stay under Codex's hook-output preview limit) |
| OpenCode V1 / V2 | With that prompt and later model calls, until the next compaction |
-| Grok Build | After the first tool result of that turn: Grok discards `UserPromptSubmit` output. A request with no tool call expires after 30 minutes or at session end |
+| Grok Build | After the first tool result of that turn: Grok discards `UserPromptSubmit` output. The recap shares that field with a handoff delivered at the same time and is sized to fit; when the handoff leaves too little room, it comes with the next tool result. A request with no tool call expires after 30 minutes or at session end |
A session that has not been compacted yet answers with a one-line notice. The
full record stays available through `memory_read_session_observations` with