Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 20:45 UTC
feat(trace): tie every mapping account and note exposure to typed trial-balance source rows
davoodmehraban89 committed
Nov 1, 2026 at 00:05 UTC
34e384a61fc572fe81ea554de0deb375a19dfe7d
1 file changed
+124
-1
backend/src/aram_backend/desktop_five_statement_integrity.py
+124
-1
@@ -3,7 +3,10 @@ from __future__ import annotations
3
4
import re
5
6
-from aram_backend.coverage_preview import CoverageError
6
+from aram_backend.coverage_preview import (
7
+ AccountTrace, CoverageError, CoveragePreview, MappingTrace,
8
+)
9
+from aram_backend.reconciliation_review import _money, _sum_exact
10
from aram_backend.balance_sheet_candidate import BalanceSheetCandidate
11
from aram_backend.cash_flow_direct_candidate import DirectCashFlowCandidate
12
from aram_backend.cross_statement_reconciliation import CrossStatementReconciliation
@@ -147,6 +150,126 @@ def validate_desktop_pipeline_result(result: FiveStatementPipelineResult) -> Non
150
)
151
if any(left != right for left, right in ties):
152
raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_MISMATCH")
153
+ # The mapping trace is generated from the same immutable XLSX bytes as
154
+ # the five statements. Do not render account-to-note lineage if it lacks
155
+ # the exact company, year, source hash, account rows or debit-net figures.
156
+ coverage = result.mapping_coverage
157
+ if type(coverage) is not CoveragePreview:
158
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_TYPE_INVALID")
159
+ if (
160
+ coverage.kind != "UNAPPROVED_MAPPING_COVERAGE_PREVIEW"
161
+ or type(coverage.financial_statement) is not bool
162
+ or type(coverage.trusted_approvals) is not bool
163
+ or type(coverage.releasable) is not bool
164
+ or coverage.financial_statement or coverage.trusted_approvals
165
+ or coverage.releasable
166
+ or coverage.company_code != reconciliation.company_code
167
+ or coverage.fiscal_year != reconciliation.fiscal_year
168
+ or coverage.source_sha256 != reconciliation.source_sha256
169
+ ):
170
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_PROVENANCE_INVALID")
171
+ if (type(coverage.accounts) is not tuple
172
+ or type(coverage.mappings) is not tuple
173
+ or type(coverage.notes) is not tuple
174
+ or type(coverage.account_count) is not int
175
+ or coverage.account_count != len(coverage.accounts)):
176
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_COUNT_INVALID")
177
+ tb_by_code = {
178
+ row.account_code: row for row in result.trial_balance.accounts
179
+ }
180
+ if len(tb_by_code) != len(result.trial_balance.accounts):
181
+ raise CoverageError("DESKTOP_PREVIEW_TB_DUPLICATE_ACCOUNT")
182
+ by_account: dict[str, AccountTrace] = {}
183
+ for account in coverage.accounts:
184
+ if type(account) is not AccountTrace or account.account_code in by_account:
185
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_ACCOUNT_INVALID")
186
+ tb_account = tb_by_code.get(account.account_code)
187
+ if (
188
+ tb_account is None or account.account_name != tb_account.account_name
189
+ or account.account_excel_row != tb_account.excel_row
190
+ or account.source_row != tb_account.source_row
191
+ or _money(account.closing_exposure) != _money(tb_account.closing)
192
+ ):
193
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_SOURCE_MISMATCH")
194
+ by_account[account.account_code] = account
195
+ if set(by_account) != set(tb_by_code):
196
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_SOURCE_MISMATCH")
197
+
198
+ mapping_counts: dict[str, int] = {}
199
+ included_accounts: set[str] = set()
200
+ seen_mapping_rows: set[int] = set()
201
+ seen_mapping_ids: set[str] = set()
202
+ note_amounts: dict[tuple[str, str, str, str], list] = {}
203
+ note_codes: dict[tuple[str, str, str, str], list[str]] = {}
204
+ seen_account_statement: set[tuple[str, str]] = set()
205
+ for mapping in coverage.mappings:
206
+ if type(mapping) is not MappingTrace:
207
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_ROW_INVALID")
208
+ account = by_account.get(mapping.account_code)
209
+ if (
210
+ account is None or type(mapping.included) is not bool
211
+ or mapping.account_name != account.account_name
212
+ or mapping.account_excel_row != account.account_excel_row
213
+ or mapping.source_row != account.source_row
214
+ or _money(mapping.closing_exposure) != _money(account.closing_exposure)
215
+ or type(mapping.mapping_excel_row) is not int
216
+ or mapping.mapping_excel_row < 2
217
+ or mapping.mapping_excel_row in seen_mapping_rows
218
+ or not isinstance(mapping.mapping_id, str)
219
+ or not mapping.mapping_id
220
+ or mapping.mapping_id in seen_mapping_ids
221
+ ):
222
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_ROW_INVALID")
223
+ seen_mapping_rows.add(mapping.mapping_excel_row)
224
+ seen_mapping_ids.add(mapping.mapping_id)
225
+ mapping_counts[mapping.account_code] = (
226
+ mapping_counts.get(mapping.account_code, 0) + 1
227
+ )
228
+ if mapping.included:
229
+ statement_key = (mapping.account_code, mapping.target_statement)
230
+ if statement_key in seen_account_statement:
231
+ raise CoverageError("DESKTOP_PREVIEW_DUPLICATE_MAPPING_TARGET")
232
+ seen_account_statement.add(statement_key)
233
+ included_accounts.add(mapping.account_code)
234
+ note_key = (
235
+ mapping.target_statement, mapping.target_section,
236
+ mapping.target_note, mapping.target_subnote,
237
+ )
238
+ note_amounts.setdefault(note_key, []).append(
239
+ _money(mapping.closing_exposure)
240
+ )
241
+ note_codes.setdefault(note_key, []).append(mapping.account_code)
242
+ for account in coverage.accounts:
243
+ if (
244
+ type(account.mapping_count) is not int
245
+ or account.mapping_count != mapping_counts.get(account.account_code, 0)
246
+ or type(account.included) is not bool
247
+ or account.included != (account.account_code in included_accounts)
248
+ ):
249
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_COUNT_INVALID")
250
+ if (
251
+ coverage.candidate_mapped_accounts != len(included_accounts)
252
+ or coverage.blocked_accounts != len(coverage.accounts) - len(included_accounts)
253
+ ):
254
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_COUNT_INVALID")
255
+
256
+ seen_note_keys: set[tuple[str, str, str, str]] = set()
257
+ for note in coverage.notes:
258
+ note_key = (
259
+ note.statement, note.section, note.note_number, note.subnote_code,
260
+ )
261
+ if note_key in seen_note_keys:
262
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_NOTE_DUPLICATE")
263
+ seen_note_keys.add(note_key)
264
+ if (
265
+ note_key not in note_amounts
266
+ or _money(note.closing_exposure) != _sum_exact(note_amounts[note_key])
267
+ or tuple(note_codes[note_key]) != note.account_codes
268
+ ):
269
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_NOTE_INVALID")
270
+ if seen_note_keys != set(note_amounts):
271
+ raise CoverageError("DESKTOP_PREVIEW_MAPPING_NOTE_INVALID")
272
+
273
overall = _require_bool(result, "eligible_for_analyst_review")
274
if overall:
275
if (not _require_bool(reconciliation, "eligible_for_analyst_review")