dsh-work-fish 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +76 -2
- package/THIRD-PARTY-NOTICES.md +59 -0
- package/board/client.js +1041 -0
- package/cordis.patch.yml +172 -0
- package/core/approve-paths.mjs +387 -0
- package/core/approve-paths.test.mjs +241 -0
- package/core/approve.mjs +182 -0
- package/core/bootstrap.mjs +185 -0
- package/core/events.mjs +80 -0
- package/core/guard.mjs +155 -0
- package/core/index.mjs +637 -0
- package/core/parse.mjs +310 -0
- package/core/persona.mjs +98 -0
- package/core/report.mjs +370 -0
- package/core/skills.mjs +111 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.webp +0 -0
- package/icon.png +0 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +69 -4
- package/rules/code-rules.md +92 -0
- package/rules/experience-pool.md +59 -0
- package/rules/file-rules.md +134 -0
- package/rules/memory-rules.md +91 -0
- package/rules/task-rules.md +71 -0
- package/skills/code-review/LICENSE +202 -0
- package/skills/code-review/NOTICE.md +75 -0
- package/skills/code-review/SKILL.md +155 -0
- package/skills/code-review/upstream/README.md +258 -0
- package/skills/code-review/upstream/code-review.command.md +110 -0
- package/skills/code-review/upstream/plugin.json +10 -0
- package/skills/code-simplifier/LICENSE +202 -0
- package/skills/code-simplifier/NOTICE.md +72 -0
- package/skills/code-simplifier/SKILL.md +126 -0
- package/skills/code-simplifier/agents/code-simplifier.md +52 -0
- package/skills/code-simplifier/upstream/plugin.json +9 -0
- package/skills/comfyui-h3/SKILL.md +455 -0
- package/skills/comfyui-h3/reference/failures.md +84 -0
- package/skills/comfyui-h3/reference/serial-production.md +173 -0
- package/skills/delete-gate/SKILL.md +48 -0
- package/skills/doc-writing/SKILL.md +152 -0
- package/skills/doc-writing/reference/skeletons.md +81 -0
- package/skills/feature-dev/LICENSE +202 -0
- package/skills/feature-dev/NOTICE.md +67 -0
- package/skills/feature-dev/SKILL.md +181 -0
- package/skills/feature-dev/agents/code-architect.md +34 -0
- package/skills/feature-dev/agents/code-explorer.md +51 -0
- package/skills/feature-dev/agents/code-reviewer.md +46 -0
- package/skills/feature-dev/upstream/README.md +412 -0
- package/skills/feature-dev/upstream/feature-dev.command.md +125 -0
- package/skills/feature-dev/upstream/plugin.json +9 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +71 -0
- package/skills/git-flow/SKILL.md +80 -0
- package/skills/plan-revision/SKILL.md +54 -0
- package/skills/task-setup/SKILL.md +124 -0
- package/template/AGENTS.template.md +28 -0
- package/template/memory/pool/experience.md +9 -0
- package/template/memory/preferences.md +17 -0
- package/template/memory/projects/README.md +6 -0
- package/template/memory/user-profile.md +33 -0
- package/template/persona-core.md +32 -0
- package/template/persona-user.md +0 -0
- package/template/rules/user-rules.md +20 -0
- package/template/rules-index.md +50 -0
- package/template/tasks/index.md +34 -0
- package/tools/lib/paths.mjs +31 -0
- package/tools/lib/persona.mjs +454 -0
- package/tools/lib/taskboard.mjs +708 -0
- package/tools/wf-audit.cmd +3 -0
- package/tools/wf-audit.ps1 +242 -0
- package/tools/wf-mem.mjs +336 -0
- package/tools/wf-metrics.mjs +601 -0
- package/tools/wf-persona.mjs +337 -0
- package/tools/wf-pool.mjs +250 -0
- package/tools/wf-preview.mjs +240 -0
- package/tools/wf-recent.mjs +168 -0
- package/tools/wf-review.mjs +650 -0
- package/tools/wf-rule.mjs +407 -0
- package/tools/wf-snapshot.mjs +100 -0
- package/tools/wf-spec.mjs +882 -0
- package/tools/wf-task.mjs +298 -0
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Work Fish bundle: one preset plus the ledger host behind the task panel.
|
|
2
|
+
#
|
|
3
|
+
# Three rows are inserted:
|
|
4
|
+
# 1. `preset-work-fish` — @deepseek-ai/dsh-agent-preset declaration; mounts the
|
|
5
|
+
# persona / tools / skills only in sessions that select this preset;
|
|
6
|
+
# 2. `work-fish-host` — the ledger host (`core/index.mjs`), a global row:
|
|
7
|
+
# installing this bundle seeds the workbench and serves `/wf/*` regardless of
|
|
8
|
+
# the selected preset. The browser half (task panel) is served from the same
|
|
9
|
+
# package through `package.json` → `dsh.client`.
|
|
10
|
+
# 3. `work-fish-approve` — answers escalation approvals on the user's behalf. Three
|
|
11
|
+
# kinds of root are trusted: this bundle's own package directory (plus, in a dev
|
|
12
|
+
# checkout, the checkout that holds `out/`), the workbench directory itself
|
|
13
|
+
# (`<WF_HOME>`, which is not a project and could never arrive any other way),
|
|
14
|
+
# and every project registered in `<WF_HOME>/projects.json`. Global row; every
|
|
15
|
+
# verdict is appended to `<WF_HOME>/auto-approve.log`.
|
|
16
|
+
#
|
|
17
|
+
# Row ids are `preset-work-fish` / `work-fish-host` / `work-fish-approve`; to override
|
|
18
|
+
# a row, restate the whole config in the profile's cordis.patch.yml (config is replaced, not merged):
|
|
19
|
+
# - id: preset-work-fish
|
|
20
|
+
# name: '@deepseek-ai/dsh-agent-preset'
|
|
21
|
+
# config:
|
|
22
|
+
# id: work-fish
|
|
23
|
+
# ...
|
|
24
|
+
- insert:
|
|
25
|
+
- id: preset-work-fish
|
|
26
|
+
name: '@deepseek-ai/dsh-agent-preset'
|
|
27
|
+
config:
|
|
28
|
+
id: work-fish
|
|
29
|
+
name: 大肥鱼
|
|
30
|
+
description: 承接日常工作的常驻助手:接单澄清、拆解执行、子代理协作、台账跟踪、按时交付。
|
|
31
|
+
order: 5
|
|
32
|
+
plugins:
|
|
33
|
+
# ── 身份 ────────────────────────────────────────────────────────
|
|
34
|
+
# 人设:骨架随插件升级(template/persona-core.md),用户段在工作台 persona.md。
|
|
35
|
+
# 段落名与 @deepseek-ai/dsh-persona 相同(deployment:persona-prefix),
|
|
36
|
+
# 所以预置里不能再挂 persona 行,否则同层重名直接抛错。
|
|
37
|
+
# complete 必须保留:没有它,dsh-llm-workbuddy 的 replace 模式会
|
|
38
|
+
# 把整段人设当成「无部署人设」丢弃,发出去的是 WorkBuddy 模板。
|
|
39
|
+
- id: wf-persona
|
|
40
|
+
name: 'dsh-work-fish/core/persona'
|
|
41
|
+
config:
|
|
42
|
+
complete: true
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# ── 上下文与记忆加载 ────────────────────────────────────────────
|
|
46
|
+
# 工作台手册(开工/台账/收工)由 wf-persona 的 complete 段一并带出,
|
|
47
|
+
# 结构上就是最高优先级的系统提示;$DSH_HOME / ~/.agents / 项目根→cwd
|
|
48
|
+
# 三条链交给 base 的 dsh-agent-instructions,本 bundle 不再另起一套。
|
|
49
|
+
- id: time-context
|
|
50
|
+
name: '@deepseek-ai/dsh-time-context'
|
|
51
|
+
|
|
52
|
+
# ── 执行:shell / 文件 / 搜索 / 后台任务 / 定时任务 ─────────────
|
|
53
|
+
- id: tool-bash
|
|
54
|
+
name: '@deepseek-ai/dsh-tool-bash'
|
|
55
|
+
disabled: !!js process.platform === 'win32'
|
|
56
|
+
- id: tool-pwsh
|
|
57
|
+
name: '@deepseek-ai/dsh-tool-pwsh'
|
|
58
|
+
disabled: !!js process.platform !== 'win32'
|
|
59
|
+
- id: tool-fs
|
|
60
|
+
name: '@deepseek-ai/dsh-tool-fs'
|
|
61
|
+
config:
|
|
62
|
+
# 已开启:跨文件/跨区域改动走 Codex 风格补丁,一次调用改完。
|
|
63
|
+
applyPatch: true
|
|
64
|
+
# 省 token 硬帽:单次 read 最多 600 行 / 24KB / 单行 300 字符
|
|
65
|
+
readLimit: 600
|
|
66
|
+
readMaxBytes: 24576
|
|
67
|
+
readMaxLineLength: 300
|
|
68
|
+
# ── 写入守卫:拦住绕过工具的直写 ───────────────────────────────
|
|
69
|
+
- id: wf-guard
|
|
70
|
+
name: 'dsh-work-fish/core/guard'
|
|
71
|
+
- id: tool-fs-search
|
|
72
|
+
name: '@deepseek-ai/dsh-tool-fs-search'
|
|
73
|
+
config:
|
|
74
|
+
sampleOverCapGlobResults: false
|
|
75
|
+
# 省 token 硬帽:grep 命中 60 条封顶、glob 50 条、单行 300 字符
|
|
76
|
+
grepMaxMatches: 60
|
|
77
|
+
globMaxResults: 50
|
|
78
|
+
grepMaxLineBytes: 300
|
|
79
|
+
- id: tool-jobs
|
|
80
|
+
name: '@deepseek-ai/dsh-tool-jobs'
|
|
81
|
+
- id: tool-schedule
|
|
82
|
+
name: '@deepseek-ai/dsh-tool-schedule'
|
|
83
|
+
- id: tool-todo
|
|
84
|
+
name: '@deepseek-ai/dsh-tool-todo'
|
|
85
|
+
config:
|
|
86
|
+
allowParallelInProgress: true
|
|
87
|
+
- id: tool-ask-user
|
|
88
|
+
name: '@deepseek-ai/dsh-tool-ask-user'
|
|
89
|
+
- id: tool-web
|
|
90
|
+
name: '@deepseek-ai/dsh-tool-web'
|
|
91
|
+
config:
|
|
92
|
+
fetch: true
|
|
93
|
+
searchTimeoutMs: 60000
|
|
94
|
+
# 省 token 硬帽:抓网页输出 200k → 60k 字符(约 17k tokens),搜索摘要 800 → 400
|
|
95
|
+
fetchMaxOutputChars: 60000
|
|
96
|
+
searchSummaryMaxChars: 400
|
|
97
|
+
|
|
98
|
+
# ── 技能库:工作方法以 SKILL.md 形式随时加载 ─────────────────────
|
|
99
|
+
- id: skill-filesystem
|
|
100
|
+
name: '@deepseek-ai/dsh-skill-filesystem'
|
|
101
|
+
- id: wf-skills
|
|
102
|
+
name: 'dsh-work-fish/core/skills'
|
|
103
|
+
- id: tool-skill
|
|
104
|
+
name: '@deepseek-ai/dsh-tool-skill'
|
|
105
|
+
|
|
106
|
+
# ── 长期任务(goal + 方案模式):刻意不挂 ───────────────────────
|
|
107
|
+
# 2026-11-13 移除:50 个 work-fish 会话里 goal 三件套与
|
|
108
|
+
# exit_plan_mode 零调用,长任务由台账与任务卡片承担。
|
|
109
|
+
# 回滚底稿:.tmp/cordis.patch.yml.before-trim-20261113
|
|
110
|
+
|
|
111
|
+
# ── 子代理与批量编排 ───────────────────────────────────────────
|
|
112
|
+
- id: delegation
|
|
113
|
+
name: cordis:group
|
|
114
|
+
group: true
|
|
115
|
+
isolate:
|
|
116
|
+
workflowEngine: true
|
|
117
|
+
config:
|
|
118
|
+
- id: tool-subagent-control
|
|
119
|
+
name: '@deepseek-ai/dsh-tool-subagent-control'
|
|
120
|
+
- id: tool-subagent
|
|
121
|
+
name: '@deepseek-ai/dsh-tool-subagent'
|
|
122
|
+
config:
|
|
123
|
+
provider: spawn
|
|
124
|
+
toolName: subagent
|
|
125
|
+
modelSelection: true
|
|
126
|
+
modelDiscovery: true
|
|
127
|
+
maxDepth: 1
|
|
128
|
+
# 子代理继承父模型,思考降为 low:脏活省输出 token,难任务仍可按次指定
|
|
129
|
+
agentOptions:
|
|
130
|
+
reasoningEffort: low
|
|
131
|
+
- id: tool-subagent-fork
|
|
132
|
+
name: '@deepseek-ai/dsh-tool-subagent'
|
|
133
|
+
config:
|
|
134
|
+
provider: fork
|
|
135
|
+
toolName: subagent_fork
|
|
136
|
+
- id: workflow-ptc
|
|
137
|
+
name: '@deepseek-ai/dsh-workflow-ptc'
|
|
138
|
+
config:
|
|
139
|
+
provider: spawn
|
|
140
|
+
- id: tool-workflow
|
|
141
|
+
name: '@deepseek-ai/dsh-tool-workflow'
|
|
142
|
+
|
|
143
|
+
# ── 上下文压缩与交付 ───────────────────────────────────────────
|
|
144
|
+
- id: compaction
|
|
145
|
+
name: cordis:group
|
|
146
|
+
group: true
|
|
147
|
+
isolate:
|
|
148
|
+
compaction: true
|
|
149
|
+
toolResultPruner: true
|
|
150
|
+
config:
|
|
151
|
+
- id: compaction-basic
|
|
152
|
+
name: '@deepseek-ai/dsh-compaction-basic'
|
|
153
|
+
- id: command-compact
|
|
154
|
+
name: '@deepseek-ai/dsh-command-compact'
|
|
155
|
+
- id: tool-result-pruner
|
|
156
|
+
name: '@deepseek-ai/dsh-compaction-tool-result-pruner'
|
|
157
|
+
config:
|
|
158
|
+
# 压缩时把老工具结果压到更小:头 2KB + 尾 0.5KB
|
|
159
|
+
thresholdChars: 4096
|
|
160
|
+
headChars: 2048
|
|
161
|
+
tailChars: 512
|
|
162
|
+
- id: present
|
|
163
|
+
name: '@deepseek-ai/dsh-tool-present'
|
|
164
|
+
- id: work-fish-host
|
|
165
|
+
name: 'dsh-work-fish'
|
|
166
|
+
config:
|
|
167
|
+
timeZone: 'Asia/Shanghai'
|
|
168
|
+
# 自动审批:白名单=本插件自己的目录(开发仓还含 out/ 所在的那一级)+工作台目录本身(<WF_HOME>,它不是项目,没法经由 projects.json 进来)+工作台 projects.json 登记的项目。
|
|
169
|
+
# 只在该次调用『每个目标都在白名单内』时代答 allowed-once,其余照旧弹窗;改完要重启 dsh 才生效。
|
|
170
|
+
# 每次判定写一条到 <WF_HOME>/auto-approve.log,放行与否都留痕。
|
|
171
|
+
- id: work-fish-approve
|
|
172
|
+
name: 'dsh-work-fish/core/approve'
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Target inspection for Work Fish auto-approval.
|
|
3
|
+
*
|
|
4
|
+
* Pure: no Cordis, no filesystem, no clock. The plugin half (`./approve.mjs`)
|
|
5
|
+
* decides *whether* to answer an approval; this half decides *what the call
|
|
6
|
+
* touches*, which is the only part worth a unit test.
|
|
7
|
+
*
|
|
8
|
+
* The rule is one-sided on purpose: a call is auto-approvable only when every
|
|
9
|
+
* target can be named and every target sits inside one of the trusted roots
|
|
10
|
+
* this bundle's Host half assembles (its own checkout, the workbench, the
|
|
11
|
+
* registered projects). Anything unnamed — a command whose paths cannot be
|
|
12
|
+
* read, an unknown tool, a `..` in a command, an empty root list — is a pass,
|
|
13
|
+
* which lets the ordinary prompt happen.
|
|
14
|
+
*
|
|
15
|
+
* @module dsh-work-fish/core/approve-paths
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { win32 } from 'node:path'
|
|
19
|
+
|
|
20
|
+
/** Tool names whose target is `file_path` or its `path` alias, plus per-edit overrides. */
|
|
21
|
+
const FILE_PATH_TOOLS = ['write', 'edit']
|
|
22
|
+
|
|
23
|
+
/** Tool whose targets are the `paths` array. */
|
|
24
|
+
const DELETE_TOOL = 'delete'
|
|
25
|
+
|
|
26
|
+
/** Tool whose targets are every `from` and `to` of `renames`. */
|
|
27
|
+
const RENAME_TOOL = 'rename'
|
|
28
|
+
|
|
29
|
+
/** Tool name whose targets live in the patch body. */
|
|
30
|
+
const PATCH_TOOL = 'apply_patch'
|
|
31
|
+
|
|
32
|
+
/** Tool names whose targets live inside a command string. */
|
|
33
|
+
export const COMMAND_TOOLS = ['pwsh', 'bash']
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Canonicalize a directory for prefix comparison.
|
|
37
|
+
*
|
|
38
|
+
* Windows comparisons are case-insensitive and tolerate either separator, so
|
|
39
|
+
* the canonical form is resolved, stripped of trailing separators, and lowered.
|
|
40
|
+
* @param {string} value - an absolute or relative path.
|
|
41
|
+
* @returns {string} the canonical comparison form.
|
|
42
|
+
*/
|
|
43
|
+
export function canonicalDir(value) {
|
|
44
|
+
const resolved = win32.resolve(value)
|
|
45
|
+
const trimmed = resolved.replace(/[\\/]+$/, '')
|
|
46
|
+
return (trimmed === '' ? resolved : trimmed).toLowerCase()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Is `candidate` the root itself or anywhere below it?
|
|
51
|
+
* @param {string} candidate - the target path.
|
|
52
|
+
* @param {string[]} roots - canonical directories from {@link canonicalDir}.
|
|
53
|
+
* @returns {boolean} true when the target is inside one of the roots.
|
|
54
|
+
*/
|
|
55
|
+
export function isInside(candidate, roots) {
|
|
56
|
+
const target = canonicalDir(candidate)
|
|
57
|
+
return roots.some((root) => target === root || target.startsWith(root + '\\'))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parameters whose value is text, never a path.
|
|
62
|
+
*
|
|
63
|
+
* A commit message, a search pattern or a replacement string may contain a
|
|
64
|
+
* drive-letter path (`git commit -m "回滚 C:\\old\\a.ts 的逻辑"`). Counting that as
|
|
65
|
+
* a target refuses an ordinary commit, so the value of these is skipped whole.
|
|
66
|
+
*/
|
|
67
|
+
const TEXT_PARAMS = new Set([
|
|
68
|
+
'-m', '--message', '-message', '-pattern', '-replacement', '-replace', '-filter',
|
|
69
|
+
'-value', '-delimiter', '-separator', '-title', '-subject', '-body', '-comment',
|
|
70
|
+
'-description', '-note', '--note', '-e', '-expression', '-simplematch',
|
|
71
|
+
])
|
|
72
|
+
|
|
73
|
+
/** Cmdlets that move the working directory; their argument rebases later tokens. */
|
|
74
|
+
const DIRECTORY_CMDS = new Set(['cd', 'chdir', 'set-location', 'sl', 'push-location', 'pop-location'])
|
|
75
|
+
|
|
76
|
+
/** Cmdlets whose positional arguments can be paths they write to. */
|
|
77
|
+
const MUTATING_CMDS = new Set([
|
|
78
|
+
'copy-item', 'move-item', 'remove-item', 'rename-item', 'set-content', 'add-content',
|
|
79
|
+
'out-file', 'new-item', 'tee-object', 'export-csv', 'export-clixml', 'invoke-webrequest',
|
|
80
|
+
'mkdir', 'md', 'cp', 'mv', 'rm', 'del', 'copy', 'move', 'tee',
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Drop here-string bodies: their content is data, not a command line.
|
|
85
|
+
*
|
|
86
|
+
* An inline script (`@'...'@` piped into a file, or `-e`-style code) often
|
|
87
|
+
* quotes paths it never touches as arguments; parsing them as targets refuses
|
|
88
|
+
* the call for no reason.
|
|
89
|
+
* @param {string} command - the raw command string.
|
|
90
|
+
* @returns {string} the command with every here-string body replaced by a token.
|
|
91
|
+
*/
|
|
92
|
+
function stripHereStrings(command) {
|
|
93
|
+
return command.replace(/@(['"])\r?\n[\s\S]*?\r?\n\1@/g, '@HERE@')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Split a command line into tokens, honouring single and double quotes.
|
|
98
|
+
* @param {string} command - the raw command string.
|
|
99
|
+
* @returns {string[]} tokens without their quotes.
|
|
100
|
+
*/
|
|
101
|
+
export function tokenizeCommand(command) {
|
|
102
|
+
const tokens = []
|
|
103
|
+
let current = ''
|
|
104
|
+
let quoted = false
|
|
105
|
+
for (const char of stripHereStrings(command)) {
|
|
106
|
+
if (char === '"' || char === "'") {
|
|
107
|
+
quoted = !quoted
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
if (!quoted && /[\s;|(),]/.test(char)) {
|
|
111
|
+
if (current.length > 0) tokens.push(current)
|
|
112
|
+
current = ''
|
|
113
|
+
continue
|
|
114
|
+
}
|
|
115
|
+
current += char
|
|
116
|
+
}
|
|
117
|
+
if (current.length > 0) tokens.push(current)
|
|
118
|
+
return tokens
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Does this token look like something on disk?
|
|
123
|
+
*
|
|
124
|
+
* Drive-letter and UNC prefixes, anything with a separator, and bare file
|
|
125
|
+
* names (`list.txt`). A bare word (`node`, `--noEmit`) is not a path.
|
|
126
|
+
* @param {string} token - one command token.
|
|
127
|
+
* @returns {boolean} true when the token may name a file.
|
|
128
|
+
*/
|
|
129
|
+
function looksLikePath(token) {
|
|
130
|
+
if (/^[a-zA-Z]:[\\/]/.test(token)) return true
|
|
131
|
+
if (token.startsWith('\\\\')) return true
|
|
132
|
+
if (/[\\/]/.test(token)) return true
|
|
133
|
+
return /^\.[^.]*$/.test(token) || (/\.[a-zA-Z0-9]{1,5}$/.test(token) && !token.includes('://'))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Every path a command line names, resolved against its working directory.
|
|
138
|
+
*
|
|
139
|
+
* Three rules keep this honest without reading the filesystem:
|
|
140
|
+
* - the value of a text parameter is skipped, so a message or a pattern that
|
|
141
|
+
* mentions a path is not a target;
|
|
142
|
+
* - `cd` rebases the tokens after it, so `cd <dir>; ..\\node_modules\\.bin\\tsc.cmd`
|
|
143
|
+
* resolves inside the project instead of being refused for a `..`;
|
|
144
|
+
* - a token that assembles its path at run time (`$p`) cannot be named, so the
|
|
145
|
+
* call is refused rather than approved.
|
|
146
|
+
* @param {string} command - the raw command string.
|
|
147
|
+
* @param {string} [cwd] - the call working directory for relative tokens.
|
|
148
|
+
* @returns {{targets: string[], problem?: string}} resolved paths, or why they cannot be named.
|
|
149
|
+
*/
|
|
150
|
+
export function commandTargets(command, cwd = '.') {
|
|
151
|
+
const targets = []
|
|
152
|
+
let directory = cwd
|
|
153
|
+
const tokens = tokenizeCommand(command)
|
|
154
|
+
const mutating = tokens.some((item) => MUTATING_CMDS.has(item.toLowerCase()))
|
|
155
|
+
/** Literal assignments seen earlier in the same command, by variable name. */
|
|
156
|
+
const literals = new Map()
|
|
157
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
158
|
+
let token = tokens[index]
|
|
159
|
+
const assignment = /^\$([A-Za-z_][\w]*)=(.*)$/.exec(token)
|
|
160
|
+
if (assignment !== null) {
|
|
161
|
+
const [, name, value] = assignment
|
|
162
|
+
// `$p="C:\\x"` names the path right here; `$p=$other` names nothing
|
|
163
|
+
if (value.length === 0 || /^[$`]/.test(value)) {
|
|
164
|
+
literals.delete(name)
|
|
165
|
+
continue
|
|
166
|
+
}
|
|
167
|
+
literals.set(name, value)
|
|
168
|
+
token = value
|
|
169
|
+
}
|
|
170
|
+
const bare = /^\$([A-Za-z_][\w]*)$/.exec(token)
|
|
171
|
+
if (bare !== null && literals.has(bare[1])) token = literals.get(bare[1])
|
|
172
|
+
const lower = token.toLowerCase()
|
|
173
|
+
if (TEXT_PARAMS.has(lower)) {
|
|
174
|
+
index += 1
|
|
175
|
+
continue
|
|
176
|
+
}
|
|
177
|
+
if (DIRECTORY_CMDS.has(lower)) {
|
|
178
|
+
const next = tokens[index + 1]
|
|
179
|
+
if (next !== undefined && /[$`]/.test(next)) return { targets, problem: 'a directory is assembled at run time' }
|
|
180
|
+
if (next !== undefined && looksLikePath(next)) {
|
|
181
|
+
const resolved = win32.resolve(directory, next)
|
|
182
|
+
targets.push(resolved)
|
|
183
|
+
directory = resolved
|
|
184
|
+
index += 1
|
|
185
|
+
}
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
if (/^[$`]/.test(token)) {
|
|
189
|
+
// 目标从环境或另一条命令里来,本行看不见它落在哪里
|
|
190
|
+
if (mutating) return { targets, problem: 'a path is assembled at run time' }
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
if (!looksLikePath(token)) continue
|
|
194
|
+
if (/[$`%]/.test(token)) return { targets, problem: 'a path is assembled at run time' }
|
|
195
|
+
targets.push(win32.resolve(directory, token))
|
|
196
|
+
}
|
|
197
|
+
return { targets: [...new Set(targets)] }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Every absolute path a command string mentions.
|
|
202
|
+
*
|
|
203
|
+
* Kept as the plain-spelling entry point; {@link commandTargets} does the
|
|
204
|
+
* work and also explains why a call cannot be judged.
|
|
205
|
+
* @param {string} command - the raw command string.
|
|
206
|
+
* @param {string} [cwd] - the call working directory for relative tokens.
|
|
207
|
+
* @returns {string[]} resolved paths, in first-seen order.
|
|
208
|
+
*/
|
|
209
|
+
export function commandPaths(command, cwd = '.') {
|
|
210
|
+
return commandTargets(command, cwd).targets
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Every file a patch body touches.
|
|
215
|
+
* @param {string} patch - the `apply_patch` body.
|
|
216
|
+
* @returns {string[]} declared paths, in body order.
|
|
217
|
+
*/
|
|
218
|
+
export function patchPaths(patch) {
|
|
219
|
+
const found = []
|
|
220
|
+
for (const line of patch.split(/\r?\n/)) {
|
|
221
|
+
// `move <from> -> <to>` names two files in one header
|
|
222
|
+
const moved = /^move (.+) -> (.+)$/.exec(line)
|
|
223
|
+
if (moved !== null) {
|
|
224
|
+
found.push(moved[1].trim(), moved[2].trim())
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
const match =
|
|
228
|
+
/^\*\*\* (?:Update|Add|Delete) File: (.+)$/.exec(line)
|
|
229
|
+
?? /^\*\*\* Move to: (.+)$/.exec(line)
|
|
230
|
+
// section headers of the newer spelling: `file <path>`, `new <path>`, `remove <path>`
|
|
231
|
+
?? /^(?:file|new|remove) (.+)$/.exec(line)
|
|
232
|
+
if (match !== null) found.push(match[1].trim())
|
|
233
|
+
}
|
|
234
|
+
return found
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Does this command string climb out of its working directory?
|
|
239
|
+
*
|
|
240
|
+
* `cd ..` followed by a relative write lands outside every project while naming
|
|
241
|
+
* no absolute path at all, so parent traversal is refused before any path is
|
|
242
|
+
* examined.
|
|
243
|
+
* @param {string} command - the raw command string.
|
|
244
|
+
* @returns {boolean} true when the string contains a `..` segment.
|
|
245
|
+
*/
|
|
246
|
+
export function climbsOut(command) {
|
|
247
|
+
return /(?:^|[\s"'=;,()/\\])\.\.(?:[\\/]|[\s"';,)`]|$)/.test(command)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Every path a file-writing call names.
|
|
252
|
+
*
|
|
253
|
+
* `edit` accepts the default target under either `file_path` or its `path`
|
|
254
|
+
* alias, and each item of `edits` may name its own `path`; all of them count,
|
|
255
|
+
* because approval requires every target to be inside.
|
|
256
|
+
* @param {Record<string, unknown>} args - the call arguments.
|
|
257
|
+
* @returns {string[]} non-empty paths, de-duplicated, in declaration order.
|
|
258
|
+
*/
|
|
259
|
+
function filePathsOf(args) {
|
|
260
|
+
const found = stringList([args.file_path, args.path])
|
|
261
|
+
for (const item of Array.isArray(args.edits) ? args.edits : []) {
|
|
262
|
+
found.push(...stringList([item?.path]))
|
|
263
|
+
}
|
|
264
|
+
return [...new Set(found)]
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The non-empty strings of a candidate list.
|
|
269
|
+
* @param {unknown} values - candidates; a non-array yields nothing.
|
|
270
|
+
* @returns {string[]} the usable entries.
|
|
271
|
+
*/
|
|
272
|
+
function stringList(values) {
|
|
273
|
+
const list = Array.isArray(values) ? values : []
|
|
274
|
+
return list.filter((value) => typeof value === 'string' && value.length > 0)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Resolve the targets a single tool call declares.
|
|
279
|
+
* @param {{name: string, args: Record<string, unknown>, cwd?: string}} call - the pending call.
|
|
280
|
+
* @returns {{targets: string[], problem?: string}} absolute targets, or why they cannot be named.
|
|
281
|
+
*/
|
|
282
|
+
export function targetsOf(call) {
|
|
283
|
+
const args = call.args ?? {}
|
|
284
|
+
const cwd = call.cwd ?? '.'
|
|
285
|
+
if (PATCH_TOOL === call.name) {
|
|
286
|
+
const body = typeof args.patch === 'string' ? args.patch : undefined
|
|
287
|
+
if (body === undefined) return { targets: [], problem: 'apply_patch without a patch body' }
|
|
288
|
+
const declared = patchPaths(body)
|
|
289
|
+
if (declared.length === 0) return { targets: [], problem: 'apply_patch declares no file' }
|
|
290
|
+
return { targets: declared.map((value) => win32.resolve(cwd, value)) }
|
|
291
|
+
}
|
|
292
|
+
if (FILE_PATH_TOOLS.includes(call.name)) {
|
|
293
|
+
const named = filePathsOf(args)
|
|
294
|
+
if (named.length === 0) return { targets: [], problem: `${call.name} without file_path` }
|
|
295
|
+
return { targets: named.map((value) => win32.resolve(cwd, value)) }
|
|
296
|
+
}
|
|
297
|
+
if (DELETE_TOOL === call.name) {
|
|
298
|
+
const named = stringList(args.paths)
|
|
299
|
+
if (named.length === 0) return { targets: [], problem: 'delete without paths' }
|
|
300
|
+
return { targets: named.map((value) => win32.resolve(cwd, value)) }
|
|
301
|
+
}
|
|
302
|
+
if (RENAME_TOOL === call.name) {
|
|
303
|
+
const named = []
|
|
304
|
+
for (const item of Array.isArray(args.renames) ? args.renames : []) {
|
|
305
|
+
named.push(...stringList([item?.from, item?.to]))
|
|
306
|
+
}
|
|
307
|
+
if (named.length === 0) return { targets: [], problem: 'rename without renames' }
|
|
308
|
+
return { targets: named.map((value) => win32.resolve(cwd, value)) }
|
|
309
|
+
}
|
|
310
|
+
if (COMMAND_TOOLS.includes(call.name)) {
|
|
311
|
+
const command = args.command
|
|
312
|
+
if (typeof command !== 'string' || command.length === 0) return { targets: [], problem: `${call.name} without a command` }
|
|
313
|
+
const { targets, problem } = commandTargets(command, win32.resolve(cwd))
|
|
314
|
+
if (problem !== undefined) return { targets, problem }
|
|
315
|
+
if (targets.length === 0) return { targets: [], problem: 'command names no absolute path' }
|
|
316
|
+
return { targets }
|
|
317
|
+
}
|
|
318
|
+
return { targets: [], problem: `tool "${call.name}" is not covered` }
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Roots a bundle may touch without asking.
|
|
323
|
+
*
|
|
324
|
+
* The bundle's own package directory is always trusted: maintaining the plugin
|
|
325
|
+
* (previews, tests, sources) is not the project work the whitelist guards. When
|
|
326
|
+
* the package sits in a dev checkout — its parent is not `node_modules` — the
|
|
327
|
+
* checkout root joins too, because the preview output lives beside the package
|
|
328
|
+
* in `out/`, not inside it.
|
|
329
|
+
*
|
|
330
|
+
* @param {string} packageDir - absolute path of the package directory.
|
|
331
|
+
* @returns {string[]} canonical roots, package first.
|
|
332
|
+
*/
|
|
333
|
+
export function bundleRoots(packageDir) {
|
|
334
|
+
const resolved = win32.resolve(packageDir)
|
|
335
|
+
const parent = win32.dirname(resolved)
|
|
336
|
+
const roots = [canonicalDir(resolved)]
|
|
337
|
+
const parentIsInstall = /[\\/]node_modules[\\/]/i.test(`${parent}\\`)
|
|
338
|
+
if (!parentIsInstall && canonicalDir(parent) !== roots[0]) roots.push(canonicalDir(parent))
|
|
339
|
+
return roots
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Every root one approval may be answered inside, canonically and deduplicated.
|
|
344
|
+
*
|
|
345
|
+
* Three kinds join here. The bundle's own roots come first, because maintaining
|
|
346
|
+
* the plugin is never judged against a project. The workbench directory is
|
|
347
|
+
* trusted on its own account: it holds the board, the rules, the memory and
|
|
348
|
+
* this feature's log, so maintaining it is the standing work this preset exists
|
|
349
|
+
* for — and it is not a registered project, so it can never arrive through
|
|
350
|
+
* `projects.json`. Registered projects and configured extras follow. Keeping the
|
|
351
|
+
* order fixed means a verdict can always name the root that matched.
|
|
352
|
+
*
|
|
353
|
+
* @param {{roots?: string[], workbench?: string, projects?: string[], extra?: string[]}} parts - directories, canonical or not.
|
|
354
|
+
* @returns {string[]} canonical roots, bundle first, duplicates collapsed.
|
|
355
|
+
*/
|
|
356
|
+
export function trustedRoots(parts) {
|
|
357
|
+
const ordered = [
|
|
358
|
+
...(Array.isArray(parts?.roots) ? parts.roots : []),
|
|
359
|
+
...(typeof parts?.workbench === 'string' ? [parts.workbench] : []),
|
|
360
|
+
...(Array.isArray(parts?.projects) ? parts.projects : []),
|
|
361
|
+
...(Array.isArray(parts?.extra) ? parts.extra : []),
|
|
362
|
+
]
|
|
363
|
+
const roots = []
|
|
364
|
+
for (const value of ordered) {
|
|
365
|
+
if (typeof value !== 'string' || value.trim() === '') continue
|
|
366
|
+
const root = canonicalDir(value)
|
|
367
|
+
if (!roots.includes(root)) roots.push(root)
|
|
368
|
+
}
|
|
369
|
+
return roots
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Decide whether one pending call may be approved on the user's behalf.
|
|
373
|
+
* @param {{name: string, args: Record<string, unknown>, cwd?: string}} call - the pending call.
|
|
374
|
+
* @param {{roots: string[], allowCommands: boolean}} options - canonical project roots and the command switch.
|
|
375
|
+
* @returns {{allow: boolean, reason: string, targets: string[]}} the verdict plus its evidence.
|
|
376
|
+
*/
|
|
377
|
+
export function inspect(call, options) {
|
|
378
|
+
if (options.roots.length === 0) return { allow: false, reason: 'no project is registered', targets: [] }
|
|
379
|
+
if (COMMAND_TOOLS.includes(call.name) && !options.allowCommands) {
|
|
380
|
+
return { allow: false, reason: 'command tools are switched off', targets: [] }
|
|
381
|
+
}
|
|
382
|
+
const { targets, problem } = targetsOf(call)
|
|
383
|
+
if (problem !== undefined) return { allow: false, reason: problem, targets }
|
|
384
|
+
const outside = targets.filter((target) => !isInside(target, options.roots))
|
|
385
|
+
if (outside.length > 0) return { allow: false, reason: `outside every project: ${outside.join('; ')}`, targets }
|
|
386
|
+
return { allow: true, reason: 'every target is inside a registered project', targets }
|
|
387
|
+
}
|