@tiangong-lca/cli 0.0.1 → 0.0.3

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.
@@ -0,0 +1,235 @@
1
+ import path from 'node:path';
2
+ import { writeJsonArtifact, writeJsonLinesArtifact } from './artifacts.js';
3
+ import { CliError } from './errors.js';
4
+ import { loadRowsFromFile } from './flow-governance.js';
5
+ import { fetchOneFlowRow, normalizeSupabaseFlowPayload, } from './flow-read.js';
6
+ import { requireSupabaseRestRuntime } from './supabase-rest.js';
7
+ import { createSupabaseDataRuntime } from './supabase-session.js';
8
+ const FLOW_FETCH_ROWS_TIMEOUT_MS = 10_000;
9
+ function normalizeToken(value) {
10
+ if (value === undefined || value === null) {
11
+ return null;
12
+ }
13
+ const trimmed = String(value).trim();
14
+ return trimmed ? trimmed : null;
15
+ }
16
+ function normalizeOptionalNonNegativeInteger(value, label, code) {
17
+ if (value === undefined || value === null || value === '') {
18
+ return null;
19
+ }
20
+ const parsed = typeof value === 'number' && Number.isInteger(value)
21
+ ? value
22
+ : Number.parseInt(String(value), 10);
23
+ if (!Number.isInteger(parsed) || parsed < 0) {
24
+ throw new CliError(`Expected ${label} to be a non-negative integer.`, {
25
+ code,
26
+ exitCode: 2,
27
+ details: value,
28
+ });
29
+ }
30
+ return parsed;
31
+ }
32
+ function normalizeFlowFetchRef(row, index) {
33
+ const id = normalizeToken(row.id);
34
+ if (!id) {
35
+ throw new CliError(`Flow ref row ${index + 1} is missing required id.`, {
36
+ code: 'FLOW_FETCH_ROWS_REF_ID_REQUIRED',
37
+ exitCode: 2,
38
+ details: row,
39
+ });
40
+ }
41
+ return {
42
+ id,
43
+ version: normalizeToken(row.version),
44
+ userId: normalizeToken(row.user_id ?? row.userId),
45
+ stateCode: normalizeOptionalNonNegativeInteger(row.state_code ?? row.stateCode, 'flow ref state_code', 'FLOW_FETCH_ROWS_INVALID_STATE_CODE'),
46
+ clusterId: normalizeToken(row.cluster_id ?? row.clusterId),
47
+ source: normalizeToken(row.source),
48
+ };
49
+ }
50
+ function toRequestedRefSummary(ref) {
51
+ return {
52
+ id: ref.id,
53
+ version: ref.version,
54
+ user_id: ref.userId,
55
+ state_code: ref.stateCode,
56
+ cluster_id: ref.clusterId,
57
+ source: ref.source,
58
+ };
59
+ }
60
+ function buildMaterializedRow(lookup, context) {
61
+ const resolvedFlowId = lookup.row.id || context.requested_ref.id;
62
+ const resolvedVersion = lookup.row.version || context.requested_ref.version || '';
63
+ return {
64
+ id: resolvedFlowId,
65
+ version: resolvedVersion,
66
+ user_id: lookup.row.user_id,
67
+ state_code: lookup.row.state_code,
68
+ modified_at: lookup.row.modified_at,
69
+ json: normalizeSupabaseFlowPayload(lookup.row.json, `${resolvedFlowId}@${resolvedVersion}`),
70
+ _materialization: context,
71
+ };
72
+ }
73
+ function buildReviewInputRow(row, flowKey, contexts) {
74
+ return {
75
+ ...row,
76
+ _materialization: {
77
+ flow_key: flowKey,
78
+ materialized_ref_count: contexts.length,
79
+ materialized_from_refs: contexts,
80
+ },
81
+ };
82
+ }
83
+ function nowIso(now = new Date()) {
84
+ return now.toISOString();
85
+ }
86
+ export async function runFlowFetchRows(options) {
87
+ const refsFile = normalizeToken(options.refsFile);
88
+ if (!refsFile) {
89
+ throw new CliError('Missing required --refs-file value.', {
90
+ code: 'FLOW_FETCH_ROWS_REFS_FILE_REQUIRED',
91
+ exitCode: 2,
92
+ });
93
+ }
94
+ const outDir = normalizeToken(options.outDir);
95
+ if (!outDir) {
96
+ throw new CliError('Missing required --out-dir value.', {
97
+ code: 'FLOW_FETCH_ROWS_OUT_DIR_REQUIRED',
98
+ exitCode: 2,
99
+ });
100
+ }
101
+ const resolvedRefsFile = path.resolve(refsFile);
102
+ const resolvedOutDir = path.resolve(outDir);
103
+ const allowLatestFallback = options.allowLatestFallback !== false;
104
+ const rows = loadRowsFromFile(resolvedRefsFile);
105
+ const fetchImpl = options.fetchImpl ?? fetch;
106
+ const timeoutMs = options.timeoutMs ?? FLOW_FETCH_ROWS_TIMEOUT_MS;
107
+ const runtime = createSupabaseDataRuntime({
108
+ runtime: requireSupabaseRestRuntime(options.env ?? process.env),
109
+ fetchImpl,
110
+ timeoutMs,
111
+ now: options.now,
112
+ });
113
+ const resolvedRowArtifacts = [];
114
+ const missingRefs = [];
115
+ const ambiguousRefs = [];
116
+ const reviewInputByKey = new Map();
117
+ const resolutionCounts = {
118
+ remote_supabase_exact: 0,
119
+ remote_supabase_latest: 0,
120
+ remote_supabase_latest_fallback: 0,
121
+ };
122
+ for (let index = 0; index < rows.length; index += 1) {
123
+ const ref = normalizeFlowFetchRef(rows[index], index);
124
+ const requestedRef = toRequestedRefSummary(ref);
125
+ let lookup;
126
+ try {
127
+ lookup = await fetchOneFlowRow({
128
+ runtime,
129
+ id: ref.id,
130
+ version: ref.version,
131
+ userId: ref.userId,
132
+ stateCode: ref.stateCode,
133
+ timeoutMs,
134
+ fetchImpl,
135
+ fallbackToLatest: allowLatestFallback && ref.version !== null,
136
+ });
137
+ }
138
+ catch (error) {
139
+ if (error instanceof CliError && error.code === 'FLOW_GET_AMBIGUOUS') {
140
+ ambiguousRefs.push({
141
+ input_index: index,
142
+ requested_ref: requestedRef,
143
+ code: error.code,
144
+ message: error.message,
145
+ details: error.details,
146
+ });
147
+ continue;
148
+ }
149
+ throw error;
150
+ }
151
+ if (!lookup) {
152
+ missingRefs.push({
153
+ input_index: index,
154
+ requested_ref: requestedRef,
155
+ code: 'FLOW_GET_NOT_FOUND',
156
+ message: ref.version
157
+ ? `Could not resolve flow dataset for ${ref.id}@${ref.version}.`
158
+ : `Could not resolve flow dataset for ${ref.id}.`,
159
+ });
160
+ continue;
161
+ }
162
+ resolutionCounts[lookup.resolution] += 1;
163
+ const resolvedFlowId = lookup.row.id || ref.id;
164
+ const resolvedVersion = lookup.row.version || ref.version || '';
165
+ const context = {
166
+ input_index: index,
167
+ requested_ref: requestedRef,
168
+ resolution: lookup.resolution,
169
+ source_url: lookup.sourceUrl,
170
+ resolved_flow_id: resolvedFlowId,
171
+ resolved_version: resolvedVersion,
172
+ };
173
+ const materializedRow = buildMaterializedRow(lookup, context);
174
+ resolvedRowArtifacts.push(materializedRow);
175
+ const flowKey = `${resolvedFlowId}@${resolvedVersion}`;
176
+ const existing = reviewInputByKey.get(flowKey);
177
+ if (existing) {
178
+ existing.contexts.push(context);
179
+ }
180
+ else {
181
+ reviewInputByKey.set(flowKey, {
182
+ row: materializedRow,
183
+ contexts: [context],
184
+ });
185
+ }
186
+ }
187
+ const reviewInputRows = [...reviewInputByKey.entries()]
188
+ .sort(([left], [right]) => left.localeCompare(right))
189
+ .map(([flowKey, entry]) => buildReviewInputRow(entry.row, flowKey, entry.contexts));
190
+ const duplicateReviewInputRowsCollapsed = resolvedRowArtifacts.length - reviewInputRows.length;
191
+ const unresolvedRefCount = missingRefs.length + ambiguousRefs.length;
192
+ const status = unresolvedRefCount > 0
193
+ ? 'completed_flow_row_materialization_with_gaps'
194
+ : 'completed_flow_row_materialization';
195
+ const resolvedRowsPath = path.join(resolvedOutDir, 'resolved-flow-rows.jsonl');
196
+ const reviewInputRowsPath = path.join(resolvedOutDir, 'review-input-rows.jsonl');
197
+ const missingRefsPath = path.join(resolvedOutDir, 'missing-flow-refs.jsonl');
198
+ const ambiguousRefsPath = path.join(resolvedOutDir, 'ambiguous-flow-refs.jsonl');
199
+ const summaryPath = path.join(resolvedOutDir, 'fetch-summary.json');
200
+ writeJsonLinesArtifact(resolvedRowsPath, resolvedRowArtifacts);
201
+ writeJsonLinesArtifact(reviewInputRowsPath, reviewInputRows);
202
+ writeJsonLinesArtifact(missingRefsPath, missingRefs);
203
+ writeJsonLinesArtifact(ambiguousRefsPath, ambiguousRefs);
204
+ const report = {
205
+ schema_version: 1,
206
+ generated_at_utc: nowIso(options.now),
207
+ status,
208
+ refs_file: resolvedRefsFile,
209
+ out_dir: resolvedOutDir,
210
+ allow_latest_fallback: allowLatestFallback,
211
+ requested_ref_count: rows.length,
212
+ resolved_ref_count: resolvedRowArtifacts.length,
213
+ review_input_row_count: reviewInputRows.length,
214
+ duplicate_review_input_rows_collapsed: duplicateReviewInputRowsCollapsed,
215
+ missing_ref_count: missingRefs.length,
216
+ ambiguous_ref_count: ambiguousRefs.length,
217
+ resolution_counts: resolutionCounts,
218
+ files: {
219
+ resolved_flow_rows: resolvedRowsPath,
220
+ review_input_rows: reviewInputRowsPath,
221
+ fetch_summary: summaryPath,
222
+ missing_flow_refs: missingRefsPath,
223
+ ambiguous_flow_refs: ambiguousRefsPath,
224
+ },
225
+ };
226
+ writeJsonArtifact(summaryPath, report);
227
+ return report;
228
+ }
229
+ export const __testInternals = {
230
+ normalizeFlowFetchRef,
231
+ normalizeOptionalNonNegativeInteger,
232
+ normalizeToken,
233
+ toRequestedRefSummary,
234
+ };
235
+ //# sourceMappingURL=flow-fetch-rows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow-fetch-rows.js","sourceRoot":"","sources":["../../../src/lib/flow-fetch-rows.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAC3E,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAmB,MAAM,sBAAsB,CAAC;AAEzE,OAAO,EACL,eAAe,EACf,4BAA4B,GAE7B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAElE,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAgE1C,SAAS,cAAc,CAAC,KAAc;IACpC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC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AG,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,oBAAoB,CAAC,CAAC;IAEpE,sBAAsB,CAAC,gBAAgB,EAAE,oBAAoB,CAAC,CAAC;IAC/D,sBAAsB,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;IAC7D,sBAAsB,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC;IACrD,sBAAsB,CAAC,iBAAiB,EAAE,aAAa,CAAC,CAAC;IAEzD,MAAM,MAAM,GAAwB;QAClC,cAAc,EAAE,CAAC;QACjB,gBAAgB,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;QACrC,MAAM;QACN,SAAS,EAAE,gBAAgB;QAC3B,OAAO,EAAE,cAAc;QACvB,qBAAqB,EAAE,mBAAmB;QAC1C,mBAAmB,EAAE,IAAI,CAAC,MAAM;QAChC,kBAAkB,EAAE,oBAAoB,CAAC,MAAM;QAC/C,sBAAsB,EAAE,eAAe,CAAC,MAAM;QAC9C,qCAAqC,EAAE,iCAAiC;QACxE,iBAAiB,EAAE,WAAW,CAAC,MAAM;QACrC,mBAAmB,EAAE,aAAa,CAAC,MAAM;QACzC,iBAAiB,EAAE,gBAAgB;QACnC,KAAK,EAAE;YACL,kBAAkB,EAAE,gBAAgB;YACpC,iBAAiB,EAAE,mBAAmB;YACtC,aAAa,EAAE,WAAW;YAC1B,iBAAiB,EAAE,eAAe;YAClC,mBAAmB,EAAE,iBAAiB;SACvC;KACF,CAAC;IAEF,iBAAiB,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACvC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,qBAAqB;IACrB,mCAAmC;IACnC,cAAc;IACd,qBAAqB;CACtB,CAAC","sourcesContent":["import path from 'node:path';\nimport { writeJsonArtifact, writeJsonLinesArtifact } from './artifacts.js';\nimport { CliError } from './errors.js';\nimport { loadRowsFromFile, type JsonRecord } from './flow-governance.js';\nimport type { FetchLike } from './http.js';\nimport {\n fetchOneFlowRow,\n normalizeSupabaseFlowPayload,\n type SupabaseFlowLookup,\n} from './flow-read.js';\nimport { requireSupabaseRestRuntime } from './supabase-rest.js';\nimport { createSupabaseDataRuntime } from './supabase-session.js';\n\nconst FLOW_FETCH_ROWS_TIMEOUT_MS = 10_000;\n\ntype FlowFetchRef = {\n id: string;\n version: string | null;\n userId: string | null;\n stateCode: number | null;\n clusterId: string | null;\n source: string | null;\n};\n\ntype FlowFetchMaterializationContext = {\n input_index: number;\n requested_ref: {\n id: string;\n version: string | null;\n user_id: string | null;\n state_code: number | null;\n cluster_id: string | null;\n source: string | null;\n };\n resolution: SupabaseFlowLookup['resolution'];\n source_url: string;\n resolved_flow_id: string;\n resolved_version: string;\n};\n\ntype FlowFetchSummaryStatus =\n | 'completed_flow_row_materialization'\n | 'completed_flow_row_materialization_with_gaps';\n\nexport type RunFlowFetchRowsOptions = {\n refsFile: string;\n outDir: string;\n allowLatestFallback?: boolean;\n env?: NodeJS.ProcessEnv;\n fetchImpl?: FetchLike;\n timeoutMs?: number;\n now?: Date;\n};\n\nexport type FlowFetchRowsReport = {\n schema_version: 1;\n generated_at_utc: string;\n status: FlowFetchSummaryStatus;\n refs_file: string;\n out_dir: string;\n allow_latest_fallback: boolean;\n requested_ref_count: number;\n resolved_ref_count: number;\n review_input_row_count: number;\n duplicate_review_input_rows_collapsed: number;\n missing_ref_count: number;\n ambiguous_ref_count: number;\n resolution_counts: Record<SupabaseFlowLookup['resolution'], number>;\n files: {\n resolved_flow_rows: string;\n review_input_rows: string;\n fetch_summary: string;\n missing_flow_refs: string;\n ambiguous_flow_refs: string;\n };\n};\n\nfunction normalizeToken(value: unknown): string | null {\n if (value === undefined || value === null) {\n return null;\n }\n\n const trimmed = String(value).trim();\n return trimmed ? trimmed : null;\n}\n\nfunction normalizeOptionalNonNegativeInteger(\n value: unknown,\n label: string,\n code: string,\n): number | null {\n if (value === undefined || value === null || value === '') {\n return null;\n }\n\n const parsed =\n typeof value === 'number' && Number.isInteger(value)\n ? value\n : Number.parseInt(String(value), 10);\n if (!Number.isInteger(parsed) || parsed < 0) {\n throw new CliError(`Expected ${label} to be a non-negative integer.`, {\n code,\n exitCode: 2,\n details: value,\n });\n }\n return parsed;\n}\n\nfunction normalizeFlowFetchRef(row: JsonRecord, index: number): FlowFetchRef {\n const id = normalizeToken(row.id);\n if (!id) {\n throw new CliError(`Flow ref row ${index + 1} is missing required id.`, {\n code: 'FLOW_FETCH_ROWS_REF_ID_REQUIRED',\n exitCode: 2,\n details: row,\n });\n }\n\n return {\n id,\n version: normalizeToken(row.version),\n userId: normalizeToken(row.user_id ?? row.userId),\n stateCode: normalizeOptionalNonNegativeInteger(\n row.state_code ?? row.stateCode,\n 'flow ref state_code',\n 'FLOW_FETCH_ROWS_INVALID_STATE_CODE',\n ),\n clusterId: normalizeToken(row.cluster_id ?? row.clusterId),\n source: normalizeToken(row.source),\n };\n}\n\nfunction toRequestedRefSummary(\n ref: FlowFetchRef,\n): FlowFetchMaterializationContext['requested_ref'] {\n return {\n id: ref.id,\n version: ref.version,\n user_id: ref.userId,\n state_code: ref.stateCode,\n cluster_id: ref.clusterId,\n source: ref.source,\n };\n}\n\nfunction buildMaterializedRow(\n lookup: SupabaseFlowLookup,\n context: FlowFetchMaterializationContext,\n): JsonRecord {\n const resolvedFlowId = lookup.row.id || context.requested_ref.id;\n const resolvedVersion = lookup.row.version || context.requested_ref.version || '';\n\n return {\n id: resolvedFlowId,\n version: resolvedVersion,\n user_id: lookup.row.user_id,\n state_code: lookup.row.state_code,\n modified_at: lookup.row.modified_at,\n json: normalizeSupabaseFlowPayload(lookup.row.json, `${resolvedFlowId}@${resolvedVersion}`),\n _materialization: context,\n };\n}\n\nfunction buildReviewInputRow(\n row: JsonRecord,\n flowKey: string,\n contexts: FlowFetchMaterializationContext[],\n): JsonRecord {\n return {\n ...row,\n _materialization: {\n flow_key: flowKey,\n materialized_ref_count: contexts.length,\n materialized_from_refs: contexts,\n },\n };\n}\n\nfunction nowIso(now: Date = new Date()): string {\n return now.toISOString();\n}\n\nexport async function runFlowFetchRows(\n options: RunFlowFetchRowsOptions,\n): Promise<FlowFetchRowsReport> {\n const refsFile = normalizeToken(options.refsFile);\n if (!refsFile) {\n throw new CliError('Missing required --refs-file value.', {\n code: 'FLOW_FETCH_ROWS_REFS_FILE_REQUIRED',\n exitCode: 2,\n });\n }\n\n const outDir = normalizeToken(options.outDir);\n if (!outDir) {\n throw new CliError('Missing required --out-dir value.', {\n code: 'FLOW_FETCH_ROWS_OUT_DIR_REQUIRED',\n exitCode: 2,\n });\n }\n\n const resolvedRefsFile = path.resolve(refsFile);\n const resolvedOutDir = path.resolve(outDir);\n const allowLatestFallback = options.allowLatestFallback !== false;\n const rows = loadRowsFromFile(resolvedRefsFile);\n\n const fetchImpl = options.fetchImpl ?? (fetch as FetchLike);\n const timeoutMs = options.timeoutMs ?? FLOW_FETCH_ROWS_TIMEOUT_MS;\n const runtime = createSupabaseDataRuntime({\n runtime: requireSupabaseRestRuntime(options.env ?? process.env),\n fetchImpl,\n timeoutMs,\n now: options.now,\n });\n\n const resolvedRowArtifacts: JsonRecord[] = [];\n const missingRefs: JsonRecord[] = [];\n const ambiguousRefs: JsonRecord[] = [];\n const reviewInputByKey = new Map<\n string,\n { row: JsonRecord; contexts: FlowFetchMaterializationContext[] }\n >();\n const resolutionCounts: FlowFetchRowsReport['resolution_counts'] = {\n remote_supabase_exact: 0,\n remote_supabase_latest: 0,\n remote_supabase_latest_fallback: 0,\n };\n\n for (let index = 0; index < rows.length; index += 1) {\n const ref = normalizeFlowFetchRef(rows[index] as JsonRecord, index);\n const requestedRef = toRequestedRefSummary(ref);\n\n let lookup: SupabaseFlowLookup | null;\n try {\n lookup = await fetchOneFlowRow({\n runtime,\n id: ref.id,\n version: ref.version,\n userId: ref.userId,\n stateCode: ref.stateCode,\n timeoutMs,\n fetchImpl,\n fallbackToLatest: allowLatestFallback && ref.version !== null,\n });\n } catch (error) {\n if (error instanceof CliError && error.code === 'FLOW_GET_AMBIGUOUS') {\n ambiguousRefs.push({\n input_index: index,\n requested_ref: requestedRef,\n code: error.code,\n message: error.message,\n details: error.details,\n });\n continue;\n }\n throw error;\n }\n\n if (!lookup) {\n missingRefs.push({\n input_index: index,\n requested_ref: requestedRef,\n code: 'FLOW_GET_NOT_FOUND',\n message: ref.version\n ? `Could not resolve flow dataset for ${ref.id}@${ref.version}.`\n : `Could not resolve flow dataset for ${ref.id}.`,\n });\n continue;\n }\n\n resolutionCounts[lookup.resolution] += 1;\n const resolvedFlowId = lookup.row.id || ref.id;\n const resolvedVersion = lookup.row.version || ref.version || '';\n const context: FlowFetchMaterializationContext = {\n input_index: index,\n requested_ref: requestedRef,\n resolution: lookup.resolution,\n source_url: lookup.sourceUrl,\n resolved_flow_id: resolvedFlowId,\n resolved_version: resolvedVersion,\n };\n const materializedRow = buildMaterializedRow(lookup, context);\n resolvedRowArtifacts.push(materializedRow);\n\n const flowKey = `${resolvedFlowId}@${resolvedVersion}`;\n const existing = reviewInputByKey.get(flowKey);\n if (existing) {\n existing.contexts.push(context);\n } else {\n reviewInputByKey.set(flowKey, {\n row: materializedRow,\n contexts: [context],\n });\n }\n }\n\n const reviewInputRows = [...reviewInputByKey.entries()]\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([flowKey, entry]) => buildReviewInputRow(entry.row, flowKey, entry.contexts));\n\n const duplicateReviewInputRowsCollapsed = resolvedRowArtifacts.length - reviewInputRows.length;\n const unresolvedRefCount = missingRefs.length + ambiguousRefs.length;\n const status: FlowFetchSummaryStatus =\n unresolvedRefCount > 0\n ? 'completed_flow_row_materialization_with_gaps'\n : 'completed_flow_row_materialization';\n\n const resolvedRowsPath = path.join(resolvedOutDir, 'resolved-flow-rows.jsonl');\n const reviewInputRowsPath = path.join(resolvedOutDir, 'review-input-rows.jsonl');\n const missingRefsPath = path.join(resolvedOutDir, 'missing-flow-refs.jsonl');\n const ambiguousRefsPath = path.join(resolvedOutDir, 'ambiguous-flow-refs.jsonl');\n const summaryPath = path.join(resolvedOutDir, 'fetch-summary.json');\n\n writeJsonLinesArtifact(resolvedRowsPath, resolvedRowArtifacts);\n writeJsonLinesArtifact(reviewInputRowsPath, reviewInputRows);\n writeJsonLinesArtifact(missingRefsPath, missingRefs);\n writeJsonLinesArtifact(ambiguousRefsPath, ambiguousRefs);\n\n const report: FlowFetchRowsReport = {\n schema_version: 1,\n generated_at_utc: nowIso(options.now),\n status,\n refs_file: resolvedRefsFile,\n out_dir: resolvedOutDir,\n allow_latest_fallback: allowLatestFallback,\n requested_ref_count: rows.length,\n resolved_ref_count: resolvedRowArtifacts.length,\n review_input_row_count: reviewInputRows.length,\n duplicate_review_input_rows_collapsed: duplicateReviewInputRowsCollapsed,\n missing_ref_count: missingRefs.length,\n ambiguous_ref_count: ambiguousRefs.length,\n resolution_counts: resolutionCounts,\n files: {\n resolved_flow_rows: resolvedRowsPath,\n review_input_rows: reviewInputRowsPath,\n fetch_summary: summaryPath,\n missing_flow_refs: missingRefsPath,\n ambiguous_flow_refs: ambiguousRefsPath,\n },\n };\n\n writeJsonArtifact(summaryPath, report);\n return report;\n}\n\nexport const __testInternals = {\n normalizeFlowFetchRef,\n normalizeOptionalNonNegativeInteger,\n normalizeToken,\n toRequestedRefSummary,\n};\n"]}