سامانه صدور فاکتور و حسابداری
hardening/wp1-ledger-integrity-20261102
Next WP1 increment: manual journal server workflow
Status: source-backed implementation plan. The read prerequisite now has an isolated candidate in JOURNAL_GRAPH_READ.md; targeted client merge is implemented as a candidate (see GRAPH_RECONCILIATION_LEDGER.md); manual adapter and remaining items are not implemented or accepted. Depends on the integrated ledger guards and versioned sync; remains in draft PR195. No production activation.
Problem and invariant
js/journal-ui.js still calls synchronous local engine operations from its explicit save/post/reverse/delete/copy/return-to-draft handlers. The server mutation RPCs return a voucher and revision, without the authoritative line/dimension graph. A whole-datastore pullAll() after a mutation can erase unrelated pending edits. Replacing the synchronous engine globally would also affect business-document callers.
Preserve the synchronous engine for existing callers; add an asynchronous adapter at the manual UI boundary. A successful server write and a successfully refreshed display are separate outcomes. Never show a generic success or silently retry a mutation after an uncertain response.
Ordered implementation
- Read-only graph RPC. Add a SQL/STABLE SECURITY INVOKER function returning the visible header plus journalLines/journalLineDimensions and separate record revisions in a single statement/snapshot. Use existing RLS; revoke PUBLIC/anon EXECUTE and grant authenticated only. A private boolean permission check must reject partially visible child/reversal graphs, without returning hidden records. Return null for an absent/invisible header, never an orphan child graph. Include organization/voucher identity and deterministic record ordering. Add
effective_statusfrom the existing security-invoker reversal view for posted originals, falling back to the stored status for drafts/reversal documents. Reject/omit cross-company or mismatched line links explicitly; do not normalize corrupt history. Document which behavior is selected in the test contract. - Pending-edit-safe graph reconciliation. Serialize with
syncRunningand the normal sync queue; capturefinoraSyncContext, hydrated tenant, company, form identity and affected records. Read/validate the complete graph before changing local arrays. Merge only affected rows and theirsyncSnap/versionSnapentries atomically, preserving unrelated dirty objects and timers. Check the context and affected local graph again after every await. Do not usefinoraPublishSyncSnapshotto replace the whole datastore after a single operation. - Manual form adapter. Capture the expected revision when opening an edit. Whitelist company/year/date/description/reference/type for creation; date/description/reference/type for edits. Never send client numbers, posted fields, source authority or owner authority. Drafts have no server number until posting. Normalize dates and integer IRR amounts. Positive capability uses RPC; only an explicitly absent capability without positive revision evidence permits legacy behavior. No fallback after a supported endpoint fails.
- Mutation outcomes/retries. Allow one pending form operation; retain the same intent ID and payload for uncertain create/reversal retries. Update/delete do not have identical replay semantics and need explicit reconciliation. If the server confirms a mutation but graph refresh fails or local input changed, display “server operation confirmed; display refresh pending” and retain the form/edits. A changed tenant/form must not be cleared by a stale completion. Derive totals from authoritative lines for display only; do not sync display-only fields into posted history.
- Linked correction transaction. Return-to-draft requires a new atomic reversal + linked corrective draft RPC and replay contract. Two separate calls can leave a reversal without the promised draft; do not ship that behavior. Copy may populate a new manual form for explicit save; existing
copyvoucher type is unsupported under ENFORCE. Opening/closing stay unavailable until their own approved server contracts. - Dimensions contract before acceptance. Current client tags can reference contact/project/branch/contract/costCenter entities directly, whereas staged server validation expects dimensionValues. Account required/optional/unavailable rules also need server enforcement. Preserve report meanings of counterpartyId/projectId through a documented authoritative or derived representation. Do not enable a workflow that silently drops these identifiers.
Required tests
- Read graph: same IDs in two organizations, revoked member, absent header with child rows, anon ACL, wrong-company child, unrelated children, full and partial reversal statuses, complete graph above client page-size limits, deterministic order and safe revisions.
- Client: no double-submit, uncertain write, confirmed write + failed refresh, null/malformed graph, editing during refresh, unrelated dirty edits, organization/company switch, stale form completion, same-user organization hydration, normal sync competing with graph merge, read-only roles, unsupported opening/closing/copy behavior.
- Server: current/stale/NULL revisions; invalid dates, amounts and dimensions; RLS/permission negatives; atomic correction and replay; real two-session conflicts; conservative DOWN/UP with retained history.
- UI: form state and truthful save status, draft numbering, preserved input, complete/partial reversal display, generated Pages/CSP behavior, full regression at the exact final SHA.
A source-derived PostgreSQL fixture is not actual Auth/JWT/PostgREST or production schema parity. Independent SQL review and real non-production service acceptance remain distinct gates. Purchase correction/cancel and other source adapters follow their own transaction contracts; this increment cannot close all of WP1 by itself.