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
2 changes: 2 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ header:
# Protocol recordings and upstream notices retain their original bytes and licenses.
- 'gateway/crates/pc-gateway-app/tests/fixtures/**'
- 'src/powercontext/builtin/artifacts/lexical_data/*.txt'
# This prompt is sent verbatim to the model; a license header would change its instructions.
- 'gateway/crates/pc-gateway-app/src/default_steering.txt'
- 'src/powercontext/server/dashboard/static/vendor/**'
- '.gitattributes'
- '**/*.service.in'
Expand Down
10 changes: 7 additions & 3 deletions docs/en/docs/operate/deploy-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ Set `PC_GATEWAY_INJECTION_CODE=on` to include code evidence from the Scope's cod

### Gateway instruction

`PC_GATEWAY_STEERING_FILE` names a file with a short, fixed instruction that the gateway adds to every request on the Responses route (`/gateway/v1/responses`); the Chat route is never changed. The gateway reads the file once at startup. It must be UTF-8, non-empty after trimming, at most 4,096 bytes, and free of PowerContext's reserved `BEGIN_POWERCONTEXT_` markers; otherwise the gateway exits at startup instead of running without it. Nothing is added when the variable is unset.
The gateway adds a built-in cost-guidance instruction (S) to every request on the Responses route (`/gateway/v1/responses`) by default; the Chat route is never changed. The text encourages targeted searches, batched reads, concise edits, and focused validation. `PC_GATEWAY_STEERING=off` disables it; unset or `on` enables it. Other values prevent startup.

`PC_GATEWAY_STEERING_FILE` replaces the built-in text with a custom instruction. When steering is enabled, the gateway reads the file once at startup. It must be UTF-8, non-empty after trimming, at most 4,096 bytes, and free of PowerContext's reserved `BEGIN_POWERCONTEXT_` markers; otherwise the gateway exits at startup instead of running with a different instruction. `PC_GATEWAY_STEERING=off` takes precedence and does not read the file.

The text becomes the first content block of the developer message that follows the client's first developer message, which is where Codex places its own `developer_instructions`. Without such a message, the gateway inserts a new developer message after the client's first developer message, or before the first user message when the request has no developer message. The text is identical on every request, so it stays part of the cached prefix. Core checks it as part of the final request like any other request text. It is independent of context injection, capture, and replay.

Expand All @@ -221,9 +223,9 @@ These are per-body and per-class concurrency limits. They do not measure retaine
### Memory extraction over the cached prefix

When the gateway runs on the same host as the Server, Atomic Memory extraction for a captured turn can reuse the
provider's prompt cache for the agent's conversation instead of sending the transcript again. With an OAuth subscription, only `codex` supports it, and only when the Server is not enforcing: the extraction request goes out with the subscription's credentials like the agent's own requests, counts against that account's usage limits, and cannot carry an output limit, which that endpoint rejects. Traffic of the other subscriptions uses normal extraction. Prefix extraction is off by default and needs both sides:
provider's prompt cache for the agent's conversation instead of sending the transcript again. The gateway enables prefix extraction by default for supported upstreams; `PC_GATEWAY_PREFIX_EXTRACTION=off` disables it, and `on` explicitly enables it. Other values prevent startup. With an OAuth subscription, only `codex` supports it, and only when the Server is confirmed as non-enforcing: the extraction request goes out with the subscription's credentials like the agent's own requests, counts against that account's usage limits, and cannot carry an output limit, which that endpoint rejects. Traffic of the other subscriptions uses normal extraction. Capture and the Server callback must also be configured:

