@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/dist/ast.d.ts CHANGED
@@ -9,78 +9,146 @@ export interface Program extends BaseNode {
9
9
  kind: 'Program';
10
10
  statements: Statement[];
11
11
  }
12
- export type Statement = FindStatement | UpsertStatement | UpdateStatement | MergeStatement | DeleteStatement | DescribeStatement | SearchStatement | ExportStatement;
12
+ export type Statement = KqlStatement | KmlStatement | MetaStatement;
13
+ /** KQL — the read language. */
14
+ export type KqlStatement = FindStatement;
15
+ /** KML — the cognitive mutation language. */
16
+ export type KmlStatement = MutateStatement | MutationClause;
17
+ /**
18
+ * Every mutation that may stand alone or inside `MUTATE { ... }`.
19
+ *
20
+ * `MUTATE` itself is excluded: the grammar forbids nesting one mutation
21
+ * transaction inside another.
22
+ */
23
+ export type MutationClause = CreateConceptStatement | UpsertConceptStatement | EnsurePropositionStatement | AssertStatement | CreateEvidenceStatement | CreateAssertionStatement | CreateActivityStatement | UpdateStatement | RetractAssertionStatement | SupersedeAssertionStatement | CorrectEvidenceStatement | TransitionActivityStatement | SetRetentionStatement | ArchiveStatement | TombstoneStatement | PurgeStatement | MergeConceptStatement;
24
+ /** META — introspection, grounding, verification, history, export. */
25
+ export type MetaStatement = DescribeStatement | ListStatement | SearchStatement | VerifyStatement | ValidateStatement | PreviewStatement | HistoryStatement | ChangesStatement | SnapshotStatement | ExportCapsuleStatement;
26
+ /** `schema_symbol = string_literal | parameter` */
27
+ export type SchemaSymbol = StringLiteral | ParameterRef;
28
+ /** `scalar_value` / `scalar_or_parameter` / `meta_value` = `parameter | literal` */
29
+ export type ScalarValue = ParameterRef | StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral;
30
+ /** `target_ref = variable | parameter | string_literal` */
31
+ export type TargetRef = VariableRef | ParameterRef | StringLiteral;
32
+ /** `handle = variable` — a block-local name bound by a KML mutation. */
33
+ export type Handle = VariableRef;
13
34
  export interface FindStatement extends BaseNode {
14
35
  kind: 'FindStatement';
15
36
  projections: Expression[];
16
- where?: WhereClause;
37
+ where: WhereClause;
38
+ asOf?: AsOfClause;
39
+ forTime?: ForTimeClause;
40
+ epistemic?: EpistemicClause;
17
41
  orderBy?: OrderByClause;
18
42
  limit?: LimitClause;
19
43
  cursor?: CursorClause;
20
44
  }
45
+ /** `AS OF SEQ|TX|TIME` — which cognitive history the read runs against. */
46
+ export interface AsOfClause extends BaseNode {
47
+ kind: 'AsOfClause';
48
+ basis: 'SEQ' | 'TX' | 'TIME';
49
+ value: ScalarValue;
50
+ }
51
+ /** `FOR TIME` — world-valid time, an axis independent of {@link AsOfClause}. */
52
+ export interface ForTimeClause extends BaseNode {
53
+ kind: 'ForTimeClause';
54
+ value: ScalarValue;
55
+ }
56
+ export interface EpistemicClause extends BaseNode {
57
+ kind: 'EpistemicClause';
58
+ options: ObjectLiteral;
59
+ }
21
60
  export interface OrderByClause extends BaseNode {
22
61
  kind: 'OrderByClause';
23
- keys: OrderByKey[];
24
- /** First sort key, preserved for compatibility with pre-RC9 consumers. */
25
- expression: Expression;
26
- /** First sort direction, preserved for compatibility with pre-RC9 consumers. */
27
- direction: 'ASC' | 'DESC';
62
+ items: OrderItem[];
28
63
  }
