@kuafucloud/repo-kit 0.0.0-stage → 0.2.2-alpha.4

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.
Files changed (212) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +6 -2
  3. package/assets/git-hooks/check-push.cjs +21 -0
  4. package/assets/git-hooks/pre-push +7 -0
  5. package/assets/git-hooks/prepare-commit-msg +5 -0
  6. package/assets/verification/heavy-test-admission-core.d.mts +1 -0
  7. package/assets/verification/heavy-test-admission-core.mjs +10 -0
  8. package/assets/verification/host-process-lock.d.mts +24 -0
  9. package/assets/verification/host-process-lock.mjs +140 -0
  10. package/assets/verification/test-heavy-admission.d.mts +4 -0
  11. package/assets/verification/test-heavy-admission.mjs +34 -0
  12. package/bin/kuafu-guard.mjs +3 -0
  13. package/bin/kuafu-verify.mjs +3 -0
  14. package/dist/cli/guard.d.ts +1 -0
  15. package/dist/cli/guard.js +59 -0
  16. package/dist/cli/verify.d.ts +1 -0
  17. package/dist/cli/verify.js +58 -0
  18. package/dist/guard/core/config.d.ts +7 -0
  19. package/dist/guard/core/config.js +159 -0
  20. package/dist/guard/core/engine.d.ts +2 -0
  21. package/dist/guard/core/engine.js +151 -0
  22. package/dist/guard/core/formatters/sarif-rules.d.ts +2 -0
  23. package/dist/guard/core/formatters/sarif-rules.js +67 -0
  24. package/dist/guard/core/formatters/sarif.d.ts +2 -0
  25. package/dist/guard/core/formatters/sarif.js +3 -0
  26. package/dist/guard/core/formatters/text.d.ts +2 -0
  27. package/dist/guard/core/formatters/text.js +22 -0
  28. package/dist/guard/core/index.d.ts +5 -0
  29. package/dist/guard/core/index.js +5 -0
  30. package/dist/guard/core/types.d.ts +94 -0
  31. package/dist/guard/core/types.js +1 -0
  32. package/dist/guard/hooks/install-rules.d.ts +2 -0
  33. package/dist/guard/hooks/install-rules.js +5 -0
  34. package/dist/guard/hooks/install.d.ts +36 -0
  35. package/dist/guard/hooks/install.js +212 -0
  36. package/dist/guard/hooks/push.d.ts +1 -0
  37. package/dist/guard/hooks/push.js +5 -0
  38. package/dist/guard/index.d.ts +16 -0
  39. package/dist/guard/index.js +16 -0
  40. package/dist/guard/product-roots.d.ts +12 -0
  41. package/dist/guard/product-roots.js +37 -0
  42. package/dist/guard/repo-args.d.ts +8 -0
  43. package/dist/guard/repo-args.js +75 -0
  44. package/dist/guard/repo-output-rules.d.ts +12 -0
  45. package/dist/guard/repo-output-rules.js +44 -0
  46. package/dist/guard/repo.d.ts +21 -0
  47. package/dist/guard/repo.js +74 -0
  48. package/dist/guard/rules/external/api/collect-orphan-tests.d.ts +2 -0
  49. package/dist/guard/rules/external/api/collect-orphan-tests.js +34 -0
  50. package/dist/guard/rules/external/api/collect.d.ts +2 -0
  51. package/dist/guard/rules/external/api/collect.js +419 -0
  52. package/dist/guard/rules/external/api/validate-orphan-tests.d.ts +2 -0
  53. package/dist/guard/rules/external/api/validate-orphan-tests.js +10 -0
  54. package/dist/guard/rules/external/api/validate.d.ts +2 -0
  55. package/dist/guard/rules/external/api/validate.js +278 -0
  56. package/dist/guard/rules/external/app/native-source-rules.d.ts +6 -0
  57. package/dist/guard/rules/external/app/native-source-rules.js +164 -0
  58. package/dist/guard/rules/external/app/validate-native-source-boundary.d.ts +2 -0
  59. package/dist/guard/rules/external/app/validate-native-source-boundary.js +187 -0
  60. package/dist/guard/rules/external/boundary/check.d.ts +4 -0
  61. package/dist/guard/rules/external/boundary/check.js +198 -0
  62. package/dist/guard/rules/external/extension/collect.d.ts +1 -0
  63. package/dist/guard/rules/external/extension/collect.js +1 -0
  64. package/dist/guard/rules/external/extension/electron-boundary-rules.d.ts +6 -0
  65. package/dist/guard/rules/external/extension/electron-boundary-rules.js +10 -0
  66. package/dist/guard/rules/external/extension/index.d.ts +6 -0
  67. package/dist/guard/rules/external/extension/index.js +29 -0
  68. package/dist/guard/rules/external/extension/manual-cross-call-rules.d.ts +19 -0
  69. package/dist/guard/rules/external/extension/manual-cross-call-rules.js +19 -0
  70. package/dist/guard/rules/external/extension/resolve-import.d.ts +1 -0
  71. package/dist/guard/rules/external/extension/resolve-import.js +43 -0
  72. package/dist/guard/rules/external/extension/scan-relative-dependencies.d.ts +5 -0
  73. package/dist/guard/rules/external/extension/scan-relative-dependencies.js +53 -0
  74. package/dist/guard/rules/external/extension/scan-source.d.ts +11 -0
  75. package/dist/guard/rules/external/extension/scan-source.js +25 -0
  76. package/dist/guard/rules/external/extension/source-import-boundary-rules.d.ts +26 -0
  77. package/dist/guard/rules/external/extension/source-import-boundary-rules.js +40 -0
  78. package/dist/guard/rules/external/extension/test-layer-import-rules.d.ts +1 -0
  79. package/dist/guard/rules/external/extension/test-layer-import-rules.js +16 -0
  80. package/dist/guard/rules/external/extension/types.d.ts +51 -0
  81. package/dist/guard/rules/external/extension/types.js +1 -0
  82. package/dist/guard/rules/external/extension/unused-export-rules.d.ts +23 -0
  83. package/dist/guard/rules/external/extension/unused-export-rules.js +119 -0
  84. package/dist/guard/rules/external/extension/unused-module-rules.d.ts +6 -0
  85. package/dist/guard/rules/external/extension/unused-module-rules.js +90 -0
  86. package/dist/guard/rules/external/extension/validate-admin.d.ts +8 -0
  87. package/dist/guard/rules/external/extension/validate-admin.js +73 -0
  88. package/dist/guard/rules/external/extension/validate-database.d.ts +3 -0
  89. package/dist/guard/rules/external/extension/validate-database.js +4 -0
  90. package/dist/guard/rules/external/extension/validate-electron-boundary.d.ts +2 -0
  91. package/dist/guard/rules/external/extension/validate-electron-boundary.js +18 -0
  92. package/dist/guard/rules/external/extension/validate-empty-dirs.d.ts +9 -0
  93. package/dist/guard/rules/external/extension/validate-empty-dirs.js +44 -0
  94. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.d.ts +2 -0
  95. package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.js +116 -0
  96. package/dist/guard/rules/external/extension/validate-features.d.ts +7 -0
  97. package/dist/guard/rules/external/extension/validate-features.js +27 -0
  98. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.d.ts +2 -0
  99. package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.js +131 -0
  100. package/dist/guard/rules/external/extension/validate-peer-extension-import.d.ts +2 -0
  101. package/dist/guard/rules/external/extension/validate-peer-extension-import.js +41 -0
  102. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.d.ts +2 -0
  103. package/dist/guard/rules/external/extension/validate-server-cross-domain-import.js +48 -0
  104. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.d.ts +1 -0
  105. package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.js +1 -0
  106. package/dist/guard/rules/external/extension/validate-shared-import-boundary.d.ts +2 -0
  107. package/dist/guard/rules/external/extension/validate-shared-import-boundary.js +40 -0
  108. package/dist/guard/rules/external/extension/validate-shared.d.ts +1 -0
  109. package/dist/guard/rules/external/extension/validate-shared.js +1 -0
  110. package/dist/guard/rules/external/extension/validate-test-layer-boundary.d.ts +8 -0
  111. package/dist/guard/rules/external/extension/validate-test-layer-boundary.js +59 -0
  112. package/dist/guard/rules/external/extension/validate-test-mirror.d.ts +4 -0
  113. package/dist/guard/rules/external/extension/validate-test-mirror.js +32 -0
  114. package/dist/guard/rules/external/extension/validate-top-level.d.ts +7 -0
  115. package/dist/guard/rules/external/extension/validate-top-level.js +76 -0
  116. package/dist/guard/rules/external/extension/validate-unused-export.d.ts +2 -0
  117. package/dist/guard/rules/external/extension/validate-unused-export.js +187 -0
  118. package/dist/guard/rules/external/extension/validate-unused-module.d.ts +2 -0
  119. package/dist/guard/rules/external/extension/validate-unused-module.js +38 -0
  120. package/dist/guard/rules/external/index.d.ts +1 -0
  121. package/dist/guard/rules/external/index.js +1 -0
  122. package/dist/guard/rules/external/npm-dependency-policy.d.ts +14 -0
  123. package/dist/guard/rules/external/npm-dependency-policy.js +216 -0
  124. package/dist/guard/rules/external/rules.d.ts +2 -0
  125. package/dist/guard/rules/external/rules.js +222 -0
  126. package/dist/guard/rules/external/shared/config.d.ts +3 -0
  127. package/dist/guard/rules/external/shared/config.js +14 -0
  128. package/dist/guard/rules/external/shared/diagnostic.d.ts +4 -0
  129. package/dist/guard/rules/external/shared/diagnostic.js +12 -0
  130. package/dist/guard/rules/external/shared/records.d.ts +4 -0
  131. package/dist/guard/rules/external/shared/records.js +46 -0
  132. package/dist/guard/rules/external/shared/ts-ast.d.ts +8 -0
  133. package/dist/guard/rules/external/shared/ts-ast.js +103 -0
  134. package/dist/guard/rules/external/shared/types.d.ts +159 -0
  135. package/dist/guard/rules/external/shared/types.js +1 -0
  136. package/dist/guard/rules/external/shared/validate-layer-key.d.ts +2 -0
  137. package/dist/guard/rules/external/shared/validate-layer-key.js +35 -0
  138. package/dist/guard/rules/external/story-rules.d.ts +15 -0
  139. package/dist/guard/rules/external/story-rules.js +59 -0
  140. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.d.ts +7 -0
  141. package/dist/guard/rules/shared/extension/api-entry-tuple-rules.js +6 -0
  142. package/dist/guard/rules/shared/extension/collect.d.ts +5 -0
  143. package/dist/guard/rules/shared/extension/collect.js +231 -0
  144. package/dist/guard/rules/shared/extension/database-reference-rules.d.ts +4 -0
  145. package/dist/guard/rules/shared/extension/database-reference-rules.js +5 -0
  146. package/dist/guard/rules/shared/extension/database-source-rules.d.ts +17 -0
  147. package/dist/guard/rules/shared/extension/database-source-rules.js +595 -0
  148. package/dist/guard/rules/shared/extension/database-structure-rules.d.ts +3 -0
  149. package/dist/guard/rules/shared/extension/database-structure-rules.js +63 -0
  150. package/dist/guard/rules/shared/extension/server-index-source-rules.d.ts +2 -0
  151. package/dist/guard/rules/shared/extension/server-index-source-rules.js +91 -0
  152. package/dist/guard/rules/shared/extension/shared-consumer-rules.d.ts +23 -0
  153. package/dist/guard/rules/shared/extension/shared-consumer-rules.js +158 -0
  154. package/dist/guard/rules/shared/extension/shared-structure-rules.d.ts +6 -0
  155. package/dist/guard/rules/shared/extension/shared-structure-rules.js +27 -0
  156. package/dist/guard/rules/shared/extension/validate-database.d.ts +4 -0
  157. package/dist/guard/rules/shared/extension/validate-database.js +15 -0
  158. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.d.ts +2 -0
  159. package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.js +9 -0
  160. package/dist/guard/rules/shared/extension/validate-shared.d.ts +18 -0
  161. package/dist/guard/rules/shared/extension/validate-shared.js +149 -0
  162. package/dist/host-protocol.d.ts +2 -0
  163. package/dist/host-protocol.js +2 -0
  164. package/dist/index.d.ts +3 -0
  165. package/dist/index.js +3 -0
  166. package/dist/verify/artifact-recovery-rules.d.ts +39 -0
  167. package/dist/verify/artifact-recovery-rules.js +48 -0
  168. package/dist/verify/artifact-session.d.ts +91 -0
  169. package/dist/verify/artifact-session.js +443 -0
  170. package/dist/verify/checkout-environment-rules.d.ts +2 -0
  171. package/dist/verify/checkout-environment-rules.js +25 -0
  172. package/dist/verify/checkout-environment.d.ts +8 -0
  173. package/dist/verify/checkout-environment.js +24 -0
  174. package/dist/verify/commit-candidate-rules.d.ts +3 -0
  175. package/dist/verify/commit-candidate-rules.js +33 -0
  176. package/dist/verify/commit-gate.d.ts +64 -0
  177. package/dist/verify/commit-gate.js +144 -0
  178. package/dist/verify/delivery-verification.d.ts +3 -0
  179. package/dist/verify/delivery-verification.js +21 -0
  180. package/dist/verify/e2e-registry-rules.d.ts +21 -0
  181. package/dist/verify/e2e-registry-rules.js +33 -0
  182. package/dist/verify/e2e-registry.d.ts +2 -0
  183. package/dist/verify/e2e-registry.js +45 -0
  184. package/dist/verify/full-command.d.ts +35 -0
  185. package/dist/verify/full-command.js +124 -0
  186. package/dist/verify/impact-plan-rules.d.ts +61 -0
  187. package/dist/verify/impact-plan-rules.js +185 -0
  188. package/dist/verify/impact-plan.d.ts +10 -0
  189. package/dist/verify/impact-plan.js +151 -0
  190. package/dist/verify/index.d.ts +22 -0
  191. package/dist/verify/index.js +22 -0
  192. package/dist/verify/repository-recipe-rules.d.ts +31 -0
  193. package/dist/verify/repository-recipe-rules.js +60 -0
  194. package/dist/verify/repository-recipe.d.ts +3 -0
  195. package/dist/verify/repository-recipe.js +58 -0
  196. package/dist/verify/static-validation-rules.d.ts +7 -0
  197. package/dist/verify/static-validation-rules.js +36 -0
  198. package/dist/verify/story-selection-rules.d.ts +57 -0
  199. package/dist/verify/story-selection-rules.js +125 -0
  200. package/dist/verify/types.d.ts +63 -0
  201. package/dist/verify/types.js +18 -0
  202. package/dist/verify/verification-candidate.d.ts +1 -0
  203. package/dist/verify/verification-candidate.js +20 -0
  204. package/dist/verify/verification-identity.d.ts +6 -0
  205. package/dist/verify/verification-identity.js +26 -0
  206. package/dist/verify/verification-proof-rules.d.ts +35 -0
  207. package/dist/verify/verification-proof-rules.js +45 -0
  208. package/dist/verify/verify-full-command-rules.d.ts +18 -0
  209. package/dist/verify/verify-full-command-rules.js +77 -0
  210. package/dist/verify/verify-full-runner.d.ts +32 -0
  211. package/dist/verify/verify-full-runner.js +251 -0
  212. package/package.json +115 -4
