dsh-auto-summary:會話超 token 閾值時摘要並交接新 Agent

前言

在 DeepSeek Harness(DSH)裏跑長任務時,同一會話會持續累積上下文與計費 token。上下文變長後,模型側壓力上升,任務邊界也容易在多輪對話裏被沖淡。常見做法是手動開新會話、自己粘貼背景,或依賴會話壓縮——後者會改寫事件流,且不一定保留「最初那條用戶指令」的原文。

下面介紹第三方 bundle dsh-auto-summary(維護者 Warsk-80486,社區分類爲工作流)。它在每個 Agent 回合結束後觀測用量,當會話跨過可配置的 token 閾值時,用一次獨立的摘要調用生成交接簡報,並通過子 Agent 啓動新的執行體;交接任務裏會強制帶上會話的首條用戶 prompt 原文,便於續做而不丟任務定義。

這是什麼

dsh-auto-summary 是以 dsh.bundle.patch 形式掛載的外部插件,不修改 DSH 源碼。它監聽已結束的回合(turn/end),從持久化會話日誌或當前請求的 token 計量判斷是否達到閾值;達到後寫交接簡報並調用 spawn 等子 Agent 提供方啓動新 Agent,摘要內容作爲新 Agent 的首條用戶消息。

與「在原會話裏繼續堆消息」或「對原會話做 compaction」不同:README 明確說明被摘要的會話不會因本插件新增事件或被 compact;摘要請求是單獨組裝的 ctx.llm.stream(),不帶 session id,避免污染原對話。

核心功能

觸發與計量

  • Turn-end 觸發:在 turn/end 之後執行,即每完成一段工作再評估。
  • Token 閾值:默認 100000000(一億),從 durable session log 計量;可在 profile 中改爲更貼近實際的數值(最小 1000)。
  • 兩種指標:total(累計 provider 計費 token,默認)或 context(當前請求壓力,ctx.tokenMeter.measure)。
  • 僅已完成回合:默認 onlyCompletedTurns: true,只在 turn/end 且 reason 爲 completed 時觸發。

首條 prompt 與交接

  • 首條 prompt 保底:從 DSH 的 titleInput 投影讀取(壓縮後仍可 survives),即使用戶自定義 handoff 模板漏寫,也會強制追加「Original first user prompt」段。
  • 新 Agent:通過 ctx.subagents.startActivation 與 handoff.provider(默認 spawn)啓動;簡報經 handoff.promptTemplate 渲染,佔位符包括 {{firstPrompt}}、{{summary}}、{{sessionId}}、{{turn}}、{{reason}}。

安全與失敗策略

  • 遞歸安全:handoff 產生的是子 Agent;默認 summarizeSubagentSessions: false,且跳過 origin: 'subagent' 或 delegationDepth > 0 的會話,避免 handoff Agent 再觸發 handoff。
  • Re-arm:一次成功交接後,閾值在用量基礎上再抬高一檔,長會話是「每跨一檔閾值交接一次」,而不是每回合都交接。
  • Fail-open:摘要或 spawn 失敗會記錄日誌,下一回合重試;連續失敗達 maxConsecutiveFailures(默認 3)後消耗當前閾值檔位,0 表示一直重試。
  • Dry run:dryRun: true 只打日誌,不調模型、不創建 Agent。

運行依賴

  • DSH 版本:engines.dsh: ^0.2.1-rc.5(package.json 與 README 一致)。
  • Profile 需掛載:agents、llm、sessionProjections、subagents、tokenMeter;README 稱 dsh-base + dsh-web-app 組合已滿足。
  • 摘要模型路由:默認沿用會話最近一次 request/header 路由;也可在配置裏同時指定 summary.provider 與 summary.model。

安裝與啓用

插件以 git 依賴形式安裝,倉庫內已提交構建產物 lib/(第三方 bundle 不跑安裝期 build script)。

在 DSH Web 界面 Plugins 中安裝該倉庫 bundle,或讓 Agent 通過 plugin_manager 執行 install_bundle:

git+https://github.com/Warsk-80486/dsh-auto-summary.git

