# Codex plugin (native mode)

`plugins/codex/` makes notesoup a native Codex plugin: five notebooks and two history tools over a bundled stdio MCP server, plus SessionStart/SubagentStart hooks that deliver the notes boot into the model's initial context. It does not wrap Codex's conversation or replace its context engine. Reset and budget stay native.

Validated against the installed **Codex 0.171.0**, including real model turns. The rollout reader checks supported wire shapes, not a version whitelist; an unknown producer version isn't automatically rejected. Unsupported or ambiguous data still fails explicitly. Compressed `.jsonl.zst` rollouts require **Node >=22.15** (`zstdDecompressSync`).

## Install (from this checkout)

```sh
npm ci
npm run build
codex plugin marketplace add /absolute/path/to/notesoup
codex plugin add notesoup@notesoup
```

Codex copies the native `.codex-plugin/plugin.json`, `.mcp.json`, hooks and built helper into its cache; build before installing or refreshing the plugin. Review and trust the three notesoup hooks in Codex's `/hooks` UI. Review and approve the notesoup MCP tools according to your own permission settings. An untrusted hook does not run, and notesoup then refuses MCP calls as `unknown_thread`. Restart Codex or resume the thread after changing configuration.

Merge these settings into `~/.codex/config.toml` rather than replacing it (or use `CODEX_HOME` to select a different Codex home):

```toml
[features]
context_management = false
retain_client_developer_messages = false

[features.token_budget]
enabled = true
use_history_notes_extension = false
```

Do not also write `token_budget = true`: it is a table here. `context_management = false` disables the competing experiment. `token_budget` supplies native `new_context` and `get_context_remaining`, while `use_history_notes_extension = false` leaves persistent notes/history to notesoup. These flags are under development and may change in later releases.

## What the agent gets

- **A boot at native window start.** `SessionStart` reads the native rollout, binds the root thread and returns a notes snapshot as hook additional context. `SubagentStart` does the same for each separately spawned child. Dedupe requires a host-persisted `hooks.additional_context` record for the exact thread and native window, never a quoted marker.
- **Codex-native reset and budget tools.** `new_context` and `get_context_remaining` remain Codex's tools; notesoup does not implement a fallback.
- **Five notebooks** through `notes_write`, `notes_update`, `notes_read` and `notes_list` (add `query` to search). `NOTESOUP_HOME` overrides the notes root (default `~/.agents/notes`); a root thread's agent name is `NOTESOUP_AGENT` or `anonymous`. A child uses its slugged hook `agent_type` (for example `worker` or `default`).
- **Native history** through `history_list` (browse a timeline or list sessions), `history_search` (find passages by query), and `history_read` (open one event by seq). Optional `session` targets another local session; `history_list` and `history_search` also accept wildcards such as `"*"`; see [cross-session history](history-sessions.md). The boot's window tree retains native window UUIDs.
- **Per-turn model identity.** `@model` follows the current turn's `x-codex-turn-metadata.model`, not the model recorded at thread start.

Codex may namespace MCP tools or expose them through its tool orchestrator; use the names in the current tool list.

## Native-window and identity rules

Hook-captured facts bind the thread and its cwd, root session, transcript, notes root, native window UUID and number. The MCP server never guesses a project from the server's working directory. Native Codex hooks receive `PLUGIN_DATA`; the native stdio launcher does not, so the fixed `notesoup@notesoup` installation reads the same binding directory at `$CODEX_HOME/plugins/data/notesoup-notesoup` (default `~/.codex`).

Codex 0.171.0 MCP `windowId` uses `<thread-id>:<window-number>`, while the rollout records a native window UUID. The adapter accepts the numbered ID only when it exactly matches the bound thread and the transcript-proven window number; the persisted UUID remains the history window identity. The older UUID form also works when it matches the same binding. All calls recheck native facts before accessing notes or history.

Calls refuse for a missing binding (`unknown_thread`), conflicting caller or transcript thread/root/window (`stale_thread_binding` / `stale_native_state`), missing native window (`missing_window`), or missing current turn model (`missing_model`). Resume or compact once to rebind root identity; a root-session disagreement is never overwritten by a hook.

The transcript decoder is strict about structural facts: unknown record/response types, malformed committed JSONL, non-increasing ordinals, copied `session_meta` ancestry and unsupported `thread_rolled_back` selection are refusals. An incomplete final line is ignored as an append tail. Native `additional_tools` catalog records consume their ordinal but aren't conversation events.

## Child rollover limitation

The root recovery path is `SessionStart` after compaction. Codex 0.171.0's child startup hook contract does not provide a supported context-injecting post-compact child recovery hook. Notesoup therefore answers a child `PreCompact` with `continue: false` before native history is cut, and tells the child to save a handoff note and start a fresh child; root `PreCompact` is transparent. Independent child first-turn boot and MCP calls are verified, but child rollover recovery is not implemented.

## Limits

| Limit | What to expect |
| --- | --- |
| Codex wire format | Real 0.171.0 root and child rollouts, plus older producer-shaped fixtures; unknown shapes may refuse regardless of version. |
| Node | >=22.15 for compressed rollouts. |
| Boot size | Local cap 128 KiB; hook `additionalContextLimit: 0` avoids host spilling. Oversized boot warns instead of injecting truncated notes. |
| Tool output | Shared result bounds; use continuation fields or narrower filters. |
| History sources | Local Pi/Claude/Codex transcripts through the shared corpus. No remote history or text recovered from opaque/encrypted payloads. |
| Reset/budget | Codex-native only. No notesoup budget warning or close-out turn. |
| Child rollover | Unsupported; blocked at `PreCompact` as above. |
| Dream | No Codex dream backend or dream skill registration. |
| Notifications/commands | No custom UI notification, runtime toggle or `/fresh-window` command. |

## Validation

The targeted tests drive copied bundled stdio helpers via the MCP SDK and exercise root, child, and fork bindings, history coordinates and strict lineage validation, caller window identity, native hook boot and dedupe, and the child PreCompact guard.

Redacted live evidence (Codex 0.171.0, 2026-11-26; fresh installation of the unmodified native bundle into an isolated Codex home):

| Probe | Native rollout ordinals and result |
| --- | --- |
| Root | 12: `hooks.additional_context` contains the pinned-note sentinel; 16: first assistant commentary reproduces it, before any tool execution; 22/23: `notes_list` and `history_list` complete successfully. The model identifies both native context tools. |
| Independent child | Spawned with `fork_turns="none"`, without the sentinel in its task. Its own ordinal 12 contains hook boot; 28/29: `history_list` and `notes_list` complete successfully; 38: final assistant message reproduces the sentinel. Parent confirms the child's initial report; no shell or notes-read operation was used. |
| History decoder | Both completed root and child rollouts decode with native window number 0 and one window. |
| Default installation | CLI install succeeded and the installed app-server listed SessionStart, SubagentStart and PreCompact as enabled, untrusted. Existing user permissions were not changed. |

The isolated probes allowed only the locally vetted plugin hooks and the two read-only list operations in their temporary configuration. No private notes were used; temporary raw diagnostics were removed after validation. No live compaction completion is claimed.

```sh
npm run typecheck
npm test
node --test dist/test/codex-history.test.js dist/test/codex-plugin.test.js
```
