V51B, a shared editing story: one story for the cart and the order

October 10, 2026 · ALA · V51B · LiveView · stories · features · checklist

V51 places one story, UndoRemoval, on both the cart and the portal pages, and keeps editing as two stories written separately. V51B is V51 with one editing story for both pages, with the pricing each page needs in a story of its own. The B says what it is: an alternative organisation of the same Features layer, not a step beyond V51. V51 shows two small stories that stay separate, which R7’s “fewest abstractions” allows; V51B shows the larger reuse. Both are kept so the two shapes can be compared on the same requirements. The shared story is the subject below. The running trace that this variant was first built for, when it was called V52, has since been added to V50 and V51 in place; its sections stay here as the record of that build.

Two lines per page

A page’s to/4 and out/1 become calls into the paradigm:

defp to(socket, key, port, payload),
  do: Steps.to(socket, __MODULE__, @parts, key, port, payload, out(key))

defp out(key), do: fn s, output -> Steps.out(s, __MODULE__, key, output, &wire/3) end

Steps.to/7 records a hop from whatever output is being routed, or :outside for a browser event, a timer or a PubSub message, to {key, port}, and then sends the story its input. Steps.out/5 marks the output as the hop being routed, runs the page’s wire/3 clause, and if the clause sent it to no story, records that it landed on the page:

def to(socket, page, parts, key, port, payload, out) do
  Trace.hop(page, Process.get(:ala_hop) || :outside, {key, port})
  Process.put(:ala_routed, true)
  parts[key].input(socket, port, payload, out)
end

def out(socket, page, key, {port, _} = output, wire) do
  previous = {Process.get(:ala_hop), Process.get(:ala_routed)}
  Process.put(:ala_hop, {key, port})
  Process.put(:ala_routed, false)
  socket = wire.(socket, key, output)
  unless Process.get(:ala_routed), do: Trace.hop(page, {key, port}, :page)
  # restore previous, return socket
end

Which output is being routed lives in the LiveView process’s dictionary for the length of one clause and is restored after, so nested routing (a story’s output sent to a story whose output lands on the page) records each hop correctly. Trace is an ETS table the test helper turns on; Trace.hop/3 is a no-op without it, so the pages pay nothing in production. The stories did not change.

The suite draws, and fails

ExUnit.after_suite writes a Mermaid chart per page from the recorded hops and reports every declared output no hop left from, exiting non-zero if there is one. The portal, as it ran:

flowchart LR
  outside -->|"stock"| catalog
  outside -->|"stock"| order
  outside -->|"goto"| submit
  outside -->|"expire"| undo
  catalog -->|"requested → add"| order
  order -->|"removed → capture"| undo
  order -->|"review → review"| submit
  order -.->|"summary"| page_order_summary[page]
  submit -.->|"form"| page_submit_form[page]
  submit -.->|"po"| page_submit_po[page]
  submit -.->|"reference"| page_submit_reference[page]
  submit -.->|"step"| page_submit_step[page]
  undo -->|"expired → confirm_removal"| order
  undo -.->|"pending"| page_undo_pending[page]
  undo -->|"restored → receive"| order

Compare it with V51’s source-parsed chart of the same page: the shape is the same, and this one is a record rather than a reading. An edge from outside is a browser event, a timer or a PubSub message that reached a story; a dotted edge to page is an output the page’s clause kept (an assign, a flash, a stream).

What the first run found

Two declared outputs had no hop: the cart page’s ui: :tab, because no test switched a tab, and the portal’s undo: :expired, because no portal test let the undo window close. Both were wired in the source, so the source-parsed check saw a clause and was satisfied. Both were untested. V51B adds the two tests, and the second one found a real sequence worth having under test: the portal’s removal becoming final after the window, with the lines told to confirm it.

That is the difference between the two kinds of check. A source-parsed drawing answers “is there a clause”; a running drawing answers “did this output ever reach anything”. The second is what Spray’s R8 coverage wants, and it is also a test-coverage report for the wiring, which the source can’t give.

One editing story for two pages

V51’s UndoRemoval was already placed by both pages. Its editing was two stories, EditCart with three parts (lines, promo, totals) and EditOrder with one (OrderLines, which priced itself by volume tier), and the duplication sat in the state layer as much as the story layer: two line abstractions that both hold lines, change quantities and hand a removed line to undo. V51B now has one editing story, EditLines, placed by both pages, and the pricing each page needs in a story of its own.

Spray’s feature “creates instances of domain abstractions, configures the instances with feature specific details, and connects them together”, and “features can also have ports and be wired together” (§2.2). What the two pages configure differently is the row, the stream, the stock rule and the undo window; what they wire differently is the pricing story and what they do with saved, line and checkout_requested:

# the cart page
|> PriceCart.mount([pricing: pricing], out(:price))
|> EditLines.mount([cart_id: cart_id, stock_status: stock_status, stream: :cart_items], out(:edit_lines))

defp wire(s, :edit_lines, {:contents, contents}), do: to(s, :price, :contents, contents)
defp wire(s, :edit_lines, {:saved, item}), do: s |> to(:saved, :stash, item) |> put_flash(:info, "Saved for later")
defp wire(s, :price, {:checkout_started, summary}), do: to(s, :check_out, :start, summary)

# the portal page
|> PriceOrder.mount([pricing: pricing], out(:price))
|> EditLines.mount([cart_id: cart_id, stock_status: stock_status, stream: :order_lines], out(:edit_lines))

def grounded, do: [edit_lines: :saved, edit_lines: :line, edit_lines: :checkout_requested]
defp wire(s, :edit_lines, {:contents, contents}), do: to(s, :price, :contents, contents)
defp wire(s, :price, {:review, summary}), do: to(s, :submit, :review, summary)

The row is the page’s. EditLines.rows takes the page’s row component as a function and renders each line through it, passing the event names the story owns:

<.stream_list :let={{dom_id, row}} id={Atom.to_string(@stream)} stream={@streams[@stream]}>
  <.line_row component={@row} id={dom_id} row={row}
    on_quantity="update_quantity" on_set_quantity="set_line_quantity" on_remove="remove_item"
    on_gift_wrap="toggle_gift_wrap" on_save="save_for_later" on_wishlist="toggle_wishlist"
    {@row_attrs} />
</.stream_list>
<!-- the cart page -->
<EditLines.rows streams={@streams} stream={:cart_items} row={&cart_item_row/1}
  row_attrs={%{wishlist_ids: @wishlist_ids, gift_wrap_label: @texts.cart.gift_wrap_label, t: @texts.cart.row}}
  count={@summary.item_count} empty={@texts.cart.empty} />
<!-- the portal page -->
<EditLines.rows streams={@streams} stream={:order_lines} row={&order_line_row/1}
  row_attrs={%{t: @texts.order.row}} count={@summary.item_count} empty={@texts.order.empty} />

V51’s R5 finding was that a name fired in one module and matched in another is a contract the call tree can’t see, so the story must own both ends of its events. It still does: the literals are in the story’s template, and the event-gap check still reads them there. The page owns what a line looks like, which is the checklist’s UI-layout note (§7.14). The portal’s row offers no save, no wishlist and no checkout, so three of the story’s outputs can never fire there; the page says so in grounded/0, and both the wiring check and the trace honour it, while still failing on any other output that never routed.

Two things in the state layer made this fit under the linter’s twelve-function limit, which is what pushed V51 to split the cart in the first place. The absolute quantity the portal’s input sends is a second clause of update_quantity, and a line arriving at a quantity is a second clause of receive. OrderLines is gone, CartLines serves both pages, and a 40-line OrderTotals prices the portal.

The portal, as it ran, after the change:

flowchart LR
  outside -->|"stock"| catalog
  outside -->|"stock"| edit_lines
  outside -->|"goto"| submit
  outside -->|"expire"| undo
  catalog -->|"requested → add"| edit_lines
  edit_lines -->|"contents → contents"| price
  edit_lines -->|"removed → capture"| undo
  price -->|"review → review"| submit
  price -.->|"summary"| page_price_summary[page]
  submit -.->|"form"| page_submit_form[page]
  submit -.->|"po"| page_submit_po[page]
  submit -.->|"reference"| page_submit_reference[page]
  submit -.->|"step"| page_submit_step[page]
  undo -->|"expired → confirm_removal"| edit_lines
  undo -.->|"pending"| page_undo_pending[page]
  undo -->|"restored → receive"| edit_lines

One more hop per line change on each page, contents to the pricing story, which V51 kept inside one story. That is the cost of a story two pages can place, and the drawing shows it.

What the linter found, with a layer map

Scoring V51B with a layer map (now kept in the variant as lint.exs, so the score is reproducible) found one thing the first write-up of this post had missed: the trace kept “which output is being routed” in the process dictionary, and the linter’s R4 check calls that hidden state. It is right. The marker now rides in socket.private for the length of one clause and is restored on the way out, which is the same nesting the process dictionary gave, in the thing the paradigm already threads. Nine findings gone, two lines of paradigm changed, no story touched.

What it scores

