@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.10

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 (285) hide show
  1. package/CHANGELOG.md +251 -0
  2. package/LICENSE +1 -1
  3. package/LICENSE-cobra.txt +174 -0
  4. package/NOTICE +15 -0
  5. package/README.md +55 -259
  6. package/dist/aim-DZHwwiIc.d.ts +139 -0
  7. package/dist/aim.d.ts +1 -116
  8. package/dist/aim.js +13 -414
  9. package/dist/alias-D9_qFlyP.d.ts +174 -0
  10. package/dist/alias.d.ts +2 -73
  11. package/dist/alias.js +27 -207
  12. package/dist/api-DAexsu2H.d.ts +77 -0
  13. package/dist/api.d.ts +1 -62
  14. package/dist/api.js +10 -164
  15. package/dist/auth-2-jfgiHn.d.ts +34 -0
  16. package/dist/auth.d.ts +1 -26
  17. package/dist/auth.js +6 -40
  18. package/dist/chunk-2JV3A7OP.js +1894 -0
  19. package/dist/chunk-3FIKGFKI.js +54 -0
  20. package/dist/chunk-4UE5TCMQ.js +104 -0
  21. package/dist/chunk-6HFAEZ52.js +29 -0
  22. package/dist/chunk-6SVOHNIC.js +17 -0
  23. package/dist/chunk-72XZWQX7.js +504 -0
  24. package/dist/chunk-ABQSE7AI.js +513 -0
  25. package/dist/chunk-BDNNPRX6.js +41 -0
  26. package/dist/chunk-BLS2OBIQ.js +263 -0
  27. package/dist/chunk-BRNEKPWX.js +37 -0
  28. package/dist/chunk-CSJWB6D3.js +151 -0
  29. package/dist/chunk-GGWJRWWA.js +309 -0
  30. package/dist/chunk-GHPJJV5L.js +613 -0
  31. package/dist/chunk-IXVQ62NZ.js +334 -0
  32. package/dist/chunk-JKMQSAJP.js +174 -0
  33. package/dist/chunk-JZ22ZUFA.js +1131 -0
  34. package/dist/chunk-K7VCKJ27.js +814 -0
  35. package/dist/chunk-KVG7YCTS.js +115 -0
  36. package/dist/chunk-L23O2FFE.js +51 -0
  37. package/dist/chunk-MQXYDYRD.js +968 -0
  38. package/dist/chunk-MUIQVU6E.js +251 -0
  39. package/dist/chunk-NLMS7HGP.js +178 -0
  40. package/dist/chunk-POR5L2DG.js +247 -0
  41. package/dist/chunk-PSFYJGDG.js +1445 -0
  42. package/dist/chunk-QJVSKKRQ.js +369 -0
  43. package/dist/chunk-QPUCXQXT.js +67 -0
  44. package/dist/chunk-QQQYCWSC.js +240 -0
  45. package/dist/chunk-R5U7XKVJ.js +16 -0
  46. package/dist/chunk-SNVGDYX7.js +187 -0
  47. package/dist/chunk-T46YPHKN.js +669 -0
  48. package/dist/chunk-UKZIAQA7.js +26 -0
  49. package/dist/chunk-WBVO43HU.js +355 -0
  50. package/dist/chunk-WUOAQVOA.js +86 -0
  51. package/dist/chunk-WWUKGJB5.js +90 -0
  52. package/dist/chunk-XJTRY453.js +868 -0
  53. package/dist/chunk-YGYQUFGY.js +219 -0
  54. package/dist/chunk-YOBAUCVN.js +583 -0
  55. package/dist/cli-C5B5tnpA.d.ts +400 -0
  56. package/dist/cli.d.ts +7 -309
  57. package/dist/cli.js +70 -945
  58. package/dist/completion-csVun6SP.d.ts +112 -0
  59. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  60. package/dist/completion-values.d.ts +1 -0
  61. package/dist/completion-values.js +29 -0
  62. package/dist/completion.d.ts +3 -0
  63. package/dist/completion.js +34 -0
  64. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  65. package/dist/compliance.d.ts +1 -0
  66. package/dist/compliance.js +17 -0
  67. package/dist/config-CaK60ByQ.d.ts +241 -0
  68. package/dist/config.d.ts +1 -97
  69. package/dist/config.js +8 -78
  70. package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
  71. package/dist/errcorrect.d.ts +1 -37
  72. package/dist/errcorrect.js +8 -68
  73. package/dist/error-Do8Qji43.d.ts +231 -0
  74. package/dist/id/index.d.ts +2 -83
  75. package/dist/id/index.js +14 -65
  76. package/dist/index-BKJZClJx.d.ts +1486 -0
  77. package/dist/index-C3Zw4wDJ.d.ts +359 -0
  78. package/dist/index-DCe1boBz.d.ts +328 -0
  79. package/dist/index-VLDcI5NF.d.ts +95 -0
  80. package/dist/index.d.ts +37 -0
  81. package/dist/index.js +132 -0
  82. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  83. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  84. package/dist/llm-anthropic.d.ts +2 -0
  85. package/dist/llm-anthropic.js +13 -0
  86. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  87. package/dist/llm-openai.d.ts +2 -0
  88. package/dist/llm-openai.js +19 -0
  89. package/dist/llm.d.ts +1 -188
  90. package/dist/llm.js +101 -356
  91. package/dist/log-CkaxMX3U.d.ts +115 -0
  92. package/dist/log.d.ts +2 -0
  93. package/dist/log.js +14 -0
  94. package/dist/mcp/index.d.ts +4 -0
  95. package/dist/mcp/index.js +184 -0
  96. package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
  97. package/dist/netpolicy.d.ts +1 -0
  98. package/dist/netpolicy.js +25 -0
  99. package/dist/output-CjZWmL8s.d.ts +420 -0
  100. package/dist/output.d.ts +3 -213
  101. package/dist/output.js +137 -447
  102. package/dist/parity-CvMuYcVI.d.ts +61 -0
  103. package/dist/progress-CAzyxeWl.d.ts +38 -0
  104. package/dist/progress.d.ts +1 -30
  105. package/dist/progress.js +6 -55
  106. package/dist/provenance-SMFHm5gx.d.ts +30 -0
  107. package/dist/provenance.d.ts +1 -24
  108. package/dist/provenance.js +6 -31
  109. package/dist/routellm-B0nv59mH.d.ts +111 -0
  110. package/dist/routellm.d.ts +2 -64
  111. package/dist/routellm.js +13 -255
  112. package/dist/rpc-D31Lg0Kc.d.ts +32 -0
  113. package/dist/rpc.d.ts +2 -25
  114. package/dist/rpc.js +4 -98
  115. package/dist/safety-CUaYEjZc.d.ts +30 -0
  116. package/dist/safety.d.ts +1 -24
  117. package/dist/safety.js +6 -43
  118. package/dist/scope-BrhYB4ij.d.ts +141 -0
  119. package/dist/scope.d.ts +1 -121
  120. package/dist/scope.js +20 -377
  121. package/dist/serve-D8yWMrTc.d.ts +646 -0
  122. package/dist/serve.d.ts +3 -0
  123. package/dist/serve.js +75 -0
  124. package/dist/sqlstore.js +6 -39
  125. package/dist/stream-CrjeCp0M.d.ts +44 -0
  126. package/dist/stream.d.ts +1 -35
  127. package/dist/stream.js +8 -51
  128. package/dist/telemetry/index.d.ts +1 -315
  129. package/dist/telemetry/index.js +26 -599
  130. package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
  131. package/dist/toolspec.d.ts +2 -0
  132. package/dist/toolspec.js +37 -0
  133. package/dist/tui/index.d.ts +6 -298
  134. package/dist/tui/index.js +17 -381
  135. package/dist/upgrade-p_zh3pvV.d.ts +102 -0
  136. package/dist/upgrade.d.ts +1 -95
  137. package/dist/upgrade.js +6 -114
  138. package/dist/xdg-DB7Shoib.d.ts +179 -0
  139. package/dist/xdg.d.ts +1 -169
  140. package/dist/xdg.js +14 -128
  141. package/package.json +192 -24
  142. package/src/README.md +15 -0
  143. package/src/alias.conformance.test.ts +250 -0
  144. package/src/alias.test.ts +206 -2
  145. package/src/alias.ts +196 -21
  146. package/src/cli.help-command.test.ts +105 -0
  147. package/src/cli.offline.test.ts +93 -0
  148. package/src/cli.parity.test.ts +192 -0
  149. package/src/cli.test.ts +95 -1
  150. package/src/cli.ts +195 -25
  151. package/src/completion-scripts.ts +16 -0
  152. package/src/completion-values.test.ts +76 -16
  153. package/src/completion-values.ts +170 -66
  154. package/src/completion.conformance.test.ts +297 -0
  155. package/src/completion.test.ts +256 -145
  156. package/src/completion.ts +767 -121
  157. package/src/compliance.test.ts +196 -2
  158. package/src/compliance.ts +440 -20
  159. package/src/config-merge.test.ts +425 -0
  160. package/src/config.test.ts +228 -1
  161. package/src/config.ts +429 -16
  162. package/src/gen/crud_pb.ts +7 -7
  163. package/src/id/README.md +38 -0
  164. package/src/id/contract.test.ts +1 -1
  165. package/src/id/index.test.ts +1 -1
  166. package/src/id/index.ts +3 -2
  167. package/src/index.test.ts +71 -0
  168. package/src/index.ts +46 -0
  169. package/src/llm-anthropic.conformance.test.ts +1135 -0
  170. package/src/llm-anthropic.test.ts +152 -0
  171. package/src/llm-anthropic.ts +940 -0
  172. package/src/llm-choice-hermetic.test.ts +335 -0
  173. package/src/llm-config.test.ts +232 -0
  174. package/src/llm-config.ts +106 -0
  175. package/src/llm-endpoint.ts +254 -0
  176. package/src/llm-error.ts +17 -0
  177. package/src/llm-errors.ts +152 -0
  178. package/src/llm-file.ts +126 -0
  179. package/src/llm-http.ts +317 -0
  180. package/src/llm-keys.ts +167 -0
  181. package/src/llm-media.conformance.test.ts +87 -0
  182. package/src/llm-media.test.ts +279 -0
  183. package/src/llm-media.ts +322 -0
  184. package/src/llm-openai.conformance.test.ts +407 -0
  185. package/src/llm-openai.test.ts +510 -0
  186. package/src/llm-openai.ts +708 -0
  187. package/src/llm-payload.ts +155 -0
  188. package/src/llm-quote.ts +46 -0
  189. package/src/llm-routellm.conformance.test.ts +44 -0
  190. package/src/llm-toolnames.conformance.test.ts +112 -0
  191. package/src/llm-toolnames.test.ts +132 -0
  192. package/src/llm-toolnames.ts +298 -0
  193. package/src/llm-uri.ts +99 -0
  194. package/src/llm.test.ts +43 -15
  195. package/src/llm.toolcall.test.ts +175 -0
  196. package/src/llm.ts +602 -190
  197. package/src/log.parity.test.ts +105 -0
  198. package/src/log.test.ts +270 -1
  199. package/src/log.ts +228 -49
  200. package/src/mcp/README.md +63 -0
  201. package/src/mcp/args-separator.test.ts +164 -0
  202. package/src/mcp/args.test.ts +223 -0
  203. package/src/mcp/args.ts +160 -0
  204. package/src/mcp/audit.test.ts +520 -0
  205. package/src/mcp/audit.ts +70 -0
  206. package/src/mcp/bridge.test.ts +222 -0
  207. package/src/mcp/bridge.ts +291 -0
  208. package/src/mcp/conformance.test.ts +357 -0
  209. package/src/mcp/dispatch.ts +359 -0
  210. package/src/mcp/identity.test.ts +166 -0
  211. package/src/mcp/identity.ts +72 -0
  212. package/src/mcp/index.ts +176 -0
  213. package/src/mcp/legacy-notify.test.ts +59 -0
  214. package/src/mcp/legacy.ts +367 -0
  215. package/src/mcp/mcp.test.ts +707 -0
  216. package/src/mcp/modern-confirm.test.ts +520 -0
  217. package/src/mcp/modern-confirm.ts +484 -0
  218. package/src/mcp/modern.ts +788 -0
  219. package/src/mcp/stdio.test.ts +313 -0
  220. package/src/mcp/stdio.ts +254 -0
  221. package/src/mcp/tasks.ts +82 -0
  222. package/src/mcp/testtree.ts +250 -0
  223. package/src/mcp/toolname.test.ts +147 -0
  224. package/src/mcp/types.ts +689 -0
  225. package/src/netpolicy.test.ts +280 -0
  226. package/src/netpolicy.ts +256 -0
  227. package/src/output/README.md +46 -0
  228. package/src/output/builtins.ts +5 -5
  229. package/src/output/dispatch.ts +10 -2
  230. package/src/output/error.ts +459 -0
  231. package/src/output/formatter.ts +29 -5
  232. package/src/output/formatters/README.md +50 -0
  233. package/src/output/formatters/csv.ts +48 -12
  234. package/src/output/formatters/json.ts +4 -25
  235. package/src/output/formatters/project.ts +34 -0
  236. package/src/output/formatters/table.ts +8 -6
  237. package/src/output/formatters/text.ts +7 -3
  238. package/src/output/formatters/yaml.ts +5 -22
  239. package/src/output/projection.test.ts +298 -0
  240. package/src/output/projection.ts +33 -59
  241. package/src/output/template.ts +11 -21
  242. package/src/output.surface.test.ts +41 -0
  243. package/src/output.ts +64 -3
  244. package/src/published-types.test.ts +133 -0
  245. package/src/routellm.test.ts +209 -28
  246. package/src/routellm.toolcall.test.ts +521 -0
  247. package/src/routellm.ts +213 -177
  248. package/src/router/README.md +44 -0
  249. package/src/router/server.test.ts +41 -0
  250. package/src/router/server.ts +11 -0
  251. package/src/safety.test.ts +19 -9
  252. package/src/scope-defaults.json +29 -29
  253. package/src/serve.test.ts +1257 -0
  254. package/src/serve.ts +1665 -0
  255. package/src/telemetry/README.md +48 -0
  256. package/src/telemetry/client.test.ts +31 -0
  257. package/src/telemetry/client.ts +12 -6
  258. package/src/telemetry/offline.test.ts +64 -0
  259. package/src/toolspec-internal.ts +279 -0
  260. package/src/toolspec-manifest.test.ts +167 -0
  261. package/src/toolspec-manifest.ts +410 -0
  262. package/src/toolspec-spec-schemas.ts +12 -0
  263. package/src/toolspec-spec.test.ts +273 -0
  264. package/src/toolspec-spec.ts +458 -0
  265. package/src/toolspec.from-command.test.ts +350 -0
  266. package/src/toolspec.test.ts +2 -2
  267. package/src/toolspec.ts +222 -0
  268. package/src/triton/README.md +34 -0
  269. package/src/tui/README.md +42 -0
  270. package/src/tui/parity.test.ts +120 -31
  271. package/src/tui/parity.ts +58 -7
  272. package/src/wizard.test.ts +7 -7
  273. package/test/llm/conformance-helpers.ts +100 -0
  274. package/test/output/csv.test.ts +164 -0
  275. package/test/output/dispatch.test.ts +1 -2
  276. package/test/output/error.test.ts +277 -0
  277. package/test/output/smoke-capture.ts +2 -2
  278. package/test/output/smoke.test.ts +2 -2
  279. package/test/output/taxonomy-contract.test.ts +148 -0
  280. package/test/setup-env.ts +16 -0
  281. package/test/toolspec/fixture.ts +79 -0
  282. package/tsconfig.json +3 -1
  283. package/tsconfig.test.json +10 -0
  284. package/vitest.config.ts +9 -0
  285. package/src/tui/parity.json +0 -74
