dsh-remote-agent 0.0.0-stage → 0.4.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 +700 -0
- package/CONTRIBUTING.md +358 -0
- package/KNOWN-ISSUES.md +601 -0
- package/LICENSE +21 -0
- package/README.en.md +353 -0
- package/README.md +317 -2
- package/SECURITY.md +348 -0
- package/bin/dsh-remote-relay.js +257 -0
- package/bin/dsh-remote-setup.js +440 -0
- package/bin//345/217/214/345/207/273/350/277/220/350/241/214-/351/205/215/347/275/256/345/220/221/345/257/274.cmd +28 -0
- package/cordis.patch.yml +87 -0
- package/deploy/relay-vps/Caddyfile +24 -0
- package/deploy/relay-vps/README.md +132 -0
- package/deploy/relay-vps/dsh-remote-relay.service +47 -0
- package/deploy/relay-vps/nginx.conf +49 -0
- package/deploy/relay-vps/relay.env.example +20 -0
- package/docs/dispatch-automation-eval.md +140 -0
- package/docs/screenshots/panel-main.png +0 -0
- package/docs/screenshots/panel-task-detail.png +0 -0
- package/docs/verification-agent-e2e.md +425 -0
- package/docs/verification-repos-routing-real.md +521 -0
- package/docs/verification-resume-real.md +254 -0
- package/docs/worker-setup-pitfalls.md +266 -0
- package/lib/artifacts.js +451 -0
- package/lib/client.js +1752 -0
- package/lib/e2e.js +304 -0
- package/lib/glob.js +225 -0
- package/lib/hub/audit-log.js +161 -0
- package/lib/hub/hub-util.js +114 -0
- package/lib/hub/index.js +500 -0
- package/lib/hub/node-registry.js +943 -0
- package/lib/hub/pull-manager.js +781 -0
- package/lib/hub/registry-handshake.js +403 -0
- package/lib/hub/registry-ingest.js +346 -0
- package/lib/hub/relay-link.js +228 -0
- package/lib/hub/subagent-provider.js +424 -0
- package/lib/hub/templates.js +693 -0
- package/lib/hub/tools.js +1059 -0
- package/lib/hub/ui-routes.js +856 -0
- package/lib/hub/views.js +351 -0
- package/lib/identity.js +271 -0
- package/lib/index.js +272 -0
- package/lib/node/artifact-service.js +231 -0
- package/lib/node/diff.js +124 -0
- package/lib/node/exec.js +536 -0
- package/lib/node/hub-pin.js +71 -0
- package/lib/node/index.js +1022 -0
- package/lib/node/repos.js +134 -0
- package/lib/node/resource.js +63 -0
- package/lib/node/runner.js +792 -0
- package/lib/node/task-engine.js +712 -0
- package/lib/node/task-store.js +273 -0
- package/lib/node/transport.js +763 -0
- package/lib/pairing.js +186 -0
- package/lib/protocol.js +414 -0
- package/lib/relay-config.js +317 -0
- package/lib/relay.js +732 -0
- package/lib/setup-routes.js +313 -0
- package/lib/setup.js +522 -0
- package/lib/ws.js +466 -0
- package/package.json +103 -4
- package/profiles/README.md +573 -0
- package/profiles/hub.cordis.yml +84 -0
- package/profiles/worker.cordis.yml +60 -0
- package/scripts/check-doc-drift.mjs +785 -0
- package/scripts/check-packaging.mjs +130 -0
- package/scripts/diag-inject.mjs +243 -0
- package/scripts/e2e-live-check.mjs +181 -0
- package/scripts/relay-init.ps1 +286 -0
- package/scripts/relay-init.sh +199 -0
package/lib/artifacts.js
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 产物角色判定 + 清单构建(零依赖,node:fs 遍历)。
|
|
3
|
+
*
|
|
4
|
+
* 契约(用户 v0.1 锁定):
|
|
5
|
+
* role:final 编译产物 / 报告文档 / 源码变更 / 媒体文件 —— 允许 hub 显式 pull
|
|
6
|
+
* role:intermediate 日志、临时缓存、中间构建文件 —— 只在副机留存,仅上报元数据
|
|
7
|
+
* role:other 未命中任何规则 —— 上报元数据,默认不可 pull
|
|
8
|
+
*
|
|
9
|
+
* 判定顺序(先命中先赢,后面的规则不覆盖前面的):
|
|
10
|
+
* 1. artifactRules.intermediate 显式排除优先("logs/**" 不该被 "**" 捞成 final)
|
|
11
|
+
* 2. artifactRules.final
|
|
12
|
+
* 3. 内置默认表(按扩展名 / 目录名)
|
|
13
|
+
* 4. 兜底 other
|
|
14
|
+
*
|
|
15
|
+
* 为什么 intermediate 优先:用户明确说"日志、临时缓存、中间构建文件不属于 final"。
|
|
16
|
+
* 如果 final 优先,一条宽松的 `dist/**` 会把 `dist/*.log` 也捞进去。
|
|
17
|
+
*/
|
|
18
|
+
import { createHash } from 'node:crypto'
|
|
19
|
+
import fs from 'node:fs'
|
|
20
|
+
import path from 'node:path'
|
|
21
|
+
import { compileRules, extname, isAbsolutePath, isInside, matchGlob, normalizePath } from './glob.js'
|
|
22
|
+
import { ARTIFACT_KIND, ARTIFACT_ROLE } from './protocol.js'
|
|
23
|
+
|
|
24
|
+
/** 内置:这些是中间产物,无论在哪。 */
|
|
25
|
+
const INTERMEDIATE_EXT = new Set([
|
|
26
|
+
'.log', '.tmp', '.temp', '.cache', '.lock', '.pid', '.swp', '.swo', '.bak',
|
|
27
|
+
// 注意:这里**不含** '.obj' —— 它与 3D 模型格式(Wavefront OBJ)同名。
|
|
28
|
+
// MSVC 的 .obj 中间文件默认落在 obj/ 目录,由 INTERMEDIATE_DIR 的 'obj' 规则覆盖;
|
|
29
|
+
// 而 3D 渲染输出的 .obj 应能作为成品过河(用户契约把「渲染输出」列为 final)。
|
|
30
|
+
// 早期版本把 '.obj' 同时放进 INTERMEDIATE_EXT 与 MEDIA_EXT,因 intermediate 优先,
|
|
31
|
+
// MEDIA_EXT 那行永不生效 —— 一处死代码,也让 3D 模型里唯独 .obj 拿不回来。
|
|
32
|
+
'.map', '.tsbuildinfo', '.pdb', '.ilk', '.exp', '.o', '.a',
|
|
33
|
+
'.pyc', '.pyo', '.class', '.gcda', '.gcno',
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
const INTERMEDIATE_DIR = [
|
|
37
|
+
'node_modules', '.git', '.cache', '.tmp', 'temp', 'tmp',
|
|
38
|
+
'.next', '.nuxt', '.turbo', '.parcel-cache', '.vite', '.rollup.cache',
|
|
39
|
+
'__pycache__', '.pytest_cache', '.mypy_cache', '.ruff_cache',
|
|
40
|
+
'obj', 'bin/obj', 'coverage', '.nyc_output', 'target/debug',
|
|
41
|
+
'.dsh-remote-agent',
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
/** 内置:报告文档。 */
|
|
45
|
+
const REPORT_EXT = new Set(['.pdf', '.docx', '.doc', '.xlsx', '.xls', '.pptx', '.ppt', '.md', '.txt', '.csv', '.rtf', '.odt'])
|
|
46
|
+
|
|
47
|
+
/** 内置:媒体文件。 */
|
|
48
|
+
const MEDIA_EXT = new Set([
|
|
49
|
+
// 图片:.tif 与 .tiff 必须都在 —— 同一种格式的两种写法结果要一致
|
|
50
|
+
'.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg', '.ico',
|
|
51
|
+
'.tiff', '.tif', '.heic', '.avif',
|
|
52
|
+
// 渲染输出:HDR / EXR / DPX 是「渲染输出」类成品的常见格式
|
|
53
|
+
'.exr', '.hdr', '.dpx',
|
|
54
|
+
'.mp4', '.mov', '.mkv', '.webm', '.avi', '.mp3', '.wav', '.flac', '.ogg',
|
|
55
|
+
'.glb', '.gltf', '.fbx', '.obj', '.stl', '.3mf', '.psd', '.ai', '.sketch',
|
|
56
|
+
])
|
|
57
|
+
|
|
58
|
+
/** 内置:编译产物。 */
|
|
59
|
+
const BUILD_EXT = new Set([
|
|
60
|
+
'.exe', '.dll', '.so', '.dylib', '.node', '.wasm',
|
|
61
|
+
'.zip', '.7z', '.tar', '.gz', '.tgz', '.rar', '.xz', '.bz2',
|
|
62
|
+
'.msi', '.msix', '.appx', '.deb', '.rpm', '.dmg', '.apk', '.ipa',
|
|
63
|
+
'.jar', '.war', '.whl', '.nupkg', '.snap',
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
/** 内置:源码变更。 */
|
|
67
|
+
const DIFF_EXT = new Set(['.diff', '.patch'])
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 内置:这些目录整体视为「编译产物」= final。
|
|
71
|
+
*
|
|
72
|
+
* 为什么需要它:用户契约把 `dist/**` 明确列为编译产物,但早期实现只按扩展名判定,
|
|
73
|
+
* 于是 `dist/bundle.js`、`dist/style.css` 落到 other —— 前端 / Node 项目最常见的
|
|
74
|
+
* 构建产物反而拿不回来。`dist/**` 此前只在调用方显式传 artifactRules.final 时生效,
|
|
75
|
+
* 内置表本身不认目录。
|
|
76
|
+
*
|
|
77
|
+
* 顺序保证:本规则在内置 intermediate 之后判定,所以 `dist/app.log` 仍判 intermediate。
|
|
78
|
+
* 若某个仓库把 `build/` 当源码目录,用 artifactRules.intermediate 覆盖即可。
|
|
79
|
+
*/
|
|
80
|
+
const FINAL_DIR = ['dist', 'build', 'out', 'release', 'bundle']
|
|
81
|
+
|
|
82
|
+
const DEFAULT_INTERMEDIATE_DIRS = INTERMEDIATE_DIR
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 判定单个条目的角色与种类。
|
|
86
|
+
* @param {string} relPath 相对工作区的规范路径(`/` 分隔)
|
|
87
|
+
* @param {{intermediate?:string[], final?:string[], caseInsensitive?:boolean}} rules
|
|
88
|
+
*/
|
|
89
|
+
export function classify(relPath, rules = {}) {
|
|
90
|
+
const ci = rules.caseInsensitive === true
|
|
91
|
+
const p = normalizePath(relPath, { caseInsensitive: ci })
|
|
92
|
+
const ext = extname(p)
|
|
93
|
+
const base = p.slice(p.lastIndexOf('/') + 1)
|
|
94
|
+
|
|
95
|
+
const inDir = (dirs) => {
|
|
96
|
+
for (const d of dirs) {
|
|
97
|
+
const nd = normalizePath(d, { caseInsensitive: ci })
|
|
98
|
+
if (p === nd || p.startsWith(`${nd}/`)) return true
|
|
99
|
+
// 也匹配路径中间出现的目录名(如 src/node_modules/x)
|
|
100
|
+
if (p.includes(`/${nd}/`)) return true
|
|
101
|
+
}
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 1. 显式 intermediate 规则
|
|
106
|
+
if (rules.intermediate?.length) {
|
|
107
|
+
const test = compileRules(rules.intermediate, { caseInsensitive: ci })
|
|
108
|
+
if (test(p)) return { role: ARTIFACT_ROLE.INTERMEDIATE, kind: kindOf(ext, p), reason: 'rule:intermediate' }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 2. 内置 intermediate(扩展名 + 目录)
|
|
112
|
+
if (INTERMEDIATE_EXT.has(ext) && !isDiffLike(p)) {
|
|
113
|
+
// `.map` 属于中间;但 `*.diff`/`*.patch` 例外(那是源码变更成品)
|
|
114
|
+
return { role: ARTIFACT_ROLE.INTERMEDIATE, kind: kindOf(ext, p), reason: 'builtin:ext' }
|
|
115
|
+
}
|
|
116
|
+
if (inDir(DEFAULT_INTERMEDIATE_DIRS)) {
|
|
117
|
+
return { role: ARTIFACT_ROLE.INTERMEDIATE, kind: kindOf(ext, p), reason: 'builtin:dir' }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 3. 显式 final 规则
|
|
121
|
+
if (rules.final?.length) {
|
|
122
|
+
const test = compileRules(rules.final, { caseInsensitive: ci })
|
|
123
|
+
if (test(p)) return { role: ARTIFACT_ROLE.FINAL, kind: kindOf(ext, p), reason: 'rule:final' }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 4. 内置 final 表
|
|
127
|
+
// 先判目录:编译产出目录整体算成品(`dist/**` 是用户契约点名的编译产物)
|
|
128
|
+
if (inDir(FINAL_DIR)) return { role: ARTIFACT_ROLE.FINAL, kind: kindOf(ext, p), reason: 'builtin:final-dir' }
|
|
129
|
+
if (DIFF_EXT.has(ext)) return { role: ARTIFACT_ROLE.FINAL, kind: ARTIFACT_KIND.DIFF, reason: 'builtin:diff' }
|
|
130
|
+
if (BUILD_EXT.has(ext)) return { role: ARTIFACT_ROLE.FINAL, kind: ARTIFACT_KIND.FILE, reason: 'builtin:build' }
|
|
131
|
+
if (REPORT_EXT.has(ext)) return { role: ARTIFACT_ROLE.FINAL, kind: ARTIFACT_KIND.REPORT, reason: 'builtin:report' }
|
|
132
|
+
if (MEDIA_EXT.has(ext)) return { role: ARTIFACT_ROLE.FINAL, kind: ARTIFACT_KIND.MEDIA, reason: 'builtin:media' }
|
|
133
|
+
|
|
134
|
+
// 目录本身不判 final(内容由文件逐条判定)
|
|
135
|
+
if (base === '') return { role: ARTIFACT_ROLE.OTHER, kind: ARTIFACT_KIND.DIR, reason: 'dir' }
|
|
136
|
+
|
|
137
|
+
return { role: ARTIFACT_ROLE.OTHER, kind: kindOf(ext, p), reason: 'default' }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isDiffLike(p) {
|
|
141
|
+
const ext = extname(p)
|
|
142
|
+
return DIFF_EXT.has(ext)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function kindOf(ext, _p) {
|
|
146
|
+
if (DIFF_EXT.has(ext)) return ARTIFACT_KIND.DIFF
|
|
147
|
+
if (REPORT_EXT.has(ext)) return ARTIFACT_KIND.REPORT
|
|
148
|
+
if (MEDIA_EXT.has(ext)) return ARTIFACT_KIND.MEDIA
|
|
149
|
+
if (BUILD_EXT.has(ext)) return ARTIFACT_KIND.FILE
|
|
150
|
+
return ARTIFACT_KIND.FILE
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 单文件 sha256。大文件走流式,不整块读进内存。 */
|
|
154
|
+
export function hashFile(absPath) {
|
|
155
|
+
return new Promise((resolve, reject) => {
|
|
156
|
+
const h = createHash('sha256')
|
|
157
|
+
const s = fs.createReadStream(absPath)
|
|
158
|
+
s.on('data', (c) => h.update(c))
|
|
159
|
+
s.on('error', reject)
|
|
160
|
+
s.on('end', () => resolve(h.digest('hex')))
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 内容 sha256(用于摘要文本等)。 */
|
|
165
|
+
export function hashText(text) {
|
|
166
|
+
return createHash('sha256').update(text, 'utf8').digest('hex')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 遍历目录,产出条目列表。
|
|
171
|
+
* 硬约束:
|
|
172
|
+
* - 不跟随符号链接(避免逃出工作区)
|
|
173
|
+
* - 遵守 denyPaths
|
|
174
|
+
* - 有上限(maxEntries / maxScanMs),防止在巨型目录上把任务卡死
|
|
175
|
+
*/
|
|
176
|
+
export function walk(rootAbs, {
|
|
177
|
+
caseInsensitive = false,
|
|
178
|
+
denyPaths = [],
|
|
179
|
+
maxEntries = 20000,
|
|
180
|
+
maxDepth = 24,
|
|
181
|
+
signal,
|
|
182
|
+
ignoreDirs = DEFAULT_INTERMEDIATE_DIRS,
|
|
183
|
+
} = {}) {
|
|
184
|
+
const out = []
|
|
185
|
+
const rootNorm = normalizePath(rootAbs, { caseInsensitive })
|
|
186
|
+
const ignored = ignoreDirs.map((d) => normalizePath(d, { caseInsensitive }))
|
|
187
|
+
const stack = [{ abs: rootAbs, rel: '', depth: 0 }]
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* denyPaths 的归一化(高危修复)。
|
|
191
|
+
*
|
|
192
|
+
* 历史缺陷:walk 只拿**相对**工作区的 childNorm 去比 denyPaths,而所有真实调用方
|
|
193
|
+
* 传的都是**绝对**路径(node/index.js 传 stateDir、task-engine 传 cfg.denyPaths)。
|
|
194
|
+
* 两者永不相等 → denyPaths 被静默忽略 → 被禁目录里的文件照常进清单,
|
|
195
|
+
* 若命中 final 规则还能被 remote_pull 拉回主机。同一目录写成相对路径却生效,
|
|
196
|
+
* 这种"有时管用"最危险。
|
|
197
|
+
*
|
|
198
|
+
* 修法:把每条 deny 归一成两种形式并都参与比对 ——
|
|
199
|
+
* ① 相对形式(与 childNorm 同空间):本身就是相对路径的,或绝对路径落在工作区内的
|
|
200
|
+
* ② 绝对形式(与 childAbs 同空间):绝对路径一律保留
|
|
201
|
+
* 两种写法都必须生效,且行为一致。
|
|
202
|
+
*/
|
|
203
|
+
const denyRel = []
|
|
204
|
+
const denyAbs = []
|
|
205
|
+
for (const raw of denyPaths ?? []) {
|
|
206
|
+
if (typeof raw !== 'string' || raw === '') continue
|
|
207
|
+
const n = normalizePath(raw, { caseInsensitive })
|
|
208
|
+
|
|
209
|
+
if (!isAbsolutePath(raw)) {
|
|
210
|
+
// 相对写法:直接作为相对前缀使用
|
|
211
|
+
denyRel.push(n)
|
|
212
|
+
continue
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 绝对写法:既保留绝对形式,也尝试换算成工作区内的相对形式
|
|
216
|
+
denyAbs.push(n)
|
|
217
|
+
if (n === rootNorm) return out // 工作区自身被禁 → 整棵树都不扫
|
|
218
|
+
if (isInside(rootNorm, n, { caseInsensitive })) {
|
|
219
|
+
const rel = n.slice(rootNorm.endsWith('/') ? rootNorm.length : rootNorm.length + 1)
|
|
220
|
+
if (rel !== '') denyRel.push(rel)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const isDeniedEntry = (childAbs, childNorm) => {
|
|
225
|
+
for (const d of denyRel) {
|
|
226
|
+
if (childNorm === d || childNorm.startsWith(`${d}/`)) return true
|
|
227
|
+
}
|
|
228
|
+
if (denyAbs.length > 0) {
|
|
229
|
+
const absNorm = normalizePath(childAbs, { caseInsensitive })
|
|
230
|
+
for (const d of denyAbs) {
|
|
231
|
+
if (absNorm === d || absNorm.startsWith(`${d}/`)) return true
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return false
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
while (stack.length > 0) {
|
|
238
|
+
if (signal?.aborted) break
|
|
239
|
+
if (out.length >= maxEntries) break
|
|
240
|
+
const cur = stack.pop()
|
|
241
|
+
if (cur.depth > maxDepth) continue
|
|
242
|
+
|
|
243
|
+
let entries
|
|
244
|
+
try {
|
|
245
|
+
entries = fs.readdirSync(cur.abs, { withFileTypes: true })
|
|
246
|
+
} catch {
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
for (const e of entries) {
|
|
251
|
+
if (out.length >= maxEntries) break
|
|
252
|
+
const childAbs = path.join(cur.abs, e.name)
|
|
253
|
+
const childRel = cur.rel === '' ? e.name : `${cur.rel}/${e.name}`
|
|
254
|
+
const childNorm = normalizePath(childRel, { caseInsensitive })
|
|
255
|
+
|
|
256
|
+
if (isDeniedEntry(childAbs, childNorm)) continue
|
|
257
|
+
|
|
258
|
+
// 不跟随符号链接:只信 Dirent 的 isSymbolicLink
|
|
259
|
+
if (e.isSymbolicLink()) continue
|
|
260
|
+
|
|
261
|
+
if (e.isDirectory()) {
|
|
262
|
+
if (ignored.some((d) => childNorm === d || childNorm.startsWith(`${d}/`) || childNorm.endsWith(`/${d}`))) continue
|
|
263
|
+
stack.push({ abs: childAbs, rel: childRel, depth: cur.depth + 1 })
|
|
264
|
+
continue
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (!e.isFile()) continue
|
|
268
|
+
|
|
269
|
+
let st
|
|
270
|
+
try {
|
|
271
|
+
st = fs.statSync(childAbs)
|
|
272
|
+
} catch {
|
|
273
|
+
continue
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
out.push({
|
|
277
|
+
rel: childNorm,
|
|
278
|
+
abs: childAbs,
|
|
279
|
+
bytes: st.size,
|
|
280
|
+
mtimeMs: st.mtimeMs,
|
|
281
|
+
root: rootNorm,
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return out
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 构建 manifest。
|
|
291
|
+
* @param {object} o
|
|
292
|
+
* @param {string} o.workspace 绝对工作区
|
|
293
|
+
* @param {string[]} [o.outputs] 需要纳入清单的 glob(相对工作区)
|
|
294
|
+
* @param {object} [o.artifactRules] { final:[], intermediate:[] }
|
|
295
|
+
* @param {boolean} [o.caseInsensitive]
|
|
296
|
+
* @param {string[]} [o.denyPaths]
|
|
297
|
+
* @param {boolean} [o.includeOther] 是否把 other 也纳入清单(默认 true,只报元数据)
|
|
298
|
+
* @param {boolean} [o.hashFinal] 是否对 final 计算 sha256(默认 true)
|
|
299
|
+
*/
|
|
300
|
+
export async function buildManifest(o) {
|
|
301
|
+
const {
|
|
302
|
+
workspace,
|
|
303
|
+
outputs,
|
|
304
|
+
artifactRules = {},
|
|
305
|
+
caseInsensitive = false,
|
|
306
|
+
denyPaths = [],
|
|
307
|
+
includeOther = true,
|
|
308
|
+
hashFinal = true,
|
|
309
|
+
signal,
|
|
310
|
+
maxEntries = 20000,
|
|
311
|
+
} = o
|
|
312
|
+
|
|
313
|
+
const files = walk(workspace, { caseInsensitive, denyPaths, signal, maxEntries })
|
|
314
|
+
const outputsTest = outputs?.length
|
|
315
|
+
? compileRules(outputs, { caseInsensitive, base: '' })
|
|
316
|
+
: null
|
|
317
|
+
|
|
318
|
+
const items = []
|
|
319
|
+
let totalFinal = 0
|
|
320
|
+
let totalIntermediate = 0
|
|
321
|
+
let totalOther = 0
|
|
322
|
+
let bytesFinal = 0
|
|
323
|
+
|
|
324
|
+
for (const f of files) {
|
|
325
|
+
if (signal?.aborted) break
|
|
326
|
+
if (outputsTest && !outputsTest(f.rel)) continue
|
|
327
|
+
|
|
328
|
+
const cls = classify(f.rel, { ...artifactRules, caseInsensitive })
|
|
329
|
+
if (cls.role === ARTIFACT_ROLE.OTHER && !includeOther) continue
|
|
330
|
+
|
|
331
|
+
const item = {
|
|
332
|
+
path: f.rel,
|
|
333
|
+
role: cls.role,
|
|
334
|
+
kind: cls.kind,
|
|
335
|
+
bytes: f.bytes,
|
|
336
|
+
mtimeMs: Math.round(f.mtimeMs),
|
|
337
|
+
reason: cls.reason,
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (cls.role === ARTIFACT_ROLE.FINAL) {
|
|
341
|
+
totalFinal += 1
|
|
342
|
+
bytesFinal += f.bytes
|
|
343
|
+
if (hashFinal) {
|
|
344
|
+
try {
|
|
345
|
+
item.sha256 = await hashFile(f.abs)
|
|
346
|
+
} catch {
|
|
347
|
+
item.sha256 = undefined
|
|
348
|
+
item.hashError = true
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
} else if (cls.role === ARTIFACT_ROLE.INTERMEDIATE) {
|
|
352
|
+
totalIntermediate += 1
|
|
353
|
+
} else {
|
|
354
|
+
totalOther += 1
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
items.push(item)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
items.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))
|
|
361
|
+
|
|
362
|
+
return {
|
|
363
|
+
workspace: normalizePath(workspace, { caseInsensitive }),
|
|
364
|
+
generatedAt: Date.now(),
|
|
365
|
+
counts: {
|
|
366
|
+
total: items.length,
|
|
367
|
+
final: totalFinal,
|
|
368
|
+
intermediate: totalIntermediate,
|
|
369
|
+
other: totalOther,
|
|
370
|
+
},
|
|
371
|
+
bytesFinal,
|
|
372
|
+
items,
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* 校验一个 pull 请求是否被允许。
|
|
378
|
+
* v0.1 策略:只允许 role:final,且必须命中 denyPaths 之外、大小在上限内。
|
|
379
|
+
*
|
|
380
|
+
* denyPaths 同样要兼容「绝对」与「相对工作区」两种写法 —— 见 walk 里的高危修复说明。
|
|
381
|
+
* 这里没有工作区绝对路径可用来做归一,所以采用保守策略:
|
|
382
|
+
* 任何 deny 条目,只要它等于 item.path、是 item.path 的前缀、或被 item.path 包含,
|
|
383
|
+
* 一律拒绝。宁可多拒一个合法产物(用户会看到明确的 artifact-denied),
|
|
384
|
+
* 也不能漏放一个被禁文件过河。
|
|
385
|
+
*/
|
|
386
|
+
export function authorizePull(manifest, relPath, { caseInsensitive = false, maxBytes = 512 * 1024 * 1024, denyPaths = [] } = {}) {
|
|
387
|
+
const want = normalizePath(relPath, { caseInsensitive })
|
|
388
|
+
const item = manifest.items.find((i) => normalizePath(i.path, { caseInsensitive }) === want)
|
|
389
|
+
if (!item) return { ok: false, code: 'artifact-missing', message: `not in manifest: ${relPath}` }
|
|
390
|
+
if (item.role !== ARTIFACT_ROLE.FINAL) {
|
|
391
|
+
return {
|
|
392
|
+
ok: false,
|
|
393
|
+
code: 'artifact-denied',
|
|
394
|
+
message: `role=${item.role} is not pullable in v0.1 (only role:final crosses the wire)`,
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const itemNorm = normalizePath(item.path, { caseInsensitive })
|
|
399
|
+
const denied = (denyPaths ?? []).some((d) => {
|
|
400
|
+
if (typeof d !== 'string' || d === '') return false
|
|
401
|
+
const dn = normalizePath(d, { caseInsensitive })
|
|
402
|
+
// deny 是 item 的祖先(同一路径空间)
|
|
403
|
+
if (isInside(dn, itemNorm, { caseInsensitive })) return true
|
|
404
|
+
// item 是 deny 的祖先(禁了整个目录,item 在其下)
|
|
405
|
+
if (isInside(itemNorm, dn, { caseInsensitive })) return true
|
|
406
|
+
if (dn === itemNorm) return true
|
|
407
|
+
|
|
408
|
+
// 跨路径空间(deny 绝对、item 相对,或反之):无法直接比较,改用路径段判定。
|
|
409
|
+
//
|
|
410
|
+
// 规则:把 deny 拆成路径段,取它的**每一个后缀**,只要其中某个后缀恰好是
|
|
411
|
+
// item 段序列的**前缀**,就判定命中。
|
|
412
|
+
// deny …/work/secret item secret/token.md
|
|
413
|
+
// → deny 的后缀 ['secret'] 是 item 的前缀 → 命中 ✓
|
|
414
|
+
// deny …/work/secret/token.md item secret/token.md
|
|
415
|
+
// → deny 的后缀 ['secret','token.md'] 等于 item → 命中 ✓
|
|
416
|
+
// deny …/work/src item app.exe
|
|
417
|
+
// → 无后缀匹配('src' ≠ 'app.exe')→ 不命中 ✓
|
|
418
|
+
//
|
|
419
|
+
// 这是**保守**方向:deny 指向另一棵树但基名巧合相同时会误拒(例如
|
|
420
|
+
// deny=d:/unrelated/secret 与 item=secret/x)。这是刻意的取舍 ——
|
|
421
|
+
// 宁可多拒一个合法产物(用户看到明确的 artifact-denied),
|
|
422
|
+
// 也不能漏放一个被禁文件过河。
|
|
423
|
+
const dSegs = dn.split('/').filter(Boolean)
|
|
424
|
+
const iSegs = itemNorm.split('/').filter(Boolean)
|
|
425
|
+
if (dSegs.length === 0 || iSegs.length === 0) return false
|
|
426
|
+
for (let start = 0; start < dSegs.length; start += 1) {
|
|
427
|
+
const suffix = dSegs.slice(start)
|
|
428
|
+
if (suffix.length > iSegs.length) continue
|
|
429
|
+
let same = true
|
|
430
|
+
for (let i = 0; i < suffix.length; i += 1) {
|
|
431
|
+
if (suffix[i] !== iSegs[i]) { same = false; break }
|
|
432
|
+
}
|
|
433
|
+
if (same) return true
|
|
434
|
+
}
|
|
435
|
+
return false
|
|
436
|
+
})
|
|
437
|
+
if (denied) {
|
|
438
|
+
return { ok: false, code: 'artifact-denied', message: `path denied: ${relPath}` }
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (item.bytes > maxBytes) {
|
|
442
|
+
return { ok: false, code: 'artifact-too-large', message: `${item.bytes} bytes exceeds maxBytes=${maxBytes}` }
|
|
443
|
+
}
|
|
444
|
+
return { ok: true, item }
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** 判断一个路径是否命中 outputs 集合(用于 hub 侧二次过滤)。 */
|
|
448
|
+
export function matchesOutputs(relPath, outputs, opts = {}) {
|
|
449
|
+
if (!outputs || outputs.length === 0) return true
|
|
450
|
+
return matchGlob(relPath, outputs, opts)
|
|
451
|
+
}
|