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

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

Synced Dec 3, 20:45 UTC

codex/aram-windows-statement-figures-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 186 lines 7.47 KB
Raw
1 """Single-snapshot orchestration for ARAM's five unsigned statement engines.
2
3 The pipeline is read-only. It invokes each existing candidate builder with the
4 same immutable workbook bytes and the same frozen note-reference collection,
5 then runs the cross-statement arithmetic gate. It never authorizes, posts,
6 exports or releases financial statements.
7 """
8 from __future__ import annotations
9
10 from dataclasses import dataclass
11 from typing import Any, Sequence
12
13 from aram_backend.balance_sheet_candidate import (
14 BalanceSheetCandidate, BalanceSheetLinePolicy, build_balance_sheet_candidate,
15 )
16 from aram_backend.cash_flow_direct_candidate import (
17 CashAccountPolicy, CashCounterpartPolicy, CashFlowEntry,
18 DirectCashFlowCandidate, build_direct_cash_flow_candidate,
19 )
20 from aram_backend.coverage_preview import CoverageError
21 from aram_backend.cross_statement_reconciliation import (
22 CashBalanceNoteSelection, CrossStatementReconciliation,
23 reconcile_cross_statements,
24 )
25 from aram_backend.equity_changes_candidate import (
26 EquityChangesCandidate, EquityMovementPolicy, build_equity_changes_candidate,
27 )
28 from aram_backend.income_statement_candidate import (
29 IncomeStatementCandidate, IncomeStatementLinePolicy,
30 build_income_statement_candidate,
31 )
32 from aram_backend.reconciliation_review import NoteReference
33 from aram_backend.trial_balance_diagnostic import (
34 TrialBalanceDiagnostic, analyze_trial_balance,
35 )
36
37
38 def _freeze_sequence(value: object, label: str) -> tuple[Any, ...]:
39 if not isinstance(value, (tuple, list)):
40 raise CoverageError("INVALID_FIVE_STATEMENT_" + label)
41 return tuple(value)
42
43
44 @dataclass(frozen=True)
45 class FiveStatementPipelineResult:
46 balance_sheet: BalanceSheetCandidate
47 income_statement: IncomeStatementCandidate
48 equity_changes: EquityChangesCandidate
49 cash_flow: DirectCashFlowCandidate
50 trial_balance: TrialBalanceDiagnostic
51 reconciliation: CrossStatementReconciliation
52 kind: str = "UNAPPROVED_FIVE_STATEMENT_PIPELINE_CANDIDATE"
53 source_ledger_completeness_verified: bool = False
54 accounting_policy_authorized: bool = False
55 independently_reviewed: bool = False
56 trusted_approvals: bool = False
57 official_financial_statement: bool = False
58 export_authorized: bool = False
59 releasable: bool = False
60
61 @property
62 def company_code(self) -> str:
63 return self.reconciliation.company_code
64
65 @property
66 def fiscal_year(self) -> int:
67 return self.reconciliation.fiscal_year
68
69 @property
70 def source_sha256(self) -> str:
71 return self.reconciliation.source_sha256
72
73 @property
74 def eligible_for_analyst_review(self) -> bool:
75 return self.reconciliation.eligible_for_analyst_review
76
77 @property
78 def findings(self) -> tuple[str, ...]:
79 return self.reconciliation.findings
80
81 def to_dict(self) -> dict[str, Any]:
82 return {
83 "kind": self.kind,
84 "company_code": self.company_code,
85 "fiscal_year": self.fiscal_year,
86 "source_sha256": self.source_sha256,
87 "eligible_for_analyst_review": self.eligible_for_analyst_review,
88 "findings": list(self.findings),
89 "balance_sheet": self.balance_sheet.to_dict(),
90 "income_statement": self.income_statement.to_dict(),
91 "equity_changes": self.equity_changes.to_dict(),
92 "cash_flow": self.cash_flow.to_dict(),
93 "trial_balance": self.trial_balance.to_dict(),
94 "reconciliation": self.reconciliation.to_dict(),
95 "source_ledger_completeness_verified": False,
96 "accounting_policy_authorized": False,
97 "independently_reviewed": False,
98 "trusted_approvals": False,
99 "official_financial_statement": False,
100 "export_authorized": False,
101 "releasable": False,
102 }
103
104
105 def build_five_statement_pipeline(
106 workbook_bytes: bytes, *,
107 company_code: str,
108 fiscal_year: int,
109 references: Sequence[NoteReference],
110 balance_sheet_policies: Sequence[BalanceSheetLinePolicy],
111 income_statement_policies: Sequence[IncomeStatementLinePolicy],
112 equity_movement_policies: Sequence[EquityMovementPolicy],
113 currency: str,
114 unit: str,
115 cash_accounts: Sequence[CashAccountPolicy],
116 counterpart_policies: Sequence[CashCounterpartPolicy],
117 transactions: Sequence[CashFlowEntry],
118 cash_balance_notes: Sequence[CashBalanceNoteSelection],
119 ) -> FiveStatementPipelineResult:
120 """Run all five candidate builders on one source snapshot and reconcile.
121
122 Input sequences are frozen once at the orchestration boundary. In
123 particular, the exact same immutable reference tuple is supplied to BS,
124 IS and EQ, preventing accidental reference-snapshot drift between engines.
125 A blocked upstream candidate is returned as a blocked pipeline result;
126 invalid source/policy preconditions raise CoverageError.
127 """
128 if type(workbook_bytes) is not bytes or not workbook_bytes:
129 raise CoverageError("INVALID_FIVE_STATEMENT_WORKBOOK_BYTES")
130 if (not isinstance(company_code, str) or not company_code.strip()
131 or company_code != company_code.strip()):
132 raise CoverageError("INVALID_FIVE_STATEMENT_COMPANY_SCOPE")
133 if type(fiscal_year) is not int or fiscal_year <= 0:
134 raise CoverageError("INVALID_FIVE_STATEMENT_FISCAL_YEAR")
135
136 shared_references = _freeze_sequence(references, "NOTE_REFERENCES")
137 bs_policies = _freeze_sequence(balance_sheet_policies, "BS_POLICIES")
138 is_policies = _freeze_sequence(income_statement_policies, "IS_POLICIES")
139 eq_policies = _freeze_sequence(equity_movement_policies, "EQ_POLICIES")
140 selected_cash_accounts = _freeze_sequence(cash_accounts, "CASH_ACCOUNTS")
141 selected_counterpart_policies = _freeze_sequence(
142 counterpart_policies, "COUNTERPART_POLICIES",
143 )
144 transaction_rows = _freeze_sequence(transactions, "CASH_TRANSACTIONS")
145 cash_note_selections = _freeze_sequence(
146 cash_balance_notes, "CASH_NOTE_SELECTIONS",
147 )
148
149 balance_sheet = build_balance_sheet_candidate(
150 workbook_bytes, company_code=company_code, fiscal_year=fiscal_year,
151 references=shared_references, policies=bs_policies,
152 )
153 income_statement = build_income_statement_candidate(
154 workbook_bytes, company_code=company_code, fiscal_year=fiscal_year,
155 references=shared_references, policies=is_policies,
156 )
157 equity_changes = build_equity_changes_candidate(
158 workbook_bytes, company_code=company_code, fiscal_year=fiscal_year,
159 references=shared_references, policies=eq_policies,
160 )
161 cash_flow = build_direct_cash_flow_candidate(
162 workbook_bytes, company_code=company_code, fiscal_year=fiscal_year,
163 currency=currency, unit=unit,
164 cash_accounts=selected_cash_accounts,
165 counterpart_policies=selected_counterpart_policies,
166 transactions=transaction_rows,
167 )
168 trial_balance = analyze_trial_balance(
169 workbook_bytes, company_code=company_code, fiscal_year=fiscal_year,
170 )
171 reconciliation = reconcile_cross_statements(
172 balance_sheet=balance_sheet,
173 income_statement=income_statement,
174 equity_changes=equity_changes,
175 cash_flow=cash_flow,
176 trial_balance=trial_balance,
177 cash_balance_notes=cash_note_selections,
178 )
179 return FiveStatementPipelineResult(
180 balance_sheet=balance_sheet,
181 income_statement=income_statement,
182 equity_changes=equity_changes,
183 cash_flow=cash_flow,
184 trial_balance=trial_balance,
185 reconciliation=reconciliation,
186 )