V04 in Rails, a long-lived composition: wired once per cart, with Puma's threads handled at wiring time

October 10, 2026 · ALA · V04 · Ruby · Rails · Puma · threads · checklist

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

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

V03V04
ala_lint default / --strict / --super-strict100 / 100 / 100100 / 100 / 100
Rules met under --super-strict (of 10 checked)1010
Accepted lines (--list-accepted)06
Integration tests5766
Browser tests (Cuprite)55
Non-blank lines in app/2,1312,212
Familiarity43

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

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.

← all posts