aontu 0.53.0 → 0.55.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 (197) hide show
  1. package/dist/aontu.d.ts +3 -2
  2. package/dist/aontu.js +36 -7
  3. package/dist/aontu.js.map +1 -1
  4. package/dist/cli.d.ts +2 -1
  5. package/dist/cli.js +500 -8
  6. package/dist/cli.js.map +1 -1
  7. package/dist/ctx.d.ts +5 -0
  8. package/dist/ctx.js +1 -0
  9. package/dist/ctx.js.map +1 -1
  10. package/dist/diff.js.map +1 -1
  11. package/dist/err.js +7 -1
  12. package/dist/err.js.map +1 -1
  13. package/dist/graph.d.ts +2 -5
  14. package/dist/graph.js +83 -46
  15. package/dist/graph.js.map +1 -1
  16. package/dist/hcanon.js +9 -10
  17. package/dist/hcanon.js.map +1 -1
  18. package/dist/hints.js +94 -26
  19. package/dist/hints.js.map +1 -1
  20. package/dist/jsonschema.js +34 -0
  21. package/dist/jsonschema.js.map +1 -1
  22. package/dist/lang.js +593 -191
  23. package/dist/lang.js.map +1 -1
  24. package/dist/lsp.d.ts +1 -1
  25. package/dist/lsp.js +86 -6
  26. package/dist/lsp.js.map +1 -1
  27. package/dist/mcp.js +153 -6
  28. package/dist/mcp.js.map +1 -1
  29. package/dist/mod-tool.js +42 -8
  30. package/dist/mod-tool.js.map +1 -1
  31. package/dist/mod.d.ts +4 -0
  32. package/dist/mod.js +97 -2
  33. package/dist/mod.js.map +1 -1
  34. package/dist/patch.d.ts +5 -0
  35. package/dist/patch.js +25 -25
  36. package/dist/patch.js.map +1 -1
  37. package/dist/provenance.d.ts +1 -0
  38. package/dist/provenance.js +2 -1
  39. package/dist/provenance.js.map +1 -1
  40. package/dist/query.js.map +1 -1
  41. package/dist/reach.d.ts +1 -0
  42. package/dist/reach.js +49 -21
  43. package/dist/reach.js.map +1 -1
  44. package/dist/relation.d.ts +4 -0
  45. package/dist/relation.js +125 -200
  46. package/dist/relation.js.map +1 -1
  47. package/dist/sig.d.ts +25 -0
  48. package/dist/sig.js +277 -0
  49. package/dist/sig.js.map +1 -0
  50. package/dist/sigdecl.d.ts +2 -0
  51. package/dist/sigdecl.js +11 -0
  52. package/dist/sigdecl.js.map +1 -0
  53. package/dist/siggate.d.ts +4 -0
  54. package/dist/siggate.js +90 -0
  55. package/dist/siggate.js.map +1 -0
  56. package/dist/std.js +75 -16
  57. package/dist/std.js.map +1 -1
  58. package/dist/subsume.js +57 -10
  59. package/dist/subsume.js.map +1 -1
  60. package/dist/tsconfig.tsbuildinfo +1 -1
  61. package/dist/unify.d.ts +2 -2
  62. package/dist/unify.js +93 -114
  63. package/dist/unify.js.map +1 -1
  64. package/dist/utility.d.ts +1 -2
  65. package/dist/utility.js +7 -61
  66. package/dist/utility.js.map +1 -1
  67. package/dist/val/AggFuncVal.d.ts +12 -1
  68. package/dist/val/AggFuncVal.js +165 -3
  69. package/dist/val/AggFuncVal.js.map +1 -1
  70. package/dist/val/BagVal.d.ts +2 -0
  71. package/dist/val/BagVal.js +56 -8
  72. package/dist/val/BagVal.js.map +1 -1
  73. package/dist/val/ConstraintVal.js +33 -5
  74. package/dist/val/ConstraintVal.js.map +1 -1
  75. package/dist/val/ContainerKindVal.d.ts +35 -0
  76. package/dist/val/ContainerKindVal.js +99 -0
  77. package/dist/val/ContainerKindVal.js.map +1 -0
  78. package/dist/val/CopyFuncVal.js +0 -7
  79. package/dist/val/CopyFuncVal.js.map +1 -1
  80. package/dist/val/DisjunctVal.d.ts +1 -2
  81. package/dist/val/DisjunctVal.js +146 -39
  82. package/dist/val/DisjunctVal.js.map +1 -1
  83. package/dist/val/ExpectVal.js +37 -2
  84. package/dist/val/ExpectVal.js.map +1 -1
  85. package/dist/val/FuncBaseVal.d.ts +1 -0
  86. package/dist/val/FuncBaseVal.js +19 -8
  87. package/dist/val/FuncBaseVal.js.map +1 -1
  88. package/dist/val/GraphAtomVal.d.ts +39 -0
  89. package/dist/val/GraphAtomVal.js +184 -0
  90. package/dist/val/GraphAtomVal.js.map +1 -0
  91. package/dist/val/JunctionVal.js +22 -5
  92. package/dist/val/JunctionVal.js.map +1 -1
  93. package/dist/val/ListVal.js +27 -34
  94. package/dist/val/ListVal.js.map +1 -1
  95. package/dist/val/MapVal.d.ts +1 -0
  96. package/dist/val/MapVal.js +82 -35
  97. package/dist/val/MapVal.js.map +1 -1
  98. package/dist/val/PathFuncVal.d.ts +2 -2
  99. package/dist/val/PathFuncVal.js +75 -16
  100. package/dist/val/PathFuncVal.js.map +1 -1
  101. package/dist/val/PathVal.d.ts +25 -0
  102. package/dist/val/PathVal.js +150 -0
  103. package/dist/val/PathVal.js.map +1 -0
  104. package/dist/val/PlusOpVal.d.ts +2 -1
  105. package/dist/val/PlusOpVal.js +50 -35
  106. package/dist/val/PlusOpVal.js.map +1 -1
  107. package/dist/val/PrefVal.d.ts +2 -0
  108. package/dist/val/PrefVal.js +159 -32
  109. package/dist/val/PrefVal.js.map +1 -1
  110. package/dist/val/RecurseVal.d.ts +19 -0
  111. package/dist/val/RecurseVal.js +217 -0
  112. package/dist/val/RecurseVal.js.map +1 -0
  113. package/dist/val/RefVal.d.ts +2 -1
  114. package/dist/val/RefVal.js +105 -72
  115. package/dist/val/RefVal.js.map +1 -1
  116. package/dist/val/ReferFuncVal.d.ts +30 -7
  117. package/dist/val/ReferFuncVal.js +395 -94
  118. package/dist/val/ReferFuncVal.js.map +1 -1
  119. package/dist/val/ScalarKindVal.d.ts +4 -2
  120. package/dist/val/ScalarKindVal.js +12 -1
  121. package/dist/val/ScalarKindVal.js.map +1 -1
  122. package/dist/val/SuperFuncVal.d.ts +4 -2
  123. package/dist/val/SuperFuncVal.js +118 -14
  124. package/dist/val/SuperFuncVal.js.map +1 -1
  125. package/dist/val/TopVal.d.ts +1 -1
  126. package/dist/val/Val.d.ts +2 -3
  127. package/dist/val/Val.js +39 -20
  128. package/dist/val/Val.js.map +1 -1
  129. package/dist/val/arith.js +4 -1
  130. package/dist/val/arith.js.map +1 -1
  131. package/dist/vet.d.ts +1 -0
  132. package/dist/vet.js +32 -1
  133. package/dist/vet.js.map +1 -1
  134. package/dist/view.d.ts +90 -0
  135. package/dist/view.js +2168 -0
  136. package/dist/view.js.map +1 -0
  137. package/grammar/aontu.gbnf +18 -10
  138. package/grammar/aontu.lark +15 -10
  139. package/grammar/aontu.tmLanguage.json +184 -0
  140. package/package.json +10 -3
  141. package/skill/grammar-card.md +1 -2
  142. package/src/aontu.ts +37 -7
  143. package/src/cli.ts +553 -8
  144. package/src/ctx.ts +20 -0
  145. package/src/diff.ts +4 -2
  146. package/src/err.ts +8 -1
  147. package/src/graph.ts +125 -75
  148. package/src/hcanon.ts +9 -11
  149. package/src/hints.ts +112 -29
  150. package/src/jsonschema.ts +41 -0
  151. package/src/lang.ts +642 -203
  152. package/src/lsp.ts +75 -6
  153. package/src/mcp.ts +164 -6
  154. package/src/mod-tool.ts +48 -9
  155. package/src/mod.ts +110 -1
  156. package/src/patch.ts +31 -27
  157. package/src/provenance.ts +8 -1
  158. package/src/query.ts +4 -2
  159. package/src/reach.ts +52 -23
  160. package/src/relation.ts +139 -236
  161. package/src/sig.ts +345 -0
  162. package/src/sigdecl.ts +11 -0
  163. package/src/siggate.ts +144 -0
  164. package/src/std.ts +77 -16
  165. package/src/subsume.ts +59 -10
  166. package/src/unify.ts +102 -125
  167. package/src/utility.ts +7 -67
  168. package/src/val/AggFuncVal.ts +231 -4
  169. package/src/val/BagVal.ts +58 -9
  170. package/src/val/ConstraintVal.ts +34 -5
  171. package/src/val/ContainerKindVal.ts +158 -0
  172. package/src/val/CopyFuncVal.ts +0 -7
  173. package/src/val/DisjunctVal.ts +152 -40
  174. package/src/val/ExpectVal.ts +39 -4
  175. package/src/val/FuncBaseVal.ts +21 -8
  176. package/src/val/GraphAtomVal.ts +264 -0
  177. package/src/val/JunctionVal.ts +23 -6
  178. package/src/val/ListVal.ts +30 -37
  179. package/src/val/MapVal.ts +87 -39
  180. package/src/val/PathFuncVal.ts +107 -19
  181. package/src/val/PathVal.ts +221 -0
  182. package/src/val/PlusOpVal.ts +56 -37
  183. package/src/val/PrefVal.ts +186 -38
  184. package/src/val/RecurseVal.ts +285 -0
  185. package/src/val/RefVal.ts +105 -83
  186. package/src/val/ReferFuncVal.ts +445 -100
  187. package/src/val/ScalarKindVal.ts +12 -0
  188. package/src/val/SuperFuncVal.ts +137 -13
  189. package/src/val/TopVal.ts +1 -1
  190. package/src/val/Val.ts +44 -35
  191. package/src/val/arith.ts +4 -1
  192. package/src/vet.ts +41 -4
  193. package/src/view.ts +2882 -0
  194. package/dist/val/IdFuncVal.d.ts +0 -13
  195. package/dist/val/IdFuncVal.js +0 -54
  196. package/dist/val/IdFuncVal.js.map +0 -1
  197. package/src/val/IdFuncVal.ts +0 -91