package/src/config.ts CHANGED
@@ -1,14 +1,50 @@
1
1
  /**
2
2
  * @packageDocumentation
3
3
  *
4
- * Layered YAML configuration loader — TypeScript port of hop.top/kit/config.
4
+ * Layered configuration loader (YAML by default) — TypeScript port of
5
+ * hop.top/kit/config.
5
6
  *
6
7
  * Merges configuration from up to three file layers in priority order:
7
8
  * system → user → project → envOverride
8
9
  *
9
- * Each later layer's values overwrite earlier ones via {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign | Object.assign}.
10
+ * Each layer merges into `dst` with these rules:
11
+ *
12
+ * - Plain objects merge recursively, key by key, so a later layer only
13
+ * overrides the keys it sets (`db: {port: 2}` keeps an earlier `db.host`).
14
+ * - Arrays and scalars are replaced wholesale.
15
+ * - `null` clears an object or array; it leaves a scalar untouched.
16
+ * - On a type conflict (object vs non-object) the later value replaces the
17
+ * earlier one.
18
+ * - Non-plain objects (`Date`, `Map`, class instances) are never merged
19
+ * into; a later value replaces them.
20
+ *
21
+ * {@link mergeLayers} applies the same rules to objects you already parsed,
22
+ * and {@link MergeOptions} (also accepted by {@link load} as `merge`) can
23
+ * relax them: `null` keeps the lower value, objects merge only to a set depth,
24
+ * arrays concatenate or union, globally or per dotted key path. A file in
25
+ * another format goes through {@link Options.parse}.
26
+ *
27
+ * The defaults match Go's `kit/core/config` for struct fields. Go differs where its
28
+ * target type differs: a `map[...]` value or an `any` field is replaced
29
+ * whole rather than merged, and a type conflict on a typed field is a decode
30
+ * error rather than a replacement.
31
+ *
10
32
  * Missing files are silently skipped. A file that exists but contains invalid
11
- * YAML causes {@link load} to throw.
33
+ * YAML causes {@link load} to throw. A document whose root is not a mapping
34
+ * is ignored.
35
+ *
36
+ * Each file is checked before it merges, with aliases expanded, and
37
+ * {@link load} throws `config: failed to parse <path>: <reason>` when:
38
+ *
39
+ * - its aliases expand past Go yaml.v3's budget: more than 100 nodes inside
40
+ * alias expansions and more than 1000 in total, with the share inside
41
+ * aliases above 0.99 (up to 400k nodes), falling linearly to 0.10 at 4M
42
+ * (`document contains excessive aliasing`). Go counts only the keys its
43
+ * target struct reads; TS has no target, so it counts every key, and a
44
+ * file Go accepts because the aliases sit under ignored keys fails here.
45
+ * - collections nest deeper than 100, counted through aliases
46
+ * (`nesting exceeded maxDepth (100)`). Go allows 10000 levels of text
47
+ * nesting and caps alias chains only through the budget above.
12
48
  *
13
49
  * @example
14
50
  * ```ts
@@ -37,6 +73,7 @@
37
73
  */
