bonescript-compiler 0.2.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 (146) hide show
  1. package/LICENSE +21 -0
  2. package/dist/algorithm_catalog.d.ts +32 -0
  3. package/dist/algorithm_catalog.js +323 -0
  4. package/dist/algorithm_catalog.js.map +1 -0
  5. package/dist/ast.d.ts +244 -0
  6. package/dist/ast.js +8 -0
  7. package/dist/ast.js.map +1 -0
  8. package/dist/cli.d.ts +4 -0
  9. package/dist/cli.js +605 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/emit_batch.d.ts +7 -0
  12. package/dist/emit_batch.js +133 -0
  13. package/dist/emit_batch.js.map +1 -0
  14. package/dist/emit_capability.d.ts +7 -0
  15. package/dist/emit_capability.js +376 -0
  16. package/dist/emit_capability.js.map +1 -0
  17. package/dist/emit_composition.d.ts +22 -0
  18. package/dist/emit_composition.js +184 -0
  19. package/dist/emit_composition.js.map +1 -0
  20. package/dist/emit_deploy.d.ts +9 -0
  21. package/dist/emit_deploy.js +191 -0
  22. package/dist/emit_deploy.js.map +1 -0
  23. package/dist/emit_events.d.ts +14 -0
  24. package/dist/emit_events.js +305 -0
  25. package/dist/emit_events.js.map +1 -0
  26. package/dist/emit_extras.d.ts +12 -0
  27. package/dist/emit_extras.js +234 -0
  28. package/dist/emit_extras.js.map +1 -0
  29. package/dist/emit_full.d.ts +13 -0
  30. package/dist/emit_full.js +273 -0
  31. package/dist/emit_full.js.map +1 -0
  32. package/dist/emit_maintenance.d.ts +16 -0
  33. package/dist/emit_maintenance.js +442 -0
  34. package/dist/emit_maintenance.js.map +1 -0
  35. package/dist/emit_runtime.d.ts +13 -0
  36. package/dist/emit_runtime.js +691 -0
  37. package/dist/emit_runtime.js.map +1 -0
  38. package/dist/emit_sourcemap.d.ts +29 -0
  39. package/dist/emit_sourcemap.js +123 -0
  40. package/dist/emit_sourcemap.js.map +1 -0
  41. package/dist/emit_tests.d.ts +15 -0
  42. package/dist/emit_tests.js +185 -0
  43. package/dist/emit_tests.js.map +1 -0
  44. package/dist/emit_websocket.d.ts +6 -0
  45. package/dist/emit_websocket.js +223 -0
  46. package/dist/emit_websocket.js.map +1 -0
  47. package/dist/emitter.d.ts +25 -0
  48. package/dist/emitter.js +511 -0
  49. package/dist/emitter.js.map +1 -0
  50. package/dist/extension_manager.d.ts +38 -0
  51. package/dist/extension_manager.js +170 -0
  52. package/dist/extension_manager.js.map +1 -0
  53. package/dist/formatter.d.ts +34 -0
  54. package/dist/formatter.js +317 -0
  55. package/dist/formatter.js.map +1 -0
  56. package/dist/index.d.ts +42 -0
  57. package/dist/index.js +113 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/ir.d.ts +168 -0
  60. package/dist/ir.js +10 -0
  61. package/dist/ir.js.map +1 -0
  62. package/dist/lexer.d.ts +195 -0
  63. package/dist/lexer.js +619 -0
  64. package/dist/lexer.js.map +1 -0
  65. package/dist/lowering.d.ts +25 -0
  66. package/dist/lowering.js +500 -0
  67. package/dist/lowering.js.map +1 -0
  68. package/dist/module_loader.d.ts +25 -0
  69. package/dist/module_loader.js +126 -0
  70. package/dist/module_loader.js.map +1 -0
  71. package/dist/optimizer.d.ts +26 -0
  72. package/dist/optimizer.js +158 -0
  73. package/dist/optimizer.js.map +1 -0
  74. package/dist/parse_decls.d.ts +13 -0
  75. package/dist/parse_decls.js +442 -0
  76. package/dist/parse_decls.js.map +1 -0
  77. package/dist/parse_decls2.d.ts +13 -0
  78. package/dist/parse_decls2.js +295 -0
  79. package/dist/parse_decls2.js.map +1 -0
  80. package/dist/parse_expr.d.ts +7 -0
  81. package/dist/parse_expr.js +197 -0
  82. package/dist/parse_expr.js.map +1 -0
  83. package/dist/parse_types.d.ts +6 -0
  84. package/dist/parse_types.js +51 -0
  85. package/dist/parse_types.js.map +1 -0
  86. package/dist/parser.d.ts +10 -0
  87. package/dist/parser.js +62 -0
  88. package/dist/parser.js.map +1 -0
  89. package/dist/parser_base.d.ts +19 -0
  90. package/dist/parser_base.js +50 -0
  91. package/dist/parser_base.js.map +1 -0
  92. package/dist/parser_recovery.d.ts +26 -0
  93. package/dist/parser_recovery.js +140 -0
  94. package/dist/parser_recovery.js.map +1 -0
  95. package/dist/scaffold.d.ts +13 -0
  96. package/dist/scaffold.js +376 -0
  97. package/dist/scaffold.js.map +1 -0
  98. package/dist/solver.d.ts +26 -0
  99. package/dist/solver.js +281 -0
  100. package/dist/solver.js.map +1 -0
  101. package/dist/typechecker.d.ts +52 -0
  102. package/dist/typechecker.js +534 -0
  103. package/dist/typechecker.js.map +1 -0
  104. package/dist/types.d.ts +38 -0
  105. package/dist/types.js +85 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/verifier.d.ts +46 -0
  108. package/dist/verifier.js +307 -0
  109. package/dist/verifier.js.map +1 -0
  110. package/package.json +52 -0
  111. package/src/algorithm_catalog.ts +345 -0
  112. package/src/ast.ts +334 -0
  113. package/src/cli.ts +624 -0
  114. package/src/emit_batch.ts +140 -0
  115. package/src/emit_capability.ts +436 -0
  116. package/src/emit_composition.ts +196 -0
  117. package/src/emit_deploy.ts +190 -0
  118. package/src/emit_events.ts +307 -0
  119. package/src/emit_extras.ts +240 -0
  120. package/src/emit_full.ts +309 -0
  121. package/src/emit_maintenance.ts +459 -0
  122. package/src/emit_runtime.ts +731 -0
  123. package/src/emit_sourcemap.ts +140 -0
  124. package/src/emit_tests.ts +205 -0
  125. package/src/emit_websocket.ts +229 -0
  126. package/src/emitter.ts +566 -0
  127. package/src/extension_manager.ts +187 -0
  128. package/src/formatter.ts +297 -0
  129. package/src/index.ts +88 -0
  130. package/src/ir.ts +215 -0
  131. package/src/lexer.ts +630 -0
  132. package/src/lowering.ts +556 -0
  133. package/src/module_loader.ts +114 -0
  134. package/src/optimizer.ts +196 -0
  135. package/src/parse_decls.ts +409 -0
  136. package/src/parse_decls2.ts +244 -0
  137. package/src/parse_expr.ts +197 -0
  138. package/src/parse_types.ts +54 -0
  139. package/src/parser.ts +64 -0
  140. package/src/parser_base.ts +57 -0
  141. package/src/parser_recovery.ts +153 -0
  142. package/src/scaffold.ts +375 -0
  143. package/src/solver.ts +330 -0
  144. package/src/typechecker.ts +591 -0
  145. package/src/types.ts +122 -0
  146. package/src/verifier.ts +348 -0
