Skip to content

fix: resolve compaction ownership for pi virtual-model selections (#1961) - #1962

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-03_pi-virtual-compaction-ownership
Oct 3, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-03_pi-virtual-compaction-ownership

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1961

Problem

pi 0.99 added virtual models (pi.registerVirtualModel): the selected model (e.g. router/auto, api: "pi-virtual") routes to a physical model per request and has an empty baseUrl. The plugin's compaction ownership check (ownsCompaction) resolved the proxy base from ctx.model.baseUrl -> detectProxyBase(""). When the process env does not carry BILLION_CONTEXT_PROXY (manual /bili/ baseUrl wrapping in models.json, or any setup where the routing evidence lives in the model catalog rather than env), that returns undefined, session_before_compact is not cancelled, pi's native compaction stays enabled, while the physical requests still go through the proxy -> double compression of one session (the failure the README warns about).

Verified against pi v0.99.1 source (packages/coding-agent/src/core/virtual-models.ts): virtual entries hardcode baseUrl: ""; "Everything below the routing step only sees physical models ... A virtual model never reaches a provider"; assistant messages record the physical provider/model.

Fix

For a virtual selection only (ctx.model.api === "pi-virtual"), resolve the proxy base from where the conversation's traffic actually went, in priority order:

  1. Latest non-failed assistant response on the session branch (mirrors pi's own findLatestResponse: skips stopReason error/aborted) -> its physical model's registry entry (ctx.modelRegistry.getModel(provider, modelId)?.baseUrl). In launcher mode the registry holds the rewritten <origin>/bili/<upstream> URL; in manual wrapping it holds the user's wrapped URL - both carry the proxy origin exactly.
  2. Launcher manifest BILI_PROVIDER_REWRITES[provider] for that provider (covers httpsRewrites lanes where the registry URL is raw upstream).
  3. Existing env fallback (detectProxyBase(undefined)) - status quo.

Every candidate passes through detectProxyBase, so the BILLION_CONTEXT_PLUGIN=0 kill switch and URL validation apply uniformly; any shape mismatch degrades to today's behavior. The event's own branchEntries is preferred over re-querying sessionManager.getBranch().

All downstream carriage gates are untouched (#1382 carriedSids/fetchStatus probe, #1392 opaque-scheme veto): this changes which proxy is asked, never whether we own - probe failure still defers to native compaction.

The same resolution automatically benefits before_provider_headers (carriedSids stamping now works for virtual sessions without env) and registerTools; the omp-lane call sites are unaffected (omp predates virtual models; the guard makes it moot).

Behavior change disclosure (old -> new)

Case Old New
Virtual selection, no BILLION_CONTEXT_PROXY env, physical route resolvable (registry/manifest) ownership never claimed -> double compression proxy resolved from physical route; carriage gates decide as before
Virtual selection, env set (standard launcher/native modes) resolved via env resolved via physical route first (same origin in standard modes); env remains final fallback
Non-virtual selections unchanged unchanged (byte-identical code path)
BILLION_CONTEXT_PLUGIN=0 kill kill (test case I)

Config surface

No new config fields/env vars/CLI flags. Only reads existing ones: BILLION_CONTEXT_PROXY (fallback, unchanged semantics), BILI_PROVIDER_REWRITES (launcher-injected manifest, already parsed by parseProviderRewrites), BILLION_CONTEXT_PLUGIN (kill switch, now applied uniformly to all candidates via detectProxyBase). No migration needed.

Tests

New #1961 test in tests/plugin-agent.test.ts (9 cases): repro (cancel without env via registry), carriage gate preserved against unknown proxy, manifest + getBranch() fallback, raw-upstream -> no claim, failed-route skipping, empty-branch env fallback intact, full stamp flow (carriedSids without env), non-virtual regression guards (both directions), kill switch beats candidates. Verified the test fails on the unfixed code (stash -> not ok -> pop).

typecheck clean - tests/plugin-agent.test.ts 63/63 - build OK - rebased onto current master

)

pi 0.99+ virtual models (router/auto, api "pi-virtual") carry an empty
baseUrl — the selection names a router, not a destination — so
detectProxyBase(ctx.model.baseUrl) had nothing to read and ownsCompaction
bailed, leaving pi's native compaction enabled while the physical requests
still reached the proxy: double compression.

Resolve the proxy from where the traffic actually went instead: the latest
non-failed assistant response's registry baseUrl (launcher httpRewrites and
manually /bili/-wrapped models.json both land there), then the launcher's
BILI_PROVIDER_REWRITES manifest entry for that provider, then the plain env
fallback. Every candidate passes through detectProxyBase so the kill switch
and URL validation apply uniformly; any shape mismatch degrades to today's
behavior. The downstream carriage gates (#1382/#1392) are untouched — this
only fixes WHICH proxy is asked, never WHETHER we own.

Closes #1961
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-03_pi-virtual-compaction-ownership (bad516d)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1975-pr.1962.106fd77

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1962.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始 review PR #1962(pi virtual-model compaction ownership)。初步状态:

  • 分支已拉取核对:head de698649,0 commits behind master,mergeable,diff 干净(src/agent/pi.ts +70/−8、tests/plugin-agent.test.ts +126,无跑题文件)。
  • CI:pi/codex/opencode/dsh 真实 lane e2e、hermetic registry e2e (ubuntu)、build-artifact、version-guard 已绿;4 个 test job(ubuntu/windows × 22/24)+ windows registry e2e 还在跑。
  • 正在本地做定向验证(typecheck + tests/plugin-agent.test.ts + build)并核对既有行为面。结论稍后按七问给出。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 结论:可合(CI 全绿 + 本地定向验证 + 测试牙齿检查通过)。合并由人执行。

结构化七问

  1. 是什么问题:pi 0.99+ 的虚拟模型(router/auto,api: "pi-virtual")硬编码空 baseUrl。当进程环境没有 BILLION_CONTEXT_PROXY(手动 /bili/ wrapping 场景)时,插件的 compaction ownership 解析不到 proxy origin → 不取消 pi 原生压缩,而物理流量实际仍走 bili → 双重压缩。
  2. 什么原因导致的:proxy base 解析绑定在 ctx.model.baseUrl 上——虚拟模型该字段恒为空字符串,是结构性盲区(不是用户漏配)。detectProxyBase("") 返回 undefined,ownership 直接放弃。
  3. 怎么修复的:仅当 api === "pi-virtual" 时改从「流量实际去向」三级回退解析:分支上最近一条非失败 assistant 响应(镜像 findLatestResponse,跳过 error/aborted)→ 其物理模型的 registry 条目 baseUrl → launcher manifest BILI_PROVIDER_REWRITES → 原 env 兜底。每个候选都过 detectProxyBase,kill switch(BILLION_CONTEXT_PLUGIN=0)与 URL 校验统一生效;事件自带 branchEntries 优先于 getBranch()。下游 carriage 闸门(Pi plugin cancels Pi's auto-compaction for conversations the proxy never sees (breaks pi-claude-bridge) #1382/[investigation] Non-http baseUrl providers (pi-claude-bridge etc.): an opt-in path to ride bili — beyond the #1383 stopgap veto #1392)未动。非虚拟路径 byte-identical。
  4. 是否有回归问题:逐项审过既有行为面——非虚拟选择(用例 H 双向钉住)、omp lane(call site 不变,omp 无虚拟模型)、kill switch(用例 I)、resolved-but-uncarried 否决(用例 B,carriage 闸门保留)、raw upstream 直连不误 claim(用例 D)、失败尾部跳过(用例 E)、空分支(用例 F 双向)、完整 stamp 流程(用例 G)。唯一行为变化就是 PR 披露的两条(virtual+无 env:从「永不 claim」→「按闸门 claim」;virtual+有 env:物理路由先于 env),与代码核对一致。未发现意外回归。
  5. 是否破坏缓存:不涉及。会话状态、持久化格式、去重键均未变;改动只影响 compaction ownership 判定,不触碰 wire 形状。
  6. 是否改了设计方向:不涉及。在既有 proxy-base 解析上扩展了模式专属回退链,架构/边界/数据流未转向。
  7. 是否引入新配置:不涉及。只读既有 BILLION_CONTEXT_PROXY / BILI_PROVIDER_REWRITES / BILLION_CONTEXT_PLUGIN,无新字段/env/flag。

验证证据

  • head de698649,0 commits behind master,diff 干净:src/agent/pi.ts +70/−8、tests/plugin-agent.test.ts +126,无跑题文件。
  • 本地(head 上):typecheck 干净;tests/plugin-agent.test.ts 63/63(含新增 pi-native: compaction ownership not detected when the selected pi model is a virtual model → double compression #1961 九例);build OK(dist-import-annotations OK)。
  • 牙齿检查:把 src/agent/pi.ts 单独回退到 base f2b18022 后跑新测试 → AssertionError 失败(真实行为断言,非恒真/非 import 错误),恢复后全绿。
  • call-site 核对:before_provider_headers(pi.ts:750)与 registerTools(pi.ts:330)经同一 proxyBaseForCtx 自动受益(虚拟会话此前同样 stamp 不了 header、只能 wire mode),用例 G 钉住;omp runtime-info 路径(pi.ts:818)不受影响;parseProviderRewrites 为 pi.ts:294 既有本地函数,manifest 值本身已过 https?:// 校验。
  • CI 14/14 绿:real pi/codex/opencode/dsh package-native lane、codex MCP overlay、codex e2e、hermetic registry e2e(ubuntu + windows)、build-artifact、version-guard、4 平台 test 矩阵全部 success。

review 未发现需顺手修的小问题,也未发现需另行立项的新缺陷。判定:可合,请人工合并 #1962 。

…ot exist on the host (#1961)

pi v0.99.1 ModelRegistry exposes find(provider, modelId) (delegating to
the private runtime.getModel); there is no public getModel, so
ctx.modelRegistry?.getModel?.(...) always short-circuited to undefined —
tier ① (the only tier that can resolve ownership in the issue's own
lane: manual /bili/ wrapping, no env, no rewrite manifest) was dead code
on the real host. Tests passed because the stubs invented the same
non-existent method; they are now shaped like the real registry surface,
and a mutation check confirms the #1961 repro test fails against the old
call. Same failure class as #1943: object-literal stubs cannot catch
host-API drift.
@ranxianglei

Copy link
Copy Markdown
Owner Author

评审发现并追加修复(bad516d):tier ① 调用了宿主上不存在的方法,原提交里它是死代码。

缺陷:原提交 ctx.modelRegistry?.getModel?.(provider, modelId) —— 对照 pi v0.99.1 源码逐行核实,class ModelRegistry(packages/coding-agent/src/core/model-registry.ts)没有公开的 getModel,公开查询面是 find(provider, modelId)(内部委托 this.runtime.getModel,runtime 是私有的)。可选链对不存在的方法静默返回 undefined,所以 registry 层在真实宿主上从不命中。

影响:三层层级里 ②(BILI_PROVIDER_REWRITES env)③(BILLION_CONTEXT_PROXY env)在 launcher/attach 模式都在,没事;但 #1961 报告者自己的场景(models.json 手动 /bili/ 包装、裸 pi、无任何 env)只有 tier ① 能解析——它是死的,issue 的复现路径原样保留。其余 4 个 call site(pi.ts:276/696/764/820)宣称的无 env carriedSids 收益同样经过这个死层。

为什么 CI 是绿的:测试桩写的是 modelRegistry: { getModel: ... } —— 按补丁自己的假设塑形的对象字面量。桩和代码"错得一致",所以全过。和 #1943(detached this 骗过 #955/#956/#1812 对象桩)同一类失败。

修复(bad516d):

  • pi.ts:getModel?.( → find?.(,Ctx 结构类型同步,Ctx 注释记录真实 API 出处;
  • 测试桩改为 { find: ... }(真实 ModelRegistry 形状),加注释说明为什么必须按宿主面塑形;
  • 变异验证:把代码改回 getModel 而 stub 保持 find 形状 → #1961: virtual-model selection resolves compaction ownership from the physical route 立即红。测试现在真的看住宿主 API 面。

验证:typecheck ✔ build ✔ plugin-agent.test.ts 61/63(2 个失败在未含本修复的原提交 de69864 上同样失败、且与本次改动无关——沙箱网络边界所致,GitHub CI 此前对该 head 14/14 绿)。其余设计(branchEntries/reason 载荷、AssistantMessage 字段、StopReason、Model.baseUrl、getBranch 白名单、carriage 门不动)均已对照 pi 0.99.1 源码核实无误。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 已核对追加修复 bad516db 并重新验证:可合(CI 全绿 + 本地复验 + 变异检查通过)。合并由人执行。

先认账:我上一轮 review 放过了这个死层——只核对了代码内部一致性,没有对照 pi 宿主的真实 API 面,而测试桩恰好按补丁自己的假设塑形({ getModel }),「错得一致」全过。与 #1943 同类失败,这轮教训已吸收。

核对结果(bad516d = de69864 + 1 commit)

  • delta 最小且精确:pi.ts Ctx 类型(:22)+ 调用点(:105)getModel?.( → find?.(,注释记录真实出处(pi v0.99.1 model-registry.ts,find(provider, modelId),无公开 getModel);测试两个桩同步改为 { find: ... } 并加注释。全仓 modelRegistry 仅这两处引用,无遗漏点。无跑题改动。
  • 本地复验(head 上):typecheck 干净;tests/plugin-agent.test.ts 63/63(你提到的 2 个失败在我这边未复现,即沙箱网络边界所致,与本改动无关)。
  • 变异验证(独立重做):把调用点改坏、桩保持 { find } 形状 → #1961 测试立即红,恢复后全绿。测试现在真的看住宿主 API 面,死层回归会被抓住。
  • 诚实边界:本沙箱没有 pi 源码副本,find 为真实公开面这一点以你的逐行核实为准,佐证是 CI 的 real pi package-native lane(真 pi 二进制对确定性 fake upstream)在新 head 上绿。
  • CI 14/14 绿(bad516d):含 real pi/codex/opencode/dsh lane、codex e2e、hermetic registry e2e(ubuntu+windows)、4 平台 test 矩阵、build-artifact、version-guard。
  • PR 状态:mergeable=True;master 期间并入 fix(preflight): calibrate local estimates against provider usage, provenance-gate the baseline, scale the fold budget (#1933) #1940(preflight 校准,触碰 src/preflight.ts/server.ts/session.ts 等),与本 PR 文件零重叠,无语义冲突,BEHIND 不阻塞合并。

七问(增量部分)

  1. 是什么问题:原提交 tier ① 调用宿主上不存在的 getModel,可选链静默 undefined → registry 层在真实宿主上是死代码,pi-native: compaction ownership not detected when the selected pi model is a virtual model → double compression #1961 报告者自己的场景(裸 pi + 手动 /bili/ 包装 + 无 env)只有这一层能解析,复现路径原样保留。
  2. 什么原因导致的:测试桩按补丁假设塑形而非按宿主面塑形——桩和代码错得一致,CI 无法暴露;与 fix(dsh): bind llm.resolveModelInfo to its service receiver — every window resolve throws, stripping x-bili-plugin-context-window #1943(detached this 骗对象桩)同一失败类。
  3. 怎么修复的:调用面从臆造的 getModel 换成宿主真实的 find(provider, modelId),Ctx 结构类型同步,桩按宿主面重铸并注释钉死出处;其余设计(branchEntries/reason 载荷、carriage 门、kill switch)不动。
  4. 是否有回归问题:相对 de69864 仅方法名一处语义变化;非虚拟路径、omp lane、kill switch、carriage 否决均不受影响(用例 H/I/B 仍钉住)。对 master 新增(fix(preflight): calibrate local estimates against provider usage, provenance-gate the baseline, scale the fold budget (#1933) #1940)无文件重叠。
  5. 是否破坏缓存:不涉及(同前轮)。
  6. 是否改了设计方向:不涉及——只是让已设计的回退链第一层真正生效。
  7. 是否引入新配置:不涉及(同前轮)。

判定:可合,请人工合并 #1962 。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pi-native: compaction ownership not detected when the selected pi model is a virtual model → double compression

1 participant