@davoodmehraban89 / Aram-1 / commits / c1235a2
Fork 0

fix(mapping): revalidate all batch rows including unchanged catalog evidence

davoodmehraban89 committed Oct 30, 2026 at 19:14 UTC c1235a2d9ad1a43e42936ace924e2b96c38372fa
1 file changed +45 -3
backend/src/aram_backend/mapping_batch_review.py
+45 -3
@@ -48,6 +48,9 @@ class MappingBatchPreview:
48 year_id: int
49 rows: tuple[MappingBatchRowResult, ...]
50 fingerprint: str
51 + # Exact submitted typed evidence, not an authentication token.
52 + # Required to replay and revalidate every row before later review.
53 + source_rows: tuple[ProposedMappingRow, ...]
54
55 @property
56 def counts(self) -> dict[str, int]:
@@ -156,17 +159,32 @@ def preview_mapping_batch(
159 target_note_id=entry.target_note_id,
160 mapping_type=entry.mapping_type,
161 expected_base_version=entry.expected_base_version,
162 + allow_unchanged=True,
163 )
164 except MappingNoChange:
161 - result = MappingBatchRowResult(source, str(entry.rule_key), "unchanged")
165 + # Defensive compatibility: only the single-item API is
166 + # expected to raise no-change with allow_unchanged=False.
167 + result = MappingBatchRowResult(source, str(entry.rule_key), "invalid",
168 + reason="missing unchanged state evidence")
169 except MappingValidationError as exc:
170 result = MappingBatchRowResult(
171 source, str(entry.rule_key), "invalid", reason=str(exc),
172 )
173 else:
174 + unchanged = preview.before is not None and (
175 + preview.before.account_code, preview.before.target_note_id,
176 + preview.before.mapping_type,
177 + ) == (
178 + preview.proposed.account_code, preview.proposed.target_note_id,
179 + preview.proposed.mapping_type,
180 + )
181 result = MappingBatchRowResult(
182 source, entry.rule_key,
169 - "new" if preview.before is None else "changed",
183 + "unchanged" if unchanged else (
184 + "new" if preview.before is None else "changed"
185 + ),
186 + # Keep full catalog and rule version snapshot even for
187 + # unchanged rows so later changes cannot go unnoticed.
188 preview=preview,
189 )
190 if isinstance(entry.rule_key, str):
@@ -190,4 +208,28 @@ def preview_mapping_batch(
208 sort_keys=True, separators=(",", ":"), ensure_ascii=True,
209 ).encode("utf-8")
210 ).hexdigest()
193 - return MappingBatchPreview(year_id=year_id, rows=tuple(results), fingerprint=fingerprint)
211 + return MappingBatchPreview(
212 + year_id=year_id, rows=tuple(results), fingerprint=fingerprint,
213 + source_rows=entries,
214 + )
215 +
216 +
217 +def verify_mapping_batch_current(session: Session, snapshot: MappingBatchPreview) -> None:
218 + """Fail closed if ANY source row, catalog or result changed after preview.
219 +
220 + Read-only verifier; it is neither authenticated approval nor authorization
221 + to write. Never replace a reviewed snapshot with a refreshed one silently.
222 + """
223 + if not isinstance(snapshot, MappingBatchPreview):
224 + raise MappingValidationError("invalid mapping batch snapshot")
225 + if not snapshot.ready_for_review:
226 + raise MappingValidationError("mapping batch is not ready for review")
227 + try:
228 + current = preview_mapping_batch(
229 + session, year_id=snapshot.year_id, rows=snapshot.source_rows,
230 + )
231 + except (MappingValidationError, TypeError, ValueError) as exc:
232 + raise MappingValidationError("mapping batch review is stale or invalid") from exc
233 + if current != snapshot or not current.ready_for_review:
234 + raise MappingValidationError("mapping batch review is stale or altered")
235 +