Skip to content

[pi] Direct connection fallback after proxy disconnect: no oversized rejection, no self-healing, no visible alerting (al | [pi] 代理失联后的直连兜底无 oversized 拒绝、无自愈、无可见告警(opencode/dsh 都有)—— 1.06M token 原始历史被直接送进上游 #4516

Closed

Description

@yanhuojiyusheng

(ai: custom/group/sol)

摘要

pi 车道的代理一旦没就绪,插件会把模型流量原样直连到真实上游:不压缩、不注册会话、不打插件头。这条兜底路径上没有任何尺寸闸门——1,063,850 token 的原始历史(窗口 500,000)被直接发给上游,整轮报废。同类护栏在兄弟车道都存在,只有 pi 缺失:

车道 oversized 本地拒绝 失效后自愈 可见告警
opencode ✅ oversizeDirectRefusal(src/agent/opencode-native.ts:120-146,ceil(bytes/4) > window → 本地拒绝) ✅ createLiveOriginResolver 部分
dsh ✅ takeoverGate 返回 refuse-oversized(#2924) ✅ 10s 定时器(dsh-native.ts:1067) ✅ 唯一上报 attribution(dsh-native.ts:1829-1839)
pi ❌ 无 ❌ 无 ❌ 无(只有一行 bili.log)

环境

billion-context 0.1.204(pi 原生插件)/ 上游 OpenAI 兼容网关(窗口 500,000)/ WSL2 / storage.unified=full。

现场

09:11:35.894Z [error] [native-client] bili-native: proxy bootstrap failed — model traffic goes direct (uncompressed):
  bili: proxy did not become healthy within 60000ms — nothing is listening on port 18803 —
  the spawned child (still alive, pid 475393) never bound it
09:11:35.895Z [error] [native-client] bili-native: proxy not ready — model request goes direct (uncompressed):
  http://<gateway>/v1/chat/completions
09:13:34.077Z  pi 会话最后一行:stopReason="error",
  errorMessage="prompt is too long: about 1063850 tokens, more than gpt-6-luna's context window of 500000"

关键点:bili.log 里这段时间完全没这个会话的请求记录(直连不过 bili)。同一会话晚些时候(08:44)经 bili 时:request: 2052 msgs, inbound=50.5MiB, outbound=34.3MiB、[acp-usage] input=214638 cached=214272——压缩本身是成功的(215K/500K,缓存命中 100%)。所以这不是“压不住”,而是“没走压缩”。

该会话的 raw 量级:文本 246.9 万字符(CJK 21.5 万)+ 48 张截图共 48.3MB base64(占 52.7MB 会话文件的 92%)。只要 bili 不在路径上,它就会 502——而该会话在客户端自己不会缩小(bili 的保护全在 wire 层)。

三处代码缺口

  1. 直连兜底不看大小:src/agent/native-intercept.ts:1217-1226,readyOrigin() === undefined 时直接 directSendOrRefuse();该函数(980-1005)只做 When the proxy goes down, incremental rounds fall back to direct connection, sending the incremental payload as-is to upstream and causing a 400 (same mechanism across opencode/omp/dsh-native). | 代理挂掉时增量轮次降级直连会把增量载荷原样发给上游导致 400(opencode/omp/dsh-native 同一… #3304 的 payload 还原,没有任何尺寸判断。bodyByteSizeOf()(447-455)已经在手上,ceil(bytes/4) > window 一算就能拒绝(本侧 50.5MiB → 13M token 地板,必然拒绝)。
  2. pi 车道从不注册 takeoverGate:全仓只有 src/agent/dsh-native.ts:1983 赋值它,所以 [dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 / The input (6165048 tokens) is longer than the model's context length (1048580 tokens(todo fix)). #4068 两套 refuse-oversized 机制在 pi 根本不执行(native-intercept.ts:1179-1214 整个分支被跳过)。
  3. 无自愈 + 无告警:src/agent/pi-native.ts 全文无 setInterval;state.respawn 只被 recover()(需先有活 origin,native-intercept.ts:951-957)与 createLiveOriginResolver(仅 opencode 用)调用 → 启动期失败 = 整个宿主进程生命周期直连,只有重启宿主能恢复。pi-native.ts:196-197 的文案却写着“bili keeps re-checking and self-heals”,无调用点支撑。attribution 上报入口(src/server/admin.ts:734-740)只有 dsh 一个上报者,且没有活 proxy 就没有面板可渲染 banner(src/web/client.ts:1063-1071)。

后果

单个会话一旦落入这个状态:每重试都 502(历史不变),用户侧只能看到 pi 的报错;bili.log 无记录、无面板、无 banner。用户很可能以为是“压缩失效/上下文没管住”,而真实原因是“那一轮根本没进 bili”。

建议

  1. 把 oversizeDirectRefusal 上提到 native-intercept.ts 的直连兜底(车道只提供 window),覆盖 pi/omp 等所有无 gate 车道;lastUnavailableReason 带上“为何不可用”。
  2. pi 车道接 createLiveOriginResolver 或加退避 respawn 定时器;修正 pi-native.ts:196-197 与实际不符的文案。
  3. 直连/拒绕状态要能被用户看到:至少 logHostEvent("error") 每次切换都报(现在 warned 只报一次),并让 pi/opencode/omp 也上报 attribution。

复现

在有 1M+ token 历史的 pi 会话上,让 spawn 失败(本机是因为冷启动超出 60s 预算,见 #4515;也可以直接占住 lane 端口),然后发一轮请求 → 直连 → 上游报 prompt is too long。

相关

Activity

  1. changed the title [-][pi] 代理失联后的直连兜底无 oversized 拒绝、无自愈、无可见告警(opencode/dsh 都有)—— 1.06M token 原始历史被直接送进上游[/-] [+][pi] Direct connection fallback after proxy disconnect: no oversized rejection, no self-healing, no visible alerting (al | [pi] 代理失联后的直连兜底无 oversized 拒绝、无自愈、无可见告警(opencode/dsh 都有)—— 1.06M token 原始历史被直接送进上游[/+] on Oct 24, 2026
  2. ranxianglei commented on Oct 24, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    查重:不重复。 #4125 是同形后果但车道不同(dsh,根因是端口被旧实例占住,且 dsh 已有全套护栏);#4515 是本次 spawn 失败的根因(冷启动扫描超预算,另一机制);#2924/#3308/#4068 是护栏先例而非同一缺陷。本条独立成立:pi 车道的直连兜底路径缺兄弟车道都有的三件套。

    标签:bug / pi / scope/wiring / p2(条件触发才致命:代理失联 + 历史超窗时整会话报废;小会话直连仍可用、重启宿主可恢复,未到「核心能力整体停摆」)。

    审核结论(已对 master aeea173a 逐条查证,三处缺口全部属实):

    1. 直连兜底无尺寸闸门 ✅ — src/agent/native-intercept.ts 的 directSendOrRefuse() 只做 When the proxy goes down, incremental rounds fall back to direct connection, sending the incremental payload as-is to upstream and causing a 400 (same mechanism across opencode/omp/dsh-native). | 代理挂掉时增量轮次降级直连会把增量载荷原样发给上游导致 400(opencode/omp/dsh-native 同一… #3304 的 payload 还原,无任何尺寸判断;readyOrigin() === undefined 分支直接放行。本侧 50.5MiB 请求 → 地板估算 ~13M token > 500K 窗口,一算就该拒。
    2. pi 从不注册 takeoverGate ✅ — 全仓仅 dsh-native.ts:1983 赋值它,[dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924/The input (6165048 tokens) is longer than the model's context length (1048580 tokens(todo fix)). #4068 两套 refuse-oversized 分支在 pi 被整体跳过。
    3. 无自愈 ✅ — state.respawn 只被两处调用:recover()(需先有活 origin 且发送失败)和 createLiveOriginResolver(仅 opencode V2 route 用)。启动期 bootstrap 失败后没有任何重试驱动——createLiveOriginResolver 自己的注释都写着 "the load-time bootstrap failed (the hook cannot observe send failures, so nothing else would retry)",而共享 intercept 正是这个「nothing else」。终身直连直到宿主重启成立;pi-native.ts 里 "bili keeps re-checking and self-heals when it comes back" 文案无调用点支撑。

    关于第 3 点建议里的 attribution 上报:现有契约(#3581/#3651 owner 条款「正常拦截不报告,报告必须可诊断」)的状态枚举是 ok/none/threw,语义是「gate 归因查找抛错」这类内部故障,与「proxy 不可用」不是一回事;且该 episode 期间 endpoint 本身就是死的那个进程,没有活端点可 POST。所以本 PR 先落日志层可见性(每次模式切换进 bili.log),attribution 面扩展作为决策项放在最终回复里列选项,不擅自扩 wire 契约。

    修复方案(一个 PR,同机制三件套):

    1. 尺寸闸门上提到共享直连兜底:directSendOrRefuse() 统一加「provably over window」判定——复用 DSH's unattributed over-limit gate prices base64 images as text (bytes/4) — media-heavy requests are falsely rejected wi | dsh 无归属超限门把 base64 图片当文本计价(bytes/4)——媒体重的请求被假拒 422,上游本会正常接受 #4111 的两阶段算法(bytes/4 地板粗筛 → 剔除 media 字节后重算核心文本,误拒方向零容忍),窗口取自出站请求已有的 x-bili-plugin-context-window 头(pi/opencode/dsh 车道都会打),取不到就 fail-open 保持现状。命中即本地 422 拒绝,文案说明「为何不可用 + 恢复方式」。覆盖 pi/omp/opencode-V1 等所有无 gate 车道的全部直连出口(bootstrap 失败、proxy 死亡 give-up、routed give-up、签名 passthrough opt-in)。
    2. 自愈:共享 intercept 在「origin 解析不到」分支加 cooldown(15s,复用 RESPAWN_COOLDOWN_MS 常量)驱动的 respawn 重试——请求驱动、singleFlight 去重、不叠 spawn。启动期失败不再等于终身直连。
    3. 可见性:一次性 warned 升级为模式切换日志(proxy↔direct↔refused 每次切换都报,含恢复通知);lastUnavailableReason 在共享路径也落值,区分「没起来 / 死了 / 活着但饱和」。
    4. pi-native.ts 文案:自愈落地后 "keeps re-checking and self-heals" 变为事实,核对措辞。

    行为变更声明(Deliverables 第 4 条):

    • ① 代理失联时的超窗请求:旧值「直连送上游等 prompt is too long 502」→ 新值「本地立即 422 并给出原因」(仅当窗口头存在且核心文本地板超窗;无头/未超窗的请求行为完全不变);
    • ② 启动期 bootstrap 失败:旧值「终身直连直到宿主重启」→ 新值「每请求至多 15s 一次 respawn 重试,成功后自动恢复压缩」;
    • ③ bili.log 新增模式切换行(含恢复通知)。
    • 无新配置项、无新 env、不动 kernel/。

    开始实现,完成后开 PR 回贴。

  3. yanhuojiyusheng commented on Oct 24, 2026

    @yanhuojiyusheng
    ContributorAuthor

    (ai: custom/group/sol)

    补充三点,都对着 master 的代码核实过,和一个现场数据点(0.1.206 / WSL2)。

    1. readyOrigin() 的 15s 上限会把首请求打成 422,和上面的修复方案有冲突

    src/agent/native-intercept.ts:513:

    return withTimeout(state.ready, state.readyTimeoutMs ?? 15000);

    state.readyTimeoutMs 全仓没有赋值点 → 恒 15s,而 SPAWN_BUDGET_MS = 60000(src/launcher.ts:179)。

    按上面 planned fix 第 2 条(cooldown 15s 驱动的 respawn 重试)推演,冷启动 34s 时:

    t 行为 结果
    0 R1 触发 respawn,等 15s undefined → 尺寸闸门 → 422
    15s R2 再触发,singleFlight(src/agent/native-bootstrap.ts:137-147)返回同一个 pending promise,再等 15s 422
    ~34-40s spawn 落成,bootstrap() 写 state.origin 恢复

    即"成功后自动恢复压缩"成立,但重启后前 1-2 轮必然 422,用户体感还是"又坏了"。建议:存在 in-flight spawn 时把等待上限放宽到 SPAWN_BUDGET_MS(或首轮免 cooldown),否则 #4515 把 boot 提速之后,冷启动那一次仍会被误拒。

    2. 回答 #4515 里标注的不确定项:那次阻塞不在 persist.boot()

    #4515 的 triage 写「34s 的大头可能落在 index.db 的打开/WAL 恢复阶段…决定根治要覆盖哪一段」。

    本机 09:10 那次失败是反证数据点:那个子进程在 60 秒里一行日志都没打——/tmp/bili-proxy-18803.log(logger 生效前的输出落这里,src/launcher.ts:4195)和 bili.log 里都没有 [storage] unified FULL mode ENABLED。而该行打在 index.db 打开成功之后、store.boot() 之前。

    所以那次阻塞确定在 index.db 打开阶段或更早,persist.boot() 的文件扫描不是阻塞点。

    旁证:同一 store 在 09:17:24 启动,[storage] → listening 只有 0.66s;对照 08:50:13 的 shutdown grace window elapsed; forcing exit (liveConns=1)(SIGKILL 抢在 flush 完成前),当时 index.db-wal 4.2MB 未 checkpoint。只修 persist.boot 可能治不到这次冷启动。

    3. 止血路径(与修复无关,但能立刻停损)

    parent-gone 让每次重启 pi 都要重赌一次冷启动,先把 proxy 移出 pi 的生命周期:

    bili start --host 127.0.0.1 --port 18803
    export BILLION_CONTEXT_ATTACH=http://127.0.0.1:18803

    attach 走 verifyAttachAndRecover(src/agent/pi-native.ts:170-205),不碰 spawn 预算。现场验证:11:23 恢复时 18803 的 pid 仍是 604269(startedAt 10:56:34),没有新起 proxy,是 attach 挂上去的。

    现场数据

    • 同一会话:经 bili input=214638 / 直连 1063850(窗口 500000)
    • sessions/ 4.0G(index.db 3.18G + retrieve/ 800M)
    • 冷启动失败 2 次:09:11:35、09:52:19,各耗满 60s 预算
  4. ranxianglei commented on Oct 24, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    三点都收到,第 1 点已按你的方向实现,第 2 点已作为数据点补进 #4515。修复已提交:PR #4546(Closes #4516)。

    对你三点的回应

    1. readyOrigin() 15s 上限冲突 —— 采纳,用「等满 in-flight spawn」而非「首轮免 cooldown」。新机制:请求发现无活 origin 时,在 15s cooldown 下触发 owner 的 singleFlight respawn(首轮必然触发,lastRespawnAttempt=0),然后对同一个 in-flight attempt 的等待上限改为 state.spawnWaitMs ?? readyTimeoutMs ?? 15_000;pi spawn 车道设 SPAWN_BUDGET_MS + 10s(70s)。对照你推演的表:t=0 R1 触发并等待,~34s spawn 落成 → 该轮直接走 bili,不再 422;并发请求共享同一次 attempt(singleFlight),不会叠 spawn。选「长等待」而不是「首轮免 cooldown」是因为它同时覆盖了你表里 t=15s 那一行——cooldown 到期后的重试同样会等满 in-flight attempt,而不是再撞一次短上限。回归测试钉住了这个形状(readiness cap 50ms / spawn budget 300ms / spawn 在 120ms 落成 → 路由成功、零直连)。

    2. #4515 的不确定项 —— 已在 #4515 留带来源标记的评论。你的零日志数据点(两次失败 60s 内一行没打 → 阻塞确定在 index.db 打开或更早)+ 脏 WAL 旁证(08:50 SIGKILL 留 4.2MB 未 checkpoint / 09:17 WAL 干净时启动仅 0.66s)正好回应了那边 floor 2 标注的「34s 大头可能落在 index.db 打开/WAL 恢复」,并提示只修 persist.boot() 治不到这类冷启动。

    3. 止血路径 —— 确认成立。attach 模式走 verifyAttachAndRecover,不碰 spawn 预算;且本 PR 的 cooldown 重试对 attach 车道同样生效(attach 失败后每 15s 自动重探挂接),比纯 attach 又多一层自愈。

    PR 内容摘要

    三处缺口全部落进共享 intercept(src/agent/native-intercept.ts),所有无 gate 车道(pi/omp/opencode-V1)一次补齐:

    行为变更(旧→新,PR 描述里有完整表格):① 无 gate 车道可证超窗的降级发送从「直发必死」变「本地 422」;② bootstrap 失败从「直连到宿主重启」变「每请求 15s cooldown 重试」;③ pi spawn 车道就绪等待从通用 15s 上限延长到 70s(慢冷启动被等待,不被误拒)。#4079 地板策略原样保留(本地 bar 永不假设 <1M 窗口,sub-1M 声明窗口照打但不生效)。

    验证:新套件 tests/issue4516-pi-direct-fallback.test.ts 11/11;native-intercept-selfheal/native-intercept/dsh-native/pi-native 136/136;其余 14 个 intercept 相关套件 241/241;typecheck 干净。全量以 CI 为准。

    八问

    1. 是什么问题:pi 等无 gate 车道在代理失联后,降级直连路径既不看尺寸也不重试代理也不可见——超窗原始历史直接送上游整轮报废,且只能靠一行 warn 反推。
    2. 什么原因导致的:三条护栏(oversizeDirectRefusal/takeoverGate/resolver+定时器/attribution)当初都是按车道分别建在 opencode/dsh 内部的,共享 intercept 的 origin-undefined 分支天生没有尺寸判断;respawn 只在「已持有 origin 且发送失败」(recover)或「opencode 专属 resolver」时被驱动,启动期失败没有任何驱动点;日志只有一个 one-shot warn。
    3. 影响范围与严重程度:所有无 takeoverGate 的 native 车道(pi/omp/opencode-V1);触发条件 = 代理 bootstrap 失败或死亡 + 会话体量可证超窗。严重度:该轮起每重试必 502、用户侧只见上游报错、bili.log/面板均无记录——「能用但体验毁灭到怀疑压缩失效」级;能装下窗口的会话仍是优雅降级(直连不压缩),行为不变。定 p2。
    4. 怎么修复的:把三条契约上提到共享层一次性覆盖所有车道——直发前统一尺寸判决(复用 dsh 已验证的 doom math,不另造一套)、请求驱动的 singleFlight respawn 重试(cooldown 限频 + owner 预算内等待 in-flight attempt)、状态切换级日志 + 原因随拒绝文案下发。
    5. 是否有回归问题:逐项审过既有行为——装得下/未知数据仍走直连(fail-open,测试钉住);dsh gate 行为字节等价(签名不变、44 测试绿);死亡→recover 自愈路径绿(selfheal 套件);one-shot warn 换成切换级日志是超集;Context Window Overflow Issue Summary: Raw Requests Sent Directly to Upstream Rejected + Local Intercept Cooldown Lockin | 上下文超窗问题汇总:请求裸冲上游被拒 + 本地拦截冷却锁死会话(统一分析入口) #4079 地板未漂移。有意变更仅上述 ①②③ 三条,均已披露。
    6. 是否破坏缓存:不涉及——不动前缀缓存/存储/游标/去重键;直连路径本就不碰账本。
    7. 是否改了设计方向:没有转向——是把「车道私有的护栏」归并进共享基础设施(与 When the proxy goes down, incremental rounds fall back to direct connection, sending the incremental payload as-is to upstream and causing a 400 (same mechanism across opencode/omp/dsh-native). | 代理挂掉时增量轮次降级直连会把增量载荷原样发给上游导致 400(opencode/omp/dsh-native 同一… #3304/[dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 的上提方向一致)。attribution 面板扩展到 pi/opencode/omp 仍作为待决策项单独留,未塞进本 PR。
    8. 是否引入新配置:无。spawnWaitMs 是 lane 内部 state 字段(pi = SPAWN_BUDGET_MS+10s),无新 env/config 面。

    判定:可合——待 CI 在 pushed head 上跑全量确认。合并后 #4515 侧的分阶段计时落地时,可用真实冷启动顺带验证本条的「慢启动被等待」路径。

  5. added 3 commits that reference this issue on Oct 24, 2026
  6. added 6 commits that reference this issue on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions