Skip to content

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

Rust

fix(opencode): mark a subagent session's end so it cannot hand over a baton

Rudy Mizrahi Celeklie8fc46b2010e
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index 503ebb6a..9e154fa1 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -5290,7 +5290,12 @@ export const AiMemoryHooks: Plugin = async ({{ directory }}) => {{
if (event?.type === "session.deleted") {{
const info = properties.info ?? {{}};
const id = properties.sessionID ?? info.id;
- endSession(id, directory, info.directory);
+ // The marker has to ride the end event too: the router refuses to hand
+ // a baton to the next session only when a child session's `session-end`
+ // identifies itself, and it reads the top-level key alone (#755).
+ endSession(id, directory, info.directory, typeof info.parentID === "string" && info.parentID
+ ? {{ agent_id: info.parentID }}
+ : {{}});
}}
if (event?.type === "session.compacted") {{
const id = properties.sessionID;
@@ -12953,6 +12958,41 @@ model = "gpt-5"
assert!(plugin.contains("projectSource = \"repo-root\";"));
}
+ #[test]
+ fn opencode_plugin_marks_a_subagent_session_end() {
+ let plugin = build_opencode_plugin("http://127.0.0.1:49374", None, None, "denylist");
+ // Slice a single `event?.type === "..."` branch so an assertion cannot
+ // be satisfied by a marker that lives in a neighbouring branch.
+ let branch = |event: &str| {
+ let needle = format!("event?.type === \"{event}\"");
+ let start = plugin
+ .find(&needle)
+ .unwrap_or_else(|| panic!("{event} branch missing"));
+ let rest = &plugin[start..];
+ let end = rest[needle.len()..]
+ .find("event?.type ===")
+ .map(|offset| offset + needle.len())
+ .unwrap_or(rest.len());
+ &rest[..end]
+ };
+ // #755: the router refuses to hand the next session a sub-task baton
+ // only when a child session's `session-end` carries the marker, so the
+ // end event must forward parentID just like `session.created` does.
+ assert!(
+ branch("session.deleted").contains("agent_id: info.parentID"),
+ "session.deleted must forward the subagent marker"
+ );
+ assert!(
+ branch("session.deleted").contains("typeof info.parentID === \"string\""),
+ "a root session has no parentID and must stay unmarked"
+ );
+ // Controls: `session.created` already forwards the marker (so the
+ // branch slicer resolves real branches) and `session.idle` carries no
+ // marker (so the slicer is not silently returning the whole plugin).
+ assert!(branch("session.created").contains("agent_id: info.parentID"));
+ assert!(!branch("session.idle").contains("agent_id"));
+ }
+
#[test]
fn opencode2_plugin_binds_the_v2_api() {
let plugin = build_opencode2_plugin(