@kensnzk/koyu 0.5.0 → 0.11.0

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 (128) hide show
  1. package/README.ja.md +43 -11
  2. package/README.md +51 -11
  3. package/dist/check.d.ts +34 -0
  4. package/dist/check.d.ts.map +1 -0
  5. package/dist/check.js +500 -0
  6. package/dist/check.js.map +1 -0
  7. package/dist/cli.d.ts +3 -0
  8. package/dist/cli.d.ts.map +1 -0
  9. package/dist/cli.js +319 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/diff.d.ts +90 -0
  12. package/dist/diff.d.ts.map +1 -0
  13. package/dist/diff.js +522 -0
  14. package/dist/diff.js.map +1 -0
  15. package/dist/graph.d.ts +81 -0
  16. package/dist/graph.d.ts.map +1 -0
  17. package/dist/graph.js +328 -0
  18. package/dist/graph.js.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +10 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/light.d.ts +15 -0
  24. package/dist/light.d.ts.map +1 -0
  25. package/dist/light.js +55 -0
  26. package/dist/light.js.map +1 -0
  27. package/dist/mcp.d.ts +3 -0
  28. package/dist/mcp.d.ts.map +1 -0
  29. package/dist/mcp.js +354 -0
  30. package/dist/mcp.js.map +1 -0
  31. package/dist/model.d.ts +264 -0
  32. package/dist/model.d.ts.map +1 -0
  33. package/dist/model.js +361 -0
  34. package/dist/model.js.map +1 -0
  35. package/dist/parse-file.d.ts +9 -0
  36. package/dist/parse-file.d.ts.map +1 -0
  37. package/dist/parse-file.js +22 -0
  38. package/dist/parse-file.js.map +1 -0
  39. package/dist/parse.d.ts +16 -0
  40. package/dist/parse.d.ts.map +1 -0
  41. package/dist/parse.js +922 -0
  42. package/dist/parse.js.map +1 -0
  43. package/dist/plan.d.ts +8 -0
  44. package/dist/plan.d.ts.map +1 -0
  45. package/dist/plan.js +267 -0
  46. package/dist/plan.js.map +1 -0
  47. package/dist/site.d.ts +24 -0
  48. package/dist/site.d.ts.map +1 -0
  49. package/dist/site.js +53 -0
  50. package/dist/site.js.map +1 -0
  51. package/examples/comparison/README.md +16 -1
  52. package/examples/comparison/gen-ifc4.ts +1 -1
  53. package/examples/comparison/gen-ifcx.ts +2 -2
  54. package/examples/comparison/two-rooms.ifcx +1 -1
  55. package/examples/house/L1.muro +16 -0
  56. package/examples/house/L2.muro +19 -0
  57. package/examples/house/assets.muro +9 -0
  58. package/examples/house/main.muro +27 -0
  59. package/examples/house/site.muro +31 -0
  60. package/examples/house.muro +1 -1
  61. package/examples/mansion.muro +1 -1
  62. package/examples/office.muro +1 -1
  63. package/examples/steps/01-one-room.muro +4 -0
  64. package/examples/steps/02-two-rooms.muro +5 -0
  65. package/examples/steps/03-door.muro +9 -0
  66. package/examples/steps/04-exterior.muro +15 -0
  67. package/examples/steps/05-two-storeys.muro +27 -0
  68. package/examples/steps/06-finished.muro +30 -0
  69. package/examples/steps/README.md +25 -0
  70. package/examples/tower/L1.muro +80 -0
  71. package/examples/tower/L11.muro +47 -0
  72. package/examples/tower/L2.muro +69 -0
  73. package/examples/tower/L3.muro +28 -0
  74. package/examples/tower/assets.muro +13 -0
  75. package/examples/tower/main.muro +37 -0
  76. package/examples/tower/site-geometry.muro +7 -0
  77. package/examples/tower/site.muro +33 -0
  78. package/examples/tower/typical.muro +124 -0
  79. package/examples/two-rooms.canonical.json +5 -2
  80. package/examples/two-rooms.muro +6 -5
  81. package/package.json +28 -2
  82. package/src/check.ts +352 -63
  83. package/src/cli.ts +76 -11
  84. package/src/diff.ts +608 -0
  85. package/src/graph.ts +90 -10
  86. package/src/index.ts +14 -2
  87. package/src/mcp.ts +390 -0
  88. package/src/model.ts +260 -55
  89. package/src/parse-file.ts +28 -0
  90. package/src/parse.ts +562 -44
  91. package/src/plan.ts +33 -4
  92. package/src/site.ts +13 -5
  93. package/.github/workflows/ci.yml +0 -16
  94. package/.zenodo.json +0 -20
  95. package/CITATION.cff +0 -26
  96. package/docs/decisions/0001-notation-v0.md +0 -29
  97. package/docs/decisions/0002-height-and-offsets.md +0 -27
  98. package/docs/decisions/0003-uncounted-divisions.md +0 -36
  99. package/docs/decisions/0004-typical-floors.md +0 -21
  100. package/docs/decisions/0005-zones-and-unions.md +0 -23
  101. package/docs/decisions/0006-voids-and-light.md +0 -31
  102. package/docs/decisions/0007-semi-outdoor-air.md +0 -27
  103. package/docs/decisions/0008-vocabulary-and-level-attr.md +0 -23
  104. package/docs/decisions/0009-site-and-exterior.md +0 -29
  105. package/docs/ifc-coverage.md +0 -82
  106. package/docs/ifcx-notes.md +0 -25
  107. package/docs/img/house-L1.svg +0 -80
  108. package/docs/img/house-L2.svg +0 -53
  109. package/docs/img/mansion-L1.svg +0 -113
  110. package/docs/img/mansion-L10.svg +0 -111
  111. package/docs/img/mansion-L5.svg +0 -160
  112. package/docs/img/office-L1.svg +0 -116
  113. package/docs/img/office-L2.svg +0 -109
  114. package/docs/img/two-rooms.svg +0 -39
  115. package/docs/log/2026-07-22.md +0 -13
  116. package/docs/log/2026-07-23.md +0 -51
  117. package/docs/roadmap.md +0 -55
  118. package/docs/writing-architecture.md +0 -119
  119. package/spec/notation-v0.md +0 -164
  120. package/spec/vocabulary.md +0 -62
  121. package/test/core.test.ts +0 -171
  122. package/test/design2.test.ts +0 -97
  123. package/test/house.test.ts +0 -74
  124. package/test/mansion.test.ts +0 -99
  125. package/test/office.test.ts +0 -71
  126. package/test/uncounted.test.ts +0 -120
  127. package/test/vertical.test.ts +0 -75
  128. package/tsconfig.json +0 -13