- **Gateway:** `PC_GATEWAY_PREFIX_EXTRACTION=on` together with capture (`PC_GATEWAY_SEDIMENT=on`). For a Responses
- **Gateway:** Enable capture with `PC_GATEWAY_SEDIMENT=on`; capture remains off by default. For a Responses
conversation, the gateway keeps the exact request it sent and the response output of each turn's final response in
memory, for at most 30 minutes, at most 64 turns and at most 64 MiB in total; the oldest are dropped first, and a
turn dropped before extraction goes through normal extraction. It records an opaque handle in the captured Source's metadata,
Expand Down Expand Up @@ -251,6 +253,8 @@ provider's prompt cache for the agent's conversation instead of sending the tran
`POWERCONTEXT_SERVER_INFERENCE_PREFIX_EXTRACTION_GATEWAY_HEADERS` set to `{"Authorization": "Bearer <PC_GATEWAY_API_KEY>"}`
when the gateway requires a key. A generation model is still required, because normal extraction is the fallback.

With `POWERCONTEXT_SERVER_INFERENCE_PREFIX_EXTRACTION_GATEWAY_URL` configured, the Server defaults Atomic Memory processing to `one_pass` unless a mode was explicitly supplied. Set `POWERCONTEXT_SERVER_RUNTIME_ATOMIC_MEMORY_PROCESSING_MODE=per_candidate` to retain per-candidate extraction, or `one_pass` to select one-pass explicitly. A Server without the callback URL and direct SDK configurations keep their existing `per_candidate` default. The gateway cannot discover or configure this URL for a separately started Server.

Cached input is not free: the provider still bills it at a reduced rate. When a processing window holds exactly one
such turn, the Server first estimates both requests. The prefixed request costs Core's instruction plus the final
response's output tokens at the full rate, plus the cached input tokens multiplied by
Expand Down
14 changes: 7 additions & 7 deletions docs/zh/design/atomic_memory_one_pass_design.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| 项 | 内容 |
| --- | --- |
| 特性类型 | 原子记忆的可选处理模式,**默认关闭** |
| 特性类型 | 原子记忆处理模式;配置网关回连的 Server 默认开启,其余场景保持逐候选默认 |
| 对外契约 | 不新增 HTTP API,不改 OpenAPI,不改持久化格式,不新增提示词 key |
| 新增配置 | 一个 Server 运行时配置项,选择原子记忆处理模式 |
| 文档状态 | 已实现,评测结果见 §6 |
Expand All @@ -18,7 +18,7 @@
可以命中模型服务的前缀缓存,因此不再为每条候选重复发送整段 Source。

它作为可选模式提供:在两个评测集上,成本是现有流程的 20%–31%,存储条数与现有流程接近,
编码场景的事实覆盖度持平,对话场景的问答准确率不低于现有流程(见 §6)。默认模式不变,替换默认模式的条件见 §8。
编码场景的事实覆盖度持平,对话场景的问答准确率不低于现有流程(见 §6)。配置网关前缀抽取回连地址的 Server 默认使用单次整理,显式配置仍优先;SDK 与未配置网关回连的 Server 保持逐候选默认。全面替换默认模式的条件见 §8。

## 1. 约束

Expand All @@ -28,7 +28,7 @@
| C2 | 溯源不变:每条发布的记忆仍带正确的 `sources` | 第 2 步的每个操作给出 `evidence_ids`,Core 校验后按现有方式计算 `sources` |
| C3 | 校验与读依赖不变 | 每个 revise / merge 转换为 `AtomicMemoryReconciliationOutput` 后由 `workset.apply()` 校验;展示过的已有记忆全部记入 workset 的读依赖,发布时照常做冲突检查 |
| C4 | 提示词配置不分裂:用户仍只维护 `atomic_memory.extract` 与 `atomic_memory.reconcile` | 流程指令是代码常量;两个 key 的现有配置嵌入其中(§4) |
| C5 | 默认行为字节级不变 | 未开启时不进入任何新代码路径 |
| C5 | 显式模式优先,默认值按部署配置选择 | 网关回连已配置且模式未指定的 Server 使用单次整理;其余默认与显式逐候选模式保留原流程 |

## 2. 现有流程(master)

