# pi-codex

A [pi](https://pi.dev) package maintained by [bermudi](https://github.com/bermudi), forked from [Manaflow](https://github.com/manaflow-ai)
that gives Codex models OpenAI Codex's native `apply_patch` and web search tools.

When the selected model uses the `openai-codex` provider, its model ID contains `codex`, or it is a `deepseek-v4` model (trained on the Codex harness), the extension replaces active `edit` and `write` tools with `apply_patch`. Switching to any other model restores the tools it removed.

## Install

Install [pi](https://pi.dev), then install this package directly from GitHub:

```bash
npm install -g --ignore-scripts @earendil-works/pi-coding-agent
pi install git:github.com/bermudi/manaflow-pi-codex
pi
```

Use `/login` to authenticate with ChatGPT, then select an `openai-codex` model
with `/model`. To pin this release instead of following the default
branch:

```bash
pi install git:github.com/bermudi/manaflow-pi-codex@v0.1.3
```

> The unscoped `pi-codex` package on npm is an unrelated project. Install this
> package from GitHub as shown above.

Pi packages execute code with your user permissions. Review the source before
installing it.

## Why the Codex dependency?

The model-facing tool does not require a provider workaround. Pi supports OpenAI custom/freeform tools with Lark grammars, and pi's Codex model catalog enables that capability.

Applying a patch correctly is separate. This package pins `@openai/codex` and invokes its native binary's internal `apply_patch` entrypoint, so parsing, fuzzy context matching, file moves, errors, and output match the upstream implementation instead of a partial TypeScript rewrite.

## Launcher

The package also includes a `pi-codex` launcher that loads the extension for
one run and defaults pi to the `openai-codex` provider. Install it from a
checkout:

```bash
git clone https://github.com/bermudi/manaflow-pi-codex.git
cd pi-codex
npm install
npm link
pi-codex
```

The launcher loads this package and defaults pi to the `openai-codex` provider. Normal pi arguments work:

```bash
pi-codex --model gpt-5.6-sol:high
pi-codex --model openai/gpt-5.3-codex
```

To install only the extension into pi without the launcher:

```bash
pi install git:github.com/bermudi/manaflow-pi-codex
```

The native Codex package is large because it includes the platform binary.

## Behavior

- Uses the upstream freeform patch format and Lark grammar.
- Keeps each registered tool's TypeBox schema, exposure, execution mode,
  capabilities, and output budget in one Codex tool contract. Remote
  compaction sends the same direct model-visible schema projection as the
  active Pi tools, while preserving deferred-loading metadata for discovery.
- Supports add, update, move, and delete hunks.
- Executes patches sequentially relative to other tool calls.
- Respects tool selection: if `apply_patch` was excluded initially, `edit` and `write` are not replaced.
- Falls back to pi's normal `edit` and `write` tools after switching away from a Codex model.
- Enables Codex's standalone live `web_search` tool for the `openai-codex` provider. Rendering adapts: standalone (or with goodies' clean-tui disabled) it renders like pi's native tool rows with the search output visible; with bermudis-pi-goodies' clean-tui enabled it renders as a clean burst row (same-tool calls share one block; output hidden until the row is expanded).
- Sends standalone search to the resolved Codex `/alpha/search` endpoint, preserving provider base URL and header overrides.
- Uses current Codex remote compaction V2 for the `openai-codex` provider.
- Applies Codex's model-facing middle-output truncation policy to package
  tools and the `tool_result` boundary, while retaining raw search output and
  patch details for rendering and inspection. Structured non-text result
  content remains attached to the truncated model-facing result.
- Applies Codex Fast mode (`service_tier: "priority"`) when enabled for models that advertise the Fast tier, covering both normal turns and remote-compaction turns. Fast mode defaults to off (see `/fast` below).
- Renders `apply_patch` like pi's native edit row (colored diffs visible) by default; with bermudis-pi-goodies' clean-tui enabled it renders as a clean burst row — collapsed header shows the touched paths, `Ctrl+O` expands to pi-native colored diffs generated from the actual before/after files.
- Collapses the normal compaction notice to one line; `Ctrl+O` still expands its summary.
- Keeps Pi's normal working spinner and adds a once-per-second elapsed-time
  label (`Working (12s)`, `Working (1m 5s)`) to the working row.

## Remote compaction

Current OpenAI Codex enables `RemoteCompactionV2` by default. `pi-codex` mirrors that flow:

1. Sends a streaming request to the resolved Codex provider base URL plus `/codex/responses` (normally `https://chatgpt.com/backend-api/codex/responses`), the same route as normal turns. Auth-provided base URL overrides are honored.
2. Appends `{ "type": "compaction_trigger" }` to the Responses input.
3. Requires exactly one `{ "type": "compaction" }` output item.
4. Persists the opaque replacement history in pi's compaction entry and reinstalls it verbatim in later Codex requests, including after resume.
5. Stores the response ID, token usage, tool-catalog fingerprint, retained
   suffix fingerprint, and checkpoint version with the replacement history. A
   v2 checkpoint is replayed only when its marker, active tool catalog, and
   retained suffix still match; legacy v1 entries remain readable.
6. Replays `x-codex-turn-state` only within the active Pi turn, including an
   immediate overflow retry.

For every model under the `openai-codex` provider, `pi-codex` matches Codex's default automatic-compaction boundary at 90% of that model's context window. For example, the live 272,000-token models compact beginning at 244,800 tokens, while the 128,000-token Spark model begins at 115,200. Internally the pi reserve includes one extra token because pi's comparison is `>` while Codex's is `>=`. The override is in-memory, applies only while an `openai-codex` model is selected, preserves an explicit `compaction.enabled: false`, and does not rewrite `settings.json` or affect other providers.

The older `/codex/responses/compact` endpoint remains in Codex for the legacy implementation, but it is not the default in the inspected upstream revision. There is no additional compaction-specific subrouter path: both normal and compaction V2 traffic use the resolved base URL's `/backend-api/codex/responses` route.

Remote compaction is intentionally limited to the `openai-codex` provider. Other providers retain pi's normal local summary compaction.

The package targets Pi 0.84 APIs. It does not attempt to reproduce Codex's
subagent tool surface yet. First-class namespace routing, model-owned tool
search dispatch, and provider response continuation remain Pi core concerns.
Deferred contracts therefore stay out of the initial model-visible tool list,
matching Codex's `model_visible_specs()` projection, while their schemas remain
available to the tool-search lifecycle and checkpoint fingerprint.

## Fast mode

Fast mode defaults to off for supported `openai-codex` models. Use:

```text
/fast          # toggle
/fast on
/fast off
/fast status
```

The selection is stored in the session, survives `/reload` and resume, and resets to off for a new session. Unsupported models and non-Codex providers ignore the tier selection.

## Development

```bash
npm install
npm run check
npm test
```

The upstream grammar is copied from `codex-rs/core/src/tools/handlers/apply_patch.lark` in `openai/codex`. See `THIRD_PARTY_NOTICES.md`.