29
- export interface OrderByKey extends BaseNode {
30
- kind: 'OrderByKey';
64
+ export interface OrderItem extends BaseNode {
65
+ kind: 'OrderItem';
31
66
  expression: Expression;
32
- direction: 'ASC' | 'DESC';
67
+ direction?: 'ASC' | 'DESC';
33
68
  }
34
69
  export interface LimitClause extends BaseNode {
35
70
  kind: 'LimitClause';
36
- value: NumberLiteral | ParameterRef;
71
+ value: ScalarValue;
37
72
  }
38
73
  export interface CursorClause extends BaseNode {
39
74
  kind: 'CursorClause';
40
- value: StringLiteral | ParameterRef;
75
+ value: ScalarValue;
41
76
  }
42
77
  export interface WhereClause extends BaseNode {
43
78
  kind: 'WhereClause';
44
79
  patterns: WherePattern[];
45
80
  }
46
- export type WherePattern = ConceptPattern | PropositionPattern | FilterClause | NotClause | OptionalClause | UnionClause;
81
+ export type WherePattern = ConceptPattern | PropositionPattern | AssertionPattern | EvidencePattern | ActivityPattern | StructuralPattern | BeliefPattern | BeliefSlotPattern | FilterClause | NotClause | OptionalClause | UnionClause;
47
82
  export interface ConceptPattern extends BaseNode {
48
83
  kind: 'ConceptPattern';
49
- variable?: string;
50
- matcher: ConceptMatcher;
51
- }
52
- export interface ConceptMatcher extends BaseNode {
53
- kind: 'ConceptMatcher';
54
- entries: ObjectEntry[];
84
+ variable: VariableRef;
85
+ /** Whether the optional `CONCEPT` keyword was written. */
86
+ explicit: boolean;
87
+ matcher: ObjectPattern;
55
88
  }
56
89
  export interface PropositionPattern extends BaseNode {
57
90
  kind: 'PropositionPattern';
58
- variable?: string;
59
- /** Match by proposition link id: (id: "...") */
60
- id?: StringLiteral | ParameterRef;
61
- subject?: PropositionEndpoint;
62
- predicate?: PredicateExpr;
63
- object?: PropositionEndpoint;
64
- }
65
- export type PropositionEndpoint = VariableRef | ConceptPattern | PropositionPattern;
66
- export type PredicateExpr = PredicateLiteral | PredicateVariable | PredicateAlternation;
67
- export interface PredicateVariable extends BaseNode {
68
- kind: 'PredicateVariable';
69
- name: string;
70
- }
71
- export interface PredicateLiteral extends BaseNode {
72
- kind: 'PredicateLiteral';
73
- value: string;
74
- hopRange?: HopRange;
75
- }
76
- export interface PredicateAlternation extends BaseNode {
77
- kind: 'PredicateAlternation';
78
- predicates: PredicateLiteral[];
79
- }
80
- export interface HopRange extends BaseNode {
81
- kind: 'HopRange';
82
- min: number;
83
- max?: number;
91
+ variable?: VariableRef;
92
+ /** Whether the optional `PROPOSITION` keyword was written. */
93
+ explicit: boolean;
94
+ tuple: PropositionTuple;
95
+ }
96
+ export interface AssertionPattern extends BaseNode {
97
+ kind: 'AssertionPattern';
98
+ variable: VariableRef;
99
+ matcher: ObjectPattern;
100
+ }
101
+ export interface EvidencePattern extends BaseNode {
102
+ kind: 'EvidencePattern';
103
+ variable: VariableRef;
104
+ matcher: ObjectPattern;
105
+ }
106
+ export interface ActivityPattern extends BaseNode {
107
+ kind: 'ActivityPattern';
108
+ variable: VariableRef;
109
+ matcher: ObjectPattern;
110
+ }
111
+ /**
112
+ * `?edge STRUCTURAL (?src, "has_step", ?dst)` — record topology.
113
+ *
114
+ * Never a semantic Proposition: a claim *about* a structural relation is a
115
+ * separate Proposition + Assertion (Spec §17.3).
116
+ */
117
+ export interface StructuralPattern extends BaseNode {
118
+ kind: 'StructuralPattern';
119
+ variable?: VariableRef;
120
+ subject: Term;
121
+ field: SchemaSymbol;
122
+ object: Term;
123
+ }
124
+ /**
125
+ * `?b BELIEF (...)` — an Epistemic Projection, virtual and read-only.
126
+ *
127
+ * Admitted by KQL only. A Projection can never be a mutation target, so the
128
+ * KML and EXPORT grammars exclude it.
129
+ */
130
+ export interface BeliefPattern extends BaseNode {
131
+ kind: 'BeliefPattern';
132
+ variable: VariableRef;
133
+ /** `BELIEF (?p)` — project an already-bound Proposition. */
134
+ proposition?: VariableRef;
135
+ /**
136
+ * `BELIEF (id: "P-1")` — project a Proposition already known by identity.
137
+ * The operand is the same Proposition expression slot as a pattern's, so it
138
+ * takes the same id form (Spec §43.2 / §46.1).
139
+ */
140
+ propositionId?: ScalarValue;
141
+ /** `BELIEF (?s, "pred", ?o)` — project a tuple (exact predicate, no path). */
142
+ subject?: Term;
143
+ predicate?: PredicateAtom;
144
+ object?: Term;
145
+ }
146
+ /** `?slot BELIEF SLOT (?s, "pred")` — candidates and conflicts for one slot. */
147
+ export interface BeliefSlotPattern extends BaseNode {
148
+ kind: 'BeliefSlotPattern';
149
+ variable: VariableRef;
150
+ subject: Term;
151
+ predicate: PredicateAtom;
84
152
  }
85
153
  export interface FilterClause extends BaseNode {
86
154
  kind: 'FilterClause';
@@ -98,120 +166,398 @@ export interface UnionClause extends BaseNode {
98
166
  kind: 'UnionClause';
99
167
  patterns: WherePattern[];
100
168
  }
101
- export interface UpsertStatement extends BaseNode {
102
- kind: 'UpsertStatement';
103
- blocks: UpsertBlock[];
104
- metadata?: WithMetadata;
105
- }
106
- export type UpsertBlock = ConceptBlock | PropositionBlock;
107
- export interface ConceptBlock extends BaseNode {
108
- kind: 'ConceptBlock';
109
- handle: string;
110
- matcher: ConceptMatcher;
111
- expectVersion?: ExpectVersion;
112
- setAttributes?: SetAttributes;
113
- setPropositions?: SetPropositions;
114
- metadata?: WithMetadata;
115
- }
116
- export interface PropositionBlock extends BaseNode {
117
- kind: 'PropositionBlock';
118
- handle?: string;
119
- /** Match an existing proposition by id: (id: "...") */
120
- id?: StringLiteral | ParameterRef;
121
- subject?: PropositionEndpoint;
122
- predicate?: PredicateExpr;
123
- object?: PropositionEndpoint;
124
- expectVersion?: ExpectVersion;
125
- setAttributes?: SetAttributes;
126
- metadata?: WithMetadata;
127
- }
128
- export interface ExpectVersion extends BaseNode {
129
- kind: 'ExpectVersion';
130
- value: NumberLiteral | ParameterRef;
131
- }
132
- export interface SetAttributes extends BaseNode {
133
- kind: 'SetAttributes';
134
- entries: ObjectEntry[];
135
- }
136
- export interface SetPropositions extends BaseNode {
137
- kind: 'SetPropositions';
138
- items: PropositionItem[];
139
- }
140
- export interface PropositionItem extends BaseNode {
141
- kind: 'PropositionItem';
142
- predicate: string;
143
- target: PropositionEndpoint;
144
- metadata?: WithMetadata;
145
- }
146
- export interface WithMetadata extends BaseNode {
147
- kind: 'WithMetadata';
148
- entries: ObjectEntry[];
169
+ /**
170
+ * The Proposition expression slot, in either of its two spellings (Spec §43.2).
171
+ *
172
+ * `(subject, predicate, object)` addresses a Proposition by structure;
173
+ * `(id: ...)` addresses the same slot by record identity. Both are tuples on
174
+ * purpose: a Proposition is not a field-matched record, and keeping one slot
175
+ * is what lets an id reference stand as a `term` endpoint — how a statement
176
+ * about a statement names an existing Proposition.
177
+ *
178
+ * `id` is present exactly when the triple fields are absent.
179
+ */
180
+ export interface PropositionTuple extends BaseNode {
181
+ kind: 'PropositionTuple';
182
+ /** `(id: "P-1")` — match-only; never resolves-or-creates. */
183
+ id?: ScalarValue;
184
+ subject?: Term;
185
+ predicate?: RawPredicateExpression;
186
+ object?: Term;
187
+ }
188
+ export type Term = VariableRef | ParameterRef | StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral | ObjectPattern | PropositionTuple;
189
+ /** `predicate_atom = string_literal | parameter | variable` */
190
+ export type PredicateAtom = StringLiteral | ParameterRef | VariableRef;
191
+ /**
192
+ * `raw_predicate_expression` — one or more path atoms joined by `|`.
193
+ *
194
+ * Path quantifiers and alternation are traversal syntax owned by KQL; KML
195
+ * and META require a bare {@link PredicateAtom}, which the parser enforces
196
+ * by position.
197
+ */
198
+ export interface RawPredicateExpression extends BaseNode {
199
+ kind: 'RawPredicateExpression';
200
+ atoms: PredicatePathAtom[];
201
+ }
202
+ export interface PredicatePathAtom extends BaseNode {
203
+ kind: 'PredicatePathAtom';
204
+ atom: PredicateAtom;
205
+ quantifier?: PathQuantifier;
206
+ }
207
+ /** `{n}` / `{n,}` / `{n,m}` — raw traversal only, never belief propagation. */
208
+ export interface PathQuantifier extends BaseNode {
209
+ kind: 'PathQuantifier';
210
+ min: number;
211
+ /** Absent means unbounded. */
212
+ max?: number;
213
+ /** Whether a comma was written, distinguishing `{2}` from `{2,}`. */
214
+ hasComma: boolean;
215
+ }
216
+ /**
217
+ * `object_pattern` — the `{...}` used to match, not to assign.
218
+ *
219
+ * Shares its node shape with {@link ObjectLiteral} on purpose: the syntax
220
+ * tree stays deliberately loose (an editor wants the loosest tree it can
221
+ * get) and `lower` closes it to the one form each position means.
222
+ */
223
+ export interface ObjectPattern extends BaseNode {
224
+ kind: 'ObjectPattern';
225
+ members: ObjectEntry[];
226
+ trailingComma?: boolean;
227
+ }
228
+ export interface MutateStatement extends BaseNode {
229
+ kind: 'MutateStatement';
230
+ clauses: MutationClause[];
231
+ }
232
+ export interface CreateConceptStatement extends BaseNode {
233
+ kind: 'CreateConceptStatement';
234
+ handle: Handle;
235
+ type?: TypeClause;
236
+ clientKey?: ClientKeyClause;
237
+ name?: NameClause;
238
+ setFields?: SetFieldsClause;
239
+ setAttributes?: SetAttributesClause;
240
+ setFacets: SetFacetClause[];
241
+ setStructural?: SetStructuralClause;
242
+ }
243
+ export interface UpsertConceptStatement extends BaseNode {
244
+ kind: 'UpsertConceptStatement';
245
+ handle: Handle;
246
+ match?: MatchClause;
247
+ expectVersion?: ExpectVersionClause;
248
+ setFields?: SetFieldsClause;
249
+ setAttributes?: SetAttributesClause;
250
+ setFacets: SetFacetClause[];
251
+ unsetAttributes?: UnsetAttributesClause;
252
+ unsetFacets: UnsetFacetClause[];
253
+ setStructural?: SetStructuralClause;
254
+ unsetStructural?: UnsetStructuralClause;
255
+ }
256
+ export interface EnsurePropositionStatement extends BaseNode {
257
+ kind: 'EnsurePropositionStatement';
258
+ handle?: Handle;
259
+ tuple: PropositionTuple;
260
+ expectVersion?: ExpectVersionClause;
261
+ }
262
+ /**
263
+ * `ASSERT (s, p, o) { by:, mode:, ... } [SUPERSEDING ref]`.
264
+ *
265
+ * Normative sugar (Spec §55.1) for `ENSURE PROPOSITION` + `CREATE ASSERTION`
266
+ * (+ `SUPERSEDE`). `lower` performs that desugaring; it never fabricates
267
+ * state beyond those parts.
268
+ */
269
+ export interface AssertStatement extends BaseNode {
270
+ kind: 'AssertStatement';
271
+ handle?: Handle;
272
+ tuple: PropositionTuple;
273
+ assignments: ObjectLiteral;
274
+ superseding?: TargetRef;
275
+ }
276
+ export interface CreateEvidenceStatement extends BaseNode {
277
+ kind: 'CreateEvidenceStatement';
278
+ handle: Handle;
279
+ clientKey?: ClientKeyClause;
280
+ setFields?: SetFieldsClause;
281
+ setFacets: SetFacetClause[];
282
+ setStructural?: SetStructuralClause;
283
+ }
284
+ export interface CreateAssertionStatement extends BaseNode {
285
+ kind: 'CreateAssertionStatement';
286
+ handle: Handle;
287
+ clientKey?: ClientKeyClause;
288
+ setFields?: SetFieldsClause;
289
+ setFacets: SetFacetClause[];
290
+ setStructural?: SetStructuralClause;
291
+ }
292
+ export interface CreateActivityStatement extends BaseNode {
293
+ kind: 'CreateActivityStatement';
294
+ handle: Handle;
295
+ clientKey?: ClientKeyClause;
296
+ setFields?: SetFieldsClause;
297
+ setFacets: SetFacetClause[];
298
+ setStructural?: SetStructuralClause;
299
+ }
300
+ export interface TypeClause extends BaseNode {
301
+ kind: 'TypeClause';
302
+ value: SchemaSymbol;
303
+ }
304
+ export interface ClientKeyClause extends BaseNode {
305
+ kind: 'ClientKeyClause';
306
+ value: ScalarValue;
307
+ }
308
+ export interface NameClause extends BaseNode {
309
+ kind: 'NameClause';
310
+ value: ScalarValue;
311
+ }
312
+ export interface MatchClause extends BaseNode {
313
+ kind: 'MatchClause';
314
+ pattern: ObjectPattern;
315
+ }
316
+ export interface SetFieldsClause extends BaseNode {
317
+ kind: 'SetFieldsClause';
318
+ assignments: ObjectLiteral;
319
+ }
320
+ export interface SetAttributesClause extends BaseNode {
321
+ kind: 'SetAttributesClause';
322
+ assignments: ObjectLiteral;
323
+ }
324
+ export interface SetFacetClause extends BaseNode {
325
+ kind: 'SetFacetClause';
326
+ facet: SchemaSymbol;
327
+ assignments: ObjectLiteral;
328
+ }
329
+ export interface UnsetAttributesClause extends BaseNode {
330
+ kind: 'UnsetAttributesClause';
331
+ fields: UnsetField[];
332
+ }
333
+ export interface UnsetFacetClause extends BaseNode {
334
+ kind: 'UnsetFacetClause';
335
+ facet: SchemaSymbol;
336
+ fields: UnsetField[];
337
+ }
338
+ export interface UnsetField extends BaseNode {
339
+ kind: 'UnsetField';
340
+ name: string;
341
+ isQuoted: boolean;
149
342
  }
150
- export interface SetMetadata extends BaseNode {
151
- kind: 'SetMetadata';
152
- entries: ObjectEntry[];
343
+ export interface SetStructuralClause extends BaseNode {
344
+ kind: 'SetStructuralClause';
345
+ assignments: StructuralAssignment[];
346
+ }
347
+ /**
348
+ * `("has_step", ?step) {index: 0}` — one structural edge, optionally placed.
349
+ *
350
+ * The trailing object carries edge options; `index` is meaningful only on a
351
+ * field declared ordered, and index order is never causality (Spec §17.4).
352
+ */
353
+ export interface StructuralAssignment extends BaseNode {
354
+ kind: 'StructuralAssignment';
355
+ field: SchemaSymbol;
356
+ value: Expression;
357
+ options?: ObjectLiteral;
358
+ }
359
+ /**
360
+ * `UNSET STRUCTURAL { ("has_step", ?wrong_step) }` — remove references.
361
+ *
362
+ * Every SET has an UNSET. An entry is the SET STRUCTURAL entry without its
363
+ * options object; removal is per reference, ordered fields re-densify, and
364
+ * cardinality is validated at commit (Spec §17.5). Admitted where UNSET
365
+ * ATTRIBUTES is — UPSERT CONCEPT and UPDATE — never on record kinds.
366
+ */
367
+ export interface UnsetStructuralClause extends BaseNode {
368
+ kind: 'UnsetStructuralClause';
369
+ removals: StructuralRemoval[];
370
+ }
371
+ export interface StructuralRemoval extends BaseNode {
372
+ kind: 'StructuralRemoval';
373
+ field: SchemaSymbol;
374
+ value: Expression;
153
375
  }
376
+ export interface ExpectVersionClause extends BaseNode {
377
+ kind: 'ExpectVersionClause';
378
+ value: ScalarValue;
379
+ }
380
+ export interface ExpectStateClause extends BaseNode {
381
+ kind: 'ExpectStateClause';
382
+ value: ScalarValue;
383
+ }
384
+ /**
385
+ * `UPDATE` reaches mutable state only.
386
+ *
387
+ * Proposition tuples, Assertion epistemic payload, Evidence payload, terminal
388
+ * Activity topology, `_system` and Governance are all out of reach; `lower`
389
+ * rejects those targets rather than letting an engine discover them.
390
+ */
154
391
  export interface UpdateStatement extends BaseNode {
155
392
  kind: 'UpdateStatement';
156
- target: string;
157
- setAttributes?: SetAttributes;
158
- setMetadata?: SetMetadata;
159
- where: WhereClause;
393
+ target: TargetRef;
394
+ expectVersion?: ExpectVersionClause;
395
+ actions: UpdateAction[];
396
+ /**
397
+ * Binds a `?variable` target; a direct `:id` / `"id"` target already names
398
+ * the element and may omit it — the same rule as ARCHIVE, TOMBSTONE, PURGE,
399
+ * SET RETENTION and RETRACT ASSERTION (Spec §58).
400
+ */
401
+ where?: WhereClause;
160
402
  limit?: LimitClause;
161
403
  }
162
- export interface MergeStatement extends BaseNode {
163
- kind: 'MergeStatement';
164
- source: string;
165
- target: string;
166
- where: WhereClause;
404
+ export type UpdateAction = SetFieldsClause | SetAttributesClause | SetFacetClause | UnsetAttributesClause | UnsetFacetClause | SetStructuralClause | UnsetStructuralClause;
405
+ export interface RetractAssertionStatement extends BaseNode {
406
+ kind: 'RetractAssertionStatement';
407
+ target: TargetRef;
408
+ where?: WhereClause;
409
+ limit?: LimitClause;
410
+ expectState?: ExpectStateClause;
411
+ }
412
+ export interface SupersedeAssertionStatement extends BaseNode {
413
+ kind: 'SupersedeAssertionStatement';
414
+ target: TargetRef;
415
+ by: TargetRef;
416
+ expectState?: ExpectStateClause;
417
+ }
418
+ export interface CorrectEvidenceStatement extends BaseNode {
419
+ kind: 'CorrectEvidenceStatement';
420
+ target: TargetRef;
421
+ by: TargetRef;
422
+ expectState?: ExpectStateClause;
423
+ }
424
+ export interface TransitionActivityStatement extends BaseNode {
425
+ kind: 'TransitionActivityStatement';
426
+ target: TargetRef;
427
+ to: ScalarValue;
428
+ finalize: (SetFieldsClause | SetStructuralClause)[];
429
+ expectState?: ExpectStateClause;
430
+ }
431
+ export interface SetRetentionStatement extends BaseNode {
432
+ kind: 'SetRetentionStatement';
433
+ target: TargetRef;
434
+ assignments: ObjectLiteral;
435
+ where?: WhereClause;
436
+ limit?: LimitClause;
437
+ expectVersion?: ExpectVersionClause;
167
438
  }
168
- export interface DeleteStatement extends BaseNode {
169
- kind: 'DeleteStatement';
170
- deleteType: 'ATTRIBUTES' | 'METADATA' | 'PROPOSITIONS' | 'CONCEPT';
171
- /** For ATTRIBUTES/METADATA: the set of keys to delete */
172
- keys?: string[];
173
- /** The target variable */
174
- target: string;
175
- /** DETACH flag for CONCEPT deletion */
176
- detach?: boolean;
177
- where: WhereClause;
439
+ export interface ArchiveStatement extends BaseNode {
440
+ kind: 'ArchiveStatement';
441
+ target: TargetRef;
442
+ where?: WhereClause;
443
+ limit?: LimitClause;
444
+ expectState?: ExpectStateClause;
178
445
  }
446
+ export interface TombstoneStatement extends BaseNode {
447
+ kind: 'TombstoneStatement';
448
+ target: TargetRef;
449
+ where?: WhereClause;
450
+ limit?: LimitClause;
451
+ expectState?: ExpectStateClause;
452
+ }
453
+ /** Physical erasure. The grammar freezes the confirmation as `CONFIRM "PURGE"`. */
454
+ export interface PurgeStatement extends BaseNode {
455
+ kind: 'PurgeStatement';
456
+ target: TargetRef;
457
+ where?: WhereClause;
458
+ limit?: LimitClause;
459
+ referencePolicy?: ScalarValue;
460
+ confirm: StringLiteral;
461
+ }
462
+ /** Non-destructive: the source stays addressable as merged history. */
463
+ export interface MergeConceptStatement extends BaseNode {
464
+ kind: 'MergeConceptStatement';
465
+ source: TargetRef;
466
+ into: TargetRef;
467
+ where?: WhereClause;
468
+ expectVersion?: ExpectVersionClause;
469
+ }
470
+ export type DescribeTargetKind = 'PRIMER' | 'PROTOCOL' | 'EXECUTION_CONTEXT' | 'CAPABILITIES' | 'SPACE' | 'SCHEMA_ENVIRONMENT' | 'PACKAGE' | 'TYPE' | 'PREDICATE' | 'FACET' | 'STRUCTURAL_FIELD' | 'COMPATIBILITY' | 'ERROR' | 'TRANSACTION' | 'TRANSACTION_BY_IDEMPOTENCY_KEY' | 'SNAPSHOT' | 'CAPSULE' | 'EPISTEMIC_POLICY' | 'PROJECTION_CAPABILITY' | 'TRUST' | 'ACCESS';
179
471
  export interface DescribeStatement extends BaseNode {
180
472
  kind: 'DescribeStatement';
181
- describeType: 'PRIMER' | 'DOMAINS' | 'CONCEPT_TYPES' | 'CONCEPT_TYPE' | 'PROPOSITION_TYPES' | 'PROPOSITION_TYPE';
182
- /** The type/predicate name for specific describe */
183
- typeName?: string;
184
- /** Raw type/predicate value, preserving either quoted string or :parameter syntax. */
185
- typeNameValue?: StringLiteral | ParameterRef;
473
+ target: DescribeTargetKind;
474
+ /** The single operand, where the target takes one. */
475
+ value?: ScalarValue;
476
+ /** `DESCRIBE PRIMER MODE ...` */
477
+ mode?: ScalarValue;
478
+ /** `DESCRIBE COMPATIBILITY FROM ... TO ...` */
479
+ from?: ScalarValue;
480
+ to?: ScalarValue;
481
+ /** `DESCRIBE SCHEMA ENVIRONMENT` / `DESCRIBE SNAPSHOT` */
482
+ asOf?: AsOfClause;
483
+ /** `DESCRIBE ACCESS WITH {...}` */
484
+ with?: ObjectLiteral;
485
+ }
486
+ export type ListTargetKind = 'SPACES' | 'SCHEMA_PACKAGES' | 'TYPES' | 'PREDICATES' | 'FACETS' | 'STRUCTURAL_FIELDS' | 'EPISTEMIC_POLICIES';
487
+ export interface ListStatement extends BaseNode {
488
+ kind: 'ListStatement';
489
+ target: ListTargetKind;
490
+ /** `LIST SCHEMA PACKAGES STATUS ...` */
491
+ status?: ScalarValue;
186
492
  limit?: LimitClause;
187
493
  cursor?: CursorClause;
188
494
  }
495
+ export type SearchKind = 'CONCEPT' | 'PROPOSITION' | 'ASSERTION' | 'EVIDENCE' | 'ACTIVITY' | 'COGNITION';
496
+ /**
497
+ * Grounding only: a SEARCH score is not confidence, and a miss is not absence.
498
+ * The golden path is SEARCH → exact id → BELIEF/FIND.
499
+ */
189
500
  export interface SearchStatement extends BaseNode {
190
501
  kind: 'SearchStatement';
191
- searchTarget: 'CONCEPT' | 'PROPOSITION';
192
- term: string;
193
- /** Raw term value, preserving either quoted string or :parameter syntax. */
194
- termValue?: StringLiteral | ParameterRef;
195
- withType?: string;
196
- /** Raw WITH TYPE value, preserving either quoted string or :parameter syntax. */
197
- withTypeValue?: StringLiteral | ParameterRef;
198
- mode?: string;
199
- /** Raw MODE value, preserving either quoted string or :parameter syntax. */
200
- modeValue?: StringLiteral | ParameterRef;
201
- threshold?: ThresholdClause;
502
+ searchKind: SearchKind;
503
+ term: ScalarValue;
504
+ withType?: ScalarValue;
505
+ withPredicate?: ScalarValue;
506
+ mode?: ScalarValue;
507
+ threshold?: ScalarValue;
508
+ /** `AS OF SEQ ...` — historical index basis. */
509
+ asOfSeq?: ScalarValue;
202
510
  limit?: LimitClause;
511
+ cursor?: CursorClause;
203
512
  }
204
- export interface ThresholdClause extends BaseNode {
205
- kind: 'ThresholdClause';
206
- value: NumberLiteral | ParameterRef;
513
+ export type VerifyTargetKind = 'CAPSULE' | 'SCHEMA_PACKAGE' | 'RECEIPT' | 'BLOB' | 'CHECKPOINT';
514
+ export interface VerifyStatement extends BaseNode {
515
+ kind: 'VerifyStatement';
516
+ target: VerifyTargetKind;
517
+ value: ScalarValue;
518
+ }
519
+ export type ValidateTargetKind = 'KQL' | 'KML' | 'CAPSULE' | 'SCHEMA_PACKAGE' | 'IMPORT_PLAN';
520
+ export interface ValidateStatement extends BaseNode {
521
+ kind: 'ValidateStatement';
522
+ target: ValidateTargetKind;
523
+ value: ScalarValue;
524
+ options?: ObjectLiteral;
525
+ }
526
+ export interface PreviewStatement extends BaseNode {
527
+ kind: 'PreviewStatement';
528
+ target: 'KML' | 'IMPORT_CAPSULE';
529
+ value: ScalarValue;
530
+ /** `PREVIEW IMPORT CAPSULE ... INTO ...` */
531
+ into?: ScalarValue;
532
+ }
533
+ export interface HistoryStatement extends BaseNode {
534
+ kind: 'HistoryStatement';
535
+ target: 'ELEMENT' | 'SPACE';
536
+ /** Present for `HISTORY ELEMENT`. */
537
+ value?: ScalarValue;
538
+ fromSeq?: ScalarValue;
539
+ toSeq?: ScalarValue;
540
+ limit?: LimitClause;
541
+ cursor?: CursorClause;
207
542
  }
208
- export interface ExportStatement extends BaseNode {
209
- kind: 'ExportStatement';
210
- target: string;
211
- where: WhereClause;
543
+ export interface ChangesStatement extends BaseNode {
544
+ kind: 'ChangesStatement';
545
+ mode: 'SINCE' | 'AFTER_SEQ';
546
+ value: ScalarValue;
212
547
  limit?: LimitClause;
213
548
  }
214
- export type Expression = BinaryExpression | UnaryExpression | FunctionCallExpr | DotExpression | VariableRef | ParameterRef | StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral | ArrayLiteral | ObjectLiteral;
549
+ export interface SnapshotStatement extends BaseNode {
550
+ kind: 'SnapshotStatement';
551
+ asOf?: AsOfClause;
552
+ }
553
+ export interface ExportCapsuleStatement extends BaseNode {
554
+ kind: 'ExportCapsuleStatement';
555
+ target: TargetRef;
556
+ where: WhereClause;
557
+ options?: ObjectLiteral;
558
+ asOf?: AsOfClause;
559
+ }
560
+ export type Expression = BinaryExpression | UnaryExpression | FunctionCallExpr | AggregateExpr | FieldAccess | VariableRef | ParameterRef | StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral | ArrayLiteral | ObjectLiteral | ObjectPattern | PropositionTuple;
215
561
  export interface BinaryExpression extends BaseNode {
216
562
  kind: 'BinaryExpression';
217
563
  operator: string;
@@ -220,7 +566,7 @@ export interface BinaryExpression extends BaseNode {
220
566
  }
221
567
  export interface UnaryExpression extends BaseNode {
222
568
  kind: 'UnaryExpression';
223
- operator: '!';
569
+ operator: '!' | '-';
224
570
  operand: Expression;
225
571
  }
226
572
  export interface FunctionCallExpr extends BaseNode {
@@ -228,10 +574,32 @@ export interface FunctionCallExpr extends BaseNode {
228
574
  name: string;
229
575
  args: Expression[];
230
576
  }
231
- export interface DotExpression extends BaseNode {
232
- kind: 'DotExpression';
233
- object: Expression;
234
- property: string;
577
+ /** `COUNT(DISTINCT ?x)` and friends — legal in projection and sort positions. */
578
+ export interface AggregateExpr extends BaseNode {
579
+ kind: 'AggregateExpr';
580
+ name: string;
581
+ distinct: boolean;
582
+ argument: Expression;
583
+ }
584
+ /**
585
+ * `?x.facets["MnemonicState"].memory_strength` — a variable plus a dot path.
586
+ *
587
+ * Kept flat rather than as nested binary nodes because every consumer wants
588
+ * the path as a sequence, and `lower` emits exactly that.
589
+ */
590
+ export interface FieldAccess extends BaseNode {
591
+ kind: 'FieldAccess';
592
+ base: VariableRef;
593
+ steps: FieldStep[];
594
+ }
595
+ export type FieldStep = DotStep | IndexStep;
596
+ export interface DotStep extends BaseNode {
597
+ kind: 'DotStep';
598
+ name: string;
599
+ }
600
+ export interface IndexStep extends BaseNode {
601
+ kind: 'IndexStep';
602
+ key: StringLiteral;
235
603
  }
236
604
  export interface VariableRef extends BaseNode {
237
605
  kind: 'VariableRef';
@@ -261,10 +629,17 @@ export interface NullLiteral extends BaseNode {
261
629
  export interface ArrayLiteral extends BaseNode {
262
630
  kind: 'ArrayLiteral';
263
631
  elements: Expression[];
632
+ /**
633
+ * Whether a comma preceded the closing bracket. JSON-value position tolerates
634
+ * it; a FILTER list does not, and only the source says which was written.
635
+ */
636
+ trailingComma?: boolean;
264
637
  }
265
638
  export interface ObjectLiteral extends BaseNode {
266
639
  kind: 'ObjectLiteral';
267
640
  entries: ObjectEntry[];
641
+ /** See {@link ArrayLiteral.trailingComma}. */
642
+ trailingComma?: boolean;
268
643
  }
269
644
  export interface ObjectEntry extends BaseNode {
270
645
  kind: 'ObjectEntry';