@cobol-ts/cobolts 0.0.14 → 0.0.17

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 (206) 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 +450 -21
  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 +227 -38
  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 +12 -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/ai.d.ts +12 -0
  148. package/dist/src/commands/ai.demo.spec.d.ts +1 -0
  149. package/dist/src/commands/ai.demo.spec.js +242 -0
  150. package/dist/src/commands/ai.demo.spec.js.map +1 -0
  151. package/dist/src/commands/ai.js +125 -0
  152. package/dist/src/commands/ai.js.map +1 -0
  153. package/dist/src/commands/ai.spec.d.ts +1 -0
  154. package/dist/src/commands/ai.spec.js +170 -0
  155. package/dist/src/commands/ai.spec.js.map +1 -0
  156. package/dist/src/commands/benchmark.d.ts +3 -0
  157. package/dist/src/commands/benchmark.js +300 -0
  158. package/dist/src/commands/benchmark.js.map +1 -0
  159. package/dist/src/commands/cel.d.ts +1 -1
  160. package/dist/src/commands/cel.js +38 -5
  161. package/dist/src/commands/cel.js.map +1 -1
  162. package/dist/src/commands/characteristics.d.ts +3 -0
  163. package/dist/src/commands/characteristics.js +107 -0
  164. package/dist/src/commands/characteristics.js.map +1 -0
  165. package/dist/src/commands/characteristics.spec.d.ts +1 -0
  166. package/dist/src/commands/characteristics.spec.js +242 -0
  167. package/dist/src/commands/characteristics.spec.js.map +1 -0
  168. package/dist/src/commands/config.validate.js +8 -4
  169. package/dist/src/commands/config.validate.js.map +1 -1
  170. package/dist/src/commands/extract.d.ts +14 -0
  171. package/dist/src/commands/extract.js +85 -0
  172. package/dist/src/commands/extract.js.map +1 -0
  173. package/dist/src/commands/extract.spec.d.ts +1 -0
  174. package/dist/src/commands/extract.spec.js +179 -0
  175. package/dist/src/commands/extract.spec.js.map +1 -0
  176. package/dist/src/commands/graph.d.ts +9 -0
  177. package/dist/src/commands/graph.js +98 -0
  178. package/dist/src/commands/graph.js.map +1 -0
  179. package/dist/src/commands/graph.report.d.ts +26 -0
  180. package/dist/src/commands/graph.report.js +75 -0
  181. package/dist/src/commands/graph.report.js.map +1 -0
  182. package/dist/src/commands/graph.spec.d.ts +1 -0
  183. package/dist/src/commands/graph.spec.js +215 -0
  184. package/dist/src/commands/graph.spec.js.map +1 -0
  185. package/dist/src/commands/periodicity.d.ts +3 -0
  186. package/dist/src/commands/periodicity.js +11 -0
  187. package/dist/src/commands/periodicity.js.map +1 -0
  188. package/dist/src/commands/validate.all.d.ts +7 -0
  189. package/dist/src/commands/validate.all.integration.spec.d.ts +1 -0
  190. package/dist/src/commands/validate.all.integration.spec.js +43 -0
  191. package/dist/src/commands/validate.all.integration.spec.js.map +1 -0
  192. package/dist/src/commands/validate.all.js +75 -0
  193. package/dist/src/commands/validate.all.js.map +1 -0
  194. package/dist/src/commands/validate.all.spec.d.ts +1 -0
  195. package/dist/src/commands/validate.all.spec.js +70 -0
  196. package/dist/src/commands/validate.all.spec.js.map +1 -0
  197. package/dist/src/context.d.ts +40 -6
  198. package/dist/src/context.js +582 -21
  199. package/dist/src/context.js.map +1 -1
  200. package/dist/src/indexed.accumulator.d.ts +18 -0
  201. package/dist/src/indexed.accumulator.js +107 -0
  202. package/dist/src/indexed.accumulator.js.map +1 -0
  203. package/dist/src/indexed.accumulator.spec.d.ts +1 -0
  204. package/dist/src/indexed.accumulator.spec.js +81 -0
  205. package/dist/src/indexed.accumulator.spec.js.map +1 -0
  206. package/package.json +27 -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,184 @@ 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, schema);
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,
247
+ async serveCharacteristics(catalogues, port) {
248
+ const { createCharacteristicsApp } = await import('@cobol-ts/characteristics-express');
249
+ const server = createCharacteristicsApp(catalogues).listen(port, '127.0.0.1');
250
+ await new Promise((resolve, reject) => {
251
+ server.once('listening', resolve);
252
+ server.once('error', reject);
253
+ });
254
+ },
80
255
  async list(paths) {
81
256
  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];
257
+ const result = [paths.catalogue];
258
+ if (await files_1.nodeTextFiles.exists(paths.policy))
259
+ result.push(paths.policy);
83
260
  if (await files_1.nodeTextFiles.exists(paths.data))
84
261
  result.push(paths.data);
85
262
  if (await files_1.nodeTextFiles.exists(paths.accumulator))
@@ -124,18 +301,67 @@ exports.nodeCliServices = {
124
301
  const loaded = await loadCelAuthoringGraph(paths, options.graph);
125
302
  if ('errors' in loaded)
126
303
  return loaded;
127
- if (options.stage === 'graph')
304
+ if (options.stage === 'graph' && !options.short)
128
305
  return { value: JSON.stringify(loaded.value, null, 2) };
129
306
  const canonical = (0, cel_graph_runtime_1.canonicaliseCelGraph)(loaded.value);
130
307
  if ('errors' in canonical)
131
308
  return canonical;
309
+ const shortNodes = () => canonical.value.map(node => `${node.metadata.name} <- ${node.dependsOn.map(path => path.join('.')).join(', ') || '-'}`).join('\n');
310
+ if (options.stage === 'graph' && options.short)
311
+ return { value: shortNodes() };
312
+ if (options.stage === 'stats') {
313
+ const metrics = { binaryResolutions: 0, dynamicBinaryResolutions: 0, dynamicConditionalResolutions: 0 };
314
+ const compile = (projection, access) => (0, cel_graph_runtime_1.prepareCelGraph)(loaded.value, projection, access, {}, {}, metrics);
315
+ if (options.logicalName !== undefined) {
316
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
317
+ configDirectory: paths.directory, logicalName: options.logicalName,
318
+ catalogue: paths.catalogue, data: paths.data
319
+ }, files_1.nodeTextFiles, yaml_jsyaml_1.jsYamlParser);
320
+ if ('errors' in source)
321
+ return source;
322
+ const details = await (0, cursor_loader_cobol_1.prepareCobolFileDetails)({
323
+ copybook: source.value.copybook,
324
+ ...(source.value.framing ? { framing: source.value.framing } : {})
325
+ });
326
+ const compiled = compile(source.value.projection, (0, cel_access_1.cobolAccessCompiler)(details, source.value.projection));
327
+ if ('errors' in compiled)
328
+ return compiled;
329
+ }
330
+ else if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
331
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
332
+ const composed = await (0, composition_definition_1.prepareCompositionDefinition)(definition, logicalName => (0, cursor_loader_source_1.prepareConfiguredFileSource)({ configDirectory: paths.directory, logicalName }));
333
+ const compiled = compile(composed.composed.projection, composed.access);
334
+ if ('errors' in compiled)
335
+ return compiled;
336
+ }
337
+ else {
338
+ const empty = {};
339
+ const compiled = compile(empty, (0, cel_access_1.jsonAccessCompiler)(empty));
340
+ if ('errors' in compiled)
341
+ return compiled;
342
+ }
343
+ return (0, cel_stats_1.celGraphStats)(canonical.value, metrics);
344
+ }
132
345
  if (options.stage === 'canonical')
