@cobol-ts/cobolts 0.0.13 → 0.0.16

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 (188) hide show
  1. package/README.benchmark-formats.md +52 -0
  2. package/README.benchmark-reporting.md +16 -0
  3. package/README.benchmark.md +199 -0
  4. package/README.cel.md +112 -57
  5. package/README.md +166 -19
  6. package/README.periodic-verification.md +83 -0
  7. package/dist/src/benchmark.access.spec.d.ts +1 -0
  8. package/dist/src/benchmark.access.spec.js +30 -0
  9. package/dist/src/benchmark.access.spec.js.map +1 -0
  10. package/dist/src/benchmark.aggregate.fixture.d.ts +10 -0
  11. package/dist/src/benchmark.aggregate.fixture.js +108 -0
  12. package/dist/src/benchmark.aggregate.fixture.js.map +1 -0
  13. package/dist/src/benchmark.aggregate.fixture.spec.d.ts +1 -0
  14. package/dist/src/benchmark.aggregate.fixture.spec.js +116 -0
  15. package/dist/src/benchmark.aggregate.fixture.spec.js.map +1 -0
  16. package/dist/src/benchmark.allocations.cli.spec.d.ts +1 -0
  17. package/dist/src/benchmark.allocations.cli.spec.js +60 -0
  18. package/dist/src/benchmark.allocations.cli.spec.js.map +1 -0
  19. package/dist/src/benchmark.allocations.d.ts +19 -0
  20. package/dist/src/benchmark.allocations.js +82 -0
  21. package/dist/src/benchmark.allocations.js.map +1 -0
  22. package/dist/src/benchmark.allocations.spec.d.ts +1 -0
  23. package/dist/src/benchmark.allocations.spec.js +64 -0
  24. package/dist/src/benchmark.allocations.spec.js.map +1 -0
  25. package/dist/src/benchmark.cli.functional.spec.d.ts +1 -0
  26. package/dist/src/benchmark.cli.functional.spec.js +370 -0
  27. package/dist/src/benchmark.cli.functional.spec.js.map +1 -0
  28. package/dist/src/benchmark.cli.golden.spec.d.ts +1 -0
  29. package/dist/src/benchmark.cli.golden.spec.js +194 -0
  30. package/dist/src/benchmark.cli.golden.spec.js.map +1 -0
  31. package/dist/src/benchmark.composed.execution.spec.d.ts +1 -0
  32. package/dist/src/benchmark.composed.execution.spec.js +120 -0
  33. package/dist/src/benchmark.composed.execution.spec.js.map +1 -0
  34. package/dist/src/benchmark.composed.fixture.d.ts +4 -0
  35. package/dist/src/benchmark.composed.fixture.js +77 -0
  36. package/dist/src/benchmark.composed.fixture.js.map +1 -0
  37. package/dist/src/benchmark.composed.fixture.spec.d.ts +1 -0
  38. package/dist/src/benchmark.composed.fixture.spec.js +50 -0
  39. package/dist/src/benchmark.composed.fixture.spec.js.map +1 -0
  40. package/dist/src/benchmark.composed.runner.d.ts +13 -0
  41. package/dist/src/benchmark.composed.runner.js +221 -0
  42. package/dist/src/benchmark.composed.runner.js.map +1 -0
  43. package/dist/src/benchmark.composed.two.fixture.d.ts +5 -0
  44. package/dist/src/benchmark.composed.two.fixture.js +111 -0
  45. package/dist/src/benchmark.composed.two.fixture.js.map +1 -0
  46. package/dist/src/benchmark.composed.two.spec.d.ts +1 -0
  47. package/dist/src/benchmark.composed.two.spec.js +86 -0
  48. package/dist/src/benchmark.composed.two.spec.js.map +1 -0
  49. package/dist/src/benchmark.format.cli.functional.spec.d.ts +1 -0
  50. package/dist/src/benchmark.format.cli.functional.spec.js +44 -0
  51. package/dist/src/benchmark.format.cli.functional.spec.js.map +1 -0
  52. package/dist/src/benchmark.format.composition.spec.d.ts +1 -0
  53. package/dist/src/benchmark.format.composition.spec.js +196 -0
  54. package/dist/src/benchmark.format.composition.spec.js.map +1 -0
  55. package/dist/src/benchmark.format.family.cli.functional.spec.d.ts +1 -0
  56. package/dist/src/benchmark.format.family.cli.functional.spec.js +102 -0
  57. package/dist/src/benchmark.format.family.cli.functional.spec.js.map +1 -0
  58. package/dist/src/benchmark.format.fixture.d.ts +26 -0
  59. package/dist/src/benchmark.format.fixture.js +181 -0
  60. package/dist/src/benchmark.format.fixture.js.map +1 -0
  61. package/dist/src/benchmark.format.oracle.d.ts +8 -0
  62. package/dist/src/benchmark.format.oracle.js +30 -0
  63. package/dist/src/benchmark.format.oracle.js.map +1 -0
  64. package/dist/src/benchmark.format.runner.d.ts +6 -0
  65. package/dist/src/benchmark.format.runner.js +271 -0
  66. package/dist/src/benchmark.format.runner.js.map +1 -0
  67. package/dist/src/benchmark.format.source.d.ts +6 -0
  68. package/dist/src/benchmark.format.source.js +15 -0
  69. package/dist/src/benchmark.format.source.js.map +1 -0
  70. package/dist/src/benchmark.format.spec.d.ts +1 -0
  71. package/dist/src/benchmark.format.spec.js +85 -0
  72. package/dist/src/benchmark.format.spec.js.map +1 -0
  73. package/dist/src/benchmark.format.stateful.spec.d.ts +1 -0
  74. package/dist/src/benchmark.format.stateful.spec.js +120 -0
  75. package/dist/src/benchmark.format.stateful.spec.js.map +1 -0
  76. package/dist/src/benchmark.gc.d.ts +26 -0
  77. package/dist/src/benchmark.gc.js +62 -0
  78. package/dist/src/benchmark.gc.js.map +1 -0
  79. package/dist/src/benchmark.gc.spec.d.ts +1 -0
  80. package/dist/src/benchmark.gc.spec.js +26 -0
  81. package/dist/src/benchmark.gc.spec.js.map +1 -0
  82. package/dist/src/benchmark.periodic.execution.spec.d.ts +1 -0
  83. package/dist/src/benchmark.periodic.execution.spec.js +166 -0
  84. package/dist/src/benchmark.periodic.execution.spec.js.map +1 -0
  85. package/dist/src/benchmark.periodic.fixture.d.ts +5 -0
  86. package/dist/src/benchmark.periodic.fixture.js +69 -0
  87. package/dist/src/benchmark.periodic.fixture.js.map +1 -0
  88. package/dist/src/benchmark.periodic.fixture.spec.d.ts +1 -0
  89. package/dist/src/benchmark.periodic.fixture.spec.js +96 -0
  90. package/dist/src/benchmark.periodic.fixture.spec.js.map +1 -0
  91. package/dist/src/benchmark.profile.d.ts +8 -0
  92. package/dist/src/benchmark.profile.js +62 -0
  93. package/dist/src/benchmark.profile.js.map +1 -0
  94. package/dist/src/benchmark.profile.spec.d.ts +1 -0
  95. package/dist/src/benchmark.profile.spec.js +65 -0
  96. package/dist/src/benchmark.profile.spec.js.map +1 -0
  97. package/dist/src/benchmark.report.d.ts +113 -0
  98. package/dist/src/benchmark.report.js +279 -0
  99. package/dist/src/benchmark.report.js.map +1 -0
  100. package/dist/src/benchmark.report.spec.d.ts +1 -0
  101. package/dist/src/benchmark.report.spec.js +271 -0
  102. package/dist/src/benchmark.report.spec.js.map +1 -0
  103. package/dist/src/benchmark.runner.d.ts +74 -0
  104. package/dist/src/benchmark.runner.js +371 -0
  105. package/dist/src/benchmark.runner.js.map +1 -0
  106. package/dist/src/benchmark.runner.spec.d.ts +1 -0
  107. package/dist/src/benchmark.runner.spec.js +241 -0
  108. package/dist/src/benchmark.runner.spec.js.map +1 -0
  109. package/dist/src/benchmark.strategy.spec.d.ts +1 -0
  110. package/dist/src/benchmark.strategy.spec.js +66 -0
  111. package/dist/src/benchmark.strategy.spec.js.map +1 -0
  112. package/dist/src/cel.composition.integration.spec.d.ts +1 -0
  113. package/dist/src/cel.composition.integration.spec.js +110 -0
  114. package/dist/src/cel.composition.integration.spec.js.map +1 -0
  115. package/dist/src/cel.entity.d.ts +12 -0
  116. package/dist/src/cel.entity.js +98 -0
  117. package/dist/src/cel.entity.js.map +1 -0
  118. package/dist/src/cel.entity.spec.d.ts +1 -0
  119. package/dist/src/cel.entity.spec.js +82 -0
  120. package/dist/src/cel.entity.spec.js.map +1 -0
  121. package/dist/src/cel.evaluate.cli.spec.d.ts +1 -0
  122. package/dist/src/cel.evaluate.cli.spec.js +63 -0
  123. package/dist/src/cel.evaluate.cli.spec.js.map +1 -0
  124. package/dist/src/cel.evaluate.integration.spec.d.ts +1 -0
  125. package/dist/src/cel.evaluate.integration.spec.js +128 -0
  126. package/dist/src/cel.evaluate.integration.spec.js.map +1 -0
  127. package/dist/src/cel.integration.spec.js +252 -34
  128. package/dist/src/cel.integration.spec.js.map +1 -1
  129. package/dist/src/cel.result.ownership.spec.d.ts +1 -0
  130. package/dist/src/cel.result.ownership.spec.js +52 -0
  131. package/dist/src/cel.result.ownership.spec.js.map +1 -0
  132. package/dist/src/cel.shared.execution.d.ts +20 -0
  133. package/dist/src/cel.shared.execution.js +49 -0
  134. package/dist/src/cel.shared.execution.js.map +1 -0
  135. package/dist/src/cel.stats.d.ts +5 -0
  136. package/dist/src/cel.stats.js +73 -0
  137. package/dist/src/cel.stats.js.map +1 -0
  138. package/dist/src/cel.stats.spec.d.ts +1 -0
  139. package/dist/src/cel.stats.spec.js +85 -0
  140. package/dist/src/cel.stats.spec.js.map +1 -0
  141. package/dist/src/cli.integration.spec.js +14 -2
  142. package/dist/src/cli.integration.spec.js.map +1 -1
  143. package/dist/src/cli.js +8 -0
  144. package/dist/src/cli.js.map +1 -1
  145. package/dist/src/cli.spec.js +46 -6
  146. package/dist/src/cli.spec.js.map +1 -1
  147. package/dist/src/commands/benchmark.d.ts +3 -0
  148. package/dist/src/commands/benchmark.js +300 -0
  149. package/dist/src/commands/benchmark.js.map +1 -0
  150. package/dist/src/commands/cel.d.ts +1 -1
  151. package/dist/src/commands/cel.js +38 -5
  152. package/dist/src/commands/cel.js.map +1 -1
  153. package/dist/src/commands/characteristics.d.ts +3 -0
  154. package/dist/src/commands/characteristics.js +57 -0
  155. package/dist/src/commands/characteristics.js.map +1 -0
  156. package/dist/src/commands/characteristics.spec.d.ts +1 -0
  157. package/dist/src/commands/characteristics.spec.js +109 -0
  158. package/dist/src/commands/characteristics.spec.js.map +1 -0
  159. package/dist/src/commands/config.validate.js +8 -4
  160. package/dist/src/commands/config.validate.js.map +1 -1
  161. package/dist/src/commands/extract.d.ts +14 -0
  162. package/dist/src/commands/extract.js +85 -0
  163. package/dist/src/commands/extract.js.map +1 -0
  164. package/dist/src/commands/extract.spec.d.ts +1 -0
  165. package/dist/src/commands/extract.spec.js +179 -0
  166. package/dist/src/commands/extract.spec.js.map +1 -0
  167. package/dist/src/commands/periodicity.d.ts +3 -0
  168. package/dist/src/commands/periodicity.js +11 -0
  169. package/dist/src/commands/periodicity.js.map +1 -0
  170. package/dist/src/commands/validate.all.d.ts +7 -0
  171. package/dist/src/commands/validate.all.integration.spec.d.ts +1 -0
  172. package/dist/src/commands/validate.all.integration.spec.js +43 -0
  173. package/dist/src/commands/validate.all.integration.spec.js.map +1 -0
  174. package/dist/src/commands/validate.all.js +75 -0
  175. package/dist/src/commands/validate.all.js.map +1 -0
  176. package/dist/src/commands/validate.all.spec.d.ts +1 -0
  177. package/dist/src/commands/validate.all.spec.js +70 -0
  178. package/dist/src/commands/validate.all.spec.js.map +1 -0
  179. package/dist/src/context.d.ts +37 -6
  180. package/dist/src/context.js +580 -23
  181. package/dist/src/context.js.map +1 -1
  182. package/dist/src/indexed.accumulator.d.ts +18 -0
  183. package/dist/src/indexed.accumulator.js +102 -0
  184. package/dist/src/indexed.accumulator.js.map +1 -0
  185. package/dist/src/indexed.accumulator.spec.d.ts +1 -0
  186. package/dist/src/indexed.accumulator.spec.js +74 -0
  187. package/dist/src/indexed.accumulator.spec.js.map +1 -0
  188. package/package.json +24 -11