需要固定版本時,可 pin 到 40 位 commit:

git+https://github.com/Warsk-80486/dsh-auto-summary.git#<40-char-commit-sha>

安裝後 bundle 出現在 profile 的 dsh.profile.bundles 列表,並掛載 auto-summary 行。若 Plugins 頁提示需要重啓,則重啓應用。

本地開發可在 DSH 應用關閉時用 CLI 鏈接目錄(需絕對路徑):

dsh plugin --profile desktop add /absolute/path/to/dsh-auto-summary

卸載:在 Plugins 頁移除 bundle,或:

dsh plugin --profile desktop remove dsh-auto-summary

說明:插件在 DSH 進程權限下運行;安裝前請自行查看 GitHub 倉庫 源碼與 MIT 許可證。社區目錄 SkillHub 插件頁 爲獨立站點,與 DeepSeek / 幻方無官方從屬關係。

配置

所有字段有默認值,不配置也可掛載。在 profile 自身的 cordis.patch.yml 中覆蓋 bundle 行(該層在 bundle 之後應用),例如:

- id: auto-summary
  config:
    thresholdTokens: 100000000
    metric: total
    onlyCompletedTurns: true
    summarizeSubagentSessions: false
    maxConsecutiveFailures: 3
    dryRun: false
    summary:
      provider: ''
      model: ''
      maxInputChars: 48000
      maxOutputTokens: 4096
      timeoutMs: 120000
    handoff:
      provider: spawn
      delivery: parent
      labelPrefix: handoff
      promptTemplate: |
        You are a continuation agent taking over a task from an earlier DeepSeek Harness session.
        ...
        ## Original first user prompt (verbatim)
        {{firstPrompt}}
        ## Handoff summary
        {{summary}}

長會話若希望更早交接,應把 thresholdTokens 調到符合預期的數值(README 默認一億相當於「默認幾乎不觸發」,適合先驗證鏈路再收緊)。

handoff.delivery:parent 時原 Agent 會收到新 Agent 完成通知;caller 爲 fire-and-forget。

典型用法

  1. 在已滿足依賴的 DSH profile 上按上文命令安裝 bundle 並重啓(若需要)。
  2. 保持默認配置時,插件僅在 token 達到極高默認閾值時纔會交接;調試可先設 dryRun: true 觀察日誌,再將 thresholdTokens 設爲可觸發的值。
  3. 長任務持續在同一主會話中推進;當計量跨過閾值且回合正常結束時,插件讀取 transcript(人類 prompt + 助手文本,新到舊,受 summary.maxInputChars 限制),調用摘要模型生成交接簡報,再 spawn 子 Agent 接手。
  4. 原會話不再寫入摘要相關事件;後續工作主要在 handoff 子 Agent 會話中繼續,且其首條任務仍包含最初用戶 prompt 原文。

適用場景與注意

適合:單會話內 token 或上下文壓力持續增長、又希望「開新 Agent 續做」且必須保留任務原文的 DSH 工作流;與 DSH「一切皆插件」的擴展方式一致,以 bundle 補丁掛載即可。

注意:

  • 默認跳過子 Agent 會話,handoff 鏈路由 spawn 承擔,避免無限遞歸。
  • 摘要失敗不會阻塞原會話正常 turn,但可能延遲交接,需結合日誌與 maxConsecutiveFailures 排查。
  • 摘要使用額外模型調用,會產生獨立 token 消耗;summary.maxOutputTokens、timeoutMs 可按任務體量調整。
  • Node.js >=20.0.0;插件版本見 package.json(當前資料爲 0.1.0)。

結尾

dsh-auto-summary 在回合結束時計量 token,超閾值則用一次輔助模型調用寫交接簡報,並以子 Agent 續任務,同時保證首條用戶 prompt 不被模板遺漏。若你在 DSH 裏做長 horizon 開發,可在社區目錄與 GitHub 查看說明與 issue:

  • 目錄頁:https://www.skillhub.cn/plugins/Warsk-80486/dsh-auto-summary
  • 倉庫:https://github.com/Warsk-80486/dsh-auto-summary
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