@ldclabs/kip-lang 0.3.1 → 2.0.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 (64) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/LICENSE +21 -0
  3. package/README.md +133 -44
  4. package/dist/ast.d.ts +519 -144
  5. package/dist/ast.d.ts.map +1 -1
  6. package/dist/budget.d.ts +37 -0
  7. package/dist/budget.d.ts.map +1 -0
  8. package/dist/budget.js +105 -0
  9. package/dist/budget.js.map +1 -0
  10. package/dist/diagnostics.d.ts +8 -2
  11. package/dist/diagnostics.d.ts.map +1 -1
  12. package/dist/diagnostics.js +32 -3
  13. package/dist/diagnostics.js.map +1 -1
  14. package/dist/errors.d.ts +29 -0
  15. package/dist/errors.d.ts.map +1 -0
  16. package/dist/errors.js +27 -0
  17. package/dist/errors.js.map +1 -0
  18. package/dist/exec-ast.d.ts +679 -0
  19. package/dist/exec-ast.d.ts.map +1 -0
  20. package/dist/exec-ast.js +24 -0
  21. package/dist/exec-ast.js.map +1 -0
  22. package/dist/formatter.d.ts.map +1 -1
  23. package/dist/formatter.js +870 -479
  24. package/dist/formatter.js.map +1 -1
  25. package/dist/index.d.ts +9 -3
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +6 -2
  28. package/dist/index.js.map +1 -1
  29. package/dist/lexer.d.ts.map +1 -1
  30. package/dist/lexer.js +46 -33
  31. package/dist/lexer.js.map +1 -1
  32. package/dist/lower.d.ts +17 -0
  33. package/dist/lower.d.ts.map +1 -0
  34. package/dist/lower.js +1566 -0
  35. package/dist/lower.js.map +1 -0
  36. package/dist/parser.d.ts.map +1 -1
  37. package/dist/parser.js +2476 -1152
  38. package/dist/parser.js.map +1 -1
  39. package/dist/semantics.d.ts +11 -7
  40. package/dist/semantics.d.ts.map +1 -1
  41. package/dist/semantics.js +295 -180
  42. package/dist/semantics.js.map +1 -1
  43. package/dist/token.d.ts +130 -40
  44. package/dist/token.d.ts.map +1 -1
  45. package/dist/token.js +264 -83
  46. package/dist/token.js.map +1 -1
  47. package/dist/version.d.ts +13 -0
  48. package/dist/version.d.ts.map +1 -0
  49. package/dist/version.js +13 -0
  50. package/dist/version.js.map +1 -0
  51. package/package.json +36 -6
  52. package/src/ast.ts +914 -0
  53. package/src/budget.ts +108 -0
  54. package/src/diagnostics.ts +182 -0
  55. package/src/errors.ts +42 -0
  56. package/src/exec-ast.ts +614 -0
  57. package/src/formatter.ts +1339 -0
  58. package/src/index.ts +226 -0
  59. package/src/lexer.ts +459 -0
  60. package/src/lower.ts +2011 -0
  61. package/src/parser.ts +3506 -0
  62. package/src/semantics.ts +392 -0
  63. package/src/token.ts +408 -0
  64. package/src/version.ts +13 -0
