@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.
- package/LICENSE.md +9 -0
- package/README.md +6 -2
- package/assets/git-hooks/check-push.cjs +21 -0
- package/assets/git-hooks/pre-push +7 -0
- package/assets/git-hooks/prepare-commit-msg +5 -0
- package/assets/verification/heavy-test-admission-core.d.mts +1 -0
- package/assets/verification/heavy-test-admission-core.mjs +10 -0
- package/assets/verification/host-process-lock.d.mts +24 -0
- package/assets/verification/host-process-lock.mjs +140 -0
- package/assets/verification/test-heavy-admission.d.mts +4 -0
- package/assets/verification/test-heavy-admission.mjs +34 -0
- package/bin/kuafu-guard.mjs +3 -0
- package/bin/kuafu-verify.mjs +3 -0
- package/dist/cli/guard.d.ts +1 -0
- package/dist/cli/guard.js +59 -0
- package/dist/cli/verify.d.ts +1 -0
- package/dist/cli/verify.js +58 -0
- package/dist/guard/core/config.d.ts +7 -0
- package/dist/guard/core/config.js +159 -0
- package/dist/guard/core/engine.d.ts +2 -0
- package/dist/guard/core/engine.js +151 -0
- package/dist/guard/core/formatters/sarif-rules.d.ts +2 -0
- package/dist/guard/core/formatters/sarif-rules.js +67 -0
- package/dist/guard/core/formatters/sarif.d.ts +2 -0
- package/dist/guard/core/formatters/sarif.js +3 -0
- package/dist/guard/core/formatters/text.d.ts +2 -0
- package/dist/guard/core/formatters/text.js +22 -0
- package/dist/guard/core/index.d.ts +5 -0
- package/dist/guard/core/index.js +5 -0
- package/dist/guard/core/types.d.ts +94 -0
- package/dist/guard/core/types.js +1 -0
- package/dist/guard/hooks/install-rules.d.ts +2 -0
- package/dist/guard/hooks/install-rules.js +5 -0
- package/dist/guard/hooks/install.d.ts +36 -0
- package/dist/guard/hooks/install.js +212 -0
- package/dist/guard/hooks/push.d.ts +1 -0
- package/dist/guard/hooks/push.js +5 -0
- package/dist/guard/index.d.ts +16 -0
- package/dist/guard/index.js +16 -0
- package/dist/guard/product-roots.d.ts +12 -0
- package/dist/guard/product-roots.js +37 -0
- package/dist/guard/repo-args.d.ts +8 -0
- package/dist/guard/repo-args.js +75 -0
- package/dist/guard/repo-output-rules.d.ts +12 -0
- package/dist/guard/repo-output-rules.js +44 -0
- package/dist/guard/repo.d.ts +21 -0
- package/dist/guard/repo.js +74 -0
- package/dist/guard/rules/external/api/collect-orphan-tests.d.ts +2 -0
- package/dist/guard/rules/external/api/collect-orphan-tests.js +34 -0
- package/dist/guard/rules/external/api/collect.d.ts +2 -0
- package/dist/guard/rules/external/api/collect.js +419 -0
- package/dist/guard/rules/external/api/validate-orphan-tests.d.ts +2 -0
- package/dist/guard/rules/external/api/validate-orphan-tests.js +10 -0
- package/dist/guard/rules/external/api/validate.d.ts +2 -0
- package/dist/guard/rules/external/api/validate.js +278 -0
- package/dist/guard/rules/external/app/native-source-rules.d.ts +6 -0
- package/dist/guard/rules/external/app/native-source-rules.js +164 -0
- package/dist/guard/rules/external/app/validate-native-source-boundary.d.ts +2 -0
- package/dist/guard/rules/external/app/validate-native-source-boundary.js +187 -0
- package/dist/guard/rules/external/boundary/check.d.ts +4 -0
- package/dist/guard/rules/external/boundary/check.js +198 -0
- package/dist/guard/rules/external/extension/collect.d.ts +1 -0
- package/dist/guard/rules/external/extension/collect.js +1 -0
- package/dist/guard/rules/external/extension/electron-boundary-rules.d.ts +6 -0
- package/dist/guard/rules/external/extension/electron-boundary-rules.js +10 -0
- package/dist/guard/rules/external/extension/index.d.ts +6 -0
- package/dist/guard/rules/external/extension/index.js +29 -0
- package/dist/guard/rules/external/extension/manual-cross-call-rules.d.ts +19 -0
- package/dist/guard/rules/external/extension/manual-cross-call-rules.js +19 -0
- package/dist/guard/rules/external/extension/resolve-import.d.ts +1 -0
- package/dist/guard/rules/external/extension/resolve-import.js +43 -0
- package/dist/guard/rules/external/extension/scan-relative-dependencies.d.ts +5 -0
- package/dist/guard/rules/external/extension/scan-relative-dependencies.js +53 -0
- package/dist/guard/rules/external/extension/scan-source.d.ts +11 -0
- package/dist/guard/rules/external/extension/scan-source.js +25 -0
- package/dist/guard/rules/external/extension/source-import-boundary-rules.d.ts +26 -0
- package/dist/guard/rules/external/extension/source-import-boundary-rules.js +40 -0
- package/dist/guard/rules/external/extension/test-layer-import-rules.d.ts +1 -0
- package/dist/guard/rules/external/extension/test-layer-import-rules.js +16 -0
- package/dist/guard/rules/external/extension/types.d.ts +51 -0
- package/dist/guard/rules/external/extension/types.js +1 -0
- package/dist/guard/rules/external/extension/unused-export-rules.d.ts +23 -0
- package/dist/guard/rules/external/extension/unused-export-rules.js +119 -0
- package/dist/guard/rules/external/extension/unused-module-rules.d.ts +6 -0
- package/dist/guard/rules/external/extension/unused-module-rules.js +90 -0
- package/dist/guard/rules/external/extension/validate-admin.d.ts +8 -0
- package/dist/guard/rules/external/extension/validate-admin.js +73 -0
- package/dist/guard/rules/external/extension/validate-database.d.ts +3 -0
- package/dist/guard/rules/external/extension/validate-database.js +4 -0
- package/dist/guard/rules/external/extension/validate-electron-boundary.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-electron-boundary.js +18 -0
- package/dist/guard/rules/external/extension/validate-empty-dirs.d.ts +9 -0
- package/dist/guard/rules/external/extension/validate-empty-dirs.js +44 -0
- package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-extension-kit-server-boundary.js +116 -0
- package/dist/guard/rules/external/extension/validate-features.d.ts +7 -0
- package/dist/guard/rules/external/extension/validate-features.js +27 -0
- package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-manual-cross-extension-call.js +131 -0
- package/dist/guard/rules/external/extension/validate-peer-extension-import.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-peer-extension-import.js +41 -0
- package/dist/guard/rules/external/extension/validate-server-cross-domain-import.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-server-cross-domain-import.js +48 -0
- package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.d.ts +1 -0
- package/dist/guard/rules/external/extension/validate-server-index-api-entry-satisfies.js +1 -0
- package/dist/guard/rules/external/extension/validate-shared-import-boundary.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-shared-import-boundary.js +40 -0
- package/dist/guard/rules/external/extension/validate-shared.d.ts +1 -0
- package/dist/guard/rules/external/extension/validate-shared.js +1 -0
- package/dist/guard/rules/external/extension/validate-test-layer-boundary.d.ts +8 -0
- package/dist/guard/rules/external/extension/validate-test-layer-boundary.js +59 -0
- package/dist/guard/rules/external/extension/validate-test-mirror.d.ts +4 -0
- package/dist/guard/rules/external/extension/validate-test-mirror.js +32 -0
- package/dist/guard/rules/external/extension/validate-top-level.d.ts +7 -0
- package/dist/guard/rules/external/extension/validate-top-level.js +76 -0
- package/dist/guard/rules/external/extension/validate-unused-export.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-unused-export.js +187 -0
- package/dist/guard/rules/external/extension/validate-unused-module.d.ts +2 -0
- package/dist/guard/rules/external/extension/validate-unused-module.js +38 -0
- package/dist/guard/rules/external/index.d.ts +1 -0
- package/dist/guard/rules/external/index.js +1 -0
- package/dist/guard/rules/external/npm-dependency-policy.d.ts +14 -0
- package/dist/guard/rules/external/npm-dependency-policy.js +216 -0
- package/dist/guard/rules/external/rules.d.ts +2 -0
- package/dist/guard/rules/external/rules.js +222 -0
- package/dist/guard/rules/external/shared/config.d.ts +3 -0
- package/dist/guard/rules/external/shared/config.js +14 -0
- package/dist/guard/rules/external/shared/diagnostic.d.ts +4 -0
- package/dist/guard/rules/external/shared/diagnostic.js +12 -0
- package/dist/guard/rules/external/shared/records.d.ts +4 -0
- package/dist/guard/rules/external/shared/records.js +46 -0
- package/dist/guard/rules/external/shared/ts-ast.d.ts +8 -0
- package/dist/guard/rules/external/shared/ts-ast.js +103 -0
- package/dist/guard/rules/external/shared/types.d.ts +159 -0
- package/dist/guard/rules/external/shared/types.js +1 -0
- package/dist/guard/rules/external/shared/validate-layer-key.d.ts +2 -0
- package/dist/guard/rules/external/shared/validate-layer-key.js +35 -0
- package/dist/guard/rules/external/story-rules.d.ts +15 -0
- package/dist/guard/rules/external/story-rules.js +59 -0
- package/dist/guard/rules/shared/extension/api-entry-tuple-rules.d.ts +7 -0
- package/dist/guard/rules/shared/extension/api-entry-tuple-rules.js +6 -0
- package/dist/guard/rules/shared/extension/collect.d.ts +5 -0
- package/dist/guard/rules/shared/extension/collect.js +231 -0
- package/dist/guard/rules/shared/extension/database-reference-rules.d.ts +4 -0
- package/dist/guard/rules/shared/extension/database-reference-rules.js +5 -0
- package/dist/guard/rules/shared/extension/database-source-rules.d.ts +17 -0
- package/dist/guard/rules/shared/extension/database-source-rules.js +595 -0
- package/dist/guard/rules/shared/extension/database-structure-rules.d.ts +3 -0
- package/dist/guard/rules/shared/extension/database-structure-rules.js +63 -0
- package/dist/guard/rules/shared/extension/server-index-source-rules.d.ts +2 -0
- package/dist/guard/rules/shared/extension/server-index-source-rules.js +91 -0
- package/dist/guard/rules/shared/extension/shared-consumer-rules.d.ts +23 -0
- package/dist/guard/rules/shared/extension/shared-consumer-rules.js +158 -0
- package/dist/guard/rules/shared/extension/shared-structure-rules.d.ts +6 -0
- package/dist/guard/rules/shared/extension/shared-structure-rules.js +27 -0
- package/dist/guard/rules/shared/extension/validate-database.d.ts +4 -0
- package/dist/guard/rules/shared/extension/validate-database.js +15 -0
- package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.d.ts +2 -0
- package/dist/guard/rules/shared/extension/validate-server-index-api-entry-satisfies.js +9 -0
- package/dist/guard/rules/shared/extension/validate-shared.d.ts +18 -0
- package/dist/guard/rules/shared/extension/validate-shared.js +149 -0
- package/dist/host-protocol.d.ts +2 -0
- package/dist/host-protocol.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/verify/artifact-recovery-rules.d.ts +39 -0
- package/dist/verify/artifact-recovery-rules.js +48 -0
- package/dist/verify/artifact-session.d.ts +91 -0
- package/dist/verify/artifact-session.js +443 -0
- package/dist/verify/checkout-environment-rules.d.ts +2 -0
- package/dist/verify/checkout-environment-rules.js +25 -0
- package/dist/verify/checkout-environment.d.ts +8 -0
- package/dist/verify/checkout-environment.js +24 -0
- package/dist/verify/commit-candidate-rules.d.ts +3 -0
- package/dist/verify/commit-candidate-rules.js +33 -0
- package/dist/verify/commit-gate.d.ts +64 -0
- package/dist/verify/commit-gate.js +144 -0
- package/dist/verify/delivery-verification.d.ts +3 -0
- package/dist/verify/delivery-verification.js +21 -0
- package/dist/verify/e2e-registry-rules.d.ts +21 -0
- package/dist/verify/e2e-registry-rules.js +33 -0
- package/dist/verify/e2e-registry.d.ts +2 -0
- package/dist/verify/e2e-registry.js +45 -0
- package/dist/verify/full-command.d.ts +35 -0
- package/dist/verify/full-command.js +124 -0
- package/dist/verify/impact-plan-rules.d.ts +61 -0
- package/dist/verify/impact-plan-rules.js +185 -0
- package/dist/verify/impact-plan.d.ts +10 -0
- package/dist/verify/impact-plan.js +151 -0
- package/dist/verify/index.d.ts +22 -0
- package/dist/verify/index.js +22 -0
- package/dist/verify/repository-recipe-rules.d.ts +31 -0
- package/dist/verify/repository-recipe-rules.js +60 -0
- package/dist/verify/repository-recipe.d.ts +3 -0
- package/dist/verify/repository-recipe.js +58 -0
- package/dist/verify/static-validation-rules.d.ts +7 -0
- package/dist/verify/static-validation-rules.js +36 -0
- package/dist/verify/story-selection-rules.d.ts +57 -0
- package/dist/verify/story-selection-rules.js +125 -0
- package/dist/verify/types.d.ts +63 -0
- package/dist/verify/types.js +18 -0
- package/dist/verify/verification-candidate.d.ts +1 -0
- package/dist/verify/verification-candidate.js +20 -0
- package/dist/verify/verification-identity.d.ts +6 -0
- package/dist/verify/verification-identity.js +26 -0
- package/dist/verify/verification-proof-rules.d.ts +35 -0
- package/dist/verify/verification-proof-rules.js +45 -0
- package/dist/verify/verify-full-command-rules.d.ts +18 -0
- package/dist/verify/verify-full-command-rules.js +77 -0
- package/dist/verify/verify-full-runner.d.ts +32 -0
- package/dist/verify/verify-full-runner.js +251 -0
- package/package.json +115 -4
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { isExactApiEntryTuple } from "./api-entry-tuple-rules.js";
|
|
3
|
+
const RULE_ID = "extension-server-index-api-entry-satisfies";
|
|
4
|
+
export function validateServerIndexSource(filePath, content) {
|
|
5
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
6
|
+
const exportAssignment = sourceFile.statements.find((statement) => ts.isExportAssignment(statement) && !statement.isExportEquals);
|
|
7
|
+
if (!exportAssignment) {
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
rule: RULE_ID,
|
|
11
|
+
filePath,
|
|
12
|
+
lineNumber: 1,
|
|
13
|
+
message: "src/server/index.ts 必须包含默认导出,并使用 `as const satisfies readonly ApiEntry[]`。",
|
|
14
|
+
},
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
if (isApiEntrySatisfiesExport(exportAssignment.expression)) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
if (ts.isIdentifier(exportAssignment.expression)) {
|
|
21
|
+
const name = exportAssignment.expression.text;
|
|
22
|
+
for (const statement of sourceFile.statements) {
|
|
23
|
+
if (!ts.isVariableStatement(statement))
|
|
24
|
+
continue;
|
|
25
|
+
const declaration = statement.declarationList.declarations.find(value => ts.isIdentifier(value.name) && value.name.text === name);
|
|
26
|
+
if (!declaration?.type || !declaration.initializer || !isApiEntrySatisfiesExport(declaration.initializer))
|
|
27
|
+
continue;
|
|
28
|
+
const annotation = declaration.type;
|
|
29
|
+
const satisfies = unwrapParenthesized(declaration.initializer);
|
|
30
|
+
const assertion = unwrapParenthesized(satisfies.expression);
|
|
31
|
+
const array = unwrapParenthesized(assertion.expression);
|
|
32
|
+
if (!ts.isTypeOperatorNode(annotation) || annotation.operator !== ts.SyntaxKind.ReadonlyKeyword
|
|
33
|
+
|| !ts.isTupleTypeNode(annotation.type) || !ts.isArrayLiteralExpression(array))
|
|
34
|
+
continue;
|
|
35
|
+
if (isExactApiEntryTuple({ constant: Boolean(statement.declarationList.flags & ts.NodeFlags.Const), readonly: true,
|
|
36
|
+
types: annotation.type.elements.map(element => ts.isTypeQueryNode(element) && ts.isIdentifier(element.exprName) ? element.exprName.text : null),
|
|
37
|
+
values: array.elements.map(value => ts.isIdentifier(value) ? value.text : null),
|
|
38
|
+
}))
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
rule: RULE_ID,
|
|
45
|
+
filePath,
|
|
46
|
+
lineNumber: sourceFile.getLineAndCharacterOfPosition(exportAssignment.expression.getStart(sourceFile)).line + 1,
|
|
47
|
+
message: "默认导出必须写为 `export default [...] as const satisfies readonly ApiEntry[];`,或导出带逐项 typeof readonly tuple 注解、相同 satisfies 初始化的 const。",
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
function isApiEntrySatisfiesExport(expression) {
|
|
52
|
+
const normalizedExpression = unwrapParenthesized(expression);
|
|
53
|
+
if (!ts.isSatisfiesExpression(normalizedExpression)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return (isConstAssertion(normalizedExpression.expression) &&
|
|
57
|
+
isReadonlyApiEntryArrayType(normalizedExpression.type));
|
|
58
|
+
}
|
|
59
|
+
function isConstAssertion(expression) {
|
|
60
|
+
const normalizedExpression = unwrapParenthesized(expression);
|
|
61
|
+
if (!ts.isAsExpression(normalizedExpression)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return (ts.isTypeReferenceNode(normalizedExpression.type) &&
|
|
65
|
+
ts.isIdentifier(normalizedExpression.type.typeName) &&
|
|
66
|
+
normalizedExpression.type.typeName.text === "const" &&
|
|
67
|
+
!normalizedExpression.type.typeArguments);
|
|
68
|
+
}
|
|
69
|
+
function isReadonlyApiEntryArrayType(typeNode) {
|
|
70
|
+
if (!ts.isTypeOperatorNode(typeNode)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (typeNode.operator !== ts.SyntaxKind.ReadonlyKeyword) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (!ts.isArrayTypeNode(typeNode.type)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const elementType = typeNode.type.elementType;
|
|
80
|
+
return (ts.isTypeReferenceNode(elementType) &&
|
|
81
|
+
ts.isIdentifier(elementType.typeName) &&
|
|
82
|
+
elementType.typeName.text === "ApiEntry" &&
|
|
83
|
+
!elementType.typeArguments);
|
|
84
|
+
}
|
|
85
|
+
function unwrapParenthesized(expression) {
|
|
86
|
+
let current = expression;
|
|
87
|
+
while (ts.isParenthesizedExpression(current)) {
|
|
88
|
+
current = current.expression;
|
|
89
|
+
}
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ExtensionStructureRecord } from "../../external/extension/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* 找到 shared 文件的所有最终消费者(递归穿透 shared 内部引用)
|
|
4
|
+
*/
|
|
5
|
+
export declare function findFinalConsumers(sharedFile: string, importGraph: Map<string, string[]>): string[];
|
|
6
|
+
/**
|
|
7
|
+
* 计算所有消费者的最长公共前缀目录
|
|
8
|
+
*/
|
|
9
|
+
export declare function computeCommonPrefix(consumers: string[], record: ExtensionStructureRecord): string;
|
|
10
|
+
/**
|
|
11
|
+
* 根据消费方公共前缀计算 shared 文件应存放的目录。
|
|
12
|
+
* src/shared/ 是 server 专属的平铺目录(不嵌套子目录)。
|
|
13
|
+
* test/shared/ 也是平铺目录。
|
|
14
|
+
*/
|
|
15
|
+
export declare function computeExpectedSharedDir(consumerPrefix: string, record: ExtensionStructureRecord): string;
|
|
16
|
+
import type { Issue } from "../../external/shared/types.js";
|
|
17
|
+
export declare function evaluateSharedConsumers(sharedFile: string, allConsumers: string[], record: ExtensionStructureRecord, workspaceRoot: string, productionUnitRule: boolean): Issue[];
|
|
18
|
+
export declare function acceptsProductionUnitRule(input: {
|
|
19
|
+
hasProductionValueConsumer: boolean;
|
|
20
|
+
hasUnitValueConsumer: boolean;
|
|
21
|
+
dependencyIssueCount: number;
|
|
22
|
+
}): boolean;
|
|
23
|
+
export declare function isUnitRuleConsumerFile(file: string, physicalLayer: string | undefined): boolean;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* 找到 shared 文件的所有最终消费者(递归穿透 shared 内部引用)
|
|
4
|
+
*/
|
|
5
|
+
export function findFinalConsumers(sharedFile, importGraph) {
|
|
6
|
+
// 构建反向 import 图,key 为无扩展名的规范化路径
|
|
7
|
+
const reverseGraph = new Map();
|
|
8
|
+
for (const [importer, imports] of importGraph) {
|
|
9
|
+
const importerNoExt = importer.replace(/\.tsx?$/, "");
|
|
10
|
+
for (const imported of imports) {
|
|
11
|
+
// imported 已经是无扩展名的(resolveImportPath 去掉了 .js)
|
|
12
|
+
const list = reverseGraph.get(imported) ?? [];
|
|
13
|
+
list.push(importerNoExt);
|
|
14
|
+
reverseGraph.set(imported, list);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const sharedFileNoExt = sharedFile.replace(/\.tsx?$/, "");
|
|
18
|
+
const directConsumers = reverseGraph.get(sharedFileNoExt) ?? [];
|
|
19
|
+
// 递归穿透 shared 内部引用
|
|
20
|
+
const visited = new Set();
|
|
21
|
+
const finalConsumers = [];
|
|
22
|
+
const queue = [...directConsumers];
|
|
23
|
+
while (queue.length > 0) {
|
|
24
|
+
const consumerNoExt = queue.pop();
|
|
25
|
+
if (visited.has(consumerNoExt))
|
|
26
|
+
continue;
|
|
27
|
+
visited.add(consumerNoExt);
|
|
28
|
+
if (isInShared(consumerNoExt)) {
|
|
29
|
+
const nextConsumers = reverseGraph.get(consumerNoExt) ?? [];
|
|
30
|
+
queue.push(...nextConsumers);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// 还原为完整路径(importGraph 的 key 是完整路径)
|
|
34
|
+
finalConsumers.push(consumerNoExt + ".ts");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return finalConsumers;
|
|
38
|
+
}
|
|
39
|
+
function isInShared(filePath) {
|
|
40
|
+
return filePath.split(path.sep).includes("shared");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 计算所有消费者的最长公共前缀目录
|
|
44
|
+
*/
|
|
45
|
+
export function computeCommonPrefix(consumers, record) {
|
|
46
|
+
if (consumers.length === 0)
|
|
47
|
+
return record.extensionRoot;
|
|
48
|
+
const srcDir = path.join(record.extensionRoot, "src");
|
|
49
|
+
const testDir = path.join(record.extensionRoot, "test");
|
|
50
|
+
// 所有消费者可能在 src 或 test 下,按第一个确定基准
|
|
51
|
+
const firstConsumer = consumers[0];
|
|
52
|
+
let baseDir;
|
|
53
|
+
if (firstConsumer.startsWith(srcDir)) {
|
|
54
|
+
baseDir = srcDir;
|
|
55
|
+
}
|
|
56
|
+
else if (firstConsumer.startsWith(testDir)) {
|
|
57
|
+
baseDir = testDir;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
baseDir = record.extensionRoot;
|
|
61
|
+
}
|
|
62
|
+
// 计算相对于 baseDir 的路径
|
|
63
|
+
const relPaths = consumers.map((c) => path.relative(baseDir, c));
|
|
64
|
+
// 计算公共前缀
|
|
65
|
+
let prefixParts = relPaths[0].split(path.sep);
|
|
66
|
+
for (let i = 1; i < relPaths.length; i++) {
|
|
67
|
+
const parts = relPaths[i].split(path.sep);
|
|
68
|
+
const common = [];
|
|
69
|
+
for (let j = 0; j < Math.min(prefixParts.length, parts.length); j++) {
|
|
70
|
+
if (prefixParts[j] === parts[j]) {
|
|
71
|
+
common.push(prefixParts[j]);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
prefixParts = common;
|
|
78
|
+
}
|
|
79
|
+
// 公共前缀已经是目录级别:因为最后不匹配的部分是文件名,
|
|
80
|
+
// 所以匹配的部分全部是目录段,无需裁剪
|
|
81
|
+
if (prefixParts.length > 0) {
|
|
82
|
+
return path.join(baseDir, ...prefixParts);
|
|
83
|
+
}
|
|
84
|
+
return baseDir;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 根据消费方公共前缀计算 shared 文件应存放的目录。
|
|
88
|
+
* src/shared/ 是 server 专属的平铺目录(不嵌套子目录)。
|
|
89
|
+
* test/shared/ 也是平铺目录。
|
|
90
|
+
*/
|
|
91
|
+
export function computeExpectedSharedDir(consumerPrefix, record) {
|
|
92
|
+
const srcDir = path.join(record.extensionRoot, "src");
|
|
93
|
+
const testDir = path.join(record.extensionRoot, "test");
|
|
94
|
+
const simulationRoot = path.join(srcDir, "simulation");
|
|
95
|
+
if (consumerPrefix === simulationRoot || consumerPrefix.startsWith(simulationRoot + path.sep))
|
|
96
|
+
return path.join(simulationRoot, "shared");
|
|
97
|
+
if (consumerPrefix.startsWith(srcDir)) {
|
|
98
|
+
return path.join(srcDir, "shared");
|
|
99
|
+
}
|
|
100
|
+
else if (consumerPrefix.startsWith(testDir)) {
|
|
101
|
+
return path.join(testDir, "shared");
|
|
102
|
+
}
|
|
103
|
+
return path.join(record.extensionRoot, "src", "shared");
|
|
104
|
+
}
|
|
105
|
+
export function evaluateSharedConsumers(sharedFile, allConsumers, record, workspaceRoot, productionUnitRule) {
|
|
106
|
+
const issues = [];
|
|
107
|
+
const sharedRelPath = toPosix(path.relative(workspaceRoot, sharedFile));
|
|
108
|
+
const srcConsumers = allConsumers.filter(c => c.startsWith(path.join(record.extensionRoot, "src")));
|
|
109
|
+
if (allConsumers.length === 0) {
|
|
110
|
+
issues.push({
|
|
111
|
+
rule: "extension-shared-dead-code",
|
|
112
|
+
filePath: sharedRelPath,
|
|
113
|
+
lineNumber: 1,
|
|
114
|
+
message: `shared 文件 "${path.basename(sharedFile)}" 没有被任何文件 import,属于死代码。请删除此文件。`,
|
|
115
|
+
});
|
|
116
|
+
return issues;
|
|
117
|
+
}
|
|
118
|
+
if (srcConsumers.length === 1 && !productionUnitRule) {
|
|
119
|
+
const consumerRelPath = toPosix(path.relative(workspaceRoot, srcConsumers[0]));
|
|
120
|
+
issues.push({
|
|
121
|
+
rule: "extension-shared-single-consumer",
|
|
122
|
+
filePath: sharedRelPath,
|
|
123
|
+
lineNumber: 1,
|
|
124
|
+
message: `shared 文件 "${path.basename(sharedFile)}" 仅被 "${consumerRelPath}" 消费。应将代码内聚到消费方文件中,不使用 shared 文件。`,
|
|
125
|
+
});
|
|
126
|
+
return issues;
|
|
127
|
+
}
|
|
128
|
+
if (srcConsumers.length === 0) {
|
|
129
|
+
// 仅被 test 消费,视为死代码
|
|
130
|
+
issues.push({
|
|
131
|
+
rule: "extension-shared-dead-code",
|
|
132
|
+
filePath: sharedRelPath,
|
|
133
|
+
lineNumber: 1,
|
|
134
|
+
message: `shared 文件 "${path.basename(sharedFile)}" 仅被测试文件 import,没有被 src/ 下任何文件使用。请删除或内聚。`,
|
|
135
|
+
});
|
|
136
|
+
return issues;
|
|
137
|
+
}
|
|
138
|
+
// 计算公共前缀并校验位置(仅基于 src 消费者)
|
|
139
|
+
const prefix = computeCommonPrefix(srcConsumers, record);
|
|
140
|
+
const expectedDir = computeExpectedSharedDir(prefix, record);
|
|
141
|
+
const actualDir = path.dirname(sharedFile);
|
|
142
|
+
if (toPosix(actualDir) !== toPosix(expectedDir)) {
|
|
143
|
+
issues.push({
|
|
144
|
+
rule: "extension-shared-prefix-mismatch",
|
|
145
|
+
filePath: sharedRelPath,
|
|
146
|
+
lineNumber: 1,
|
|
147
|
+
message: `shared 文件 "${path.basename(sharedFile)}" 位置不正确。消费方公共前缀为 "${toPosix(path.relative(record.extensionRoot, prefix))}",文件应在 "${toPosix(path.relative(workspaceRoot, expectedDir))}/" 下。`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return issues;
|
|
151
|
+
}
|
|
152
|
+
function toPosix(value) { return value.split(path.sep).join("/"); }
|
|
153
|
+
export function acceptsProductionUnitRule(input) {
|
|
154
|
+
return input.hasProductionValueConsumer && input.hasUnitValueConsumer && input.dependencyIssueCount === 0;
|
|
155
|
+
}
|
|
156
|
+
export function isUnitRuleConsumerFile(file, physicalLayer) {
|
|
157
|
+
return /\.test\.tsx?$/u.test(file) && physicalLayer === "unit";
|
|
158
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
export function checkMixedContent(sharedDir, workspaceRoot, entries) {
|
|
3
|
+
const issues = [];
|
|
4
|
+
const looseFiles = [];
|
|
5
|
+
const subdirs = [];
|
|
6
|
+
for (const entry of entries) {
|
|
7
|
+
if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
8
|
+
looseFiles.push(entry.name);
|
|
9
|
+
}
|
|
10
|
+
else if (entry.isDirectory()) {
|
|
11
|
+
subdirs.push(entry.name);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (looseFiles.length > 0 && subdirs.length > 0) {
|
|
15
|
+
for (const fileName of looseFiles) {
|
|
16
|
+
const filePath = path.relative(workspaceRoot, path.join(sharedDir, fileName));
|
|
17
|
+
issues.push({
|
|
18
|
+
rule: "extension-shared-mixed-content",
|
|
19
|
+
filePath: toPosix(filePath),
|
|
20
|
+
lineNumber: 1,
|
|
21
|
+
message: `shared/ 目录同时包含散落文件和子目录。文件 "${fileName}" 应按消费者组织到子目录中。`,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return issues;
|
|
26
|
+
}
|
|
27
|
+
function toPosix(value) { return value.split(path.sep).join("/"); }
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ExtensionDatabaseRecord } from "./database-source-rules.js";
|
|
2
|
+
export type { ExtensionDatabaseRecord, DatabaseIssue } from "./database-source-rules.js";
|
|
3
|
+
/** 文件读取只在真实装配层;内部/外部入口消费同一份AST规则。 */
|
|
4
|
+
export declare function validateDatabaseRecord(record: ExtensionDatabaseRecord, workspaceRoot: string): import("./database-source-rules.js").DatabaseIssue[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { validateDatabaseSources } from "./database-source-rules.js";
|
|
3
|
+
/** 文件读取只在真实装配层;内部/外部入口消费同一份AST规则。 */
|
|
4
|
+
export function validateDatabaseRecord(record, workspaceRoot) {
|
|
5
|
+
const sources = new Map();
|
|
6
|
+
for (const file of record.srcFiles) {
|
|
7
|
+
if (!/\.tsx?$/u.test(file) || file.endsWith(".d.ts"))
|
|
8
|
+
continue;
|
|
9
|
+
try {
|
|
10
|
+
sources.set(file, fs.readFileSync(file, "utf8"));
|
|
11
|
+
}
|
|
12
|
+
catch { /* 保持原规则忽略不可读候选文件的合同。 */ }
|
|
13
|
+
}
|
|
14
|
+
return validateDatabaseSources(record, workspaceRoot, sources);
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { validateServerIndexSource } from "./server-index-source-rules.js";
|
|
4
|
+
export async function validateServerIndexApiEntrySatisfies(record, workspaceRoot) {
|
|
5
|
+
const file = path.join(record.extensionRoot, "src/server/index.ts");
|
|
6
|
+
if (!existsSync(file))
|
|
7
|
+
return [];
|
|
8
|
+
return validateServerIndexSource(path.relative(workspaceRoot, file).split(path.sep).join("/"), readFileSync(file, "utf8"));
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type SharedUnitRuleInspector = {
|
|
2
|
+
resolveTestLayer(workspaceRoot: string, relativePath: string): string | undefined;
|
|
3
|
+
countDependencyIssues(sharedFile: string, workspaceRoot: string): number;
|
|
4
|
+
};
|
|
5
|
+
export declare function registerSharedUnitRuleInspector(inspector: SharedUnitRuleInspector | null): void;
|
|
6
|
+
import type { ExtensionStructureRecord } from "../../external/extension/types.js";
|
|
7
|
+
import type { Issue } from "../../external/shared/types.js";
|
|
8
|
+
/**
|
|
9
|
+
* 校验所有 shared/ 目录下文件的放置位置。
|
|
10
|
+
* 通过 AST 分析 import 关系,计算消费方公共前缀,判断文件位置是否正确。
|
|
11
|
+
*
|
|
12
|
+
* 规则:
|
|
13
|
+
* - extension-shared-dead-code: 0 消费者(死代码)
|
|
14
|
+
* - extension-shared-single-consumer: 仅 1 个消费者(除生产与真实 Unit 共同消费的纯规则外,应内聚)
|
|
15
|
+
* - extension-shared-prefix-mismatch: 消费者公共前缀与文件位置不匹配
|
|
16
|
+
* - extension-shared-mixed-content: shared/ 同时包含平铺文件和子目录
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateShared(record: ExtensionStructureRecord, workspaceRoot: string): Promise<Issue[]>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { findFinalConsumers, evaluateSharedConsumers, acceptsProductionUnitRule, isUnitRuleConsumerFile } from "./shared-consumer-rules.js";
|
|
2
|
+
let registeredUnitRuleInspector = null;
|
|
3
|
+
export function registerSharedUnitRuleInspector(inspector) {
|
|
4
|
+
registeredUnitRuleInspector = inspector;
|
|
5
|
+
}
|
|
6
|
+
function defaultResolveTestLayer(_workspaceRoot, relativePath) {
|
|
7
|
+
return /(?:^|\/)test\/unit\//u.test(relativePath) ? "unit" : undefined;
|
|
8
|
+
}
|
|
9
|
+
import { checkMixedContent } from "./shared-structure-rules.js";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import ts from "typescript";
|
|
13
|
+
/**
|
|
14
|
+
* 校验所有 shared/ 目录下文件的放置位置。
|
|
15
|
+
* 通过 AST 分析 import 关系,计算消费方公共前缀,判断文件位置是否正确。
|
|
16
|
+
*
|
|
17
|
+
* 规则:
|
|
18
|
+
* - extension-shared-dead-code: 0 消费者(死代码)
|
|
19
|
+
* - extension-shared-single-consumer: 仅 1 个消费者(除生产与真实 Unit 共同消费的纯规则外,应内聚)
|
|
20
|
+
* - extension-shared-prefix-mismatch: 消费者公共前缀与文件位置不匹配
|
|
21
|
+
* - extension-shared-mixed-content: shared/ 同时包含平铺文件和子目录
|
|
22
|
+
*/
|
|
23
|
+
export async function validateShared(record, workspaceRoot) {
|
|
24
|
+
const issues = [];
|
|
25
|
+
let runtimeImportGraph;
|
|
26
|
+
for (const sharedDir of record.sharedDirs) {
|
|
27
|
+
if (!fs.existsSync(sharedDir)) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
// 混合内容检测:平铺 .ts 文件和子目录共存时报错
|
|
31
|
+
const mixedIssues = checkMixedContent(sharedDir, workspaceRoot, fs.readdirSync(sharedDir, { withFileTypes: true }));
|
|
32
|
+
issues.push(...mixedIssues);
|
|
33
|
+
// 更精确的过滤:文件路径以 sharedDir 开头
|
|
34
|
+
const actualSharedFiles = (record.srcFiles.concat(record.testFiles)).filter((f) => {
|
|
35
|
+
const normalizedShared = sharedDir.endsWith(path.sep) ? sharedDir : sharedDir + path.sep;
|
|
36
|
+
return f.startsWith(normalizedShared) && f.endsWith(".ts");
|
|
37
|
+
});
|
|
38
|
+
// 构建 import 图
|
|
39
|
+
const importGraph = buildImportGraph(record);
|
|
40
|
+
for (const sharedFile of actualSharedFiles) {
|
|
41
|
+
// 找到所有最终消费者
|
|
42
|
+
const allConsumers = findFinalConsumers(sharedFile, importGraph);
|
|
43
|
+
// 只用 src/ 下的消费者做前缀计算(test 文件 import src/shared 是测试行为,不影响放置规则)
|
|
44
|
+
const srcDir = path.join(record.extensionRoot, "src");
|
|
45
|
+
const srcConsumers = allConsumers.filter((c) => c.startsWith(srcDir));
|
|
46
|
+
const productionUnitRule = srcConsumers.length === 1 && isProductionUnitRule(sharedFile, record.testFiles, workspaceRoot, findFinalConsumers(sharedFile, runtimeImportGraph ??= buildImportGraph(record, true)).some(file => file.startsWith(srcDir)));
|
|
47
|
+
issues.push(...evaluateSharedConsumers(sharedFile, allConsumers, record, workspaceRoot, productionUnitRule));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return issues;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 检查 shared/ 是否同时包含平铺 .ts 文件和子目录
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* 构建 import 图:key = 文件绝对路径, value = 该文件 import 的目标路径列表
|
|
57
|
+
*/
|
|
58
|
+
function buildImportGraph(record, runtimeOnly = false) {
|
|
59
|
+
const graph = new Map();
|
|
60
|
+
const allFiles = [...record.srcFiles, ...record.testFiles];
|
|
61
|
+
for (const filePath of allFiles) {
|
|
62
|
+
if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const imports = resolveImports(filePath, runtimeOnly);
|
|
66
|
+
graph.set(filePath, imports);
|
|
67
|
+
}
|
|
68
|
+
return graph;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 解析文件中的 import/export ... from 语句,返回目标路径列表
|
|
72
|
+
*/
|
|
73
|
+
function resolveImports(filePath, runtimeOnly = false) {
|
|
74
|
+
try {
|
|
75
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
76
|
+
const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
|
|
77
|
+
const imports = [];
|
|
78
|
+
for (const statement of sourceFile.statements) {
|
|
79
|
+
if (ts.isImportDeclaration(statement) && statement.moduleSpecifier) {
|
|
80
|
+
const clause = statement.importClause;
|
|
81
|
+
if (runtimeOnly && (!clause || clause.isTypeOnly || !clause.name && clause.namedBindings
|
|
82
|
+
&& ts.isNamedImports(clause.namedBindings) && clause.namedBindings.elements.every(item => item.isTypeOnly)))
|
|
83
|
+
continue;
|
|
84
|
+
const spec = getStringLiteral(statement.moduleSpecifier);
|
|
85
|
+
if (spec) {
|
|
86
|
+
const resolved = resolveImportPath(spec, filePath);
|
|
87
|
+
if (resolved)
|
|
88
|
+
imports.push(resolved);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (ts.isExportDeclaration(statement) && statement.moduleSpecifier) {
|
|
92
|
+
if (runtimeOnly && (statement.isTypeOnly || statement.exportClause && ts.isNamedExports(statement.exportClause)
|
|
93
|
+
&& statement.exportClause.elements.every(item => item.isTypeOnly)))
|
|
94
|
+
continue;
|
|
95
|
+
const spec = getStringLiteral(statement.moduleSpecifier);
|
|
96
|
+
if (spec) {
|
|
97
|
+
const resolved = resolveImportPath(spec, filePath);
|
|
98
|
+
if (resolved)
|
|
99
|
+
imports.push(resolved);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return imports;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** 只允许真实物理 Unit 的直接值导入;类型引用、support 和重型能力不构成规则复用。 */
|
|
110
|
+
function isProductionUnitRule(sharedFile, testFiles, workspaceRoot, hasProductionValueConsumer) {
|
|
111
|
+
if (!hasProductionValueConsumer)
|
|
112
|
+
return false;
|
|
113
|
+
const hasUnitValueConsumer = testFiles.some((file) => {
|
|
114
|
+
if (!isUnitRuleConsumerFile(file, (registeredUnitRuleInspector ? registeredUnitRuleInspector.resolveTestLayer(workspaceRoot, toPosix(path.relative(workspaceRoot, file))) : defaultResolveTestLayer(workspaceRoot, toPosix(path.relative(workspaceRoot, file))))))
|
|
115
|
+
return false;
|
|
116
|
+
const source = ts.createSourceFile(file, fs.readFileSync(file, "utf8"), ts.ScriptTarget.Latest, true);
|
|
117
|
+
return source.statements.some((statement) => {
|
|
118
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
119
|
+
return false;
|
|
120
|
+
const clause = statement.importClause;
|
|
121
|
+
if (!clause || clause.isTypeOnly)
|
|
122
|
+
return false;
|
|
123
|
+
const bindings = clause.namedBindings;
|
|
124
|
+
const hasValue = Boolean(clause.name || bindings && (ts.isNamespaceImport(bindings)
|
|
125
|
+
|| bindings.elements.some((element) => !element.isTypeOnly)));
|
|
126
|
+
return hasValue && resolveImportPath(statement.moduleSpecifier.text, file) === sharedFile.replace(/\.tsx?$/u, "");
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
return acceptsProductionUnitRule({ hasProductionValueConsumer, hasUnitValueConsumer, dependencyIssueCount: hasUnitValueConsumer ? (registeredUnitRuleInspector ? registeredUnitRuleInspector.countDependencyIssues(sharedFile, workspaceRoot) : 0) : 0 });
|
|
130
|
+
}
|
|
131
|
+
function getStringLiteral(expr) {
|
|
132
|
+
if (ts.isStringLiteral(expr))
|
|
133
|
+
return expr.text;
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 将相对 import 路径解析为绝对路径(不带扩展名)
|
|
138
|
+
*/
|
|
139
|
+
function resolveImportPath(specifier, fromFile) {
|
|
140
|
+
if (!specifier.startsWith("."))
|
|
141
|
+
return null;
|
|
142
|
+
const fromDir = path.dirname(fromFile);
|
|
143
|
+
const resolved = path.normalize(path.resolve(fromDir, specifier));
|
|
144
|
+
// 去掉 .js 扩展名(TypeScript import 通常写 .js)
|
|
145
|
+
return resolved.replace(/\.js$/, "");
|
|
146
|
+
}
|
|
147
|
+
function toPosix(value) {
|
|
148
|
+
return value.split(path.sep).join("/");
|
|
149
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { FailedVerificationArtifactSession } from "./artifact-session.js";
|
|
2
|
+
/** 只判定已采样事实;进程探测、路径/字节读取、归档和删除仍由真实adapter负责。 */
|
|
3
|
+
export declare function inspectInterruptedArtifactOwner(record: FailedVerificationArtifactSession, recordPath: string, hasPreparation: boolean): {
|
|
4
|
+
preparing: boolean;
|
|
5
|
+
previousState: "failed-retained" | "preparing" | "consuming";
|
|
6
|
+
};
|
|
7
|
+
export type ArtifactAllocation = {
|
|
8
|
+
pid: number;
|
|
9
|
+
dev: string;
|
|
10
|
+
ino: string;
|
|
11
|
+
};
|
|
12
|
+
type PreparationFacts = {
|
|
13
|
+
schemaVersion?: number;
|
|
14
|
+
status?: string;
|
|
15
|
+
ownerSessionId?: string;
|
|
16
|
+
runRoot?: string;
|
|
17
|
+
allocation?: Partial<ArtifactAllocation>;
|
|
18
|
+
};
|
|
19
|
+
export declare function readInterruptedArtifactAllocation(preparation: PreparationFacts, ownerId: string, runRoot: string): ArtifactAllocation;
|
|
20
|
+
export declare function assertInterruptedArtifactIdentity(allocation: ArtifactAllocation, identity: {
|
|
21
|
+
directory: boolean;
|
|
22
|
+
realPath: string;
|
|
23
|
+
dev: string;
|
|
24
|
+
ino: string;
|
|
25
|
+
}, runRoot: string): void;
|
|
26
|
+
type ArtifactReceiptFacts = {
|
|
27
|
+
schemaVersion?: number;
|
|
28
|
+
kind?: string;
|
|
29
|
+
ownerSessionId?: string;
|
|
30
|
+
repositoryRoot?: string;
|
|
31
|
+
runRoot?: string;
|
|
32
|
+
source?: {
|
|
33
|
+
digestBefore?: string;
|
|
34
|
+
digestAfter?: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export declare function assertOwnedArtifactReceipt(record: Pick<FailedVerificationArtifactSession, "id" | "receiptSha256" | "sourceDigest">, receipt: ArtifactReceiptFacts, receiptSha256: string, root: string, runRoot: string): void;
|
|
38
|
+
export declare function assertFailedArtifactOwnerFacts(record: FailedVerificationArtifactSession, recordPath: string): void;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** 只判定已采样事实;进程探测、路径/字节读取、归档和删除仍由真实adapter负责。 */
|
|
2
|
+
export function inspectInterruptedArtifactOwner(record, recordPath, hasPreparation) {
|
|
3
|
+
const failedHandoff = record.state === "failed-retained" && record.receiptPath === undefined
|
|
4
|
+
&& typeof record.error === "string" && Boolean(record.error.trim());
|
|
5
|
+
const preparing = hasPreparation && (record.state === "preparing" || failedHandoff);
|
|
6
|
+
const previousState = preparing ? (failedHandoff ? "failed-retained" : "preparing") : "consuming";
|
|
7
|
+
if (record.schemaVersion !== 1 || !/^[a-f0-9-]{36}$/u.test(record.id) || (!preparing && record.state !== "consuming")
|
|
8
|
+
|| record.ownerRecordPath !== recordPath || !Number.isSafeInteger(record.pid) || record.pid <= 0
|
|
9
|
+
|| !preparing && (!/^[a-f0-9]{64}$/u.test(record.receiptSha256) || !/^[a-f0-9]{64}$/u.test(record.sourceDigest))
|
|
10
|
+
|| !Array.isArray(record.stages) || !record.stages.length
|
|
11
|
+
|| record.stages.some(stage => typeof stage.command !== "string" || !stage.command.trim()
|
|
12
|
+
|| !["pending", "running", "passed", "failed"].includes(stage.state)
|
|
13
|
+
|| stage.state === "passed" && (!Number.isFinite(stage.durationMs) || stage.durationMs < 0)))
|
|
14
|
+
throw new Error("中断消费owner缺少有效身份或阶段事实。");
|
|
15
|
+
return { preparing, previousState };
|
|
16
|
+
}
|
|
17
|
+
export function readInterruptedArtifactAllocation(preparation, ownerId, runRoot) {
|
|
18
|
+
const allocation = preparation.allocation;
|
|
19
|
+
if (preparation.schemaVersion !== 1 || preparation.status !== "ready" || preparation.ownerSessionId !== ownerId
|
|
20
|
+
|| preparation.runRoot !== runRoot || !Number.isSafeInteger(allocation?.pid) || allocation.pid <= 0
|
|
21
|
+
|| typeof allocation?.dev !== "string" || typeof allocation.ino !== "string"
|
|
22
|
+
|| !/^\d+$/u.test(allocation.dev) || !/^\d+$/u.test(allocation.ino))
|
|
23
|
+
throw new Error("准备交接缺少匹配的producer分配身份。");
|
|
24
|
+
return allocation;
|
|
25
|
+
}
|
|
26
|
+
export function assertInterruptedArtifactIdentity(allocation, identity, runRoot) {
|
|
27
|
+
if (!identity.directory || identity.realPath !== runRoot || identity.dev !== allocation.dev || identity.ino !== allocation.ino) {
|
|
28
|
+
throw new Error("准备交接输入目录已被替换,拒绝接管。");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function assertOwnedArtifactReceipt(record, receipt, receiptSha256, root, runRoot) {
|
|
32
|
+
if (receipt.schemaVersion !== 1 || receipt.kind !== "host-unit-artifacts" || receipt.source?.digestBefore !== record.sourceDigest || receipt.source?.digestAfter !== record.sourceDigest
|
|
33
|
+
|| receipt.ownerSessionId !== record.id || receipt.repositoryRoot !== root || receipt.runRoot !== runRoot
|
|
34
|
+
|| receiptSha256 !== record.receiptSha256)
|
|
35
|
+
throw new Error("验证制品 owner 或收据字节已改变,拒绝回收。");
|
|
36
|
+
}
|
|
37
|
+
export function assertFailedArtifactOwnerFacts(record, recordPath) {
|
|
38
|
+
if (record.schemaVersion !== 1 || !/^[a-f0-9-]{36}$/u.test(record.id)
|
|
39
|
+
|| !["failed-retained", "retiring-failure", "failed-cleaned"].includes(record.state)
|
|
40
|
+
|| !Number.isInteger(record.pid) || record.pid <= 0 || typeof record.error !== "string" || !record.error
|
|
41
|
+
|| record.retentionEligibleAt !== undefined && (!Number.isSafeInteger(record.retentionEligibleAt) || record.retentionEligibleAt <= 0)
|
|
42
|
+
|| record.ownerRecordPath !== undefined && record.ownerRecordPath !== recordPath
|
|
43
|
+
|| !/^[a-f0-9]{64}$/u.test(record.receiptSha256) || !/^[a-f0-9]{64}$/u.test(record.sourceDigest)
|
|
44
|
+
|| !Array.isArray(record.stages) || !record.stages.length
|
|
45
|
+
|| record.stages.some(stage => typeof stage.command !== "string" || !stage.command.trim()
|
|
46
|
+
|| !["pending", "passed", "failed", "interrupted"].includes(stage.state)))
|
|
47
|
+
throw new Error("失败制品 owner 状态或身份无效。");
|
|
48
|
+
}
|