38
74
 
39
75
  import * as fs from "fs";
76
+ import * as path from "path";
40
77
  import * as yaml from "js-yaml";
41
78
 
42
79
  // ---------------------------------------------------------------------------
@@ -59,13 +96,13 @@ export interface Options<T extends object = object> {
59
96
 
60
97
  /**
61
98
  * Path to the per-user config file, e.g. `~/.config/mytool/config.yaml`.
62
- * Applied second; overwrites system values for shared keys.
99
+ * Applied second; merges over system values (see the module merge rules).
63
100
  */
64
101
  userConfigPath?: string;
65
102
 
66
103
  /**
67
104
  * Path to the project-level config file, e.g. `.mytool.yaml`.
68
- * Applied third; overwrites system and user values for shared keys.
105
+ * Applied third; merges over system and user values.
69
106
  */
70
107
  projectConfigPath?: string;
71
108
 
@@ -77,6 +114,124 @@ export interface Options<T extends object = object> {
77
114
  * @param cfg - The merged config object (same reference as `dst`).
78
115
  */
79
116
  envOverride?: (cfg: T) => void;
117
+
118
+ /**
119
+ * Parser for file contents; YAML when omitted. A function parses every
120
+ * file. A record maps a lowercased extension with its dot (`".json"`;
121
+ * `""` for an extensionless file such as `.toolrc`) to a parser, and files
122
+ * whose extension has no entry parse as YAML. The parser gets the text and
123
+ * the path (so a `.js` parser can `require` the path instead); a throw
124
+ * fails the load like a YAML error, and a result that is not a plain
125
+ * object is ignored. Without a parser a `.js` file parses as YAML text
126
+ * and is never executed.
127
+ */
128
+ parse?: ConfigParser | Record<string, ConfigParser>;
129
+
130
+ /**
131
+ * Merge policies for the file layers, `dst` being the lowest layer. They
132
+ * do not touch {@link envOverride}, which assigns whatever it sets.
133
+ * Defaults are the module merge rules.
134
+ */
135
+ merge?: MergeOptions;
136
+ }
137
+
138
+ /** Parses one config file's text; see {@link Options.parse}. */
139
+ export type ConfigParser = (text: string, filePath: string) => unknown;
140
+
141
+ /** What a `null` in a higher layer does; see {@link MergeOptions.nullPolicy}. */
142
+ export type NullPolicy = "clear" | "keep-lower";
143
+
144
+ /** How two arrays at one key combine; see {@link MergeOptions.arrayPolicy}. */
145
+ export type ArrayPolicy = "replace" | "concat" | "union";
146
+
147
+ /**
148
+ * Custom array merge for one path: gets the lower array (already in the
149
+ * result), a copy of the higher array and the dotted path; returns the value
150
+ * to store.
151
+ */
152
+ export type ArrayMergeFn = (lower: unknown[], higher: unknown[], path: string) => unknown;
153
+
154
+ /**
155
+ * Merge policies for {@link mergeLayers} and {@link Options.merge}. All
156
+ * policies act only where a higher layer's key meets a key already in the
157
+ * result; a value with nothing under it (a new key, a type conflict, an
158
+ * object past `maxDepth`) is copied whole, as is.
159
+ */
160
+ export interface MergeOptions {
161
+ /**
162
+ * `"clear"` (default): `null` clears an object or array, sets a key that
163
+ * is absent or already `null`, and leaves a scalar untouched.
164
+ * `"keep-lower"`: `null` is skipped, so the lower value stays and an
165
+ * absent key stays absent.
166
+ */
167
+ nullPolicy?: NullPolicy;
168
+
169
+ /**
170
+ * How many object levels below the root merge key by key; deeper objects
171
+ * replace whole. `Infinity` (default) merges at every depth, `1` merges the
172
+ * root and the objects directly under it (`{db: {opts: {...}}}` merges
173
+ * `db` but replaces `db.opts`), `0` assigns root keys only, like
174
+ * `Object.assign`. Must be a non-negative integer or `Infinity`.
175
+ */
176
+ maxDepth?: number;
177
+
178
+ /**
179
+ * How two arrays at one key combine. `"replace"` (default): the higher
180
+ * array wins. `"concat"`: lower items, then higher items. `"union"`: lower
181
+ * then higher items, keeping the first of equal items, where plain objects
182
+ * and arrays compare structurally (object key order ignored) and other
183
+ * values by SameValueZero. A policy acts only when both sides are arrays;
184
+ * otherwise the higher value replaces.
185
+ */
186
+ arrayPolicy?: ArrayPolicy;
187
+
188
+ /**
189
+ * Per-path override of {@link arrayPolicy}: dotted key path from the root
190
+ * (`"scope.hints"`, split on every `.` like the Go SDK's dotted keys, so a
191
+ * key containing a dot cannot be named) to a policy or an
192
+ * {@link ArrayMergeFn}, which gets the concrete path. A segment that is
193
+ * exactly `*` matches any one key (`"scopes.*.hints"`, never zero or
194
+ * several; `a*` is literal, `**` throws). When several entries match, the
195
+ * exact path wins, then the pattern with the fewest `*`, then the
196
+ * lexically smallest pattern (UTF-16 order, so `*.b.c` beats `a.*.c`).
197
+ */
198
+ arrayPolicyByPath?: Record<string, ArrayPolicy | ArrayMergeFn>;
199
+ }
200
+
201
+ /**
202
+ * Merges already-parsed layers, lowest priority first, into a new object and
203
+ * returns it. Layers that are not plain objects (`null`, arrays, scalars)
204
+ * are skipped; the layers are never mutated or shared with the result.
205
+ * Defaults follow the module merge rules; {@link MergeOptions} relaxes them.
206
+ *
207
+ * @throws {Error} `config: layer <i>: <reason>` when a layer nests deeper
208
+ * than 100 levels or contains a cycle, and on an invalid option.
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * const cfg = mergeLayers([globalRc, projectRc], {
213
+ * nullPolicy: "keep-lower",
214
+ * maxDepth: 1,
215
+ * arrayPolicyByPath: { hints: "union" },
216
+ * });
217
+ * ```
218
+ */
219
+ export function mergeLayers<T extends object = Record<string, unknown>>(
220
+ layers: readonly unknown[],
221
+ opts: MergeOptions = {},
222
+ ): T {
223
+ const policy = resolvePolicy(opts);
224
+ const out: Record<string, unknown> = {};
225
+ layers.forEach((layer, i) => {
226
+ if (!isPlainObject(layer)) return;
227
+ try {
228
+ checkBounds(layer);
229
+ } catch (err: unknown) {
230
+ throw new Error(`config: layer ${i}: ${errMessage(err)}`);
231
+ }
232
+ mergeInto(out, layer, policy, 0, "");
233
+ });
234
+ return out as T;
80
235
  }
81
236
 
82
237
  /**
@@ -85,6 +240,10 @@ export interface Options<T extends object = object> {
85
240
  * Merge order: `systemConfigPath` → `userConfigPath` → `projectConfigPath` →
86
241
  * `envOverride`.
87
242
  *
243
+ * - Objects merge recursively; arrays and scalars replace; `null` clears
244
+ * objects and arrays but not scalars (see the module docs for details),
245
+ * unless {@link Options.merge} says otherwise.
246
+ * - Files parse as YAML unless {@link Options.parse} supplies a parser.
88
247
  * - Missing files (ENOENT) are silently skipped.
89
248
  * - A file that exists but is not valid YAML causes this function to throw
90
249
  * a descriptive error wrapping the parse failure.
@@ -95,7 +254,8 @@ export interface Options<T extends object = object> {
95
254
  * object; mutated in place.
96
255
  * @param opts - Config sources. All fields are optional.
97
256
  * @returns The mutated `dst` object.
98
- * @throws {Error} When a file exists but cannot be parsed as YAML.
257
+ * @throws {Error} When a file exists but cannot be parsed, or on an
258
+ * invalid {@link Options.merge}.
99
259
  *
100
260
  * @example
101
261
  * ```ts
@@ -105,6 +265,7 @@ export interface Options<T extends object = object> {
105
265
  * ```
106
266
  */
107
267
  export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
268
+ const policy = resolvePolicy(opts.merge ?? {});
108
269
  const paths = [
109
270
  opts.systemConfigPath,
110
271
  opts.userConfigPath,
@@ -113,7 +274,7 @@ export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
113
274
 
114
275
  for (const p of paths) {
115
276
  if (!p) continue;
116
- mergeFile(dst, p);
277
+ mergeFile(dst, p, opts.parse, policy);
117
278
  }
118
279
 
119
280
  if (opts.envOverride) {
@@ -128,10 +289,16 @@ export function load<T extends object>(dst: T, opts: Options<T> = {}): T {
128
289
  // ---------------------------------------------------------------------------
129
290
 
130
291
  /**
131
- * Read `path`, parse as YAML, and shallow-merge into `dst`.
132
- * ENOENT is silently ignored; other errors propagate.
292
+ * Read `path`, parse it (YAML unless `parse` picks a parser), and merge into
293
+ * `dst` with {@link mergeInto}. ENOENT is silently ignored; other errors
294
+ * propagate.
133
295
  */
134
- function mergeFile<T extends object>(dst: T, filePath: string): void {
296
+ function mergeFile<T extends object>(
297
+ dst: T,
298
+ filePath: string,
299
+ parse: Options["parse"],
300
+ policy: Policy,
301
+ ): void {
135
302
  let raw: string;
136
303
  try {
137
304
  raw = fs.readFileSync(filePath, "utf8");
@@ -142,18 +309,264 @@ function mergeFile<T extends object>(dst: T, filePath: string): void {
142
309
 
143
310
  let parsed: unknown;
144
311
  try {
145
- parsed = yaml.load(raw);
312
+ const parser = parserFor(parse, filePath);
313
+ parsed = parser ? parser(raw, filePath) : yaml.load(raw);
314
+ checkBounds(parsed);
146
315
  } catch (err: unknown) {
147
- throw new Error(
148
- `config: failed to parse ${filePath}: ${err instanceof Error ? err.message : String(err)}`,
149
- );
316
+ throw new Error(`config: failed to parse ${filePath}: ${errMessage(err)}`);
150
317
  }
151
318
 
152
- if (parsed !== null && parsed !== undefined && typeof parsed === "object") {
153
- Object.assign(dst, parsed);
319
+ if (isPlainObject(parsed)) {
320
+ mergeInto(dst as Record<string, unknown>, parsed, policy, 0, "");
154
321
  }
155
322
  }
156
323
 
324
+ function parserFor(parse: Options["parse"], filePath: string): ConfigParser | undefined {
325
+ if (parse === undefined) return undefined;
326
+ if (typeof parse === "function") return parse;
327
+ const ext = path.extname(filePath).toLowerCase();
328
+ return Object.prototype.hasOwnProperty.call(parse, ext) ? parse[ext] : undefined;
329
+ }
330
+
331
+ function errMessage(err: unknown): string {
332
+ return err instanceof Error ? err.message : String(err);
333
+ }
334
+
335
+ /** {@link MergeOptions} validated, with defaults filled in. */
336
+ interface Policy {
337
+ keepLower: boolean;
338
+ maxDepth: number;
339
+ array: ArrayPolicy;
340
+ byPath: Map<string, ArrayPolicy | ArrayMergeFn>;
341
+ /** Wildcard patterns, best first: fewest `*`, then lexically smallest. */
342
+ wildcards: { pattern: string; segs: string[]; rule: ArrayPolicy | ArrayMergeFn }[];
343
+ }
344
+
345
+ const ARRAY_POLICIES: readonly string[] = ["replace", "concat", "union"];
346
+
347
+ function resolvePolicy(opts: MergeOptions): Policy {
348
+ const { nullPolicy = "clear", maxDepth = Infinity, arrayPolicy = "replace", arrayPolicyByPath } = opts;
349
+ if (nullPolicy !== "clear" && nullPolicy !== "keep-lower") {
350
+ throw new Error(`config: invalid nullPolicy ${JSON.stringify(nullPolicy)}`);
351
+ }
352
+ if (maxDepth !== Infinity && !(Number.isInteger(maxDepth) && maxDepth >= 0)) {
353
+ throw new Error(`config: invalid maxDepth ${String(maxDepth)}: want a non-negative integer or Infinity`);
354
+ }
355
+ if (!ARRAY_POLICIES.includes(arrayPolicy)) {
356
+ throw new Error(`config: invalid arrayPolicy ${JSON.stringify(arrayPolicy)}`);
357
+ }
358
+ const byPath = new Map<string, ArrayPolicy | ArrayMergeFn>();
359
+ const wildcards: Policy["wildcards"] = [];
360
+ for (const [p, v] of Object.entries(arrayPolicyByPath ?? {})) {
361
+ if (typeof v !== "function" && !ARRAY_POLICIES.includes(v)) {
362
+ throw new Error(`config: invalid arrayPolicyByPath[${JSON.stringify(p)}] ${JSON.stringify(v)}`);
363
+ }
364
+ const segs = p.split(".");
365
+ if (segs.includes("**")) {
366
+ throw new Error(`config: invalid arrayPolicyByPath[${JSON.stringify(p)}]: ** is not supported`);
367
+ }
368
+ byPath.set(p, v);
369
+ if (segs.includes("*")) wildcards.push({ pattern: p, segs, rule: v });
370
+ }
371
+ const stars = (segs: string[]): number => segs.filter((x) => x === "*").length;
372
+ wildcards.sort(
373
+ (a, b) => stars(a.segs) - stars(b.segs) || (a.pattern < b.pattern ? -1 : a.pattern > b.pattern ? 1 : 0),
374
+ );
375
+ return { keepLower: nullPolicy === "keep-lower", maxDepth, array: arrayPolicy, byPath, wildcards };
376
+ }
377
+
378
+ /**
379
+ * Collection nesting limit, counted through aliases. js-yaml already rejects
380
+ * text nested deeper than its `maxDepth` (default 100, not in its typings),
381
+ * but an alias chain can nest deeper than the text does, and merge/copy
382
+ * recurse once per level.
383
+ */
384
+ const MAX_DEPTH = 100;
385
+
386
+ /** yaml.v3's alias budget (decode.go): see {@link checkBounds}. */
387
+ const ALIAS_MIN_ALIASES = 100;
388
+ const ALIAS_MIN_DECODED = 1000;
389
+ const ALIAS_RATIO_LOW = 400_000;
390
+ const ALIAS_RATIO_HIGH = 4_000_000;
391
+
392
+ function allowedAliasRatio(decoded: number): number {
393
+ if (decoded <= ALIAS_RATIO_LOW) return 0.99;
394
+ if (decoded >= ALIAS_RATIO_HIGH) return 0.1;
395
+ return 0.99 - 0.89 * ((decoded - ALIAS_RATIO_LOW) / (ALIAS_RATIO_HIGH - ALIAS_RATIO_LOW));
396
+ }
397
+
398
+ /**
399
+ * Walks the parsed document the way merge and copy will, aliases expanded,
400
+ * and throws before that work can blow up.
401
+ *
402
+ * js-yaml returns an alias as a second reference to the anchored object, so
403
+ * copying it expands every reference: a "billion laughs" file of a few
404
+ * hundred bytes turns into ~10^9 nodes. js-yaml has no option to cap aliases,
405
+ * so this applies Go yaml.v3's budget: once more than 100 nodes were decoded
406
+ * inside alias expansions and more than 1000 in total, the share decoded
407
+ * inside aliases must stay at or below 0.99 (up to 400k nodes), falling
408
+ * linearly to 0.10 at 4M; otherwise `document contains excessive aliasing`.
409
+ * The first reference to an object is its anchor; later ones count as alias
410
+ * expansions. Scalars are counted but cannot be told apart from aliased
411
+ * scalars. Unlike Go, which skips keys its target struct ignores, every key
412
+ * counts. Iterative, so depth never touches the call stack.
413
+ */
414
+ function checkBounds(root: unknown): void {
415
+ const seen = new WeakSet<object>();
416
+ // [node, depth, inside an alias expansion]
417
+ const stack: [unknown, number, boolean][] = [[root, 1, false]];
418
+ let decoded = 0;
419
+ let aliased = 0;
420
+ while (stack.length > 0) {
421
+ const [node, depth, inAlias] = stack.pop() as [unknown, number, boolean];
422
+ let alias = inAlias;
423
+ const container = Array.isArray(node) || isPlainObject(node);
424
+ if (container) {
425
+ if (seen.has(node)) {
426
+ decoded++; // the alias node itself, as yaml.v3 counts it
427
+ alias = true;
428
+ } else {
429
+ seen.add(node);
430
+ }
431
+ }
432
+ decoded++;
433
+ if (alias) aliased++;
434
+ if (
435
+ aliased > ALIAS_MIN_ALIASES &&
436
+ decoded > ALIAS_MIN_DECODED &&
437
+ aliased / decoded > allowedAliasRatio(decoded)
438
+ ) {
439
+ throw new Error("document contains excessive aliasing");
440
+ }
441
+ if (!container) continue;
442
+ if (depth > MAX_DEPTH) {
443
+ throw new Error(`nesting exceeded maxDepth (${MAX_DEPTH})`);
444
+ }
445
+ if (Array.isArray(node)) {
446
+ for (let i = node.length - 1; i >= 0; i--) stack.push([node[i], depth + 1, alias]);
447
+ } else {
448
+ const keys = Object.keys(node);
449
+ for (let i = keys.length - 1; i >= 0; i--) {
450
+ decoded++; // the key node
451
+ if (alias) aliased++;
452
+ stack.push([(node as Record<string, unknown>)[keys[i]], depth + 1, alias]);
453
+ }
454
+ }
455
+ }
456
+ }
457
+
458
+ /**
459
+ * Merges `src` into `dst` in place. With the default policy: plain objects
460
+ * recurse, everything else replaces, and `null` clears only objects and
461
+ * arrays. `depth` counts object levels below the root; `at` is the dotted
462
+ * path of `dst`.
463
+ */
464
+ function mergeInto(
465
+ dst: Record<string, unknown>,
466
+ src: Record<string, unknown>,
467
+ policy: Policy,
468
+ depth: number,
469
+ at: string,
470
+ ): void {
471
+ for (const key of Object.keys(src)) {
472
+ const next = src[key];
473
+ const has = Object.prototype.hasOwnProperty.call(dst, key);
474
+ const cur = has ? dst[key] : undefined;
475
+ if (next === null) {
476
+ if (policy.keepLower) continue; // null never replaces, never adds
477
+ if (has && cur !== null && cur !== undefined && typeof cur !== "object") {
478
+ continue; // null leaves a scalar untouched
479
+ }
480
+ setOwn(dst, key, null);
481
+ } else if (isPlainObject(next) && isPlainObject(cur) && depth < policy.maxDepth) {
482
+ mergeInto(cur, next, policy, depth + 1, at === "" ? key : `${at}.${key}`);
483
+ } else if (Array.isArray(next) && Array.isArray(cur)) {
484
+ setOwn(dst, key, mergeArrays(cur, next, policy, at === "" ? key : `${at}.${key}`));
485
+ } else {
486
+ setOwn(dst, key, copy(next));
487
+ }
488
+ }
489
+ }
490
+
491
+ function mergeArrays(lower: unknown[], higher: unknown[], policy: Policy, at: string): unknown {
492
+ const rule = policy.byPath.get(at) ?? matchWildcard(policy, at) ?? policy.array;
493
+ const items = copy(higher) as unknown[];
494
+ if (typeof rule === "function") return rule(lower, items, at);
495
+ switch (rule) {
496
+ case "concat":
497
+ return [...lower, ...items];
498
+ case "union": {
499
+ const out: unknown[] = [];
500
+ for (const item of [...lower, ...items]) {
501
+ if (!out.some((o) => deepEqual(o, item))) out.push(item);
502
+ }
503
+ return out;
504
+ }
505
+ default:
506
+ return items;
507
+ }
508
+ }
509
+
510
+ /** The best wildcard pattern matching `at`; `*` stands for one whole segment. */
511
+ function matchWildcard(policy: Policy, at: string): ArrayPolicy | ArrayMergeFn | undefined {
512
+ if (policy.wildcards.length === 0) return undefined;
513
+ const segs = at.split(".");
514
+ const hit = policy.wildcards.find(
515
+ (w) => w.segs.length === segs.length && w.segs.every((x, i) => x === "*" || x === segs[i]),
516
+ );
517
+ return hit?.rule;
518
+ }
519
+
520
+ /**
521
+ * Structural equality for {@link MergeOptions.arrayPolicy} `"union"`: plain
522
+ * objects (key order ignored) and arrays compare by content, anything else
523
+ * by SameValueZero.
524
+ */
525
+ function deepEqual(a: unknown, b: unknown): boolean {
526
+ if (a === b || (a !== a && b !== b)) return true;
527
+ if (Array.isArray(a)) {
528
+ return Array.isArray(b) && a.length === b.length && a.every((v, i) => deepEqual(v, b[i]));
529
+ }
530
+ if (isPlainObject(a) && isPlainObject(b)) {
531
+ const ka = Object.keys(a);
532
+ if (ka.length !== Object.keys(b).length) return false;
533
+ return ka.every((k) => Object.prototype.hasOwnProperty.call(b, k) && deepEqual(a[k], b[k]));
534
+ }
535
+ return false;
536
+ }
537
+
538
+ /** Own data property even for keys like `__proto__`, never a setter call. */
539
+ function setOwn(dst: Record<string, unknown>, key: string, value: unknown): void {
540
+ Object.defineProperty(dst, key, {
541
+ value,
542
+ writable: true,
543
+ enumerable: true,
544
+ configurable: true,
545
+ });
546
+ }
547
+
548
+ /**
549
+ * Copies plain objects and arrays so YAML aliases do not share one instance
550
+ * in `dst` (a later merge into one key would otherwise leak into another).
551
+ * Memoizing per source object would bring that sharing back, so each
552
+ * reference is copied; {@link checkBounds} has already capped the total.
553
+ */
554
+ function copy(v: unknown): unknown {
555
+ if (Array.isArray(v)) return v.map(copy);
556
+ if (isPlainObject(v)) {
557
+ const out: Record<string, unknown> = {};
558
+ for (const k of Object.keys(v)) setOwn(out, k, copy(v[k]));
559
+ return out;
560
+ }
561
+ return v;
562
+ }
563
+
564
+ function isPlainObject(v: unknown): v is Record<string, unknown> {
565
+ if (v === null || typeof v !== "object" || Array.isArray(v)) return false;
566
+ const proto: unknown = Object.getPrototypeOf(v);
567
+ return proto === Object.prototype || proto === null;
568
+ }
569
+
157
570
  function isEnoent(err: unknown): boolean {
158
571
  return (
159
572
  typeof err === "object" &&
@@ -1,4 +1,4 @@
1
- // @generated by protoc-gen-es v2.11.0 with parameter "target=ts"
1
+ // @generated by protoc-gen-es v2.15.0 with parameter "target=ts"
2
2
  // @generated from file crud.proto (package crud.v1, syntax proto3)
3
3
  /* eslint-disable */
4
4
 
@@ -11,7 +11,7 @@ import type { JsonObject, Message } from "@bufbuild/protobuf";
11
11
  * Describes the file crud.proto.
12
12
  */
13
13
  export const file_crud: GenFile = /*@__PURE__*/
14
- fileDesc("CgpjcnVkLnByb3RvEgdjcnVkLnYxIjgKDUNyZWF0ZVJlcXVlc3QSJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCI5Cg5DcmVhdGVSZXNwb25zZRInCgZlbnRpdHkYASABKAsyFy5nb29nbGUucHJvdG9idWYuU3RydWN0IhgKCkdldFJlcXVlc3QSCgoCaWQYASABKAkiNgoLR2V0UmVzcG9uc2USJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCJKCgtMaXN0UmVxdWVzdBINCgVsaW1pdBgBIAEoBRIOCgZvZmZzZXQYAiABKAUSDAoEc29ydBgDIAEoCRIOCgZzZWFyY2gYBCABKAkiOQoMTGlzdFJlc3BvbnNlEikKCGVudGl0aWVzGAEgAygLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCI4Cg1VcGRhdGVSZXF1ZXN0EicKBmVudGl0eRgBIAEoCzIXLmdvb2dsZS5wcm90b2J1Zi5TdHJ1Y3QiOQoOVXBkYXRlUmVzcG9uc2USJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCIbCg1EZWxldGVSZXF1ZXN0EgoKAmlkGAEgASgJIhAKDkRlbGV0ZVJlc3BvbnNlMqcCCg1FbnRpdHlTZXJ2aWNlEjkKBkNyZWF0ZRIWLmNydWQudjEuQ3JlYXRlUmVxdWVzdBoXLmNydWQudjEuQ3JlYXRlUmVzcG9uc2USMAoDR2V0EhMuY3J1ZC52MS5HZXRSZXF1ZXN0GhQuY3J1ZC52MS5HZXRSZXNwb25zZRIzCgRMaXN0EhQuY3J1ZC52MS5MaXN0UmVxdWVzdBoVLmNydWQudjEuTGlzdFJlc3BvbnNlEjkKBlVwZGF0ZRIWLmNydWQudjEuVXBkYXRlUmVxdWVzdBoXLmNydWQudjEuVXBkYXRlUmVzcG9uc2USOQoGRGVsZXRlEhYuY3J1ZC52MS5EZWxldGVSZXF1ZXN0GhcuY3J1ZC52MS5EZWxldGVSZXNwb25zZUJ3Cgtjb20uY3J1ZC52MUIJQ3J1ZFByb3RvUAFaIGhvcC50b3Ava2l0L3Byb3RvL2NydWQvdjE7Y3J1ZHYxogIDQ1hYqgIHQ3J1ZC5WMcoCB0NydWRcVjHiAhNDcnVkXFYxXEdQQk1ldGFkYXRh6gIIQ3J1ZDo6VjFiBnByb3RvMw", [file_google_protobuf_struct]);
14
+ fileDesc("CgpjcnVkLnByb3RvEgdjcnVkLnYxIjgKDUNyZWF0ZVJlcXVlc3QSJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCI5Cg5DcmVhdGVSZXNwb25zZRInCgZlbnRpdHkYASABKAsyFy5nb29nbGUucHJvdG9idWYuU3RydWN0IhgKCkdldFJlcXVlc3QSCgoCaWQYASABKAkiNgoLR2V0UmVzcG9uc2USJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCJKCgtMaXN0UmVxdWVzdBINCgVsaW1pdBgBIAEoBRIOCgZvZmZzZXQYAiABKAUSDAoEc29ydBgDIAEoCRIOCgZzZWFyY2gYBCABKAkiOQoMTGlzdFJlc3BvbnNlEikKCGVudGl0aWVzGAEgAygLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCI4Cg1VcGRhdGVSZXF1ZXN0EicKBmVudGl0eRgBIAEoCzIXLmdvb2dsZS5wcm90b2J1Zi5TdHJ1Y3QiOQoOVXBkYXRlUmVzcG9uc2USJwoGZW50aXR5GAEgASgLMhcuZ29vZ2xlLnByb3RvYnVmLlN0cnVjdCIbCg1EZWxldGVSZXF1ZXN0EgoKAmlkGAEgASgJIhAKDkRlbGV0ZVJlc3BvbnNlMqcCCg1FbnRpdHlTZXJ2aWNlEjkKBkNyZWF0ZRIWLmNydWQudjEuQ3JlYXRlUmVxdWVzdBoXLmNydWQudjEuQ3JlYXRlUmVzcG9uc2USMAoDR2V0EhMuY3J1ZC52MS5HZXRSZXF1ZXN0GhQuY3J1ZC52MS5HZXRSZXNwb25zZRIzCgRMaXN0EhQuY3J1ZC52MS5MaXN0UmVxdWVzdBoVLmNydWQudjEuTGlzdFJlc3BvbnNlEjkKBlVwZGF0ZRIWLmNydWQudjEuVXBkYXRlUmVxdWVzdBoXLmNydWQudjEuVXBkYXRlUmVzcG9uc2USOQoGRGVsZXRlEhYuY3J1ZC52MS5EZWxldGVSZXF1ZXN0GhcuY3J1ZC52MS5EZWxldGVSZXNwb25zZUKBAQoLY29tLmNydWQudjFCCUNydWRQcm90b1ABWipob3AudG9wL2tpdC9jb250cmFjdHMvcHJvdG8vY3J1ZC92MTtjcnVkdjGiAgNDWFiqAgdDcnVkLlYxygIHQ3J1ZFxWMeICE0NydWRcVjFcR1BCTWV0YWRhdGHqAghDcnVkOjpWMWIGcHJvdG8z", [file_google_protobuf_struct]);
15
15
 
16
16
  /**
17
17
  * @generated from message crud.v1.CreateRequest
@@ -22,7 +22,7 @@ export type CreateRequest = Message<"crud.v1.CreateRequest"> & {
22
22
  *
23
23
  * @generated from field: google.protobuf.Struct entity = 1;
24
24
  */
25
- entity?: JsonObject;
25
+ entity?: JsonObject | undefined;
26
26
  };
27
27
 
28
28
  /**
@@ -41,7 +41,7 @@ export type CreateResponse = Message<"crud.v1.CreateResponse"> & {
41
41
  *
42
42
  * @generated from field: google.protobuf.Struct entity = 1;
43
43
  */
44
- entity?: JsonObject;
44
+ entity?: JsonObject | undefined;
45
45
  };
46
46
 
47
47
  /**
@@ -75,7 +75,7 @@ export type GetResponse = Message<"crud.v1.GetResponse"> & {
75
75
  /**
76
76
  * @generated from field: google.protobuf.Struct entity = 1;
77
77
  */
78
- entity?: JsonObject;
78
+ entity?: JsonObject | undefined;
79
79
  };
80
80
 
81
81
  /**
@@ -149,7 +149,7 @@ export type UpdateRequest = Message<"crud.v1.UpdateRequest"> & {
149
149
  /**
150
150
  * @generated from field: google.protobuf.Struct entity = 1;
151
151
  */
152
- entity?: JsonObject;
152
+ entity?: JsonObject | undefined;
153
153
  };
154
154
 
155
155
  /**
@@ -166,7 +166,7 @@ export type UpdateResponse = Message<"crud.v1.UpdateResponse"> & {
166
166
  /**
167
167
  * @generated from field: google.protobuf.Struct entity = 1;
168
168
  */
169
- entity?: JsonObject;
169
+ entity?: JsonObject | undefined;
170
170
  };
171
171
 
172
172
  /**
@@ -0,0 +1,38 @@
1
+ # id
2
+
3
+ ## What it answers
4
+
5
+ How to mint and parse kit entity IDs so that Go, Python, Rust and PHP produce the same string for the same `(prefix, uuid)`. Canonical wire form: `<prefix>_<26-char-base32>`, a Crockford-base32 UUIDv7 behind a lowercase prefix. Wrong module for URI composition such as `tlc://task/task_...`: use `@hop-top/cite`.
6
+
7
+ ## Use it when
8
+
9
+ - new entity: `newId('task')`
10
+ - validate or split an incoming id: `parse(s)` returns `{ prefix, uuid }` and throws on malformed input
11
+ - compile-time prefix safety: `Typed<'task'>` with `newTyped` / `parseTyped`
12
+
13
+ ## Quick start
14
+
15
+ ```ts
16
+ import { newId, parse } from '@hop-top/kit/id';
17
+
18
+ const id = newId('task');
19
+ console.log(id, parse(id).uuid);
20
+ ```
21
+
22
+ ## Contract
23
+
24
+ - Thin wrapper over `typeid-js`; the string returned by `newId` is the JSON wire form and round-trips losslessly through `parse`.
25
+ - Prefix grammar and length limit follow the typeid spec pinned in the fixture; a divergence is a wire-format break.
26
+ - No transitive dependency on the URI registry, by design.
27
+ - Parity: [`contracts/typeid-v1/fixtures.json`](../../../../contracts/typeid-v1/fixtures.json), replayed by [`contract.test.ts`](contract.test.ts).
28
+
29
+ ## Neighbours
30
+
31
+ - `@hop-top/cite`: URI composition on top of the canonical string
32
+ - `@hop-top/kit/sqlstore`: persistence keyed by these ids
33
+ - Go reference: `go/core/id`; Python port: [`hop_top_kit/id`](../../../py/hop_top_kit/id/README.md)
34
+
35
+ ## See also
36
+
37
+ - [`docs/announcements/2026-05-typeid-primitive.md`](../../../../docs/announcements/2026-05-typeid-primitive.md)
38
+ - [`docs/adopters/reference/go-primitives.md`](../../../../docs/adopters/reference/go-primitives.md)
@@ -1,4 +1,4 @@
1
- // Cross-language parity contract test (tlc T-0753).
1
+ // Cross-language parity contract test.
2
2
  //
3
3
  // Loads contracts/typeid-v1/fixtures.json from the repo root and
4
4
  // asserts that this TS SDK's encode (via typeid-js's TypeID.fromUUID)
@@ -10,7 +10,7 @@ import {
10
10
  } from './index';
11
11
 
12
12
  // Canonical fixture set — these EXACT uuid inputs are shared with the
13
- // other language SDKs so future cross-language parity work (T-0753)
13
+ // other language SDKs so future cross-language parity work
14
14
  // finds matching outputs. Do not mutate without coordinating across
15
15
  // sdk/{go,rs,py,php}.
16
16
  const FIXTURES: ReadonlyArray<{ prefix: string; uuid: string }> = [
package/src/id/index.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  * @package @hop-top/kit
4
4
  *
5
5
  * TypeID primitive — thin wrapper around `typeid-js` (jetify-com)
6
- * implementing the cross-language kit API SHAPE per ADR 0001.
6
+ * implementing the cross-language kit API SHAPE per
7
+ * docs/announcements/2026-05-typeid-primitive.md.
7
8
  *
8
9
  * Canonical wire form: `<prefix>_<26-char-base32>` where the suffix is
9
10
  * a Crockford-base32-encoded UUIDv7. The TS surface adds a
@@ -13,7 +14,7 @@
13
14
  * URI composition (e.g. `tlc://task/task_…`) is NOT in this module —
14
15
  * callers should use the `@hop-top/cite` package directly. This
15
16
  * keeps `kit/id` free of any transitive dependency on the URI
16
- * registry, per the ADR's scope boundary.
17
+ * registry.
17
18
  */
18
19
 
19
20
  import { typeid, TypeID } from 'typeid-js';