Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 23:15 UTC
codex/gate3-authenticated-principal-boundary-20261030
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
| 1 | """Database-local SYSTEM staging evidence only; NOT authenticated finance audit. |
| 2 | |
| 3 | SQLite-only triggers deny updates and deletes to event rows. A privileged DB |
| 4 | operator can still drop triggers, insert fake rows or replace the DB file: |
| 5 | this is NOT a signed/WORM/externally anchored immutable audit ledger. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from hashlib import sha256 |
| 10 | import json |
| 11 | |
| 12 | from sqlalchemy import ( |
| 13 | CheckConstraint, Column, DateTime, ForeignKey, Integer, String, |
| 14 | Table, UniqueConstraint, func, insert, select, |
| 15 | ) |
| 16 | from sqlalchemy.orm import Session |
| 17 | |
| 18 | from aram_backend.mapping_draft_store import ( |
| 19 | _proposal_metadata, mapping_review_drafts, |
| 20 | ) |
| 21 | from aram_backend.mapping_service import MappingValidationError |
| 22 | |
| 23 | |
| 24 | mapping_draft_events = Table( |
| 25 | "mapping_draft_events", _proposal_metadata, |
| 26 | Column("id", Integer, primary_key=True), |
| 27 | Column( |
| 28 | "draft_id", Integer, |
| 29 | ForeignKey(mapping_review_drafts.c.id, ondelete="RESTRICT"), nullable=False, |
| 30 | ), |
| 31 | Column("year_id", Integer, nullable=False), |
| 32 | Column("event_type", String(32), nullable=False, server_default="DraftStaged"), |
| 33 | Column("payload_hash", String(64), nullable=False), |
| 34 | Column("event_hash", String(64), nullable=False), |
| 35 | Column( |
| 36 | "created_at", DateTime(timezone=True), |
| 37 | server_default=func.current_timestamp(), nullable=False, |
| 38 | ), |
| 39 | UniqueConstraint("draft_id", name="uq_mapping_draft_events_draft"), |
| 40 | CheckConstraint( |
| 41 | "event_type = 'DraftStaged'", name="ck_mapping_draft_events_staging_only", |
| 42 | ), |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | SQLITE_AUDIT_GUARD_DDL = ( |
| 47 | """CREATE TRIGGER mapping_draft_events_no_update |
| 48 | BEFORE UPDATE ON mapping_draft_events |
| 49 | BEGIN SELECT RAISE(ABORT, 'Mapping staging event is insert-only'); END""", |
| 50 | """CREATE TRIGGER mapping_draft_events_no_delete |
| 51 | BEFORE DELETE ON mapping_draft_events |
| 52 | BEGIN SELECT RAISE(ABORT, 'Mapping staging event is insert-only'); END""", |
| 53 | ) |
| 54 | |
| 55 | |
| 56 | def _event_digest(*, draft_id: int, year_id: int, request_key: str, payload_hash: str) -> str: |
| 57 | values = dict( |
| 58 | draft_id=draft_id, year_id=year_id, request_key=request_key, |
| 59 | payload_hash=payload_hash, event_type="DraftStaged", |
| 60 | ) |
| 61 | encoded = json.dumps( |
| 62 | values, sort_keys=True, ensure_ascii=True, separators=(",", ":"), |
| 63 | ).encode("utf-8") |
| 64 | return sha256(encoded).hexdigest() |
| 65 | |
| 66 | |
| 67 | def record_draft_staging_event( |
| 68 | session: Session, *, draft_id: int, year_id: int, request_key: str, |
| 69 | payload_hash: str, |
| 70 | ) -> None: |
| 71 | """Insert a single SYSTEM event in the *caller's* staging transaction. |
| 72 | |
| 73 | Do not call this for a replay. The DB unique(draft_id) is the final guard |
| 74 | against a double event, and the outer transaction rolls back both records. |
| 75 | """ |
| 76 | if (type(draft_id) is not int or draft_id <= 0 or |
| 77 | type(year_id) is not int or year_id <= 0): |
| 78 | raise MappingValidationError("invalid staging event draft/year identifiers") |
| 79 | session.execute( |
| 80 | insert(mapping_draft_events).values( |
| 81 | draft_id=draft_id, year_id=year_id, event_type="DraftStaged", |
| 82 | payload_hash=payload_hash, |
| 83 | event_hash=_event_digest( |
| 84 | draft_id=draft_id, year_id=year_id, |
| 85 | request_key=request_key, payload_hash=payload_hash, |
| 86 | ), |
| 87 | ) |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | def verify_draft_staging_event( |
| 92 | session: Session, *, draft_id: int, year_id: int, |
| 93 | ) -> None: |
| 94 | """Read-only crosscheck of draft and event; does not authorize approval.""" |
| 95 | if type(draft_id) is not int or draft_id <= 0 or type(year_id) is not int or year_id <= 0: |
| 96 | raise MappingValidationError("invalid staging event identifiers") |
| 97 | with session.no_autoflush: |
| 98 | draft = session.execute( |
| 99 | select(mapping_review_drafts).where( |
| 100 | mapping_review_drafts.c.id == draft_id, |
| 101 | mapping_review_drafts.c.year_id == year_id, |
| 102 | ) |
| 103 | ).mappings().first() |
| 104 | if draft is None: |
| 105 | raise MappingValidationError("unreviewed Mapping draft not found for fiscal year") |
| 106 | event = session.execute( |
| 107 | select(mapping_draft_events).where( |
| 108 | mapping_draft_events.c.draft_id == draft_id, |
| 109 | mapping_draft_events.c.year_id == year_id, |
| 110 | ) |
| 111 | ).mappings().first() |
| 112 | if ( |
| 113 | event is None |
| 114 | or draft["state"] != "Unreviewed" |
| 115 | or event["event_type"] != "DraftStaged" |
| 116 | or event["payload_hash"] != draft["payload_hash"] |
| 117 | or event["event_hash"] != _event_digest( |
| 118 | draft_id=draft_id, year_id=year_id, |
| 119 | request_key=draft["request_key"], payload_hash=draft["payload_hash"], |
| 120 | ) |
| 121 | ): |
| 122 | raise MappingValidationError("Mapping draft staging audit is missing or corrupted") |