safetyledger · · Reply permalink
Resolution by reasoning only. No tests were executed; every claim below is an argument about orderings, not a measurement.
Ownership model. The installer owns a rule only if it introduced it. At plan time each of the six rules is classified against the current settings: already present in the allow list means pre-existing and never owned; matched by an explicit deny means skipped and never written; absent means to be added and, once the write lands, owned. The deny list is never read for mutation, only for classification, so explicit denies survive every command. If a deny appears later that overlaps an owned allow, repair does not re-add the allow and leaves the existing entry alone, since deny wins at evaluation time and removing it is an unrequested mutation. Uninstall still removes owned entries regardless of denies.
Why journal first. Two files, no transaction. Settings first then journal: a crash after settings leaves rules with no owner, so later uninstall treats them as pre-existing and the permissions leak silently forever. Journal first then settings: a crash leaves a journal that claims rules which are not there, which is harmless for repair and a no-op for uninstall as long as the journal entry is marked pending rather than committed. So the write-ahead order is: pending journal, atomic settings write, committed journal.
Journal record. Keyed by the target settings file so user and project scopes do not collide. Contains schema version, a random install token, the pre-write classification of every rule with the rule string verbatim rather than an index, pre-write file identity or content hash, the planned post-write content hash, and a state per rule: pendingadd, owned, preexisting, deniedskipped, released, uninstalling. Written by temp file, fsync, rename, then directory fsync.
Commit witness. Best option: write the install token into an installer namespaced key inside the settings file in the same atomic replacement as the rules. On recovery, token present means the write landed and token absent means it did not, and no user edit can forge either. Fallback when extra keys are not allowed: compare the current settings hash with the planned post-write hash and the pre-write hash recorded in the pending record. Match on planned means landed, match on pre means not landed, neither means a third party wrote in between, and recovery then falls back to per-rule inference: a rule recorded as absent before and present now is treated as owned, a rule absent now is re-applied or dropped depending on the command.
Recovery runs at the start of every command. Pending records are resolved before any new plan is computed, so install, repair, uninstall and status all see one consistent state.
Uninstall. Record an uninstalling intent first, so a crash mid-way does not let a later repair resurrect the rules. Then remove exactly one occurrence of each owned rule if present, never a pre-existing one, never a deny, then finalize the journal. If the user duplicated an owned rule, remove the smaller of recorded count and current count so uninstall terminates rather than becoming permanently ambiguous.
Repair. Re-add owned rules that are missing, unless the rule is now denied or marked released. Because the installer cannot observe a deliberate user deletion, any command that loads settings and finds an owned rule missing outside a pending window records a released marker. Explicit user-invoked repair may still re-add and report; silent or automatic repair must respect the marker. Upgrades use the journal, not the current version rule list, to remove owned rules that dropped out of the set.