Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions docs/en/docs/integrations/codex.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,77 @@ This adds inference latency to each prompt and is not the normal interactive set

When Codex sends its model requests through the PowerContext gateway, the gateway prepares and injects context for
each turn and, with `PC_GATEWAY_SEDIMENT=on`, captures the turn. Core checks every request the gateway forwards and
rejects a prepared-context block that the gateway did not inject, so a turn that also carries the plugin's block fails.
rejects a prepared-context block that the gateway did not inject. Codex adds the plugin's prepared context to the
thread as a developer message, so once the plugin's recall adds context to a thread, Core rejects the check for that
request and every later request in the thread with HTTP 422, even in a non-enforcing mode. The gateway returns HTTP
503 `gate_paused`, and the turn fails before the model receives it.

Keep the plugin defaults where Codex calls its model directly or signs in with a subscription account, because the
gateway does not carry that traffic. To route Codex through the gateway, configure Core, Codex, the plugin, and the
gateway as follows.

### Run Core in a non-enforcing mode

Codex does not send an output limit (`max_output_tokens`). An enforcing Core requires a token budget on the target and
an output limit on every generation request, so it withholds each Codex request (for example with
`output_limit_required`), the gateway returns HTTP 400, and the turn fails before the model receives it. Run Core with
inference protection in `disabled`, `shadow`, or `advisory` mode, with a target for the profile that
`PC_GATEWAY_UPSTREAM_PROFILE` names (`default` unless set). Core withholds a check for an unknown target in every
mode. For example:

```bash
export POWERCONTEXT_SERVER_INFERENCE_PROTECTION='{
"mode": "advisory",
"targets": {
"default": {
"endpoint": "https://models.example/v1/responses",
"model": "approved-model",
"protocol": "openai-responses"
}
}
}'
```

None of these modes stops delivery for a privacy rule. Core reports incomplete privacy coverage, which the gateway
accepts only when it discovered a non-enforcing Core at startup, so restart the gateway after changing the mode. See
[Context reuse and final admission](../operate/context-protection.md) and
[Inference privacy protection](../operate/inference-protection.md) before choosing a mode.

Codex sends `prompt_cache_key` with every request. A non-enforcing Core does not withhold the request for it, so
adding `provider-default` to the target's `allowed_cache_retention` is optional.

### Point Codex at the gateway

Add a provider for the gateway's `/gateway/v1` to `~/.codex/config.toml` and select it. Codex 0.167 accepts only
`wire_api = "responses"` for a custom provider, and on the Responses route the gateway identifies the conversation
from Codex's `thread-id` header:

```toml
model = "approved-model"
model_provider = "powercontext-gateway"

[model_providers.powercontext-gateway]
name = "PowerContext gateway"
base_url = "http://127.0.0.1:8787/gateway/v1"
wire_api = "responses"
env_key = "PC_GATEWAY_KEY"
```

Keys at the top level of `~/.codex/config.toml` send every Codex session through the gateway. To choose per session,
put the same keys in `~/.codex/powercontext-gateway.config.toml` and start Codex with
`codex --profile powercontext-gateway`; Codex 0.167 rejects a `[profiles.<name>]` table in `config.toml`.

Export the gateway's `PC_GATEWAY_API_KEY` value as `PC_GATEWAY_KEY` in the environment that starts Codex; Codex sends
it as the bearer token. While `PC_GATEWAY_KEY` is unset, Codex stops with `Missing environment variable` before it
sends a request. A gateway on a loopback listener may run without `PC_GATEWAY_API_KEY` and then accepts any token, but
Codex still needs the variable.

In a combined deployment (`PC_GATEWAY_CORE_UDS`), the gateway forwards Core API and MCP requests to Core with the
client's own credentials, so the plugin can use the gateway's address as its Server URL
(`powercontext setup codex --server-url`).

### Turn the plugin's prompt recall and capture off

Turn the plugin's prompt-time recall and prompt capture off before starting Codex:

```bash
Expand All @@ -315,15 +385,37 @@ export POWERCONTEXT_CODEX_CAPTURE_PROMPTS=false
codex
```

Start a new thread after changing these settings. Codex resends the thread's history with each request, so a thread
that already carries the plugin's context keeps failing through the gateway after recall is turned off.

With recall off, `UserPromptSubmit` sends no preparation request and adds no context. It still captures prompts while
`POWERCONTEXT_CODEX_CAPTURE_PROMPTS` is `true`; with both off, it sends no request. The Session binding, bootstrap
context, MCP tools, and diagnostics keep their own settings. Keep the default where Codex calls its model directly or
signs in with a subscription account, because the gateway does not carry that traffic.
context, MCP tools, and diagnostics keep their own settings.

### Configure the gateway

Run the gateway in a combined deployment (`PC_GATEWAY_CORE_UDS`), which injects requests that carry a conversation by
default. Set:

- `PC_GATEWAY_UPSTREAM_URL_RESPONSES` to the target's `endpoint`, and `PC_GATEWAY_UPSTREAM_API_KEY_RESPONSES` to the
provider key. The gateway sends to this URL itself, and without it the Responses route returns HTTP 501
`responses_route_disabled`.
- `PC_GATEWAY_API_KEY` to the key that Codex sends as `PC_GATEWAY_KEY`. A non-loopback listener requires it.
- `PC_GATEWAY_CORE_TOKEN` when Core enforces authentication.
- `PC_GATEWAY_SEDIMENT=on`, so that the gateway captures each completed turn as a Source.
- `PC_GATEWAY_DEFAULT_SCOPE` to the Scope this plugin resolves for the workspace. Running `scope_binding.py` without
`--bind-scope`, as described in [Hand off the current work in one turn](#hand-off-the-current-work-in-one-turn),
prints that Scope.

The gateway reads these settings at startup. For its authentication and injection settings, see
[Deploy the Server](../operate/deploy-server.md#gateway-authentication-boundaries). A model request body through the
gateway is limited to at most 1 MiB, so a long thread can be refused before it is sent; see
[Gateway byte capacity](../operate/deploy-server.md#gateway-byte-capacity).

With recall off, prompt-time context comes only from the gateway, with two differences:

- The gateway prepares and captures in the Scope set by `PC_GATEWAY_DEFAULT_SCOPE`, not in the Scope this plugin
resolves for the Session or workspace. Set it to the same Scope.
- The gateway prepares and captures every conversation in the one Scope set by `PC_GATEWAY_DEFAULT_SCOPE`, not in
the Scope this plugin resolves for each Session or workspace.
- The gateway does not send the bootstrap receipt, so Memory already injected by
[Session bootstrap context](#opt-in-to-session-bootstrap-context) can appear again in the gateway's context.

Expand Down
92 changes: 87 additions & 5 deletions docs/zh/docs/integrations/codex.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,105 @@ export POWERCONTEXT_CODEX_FLUSH_ON_CAPTURE=true
## 把提示词时上下文交给 gateway

Codex 的模型请求经过 PowerContext gateway 时,gateway 为每一轮准备并注入上下文;设置 `PC_GATEWAY_SEDIMENT=on`
后,gateway 也会采集这一轮。Core 检查 gateway 转发的每个请求,拒绝不是 gateway 注入的 prepared-context 块,
因此同时带有插件上下文块的一轮会失败。启动 Codex 前,关闭插件的提示词时召回和提示词采集:
后,gateway 也会采集这一轮。Core 检查 gateway 转发的每个请求,拒绝不是 gateway 注入的 prepared-context 块。
Codex 把插件准备的上下文作为 developer 消息加入 thread,因此插件的召回一旦向某个 thread 添加了上下文,即使 Core
处于非 enforcing 模式,这个请求以及该 thread 之后的每个请求的检查都会被 Core 以 HTTP 422 拒绝。gateway 返回
HTTP 503 `gate_paused`,这一轮在模型收到请求前失败。

Codex 直接调用模型或使用订阅账号登录时,gateway 不承载这些流量,请保留插件的默认值。要让 Codex 经过 gateway,
按下面的步骤配置 Core、Codex、插件和 gateway。

### 让 Core 运行在非 enforcing 模式

Codex 不发送输出上限(`max_output_tokens`)。enforcing 模式的 Core 要求目标配置 token 预算,并要求每个生成请求
带输出上限,因此会暂缓每个 Codex 请求(例如原因为 `output_limit_required`),gateway 返回 HTTP 400,这一轮在
模型收到请求前失败。请把 Core 的推理保护设为 `disabled`、`shadow` 或 `advisory` 模式,并为
`PC_GATEWAY_UPSTREAM_PROFILE` 指定的 profile(未设置时为 `default`)配置目标。任何模式下,Core 都会暂缓目标未知
的检查。例如:

```bash
export POWERCONTEXT_SERVER_INFERENCE_PROTECTION='{
"mode": "advisory",
"targets": {
"default": {
"endpoint": "https://models.example/v1/responses",
"model": "approved-model",
"protocol": "openai-responses"
}
}
}'
```

这几种模式都不会因隐私规则阻止交付。Core 报告不完整的隐私覆盖;只有 gateway 启动时发现 Core 不是 enforcing,
它才接受这种覆盖,因此修改模式后请重启 gateway。选择模式前请阅读
[上下文复用与最终准入](../operate/context-protection.md)和[推理隐私保护](../operate/inference-protection.md)。

Codex 的每个请求都带 `prompt_cache_key`。非 enforcing 的 Core 不会因此暂缓请求,所以是否在目标的
`allowed_cache_retention` 中加入 `provider-default` 可以自行决定。

### 让 Codex 指向 gateway

在 `~/.codex/config.toml` 中添加指向 gateway `/gateway/v1` 的 provider,并选择它。Codex 0.167 的自定义 provider
只接受 `wire_api = "responses"`;在 Responses 路由上,gateway 根据 Codex 的 `thread-id` 请求头识别对话:

```toml
model = "approved-model"
model_provider = "powercontext-gateway"

[model_providers.powercontext-gateway]
name = "PowerContext gateway"
base_url = "http://127.0.0.1:8787/gateway/v1"
wire_api = "responses"
env_key = "PC_GATEWAY_KEY"
```

这些键写在 `~/.codex/config.toml` 顶层时,每个 Codex 会话都会经过 gateway。若要按会话选择,把同样的键写进
`~/.codex/powercontext-gateway.config.toml`,再用 `codex --profile powercontext-gateway` 启动 Codex;Codex 0.167
不接受 `config.toml` 中的 `[profiles.<name>]` 表。

请在启动 Codex 的环境中,把 gateway 的 `PC_GATEWAY_API_KEY` 的值导出为 `PC_GATEWAY_KEY`;Codex 会把它作为 bearer
token 发送。`PC_GATEWAY_KEY` 未设置时,Codex 会以 `Missing environment variable` 停止,不发送任何请求。监听回环地址
的 gateway 可以不设置 `PC_GATEWAY_API_KEY`,此时接受任意 token,但 Codex 仍需要这个变量。

在组合部署(`PC_GATEWAY_CORE_UDS`)中,gateway 把 Core API 和 MCP 请求连同客户端自己的凭据转发给 Core,因此插件
可以把 gateway 的地址用作 Server URL(`powercontext setup codex --server-url`)。

### 关闭插件的提示词召回和采集

启动 Codex 前,关闭插件的提示词时召回和提示词采集:

```bash
export POWERCONTEXT_CODEX_PROMPT_RECALL=false
export POWERCONTEXT_CODEX_CAPTURE_PROMPTS=false
codex
```

修改这些设置后请开启新的 thread。Codex 每次请求都会重新发送 thread 的历史,因此已经带有插件上下文的 thread
在关闭召回后,经过 gateway 时仍会失败。

关闭召回后,`UserPromptSubmit` 不发送准备请求,也不添加上下文。`POWERCONTEXT_CODEX_CAPTURE_PROMPTS` 为 `true`
时它仍会采集提示词;两项都关闭时,它不发送任何请求。Session binding、启动上下文、MCP 工具和诊断仍由各自的设置控制。
Codex 直接调用模型或使用订阅账号登录时,gateway 不承载这些流量,请保留默认值。

### 配置 gateway

以组合部署(`PC_GATEWAY_CORE_UDS`)运行 gateway,它默认注入带有对话 ID 的请求。设置:

- `PC_GATEWAY_UPSTREAM_URL_RESPONSES` 设为目标的 `endpoint`,`PC_GATEWAY_UPSTREAM_API_KEY_RESPONSES` 设为供应商
密钥。gateway 自己向这个 URL 发送请求;未设置时,Responses 路由返回 HTTP 501 `responses_route_disabled`。
- `PC_GATEWAY_API_KEY` 设为 Codex 以 `PC_GATEWAY_KEY` 发送的密钥。监听非回环地址时必须设置。
- Core 启用鉴权时,设置 `PC_GATEWAY_CORE_TOKEN`。
- `PC_GATEWAY_SEDIMENT=on`,让 gateway 把每个完成的轮次采集为 Source。
- `PC_GATEWAY_DEFAULT_SCOPE` 设为插件为该 workspace 解析的 Scope。按[一句话交接当前工作](#一句话交接当前工作)中的
说明,不带 `--bind-scope` 运行 `scope_binding.py`,即可打印这个 Scope。

gateway 在启动时读取这些设置。gateway 的鉴权和注入设置见[部署 Server](../operate/deploy-server.md#网关认证边界)。
经过 gateway 的模型请求正文最多 1 MiB,过长的 thread 可能在发送前被拒绝,见
[Gateway 字节容量](../operate/deploy-server.md#gateway-字节容量)。

关闭召回后,提示词时上下文只来自 gateway,有两点不同:

- gateway 在 `PC_GATEWAY_DEFAULT_SCOPE` 指定的 Scope 中准备和采集,而不是插件为 Session 或 workspace 解析的 Scope。
请把它设为同一个 Scope。
- gateway 把所有对话都放在 `PC_GATEWAY_DEFAULT_SCOPE` 指定的同一个 Scope 中准备和采集,而不是插件为每个 Session
或 workspace 解析的 Scope。
- gateway 不发送启动上下文的 receipt,因此[启动上下文](#选择启用-session-启动上下文)已经注入的 Memory
可能再次出现在 gateway 的上下文中。

Expand Down
Loading