@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,185 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { selectVerificationE2ESuites } from "./e2e-registry-rules.js";
|
|
3
|
+
export const VERIFICATION_CLASSIFIER_VERSION = "1.0.0";
|
|
4
|
+
export const AFFECTED_PROJECT_LIMIT = 12;
|
|
5
|
+
export const AFFECTED_PROJECT_RATIO = 0.4;
|
|
6
|
+
const ALWAYS_RUN_CHECKS = [
|
|
7
|
+
"task-contract",
|
|
8
|
+
"repo-guard",
|
|
9
|
+
"architecture",
|
|
10
|
+
"extension-facades",
|
|
11
|
+
"impact-classifier",
|
|
12
|
+
"e2e-collection",
|
|
13
|
+
];
|
|
14
|
+
const FULL_PATH_RULES = [
|
|
15
|
+
[/^(?:package\.json|pnpm-lock\.yaml|pnpm-workspace\.yaml)$/u, "root_dependency_graph_changed"],
|
|
16
|
+
[/^(?:nx\.json|tsconfig(?:\.[^/]+)?\.json|eslint\.config\.[^/]+|vitest\.[^/]+)$/u, "verification_topology_changed"],
|
|
17
|
+
[/^packages\/host-tooling\/(?:src\/(?:task|verify|agent-tool\/commands\/verify|agent-tool\/commands\/guard)|test\/(?:task|agent-tool\/verify))/u, "verification_infrastructure_changed"],
|
|
18
|
+
[/^products\/host\/src\/server\/application\/(?:verification|workspace)\//u, "delivery_orchestration_changed"],
|
|
19
|
+
[/^(?:playwright\.config\.[^/]+|test\/stories\/|packages\/product-story\/)/u, "story_infrastructure_changed"],
|
|
20
|
+
[/^packages\/(?:product-build|product-test)\//u, "story_infrastructure_changed"],
|
|
21
|
+
[/^packages\/(?:product-runtime|extension-kit)(?:\/|$)/u, "shared_runtime_changed"],
|
|
22
|
+
[/^products\/host\/(?:src\/(?:main|preload)|test\/electron)/u, "host_runtime_changed"],
|
|
23
|
+
[/^packages\/platform-tool\/(?:src\/(?:artifacts|commands\/(?:product|npm-package))|test\/)/u, "artifact_or_publish_pipeline_changed"],
|
|
24
|
+
[/^(?:\.gitmodules|sub-repos\/extensions(?:\/|$))/u, "submodule_topology_changed"],
|
|
25
|
+
[/^(?:AGENTS\.md|\.kuafu\/)/u, "verification_topology_changed"],
|
|
26
|
+
];
|
|
27
|
+
const PROJECT_IDENTITY_FILES = new Set(["package.json", "product.json", "project.json"]);
|
|
28
|
+
const SOURCE_PREFIXES = ["packages/", "products/", "test/", "scripts/"];
|
|
29
|
+
export function buildVerificationImpactPlan(input) {
|
|
30
|
+
const changedPaths = stableChanges(input.changedPaths);
|
|
31
|
+
const projectNames = Object.keys(input.graph.nodes).sort();
|
|
32
|
+
const direct = new Set();
|
|
33
|
+
const escalation = new Set();
|
|
34
|
+
const roots = projectNames
|
|
35
|
+
.map((name) => input.graph.nodes[name])
|
|
36
|
+
.sort((left, right) => right.root.length - left.root.length || left.name.localeCompare(right.name));
|
|
37
|
+
if (input.requestedScope === "full")
|
|
38
|
+
escalation.add("explicit_full");
|
|
39
|
+
if (input.repositoryRecipe)
|
|
40
|
+
escalation.add("native_tool_own_script_full");
|
|
41
|
+
for (const change of changedPaths) {
|
|
42
|
+
if (change.kind === "T")
|
|
43
|
+
escalation.add("path_type_changed");
|
|
44
|
+
for (const candidate of [change.oldPath, change.path].filter((value) => Boolean(value))) {
|
|
45
|
+
const normalized = normalizePath(candidate);
|
|
46
|
+
for (const [pattern, reason] of FULL_PATH_RULES) {
|
|
47
|
+
if (pattern.test(normalized))
|
|
48
|
+
escalation.add(reason);
|
|
49
|
+
}
|
|
50
|
+
if (PROJECT_IDENTITY_FILES.has(normalized.split("/").at(-1) ?? "")) {
|
|
51
|
+
escalation.add("project_identity_changed");
|
|
52
|
+
}
|
|
53
|
+
const owners = roots.filter((node) => node.root === "" || normalized === node.root || normalized.startsWith(`${node.root}/`));
|
|
54
|
+
if (owners.length === 1) {
|
|
55
|
+
direct.add(owners[0].name);
|
|
56
|
+
}
|
|
57
|
+
else if (owners.length > 1 && owners[0].root.length === owners[1].root.length) {
|
|
58
|
+
escalation.add("impact_graph_unreliable");
|
|
59
|
+
}
|
|
60
|
+
else if (SOURCE_PREFIXES.some((prefix) => normalized.startsWith(prefix)) && !isKnownNonProjectPath(normalized)) {
|
|
61
|
+
escalation.add("unowned_source_path");
|
|
62
|
+
if (change.kind === "D" || change.oldPath === candidate)
|
|
63
|
+
escalation.add("historical_owner_unknown");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const affected = collectReverseDependencyClosure(direct, input.graph.dependencies);
|
|
68
|
+
const totalRunnable = projectNames.filter((name) => input.graph.nodes[name].targets.length > 0).length;
|
|
69
|
+
if (affected.size >= AFFECTED_PROJECT_LIMIT
|
|
70
|
+
|| (totalRunnable > 0 && affected.size / totalRunnable >= AFFECTED_PROJECT_RATIO)) {
|
|
71
|
+
escalation.add("affected_scope_too_large");
|
|
72
|
+
}
|
|
73
|
+
const domains = new Set([...affected].map(projectDomain));
|
|
74
|
+
if (["runtime", "host", "extension", "platform"].filter((domain) => domains.has(domain)).length >= 3) {
|
|
75
|
+
escalation.add("cross_domain_change");
|
|
76
|
+
}
|
|
77
|
+
const resolvedMode = escalation.size > 0 ? "full" : "affected";
|
|
78
|
+
const selectedProjects = resolvedMode === "full" ? projectNames : [...affected].sort();
|
|
79
|
+
const targetsByProject = Object.fromEntries(selectedProjects.map((name) => [
|
|
80
|
+
name,
|
|
81
|
+
resolveRequiredProjectTargets(input.graph.nodes[name]),
|
|
82
|
+
]));
|
|
83
|
+
const affectedProducts = selectedProjects.filter((name) => input.graph.nodes[name]?.root.startsWith("products/"));
|
|
84
|
+
const e2eTargets = input.repositoryRecipe || input.e2eRegistryEnabled === false
|
|
85
|
+
? []
|
|
86
|
+
: selectVerificationE2ESuites(selectedProjects, resolvedMode === "full");
|
|
87
|
+
const nxGraphDigest = digest(stableGraph(input.graph));
|
|
88
|
+
const planWithoutDigest = {
|
|
89
|
+
schemaVersion: 1,
|
|
90
|
+
classifierVersion: VERIFICATION_CLASSIFIER_VERSION,
|
|
91
|
+
requestedScope: input.requestedScope,
|
|
92
|
+
resolvedMode,
|
|
93
|
+
headSha: input.headSha ?? "fixture",
|
|
94
|
+
stagedChangesHash: input.stagedChangesHash ?? "fixture",
|
|
95
|
+
changedPaths,
|
|
96
|
+
directProjects: [...direct].sort(),
|
|
97
|
+
affectedProjects: selectedProjects,
|
|
98
|
+
affectedProducts,
|
|
99
|
+
targetsByProject,
|
|
100
|
+
e2eTargets,
|
|
101
|
+
alwaysRunChecks: input.repositoryRecipe ? ["task-contract", "impact-classifier", "repository-verify"] : [...ALWAYS_RUN_CHECKS],
|
|
102
|
+
...(input.repositoryRecipe ? { repositoryRecipe: input.repositoryRecipe } : {}),
|
|
103
|
+
escalationReasons: [...escalation].sort(),
|
|
104
|
+
nxGraphDigest,
|
|
105
|
+
};
|
|
106
|
+
return { ...planWithoutDigest, planDigest: digest(planWithoutDigest) };
|
|
107
|
+
}
|
|
108
|
+
export const STANDARD_PROJECT_TARGETS = ["check-types", "lint", "build", "test"];
|
|
109
|
+
function resolveRequiredProjectTargets(node) {
|
|
110
|
+
const available = new Set(node.targets);
|
|
111
|
+
if (!node.verification) {
|
|
112
|
+
return STANDARD_PROJECT_TARGETS.filter((target) => available.has(target));
|
|
113
|
+
}
|
|
114
|
+
for (const target of STANDARD_PROJECT_TARGETS) {
|
|
115
|
+
if (available.has(target))
|
|
116
|
+
continue;
|
|
117
|
+
const reason = node.verification.targetExemptions[target]?.trim();
|
|
118
|
+
if (!reason) {
|
|
119
|
+
throw new Error(`verification.required_target_missing: ${node.name} (${node.verification.type}) has no ${target} target or explicit exemption`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return STANDARD_PROJECT_TARGETS.filter((target) => available.has(target));
|
|
123
|
+
}
|
|
124
|
+
function collectReverseDependencyClosure(direct, dependencies) {
|
|
125
|
+
const reverse = new Map();
|
|
126
|
+
for (const [source, targets] of Object.entries(dependencies)) {
|
|
127
|
+
for (const target of targets) {
|
|
128
|
+
const dependents = reverse.get(target) ?? new Set();
|
|
129
|
+
dependents.add(source);
|
|
130
|
+
reverse.set(target, dependents);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const result = new Set(direct);
|
|
134
|
+
const queue = [...direct];
|
|
135
|
+
while (queue.length > 0) {
|
|
136
|
+
const current = queue.shift();
|
|
137
|
+
for (const dependent of reverse.get(current) ?? []) {
|
|
138
|
+
if (result.has(dependent))
|
|
139
|
+
continue;
|
|
140
|
+
result.add(dependent);
|
|
141
|
+
queue.push(dependent);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function isKnownNonProjectPath(file) {
|
|
147
|
+
return file.startsWith("packages/host-tooling/scripts/")
|
|
148
|
+
|| file.startsWith("products/host/scripts/")
|
|
149
|
+
|| file.startsWith("test/e2e/");
|
|
150
|
+
}
|
|
151
|
+
function projectDomain(name) {
|
|
152
|
+
if (name.includes("runtime"))
|
|
153
|
+
return "runtime";
|
|
154
|
+
if (name === "host" || name.includes("console-host"))
|
|
155
|
+
return "host";
|
|
156
|
+
if (name.includes("platform") || name.includes("cloud") || name.includes("gateway"))
|
|
157
|
+
return "platform";
|
|
158
|
+
if (name.includes("extension") || name === "data" || name.startsWith("data-") || name.includes("apm") || name.includes("experimentation"))
|
|
159
|
+
return "extension";
|
|
160
|
+
return "product";
|
|
161
|
+
}
|
|
162
|
+
export function stableChanges(changes) {
|
|
163
|
+
return [...changes].map((change) => ({ ...change, path: normalizePath(change.path), ...(change.oldPath ? { oldPath: normalizePath(change.oldPath) } : {}) }))
|
|
164
|
+
.sort((left, right) => `${left.oldPath ?? ""}\0${left.path}\0${left.kind}`.localeCompare(`${right.oldPath ?? ""}\0${right.path}\0${right.kind}`));
|
|
165
|
+
}
|
|
166
|
+
function stableGraph(graph) {
|
|
167
|
+
return {
|
|
168
|
+
nodes: Object.fromEntries(Object.keys(graph.nodes).sort().map((name) => [name, graph.nodes[name]])),
|
|
169
|
+
dependencies: Object.fromEntries(Object.keys(graph.dependencies).sort().map((name) => [name, [...graph.dependencies[name]].sort()])),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function digest(value) {
|
|
173
|
+
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
174
|
+
}
|
|
175
|
+
export function normalizePath(value) {
|
|
176
|
+
const normalized = value.replaceAll("\\", "/").replace(/^\.\//u, "");
|
|
177
|
+
if (!normalized
|
|
178
|
+
|| normalized.startsWith("/")
|
|
179
|
+
|| /^[A-Za-z]:\//u.test(normalized)
|
|
180
|
+
|| normalized.split("/").includes("..")
|
|
181
|
+
|| /[\u0000-\u001f\u007f]/u.test(normalized)) {
|
|
182
|
+
throw new Error(`verification.classification_failed: invalid repository path ${JSON.stringify(value)}`);
|
|
183
|
+
}
|
|
184
|
+
return normalized;
|
|
185
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type VerificationScope, type VerificationImpactPlanV1, type VerificationChangedPath, type VerificationProjectGraph } from "./impact-plan-rules.js";
|
|
2
|
+
export * from "./impact-plan-rules.js";
|
|
3
|
+
export declare function createVerificationImpactPlan(input: {
|
|
4
|
+
workspaceRoot: string;
|
|
5
|
+
requestedScope: VerificationScope;
|
|
6
|
+
changedPaths?: VerificationChangedPath[];
|
|
7
|
+
graph?: VerificationProjectGraph;
|
|
8
|
+
}): Promise<VerificationImpactPlanV1>;
|
|
9
|
+
export declare function readStagedChanges(workspaceRoot: string): Promise<VerificationChangedPath[]>;
|
|
10
|
+
export declare function readNxProjectGraph(workspaceRoot: string): Promise<VerificationProjectGraph>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { createCheckoutVerificationEnvironment } from "./checkout-environment.js";
|
|
7
|
+
import { readNativeToolVerificationRecipe } from "./repository-recipe.js";
|
|
8
|
+
import { buildVerificationImpactPlan, stableChanges, normalizePath, STANDARD_PROJECT_TARGETS } from "./impact-plan-rules.js";
|
|
9
|
+
export * from "./impact-plan-rules.js";
|
|
10
|
+
export async function createVerificationImpactPlan(input) {
|
|
11
|
+
const changedPaths = stableChanges(input.changedPaths ?? await readStagedChanges(input.workspaceRoot));
|
|
12
|
+
const repositoryRecipe = await readNativeToolVerificationRecipe(input.workspaceRoot);
|
|
13
|
+
const graph = input.graph ?? await readNxProjectGraph(input.workspaceRoot);
|
|
14
|
+
const [headSha, stagedChangesHash] = await Promise.all([
|
|
15
|
+
gitText(input.workspaceRoot, ["rev-parse", "HEAD"]),
|
|
16
|
+
gitBuffer(input.workspaceRoot, ["diff", "--cached", "--raw", "--no-abbrev"])
|
|
17
|
+
.then((value) => createHash("sha256").update(value).digest("hex")),
|
|
18
|
+
]);
|
|
19
|
+
return buildVerificationImpactPlan({
|
|
20
|
+
requestedScope: input.requestedScope,
|
|
21
|
+
changedPaths,
|
|
22
|
+
graph,
|
|
23
|
+
headSha: headSha.trim(),
|
|
24
|
+
stagedChangesHash,
|
|
25
|
+
e2eRegistryEnabled: existsSync(path.join(input.workspaceRoot, "packages/platform-tool/package.json")),
|
|
26
|
+
...(repositoryRecipe ? { repositoryRecipe } : {}),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export async function readStagedChanges(workspaceRoot) {
|
|
30
|
+
const output = await gitBuffer(workspaceRoot, [
|
|
31
|
+
"diff", "--cached", "--name-status", "-z", "-M", "-C", "--diff-filter=ACDMRTUXB",
|
|
32
|
+
]);
|
|
33
|
+
const fields = output.toString("utf8").split("\0");
|
|
34
|
+
const changes = [];
|
|
35
|
+
for (let index = 0; index < fields.length;) {
|
|
36
|
+
const rawStatus = fields[index++];
|
|
37
|
+
if (!rawStatus)
|
|
38
|
+
break;
|
|
39
|
+
const kind = rawStatus[0];
|
|
40
|
+
if (kind === "U" || kind === "X" || kind === "B") {
|
|
41
|
+
throw new Error(`verification.classification_failed: unsupported staged status ${rawStatus}`);
|
|
42
|
+
}
|
|
43
|
+
if (kind === "R" || kind === "C") {
|
|
44
|
+
const oldPath = fields[index++];
|
|
45
|
+
const nextPath = fields[index++];
|
|
46
|
+
if (!oldPath || !nextPath)
|
|
47
|
+
throw new Error("verification.classification_failed: malformed rename/copy record");
|
|
48
|
+
changes.push({ kind, oldPath: normalizePath(oldPath), path: normalizePath(nextPath) });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const file = fields[index++];
|
|
52
|
+
if (!file || !["A", "D", "M", "T"].includes(kind)) {
|
|
53
|
+
throw new Error(`verification.classification_failed: malformed staged status ${rawStatus}`);
|
|
54
|
+
}
|
|
55
|
+
changes.push({ kind: kind, path: normalizePath(file) });
|
|
56
|
+
}
|
|
57
|
+
return stableChanges(changes);
|
|
58
|
+
}
|
|
59
|
+
export async function readNxProjectGraph(workspaceRoot) {
|
|
60
|
+
if (!existsSync(`${workspaceRoot}/nx.json`)) {
|
|
61
|
+
return {
|
|
62
|
+
nodes: { repository: { name: "repository", root: "", targets: ["verify"], tags: [] } },
|
|
63
|
+
dependencies: { repository: [] },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const raw = await execFileText("pnpm", ["exec", "nx", "graph", "--file=stdout"], workspaceRoot)
|
|
67
|
+
.catch((error) => {
|
|
68
|
+
throw new Error("verification.nx_graph_unavailable: unable to read Nx project graph", { cause: error });
|
|
69
|
+
});
|
|
70
|
+
const parsed = JSON.parse(raw);
|
|
71
|
+
if (!parsed.graph?.nodes || !parsed.graph.dependencies) {
|
|
72
|
+
throw new Error("verification.nx_graph_unavailable: invalid Nx graph payload");
|
|
73
|
+
}
|
|
74
|
+
const nodes = {};
|
|
75
|
+
for (const [name, node] of Object.entries(parsed.graph.nodes)) {
|
|
76
|
+
const root = node.data?.root;
|
|
77
|
+
if (!root)
|
|
78
|
+
throw new Error(`verification.nx_graph_unavailable: project ${name} has no root`);
|
|
79
|
+
nodes[name] = {
|
|
80
|
+
name,
|
|
81
|
+
root: normalizePath(root).replace(/\/$/u, ""),
|
|
82
|
+
targets: Object.keys(node.data?.targets ?? {}).sort(),
|
|
83
|
+
tags: [...(node.data?.tags ?? [])].sort(),
|
|
84
|
+
...await readProjectVerificationContract(workspaceRoot, normalizePath(root).replace(/\/$/u, "")),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const dependencies = Object.fromEntries(Object.keys(nodes).sort().map((name) => [
|
|
88
|
+
name,
|
|
89
|
+
[...new Set((parsed.graph.dependencies[name] ?? []).map((edge) => edge.target).filter((target) => Boolean(target && nodes[target])))].sort(),
|
|
90
|
+
]));
|
|
91
|
+
return { nodes, dependencies };
|
|
92
|
+
}
|
|
93
|
+
async function readProjectVerificationContract(workspaceRoot, projectRoot) {
|
|
94
|
+
const packagePath = path.join(workspaceRoot, projectRoot, "package.json");
|
|
95
|
+
if (!existsSync(packagePath))
|
|
96
|
+
return {};
|
|
97
|
+
const value = JSON.parse(await readFile(packagePath, "utf8"));
|
|
98
|
+
const verification = value.kuafucloud?.verification;
|
|
99
|
+
if (!verification)
|
|
100
|
+
return {};
|
|
101
|
+
if (typeof verification.type !== "string" || !/^[a-z][a-z0-9-]*$/u.test(verification.type)) {
|
|
102
|
+
throw new Error(`verification.classification_failed: invalid verification type in ${projectRoot}/package.json`);
|
|
103
|
+
}
|
|
104
|
+
const exemptions = verification.targetExemptions ?? {};
|
|
105
|
+
if (!exemptions || typeof exemptions !== "object" || Array.isArray(exemptions)) {
|
|
106
|
+
throw new Error(`verification.classification_failed: invalid target exemptions in ${projectRoot}/package.json`);
|
|
107
|
+
}
|
|
108
|
+
const targetExemptions = {};
|
|
109
|
+
for (const [target, reason] of Object.entries(exemptions)) {
|
|
110
|
+
if (!STANDARD_PROJECT_TARGETS.includes(target) || typeof reason !== "string" || !reason.trim()) {
|
|
111
|
+
throw new Error(`verification.classification_failed: invalid ${target} exemption in ${projectRoot}/package.json`);
|
|
112
|
+
}
|
|
113
|
+
targetExemptions[target] = reason.trim();
|
|
114
|
+
}
|
|
115
|
+
return { verification: { type: verification.type, targetExemptions } };
|
|
116
|
+
}
|
|
117
|
+
async function gitText(workspaceRoot, args) {
|
|
118
|
+
return (await gitBuffer(workspaceRoot, args)).toString("utf8");
|
|
119
|
+
}
|
|
120
|
+
function gitBuffer(cwd, args) {
|
|
121
|
+
// Git hook 会继承提交命令的配置与索引环境;计划必须只依赖指定仓库的
|
|
122
|
+
// 既有暂存树;固定计划原有的路径编码,不能受调用入口影响。
|
|
123
|
+
const env = { ...process.env };
|
|
124
|
+
for (const key of [
|
|
125
|
+
"GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_CONFIG", "GIT_CONFIG_PARAMETERS",
|
|
126
|
+
"GIT_CONFIG_COUNT", "GIT_OBJECT_DIRECTORY", "GIT_DIR", "GIT_WORK_TREE",
|
|
127
|
+
"GIT_IMPLICIT_WORK_TREE", "GIT_GRAFT_FILE", "GIT_INDEX_FILE",
|
|
128
|
+
"GIT_NO_REPLACE_OBJECTS", "GIT_REPLACE_REF_BASE", "GIT_PREFIX",
|
|
129
|
+
"GIT_SHALLOW_FILE", "GIT_COMMON_DIR",
|
|
130
|
+
])
|
|
131
|
+
delete env[key];
|
|
132
|
+
env.GIT_OPTIONAL_LOCKS = "0";
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
execFile("git", ["-c", "core.quotePath=true", ...args], { cwd, env, encoding: "buffer", maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
|
|
135
|
+
if (error)
|
|
136
|
+
reject(error);
|
|
137
|
+
else
|
|
138
|
+
resolve(stdout);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function execFileText(command, args, cwd) {
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
execFile(command, args, { cwd, env: createCheckoutVerificationEnvironment(cwd, process.env), encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
|
|
145
|
+
if (error)
|
|
146
|
+
reject(error);
|
|
147
|
+
else
|
|
148
|
+
resolve(stdout);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from "./commit-candidate-rules.js";
|
|
2
|
+
export * from "./commit-gate.js";
|
|
3
|
+
export * from "./delivery-verification.js";
|
|
4
|
+
export * from "./verification-candidate.js";
|
|
5
|
+
export * from "./verification-identity.js";
|
|
6
|
+
export * from "./verification-proof-rules.js";
|
|
7
|
+
export * from "./verify-full-command-rules.js";
|
|
8
|
+
export * from "./verify-full-runner.js";
|
|
9
|
+
export * from "./impact-plan.js";
|
|
10
|
+
export * from "./impact-plan-rules.js";
|
|
11
|
+
export * from "./checkout-environment.js";
|
|
12
|
+
export * from "./checkout-environment-rules.js";
|
|
13
|
+
export * from "./e2e-registry.js";
|
|
14
|
+
export * from "./e2e-registry-rules.js";
|
|
15
|
+
export * from "./full-command.js";
|
|
16
|
+
export * from "./repository-recipe.js";
|
|
17
|
+
export * from "./repository-recipe-rules.js";
|
|
18
|
+
export * from "./static-validation-rules.js";
|
|
19
|
+
export * from "./artifact-session.js";
|
|
20
|
+
export * from "./artifact-recovery-rules.js";
|
|
21
|
+
export * from "./story-selection-rules.js";
|
|
22
|
+
export * from "./types.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * from "./commit-candidate-rules.js";
|
|
2
|
+
export * from "./commit-gate.js";
|
|
3
|
+
export * from "./delivery-verification.js";
|
|
4
|
+
export * from "./verification-candidate.js";
|
|
5
|
+
export * from "./verification-identity.js";
|
|
6
|
+
export * from "./verification-proof-rules.js";
|
|
7
|
+
export * from "./verify-full-command-rules.js";
|
|
8
|
+
export * from "./verify-full-runner.js";
|
|
9
|
+
export * from "./impact-plan.js";
|
|
10
|
+
export * from "./impact-plan-rules.js";
|
|
11
|
+
export * from "./checkout-environment.js";
|
|
12
|
+
export * from "./checkout-environment-rules.js";
|
|
13
|
+
export * from "./e2e-registry.js";
|
|
14
|
+
export * from "./e2e-registry-rules.js";
|
|
15
|
+
export * from "./full-command.js";
|
|
16
|
+
export * from "./repository-recipe.js";
|
|
17
|
+
export * from "./repository-recipe-rules.js";
|
|
18
|
+
export * from "./static-validation-rules.js";
|
|
19
|
+
export * from "./artifact-session.js";
|
|
20
|
+
export * from "./artifact-recovery-rules.js";
|
|
21
|
+
export * from "./story-selection-rules.js";
|
|
22
|
+
export * from "./types.js";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type NativeToolVerificationRecipe = {
|
|
2
|
+
kind: "native-tool";
|
|
3
|
+
tool: "native-bundler";
|
|
4
|
+
};
|
|
5
|
+
export type NativeToolWorkspaceFacts = {
|
|
6
|
+
manifestIsTrackedFile: boolean;
|
|
7
|
+
packagesRootIsInternalDirectory: boolean;
|
|
8
|
+
patterns: unknown;
|
|
9
|
+
members: Array<{
|
|
10
|
+
path: string;
|
|
11
|
+
internalDirectory: boolean;
|
|
12
|
+
manifestIsTrackedFile: boolean;
|
|
13
|
+
manifest: unknown;
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
/** 先核对显式声明,再采样该工具仓的物理闭包;不以文件缺失推导种类。 */
|
|
17
|
+
export declare function resolveNativeToolVerificationDeclaration(rootPackage: unknown): NativeToolVerificationRecipe | null;
|
|
18
|
+
/** 递归脚本的成员范围与包脚本同属正式配方,不能让 -r 静默略过检查。 */
|
|
19
|
+
export declare function resolveNativeToolVerificationRecipe(input: {
|
|
20
|
+
rootPackage: unknown;
|
|
21
|
+
conflictingPaths: string[];
|
|
22
|
+
packageManifests: Array<{
|
|
23
|
+
path: string;
|
|
24
|
+
manifest: unknown;
|
|
25
|
+
}>;
|
|
26
|
+
workspace: NativeToolWorkspaceFacts;
|
|
27
|
+
}): NativeToolVerificationRecipe | null;
|
|
28
|
+
export declare function assertNativeToolVerificationRuntime(recipe: NativeToolVerificationRecipe | undefined, runtime: {
|
|
29
|
+
node: string;
|
|
30
|
+
pnpm: string;
|
|
31
|
+
}): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
function record(value) {
|
|
2
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
3
|
+
}
|
|
4
|
+
function fail(reason) { throw new Error(`verification.native_tool_contract_invalid: ${reason}`); }
|
|
5
|
+
/** 先核对显式声明,再采样该工具仓的物理闭包;不以文件缺失推导种类。 */
|
|
6
|
+
export function resolveNativeToolVerificationDeclaration(rootPackage) {
|
|
7
|
+
const root = record(rootPackage);
|
|
8
|
+
const verification = record(record(root?.kuafucloud)?.verification);
|
|
9
|
+
if (!verification || !Object.hasOwn(verification, "kind"))
|
|
10
|
+
return null;
|
|
11
|
+
if (verification.kind !== "native-tool" || verification.tool !== "native-bundler"
|
|
12
|
+
|| Object.keys(verification).some(key => !["kind", "tool"].includes(key)))
|
|
13
|
+
fail("未知工具仓验证合同");
|
|
14
|
+
if (!root)
|
|
15
|
+
return fail("根 package 身份不匹配");
|
|
16
|
+
if (root.name !== "kuafu-native-bundler" || root.private !== true
|
|
17
|
+
|| typeof root.version !== "string" || !/^\d+\.\d+\.\d+(?:[-+][\w.-]+)?$/u.test(root.version))
|
|
18
|
+
fail("根 package 身份不匹配");
|
|
19
|
+
if (root.packageManager !== "pnpm@11.8.0" || record(root.engines)?.node !== "22.22.0")
|
|
20
|
+
fail("工具仓必须固定 Node/pnpm");
|
|
21
|
+
const scripts = record(root.scripts);
|
|
22
|
+
if (scripts?.verify !== "pnpm run check-types && pnpm run lint && pnpm run test"
|
|
23
|
+
|| ["check-types", "lint", "test"].some(target => scripts[target] !== `pnpm -r run ${target}`))
|
|
24
|
+
fail("缺少受支持的 Bundler 自有全量脚本");
|
|
25
|
+
return { kind: "native-tool", tool: "native-bundler" };
|
|
26
|
+
}
|
|
27
|
+
/** 递归脚本的成员范围与包脚本同属正式配方,不能让 -r 静默略过检查。 */
|
|
28
|
+
export function resolveNativeToolVerificationRecipe(input) {
|
|
29
|
+
const recipe = resolveNativeToolVerificationDeclaration(input.rootPackage);
|
|
30
|
+
if (!recipe)
|
|
31
|
+
return null;
|
|
32
|
+
if (input.conflictingPaths.length)
|
|
33
|
+
fail(`工具仓与产品/平台验证合同冲突:${input.conflictingPaths.join(", ")}`);
|
|
34
|
+
const workspace = input.workspace;
|
|
35
|
+
if (!workspace.manifestIsTrackedFile || !workspace.packagesRootIsInternalDirectory
|
|
36
|
+
|| !Array.isArray(workspace.patterns) || workspace.patterns.length !== 1 || workspace.patterns[0] !== "packages/*"
|
|
37
|
+
|| workspace.members.length === 0)
|
|
38
|
+
fail("工具仓必须使用自身真实 packages/* 工作区");
|
|
39
|
+
for (const member of workspace.members) {
|
|
40
|
+
const scripts = record(record(member.manifest)?.scripts);
|
|
41
|
+
if (!/^packages\/[^/.][^/]*$/u.test(member.path) || !member.internalDirectory || !member.manifestIsTrackedFile
|
|
42
|
+
|| ["check-types", "lint", "test"].some(target => typeof scripts?.[target] !== "string" || !scripts[target].trim())) {
|
|
43
|
+
fail(`工作区成员必须是仓库内真实包并拥有完整检查:${member.path}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const entry of [{ path: "package.json", manifest: input.rootPackage }, ...input.packageManifests]) {
|
|
47
|
+
const manifest = record(entry.manifest);
|
|
48
|
+
if (!manifest)
|
|
49
|
+
return fail(`无效 package 清单:${entry.path}`);
|
|
50
|
+
if (manifest.name === "@kuafucloud/product-build"
|
|
51
|
+
|| ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"].some(key => Object.hasOwn(record(manifest[key]) ?? {}, "@kuafucloud/product-build")))
|
|
52
|
+
fail(`工具仓不能声明 ProductBuild:${entry.path}`);
|
|
53
|
+
}
|
|
54
|
+
return recipe;
|
|
55
|
+
}
|
|
56
|
+
export function assertNativeToolVerificationRuntime(recipe, runtime) {
|
|
57
|
+
if (recipe && (runtime.node !== "v22.22.0" || runtime.pnpm !== "11.8.0")) {
|
|
58
|
+
throw new Error("verification.native_tool_runtime_mismatch: Bundler 验证必须使用固定 Node 22.22.0 / pnpm 11.8.0。");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { lstat, readFile, readdir, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { execFile } from "node:child_process";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { parse } from "yaml";
|
|
7
|
+
import { resolveNativeToolVerificationDeclaration, resolveNativeToolVerificationRecipe } from "./repository-recipe-rules.js";
|
|
8
|
+
/** 采样当前 checkout 自己的清单,不从父目录或安装态 SDK 推导仓库类型。 */
|
|
9
|
+
export async function readNativeToolVerificationRecipe(workspaceRoot) {
|
|
10
|
+
const rootPackagePath = path.join(workspaceRoot, "package.json");
|
|
11
|
+
if (!existsSync(rootPackagePath))
|
|
12
|
+
return null;
|
|
13
|
+
const rootPackage = JSON.parse(await readFile(rootPackagePath, "utf8"));
|
|
14
|
+
if (!resolveNativeToolVerificationDeclaration(rootPackage))
|
|
15
|
+
return null;
|
|
16
|
+
const root = await realpath(workspaceRoot);
|
|
17
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_")));
|
|
18
|
+
const git = async (args) => (await promisify(execFile)("git", args, { cwd: root, env: { ...env, GIT_OPTIONAL_LOCKS: "0" }, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 })).stdout;
|
|
19
|
+
if (await realpath((await git(["rev-parse", "--show-toplevel"])).trim()) !== root) {
|
|
20
|
+
throw new Error("verification.native_tool_contract_invalid: 必须使用工具仓自身 Git root。");
|
|
21
|
+
}
|
|
22
|
+
const files = (await git(["ls-files", "--cached", "-z"])).split("\0").filter(Boolean);
|
|
23
|
+
if (!files.includes("package.json"))
|
|
24
|
+
throw new Error("verification.native_tool_contract_invalid: 根 package 必须进入候选树。");
|
|
25
|
+
const conflictingPaths = [...new Set([
|
|
26
|
+
...files.filter(file => /(?:^|\/)(?:product|kuafu)\.json$/u.test(file)),
|
|
27
|
+
...["kuafu.json", "product.json", "products", "nx.json", "guard.config.ts", "packages/platform-tool/package.json", "node_modules/@kuafucloud/product-build/package.json"]
|
|
28
|
+
.filter(file => existsSync(path.join(root, file))),
|
|
29
|
+
])].sort();
|
|
30
|
+
const packageManifests = [];
|
|
31
|
+
for (const file of files.filter(file => file === "package.json" || file.endsWith("/package.json"))) {
|
|
32
|
+
const target = path.join(root, file);
|
|
33
|
+
if (!(await lstat(target)).isFile() || path.relative(root, await realpath(target)).startsWith(`..${path.sep}`)) {
|
|
34
|
+
throw new Error("verification.native_tool_contract_invalid: package 必须是仓库内真实文件。");
|
|
35
|
+
}
|
|
36
|
+
packageManifests.push({ path: file, manifest: JSON.parse(await readFile(target, "utf8")) });
|
|
37
|
+
}
|
|
38
|
+
const internalDirectory = async (relative) => {
|
|
39
|
+
const target = path.join(root, relative);
|
|
40
|
+
const stat = await lstat(target).catch(() => null);
|
|
41
|
+
return Boolean(stat?.isDirectory() && !path.relative(root, await realpath(target)).startsWith(`..${path.sep}`));
|
|
42
|
+
};
|
|
43
|
+
const workspacePath = path.join(root, "pnpm-workspace.yaml");
|
|
44
|
+
const manifestIsTrackedFile = files.includes("pnpm-workspace.yaml") && (await lstat(workspacePath).catch(() => null))?.isFile() === true;
|
|
45
|
+
const workspaceManifest = manifestIsTrackedFile ? parse(await readFile(workspacePath, "utf8")) : null;
|
|
46
|
+
const packagesRootIsInternalDirectory = await internalDirectory("packages");
|
|
47
|
+
const members = [];
|
|
48
|
+
if (packagesRootIsInternalDirectory) {
|
|
49
|
+
for (const name of (await readdir(path.join(root, "packages"))).sort()) {
|
|
50
|
+
const memberPath = `packages/${name}`;
|
|
51
|
+
const manifest = packageManifests.find(entry => entry.path === `${memberPath}/package.json`);
|
|
52
|
+
members.push({ path: memberPath, internalDirectory: await internalDirectory(memberPath),
|
|
53
|
+
manifestIsTrackedFile: Boolean(manifest), manifest: manifest?.manifest });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return resolveNativeToolVerificationRecipe({ rootPackage, conflictingPaths, packageManifests,
|
|
57
|
+
workspace: { manifestIsTrackedFile, packagesRootIsInternalDirectory, patterns: workspaceManifest?.packages, members } });
|
|
58
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type StaticValidationTarget = "check-types" | "lint";
|
|
2
|
+
export declare function readStaticValidationTarget(value: unknown): StaticValidationTarget;
|
|
3
|
+
/** 校验Nx实际解析结果,项目覆盖或新增依赖不能把写入任务混入只读并行池。 */
|
|
4
|
+
export declare function assertStaticValidationGraph(value: unknown, requested: StaticValidationTarget): {
|
|
5
|
+
checks: number;
|
|
6
|
+
exclusiveDependencies: number;
|
|
7
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function readStaticValidationTarget(value) {
|
|
2
|
+
if (value !== "check-types" && value !== "lint")
|
|
3
|
+
throw new Error("静态并行入口只接受check-types或lint。");
|
|
4
|
+
return value;
|
|
5
|
+
}
|
|
6
|
+
function record(value) {
|
|
7
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
8
|
+
throw new Error("静态验证缺少有效Nx任务图。");
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
/** 校验Nx实际解析结果,项目覆盖或新增依赖不能把写入任务混入只读并行池。 */
|
|
12
|
+
export function assertStaticValidationGraph(value, requested) {
|
|
13
|
+
const graph = record(record(value).tasks), tasks = record(graph.tasks), dependencies = record(graph.dependencies);
|
|
14
|
+
const ids = Object.keys(tasks);
|
|
15
|
+
if (!ids.length || Object.keys(dependencies).length !== ids.length)
|
|
16
|
+
throw new Error("静态验证Nx任务/依赖集合不完整。");
|
|
17
|
+
let checks = 0, exclusiveDependencies = 0;
|
|
18
|
+
for (const id of ids) {
|
|
19
|
+
const task = record(tasks[id]), target = record(task.target);
|
|
20
|
+
if (typeof target.project !== "string" || !target.project || typeof target.target !== "string"
|
|
21
|
+
|| typeof task.parallelism !== "boolean" || !Array.isArray(dependencies[id])
|
|
22
|
+
|| dependencies[id].some(dependency => typeof dependency !== "string" || !Object.hasOwn(tasks, dependency))) {
|
|
23
|
+
throw new Error(`静态验证Nx任务或依赖无效:${id}`);
|
|
24
|
+
}
|
|
25
|
+
if (target.target === requested)
|
|
26
|
+
checks++;
|
|
27
|
+
else {
|
|
28
|
+
if (task.parallelism !== false)
|
|
29
|
+
throw new Error(`静态并行前必须将依赖任务设为排他:${id}`);
|
|
30
|
+
exclusiveDependencies++;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!checks)
|
|
34
|
+
throw new Error("Nx任务图没有实际请求的静态检查。");
|
|
35
|
+
return { checks, exclusiveDependencies };
|
|
36
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export type StoryCatalogItemSummary = {
|
|
2
|
+
id: string;
|
|
3
|
+
product: string;
|
|
4
|
+
frontends: Record<string, {
|
|
5
|
+
product: string;
|
|
6
|
+
account: number;
|
|
7
|
+
}>;
|
|
8
|
+
};
|
|
9
|
+
export type StorySelectionResult = {
|
|
10
|
+
mode: "none" | "affected" | "all";
|
|
11
|
+
storyIds: string[];
|
|
12
|
+
affectedProducts: string[];
|
|
13
|
+
reasons: string[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* 方案 19 §7.6 fail-closed 受影响故事选择规则:
|
|
17
|
+
* 1. 仅文档 / 技能 / 纯单测 → mode = "none";
|
|
18
|
+
* 2. 公共预览链路、锁文件、根配置、或无法归类的改动 → mode = "all"(fail-closed);
|
|
19
|
+
* 3. 受影响产品集合取所有故事 `product` 与 `frontends[*].product` 的并集命中。
|
|
20
|
+
*/
|
|
21
|
+
export declare function selectAffectedStories(input: {
|
|
22
|
+
changedPaths: readonly string[];
|
|
23
|
+
affectedProductsFromGraph: readonly string[];
|
|
24
|
+
catalog: readonly StoryCatalogItemSummary[];
|
|
25
|
+
forceAll?: boolean;
|
|
26
|
+
}): StorySelectionResult;
|
|
27
|
+
export type StoryVerificationProof = {
|
|
28
|
+
schemaVersion: 1;
|
|
29
|
+
mode: "none" | "affected" | "all";
|
|
30
|
+
treeHash: string;
|
|
31
|
+
baselineKey: string;
|
|
32
|
+
catalogDigest: string;
|
|
33
|
+
artifactDigests: Record<string, string>;
|
|
34
|
+
driver: "cdp";
|
|
35
|
+
source: "staged" | "push" | "agent";
|
|
36
|
+
executedStoryIds: string[];
|
|
37
|
+
passed: number;
|
|
38
|
+
failed: number;
|
|
39
|
+
skipped: number;
|
|
40
|
+
budgetExceeded: number;
|
|
41
|
+
retries: number;
|
|
42
|
+
flakyStoryIds: string[];
|
|
43
|
+
};
|
|
44
|
+
/** 校验工作区本地数据后端:故事与交付只支持 DO SQLite,Postgres 后端直接拒绝。 */
|
|
45
|
+
export declare function assertStoryLocalBackendAllowed(backend: string | undefined): void;
|
|
46
|
+
/** 校验交付/提交时的故事验证证明是否完整、匹配当前暂存树且无跳过或超限。 */
|
|
47
|
+
export declare function validateStoryVerificationProof(proof: Partial<StoryVerificationProof> | null | undefined, expected: {
|
|
48
|
+
treeHash: string;
|
|
49
|
+
expectedStoryIds: readonly string[];
|
|
50
|
+
catalogDigest?: string;
|
|
51
|
+
baselineKey?: string;
|
|
52
|
+
}): {
|
|
53
|
+
ok: true;
|
|
54
|
+
} | {
|
|
55
|
+
ok: false;
|
|
56
|
+
reason: string;
|
|
57
|
+
};
|