skillhub/plugins/dsh/scripts/probe-host.mjs
XiaoSeS 6c276d38fc
Some checks are pending
Security / Dependency Review (push) Waiting to run
Security / CodeQL (java-kotlin) (push) Waiting to run
Security / CodeQL (javascript-typescript) (push) Waiting to run
Security / CodeQL (python) (push) Waiting to run
fix(dsh): preserve legacy compatibility and Windows lifecycle safety (#1033)
保持共享库存 v2 与旧版可读普通日志,从真实入口派生启停;隔离扩展日志并保护恢复时的合法后续写入。修复 Windows 库存/日志原子替换及目标锁的短时共享冲突,不删除原数据、不抢占活锁、不重放事务。

补充安全探针诊断与仅公共下载内容的 CI 缓存,Profile 和安装状态仍逐次隔离。准备 CLI 0.3.0 / DSH 0.6.0,npm 发布另行执行。

最终提交的 CLI 三平台、DSH 14 项矩阵及 12 份启停场景摘要均通过。Windows 新 RC 依赖准备仍接近限额,作为 CI 准备稳定性风险保留。

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-10-29 21:08:14 +08:00

242 lines
14 KiB
JavaScript

import { safeAvailabilityCheckpoint, safeToggleCheckpoint, safeFixtureLocation, safeOperationObservation, knownHostFailureCodes } from './smoke-native-host.mjs';
import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { mkdir, readFile, realpath, writeFile, access, open } from 'node:fs/promises';
import { openSync, closeSync } from 'node:fs';
import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { setTimeout as delay } from 'node:timers/promises';
import { parseArgs } from 'node:util';
const probes = {
'plugin-lifecycle': 'plugin-lifecycle-result.json',
'plugin-coexistence': 'plugin-lifecycle-result.json',
'history-restart': 'history-restart-result.json',
'runtime-baseline': 'runtime-baseline-result.json',
'runtime-unsupported': 'runtime-unsupported-result.json',
'installed-state': 'installed-state-result.json',
'skill-availability': 'skill-availability-result.json',
'skill-toggle': 'skill-toggle-result.json',
'skill-toggle-watch-off': 'skill-toggle-result.json',
'upgrade-candidate': 'upgrade-candidate-result.json',
'upgrade-drift': 'upgrade-drift-result.json',
'plan-context': 'plan-context-result.json',
'plan-source': 'plan-source-result.json',
};
let stage = 'setup';
let selectedProbe;
let observerStatusFile;
let dependencyStatusFile;
let availabilityCheckpoint;
let toggleCheckpoint;
let fixtureLocation;
let operationObservation;
let hostLogFile;
let failureCode = 'PROBE_FAILED';
function fail(code, message) { failureCode = code; throw new Error(message); }
async function main() {
const { values } = parseArgs({ options: { root: { type: 'string' }, probe: { type: 'string' }, help: { type: 'boolean' } } });
if (values.help) {
console.log(`node scripts/probe-host.mjs --root <prepared temporary DSH root> --probe <${Object.keys(probes).join('|')}>`);
process.exit(0);
}
if (!values.root || !Object.hasOwn(probes, values.probe ?? '')) throw new Error('Specify --root and a supported --probe; see --help.');
selectedProbe = values.probe;
stage = 'validate-root';
const root = await realpath(resolve(values.root));
const temporary = await realpath(tmpdir());
const rel = relative(temporary, root);
if (!rel || rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel)) throw new Error('Root must be an existing directory below the OS temporary directory.');
for (const directory of ['home', 'native-install-home', 'agents']) {
const actual = await realpath(join(root, directory));
if (!actual.startsWith(root + sep)) throw new Error('State directories must remain inside the temporary root.');
}
stage = 'prepare-fixture';
const pluginRoot = dirname(dirname(fileURLToPath(import.meta.url)));
const fixtureRoot = join(pluginRoot, 'test/integration');
const dshBin = join(root, 'node_modules/@deepseek-ai/dsh/lib/bin.js');
await access(dshBin);
await access(join(root, 'home/profiles/skillhub-m0/node_modules/@astron-team/skillhub-dsh/lib/client.js'));
const installedPatch = await readFile(join(root, 'home/profiles/skillhub-m0/node_modules/@astron-team/skillhub-dsh/cordis.patch.yml'), 'utf8');
const loaderIds = [...installedPatch.matchAll(/^\s*- id: (\S+)$/gm)].map(match => match[1]);
if (loaderIds.length !== 1 || !['astron-skillhub', 'skillhub'].includes(loaderIds[0])) {
throw new Error('Expected the current or retained development package Loader ID');
}
const installedLoaderId = loaderIds[0];
if (values.probe === 'plugin-coexistence' && installedLoaderId === 'skillhub') {
throw new Error('Retained Loader ID conflicts with the reference package; use the current package for coexistence');
}
stage = 'fixture-ports';
for (const port of [43219, 43220]) {
await new Promise((resolvePort, reject) => {
const server = createServer();
server.once('error', () => reject(new Error(`Fixture port ${port} is already in use; no process was stopped.`)));
server.listen(port, '127.0.0.1', () => server.close(error => error ? reject(error) : resolvePort()));
});
}
const id = randomUUID();
const run = join(root, 'probe-' + id);
await mkdir(run, { mode: 0o700 });
const resultName = `probe-${id}-result.json`;
const resultFile = join(root, resultName);
const observer = join(run, 'observer.mjs');
hostLogFile = join(run, 'host.log');
observerStatusFile = join(run, 'observer-status.json');
dependencyStatusFile = join(run, 'dependency-status.json');
const runtimeProbe = values.probe === 'runtime-baseline' || values.probe === 'runtime-unsupported';
const source = await readFile(join(fixtureRoot, runtimeProbe ? 'runtime-baseline-probe.mjs' : values.probe === 'plugin-coexistence' ? 'plugin-lifecycle-fixture.mjs' : `${values.probe === 'skill-toggle-watch-off' ? 'skill-toggle' : values.probe}-fixture.mjs`), 'utf8');
await writeFile(observer, source.replaceAll(probes[values.probe], resultName), { flag: 'wx', mode: 0o600 });
const state = join(run, 'update-state.txt');
const downloads = join(run, 'download-state.txt');
await writeFile(state, 'current');
await writeFile(downloads, 'normal');
const patch = join(run, 'observer.yml');
const config = { root, ...(values.probe === 'skill-toggle-watch-off' ? { watchOff: true } : {}), ...(['plugin-lifecycle', 'plugin-coexistence'].includes(values.probe) ? { bundle: true } : {}),
...(values.probe === 'plugin-coexistence' ? { referencePackage: '@cocofhu/skillhub' } : {}) };
if (runtimeProbe) {
const installedPackage = join(root, 'home/profiles/skillhub-m0/node_modules/@astron-team/skillhub-dsh');
const manifest = JSON.parse(await readFile(join(installedPackage, 'package.json'), 'utf8'));
Object.assign(config, { output: resultName, pluginEntry: join(installedPackage, 'lib/index.js'),
hostPackages: ['@deepseek-ai/dsh', '@deepseek-ai/dsh-app-boot', ...Object.keys(manifest.peerDependencies),
'@deepseek-ai/cordis-plugin-loader', '@deepseek-ai/cordis-plugin-include', '@deepseek-ai/cordis-plugin-timer'],
clientPackages: manifest.dsh.client.inject,
...(values.probe === 'runtime-unsupported' ? { expectBlocked: true } : { reloadClient: '@deepseek-ai/dsh-client-ui-layout' }) });
}
let diagnosticPatch = '';
if (runtimeProbe) {
const watcher = join(run, 'dependency-observer.mjs');
await writeFile(watcher, `import { writeFileSync } from 'node:fs';
export function apply(ctx) {
const report = () => {
const services = Object.fromEntries(['loader', 'pluginPackages', 'clientModules', 'skillhub'].map(name => [name, !!ctx.get(name)]));
try { writeFileSync(${JSON.stringify(dependencyStatusFile)}, JSON.stringify(services)); } catch {}
};
report(); const timer = setInterval(report, 500);
return () => clearInterval(timer);
}
`);
diagnosticPatch = `- insert:\n - id: skillhub-dependency-observer-${id}\n name: ${JSON.stringify(watcher)}\n`;
}
const referencePatch = values.probe === 'plugin-coexistence'
? '- id: skillhub\n config:\n apiBase: http://127.0.0.1:43219\n webBase: http://127.0.0.1:43219\n timeoutMs: 1000\n' : '';
await writeFile(patch, `${values.probe === 'skill-toggle-watch-off' ? '- id: skill-filesystem\n config:\n watch: false\n' : ''}${diagnosticPatch}${referencePatch}- id: m0-observer\n disabled: true\n- id: skillhub-local-runtime-probe\n disabled: true\n- id: ${installedLoaderId}\n config:\n registryUrl: http://127.0.0.1:43219\n- insert:\n - id: skillhub-probe-${id}\n name: ${JSON.stringify(observer)}\n config: ${JSON.stringify(config)}\n`);
// Pin every spelling used by Node to the parent-validated temporary directory.
// Windows process.env is case-insensitive in the main thread, but not in Workers.
const inherited = Object.fromEntries(Object.entries(process.env)
.filter(([key]) => !['TEMP', 'TMP', 'TMPDIR'].includes(key.toUpperCase())));
const env = { ...inherited, TEMP: temporary, TMP: temporary, TMPDIR: temporary,
HOME: join(root, 'native-install-home'), USERPROFILE: join(root, 'native-install-home'),
DSH_HOME: join(root, 'home'), DSH_AGENTS_HOME: join(root, 'agents'), DSH_TELEMETRY_DISABLED: '1', DSH_PROBE_SKIP_DISCOVERY: '1',
DSH_RUNTIME_PROBE_STATUS: observerStatusFile,
DSH_FIXTURE_NAMESPACE: `probe-${id}`, DSH_FIXTURE_CATALOG_SCROLL: '0', DSH_FIXTURE_UPDATE_STATE: state,
DSH_FIXTURE_DOWNLOAD_STATE: downloads, DSH_FIXTURE_REQUEST_LOG: join(run, 'requests.jsonl') };
const children = [];
const abort = new AbortController();
const stop = () => abort.abort();
process.once('SIGINT', stop);
process.once('SIGTERM', stop);
function launch(args, log, overrides = {}) {
const fd = openSync(join(run, log), 'wx', 0o600);
try {
const child = spawn(process.execPath, args, { cwd: root, env: { ...env, ...overrides }, stdio: ['ignore', fd, fd] });
const state = { child, ended: false };
state.done = new Promise(resolveExit => {
child.once('error', () => { state.ended = true; resolveExit(); });
child.once('exit', () => { state.ended = true; resolveExit(); });
});
children.push(state);
} finally { closeSync(fd); }
}
try {
stage = 'registry-ready';
launch([join(fixtureRoot, 'catalog-fixture.mjs')], 'registry.log', { DSH_FIXTURE_PORT: '43219' });
launch([join(fixtureRoot, 'catalog-fixture.mjs')], 'registry-second.log', { DSH_FIXTURE_PORT: '43220', DSH_FIXTURE_REQUEST_LOG: join(run, 'second-requests.jsonl') });
// Await this fixture's successful HTTP response before starting the Host.
const readyDeadline = Date.now() + 10000;
while (!abort.signal.aborted) {
abort.signal.throwIfAborted();
if (children.some(child => child.ended)) fail('REGISTRY_EXITED', 'Registry process exited; inspect private run logs.');
const ready = await fetch('http://127.0.0.1:43219/api/v1/search', { signal: AbortSignal.timeout(1000) }).then(r => r.ok, () => false);
if (ready) break;
if (Date.now() >= readyDeadline) fail('REGISTRY_TIMEOUT', 'Registry did not become ready.');
await delay(100, undefined, { signal: abort.signal });
}
abort.signal.throwIfAborted();
stage = 'host-result';
launch([dshBin, '--profile', 'skillhub-m0', '--patch', patch, '--host', '127.0.0.1', '--port', '0', '--no-open'], 'host.log');
const deadline = Date.now() + 90000;
while (!abort.signal.aborted) {
abort.signal.throwIfAborted();
if (children.some(child => child.ended)) fail('CHILD_EXITED', 'Probe process exited before completion; inspect private run logs.');
if (runtimeProbe) {
let marker;
try { marker = JSON.parse(await readFile(observerStatusFile, 'utf8')); }
catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
if (marker?.stage === 'validation-failed') {
stage = 'host-assertion';
fail('HOST_ASSERTION_FAILED', 'Host observer rejected the fixture; inspect private run logs.');
}
}
let report;
try { report = JSON.parse(await readFile(resultFile, 'utf8')); }
catch (error) { if (error.code !== 'ENOENT' && !(error instanceof SyntaxError)) throw error; }
if (report) {
if (report.passed !== true) {
if (['skill-availability', 'skill-toggle', 'skill-toggle-watch-off'].includes(values.probe)) operationObservation = safeOperationObservation(report.operation);
if (values.probe === 'skill-availability') availabilityCheckpoint = safeAvailabilityCheckpoint(report.checkpoint);
if (['skill-toggle', 'skill-toggle-watch-off'].includes(values.probe)) {
toggleCheckpoint = safeToggleCheckpoint(report.checkpoint);
fixtureLocation = safeFixtureLocation(report.location);
}
stage = 'host-assertion';
fail('HOST_ASSERTION_FAILED', 'Host probe failed; inspect its result file.');
}
console.log(JSON.stringify({ passed: true, probe: values.probe, report: resultFile, runDirectory: run }));
break;
}
if (Date.now() >= deadline) fail('HOST_RESULT_TIMEOUT', 'Probe exceeded its 90-second execution deadline.');
await delay(200, undefined, { signal: abort.signal });
}
abort.signal.throwIfAborted();
} finally {
for (const state of children.reverse()) {
if (!state.ended) {
state.child.kill('SIGTERM');
const timeout = setTimeout(() => { if (!state.ended) state.child.kill('SIGKILL'); }, 10000);
await state.done;
clearTimeout(timeout);
}
}
process.removeListener('SIGINT', stop);
process.removeListener('SIGTERM', stop);
}
}
main().catch(async () => {
const diagnostics = {};
if (availabilityCheckpoint) diagnostics.availability = availabilityCheckpoint;
if (toggleCheckpoint) diagnostics.toggle = toggleCheckpoint;
if (fixtureLocation) diagnostics.location = fixtureLocation;
if (operationObservation) diagnostics.operation = operationObservation;
if (hostLogFile && ['skill-availability', 'skill-toggle', 'skill-toggle-watch-off'].includes(selectedProbe)) {
let file;
try {
file = await open(hostLogFile, 'r');
const { size } = await file.stat();
const bytes = Buffer.alloc(Math.min(size, 1024 * 1024));
const { bytesRead } = await file.read(bytes, 0, bytes.length, Math.max(0, size - bytes.length));
diagnostics.observedHostCodes = knownHostFailureCodes(bytes.subarray(0, bytesRead).toString('utf8'));
} catch { /* Diagnostics must not replace the original failure. */ }
finally { await file?.close().catch(() => {}); }
}
for (const [key, file] of [['observer', observerStatusFile], ['dependencies', dependencyStatusFile]]) {
if (!file) continue;
try { diagnostics[key] = JSON.parse(await readFile(file, 'utf8')); } catch { /* Missing marker is itself diagnostic. */ }
}
// Fixed fields only: never copy Host errors, paths or tokens into CI diagnostics.
console.error(JSON.stringify({ passed: false, probe: selectedProbe, stage, failureCode, diagnostics }));
process.exitCode = 1;
});