Skip to content

context_pivot can pass the 30k gate but fail with “Nothing to compact” #24

Open

Description

@samsen3

This makes the advertised 30k threshold misleading.

Reproduction

  1. Start a session whose reported context usage reaches at least 30k tokens, while most of that context is from
    system instructions, loaded skills, recent content, or other retained context rather than old conversation
    turns.

  2. Run:

      /context-pivot <next phase>                                                                                    
    
  3. Let the agent prepare the brief and invoke context_pivot.

  4. Observe:

      Context pivot failed: Nothing to compact (session too small)                                                   
    

The same underlying error can also be seen when manually running /compact:

  Compaction failed: Nothing to compact (session too small)                                                         

Root cause

OpenPI's context_pivot extension uses ctx.getContextUsage() and permits a pivot once context usage is at least
30,000 tokens.

After passing that check, it calls Pi's native ctx.compact().

Pi's compaction implementation has a different eligibility condition: it must find actual old session messages that
can be summarized and removed while retaining the recent-message budget (20k tokens by default). A session may
report more than 30k context tokens but still have no eligible historical message segment to compact.

Therefore, the OpenPI preflight condition and Pi's native compaction condition are not equivalent:

  • OpenPI: total reported context is >= 30k
  • Pi: there is a non-empty, discardable historical conversation segment

When only the first condition is true, the pivot starts and then fails.

Expected behavior

context_pivot should only be offered or started when native compaction can actually proceed. If it cannot proceed,
the user should receive a clear explanation and be directed to /handoff for a new session.

Suggested fix

  1. Add a native-compaction eligibility/preflight API that extensions can query, ideally returning a reason when
    compaction is unavailable.

  2. Make context_pivot use that eligibility result in addition to its context-size threshold.

  3. If no discardable history exists, do not start the pivot; show a message such as:

      This session has no discardable history to compact. Use /handoff to begin the next phase in a new session.     
    
  4. As a fallback, translate native Nothing to compact failures into the same actionable /handoff guidance instead
    of presenting it as an unexpected pivot failure.

Environment

  • Pi coding agent with the OpenPI context-pivot extension enabled.