package/src/model.ts CHANGED
@@ -5,6 +5,11 @@
5
5
  export type AttrValue = string | number;
6
6
  export type Attrs = Record<string, AttrValue>;
7
7
 
8
+ /** このツールが受理する言語版 (ADR-0017)。旧版は意味保存の場合のみ受理される (checkが検査する) */
9
+ export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = ["0.1", "0.2", "0.3"];
10
+ /** 版宣言を省略したときの解釈 — 常に最新版の意味論 (省略はツール版を跨いで意味安定ではない) */
11
+ export const DEFAULT_LANGUAGE_VERSION = "0.3";
12
+
8
13
  /** 方位。edge指定は「最初に書いた空間」の矩形から見た辺。N=+Y, S=-Y, E=+X, W=-X */
9
14
  export type Edge = "N" | "E" | "S" | "W";
10
15
 
@@ -68,6 +73,8 @@ export interface Space {
68
73
  areas: Area[];
69
74
  attrs: Attrs;
70
75
  line: number;
76
+ /** 合成時の出所ファイル (コンフリクト報告用) */
77
+ file?: string;
71
78
  }
72
79
 
73
80
  /**
@@ -78,6 +85,21 @@ export interface Zone {
78
85
  path: string;
79
86
  attrs: Attrs;
80
87
  line: number;
88
+ /** 合成時の出所ファイル (コンフリクト報告用) */
89
+ file?: string;
90
+ }
91
+
92
+ /**
93
+ * 建具アセット — RevitのFamily、USDのReferenceにあたる型の宣言 (ADR-0010)。
94
+ * `asset SD1 door w:800 style:sliding` と宣言し、開口が `door SD1 ...` で参照する。
95
+ * インスタンス側の属性が上書きする。別ファイル (アセット集) に置いて import できる
96
+ */
97
+ export interface Asset {
98
+ name: string;
99
+ kind: "door" | "window";
100
+ attrs: Attrs;
101
+ line: number;
102
+ file?: string;
81
103
  }
