前言¶
在 DeepSeek Harness(DSH)裏讓智能體「把測試跑綠」,有時它會用你不希望的方式達成目標:給失敗用例加 .skip、註釋掉斷言、把 expect(total).toBe(42) 改成 expect(total).toBeDefined()。測試報告變綠,缺陷仍在。人工每次 diff 都盯一遍成本高,單靠提示詞也不穩定。
dsh-protect-tests 是社區維護的 DSH 插件(目錄分類爲 admin-security),在每次對測試文件的編輯落地前對比新舊文本;若變更讓測試「檢查得更少」,則拒絕該編輯並向模型說明原因。下面介紹它的定位、能力與安裝配置方式。
這是什麼¶
- 名稱:
dsh-protect-tests(GitHub:acus9345/dsh-protect-tests) - 維護者:acus9345
- 許可證:MIT(據倉庫 README)
- 一句話:DeepSeek Harness 插件,攔截削弱測試的 agent 編輯,例如
.skip、刪除或稀釋斷言。
插件以 DSH 的 Cordis 補丁形式掛載,安裝後對匹配規則的測試文件編輯生效;正常增刪用例、加斷言、重命名、整理 setup 等不受影響。
核心功能¶
攔截時機與反饋¶
編輯測試文件時,插件在變更應用前比較舊文與新文。若判定爲削弱測試,會拒絕並返回說明,例如:
dsh-protect-tests blocked this change to src/sum.test.ts because it makes the tests check less:
- replaces specific assertions with existence/truthiness checks (0 → 1): `expect(sum(1, 2)).toBeDefined()`
Tests should keep failing until the code is fixed. Fix the implementation instead, or make
the test more accurate without weakening it. If the test itself is wrong (the requirement
changed, or it tested a bug), tell the user which test you are changing and why, then
repeat the identical call once to confirm.
模型應改實現而非弱化測試;若測試本身有誤,需向用戶說明要改哪條測試及原因,再重複一次相同調用以確認,第二次可通過(allowIdenticalRetry 默認爲 true)。
能識別的模式(據 README)¶
- 新增 skip:
it.skip、test.only、xit、xdescribe、@pytest.mark.skip/xfail、@unittest.skip、t.Skip()、@Disabled、#[ignore],以及 xUnit、RSpec、PHPUnit、XCTest 等等價寫法。 - 測試或斷言變少:刪除用例,或單次編輯中刪除不少於閾值的斷言(默認
minAssertionsRemoved: 2、minTestsRemoved: 1,可配置)。 - 無法失敗的斷言:如
expect(true).toBe(true)、assert True、assertEquals(x, x)、assert!(true)。 - 註釋掉的斷言。
- 吞掉失敗:空
catch {}或except AssertionError: pass包住斷言。 - 弱化斷言:具體斷言換成
toBeDefined()、toBeTruthy()、assert.ok(x)、is not None等。 - 刪除測試文件:通過 shell 對測試文件或目錄執行
rm、git rm、unlink;Windows 上 PowerShell 的Remove-Item、del、rd亦在監視範圍內(guardDeletes)。 - 無法失敗的 test 腳本:修改
package.json的"test"爲echo、exit 0、... || true,或加入--passWithNoTests(guardTestScript)。
測試文件如何被識別¶
默認按文件名與路徑匹配,例如 *.test.*、*.spec.*、*_test.*、test_*.py、*Test.java、conftest.py,以及 tests/、__tests__/、spec/、e2e/ 等目錄下的文件。列表可通過 testFiles 覆蓋,支持排除(如 !tests/fixtures/**)。
README 說明:合併多個近似用例爲 test.each / @pytest.mark.parametrize / t.Run,或將多個 toBe 合併爲 toEqual({...}) 等 refactor,雖在紙面上像「變少」,一般會放行。
刻意不覆蓋的情況¶
插件基於模式而非語義。把 toBe(4) 改成 toBe(5) 以迎合錯誤實現、或重寫測試邏輯使其不再觸及缺陷,通常不會被攔。它主要針對常見偷懶手段,不能替代人工閱讀 diff。
安裝與啓用¶
DSH 按 profile 維護插件列表;web 對應 dsh web 的瀏覽器 UI,headless 用於腳本與 CI,acp 用於編輯器集成。你實際使用的每個 profile 都要單獨安裝。
在 web profile 下安裝:
dsh plugin --profile web add dsh-protect-tests
安裝後即生效,無需額外開關。卸載:
dsh plugin --profile web remove dsh-protect-tests
若你通過 headless 或 acp 運行 DSH,將 --profile web 換成 headless 或 acp 即可。
社區插件目錄頁:https://www.skillhub.cn/plugins/acus9345/dsh-protect-tests(獨立站點,與 DeepSeek / 幻方無官方從屬關係)。倉庫:https://github.com/acus9345/dsh-protect-tests。
配置¶
可在 DSH 側欄 Plugins 中點選 dsh-protect-tests,修改後 Save,下一次工具調用即生效,無需重啓。
或在 ~/.dsh/profiles/<profile>/cordis.patch.yml(僅當前 profile)或 ~/.dsh/cordis.patch.yml(全部 profile)中增加片段;未寫的鍵保持默認:
- id: protect-tests
config:
mode: deny # 或 ask:彈出審批
testFiles: ['*.test.*', '**/tests/**', '!tests/fixtures/**']
minAssertionsRemoved: 2
minTestsRemoved: 1
disabledSignals: [] # skip, fewer-assertions, fewer-tests, trivial-assert,
# commented-assert, swallowed-failure, weaker-assert
guardDeletes: true
guardTestScript: true
allowIdenticalRetry: true # false 時拒絕爲最終決定
shellTools: [bash, pwsh] # Windows 上 DSH 使用 pwsh
mode: ask 時,削弱類變更可走審批而非一律拒絕。
適用場景與注意¶
適合:在 DSH 中讓智能體修代碼、跑測試的團隊或個人,希望默認阻止「skip / 刪斷言 / 換弱斷言」類 reward hacking,同時保留在說明理由後二次確認改測試的路徑。
權限與安全:插件隨當前 DSH 進程權限運行,可攔截編輯並監視配置中的 shell 工具對測試路徑的刪除。安裝前請閱讀倉庫源碼與 MIT 許可證,確認符合你的安全策略;勿在未審閱的情況下在敏感倉庫盲目添加第三方插件。
侷限:無法防止語義層面的測試退化;關鍵變更仍應人工 review。
結尾¶
dsh-protect-tests 在測試文件編輯鏈路上加了一層模式級護欄:測試應持續失敗直到實現修好,而不是被悄悄改綠。需要時可在目錄頁查看條目,或在 GitHub 提 issue。