Expand Down Expand Up @@ -143,7 +143,7 @@ Core 在应用时记录本次回复已写过的记忆,再次以 revise / merge
### 5.1 配置

`RuntimeConfig` 新增 `atomic_memory_processing_mode: Literal["per_candidate", "one_pass"] = "per_candidate"`,
对应环境变量 `POWERCONTEXT_SERVER_RUNTIME_ATOMIC_MEMORY_PROCESSING_MODE`,中英文配置文档已同步。
对应环境变量 `POWERCONTEXT_SERVER_RUNTIME_ATOMIC_MEMORY_PROCESSING_MODE`。Server 配置了 `inference.prefix_extraction_gateway_url` 且未显式指定模式时,解析后的运行时默认值为 `one_pass`;显式 `per_candidate` 或 `one_pass` 均优先。SDK 的 `RuntimeConfig` 默认值不变,完整配置序列化中的具体模式也按显式设置处理。
`AtomicMemoryProcessingConfig.from_runtime()` 读取该值。第 1 步的项数上限(30)、每个检索词展示的记忆数(5)
与展示总数上限(40)是 `one_pass.py` 中的常量。

Expand All @@ -156,7 +156,7 @@ Core 在应用时记录本次回复已写过的记忆,再次以 revise / merge
流程指令、它所续接的对话全文(此前各步的消息、模型回复和纠正请求,由推理接口随回复一并返回),以及本步的新消息。
第 3 步拆成多次调用时,每次调用都只续接第 2 步,各自单独估算。

- 第 1 步或第 2 步的输入超出预算时,本窗口改用逐候选流程(§2)处理,结果与默认模式相同。
- 第 1 步或第 2 步的输入超出预算时,本窗口改用逐候选流程(§2)处理。
第 2 步不拆成多次调用,召回到的已有记忆也不因预算丢弃:逐候选流程在预算不足时本来就会把每次比对的记忆数
减半直到一条,把第 2 步拆到最细也是同样的调用方式,却要另外处理跨批次的相似条目。回到逐候选流程不引入新的
行为,代价是该窗口没有成本收益,第 2 步超出时还多付一次第 1 步调用。
Expand Down Expand Up @@ -293,7 +293,7 @@ Core 在应用时记录本次回复已写过的记忆,再次以 revise / merge
6. **其他模型服务的前缀缓存。** 成本数据来自一个支持自动前缀缓存的模型服务。不支持前缀缓存、或需要显式
标记缓存位置的服务上,成本收益会小于 §6.2。

## 8. 替换默认模式的条件
## 8. 全面替换默认模式的条件

同时满足以下条件后,再讨论把单次整理设为默认:

Expand All @@ -303,7 +303,7 @@ Core 在应用时记录本次回复已写过的记忆,再次以 revise / merge
- 存储条数与每条记忆的长度和现有流程接近。

以上各项在 §6 的评测中已满足。评测只覆盖一个模型服务、内置抽取规则和单 Source 窗口(§7),
在这些条件之外重复得到同样结果后,再提出替换默认模式。
当前默认切换仅适用于已配置网关回连地址、且未显式指定模式的 Server。在这些条件之外重复得到同样结果后,再提出替换 SDK 与其他 Server 场景的默认模式。

## 附录 A:流程指令

Expand Down
10 changes: 7 additions & 3 deletions docs/zh/docs/operate/deploy-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ Gemini 关闭 Gateway 上下文注入。Claude CLI 只接受新的单轮文本

### 网关固定指令

