dsh-session-webhook 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +412 -2
- package/cordis.patch.yml +8 -0
- package/docs/ACCEPTANCE.md +158 -0
- package/docs/DESIGN.md +136 -0
- package/docs/TOOL-CONTRACT.md +315 -0
- package/icon.svg +13 -0
- package/lib/bases.js +527 -0
- package/lib/client.js +1755 -0
- package/lib/control.js +321 -0
- package/lib/deliver.js +272 -0
- package/lib/errors.js +62 -0
- package/lib/http.js +258 -0
- package/lib/index.js +220 -0
- package/lib/store.js +381 -0
- package/lib/tools.js +433 -0
- package/lib/types.js +1 -0
- package/lib/urlfile.js +290 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
- package/scripts/cold-resume-health.mjs +268 -0
- package/scripts/verify-client.mjs +1199 -0
- package/scripts/verify-health-contract.mjs +173 -0
- package/scripts/verify-host.mjs +1322 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `cold-resume-health.mjs` 的**契约测试**。
|
|
4
|
+
*
|
|
5
|
+
* 为什么单独有这个文件:下游项目直接调用那个脚本并按退出码判 CI,
|
|
6
|
+
* 所以「退出码语义」是**对外契约**,改动必须被测试挡住。判据本身(怎么判定降级)也可能变,
|
|
7
|
+
* 但退出码的含义不能悄悄变。
|
|
8
|
+
*
|
|
9
|
+
* 契约定版 v1:
|
|
10
|
+
* 0 = 全部健康(**含曾降级但已恢复**)
|
|
11
|
+
* 1 = 存在当前仍降级的会话
|
|
12
|
+
* 2 = 用法错误 / 找不到日志
|
|
13
|
+
*
|
|
14
|
+
* node scripts/verify-health-contract.mjs
|
|
15
|
+
*/
|
|
16
|
+
import assert from 'node:assert/strict';
|
|
17
|
+
import { spawnSync } from 'node:child_process';
|
|
18
|
+
import { mkdtemp, rm, writeFile, mkdir } from 'node:fs/promises';
|
|
19
|
+
import { tmpdir } from 'node:os';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { zstdCompressSync } from 'node:zlib';
|
|
22
|
+
|
|
23
|
+
const SCRIPT = new URL('./cold-resume-health.mjs', import.meta.url).pathname;
|
|
24
|
+
|
|
25
|
+
let passed = 0;
|
|
26
|
+
const failures = [];
|
|
27
|
+
|
|
28
|
+
async function test(name, fn) {
|
|
29
|
+
try {
|
|
30
|
+
await fn();
|
|
31
|
+
passed += 1;
|
|
32
|
+
console.log(` ok ${name}`);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
failures.push({ name, error });
|
|
35
|
+
console.log(`FAIL ${name}\n ${error?.stack ?? error}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 跑脚本,返回 { status, stdout, stderr }。 */
|
|
40
|
+
function run(args, env = {}) {
|
|
41
|
+
const result = spawnSync(process.execPath, [SCRIPT, ...args], {
|
|
42
|
+
encoding: 'utf8',
|
|
43
|
+
env: { ...process.env, ...env },
|
|
44
|
+
});
|
|
45
|
+
return { status: result.status, stdout: result.stdout ?? '', stderr: result.stderr ?? '' };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 造一个会话日志:多帧 zstd,每行一条 JSON 事件。
|
|
50
|
+
* 只写本测试需要的事件类型(request/header 与 user/message)。
|
|
51
|
+
*/
|
|
52
|
+
async function writeSession(root, workspace, id, events) {
|
|
53
|
+
// 脚本读的是 $DSH_HOME/sessions/<workspace>/<id>/session.v4.jsonl.zstd
|
|
54
|
+
const dir = join(root, 'sessions', workspace, id);
|
|
55
|
+
await mkdir(dir, { recursive: true });
|
|
56
|
+
// 每帧一条事件,模拟 dsh 的落盘形态(多帧,必须按 magic 切)
|
|
57
|
+
const frames = events.map((event) => zstdCompressSync(Buffer.from(`${JSON.stringify(event)}\n`, 'utf8')));
|
|
58
|
+
await writeFile(join(dir, 'session.v4.jsonl.zstd'), Buffer.concat(frames));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 造一条 request/header 事件。
|
|
63
|
+
* @param count **工具总数**(含 bash)—— 断言里写的数字就是脚本会数出来的数字。
|
|
64
|
+
* @param withBash 是否包含 bash(它是判断 preset 是否挂上的关键工具)。
|
|
65
|
+
*/
|
|
66
|
+
function header(seq, reason, count, withBash, at) {
|
|
67
|
+
const others = Array.from({ length: withBash ? count - 1 : count }, (_, index) => ({ name: `tool_${index}` }));
|
|
68
|
+
const tools = withBash ? [...others, { name: 'bash' }] : others;
|
|
69
|
+
return { seq, type: 'request/header', at, data: { reason, header: { tools } } };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const HEALTHY = 'session-healthy';
|
|
73
|
+
const RECOVERED = 'session-recovered';
|
|
74
|
+
const DEGRADED = 'session-degraded';
|
|
75
|
+
|
|
76
|
+
async function main() {
|
|
77
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-health-contract-'));
|
|
78
|
+
try {
|
|
79
|
+
// 健康:工具数稳定,bash 全程在场
|
|
80
|
+
await writeSession(root, '--ws-a--', HEALTHY, [
|
|
81
|
+
header(1, 'initial', 77, true, 1000),
|
|
82
|
+
header(2, 'resume', 77, true, 2000),
|
|
83
|
+
]);
|
|
84
|
+
// 曾降级但已恢复:51 → 26(丢 bash)→ 81(bash 回来)
|
|
85
|
+
await writeSession(root, '--ws-a--', RECOVERED, [
|
|
86
|
+
header(1, 'initial', 51, true, 1000),
|
|
87
|
+
header(2, 'resume', 26, false, 2000),
|
|
88
|
+
header(3, 'resume', 81, true, 3000),
|
|
89
|
+
]);
|
|
90
|
+
// 仍降级:78 → 50(丢 bash),之后没再恢复
|
|
91
|
+
await writeSession(root, '--ws-b--', DEGRADED, [
|
|
92
|
+
header(1, 'initial', 78, true, 1000),
|
|
93
|
+
header(2, 'resume', 50, false, 2000),
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
const env = { DSH_HOME: root };
|
|
97
|
+
|
|
98
|
+
await test('无参数 → 退出码 2(用法错误)', () => {
|
|
99
|
+
const { status, stderr } = run([], env);
|
|
100
|
+
assert.equal(status, 2, stderr);
|
|
101
|
+
assert.match(stderr, /用法/);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await test('找不到会话 → 退出码 2', () => {
|
|
105
|
+
const { status } = run(['no-such-session'], env);
|
|
106
|
+
assert.equal(status, 2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await test('健康会话 → 退出码 0', () => {
|
|
110
|
+
const { status, stdout } = run([HEALTHY], env);
|
|
111
|
+
assert.equal(status, 0, stdout);
|
|
112
|
+
assert.match(stdout, /健康/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await test('曾降级但已恢复 → 退出码 0(不能因为历史降级让 CI 红)', () => {
|
|
116
|
+
const { status, stdout } = run([RECOVERED], env);
|
|
117
|
+
assert.equal(status, 0, stdout);
|
|
118
|
+
assert.match(stdout, /已恢复/);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
await test('仍降级 → 退出码 1', () => {
|
|
122
|
+
const { status, stdout } = run([DEGRADED], env);
|
|
123
|
+
assert.equal(status, 1, stdout);
|
|
124
|
+
assert.match(stdout, /仍处于降级态/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await test('--all 有仍降级会话 → 退出码 1,且两种模式语义一致', () => {
|
|
128
|
+
const { status, stdout } = run(['--all'], env);
|
|
129
|
+
assert.equal(status, 1, stdout);
|
|
130
|
+
assert.match(stdout, /仍降级 1 个/);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await test('--all 只剩已恢复会话 → 退出码 0', async () => {
|
|
134
|
+
// 删掉仍降级的那个会话
|
|
135
|
+
await rm(join(root, 'sessions', '--ws-b--'), { recursive: true, force: true });
|
|
136
|
+
const { status, stdout } = run(['--all'], env);
|
|
137
|
+
assert.equal(status, 0, stdout);
|
|
138
|
+
assert.match(stdout, /已恢复 1 个,仍降级 0 个/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await test('bash 从来不存在的会话不被误判为降级(IM 桥接会话形态)', async () => {
|
|
142
|
+
// IM 桥接类非 preset 会话天生没有 bash:不能只看「没有 bash」就判坏。
|
|
143
|
+
await writeSession(root, '--ws-c--', 'im-bridge-x', [
|
|
144
|
+
header(20, 'initial', 12, false, 1000),
|
|
145
|
+
header(21, 'resume', 12, false, 2000),
|
|
146
|
+
]);
|
|
147
|
+
const { status, stdout } = run(['im-bridge-x'], env);
|
|
148
|
+
assert.equal(status, 0, stdout);
|
|
149
|
+
assert.match(stdout, /健康/);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await test('多帧 zstd 全部被解析(只解首帧会漏掉后面的降级)', async () => {
|
|
153
|
+
// 自包含:前面的用例会删掉 --ws-b--,这里自己造一个「第 3 帧才降级」的会话。
|
|
154
|
+
// 若脚本只解第一帧,就会把工具数下降整段漏掉、判成健康。
|
|
155
|
+
const id = 'session-multiframe';
|
|
156
|
+
await writeSession(root, '--ws-d--', id, [
|
|
157
|
+
header(1, 'initial', 78, true, 1000),
|
|
158
|
+
header(2, 'resume', 78, true, 2000), // 仍健康
|
|
159
|
+
header(3, 'resume', 50, false, 3000), // 第 3 帧才降级
|
|
160
|
+
]);
|
|
161
|
+
const { status, stdout } = run([id], env);
|
|
162
|
+
assert.equal(status, 1, `应检出第 3 帧才出现的降级:${stdout}`);
|
|
163
|
+
assert.match(stdout, /工具数下降 78 → 50/);
|
|
164
|
+
});
|
|
165
|
+
} finally {
|
|
166
|
+
await rm(root, { recursive: true, force: true });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.log(`\n${failures.length === 0 ? '' : `${failures.length} failed, `}${passed} passed`);
|
|
170
|
+
if (failures.length > 0) process.exitCode = 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
await main();
|