@@ -3,18 +3,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.nodeCliServices = void 0;
4
4
  exports.discoverPaths = discoverPaths;
5
5
  exports.namingFor = namingFor;
6
+ exports.prepareIndexedComposedExecutor = prepareIndexedComposedExecutor;
7
+ const promises_1 = require("node:fs/promises");
8
+ const spreadsheet_1 = require("@cobol-ts/spreadsheet");
9
+ const spreadsheet_excel_1 = require("@cobol-ts/spreadsheet-excel");
6
10
  const node_path_1 = require("node:path");
11
+ const node_perf_hooks_1 = require("node:perf_hooks");
7
12
  const files_1 = require("@cobol-ts/files");
8
13
  const file_catalogue_1 = require("@cobol-ts/file-catalogue");
9
14
  const codegen_1 = require("@cobol-ts/codegen");
10
15
  const file_display_1 = require("@cobol-ts/file-display");
11
- const file_display_2 = require("@cobol-ts/file-display");
12
16
  const cursor_loader_cobol_1 = require("@cobol-ts/cursor-loader-cobol");
13
17
  const cel_access_1 = require("@cobol-ts/cel-access");
14
18
  const cel_graph_runtime_1 = require("@cobol-ts/cel-graph-runtime");
19
+ const planned_graph_1 = require("@cobol-ts/planned_graph");
15
20
  const cel_json_1 = require("./cel.json");
