Skip to content

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

Rust

fix(setup): do not report ready while Codex has not trusted its hooks

AkitaOnRailsfc0e35322260
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 59269ef5..5ad3bd0f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,12 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Fixed `ai-memory setup` reporting "ai-memory is set up" after it installed
+ Codex hooks that Codex had not trusted yet. Codex skips an untrusted hook,
+ so Codex sessions went uncaptured. When Codex was wired, `setup` now asks
+ Codex for the hooks' trust state, the way `doctor` does, and when any are
+ untrusted or modified it names them and says to open Codex, run `/hooks`,
+ and trust them. It still exits 0. (#1313)
- Fixed `install-instructions` treating a line that is exactly `@AGENTS.md` as
proof that Claude Code loads the `AGENTS.md` being written. Claude Code
resolves `@path` from the file that contains the import, including in the
diff --git a/crates/ai-memory-cli/src/commands/doctor.rs b/crates/ai-memory-cli/src/commands/doctor.rs
index 4fadd4d2..e9b26185 100644
--- a/crates/ai-memory-cli/src/commands/doctor.rs
+++ b/crates/ai-memory-cli/src/commands/doctor.rs
@@ -688,7 +688,7 @@ pub async fn run(config: &Config, args: crate::cli::DoctorArgs) -> Result<()> {
/// The trust state of the installed ai-memory Codex hooks, when there are
/// any: asking Codex starts it, so a machine without ai-memory's Codex hooks
/// is left alone.
-async fn codex_hook_trust(
+pub(super) async fn codex_hook_trust(
env: &crate::config::RuntimeEnv,
cwd: &Path,
) -> Option<super::codex_hook_trust::CodexHookTrust> {
diff --git a/crates/ai-memory-cli/src/commands/setup.rs b/crates/ai-memory-cli/src/commands/setup.rs
index d9bf6ce1..648a69e7 100644
--- a/crates/ai-memory-cli/src/commands/setup.rs
+++ b/crates/ai-memory-cli/src/commands/setup.rs
@@ -530,9 +530,8 @@ async fn execute(
println!("{hint}");
}
if problems.is_empty() {
- println!(
- "ai-memory is set up. Restart your agents so they load the new hooks and MCP entry."
- );
+ let codex = codex_hook_trust(&config, &plan).await;
+ println!("{}", ready_message(codex.as_ref()));
return Ok(());
}
for problem in &problems {
@@ -544,6 +543,57 @@ async fn execute(
)
}
+/// Codex's trust state for the ai-memory hooks setup left in place: the
+/// hooks file `install-hooks` wrote, or the enabled plugin's hooks. `None`
+/// when Codex was not wired or its state is unknown (Codex not installed or
+/// not answering).
+async fn codex_hook_trust(
+ config: &Config,
+ plan: &SetupPlan,
+) -> Option<super::codex_hook_trust::CodexHookTrust> {
+ let codex = plan
+ .agents
+ .iter()
+ .find(|agent| agent.agent == AgentChoice::Codex)?;
+ let cwd = std::env::current_dir().ok()?;
+ if codex.plugin {
+ super::codex_hook_trust::probe(
+ Path::new("codex"),
+ super::codex_hook_trust::HookSource::Plugin(agent_plugins::PLUGIN_ID),
+ &cwd,
+ )
+ .await
+ } else {
+ doctor::codex_hook_trust(&config.runtime_env, &cwd).await
+ }
+}
+
+/// The closing line of a run with no problems. Codex skips a hook the user
+/// has not trusted in its `/hooks` review, so with any skipped the install
+/// is not ready for Codex yet. Setup still succeeds: only the user can grant
+/// that trust, inside Codex, and re-running setup would not change it.
+fn ready_message(codex: Option<&super::codex_hook_trust::CodexHookTrust>) -> String {
+ let restart = "Restart your agents so they load the new hooks and MCP entry.";
+ match codex.filter(|trust| !trust.skipped.is_empty()) {
+ None => format!("ai-memory is set up. {restart}"),
+ Some(trust) => {
+ let skipped: Vec<String> = trust
+ .skipped
+ .iter()
+ .map(|(event, status)| format!("{event} ({status})"))
+ .collect();
+ format!(
+ "ai-memory is installed, but Codex has not approved its {} ai-memory hook(s) \
+ yet: {}. Codex skips a hook until you trust it, so Codex sessions are not \
+ captured until then. Action required: open Codex, run /hooks, and trust \
+ them. {restart}",
+ trust.skipped.len(),
+ skipped.join(", "),
+ )
+ }
+ }
+}
+
/// How a browser signs in to the web UI on a token-file install. With no
/// human accounts the server answers `/web` with a Basic challenge, and the
/// token file's contents are the password. The token itself is never printed.
@@ -855,6 +905,39 @@ mod tests {
assert!(AgentPlan::detect(AgentChoice::Codex, home.path(), &env).plugin);
}
+ /// Codex skips an untrusted hook, so a run that left any unapproved says
+ /// so and names `/hooks` instead of claiming the install is ready; all
+ /// trusted, or an unknown state, keeps the plain ready line.
+ #[test]
+ fn untrusted_codex_hooks_are_not_reported_as_ready() {
+ use super::super::codex_hook_trust::CodexHookTrust;
+ let ready = "ai-memory is set up.";
+
+ let untrusted = CodexHookTrust {
+ trusted: vec!["stop".to_owned()],
+ skipped: vec![
+ ("sessionStart".to_owned(), "untrusted".to_owned()),
+ ("userPromptSubmit".to_owned(), "modified".to_owned()),
+ ],
+ };
+ let message = ready_message(Some(&untrusted));
+ assert!(!message.contains(ready), "{message}");
+ assert!(message.contains("run /hooks"), "{message}");
+ assert!(
+ message.contains("2 ai-memory hook(s)")
+ && message.contains("sessionStart (untrusted)")
+ && message.contains("userPromptSubmit (modified)"),
+ "{message}"
+ );
+
+ let trusted = CodexHookTrust {
+ trusted: vec!["sessionStart".to_owned()],
+ skipped: Vec::new(),
+ };
+ assert!(ready_message(Some(&trusted)).starts_with(ready));
+ assert!(ready_message(None).starts_with(ready));
+ }
+
/// A token-file install learns how to open the web UI: the URL and the
/// file to read, never the token. An inline or absent token prints no hint.
#[test]
diff --git a/crates/ai-memory-cli/tests/suite/setup_e2e.rs b/crates/ai-memory-cli/tests/suite/setup_e2e.rs
index 7f9abff8..1e312d65 100644
--- a/crates/ai-memory-cli/tests/suite/setup_e2e.rs
+++ b/crates/ai-memory-cli/tests/suite/setup_e2e.rs
@@ -439,6 +439,54 @@ esac
);
}
+ /// Codex skips hooks the user has not trusted in `/hooks`. A `setup` that
+ /// wired Codex while Codex reports its hooks untrusted names the `/hooks`
+ /// approval instead of claiming everything is set up, and still exits 0:
+ /// only the user can grant that trust. Codex's `hooks/list` answer comes
+ /// from a fake `codex` first on `PATH`; the operator's Codex never runs.
+ #[tokio::test]
+ async fn setup_names_pending_codex_hook_trust_instead_of_claiming_ready() {
+ use std::os::unix::fs::PermissionsExt as _;
+ let sandbox = Sandbox::new();
+ let service = Service::new(&sandbox.root);
+ let hooks = sandbox.home.join(".codex/hooks.json");
+ let reply = json!({"jsonrpc": "2.0", "id": 2, "result": {"data": [{"hooks": [{
+ "eventName": "sessionStart", "trustStatus": "untrusted", "sourcePath": hooks,
+ "command": "ai-memory hook --event session-start --agent codex"
+ }]}]}})
+ .to_string();
+ let fake = service.bin.join("codex");
+ std::fs::write(
+ &fake,
+ format!(
+ "#!/bin/sh\nwhile IFS= read -r line; do\ncase \"$line\" in\n*'\"id\":2'*) \
+ printf '%s\\n' '{reply}' ;;\nesac\ndone\n"
+ ),
+ )
+ .unwrap();
+ std::fs::set_permissions(&fake, std::fs::Permissions::from_mode(0o755)).unwrap();
+ let port = crate::e2e_support::free_port();
+
+ let out = setup(&sandbox, &service, port, &["--yes", "--agent", "codex"]);
+ assert!(out.status.success(), "{}", text(&out));
+ let stdout = String::from_utf8_lossy(&out.stdout);
+ assert!(
+ std::fs::read_to_string(&hooks)
+ .unwrap()
+ .contains("ai-memory"),
+ "{}",
+ text(&out)
+ );
+ assert!(!stdout.contains("ai-memory is set up."), "{}", text(&out));
+ assert!(
+ stdout.contains("Codex has not approved its 1 ai-memory hook(s)")
+ && stdout.contains("sessionStart (untrusted)")
+ && stdout.contains("run /hooks"),
+ "{}",
+ text(&out)
+ );
+ }
+
fn uninstall(sandbox: &Sandbox, service: &Service, port: u16) -> Output {
let mut cmd = sandbox.cmd(Some(port));
service.apply(&mut cmd);
diff --git a/docs/install.md b/docs/install.md
index 5d113ce6..7cb0b379 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -131,7 +131,11 @@ the commands a native install otherwise needs one by one:
engine share a store with OpenCode and Kiro CLI, so they are wired only
when named, for example `--agent opencode2`.
4. A self-check that writes nothing: `GET /healthz`, a `memory_status` call
- over MCP with the install's credential, and `doctor`.
+ over MCP with the install's credential, and `doctor`. When Codex was wired,
+ `setup` also asks Codex whether it will run the ai-memory hooks. If Codex
+ has not trusted them yet, the closing line says so and tells you to open
+ Codex, run `/hooks`, and trust them, instead of reporting the install as
+ ready. That still exits 0, because only you can grant the trust in Codex.
Without `--yes`, `setup` prints this plan and asks on a terminal; with no
terminal (a script, an agent's tool call) it changes nothing and exits