Skip to content

service: push owner address changes over the websocket and localize the owner notices - #328

Merged
max-tet merged 7 commits into
mainfrom
fix/clayde/owner-email-live-and-i18n
Nov 28, 2026
Merged

max-tet merged 7 commits into
mainfrom
fix/clayde/owner-email-live-and-i18n

Conversation

@ClaydeCode

Copy link
Copy Markdown
Contributor

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_email changed 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_pending
  • clear_email

These paths send nothing because they change nothing:

  • confirm_email when the candidate moved on before promotion
  • the no-op branch of clear_email, when both fields are already empty
  • resend_verification

Each 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_update broadcasts the row to every connected socket.

WebSocket contract (a sundial PR depends on this exact shape)

  • message_type: "owner_user_update"
  • message: an OutputUser, the same shape GET /protected/users/me returns:
{
  "id": 1,
  "username": "owner",
  "display_name": "Owner",
  "email": "[email protected]",
  "pending_email": "[email protected]",
  "pending_email_sent": true,
  "role": "owner"
}

email and pending_email may be null. A client can treat a push and a REST fetch of the row the same way. test_the_push_carries_exactly_the_output_user_fields pins the serialized message, and each per-path test asserts that the push equals GET /me.

2. Localize the three owner notices and call the instance a Shard

_notify relays free-form subject and body text to the controller's api/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, which set_email and resend_verification already pass along for the confirmation-link mail. German goes to "de"; English goes to None and 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.py adds tests for:

  • Pushes. Each of the four changing paths pushes exactly one row equal to 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.
  • Notice language. Each notice is German for a stored "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:

  • disk space: the local /tmp is a small tmpfs
  • docker prune: timed out
  • two caplog tests: they failed only because the local run used -p no:logging, and pass without it

CI runs the full suite.

Review panel

The reviewers ran one at a time, each as a fresh agent.

  1. Adversarial reviewer. No blocking findings. I fixed three non-blocking ones in def60f8:

    • Pushes in the unverified set_email branch and in clear_email went out after the controller mirror. Concurrent changes could then arrive out of order, so they now go out right after the commit.
    • The push-capture fixture depended on fixture order, and now depends on app_client explicitly.
    • The exact-payload test now asserts that it read the only queued message.

    Raised for follow-up:

    • The pairing fallback in identity.py (_write_unsent_candidate) rewrites pending_email without a push. It is outside this module and runs during first pairing, so it is left as is.
    • A socket that stays open after its terminal is unpaired now also receives the owner's address. This widens the existing revocation gap tracked in #294; it does not create a new one.
  2. 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.

  3. 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:

    • per-notice copy builders, so the long text blocks no longer split confirm_email
    • keyword-only english / german in _notify
    • _Notice as a frozen dataclass, consistent with ConfirmResult
    • consistent placement of the send

    Not taken:

    • Renaming the signal to on_owner_user_update: the name was agreed in advance.
    • Matching regional de-* tags: the shard follows the controller's exact-"de" rule, and sundial only stores en/de.
    • The leftover em dash and lowercase "shard" in the English removal body: deliberately left unchanged.

🤖 Generated with Claude Code

ClaydeCode and others added 5 commits November 27, 2026 14:47
…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]>
Comment thread shard_core/service/owner_email.py Outdated

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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread shard_core/service/owner_email.py Outdated
log.error(f"could not notify the owner about an address change: {e}")


def _removal_notices() -> tuple[_Notice, _Notice]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@ClaydeCode

Copy link
Copy Markdown
Contributor Author

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.

Comment thread shard_core/service/owner_email.py Outdated
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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Narrative comment, remove.


async def _notify(subject: str, body: list[str]):
"""Best effort: a failed courtesy message must not block the change."""
async def _notify(notice: _Notice):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does very little now. Consider inlining it. Ist your judgement.

Comment thread shard_core/service/owner_email.py Outdated


# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@ClaydeCode

Copy link
Copy Markdown
Contributor Author

Fixed in 344e019:

  • `_notify`'s docstring and the comment above the three notice functions were both narrating the change relative to the design the previous commit already replaced — trimmed to just the one fact still worth stating (the `"en"` fallback matches the verification mail's own convention).
  • On inlining `_notify`: kept it as-is. It's thin now, but it's the same try/except+log pattern at all three call sites — inlining would triplicate that error handling rather than remove real complexity, and a future change to how a failed notify is logged would need three edits instead of one.

@max-tet
max-tet self-requested a review November 28, 2026 11:00
@max-tet
max-tet merged commit 157043b into main Nov 28, 2026
7 checks passed
@max-tet
max-tet deleted the fix/clayde/owner-email-live-and-i18n branch November 28, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants