سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 08:00 UTC
hardening/wp1-ledger-integrity-20261102
WP1 client sync contract
This is candidate application code in draft PR195, not a production activation or complete WP1 acceptance.
- Existing record hydration requests select
*under existing RLS (and the organization filter). Optional server revisions are captured in a separate map. No extra capability call is made during login; a missing version column does not break the legacy read. Present but unsafe revisions reject the snapshot rather than permitting a legacy downgrade. - Before a changed batch is sent, a bounded guard-mode read discovers the staged protocol. Only explicitly absent function codes, with no prior revision/capability evidence, allow the old transactional RPC. Transport, permission and malformed responses stop the write. Once a versioned server is observed, it never silently downgrades to a legacy write.
- Existing rows require the last read revision; creates require zero. A missing revision blocks the batch without discarding pending edits. Upserts and deletes use the same V2 transaction and organization argument. Captured serialized payloads cannot be changed by nested edits made during the capability request.
- The complete acknowledgement must identify every requested row exactly once, without extra/deleted identities or invalid revisions. Every returned upsert revision must be exactly expected+1. No partial acknowledgement advances either snapshot.
- Identity, organization, user object and snapshot generation are checked around awaited work. The published snapshot also carries its own user/organization binding: changing the active organization before its read completes cannot send the old organization’s data, even for the same user and colliding record IDs. Late responses cannot publish into another tenant or overwrite a newer same-tenant refresh. Revocation clears revisions and the tenant binding, and advances the generation. A queued save in a newly hydrated context resumes after an old request releases the lock; same-context conflicts do not gain a retry timer.
- V2 rejection, CAS conflict, malformed reply, timeout and capability failure preserve local edits, keep
syncPending, show an unconfirmed-save badge, and schedule no automatic retry timer or reload. Local edits are memory-only: exiting/reloading can still lose them. Existing explicit save/visibility actions may retry; full user-facing reconciliation/export remains an open WP1 task. A timeout can mean the server committed without a received acknowledgement; no success is inferred. - The organization extension no longer replaces the write protocol with a separate legacy implementation. Existing legacy financial-conflict recovery is retained only on the confirmed legacy path.
Evidence and limits
node --test scripts/wp1-client-capability-test.mjs: 43 PASS. The original 36-case subset against pre-change files from799cff4 fails31 cases, including incomplete acknowledgements, missing revisions and tenant races. The later scheduling/tenant/status cases have separate RED evidence captured before their fixes. These are synthetic VM protocol tests. Source browser coverage also exercises the real organization extension and rendered save badge; generated Pages testing uses the same contract with bundled code/CSP. Full acceptance must still exercise real Auth/JWT/PostgREST and the actual deployed schema, and server-authoritative manual/purchase workflows remain separate work.
The local image initially lacked Chromium; it was installed. Default system WOFF2 fonts then reproduced invoice input/printing failures on unchanged799cff4. With a process-only fontconfig selecting installed TTF/OTF system fonts, both baseline comparison cases passed without application/assertion changes. This environment workaround is outside repository/CI configuration. Failed/incomplete attempts are not counted as passes.
Independent review identified and reproduced two scheduling/tenant races. Both were fixed with failing-before/passing-after tests; final reviewer independently ran43/43 tests and found no remaining blocking issue in this scoped review. This does not replace browser or production acceptance. Legacy browser fixtures explicitly model the absent mode RPC (PGRST202), and real hydration is required before fixture writes; existing persistence/print assertions are retained.
A final identity-refresh status regression is also covered: with no queued save, stale write completion changes a still-pending same-tenant badge from saving to unconfirmed, without accepting the stale ACK or clearing a newer save timer.
Final local checkpoint2026-11-04:43Node,222full-browser,8generatedPages and55quality commands PASS. C8 fixture now waits for actual organization/startup readiness before injecting synthetic authority;30repeated cases PASS. Hashes and evidence. Exact client head56818d8 subsequently passed Quality41758729100,Security41758729122 andWP1proof41758729204. Later SQL changes require their own exact-head CI.
Targeted journal graph reconciliation (candidate, 2026-11-11)
finoraCaptureJournalRefresh(voucherId, readViewContext) captures a hydrated user/organization/generation, the affected local records plus their snapshots/revisions, and a live view identity. The accessor must return {companyId, formId, editRevision}: formId is unique per form opening, and editRevision advances for every relevant input change. The future manual adapter must capture before asynchronous work, not after it. Affected unsaved edits refuse refresh; unrelated dirty rows are allowed. No existing manual handler calls this API yet.
await finoraRefreshJournalGraph(capture) queues behind the normal sync lock (20-second bound), validates the capture after acquiring it, calls only finora_journal_read_graph (20-second bound), revalidates the capture and validates the entire reply before publishing. Null/missing header, invalid tenant/company/links/revisions/status, stale revision and ID collision refuse publication. Normal saves cannot overlap the read/publication. A late response after timeout cannot publish. Only the three affected journal arrays and corresponding snapshot entries change; unrelated row references, pending deletions and save timers survive. Deleted children are removed only after a valid complete graph is received. A null graph is never interpreted as confirmed deletion.
Return value {voucherId, effectiveStatus} is display metadata. Stored original status remains intact. Refresh does not clear the form, authorize a write, mark a mutation successful, or use a full pullAll/snapshot replacement. A read-only member can refresh visible records; the RPC remains the authority for visibility. The planned mutation adapter still needs its own operation serialization, intent/retry policy and truthful confirmed-write/failed-refresh state; do not infer those from this read-only API.
A successful targeted publication advances the snapshot generation. Full reads already in flight cannot overwrite it, and older queued graph captures must be recaptured explicitly. A preserve-pending write failure discards save intent queued before that failure; a genuinely new explicit save during the following graph read still resumes. These two ordering regressions were independently reproduced before their fixes.