From 210ac86b0be3b0d7d8e51e7c27dc42b82a074be4 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 28 Nov 2026 10:52:32 +0800 Subject: [PATCH] feat: save a reviewed completed run as static knowledge Add the reviewed outcome flow: a completed, fresh run can be confirmed into a static knowledge document that records applicability, executable asset identity, run/evidence id and boundaries. The generated document has no @exec region and grants no execution permission; it enters the same private knowledge index and can be searched later. Expose it as POST /mdh/api/runs//candidate with explicit confirmation and a workbench button, and cover the policy in tests. Record the View result-binding gap as marqdo-view#15 instead of rendering results here. Part of marqdo#25 (MDH-07, K07). --- docs/mdh/STATUS.md | 6 +- docs/mdh/evidence/2026-11-28-m1-m6-product.md | 11 ++- .../marqdo/harness/src/knowledge/manager.ts | 77 +++++++++++++++++++ packages/marqdo/harness/src/web/assets.ts | 17 ++++ packages/marqdo/harness/src/web/index.ts | 34 ++++++++ .../marqdo/harness/tests/knowledge.test.ts | 41 ++++++++++ 6 files changed, 182 insertions(+), 4 deletions(-) diff --git a/docs/mdh/STATUS.md b/docs/mdh/STATUS.md index 342574c899..0f3f94f14f 100644 --- a/docs/mdh/STATUS.md +++ b/docs/mdh/STATUS.md @@ -44,7 +44,7 @@ | P06 失败/超时/取消 | PARTIAL | 真实除零运行得到 `failed` + `runtime.division_by_zero`;进程超时/取消与进程组清理有单元测试;CLI 级超时/取消待补。 | | P07 ValueWire | PASS | 超过安全范围的整数被拒绝;`decimal` 字符串原样保留。 | | P08 只读检查/源变更重检 | PASS | check/read 不执行;源 digest 变化使证据 `stale`,重新 check 才可再次执行。 | -| P09 View 离线导出 | UNVERIFIED | `marqdo-view` 0.3.0 公开 `view export` 可用,但尚未安装并接入“绑定已发生运行”的导出 API。 | +| P09 View 离线导出 | UNVERIFIED(上游缺口) | `marqdo-view` 0.3.0 的 `view export` 没有接受既有运行身份的公开参数;已开 [marqdo-view#15](https://git.cflmy.cn/cflmy/marqdo-view/issues/15)。Harness 不自行渲染结果。 | | P10 真实模型任务 A | UNVERIFIED | 当前环境没有可用模型凭据;没有用 fixture 冒充。 | ## 私人知识与复用(K) @@ -57,8 +57,8 @@ | K04 恶意知识不触发执行 | PASS | `.mq.md`、`@exec` 区域、符号链接、越界路径、危险类型、超限均拒绝;知识无执行入口。 | | K05 启停/删除/导出/索引重建 | PASS | 单元测试覆盖;原始外部文件不被修改。 | | K06 远程上下文仅批准片段 | PARTIAL | `knowledge_search` 只返回有界片段与来源,未启用集合不会检索;尚未接入模型请求的逐请求审计 UI。 | -| K07 用户审核后保存候选 | UNVERIFIED | 首版尚未实现“保存为可复用成果”的交互。 | -| K08 任务 B 真实复用 | UNVERIFIED | 依赖真实模型;`marqdo_program` 复用同一文稿与函数身份的能力已具备。 | +| K07 用户审核后保存候选 | PASS(API/单元) | `POST /mdh/api/runs//candidate` 要求 completed + fresh + 显式确认,生成无 `@exec` 的静态知识文档并进入私人索引;单元测试覆盖拒绝多行标题与可检索性。浏览器按钮流程属 S06。 | +| K08 任务 B 真实复用 | UNVERIFIED | 依赖真实模型;`marqdo_program` 复用同一文稿与函数身份、且每次生成新 run_id/新证据的能力已具备。 | | K09 旧证据标记 | PASS | 源变更后记录 `stale`;不把旧结果当新运行。 | ## 安全、产品与交付(S) diff --git a/docs/mdh/evidence/2026-11-28-m1-m6-product.md b/docs/mdh/evidence/2026-11-28-m1-m6-product.md index f2b2860cd4..8d9aecff43 100644 --- a/docs/mdh/evidence/2026-11-28-m1-m6-product.md +++ b/docs/mdh/evidence/2026-11-28-m1-m6-product.md @@ -80,7 +80,16 @@ GET /mdh/api/status Origin: http://evil.example -> 403 上游 `plugin-manager` 服务关闭后,`creation-recommendations`/`plugin-recommendations` 等待该服务且不激活;不是仅隐藏 UI。 -## 7. 候选归档 +## 7. 成果沉淀 + +```text +POST /mdh/api/runs//candidate {"confirm":true,"title":...,"applicability":...} +-> 201 collection= source=outcome-.md executable=none +``` + +单元测试 `knowledge.test.ts` 验证生成文档含适用条件/来源/运行 ID/边界、无 `@exec`、可被检索;多行标题被拒绝。 + +## 8. 候选归档 ```text scripts/package.sh diff --git a/packages/marqdo/harness/src/knowledge/manager.ts b/packages/marqdo/harness/src/knowledge/manager.ts index 1827b498aa..37cc316eb4 100644 --- a/packages/marqdo/harness/src/knowledge/manager.ts +++ b/packages/marqdo/harness/src/knowledge/manager.ts @@ -668,6 +668,83 @@ export class KnowledgeManager { return hits } + /** + * Save one completed run as a reviewed static knowledge candidate. + * + * The generated document contains only prose and source identity: no + * execution region, no credentials, no session text. Creating it does not + * grant execution and does not run the document again. + * @param input - user-reviewed title, applicability and exact run identity. + * @returns the target collection and the imported candidate. + */ + async saveOutcome(input: { + title: string + applicability: string + notes?: string | undefined + source: { + document: string + documentDigest: string + analysisDigest: string + functionName: string | null + runId: string + evidenceId: string + valueRender: string | null + mode: string + environmentId: string | null + } + }): Promise<{ collection: KnowledgeCollection; imported: readonly KnowledgeSource[] }> { + const title = input.title.trim() + if (title === '' || title.length > 200) throw new KnowledgeError('outcome title must be 1..200 characters') + if (input.title.includes('\n') || input.title.includes('\r')) throw new KnowledgeError('outcome title must be one line') + const applicability = input.applicability.trim() + if (applicability === '' || applicability.length > 2000) { + throw new KnowledgeError('applicability must be 1..2000 characters') + } + let collection = (await this.collections()).find(entry => entry.name === '运行成果') + if (!collection) collection = await this.create({ name: '运行成果', description: '用户审核后保存的已完成运行' }) + const heading = title.replace(/^#+\s*/, '') + const content = [ + `# ${heading}`, + '', + '## 适用条件', + '', + applicability, + '', + '## 可执行资产', + '', + `- 文稿:\`${input.source.document}\``, + `- 文稿摘要:\`${input.source.documentDigest}\``, + `- Analysis:\`${input.source.analysisDigest}\``, + `- 函数:\`${input.source.functionName ?? 'run'}\``, + `- 执行模式:\`${input.source.mode}\``, + `- 环境:\`${input.source.environmentId ?? 'unknown'}\``, + '', + '## 运行证据', + '', + `- run:\`${input.source.runId}\``, + `- evidence:\`${input.source.evidenceId}\``, + `- 结果:${input.source.valueRender ?? '(无标量结果)'}`, + '', + '## 未解决问题', + '', + input.notes?.trim() || '无。', + '', + '## 边界', + '', + '本记录是静态知识,不含可执行区域,也不授予任何执行权限。再次使用必须重新检查源文稿、依赖与适用范围,并通过执行审批。', + '', + ].join('\n') + const imported = await this.import({ + collection: collection.id, + contents: [{ filename: `outcome-${input.source.evidenceId}.md`, content }], + }) + if (imported.imported.length === 0) { + throw new KnowledgeError(`outcome was rejected: ${imported.rejected[0]?.reason ?? 'unknown reason'}`) + } + const refreshed = await this.collection(collection.id) + return { collection: refreshed ?? collection, imported: imported.imported } + } + /** Export one collection without secrets, sessions or unrelated files. */ async export(id: string, destination: string): Promise<{ files: number; manifest: string }> { const collection = await this.collection(id) diff --git a/packages/marqdo/harness/src/web/assets.ts b/packages/marqdo/harness/src/web/assets.ts index e0b8a52d4f..add0a191bc 100644 --- a/packages/marqdo/harness/src/web/assets.ts +++ b/packages/marqdo/harness/src/web/assets.ts @@ -380,6 +380,23 @@ async function loadRuns() { const node = $('#run-detail'); clear(node); node.append(el('pre', { class: 'code', text: JSON.stringify(detail.run, null, 2) })); + if (detail.run.status === 'completed' && detail.run.freshness === 'fresh') { + node.append(el('button', { type: 'button', text: '保存为可复用成果', onclick: async () => { + const title = prompt('成果标题'); + if (!title) return; + const applicability = prompt('适用条件(什么输入与边界下可复用)'); + if (!applicability) return; + if (!confirm('保存为静态知识候选?不会再次执行文稿,也不会授予执行权限。')) return; + try { + const saved = await api('/runs/' + run.run_id + '/candidate', { + method: 'POST', body: JSON.stringify({ confirm: true, title, applicability }), + }); + await loadCollections(); + await loadStatus(); + alert('已保存到集合 ' + saved.collection + ':' + saved.source); + } catch (error) { showError('保存失败:' + error.message); } + } })); + } } }), ), )); diff --git a/packages/marqdo/harness/src/web/index.ts b/packages/marqdo/harness/src/web/index.ts index cf2d370895..5e4349803a 100644 --- a/packages/marqdo/harness/src/web/index.ts +++ b/packages/marqdo/harness/src/web/index.ts @@ -320,6 +320,40 @@ async function handleApi( json(res, 200, { runs: await runtime.evidence.list(runtime.workspace, 50) }) return } + const candidateMatch = /^\/runs\/([A-Za-z0-9._-]+)\/candidate$/.exec(path) + if (candidateMatch && method === 'POST') { + const runId = candidateMatch[1] + if (runId === undefined) throw new HttpError(400, 'missing run id') + const body = await readJson(req) + if (body.confirm !== true) throw new HttpError(400, 'saving an outcome requires explicit confirmation') + const record = await runtime.evidence.read(runId, runtime.workspace) + if (!record) throw new HttpError(404, 'unknown run') + if (record.status !== 'completed') throw new HttpError(409, 'only a completed run can become an outcome') + if (record.freshness !== 'fresh') throw new HttpError(409, 'the run evidence is stale; re-check and re-run before saving') + const saved = await runtime.knowledge.saveOutcome({ + title: stringField(body, 'title'), + applicability: stringField(body, 'applicability'), + notes: typeof body.notes === 'string' ? body.notes : undefined, + source: { + document: record.document, + documentDigest: record.document_digest, + analysisDigest: record.analysis_digest, + functionName: record.function_name, + runId: record.run_id, + evidenceId: record.run_id, + valueRender: record.result.value_render, + mode: record.mode, + environmentId: record.environment_id, + }, + }) + json(res, 201, { + collection: saved.collection.id, + source: saved.imported[0]?.relPath ?? null, + digest: saved.imported[0]?.digest ?? null, + executable: 'none', + }) + return + } const runMatch = /^\/runs\/([A-Za-z0-9._-]+)$/.exec(path) if (runMatch && method === 'GET') { const runId = runMatch[1] diff --git a/packages/marqdo/harness/tests/knowledge.test.ts b/packages/marqdo/harness/tests/knowledge.test.ts index e7e3353ec6..cd3d3178ca 100644 --- a/packages/marqdo/harness/tests/knowledge.test.ts +++ b/packages/marqdo/harness/tests/knowledge.test.ts @@ -175,3 +175,44 @@ test('delete removes the copy and the collection metadata', async () => { assert.match(await readFile(source, 'utf8'), /删除/) await assert.rejects(readdir(knowledge.sourceRoot(collection.id)), /ENOENT/) }) + +test('a reviewed run becomes a static, searchable outcome without execution regions', async () => { + const { manager: knowledge } = await manager() + const saved = await knowledge.saveOutcome({ + title: '校正规则复用', + applicability: '非负读数 0..100,输入单位一致', + source: { + document: 'index.mq.md', + documentDigest: 'sha256:' + 'a'.repeat(64), + analysisDigest: 'sha256:' + 'b'.repeat(64), + functionName: '数学.校正', + runId: 'run-1', + evidenceId: 'run-1', + valueRender: '9', + mode: 'pure', + environmentId: 'cli-3.0.0-rc.6', + }, + }) + assert.equal(saved.collection.name, '运行成果') + assert.equal(saved.imported[0]?.relPath, 'outcome-run-1.md') + const source = join(knowledge.sourceRoot(saved.collection.id), 'outcome-run-1.md') + const text = await readFile(source, 'utf8') + assert.match(text, /## 适用条件/) + assert.match(text, /run-1/) + assert.match(text, /不授予任何执行权限/) + assert.doesNotMatch(text, /@exec/) + const found = await knowledge.search({ query: '校正' }) + assert.equal(found.hits.length, 1) + assert.equal(found.hits[0]?.collection, saved.collection.id) + await assert.rejects( + knowledge.saveOutcome({ + title: 'bad\nheading', + applicability: 'x', + source: { + document: 'a.mq.md', documentDigest: 'sha256:x', analysisDigest: 'sha256:y', + functionName: null, runId: 'r', evidenceId: 'r', valueRender: null, mode: 'pure', environmentId: null, + }, + }), + /one line/, + ) +})