Open History to see the last commit
1---------------------------- MODULE SourceHealth ----------------------------
2(***************************************************************************)
3(* The source health channel. Status gates the first ready; health is *)
4(* metadata that a source reports after it. A live query exposes an error *)
5(* tree with one node per collection it depends on. The tree always has *)
6(* the same shape, and each node mirrors its collection's reports. *)
7(* *)
8(* Topology: sources A and B. Live query L1 reads A. Live query Top reads *)
9(* L1 and B, so A reaches Top only through L1. Properties H1 to H11 are *)
10(* the frozen list in README.md. *)
11(***************************************************************************)
12EXTENDS Naturals
13
14CONSTANTS
15 HealthFlipsStatus, \* a post-ready failure changes the gate status (old Electric/PowerSync)
16 NestedDoesNotRelay, \* a nested live query does not pass its sources' health up
17 FrameworkClears, \* the framework clears an error without a source report
18 DropHealthyNodes, \* the tree lists only failing nodes
19 FailureClearsRows \* a post-ready failure discards visible rows
20
21Sources == {"A", "B"}
22Nodes == {"A", "B", "L1"} \* the nodes in Top's health tree
23
24VARIABLES
25 status, \* gate per source: "loading" | "ready" | "error"
26 health, \* per source, after ready: "healthy" | "failed"
27 rows, \* per source: visible rows exist
28 tree, \* Top's health tree: node -> "ok" | "error"; DOMAIN is the shape
29 topStatus, \* Top's gate: "loading" | "ready" | "error"
30 topRows, \* Top's visible rows
31 reported \* per source: the source's latest report was a failure
32
33vars == <<status, health, rows, tree, topStatus, topRows, reported>>
34
35\* What Top's tree must show, derived only from source state (H4, H11).
36NodeState(n) ==
37 CASE n = "L1" -> IF status["A"] = "error" \/ health["A"] = "failed" THEN "error" ELSE "ok"
38 [] OTHER -> IF status[n] = "error" \/ health[n] = "failed" THEN "error" ELSE "ok"
39
40Relay ==
41 [n \in (IF DropHealthyNodes THEN {m \in Nodes : NodeState(m) = "error"} ELSE Nodes) |->
42 IF NestedDoesNotRelay /\ n = "L1" THEN "ok" ELSE NodeState(n)]
43
44\* The gate is unchanged from the failure law: Top waits for both inputs.
45TopGate ==
46 IF \E s \in Sources : status[s] = "error" THEN "error"
47 ELSE IF \A s \in Sources : status[s] = "ready" THEN "ready" ELSE "loading"
48
49Propagate(keepGate) ==
50 /\ tree' = Relay'
51 /\ topStatus' = IF keepGate /\ topStatus = "ready" THEN "ready" ELSE TopGate'
52 /\ topRows' = IF topStatus' = "ready" THEN rows'["A"] /\ rows'["B"] ELSE topRows
53
54Init ==
55 /\ status = [s \in Sources |-> "loading"]
56 /\ health = [s \in Sources |-> "healthy"]
57 /\ rows = [s \in Sources |-> FALSE]
58 /\ reported = [s \in Sources |-> FALSE]
59 /\ tree = [n \in Nodes |-> "ok"]
60 /\ topStatus = "loading" /\ topRows = FALSE
61
62\* Before the first ready, the gate decides (H2).
63Ready(s) ==
64 /\ status[s] = "loading"
65 /\ status' = [status EXCEPT ![s] = "ready"] /\ rows' = [rows EXCEPT ![s] = TRUE]
66 /\ UNCHANGED <<health, reported>>
67 /\ Propagate(TRUE)
68
69InitialFailure(s) ==
70 /\ status[s] = "loading"
71 /\ status' = [status EXCEPT ![s] = "error"]
72 /\ UNCHANGED <<health, rows, reported>>
73 /\ Propagate(TRUE)
74
75\* After the first ready, a failure is metadata (H1).
76ReportFailure(s) ==
77 /\ status[s] = "ready" /\ health[s] = "healthy"
78 /\ health' = [health EXCEPT ![s] = "failed"] /\ reported' = [reported EXCEPT ![s] = TRUE]
79 /\ status' = IF HealthFlipsStatus THEN [status EXCEPT ![s] = "error"] ELSE status
80 /\ rows' = IF FailureClearsRows THEN [rows EXCEPT ![s] = FALSE] ELSE rows
81 /\ Propagate(~HealthFlipsStatus)
82
83\* Only the source clears its own failure, by reporting that sync works (H3).
84ReportHealthy(s) ==
85 /\ health[s] = "failed"
86 /\ health' = [health EXCEPT ![s] = "healthy"] /\ reported' = [reported EXCEPT ![s] = FALSE]
87 /\ UNCHANGED <<status, rows>>
88 /\ Propagate(TRUE)
89
90\* Weakening: the framework clears a failure on its own (H3).
91FrameworkClear(s) ==
92 /\ FrameworkClears /\ health[s] = "failed"
93 /\ health' = [health EXCEPT ![s] = "healthy"]
94 /\ UNCHANGED <<status, rows, reported>>
95 /\ Propagate(TRUE)
96
97Next == \E s \in Sources :
98 Ready(s) \/ InitialFailure(s) \/ ReportFailure(s) \/ ReportHealthy(s) \/ FrameworkClear(s)
99
100Spec == Init /\ [][Next]_vars
101
102---------------------------------------------------------------------------
103\* H11: the tree always has the same shape.
104StableShape == DOMAIN tree = Nodes
105\* H4: every node mirrors its collection, including through the nested live query.
106TreeMirrorsSources == \A n \in DOMAIN tree : tree[n] = NodeState(n)
107\* H3: a failure shows until its source reports otherwise.
108OnlySourceClears == \A s \in Sources : (health[s] = "healthy") => ~reported[s]
109\* H1 and H5: after Top's first ready, health never moves its gate or its rows.
110GateIgnoresHealth == [][topStatus = "ready" => (topStatus' = "ready" /\ topRows')]_vars
111\* H2: the gate's error is explained by a node in the tree.
112GateExplained == topStatus = "error" => \E n \in DOMAIN tree : tree[n] = "error"
113=============================================================================