# Codex adapter

Shows the sessions of Codex in the notch and lets you answer its approval requests from Blossom.
Codex reports through hooks: each hook posts its event with `curl` to a Unix socket in your own folder, `~/Library/Application Support/Blossom/codex/socket`, where this adapter listens. Blossom launches the adapter.
The hooks are a `hooks.json` file that Codex reads from its folder, `~/.codex`, and that Codex runs only after you review and trust them.

**Not run against a real Codex.** The adapter was built from the source of Codex at `rust-v0.172.0` and from its documentation; how a hook runs and what an approval carries were read again at `rust-v0.173.0`. Its tests use payloads written from the schemas Codex publishes, not captured from a run. Everything under "Checks by hand" is still to be seen. How it was researched is in [../../documentation/roadmap/codex.md](../../documentation/roadmap/codex.md).

You need:

- Blossom. It includes this adapter and the Bun that runs it; from a clone, `bun run build` makes one.
- Codex, with hooks, which are on by default. Read against 0.172.0; hook payloads may change between releases.
- `curl`, which every hook runs as `/usr/bin/curl -q`, and `ps`, which the hook of a prompt runs as `/bin/ps`. macOS ships both.
- `jq`, only to install or remove the hooks by hand.

## Connect from Blossom

The quickest way. Blossom finds Codex on your Mac, and connecting it is one click: "Continue" on the second step of the introduction, with Codex checked, or its switch in Settings > Agents. Blossom then adds this adapter to its configuration, starts it, and puts the hooks of `adapters/codex/hooks.json` in `~/.codex/hooks.json`, after the hooks you already have. Nothing of yours changes, and a file that does not exist is created with the hooks alone.

Before it writes over the file, Blossom keeps a copy of it in `~/Library/Application Support/Blossom/backups/codex/`, named after the moment in UTC. It writes the new content to a file next to yours and moves it into place, so a failure halfway leaves your file as it was; a symbolic link stays a link. A file that is not plain JSON, such as one with comments, or that is read-only, Blossom leaves alone and says why in its row. Blossom writes `~/.codex/hooks.json` and does not follow `CODEX_HOME`; if you set it, install by hand. Codex then runs no hook until you trust it: open `/hooks` in Codex and trust the eleven hooks, as the row says; see "Trust the hooks" below. "Repair" in the row puts the hooks back when they are missing or not the ones this Blossom installs. "Disconnect", shown while the agent is off, takes the adapter out of Blossom and its hooks out of the file, and nothing else. What Blossom writes and why is in [the decision](../../documentation/decisions/connecting.md).

## Run Blossom with the adapter

From the root of the clone:

```sh
bun install && bun run bootstrap && bun run build
BLOSSOM_CONFIGURATION=adapters/codex/configuration.json apps/macos/build/Build/Products/Debug/Blossom.app/Contents/MacOS/Blossom
```

The notch shows nothing until a session of Codex reports. Run one Blossom at a time: the adapter holds the socket, and the adapter of a second Blossom logs `cannot listen for the hooks of codex` and exits.

To run it next to the Claude Code adapter, or from Finder, put both in one configuration file, each by its package, `{ "package": "codex" }`, as [`../claude/README.md`](../claude/README.md) shows for one.

## Install by hand

For a clone without the app, a Codex folder in `CODEX_HOME`, or a file Blossom leaves alone. The `hooks` of `adapters/codex/hooks.json` go into the `hooks.json` of your Codex folder, after the hooks you already have. The commands below write a new file, and you put its content in place. Codex keeps its folder in `CODEX_HOME`, or in `~/.codex` when that is not set; the first line names it for the commands that follow, from the root of the clone:

```sh
codex_folder="${CODEX_HOME:-$HOME/.codex}"
```

If `"$codex_folder/hooks.json"` does not exist, there is nothing to merge. Copy the file of the adapter and go on to "Trust the hooks". The copy never overwrites a file that is there:

```sh
mkdir -p "$codex_folder" && cp -n adapters/codex/hooks.json "$codex_folder/hooks.json"
```

Otherwise write the merged hooks to a new file. Each hook of the adapter is added after the hooks you have for the same event; nothing of yours changes.

```sh
jq -n --slurpfile user "$codex_folder/hooks.json" --slurpfile blossom adapters/codex/hooks.json '
  ($user[0] // {}) | .hooks = reduce ($blossom[0].hooks | to_entries[]) as $event
    (.hooks // {}; .[$event.key] += $event.value)
' > "$codex_folder/hooks.merged.json"
```

