Skip to main content

billion-context-dsh

33Stars3Forks4Issues0Watchers

Model-driven context management (Active Context Pruning / ACP) for the DeepSeek Harness — the model decides when and what to compress. Ported from billion-context-pi (ranxianglei); acp-kernel reused verbatim. CompactionEngine backend with compress/decompress/search_context/acp_status tools.

Evidence5/5methodologySourceInstallMaintenanceDSH versionSecurity scan
Machine-auditedInstall commandRepo verifieddsh-plugin topicLicenseREADMEAI wiki
Language
TypeScript
License
MIT
Branch
main
acpbillion-contextcontext-managementdsh-plugin

Install

cmdweb profile
$ dsh plugin --profile web add billion-context-dsh

Run the command above in your terminal to install this plugin via the dsh CLI. You can switch Profile in the top-right corner. New to dsh? Read the beginner tutorial

Install via your agent

Install the DeepSeek Harness plugin Tyan66666/billion-context-dsh for me: review the repository at https://github.com/Tyan66666/billion-context-dsh first, then run the install command and verify the plugin loads successfully.

Paste this instruction to the DSH Web GUI assistant — it will install and verify for you.

一句话定位

为 DeepSeek Harness 提供模型驱动的上下文压缩后端:当对话变长时,模型自己看到"效率提示",由它判断何时压缩、写哪段摘要;原文始终保留在 append-only 会话日志里,可随时检索或恢复,不再被静默硬截断。

核心能力

  • 提供 compress 工具,让模型用自己的摘要替换一段已读完的对话边界(自动平衡到 tool-call/result 配对点),对某块的摘要节点再次压缩即可分层蒸馏(tier 2/3)。
  • 提供 decompress 工具,按 acp_status 显示的 bN 编号只读恢复压缩块的原始内容。
  • 提供 search_context 工具,在压缩块摘要和原文里做混合检索(词干化 + CJK bigram + 字符 n-gram),命中会回链到所属块。
  • 提供 acp_status 工具,向模型展示上下文分解(tool/text/summaries 占比)+ 压缩块账本 + nudge 决策;支持 scope/view/tool/sort/limit 钻取。
  • 在 agent/pre-step 钩子注入 nudge 消息:当内核建议压缩时,向模型推荐一段可压缩范围表(surface seq + tool/text 占比)。
  • 注册 /acp 斜杠命令:在命令行执行 status(含 nudge 仲裁和窗口来源)/ compress / decompress,让人在终端也能查看压缩账本。
  • 自动探测模型的真实上下文窗口(按 provider/model 缓存,失败也缓存);同时把压缩的 token 费用按宿主 token-meter 的词汇记账(避免把宿主账本扣成负数)。
  • 接管 host 内置 ctx.compaction 槽位,把 compactIfNeeded/compactNow 显式短路为 null —— 自动策略永不自动摘要,只 nudge。

技术实现

  • 语言: TypeScript(strict, ESM,.ts 导入后缀)
  • 关键依赖: [email protected](构建时由 tsup 内联进 bundle),@deepseek-ai/dsh-compaction@^0.1.0-rc.6(peer 接缝),@deepseek-ai/cordis@^4.0.1(peer 接缝)
  • 架构模式: 作为 CompactionEngine 后端挂到 DSH,eager 注册 4 个模型工具 + /acp 命令 + system prompt 段到对应服务;监听 agent/pre-step 注入 nudge;监听 session/event 在压缩完成后微任务隐藏 call/result 配对(避免 strict provider 报 HTTP 400)
  • 入口文件: src/index.ts(AcpCompactionEngine),模块拆为 messages(M1 事件投影)/ state(M2 每会话内核状态)/ tools(M3 模型工具)/ nudge(M4 注入式建议)/ system-prompt(M4 一次性指引段)/ prompts(M4 可配置模板)/ commands(M4 /acp 命令)/ region(M5 持久化事务 + 日志重建账本)/ window(自动窗口探测)/ config(阈值组装)/ host-tokens(M12 影子价格镜像)

适用场景

长任务、多轮调试、跨多文件探索这类容易把上下文撑爆的会话。当模型跑着跑着发现"上下文已经过半",它会收到 nudge 提示,然后主动把已完成的子任务、跑过的日志、读过一遍的文件摘要成一段紧凑文本 —— 不是硬截断、不是后台静默动作,模型自己决定哪些能压缩。下次需要原文就 decompress 一段,找细节就 search_context 关键词,特别适合需要长时间保持状态的开发会话。

前置依赖与兼容性

依赖最低版本说明
DeepSeek Harness0.1.0-rc.6peer 依赖 dsh-compaction 接缝所在行
Node.js>=20package.json engines.node 声明
平台跨平台未声明 os/cpu 限制
原生模块无无 node-gyp 依赖,纯 TypeScript

平台限制声明在源码中未明确列出。

安装方式

dsh plugin --profile web add github:Tyan66666/billion-context-dsh

需要自定义 config(如指定 modelContextLimit / 自定义 prompts)时,建议手写 profile 补丁 —— bundle 补丁(cordis.patch.yml)只插入无 config 的默认行。

配置项

