Skip to content

observability: 从累计 cache 命中率升级到逐 turn 的 prompt-cache 失效诊断 #156

Open

Description

@tt-a1i

一句话结论

OpenPI Footer 当前显示整条 branch 的累计 cache 百分比;这个数字能看效率,却很难回答“哪一次模型/工具/system/context 变化打断了已经工作的 prefix cache”。

OMP 已实现一个很窄的 warm → cold detector。建议先在 OpenPI 做 provider-aware 的逐 turn 诊断研究,验证误报率后再决定是否提供 opt-in marker;不要把所有 cacheRead=0 都叫 cache miss。

固定证据

对比固定在:

  • OpenPI main@2a69d3f32994da4123f1312b7fa84ef3d6119be1
  • OMP main@7623b960540518bb1291808bbae28332065e9dba

OMP 当前:

  • 只有上一轮实际读回至少 2048 cache tokens、当前轮 cacheRead=0、并且显式 prefix cache 发生 rewrite 时才判定 invalidation:detector contract
  • 它刻意排除首轮、tiny context、连续冷轮,以及 OpenAI/Google 等 implicit best-effort cache 的传播噪声,避免制造假结论:provider boundary
  • marker 默认关闭:setting

OpenPI 当前:

  • model-info 遍历 active branch,把 cacheRead / (input + cacheRead + cacheWrite) 聚合成一个百分比:aggregate metric
  • Footer 只渲染 cache N%,没有 turn boundary 或突变信息:footer projection

为什么值得讨论

OpenPI 的核心优势之一是 ordinary turn 的静态面小、工具渐进披露。要持续证明这个优势,需要能定位真实 cache 回退发生在哪个 turn,而不是只看 Session 结束后的平均数。

这可以帮助验证:

  • capability group 加载是否导致 prefix 重写;
  • tool schema / prompt 文案变动是否破坏 cache;
  • compaction、branch move、model/thinking change 的成本;
  • 某次 cache 降低是可控 invalidation,还是 provider 的正常 best-effort 波动;
  • OpenPI 与 Bare Pi / OMP benchmark 的静态税和动态税分别来自哪里。

OpenPI 应保留的优势

  • 这是 operator-facing 诊断,不向模型注入纠偏策略;
  • 默认关闭,不增加普通 transcript 噪声;
  • 只解释 Pi 已提供的 authoritative usage,不发明 provider 账单事实;
  • provider 语义不清楚时显示 unknown,而不是推断“用户导致 miss”;
  • 不把 cache 优化变成硬编码 workflow。

建议研究形态

先实现纯函数 + trace report,不急着做 UI:

  1. 定义上一轮 warm、当前轮 cold、reprocessed tokens 的最小条件;
  2. 按 provider/cache 类型区分 explicit prefix cache 与 implicit best-effort cache;
  3. 对 model、thinking、active tools、compaction/branch change 记录可选的本地原因标签,但只能标作 correlation,不能冒充 provider 因果;
  4. 用真实 Session usage 回放测试误报/漏报;
  5. 只有结果可信,再加 opt-in transcript marker 或 /model-info 明细。

非目标

  • 不保证 provider cache 一定命中;
  • 不把 cacheRead=0 一律当异常;
  • 不自动切模型、改 thinking 或阻止工具加载;
  • 不把 OpenPI 变成 provider adapter;
  • 不默认显示每轮 token 详情。

完成条件

  • 列出 Pi usage 字段在各 provider 上可支持的结论和 unknown 边界
  • detector 覆盖 first turn、warm→cold、cold→cold、partial hit、TTL expiry、compaction、model/tool change
  • implicit cache provider 不因普通传播噪声产生高频假 marker
  • 输出明确区分 observation、correlation 与 verified cause
  • 用至少一组 OpenPI/Bare Pi/OMP trace 验证 detector
  • 若加 UI,默认关闭且不改变 Session/model context
  • 保留现有累计 cache% 作为趋势指标,不用单次 marker 取代它

