@zoraxylia/dsh-codex-adapter 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/.claude-plugin/marketplace.json +15 -0
- package/.claude-plugin/plugin.json +8 -0
- package/.mcp.json +8 -0
- package/LICENSE +21 -0
- package/README.de.md +480 -0
- package/README.es.md +480 -0
- package/README.fr.md +480 -0
- package/README.hi.md +480 -0
- package/README.id.md +480 -0
- package/README.ja.md +478 -0
- package/README.ko.md +480 -0
- package/README.md +483 -2
- package/README.pt.md +480 -0
- package/README.ru.md +480 -0
- package/README.th.md +480 -0
- package/README.tr.md +480 -0
- package/README.vi.md +480 -0
- package/README.zh-TW.md +478 -0
- package/README.zh.md +482 -0
- package/agents/ds-flash.md +17 -0
- package/agents/ds-pro.md +17 -0
- package/agy/agents/ds-flash.md +18 -0
- package/agy/agents/ds-pro.md +18 -0
- package/agy/skills/dsh-config/SKILL.md +6 -0
- package/agy/skills/dsh-playbook/SKILL.md +65 -0
- package/agy/skills/dsh-status/SKILL.md +6 -0
- package/codex/agents/ds-flash.toml +23 -0
- package/codex/agents/ds-pro.toml +23 -0
- package/codex/prompts/dsh-config.md +3 -0
- package/codex/prompts/dsh-playbook.md +60 -0
- package/codex/prompts/dsh-status.md +1 -0
- package/commands/config.md +9 -0
- package/commands/off.md +5 -0
- package/commands/on.md +5 -0
- package/commands/playbook.md +64 -0
- package/commands/status.md +5 -0
- package/cordis.patch.yml +4 -0
- package/docs/images/dsh-crew-host.png +0 -0
- package/docs/images/dsh-crew-jobs.png +0 -0
- package/docs/images/dsh-crew-logo.png +0 -0
- package/docs/images/dsh-crew-overview.png +0 -0
- package/grok/agents/ds-flash.md +19 -0
- package/grok/agents/ds-pro.md +19 -0
- package/grok/commands/dsh-config.md +9 -0
- package/grok/commands/dsh-playbook.md +66 -0
- package/grok/commands/dsh-status.md +5 -0
- package/lib/client.js +2684 -0
- package/lib/server.mjs +15417 -0
- package/package.json +137 -4
- package/scripts/build-client.mjs +23 -0
- package/scripts/build-mcp.mjs +43 -0
- package/scripts/fake-agy.sh +23 -0
- package/scripts/release-local.mjs +87 -0
- package/scripts/smoke-bundle.mjs +166 -0
- package/scripts/smoke-cwd.mjs +41 -0
- package/scripts/smoke-pack.mjs +285 -0
- package/scripts/smoke.mjs +79 -0
- package/scripts/verify-cli-workers.mjs +133 -0
- package/scripts/verify-wpc10-client.mjs +237 -0
- package/scripts/verify-wpc14-proc-endpoints.mjs +356 -0
- package/scripts/verify-wpc9-guardrails.mjs +432 -0
- package/src/cli-workers.mjs +396 -0
- package/src/client/index.tsx +1133 -0
- package/src/client/jobs-view.ts +374 -0
- package/src/cwd-lock.mjs +91 -0
- package/src/hub/index.mjs +643 -0
- package/src/hub-client.mjs +72 -0
- package/src/i18n.mjs +19 -0
- package/src/install/cli.mjs +34 -0
- package/src/install/install.mjs +577 -0
- package/src/jobs.mjs +289 -0
- package/src/multimodal.mjs +744 -0
- package/src/origin-guard.mjs +133 -0
- package/src/paths.mjs +43 -0
- package/src/plugin-env.mjs +65 -0
- package/src/proc-kill.mjs +208 -0
- package/src/process-info.mjs +60 -0
- package/src/provider-probe.mjs +35 -0
- package/src/server.mjs +438 -0
- package/src/status-shard.mjs +95 -0
- package/src/tier-binding.mjs +63 -0
- package/src/vision-route.mjs +139 -0
- package/src/worker-profiles.mjs +95 -0
- package/src/worker-question.mjs +22 -0
- package/statusline/statusline.sh +14 -0
- package/statusline/worker-segment.sh +35 -0
- package/worker.cordis.yml +77 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// WPC10 client-logic verification: bundles src/client/jobs-view.ts with
|
|
2
|
+
// esbuild and exercises the pure view helpers — orphan-ghost reconciliation
|
|
3
|
+
// (vanished + writer-gone), running-first ordering, held-workspace grouping,
|
|
4
|
+
// path/chain rendering and status-cell labeling. WPC13 additions on top:
|
|
5
|
+
// workspace grouping (multi-cwd, no-cwd bucket, running-first within a
|
|
6
|
+
// group), per-group finished counts, lock-badge derivation (kind:"cwd-lock"
|
|
7
|
+
// entries + running-job fallback) and the group-header path helpers
|
|
8
|
+
// (basename, ~-substitution + middle ellipsis, stable storage key).
|
|
9
|
+
// No browser needed.
|
|
10
|
+
//
|
|
11
|
+
// node scripts/verify-wpc10-client.mjs
|
|
12
|
+
|
|
13
|
+
import { build } from 'esbuild';
|
|
14
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
15
|
+
import { tmpdir } from 'node:os';
|
|
16
|
+
import { join, dirname } from 'node:path';
|
|
17
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
18
|
+
|
|
19
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
20
|
+
const tmp = mkdtempSync(join(tmpdir(), 'dsh-crew-wpc10-'));
|
|
21
|
+
let failures = 0;
|
|
22
|
+
function check(name, cond) {
|
|
23
|
+
if (cond) console.log(' ok - ' + name);
|
|
24
|
+
else { failures += 1; console.error(' FAIL - ' + name); }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
await build({
|
|
29
|
+
entryPoints: [join(ROOT, 'src', 'client', 'jobs-view.ts')],
|
|
30
|
+
bundle: true, platform: 'node', format: 'esm',
|
|
31
|
+
outfile: join(tmp, 'jobs-view.mjs'), logLevel: 'silent',
|
|
32
|
+
});
|
|
33
|
+
const mod = await import(pathToFileURL(join(tmp, 'jobs-view.mjs')).href);
|
|
34
|
+
const {
|
|
35
|
+
reconcileGhosts, displayJobs, heldWorkspaces, shortPath, chainText, statusInfoOf,
|
|
36
|
+
groupJobs, finishedCountOf, workspaceKey, basenameOf, abbrevPath, workspaceLocks, lockBadgeOf,
|
|
37
|
+
ghostPidOf, procBadgeOf, dueProcProbes, PROC_PROBE_INTERVAL_MS,
|
|
38
|
+
ORPHAN_MISSES, GHOST_CAP,
|
|
39
|
+
} = mod;
|
|
40
|
+
|
|
41
|
+
const zh = {
|
|
42
|
+
statusRunning: '运行中', statusStalled: '疑似卡住', statusDone: '完成',
|
|
43
|
+
statusFailed: '失败', statusCancelled: '已取消', statusOrphaned: '失联',
|
|
44
|
+
statusTitle: (s) => `状态:${s}`, orphanWriterGone: (o) => `writer gone ${o}`,
|
|
45
|
+
orphanVanished: (a) => `vanished ${a}`, stalledTip: (l) => `stalled ${l}`,
|
|
46
|
+
shardStale: (m) => `stale ${m}`,
|
|
47
|
+
};
|
|
48
|
+
const now = Date.now();
|
|
49
|
+
const job = (o = {}) => ({
|
|
50
|
+
id: 'job-1-x', status: 'running', startedAt: new Date(now - 60_000).toISOString(),
|
|
51
|
+
tier: 'flash', effort: 'max', task: 'do the thing', source: 'claude-code', cwd: '/w/a', ...o,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// 1. Vanished-job detection: two consecutive OK snapshots without the job → orphaned ghost.
|
|
55
|
+
let g = reconcileGhosts([job()], {}, now);
|
|
56
|
+
check('running job registers a ghost', g['job-1-x']?.misses === 0 && g['job-1-x']?.job?.status === 'running');
|
|
57
|
+
g = reconcileGhosts([], g, now + 3000);
|
|
58
|
+
check('first miss keeps it running (torn-read protection)',
|
|
59
|
+
g['job-1-x']?.misses === 1 && g['job-1-x']?.job?.status === 'running');
|
|
60
|
+
g = reconcileGhosts([], g, now + 6000);
|
|
61
|
+
check('second miss marks it orphaned/vanished',
|
|
62
|
+
g['job-1-x']?.job?.status === 'orphaned' && g['job-1-x']?.job?.orphan?.kind === 'vanished'
|
|
63
|
+
&& g['job-1-x']?.job?.orphan?.lastSeenAt === now);
|
|
64
|
+
|
|
65
|
+
// 2. A terminal state observed in a later snapshot drops the ghost (normal finish).
|
|
66
|
+
let g2 = reconcileGhosts([job()], {}, now);
|
|
67
|
+
g2 = reconcileGhosts([job({ status: 'done', endedAt: new Date(now).toISOString() })], g2, now + 3000);
|
|
68
|
+
check('terminal job drops its ghost', !g2['job-1-x']);
|
|
69
|
+
|
|
70
|
+
// 3. Reappearance resets the miss counter.
|
|
71
|
+
let g3 = reconcileGhosts([job()], {}, now);
|
|
72
|
+
g3 = reconcileGhosts([], g3, now + 3000);
|
|
73
|
+
g3 = reconcileGhosts([job()], g3, now + 6000);
|
|
74
|
+
check('reappearing job resets misses', g3['job-1-x']?.misses === 0);
|
|
75
|
+
|
|
76
|
+
// 4. Writer-gone: a live row whose writer process is dead is re-labeled immediately,
|
|
77
|
+
// and the kind survives the later vanish.
|
|
78
|
+
const dead = job({ originWriterAlive: false, origin: 'hub-99999' });
|
|
79
|
+
const rows = displayJobs([dead], {});
|
|
80
|
+
check('writer-gone live row re-labeled orphaned',
|
|
81
|
+
rows[0]?.status === 'orphaned' && rows[0]?.orphan?.kind === 'writer-gone' && rows[0]?.orphan?.origin === 'hub-99999');
|
|
82
|
+
let g4 = reconcileGhosts([dead], {}, now);
|
|
83
|
+
g4 = reconcileGhosts([], g4, now + 3000);
|
|
84
|
+
g4 = reconcileGhosts([], g4, now + 6000);
|
|
85
|
+
check('vanished writer-gone ghost keeps its kind', g4['job-1-x']?.job?.orphan?.kind === 'writer-gone');
|
|
86
|
+
const aliveForeign = job({ originWriterAlive: true, origin: 'mcp-111', id: 'job-2-y' });
|
|
87
|
+
check('alive writer stays running', displayJobs([aliveForeign], {})[0]?.status === 'running');
|
|
88
|
+
|
|
89
|
+
// 5. Ordering: running → orphaned → finished, newest startedAt first.
|
|
90
|
+
const rNew = job({ id: 'r1', startedAt: new Date(now - 1000).toISOString() });
|
|
91
|
+
const rOld = job({ id: 'r2', startedAt: new Date(now - 5000).toISOString() });
|
|
92
|
+
const orph = job({ id: 'o1', status: 'orphaned', orphan: { kind: 'vanished', lastSeenAt: now } });
|
|
93
|
+
const fin = job({ id: 'f1', status: 'done', startedAt: new Date(now - 2000).toISOString() });
|
|
94
|
+
const ordered = displayJobs([fin, rOld, orph, rNew], {});
|
|
95
|
+
check('running first, orphaned second, finished last (newest first)',
|
|
96
|
+
ordered.map((j) => j.id).join(',') === 'r1,r2,o1,f1');
|
|
97
|
+
|
|
98
|
+
// 6. Held workspaces: only live running rows hold a cwd; grouped and sorted by cwd.
|
|
99
|
+
const held = heldWorkspaces([job({ cwd: '/w/z' }), job({ cwd: '/w/a' }), orph, job({ cwd: '/w/z', id: 'job-2-y' })]);
|
|
100
|
+
check('heldWorkspaces groups and sorts by cwd',
|
|
101
|
+
held.length === 2 && held[0].cwd === '/w/a' && held[1].cwd === '/w/z' && held[1].jobs.length === 2);
|
|
102
|
+
check('orphaned rows never hold a workspace', held.every((h) => h.jobs.every((j) => j.status === 'running')));
|
|
103
|
+
|
|
104
|
+
// 7. Rendering helpers.
|
|
105
|
+
const long = '/a/very/long/path/that/exceeds/the/length/limit/repo';
|
|
106
|
+
const sp = shortPath(long);
|
|
107
|
+
check('shortPath trims the head and keeps the tail', sp.length === 34 && sp.startsWith('…') && sp.endsWith('repo'));
|
|
108
|
+
check('shortPath leaves short paths alone', shortPath('/w/a') === '/w/a');
|
|
109
|
+
check('chainText renders hops', chainText([{ source: 'claude-code', backend: 'hub', cwd: '/w/a' }]).includes('claude-code → hub @ /w/a'));
|
|
110
|
+
|
|
111
|
+
// 8. Status-cell labeling.
|
|
112
|
+
const sRun = statusInfoOf(job(), zh);
|
|
113
|
+
check('running → blue + running label', sRun.color === '#4a9eff' && sRun.label === '运行中');
|
|
114
|
+
const sStall = statusInfoOf(job({ liveness: 'stalled', activityLastEventAt: new Date(now - 130_000).toISOString() }), zh);
|
|
115
|
+
check('stalled CLI → amber + stalled label', sStall.color === '#d29922' && sStall.label === '疑似卡住');
|
|
116
|
+
const sStaleWarn = statusInfoOf(job({ originShardUpdatedAt: new Date(now - 11 * 60_000).toISOString() }), zh);
|
|
117
|
+
check('stale shard (≥10min) → amber warning while still running', sStaleWarn.color === '#d29922' && sStaleWarn.label === '运行中');
|
|
118
|
+
const sStaleHint = statusInfoOf(job({ originShardUpdatedAt: new Date(now - 6 * 60_000).toISOString() }), zh);
|
|
119
|
+
check('mildly stale shard (≥5min) → blue with hint', sStaleHint.color === '#4a9eff' && sStaleHint.title.startsWith('stale'));
|
|
120
|
+
const sOrph = statusInfoOf(orph, zh);
|
|
121
|
+
check('orphaned → orange + orphan label', sOrph.color === '#f0883e' && sOrph.label === '失联');
|
|
122
|
+
const sDone = statusInfoOf(job({ status: 'done' }), zh);
|
|
123
|
+
check('done → green', sDone.color === '#3fb950' && sDone.label === '完成');
|
|
124
|
+
|
|
125
|
+
// 9. Ghost registry is capped.
|
|
126
|
+
let gc = {};
|
|
127
|
+
const many = Array.from({ length: GHOST_CAP + 5 }, (_, i) => job({ id: `job-${i}-y` }));
|
|
128
|
+
gc = reconcileGhosts(many, gc, now);
|
|
129
|
+
check('ghost registry stays capped', Object.keys(gc).length <= GHOST_CAP);
|
|
130
|
+
// 10. WPC13 workspace grouping: multi-cwd groups, no-cwd bucket, ordering.
|
|
131
|
+
const gj = groupJobs([
|
|
132
|
+
job({ cwd: '/w/z' }), job({ cwd: '/w/a' }),
|
|
133
|
+
job({ cwd: '/w/z', id: 'job-2-y', startedAt: new Date(now - 2000).toISOString() }),
|
|
134
|
+
job({ cwd: undefined, id: 'job-n1' }),
|
|
135
|
+
job({ cwd: '', id: 'job-n2', status: 'done' }),
|
|
136
|
+
]);
|
|
137
|
+
check('groupJobs splits rows into per-cwd groups sorted by path',
|
|
138
|
+
gj.length === 3 && gj[0].cwd === '/w/a' && gj[0].jobs.length === 1
|
|
139
|
+
&& gj[1].cwd === '/w/z' && gj[1].jobs.length === 2);
|
|
140
|
+
check('running rows precede finished rows inside a group',
|
|
141
|
+
gj[1].jobs[0].status === 'running' && gj[1].jobs[1].status === 'running');
|
|
142
|
+
check('jobs without a cwd land in the trailing null-cwd group',
|
|
143
|
+
gj[2].cwd === null && gj[2].jobs.map((j) => j.id).join(',') === 'job-n1,job-n2');
|
|
144
|
+
check('finishedCountOf counts only settled rows',
|
|
145
|
+
finishedCountOf(gj[1].jobs) === 0 && finishedCountOf(gj[2].jobs) === 1);
|
|
146
|
+
|
|
147
|
+
// 11. WPC13 grouping skips non-job status rows (kind: "cwd-lock").
|
|
148
|
+
const mixedGroup = groupJobs([job({ cwd: '/w/x' }), { kind: 'cwd-lock', id: 'cwd-lock-1', cwd: '/w/l', holder: {} }]);
|
|
149
|
+
check('groupJobs skips kind:"cwd-lock" feed entries',
|
|
150
|
+
mixedGroup.length === 1 && mixedGroup[0].cwd === '/w/x' && mixedGroup[0].jobs.length === 1);
|
|
151
|
+
|
|
152
|
+
// 12. Orphaned ghosts stay visible in their workspace group.
|
|
153
|
+
const ghostGroup = groupJobs(displayJobs([], {
|
|
154
|
+
'job-1-x': { job: job({ status: 'orphaned', orphan: { kind: 'vanished', lastSeenAt: now }, cwd: '/w/g' }), lastSeenAt: now, misses: 2 },
|
|
155
|
+
}));
|
|
156
|
+
check('orphaned ghosts stay in their workspace group',
|
|
157
|
+
ghostGroup.length === 1 && ghostGroup[0].cwd === '/w/g' && ghostGroup[0].jobs[0].id === 'job-1-x');
|
|
158
|
+
|
|
159
|
+
// 13. WPC13 lock view: kind:"cwd-lock" entries and the running-job fallback.
|
|
160
|
+
const lockRow = { kind: 'cwd-lock', id: 'cwd-lock-1', cwd: '/w/l', holder: { jobId: 'hub-1', backend: 'hub-1', mode: 'hub', startedAt: new Date(now - 462_000).toISOString(), cwd: '/w/l' } };
|
|
161
|
+
const wl = workspaceLocks([lockRow, job({ cwd: '/w/l' }), job({ cwd: '/w/q', status: 'done' }), orph]);
|
|
162
|
+
check('workspaceLocks reads kind:"cwd-lock" entries with their holder',
|
|
163
|
+
wl.some((l) => l.cwd === '/w/l' && l.holder.jobId === 'hub-1' && l.holder.backend === 'hub-1'));
|
|
164
|
+
check('workspaceLocks falls back to running jobs and ignores settled rows',
|
|
165
|
+
wl.some((l) => l.cwd === '/w/l' && l.holder.jobId === 'job-1-x') && !wl.some((l) => l.cwd === '/w/q'));
|
|
166
|
+
|
|
167
|
+
// 14. WPC13 badge derivation: backend + age from startedAt, null when free.
|
|
168
|
+
const badgeCopy = { lockBadgeTip: (h) => 'tip:' + h.jobId + ':' + h.backend };
|
|
169
|
+
const badge = lockBadgeOf(wl, '/w/l', badgeCopy, now);
|
|
170
|
+
check('lock badge shows backend and age from startedAt',
|
|
171
|
+
badge !== null && badge.text === '🔒 hub-1 · 7m42s' && badge.title === 'tip:hub-1:hub-1');
|
|
172
|
+
check('no badge for a free workspace or the no-cwd group',
|
|
173
|
+
lockBadgeOf(wl, '/w/free', badgeCopy, now) === null && lockBadgeOf(wl, null, badgeCopy, now) === null);
|
|
174
|
+
|
|
175
|
+
// 15. WPC13 group-header path helpers.
|
|
176
|
+
check('basenameOf strips trailing slashes and handles null/root',
|
|
177
|
+
basenameOf('/w/a/repo') === 'repo' && basenameOf('/w/a/repo/') === 'repo'
|
|
178
|
+
&& basenameOf(null) === '' && basenameOf('/') === '/');
|
|
179
|
+
check('abbrevPath swaps $HOME for ~',
|
|
180
|
+
abbrevPath('/home/fini/workspace/dsh-plugins/dsh-crew', '/home/fini') === '~/workspace/dsh-plugins/dsh-crew');
|
|
181
|
+
const longAp = abbrevPath('/a/very/long/path/that/keeps/going/way/past/the/limit/repo');
|
|
182
|
+
check('abbrevPath middle-ellipsizes while keeping head and tail',
|
|
183
|
+
longAp.startsWith('/a/very') && longAp.endsWith('repo') && longAp.includes('…') && longAp.length <= 44);
|
|
184
|
+
check('abbrevPath leaves short paths alone', abbrevPath('/w/a') === '/w/a');
|
|
185
|
+
check('workspaceKey is stable, distinct and storage-safe',
|
|
186
|
+
workspaceKey('/w/a') === workspaceKey('/w/a') && workspaceKey('/w/a') !== workspaceKey('/w/b')
|
|
187
|
+
&& !workspaceKey('/w/a/b').includes('/') && workspaceKey(null) === workspaceKey('')
|
|
188
|
+
&& workspaceKey(null) !== workspaceKey('/w/a'));
|
|
189
|
+
|
|
190
|
+
// 16. WPC14 ghost pid extraction: only valid positive-integer pairs count.
|
|
191
|
+
check('ghostPidOf reads a pid and defaults pgid to null',
|
|
192
|
+
ghostPidOf(job({ pid: 4242 }))?.pid === 4242 && ghostPidOf(job({ pid: 4242 }))?.pgid === null);
|
|
193
|
+
check('ghostPidOf reads a pid+pgid pair',
|
|
194
|
+
JSON.stringify(ghostPidOf(job({ pid: 4242, pgid: 4242 }))) === JSON.stringify({ pid: 4242, pgid: 4242 }));
|
|
195
|
+
check('ghostPidOf rejects missing/zero/negative/non-integer pid',
|
|
196
|
+
ghostPidOf(job()) === null && ghostPidOf(job({ pid: 0 })) === null
|
|
197
|
+
&& ghostPidOf(job({ pid: -5 })) === null && ghostPidOf(job({ pid: 4.5 })) === null
|
|
198
|
+
&& ghostPidOf(job({ pid: '42' })) === null);
|
|
199
|
+
check('ghostPidOf rejects an invalid pgid',
|
|
200
|
+
ghostPidOf(job({ pid: 4242, pgid: -1 })) === null && ghostPidOf(job({ pid: 4242, pgid: 'x' })) === null);
|
|
201
|
+
|
|
202
|
+
// 17. WPC14 badge derivation: only an affirmative probe on an orphaned
|
|
203
|
+
// pid-carrying tombstone shows the badge + kill button.
|
|
204
|
+
const ghostPidJob = job({ status: 'orphaned', pid: 4242, pgid: 4242, orphan: { kind: 'vanished', lastSeenAt: now } });
|
|
205
|
+
check('alive probe shows the badge with pid+pgid',
|
|
206
|
+
JSON.stringify(procBadgeOf(ghostPidJob, { alive: true })) === JSON.stringify({ pid: 4242, pgid: 4242 }));
|
|
207
|
+
check('failed probe (alive: null) hides the badge', procBadgeOf(ghostPidJob, { alive: null }) === null);
|
|
208
|
+
check('no probe yet hides the badge', procBadgeOf(ghostPidJob, null) === null);
|
|
209
|
+
check('dead-process probe hides the badge', procBadgeOf(ghostPidJob, { alive: false }) === null);
|
|
210
|
+
check('badge needs a pid on the tombstone', procBadgeOf(job({ status: 'orphaned' }), { alive: true }) === null);
|
|
211
|
+
check('badge only applies to orphaned rows', procBadgeOf(job({ pid: 4242, status: 'running' }), { alive: true }) === null);
|
|
212
|
+
|
|
213
|
+
// 18. WPC14 probe scheduling: pid-carrying orphans only, throttled per ghost id.
|
|
214
|
+
const ghostWithPid = { g1: { job: ghostPidJob, lastSeenAt: now, misses: 2 } };
|
|
215
|
+
const noPidGhost = { g2: { job: job({ status: 'orphaned' }), lastSeenAt: now, misses: 2 } };
|
|
216
|
+
const runningGhost = { g3: { job: job({ pid: 9 }), lastSeenAt: now, misses: 0 } };
|
|
217
|
+
const due = dueProcProbes({ ...ghostWithPid, ...noPidGhost, ...runningGhost }, {}, now);
|
|
218
|
+
check('dueProcProbes picks pid-carrying orphaned ghosts only',
|
|
219
|
+
due.length === 1 && due[0].id === 'g1' && due[0].pid === 4242 && due[0].pgid === 4242);
|
|
220
|
+
check('recently probed ghosts are throttled',
|
|
221
|
+
dueProcProbes(ghostWithPid, { g1: now - PROC_PROBE_INTERVAL_MS + 1000 }, now).length === 0);
|
|
222
|
+
check('an old probe attempt makes the ghost due again',
|
|
223
|
+
dueProcProbes(ghostWithPid, { g1: now - PROC_PROBE_INTERVAL_MS - 1000 }, now).length === 1);
|
|
224
|
+
|
|
225
|
+
// 19. WPC14 tombstones keep the recorded pid through the vanish sequence.
|
|
226
|
+
let gp = reconcileGhosts([job({ pid: 4242 })], {}, now);
|
|
227
|
+
gp = reconcileGhosts([], gp, now + 3000);
|
|
228
|
+
gp = reconcileGhosts([], gp, now + 6000);
|
|
229
|
+
check('orphaned tombstone keeps the recorded pid',
|
|
230
|
+
gp['job-1-x']?.job?.pid === 4242 && gp['job-1-x']?.job?.status === 'orphaned');
|
|
231
|
+
|
|
232
|
+
console.log(' (ORPHAN_MISSES = ' + ORPHAN_MISSES + ', GHOST_CAP = ' + GHOST_CAP + ')');
|
|
233
|
+
if (failures > 0) { console.error('\nWPC10 CLIENT CHECKS FAILED: ' + failures); process.exit(1); }
|
|
234
|
+
console.log('\nALL_WPC10_CLIENT_CHECKS_PASSED');
|
|
235
|
+
} finally {
|
|
236
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
237
|
+
}
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// WPC14 endpoint verification: ghost-process liveness probing and the
|
|
2
|
+
// verified kill path (src/proc-kill.mjs + hub routes /_dsh/dsh-crew/proc-alive
|
|
3
|
+
// and /_dsh/dsh-crew/proc-kill).
|
|
4
|
+
//
|
|
5
|
+
// node scripts/verify-wpc14-proc-endpoints.mjs
|
|
6
|
+
//
|
|
7
|
+
// What runs for real:
|
|
8
|
+
// 1. Pure logic always: parsePid / isWorkerCommand / procCommand against the
|
|
9
|
+
// REAL verified command shapes (dsh runtime node command from the
|
|
10
|
+
// cmd-shim exec line, agy/grok buildArgs from cli-workers.mjs).
|
|
11
|
+
// 2. killWorkerProcess with an injected ps (deterministic, no /bin/ps):
|
|
12
|
+
// refusal on non-worker command lines, refusal on ps failure, SIGTERM
|
|
13
|
+
// kill of a fake worker process, pgid-mismatch refusal.
|
|
14
|
+
// 3. Real-ps integration (skipped when /bin/ps is unavailable, reported):
|
|
15
|
+
// a spawned `sleep 300` (non-worker) is refused and SURVIVES, a fake
|
|
16
|
+
// dsh runtime process (path+args matching the real shape) is killed,
|
|
17
|
+
// already-dead pids refuse as no-such-process.
|
|
18
|
+
// 4. Live hub endpoints at 127.0.0.1:3080, only when the running instance
|
|
19
|
+
// has the WPC14 routes (an instance booted before this change answers
|
|
20
|
+
// 404/empty and the script says so - restart DSH to activate).
|
|
21
|
+
//
|
|
22
|
+
// Every spawned process is cleaned up before exit.
|
|
23
|
+
|
|
24
|
+
import { spawn, execFileSync } from 'node:child_process';
|
|
25
|
+
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
|
26
|
+
import { tmpdir } from 'node:os';
|
|
27
|
+
import { join } from 'node:path';
|
|
28
|
+
import {
|
|
29
|
+
parsePid, procAlive, procCommand, isWorkerCommand, killWorkerProcess, ProcKillRefused, PROC_KILL_GRACE_MS,
|
|
30
|
+
} from '../src/proc-kill.mjs';
|
|
31
|
+
import { createShardWriter, readMergedStatus } from '../src/status-shard.mjs';
|
|
32
|
+
|
|
33
|
+
const HUB = 'http://127.0.0.1:3080/_dsh/dsh-crew';
|
|
34
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
35
|
+
const spawned = []; // every child we ever spawn, cleaned up in finally
|
|
36
|
+
let failures = 0;
|
|
37
|
+
function check(name, cond, detail = '') {
|
|
38
|
+
if (cond) console.log(' ok - ' + name);
|
|
39
|
+
else { failures += 1; console.error(' FAIL - ' + name + (detail ? ' | ' + detail : '')); }
|
|
40
|
+
}
|
|
41
|
+
function note(msg) { console.log(' -- ' + msg); }
|
|
42
|
+
|
|
43
|
+
function spawnTracked(cmd, args, opts = {}) {
|
|
44
|
+
const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'ignore'], ...opts });
|
|
45
|
+
spawned.push(child);
|
|
46
|
+
return child;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function waitDead(pid, maxMs = 3000) {
|
|
50
|
+
const deadline = Date.now() + maxMs;
|
|
51
|
+
while (procAlive(pid) && Date.now() < deadline) await sleep(100);
|
|
52
|
+
return !procAlive(pid);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------- pure logic
|
|
56
|
+
console.log('== 1. pure logic (parsePid / isWorkerCommand / procCommand) ==');
|
|
57
|
+
check('parsePid accepts positive integers', parsePid(1) === 1 && parsePid(4242) === 4242);
|
|
58
|
+
check('parsePid rejects garbage',
|
|
59
|
+
parsePid(0) === null && parsePid(-5) === null && parsePid(4.5) === null
|
|
60
|
+
&& parsePid('abc') === null && parsePid(null) === null && parsePid(undefined) === null
|
|
61
|
+
&& parsePid(99999999999) === null && parsePid('') === null);
|
|
62
|
+
|
|
63
|
+
const REAL_SHAPES = {
|
|
64
|
+
dsh: 'node /Users/fini/workspace/dsh-plugins/dsh-crew/node_modules/@deepseek-ai/dsh-sdk-jsonrpc-demo/lib/bin.js /Users/fini/workspace/dsh-plugins/dsh-crew/worker.cordis.yml',
|
|
65
|
+
agy: '/Users/fini/.local/bin/agy --print do the thing --output-format stream-json --dangerously-skip-permissions --mode accept-edits --print-timeout 1800s',
|
|
66
|
+
grok: '/Users/fini/.local/bin/grok -p do the thing --output-format streaming-messages-json --include-partial-messages --permission-mode bypassPermissions',
|
|
67
|
+
shim: '/bin/sh /Users/fini/workspace/dsh-plugins/dsh-crew/node_modules/.bin/dsh-jsonrpc-agent /Users/fini/workspace/dsh-plugins/dsh-crew/worker.cordis.yml',
|
|
68
|
+
};
|
|
69
|
+
check('isWorkerCommand accepts the real dsh runtime shape (shim exec line)', isWorkerCommand(REAL_SHAPES.dsh));
|
|
70
|
+
check('isWorkerCommand accepts the real agy shape', isWorkerCommand(REAL_SHAPES.agy));
|
|
71
|
+
check('isWorkerCommand accepts the real grok shape', isWorkerCommand(REAL_SHAPES.grok));
|
|
72
|
+
check('isWorkerCommand accepts the transient pnpm-shim shape', isWorkerCommand(REAL_SHAPES.shim));
|
|
73
|
+
check('isWorkerCommand rejects sleep/ls/garbage',
|
|
74
|
+
!isWorkerCommand('sleep 300') && !isWorkerCommand('/bin/ls -la') && !isWorkerCommand('') && !isWorkerCommand(null));
|
|
75
|
+
check('isWorkerCommand rejects unrelated node scripts (pid-recycling case)',
|
|
76
|
+
!isWorkerCommand('node /usr/local/bin/npm install react')
|
|
77
|
+
&& !isWorkerCommand('node /x/dsh-sdk-jsonrpc-demo/lib/bin.js something-else.yml'));
|
|
78
|
+
check('isWorkerCommand rejects bash wrappers and marker-less binaries',
|
|
79
|
+
!isWorkerCommand("bash -c 'agy --print hi --output-format stream-json'")
|
|
80
|
+
&& !isWorkerCommand('/Users/fini/.local/bin/agy --version'));
|
|
81
|
+
|
|
82
|
+
check('procCommand returns no-such-process for a fake ps exiting 1',
|
|
83
|
+
(() => {
|
|
84
|
+
const err = new Error('exited'); err.status = 1;
|
|
85
|
+
const r = procCommand(1, () => { throw err; });
|
|
86
|
+
return r.ok === false && r.reason === 'no-such-process';
|
|
87
|
+
})());
|
|
88
|
+
check('procCommand returns ps-failed for a broken ps',
|
|
89
|
+
(() => {
|
|
90
|
+
const r = procCommand(1, () => { throw new Error('EPERM'); });
|
|
91
|
+
return r.ok === false && r.reason === 'ps-failed';
|
|
92
|
+
})());
|
|
93
|
+
check('procCommand reads an injected command line',
|
|
94
|
+
procCommand(1, () => REAL_SHAPES.agy).command === REAL_SHAPES.agy);
|
|
95
|
+
|
|
96
|
+
// ---------------------- shard mirror passthrough (pid reaches the feed)
|
|
97
|
+
console.log('== 1b. shard mirror carries pid/pgid into the merged feed ==');
|
|
98
|
+
{
|
|
99
|
+
const shardWriter = createShardWriter('wpc14-verify');
|
|
100
|
+
shardWriter.publish([{ id: 'job-wpc14-fake', status: 'running', pid: 424242, pgid: 424242, startedAt: new Date().toISOString() }]);
|
|
101
|
+
const merged = readMergedStatus({});
|
|
102
|
+
const mine = merged.find((j) => j.id === 'job-wpc14-fake');
|
|
103
|
+
check('shard mirror keeps pid+pgid (ghost tombstones snapshot this feed)',
|
|
104
|
+
mine?.pid === 424242 && mine?.pgid === 424242);
|
|
105
|
+
check('shard mirror marks the verify writer alive', mine?.originWriterAlive === true);
|
|
106
|
+
shardWriter.dispose();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ------------------------------------- deterministic kills with injected ps
|
|
110
|
+
console.log('== 2. killWorkerProcess with injected ps ==');
|
|
111
|
+
const s1 = spawnTracked('sleep', ['300']);
|
|
112
|
+
await sleep(300);
|
|
113
|
+
check('sleep spawns and lives', procAlive(s1.pid));
|
|
114
|
+
await (async () => {
|
|
115
|
+
try { await killWorkerProcess({ pid: s1.pid, ps: () => 'sleep 300' }); check('non-worker with injected ps refuses', false); }
|
|
116
|
+
catch (e) { check('non-worker with injected ps refuses', e instanceof ProcKillRefused && e.code === 'not-a-worker'); }
|
|
117
|
+
})();
|
|
118
|
+
check('refused sleep survives', procAlive(s1.pid));
|
|
119
|
+
await (async () => {
|
|
120
|
+
try { await killWorkerProcess({ pid: s1.pid, ps: () => { throw new Error('EPERM'); } }); check('unreadable command line refuses', false); }
|
|
121
|
+
catch (e) { check('unreadable command line refuses', e instanceof ProcKillRefused && e.code === 'ps-failed'); }
|
|
122
|
+
})();
|
|
123
|
+
await (async () => {
|
|
124
|
+
try { await killWorkerProcess({ pid: s1.pid, pgid: s1.pid + 1, ps: () => REAL_SHAPES.agy }); check('pgid mismatch refuses', false); }
|
|
125
|
+
catch (e) { check('pgid mismatch refuses', e instanceof ProcKillRefused && e.code === 'pgid-mismatch'); }
|
|
126
|
+
})();
|
|
127
|
+
check('sleep survives all refusals', procAlive(s1.pid));
|
|
128
|
+
// worker-shaped fake ps: SIGTERM actually lands on the sleep and kills it.
|
|
129
|
+
const r1 = await killWorkerProcess({ pid: s1.pid, ps: () => REAL_SHAPES.dsh });
|
|
130
|
+
check('worker-shaped ps kills via SIGTERM', r1.ok === true && r1.killed === true && r1.signal === 'SIGTERM');
|
|
131
|
+
check('the killed process is gone', await waitDead(s1.pid));
|
|
132
|
+
// With an injected ps the command check cannot see the death; the kill path
|
|
133
|
+
// then reports alreadyGone instead of refusing (real ps produces the
|
|
134
|
+
// no-such-process refusal - see section 3).
|
|
135
|
+
const rGone = await killWorkerProcess({ pid: s1.pid, ps: () => REAL_SHAPES.dsh });
|
|
136
|
+
check('injected ps on a dead pid reports alreadyGone',
|
|
137
|
+
rGone.ok === true && rGone.killed === false && rGone.alreadyGone === true && rGone.signal === 'none');
|
|
138
|
+
await (async () => {
|
|
139
|
+
try { await killWorkerProcess({ pid: 0 }); check('invalid pid refuses', false); }
|
|
140
|
+
catch (e) { check('invalid pid refuses', e instanceof ProcKillRefused && e.code === 'bad-pid'); }
|
|
141
|
+
})();
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------- real ps (when /bin/ps works)
|
|
144
|
+
console.log('== 3. real-ps integration ==');
|
|
145
|
+
let psAvailable = false;
|
|
146
|
+
try {
|
|
147
|
+
const self = procCommand(process.pid);
|
|
148
|
+
psAvailable = self.ok === true && /node/.test(self.command ?? '');
|
|
149
|
+
} catch { psAvailable = false; }
|
|
150
|
+
if (!psAvailable) {
|
|
151
|
+
note('SKIPPED: /bin/ps is not executable in this environment (sandbox denies it) - the real-command-line checks above ran with injected ps instead');
|
|
152
|
+
} else {
|
|
153
|
+
note('ps available: running real command-line verification');
|
|
154
|
+
const s2 = spawnTracked('sleep', ['300']);
|
|
155
|
+
await sleep(300);
|
|
156
|
+
check('real ps: procAlive sees the live sleep', procAlive(s2.pid) === true);
|
|
157
|
+
await (async () => {
|
|
158
|
+
try { await killWorkerProcess({ pid: s2.pid }); check('real ps: sleep 300 refuses', false); }
|
|
159
|
+
catch (e) { check('real ps: sleep 300 refuses', e instanceof ProcKillRefused && e.code === 'not-a-worker'); }
|
|
160
|
+
})();
|
|
161
|
+
check('real ps: refused sleep still alive', procAlive(s2.pid));
|
|
162
|
+
try { process.kill(s2.pid, 'SIGKILL'); } catch {}
|
|
163
|
+
check('cleanup: sleep gone', await waitDead(s2.pid));
|
|
164
|
+
check('real ps: procAlive sees the dead sleep', procAlive(s2.pid) === false);
|
|
165
|
+
await (async () => {
|
|
166
|
+
try { await killWorkerProcess({ pid: s2.pid }); check('real ps: dead pid refuses as no-such-process', false); }
|
|
167
|
+
catch (e) { check('real ps: dead pid refuses as no-such-process', e instanceof ProcKillRefused && e.code === 'no-such-process'); }
|
|
168
|
+
})();
|
|
169
|
+
|
|
170
|
+
// Fake dsh runtime: path + args match the real standalone shape, and it
|
|
171
|
+
// leads its own process group (detached) so the pgid group-kill is real too.
|
|
172
|
+
const dir = mkdtempSync(join(tmpdir(), 'wpc14-fake-dsh-'));
|
|
173
|
+
const binDir = join(dir, 'dsh-sdk-jsonrpc-demo', 'lib');
|
|
174
|
+
mkdirSync(binDir, { recursive: true });
|
|
175
|
+
writeFileSync(join(binDir, 'bin.js'), "setInterval(() => {}, 1000);\n");
|
|
176
|
+
const cordis = join(dir, 'worker.cordis.yml');
|
|
177
|
+
writeFileSync(cordis, 'fake\n');
|
|
178
|
+
const fake = spawnTracked('node', [join(binDir, 'bin.js'), cordis], { detached: true });
|
|
179
|
+
await sleep(500);
|
|
180
|
+
const info = procCommand(fake.pid);
|
|
181
|
+
check('real ps reads the fake runtime command', info.ok === true && isWorkerCommand(info.command));
|
|
182
|
+
const r2 = await killWorkerProcess({ pid: fake.pid, pgid: fake.pid });
|
|
183
|
+
check('real ps: group kill of the fake runtime succeeds', r2.ok === true && r2.killed === true && r2.group === true);
|
|
184
|
+
check('real ps: the fake runtime is dead', await waitDead(fake.pid));
|
|
185
|
+
rmSync(dir, { recursive: true, force: true });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ----------------------- real command lines via pgrep (no /bin/ps needed)
|
|
189
|
+
console.log('== 3b. real process command lines via pgrep ==');
|
|
190
|
+
let pgrepAvailable = false;
|
|
191
|
+
try {
|
|
192
|
+
const probe = execFileSync('pgrep', ['-fl', 'launchd'], { encoding: 'utf8', timeout: 3000 });
|
|
193
|
+
pgrepAvailable = probe.trim() !== '';
|
|
194
|
+
} catch { pgrepAvailable = false; }
|
|
195
|
+
if (!pgrepAvailable) {
|
|
196
|
+
note('SKIPPED: pgrep unavailable in this environment');
|
|
197
|
+
} else {
|
|
198
|
+
const s5 = spawnTracked('sleep', ['300']);
|
|
199
|
+
await sleep(400);
|
|
200
|
+
let cmd = null;
|
|
201
|
+
try {
|
|
202
|
+
const out5 = execFileSync('pgrep', ['-fl', '^sleep 300$'], { encoding: 'utf8', timeout: 3000 });
|
|
203
|
+
const line = out5.split('\n').find((l) => l.startsWith(String(s5.pid) + ' '));
|
|
204
|
+
cmd = line ? line.slice(line.indexOf(' ') + 1) : null;
|
|
205
|
+
} catch { cmd = null; }
|
|
206
|
+
check('pgrep: real command line of the live sleep process', cmd === 'sleep 300');
|
|
207
|
+
check('pgrep: the real sleep command rejects the worker check',
|
|
208
|
+
cmd !== null && isWorkerCommand(cmd) === false);
|
|
209
|
+
try { process.kill(s5.pid, 'SIGKILL'); } catch {}
|
|
210
|
+
await waitDead(s5.pid);
|
|
211
|
+
|
|
212
|
+
// Fake dsh runtime: same path+args shape the standalone spawn produces
|
|
213
|
+
// (verified from the cmd-shim exec line), read back via pgrep.
|
|
214
|
+
const dir = mkdtempSync(join(tmpdir(), 'wpc14-fake-dsh-'));
|
|
215
|
+
const binDir = join(dir, 'dsh-sdk-jsonrpc-demo', 'lib');
|
|
216
|
+
mkdirSync(binDir, { recursive: true });
|
|
217
|
+
writeFileSync(join(binDir, 'bin.js'), "setInterval(() => {}, 1000);\n");
|
|
218
|
+
const cordis = join(dir, 'worker.cordis.yml');
|
|
219
|
+
writeFileSync(cordis, 'fake\n');
|
|
220
|
+
const fake = spawnTracked('node', [join(binDir, 'bin.js'), cordis], { detached: true });
|
|
221
|
+
await sleep(500);
|
|
222
|
+
let fakeCmd = null;
|
|
223
|
+
try {
|
|
224
|
+
const outF = execFileSync('pgrep', ['-fl', 'wpc14-fake-dsh'], { encoding: 'utf8', timeout: 3000 });
|
|
225
|
+
const line = outF.split('\n').find((l) => l.startsWith(String(fake.pid) + ' '));
|
|
226
|
+
fakeCmd = line ? line.slice(line.indexOf(' ') + 1) : null;
|
|
227
|
+
} catch { fakeCmd = null; }
|
|
228
|
+
check('pgrep: real command line of the fake runtime', !!fakeCmd);
|
|
229
|
+
check('pgrep: the real fake-runtime command matches the worker check',
|
|
230
|
+
fakeCmd !== null && isWorkerCommand(fakeCmd) === true);
|
|
231
|
+
try { process.kill(-fake.pid, 'SIGKILL'); } catch {}
|
|
232
|
+
await waitDead(fake.pid);
|
|
233
|
+
rmSync(dir, { recursive: true, force: true });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ---------------------------- real route handlers via a mocked webServer
|
|
237
|
+
console.log('== 4. hub route handlers driven through apply(ctx) with a mock webServer ==');
|
|
238
|
+
{
|
|
239
|
+
const { apply } = await import('../src/hub/index.mjs');
|
|
240
|
+
const handlers = new Map();
|
|
241
|
+
const webServer = { register: (spec) => { handlers.set(spec.path, spec.handler); return () => {}; } };
|
|
242
|
+
const ctx = {
|
|
243
|
+
inject: (deps, cb) => { if (deps?.includes?.('webServer')) cb({ webServer }); },
|
|
244
|
+
settings: { get: () => undefined },
|
|
245
|
+
logger: { info: () => {}, warn: () => {} },
|
|
246
|
+
};
|
|
247
|
+
const dispose = await apply(ctx);
|
|
248
|
+
const BASE = '/_dsh/dsh-crew';
|
|
249
|
+
const route = (name) => handlers.get(BASE + name);
|
|
250
|
+
check('proc-alive + proc-kill routes registered on the mock webServer',
|
|
251
|
+
typeof route('/proc-alive') === 'function' && typeof route('/proc-kill') === 'function');
|
|
252
|
+
const mkReq = (method, url, body, remote = '127.0.0.1') => ({
|
|
253
|
+
method,
|
|
254
|
+
url,
|
|
255
|
+
socket: { remoteAddress: remote },
|
|
256
|
+
headers: { host: '127.0.0.1:3080' },
|
|
257
|
+
async *[Symbol.asyncIterator]() { if (body !== undefined) yield Buffer.from(body); },
|
|
258
|
+
});
|
|
259
|
+
const call = async (name, method, url, body, remote) => {
|
|
260
|
+
const res = { out: null };
|
|
261
|
+
res.writeHead = (status, headers) => { res.out = { status, headers, body: '' }; };
|
|
262
|
+
res.end = (data) => { res.out.body = String(data); };
|
|
263
|
+
await route(name)(mkReq(method, url, body, remote), res);
|
|
264
|
+
let json = null;
|
|
265
|
+
try { json = JSON.parse(res.out?.body ?? ''); } catch {}
|
|
266
|
+
return { status: res.out?.status, json };
|
|
267
|
+
};
|
|
268
|
+
let r = await call('/proc-alive', 'GET', BASE + '/proc-alive?pid=' + process.pid);
|
|
269
|
+
check('route proc-alive: own pid alive', r.status === 200 && r.json?.alive === true);
|
|
270
|
+
r = await call('/proc-alive', 'GET', BASE + '/proc-alive?pid=99999999');
|
|
271
|
+
check('route proc-alive: absurd pid dead', r.status === 200 && r.json?.alive === false);
|
|
272
|
+
r = await call('/proc-alive', 'GET', BASE + '/proc-alive?pid=abc');
|
|
273
|
+
check('route proc-alive: non-integer pid -> 400', r.status === 400 && r.json?.ok === false);
|
|
274
|
+
r = await call('/proc-alive', 'GET', BASE + '/proc-alive?pid=-1');
|
|
275
|
+
check('route proc-alive: negative pid -> 400', r.status === 400 && r.json?.ok === false);
|
|
276
|
+
r = await call('/proc-alive', 'POST', BASE + '/proc-alive?pid=1');
|
|
277
|
+
check('route proc-alive: wrong method -> 405', r.status === 405);
|
|
278
|
+
r = await call('/proc-alive', 'GET', BASE + '/proc-alive?pid=1', undefined, '10.0.0.9');
|
|
279
|
+
check('route proc-alive: non-loopback -> 403', r.status === 403 && r.json?.ok === false);
|
|
280
|
+
r = await call('/proc-kill', 'POST', BASE + '/proc-kill', JSON.stringify({ pid: 'x' }));
|
|
281
|
+
check('route proc-kill: invalid pid -> 400', r.status === 400 && r.json?.ok === false);
|
|
282
|
+
r = await call('/proc-kill', 'POST', BASE + '/proc-kill', JSON.stringify({ pid: 5, pgid: 'y' }));
|
|
283
|
+
check('route proc-kill: invalid pgid -> 400', r.status === 400 && r.json?.ok === false);
|
|
284
|
+
const s4 = spawnTracked('sleep', ['300']);
|
|
285
|
+
await sleep(300);
|
|
286
|
+
r = await call('/proc-kill', 'POST', BASE + '/proc-kill', JSON.stringify({ pid: s4.pid }));
|
|
287
|
+
// Under a ps-denying sandbox this is ps-failed; with real ps it is
|
|
288
|
+
// not-a-worker. Both are refusals with a readable error - the point here
|
|
289
|
+
// is the route never kills an unverified process.
|
|
290
|
+
check('route proc-kill: sleep refuses (403) with a readable error',
|
|
291
|
+
r.status === 403 && r.json?.ok === false
|
|
292
|
+
&& ['not-a-worker', 'ps-failed'].includes(r.json?.code)
|
|
293
|
+
&& typeof r.json?.error === 'string' && r.json.error.length > 10);
|
|
294
|
+
check('route proc-kill: refused sleep survives', procAlive(s4.pid));
|
|
295
|
+
try { process.kill(s4.pid, 'SIGKILL'); } catch {}
|
|
296
|
+
await waitDead(s4.pid);
|
|
297
|
+
await dispose().catch(() => {});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ------------------------------------------------------- live hub endpoints
|
|
301
|
+
console.log('== 5. live hub endpoints (' + HUB + ') ==');
|
|
302
|
+
let routesLive = false;
|
|
303
|
+
try {
|
|
304
|
+
const ping = await fetch(HUB + '/ping', { signal: AbortSignal.timeout(1500) });
|
|
305
|
+
const pingBody = await ping.json();
|
|
306
|
+
note('hub ping: ' + JSON.stringify(pingBody));
|
|
307
|
+
const probe = await fetch(HUB + '/proc-alive?pid=' + process.pid, { signal: AbortSignal.timeout(1500) });
|
|
308
|
+
const probeText = await probe.text();
|
|
309
|
+
try {
|
|
310
|
+
const probeBody = JSON.parse(probeText);
|
|
311
|
+
if (probe.ok && typeof probeBody?.alive === 'boolean') routesLive = true;
|
|
312
|
+
} catch { /* empty/HTML 404 from a pre-WPC14 instance */ }
|
|
313
|
+
if (!routesLive) {
|
|
314
|
+
note('the running hub does not have the WPC14 routes yet (HTTP ' + probe.status + ', body: ' + JSON.stringify(probeText.slice(0, 120)) + ')');
|
|
315
|
+
note('-> live endpoint tests SKIPPED: the instance was booted before this change; restart DSH to activate the routes (module tests above cover the logic)');
|
|
316
|
+
}
|
|
317
|
+
} catch (e) {
|
|
318
|
+
note('hub unreachable (' + String(e?.message ?? e) + ') -> live endpoint tests SKIPPED');
|
|
319
|
+
}
|
|
320
|
+
if (routesLive) {
|
|
321
|
+
const getJson = async (path, init = {}) => {
|
|
322
|
+
const res = await fetch(HUB + path, { signal: AbortSignal.timeout(15000), ...init });
|
|
323
|
+
let body = null;
|
|
324
|
+
try { body = await res.json(); } catch {}
|
|
325
|
+
return { res, body };
|
|
326
|
+
};
|
|
327
|
+
let a = await getJson('/proc-alive?pid=' + process.pid);
|
|
328
|
+
check('live proc-alive: own pid is alive', a.res.ok && a.body?.alive === true);
|
|
329
|
+
a = await getJson('/proc-alive?pid=99999999');
|
|
330
|
+
check('live proc-alive: absurd pid is dead', a.res.ok && a.body?.alive === false);
|
|
331
|
+
a = await getJson('/proc-alive?pid=abc');
|
|
332
|
+
check('live proc-alive: non-integer pid -> 400', a.res.status === 400 && a.body?.ok === false);
|
|
333
|
+
a = await getJson('/proc-alive?pid=-1');
|
|
334
|
+
check('live proc-alive: negative pid -> 400', a.res.status === 400 && a.body?.ok === false);
|
|
335
|
+
a = await getJson('/proc-kill', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ pid: 'x' }) });
|
|
336
|
+
check('live proc-kill: invalid pid -> 400', a.res.status === 400 && a.body?.ok === false);
|
|
337
|
+
const s3 = spawnTracked('sleep', ['300']);
|
|
338
|
+
await sleep(300);
|
|
339
|
+
a = await getJson('/proc-kill', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ pid: s3.pid }) });
|
|
340
|
+
check('live proc-kill: sleep 300 refuses (403)', a.res.status === 403 && a.body?.ok === false && a.body?.code === 'not-a-worker');
|
|
341
|
+
check('live proc-kill: refused sleep survives', procAlive(s3.pid));
|
|
342
|
+
try { process.kill(s3.pid, 'SIGKILL'); } catch {}
|
|
343
|
+
await waitDead(s3.pid);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// ------------------------------------------------------------------ cleanup
|
|
347
|
+
for (const child of spawned) {
|
|
348
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
349
|
+
try { child.kill('SIGKILL'); } catch {}
|
|
350
|
+
if (child.pid) { try { process.kill(-child.pid, 'SIGKILL'); } catch {} }
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
await sleep(300);
|
|
354
|
+
|
|
355
|
+
if (failures > 0) { console.error('\nWPC14 ENDPOINT CHECKS FAILED: ' + failures); process.exit(1); }
|
|
356
|
+
console.log('\nALL_WPC14_ENDPOINT_CHECKS_PASSED (grace = ' + PROC_KILL_GRACE_MS + 'ms, real-ps = ' + psAvailable + ', real-pgrep = ' + pgrepAvailable + ', mock-routes = true, live-routes = ' + routesLive + ')');
|