Files
cflmy 25fc24eea4 feat: add Marqdo Harness product services and workbench
Implement the downstream product in `packages/marqdo`:

- `@marqdo/mdh-harness`: application home layout, atomic writes, locks,
  bounded fixed-argv processes, safe tar extraction, Observation 1 and
  ValueWire validation;
- trusted Gitea release discovery, SHA-256/platform/Bundle verification,
  transactional environment install, rollback and cleanup, CLI Tool changes;
- private knowledge collections with strict import policy, the official
  `marqdo-knowledge` compile/query adapter and a disclosed locator fallback;
- Analysis 3 parsing, approved run/call execution, evidence and staleness;
- the six model operations (find/read/check/program/ecosystem/knowledge_search);
- the `/mdh` workbench over the existing web server with a same-origin fence;
- `mdh` launcher, CI/package/upstream scripts and a Woodpecker pipeline.

Tests: 28 node:test cases (27 PASS, one network-gated install test also PASS
with MDH_TEST_INSTALL=1). Product install/workbench evidence is recorded in
docs/mdh/evidence.

Part of marqdo#25 (MDH-03..MDH-08).
2026-11-28 10:41:39 +08:00

124 lines
4.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Marqdo Harness product launcher (`mdh`).
*
* Creates the application home, materialises the single `mdh` DSH profile with
* its bundle row, and starts the forked `dsh` launcher. It never rewrites a
* user's existing DSH home and never resolves a floating Marqdo runtime: the
* active environment is whatever the ecosystem manager activated.
*
* @module marqdo-app-harness/launcher
*/
import { access, chmod, mkdir, readFile, symlink, unlink, writeFile } from 'node:fs/promises'
import { constants } from 'node:fs'
import { homedir } from 'node:os'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawn } from 'node:child_process'
const here = dirname(fileURLToPath(import.meta.url))
const root = resolve(here, '..')
/** Resolve the application home without touching the user's `~/.dsh`. */
function resolveHome() {
const configured = process.env.MDH_HOME?.trim()
if (configured) return resolve(configured)
const xdg = process.env.XDG_DATA_HOME?.trim()
return xdg ? join(xdg, 'marqdo-harness') : join(homedir(), '.local', 'share', 'marqdo-harness')
}
/** Whether a path exists. */
async function exists(path) {
try {
await access(path, constants.F_OK)
return true
} catch {
return false
}
}
/** Find the built `dsh` launcher, or fall back to the installed dependency. */
async function findDsh() {
const override = process.env.MDH_DSH_BIN?.trim()
const candidates = [
override,
join(root, 'apps', 'cli', 'lib', 'bin.js'),
join(root, 'node_modules', '@deepseek-ai', 'dsh', 'lib', 'bin.js'),
].filter(Boolean)
for (const candidate of candidates) {
if (await exists(candidate)) return candidate
}
throw new Error(
'dsh launcher not found; run `pnpm install && pnpm run build` in the application checkout or set MDH_DSH_BIN',
)
}
/** Ensure the single product profile and its local bundle links. */
async function ensureProfile(dshHome) {
const profileDir = join(dshHome, 'profiles', 'mdh')
const nodeModules = join(profileDir, 'node_modules', '@marqdo')
await mkdir(nodeModules, { recursive: true, mode: 0o700 })
await chmod(profileDir, 0o700)
const manifest = {
name: 'mdh-profile',
private: true,
dsh: {
profile: {
bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', '@marqdo/mdh-bundle'],
},
},
}
const manifestPath = join(profileDir, 'package.json')
const current = await readFile(manifestPath, 'utf8').catch(() => '')
if (current !== `${JSON.stringify(manifest, null, 2)}\n`) {
await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, { mode: 0o600 })
}
// Bundle packages are resolved from the profile's own node_modules. Removing
// a stale link first keeps a moved checkout from loading old code.
for (const [name, target] of [
['mdh-bundle', join(root, 'packages', 'marqdo', 'bundle')],
['mdh-harness', join(root, 'packages', 'marqdo', 'harness')],
]) {
const link = join(nodeModules, name)
await unlink(link).catch(() => undefined)
await symlink(target, link, 'dir')
}
return profileDir
}
/** Start the forked launcher with the application home. */
async function main() {
const home = resolveHome()
await mkdir(home, { recursive: true, mode: 0o700 })
await chmod(home, 0o700)
const dshHome = join(home, 'harness')
await mkdir(dshHome, { recursive: true, mode: 0o700 })
await ensureProfile(dshHome)
const dsh = await findDsh()
const args = [dsh, '--profile', 'mdh', ...process.argv.slice(2)]
const child = spawn(process.execPath, args, {
stdio: 'inherit',
env: {
...process.env,
DSH_HOME: dshHome,
MDH_HOME: home,
MDH_WORKSPACE: process.env.MDH_WORKSPACE?.trim() || join(home, 'workspaces', 'default'),
},
})
const forward = (signal) => {
if (child.exitCode === null && child.signalCode === null) child.kill(signal)
}
process.on('SIGINT', () => forward('SIGINT'))
process.on('SIGTERM', () => forward('SIGTERM'))
child.on('exit', (code, signal) => {
if (signal) process.kill(process.pid, signal)
else process.exit(code ?? 1)
})
}
main().catch((error) => {
process.stderr.write(`mdh: ${error instanceof Error ? error.message : String(error)}\n`)
process.exit(1)
})