Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 20:45 UTC
docs: review fiscal year safety PR chain
davoodmehraban89 committed
Nov 11, 2026 at 23:12 UTC
0d86d2e4371883f6e3b14e9eb631b0020b9aebf2
1 file changed
+103
docs/PR32_PR35_FISCAL_YEAR_SAFETY_REVIEW_20261111.md
new
+103
@@ -0,0 +1,103 @@
1
+# Fiscal-Year Ownership Safety Review — PR #32 / PR #35
2
+
3
+**Review date:** 2026-11-11
4
+**Status:** TESTED ORM CANDIDATE — INDEPENDENT ACCEPTANCE BLOCKED
5
+**Accepted baseline:** `source-data-cleanup@7dc864fd032f0c4ae4d650d89304a01286a6d914`
6
+
7
+## 1. Executive decision
8
+
9
+PR #32 and its child PR #35 contain meaningful and tested ORM-level protection for fiscal-year ownership and cross-year note references. PR #35 closes two gaps explicitly left open by PR #32: same-flush Draft-to-Approved/Locked transitions with related mutations, and moves of NoteDefinition / FinancialStatement parents that would invalidate clean, unchanged dependent rows.
10
+
11
+This is a strong application-layer control, but it is **not a complete production integrity boundary**. Both PR descriptions explicitly exclude raw/bulk SQL and concurrent-write enforcement. Neither PR has a submitted GitHub review. The candidate must remain Draft / unmerged until independent acceptance and the residual database/concurrency boundary is addressed or formally accepted for the exact V1 deployment model.
12
+
13
+## 2. Exact candidate lineage and CI
14
+
15
+| Item | PR #32 | PR #35 |
16
+|---|---|---|
17
+| Purpose | Persisted fiscal-year ownership and note isolation | Same-flush and clean-reference gaps |
18
+| Head branch | `codex/v1-year-isolation-followup-20261023` | `codex/aram-same-flush-fiscal-lock-20261030` |
19
+| Final head | `28d1c57882ec20fc17c2c133bbe62ff56e12973f` | `e09aa43d22eb56eca4ceb6c41519abe1c6f571f8` |
20
+| Base | `source-data-cleanup` | PR #32 head |
21
+| Tests reported | 110 passed | 132 passed, 1 warning |
22
+| Exact-head GitHub Actions | [39959793328](https://github.com/davoodmehraban89/Aram/actions/runs/39959793328) | [41062134617](https://github.com/davoodmehraban89/Aram/actions/runs/41062134617) |
23
+| Review submissions | 0 | 0 |
24
+| State | Draft / open / unmerged | Draft / open / unmerged |
25
+
26
+PR #35 is a child of PR #32. Do not merge both as independent roots or cherry-pick PR #35 without its parent changes. Experimental PR #41 was closed without merge and is superseded by the consolidated PR #35 candidate.
27
+
28
+## 3. What is implemented
29
+
30
+### PR #32
31
+
32
+- Reads persisted original ownership before flush, including expired attributes.
33
+- Resolves pending relationship objects, changed foreign keys, cached references and pending explicit-ID parents.
34
+- Rejects mutations of records belonging to Approved / Locked fiscal years.
35
+- Rejects invalid fiscal-year status transitions and identity changes after protection.
36
+- Checks MappingRule and StatementLine target-note fiscal-year consistency.
37
+- Adds 43 regression / valid-path cases; final CI reports 110 passed.
38
+
39
+### PR #35 additions
40
+
41
+- Considers pending FiscalYear approval / locking status in the same flush when evaluating protected records.
42
+- Rejects a Company mutation in the same transaction as Draft-to-Approved / Locked transition.
43
+- Checks persisted, clean MappingRule and StatementLine references when a NoteDefinition moves year.
44
+- Checks clean StatementLine references when a FinancialStatement moves year.
45
+- Preserves valid Draft moves when references are explicitly retargeted or move together.
46
+- Adds adversarial negative, rollback and valid-path tests; final CI reports 132 passed.
47
+
48
+## 4. Residual risks — not covered by these PRs
49
+
50
+### 4.1 Raw SQL / bulk DML bypass
51
+
52
+SQLAlchemy `Session.before_flush` does not intercept direct SQL, bulk update/delete statements, external database clients, migration scripts or other applications writing to the same database. The PRs explicitly exclude these paths. If the product permits direct DB access or maintenance scripts, the ORM hook alone is not sufficient.
53
+
54
+### 4.2 Concurrent transaction race
55
+
56
+The guard reads persisted fiscal-year status and ownership during `before_flush`, but it does not establish a database-level lock or atomic constraint tying the status check to the subsequent write. A concurrent transaction may change status or ownership after the check. The PRs explicitly make no concurrency guarantee.
57
+
58
+### 4.3 Provider / production database boundary
59
+
60
+The CI evidence covers the repository's configured test database and Alembic round-trip, not a live Peyvast SQL Server or another production provider. No production schema, provider-level constraint or real multi-worker deployment is verified.
61
+
62
+### 4.4 Query scale and performance
63
+
64
+The clean-reference guard queries dependent MappingRule and StatementLine rows when parent years move. No workload benchmark or index review is recorded for large ledgers / note catalogs. Profile the relevant foreign-key indexes and query plan before production.
65
+
66
+### 4.5 Independent acceptance
67
+
68
+Both PRs have zero submitted GitHub reviews. Passing CI and the author's test report are not independent approval. The author cannot self-approve financial integrity, security and operational acceptance.
69
+
70
+## 5. Required production acceptance gates
71
+
72
+| Gate | Required evidence | State |
73
+|---|---|---|
74
+| ORM invariant tests | Exact-head regression tests for original-owner, same-flush, clean-reference, rollback and valid Draft paths | PASS in candidate CI |
75
+| Database-level integrity | Composite foreign keys / constraints or triggers that enforce same-year references and protect Approved/Locked ownership against direct SQL | NOT_IMPLEMENTED / NOT_VERIFIED |
76
+| Concurrency | Two-session race tests for Draft->Approved vs concurrent row mutation and parent-year moves | NOT_IMPLEMENTED |
77
+| Raw SQL / bulk DML | Adversarial direct SQL tests proving the database rejects protected writes and cross-year links | NOT_IMPLEMENTED |
78
+| Migration | Upgrade/downgrade against empty and pre-existing synthetic data with the chosen constraints | PARTIAL; current Alembic round-trip passes but no new DB enforcement exists |
79
+| Performance | Explain/query-plan and workload test for clean-reference scans and foreign-key indexes | NOT_VERIFIED |
80
+| Provider compatibility | Test on the exact V1 database provider and supported deployment topology | NOT_VERIFIED |
81
+| Independent reviews | Separate engineering, accounting-control and release/operations approvals on exact final SHA | BLOCKED |
82
+
83
+## 6. Recommended technical direction
84
+
85
+For a multi-user or externally accessible deployment, add a database-level enforcement layer rather than relying solely on ORM hooks:
86
+
87
+1. For `MappingRule`, consider a composite uniqueness key on `NoteDefinition(id, year_id)` and a composite foreign key `(target_note_id, year_id)` to prevent cross-year targets.
88
+2. For `StatementLine`, evaluate storing / constraining its owning `year_id` so both `(statement_id, year_id)` and `(target_note_id, year_id)` can be enforced, or use provider-specific triggers if denormalization is unacceptable.
89
+3. Add database triggers / constraints or an equivalent controlled-write boundary to prevent changing company/year identity or mutating year-owned rows once the parent year is Approved / Locked.
90
+4. Use transaction isolation / row-locking appropriate to the actual database provider and prove race behavior with two independent sessions.
91
+5. Keep the ORM hook as an early, user-friendly validation layer; it should complement, not replace, database enforcement.
92
+
93
+These are design recommendations, not an approved schema change. The exact mechanism depends on the selected V1 database and migration strategy.
94
+
95
+## 7. Integration recommendation
96
+
97
+- Keep PR #32 and #35 as one dependency family.
98
+- Treat PR #35 as the more complete ORM candidate, but do not call it production-ready.
99
+- Do not merge PR #32 alone and then forget the PR #35 same-flush / clean-reference follow-up.
100
+- Do not merge PR #35 into the accepted baseline until the database/concurrency decision is explicit, independent reviews are submitted, and final CI runs on the rebased exact head.
101
+- Preserve PR #41 as closed-without-merge historical RED/GREEN evidence; do not resurrect it.
102
+
103
+**Decision:** retain the family as a P0 safety candidate. Current evidence proves tested ORM-level behavior only; production fiscal-year integrity remains NO-GO until the residual gates are passed or the deployment threat model is explicitly narrowed and approved.