If `jq` printed an error, stop: your `hooks.json` is not valid JSON, Codex already skips it, and the new file is empty. Otherwise review it. The only differences are added hooks that name the socket:

```sh
diff <(jq -S . "$codex_folder/hooks.json") <(jq -S . "$codex_folder/hooks.merged.json")
```

Keep a copy and put the new content in place. A backup that already exists is kept, so the first one survives. The second line does nothing when the new file is empty or not valid JSON, and it replaces the content of your file, not the file: a symbolic link and the mode of the file stay as they are.

```sh
cp -n "$codex_folder/hooks.json" "$codex_folder/hooks.backup.json"
jq -e . "$codex_folder/hooks.merged.json" > /dev/null && cp "$codex_folder/hooks.merged.json" "$codex_folder/hooks.json" && rm "$codex_folder/hooks.merged.json"
```

Install once. Running the commands again adds every hook a second time; remove first.

### Trust the hooks

This holds also after connecting from Blossom. Codex runs no hook you have not reviewed. Start `codex`: it warns that hooks need review. Open `/hooks`, look at the eleven hooks whose command names `Blossom/codex/socket`, and trust them. In the desktop app the same list is under Settings > Hooks. Codex remembers the trust in your `config.toml`, against the exact text of each hook, so a hook that changes asks again.

A session started after that reports to Blossom, from its first prompt on.

## Remove by hand

