Skip to content

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

Rust

fix(plugins): start the Claude Code launcher with sh

AkitaOnRails09deb4f5141f
diff --git a/crates/ai-memory-cli/src/commands/render_plugins.rs b/crates/ai-memory-cli/src/commands/render_plugins.rs
index e6e0d223..cc187a4c 100644
--- a/crates/ai-memory-cli/src/commands/render_plugins.rs
+++ b/crates/ai-memory-cli/src/commands/render_plugins.rs
@@ -191,6 +191,9 @@ fn claude_code_plugin(version: &str) -> Vec<PluginFile> {
),
},
});
+ // Run through `sh`, as the Codex plugin does, rather than executing the
+ // launcher: a plugin uploaded as a desktop zip can lose the exec bit, and
+ // Windows Git Bash does not honour it at all.
let mut hooks = serde_json::Map::new();
for (name, script) in CLAUDE_CODE_EVENTS {
let event = script.trim_end_matches(".sh");
@@ -198,7 +201,7 @@ fn claude_code_plugin(version: &str) -> Vec<PluginFile> {
name.to_owned(),
json!([{ "matcher": "", "hooks": [{
"type": "command",
- "command": format!("\"${{CLAUDE_PLUGIN_ROOT}}\"/{LAUNCHER_PATH} claude-code {event}"),
+ "command": format!("sh \"${{CLAUDE_PLUGIN_ROOT}}/{LAUNCHER_PATH}\" claude-code {event}"),
}]}]),
);
}
@@ -353,7 +356,7 @@ mod tests {
assert_eq!(
command,
format!(
- "\"${{CLAUDE_PLUGIN_ROOT}}\"/scripts/ai-memory-hook claude-code {}",
+ "sh \"${{CLAUDE_PLUGIN_ROOT}}/scripts/ai-memory-hook\" claude-code {}",
script.trim_end_matches(".sh")
)
);
diff --git a/crates/ai-memory-cli/tests/suite/plugins.rs b/crates/ai-memory-cli/tests/suite/plugins.rs
index d71a7967..4b3eec81 100644
--- a/crates/ai-memory-cli/tests/suite/plugins.rs
+++ b/crates/ai-memory-cli/tests/suite/plugins.rs
@@ -554,6 +554,59 @@ mod launcher {
assert_eq!(sb.spooled().len(), 2, "folderless Droid session");
}
+ /// The committed hooks.json command still works when the launcher lost
+ /// its exec bit (a desktop zip upload, a Windows checkout): Claude Code
+ /// runs the command line through a shell, and it starts the launcher
+ /// with `sh` instead of executing it.
+ #[test]
+ fn the_hook_command_runs_a_launcher_without_the_exec_bit() {
+ use std::os::unix::fs::PermissionsExt as _;
+
+ let sb = Sandbox::new();
+ let source = repo_root().join("plugins").join("claude-code");
+ let plugin = sb.home.join("unzipped-plugin");
+ std::fs::create_dir_all(plugin.join("scripts")).unwrap();
+ let launcher = plugin.join("scripts/ai-memory-hook");
+ std::fs::copy(source.join("scripts/ai-memory-hook"), &launcher).unwrap();
+ std::fs::set_permissions(&launcher, std::fs::Permissions::from_mode(0o644)).unwrap();
+ let hooks: Value = serde_json::from_str(
+ &std::fs::read_to_string(source.join("hooks/hooks.json")).unwrap(),
+ )
+ .unwrap();
+ let command = hooks["hooks"]["UserPromptSubmit"][0]["hooks"][0]["command"]
+ .as_str()
+ .unwrap();
+
+ let mut cmd = hermetic("sh");
+ cmd.arg("-c")
+ .arg(command)
+ .current_dir(&sb.repo)
+ .env("HOME", &sb.home)
+ .env("PATH", "/usr/bin:/bin")
+ .env("CLAUDE_CONFIG_DIR", &sb.claude)
+ .env("CLAUDE_PLUGIN_ROOT", &plugin)
+ .env("CLAUDE_PLUGIN_OPTION_AI_MEMORY_BIN", BIN)
+ .env("CLAUDE_PLUGIN_OPTION_DATA_DIR", &sb.data)
+ .env("CLAUDE_PLUGIN_OPTION_SERVER_URL", &sb.server)
+ .env_remove("CLAUDE_PROJECT_DIR")
+ .env_remove("PLUGIN_ROOT")
+ .env_remove("DROID_PLUGIN_ROOT")
+ .env("RUST_LOG", "off")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ let mut child = cmd.spawn().unwrap();
+ child
+ .stdin
+ .take()
+ .unwrap()
+ .write_all(sb.prompt().to_string().as_bytes())
+ .unwrap();
+ let output = child.wait_with_output().unwrap();
+ assert!(output.status.success(), "{output:?}");
+ assert_eq!(sb.spooled().len(), 1, "{output:?}");
+ }
+
/// A session with no project folder is not captured through the plugin.
#[test]
fn folderless_sessions_are_not_captured_through_the_plugin() {
diff --git a/docs/design-marketplace-plugins.md b/docs/design-marketplace-plugins.md
index 3ae9479b..8c24ecfa 100644
--- a/docs/design-marketplace-plugins.md
+++ b/docs/design-marketplace-plugins.md
@@ -558,15 +558,15 @@ matchers `install-hooks` writes today:
{
"description": "ai-memory lifecycle capture through the installed ai-memory binary",
"hooks": {
- "SessionStart": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code session-start" }] }],
- "UserPromptSubmit": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code user-prompt-submit" }] }],
- "PreToolUse": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code pre-tool-use" }] }],
- "PostToolUse": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code post-tool-use" }] }],
- "PreCompact": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code pre-compact" }] }],
- "Stop": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code stop" }] }],
- "SessionEnd": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code session-end" }] }],
- "SubagentStart": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code subagent-start" }] }],
- "SubagentStop": [{ "matcher": "", "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code subagent-stop" }] }]
+ "SessionStart": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code session-start" }] }],
+ "UserPromptSubmit": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code user-prompt-submit" }] }],
+ "PreToolUse": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code pre-tool-use" }] }],
+ "PostToolUse": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code post-tool-use" }] }],
+ "PreCompact": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code pre-compact" }] }],
+ "Stop": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code stop" }] }],
+ "SessionEnd": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code session-end" }] }],
+ "SubagentStart": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code subagent-start" }] }],
+ "SubagentStop": [{ "matcher": "", "hooks": [{ "type": "command", "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code subagent-stop" }] }]
}
}
```
diff --git a/plugins/claude-code/hooks/hooks.json b/plugins/claude-code/hooks/hooks.json
index 6f796d9f..92addbc5 100644
--- a/plugins/claude-code/hooks/hooks.json
+++ b/plugins/claude-code/hooks/hooks.json
@@ -7,7 +7,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code session-start"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code session-start"
}
]
}
@@ -18,7 +18,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code user-prompt-submit"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code user-prompt-submit"
}
]
}
@@ -29,7 +29,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code pre-tool-use"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code pre-tool-use"
}
]
}
@@ -40,7 +40,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code post-tool-use"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code post-tool-use"
}
]
}
@@ -51,7 +51,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code pre-compact"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code pre-compact"
}
]
}
@@ -62,7 +62,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code stop"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code stop"
}
]
}
@@ -73,7 +73,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code session-end"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code session-end"
}
]
}
@@ -84,7 +84,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code subagent-start"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code subagent-start"
}
]
}
@@ -95,7 +95,7 @@
"hooks": [
{
"type": "command",
- "command": "\"${CLAUDE_PLUGIN_ROOT}\"/scripts/ai-memory-hook claude-code subagent-stop"
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/scripts/ai-memory-hook\" claude-code subagent-stop"
}
]
}