Whatsmore-nf/dsh-context-steward1

@whatsmore-nf/dsh-context-steward

DeepSeek Harness 插件:固定上下文容量下的认知资源调度与智能压缩系统(支持 dsh plugin CLI 安装)

包名
@whatsmore-nf/dsh-context-steward
版本
0.20.0
许可证
MIT
最近更新
2026年8月20日

安装

$npx -p @deepseek-ai/dsh dsh plugin --profile web add github:Whatsmore-nf/dsh-context-steward

配置(ContextStewardConfig

全部字段可选,缺省时使用下表默认值。未知键、类型错误、越界的比例参数会导致插件加载失败 (resolveConfig 严格校验,与官方插件一致)。

默认值含义
capacity4000注入的压缩上下文 token 预算(固定带宽上限)。
reserved0系统保留 tokens,不计入可压缩区。
decisionGuaranteefloor(capacity × 0.35)关键决策注意力带宽下限:保护决策时工作集最多占用的 tokens。
halfLifeMs600000时间衰减半衰期(10 分钟)。
adaptiveRecencytrue根据决策节奏自适应半衰期。
minAdaptiveHalfLifeMs10000自适应半衰期下限。
maxAdaptiveHalfLifeMs3600000自适应半衰期上限。
adaptiveThresholdstrue根据抖动自调优升降级阈值。
churnWindowMs60000阈值自调优的抖动观察窗口。
tuneStep0.03阈值自调优步长。
demoteThreshold0.35分数低于此值的工作项降级到冷池。
promoteThreshold0.55分数高于此值的冷池项提升回工作集。
workingRenderRatio0.5工作集占渲染预算的比例。
maxProtectedDecisions4全保真保留的关键决策快照数(超出部分决策老化)。
coldCompactScore0.4冷池分级压缩的最低分数门槛。
rehydrateThreshold0.6存档回灌工作集的分数门槛。
dedupetrue重复观测聚合为 重复×N 记录。
consolidatetrue已完成的阶段巩固为一条结构化摘要。
renderBudget可用容量渲染注入上下文的 token 上限。
maxItemChars6000单条 tool/observation 源文本截断上限(完整原文保留在金库)。
reclaimPeekLimit16价值密度回收的候选窥视条数。
enabledtruefalse 时只注册服务、不监听事件。
injecttrue启用压缩快照注入(仍受 injectThresholdRatio 门控)。
injectThresholdRatio0.8仅当实测会话压力 totalTokens >= floor(窗口 × 比例) 时才注入压缩快照——与官方 compaction 的 thresholdRatio 触发语义一致;低于阈值时插件只做不可见的内存书签,不干扰正常推理。
injectContextWindow0上下文窗口显式覆盖(tokens)。0 = 自动取路由模型适配器上报的 contextWindow;解析不到且未覆盖时保守跳过注入。

使用

Service 形态(Harness 内)

import type { Context } from '@deepseek-ai/cordis'
import ContextSteward from '@whatsmore-nf/dsh-context-steward'

export const name = 'context-steward'
export const inject = ['sessions']

export function apply(ctx: Context): void {
  const plugin = ctx.plugin(ContextSteward, { capacity: 8000 })
  // 可选:接入 LLM 语义摘要(pre-step 空闲期异步预热压缩,压缩质量从启发式升级为结构化摘要)
  // plugin.asyncSummarize = async (content, depth) => await llm.complete(
  //   buildCompactionPrompt({ context: [content] }), { maxTokens: depth >= 3 ? 160 : 80 },
  // )
}

服务以 ctx.contextSteward 暴露;按会话取独立调度器: ctx.contextSteward.scheduler(session)

standalone 形态(演示 / 单测,无需 Harness 运行时)

import { createContextStewardPlugin } from '@whatsmore-nf/dsh-context-steward'

const plugin = createContextStewardPlugin({ capacity: 8000 })
plugin.hooks.onAppend?.({ id: 'u1', kind: 'user', content: '目标是部署服务', timestamp: 0 })
plugin.hooks.onDecision?.({ goal: '部署服务', currentStep: '选型', attentionFocus: ['部署', '服务'] })
const prompt = plugin.hooks.onBeforePrompt?.() // 注入压缩后的上下文

调度器核心

CognitiveResourceScheduler 独立导出:ingest()checkpoint()setPhase()consolidatePhase()compiledContext()query()exportArchive()exportState() / restoreState() —— 完整签名见类型声明。