@@ -0,0 +1,556 @@
1
+ /**
2
+ * bone ir Lowering — Stage 4 of the compilation pipeline.
3
+ * Converts typed AST into the Architecture IR (spec/07_IR_SPEC.md).
4
+ *
5
+ * Rules:
6
+ * - One module per semantic component
7
+ * - All IDs are deterministic (derived from system name + component name)
8
+ * - All ontology-entailed fields are inserted
9
+ * - Effects are serialized in declaration order
10
+ */
11
+
12
+ import { createHash } from "crypto";
13
+ import * as AST from "./ast";
14
+ import * as IR from "./ir";
15
+
16
+ function toSnakeCase(s: string): string {
17
+ return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
18
+ }
19
+
20
+ // ─── Deterministic ID Generation ─────────────────────────────────────────────
21
+
22
+ function makeId(systemName: string, kind: string, name: string): string {
23
+ const input = `${systemName}.${kind}.${name}`;
24
+ return createHash("sha256").update(input).digest("hex").slice(0, 16);
25
+ }
26
+
27
+ // ─── Duration Parsing ────────────────────────────────────────────────────────
28
+
29
+ function parseDurationMs(dur: string | null): number | null {
30
+ if (!dur) return null;
31
+ const match = dur.match(/^(\d+)(ms|s|m|h|d)$/);
32
+ if (!match) return null;
33
+ const value = parseInt(match[1], 10);
34
+ switch (match[2]) {
35
+ case "ms": return value;
36
+ case "s": return value * 1000;
37
+ case "m": return value * 60_000;
38
+ case "h": return value * 3_600_000;
39
+ case "d": return value * 86_400_000;
40
+ default: return null;
41
+ }
42
+ }
43
+
44
+ // ─── Type Expression Serialization ───────────────────────────────────────────
45
+
46
+ function serializeType(t: AST.TypeExprNode): string {
47
+ switch (t.kind) {
48
+ case "PrimitiveType": return t.name;
49
+ case "GenericType": return `${t.name}<${t.typeArgs.map(serializeType).join(", ")}>`;
50
+ case "EntityRefType": return t.name;
51
+ case "TupleType": return `(${t.elements.map(serializeType).join(", ")})`;
52
+ case "UnionType": return t.members.map(serializeType).join(" | ");
53
+ }
54
+ }
55
+
56
+ // ─── Expression Serialization ────────────────────────────────────────────────
57
+
58
+ function serializeExpr(e: AST.ExprNode): string {
59
+ switch (e.kind) {
60
+ case "Literal":
61
+ if (e.type === "string") return `"${e.value}"`;
62
+ if (e.type === "list") return `[${(e.value as AST.ExprNode[]).map(serializeExpr).join(", ")}]`;
63
+ return String(e.value);
64
+ case "FieldRef":
65
+ return e.path.join(".");
66
+ case "BinaryExpr":
67
+ return `(${serializeExpr(e.left)} ${e.op} ${serializeExpr(e.right)})`;
68
+ case "UnaryExpr":
69
+ return `(${e.op} ${serializeExpr(e.operand)})`;
70
+ case "CallExpr":
71
+ return `${e.name}(${e.args.map(serializeExpr).join(", ")})`;
72
+ case "TernaryExpr":
73
+ return `(${serializeExpr(e.condition)} ? ${serializeExpr(e.consequent)} : ${serializeExpr(e.alternate)})`;
74
+ }
75
+ }
76
+
77
+ // ─── Lowering Engine ─────────────────────────────────────────────────────────
78
+
79
+ export class Lowering {
80
+ private systemName: string = "";
81
+
82
+ lower(program: AST.ProgramNode, sourceHash: string): IR.IRSystem[] {
83
+ const systems: IR.IRSystem[] = [];
84
+
85
+ for (const sys of program.systems) {
86
+ systems.push(this.lowerSystem(sys, sourceHash));
87
+ }
88
+
89
+ return systems;
90
+ }
91
+
92
+ private lowerSystem(sys: AST.SystemDeclNode, sourceHash: string): IR.IRSystem {
93
+ this.systemName = sys.name;
94
+
95
+ const modules: IR.IRModule[] = [];
96
+ const events: IR.IREvent[] = [];
97
+ const flows: IR.IRFlow[] = [];
98
+ const invariants: IR.IRInvariant[] = [];
99
+
100
+ // Collect declarations by type
101
+ const entities = sys.declarations.filter((d): d is AST.EntityDeclNode => d.kind === "EntityDecl");
102
+ const capabilities = sys.declarations.filter((d): d is AST.CapabilityDeclNode => d.kind === "CapabilityDecl");
103
+ const channels = sys.declarations.filter((d): d is AST.ChannelDeclNode => d.kind === "ChannelDecl");
104
+ const stores = sys.declarations.filter((d): d is AST.StoreDeclNode => d.kind === "StoreDecl");
105
+ const eventDecls = sys.declarations.filter((d): d is AST.EventDeclNode => d.kind === "EventDecl");
106
+ const constraints = sys.declarations.filter((d): d is AST.ConstraintDeclNode => d.kind === "ConstraintDecl");
107
+ const policies = sys.declarations.filter((d): d is AST.PolicyDeclNode => d.kind === "PolicyDecl");
108
+ const flowDecls = sys.declarations.filter((d): d is AST.FlowDeclNode => d.kind === "FlowDecl");
109
+ const extensionPoints = sys.declarations.filter((d): d is AST.ExtensionPointDeclNode => d.kind === "ExtensionPointDecl");
110
+
111
+ // Lower stores → data_store modules
112
+ for (const store of stores) {
113
+ modules.push(this.lowerStore(store));
114
+ }
115
+
116
+ // Lower entities → api_service modules (with CRUD + capabilities)
117
+ for (const entity of entities) {
118
+ const relatedCaps = capabilities.filter(c =>
119
+ c.params.some(p => p.type.kind === "EntityRefType" && p.type.name === entity.name)
120
+ );
121
+ modules.push(this.lowerEntity(entity, relatedCaps, stores));
122
+ }
123
+
124
+ // Lower channels → realtime_service modules
125
+ for (const channel of channels) {
126
+ modules.push(this.lowerChannel(channel));
127
+ }
128
+
129
+ // Lower events
130
+ for (const ev of eventDecls) {
131
+ events.push(this.lowerEvent(ev));
132
+ }
133
+
134
+ // Lower flows
135
+ for (const flow of flowDecls) {
136
+ flows.push(this.lowerFlow(flow));
137
+ }
138
+
139
+ // Lower constraints → invariants
140
+ for (const c of constraints) {
141
+ invariants.push({
142
+ id: makeId(this.systemName, "invariant", c.name),
143
+ expression: serializeExpr(c.expr),
144
+ scope: "global",
145
+ });
146
+ }
147
+
148
+ // Add gateway module (ontology: always present)
149
+ const gatewayConfig: Record<string, string | number | boolean> = {
150
+ rate_limit: 1000,
151
+ cors: true,
152
+ tls: true,
153
+ };
154
+ if (policies.length > 0) {
155
+ const mainPolicy = policies[0];
156
+ if (mainPolicy.rateLimit) {
157
+ gatewayConfig["rate_limit"] = mainPolicy.rateLimit.count;
158
+ }
159
+ if (mainPolicy.encryption) {
160
+ gatewayConfig["encryption"] = mainPolicy.encryption;
161
+ }
162
+ }
163
+ modules.push({
164
+ id: makeId(this.systemName, "gateway", "APIGateway"),
165
+ kind: "gateway",
166
+ name: "APIGateway",
167
+ interfaces: [],
168
+ models: [],
169
+ events: [],
170
+ state_machines: [],
171
+ relations: [],
172
+ dependencies: modules.filter(m => m.kind === "api_service" || m.kind === "realtime_service").map(m => m.id),
173
+ config: gatewayConfig,
174
+ });
175
+
176
+ return {
177
+ name: sys.name,
178
+ version: "1.0.0",
179
+ source_hash: sourceHash,
180
+ domain: sys.domain,
181
+ modules,
182
+ events,
183
+ flows,
184
+ invariants,
185
+ resolution: {},
186
+ extension_points: extensionPoints.map(ep => ({
187
+ name: ep.name,
188
+ params: ep.params.map(p => ({ name: p.name, type: serializeType(p.type) })),
189
+ returns: ep.returns ? serializeType(ep.returns) : null,
190
+ stable: ep.stable,
191
+ })),
192
+ };
193
+ }
194
+
195
+ // ─── Store Lowering ────────────────────────────────────────────────────────
196
+
197
+ private lowerStore(store: AST.StoreDeclNode): IR.IRModule {
198
+ // Strip "Store" suffix from model name so SellerStore → Seller → table "sellers"
199
+ const entityName = store.name.replace(/Store$/, "") || store.name;
200
+ const model: IR.IRModel = {
201
+ name: entityName,
202
+ fields: store.schema.map(f => this.lowerField(f)),
203
+ primary_key: "id",
204
+ indexes: [],
205
+ constraints: [],
206
+ };
207
+
208
+ // Add id field if not present
209
+ if (!model.fields.find(f => f.name === "id")) {
210
+ model.fields.unshift({
211
+ name: "id", type: "uuid", nullable: false, unique: true, indexed: true, default_value: "gen_random_uuid()",
212
+ });
213
+ }
214
+
215
+ return {
216
+ id: makeId(this.systemName, "data_store", store.name),
217
+ kind: "data_store",
218
+ name: store.name,
219
+ interfaces: [],
220
+ models: [model],
221
+ events: [],
222
+ state_machines: [],
223
+ relations: [],
224
+ dependencies: [],
225
+ config: {
226
+ engine: store.engine || "postgresql",
227
+ replicas: store.replicas || 1,
228
+ ...(store.retention ? { retention_ms: parseDurationMs(store.retention) || 0 } : {}),
229
+ ...(store.partition ? { partition_key: store.partition } : {}),
230
+ },
231
+ };
232
+ }
233
+
234
+ // ─── Entity Lowering ───────────────────────────────────────────────────────
235
+
236
+ private lowerEntity(entity: AST.EntityDeclNode, capabilities: AST.CapabilityDeclNode[], stores: AST.StoreDeclNode[]): IR.IRModule {
237
+ const moduleId = makeId(this.systemName, "api_service", `${entity.name}Service`);
238
+
239
+ // Build model from entity fields + ontology entailments
240
+ const fields: IR.IRField[] = [
241
+ { name: "id", type: "uuid", nullable: false, unique: true, indexed: true, default_value: "gen_random_uuid()" },
242
+ { name: "created_at", type: "timestamp", nullable: false, unique: false, indexed: true, default_value: "now()" },
243
+ { name: "updated_at", type: "timestamp", nullable: false, unique: false, indexed: false, default_value: "now()" },
244
+ ];
245
+ for (const f of entity.owns) {
246
+ fields.push(this.lowerField(f));
247
+ }
248
+
249
+ // Add derived fields as generated columns (stored: false = virtual)
250
+ const derivedFields: IR.IRField[] = entity.derived.map(d => ({
251
+ name: d.name,
252
+ type: "json", // type inferred at runtime
253
+ nullable: true,
254
+ unique: false,
255
+ indexed: false,
256
+ default_value: `GENERATED ALWAYS AS (${serializeExpr(d.expr)}) STORED`,
257
+ }));
258
+
259
+ // Build indexes from entity index declarations
260
+ const indexes: IR.IRIndex[] = [];
261
+ for (const idx of entity.indexes) {
262
+ indexes.push({ fields: idx, unique: false });
263
+ }
264
+
265
+ // Build constraints from entity constraints
266
+ const modelConstraints: IR.IRModelConstraint[] = [];
267
+ for (const c of entity.constraints) {
268
+ const serialized = serializeExpr(c);
269
+ // Detect unique constraints
270
+ if (c.kind === "FieldRef" && c.path[c.path.length - 1] === "unique") {
271
+ const field = c.path.slice(0, -1).join(".");
272
+ modelConstraints.push({ kind: "unique", target: field, params: {} });
273
+ indexes.push({ fields: [field], unique: true });
274
+ } else {
275
+ modelConstraints.push({ kind: "check", target: entity.name, params: { expression: serialized } });
276
+ }
277
+ }
278
+
279
+ const model: IR.IRModel = {
280
+ name: entity.name,
281
+ fields: [...fields, ...derivedFields],
282
+ primary_key: "id",
283
+ indexes,
284
+ constraints: modelConstraints,
285
+ };
286
+
287
+ // Build state machine if entity has states
288
+ const stateMachines: IR.IRStateMachine[] = [];
289
+ if (entity.states) {
290
+ const states = entity.states.nodes.map(n => n.name);
291
+ const transitions: IR.IRTransition[] = [];
292
+ for (const node of entity.states.nodes) {
293
+ for (const target of node.transitions) {
294
+ transitions.push({ from: node.name, to: target, trigger: `${node.name}_to_${target}`, guard: null });
295
+ }
296
+ for (const target of node.branches) {
297
+ transitions.push({ from: node.name, to: target, trigger: `${node.name}_to_${target}`, guard: null });
298
+ }
299
+ }
300
+ stateMachines.push({
301
+ entity: entity.name,
302
+ states,
303
+ initial: states[0],
304
+ transitions,
305
+ });
306
+ }
307
+
308
+ // Build interface from capabilities
309
+ const methods: IR.IRMethod[] = [];
310
+
311
+ // CRUD methods (always generated for entities)
312
+ methods.push(this.makeCrudMethod("create", entity.name, fields));
313
+ methods.push(this.makeCrudMethod("read", entity.name, fields));
314
+ methods.push(this.makeCrudMethod("update", entity.name, fields));
315
+ methods.push(this.makeCrudMethod("delete", entity.name, fields));
316
+ methods.push(this.makeCrudMethod("list", entity.name, fields));
317
+
318
+ // Capability-derived methods
319
+ for (const cap of capabilities) {
320
+ methods.push(this.lowerCapability(cap));
321
+ }
322
+
323
+ const iface: IR.IRInterface = {
324
+ name: `I${entity.name}Service`,
325
+ methods,
326
+ };
327
+
328
+ // Find related store
329
+ const relatedStore = stores.find(s => s.name.toLowerCase().includes(entity.name.toLowerCase()));
330
+ const deps: string[] = [];
331
+ if (relatedStore) {
332
+ deps.push(makeId(this.systemName, "data_store", relatedStore.name));
333
+ }
334
+
335
+ // Lower relations
336
+ const relations: IR.IRRelation[] = entity.relations.map(rel => {
337
+ const fromTable = toSnakeCase(entity.name) + "s";
338
+ const toTable = toSnakeCase(rel.target) + "s";
339
+ let foreignKey: string;
340
+ let junctionTable: string | undefined;
341
+
342
+ switch (rel.relationType) {
343
+ case "belongs_to":
344
+ foreignKey = toSnakeCase(rel.target) + "_id";
345
+ break;
346
+ case "has_one":
347
+ case "has_many":
348
+ foreignKey = toSnakeCase(entity.name) + "_id";
349
+ break;
350
+ case "many_to_many":
351
+ foreignKey = toSnakeCase(entity.name) + "_id";
352
+ junctionTable = [fromTable, toTable].sort().join("_");
353
+ break;
354
+ default:
355
+ foreignKey = toSnakeCase(rel.target) + "_id";
356
+ }
357
+
358
+ return {
359
+ name: rel.name,
360
+ kind: rel.relationType,
361
+ from_entity: entity.name,
362
+ to_entity: rel.target,
363
+ from_table: fromTable,
364
+ to_table: toTable,
365
+ foreign_key: foreignKey,
366
+ junction_table: junctionTable,
367
+ };
368
+ });
369
+
370
+ return {
371
+ id: moduleId,
372
+ kind: "api_service",
373
+ name: `${entity.name}Service`,
374
+ interfaces: [iface],
375
+ models: [model],
376
+ events: [],
377
+ state_machines: stateMachines,
378
+ relations,
379
+ dependencies: deps,
380
+ config: {
381
+ authenticated: entity.auth !== null && entity.auth !== "none",
382
+ auth_method: entity.auth || "none",
383
+ },
384
+ };
385
+ }
386
+
387
+ private makeCrudMethod(op: string, entityName: string, fields: IR.IRField[]): IR.IRMethod {
388
+ const input: IR.IRField[] = op === "create" || op === "update"
389
+ ? fields.filter(f => f.name !== "id" && f.name !== "created_at" && f.name !== "updated_at")
390
+ : op === "list"
391
+ ? [
392
+ { name: "page", type: "uint", nullable: true, unique: false, indexed: false, default_value: "1" },
393
+ { name: "page_size", type: "uint", nullable: true, unique: false, indexed: false, default_value: "50" },
394
+ ]
395
+ : [{ name: "id", type: "uuid", nullable: false, unique: true, indexed: true, default_value: null }];
396
+
397
+ return {
398
+ name: op,
399
+ input,
400
+ output: op === "list" ? `list<${entityName}>` : op === "delete" ? "bool" : entityName,
401
+ preconditions: [],
402
+ effects: [],
403
+ emissions: [],
404
+ idempotent: op === "read" || op === "list",
405
+ authenticated: true,
406
+ timeout_ms: 30000,
407
+ retry: null,
408
+ pipeline: null,
409
+ algorithm: null,
410
+ sync: null,
411
+ };
412
+ }
413
+
414
+ private lowerCapability(cap: AST.CapabilityDeclNode): IR.IRMethod {
415
+ const input: IR.IRField[] = cap.params.map(p => ({
416
+ name: p.name,
417
+ type: serializeType(p.type),
418
+ nullable: false,
419
+ unique: false,
420
+ indexed: false,
421
+ default_value: null,
422
+ }));
423
+
424
+ const preconditions: IR.IRPrecondition[] = cap.requires.map(r => ({
425
+ expression: serializeExpr(r),
426
+ description: serializeExpr(r),
427
+ }));
428
+
429
+ const effects: IR.IREffect[] = cap.effects.map(e => ({
430
+ target: e.target.path.join("."),
431
+ op: e.op === "=" ? "assign" as const : e.op === "+=" ? "add" as const : "remove" as const,
432
+ value: serializeExpr(e.value),
433
+ }));
434
+
435
+ const emissions = cap.emits.map(e => e.eventName);
436
+
437
+ // Lower pipeline if present
438
+ let pipeline: IR.IRPipeline | null = null;
439
+ if (cap.pipeline) {
440
+ pipeline = {
441
+ parallel: cap.pipeline.parallel,
442
+ steps: cap.pipeline.steps.map(step => ({
443
+ call_name: step.call.name,
444
+ call_args: step.call.args.map(a => serializeExpr(a)),
445
+ bind_as: step.bindAs,
446
+ })),
447
+ on_error: cap.pipeline.onError ? {
448
+ action: cap.pipeline.onError.action,
449
+ call_name: cap.pipeline.onError.call?.name || null,
450
+ call_args: cap.pipeline.onError.call?.args.map(a => serializeExpr(a)) || [],
451
+ } : null,
452
+ };
453
+ }
454
+
455
+ // Lower algorithm if present
456
+ let algorithm: IR.IRAlgorithm | null = null;
457
+ if (cap.algorithm) {
458
+ algorithm = {
459
+ catalog_name: cap.algorithm.name,
460
+ bindings: cap.algorithm.using.map(b => ({
461
+ param: b.param,
462
+ value: serializeExpr(b.value),
463
+ })),
464
+ };
465
+ }
466
+
467
+ return {
468
+ name: cap.name,
469
+ input,
470
+ output: cap.returns ? serializeType(cap.returns) : "result<void, error>",
471
+ preconditions,
472
+ effects,
473
+ emissions,
474
+ idempotent: cap.idempotent || false,
475
+ authenticated: true,
476
+ timeout_ms: parseDurationMs(cap.timeout) || 30000,
477
+ retry: cap.retry ? {
478
+ max_attempts: cap.retry.maxAttempts || 3,
479
+ backoff: (cap.retry.backoff as IR.IRRetryPolicy["backoff"]) || "exponential",
480
+ interval_ms: parseDurationMs(cap.retry.interval) || 1000,
481
+ } : null,
482
+ pipeline,
483
+ algorithm,
484
+ sync: cap.sync,
485
+ };
486
+ }
487
+
488
+ // ─── Channel Lowering ──────────────────────────────────────────────────────
489
+
490
+ private lowerChannel(channel: AST.ChannelDeclNode): IR.IRModule {
491
+ return {
492
+ id: makeId(this.systemName, "realtime_service", channel.name),
493
+ kind: "realtime_service",
494
+ name: channel.name,
495
+ interfaces: [{
496
+ name: `I${channel.name}Channel`,
497
+ methods: [
498
+ { name: "connect", input: [], output: "connection", preconditions: [], effects: [], emissions: [], idempotent: false, authenticated: true, timeout_ms: 5000, retry: null, pipeline: null, algorithm: null, sync: null },
499
+ { name: "subscribe", input: [{ name: "topic", type: "string", nullable: false, unique: false, indexed: false, default_value: null }], output: "subscription", preconditions: [], effects: [], emissions: [], idempotent: true, authenticated: true, timeout_ms: 5000, retry: null, pipeline: null, algorithm: null, sync: null },
500
+ { name: "publish", input: [{ name: "message", type: "json", nullable: false, unique: false, indexed: false, default_value: null }], output: "void", preconditions: [], effects: [], emissions: [], idempotent: false, authenticated: true, timeout_ms: 5000, retry: null, pipeline: null, algorithm: null, sync: null },
501
+ ],
502
+ }],
503
+ models: [],
504
+ events: [],
505
+ state_machines: [],
506
+ relations: [],
507
+ dependencies: [],
508
+ config: {
509
+ transport: channel.transport || "websocket",
510
+ ordering: channel.ordering || "fifo",
511
+ persistence: channel.persistence || "none",
512
+ max_size: channel.maxSize || 10000,
513
+ },
514
+ };
515
+ }
516
+
517
+ // ─── Event Lowering ────────────────────────────────────────────────────────
518
+
519
+ private lowerEvent(ev: AST.EventDeclNode): IR.IREvent {
520
+ return {
521
+ id: makeId(this.systemName, "event", ev.name),
522
+ name: ev.name,
523
+ payload: ev.payload.map(f => this.lowerField(f)),
524
+ source: "unknown", // resolved during dependency resolution
525
+ delivery: (ev.delivery as IR.IRDeliveryMode) || "at_least_once",
526
+ ordering: "fifo",
527
+ ttl_ms: parseDurationMs(ev.ttl),
528
+ };
529
+ }
530
+
531
+ // ─── Flow Lowering ─────────────────────────────────────────────────────────
532
+
533
+ private lowerFlow(flow: AST.FlowDeclNode): IR.IRFlow {
534
+ return {
535
+ name: flow.name,
536
+ steps: flow.steps.map(s => ({
537
+ name: s.name,
538
+ action: `${s.action.name}(${s.action.args.map(serializeExpr).join(", ")})`,
539
+ compensation: s.compensate ? `${s.compensate.name}(${s.compensate.args.map(serializeExpr).join(", ")})` : null,
540
+ })),
541
+ };
542
+ }
543
+
544
+ // ─── Field Lowering ────────────────────────────────────────────────────────
545
+
546
+ private lowerField(f: AST.FieldNode): IR.IRField {
547
+ return {
548
+ name: f.name,
549
+ type: serializeType(f.type),
550
+ nullable: f.type.kind === "GenericType" && f.type.name === "optional",
551
+ unique: false,
552
+ indexed: false,
553
+ default_value: f.defaultValue ? serializeExpr(f.defaultValue) : null,
554
+ };
555
+ }
556
+ }