dsh-ruleset 0.0.0-stage → 0.2.1
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/CHANGELOG.md +36 -0
- package/DEVELOPMENT.md +121 -0
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/README.zh.md +131 -0
- package/client.js +822 -0
- package/cordis.patch.yml +10 -0
- package/docs/PREDICATE-PLAYBOOK.md +42 -0
- package/docs-schema.md +38 -0
- package/examples/policy.example.yaml +50 -0
- package/examples/policy.full.yaml +162 -0
- package/examples/policy.starter.yaml +65 -0
- package/lib/backups.js +77 -0
- package/lib/commands.js +38 -0
- package/lib/dedupe.js +22 -0
- package/lib/explain.js +28 -0
- package/lib/export.js +33 -0
- package/lib/extract.js +104 -0
- package/lib/facts.js +250 -0
- package/lib/findings.js +101 -0
- package/lib/guard.js +76 -0
- package/lib/history.js +71 -0
- package/lib/hygiene.js +86 -0
- package/lib/index.js +70 -0
- package/lib/init.js +29 -0
- package/lib/inject.js +64 -0
- package/lib/injected-log.js +30 -0
- package/lib/introspect.js +55 -0
- package/lib/judge.js +73 -0
- package/lib/llm-parse.js +131 -0
- package/lib/observe.js +132 -0
- package/lib/policy.js +57 -0
- package/lib/predicates.js +444 -0
- package/lib/report.js +66 -0
- package/lib/routes.js +62 -0
- package/lib/rules-edit.js +140 -0
- package/lib/run.js +109 -0
- package/lib/service.js +91 -0
- package/lib/source.js +93 -0
- package/lib/stats.js +63 -0
- package/lib/validate.js +73 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/package.json +68 -3
- package/schema/policy.schema.json +41 -0
- package/test/dedupe.test.mjs +19 -0
- package/test/guard.test.mjs +30 -0
- package/test/load.test.mjs +21 -0
- package/test/predicates.test.mjs +49 -0
- package/test/source.test.mjs +32 -0
- package/test/validate.test.mjs +33 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
Community listing and documentation polish.
|
|
6
|
+
|
|
7
|
+
- **listed** in `awesome-dsh-plugin/awesome-dsh-plugin` under Security & Permissions (merged 2026-11-18, PR #16379)
|
|
8
|
+
|
|
9
|
+
- submitted to `awesome-dsh-plugin/awesome-dsh-plugin` (plugin-market index source) and `kejixiaoliang/awesome-dsh-plugins`
|
|
10
|
+
- READMEs list community submissions and the GitHub install command while npm publishing is unavailable
|
|
11
|
+
|
|
12
|
+
## 0.2.0
|
|
13
|
+
|
|
14
|
+
Rule authoring and verification, end to end.
|
|
15
|
+
|
|
16
|
+
- web panel (card grid, three columns: redline / hard / soft) with drag-and-drop re-tiering written back to `policy.yaml`
|
|
17
|
+
- one-sentence rule authoring: model picks the predicate, deterministic fallback when the model is unavailable, plus "explain this rule" (object / trigger / expectation / exception) via `ruleset_introspect`
|
|
18
|
+
- predicate set now 15: added `never`, `per-turn-cap`, `wal`, `bypass`, `approval-gated`, `permission-change`, `job-dangling`, `command-audit`, `change-gate`, `doc-refs`, `explain-first`
|
|
19
|
+
- red-line guard: `tools/pre-execute` returns `ask` for `sudo`, `chmod 777`, `curl | sh`, system-path writes and credential-shaped strings; hits logged to `.ruleset/guard.log`
|
|
20
|
+
- findings with lifecycle (`open/fixed/accepted/false_positive/superseded`); triaged findings subtract from hit counts and are never deleted; 15s incident merging; stable keys; 7-day supersede
|
|
21
|
+
- baseline significance: two-proportion z-test (`|z| < 1.96` = no meaningful change), per-rule direction shown in the panel
|
|
22
|
+
- injection fingerprinting: `.ruleset/injected.log` records every distinct injected rule text
|
|
23
|
+
- rule hygiene: duplicates, conflicts, rules without predicates, injected token cost
|
|
24
|
+
- ownership guarantees: `AGENTS.md` read-only; `policy.yaml` written only on explicit action, backed up to `.ruleset/backups/` with one-click rollback and export
|
|
25
|
+
- docs: `docs/PREDICATE-PLAYBOOK.md` (lessons that came from real false positives) injected as a short section and readable via `ruleset_playbook`
|
|
26
|
+
- 20 tests, including a plugin-load smoke test and guard behaviour tests; GitHub Actions on Node 22/24
|
|
27
|
+
- transport: HTTP routes are thin adapters over a single `service.js`; a `/ruleset` host command provides a fallback where routes are unavailable
|
|
28
|
+
|
|
29
|
+
## 0.1.0
|
|
30
|
+
|
|
31
|
+
- one policy file drives prompt injection and three-value checks
|
|
32
|
+
- 9 predicate types: regex, path-boundary, field-presence, sequence-order, count-threshold, requires, turn-condition, duration, metric
|
|
33
|
+
- policy validation on load (zero dependency); invalid policy → unknown, never falls back
|
|
34
|
+
- legacy split by file mtime; daily snapshot is out of scope for this package
|
|
35
|
+
- 12 tests: validation + predicate behaviour
|
|
36
|
+
- alignment vs a hand-written collector set: 8/10 identical, 2 differ in evidence granularity
|
package/DEVELOPMENT.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Development notes
|
|
2
|
+
|
|
3
|
+
> 这份文档记录 dsh-ruleset 的设计取舍、踩过的坑,以及为什么现在是这样。
|
|
4
|
+
> 目标读者:接手这个插件的开发者,或者想自己写行为审计插件的人。
|
|
5
|
+
|
|
6
|
+
## 1. 这个插件解决什么问题
|
|
7
|
+
|
|
8
|
+
`AGENTS.md` / `policy` 类文件本质上只是**提示**。业界抱怨集中在四点:
|
|
9
|
+
|
|
10
|
+
1. 规则只是建议,模型可以忘、可以误用、可以被注入文本说服
|
|
11
|
+
2. 规则越写越多,没人敢删,遇到冲突时模型要猜优先级
|
|
12
|
+
3. 文件越长,注意力越稀释(Lost in the Middle),成本还上涨
|
|
13
|
+
4. 没人知道这次到底哪条规则进了模型、有没有生效
|
|
14
|
+
|
|
15
|
+
dsh-ruleset 的定位不是再写一份规则文件,而是把规则变成**可判、可拦、可核**的东西。
|
|
16
|
+
|
|
17
|
+
## 2. 三层结构
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
policy.yaml ──注入──▶ system prompt(按 tier 分段)
|
|
21
|
+
│
|
|
22
|
+
├──判定──▶ predicates(机械判据) + 模型复核(仅语义规则)
|
|
23
|
+
├──拦截──▶ tools/pre-execute 守卫(红线命中→ask)
|
|
24
|
+
└──记账──▶ findings.json / baseline.json / injected.log / guard.log
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
所有操作实现都收在 `lib/service.js`,工具、HTTP 路由、宿主命令三个入口都是薄适配层。
|
|
28
|
+
|
|
29
|
+
## 3. 关键取舍
|
|
30
|
+
|
|
31
|
+
### 3.1 三值 + 分级,而不是二元
|
|
32
|
+
|
|
33
|
+
- 状态:`pass / violation / warn / stat / unknown`
|
|
34
|
+
- `unknown` 绝不当作 `pass`:没有数据就是没有数据
|
|
35
|
+
- 严重度:`red`(红线)/ `yellow`(硬约束)/ `stat`(只统计不飘红)
|
|
36
|
+
|
|
37
|
+
### 3.2 显著性判定(移植自 OpenClaw 的 audit-verdict)
|
|
38
|
+
|
|
39
|
+
红线之外的变化要和基线比,`|z| < 1.96` 视为无明显变化。这条解决的是"累积一定飘红"的伪问题——不是靠调容忍度,而是靠统计上是否真的变差。
|
|
40
|
+
|
|
41
|
+
### 3.3 误报是长期成本
|
|
42
|
+
|
|
43
|
+
发现记录有生命周期:`open / fixed / accepted / false_positive / superseded`。误报**永不删除**,标记后从命中里扣减。没有这条,数字会永久失真。
|
|
44
|
+
|
|
45
|
+
### 3.4 插件不拥有用户的规则文件
|
|
46
|
+
|
|
47
|
+
三条硬原则:
|
|
48
|
+
|
|
49
|
+
1. **AGENTS.md 只读**:插件从不修改它,只在没有 policy 时读取并生成到 `.ruleset/policy.draft.yaml`
|
|
50
|
+
2. **policy.yaml 只在显式操作时写**:用户在面板点保存、或调用工具时才写;写前自动备份到 `.ruleset/backups/`
|
|
51
|
+
3. **卸载不带走能力**:「导出」按钮把 policy 与 AGENTS.md 复制到 `.ruleset/export/`,卸载前用户能完整取回
|
|
52
|
+
|
|
53
|
+
配套机制:
|
|
54
|
+
|
|
55
|
+
| 机制 | 作用 |
|
|
56
|
+
|---|---|
|
|
57
|
+
| `backupPolicy()` | 每次写前留快照,索引写入 `backups/index.json` |
|
|
58
|
+
| 回滚 | 选中任一份备份还原;**回滚前再备份**(`pre-restore`),避免回滚不可逆 |
|
|
59
|
+
| `exportForKeep()` | 导出 policy + AGENTS.md,供卸载后保留 |
|
|
60
|
+
|
|
61
|
+
这条原则的由来:审计工具最容易被怀疑“会不会改我的东西”。宁可多一步备份,也不要让用户担心文件被接管。
|
|
62
|
+
|
|
63
|
+
### 3.5 拦截用 ask,不用硬拒绝
|
|
64
|
+
|
|
65
|
+
`tools/pre-execute` 返回 `ask`:能拦,但你能临时放行。`ctx.tools.guard()` 是不可逆拒绝,弹性太差,只在真正零容忍的场景考虑。
|
|
66
|
+
|
|
67
|
+
## 4. 传输层演进(重要)
|
|
68
|
+
|
|
69
|
+
1. 最早用 `connection.fetch.register` 注册 HTTP 路由,web 端正常
|
|
70
|
+
2. 桌面端 `dsh-app://` 下这些路由全部 404——不是插件没加载,是该协议下没有这条通道
|
|
71
|
+
3. 排查过程走了弯路:先怀疑插件加载失败,再到 profile 里装 inspector 包(无效,那两个包不是 bundle)
|
|
72
|
+
4. 最终方案:`lib/commands.js` 注册 `/ruleset <json>` 命令,客户端先试命令通道、失败再回 HTTP
|
|
73
|
+
5. 结论:`connection.fetch.register` 本身是官方 API,但**不是所有运行时都具备**
|
|
74
|
+
|
|
75
|
+
教训:不要假设某个服务在所有宿主里都存在,要么探测能力,要么准备回退。
|
|
76
|
+
|
|
77
|
+
## 5. 判据实现踩过的坑
|
|
78
|
+
|
|
79
|
+
| 坑 | 现象 | 修法 |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| 宽泛正则 | `来源\|sources` 命中 195/195 | 改用字段级 `field-presence` |
|
|
82
|
+
| 匹配对象过宽 | 调研规则把 `AGENTS.md` 当调研记录 | 加 `pathPattern` + `excludePath` |
|
|
83
|
+
| 命令文本里的字面量 | `python3 -c "…sudo…"` 被当命令 | 段首锚定 + 引号感知 |
|
|
84
|
+
| JS 不支持 `(?s)` | 整条判据报错 | 编译前转换 |
|
|
85
|
+
| 基数不是事件数 | 同一份记录被判多次 | 按文件去重、事故合并(15s 窗口) |
|
|
86
|
+
| 跨窗口重复 | 同一问题因窗口变化反复出现 | supersede + 稳定键 |
|
|
87
|
+
|
|
88
|
+
## 6. 已发生的"整包不加载"故障
|
|
89
|
+
|
|
90
|
+
宿主侧任何一处不合规都会让整个插件静默不加载,而客户端 bundle 仍是旧的,表现为"面板在、接口全 404"。
|
|
91
|
+
|
|
92
|
+
已踩到三次:
|
|
93
|
+
|
|
94
|
+
1. `package.json` 的 `exports` 键写成 `'."'`
|
|
95
|
+
2. 工具输出 schema 用了属性级 `required: true`
|
|
96
|
+
3. `routes.js` / `observe.js` 重复 `import` 同一标识符
|
|
97
|
+
|
|
98
|
+
防线:`test/load.test.mjs` 会真的 `import` 插件并断言工具与路由都注册成功。
|
|
99
|
+
|
|
100
|
+
## 7. 测试
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm test # 20 条:校验、判据、抽取、去重、加载、守卫
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
新增判据时至少补三条:命中、放行、不该命中的边界。
|
|
107
|
+
|
|
108
|
+
## 8. 已知问题
|
|
109
|
+
|
|
110
|
+
1. 改插件必须重启宿主进程,没有热插拔(DSH 的 HMR 只覆盖构建产物)
|
|
111
|
+
2. 客户端 slot props 与主题令牌无法通过 `cordis_inspect` 获取(client provider 未注册)
|
|
112
|
+
3. 规则"对象定义"仍分散在判据参数里,没有独立字段
|
|
113
|
+
4. 模型复核未做人工校准(无 kappa),目前只作参考、不参与红黄
|
|
114
|
+
5. 审计每次全量解压会话(约 48MB/26 会话),会话多了会慢
|
|
115
|
+
|
|
116
|
+
## 9. 下一步优先级
|
|
117
|
+
|
|
118
|
+
1. 规则对象字段化(`object: file|command|turn|task`),判据必须声明
|
|
119
|
+
2. 注入可核加强:把注入指纹写进会话事件,而不只是插件日志
|
|
120
|
+
3. 模型复核抽样 + kappa 校准
|
|
121
|
+
4. 性能:改用 session projections,避免全量扫描
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 caifengmao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-ruleset (draft)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Behavioral rule compliance for DSH — one policy file drives both prompt injection and evidence-backed three-value checks.**
|
|
4
|
+
|
|
5
|
+
> Audits **agent behavior**, not plugin quality. For plugin conformance, see `PerryLink/dsh-standards` (ENTRY/FLOW/BOOST).
|
|
6
|
+
|
|
7
|
+
## Relationship to the methodology
|
|
8
|
+
|
|
9
|
+
The four-tier enforcement model (interception / exit gate / detection / prompt-only) and the "detectable is not blocked" rule live in [`agent-rule-enforcement`](https://github.com/cengcengcai-creator/agent-rule-enforcement). **This package is the detection tier**: it measures behaviour against logs and never claims to block anything.
|
|
10
|
+
|
|
11
|
+
Interop: `lib/export.js` emits a generic JSONL event log (`ts` / `session` / `tool` / `ok` / `checkpoint` / `outcome`), which `audit_events.py` from that repo consumes directly. Verified: 1,138 events → 40 task segments → `long_task_without_checkpoint` 15/40.
|
|
12
|
+
|
|
13
|
+
## Why
|
|
14
|
+
|
|
15
|
+
Most rule plugins inject text. Most audit plugins measure tooling. Neither answers: *did the agent follow the rules it was told?*
|
|
16
|
+
|
|
17
|
+
`dsh-ruleset` keeps one source of truth (`policy.yaml`):
|
|
18
|
+
|
|
19
|
+
1. **inject** — rule text into the system prompt, hot-reloaded on file change
|
|
20
|
+
2. **observe** — rule-by-rule verdicts: `pass` / `violation` / `unknown`, each pointing back to a `seq` or file
|
|
21
|
+
|
|
22
|
+
Contract: **`unknown` is never read as `pass`** (no data source or too few samples stays `unknown`).
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# from GitHub (works today)
|
|
28
|
+
dsh plugin --profile web add github:cengcengcai-creator/dsh-ruleset
|
|
29
|
+
|
|
30
|
+
# from npm (once published)
|
|
31
|
+
dsh plugin --profile web add dsh-ruleset
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Status
|
|
35
|
+
|
|
36
|
+
Functional, self-hosted, no npm account needed — install straight from GitHub.
|
|
37
|
+
|
|
38
|
+
What it does today:
|
|
39
|
+
|
|
40
|
+
1. **Write rules in one sentence** — a model turns it into a predicate you can adjust (`/api/dsh-ruleset/parse`, deterministic fallback included).
|
|
41
|
+
2. **Three tiers** — redline / hard / soft, injected in that order so hard rules are not buried.
|
|
42
|
+
3. **Judge behaviour** — 15 predicate types over native DSH events (approvals, sandbox modes, jobs, turns, file writes), never regex-guessing what the platform already records.
|
|
43
|
+
4. **Gate red lines** — a `tools/pre-execute` guard returns `ask` for `sudo`, `chmod 777`, `curl | sh`, system-path writes, credential-shaped strings. Hits are logged to `.ruleset/guard.log`.
|
|
44
|
+
5. **Keep findings honest** — per-finding lifecycle (`open/fixed/accepted/false_positive/superseded`), false positives subtract from hit counts and are never deleted.
|
|
45
|
+
6. **Compare against a baseline** — two-proportion z-test (ported from OpenClaw's `audit-verdict.py`); `|z| < 1.96` is reported as "no meaningful change" instead of red.
|
|
46
|
+
7. **Verify what reached the model** — every distinct injected rule text is fingerprinted into `.ruleset/injected.log`.
|
|
47
|
+
8. **Own nothing** — `AGENTS.md` is read-only; `policy.yaml` is written only on explicit user action, always after a backup; one-click rollback and export are built in.
|
|
48
|
+
|
|
49
|
+
Rule hygiene (`/api/dsh-ruleset/hygiene`): duplicates, conflicts, rules without predicates, token cost.
|
|
50
|
+
|
|
51
|
+
Tests: `npm test` — 20 cases (validation, predicates, extraction, dedupe, plugin-load smoke, guard behaviour).
|
|
52
|
+
|
|
53
|
+
Known limits: no hot reload (host restart required), client-side slot props are not inspectable, model judging is advisory only (not calibrated with kappa yet).
|
|
54
|
+
|
|
55
|
+
## Community listings
|
|
56
|
+
|
|
57
|
+
Listed:
|
|
58
|
+
|
|
59
|
+
1. **Included** — [awesome-dsh-plugin/awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin) under *Security & Permissions* (merged 2026-11-18 via [PR #16379](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin/pull/16379)); this list also powers the in-app plugin market index.
|
|
60
|
+
|
|
61
|
+
Pending review:
|
|
62
|
+
|
|
63
|
+
1. [kejixiaoliang/awesome-dsh-plugins](https://github.com/kejixiaoliang/awesome-dsh-plugins/pull/223) — health checks / diagnostics / audit category
|
|
64
|
+
|
|
65
|
+
Not on npm yet; install from GitHub until it is:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
dsh plugin --profile web add github:cengcengcai-creator/dsh-ruleset
|
|
69
|
+
```
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# dsh-ruleset(草稿)
|
|
2
|
+
|
|
3
|
+
**把工作纪律变成可观测证据:一份 policy 同时驱动提示词注入与三值判定。**
|
|
4
|
+
|
|
5
|
+
> 判的是 **agent 的行为**,不是插件的质量。插件合规看 `PerryLink/dsh-standards`(ENTRY/FLOW/BOOST)。
|
|
6
|
+
|
|
7
|
+
## 与方法论的关系
|
|
8
|
+
|
|
9
|
+
四层执行深度(拦截 / 退出闸门 / 事后检测 / 仅提示)与「可检测 ≠ 可阻断」来自 [`agent-rule-enforcement`](https://github.com/cengcengcai-creator/agent-rule-enforcement)。**本包只做检测层**:拿日志量行为,不宣称能拦。
|
|
10
|
+
|
|
11
|
+
互操作:`lib/export.js` 可导出通用事件日志(`ts` / `session` / `tool` / `ok` / `checkpoint` / `outcome`),该仓库的 `audit_events.py` 能直接消费。实测:1,138 条事件 → 40 个任务段 → `long_task_without_checkpoint` 15/40。
|
|
12
|
+
|
|
13
|
+
## 为什么开源它有意义
|
|
14
|
+
|
|
15
|
+
市面上两类插件各占一端:
|
|
16
|
+
|
|
17
|
+
1. 规则注入类(`dsh-tool-policy`、`dsh-security-guard`):把规矩写进提示词,但没人知道有没有被遵守
|
|
18
|
+
2. 观测/审计类(`dsh-observe` 类 OTel 导出、`dsh-dashboard`、`dsh-context-doctor`):测 token、测工具耗时,但不测“守没守规矩”
|
|
19
|
+
|
|
20
|
+
缺的是中间那段:**规则说了,到底做到没有**。`dsh-ruleset` 就做这一段,并且坚持两条契约:
|
|
21
|
+
|
|
22
|
+
- `unknown` 永远不得读作 `pass`
|
|
23
|
+
- 每条 violation 必须能指回 `seq` 或文件路径
|
|
24
|
+
|
|
25
|
+
## 通用性从哪来
|
|
26
|
+
|
|
27
|
+
规则不内置。用户用 9 类谓词在 policy 里声明判据:
|
|
28
|
+
|
|
29
|
+
`regex` · `path-boundary` · `field-presence` · `sequence-order` · `count-threshold` · `requires` · `turn-condition` · `duration` · `metric`
|
|
30
|
+
|
|
31
|
+
我们自己的 15 条规则里,10 条能用声明式表达;剩下 5 条如实标 `unknown`(语义类)。这不是 100%,但是可验证的边界。
|
|
32
|
+
|
|
33
|
+
## 装法
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# GitHub 直装(现在可用)
|
|
37
|
+
dsh plugin --profile web add github:cengcengcai-creator/dsh-ruleset
|
|
38
|
+
|
|
39
|
+
# npm(发布后可用)
|
|
40
|
+
dsh plugin --profile web add dsh-ruleset
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
# profiles/<name>/cordis.patch.yml
|
|
45
|
+
- insert:
|
|
46
|
+
- id: dsh-ruleset
|
|
47
|
+
name: 'dsh-ruleset'
|
|
48
|
+
config:
|
|
49
|
+
policyPath: '' # 空 = $DSH_RULESET_POLICY → ~/.dsh/workspace/policy.yaml
|
|
50
|
+
injectMode: compact # compact | full | off
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 设计
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
lib/index.js 接线
|
|
57
|
+
lib/policy.js 读取 + 校验 + mtime 热更
|
|
58
|
+
lib/inject.js 系统提示词段
|
|
59
|
+
lib/observe.js ruleset_audit 工具
|
|
60
|
+
lib/facts.js 平台事实流(命令/审批/写入/子代理/作业/指标)
|
|
61
|
+
lib/predicates.js 九类谓词执行器
|
|
62
|
+
lib/report.js 三值判定与渲染
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
参见 [docs-schema.md](docs-schema.md) 与 [ALIGNMENT.md](ALIGNMENT.md)(与手写采集器的逐条对照)。
|
|
66
|
+
|
|
67
|
+
## 来源与声明
|
|
68
|
+
|
|
69
|
+
本包由 **AI agent(DeepSeek Harness 作为主要执行者)开发**:在本机真实运行中迭代、按失败纠正,而不是人工手写的提示词模板。它引用的方法论仓库同样如此。请把它当成起点,而不是认证。
|
|
70
|
+
|
|
71
|
+
插件不内置规则手册。规则来自用户自己的 `policy.yaml`;这里提供的是 schema、谓词执行器和证据采集。
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
## 只有 AGENTS.md 也能用
|
|
75
|
+
|
|
76
|
+
多数人的规则是在对话里慢慢攒进 `AGENTS.md` 的,不是按工程规范写的。插件按三种情况处理:
|
|
77
|
+
|
|
78
|
+
1. 有 `policy.yaml` → 直接用它
|
|
79
|
+
2. 没有,但有 `AGENTS.md` → 自动抽取成**草稿**:注入提示词,但**不判定**(每条都报 unknown,注明“草稿未确认”)
|
|
80
|
+
3. 两者都没有 → 什么都不判,报告明确写“无规则来源”,不猜
|
|
81
|
+
|
|
82
|
+
把草稿升级成真规则:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
node scripts/extract.mjs AGENTS.md policy.draft.yaml # 82 条强制句 → 分类草稿
|
|
86
|
+
# 删掉不要的,给关键的几条补上 observe.bind,改名为 policy.yaml
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
起步包在 `examples/policy.starter.yaml`(5 条最常被违反、且能机械判的规则)。
|
|
90
|
+
|
|
91
|
+
## 可插拔:不碰用户文件
|
|
92
|
+
|
|
93
|
+
- 插件**从不写** `AGENTS.md`、`policy.yaml` 或任何用户文件,全部在调用时只读。
|
|
94
|
+
- 卸载后提示词原样恢复:注册走 `ctx.effect`,随插件一起 dispose。
|
|
95
|
+
- 注入与 `AGENTS.md` 去重:规则文本若已出现在 AGENTS.md 里,就**不再重复注入**(报告显示 `注入 N 条 / 跳过 M 条`);规则本身就来自 AGENTS.md 时,注入层自动关闭。
|
|
96
|
+
|
|
97
|
+
## 工具
|
|
98
|
+
|
|
99
|
+
- `ruleset_audit` — 输出最近 N 小时的三值报告,同时写一份 `.ruleset/report.json`(插件自有状态,不是用户文件),供 UI 或外部读取。
|
|
100
|
+
- `ruleset_init` — 仅在你明确要求时:把 `AGENTS.md` 抽取成 `.ruleset/policy.draft.yaml`。永不触碰正式 `policy.yaml`;草稿已存在时需 `overwrite: true` 才覆盖。
|
|
101
|
+
|
|
102
|
+
## 现在的功能(2026-11)
|
|
103
|
+
|
|
104
|
+
1. **一句话写规则**:模型拆成判据,可手调;模型不可用时走确定性兜底
|
|
105
|
+
2. **三档分级**:红线 / 硬约束 / 软规则,注入按此顺序,硬规则不会被埋
|
|
106
|
+
3. **判行为**:15 类判据,优先用 DSH 原生事件(审批、沙箱模式、后台任务、轮次、写入),不拿正则猜平台已有的事实
|
|
107
|
+
4. **拦红线**:`tools/pre-execute` 守卫对 `sudo`、`chmod 777`、`curl | sh`、系统目录写入、凭据形态返回 `ask`,命中记入 `.ruleset/guard.log`
|
|
108
|
+
5. **发现可维护**:每条发现可标 误报/已修/接受,误报从命中里扣减且永不删除;7 天后自动 supersede
|
|
109
|
+
6. **与基线比**:两比例 z 检验(移植自 OpenClaw 的 `audit-verdict.py`),`|z|<1.96` 显示「无明显变化」而不是飘红
|
|
110
|
+
7. **注入可核**:每次真正注入的规则文本都会留指纹(`.ruleset/injected.log`)
|
|
111
|
+
8. **不拥有你的规则**:`AGENTS.md` 只读;`policy.yaml` 只在显式操作时写,写前自动备份,支持一键回滚与导出
|
|
112
|
+
|
|
113
|
+
规则体检:重复、冲突、无判据、token 成本。测试:`npm test`(20 条)。
|
|
114
|
+
|
|
115
|
+
已知限制:无热插拔(改插件需重启宿主)、客户端 slot props 无法查询、模型复核仅供参照(未做 kappa 校准)。
|
|
116
|
+
|
|
117
|
+
## 社区收录
|
|
118
|
+
|
|
119
|
+
已收录:
|
|
120
|
+
|
|
121
|
+
1. **[awesome-dsh-plugin/awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)** ——“Security & Permissions”分类(2026-11-18 合并,[PR #16379](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin/pull/16379));该列表同时是应用内插件市场的索引源
|
|
122
|
+
|
|
123
|
+
审核中:
|
|
124
|
+
|
|
125
|
+
1. [kejixiaoliang/awesome-dsh-plugins](https://github.com/kejixiaoliang/awesome-dsh-plugins/pull/223)——健康检查 / 诊断 / 审计分类
|
|
126
|
+
|
|
127
|
+
暂未发布到 npm,发布前请用 GitHub 直装:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
dsh plugin --profile web add github:cengcengcai-creator/dsh-ruleset
|
|
131
|
+
```
|