dsh-direnv:爲 DSH 會話注入工作區 direnv 環境

前言

在 DeepSeek Harness(DSH)裏跑 shell 命令時,子進程默認繼承宿主進程的環境,不會自動執行項目根目錄下的 .envrc。若你本地已用 direnv 管理 API_BASE、工具鏈路徑等變量,智能體執行的命令往往拿不到同一套環境,排查時容易誤以爲是代碼或腳本問題。dsh-direnv 在 DSH 側按「會話工作區」解析 direnv,把 direnv export json 的結果合併進每條命令的環境變量,並在 .envrc 未批准時走 DSH 審批通道,而不是把未授權內容暴露給模型。

下面介紹插件定位、能力與安裝配置方式。事實以 GitHub 倉庫 README 爲準。

這是什麼

dsh-direnv(npm 包名 @snylonue/dsh-direnv)由維護者 snylonue 發佈,分類爲客戶端插件,簡介爲「direnv support for DeepSeek Harness」。當前 package 版本爲 0.4.0,許可證 MIT。

插件只支持 DSH 0.2.1-rc.2;更早的 host 版本範圍已移除。開發與集成測試均針對該 host 版本。direnv 快照綁定於會話創建時的工作區,不會隨後續 working_directory 變更或單條命令的 cwd 而切換;重載與批准始終針對這一固定工作區。

核心功能

環境注入

行爲 說明
注入 將 direnv export json 在工作區上的結果合併進子進程環境變量
作用範圍 工作區由調用方 agent 的 session cwd 固定
會話啓動提示 會話開始時向模型注入一條快照:說明工作區 direnv 是否生效、注入了哪些變量名(不展示值);無 .envrc 的工作區保持靜默
批准後再加載 direnv_allow 經 DSH 審批通道執行,通過後再運行 direnv allow

命令執行時只讀取已解析好的環境;若 direnv 解析在啓動階段失敗,錯誤會被記錄,但不會阻止會話啓動。

會話啓動時的模型上下文

會話創建時,插件解析工作區一次,並在首輪對話前通過 agent/created 與 agent.inject() 排隊一條面向模型的上下文。模型無需先跑命令即可知道 direnv 狀態。已激活時示例(README 原文):

[dsh-direnv] The workspace direnv environment is active.
[dsh-direnv] /home/me/proj/.envrc injects 3 variable(s) into every command the agent runs:
[dsh-direnv]   API_BASE, PROJECT_TOOLCHAIN, RUST_LOG
[dsh-direnv] Values are applied to each command's environment and are deliberately not shown here.

被阻止、拒絕或失敗的工作區會使用與命令側一致的可操作文案,便於模型直接調用 direnv_allow。compact 或 resume 之後會再發一條新消息。會話創建會等待該解析完成再進入首輪;將 sessionContext 設爲 false 可關閉這條消息,工作區仍會解析。

未批准 .envrc 時的行爲

未批准的 .envrc 不會導致命令失敗,也不會泄露文件內容;命令在無 direnv 環境的情況下運行。若配置 notifyOnBlocked: true,會在命令結果的 stderr 追加提示,例如:

[dsh-direnv] This command ran WITHOUT the workspace direnv environment.
[dsh-direnv] /home/me/proj/.envrc is not approved, so native direnv refused to load it.
[dsh-direnv] Call the direnv_allow tool to ask the user to approve this workspace:
[dsh-direnv]   direnv_allow
[dsh-direnv] Workspace: /home/me/proj

模型可調用 direnv_allow(可選 reason),由用戶批准當前會話工作區;host 在工作區執行 direnv allow,且不讀取或預覽 .envrc 內容。

顯式重載

啓動時會於固定工作區運行 direnv status --json 與 direnv export json 並等待結果。環境變更後需通過面向模型的工具顯式重載:

direnv_reload                          # reload the calling session workspace

安裝與啓用

DSH 生態採用「一切皆插件」;社區目錄頁在 SkillHub,與 DeepSeek / 幻方無官方從屬關係,安裝前請自行查看源碼與許可證。

1、確認本機已安裝 direnv,且 DSH 版本爲 0.2.1-rc.2。

2、使用 web profile 從 npm 安裝(README 原文命令):

dsh plugin --profile web add @snylonue/dsh-direnv

3、安裝後必須重啓 DSH 插件纔會生效。

卸載:

dsh plugin --profile web remove @snylonue/dsh-direnv

配置

在 Cordis 插件行中增加或調整如下片段(disabled: true 寫在插件行外層,不在 config 內,用於關閉 provider 及其依賴的 adapter 與工具):

- id: direnv
  name: dsh-direnv
  config:
    executable: direnv          # bare PATH name or absolute path
    probeTimeoutMs: 10000       # budget for one direnv run
    notifyOnBlocked: false      # opt in to append actionable notices to results
    sessionContext: true        # inject the workspace's direnv variables at session start
字段 默認值 含義
executable direnv direnv 可執行文件;須在 PATH 中或爲絕對路徑
probeTimeoutMs 10000 單次 direnv status --json 或 direnv export json 超時上限(毫秒)
notifyOnBlocked false 爲 true 時在命令結果 stderr 追加可操作提示
sessionContext true 爲 true 時會話啓動注入工作區 direnv 變量名或失敗原因

典型用法

1、在項目根放置 .envrc,本地先用 direnv 習慣完成編輯;在 DSH 中打開以該目錄爲 session cwd 的會話。

2、若會話啓動上下文或 notifyOnBlocked 提示未批准,由模型調用 direnv_allow,用戶在 DSH 審批通道確認;host 在工作區執行 direnv allow。

3、修改 .envrc 或 direnv 狀態後,調用 direnv_reload 對當前會話工作區重新解析並更新注入環境。

適用場景與注意

適合已在倉庫中使用 direnv、且希望 DSH 智能體執行的 shell 命令與本地終端共享同一套工作區變量的開發者。

使用前請確認:executable 指向的 direnv 可用;探測超時可通過 probeTimeoutMs 調整。插件以當前 DSH 進程權限運行 direnv 與子進程,安裝與啓用前請閱讀 GitHub 源碼 與 MIT 許可證。圖標使用 direnv logo(Peter Waller,CC BY 4.0)。

結尾

dsh-direnv 將會話工作區與 direnv 對齊:啓動時告知模型變量名、命令側注入環境、未批准時走用戶審批而不泄露 .envrc 內容。目錄頁:SkillHub - dsh-direnv;源碼與完整說明:github.com/snylonue/dsh-direnv。

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

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

小夜