82
104
 
83
105
  /**
@@ -93,12 +115,20 @@ export type BoundaryKind = "wall" | "open" | "stair" | "shaft" | "void";
93
115
 
94
116
  export interface Opening {
95
117
  kind: "door" | "window";
118
+ /** 参照した建具アセット名 (Reference — ADR-0010) */
119
+ ref?: string;
96
120
  /** 幅 mm */
97
121
  w: number;
98
122
  /** 高さ mm */
99
123
  h?: number;
100
- /** 区間上の位置 0..1 (既定 0.5) */
124
+ /** 区間上の位置 0..1 (既定 0.5)。比率指定はクランプされる */
101
125
  at: number;
126
+ /** 明示位置: 書かれた通り参照 (at:X2+450 など) */
127
+ atRef?: string;
128
+ /** 明示位置: 解決済みの絶対座標 mm。はみ出しはエラーになる (クランプしない) */
129
+ atAbs?: number;
130
+ /** 明示位置の軸 (水平線分はX系、垂直線分はY系でなければならない) */
131
+ atAxis?: "X" | "Y";
102
132
  /** 区間が複数あるとき (外部境界など) の辺の指定 */
103
133
  edge?: Edge;
104
134
  /** 開き勝手: 吊元の側 (水平線分ならW/E、垂直線分ならS/N)。既定は始端側 */
@@ -118,6 +148,10 @@ export interface Seg {
118
148
  w: number;
119
149
  /** 区間中心の位置 0..1 (既定 0.5) */
120
150
  at: number;
151
+ /** 明示位置 (開口と同じ流儀 — at:X2+450) */
152
+ atRef?: string;
153
+ atAbs?: number;
154
+ atAxis?: "X" | "Y";
121
155
  edge?: Edge;
122
156
  attrs: Attrs;
123
157
  line: number;
@@ -141,6 +175,15 @@ export interface Boundary {
141
175
  /** 数えない分節 (字下げのseg行) */
142
176
  segs: Seg[];
143
177
  line: number;
178
+ /** 合成時の出所レイヤー (ADR-0010) */
179
+ file?: string;
180
+ /** 既定境界 (ADR-0014) — 宣言されず、接触から導出された壁。正準JSONには出ない (書かれた構成のみ) */
181
+ derived?: boolean;
182
+ }
183
+
184
+ /** エラー・警告の位置表記 — 合成時はどのレイヤーのことかを言葉にする (ADR-0010) */
185
+ export function srcRef(line: number, file?: string): string {
186
+ return `${file ? `${file}:` : ""}${line}行目`;
144
187
  }
145
188
 
146
189
  export interface Model {
@@ -151,15 +194,138 @@ export interface Model {
151
194
  levels: Record<string, Level>;
152
195
  spaces: Map<string, Space>;
153
196
  zones: Map<string, Zone>;
197
+ assets: Map<string, Asset>;
154
198
  boundaries: Boundary[];
199
+ /** 敷地形状 — 所与のジオメトリ (ADR-0011)。パス→頂点列 (mm)。
200
+ * 唯一の自由頂点列 — 空間の領域はグリッド参照の矩形として書かれる */
201
+ polygons: Map<string, SitePolygon>;
202
+ /** 合成に参加したレイヤー (ローダーのキー、合成順 — entryが先頭)。単一ソースのparseでは空 */
203
+ layers: string[];
204
+ /** koyu版が明示宣言されたか (base層でのみ・一度だけ — ADR-0017の合成規則の管理用) */
205
+ versionDeclared?: boolean;
206
+ }
207
+
208
+ /** 平面上の点 (mm) */
209
+ export interface Pt {
210
+ x: number;
211
+ y: number;
212
+ }
213
+
214
+ /** 敷地形状 (ADR-0011) — 測量に由来する所与の多角形。建物の形は生成物のままで、
215
+ * これはゾーン (site:1) に付く入力データ。隔離レイヤー (別ファイル+import) 推奨 */
216
+ export interface SitePolygon {
217
+ path: string;
218
+ points: Pt[];
219
+ line: number;
220
+ file?: string;
221
+ }
222
+
223
+ /** 多角形の面積 ㎡ (シューレース公式)。頂点は順不同 (時計/反時計どちらでも) */
224
+ export function polygonAreaM2(points: Pt[]): number {
225
+ let sum = 0;
226
+ for (let i = 0; i < points.length; i++) {
227
+ const a = points[i]!;
228
+ const b = points[(i + 1) % points.length]!;
229
+ sum += a.x * b.y - b.x * a.y;
230
+ }
231
+ return Math.abs(sum) / 2 / 1e6;
232
+ }
233
+
234
+ /** 線分同士が内部で交差するか (端点・境界上の接触は交差としない、許容誤差eps mm)。交点を返す */
235
+ function properCrossing(a1: Pt, a2: Pt, b1: Pt, b2: Pt, eps = 1): Pt | undefined {
236
+ const d = (a2.x - a1.x) * (b2.y - b1.y) - (a2.y - a1.y) * (b2.x - b1.x);
237
+ if (d === 0) return undefined; // 平行・共線 (共線の重なりは接触扱い — 境界上は内側)
238
+ const t = ((b1.x - a1.x) * (b2.y - b1.y) - (b1.y - a1.y) * (b2.x - b1.x)) / d;
239
+ const u = ((b1.x - a1.x) * (a2.y - a1.y) - (b1.y - a1.y) * (a2.x - a1.x)) / d;
240
+ const la = Math.hypot(a2.x - a1.x, a2.y - a1.y);
241
+ const lb = Math.hypot(b2.x - b1.x, b2.y - b1.y);
242
+ if (la === 0 || lb === 0) return undefined;
243
+ const ea = eps / la;
244
+ const eb = eps / lb;
245
+ if (t <= ea || t >= 1 - ea || u <= eb || u >= 1 - eb) return undefined; // 端点付近は接触
246
+ return { x: a1.x + t * (a2.x - a1.x), y: a1.y + t * (a2.y - a1.y) };
247
+ }
248
+
249
+ /** 多角形の自己交差点 (隣接しない辺同士の内部交差)。なければundefined */
250
+ export function polygonSelfIntersection(poly: Pt[], eps = 1): Pt | undefined {
251
+ const n = poly.length;
252
+ for (let i = 0; i < n; i++) {
253
+ for (let j = i + 2; j < n; j++) {
254
+ if (i === 0 && j === n - 1) continue; // 先頭と末尾の辺は隣接
255
+ const x = properCrossing(poly[i]!, poly[(i + 1) % n]!, poly[j]!, poly[(j + 1) % n]!, eps);
256
+ if (x) return x;
257
+ }
258
+ }
259
+ return undefined;
260
+ }
261
+
262
+ /**
263
+ * 矩形が多角形からはみ出す点 (完全に内側ならundefined)。凹多角形にも正しい:
264
+ * 四隅の内包に加え、多角形の頂点の矩形内への入り込みと、辺同士の内部交差を検査する
265
+ */
266
+ export function rectEscapesPolygon(r: Rect, poly: Pt[], eps = 1): Pt | undefined {
267
+ const corners: Pt[] = [
268
+ { x: r.x1, y: r.y1 },
269
+ { x: r.x2, y: r.y1 },
270
+ { x: r.x2, y: r.y2 },
271
+ { x: r.x1, y: r.y2 },
272
+ ];
273
+ for (const c of corners) if (!pointInPolygon(c, poly, eps)) return c;
274
+ for (const p of poly) {
275
+ if (p.x > r.x1 + eps && p.x < r.x2 - eps && p.y > r.y1 + eps && p.y < r.y2 - eps) return p;
276
+ }
277
+ const edges: Array<[Pt, Pt]> = [
278
+ [corners[0]!, corners[1]!],
279
+ [corners[1]!, corners[2]!],
280
+ [corners[2]!, corners[3]!],
281
+ [corners[3]!, corners[0]!],
282
+ ];
283
+ for (let i = 0; i < poly.length; i++) {
284
+ const a = poly[i]!;
285
+ const b = poly[(i + 1) % poly.length]!;
286
+ for (const [p, q] of edges) {
287
+ const x = properCrossing(a, b, p, q, eps);
288
+ if (x) return x;
289
+ }
290
+ }
291
+ return undefined;
292
+ }
293
+
294
+ /** 点が多角形の内側にあるか (境界上は内側扱い、許容誤差eps mm) */
295
+ export function pointInPolygon(p: Pt, poly: Pt[], eps = 1): boolean {
296
+ // 境界上の判定 (線分との距離 ≤ eps)
297
+ for (let i = 0; i < poly.length; i++) {
298
+ const a = poly[i]!;
299
+ const b = poly[(i + 1) % poly.length]!;
300
+ const dx = b.x - a.x;
301
+ const dy = b.y - a.y;
302
+ const len2 = dx * dx + dy * dy;
303
+ const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2));
304
+ const qx = a.x + t * dx;
305
+ const qy = a.y + t * dy;
306
+ if ((p.x - qx) ** 2 + (p.y - qy) ** 2 <= eps * eps) return true;
307
+ }
308
+ // レイキャスト
309
+ let inside = false;
310
+ for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
311
+ const a = poly[i]!;
312
+ const b = poly[j]!;
313
+ if (a.y > p.y !== b.y > p.y && p.x < ((b.x - a.x) * (p.y - a.y)) / (b.y - a.y) + a.x) {
314
+ inside = !inside;
315
+ }
316
+ }
317
+ return inside;
155
318
  }
156
319
 
157
320
  export class SourceError extends Error {
158
321
  constructor(
159
322
  public line: number,
160
- message: string,
323
+ /** 位置情報を除いた本文 (合成時のファイル付与に使う) */
324
+ public raw: string,
325
+ /** 出所ファイル (合成時) */
326
+ public file?: string,
161
327
  ) {
162
- super(`${line}行目: ${message}`);
328
+ super(`${file ? `${file}:` : ""}${line}行目: ${raw}`);
163
329
  this.name = "SourceError";
164
330
  }
165
331
  }
@@ -274,69 +440,98 @@ export function displayName(s: Space): string {
274
440
  return typeof n === "string" ? n : (s.path.split("/").pop() ?? s.path);
275
441
  }
276
442
 
443
+ /** 正準JSONの空間エントリ (書かれた表記・正準順)。semantic diff (ADR-0018) が比較基底として共有する */
444
+ export function canonicalSpaceEntry(s: Space): Record<string, unknown> {
445
+ return {
446
+ type: s.type,
447
+ // 明示の level: (パス先頭セグメントと異なる所属 — メゾネット等) は書かれた構成として保存する。
448
+ // これが無いとJSONだけでは所属レベル (垂直検査・集計・既定境界の前提) を復元できない
449
+ ...(s.level !== undefined && s.path.split("/")[1] !== s.level ? { level: s.level } : {}),
450
+ ...(s.grids.length === 1
451
+ ? { at: [s.grids[0]!.xa, s.grids[0]!.ya, s.grids[0]!.xb, s.grids[0]!.yb] }
452
+ : s.grids.length > 1
453
+ ? { at: sortBySerial(s.grids.map((g) => [g.xa, g.ya, g.xb, g.yb])) }
454
+ : {}),
455
+ ...(Object.keys(s.attrs).length ? { attrs: sortObj(s.attrs) } : {}),
456
+ ...(s.areas.length
457
+ ? {
458
+ areas: sortBySerial(
459
+ s.areas.map((a) => ({
460
+ at: [a.grid.xa, a.grid.ya, a.grid.xb, a.grid.yb],
461
+ ...(Object.keys(a.attrs).length ? { attrs: sortObj(a.attrs) } : {}),
462
+ })),
463
+ ),
464
+ }
465
+ : {}),
466
+ };
467
+ }
468
+
469
+ /** 正準JSONの開口エントリ (atRef??at 等の正準表記) — toCanonicalとsemantic diffが共有 */
470
+ export function canonicalOpeningEntry(o: Opening): Record<string, unknown> {
471
+ return {
472
+ kind: o.kind,
473
+ ...(o.ref ? { ref: o.ref } : {}),
474
+ w: o.w,
475
+ ...(o.h !== undefined ? { h: o.h } : {}),
476
+ at: o.atRef ?? o.at,
477
+ ...(o.edge ? { edge: o.edge } : {}),
478
+ ...(o.hinge ? { hinge: o.hinge } : {}),
479
+ ...(o.swing ? { swing: o.swing } : {}),
480
+ ...(Object.keys(o.attrs).length ? { attrs: sortObj(o.attrs) } : {}),
481
+ };
482
+ }
483
+
484
+ /** 正準JSONのsegエントリ — toCanonicalとsemantic diffが共有 */
485
+ export function canonicalSegEntry(g: Seg): Record<string, unknown> {
486
+ return {
487
+ w: g.w,
488
+ at: g.atRef ?? g.at,
489
+ ...(g.edge ? { edge: g.edge } : {}),
490
+ ...(Object.keys(g.attrs).length ? { attrs: sortObj(g.attrs) } : {}),
491
+ };
492
+ }
493
+
494
+ /** 正準JSONの境界エントリ。a/bの向きは書かれた表記 (aキー) として保存する — edge/swingはa側から読む */
495
+ export function canonicalBoundaryEntry(b: Boundary): Record<string, unknown> {
496
+ return {
497
+ between: [b.a, b.b].sort(),
498
+ a: b.a,
499
+ kind: b.kind,
500
+ ...(b.t !== undefined ? { t: b.t } : {}),
501
+ ...(b.air ? { air: true } : {}),
502
+ ...(b.edge ? { edge: b.edge } : {}),
503
+ ...(Object.keys(b.attrs).length ? { attrs: sortObj(b.attrs) } : {}),
504
+ ...(b.openings.length ? { openings: sortBySerial(b.openings.map(canonicalOpeningEntry)) } : {}),
505
+ ...(b.segs.length ? { segs: sortBySerial(b.segs.map(canonicalSegEntry)) } : {}),
506
+ };
507
+ }
508
+
277
509
  /** 正準JSON — 機械形式。差分とレイヤー合成の土台 (キーは安定順) */
