Skip to content

Commit 57edab8

Browse files
committed
An album and an artist are rows, and each carries its own cover
`set_artwork` and the `artwork` table are gone. `album` and `artist` are tables, `song.album_name` is a foreign key into the first, and the cover is an ordinary column on each rather than a row in a table of pictures about things that were not rows. **Keyed by the name, not by an id**, and that decides everything else. The mechanical reason first: `fill_auto` hands a mutation exactly *one* uuid, so no single verb can mint an album row and a song row in the same entry — and splitting it into two verbs would mean a client authoring a reference to a row it has not seen confirmed. The second reason is the rebase: two peers each adding "Water Music" offline would author two rows with two ids, one of which loses, and every song pointing at the loser points at nothing. A name is what both peers already agree on without being told. So there is no `create_album` and no `create_artist` — a row appears because a song named it, which is what lets `add_song` write it. Which is where the covers ride: `album_art` and `artist_art`, at the end of `add_song`'s arguments. The entry that knows a track's album is the entry that adds the track. The demo's `ART` stays the short table it was — a cover written once where it belongs, rather than copied onto every `Seed` row of a fifty-track record — and `art_of` looks it up per track on the way into the log. The scanner sends both empty until it learns to read an embedded tag. `art_to_write` is the one last-write-wins rule in the domain, and its three cases are indistinguishable by reading, so each is asserted: - no row yet, make one, with whatever the entry brought — often nothing, and a row with an empty `art` is right; - a picture replaces a picture, because somebody who picks a better cover means the newer one, and a total order is what makes "newer" a fact; - **nothing replaces nothing** — an entry with no picture leaves the one that is there, which is the case the scanner is in on every rescan and the one that wipes a library's covers one track at a time if it is wrong. Two things the schema found that reading would not have. **`song.album_name` had to be nullable, and the foreign key is what said so.** It went in `NOT NULL`, and every test writing a song with no album failed with `writing to song: FOREIGN KEY constraint failed` — Petros enforces these. A song on no record has nothing to point at, and `''` would need an album called `''` to point at; inventing one would put a nameless card on the albums page for every single somebody ever typed in. It also fixed a latent bug, since `albums()` had been grouping every album-less song under one entry named `""`. **Nothing has a foreign key into `artist`**, deliberately. `media.creator` is the one kind-neutral name for whoever made a thing — a song's artist, a sermon's speaker, a podcast's show — and a foreign key from it into a table called `artist` would name two of those three wrongly. So `artist` holds what is *known about* a creator, joined by name, and `artists()` still reads its names from `media`: a new kind appears in that list without the query learning about it. `albums()` is the same shape — which albums exist is the songs' answer; the table is asked only for the picture. Deleting a mutation variant is forbidden, and the exemption here is factual rather than an override: `set_artwork` was missing from `peer!` for its entire life, so every call was refused at apply time and no log anywhere has ever held a `SetArtwork` entry. `log-compat` refused the removal by name and `--write` recorded the new surface deliberately — six verbs now, with `AddSong` two arguments longer, which is an addition the log survives. **No `SCHEMA_VERSION` bump, and this is what that costs.** `song` lost a column and gained another, which is exactly the shape change the bump exists for — so an existing database keeps its old `song.album`, `migrate` will not alter it, and every query breaks on the missing `album_name`. Anyone holding a database reinstalls. That was the call ("still alpha"); it is a one-line change to make the day somebody's library is worth keeping, and it is noted in CLAUDE.md as such rather than left to be discovered. Verified: whole suite green with `--all-features`, clippy and fmt clean, and `log-compat` reports the rebuilt module compatible with the recorded surface. Each new assertion falsified against the line it holds — breaking either half of `art_to_write` fails the cover test naming the two URLs, and pointing an album-less song at `""` fails four tests with the foreign key error above. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XcBd33WHuwVJf6eCw7CpeG
1 parent d94e62d commit 57edab8

15 files changed

Lines changed: 494 additions & 225 deletions

File tree

‎CLAUDE.md‎

