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
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 仓库清单扫描(提案 #13):副机把 roots 下的 git 仓库报给 hub,hub 由此
|
|
3
|
+
* 建立 repo→node 索引,`remote_task` 的 `repo:` 参数据此路由(多节点时
|
|
4
|
+
* "把任务派给有这个仓库的那台机器"不再需要人肉记 nodeId)。
|
|
5
|
+
*
|
|
6
|
+
* 三条设计边界:
|
|
7
|
+
* - **零子进程**:只 `readdir` + 读 `.git/config` 文本,不 spawn git。
|
|
8
|
+
* 扫描在副机常驻进程里跑,起子进程既慢又会在某些环境下挂住。
|
|
9
|
+
* - **凭证不离开副机**:`.git/config` 的 remote url 可能内嵌
|
|
10
|
+
* `https://user:token@host/...`(CI 推送的常见写法)。上报前一律剥掉
|
|
11
|
+
* userinfo —— hub 只需要"这台机器有这个仓库",不需要能推它的凭证。
|
|
12
|
+
* - **有界**:深度 ≤3、数量 ≤50、跳过 node_modules 等已知噪音目录;
|
|
13
|
+
* 单目录读失败静默跳过(权限不足的目录在扫描里是常态而非异常)。
|
|
14
|
+
*
|
|
15
|
+
* `.git` 只认**目录**(常规 clone)。worktree/submodule 的 `.git` 是文件,
|
|
16
|
+
* 指针解析留 v2 —— 误报比漏报更伤路由(把任务派到没有仓库的机器上)。
|
|
17
|
+
*/
|
|
18
|
+
import { promises as fs } from 'node:fs'
|
|
19
|
+
import path from 'node:path'
|
|
20
|
+
|
|
21
|
+
/** 扫描深度:root 本身算第 0 层,最多下钻 3 层(root/a/b/repo)。 */
|
|
22
|
+
const MAX_DEPTH = 3
|
|
23
|
+
/** 上报上限:超出的仓库直接丢弃(索引是路由提示,不是全量清单)。 */
|
|
24
|
+
export const MAX_REPOS = 50
|
|
25
|
+
/** 下降时跳过的目录名(小写比较):已知噪音,进去纯属浪费时间。 */
|
|
26
|
+
const SKIP_DIRS = new Set(['node_modules', '$recycle.bin', 'system volume information'])
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 剥掉 remote url 里的 userinfo(凭证打码)。
|
|
30
|
+
*
|
|
31
|
+
* - `https://user:token@github.com/a/b.git` → `https://github.com/a/b.git`
|
|
32
|
+
* - scp 形态 `git@github.com:a/b.git`:不是合法 URL,走下面的 scp 分支 ——
|
|
33
|
+
* 把 `@` 前的用户名一并剥掉(真机验证 §问题4:有些自建 Git 服务把 token
|
|
34
|
+
* 放在用户名位置,而 `user@` 里的 user 可能是任何东西;url 只用于展示与
|
|
35
|
+
* 路由匹配,剥掉用户名无害且封死这条泄漏路径)
|
|
36
|
+
* - 非 URL 且非 scp 形态(本地路径等)原样返回
|
|
37
|
+
*
|
|
38
|
+
* @param {string} raw .git/config 里的原始 url
|
|
39
|
+
* @returns {string} 打码后的 url
|
|
40
|
+
*/
|
|
41
|
+
export function sanitizeRepoUrl(raw) {
|
|
42
|
+
const s = typeof raw === 'string' ? raw.trim() : ''
|
|
43
|
+
if (s === '') return ''
|
|
44
|
+
try {
|
|
45
|
+
const u = new URL(s)
|
|
46
|
+
if (u.username === '' && u.password === '') return s
|
|
47
|
+
u.username = ''
|
|
48
|
+
u.password = ''
|
|
49
|
+
return u.toString()
|
|
50
|
+
} catch {
|
|
51
|
+
// scp 形态(user@host:path):用户名位置不可信,整段剥掉
|
|
52
|
+
return s.replace(/^[^/@\s]+@/, '')
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 解析 `.git/config`,取出优先级最高的 remote url(origin 优先,其次第一个
|
|
58
|
+
* remote)。没有 remote(纯本地仓库)返回空字符串 —— 仓库仍上报,靠 name/path
|
|
59
|
+
* 一样能路由。
|
|
60
|
+
*
|
|
61
|
+
* @param {string} repoPath 仓库根目录(其下有 .git/config)
|
|
62
|
+
* @returns {Promise<string>} 打码后的 url;读不到/解析不出都是 ''
|
|
63
|
+
*/
|
|
64
|
+
export async function readGitRemote(repoPath) {
|
|
65
|
+
const urls = []
|
|
66
|
+
let current = ''
|
|
67
|
+
let text
|
|
68
|
+
try {
|
|
69
|
+
text = await fs.readFile(path.join(repoPath, '.git', 'config'), 'utf8')
|
|
70
|
+
} catch {
|
|
71
|
+
return ''
|
|
72
|
+
}
|
|
73
|
+
for (const line of text.split(/\r?\n/)) {
|
|
74
|
+
const section = line.match(/^\s*\[(?:remote\s+"([^"]+)"|.+?)\]\s*$/)
|
|
75
|
+
if (section) {
|
|
76
|
+
current = section[1] ?? '' // 任何其他节头(core/branch/...)都把 remote 上下文清掉
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
const url = line.match(/^\s*url\s*=\s*(.+?)\s*$/)
|
|
80
|
+
if (url && current !== '') urls.push({ remote: current, url: url[1] })
|
|
81
|
+
}
|
|
82
|
+
const pick = urls.find((u) => u.remote === 'origin') ?? urls[0]
|
|
83
|
+
return pick ? sanitizeRepoUrl(pick.url) : ''
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 扫描 roots 下的 git 仓库。
|
|
88
|
+
*
|
|
89
|
+
* @param {string[]} roots 工作区根(绝对路径);与 hello.roots 同源
|
|
90
|
+
* @param {object} [o]
|
|
91
|
+
* @param {number} [o.maxDepth] 下钻层数(root=0 层)
|
|
92
|
+
* @param {number} [o.maxRepos] 上报上限
|
|
93
|
+
* @returns {Promise<Array<{name: string, url: string, path: string}>>}
|
|
94
|
+
*/
|
|
95
|
+
export async function scanRepos(roots, { maxDepth = MAX_DEPTH, maxRepos = MAX_REPOS } = {}) {
|
|
96
|
+
const out = []
|
|
97
|
+
const rootArr = Array.isArray(roots) ? roots.filter((r) => typeof r === 'string' && r.trim() !== '') : []
|
|
98
|
+
|
|
99
|
+
const walk = async (dir, depth) => {
|
|
100
|
+
if (out.length >= maxRepos) return
|
|
101
|
+
let isRepo = false
|
|
102
|
+
try {
|
|
103
|
+
isRepo = (await fs.stat(path.join(dir, '.git'))).isDirectory()
|
|
104
|
+
} catch {
|
|
105
|
+
/* 不是仓库或不可读 —— 都按"继续下钻"处理 */
|
|
106
|
+
}
|
|
107
|
+
if (isRepo) {
|
|
108
|
+
const name = path.basename(dir) || dir
|
|
109
|
+
const url = await readGitRemote(dir)
|
|
110
|
+
out.push({ name, url, path: dir })
|
|
111
|
+
return // 仓库内不再下钻(嵌套 clone 不是路由目标)
|
|
112
|
+
}
|
|
113
|
+
if (depth >= maxDepth) return
|
|
114
|
+
let entries
|
|
115
|
+
try {
|
|
116
|
+
entries = await fs.readdir(dir, { withFileTypes: true })
|
|
117
|
+
} catch {
|
|
118
|
+
return // 权限/IO 错误:这一枝放弃
|
|
119
|
+
}
|
|
120
|
+
for (const e of entries) {
|
|
121
|
+
if (out.length >= maxRepos) return
|
|
122
|
+
if (!e.isDirectory()) continue
|
|
123
|
+
const lower = e.name.toLowerCase()
|
|
124
|
+
if (lower.startsWith('.') || SKIP_DIRS.has(lower)) continue
|
|
125
|
+
await walk(path.join(dir, e.name), depth + 1)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const root of rootArr) {
|
|
130
|
+
if (out.length >= maxRepos) return out
|
|
131
|
+
await walk(root, 0)
|
|
132
|
+
}
|
|
133
|
+
return out
|
|
134
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 副机资源采样(零外部依赖)。
|
|
3
|
+
*
|
|
4
|
+
* 定位:每 30s 采一次 CPU/内存/负载,经 `node/resource` 帧发给 hub ——
|
|
5
|
+
* 派发前能先看一眼节点负载(remote_nodes + 面板节点行)。尽力而为的遥测:
|
|
6
|
+
* 采不到的字段就给 null,绝不因采样失败影响任务链路。
|
|
7
|
+
*
|
|
8
|
+
* CPU 用两次采样的**增量**算利用率:os.cpus() 的 times 是开机以来的累计值,
|
|
9
|
+
* 直接读出来永远是"开机平均",没有参考价值。首次采样没有基线 → cpuPct=null,
|
|
10
|
+
* hub 侧显示为"待下次上报"。
|
|
11
|
+
*/
|
|
12
|
+
import os from 'node:os'
|
|
13
|
+
|
|
14
|
+
/** 默认上报间隔。与心跳(15s)错开半个量级:资源数据不需要秒级新鲜度。 */
|
|
15
|
+
export const RESOURCE_INTERVAL_MS = 30_000
|
|
16
|
+
|
|
17
|
+
/** CPU 时间片快照:全核 idle 与 total 的累计和。 */
|
|
18
|
+
export function snapshotCpu(osLike = os) {
|
|
19
|
+
let idle = 0
|
|
20
|
+
let total = 0
|
|
21
|
+
for (const c of osLike.cpus()) {
|
|
22
|
+
const t = c.times
|
|
23
|
+
const iowait = t.iowait ?? 0
|
|
24
|
+
idle += t.idle + iowait
|
|
25
|
+
total += t.user + t.nice + t.sys + t.irq + t.idle + iowait
|
|
26
|
+
}
|
|
27
|
+
return { idle, total }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 两次快照之间的 CPU 利用率(0-100)。无基线或增量非法(计数器重置)→ null。 */
|
|
31
|
+
export function cpuPctBetween(prev, cur) {
|
|
32
|
+
if (!prev || !cur) return null
|
|
33
|
+
const dTotal = cur.total - prev.total
|
|
34
|
+
const dIdle = cur.idle - prev.idle
|
|
35
|
+
if (!(dTotal > 0) || dIdle < 0) return null
|
|
36
|
+
return Math.min(100, Math.max(0, ((dTotal - dIdle) / dTotal) * 100))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 有状态采样器:内部保留上一次 CPU 快照,sample() 返回本次上报载荷。
|
|
41
|
+
* osLike 参数专为测试注入(假 os,时间可控)。
|
|
42
|
+
*/
|
|
43
|
+
export function createResourceSampler({ osLike = os, round = (v) => Math.round(v * 10) / 10 } = {}) {
|
|
44
|
+
let prev = null
|
|
45
|
+
return {
|
|
46
|
+
sample() {
|
|
47
|
+
const cur = snapshotCpu(osLike)
|
|
48
|
+
const pct = cpuPctBetween(prev, cur)
|
|
49
|
+
prev = cur
|
|
50
|
+
const total = typeof osLike.totalmem === 'function' ? osLike.totalmem() : 0
|
|
51
|
+
const free = typeof osLike.freemem === 'function' ? osLike.freemem() : 0
|
|
52
|
+
const load = typeof osLike.loadavg === 'function' ? osLike.loadavg() : [0, 0, 0]
|
|
53
|
+
return {
|
|
54
|
+
// null = 暂无基线(首次采样)或采样无效 —— hub 侧显示"待下次上报"
|
|
55
|
+
cpuPct: pct === null ? null : round(pct),
|
|
56
|
+
// Windows 的 loadavg 恒为 [0,0,0],无参考价值但也不误导(恒 0 = 不忙)
|
|
57
|
+
load1: round(load[0] ?? 0),
|
|
58
|
+
memUsedMb: Math.max(0, Math.round((total - free) / (1024 * 1024))),
|
|
59
|
+
memTotalMb: Math.max(0, Math.round(total / (1024 * 1024))),
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
}
|