dsh-trajectory-governor:爲 DeepSeek Harness 提供閉環 Agent 軌跡控制

前言

在 DeepSeek Harness(DSH)裏跑 Agent,常見做法是靠 system prompt 約束行爲,或在會話層面切換 spec / react 等模式。這類手段能影響模型「怎麼說」,卻難以圍繞真實工具事件流做閉環:一次 write 之後有沒有 readback,性能改動有沒有完整 benchmark,重複調用是否帶來新信息,往往仍靠模型自覺。

dsh-trajectory-governor(維護者 chunsi-w)走的是另一條路:在 Harness 事件流上維護任務階段、驗證債務與完成門檻,把軌跡策略做成可配置的 control plane。它是 dsh-mode-boost 的 clean-sheet 重構,不依賴 preset fork,也不依賴 super-injector。項目在 GitHub 上約 9 stars,SkillHub 分類爲「工作流」。

這是什麼

一句話定位:面向 DeepSeek Harness 的閉環 Agent 軌跡控制平面(Closed-loop trajectory policy plane)。

插件監聽 inbox 消息、工具調用與 Code Mode 子調用,在同一請求內追加可重建的近場 policy message,並維護:

  • Task Episode 與連續性關係(new / continuation / extension / correction / review 等);
  • 當前工作階段與結構化信息增益;
  • workspace revision 與驗證債務(Verification Debt);
  • benchmark 證據與同規格、帶容差的性能迴歸判斷;
  • scoped 工具能力面(必要時暫時隱藏 write / edit);
  • 顯式 finish 與自然結束的完成門;
  • 可選自適應 reasoning effort;
  • 本地、非模型可見的決策賬本。

閉環如何工作

README 給出的主路徑如下:

真人消息被 inbox claim
  -> 在第一次 prompt assembly 前建立 Task Contract
  -> 判斷 new / continuation / extension / correction / review / conversation
  -> 必要時通過 agent.ctx.tools.restrict() 暫時隱藏 write/edit
  -> agent/pre-step 在同一個請求內追加可重建的近場 policy message
  -> Native tool 或 Code Mode SDK 子調用產生 durable 事件
  -> 計算 observation novelty / mutation / verification
  -> 修改產生 Verification Debt
  -> readback + test/build/check 清償當前 revision 的債務
  -> 可選 benchmark gate 只接受當前 revision 的完整、可解析結果
  -> 同 query count / concurrency / warmup 才比較 QPS,容差內不誤判噪聲
  -> finish guard 與 turn-stopping 阻止無證據結束
  -> 有限續步耗盡後要求模型明確報告 blocker

下面分幾塊說明各模塊在做什麼。

Task Episode 與關係判斷

當前確定性 relation 包括:new-objectivecontinuationextensioncorrectionclarificationreviewconversation。判斷綜合指代詞、文件名與 artifact 重合、與上一 objective 的詞面相似度,以及 fix/build/review 語義。第一條消息若是寒暄,不會永久關閉插件;下一條真實任務會建立新的 objective。

能力面控制

當前版本把明確的 writeedit 視爲專用 mutation 工具。str_replace_editor 是讀寫混合工具,只有在仍有獨立 read 時纔會被暫時隱藏,避免 Minimal preset 失去觀察能力。Code Mode 下 restriction 會改變生成的 TypeScript SDK,但不會刪除 run_code transport。

bash / pwsh 仍是混合讀寫工具。對 apply_patch、重定向、sed -igit apply、包管理安裝等常見寫入簽名,Governor 會保守標記爲 mutation risk:成功後遞增 workspace revision 並創建需要命令驗證的債務。需要明確:Governor 是軌跡策略,不是安全邊界;真正權限仍由官方 sandbox / approval 執行。

Verification Debt

成功的 write / edit / str_replace_editor mutation 或高風險 shell 寫入會創建驗證債務,並綁定創建時的 workspace revision;後續修改會使舊 readback / test 證據失效。

  • 源代碼:需要 readback + test / build / check;
  • 文檔:需要 readback;
  • 未知 artifact:需要可執行驗證。

以下 shell 命令會被識別爲 verification:

npm/pnpm/yarn/bun test|build|lint|typecheck|check
pytest / vitest / jest / mocha / tsc
cargo test / go test / dotnet test / mvn test / gradle test / make test

bash 文本里出現非零 [exit code: N] 時也會被視爲失敗。債務未清時,Governor 最多按配置追加有限驗證步;到達上限後追加一次僅用於報告 blocker 的步驟,不會無限循環,也不會靜默放行。

Benchmark-aware Stop Controller

實驗型性能任務可顯式打開 benchmark gate,不影響普通開發任務。規則是確定性的:

  • 只有 total_queries >= fullBenchmarkMinQueriesrecall >= fullBenchmarkMinRecall 才能通過當前 revision;無法解析的結果明確成爲 blocker。
  • QPS 只和相同 total_queriesconcurrencywarmup 的 best record 比較。
  • 同規格下降未超過 benchmarkScoreTolerancePercent 時保留爲可接受噪聲。
  • 每次已識別 mutation 或高風險 shell 寫入都會使先前 benchmark 失效。

benchmark 工具應在 meta 或模型可見文本中返回完整命名指標,JSON 最可靠,例如:

{
  "total_queries": 10000,
  "recall": 0.98,
  "qps": 1250.5,
  "concurrency": 8,
  "warmup": 500
}

Governor 只保留 benchmark best record 與狀態,不直接寫用戶工作區,因此不會僞造「自動 rollback」。

