forked from cflmy/deepseek-harness
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).
174 lines
8.1 KiB
TypeScript
174 lines
8.1 KiB
TypeScript
/**
|
|
* Private knowledge behavior: import policy, locator disclosure, exact reads,
|
|
* export and deletion.
|
|
*/
|
|
|
|
import assert from 'node:assert/strict'
|
|
import { mkdtemp, readFile, readdir, symlink, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { test } from 'node:test'
|
|
|
|
import { ensureMdhHome } from '../src/core/home.ts'
|
|
import { KnowledgeManager } from '../src/knowledge/manager.ts'
|
|
|
|
/** Create a manager over a private temporary home with no CLI/Tool. */
|
|
async function manager(): Promise<{ manager: KnowledgeManager; home: string; root: string }> {
|
|
const root = await mkdtemp(join(tmpdir(), 'mdh-knowledge-'))
|
|
const layout = await ensureMdhHome(join(root, 'home'))
|
|
const manager = new KnowledgeManager({ layout, cli: async () => undefined, toolHome: async () => undefined })
|
|
return { manager, home: layout.home, root }
|
|
}
|
|
|
|
test('collections have stable local ids and reject duplicates', async () => {
|
|
const { manager: knowledge } = await manager()
|
|
const collection = await knowledge.create({ name: '校正手册', description: '实验规则' })
|
|
assert.match(collection.id, /^kc-[0-9a-f]{12}$/)
|
|
assert.equal(collection.enabled, true)
|
|
assert.equal(collection.sources.length, 0)
|
|
await assert.rejects(knowledge.create({ name: '校正手册' }), /already exists/)
|
|
const listed = await knowledge.collections()
|
|
assert.equal(listed.length, 1)
|
|
})
|
|
|
|
test('plain Markdown is copied and executable documents are refused', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '笔记' })
|
|
const source = join(root, 'calibration.md')
|
|
await writeFile(source, '# 校正规则\n\n对于非负读数 x,校正值为 2*x+1。\n')
|
|
const result = await knowledge.import({ collection: collection.id, paths: [source] })
|
|
assert.equal(result.imported.length, 1)
|
|
assert.equal(result.imported[0].title, '校正规则')
|
|
assert.match(result.imported[0].digest, /^sha256:[0-9a-f]{64}$/)
|
|
// The original file is never modified.
|
|
assert.match(await readFile(source, 'utf8'), /2\*x\+1/)
|
|
|
|
const executable = join(root, 'exec.mq.md')
|
|
await writeFile(executable, '# @knowledge\n\n## @exec 计算\n\n`return 1`\n')
|
|
const refused = await knowledge.import({ collection: collection.id, paths: [executable] })
|
|
assert.equal(refused.imported.length, 0)
|
|
assert.match(refused.rejected[0].reason, /executable \.mq\.md/)
|
|
|
|
const marker = join(root, 'marker.md')
|
|
await writeFile(marker, '# 规则\n\n## @exec 隐藏计算\n\n`return 1`\n')
|
|
const markerResult = await knowledge.import({ collection: collection.id, paths: [marker] })
|
|
assert.equal(markerResult.imported.length, 0)
|
|
assert.match(markerResult.rejected[0].reason, /@exec region/)
|
|
|
|
const binary = join(root, 'image.png')
|
|
await writeFile(binary, Buffer.from([0x89, 0x50, 0x4e, 0x47]))
|
|
const binaryResult = await knowledge.import({ collection: collection.id, paths: [binary] })
|
|
assert.equal(binaryResult.imported.length, 0)
|
|
assert.match(binaryResult.rejected[0].reason, /unsupported knowledge file type/)
|
|
})
|
|
|
|
test('symlinked imports and escaping paths are refused', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '链接' })
|
|
const real = join(root, 'real.md')
|
|
await writeFile(real, '# real\n')
|
|
const link = join(root, 'link.md')
|
|
await symlink(real, link)
|
|
const result = await knowledge.import({ collection: collection.id, paths: [link] })
|
|
assert.equal(result.imported.length, 0)
|
|
assert.match(result.rejected[0].reason, /symbolic link/)
|
|
})
|
|
|
|
test('browser uploads share the same import policy', async () => {
|
|
const { manager: knowledge } = await manager()
|
|
const collection = await knowledge.create({ name: '上传' })
|
|
const result = await knowledge.import({
|
|
collection: collection.id,
|
|
contents: [{ filename: 'upload.md', content: '# 上传文档\n\n内容\n' }],
|
|
})
|
|
assert.equal(result.imported.length, 1)
|
|
assert.equal(result.imported[0].origin, 'upload.md')
|
|
})
|
|
|
|
test('locator search is disclosed, bounded and keeps source identity', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '实验' })
|
|
const first = join(root, 'a.md')
|
|
await writeFile(first, '# 第一条\n\n量子态归一化检查方法。\n')
|
|
const second = join(root, 'b.md')
|
|
await writeFile(second, '# 第二条\n\n无关内容。\n')
|
|
await knowledge.import({ collection: collection.id, paths: [first, second] })
|
|
const result = await knowledge.search({ query: '归一化' })
|
|
assert.equal(result.mode, 'locator')
|
|
assert.equal(result.index_current, false)
|
|
assert.match(result.note, /read-only file locator/)
|
|
assert.equal(result.hits.length, 1)
|
|
const hit = result.hits[0]
|
|
assert.equal(hit.collection, collection.id)
|
|
assert.equal(hit.rel_path, 'a.md')
|
|
assert.equal(hit.title, '第一条')
|
|
assert.match(hit.digest, /^sha256:/)
|
|
assert.match(hit.snippet, /量子态归一化/)
|
|
assert.equal(hit.mode, 'locator')
|
|
|
|
await knowledge.setEnabled(collection.id, false)
|
|
const disabled = await knowledge.search({ query: '归一化' })
|
|
assert.equal(disabled.hits.length, 0)
|
|
})
|
|
|
|
test('exact read verifies the digest and is bounded', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '读取' })
|
|
const source = join(root, 'doc.md')
|
|
await writeFile(source, '# 标题\n\nabcdefghij\n')
|
|
const imported = await knowledge.import({ collection: collection.id, paths: [source] })
|
|
const digest = imported.imported[0].digest
|
|
const read = await knowledge.read({ collection: collection.id, relPath: 'doc.md', digest, start: 0, end: 10 })
|
|
assert.equal(read.text, '# 标题\n\n')
|
|
await assert.rejects(
|
|
knowledge.read({ collection: collection.id, relPath: 'doc.md', digest: 'sha256:deadbeef' }),
|
|
/digest does not match/,
|
|
)
|
|
await assert.rejects(
|
|
knowledge.read({ collection: collection.id, relPath: '../escape.md' }),
|
|
/unsafe source path/,
|
|
)
|
|
})
|
|
|
|
test('re-import preserves a digest history and changes the source revision', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '版本' })
|
|
const source = join(root, 'doc.md')
|
|
await writeFile(source, '# 第一版\n')
|
|
const first = await knowledge.import({ collection: collection.id, paths: [source] })
|
|
await writeFile(source, '# 第二版\n')
|
|
const second = await knowledge.import({ collection: collection.id, paths: [source] })
|
|
assert.notEqual(first.imported[0].digest, second.imported[0].digest)
|
|
const updated = await knowledge.collection(collection.id)
|
|
assert.equal(updated?.revision, 3)
|
|
const history = await readdir(join(knowledge.sourceRoot(collection.id), '.history'))
|
|
assert.equal(history.length, 1)
|
|
})
|
|
|
|
test('export copies only the selected collection', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '导出' })
|
|
const source = join(root, 'doc.md')
|
|
await writeFile(source, '# 导出文档\n')
|
|
await knowledge.import({ collection: collection.id, paths: [source] })
|
|
const destination = join(root, 'export')
|
|
const result = await knowledge.export(collection.id, destination)
|
|
assert.equal(result.files, 1)
|
|
assert.equal(await readFile(join(destination, 'doc.md'), 'utf8'), '# 导出文档\n')
|
|
const manifest = JSON.parse(await readFile(join(destination, 'mdh-knowledge-export.json'), 'utf8'))
|
|
assert.equal(manifest.schema, 'marqdo.mdh.knowledge-export')
|
|
assert.equal(manifest.sources.length, 1)
|
|
})
|
|
|
|
test('delete removes the copy and the collection metadata', async () => {
|
|
const { manager: knowledge, root } = await manager()
|
|
const collection = await knowledge.create({ name: '删除' })
|
|
const source = join(root, 'doc.md')
|
|
await writeFile(source, '# 删除\n')
|
|
await knowledge.import({ collection: collection.id, paths: [source] })
|
|
await knowledge.delete(collection.id)
|
|
assert.equal(await knowledge.collection(collection.id), undefined)
|
|
assert.match(await readFile(source, 'utf8'), /删除/)
|
|
await assert.rejects(readdir(knowledge.sourceRoot(collection.id)), /ENOENT/)
|
|
})
|