shikibu 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +121 -0
  3. data/lib/shikibu/activity.rb +84 -48
  4. data/lib/shikibu/actors.rb +129 -0
  5. data/lib/shikibu/app.rb +45 -19
  6. data/lib/shikibu/blobs.rb +42 -0
  7. data/lib/shikibu/channels.rb +240 -64
  8. data/lib/shikibu/constants.rb +8 -0
  9. data/lib/shikibu/context.rb +184 -14
  10. data/lib/shikibu/errors.rb +56 -0
  11. data/lib/shikibu/fs.rb +93 -0
  12. data/lib/shikibu/integrations/sidekiq.rb +9 -13
  13. data/lib/shikibu/middleware/rack_app.rb +17 -1
  14. data/lib/shikibu/replay.rb +248 -100
  15. data/lib/shikibu/storage/actor_ops.rb +331 -0
  16. data/lib/shikibu/storage/channel_ops.rb +421 -0
  17. data/lib/shikibu/storage/cursor_ops.rb +37 -0
  18. data/lib/shikibu/storage/sequel_storage.rb +400 -324
  19. data/lib/shikibu/storage/sqlite_ops.rb +58 -0
  20. data/lib/shikibu/supervisor.rb +279 -0
  21. data/lib/shikibu/tasks.rb +193 -0
  22. data/lib/shikibu/typed_payload.rb +189 -0
  23. data/lib/shikibu/version.rb +1 -1
  24. data/lib/shikibu/worker.rb +155 -73
  25. data/lib/shikibu/workflow.rb +203 -50
  26. data/lib/shikibu.rb +5 -0
  27. data/schema/SEMANTICS.md +351 -0
  28. data/schema/db/migrations/mysql/20261123000000_outbox_claimed_at.sql +5 -0
  29. data/schema/db/migrations/mysql/20261201000000_actor_registry.sql +24 -0
  30. data/schema/db/migrations/mysql/20261202000000_actor_capabilities.sql +15 -0
  31. data/schema/db/migrations/mysql/20261203000000_payload_blobs.sql +11 -0
  32. data/schema/db/migrations/mysql/20261204000000_actor_fs.sql +13 -0
  33. data/schema/db/migrations/mysql/20261205000000_actor_tasks.sql +15 -0
  34. data/schema/db/migrations/mysql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  35. data/schema/db/migrations/mysql/20261207000000_add_sandboxed.sql +5 -0
  36. data/schema/db/migrations/mysql/20261208000000_widen_actor_owner_columns.sql +15 -0
  37. data/schema/db/migrations/postgresql/20261123000000_outbox_claimed_at.sql +5 -0
  38. data/schema/db/migrations/postgresql/20261201000000_actor_registry.sql +23 -0
  39. data/schema/db/migrations/postgresql/20261202000000_actor_capabilities.sql +15 -0
  40. data/schema/db/migrations/postgresql/20261203000000_payload_blobs.sql +11 -0
  41. data/schema/db/migrations/postgresql/20261204000000_actor_fs.sql +13 -0
  42. data/schema/db/migrations/postgresql/20261205000000_actor_tasks.sql +15 -0
  43. data/schema/db/migrations/postgresql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  44. data/schema/db/migrations/postgresql/20261207000000_add_sandboxed.sql +5 -0
  45. data/schema/db/migrations/postgresql/20261208000000_widen_actor_owner_columns.sql +7 -0
  46. data/schema/db/migrations/sqlite/20261123000000_outbox_claimed_at.sql +5 -0
  47. data/schema/db/migrations/sqlite/20261201000000_actor_registry.sql +23 -0
  48. data/schema/db/migrations/sqlite/20261202000000_actor_capabilities.sql +15 -0
  49. data/schema/db/migrations/sqlite/20261203000000_payload_blobs.sql +11 -0
  50. data/schema/db/migrations/sqlite/20261204000000_actor_fs.sql +13 -0
  51. data/schema/db/migrations/sqlite/20261205000000_actor_tasks.sql +15 -0
  52. data/schema/db/migrations/sqlite/20261206000000_actor_tasks_claimed_at.sql +7 -0
  53. data/schema/db/migrations/sqlite/20261207000000_add_sandboxed.sql +5 -0
  54. data/schema/db/migrations/sqlite/20261208000000_widen_actor_owner_columns.sql +7 -0
  55. data/schema/docs/column-values.md +10 -0
  56. data/schema/spec/ActorFS.tla +117 -0
  57. data/schema/spec/ActorFS_lww_monotonic.cfg +7 -0
  58. data/schema/spec/ActorFS_new.cfg +8 -0
  59. data/schema/spec/ActorFS_old.cfg +7 -0
  60. data/schema/spec/CompetingMessageClaim.tla +127 -0
  61. data/schema/spec/CompetingMessageClaimSplit.tla +132 -0
  62. data/schema/spec/CompetingMessageClaimSplit_atomic_lost.cfg +7 -0
  63. data/schema/spec/CompetingMessageClaimSplit_atomic_once.cfg +7 -0
  64. data/schema/spec/CompetingMessageClaimSplit_fixed.cfg +7 -0
  65. data/schema/spec/CompetingMessageClaimSplit_split_lost.cfg +7 -0
  66. data/schema/spec/CompetingMessageClaimSplit_split_once.cfg +7 -0
  67. data/schema/spec/CompetingMessageClaimSplit_waitonly_lost.cfg +7 -0
  68. data/schema/spec/CompetingMessageClaimSplit_waitonly_once.cfg +7 -0
  69. data/schema/spec/CompetingMessageClaim_new.cfg +10 -0
  70. data/schema/spec/CompetingMessageClaim_old.cfg +11 -0
  71. data/schema/spec/EpochFencing.tla +87 -0
  72. data/schema/spec/EpochFencingRuns.tla +107 -0
  73. data/schema/spec/EpochFencingRuns_epoch.cfg +6 -0
  74. data/schema/spec/EpochFencingRuns_none.cfg +6 -0
  75. data/schema/spec/EpochFencingRuns_witness.cfg +6 -0
  76. data/schema/spec/EpochFencingRuns_worker.cfg +6 -0
  77. data/schema/spec/EpochFencingRuns_worker1.cfg +6 -0
  78. data/schema/spec/EpochFencing_new.cfg +8 -0
  79. data/schema/spec/EpochFencing_old.cfg +9 -0
  80. data/schema/spec/InstanceClaim.tla +196 -0
  81. data/schema/spec/InstanceClaimVerdict.tla +116 -0
  82. data/schema/spec/InstanceClaimVerdict_changedrows.cfg +6 -0
  83. data/schema/spec/InstanceClaimVerdict_changedrows_live.cfg +6 -0
  84. data/schema/spec/InstanceClaimVerdict_foundrows.cfg +6 -0
  85. data/schema/spec/InstanceClaimVerdict_foundrows_live.cfg +6 -0
  86. data/schema/spec/InstanceClaim_atomic_unfenced.cfg +9 -0
  87. data/schema/spec/InstanceClaim_live.cfg +9 -0
  88. data/schema/spec/InstanceClaim_new.cfg +9 -0
  89. data/schema/spec/InstanceClaim_old.cfg +11 -0
  90. data/schema/spec/InstanceClaim_racy_fenced.cfg +9 -0
  91. data/schema/spec/README.md +97 -0
  92. data/schema/spec/SupervisorRestart.tla +108 -0
  93. data/schema/spec/SupervisorRestartCAS.tla +84 -0
  94. data/schema/spec/SupervisorRestartCAS_nonsticky.cfg +6 -0
  95. data/schema/spec/SupervisorRestartCAS_snapshot.cfg +6 -0
  96. data/schema/spec/SupervisorRestartCAS_sticky.cfg +6 -0
  97. data/schema/spec/SupervisorRestart_racy.cfg +7 -0
  98. data/schema/spec/SupervisorRestart_strict.cfg +7 -0
  99. data/schema/spec/TaskClaim.tla +92 -0
  100. data/schema/spec/TaskClaimReadBack.tla +84 -0
  101. data/schema/spec/TaskClaimReadBack_byid.cfg +6 -0
  102. data/schema/spec/TaskClaimReadBack_order.cfg +6 -0
  103. data/schema/spec/TaskClaim_new.cfg +8 -0
  104. data/schema/spec/TaskClaim_old.cfg +9 -0
  105. data/schema/spec/TaskReclaim.tla +110 -0
  106. data/schema/spec/TaskReclaimAttempts.tla +73 -0
  107. data/schema/spec/TaskReclaimAttempts_attempt.cfg +6 -0
  108. data/schema/spec/TaskReclaimAttempts_claimant.cfg +6 -0
  109. data/schema/spec/TaskReclaim_new.cfg +8 -0
  110. data/schema/spec/TaskReclaim_old.cfg +8 -0
  111. data/schema/spec/WaitRegistration.tla +170 -0
  112. data/schema/spec/WaitRegistrationSub.tla +133 -0
  113. data/schema/spec/WaitRegistrationSub_fixed.cfg +6 -0
  114. data/schema/spec/WaitRegistrationSub_fixed_guarded.cfg +6 -0
  115. data/schema/spec/WaitRegistrationSub_guardonly_reg.cfg +6 -0
  116. data/schema/spec/WaitRegistrationSub_guardonly_res.cfg +6 -0
  117. data/schema/spec/WaitRegistrationSub_head_reg.cfg +6 -0
  118. data/schema/spec/WaitRegistrationSub_head_res.cfg +6 -0
  119. data/schema/spec/WaitRegistrationSub_noclear_reg.cfg +6 -0
  120. data/schema/spec/WaitRegistrationSub_noclear_res.cfg +6 -0
  121. data/schema/spec/WaitRegistrationSub_unfencedreg_reg.cfg +6 -0
  122. data/schema/spec/WaitRegistrationSub_unfencedreg_res.cfg +6 -0
  123. data/schema/spec/WaitRegistration_new.cfg +10 -0
  124. data/schema/spec/WaitRegistration_old.cfg +11 -0
  125. data/schema/spec/WaitRegistration_residual.cfg +11 -0
  126. data/schema/spec/WaitRegistration_sweep.cfg +11 -0
  127. data/schema/spec/actorfs_check_test.go +165 -0
  128. data/schema/spec/claim_check_test.go +387 -0
  129. data/schema/spec/competing_claim_test.go +194 -0
  130. data/schema/spec/epoch_check_test.go +128 -0
  131. data/schema/spec/go.mod +3 -0
  132. data/schema/spec/supervisorrestart_check_test.go +131 -0
  133. data/schema/spec/taskclaim_check_test.go +135 -0
  134. data/schema/spec/taskreclaim_check_test.go +125 -0
  135. data/schema/spec/tlc-check.sh +45 -0
  136. data/schema/spec/tlc-matrix.txt +68 -0
  137. data/schema/spec/wait_check_test.go +256 -0
  138. metadata +136 -2