Lines changed: 126 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ domain/ the domain — the ONLY apply
3131
a mutation takes `ctx: &Ctx` for who authored it
3232
tests/conformance.rs the native and wasm builds of `apply`, compared
3333
tests/converge.rs the domain against a simulated fleet
34-
tests/read_model.rs library() and favorites() against rows apply wrote
34+
tests/read_model.rs library(), the covers and the playlists, against
35+
rows apply wrote
3536
src/lib.rs …and, under `cfg(wasm32)`, the module's ABI
3637
src/foreign_client.rs the client a foreign caller sees (feature `foreign`)
3738
src/wasm_app.rs the App whose `apply` is a module (feature `foreign`)
@@ -59,6 +60,10 @@ iced/ the desktop and browser client
5960
the socket, and the one rule about making a sound
6061
src/player.rs what is playing — an <audio> element in a browser,
6162
and nothing at all on the desktop
63+
src/covers.rs a cover, fetched once and shrunk before the renderer
64+
ever sees it; two caches, one `want`/`handle` pair
65+
src/art.rs …and the square derived from the name, for the eight
66+
albums in twelve that have no picture
6267
nix/readme.nix its section of README.md
6368
web/ the browser shell `nix run .#web` serves
6469
nix/default.nix the desktop package and `iced`
@@ -525,8 +530,10 @@ CreatePlaylist, AddToPlaylist, AddAllToPlaylist, RemoveFromPlaylist,
525530
RemoveMedia
526531
```
527532

528-
at run time, from `mutate`. `set_artwork` shipped that way for two commits.
529-
Three things made it invisible and each is worth knowing on its own:
533+
at run time, from `mutate`. `set_artwork` shipped that way for the whole two
534+
commits it existed — which is also what later made it the one verb that could
535+
be deleted rather than kept forever, since no log ever carried an entry naming
536+
it. Three things made it invisible and each is worth knowing on its own:
530537

531538
- **`mutations.txt` is not evidence a mutation can be applied.** It is
532539
generated from the module's schema section, which `#[mutation]` produces —
@@ -542,8 +549,9 @@ Three things made it invisible and each is worth knowing on its own:
542549
which is what eight of the twelve demo albums correctly do. A grid of
543550
gold squares is the same picture whether the feature works or has never
544551
once run, and that is the shape of bug to write a test for rather than
545-
read for: `domain/tests/read_model.rs` unwraps `set_artwork` and
546-
`iced/src/main.rs`'s `demo_covers` walks the demo's own boot.
552+
read for: `domain/tests/read_model.rs` unwraps every cover mutation rather
553+
than discarding it, and `iced/src/main.rs`'s `demo_covers` walks the demo's
554+
own boot.
547555

548556
The server and the iced peer link these; the phone runs the same source compiled
549557
to wasm and interpreted by `petros-wasm-host`, because that is the only peer
@@ -1162,7 +1170,7 @@ every row is one line. Four things about it are load-bearing:
11621170
entirely. The playing track is the one row drawn in the accent *color*
11631171
rather than filled with it, so it stays findable under either.
11641172

1165-
The Album column is the interesting one, because `album` is on the `song`
1173+
The Album column is the interesting one, because `album_name` is on the `song`
11661174
side table and the library row is deliberately kind-neutral. Rather than widen
11671175
`Item` — which would put a join behind every list, the thing `media` exists to
11681176
avoid — `track_details()` returns the song-only facts and the client joins them
@@ -1223,7 +1231,7 @@ header would be saying "1". Lengths there are `2 hr 30 min` rather than
12231231
length — `clock` is still what a row uses.
12241232

12251233
The client is a library with a sidebar: playlists, then albums, then artists.
1226-
Which of those a row belongs to is not a column on the library list — `album`
1234+
Which of those a row belongs to is not a column on the library list — the album
12271235
lives on the `song` side table precisely so that `media` stays kind-neutral —
12281236
so the grouping is four queries in the domain (`albums`, `artists`, `album`,
12291237
`artist`) rather than a wider `Item`. Both clients would then fold the library
@@ -1684,34 +1692,93 @@ fresh hand-off rather than a `media_previous_track`, and nothing does that
16841692
yet. And none of this has been run against a real Home Assistant from here —
16851693
the rules are tested, the six service calls are not.
16861694

