@davoodmehraban89 / Aram / commits / ff6843c
Fork 0

fix(gate3): preserve reviewed evidence and isolate exports; document three-pass audit

Davood Mehraban committed Nov 1, 2026 at 21:37 UTC ff6843cd0a1544a3a5355a8d555d4d692b8716fc
4 files changed +92 -8
backend/src/aram_backend/mapping_workflow.py
+27 -8
@@ -33,6 +33,7 @@ class MappingDecision:
33 status: str
34 reviewer: str | None
35 source: str
36 + review_date: str | None = None
37
38
39 @dataclass(frozen=True)
@@ -86,7 +87,8 @@ class MappingWorkspace:
87 self._check_scope(decision)
88 if not actor.strip() or not decision.mapping_id.strip() or not decision.account_full_code.strip():
89 raise MappingWorkflowError("actor, mapping_id and account are required")
89 - if decision.status != "Draft" or decision.version != 1 or decision.reviewer is not None:
90 + if (decision.status != "Draft" or decision.version != 1
91 + or decision.reviewer is not None or decision.review_date is not None):
92 raise MappingWorkflowError("new decisions must begin as unreviewed Draft v1")
93 if decision.mapping_id in self._decisions:
94 raise MappingWorkflowError("mapping_id already exists; use revise")
@@ -104,9 +106,16 @@ class MappingWorkspace:
106 raise MappingWorkflowError("invalid mapping lifecycle transition")
107 if not actor.strip():
108 raise MappingWorkflowError("reviewer identity required")
109 + if status == "Reviewed" and any(
110 + event.mapping_id == mapping_id and event.new_status == "Draft"
111 + and event.actor == actor for event in self._audit
112 + ):
113 + raise MappingWorkflowError("maker cannot review own mapping")
114 if status == "Approved" and actor == old.reviewer:
115 raise MappingWorkflowError("approval requires independent reviewer")
109 - new = replace(old, status=status, reviewer=actor, version=old.version + 1)
116 + new = replace(old, status=status, reviewer=actor,
117 + review_date=datetime.now(timezone.utc).date().isoformat(),
118 + version=old.version + 1)
119 self._record(old, new, actor)
120 return new
121
@@ -120,6 +129,7 @@ class MappingWorkspace:
129 new = replace(old, target_statement=target_statement,
130 statement_section=statement_section, note_number=note_number,
131 subnote_code=subnote_code, status="Draft", reviewer=None,
132 + review_date=None,
133 version=old.version + 1)
134 self._record(old, new, actor)
135 return new
@@ -138,7 +148,8 @@ class MappingWorkspace:
148 raise MappingWorkflowError("only approved mappings may be carried forward")
149 for d in candidates:
150 self.propose(replace(d, fiscal_year=self.fiscal_year, status="Draft",
141 - reviewer=None, version=1, source="CarryForward"), actor=actor)
151 + reviewer=None, review_date=None, version=1,
152 + source="CarryForward"), actor=actor)
153 return len(candidates)
154
155
@@ -161,7 +172,8 @@ def import_mapping_workbook(content: bytes, company_code: str, fiscal_year: int)
172 row["mapping_id"], row["company_code"], row["fiscal_year"],
173 row["account_full_code"], row["target_statement"],
174 row["statement_section"], row["note_number"], row["subnote_code"],
164 - row["mapping_version"], row["approval_status"], row["reviewer"], row["source"],
175 + row["mapping_version"], row["approval_status"], row["reviewer"],
176 + row["source"], row["review_date"],
177 ))
178 return tuple(decisions)
179 finally:
@@ -251,10 +263,13 @@ def export_pilot_mapping_workbook(
263 notes.add((row["company_code"], row["fiscal_year"], row["statement"],
264 row["statement_section"], row["note_number"], row["subnote_code"]))
265 previous = {}
266 + foreign_rows = []
267 for values in mapping_sheet.iter_rows(min_row=2, values_only=True):
268 if any(v is not None for v in values):
269 row = dict(zip(headers, values))
270 previous[row["mapping_id"]] = row
271 + if (row["company_code"], row["fiscal_year"]) != (company_code, fiscal_year):
272 + foreign_rows.append(tuple(row[field] for field in headers))
273 replacements = []
274 seen = set()
275 for d in decisions:
@@ -269,8 +284,10 @@ def export_pilot_mapping_workbook(
284 if (company_code, fiscal_year, d.target_statement, d.statement_section,
285 d.note_number, d.subnote_code) not in notes:
286 raise MappingWorkflowError("unknown note/subnote in export")
272 - if d.status != "Draft":
273 - raise MappingWorkflowError("approved/reviewed mappings require dated review evidence")
287 + if d.status not in {"Draft", "Reviewed", "Approved"}:
288 + raise MappingWorkflowError("invalid mapping status")
289 + if d.status != "Draft" and (not d.reviewer or not d.review_date):
290 + raise MappingWorkflowError("review evidence required")
291 row = {field: None for field in headers}
292 row.update({
293 "mapping_id": d.mapping_id, "company_code": company_code,
@@ -281,7 +298,9 @@ def export_pilot_mapping_workbook(
298 "statement_section": d.statement_section,
299 "note_number": d.note_number, "subnote_code": d.subnote_code,
300 "mapping_version": d.version, "approval_status": d.status,
284 - "source": d.source, "validation_status": "Pending",
301 + "source": d.source, "reviewer": d.reviewer,
302 + "review_date": d.review_date,
303 + "validation_status": "Valid" if d.status == "Approved" else "Pending",
304 })
305 for field in ("general_code", "subsidiary_code", "detail1_code",
306 "detail2_code", "detail3_code", "detail4_code"):
@@ -289,7 +308,7 @@ def export_pilot_mapping_workbook(
308 replacements.append(tuple(row[field] for field in headers))
309 if mapping_sheet.max_row > 1:
310 mapping_sheet.delete_rows(2, mapping_sheet.max_row - 1)
292 - for values in replacements:
311 + for values in foreign_rows + replacements:
312 mapping_sheet.append(values)
313 stream = BytesIO()
314 workbook.save(stream)
backend/tests/test_mapping_store.py
+9
@@ -100,3 +100,12 @@ def test_export_rejects_foreign_and_unverifiable_approval():
100 with pytest.raises(MappingWorkflowError, match="review"):
101 export_pilot_mapping_workbook(original, [replace(decision(), status="Approved")],
102 company_code="001", fiscal_year=1405)
103 +
104 +
105 +def test_approved_workbook_roundtrip_with_review_evidence():
106 + original = workbook_bytes(valid_rows())
107 + approved = replace(decision(), version=3, status="Approved",
108 + reviewer="approver", review_date="2026-11-01")
109 + exported = export_pilot_mapping_workbook(original, [approved],
110 + company_code="001", fiscal_year=1405)
111 + assert import_mapping_workbook(exported, "001", 1405) == (approved,)
backend/tests/test_mapping_workflow.py
+8
@@ -121,3 +121,11 @@ def test_unknown_queue_rejects_foreign_scope():
121 with pytest.raises(MappingWorkflowError, match="foreign mapping"):
122 unknown_account_queue([], [sample(company="002")],
123 company_code="001", fiscal_year=1405)
124 +
125 +
126 +def test_maker_cannot_review_own_mapping():
127 + space = MappingWorkspace("001", 1405)
128 + space.propose(sample(), actor="maker")
129 + with pytest.raises(MappingWorkflowError, match="maker"):
130 + space.transition("M001", "Reviewed", actor="maker")
131 + assert len(space.audit) == 1
docs/GATE3_REVIEW_82.md new
+48
@@ -0,0 +1,48 @@
1 +# Gate 3 PR #82 — Three-perspective self-audit (not independent sign-off)
2 +
3 +Reviewed initial head: d75cbbbc16068acc3735e8d1e8204ee57d266318.
4 +Base: source-data-cleanup at 7dc864fd032f0c4ae4d650d89304a01286a6d914.
5 +Changed files: backend/src/aram_backend/mapping_workflow.py,
6 +backend/src/aram_backend/mapping_store.py,
7 +backend/tests/test_mapping_workflow.py,
8 +backend/tests/test_mapping_store.py.
9 +
10 +## Pass 1 — Functional QA
11 +Checked scoped import, explicit lifecycle, review evidence, workbook preflight,
12 +round-trip and original-sheet preservation, unknown-account queue, carry-forward.
13 +Finding: approved mappings could not round-trip; missing review date.
14 +Fix: store review_date in decisions, populate on transition, import/export with
15 +Valid status and mandatory evidence. Added approved round-trip test.
16 +Finding: exporting selected scope removed foreign-scope mappings.
17 +Fix: preserve foreign-scope rows during export.
18 +
19 +## Pass 2 — Architecture and security
20 +Checked parameterized SQL, transaction boundaries, optimistic revision,
21 +append-only audit, company/year isolation, workbook formula/code validation.
22 +Finding: maker could review their own mapping.
23 +Fix: forbid same actor for Draft author and Reviewed transition.
24 +Finding: WAL mode configured on every connection.
25 +Fix: initialize WAL once.
26 +Residual risk: store relies on application-level audit integrity; privileged
27 +database edits can rewrite both state and audit. No cryptographic tamper-evidence.
28 +Residual risk: no authorization/identity provider; actor names are caller-supplied.
29 +
30 +## Pass 3 — Adversarial / chaos
31 +Exercised stale concurrent writers, invalid transitions, cross-company/year
32 +inputs, duplicate IDs, missing review evidence, invalid XLSX, re-opened
33 +persistent state and audit prefix manipulation. Automated tests pass locally.
34 +Residual risk: multi-process stress, power-loss recovery, large-file resource
35 +limits and adversarial malicious workbook security require separate acceptance.
36 +
37 +## Evidence and decision
38 +Backend CI at d75cbbb: https://github.com/davoodmehraban89/Aram/actions/runs/41334206919 — success.
39 +Local full backend suite: 80 tests passed before latest review fixes; rerun
40 +performed after fixes with exit code 0 (82 tests).
41 +No binary, database, financial source or canonical template changed.
42 +Rollback: revert the focused feature branch commits; no migrations applied to
43 +production data. This PR remains Draft.
44 +
45 +**Decision: CHANGES REQUESTED / NOT ACCEPTED.**
46 +These are three sequential self-audit perspectives, not three independent
47 +human or separately instantiated reviewer identities. External independent
48 +review and updated final-head CI are required before formal acceptance.