[Feature] 额度阈值前主动压缩:5h/周/月 usedRatio 触发自动 compact(可配置)

Open
#5,363 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript
Domain
backend

Research direction

Start by reading the compaction trigger path in packages/agent-core-v2/src/agent/loop/configSection.ts and fullCompaction/strategy.ts to see how compaction_trigger_ratio is consumed today, then read packages/oauth/src/managed-usage.ts for the usedRatio fields (limit_5h, limit_7d, limit_month_total). The core design work is where a safe turn boundary is detectable and where a new quota_compaction config section should be parsed; monthly_ratio is blocked on #4046, so the 5h and weekly windows are the startable slice. Done means a configured threshold fires exactly one compaction per window, with a journal event, and unchanged behavior when disabled.

Written by the indexing model from the issue text.

Description

你希望看到什么功能?

在额度即将耗尽前主动压缩上下文:当 5 小时 / 周 / 月窗口的已用比例达到可配置阈值(例如 95%)时,在安全的 turn/step 边界自动执行一次压缩,而不是等上下文窗口自然满或额度被打爆。

现状

  • 自动压缩只由上下文窗口压力触发(reserved_context_size / 未文档化的 compaction_trigger_ratio,见 packages/agent-core-v2/src/agent/loop/configSection.ts 和 fullCompaction/strategy.ts)。
  • 额度耗尽时 turn 直接失败(403/429),长任务中断;#1482 和 #5111 是实际案例。
  • 客户端已经解析 5 小时 / 周 / 月额度及 usedRatio(packages/oauth/src/managed-usage.ts 的 limit_5h、limit_7d、limit_month_total、limit_month_code),但这些数据目前只用于展示/状态栏。

期望行为

  1. 新增可配置的 quota 触发(字段名可讨论,关键是窗口和阈值可配置,且可单独关闭某个窗口):
    [loop_control.quota_compaction]
    enabled = true
    five_hour_ratio = 0.95
    weekly_ratio = 0.95
    monthly_ratio = 0.95      # 依赖月度用量接口,见 #4046
    min_context_tokens = 50000 # 上下文太小时压缩没有意义
    
  2. 触发时机:只在该 turn 的安全边界(两次模型调用之间、没有待提交的工具结果)压缩一次;同一额度窗口内不重复触发,窗口重置后才重新武装。
  3. 压缩事件写入 journal(例如 compaction/start|end),并在 TUI 中可见,说明是由额度阈值触发。
  4. 可选联动:压缩使用独立的 compaction_model(见 #5362),进一步降低压缩成本。
  5. 如果阈值判断时拿不到可靠的 usedRatio(接口异常/字段缺失),跳过而不是误触发。

动机

  • 在 95% 时压缩,剩余额度用于生成摘要,而不是被下一次全量 prefill 直接打爆;额度重置后首轮也从更小的上下文开始,避免 #5111 中重置后多会话同时 Continue、9 分钟烧掉 43% 的情况。
  • 压缩本身也消耗额度,所以阈值必须可配置,并允许按窗口单独启用。
  • 对无人值守的长任务(goal / deep research)价值最大。

与已有 issue 的关系

  • #4936 请求可编程压缩触发(idle 时间 + context 阈值,或 hook/CLI 触发)。本 issue 新增的是 quota 维度;如果 #4936 落地为通用触发框架,本需求可以作为其中一种 trigger。
  • #4639、#2833(#2138 已关闭)都是「限额重置后自动恢复执行」,不是「限额前主动压缩」。
  • #4046 请求在 /coding/v1/usages 暴露月度额度,是 monthly_ratio 的前置依赖;5h 和周阈值不受影响,可先行。
  • #4261 是 quota-aware 的模型路由/供应商 fallback,不涉及压缩。

验收标准(建议)

  • 构造 usedRatio ≥ 阈值时,压缩在边界触发一次并写入事件;开关关闭时行为与当前完全一致。
  • 同一额度窗口内只触发一次;窗口重置后按需重新触发。
  • 压缩消耗计入 usage,且触发压缩后不会立即因额度不足失败(阈值留有余量)。
  • 5h / 周 / 月三个窗口可独立配置阈值或关闭。
Dominant language
TypeScript
Stars
8.3k
Forks
1.4k
Avg merge
12h 6m
Merged PRs (30d)
596

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MoonshotAI/kimi-code

All issues in MoonshotAI/kimi-code

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.