133
- return { value: JSON.stringify(canonical.value, null, 2) };
346
+ return { value: options.short
347
+ ? shortNodes()
348
+ : JSON.stringify(canonical.value, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
134
349
  const sorted = (0, cel_graph_runtime_1.planCanonicalCelGraph)(canonical.value);
135
350
  if ('errors' in sorted)
136
351
  return sorted;
137
352
  if (options.stage === 'sorted') {
138
- return { value: JSON.stringify({ generations: sorted.value.generations }, null, 2) };
353
+ return { value: options.short
354
+ ? sorted.value.generations.map((nodes, index) => `G${index}: ${nodes.map(node => `${node.metadata.name}(${node.dependsOn.map(path => path.join('.')).join(', ') || '-'})`).join(' ')}`).join('\n')
355
+ : JSON.stringify({ generations: sorted.value.generations }, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
356
+ }
357
+ if (options.stage === 'planned') {
358
+ let index = 0;
359
+ const planned = (0, planned_graph_1.makePlannedGraph)(sorted.value.generations, sorted.value.graph, (node, downstream) => ({ index: index++, node, downstream }));
360
+ if ('errors' in planned)
361
+ return planned;
362
+ return { value: options.short
363
+ ? planned.value.map((generation, i) => `G${i}: ${generation.map(n => `${n.index}:${n.node.metadata.name}->[${n.downstream.join(',')}]`).join(' ')}`).join('\n')
364
+ : JSON.stringify({ generations: planned.value }, (_name, value) => typeof value === 'bigint' ? value.toString() : value, 2) };
139
365
  }
140
366
  if (options.stage !== 'validate')
141
367
  return { errors: [`Unexpected CEL operation: ${options.stage}`] };
@@ -145,10 +371,25 @@ exports.nodeCliServices = {
145
371
  const checked = (0, cel_graph_runtime_1.validateInitialAccumulator)(initial.value, loaded.value, canonical.value);
146
372
  if ('errors' in checked)
147
373
  return checked;
374
+ if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
375
+ try {
376
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
377
+ const composed = await (0, composition_definition_1.prepareCompositionDefinition)(definition, logicalName => (0, cursor_loader_source_1.prepareConfiguredFileSource)({ configDirectory: paths.directory, logicalName }));
378
+ const graph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(loaded.value, composed.composed.projection, composed.access);
379
+ if ('errors' in graph)
380
+ return graph;
381
+ return { value: 'Valid composed graph: ' + graph.value.schema.nodes.length +
382
+ ' nodes, ' + composed.sources.length + ' sources' };
383
+ }
384
+ catch (error) {
385
+ return { errors: ['Composed CEL validation: ' +
386
+ (error instanceof Error ? error.message : String(error))] };
387
+ }
388
+ }
148
389
  if (options.logicalName === undefined) {
149
390
  return { value: `Valid graph: ${canonical.value.length} nodes, ${sorted.value.generations.length} generations (structural)` };
150
391
  }
151
- const source = await (0, file_display_2.openConfiguredCobolFile)({
392
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
152
393
  configDirectory: paths.directory, logicalName: options.logicalName,
153
394
  catalogue: paths.catalogue, data: paths.data
154
395
  }, files_1.nodeTextFiles, yaml_jsyaml_1.jsYamlParser);
@@ -168,7 +409,27 @@ exports.nodeCliServices = {
168
409
  return { errors: [`CEL validation ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
169
410
  }
170
411
  },
171
- async celExecute(paths, options, emit) {
412
+ async celExecute(paths, options, emit, diagnostic = message => console.error(message)) {
413
+ const variables = options.periodicity === undefined ? {} : { periodicity: options.periodicity };
414
+ let requestedEntity;
415
+ if (options.entityId !== undefined) {
416
+ requestedEntity = options.entityId;
417
+ if (options.skip !== undefined || options.count !== undefined || options.all || options.onError !== undefined)
418
+ return { errors: ['CEL evaluate cannot combine an entity ID with count, skip, all or on-error'] };
419
+ }
420
+ const measurement = options.measurement;
421
+ const target = requestedEntity;
422
+ const debugging = options.debug || options.debugNode !== undefined;
423
+ const trace = (label, value) => {
424
+ if (!debugging)
425
+ return;
426
+ if (options.debugNode !== undefined && (label === 'PREVIOUS' || label === 'RESULT')) {
427
+ const state = value;
428
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)({ [options.debugNode]: state[options.debugNode] }));
429
+ }
430
+ else
431
+ diagnostic(label + ' ' + (0, cel_json_1.stringifyCelJson)(value));
432
+ };
172
433
  const count = options.all ? undefined : (options.count ?? 10);
173
434
  const skip = options.skip ?? 0;
174
435
  if (options.all && options.count !== undefined)
@@ -182,6 +443,8 @@ exports.nodeCliServices = {
182
443
  const authored = await loadCelAuthoringGraph(paths, options.graph);
183
444
  if ('errors' in authored)
184
445
  return authored;
446
+ if (options.debugNode !== undefined && !Object.prototype.hasOwnProperty.call(authored.value, options.debugNode))
447
+ return { errors: [`Unknown debug node ${options.debugNode}`] };
185
448
  const canonical = (0, cel_graph_runtime_1.canonicaliseCelGraph)(authored.value);
186
449
  if ('errors' in canonical)
187
450
  return canonical;
@@ -194,7 +457,293 @@ exports.nodeCliServices = {
194
457
  const checked = (0, cel_graph_runtime_1.validateInitialAccumulator)(initial.value, authored.value, canonical.value);
195
458
  if ('errors' in checked)
196
459
  return checked;
197
- const source = await (0, file_display_2.openConfiguredCobolFile)({
460
+ let stateConfiguration;
461
+ try {
462
+ stateConfiguration = (await (0, indexed_accumulator_1.readAccumulatorDocument)(paths.accumulator)).state;
463
+ }
464
+ catch (error) {
465
+ return { errors: [`Accumulator configuration: ${error instanceof Error ? error.message : String(error)}`] };
466
+ }
467
+ if (options.strategyOverride)
468
+ stateConfiguration = { ...stateConfiguration, strategy: options.strategyOverride };
469
+ if (await (0, composition_definition_1.hasCompositionDefinition)(paths.directory)) {
470
+ if (options.input !== undefined)
471
+ return { errors: ['--input is not supported by composed CEL execution; configure physical files in data.yaml'] };
472
+ if (options.onError === 'continue')
473
+ return { errors: ['Composed execution does not support --on-error continue without rollback'] };
474
+ try {
475
+ const definition = (0, errors_1.valueOrThrow)(await (0, composition_definition_1.readCompositionDefinition)(paths.directory));
476
+ // The physical-source adapter supplies its configured COBOL projection.
477
+ // Other formats require an explicit projection from their authoring layer.
478
+ const open = async (logicalName) => (0, cursor_loader_source_1.prepareConfiguredFileSource)({
479
+ configDirectory: paths.directory, logicalName
480
+ });
481
+ const prepared = await (0, composition_definition_1.prepareCompositionDefinition)(definition, open);
482
+ if (stateConfiguration.strategy === 'object') {
483
+ const graph = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, prepared.composed.projection, prepared.access);
484
+ if ('errors' in graph)
485
+ return graph;
486
+ const observe = debugging ? (generation, state) => {
487
+ const values = {};
488
+ for (const node of graph.value.generations[generation]) {
489
+ if (options.debugNode !== undefined && node.metadata.name !== options.debugNode)
490
+ continue;
491
+ values[node.metadata.name] = state[node.metadata.name];
492
+ }
493
+ if (Object.keys(values).length !== 0) {
494
+ const selected = options.debugNode === undefined ? undefined : graph.value.generations[generation].find(node => node.metadata.name === options.debugNode);
495
+ diagnostic(`GENERATION ${generation} ${selected ? selected.type === 'cel' ?
496
+ `${selected.metadata.name} CEL ${JSON.stringify(selected.cel)} ` :
497
+ `${selected.metadata.name} AGGREGATE ${selected.operation}(${selected.window}) ` : ''}${(0, cel_json_1.stringifyCelJson)(values)}`);
498
+ }
499
+ } : undefined;
500
+ let visited = 0, emitted = 0;
501
+ if (!options.raw && target === undefined)
502
+ emit('# ' + paths.catalogue);
503
+ if (stateConfiguration.file) {
504
+ // Object and indexed execution must read the same historical state.
505
+ const historicalGraph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, prepared.composed.projection, prepared.access);
506
+ if ('errors' in historicalGraph)
507
+ return historicalGraph;
508
+ const normalizeKey = (0, indexed_accumulator_1.indexedKeyNormalizer)(stateConfiguration.customerIdType, stateConfiguration.customerId);
509
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(prepared.composed, {
510
+ schema: historicalGraph.value.schema, optional: stateConfiguration.optional,
511
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value, historicalGraph.value.schema),
512
+ filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file)
513
+ }, row => normalizeKey(row.key), (row, state) => {
514
+ visited++;
515
+ if (target !== undefined && (0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
516
+ return true;
517
+ if (target === undefined && visited <= skip)
518
+ return true;
519
+ const previous = (0, cel_entity_1.indexedStateObject)(historicalGraph.value.schema, state);
520
+ if (debugging) {
521
+ const input = (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row);
522
+ trace('INPUT', input);
523
+ trace('PREVIOUS', previous);
524
+ }
525
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
526
+ const result = graph.value.run(row, previous, variables, [], observe);
527
+ if (measurement) {
528
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
529
+ measurement.records++;
530
+ }
531
+ options.observeObject?.(row.key, result);
532
+ if (!options.suppressOutput)
533
+ emit((0, cel_json_1.stringifyCelJson)(result));
534
+ trace('RESULT', result);
535
+ emitted++;
536
+ return target === undefined && (count === undefined || emitted < count);
537
+ });
538
+ return target !== undefined && emitted === 0
539
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
540
+ }
541
+ await prepared.composed.scan(row => {
542
+ visited++;
543
+ if (target !== undefined && (0, cel_entity_1.selectEntity)(row.key, target) === 'skip')
544
+ return true;
545
+ if (target === undefined && visited <= skip)
546
+ return true;
547
+ if (debugging) {
548
+ const input = (0, cel_entity_1.debugProjectedInput)(prepared.composed.projection, prepared.access, row);
549
+ trace('INPUT', input);
550
+ trace('PREVIOUS', checked.value);
551
+ }
552
+ const startedCel = measurement ? node_perf_hooks_1.performance.now() : 0;
553
+ const result = graph.value.run(row, checked.value, variables, [], observe);
554
+ if (measurement) {
555
+ measurement.celMs += node_perf_hooks_1.performance.now() - startedCel;
556
+ measurement.records++;
557
+ }
558
+ options.observeObject?.(row.key, result);
559
+ if (!options.suppressOutput)
560
+ emit((0, cel_json_1.stringifyCelJson)(result));
561
+ trace('RESULT', result);
562
+ emitted++;
563
+ return target === undefined && (count === undefined || emitted < count);
564
+ });
565
+ return target !== undefined && emitted === 0
566
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
567
+ }
568
+ const executor = compileIndexedComposedExecutor(paths, authored.value, checked.value, stateConfiguration, prepared);
569
+ if ('errors' in executor)
570
+ return executor;
571
+ return await executor.value(options, emit, diagnostic);
572
+ }
573
+ catch (error) {
574
+ return { errors: ['Composed CEL: ' + (error instanceof Error ? error.message : String(error))] };
575
+ }
576
+ }
577
+ if (options.logicalName === undefined)
578
+ return { errors: ['A logical file is required when no composition is declared in files.yaml'] };
579
+ if (stateConfiguration.strategy === 'indexed') {
580
+ // An in-place update may leave the current customer's state partial on failure.
581
+ // Fail fast until rollback is implemented.
582
+ if (options.onError === 'continue')
583
+ return { errors: [
584
+ 'Indexed accumulator execution does not support --on-error continue without rollback'
585
+ ] };
586
+ try {
587
+ const input = await (0, cursor_loader_source_1.prepareConfiguredFileSource)({
588
+ configDirectory: paths.directory, logicalName: options.logicalName,
589
+ ...(options.input !== undefined ? { input: options.input } : {})
590
+ });
591
+ return await (0, cursor_loader_source_1.withPreparedFileSource)(input, async (source) => {
592
+ const prepared = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, source.projection, source.access);
593
+ if ('errors' in prepared)
594
+ return prepared;
595
+ const session = prepared.value.createIndexedSession(undefined, debugging ? (0, cel_entity_1.debugGenerations)(prepared.value.generations, prepared.value.schema, diagnostic, options.debugNode) : undefined);
596
+ const variables = options.periodicity === undefined ? {} : { periodicity: options.periodicity };
597
+ const indices = [];
598
+ let visited = 0, emitted = 0;
599
+ if (!options.raw && target === undefined)
600
+ emit(`# ${source.filename}`);
601
+ if (target !== undefined && !stateConfiguration.file) {
602
+ const keyAccess = source.access.compileScalar([stateConfiguration.customerId]);
603
+ if ('errors' in keyAccess)
604
+ return keyAccess;
605
+ await source.scan((record, lease) => {
606
+ try {
607
+ if ((0, cel_entity_1.selectEntity)(keyAccess.value.read(record, indices), target) === 'skip')
608
+ return true;
609
+ const state = (0, cel_entity_1.indexedStateFromInitial)(prepared.value.schema, checked.value);
610
+ state.entityKey = target;
611
+ if (debugging) {
612
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
613
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state));
614
+ }
615
+ session.run(record, state, variables, indices);
616
+ const result = (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state);
617
+ emit((0, cel_json_1.stringifyCelJson)(result));
618
+ trace('RESULT', result);
619
+ emitted++;
620
+ return false;
621
+ }
622
+ finally {
623
+ lease.close();
624
+ }
625
+ });
626
+ return emitted === 0 ? { errors: [`Entity ${target} not found`] } : { value: undefined };
627
+ }
628
+ const key = (0, indexed_accumulator_1.indexedCustomerKey)(source, stateConfiguration.customerId, stateConfiguration.customerIdType);
629
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(source, {
630
+ schema: prepared.value.schema, optional: stateConfiguration.optional,
631
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value, prepared.value.schema),
632
+ ...(stateConfiguration.file ? { filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file) } : {})
633
+ }, key, (record, state) => {
634
+ visited++;
635
+ if (target !== undefined) {
636
+ const entity = key(record);
637
+ const selection = (0, cel_entity_1.selectEntity)(entity, target);
638
+ if (selection === 'skip')
639
+ return true;
640
+ }
641
+ else if (visited <= skip)
642
+ return true;
643
+ if (debugging)
644
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
645
+ if (debugging)
646
+ trace('PREVIOUS', (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state));
647
+ try {
648
+ session.run(record, state, variables, indices);
649
+ }
650
+ catch (error) {
651
+ throw new Error(`Record ${visited}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
652
+ }
653
+ const result = (0, cel_entity_1.indexedStateObject)(prepared.value.schema, state);
654
+ emit((0, cel_json_1.stringifyCelJson)(result));
655
+ trace('RESULT', result);
656
+ emitted++;
657
+ return target === undefined && (count === undefined || emitted < count);
658
+ });
659
+ return target !== undefined && emitted === 0
660
+ ? { errors: [`Entity ${target} not found`] } : { value: undefined };
661
+ });
662
+ }
663
+ catch (error) {
664
+ return { errors: [`CEL ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
665
+ }
666
+ }
667
+ if (target !== undefined) {
668
+ try {
669
+ const input = await (0, cursor_loader_source_1.prepareConfiguredFileSource)({
670
+ configDirectory: paths.directory, logicalName: options.logicalName,
671
+ ...(options.input !== undefined ? { input: options.input } : {})
672
+ });
673
+ return await (0, cursor_loader_source_1.withPreparedFileSource)(input, async (source) => {
674
+ const graph = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, source.projection, source.access);
675
+ if ('errors' in graph)
676
+ return graph;
677
+ const observe = debugging ? (generation, state) => {
678
+ const values = {};
679
+ for (const node of graph.value.generations[generation]) {
680
+ if (options.debugNode !== undefined && node.metadata.name !== options.debugNode)
681
+ continue;
682
+ values[node.metadata.name] = state[node.metadata.name];
683
+ }
684
+ if (Object.keys(values).length !== 0) {
685
+ const selected = options.debugNode === undefined ? undefined : graph.value.generations[generation].find(node => node.metadata.name === options.debugNode);
686
+ diagnostic(`GENERATION ${generation} ${selected ? selected.type === 'cel' ?
687
+ `${selected.metadata.name} CEL ${JSON.stringify(selected.cel)} ` :
688
+ `${selected.metadata.name} AGGREGATE ${selected.operation}(${selected.window}) ` : ''}${(0, cel_json_1.stringifyCelJson)(values)}`);
689
+ }
690
+ } : undefined;
691
+ if (stateConfiguration.file) {
692
+ const historicalGraph = (0, cel_graph_runtime_1.prepareIndexedCelGraph)(authored.value, source.projection, source.access);
693
+ if ('errors' in historicalGraph)
694
+ return historicalGraph;
695
+ const key = (0, indexed_accumulator_1.indexedCustomerKey)(source, stateConfiguration.customerId, stateConfiguration.customerIdType);
696
+ let matched = false;
697
+ await (0, cursor_loader_indexed_graph_state_1.scanIndexedAccumulator)(source, {
698
+ schema: historicalGraph.value.schema, optional: stateConfiguration.optional,
699
+ removed: stateConfiguration.removed, customerIdType: stateConfiguration.customerIdType, initial: (0, indexed_accumulator_1.indexedSeedValues)(checked.value, historicalGraph.value.schema),
700
+ filename: (0, node_path_1.resolve)(paths.directory, stateConfiguration.file)
701
+ }, key, (record, state) => {
702
+ if ((0, cel_entity_1.selectEntity)(key(record), target) === 'skip')
703
+ return true;
704
+ matched = true;
705
+ const previous = (0, cel_entity_1.indexedStateObject)(historicalGraph.value.schema, state);
706
+ if (debugging) {
707
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
708
+ trace('PREVIOUS', previous);
709
+ }
710
+ const result = graph.value.run(record, previous, variables, [], observe);
711
+ emit((0, cel_json_1.stringifyCelJson)(result));
712
+ trace('RESULT', result);
713
+ return false;
714
+ });
715
+ return matched ? { value: undefined } : { errors: [`Entity ${target} not found`] };
716
+ }
717
+ const keyAccess = source.access.compileScalar([stateConfiguration.customerId]);
718
+ if ('errors' in keyAccess)
719
+ return keyAccess;
720
+ let matched = false;
721
+ await source.scan((record, lease) => {
722
+ try {
723
+ if ((0, cel_entity_1.selectEntity)(keyAccess.value.read(record, []), target) === 'skip')
724
+ return true;
725
+ matched = true;
726
+ if (debugging) {
727
+ trace('INPUT', (0, cel_entity_1.debugProjectedInput)(source.projection, source.access, record));
728
+ trace('PREVIOUS', checked.value);
729
+ }
730
+ const result = graph.value.run(record, checked.value, variables, [], observe);
731
+ emit((0, cel_json_1.stringifyCelJson)(result));
732
+ trace('RESULT', result);
733
+ return false;
734
+ }
735
+ finally {
736
+ lease.close();
737
+ }
738
+ });
739
+ return matched ? { value: undefined } : { errors: [`Entity ${target} not found`] };
740
+ });
741
+ }
742
+ catch (error) {
743
+ return { errors: [`CEL ${options.logicalName}: ${error instanceof Error ? error.message : String(error)}`] };
744
+ }
745
+ }
746
+ const source = await (0, file_display_1.openConfiguredCobolFile)({
198
747
  configDirectory: paths.directory, logicalName: options.logicalName,
199
748
  catalogue: paths.catalogue, data: paths.data,
200
749
  ...(options.input !== undefined ? { input: options.input } : {})
@@ -204,10 +753,12 @@ exports.nodeCliServices = {
204
753
  let prepared;
205
754
  let visit = 0;
206
755
  let emitted = 0;
207
- let previous = checked.value;
756
+ const previous = checked.value;
757
+ const indices = [];
758
+ let session;
208
759
  let fatalErrors;
209
760
  try {
210
- await (0, cursor_loader_cobol_1.scanCobolFile)({
761
+ await (0, cursor_loader_cobol_1.scanCobolFileLeased)({
211
762
  filename: source.value.filename,
212
763
  copybook: source.value.copybook,
213
764
  projection: source.value.runtimeProjection,
@@ -216,19 +767,24 @@ exports.nodeCliServices = {
216
767
  prepared = (0, cel_graph_runtime_1.prepareCelGraph)(authored.value, source.value.projection, (0, cel_access_1.cobolAccessCompiler)(details, source.value.projection));
217
768
  if ('errors' in prepared)
218
769
  throw new Error(prepared.errors.join('; '));
770
+ session = prepared.value.createSession(previous);
219
771
  // Only emit output after all CEL nodes have compiled successfully.
220
772
  if (!options.raw)
221
773
  emit(`# ${source.value.filename}`);
222
774
  }
223
- }, (_view, parsed) => {
775
+ }, (upstream, parsed) => {
224
776
  visit++;
225
- if (visit <= skip)
777
+ if (visit <= skip) {
778
+ // No graph result is created for skipped rows, so the
779
+ // cursor lease has no downstream owner to transfer to.
780
+ upstream.close();
226
781
  return;
227
- if (!prepared || 'errors' in prepared)
782
+ }
783
+ if (!session)
228
784
  throw new Error('CEL graph not prepared');
229
785
  let evaluated;
230
786
  try {
231
- evaluated = prepared.value.run(parsed, previous, {}, []);
787
+ evaluated = session.runLeased(parsed, upstream, variables, indices);
232
788
  }
233
789
  catch (error) {
234
790
  emitted++;
@@ -242,9 +798,14 @@ exports.nodeCliServices = {
242
798
  return false;
243
799
  return;
244
800
  }
245
- emitted++;
246
- previous = evaluated;
247
- emit((0, cel_json_1.stringifyCelJson)(evaluated));
801
+ try {
802
+ emitted++;
803
+ emit((0, cel_json_1.stringifyCelJson)(evaluated.value));
804
+ }
805
+ finally {
806
+ // Sole normal owner of the upstream record's lifetime.
807
+ evaluated.close();
808
+ }
248
809
  if (count !== undefined && emitted >= count)
249
810
  return false;
250
811
  });