feat(desktop): read-only five-statement controller with invalidated selections and stable file guards
davoodmehraban89 committed
Oct 31, 2026 at 18:29 UTC
d634ef23a5ef1ff0b59e7096b5ca8580e1f764b8
1 file changed
+99
backend/src/aram_backend/desktop_five_statement_core.py
new
+99
@@ -0,0 +1,99 @@
1
+"""GUI-independent read-only controller for ARAM's five-statement Windows pilot.
2
+
3
+All financial calculations are delegated to the audited pipeline CLI function.
4
+Selection never reads or writes the selected files. Execution revalidates both
5
+files using the CLI's bounded, no-symlink, stable-read guards.
6
+"""
7
+from __future__ import annotations
8
+
9
+from pathlib import Path
10
+import stat
11
+
12
+from aram_backend.coverage_preview import CoverageError
13
+from aram_backend.coverage_preview_cli import MAX_INPUT_BYTES
14
+from aram_backend.cash_flow_direct_candidate_cli import MAX_CASH_MANIFEST_BYTES
15
+from aram_backend.five_statement_pipeline import FiveStatementPipelineResult
16
+from aram_backend.five_statement_pipeline_cli import run_pipeline_cli
17
+
18
+
19
+class DesktopFiveInputError(ValueError):
20
+ """Local selection or source validation was refused."""
21
+
22
+
23
+def _select_regular_file(path_value: str, *, suffix: str, limit: int) -> Path:
24
+ if not isinstance(path_value, str) or not path_value:
25
+ raise DesktopFiveInputError("FILE_REQUIRED")
26
+ path = Path(path_value)
27
+ if path.suffix.lower() != suffix:
28
+ raise DesktopFiveInputError("FILE_EXTENSION_INVALID")
29
+ try:
30
+ info = path.lstat()
31
+ except OSError as exc:
32
+ raise DesktopFiveInputError("FILE_NOT_READABLE") from exc
33
+ if not stat.S_ISREG(info.st_mode):
34
+ raise DesktopFiveInputError("FILE_NOT_REGULAR")
35
+ if not 0 < info.st_size <= limit:
36
+ raise DesktopFiveInputError("FILE_SIZE_INVALID")
37
+ return path
38
+
39
+
40
+class DesktopFiveStatementController:
41
+ """No stale approval, financial posting, database or export state."""
42
+
43
+ def __init__(self) -> None:
44
+ self.workbook_path: Path | None = None
45
+ self.manifest_path: Path | None = None
46
+ self.result: FiveStatementPipelineResult | None = None
47
+ self.error_code: str | None = None
48
+ self.state = "EMPTY"
49
+ self.generation = 0
50
+
51
+ @property
52
+ def can_run(self) -> bool:
53
+ return self.workbook_path is not None and self.manifest_path is not None
54
+
55
+ def _invalidate(self) -> None:
56
+ self.generation += 1
57
+ self.result = None
58
+ self.error_code = None
59
+ self.state = "SELECTED" if self.can_run else "EMPTY"
60
+
61
+ def select_workbook(self, path_value: str) -> None:
62
+ self.workbook_path = None
63
+ self._invalidate()
64
+ self.workbook_path = _select_regular_file(
65
+ path_value, suffix=".xlsx", limit=MAX_INPUT_BYTES,
66
+ )
67
+ self._invalidate()
68
+
69
+ def select_manifest(self, path_value: str) -> None:
70
+ self.manifest_path = None
71
+ self._invalidate()
72
+ self.manifest_path = _select_regular_file(
73
+ path_value, suffix=".json", limit=MAX_CASH_MANIFEST_BYTES,
74
+ )
75
+ self._invalidate()
76
+
77
+ def clear(self) -> None:
78
+ self.workbook_path = None
79
+ self.manifest_path = None
80
+ self._invalidate()
81
+
82
+ def validate(self) -> FiveStatementPipelineResult:
83
+ self.result = None
84
+ self.error_code = None
85
+ self.state = "RUNNING"
86
+ if not self.can_run:
87
+ self.state = "EMPTY"
88
+ raise DesktopFiveInputError("TWO_INPUT_FILES_REQUIRED")
89
+ try:
90
+ outcome = run_pipeline_cli(
91
+ str(self.workbook_path), str(self.manifest_path),
92
+ )
93
+ except CoverageError as exc:
94
+ self.error_code = exc.code
95
+ self.state = "INPUT_ERROR"
96
+ raise DesktopFiveInputError(exc.code) from exc
97
+ self.result = outcome
98
+ self.state = "ELIGIBLE" if outcome.eligible_for_analyst_review else "BLOCKED"
99
+ return outcome