soma-core 1.4.2 → 1.5.1

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 (216) hide show
  1. package/bin/component.cjs +240 -0
  2. package/bin/eject.cjs +174 -0
  3. package/bin/migrate.cjs +362 -0
  4. package/core/build/blocks/block-compile.ts +143 -17
  5. package/core/build/blocks/migrate-flat.ts +118 -25
  6. package/core/build/compiler/page-declaration-compiler.ts +149 -5
  7. package/core/build/compiler/route-builder.ts +0 -4
  8. package/core/build/compiler/soma-precompile.js +431 -26
  9. package/core/build/component/component-graph.ts +28 -105
  10. package/core/build/component/package-loader.ts +18 -4
  11. package/core/build/eject/component-eject.ts +8 -2
  12. package/core/build/layout/constraint-solver.ts +131 -16
  13. package/core/build/layout/css-emitter.ts +29 -10
  14. package/core/build/ooui/artifact/artifact.types.ts +275 -0
  15. package/core/build/ooui/artifact/emit-artifact.ts +269 -0
  16. package/core/build/ooui/artifact/emit-html.ts +204 -0
  17. package/core/build/ooui/artifact/index.ts +53 -0
  18. package/core/build/ooui/artifact/spatial-tree.ts +268 -0
  19. package/core/build/ooui/artifact/utility-class.ts +83 -0
  20. package/core/build/ooui/composition/candidate.types.ts +323 -0
  21. package/core/build/ooui/composition/control-model.ts +114 -0
  22. package/core/build/ooui/composition/registry.ts +520 -0
  23. package/core/build/ooui/composition/select.ts +232 -0
  24. package/core/build/ooui/diagnostics/codes.ts +230 -0
  25. package/core/build/ooui/diagnostics/error.ts +235 -0
  26. package/core/build/ooui/diagnostics/format.ts +106 -0
  27. package/core/build/ooui/diagnostics/index.ts +62 -0
  28. package/core/build/ooui/diagnostics/issue.types.ts +112 -0
  29. package/core/build/ooui/engine-slot.ts +184 -0
  30. package/core/build/ooui/index.ts +1427 -0
  31. package/core/build/ooui/intent/derive-intent.ts +450 -0
  32. package/core/build/ooui/intent/field-rules.ts +330 -0
  33. package/core/build/ooui/intent/intent.types.ts +476 -0
  34. package/core/build/ooui/intent/verb-events.ts +285 -0
  35. package/core/build/ooui/page/budget.ts +503 -0
  36. package/core/build/ooui/page/compose-page.ts +736 -0
  37. package/core/build/ooui/page/index.ts +66 -0
  38. package/core/build/ooui/page/page.types.ts +402 -0
  39. package/core/build/ooui/page/region-row.ts +241 -0
  40. package/core/build/ooui/semantic/expand-page-surface.ts +180 -0
  41. package/core/build/ooui/semantic/kernel.types.ts +187 -0
  42. package/core/build/ooui/semantic/normalize.ts +784 -0
  43. package/core/build/ooui/semantic/origin.types.ts +211 -0
  44. package/core/build/ooui/semantic/role-alias.ts +154 -0
  45. package/core/build/ooui/semantic/shorthand.ts +233 -0
  46. package/core/build/ooui/semantic/surface.types.ts +78 -0
  47. package/core/build/ooui/semantic/validate-request.ts +267 -0
  48. package/core/build/ooui/spatial/emit-utility.ts +480 -0
  49. package/core/build/ooui/spatial/ir.types.ts +443 -0
  50. package/core/build/ooui/spatial/registry.ts +565 -0
  51. package/core/build/ooui/spatial/solve.ts +769 -0
  52. package/core/build/ooui/spatial/utility.types.ts +242 -0
  53. package/core/build/ooui/trace/index.ts +40 -0
  54. package/core/build/ooui/trace/render.ts +190 -0
  55. package/core/build/ooui/trace/source.types.ts +69 -0
  56. package/core/build/ooui/trace/trace.types.ts +326 -0
  57. package/core/client/component/component-expander.ts +18 -3
  58. package/core/client/component/component-manager.ts +4 -0
  59. package/core/client/component/example-blocks.ts +1 -7
  60. package/core/client/component/theme-component-tokens.ts +12 -15
  61. package/core/client/component/verify-pack.ts +43 -0
  62. package/core/client/data/data-object-resolver.ts +27 -8
  63. package/core/client/diagnostics/telemetry-context.ts +0 -4
  64. package/core/client/engine/asset-resolver.ts +0 -1
  65. package/core/client/engine/event-engine.ts +7 -3
  66. package/core/client/engine/soma-bootstrap.ts +292 -34
  67. package/core/client/engine/style-binder.ts +0 -1
  68. package/core/client/infra/http/IHttpClient.ts +0 -6
  69. package/core/client/infra/http/SomaHttpService.ts +4 -1
  70. package/core/client/infra/notify/DomNotifyService.ts +1 -1
  71. package/core/client/infra/router/router-path.ts +0 -6
  72. package/core/client/parser/annotation-extractor.ts +1 -1
  73. package/core/client/parser/blocks/namespace.ts +30 -5
  74. package/core/client/parser/blocks/page-parser.ts +44 -1
  75. package/core/client/parser/blocks/use-parser.ts +266 -46
  76. package/core/client/parser/data-config-parser.ts +71 -3
  77. package/core/client/parser/layout-parser.ts +4 -3
  78. package/core/client/parser/reaction-parser.ts +0 -1
  79. package/core/client/parser/ui-config-parser.ts +212 -74
  80. package/core/client/theme/scene-tokens.ts +9 -3
  81. package/core/client/theme/theme-axes.ts +74 -16
  82. package/core/client/theme/theme-engine.ts +341 -70
  83. package/core/client/theme/tone-solver.ts +9 -8
  84. package/core/index.ts +24 -5
  85. package/core/server/api/load-host-service.ts +8 -2
  86. package/core/server/declaration-safety.ts +11 -0
  87. package/core/server/host/page-response-request.ts +0 -2
  88. package/core/server/host/published-page-artifact-cache.ts +244 -0
  89. package/core/server/host/published-page-assets.ts +44 -5
  90. package/core/server/mock/node/cli.mjs +0 -1
  91. package/core/server/mock/node/schema-generator.mjs +0 -1
  92. package/core/server/ooui.ts +34 -0
  93. package/core/server/page-assets.ts +8 -0
  94. package/core/server/page-declaration.ts +18 -0
  95. package/core/server/registry/asset-store.ts +327 -0
  96. package/core/server/registry/declaration-precompiler.ts +49 -6
  97. package/core/server/registry/index.ts +47 -1
  98. package/core/server/registry/page-registry-repository.ts +6 -2
  99. package/core/server/registry/page-registry-store.ts +133 -19
  100. package/core/server/registry/publish-asset-pin.ts +557 -0
  101. package/core/server/registry/services/pages/List.ts +15 -3
  102. package/core/server/registry/validation/compile-gate.ts +180 -8
  103. package/core/server/registry/validation/declaration-safety-scan.ts +113 -23
  104. package/core/shared/component/derive-sample.ts +201 -16
  105. package/core/shared/component/field-lines.ts +98 -8
  106. package/core/shared/container-breakpoints.ts +46 -0
  107. package/core/shared/object-semantics.ts +240 -0
  108. package/core/shared/ooui-declaration.ts +235 -0
  109. package/core/shared/types/component-contract.types.ts +7 -6
  110. package/core/shared/types/data-config.types.ts +95 -0
  111. package/core/shared/types/domain.types.ts +0 -24
  112. package/core/shared/types/engine.types.ts +0 -5
  113. package/core/shared/types/layout-constraint.types.ts +16 -0
  114. package/core/shared/types/ui-config.types.ts +163 -13
  115. package/dist/cli/soma.js +24 -11
  116. package/dist/core/index.cjs +1829 -1028
  117. package/dist/types/build/blocks/block-compile.d.ts +16 -1
  118. package/dist/types/build/blocks/migrate-flat.d.ts +17 -0
  119. package/dist/types/build/compiler/page-declaration-compiler.d.ts +46 -0
  120. package/dist/types/build/compiler/route-builder.d.ts +0 -1
  121. package/dist/types/build/component/component-graph.d.ts +17 -53
  122. package/dist/types/build/component/package-loader.d.ts +8 -1
  123. package/dist/types/build/layout/constraint-solver.d.ts +46 -4
  124. package/dist/types/build/ooui/artifact/artifact.types.d.ts +180 -0
  125. package/dist/types/build/ooui/artifact/emit-artifact.d.ts +58 -0
  126. package/dist/types/build/ooui/artifact/emit-html.d.ts +53 -0
  127. package/dist/types/build/ooui/artifact/index.d.ts +5 -0
  128. package/dist/types/build/ooui/artifact/spatial-tree.d.ts +48 -0
  129. package/dist/types/build/ooui/artifact/utility-class.d.ts +46 -0
  130. package/dist/types/build/ooui/composition/candidate.types.d.ts +206 -0
  131. package/dist/types/build/ooui/composition/control-model.d.ts +37 -0
  132. package/dist/types/build/ooui/composition/registry.d.ts +66 -0
  133. package/dist/types/build/ooui/composition/select.d.ts +63 -0
  134. package/dist/types/build/ooui/diagnostics/codes.d.ts +28 -0
  135. package/dist/types/build/ooui/diagnostics/error.d.ts +135 -0
  136. package/dist/types/build/ooui/diagnostics/format.d.ts +51 -0
  137. package/dist/types/build/ooui/diagnostics/index.d.ts +25 -0
  138. package/dist/types/build/ooui/diagnostics/issue.types.d.ts +56 -0
  139. package/dist/types/build/ooui/engine-slot.d.ts +41 -0
  140. package/dist/types/build/ooui/index.d.ts +286 -0
  141. package/dist/types/build/ooui/intent/derive-intent.d.ts +32 -0
  142. package/dist/types/build/ooui/intent/field-rules.d.ts +99 -0
  143. package/dist/types/build/ooui/intent/intent.types.d.ts +318 -0
  144. package/dist/types/build/ooui/intent/verb-events.d.ts +70 -0
  145. package/dist/types/build/ooui/page/budget.d.ts +28 -0
  146. package/dist/types/build/ooui/page/compose-page.d.ts +41 -0
  147. package/dist/types/build/ooui/page/index.d.ts +4 -0
  148. package/dist/types/build/ooui/page/page.types.d.ts +264 -0
  149. package/dist/types/build/ooui/page/region-row.d.ts +16 -0
  150. package/dist/types/build/ooui/semantic/expand-page-surface.d.ts +33 -0
  151. package/dist/types/build/ooui/semantic/kernel.types.d.ts +125 -0
  152. package/dist/types/build/ooui/semantic/normalize.d.ts +45 -0
  153. package/dist/types/build/ooui/semantic/origin.types.d.ts +95 -0
  154. package/dist/types/build/ooui/semantic/role-alias.d.ts +35 -0
  155. package/dist/types/build/ooui/semantic/shorthand.d.ts +60 -0
  156. package/dist/types/build/ooui/semantic/surface.types.d.ts +16 -0
  157. package/dist/types/build/ooui/semantic/validate-request.d.ts +19 -0
  158. package/dist/types/build/ooui/spatial/emit-utility.d.ts +43 -0
  159. package/dist/types/build/ooui/spatial/ir.types.d.ts +280 -0
  160. package/dist/types/build/ooui/spatial/registry.d.ts +110 -0
  161. package/dist/types/build/ooui/spatial/solve.d.ts +42 -0
  162. package/dist/types/build/ooui/spatial/utility.types.d.ts +191 -0
  163. package/dist/types/build/ooui/trace/index.d.ts +3 -0
  164. package/dist/types/build/ooui/trace/render.d.ts +42 -0
  165. package/dist/types/build/ooui/trace/source.types.d.ts +25 -0
  166. package/dist/types/build/ooui/trace/trace.types.d.ts +143 -0
  167. package/dist/types/client/component/component-expander.d.ts +1 -0
  168. package/dist/types/client/component/example-blocks.d.ts +0 -2
  169. package/dist/types/client/component/theme-component-tokens.d.ts +8 -5
  170. package/dist/types/client/data/data-object-resolver.d.ts +10 -2
  171. package/dist/types/client/diagnostics/telemetry-context.d.ts +0 -1
  172. package/dist/types/client/engine/soma-bootstrap.d.ts +123 -10
  173. package/dist/types/client/infra/http/IHttpClient.d.ts +0 -5
  174. package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
  175. package/dist/types/client/infra/router/router-path.d.ts +0 -2
  176. package/dist/types/client/parser/blocks/namespace.d.ts +20 -0
  177. package/dist/types/client/parser/blocks/page-parser.d.ts +9 -0
  178. package/dist/types/client/parser/blocks/use-parser.d.ts +38 -4
  179. package/dist/types/client/parser/data-config-parser.d.ts +13 -0
  180. package/dist/types/client/parser/ui-config-parser.d.ts +1 -11
  181. package/dist/types/client/theme/scene-tokens.d.ts +9 -3
  182. package/dist/types/client/theme/theme-axes.d.ts +64 -1
  183. package/dist/types/client/theme/theme-engine.d.ts +13 -8
  184. package/dist/types/client/theme/tone-solver.d.ts +9 -5
  185. package/dist/types/index.d.ts +4 -6
  186. package/dist/types/server/declaration-safety.d.ts +7 -0
  187. package/dist/types/server/host/page-response-request.d.ts +0 -1
  188. package/dist/types/server/host/published-page-artifact-cache.d.ts +64 -0
  189. package/dist/types/server/ooui.d.ts +18 -0
  190. package/dist/types/server/page-assets.d.ts +7 -0
  191. package/dist/types/server/page-declaration.d.ts +14 -0
  192. package/dist/types/server/registry/asset-store.d.ts +112 -0
  193. package/dist/types/server/registry/declaration-precompiler.d.ts +23 -3
  194. package/dist/types/server/registry/index.d.ts +3 -1
  195. package/dist/types/server/registry/page-registry-store.d.ts +57 -4
  196. package/dist/types/server/registry/publish-asset-pin.d.ts +166 -0
  197. package/dist/types/server/registry/services/pages/List.d.ts +9 -3
  198. package/dist/types/server/registry/validation/compile-gate.d.ts +63 -0
  199. package/dist/types/shared/component/derive-sample.d.ts +14 -0
  200. package/dist/types/shared/container-breakpoints.d.ts +42 -0
  201. package/dist/types/shared/object-semantics.d.ts +42 -0
  202. package/dist/types/shared/ooui-declaration.d.ts +135 -0
  203. package/dist/types/shared/types/component-contract.types.d.ts +7 -6
  204. package/dist/types/shared/types/data-config.types.d.ts +86 -0
  205. package/dist/types/shared/types/domain.types.d.ts +0 -21
  206. package/dist/types/shared/types/engine.types.d.ts +0 -4
  207. package/dist/types/shared/types/layout-constraint.types.d.ts +1 -0
  208. package/dist/types/shared/types/ui-config.types.d.ts +157 -12
  209. package/next.config.js +106 -13
  210. package/package.json +29 -2
  211. package/core/client/host-bridge/index.ts +0 -27
  212. package/core/client/parser/yaml-loader.ts +0 -36
  213. package/core/shared/manifest/manifest-contract.ts +0 -12
  214. package/dist/types/client/host-bridge/index.d.ts +0 -9
  215. package/dist/types/client/parser/yaml-loader.d.ts +0 -21
  216. package/dist/types/shared/manifest/manifest-contract.d.ts +0 -4
