Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): keep a Claude parent session when its subagent drops
AkitaOnRailsba56b8920c0f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ccb6a3fb..86ef660e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -15,7 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Fixed `drop_subagent_captures` discarding entire top-level Claude Codesessions launched with `--agent`. An `agent_type` alone no longer marks asession as a subagent; `agent_id` and Grok's `subagentType` still do, so- actual subagent filtering remains enabled. (#1041)+ actual subagent filtering remains enabled. Claude Code reports a Task+ subagent under its parent's session id, so a subagent's events no longer+ mark that session as a subagent: the parent's Stop, SessionEnd, summary and+ handoff are kept. (#1041, #1048)- Fixed interrupted launchers blocking an immediate managed-workstream restartby adding explicit `ai-memory run --force-unlock` recovery. The serveratomically expires and replaces only a lease attributed to the samediff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex 224371fe..1da2140d 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -1184,6 +1184,10 @@ const fn canonical_tool_name(family: ToolFamily) -> &'static str {/// `stop` / `session_end`) of a session already known to be a subagent. No-op/// (returns `false`) unless this event's project opted in via the per-event/// `drop_subagent` flag (sourced from its `.ai-memory.toml`).+///+/// Claude Code is the exception to seeding: its subagent events carry the+/// parent's `session_id` and differ only by `agent_id`, so seeding would drop+/// the main session's own tail. Its marked events still drop one by one.async fn should_drop_subagent(state: &HookState,env: &HookEnvelope,@@ -1219,7 +1223,9 @@ async fn should_drop_subagent() || body_is_subagent(&env.raw);if marked {- state.subagent_sessions.lock().await.insert(key);+ if env.agent != AgentKind::ClaudeCode {+ state.subagent_sessions.lock().await.insert(key);+ }return true;}@@ -7200,6 +7206,84 @@ mod tests {}}+ /// Claude Code reports a Task subagent under the parent's `session_id`,+ /// told apart only by `agent_id`. The subagent's events drop, but they must+ /// not mark the shared session as a subagent and take the main tail with it.+ #[tokio::test]+ async fn claude_subagent_events_drop_without_dropping_the_parent_tail() {+ let tmp = TempDir::new().unwrap();+ let state = Arc::new(make_state(&tmp).await);+ let session = SessionId::new();+ let main = |event: &str| HookBatchItem {+ url: format!("http://h/hook?event={event}&agent=claude-code&drop_subagent=1"),+ body: serde_json::json!({+ "session_id": session.to_string(),+ "prompt": "Ship the parent change",+ "tool_name": "Bash",+ "tool_response": "done",+ }),+ };+ let subagent = |event: &str| HookBatchItem {+ url: format!("http://h/hook?event={event}&agent=claude-code&drop_subagent=1"),+ body: serde_json::json!({+ "session_id": session.to_string(),+ "agent_id": "agent-task-1",+ "agent_type": "Explore",+ "tool_name": "Grep",+ "tool_response": "subagent output",+ }),+ };+ let items = vec![+ main("session-start"),+ main("user-prompt-submit"),+ subagent("subagent-start"),+ subagent("pre-tool-use"),+ subagent("post-tool-use"),+ subagent("subagent-stop"),+ main("post-tool-use"),+ main("stop"),+ main("session-end"),+ ];+ let response = handle_hook_batch(+ State(state.clone()),+ None,+ None,+ None,+ HeaderMap::new(),+ Json(items),+ )+ .await+ .into_response();+ assert_eq!(response.status(), StatusCode::OK);+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)+ .await+ .unwrap();+ let ack: serde_json::Value = serde_json::from_slice(&bytes).unwrap();+ assert_eq!(ack["accepted"], 9, "stored and dropped events are acked");++ let metrics = state.ingest_metrics.snapshot();+ assert_eq!(+ metrics.dropped_by_policy, 4,+ "only the subagent's events drop"+ );+ assert_eq!(metrics.accepted, 5, "the parent's events, tail included");+ assert_eq!(+ state+ .reader+ .observations_for_session(session)+ .await+ .unwrap()+ .len(),+ 5,+ );+ let pages = session_pages(&state).await;+ assert_eq!(pages.len(), 1, "the parent session keeps its summary");+ assert!(+ open_handoff_exists(&state).await,+ "the parent session keeps its baton"+ );+ }+#[tokio::test]async fn handle_hook_batch_keeps_subagent_events_when_disabled() {let tmp = TempDir::new().unwrap();diff --git a/docs/marker-file.md b/docs/marker-file.mdindex 154806d0..ae7d3f17 100644--- a/docs/marker-file.md+++ b/docs/marker-file.md@@ -169,8 +169,11 @@ for instructions the agent is expected to obey every turn.subagent captures stored as usual. Top-level (non-subagent) sessions arealways stored regardless, including Claude Code sessions launched with`--agent`: `agent_type` alone describes the selected agent, while a non-empty-`agent_id` identifies a subagent. Grok's `subagentType` marker still identifies-subagent captures. This is per-project on purpose: there is no server-global+`agent_id` identifies a subagent. Claude Code reports a subagent under the+parent's `session_id`, so only the events carrying `agent_id` (plus+`SubagentStart`/`SubagentStop`) are dropped and the parent session's own+prompts, Stop, SessionEnd and summary are kept. Grok's `subagentType` marker+still identifies subagent captures. This is per-project on purpose: there is no server-globalswitch, so opting one noisy project in never sheds subagent captures for theothers on a shared instance.