与现有 Issue 的关系

Activity

  1. added
    enhancement
    on Aug 25, 2026
  2. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    I am taking this issue for a focused first pass: document the provider-aware per-turn cache diagnostic contract and evidence boundaries, without changing runtime behavior or treating every cacheRead=0 as an invalidation. I will report the scope and validation in a follow-up.

  3. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Update: the focused design pass is available in PR #256: https://github.com/tt-a1i/openpi/pull/256\n\nIt defines provider-aware per-turn observations, conservative warm-to-cold evidence rules, unknown handling, replay coverage, and the boundary for a future opt-in detector. Validation passed: git diff --check, bun run lint, bun run typecheck. This PR is intentionally docs-only and does not claim runtime observability is implemented.

  4. tt-a1i commented on Aug 30, 2026

    @tt-a1i
    CollaboratorAuthor

    相关设计讨论已展开:Tool lifecycle across compaction #311。#311 定义动态工具跨越 compaction/recovery 的状态边界;本 Issue 继续负责逐 turn 记录 compaction boundary、tool-surface fingerprint、重新物化与 cache read/write 证据。

  5. testikun commented on Sep 4, 2026

    @testikun
    Collaborator

    我来认领 #156 尚未落地的运行时代码阶段,不重复此前的 provider-aware 设计文档。

    技术方案:实现无副作用的逐 turn cache observation/detector,并从 Pi authoritative usage 构建有界 trace report;first turn、warm→cold、cold→cold、partial hit、TTL/implicit-cache unknown、compaction/model/thinking/tool-surface correlation 会有独立判定。输出严格区分 observation、correlation 与 verified cause,不把 cacheRead=0 自动称为失效,也不改变现有累计 cache% 或模型上下文。

    实现已提交:PR #372。

    验证:bun run check 通过;完整 bun run test 为 Node 1248 passed / 1 skipped、Vitest 30 passed;Node 22.19 定向测试 11/11 通过。实现不修改 UI,因此不适用真实 UI 截图要求。

  6. tt-a1i commented on Sep 8, 2026

    @tt-a1i
    CollaboratorAuthor

    本轮社区调研补一项可学的统计口径与展示:pi-cache-graph 明确区分 active branch 和 whole session tree,逐 entry 保留 provider/model/timestamp,让 rewind/切分支后已经发生的调用成本仍可检查。session-data.ts@40c5ae46。

    这不是“OpenPI 还没有逐 turn 诊断”。当前 ed9dbc1 已有 cache-diagnostics tracker,并明确区分 observation/correlation/unknown。可在现有 tracker 上补有界时间线及导出,明确显示 branch、tree 等口径。

    不能照搬社区包的计算:它仅计 assistant message,OpenPI 现有 session-metrics 已包含 toolResult、compaction、branch_summary 的 usage。新增展示要保留这些事实;whole tree 也不等于所有 child/runtime/account 的完整账单。

    这是源码学习建议,未做运行或成本实验。界面应始终说明口径,不因为切换视图造成“费用被清零”的误解,也不从 cache 曲线推断 provider 的确定因果。

    研究记录:Pi 社区能力研究与候选清单(2026-09-08)。源码证据、运行验证与采用决策分别记录。

  7. tt-a1i commented on Sep 9, 2026

    @tt-a1i
    CollaboratorAuthor

    2026-09-09 源码对照(未安装、未运行、未 pi install)。强制两棵树:

    • 本 checkout a9b40f0044ee59c360a6077c1c0bdbdbd30da10b(package 0.4.0)没有 extensions/model-info/cache-diagnostics.ts。model-info/ 只有内联 getSessionMetrics():沿 getBranch() 累加 cacheRead / (input+cacheRead+cacheWrite),经 dashboard:model-info 进 Footer 的 cache N%(extensions/model-info/index.ts L14–38;footer.ts L190–196)。
    • ed9dbc1018f890fd54375f5371990ddfee8af5df 与 origin/main 0d17f4577fe31315fe6c95370d251bdb4e2413cf(0.8.1)上该文件是同一 blob 16648517(220 行)。引入提交是 PR feat(model-info): add per-turn cache diagnostics #372 merge 24d740762038b08bd18e0de5e788bb458d6bf5c5(2026-09-05)。24d7407 不在本 HEAD,在 origin。

    因此 2026-09-08 评论「这不是 OpenPI 还没有逐 turn 诊断」对 origin 为真,对 本 checkout 为假——和 checkout-vs-tracker 漂移同类。不要把本树的「没有文件」焊成「OpenPI 没有 diagnostics」,也不要把 #372 MERGED 焊成本 HEAD 已含修复。

    新研究记录(本地 draft,未 commit):docs/research/EXPERIMENT_CACHE_INVALIDATION_156_2026-09-09.md。覆盖猎取见 docs/research/EXPERIMENT_UNCOVERED_TRACKERS_2026-09-09.md。两份都还只在写出它们的工作区,不在 a9b40f0 blob 上。

    源码对照后的合同差(不是运行收据):

    1. Footer 两边仍只画累计 cache N%。 origin 的 diagnostics 走 model-info:cache-diagnostics 事件总线;生产 UI 没有消费者。PR feat(model-info): add per-turn cache diagnostics #372 自己写:no marker, no model-visible change, Does not close this Issue。
    2. 三份 detector 谓词不同,不能焊成一句 warm→cold。 OMP 7623b960 要求本轮 cacheWrite > 0(显式 prefix rewrite)。origin OpenPI 用 provider 名把 只有 anthropic 标成 explicit-prefix,不要求 cacheWrite,reprocessedTokens = current.input。Pi 0.85.1 6160683 的 cache-stats.ts 用 min(prev.prompt, current.prompt) − cacheRead,compaction 后 重置基线,showCacheMissNotices 默认 false,UI 还有 20k / $0.1 门槛。本工作区 node_modules 仍是 Pi 0.84.1,没有 cache-stats.ts。
    3. 所有权: prompt-cache 归 provider + pi-ai(归一化 Usage);Session JSONL 归 Pi SessionManager;tool result 是 JSONL 里的 message,不是第二份 KV。不要 Adopt pi-cache-optimizer / pi-cache-graph / oh-my-pi-plugin-cache-miss,不要新开 /cache 或 Marketplace / Memory 产品。docs(cache): clarify prompt-cache ownership, identity, accounting, and lifecycle boundaries #191 继续只做所有权文档。
    4. 本 Issue 仍应 OPEN。 剩余仍是 PR feat(model-info): add per-turn cache diagnostics #372 列出的:provider usage 字段矩阵、TTL 作为独立 kind(必须有证据)、至少一组真实 OpenPI / Bare Pi / OMP Session 回放、任何 opt-in marker 之前的门。本轮不实现、不快进、不 cherry-pick feat(model-info): add per-turn cache diagnostics #372。

    #311 只评论,不另写实验文件。#74 / #75 / #90 不在本帖范围。

  8. ooiuuii commented on Sep 11, 2026

    @ooiuuii
    Contributor

    I would like to contribute a narrow provider usage field matrix + synthetic replay regression evidence slice following the latest maintainer clarification.

    The diagnostic implementation from #372 already exists on current main@5bf2fe29e52801d79826c2eb573be403f53285e5; I will not recreate it, change provider classification/arithmetic, or add a /cache command/marker. The contribution will freeze the actual Pi 0.85.1 source identity, document normalized usage and unknown boundaries, and exercise the existing tracker/extension with explicitly synthetic multi-turn fixtures.

    This is not a claim of real provider billing accuracy or a live OpenPI/Bare Pi/OMP comparison. Synthetic fixture replay cannot satisfy the outstanding genuine comparative-trace requirement, and TTL will not be presented as a verified cause without supporting evidence. The PR will use Refs #156, leave those remaining gates open, and separate source verification, deterministic tests, and unperformed live experiments.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions