Repository navigation
service: push owner address changes over the websocket and localize the owner notices - #328
Conversation
…nges Confirming, setting, discarding or clearing the owner's address changed users.email/pending_email without telling any connected terminal, so a second paired device kept showing the old state until its next fetch, and even the confirming device needed an extra request. Every owner_email path that writes the row now sends on_owner_user_changed with the row it wrote, and websocket.py broadcasts it as owner_user_update carrying OutputUser, the same shape GET /protected/users/me returns, so a client can treat a push and a fetch interchangeably. The send in set_email goes out before delivery is attempted: the candidate is already written and survives a failed delivery. The moved-on branch of confirm_email, the no-op branch of clear_email and resend_verification change nothing and send nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The three courtesy notices owner_email relays (address removed, address changing, address confirmed) were English-only, although the controller's relay sends subject and body verbatim and the shard already knows the owner's stored UI language, which it passes for the confirmation-link mail. _notify now takes the stored language plus an English and a German notice and picks German for "de", English for anything else, the same fallback the controller applies to the confirmation mail. The notices also called the owner's instance "your Freeshard". Freeshard is the product; the instance is a Shard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
In set_email's unverified branch and in clear_email the push went out after the controller round-trip, so a concurrent change that committed later could push first and be overtaken by the older row. Push right after the commit, as confirm_email already does. The test fixture capturing owner_user_update now depends on app_client, which replaces ws_worker on reload, and the exact-payload test asserts it reads the only queued message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The German notice tests checked only the subject and first line, so a "Freeshard" slipping back into a later paragraph went unnoticed; the already-empty clear test could pass without the request reaching clear_email at all. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The inline English and German copy split confirm_email's steps with two long text blocks. Each notice now has a builder next to _notify, which takes the two notices keyword-only so they cannot be swapped by position. _Notice is a frozen dataclass like the module's other value type. Test comments that restated the code are trimmed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
||
| async def _notify(subject: str, body: list[str]): | ||
| """Best effort: a failed courtesy message must not block the change.""" | ||
| async def _notify(language: str | None, *, english: _Notice, german: _Notice): |
There was a problem hiding this comment.
Oh, this is bad. If the caller knows the language, they can just pass the correct notice. This method is just for making a choice the caller should do.
| log.error(f"could not notify the owner about an address change: {e}") | ||
|
|
||
|
|
||
| def _removal_notices() -> tuple[_Notice, _Notice]: |
There was a problem hiding this comment.
These and the other notices are badly designed. Returning means ordering is part of the contract. Easy to get wrong. And if another language is added there are too many places to change.
The strings should live in a common place easily accessible by language. Perhaps a dict of something similar. Research best practices.
I am surprised devX review did not catch that.
_notify took two fully-built notices and picked between them by language at the point of sending, with each call site unpacking the english/german pair positionally to pass back in. That makes return-value ordering part of the contract (silently swappable) and does not scale: a third language would mean widening _notify's signature and touching every call site. Each notice is now a dict keyed by language code, and the caller (which already has the language) resolves its own notice before calling _notify, which just relays whatever single notice it is handed. Adding a language is now "add one key to three dicts", not "thread a new parameter through _notify and every caller". Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
Fixed in 9ca8bed: `_notify` now takes a single already-resolved `_Notice`; each of the three notices is its own dict keyed by language code (`"en"`/`"de"`), and the caller resolves its own notice before calling `_notify` — no more positional english/german pair threaded through a shared parameter list. A third language is now "add a key to three dicts," not "widen `_notify`'s signature and touch every call site." Fair catch on the DevEx review missing this — a lens built for naming/structure/comment-narration didn't have "is this API shape itself fragile" in its brief. Noting that for next time. All 61 tests in `tests/test_owner_email.py` still pass; ruff clean. |
| async def _notify(notice: _Notice): | ||
| """Best effort: a failed courtesy message must not block the change. | ||
|
|
||
| The caller has already picked the language — this only relays the result, |
|
|
||
| async def _notify(subject: str, body: list[str]): | ||
| """Best effort: a failed courtesy message must not block the change.""" | ||
| async def _notify(notice: _Notice): |
There was a problem hiding this comment.
This method does very little now. Consider inlining it. Ist your judgement.
|
|
||
|
|
||
| # One dict per notice, keyed by language code, so a third language is a line | ||
| # added to each dict rather than a new positional slot threaded through every |
There was a problem hiding this comment.
Remove history narration from comment.
Both _notify's docstring and the comment above the three notice functions explained the change by contrasting it with the design that already got replaced in the previous commit — useful while reviewing that commit, dead weight afterwards. Cut to the one fact that's still non-obvious from the code alone (the "en" fallback matches the verification mail's own convention) and dropped the rest. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
Fixed in 344e019:
|
Two fixes in
shard_core/service/owner_email.py.1. Push the owner's row over the websocket when the address changes
Until now nothing told connected terminals when
users.email/users.pending_emailchanged on the server. A second paired device kept showing the old state until its next fetch, and even the device that confirmed the address needed an extra request.A new signal,
signals.on_owner_user_changed, is sent with the row that was just written on every path that changes the address:set_email, both branches: the direct set when mail is disabled, and the pending-candidate set. The candidate push goes out before delivery is attempted, because the candidate is written and survives a failed delivery.confirm_email, only after the promotion actually wrote the row.discard_pendingclear_emailThese paths send nothing because they change nothing:
confirm_emailwhen the candidate moved on before promotionclear_email, when both fields are already emptyresend_verificationEach push goes out right after the commit and before the controller mirror. A slow mirror round-trip therefore can't reorder pushes.
websocket.send_owner_user_updatebroadcasts the row to every connected socket.WebSocket contract (a sundial PR depends on this exact shape)
message_type:"owner_user_update"message: anOutputUser, the same shapeGET /protected/users/mereturns:{ "id": 1, "username": "owner", "display_name": "Owner", "email": "[email protected]", "pending_email": "[email protected]", "pending_email_sent": true, "role": "owner" }emailandpending_emailmay benull. A client can treat a push and a REST fetch of the row the same way.test_the_push_carries_exactly_the_output_user_fieldspins the serialized message, and each per-path test asserts that the push equalsGET /me.2. Localize the three owner notices and call the instance a Shard
_notifyrelays free-form subject and body text to the controller'sapi/email_relay, which does no localization. The three notices (address being removed, address changing, address confirmed) were therefore always English. They now use the owner's stored UI language, whichset_emailandresend_verificationalready pass along for the confirmation-link mail. German goes to"de"; English goes toNoneand every other value, the same fallback the controller uses for the confirmation mail. The German copy uses informal du-form, matching sundial's German UI.The notices also said "your Freeshard". Freeshard is the product, and a single instance is a Shard, so the English copy now says "your Shard" throughout. The existing lowercase "your shard" phrases were left as they were.
The copy for each notice lives in a small builder next to
_notify(_removal_notices,_change_notices,_confirmed_notices), so the address flows stay readable.Tests
tests/test_owner_email.pyadds tests for:GET /me, including after a failed delivery. The moved-on confirmation, the no-op clear and resend push nothing. The exact serialized message is pinned."de"and English for no preference,"en"and"fr". No notice says "Freeshard".Ruff is clean. The full local suite passed except 4 tests unrelated to this change, all caused by the local environment:
/tmpis a small tmpfscaplogtests: they failed only because the local run used-p no:logging, and pass without itCI runs the full suite.
Review panel
The reviewers ran one at a time, each as a fresh agent.
Adversarial reviewer. No blocking findings. I fixed three non-blocking ones in def60f8:
set_emailbranch and inclear_emailwent out after the controller mirror. Concurrent changes could then arrive out of order, so they now go out right after the commit.app_clientexplicitly.Raised for follow-up:
identity.py(_write_unsent_candidate) rewritespending_emailwithout a push. It is outside this module and runs during first pairing, so it is left as is.Test adversary. 35 hand mutants, 33 killed. Every new logic path is covered: each send site, both must-not-send exclusions, resend, the message type, the payload fields, language selection at each call site, and argument swaps. The 2 survivors were copy-only edits to the second German paragraph, including a "Freeshard" put back in. 00e3b08 closes both by checking the German notices for "Freeshard" too; the same commit adds a missing status-code assertion to the no-op clear test.
DevEx / readability. Three blocking findings, all test comments that restated the code (two section headers and a fixture docstring). ab8dbac trims or removes them. The same commit takes these non-blocking suggestions:
confirm_emailenglish/germanin_notify_Noticeas a frozen dataclass, consistent withConfirmResultNot taken:
on_owner_user_update: the name was agreed in advance.de-*tags: the shard follows the controller's exact-"de"rule, and sundial only storesen/de.🤖 Generated with Claude Code