@davoodmehraban89 / Aram-1 / commits / 2d1c875
Fork 0

docs(integrity): add issue-36 scoped design and test plan

davoodmehraban89 committed Oct 30, 2026 at 16:45 UTC 2d1c87598dd078497317610cb47d81461ec0d30e
1 file changed +57
docs/superpowers/specs/2026-10-30-fiscal-year-graph-integrity.md new
+57
@@ -0,0 +1,57 @@
1 +# Fiscal-year Unit-of-Work and Graph Integrity — 2026-10-30
2 +
3 +- **Status:** Proposed implementation, pending independent review.
4 +- **Authority:** [Issue #36](https://github.com/davoodmehraban89/Aram/issues/36).
5 +- **Exact base:** unmerged [PR #32](https://github.com/davoodmehraban89/Aram/pull/32) `codex/v1-year-isolation-followup-20261023@28d1c57882ec20fc17c2c133bbe62ff56e12973f`.
6 +- **Candidate:** [Draft PR #35](https://github.com/davoodmehraban89/Aram/pull/35), consolidated from initial separate Drafts #35 and #41; #41 is superseded by this candidate.
7 +- **Accepted branch:** `source-data-cleanup@7dc864fd032f0c4ae4d650d89304a01286a6d914`, not modified.
8 +
9 +## Purpose and threat model
10 +
11 +Financial-year integrity is not merely a row-level constraint. SQLAlchemy's `Session.before_flush` inspects pending changes before SQL has been emitted, and persisted status may still be Draft even though the in-memory FiscalYear object is already Approved. Separately, moving a fiscal-year parent can invalidate already-persisted clean mapping/statement dependencies that are absent from the Session's dirty set.
12 +
13 +These are reproduced defects in an **ORM application boundary**. This change does **not** claim to implement database-level invariants, row locks, concurrent-session isolation, custom permissions, PostgreSQL/SQL Server triggers, external writer restrictions, or customer data validation. Those require separate scoped designs and provider-specific evidence.
14 +
15 +## Invariant S1 — Same-flush approval is atomic
16 +
17 +When an existing FiscalYear transitions Draft → Approved in a Session, no creation, modification or deletion of an associated year-owned record, `NoteRow`, or `StatementLine` is permitted in that same flush. Modification of the associated Company is also forbidden. The same rules apply to Locked status.
18 +
19 +Resolve **proposed effective status** from `Session.new | dirty | deleted` for a matching FiscalYear identity, then consult persisted status for all other years. A database query alone is inadequate before SQLAlchemy flushes the transition.
20 +
21 +- **Reject:** Draft → Approved plus edit of AccountVersion, SourceDocument, NoteDefinition, FinancialStatement, MappingRule, NoteRow or StatementLine in one commit.
22 +- **Reject:** Draft → Approved plus new SourceDocument, or an existing Company change in the same transaction.
23 +- **Permit:** ordinary Draft edits committed separately before an approval-only transition, and valid Draft→Approved/Approved→Locked transitions without concurrent data mutation.
24 +- **Rollback:** rejected transaction must persist neither the status transition nor the affected record change.
25 +
26 +## Invariant S2 — Re-evaluate clean graph references
27 +
28 +When a **persisted** `NoteDefinition.year_id` changes between Draft years, inspect persisted `MappingRule` and `StatementLine` rows whose target_note_id points to it, even if they are not in `Session.dirty`. Re-evaluate each dependent's effective in-session mapping year or financial-statement year against the effective target note's year.
29 +
30 +When a **persisted** `FinancialStatement.year_id` changes between Draft years, inspect persisted linked StatementLine rows, even when unchanged; reject any non-null target note whose effective year disagrees with its financial statement's effective year.
31 +
32 +- **Reject:** moving a Draft note while a clean mapping or note-linked statement line would remain tied to the old year, whether the years belong to the same Company or different Companies.
33 +- **Reject:** moving a Draft statement while a clean note-linked statement line remains tied to a note in the old year.
34 +- **Permit:** moving a previously unreferenced Draft note; moving a Draft statement whose existing lines have no target note.
35 +- **Permit when internally consistent:** coordinated in-Session movement of a Draft note and the mapping owner, coordinated movement of a Draft note and statement parent, or an explicit reference retarget to a different note in the original Draft year.
36 +- **Reject** any outcome still containing cross-year mapping/statement-note references. If the requested accounting operation cannot be independently reviewed and justified, prefer a **separate authorized transaction**: detach/review old references, commit, then move/rebuild the parent; do not silently carry hidden dependencies.
37 +- **Rollback:** database reload after rejected commit shows original fiscal years and persistent references.
38 +
39 +## Implementation boundary
40 +
41 +Use one existing synchronous SQLAlchemy `before_flush` event. Add narrowly scoped helper(s) to inspect pending status and affected persisted references. Do **not** mutate accounting data, auto-repair links, rewrite note numbers, infer mapping decisions, or make an unauthorized mapping transfer. Parent graph checks run only when a persisted note/statement has actually changed `year_id`.
42 +
43 +This is application-level prevention within a Session using the ORM. It does not eliminate race conditions: another Session can change the linked rows after the check without database enforcement. The target-note relations may be unindexed in the current schema; synthetic-scale performance profiling and an independently reviewed migration may be necessary before deployment.
44 +
45 +## Acceptance tests and evidence
46 +
47 +- RED: run newly added regressions on unchanged PR #32; demonstrate nine same-flush bypass failures and six clean-reference bypass failures (the latter reproduced after first fix; all failures must be for the intended missing ValueError).
48 +- GREEN: full test suite verifies allowed Draft operations as well as prohibited transitions, rollback and relationship isolation. Final-head GitHub Actions must show all tests, isolated Alembic upgrade/downgrade and compileall success.
49 +- Exact changed-file allowlist: `backend/src/aram_backend/models.py`, `backend/tests/test_models.py`, this spec, its companion plan, `docs/PROJECT_LOG.md`, `docs/handoff/LATEST_HANDOFF.md`.
50 +- No `PROJECT_STATUS.md` change relative to parent #32; no migration, database, `data/`, canonical XLSX/PDF, dependency, workflow, binary, secret or release change.
51 +- All source-to-report financial integrity and owner accounting approvals beyond these invariants remain unverified.
52 +
53 +## Reviewer separation and release safety
54 +
55 +C1 technical/security, C2 financial/data integrity, and C3 acceptance/release must be three separate, verifiable, non-author reviewer identities. An author-run three-pass audit is not independent evidence. The proposal stays Draft/unmerged when any gate is missing. Rebase on an **accepted** parent SHA only after #32 itself passes its acceptance gates; then rerun all checks.
56 +
57 +Rollback before merge: close Draft PR. After a properly authorized merge: independently reviewed revert PR; never force-push, modify protected canonical financial sources, or misstate unmerged code as accepted.