skillhub/plugins/dsh/test/integration/runtime-baseline-probe.mjs
XiaoSeS 3560679793
Some checks failed
Deploy Docs / build (push) Has been cancelled
Security / Dependency Review (push) Has been cancelled
Security / CodeQL (java-kotlin) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Deploy Docs / Deploy (push) Has been cancelled
feat(dsh): add native SkillHub plugin for DeepSeek Harness (#956)
Add the native SkillHub plugin for DeepSeek Harness with a scoped skill center, conversation search/install tools, exact-version installation and protected lifecycle management. Prepare the independently versioned 0.1.0 npm candidate with an empty default Registry and documented compatibility limits.

Keep formal specifications and executable regression tests; remove archived development screenshots and runtime reports from the final tree. Review fixes cover independent scopes, installed references, pre-staging recovery and idempotent discovery.

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-10-16 19:22:26 +08:00

127 lines
5.9 KiB
JavaScript

// Temporary Host observer; never included in the published plugin.
// Copy outside the package before loading: DSH associates local modules with their owning manifest.
import assert from 'node:assert/strict';
import { realpathSync, writeFileSync } from 'node:fs';
import { readFile, readdir, writeFile } from 'node:fs/promises';
import { randomUUID } from 'node:crypto';
import { tmpdir } from 'node:os';
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
function mark(stage, validation) {
if (!process.env.DSH_RUNTIME_PROBE_STATUS) return;
try { writeFileSync(process.env.DSH_RUNTIME_PROBE_STATUS, JSON.stringify({ stage, validation })); }
catch { /* Diagnostics must not replace the actual probe result. */ }
}
mark('imported');
export const inject = ['loader', 'pluginPackages', 'clientModules', 'skillhub'];
export function apply(ctx, config) {
mark('applied');
let root;
let validation = 'root-realpath';
try {
root = realpathSync(resolve(config.root));
validation = 'temporary-realpath';
const rel = relative(realpathSync(tmpdir()), root);
validation = 'temporary-containment';
assert(rel && !rel.startsWith('..') && !isAbsolute(rel));
validation = 'home-identity';
assert.equal(process.env.DSH_HOME, join(root, 'home'));
} catch (error) { mark('validation-failed', validation); throw error; }
mark('validated');
const timer = setTimeout(() => {
mark('running');
run(ctx, config).then(
result => { mark('completed'); return writeFile(join(root, config.output), JSON.stringify(result, null, 2)); },
error => { mark('assertion-failed'); return writeFile(join(root, config.output), JSON.stringify({ passed: false, error: error.message })); },
).catch(() => undefined);
}, 1000);
return () => clearTimeout(timer);
}
async function ownerOf(file, name) {
let directory = dirname(file);
while (directory) {
try {
const manifest = JSON.parse(await readFile(join(directory, 'package.json'), 'utf8'));
if (manifest.name === name) return { name, version: manifest.version };
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
const parent = dirname(directory);
if (parent === directory) break;
directory = parent;
}
throw new Error(`No owning manifest for ${name}`);
}
async function inspect(ctx, config) {
const parentURL = pathToFileURL(config.pluginEntry).href;
const host = config.hostPackages.map(name => {
const pkg = ctx.pluginPackages.packageOf(name, parentURL);
return { name, resolved: !!pkg, version: pkg?.version ?? null };
});
const client = await Promise.all(config.clientPackages.map(async name => {
const file = ctx.clientModules.clientPath(name);
return file ? { ...await ownerOf(file, name), active: true } : { name, active: false };
}));
const pluginCompatibility = ctx.skillhub.getCompatibility();
let rejectedBeforePreparation;
if (config.expectBlocked === true) {
assert.equal(pluginCompatibility.localWritesAllowed, false, 'Unverified runtime must reject writes');
const runtime = Reflect.get(ctx.skillhub, 'localRuntime');
assert.equal(runtime?.getStatus().state, 'ready');
const journal = join(config.root, 'home/skillhub/operations');
const before = (await readdir(journal)).filter(name => name.endsWith('.json')).sort();
let prepared = false;
await assert.rejects(runtime.submitInstall({
hostInstanceId: runtime.getStatus().hostInstanceId,
ownerId: 'runtime-baseline-probe', requestKey: randomUUID(), mode: 'new', parameters: { probe: true },
}, async () => { prepared = true; throw new Error('Unexpected preparation'); }), /runtime baseline could not be verified/);
assert.equal(prepared, false);
assert.deepEqual((await readdir(journal)).filter(name => name.endsWith('.json')).sort(), before);
rejectedBeforePreparation = true;
}
return { passed: true, node: process.versions.node, host, client, pluginCompatibility, rejectedBeforePreparation };
}
async function waitFor(check, message = 'Client graph did not reach expected state') {
const deadline = Date.now() + 10000;
while (!check()) {
assert(Date.now() < deadline, message);
await new Promise(resolve => setTimeout(resolve, 25));
}
}
async function run(ctx, config) {
// Disk-backed recovery initializes independently of the plugin registration.
// Observe readiness before testing reload behavior, rather than assuming 1s is enough.
await waitFor(() => {
const state = Reflect.get(ctx.skillhub, 'localRuntime')?.getStatus().state;
assert(!['failed', 'closing', 'disposed'].includes(state), `Local runtime entered ${state} before probe`);
return state === 'ready';
}, 'Local runtime did not become ready before probe');
if (!config.reloadClient) return inspect(ctx, config);
assert.equal(config.reloadClient, '@deepseek-ai/dsh-client-ui-layout');
const matches = [...ctx.loader.entries()].filter(entry => entry.options.name === config.reloadClient);
assert.equal(matches.length, 1);
const entry = matches[0];
assert.equal(entry.disabled, false, 'Layout entry must initially be enabled');
const disabled = entry.options.disabled;
const initial = await inspect(ctx, config);
assert.equal(initial.pluginCompatibility.localWritesAllowed, true, 'Initial runtime must match baseline');
let blocked;
try {
await entry.update({ disabled: true });
await waitFor(() => !ctx.clientModules.clientPath(config.reloadClient));
blocked = await inspect(ctx, { ...config, expectBlocked: true });
} finally {
await entry.update({ disabled: disabled ?? null });
}
await waitFor(() => !!ctx.clientModules.clientPath(config.reloadClient));
const restored = await inspect(ctx, config);
assert.equal(restored.pluginCompatibility.localWritesAllowed, true, 'Restored layout must allow baseline writes');
return { passed: true, sameProcess: true, initial, blocked, restored };
}