`PC_GATEWAY_STEERING_FILE` 指定一个文件,其中是一段简短的固定指令,网关把它加入 Responses 入口(`/gateway/v1/responses`)的每个请求;Chat 入口的请求不受影响。网关只在启动时读取一次该文件。文件必须是 UTF-8,去掉首尾空白后不能为空,不超过 4096 字节,并且不能含有 PowerContext 保留的 `BEGIN_POWERCONTEXT_` 标记;否则网关在启动时退出,而不是不带指令运行。未设置该变量时不加入任何内容。
网关默认向 Responses 入口(`/gateway/v1/responses`)的每个请求加入内置的成本指导指令(S),提示模型定向搜索、合并读取、集中修改并进行针对性验证;Chat 入口不受影响。`PC_GATEWAY_STEERING=off` 关闭指令;未设置或设为 `on` 时开启,其他值会阻止启动。

`PC_GATEWAY_STEERING_FILE` 用自定义指令替换内置文本。开启指令时,网关只在启动时读取一次该文件。文件必须是 UTF-8,去掉首尾空白后不能为空,不超过 4096 字节,并且不能含有 PowerContext 保留的 `BEGIN_POWERCONTEXT_` 标记;否则网关在启动时退出,不会改用其他指令。`PC_GATEWAY_STEERING=off` 优先于文件配置,关闭时不读取文件。

指令文本成为客户端第一条 developer 消息之后那条 developer 消息的第一个内容块,也就是 Codex 放置自身 `developer_instructions` 的位置。没有这样的消息时,网关在客户端第一条 developer 消息之后插入一条新的 developer 消息;请求中没有 developer 消息时,插在第一条用户消息之前。每个请求中的文本都相同,因此它始终是缓存前缀的一部分。Core 把它作为最终请求的一部分,像其他请求文本一样检查。它与上下文注入、沉淀和重放相互独立。

Expand All @@ -223,9 +225,9 @@ Rust Gateway 在启动时通过 `/v1/capabilities` 查询一次 `context_protect
### 复用缓存前缀抽取记忆

网关与 Server 部署在同一主机时,为一个已沉淀回合抽取 Atomic Memory 可以复用服务商为 Agent 会话写下的提示缓存,
而不必再次发送对话记录。使用 OAuth 订阅时,只有 `codex` 支持前缀抽取,并且 Server 不能是 enforcing:抽取请求和 agent 自己的请求一样用订阅的凭据发出,计入该账号的用量限额,并且不能带输出上限(该端点拒绝这个参数)。其他订阅的流量使用普通抽取。前缀抽取默认关闭,需要两端同时开启:
而不必再次发送对话记录。网关对支持的上游默认开启前缀抽取;`PC_GATEWAY_PREFIX_EXTRACTION=off` 关闭,`on` 显式开启,其他值会阻止启动。使用 OAuth 订阅时,只有 `codex` 支持前缀抽取,并且必须确认 Server 为非 enforcing:抽取请求和 agent 自己的请求一样用订阅的凭据发出,计入该账号的用量限额,并且不能带输出上限(该端点拒绝这个参数)。其他订阅的流量使用普通抽取。还需配置沉淀与 Server 回连:

- **网关:** 设置 `PC_GATEWAY_PREFIX_EXTRACTION=on`,并开启沉淀(`PC_GATEWAY_SEDIMENT=on`)。对 Responses 会话,网关
- **网关:** 设置 `PC_GATEWAY_SEDIMENT=on` 开启沉淀;沉淀仍默认关闭。对 Responses 会话,网关
在内存中保留每个回合终止响应对应的、实际发出的请求及其响应输出,最多 30 分钟、最多 64 个回合、总计最多 64 MiB,
超出时先丢弃最早的,抽取前已被丢弃的回合走普通抽取;并在沉淀的 Source
元数据中记录一个不透明的句柄,以及该终止响应的输入和输出 token 数。请求和响应本身从不进入 Source、证据或日志。
Expand All @@ -245,6 +247,8 @@ Rust Gateway 在启动时通过 `/v1/capabilities` 查询一次 `context_protect
将 `POWERCONTEXT_SERVER_INFERENCE_PREFIX_EXTRACTION_GATEWAY_HEADERS` 设为
`{"Authorization": "Bearer <PC_GATEWAY_API_KEY>"}`。仍需配置生成模型,因为普通抽取是回退路径。

