Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 18:15 UTC
fix(cli): validate manifest reference-policy graph, cash scope and unique transaction IDs
davoodmehraban89 committed
Oct 31, 2026 at 18:20 UTC
16ef242d035963005ea3c5713808478e61a6d66e
1 file changed
+95
-1
backend/src/aram_backend/five_statement_pipeline_cli.py
+95
-1
@@ -85,6 +85,7 @@ def _rows(
85
string_fields: tuple[str, ...] = (),
86
sign_field: str | None = None,
87
amount_fields: tuple[str, ...] = (),
88
+ optional_text_fields: tuple[str, ...] = (),
89
) -> tuple[Any, ...]:
90
items = payload[key]
91
if (not isinstance(items, list) or len(items) < minimum
@@ -102,6 +103,14 @@ def _rows(
103
raise CoverageError(
104
"PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID"
105
)
106
+ for field in optional_text_fields:
107
+ value = item[field]
108
+ if (not isinstance(value, str) or value != value.strip()
109
+ or len(value) > 256
110
+ or any(ord(char) < 32 or ord(char) == 127 for char in value)):
111
+ raise CoverageError(
112
+ "PIPELINE_MANIFEST_" + key.upper() + "_ROW_INVALID"
113
+ )
114
for field in amount_fields:
115
value = item[field]
116
if not isinstance(value, str) or value != value.strip():
@@ -127,6 +136,85 @@ def _rows(
136
return tuple(result)
137
138
139
+def _validate_manifest_references(manifest: ParsedPipelineManifest) -> ParsedPipelineManifest:
140
+ """Fail closed on misleading cross-links before any XLSX is opened.
141
+
142
+ The caller supplies a reference/policy graph, not an authenticated approval.
143
+ Missing policies are left to the engines as *blocked preview findings*,
144
+ while duplicate or dangling input links are malformed manifest errors.
145
+ """
146
+ refs: dict[tuple[str, str, str, str], NoteReference] = {}
147
+ for item in manifest.references:
148
+ if item.statement not in {"BS", "IS", "EQ", "CF"}:
149
+ raise CoverageError("PIPELINE_MANIFEST_REFERENCE_STATEMENT_INVALID")
150
+ key = (item.statement, item.section, item.note_number, item.subnote_code)
151
+ if key in refs:
152
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_NOTE_REFERENCE")
153
+ refs[key] = item
154
+
155
+ policies_by_statement: dict[str, dict[tuple[str, str, str, str], object]] = {}
156
+ for statement, policies in (
157
+ ("BS", manifest.balance_sheet_policies),
158
+ ("IS", manifest.income_statement_policies),
159
+ ("EQ", manifest.equity_movement_policies),
160
+ ):
161
+ policy_by_key: dict[tuple[str, str, str, str], object] = {}
162
+ for item in policies:
163
+ key = (
164
+ statement, item.statement_section, item.note_number,
165
+ item.subnote_code,
166
+ )
167
+ if key in policy_by_key:
168
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_" + statement + "_POLICY")
169
+ if key not in refs:
170
+ raise CoverageError("PIPELINE_MANIFEST_ORPHAN_" + statement + "_POLICY")
171
+ policy_by_key[key] = item
172
+ policies_by_statement[statement] = policy_by_key
173
+
174
+ cash_note_keys: set[tuple[str, str, str, str]] = set()
175
+ for selection in manifest.cash_balance_notes:
176
+ key = (
177
+ "BS", selection.statement_section, selection.note_number,
178
+ selection.subnote_code,
179
+ )
180
+ if key in cash_note_keys:
181
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_CASH_NOTE_SELECTION")
182
+ cash_note_keys.add(key)
183
+ if key not in refs:
184
+ raise CoverageError("PIPELINE_MANIFEST_CASH_NOTE_REFERENCE_MISSING")
185
+ policy = policies_by_statement["BS"].get(key)
186
+ if policy is None or policy.category != "ASSETS":
187
+ raise CoverageError("PIPELINE_MANIFEST_CASH_NOTE_NOT_ASSET")
188
+
189
+ cash_codes: set[str] = set()
190
+ for item in manifest.cash_accounts:
191
+ if item.account_code in cash_codes:
192
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_CASH_ACCOUNT")
193
+ cash_codes.add(item.account_code)
194
+
195
+ counterpart_codes: set[str] = set()
196
+ for item in manifest.counterpart_policies:
197
+ if item.account_code in cash_codes:
198
+ raise CoverageError("PIPELINE_MANIFEST_CASH_COUNTERPART_OVERLAP")
199
+ if item.account_code in counterpart_codes:
200
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_COUNTERPART_POLICY")
201
+ counterpart_codes.add(item.account_code)
202
+
203
+ line_ids: set[str] = set()
204
+ for item in manifest.transactions:
205
+ if item.line_id in line_ids:
206
+ raise CoverageError("PIPELINE_MANIFEST_DUPLICATE_TRANSACTION_LINE")
207
+ line_ids.add(item.line_id)
208
+ if item.cash_account_code not in cash_codes:
209
+ raise CoverageError("PIPELINE_MANIFEST_UNKNOWN_CASH_ACCOUNT")
210
+ if item.cash_account_code == item.counterpart_account_code:
211
+ raise CoverageError("PIPELINE_MANIFEST_SAME_ACCOUNT_PAIR")
212
+ if (item.counterpart_account_code not in counterpart_codes
213
+ and item.counterpart_account_code not in cash_codes):
214
+ raise CoverageError("PIPELINE_MANIFEST_UNKNOWN_COUNTERPART_ACCOUNT")
215
+ return manifest
216
+
217
+
218
def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
219
if (not isinstance(content, bytes)
220
or not 0 < len(content) <= MAX_CASH_MANIFEST_BYTES):
@@ -168,7 +256,7 @@ def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
256
or item.get("fiscal_year") != year)):
257
raise CoverageError("PIPELINE_MANIFEST_TRANSACTION_SCOPE_MISMATCH")
258
171
- return ParsedPipelineManifest(
259
+ manifest = ParsedPipelineManifest(
260
company_code=company,
261
fiscal_year=year,
262
currency=currency,
@@ -178,24 +266,28 @@ def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
266
MAX_POLICY_ROWS, minimum=1,
267
string_fields=("statement", "section", "note_number", "evidence_ref"),
268
amount_fields=("expected_closing_exposure",),
269
+ optional_text_fields=("subnote_code",),
270
),
271
balance_sheet_policies=_rows(
272
payload, "balance_sheet_policies", BS_POLICY_FIELDS,
273
BalanceSheetLinePolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
274
string_fields=("statement_section", "note_number", "category",
275
"line_label", "policy_evidence_ref"),
276
+ optional_text_fields=("subnote_code",),
277
),
278
income_statement_policies=_rows(
279
payload, "income_statement_policies", IS_POLICY_FIELDS,
280
IncomeStatementLinePolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
281
string_fields=("statement_section", "note_number", "category",
282
"line_label", "policy_evidence_ref"),
283
+ optional_text_fields=("subnote_code",),
284
),
285
equity_movement_policies=_rows(
286
payload, "equity_movement_policies", EQ_POLICY_FIELDS,
287
EquityMovementPolicy, MAX_POLICY_ROWS, sign_field="sign_multiplier",
288
string_fields=("statement_section", "note_number", "equity_component",
289
"movement_category", "line_label", "policy_evidence_ref"),
290
+ optional_text_fields=("subnote_code",),
291
),
292
cash_accounts=_rows(
293
payload, "cash_accounts", CASH_ACCOUNT_FIELDS,
@@ -220,8 +312,10 @@ def parse_pipeline_manifest(content: bytes) -> ParsedPipelineManifest:
312
payload, "cash_balance_notes", CASH_NOTE_FIELDS,
313
CashBalanceNoteSelection, MAX_POLICY_ROWS, minimum=1,
314
string_fields=("statement_section", "note_number", "evidence_ref"),
315
+ optional_text_fields=("subnote_code",),
316
),
317
)
318
+ return _validate_manifest_references(manifest)
319
320
321
def run_pipeline_cli(workbook_path: str, manifest_path: str) -> FiveStatementPipelineResult: