Skip to content

fix(cli): refresh Codex hooks when MCP is already wired - #1150

Open
berthojoris wants to merge 2 commits into
rohitg00:mainfrom
berthojoris:feature/codex-native-hooks-harden
Open

fix(cli): refresh Codex hooks when MCP is already wired#1150
berthojoris wants to merge 2 commits into
rohitg00:mainfrom
berthojoris:feature/codex-native-hooks-harden

Conversation

@berthojoris

@berthojoris berthojoris commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an issue where agentmemory connect codex --with-hooks skipped installing/refreshing ~/.codex/hooks.json if config.toml was already wired with MCP servers (already-wired status).

Changes

  • CLI Adapter (src/cli/connect/codex.ts): --with-hooks execution is now independent of MCP wiring status, allowing users to refresh hook script paths without needing --force.
  • Warning Logs: Properly surface skipped hook results in dry-run and already-wired execution paths.
  • Tests (test/codex-connect-hooks.test.ts): Added unit tests covering --with-hooks refresh behavior, idempotency, custom hook preservation, and Windows USERPROFILE path isolation.

Test Plan

  • npx vitest run test/codex-connect-hooks.test.ts (14/14 passed)
  • Tested idempotency and custom hook retention
  • Verified --dry-run output and --with-hooks behavior when config.toml is already wired

Summary by CodeRabbit

  • New Features

    • The Codex adapter’s --with-hooks option now installs or refreshes the global hooks workaround, including when MCP configuration already exists.
    • Dry-run hook installation failures now show a warning with the reason.
  • Bug Fixes

    • Hook setup preserves existing user hooks and supports safe, repeatable reinstallation.
  • Documentation

    • Updated Codex guidance and REST API reference to reflect current behavior and 119 registered endpoints.

`connect codex --with-hooks` no-oped for users with an existing
mcp_servers.agentmemory block. Mirror Claude Code/Droid so re-runs
still merge ~/.codex/hooks.json, and cover the path with adapter tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

@berthojoris is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9e2f980a-ed67-43a8-9228-5a6e4a68f8d8

📥 Commits

Reviewing files that changed from the base of the PR and between cb52b65 and a388455.

📒 Files selected for processing (3)
  • plugin/skills/agentmemory-rest-api/REFERENCE.md
  • src/cli/connect/codex.ts
  • test/codex-connect-hooks.test.ts
💤 Files with no reviewable changes (1)
  • src/cli/connect/codex.ts

📝 Walkthrough

Walkthrough

The Codex adapter now installs or refreshes global hooks when --with-hooks is used, even when MCP wiring already exists. Tests cover lifecycle events, dry-run behavior, repeated installation, preservation of user hooks, and updated reference metadata.

Changes

Codex hook lifecycle

Layer / File(s) Summary
Adapter hook installation flow
src/cli/connect/codex.ts, plugin/skills/agentmemory-agents/REFERENCE.md
--with-hooks installs or refreshes ~/.codex/hooks.json. Existing MCP wiring no longer prevents hook handling. Dry-run skips include their reason.
Hook installation integration coverage
test/codex-connect-hooks.test.ts
Tests verify lifecycle events, no-hook behavior, existing MCP configurations, idempotent reinstallations, and preservation of user hooks.

Reference metadata

Layer / File(s) Summary
REST endpoint reference count
plugin/skills/agentmemory-rest-api/REFERENCE.md
The documented registered REST endpoint count changes from 118 to 119.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: rohitg00

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: refreshing Codex hooks when MCP wiring already exists.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/cli/connect/codex.ts (1)

82-83: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove the behavior comments.

These comments explain behavior that the opts.withHooks branch already shows. Keep the condition and remove the comments.

Proposed change
-      // --with-hooks is independent of MCP wiring (issue `#508` / openai/codex#16430).
-      // Re-run refreshes absolute script paths even when MCP is already in place.
       if (opts.withHooks) {

As per coding guidelines, TypeScript source code must avoid comments explaining WHAT.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/cli/connect/codex.ts` around lines 82 - 83, Remove the two
behavior-explaining comments immediately above the opts.withHooks branch in the
Codex connection flow, leaving the condition and implementation unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@plugin/skills/agentmemory-agents/REFERENCE.md`:
- Line 13: Update the Codex CLI source text corresponding to the documentation
row so it matches the expected behavior in src/cli/connect/codex.ts, then run
npm run skills:gen to regenerate the documentation block and include the
generated output.

In `@test/codex-connect-hooks.test.ts`:
- Around line 172-187: Update the afterEach cleanup around HOME to mirror the
existing USERPROFILE restoration: delete process.env["HOME"] when ORIG was
initially undefined; otherwise restore ORIG. Keep the current USERPROFILE
handling and temporary home removal unchanged.

---

Nitpick comments:
In `@src/cli/connect/codex.ts`:
- Around line 82-83: Remove the two behavior-explaining comments immediately
above the opts.withHooks branch in the Codex connection flow, leaving the
condition and implementation unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 22b0e84d-55b7-40a0-8ae2-495e55065fdb

📥 Commits

Reviewing files that changed from the base of the PR and between 5023cf3 and cb52b65.

📒 Files selected for processing (3)
  • plugin/skills/agentmemory-agents/REFERENCE.md
  • src/cli/connect/codex.ts
  • test/codex-connect-hooks.test.ts

| Claude Code | `claude-code` | Using MCP. Hooks are also available, see https://github.com/rohitg00/agentmemory#claude-code-one-block-paste-it. |
| Cline | `cline` | Using MCP via ~/.cline/mcp.json (CLI). VS Code users: add the same block via Cline Settings → MCP Servers → Edit JSON. |
| Codex CLI | `codex` | Using MCP. Hooks ship via the Codex plugin; on Codex Desktop, also pass --with-hooks to install the global hooks.json workaround for openai/codex#16430. |
| Codex CLI | `codex` | Using MCP. Hooks ship via the Codex plugin; on Codex Desktop, also pass --with-hooks to install (or refresh) the global ~/.codex/hooks.json workaround for openai/codex#16430, works even when MCP is already wired. |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Regenerate this documentation block.

Line 3 requires generated output. This row differs from src/cli/connect/codex.ts Line 69. Update the source text, run npm run skills:gen, and commit its output.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@plugin/skills/agentmemory-agents/REFERENCE.md` at line 13, Update the Codex
CLI source text corresponding to the documentation row so it matches the
expected behavior in src/cli/connect/codex.ts, then run npm run skills:gen to
regenerate the documentation block and include the generated output.

Comment thread test/codex-connect-hooks.test.ts
@berthojoris

berthojoris commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Addressed review comments in commit a388455:

  • Updated HOME environment variable restoration logic in test/codex-connect-hooks.test.ts.
  • Removed redundant behavior comments in src/cli/connect/codex.ts.
  • Regenerated skill reference documentation (npm run skills:gen).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant