198 lines
10 KiB
JavaScript
198 lines
10 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Materialize the independent dsh-brain Mnemon fork.
|
|
*
|
|
* The snapshot is omdsh-dev/dsh-mnemon with every package, import specifier,
|
|
* runtime schema id and bundle entry id renamed from dsh-mnemon to
|
|
* dsh-brain-mnemon. The MIT license and upstream provenance stay in the tree.
|
|
*
|
|
* Usage:
|
|
* node scripts/fork-mnemon.mjs --source <upstream-checkout> [--target vendor/dsh-mnemon] [--force]
|
|
* node scripts/fork-mnemon.mjs --check [--target vendor/dsh-mnemon]
|
|
*/
|
|
import { cp, mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
import { dirname, join, relative, resolve, sep } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
const DEFAULT_TARGET = join(ROOT, 'vendor', 'dsh-mnemon')
|
|
const UPSTREAM_REPOSITORY = 'https://github.com/omdsh-dev/dsh-mnemon.git'
|
|
const PINNED_REVISION = '8b650ab5ef33522e03887b250b13336593daa26f'
|
|
const PINNED_VERSION = '0.5.54'
|
|
const FORK_NAME = 'dsh-brain-mnemon'
|
|
const FORK_VERSION = PINNED_VERSION + '-dsh.1'
|
|
const FORK_DISPLAY_NAME = 'Dsh Brain Mnemon'
|
|
|
|
const REWRITE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.mjs', '.cjs', '.json', '.jsonc', '.yml', '.yaml', '.css', '.scss', '.html', '.toml', '.sh', '.ps1', '.patch', '.snapshot'])
|
|
const SKIP_DIRS = new Set(['.git', 'node_modules', 'dist', 'coverage'])
|
|
const SKIP_PREFIXES = ['LICENSE', 'THIRD_PARTY_NOTICES', 'CHANGELOG', 'README', 'CONTRIBUTING', 'SECURITY', 'ISSUE_TRIAGE', 'docs/', 'assets/', 'examples/']
|
|
|
|
function parseArgs(argv) {
|
|
const args = { target: DEFAULT_TARGET, force: false, check: false, source: '' }
|
|
for (let i = 0; i < argv.length; i += 1) {
|
|
const token = argv[i]
|
|
if (token === '--force') args.force = true
|
|
else if (token === '--check') args.check = true
|
|
else if (token === '--source') args.source = resolve(argv[++i] || '')
|
|
else if (token === '--target') args.target = resolve(argv[++i] || '')
|
|
else if (token === '--help' || token === '-h') args.help = true
|
|
else throw new Error('Unknown argument: ' + token)
|
|
}
|
|
return args
|
|
}
|
|
|
|
function assertSafeTarget(target) {
|
|
const vendorRoot = join(ROOT, 'vendor')
|
|
if (resolve(target) !== DEFAULT_TARGET) throw new Error('Refusing to write outside the canonical vendor/dsh-mnemon target')
|
|
}
|
|
|
|
async function walk(dir, files = []) {
|
|
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
if (entry.isSymbolicLink()) continue
|
|
const full = join(dir, entry.name)
|
|
if (entry.isDirectory()) { if (!SKIP_DIRS.has(entry.name)) await walk(full, files) }
|
|
else if (entry.isFile()) files.push(full)
|
|
}
|
|
return files
|
|
}
|
|
|
|
function isTextFile(file) {
|
|
const ext = file.slice(file.lastIndexOf('.')).toLowerCase()
|
|
return REWRITE_EXTENSIONS.has(ext) || !file.includes('.')
|
|
}
|
|
|
|
function shouldRewrite(file, target) {
|
|
const rel = relative(target, file).split(sep).join('/')
|
|
if (SKIP_PREFIXES.some(prefix => rel === prefix.replace(/\/$/, '') || rel.startsWith(prefix))) return false
|
|
return isTextFile(file)
|
|
}
|
|
|
|
async function rewriteTree(target) {
|
|
let rewritten = 0
|
|
for (const file of await walk(target)) {
|
|
if (!shouldRewrite(file, target)) continue
|
|
const before = await readFile(file, 'utf8')
|
|
if (!before.includes('dsh-mnemon') && !before.includes('id: mnemon')) continue
|
|
let after = before.replaceAll('dsh-mnemon', FORK_NAME)
|
|
after = after.replace(/id: mnemon-bundle\b/g, 'id: ' + FORK_NAME + '-bundle')
|
|
after = after.replace(/id: mnemon\b/g, 'id: ' + FORK_NAME)
|
|
after = after.replace(/id: mnemon-/g, 'id: ' + FORK_NAME + '-')
|
|
if (after !== before) { await writeFile(file, after, 'utf8'); rewritten += 1 }
|
|
}
|
|
return rewritten
|
|
}
|
|
|
|
async function restoreUpstreamUrls(target) {
|
|
let restored = 0
|
|
for (const file of await walk(target)) {
|
|
if (!shouldRewrite(file, target)) continue
|
|
const before = await readFile(file, 'utf8')
|
|
const after = before.replaceAll('https://github.com/omdsh-dev/dsh-brain-mnemon', 'https://github.com/omdsh-dev/dsh-mnemon')
|
|
if (after !== before) { await writeFile(file, after, 'utf8'); restored += 1 }
|
|
}
|
|
return restored
|
|
}
|
|
|
|
async function renamePluginDirs(target) {
|
|
const pluginRoot = join(target, 'plugins')
|
|
let renamed = 0
|
|
for (const entry of await readdir(pluginRoot, { withFileTypes: true })) {
|
|
if (!entry.isDirectory() || !entry.name.startsWith('dsh-mnemon-')) continue
|
|
await rename(join(pluginRoot, entry.name), join(pluginRoot, entry.name.replace(/^dsh-mnemon-/, FORK_NAME + '-')))
|
|
renamed += 1
|
|
}
|
|
return renamed
|
|
}
|
|
|
|
async function finalizeRootPackage(target) {
|
|
const packagePath = join(target, 'package.json')
|
|
const pkg = JSON.parse(await readFile(packagePath, 'utf8'))
|
|
pkg.name = FORK_NAME
|
|
pkg.version = FORK_VERSION
|
|
pkg.private = true
|
|
pkg.description = FORK_DISPLAY_NAME + ': independent dsh-brain fork of dsh-mnemon. Composable, view-based context for DeepSeek Harness with pluggable memory sources, strategies and skills.'
|
|
if (pkg.publishConfig) delete pkg.publishConfig
|
|
pkg.repository = { type: 'git', url: UPSTREAM_REPOSITORY }
|
|
pkg.homepage = UPSTREAM_REPOSITORY.replace(/\.git$/, '') + '#readme'
|
|
pkg.bugs = { url: UPSTREAM_REPOSITORY.replace(/\.git$/, '') + '/issues' }
|
|
pkg.dshBrainFork = { displayName: FORK_DISPLAY_NAME, upstream: UPSTREAM_REPOSITORY, upstreamRevision: PINNED_REVISION, upstreamVersion: PINNED_VERSION, license: 'MIT', note: 'Keep upstream URLs and MIT notices when syncing.' }
|
|
if (pkg.bin && typeof pkg.bin === 'object') {
|
|
pkg.bin = Object.fromEntries(Object.entries(pkg.bin).map(([name, value]) => [name.replace(/^dsh-mnemon/, FORK_NAME), value]))
|
|
}
|
|
await writeFile(packagePath, JSON.stringify(pkg, null, 2) + '\n', 'utf8')
|
|
}
|
|
|
|
async function writeForkNotice(target) {
|
|
const notice = [
|
|
'# dsh-brain Mnemon fork',
|
|
'',
|
|
'- Display name: ' + FORK_DISPLAY_NAME,
|
|
'- Fork package name: ' + FORK_NAME,
|
|
'- Fork version: ' + FORK_VERSION,
|
|
'- Upstream: ' + UPSTREAM_REPOSITORY,
|
|
'- Upstream revision: ' + PINNED_REVISION,
|
|
'- Upstream version: ' + PINNED_VERSION,
|
|
'- License: MIT (see LICENSE)',
|
|
'',
|
|
'This directory is a source snapshot, not a git submodule. The tree keeps the upstream',
|
|
'MIT license, copyright notice and third-party notices. Package names, import',
|
|
'specifiers, runtime schema ids, bundle entry ids and the repair-session bin name',
|
|
'are rewritten from dsh-mnemon to ' + FORK_NAME + ' so the fork can coexist with',
|
|
'the upstream plugin without registering the same package or entry ids.',
|
|
'',
|
|
'Regenerate with:',
|
|
'',
|
|
' node scripts/fork-mnemon.mjs --source <upstream-checkout> --target vendor/dsh-mnemon --force',
|
|
'',
|
|
'Update procedure: check out the desired upstream revision, run the command above,',
|
|
'then bump PINNED_REVISION / PINNED_VERSION in scripts/fork-mnemon.mjs.',
|
|
'Review the diff before committing; do not rewrite upstream docs or license files.',
|
|
'',
|
|
].join('\n')
|
|
await writeFile(join(target, 'FORK_NOTICE.md'), notice, 'utf8')
|
|
}
|
|
|
|
async function verify(target) {
|
|
const pkg = JSON.parse(await readFile(join(target, 'package.json'), 'utf8'))
|
|
if (pkg.name !== FORK_NAME) throw new Error('package.name is ' + pkg.name + ', expected ' + FORK_NAME)
|
|
if (!pkg.private) throw new Error('fork package must stay private')
|
|
if (!pkg.dshBrainFork || pkg.dshBrainFork.upstreamRevision !== PINNED_REVISION) throw new Error('missing fork provenance metadata')
|
|
const entry = await readFile(join(target, 'cordis.patch.yml'), 'utf8')
|
|
if (!entry.includes('name: ' + FORK_NAME + '/bundle')) throw new Error('bundle entry was not renamed')
|
|
if (!entry.includes('id: ' + FORK_NAME + '-bundle')) throw new Error('bundle entry id was not renamed')
|
|
let forbidden = 0
|
|
for (const file of await walk(target)) {
|
|
if (!shouldRewrite(file, target) || relative(target, file) === 'package.json') continue
|
|
const body = await readFile(file, 'utf8')
|
|
const sanitized = body.replaceAll('https://github.com/omdsh-dev/dsh-mnemon', '')
|
|
if (sanitized.includes('dsh-mnemon') || /id: mnemon\b/.test(sanitized)) { forbidden += 1; console.error('unrenamed upstream identifier: ' + relative(ROOT, file)) }
|
|
}
|
|
if (forbidden > 0) throw new Error(forbidden + ' rewritten-eligible files still mention dsh-mnemon')
|
|
for (const entry of await readdir(join(target, 'plugins'), { withFileTypes: true })) {
|
|
if (entry.isDirectory() && !entry.name.startsWith(FORK_NAME + '-')) throw new Error('plugin directory not renamed: ' + entry.name)
|
|
}
|
|
return { target: relative(ROOT, target), name: pkg.name, version: pkg.version, private: pkg.private, revision: PINNED_REVISION }
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2))
|
|
if (args.help) { console.log('Usage: node scripts/fork-mnemon.mjs --source <upstream-checkout> [--target vendor/dsh-mnemon] [--force]'); return }
|
|
assertSafeTarget(args.target)
|
|
if (args.check) { console.log(JSON.stringify(await verify(args.target), null, 2)); return }
|
|
if (!args.source) throw new Error('--source is required when materializing a fork (or use --check)')
|
|
const sourcePackage = JSON.parse(await readFile(join(args.source, 'package.json'), 'utf8'))
|
|
if (sourcePackage.name !== 'dsh-mnemon') throw new Error('source package name is ' + sourcePackage.name + ', expected dsh-mnemon')
|
|
if (sourcePackage.version !== PINNED_VERSION) throw new Error('source version is ' + sourcePackage.version + ', expected ' + PINNED_VERSION)
|
|
try { await stat(args.target); if (!args.force) throw new Error('target exists; pass --force to replace it'); await rm(args.target, { recursive: true, force: true }) }
|
|
catch (error) { if (error.code !== 'ENOENT') throw error }
|
|
await mkdir(args.target, { recursive: true })
|
|
await cp(args.source, args.target, { recursive: true, filter: src => !src.includes(sep + '.git' + sep) && !src.endsWith(sep + '.git') && !src.includes(sep + 'node_modules' + sep) })
|
|
const rewritten = await rewriteTree(args.target)
|
|
const restoredUrls = await restoreUpstreamUrls(args.target)
|
|
const renamedDirs = await renamePluginDirs(args.target)
|
|
await finalizeRootPackage(args.target)
|
|
await writeForkNotice(args.target)
|
|
console.log(JSON.stringify({ ...(await verify(args.target)), rewrittenFiles: rewritten, restoredUpstreamUrls: restoredUrls, renamedPluginDirs: renamedDirs }, null, 2))
|
|
}
|
|
|
|
main().catch(error => { console.error('fork-mnemon: ' + error.message); process.exitCode = 1 }) |