21
+ const cel_stats_1 = require("./cel.stats");
22
+ const cel_entity_1 = require("./cel.entity");
16
23
  const errors_1 = require("@cobol-ts/errors");
17
24
  const yaml_jsyaml_1 = require("@cobol-ts/yaml-jsyaml");
25
+ const cursor_loader_source_1 = require("@cobol-ts/cursor-loader-source");
26
+ const cursor_loader_indexed_graph_state_1 = require("@cobol-ts/cursor-loader-indexed-graph-state");
27
+ const composition_definition_1 = require("@cobol-ts/composition-definition");
28
+ const indexed_accumulator_1 = require("./indexed.accumulator");
18
29
  async function discoverPaths(options) {
19
30
  const startDirectory = options.directory ?? options.root ?? process.cwd();
20
31
  const directory = await (0, files_1.findDirectoryUp)('.cobolts', (0, node_path_1.resolve)(startDirectory));
@@ -68,18 +79,176 @@ async function loadInitialAccumulator(paths) {
68
79
  catch (error) {
69
80
  return { errors: [`Reading accumulator ${paths.accumulator}: ${error instanceof Error ? error.message : String(error)}`] };
70
81
  }
71
- const result = (0, cel_graph_runtime_1.parseInitialAccumulator)(source, yaml_jsyaml_1.jsYamlParser);
72
- return 'errors' in result
73
- ? { errors: result.errors.map(message => `${paths.accumulator}: ${message}`) }
74
- : result;
82
+ try {
83
+ return { value: (0, indexed_accumulator_1.parseAccumulatorDocument)(source).initial };
84
+ }
85
+ catch (error) {
86
+ return { errors: [paths.accumulator + ': ' + (error instanceof Error ? error.message : String(error))] };
87
+ }
88
+ }
89
+ function compileIndexedComposedExecutor(paths, authored, initial, stateConfiguration, prepared) {
90
+ const graph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored, prepared.composed.projection, prepared.access);
91
+ if ('errors' in graph)
92
+ return graph;
93
+ const schema = graph.value.schema;
94
+ const initialValues = (0, indexed_accumulator_1.indexedSeedValues)(initial);
95
+ const stateFile = stateConfiguration.file ? (0, node_path_1.resolve)(paths.directory, stateConfiguration.file) : undefined;
96
+ // Accumulator preparation has the same lifetime as the compiled executor.
97
+ // Each run still opens fresh composition and historical-state cursors.
98
+ const scanAccumulator = (0, cursor_loader_indexed_graph_state_1.prepareIndexedAccumulator)({
99
+ schema, optional: stateConfiguration.optional,
100
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType,
101
+ initial: initialValues,
102
+ ...(stateFile ? { filename: stateFile } : {})
103
+ });
104
+ // The session is stateful and execution must be sequential, not concurrent.
105
+ const normalSession = graph.value.createIndexedSession();
106
+ const indices = [];
107
+ return { value: async (options, emit, diagnostic = message => console.error(message)) => {
108
+ const measurement = options.measurement;
109
+ const target = options.entityId;
110
+ const debugging = !!options.debug || options.debugNode !== undefined;
111
+ const count = options.all ? undefined : (options.count ?? 10);
112
+ const skip = options.skip ?? 0;
113
+ const graphTiming = measurement?.timingSampleEvery === undefined ? undefined :
114
+ (0, cel_graph_runtime_1.createCelGraphTimings)(graph.value.generations, measurement.timingSampleEvery);
115
+ if (measurement)
116
+ measurement.graphTiming = graphTiming;
117
+ const session = graphTiming || debugging
118
+ ? graph.value.createIndexedSession(graphTiming, debugging ? (0, cel_entity_1.debugGenerations)(graph.value.generations, schema, diagnostic, options.debugNode) : undefined)
119
+ : normalSession;
120
+ const trace = (label, value) => {
121
+ if (!debugging)
122
+ return;
123
+ if (options.debugNode !== undefined && (label === 'PREVIOUS' || label === 'RESULT')) {
124
+ const state = value;
125
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)({ [options.debugNode]: state[options.debugNode] }));
126
+ }
127
+ else
128
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)(value));
129
+ };
130
+ const variables = options.periodicity === undefined ? {} : { periodicity: options.periodicity };
131
+ let visited = 0, emitted = 0;
132
+ if (!options.raw && target === undefined)
133
+ emit('# ' + paths.catalogue);
134
+ try {
135
+ if (target !== undefined && !stateFile) {
136
+ await prepared.composed.scan(row => {
137
+ if ((0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
138
+ return true;
139
+ const state = (0, cel_entity_1.indexedStateFromInitial)(schema, initial);
140
+ state.entityKey = String(row.key);
141
+ if (debugging) {
142
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row));
143
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(schema, state));
144
+ }
145
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
146
+ session.run(row, state, variables, indices);
147
+ if (measurement) {
148
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
149
+ measurement.records++;
150
+ }
151
+ const result = (0, cel_entity_1.indexedStateObject)(schema, state);
152
+ emit((0, cel_json_1.stringifyCelJson)(result));
153
+ trace('RESULT', result);
154
+ emitted++;
155
+ return false;
156
+ });
157
+ return emitted === 0 ? { errors: [`Entity ${target} not found`] } : { value: undefined };
158
+ }
159
+ const normalizeKey = (0, indexed_accumulator_1.indexedKeyNormalizer)(stateConfiguration.customerIdType, stateConfiguration.customerId);
160
+ await scanAccumulator(prepared.composed, row => normalizeKey(row.key), (row, state) => {
161
+ visited++;
162
+ if (target !== undefined) {
163
+ if ((0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
164
+ return true;
165
+ }
166
+ else if (visited <= skip)
167
+ return true;
168
+ if (debugging) {
169
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row));
170
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(schema, state));
171
+ }
172
+ try {
173
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
174
+ session.run(row, state, variables, indices);
175
+ if (measurement) {
176
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
177
+ measurement.records++;
178
+ }
179
+ }
180
+ catch (error) {
181
+ throw new Error('Record ' + visited + ': ' + (error instanceof Error ? error.message : String(error)), { cause: error });
182
+ }
183
+ options.observeIndexed?.(row.key, state.values, schema);
184
+ if (!options.suppressOutput || debugging) {
185
+ const result = (0, cel_entity_1.indexedStateObject)(schema, state);
186
+ if (!options.suppressOutput)
187
+ emit((0, cel_json_1.stringifyCelJson)(result));
188
+ trace('RESULT', result);
189
+ }
190
+ emitted++;
191
+ return target === undefined && (count === undefined || emitted < count);
192
+ });
193
+ if (target !== undefined && emitted === 0)
194
+ return { errors: [`Entity ${target} not found`] };
195
+ return { value: undefined };
196
+ }
197
+ catch (error) {
198
+ return { errors: ['Composed CEL: ' + (error instanceof Error ? error.message : String(error))] };
199
+ }
200
+ } };
201
+ }
202
+ /** Compile once; execute repeatedly. This is a factory, not a cache. */
203
+ async function prepareIndexedComposedExecutor(paths, graphName) {
204
+ const authored = await loadCelAuthoringGraph(paths, graphName);
205
+ if ('errors' in authored)
206
+ return authored;
207
+ const canonical = (0, cel_graph_runtime_1.canonicaliseCelGraph)(authored.value);
208
+ if ('errors' in canonical)
209
+ return canonical;
210
+ const sorted = (0, cel_graph_runtime_1.planCanonicalCelGraph)(canonical.value);
211
+ if ('errors' in sorted)
212
+ return sorted;
213
+ const initial = await loadInitialAccumulator(paths);
214
+ if ('errors' in initial)
215
+ return initial;
216
+ const checked = (0, cel_graph_runtime_1.validateInitialAccumulator)(initial.value, authored.value, canonical.value);
217
+ if ('errors' in checked)
218
+ return checked;
219
+ let stateConfiguration;
220
+ try {
221
+ stateConfiguration = (await (0, indexed_accumulator_1.readAccumulatorDocument)(paths.accumulator)).state;
222
+ }
223
+ catch (error) {
224
+ return { errors: [`Accumulator configuration: ${error instanceof Error ? error.message : String(error)}`] };
225
+ }
226
+ if (!await (0, composition_definition_1.hasCompositionDefinition)(paths.directory))
227
+ return { errors: ['Prepared indexed CEL execution requires a composition definition'] };
228
+ try {
229
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
230
+ const prepared = await (0, composition_definition_1.prepareCompositionDefinition)(definition, logicalName => (0, cursor_loader_source_1.prepareConfiguredFileSource)({ configDirectory: paths.directory, logicalName }));
231
+ return compileIndexedComposedExecutor(paths, authored.value, checked.value, { ...stateConfiguration, strategy: 'indexed' }, prepared);
232
+ }
233
+ catch (error) {
234
+ return { errors: ['Composed CEL: ' + (error instanceof Error ? error.message : String(error))] };
235
+ }
75
236
  }