After an install by hand; after connecting from Blossom, Disconnect does it, and removing Blossom takes out the hooks of every tool, see [Removing it](../../documentation/privacy.md#removing-it). Write your hooks without those of the adapter to a new file. The command drops the hooks whose command names the socket, and whatever they leave empty. Every other hook stays as it is.

```sh
jq '
  def blossom: (.command // "") | contains("Application Support/Blossom/codex/socket");
  def only(entries): length > 0 and all(.[]; entries);
  del(.hooks | select(only(only(.hooks | only(blossom)))))
  | del(.hooks[]? | select(only(.hooks | only(blossom))))
  | del(.hooks[]?[]? | select(.hooks | only(blossom)))
  | del(.hooks[]?[]?.hooks[]? | select(blossom))
' "$codex_folder/hooks.json" > "$codex_folder/hooks.cleaned.json"
```

Review it and put the new content in place, as when installing:

```sh
diff <(jq -S . "$codex_folder/hooks.json") <(jq -S . "$codex_folder/hooks.cleaned.json")
cp -n "$codex_folder/hooks.json" "$codex_folder/hooks.backup.json"
jq -e . "$codex_folder/hooks.cleaned.json" > /dev/null && cp "$codex_folder/hooks.cleaned.json" "$codex_folder/hooks.json" && rm "$codex_folder/hooks.cleaned.json"
```

Check that nothing is left. This prints `0`:

```sh
grep -c 'Blossom/codex/socket' "$codex_folder/hooks.json"
```

If the file the copy made holds only the hooks of the adapter, what is left is `{}`, which Codex reads as no hooks. The trust Codex recorded stays in `config.toml`, under `[hooks.state]`, and does nothing without the hooks; `/hooks` no longer lists them.

## What to know

- **The name comes from the first prompt and the repository.** The first line of the first prompt is the title, cut to 60 characters, and stays; a prompt that starts with `/` or `!` titles nothing. The project is the repository and the branch of its folder, read from `HEAD` and the pointers of `.git` at most once every five seconds, without running `git`, and the branch rides beside the title; the rest of the prompt is dropped as it arrives. See [../../documentation/decisions/names.md](../../documentation/decisions/names.md).
- **Blossom answers first.** Codex runs the hook of an approval before it shows its own, so while Blossom shows a request the terminal shows the hook's status, "Waiting for an answer in Blossom", and no approval. Answer in Blossom, or interrupt the turn in Codex. After sixty seconds the hook gives up, Codex asks in the terminal as usual, and the session waits for you in the notch, without a request to answer there. When Codex's automatic reviewer decides instead of you, the session shows that it waits until Codex goes on.
- **Allow and Reject only.** Codex reads no rule from a hook yet, so there is no Always. Reject tells Codex "Rejected in Blossom."
- **What the request shows.** A shell command shows the command, not the reason the model gave for it. A patch shows the whole patch, with its line breaks as `\n`. Any other tool shows its name and its whole input. A subagent's request starts with its type. Control characters, line breaks and reordering characters are shown as their escapes. A request for network access shows the host Codex names, then the command that triggered it: `network-access registry.npmjs.org: npm install`. The model writes the reason of an ordinary command, and could start it with the same words; the command is shown either way.
- **What the request would do.** For a shell command the request also carries the command, and for a patch that only adds or changes files, the text before and after each change, with the name of its file, for the panel to show once it draws it, unless "Show what a request would do" is off. A patch that deletes or moves a file, or that does not read as the patch format of Codex, carries nothing more than its title. MCP tools, `write_stdin` and `request_permissions` carry their title alone. The adapter keeps none of it after the request.
- **A policy can still ask.** A checkpoint your organization says needs a person is asked in Codex even after Allow in Blossom.
- **Without Blossom, Codex works as before.** With Blossom closed nothing listens on the socket. Each hook gives up at once, prints nothing and ends without an error, and Codex asks for every approval itself. That holds too when a Blossom that was killed left its socket behind.
- **An approval is never allowed by default.** The hook of an approval prints what the adapter answered and nothing else. With Blossom closed, the adapter gone, no answer within the minute or any other failure, it prints nothing, which Codex reads as no decision. Every command names `/usr/bin/curl` and `/usr/bin/true` in full, ignores your curl configuration with `-q`, and ends with the status 0: Codex reads the status 2 as a denial.
- **Your shell's startup file comes first.** Codex runs every hook with your shell and `-c`: `zsh` reads `/etc/zshenv` and the `.zshenv` of `$ZDOTDIR`, `~/.zshenv` by default, and `bash` reads the file in `$BASH_ENV`. When Codex knows no shell for the session it uses `$SHELL -lc`, and a login shell also reads `.zprofile` and `.zlogin`, or `.bash_profile`. The environment Codex was started in reaches the hooks too. Whatever that file prints is part of what the hook prints. A file that prints text breaks the hooks: Codex cannot read Blossom's answer and asks in the terminal, and it reports the hook of the end of a turn as failed. Keep those files quiet. A file that printed a decision of its own could allow; such a file runs in every shell you open, so whoever can write it can already run anything as you.
- **A failed turn shows after a minute.** Codex runs no hook when a turn ends on an error, and it retries a request that failed, waiting up to sixty seconds between tries. So a session fails in the notch when a request of the session failed and nothing else happened for sixty-five seconds. A connection that stays down that long shows as a failure, and the session works again when Codex retries.
- **An interrupted turn is done.** It is your turn.
- **A question stays in Codex.** When Codex asks you something with its own question tool, the session waits for you in the notch, and you answer in Codex.
- **Runs of `codex exec` show.** Their hooks look like those of a session in the terminal, so the adapter cannot leave them out. They close when the run ends.
- **A session whose Codex was killed leaves within seconds.** Every hook names the parent of its shell, the Codex process, in the header `Blossom-Process`. Every five seconds the adapter asks the system whether the process each session last named still exists, and closes the session when it is gone. Under the desktop app or the IDE extension that process is their app server, which outlives the session; those sessions close when Codex says the session ended. A process identifier the system gave to another process keeps a dead session until that one ends too.
- **A session shows up when it starts working.** An open session with nothing typed shows nothing.
- **The socket is yours alone.** It lives in a folder only your user can open, as for Claude Code; see [../../documentation/decisions/socket.md](../../documentation/decisions/socket.md). A process that runs as you can still post events or listen in Blossom's place while it is closed, and could then answer an approval. Such a process can also rewrite `hooks.json`.
- **The hooks find the socket through `$HOME`.** A Codex started with another `HOME` does not report.
- **The hook of a prompt finds the terminal of its session.** Before it posts, it walks up its processes with `/bin/ps`, from Codex upwards, sixteen at most, for the terminal device and the processes above it, and reads the variables terminal programs and multiplexers set, such as `TERM_PROGRAM`, `__CFBundleIdentifier`, `TMUX_PANE` and `KITTY_WINDOW_ID`, from the environment Codex started with. Codex runs the hook in a session of its own, without a terminal, so the device comes from Codex and the processes above it. It sends each value in a header only when it is short and of plain characters, and Blossom keeps it in memory, shows it nowhere, and uses it to bring that terminal forward when you ask. It adds about 50 ms to a prompt, and to no other hook. Under the desktop app or the IDE extension no terminal is found, and the application is the one that started Codex. Hooks connected before this version need Repair in Settings > Agents, and then a new review in `/hooks`, since their command changed.

## What a session changed

When a turn ends, Blossom asks the adapter what the repository of the session holds uncommitted, and the adapter runs `git` in the folder the hooks named: the names of the files without their folder, the lines added and removed, whether each is new or deleted, and the commits made since the turn began, never the content of a file. The reply panel, detail and the row of the inbox show it; "Show what a session changed" in Settings > General turns it off, and then `git` does not run. `git` is taken only from `/opt/homebrew/bin`, `/usr/local/bin`, the Command Line Tools or Xcode, never from your `PATH`, and no program a repository names runs; see [decisions/git.md](../../documentation/decisions/git.md) and [decisions/changes.md](../../documentation/decisions/changes.md). Tried with payloads written by hand and a real `git` on repositories made by the tests, not with a real Codex.

## Plan limits

The adapter offers the limits of the plan. When Blossom asks, it reads the end of the transcript the last hook named, at most its last MiB, and answers the last `token_count` of the bucket `codex`: the share, the length and the reset of its primary and secondary windows. It reaches no network and starts nothing. Codex writes those values from the headers of its last model response, so they are as old as that response and miss usage on another device. A key of the API writes none: Blossom then shows nothing. Tested against transcripts written by hand with the shape of Codex `rust-v0.172.0`, never against a real one; see [the study](../../documentation/roadmap/limits.md).

## Checks by hand

Not run against a real Codex. Each check needs a Codex that works and is signed in, Blossom running with this adapter, and the hooks installed and trusted. Work in a temporary folder:

```sh
cd "$(mktemp -d)" && git init -q && codex --ask-for-approval on-request
```

Write down what the line in italics asks.

1. **The tile.** Type `hi`. *Does the Codex tile appear with the prompt, and turn green when the answer ends?*
2. **Who shows the approval.** Ask `run the shell command touch ~/blossom-probe.txt`: a file outside the folder needs approval in the default sandbox. *Does the request panel open, and does the terminal show "Waiting for an answer in Blossom" and no approval of its own?*
3. **Allow.** Answer Allow in Blossom. *Does `~/blossom-probe.txt` exist, with no approval asked in the terminal?* Delete it.
4. **Reject.** Ask again with `~/blossom-rejected.txt` and answer Reject. *Is there no such file, and does Codex say it was rejected in Blossom?*
5. **Answering nowhere.** Ask again with `~/blossom-late.txt` and wait more than a minute. *Does the request leave the panel after a minute, does the terminal then ask, and does the badge stay amber until you answer there?*
6. **Interrupting.** Ask again with `~/blossom-interrupted.txt` and press `esc` in Codex while the panel shows it. *Does the request leave the panel at once, and does the tile turn green?*
7. **A failed turn.** Start `codex -m nonexistent-model` in the same folder and type `hi`. *Does the tile turn red about a minute after the error, and not before?*
8. **Closing.** Quit Codex with `/quit`. *Does the tile leave at once?*
9. **A Codex that is killed.** Start Codex again, type `hi`, and from another terminal run `kill -9` on its process, which `ps -ax -o pid,command | grep '[c]odex'` lists. *Does the tile leave within about five seconds?* Start another, type `hi` and leave it idle for a minute. *Does its tile stay?*
10. **A new conversation.** Type `hi`, then `/new`, then `hi` again. *Does the first session leave, or stay green until dismissed?*
11. **A question.** In plan mode, ask for something that makes Codex ask you a question. *Does the tile turn amber while the question waits?*
12. **Quitting Blossom.** Quit Blossom and ask for an approval again. *Does Codex ask in the terminal at once, with no hook error?*
13. **The desktop app and the IDE extension.** Open the temporary folder in each, trust the hooks in Settings > Hooks if it asks, and send `hi`. *Does the session show up, and does it stay after a minute idle?*
14. **What an approval would do.** Ask `run the shell command touch ~/blossom-content.txt`, then, in a folder where writing asks, `create notes.md with one line`. *Does the request panel open for each while the terminal waits, and, once the panel draws what a request would do, does it show the command, and then the new file with its one line?*
15. **The terminal of a session.** Send a prompt from Terminal, one from iTerm and one inside tmux. *Does each prompt start as quickly as before, and does `/hooks` show no failed hook?*