Activity

  1. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Hi! I am working on this issue. The current Pi extension API does not expose native compaction eligibility before calling compact(), so I will implement the smallest safe boundary: translate the native Nothing to compact failure into an actionable context-pivot message and add a regression test, without pretending to add a preflight API.

  2. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Implementation is ready in PR #235: #235. This is a focused, backward-compatible sub-fix for the native "Nothing to compact (session too small)" failure; the issue remains open for any broader eligibility API work.

  3. 627150795 commented on Aug 28, 2026

    @627150795
    Contributor

    Update: PR #235 is now based on the latest upstream main. The native no-history error handling remains scoped to context_pivot; focused context-pivot tests (4/4), lint, typecheck, and diff checks pass.

  4. 627150795 commented on Aug 29, 2026

    @627150795
    Contributor

    代码审计更新(对应 PR #235):

    • 已补充 headless/print/json 模式下的 context pivot 失败输出,原生 “Nothing to compact” 不再静默。
    • context-pivot 针对性测试 5/5,通过 lint、typecheck 和 diff 检查。
    • 说明:原问题中“总 token 超过 30k、但没有可压缩历史”的 false-positive 仍未完全解决;当前 Pi 扩展 API 没有可查询的 native compaction eligibility。因此 PR fix(context-pivot): clarify native no-history failures #235 是错误可见性修复,不宣称已经关闭这个 Issue。
  5. 627150795 commented on Aug 29, 2026

    @627150795
    Contributor

    代码有效性审计结论:PR #235 的 native Nothing to compact (session too small) 错误映射确实能在 UI/headless 路径被触发,并能清理 pending/compacting 状态;但 Issue #24 的根因仍未消失:context usage 达到 30k 后仍可能先返回 Context pivot started.,再由 native compaction 异步失败。当前 Pi 扩展 API 没有可查询的 native eligibility,因此本 PR应继续标记为“错误可见性子修复”,不宣称消除了 false-positive。

  6. testikun commented on Aug 31, 2026

    @testikun
    Collaborator

    This was generated by AI during triage.

    我想认领 #24 中尚未完成的核心 false-positive 子范围,不重复已合入的 PR #235(仅负责把原生错误转换为可行动提示)。

    技术方案:复用 Pi 公开导出的 prepareCompaction 与 DEFAULT_COMPACTION_SETTINGS,在 context_pivot 进入 ctx.compact() 前,对当前 ctx.sessionManager.getBranch() 做原生 compaction preparation 资格检查。若 Pi 判断没有可丢弃的历史,则在同步阶段拒绝启动、保持 pending/compacting 状态不变,并返回与 #235 一致的 /sessions 引导;只有 preparation 存在时才继续走现有异步 compaction 回调。

    我会增加纯函数/工具回归测试,覆盖 system/context 占用达到 30k 但没有可压缩 Session message、存在可压缩历史、已 compaction 分支和 malformed/empty branch;同时保留原生错误回调作为最终防线。不会新增第二套压缩器、不会修改 Pi Session 持久化和 compaction 语义。完成后运行仓库要求的 check/test 并提交 PR。

  7. testikun commented on Aug 31, 2026

    @testikun
    Collaborator

    This was generated by AI during triage.

    进度更新:当前 Pi 运行包的顶层实际导出不包含 prepareCompaction(仅类型声明暴露),因此没有引入未公开的 deep import。实现改为使用 Pi 公开的 findCutPoint、sessionEntryToContextMessages 和默认 compaction 配置,镜像原生的边界规则;ctx.compact() 仍是最终执行权威,preflight 与异步读取之间的竞态由现有原生错误回调兜底。

    另外补齐了 /context-pivot 命令路径:当上下文达到 30k 但没有可丢弃历史时,命令直接给出 /sessions/新 Session 引导,不再排队一个随后无法调用的模型工具请求;损坏或缺少首个保留 entry id 的分支也会 fail-closed。

  8. testikun commented on Aug 31, 2026

    @testikun
    Collaborator

    This was generated by AI during triage.

    实现已提交 PR #320。

    本 PR 修复的是 Issue #24 尚未覆盖的核心 false-positive:30k context usage 达标但没有可压缩历史时,context_pivot 不再启动;工具和 /context-pivot 命令都会在启动前 fail-closed,并保留 Pi 原生 compaction 错误回调处理检查与异步执行之间的竞态。

    验证:仓库 check 通过;context-pivot 定向测试 9/9;完整 Node 测试 1079/1079(1 skip);Vitest 30/30。

  9. testikun commented on Aug 31, 2026

    @testikun
    Collaborator

    This was generated by AI during triage.

    补充边界:ExtensionContext 当前不暴露 SettingsManager 的 keepRecentTokens,因此 preflight 使用 Pi 公开的默认 20k cut-point 配置;这不是新的 compaction source of truth。真正的 ctx.compact() 仍重新读取运行时配置并负责最终判定,若项目自定义 compaction 设置或分支在检查后变化,原生 onError 仍会 fail-closed 并给出相同引导。

  10. tt-a1i commented on Sep 9, 2026

    @tt-a1i
    Collaborator

    源码对照(2026-09-09,未 pi install、未跑 TUI)。本地记录:docs/research/EXPERIMENT_CONTEXT_PIVOT_2026-09-09.md(checkout a9b40f0044ee59c360a6077c1c0bdbdbd30da10b)。

    资格门今天仍是 30k occupancy。 MIN_CONTEXT_PIVOT_TOKENS = 30_000 + ctx.getContextUsage()。没有可丢弃历史预检。本 checkout 还没有 #235;origin/main 0d17f45 已有错误翻译与 headless console.error,资格门本身没变。

    相对 #24 仍缺的: (1) Pi 按生效 Settings 给出的公开 eligibility 查询;(2) 在 ctx.compact() 之前同步拒绝;(3) 模型可见的失败(不要先返回 Context pivot started.)。#320 用默认 20k findCutPoint 镜像资格已被审查证伪(自定义 keepRecentTokens 假阴性;compaction 条目经 sessionEntryToContextMessages 假阳性)。Pi 0.85.1(6160683)包根仍不导出 prepareCompaction,只有 findCutPoint / DEFAULT_COMPACTION_SETTINGS。同意关闭评语:不要再送第二套 cut-point。

    Mid-tool 不是 fail-closed。 扩展 ctx.compact() 是 fire-and-forget;AgentSession.compact() 先 abort() 当前 turn。terminate: true 只有 batch 内每个 finalized 结果都 terminate 才结束回合。30k 假阳性在工具路径上会先掐掉进行中的 turn,再异步 Nothing to compact。失败只到 TUI notify / origin stderr,回不到已经拿到 started 的模型。/context-pivot 命令不 compact,非 idle 只 followUp——相对更安全,但仍会约一次随后可能假启动的工具。

    handoff ≠ pivot:同 Session brief ≠ 官方 /handoff 新会话 ≠ /sessions 换文件 ≠ #154 rewind。本树 SETUP / 工具 description 仍写不存在的 /handoff skill;origin SETUP 已改 /sessions。文案债不是资格实现票。

    不实现、不重开 #320。#24 继续等 Pi-native eligibility API,接上后再同步拒绝。

  11. tt-a1i commented on Sep 9, 2026

    @tt-a1i
    Collaborator

    PR 考古补充(相对此前 30k / mid-tool 对照;未 pi install)。本地记录:docs/research/EXPERIMENT_PIVOT_PREFLIGHT_320_2026-09-09.md。

    #320 6b14f2b 比「默认 20k 预检」多三处硬门。 hasDiscardableHistory() 同时挡 agent_start 可见性、execute、/context-pivot 命令。假阴性会把工具藏起来,onError 兜不住——审查复现的 compactCalls=0 就是这个形状。try/catch → false 还会把异常翻译成「无历史」。

    认领先写公开 prepareCompaction,一小时后改口用 findCutPoint。 0.85.1(6160683)包根仍不导出 prepareCompaction / CompactionPreparation。findCutPoint 永远返回 CutPointResult,不是谓词;shouldCompact 问的是自动阈值(且读 enabled),手动 compact() / prepareCompaction 都不把 enabled: false 当否决。PR 自带 9 个测试只锁默认 20k fixture 的自洽,从未拿原生 prepare 当 oracle,所以 check 全绿与两处 P2 同时成立。

    只把 prepareCompaction 补进包根不够。 扩展 ctx 没有 getCompactionSettings();#320 第一版认领会再撞「谁提供生效 keepRecentTokens」。需要的是 Pi 拥有的、挂在 ExtensionContext 上的资格查询:与 compact() 同一 prepare 路径、带上已有的 Already compacted / Nothing to compact reason、不 abort()、不开始 summarization。不要再送 cut-point 克隆或 OpenPI Settings 读取器。

    不实现、不重开 #320。

  12. tt-a1i commented on Sep 9, 2026

    @tt-a1i
    Collaborator

    Hook ownership (session_compact_failed, 2026-09-09 source-read)

    Pi 0.85.1 (6160683, types.ts blob eaff3a86) adds session_compact_failed. It is an after-the-fact observe hook, not an eligibility query.

    AgentSession.compact() throws "Nothing to compact (session too small)" / "Already compacted" before session_before_compact when prepareCompaction returns null. The catch still emits session_compact_failed (reason: "manual", errorMessage: "Compaction failed: …"). Overflow has its own emit.

    Official examples/extensions/ never subscribe. OpenPI checkout a9b40f0 (peer 0.84.1) does not have the event type. origin/main 0d17f45 (0.8.1 / peer >=0.85.1) still does not subscribe. context_pivot only writes session_before_compact on the customize path.

    This issue still waits on a Pi-owned preflight eligibility query (PIVOT_PREFLIGHT locally as docs/research/EXPERIMENT_PIVOT_PREFLIGHT_320_2026-09-09.md). Listening to session_compact_failed would only observe after ctx.compact() already abort()ed. That is Pi-native observe/warn, not a second compact OS. Do not adopt cyclotomy’s session_before_compact { cancel: true } veto.

    Canonical inventory: docs/research/EXPERIMENT_OFFICIAL_HOOKS_2026-09-09.md. No new tracker.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions