@kensnzk/koyu 0.18.0 → 0.20.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 (74) hide show
  1. package/README.ja.md +3 -1
  2. package/README.md +4 -2
  3. package/dist/analysis/contracts.d.ts +11 -1
  4. package/dist/analysis/contracts.d.ts.map +1 -1
  5. package/dist/analysis/contracts.js +11 -1
  6. package/dist/analysis/contracts.js.map +1 -1
  7. package/dist/cli.js +37 -2
  8. package/dist/cli.js.map +1 -1
  9. package/dist/core/diagnose.d.ts +2 -0
  10. package/dist/core/diagnose.d.ts.map +1 -1
  11. package/dist/core/diagnose.js +12 -1
  12. package/dist/core/diagnose.js.map +1 -1
  13. package/dist/core/diff.d.ts.map +1 -1
  14. package/dist/core/diff.js +4 -1
  15. package/dist/core/diff.js.map +1 -1
  16. package/dist/core/model.d.ts +206 -8
  17. package/dist/core/model.d.ts.map +1 -1
  18. package/dist/core/model.js +214 -10
  19. package/dist/core/model.js.map +1 -1
  20. package/dist/core/parse.d.ts.map +1 -1
  21. package/dist/core/parse.js +121 -11
  22. package/dist/core/parse.js.map +1 -1
  23. package/dist/core/vocabulary.d.ts.map +1 -1
  24. package/dist/core/vocabulary.js +11 -0
  25. package/dist/core/vocabulary.js.map +1 -1
  26. package/dist/draw/plan.d.ts.map +1 -1
  27. package/dist/draw/plan.js +24 -0
  28. package/dist/draw/plan.js.map +1 -1
  29. package/dist/form.d.ts +1 -0
  30. package/dist/form.d.ts.map +1 -1
  31. package/dist/form.js +5 -0
  32. package/dist/form.js.map +1 -1
  33. package/dist/index.d.ts +1 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +4 -1
  36. package/dist/index.js.map +1 -1
  37. package/dist/mcp.js +27 -2
  38. package/dist/mcp.js.map +1 -1
  39. package/dist/model.d.ts +2 -2
  40. package/dist/model.d.ts.map +1 -1
  41. package/dist/model.js +1 -1
  42. package/dist/model.js.map +1 -1
  43. package/dist/validate/contracts.d.ts +6 -1
  44. package/dist/validate/contracts.d.ts.map +1 -1
  45. package/dist/validate/contracts.js +6 -1
  46. package/dist/validate/contracts.js.map +1 -1
  47. package/editors/vscode/package.json +1 -1
  48. package/editors/vscode/syntaxes/koyu.tmLanguage.json +56 -56
  49. package/examples/basement/main.muro +1 -1
  50. package/examples/complex/main.muro +1 -1
  51. package/examples/house/main.muro +1 -1
  52. package/examples/house.muro +1 -1
  53. package/examples/mansion.muro +1 -1
  54. package/examples/office.muro +1 -1
  55. package/examples/steps/06-finished.muro +1 -1
  56. package/examples/tower/main.muro +1 -1
  57. package/examples/tower/site-geometry.muro +9 -0
  58. package/examples/twin/main.muro +1 -1
  59. package/examples/two-rooms.canonical.json +2 -2
  60. package/examples/two-rooms.muro +1 -1
  61. package/package.json +16 -2
  62. package/src/analysis/contracts.ts +11 -1
  63. package/src/cli.ts +40 -2
  64. package/src/core/diagnose.ts +12 -1
  65. package/src/core/diff.ts +4 -1
  66. package/src/core/model.ts +273 -9
  67. package/src/core/parse.ts +156 -9
  68. package/src/core/vocabulary.ts +12 -0
  69. package/src/draw/plan.ts +31 -0
  70. package/src/form.ts +13 -0
  71. package/src/index.ts +5 -0
  72. package/src/mcp.ts +33 -2
  73. package/src/model.ts +8 -0
  74. package/src/validate/contracts.ts +6 -1
@@ -218,7 +218,17 @@ export interface AnalysisResult<T extends JsonValue = JsonValue> {
218
218
  readonly artifact: AnalysisArtifact<T>;
219
219
  }
