V04 in Rails, a long-lived composition: wired once per cart, with Puma's threads handled at wiring time
Spray’s application is wired once, when it starts, and then runs: “the entire application is wired up once at the beginning when the application starts executing” (§3.11.3). Every Rails variant so far built its screen composition per request, pushed one input, rendered, and threw it away. The Rails post named this the largest gap between Rails and the ideal, and V03 made it measurable: every change loaded every value before it could apply one input. V04 keeps the cart’s composition.
One composition per cart
ProgrammingParadigms::LongLived is an execution model in the paradigms layer, beside PushLater and LiveUpdate. It knows threads and staleness and nothing about carts:
class LongLived
Entry = Struct.new(:instance, :lock, :seen)
def initialize(build:, version:, capacity:, reload: :load, begin: nil) ...
# Runs the block with the composition for `key`, built on first use, reloaded when stale.
def with(key)
entry = @lock.synchronize { @entries[key] ||= admit(key) }
entry.lock.synchronize do
current = @version.(key)
if entry.seen != current
entry.instance.input_port(@reload).send_event
entry.seen = current
end
entry.instance.input_port(@begin).send_event if @begin
result = yield entry.instance
entry.seen = @version.(key)
result
end
end
end
The application configures one in an initializer and the cart’s controllers run inside it:
Rails.configuration.x.cart_screens = ProgrammingParadigms::LongLived.new(
build: ->(cart_id) { Screens::Cart.new(cart_id:) },
version: ->(cart_id) { Cart.version_of(cart_id) },
reload: :reload, begin: :begin, capacity: 1000)
class CartItemsController < ApplicationController
def destroy
change { _1.input_port(:remove).push(params[:id].to_i) } # one input, on the cart's kept screen
end
end
change runs its block inside with(cart_id) and then renders the patches the outputs made, still inside. A GET sends show and renders what the values hold. Screens::Cart.new is V03’s, called once per cart.
Three decisions, and where Spray puts each
Threads. Puma serves requests on several threads, and a value is not thread-safe. Spray wants it that way: “it would be prudent for only one thread to be using each instance of such objects” (§3.9), and locks don’t go in the class, because “successful locking requires detailed knowledge of the specific threading” of the callers, which is coupling (§3.10.2). The registry holds one lock per kept composition and runs a request under it. Two requests on one cart take turns; two carts run at once. The arbitrator is this object, at wiring time. The linter’s R4 check flags a Mutex in any class, and the two lines that make them are accepted with that reason, listed by --list-accepted.
Other writers. A second Puma process, the checkout job, the catalogue’s “add to cart” button and the portal all write the same tables without going through this composition. carts.version is the cart’s write stamp: every row that belongs to a cart bumps it on commit.
class CartItem < ApplicationRecord
belongs_to :cart
# ala:accept r1 -- the write stamp is the store's own: a row's commit moves its cart's version and decides nothing above
after_commit { Cart.bump(cart_id) }
end
The registry reads the stamp before each request and sends reload when it moved, then records the stamp after the request so the composition’s own writes are not stale. A composition kept by another process sees the bump on its next request. No invalidation bus, one integer column. The linter flagged the four callbacks under ALA R1 as a lower record reaching another; they are accepted as the store’s own stamp, which decides nothing, and the acceptance is the design decision made visible.
A request’s own state. Flash and patches belong to one request; begin clears them. Values, landed rows and the summary belong to the cart and stay.
What the screen changed
reloadis V03’s load, every value pulled from its store.showasks the values to say what they hold (rows,contents, counts and ids again, from memory) for a GET. Each change input is now one push, with no load in front.- Three values gained a
showinput. The lines store gainedfetch→found, so a line thatUndorestores comes back intoCartLinesthrough the store the way an added line does. V03 never needed it: the next request reloaded. The integration tests found this on the first run, which is what they are for. - Checkout and the portal stay per request. Checkout runs inside a job as well as in requests, and neither has V03’s patches to make a kept composition pay. The asymmetry is a finding: the cart is the page with the most state and the most clicks, and it is where keeping the composition shows.
The tests that say it works: the same object serves a cart’s second request; a line added through the catalogue is on the cart page by way of the stamp; a raw update_all is invisible until something bumps the cart, and visible after, with the composition not rebuilt; each request starts with an empty flash. And five on the execution model alone with a fake composition: loaded once and begun each time, reloaded when the version moves outside a request and not when it moves inside one, two threads on one key take turns while two keys run at once, and the oldest key is dropped past capacity.
What it scores
| V03 | V04 | |
|---|---|---|
ala_lint default / --strict / --super-strict | 100 / 100 / 100 | 100 / 100 / 100 |
Rules met under --super-strict (of 10 checked) | 10 | 10 |
Accepted lines (--list-accepted) | 0 | 6 |
| Integration tests | 57 | 66 |
| Browser tests (Cuprite) | 5 | 5 |
Non-blank lines in app/ | 2,131 | 2,212 |
| Familiarity | 4 | 3 |
Two more findings came up and were fixed rather than accepted: the registry defaulted capacity: 1000, a product decision in a paradigm class (ALA R3), now configured in the initializer; and three values’ header comments did not list their new port.
What it costs at run time
- A GET on a kept cart runs no store queries except
Undo’s window check and the version read; a change runs the one write its input decided plus the stamp. V03 ran every load on every request. This is the run-time property the checklist’s cost section describes: an input costs what it reaches, and in V04 a bump reaches one line’s write and the patches. - Memory per kept cart: the values, their catalogue snapshots and the landed rows. A long tail of abandoned carts is what
capacitybounds; the oldest cart is dropped and rebuilt on its next request. - Correctness rests on the stamp. A write that bypasses the models is invisible until something bumps the cart.
- Multi-process deployments keep one composition per cart per process, each reloading on the other’s bump. Correct, and wasteful in proportion to the process count. Sticky sessions would make it one and are outside the design.
- Rendering happens inside the composition’s lock, so a slow template holds the cart’s turn. For this app the templates are small.
Later (2026-10-19)
A clone check joined the linter (R7, reported at the default tier and scored from --strict). It finds three pieces of logic written twice here: the checkout’s and the portal’s step flow, the product-row projection, and the two totals summaries, the duplicates later extracted in V05 to V07. They are left as they are in this archived variant, so it scores 100 / 99 / 99. The same day CartLines#find was written def find(id) = … or raise …, and Ruby attaches the or raise to the method definition, so a line it didn’t hold came back nil instead of raising; it is bracketed now, with a test. 67 integration tests and 5 browser tests.
What it shows
A Rails composition can be Spray’s: built once, kept, and run. What Rails lacked was not the shape but the two things a request-per-action framework had let the design skip, which thread may use an instance and who else writes the store. Both turned out to be wiring-time decisions with a home in the paradigms layer, and the domain abstractions did not change for either. I put familiarity at 3 because a Rails developer meets a registry of live objects with locks, which is a thing Rails apps go out of their way not to have; the trade is that every click costs what it changed.
The code is in ala_variants_ruby/v04-long-lived.