@@ -0,0 +1,107 @@
1
+ ---- MODULE EpochFencingRuns ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Proposed replacement for EpochFencing.tla (review artifact). *)
6
+ (* *)
7
+ (* EpochFencing.tla keeps ONE heldEpoch per worker, so when a worker *)
8
+ (* re-acquires, its old credential is overwritten and the case epochs *)
9
+ (* exist for -- an old run of the SAME worker still executing after the *)
10
+ (* worker re-claimed the row -- is unrepresentable (TLC: fencing on *)
11
+ (* worker id alone already satisfies NoStaleWrite there). *)
12
+ (* *)
13
+ (* Here the unit of credential is a RUN: [w |-> worker, e |-> epoch]. A *)
14
+ (* worker may start a new run while an older run of its own is still *)
15
+ (* executing (romancy: a goroutine blocked in an activity while the same *)
16
+ (* process's resumption loop re-claims the expired row). *)
17
+ (* *)
18
+ (* FenceMode = "none" : unfenced writes *)
19
+ (* FenceMode = "worker" : WHERE locked_by = <workerID> (pre-epoch) *)
20
+ (* FenceMode = "epoch" : WHERE locked_by = <workerID#token> (romancy) *)
21
+ (* Lock tokens only need to be unique for equality fencing; romancy uses *)
22
+ (* 64 random bits, the model uses a counter. *)
23
+ (***************************************************************************)
24
+
25
+ CONSTANTS Workers, FenceMode, MaxEpoch
26
+
27
+ VARIABLES epoch, \* last minted token
28
+ lockW, \* worker part of locked_by ("none" when free)
29
+ lockE, \* token part of locked_by (0 when free)
30
+ expired, \* lease expired (abstract time)
31
+ runs, \* runs still executing
32
+ wrote, \* runs that had one write accepted
33
+ staleWrite \* a write by a non-current run was accepted
34
+
35
+ vars == <<epoch, lockW, lockE, expired, runs, wrote, staleWrite>>
36
+
37
+ None == "none"
38
+ Run == [w : Workers, e : 1..MaxEpoch]
39
+
40
+ Init ==
41
+ /\ epoch = 0
42
+ /\ lockW = None
43
+ /\ lockE = 0
44
+ /\ expired = FALSE
45
+ /\ runs = {}
46
+ /\ wrote = {}
47
+ /\ staleWrite = FALSE
48
+
49
+ \* Conditional claim (row free or lease expired) mints a fresh token.
50
+ Acquire(w) ==
51
+ /\ epoch < MaxEpoch
52
+ /\ lockW = None \/ expired
53
+ /\ epoch' = epoch + 1
54
+ /\ lockW' = w
55
+ /\ lockE' = epoch + 1
56
+ /\ expired' = FALSE
57
+ /\ runs' = runs \cup {[w |-> w, e |-> epoch + 1]}
58
+ /\ UNCHANGED <<wrote, staleWrite>>
59
+
60
+ Expire ==
61
+ /\ lockW # None
62
+ /\ ~expired
63
+ /\ expired' = TRUE
64
+ /\ UNCHANGED <<epoch, lockW, lockE, runs, wrote, staleWrite>>
65
+
66
+ Current(r) == lockW = r.w /\ lockE = r.e
67
+
68
+ FencePasses(r) ==
69
+ CASE FenceMode = "none" -> TRUE
70
+ [] FenceMode = "worker" -> lockW = r.w
71
+ [] FenceMode = "epoch" -> Current(r)
72
+
73
+ Write(r) ==
74
+ /\ r \notin wrote
75
+ /\ FencePasses(r)
76
+ /\ wrote' = wrote \cup {r}
77
+ /\ staleWrite' = (staleWrite \/ ~Current(r))
78
+ /\ UNCHANGED <<epoch, lockW, lockE, expired, runs>>
79
+
80
+ \* A run that is told it lost the lock (rejected write / failed refresh) stops.
81
+ Abort(r) ==
82
+ /\ ~FencePasses(r)
83
+ /\ runs' = runs \ {r}
84
+ /\ UNCHANGED <<epoch, lockW, lockE, expired, wrote, staleWrite>>
85
+
86
+ \* A run finishes; its release is conditional on the same fence.
87
+ Finish(r) ==
88
+ /\ runs' = runs \ {r}
89
+ /\ IF FencePasses(r)
90
+ THEN /\ lockW' = None /\ lockE' = 0 /\ expired' = FALSE
91
+ ELSE UNCHANGED <<lockW, lockE, expired>>
92
+ /\ UNCHANGED <<epoch, wrote, staleWrite>>
93
+
94
+ Next ==
95
+ \/ \E w \in Workers : Acquire(w)
96
+ \/ Expire
97
+ \/ \E r \in runs : Write(r) \/ Abort(r) \/ Finish(r)
98
+
99
+ Spec == Init /\ [][Next]_vars
100
+
101
+ TypeOK == runs \subseteq Run /\ wrote \subseteq Run
102
+
103
+ NoStaleWrite == ~staleWrite
104
+
105
+ \* Witness probe: two live runs of the same worker are reachable.
106
+ NoTwoRunsSameWorker == \A r1, r2 \in runs : r1.w = r2.w => r1 = r2
107
+ ====
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ FenceMode = "epoch"
4
+ MaxEpoch = 3
5
+ SPECIFICATION Spec
6
+ INVARIANTS TypeOK NoStaleWrite
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ FenceMode = "none"
4
+ MaxEpoch = 3
5
+ SPECIFICATION Spec
6
+ INVARIANTS TypeOK NoStaleWrite
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ FenceMode = "epoch"
4
+ MaxEpoch = 3
5
+ SPECIFICATION Spec
6
+ INVARIANTS NoTwoRunsSameWorker
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ FenceMode = "worker"
4
+ MaxEpoch = 3
5
+ SPECIFICATION Spec
6
+ INVARIANTS TypeOK NoStaleWrite
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1}
3
+ FenceMode = "worker"
4
+ MaxEpoch = 3
5
+ SPECIFICATION Spec
6
+ INVARIANTS TypeOK NoStaleWrite
@@ -0,0 +1,8 @@
1
+ \* Fenced writes: stale-epoch writes are always rejected.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ Fenced = TRUE
6
+ MaxEpoch = 4
7
+ SPECIFICATION Spec
8
+ INVARIANTS NoStaleWrite
@@ -0,0 +1,9 @@
1
+ \* Unfenced writes: a stale holder (including the same worker before its
2
+ \* reacquisition landed) can still write.
3
+ \* Expected TLC result: NoStaleWrite violated.
4
+ CONSTANTS
5
+ Workers = {w1, w2}
6
+ Fenced = FALSE
7
+ MaxEpoch = 4
8
+ SPECIFICATION Spec
9
+ INVARIANTS NoStaleWrite
@@ -0,0 +1,196 @@
1
+ ----------------------------- MODULE InstanceClaim -----------------------------
2
+ \* Model of romancy's workflow-instance claim protocol.
3
+ \*
4
+ \* Workers acquire a lease on a workflow_instances row (locked_by /
5
+ \* lock_expires_at), run the workflow while holding it, and append history /
6
+ \* update status / write output. Both designs live in one model, selected by
7
+ \* constants:
8
+ \*
9
+ \* AtomicClaim = FALSE, Fenced = FALSE the pre-fix design:
10
+ \* TryAcquireLock read the row, decided it was free, then issued a
11
+ \* separate UPDATE -- two workers can both pass the check and both
12
+ \* believe they hold the lock. Writes were unconditional, so a worker
13
+ \* that lost its lease could still append history over the new owner.
14
+ \*
15
+ \* AtomicClaim = TRUE, Fenced = TRUE the current design:
16
+ \* TryAcquireLock is a single conditional UPDATE ... WHERE the row is
17
+ \* still claimable (SQLite/PostgreSQL/MySQL all use RowsAffected as the
18
+ \* verdict). A heartbeat renews the lease while running; every history /
19
+ \* status / output write is conditioned on `locked_by = worker`, so a
20
+ \* stale worker's writes no longer land.
21
+ \*
22
+ \* Abstract time: the lease is either alive or expired; ExpireLease is a
23
+ \* nondeterministic tick that can fire while the holder still runs.
24
+ \*
25
+ \* Check with TLC (see spec/*.cfg; -deadlock because workers that finish or
26
+ \* abort legitimately go idle):
27
+ \* java -jar tla2tools.jar -deadlock -config InstanceClaim_old.cfg InstanceClaim.tla
28
+ \* -> verified: invariant violation, a 6-step counterexample where both
29
+ \* workers pass the stale read, both believe they hold the lock, and the
30
+ \* non-holder's write lands (claim race in the pre-fix design)
31
+ \* java -jar tla2tools.jar -deadlock -config InstanceClaim_new.cfg InstanceClaim.tla
32
+ \* -> verified: no errors over all 81 distinct states
33
+ \* java -jar tla2tools.jar -deadlock -config InstanceClaim_live.cfg InstanceClaim.tla
34
+ \* -> verified: the liveness property holds
35
+ \* The same state machine is model-checked from Go in claim_check_test.go
36
+ \* so `go test ./spec` verifies the design without a Java toolchain.
37
+
38
+ EXTENDS Naturals
39
+
40
+ CONSTANTS Workers, \* set of worker identities
41
+ NULL, \* no-holder marker (model value, distinct from Workers)
42
+ AtomicClaim, \* BOOLEAN: single conditional-UPDATE claim?
43
+ Fenced \* BOOLEAN: writes conditioned on `locked_by = w`?
44
+
45
+ VARIABLES lockHolder, \* row's locked_by: Workers \cup {NULL}
46
+ leaseExpired, \* row's lease state: BOOLEAN
47
+ believes, \* [Workers -> BOOLEAN] worker thinks it is running
48
+ pc, \* [Workers -> {"idle","read","running","aborted","done"}]
49
+ sawFree, \* [Workers -> BOOLEAN] stale read result (old design)
50
+ legit, \* [Workers -> BOOLEAN] the claim itself was legitimate
51
+ wrote, \* [Workers -> BOOLEAN] the DB accepted a write of w's
52
+ staleWrite \* BOOLEAN: a non-holder's write was accepted
53
+
54
+ vars == <<lockHolder, leaseExpired, believes, pc, sawFree, legit, wrote, staleWrite>>
55
+
56
+ \* Is the row claimable by w right now?
57
+ FreeFor(w) == lockHolder = NULL \/ leaseExpired \/ lockHolder = w
58
+
59
+ Init ==
60
+ /\ lockHolder = NULL
61
+ /\ leaseExpired = FALSE
62
+ /\ believes = [w \in Workers |-> FALSE]
63
+ /\ pc = [w \in Workers |-> "idle"]
64
+ /\ sawFree = [w \in Workers |-> FALSE]
65
+ /\ legit = [w \in Workers |-> TRUE]
66
+ /\ wrote = [w \in Workers |-> FALSE]
67
+ /\ staleWrite = FALSE
68
+
69
+ \* ---- claim ----------------------------------------------------------
70
+
71
+ \* Fixed design: check and claim are one database operation.
72
+ \* The UPDATE either matches (row was free) and installs w, or matches no row
73
+ \* and w retries -- exactly RowsAffected > 0 in the Go code.
74
+ TryAcquire(w) ==
75
+ /\ AtomicClaim
76
+ /\ pc[w] = "idle"
77
+ /\ IF FreeFor(w)
78
+ THEN /\ lockHolder' = w
79
+ /\ leaseExpired' = FALSE
80
+ /\ believes' = [believes EXCEPT ![w] = TRUE]
81
+ /\ legit' = [legit EXCEPT ![w] = TRUE]
82
+ /\ pc' = [pc EXCEPT ![w] = "running"]
83
+ ELSE /\ pc' = [pc EXCEPT ![w] = "idle"] \* busy; stays eligible
84
+ /\ UNCHANGED <<lockHolder, leaseExpired, believes, legit>>
85
+ /\ UNCHANGED <<sawFree, wrote, staleWrite>>
86
+ \* legit is set TRUE only because FreeFor(w) held at that instant.
87
+
88
+ \* Old design step 1: read the row and remember whether it looked free.
89
+ ReadLock(w) ==
90
+ /\ ~AtomicClaim
91
+ /\ pc[w] = "idle"
92
+ /\ pc' = [pc EXCEPT ![w] = "read"]
93
+ /\ sawFree' = [sawFree EXCEPT ![w] = FreeFor(w)]
94
+ /\ UNCHANGED <<lockHolder, leaseExpired, believes, legit, wrote, staleWrite>>
95
+
96
+ \* Old design step 2: act on the stale read.
97
+ \* Between ReadLock and Claim another worker may have installed itself; the
98
+ \* UPDATE still fires, overwriting locked_by. `legit` records what a
99
+ \* conditional claim would have seen -- FALSE marks a stolen live lease.
100
+ Claim(w) ==
101
+ /\ ~AtomicClaim
102
+ /\ pc[w] = "read"
103
+ /\ IF sawFree[w]
104
+ THEN /\ legit' = [legit EXCEPT ![w] = FreeFor(w)]
105
+ /\ lockHolder' = w
106
+ /\ leaseExpired' = FALSE
107
+ /\ believes' = [believes EXCEPT ![w] = TRUE]
108
+ /\ pc' = [pc EXCEPT ![w] = "running"]
109
+ ELSE /\ pc' = [pc EXCEPT ![w] = "idle"]
110
+ /\ UNCHANGED <<lockHolder, leaseExpired, believes, legit>>
111
+ /\ sawFree' = [sawFree EXCEPT ![w] = FALSE]
112
+ /\ UNCHANGED <<wrote, staleWrite>>
113
+
114
+ \* ---- run ------------------------------------------------------------
115
+
116
+ \* Abstract tick: any live lease can expire (the 300 s lease has no
117
+ \* relationship to how long the activity runs). The holder keeps believing;
118
+ \* only the row changes.
119
+ ExpireLease ==
120
+ /\ lockHolder # NULL
121
+ /\ ~leaseExpired
122
+ /\ leaseExpired' = TRUE
123
+ /\ UNCHANGED <<lockHolder, believes, pc, sawFree, legit, wrote, staleWrite>>
124
+
125
+ \* Heartbeat: the holder extends its lease (RefreshLock).
126
+ Renew(w) ==
127
+ /\ pc[w] = "running"
128
+ /\ lockHolder = w
129
+ /\ leaseExpired' = FALSE
130
+ /\ UNCHANGED <<lockHolder, believes, pc, sawFree, legit, wrote, staleWrite>>
131
+
132
+ \* A running worker appends history / updates status / writes output.
133
+ \* Fenced: the write is the conditional UPDATE ... WHERE locked_by = w, so it
134
+ \* is enabled only while w is the holder. Unfenced: always enabled, and a
135
+ \* write by a non-holder is recorded as the staleWrite violation.
136
+ Write(w) ==
137
+ /\ pc[w] = "running"
138
+ /\ believes[w]
139
+ /\ IF Fenced
140
+ THEN /\ lockHolder = w
141
+ /\ wrote' = [wrote EXCEPT ![w] = TRUE]
142
+ /\ staleWrite' = staleWrite
143
+ ELSE /\ wrote' = [wrote EXCEPT ![w] = TRUE]
144
+ /\ staleWrite' = (staleWrite \/ lockHolder # w)
145
+ /\ UNCHANGED <<lockHolder, leaseExpired, believes, pc, sawFree, legit>>
146
+
147
+ \* The heartbeat or a rejected write (ErrLockNotHeld) aborts the run.
148
+ Abort(w) ==
149
+ /\ pc[w] = "running"
150
+ /\ lockHolder # w
151
+ /\ believes' = [believes EXCEPT ![w] = FALSE]
152
+ /\ pc' = [pc EXCEPT ![w] = "aborted"]
153
+ /\ UNCHANGED <<lockHolder, leaseExpired, sawFree, legit, wrote, staleWrite>>
154
+
155
+ \* The run finishes and releases conditionally (ReleaseLock only clears the
156
+ \* row while w is still the holder).
157
+ Finish(w) ==
158
+ /\ pc[w] = "running"
159
+ /\ believes' = [believes EXCEPT ![w] = FALSE]
160
+ /\ pc' = [pc EXCEPT ![w] = "done"]
161
+ /\ IF lockHolder = w
162
+ THEN /\ lockHolder' = NULL
163
+ /\ leaseExpired' = FALSE
164
+ ELSE UNCHANGED <<lockHolder, leaseExpired>>
165
+ /\ UNCHANGED <<sawFree, legit, wrote, staleWrite>>
166
+
167
+ Next ==
168
+ \/ \E w \in Workers :
169
+ \/ TryAcquire(w) \/ ReadLock(w) \/ Claim(w)
170
+ \/ Renew(w) \/ Write(w) \/ Abort(w) \/ Finish(w)
171
+ \/ ExpireLease
172
+
173
+ Spec == Init /\ [][Next]_vars
174
+
175
+ \* ---- properties ------------------------------------------------------
176
+
177
+ \* SAFETY 1: the DB never accepted a write from a worker that did not hold
178
+ \* the row at write time. Violated by the old design both via the claim race
179
+ \* (two believers, only one is locked_by) and via lease expiry without
180
+ \* fencing (stale holder writes after a takeover).
181
+ NoStaleWrite == ~staleWrite
182
+
183
+ \* SAFETY 2: no worker's accepted write follows an illegitimate claim -- the
184
+ \* non-atomic claim can install a worker over a live lease; the conditional
185
+ \* UPDATE cannot.
186
+ LegitimateWriters == \A w \in Workers : wrote[w] => legit[w]
187
+
188
+ \* LIVENESS: a resumable instance eventually finishes on some worker.
189
+ \* Checked under per-worker fairness on attempting and finishing.
190
+ Attempt(w) == TryAcquire(w) \/ ReadLock(w) \/ Claim(w)
191
+ FairSpec == Spec
192
+ /\ \A w \in Workers : WF_vars(Attempt(w)) /\ WF_vars(Finish(w))
193
+ SomeDone == \E w \in Workers : pc[w] = "done"
194
+ Liveness == <>(SomeDone)
195
+
196
+ =============================================================================
@@ -0,0 +1,116 @@
1
+ ---- MODULE InstanceClaimVerdict ----
2
+ EXTENDS Naturals
3
+
4
+ (***************************************************************************)
5
+ (* Review extension of InstanceClaim.tla (current design only). *)
6
+ (* *)
7
+ (* InstanceClaim.tla equates "the conditional UPDATE succeeded" with "its *)
8
+ (* WHERE clause matched" and lets each worker claim at most once, so the *)
9
+ (* same-holder rewrite path never executes (TLC: NoReentrantAcquire holds). *)
10
+ (* romancy HEAD does exercise it: ClaimResumableWorkflows installs the *)
11
+ (* batch token, then ResumeWorkflowWithLockID calls TryAcquireLock with *)
12
+ (* the SAME token (the `locked_by = ?` arm). RefreshLock rewrites the *)
13
+ (* lease likewise. *)
14
+ (* *)
15
+ (* ChangedRows = TRUE : the driver reports CHANGED rows (MySQL without *)
16
+ (* clientFoundRows). A same-holder rewrite within one clock tick of *)
17
+ (* the column resolution (TIMESTAMP = 1 s) changes nothing and *)
18
+ (* reports 0. *)
19
+ (* ChangedRows = FALSE: matched rows (SQLite, PostgreSQL, MySQL with *)
20
+ (* clientFoundRows / CLIENT_FOUND_ROWS). *)
21
+ (***************************************************************************)
22
+
23
+ CONSTANTS Workers, NULL, ChangedRows
24
+
25
+ VARIABLES lockHolder, leaseExpired, pc, sameTick, falseLoss
26
+
27
+ vars == <<lockHolder, leaseExpired, pc, sameTick, falseLoss>>
28
+
29
+ FreeFor(w) == lockHolder = NULL \/ leaseExpired \/ lockHolder = w
30
+
31
+ \* RowsAffected verdict of a conditional UPDATE whose WHERE `matched`; a
32
+ \* same-holder rewrite changes column values only if the clock ticked.
33
+ Verdict(matched, sameHolder) ==
34
+ IF ChangedRows /\ sameHolder /\ sameTick THEN FALSE ELSE matched
35
+
36
+ Init ==
37
+ /\ lockHolder = NULL
38
+ /\ leaseExpired = FALSE
39
+ /\ pc = [w \in Workers |-> "idle"]
40
+ /\ sameTick = FALSE
41
+ /\ falseLoss = FALSE
42
+
43
+ \* ClaimResumableWorkflows (FOR UPDATE SKIP LOCKED + UPDATE).
44
+ BatchClaim(w) ==
45
+ /\ pc[w] = "idle"
46
+ /\ lockHolder = NULL \/ leaseExpired
47
+ /\ lockHolder' = w
48
+ /\ leaseExpired' = FALSE
49
+ /\ sameTick' = TRUE
50
+ /\ pc' = [pc EXCEPT ![w] = "claimed"]
51
+ /\ UNCHANGED falseLoss
52
+
53
+ \* ResumeWorkflowWithLockID -> TryAcquireLock(same token).
54
+ SelfReclaim(w) ==
55
+ /\ pc[w] = "claimed"
56
+ /\ LET matched == FreeFor(w)
57
+ ok == Verdict(matched, lockHolder = w)
58
+ IN IF ok
59
+ THEN /\ pc' = [pc EXCEPT ![w] = "running"]
60
+ /\ lockHolder' = w /\ leaseExpired' = FALSE /\ sameTick' = TRUE
61
+ /\ UNCHANGED falseLoss
62
+ ELSE \* "failed to acquire lock": the run is skipped; the row
63
+ \* stays locked by w's token until the lease expires.
64
+ /\ pc' = [pc EXCEPT ![w] = "idle"]
65
+ /\ falseLoss' = (falseLoss \/ lockHolder = w)
66
+ /\ UNCHANGED <<lockHolder, leaseExpired, sameTick>>
67
+
68
+ \* RefreshLock heartbeat.
69
+ Renew(w) ==
70
+ /\ pc[w] = "running"
71
+ /\ LET ok == Verdict(lockHolder = w, lockHolder = w) IN
72
+ IF ok
73
+ THEN /\ leaseExpired' = FALSE /\ sameTick' = TRUE
74
+ /\ UNCHANGED <<pc, lockHolder, falseLoss>>
75
+ ELSE \* ErrLockNotHeld -> run cancelled
76
+ /\ pc' = [pc EXCEPT ![w] = "aborted"]
77
+ /\ falseLoss' = (falseLoss \/ lockHolder = w)
78
+ /\ UNCHANGED <<lockHolder, leaseExpired, sameTick>>
79
+
80
+ Finish(w) ==
81
+ /\ pc[w] = "running"
82
+ /\ pc' = [pc EXCEPT ![w] = "done"]
83
+ /\ IF lockHolder = w
84
+ THEN lockHolder' = NULL /\ leaseExpired' = FALSE
85
+ ELSE UNCHANGED <<lockHolder, leaseExpired>>
86
+ /\ UNCHANGED <<sameTick, falseLoss>>
87
+
88
+ Tick ==
89
+ /\ sameTick
90
+ /\ sameTick' = FALSE
91
+ /\ UNCHANGED <<lockHolder, leaseExpired, pc, falseLoss>>
92
+
93
+ \* The self-reclaim follows the batch claim within milliseconds; a 300 s
94
+ \* lease cannot lapse in between.
95
+ ExpireLease ==
96
+ /\ lockHolder # NULL
97
+ /\ ~leaseExpired
98
+ /\ \A w \in Workers : pc[w] # "claimed"
99
+ /\ leaseExpired' = TRUE
100
+ /\ UNCHANGED <<lockHolder, pc, sameTick, falseLoss>>
101
+
102
+ Next ==
103
+ \/ \E w \in Workers : BatchClaim(w) \/ SelfReclaim(w) \/ Renew(w) \/ Finish(w)
104
+ \/ Tick \/ ExpireLease
105
+
106
+ Spec == Init /\ [][Next]_vars
107
+
108
+ \* A worker never concludes it lost a lock it still holds.
109
+ NoFalseLoss == ~falseLoss
110
+
111
+ \* Liveness: some run completes. Fair on every action, including time.
112
+ FairSpec == Spec /\ WF_vars(Tick) /\ WF_vars(ExpireLease)
113
+ /\ \A w \in Workers : WF_vars(BatchClaim(w)) /\ WF_vars(SelfReclaim(w)) /\ WF_vars(Finish(w))
114
+ SomeDone == \E w \in Workers : pc[w] = "done"
115
+ Liveness == <>SomeDone
116
+ ====
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ NULL = NULL
4
+ ChangedRows = TRUE
5
+ SPECIFICATION Spec
6
+ INVARIANTS NoFalseLoss
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ NULL = NULL
4
+ ChangedRows = TRUE
5
+ SPECIFICATION FairSpec
6
+ PROPERTY Liveness
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ NULL = NULL
4
+ ChangedRows = FALSE
5
+ SPECIFICATION Spec
6
+ INVARIANTS NoFalseLoss
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ NULL = NULL
4
+ ChangedRows = FALSE
5
+ SPECIFICATION FairSpec
6
+ PROPERTY Liveness
@@ -0,0 +1,9 @@
1
+ \* Atomic claim WITHOUT fencing: a stale holder still writes.
2
+ \* Expected TLC result: NoStaleWrite violated.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ NULL = NULL
6
+ AtomicClaim = TRUE
7
+ Fenced = FALSE
8
+ SPECIFICATION Spec
9
+ INVARIANTS NoStaleWrite LegitimateWriters
@@ -0,0 +1,9 @@
1
+ \* Liveness under per-worker fairness: with the fixed protocol a resumable
2
+ \* instance eventually finishes on some worker.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ NULL = NULL
6
+ AtomicClaim = TRUE
7
+ Fenced = TRUE
8
+ SPECIFICATION FairSpec
9
+ PROPERTY Liveness
@@ -0,0 +1,9 @@
1
+ \* Current design: atomic conditional-UPDATE claim + fenced writes + heartbeat.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ NULL = NULL
6
+ AtomicClaim = TRUE
7
+ Fenced = TRUE
8
+ SPECIFICATION Spec
9
+ INVARIANTS NoStaleWrite LegitimateWriters
@@ -0,0 +1,11 @@
1
+ \* Pre-fix design: non-atomic claim, unfenced writes.
2
+ \* Expected TLC result: INVARIANT violation -- the counterexample is either
3
+ \* the claim race (two workers pass the stale read, both run, the non-holder
4
+ \* writes) or lease expiry without fencing.
5
+ CONSTANTS
6
+ Workers = {w1, w2}
7
+ NULL = NULL
8
+ AtomicClaim = FALSE
9
+ Fenced = FALSE
10
+ SPECIFICATION Spec
11
+ INVARIANTS NoStaleWrite LegitimateWriters
@@ -0,0 +1,9 @@
1
+ \* Fencing WITHOUT an atomic claim: two workers believe they hold the lock.
2
+ \* Expected TLC result: LegitimateWriters violated.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ NULL = NULL
6
+ AtomicClaim = FALSE
7
+ Fenced = TRUE
8
+ SPECIFICATION Spec
9
+ INVARIANTS NoStaleWrite LegitimateWriters
@@ -0,0 +1,97 @@
1
+ # spec/ — formal models of the cross-language concurrency protocols
2
+
3
+ This directory is the normative specification of the protocols every
4
+ language implementation (romancy/Go, edda/Python, shikibu/Ruby) must
5
+ follow. Per protocol:
6
+
7
+ - `<Name>.tla` — the TLA+ model of record, with `*_new.cfg` / `*_old.cfg`
8
+ (or equivalent) configs. The `_old` config deliberately disables the
9
+ protection mechanism and MUST violate the invariant: it proves the
10
+ model has discriminating power (the protection is load-bearing).
11
+ - `tlc-matrix.txt` — the expected TLC verdict of every config, checked by
12
+ `tlc-check.sh` in CI. A config without a row fails CI.
13
+ - `<name>_check_test.go` (some models) — a dependency-free exhaustive BFS
14
+ checker over the same state machine, runnable without TLC:
15
+ `go test ./...`. These are transcriptions; the `.tla` is authoritative.
16
+
17
+ ## Coverage map: spec action → implementation
18
+
19
+ Names, not line numbers (line numbers rot). Paths: romancy
20
+ `internal/storage/{sqlite,postgres,mysql}.go`, edda
21
+ `edda/storage/sqlalchemy_storage.py`, shikibu `lib/shikibu/storage/*.rb`
22
+ unless noted.
23
+
24
+ | Spec | Key action | romancy (Go) | edda (Python) | shikibu (Ruby) |
25
+ |---|---|---|---|---|
26
+ | InstanceClaim | CAS claim | `TryAcquireLock`, `ClaimResumableWorkflows` | `try_acquire_lock`, `claim_resumable_workflows` | `try_acquire_lock`, `claim_resumable_workflows` |
27
+ | InstanceClaim | lease renewal, stale steal | `RefreshLock`; expired-lease arm of the claims; `CleanupStaleLocks` (leader `cleanupStaleLocks`, `app.go`) | `refresh_lock`, `cleanup_stale_locks` (`locking.py` loops) | `refresh_lock`, `run_stale_lock_cleanup` (`worker.rb`) |
28
+ | InstanceClaimVerdict | RowsAffected verdict of a same-holder rewrite | `TryAcquireLock` after `ClaimResumableWorkflows` (same token), `RefreshLock` | same | same |
29
+ | EpochFencing, EpochFencingRuns | fenced write | `locked_by` = `NewLockIdentity` per run; `UpdateInstanceStatusIfLocked`, `AppendHistoryIfLocked` | `append_history`, `update_instance_status` | `append_history`, `update_instance_status` |
30
+ | CompetingMessageClaim, CompetingMessageClaimSplit | claim, record, delete | `ClaimChannelMessage`, `DeliverChannelMessageWithLock`, `Receive` (`channels.go`) | `claim_channel_message`, `deliver_channel_message`, `receive` (`channels.py`) | `claim_message`, `claim_message_in_transaction`, `deliver_channel_message` |
31
+ | WaitRegistration, WaitRegistrationSub | wait registration, requeue sweep, timeout | `RegisterChannelReceiveAndReleaseLock`, `RequeueChannelWaitsWithPending`, `FindExpiredChannelSubscriptions` | `register_channel_receive_and_release_lock`, `requeue_channel_waits_with_pending` | `register_channel_wait`, `requeue_channel_waits_with_pending` |
32
+ | TaskClaim, TaskClaimReadBack | CAS claim + read-back | `ClaimTask` | `claim_task` | `claim_task` |
33
+ | TaskReclaim, TaskReclaimAttempts | fenced completion | `CompleteTask` (`WHERE claimed_by = ? AND state = 'claimed'`) | `complete_task` | `complete_task` |
34
+ | TaskReclaim | orphan reclaim | `ReclaimStaleTasks` (leader `cleanupStaleLocks`) | `reclaim_stale_tasks` | `reclaim_stale_tasks` |
35
+ | SupervisorRestart, SupervisorRestartCAS | snapshot + cancel/respawn | `superviseSync`, `cancelChild` (`supervisor.go`), `CancelInstance` | `supervisor.py` sync activity, `cancel_instance` | `supervisor.rb` sync activity, `cancel_instance` |
36
+ | ActorFS | conditional write | `FSPut` (`FSDelete` not modeled) | `fs_put` | `fs_put` (`fs_delete` not modeled) |
37
+
38
+ ## What the models say — and what they deliberately don't
39
+
40
+ **TaskReclaim** — fencing (`claimed_by`-guarded completion write) protects
41
+ the row: no completion write can clobber an already-done task. It does
42
+ NOT prevent duplicate side-effect execution: task claims have no
43
+ heartbeat/renewal, so a slow-but-alive worker's claim can be reclaimed
44
+ and a second worker executes the same work. Execution is **at-least-once**;
45
+ task handlers must be idempotent. The 176-state fenced exploration shows
46
+ `clobbered` unreachable while duplicate execution remains reachable.
47
+ `claimed_by` is the instance id, so the fence cannot tell two attempts of
48
+ the same instance apart (a stale run next to its replacement, or a
49
+ re-claim after an orphan reclaim): `TaskReclaimAttempts` shows a stale
50
+ attempt's completion landing unless the write is also fenced on the
51
+ attempt.
52
+
53
+ **SupervisorRestart** — `CompletedNeverRespawned` (for a child whose
54
+ restart mode does not restart normal exits) needs two things, shown by
55
+ `SupervisorRestartCAS`: the cancel is a conditional update from a
56
+ non-terminal status whose result gates the respawn (a child that
57
+ completed first is not respawned), and terminal statuses are sticky (no
58
+ timer, channel or completion path moves a row off `cancelled` /
59
+ `completed`). Either one alone is not enough. A best-effort cancel whose
60
+ result is ignored — the `racy` config — violates it: a completed sibling's
61
+ replacement generation may run its side effects again.
62
+
63
+ ## Known gaps (not yet modeled)
64
+
65
+ - Recur (archive + input carry-forward), and supervision across recur: a
66
+ supervisor that watches a child which recurs onto a new instance id
67
+ (edda, shikibu) must follow `continued_from`.
68
+ - Cancellation in general (only the supervisor's cancel is modeled).
69
+ - `FSDelete` / `fs_delete` with version 0 (unconditional): delete and
70
+ recreate is an ABA the ActorFS model cannot see.
71
+ - Clock bases: every model uses abstract lease expiry. Mixed clocks
72
+ (app-clock writes compared against `NOW()`, session time zones) need a
73
+ conformance test, not a model.
74
+ - Transactional publish (post-commit delivery / rollback non-delivery).
75
+ - Compensation (saga) ordering and idempotency.
76
+ - Outbox event claiming.
77
+
78
+ ## Running TLC proper
79
+
80
+ The Go checkers are hand-written transcriptions of the `.tla` models;
81
+ they can drift. CI runs every config in `tlc-matrix.txt` with real TLC
82
+ (`.github/workflows/tla.yml`). To run locally (Java 11+):
83
+
84
+ ```
85
+ curl -fsSL -o /tmp/tla2tools.jar \
86
+ https://github.com/tlaplus/tlaplus/releases/download/v1.7.4/tla2tools.jar
87
+ spec/tlc-check.sh /tmp/tla2tools.jar
88
+ ```
89
+
90
+ One config by hand:
91
+
92
+ ```
93
+ java -cp /tmp/tla2tools.jar tlc2.TLC -deadlock -config TaskReclaim_new.cfg TaskReclaim.tla
94
+ ```
95
+
96
+ `-deadlock` turns deadlock checking off: workers that finish or abort
97
+ legitimately stop.