Skip to content

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

Rust

fix(workstream): exclude Grok's injected system reminders from the ledger

iagogfed4585a585de0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62930bb7..d485707d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,8 +22,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
acknowledged only after the child spawns). Transcript import reads
`$GROK_HOME/sessions/*/*/chat_history.jsonl` read-only with a
prefix-validated cursor and content-hash event ids so rewind-driven journal
- rewrites cannot duplicate history; system prompts, encrypted reasoning, and
- the injected `<user_info>` block are excluded as loss annotations. Discovery
+ rewrites cannot duplicate history; system prompts and encrypted reasoning
+ are excluded as loss annotations, as are the harness-injected `<user_info>`
+ and `<system-reminder>` blocks Grok stores inside `user` records (project
+ instructions, the skills catalogue, and connected MCP servers), which would
+ otherwise leak harness internals into the portable ledger and evict real
+ conversation from the startup packet budget. Discovery
matches checkouts through `summary.json`'s recorded `info.cwd` and honors
`GROK_HOME`. Grok stays out of the bare-mode automatic pool. Verified
against Grok Build CLI v0.2.111 ([#237]).
diff --git a/crates/ai-memory-workstream/src/transcript.rs b/crates/ai-memory-workstream/src/transcript.rs
index 49b7fef9..0252d85c 100644
--- a/crates/ai-memory-workstream/src/transcript.rs
+++ b/crates/ai-memory-workstream/src/transcript.rs
@@ -1164,8 +1164,17 @@ fn codex_synthetic_context(agent: AgentKind, role: &str, text: &str) -> bool {
|| trimmed.starts_with("<INSTRUCTIONS>")
}
AgentKind::ClaudeCode => trimmed.starts_with("<system-reminder>"),
- // Grok injects an environment block into the first user message.
- AgentKind::Grok => trimmed.starts_with("<user_info>"),
+ // Grok stores harness scaffolding inside `user` records rather than a
+ // separate record type: an environment block, then Claude-style
+ // reminders carrying project instructions, the skills catalogue, and
+ // the connected MCP servers. A single session's reminders measured
+ // 42KB against 270 bytes of real input, so importing them both leaks
+ // harness internals into the portable ledger and evicts real
+ // conversation from the startup packet budget. Genuine input arrives
+ // wrapped in `<user_query>`.
+ AgentKind::Grok => {
+ trimmed.starts_with("<user_info>") || trimmed.starts_with("<system-reminder>")
+ }
_ => false,
}
}
@@ -2843,6 +2852,45 @@ mod tests {
assert!(cursor.prefix_sha256.is_some());
}
+ /// Grok keeps harness scaffolding in `user` records, unlike Kimi, which
+ /// isolates it in `config.update` records the adapter never reads. A real
+ /// session measured 42KB of reminders (skills catalogue plus connected MCP
+ /// servers) against 270 bytes of genuine input, which both leaked harness
+ /// internals into the ledger and evicted real conversation from the
+ /// startup packet budget.
+ #[test]
+ fn grok_adapter_excludes_injected_system_reminders_from_user_records() {
+ let records = [
+ json!({"type":"user","content":[{"type":"text","text":"<user_info>\nOS Version: linux\n</user_info>"}]}),
+ json!({"type":"user","content":[{"type":"text","text":"<system-reminder>\nAs you answer the user's questions, you can use the following context\n</system-reminder>"}]}),
+ // Indented in real sessions, so the check must tolerate leading space.
+ json!({"type":"user","content":[{"type":"text","text":" <system-reminder> The following skills are available for use: ego-browser…</system-reminder>"}]}),
+ json!({"type":"user","content":[{"type":"text","text":"<system-reminder>\nMCP servers connected: - ai-memory (16 tools)\n</system-reminder>"}]}),
+ json!({"type":"user","content":[{"type":"text","text":"<user_query>\nfix the bug\n</user_query>"}]}),
+ ];
+ let mut events = Vec::new();
+ let mut losses = Vec::new();
+ for (index, record) in records.iter().enumerate() {
+ parse_grok(
+ record,
+ "019f-session",
+ &format!("record-{index}"),
+ &mut events,
+ &mut losses,
+ );
+ }
+
+ let contents: Vec<_> = events
+ .iter()
+ .map(|event| event.content.as_str())
+ .collect::<Vec<_>>();
+ assert_eq!(
+ contents,
+ ["<user_query>\nfix the bug\n</user_query>"],
+ "only genuine user input may reach the ledger"
+ );
+ }
+
#[test]
fn grok_export_resets_cursor_after_an_in_place_journal_rewrite() {
let temp = tempfile::tempdir().unwrap();