278
510
  export function toCanonical(model: Model): string {
279
511
  const spaces: Record<string, unknown> = {};
280
512
  for (const p of [...model.spaces.keys()].sort()) {
281
- const s = model.spaces.get(p)!;
282
- spaces[p] = {
283
- type: s.type,
284
- ...(s.grids.length === 1
285
- ? { at: [s.grids[0]!.xa, s.grids[0]!.ya, s.grids[0]!.xb, s.grids[0]!.yb] }
286
- : s.grids.length > 1
287
- ? { at: s.grids.map((g) => [g.xa, g.ya, g.xb, g.yb]) }
288
- : {}),
289
- ...(Object.keys(s.attrs).length ? { attrs: sortObj(s.attrs) } : {}),
290
- ...(s.areas.length
291
- ? {
292
- areas: s.areas.map((a) => ({
293
- at: [a.grid.xa, a.grid.ya, a.grid.xb, a.grid.yb],
294
- ...(Object.keys(a.attrs).length ? { attrs: sortObj(a.attrs) } : {}),
295
- })),
296
- }
297
- : {}),
298
- };
513
+ spaces[p] = canonicalSpaceEntry(model.spaces.get(p)!);
299
514
  }
300
- const boundaries = [...model.boundaries]
301
- .map((b) => ({
302
- between: [b.a, b.b].sort(),
303
- kind: b.kind,
304
- ...(b.t !== undefined ? { t: b.t } : {}),
305
- ...(b.air ? { air: true } : {}),
306
- ...(b.edge ? { edge: b.edge } : {}),
307
- ...(Object.keys(b.attrs).length ? { attrs: sortObj(b.attrs) } : {}),
308
- ...(b.openings.length
309
- ? {
310
- openings: b.openings.map((o) => ({
311
- kind: o.kind,
312
- w: o.w,
313
- ...(o.h !== undefined ? { h: o.h } : {}),
314
- at: o.at,
315
- ...(o.edge ? { edge: o.edge } : {}),
316
- ...(o.hinge ? { hinge: o.hinge } : {}),
317
- ...(o.swing ? { swing: o.swing } : {}),
318
- ...(Object.keys(o.attrs).length ? { attrs: sortObj(o.attrs) } : {}),
319
- })),
320
- }
321
- : {}),
322
- ...(b.segs.length
323
- ? {
324
- segs: b.segs.map((g) => ({
325
- w: g.w,
326
- at: g.at,
327
- ...(g.edge ? { edge: g.edge } : {}),
328
- ...(Object.keys(g.attrs).length ? { attrs: sortObj(g.attrs) } : {}),
329
- })),
330
- }
331
- : {}),
332
- }))
333
- .sort((x, y) => (x.between.join() < y.between.join() ? -1 : 1));
515
+ // 境界: 宣言順は意味を持たないため、並びは内容の正準順 (betweenの辞書順、同一betweenは直列化順)。
516
+ // 既定境界 (derived — ADR-0014) は出さない: 正準JSONは書かれた構成のみで、意味は導出後のModelが持つ
517
+ const boundaries = sortBySerial(
518
+ [...model.boundaries].filter((b) => !b.derived).map(canonicalBoundaryEntry),
519
+ );
334
520
 
335
521
  const zones: Record<string, unknown> = {};
336
522
  for (const p of [...model.zones.keys()].sort()) {
337
523
  const z = model.zones.get(p)!;
338
524
  zones[p] = Object.keys(z.attrs).length ? { attrs: sortObj(z.attrs) } : {};
339
525
  }
526
+ const assets: Record<string, unknown> = {};
527
+ for (const n of [...model.assets.keys()].sort()) {
528
+ const a = model.assets.get(n)!;
529
+ assets[n] = { kind: a.kind, ...(Object.keys(a.attrs).length ? { attrs: sortObj(a.attrs) } : {}) };
530
+ }
531
+ const polygons: Record<string, number[][]> = {};
532
+ for (const p of [...model.polygons.keys()].sort()) {
533
+ polygons[p] = model.polygons.get(p)!.points.map((pt) => [pt.x, pt.y]);
534
+ }
340
535
 
341
536
  const doc = {
342
537
  koyu: model.version,
@@ -355,6 +550,8 @@ export function toCanonical(model: Model): string {
355
550
  ]),
356
551
  ),