@@ -36,61 +36,59 @@ import {
36
36
  import { makeNilErr } from '../err'
37
37
 
38
38
  import { FuncBaseVal } from './FuncBaseVal'
39
+ import {
40
+ Address,
41
+ PathVal,
42
+ parseAddress,
43
+ prefixMeet,
44
+ } from './PathVal'
39
45
  import { FeatureVal } from './FeatureVal'
40
- import { StringVal } from './StringVal'
46
+ import { ConjunctVal } from './ConjunctVal'
41
47
  import { unite } from '../unify'
42
48
  import { top } from './top'
43
49
  import { propagateMarks, walk } from '../utility'
44
50
 
45
51
 
46
- // A segment of the path INSIDE an entity. The entity name's own
47
- // grammar (no dots) is what makes the split unambiguous: everything
48
- // before the first dot names the entity, everything after walks its
49
- // value.
50
- const ADDR_SEGMENT = /^[A-Za-z0-9_-]+$/
51
- const ADDR_NAME = /^[A-Za-z0-9_/-]+$/
52
-
53
-
54
- export type Address = {
55
- name: string
56
- path: string[]
57
- }
52
+ // A RELATION PREDICATE is still a declared D-1 name
53
+ // (docs/design/RELATIONS.0.md §3.2). Entity names are gone with
54
+ // ADR-014; predicate names are not -- a relation is a vocabulary term,
55
+ // not an address.
56
+ const PREDICATE_NAME = /^[_a-zA-Z][-_a-zA-Z0-9]*$/
58
57
 
59
58
 
60
- // The address a string spells, or undefined when it does not spell
61
- // one. `svc/auth` is the entity; `svc/auth.ports.http` is a node
62
- // inside it — the two addressing schemes reconciled: `$.a.b` answers
63
- // WHERE, an address answers WHAT, and beneath entity granularity the
64
- // tree is authoritative again.
65
- export function parseAddress(s: string): Address | undefined {
66
- const parts = s.split('.')
67
- if (!ADDR_NAME.test(parts[0])) {
68
- return undefined
59
+ // The tree path an address resolves to from `at` -- the position of
60
+ // the link itself -- or undefined when a relative address climbs off
61
+ // the top of the tree.
62
+ export function addressPath(
63
+ addr: Address, at: (string | number)[]
64
+ ): string[] | undefined {
65
+ if (addr.absolute) {
66
+ return addr.parts
69
67
  }
70
- for (const seg of parts.slice(1)) {
71
- if (!ADDR_SEGMENT.test(seg)) {
72
- return undefined
73
- }
68
+ // The SIBLING scope: drop the link's own key, then take the parent
69
+ // steps. A link at `$.a.b.dep` spelling `.other` means `$.a.b.other`.
70
+ const cut = at.length - 1 - addr.up
71
+ if (cut < 0) {
72
+ return undefined
74
73
  }
75
- return { name: parts[0], path: parts.slice(1) }
74
+ return at.slice(0, cut).map(String).concat(addr.parts)
76
75
  }
77
76
 
78
77
 
79
- // The value an address names, or undefined when the evaluation does
80
- // not (yet) have one. Pending is not failure: an entity may be
81
- // declared by a later conjunct, include or spread, so `refer`
78
+ // The node a tree path names, with the parent and key that hold it so
79
+ // a type flow can be written back. Pending is not failure: the target
80
+ // may be introduced by a later conjunct, include or spread, so `refer`
82
81
  // residuates exactly as a forward reference does.
83
- export function findEntity(
84
- reg: Map<string, Val> | undefined, addr: Address
82
+ export function findAt(
83
+ root: Val | undefined, path: string[]
85
84
  ): { parent?: any, key?: string, val: Val } | undefined {
86
- const rep: any = reg?.get(addr.name)
87
- if (null == rep) {
85
+ if (null == root || 0 === path.length) {
88
86
  return undefined
89
87
  }
90
88
  let parent: any = undefined
91
89
  let key: string | undefined = undefined
92
- let val: any = rep
93
- for (const seg of addr.path) {
90
+ let val: any = root
91
+ for (const seg of path) {
94
92
  if (true !== val?.isMap && true !== val?.isList) {
95
93
  return undefined
96
94
  }
@@ -141,6 +139,12 @@ function concreteFlow(ctx: AontuContext, t: Val): Val {
141
139
  class ReferVal extends FeatureVal {
142
140
  isRefer = true
143
141
  isGenable = true
142
+ // AFTER the plain values (base 99999), BEFORE the sizing atoms
143
+ // (LATE_CJO): sibling path values fold together first under the
144
+ // prefix rule, and the residual then meets ONE merged address --
145
+ // without this, `refer() & path($.a) & path($.a.b)` settled on the
146
+ // first path and the second met the finished link too late.
147
+ cjo = 120000
144
148
 
145
149
  // The type to flow into the target; TOP when `refer()` was written
146
150
  // with no argument.
@@ -152,6 +156,15 @@ class ReferVal extends FeatureVal {
152
156
  // Constraints met while the address was still pending — a kind, a
153
157
  // regex, a preference. They meet the LINK once there is one.
154
158
  held?: Val
159
+ // The PREDICATE the produced link belongs to, when this residual was
160
+ // minted by `rel()` on a field: the field's key, stamped onto the
161
+ // link so the graph reports the edge under a DECLARED relation
162
+ // rather than inferring one from the path (RELATIONS.0.md §3.2).
163
+ relkey?: string
164
+ // The codes this residual refuses with: refer() keeps its own,
165
+ // rel()-minted residuals carry rel_address/rel_unresolved.
166
+ addrcode: string = 'refer_address'
167
+ unresolvedcode: string = 'refer_unresolved'
155
168
 
156
169
  constructor(spec: ValSpec, ctx?: AontuContext) {
157
170
  super(spec, ctx)
@@ -166,7 +179,7 @@ class ReferVal extends FeatureVal {
166
179
  // met, the constraints it holds — TRAVELS with the clone. A spread
167
180
  // template holds the FUNCTION, so a template never needs this; a
168
181
  // REFERENCE to a value that already contains a resolved link does
169
- // (`z: id(a) & {u: refer() & "a"}` then `s: $.z`). Without it the
182
+ // (`z: {u: refer() & "$.a"}` then `s: $.z`). Without it the
170
183
  // clone came back as a bare `refer()` — the address silently
171
184
  // dropped, and the copied link resolving to nothing.
172
185
  //
@@ -178,6 +191,9 @@ class ReferVal extends FeatureVal {
178
191
  out.addr = this.addr
179
192
  out.addrsrc = this.addrsrc
180
193
  out.held = this.held
194
+ out.relkey = this.relkey
195
+ out.addrcode = this.addrcode
196
+ out.unresolvedcode = this.unresolvedcode
181
197
  return out
182
198
  }
183
199
 
@@ -205,34 +221,47 @@ class ReferVal extends FeatureVal {
205
221
  return peer
206
222
  }
207
223
 
208
- // A STRING is the ADDRESS, when there is not one yet. It is the
209
- // only thing that can be: a link's value is its address.
210
- if (undefined === this.addr
211
- && true === p.isScalar && 'string' === typeof p.peg) {
212
- const addr = parseAddress(p.peg)
213
- if (undefined === addr) {
214
- return makeNilErr(ctx, 'refer_address', this, peer, 'refer',
215
- { addr: p.peg })
216
- }
224
+ // A PATH VALUE is the ADDRESS, when there is not one yet. Only a
225
+ // path value can be one (PATHS.0.md, amended): a bare string is
226
+ // never a path -- `path("...")` is the one string conversion, and
227
+ // it happens at the call, not here. The peg is pre-validated by
228
+ // the capture, so no parse can fail.
229
+ if (undefined === this.addr && true === p.isPath) {
230
+ const addr = parseAddress(p.peg) as Address
217
231
  return this.with(ctx, { addr, addrsrc: p.peg }, peer)
218
232
  }
219
233
 
220
- // A value that can never BE a string cannot constrain one either,
221
- // and no later pass can repair it — so this arm refuses rather
222
- // than defers. A KIND or a constraint is not in it: `string`,
223
- // `re("^svc/")` and the like are perfectly good constraints on an
224
- // address, and are held below until there is one to apply them to.
225
- if ((true === p.isScalar && 'string' !== typeof p.peg)
234
+ // A SECOND path peer refines the address by the prefix rule: the
235
+ // longer of the two when one opens the other, exactly as two path
236
+ // values meet on their own. Two incomparable addresses are the
237
+ // same conflict two unequal scalars are.
238
+ if (undefined !== this.addr && true === p.isPath) {
239
+ const merged = prefixMeet(this.addrsrc as string, p.peg)
240
+ if (undefined === merged) {
241
+ return makeNilErr(ctx, 'scalar_value', this, peer)
242
+ }
243
+ return this.with(ctx,
244
+ { addr: parseAddress(merged) as Address, addrsrc: merged }, peer)
245
+ }
246
+
247
+ // A value that can never BE a path cannot be the address, and no
248
+ // later pass can repair it — so this arm refuses rather than
249
+ // defers. A bare STRING is in it now (PATHS.0.md, amended):
250
+ // `path("...")` is the one conversion, at the call. A KIND or a
251
+ // constraint is not in it: `string`, `re("^svc_")` and the like
252
+ // are perfectly good constraints on an address, and are held
253
+ // below until there is one to apply them to.
254
+ if ((true === p.isScalar && true !== p.isPath)
226
255
  || true === p.isMap || true === p.isList) {
227
- return makeNilErr(ctx, 'refer_address', this, peer, 'refer')
256
+ return makeNilErr(ctx, this.addrcode, this, peer, 'refer')
228
257
  }
229
258
 
230
259
  // HELD: everything else waits for the address. Carried on the
231
260
  // residual rather than parked in a conjunct, because a conjunct
232
261
  // rebuilt every pass grows a level every pass; the held constraint
233
262
  // meets the link the moment the address resolves, so
234
- // `refer() & "x" & "y"` still conflicts and `refer() & string & "x"`
235
- // still passes.
263
+ // `refer() & re("a") & re("b") & path($.z)` still applies both and
264
+ // `refer() & string & path($.z)` still passes.
236
265
  return this.with(ctx, {
237
266
  held: null == this.held ? peer : unite(ctx, this.held, peer, 'refer-held'),
238
267
  }, this)
@@ -247,6 +276,9 @@ class ReferVal extends FeatureVal {
247
276
  out.addr = spec.addr ?? this.addr
248
277
  out.addrsrc = spec.addrsrc ?? this.addrsrc
249
278
  out.held = spec.held ?? this.held
279
+ out.relkey = this.relkey
280
+ out.addrcode = this.addrcode
281
+ out.unresolvedcode = this.unresolvedcode
250
282
  propagateMarks(this, out)
251
283
  out.site = site.site
252
284
  out.path = this.path
@@ -267,79 +299,141 @@ class ReferVal extends FeatureVal {
267
299
  this.dc = 0
268
300
  return this
269
301
  }
270
- const reg: Map<string, Val> | undefined = (ctx as any)?.entities
271
- const found = findEntity(reg, this.addr)
302
+ // The address is a TREE PATH, resolved from the link's own
303
+ // position for a relative one. A climb off the top of the tree can
304
+ // never be repaired by a later pass, so it refuses at once.
305
+ const target = addressPath(this.addr, this.path)
306
+ if (undefined === target) {
307
+ return makeNilErr(ctx, this.unresolvedcode, this, undefined, 'refer',
308
+ { addr: this.addrsrc as string })
309
+ }
310
+ const found = findAt(ctx?.root, target)
272
311
  if (undefined === found) {
273
- // PENDING, not failed — until the last pass. An entity may be
274
- // declared by a later conjunct, include or spread, so `refer`
312
+ // PENDING, not failed — until the last pass. The target may be
313
+ // introduced by a later conjunct, include or spread, so `refer`
275
314
  // residuates as a forward reference does; but within ONE
276
315
  // evaluation the document-set is fixed, so existence IS
277
316
  // decidable, and the final pass is where it is decided. A
278
317
  // pending refer keeps the tree not-done, so the pass loop always
279
318
  // reaches that pass when there is one to decide.
280
319
  if (ctx.cc + 1 >= ctx.budget.passes) {
281
- return makeNilErr(ctx, 'refer_unresolved', this, undefined, 'refer',
320
+ return makeNilErr(ctx, this.unresolvedcode, this, undefined, 'refer',
282
321
  { addr: this.addrsrc as string })
283
322
  }
284
323
  this.dc = 0
285
324
  return this
286
325
  }
287
326
 
288
- // THE FLOW. `t` is unified into the target and written back, so
289
- // every position of the entity carries it after the pass's
290
- // identity merge — the same channel the merge itself uses.
327
+ // THE FLOW. `t` is unified into the target and written back at the
328
+ // target's own position — one position, because the tree is the
329
+ // namespace and a path names exactly one node.
291
330
  //
292
- // RE-ENTRANT ONLY ONCE PER ENTITY (use-cases/BUGS.md §19). Uniting
331
+ // RE-ENTRANT ONLY ONCE PER TARGET (use-cases/BUGS.md §19). Uniting
293
332
  // the target drives the target's OWN subtree, and if the target
294
333
  // links back — `a` typed-refers `b`, `b` typed-refers `a`, the
295
- // shape every inverse pair has — that drives this entity again,
334
+ // shape every inverse pair has — that drives this target again,
296
335
  // and the two flow into each other until the depth budget or the
297
336
  // host stack ends it. `unify_cycle` on a model whose meet plainly
298
337
  // converges: `{k:1}` meeting `{k:1}` is a fixpoint, and the
299
338
  // evaluator never got far enough to notice.
300
339
  //
301
- // The guard is the set of entities a flow is currently inside, on
302
- // the context. A flow that would re-enter one is SKIPPED, not
303
- // failed: the outer flow it is nested in is already uniting that
304
- // entity, so the same information arrives by the same channel one
305
- // frame up. What each flow contributes is unchanged; only the
306
- // order it arrives in is, and unification does not care.
307
- const flowing: Set<string> = ((ctx as any)._referflow ??=
308
- new Set<string>())
309
- if (!this.tval.isTop && !flowing.has(this.addr.name)) {
310
- flowing.add(this.addr.name)
311
- try {
312
- // The flowed type is CONCRETE at the target: a schema flowing
313
- // into a value must not make the value a schema. Same reasoning
314
- // as a reference's clone clearing marks — `refer($.std.Service)`
315
- // says the target IS a Service, not that it is the definition
316
- // of one — and without it the target silently stopped
317
- // generating.
318
- const merged = unite(ctx, found.val, concreteFlow(ctx, this.tval),
319
- 'refer-flow')
320
- if (true === (merged as any).isNil) {
321
- return merged
322
- }
323
- if (undefined === found.parent) {
324
- reg!.set(this.addr.name, merged)
340
+ // The guard is the set of paths a flow is currently inside, on the
341
+ // context. A flow that would re-enter one has its MEET skipped:
342
+ // the outer flow it is nested in is already uniting that node, and
343
+ // re-entering is what runs the host stack out.
344
+ //
345
+ // ITS RECORD IS STILL WRITTEN. The skip used to take the record
346
+ // with it, on the reasoning that "the same information arrives by
347
+ // the same channel one frame up" -- true of the outer flow's TYPE,
348
+ // and false of a nested flow carrying a DIFFERENT one. The outer
349
+ // flow delivers only its own type, the residual is consumed into a
350
+ // settled link in the same breath, and nothing re-offers it: the
351
+ // nested type was lost for good, and which of two flows was the
352
+ // nested one depended on which link the evaluator happened to
353
+ // reach first. A layering rule declared on one field and reached
354
+ // through another module's mirror therefore held in one
355
+ // declaration order and not in the other (BUGS.md 69).
356
+ //
357
+ // Recording it DEFERS the meet to `applyFlows`, which runs at the
358
+ // top of the tree with no flow in flight and so cannot re-enter
359
+ // anything -- the channel this record already exists to be.
360
+ const guard = target.join('.')
361
+ // Seeded on the unify root (ts/src/unify.ts): a `??=` here would
362
+ // make a fresh set on whichever descended context asked first.
363
+ const flowing: Set<string> = (ctx as any)._referflow ?? new Set<string>()
364
+ if (!this.tval.isTop) {
365
+ // The flowed type is CONCRETE at the target: a schema flowing
366
+ // into a value must not make the value a schema. Same reasoning
367
+ // as a reference's clone clearing marks — `refer($.std.Service)`
368
+ // says the target IS a Service, not that it is the definition
369
+ // of one — and without it the target silently stopped
370
+ // generating.
371
+ const flow = concreteFlow(ctx, this.tval)
372
+
373
+ // THE RECORD, keyed by the target's path, replayed onto every
374
+ // later pass by applyFlows in ts/src/unify.ts. A pass rebuilds
375
+ // subtrees, so the write below does not survive one when the
376
+ // link sits inside its own target or two nodes link at each
377
+ // other; the record is what makes the flow reach the result
378
+ // rather than the tree it was computed from.
379
+ const flows: Map<string, Val> = (ctx as any).referflows ??
380
+ new Map<string, Val>()
381
+ const key = target.join('\x00')
382
+ const prev = flows.get(key)
383
+ flows.set(key, null == prev ? flow : unite(ctx, prev, flow,
384
+ 'refer-flow-record'))
385
+
386
+ if (!flowing.has(guard)) {
387
+ flowing.add(guard)
388
+ try {
389
+ const merged = unite(ctx, found.val, flow, 'refer-flow')
390
+ if (true === (merged as any).isNil) {
391
+ return merged
392
+ }
393
+ // The write into THIS pass's view, so the rest of the pass
394
+ // sees it. findAt refuses the empty path, so a resolved
395
+ // target always has a parent holding it.
396
+ //
397
+ // NOT FROM INSIDE A TRIAL. A disjunction member is a
398
+ // HYPOTHESIS, and writing its flow into the live tree
399
+ // asserts the member's type on another node before the
400
+ // member is known to survive. The `unite` above still runs:
401
+ // its nil is how a member legitimately fails. Only the
402
+ // COMMIT waits, and the surviving member's flow reaches the
403
+ // tree on the next pass, from the record DisjunctVal stages
404
+ // and merges for survivors alone.
405
+ if (true !== (ctx as any)._trialMode) {
406
+ found.parent.peg[found.key as string] = merged
407
+ }
325
408
  }
326
- else {
327
- found.parent.peg[found.key as string] = merged
409
+ finally {
410
+ flowing.delete(guard)
328
411
  }
329
412
  }
330
- finally {
331
- flowing.delete(this.addr.name)
332
- }
333
413
  }
334
414
 
335
- // The value IS the address string: a link, not an embedding.
336
- const out: any = new StringVal({ peg: this.addrsrc as string }, ctx)
415
+ // The value IS the address, as a PATH VALUE (ADR-016): a link,
416
+ // not an embedding -- and re-stating or refining the address
417
+ // still meets it, which a string link could not do under the
418
+ // strict rules.
419
+ const out: any = new PathVal({ peg: this.addrsrc as string }, ctx)
337
420
  out.dc = DONE
338
421
  // STAMPED as a link (G4 phase 3): the value is the address string,
339
422
  // so without this nothing downstream could tell a checked link from
340
423
  // a literal that happens to look like one. The edge set is exactly
341
- // the set of these stamps.
342
- out.link = this.addrsrc
424
+ // the set of these stamps. A rel()-minted link also carries its
425
+ // PREDICATE (the rel field's key), so the graph reports a declared
426
+ // relation rather than inferring one from the path.
427
+ //
428
+ // The stamp is the RESOLVED path, not the written one: a relative
429
+ // address means a different node from each position it is written
430
+ // at, and an edge set whose far ends were spellings rather than
431
+ // nodes could not be traversed. The VALUE stays what the author
432
+ // wrote --- the link is what it says, the edge is where it goes.
433
+ out.link = '$.' + target.join('.')
434
+ if (undefined !== this.relkey) {
435
+ out.relkey = this.relkey
436
+ }
343
437
  propagateMarks(this, out)
344
438
  out.site = site.site
345
439
  out.path = this.path
@@ -350,8 +444,255 @@ class ReferVal extends FeatureVal {
350
444
  const t = this.tval.isTop ? '' : this.tval.canon
351
445
  const call = 'refer(' + t + ')' +
352
446
  (null == this.held ? '' : '&' + this.held.canon)
447
+ // The address renders as the path call: a bare string address no
448
+ // longer reparses (PATHS.0.md, amended), and canon must.
353
449
  return undefined === this.addrsrc
354
- ? call : call + '&' + JSON.stringify(this.addrsrc)
450
+ ? call : call + '&path(' + this.addrsrc + ')'
451
+ }
452
+ }
453
+
454
+
455
+ // RelVal is what `rel(t?)` RESOLVES to (RELATIONS.0.md §3.2): the
456
+ // relation constraint, sited on the FIELD the way the sizing atoms sit
457
+ // on containers. Its value may be one address, a LIST of addresses, or
458
+ // a MAP whose string leaves are addresses -- the container shapes are
459
+ // rewritten leaf by leaf through the refer machinery, so pending
460
+ // resolution, type flow and link stamping are the one battle-tested
461
+ // path, and the data side stays plain strings. The PREDICATE of every
462
+ // edge produced is the key the rel() sits on -- declared once, in the
463
+ // schema, never inferred from the path.
464
+ class RelVal extends FeatureVal {
465
+ isRel = true
466
+ isGenable = true
467
+ cjo = 45000
468
+
469
+ // The type to flow into each target; TOP when `rel()` has none.
470
+ tval: Val
471
+ // Container-level constraints met before the container arrived
472
+ // (`rel() & length(min(1)) & [...]`): they meet the REWRITTEN
473
+ // container, exactly as refer's held meets the link.
474
+ held?: Val
475
+
476
+ constructor(spec: ValSpec, ctx?: AontuContext) {
477
+ super(spec, ctx)
478
+ this.tval = (spec as any).tval ?? top()
479
+ this.held = (spec as any).held
480
+ // DONE while unmet, deliberately -- the property refer() lacks and
481
+ // G4 phase 4 records the cost of: a type() body holding a rel()
482
+ // must SETTLE, or the schema idiom (`dependsOn?: rel($.T)` inside
483
+ // a vocabulary) leaves the type unresolved and every reference to
484
+ // it deferring forever. An unmet rel is its own settled residual,
485
+ // like `min(1)`; the meet re-activates it whenever a value
486
+ // arrives, because map merges build the conjunct regardless.
487
+ this.dc = DONE
488
+ }
489
+
490
+ clone(ctx: AontuContext, spec?: ValSpec): Val {
491
+ const out: any = super.clone(ctx, spec)
492
+ out.tval = this.tval
493
+ out.held = this.held
494
+ return out
495
+ }
496
+
497
+ // The predicate: the last segment of the field path the constraint
498
+ // is being APPLIED at -- when that segment is a D-1 NAME. A relation
499
+ // predicate is a declared name (RELATIONS.0.md §3.2), so a list
500
+ // index or a key outside the name grammar produces unlabelled
501
+ // links, and the graph falls back to its old inference. The D-1
502
+ // test is also what keeps the two ports' edges identical: a list
503
+ // index is a number here and a string in Go.
504
+ //
505
+ // THE PATH COMES FROM THE CONTEXT, not from `this.path`. The val's
506
+ // own path records where this constraint was CLONED FROM, and the
507
+ // two part company the moment a second document merges into an
508
+ // entity: the field's constraint is then driven at the ENTITY, and
509
+ // `this.path` names the entity while the context names the field.
510
+ // The predicate became the entity's own key, every link under it
511
+ // minted an edge starting one level too high, and `inverse(n)`
512
+ // reported a mirror that was written as missing.
513
+ fieldkey(ctx: AontuContext): string | undefined {
514
+ const path: any[] = (ctx as any).path
515
+ const seg = path[path.length - 1]
516
+ return 'string' === typeof seg && PREDICATE_NAME.test(seg) ? seg : undefined
517
+ }
518
+
519
+ // One leaf's residual: the refer machinery carrying rel's codes,
520
+ // predicate and type. The predicate is passed IN, because the field
521
+ // it names is decided where the rewrite starts and must not be
522
+ // re-read on the way down (see `rewriteUnder`).
523
+ leafRefer(ctx: AontuContext, relkey: string | undefined): ReferVal {
524
+ const rv = new ReferVal({ tval: this.tval } as any, ctx)
525
+ rv.addrcode = 'rel_address'
526
+ rv.unresolvedcode = 'rel_unresolved'
527
+ rv.relkey = relkey
528
+ rv.site = this.site
529
+ rv.path = this.path
530
+ return rv
531
+ }
532
+
533
+ // The container, every string leaf wrapped as a link. Nested
534
+ // containers descend; a leaf already STAMPED as a link is left
535
+ // alone, which is what makes a second application (a template
536
+ // re-applied, a later pass) a no-op.
537
+ rewrite(ctx: AontuContext, container: any): Val {
538
+ return this.rewriteUnder(ctx, container, this.fieldkey(ctx))
539
+ }
540
+
541
+ // The rewrite proper, carrying the predicate decided at the field.
542
+ // It is decided ONCE, at the top: a map-valued relation
543
+ // (`dependsOn: {primary: [...]}`) descends through the inner label
544
+ // and must still report `dependsOn`, which is the whole reason a
545
+ // link carries a declared predicate rather than letting the graph
546
+ // infer one from its position.
547
+ rewriteUnder(
548
+ ctx: AontuContext, container: any, relkey: string | undefined
549
+ ): Val {
550
+ const out: any = container.clone(ctx)
551
+ const peg: any = out.peg
552
+ const keys: any[] = Array.isArray(peg)
553
+ ? peg.map((_v: any, i: number) => i) : Object.keys(peg)
554
+ let pending = false
555
+ let nested = false
556
+ for (const k of keys) {
557
+ // Children here are always Vals: the parse builds Vals, elision
558
+ // builds a NilVal, and clone preserved whatever the container
559
+ // held.
560
+ const child: any = peg[k]
561
+ if (true === child.isMap || true === child.isList) {
562
+ nested = true
563
+ const sub: any = this.rewriteUnder(ctx.descend('' + k), child, relkey)
564
+ peg[k] = sub
565
+ pending = pending || true !== sub.done
566
+ }
567
+ else if (undefined === child.link) {
568
+ let leaf = unite(ctx.descend('' + k), this.leafRefer(ctx, relkey),
569
+ child, 'rel-leaf')
570
+ // The held constraints apply PER LEAF: a re() on the relation
571
+ // constrains every address, never the container that holds
572
+ // them (found by the service catalog, whose re("^svc_") met
573
+ // the whole list and refused it).
574
+ if (null != this.held) {
575
+ leaf = unite(ctx.descend('' + k), leaf, this.held, 'rel-held')
576
+ }
577
+ peg[k] = leaf
578
+ pending = pending || true !== leaf.done
579
+ }
580
+ }
581
+ // The rewrite holds its PENDING leaves open (an address whose
582
+ // entity a later statement declares), so the pass loop keeps
583
+ // driving the container until every link settles. A rewrite that
584
+ // minted NOTHING pending -- every leaf already linked or settled
585
+ // in place -- keeps the clone's doneness: re-applying a template
586
+ // over a settled value must converge in the same pass, or the
587
+ // enclosing bags reopen forever (the service catalog, where the
588
+ // entity merge drops the spread stamp each pass).
589
+ if (pending) {
590
+ out.dc = 0
591
+ }
592
+ // Elements that arrive AFTER this rewrite -- another statement of
593
+ // the list, a patch position of the entity -- must convert too, so
594
+ // the rewrite installs its own leaf constraint as the container's
595
+ // ELEMENT SPREAD, exactly the machinery the old per-element
596
+ // `[&: refer()]` idiom used. Only on a FLAT address container (a
597
+ // labelled map's sub-containers get their own spread from the
598
+ // recursion above; a leaf template meeting a map child would
599
+ // refuse it), and only where no spread already stands: a schema's
600
+ // own template is not this rewrite's to clobber.
601
+ if (!nested && null == (out as any).spread.cj) {
602
+ let tmpl: Val = this.leafRefer(ctx, relkey)
603
+ if (null != this.held) {
604
+ tmpl = new ConjunctVal({ peg: [tmpl, this.held] }, ctx)
605
+ }
606
+ ; (out as any).spread.cj = tmpl
607
+ }
608
+ return out
609
+ }
610
+
611
+ unify(peer: Val, ctx: AontuContext): Val {
612
+ const p: any = peer
613
+
614
+ // Two rel() at one field: one relation, both types.
615
+ if (true === p?.isRel) {
616
+ const out: any = new RelVal({}, ctx)
617
+ out.tval = unite(ctx, this.tval, p.tval, 'rel-t')
618
+ out.held = null == this.held ? p.held
619
+ : null == p.held ? this.held
620
+ : unite(ctx, this.held, p.held, 'rel-held')
621
+ propagateMarks(this, out)
622
+ out.site = this.site
623
+ out.path = this.path
624
+ return out
625
+ }
626
+
627
+ // No null/top/nil arms: unite's dispatch ladder absorbs those
628
+ // peers before any Val's own unify is consulted (a null or top
629
+ // peer returns this side; a nil peer returns the nil), and unite
630
+ // is the only entrance -- the container hand-offs pass the
631
+ // container itself.
632
+
633
+ // ONE ADDRESS: the scalar-valued field, refer's own shape. A path
634
+ // value only -- a bare string is never an address (PATHS.0.md,
635
+ // amended); the scalar arm below refuses it.
636
+ if (true === p.isPath) {
637
+ const out = unite(ctx, this.leafRefer(ctx, this.fieldkey(ctx)), peer,
638
+ 'rel-scalar')
639
+ return null == this.held ? out
640
+ : unite(ctx, out, this.held, 'rel-held')
641
+ }
642
+
643
+ // A SET OF LINKS: list or map, rewritten leaf by leaf; the held
644
+ // constraints ride into each leaf inside the rewrite.
645
+ if (true === p.isMap || true === p.isList) {
646
+ return this.rewrite(ctx, peer)
647
+ }
648
+
649
+ // A scalar that can never be an address.
650
+ if (true === p.isScalar) {
651
+ return makeNilErr(ctx, 'rel_address', this, peer, 'refer')
652
+ }
653
+
654
+ // Everything else -- a reference still resolving, a kind, a
655
+ // container constraint -- waits for the value, as refer's held
656
+ // does.
657
+ const out: any = new RelVal({}, ctx)
658
+ out.tval = this.tval
659
+ out.held = null == this.held ? peer
660
+ : unite(ctx, this.held, peer, 'rel-held')
661
+ propagateMarks(this, out)
662
+ out.site = this.site
663
+ out.path = this.path
664
+ return out
665
+ }
666
+
667
+ get canon() {
668
+ const t = this.tval.isTop ? '' : this.tval.canon
669
+ return 'rel(' + t + ')' +
670
+ (null == this.held ? '' : '&' + this.held.canon)
671
+ }
672
+ }
673
+
674
+
675
+ class RelFuncVal extends FuncBaseVal {
676
+ isRelFunc = true
677
+
678
+ constructor(spec: ValSpec, ctx?: AontuContext) {
679
+ super(spec, ctx)
680
+ }
681
+
682
+ make(_ctx: AontuContext, spec: ValSpec): Val {
683
+ return new RelFuncVal(spec)
684
+ }
685
+
686
+ funcname() {
687
+ return 'rel'
688
+ }
689
+
690
+ resolve(ctx: AontuContext, args: Val[]) {
691
+ const out = new RelVal({}, ctx)
692
+ out.tval = 0 < args.length ? args[0] : top()
693
+ out.site = this.site
694
+ out.path = this.path
695
+ return out
355
696
  }
356
697
  }
357
698
 
@@ -378,10 +719,14 @@ class ReferFuncVal extends FuncBaseVal {
378
719
  out.path = this.path
379
720
  return out
380
721
  }
381
- } /* node:coverage ignore next 6 */
722
+ } /* node:coverage ignore next 10 */
382
723
 
383
724
 
725
+ // The address machinery itself (Address, parseAddress, prefixMeet)
726
+ // lives in PathVal, its home since ADR-016 -- import it from there.
384
727
  export {
385
728
  ReferFuncVal,
386
729
  ReferVal,
730
+ RelFuncVal,
731
+ RelVal,
387
732
  }