feat(pipeline): orchestrate five statement candidates from one immutable workbook snapshot
davoodmehraban89 committed
Oct 31, 2026 at 11:38 UTC
eaff495dacbc4106f3e0d1d1a7dc63958977fb92
1 file changed
+186
backend/src/aram_backend/five_statement_pipeline.py
new
+186
@@ -0,0 +1,186 @@
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
+ )