357
552
  ),
553
+ ...(Object.keys(assets).length ? { assets } : {}),
554
+ ...(Object.keys(polygons).length ? { polygons } : {}),
358
555
  ...(Object.keys(zones).length ? { zones } : {}),
359
556
  spaces,
360
557
  boundaries,
@@ -365,3 +562,11 @@ export function toCanonical(model: Model): string {
365
562
  function sortObj<T>(o: Record<string, T>): Record<string, T> {
366
563
  return Object.fromEntries(Object.entries(o).sort(([a], [b]) => (a < b ? -1 : 1)));
367
564
  }
565
+
566
+ /** 宣言順に意味の無い集合を、直列化したJSONの辞書順に並べる — 正準順の土台 (diffも同じ順で比べる) */
567
+ export function sortBySerial<T>(items: T[]): T[] {
568
+ return items
569
+ .map((it) => [JSON.stringify(it), it] as const)
570
+ .sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0))
571
+ .map(([, it]) => it);
572
+ }
@@ -0,0 +1,28 @@
1
+ // ファイルシステムからの合成 — node専用の入口 (ADR-0010)。
2
+ // パーサ本体 (parse.ts) は純粋で、fsはこの薄い層だけが知る。
3
+ // ブラウザ (ugatsu等) は parseFiles (仮想ファイル群) を使う。
4
+
5
+ import { readFileSync } from "node:fs";
6
+ import { dirname, resolve } from "node:path";
7
+ import type { Model } from "./model.js";
8
+ import { parseWith } from "./parse.js";
9
+
10
+ /** ファイルから読む。import は書かれたファイルからの相対で解決される */
11
+ export function parseFile(filePath: string): Model {
12
+ return parseFileWith(filePath);
13
+ }
14
+
15
+ /**
16
+ * 差し替えつきのファイル合成 — 書き込み前の門番 (MCPのwrite_layer) が使う。
17
+ * overlay が文字列を返したパスは、ディスクの内容の代わりにそれが合成される
18
+ */
19
+ export function parseFileWith(
20
+ filePath: string,
21
+ overlay?: (absPath: string) => string | undefined,
22
+ ): Model {
23
+ return parseWith((from, ref) => {
24
+ const key = from === undefined ? resolve(ref) : resolve(dirname(from), ref);
25
+ const src = overlay?.(key) ?? readFileSync(key, "utf8");
26
+ return { key, src };
27
+ }, filePath);
28
+ }