76
237
  exports.nodeCliServices = {
77
238
  output: message => console.log(message),
239
+ diagnostic: message => console.error(message),
240
+ async write(filename, content) {
241
+ await (0, promises_1.mkdir)((0, node_path_1.dirname)(filename), { recursive: true });
242
+ await (0, promises_1.writeFile)(filename, content, 'utf8');
243
+ },
244
+ extractSpreadsheet: config => (0, spreadsheet_1.extractSpreadsheet)(spreadsheet_excel_1.excelSpreadsheetReader, config),
78
245
  read: filename => files_1.nodeTextFiles.read(filename),
79
246
  paths: discoverPaths,
80
247
  async list(paths) {
81
248
  const catalogue = (0, errors_1.valueOrThrow)(await (0, file_catalogue_1.readFileCatalogue)(files_1.nodeTextFiles, paths.catalogue, yaml_jsyaml_1.jsYamlParser));
82
- const result = [paths.catalogue, paths.policy];
249
+ const result = [paths.catalogue];
250
+ if (await files_1.nodeTextFiles.exists(paths.policy))
251
+ result.push(paths.policy);
83
252
  if (await files_1.nodeTextFiles.exists(paths.data))
84
253
  result.push(paths.data);
85
254
  if (await files_1.nodeTextFiles.exists(paths.accumulator))
@@ -124,18 +293,67 @@ exports.nodeCliServices = {
124
293
  const loaded = await loadCelAuthoringGraph(paths, options.graph);
125
294
  if ('errors' in loaded)
126
295
  return loaded;
127
- if (options.stage === 'graph')
296
+ if (options.stage === 'graph' && !options.short)
128
297
  return { value: JSON.stringify(loaded.value, null, 2) };
129
298
  const canonical = (0, cel_graph_runtime_1.canonicaliseCelGraph)(loaded.value);
130
299
  if ('errors' in canonical)
131
300
  return canonical;
301
+ const shortNodes = () => canonical.value.map(node => `${node.metadata.name} <- ${node.dependsOn.map(path => path.join('.')).join(', ') || '-'}`).join('\n');
302
+ if (options.stage === 'graph' && options.short)
303
+ return { value: shortNodes() };
304
+ if (options.stage === 'stats') {
305
+ const metrics = { binaryResolutions: 0, dynamicBinaryResolutions: 0, dynamicConditionalResolutions: 0 };
306
+ const compile = (projection, access) => (0, cel_graph_runtime_1.prepareCelGraph)(loaded.value, projection, access, {}, {}, metrics);
307
+ if (options.logicalName !== undefined) {
308
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
309
+ configDirectory: paths.directory, logicalName: options.logicalName,
310
+ catalogue: paths.catalogue, data: paths.data
311
+ }, files_1.nodeTextFiles, yaml_jsyaml_1.jsYamlParser);
312
+ if ('errors' in source)
313
+ return source;
314
+ const details = await (0, cursor_loader_cobol_1.prepareCobolFileDetails)({
315
+ copybook: source.value.copybook,
316
+ ...(source.value.framing ? { framing: source.value.framing } : {})
317
+ });
318
+ const compiled = compile(source.value.projection, (0, cel_access_1.cobolAccessCompiler)(details, source.value.projection));
319
+ if ('errors' in compiled)
320
+ return compiled;
321
+ }
322
+ else if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
323
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
324
+ const composed = await (0, composition_definition_1.prepareCompositionDefinition)(definition, logicalName => (0, cursor_loader_source_1.prepareConfiguredFileSource)({ configDirectory: paths.directory, logicalName }));
325
+ const compiled = compile(composed.composed.projection, composed.access);
326
+ if ('errors' in compiled)
327
+ return compiled;
328
+ }
329
+ else {
330
+ const empty = {};
331
+ const compiled = compile(empty, (0, cel_access_1.jsonAccessCompiler)(empty));
332
+ if ('errors' in compiled)
333
+ return compiled;
334
+ }
335
+ return (0, cel_stats_1.celGraphStats)(canonical.value, metrics);
336
+ }
132
337
  if (options.stage === 'canonical')
