@kangyishuai/dsh-casefile 0.5.1 → 0.7.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/README.md +6 -3
- package/README.zh-CN.md +6 -3
- package/lib/actions.js +296 -7
- package/lib/client.js +16 -0
- package/lib/core/autonomy.js +143 -0
- package/lib/core/bundle.js +136 -0
- package/lib/core/checks.js +56 -5
- package/lib/core/ingest.js +213 -0
- package/lib/core/model.js +2 -0
- package/lib/core/png.js +33 -0
- package/lib/core/report.js +142 -0
- package/lib/core/store.js +7 -0
- package/lib/service.js +90 -8
- package/lib/tools.js +103 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -138,12 +138,15 @@ Config is read from the `casefile` row in `cordis.patch.yml`. Do not edit the pr
|
|
|
138
138
|
|
|
139
139
|
## Compatibility
|
|
140
140
|
|
|
141
|
-
| Plugin
|
|
142
|
-
|
|
|
143
|
-
| 0.1.x
|
|
141
|
+
| Plugin | Verified on DSH | What was run |
|
|
142
|
+
| ----------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| 0.1.x–0.2.x | 0.2.2-rc.3 (macOS, desktop + web) | 138 → 177 unit tests; 52-assertion web end-to-end smoke on a throwaway DSH home; **14-assertion desktop read-only regression on a real Electron renderer (CDP)**; `pnpm gate casefile` 4/4 steps green. |
|
|
144
|
+
| 0.3.x–0.5.x | same | 172 → 177 unit tests; adds `case_access` (sign-in wait), `case_app` (app-bundle first hop), artifact archiving, and project `archive`/`drop`; **exercised on 8 real projects** (local macOS app, sign-in-gated page, external-collector ingestion). |
|
|
144
145
|
|
|
145
146
|
Host APIs used here were verified by runtime inspection; the findings are recorded in `docs/runtime/0.2.2-rc.3.md` §10 (including two pitfalls: a missed `declareRemote` silently 404s only that endpoint, and `agentPresets` cannot be composed from bundle names).
|
|
146
147
|
|
|
148
|
+
Four target kinds were exercised in real tasks: **a local macOS app bundle** (Info.plist, signature surface, binary inventory — the binary layer needs REA + Ghidra, see `docs/runtime/0.2.2-rc.3.md` §13–§14), **a sign-in-gated web page** (`case_access await-login`, structure-only capture), **external-collector artifacts** (`producer` + `artifactPath` archiving), and **an app's own release package** (survey → spec → acceptance, closed end to end).
|
|
149
|
+
|
|
147
150
|
## Side effects & risks
|
|
148
151
|
|
|
149
152
|
- **Files it writes:** only under the data root above. It never writes into the investigated project, never modifies the REA binary, and never edits the profile.
|
package/README.zh-CN.md
CHANGED
|
@@ -138,12 +138,15 @@ casefile/
|
|
|
138
138
|
|
|
139
139
|
## 兼容性
|
|
140
140
|
|
|
141
|
-
| 插件
|
|
142
|
-
|
|
|
143
|
-
| 0.1.x | 0.2.2-rc.3(macOS,桌面版 + web) | 138 项单元测试;临时 DSH_HOME 上 52 项 web 端到端冒烟;**真实 Electron 渲染进程上 14 项桌面只读回归(CDP)**;`pnpm gate casefile` 4/4 步全绿。
|
|
141
|
+
| 插件 | 已在哪个 DSH 上验证 | 跑了什么 |
|
|
142
|
+
| ----------- | --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
143
|
+
| 0.1.x–0.2.x | 0.2.2-rc.3(macOS,桌面版 + web) | 138 → 177 项单元测试;临时 DSH_HOME 上 52 项 web 端到端冒烟;**真实 Electron 渲染进程上 14 项桌面只读回归(CDP)**;`pnpm gate casefile` 4/4 步全绿。 |
|
|
144
|
+
| 0.3.x–0.5.x | 同上 | 172 → 177 项单元测试;新增 `case_access`(等登录)、`case_app`(应用包第一跳)、产物归档、项目 `archive`/`drop`;**在 8 个真实项目上跑通**(含本机 macOS 应用、登录态网页、外部采集器摄入)。 |
|
|
144
145
|
|
|
145
146
|
本插件用到的宿主 API 都经过运行时勘察,结论记在 `docs/runtime/0.2.2-rc.3.md` §10(含两个坑:漏一条 `declareRemote` 只会让那一个端点静默 404;`agentPresets` 不能用 bundle 名组合)。
|
|
146
147
|
|
|
148
|
+
真实任务里验证过的四类目标:**本机 macOS 应用包**(Info.plist/签名面/二进制库存,二进制层需 REA + Ghidra,见 `docs/runtime/0.2.2-rc.3.md` §13–§14)、**登录态网页**(`case_access await-login` + 只取结构)、**外部采集器产物**(`producer` + `artifactPath` 归档)、**应用自身发布包**(mas 包勘察 → 规格 → 验收闭环)。
|
|
149
|
+
|
|
147
150
|
## 副作用与风险
|
|
148
151
|
|
|
149
152
|
- **写哪些文件:** 只写上面的数据根。不写被调查的项目、不改 REA 二进制、不编辑 profile。
|
package/lib/actions.js
CHANGED
|
@@ -8,9 +8,17 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import { parseArchitectures, summarizeAppBundle } from "./core/app-bundle.js";
|
|
10
10
|
import { buildAttestation } from "./core/attestation.js";
|
|
11
|
+
import {
|
|
12
|
+
evaluateBudget,
|
|
13
|
+
evaluateUnlock,
|
|
14
|
+
normalizeAutonomy,
|
|
15
|
+
normalizeBudget,
|
|
16
|
+
} from "./core/autonomy.js";
|
|
11
17
|
import { buildSpecPack, redactPack, renderSpecMarkdown, writeSpecPack } from "./core/brief.js";
|
|
18
|
+
import { buildBundle } from "./core/bundle.js";
|
|
12
19
|
import { createCheckers } from "./core/checks.js";
|
|
13
20
|
import { hashTree, shortDigest } from "./core/digest.js";
|
|
21
|
+
import { validateIngestPayload } from "./core/ingest.js";
|
|
14
22
|
import { awaitLogin } from "./core/login-wait.js";
|
|
15
23
|
import { gradeCapabilities } from "./core/manifest.js";
|
|
16
24
|
import {
|
|
@@ -29,6 +37,7 @@ import {
|
|
|
29
37
|
buildReport,
|
|
30
38
|
computeCoverage,
|
|
31
39
|
evaluateClose,
|
|
40
|
+
renderReportHtml,
|
|
32
41
|
renderReportMarkdown,
|
|
33
42
|
} from "./core/report.js";
|
|
34
43
|
import {
|
|
@@ -196,6 +205,8 @@ export function createActions(options) {
|
|
|
196
205
|
const data = store.readAll(project.id);
|
|
197
206
|
return {
|
|
198
207
|
project,
|
|
208
|
+
// 面板要显示当前自主性级别(L3 是危险状态,必须看得见)
|
|
209
|
+
autonomy: normalizeAutonomy(project.autonomy),
|
|
199
210
|
stats: projectStats(project, data),
|
|
200
211
|
claims: data.claims,
|
|
201
212
|
evidence: data.evidence,
|
|
@@ -263,7 +274,8 @@ export function createActions(options) {
|
|
|
263
274
|
: null;
|
|
264
275
|
const record = normalizeEvidence({
|
|
265
276
|
...input,
|
|
266
|
-
|
|
277
|
+
// 外部摄入时,同一份报告可以同时是多条证据的出处:允许直接传入已归档对象。
|
|
278
|
+
artifact: archived ?? input?.artifact ?? null,
|
|
267
279
|
sessionId: input?.sessionId ?? sessionOf(exec) ?? null,
|
|
268
280
|
});
|
|
269
281
|
store.appendRecord(project.id, "evidence", record);
|
|
@@ -1146,6 +1158,32 @@ export function createActions(options) {
|
|
|
1146
1158
|
if (typeof command !== "string" || command.trim() === "") {
|
|
1147
1159
|
throw new CasefileInputError("invalid-input", "runCycle 需要 command(构建命令)");
|
|
1148
1160
|
}
|
|
1161
|
+
// L3 无人值守:预算与"任一失败即停"在循环前/后强制生效,不可由调用方放宽。
|
|
1162
|
+
const autonomy = normalizeAutonomy(project.autonomy);
|
|
1163
|
+
/** @type {{ maxRounds: number, maxBuilds: number, maxWallClockMs: number } | null} */
|
|
1164
|
+
let budget = null;
|
|
1165
|
+
if (autonomy.level === "L3") {
|
|
1166
|
+
budget = normalizeBudget(autonomy.budget);
|
|
1167
|
+
const existing = store.readAll(project.id).builds;
|
|
1168
|
+
const preStop = evaluateBudget({
|
|
1169
|
+
builds: existing,
|
|
1170
|
+
startedAt: autonomy.unlockedAt,
|
|
1171
|
+
budget,
|
|
1172
|
+
});
|
|
1173
|
+
if (preStop.stop) {
|
|
1174
|
+
store.audit(project.id, {
|
|
1175
|
+
actor: "agent",
|
|
1176
|
+
action: "autonomy/stop",
|
|
1177
|
+
target: project.id,
|
|
1178
|
+
detail: { reason: preStop.reason, ...preStop.detail },
|
|
1179
|
+
});
|
|
1180
|
+
throw Object.assign(new Error(`L3 停止:${preStop.reason}`), {
|
|
1181
|
+
code: "autonomy-stop",
|
|
1182
|
+
reason: preStop.reason,
|
|
1183
|
+
detail: preStop.detail,
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1149
1187
|
const loop = await this.advanceLoop(project.id, {
|
|
1150
1188
|
command,
|
|
1151
1189
|
cwd: input?.cwd,
|
|
@@ -1178,6 +1216,187 @@ export function createActions(options) {
|
|
|
1178
1216
|
return { ...overview, summary: summarizeOverview(overview) };
|
|
1179
1217
|
},
|
|
1180
1218
|
|
|
1219
|
+
// ---------- 外部报告摄入 ----------
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* 摄入外部 agent 的产出(通用契约)。只记录、不背书:authority=external。
|
|
1223
|
+
* @param {string} projectId
|
|
1224
|
+
* @param {any} payload
|
|
1225
|
+
* @param {{ dryRun?: boolean }} [options]
|
|
1226
|
+
* @returns {Promise<Record<string, any>>}
|
|
1227
|
+
*/
|
|
1228
|
+
async ingestReport(projectId, payload, options = {}) {
|
|
1229
|
+
const project = requireProject(projectId);
|
|
1230
|
+
const normalized = validateIngestPayload(payload);
|
|
1231
|
+
const dryRun = options?.dryRun === true;
|
|
1232
|
+
|
|
1233
|
+
// 报告本体若有路径,归档进项目目录(/tmp 会过期,过期就没法复算)
|
|
1234
|
+
const archived =
|
|
1235
|
+
normalized.source.reportPath == null
|
|
1236
|
+
? null
|
|
1237
|
+
: store.archiveArtifact(project.id, normalized.source.reportPath);
|
|
1238
|
+
|
|
1239
|
+
if (dryRun) {
|
|
1240
|
+
return {
|
|
1241
|
+
dryRun: true,
|
|
1242
|
+
source: normalized.source,
|
|
1243
|
+
wouldCreate: {
|
|
1244
|
+
evidence: normalized.evidence.length,
|
|
1245
|
+
claims: normalized.claims.length,
|
|
1246
|
+
unknowns: normalized.unknowns.length,
|
|
1247
|
+
artifact: archived,
|
|
1248
|
+
},
|
|
1249
|
+
summary: `预演:将写入 ${normalized.evidence.length} 证据 / ${normalized.claims.length} 结论 / ${normalized.unknowns.length} 未知项`,
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
const producer = {
|
|
1254
|
+
tool: normalized.source.tool,
|
|
1255
|
+
version: normalized.source.version,
|
|
1256
|
+
mode: normalized.source.mode,
|
|
1257
|
+
outputKind: "external-report",
|
|
1258
|
+
accessConfirmedByUser: true,
|
|
1259
|
+
confirmationNote: "外部 agent 产出,casefile 只记录与引用;其判断归原工具,casefile 不背书",
|
|
1260
|
+
};
|
|
1261
|
+
|
|
1262
|
+
/** @type {string[]} */
|
|
1263
|
+
const evidenceIds = [];
|
|
1264
|
+
for (const item of normalized.evidence) {
|
|
1265
|
+
const record = await this.addEvidence(project.id, {
|
|
1266
|
+
summary: item.summary,
|
|
1267
|
+
kind: item.kind,
|
|
1268
|
+
authority: item.authority,
|
|
1269
|
+
confidence: "observed",
|
|
1270
|
+
limitations: item.limitations,
|
|
1271
|
+
refs: item.refs,
|
|
1272
|
+
...(item.artifactPath == null ? {} : { artifactPath: item.artifactPath }),
|
|
1273
|
+
...(archived == null ? {} : { artifact: archived }),
|
|
1274
|
+
producer,
|
|
1275
|
+
});
|
|
1276
|
+
evidenceIds.push(record.id);
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/** @type {string[]} */
|
|
1280
|
+
const claimIds = [];
|
|
1281
|
+
for (const item of normalized.claims) {
|
|
1282
|
+
const record = await this.addClaim(project.id, {
|
|
1283
|
+
text: item.text,
|
|
1284
|
+
kind: item.kind,
|
|
1285
|
+
status: item.status,
|
|
1286
|
+
limitations: item.limitations,
|
|
1287
|
+
evidenceIds: item.evidenceIndexes.map(
|
|
1288
|
+
(/** @type {number} */ index) => evidenceIds[index],
|
|
1289
|
+
),
|
|
1290
|
+
});
|
|
1291
|
+
claimIds.push(record.id);
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/** @type {string[]} */
|
|
1295
|
+
const unknownIds = [];
|
|
1296
|
+
for (const item of normalized.unknowns) {
|
|
1297
|
+
const record = await this.addUnknown(project.id, {
|
|
1298
|
+
text: item.text,
|
|
1299
|
+
severity: item.severity,
|
|
1300
|
+
});
|
|
1301
|
+
unknownIds.push(record.id);
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
store.audit(project.id, {
|
|
1305
|
+
actor: "agent",
|
|
1306
|
+
action: "ingest/external-report",
|
|
1307
|
+
target: project.id,
|
|
1308
|
+
detail: {
|
|
1309
|
+
tool: producer.tool,
|
|
1310
|
+
version: producer.version,
|
|
1311
|
+
evidence: evidenceIds.length,
|
|
1312
|
+
claims: claimIds.length,
|
|
1313
|
+
unknowns: unknownIds.length,
|
|
1314
|
+
artifactDigest: archived?.sha256 ?? null,
|
|
1315
|
+
},
|
|
1316
|
+
});
|
|
1317
|
+
|
|
1318
|
+
return {
|
|
1319
|
+
source: normalized.source,
|
|
1320
|
+
evidenceIds,
|
|
1321
|
+
claimIds,
|
|
1322
|
+
unknownIds: unknownIds.filter((item) => item !== ""),
|
|
1323
|
+
artifact: archived,
|
|
1324
|
+
summary: `已摄入 ${evidenceIds.length} 证据 / ${claimIds.length} 结论 / ${unknownIds.length} 未知项(来源 ${producer.tool})`,
|
|
1325
|
+
};
|
|
1326
|
+
},
|
|
1327
|
+
|
|
1328
|
+
// ---------- 自主性阶梯 ----------
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* 解锁 / 降级自主性。L3 有硬条件,不满足就拒绝并列出 blocker。
|
|
1332
|
+
* @param {string} projectId
|
|
1333
|
+
* @param {{ level?: string, budget?: Record<string, any> | null }} input
|
|
1334
|
+
* @returns {Promise<Record<string, any>>}
|
|
1335
|
+
*/
|
|
1336
|
+
async setAutonomy(projectId, input) {
|
|
1337
|
+
const project = requireProject(projectId);
|
|
1338
|
+
const level = String(input?.level ?? "L3");
|
|
1339
|
+
if (!["L0", "L1", "L2", "L3"].includes(level)) {
|
|
1340
|
+
throw new CasefileInputError("invalid-input", `未知级别:${level}`);
|
|
1341
|
+
}
|
|
1342
|
+
if (level !== "L3") {
|
|
1343
|
+
// 降级永远允许且立即生效:这是"停"的开关。
|
|
1344
|
+
const next = store.updateProject(project.id, {
|
|
1345
|
+
autonomy: { level, unlockedAt: null, budget: null },
|
|
1346
|
+
});
|
|
1347
|
+
store.audit(project.id, {
|
|
1348
|
+
actor: "agent",
|
|
1349
|
+
action: "autonomy/lock",
|
|
1350
|
+
target: project.id,
|
|
1351
|
+
detail: { level },
|
|
1352
|
+
});
|
|
1353
|
+
return { project: next, autonomy: normalizeAutonomy(next.autonomy) };
|
|
1354
|
+
}
|
|
1355
|
+
const data = store.readAll(project.id);
|
|
1356
|
+
const unlock = evaluateUnlock({
|
|
1357
|
+
project,
|
|
1358
|
+
obligations: data.obligations,
|
|
1359
|
+
budget: input?.budget,
|
|
1360
|
+
});
|
|
1361
|
+
if (!unlock.ok) {
|
|
1362
|
+
throw Object.assign(new Error(`L3 解锁被拒绝:${unlock.blockers.join("、")}`), {
|
|
1363
|
+
code: "autonomy-blocked",
|
|
1364
|
+
blockers: unlock.blockers,
|
|
1365
|
+
});
|
|
1366
|
+
}
|
|
1367
|
+
const next = store.updateProject(project.id, {
|
|
1368
|
+
autonomy: {
|
|
1369
|
+
level: "L3",
|
|
1370
|
+
unlockedAt: nowIso(),
|
|
1371
|
+
budget: unlock.budget,
|
|
1372
|
+
},
|
|
1373
|
+
});
|
|
1374
|
+
store.audit(project.id, {
|
|
1375
|
+
actor: "agent",
|
|
1376
|
+
action: "autonomy/unlock",
|
|
1377
|
+
target: project.id,
|
|
1378
|
+
detail: { level, budget: unlock.budget, obligations: data.obligations.length },
|
|
1379
|
+
});
|
|
1380
|
+
return { project: next, autonomy: normalizeAutonomy(next.autonomy) };
|
|
1381
|
+
},
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* 当前自主性状态(含 L3 前置条件检查,便于面板展示"为什么不能升")。
|
|
1385
|
+
* @param {string} projectId
|
|
1386
|
+
* @returns {Promise<Record<string, any>>}
|
|
1387
|
+
*/
|
|
1388
|
+
async autonomyStatus(projectId) {
|
|
1389
|
+
const project = requireProject(projectId);
|
|
1390
|
+
const data = store.readAll(project.id);
|
|
1391
|
+
const autonomy = normalizeAutonomy(project.autonomy);
|
|
1392
|
+
const unlock = evaluateUnlock({
|
|
1393
|
+
project,
|
|
1394
|
+
obligations: data.obligations,
|
|
1395
|
+
budget: autonomy.budget,
|
|
1396
|
+
});
|
|
1397
|
+
return { autonomy, unlock: { ok: unlock.ok, blockers: unlock.blockers } };
|
|
1398
|
+
},
|
|
1399
|
+
|
|
1181
1400
|
// ---------- 等登录(人机协作) ----------
|
|
1182
1401
|
|
|
1183
1402
|
/**
|
|
@@ -1369,7 +1588,17 @@ export function createActions(options) {
|
|
|
1369
1588
|
* @param {string} projectId
|
|
1370
1589
|
* @returns {Promise<Record<string, any>>}
|
|
1371
1590
|
*/
|
|
1372
|
-
|
|
1591
|
+
/**
|
|
1592
|
+
* 导出报告。
|
|
1593
|
+
* @param {string} projectId
|
|
1594
|
+
* @param {{ format?: "md" | "html" | "both" }} [options]
|
|
1595
|
+
* @returns {Promise<Record<string, any>>}
|
|
1596
|
+
*/
|
|
1597
|
+
async exportReport(projectId, options = {}) {
|
|
1598
|
+
const format = options?.format ?? "md";
|
|
1599
|
+
if (!["md", "html", "both"].includes(format)) {
|
|
1600
|
+
throw new CasefileInputError("invalid-input", `format 只能是 md / html / both:${format}`);
|
|
1601
|
+
}
|
|
1373
1602
|
const project = requireProject(projectId);
|
|
1374
1603
|
const data = store.readAll(project.id);
|
|
1375
1604
|
const current = latestBuild(data.builds);
|
|
@@ -1382,11 +1611,21 @@ export function createActions(options) {
|
|
|
1382
1611
|
const report = buildReport({ project, data, coverage, stop });
|
|
1383
1612
|
const stamp = String(report.generatedAt).replace(/[:.]/g, "-");
|
|
1384
1613
|
const dir = join(store.projectDir(project.id), "reports", stamp);
|
|
1385
|
-
const md = join(dir, "report.md");
|
|
1386
|
-
const json = join(dir, "report.json");
|
|
1387
1614
|
mkdirSync(dir, { recursive: true });
|
|
1388
|
-
|
|
1389
|
-
|
|
1615
|
+
/** @type {string[]} */
|
|
1616
|
+
const files = [];
|
|
1617
|
+
if (format === "md" || format === "both") {
|
|
1618
|
+
writeFileSync(join(dir, "report.md"), `${renderReportMarkdown(report)}\n`, "utf8");
|
|
1619
|
+
files.push("report.md");
|
|
1620
|
+
}
|
|
1621
|
+
if (format === "html" || format === "both") {
|
|
1622
|
+
writeFileSync(join(dir, "report.html"), `${renderReportHtml(report)}\n`, "utf8");
|
|
1623
|
+
files.push("report.html");
|
|
1624
|
+
} else {
|
|
1625
|
+
// md 模式保持旧行为:仍然写 report.json 供程序消费
|
|
1626
|
+
}
|
|
1627
|
+
writeFileSync(join(dir, "report.json"), `${JSON.stringify(report, null, 2)}\n`, "utf8");
|
|
1628
|
+
files.push("report.json");
|
|
1390
1629
|
store.audit(project.id, {
|
|
1391
1630
|
actor: "agent",
|
|
1392
1631
|
action: "report/export",
|
|
@@ -1398,12 +1637,62 @@ export function createActions(options) {
|
|
|
1398
1637
|
});
|
|
1399
1638
|
return {
|
|
1400
1639
|
path: dir,
|
|
1401
|
-
files
|
|
1640
|
+
files,
|
|
1402
1641
|
coverage,
|
|
1403
1642
|
summary: `报告已写入 ${dir}(覆盖率 ${(coverage.ratio * 100).toFixed(0)}%)`,
|
|
1404
1643
|
};
|
|
1405
1644
|
},
|
|
1406
1645
|
|
|
1646
|
+
/**
|
|
1647
|
+
* 导出机器可读证据包(全量账本,确定性、可校验)。
|
|
1648
|
+
* @param {string} projectId
|
|
1649
|
+
* @param {{ redact?: boolean }} [options]
|
|
1650
|
+
* @returns {Promise<Record<string, any>>}
|
|
1651
|
+
*/
|
|
1652
|
+
async exportBundle(projectId, options = {}) {
|
|
1653
|
+
const project = requireProject(projectId);
|
|
1654
|
+
const data = store.readAll(project.id);
|
|
1655
|
+
const coverage = computeCoverage(data.obligations);
|
|
1656
|
+
const build = buildBundle({
|
|
1657
|
+
project,
|
|
1658
|
+
evidence: data.evidence,
|
|
1659
|
+
claims: data.claims,
|
|
1660
|
+
obligations: data.obligations,
|
|
1661
|
+
checks: data.checks,
|
|
1662
|
+
unknowns: data.unknowns,
|
|
1663
|
+
coverage,
|
|
1664
|
+
counts: {
|
|
1665
|
+
evidence: data.evidence.length,
|
|
1666
|
+
claims: data.claims.length,
|
|
1667
|
+
obligations: data.obligations.length,
|
|
1668
|
+
checks: data.checks.length,
|
|
1669
|
+
unknowns: data.unknowns.length,
|
|
1670
|
+
},
|
|
1671
|
+
redact: options?.redact !== false,
|
|
1672
|
+
});
|
|
1673
|
+
const stamp = String(build.generatedAt).replace(/[:.]/g, "-");
|
|
1674
|
+
const dir = join(store.projectDir(project.id), "bundles", stamp);
|
|
1675
|
+
mkdirSync(dir, { recursive: true });
|
|
1676
|
+
const file = join(dir, "bundle.json");
|
|
1677
|
+
writeFileSync(file, `${JSON.stringify(build, null, 2)}\n`, "utf8");
|
|
1678
|
+
store.audit(project.id, {
|
|
1679
|
+
actor: "agent",
|
|
1680
|
+
action: "bundle/export",
|
|
1681
|
+
target: dir,
|
|
1682
|
+
detail: {
|
|
1683
|
+
digest: build.integrity.digest.slice(0, 16),
|
|
1684
|
+
redacted: options?.redact !== false,
|
|
1685
|
+
},
|
|
1686
|
+
});
|
|
1687
|
+
return {
|
|
1688
|
+
path: dir,
|
|
1689
|
+
files: ["bundle.json"],
|
|
1690
|
+
integrity: build.integrity.digest,
|
|
1691
|
+
generatedAt: build.generatedAt,
|
|
1692
|
+
summary: `证据包已写入 ${dir}(integrity ${build.integrity.digest.slice(0, 16)})`,
|
|
1693
|
+
};
|
|
1694
|
+
},
|
|
1695
|
+
|
|
1407
1696
|
// ---------- 规格包 ----------
|
|
1408
1697
|
|
|
1409
1698
|
/**
|
package/lib/client.js
CHANGED
|
@@ -83,6 +83,9 @@ window.__ModuleLoader__.load({
|
|
|
83
83
|
"blocker.failedObligations": "有失败的义务",
|
|
84
84
|
"blocker.blockedObligations": "有阻塞的义务",
|
|
85
85
|
"blocker.coverageBelow": "覆盖率不足",
|
|
86
|
+
"autonomy.label": "自主性",
|
|
87
|
+
"autonomy.hint":
|
|
88
|
+
"L3 无人值守仅对 scope=selected 且义务全带检查配方的项目开放;任一失败即停。",
|
|
86
89
|
"wait.title": "案卷:等待登录",
|
|
87
90
|
"wait.hint": "请在弹出的浏览器窗口里完成登录;就绪后会自动继续。",
|
|
88
91
|
"wait.elapsed": "已等待 {seconds}s",
|
|
@@ -175,6 +178,9 @@ window.__ModuleLoader__.load({
|
|
|
175
178
|
"blocker.failedObligations": "failed obligations",
|
|
176
179
|
"blocker.blockedObligations": "blocked obligations",
|
|
177
180
|
"blocker.coverageBelow": "coverage below target",
|
|
181
|
+
"autonomy.label": "Autonomy",
|
|
182
|
+
"autonomy.hint":
|
|
183
|
+
"L3 unattended runs are limited to scope=selected projects whose obligations all carry check recipes; any failure stops the loop.",
|
|
178
184
|
"wait.title": "Casefile: waiting for sign-in",
|
|
179
185
|
"wait.hint":
|
|
180
186
|
"Finish signing in in the browser window; it continues automatically once ready.",
|
|
@@ -1096,6 +1102,16 @@ window.__ModuleLoader__.load({
|
|
|
1096
1102
|
h("h2", null, text(t, "panel.title")),
|
|
1097
1103
|
h("span", { className: "cf-sub" }, text(t, "panel.subtitle")),
|
|
1098
1104
|
h("span", { className: "cf-grow" }),
|
|
1105
|
+
h(
|
|
1106
|
+
"span",
|
|
1107
|
+
{
|
|
1108
|
+
className: "cf-muted",
|
|
1109
|
+
"data-casefile-autonomy": detail?.autonomy?.level || "L2",
|
|
1110
|
+
title: text(t, "autonomy.hint"),
|
|
1111
|
+
style: { marginRight: 8 },
|
|
1112
|
+
},
|
|
1113
|
+
`${text(t, "autonomy.label")} ${detail?.autonomy?.level || "L2"}`,
|
|
1114
|
+
),
|
|
1099
1115
|
h(
|
|
1100
1116
|
"button",
|
|
1101
1117
|
{
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// core/autonomy.js:自主性阶梯(L0–L3)的解锁与预算判定。
|
|
3
|
+
//
|
|
4
|
+
// 为什么要有这一层:`runCycle` 已经能自动跑"构建→检查→关闭义务",但**谁能让它
|
|
5
|
+
// 无人值守地跑**必须有硬条件,否则就是"全自动复刻"——那是明确不做的非目标。
|
|
6
|
+
//
|
|
7
|
+
// 硬规则(写死,不可配置放宽):
|
|
8
|
+
// 1. 只有 `scope: "selected"` 的项目能升到 L3;
|
|
9
|
+
// 2. 只有 existence / structure / behavior / visual 四类义务允许 L3;
|
|
10
|
+
// `coverage`(覆盖率硬门)与 `regression`(回归门)永远人工复核;
|
|
11
|
+
// 3. 每条义务必须有**可执行的检查配方**(`check`),否则"没人验"会被自动跑成绿灯;
|
|
12
|
+
// 4. 必须有预算上限;预算耗尽或任一轮 fail/unknown 立即停。
|
|
13
|
+
|
|
14
|
+
export const AUTONOMY_LEVELS = /** @type {const} */ (["L0", "L1", "L2", "L3"]);
|
|
15
|
+
|
|
16
|
+
/** L3 允许自动关闭的义务种类。 */
|
|
17
|
+
export const L3_ALLOWED_KINDS = /** @type {const} */ ([
|
|
18
|
+
"existence",
|
|
19
|
+
"structure",
|
|
20
|
+
"behavior",
|
|
21
|
+
"visual",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
export const AUTONOMY_DEFAULT_BUDGET = {
|
|
25
|
+
maxRounds: 5,
|
|
26
|
+
maxBuilds: 20,
|
|
27
|
+
maxWallClockMs: 30 * 60 * 1000,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 归一化预算:缺项用默认值,非法值报错。
|
|
32
|
+
* @param {Record<string, any> | null | undefined} raw
|
|
33
|
+
* @returns {{ maxRounds: number, maxBuilds: number, maxWallClockMs: number }}
|
|
34
|
+
*/
|
|
35
|
+
export function normalizeBudget(raw) {
|
|
36
|
+
/** @param {"maxRounds" | "maxBuilds" | "maxWallClockMs"} key */
|
|
37
|
+
const pick = (key) => {
|
|
38
|
+
const value = raw?.[key];
|
|
39
|
+
if (value == null) return AUTONOMY_DEFAULT_BUDGET[key];
|
|
40
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
41
|
+
throw Object.assign(new Error(`预算 ${key} 必须是正数`), { code: "invalid-input" });
|
|
42
|
+
}
|
|
43
|
+
return Math.floor(value);
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
maxRounds: pick("maxRounds"),
|
|
47
|
+
maxBuilds: pick("maxBuilds"),
|
|
48
|
+
maxWallClockMs: pick("maxWallClockMs"),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 判断能否解锁到 L3。
|
|
54
|
+
* @param {{
|
|
55
|
+
* project: Record<string, any>,
|
|
56
|
+
* obligations: Record<string, any>[],
|
|
57
|
+
* budget?: Record<string, any> | null,
|
|
58
|
+
* }} input
|
|
59
|
+
* @returns {{ ok: boolean, blockers: string[], budget: { maxRounds: number, maxBuilds: number, maxWallClockMs: number } }}
|
|
60
|
+
*/
|
|
61
|
+
export function evaluateUnlock(input) {
|
|
62
|
+
/** @type {string[]} */
|
|
63
|
+
const blockers = [];
|
|
64
|
+
const project = input.project ?? {};
|
|
65
|
+
if (project.scope !== "selected") {
|
|
66
|
+
blockers.push("scope-not-selected");
|
|
67
|
+
}
|
|
68
|
+
const obligations = Array.isArray(input.obligations) ? input.obligations : [];
|
|
69
|
+
if (obligations.length === 0) {
|
|
70
|
+
blockers.push("no-obligations");
|
|
71
|
+
}
|
|
72
|
+
const disallowed = obligations.filter((item) => !L3_ALLOWED_KINDS.includes(item.kind));
|
|
73
|
+
if (disallowed.length > 0) {
|
|
74
|
+
blockers.push(
|
|
75
|
+
`kind-not-allowed:${[...new Set(disallowed.map((item) => item.kind))].join(",")}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
const withoutCheck = obligations.filter((item) => item.check == null);
|
|
79
|
+
if (withoutCheck.length > 0) {
|
|
80
|
+
blockers.push(`obligation-without-check:${withoutCheck.length}`);
|
|
81
|
+
}
|
|
82
|
+
if (project.state === "archived") {
|
|
83
|
+
blockers.push("project-archived");
|
|
84
|
+
}
|
|
85
|
+
let budget = AUTONOMY_DEFAULT_BUDGET;
|
|
86
|
+
try {
|
|
87
|
+
budget = normalizeBudget(input.budget);
|
|
88
|
+
} catch {
|
|
89
|
+
blockers.push("invalid-budget");
|
|
90
|
+
}
|
|
91
|
+
return { ok: blockers.length === 0, blockers, budget };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 按预算判停。
|
|
96
|
+
* @param {{
|
|
97
|
+
* builds: Record<string, any>[],
|
|
98
|
+
* startedAt: string | null,
|
|
99
|
+
* nowMs?: number,
|
|
100
|
+
* budget: { maxRounds: number, maxBuilds: number, maxWallClockMs: number },
|
|
101
|
+
* lastVerdict?: string | null,
|
|
102
|
+
* }} input
|
|
103
|
+
* @returns {{ stop: boolean, reason: string | null, detail: Record<string, any> }}
|
|
104
|
+
*/
|
|
105
|
+
export function evaluateBudget(input) {
|
|
106
|
+
const builds = Array.isArray(input.builds) ? input.builds : [];
|
|
107
|
+
const budget = input.budget ?? AUTONOMY_DEFAULT_BUDGET;
|
|
108
|
+
const nowMs = typeof input.nowMs === "number" ? input.nowMs : Date.now();
|
|
109
|
+
const startedMs = input.startedAt == null ? null : Date.parse(input.startedAt);
|
|
110
|
+
const elapsedMs =
|
|
111
|
+
startedMs == null || !Number.isFinite(startedMs) ? 0 : Math.max(0, nowMs - startedMs);
|
|
112
|
+
const detail = {
|
|
113
|
+
builds: builds.length,
|
|
114
|
+
budgetBuilds: budget.maxBuilds,
|
|
115
|
+
elapsedMs,
|
|
116
|
+
budgetMs: budget.maxWallClockMs,
|
|
117
|
+
};
|
|
118
|
+
// 任何一轮 fail/unknown 立即停:无人值守时"失败"必须交回给人,不能刷灯。
|
|
119
|
+
if (input.lastVerdict === "fail" || input.lastVerdict === "unknown") {
|
|
120
|
+
return { stop: true, reason: `l3-${input.lastVerdict}`, detail };
|
|
121
|
+
}
|
|
122
|
+
if (builds.length >= budget.maxBuilds) {
|
|
123
|
+
return { stop: true, reason: "l3-budget-builds", detail };
|
|
124
|
+
}
|
|
125
|
+
if (elapsedMs >= budget.maxWallClockMs) {
|
|
126
|
+
return { stop: true, reason: "l3-budget-time", detail };
|
|
127
|
+
}
|
|
128
|
+
return { stop: false, reason: null, detail };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 归一化项目上的 autonomy 字段。
|
|
133
|
+
* @param {Record<string, any> | null | undefined} raw
|
|
134
|
+
* @returns {{ level: "L0" | "L1" | "L2" | "L3", unlockedAt: string | null, budget: { maxRounds: number, maxBuilds: number, maxWallClockMs: number } }}
|
|
135
|
+
*/
|
|
136
|
+
export function normalizeAutonomy(raw) {
|
|
137
|
+
const level = raw && AUTONOMY_LEVELS.includes(raw.level) ? raw.level : "L2";
|
|
138
|
+
return {
|
|
139
|
+
level,
|
|
140
|
+
unlockedAt: typeof raw?.unlockedAt === "string" ? raw.unlockedAt : null,
|
|
141
|
+
budget: normalizeBudget(raw?.budget ?? null),
|
|
142
|
+
};
|
|
143
|
+
}
|