Skip to content

fix: stop a superseded latest_wins call from changing the page - #324

Merged
JuroOravec merged 2 commits into
mainfrom
fix/superseded-call-actions
Oct 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/superseded-call-actions

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #306

What changes

With @event(latest_wins=True), a newer call cancels the older one and rejects its promise. Before this change, the bridge kept running the older call's remaining actions anyway:

  • A blocking delay held the newer call back in the queue.
  • The older call's delayed Dispatch, State, URL changes, Redirect and Data still ran.
  • Its wait=False actions still fired, even after the older call had finished.
  • $loading() and citry:events:after reported the call as a success.

Now a superseded call changes nothing more on the page. That follows the maintainer's decision for this issue, which includes wait=False actions and Redirect.

How the bridge (packages/js/citry-client/src/citry-events-vue.ts) does it:

  • The call remembers it was cancelled. It records its cancellation error (Job.cancelError). Cancelling also stops the delays of its wait=False actions, even when the call has already finished. The bridge keeps calls whose wait=False actions have not finished (detachedJobs), so a newer call can supersede them too.
  • Waiting ends at once. The delay helper stops waiting when the call is cancelled, so the queue moves on to the newer call immediately.
  • Every remaining step checks the cancellation. continuationCurrent, the hoisted State commits, and the step after preflight reject a cancelled call, so none of its remaining actions start. The preflight check comes before the accepted-result count moves, so a cancelled result does not count as a newer one.
  • The outcome is reported as stale. Each wait=False action that had not finished fires citry:events:stale with reason superseded, with no console error. A call cancelled after its last action ran is reported as cancelled, not as a success.
  • dispose() stops sourceless delays. It now stops the delays of applyActions calls made without a component.

The committed bundles packages/py/citry/citry/_vue/events.js and runtime.js are rebuilt from the source.

Spec and docs

  • packages/protocol/events/v1/spec.md has a new "Superseded calls" section. It says what happens to a queued call, an in-flight call and an accepted result, for every action type, Redirect included. It also says when the stale events fire, and that supersession takes effect when the newer call is queued, even if the newer call fails. Finally, it lists what supersession does not change:
    • the server still runs the call;
    • a commit already under way finishes;
    • only calls from the same component instance supersede each other;
    • applyActions is never superseded.
  • The latest_wins docstring and the superseded row of the stale reasons table in docs_site/content/reference/browser-apis.md say the same.
  • The protocol version stays 1.

Tests

There are 17 new tests in packages/js/citry-client/test/vue-events.test.mjs:

  • the issue's reproduction;
  • a delayed blocking action and a delayed wait=False action, for each of Dispatch, Redirect, PushUrl, ReplaceUrl and State;
  • delayed Render and Data;
  • no success reported for a call superseded during a delay, or by its own last action;
  • a call superseded by its own first State commit;
  • dispose() stopping a sourceless applyActions.

The wait=False tests make the newer result arrive after the old action is due, so only supersession can stop the action. The tests that wait for acceptance use a deterministic signal from preflightResult.

Against main's bridge source, all 16 tests that cover the fix fail. Against this branch, all 78 tests in the file pass.

Evidence

Run locally, all passing. These are the non-cargo phases of scripts/check.py, run by hand:

  • uv lock --check, ruff check, ruff format --check
  • mypy on the gate's paths, and mypy --platform linux
  • the citry-client phase: vue-types check, tsc, biome, and the node tests
  • protocol contracts, protocol Python copies, events protocol JavaScript
  • scripts/validate.py

PR CI runs pytest and the cargo phases. This change has no Python logic and no Rust, and the local Python suite would first need a citry_core rebuild for #318.

Review

An independent adversarial review, run twice with prose passes. It found these problems, all now fixed:

  • a finished call's pending wait=False action was still running;
  • the hoisted State gap;
  • loose test timings;
  • missing Render and last-action tests;
  • spec gaps.

#307 is stacked on this branch and must merge after it.

🤖 Generated with Claude Code

With @event(latest_wins=True), a newer call cancelled the older one and
rejected its promise, but the bridge kept running the older call's
remaining actions. A blocking delay, a delayed Render included, held the
newer call back in the queue until it ended.

After that, the delayed Dispatch, State, URL, Redirect and Data actions
still ran, and wait=False actions still fired, even after the older call
had finished. $loading() and citry:events:after reported the call as a
success.

A cancelled call now records its cancellation error (Job.cancelError),
and cancelling a call also stops the delays of its wait=False actions,
even when the call has already finished; the bridge keeps finished calls
that still have such actions pending (detachedJobs), so a newer call
can supersede them. The delay helper stops waiting when the call is
cancelled, so the queue moves on to the newer call at once, and
continuationCurrent, the hoisted State commits and the step after
preflight reject a cancelled call, so none of its remaining actions
start. Each wait=False action that had not finished fires
citry:events:stale with reason superseded, without a console error. A
call cancelled after its last action ran reports as cancelled, not as a
success. Delays of applyActions calls made without a component are now
stopped by dispose().

The Events spec gains a "Superseded calls" section that states this for
every action type, Redirect included, and what supersession does not
change. The latest_wins docstring and the stale-reason table say the
same.

Fixes #306

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 278d092 into main Oct 24, 2026
14 of 26 checks passed
@JuroOravec
JuroOravec deleted the fix/superseded-call-actions branch October 24, 2026 15:17
JuroOravec added a commit that referenced this pull request Oct 24, 2026
…ives

When an Events handler's Render replaced its component, the bridge
marked the call as having remounted its own sender and kept running its
later actions, so a delayed wait=False Dispatch, URL change or State
refresh still ran after the page accepted a newer result from the
component that replaced it. The check compared only the retired
instance's count of accepted results, which the new component's
results never raise.

The bridge now also counts the results it accepted at each spot on the
page (stableId), whichever instance is mounted there, and records the
count when a call's own result is accepted. After that call's Render
replaced the caller, a delayed action other than Redirect runs only
while the count is unchanged. With no newer result, the delayed actions
still run, as before. An applyActions on the new component counts once
its citry:events:before listeners let it apply, and dispose() clears
the counts.

A delayed Redirect now survives a newer result in every case, with or
without a replacement: it reports a result the server already produced.
Like every action of a cancelled call, it is dropped when its call is
superseded, its caller is removed, or the client is disposed.

The Events spec's "Order and timing" section states the two rules once
(a cancelled call changes nothing on the page; a newer result drops the
rest of an older one, except a redirect), then gives one table of what
happens in each case and the details: error results count, only results
for the component at the caller's place count, actions listed before the
Render, applyActions, and unknown action types. The browser APIs page
and the actions page say when a delayed action is dropped. The
superseded row describes the behavior from the #306 fix (#324).

Fixes #307

Co-Authored-By: Claude Opus 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.

With latest_wins, a superseded call still runs its delayed actions and holds up the newer call

1 participant