133
- return { value: JSON.stringify(canonical.value, null, 2) };
338
+ return { value: options.short
339
+ ? shortNodes()
340
+ : JSON.stringify(canonical.value, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
134
341
  const sorted = (0, cel_graph_runtime_1.planCanonicalCelGraph)(canonical.value);
135
342
  if ('errors' in sorted)
136
343
  return sorted;
137
344
  if (options.stage === 'sorted') {
138
- return { value: JSON.stringify({ generations: sorted.value.generations }, null, 2) };
345
+ return { value: options.short
346
+ ? sorted.value.generations.map((nodes, index) => `G${index}: ${nodes.map(node => `${node.metadata.name}(${node.dependsOn.map(path => path.join('.')).join(', ') || '-'})`).join(' ')}`).join('\n')
347
+ : JSON.stringify({ generations: sorted.value.generations }, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
348
+ }
349
+ if (options.stage === 'planned') {
350
+ let index = 0;
351
+ const planned = (0, planned_graph_1.makePlannedGraph)(sorted.value.generations, sorted.value.graph, (node, downstream) => ({ index: index++, node, downstream }));
352
+ if ('errors' in planned)
353
+ return planned;
354
+ return { value: options.short
355
+ ? planned.value.map((generation, i) => `G${i}: ${generation.map(n => `${n.index}:${n.node.metadata.name}->[${n.downstream.join(',')}]`).join(' ')}`).join('\n')
356
+ : JSON.stringify({ generations: planned.value }, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
139
357
  }
140
358
  if (options.stage !== 'validate')
141
359
  return { errors: [`Unexpected CEL operation: ${options.stage}`] };
@@ -145,10 +363,25 @@ exports.nodeCliServices = {
145
363
  const checked = (0, cel_graph_runtime_1.validateInitialAccumulator)(initial.value, loaded.value, canonical.value);
146
364
  if ('errors' in checked)
147
365
  return checked;
366
+ if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
367
+ try {
368
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
369
+ const composed = await (0, composition_definition_1.prepareCompositionDefinition)(definition, logicalName => (0, cursor_loader_source_1.prepareConfiguredFileSource)({ configDirectory: paths.directory, logicalName }));
370
+ const graph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(loaded.value, composed.composed.projection, composed.access);
371
+ if ('errors' in graph)
372
+ return graph;
373
+ return { value: 'Valid composed graph: ' + graph.value.schema.nodes.length +
374
+ ' nodes, ' + composed.sources.length + ' sources' };
375
+ }
376
+ catch (error) {
377
+ return { errors: ['Composed CEL validation: ' +
378
+ (error instanceof Error ? error.message : String(error))] };
379
+ }
380
+ }
148
381
  if (options.logicalName === undefined) {
149
382
  return { value: `Valid graph: ${canonical.value.length} nodes, ${sorted.value.generations.length} generations (structural)` };
150
383
  }
151
- const source = await (0, file_display_2.openConfiguredCobolFile)({
384
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
152
385
  configDirectory: paths.directory, logicalName: options.logicalName,
153
386
  catalogue: paths.catalogue, data: paths.data
154
387
  }, files_1.nodeTextFiles, yaml_jsyaml_1.jsYamlParser);
@@ -168,7 +401,27 @@ exports.nodeCliServices = {
168
401
  return { errors: [`CEL validation ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
169
402
  }
170
403
  },
171
- async celExecute(paths, options, emit) {
404
+ async celExecute(paths, options, emit, diagnostic = message => console.error(message)) {
405
+ const variables = options.periodicity === undefined ? {} : { periodicity: options.periodicity };
406
+ let requestedEntity;
407
+ if (options.entityId !== undefined) {
408
+ requestedEntity = options.entityId;
409
+ if (options.skip !== undefined || options.count !== undefined || options.all || options.onError !== undefined)
410
+ return { errors: ['CEL evaluate cannot combine an entity ID with count, skip, all or on-error'] };
411
+ }
412
+ const measurement = options.measurement;
413
+ const target = requestedEntity;
414
+ const debugging = options.debug || options.debugNode !== undefined;
415
+ const trace = (label, value) => {
416
+ if (!debugging)
417
+ return;
418
+ if (options.debugNode !== undefined && (label === 'PREVIOUS' || label === 'RESULT')) {
419
+ const state = value;
420
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)({ [options.debugNode]: state[options.debugNode] }));
421
+ }
422
+ else
423
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)(value));
424
+ };
172
425
  const count = options.all ? undefined : (options.count ?? 10);
173
426
  const skip = options.skip ?? 0;
174
427
  if (options.all && options.count !== undefined)
@@ -182,6 +435,8 @@ exports.nodeCliServices = {
182
435
  const authored = await loadCelAuthoringGraph(paths, options.graph);
183
436
  if ('errors' in authored)
184
437
  return authored;
438
+ if (options.debugNode !== undefined && !Object.prototype.hasOwnProperty.call(authored.value, options.debugNode))
439
+ return { errors: [`Unknown debug node ${options.debugNode}`] };
185
440
  const canonical = (0, cel_graph_runtime_1.canonicaliseCelGraph)(authored.value);
186
441
  if ('errors' in canonical)
187
442
  return canonical;
@@ -194,7 +449,293 @@ exports.nodeCliServices = {
194
449
  const checked = (0, cel_graph_runtime_1.validateInitialAccumulator)(initial.value, authored.value, canonical.value);
195
450
  if ('errors' in checked)
196
451
  return checked;
197
- const source = await (0, file_display_2.openConfiguredCobolFile)({
452
+ let stateConfiguration;
453
+ try {
454
+ stateConfiguration = (await (0, indexed_accumulator_1.readAccumulatorDocument)(paths.accumulator)).state;
455
+ }
456
+ catch (error) {
457
+ return { errors: [`Accumulator configuration: ${error instanceof Error ? error.message : String(error)}`] };
458
+ }
459
+ if (options.strategyOverride)
460
+ stateConfiguration = { ...stateConfiguration, strategy: options.strategyOverride };
461
+ if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
462
+ if (options.input !== undefined)
463
+ return { errors: ['--input is not supported by composed CEL execution; configure physical files in data.yaml'] };
464
+ if (options.onError === 'continue')
465
+ return { errors: ['Composed execution does not support --on-error continue without rollback'] };
466
+ try {
467
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
468
+ // The physical-source adapter supplies its configured COBOL projection.
469
+ // Other formats require an explicit projection from their authoring layer.
470
+ const open = async (logicalName) => (0, cursor_loader_source_1.prepareConfiguredFileSource)({
471
+ configDirectory: paths.directory, logicalName
472
+ });
473
+ const prepared = await (0, composition_definition_1.prepareCompositionDefinition)(definition, open);
474
+ if (stateConfiguration.strategy === 'object') {
475
+ const graph = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, prepared.composed.projection, prepared.access);
476
+ if ('errors' in graph)
477
+ return graph;
478
+ const observe = debugging ? (generation, state) => {
479
+ const values = {};
480
+ for (const node of graph.value.generations[generation]) {
481
+ if (options.debugNode !== undefined && node.metadata.name !== options.debugNode)
482
+ continue;
483
+ values[node.metadata.name] = state[node.metadata.name];
484
+ }
485
+ if (Object.keys(values).length !== 0) {
486
+ const selected = options.debugNode === undefined ? undefined : graph.value.generations[generation].find(node => node.metadata.name === options.debugNode);
487
+ diagnostic(`GENERATION ${generation} ${selected ? selected.type === 'cel' ?
488
+ `${selected.metadata.name} CEL ${JSON.stringify(selected.cel)} ` :
489
+ `${selected.metadata.name} AGGREGATE ${selected.operation}(${selected.window}) ` : ''}${(0, cel_json_1.stringifyCelJson)(values)}`);
490
+ }
491
+ } : undefined;
492
+ let visited = 0, emitted = 0;
493
+ if (!options.raw && target === undefined)
494
+ emit('# ' + paths.catalogue);
495
+ if (stateConfiguration.file) {
496
+ // Object and indexed execution must read the same historical state.
497
+ const historicalGraph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, prepared.composed.projection, prepared.access);
498
+ if ('errors' in historicalGraph)
499
+ return historicalGraph;
500
+ const normalizeKey = (0, indexed_accumulator_1.indexedKeyNormalizer)(stateConfiguration.customerIdType, stateConfiguration.customerId);
501
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(prepared.composed, {
502
+ schema: historicalGraph.value.schema, optional: stateConfiguration.optional,
503
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value),
504
+ filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file)
505
+ }, row => normalizeKey(row.key), (row, state) => {
506
+ visited++;
507
+ if (target !== undefined && (0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
508
+ return true;
509
+ if (target === undefined && visited <= skip)
510
+ return true;
511
+ const previous = (0, cel_entity_1.indexedStateObject)(historicalGraph.value.schema, state);
512
+ if (debugging) {
513
+ const input = (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row);
514
+ trace('INPUT', input);
515
+ trace('PREVIOUS', previous);
516
+ }
517
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
518
+ const result = graph.value.run(row, previous, variables, [], observe);
519
+ if (measurement) {
520
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
521
+ measurement.records++;
522
+ }
523
+ options.observeObject?.(row.key, result);
524
+ if (!options.suppressOutput)
525
+ emit((0, cel_json_1.stringifyCelJson)(result));
526
+ trace('RESULT', result);
527
+ emitted++;
528
+ return target === undefined && (count === undefined || emitted < count);
529
+ });
530
+ return target !== undefined && emitted === 0
531
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
532
+ }
533
+ await prepared.composed.scan(row => {
534
+ visited++;
535
+ if (target !== undefined && (0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
536
+ return true;
537
+ if (target === undefined && visited <= skip)
538
+ return true;
539
+ if (debugging) {
540
+ const input = (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row);
541
+ trace('INPUT', input);
542
+ trace('PREVIOUS', checked.value);
543
+ }
544
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
545
+ const result = graph.value.run(row, checked.value, variables, [], observe);
546
+ if (measurement) {
547
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
548
+ measurement.records++;
549
+ }
550
+ options.observeObject?.(row.key, result);
551
+ if (!options.suppressOutput)
552
+ emit((0, cel_json_1.stringifyCelJson)(result));
553
+ trace('RESULT', result);
554
+ emitted++;
555
+ return target === undefined && (count === undefined || emitted < count);
556
+ });
557
+ return target !== undefined && emitted === 0
558
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
559
+ }
560
+ const executor = compileIndexedComposedExecutor(paths, authored.value, checked.value, stateConfiguration, prepared);
561
+ if ('errors' in executor)
562
+ return executor;
563
+ return await executor.value(options, emit, diagnostic);
564
+ }
565
+ catch (error) {
566
+ return { errors: ['Composed CEL: ' + (error instanceof Error ? error.message : String(error))] };
567
+ }
568
+ }
569
+ if (options.logicalName === undefined)
570
+ return { errors: ['A logical file is required when no composition is declared in files.yaml'] };
571
+ if (stateConfiguration.strategy === 'indexed') {
572
+ // An in-place update may leave the current customer's state partial on failure.
573
+ // Fail fast until rollback is implemented.
574
+ if (options.onError === 'continue')
575
+ return { errors: [
576
+ 'Indexed accumulator execution does not support --on-error continue without rollback'
577
+ ] };
578
+ try {
579
+ const input = await (0, cursor_loader_source_1.prepareConfiguredFileSource)({
580
+ configDirectory: paths.directory, logicalName: options.logicalName,
581
+ ...(options.input !== undefined ? { input: options.input } : {})
582
+ });
583
+ return await (0, cursor_loader_source_1.withPreparedFileSource)(input, async (source) => {
584
+ const prepared = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, source.projection, source.access);
585
+ if ('errors' in prepared)
586
+ return prepared;
587
+ const session = prepared.value.createIndexedSession(undefined, debugging ? (0, cel_entity_1.debugGenerations)(prepared.value.generations, prepared.value.schema, diagnostic, options.debugNode) : undefined);
588
+ const variables = options.periodicity === undefined ? {} : { periodicity: options.periodicity };
589
+ const indices = [];
590
+ let visited = 0, emitted = 0;
591
+ if (!options.raw && target === undefined)
592
+ emit(`# ${source.filename}`);
593
+ if (target !== undefined && !stateConfiguration.file) {
594
+ const keyAccess = source.access.compileScalar([stateConfiguration.customerId]);
595
+ if ('errors' in keyAccess)
596
+ return keyAccess;
597
+ await source.scan((record, lease) => {
598
+ try {
599
+ if ((0, cel_entity_1.selectEntity)(keyAccess.value.read(record, indices), target) === 'skip')
600
+ return true;
601
+ const state = (0, cel_entity_1.indexedStateFromInitial)(prepared.value.schema, checked.value);
602
+ state.entityKey = target;
603
+ if (debugging) {
604
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
605
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state));
606
+ }
607
+ session.run(record, state, variables, indices);
608
+ const result = (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state);
609
+ emit((0, cel_json_1.stringifyCelJson)(result));
610
+ trace('RESULT', result);
611
+ emitted++;
612
+ return false;
613
+ }
614
+ finally {
615
+ lease.close();
616
+ }
617
+ });
618
+ return emitted === 0 ? { errors: [`Entity ${target} not found`] } : { value: undefined };
619
+ }
620
+ const key = (0, indexed_accumulator_1.indexedCustomerKey)(source, stateConfiguration.customerId, stateConfiguration.customerIdType);
621
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(source, {
622
+ schema: prepared.value.schema, optional: stateConfiguration.optional,
623
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value),
624
+ ...(stateConfiguration.file ? { filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file) } : {})
625
+ }, key, (record, state) => {
626
+ visited++;
627
+ if (target !== undefined) {
628
+ const entity = key(record);
629
+ const selection = (0, cel_entity_1.selectEntity)(entity, target);
630
+ if (selection === 'skip')
631
+ return true;
632
+ }
633
+ else if (visited <= skip)
634
+ return true;
635
+ if (debugging)
636
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
637
+ if (debugging)
638
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state));
639
+ try {
640
+ session.run(record, state, variables, indices);
641
+ }
642
+ catch (error) {
643
+ throw new Error(`Record ${visited}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
644
+ }
645
+ const result = (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state);
646
+ emit((0, cel_json_1.stringifyCelJson)(result));
647
+ trace('RESULT', result);
648
+ emitted++;
649
+ return target === undefined && (count === undefined || emitted < count);
650
+ });
651
+ return target !== undefined && emitted === 0
652
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
653
+ });
654
+ }
655
+ catch (error) {
656
+ return { errors: [`CEL ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
657
+ }
658
+ }
659
+ if (target !== undefined) {
660
+ try {
661
+ const input = await (0, cursor_loader_source_1.prepareConfiguredFileSource)({
662
+ configDirectory: paths.directory, logicalName: options.logicalName,
663
+ ...(options.input !== undefined ? { input: options.input } : {})
664
+ });
665
+ return await (0, cursor_loader_source_1.withPreparedFileSource)(input, async (source) => {
666
+ const graph = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, source.projection, source.access);
667
+ if ('errors' in graph)
668
+ return graph;
669
+ const observe = debugging ? (generation, state) => {
670
+ const values = {};
671
+ for (const node of graph.value.generations[generation]) {
672
+ if (options.debugNode !== undefined && node.metadata.name !== options.debugNode)
673
+ continue;
674
+ values[node.metadata.name] = state[node.metadata.name];
675
+ }
676
+ if (Object.keys(values).length !== 0) {
677
+ const selected = options.debugNode === undefined ? undefined : graph.value.generations[generation].find(node => node.metadata.name === options.debugNode);
678
+ diagnostic(`GENERATION ${generation} ${selected ? selected.type === 'cel' ?
679
+ `${selected.metadata.name} CEL ${JSON.stringify(selected.cel)} ` :
680
+ `${selected.metadata.name} AGGREGATE ${selected.operation}(${selected.window}) ` : ''}${(0, cel_json_1.stringifyCelJson)(values)}`);
681
+ }
682
+ } : undefined;
683
+ if (stateConfiguration.file) {
684
+ const historicalGraph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, source.projection, source.access);
685
+ if ('errors' in historicalGraph)
686
+ return historicalGraph;
687
+ const key = (0, indexed_accumulator_1.indexedCustomerKey)(source, stateConfiguration.customerId, stateConfiguration.customerIdType);
688
+ let matched = false;
689
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(source, {
690
+ schema: historicalGraph.value.schema, optional: stateConfiguration.optional,
691
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value),
692
+ filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file)
693
+ }, key, (record, state) => {
694
+ if ((0, cel_entity_1.selectEntity)(key(record), target) === 'skip')
695
+ return true;
696
+ matched = true;
697
+ const previous = (0, cel_entity_1.indexedStateObject)(historicalGraph.value.schema, state);
698
+ if (debugging) {
699
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
700
+ trace('PREVIOUS', previous);
701
+ }
702
+ const result = graph.value.run(record, previous, variables, [], observe);
703
+ emit((0, cel_json_1.stringifyCelJson)(result));
704
+ trace('RESULT', result);
705
+ return false;
706
+ });
707
+ return matched ? { value: undefined } : { errors: [`Entity ${target} not found`] };
708
+ }
709
+ const keyAccess = source.access.compileScalar([stateConfiguration.customerId]);
710
+ if ('errors' in keyAccess)
711
+ return keyAccess;
712
+ let matched = false;
713
+ await source.scan((record, lease) => {
714
+ try {
715
+ if ((0, cel_entity_1.selectEntity)(keyAccess.value.read(record, []), target) === 'skip')
716
+ return true;
717
+ matched = true;
718
+ if (debugging) {
719
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
720
+ trace('PREVIOUS', checked.value);
721
+ }
722
+ const result = graph.value.run(record, checked.value, variables, [], observe);
723
+ emit((0, cel_json_1.stringifyCelJson)(result));
724
+ trace('RESULT', result);
725
+ return false;
726
+ }
727
+ finally {
728
+ lease.close();
729
+ }
730
+ });
731
+ return matched ? { value: undefined } : { errors: [`Entity ${target} not found`] };
732
+ });
733
+ }
734
+ catch (error) {
735
+ return { errors: [`CEL ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
736
+ }
737
+ }
738
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
198
739
  configDirectory: paths.directory, logicalName: options.logicalName,
199
740
  catalogue: paths.catalogue, data: paths.data,
200
741
  ...(options.input !== undefined ? { input: options.input } : {})
@@ -204,10 +745,12 @@ exports.nodeCliServices = {
204
745
  let prepared;
205
746
  let visit = 0;
206
747
  let emitted = 0;
207
- let previous = checked.value;
748
+ const previous = checked.value;
749
+ const indices = [];
750
+ let session;
208
751
  let fatalErrors;
209
752
  try {
210
- await (0, cursor_loader_cobol_1.scanCobolFile)({
753
+ await (0, cursor_loader_cobol_1.scanCobolFileLeased)({
211
754
  filename: source.value.filename,
212
755
  copybook: source.value.copybook,
213
756
  projection: source.value.runtimeProjection,
@@ -216,20 +759,28 @@ exports.nodeCliServices = {
216
759
  prepared = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, source.value.projection, (0, cel_access_1.cobolAccessCompiler)(details, source.value.projection));
217
760
  if ('errors' in prepared)
218
761
  throw new Error(prepared.errors.join('; '));
762
+ session = prepared.value.createSession(previous);
219
763
  // Only emit output after all CEL nodes have compiled successfully.
220
764
  if (!options.raw)
221
765
  emit(`# ${source.value.filename}`);
222
766
  }
223
- }, (_view, parsed) => {
767
+ }, (upstream, parsed) => {
224
768
  visit++;
225
- if (visit <= skip)
769
+ if (visit <= skip) {
770
+ // No graph result is created for skipped rows, so the
771
+ // cursor lease has no downstream owner to transfer to.
772
+ upstream.close();
226
773
  return;
227
- if (!prepared || 'errors' in prepared)
774
+ }
775
+ if (!session)
228
776
  throw new Error('CEL graph not prepared');
229
- const evaluated = prepared.value.evaluate(parsed, previous, {}, [], { mode: options.onError === 'continue' ? 'continue' : 'throw' });
230
- emitted++;
231
- if ('errors' in evaluated) {
232
- const problems = evaluated.errors.map(message => `Record ${visit}: ${message}`);
777
+ let evaluated;
778
+ try {
779
+ evaluated = session.runLeased(parsed, upstream, variables, indices);
780
+ }
781
+ catch (error) {
782
+ emitted++;
783
+ const problems = [`Record ${visit}: ${error instanceof Error ? error.message : String(error)}`];
233
784
  if (options.onError !== 'continue') {
234
785
  fatalErrors = problems;
235
786
  return false;
@@ -239,8 +790,14 @@ exports.nodeCliServices = {
239
790
  return false;
240
791
  return;
241
792
  }
242
- previous = evaluated.value;
243
- emit((0, cel_json_1.stringifyCelJson)(evaluated.value));
793
+ try {
794
+ emitted++;
795
+ emit((0, cel_json_1.stringifyCelJson)(evaluated.value));
796
+ }
797
+ finally {
798
+ // Sole normal owner of the upstream record's lifetime.
799
+ evaluated.close();
800
+ }
244
801
  if (count !== undefined && emitted >= count)
245
802
  return false;
246
803
  });