codex/gate3-signed-year-scope-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
md 6.27 KB

ARAM Gate 3 — current note targets and read-only mapping change preview

Status: PROPOSED, unaccepted Draft PR #50. Parent: Draft PR #26 exact SHA 185e47f031902a8918c93c8dbb420122e7408403. Accepted source-data-cleanup@7dc864fd032f0c4ae4d650d89304a01286a6d914 is unchanged.

Financial integrity contract

The existing explicit save_mapping_rule validated same fiscal year but could persist a MappingRule that targets an obsolete NoteDefinition version of that year. A later review would classify it stale, while initial persistence had already succeeded. This is an inconsistent financial classification boundary.

  • save_mapping_rule must reject stale NoteDefinition targets, where stale means a different NoteDefinition of the same year_id and note_number has the latest version DESC, id DESC. This check must happen before replay/idempotence handling, preventing an obsolete rule from silently being reaffirmed.
  • A current target version remains valid for explicit persistence, subject to the existing Draft, company/year/account code, length, input and caller-controlled commit checks.
  • This guard is SQLAlchemy ORM/service only, not DB protection against raw SQL, bulk SQL, external writers or races. Provider-level enforcement is a later gate Issue #43.

Review preview contract — NOT approval

Provide preview_mapping_change as a deterministic, strictly read-only preparation stage for a future authenticated human approval workflow:

  1. Caller must supply positive year_id, exact rule_key, account_code, target_note_id, mapping_type and a nonnegative expected_base_version. 0 is the explicit precondition that this rule key has no saved version.
  2. Fiscal year must be Draft; account must exist in the same year; note must exist in the same year and be its latest version. Never guess account, note, version, precedence or financial value.
  3. Read the latest rule for the exact year/key. Reject a stale expected version, and reject a no-op proposal instead of pretending it is a review decision. Propose exactly previous.version + 1 (or version 1 on first creation).
  4. Return immutable old/new snapshots, fiscal year and company IDs, latest target-note version/number, latest account version ID/version and deterministic SHA-256 fingerprint of their canonical JSON representation. This digest is an unsigned comparison checksum, not user identity, authorization, review proof, tamper-resistant signature or approval.
  5. verify_mapping_change_preview_current must regenerate all authoritative values from the Session and reject changed rule version/target, account version, target-note version, fiscal-year status or altered preview data. It only checks staleness at query time and does not save changes.
  6. Fail closed on unflushed pending changes to FiscalYear, AccountVersion, MappingRule or NoteDefinition rather than silently capture a misleading persisted snapshot. Unrelated pending Company objects are not implicitly flushed. Query path uses Session.no_autoflush.
  7. No approval state transition, actor account, role, audit write, generated statement, mapping mutation, automatically persisted candidate or financial source file can be produced by either preview function.

Why this is not a complete approval lifecycle

A caller still can invoke the existing low-level save_mapping_rule directly without going through preview. No authenticated principal, qualified reviewer roster, approved mapping decision, immutable audit trail or DB-level transaction lock is in place. Even a perfectly matching verify result is not safe against a concurrent update after verification. Only a separately reviewed authenticated approval endpoint/service with strict user roles, distinct identities, transactional version compare-and-set, immutable audit and provider-specific isolation may execute an Approved transition.

Those outstanding deliverables remain tracked by Issue #49, Issue #39 and Issue #43. Do not advertise this PR as Gate 3 complete or merge before parent and independent C1/C2/C3 controls pass.

Acceptance tests

Synthetic-only regression: stale same-year note rejected on save and on attempted idempotent replay; latest note allowed. Read-only preview: creation, current old/new, version increments, no-op, stale expected version, changed source rule, changed note, changed account, year lock, wrong-year target, falsified comparison fields, invalid inputs, fail-closed pending writes, no autoflush and no DB mutation. Full pytest, disposable Alembic round-trip, compileall and final-head CI; independent finance acceptance required. No customer XLSX/PDF or production database touched.

Rollback: close the unmerged Draft, or revert with separately reviewed PR after authorized merge.

Explicit external-writer / identity-map stale-read control

A Session identity map may hold objects loaded before an independent connection updated a Rule or FiscalYear. Calling Session.get without refresh, or executing an ORM select without refreshing existing identity-map objects, can return stale object fields even when a SQL SELECT runs. Merely recalculating a fingerprint against those cached objects is inadequate.

  • For the fiscal-year row and requested NoteDefinition use Session.get(..., populate_existing=True); for the latest Rule, latest NoteDefinition and latest AccountVersion queries use .execution_options(populate_existing=True). All remain within Session.no_autoflush and the clean-related-Session precondition.
  • Synthetic two-session tests prove the former implementation wrongly accepted the old preview after an independent SQL UPDATE of the same Rule content or FiscalYear status.
  • This improvement refreshes visible committed state under the test SQLite transaction behavior. It is not an isolation-level guarantee for every provider, not a lock, and cannot prevent a concurrent transaction that commits after verification. Provider-specific transaction/compare-and-set enforcement and human approval remain separate.