sdd-flow-kit 1.3.15 → 1.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/cli.js +16 -0
  2. package/dist/core/apiFieldContract.js +90 -0
  3. package/dist/core/demoImplementationCoverage.js +242 -0
  4. package/dist/core/e2eAssertionDepth.js +233 -0
  5. package/dist/core/prdArtifacts.js +12 -1
  6. package/dist/core/prdCoverage.js +46 -9
  7. package/dist/core/prdSemanticDiff.js +7 -4
  8. package/dist/core/syncSourcePrd.js +181 -0
  9. package/dist/core/techDocQuality.js +87 -0
  10. package/dist/core/testAssertionAnalysis.js +213 -0
  11. package/dist/core/writeNext.js +1 -1
  12. package/dist/index.js +6 -1
  13. package/dist/steps/runGate.js +49 -5
  14. package/dist/steps/runSyncSourcePrd.js +38 -0
  15. package/dist/steps/runValidate.js +4 -2
  16. package/dist/steps/setupProject.js +3 -0
  17. package/dist/steps/startFlow.js +7 -2
  18. package/package.json +2 -1
  19. package/scripts/assert-active-openspec-change.sh +114 -0
  20. package/scripts/assess-api-field-contract.mjs +119 -0
  21. package/scripts/assess-demo-implementation-coverage.mjs +188 -0
  22. package/scripts/assess-e2e-assertion-depth.mjs +146 -0
  23. package/scripts/assess-playwright-e2e-coverage.mjs +244 -0
  24. package/scripts/assess-tech-doc-quality.mjs +91 -0
  25. package/scripts/install-git-hooks.sh +27 -0
  26. package/scripts/lib/test-assertion-analysis.mjs +134 -0
  27. package/scripts/resolve-playwright-e2e-scope.mjs +248 -0
  28. package/src/templates/artifacts/01-adi-doc-skill-/346/214/207/345/274/225.template.md +6 -2
  29. package/src/templates/artifacts/02-/351/234/200/346/261/202/345/210/206/346/236/220/346/217/220/347/244/272/350/257/215.template.md +5 -0
  30. package/src/templates/artifacts/04-/346/212/200/346/234/257/346/226/207/346/241/243.template.md +14 -0
  31. package/src/templates/artifacts/05-/351/252/214/346/224/266/346/270/205/345/215/225.template.md +2 -2
  32. package/src/templates/artifacts/06-openspec-/346/217/220/346/241/210/350/215/211/347/250/277.template.md +2 -2
  33. package/src/templates/artifacts/07-opsx-auto-chain.template.md +2 -2
  34. package/src/templates/artifacts/08-PRD/344/270/200/350/207/264/346/200/247/345/244/215/346/237/245/346/212/245/345/221/212.template.md +2 -0
  35. package/src/templates/artifacts/design-demo-reference.template.md +24 -0
  36. package/src/templates/prompts/02-sdd-loop-prompt.md +25 -11
  37. package/src/templates/prompts/04-tech-doc-fill-prompt.md +71 -0
  38. package/src/templates/skills/confluence-doc.py.template +64 -25
  39. package/src/templates/skills/demo-reference-mandatory-workflow.md +77 -0
  40. package/src/templates/skills/doc-skill.SKILL.md.template +13 -6
  41. package/src/templates/skills/layered-testing-strategy.md +13 -6
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadRunSession = exports.resolveRunDir = exports.runDeliver = exports.runPropose = exports.runPhaseAdvance = exports.printGateResult = exports.runGate = exports.toRunOptions = exports.parseInvokeText = exports.runDoctor = exports.setupProject = exports.runFlow = exports.startFlowScaffold = void 0;
3
+ exports.loadRunSession = exports.resolveRunDir = exports.tryAutoSyncSourcePrd = exports.syncSourcePrdFromDocs = exports.runSyncSourcePrd = exports.runDeliver = exports.runPropose = exports.runPhaseAdvance = exports.printGateResult = exports.runGate = exports.toRunOptions = exports.parseInvokeText = exports.runDoctor = exports.setupProject = exports.runFlow = exports.startFlowScaffold = void 0;
4
4
  var startFlow_1 = require("./steps/startFlow");
5
5
  Object.defineProperty(exports, "startFlowScaffold", { enumerable: true, get: function () { return startFlow_1.startFlowScaffold; } });
6
6
  var runFlow_1 = require("./steps/runFlow");
@@ -21,6 +21,11 @@ var runPropose_1 = require("./steps/runPropose");
21
21
  Object.defineProperty(exports, "runPropose", { enumerable: true, get: function () { return runPropose_1.runPropose; } });
22
22
  var runDeliver_1 = require("./steps/runDeliver");
23
23
  Object.defineProperty(exports, "runDeliver", { enumerable: true, get: function () { return runDeliver_1.runDeliver; } });
24
+ var runSyncSourcePrd_1 = require("./steps/runSyncSourcePrd");
25
+ Object.defineProperty(exports, "runSyncSourcePrd", { enumerable: true, get: function () { return runSyncSourcePrd_1.runSyncSourcePrd; } });
26
+ var syncSourcePrd_1 = require("./core/syncSourcePrd");
27
+ Object.defineProperty(exports, "syncSourcePrdFromDocs", { enumerable: true, get: function () { return syncSourcePrd_1.syncSourcePrdFromDocs; } });
28
+ Object.defineProperty(exports, "tryAutoSyncSourcePrd", { enumerable: true, get: function () { return syncSourcePrd_1.tryAutoSyncSourcePrd; } });
24
29
  var resolveRun_1 = require("./steps/resolveRun");
25
30
  Object.defineProperty(exports, "resolveRunDir", { enumerable: true, get: function () { return resolveRun_1.resolveRunDir; } });
26
31
  Object.defineProperty(exports, "loadRunSession", { enumerable: true, get: function () { return resolveRun_1.loadRunSession; } });
@@ -7,11 +7,14 @@ exports.runGate = runGate;
7
7
  exports.printGateResult = printGateResult;
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const prdArtifacts_1 = require("../core/prdArtifacts");
10
+ const syncSourcePrd_1 = require("../core/syncSourcePrd");
10
11
  const prdCoverage_1 = require("../core/prdCoverage");
11
12
  const gitDiffGuard_1 = require("../core/gitDiffGuard");
12
13
  const acMatrix_1 = require("../core/acMatrix");
13
14
  const projectRoots_1 = require("../core/projectRoots");
14
15
  const openspecGate_1 = require("../core/openspecGate");
16
+ const demoImplementationCoverage_1 = require("../core/demoImplementationCoverage");
17
+ const techDocQuality_1 = require("../core/techDocQuality");
15
18
  const resolveRun_1 = require("./resolveRun");
16
19
  const fs_1 = require("../core/fs");
17
20
  function fail(checks, expect, runId) {
@@ -73,7 +76,7 @@ async function resolveImplRoots(projectRoot) {
73
76
  * 脚本门禁:失败时 process.exitCode=1,成功 exit 0。
74
77
  */
75
78
  async function runGate(options) {
76
- var _a, _b, _c;
79
+ var _a, _b, _c, _d, _e, _f, _g;
77
80
  const loaded = await (0, resolveRun_1.loadRunSession)(options.projectRoot, options.runId);
78
81
  const checks = [];
79
82
  if (!loaded || !loaded.resolved) {
@@ -85,7 +88,27 @@ async function runGate(options) {
85
88
  const runId = resolved.runId;
86
89
  const implRoots = await resolveImplRoots(options.projectRoot);
87
90
  if (options.expect === "prd-fetched") {
88
- const prd = await (0, prdArtifacts_1.isPrdFetched)(outputRoot);
91
+ let prd = await (0, prdArtifacts_1.isPrdFetched)(outputRoot);
92
+ if (!prd.ok) {
93
+ const product = (_b = (_a = session === null || session === void 0 ? void 0 : session.product) !== null && _a !== void 0 ? _a : runId.split("-")[1]) !== null && _b !== void 0 ? _b : "ADI";
94
+ const version = (_d = (_c = session === null || session === void 0 ? void 0 : session.version) !== null && _c !== void 0 ? _c : runId.split("-").slice(2).join("-")) !== null && _d !== void 0 ? _d : "";
95
+ const sync = await (0, syncSourcePrd_1.tryAutoSyncSourcePrd)({
96
+ projectRoot: options.projectRoot,
97
+ outputRoot,
98
+ product,
99
+ version,
100
+ });
101
+ if (sync.attempted) {
102
+ checks.push({
103
+ name: "source-prd-sync",
104
+ ok: sync.ok,
105
+ detail: sync.detail,
106
+ });
107
+ }
108
+ if (sync.ok) {
109
+ prd = await (0, prdArtifacts_1.isPrdFetched)(outputRoot);
110
+ }
111
+ }
89
112
  checks.push({ name: "prd-fetched", ...prd });
90
113
  return pass(checks, options.expect, runId);
91
114
  }
@@ -105,6 +128,14 @@ async function runGate(options) {
105
128
  checks.push({ name: "questions-closed", ...closed });
106
129
  const docs = await (0, prdArtifacts_1.areAnalysisDocsReady)(outputRoot);
107
130
  checks.push({ name: "analysis-docs", ...docs });
131
+ const techRaw = await (0, fs_1.readFileIfExists)(path_1.default.join(outputRoot, "04-技术文档草稿.md"));
132
+ if (process.env.OPSX_SKIP_TECH_DOC_QUALITY !== "1") {
133
+ const techStruct = (0, techDocQuality_1.checkTechDocStructure)(techRaw !== null && techRaw !== void 0 ? techRaw : "");
134
+ checks.push({ name: "tech-doc-structure", ok: techStruct.ok, detail: techStruct.detail });
135
+ }
136
+ else {
137
+ checks.push({ name: "tech-doc-structure", ok: true, detail: "OPSX_SKIP_TECH_DOC_QUALITY=1 已跳过" });
138
+ }
108
139
  const acDefined = await (0, prdArtifacts_1.isAcceptanceChecklistDefined)(outputRoot);
109
140
  checks.push({ name: "acceptance-checklist-defined", ...acDefined });
110
141
  if (session && session.docModeOnly === false) {
@@ -173,6 +204,11 @@ async function runGate(options) {
173
204
  detail: `openspec/changes/${changeName} 不存在,无法校验 AC 映射`,
174
205
  });
175
206
  }
207
+ const pkgRoot = await (0, projectRoots_1.resolvePackageRoot)(installRoot);
208
+ const demoArtifactChecks = await (0, demoImplementationCoverage_1.checkDemoReferenceArtifacts)(pkgRoot, changeName);
209
+ for (const c of demoArtifactChecks) {
210
+ checks.push({ name: c.name, ok: c.ok, detail: c.detail });
211
+ }
176
212
  return pass(checks, options.expect, runId);
177
213
  }
178
214
  if (options.expect === "ac-signed") {
@@ -183,7 +219,10 @@ async function runGate(options) {
183
219
  if (options.expect === "prd-coverage") {
184
220
  const installRoot = (0, projectRoots_1.resolveInstallRoot)(options.projectRoot);
185
221
  const pkgRoot = await (0, projectRoots_1.resolvePackageRoot)(installRoot);
186
- const coverage = await (0, prdCoverage_1.checkPrdCoverage)(outputRoot, pkgRoot);
222
+ const changeName = await readActiveChange(options.projectRoot, session, options.change);
223
+ const coverage = await (0, prdCoverage_1.checkPrdCoverage)(outputRoot, pkgRoot, {
224
+ changeName: changeName !== null && changeName !== void 0 ? changeName : undefined,
225
+ });
187
226
  for (const c of coverage.checks) {
188
227
  checks.push({ name: c.name, ok: c.ok, detail: c.detail });
189
228
  }
@@ -211,6 +250,11 @@ async function runGate(options) {
211
250
  found = true;
212
251
  const st = (0, openspecGate_1.checkOpenspecApplyReady)(root, changeName);
213
252
  checks.push({ name: "openspec-apply-ready", ok: st.applyReady, detail: st.detail });
253
+ const pkgRoot = await (0, projectRoots_1.resolvePackageRoot)(installRoot);
254
+ const demoArtifactChecks = await (0, demoImplementationCoverage_1.checkDemoReferenceArtifacts)(pkgRoot, changeName);
255
+ for (const c of demoArtifactChecks) {
256
+ checks.push({ name: c.name, ok: c.ok, detail: c.detail });
257
+ }
214
258
  break;
215
259
  }
216
260
  }
@@ -330,8 +374,8 @@ async function runGate(options) {
330
374
  name: "validate-report",
331
375
  ok,
332
376
  detail: ok
333
- ? `验收通过 (pipeline=${(_a = j.pipelineShortExitCode) !== null && _a !== void 0 ? _a : "?"}, e2e=${(_b = j.e2eExitCode) !== null && _b !== void 0 ? _b : "?"})`
334
- : `验收结果: ${(_c = j.verdict) !== null && _c !== void 0 ? _c : "unknown"}`,
377
+ ? `验收通过 (pipeline=${(_e = j.pipelineShortExitCode) !== null && _e !== void 0 ? _e : "?"}, e2e=${(_f = j.e2eExitCode) !== null && _f !== void 0 ? _f : "?"})`
378
+ : `验收结果: ${(_g = j.verdict) !== null && _g !== void 0 ? _g : "unknown"}`,
335
379
  });
336
380
  }
337
381
  catch {
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runSyncSourcePrd = runSyncSourcePrd;
4
+ const syncSourcePrd_1 = require("../core/syncSourcePrd");
5
+ const resolveRun_1 = require("./resolveRun");
6
+ /**
7
+ * CLI:将 docs 版本目录 PRD 同步到 openspec run 的 source/。
8
+ */
9
+ async function runSyncSourcePrd(options) {
10
+ var _a, _b, _c, _d;
11
+ const loaded = await (0, resolveRun_1.loadRunSession)(options.projectRoot, options.runId);
12
+ if (!(loaded === null || loaded === void 0 ? void 0 : loaded.resolved)) {
13
+ return { ok: false, detail: "未找到 openspec/PRD/<runId> 目录", runId: null };
14
+ }
15
+ const { resolved, session } = loaded;
16
+ const parts = resolved.runId.split("-");
17
+ const product = (_b = (_a = session === null || session === void 0 ? void 0 : session.product) !== null && _a !== void 0 ? _a : parts[1]) !== null && _b !== void 0 ? _b : "ADI";
18
+ const version = (_d = (_c = session === null || session === void 0 ? void 0 : session.version) !== null && _c !== void 0 ? _c : parts.slice(2).join("-")) !== null && _d !== void 0 ? _d : "";
19
+ if (options.dryRun) {
20
+ return {
21
+ ok: true,
22
+ detail: `dry-run: 将把 docs 版本 PRD 同步到 ${resolved.outputRoot}/source/`,
23
+ runId: resolved.runId,
24
+ };
25
+ }
26
+ const result = await (0, syncSourcePrd_1.syncSourcePrdFromDocs)({
27
+ projectRoot: options.projectRoot,
28
+ outputRoot: resolved.outputRoot,
29
+ product,
30
+ version,
31
+ });
32
+ return {
33
+ ok: result.ok,
34
+ detail: result.detail,
35
+ runId: resolved.runId,
36
+ imageCount: result.imageCount,
37
+ };
38
+ }
@@ -114,7 +114,9 @@ async function runValidate(options) {
114
114
  if (remediateLoop.escalated) {
115
115
  needUserConfirmFromRemediate = true;
116
116
  }
117
- let prdCoverage = await (0, prdCoverage_1.checkPrdCoverage)(loaded.resolved.outputRoot, pkgRoot);
117
+ let prdCoverage = await (0, prdCoverage_1.checkPrdCoverage)(loaded.resolved.outputRoot, pkgRoot, {
118
+ changeName: options.change,
119
+ });
118
120
  if (!prdCoverage.ok) {
119
121
  const failed = prdCoverage.checks.filter((c) => !c.ok).map((c) => `${c.name}: ${c.detail}`);
120
122
  const payload = {
@@ -210,7 +212,7 @@ async function runValidate(options) {
210
212
  }
211
213
  return {
212
214
  ok: true,
213
- detail: "验收通过(prd-coverage + lint+type+unit + 强制 e2e),可进入交付",
215
+ detail: "验收通过(prd-coverage 含 E2E 深度与 API 契约 + lint+type+unit + 强制 e2e),可进入交付",
214
216
  needUserConfirm: false,
215
217
  };
216
218
  }
@@ -134,6 +134,7 @@ async function installAgentSkill(projectRoot, projectKind) {
134
134
  return;
135
135
  const template = await (0, templates_1.loadTemplateText)("artifacts/06-agent-skill.template.md");
136
136
  const layered = await (0, templates_1.loadTemplateText)("skills/layered-testing-strategy.md");
137
+ const demoRef = await (0, templates_1.loadTemplateText)("skills/demo-reference-mandatory-workflow.md");
137
138
  for (const target of targets) {
138
139
  const agentName = target.includes("/.cursor/")
139
140
  ? "cursor"
@@ -150,6 +151,8 @@ async function installAgentSkill(projectRoot, projectKind) {
150
151
  await (0, fs_1.writeTextFileEnsuredDir)(target, content);
151
152
  const layeredPath = path_1.default.join(path_1.default.dirname(target), "LAYERED-TESTING.md");
152
153
  await (0, fs_1.writeTextFileEnsuredDir)(layeredPath, layered);
154
+ const demoRefPath = path_1.default.join(path_1.default.dirname(target), "DEMO-REFERENCE-WORKFLOW.md");
155
+ await (0, fs_1.writeTextFileEnsuredDir)(demoRefPath, demoRef);
153
156
  }
154
157
  }
155
158
  async function installSddVersionTriggerRule(projectRoot, agent) {
@@ -10,6 +10,7 @@ const utils_1 = require("../core/utils");
10
10
  const fs_1 = require("../core/fs");
11
11
  const templates_1 = require("../core/templates");
12
12
  const inferProject_1 = require("../core/inferProject");
13
+ const syncSourcePrd_1 = require("../core/syncSourcePrd");
13
14
  const sessionState_1 = require("../core/sessionState");
14
15
  const projectRoots_1 = require("../core/projectRoots");
15
16
  const writeNext_1 = require("../core/writeNext");
@@ -30,12 +31,14 @@ async function startFlowScaffold(options) {
30
31
  // 1) Step1: doc-skill 指引(按产品解析脚本路径,支持 monorepo frontend)
31
32
  const kind = (0, inferProject_1.productToKind)(options.product);
32
33
  const docSkill = await (0, inferProject_1.resolveDocSkillDir)(options.projectRoot, kind);
34
+ const syncSourceDirRel = await (0, syncSourcePrd_1.resolveSyncSourceDirRel)(options.projectRoot, sourceDir, options.product);
33
35
  const step1Template = await (0, templates_1.loadTemplateText)("artifacts/01-adi-doc-skill-指引.template.md");
34
36
  const step1 = renderTemplate(step1Template, {
35
37
  PRODUCT: options.product,
36
38
  PRODUCT_PREFIX: inferProject_1.PRODUCT_PREFIX_BY_KIND[kind],
37
39
  DOC_SKILL_REL: docSkill.relFromRoot.replace(/\\/g, "/"),
38
40
  VERSION_EXAMPLE: options.version,
41
+ SYNC_SOURCE_DIR_REL: syncSourceDirRel,
39
42
  });
40
43
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "01-获取需求文档指引.md"), step1);
41
44
  // source 占位:让第 2 步提示词在任何工具里都能直接读
@@ -74,9 +77,11 @@ async function startFlowScaffold(options) {
74
77
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "02-改动点清单.md"), file02Template);
75
78
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "03-待确认问题清单.md"), file03Template);
76
79
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "05-验收清单.md"), file05Template);
77
- // 4) Step4 技术文档入口(草稿)
80
+ // 4) Step4 技术文档入口(完整模板 + 填写指引)
78
81
  const techDocTemplate = await (0, templates_1.loadTemplateText)("artifacts/04-技术文档.template.md");
79
82
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "04-技术文档草稿.md"), techDocTemplate);
83
+ const techDocFillGuide = await (0, templates_1.loadTemplateText)("prompts/04-tech-doc-fill-prompt.md");
84
+ await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "04-技术文档生成指引.md"), techDocFillGuide);
80
85
  // 5) Step5 openspec 提案入口(草稿)
81
86
  const openspecEntryTemplate = await (0, templates_1.loadTemplateText)("artifacts/06-openspec-提案草稿.template.md");
82
87
  await (0, fs_1.writeTextFileEnsuredDir)(path_1.default.join(outputRoot, "06-openspec-提案草稿.md"), openspecEntryTemplate);
@@ -118,7 +123,7 @@ async function startFlowScaffold(options) {
118
123
  "用户确认后产出(顺序重要):",
119
124
  "- `03-待确认问题清单.md`(已闭合)",
120
125
  "- `05-验收清单.md`(从 `source/PRD.md` 逐条抽取 AC,标注「定义状态:已定义」)",
121
- "- `04-技术文档草稿.md`(引用 AC-ID)",
126
+ "- `04-技术文档草稿.md`(**在脚手架模板原文件上逐节填写**,见 `04-技术文档生成指引.md`;禁止摘要替换)",
122
127
  "- `01-需求分析报告.md`(业务概述,非验收 SSOT)",
123
128
  "- `02-改动点清单.md`",
124
129
  "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdd-flow-kit",
3
- "version": "1.3.15",
3
+ "version": "1.3.18",
4
4
  "private": false,
5
5
  "description": "Cross-agent SDD automated development workflow kit (ADI/ADI-like).",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "files": [
13
13
  "dist",
14
14
  "src/templates",
15
+ "scripts",
15
16
  "README.md"
16
17
  ],
17
18
  "bin": {
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env bash
2
+ # 校验:改动 src/ 或 e2e/ 时必须绑定活跃 OpenSpec change(见 AGENTS.md)
3
+ set -euo pipefail
4
+
5
+ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6
+ cd "$ROOT_DIR"
7
+
8
+ WATCH_PATHS=('src/' 'e2e/')
9
+ CHANGE_DIR_PREFIX="openspec/changes"
10
+ ACTIVE_CHANGE_FILES=('.opsx-active-change' '.poquan-active-change')
11
+
12
+ log() { printf '[opsx:gate] %s\n' "$*"; }
13
+ die() { log "ERROR: $*"; exit 1; }
14
+
15
+ path_is_watched() {
16
+ local path="$1"
17
+ local prefix
18
+ for prefix in "${WATCH_PATHS[@]}"; do
19
+ if [[ "$path" == "$prefix"* ]] || [[ "$path" == */"$prefix"* ]]; then
20
+ return 0
21
+ fi
22
+ done
23
+ return 1
24
+ }
25
+
26
+ collect_candidate_files() {
27
+ CANDIDATE_FILES=()
28
+ if [[ "${1:-}" == "--ci" ]]; then
29
+ local base="${GITHUB_BASE_REF:-main}"
30
+ base="${base#origin/}"
31
+ git fetch origin "$base" --depth=1 2>/dev/null || true
32
+ local merge_base
33
+ merge_base="$(git merge-base "origin/${base}" HEAD 2>/dev/null || git merge-base "${base}" HEAD 2>/dev/null || echo "")"
34
+ [[ -n "$merge_base" ]] || merge_base="HEAD~1"
35
+ while IFS= read -r line; do
36
+ [[ -n "$line" ]] && CANDIDATE_FILES+=("$line")
37
+ done < <(git diff --name-only "$merge_base"...HEAD 2>/dev/null || git diff --name-only HEAD~1 HEAD)
38
+ else
39
+ while IFS= read -r line; do
40
+ [[ -n "$line" ]] && CANDIDATE_FILES+=("$line")
41
+ done < <(git diff --cached --name-only --diff-filter=ACMR 2>/dev/null || true)
42
+ if [[ ${#CANDIDATE_FILES[@]} -eq 0 ]]; then
43
+ while IFS= read -r line; do
44
+ [[ -n "$line" ]] && CANDIDATE_FILES+=("$line")
45
+ done < <(git diff --name-only --diff-filter=ACMR HEAD 2>/dev/null || true)
46
+ fi
47
+ fi
48
+ }
49
+
50
+ has_watched_changes() {
51
+ local file
52
+ for file in "${CANDIDATE_FILES[@]}"; do
53
+ if path_is_watched "$file"; then
54
+ return 0
55
+ fi
56
+ done
57
+ return 1
58
+ }
59
+
60
+ resolve_change_name() {
61
+ if [[ -n "${OPSX_ACTIVE_CHANGE:-}" ]]; then
62
+ echo "$OPSX_ACTIVE_CHANGE"
63
+ return 0
64
+ fi
65
+ if [[ -n "${POQUAN_ACTIVE_CHANGE:-}" ]]; then
66
+ echo "$POQUAN_ACTIVE_CHANGE"
67
+ return 0
68
+ fi
69
+ local file name
70
+ for file in "${ACTIVE_CHANGE_FILES[@]}"; do
71
+ if [[ -f "$file" ]]; then
72
+ name="$(tr -d '[:space:]' <"$file")"
73
+ [[ -n "$name" ]] && { echo "$name"; return 0; }
74
+ fi
75
+ done
76
+ local branch
77
+ branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")"
78
+ if [[ "$branch" =~ ^change/([^/]+) ]]; then
79
+ echo "${BASH_REMATCH[1]}"
80
+ return 0
81
+ fi
82
+ return 1
83
+ }
84
+
85
+ validate_change_dir() {
86
+ local name="$1"
87
+ local dir="${CHANGE_DIR_PREFIX}/${name}"
88
+ [[ -d "$dir" ]] || die "change 目录不存在: ${dir}(请先 Propose 或设置 OPSX_ACTIVE_CHANGE)"
89
+ [[ "$dir" != *"/archive/"* ]] || die "change 已归档,请新建 change: ${name}"
90
+ if [[ -f "${dir}/tasks.md" ]] && grep -q '^- \[ \]' "${dir}/tasks.md" 2>/dev/null; then
91
+ log "WARN: tasks.md 仍有未完成项;合码前建议全部勾选并跑 delivery-pipeline"
92
+ fi
93
+ }
94
+
95
+ main() {
96
+ if [[ "${OPSX_SKIP_OPSX:-}" == "1" ]] || [[ "${POQUAN_SKIP_OPSX:-}" == "1" ]]; then
97
+ log "已跳过(OPSX_SKIP_OPSX 或 POQUAN_SKIP_OPSX=1)"
98
+ exit 0
99
+ fi
100
+
101
+ collect_candidate_files "${1:-}"
102
+ if ! has_watched_changes; then
103
+ log "无 src/ 或 e2e/ 改动,跳过"
104
+ exit 0
105
+ fi
106
+
107
+ local change_name
108
+ change_name="$(resolve_change_name)" || die "改动 src/e2e 但未绑定 OpenSpec change。请先 Propose,或设置 OPSX_ACTIVE_CHANGE / .opsx-active-change / 分支 change/<name>。详见 AGENTS.md"
109
+
110
+ validate_change_dir "$change_name"
111
+ log "OK — 活跃 change: ${change_name}"
112
+ }
113
+
114
+ main "$@"
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * API 字段契约门禁:change 引用的 config/api/modules 须含 snake/camel normalize(防 SNAKE_CASE 漏映射)。
4
+ *
5
+ * 用法: node scripts/assess-api-field-contract.mjs --change <change-name>
6
+ */
7
+ import fs from "node:fs";
8
+ import path from "node:path";
9
+ import { fileURLToPath } from "node:url";
10
+
11
+ const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
12
+ const NORMALIZE_MARKERS =
13
+ /snakeToCamel|camelToSnake|normalizeFinanceApiResult|normalize\w*Api|caseTransform|toCamelCase|toSnakeCase/i;
14
+ const API_CLIENT_CALL_RE = /\b(?:apiClient|request|post|get)\s*[.(]/i;
15
+
16
+ function parseArgs(argv) {
17
+ let change = "";
18
+ for (let i = 2; i < argv.length; i += 1) {
19
+ const a = argv[i];
20
+ if (a === "--change" && argv[i + 1]) change = argv[++i];
21
+ else if (!a.startsWith("-") && !change) change = a;
22
+ }
23
+ return { change };
24
+ }
25
+
26
+ function readChangeBlob(changeDir) {
27
+ let blob = "";
28
+ for (const f of ["tasks.md", "proposal.md", "design.md"]) {
29
+ const fp = path.join(changeDir, f);
30
+ if (!fs.existsSync(fp)) continue;
31
+ blob += fs.readFileSync(fp, "utf8");
32
+ }
33
+ return blob;
34
+ }
35
+
36
+ function resolveModuleFile(mod) {
37
+ const candidates = [
38
+ path.join(ROOT, "src/config/api/modules", mod, "index.ts"),
39
+ path.join(ROOT, "frontend/src/config/api/modules", mod, "index.ts"),
40
+ path.join(ROOT, "src/config/api/modules", `${mod}.ts`),
41
+ path.join(ROOT, "frontend/src/config/api/modules", `${mod}.ts`),
42
+ ];
43
+ for (const fp of candidates) {
44
+ if (fs.existsSync(fp)) return fp;
45
+ }
46
+ return null;
47
+ }
48
+
49
+ function fail(messages) {
50
+ console.error("[assess-api-field-contract] FAIL");
51
+ for (const msg of messages) console.error(` - ${msg}`);
52
+ console.error(
53
+ " 修复: 在 config/api/modules/<domain> 对请求体 camelToSnake、响应 data snakeToCamel;参考 finance/caseTransform.ts",
54
+ );
55
+ process.exit(1);
56
+ }
57
+
58
+ function main() {
59
+ const skip =
60
+ process.env.POQUAN_SKIP_API_FIELD_CONTRACT ||
61
+ process.env.OPSX_SKIP_API_FIELD_CONTRACT;
62
+ if (skip === "1") {
63
+ console.warn("[assess-api-field-contract] SKIP");
64
+ process.exit(0);
65
+ }
66
+
67
+ const { change } = parseArgs(process.argv);
68
+ if (!change) {
69
+ console.error("[assess-api-field-contract] 缺少 --change <name>");
70
+ process.exit(2);
71
+ }
72
+
73
+ const changeDir = path.join(ROOT, "openspec/changes", change);
74
+ if (!fs.existsSync(changeDir)) {
75
+ console.error(`[assess-api-field-contract] change 不存在: openspec/changes/${change}`);
76
+ process.exit(2);
77
+ }
78
+
79
+ const blob = readChangeBlob(changeDir);
80
+ const moduleRefs = new Set();
81
+ const modRe = /config\/api\/modules\/([\w/-]+)/g;
82
+ let m;
83
+ while ((m = modRe.exec(blob)) !== null) {
84
+ moduleRefs.add(m[1].replace(/\/index$/, ""));
85
+ }
86
+
87
+ if (moduleRefs.size === 0) {
88
+ console.log(`[assess-api-field-contract] PASS(无 API 模块引用) change=${change}`);
89
+ process.exit(0);
90
+ }
91
+
92
+ const errors = [];
93
+ const passed = [];
94
+ for (const mod of moduleRefs) {
95
+ const fp = resolveModuleFile(mod);
96
+ if (!fp) {
97
+ errors.push(`config/api/modules/${mod} 引用但文件不存在`);
98
+ continue;
99
+ }
100
+ const rel = path.relative(ROOT, fp).replace(/\\/g, "/");
101
+ const content = fs.readFileSync(fp, "utf8");
102
+ if (!API_CLIENT_CALL_RE.test(content)) {
103
+ passed.push(`${rel}(无 HTTP 调用)`);
104
+ continue;
105
+ }
106
+ if (!NORMALIZE_MARKERS.test(content)) {
107
+ errors.push(`${rel} 含 API 调用但未发现字段 normalize`);
108
+ } else {
109
+ passed.push(rel);
110
+ }
111
+ }
112
+
113
+ if (errors.length > 0) fail(errors);
114
+ console.log(
115
+ `[assess-api-field-contract] PASS change=${change} modules=${passed.join(", ")}`,
116
+ );
117
+ }
118
+
119
+ main();