@@ -1,14 +1,16 @@
1
- // 组件依赖图与可寻址目录(REQ-010 F-46)。
1
+ // 组件依赖图(REQ-010 F-46):契约 `uses` 建图、检环、求传递闭包,并与 CSS 依赖表对账。
2
2
  //
3
- // ── 两个具体的坏结果 ──────────────────────────────────────────────────
4
- // ① **少拉一个依赖**。TreeSelect 的面板模板用了 Tree 分片里的类,但这条依赖
5
- // 只登记在 soma-ui 的一张手工表里、且曾经漏登记过——只用 TreeSelect 不用 Tree 的
6
- // 页面上,展开箭头与节点文字是无样式的。它不报错,只是长得不对。
7
- // ② **说不出这份副本从哪来**。shadcn 的 registry 里没有版本、没有哈希、没有依赖,
8
- // 所以"我这份 Select 是从哪一版复制的、它还需要谁"这两个问题都答不出来。
3
+ // ── 它防的坏结果 ──────────────────────────────────────────────────────
4
+ // **少拉一个依赖**。TreeSelect 的面板模板用了 Tree 分片里的类,但这条依赖
5
+ // 只登记在 soma-ui 的一张手工表里、且曾经漏登记过——只用 TreeSelect 不用 Tree 的
6
+ // 页面上,展开箭头与节点文字是无样式的。它不报错,只是长得不对。
9
7
  //
10
- // 把依赖写进契约(`uses`),这两件事都有了答案:拉组件时按图拉齐,
11
- // 发目录时每一项带版本、哈希与依赖。
8
+ // 把依赖写进契约(`uses`),再拿图与 CSS 依赖表逐项对账(`reconcileCssDeps`,
9
+ // 判据在 soma-ui/tests/component-css-deps.test.ts),这条依赖就有了机器看得见的真源;
10
+ // `soma component eject` 列副本仍需的组件依赖时读的也是契约 `uses`。
11
+ //
12
+ // (这里曾经还有「按图拉齐」与带版本/哈希/依赖的 `registry.json` 目录产出器,
13
+ // 全仓从未有生产调用者、目录文件也从未产出过——已删除,不再对外承诺。)
12
14
 
13
15
  import type { SomaComponentDefinition } from '../../client/component/component-definition.types';
14
16
 
@@ -33,9 +35,8 @@ export interface ComponentGraph {
33
35
  /**
34
36
  * 建图。
35
37
  *
36
- * 缺失依赖与环**不抛**,而是收进产物:这个函数同时服务于闸门与 CLI,
37
- * 闸门要拿到全部问题一次报完(抛的话作者修一条跑一次,修十条跑十次),
38
- * CLI 则要在有问题时也能给出一份"能拉的先拉上"的清单。判定归调用方。
38
+ * 缺失依赖与环**不抛**,而是收进产物:闸门要拿到全部问题一次报完
39
+ * (抛的话作者修一条跑一次,修十条跑十次)。判定归调用方。
39
40
  */
40
41
  export function buildComponentGraph(
41
42
  definitions: Record<string, SomaComponentDefinition>,
@@ -92,102 +93,24 @@ export function buildComponentGraph(
92
93
  }
93
94
 
94
95
  /**
95
- * 按图拉齐:给一组组件名,返回它们加上全部传递依赖的名单(拓扑序)。
96
+ * 契约 `uses` 与 CSS 依赖表的对账。**两个方向的含义不同,判据不对称。**
96
97
  *
97
- * 拓扑序而不是字母序:注册组件时被依赖的要先在册,否则合成期查注册表会落空。
98
- */
99
- export function resolveWithDependencies(
100
- graph: ComponentGraph,
101
- wanted: readonly string[],
102
- ): string[] {
103
- const out: string[] = [];
104
- const seen = new Set<string>();
105
- const visit = (name: string): void => {
106
- if (seen.has(name)) return;
107
- seen.add(name);
108
- const node = graph.nodes[name];
109
- if (!node) return; // 未知名字由调用方另行报错,这里不吞也不猜
110
- for (const dep of node.uses) visit(dep);
111
- out.push(name);
112
- };
113
- for (const name of wanted) visit(name);
114
- return out;
115
- }
116
-
117
- /** `registry.json` 的一项 */
118
- export interface ComponentRegistryEntry {
119
- name: string;
120
- layer: string;
121
- /** 契约版本(破坏性变更升 major) */
122
- version: string;
123
- /** 源码内容哈希:回答"我这份是不是官方那一份" */
124
- sha256: string;
125
- /** 直接依赖 */
126
- uses: string[];
127
- /** 传递依赖(拉齐时按它一次拉全) */
128
- requires: string[];
129
- /** 源文件相对路径(eject / upgrade 的取源位置) */
130
- source: string;
131
- description: string;
132
- }
133
-
134
- export interface ComponentRegistryManifest {
135
- /** 目录格式版本:读的人据此判断自己认不认得这份文件 */
136
- schema: 1;
137
- /** 产出这份目录的包与版本 */
138
- pack: { name: string; version: string };
139
- generatedFrom: string;
140
- components: ComponentRegistryEntry[];
141
- }
142
-
143
- /**
144
- * 产出可寻址目录。
98
+ * 头一版这里写的是"两张表必须一致",接线实测之后发现那个前提是错的
99
+ * (11 条不一致里 10 条属于合法的那个方向):
145
100
  *
146
- * 与 shadcn registry 的差别就是这三列:`version` / `sha256` / `requires`。
147
- * 少了它们,一份被复制出去的组件就永久脱离了升级链路——
148
- * 而"脱离了"这件事本身也是看不出来的。
149
- */
150
- export function buildRegistryManifest(
151
- definitions: Record<string, SomaComponentDefinition>,
152
- options: {
153
- packName: string;
154
- packVersion: string;
155
- /** 组件名 → 源码相对路径 */
156
- sourceOf(name: string): string;
157
- /** 组件名 → 源码内容哈希 */
158
- hashOf(name: string): string;
159
- },
160
- ): ComponentRegistryManifest {
161
- const graph = buildComponentGraph(definitions);
162
- const components = Object.keys(definitions)
163
- .sort()
164
- .map((name) => {
165
- const contract = definitions[name].contract;
166
- return {
167
- name,
168
- layer: String(contract.layer),
169
- version: String(contract.version),
170
- sha256: options.hashOf(name),
171
- uses: graph.nodes[name].uses,
172
- requires: graph.nodes[name].closure,
173
- source: options.sourceOf(name),
174
- description: String(contract.description ?? ''),
175
- };
176
- });
177
- return {
178
- schema: 1,
179
- pack: { name: options.packName, version: options.packVersion },
180
- generatedFrom: `${options.packName}@${options.packVersion}`,
181
- components,
182
- };
183
- }
184
-
185
- /**
186
- * 契约 `uses` 与 CSS 依赖表的对账。
101
+ * · **`onlyInCss`(CSS 表有、契约没有)= 缺陷。**
102
+ * 借样式而不建实例的组件(如 `HelpConsole` 内联写 `class="soma-accordion"`)
103
+ * 必须在契约 `uses` 里登记那个被借的组件,否则 `soma component eject`
104
+ * 少拉一个源文件,副本里那一块**没有样式**——渲染正常、控制台一行都没有。
105
+ * `COMPONENT_CSS_DEPS` 少写一条 `TreeSelect: ['Tree']` 那次就是这条。
106
+ *
107
+ * · **`onlyInContract`(契约有、CSS 表没有)= 正常,不是缺陷。**
108
+ * 父组件在模板里嵌了子组件的**真实例**时,子实例挂载时自己注样式,
109
+ * 父不需要 CSS 依赖。反而登记了会让子组件的样式在它没被渲染时也被注入。
187
110
  *
188
- * 两张表都在描述"这个组件还需要谁",所以它们必须一致。不对账的话,
189
- * 一条只写在 CSS 表里的依赖会让 eject 少拉一个文件,而一条只写在契约里的依赖
190
- * 会让页面少注一段样式——两种坏法都不报错,都只是长得不对。
111
+ * 所以本函数**两个方向都报**(它是报告器,不是裁决者),
112
+ * 由调用方按上面的语义决定哪一侧是硬红。判据在
113
+ * `soma-ui/tests/component-css-deps.test.ts`。
191
114
  */
192
115
  export function reconcileCssDeps(
193
116
  graph: ComponentGraph,
@@ -32,8 +32,13 @@ import {
32
32
  import type { SomaComponentDefinition } from '../../client/component/component-definition.types';
33
33
 
34
34
  export class SomaComponentPackageError extends Error {
35
- constructor(message: string, readonly packageDir: string) {
36
- super(`[SomaComponentPackage] ${message}(组件包: ${packageDir})`);
35
+ /**
36
+ * `options.cause`:包一层解析器原错误时带上它。消息里只留得下原错误的 message,
37
+ * 带 `line` / `blockId` / `path` 的结构化错误(SomaFieldLinesError、SomaDataConfigError…)
38
+ * 与原始栈只有经 `cause` 才传得到调用方——不带就在这一层蒸发。
39
+ */
40
+ constructor(message: string, readonly packageDir: string, options?: { cause?: unknown }) {
41
+ super(`[SomaComponentPackage] ${message}(组件包: ${packageDir})`, options);
37
42
  this.name = 'SomaComponentPackageError';
38
43
  }
39
44
  }
@@ -100,7 +105,11 @@ export function loadComponentPackageEntry(packageDir: string): PackageEntry {
100
105
  try {
101
106
  definition.uiConfig = parseUiConfig(yaml.load(fs.readFileSync(uiPath, 'utf-8')));
102
107
  } catch (error) {
103
- throw new SomaComponentPackageError(`ui.yaml 非法: ${(error as Error).message}`, packageDir);
108
+ throw new SomaComponentPackageError(
109
+ `ui.yaml 非法: ${(error as Error).message}`,
110
+ packageDir,
111
+ { cause: error },
112
+ );
104
113
  }
105
114
  }
106
115
 
@@ -110,7 +119,11 @@ export function loadComponentPackageEntry(packageDir: string): PackageEntry {
110
119
  const rawEvents = yaml.load(fs.readFileSync(eventPath, 'utf-8'));
111
120
  definition.eventConfig = parseEventConfig((rawEvents ?? {}) as Record<string, never>);
112
121
  } catch (error) {
113
- throw new SomaComponentPackageError(`event.yaml 非法: ${(error as Error).message}`, packageDir);
122
+ throw new SomaComponentPackageError(
123
+ `event.yaml 非法: ${(error as Error).message}`,
124
+ packageDir,
125
+ { cause: error },
126
+ );
114
127
  }
115
128
  }
116
129
 
@@ -254,6 +267,7 @@ function normalizeFieldBlocks(contract: Record<string, unknown>, packageDir: str
254
267
  throw new SomaComponentPackageError(
255
268
  `contract.yaml 的 ${key} 文本块非法:${(error as Error).message}`,
256
269
  packageDir,
270
+ { cause: error },
257
271
  );
258
272
  }
259
273
  }
@@ -87,8 +87,14 @@ const PROVENANCE_MARK = '@soma-ejected-from';
87
87
  const IMPORT_REWRITES: Array<{ test: RegExp; to: (match: RegExpMatchArray) => string }> = [
88
88
  { test: /^\.\.\/shared\/(.+)$/, to: (m) => `soma-ui/shared/${m[1]}` },
89
89
  { test: /^\.\.\/i18n$/, to: () => 'soma-ui' },
90
- { test: /^\.\.\/adaptive\/(.+)$/, to: (m) => `soma-ui/adaptive/${m[1]}` },
91
- { test: /^\.\.\/catalog\/(.+)$/, to: (m) => `soma-ui/catalog/${m[1]}` },
90
+ // 不能把 `adaptive/*` / `catalog/*` 一概映成路径通配:eject 产物会由第三方
91
+ // Host 安装,只有 package exports 中明确承诺的入口才能存活。未知内部模块在
92
+ // 这里 fail-closed,而不是为了当前本仓链接方便而悄悄变成公共 API。
93
+ { test: /^\.\.\/adaptive\/(?:density-arbiter|layout-primitives)$/, to: () => 'soma-ui/adaptive' },
94
+ { test: /^\.\.\/adaptive\/field-components$/, to: () => 'soma-ui/adaptive/field-components' },
95
+ { test: /^\.\.\/adaptive\/shape-infer$/, to: () => 'soma-ui/adaptive/shape-infer' },
96
+ { test: /^\.\.\/adaptive\/shorthand-columns$/, to: () => 'soma-ui/adaptive/shorthand-columns' },
97
+ { test: /^\.\.\/catalog\/layout-vocabulary$/, to: () => 'soma-ui/catalog/layout-vocabulary' },
92
98
  { test: /^\.\.\/(styles|style-base|theme-recipes|style-presets)$/, to: () => 'soma-ui' },
93
99
  ];
94
100
 
@@ -20,13 +20,19 @@ import {
20
20
  type SomaLayoutNodeConstraint,
21
21
  type SomaLayoutPlan,
22
22
  type SomaLayoutSolution,
23
+ type SomaPriority,
23
24
  } from '../../shared/types/layout-constraint.types';
25
+ import { CONTAINER_STEPS } from '../../shared/container-breakpoints';
24
26
 
25
- /** 容器断点八档(rem)。与 soma-ui 的 `responsive-constants` 同源同值 */
26
- export const CONTAINER_STEPS: ReadonlyArray<readonly [string, number]> = [
27
- ['cqXs', 20], ['cqSm', 24], ['cqMd', 28], ['cqLg', 36],
28
- ['cqXl', 40], ['cq2xl', 48], ['cq3xl', 56], ['cq4xl', 64],
29
- ];
27
+ /**
28
+ * 容器断点八档(rem)。**真源在 `core/shared/container-breakpoints.ts`**——
29
+ * 此处只重导出,保住既有 import 路径不变。
30
+ *
31
+ * 此前这里是一份字面量,与 soma-ui 的 `responsive-constants` 靠注释
32
+ * "同源同值"手工对齐(还有第三份在视觉矩阵脚本里)。三份都对不上时零报错,
33
+ * 表现是求解出的 CSS 与组件 CSS 在不同宽度上折断。
34
+ */
35
+ export { CONTAINER_STEPS } from '../../shared/container-breakpoints';
30
36
 
31
37
  export class SomaLayoutConstraintError extends Error {
32
38
  /** 打架的两条声明(CL-1 要求错误信息含冲突约束对) */
@@ -39,6 +45,16 @@ export class SomaLayoutConstraintError extends Error {
39
45
  }
40
46
  }
41
47
 
48
+ /** 长度字面量的文法(数值 + rem / % / cqi)。`toRem` 与 `validateFillSurplus` 读的是同一条 */
49
+ const LENGTH_LITERAL = /^(-?\d+(?:\.\d+)?)(rem|%|cqi)$/;
50
+
51
+ /**
52
+ * 没写 priority 的子项按「中庸」(500)求解。
53
+ * 求解(`solveStep`)与富余退化校验(`validateFillSurplus`)必须是同一个数——
54
+ * 两处各写一个 500,改了一处,校验就会对着一个求解器并不这么算的优先级下结论。
55
+ */
56
+ const DEFAULT_PRIORITY: SomaPriority = 500;
57
+
42
58
  /**
43
59
  * 长度字面量 → rem。
44
60
  *
@@ -48,7 +64,7 @@ export class SomaLayoutConstraintError extends Error {
48
64
  */
49
65
  export function toRem(literal: string, containerRem: number, where: string): number {
50
66
  const text = String(literal).trim();
51
- const match = /^(-?\d+(?:\.\d+)?)(rem|%|cqi)$/.exec(text);
67
+ const match = LENGTH_LITERAL.exec(text);
52
68
  if (!match) {
53
69
  throw new SomaLayoutConstraintError(
54
70
  `${where} 的长度 "${text}" 非法——只认 rem / % / cqi(单位必填,不猜)`,
@@ -62,7 +78,12 @@ export function toRem(literal: string, containerRem: number, where: string): num
62
78
  return match[2] === 'rem' ? value : (value / 100) * containerRem;
63
79
  }
64
80
 
65
- /** 声明校验(CL-3):优先级四档、min ≤ max、fixed 必须给 width */
81
+ /**
82
+ * 声明校验(CL-3):优先级四档、min ≤ max、fixed 必须给 width。
83
+ *
84
+ * OOUI 空间层逐字复用本函数。富余分配的退化配置(`validateFillSurplus`)**不在这里**——
85
+ * 那条只挂在手写 `layout:` 的入口上,原因见它的注释。
86
+ */
66
87
  export function validateConstraint(constraint: SomaLayoutConstraint, path: string): void {
67
88
  const names = Object.keys(constraint.nodes ?? {});
68
89
  if (names.length === 0) {
@@ -107,6 +128,73 @@ export function validateConstraint(constraint: SomaLayoutConstraint, path: strin
107
128
  }
108
129
  }
109
130
 
131
+ interface FillItem {
132
+ name: string;
133
+ node: SomaLayoutNodeConstraint;
134
+ priority: SomaPriority;
135
+ }
136
+
137
+ /**
138
+ * 富余分配的退化配置:一个 fill 项在每一档都分不到宽度。手写 `layout:` 的入口(`solveLayout`)调用。
139
+ *
140
+ * 成因在 `solveStep` 的步 1 与步 3:fill 从 min 起步(没写就是 0);富余按优先级从低到高分给
141
+ * fill 项、每项受自己的 max 限制。没写 max 的 fill 项余量是 ∞,它所在的那一档优先级就把富余
142
+ * **全部**拿走(余量为 ∞ 时分出去的就是全部剩余),更高优先级的 fill 项一点也分不到。
143
+ *
144
+ * 判定:同一组里,存在 fill 项 A 满足 max 缺省(= ∞),同时存在另一个 fill 项 B 满足
145
+ * `B.priority > A.priority`(没写按 500)且 B 没写 min(或 min 为 0)。此时 B 在每一个
146
+ * 不换行的档上都停在 0rem;产物里它是 `flex: 1 1 0rem`,容器宽恰好落在某一档上时没有宽度,
147
+ * 落在两档之间时又和 A 平分那段档间余量(两者 flex-grow 都是 1),看上去时有时无。
148
+ * 这种配置没有一种说得通的读法。
149
+ *
150
+ * 点名的 A 取「max 缺省的 fill 项里优先级最低的那个」(拿走富余的正是它所在的那一档;
151
+ * 同优先级取书写序第一个),B 取书写序里第一个符合条件的。min 写法非法时不算 0——
152
+ * 让 `toRem` 在求解时报它自己那条更准的错,这里不抢。
153
+ *
154
+ * **为什么不放进 `validateConstraint`**:OOUI 空间层(`build/ooui/spatial/solve.ts`)逐字复用
155
+ * `validateConstraint`,而页面区域预算(`build/ooui/page/budget.ts`)会给没写 layout 的区域
156
+ * 补缺省 `{ sizing: 'fill' }`——正好是一个 max = ∞、priority 500 的 A。作者只给另一个区域写
157
+ * `layout: { priority: 750 }` 就凑成这一对;这里的错误翻译到空间层是 `OOUI-SPA-UNSATISFIABLE`,
158
+ * 修法里「给低优先级项写 max」指向的是一个作者根本没写过 layout 的区域。
159
+ * 所以那一侧另拦:页面预算(`build/ooui/page/budget.ts` 的 `assertNoStarvedRegion`)调用本函数取
160
+ * 冲突对,再翻译成它自己的 `OOUI-SPA-BUDGET-STARVED`,修法只指向作者写过 layout 的区域——判据仍只有这一份。
161
+ */
162
+ export function validateFillSurplus(constraint: SomaLayoutConstraint, path: string): void {
163
+ const nodes: Record<string, SomaLayoutNodeConstraint> = constraint.nodes ?? {};
164
+ const fills: FillItem[] = Object.entries(nodes)
165
+ .filter(([, node]) => node.sizing === 'fill')
166
+ .map(([name, node]) => ({ name, node, priority: node.priority ?? DEFAULT_PRIORITY }));
167
+ const unbounded = fills.filter((item) => item.node.max === undefined);
168
+ if (unbounded.length === 0) return;
169
+ const absorber = unbounded.reduce((lowest, item) => (item.priority < lowest.priority ? item : lowest));
170
+ const starved = fills.find((item) => item.priority > absorber.priority && floorIsZero(item.node.min));
171
+ if (starved === undefined) return;
172
+
173
+ const priorityOf = (item: FillItem): string =>
174
+ `priority ${item.priority}${item.node.priority === undefined ? '(缺省)' : ''}`;
175
+ const high = `${path}.${starved.name}`;
176
+ const low = `${path}.${absorber.name}`;
177
+ throw new SomaLayoutConstraintError(
178
+ `${high} 在不换行的每一档上都分不到宽度:它是 fill、${priorityOf(starved)},`
179
+ + `${starved.node.min === undefined ? '没写 min' : `min 为 ${starved.node.min}`};`
180
+ + `同组的 ${low} 也是 fill、${priorityOf(absorber)},没写 max。`
181
+ + '富余按优先级从低到高分给 fill 项,没有 max 的那一项会把富余全部拿走,'
182
+ + `于是 "${starved.name}" 在这些档上都停在 0rem——运行时它的 flex-basis 是 0,`
183
+ + '只在档与档之间偶尔分到一点宽度,看上去时有时无。'
184
+ + `三种修法任选其一:给 "${starved.name}" 写 min(它至少要多宽);`
185
+ + `或给 "${absorber.name}" 写 max(它最多拿多宽);`
186
+ + '或把两者的 priority 调成一致(同一优先级的 fill 项一起分富余)。',
187
+ [high, low],
188
+ );
189
+ }
190
+
191
+ /** min 缺省,或数值为 0(任何单位的 0 在任何一档都是 0)。写法非法的不算——交给 `toRem` 报 */
192
+ function floorIsZero(min: string | undefined): boolean {
193
+ if (min === undefined) return true;
194
+ const match = LENGTH_LITERAL.exec(String(min).trim());
195
+ return match !== null && Number(match[1]) === 0;
196
+ }
197
+
110
198
  /**
111
199
  * 单档求解。
112
200
  *
@@ -154,7 +242,7 @@ export function solveStep(
154
242
  node,
155
243
  min,
156
244
  max,
157
- priority: node.priority ?? 500,
245
+ priority: node.priority ?? DEFAULT_PRIORITY,
158
246
  width: Math.min(Math.max(base, min), max),
159
247
  };
160
248
  });
@@ -195,6 +283,8 @@ export function solveStep(
195
283
  }
196
284
 
197
285
  // ── 步 3:分配富余(低优先级先拿) ────────────────────────────────
286
+ // 没写 max 的 fill 项余量是 ∞,它这一档会拿走全部富余;更高优先级、又没写 min 的 fill 项
287
+ // 因此每档都是 0rem——手写 `layout:` 在声明期就拦这种配置(`validateFillSurplus`)
198
288
  if (!wrapped && total < available) {
199
289
  for (const priority of SOMA_LAYOUT_PRIORITIES) {
200
290
  const rest = available - total;
@@ -204,14 +294,34 @@ export function solveStep(
204
294
  );
205
295
  if (group.length === 0) continue;
206
296
  const room = group.reduce((sum, item) => sum + (item.max - item.width), 0);
207
- const give = Math.min(rest, room);
208
- for (const item of group) {
209
- const share = room === Number.POSITIVE_INFINITY
210
- ? give / group.length
211
- : ((item.max - item.width) / room) * give;
212
- item.width += share;
297
+ if (room !== Number.POSITIVE_INFINITY) {
298
+ // 全组都有上限:按各自余量成比例分,give ≤ room,任何一项都不会越过自己的 max
299
+ const give = Math.min(rest, room);
300
+ for (const item of group) item.width += ((item.max - item.width) / room) * give;
301
+ total += give;
302
+ continue;
213
303
  }
214
- total += give;
304
+ // 组里至少一项没写 max(余量 ∞):注水式平分。每轮把剩余平分给还没封顶的项,
305
+ // 分到超过自己 max 的项在 max 处封顶、退出本组,多出来的留给下一轮继续平分——
306
+ // 旧写法整组一次平分,写了有限 max 的项会被分到超过自己的 max,解出来的数就违反了约束,
307
+ // 只是运行期的 max-inline-size 把它夹住才看不出来
308
+ let open = group;
309
+ let left = rest;
310
+ while (open.length > 0 && left > 1e-9) {
311
+ const share = left / open.length;
312
+ const capped = open.filter((item) => item.width + share > item.max);
313
+ if (capped.length === 0) {
314
+ for (const item of open) item.width += share;
315
+ left = 0;
316
+ break;
317
+ }
318
+ for (const item of capped) {
319
+ left -= item.max - item.width;
320
+ item.width = item.max;
321
+ }
322
+ open = open.filter((item) => !capped.includes(item));
323
+ }
324
+ total += rest - left;
215
325
  }
216
326
  }
217
327
 
@@ -220,9 +330,14 @@ export function solveStep(
220
330
  return { breakpoint, containerRem, widths, wrapped };
221
331
  }
222
332
 
223
- /** 八档各解一次(CL-1) */
333
+ /**
334
+ * 八档各解一次(CL-1)。这是手写 `layout:` 段的求解入口(`page-layout.ts` 调用):
335
+ * 声明校验之外再核一条富余分配的退化配置(`validateFillSurplus`)。
336
+ * OOUI 空间层不经这里(它直接用 `validateConstraint` + `solveStep`)。
337
+ */
224
338
  export function solveLayout(constraint: SomaLayoutConstraint, path: string): SomaLayoutPlan {
225
339
  validateConstraint(constraint, path);
340
+ validateFillSurplus(constraint, path);
226
341
  const solutions = CONTAINER_STEPS.map(
227
342
  ([name, rem]) => solveStep(constraint, rem, name, path),
228
343
  );
@@ -59,20 +59,24 @@ export function emitLayoutCss(plan: SomaLayoutPlan, options: EmitOptions): strin
59
59
  align !== undefined && ALIGN_CROSS[align] !== undefined
60
60
  ? ` align-items: ${ALIGN_CROSS[align]};`
61
61
  : null,
62
+ // 换行开关写死在组根上,**不按档切换**。组根自己就是查询容器,而元素的容器查询
63
+ // 只会命中它的**祖先**容器、查不到它自己:把 `${root} { flex-wrap: … }` 写进
64
+ // `@container (min-width: N)` 里,那条规则量的是外面某个祖先的宽度(没有祖先容器时
65
+ // 永远不生效),与子项查询的尺子不是同一把——组根与子项会在不同宽度上切档。
66
+ // 写死之后行为等价:非换行档求解器保证子项放得下,开着 wrap 也不会真的折行;
67
+ // 换行档的子项是 `flex: 1 1 100%`,每项照样独占一行。
68
+ ` flex-wrap: ${wrap === 'below' ? 'wrap' : 'nowrap'};`,
62
69
  wrap === 'reel' ? ' overflow-inline: auto;' : null,
63
70
  '}',
64
71
  ].filter((line) => line !== null).join('\n');
65
72
 
66
73
  const ruleFor = (index: number): string => {
67
74
  const solution = plan.solutions[index];
75
+ const wrappedBelow = solution.wrapped && wrap === 'below';
68
76
  const lines: string[] = [];
69
- // 换行档:整组切竖排。用 flex-wrap 而不是改 flex-direction,
70
- // 是为了让 gap 与对齐在两种形态下是同一套值(改方向会把主轴交叉轴对调)
71
- if (solution.wrapped && wrap === 'below') {
72
- lines.push(`${root} { flex-wrap: wrap; }`);
73
- } else if (wrap !== 'reel') {
74
- lines.push(`${root} { flex-wrap: nowrap; }`);
75
- }
77
+ // 每档只写**子项**:子项的容器查询命中的是组根,尺子对得上。组根自己的换行开关
78
+ // 在 containerRule 里写死(原因见上)。换行档靠子项 `flex: 1 1 100%` 整组切竖排——
79
+ // 用 flex-wrap 而不是改 flex-direction,gap 与对齐在两种形态下才是同一套值
76
80
  for (const [name, width] of Object.entries(solution.widths)) {
77
81
  const node = plan.constraint.nodes[name];
78
82
  const selector = selectorOf(name);
@@ -84,12 +88,14 @@ export function emitLayoutCss(plan: SomaLayoutPlan, options: EmitOptions): strin
84
88
  const basis = `${width}rem`;
85
89
  const grow = node.sizing === 'fill' ? '1 1' : '0 0';
86
90
  const bounds = [
87
- node.min === undefined ? null : `min-inline-size: ${node.min};`,
91
+ // 竖排已退出单行宽度约束;min 比容器还大时应随容器收窄,否则 100% 的
92
+ // flex-basis 仍会被原 min 撑破。宽档与横向卷轴继续保留作者声明的下界。
93
+ node.min === undefined ? null : `min-inline-size: ${wrappedBelow ? `min(100%, ${node.min})` : node.min};`,
88
94
  node.max === undefined ? null : `max-inline-size: ${node.max};`,
89
95
  ].filter((part) => part !== null).join(' ');
90
96
  // 换行档上宽度全给 100%:竖排之后每项独占一行,留着解出来的 rem 值
91
97
  // 会让第二行的项莫名其妙只占半行宽
92
- const declaration = solution.wrapped && wrap === 'below'
98
+ const declaration = wrappedBelow
93
99
  ? `flex: 1 1 100%;`
94
100
  : `flex: ${grow} ${basis};`;
95
101
  lines.push(`${selector} { ${declaration}${bounds === '' ? '' : ` ${bounds}`} }`);
@@ -97,7 +103,20 @@ export function emitLayoutCss(plan: SomaLayoutPlan, options: EmitOptions): strin
97
103
  return lines.join('\n');
98
104
  };
99
105
 
100
- const blocks: string[] = [containerRule, ruleFor(0)];
106
+ // 交叉轴是缺省的 stretch 时,并排的宿主已被拉到行高,但宿主里的组件根节点仍按内容高——
107
+ // 两张卡并排一高一矮,矮的下面空出一截(2026-09-14 实测:生成页授权面板 116px 对聊天台 352px)。
108
+ // 让非 hug 子项的宿主变成纵向 flex、组件根节点吃满剩余高度,并排表面即等高。
109
+ // hug 子项(按钮、徽标)不拉伸:把一枚按钮拉成整行高是错的。显式声明了交叉轴对齐时尊重作者。
110
+ const stretchCross = align === undefined || ALIGN_CROSS[align] === undefined;
111
+ const fillTargets = stretchCross
112
+ ? Object.entries(plan.constraint.nodes).filter(([, node]) => node.sizing !== 'hug').map(([name]) => selectorOf(name))
113
+ : [];
114
+ const fillRule = fillTargets.length === 0
115
+ ? null
116
+ : `${fillTargets.join(',\n')} { display: flex; flex-direction: column; }\n`
117
+ + `${fillTargets.map((selector) => `${selector} > *`).join(',\n')} { flex: 1 1 auto; }`;
118
+
119
+ const blocks: string[] = [containerRule, ...(fillRule === null ? [] : [fillRule]), ruleFor(0)];
101
120
  for (let index = 1; index < plan.solutions.length; index += 1) {
102
121
  const [, rem] = CONTAINER_STEPS[index];
103
122
  const body = ruleFor(index);