配置 `POWERCONTEXT_SERVER_INFERENCE_PREFIX_EXTRACTION_GATEWAY_URL` 后,Server 在没有显式指定处理模式时默认使用 `one_pass` 整理 Atomic Memory。设置 `POWERCONTEXT_SERVER_RUNTIME_ATOMIC_MEMORY_PROCESSING_MODE=per_candidate` 可保留逐候选处理,设为 `one_pass` 可显式选择单次整理。未配置回连地址的 Server 和直接使用 SDK 的配置仍默认使用 `per_candidate`。网关不会替独立启动的 Server 自动发现或配置这个地址。

命中缓存的输入并非免费,服务商仍按较低的价格计费。处理窗口恰好只含一个这样的回合时,Server 先估算两种请求的成本:
前缀请求为 Core 的指令加上终止响应的输出 token(按全价),再加上命中缓存的输入 token 乘以
`POWERCONTEXT_SERVER_INFERENCE_PREFIX_EXTRACTION_CACHED_INPUT_RATIO`(默认 `0.1`);普通抽取为其指令加上 Source
Expand Down
6 changes: 5 additions & 1 deletion gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ Use `X-PowerContext-Conversation-ID` to identify a conversation. The Gateway als

Set `PC_GATEWAY_SEDIMENT=on` to capture completed user turns as Sources for later extraction. Capture excludes injected context and provider reasoning ciphertext. Tool continuations remain open until the final response. Long turns are split into Sources, and Core stores all parts in one batch. With a trusted conversation ID, captures share a turn digest and include a digest of the ordered normalized Source parts in their Source IDs. Distinct terminal records are stored independently; identical normalized parts are idempotent even when provider response IDs or usage change. Existing Sources remain unchanged, and a repeated capture under the former turn-only ID creates a new Source. Capture requires a Core that supports batch capture. Capture failure does not prevent response delivery. Blocked and degraded turns are not captured.

For a Responses upstream, `PC_GATEWAY_PREFIX_EXTRACTION=on` lets Core reuse the provider's cached prefix for memory extraction. With a subscription this applies to `codex` only, on a Core confirmed as non-enforcing; the extraction requests use the subscription account. Traffic of the other subscriptions uses normal extraction. See [prefix extraction settings](../docs/en/docs/operate/deploy-server.md#memory-extraction-over-the-cached-prefix).
For a supported Responses upstream, prefix extraction is enabled by default; `PC_GATEWAY_PREFIX_EXTRACTION=off` disables it. With a subscription this applies to `codex` only, on a Core confirmed as non-enforcing; the extraction requests use the subscription account. Traffic of the other subscriptions uses normal extraction. Capture remains opt-in. Core also needs its gateway callback URL and generation model configured. With that URL configured, the Server defaults to `one_pass` unless a processing mode was explicitly supplied; `POWERCONTEXT_SERVER_RUNTIME_ATOMIC_MEMORY_PROCESSING_MODE=per_candidate` retains the previous mode. Both processing modes can reuse an eligible agent prefix when estimated cheaper, with normal extraction as the fallback. See [prefix extraction settings](../docs/en/docs/operate/deploy-server.md#memory-extraction-over-the-cached-prefix).

## Steering

Responses requests include the [built-in cost guidance](crates/pc-gateway-app/src/default_steering.txt) by default. Set `PC_GATEWAY_STEERING_FILE` to replace it, or `PC_GATEWAY_STEERING=off` to disable steering even when a file is configured. Chat requests are unchanged. Both `PC_GATEWAY_STEERING` and `PC_GATEWAY_PREFIX_EXTRACTION` accept only `on` or `off` when set. See [instruction settings](../docs/en/docs/operate/deploy-server.md#gateway-instruction).

## Run

Expand Down
Loading
Loading