Skip to content

ViewState writes emit store jobs keyed by ClientKey; a write whose profile or client moved is dropped, never sent as another profile - #1026

Merged
GLinnik21 merged 9 commits into
mainfrom
sansio/store-jobs-viewstate
Oct 24, 2026
Merged

GLinnik21 merged 9 commits into
mainfrom
sansio/store-jobs-viewstate

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Closes #1024

A household's watched-state writes (mark watched, mark unwatched, remove from Continue Watching, and the fan-out to the same item on the other servers) no longer start the ViewState store's own worker, and they no longer read the server registry from the step or the worker.

  • The step decides from a borrowed ClientKeys and the press's profile.
  • A write leaves as a typed StoreJob::ViewWrite, naming the ClientKey it decided.
  • The app's StoreRunner starts the write only if that key still holds.
  • The fan-out reaches another server only through a client whose key still matches the step's snapshot (KeyedClients).

This closes a cross-profile write on main. A profile switch re-tokens the registry's Client objects in place, so a press queued behind another write, or a fan-out still running, could be sent with the next profile's token. It is wave 5, lane B of the Sans-IO series (docs/sans-io.md, "The store job lane").

What changed

The step (data/src/viewstate.rs):

  • Req carries key: ClientKey and profile: u32, captured at the press.
  • request and fan_out_origin_watched check keys.key(sid) instead of client_for(sid).
  • The profile comes from the Bridge as an argument, Bridge::profile_scope() = session::current_gen(), the way Search takes its household. The step reads no session.

One judgment, admit, runs when a queued write starts and when a refused write is tried again:

  • the profile moved → drop, retire its ledger entry, log;
  • the server left the registry → drop, retire, log;
  • the client key moved → drop, retire, log (viewstate: rk=.. DROPPED — server N was re-tokened while it was queued).

The third rule is stricter than first planned, and the review proved why. A profile switch re-tokens the registry in the commit that adopts the swap, but current_gen() moves on a later commit (publish_profile, from take_ready). For a frame or more, a real switch therefore looks to the step like a same-profile token refresh. Re-keying in that window would send the first profile's press under the second profile's token. A fails-first test reproduces it.

The cost: a same-profile token refresh also drops a press that was queued or refused across it. The read-back then shows the server's values, so nothing is written wrongly; the press is lost. #1025 tracks an identity that moves at the switch commit itself, which would let a same-profile refresh proceed again.

The job (data/src/viewstate.rs, data/src/stores/{viewstate,mod}.rs):

  • StoreJob::ViewWrite(Job<ViewWriteReq>, ViewWritePort), Direct, belongs to StoreId::ViewState.
  • The request carries the write, the origin's key and the ClientKeys snapshot the fan-out may use.
  • The body is finish_view_write_job. It runs perform on the origin, then fan_out through KeyedClients, all inside catch_unwind. The answer is posted outside the guard, so a panic answers a failed write and never a refusal.
  • kick emits the job and starts nothing.

KeyedClients (plex/src/plex/client_keys.rs) resolves a server's client only while its key equals the snapshot's. So a fan-out target re-tokened after the step gets no request.

The runner (app/adapters/stores.rs):

  • start_view_write / launch_view_write resolve the origin's key once at launch. A moved key refuses the job.
  • A parked write is resolved again when it runs.
  • The arms are in start, admit_home, retire_parked_details and run_held_where.
  • The held runner parks ViewWrite.

Drains (S3):

  • VIEWSTATE_FILLS is now [Browse, Hubs, ViewState].
  • The new RECONCILE_FILLS is [ViewState, ViewReconcile]. Bridge::reconcile_pump drains it with start_store_jobs_in, because an end-of-stream fan-out it queues starts ViewState's next write.
  • The order assertion covers all three lists.

Ledgers

Before After
ci/allow/workers/data-viewstate.txt 1 deleted (kick::spawn_small)
ci/allow/globals/data-viewstate.txt 5 deleted (request, fan_out_origin_watched, kick, fan_out, ask_sources client_for)
PINNED_LEDGER_TOTALS (workers/globals/forks/fxlanes) 12/73/58/11 11/68/58/11

No ledger line was added or edited.

Tests

All tests run on loopback: a fake PMS or a counting listener on 127.0.0.1. No real server or account is involved.

Fails-first:

  • a_write_whose_client_was_re_tokened_while_the_profile_still_reads_the_old_one_is_dropped_and_retired: the profile stays, and the token is swapped in place. On the tree before the fix it sent one scrobble (left: 1 right: 0); now it sends none.

Mutation checks, each restored:

  • The profile compare: if req.profile != profile → if false && .. fails a_write_whose_profile_moved_is_dropped_and_retired_wherever_it_is_judged, which keeps the keys unmoved so that it pins the profile rule alone.
  • fan_out / ask_sources going back to the raw client_for fails a_fanout_target_whose_key_moved_after_the_step_gets_no_request: the other server heard 3 requests instead of 0.
  • Removing the runner's key compare fails both runner key tests (one connection reached the sink).

New and replaced tests:

  • 8 in viewstate.rs: emit, refused-then-ladder, the drop rules, a reset's retired adapter, and a panicking worker.
  • the fan-out key test in viewreconcile_tests.rs;
  • 5 runner tests: started and answered once, not taped; a moved key not started; parked and kept by retire_parked_details; a held run; a parked write whose key moved is refused;
  • viewstate_controlled_tests.rs.

Targeted suites at 86baeeb, which merges main at 84a780d:

  • plx_data: 1238 passed, 0 failed.
  • plx_screens: 1706 passed, 0 failed.
  • the app crate: 1031 passed, 0 failed.
  • plx_plex: 732 passed, 0 failed.
  • hostsim, the whole app crate: 1144 passed, 0 failed.
  • The ledger tests and ci/check-deps.sh are green.

Gates

This PR's CI is the gate for make check, check-nodef, the replay fixtures and the ARM build. They were not run locally.

Review. One review, verdict FIX FIRST, with one blocker, two should-fix findings and two notes:

Doc-claim audit. It found five sentences made false, and all five are fixed:

  • reconcile_pump's drain and RECONCILE_FILLS;
  • "all five client_for reads";
  • the Direct admission list;
  • the refused-write wording, written to the post-B1 behaviour;
  • the step's one remaining registry read (server_ids()).

Live path

Checked in the macOS simulator against tests/mock_pms.py, with a placeholder token and plex.tv and Discover pointed at the mock. It was a mock-only run with no real account.

Before any key, the log showed:

  • plex: server slot 0 registered at 127.0.0.1:32611;
  • pms: server 0 version=1.41.0.0000-synthetic;
  • notify: roster servers=1 described=1 owned=1.

The run:

  • The hold menu on the first Continue Watching card took down, down to Mark as Watched, then ok.
  • The mock's request log showed exactly one write, GET /:/scrobble key=200622, with no unscrobble, followed by the read-back. Nothing more arrived 8 seconds later.
  • The app log read viewstate: rk=200622 watched → server 0 (sent to the runner), then watched ok=1 others=0. There was no DROPPED, no stores: WARN and no panic.
  • The episode left the deck after the read-back.

This is host-simulator evidence only, with no TV claim. A profile switch, a token refresh and a refused launch were not driven in the simulator; the host tests above cover them.

Not in this PR

🤖 Generated with Claude Code

GLinnik21 and others added 9 commits October 25, 2026 00:58
A recording or a replay of a Home boot reaches the detail hero's ViewState store command and the item menu's committed row only through effects::store and effects::app, and neither has an arm for them. The new test sends each effect, for all three writes and for record and replay, to a fresh controlled Bridge and asserts the refusal, that nothing is delivered, that the loop's item-menu queue stays empty, that no outbox holds a job and that no write is queued. A pin: it is green on the tree that adds it, and fails the day someone gives either effect a recording arm.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…worker

A step decides from a ClientKeys snapshot, and the worker it hands the work to runs later, after a profile switch may have re-tokened the registry's Client objects in place. KeyedClients holds the snapshot and a lookup fn pointer (a Send + Copy value a worker can build from its job's own keys), and client(sid) answers only while the registry's client still carries the snapshot's key. A slot the snapshot never held, a re-pointed slot and a moved credential all answer None.

Pure addition: no caller yet. Four unit tests, one per refusal and one for the match; swapping the filter for an instance-only comparison fails the credential test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s's profile

A scrobble, unscrobble, Remove from Continue Watching and the cross-server
origin-watched fan-out are statements about one household member's viewing
record. The step now captures what it needs to keep them that way at the press
and decides at the kick from the snapshot the Bridge hands it:

- `Req` records the server's `ClientKey` and the profile identity
  (`session::current_gen`, passed in by the Bridge as `Bridge::profile_scope()`,
  the way Search takes the household). The identity moves on seat, detach and
  sign-out, and not on a same-profile token refresh.
- `request` and `fan_out_origin_watched` read `keys.key(sid)` instead of the
  registry (-2 globals).
- `kick` drops and retires a write whose profile moved (logged); a write whose
  server is gone from the snapshot is dropped as before; the same profile with a
  re-tokened client is re-keyed and proceeds.
- `viewstate_run`, `viewstate_pump` and `reconcile_pump` take the profile as a
  trailing argument.

Tests are loopback only: a fake PMS on 127.0.0.1:0 counts the scrobbles. With the
profile comparison scratch-removed, the cross-profile test fails with
"nothing was sent under the new profile's token".

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…the app's runner

A scrobble, an unscrobble, a Continue Watching removal and the cross-server
origin-watched fan-out no longer start their own worker. The step decides from
the borrowed ClientKeys and the press's profile, and sends the runner one
typed job per write; the job's Port takes exactly one answer.

- StoreJob::ViewWrite (Direct admission, no tape record) carries the origin
  server and the key the step decided on, the write, and the whole ClientKeys
  snapshot it decided under. The runner starts it only through the client that
  key names, and refuses it (nothing left the device) when the registry has
  moved on, at the launch and again when a parked job runs.
- The worker reaches the title's OTHER copies only through KeyedClients built
  from that snapshot, so a profile switch that re-tokens a server between the
  press and the worker cannot send the lookup or the scrobble to it under the
  next profile's credential. The registry read is passed in by the runner, so
  no data-layer line names client_for.
- A refusal is NotStarted, not a failed write: no ledger change, no read-back,
  no hub refresh. The write goes back to the head of the queue after the same
  judgment any queued write gets (a moved profile drops and retires it, a
  re-tokened client re-keys it); only an unmoved client waits out the retry
  ladder. A panicking worker answers a failed write, never NotStarted, because
  a second scrobble would add a second viewCount.
- Stores::VIEWSTATE_FILLS gains ViewState in StoreId order, and
  RECONCILE_FILLS names the two outboxes Bridge::reconcile_pump now drains
  (an end-of-stream fan-out it queues starts the next write).
- Ledger: ci/allow/{workers,globals}/data-viewstate.txt are deleted (workers
  14 to 13, globals 73 to 70).

Tests: the job lane is graded port-driven (emission, the retry ladder, a
moved profile, a re-tokened client, a reset's outbox, a panicking worker); a
fan-out target whose key moved after the step receives no request (fails with
the raw registry read in fan_out/ask_sources: the server saw 3 requests); the
runner refuses a write whose origin key moved, live or parked (fails without
the key comparison in resolve).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
docs/sans-io.md gains the view-state write lane (the tenth store on the job
lane): the profile as a second step input, the fan-out through KeyedClients,
NotStarted as a non-failure, and the two fill lists; it no longer says
viewstate_run emits nothing. The module docs of viewstate.rs, the runner, the
reconcile rig and client_keys.rs, and the worker-inventory row in
docs/poster-scheduler-design.md, now say the runner starts the write.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…tever the profile reads (B1)

A profile switch swaps the registry's tokens in the frame the session adopts them, but the new
profile's identity (Bridge::profile_scope) is published by a later commit, so for at least one frame
a real switch looked to the step like a same-profile refresh and a queued write was re-keyed and
sent under the next profile's token. The step cannot tell the two apart, so admit now drops and
retires a write whose key moved (the design default, lead decision 4's fallback), and
restart_refused reduces to the admit judgment plus the ladder. The profile compare stays.

Fails first: the former re-key test, flipped to expect no scrobble and a retired ledger entry, sent
one scrobble on the previous tree (left: 1, right: 0). The two re-key unit tests and the two runner
tests flip to a drop; the profile test now keeps the keys unmoved so the profile compare is pinned
on its own. The module doc and docs/sans-io.md say the fallback and its cost.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Five sentences the lane left false: reconcile_pump drains RECONCILE_FILLS with start_store_jobs_in
(not start_store_jobs_of) and joins the fan-out list; the deleted globals file held five client_for
reads, not three; StoreJob::admission() names the view-state writes under Direct; a refused write
is judged again (a moved profile, a vanished server or a re-tokened client drops it) before it
waits out the ladder; the step reads no client_for but fan_out_origin_watched still reads
server_ids().

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Conflicts, all of the "keep every arm of both sides" kind:
- data/src/stores/mod.rs: StoreJob gains ViewWrite beside MetaSeason and MetaEpisodes, and all
  three are Direct in admission().
- app/adapters/stores.rs: start, admit_home, retire_parked_details and run_held_where take the
  ViewWrite arm and the two Metadata arms; the module doc names both lanes.
- docs/sans-io.md: Metadata M2 is the tenth store on the job lane and the view-state writes the
  eleventh; the fan-out drain list and the admission list name both.
- tests/test_harness.py: PINNED_LEDGER_TOTALS recomputed from the # count: headers at the merged
  tree: workers 11, globals 68, forks 58, fxlanes 11 (the comment lines of both sides kept).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The profile, vanished-server and moved-key branches each logged a DROPPED line and retired the
ledger entry; drop_queued does both once. The log strings are unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@GLinnik21
GLinnik21 merged commit d51aff4 into main Oct 24, 2026
19 checks passed
@GLinnik21
GLinnik21 deleted the sansio/store-jobs-viewstate branch October 24, 2026 23:32
GLinnik21 added a commit that referenced this pull request Oct 24, 2026
Metadata M2 (#1023) and ViewState (#1026) landed after the base. Both lanes and this one
added their own test-only LiveKind, took PINNED_LEDGER_TOTALS down from the same base, and
extended the same exhaustive StoreJob matches.

- LiveKind = {Hubs, MetaSeason, BrowsePage}. held() runs all three live, so the season
  switch stays live for menu_play_season_tests and the page read for the library route
  rigs and session_dispatch_tests. park_page_reads() sets {Hubs, MetaSeason}: it parks
  only the page read. held_all() stays [].
- Every StoreJob match in adapters/stores.rs (start, admit_home, retire_parked_details,
  run_held_where) lists both lanes' kinds.
- PINNED_LEDGER_TOTALS = workers 9, globals 66, forks 58, fxlanes 11, recomputed from the
  `# count:` headers of the merged tree (main is 11/68; this lane takes 2 and 2), with
  every lane's comment kept.
- docs/sans-io.md and the stores.rs //! paragraph carry every lane's prose.
- Review notes N2 (the /// docs that named only the section-hubs read now also name the
  directory or page read) and N3 (one sentence: an Addr is not an identity across kinds).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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.

ViewState writes: emit store jobs keyed by ClientKey, so a write is never sent under another profile's token

1 participant