@@ -0,0 +1,91 @@
1
+ export type DiagnosticArchiveExpiration = {
2
+ schemaVersion: 1;
3
+ state: "expiring" | "expired";
4
+ root: string;
5
+ dev: string;
6
+ ino: string;
7
+ ownerSha256: string;
8
+ files: {
9
+ name: string;
10
+ sha256: string;
11
+ size: number;
12
+ }[];
13
+ startedAt: number;
14
+ completedAt?: number;
15
+ reason: string;
16
+ };
17
+ export type VerificationArtifactSession = {
18
+ schemaVersion: 1;
19
+ id: string;
20
+ state: string;
21
+ receiptPath: string;
22
+ receiptSha256: string;
23
+ sourceDigest: string;
24
+ ownerRecordPath?: string;
25
+ archiveExpiration?: DiagnosticArchiveExpiration;
26
+ interruption?: {
27
+ previousState: "preparing" | "consuming" | "failed-retained";
28
+ detectedAt: number;
29
+ archivePath: string;
30
+ archiveSha256: string;
31
+ preparationArchiveSha256?: string;
32
+ };
33
+ release?: {
34
+ schemaVersion: 1;
35
+ runRoot: string;
36
+ dev: string;
37
+ ino: string;
38
+ initiatorPid: number;
39
+ startedAt: number;
40
+ completedAt?: number;
41
+ lastError?: string;
42
+ };
43
+ stages: {
44
+ command: string;
45
+ state: string;
46
+ durationMs: number;
47
+ }[];
48
+ };
49
+ /** 只接受当前 runner 明确传入的记录,不扫描/猜测其它轮次的目录。 */
50
+ export declare function readVerificationArtifactSession(workspaceRoot: string, recordPath: string): Promise<VerificationArtifactSession | null>;
51
+ /** 最后消费者与输入复验完成后,释放本轮唯一 owner;父验证独立核对候选并发布 proof。 */
52
+ export declare function releaseVerificationArtifactSession(workspaceRoot: string, recordPath: string, record: VerificationArtifactSession): Promise<void>;
53
+ /** 调用方持既有重型锁并结清进程账本;恢复只完成删除,绝不生成测试成功proof。 */
54
+ export declare function recoverVerificationArtifactRelease(workspaceRoot: string, recordPath: string): Promise<void>;
55
+ /** 恢复已退出的准备交接/正式消费owner;调用方持排他锁且已确认后代进程结清。 */
56
+ export declare function recoverInterruptedVerificationArtifactSession(workspaceRoot: string, recordPath: string, preparationReportPath?: string): Promise<boolean>;
57
+ export type FailedVerificationArtifactSession = Omit<VerificationArtifactSession, "stages"> & {
58
+ pid: number;
59
+ error: string;
60
+ retentionEligibleAt?: number;
61
+ stages: {
62
+ command: string;
63
+ state: string;
64
+ durationMs?: number;
65
+ }[];
66
+ retirement?: {
67
+ schemaVersion: 1;
68
+ reason: string;
69
+ startedAt: number;
70
+ completedAt?: number;
71
+ runRoot: string;
72
+ dev: string;
73
+ ino: string;
74
+ archiveRoot: string;
75
+ receiptArchiveSha256: string;
76
+ sessionArchiveSha256: string;
77
+ };
78
+ };
79
+ /** 失败保留事实与成功证明严格分开;调用方必须先持排他锁并确认消费者进程已结清。 */
80
+ export declare function readFailedVerificationArtifactSession(workspaceRoot: string, recordPath: string): Promise<FailedVerificationArtifactSession>;
81
+ /** 归档回收不能改写既有owner事实,日志自身的进度不参与其来源摘要。 */
82
+ export declare function diagnosticArchiveOwnerSha256(record: object): string;
83
+ /** 不改写失败结果;先压缩归档,再登记目录身份,真实删除并读回,支持中断后重入。 */
84
+ export declare function retireFailedVerificationArtifactSession(workspaceRoot: string, recordPath: string, reason: string): Promise<void>;
85
+ /** 与原退休共用同一原始归档验真,不允许摘要篡改后删除唯一原始证据。 */
86
+ export declare function validateFailedVerificationArchive(workspaceRoot: string, recordPath: string): Promise<void>;
87
+ /** 自定义报告位置仍登记到既有 owner 目录,失败回收不需要广扫报告或猜测引用。 */
88
+ export declare function persistVerificationArtifactSession(workspaceRoot: string, recordPath: string, record: object & {
89
+ id: string;
90
+ ownerRecordPath?: string;
91
+ }): Promise<void>;
@@ -0,0 +1,443 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { lstat, mkdir, readFile, realpath, rename, rm, writeFile } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { gzipSync, gunzipSync } from "node:zlib";
5
+ import { assertFailedArtifactOwnerFacts, inspectInterruptedArtifactOwner, readInterruptedArtifactAllocation, assertInterruptedArtifactIdentity, assertOwnedArtifactReceipt } from "./artifact-recovery-rules.js";
6
+ /** 只接受当前 runner 明确传入的记录,不扫描/猜测其它轮次的目录。 */
7
+ export async function readVerificationArtifactSession(workspaceRoot, recordPath) {
8
+ const root = await realpath(workspaceRoot);
9
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
10
+ let bytes;
11
+ try {
12
+ if (await realpath(recordPath) !== recordPath)
13
+ throw new Error("验证 owner 报告不能经过缓存内部软链。");
14
+ bytes = await readFile(recordPath);
15
+ }
16
+ catch (error) {
17
+ if (error.code === "ENOENT")
18
+ return null;
19
+ throw error;
20
+ }
21
+ const record = JSON.parse(bytes.toString());
22
+ if (record.schemaVersion !== 1 || !/^[a-f0-9-]{36}$/u.test(record.id) || record.state !== "passed-retained"
23
+ || record.ownerRecordPath !== undefined && resolveCacheRecordPath(workspaceRoot, root, record.ownerRecordPath) !== recordPath
24
+ || !/^[a-f0-9]{64}$/u.test(record.receiptSha256) || !/^[a-f0-9]{64}$/u.test(record.sourceDigest)
25
+ || !Array.isArray(record.stages) || !record.stages.length
26
+ || record.stages.some(stage => typeof stage.command !== "string" || !stage.command.trim() || stage.state !== "passed" || !Number.isFinite(stage.durationMs) || stage.durationMs < 0)) {
27
+ throw new Error("验证制品会话未完整通过或身份无效。");
28
+ }
29
+ await validateOwnedReceipt(workspaceRoot, record);
30
+ return record;
31
+ }
32
+ async function validateOwnedReceipt(workspaceRoot, record) {
33
+ const root = await realpath(workspaceRoot);
34
+ const pool = path.join(root, ".cache/tmp/host-unit-artifacts");
35
+ const runRoot = path.dirname(record.receiptPath);
36
+ if (path.dirname(runRoot) !== pool || !/^[a-f0-9-]{36}$/u.test(path.basename(runRoot))
37
+ || path.basename(record.receiptPath) !== "input-receipt.json" || await realpath(runRoot) !== runRoot
38
+ || await realpath(record.receiptPath) !== record.receiptPath)
39
+ throw new Error("验证制品回收目录不属于本轮独立输入。");
40
+ const bytes = await readFile(record.receiptPath);
41
+ const receipt = JSON.parse(bytes.toString());
42
+ assertOwnedArtifactReceipt(record, receipt, createHash("sha256").update(bytes).digest("hex"), root, runRoot);
43
+ return runRoot;
44
+ }
45
+ /** 最后消费者与输入复验完成后,释放本轮唯一 owner;父验证独立核对候选并发布 proof。 */
46
+ export async function releaseVerificationArtifactSession(workspaceRoot, recordPath, record) {
47
+ const root = await realpath(workspaceRoot);
48
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
49
+ const latest = ["releasing-success", "cleanup-blocked"].includes(record.state)
50
+ ? await readReleasingSession(root, recordPath) : await readVerificationArtifactSession(root, recordPath);
51
+ if (!latest || JSON.stringify(latest) !== JSON.stringify(record))
52
+ throw new Error("验证制品会话在回收前已改变。");
53
+ if (record.release) {
54
+ if (record.release.initiatorPid !== process.pid)
55
+ throw new Error("成功清理重试必须属于原清理者;退出后的恢复需走独立入口。");
56
+ }
57
+ else {
58
+ const runRoot = await validateOwnedReceipt(root, record);
59
+ const identity = await lstat(runRoot, { bigint: true });
60
+ record.release = { schemaVersion: 1, runRoot, dev: identity.dev.toString(), ino: identity.ino.toString(),
61
+ initiatorPid: process.pid, startedAt: Date.now() };
62
+ record.state = "releasing-success";
63
+ // 先持久化删除授权,不能在部分删除后再靠缺失的receipt猜测原目录身份。
64
+ await persistVerificationArtifactSession(root, recordPath, record);
65
+ }
66
+ await finishArtifactRelease(root, recordPath, record);
67
+ }
68
+ async function readReleasingSession(root, recordPath) {
69
+ if (await realpath(recordPath) !== recordPath)
70
+ throw new Error("成功清理记录不能经过软链。");
71
+ const record = JSON.parse(await readFile(recordPath, "utf8"));
72
+ const release = record.release, runRoot = typeof record.receiptPath === "string" ? path.dirname(record.receiptPath) : "";
73
+ if (record.schemaVersion !== 1 || !/^[a-f0-9-]{36}$/u.test(record.id)
74
+ || !["releasing-success", "cleanup-blocked"].includes(record.state) || record.ownerRecordPath !== recordPath
75
+ || !/^[a-f0-9]{64}$/u.test(record.receiptSha256) || !/^[a-f0-9]{64}$/u.test(record.sourceDigest)
76
+ || !Array.isArray(record.stages) || !record.stages.length
77
+ || record.stages.some(stage => typeof stage.command !== "string" || !stage.command.trim() || stage.state !== "passed"
78
+ || !Number.isFinite(stage.durationMs) || stage.durationMs < 0)
79
+ || !release || release.schemaVersion !== 1 || release.runRoot !== runRoot
80
+ || path.dirname(runRoot) !== path.join(root, ".cache/tmp/host-unit-artifacts")
81
+ || !/^[a-f0-9-]{36}$/u.test(path.basename(runRoot)) || path.basename(record.receiptPath) !== "input-receipt.json"
82
+ || typeof release.dev !== "string" || typeof release.ino !== "string" || !/^\d+$/u.test(release.dev) || !/^\d+$/u.test(release.ino)
83
+ || !Number.isSafeInteger(release.initiatorPid) || release.initiatorPid <= 0
84
+ || !Number.isSafeInteger(release.startedAt) || release.startedAt <= 0)
85
+ throw new Error("成功清理缺少有效的原owner退休日志,拒绝猜测回收。");
86
+ return record;
87
+ }
88
+ /** 调用方持既有重型锁并结清进程账本;恢复只完成删除,绝不生成测试成功proof。 */
89
+ export async function recoverVerificationArtifactRelease(workspaceRoot, recordPath) {
90
+ const root = await realpath(workspaceRoot);
91
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
92
+ const record = await readReleasingSession(root, recordPath);
93
+ try {
94
+ process.kill(record.release.initiatorPid, 0);
95
+ throw new Error("成功清理者仍活跃,不能接管。");
96
+ }
97
+ catch (error) {
98
+ if (error.code !== "ESRCH")
99
+ throw error;
100
+ }
101
+ await finishArtifactRelease(root, recordPath, record);
102
+ }
103
+ async function finishArtifactRelease(root, recordPath, record) {
104
+ const release = record.release;
105
+ try {
106
+ try {
107
+ const current = await lstat(release.runRoot, { bigint: true });
108
+ if (!current.isDirectory() || current.dev.toString() !== release.dev || current.ino.toString() !== release.ino
109
+ || await realpath(release.runRoot) !== release.runRoot)
110
+ throw new Error("成功清理目录身份已改变,拒绝删除。");
111
+ await rm(release.runRoot, { recursive: true, force: false });
112
+ }
113
+ catch (error) {
114
+ if (error.code !== "ENOENT")
115
+ throw error;
116
+ }
117
+ try {
118
+ await lstat(release.runRoot);
119
+ throw new Error("验证制品清理后仍然存在。");
120
+ }
121
+ catch (error) {
122
+ if (error.code !== "ENOENT")
123
+ throw error;
124
+ }
125
+ record.state = "passed-cleaned";
126
+ release.completedAt = Date.now();
127
+ }
128
+ catch (error) {
129
+ record.state = "cleanup-blocked";
130
+ release.lastError = error instanceof Error ? error.message : String(error);
131
+ try {
132
+ await persistVerificationArtifactSession(root, recordPath, record);
133
+ }
134
+ catch (recordError) {
135
+ throw new AggregateError([error, recordError], "验证制品清理和阻断记录保存同时失败。");
136
+ }
137
+ throw error;
138
+ }
139
+ await persistVerificationArtifactSession(root, recordPath, record);
140
+ }
141
+ /** 恢复已退出的准备交接/正式消费owner;调用方持排他锁且已确认后代进程结清。 */
142
+ export async function recoverInterruptedVerificationArtifactSession(workspaceRoot, recordPath, preparationReportPath) {
143
+ const root = await realpath(workspaceRoot);
144
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
145
+ if (await realpath(recordPath) !== recordPath)
146
+ throw new Error("中断owner记录不能经过软链。");
147
+ const bytes = await readFile(recordPath), record = JSON.parse(bytes.toString());
148
+ const { preparing, previousState } = inspectInterruptedArtifactOwner(record, recordPath, preparationReportPath !== undefined);
149
+ try {
150
+ process.kill(record.pid, 0);
151
+ return false;
152
+ }
153
+ catch (error) {
154
+ if (error.code !== "ESRCH")
155
+ throw error;
156
+ }
157
+ let preparationBytes;
158
+ if (preparing) {
159
+ if (record.receiptPath !== undefined)
160
+ throw new Error("准备交接恢复不能替换父owner已有的输入引用。");
161
+ const file = preparationReportPath;
162
+ if (path.dirname(file) !== path.join(root, ".cache/reports/host-unit-artifacts")
163
+ || !/^[a-f0-9-]{36}\.json$/u.test(path.basename(file))
164
+ || !(await lstat(file)).isFile() || await realpath(file) !== file)
165
+ throw new Error("准备交接报告路径无效。");
166
+ preparationBytes = await readFile(file);
167
+ const preparation = JSON.parse(preparationBytes.toString());
168
+ const runRoot = path.join(root, ".cache/tmp/host-unit-artifacts", path.basename(file, ".json"));
169
+ const allocation = readInterruptedArtifactAllocation(preparation, record.id, runRoot);
170
+ try {
171
+ process.kill(allocation.pid, 0);
172
+ return false;
173
+ }
174
+ catch (error) {
175
+ if (error.code !== "ESRCH")
176
+ throw error;
177
+ }
178
+ const identity = await lstat(runRoot, { bigint: true });
179
+ assertInterruptedArtifactIdentity(allocation, { directory: identity.isDirectory(), realPath: await realpath(runRoot),
180
+ dev: identity.dev.toString(), ino: identity.ino.toString() }, runRoot);
181
+ record.receiptPath = path.join(runRoot, "input-receipt.json");
182
+ if (!(await lstat(record.receiptPath)).isFile() || await realpath(record.receiptPath) !== record.receiptPath)
183
+ throw new Error("准备交接receipt不能经过软链。");
184
+ const receiptBytes = await readFile(record.receiptPath), receipt = JSON.parse(receiptBytes.toString());
185
+ record.receiptSha256 = digest(receiptBytes);
186
+ record.sourceDigest = receipt.source?.digestBefore;
187
+ if (!/^[a-f0-9]{64}$/u.test(record.sourceDigest))
188
+ throw new Error("准备交接receipt缺少源码摘要。");
189
+ }
190
+ await validateOwnedReceipt(root, record);
191
+ const archiveRoot = path.join(root, ".cache/reports/test-artifact-retirements", record.id);
192
+ await ensureCacheDirectory(root, archiveRoot);
193
+ const archivePath = path.join(archiveRoot, "interrupted-session.json.gz");
194
+ const archiveSha256 = await archiveOnce(archivePath, gzipSync(bytes));
195
+ const detectedAt = Date.now();
196
+ record.interruption = { previousState, detectedAt, archivePath, archiveSha256,
197
+ ...(preparationBytes ? { preparationArchiveSha256: await archiveOnce(path.join(archiveRoot, "prepared-input.json.gz"), gzipSync(preparationBytes)) } : {}) };
198
+ record.stages = record.stages.map(stage => stage.state === "running" ? { ...stage, state: "interrupted" } : stage);
199
+ record.state = "failed-retained";
200
+ record.error = [record.error, preparing ? "验证owner在准备交接时退出,未开始消费;未生成成功证明。" : "验证owner已退出,消费阶段中断;未生成成功证明。"].filter(Boolean).join("; ");
201
+ record.retentionEligibleAt = detectedAt;
202
+ await persistVerificationArtifactSession(root, recordPath, record);
203
+ return true;
204
+ }
205
+ /** 失败保留事实与成功证明严格分开;调用方必须先持排他锁并确认消费者进程已结清。 */
206
+ export async function readFailedVerificationArtifactSession(workspaceRoot, recordPath) {
207
+ const root = await realpath(workspaceRoot);
208
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
209
+ const relative = path.relative(path.join(root, ".cache"), recordPath);
210
+ if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)
211
+ || await realpath(recordPath) !== recordPath)
212
+ throw new Error("失败 owner 记录必须属于当前工作区缓存报告,不能经过软链。");
213
+ const record = JSON.parse(await readFile(recordPath, "utf8"));
214
+ assertFailedArtifactOwnerFacts(record, recordPath);
215
+ let archiveExpired = false;
216
+ if (record.archiveExpiration) {
217
+ const expiration = record.archiveExpiration;
218
+ const expected = {
219
+ "input-receipt.json.gz": record.retirement?.receiptArchiveSha256, "session.json.gz": record.retirement?.sessionArchiveSha256,
220
+ ...(record.interruption ? { "interrupted-session.json.gz": record.interruption.archiveSha256 } : {}),
221
+ ...(record.interruption && record.interruption.previousState !== "consuming" ? { "prepared-input.json.gz": record.interruption.preparationArchiveSha256 } : {}),
222
+ };
223
+ if (record.state !== "failed-cleaned" || expiration.schemaVersion !== 1 || expiration.ownerSha256 !== diagnosticArchiveOwnerSha256(record)
224
+ || expiration.root !== path.join(root, ".cache/reports/test-artifact-retirements", record.id)
225
+ || !/^\d+$/u.test(expiration.dev) || !/^\d+$/u.test(expiration.ino)
226
+ || !Number.isSafeInteger(expiration.startedAt) || expiration.startedAt <= 0 || !expiration.reason?.trim()
227
+ || !Array.isArray(expiration.files) || !expiration.files.length
228
+ || JSON.stringify(expiration.files.map(file => file.name).sort()) !== JSON.stringify(Object.keys(expected).sort())
229
+ || new Set(expiration.files.map(file => file.name)).size !== expiration.files.length
230
+ || expiration.files.some(file => !["input-receipt.json.gz", "session.json.gz", "interrupted-session.json.gz", "prepared-input.json.gz"].includes(file.name)
231
+ || !/^[a-f0-9]{64}$/u.test(file.sha256) || file.sha256 !== expected[file.name] || !Number.isSafeInteger(file.size) || file.size < 0))
232
+ throw new Error("失败诊断过期记录身份无效。");
233
+ if (expiration.state === "expiring")
234
+ throw new Error("失败诊断归档回收尚未完成,必须先恢复原回收。");
235
+ if (expiration.state !== "expired" || !Number.isSafeInteger(expiration.completedAt) || expiration.completedAt < expiration.startedAt)
236
+ throw new Error("失败诊断过期终态无效。");
237
+ try {
238
+ await lstat(expiration.root);
239
+ throw new Error("已过期诊断目录重新出现,拒绝将其当作原归档。");
240
+ }
241
+ catch (error) {
242
+ if (error.code !== "ENOENT")
243
+ throw error;
244
+ }
245
+ archiveExpired = true;
246
+ }
247
+ if (record.interruption && !archiveExpired) {
248
+ const interruption = record.interruption;
249
+ if (!["preparing", "consuming", "failed-retained"].includes(interruption.previousState) || !Number.isSafeInteger(interruption.detectedAt) || interruption.detectedAt <= 0
250
+ || interruption.archivePath !== path.join(root, ".cache/reports/test-artifact-retirements", record.id, "interrupted-session.json.gz")
251
+ || !/^[a-f0-9]{64}$/u.test(interruption.archiveSha256) || await realpath(interruption.archivePath) !== interruption.archivePath)
252
+ throw new Error("中断owner原始归档身份无效。");
253
+ const bytes = await readFile(interruption.archivePath);
254
+ if (digest(bytes) !== interruption.archiveSha256)
255
+ throw new Error("中断owner原始归档字节已改变。");
256
+ const original = JSON.parse(gunzipSync(bytes).toString());
257
+ if (original.id !== record.id || original.state !== interruption.previousState
258
+ || interruption.previousState === "consuming" && (original.receiptSha256 !== record.receiptSha256 || original.sourceDigest !== record.sourceDigest)
259
+ || !Array.isArray(original.stages)
260
+ || JSON.stringify(original.stages.map(stage => stage.state === "running" ? { ...stage, state: "interrupted" } : stage)) !== JSON.stringify(record.stages))
261
+ throw new Error("中断恢复不能改变原消费阶段事实。");
262
+ if (interruption.previousState !== "consuming") {
263
+ const file = path.join(root, ".cache/reports/test-artifact-retirements", record.id, "prepared-input.json.gz");
264
+ if (!/^[a-f0-9]{64}$/u.test(interruption.preparationArchiveSha256 ?? "") || await realpath(file) !== file)
265
+ throw new Error("准备交接原producer归档身份无效。");
266
+ const preparationBytes = await readFile(file);
267
+ if (digest(preparationBytes) !== interruption.preparationArchiveSha256)
268
+ throw new Error("准备交接原producer归档已改变。");
269
+ const preparation = JSON.parse(gunzipSync(preparationBytes).toString());
270
+ if (original.receiptPath !== undefined || interruption.previousState === "failed-retained" && (!original.error || !record.error.startsWith(original.error))
271
+ || preparation.status !== "ready" || preparation.ownerSessionId !== record.id
272
+ || preparation.runRoot !== path.dirname(record.receiptPath))
273
+ throw new Error("准备交接不能改变原producer与owner关联。");
274
+ if (record.state === "failed-retained") {
275
+ const identity = await lstat(preparation.runRoot, { bigint: true });
276
+ if (!identity.isDirectory() || await realpath(preparation.runRoot) !== preparation.runRoot
277
+ || identity.dev.toString() !== preparation.allocation?.dev || identity.ino.toString() !== preparation.allocation?.ino)
278
+ throw new Error("准备交接输入目录已被替换,拒绝退休。");
279
+ }
280
+ }
281
+ }
282
+ const runRoot = path.dirname(record.receiptPath);
283
+ if (path.dirname(runRoot) !== path.join(root, ".cache/tmp/host-unit-artifacts")
284
+ || !/^[a-f0-9-]{36}$/u.test(path.basename(runRoot)) || path.basename(record.receiptPath) !== "input-receipt.json")
285
+ throw new Error("失败输入不属于独立制品池。");
286
+ if (record.state === "failed-retained")
287
+ await validateOwnedReceipt(root, record);
288
+ else if (!record.retirement || record.retirement.schemaVersion !== 1 || record.retirement.runRoot !== runRoot
289
+ || record.retirement.archiveRoot !== path.join(root, ".cache/reports/test-artifact-retirements", record.id)
290
+ || typeof record.retirement.dev !== "string" || typeof record.retirement.ino !== "string"
291
+ || !/^\d+$/u.test(record.retirement.dev) || !/^\d+$/u.test(record.retirement.ino))
292
+ throw new Error("失败退休日志无效,禁止猜测删除目标。");
293
+ return record;
294
+ }
295
+ function digest(bytes) { return createHash("sha256").update(bytes).digest("hex"); }
296
+ /** 归档回收不能改写既有owner事实,日志自身的进度不参与其来源摘要。 */
297
+ export function diagnosticArchiveOwnerSha256(record) {
298
+ return digest(Buffer.from(JSON.stringify(Object.fromEntries(Object.entries(record).filter(([key]) => key !== "archiveExpiration")))));
299
+ }
300
+ async function archiveOnce(file, bytes) {
301
+ try {
302
+ await writeFile(file, bytes, { flag: "wx" });
303
+ }
304
+ catch (error) {
305
+ if (error.code !== "EEXIST")
306
+ throw error;
307
+ }
308
+ if (await realpath(file) !== file || digest(await readFile(file)) !== digest(bytes))
309
+ throw new Error("失败诊断归档字节已改变,保留输入。");
310
+ return digest(bytes);
311
+ }
312
+ /** 不改写失败结果;先压缩归档,再登记目录身份,真实删除并读回,支持中断后重入。 */
313
+ export async function retireFailedVerificationArtifactSession(workspaceRoot, recordPath, reason) {
314
+ if (!reason.trim())
315
+ throw new Error("失败退休必须说明原因。");
316
+ const root = await realpath(workspaceRoot);
317
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
318
+ const record = await readFailedVerificationArtifactSession(root, recordPath);
319
+ const runRoot = path.dirname(record.receiptPath);
320
+ if (record.state === "failed-cleaned") {
321
+ try {
322
+ await lstat(runRoot);
323
+ throw new Error("已退休的输入路径重新出现,拒绝删除。");
324
+ }
325
+ catch (error) {
326
+ if (error.code !== "ENOENT")
327
+ throw error;
328
+ }
329
+ return;
330
+ }
331
+ if (!Number.isFinite(record.retentionEligibleAt)) {
332
+ try {
333
+ process.kill(record.pid, 0);
334
+ throw new Error("失败输入 owner 仍活跃且未完成保留交接,拒绝退休。");
335
+ }
336
+ catch (error) {
337
+ if (error.code !== "ESRCH")
338
+ throw error;
339
+ }
340
+ }
341
+ if (record.state === "failed-retained") {
342
+ const archiveRoot = path.join(root, ".cache/reports/test-artifact-retirements", record.id);
343
+ await ensureCacheDirectory(root, archiveRoot);
344
+ const identity = await lstat(runRoot, { bigint: true });
345
+ const receiptArchiveSha256 = await archiveOnce(path.join(archiveRoot, "input-receipt.json.gz"), gzipSync(await readFile(record.receiptPath)));
346
+ const sessionArchiveSha256 = await archiveOnce(path.join(archiveRoot, "session.json.gz"), gzipSync(Buffer.from(JSON.stringify(record))));
347
+ record.retirement = { schemaVersion: 1, reason, startedAt: Date.now(), runRoot, dev: identity.dev.toString(), ino: identity.ino.toString(),
348
+ archiveRoot, receiptArchiveSha256, sessionArchiveSha256 };
349
+ record.state = "retiring-failure";
350
+ await persistVerificationArtifactSession(root, recordPath, record);
351
+ }
352
+ const retirement = record.retirement;
353
+ await validateRetirementArchives(record);
354
+ try {
355
+ const identity = await lstat(runRoot, { bigint: true });
356
+ if (!identity.isDirectory() || identity.dev.toString() !== retirement.dev || identity.ino.toString() !== retirement.ino || await realpath(runRoot) !== runRoot)
357
+ throw new Error("失败输入目录已被替换,拒绝删除。");
358
+ await rm(runRoot, { recursive: true, force: false });
359
+ }
360
+ catch (error) {
361
+ if (error.code !== "ENOENT")
362
+ throw error;
363
+ }
364
+ try {
365
+ await lstat(runRoot);
366
+ throw new Error("失败输入退休后仍然存在。");
367
+ }
368
+ catch (error) {
369
+ if (error.code !== "ENOENT")
370
+ throw error;
371
+ }
372
+ record.state = "failed-cleaned";
373
+ retirement.completedAt = Date.now();
374
+ await persistVerificationArtifactSession(root, recordPath, record);
375
+ }
376
+ /** 与原退休共用同一原始归档验真,不允许摘要篡改后删除唯一原始证据。 */
377
+ export async function validateFailedVerificationArchive(workspaceRoot, recordPath) {
378
+ const record = await readFailedVerificationArtifactSession(workspaceRoot, recordPath);
379
+ if (record.archiveExpiration || !record.retirement || !["retiring-failure", "failed-cleaned"].includes(record.state))
380
+ throw new Error("失败诊断尚未完成归档或已进入过期流程。");
381
+ await validateRetirementArchives(record);
382
+ }
383
+ async function validateRetirementArchives(record) {
384
+ const retirement = record.retirement;
385
+ if (await realpath(retirement.archiveRoot) !== retirement.archiveRoot)
386
+ throw new Error("失败归档身份改变。");
387
+ const archivedReceipt = await readFile(path.join(retirement.archiveRoot, "input-receipt.json.gz"));
388
+ const archivedSession = await readFile(path.join(retirement.archiveRoot, "session.json.gz"));
389
+ if (digest(archivedReceipt) !== retirement.receiptArchiveSha256 || digest(archivedSession) !== retirement.sessionArchiveSha256
390
+ || digest(gunzipSync(archivedReceipt)) !== record.receiptSha256)
391
+ throw new Error("失败退休归档核验失败,保留输入。");
392
+ const original = JSON.parse(gunzipSync(archivedSession).toString());
393
+ if (original.id !== record.id || original.state !== "failed-retained" || original.error !== record.error
394
+ || original.receiptSha256 !== record.receiptSha256 || original.receiptPath !== record.receiptPath || original.sourceDigest !== record.sourceDigest || JSON.stringify(original.stages) !== JSON.stringify(record.stages))
395
+ throw new Error("失败退休不能改变原失败事实。");
396
+ }
397
+ async function writeRecord(recordPath, record) {
398
+ const temporary = `${recordPath}.${randomUUID()}.release.tmp`;
399
+ await writeFile(temporary, JSON.stringify(record, null, 2), { flag: "wx" });
400
+ await rename(temporary, recordPath);
401
+ }
402
+ /** 自定义报告位置仍登记到既有 owner 目录,失败回收不需要广扫报告或猜测引用。 */
403
+ export async function persistVerificationArtifactSession(workspaceRoot, recordPath, record) {
404
+ const root = await realpath(workspaceRoot);
405
+ recordPath = resolveCacheRecordPath(workspaceRoot, root, recordPath);
406
+ if (!/^[a-f0-9-]{36}$/u.test(record.id) || record.ownerRecordPath !== undefined
407
+ && resolveCacheRecordPath(workspaceRoot, root, record.ownerRecordPath) !== recordPath)
408
+ throw new Error("制品 owner 报告身份或路径无效。");
409
+ // 只规范化工作区本身的系统别名(如macOS /var);缓存内部仍逐层拒绝软链。
410
+ await ensureCacheDirectory(root, path.dirname(recordPath));
411
+ record.ownerRecordPath = recordPath;
412
+ await writeRecord(recordPath, record);
413
+ const registry = path.join(root, ".cache/reports/test-artifact-sessions");
414
+ await ensureCacheDirectory(root, registry);
415
+ const registeredPath = path.join(registry, `${record.id}.json`);
416
+ if (registeredPath !== recordPath)
417
+ await writeRecord(registeredPath, record);
418
+ }
419
+ function resolveCacheRecordPath(workspaceRoot, root, recordPath) {
420
+ if (!path.isAbsolute(recordPath))
421
+ throw new Error("制品 owner 报告必须是绝对路径。");
422
+ for (const ownerRoot of [root, path.resolve(workspaceRoot)]) {
423
+ const relative = path.relative(path.join(ownerRoot, ".cache"), recordPath);
424
+ if (relative && relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
425
+ return path.join(root, ".cache", relative);
426
+ }
427
+ throw new Error("制品 owner 报告必须属于当前工作区缓存。");
428
+ }
429
+ async function ensureCacheDirectory(root, directory) {
430
+ let parent = root;
431
+ for (const segment of path.relative(root, directory).split(path.sep)) {
432
+ parent = path.join(parent, segment);
433
+ try {
434
+ await mkdir(parent);
435
+ }
436
+ catch (error) {
437
+ if (error.code !== "EEXIST")
438
+ throw error;
439
+ }
440
+ if (!(await lstat(parent)).isDirectory() || await realpath(parent) !== parent)
441
+ throw new Error("制品 owner 缓存目录不能经过软链。");
442
+ }
443
+ }
@@ -0,0 +1,2 @@
1
+ /** 平台门禁构建当前 checkout,不能复用启动 Host 的安装态预览制品。 */
2
+ export declare function createCheckoutVerificationEnvironmentFromFacts(platformRepository: boolean, source: Record<string, string | undefined>): Record<string, string | undefined>;
@@ -0,0 +1,25 @@
1
+ /** 平台门禁构建当前 checkout,不能复用启动 Host 的安装态预览制品。 */
2
+ export function createCheckoutVerificationEnvironmentFromFacts(platformRepository, source) {
3
+ const environment = { ...source };
4
+ // pnpm 11 默认在 exec/run 前自动安装。验证只检查既有冻结依赖,不能隐式修复。
5
+ environment.pnpm_config_verify_deps_before_run = "error";
6
+ if (!platformRepository)
7
+ return environment;
8
+ for (const key of [
9
+ "KUAFU_E2E_HOST_DIST_ROOT",
10
+ "PLATFORM_E2E_BROWSER_RENDERER_ROOT",
11
+ "DEVMANAGER_WEB_APP_DIST_ROOT", "DEVMANAGER_DEVELOPMENT_UI_ROOT",
12
+ "KUAFU_E2E_PREVIEW_INSTALLED_EXECUTABLE",
13
+ "KUAFU_E2E_ELECTRON_EXECUTABLE",
14
+ "KUAFU_DEV_TOOL_RUNTIME_ROOT",
15
+ "KUAFUCLOUD_PLAYWRIGHT_TEST_MODULE_PATH",
16
+ "KUAFU_LOCAL_HOST_RUNTIME_VITE_RESOLVER_PATH",
17
+ "KUAFU_LOCAL_HOST_RUNTIME",
18
+ ])
19
+ delete environment[key];
20
+ for (const prefix of ["APP", "APP_SIMULATOR", "SITE", "MINI_PROGRAM", "MINI_PROGRAM_SIMULATOR"]) {
21
+ delete environment[`KUAFU_${prefix}_RUNTIME_ARTIFACT_MANIFEST`];
22
+ delete environment[`KUAFU_${prefix}_SERVICE_MODULE_ARTIFACT_SET`];
23
+ }
24
+ return environment;
25
+ }
@@ -0,0 +1,8 @@
1
+ /** 验证证据绑定实际执行工具,不把工程的 packageManager 偏好当作执行事实。 */
2
+ export declare function readCheckoutVerificationRuntime(workspaceRoot: string): Promise<{
3
+ node: string;
4
+ pnpm: string;
5
+ hostToolchain: string | null;
6
+ }>;
7
+ /** 默认入口只负责采样仓库身份和进程输入;规则拥有者不访问文件系统。 */
8
+ export declare function createCheckoutVerificationEnvironment(workspaceRoot: string, source: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
@@ -0,0 +1,24 @@
1
+ import { createCheckoutVerificationEnvironmentFromFacts } from "./checkout-environment-rules.js";
2
+ import { existsSync } from "node:fs";
3
+ import { execFile } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import path from "node:path";
6
+ /** 验证证据绑定实际执行工具,不把工程的 packageManager 偏好当作执行事实。 */
7
+ export async function readCheckoutVerificationRuntime(workspaceRoot) {
8
+ const toolchainRoot = process.env.KUAFU_HOST_TOOLCHAIN_ROOT;
9
+ if (toolchainRoot && !path.isAbsolute(toolchainRoot))
10
+ throw new Error("Host Toolchain 必须是绝对路径。");
11
+ // 受管路径直接由当前 Host Node 执行 pnpm 程序;Windows 无需 shell 解释 .cmd。
12
+ const { stdout } = await promisify(execFile)(toolchainRoot ? process.execPath : "pnpm", toolchainRoot ? [path.join(toolchainRoot, "pnpm/bin/pnpm.cjs"), "-v"] : ["-v"], {
13
+ cwd: workspaceRoot,
14
+ env: createCheckoutVerificationEnvironment(workspaceRoot, process.env),
15
+ });
16
+ const pnpm = stdout.trim();
17
+ if (!/^\d+\.\d+\.\d+(?:[-+][\w.-]+)?$/u.test(pnpm))
18
+ throw new Error("无法读取实际验证 pnpm 版本。");
19
+ return { node: process.version, pnpm, hostToolchain: process.env.KUAFU_HOST_TOOLCHAIN_FINGERPRINT ?? null };
20
+ }
21
+ /** 默认入口只负责采样仓库身份和进程输入;规则拥有者不访问文件系统。 */
22
+ export function createCheckoutVerificationEnvironment(workspaceRoot, source) {
23
+ return createCheckoutVerificationEnvironmentFromFacts(existsSync(path.join(workspaceRoot, "packages/platform-tool/package.json")), source);
24
+ }
@@ -0,0 +1,3 @@
1
+ import type { TaskCommitGateError } from "./commit-gate.js";
2
+ export declare function findStagedProcessIssue(stagedFiles: string[]): TaskCommitGateError | null;
3
+ export declare function isDevProcessDocumentPath(docPath: string): boolean;
@@ -0,0 +1,33 @@
1
+ export function findStagedProcessIssue(stagedFiles) {
2
+ const stagedDevelopmentE2EFiles = stagedFiles.filter(isDevelopmentE2EPath);
3
+ if (stagedDevelopmentE2EFiles.length > 0) {
4
+ return {
5
+ code: "e2e.development_staged",
6
+ message: "Development E2E specs under test/e2e/.development are temporary DEV assets and must not be staged.",
7
+ details: { files: stagedDevelopmentE2EFiles },
8
+ };
9
+ }
10
+ if (stagedFiles.some((file) => file.endsWith(".temp.test.ts"))) {
11
+ return {
12
+ code: "e2e.temp_staged",
13
+ message: "Temporary E2E files ending with .temp.test.ts must not be staged. Convert to .test.ts only when intentionally persistent.",
14
+ details: { files: stagedFiles.filter((file) => file.endsWith(".temp.test.ts")) },
15
+ };
16
+ }
17
+ const stagedDevProcessFiles = stagedFiles.filter(isDevProcessDocumentPath);
18
+ if (stagedDevProcessFiles.length > 0) {
19
+ return {
20
+ code: "task.dev_process_file_staged",
21
+ message: "DEV 开发记录和计划是临时过程文档,不能暂存提交。",
22
+ details: { files: stagedDevProcessFiles },
23
+ };
24
+ }
25
+ return null;
26
+ }
27
+ function isDevelopmentE2EPath(file) {
28
+ return /(?:^|\/)test\/e2e\/\.development\//u.test(file.replaceAll("\\", "/"));
29
+ }
30
+ export function isDevProcessDocumentPath(docPath) {
31
+ // DEV 过程文档不进入提交;日期、计划名称和归档目录均遵循 DEV*.md 合同。
32
+ return /^docs\/(?:[^/]+\/)*DEV[^/]*\.md$/u.test(docPath.replace(/\\/g, "/"));
33
+ }