@davoodmehraban89 / Aram / commits / d4f13f1
Fork 0

feat(desktop): bounded read-only XLSX preflight controller, no persistence

davoodmehraban89 committed Oct 30, 2026 at 23:37 UTC d4f13f18dc912f33eb771daf1e8d1b4f6eb67d0b
1 file changed +68
backend/src/aram_backend/desktop_pilot_core.py new
+68
@@ -0,0 +1,68 @@
1 +"""Read-only desktop preflight controller; no database or financial postings.
2 +
3 +Native Tk window is a presentation adapter. No source is ever mutated,
4 +uploaded, auto-imported, or used to generate statements.
5 +"""
6 +from __future__ import annotations
7 +
8 +from pathlib import Path
9 +
10 +from aram_backend.pilot_workbook import PilotPreflightResult, preflight_pilot_workbook
11 +
12 +MAX_WORKBOOK_BYTES = 20 * 1024 * 1024
13 +
14 +
15 +class PilotInputError(ValueError):
16 + """Refuse unsafe, absent or unsupported local input without side effects."""
17 +
18 +
19 +class DesktopPilotController:
20 + """Small, GUI-independent application state for native desktop tests."""
21 +
22 + def __init__(self) -> None:
23 + self.selected_path: Path | None = None
24 + self.result: PilotPreflightResult | None = None
25 + self.state = "EMPTY"
26 +
27 + def clear(self) -> None:
28 + self.selected_path = None
29 + self.result = None
30 + self.state = "EMPTY"
31 +
32 + def choose_file(self, file_path: str | Path) -> None:
33 + # An attempted invalid switch must not leave the earlier workbook
34 + # looking like the currently selected and successfully checked one.
35 + self.clear()
36 + try:
37 + path = Path(file_path)
38 + if path.suffix.lower() != ".xlsx" or not path.is_file():
39 + raise PilotInputError("Only an existing local .xlsx file is supported")
40 + if path.is_symlink():
41 + raise PilotInputError("Symbolic links are not accepted")
42 + size = path.stat().st_size
43 + if size <= 0 or size > MAX_WORKBOOK_BYTES:
44 + raise PilotInputError("Workbook must be nonempty and at most 20 MiB")
45 + except (OSError, ValueError) as exc:
46 + raise PilotInputError("Cannot select the workbook") from exc
47 + self.selected_path = path
48 + self.state = "SELECTED"
49 +
50 + def validate(self) -> PilotPreflightResult:
51 + """Read bounded bytes and run the existing governed in-memory contract."""
52 + if self.selected_path is None:
53 + raise PilotInputError("Select an .xlsx Pilot workbook first")
54 + self.result = None
55 + self.state = "SELECTED"
56 + try:
57 + # Bound the *actual* bytes read, not just an earlier stat, to
58 + # reject a changing file and avoid unbounded memory consumption.
59 + with self.selected_path.open("rb") as handle:
60 + content = handle.read(MAX_WORKBOOK_BYTES + 1)
61 + except OSError as exc:
62 + raise PilotInputError("Selected workbook cannot be read") from exc
63 + if not content or len(content) > MAX_WORKBOOK_BYTES:
64 + raise PilotInputError("Workbook is empty or exceeds the 20 MiB limit")
65 + checked = preflight_pilot_workbook(content)
66 + self.result = checked
67 + self.state = "VALID" if checked.valid else "INVALID"
68 + return checked