1687-
## A cover is a row, because an album is not one
1688-
1689-
`artwork` is a table of its own, keyed by `(subject, name)` where `subject` is
1690-
`'album'` or `'artist'`. That shape is forced rather than chosen: an album is a
1691-
string on `song` and an artist is `media.creator`, so neither is a row for a
1692-
picture to hang on. On `song` a cover would be repeated once per track and two
1693-
tracks of one album could disagree about it; on `media` it would be a column
1694-
every kind pays for so that one kind can have a picture, which is the argument
1695-
that kept artwork out of the log in the first place.
1696-
1697-
**`file` is spelt exactly as `media.file` is** — a path under the media root or
1695+
## An album and an artist are rows, and each carries its own cover
1696+
1697+
They were not, and a cover had nowhere to live. An album was a string on `song`
1698+
and an artist was `media.creator`, so the first answer was an `artwork` table
1699+
keyed by `(subject, name)` — a table of pictures about things that were not
1700+
rows — and a `set_artwork` verb to write it. Both are gone. `album` and
1701+
`artist` are tables now, `song.album_name` is a foreign key into the first, and
1702+
the cover is an ordinary column on each.
1703+
1704+
**Keyed by the name, not by an id**, which is the decision everything else
1705+
follows from. Two reasons, and the mechanical one comes first: `fill_auto`
1706+
hands a mutation exactly *one* uuid, so no single verb can mint an album row
1707+
and a song row in the same entry — and splitting it into two verbs would mean
1708+
a client authoring a reference to a row it has not seen confirmed. The second
1709+
is the rebase. Two peers each adding "Water Music" offline would author two
1710+
rows with two ids, one of which loses, and every song pointing at the loser is
1711+
left pointing at nothing. A name is the thing both peers already agree on
1712+
without being told. So there is no `create_album` and no `create_artist`: a row
1713+
appears because a song named it.
1714+
1715+
**Which means `add_song` writes them**, and carries the two covers:
1716+
`album_art` and `artist_art` at the end of its arguments. That is what "the
1717+
cover arrives with the song" means — the entry that knows a track's album is
1718+
the entry that adds the track, and a picture reaches the log on it. The scanner
1719+
sends both empty until it learns to read an embedded tag or a `folder.jpg`.
1720+
1721+
**`art_to_write` is the one last-write-wins rule in the domain**, and it has
1722+
three cases that reading cannot tell apart, so `read_model.rs` asserts each:
1723+
1724+
- **No row yet — make one**, with whatever the entry brought. Usually nothing,
1725+
and a row with an empty `art` is right: the row is what a song points at, and
1726+
having no cover is a fact about the album rather than a reason not to have
1727+
one.
1728+
- **A picture replaces a picture.** `add_song` lets the *first* entry win on a
1729+
file, because adding a song twice is a mistake — but somebody who picks a
1730+
better cover means the newer one, and the log being totally ordered is what
1731+
makes "newer" a fact every replica reaches the same way.
1732+
- **Nothing replaces nothing.** An entry with no picture leaves the one that is
1733+
there. This is the case the scanner is in on *every* rescan, and getting it
1734+
wrong wipes a library's covers one track at a time.
1735+
1736+
…and the same picture again writes nothing at all, which is what keeps a
1737+
rescan of a thousand tracks a thousand reads and no writes: a `put` would
1738+
move `added_ms` and report a change to every maintained view watching.
1739+
1740+
**`song.album_name` is nullable, and a foreign key is why.** Petros enforces
1741+
these — the first version had it `NOT NULL`, and every test writing a song with
1742+
no album failed with `writing to song: FOREIGN KEY constraint failed`, which is
1743+
the schema being right rather than the tests being wrong. A song on no record
1744+
has nothing to point at; `''` would need an album called `''` to point at, and
1745+
inventing one would put a nameless card on the albums page for every single
1746+
somebody ever typed in. It also fixed a latent bug: `albums()` used to group
1747+
every album-less song under one entry named `""`.
1748+
1749+
**Nothing has a foreign key into `artist`**, deliberately. `media.creator` is
1750+
the one kind-neutral name for whoever made a thing — a song's artist, a
1751+
sermon's speaker, a podcast's show — and a foreign key from it into a table
1752+
called `artist` would be naming two of those three wrongly. So `artist` is a
1753+
table of what is *known about* a creator, joined by name where there is a row,
1754+
and `artists()` still reads its names from `media`: a new kind appears in that
1755+
list without the query learning about it, and a creator with no row is a
1756+
creator with no cover rather than one who does not exist. `albums()` is the
1757+
same shape for the same reason — which albums *exist* is the songs' answer, and
1758+
the table is asked only for the picture.
1759+
1760+
**`art` is spelt exactly as `media.file` is** — a path under the media root or
16981761
a whole URL — because a client already knows how to turn one of those into
16991762
something it can fetch, and a second rule for pictures would be a second thing
17001763
to get wrong.
17011764

