跳转到内容

压缩

运行时上下文压缩:对话在数据库中全量保留;仅裁切发给 LLM 的四段视图。 关联:sleep.mdmemory.md

原则说明
不删消息历史保留;压缩仅改变运行时视图与 conversation meta
四段LLM 上下文 = system + summary + slim + raw
按需触发用量接近窗口上限时压缩;工具循环内阈值更高
与记忆独立压缩不触发语义提取;浅睡 cron 独立运行
① system — system prompt (self layer + resident memory + project context)
② summary — text summary of compressed portion (synthetic; not stored in messages)
③ slim — middle segment: trimmed user/assistant messages
④ raw — recent segment: full messages including tool calls

非压缩路径:新消息仅追加到 raw;边界不变。

压缩路径:用量超过阈值时,较早消息移入 summary/slim;摘要文本异步更新。

Adjust in Habitat runtime (habitat_runtime_config; Shell Settings → Habitat 服务 → 服务配置):

# habitat_runtime_config fragment (not config.yaml)
models:
deepseek-v4-flash:
context_window: 1000000
compression:
enabled: true
reserved_tokens: 8192
trigger_low: 0.60 # outside tool loop: compress at 60% usage
trigger_high: 0.80 # inside tool loop: compress at 80% usage
emergency_ratio: 0.92
raw_min_messages: 5
slim_min_messages: 50
summary_max_tokens: 4000
设置默认值说明
trigger_low0.60普通对话中的阈值
trigger_high0.80工具循环内的阈值
raw_min_messages5raw 段最少消息数
slim_min_messages50裁剪后 slim 段最少消息数

Compression and embedding batch packing use heuristic token estimates (tokenx in-process, id __estimate__:tokenx), not full HuggingFace vocabularies. Counts are approximate (good enough for budget triggers and chunk sizing); they are not provider-billing exact.

估算压缩预算时的优先级:

  1. models.<model>.context_window in Habitat runtime config
  2. compression.default_context_window(适用于所有模型)
  3. Provider /models 目录中的 contextWindow(Habitat 已注册 lookup 时)
  4. 以上皆无时的消息数回退(max_rounds 阈值)

Per-model config always wins over dynamically fetched catalog values; catalog is read-only and never written back to runtime config.

无任何上下文窗口来源时,压缩回退到按消息数触发。

在对话中强制压缩:/compress--force 忽略迟滞)。

Manual summarize (Cursor-style): /summarize collapses history into the runtime summary without waiting for automatic thresholds. When the turn is idle (last message is a completed assistant reply), boundaries become l2 = l3 = l4 and slim/raw are empty. During an in-progress turn, only content through the last completed assistant is summarized; the unfinished tail stays in raw. Summary text is merged incrementally (same as automatic compression), and the command waits for the summary LLM to finish.

Automatic boundary derivation keeps a non-empty raw segment (with a leading user message) so turns can continue while usage-based compression runs. That rule applies when some raw tail must remain. It is not a ban on empty raw: when the user explicitly /summarize on an idle conversation, full collapse (l2 = l3 = l4) is allowed. Mid-turn protection is implemented by not folding incomplete turns into the summary, not by permanently requiring a user in raw.

压缩与浅睡/深睡独立运行:压缩管理当前 conversation 的 LLM 窗口;记忆提取每晚从完整对话存档运行。