220
220
 
221
- export const ANALYSIS_FORMAT = "koyu-analysis/1" as const;
221
+ /**
222
+ * The name of the shape an analysis returns. **It carries no version, deliberately.**
223
+ *
224
+ * A version promises that something is worth checking against — and nothing checks this. It
225
+ * is written by `assess` and read by no one; the face it sits on is the one the project
226
+ * declares unfrozen, so a `/1` on it would announce a stability nobody intends to keep.
227
+ *
228
+ * The counter-example is `koyu-context/1`, the input this protocol takes: that one is checked
229
+ * on arrival and refused on mismatch, which is what makes its version worth carrying.
230
+ */
231
+ export const ANALYSIS_FORMAT = "koyu-analysis" as const;
222
232
 
223
233
  export interface AnalysisReport<T extends JsonValue = JsonValue> {
224
234
  readonly schema: typeof ANALYSIS_FORMAT;
package/src/cli.ts CHANGED
@@ -35,7 +35,8 @@ import {
35
35
  type Model,
36
36
  srcRef,
37
37
  isOutside,
38
- isVoid
38
+ isVoid,
39
+ versionLine
39
40
  } from "./core/model.js";
40
41
  import { parseFile } from "./parse-file.js";
41
42
  import { svgPlan } from "./draw/plan.js";
@@ -190,6 +191,18 @@ function firstModelLine(evidence: AssessmentReport["findings"][number]["outcome"
190
191
 
191
192
  function main(argv: string[]): number {
192
193
  const [cmd, file, ...rest] = argv;
194
+ // Asked before the usage text, because it takes no file and a tool that cannot state its
195
+ // own version sends the reader to package.json to find out what they are running.
196
+ if (cmd === "--version" || cmd === "-v") {
197
+ // It takes no file, so being handed one is a calling mistake like any other. Printing the
198
+ // version and exiting 0 would let a wrapper that passes the wrong flag look like it worked.
199
+ if (file !== undefined) {
200
+ console.error(`--version takes no arguments: ${[file, ...rest].join(" ")}`);
201
+ return 2;
202
+ }
203
+ console.log(versionLine());
204
+ return 0;
205
+ }
193
206
  if (!cmd || !file) {
194
207
  console.log(
195
208
  "Usage: koyu <check|validate|layers|diff|plan|axo|doors|graph|stats|levels|runs|light|site|json> <file.muro> [args...]\n" +
@@ -240,7 +253,8 @@ function main(argv: string[]): number {
240
253
  // check --json は構文・合成エラー (SourceError) でも有効JSONを返す — SYN01 の1件に写す (ADR-0016)
241
254
  if (cmd === "check" && rest.includes("--json") && e instanceof SourceError) {
242
255
  const d: Diagnostic = {
243
- code: "SYN01",
256
+ // A parse failure that named its own condition keeps that code; the rest are syntax.
257
+ code: e.code ?? "SYN01",
244
258
  severity: "error",
245
259
  message: e.raw,
246
260
  ...(e.line ? { line: e.line } : {}),
@@ -484,6 +498,30 @@ function main(argv: string[]): number {
484
498
  case "site": {
485
499
  // 敷地の問い: 敷地面積・接道・建蔽率・容積率 (基本計画のボリューム検討の数字)
486
500
  const r = siteReport(model);
501
+ // 測地の枠 (ADR-0057) — 敷地の有無と無関係に、書かれていれば読み上げる。
502
+ // **方位は言葉にする。**磁北を書き写した値は範囲内の整った数で、文法では捕まらない。
503
+ // 人が一度声に出して読むことだけが、5〜9.5度の取り違えを捕まえる
504
+ if (model.origin) {
505
+ const o = model.origin;
506
+ const height =
507
+ o.elevation !== undefined
508
+ ? ` / elevation ${o.elevation} m of z=0 (vertical CRS ${o.vertical})`
509
+ : "";
510
+ console.log(`Frame: EPSG ${o.epsg} / easting ${o.easting} m / northing ${o.northing} m${height}`);
511
+ }
512
+ if (model.azimuth) {
513
+ const deg = model.azimuth.deg;
514
+ const trim = (n: number): string => String(Math.round(n * 1e4) / 1e4);
515
+ const words =
516
+ deg === 0
517
+ ? "due north"
518
+ : deg === 180
519
+ ? "due south"
520
+ : deg < 180
521
+ ? `${trim(deg)}° east of true north`
522
+ : `${trim(360 - deg)}° west of true north`;
523
+ console.log(`Bearing: +Y bears ${trim(deg)}° true — ${words}`);
524
+ }
487
525
  if (!r.siteZone && r.roads.length === 0) {
488
526
  console.log("There is no site (write site:1 on a zone and road:<width> on the road)");
489
527
  return 1;
@@ -103,6 +103,7 @@ export const DIAGNOSTIC_CODES = {
103
103
  SIT01: "error", // 敷地形状の重複頂点
104
104
  SIT02: "error", // 敷地形状の自己交差
105
105
  SIT04: "warning", // 対応するゾーンの無いpolygon
106
+ SIT06: "warning", // originがあってazimuthが無い — 位置は決まるが向きが決まらない (ADR-0057)
106
107
  UID01: "error", // 数字だけのuid (ADR-0015)
107
108
  UID02: "error", // 空白を含むuid
108
109
  UID03: "error", // uidの重複
@@ -125,6 +126,7 @@ export const DIAGNOSTIC_CODES = {
125
126
  VER03: "error", // koyu 0.4以前のファイルに0.5の語 (縦動線・線・柱・地下)
126
127
  VER04: "error", // koyu 0.5以前のファイルに1.0の語 (over・drop・集合編集 — ADR-0035/0038)
127
128
  VER05: "error", // koyu 1.0以前のファイルの型の位置に exterior / void (ADR-0051)
129
+ VER06: "error", // the file declares a language version newer than this build reads
128
130
  SYN01: "error", // 構文・合成エラー (SourceError の写し — check --json のみ)
129
131
  } as const satisfies Record<string, "error" | "warning">;
130
132
 
@@ -1166,9 +1168,18 @@ function checkLevelSufficiency(ctx: Ctx): void {
1166
1168
  }
1167
1169
  }
1168
1170
 
1169
- /** 敷地形状 — SIT01 / SIT02 / SIT04 (与件の健全性。建物との関係の判断は検証の面) */
1171
+ /** 敷地形状と測地の枠 — SIT06 / SIT01 / SIT02 / SIT04 (与件の健全性。建物との関係の判断は検証の面) */
1170
1172
  function checkSite(ctx: Ctx): void {
1171
1173
  const { model, emit, loc, withRect, levels, levelIndex } = ctx;
1174
+ // 測地の枠 (ADR-0057): 位置だけ書いて向きを書かないモデルは配置できない。回転を受け取らなかった
1175
+ // 消費者は 0 と仮定して最大360度間違える — 両方書かないより悪い静かな失敗なので警告する。
1176
+ // 逆は警告しない: azimuth 単独は完結した言明である (向きを知り、位置を知らない)
1177
+ if (model.origin && !model.azimuth) {
1178
+ emit("SIT06", "origin is written without azimuth, so the model has a position but no bearing", {
1179
+ line: model.origin.line,
1180
+ file: model.origin.file,
1181
+ });
1182
+ }
1172
1183
  // 敷地形状 (ADR-0011): 形の妥当性、対応ゾーンの存在、宣言面積との照合、建物のはみ出し検査。
1173
1184
  // はみ出しは四隅の内包に加え、多角形の頂点の入り込みと辺の交差を見る — 凹敷地でも正しい (ADR-0013)。
1174
1185
  // 地上の外部空間タイル (庭・通路) は近似なので検査しない — 面積の真は多角形が持つ
package/src/core/diff.ts CHANGED
@@ -686,7 +686,10 @@ export function renderDiff(d: ModelDiff): string[] {
686
686
  else if (g.kind === "added") out.push(`+ grid ${g.axis} ${g.name} ${g.to}`);
687
687
  else out.push(`− grid ${g.axis} ${g.name} ${g.from}`);
688
688
  }
689
- if (d.version) out.push(`± koyu ${d.version.from} → ${d.version.to}`);
689
+ // **The label names the language, not the implementation.** It also may not be the written
690
+ // declaration: from 1.2 the word is `muro`, so printing `koyu 1.2` would show a line the
691
+ // parser refuses. This is a label for a change, not a copy of either file's first line.
692
+ if (d.version) out.push(`± muro ${d.version.from} → ${d.version.to}`);
690
693
  if (d.name) out.push(`± name ${d.name.from ?? "—"} → ${d.name.to ?? "—"}`);
691
694
  for (const n of d.levels.added) out.push(`+ level ${n}`);
692
695
  for (const n of d.levels.removed) out.push(`− level ${n}`);
package/src/core/model.ts CHANGED
@@ -8,20 +8,205 @@ export type AttrValue = string | number;
8
8
  export type Attrs = Record<string, AttrValue>;
9
9
 
10
10
  /**
11
- * このツールが受理する言語版 (ADR-0017)。旧版は意味保存の場合のみ受理される (checkが検査する)。
12
- * **この並びが版の新旧の順である** — 版の比較はここの添字で行う (辞書順では 0.5 が 1.0 より後になる)
11
+ * The correspondence between the two version lines, and the only place it is recorded.
12
+ *
13
+ * muro is the language; koyu is the implementation that reads it. They are counted
14
+ * separately because they promise different things, and until this ledger existed nothing
15
+ * said which koyu implemented which muro — the published norm asserted the declaration in
16
+ * prose while no such declaration existed on any surface.
17
+ *
18
+ * - `muro` the language version, as written in a `.muro` file
19
+ * - `since` the koyu version that first read it. **A version that shipped** — `1.0.0-rc.1`
20
+ * carried muro 1.0 in the tree but never reached npm, so muro 1.0 arrived for
21
+ * anyone outside this repository at 0.16.0, and that is what this records
22
+ * - `until` the last koyu version that reads it, once a version is retired. Empty on every
23
+ * row: nothing has been retired. The field exists so that retiring is filling in
24
+ * a value rather than reshaping the ledger
25
+ *
26
+ * **The order of this array is the order of the versions, oldest first.** Comparison is by
27
+ * index, never by spelling — as strings, "0.5" sorts after "1.0".
28
+ *
29
+ * Cutting a language version is adding a row here. `test/release.test.ts` holds that: a
30
+ * release that moves the newest version must carry a row whose `since` is its own version.
31
+ */
32
+ export const MURO_SUPPORT: readonly { muro: string; since: string; until: string | null }[] = [
33
+ // The `koyu` keyword was read from the first commit, accepting whatever followed it.
34
+ { muro: "0.1", since: "0.0.1", until: null },
35
+ // 0.9.0 introduced the accepted-version list, and with it the first rejection.
36
+ { muro: "0.2", since: "0.9.0", until: null },
37
+ { muro: "0.3", since: "0.11.0", until: null },
38
+ { muro: "0.4", since: "0.11.0", until: null },
39
+ { muro: "0.5", since: "0.11.0", until: null },
40
+ // Not 1.0.0-rc.1. That version set muro 1.0 in the tree and was never published; the
41
+ // implementation version was returned to the 0.x line and shipped as 0.16.0 instead.
42
+ { muro: "1.0", since: "0.16.0", until: null },
43
+ { muro: "1.1", since: "0.17.0", until: null },
44
+ // The version line stops answering to the implementation's name: `muro 1.2`, not `koyu 1.2`.
45
+ { muro: "1.2", since: "0.20.0", until: null },
46
+ ];
47
+
48
+ /** The word a version line is written with from 1.2 on. */
49
+ export const MURO_KEYWORD = "muro";
50
+
51
+ /**
52
+ * The last version whose line is spelled `koyu`.
53
+ *
54
+ * **One spelling per version, never both.** Up to and including this version a file declares
55
+ * itself `koyu <v>`, and from the next one it declares itself `muro <v>`. Accepting both for
56
+ * the same version would give one declaration two spellings, and the canonical form's
57
+ * uniqueness — which the whole conformance suite rests on — would go with it.
58
+ *
59
+ * Files written before 1.2 keep the old spelling and keep meaning exactly what they meant.
60
+ * Nothing migrates, and nothing has to.
61
+ */
62
+ export const LAST_KOYU_SPELLED_VERSION = "1.1";
63
+
64
+ /**
65
+ * This implementation's own version — the one npm installs, held in step with `package.json`
66
+ * by `test/release.test.ts`.
67
+ *
68
+ * It sits beside `MURO_SUPPORT` because the two are one fact read from two directions: the
69
+ * `since` column is written in this vocabulary, and every surface that answers "which muro
70
+ * does this build speak" needs both halves at once.
71
+ */
72
+ export const KOYU_VERSION = "0.20.0";
73
+
74
+ /**
75
+ * Whether `a` names a later language version than `b`.
76
+ *
77
+ * **Not by index in the ledger** — the whole point is to answer for a version the ledger has
78
+ * never heard of, which is what a file from the future carries. Compared as numbers, because
79
+ * as strings `"0.5" > "1.0"`. A spelling that is not `major.minor` is not a version at all
80
+ * and is never "newer": it is unreadable, which is a different answer.
81
+ */
82
+ export function isNewerVersion(a: string, b: string): boolean {
83
+ const shape = /^(\d+)\.(\d+)$/;
84
+ const pa = shape.exec(a);
85
+ const pb = shape.exec(b);
86
+ if (!pa || !pb) return false;
87
+ const major = Number(pa[1]) - Number(pb[1]);
88
+ return major !== 0 ? major > 0 : Number(pa[2]) > Number(pb[2]);
89
+ }
90
+
91
+ /**
92
+ * What this build reads, what the newest version is, and how an undeclared file is read.
93
+ *
94
+ * The last two are separate facts and only coincide today. Once a newer version exists they
95
+ * differ permanently, and the line has to say both or it is telling half the truth to the
96
+ * person most likely to be surprised by it.
97
+ */
98
+ export function versionLine(): string {
99
+ const read = SUPPORTED_LANGUAGE_VERSIONS;
100
+ const range = `${read[0]}–${read[read.length - 1]}`;
101
+ return `koyu ${KOYU_VERSION} — reads muro ${range} (newest ${NEWEST_LANGUAGE_VERSION}; a file with no version line is read as ${DEFAULT_LANGUAGE_VERSION})`;
102
+ }
103
+
104
+ /**
105
+ * The koyu version that first read this muro version, or `undefined` if no such version is
106
+ * accepted. This is the direction a downstream needs: it depends on a language version and
107
+ * has to turn that into a package range.
108
+ */
109
+ export function koyuSince(muro: string): string | undefined {
110
+ return MURO_SUPPORT.find((r) => r.muro === muro)?.since;
111
+ }
112
+
113
+ /**
114
+ * Whether this build reads the given muro version.
115
+ *
116
+ * **What a downstream actually depends on is a language version, not a package range.** An
117
+ * application that writes `muro 1.1` needs a koyu that reads 1.1; which koyu that is, is the
118
+ * ledger's business, not the application's. Asserting this at startup turns a version skew
119
+ * into one sentence instead of a parse error somewhere later.
120
+ */
121
+ export function speaksMuro(muro: string): boolean {
122
+ return SUPPORTED_LANGUAGE_VERSIONS.includes(muro);
123
+ }
124
+
125
+ /**
126
+ * Throw unless this build reads the given muro version, naming what would fix it.
127
+ *
128
+ * Separate from `speaksMuro` because the useful thing at a startup check is the message: the
129
+ * caller knows the version it needs and nothing else, and the ledger is the only place that
130
+ * can say which koyu to install for it.
131
+ */
132
+ export function requireMuro(muro: string): void {
133
+ if (speaksMuro(muro)) return;
134
+ const row = MURO_SUPPORT.find((r) => r.muro === muro);
135
+ const head = `This build of koyu (${KOYU_VERSION}) does not read muro ${muro}.`;
136
+ // A row with `until` set is retired, and the advice is the opposite of the usual one:
137
+ // a newer koyu will not help, because newer is what dropped it.
138
+ if (row?.until) {
139
+ throw new Error(
140
+ `${head} It was retired after koyu ${row.until} — migrate the file, or install koyu ${row.until} or earlier.`,
141
+ );
142
+ }
143
+ if (row) throw new Error(`${head} It arrived in koyu ${row.since} — install koyu ${row.since} or later.`);
144
+ // **No row is not evidence that no koyu reads it.** A build only carries the rows compiled
145
+ // into it, so a version released after this one looks exactly like a version that never
146
+ // existed. Saying which is which is not this build's to say; saying what it knows is.
147
+ const newer = isNewerVersion(muro, NEWEST_LANGUAGE_VERSION);
148
+ throw new Error(
149
+ newer
150
+ ? `${head} It is newer than anything this build knows (it reads up to ${NEWEST_LANGUAGE_VERSION}) — upgrade koyu.`
151
+ : `${head} This build reads ${SUPPORTED_LANGUAGE_VERSIONS.join(", ")}.`,
152
+ );
153
+ }
154
+
155
+ /**
156
+ * The language versions this build accepts (ADR-0017). An older version is accepted only
157
+ * where the meaning is preserved; `check` is what decides that.
158
+ *
159
+ * **Derived from `MURO_SUPPORT`, not declared beside it.** Two lists of one fact is how the
160
+ * correspondence drifted in the first place.
161
+ */
162
+ export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = MURO_SUPPORT.filter(
163
+ (r) => r.until === null,
164
+ ).map((r) => r.muro);
165
+
166
+ /**
167
+ * The newest language version this build accepts — what to declare to get everything.
168
+ * Derived, for the same reason as above.
169
+ */
170
+ export const NEWEST_LANGUAGE_VERSION = SUPPORTED_LANGUAGE_VERSIONS[SUPPORTED_LANGUAGE_VERSIONS.length - 1]!;
171
+
172
+ /**
173
+ * How a file with no version line is read. **Frozen at 1.1, and it does not follow the newest
174
+ * version.**
175
+ *
176
+ * It used to be the newest, which meant an undeclared file was re-read under new semantics
177
+ * every time the tool moved — silently, because nothing reports the absence of a declaration.
178
+ * That is not hypothetical: the 1.0 → 1.1 move read `exterior` out of the type position, and
179
+ * undeclared files written in the old dialect lost their outside spaces without a word.
180
+ *
181
+ * Freezing removes the danger rather than reporting it. **Newer semantics are opt-in: you get
182
+ * them by naming them.** The cost is that an undeclared file never gets new notation, which is
183
+ * the same statement read from the other side.
184
+ *
185
+ * Freezing this was not itself a version bump: at the moment it froze, the frozen value and
186
+ * the newest version were both 1.1, so no file that existed read differently. They part
187
+ * company from 1.2 on, and that gap is the whole point — an old file that names no version
188
+ * does not quietly become a 1.2 file.
13
189
  */
14
- export const SUPPORTED_LANGUAGE_VERSIONS: readonly string[] = ["0.1", "0.2", "0.3", "0.4", "0.5", "1.0", "1.1"];
15
- /** 版宣言を省略したときの解釈 — 常に最新版の意味論 (省略はツール版を跨いで意味安定ではない) */
16
190
  export const DEFAULT_LANGUAGE_VERSION = "1.1";
17
191
 
18
192
  /**
19
193
  * 機械形式 (正準JSON) が自分を名乗る版 (ADR-0036)。**言語版でもツール版でもない** —
20
- * 数えるのは綴りだけである。minorはキーが増えたとき (増えたキーを持たない文書のバイトは変わらない)、
21
- * majorは既存のキーの名前・並び・照合順・正規化・数の綴りが変わったとき (既存の文書のバイトが変わる)。
22
- * 意味論の版は muro が持つので、`koyu` キー (書かれた版宣言の素通し) とは別の面である
194
+ * 数えるのは綴りだけである。minorはキーが増えたとき、majorは既存のキーの名前・並び・照合順・
195
+ * 正規化・数の綴りが変わったとき。**minorでも全ての文書のバイトは動く** — この文字列自体が
196
+ * 第一キーだからで、増えたキーを持たない文書も先頭行だけは変わる (ADR-0051 が実測している)。
197
+ * 意味論の版は muro が持つので、`muro` キー (書かれた版宣言の素通し) とは別の面である。
198
+ *
199
+ * The format keeps the name `koyu` because it is the implementation's output spelling — the
200
+ * shape koyu writes. The key inside it names the language, and is spelled `muro` for the same
201
+ * reason the version line is.
202
+ *
203
+ * **That rename is why this is 2.0 and not 1.3.** An existing key changed its name, which the
204
+ * rule above puts squarely in major. Shipping it as a minor would have told every reader that
205
+ * `1.x` stays compatible — so a reader expecting `koyu` would have accepted the document and
206
+ * found no language version in it, which is the silent misread the format version exists to
207
+ * prevent.
23
208
  */
24
- export const CANONICAL_FORMAT = "koyu-canonical/1.1";
209
+ export const CANONICAL_FORMAT = "koyu-canonical/2.0";
25
210
 
26
211
  /** 方位。edge指定は「最初に書いた空間」の矩形から見た辺。N=+Y, S=-Y, E=+X, W=-X */
27
212
  export type Edge = "N" | "E" | "S" | "W";
@@ -250,6 +435,13 @@ export interface Model {
250
435
  version: string;
251
436
  name?: string;
252
437
  unit: "mm";
438
+ /**
439
+ * 測地の枠 — 位置 (ADR-0057)。**core は持つだけで、投影も子午線収差角も計算しない。**
440
+ * モデルは一つの枠を持つ (`origin` は全レイヤーを通して一度)
441
+ */
442
+ origin?: SiteOrigin;
443
+ /** 測地の枠 — 真北 (ADR-0057)。**不在は 0 ではなく「未知」である** */
444
+ azimuth?: Azimuth;
253
445
  grid: { X: GridAxis; Y: GridAxis };
254
446
  levels: Record<string, Level>;
255
447
  spaces: Map<string, Space>;
@@ -350,6 +542,47 @@ export interface SitePolygon {
350
542
  file?: string;
351
543
  }
352
544
 
545
+ /**
546
+ * 測地の原点 (ADR-0057) — モデルの (0,0,0) が外の座標系のどこに座るか。測量に由来する所与で、
547
+ * 敷地形状 (SitePolygon) と同じ隔離レイヤーに住む。
548
+ *
549
+ * **単位はメートルであって mm ではない。** これはモデルの中の長さではなく、外の枠の中の点であり、
550
+ * 外の枠は自分の数字を持つ。mm を選べば `-8000.12` → `-800012` の桁埋め誤りが 7.2m のずれとして
551
+ * 完全に静かに通る (1000倍の誤りは騒がしいが、桁埋めの誤りは騒がない)。
552
+ *
553
+ * **EPSG コードは解釈しない。**測地系・投影・系番号・軸順を一語で言い切るので、測地系を別に綴れば
554
+ * 矛盾の余地ができる。core が見るのは正の整数であることだけで、コード表は持たない
555
+ */
556
+ export interface SiteOrigin {
557
+ /** 水平 (2D 投影) CRS の EPSG コード。複合コードは受けない — `vertical` と役割を分ける */
558
+ epsg: number;
559
+ /** 東距 m。**平面直角座標系の成果では Y 欄である** */
560
+ easting: number;
561
+ /** 北距 m。**平面直角座標系の成果では X 欄である** — 通常の慣習の逆 */
562
+ northing: number;
563
+ /** モデルの z=0 の高さ m。**GL でも地盤面でも平均地盤面でもない。**`vertical` と対で書く */
564
+ elevation?: number;
565
+ /** 鉛直 CRS の EPSG コード (日本は 6695 = T.P. 系)。`elevation` と対で必須 */
566
+ vertical?: number;
567
+ line: number;
568
+ file?: string;
569
+ }
570
+
571
+ /**
572
+ * 真北 (ADR-0057) — モデルの +Y 軸の真方位角。真北から時計回りの度、0 ≤ deg < 360。
573
+ *
574
+ * **方位角であって回転角ではない。**「+Y から北へ時計回り」と綴れば逆向きの方位角になり、
575
+ * 建築も測量も時計回りの角を北から対象へ測るので、読者の既定の読みが定義の逆になる。
576
+ *
577
+ * `N/E/S/W` は依然として軸の語である (docs/reference/muro/orientation.md)。方位を持つ場所は
578
+ * ここひとつで、面の真方位角は deg / deg+90 / deg+180 / deg+270 と消費者の側で一行で出る
579
+ */
580
+ export interface Azimuth {
581
+ deg: number;
582
+ line: number;
583
+ file?: string;
584
+ }
585
+
353
586
  /** 多角形の面積 ㎡。シューレースの実体は poly.ts にひとつだけある */
354
587
  export const polygonAreaM2 = (points: Pt[]): number => poly.area(points) / 1e6;
355
588
 
@@ -412,6 +645,19 @@ export class SourceError extends Error {
412
645
  public raw: string,
413
646
  /** 出所ファイル (合成時) */
414
647
  public file?: string,
648
+ /**
649
+ * The diagnostic code this failure carries in `check --json`, when it has one of its own.
650
+ *
651
+ * Most parse failures are just syntax and are reported as `SYN01`. A few are a named
652
+ * condition a caller has to be able to act on without matching the message text — a file
653
+ * declaring a language version this build cannot read is the case that forced this: the
654
+ * answer is "upgrade koyu", and a viewer wanting to say so should not be reading English.
655
+ *
656
+ * Narrow on purpose. The diagnostic ledger lives in `diagnose.ts`, which reads this
657
+ * module, so the union cannot be imported here — and widening it to `string` would let
658
+ * a typo reach `check --json` as a code no page documents.
659
+ */
660
+ public code?: "VER06",
415
661
  ) {
416
662
  super(`${file ? `${file}:` : ""}line ${line}: ${raw}`);
417
663
  this.name = "SourceError";
@@ -892,9 +1138,27 @@ export function toCanonical(model: Model): string {
892
1138
  // 出せば、その版を著者は書いていないのに書いたことになり、しかもツールの既定が動いた日に
893
1139
  // 同じ入力のバイトが変わる。決定性は形式の側の約束なので、ツールの既定に預けない
894
1140
  format: CANONICAL_FORMAT,
895
- ...(model.versionDeclared ? { koyu: model.version } : {}),
1141
+ // **The key names the language, so it is spelled `muro`** — whatever word the file used.
1142
+ // A document written `koyu 1.1` still says `"muro": "1.1"` here: the key is the name of
1143
+ // the thing being versioned, not a copy of how the author spelled the declaration.
1144
+ ...(model.versionDeclared ? { muro: model.version } : {}),
896
1145
  ...(model.name ? { name: model.name } : {}),
897
1146
  unit: model.unit,
1147
+ // 測地の枠 (ADR-0057) — **単位を言った直後、グリッドを言う前。**この二つは grid が張る
1148
+ // 座標系そのものを外の世界に結びつけるので、その座標系より先に立つ。
1149
+ // **値はメートル**であって mm ではない (外の枠の中の点だから)。書かれなければ鍵ごと出ない
1150
+ ...(model.origin
1151
+ ? {
1152
+ origin: {
1153
+ epsg: model.origin.epsg,
1154
+ easting: model.origin.easting,
1155
+ northing: model.origin.northing,
1156
+ ...(model.origin.elevation !== undefined ? { elevation: model.origin.elevation } : {}),
1157
+ ...(model.origin.vertical !== undefined ? { vertical: model.origin.vertical } : {}),
1158
+ },
1159
+ }
1160
+ : {}),
1161
+ ...(model.azimuth ? { azimuth: model.azimuth.deg } : {}),
898
1162
  grid: { X: model.grid.X.coords, Y: model.grid.Y.coords },
899
1163
  levels: sortObj(
900
1164
  Object.fromEntries(