@davoodmehraban89 / Aram-2 Mirror · read-only
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 4, 01:45 UTC

codex/aram-analyst-review-workbook-20261031
add-index-html-10623938925177053894 arena/094f314c-aram chatgpt-phase1b2-autopilot codex/adr-future-connectors-pi-v1 codex/aram-account-note-source-trace-20261031 codex/aram-actions-node24-maintenance-20261031 codex/aram-analyst-review-workbook-20261031 codex/aram-bs-candidate-statement-20261031 codex/aram-cash-flow-direct-candidate-20261031 codex/aram-clean-reference-year-integrity-20261030 codex/aram-cross-statement-controls-20261031 codex/aram-equity-changes-candidate-20261031 codex/aram-five-statement-cli-20261031 codex/aram-five-statement-integration-20261031 codex/aram-five-statement-orchestrator-20261031 codex/aram-gate3-coverage-target-guard-20261030 codex/aram-gate3-hidden-xlsx-guard-20261030 codex/aram-gate3-integration-validation-20261030 codex/aram-gate3-mapping-change-preview-20261030 codex/aram-gate3-stale-note-guard-20261030 codex/aram-income-statement-candidate-20261031 codex/aram-manifest-referential-integrity-20261031 codex/aram-mapping-source-parser-20261101 codex/aram-multidimensional-mapping-20261101 codex/aram-multistatement-mapping-20261031 codex/aram-pipeline-manifest-schema-20261031 codex/aram-readonly-mapping-coverage-20261031 codex/aram-reconciliation-review-gate-20261031 codex/aram-same-flush-fiscal-lock-20261030 codex/aram-synthetic-windows-demo-20261031 codex/aram-trial-balance-diagnostic-20261031 codex/aram-windows-background-detail-search-20261031 codex/aram-windows-detail-review-20261031 codex/aram-windows-five-statement-pilot-20261031 codex/aram-windows-statement-figures-20261031 codex/gate-2a-pilot-workbook codex/gate-2a-pilot-workbook-6864754244772568979 codex/gate3-authenticated-principal-boundary-20261030 codex/gate3-authored-draft-staging-20261030 codex/gate3-carry-forward-preview-20261023 codex/gate3-draft-staging-audit-20261030 codex/gate3-mapping-batch-freshness-20261030 codex/gate3-mapping-batch-review-20261030 codex/gate3-mapping-core codex/gate3-mapping-coverage-20261023 codex/gate3-mapping-excel-20261023 codex/gate3-note-mapping-review-20261023 codex/gate3-signed-year-scope-20261030 codex/gate3-unapproved-proposal-storage-20261030 codex/payvast-mapping-discovery-20261023 codex/phase-2a-backend-foundation codex/phase-2b-post-merge-record codex/phase-2b-source-registration codex/phase-2b-source-registration-12955190277775369594 codex/post-gate2a-control-reconciliation codex/post-pr18-acceptance-record codex/project-control-foundation codex/t1-repo-hygiene-20261023 codex/t2-ci-hardening-20261023 codex/t3-explicit-migrations-20261023 codex/t6a-readonly-diagnostics-20261023 codex/v1-01-safety-gate-hardening codex/v1-year-isolation-followup-20261023 codex/windows-native-pilot-shell-20261030 docs/aram-master-control-plan-20261111 docs/aram-roadmap-dashboard-20261030 docs/financial-statement-reconciliation-1402-1404-20261110 feat/gate3-governed-mapping-20261101 feat/gate3-package9-1-security-20261103 feat/gate3-package9-isolated-20261103 fix/password-unicode-validation-20261111 governance/aram-5-1-3-controls-20261030 governance/role-based-operating-model jules-1168663913887856558-73b8a885 jules-gate-2b-mapping-workflow-foundation-4137724387867065330 phase-1b1-audit-report-7365389436990903239 phase-2a-backend-foundation-10237597358138521261 phase1b2-postmerge-metadata-fix refactor/drg-parent-year-id-15088443704684163304 source-data-cleanup
py 162 lines 9.48 KB
Raw
1 """Fail-closed provenance and tie-out checks for desktop-only previews."""
2 from __future__ import annotations
3
4 import re
5
6 from aram_backend.coverage_preview import CoverageError
7 from aram_backend.balance_sheet_candidate import BalanceSheetCandidate
8 from aram_backend.cash_flow_direct_candidate import DirectCashFlowCandidate
9 from aram_backend.cross_statement_reconciliation import CrossStatementReconciliation
10 from aram_backend.equity_changes_candidate import EquityChangesCandidate
11 from aram_backend.five_statement_pipeline import FiveStatementPipelineResult
12 from aram_backend.income_statement_candidate import IncomeStatementCandidate
13 from aram_backend.trial_balance_diagnostic import TrialBalanceDiagnostic
14
15
16 def _require_false(obj: object, fields: tuple[str, ...]) -> None:
17 for field in fields:
18 value = getattr(obj, field)
19 if type(value) is not bool or value:
20 raise CoverageError("DESKTOP_PREVIEW_PROVENANCE_INVALID")
21
22
23 def _require_bool(obj: object, field: str) -> bool:
24 value = getattr(obj, field)
25 if type(value) is not bool:
26 raise CoverageError("DESKTOP_PREVIEW_BOOLEAN_INVALID")
27 return value
28
29
30 def validate_desktop_pipeline_result(result: FiveStatementPipelineResult) -> None:
31 """Verify the exact typed orchestration result before rendering any data."""
32 if type(result) is not FiveStatementPipelineResult:
33 raise CoverageError("DESKTOP_PREVIEW_PIPELINE_TYPE_INVALID")
34 candidates = (
35 (result.balance_sheet, BalanceSheetCandidate, "UNAPPROVED_BALANCE_SHEET_PRESENTATION_CANDIDATE"),
36 (result.income_statement, IncomeStatementCandidate, "UNAPPROVED_INCOME_STATEMENT_PRESENTATION_CANDIDATE"),
37 (result.equity_changes, EquityChangesCandidate, "UNAPPROVED_EQUITY_CHANGES_PRESENTATION_CANDIDATE"),
38 (result.cash_flow, DirectCashFlowCandidate, "UNAPPROVED_DIRECT_CASH_FLOW_CANDIDATE"),
39 (result.trial_balance, TrialBalanceDiagnostic, "UNAPPROVED_TRIAL_BALANCE_DIAGNOSTIC"),
40 )
41 if result.kind != "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE":
42 raise CoverageError("DESKTOP_PREVIEW_PIPELINE_KIND_INVALID")
43 if type(result.reconciliation) is not CrossStatementReconciliation:
44 raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_TYPE_INVALID")
45 if result.reconciliation.kind != "UNAPPROVED_CROSS_STATEMENT_RECONCILIATION":
46 raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_KIND_INVALID")
47 _require_false(result, (
48 "source_ledger_completeness_verified", "accounting_policy_authorized",
49 "independently_reviewed", "trusted_approvals",
50 "official_financial_statement", "export_authorized", "releasable",
51 ))
52 _require_false(result.reconciliation, (
53 "source_ledger_completeness_verified", "accounting_policy_authorized",
54 "independently_reviewed", "trusted_approvals",
55 "official_financial_statement", "export_authorized", "releasable",
56 ))
57 source_hashes: set[str] = set()
58 companies: set[str] = set()
59 years: set[int] = set()
60 eligibility: list[bool] = []
61 for candidate, expected_type, expected_kind in candidates:
62 if type(candidate) is not expected_type or candidate.kind != expected_kind:
63 raise CoverageError("DESKTOP_PREVIEW_CANDIDATE_TYPE_INVALID")
64 if (not isinstance(candidate.source_sha256, str)
65 or not re.fullmatch(r"[0-9a-f]{64}", candidate.source_sha256)):
66 raise CoverageError("DESKTOP_PREVIEW_SOURCE_HASH_INVALID")
67 source_hashes.add(candidate.source_sha256)
68 if (not isinstance(candidate.company_code, str)
69 or not candidate.company_code
70 or candidate.company_code != candidate.company_code.strip()
71 or type(candidate.fiscal_year) is not int
72 or candidate.fiscal_year <= 0):
73 raise CoverageError("DESKTOP_PREVIEW_SCOPE_INVALID")
74 companies.add(candidate.company_code)
75 years.add(candidate.fiscal_year)
76 for hash_field in (
77 "note_references_sha256", "presentation_policies_sha256",
78 "transaction_ledger_sha256", "classification_policies_sha256",
79 ):
80 if hasattr(candidate, hash_field):
81 digest = getattr(candidate, hash_field)
82 if not isinstance(digest, str) or not re.fullmatch(r"[0-9a-f]{64}", digest):
83 raise CoverageError("DESKTOP_PREVIEW_PROVENANCE_HASH_INVALID")
84 eligible_field = (
85 "balanced_for_analyst_review" if isinstance(candidate, TrialBalanceDiagnostic)
86 else "eligible_for_analyst_review"
87 )
88 eligibility.append(_require_bool(candidate, eligible_field))
89 findings = candidate.findings
90 if not isinstance(findings, tuple) or any(
91 not isinstance(item, str) or not item for item in findings
92 ):
93 raise CoverageError("DESKTOP_PREVIEW_FINDINGS_INVALID")
94 if isinstance(candidate, BalanceSheetCandidate):
95 flags = ("official_financial_statement", "accounting_policy_authorized",
96 "independently_reviewed", "export_authorized", "releasable")
97 elif isinstance(candidate, IncomeStatementCandidate):
98 flags = ("official_financial_statement", "accounting_policy_authorized",
99 "independently_reviewed", "export_authorized", "releasable")
100 elif isinstance(candidate, EquityChangesCandidate):
101 flags = ("official_financial_statement", "accounting_policy_authorized",
102 "source_ledger_completeness_verified", "independently_reviewed",
103 "export_authorized", "releasable")
104 elif isinstance(candidate, DirectCashFlowCandidate):
105 flags = ("cash_account_scope_owner_approved",
106 "transaction_ledger_completeness_verified",
107 "accounting_policy_authorized", "official_financial_statement",
108 "independently_reviewed", "export_authorized", "releasable")
109 else:
110 flags = ("source_ledger_completeness_verified",
111 "accounting_policies_verified", "official_financial_statement",
112 "independently_reviewed", "releasable")
113 _require_false(candidate, flags)
114 reconciliation = result.reconciliation
115 if (len(source_hashes) != 1
116 or reconciliation.source_sha256 not in source_hashes
117 or not isinstance(reconciliation.source_sha256, str)
118 or not re.fullmatch(r"[0-9a-f]{64}", reconciliation.source_sha256)
119 or reconciliation.same_source_snapshot is not True):
120 raise CoverageError("DESKTOP_PREVIEW_SOURCE_SNAPSHOT_MISMATCH")
121 if (not isinstance(reconciliation.cash_flow_transaction_ledger_sha256, str)
122 or not re.fullmatch(r"[0-9a-f]{64}", reconciliation.cash_flow_transaction_ledger_sha256)
123 or not isinstance(reconciliation.cash_note_selection_sha256, str)
124 or not re.fullmatch(r"[0-9a-f]{64}", reconciliation.cash_note_selection_sha256)):
125 raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_HASH_INVALID")
126 if (not isinstance(reconciliation.findings, tuple)
127 or any(not isinstance(item, str) or not item for item in reconciliation.findings)):
128 raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_FINDINGS_INVALID")
129 if companies != {reconciliation.company_code} or years != {reconciliation.fiscal_year}:
130 raise CoverageError("DESKTOP_PREVIEW_SCOPE_MISMATCH")
131 if (result.balance_sheet.note_references_sha256
132 != result.income_statement.note_references_sha256
133 or result.balance_sheet.note_references_sha256
134 != result.equity_changes.note_references_sha256):
135 raise CoverageError("DESKTOP_PREVIEW_REFERENCE_SNAPSHOT_MISMATCH")
136 ties = (
137 (result.balance_sheet.equity_total, reconciliation.balance_sheet_equity_total),
138 (result.balance_sheet.balance_difference, reconciliation.balance_sheet_difference),
139 (result.income_statement.net_result, reconciliation.income_statement_net_result),
140 (result.equity_changes.closing_equity_total, reconciliation.equity_statement_closing_total),
141 (result.equity_changes.net_profit_loss_movements, reconciliation.equity_net_profit_loss_movements),
142 (result.cash_flow.closing_cash_and_equivalents, reconciliation.cash_flow_closing_cash),
143 (result.trial_balance.opening_debit_net_total, reconciliation.trial_balance_opening_debit_net_total),
144 (result.trial_balance.movement_difference, reconciliation.trial_balance_movement_difference),
145 (result.trial_balance.closing_debit_net_total, reconciliation.trial_balance_closing_debit_net_total),
146 (result.cash_flow.transaction_ledger_sha256, reconciliation.cash_flow_transaction_ledger_sha256),
147 )
148 if any(left != right for left, right in ties):
149 raise CoverageError("DESKTOP_PREVIEW_RECONCILIATION_MISMATCH")
150 overall = _require_bool(result, "eligible_for_analyst_review")
151 if overall:
152 if (not _require_bool(reconciliation, "eligible_for_analyst_review")
153 or not all(eligibility) or result.findings
154 or any(candidate.findings for candidate, _, _ in candidates)):
155 raise CoverageError("DESKTOP_PREVIEW_FALSE_ELIGIBILITY")
156 else:
157 if _require_bool(reconciliation, "eligible_for_analyst_review"):
158 raise CoverageError("DESKTOP_PREVIEW_ELIGIBILITY_MISMATCH")
159 if not reconciliation.findings and all(eligibility) and not any(
160 candidate.findings for candidate, _, _ in candidates
161 ):
162 raise CoverageError("DESKTOP_PREVIEW_BLOCKED_WITHOUT_FINDINGS")