@cloud411716/fancy-webnovel 2.2.0 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/README.md +65 -0
  2. package/client.js +201 -15
  3. package/cordis.patch.yml +5 -1
  4. package/docs/douyin-plugin.md +118 -0
  5. package/docs/material-library/README.md +404 -0
  6. package/docs/material-library/import.js +437 -0
  7. package/docs/material-library/prompt.md +370 -0
  8. package/docs/material-library/queries.surql +94 -0
  9. package/docs/material-library/sample.jsonl +14 -0
  10. package/docs/material-library/schema.surql +46 -0
  11. package/docs/mcp-profile.example.yml +24 -0
  12. package/docs/story-graph-design.md +602 -0
  13. package/docs/story-graph-validation.md +81 -0
  14. package/docs/workspace-mcp.md +284 -0
  15. package/index.js +12 -7
  16. package/lib/atomic-write.js +109 -0
  17. package/lib/chapter-order.js +100 -0
  18. package/lib/confirmation.js +80 -0
  19. package/lib/db-init.js +123 -0
  20. package/lib/deployed-state.js +11 -11
  21. package/lib/direction-labels.js +265 -0
  22. package/lib/error-codes.js +31 -31
  23. package/lib/infra.js +29 -4
  24. package/lib/material-db.js +149 -0
  25. package/lib/path-guard.js +150 -0
  26. package/lib/projects.js +400 -0
  27. package/lib/scraper-utils.js +140 -0
  28. package/lib/surreal-server.js +338 -0
  29. package/lib/topic-decision.js +860 -0
  30. package/lib/topic-fields.js +251 -0
  31. package/lib/topic-schema.js +225 -0
  32. package/lib/workspace.js +501 -0
  33. package/package.json +31 -7
  34. package/plugins/fancy-analyze/analyze-definitions.md +519 -0
  35. package/plugins/fancy-analyze/index.js +1128 -77
  36. package/plugins/fancy-analyze/ingest-coverage.js +107 -0
  37. package/plugins/fancy-analyze/schema-analyze.md +355 -107
  38. package/plugins/fancy-analyze/scripts/append-summary-chapter.js +612 -0
  39. package/plugins/fancy-analyze/summary-progress.js +71 -7
  40. package/plugins/fancy-analyze/templates.js +437 -139
  41. package/plugins/fancy-bootstrap/index.js +276 -131
  42. package/plugins/fancy-bootstrap/scripts/init.js +59 -25
  43. package/plugins/fancy-bootstrap/templates.js +35 -18
  44. package/plugins/fancy-db/index.js +172 -0
  45. package/plugins/fancy-douyin/deploy.js +134 -0
  46. package/plugins/fancy-douyin/index.js +551 -0
  47. package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +84 -0
  48. package/plugins/fancy-douyin/scripts/crawl-note.cjs +194 -0
  49. package/plugins/fancy-douyin/scripts/lib.cjs +197 -0
  50. package/plugins/fancy-douyin/scripts/scan-account.cjs +128 -0
  51. package/plugins/fancy-douyin/scripts/start-chrome.cjs +203 -0
  52. package/plugins/fancy-douyin/state.js +197 -0
  53. package/plugins/fancy-douyin/templates.js +127 -0
  54. package/plugins/fancy-export/exporter.js +805 -0
  55. package/plugins/fancy-export/index.js +63 -0
  56. package/plugins/fancy-import/import-driver.js +29 -6
  57. package/plugins/fancy-import/index.js +13 -5
  58. package/plugins/fancy-import/prompts/01-import-prompt.md +39 -14
  59. package/plugins/fancy-import/templates.js +17 -8
  60. package/plugins/fancy-material/exporter.js +153 -0
  61. package/plugins/fancy-material/genres.js +256 -0
  62. package/plugins/fancy-material/index.js +835 -0
  63. package/plugins/fancy-material/inventory.js +145 -0
  64. package/plugins/fancy-material/kinds.js +313 -0
  65. package/plugins/fancy-material/queue.js +55 -0
  66. package/plugins/fancy-material/scripts/query.js +254 -0
  67. package/plugins/fancy-material/templates.js +258 -0
  68. package/plugins/fancy-material/tool-deploy.js +138 -0
  69. package/plugins/fancy-material/validate.js +149 -0
  70. package/plugins/fancy-project/index.js +133 -0
  71. package/plugins/fancy-scan/existing-block.js +100 -0
  72. package/plugins/fancy-scan/index.js +410 -21
  73. package/plugins/fancy-scan/prompts/01-analysis-prompt.md +302 -0
  74. package/plugins/fancy-scan/scripts/append-block.js +725 -0
  75. package/plugins/fancy-scan/scripts/report-validate.js +824 -0
  76. package/plugins/fancy-scan/scripts/run-scan.js +169 -22
  77. package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +145 -16
  78. package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +179 -49
  79. package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +38 -20
  80. package/plugins/fancy-scan/templates/blocks.js +155 -0
  81. package/plugins/fancy-scan/templates/platforms.js +372 -0
  82. package/plugins/fancy-scan/templates/report-schema.js +90 -0
  83. package/plugins/fancy-scan/templates.js +545 -146
  84. package/plugins/fancy-scan/tool-deploy.js +335 -0
  85. package/plugins/fancy-scan/wait-turn.js +46 -0
  86. package/plugins/fancy-story-builder/db/README.md +106 -0
  87. package/plugins/fancy-story-builder/db/client.js +228 -0
  88. package/plugins/fancy-story-builder/db/commands.js +145 -0
  89. package/plugins/fancy-story-builder/db/ids.js +39 -0
  90. package/plugins/fancy-story-builder/db/import.js +798 -0
  91. package/plugins/fancy-story-builder/db/project.js +299 -0
  92. package/plugins/fancy-story-builder/db/query.js +361 -0
  93. package/plugins/fancy-story-builder/db/schema.js +417 -0
  94. package/plugins/fancy-story-builder/db/stage-sync.js +238 -0
  95. package/plugins/fancy-story-builder/flesh-output.js +19 -6
  96. package/plugins/fancy-story-builder/index.js +4105 -396
  97. package/plugins/fancy-story-builder/patch-driver.js +390 -0
  98. package/plugins/fancy-story-builder/prompts/01-skeleton-prompt.md +29 -19
  99. package/plugins/fancy-story-builder/prompts/02-flesh-prompt.md +72 -10
  100. package/plugins/fancy-story-builder/prompts/03-rhythm-prompt.md +40 -5
  101. package/plugins/fancy-story-builder/prompts/04-writing-prompt.md +37 -0
  102. package/plugins/fancy-story-builder/purpose-template.md +42 -66
  103. package/plugins/fancy-story-builder/references/formulas-skeleton.json +229 -56
  104. package/plugins/fancy-story-builder/references/genre-profiles.json +3514 -0
  105. package/plugins/fancy-story-builder/references/glossary.md +15 -6
  106. package/plugins/fancy-story-builder/references/index.md +97 -6
  107. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/00-index.md +1 -1
  108. package/plugins/fancy-story-builder/references//350/212/202/345/245/217/346/214/207/345/257/274/07-audit.md +25 -7
  109. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/01-character.md +5 -1
  110. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/03-relations.md +6 -2
  111. package/plugins/fancy-story-builder/references//350/241/200/350/202/211/346/214/207/345/257/274/05-audit.md +15 -5
  112. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/00-index.md +1 -1
  113. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/01-positioning.md +2 -1
  114. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/03-entities.md +1 -1
  115. package/plugins/fancy-story-builder/references//351/252/250/346/236/266/346/214/207/345/257/274/06-audit.md +19 -1
  116. package/plugins/fancy-story-builder/schema-setting.json +1100 -0
  117. package/plugins/fancy-story-builder/schema-setting.md +201 -293
  118. package/plugins/fancy-story-builder/scripts/brief-validate.js +164 -0
  119. package/plugins/fancy-story-builder/scripts/content-quality.js +4 -1
  120. package/plugins/fancy-story-builder/scripts/flesh-validate.js +135 -12
  121. package/plugins/fancy-story-builder/scripts/graph-utils.js +146 -47
  122. package/plugins/fancy-story-builder/scripts/reference-log.js +110 -0
  123. package/plugins/fancy-story-builder/scripts/rhythm-validate.js +100 -4
  124. package/plugins/fancy-story-builder/scripts/schema-pages.js +2668 -0
  125. package/plugins/fancy-story-builder/scripts/semantic-lint.js +5 -1
  126. package/plugins/fancy-story-builder/scripts/skeleton-validate.js +158 -17
  127. package/plugins/fancy-story-builder/scripts/stage-batches.js +36 -2
  128. package/plugins/fancy-story-builder/scripts/stage-contract.js +12 -0
  129. package/plugins/fancy-story-builder/scripts/story-artifacts.js +933 -0
  130. package/plugins/fancy-story-builder/scripts/upstream-query.js +1 -1
  131. package/plugins/fancy-story-builder/scripts/v2/baseline.js +112 -0
  132. package/plugins/fancy-story-builder/scripts/v2/cast.js +517 -0
  133. package/plugins/fancy-story-builder/scripts/v2/context.js +434 -0
  134. package/plugins/fancy-story-builder/scripts/v2/contracts.js +493 -0
  135. package/plugins/fancy-story-builder/scripts/v2/core.js +419 -0
  136. package/plugins/fancy-story-builder/scripts/v2/engine.js +1068 -0
  137. package/plugins/fancy-story-builder/scripts/v2/export-markdown.js +794 -0
  138. package/plugins/fancy-story-builder/scripts/v2/genre.js +195 -0
  139. package/plugins/fancy-story-builder/scripts/v2/knowledge.js +321 -0
  140. package/plugins/fancy-story-builder/scripts/v2/macro.js +409 -0
  141. package/plugins/fancy-story-builder/scripts/v2/manifest.js +538 -0
  142. package/plugins/fancy-story-builder/scripts/v2/migrate.js +137 -0
  143. package/plugins/fancy-story-builder/scripts/v2/prompts.js +1335 -0
  144. package/plugins/fancy-story-builder/scripts/v2/rhythm.js +618 -0
  145. package/plugins/fancy-story-builder/scripts/v2/scale.js +843 -0
  146. package/plugins/fancy-story-builder/scripts/v2/snapshot.js +121 -0
  147. package/plugins/fancy-story-builder/scripts/v2/stages.js +273 -0
  148. package/plugins/fancy-story-builder/scripts/v2/writing.js +216 -0
  149. package/plugins/fancy-story-builder/storydb/README.md +271 -0
  150. package/plugins/fancy-story-builder/storydb/cli.js +458 -0
  151. package/plugins/fancy-story-builder/storydb/client.js +351 -0
  152. package/plugins/fancy-story-builder/storydb/compile.js +452 -0
  153. package/plugins/fancy-story-builder/storydb/context.js +288 -0
  154. package/plugins/fancy-story-builder/storydb/definitions.js +699 -0
  155. package/plugins/fancy-story-builder/storydb/driver.js +106 -0
  156. package/plugins/fancy-story-builder/storydb/dsh-tools.js +313 -0
  157. package/plugins/fancy-story-builder/storydb/export.js +175 -0
  158. package/plugins/fancy-story-builder/storydb/ids.js +31 -0
  159. package/plugins/fancy-story-builder/storydb/impact.js +170 -0
  160. package/plugins/fancy-story-builder/storydb/import.js +559 -0
  161. package/plugins/fancy-story-builder/storydb/mcp.js +99 -0
  162. package/plugins/fancy-story-builder/storydb/prompts.js +952 -0
  163. package/plugins/fancy-story-builder/storydb/stage-command.js +379 -0
  164. package/plugins/fancy-story-builder/storydb/stage.js +426 -0
  165. package/plugins/fancy-story-builder/storydb/store.js +891 -0
  166. package/plugins/fancy-story-builder/storydb/tools.js +592 -0
  167. package/plugins/fancy-story-builder/storydb/validation.js +183 -0
  168. package/plugins/fancy-story-builder/templates/v2/10-scale/scale-template.json +144 -0
  169. package/plugins/fancy-story-builder/templates/v2/20-macro/emotion-template.json +15 -0
  170. package/plugins/fancy-story-builder/templates/v2/20-macro/endings-template.json +16 -0
  171. package/plugins/fancy-story-builder/templates/v2/20-macro/lines-template.json +28 -0
  172. package/plugins/fancy-story-builder/templates/v2/20-macro/roles-template.json +10 -0
  173. package/plugins/fancy-story-builder/templates/v2/20-macro/story-template.json +19 -0
  174. package/plugins/fancy-story-builder/templates/v2/20-macro/volumes-template.json +24 -0
  175. package/plugins/fancy-story-builder/templates/v2/20-macro/world-template.json +24 -0
  176. package/plugins/fancy-story-builder/templates/v2/30-cast/characters-template.json +49 -0
  177. package/plugins/fancy-story-builder/templates/v2/30-cast/climaxes-template.json +14 -0
  178. package/plugins/fancy-story-builder/templates/v2/30-cast/conflicts-template.json +30 -0
  179. package/plugins/fancy-story-builder/templates/v2/30-cast/factions-template.json +18 -0
  180. package/plugins/fancy-story-builder/templates/v2/30-cast/foreshadows-template.json +29 -0
  181. package/plugins/fancy-story-builder/templates/v2/30-cast/information-template.json +29 -0
  182. package/plugins/fancy-story-builder/templates/v2/30-cast/items-template.json +13 -0
  183. package/plugins/fancy-story-builder/templates/v2/30-cast/locations-template.json +12 -0
  184. package/plugins/fancy-story-builder/templates/v2/30-cast/payoffs-template.json +16 -0
  185. package/plugins/fancy-story-builder/templates/v2/30-cast/relations-template.json +44 -0
  186. package/plugins/fancy-story-builder/templates/v2/40-rhythm/chapter-map-template.json +81 -0
  187. package/plugins/fancy-story-builder/templates/v2/40-rhythm/outline-template.json +162 -0
  188. package/plugins/fancy-story-builder/templates/v2/50-writing/ledger-template.json +46 -0
  189. package/plugins/fancy-story-builder/templates/v2/60-deslop/report-template.json +10 -0
  190. package/plugins/fancy-story-builder/templates//350/212/202/345/245/217//346/227/266/345/272/217/346/230/240/345/260/204/346/250/241/346/235/277.json +267 -44
  191. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//344/270/226/347/225/214/350/247/202/350/247/204/345/210/231/346/250/241/346/235/277.json +121 -19
  192. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/205/263/351/224/256/351/201/223/345/205/267/346/250/241/346/235/277.json +129 -21
  193. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/205/266/344/273/226/345/256/236/344/275/223/346/250/241/346/235/277.json +62 -0
  194. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/206/262/347/252/201/346/250/241/346/235/277.json +76 -17
  195. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/211/247/346/203/205/346/250/241/346/235/277.json +56 -8
  196. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/212/277/345/212/233/346/250/241/346/235/277.json +63 -13
  197. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//345/234/260/347/202/271/346/250/241/346/235/277.json +34 -6
  198. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//347/273/223/345/261/200/346/250/241/346/235/277.json +81 -18
  199. package/plugins/fancy-story-builder/templates//350/241/200/350/202/211//350/247/222/350/211/262/346/250/241/346/235/277.json +202 -42
  200. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//345/205/263/347/263/273/346/250/241/346/235/277.json +160 -27
  201. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//345/256/236/344/275/223/346/250/241/346/235/277.json +217 -41
  202. package/plugins/fancy-story-builder/templates//351/252/250/346/236/266//346/241/206/346/236/266/346/250/241/346/235/277.json +215 -41
  203. package/plugins/fancy-story-builder/templates.js +272 -74
  204. package/plugins/fancy-story-builder/wiki-references.js +429 -93
  205. package/plugins/fancy-story-builder/writing-driver.js +336 -9
  206. package/plugins/fancy-topic/index.js +414 -61
  207. package/plugins/fancy-topic/prompts/01-exploration-prompt.md +115 -0
  208. package/plugins/fancy-topic/scripts/save-suggestion.js +219 -0
  209. package/plugins/fancy-topic/scripts/select-topic.js +106 -63
  210. package/plugins/fancy-topic/templates.js +225 -122
  211. package/plugins/fancy-topic/tool-deploy.js +200 -0
  212. package/lib/safe-rm.js +0 -94
@@ -0,0 +1,618 @@
1
+ /**
2
+ * v2/rhythm.js —— 40-rhythm:章节地图 + 可落地细纲。
3
+ *
4
+ * 章节地图回答“每章写什么”(5W),细纲回答“这一章具体怎么执行”。
5
+ * 细纲是一章一文件的自包含契约;章节地图是全局覆盖矩阵。
6
+ */
7
+ import fs from 'node:fs';
8
+ import path from 'node:path';
9
+ import {
10
+ asArray,
11
+ indexById,
12
+ readJson,
13
+ v2Error,
14
+ } from './core.js';
15
+ import { eventKey, replayKnowledge, validateKnowledgeSnapshot } from './knowledge.js';
16
+
17
+ const BRANCH_REGEX = /^(B\d{3})(?:-\d+)?$/;
18
+
19
+ function requireString(errors, obj, p, field, contract) {
20
+ const value = obj && obj[field];
21
+ if (typeof value !== 'string' || !value.trim()) {
22
+ errors.push(v2Error('RHYTHM_REQUIRED_FIELD', { path: p + '.' + field, message: '缺少必填文本字段', actual: value, contract }));
23
+ }
24
+ }
25
+
26
+ function requireArray(errors, obj, p, field, contract, min = 0) {
27
+ const value = obj && obj[field];
28
+ if (!Array.isArray(value) || value.length < min) {
29
+ errors.push(v2Error('RHYTHM_REQUIRED_FIELD', {
30
+ path: p + '.' + field,
31
+ message: '必须是至少 ' + min + ' 项的数组',
32
+ actual: Array.isArray(value) ? value.length : typeof value,
33
+ contract,
34
+ }));
35
+ }
36
+ }
37
+
38
+ function normAction(value) {
39
+ const s = String(value || '').trim();
40
+ if (/埋|plant|setup/i.test(s)) return '埋设';
41
+ if (/收|recover|payoff|回收/i.test(s)) return '回收';
42
+ if (/推进|progress/i.test(s)) return '推进';
43
+ if (/强化|reinforce/i.test(s)) return '强化';
44
+ return s;
45
+ }
46
+
47
+ function actionSet(rows) {
48
+ const out = new Set();
49
+ for (const row of asArray(rows)) {
50
+ if (!row) continue;
51
+ const id = typeof row === 'string' ? row : row.id;
52
+ if (!id) continue;
53
+ out.add(id + '|' + (typeof row === 'string' ? '' : normAction(row.action)));
54
+ }
55
+ return out;
56
+ }
57
+
58
+ function actionIds(rows) {
59
+ return new Set(asArray(rows).map((row) => (typeof row === 'string' ? row : row && row.id)).filter(Boolean));
60
+ }
61
+
62
+ function outlineCandidates(outline) {
63
+ const chapter = (outline && outline.chapter) || {};
64
+ const hard = chapter.hard_constraints || outline.hard_constraints || {};
65
+ const how = chapter.how || outline.how || {};
66
+ return { chapter, hard, how };
67
+ }
68
+
69
+ export function validateRhythm(rhythm, scale, macro, cast, opts = {}) {
70
+ const errors = [];
71
+ const warnings = [];
72
+ const docs = rhythm || {};
73
+ const chapterMap = docs.chapter_map || {};
74
+ const chapters = asArray(chapterMap.chapters);
75
+ const T = Number(scale && scale.capacity && scale.capacity.target_chapters);
76
+ const scaleVolumes = asArray(scale && scale.structure && scale.structure.volumes);
77
+ const scaleActs = asArray(scale && scale.structure && scale.structure.acts);
78
+ const volumeById = indexById(scaleVolumes.map((v) => ({ ...v, id: v.volume_id || v.id })));
79
+ const actById = indexById(scaleActs.map((a) => ({ ...a, id: a.act_id })));
80
+ const charIds = new Set(asArray(cast && cast.characters && cast.characters.characters).map((c) => c && c.id).filter(Boolean));
81
+ const locationIds = new Set(asArray(cast && cast.locations && cast.locations.locations).map((l) => l && l.id).filter(Boolean));
82
+ const conflictIds = new Set(asArray(cast && cast.conflicts && cast.conflicts.conflicts).map((x) => x && x.id).filter(Boolean));
83
+ const foreshadowIds = new Set(asArray(cast && cast.foreshadows && cast.foreshadows.foreshadows).map((x) => x && x.id).filter(Boolean));
84
+ const payoffIds = new Set(asArray(cast && cast.payoffs && cast.payoffs.payoffs).map((x) => x && x.id).filter(Boolean));
85
+ const infoIds = new Set(asArray(cast && cast.information && cast.information.information_gaps).map((x) => x && x.id).filter(Boolean));
86
+ const mainNodeIds = new Set(asArray(macro && macro.lines && macro.lines.main_line && macro.lines.main_line.nodes).map((n) => n && n.id).filter(Boolean));
87
+ const branchIds = new Set(asArray(macro && macro.lines && macro.lines.branches).map((b) => b && b.id).filter(Boolean));
88
+ const endingIds = new Set(asArray(macro && macro.endings && macro.endings.endings).map((e) => e && e.id).filter(Boolean));
89
+
90
+ if (!Number.isInteger(T) || T <= 0) {
91
+ errors.push(v2Error('RHYTHM_SCALE', { path: 'chapter_map.chapters', message: '读不到 scale.capacity.target_chapters,无法校验章节地图', contract: '40-rhythm/README.md' }));
92
+ return { ok: false, errors, warnings, data: docs };
93
+ }
94
+ if (chapters.length !== T) {
95
+ errors.push(v2Error('RHYTHM_CHAPTER_COUNT', {
96
+ path: 'chapter_map.chapters',
97
+ message: '章节地图必须覆盖全部章节',
98
+ expected: T, actual: chapters.length,
99
+ contract: '40-rhythm/README.md#chapter-map',
100
+ reference: '10-scale/scale.json#capacity.target_chapters',
101
+ }));
102
+ }
103
+
104
+ const byDisplay = new Map();
105
+ const chapterIdSet = new Set();
106
+ const displaySeen = new Set();
107
+ const coveredMain = new Set();
108
+ const coveredBranch = new Set();
109
+ const coveredConflict = new Set();
110
+ const coveredForeshadow = new Set();
111
+ const coveredPayoff = new Set();
112
+ const coveredInfo = new Set();
113
+ const coveredEnding = new Set();
114
+ const foreshadowFirst = new Map();
115
+ const payoffFirst = new Map();
116
+ let wordSum = 0;
117
+ let prevEmotion = null;
118
+ const recentEmotions = [];
119
+
120
+ for (let i = 0; i < chapters.length; i += 1) {
121
+ const c = chapters[i] || {};
122
+ const p = 'chapter_map.chapters[' + i + ']';
123
+ const displayNo = Number(c.display_no);
124
+ if (!Number.isInteger(displayNo) || displayNo <= 0) {
125
+ errors.push(v2Error('RHYTHM_DISPLAY_NO', { path: p + '.display_no', message: 'display_no 必须是正整数', actual: c.display_no, contract: '40-rhythm/README.md#chapter-map' }));
126
+ } else {
127
+ if (displayNo < 1 || displayNo > T) {
128
+ errors.push(v2Error('RHYTHM_DISPLAY_NO_RANGE', { path: p + '.display_no', message: 'display_no 超出 1..T', actual: displayNo, contract: '40-rhythm/README.md#chapter-map' }));
129
+ }
130
+ if (displaySeen.has(displayNo)) {
131
+ errors.push(v2Error('RHYTHM_DISPLAY_NO_DUP', { path: p + '.display_no', message: 'display_no 重复:' + displayNo, contract: '40-rhythm/README.md#chapter-map' }));
132
+ }
133
+ displaySeen.add(displayNo);
134
+ byDisplay.set(displayNo, c);
135
+ }
136
+ if (!c.chapter_id) {
137
+ errors.push(v2Error('RHYTHM_CHAPTER_ID', { path: p + '.chapter_id', message: '缺少稳定 chapter_id', contract: '40-rhythm/README.md#chapter-map' }));
138
+ } else if (chapterIdSet.has(c.chapter_id)) {
139
+ errors.push(v2Error('RHYTHM_CHAPTER_ID_DUP', { path: p + '.chapter_id', message: 'chapter_id 重复:' + c.chapter_id, contract: '40-rhythm/README.md#chapter-map' }));
140
+ } else {
141
+ chapterIdSet.add(c.chapter_id);
142
+ }
143
+ requireString(errors, c, p, 'title', '40-rhythm/README.md#chapter-map');
144
+
145
+ // 卷/幕归属
146
+ const vol = volumeById.get(c.volume);
147
+ if (!vol) {
148
+ errors.push(v2Error('RHYTHM_VOLUME_REF', { path: p + '.volume', message: '引用了不存在的卷:' + c.volume, contract: '40-rhythm/README.md#chapter-map' }));
149
+ } else if (Number.isInteger(displayNo) && (displayNo < Number(vol.chapter_start) || displayNo > Number(vol.chapter_end))) {
150
+ errors.push(v2Error('RHYTHM_VOLUME_RANGE', {
151
+ path: p + '.volume',
152
+ message: '本章不在该卷的章节区间内',
153
+ expected: vol.chapter_start + '-' + vol.chapter_end,
154
+ actual: displayNo,
155
+ contract: '40-rhythm/README.md#chapter-map',
156
+ }));
157
+ }
158
+ const act = actById.get(c.act);
159
+ if (!act) {
160
+ errors.push(v2Error('RHYTHM_ACT_REF', { path: p + '.act', message: '引用了不存在的幕:' + c.act, contract: '40-rhythm/README.md#chapter-map' }));
161
+ } else {
162
+ if (act.volume_id !== c.volume) {
163
+ errors.push(v2Error('RHYTHM_ACT_VOLUME', { path: p + '.act', message: '幕不属于本章的卷', expected: act.volume_id, actual: c.volume, contract: '40-rhythm/README.md#chapter-map' }));
164
+ }
165
+ if (Number.isInteger(displayNo) && (displayNo < Number(act.chapter_start) || displayNo > Number(act.chapter_end))) {
166
+ errors.push(v2Error('RHYTHM_ACT_RANGE', {
167
+ path: p + '.act',
168
+ message: '本章不在该幕的章节区间内',
169
+ expected: act.chapter_start + '-' + act.chapter_end,
170
+ actual: displayNo,
171
+ contract: '40-rhythm/README.md#chapter-map',
172
+ }));
173
+ }
174
+ }
175
+
176
+ // 5W:谁/何时/何地/为什么/怎么写
177
+ if (!charIds.has(c.pov)) {
178
+ errors.push(v2Error('RHYTHM_POV_REF', { path: p + '.pov', message: '视角人物不存在:' + c.pov, contract: '40-rhythm/README.md#chapter-map' }));
179
+ }
180
+ const castIds = asArray(c.cast);
181
+ if (!castIds.length) {
182
+ errors.push(v2Error('RHYTHM_CAST_EMPTY', { path: p + '.cast', message: '每章至少要有一个出场人物', contract: '40-rhythm/README.md#chapter-map' }));
183
+ }
184
+ if (c.pov && !castIds.includes(c.pov)) {
185
+ errors.push(v2Error('RHYTHM_POV_NOT_CAST', { path: p + '.cast', message: '视角人物必须在本章出场人物里', actual: c.pov, contract: '40-rhythm/README.md#chapter-map' }));
186
+ }
187
+ for (const id of castIds) {
188
+ if (!charIds.has(id)) errors.push(v2Error('RHYTHM_CAST_REF', { path: p + '.cast', message: '出场人物不存在:' + id, contract: '40-rhythm/README.md#chapter-map' }));
189
+ }
190
+ const locs = asArray(c.locations);
191
+ if (!locs.length) {
192
+ errors.push(v2Error('RHYTHM_LOCATION_EMPTY', { path: p + '.locations', message: '每章至少要有一个地点', contract: '40-rhythm/README.md#chapter-map' }));
193
+ }
194
+ for (const id of locs) {
195
+ if (!locationIds.has(id)) errors.push(v2Error('RHYTHM_LOCATION_REF', { path: p + '.locations', message: '地点不存在:' + id, contract: '40-rhythm/README.md#chapter-map' }));
196
+ }
197
+ if (!c.time || typeof c.time !== 'object') {
198
+ errors.push(v2Error('RHYTHM_TIME', { path: p + '.time', message: '缺少本章故事时间', contract: '40-rhythm/README.md#chapter-map' }));
199
+ } else {
200
+ if (c.time.story_day !== undefined && (!Number.isInteger(Number(c.time.story_day)) || Number(c.time.story_day) <= 0)) {
201
+ errors.push(v2Error('RHYTHM_TIME_DAY', { path: p + '.time.story_day', message: 'story_day 必须是正整数', contract: '40-rhythm/README.md#chapter-map' }));
202
+ }
203
+ requireString(errors, c.time, p + '.time', 'clock', '40-rhythm/README.md#chapter-map');
204
+ }
205
+ const why = c.why || {};
206
+ requireArray(errors, why, p + '.why', 'main_line_nodes', '40-rhythm/README.md#chapter-map', 1);
207
+ for (const id of asArray(why.main_line_nodes)) {
208
+ if (!mainNodeIds.has(id)) errors.push(v2Error('RHYTHM_MAIN_NODE_REF', { path: p + '.why.main_line_nodes', message: '主线节点不存在:' + id, contract: '40-rhythm/README.md#chapter-map' }));
209
+ else coveredMain.add(id);
210
+ }
211
+ for (const id of asArray(why.branch_nodes)) {
212
+ const m = BRANCH_REGEX.exec(String(id));
213
+ if (!m || !branchIds.has(m[1])) {
214
+ errors.push(v2Error('RHYTHM_BRANCH_REF', { path: p + '.why.branch_nodes', message: '支线不存在:' + id, contract: '40-rhythm/README.md#chapter-map' }));
215
+ } else {
216
+ coveredBranch.add(m[1]);
217
+ }
218
+ }
219
+ for (const id of asArray(why.conflict)) {
220
+ if (!conflictIds.has(id)) errors.push(v2Error('RHYTHM_CONFLICT_REF', { path: p + '.why.conflict', message: '冲突不存在:' + id, contract: '40-rhythm/README.md#chapter-map' }));
221
+ else coveredConflict.add(id);
222
+ }
223
+ requireString(errors, why, p + '.why', 'theme', '40-rhythm/README.md#chapter-map');
224
+ requireString(errors, why, p + '.why', 'stakes', '40-rhythm/README.md#chapter-map');
225
+ const how = c.how || {};
226
+ for (const field of ['function_main', 'turn', 'ending_hook']) requireString(errors, how, p + '.how', field, '40-rhythm/README.md#chapter-map');
227
+
228
+ // 伏笔/爽点动作
229
+ const fsActions = asArray(c.foreshadow_actions);
230
+ for (let j = 0; j < fsActions.length; j += 1) {
231
+ const a = fsActions[j] || {};
232
+ if (!foreshadowIds.has(a.id)) {
233
+ errors.push(v2Error('RHYTHM_FORESHADOW_REF', { path: p + '.foreshadow_actions[' + j + '].id', message: '伏笔不存在:' + a.id, contract: '40-rhythm/README.md#chapter-map' }));
234
+ continue;
235
+ }
236
+ coveredForeshadow.add(a.id);
237
+ const action = normAction(a.action);
238
+ if (!action) {
239
+ errors.push(v2Error('RHYTHM_FORESHADOW_ACTION', { path: p + '.foreshadow_actions[' + j + '].action', message: '伏笔动作不能为空', contract: '40-rhythm/README.md#chapter-map' }));
240
+ }
241
+ if (action === '埋设' && !foreshadowFirst.has(a.id)) foreshadowFirst.set(a.id, { plant: displayNo, recover: null });
242
+ if (action === '回收') {
243
+ const rec = foreshadowFirst.get(a.id) || { plant: null, recover: null };
244
+ if (rec.recover === null) rec.recover = displayNo;
245
+ foreshadowFirst.set(a.id, rec);
246
+ }
247
+ }
248
+ const plActions = asArray(c.payoff_actions);
249
+ for (let j = 0; j < plActions.length; j += 1) {
250
+ const a = plActions[j] || {};
251
+ if (!payoffIds.has(a.id)) {
252
+ errors.push(v2Error('RHYTHM_PAYOFF_REF', { path: p + '.payoff_actions[' + j + '].id', message: '爽点不存在:' + a.id, contract: '40-rhythm/README.md#chapter-map' }));
253
+ continue;
254
+ }
255
+ coveredPayoff.add(a.id);
256
+ if (normAction(a.action) === '回收' && !payoffFirst.has(a.id)) payoffFirst.set(a.id, displayNo);
257
+ }
258
+ for (let j = 0; j < asArray(c.information_gaps).length; j += 1) {
259
+ const g = c.information_gaps[j] || {};
260
+ if (!infoIds.has(g.id)) {
261
+ errors.push(v2Error('RHYTHM_INFO_REF', { path: p + '.information_gaps[' + j + '].id', message: '信息差不存在:' + g.id, contract: '40-rhythm/README.md#chapter-map' }));
262
+ } else {
263
+ coveredInfo.add(g.id);
264
+ }
265
+ if (!['created', 'progressed', 'resolved'].includes(g.state)) {
266
+ errors.push(v2Error('RHYTHM_INFO_STATE', { path: p + '.information_gaps[' + j + '].state', message: '信息差状态必须是 created/progressed/resolved', actual: g.state, contract: '40-rhythm/README.md#chapter-map' }));
267
+ }
268
+ }
269
+ for (const a of asArray(c.ending_actions)) {
270
+ if (!endingIds.has(a.id)) {
271
+ errors.push(v2Error('RHYTHM_ENDING_REF', { path: p + '.ending_actions', message: '结局节点不存在:' + a.id, contract: '40-rhythm/README.md#chapter-map' }));
272
+ } else {
273
+ coveredEnding.add(a.id);
274
+ }
275
+ }
276
+
277
+ // 情绪 + 钩子
278
+ const emotion = c.emotion || {};
279
+ const value = Number(emotion.value);
280
+ if (!Number.isFinite(value) || value < 1 || value > 10) {
281
+ errors.push(v2Error('RHYTHM_EMOTION', { path: p + '.emotion.value', message: '情绪强度必须是 1..10', actual: emotion.value, contract: '40-rhythm/README.md#chapter-map' }));
282
+ } else {
283
+ if (prevEmotion !== null && Math.abs(value - prevEmotion) > 6) {
284
+ warnings.push(v2Error('RHYTHM_EMOTION_JUMP', { path: p + '.emotion.value', severity: 'warning', message: '相邻章节情绪跳变超过 6;转折/高潮章可忽略', actual: prevEmotion + ' -> ' + value, contract: '40-rhythm/README.md#chapter-map' }));
285
+ }
286
+ recentEmotions.push(value);
287
+ if (recentEmotions.length > 3) recentEmotions.shift();
288
+ if (recentEmotions.length === 3 && Math.max(...recentEmotions) - Math.min(...recentEmotions) < 1) {
289
+ warnings.push(v2Error('RHYTHM_EMOTION_FLAT', { path: p + '.emotion.value', severity: 'warning', message: '连续 3 章情绪波动 < 1,剧情可能平流', actual: recentEmotions.join(' / '), contract: '40-rhythm/README.md#chapter-map' }));
290
+ }
291
+ prevEmotion = value;
292
+ }
293
+ requireString(errors, emotion, p + '.emotion', 'phase', '40-rhythm/README.md#chapter-map');
294
+ const hook = c.hook || {};
295
+ if (!hook.type || !['强', '中', '弱', 'strong', 'medium', 'weak'].includes(String(hook.strength))) {
296
+ errors.push(v2Error('RHYTHM_HOOK', { path: p + '.hook', message: '每章必须有钩子类型与强度(强/中/弱)', contract: '40-rhythm/README.md#chapter-map' }));
297
+ }
298
+ if (i < 3 && String(hook.strength) !== '强' && String(hook.strength) !== 'strong') {
299
+ warnings.push(v2Error('RHYTHM_GOLDEN_THREE_HOOK', { path: p + '.hook.strength', severity: 'warning', message: '黄金三章建议使用强钩子', contract: '40-rhythm/README.md#chapter-map' }));
300
+ }
301
+ const wt = Number(c.word_target);
302
+ if (!Number.isInteger(wt) || wt <= 0) {
303
+ errors.push(v2Error('RHYTHM_WORD_TARGET', { path: p + '.word_target', message: 'word_target 必须是正整数', actual: c.word_target, contract: '40-rhythm/README.md#chapter-map' }));
304
+ } else {
305
+ wordSum += wt;
306
+ }
307
+ }
308
+
309
+ for (let ch = 1; ch <= T; ch += 1) {
310
+ if (!displaySeen.has(ch)) {
311
+ errors.push(v2Error('RHYTHM_CHAPTER_GAP', { path: 'chapter_map.chapters', message: '缺少第 ' + ch + ' 章', contract: '40-rhythm/README.md#chapter-map' }));
312
+ }
313
+ }
314
+ if (Number(scale.capacity.target_words) && Math.abs(wordSum - Number(scale.capacity.target_words)) > T) {
315
+ errors.push(v2Error('RHYTHM_WORD_SUM', {
316
+ path: 'chapter_map.chapters[*].word_target',
317
+ message: '各章字数目标总和必须匹配 capacity.target_words',
318
+ expected: Number(scale.capacity.target_words), actual: wordSum,
319
+ contract: '40-rhythm/README.md#chapter-map',
320
+ reference: '10-scale/scale.json#capacity.target_words',
321
+ }));
322
+ }
323
+
324
+ // 覆盖矩阵
325
+ const coverageChecks = [
326
+ ['RHYTHM_MAIN_COVERAGE', '主线节点', mainNodeIds, coveredMain, '20-macro/lines.json#main_line.nodes'],
327
+ ['RHYTHM_BRANCH_COVERAGE', '支线', branchIds, coveredBranch, '20-macro/lines.json#branches'],
328
+ ['RHYTHM_CONFLICT_COVERAGE', '冲突', conflictIds, coveredConflict, '30-cast/conflicts.json#conflicts'],
329
+ ['RHYTHM_FORESHADOW_COVERAGE', '伏笔', foreshadowIds, coveredForeshadow, '30-cast/foreshadows.json#foreshadows'],
330
+ ['RHYTHM_PAYOFF_COVERAGE', '爽点', payoffIds, coveredPayoff, '30-cast/payoffs.json#payoffs'],
331
+ ['RHYTHM_INFO_COVERAGE', '信息差', infoIds, coveredInfo, '30-cast/information.json#information_gaps'],
332
+ ];
333
+ for (const [code, label, all, covered, reference] of coverageChecks) {
334
+ const missing = [...all].filter((id) => !covered.has(id));
335
+ if (missing.length) {
336
+ errors.push(v2Error(code, {
337
+ path: 'chapter_map.chapters',
338
+ message: label + '没有章节落点:' + missing.slice(0, 12).join('、') + (missing.length > 12 ? ' …' : ''),
339
+ actual: missing,
340
+ contract: '40-rhythm/README.md#coverage',
341
+ reference,
342
+ }));
343
+ }
344
+ }
345
+ const uncoveredEnding = [...endingIds].filter((id) => !coveredEnding.has(id));
346
+ if (uncoveredEnding.length) {
347
+ warnings.push(v2Error('RHYTHM_ENDING_COVERAGE', { path: 'chapter_map.chapters', severity: 'warning', message: '结局节点没有章节落点:' + uncoveredEnding.join('、'), contract: '40-rhythm/README.md#coverage' }));
348
+ }
349
+
350
+ // 伏笔顺序
351
+ for (const [id, rec] of foreshadowFirst.entries()) {
352
+ if (rec.recover !== null && (rec.plant === null || rec.recover < rec.plant)) {
353
+ errors.push(v2Error('RHYTHM_FORESHADOW_ORDER', {
354
+ path: 'chapter_map.chapters',
355
+ message: '伏笔必须先埋后收:' + id,
356
+ actual: rec.plant === null ? '只回收未埋设' : '第 ' + rec.recover + ' 章回收,第 ' + rec.plant + ' 章才埋设',
357
+ hint: '检查 30-cast/foreshadows.json 的 setup 与 payoff_target,再调整章节地图',
358
+ contract: '40-rhythm/README.md#foreshadow-order',
359
+ reference: '30-cast/foreshadows.json#foreshadows',
360
+ }));
361
+ }
362
+ }
363
+
364
+ // ---- 角色知识时间线 ----
365
+ const knowledge = replayKnowledge(chapters, cast);
366
+ errors.push(...knowledge.errors.map((e) => v2Error(e.code, { ...e, contract: '40-rhythm/README.md#knowledge' })));
367
+ warnings.push(...knowledge.warnings.map((e) => v2Error(e.code, { ...e, severity: 'warning', contract: '40-rhythm/README.md#knowledge' })));
368
+ if (!knowledge.events.length && chapters.some((c) => asArray(c.information_gaps).length || asArray(c.foreshadow_actions).length)) {
369
+ warnings.push(v2Error('RHYTHM_KNOWLEDGE_MISSING', {
370
+ path: 'chapter_map.chapters[*].knowledge_events',
371
+ severity: 'warning',
372
+ message: '章节地图尚无角色级知识事件,无法校验“谁在何时、如何知道什么”。建议重跑 rhythm 或补充 knowledge_events。',
373
+ contract: '40-rhythm/README.md#knowledge',
374
+ }));
375
+ }
376
+
377
+ // ---- 细纲 ----
378
+ const outlines = docs.outlines instanceof Map ? docs.outlines : new Map();
379
+ const requireOutlines = opts.requireOutlines !== false;
380
+ const outlineFilter = opts.outlineFilter instanceof Set ? opts.outlineFilter : null;
381
+ if (requireOutlines) {
382
+ for (const [displayNo, c] of byDisplay.entries()) {
383
+ const id = c.chapter_id;
384
+ if (outlineFilter && !outlineFilter.has(id)) continue;
385
+ const outline = id ? outlines.get(id) : null;
386
+ if (!outline) {
387
+ errors.push(v2Error('OUTLINE_MISSING', {
388
+ path: 'outlines/' + (id || '?') + '.json',
389
+ message: '第 ' + displayNo + ' 章缺少可落地细纲',
390
+ contract: '40-rhythm/README.md#outline',
391
+ reference: 'chapter_map.chapters[' + displayNo + ']',
392
+ }));
393
+ continue;
394
+ }
395
+ const p = 'outlines/' + id + '.json';
396
+ const { chapter, hard, how } = outlineCandidates(outline);
397
+ const number = Number(chapter.number || chapter.display_no || chapter.ch);
398
+ if (Number.isInteger(number) && number !== displayNo) {
399
+ errors.push(v2Error('OUTLINE_CHAPTER_MISMATCH', { path: p + '.chapter.number', message: '细纲章号与章节地图不一致', expected: displayNo, actual: number, contract: '40-rhythm/README.md#outline' }));
400
+ }
401
+ if (chapter.chapter_id && chapter.chapter_id !== id) {
402
+ errors.push(v2Error('OUTLINE_CHAPTER_ID_MISMATCH', { path: p + '.chapter.chapter_id', message: '细纲 chapter_id 与章节地图不一致', expected: id, actual: chapter.chapter_id, contract: '40-rhythm/README.md#outline' }));
403
+ }
404
+ if (!chapter.pov || !chapter.pov.id) {
405
+ errors.push(v2Error('OUTLINE_POV', { path: p + '.chapter.pov', message: '细纲必须有视角人物', contract: '40-rhythm/README.md#outline' }));
406
+ } else if (!charIds.has(chapter.pov.id)) {
407
+ errors.push(v2Error('OUTLINE_POV_REF', { path: p + '.chapter.pov.id', message: '视角人物不存在:' + chapter.pov.id, contract: '40-rhythm/README.md#outline' }));
408
+ }
409
+ const oCast = asArray(chapter.cast);
410
+ if (!oCast.length) {
411
+ errors.push(v2Error('OUTLINE_CAST_EMPTY', { path: p + '.chapter.cast', message: '细纲必须列出本章出场人物', contract: '40-rhythm/README.md#outline' }));
412
+ }
413
+ const oCastIds = new Set(oCast.map((x) => (typeof x === 'string' ? x : x && x.id)).filter(Boolean));
414
+ for (const cid of oCastIds) {
415
+ if (!charIds.has(cid)) {
416
+ errors.push(v2Error('OUTLINE_CAST_REF', { path: p + '.chapter.cast', message: '细纲出场人物不存在:' + cid, contract: '40-rhythm/README.md#outline' }));
417
+ }
418
+ if (!asArray(c.cast).includes(cid)) {
419
+ errors.push(v2Error('OUTLINE_CAST_MISMATCH', {
420
+ path: p + '.chapter.cast', message: '细纲出场人物必须与章节地图一致:' + cid,
421
+ contract: '40-rhythm/README.md#outline', reference: 'chapter_map.chapters[' + displayNo + '].cast',
422
+ }));
423
+ }
424
+ }
425
+ for (const cid of asArray(c.cast)) {
426
+ if (!oCastIds.has(cid)) {
427
+ errors.push(v2Error('OUTLINE_CAST_MISMATCH', {
428
+ path: p + '.chapter.cast', message: '细纲漏掉章节地图里的出场人物:' + cid,
429
+ contract: '40-rhythm/README.md#outline', reference: 'chapter_map.chapters[' + displayNo + '].cast',
430
+ }));
431
+ }
432
+ }
433
+ const oLocs = asArray(chapter.locations);
434
+ const oLocIds = new Set(oLocs.map((x) => (typeof x === 'string' ? x : x && x.id)).filter(Boolean));
435
+ if (!oLocIds.size) {
436
+ errors.push(v2Error('OUTLINE_LOCATION_EMPTY', { path: p + '.chapter.locations', message: '细纲必须列出本章地点', contract: '40-rhythm/README.md#outline' }));
437
+ }
438
+ for (const lid of oLocIds) {
439
+ if (!locationIds.has(lid)) errors.push(v2Error('OUTLINE_LOCATION_REF', { path: p + '.chapter.locations', message: '细纲地点不存在:' + lid, contract: '40-rhythm/README.md#outline' }));
440
+ }
441
+ for (const lid of asArray(c.locations)) {
442
+ if (!oLocIds.has(lid)) {
443
+ errors.push(v2Error('OUTLINE_LOCATION_MISMATCH', {
444
+ path: p + '.chapter.locations', message: '细纲漏掉章节地图里的地点:' + lid,
445
+ contract: '40-rhythm/README.md#outline', reference: 'chapter_map.chapters[' + displayNo + '].locations',
446
+ }));
447
+ }
448
+ }
449
+
450
+ const scenes = asArray(how.scenes);
451
+ if (!scenes.length) {
452
+ errors.push(v2Error('OUTLINE_SCENES', { path: p + '.chapter.how.scenes', message: '细纲至少要有 1 个场景', contract: '40-rhythm/README.md#outline' }));
453
+ }
454
+ for (let j = 0; j < scenes.length; j += 1) {
455
+ const s = scenes[j] || {};
456
+ for (const field of ['goal', 'obstacle', 'turn', 'outcome']) {
457
+ if (typeof s[field] !== 'string' || !s[field].trim()) {
458
+ errors.push(v2Error('OUTLINE_BEAT_INCOMPLETE', {
459
+ path: p + '.chapter.how.scenes[' + j + '].' + field,
460
+ message: '场景缺少 ' + field,
461
+ contract: '40-rhythm/README.md#outline',
462
+ }));
463
+ }
464
+ }
465
+ if (s.location && !oLocIds.has(s.location)) {
466
+ errors.push(v2Error('OUTLINE_SCENE_LOCATION', { path: p + '.chapter.how.scenes[' + j + '].location', message: '场景地点不在本章地点里:' + s.location, contract: '40-rhythm/README.md#outline' }));
467
+ }
468
+ }
469
+ const must = asArray(hard.must_write || how.must_write || chapter.must_write);
470
+ const mustNot = asArray(hard.must_not_write || how.must_not_write || chapter.must_not_write);
471
+ if (!must.length) errors.push(v2Error('OUTLINE_MUST_WRITE', { path: p + '.chapter.hard_constraints.must_write', message: '细纲必须写清楚本章必须写什么', contract: '40-rhythm/README.md#outline' }));
472
+ if (!mustNot.length) errors.push(v2Error('OUTLINE_BAN_MISSING', { path: p + '.chapter.hard_constraints.must_not_write', message: '细纲必须写清楚本章绝对不能写什么', contract: '40-rhythm/README.md#outline' }));
473
+ const ending = how.ending || hard.ending || {};
474
+ if (!ending.type || !ending.text || !ending.hook) {
475
+ errors.push(v2Error('OUTLINE_ENDING', { path: p + '.chapter.how.ending', message: '细纲必须说明本章如何结束与章尾钩子', contract: '40-rhythm/README.md#outline' }));
476
+ }
477
+ requireString(errors, whyOf(chapter), p + '.chapter.why', 'stakes', '40-rhythm/README.md#outline');
478
+
479
+ // 硬约束与章节地图一致
480
+ const mapFs = actionSet(c.foreshadow_actions);
481
+ const outlineFs = actionSet(hard.foreshadow_actions || chapter.foreshadow_actions || how.foreshadow_actions);
482
+ for (const key of mapFs) {
483
+ if (!outlineFs.has(key)) {
484
+ errors.push(v2Error('OUTLINE_FORESHADOW_MISMATCH', {
485
+ path: p + '.chapter.hard_constraints.foreshadow_actions',
486
+ message: '细纲漏掉章节地图的伏笔动作:' + key,
487
+ contract: '40-rhythm/README.md#outline',
488
+ reference: 'chapter_map.chapters[' + displayNo + '].foreshadow_actions',
489
+ }));
490
+ }
491
+ }
492
+ const mapPl = actionSet(c.payoff_actions);
493
+ const outlinePl = actionSet(hard.payoff_actions || chapter.payoff_actions || how.payoff_actions);
494
+ for (const key of mapPl) {
495
+ if (!outlinePl.has(key)) {
496
+ errors.push(v2Error('OUTLINE_PAYOFF_MISMATCH', {
497
+ path: p + '.chapter.hard_constraints.payoff_actions',
498
+ message: '细纲漏掉章节地图的爽点动作:' + key,
499
+ contract: '40-rhythm/README.md#outline',
500
+ reference: 'chapter_map.chapters[' + displayNo + '].payoff_actions',
501
+ }));
502
+ }
503
+ }
504
+ const coverage = chapter.coverage || outline.coverage || {};
505
+ const mapMain = new Set(asArray(c.why && c.why.main_line_nodes));
506
+ const covMain = new Set(asArray(coverage.main_line));
507
+ for (const id of mapMain) if (!covMain.has(id)) {
508
+ errors.push(v2Error('OUTLINE_COVERAGE_GAP', { path: p + '.chapter.coverage.main_line', message: '细纲覆盖矩阵漏掉主线节点:' + id, contract: '40-rhythm/README.md#outline' }));
509
+ }
510
+ const mapConflict = new Set(asArray(c.why && c.why.conflict));
511
+ const covConflict = new Set(asArray(coverage.conflict));
512
+ for (const id of mapConflict) if (!covConflict.has(id)) {
513
+ errors.push(v2Error('OUTLINE_COVERAGE_GAP', { path: p + '.chapter.coverage.conflict', message: '细纲覆盖矩阵漏掉冲突:' + id, contract: '40-rhythm/README.md#outline' }));
514
+ }
515
+ const mapInfo = actionIds(c.information_gaps);
516
+ const covInfo = actionIds(coverage.information);
517
+ for (const id of mapInfo) if (!covInfo.has(id)) {
518
+ errors.push(v2Error('OUTLINE_COVERAGE_GAP', { path: p + '.chapter.coverage.information', message: '细纲覆盖矩阵漏掉信息差:' + id, contract: '40-rhythm/README.md#outline' }));
519
+ }
520
+
521
+ // 知识状态必须与章节地图回放一致;细纲是最接近正文的一层,不能再各自发明知识。
522
+ const expectedKnowledge = knowledge.snapshots.get(displayNo) || new Map();
523
+ const snapshotRows = [];
524
+ for (const person of asArray(chapter.cast)) {
525
+ if (!person || !Array.isArray(person.knowledge)) continue;
526
+ for (const item of person.knowledge) if (item && typeof item === 'object') snapshotRows.push(item);
527
+ }
528
+ if (Array.isArray(hard.knowledge_state)) snapshotRows.push(...hard.knowledge_state.filter((x) => x && typeof x === 'object'));
529
+ const snapshot = validateKnowledgeSnapshot(snapshotRows, expectedKnowledge, {
530
+ chapterNo: displayNo,
531
+ path: p + '.chapter.cast[].knowledge',
532
+ });
533
+ errors.push(...snapshot.errors.map((e) => v2Error(e.code, { ...e, contract: '40-rhythm/README.md#knowledge' })));
534
+ warnings.push(...snapshot.warnings.map((e) => v2Error(e.code, { ...e, severity: 'warning', contract: '40-rhythm/README.md#knowledge' })));
535
+
536
+ const mapEvents = asArray(c.knowledge_events);
537
+ const outlineEvents = asArray(chapter.knowledge_events);
538
+ if (mapEvents.length || outlineEvents.length) {
539
+ const mapKeys = new Set(mapEvents.map(eventKey));
540
+ const outlineKeys = new Set(outlineEvents.map(eventKey));
541
+ for (const key of mapKeys) {
542
+ if (!outlineKeys.has(key)) {
543
+ errors.push(v2Error('OUTLINE_KNOWLEDGE_MISSING', {
544
+ path: p + '.chapter.knowledge_events',
545
+ message: '细纲漏掉章节地图的知识事件:' + key,
546
+ contract: '40-rhythm/README.md#knowledge',
547
+ reference: 'chapter_map.chapters[' + displayNo + '].knowledge_events',
548
+ }));
549
+ }
550
+ }
551
+ for (const key of outlineKeys) {
552
+ if (!mapKeys.has(key)) {
553
+ warnings.push(v2Error('OUTLINE_KNOWLEDGE_EXTRA', {
554
+ path: p + '.chapter.knowledge_events',
555
+ severity: 'warning',
556
+ message: '细纲新增了章节地图没有的知识事件:' + key,
557
+ contract: '40-rhythm/README.md#knowledge',
558
+ }));
559
+ }
560
+ }
561
+ }
562
+ const continuity = chapter.continuity || outline.continuity || {};
563
+ if (displayNo > 1 && !continuity.from_prev) {
564
+ errors.push(v2Error('OUTLINE_CONTINUITY_GAP', { path: p + '.chapter.continuity.from_prev', message: '非首章细纲必须说明与上一章的衔接', contract: '40-rhythm/README.md#outline' }));
565
+ }
566
+ if (displayNo < T && !continuity.to_next) {
567
+ errors.push(v2Error('OUTLINE_CONTINUITY_GAP', { path: p + '.chapter.continuity.to_next', message: '非末章细纲必须说明与下一章的衔接', contract: '40-rhythm/README.md#outline' }));
568
+ }
569
+ if (outline._meta && Array.isArray(outline._meta.context_files)) {
570
+ for (const rel of outline._meta.context_files) {
571
+ if (typeof rel !== 'string' || rel.startsWith('/') || rel.includes('..')) {
572
+ errors.push(v2Error('OUTLINE_CONTEXT_MISSING', { path: p + '._meta.context_files', message: '细纲上下文必须是阶段目录内的相对路径', actual: rel, contract: '40-rhythm/README.md#outline' }));
573
+ continue;
574
+ }
575
+ const target = path.join(path.dirname(path.join(opts.stageDir || '.', path.basename(p))), rel);
576
+ if (opts.stageDir && !fs.existsSync(target)) {
577
+ errors.push(v2Error('OUTLINE_CONTEXT_MISSING', { path: p + '._meta.context_files', message: '细纲引用的上下文文件不存在:' + rel, contract: '40-rhythm/README.md#outline' }));
578
+ }
579
+ }
580
+ }
581
+ }
582
+ const extra = [...outlines.keys()].filter((id) => !chapterIdSet.has(id));
583
+ if (!outlineFilter && extra.length) {
584
+ warnings.push(v2Error('OUTLINE_EXTRA', { path: 'outlines', severity: 'warning', message: '存在章节地图里没有的细纲:' + extra.slice(0, 10).join('、'), contract: '40-rhythm/README.md#outline' }));
585
+ }
586
+ }
587
+
588
+ return { ok: errors.length === 0, errors, warnings, data: docs };
589
+ }
590
+
591
+ function whyOf(chapter) {
592
+ return chapter.why || {};
593
+ }
594
+
595
+ /** 从阶段目录读取章节地图与所有细纲。 */
596
+ export function loadRhythm(projectRoot, stageDirPath, opts = {}) {
597
+ const chapterMap = opts.chapterMap || readJson(path.join(stageDirPath, 'chapter_map.json'), null);
598
+ const outlineDir = path.join(stageDirPath, 'outlines');
599
+ const outlines = new Map();
600
+ let names = [];
601
+ try {
602
+ names = fs.readdirSync(outlineDir);
603
+ } catch {
604
+ names = [];
605
+ }
606
+ const only = opts.outlineIds instanceof Set ? opts.outlineIds : null;
607
+ for (const name of names) {
608
+ if (!name.endsWith('.json') || name.startsWith('.')) continue;
609
+ const fileId = name.replace(/\.json$/, '');
610
+ if (only && !only.has(fileId)) continue;
611
+ const data = readJson(path.join(outlineDir, name), null);
612
+ if (!data) continue;
613
+ const chapter = data.chapter || {};
614
+ const id = chapter.chapter_id || chapter.id || fileId;
615
+ outlines.set(id, data);
616
+ }
617
+ return { chapter_map: chapterMap || { chapters: [] }, outlines };
618
+ }