orfeas_lyra 1.0.0 → 1.0.1

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +387 -0
  3. data/README.md +20 -8
  4. data/Rakefile +1 -1
  5. data/lib/generators/lyra/install/install_generator.rb +49 -0
  6. data/lib/generators/lyra/install/templates/create_lyra_tables.rb.tt +163 -0
  7. data/lib/generators/lyra/install/templates/initializer.rb.tt +22 -0
  8. data/lib/lyra/associations/habtm_links.rb +4 -0
  9. data/lib/lyra/bypass_events.rb +37 -5
  10. data/lib/lyra/command_handler.rb +5 -0
  11. data/lib/lyra/configuration.rb +32 -3
  12. data/lib/lyra/dual_view.rb +5 -2
  13. data/lib/lyra/effect_check.rb +52 -0
  14. data/lib/lyra/engine.rb +13 -0
  15. data/lib/lyra/erasure.rb +4 -4
  16. data/lib/lyra/event_store_adapter.rb +59 -0
  17. data/lib/lyra/genesis.rb +16 -4
  18. data/lib/lyra/interceptors/crud_interceptor.rb +70 -8
  19. data/lib/lyra/interceptors/lazy_reads.rb +54 -1
  20. data/lib/lyra/mode_sync.rb +86 -0
  21. data/lib/lyra/mode_transition.rb +96 -3
  22. data/lib/lyra/projections/async_checkpoint.rb +14 -1
  23. data/lib/lyra/projections/cached_relation.rb +194 -3
  24. data/lib/lyra/projections/lazy_projection.rb +496 -68
  25. data/lib/lyra/projections/model_projection.rb +12 -5
  26. data/lib/lyra/projections/rebuild.rb +163 -6
  27. data/lib/lyra/projections/unique_indexes.rb +193 -13
  28. data/lib/lyra/projections/uniqueness.rb +65 -2
  29. data/lib/lyra/repair.rb +3 -1
  30. data/lib/lyra/stream_heads.rb +309 -0
  31. data/lib/lyra/strict_data_access.rb +12 -4
  32. data/lib/lyra/verification/async_lock_order_net.rb +327 -0
  33. data/lib/lyra/verification/bypass_workflow.rb +3 -2
  34. data/lib/lyra/verification/concurrent_writers_net.rb +350 -0
  35. data/lib/lyra/verification/gate_settle_net.rb +208 -0
  36. data/lib/lyra/verification/lazy_catch_up_net.rb +228 -0
  37. data/lib/lyra/verification/lazy_lock_order_net.rb +221 -0
  38. data/lib/lyra/verification/multi_record_lock_net.rb +271 -0
  39. data/lib/lyra/verification/template_invariants.rb +71 -0
  40. data/lib/lyra/verification/trace_replay.rb +85 -0
  41. data/lib/lyra/version.rb +1 -1
  42. data/lib/lyra.rb +19 -0
  43. data/lib/tasks/lyra_projections.rake +9 -3
  44. metadata +14 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 353d67ab256042efa0efc50975912ec1f83b68ff1a1c449de4a53450289017a4
4
- data.tar.gz: 76a93fd07660152db6cf0aee259adc21e1ea36632feaf2ddc8ab967bfbbbfa4c
3
+ metadata.gz: a45745e2e4f4e433103d1c114c8fead7950c75270d09a0d3db53b78b68ceb66b
4
+ data.tar.gz: 702d1ba3aa99ffded1f5ffb5b7d354a1fc81bda819a8c412825b2d3c234bc7ff
5
5
  SHA512:
6
- metadata.gz: bcbb270eaa064071766f11aef170a3cae53908899472630ed3946a46c519edcb7d916a80c0515ff5bef1f8e43dbf6b7d43e4fe267116a7530f1f5c5d406a0ea0
7
- data.tar.gz: 14c96c3e8154cff0046b18806a144424da30287d9c770f26c5904ee25b30a0eb92f9dc8dd2eb2f4defe85e5e850c94e9369f53714a3988053194432e5c465d13
6
+ metadata.gz: 7c4f8a4925ea9191907b0595cb188f9edc0ab454af84bf682b48c0336dffd2c06965d53761595cbb227e20647a265c334455575581a2c08a2d483d118f98ff04
7
+ data.tar.gz: 189c9c0525fcaa50e5893601e6f3c7058b7c12d8c5386444fcc541c3aa9fd879d2ead10ccd856209255fb6f15239e43d778997a95e671cdd0d5e5fc5180ae7fd
data/CHANGELOG.md CHANGED
@@ -7,6 +7,393 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.0.1] - 2026-11-26
11
+
12
+ A patch release of `orfeas_lyra`; `orfeas_pam_dsl` and `orfeas_petri_flow` stay at 1.0.0. It adds
13
+ one table of Lyra's own, `lyra_stream_heads`, and one column, `lyra_projection_checkpoints.event_id`,
14
+ each created on first use like the others or by the new install migration, and three settings:
15
+ `stream_heads` (on), `check_effects` (off) and `mode_transition_settle_timeout`. A record's history is
16
+ checked where its events are appended; in ES-Lazy a catch-up no longer holds its lock to the
17
+ application's commit; a read under a row lock sees every committed write of its records and keeps
18
+ out the others until it ends, in ES-Async, ES-Lazy and ES-NoProj; and the mode gate waits for the
19
+ writes in flight (see Fixed). What remains is listed under Known issues.
20
+
21
+ ### Added
22
+ - **`config.check_effects`, opt-in (off by default; `LYRA_CHECK_EFFECTS=1`): the effect clause of
23
+ Hypothesis H checked at the write.** In Hijack and ES-Sync the row is written in the same
24
+ transaction as the event. With the setting on, after each create, update or destroy through
25
+ callbacks, Lyra compares the record's row with the replay of its stream, as the mode gate and
26
+ DualView compare them, and raises `Lyra::EffectMismatchError` when they differ (a trigger, a
27
+ default the database computes, a type that does not round-trip), so the write and its event roll
28
+ back together. Off, such a write goes through as in 1.0.0 and DualView finds the difference
29
+ later. Monitor takes its event from what its table did, and ES-Async, ES-Lazy and ES-NoProj
30
+ write the row later or never, so the check does not apply there; writes that skip callbacks
31
+ (`update_columns`, `update_all`, `insert_all`) are not checked, nor are a soft-deleted record (its
32
+ stream ends in a destroy while its row stays, by design) and a model on another database than
33
+ the event store; an STI record is compared by its base class, as its stream names it. Cost: a read of the row and of
34
+ the stream and a replay per write. The benchmark campaign runs with it off.
35
+ - **`bin/rails generate lyra:install`** writes one migration with Lyra's own tables
36
+ (`lyra_mode_transitions`, `lyra_projection_checkpoints`, `lyra_async_checkpoints`, `lyra_rights`,
37
+ `lyra_consents`, `lyra_stream_heads`), with the definitions Lyra uses when it creates them at run
38
+ time, and an index on Rails Event Store's `event_store_events_in_streams (stream, id)`, which
39
+ serves Lyra's backward reads by id (Rails Event Store 3 creates it for a new event store; event
40
+ stores created by earlier versions lack it), and one on its `event_id`, which serves ES-NoProj's
41
+ reference links (Rails Event Store 3 creates it too; below); each only where it is missing, and on
42
+ PostgreSQL concurrently. It also
43
+ writes `config/initializers/lyra.rb` unless the application has one (`--skip-initializer`), and
44
+ takes `--database` for another database's migrations path. With the migration the tables are in
45
+ `db/schema.rb` or `db/structure.sql`, so `db:schema:load` and test databases have them, and the
46
+ application's database role needs no right to create tables. Creation on first use stays: without
47
+ the migration Lyra creates each table the first time it needs it, as in 1.0.0, and with it finds
48
+ the table there and does nothing. Every statement is "if not exists", so the migration runs where
49
+ Lyra already created the tables, leaves them as they are, and gives a `lyra_mode_transitions`
50
+ created by an earlier version the columns later versions added; rolling it back changes nothing.
51
+ **Upgrading:** run the generator and `bin/rails db:migrate`; the stream heads need no backfill, as
52
+ each record's head is built from its events the first time its stream is met. A test creates
53
+ every table both ways and compares them in PostgreSQL's catalog (columns, types, nulls, defaults,
54
+ indexes, constraints), and loads them from a dumped `db/schema.rb`.
55
+
56
+ - **`Lyra::Verification::MultiRecordLockNet`: locks across records.** The other concurrency nets
57
+ model one record's locks; this one models ES-NoProj's transactions on records of one model or
58
+ several: writes, locking reads of one record or several, and bulk writes, with each record's
59
+ aggregate lock and stream head, against plain Active Record's row locks in any order its scans
60
+ take them. Three conditions, checked on the net's structure, make every deadlock ES-NoProj can
61
+ reach one plain Active Record can reach, for any number of transactions, steps and records: a
62
+ stream head is taken only under its record's aggregate lock, a step locks only its own records,
63
+ and in an order plain Active Record's scans can take (stream-name order for a locking read, the
64
+ keys' hash order for a bulk write). Assumed: each record's lock is its own (two streams whose
65
+ 32-bit lock keys collide share one). Explored: with single-record steps, the mixes of two
66
+ transactions of two steps on three records that can deadlock are plain Active Record's (48 of
67
+ 666); with every step kind, 8,056 of 19,306 can, all among plain Active Record's 13,192. A read
68
+ that locked every record of the model would break two conditions and deadlock in 219 of the 666.
69
+ Tests check that the code takes the locks in the net's orders: records of two models, Rails'
70
+ writes of other records (`touch: true`, `dependent: :destroy`), a locking read of several records
71
+ and a bulk write.
72
+ - **The concurrency nets documented** (`docs/CONCURRENCY_NETS.md`): the six nets of
73
+ `Lyra::Verification`, their places, writers and properties, and how to run them; and
74
+ `benchmarks/petri-concurrent-writers/export_nets.rb`, which writes one configuration of each as PNML
75
+ and Graphviz DOT, with a summary of its exploration.
76
+ - **`docs/PERFORMANCE.md` gives the 1.0.1 measurement** (23-25 November 2026, three runs, 4, 16 and
77
+ 64 threads, every mode and its controls, the stream heads' included), with the statement traces of
78
+ 1.0.1, in place of 0.9.0's; the README's summary follows it (at 16 threads Hijack about 25%, Monitor
79
+ about 44%). The runs are in the replication package, https://doi.org/10.5281/zenodo.24390709.
80
+
81
+ ### Fixed
82
+ - **Hijack imports the rows that predate Lyra before a model's first use, as the event-sourcing
83
+ modes do** (`config.genesis` `:auto`, the default). Its stream heads refuse an update of a record
84
+ with no history, and `:auto` imported only in the event-sourcing modes: where the mode gate did
85
+ not run (the test environment's default, a forced switch), every update of such a row in Hijack
86
+ was refused (`MissingRecordError`). The benchmark campaign's smoke step met it in its Hijack cells.
87
+ - **Partial unique indexes are enforced in the event-sourcing modes** where their condition is one
88
+ PostgreSQL writes back in a common form: a column `IS [NOT] NULL`, a column `=` or `<>` a literal
89
+ (casts on either side), a boolean column alone or under `NOT`, joined by `AND`. A record outside the
90
+ index claims nothing, and a holder outside it holds nothing, as the table's index decides; ES-Sync
91
+ adds the index's own `WHERE` to its lookup. Before, every partial index was skipped: ES-NoProj saved
92
+ the duplicate, and in ES-Async and ES-Lazy it reached the log and its projection met the index. A
93
+ condition with `OR`, `IN`, a function or another column is still left to the table (named in the
94
+ log).
95
+ - **ES-NoProj: a relation built before a write and read after it sees the write,** as Active Record's
96
+ does (it runs its query at the first read). ES-NoProj evaluated a relation when it was built, so a
97
+ record created or changed in between was missing from its reads and its count. A relation that has
98
+ not been loaded is now evaluated again at its first use if a record event was appended in this
99
+ process since it was built; a loaded one keeps its records until `reset` or `reload`, as in Active
100
+ Record. A write of another process is seen by a relation built after it.
101
+ - **ES-Async: a record-level `touch` or `update_columns` is no longer written over by a projection
102
+ job.** Such a write took the record's aggregate lock but not its stream's, so a job in its last
103
+ pass (the stream's lock, the events after its checkpoint, then the row) could read the stream
104
+ before the write, wait for its row, and then write the older value over it, with its checkpoint
105
+ below the write's event, which no job then applied: the row stayed behind its log until the
106
+ record's next write (the Olist replay at 4 threads, `Spree::Shipment`'s `updated_at`; an
107
+ `update_columns` lost its value). The write now applies the events its job has not, under the
108
+ stream's lock, before its row, as a bulk write does.
109
+ - **ES-NoProj: inserts inside `where(...).scoping { }` and on a relation take the scope's
110
+ attributes,** as Active Record's `InsertAll` writes them in Monitor: `insert_all`, `insert_all!`
111
+ and `insert` wrote their records without them.
112
+ - **ES-Async: a bulk write starts from the row as its stream has it.** `update_all` (an SQL
113
+ expression such as `n = n + 1`), `update_counters`, `increment!` and `touch_all` on a record whose
114
+ projection job had not run read the row the job had not written yet: the update pending in the job
115
+ was lost (a log of `n` 0 → 7, then 0 → 1, where Monitor gives 8), with no error. Under the records'
116
+ aggregate locks, the events their jobs have not applied are now applied first, under the streams'
117
+ locks (`AsyncProjectionJob.project_pending`), as `insert_all` and `upsert_all` already did; a
118
+ pending destroy deletes its row there, and the write leaves it out as before. Found by trace
119
+ conformance: the ES-Async lock-order net models bulk writes that way, and the recorded
120
+ `update_all` took the row without the stream lock.
121
+ - **ES-Lazy: a catch-up that fell back to its reader's transaction no longer hangs the readers.**
122
+ Where no second connection is free, a read inside a transaction catches up in it, and holds the
123
+ catch-up lock to its commit. Two ways of waiting for that lock hung, with no error, before this
124
+ release: the same transaction's next read, once a connection was free, caught up there and waited
125
+ for the lock its own transaction held; and a reader holding a record that the fallback's
126
+ transaction then wrote caught up on its own connection and waited for the lock while its
127
+ transaction waited for it in Ruby, a cycle PostgreSQL's detector cannot see. The Olist replay
128
+ hung within a minute in ES-Lazy at 16 threads with a pool of 20 (the concurrency nets found the
129
+ second way first: `LazyLockOrderNet`'s hang). Now a transaction that holds the lock catches up in
130
+ place, and a catch-up on its own connection waits at most 1 s for the lock
131
+ (`LazyProjection::OWN_LOCK_WAIT`), then runs in its reader's transaction, where a cycle is a
132
+ deadlock PostgreSQL detects (`ActiveRecord::Deadlocked`, which the application can retry).
133
+ - **ES-NoProj's deletes no longer slow down as the event store grows.** A destroy checks its
134
+ dependent associations through the reference links, and the lookup joined Rails Event Store's
135
+ links table to itself by `event_id`. Rails Event Store 3's own migration indexes that column, but an
136
+ event store set up otherwise can lack the index (the testbed's did), and the lookup then read every
137
+ link row: on the testbed a delete took about 120 ms at 10⁵ events and about 1 s at 10⁶. The install
138
+ migration now adds the index where it is missing, with which the same delete takes about 15 ms at
139
+ 10⁵, as at 10⁴. An application that ran the migration of an earlier 1.0.1 build adds it with
140
+ `add_index :event_store_events_in_streams, :event_id, algorithm: :concurrently, if_not_exists: true`.
141
+ Where it is new, the index adds one entry per link row to every append.
142
+ - **ES-Async's check for rows behind their jobs works where Rails Event Store's `event_id` is a
143
+ uuid column** (Solidus's schema, and others): it compared that column with Lyra's string checkpoint
144
+ column, which PostgreSQL refuses (`operator does not exist: uuid = character varying`). The
145
+ query ran for writes that skip callbacks, and from 1.0.1 for every locking read; it now casts
146
+ the checkpoint's id to the store's type, as the stream heads do. Found by a 20-order Olist replay
147
+ before the release: read-your-writes then matched Disabled in every order.
148
+ - **DualView reads the row without the model's default scope, and so do ES-Sync's projection
149
+ writes.** DualView, and with it the mode gate, the sampler and `lyra:repair`, looked the row up
150
+ through the model's default scope, so a row it hides (a soft-deleted one) was reported missing
151
+ while its events described it; ES-Sync's projection updated and deleted through the same scope,
152
+ so an update or a destroy of such a record left its row as it was. Both read and write the row
153
+ unscoped now.
154
+ - **A read-modify-write under a row lock no longer loses an update in ES-Async, ES-Lazy and
155
+ ES-NoProj.** Two transactions that each read a record with `lock!`, `with_lock` or
156
+ `Model.lock.find(id)` and increment it could give 1 instead of 2, with no error: in ES-Async the
157
+ row lock read a row waiting for its projection job, in ES-NoProj the lock locked nothing (there
158
+ is no row), and in ES-Lazy the catch-up ran before the lock was granted. Now such a read first
159
+ takes the records' aggregate locks, which every writer of them takes before its append; in
160
+ ES-Async also the records' stream locks, under which their rows are brought up to date with
161
+ the events no job has applied yet; then it reads (in ES-Lazy, under the row lock and the
162
+ records' stream heads, the records behind their streams are replayed and read again). A writer
163
+ of those records waits until the reader's transaction ends. The order (the aggregate lock, the
164
+ stream's, then the row) is the one writes that skip callbacks take, and it removes ES-Async's
165
+ deadlocks between `with_lock` followed by a write and a write that skips callbacks or a
166
+ read-your-writes projection (checked on `Lyra::Verification::AsyncLockOrderNet`). A locking read
167
+ whose keys are not known before it runs (`Model.lock.where(...)`, a `lock` with a join) reads the
168
+ keys it selects first, without the lock, takes their locks, then runs; a record that started to
169
+ match in between is locked and the read runs again (up to twice). In ES-NoProj a locked relation
170
+ (`lock`, carried to what is built from it, as Active Record's is) locks its records before its
171
+ first read and reads them again under the locks. Hijack, ES-Sync and Monitor, whose row lock is
172
+ the lock, are unchanged. In ES-NoProj a locking read through `find_by`, which is how Rails reads `lock!`, `with_lock` and
173
+ `reload(lock: true)` (`all.lock.find_by!(id: ...)`), locks only the records it reads, as
174
+ `lock.find(ids)` does.
175
+
176
+ - **ES-Lazy: a checkpoint whose event is gone is no longer trusted.** An event store emptied or
177
+ restored without `lyra_projection_checkpoints`, whose new events reached the old checkpoint's
178
+ position, was not caught up: the guard compared positions only, so the new events below the
179
+ position looked applied. The checkpoint now keeps the id of the event at its position (a new
180
+ column, `event_id`, added at run time to a table an earlier version created, and by the install
181
+ migration), and a checkpoint whose event is no longer there is started over, with a warning, as
182
+ one past the newest event was. A checkpoint written by 1.0.0 names no event and is taken as
183
+ before until the next catch-up moves it.
184
+ - **A record's history is now checked where its events are appended (stream heads).** Lyra 1.0.0 did
185
+ not check that a record's operations are valid for the ORM, an assumption its correctness
186
+ arguments make. A `create` with a live record's primary key, given or generated by a sequence
187
+ behind the table, appended a second `Created` in ES-Sync, ES-Async, ES-Lazy and ES-NoProj, with no
188
+ error: the table kept the old row while the log said the new one, and ES-NoProj served the new
189
+ values. An `upsert_all` whose generated key met a live row recorded a second `Created` in every
190
+ mode that records events but ES-NoProj. Now every replayed CRUD event Lyra appends first moves
191
+ its record's head, a row in the new table `lyra_stream_heads`, in the same transaction:
192
+ - a create or an import needs no live record; an update or a destroy needs a live one; a destroy
193
+ makes it gone, and a destroyed record's key can be created again;
194
+ - two concurrent creators of one key meet on the head's primary key: the second waits for the
195
+ first's transaction, as on the table's own key, and is refused if that committed;
196
+ - in Hijack and the event-sourcing modes a refused write raises before its event is stored:
197
+ `Lyra::StreamHeads::LiveKeyError` (an `ActiveRecord::RecordNotUnique`, as plain Active Record
198
+ raises for the table's key, so `create_or_find_by` works), `Lyra::RecordDestroyedError` or
199
+ `Lyra::StreamHeads::MissingRecordError` (both `ActiveRecord::RecordNotFound`);
200
+ - Monitor never refuses a write its table took: it records it, and reports a head that disagreed
201
+ (the log, and the `lyra.stream_head_mismatch` notification) for DualView and `lyra:repair`.
202
+
203
+ The head is a row lock, not an advisory lock, so a transaction may create any number of
204
+ records. It is taken last before the append, after the aggregate lock and the unique-index
205
+ locks. The log stays the authority: a head whose last event is no longer in the store (events
206
+ deleted by hand, a test database cleaned of them) is not trusted, and a stream with events but no
207
+ head (written before 1.0.1) gets its head from its events when first met. The table is created
208
+ on first use; `config.stream_heads = false` restores 1.0.0's behaviour.
209
+ - **A change of a record's primary key** is refused in Hijack and the event-sourcing modes
210
+ (`Lyra::StreamHeads::KeyChangeError`), where 1.0.0 recorded the update on the new key's stream:
211
+ onto a live key it overwrote that other record with no error, onto a free key it began a stream
212
+ with no create. Monitor, which records what its table did, records it as the destroy of the old
213
+ key and the create of the new one.
214
+ - **An `upsert_all` that met a live record is recorded as that record's update**, as the database
215
+ executed it, with its changes from the record's replayed state, where 1.0.0 recorded a create.
216
+ - **ES-Sync refuses a create whose row's key is already taken** (`LiveKeyError`), as Monitor's
217
+ `INSERT` is refused; 1.0.0 skipped the insert and kept the event.
218
+ - **ES-Lazy: the check before a read skipped committed events below the checkpoint, the thread's
219
+ own included.** Event ids are assigned before commit, so a write can commit below the checkpoint
220
+ (a gap) when a later id was applied first. The check took the tables for up to date whenever the
221
+ process had re-checked the gaps less than a second before, whether or not a gap's event had
222
+ committed since. Such a write was invisible to reads until a later catch-up, with no error where
223
+ it was read: a thread got `RecordNotFound` for a record it had just created, two increments from
224
+ 0 by one thread, one after the other, gave 1, `update_all` matched no row, and
225
+ `catch_up!(force: true)` left the write out of the tables. The defect is not new in 1.0.0: 0.9.0
226
+ has it too. Now, while gaps are open, the check asks in one more query whether the event of one
227
+ of them is visible to the read, and catches up if so.
228
+ - **ES-Lazy: a late event under a foreign key no longer stops every read.** A catch-up applied the
229
+ events above the checkpoint first and filled the gaps after them, by replaying each gap's whole
230
+ record. A child above the checkpoint whose parent's create was a gap failed on its foreign key,
231
+ and so did a replay that passed through a parent destroyed since; the catch-up rolled back, and
232
+ every later read raised `ActiveRecord::InvalidForeignKey`, also after the gap expired. Now a
233
+ gap's event, once visible, is applied in its place in the log's order, before the events above
234
+ it, followed only by those later events of its record that were applied before it. A store
235
+ already in that state recovers at the next read.
236
+ - **ES-Lazy: the check and the catch-up are no longer answered from the query cache** (in a
237
+ request or a job). The check came from the cache, so a read could miss another connection's
238
+ committed write until the cache was next cleared. So did the attempt to take the catch-up lock,
239
+ so a catch-up could run on an earlier answer without holding the lock.
240
+
241
+ - **A mode set in a process after boot is no longer overridden by a switch recorded before that
242
+ boot.** A process whose boot check does not run (every rake task, and any process with
243
+ `mode_transition_gate = false` and `mode_sync = true`) took the latest `applied` row of
244
+ `lyra_mode_transitions` for a new switch at its first ModeSync check. It adopted that row over a
245
+ mode it had set itself with `config.mode =`, `projection_mode =`, `hijack_enabled =`, `disable!`
246
+ or an `enable_*!` helper, so a benchmark that replayed Disabled in a rake task ran in Monitor and
247
+ stored events. Now such a process notes, when it finishes booting, the latest applied switch,
248
+ and a switch recorded up to then does not override any mode it sets after boot, including the
249
+ one it booted in. A process that sets no mode after boot still adopts the application's mode at
250
+ its first check, so a task or console booted with an older configuration does not write in the
251
+ wrong mode. The configuration a code reload applies again is not a set. A switch recorded after
252
+ the boot is adopted as before. With ModeSync on, booting costs one read-only query, which creates
253
+ nothing and gives back the connection it opened.
254
+ - **Leaving Hijack with `config.mode =` clears `config.hijack_enabled`.** `enable_hijack!`, a switch
255
+ into Hijack and ModeSync's adoption of one set the flag, and `mode =` never cleared it.
256
+ `hijack_mode?` stayed true, so a process set to Disabled or Monitor still routed writes through
257
+ events as in Hijack. Now `mode =` clears the flag when it leaves Hijack, as a switch does, and
258
+ `enable_hijack!` followed by `mode =` follows the last call. Code that relied on the flag
259
+ outliving `mode =` now really leaves Hijack. `hijack_enabled = true` set on its own, outside
260
+ Hijack, still makes Lyra act as Hijack whatever `mode` says.
261
+
262
+ - **A rebuild no longer destroys rows the event log cannot restore without being asked to.**
263
+ `Lyra::Projections::Rebuild.rebuild` (and `rebuild_all`, and `rake lyra:projections:rebuild`)
264
+ clears the table by default before it replays the streams, and the database then applies each
265
+ foreign key's `ON DELETE` action: the rows of another table on an `ON DELETE CASCADE` key into
266
+ it were deleted, and `SET NULL` or `SET DEFAULT` references rewritten, with no error; rows of the
267
+ table itself that had no stream (not yet imported) were deleted and not restored. A clearing
268
+ rebuild now looks for both first, foreign keys from PostgreSQL's catalog, and raises
269
+ `Rebuild::Refused` before writing anything, naming each key with its table, column, action,
270
+ constraint and the rows it reaches, and the rows without a stream. A key counts only where rows
271
+ reference the table; the table's references to itself and its model's own
272
+ `has_and_belongs_to_many` join tables (restored from their link events) do not count, and the
273
+ rows of a monitored table do, as a rebuild restores one model's table. The message says how to
274
+ proceed: back up, import the rows without a stream (Genesis), then rebuild in place
275
+ (`truncate: false`, `TRUNCATE=false`, unchanged) or clear anyway with the new `force: true`
276
+ (`FORCE=1`). `Rebuild.clear_hazards(Model)` returns what a clearing rebuild would lose.
277
+ - **A mode check that rebuilds no longer changes the tables when it fails.**
278
+ `Lyra::ModeTransition.check(rebuild: true)` (and so `to!(..., rebuild: true)` and
279
+ `rake lyra:mode:check REBUILD=1`) rebuilt the tables, committed, then compared, so a check that
280
+ found discrepancies, or a switch it refused, had already rewritten them. The rebuild and the
281
+ comparison now run in one transaction (a savepoint inside a transaction already open), rolled
282
+ back unless the check is clean; a clean check keeps the rebuilt tables and its certificate. A
283
+ rebuild the check cannot do safely (above) raises `ModeTransition::Refused` and changes no
284
+ table.
285
+ - **The mode gate no longer certifies a switch with a write in flight.** The check noted the event
286
+ store's position (its highest event id) and later re-checked only the records with events above
287
+ it. A write whose event took an id below that position and committed after it was never
288
+ re-checked: after a gated switch from ES-Lazy to ES-Sync its record was in the log and missing
289
+ from the table, with no error. On PostgreSQL the gate now settles each position it uses, at
290
+ the check's start and end and at the switch: it waits until every writing transaction of the
291
+ database running when the position was read has ended, at most `mode_transition_settle_timeout`
292
+ seconds (new, default 30), so the catch-up, the comparison and the re-check that follow see what
293
+ those writes committed. A write that does not end in time leaves the check unclean, with the
294
+ discrepancy "a write in flight at the check did not end", and no certificate is stored. This
295
+ relies on a Lyra write holding a transaction id before its event takes an id, which writing the
296
+ record's stream head first ensures. Elsewhere the position is read as before.
297
+
298
+ ### Changed
299
+ - **ES-NoProj: a uniqueness validation a unique index covers takes no lock.** Where a unique index on
300
+ every row has exactly the validated attribute and its `scope:` columns (and the validation no
301
+ `conditions:`, and `case_sensitive: false` only on a citext column), the validation takes neither its
302
+ value lock nor the record's aggregate lock before it: two writers of one value may both pass it, as
303
+ in Monitor, and the second is refused at its append by the index check, under the value's lock
304
+ (`ActiveRecord::RecordNotUnique`, the table index's error in Monitor, where it was
305
+ `ActiveRecord::RecordInvalid`). A failed validation there no longer holds the aggregate lock to the
306
+ end of its transaction. A validation no index covers keeps both locks, which Monitor does not take
307
+ (Rails' validation alone is a read, then a write).
308
+ - **ES-Lazy warns once per process when a catch-up finds no free connection** and runs in its
309
+ reader's transaction (the fallback), naming the pool's size: that transaction then holds the
310
+ catch-up lock to its commit, and every other reader's catch-up waits for it. A pool of twice the
311
+ threads that read inside transactions keeps it rare. Not for a pinned connection (Rails'
312
+ transactional tests).
313
+ - **ES-NoProj: a uniqueness validation on a column with a unique index reads its value's holders,
314
+ not every record of the model.** Every record that ever held the value has an event in that value's
315
+ stream (the one the unique-index check reads), so those are read and the validation's conditions
316
+ tested on them as before: at 3,000 records a `valid?` took 0.9 ms instead of 29 ms, and a
317
+ `create!` 24 ms instead of 70 (dev VM). Without a unique index, under `case_sensitive: false` on a
318
+ column that is not citext, or with value links off, every record is read as before.
319
+ - **Writes cost more statements.** A create adds the head's insert and a check of its stream; an
320
+ update or a destroy one statement on its head. The cost is measured in the 1.0.1 benchmark
321
+ campaign.
322
+ - **An update or destroy of a record with no event history is refused** in Hijack and the
323
+ event-sourcing modes (`MissingRecordError`), where 1.0.0 appended an event that began its
324
+ stream. Genesis imports the rows that predate Lyra before a model's first write, so this meets
325
+ only rows written around Lyra; import them with Genesis or `lyra:repair` first.
326
+ - **The head is a lock in every mode that records events**, also with
327
+ `lock_aggregate_before_append` off: two writers of one record are ordered at the append. A
328
+ comparison without any lock needs `stream_heads = false` too.
329
+ - **A soft delete is recorded as the update it is, not as a destroy.** A soft delete (paranoia's
330
+ `paranoia_destroy`, which runs the destroy callbacks itself and keeps the row; discard's
331
+ `discard`; a retention rule's `:soft_delete`) writes `deleted_at` (or `discarded_at`), and that
332
+ write is recorded, as `Updated`, in every mode. The destroy callbacks it runs record nothing
333
+ themselves, as no row was deleted. 1.0.0 recorded a `Destroyed`: the record's stream ended while
334
+ its row stayed, so DualView, the mode gate and `lyra:repair` reported every soft-deleted record
335
+ as a mismatch, ES-NoProj lost the record from its view where Monitor keeps it behind its default
336
+ scope, and an update or a `restore` after it was an update after a destroy. Now the stream
337
+ follows the row: a soft-deleted record can be updated, touched and restored, each recorded as an
338
+ update, and a real destroy of it (paranoia's `really_destroy!`) is its destroy. The writes in a
339
+ soft delete's destroy callbacks name its write scope as their cause, which, recording no event
340
+ of its own, is a `SaveWithoutEffect` marker. A stream written by 1.0.0, which ends in a destroy
341
+ while its row stays, keeps that history: a touch of it writes the row and records nothing.
342
+ - **The stream heads are kept on PostgreSQL only.** They rely on its row locks (`FOR UPDATE`) and
343
+ `INSERT ... ON CONFLICT`. On SQLite they are not kept, and the history clauses are not enforced,
344
+ as in 1.0.0. A head is taken on the record's own database, where Lyra keeps its event store.
345
+ - **Tests that delete the event store's rows** should delete `lyra_stream_heads`' rows too. A head
346
+ left behind is corrected from the events before it refuses anything, but until then it orders
347
+ writers by itself.
348
+ - **ES-Lazy: a read inside a transaction catches up on a connection of its own** (see Fixed), so
349
+ while it does, it uses a second connection of the pool. Where none is free, or the connection
350
+ is pinned (transactional tests), the catch-up runs in the reader's transaction as in 1.0.0, with
351
+ 1.0.0's deadlock, and holds the catch-up lock to that commit, so other readers' catch-ups wait
352
+ for it (see Fixed for the hangs this could cause). A pool of twice the threads that read inside
353
+ transactions keeps the fallback rare. A read that only its own transaction's events are behind replays those
354
+ records itself and runs no catch-up.
355
+ - **ES-Lazy costs one query more per read while gaps are open,** including for the up to 300
356
+ seconds that a rolled-back transaction's ids stay gaps. The query grows with their number: on
357
+ a development VM, a read by id took about 0.5 ms longer with one gap and 3.3 ms longer with
358
+ 1,000, and the query alone about 22 ms with 10,000 in a store of 2 million events. With no gap
359
+ open a read sends the same two statements as before (the check and the read), except that in a
360
+ request or a job its check is sent every time instead of coming from the cache.
361
+ - A catch-up that cannot be applied now fails the reads that need it at once, where between
362
+ re-checks they went on from stale tables.
363
+
364
+ ### Security
365
+ - The example applications' lockfiles take the fixed releases of gems with published advisories
366
+ (puma, rack and crass; in the Solidus case study also addressable, msgpack, net-ssh, ransack,
367
+ rubyzip and view_component 3.27), with Ruby, Rails, Rails Event Store and Solidus unchanged, and
368
+ the Solidus case study's canonical tag no longer builds the request's format parameter into a
369
+ regular expression, which a `?format=` query could make fail or backtrack.
370
+
371
+ ### Known issues
372
+ This release does not change these:
373
+ - ES-Lazy, ES-Async and ES-NoProj: a locking read Lyra cannot order runs as in 1.0.0: raw SQL
374
+ (`find_by_sql` with `FOR UPDATE`), an eager-loading locking read (`lock.includes(...).references`),
375
+ one that selects no primary key, and, in ES-NoProj, a locked relation with a window (`limit`,
376
+ `offset`). A record that starts to match a locking read after its keys were read, more than
377
+ twice, is left out of it (ES-NoProj) or takes its row lock before its aggregate lock (ES-Lazy,
378
+ ES-Async), which can deadlock with a writer of it (`ActiveRecord::Deadlocked`);
379
+ - ES-Lazy: a catch-up that cannot be applied fails every read: events that no order of the log
380
+ makes valid under the table's constraints, or one that needs an event skipped for good;
381
+ - ES-Lazy: a write whose transaction stays open more than 300 seconds after its id became a gap,
382
+ one in flight more than 100 ids below the first event of a fresh checkpoint, and one in flight
383
+ when the application leaves ES-Lazy without the gate (`mode_transition_gate` off, or a forced
384
+ switch) can be missing from the tables for good;
385
+ - ES-Lazy: `eager_load` (and `includes` with `references`) loading records, `ids`, `count_by_sql`,
386
+ a relation's `cache_version` and raw SQL do not catch up before they read;
387
+ - ES-Lazy: in `REPEATABLE READ` and `SERIALIZABLE` a transaction does not see rows a catch-up
388
+ applied after its snapshot was taken, so it can read stale rows;
389
+ - ES-Lazy: a query repeated in a query-cache scope can be answered from that cache, as in Active
390
+ Record;
391
+ - ES-NoProj does not keep counter caches: a `belongs_to` with `counter_cache:` leaves the
392
+ parent's counter as it was when a child is created or destroyed, and the `touch: true` Rails
393
+ sends in the same statement is left out with it (it takes no lock either). A child's update
394
+ touches its parent.
395
+
396
+
10
397
  ## [1.0.0] - 2026-11-22
11
398
 
12
399
  ### Breaking
data/README.md CHANGED
@@ -59,8 +59,8 @@ seven configurations:
59
59
  - PostgreSQL. Lyra depends on the `pg` gem; the advisory locks behind Genesis
60
60
  and ES-Lazy, and id reservation in Hijack mode, need PostgreSQL.
61
61
 
62
- The gems are on RubyGems, all three at 1.0.0 (22 November 2026):
63
- `orfeas_lyra`, `orfeas_pam_dsl` and `orfeas_petri_flow`. Their names differ from the files to require:
62
+ The gems are on RubyGems: `orfeas_lyra` 1.0.1 (26 November 2026), and `orfeas_pam_dsl` and
63
+ `orfeas_petri_flow` 1.0.0 (22 November 2026). Their names differ from the files to require:
64
64
 
65
65
  ```ruby
66
66
  # Gemfile
@@ -70,9 +70,16 @@ gem "orfeas_petri_flow", "~> 1.0", require: "petri_flow" # optional, verificati
70
70
  ```
71
71
 
72
72
  Create the event store tables with the RailsEventStore 3 generator
73
- (`bin/rails generate ruby_event_store:active_record:migration`), then add an
74
- initializer. The [Migration Guide](docs/MIGRATION_GUIDE.md) walks through the
75
- whole path from Disabled to event sourcing.
73
+ (`bin/rails generate ruby_event_store:active_record:migration`), then Lyra's
74
+ own tables with `bin/rails generate lyra:install` (from 1.0.1): one migration
75
+ with them and two indexes on `event_store_events_in_streams`, where they are
76
+ missing: `(stream, id)`, which Lyra's backward reads use, and `(event_id)`, which
77
+ ES-NoProj's reference links use (Rails Event Store 3 creates both for a new event
78
+ store); and `config/initializers/lyra.rb` unless the
79
+ application has one (`--skip-initializer`). Without the migration Lyra creates
80
+ each table the first time it needs it, as before. The
81
+ [Migration Guide](docs/MIGRATION_GUIDE.md) walks through the whole path from
82
+ Disabled to event sourcing.
76
83
 
77
84
  ## Features
78
85
 
@@ -153,9 +160,10 @@ individual calls.
153
160
  - **[Architecture Overview](docs/ARCHITECTURE.md)** - System design and components
154
161
  - **[Switching Modes](docs/MIGRATION_GUIDE.md#switching-modes)** - Who holds the mode, and how to switch safely (the deploy-time rule)
155
162
  - **[Adopting Lyra](docs/MIGRATION_GUIDE.md#adopting-lyra-in-an-existing-application)** - What "non-intrusive" means, the costs, and a checklist for a new codebase
156
- - **[Performance](docs/PERFORMANCE.md)** - What each mode costs and why (measured on Lyra 0.9.0, with the SQL statements per write)
163
+ - **[Performance](docs/PERFORMANCE.md)** - What each mode costs and why (measured on Lyra 1.0.1, with the SQL statements per write)
157
164
  - **[Troubleshooting](docs/TROUBLESHOOTING.md)** - Common problems
158
165
  - **[Workflow Generator](docs/WORKFLOW_GENERATOR.md)** - The Petri net workflow models of Lyra's modes and their verification
166
+ - **[Concurrency Nets](docs/CONCURRENCY_NETS.md)** - The six Petri nets on which Lyra's lock orders were chosen and are checked
159
167
  - **[Changelog](CHANGELOG.md)**
160
168
 
161
169
  ### Theoretical Foundation
@@ -213,7 +221,11 @@ with the Olist orders, the BPI 2017 loan application) are in the replication pac
213
221
  |:------|:-----|:------------------------------|
214
222
  | *From ORM to OEM* (IEEE Software, 2026) | tag [`paperoem_final`](https://github.com/mpantel/lyra-engine/tree/paperoem_final), commit `b863d2e` (numbered 0.6.0, three commits after the [`v0.6.0`](https://github.com/mpantel/lyra-engine/tree/v0.6.0) release) | The benchmark run, its SQL statement trace, the per-trial values and 95% confidence intervals, and the harness as measured: [10.5281/zenodo.23648730](https://doi.org/10.5281/zenodo.23648730) |
215
223
  | *PAM* (Requirements Engineering, 2026) | tag [`v0.8.0`](https://github.com/mpantel/lyra-engine/tree/v0.8.0); `orfeas_pam_dsl` 0.8.0 on RubyGems | The policies, the testbed's models, tests and reports, the Solidus replication and the microbenchmark: [10.5281/zenodo.23649008](https://doi.org/10.5281/zenodo.23649008) |
216
- | *ORFEAS* (IEEE Transactions on Software Engineering, under review) | tag [`v0.9.0`](https://github.com/mpantel/lyra-engine/tree/v0.9.0); `orfeas_lyra` 0.9.0 on RubyGems | Replication package: [10.5281/zenodo.23646667](https://doi.org/10.5281/zenodo.23646667) (all versions: [10.5281/zenodo.17770613](https://doi.org/10.5281/zenodo.17770613)); benchmark results (CC BY-NC-SA 4.0): [10.5281/zenodo.23640846](https://doi.org/10.5281/zenodo.23640846) |
224
+ | *Privacy Attribute Matrix: Enterprise Modeling for Trustworthy Systems in Industry 5.0* (PoEM 2026, IFIP WG 8.1, Springer LNBIP) | tag [`v0.8.0`](https://github.com/mpantel/lyra-engine/tree/v0.8.0); `orfeas_pam_dsl` 0.8.0 on RubyGems | The case study the paper revisits: the scoped (79 lines), consent-extension (101) and unified (203) policies and the testbed's reports, in the same package as the *PAM* article: [10.5281/zenodo.23649008](https://doi.org/10.5281/zenodo.23649008) |
225
+ | *ORFEAS* (IEEE Transactions on Software Engineering, under review) | tag [`v0.9.0`](https://github.com/mpantel/lyra-engine/tree/v0.9.0); `orfeas_lyra` 0.9.0 on RubyGems | Published with the paper |
226
+ | *Objects or Events? A Methodology to Enable Different Views on the Same Software System* (doctoral thesis, University of the Aegean, 2026) | tag [`v1.0.1`](https://github.com/mpantel/lyra-engine/tree/v1.0.1); `orfeas_lyra` 1.0.1, `orfeas_pam_dsl` and `orfeas_petri_flow` 1.0.0 on RubyGems | Replication package, version 1.0.1: the code, the testbed, the replay applications, the harness and every run of the 1.0.1 campaign (research licence; Lyra, PAM DSL and PetriFlow MIT): [10.5281/zenodo.24390709](https://doi.org/10.5281/zenodo.24390709) |
227
+
228
+ The counts in the PoEM paper (18 candidate attributes in 10 models) are those of 0.8.0. Release 1.0.0 adds restriction of processing and the right to object (Arts. 18 and 21) and every Art. 9(1) special category, and its policy generator reports different counts.
217
229
 
218
230
  ### Research Areas
219
231
 
@@ -296,7 +308,7 @@ If you use this software in academic research, please cite:
296
308
  year={2026},
297
309
  note={Part of ORFEAS Framework},
298
310
  url={https://github.com/mpantel/lyra-engine},
299
- doi={10.5281/zenodo.17770613}
311
+ doi={10.5281/zenodo.24390709}
300
312
  }
301
313
  ```
302
314
 
data/Rakefile CHANGED
@@ -98,7 +98,7 @@ task default: :test
98
98
  TESTBED_DIR = File.expand_path("examples/aegean_epay_testbed", __dir__)
99
99
  # The testbed is in the replication package, not in the lyra-engine repository.
100
100
  TESTBED_MISSING = "The Aegean testbed is not in this checkout; it is in the replication package " \
101
- "(https://doi.org/10.5281/zenodo.17770613)."
101
+ "(https://doi.org/10.5281/zenodo.24390709)."
102
102
 
103
103
  # =============================================================================
104
104
  # Benchmark tasks
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module Lyra
7
+ module Generators
8
+ # bin/rails generate lyra:install
9
+ #
10
+ # Writes one migration with Lyra's own tables and the index Lyra's reads
11
+ # need on Rails Event Store's table, and an initializer when the
12
+ # application has none. Without the migration Lyra creates its tables the
13
+ # first time it needs each one, as before; with it they exist beforehand,
14
+ # appear in db/schema.rb or db/structure.sql, and the application's
15
+ # database role needs no right to create tables.
16
+ class InstallGenerator < ::Rails::Generators::Base
17
+ include ::ActiveRecord::Generators::Migration
18
+
19
+ source_root File.expand_path("templates", __dir__)
20
+
21
+ desc "Create a migration for Lyra's own tables (and an initializer, unless one exists)."
22
+
23
+ class_option :database, type: :string, aliases: %i[--db],
24
+ desc: "The database whose migrations_paths receive the migration"
25
+ class_option :skip_initializer, type: :boolean, default: false,
26
+ desc: "Do not write config/initializers/lyra.rb"
27
+
28
+ INITIALIZER = "config/initializers/lyra.rb"
29
+
30
+ def create_migration_file
31
+ migration_template "create_lyra_tables.rb.tt", File.join(db_migrate_path, "create_lyra_tables.rb")
32
+ end
33
+
34
+ # An existing initializer is the application's: it is never replaced.
35
+ def create_initializer
36
+ return if options[:skip_initializer]
37
+ return say_status(:exist, INITIALIZER, :blue) if File.exist?(File.join(destination_root, INITIALIZER))
38
+
39
+ template "initializer.rb.tt", INITIALIZER
40
+ end
41
+
42
+ private
43
+
44
+ def migration_version
45
+ ::ActiveRecord::Migration.current_version
46
+ end
47
+ end
48
+ end
49
+ end