Back to Openviking

Codex Memory Plugin

docs/en/agent-integrations/04-codex.md

0.4.17.3 KB
Original Source

Codex Memory Plugin

Equip Codex with persistent memory across sessions. Install it once, and memories will be automatically recalled with every prompt, captured after each turn, and committed before compaction. The plugin also connects Codex to OpenViking's /mcp endpoint, enabling the model to search, store, and manage memories directly.

Source: examples/codex-memory-plugin | Blog: Motivation & demo

Install

bash
bash <(curl -fsSL https://raw.githubusercontent.com/volcengine/OpenViking/main/examples/codex-memory-plugin/setup-helper/install.sh)

The installer checks dependencies, configures the OpenViking connection, and registers the plugin. Every step is idempotent.

In regions where GitHub is hard to reach, use the equivalent command below:

bash
bash <(curl -fsSL https://ovrelease.tos-cn-beijing.volces.com/codex-memory-plugin/tos-install.sh)

After installation, activate the codex wrapper in your current terminal (or open a new terminal window):

bash
source ~/.openviking/openviking-repo/examples/codex-memory-plugin/setup-helper/wrapper.sh
codex              # First run: approve hooks once when prompted via /hooks

Launch Codex through a custom command? A wrapper script like codex-custom, or a multi-word launcher (a base command plus a sub-command) — list it at the installer's "Extra launch commands" step (or pass OPENVIKING_CODEX_WRAP_EXTRA='codex-custom') to inject credentials there too.

<details> <summary><b>Manual setup</b></summary>

Prerequisites: Node.js >= 22, Codex >= 0.130.0, and the codex_hooks feature enabled.

  1. Shell function wrapper — Source the plugin wrapper from your shell profile (e.g., .bashrc or .zshrc) so Codex receives the active ovcli.conf credentials, MCP is re-rendered, and stale inherited credential env vars are stripped. See the plugin README for the snippet.

  2. Plugin installation — Register a local marketplace and enable the plugin. See setup-helper/install.sh for the exact commands.

  3. Placeholder rendering — The provided .mcp.json and hooks.json files contain placeholders that must be substituted when copied to Codex's plugin cache. The automated installer handles this for you.

</details>

Verify

bash
type codex         # Expectation: codex is a shell function

If the previous command printed a path instead of shell function, the wrapper isn't active yet. Re-source it (or open a new terminal) before launching, otherwise Codex may start without the credentials needed by MCP.

Once inside Codex, the plugin should seamlessly recall memories on every prompt. Set OPENVIKING_DEBUG=1 to write events to ~/.openviking/logs/codex-hooks.log.

How it works

The plugin integrates with Codex's lifecycle by hooking into key events. It searches OpenViking and injects relevant memories before every prompt (UserPromptSubmit), appends new turns to the session after each response (Stop), and commits the full transcript before compaction (PreCompact) to ensure memory extraction processes the entire conversation. Upon starting a fresh session, it also cleans up any orphaned sessions from previous runs.

Known limitation: Codex does not fire a hook upon SIGTERM, Ctrl+C, or /exit. Orphaned sessions are recovered during the next SessionStart via the idle-TTL sweep (30 minutes) or the active-window heuristic.

<details> <summary><b>Configuration</b></summary>

Credential source: active ovcli.conf wins by default (OPENVIKING_CLI_CONFIG_FILE or ~/.openviking/ovcli.conf), so ov config switch <name> changes hooks, MCP, and child ov commands together on the next launch. Set OPENVIKING_CREDENTIAL_SOURCE=env only when you intentionally want env vars to override the CLI config; the wrapper then writes a mode-0600 runtime ovcli config under ~/.openviking/codex-plugin-state/ so child ov commands still match. Without an ovcli config, env vars then ov.conf then built-in defaults are used.

Env VarDefaultDescription
OPENVIKING_URL / OPENVIKING_BASE_URLFull server URL
OPENVIKING_API_KEYAPI key (sent as Authorization: Bearer)
OPENVIKING_CLI_CONFIG_FILE~/.openviking/ovcli.confActive CLI config to use for hooks, MCP, and child ov commands
OPENVIKING_CREDENTIAL_SOURCEautoSet env to force env-var credentials instead of active ovcli config
OPENVIKING_CODEX_ACTIVE_WINDOW_MS120000SessionStart active-window threshold
OPENVIKING_CODEX_IDLE_TTL_MS1800000SessionStart idle-TTL sweep threshold
OPENVIKING_DEBUGfalseWrite logs to ~/.openviking/logs/codex-hooks.log

Additional tuning options (e.g., OPENVIKING_RECALL_LIMIT, OPENVIKING_CAPTURE_ASSISTANT_TURNS) are documented in the plugin README.

</details>

Troubleshooting

SymptomCauseFix
MCP server is not logged inCredentials were not injected at Codex launch, or the active ovcli config has no api_key for an authenticated serverEnsure the codex() shell function is sourced and ovcli.conf contains a valid api_key.
type codex shows a path instead of a shell function (wrapper inactive)The rc wasn't sourced after install, or you launched from a terminal that didn't load itRun source ~/.zshrc (or ~/.bashrc on bash), or open a new terminal
Launching via an alias (e.g. cx) injects no credentialsThe alias name was listed in OPENVIKING_CODEX_WRAP_EXTRA (alias names are skipped), or the alias's target command isn't wrappedWrap the command the alias points to, not the alias: alias cx=codex needs nothing; for alias cx=codex-custom, add codex-custom
4 hooks need reviewSecurity review on first launchRun /hooks within Codex and approve the hooks.
hook (failed) exited with code 1Stale placeholders in the plugin cacheRe-run the one-line installer.
Recall returns nothingServer is unreachable or the URL is incorrectCheck the endpoint: curl "$(jq -r '.url' ~/.openviking/ovcli.conf)/health"
Hook 401 but MCP works (or vice versa)Cached MCP config or launch env is staleRestart Codex through the wrapper. If you intentionally use env credentials, set OPENVIKING_CREDENTIAL_SOURCE=env; otherwise use ov config switch <name> or OPENVIKING_CLI_CONFIG_FILE.

See also