This is a Pi extension which adds Codex-style remote compaction for OpenAI models, giving you better continuity across compaction boundaries while preserving all of Pi's normal features.
What does that mean? Why would you want it? My impression has been that Codex compacts better than Claude Code and better than Pi. And I supposed this was because Codex compacts by using OpenAI's server-side Responses compaction protocol. That protocol sends a compaction_trigger through POST /v1/responses and receives an encrypted compaction item. This extension configures Pi to use that protocol for OpenAI models alongside Pi's native compaction logic.
But is Codex's compaction actually better? Since the OpenAI compaction endpoint compacts to encrypted binary blobs, no one can say what it is doing under the hood. However, we don't need to know how it works to determine if it works better. Anyone can call the endpoint. And since codex is an open source, we can mimic exactly how codex itself uses the endpoint. That is what this extension configures Pi to do.
So is native compaction better? For the user-facing comparison I care about, the evidence says yes, with important price and reliability qualifiers. A held-out benchmark of the real product defaults found 78.0% exact recall for this extension's native policy versus 48.0% for Pi's default compactor; full context scored 100%. Native did this while emitting 4.58x as many compaction output tokens and leaving a 29% larger billed downstream context. It preserved much more old state, but this is not evidence that it is better at the same token budget. Native was also highly variable: every large artifact scored perfectly, while three small artifacts performed about as poorly as Pi.
Strictly, this directly compares Pi with this extension's reconstruction of Codex-style compaction, not with an end-to-end run of the Codex CLI. The result also does not show that the endpoint reliably detects when more capacity is needed: its short artifacts were the failures. What it does show is that the native default sometimes allocates far more context, and those large-allocation runs drove its aggregate advantage.
An earlier benchmark reported 100% native recall versus 82.8% and 76.7% for two text summaries at apparently matched downstream sizes. That procedure first observed native's output usage and then imposed it as the text arm's maximum, which is asymmetric and can favor native. Its same-budget interpretation is therefore superseded. See the new product-defaults report and reproduction instructions. The older matched-cap report remains retained with a methodological correction.
None of this proves the encrypted blobs use a clever latent-space representation. They might be encrypted optimized text or structured state values. (A little reverse engineering suggests the blobs are produced through a textual prompt, for what it is worth: https://x.com/alexisgallagher/status/2042396986327060736?s=20 .)
Status: experimental but live-tested against real Pi + real OpenAI backends. Recommended rollout: install project-local first, use for a week, keep rollback easy.
| Provider/model family | Remote compaction | previous_response_id continuity | Custom WS stream | Live-tested |
|---|---|---|---|---|
openai/* | Yes | Yes | Yes | Yes |
openai-codex/* | Yes | No (built-in transport retained) | No (built-in transport retained) | Yes |
| Azure | Partial (opt-in via config) | Partial | No | No |
Project-local (recommended):
pi install -l git:github.com/algal/pi-openai-server-compaction
Global:
pi install git:github.com/algal/pi-openai-server-compaction
One-shot, non-persistent:
git clone https://github.com/algal/pi-openai-server-compaction.git
cd pi-openai-server-compaction && npm install
pi -e ./src/index.ts --model openai/gpt-5.6-luna
>= 22>=0.80.9 <0.81.0openai/gpt-5.6-sol or openai-codex/gpt-5.6-solOn compaction, the extension requests Responses compaction v2 through /v1/responses in parallel with generating a portable Pi text summary. This gives you both:
For direct openai/* models between compactions, the extension also:
store: true and context_managementprevious_response_id for live continuation when safeFor openai-codex/* models, the extension preserves the built-in Codex transport and only injects reconstructed remote compaction history after compaction boundaries.
On Pi compaction events for supported models, the extension:
POST /v1/responses with the conversation history, a trailing compaction_trigger, system prompt, tools, reasoning config, and text configcompaction item in CompactionEntry.details.remoteCompactionThe compaction request mirrors the shape of surrounding normal requests (reasoning effort, text settings, tool definitions) rather than using endpoint defaults.
The extension clears live continuation state on: session start/reload/resume, switch/fork, tree navigation, compaction completion, model selection, and shutdown.
Remote compaction history is only replayed for compatible models. Cross-model turns are filtered from reconstructed replay history to prevent contamination after resume or tree navigation.
Users should be aware:
openai/* models, the extension sets store: true on requests, meaning OpenAI retains conversation data server-sideConfig is read from:
~/.pi/agent/openai-server-compaction.json (global).pi/openai-server-compaction.json (project-local, takes precedence){
"enabled": true,
"includeAzure": false,
"thresholdRatio": 0.7,
"compactThreshold": 0,
"usePreviousResponseId": true,
"notify": false
}
Environment overrides:
| Variable | Effect |
|---|---|
PI_OPENAI_SERVER_COMPACTION_ENABLED | Enable/disable the extension |
PI_OPENAI_SERVER_COMPACTION_AZURE | Include Azure OpenAI models |
PI_OPENAI_SERVER_COMPACTION_THRESHOLD | Explicit compact threshold (tokens) |
PI_OPENAI_SERVER_COMPACTION_RATIO | Compact threshold as ratio of context window (default: 0.7) |
PI_OPENAI_SERVER_COMPACTION_PREVIOUS_RESPONSE_ID | Enable/disable previous_response_id |
PI_OPENAI_SERVER_COMPACTION_NOTIFY | Show UI notifications when features activate |
If something goes wrong:
PI_OPENAI_SERVER_COMPACTION_ENABLED=0 or add "enabled": false to config--no-extensions/reload in Pi to re-initialize extensionspi remove pi-openai-server-compactioncompaction entries with details.remoteCompaction to see if remote compaction was recordedSmoke test (offline, verifies imports and key algorithms):
npm run smoke
Live end-to-end test (requires working Pi + OpenAI auth):
npm run test:live
Override the test model:
PI_OPENAI_SERVER_COMPACTION_TEST_MODEL=openai-codex/gpt-5.6-sol npm run test:live
get_session_stats() (requires Pi core changes)| File | Purpose |
|---|---|
src/index.ts | Extension wiring, compaction hook, lifecycle handling |
src/remote-compaction.ts | Responses compaction v2 integration and replacement-history handling |
src/openai-ws-stream.ts | WebSocket continuation path |
src/openai-ws-connection.ts | WebSocket connection manager |
src/openai.ts | Model detection and payload patching |
src/custom-stream.ts | Provider override entrypoint |
src/config.ts | Configuration loading |
src/state.ts | Ephemeral per-session runtime state |
src/stream-message-shared.ts | Shared assistant message builders |
tests/live/openai-compaction-rpc-live.ts | Live Pi RPC regression test |
scripts/smoke.mjs | Offline smoke test with peer-package bootstrapping |
benchmarks/product-defaults/ | Current default-vs-default benchmark, retained evidence, and report |
benchmarks/native-vs-text/ | Earlier matched-cap benchmark, retained with a correction |
ARCHITECTURE.md | Design and control-flow documentation |
TESTPLAN.md | Manual and automated test plan |
CHANGELOG.md | Version history |
MIT. See LICENSE.md.
8 commits
TypeScript
54.9%
JavaScript
45.1%
This is a Pi extension which adds Codex-style remote compaction for OpenAI models, giving you better continuity across compaction boundaries while preserving all of Pi's normal features.
What does that mean? Why would you want it? My impression has been that Codex compacts better than Claude Code and better than Pi. And I supposed this was because Codex compacts by using OpenAI's server-side Responses compaction protocol. That protocol sends a compaction_trigger through POST /v1/responses and receives an encrypted compaction item. This extension configures Pi to use that protocol for OpenAI models alongside Pi's native compaction logic.
But is Codex's compaction actually better? Since the OpenAI compaction endpoint compacts to encrypted binary blobs, no one can say what it is doing under the hood. However, we don't need to know how it works to determine if it works better. Anyone can call the endpoint. And since codex is an open source, we can mimic exactly how codex itself uses the endpoint. That is what this extension configures Pi to do.
So is native compaction better? For the user-facing comparison I care about, the evidence says yes, with important price and reliability qualifiers. A held-out benchmark of the real product defaults found 78.0% exact recall for this extension's native policy versus 48.0% for Pi's default compactor; full context scored 100%. Native did this while emitting 4.58x as many compaction output tokens and leaving a 29% larger billed downstream context. It preserved much more old state, but this is not evidence that it is better at the same token budget. Native was also highly variable: every large artifact scored perfectly, while three small artifacts performed about as poorly as Pi.
Strictly, this directly compares Pi with this extension's reconstruction of Codex-style compaction, not with an end-to-end run of the Codex CLI. The result also does not show that the endpoint reliably detects when more capacity is needed: its short artifacts were the failures. What it does show is that the native default sometimes allocates far more context, and those large-allocation runs drove its aggregate advantage.
An earlier benchmark reported 100% native recall versus 82.8% and 76.7% for two text summaries at apparently matched downstream sizes. That procedure first observed native's output usage and then imposed it as the text arm's maximum, which is asymmetric and can favor native. Its same-budget interpretation is therefore superseded. See the new product-defaults report and reproduction instructions. The older matched-cap report remains retained with a methodological correction.
None of this proves the encrypted blobs use a clever latent-space representation. They might be encrypted optimized text or structured state values. (A little reverse engineering suggests the blobs are produced through a textual prompt, for what it is worth: https://x.com/alexisgallagher/status/2042396986327060736?s=20 .)
Status: experimental but live-tested against real Pi + real OpenAI backends. Recommended rollout: install project-local first, use for a week, keep rollback easy.
| Provider/model family | Remote compaction | previous_response_id continuity | Custom WS stream | Live-tested |
|---|---|---|---|---|
openai/* | Yes | Yes | Yes | Yes |
openai-codex/* | Yes | No (built-in transport retained) | No (built-in transport retained) | Yes |
| Azure | Partial (opt-in via config) | Partial | No | No |
Project-local (recommended):
pi install -l git:github.com/algal/pi-openai-server-compaction
Global:
pi install git:github.com/algal/pi-openai-server-compaction
One-shot, non-persistent:
git clone https://github.com/algal/pi-openai-server-compaction.git
cd pi-openai-server-compaction && npm install
pi -e ./src/index.ts --model openai/gpt-5.6-luna
>= 22>=0.80.9 <0.81.0openai/gpt-5.6-sol or openai-codex/gpt-5.6-solOn compaction, the extension requests Responses compaction v2 through /v1/responses in parallel with generating a portable Pi text summary. This gives you both:
For direct openai/* models between compactions, the extension also:
store: true and context_managementprevious_response_id for live continuation when safeFor openai-codex/* models, the extension preserves the built-in Codex transport and only injects reconstructed remote compaction history after compaction boundaries.
On Pi compaction events for supported models, the extension:
POST /v1/responses with the conversation history, a trailing compaction_trigger, system prompt, tools, reasoning config, and text configcompaction item in CompactionEntry.details.remoteCompactionThe compaction request mirrors the shape of surrounding normal requests (reasoning effort, text settings, tool definitions) rather than using endpoint defaults.
The extension clears live continuation state on: session start/reload/resume, switch/fork, tree navigation, compaction completion, model selection, and shutdown.
Remote compaction history is only replayed for compatible models. Cross-model turns are filtered from reconstructed replay history to prevent contamination after resume or tree navigation.
Users should be aware:
openai/* models, the extension sets store: true on requests, meaning OpenAI retains conversation data server-sideConfig is read from:
~/.pi/agent/openai-server-compaction.json (global).pi/openai-server-compaction.json (project-local, takes precedence){
"enabled": true,
"includeAzure": false,
"thresholdRatio": 0.7,
"compactThreshold": 0,
"usePreviousResponseId": true,
"notify": false
}
Environment overrides:
| Variable | Effect |
|---|---|
PI_OPENAI_SERVER_COMPACTION_ENABLED | Enable/disable the extension |
PI_OPENAI_SERVER_COMPACTION_AZURE | Include Azure OpenAI models |
PI_OPENAI_SERVER_COMPACTION_THRESHOLD | Explicit compact threshold (tokens) |
PI_OPENAI_SERVER_COMPACTION_RATIO | Compact threshold as ratio of context window (default: 0.7) |
PI_OPENAI_SERVER_COMPACTION_PREVIOUS_RESPONSE_ID | Enable/disable previous_response_id |
PI_OPENAI_SERVER_COMPACTION_NOTIFY | Show UI notifications when features activate |
If something goes wrong:
PI_OPENAI_SERVER_COMPACTION_ENABLED=0 or add "enabled": false to config--no-extensions/reload in Pi to re-initialize extensionspi remove pi-openai-server-compactioncompaction entries with details.remoteCompaction to see if remote compaction was recordedSmoke test (offline, verifies imports and key algorithms):
npm run smoke
Live end-to-end test (requires working Pi + OpenAI auth):
npm run test:live
Override the test model:
PI_OPENAI_SERVER_COMPACTION_TEST_MODEL=openai-codex/gpt-5.6-sol npm run test:live
get_session_stats() (requires Pi core changes)| File | Purpose |
|---|---|
src/index.ts | Extension wiring, compaction hook, lifecycle handling |
src/remote-compaction.ts | Responses compaction v2 integration and replacement-history handling |
src/openai-ws-stream.ts | WebSocket continuation path |
src/openai-ws-connection.ts | WebSocket connection manager |
src/openai.ts | Model detection and payload patching |
src/custom-stream.ts | Provider override entrypoint |
src/config.ts | Configuration loading |
src/state.ts | Ephemeral per-session runtime state |
src/stream-message-shared.ts | Shared assistant message builders |
tests/live/openai-compaction-rpc-live.ts | Live Pi RPC regression test |
scripts/smoke.mjs | Offline smoke test with peer-package bootstrapping |
benchmarks/product-defaults/ | Current default-vs-default benchmark, retained evidence, and report |
benchmarks/native-vs-text/ | Earlier matched-cap benchmark, retained with a correction |
ARCHITECTURE.md | Design and control-flow documentation |
TESTPLAN.md | Manual and automated test plan |
CHANGELOG.md | Version history |
MIT. See LICENSE.md.
8 commits
TypeScript
54.9%
JavaScript
45.1%