配置类型说明默认值
modelContextLimitnumber显式指定上下文窗口(tokens)。写了这个值就跳过自动探测未设置(走自动探测)
autoModelContextLimitboolean从 LLM 运行时探测模型真实窗口;探测失败回退到 128000true
nudgeMinContextLimitPctnumbernudge 窗口下界(仅作配置校验,增长路径无百分比下限)内核默认 0.45
nudgeMaxContextLimitPctnumber过限线:超过即触发 nudge;engine 故意压低以抢在宿主 80% 自动压缩线之前0.7
nudgeEmergencyThresholdPctnumber紧急 nudge 阈值(绕过每轮去重);故意从内核默认 0.95 下调0.85
coreOverridesobject任意 acp-kernel Config 覆盖(逃生口)undefined
countTokensfunction自定义 token 估算函数(仅影响内核内部估算,不影响 nudge 压力决策和 acp_status)undefined
autoToolsboolean注册 4 个模型工具(compress / decompress / search_context / acp_status)true
autoCommandboolean注册 /acp 斜杠命令true
autoNudgeboolean在 agent/pre-step 注入内核建议的 nudgetrue
promptsobject按槽位覆盖模板(nudge / rangeTable / tools / systemPrompt),支持命名占位符;模板拼写错误会在引擎启动时抛错undefined

modelContextLimit 与 nudgeMinContextLimitPct/nudgeEmergencyThresholdPct 在源码注释里同时存在,作为常见配置项完整列出。

常见问题

Q: 装上后没看到任何效果,怎么排查?

A: 首先确认没有和内置 dsh-compaction-basic 并存(同一 realm 内两个都 provide ctx.compaction 会冲突),把 compaction-basic 设为 disabled: true。然后在终端跑 /acp status,应该看到 blocks、tokens compressed、estimated context、nudge: 行;空白通常意味着模型还没看到 nudge,或者 autoNudge: false 被关掉了。

Q: 模型没主动压缩,nudge 一直 idle,正常吗?

A: 正常。ACP 不抢方向盘 —— 它只在压力到位时才提示;模型可以忽略 prompt(哲学就是"由模型决定")。如果你看到 next nudge: ~N tokens to go,那只是提示离下次提示还差多少 token,不一定要立刻压缩。

Q: 压缩过程中报错 Range contains no compressible messages,怎么修?

A: 这通常意味着请求的 seq 范围已经被先前的 compress 全部吸收了(seq 已经不在当前 surface)。先把边界换成本轮 acp_status Surface 行里的最新 seq,或者用钻取后的 mN 编号(工具会自动反向映射到 live surface seq);完全压在已压缩范围内的请求就是 AlreadyCompressedRangeError,工具会报告 "already compressed"。

Q: 怎么确认压缩的 token 真的算进宿主账本了?

A: 看宿主的状态栏 / token 占用率。插件按宿主 token-meter 的词汇(ctx.tokenMeter.measure 优先,失败回退到 src/host-tokens.ts 的本地镜像)写 shadowedTokenCount,所以宿主的账本和压缩账本应该是同一套数值;如果差异巨大,可能是宿主自身估算器未导出导致(issue #54 描述的场景,目前用本地镜像兜底)。

Q: 跨会话/重启后压缩块还在吗?

A: 在。压缩块账本是从 append-only 会话日志里 rebuildBlockLedger 重建出来的(src/region.ts),内核状态也按会话持久化;重启后可以继续对既有块做 tier 2/3 蒸馏。

Q: 卸载会影响已有数据吗?

A: 卸载 bundle 只会移除 profile 里的组合行;append-only 会话日志里所有原文、压缩 checkpoint、账本事件都保留 —— 这是设计目标,decompress/search_context 必须能继续工作。要彻底清空,对应会话手动删日志文件即可。

上手难度

进阶 —— 替换了 host 的核心上下文管理策略,需要理解 DSH 的 compaction 接缝、ctx.compaction 多 provider 冲突规则、profile 补丁机制;初次接入最常踩的坑就是忘了禁 dsh-compaction-basic。

已知问题与限制

  • 公开测试版:v0.2.9 与 DeepSeek Harness 自身均处于公开测试版,README 明确写"请勿用于生产环境",预期有破坏性变更(README.md:5-6)。
  • 必须独占 compaction 接缝:与内置 dsh-compaction-basic 在同一 realm 内会冲突,必须禁用或删除其中一行(README.md:81-82)。
  • 范围表自算是临时绕行:buildCompressibleSeqRanges 不复用内核 compressibleRanges,是 kernel ref-map 漂移缺陷的临时宿主侧绕行(issue #38);每次内核升级时需检查漂移是否已在上游修复,修复后应删除该自算逻辑改回内核实现(AGENTS.md:55,63 / docs/dsh-porting-verification.md:176)。
  • 宿主 token-meter 估算器未导出:当前用 src/host-tokens.ts 的本地镜像兜底,且宿主扁平 4 字符/token 本身对 CJK 占用率低估约 4 倍(issue #54);当 dsh-token-meter 导出 estimateContent/estimateMessage 或改为 CJK-aware 时应改用导出实现(docs/dsh-porting-verification.md:178)。
  • CJK 1 字 1 token 是估算,不是真分词:内核 defaultCountTokens 用 1 CJK 字 = 1 token、4 其他字符 = 1 token 的字符启发式估算(与 DeepSeek 官方 ~0.3 token/字的真实分词不一致),可通过 countTokens 替换为更准确的实现;该估算只用于内部压力判断与账本展示,宿主账本走宿主词汇(src/index.ts:127)。
  • pressure 压力显示可能有偏差:/acp status 显示的 estimated context 优先取宿主 sessionProjections.contextPressure.projectedTokens,该值随宿主策略变化;如未注册该服务会回退到 tokenMeter surfaceTokens,最后才用字符估算(src/nudge.ts:53-70)。

Read the usage guide →

Install steps, key points, FAQ and compatibility for this plugin — auto-derived from indexed fields.

Listing badge

Listed on deepseek-plugin.org
[![Listed on deepseek-plugin.org](https://img.shields.io/badge/listed_on-deepseek--plugin.org-007EC6)](https://deepseek-plugin.org/plugins/Tyan66666/billion-context-dsh)

Paste this markdown into your GitHub README to link back to this listing. The badge only states the listing — not a security endorsement.

← Back to plugin directory