dsh-skill-gate 0.0.0-stage → 0.1.0
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 +61 -0
- package/LICENSE +21 -0
- package/README.md +191 -2
- package/README.zh.md +114 -0
- package/cordis.patch.yml +7 -0
- package/index.js +170 -0
- package/lib/policy.js +298 -0
- package/package.json +42 -3
- package/skill-gate.example.json +20 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `dsh-skill-gate` are recorded here.
|
|
4
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and
|
|
5
|
+
this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.0] — 2026-11-19
|
|
10
|
+
|
|
11
|
+
First release. Per-preset skill filtering for the DSH host: a hidden skill does
|
|
12
|
+
not reach the model's `<available_skills>` catalog and cannot be loaded through
|
|
13
|
+
the `skill` tool.
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
|
|
17
|
+
- **Per-preset policy** from `$DSH_HOME/skill-gate.json`, with `allow`
|
|
18
|
+
(whitelist) and `deny` (blacklist) modes and `*` wildcards in skill names
|
|
19
|
+
(`office*`, `role-*`).
|
|
20
|
+
- **Optional `enabled`** per preset, defaulting to `true`, to switch the gate off
|
|
21
|
+
for one preset without deleting its list.
|
|
22
|
+
- **Catalog rewrite** on `agent/pre-step`, registered with `{ prepend: true }`
|
|
23
|
+
and running after `next()`. Only the model-facing text inside
|
|
24
|
+
`<available_skills>` is rewritten; `source.entries` is left untouched so the
|
|
25
|
+
host's catalog digest stays stable and the catalog is not re-sent every step.
|
|
26
|
+
- **Call guard**: `skill("<hidden>")` is refused with a reason that names
|
|
27
|
+
`dsh-skill-gate`.
|
|
28
|
+
- **Hot reload** without restarting the host: the config is re-read when its
|
|
29
|
+
mtime or size changes, checked once per step.
|
|
30
|
+
- **One log line per filtered step**:
|
|
31
|
+
`[skill-gate] preset=dev mode=allow form=catalog kept=7/38 bytes=9917->2147`.
|
|
32
|
+
|
|
33
|
+
### Design notes
|
|
34
|
+
|
|
35
|
+
- Shipping state is **inert**: with no config file, or with invalid JSON, or with
|
|
36
|
+
a field of the wrong type, every preset passes through unfiltered and one
|
|
37
|
+
warning is logged. A broken config must never leave an agent with no skills.
|
|
38
|
+
- Presets absent from `presets` are not touched at all.
|
|
39
|
+
- When every skill is hidden, the catalog message is kept (deleting it would
|
|
40
|
+
desynchronise the host's digest) and its body is replaced with one notice line.
|
|
41
|
+
- No tools, no system-prompt section, no injected messages: the plugin spends no
|
|
42
|
+
context of its own.
|
|
43
|
+
- Every failure path logs and degrades to "no filtering"; exceptions never escape
|
|
44
|
+
the hooks.
|
|
45
|
+
|
|
46
|
+
### Known limitations
|
|
47
|
+
|
|
48
|
+
- `source.entries` keeps every skill even after the text is filtered, so
|
|
49
|
+
consumers that read it (skill badges) disagree with what the model sees.
|
|
50
|
+
- The empty-catalog notice is English regardless of session language.
|
|
51
|
+
|
|
52
|
+
### Verification status
|
|
53
|
+
|
|
54
|
+
Filtering behaviour was measured against the real `@deepseek-ai/dsh-tool-skill`
|
|
55
|
+
module (7-cell matrix, catalog rotation, 4 mutations, anti-self-proof run — see
|
|
56
|
+
`cangku/tmp/261119_k4m_gate1.md`). **End-to-end first-turn byte savings have not
|
|
57
|
+
been captured on a live host yet**; see `cangku/261119_k4m_skillgate_report.md`
|
|
58
|
+
for which acceptance items are measured and which are open.
|
|
59
|
+
|
|
60
|
+
[Unreleased]: https://example.invalid/dsh-skill-gate/compare/v0.1.0...HEAD
|
|
61
|
+
[0.1.0]: https://example.invalid/dsh-skill-gate/releases/tag/v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MeruSu
|
|
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,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-skill-gate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Installing this plugin changes nothing.** With no config file, every preset
|
|
4
|
+
sees exactly the skill catalog it saw before — the plugin is a no-op until you
|
|
5
|
+
give it a list.
|
|
6
|
+
|
|
7
|
+
Skills are the second-largest fixed cost in a DSH first turn: 9,917 B / 38
|
|
8
|
+
entries in the measured `dev` preset, 24.6 % of a 40,250 B opening request
|
|
9
|
+
(`cangku/261119_k4m_skillgate_spec.md` §1). Most presets use a handful of them.
|
|
10
|
+
`dsh-skill-gate` lets you say **per preset** which skills are worth showing; a
|
|
11
|
+
hidden skill neither reaches the model's context nor loads when the model tries
|
|
12
|
+
to call it.
|
|
13
|
+
|
|
14
|
+
Your lists are your own. Nothing ships pre-configured, and per-preset setup is
|
|
15
|
+
the whole point — a saving only exists after you write a list.
|
|
16
|
+
|
|
17
|
+
> **Status: 0.1.0, not yet verified on a live host.** The filtering behaviour is
|
|
18
|
+
> measured against the real `@deepseek-ai/dsh-tool-skill` module
|
|
19
|
+
> (`cangku/261119_k4m_skillgate_report.md`); end-to-end first-turn byte savings
|
|
20
|
+
> have not been captured on a real session yet. Every number in these docs is
|
|
21
|
+
> attributed to the evidence file it came from.
|
|
22
|
+
|
|
23
|
+
## 1. What it does
|
|
24
|
+
|
|
25
|
+
Two hooks, nothing else:
|
|
26
|
+
|
|
27
|
+
1. **Catalog rewrite** — on `agent/pre-step`, after the host has rendered the
|
|
28
|
+
`<available_skills>` catalog, the gate removes the lines belonging to hidden
|
|
29
|
+
skills.
|
|
30
|
+
2. **Call guard** — if the model calls `skill("office")` anyway, the call is
|
|
31
|
+
refused with `技能「office」在本预设中被隐藏(dsh-skill-gate)` — Chinese, like
|
|
32
|
+
the other refusal strings this host hands to the model; the catalog notice
|
|
33
|
+
below stays English because it is rendered prose, not an error reason.
|
|
34
|
+
|
|
35
|
+
The plugin has **no tools, no system-prompt section and no injected messages**.
|
|
36
|
+
It exists to save context; it must not spend any.
|
|
37
|
+
|
|
38
|
+
## 2. Configure it — nothing happens until you do
|
|
39
|
+
|
|
40
|
+
One file: `$DSH_HOME/skill-gate.json` (`$DSH_HOME` defaults to `~/.dsh`).
|
|
41
|
+
|
|
42
|
+
**No file → no filtering, any preset.** That is the delivered state.
|
|
43
|
+
|
|
44
|
+
Here is a config you can copy as-is and then edit:
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"version": 1,
|
|
49
|
+
"presets": {
|
|
50
|
+
"dev": {
|
|
51
|
+
"mode": "allow",
|
|
52
|
+
"names": ["plugin-work", "web-work", "motion-work", "dsh-plugin-dev",
|
|
53
|
+
"cordis-composition-reference", "cangku-snapshots", "long-session-fold"]
|
|
54
|
+
},
|
|
55
|
+
"minimal": { "mode": "allow", "names": ["cangku-snapshots"] },
|
|
56
|
+
"clerk": { "mode": "deny", "names": ["office*", "univer*"] },
|
|
57
|
+
"cordis": { "enabled": false }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**`mode`**
|
|
63
|
+
|
|
64
|
+
| value | meaning |
|
|
65
|
+
|---|---|
|
|
66
|
+
| `allow` | keep only the skills whose name matches `names`; hide everything else |
|
|
67
|
+
| `deny` | hide the skills whose name matches `names`; keep everything else |
|
|
68
|
+
|
|
69
|
+
**`names`** — exact skill names, `*` allowed as a wildcard: `office*`, `role-*`,
|
|
70
|
+
`*work`. A `*` matches any run of characters, including none, and the **whole**
|
|
71
|
+
name must match — `office*` matches `office` and `office-xlsx` but not `xoffice`.
|
|
72
|
+
Every other regex metacharacter is literal, so `a.b` matches only the skill
|
|
73
|
+
literally named `a.b`.
|
|
74
|
+
|
|
75
|
+
**`enabled`** — optional, defaults to `true`. `{ "enabled": false }` switches the
|
|
76
|
+
gate off for that one preset while leaving other entries alone; it is the
|
|
77
|
+
per-preset "off switch" you want when you would otherwise have to delete an entry
|
|
78
|
+
you are still tuning.
|
|
79
|
+
|
|
80
|
+
**Presets you do not mention are not touched at all.** This is the most
|
|
81
|
+
important rule: an unconfigured preset gets exactly the catalog the host would
|
|
82
|
+
have produced.
|
|
83
|
+
|
|
84
|
+
**Broken config allows everything.** A missing file, invalid JSON, or a field of
|
|
85
|
+
the wrong type means all presets pass through: the plugin logs one warning and
|
|
86
|
+
steps aside. Malformed *individual* preset entries are dropped and reported while
|
|
87
|
+
the rest of the file keeps working. A broken config must never leave an agent
|
|
88
|
+
with no skills.
|
|
89
|
+
|
|
90
|
+
### How `*` interacts with `allow`
|
|
91
|
+
|
|
92
|
+
`allow` plus `names: []` hides every skill; the catalog keeps its
|
|
93
|
+
`<available_skills>` markers and gets one notice line instead of the list (see
|
|
94
|
+
*Known limitations*). `allow` is a whitelist, so adding a new skill to the host
|
|
95
|
+
does not show up in that preset until you add its name.
|
|
96
|
+
|
|
97
|
+
### What "hidden" means, precisely
|
|
98
|
+
|
|
99
|
+
The gate edits the **text** of the catalog message. It deliberately leaves the
|
|
100
|
+
machine-readable `source.entries` array untouched.
|
|
101
|
+
|
|
102
|
+
The reason lives in the host: `@deepseek-ai/dsh-tool-skill` decides whether the
|
|
103
|
+
catalog changed by hashing `source.entries` **as read back from the persisted
|
|
104
|
+
session event** (`catalogHistory`, `dsh-tool-skill/lib/index.js` 1065-1083). If
|
|
105
|
+
the plugin shrinks the persisted entries, the persisted digest disagrees with the
|
|
106
|
+
live one on every step and the host re-renders a full catalog every step — the
|
|
107
|
+
opposite of saving bytes. Measured against the real module: the "filter entries
|
|
108
|
+
**and** text" variant emits a fresh `update` on steps 2 and 3 (2,147 B, then
|
|
109
|
+
2,010 B, then 2,010 B; three catalog events —
|
|
110
|
+
`cangku/tmp/k4m_node/k4m_gate1_matrix.txt:11-13`).
|
|
111
|
+
|
|
112
|
+
So entries stay whole, the digest stays stable, and the model sees only the kept
|
|
113
|
+
lines. The cost is under *Known limitations*.
|
|
114
|
+
|
|
115
|
+
## 3. Verify it actually took effect
|
|
116
|
+
|
|
117
|
+
1. **Log line.** Each step that really filters writes exactly one line, for
|
|
118
|
+
example:
|
|
119
|
+
```
|
|
120
|
+
[skill-gate] preset=dev mode=allow form=catalog kept=7/38 bytes=9917->2147
|
|
121
|
+
```
|
|
122
|
+
`kept=7/38` is kept-over-total entries, `bytes=9917->2147` is the catalog text
|
|
123
|
+
before and after, and `form=` is `catalog` for the first catalog and `update`
|
|
124
|
+
when the host re-published one. If you never see this line, no preset matched your
|
|
125
|
+
config — check the spelling of the preset id first.
|
|
126
|
+
2. **Catalog in a session.** Open a session in a configured preset; the
|
|
127
|
+
`<available_skills>` block must list only the kept skills.
|
|
128
|
+
3. **Call guard.** Ask the model to use a hidden skill (`skill("office")` under
|
|
129
|
+
the `dev` list above). The call must be refused with
|
|
130
|
+
`技能「office」在本预设中被隐藏(dsh-skill-gate)`.
|
|
131
|
+
4. **An untouched preset still works.** Open a session in a preset absent from
|
|
132
|
+
your config; its catalog must be identical to a run with the plugin disabled.
|
|
133
|
+
This is the check that catches "installed but silently inactive".
|
|
134
|
+
5. **Broken config.** Write `{` into `skill-gate.json`, open a new session: all
|
|
135
|
+
skills present, session runs normally, one warning logged.
|
|
136
|
+
|
|
137
|
+
## 4. Known limitations
|
|
138
|
+
|
|
139
|
+
**The catalog text and `source.entries` disagree, by design.** Anything reading
|
|
140
|
+
`source.entries` still sees every skill, including ones the model was never
|
|
141
|
+
shown. Measured directly: after the rewrite the text carries no `role-dev` line
|
|
142
|
+
while the stored entries still list it (`textHasRoleDev=false`,
|
|
143
|
+
`storedHasRoleDev=true`, all four steps,
|
|
144
|
+
`cangku/tmp/k4m_node/k4m_gate1_rotation.json`). Consumers such as skill badges
|
|
145
|
+
therefore describe a different world than the model does. This is the accepted
|
|
146
|
+
price of a stable digest.
|
|
147
|
+
|
|
148
|
+
**The empty-catalog notice is written by this plugin.** When a preset hides every
|
|
149
|
+
skill, the catalog body becomes one fixed line,
|
|
150
|
+
`- No skills are available in this preset (dsh-skill-gate)`. It is English
|
|
151
|
+
regardless of the session language, and it is the plugin speaking, not the host.
|
|
152
|
+
|
|
153
|
+
**The gate only sees what the host renders.** Skills that never reach the catalog
|
|
154
|
+
are outside its reach. `user-invocable` skills are out of scope too: the gate
|
|
155
|
+
governs what the model can see and call, not what a human can invoke.
|
|
156
|
+
|
|
157
|
+
**`@skill` mentions are affected.** The `scope-manager` plugin rewrites
|
|
158
|
+
`@skillname` into `/skillname`, and when that path ends in the `skill` tool the
|
|
159
|
+
guard refuses it for hidden skills. Intended, but surprising the first time.
|
|
160
|
+
|
|
161
|
+
**Changes apply on the next request, not instantly.** The config is re-read when
|
|
162
|
+
its mtime or size changes, checked once per step. Nothing uses `fs.watch`, so
|
|
163
|
+
there is no file handle to leak on Windows — and no reaction between steps
|
|
164
|
+
either.
|
|
165
|
+
|
|
166
|
+
**Installation is manual, and the host must be restarted once after install.**
|
|
167
|
+
|
|
168
|
+
## 5. Guarantees the plugin keeps
|
|
169
|
+
|
|
170
|
+
- Never modifies `app.asar` or any other plugin's `node_modules`.
|
|
171
|
+
- Disable or uninstall → stock behaviour immediately, no residue.
|
|
172
|
+
- Every failure path logs and degrades to "no filtering". Exceptions never escape
|
|
173
|
+
the hooks: this plugin is the outermost `agent/pre-step` listener, so a throw
|
|
174
|
+
would take down the whole turn.
|
|
175
|
+
- One `statSync` per step; filtering is a single pass over one already-rendered
|
|
176
|
+
message.
|
|
177
|
+
- The `agent/pre-step` listener always registers with `{ prepend: true }`, and a
|
|
178
|
+
test asserts it: mounted after `dsh-tool-skill` with a plain `on()`, the plugin
|
|
179
|
+
would sit *inside* the host's own listener and its edit would be overwritten
|
|
180
|
+
(measured: the catalog stays at 9,917 B and nothing is filtered).
|
|
181
|
+
|
|
182
|
+
## 6. Evidence
|
|
183
|
+
|
|
184
|
+
| What | Where |
|
|
185
|
+
|---|---|
|
|
186
|
+
| Requirement and baseline measurement | `cangku/261119_k4m_skillgate_spec.md` |
|
|
187
|
+
| Gate-1 experiments (matrix, rotation, mutation) | `cangku/tmp/261119_k4m_gate1.md`, `cangku/tmp/k4m_node/` |
|
|
188
|
+
| Acceptance report | `cangku/261119_k4m_skillgate_report.md` |
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
Part of the Meru private plugin set.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# dsh-skill-gate
|
|
2
|
+
|
|
3
|
+
**装上这个插件,什么都不会变。** 没有配置文件时,每个预设看到的技能目录和装之前一模一样——不配名单,它就是个空转的插件。
|
|
4
|
+
|
|
5
|
+
技能目录是 DSH 首轮请求里第二大的一块固定开销:实测 `dev` 预设 9,917 B / 38 条,占 40,250 B 首轮请求的 24.6%(`cangku/261119_k4m_skillgate_spec.md` §1)。而大多数预设真正用得上的只有几条。`dsh-skill-gate` 让你**按预设**决定哪些技能值得露出来:被隐藏的技能既不进模型上下文,模型想调也调不动。
|
|
6
|
+
|
|
7
|
+
名单是你自己的事。插件不预置任何名单,省下的字节只在你写完名单之后才存在。
|
|
8
|
+
|
|
9
|
+
> **状态:0.1.0,尚未在真机宿主上验收。** 过滤行为已对着真的 `@deepseek-ai/dsh-tool-skill` 模块测过(`cangku/261119_k4m_skillgate_report.md`);端到端的首轮字节节省还没有在真会话里采到。本文里的每个数字都注明了出自哪份证据文件。
|
|
10
|
+
|
|
11
|
+
## 一、它做什么
|
|
12
|
+
|
|
13
|
+
只挂两个钩子,别的什么都不干:
|
|
14
|
+
|
|
15
|
+
1. **改目录文本** —— 在 `agent/pre-step` 阶段,宿主把 `<available_skills>` 目录渲染出来之后,插件把属于被隐藏技能的那几行删掉。
|
|
16
|
+
2. **拦调用** —— 就算模型还是调了 `skill("office")`,这次调用会被拒绝,理由里写明是本预设的策略挡的。
|
|
17
|
+
|
|
18
|
+
插件**零工具、零系统提示段、零注入消息**。它存在的意义是省上下文,不能自己再占。
|
|
19
|
+
|
|
20
|
+
## 二、配置——不配就不生效
|
|
21
|
+
|
|
22
|
+
只有一个文件:`$DSH_HOME/skill-gate.json`(`$DSH_HOME` 默认是 `~/.dsh`)。
|
|
23
|
+
|
|
24
|
+
**文件不存在 → 任何预设都不过滤。** 这就是交付后的默认状态。
|
|
25
|
+
|
|
26
|
+
下面这份可以直接抄,抄完再改:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"version": 1,
|
|
31
|
+
"presets": {
|
|
32
|
+
"dev": {
|
|
33
|
+
"mode": "allow",
|
|
34
|
+
"names": ["plugin-work", "web-work", "motion-work", "dsh-plugin-dev",
|
|
35
|
+
"cordis-composition-reference", "cangku-snapshots", "long-session-fold"]
|
|
36
|
+
},
|
|
37
|
+
"minimal": { "mode": "allow", "names": ["cangku-snapshots"] },
|
|
38
|
+
"clerk": { "mode": "deny", "names": ["office*", "univer*"] },
|
|
39
|
+
"cordis": { "enabled": false }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**`mode`**
|
|
45
|
+
|
|
46
|
+
| 值 | 含义 |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `allow` | 白名单:只保留 `names` 命中的技能,其余全藏 |
|
|
49
|
+
| `deny` | 黑名单:藏掉 `names` 命中的技能,其余全留 |
|
|
50
|
+
|
|
51
|
+
**`names`** —— 技能名,支持 `*` 通配:`office*`、`role-*`、`*work`。`*` 匹配任意长度(含零)的字符,且**整名匹配**——`office*` 命中 `office` 和 `office-xlsx`,不命中 `xoffice`。其余正则元字符一律按字面处理,`a.b` 只命中真的叫 `a.b` 的技能。
|
|
52
|
+
|
|
53
|
+
**`enabled`** —— 可选,默认 `true`。`{ "enabled": false }` 可以单独关掉某一个预设的过滤,不影响文件里其他条目;名单还在调、不想删的时候用它当开关。
|
|
54
|
+
|
|
55
|
+
**没写进 `presets` 的预设,一个字都不改。** 这是最重要的一条:未配置的预设拿到的目录和宿主原生输出完全一致。
|
|
56
|
+
|
|
57
|
+
**配置坏了 = 全部放行。** 文件不存在、JSON 非法、字段类型不对,一律让所有预设原样通过:插件打一条 warn 就让开。**某个**预设条目写错了,只丢这一条并报出来,同一文件里其他条目照常生效。配置坏掉绝不能让 agent 手上一条技能都没有。
|
|
58
|
+
|
|
59
|
+
### `allow` 与 `*` 的相互作用
|
|
60
|
+
|
|
61
|
+
`allow` 配 `names: []` = 全部隐藏;此时目录仍保留 `<available_skills>` 首尾标记,正文换成一行提示(见「已知局限」)。`allow` 是白名单,所以宿主新增技能不会自动露出来,要你主动加名字。
|
|
62
|
+
|
|
63
|
+
### 「隐藏」到底改的是什么
|
|
64
|
+
|
|
65
|
+
插件改的是目录消息的**文本**。机器可读的 `source.entries` 数组**故意一字不动**。
|
|
66
|
+
|
|
67
|
+
原因在宿主那边,不是口味问题:`@deepseek-ai/dsh-tool-skill` 判断「目录变没变」时,哈希的是**从持久化会话事件里读回来的** `source.entries`(`catalogHistory`,`dsh-tool-skill/lib/index.js` 1065-1083)。如果插件把持久化的 entries 改小,持久化 digest 与实时 digest 每一步都对不上,宿主就会每一步重渲染一整份目录——正好与省字节相反。对着真模块实测:「entries 与文本同改」这一变体在第 2、3 步各多发一条 `update`(2,147 B → 2,010 B → 2,010 B,共三条目录事件,`cangku/tmp/k4m_node/k4m_gate1_matrix.txt:11-13`)。
|
|
68
|
+
|
|
69
|
+
所以:entries 保持全量,digest 保持稳定,模型眼里只剩留下的那几行。代价写在「已知局限」里。
|
|
70
|
+
|
|
71
|
+
## 三、怎么验证它真的生效了
|
|
72
|
+
|
|
73
|
+
1. **看日志行。** 每一步真的过滤了,就会打且只打一条,形如:
|
|
74
|
+
```
|
|
75
|
+
[skill-gate] preset=dev mode=allow form=catalog kept=7/38 bytes=9917->2147
|
|
76
|
+
```
|
|
77
|
+
`kept=7/38` 是保留条数/总条数,`bytes=9917->2147` 是目录文本改前改后的字节。看不到这行,说明没有预设命中你的配置——先核对预设 id 拼写。
|
|
78
|
+
2. **看会话里的目录。** 在配过的预设里开一个会话,`<available_skills>` 块里应当只剩保留的技能。
|
|
79
|
+
3. **试调用拦截。** 让模型去用被隐藏的技能(在上面那份 `dev` 名单下试 `skill("office")`),调用必须被拒,理由里带 `dsh-skill-gate`。
|
|
80
|
+
4. **未配置的预设不受影响。** 在配置里没出现的预设开一个会话,它的目录必须和禁用插件时逐字相同。这一条专抓「装了但静默没生效」。
|
|
81
|
+
5. **坏配置。** 把 `{` 写进 `skill-gate.json`,开新会话:技能全在、会话正常跑完、只多一条 warn。
|
|
82
|
+
|
|
83
|
+
## 四、已知局限
|
|
84
|
+
|
|
85
|
+
**目录文本与 `source.entries` 不一致,这是设计取舍。** 任何读 `source.entries` 的地方仍然看得到全部技能,包括模型从没见过的那些。有直接实测:改写后文本里没有 `role-dev` 这一行,而存储的 entries 里它还在(`textHasRoleDev=false`、`storedHasRoleDev=true`,四步全是,`cangku/tmp/k4m_node/k4m_gate1_rotation.json`)。技能徽章这类消费者描述的图景因此和模型看到的不一样。这是稳定 digest 换来的代价。
|
|
86
|
+
|
|
87
|
+
**空名单时那一行提示由本插件写入。** 某个预设把所有技能都藏了时,目录正文变成固定一行 `- No skills are available in this preset (dsh-skill-gate)`。它不随会话语言变化,且是插件在说话,不是宿主。(拒绝调用时返回的是中文原因 `技能「X」在本预设中被隐藏(dsh-skill-gate)`:那是错误原因文本,跟本机其它拒绝文案同一个语种;这里是嵌进宿主英文渲染段落的正文,所以两种语言并存。)
|
|
88
|
+
|
|
89
|
+
**插件只看得到宿主渲染出来的东西。** 压根没进目录的技能不在它管辖范围内。`user-invocable` 技能也不归它管:它决定的是模型看得见什么、调得动什么,不是人能触发什么。
|
|
90
|
+
|
|
91
|
+
**`@技能名` 手势会连带受影响。** `scope-manager` 会把 `@技能名` 改写成 `/技能名`,当这条路径最终落到 `skill` 工具时,被隐藏的技能会被本插件的 guard 拦住。这是预期行为,但第一次遇到会意外。
|
|
92
|
+
|
|
93
|
+
**改动在下一个请求生效,不是立刻。** 配置的 mtime 或大小变了才会重读,每步检查一次。没有用 `fs.watch`,所以 Windows 上不会有句柄泄漏——但步与步之间也不会立刻响应。
|
|
94
|
+
|
|
95
|
+
**安装是手动的,装完要重启一次宿主。**
|
|
96
|
+
|
|
97
|
+
## 五、插件守住的几条底线
|
|
98
|
+
|
|
99
|
+
- 不改 `app.asar`,不改任何别的插件的 `node_modules`。
|
|
100
|
+
- 禁用或卸载 → 立刻回到原生行为,不留残留。
|
|
101
|
+
- 任何出错路径都只记日志并退化成「不过滤」。异常绝不逃出钩子:本插件是最外层的 `agent/pre-step` listener,抛出去会带崩整轮对话。
|
|
102
|
+
- 每步一次 `statSync`;过滤只是对一条已渲染消息做一遍数组扫描。
|
|
103
|
+
|
|
104
|
+
## 六、证据
|
|
105
|
+
|
|
106
|
+
| 什么 | 在哪 |
|
|
107
|
+
|---|---|
|
|
108
|
+
| 需求与基线测量 | `cangku/261119_k4m_skillgate_spec.md` |
|
|
109
|
+
| 门1 实验(矩阵 / 轮换 / 变异) | `cangku/tmp/261119_k4m_gate1.md`、`cangku/tmp/k4m_node/` |
|
|
110
|
+
| 验收报告 | `cangku/261119_k4m_skillgate_report.md` |
|
|
111
|
+
|
|
112
|
+
## 许可
|
|
113
|
+
|
|
114
|
+
Meru 私有插件集的一部分。
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Bundle patch: one row for dsh-skill-gate.
|
|
2
|
+
# The gate is host-only: it rewrites the skill catalog during `agent/pre-step` and
|
|
3
|
+
# refuses hidden `skill` calls through `tools.guard`. It registers no tool, no
|
|
4
|
+
# system-prompt section and no client half, because it exists to save context.
|
|
5
|
+
- insert:
|
|
6
|
+
- id: skill-gate
|
|
7
|
+
name: 'dsh-skill-gate'
|
package/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of `dsh-skill-gate` — hide skills per agent preset.
|
|
3
|
+
*
|
|
4
|
+
* The host has no "filter skills per preset" switch (`dsh-tool-skill` only knows
|
|
5
|
+
* `catalogDescriptionMaxLength`, and `dsh-skill`'s registry is append-only), so
|
|
6
|
+
* this plugin works the two places that do exist:
|
|
7
|
+
*
|
|
8
|
+
* 1. `agent/pre-step` — rewrite the model-facing `<available_skills>` block of the
|
|
9
|
+
* `skill-catalog` message, so a hidden skill never reaches the context.
|
|
10
|
+
* 2. `ctx.tools.guard` — refuse a `skill` call for a hidden name.
|
|
11
|
+
*
|
|
12
|
+
* Installing this plugin changes nothing: with no `$DSH_HOME/skill-gate.json`
|
|
13
|
+
* there is no list, so every preset passes through untouched.
|
|
14
|
+
*
|
|
15
|
+
* Three rules this file keeps, each one paid for by a measurement
|
|
16
|
+
* (`cangku/tmp/261119_k4m_gate1.md`):
|
|
17
|
+
*
|
|
18
|
+
* - The listener registers with `{ prepend: true }`. Cordis runs a waterfall
|
|
19
|
+
* outermost-first (cordis/lib/index.js:903-918), so a plain `on()` that mounts
|
|
20
|
+
* after `dsh-tool-skill` would sit INSIDE its listener: it would only see the
|
|
21
|
+
* decision after the host had produced it, and the host's own listener would
|
|
22
|
+
* replace the work on the next step (measured: the message stays at its full
|
|
23
|
+
* 9,917 B and nothing is filtered).
|
|
24
|
+
* - `source.entries` is passed through untouched. `dsh-tool-skill` digests the
|
|
25
|
+
* entries of the PERSISTED catalog message (dsh-tool-skill/lib/index.js
|
|
26
|
+
* 1065-1083); filtering them makes the persisted digest disagree with the live
|
|
27
|
+
* snapshot on every step, and the host answers by re-sending a full catalog each
|
|
28
|
+
* step (measured: +1 `update` message per step).
|
|
29
|
+
* - Nothing throws. The listener is the outermost one, so an exception would
|
|
30
|
+
* reach the agent loop's `preStep` catch and fail the whole turn
|
|
31
|
+
* (dsh-agent-loop/lib/index.js:709-712).
|
|
32
|
+
*
|
|
33
|
+
* Zero tools, zero system-prompt sections, zero injected messages: this plugin
|
|
34
|
+
* exists to save context, so it must not spend any.
|
|
35
|
+
*/
|
|
36
|
+
import { join } from "node:path";
|
|
37
|
+
import {
|
|
38
|
+
CONFIG_FILENAME,
|
|
39
|
+
ConfigLoader,
|
|
40
|
+
catalogFormLabel,
|
|
41
|
+
dshHomeOf,
|
|
42
|
+
filterCatalogText,
|
|
43
|
+
guardVerdict,
|
|
44
|
+
isHiddenByPolicy,
|
|
45
|
+
resolvePresetPolicy,
|
|
46
|
+
} from "./lib/policy.js";
|
|
47
|
+
|
|
48
|
+
/** Package name; also the module id the loader registers. */
|
|
49
|
+
export const name = "dsh-skill-gate";
|
|
50
|
+
|
|
51
|
+
/** Only the guard needs a service; `agent/pre-step` is an event and `agentPresets` is optional. */
|
|
52
|
+
export const inject = ["tools"];
|
|
53
|
+
|
|
54
|
+
/** Prefix of every log line this plugin writes. */
|
|
55
|
+
const LOG_PREFIX = "[skill-gate]";
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The reason a hidden skill gives when the model tries to load it. Chinese, like
|
|
59
|
+
* every other guard refusal on this machine (`dsh-tool-skill`'s own errors, the
|
|
60
|
+
* sandbox's framed denials all render in the host's language); it is an error
|
|
61
|
+
* string, not a rendered list, so it does not follow the catalog's English.
|
|
62
|
+
* @param skillName - the refused skill name.
|
|
63
|
+
* @returns the refusal text handed to the model.
|
|
64
|
+
*/
|
|
65
|
+
export function hiddenReason(skillName) {
|
|
66
|
+
return `技能「${skillName}」在本预设中被隐藏(dsh-skill-gate)`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Composed preset id of one agent, or "" when it cannot be determined.
|
|
71
|
+
* Mirrors `dsh-scope-manager/lib/index.js:522-533`: the preset service is an
|
|
72
|
+
* optional dependency, so a missing or throwing service degrades to "" (= do not
|
|
73
|
+
* filter) instead of failing the step.
|
|
74
|
+
* @param ctx - the plugin context.
|
|
75
|
+
* @param agent - the agent of the current step.
|
|
76
|
+
* @returns the preset id, or "".
|
|
77
|
+
*/
|
|
78
|
+
export function presetIdOfAgent(ctx, agent) {
|
|
79
|
+
const presets = ctx.get("agentPresets", false);
|
|
80
|
+
if (!presets || typeof presets.composedPreset !== "function") return "";
|
|
81
|
+
try {
|
|
82
|
+
return String(presets.composedPreset(agent?.ctx) ?? "");
|
|
83
|
+
} catch {
|
|
84
|
+
return "";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Mount the gate.
|
|
90
|
+
* @param ctx - Cordis context.
|
|
91
|
+
* @param config - reserved; the gate reads nothing from it (the list lives in
|
|
92
|
+
* `$DSH_HOME/skill-gate.json`, so it can be edited without recomposing).
|
|
93
|
+
*/
|
|
94
|
+
export function apply(ctx, config = {}) {
|
|
95
|
+
void config;
|
|
96
|
+
const logger = ctx.logger;
|
|
97
|
+
const warn = (message) => {
|
|
98
|
+
try {
|
|
99
|
+
logger?.warn?.(`${LOG_PREFIX} ${message}`);
|
|
100
|
+
} catch {
|
|
101
|
+
/* logging must never break a step */
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const configFile = join(dshHomeOf(process.env), CONFIG_FILENAME);
|
|
105
|
+
const loader = new ConfigLoader({ file: configFile, warn });
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Policy for one agent's preset.
|
|
109
|
+
* @param agent - the agent owning the step or the tool call.
|
|
110
|
+
* @returns a resolved policy, or null when the preset must be left alone.
|
|
111
|
+
*/
|
|
112
|
+
function policyOf(agent) {
|
|
113
|
+
const presetId = presetIdOfAgent(ctx, agent);
|
|
114
|
+
if (presetId === "") return null;
|
|
115
|
+
return resolvePresetPolicy(loader.read(), presetId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One step: run the chain, then rewrite the catalog message in place.
|
|
120
|
+
* @param payload - `{ agent, messages, signal }` from the waterfall.
|
|
121
|
+
* @param next - the next listener in the chain; must be called exactly once.
|
|
122
|
+
* @returns the decision, with the catalog text filtered when a policy applies.
|
|
123
|
+
*/
|
|
124
|
+
async function rewriteCatalog(payload, next) {
|
|
125
|
+
const decision = await next();
|
|
126
|
+
try {
|
|
127
|
+
if (decision === undefined || decision === null || decision.kind === "reject") return decision;
|
|
128
|
+
const messages = decision.messages;
|
|
129
|
+
if (!Array.isArray(messages)) return decision;
|
|
130
|
+
const index = messages.findIndex((message) => message?.source?.kind === "skill-catalog");
|
|
131
|
+
if (index < 0) return decision;
|
|
132
|
+
const policy = policyOf(payload?.agent);
|
|
133
|
+
if (policy === null) return decision;
|
|
134
|
+
const message = messages[index];
|
|
135
|
+
const content = Array.isArray(message.content) ? message.content : [];
|
|
136
|
+
const textPart = content[0];
|
|
137
|
+
if (textPart === undefined || typeof textPart.text !== "string") return decision;
|
|
138
|
+
const filtered = filterCatalogText(textPart.text, policy);
|
|
139
|
+
if (!filtered.changed) return decision;
|
|
140
|
+
const before = Buffer.byteLength(textPart.text, "utf8");
|
|
141
|
+
const after = Buffer.byteLength(filtered.text, "utf8");
|
|
142
|
+
logger?.debug?.(
|
|
143
|
+
`${LOG_PREFIX} preset=${presetIdOfAgent(ctx, payload?.agent)} mode=${policy.mode} `
|
|
144
|
+
+ `form=${catalogFormLabel(message.source)} kept=${filtered.kept}/${filtered.total} bytes=${before}->${after}`,
|
|
145
|
+
);
|
|
146
|
+
const replacement = {
|
|
147
|
+
...message,
|
|
148
|
+
content: [{ ...textPart, text: filtered.text }, ...content.slice(1)],
|
|
149
|
+
};
|
|
150
|
+
const nextMessages = messages.slice();
|
|
151
|
+
nextMessages[index] = replacement;
|
|
152
|
+
return { ...decision, messages: nextMessages };
|
|
153
|
+
} catch (err) {
|
|
154
|
+
warn(`catalog rewrite skipped: ${err?.message ?? err}`);
|
|
155
|
+
return decision;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
ctx.on("agent/pre-step", rewriteCatalog, { prepend: true });
|
|
160
|
+
|
|
161
|
+
const releaseGuard = ctx.tools.guard((exec) => guardVerdict(exec, policyOf, hiddenReason));
|
|
162
|
+
if (typeof releaseGuard === "function") ctx.effect?.(() => releaseGuard);
|
|
163
|
+
|
|
164
|
+
// Read-only view for an operator poking at a live host; it registers no tool.
|
|
165
|
+
ctx.skillGate = {
|
|
166
|
+
configFile,
|
|
167
|
+
policyOf,
|
|
168
|
+
isHiddenByPolicy,
|
|
169
|
+
};
|
|
170
|
+
}
|
package/lib/policy.js
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure rules behind `dsh-skill-gate` — everything that can be decided without a
|
|
3
|
+
* running Cordis host lives here, so the test suite can assert the contract
|
|
4
|
+
* against the same code the plugin runs.
|
|
5
|
+
*
|
|
6
|
+
* Two rules exist:
|
|
7
|
+
*
|
|
8
|
+
* - `resolvePresetPolicy` turns one `skill-gate.json` document plus one preset
|
|
9
|
+
* name into `{ mode, names, matchers } | null`. `null` means "do not touch
|
|
10
|
+
* this preset", which is the answer for every unconfigured preset, for
|
|
11
|
+
* `enabled: false`, and for every configuration that could not be trusted.
|
|
12
|
+
* - `filterCatalogText` rewrites the model-facing catalog text. It keeps the
|
|
13
|
+
* original line objects, never a re-rendered description: the catalog lines are
|
|
14
|
+
* produced by `@deepseek-ai/dsh-tool-skill` through `escapeText`, so
|
|
15
|
+
* re-escaping would double-escape `& < >`.
|
|
16
|
+
*
|
|
17
|
+
* Why the text and not `source.entries` (gate-1 evidence, 2026-11-19):
|
|
18
|
+
* `dsh-tool-skill` keys its "did the catalog change" decision on a digest of
|
|
19
|
+
* `source.entries` read back from the PERSISTED session event
|
|
20
|
+
* (`catalogHistory`, dsh-tool-skill/lib/index.js 1065-1083). Rewriting the
|
|
21
|
+
* entries makes the persisted digest disagree with the live one on every step,
|
|
22
|
+
* so the host re-renders a full catalog every step; leaving them alone keeps the
|
|
23
|
+
* digest stable and the host drops its own message on the next step instead of
|
|
24
|
+
* re-sending it. See cangku/tmp/261119_k4m_gate1.md.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { statSync, readFileSync } from "node:fs";
|
|
28
|
+
import { homedir } from "node:os";
|
|
29
|
+
import { join } from "node:path";
|
|
30
|
+
|
|
31
|
+
/** Config file name, directly under `$DSH_HOME`. */
|
|
32
|
+
export const CONFIG_FILENAME = "skill-gate.json";
|
|
33
|
+
|
|
34
|
+
/** The example file next to the package; documentation only, never read by the plugin. */
|
|
35
|
+
export const EXAMPLE_FILENAME = "skill-gate.example.json";
|
|
36
|
+
|
|
37
|
+
/** The two list shapes: `allow` keeps only matches, `deny` drops matches. */
|
|
38
|
+
export const MODES = ["allow", "deny"];
|
|
39
|
+
|
|
40
|
+
/** Shown in place of the entries when a preset hides every skill. */
|
|
41
|
+
export const EMPTY_NOTICE = "- No skills are available in this preset (dsh-skill-gate)";
|
|
42
|
+
|
|
43
|
+
/** Opens and closes the model-facing list inside the catalog message. */
|
|
44
|
+
export const SKILLS_OPEN = "<available_skills>";
|
|
45
|
+
export const SKILLS_CLOSE = "</available_skills>";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Escape a literal name into a regular expression that matches the whole name.
|
|
49
|
+
* Every regex metacharacter is escaped, then `\*` becomes `.*`, so `office*` and
|
|
50
|
+
* `role-*` work while `x.y` stays literal.
|
|
51
|
+
* @param pattern - a configured name, possibly containing `*`.
|
|
52
|
+
* @returns an anchored regular expression source.
|
|
53
|
+
*/
|
|
54
|
+
export function nameToRegExpSource(pattern) {
|
|
55
|
+
const escaped = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
56
|
+
return `^${escaped.replace(/\\\*/g, ".*")}$`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether one skill name is hidden by one policy.
|
|
61
|
+
* @param policy - a resolved policy from {@link resolvePresetPolicy}.
|
|
62
|
+
* @param name - the skill name to test.
|
|
63
|
+
* @returns whether the skill must not be shown or loaded.
|
|
64
|
+
*/
|
|
65
|
+
export function isHiddenByPolicy(policy, name) {
|
|
66
|
+
if (policy === null || policy === undefined) return false;
|
|
67
|
+
if (typeof name !== "string" || name === "") return false;
|
|
68
|
+
const hit = policy.matchers.some((re) => re.test(name));
|
|
69
|
+
return policy.mode === "allow" ? !hit : hit;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isPlainObject(value) {
|
|
73
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Validate one preset entry of the config document.
|
|
78
|
+
* @param entry - the raw `presets[<name>]` value.
|
|
79
|
+
* @returns `{ ok: true, enabled, mode, names }` or `{ ok: false, reason }`.
|
|
80
|
+
*/
|
|
81
|
+
export function validatePresetEntry(entry) {
|
|
82
|
+
if (!isPlainObject(entry)) return { ok: false, reason: "preset entry is not an object" };
|
|
83
|
+
const enabled = entry.enabled ?? true;
|
|
84
|
+
if (typeof enabled !== "boolean") return { ok: false, reason: "enabled must be a boolean" };
|
|
85
|
+
const mode = entry.mode;
|
|
86
|
+
if (typeof mode !== "string" || !MODES.includes(mode)) {
|
|
87
|
+
return { ok: false, reason: `mode must be one of ${MODES.join("/")}` };
|
|
88
|
+
}
|
|
89
|
+
const names = entry.names;
|
|
90
|
+
if (!Array.isArray(names)) return { ok: false, reason: "names must be an array" };
|
|
91
|
+
for (const name of names) {
|
|
92
|
+
if (typeof name !== "string" || name === "") return { ok: false, reason: "names must be non-empty strings" };
|
|
93
|
+
}
|
|
94
|
+
return { ok: true, enabled, mode, names: [...names] };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Parse a config document into a lookup keyed by preset name.
|
|
99
|
+
* Never throws: an unreadable document yields an empty map plus one reason.
|
|
100
|
+
* @param text - raw file contents.
|
|
101
|
+
* @returns `{ presets: Map, error: string | null }`.
|
|
102
|
+
*/
|
|
103
|
+
export function parseConfigDocument(text) {
|
|
104
|
+
let parsed;
|
|
105
|
+
try {
|
|
106
|
+
parsed = JSON.parse(text);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
return { presets: new Map(), error: `invalid JSON (${err?.message ?? err})` };
|
|
109
|
+
}
|
|
110
|
+
if (!isPlainObject(parsed)) return { presets: new Map(), error: "config root is not an object" };
|
|
111
|
+
const rawPresets = parsed.presets;
|
|
112
|
+
if (rawPresets === undefined) return { presets: new Map(), error: null };
|
|
113
|
+
if (!isPlainObject(rawPresets)) return { presets: new Map(), error: "presets is not an object" };
|
|
114
|
+
const presets = new Map();
|
|
115
|
+
const errors = [];
|
|
116
|
+
for (const [presetName, entry] of Object.entries(rawPresets)) {
|
|
117
|
+
const checked = validatePresetEntry(entry);
|
|
118
|
+
if (!checked.ok) {
|
|
119
|
+
errors.push(`presets.${presetName}: ${checked.reason}`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
presets.set(presetName, { enabled: checked.enabled, mode: checked.mode, names: checked.names });
|
|
123
|
+
}
|
|
124
|
+
return { presets, error: errors.length > 0 ? errors.join("; ") : null };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Resolve one preset's policy out of a parsed config.
|
|
129
|
+
* @param presets - the map from {@link parseConfigDocument}.
|
|
130
|
+
* @param presetId - the composed preset id of the calling agent ("" = unknown).
|
|
131
|
+
* @returns `{ mode, names, matchers }` or `null` when the preset must be left alone.
|
|
132
|
+
*/
|
|
133
|
+
export function resolvePresetPolicy(presets, presetId) {
|
|
134
|
+
if (!(presets instanceof Map) || typeof presetId !== "string" || presetId === "") return null;
|
|
135
|
+
const entry = presets.get(presetId);
|
|
136
|
+
if (entry === undefined) return null;
|
|
137
|
+
if (entry.enabled === false) return null;
|
|
138
|
+
return {
|
|
139
|
+
mode: entry.mode,
|
|
140
|
+
names: [...entry.names],
|
|
141
|
+
matchers: entry.names.map((pattern) => new RegExp(nameToRegExpSource(pattern))),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Rewrite the model-facing catalog text.
|
|
147
|
+
*
|
|
148
|
+
* The list is located by its opening and closing marker lines; every line in
|
|
149
|
+
* between is kept verbatim unless its parsed name is hidden. Lines that do not
|
|
150
|
+
* parse as `- \`name\`: description` are kept as-is (never dropped silently, and
|
|
151
|
+
* never mistaken for an entry). When every entry is hidden the open/close pair
|
|
152
|
+
* stays, with one notice line between them, so the message itself — and therefore
|
|
153
|
+
* the host's catalog digest — is untouched.
|
|
154
|
+
* @param text - the catalog message text.
|
|
155
|
+
* @param policy - a resolved policy, or null to pass through.
|
|
156
|
+
* @returns `{ text, kept, total, changed }`.
|
|
157
|
+
*/
|
|
158
|
+
export function filterCatalogText(text, policy) {
|
|
159
|
+
if (typeof text !== "string" || policy === null || policy === undefined) {
|
|
160
|
+
return { text, kept: 0, total: 0, changed: false };
|
|
161
|
+
}
|
|
162
|
+
const lines = text.split("\n");
|
|
163
|
+
const open = lines.indexOf(SKILLS_OPEN);
|
|
164
|
+
const close = lines.indexOf(SKILLS_CLOSE);
|
|
165
|
+
if (open < 0 || close < 0 || close < open) return { text, kept: 0, total: 0, changed: false };
|
|
166
|
+
const body = [];
|
|
167
|
+
let kept = 0;
|
|
168
|
+
let total = 0;
|
|
169
|
+
for (let index = open + 1; index < close; index += 1) {
|
|
170
|
+
const line = lines[index];
|
|
171
|
+
const match = /^- `([^`]*)`: /.exec(line);
|
|
172
|
+
if (match === null) {
|
|
173
|
+
body.push(line);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
total += 1;
|
|
177
|
+
if (isHiddenByPolicy(policy, match[1])) continue;
|
|
178
|
+
kept += 1;
|
|
179
|
+
body.push(line);
|
|
180
|
+
}
|
|
181
|
+
if (kept === 0 && total > 0) body.push(EMPTY_NOTICE);
|
|
182
|
+
const rewritten = [
|
|
183
|
+
...lines.slice(0, open + 1),
|
|
184
|
+
...body,
|
|
185
|
+
...lines.slice(close),
|
|
186
|
+
].join("\n");
|
|
187
|
+
return { text: rewritten, kept, total, changed: rewritten !== text };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Log label for the catalog message shape. Both shapes carry the same list, so
|
|
192
|
+
* the rewrite is identical; the label only keeps the log honest about which
|
|
193
|
+
* renderer produced the message.
|
|
194
|
+
* @param source - a message `source`.
|
|
195
|
+
* @returns `"catalog"` for the first catalog, `"update"` for a re-published one.
|
|
196
|
+
*/
|
|
197
|
+
export function catalogFormLabel(source) {
|
|
198
|
+
return source?.update === true ? "update" : "catalog";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* `$DSH_HOME`, falling back to `~/.dsh` exactly like the profile launcher does.
|
|
203
|
+
* @param env - process environment (injected for tests).
|
|
204
|
+
* @returns the absolute home directory.
|
|
205
|
+
*/
|
|
206
|
+
export function dshHomeOf(env = process.env) {
|
|
207
|
+
const explicit = env?.DSH_HOME;
|
|
208
|
+
if (typeof explicit === "string" && explicit !== "") return explicit;
|
|
209
|
+
return join(homedir(), ".dsh");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* mtime-keyed config reader: one `statSync` per call, a re-read only when the
|
|
214
|
+
* mtime or size moved. A missing file is the normal state (the plugin ships no
|
|
215
|
+
* list), so it is silent; an unreadable or malformed file warns once.
|
|
216
|
+
*/
|
|
217
|
+
export class ConfigLoader {
|
|
218
|
+
/**
|
|
219
|
+
* @param options.file - absolute config path.
|
|
220
|
+
* @param options.warn - sink for the one-line warnings; never throws upstream.
|
|
221
|
+
*/
|
|
222
|
+
constructor({ file, warn }) {
|
|
223
|
+
this.file = file;
|
|
224
|
+
this.warn = warn ?? (() => {});
|
|
225
|
+
/** @type {string | null} */
|
|
226
|
+
this.stamp = null;
|
|
227
|
+
/** @type {Map<string, {enabled: boolean, mode: string, names: string[]}>} */
|
|
228
|
+
this.presets = new Map();
|
|
229
|
+
/** Last reported problem, so the same state warns once. */
|
|
230
|
+
this.reported = null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Read the config if it changed since the previous call.
|
|
235
|
+
* @returns the current preset map (never null; empty means "allow everything").
|
|
236
|
+
*/
|
|
237
|
+
read() {
|
|
238
|
+
let stamp;
|
|
239
|
+
try {
|
|
240
|
+
const info = statSync(this.file);
|
|
241
|
+
stamp = `${info.mtimeMs}:${info.size}`;
|
|
242
|
+
} catch (err) {
|
|
243
|
+
stamp = `missing:${err?.code ?? "unknown"}`;
|
|
244
|
+
}
|
|
245
|
+
if (stamp === this.stamp) return this.presets;
|
|
246
|
+
this.stamp = stamp;
|
|
247
|
+
this.presets = new Map();
|
|
248
|
+
if (stamp.startsWith("missing:")) {
|
|
249
|
+
// No config is the shipped default: every preset untouched, no warning.
|
|
250
|
+
this.reported = null;
|
|
251
|
+
return this.presets;
|
|
252
|
+
}
|
|
253
|
+
let text;
|
|
254
|
+
try {
|
|
255
|
+
text = readFileSync(this.file, "utf8");
|
|
256
|
+
} catch (err) {
|
|
257
|
+
this.report(`cannot read ${this.file} (${err?.message ?? err})`);
|
|
258
|
+
return this.presets;
|
|
259
|
+
}
|
|
260
|
+
const parsed = parseConfigDocument(text);
|
|
261
|
+
this.presets = parsed.presets;
|
|
262
|
+
this.report(parsed.error === null ? null : `${this.file}: ${parsed.error}`);
|
|
263
|
+
return this.presets;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Report a problem once per distinct text; a clean read clears the latch so a
|
|
268
|
+
* later breakage is reported again.
|
|
269
|
+
* @param problem - the warning text, or null when the read was clean.
|
|
270
|
+
*/
|
|
271
|
+
report(problem) {
|
|
272
|
+
if (problem === this.reported) return;
|
|
273
|
+
this.reported = problem;
|
|
274
|
+
if (problem !== null) this.warn(problem);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Guard verdict for one `skill` tool call.
|
|
280
|
+
* @param exec - `{ name, arguments, agent }` from `ctx.tools.guard`.
|
|
281
|
+
* @param policyOf - `(agent) => policy | null`.
|
|
282
|
+
* @param reasonOf - `(name) => string` for the refusal text.
|
|
283
|
+
* @returns the refusal string, or undefined to allow the call.
|
|
284
|
+
*/
|
|
285
|
+
export function guardVerdict(exec, policyOf, reasonOf) {
|
|
286
|
+
const name = String(exec?.name ?? "");
|
|
287
|
+
if (name !== "skill") return undefined;
|
|
288
|
+
const skillName = String(exec?.arguments?.name ?? "");
|
|
289
|
+
if (skillName === "") return undefined;
|
|
290
|
+
let policy = null;
|
|
291
|
+
try {
|
|
292
|
+
policy = policyOf(exec?.agent);
|
|
293
|
+
} catch {
|
|
294
|
+
policy = null;
|
|
295
|
+
}
|
|
296
|
+
if (!isHiddenByPolicy(policy, skillName)) return undefined;
|
|
297
|
+
return reasonOf(skillName);
|
|
298
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-skill-gate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Hide skills per agent preset: a filtered skill catalog for the model, and a refused `skill` call for what is hidden. Ships with no list — installing it changes nothing until you write skill-gate.json.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "MeruSu"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0-0"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"dsh",
|
|
15
|
+
"dsh-plugin",
|
|
16
|
+
"deepseek-harness",
|
|
17
|
+
"skill",
|
|
18
|
+
"context",
|
|
19
|
+
"preset"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./index.js",
|
|
25
|
+
"./lib/*.js": "./lib/*.js",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"index.js",
|
|
30
|
+
"lib",
|
|
31
|
+
"cordis.patch.yml",
|
|
32
|
+
"skill-gate.example.json",
|
|
33
|
+
"README.md",
|
|
34
|
+
"CHANGELOG.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"dsh": {
|
|
38
|
+
"bundle": {
|
|
39
|
+
"patch": "./cordis.patch.yml"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "node test/policy.test.mjs && node test/plugin.test.mjs"
|
|
44
|
+
}
|
|
6
45
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"presets": {
|
|
4
|
+
"dev": {
|
|
5
|
+
"mode": "allow",
|
|
6
|
+
"names": [
|
|
7
|
+
"plugin-work",
|
|
8
|
+
"web-work",
|
|
9
|
+
"motion-work",
|
|
10
|
+
"dsh-plugin-dev",
|
|
11
|
+
"cordis-composition-reference",
|
|
12
|
+
"cangku-snapshots",
|
|
13
|
+
"long-session-fold"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"minimal": { "mode": "allow", "names": ["cangku-snapshots"] },
|
|
17
|
+
"clerk": { "mode": "deny", "names": ["office*", "univer*"] },
|
|
18
|
+
"scholar": { "enabled": false, "mode": "deny", "names": [] }
|
|
19
|
+
}
|
|
20
|
+
}
|