|
| 1 | +---- MODULE AgentSupervisor ---- |
| 2 | +\* TLA+ specification for Terraphim Agent Supervisor |
| 3 | +\* Models OTP-style supervision with OneForOne, OneForAll, RestForOne strategies |
| 4 | +\* |
| 5 | +\* Validates: |
| 6 | +\* Safety: restart count never exceeds max_restarts within time_window |
| 7 | +\* Safety: RestForOne only restarts agents started after the failed one |
| 8 | +\* Liveness: a failed agent is eventually restarted or supervisor stops |
| 9 | +\* Safety: no agent is in both Running and Restarting state simultaneously |
| 10 | + |
| 11 | +EXTENDS Integers, Sequences, FiniteSets, TLC |
| 12 | + |
| 13 | +CONSTANTS |
| 14 | + Agents, \* Set of agent PIDs (e.g., {"a1", "a2", "a3"}) |
| 15 | + MaxRestarts, \* Maximum restarts allowed in time window |
| 16 | + MaxTime, \* Bounded time horizon for model checking |
| 17 | + Strategy \* "OneForOne" | "OneForAll" | "RestForOne" |
| 18 | + |
| 19 | +VARIABLES |
| 20 | + agentStatus, \* Function: Agent -> {"Starting","Running","Stopped","Failed","Restarting"} |
| 21 | + restartCount, \* Function: Agent -> Nat (restarts in current window) |
| 22 | + supervisorStatus, \* "Running" | "Stopped" | "Failed" |
| 23 | + startOrder, \* Sequence of agents in start order (for RestForOne) |
| 24 | + clock \* Abstract time counter |
| 25 | + |
| 26 | +vars == <<agentStatus, restartCount, supervisorStatus, startOrder, clock>> |
| 27 | + |
| 28 | +\* Type invariant |
| 29 | +TypeOK == |
| 30 | + /\ agentStatus \in [Agents -> {"Starting", "Running", "Stopped", "Failed", "Restarting"}] |
| 31 | + /\ restartCount \in [Agents -> 0..MaxRestarts+1] |
| 32 | + /\ supervisorStatus \in {"Running", "Stopped", "Failed"} |
| 33 | + /\ clock \in 0..MaxTime |
| 34 | + |
| 35 | +\* ------ Initial State ------ |
| 36 | + |
| 37 | +Init == |
| 38 | + /\ agentStatus = [a \in Agents |-> "Running"] |
| 39 | + /\ restartCount = [a \in Agents |-> 0] |
| 40 | + /\ supervisorStatus = "Running" |
| 41 | + /\ startOrder = SetToSeq(Agents) \* Arbitrary but fixed ordering |
| 42 | + /\ clock = 0 |
| 43 | + |
| 44 | +\* ------ Helper: agents started after a given agent in start order ------ |
| 45 | + |
| 46 | +StartedAfter(failedAgent) == |
| 47 | + LET idx == CHOOSE i \in 1..Len(startOrder) : startOrder[i] = failedAgent |
| 48 | + IN {startOrder[j] : j \in {k \in idx+1..Len(startOrder) : TRUE}} |
| 49 | + |
| 50 | +\* Convert set to sequence (deterministic for model checking) |
| 51 | +SetToSeq(S) == |
| 52 | + CHOOSE seq \in [1..Cardinality(S) -> S] : |
| 53 | + \A i, j \in 1..Cardinality(S) : i /= j => seq[i] /= seq[j] |
| 54 | + |
| 55 | +\* ------ Actions ------ |
| 56 | + |
| 57 | +\* An agent crashes (transitions from Running to Failed) |
| 58 | +AgentFails(agent) == |
| 59 | + /\ supervisorStatus = "Running" |
| 60 | + /\ agentStatus[agent] = "Running" |
| 61 | + /\ agentStatus' = [agentStatus EXCEPT ![agent] = "Failed"] |
| 62 | + /\ UNCHANGED <<restartCount, supervisorStatus, startOrder, clock>> |
| 63 | + |
| 64 | +\* Supervisor restarts agent(s) according to strategy |
| 65 | +\* OneForOne: restart only the failed agent |
| 66 | +RestartOneForOne(agent) == |
| 67 | + /\ Strategy = "OneForOne" |
| 68 | + /\ supervisorStatus = "Running" |
| 69 | + /\ agentStatus[agent] = "Failed" |
| 70 | + /\ restartCount[agent] < MaxRestarts |
| 71 | + /\ agentStatus' = [agentStatus EXCEPT ![agent] = "Restarting"] |
| 72 | + /\ restartCount' = [restartCount EXCEPT ![agent] = @ + 1] |
| 73 | + /\ UNCHANGED <<supervisorStatus, startOrder, clock>> |
| 74 | + |
| 75 | +\* OneForAll: restart all agents when one fails |
| 76 | +RestartOneForAll(agent) == |
| 77 | + /\ Strategy = "OneForAll" |
| 78 | + /\ supervisorStatus = "Running" |
| 79 | + /\ agentStatus[agent] = "Failed" |
| 80 | + /\ restartCount[agent] < MaxRestarts |
| 81 | + /\ agentStatus' = [a \in Agents |-> |
| 82 | + IF a = agent THEN "Restarting" |
| 83 | + ELSE IF agentStatus[a] = "Running" THEN "Restarting" |
| 84 | + ELSE agentStatus[a]] |
| 85 | + /\ restartCount' = [restartCount EXCEPT ![agent] = @ + 1] |
| 86 | + /\ UNCHANGED <<supervisorStatus, startOrder, clock>> |
| 87 | + |
| 88 | +\* RestForOne: restart failed agent + all agents started after it |
| 89 | +RestartRestForOne(agent) == |
| 90 | + /\ Strategy = "RestForOne" |
| 91 | + /\ supervisorStatus = "Running" |
| 92 | + /\ agentStatus[agent] = "Failed" |
| 93 | + /\ restartCount[agent] < MaxRestarts |
| 94 | + /\ LET toRestart == StartedAfter(agent) \union {agent} |
| 95 | + IN agentStatus' = [a \in Agents |-> |
| 96 | + IF a \in toRestart THEN "Restarting" |
| 97 | + ELSE agentStatus[a]] |
| 98 | + /\ restartCount' = [restartCount EXCEPT ![agent] = @ + 1] |
| 99 | + /\ UNCHANGED <<supervisorStatus, startOrder, clock>> |
| 100 | + |
| 101 | +\* Restarting agent becomes Running |
| 102 | +AgentRestarted(agent) == |
| 103 | + /\ agentStatus[agent] = "Restarting" |
| 104 | + /\ agentStatus' = [agentStatus EXCEPT ![agent] = "Running"] |
| 105 | + /\ UNCHANGED <<restartCount, supervisorStatus, startOrder, clock>> |
| 106 | + |
| 107 | +\* Supervisor gives up on agent that exceeded restart limit |
| 108 | +SupervisorEscalates(agent) == |
| 109 | + /\ supervisorStatus = "Running" |
| 110 | + /\ agentStatus[agent] = "Failed" |
| 111 | + /\ restartCount[agent] >= MaxRestarts |
| 112 | + /\ supervisorStatus' = "Failed" |
| 113 | + /\ UNCHANGED <<agentStatus, restartCount, startOrder, clock>> |
| 114 | + |
| 115 | +\* Time advances |
| 116 | +Tick == |
| 117 | + /\ clock < MaxTime |
| 118 | + /\ clock' = clock + 1 |
| 119 | + /\ UNCHANGED <<agentStatus, restartCount, supervisorStatus, startOrder>> |
| 120 | + |
| 121 | +\* ------ Next State Relation ------ |
| 122 | + |
| 123 | +Next == |
| 124 | + \/ \E a \in Agents : AgentFails(a) |
| 125 | + \/ \E a \in Agents : RestartOneForOne(a) |
| 126 | + \/ \E a \in Agents : RestartOneForAll(a) |
| 127 | + \/ \E a \in Agents : RestartRestForOne(a) |
| 128 | + \/ \E a \in Agents : AgentRestarted(a) |
| 129 | + \/ \E a \in Agents : SupervisorEscalates(a) |
| 130 | + \/ Tick |
| 131 | + |
| 132 | +\* ------ Safety Properties ------ |
| 133 | + |
| 134 | +\* No agent exceeds max restart count |
| 135 | +RestartBoundSafety == |
| 136 | + \A a \in Agents : restartCount[a] <= MaxRestarts |
| 137 | + |
| 138 | +\* No agent is simultaneously in two active states |
| 139 | +NoSimultaneousStates == |
| 140 | + \A a \in Agents : |
| 141 | + ~(agentStatus[a] = "Running" /\ agentStatus[a] = "Restarting") |
| 142 | + |
| 143 | +\* RestForOne only restarts agents started AFTER the failed one |
| 144 | +\* (This is checked structurally in RestartRestForOne action definition) |
| 145 | + |
| 146 | +\* If supervisor is Failed, no more restarts happen |
| 147 | +FailedSupervisorNoRestarts == |
| 148 | + supervisorStatus = "Failed" => |
| 149 | + \A a \in Agents : agentStatus'[a] /= "Restarting" |
| 150 | + |
| 151 | +\* ------ Liveness Properties ------ |
| 152 | + |
| 153 | +\* A failed agent is eventually restarted or supervisor escalates |
| 154 | +EventualRecovery == |
| 155 | + \A a \in Agents : |
| 156 | + agentStatus[a] = "Failed" ~> |
| 157 | + (agentStatus[a] = "Running" \/ supervisorStatus = "Failed") |
| 158 | + |
| 159 | +\* ------ Specification ------ |
| 160 | + |
| 161 | +Spec == Init /\ [][Next]_vars /\ WF_vars(Next) |
| 162 | + |
| 163 | +==== |
0 commit comments