@gluztm/dsh-guard 2.4.1 → 2.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +38 -58
- package/README.md +41 -60
- package/bin/dsh-guard.mjs +160 -47
- package/lib/client.js +1942 -1103
- package/lib/governance.mjs +994 -0
- package/lib/index.mjs +396 -53
- package/lib/inspector.mjs +1820 -429
- package/package.json +7 -3
- package/screenshots.json +1 -1
package/README.en.md
CHANGED
|
@@ -1,64 +1,44 @@
|
|
|
1
|
-
# @gluztm/dsh-guard
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
- **
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
- `dsh_guard_snapshots`: Pre-change snapshot listing filtered by track
|
|
31
|
-
- `dsh_guard_open`: Locate or launch local Windows `DSHGuard.exe`, or return the official release link
|
|
32
|
-
- `/guard [status|snapshots|open]`: Quick slash-command access
|
|
33
|
-
|
|
34
|
-
> Scoped package note: the unscoped name `dsh-guard` is already taken by another author on npm, so this official companion package uses `@gluztm/dsh-guard` and distributes signed release `.tgz` tarballs directly via [GitHub Release 2.4.1](https://github.com/gluztm/DSHGuard/releases/tag/2.4.1) alongside `DSHGuard-Setup-2.4.1.exe`.
|
|
1
|
+
# @gluztm/dsh-guard
|
|
2
|
+
|
|
3
|
+
> Cross-platform DSH control plane and companion governance plugin for **DeepSeek Harness (DSH)**. Runs directly inside DSH Web and Desktop without external installers: **auto-detects the running host profile (`desktop` on Desktop, `web` on Web)** with zero cross-profile noise, toggles plugins/MCP & tunes timeouts with local loopback probing, governs Skills/context footprint, blind-writes `.credentials.yaml` refs, and maintains pinned YAML/JSON snapshots with redacted audit logs.
|
|
4
|
+
|
|
5
|
+
[中文文档](./README.md) · [GitHub Repository](https://github.com/gluztm/DSHGuard) · [Roadmap](https://github.com/gluztm/DSHGuard/blob/main/docs/ROADMAP-COMPANION.md) · [Releases](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3)
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Built-In Capabilities
|
|
10
|
+
|
|
11
|
+
1. **Single-Track Host Auto-Detection & Broken Symlink Repair**
|
|
12
|
+
- Automatically locks onto the active DSH host track (`desktop` when launched inside DSH Desktop, `web` when launched in DSH Web), omitting `headless`, custom directories, and cross-profile drift alerts.
|
|
13
|
+
- Detects broken symlinks / NTFS junctions under `node_modules` and relinks valid targets from `.dsh-module-fallback` or sibling profiles in pure Node.js; any unresolvable links return HTTP 200 with a clear list instead of failing with HTTP 400.
|
|
14
|
+
2. **Plugin & MCP Switchboard, Timeout Tuning & Safe Local Probe**
|
|
15
|
+
- Resolves `dsh.bundle.patch` / `cordis.patch.yml` loader IDs and `@deepseek-ai/dsh-mcp-client` entries while protecting core `@deepseek-ai/*` modules and the companion plugin itself.
|
|
16
|
+
- Losslessly tunes per-MCP `toolCallTimeoutMs` (`5000`–`600000 ms`) with automatic pre-change `auto-mcp` snapshots.
|
|
17
|
+
- Probes MCP health safely without side effects: HTTP/SSE servers are probed **strictly on loopback addresses** (`127.0.0.1` / `localhost` / `::1`, SSRF-safe) via TCP connect; `stdio` entries are checked on disk for executable and script path readiness.
|
|
18
|
+
3. **Startup Context Footprint Audit & Skills Visibility Governance**
|
|
19
|
+
- Estimates the active profile's startup token overhead across active third-party plugins, enabled MCP servers, visible Skills, and permanent prompt files (`prompt-inject.md`, `MEMORY.md`, `prompts/*.md`, `system-prompt`).
|
|
20
|
+
- Scans `~/.dsh/skills`, `~/.agents/skills`, and read-only plugin skills (`SKILL.md`), allowing lossless toggles of `disable-model-invocation` and `user-invocable` frontmatter flags with SHA256 fingerprint conflict detection.
|
|
21
|
+
4. **`llm-pi-ai` Inspection & One-Way `.credentials.yaml` Blind Write**
|
|
22
|
+
- Displays configured providers, protocols, endpoints, model counts, and key readiness for the active profile; includes 7 mainstream provider env-key presets in the add-key drawer.
|
|
23
|
+
- **Zero secret readback**: writes user-entered API keys strictly one-way into `.credentials.yaml` `refs:` while preserving `version`, `records`, comments, and protected `*_ACCOUNT_*` OAuth blocks (`redLines: []`, `secretTouches: []` in `dsh-trust-check`).
|
|
24
|
+
5. **Pinned Snapshots, Live SHA256 Diff Rollback & Redacted Audit Log**
|
|
25
|
+
- Backs up `cordis.patch.yml`, `cordis.yml`, `package.json`, and `settings.yaml` into `~/.dsh/guard-snapshots` (strictly excluding `.credentials.yaml`), supports **📌 Pin** protection against auto-pruning, 1-click whitelist rollback, and redacted mutation tracking in `~/.dsh/guard-state/audit.jsonl`.
|
|
26
|
+
6. **6 Agent Tools, Slash Command & CLI**
|
|
27
|
+
- Tools: `dsh_guard_status`, `dsh_guard_context_audit`, `dsh_guard_mcp_probe`, `dsh_guard_snapshots`, `dsh_guard_backup`, `dsh_guard_open`
|
|
28
|
+
- Slash command: `/guard [status|context|mcp|snapshots|backup [web|desktop]|open]`
|
|
29
|
+
- CLI: `dsh-guard status [--target web|desktop] [--json]`, `dsh-guard context [--json]`, `dsh-guard mcp-probe [--json]`, `dsh-guard snapshots`, `dsh-guard backup [web|desktop]`, `dsh-guard repair [web|desktop]`, `dsh-guard audit [--json]`
|
|
35
30
|
|
|
36
31
|
---
|
|
37
32
|
|
|
38
|
-
##
|
|
33
|
+
## Architecture & Security Boundaries (When Native Desktop App Is Needed)
|
|
39
34
|
|
|
40
|
-
|
|
41
|
-
# Add to DSH Web profile (Sidebar UI + Agent tools + /guard)
|
|
42
|
-
dsh plugin --profile web add https://github.com/gluztm/DSHGuard/releases/download/2.4.1/dsh-guard-2.4.1.tgz
|
|
43
|
-
|
|
44
|
-
# Add to DSH Desktop profile
|
|
45
|
-
dsh plugin --profile desktop add https://github.com/gluztm/DSHGuard/releases/download/2.4.1/dsh-guard-2.4.1.tgz
|
|
46
|
-
|
|
47
|
-
# Pair with the Windows Desktop Control Shell (Lifecycle / Credential Vault / Snapshots)
|
|
48
|
-
# https://github.com/gluztm/DSHGuard/releases/download/2.4.1/DSHGuard-Setup-2.4.1.exe
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Standalone CLI (Node.js >= 18)
|
|
52
|
-
|
|
53
|
-
```powershell
|
|
54
|
-
node packages/dsh-guard/bin/dsh-guard.mjs status
|
|
55
|
-
node packages/dsh-guard/bin/dsh-guard.mjs snapshots --json
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
---
|
|
35
|
+
Routine active-profile inspection, extension & context governance, local MCP probing, blind-write credential setup, and YAML config snapshots all run directly inside `@gluztm/dsh-guard`. A smaller set of OS-level / out-of-band workflows cannot safely execute inside a live DSH host plugin and live exclusively in the standalone **[DSHGuard Desktop (Windows)](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3)** app:
|
|
59
36
|
|
|
60
|
-
|
|
37
|
+
| Area | Handled Inside Companion Plugin | Exclusive to Native Desktop App (Why) |
|
|
38
|
+
| :--- | :--- | :--- |
|
|
39
|
+
| **Extension Lifecycle** | Toggle plugins, MCP & Skills; tune MCP timeouts; local probe; repair symlinks | **Fresh install / full uninstall / force kill locks**: running `npm install` or deleting `node_modules` inside the live host tears loaded modules and hits file locks; plugins should also never pull remote installers or terminate the host process. |
|
|
40
|
+
| **Secrets & Migration** | Key existence audit + one-way write to `.credentials.yaml` | **Plaintext vault, Windows DPAPI & CC Switch import**: Web UI plugins with `fetch` are barred from reading back secrets; hardware-bound DPAPI encryption and cross-tool migrations rely on native Windows security APIs. |
|
|
41
|
+
| **System Rollback** | Live diff, pinning & rollback of YAML/JSON configs | **Full `node_modules` ZIP recovery & crash recovery**: restoring full package trees and relaunching DSH requires an external supervisor running out of process. |
|
|
61
42
|
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
- **Strictly Read-Only**: Never mutates DSH profiles or third-party plugins. Config/plugin mutations, snapshot rollback, and CC Switch import remain isolated in the out-of-band Windows `DSHGuard.exe` supervisor.
|
|
43
|
+
- When `DSHGuard.exe` is detected locally on Windows, the header shows an unobtrusive **Open Desktop** action.
|
|
44
|
+
- Windows users who need those out-of-band capabilities can obtain [DSHGuard-Setup-2.4.3.exe](https://github.com/gluztm/DSHGuard/releases/download/2.4.3/DSHGuard-Setup-2.4.3.exe) from [Release v2.4.3](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3).
|
package/README.md
CHANGED
|
@@ -1,68 +1,49 @@
|
|
|
1
|
-
# @gluztm/dsh-guard
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
- `dsh_guard_open`:定位或唤起本机 Windows `DSHGuard.exe`(未安装时返回官方下载链接)
|
|
32
|
-
- `/guard [status|snapshots|open]`:聊天内直达命令
|
|
33
|
-
|
|
34
|
-
> 为什么包名是 `@gluztm/dsh-guard`?因为无作用域名称 `dsh-guard` 在 npm 上已被其他作者占用,为避免社区清单路由冲突,伴生插件统一采用 `@gluztm/dsh-guard` 命名并随 [GitHub Release 2.4.1](https://github.com/gluztm/DSHGuard/releases/tag/2.4.1) 直接分发签名 `.tgz` 安装包;桌面端安装包 `DSHGuard-Setup-2.4.1.exe` 亦在同一 Release 页分发。
|
|
1
|
+
# @gluztm/dsh-guard
|
|
2
|
+
|
|
3
|
+
> 面向 **DeepSeek Harness (DSH)** 的跨平台运行时底座治理与伴生控制面板:无需额外安装独立软件,**自动识别当前宿主运行端(桌面端仅看 `desktop`、Web 端仅看 `web`)**,直接在 Web 侧边栏与 CLI 内完成 **健康巡检与断链自愈**、**插件/MCP 启停与超时调优及本地探活**、**上下文与 Skills 可见性治理**、**`.credentials.yaml` 单向盲写** 及 **本地配置快照置顶 + 脱敏审计回滚**。
|
|
4
|
+
|
|
5
|
+
[English Documentation](./README.en.md) · [GitHub 仓库](https://github.com/gluztm/DSHGuard) · [演进计划书](https://github.com/gluztm/DSHGuard/blob/main/docs/ROADMAP-COMPANION.md) · [Releases](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3)
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## 伴生插件内置核心能力
|
|
10
|
+
|
|
11
|
+
1. **当前运行端自动识别与断链自愈**
|
|
12
|
+
- 自动识别当前 DSH 宿主运行端(桌面端运行自动绑定 `desktop`,Web 端运行自动绑定 `web`),仅聚焦展示并管理当前活跃档案,不扫描 `headless` 或私有目录,彻底消除跨端对比冗余与漂移噪音。
|
|
13
|
+
- 检测 `node_modules` 下失效的符号链接 / NTFS Junction,自动从 `.dsh-module-fallback` 或兄弟档案恢复;若个别断链已无法从本地找到有效源,仍安全完成可修项目并返回清晰清单提示(不再报 HTTP 400)。
|
|
14
|
+
2. **插件与 MCP 全生命周期启停、超时微调与本地安全探活**
|
|
15
|
+
- 自动解析 `dsh.bundle.patch` / `cordis.patch.yml` 的 `loaderId` 及 `@deepseek-ai/dsh-mcp-client` 服务,保护 `@deepseek-ai/*` 核心组件与插件自身。
|
|
16
|
+
- 支持无损调节单个 MCP 的 `toolCallTimeoutMs`(`5000`–`600000 ms`),改动前自动保留 `auto-mcp` 快照。
|
|
17
|
+
- 提供只读本地 MCP 探活:HTTP/SSE 型**严格限制仅探测回环地址**(`127.0.0.1` / `localhost` / `::1`)防 SSRF;`stdio` 型校验本地命令与入口脚本文件存在性,不执行第三方命令。
|
|
18
|
+
3. **上下文负担体检与 Skills 可见性治理**
|
|
19
|
+
- 汇总当前运行端启用的第三方插件、MCP 工具与常驻提示词(`prompt-inject.md`、`MEMORY.md`、`prompts/*.md`、`system-prompt`)估算基线 Token 与负担等级,提供精简建议。
|
|
20
|
+
- 扫描 `~/.dsh/skills`、`~/.agents/skills` 与插件内置 `SKILL.md`(插件内只读保护),允许对用户技能无损切换 `disable-model-invocation` 与 `user-invocable` 前置元数据开关(保存前比对 SHA256 指纹并保留原换行符/BOM)。
|
|
21
|
+
4. **模型提供商巡检与 `.credentials.yaml` 纯单向盲写**
|
|
22
|
+
- 汇总当前运行档案中的 `llm-pi-ai` 协议(`anthropic-messages` / `openai-completions` / `google-generative-ai`)、`baseURL` 与凭据就绪状态;内置 7 套通用主流平台预设快速填入环境变量名。
|
|
23
|
+
- **零明文回显**:保留 `version`、`records`、`*_ACCOUNT_*` 受保护 OAuth 块,响应报文绝不返回密钥字符(通过 `dsh-trust-check` 静态扫描零红线)。
|
|
24
|
+
5. **配置快照置顶、SHA256 差异回滚与脱敏审计流水**
|
|
25
|
+
- 备份 `cordis.patch.yml`、`cordis.yml`、`package.json`、`settings.yaml` 至 `~/.dsh/guard-snapshots`(严格排除密钥文件),支持 **📌 置顶保护**(免受自动轮转清理)与白名单一键还原。
|
|
26
|
+
- 每次通过伴生插件完成的变更自动写入 `~/.dsh/guard-state/audit.jsonl`(纯结构化脱敏记录,绝不含密钥值)。
|
|
27
|
+
6. **内置 6 个 Agent 工具 + `/guard` 斜杠命令 + CLI**
|
|
28
|
+
- Agent 工具:`dsh_guard_status`、`dsh_guard_context_audit`、`dsh_guard_mcp_probe`、`dsh_guard_snapshots`、`dsh_guard_backup`、`dsh_guard_open`
|
|
29
|
+
- 斜杠命令:`/guard`、`/guard status`、`/guard context`、`/guard mcp`、`/guard snapshots`、`/guard backup [web|desktop]`、`/guard open`
|
|
30
|
+
- CLI:`dsh-guard status [--target web|desktop] [--json]`、`dsh-guard context [--json]`、`dsh-guard mcp-probe [--json]`、`dsh-guard snapshots`、`dsh-guard backup [web|desktop]`、`dsh-guard repair [web|desktop]`、`dsh-guard audit [--json]`
|
|
35
31
|
|
|
36
32
|
---
|
|
37
33
|
|
|
38
|
-
##
|
|
34
|
+
## 技术与安全边界说明(何时需要原生桌面端)
|
|
39
35
|
|
|
40
|
-
|
|
36
|
+
本伴生插件已经覆盖当前运行端的日常巡检、扩展与上下文治理、MCP 本地探活、密钥盲写与配置快照回滚需求;以下能力由于操作系统或安全架构限制,**无法在运行中的 DSH Node 宿主插件内部安全实现**,仅在独立进程外原生程序 **[DSHGuard Desktop (Windows)](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3)** 中提供:
|
|
41
37
|
|
|
42
|
-
|
|
38
|
+
| 能力领域 | 伴生插件内置能力 | 仅独立原生桌面端提供(及原因) |
|
|
39
|
+
| :--- | :--- | :--- |
|
|
40
|
+
| **插件包生命周期** | 安全切换插件/MCP/Skills 开关、超时调优、本地探活、断链修复 | **全新安装 / 彻底卸载 / 强杀锁死进程**:运行时在宿主进程内删除或重装 `node_modules` 会撕裂已加载模块并触发文件占用锁死;且插件不应持有远端包拉取与进程强杀权。 |
|
|
41
|
+
| **凭据安全与导入** | `.credentials.yaml` 键名审计 + 单向盲写 | **明文编辑 / Windows DPAPI 硬件加密库 / CC Switch 导入**:Web 插件拥有前端请求能力,依据安全合规禁止反向回显明文;DPAPI 与跨工具配置迁移依赖 Windows 原生安全 API。 |
|
|
42
|
+
| **系统级备份恢复** | YAML/JSON 配置文件实时对比、置顶保护与白名单回滚 | **整包扩展目录 ZIP 归档 + 主程序崩溃带外恢复**:涵盖整套 `node_modules` 与主程序启停的重型恢复必须脱离 DSH 宿主进程独立调度。 |
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
dsh plugin --profile web add https://github.com/gluztm/DSHGuard/releases/download/2.4.1/dsh-guard-2.4.1.tgz
|
|
47
|
-
|
|
48
|
-
# 同步安装到 DSH 桌面版
|
|
49
|
-
dsh plugin --profile desktop add https://github.com/gluztm/DSHGuard/releases/download/2.4.1/dsh-guard-2.4.1.tgz
|
|
50
|
-
|
|
51
|
-
# 配套安装 Windows 完整桌面端管控壳(含启停/凭据保险箱/自动快照回滚)
|
|
52
|
-
# https://github.com/gluztm/DSHGuard/releases/download/2.4.1/DSHGuard-Setup-2.4.1.exe
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### 方式 B:从源码子包本地运行只读 CLI(Node.js >= 18)
|
|
56
|
-
|
|
57
|
-
```powershell
|
|
58
|
-
node packages/dsh-guard/bin/dsh-guard.mjs status
|
|
59
|
-
node packages/dsh-guard/bin/dsh-guard.mjs snapshots --json
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
---
|
|
44
|
+
- 若本机已安装 Windows 原生桌面端,插件右上角会自动显示「打开桌面端」快捷入口。
|
|
45
|
+
- 仅需要上述带外桌面功能时,可在 [GitHub Release v2.4.3](https://github.com/gluztm/DSHGuard/releases/tag/2.4.3) 获取 [DSHGuard-Setup-2.4.3.exe](https://github.com/gluztm/DSHGuard/releases/download/2.4.3/DSHGuard-Setup-2.4.3.exe)(源码可审计,基于 .NET 10 + WPF 原生构建)。
|
|
63
46
|
|
|
64
|
-
##
|
|
47
|
+
## 许可协议
|
|
65
48
|
|
|
66
|
-
-
|
|
67
|
-
- **同源校验鉴权**:服务端暴露的 `/api/dsh-guard/overview` 与 `/api/dsh-guard/open` 路由复用 DSH 的 `connection.requestRejection` 校验,仅使用服务端 `DSH_HOME` / `DSHGUARD_HOME` 环境变量路径,不接受外部路径参数。
|
|
68
|
-
- **只读原则**:本插件不会改写 DSH 配置、卸载或更新第三方插件;所有高风险变更(装卸更新、快照回滚、CC Switch 多源导入)均交由进程外的 Windows 桌面壳 `DSHGuard.exe` 先自动存档后隔离执行。
|
|
49
|
+
GPL-3.0 © gluztm
|
package/bin/dsh-guard.mjs
CHANGED
|
@@ -1,66 +1,179 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
+
PACKAGE_VERSION,
|
|
4
|
+
createSnapshot,
|
|
5
|
+
detectActiveProfile,
|
|
6
|
+
formatContextAuditText,
|
|
3
7
|
formatHealthText,
|
|
8
|
+
formatMcpProbeText,
|
|
4
9
|
formatSnapshotsText,
|
|
10
|
+
inspectContextAudit,
|
|
5
11
|
inspectHealth,
|
|
6
12
|
inspectSnapshots,
|
|
13
|
+
normalizeProfileTarget,
|
|
7
14
|
openGuard,
|
|
15
|
+
probeMcpServers,
|
|
16
|
+
readAuditLog,
|
|
17
|
+
repairBrokenModuleLinks,
|
|
18
|
+
resolveDshHome,
|
|
8
19
|
} from '../lib/inspector.mjs'
|
|
9
20
|
|
|
10
21
|
function parseArgs(argv) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let help = false
|
|
21
|
-
|
|
22
|
-
for (let i = 0; i < args.length; i++) {
|
|
23
|
-
const a = args[i]
|
|
24
|
-
if (a === '--help' || a === '-h') help = true
|
|
25
|
-
else if (a === '--json') json = true
|
|
26
|
-
else if (a === '--launch') launch = true
|
|
27
|
-
else if (a === '--dsh-home' && i + 1 < args.length) dshHome = args[++i]
|
|
28
|
-
else if (a === '--guard-home' && i + 1 < args.length) guardHome = args[++i]
|
|
29
|
-
else if (a === '--snapshots-dir' && i + 1 < args.length) snapshotsDir = args[++i]
|
|
30
|
-
else if (a === '--target' && i + 1 < args.length) target = args[++i]
|
|
31
|
-
else if (a === '--limit' && i + 1 < args.length) limit = Number(args[++i]) || 20
|
|
32
|
-
else if (!a.startsWith('-')) command = a
|
|
22
|
+
const opts = {
|
|
23
|
+
command: 'status',
|
|
24
|
+
json: false,
|
|
25
|
+
launch: false,
|
|
26
|
+
target: '',
|
|
27
|
+
limit: 20,
|
|
28
|
+
dshHome: undefined,
|
|
29
|
+
guardHome: undefined,
|
|
30
|
+
help: false,
|
|
33
31
|
}
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const positionals = []
|
|
33
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
34
|
+
const arg = argv[i]
|
|
35
|
+
if (arg === '--json') {
|
|
36
|
+
opts.json = true
|
|
37
|
+
} else if (arg === '--launch') {
|
|
38
|
+
opts.launch = true
|
|
39
|
+
} else if (arg === '-h' || arg === '--help') {
|
|
40
|
+
opts.help = true
|
|
41
|
+
} else if (arg === '--target' && i + 1 < argv.length) {
|
|
42
|
+
opts.target = argv[++i]
|
|
43
|
+
} else if (arg.startsWith('--target=')) {
|
|
44
|
+
opts.target = arg.slice('--target='.length)
|
|
45
|
+
} else if (arg === '--limit' && i + 1 < argv.length) {
|
|
46
|
+
opts.limit = Number(argv[++i]) || 20
|
|
47
|
+
} else if (arg.startsWith('--limit=')) {
|
|
48
|
+
opts.limit = Number(arg.slice('--limit='.length)) || 20
|
|
49
|
+
} else if (arg === '--dsh-home' && i + 1 < argv.length) {
|
|
50
|
+
opts.dshHome = argv[++i]
|
|
51
|
+
} else if (arg.startsWith('--dsh-home=')) {
|
|
52
|
+
opts.dshHome = arg.slice('--dsh-home='.length)
|
|
53
|
+
} else if (arg === '--guard-home' && i + 1 < argv.length) {
|
|
54
|
+
opts.guardHome = argv[++i]
|
|
55
|
+
} else if (arg.startsWith('--guard-home=')) {
|
|
56
|
+
opts.guardHome = arg.slice('--guard-home='.length)
|
|
57
|
+
} else if (!arg.startsWith('-')) {
|
|
58
|
+
positionals.push(arg)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (positionals.length > 0) {
|
|
62
|
+
opts.command = positionals[0].toLowerCase()
|
|
63
|
+
}
|
|
64
|
+
if (
|
|
65
|
+
positionals.length > 1 &&
|
|
66
|
+
(opts.command === 'backup' || opts.command === 'repair' || opts.command === 'context' || opts.command === 'mcp-probe')
|
|
67
|
+
) {
|
|
68
|
+
opts.target = positionals[1]
|
|
69
|
+
}
|
|
70
|
+
return opts
|
|
36
71
|
}
|
|
37
72
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
'dsh-guard — Companion read-only inspector & snapshot bridge for DSHGuard',
|
|
43
|
-
'',
|
|
44
|
-
'Usage:',
|
|
45
|
-
' dsh-guard status [--target all|web|desktop] [--dsh-home <dir>] [--guard-home <dir>] [--json]',
|
|
46
|
-
' dsh-guard snapshots [--target all|web|desktop] [--limit N] [--guard-home <dir>] [--json]',
|
|
47
|
-
' dsh-guard open [--launch] [--guard-home <dir>] [--json]',
|
|
48
|
-
].join('\n'))
|
|
49
|
-
process.exit(0)
|
|
73
|
+
function resolveCliTarget(rawTarget, dshHome) {
|
|
74
|
+
const explicit = normalizeProfileTarget(rawTarget)
|
|
75
|
+
if (explicit) return explicit
|
|
76
|
+
return detectActiveProfile({ dshHome, preferExistingOnCli: true })
|
|
50
77
|
}
|
|
51
78
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
function printHelp() {
|
|
80
|
+
console.log(
|
|
81
|
+
[
|
|
82
|
+
`dsh-guard v${PACKAGE_VERSION} — DSH 单端自动感知伴生管理工具`,
|
|
83
|
+
'',
|
|
84
|
+
'用法:',
|
|
85
|
+
' dsh-guard status [--target desktop|web] [--dsh-home PATH] [--json]',
|
|
86
|
+
' dsh-guard context [--target desktop|web] [--dsh-home PATH] [--json]',
|
|
87
|
+
' dsh-guard mcp-probe [--target desktop|web] [--dsh-home PATH] [--json]',
|
|
88
|
+
' dsh-guard snapshots [--target desktop|web] [--limit N] [--json]',
|
|
89
|
+
' dsh-guard backup [desktop|web] [--dsh-home PATH] [--json]',
|
|
90
|
+
' dsh-guard repair [desktop|web] [--dsh-home PATH] [--json]',
|
|
91
|
+
' dsh-guard audit [--limit N] [--dsh-home PATH] [--json]',
|
|
92
|
+
' dsh-guard open [--launch] [--json]',
|
|
93
|
+
].join('\n'),
|
|
94
|
+
)
|
|
56
95
|
}
|
|
57
96
|
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
97
|
+
async function main() {
|
|
98
|
+
const opts = parseArgs(process.argv.slice(2))
|
|
99
|
+
if (opts.help || opts.command === 'help') {
|
|
100
|
+
printHelp()
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const dshHome = resolveDshHome(opts.dshHome)
|
|
105
|
+
const activeTarget = resolveCliTarget(opts.target, dshHome)
|
|
106
|
+
|
|
107
|
+
if (opts.command === 'snapshots' || opts.command === 'snap') {
|
|
108
|
+
const report = inspectSnapshots({
|
|
109
|
+
dshHome,
|
|
110
|
+
guardHome: opts.guardHome,
|
|
111
|
+
target: activeTarget,
|
|
112
|
+
limit: opts.limit,
|
|
113
|
+
})
|
|
114
|
+
console.log(opts.json ? JSON.stringify(report, null, 2) : formatSnapshotsText(report))
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (opts.command === 'context' || opts.command === 'ctx') {
|
|
119
|
+
const health = inspectHealth({ dshHome, guardHome: opts.guardHome, activeProfile: activeTarget })
|
|
120
|
+
const report = inspectContextAudit(dshHome, health, activeTarget)
|
|
121
|
+
console.log(opts.json ? JSON.stringify(report, null, 2) : formatContextAuditText(report))
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (opts.command === 'mcp-probe' || opts.command === 'mcp') {
|
|
126
|
+
const health = inspectHealth({ dshHome, guardHome: opts.guardHome, activeProfile: activeTarget })
|
|
127
|
+
const report = await probeMcpServers(dshHome, health.extensions, activeTarget)
|
|
128
|
+
console.log(opts.json ? JSON.stringify(report, null, 2) : formatMcpProbeText(report))
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (opts.command === 'audit') {
|
|
133
|
+
const records = readAuditLog(dshHome, opts.limit)
|
|
134
|
+
if (opts.json) {
|
|
135
|
+
console.log(JSON.stringify({ total: records.length, records }, null, 2))
|
|
136
|
+
} else if (records.length === 0) {
|
|
137
|
+
console.log('暂无变更审计记录。')
|
|
138
|
+
} else {
|
|
139
|
+
for (const r of records) {
|
|
140
|
+
console.log(`[${r.ts}] (${r.action}/${r.target}) ${r.summary}${r.snapshotId ? ` [snap=${r.snapshotId}]` : ''}`)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (opts.command === 'backup') {
|
|
147
|
+
const res = createSnapshot(dshHome, activeTarget, 'manual', 'CLI dsh-guard backup')
|
|
148
|
+
if (opts.json) {
|
|
149
|
+
console.log(JSON.stringify(res, null, 2))
|
|
150
|
+
} else {
|
|
151
|
+
console.log(`已创建配置快照 ${res.snapshot.id}(${res.snapshot.target} · ${res.snapshot.itemCount} 个文件)`)
|
|
152
|
+
}
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (opts.command === 'repair') {
|
|
157
|
+
const res = repairBrokenModuleLinks(dshHome, activeTarget)
|
|
158
|
+
if (opts.json) {
|
|
159
|
+
console.log(JSON.stringify(res, null, 2))
|
|
160
|
+
} else {
|
|
161
|
+
console.log(res.message || `断链修复 (${res.target}): 已修复 ${res.repairedCount},未解析 ${res.unresolvableCount}`)
|
|
162
|
+
}
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (opts.command === 'open') {
|
|
167
|
+
const res = openGuard({ guardHome: opts.guardHome, launch: opts.launch })
|
|
168
|
+
console.log(opts.json ? JSON.stringify(res, null, 2) : res.message)
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const health = inspectHealth({ dshHome, guardHome: opts.guardHome, activeProfile: activeTarget })
|
|
173
|
+
console.log(opts.json ? JSON.stringify(health, null, 2) : formatHealthText(health))
|
|
62
174
|
}
|
|
63
175
|
|
|
64
|
-
|
|
65
|
-
console.
|
|
66
|
-
process.
|
|
176
|
+
main().catch((err) => {
|
|
177
|
+
console.error(err instanceof Error ? err.message : String(err))
|
|
178
|
+
process.exitCode = 1
|
|
179
|
+
})
|