Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 14:15 UTC
dependabot/github_actions/actions/checkout-7
add-index-html-10623938925177053894
arena/094f314c-aram
arena/2d4eda07-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
dependabot/github_actions/actions/checkout-7
dependabot/github_actions/actions/setup-node-6
dependabot/github_actions/actions/setup-python-7
dependabot/github_actions/actions/upload-artifact-7
dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1
dependabot/npm_and_yarn/frontend/globals-17.15.0
dependabot/npm_and_yarn/frontend/vite-8.4.0
dependabot/npm_and_yarn/frontend/vitest-5.1.2
dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2
dependabot/pip/backend/mypy-gte-1.13-and-lt-3
dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8
dependabot/pip/backend/setuptools-gte-75-and-lt-85
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
| 1 | """Offline Windows desktop entrypoint (Phase 8, booklet §15). |
| 2 | |
| 3 | Runs the FastAPI backend and serves the bundled React workbench on 127.0.0.1 |
| 4 | (loopback only; zero runtime network). On first launch it initializes a local |
| 5 | SQLite database (WAL, foreign keys ON) and the content-addressed source store in |
| 6 | a per-user data directory, applying the schema and D-07 immutability triggers. |
| 7 | |
| 8 | The packaged executable (PyInstaller, see packaging/aram.spec) bundles this |
| 9 | module, the migrations, and the built frontend (frontend/dist). No external |
| 10 | service, browser plugin, or network access is required at runtime. |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | from pathlib import Path |
| 18 | |
| 19 | from fastapi import FastAPI |
| 20 | from fastapi.staticfiles import StaticFiles |
| 21 | |
| 22 | from aram_backend import __version__ |
| 23 | from aram_backend.admin_api import launch_router |
| 24 | from aram_backend.admin_api import public_router as auth_router |
| 25 | from aram_backend.admin_api import router as admin_router |
| 26 | from aram_backend.auth import LaunchGuard |
| 27 | from aram_backend.config import get_settings |
| 28 | from aram_backend.database import Base, build_engine, ensure_triggers |
| 29 | from aram_backend.models import FiscalYearStatus, StatementType # noqa: F401 (register mappers) |
| 30 | from aram_backend.schemas import HealthResponse |
| 31 | from aram_backend.statement_api import router as statement_router |
| 32 | |
| 33 | |
| 34 | def default_data_dir() -> Path: |
| 35 | """Per-user data directory (override with ARAM_DATA_DIR).""" |
| 36 | override = os.environ.get("ARAM_DATA_DIR") |
| 37 | if override: |
| 38 | return Path(override) |
| 39 | if sys.platform == "win32": |
| 40 | base = Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming")) |
| 41 | return base / "Aram" |
| 42 | return Path.home() / ".aram" |
| 43 | |
| 44 | |
| 45 | def _bundled_frontend() -> Path | None: |
| 46 | """Locate the built React workbench (PyInstaller _MEIPASS or repo dist).""" |
| 47 | candidates = [] |
| 48 | meipass = getattr(sys, "_MEIPASS", None) |
| 49 | if meipass: |
| 50 | candidates.append(Path(meipass) / "frontend_dist") |
| 51 | candidates.append(Path(__file__).resolve().parents[2] / "frontend_dist") |
| 52 | candidates.append(Path(__file__).resolve().parents[3] / "frontend" / "dist") |
| 53 | for candidate in candidates: |
| 54 | if (candidate / "index.html").exists(): |
| 55 | return candidate |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | def initialize_database(data_dir: Path) -> None: |
| 60 | """Create the schema + triggers idempotently for a fresh local install.""" |
| 61 | data_dir.mkdir(parents=True, exist_ok=True) |
| 62 | (data_dir / "source_store").mkdir(exist_ok=True) |
| 63 | db_path = data_dir / "aram.db" |
| 64 | engine = build_engine(f"sqlite:///{db_path}") |
| 65 | Base.metadata.create_all(engine) |
| 66 | ensure_triggers(engine) |
| 67 | engine.dispose() |
| 68 | |
| 69 | |
| 70 | def create_desktop_app() -> FastAPI: |
| 71 | """Desktop app guarded by a one-time local launch token (R1, booklet 2.1). |
| 72 | |
| 73 | The launcher arms a random 256-bit token and hands it to the browser it opens |
| 74 | in the URL *fragment* (never sent over HTTP, never logged). The page exchanges |
| 75 | it once at /auth/launch for a grant kept in sessionStorage; every later call |
| 76 | carries that grant in X-Aram-Launch. Another process on the same machine has |
| 77 | neither the token nor the grant, so it cannot drive the API — not even the |
| 78 | first-admin bootstrap. |
| 79 | """ |
| 80 | app = FastAPI( |
| 81 | title="Aram Desktop", version=__version__, docs_url=None, redoc_url=None, openapi_url=None |
| 82 | ) |
| 83 | guard = LaunchGuard() |
| 84 | app.state.launch_guard = guard |
| 85 | app.state.launch_token = guard.arm() |
| 86 | app.include_router(launch_router) |
| 87 | app.include_router(auth_router) |
| 88 | app.include_router(statement_router) |
| 89 | app.include_router(admin_router) |
| 90 | |
| 91 | @app.get("/api/v1/health", response_model=HealthResponse, tags=["system"]) |
| 92 | def health() -> HealthResponse: |
| 93 | return HealthResponse(status="ok", service="Aram Desktop", version=__version__) |
| 94 | |
| 95 | frontend = _bundled_frontend() |
| 96 | if frontend is not None: |
| 97 | app.mount("/", StaticFiles(directory=str(frontend), html=True), name="workbench") |
| 98 | return app |
| 99 | |
| 100 | |
| 101 | def _open_browser_when_ready(port: int, launch_token: str, timeout: float = 30.0) -> None: |
| 102 | """Open the workbench once the loopback port answers, token in the fragment.""" |
| 103 | import socket |
| 104 | import threading |
| 105 | import time |
| 106 | import webbrowser |
| 107 | |
| 108 | def _wait_and_open() -> None: |
| 109 | deadline = time.monotonic() + timeout |
| 110 | url = f"http://127.0.0.1:{port}/#launch={launch_token}" |
| 111 | while time.monotonic() < deadline: |
| 112 | try: |
| 113 | with socket.create_connection(("127.0.0.1", port), timeout=0.5): |
| 114 | webbrowser.open(url) |
| 115 | return |
| 116 | except OSError: |
| 117 | time.sleep(0.2) |
| 118 | |
| 119 | threading.Thread(target=_wait_and_open, daemon=True).start() |
| 120 | |
| 121 | |
| 122 | def main() -> None: |
| 123 | import uvicorn |
| 124 | |
| 125 | from aram_backend.database import reconfigure |
| 126 | |
| 127 | data_dir = default_data_dir() |
| 128 | initialize_database(data_dir) |
| 129 | # Point the shared settings + engine at the local data dir before serving. |
| 130 | db_url = f"sqlite:///{data_dir / 'aram.db'}" |
| 131 | os.environ["ARAM_DATABASE_URL"] = db_url |
| 132 | os.environ["ARAM_SOURCE_STORE_ROOT"] = str(data_dir / "source_store") |
| 133 | get_settings.cache_clear() |
| 134 | reconfigure(db_url) |
| 135 | |
| 136 | app = create_desktop_app() |
| 137 | port = int(os.environ.get("ARAM_PORT", "8765")) |
| 138 | _open_browser_when_ready(port, app.state.launch_token) |
| 139 | # Loopback only: the desktop app never exposes a network interface. |
| 140 | uvicorn.run(app, host="127.0.0.1", port=port, log_level="info") |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | main() |