Native 與 Code Mode

Governor 同時觀察 Native 的 tool/call / tool/result,以及 Code Mode 的 tool/code-dispatch-start / tool/code-dispatchrun_code 內部的 read / write / edit 也會更新信息增益、釋放 restriction、創建並清償驗證債務。

狀態工具與決策賬本

只讀工具 trajectory_policy_status 返回當前 Agent 的 episode、relation / phase / risk、artifacts、restriction、verification debt、benchmark 狀態、ledger 狀態與 assembly hash 等。實現嚴格使用 exec.agent,不會讀取其他會話。

決策賬本默認寫入:

$DSH_HOME/trajectory-governor/decisions.jsonl

賬本保存 session / message id、原消息 SHA-256(不保存原文)、relation / phase / risk、tool effect、open verification debt、request assembly hash、turn stop reason 等。賬本失敗不會改變官方 Agent 執行流,但會通過 console.errortrajectory_policy_status 暴露原因。

安裝與啓用

環境要求

  • Node.js ^22.19.0 || >=24.0.0
  • DeepSeek Harness 0.1.0-rc.7(開發與集成測試基線);peer range 兼容 0.1.0-rc.5<0.2.0

從 npm 安裝(推薦)

當前包名爲 @chunsi-m/dsh-trajectory-governor,版本 0.2.0,MIT 許可證。安裝命令:

dsh plugin --profile web add @chunsi-m/dsh-trajectory-governor
dsh --profile web --dump-config

如需固定版本:

dsh plugin --profile web add @chunsi-m/dsh-trajectory-governor@0.2.0

包已聲明 cordis.patch.yml bundle patch,dsh plugin --profile web add ... 會把它加入 web profile 的 bundle 層,而不是隻安裝成普通依賴。

從源碼或 tarball 安裝

從當前目錄:

npm run build
dsh plugin --profile web add .
dsh --profile web --dump-config

安裝 tarball:

npm run pack:release
dsh plugin --profile web add ./chunsi-m-dsh-trajectory-governor-0.2.0.tgz

配置與推薦上線順序

cordis.patch.yml 默認配置節選:

- insert:
    - id: trajectory-governor
      name: '@chunsi-m/dsh-trajectory-governor'
      config:
        mode: active
        adaptiveReasoning: false
        restrictBeforeEvidence: true
        autoVerify: true
        maxAutomaticContinuations: 1
        exposeStatusTool: true
        ledger: true
        maxLedgerBytes: 10485760
        benchmarkRequired: false
        benchmarkToolNames: [run_benchmark]
        verificationToolNames: [build_project, run_correctness_test]
        finishToolNames: [finish]
        fullBenchmarkMinQueries: 10000
        fullBenchmarkMinRecall: 0.95
        benchmarkScoreTolerancePercent: 2
        maxActionsWithoutBenchmark: 8
        maxStagnantFullBenchmarks: 2
        stopRetryOnDeterministicErrors: true

常用字段含義:

字段 默認 說明
mode active off / shadow / active;shadow 只決策和記賬,不改請求
restrictBeforeEvidence true fix / continuation 等任務在觀察前臨時隱藏已知專用寫工具
autoVerify true 有完成 blocker 時允許 agent/turn-stopping 追加有限驗證步驟
maxAutomaticContinuations 1 每個 turn 的自動驗證續步上限
benchmarkRequired false 爲性能任務強制當前 revision 的完整 benchmark
ledger true 寫入本地 policy ledger,不進入模型歷史

先做 shadow 觀察決策是否符合真實會話:

mode: shadow
ledger: true

確認 relation / phase 判斷合理後,再切換爲 activeadaptiveReasoning 默認關閉,因爲改變 reasoning effort 會改變 request header 與緩存形狀;應在具體 provider / model 上完成校準後再啓用。

性能任務示例配置:

benchmarkRequired: true
benchmarkToolNames: [run_benchmark]
verificationToolNames: [build_project, run_correctness_test]
finishToolNames: [finish]
fullBenchmarkMinQueries: 10000
fullBenchmarkMinRecall: 0.95
benchmarkScoreTolerancePercent: 2

適用場景與注意

適合誰

  • 已在 DSH 上跑長鏈路 Agent,希望用事件流而非純 prompt 約束「先觀察再改、改完要驗證、性能改動要有完整 benchmark」的團隊;
  • 需要 Task Episode 連續性判斷、驗證債務與 finish gate 的工作流場景;
  • 使用 Native tools 或 Code Mode,且工具名可按 harness 配置(benchmarkToolNamesverificationToolNamesfinishToolNames)。

注意事項

  • 插件以當前 dsh 進程權限運行,安裝前應閱讀 源碼 與 MIT 許可證,確認 bundle patch 與默認配置符合你的環境。
  • Governor 是軌跡策略層,不能替代 sandbox 與人工 approval;高風險 shell 寫入只會創建驗證債務,不會單獨充當安全邊界。
  • SkillHub(目錄頁)是社區插件目錄,與 DeepSeek / 幻方無官方從屬關係;DSH 生態理念是「一切皆插件」,本插件是其中一條工作流向的補充。

結尾

dsh-trajectory-governor 把 Task Episode、驗證債務、benchmark gate 與完成門檻接到 Harness 真實事件流上,讓 Agent 軌跡從「靠 prompt 自律」變成可配置、可記賬、可逐步上線的閉環策略。建議先用 shadow 模式對照 ledger,再切 active;性能類任務再單獨打開 benchmarkRequired

羽毛球分组比赛记分
小程序二维码

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

小夜