@ldclabs/kip-lang 0.4.0 → 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 (54) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/LICENSE +21 -0
  3. package/README.md +89 -64
  4. package/dist/ast.d.ts +511 -145
  5. package/dist/ast.d.ts.map +1 -1
  6. package/dist/diagnostics.d.ts +8 -2
  7. package/dist/diagnostics.d.ts.map +1 -1
  8. package/dist/diagnostics.js +32 -3
  9. package/dist/diagnostics.js.map +1 -1
  10. package/dist/exec-ast.d.ts +514 -149
  11. package/dist/exec-ast.d.ts.map +1 -1
  12. package/dist/exec-ast.js +8 -7
  13. package/dist/exec-ast.js.map +1 -1
  14. package/dist/formatter.d.ts.map +1 -1
  15. package/dist/formatter.js +870 -479
  16. package/dist/formatter.js.map +1 -1
  17. package/dist/index.d.ts +4 -4
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/lexer.d.ts.map +1 -1
  22. package/dist/lexer.js +12 -30
  23. package/dist/lexer.js.map +1 -1
  24. package/dist/lower.d.ts +1 -2
  25. package/dist/lower.d.ts.map +1 -1
  26. package/dist/lower.js +1287 -598
  27. package/dist/lower.js.map +1 -1
  28. package/dist/parser.d.ts.map +1 -1
  29. package/dist/parser.js +2410 -1300
  30. package/dist/parser.js.map +1 -1
  31. package/dist/semantics.d.ts +11 -7
  32. package/dist/semantics.d.ts.map +1 -1
  33. package/dist/semantics.js +295 -180
  34. package/dist/semantics.js.map +1 -1
  35. package/dist/token.d.ts +130 -40
  36. package/dist/token.d.ts.map +1 -1
  37. package/dist/token.js +264 -83
  38. package/dist/token.js.map +1 -1
  39. package/dist/version.d.ts +2 -2
  40. package/dist/version.js +2 -2
  41. package/package.json +35 -5
  42. package/src/ast.ts +914 -0
  43. package/src/budget.ts +108 -0
  44. package/src/diagnostics.ts +182 -0
  45. package/src/errors.ts +42 -0
  46. package/src/exec-ast.ts +614 -0
  47. package/src/formatter.ts +1339 -0
  48. package/src/index.ts +226 -0
  49. package/src/lexer.ts +459 -0
  50. package/src/lower.ts +2011 -0
  51. package/src/parser.ts +3506 -0
  52. package/src/semantics.ts +392 -0
  53. package/src/token.ts +408 -0
  54. 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,122 +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
- /** `?local_handle`, absent when the block is not referenced elsewhere. */
110
- handle?: string;
111
- matcher: ConceptMatcher;
112
- expectVersion?: ExpectVersion;
113
- setAttributes?: SetAttributes;
114
- setPropositions?: SetPropositions;
115
- metadata?: WithMetadata;
116
- }
117
- export interface PropositionBlock extends BaseNode {
118
- kind: 'PropositionBlock';
119
- handle?: string;
120
- /** Match an existing proposition by id: (id: "...") */
121
- id?: StringLiteral | ParameterRef;
122
- subject?: PropositionEndpoint;
123
- predicate?: PredicateExpr;
124
- object?: PropositionEndpoint;
125
- expectVersion?: ExpectVersion;
126
- setAttributes?: SetAttributes;
127
- metadata?: WithMetadata;
128
- }
129
- export interface ExpectVersion extends BaseNode {
130
- kind: 'ExpectVersion';
131
- value: NumberLiteral | ParameterRef;
132
- }
133
- export interface SetAttributes extends BaseNode {
134
- kind: 'SetAttributes';
135
- entries: ObjectEntry[];
136
- }
137
- export interface SetPropositions extends BaseNode {
138
- kind: 'SetPropositions';
139
- items: PropositionItem[];
140
- }
141
- export interface PropositionItem extends BaseNode {
142
- kind: 'PropositionItem';
143
- predicate: string;
144
- target: PropositionEndpoint;
145
- metadata?: WithMetadata;
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;
146
227
  }
147
- export interface WithMetadata extends BaseNode {
148
- kind: 'WithMetadata';
149
- entries: ObjectEntry[];
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;
150
342
  }
151
- export interface SetMetadata extends BaseNode {
152
- kind: 'SetMetadata';
153
- 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;
154
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
+ */
155
391
  export interface UpdateStatement extends BaseNode {
156
392
  kind: 'UpdateStatement';
157
- target: string;
158
- setAttributes?: SetAttributes;
159
- setMetadata?: SetMetadata;
160
- 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;
161
402
  limit?: LimitClause;
162
403
  }
163
- export interface MergeStatement extends BaseNode {
164
- kind: 'MergeStatement';
165
- source: string;
166
- target: string;
167
- 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;
168
438
  }
169
- export interface DeleteStatement extends BaseNode {
170
- kind: 'DeleteStatement';
171
- deleteType: 'ATTRIBUTES' | 'METADATA' | 'PROPOSITIONS' | 'CONCEPT';
172
- /** For ATTRIBUTES/METADATA: the set of keys to delete */
173
- keys?: string[];
174
- /** The target variable */
175
- target: string;
176
- /** DETACH flag for CONCEPT deletion */
177
- detach?: boolean;
178
- where: WhereClause;
439
+ export interface ArchiveStatement extends BaseNode {
440
+ kind: 'ArchiveStatement';
441
+ target: TargetRef;
442
+ where?: WhereClause;
443
+ limit?: LimitClause;
444
+ expectState?: ExpectStateClause;
179
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';
180
471
  export interface DescribeStatement extends BaseNode {
181
472
  kind: 'DescribeStatement';
182
- describeType: 'PRIMER' | 'DOMAINS' | 'CONCEPT_TYPES' | 'CONCEPT_TYPE' | 'PROPOSITION_TYPES' | 'PROPOSITION_TYPE';
183
- /** The type/predicate name for specific describe */
184
- typeName?: string;
185
- /** Raw type/predicate value, preserving either quoted string or :parameter syntax. */
186
- 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;
187
492
  limit?: LimitClause;
188
493
  cursor?: CursorClause;
189
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
+ */
190
500
  export interface SearchStatement extends BaseNode {
191
501
  kind: 'SearchStatement';
192
- searchTarget: 'CONCEPT' | 'PROPOSITION';
193
- term: string;
194
- /** Raw term value, preserving either quoted string or :parameter syntax. */
195
- termValue?: StringLiteral | ParameterRef;
196
- withType?: string;
197
- /** Raw WITH TYPE value, preserving either quoted string or :parameter syntax. */
198
- withTypeValue?: StringLiteral | ParameterRef;
199
- mode?: string;
200
- /** Raw MODE value, preserving either quoted string or :parameter syntax. */
201
- modeValue?: StringLiteral | ParameterRef;
202
- 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;
203
510
  limit?: LimitClause;
511
+ cursor?: CursorClause;
204
512
  }
205
- export interface ThresholdClause extends BaseNode {
206
- kind: 'ThresholdClause';
207
- value: NumberLiteral | ParameterRef;
208
- }
209
- export interface ExportStatement extends BaseNode {
210
- kind: 'ExportStatement';
211
- target: string;
212
- where: WhereClause;
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;
213
540
  limit?: LimitClause;
214
541
  cursor?: CursorClause;
215
542
  }
216
- export type Expression = BinaryExpression | UnaryExpression | FunctionCallExpr | DotExpression | VariableRef | ParameterRef | StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral | ArrayLiteral | ObjectLiteral;
543
+ export interface ChangesStatement extends BaseNode {
544
+ kind: 'ChangesStatement';
545
+ mode: 'SINCE' | 'AFTER_SEQ';
546
+ value: ScalarValue;
547
+ limit?: LimitClause;
548
+ }
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;
217
561
  export interface BinaryExpression extends BaseNode {
218
562
  kind: 'BinaryExpression';
219
563
  operator: string;
@@ -222,7 +566,7 @@ export interface BinaryExpression extends BaseNode {
222
566
  }
223
567
  export interface UnaryExpression extends BaseNode {
224
568
  kind: 'UnaryExpression';
225
- operator: '!';
569
+ operator: '!' | '-';
226
570
  operand: Expression;
227
571
  }
228
572
  export interface FunctionCallExpr extends BaseNode {
@@ -230,10 +574,32 @@ export interface FunctionCallExpr extends BaseNode {
230
574
  name: string;
231
575
  args: Expression[];
232
576
  }
233
- export interface DotExpression extends BaseNode {
234
- kind: 'DotExpression';
235
- object: Expression;
236
- 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;
237
603
  }
238
604
  export interface VariableRef extends BaseNode {
239
605
  kind: 'VariableRef';