@davoodmehraban89 / Factor-easy / commits / 4ca089f
Fork 0

سامانه صدور فاکتور و حسابداری

fix(stage2.2): refresh server-authoritative guard on manual UI actions; real JWT stale-mode regression

davoodmehraban89 committed Oct 20, 2026 at 08:52 UTC 4ca089f9142cc2dcd80fb6a2ed9bcb1254db4ec0
3 files changed +39 -2
PROJECT_STATE.md
+6
@@ -206,3 +206,9 @@
206 - REAL-STACK (not harness) Run **39373204611** Job **118465430952** PASS; built Pages + isolated local real GoTrue login/user JWT/PostgREST; JWT REST forged-post **HTTP 400** and cross-org **HTTP 403** denied; one posted voucher, balanced journal, unique number, server audit, legacy audit violation. Artifact **12241978982**, sanitized network evidence includes enforce sync without server-owned fields and legacy audit violations.
207 - HARNESS (not real-stack) Run **39373204631** Job **118465431091** PASS positive manual client 3/3, owner/guard/RPC and legacy negative, local PostgreSQL; artifact **12241824202**. Snapshot baseline parity Run **39373204615** Job **118465431124** PASS (1069 objects, 154 functions lexically equal), artifact **12241869211**; **not fresh live Production parity**. Stage2.1 canaries Run **39373204612** PASS.
208 - No actual independent external review agents commissioned or approvals received; separate real-stack, harness, Quality, Security and parity CI are evidence, **not agent approvals**. Cached off/audit mode has a noted transition risk; Production and merge remain blocked. PR #162 Draft [DO NOT MERGE], Stage2.2 owner acceptance HOLD, no Production writes/enforce/deploy, main merge, Phase1B, Stage2.3 or stopped login work.
209 +
210 +
211 +### 2026-10-20 — Stage2.2 cached guard mode edge regression (test pending)
212 +- An independent edge-case review identified a **stale off/audit UI cache**: an owner could enable enforce elsewhere while the existing tab retained audit. Server triggers were authoritative, but a normal UI action could temporarily use the wrong path.
213 +- Before saving, posting or deleting a manual draft, `js/journal-ui.js` now re-reads organization guard mode from the server; no refresh to a full datastore and no changes to source/commercial flows. `scripts/stage22-real-stack.mjs` now asserts a real JWT-mediated AUDIT→ENFORCE flip with a stale browser cache and proves the UI produces an unnumbered server draft after refresh.
214 +- Previous green exact-head evidence is for `44df49a2fc20c35cddd0665bba945d1e4bd38237` only; this new change requires a fresh all-green exact SHA before claiming verified.
js/journal-ui.js
+3 -2
@@ -61,6 +61,7 @@ async function jeSaveManual(){
61 if(button?.disabled)return;
62 if(button)button.disabled=true;
63 try{
64 + if(typeof finoraManualMode==='function')await finoraManualMode(); // refresh server mode before any UI mutation
65 const input={date:document.getElementById('je-date').value||getJalaliNumeric(),
66 voucherType:document.getElementById('je-voucher-type').value,
67 reference:document.getElementById('je-reference').value,
@@ -82,7 +83,7 @@ async function jePostUi(id){
83 const button=document.querySelector('#je-vouchers button[data-post-id="'+id+'"]');
84 if(button?.disabled)return;
85 if(button)button.disabled=true;
85 - try{await jePost(id);renderJournal();}
86 + try{if(typeof finoraManualMode==='function')await finoraManualMode();await jePost(id);renderJournal();}
87 catch(e){
88 if(/FINORA_VERSION_CONFLICT/.test(String(e?.message||''))&&
89 confirm('نسخه سرور تغییر کرده است. بارگذاری نسخه سرور؟')){
@@ -95,7 +96,7 @@ function jeReverseUi(id){if(window.finoraManualGuardMode==='enforce')return aler
96 function jeCopyVoucherUi(id){if(window.finoraManualGuardMode==='enforce')return alert('کپی سند هنوز برای گام ۲٫۲ فعال نشده است.');try{const v=jeCopyVoucherToDraft(id,getJalaliNumeric());alert('کپی سند به پیش‌نویس '+v.number+' ایجاد شد.');renderJournal()}catch(e){alert(e.message)}}
97 async function jeDeleteUi(id){
98 if(!confirm('پیش‌نویس حذف شود؟'))return;
98 - try{await jeDeleteDraft(id);renderJournal();}
99 + try{if(typeof finoraManualMode==='function')await finoraManualMode();await jeDeleteDraft(id);renderJournal();}
100 catch(e){alert(e.message||String(e))}
101 }
102
scripts/stage22-real-stack.mjs
+30
@@ -193,6 +193,36 @@ try{
193 }
194 if(violations<1)throw Error('AUDIT_MODE_NO_VIOLATION_RECORDED');
195 evidence.scenarios.push('legacy-numbered-draft-audit-with-violation-row');
196 +
197 + // Edge case: mode changes remotely after a client cached AUDIT. A normal
198 + // UI save MUST refresh owner-authorized server state before selecting path.
199 + const remoteSwitch=await page.evaluate(o=>sb.rpc('finora_journal_set_guard_mode',{
200 + p_organization_id:o,p_expected_mode:'audit',p_mode:'enforce',
201 + p_reason:'Real stack stale client guard transition regression test'}),org);
202 + if(remoteSwitch.error||remoteSwitch.data?.mode!=='enforce')throw Error('REMOTE_MODE_FLIP_FAILED');
203 + const cached=await page.evaluate(()=>window.finoraManualGuardMode);
204 + if(cached!=='audit')throw Error('STALE_MODE_REGRESSION_NOT_ESTABLISHED');
205 + await page.evaluate(()=>{
206 + jeClearManualForm();renderJournal();
207 + const lines=document.querySelectorAll('#je-lines .je-line');
208 + if(lines.length<2)throw Error('STALE_MODE_UI_ROWS_MISSING');
209 + lines[0].querySelector('.je-account').value='ST22_CASH';
210 + lines[0].querySelector('.je-debit').value='700';
211 + lines[1].querySelector('.je-account').value='ST22_REV';
212 + lines[1].querySelector('.je-credit').value='700';
213 + document.getElementById('je-date').value='1405/07/26';
214 + document.getElementById('je-desc').value='Stale guard mode browser revision';
215 + });
216 + await page.locator('#je-save-draft-button').click();
217 + await page.waitForFunction(()=>getMyJournalVouchers().some(v=>v.description==='Stale guard mode browser revision'),{timeout:20000});
218 + const revalidated=await page.evaluate(()=>{
219 + const d=getMyJournalVouchers().find(v=>v.description==='Stale guard mode browser revision');
220 + return {number:d?.number||'',status:d?.status,mode:window.finoraManualGuardMode};
221 + });
222 + if(revalidated.number||revalidated.status!=='draft'||revalidated.mode!=='enforce')
223 + throw Error('UI_STALE_AUDIT_MODE_FAILED_TO_REVALIDATE_SERVER');
224 + evidence.scenarios.push('ui-stale-audit-to-enforce-refreshes-server-before-save');
225 +
226 if(remote.length)throw Error('PRODUCTION_NETWORK_ATTEMPT');
227 const enforcedSync=evidence.syncPayload.filter(x=>x.phase==='enforce');
228 const auditSync=evidence.syncPayload.filter(x=>x.phase==='audit');