package/src/ast.ts ADDED
@@ -0,0 +1,914 @@
1
+ import type { Range } from './token.js'
2
+
3
+ // ─── Base ────────────────────────────────────────────────────────────
4
+
5
+ export interface BaseNode {
6
+ kind: string
7
+ range: Range
8
+ leadingComments?: string[]
9
+ trailingComment?: string
10
+ }
11
+
12
+ // ─── Program (root) ──────────────────────────────────────────────────
13
+
14
+ export interface Program extends BaseNode {
15
+ kind: 'Program'
16
+ statements: Statement[]
17
+ }
18
+
19
+ export type Statement = KqlStatement | KmlStatement | MetaStatement
20
+
21
+ /** KQL — the read language. */
22
+ export type KqlStatement = FindStatement
23
+
24
+ /** KML — the cognitive mutation language. */
25
+ export type KmlStatement = MutateStatement | MutationClause
26
+
27
+ /**
28
+ * Every mutation that may stand alone or inside `MUTATE { ... }`.
29
+ *
30
+ * `MUTATE` itself is excluded: the grammar forbids nesting one mutation
31
+ * transaction inside another.
32
+ */
33
+ export type MutationClause =
34
+ | CreateConceptStatement
35
+ | UpsertConceptStatement
36
+ | EnsurePropositionStatement
37
+ | AssertStatement
38
+ | CreateEvidenceStatement
39
+ | CreateAssertionStatement
40
+ | CreateActivityStatement
41
+ | UpdateStatement
42
+ | RetractAssertionStatement
43
+ | SupersedeAssertionStatement
44
+ | CorrectEvidenceStatement
45
+ | TransitionActivityStatement
46
+ | SetRetentionStatement
47
+ | ArchiveStatement
48
+ | TombstoneStatement
49
+ | PurgeStatement
50
+ | MergeConceptStatement
51
+
52
+ /** META — introspection, grounding, verification, history, export. */
53
+ export type MetaStatement =
54
+ | DescribeStatement
55
+ | ListStatement
56
+ | SearchStatement
57
+ | VerifyStatement
58
+ | ValidateStatement
59
+ | PreviewStatement
60
+ | HistoryStatement
61
+ | ChangesStatement
62
+ | SnapshotStatement
63
+ | ExportCapsuleStatement
64
+
65
+ // ─── Shared operand shapes ───────────────────────────────────────────
66
+
67
+ /** `schema_symbol = string_literal | parameter` */
68
+ export type SchemaSymbol = StringLiteral | ParameterRef
69
+
70
+ /** `scalar_value` / `scalar_or_parameter` / `meta_value` = `parameter | literal` */
71
+ export type ScalarValue =
72
+ | ParameterRef
73
+ | StringLiteral
74
+ | NumberLiteral
75
+ | BooleanLiteral
76
+ | NullLiteral
77
+
78
+ /** `target_ref = variable | parameter | string_literal` */
79
+ export type TargetRef = VariableRef | ParameterRef | StringLiteral
80
+
81
+ /** `handle = variable` — a block-local name bound by a KML mutation. */
82
+ export type Handle = VariableRef
83
+
84
+ // ─── KQL: FIND ───────────────────────────────────────────────────────
85
+
86
+ export interface FindStatement extends BaseNode {
87
+ kind: 'FindStatement'
88
+ projections: Expression[]
89
+ where: WhereClause
90
+ asOf?: AsOfClause
91
+ forTime?: ForTimeClause
92
+ epistemic?: EpistemicClause
93
+ orderBy?: OrderByClause
94
+ limit?: LimitClause
95
+ cursor?: CursorClause
96
+ }
97
+
98
+ /** `AS OF SEQ|TX|TIME` — which cognitive history the read runs against. */
99
+ export interface AsOfClause extends BaseNode {
100
+ kind: 'AsOfClause'
101
+ basis: 'SEQ' | 'TX' | 'TIME'
102
+ value: ScalarValue
103
+ }
104
+
105
+ /** `FOR TIME` — world-valid time, an axis independent of {@link AsOfClause}. */
106
+ export interface ForTimeClause extends BaseNode {
107
+ kind: 'ForTimeClause'
108
+ value: ScalarValue
109
+ }
110
+
111
+ export interface EpistemicClause extends BaseNode {
112
+ kind: 'EpistemicClause'
113
+ options: ObjectLiteral
114
+ }
115
+
116
+ export interface OrderByClause extends BaseNode {
117
+ kind: 'OrderByClause'
118
+ items: OrderItem[]
119
+ }
120
+
121
+ export interface OrderItem extends BaseNode {
122
+ kind: 'OrderItem'
123
+ expression: Expression
124
+ direction?: 'ASC' | 'DESC'
125
+ }
126
+
127
+ export interface LimitClause extends BaseNode {
128
+ kind: 'LimitClause'
129
+ value: ScalarValue
130
+ }
131
+
132
+ export interface CursorClause extends BaseNode {
133
+ kind: 'CursorClause'
134
+ value: ScalarValue
135
+ }
136
+
137
+ // ─── WHERE ───────────────────────────────────────────────────────────
138
+
139
+ export interface WhereClause extends BaseNode {
140
+ kind: 'WhereClause'
141
+ patterns: WherePattern[]
142
+ }
143
+
144
+ export type WherePattern =
145
+ | ConceptPattern
146
+ | PropositionPattern
147
+ | AssertionPattern
148
+ | EvidencePattern
149
+ | ActivityPattern
150
+ | StructuralPattern
151
+ | BeliefPattern
152
+ | BeliefSlotPattern
153
+ | FilterClause
154
+ | NotClause
155
+ | OptionalClause
156
+ | UnionClause
157
+
158
+ export interface ConceptPattern extends BaseNode {
159
+ kind: 'ConceptPattern'
160
+ variable: VariableRef
161
+ /** Whether the optional `CONCEPT` keyword was written. */
162
+ explicit: boolean
163
+ matcher: ObjectPattern
164
+ }
165
+
166
+ export interface PropositionPattern extends BaseNode {
167
+ kind: 'PropositionPattern'
168
+ variable?: VariableRef
169
+ /** Whether the optional `PROPOSITION` keyword was written. */
170
+ explicit: boolean
171
+ tuple: PropositionTuple
172
+ }
173
+
174
+ export interface AssertionPattern extends BaseNode {
175
+ kind: 'AssertionPattern'
176
+ variable: VariableRef
177
+ matcher: ObjectPattern
178
+ }
179
+
180
+ export interface EvidencePattern extends BaseNode {
181
+ kind: 'EvidencePattern'
182
+ variable: VariableRef
183
+ matcher: ObjectPattern
184
+ }
185
+
186
+ export interface ActivityPattern extends BaseNode {
187
+ kind: 'ActivityPattern'
188
+ variable: VariableRef
189
+ matcher: ObjectPattern
190
+ }
191
+
192
+ /**
193
+ * `?edge STRUCTURAL (?src, "has_step", ?dst)` — record topology.
194
+ *
195
+ * Never a semantic Proposition: a claim *about* a structural relation is a
196
+ * separate Proposition + Assertion (Spec §17.3).
197
+ */
198
+ export interface StructuralPattern extends BaseNode {
199
+ kind: 'StructuralPattern'
200
+ variable?: VariableRef
201
+ subject: Term
202
+ field: SchemaSymbol
203
+ object: Term
204
+ }
205
+
206
+ /**
207
+ * `?b BELIEF (...)` — an Epistemic Projection, virtual and read-only.
208
+ *
209
+ * Admitted by KQL only. A Projection can never be a mutation target, so the
210
+ * KML and EXPORT grammars exclude it.
211
+ */
212
+ export interface BeliefPattern extends BaseNode {
213
+ kind: 'BeliefPattern'
214
+ variable: VariableRef
215
+ /** `BELIEF (?p)` — project an already-bound Proposition. */
216
+ proposition?: VariableRef
217
+ /**
218
+ * `BELIEF (id: "P-1")` — project a Proposition already known by identity.
219
+ * The operand is the same Proposition expression slot as a pattern's, so it
220
+ * takes the same id form (Spec §43.2 / §46.1).
221
+ */
222
+ propositionId?: ScalarValue
223
+ /** `BELIEF (?s, "pred", ?o)` — project a tuple (exact predicate, no path). */
224
+ subject?: Term
225
+ predicate?: PredicateAtom
226
+ object?: Term
227
+ }
228
+
229
+ /** `?slot BELIEF SLOT (?s, "pred")` — candidates and conflicts for one slot. */
230
+ export interface BeliefSlotPattern extends BaseNode {
231
+ kind: 'BeliefSlotPattern'
232
+ variable: VariableRef
233
+ subject: Term
234
+ predicate: PredicateAtom
235
+ }
236
+
237
+ export interface FilterClause extends BaseNode {
238
+ kind: 'FilterClause'
239
+ expression: Expression
240
+ }
241
+
242
+ export interface NotClause extends BaseNode {
243
+ kind: 'NotClause'
244
+ patterns: WherePattern[]
245
+ }
246
+
247
+ export interface OptionalClause extends BaseNode {
248
+ kind: 'OptionalClause'
249
+ patterns: WherePattern[]
250
+ }
251
+
252
+ export interface UnionClause extends BaseNode {
253
+ kind: 'UnionClause'
254
+ patterns: WherePattern[]
255
+ }
256
+
257
+ // ─── Raw semantic tuples ─────────────────────────────────────────────
258
+
259
+ /**
260
+ * The Proposition expression slot, in either of its two spellings (Spec §43.2).
261
+ *
262
+ * `(subject, predicate, object)` addresses a Proposition by structure;
263
+ * `(id: ...)` addresses the same slot by record identity. Both are tuples on
264
+ * purpose: a Proposition is not a field-matched record, and keeping one slot
265
+ * is what lets an id reference stand as a `term` endpoint — how a statement
266
+ * about a statement names an existing Proposition.
267
+ *
268
+ * `id` is present exactly when the triple fields are absent.
269
+ */
270
+ export interface PropositionTuple extends BaseNode {
271
+ kind: 'PropositionTuple'
272
+ /** `(id: "P-1")` — match-only; never resolves-or-creates. */
273
+ id?: ScalarValue
274
+ subject?: Term
275
+ predicate?: RawPredicateExpression
276
+ object?: Term
277
+ }
278
+
279
+ export type Term =
280
+ | VariableRef
281
+ | ParameterRef
282
+ | StringLiteral
283
+ | NumberLiteral
284
+ | BooleanLiteral
285
+ | NullLiteral
286
+ | ObjectPattern
287
+ | PropositionTuple
288
+
289
+ /** `predicate_atom = string_literal | parameter | variable` */
290
+ export type PredicateAtom = StringLiteral | ParameterRef | VariableRef
291
+
292
+ /**
293
+ * `raw_predicate_expression` — one or more path atoms joined by `|`.
294
+ *
295
+ * Path quantifiers and alternation are traversal syntax owned by KQL; KML
296
+ * and META require a bare {@link PredicateAtom}, which the parser enforces
297
+ * by position.
298
+ */
299
+ export interface RawPredicateExpression extends BaseNode {
300
+ kind: 'RawPredicateExpression'
301
+ atoms: PredicatePathAtom[]
302
+ }
303
+
304
+ export interface PredicatePathAtom extends BaseNode {
305
+ kind: 'PredicatePathAtom'
306
+ atom: PredicateAtom
307
+ quantifier?: PathQuantifier
308
+ }
309
+
310
+ /** `{n}` / `{n,}` / `{n,m}` — raw traversal only, never belief propagation. */
311
+ export interface PathQuantifier extends BaseNode {
312
+ kind: 'PathQuantifier'
313
+ min: number
314
+ /** Absent means unbounded. */
315
+ max?: number
316
+ /** Whether a comma was written, distinguishing `{2}` from `{2,}`. */
317
+ hasComma: boolean
318
+ }
319
+
320
+ // ─── Match objects ───────────────────────────────────────────────────
321
+
322
+ /**
323
+ * `object_pattern` — the `{...}` used to match, not to assign.
324
+ *
325
+ * Shares its node shape with {@link ObjectLiteral} on purpose: the syntax
326
+ * tree stays deliberately loose (an editor wants the loosest tree it can
327
+ * get) and `lower` closes it to the one form each position means.
328
+ */
329
+ export interface ObjectPattern extends BaseNode {
330
+ kind: 'ObjectPattern'
331
+ members: ObjectEntry[]
332
+ trailingComma?: boolean
333
+ }
334
+
335
+ // ─── KML: create / ensure / upsert ───────────────────────────────────
336
+
337
+ export interface MutateStatement extends BaseNode {
338
+ kind: 'MutateStatement'
339
+ clauses: MutationClause[]
340
+ }
341
+
342
+ export interface CreateConceptStatement extends BaseNode {
343
+ kind: 'CreateConceptStatement'
344
+ handle: Handle
345
+ type?: TypeClause
346
+ clientKey?: ClientKeyClause
347
+ name?: NameClause
348
+ setFields?: SetFieldsClause
349
+ setAttributes?: SetAttributesClause
350
+ setFacets: SetFacetClause[]
351
+ setStructural?: SetStructuralClause
352
+ }
353
+
354
+ export interface UpsertConceptStatement extends BaseNode {
355
+ kind: 'UpsertConceptStatement'
356
+ handle: Handle
357
+ match?: MatchClause
358
+ expectVersion?: ExpectVersionClause
359
+ setFields?: SetFieldsClause
360
+ setAttributes?: SetAttributesClause
361
+ setFacets: SetFacetClause[]
362
+ unsetAttributes?: UnsetAttributesClause
363
+ unsetFacets: UnsetFacetClause[]
364
+ setStructural?: SetStructuralClause
365
+ unsetStructural?: UnsetStructuralClause
366
+ }
367
+
368
+ export interface EnsurePropositionStatement extends BaseNode {
369
+ kind: 'EnsurePropositionStatement'
370
+ handle?: Handle
371
+ tuple: PropositionTuple
372
+ expectVersion?: ExpectVersionClause
373
+ }
374
+
375
+ /**
376
+ * `ASSERT (s, p, o) { by:, mode:, ... } [SUPERSEDING ref]`.
377
+ *
378
+ * Normative sugar (Spec §55.1) for `ENSURE PROPOSITION` + `CREATE ASSERTION`
379
+ * (+ `SUPERSEDE`). `lower` performs that desugaring; it never fabricates
380
+ * state beyond those parts.
381
+ */
382
+ export interface AssertStatement extends BaseNode {
383
+ kind: 'AssertStatement'
384
+ handle?: Handle
385
+ tuple: PropositionTuple
386
+ assignments: ObjectLiteral
387
+ superseding?: TargetRef
388
+ }
389
+
390
+ export interface CreateEvidenceStatement extends BaseNode {
391
+ kind: 'CreateEvidenceStatement'
392
+ handle: Handle
393
+ clientKey?: ClientKeyClause
394
+ setFields?: SetFieldsClause
395
+ setFacets: SetFacetClause[]
396
+ setStructural?: SetStructuralClause
397
+ }
398
+
399
+ export interface CreateAssertionStatement extends BaseNode {
400
+ kind: 'CreateAssertionStatement'
401
+ handle: Handle
402
+ clientKey?: ClientKeyClause
403
+ setFields?: SetFieldsClause
404
+ setFacets: SetFacetClause[]
405
+ setStructural?: SetStructuralClause
406
+ }
407
+
408
+ export interface CreateActivityStatement extends BaseNode {
409
+ kind: 'CreateActivityStatement'
410
+ handle: Handle
411
+ clientKey?: ClientKeyClause
412
+ setFields?: SetFieldsClause
413
+ setFacets: SetFacetClause[]
414
+ setStructural?: SetStructuralClause
415
+ }
416
+
417
+ // ─── KML: clause vocabulary ──────────────────────────────────────────
418
+
419
+ export interface TypeClause extends BaseNode {
420
+ kind: 'TypeClause'
421
+ value: SchemaSymbol
422
+ }
423
+
424
+ export interface ClientKeyClause extends BaseNode {
425
+ kind: 'ClientKeyClause'
426
+ value: ScalarValue
427
+ }
428
+
429
+ export interface NameClause extends BaseNode {
430
+ kind: 'NameClause'
431
+ value: ScalarValue
432
+ }
433
+
434
+ export interface MatchClause extends BaseNode {
435
+ kind: 'MatchClause'
436
+ pattern: ObjectPattern
437
+ }
438
+
439
+ export interface SetFieldsClause extends BaseNode {
440
+ kind: 'SetFieldsClause'
441
+ assignments: ObjectLiteral
442
+ }
443
+
444
+ export interface SetAttributesClause extends BaseNode {
445
+ kind: 'SetAttributesClause'
446
+ assignments: ObjectLiteral
447
+ }
448
+
449
+ export interface SetFacetClause extends BaseNode {
450
+ kind: 'SetFacetClause'
451
+ facet: SchemaSymbol
452
+ assignments: ObjectLiteral
453
+ }
454
+
455
+ export interface UnsetAttributesClause extends BaseNode {
456
+ kind: 'UnsetAttributesClause'
457
+ fields: UnsetField[]
458
+ }
459
+
460
+ export interface UnsetFacetClause extends BaseNode {
461
+ kind: 'UnsetFacetClause'
462
+ facet: SchemaSymbol
463
+ fields: UnsetField[]
464
+ }
465
+
466
+ export interface UnsetField extends BaseNode {
467
+ kind: 'UnsetField'
468
+ name: string
469
+ isQuoted: boolean
470
+ }
471
+
472
+ export interface SetStructuralClause extends BaseNode {
473
+ kind: 'SetStructuralClause'
474
+ assignments: StructuralAssignment[]
475
+ }
476
+
477
+ /**
478
+ * `("has_step", ?step) {index: 0}` — one structural edge, optionally placed.
479
+ *
480
+ * The trailing object carries edge options; `index` is meaningful only on a
481
+ * field declared ordered, and index order is never causality (Spec §17.4).
482
+ */
483
+ export interface StructuralAssignment extends BaseNode {
484
+ kind: 'StructuralAssignment'
485
+ field: SchemaSymbol
486
+ value: Expression
487
+ options?: ObjectLiteral
488
+ }
489
+
490
+ /**
491
+ * `UNSET STRUCTURAL { ("has_step", ?wrong_step) }` — remove references.
492
+ *
493
+ * Every SET has an UNSET. An entry is the SET STRUCTURAL entry without its
494
+ * options object; removal is per reference, ordered fields re-densify, and
495
+ * cardinality is validated at commit (Spec §17.5). Admitted where UNSET
496
+ * ATTRIBUTES is — UPSERT CONCEPT and UPDATE — never on record kinds.
497
+ */
498
+ export interface UnsetStructuralClause extends BaseNode {
499
+ kind: 'UnsetStructuralClause'
500
+ removals: StructuralRemoval[]
501
+ }
502
+
503
+ export interface StructuralRemoval extends BaseNode {
504
+ kind: 'StructuralRemoval'
505
+ field: SchemaSymbol
506
+ value: Expression
507
+ }
508
+
509
+ export interface ExpectVersionClause extends BaseNode {
510
+ kind: 'ExpectVersionClause'
511
+ value: ScalarValue
512
+ }
513
+
514
+ export interface ExpectStateClause extends BaseNode {
515
+ kind: 'ExpectStateClause'
516
+ value: ScalarValue
517
+ }
518
+
519
+ // ─── KML: update ─────────────────────────────────────────────────────
520
+
521
+ /**
522
+ * `UPDATE` reaches mutable state only.
523
+ *
524
+ * Proposition tuples, Assertion epistemic payload, Evidence payload, terminal
525
+ * Activity topology, `_system` and Governance are all out of reach; `lower`
526
+ * rejects those targets rather than letting an engine discover them.
527
+ */
528
+ export interface UpdateStatement extends BaseNode {
529
+ kind: 'UpdateStatement'
530
+ target: TargetRef
531
+ expectVersion?: ExpectVersionClause
532
+ actions: UpdateAction[]
533
+ /**
534
+ * Binds a `?variable` target; a direct `:id` / `"id"` target already names
535
+ * the element and may omit it — the same rule as ARCHIVE, TOMBSTONE, PURGE,
536
+ * SET RETENTION and RETRACT ASSERTION (Spec §58).
537
+ */
538
+ where?: WhereClause
539
+ limit?: LimitClause
540
+ }
541
+
542
+ export type UpdateAction =
543
+ | SetFieldsClause
544
+ | SetAttributesClause
545
+ | SetFacetClause
546
+ | UnsetAttributesClause
547
+ | UnsetFacetClause
548
+ | SetStructuralClause
549
+ | UnsetStructuralClause
550
+
551
+ // ─── KML: lifecycle and correction ───────────────────────────────────
552
+
553
+ export interface RetractAssertionStatement extends BaseNode {
554
+ kind: 'RetractAssertionStatement'
555
+ target: TargetRef
556
+ where?: WhereClause
557
+ limit?: LimitClause
558
+ expectState?: ExpectStateClause
559
+ }
560
+
561
+ export interface SupersedeAssertionStatement extends BaseNode {
562
+ kind: 'SupersedeAssertionStatement'
563
+ target: TargetRef
564
+ by: TargetRef
565
+ expectState?: ExpectStateClause
566
+ }
567
+
568
+ export interface CorrectEvidenceStatement extends BaseNode {
569
+ kind: 'CorrectEvidenceStatement'
570
+ target: TargetRef
571
+ by: TargetRef
572
+ expectState?: ExpectStateClause
573
+ }
574
+
575
+ export interface TransitionActivityStatement extends BaseNode {
576
+ kind: 'TransitionActivityStatement'
577
+ target: TargetRef
578
+ to: ScalarValue
579
+ finalize: (SetFieldsClause | SetStructuralClause)[]
580
+ expectState?: ExpectStateClause
581
+ }
582
+
583
+ // ─── KML: retention and removal ──────────────────────────────────────
584
+
585
+ export interface SetRetentionStatement extends BaseNode {
586
+ kind: 'SetRetentionStatement'
587
+ target: TargetRef
588
+ assignments: ObjectLiteral
589
+ where?: WhereClause
590
+ limit?: LimitClause
591
+ expectVersion?: ExpectVersionClause
592
+ }
593
+
594
+ export interface ArchiveStatement extends BaseNode {
595
+ kind: 'ArchiveStatement'
596
+ target: TargetRef
597
+ where?: WhereClause
598
+ limit?: LimitClause
599
+ expectState?: ExpectStateClause
600
+ }
601
+
602
+ export interface TombstoneStatement extends BaseNode {
603
+ kind: 'TombstoneStatement'
604
+ target: TargetRef
605
+ where?: WhereClause
606
+ limit?: LimitClause
607
+ expectState?: ExpectStateClause
608
+ }
609
+
610
+ /** Physical erasure. The grammar freezes the confirmation as `CONFIRM "PURGE"`. */
611
+ export interface PurgeStatement extends BaseNode {
612
+ kind: 'PurgeStatement'
613
+ target: TargetRef
614
+ where?: WhereClause
615
+ limit?: LimitClause
616
+ referencePolicy?: ScalarValue
617
+ confirm: StringLiteral
618
+ }
619
+
620
+ /** Non-destructive: the source stays addressable as merged history. */
621
+ export interface MergeConceptStatement extends BaseNode {
622
+ kind: 'MergeConceptStatement'
623
+ source: TargetRef
624
+ into: TargetRef
625
+ where?: WhereClause
626
+ expectVersion?: ExpectVersionClause
627
+ }
628
+
629
+ // ─── META: DESCRIBE ──────────────────────────────────────────────────
630
+
631
+ export type DescribeTargetKind =
632
+ | 'PRIMER'
633
+ | 'PROTOCOL'
634
+ | 'EXECUTION_CONTEXT'
635
+ | 'CAPABILITIES'
636
+ | 'SPACE'
637
+ | 'SCHEMA_ENVIRONMENT'
638
+ | 'PACKAGE'
639
+ | 'TYPE'
640
+ | 'PREDICATE'
641
+ | 'FACET'
642
+ | 'STRUCTURAL_FIELD'
643
+ | 'COMPATIBILITY'
644
+ | 'ERROR'
645
+ | 'TRANSACTION'
646
+ | 'TRANSACTION_BY_IDEMPOTENCY_KEY'
647
+ | 'SNAPSHOT'
648
+ | 'CAPSULE'
649
+ | 'EPISTEMIC_POLICY'
650
+ | 'PROJECTION_CAPABILITY'
651
+ | 'TRUST'
652
+ | 'ACCESS'
653
+
654
+ export interface DescribeStatement extends BaseNode {
655
+ kind: 'DescribeStatement'
656
+ target: DescribeTargetKind
657
+ /** The single operand, where the target takes one. */
658
+ value?: ScalarValue
659
+ /** `DESCRIBE PRIMER MODE ...` */
660
+ mode?: ScalarValue
661
+ /** `DESCRIBE COMPATIBILITY FROM ... TO ...` */
662
+ from?: ScalarValue
663
+ to?: ScalarValue
664
+ /** `DESCRIBE SCHEMA ENVIRONMENT` / `DESCRIBE SNAPSHOT` */
665
+ asOf?: AsOfClause
666
+ /** `DESCRIBE ACCESS WITH {...}` */
667
+ with?: ObjectLiteral
668
+ }
669
+
670
+ // ─── META: LIST ──────────────────────────────────────────────────────
671
+
672
+ export type ListTargetKind =
673
+ | 'SPACES'
674
+ | 'SCHEMA_PACKAGES'
675
+ | 'TYPES'
676
+ | 'PREDICATES'
677
+ | 'FACETS'
678
+ | 'STRUCTURAL_FIELDS'
679
+ | 'EPISTEMIC_POLICIES'
680
+
681
+ export interface ListStatement extends BaseNode {
682
+ kind: 'ListStatement'
683
+ target: ListTargetKind
684
+ /** `LIST SCHEMA PACKAGES STATUS ...` */
685
+ status?: ScalarValue
686
+ limit?: LimitClause
687
+ cursor?: CursorClause
688
+ }
689
+
690
+ // ─── META: SEARCH ────────────────────────────────────────────────────
691
+
692
+ export type SearchKind =
693
+ | 'CONCEPT'
694
+ | 'PROPOSITION'
695
+ | 'ASSERTION'
696
+ | 'EVIDENCE'
697
+ | 'ACTIVITY'
698
+ | 'COGNITION'
699
+
700
+ /**
701
+ * Grounding only: a SEARCH score is not confidence, and a miss is not absence.
702
+ * The golden path is SEARCH → exact id → BELIEF/FIND.
703
+ */
704
+ export interface SearchStatement extends BaseNode {
705
+ kind: 'SearchStatement'
706
+ searchKind: SearchKind
707
+ term: ScalarValue
708
+ withType?: ScalarValue
709
+ withPredicate?: ScalarValue
710
+ mode?: ScalarValue
711
+ threshold?: ScalarValue
712
+ /** `AS OF SEQ ...` — historical index basis. */
713
+ asOfSeq?: ScalarValue
714
+ limit?: LimitClause
715
+ cursor?: CursorClause
716
+ }
717
+
718
+ // ─── META: VERIFY / VALIDATE / PREVIEW ───────────────────────────────
719
+
720
+ export type VerifyTargetKind =
721
+ | 'CAPSULE'
722
+ | 'SCHEMA_PACKAGE'
723
+ | 'RECEIPT'
724
+ | 'BLOB'
725
+ | 'CHECKPOINT'
726
+
727
+ export interface VerifyStatement extends BaseNode {
728
+ kind: 'VerifyStatement'
729
+ target: VerifyTargetKind
730
+ value: ScalarValue
731
+ }
732
+
733
+ export type ValidateTargetKind =
734
+ | 'KQL'
735
+ | 'KML'
736
+ | 'CAPSULE'
737
+ | 'SCHEMA_PACKAGE'
738
+ | 'IMPORT_PLAN'
739
+
740
+ export interface ValidateStatement extends BaseNode {
741
+ kind: 'ValidateStatement'
742
+ target: ValidateTargetKind
743
+ value: ScalarValue
744
+ options?: ObjectLiteral
745
+ }
746
+
747
+ export interface PreviewStatement extends BaseNode {
748
+ kind: 'PreviewStatement'
749
+ target: 'KML' | 'IMPORT_CAPSULE'
750
+ value: ScalarValue
751
+ /** `PREVIEW IMPORT CAPSULE ... INTO ...` */
752
+ into?: ScalarValue
753
+ }
754
+
755
+ // ─── META: HISTORY / CHANGES / SNAPSHOT ──────────────────────────────
756
+
757
+ export interface HistoryStatement extends BaseNode {
758
+ kind: 'HistoryStatement'
759
+ target: 'ELEMENT' | 'SPACE'
760
+ /** Present for `HISTORY ELEMENT`. */
761
+ value?: ScalarValue
762
+ fromSeq?: ScalarValue
763
+ toSeq?: ScalarValue
764
+ limit?: LimitClause
765
+ cursor?: CursorClause
766
+ }
767
+
768
+ export interface ChangesStatement extends BaseNode {
769
+ kind: 'ChangesStatement'
770
+ mode: 'SINCE' | 'AFTER_SEQ'
771
+ value: ScalarValue
772
+ limit?: LimitClause
773
+ }
774
+
775
+ export interface SnapshotStatement extends BaseNode {
776
+ kind: 'SnapshotStatement'
777
+ asOf?: AsOfClause
778
+ }
779
+
780
+ // ─── META: EXPORT CAPSULE ────────────────────────────────────────────
781
+
782
+ export interface ExportCapsuleStatement extends BaseNode {
783
+ kind: 'ExportCapsuleStatement'
784
+ target: TargetRef
785
+ where: WhereClause
786
+ options?: ObjectLiteral
787
+ asOf?: AsOfClause
788
+ }
789
+
790
+ // ─── Expressions ─────────────────────────────────────────────────────
791
+
792
+ export type Expression =
793
+ | BinaryExpression
794
+ | UnaryExpression
795
+ | FunctionCallExpr
796
+ | AggregateExpr
797
+ | FieldAccess
798
+ | VariableRef
799
+ | ParameterRef
800
+ | StringLiteral
801
+ | NumberLiteral
802
+ | BooleanLiteral
803
+ | NullLiteral
804
+ | ArrayLiteral
805
+ | ObjectLiteral
806
+ | ObjectPattern
807
+ | PropositionTuple
808
+
809
+ export interface BinaryExpression extends BaseNode {
810
+ kind: 'BinaryExpression'
811
+ operator: string
812
+ left: Expression
813
+ right: Expression
814
+ }
815
+
816
+ export interface UnaryExpression extends BaseNode {
817
+ kind: 'UnaryExpression'
818
+ operator: '!' | '-'
819
+ operand: Expression
820
+ }
821
+
822
+ export interface FunctionCallExpr extends BaseNode {
823
+ kind: 'FunctionCallExpr'
824
+ name: string
825
+ args: Expression[]
826
+ }
827
+
828
+ /** `COUNT(DISTINCT ?x)` and friends — legal in projection and sort positions. */
829
+ export interface AggregateExpr extends BaseNode {
830
+ kind: 'AggregateExpr'
831
+ name: string
832
+ distinct: boolean
833
+ argument: Expression
834
+ }
835
+
836
+ /**
837
+ * `?x.facets["MnemonicState"].memory_strength` — a variable plus a dot path.
838
+ *
839
+ * Kept flat rather than as nested binary nodes because every consumer wants
840
+ * the path as a sequence, and `lower` emits exactly that.
841
+ */
842
+ export interface FieldAccess extends BaseNode {
843
+ kind: 'FieldAccess'
844
+ base: VariableRef
845
+ steps: FieldStep[]
846
+ }
847
+
848
+ export type FieldStep = DotStep | IndexStep
849
+
850
+ export interface DotStep extends BaseNode {
851
+ kind: 'DotStep'
852
+ name: string
853
+ }
854
+
855
+ export interface IndexStep extends BaseNode {
856
+ kind: 'IndexStep'
857
+ key: StringLiteral
858
+ }
859
+
860
+ export interface VariableRef extends BaseNode {
861
+ kind: 'VariableRef'
862
+ name: string // including ?
863
+ }
864
+
865
+ export interface ParameterRef extends BaseNode {
866
+ kind: 'ParameterRef'
867
+ name: string // including :
868
+ }
869
+
870
+ export interface StringLiteral extends BaseNode {
871
+ kind: 'StringLiteral'
872
+ value: string // the raw string with quotes
873
+ parsed: string // the unescaped value
874
+ }
875
+
876
+ export interface NumberLiteral extends BaseNode {
877
+ kind: 'NumberLiteral'
878
+ value: number
879
+ raw: string
880
+ }
881
+
882
+ export interface BooleanLiteral extends BaseNode {
883
+ kind: 'BooleanLiteral'
884
+ value: boolean
885
+ }
886
+
887
+ export interface NullLiteral extends BaseNode {
888
+ kind: 'NullLiteral'
889
+ }
890
+
891
+ export interface ArrayLiteral extends BaseNode {
892
+ kind: 'ArrayLiteral'
893
+ elements: Expression[]
894
+ /**
895
+ * Whether a comma preceded the closing bracket. JSON-value position tolerates
896
+ * it; a FILTER list does not, and only the source says which was written.
897
+ */
898
+ trailingComma?: boolean
899
+ }
900
+
901
+ export interface ObjectLiteral extends BaseNode {
902
+ kind: 'ObjectLiteral'
903
+ entries: ObjectEntry[]
904
+ /** See {@link ArrayLiteral.trailingComma}. */
905
+ trailingComma?: boolean
906
+ }
907
+
908
+ export interface ObjectEntry extends BaseNode {
909
+ kind: 'ObjectEntry'
910
+ key: string
911
+ /** Whether the key was originally quoted (e.g. `"description"` vs `description`) */
912
+ isQuoted: boolean
913
+ value: Expression
914
+ }