@kangyishuai/dsh-casefile 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DISCLAIMER.md +24 -0
- package/LICENSE +21 -0
- package/README.md +158 -2
- package/README.zh-CN.md +159 -0
- package/cordis.patch.yml +3 -0
- package/lib/actions.js +1269 -0
- package/lib/client.js +1275 -0
- package/lib/core/attestation.js +95 -0
- package/lib/core/brief.js +253 -0
- package/lib/core/checks.js +436 -0
- package/lib/core/digest.js +122 -0
- package/lib/core/manifest.js +117 -0
- package/lib/core/model.js +390 -0
- package/lib/core/overview.js +80 -0
- package/lib/core/png.js +154 -0
- package/lib/core/report.js +316 -0
- package/lib/core/runs.js +211 -0
- package/lib/core/store.js +382 -0
- package/lib/index.js +5 -0
- package/lib/service.js +675 -0
- package/lib/tool-schema.js +38 -0
- package/lib/tools.js +658 -0
- package/locale/en.json +30 -0
- package/locale/zh.json +30 -0
- package/package.json +55 -4
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// core/attestation.js:净室 attestation —— 记录「builder 到底收到了什么、被要求了什么」。
|
|
3
|
+
//
|
|
4
|
+
// 为什么需要:DSH 的 agent preset 目前无法用 bundle 组合(见 docs/runtime/0.2.2-rc.3.md §12.7),
|
|
5
|
+
// 所以 builder 的工具面**没有**白名单隔离。我们能做到的是信息流隔离 + 可核对的声明:
|
|
6
|
+
// - 记录投喂给 builder 的 prompt 摘要(sha256)与规格包摘要;
|
|
7
|
+
// - 记录「要求它不要读取原件」这条指令确实在 prompt 里;
|
|
8
|
+
// - 把这些写进报告,让第三方能判断「这次复刻是净室做的吗」。
|
|
9
|
+
// 声明必须可验证,不能只是嘴上说说。
|
|
10
|
+
|
|
11
|
+
import { sha256Text, shortDigest } from "./digest.js";
|
|
12
|
+
import { nowIso } from "./model.js";
|
|
13
|
+
|
|
14
|
+
/** 净室指令的规范表述(prompt 里必须包含的片段)。 */
|
|
15
|
+
export const CLEAN_ROOM_CLAUSE = "不要读取或寻找原始目标";
|
|
16
|
+
|
|
17
|
+
/** 兼容英文表述(若有人用英文 prompt 覆盖)。 */
|
|
18
|
+
export const CLEAN_ROOM_CLAUSE_EN = "Do not read or look for the original target";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 生成一条 attestation。
|
|
22
|
+
* @param {{
|
|
23
|
+
* project: Record<string, any>,
|
|
24
|
+
* prompt: string,
|
|
25
|
+
* pack: Record<string, any>,
|
|
26
|
+
* sessionId: string,
|
|
27
|
+
* builderId: string,
|
|
28
|
+
* override?: boolean,
|
|
29
|
+
* reason?: string,
|
|
30
|
+
* }} input
|
|
31
|
+
* @returns {Record<string, any>}
|
|
32
|
+
*/
|
|
33
|
+
export function buildAttestation(input) {
|
|
34
|
+
const prompt = String(input.prompt ?? "");
|
|
35
|
+
const claims = (input.pack?.claims ?? []).map((/** @type {any} */ item) => item.id);
|
|
36
|
+
const evidence = (input.pack?.evidence ?? []).map((/** @type {any} */ item) => item.id);
|
|
37
|
+
const hasClause = prompt.includes(CLEAN_ROOM_CLAUSE) || prompt.includes(CLEAN_ROOM_CLAUSE_EN);
|
|
38
|
+
return {
|
|
39
|
+
id: `att-${input.builderId}`,
|
|
40
|
+
builderId: input.builderId,
|
|
41
|
+
sessionId: input.sessionId,
|
|
42
|
+
at: nowIso(),
|
|
43
|
+
authTier: String(input.project?.authTier ?? "research"),
|
|
44
|
+
researchOverride: input.override === true,
|
|
45
|
+
overrideReason: input.reason == null ? "" : String(input.reason).slice(0, 500),
|
|
46
|
+
// 可核对的事实
|
|
47
|
+
promptDigest: sha256Text(prompt),
|
|
48
|
+
specDigest: sha256Text(JSON.stringify(input.pack ?? {})),
|
|
49
|
+
claimCount: claims.length,
|
|
50
|
+
evidenceCount: evidence.length,
|
|
51
|
+
// 声明:我们要求了什么
|
|
52
|
+
cleanRoomInstruction: hasClause,
|
|
53
|
+
// 诚实的能力边界:DSH 层没有工具白名单
|
|
54
|
+
toolIsolation: "none: builder 使用宿主默认工具面;隔离靠只投喂规格包 + 本声明",
|
|
55
|
+
statement:
|
|
56
|
+
"本次构建只向 builder 投喂规格包(脱敏后的结论、证据摘要与未知项),未提供原件路径;" +
|
|
57
|
+
"不能保证 builder 未通过其它工具接触原件,故本声明不构成净室证明。",
|
|
58
|
+
shortPromptDigest: shortDigest(sha256Text(prompt)),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 从 attestation 列表里取最近一条。
|
|
64
|
+
* @param {Array<Record<string, any>>} attestations
|
|
65
|
+
* @returns {Record<string, any> | null}
|
|
66
|
+
*/
|
|
67
|
+
export function latestAttestation(attestations) {
|
|
68
|
+
const list = [...(attestations ?? [])].sort((a, b) =>
|
|
69
|
+
String(a.at ?? "").localeCompare(String(b.at ?? "")),
|
|
70
|
+
);
|
|
71
|
+
return list.at(-1) ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 报告用的净室小结。
|
|
76
|
+
* @param {Array<Record<string, any>>} attestations
|
|
77
|
+
* @returns {Record<string, any> | null}
|
|
78
|
+
*/
|
|
79
|
+
export function attestationSummary(attestations) {
|
|
80
|
+
const latest = latestAttestation(attestations);
|
|
81
|
+
if (!latest) return null;
|
|
82
|
+
return {
|
|
83
|
+
at: latest.at,
|
|
84
|
+
builderSession: latest.sessionId,
|
|
85
|
+
authTier: latest.authTier,
|
|
86
|
+
researchOverride: latest.researchOverride === true,
|
|
87
|
+
promptDigest: latest.shortPromptDigest ?? shortDigest(latest.promptDigest),
|
|
88
|
+
claimCount: latest.claimCount,
|
|
89
|
+
evidenceCount: latest.evidenceCount,
|
|
90
|
+
cleanRoomInstruction: latest.cleanRoomInstruction === true,
|
|
91
|
+
toolIsolation: latest.toolIsolation,
|
|
92
|
+
statement: latest.statement,
|
|
93
|
+
count: (attestations ?? []).length,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// core/brief.js:规格包(SpecPack)生成、脱敏与 Markdown 渲染。纯 Node,可单测。
|
|
3
|
+
//
|
|
4
|
+
// 产物①(证据结论)的交付物就是它:给另一个 agent / 另一个人的「去原始化」输入。
|
|
5
|
+
// 硬要求(spec/01-product.md §5、spec/03-acceptance.md §4):
|
|
6
|
+
// - 每条 claim 带 provenance / confidence / limitations;
|
|
7
|
+
// - 未知项必须随包导出,不能"整理"掉;
|
|
8
|
+
// - 默认脱敏:本机绝对路径、home、常见 token 形态。
|
|
9
|
+
|
|
10
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
|
|
13
|
+
import { sha256Text } from "./digest.js";
|
|
14
|
+
|
|
15
|
+
/** 规格包版本。 */
|
|
16
|
+
export const SPEC_VERSION = 1;
|
|
17
|
+
|
|
18
|
+
/** 常见 token 形态(宁可多遮,不可漏)。 */
|
|
19
|
+
const SECRET_PATTERNS = [
|
|
20
|
+
/\bsk-[A-Za-z0-9_-]{16,}\b/g,
|
|
21
|
+
/\bghp_[A-Za-z0-9]{20,}\b/g,
|
|
22
|
+
/\bgithub_pat_[A-Za-z0-9_]{20,}\b/g,
|
|
23
|
+
/\bAKIA[0-9A-Z]{16}\b/g,
|
|
24
|
+
/\bxox[abp]-[A-Za-z0-9-]{10,}\b/g,
|
|
25
|
+
/\bBearer\s+[A-Za-z0-9._-]{16,}\b/gi,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 脱敏一段文本。
|
|
30
|
+
* @param {string} text
|
|
31
|
+
* @param {{ home?: string, cwd?: string, extraPaths?: readonly string[] }} [options]
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
export function redactText(text, options = {}) {
|
|
35
|
+
let out = String(text);
|
|
36
|
+
const { home, cwd, extraPaths = [] } = options;
|
|
37
|
+
// 顺序很重要:先按「绝对路径整段」抹掉,再按已知根目录兜底替换。
|
|
38
|
+
// 反过来会先替换前缀,留下 `<path>/x.bin` 这种半截路径。
|
|
39
|
+
out = out.replace(/(?:\/Users|\/home|\/var|\/tmp|\/private)\/[^\s"'`)]+/g, "<path>");
|
|
40
|
+
out = out.replace(/[A-Za-z]:\\[^\s"'`)]+/g, "<path>");
|
|
41
|
+
/** @param {string | undefined} item @returns {item is string} */
|
|
42
|
+
const isPath = (item) => typeof item === "string" && item.length > 1;
|
|
43
|
+
const paths = [home, cwd, ...extraPaths].filter(isPath);
|
|
44
|
+
for (const path of paths.sort((a, b) => b.length - a.length)) {
|
|
45
|
+
out = out.split(path).join("<path>");
|
|
46
|
+
}
|
|
47
|
+
for (const pattern of SECRET_PATTERNS) out = out.replace(pattern, "[redacted]");
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 深拷贝并脱敏(只处理字符串值)。
|
|
53
|
+
* @param {any} value
|
|
54
|
+
* @param {{ home?: string, cwd?: string, extraPaths?: readonly string[] }} [options]
|
|
55
|
+
* @returns {any}
|
|
56
|
+
*/
|
|
57
|
+
export function redactPack(value, options = {}) {
|
|
58
|
+
if (typeof value === "string") return redactText(value, options);
|
|
59
|
+
if (Array.isArray(value)) return value.map((item) => redactPack(item, options));
|
|
60
|
+
if (value && typeof value === "object") {
|
|
61
|
+
/** @type {Record<string, any>} */
|
|
62
|
+
const out = {};
|
|
63
|
+
for (const [key, item] of Object.entries(value)) out[key] = redactPack(item, options);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 组装规格包。只包含「被选中的结论」及其证据、功能、未知项。
|
|
71
|
+
* @param {{
|
|
72
|
+
* project: Record<string, any>,
|
|
73
|
+
* evidence: Record<string, any>[],
|
|
74
|
+
* claims: Record<string, any>[],
|
|
75
|
+
* features: Record<string, any>[],
|
|
76
|
+
* unknowns: Record<string, any>[],
|
|
77
|
+
* generatedAt?: string,
|
|
78
|
+
* }} input
|
|
79
|
+
* @returns {Record<string, any>}
|
|
80
|
+
*/
|
|
81
|
+
export function buildSpecPack(input) {
|
|
82
|
+
const { project } = input;
|
|
83
|
+
const selectedClaims = input.claims.filter((claim) => claim.selected === true);
|
|
84
|
+
const evidenceIds = new Set(selectedClaims.flatMap((claim) => claim.evidenceIds ?? []));
|
|
85
|
+
const featureIds = new Set(selectedClaims.flatMap((claim) => claim.featureIds ?? []));
|
|
86
|
+
const features = input.features.filter((feature) => featureIds.has(feature.id));
|
|
87
|
+
const openUnknowns = input.unknowns.filter((unknown) => unknown.status !== "resolved");
|
|
88
|
+
|
|
89
|
+
const pack = {
|
|
90
|
+
version: SPEC_VERSION,
|
|
91
|
+
generatedAt: input.generatedAt ?? new Date().toISOString(),
|
|
92
|
+
project: {
|
|
93
|
+
id: project.id,
|
|
94
|
+
title: project.title,
|
|
95
|
+
target: project.target,
|
|
96
|
+
authTier: project.authTier,
|
|
97
|
+
scope: project.scope,
|
|
98
|
+
reaVersion: project.reaVersion,
|
|
99
|
+
capabilityHash: project.capabilityHash,
|
|
100
|
+
},
|
|
101
|
+
claims: selectedClaims.map((claim) => ({
|
|
102
|
+
id: claim.id,
|
|
103
|
+
text: claim.text,
|
|
104
|
+
kind: claim.kind,
|
|
105
|
+
status: claim.status,
|
|
106
|
+
limitations: claim.limitations ?? [],
|
|
107
|
+
evidenceIds: claim.evidenceIds ?? [],
|
|
108
|
+
featureIds: claim.featureIds ?? [],
|
|
109
|
+
})),
|
|
110
|
+
evidence: input.evidence
|
|
111
|
+
.filter((item) => evidenceIds.has(item.id))
|
|
112
|
+
.map((item) => ({
|
|
113
|
+
id: item.id,
|
|
114
|
+
kind: item.kind,
|
|
115
|
+
sourceTool: item.sourceTool,
|
|
116
|
+
observedAt: item.observedAt,
|
|
117
|
+
confidence: item.confidence,
|
|
118
|
+
authority: item.authority,
|
|
119
|
+
limitations: item.limitations ?? [],
|
|
120
|
+
integrity: item.integrity,
|
|
121
|
+
summary: item.summary,
|
|
122
|
+
refs: item.refs ?? [],
|
|
123
|
+
})),
|
|
124
|
+
features: features.map((feature) => ({
|
|
125
|
+
id: feature.id,
|
|
126
|
+
name: feature.name,
|
|
127
|
+
summary: feature.summary,
|
|
128
|
+
boundaries: feature.boundaries ?? [],
|
|
129
|
+
nonGoals: feature.nonGoals ?? [],
|
|
130
|
+
status: feature.status,
|
|
131
|
+
})),
|
|
132
|
+
unknowns: openUnknowns.map((unknown) => ({
|
|
133
|
+
id: unknown.id,
|
|
134
|
+
text: unknown.text,
|
|
135
|
+
severity: unknown.severity,
|
|
136
|
+
status: unknown.status,
|
|
137
|
+
})),
|
|
138
|
+
coverage: {
|
|
139
|
+
selectedClaims: selectedClaims.length,
|
|
140
|
+
totalClaims: input.claims.length,
|
|
141
|
+
evidence: evidenceIds.size,
|
|
142
|
+
features: features.length,
|
|
143
|
+
openUnknowns: openUnknowns.length,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
/** @type {any} */
|
|
147
|
+
const withIntegrity = pack;
|
|
148
|
+
withIntegrity.integrity = sha256Text(JSON.stringify({ ...pack, integrity: undefined }));
|
|
149
|
+
return withIntegrity;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 渲染成人/agent 都能读的 Markdown。
|
|
154
|
+
* @param {Record<string, any>} pack
|
|
155
|
+
* @returns {string}
|
|
156
|
+
*/
|
|
157
|
+
export function renderSpecMarkdown(pack) {
|
|
158
|
+
const lines = [];
|
|
159
|
+
lines.push(`# 规格包:${pack.project.title}`);
|
|
160
|
+
lines.push("");
|
|
161
|
+
lines.push(`- 项目:\`${pack.project.id}\``);
|
|
162
|
+
lines.push(`- 目标:${pack.project.target.kind} · ${pack.project.target.ref}`);
|
|
163
|
+
lines.push(`- 授权档:${pack.project.authTier}`);
|
|
164
|
+
lines.push(`- 范围:${pack.project.scope === "selected" ? "仅选中功能" : "全量"}`);
|
|
165
|
+
lines.push(`- 生成时间:${pack.generatedAt}`);
|
|
166
|
+
lines.push(`- 完整性:\`${String(pack.integrity).slice(0, 16)}\``);
|
|
167
|
+
lines.push(`- REA 版本:${pack.project.reaVersion ?? "未记录"}`);
|
|
168
|
+
lines.push("");
|
|
169
|
+
lines.push("## 选中的功能");
|
|
170
|
+
if (pack.features.length === 0) {
|
|
171
|
+
lines.push("");
|
|
172
|
+
lines.push("_没有功能分组:结论是散点,建议先在 casefile 里把它们归到功能。_");
|
|
173
|
+
}
|
|
174
|
+
for (const feature of pack.features) {
|
|
175
|
+
lines.push("");
|
|
176
|
+
lines.push(`### ${feature.name}`);
|
|
177
|
+
lines.push("");
|
|
178
|
+
if (feature.summary) lines.push(feature.summary, "");
|
|
179
|
+
if (feature.boundaries?.length) {
|
|
180
|
+
lines.push("**边界**:");
|
|
181
|
+
for (const item of feature.boundaries) lines.push(`- ${item}`);
|
|
182
|
+
lines.push("");
|
|
183
|
+
}
|
|
184
|
+
if (feature.nonGoals?.length) {
|
|
185
|
+
lines.push("**明确不做**:");
|
|
186
|
+
for (const item of feature.nonGoals) lines.push(`- ${item}`);
|
|
187
|
+
lines.push("");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
lines.push("");
|
|
191
|
+
lines.push("## 结论(每条带出处与局限)");
|
|
192
|
+
for (const claim of pack.claims) {
|
|
193
|
+
lines.push("");
|
|
194
|
+
lines.push(`### ${claim.text}`);
|
|
195
|
+
lines.push("");
|
|
196
|
+
lines.push(`- 种类:${claim.kind}`);
|
|
197
|
+
lines.push(`- 可信度:${claim.status}`);
|
|
198
|
+
lines.push(`- 证据:${claim.evidenceIds.length === 0 ? "无" : claim.evidenceIds.join("、")}`);
|
|
199
|
+
if (claim.limitations.length > 0) {
|
|
200
|
+
lines.push("- 局限:");
|
|
201
|
+
for (const item of claim.limitations) lines.push(` - ${item}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
lines.push("");
|
|
205
|
+
lines.push("## 证据");
|
|
206
|
+
for (const item of pack.evidence) {
|
|
207
|
+
lines.push("");
|
|
208
|
+
lines.push(`- \`${item.id}\` · ${item.kind} · ${item.confidence} / ${item.authority}`);
|
|
209
|
+
if (item.summary) lines.push(` - ${item.summary}`);
|
|
210
|
+
if (item.limitations.length > 0) lines.push(` - 局限:${item.limitations.join(";")}`);
|
|
211
|
+
if (item.refs.length > 0) lines.push(` - 引用:${item.refs.join("、")}`);
|
|
212
|
+
}
|
|
213
|
+
lines.push("");
|
|
214
|
+
lines.push("## 未知项(必须随包交付)");
|
|
215
|
+
if (pack.unknowns.length === 0) {
|
|
216
|
+
lines.push("");
|
|
217
|
+
lines.push("_没有未解决的未知项。_");
|
|
218
|
+
} else {
|
|
219
|
+
for (const unknown of pack.unknowns) {
|
|
220
|
+
lines.push(`- [${unknown.severity}] ${unknown.text}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
lines.push("");
|
|
224
|
+
lines.push("## 验收清单(交给 builder 前先补全)");
|
|
225
|
+
lines.push("");
|
|
226
|
+
lines.push("- [ ] 每条结论的行为/状态/异常/依赖/资源/界面六维已展开(缺项标 UNKNOWN)");
|
|
227
|
+
lines.push("- [ ] 每个功能有可判的验收断言");
|
|
228
|
+
lines.push("- [ ] 未解决的未知项已确认或显式接受");
|
|
229
|
+
lines.push("");
|
|
230
|
+
return lines.join("\n");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 写规格包到项目目录:`spec/<timestamp>/{spec.json,brief.md}`。
|
|
235
|
+
* @param {{
|
|
236
|
+
* projectDir: string,
|
|
237
|
+
* pack: Record<string, any>,
|
|
238
|
+
* redact?: boolean,
|
|
239
|
+
* redactOptions?: { home?: string, cwd?: string, extraPaths?: readonly string[] },
|
|
240
|
+
* }} input
|
|
241
|
+
* @returns {{ dir: string, files: string[] }}
|
|
242
|
+
*/
|
|
243
|
+
export function writeSpecPack(input) {
|
|
244
|
+
const stamp = String(input.pack.generatedAt).replace(/[:.]/g, "-");
|
|
245
|
+
const dir = join(input.projectDir, "spec", stamp);
|
|
246
|
+
mkdirSync(dir, { recursive: true });
|
|
247
|
+
const pack = input.redact === false ? input.pack : redactPack(input.pack, input.redactOptions);
|
|
248
|
+
const specFile = join(dir, "spec.json");
|
|
249
|
+
const briefFile = join(dir, "brief.md");
|
|
250
|
+
writeFileSync(specFile, `${JSON.stringify(pack, null, 2)}\n`, "utf8");
|
|
251
|
+
writeFileSync(briefFile, `${renderSpecMarkdown(pack)}\n`, "utf8");
|
|
252
|
+
return { dir, files: [specFile, briefFile] };
|
|
253
|
+
}
|