Skip to content

refactor(context): unify request budgeting and context pipeline across agent and repair flows - #50

Merged
ztygod merged 4 commits into
mainfrom
feat/context-wanshan
Jul 20, 2026
Merged

refactor(context): unify request budgeting and context pipeline across agent and repair flows#50
ztygod merged 4 commits into
mainfrom
feat/context-wanshan

Conversation

@ztygod

@ztygod ztygod commented Jul 20, 2026

Copy link
Copy Markdown
Owner

背景

本次变更完善了模型请求的 Token 预算治理,并重新整理 Context 模块职责边界。

此前预算只覆盖 Runtime Context Sections,没有完整计算 System Prompt、Transcript、Tool Calls、Tool Results、Tool Schemas 和消息协议开销。同时,ContextManager 承担了过多 pipeline 内部编排职责,Repair 阶段也缺少明确的 stage 和验证失败 Section。

主要变更

  • 完整计算模型请求的 Token 使用量:
    • System Prompt
    • User/Assistant/Tool Messages
    • Assistant Tool Calls 及 arguments
    • Tool Results
    • Tool Schemas
    • 消息封装开销
    • 输出预留和安全余量
  • Section 压缩与截断统一使用 availableSectionTokens
  • 增加发送前硬校验,超限时抛出 ContextWindowExceededError,不发送非法请求。
  • 增加 Transcript Budget 策略:
    • 保留当前用户任务
    • 保留最新完整工具交换
    • 归档旧工具交换
    • 支持确定性压缩和可插拔模型摘要
  • 引入独立 ContextPipeline,统一负责:
    • Transcript Projection
    • Token Budget
    • Transcript Compaction
    • Section Compression
    • Truncation
    • Prompt Assembly
    • Diagnostics
    • 最终预算校验
  • 精简 ContextManagerOptions,隐藏 pipeline 内部实现细节。
  • 主 Agent 与 Repair 请求统一通过 Context Pipeline 构建。
  • Repair 使用 stage: "repair",并将验证失败作为高优先级、required 的临时 Context Section 注入。
  • Repair 失败信息不再写入长期 Transcript。
  • Diagnostics 增加 stage、各类 Token 用量、压缩结果和最终剩余空间。
  • 增加 Context 压缩、预算失败和估算误差观测。
  • 清理 Context 模块中的重复逻辑、冗余字段、重复转发方法及过量注释。

兼容性

  • 保留现有公开 Context 组件及导出。
  • maxInputTokens 暂时保留为兼容字段,内部 Section 预算改用 availableSectionTokens
  • 未修改现有预算阈值、压缩策略和截断语义。

验证

  • TypeScript 检查通过
  • ESLint 检查通过
  • Prettier 检查通过
  • Context/Agent 相关测试全部通过
  • 全量测试 131/132 通过
  • 唯一失败为既有的 Windows PIXELLE_CONFIG_HOME 路径模拟测试,与本次变更无关

@ztygod
ztygod merged commit d5c132c into main Jul 20, 2026
2 checks passed
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.

1 participant