V51V51B
Full checklist, by hand100100
ala_lint default / --strict / --super-strict, with the layer map100 / 100 / 100100 / 100 / 100
Rules met under --super-strict (of 10 checked)1010
Declared outputs that never routed under the suitenot known0
Stories placed by both pages12
Line abstractions21
Cart page lines, with template292322
Non-blank lines in lib/4,9164,661
Tests126129
Familiarity44

What it costs

Later: defects and the clone check (2026-10-19)

An experiment that made the same requirement changes to conventional and ALA variants exposed four defects here, now fixed with regression tests: the portal never said “Item restored”; paying while a removal was held settled the held item; the info and error flashes shared one id; and a removal held for undo was lost if the page closed inside the window (holds are now marked in the database and made final when the cart is next read).

A clone check then joined the linter (R7, scored from --strict). It found the checkout’s and the portal’s step machine (advance and goto) written twice; both now use the Transitions paradigm, with nothing accepted. Rechecking the rules met showed 9 of 10 under --strict: the linter read PriceOrder.mount/3, which assigns a built OrderTotals to the socket, as a pass-through. It already read the same thing written as a struct update as building a value, so it now reads LiveView’s assign that way too, and V51B meets 10 of 10 at every tier again. Later that day save for later was fixed too: a saved line is now marked in the database, so it stays in the Saved tab across a reload and is never charged, deducted or recorded at checkout. Now: 100 at every tier, 147 tests.

The step machine went into each variant’s own Transitions module, and the three did it differently: V50’s is a struct answering {:moved, to, machine} or :stayed, V51’s a struct answering {:moved, machine} or {:stayed, machine}, and V51B’s two plain functions answering the next state. The names and APIs are left as they are for now, so a reader comparing the variants meets the same idea three ways.

Later: outside change as a part (2026-10-21)

Each page used to subscribe through the on_mount hook Subscribed to Foundation.Broadcast’s topic and route what came in by hand: a handle_info clause per message kind, building the stories’ %{product_id, stock} change itself, and two clauses on each page to ignore catalogue edits. The fact format lived in three places, and a new kind of change meant a clause on every page. Those clauses are the outside -->|"stock"| edges in the portal drawings above.

Paradigms.ChangeFeed replaces them. It is the checklist’s “live subscription port”, in the layer Spray keeps for paradigm interfaces (§2.2). A page mounts it like a story, lists it in @parts as feed, and wires its outputs with wire/3 clauses:

|> ChangeFeed.mount([topic: Catalog.changes_topic(), facts: [:stock]], out(:feed))

defp wire(s, :feed, {:stock, change}),
  do: s |> to(:edit_lines, :stock, change) |> to(:catalog, :stock, change)    # the portal

It subscribes once connected and attaches one handle_info hook that sends each fact the page asked for through the page’s out, so the trace records feed → edit_lines as it records any other hop, and the portal’s running drawing now reads feed -->|"stock → stock"| catalog and edit_lines. Publishing is ChangeFeed.publish(opts, product), configured the same way and called by the page that makes the change: the cart hands it to SettleOrder as announce, and the product pages call it on saved. SaveProduct only saves now. ChangeFeed.facts/1 builds the facts in one place: the product itself, and %{product_id, stock}. The cart and portal ask only for :stock and ground feed: :product in grounded/0, which names the kind twice, and nothing checks that the two agree.

The move found a gap. A stock edit at /products reached the product list and no cart or portal, which ignored :product_updated. With the feed the fix was one word, :stock in the product pages’ publish config, and a test now edits stock at /products/:id/show/edit and sees an open cart say “Out of stock”. Still not live: a price, name or new product on an open cart or portal (no input takes them), and a deleted product anywhere (nothing publishes it). Foundation.Broadcast, Paradigms.Subscribed and the pages’ catalogue handle_info clauses are gone. Now: 100 at every tier, 10 of 10 rules, 152 tests.

Isn’t this just LiveView? No. LiveView keeps a process per open page and pushes rendered diffs from it to the browser, which solves page to browser. ChangeFeed solves world to page: it gets a change made somewhere else into the stories inside that process, as identity-keyed facts over Phoenix.PubSub, wired like any story. LiveView then re-renders the new state and sends the diff, as always. How close can a LiveView app come to ideal ALA? has the longer answer.

What it shows

The R8 picture in a LiveView app can be the code that ran, with no parser and no annotation, because every hop already passes through two functions the page owns. The Rails variants reach the same property by walking a built object graph; Elixir reaches it by recording the graph as it is traversed. In both the diagram cannot drift from the code, and in V51B it cannot drift from the tests either.

The code is in ala_variants_elixir/v51b-shared-story.

← all posts