1702-
**`set_artwork` is last-write-wins, which is the opposite of every other verb
1703-
here.** `add_song` and `create_playlist` both let the *first* entry win,
1704-
because adding the same thing twice is a mistake and the log is where the first
1705-
answer lives. A cover is not that: replacing one is the whole point, and
1706-
somebody who picks a better picture means the newer one. The log being totally
1707-
ordered is what makes "newer" a fact rather than a race — every peer replaying
1708-
reaches the same last write — so this is one `put` over a row keyed by the
1709-
pair, and `INSERT OR REPLACE` does the rest. Clearing a cover is `file` empty
1710-
rather than a `remove_artwork` beside it: a row with nothing in it and no row
1711-
at all are the same answer to `albums()`.
1712-
1713-
No `SCHEMA_VERSION` bump. A new table is `CREATE TABLE IF NOT EXISTS` and the
1714-
engine only rebuilds when an existing table's *shape* moves.
1765+
**No `SCHEMA_VERSION` bump, by decision rather than by accident.** `song` lost
1766+
a column and gained another, which is exactly the shape change the bump exists
1767+
for — so an existing database keeps its old `song.album`, `migrate` will not
1768+
alter it (`CREATE TABLE IF NOT EXISTS` does nothing to a table that is there),
1769+
and every query breaks on the missing `album_name`. The call was "still alpha":
1770+
nobody carrying a database has to be able to open it, so anyone holding one
1771+
reinstalls. Bumping it is a one-line change to make later, and it is the thing
1772+
to do the day somebody's library is worth keeping.
1773+
1774+
**`set_artwork` could only be deleted because it had never once run.** The
1775+
engine forbids removing a mutation variant — `log-compat` refuses it by name,
1776+
"the log still carries entries naming it, and every peer replays them" — and
1777+
that rule is right. The exemption here is factual rather than an override: the
1778+
verb was missing from `peer!` for its entire life, so every call was refused at
1779+
apply time and no log anywhere has ever held a `SetArtwork` entry. `--write`
1780+
records the new surface deliberately, and this was the one moment it could be
1781+
done honestly.
17151782

17161783
**Both clients draw it now, and the caches are different on purpose.**
17171784
`iced::widget::image` takes bytes or a path and there is no URL widget, so the
@@ -1764,8 +1831,34 @@ Three things that are each a decision:
17641831
wants a fresh demo deletes all four files, and it went green against a
17651832
stale one first.
17661833
- **What is cached beyond the session is bytes, never handles.** A `Handle`
1767-
holds decoded pixels and forty albums of those is tens of megabytes. The map
1768-
is the session's; the disk is the machine's.
1834+
holds decoded pixels; the map is the session's and the disk is the machine's.
1835+
- **The decode happens where the fetch is, and putting it anywhere else was a
1836+
visible stutter.** `Handle::from_bytes` does *not* decode — it hands iced the
1837+
encoded bytes and the decode runs inside whichever frame first draws them. So
1838+
each seeded portrait was a 1.2 megapixel JPEG decoded in a frame and then
1839+
4.8 MB of RGBA in the atlas for something drawn 132 pixels wide; seven
1840+
artists is 34 MB. iced evicts what a frame did not use, so opening an artist
1841+
page dropped six of them and going back decoded all six again — which is why
1842+
it presented as a hitch on *navigation* rather than on load.
1843+
1844+
`covers::BOUND` is 384, a shade under 3× the largest square this program
1845+
draws, and `Handle::from_rgba` is what reaches the renderer. Natively that is
1846+
the `image` crate on the thread the blocking fetch is already on — no new
1847+
crate, since iced's own `image` feature already put it in `Cargo.lock`, so
1848+
`cargoVendorHash` does not move. In a browser it is `createImageBitmap`,
1849+
the platform's decoder, off the main thread.
1850+
1851+
**Decoded from the fetched blob, never from the URL.** A canvas that has
1852+
drawn a cross-origin image is *tainted* and `getImageData` on it throws a
1853+
SecurityError, so going through bytes the page already holds is what makes
1854+
the pixels readable at all.
1855+
1856+
It is a bound and not a size: aspect is kept, so `ContentFit::Cover` still
1857+
does the cropping in `view` where that decision belongs. And what the disk
1858+
caches is still the *encoded original* — that cache is of the fetch, and
1859+
keeping the shrunk pixels would be ten times the disk to save CPU that is no
1860+
longer on the render thread, in a form that could not be re-shrunk if the
1861+
bound ever moved.
17691862

17701863
## The square when there is no cover
17711864

‎domain/mutations.txt‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
# build. A verb or an argument may be ADDED — a payload without a field decodes
33
# to that field's default. Removing or renaming a verb, or dropping or retyping
44
# an argument, breaks every entry already written, and the check refuses it.
5-
AddSong title:Text artist:Text album:Text duration_ms:Integer file:Text track:Integer part:Text catalogue:Text performer:Text bpm:Integer
6-
SetArtwork subject:Text name:Text file:Text
7-
CreatePlaylist name:Text
5+
AddSong title:Text artist:Text album:Text duration_ms:Integer file:Text track:Integer part:Text catalogue:Text performer:Text bpm:Integer album_art:Text artist_art:Text
6+
RemoveMedia id:Id(media)
87
AddToPlaylist playlist_id:Id(playlist) media_id:Id(media)
9-
RemoveFromPlaylist playlist_id:Id(playlist) media_id:Id(media)
8+
CreatePlaylist name:Text
109
AddAllToPlaylist playlist_id:Id(playlist)
11-
RemoveMedia id:Id(media)
10+
RemoveFromPlaylist playlist_id:Id(playlist) media_id:Id(media)

0 commit comments

Comments
 (0)