Skip to content

feat(events): renew a State token once it is past half its lifetime - #858

Merged
JuroOravec merged 5 commits into
mainfrom
feat/state-token-renewal
Nov 24, 2026
Merged

JuroOravec merged 5 commits into
mainfrom
feat/state-token-renewal

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Closes #856

A page that is in use no longer gets stale_state when its State token gets old. When a call arrives with a valid token that is past half its lifetime (12 hours by default), Citry sends a new token with a full lifetime in a state action, the one it already sends when State changes, even when the handler did not change State. A component that gets no call in the second half of its token's lifetime still expires. The citry-events/1 protocol does not change: no new field or value.

How it works

  • verify_state_token now returns renew_after, the half-way point of the token's own lifetime (t + (x - t) / 2; a token from before 1.0.0 with "x": null counts as 24 hours). state_renewal_due() compares it with the server's clock.
  • The dispatcher sends the renewal through the same _resign_state step as a State change, so a call returns at most one token.

Citry skips the renewal:

  • when the call already returns a token (State changed, or the result renders the caller);
  • when the call has no caller instance, the client does not accept the state action, or the call is a form posted without JavaScript;
  • for detached handlers, whose new token could replace a newer one from a queued call;
  • when an earlier call in the same request already sent the caller a token, so a later read-only call cannot send older State over a change;
  • when a new token cannot be made (a lowered _max_bytes, a public value the browser cannot read, a cache that refuses the write). The call still succeeds, Citry logs a warning once per component class, and the current token keeps working until it expires.

With server storage, a renewal writes a new cache entry with a full time-to-live, written only after the state action is built. The old entry expires on its own. A render replayed from the render cache already signs its State again, so it needs no change.

Edge cases and tests

Case Behavior Test
Signed and server storage New token, full lifetime; server storage adds one entry TestStateRenewal
Call that also changes State One token, with the changed State test_a_call_that_changes_state_sends_one_token
Render of the caller No state action; the render's token is fresh test_a_render_of_the_caller_already_carries_the_new_token
Batched calls sharing a token Each caller renewed once; a later read never undoes an earlier change; a render of another instance counts test_a_batch_*, test_a_later_read_*, test_a_change_after_*, test_a_render_of_another_instance_*
GET with / without a token Renewed / nothing to renew TestGetHandlers in test_events_routes.py
Render-cache replay Token re-signed with the replay time test_a_render_replayed_from_the_cache_carries_a_new_token
Token from before 1.0.0 ("x": null) Due after 12 hours test_a_token_from_before_1_0_0_*, TestRenewalTime
Clock skew A token from a server whose clock runs ahead is renewed later, never refused test_a_token_minted_by_a_server_whose_clock_runs_ahead_still_works
Old token replayed after a new one Valid until its own expiry, as after a State change test_an_old_token_keeps_working_until_its_own_expiry
Renewal cannot be made Call succeeds, no orphan cache entry, one warning test_state_that_cannot_*, test_a_public_value_*, test_a_cache_that_refuses_*, test_a_failed_renewal_warns_once_*

Security (Mechanism 7)

Places touched: State tokens the browser stores and sends back, and Events responses carrying publicState. An independent reviewer checked every route from a token to a new token:

  • A copied token can now be kept alive by anyone who calls at least once per half lifetime. For components with a State-changing handler, a _model field, or a self-render, that was already possible. For a component whose handlers only read State, it is new. Handlers use POST by default, so a token reaches a URL, and so server and proxy logs, only for a handler that opts into GET (methods=("GET",) or _methods) and declares state; whoever reads those logs can keep that token alive. security.md, docs/design/events.md 7.1, and trust_boundaries.md now say so. Tokens still give no permission; every handler checks it.
  • publicState in a renewal holds only _public fields. callerRenderId reaches no new place.
  • Server-storage writes per request stay within the envelope's call limit and the State budget, as for a State-changing call.

Fixed during the review: a cache-write failure during renewal answered 500 on a read-only call; a failed state action could leave an orphan server-storage entry; a no-JavaScript form got a renewal its answer drops; the warning repeated on every call and hid unexpected errors; the docs overstated how long a page may sit idle and did not say that a renewal re-sends public State (a watcher on an object or list field of $state runs).

Left out of scope, and older than this PR: when a State change's state action cannot be built, server storage has already written the entry, and a form posted without JavaScript that changes State writes an entry its answer drops. The renewal path uses the safer order; the State-change path could follow in a small separate change.

Versioning

Nothing the 1.x promise covers breaks: the wire format is unchanged, each token still expires _max_age after Citry issues it, and VerifiedState gains an optional field with a default. It is a new feature, so it needs a minor release, 1.3.0, which the changelog entry targets.

Checks

  • Focused: the Events and State Python tests (2,934 passed, 68 skipped after the merge), ruff, mypy, and mypy --platform linux on citry and the typing contract tests.
  • python -m docs_site build-check --strict (0 errors, 0 warnings) and scripts/validate.py pass on the merged branch.
  • Before the merge, the Python phases of the fast profile passed. The cargo and pnpm phases are skipped locally because no Rust or JavaScript changed; CI runs them.

Merged with main after #855

#855 (State settings docs) has merged, and this branch now contains it (merge commit, no rebase or force-push). The renewal text lives in #855's "Set State lifetime" subsection of docs_site/content/events/state.md (anchor #max-age), in its format. security.md, versioning.md, cache-backends.md, and the Component.State docstring keep #855's wording with the renewal facts added.

Maintainer decision

Accepted: a copied token for a component whose handlers only read State can now be kept alive while someone keeps calling. Handlers use POST by default, so the token reaches a URL only for a GET handler that declares state; #761 tracks moving that token into a header. This PR documents the trade-off. No opt-out setting is added.

🤖 Generated with Claude Code

JuroOravec and others added 3 commits November 24, 2026 13:57
A call that arrives with a valid State token past half its lifetime now
gets a new token with a full lifetime in a state action, even when the
handler did not change State. A page in use no longer gets stale_state;
a page left idle still expires after _max_age. The protocol is unchanged.

The renewal is skipped when the call already returns a token (State
changed, or a render of the caller), when it has no caller instance or
the client does not accept the state action, for detached handlers, when
an earlier call in the same request already sent the caller a token, and
when the token cannot be made again (logged, the call still succeeds).

Closes #856

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A renewal that fails for any reason (including a cache that refuses
  the write) keeps the current token instead of failing the call. The
  server-storage entry is written only after the state action is built,
  so a dropped token leaves no entry behind.
- Skip renewal for a form posted without JavaScript, whose answer drops
  state actions. Warn once per component class when renewal fails.
- Docs: state the real guarantee (a component that gets a call at least
  once per half lifetime keeps working), that renewal is per component,
  that detached calls do not renew, and that a copied or logged token
  can be kept alive even on a component whose handlers only read State.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ighten the renewal docs

- A renewal failure that is not a ValueError or OSError logs its traceback,
  and the once-only warning is kept per component class and error kind.
- Document that a renewal sends the public State again, so a watcher on
  an object or list field of $state runs; state the renewal point as half
  of the token's lifetime rather than a fixed 12 hours.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec and others added 2 commits November 24, 2026 15:31
…enewal

#855 turned the State guide into a "State settings" section with one
subsection per setting. The renewal text now lives in the `_max_age`
subsection, in that format, and the other pages keep #855's wording
with the renewal facts added.

Conflicts resolved:

- docs_site/content/events/state.md: #855's structure and anchors win.
  The "Set State lifetime" subsection says when a token is renewed, what
  does not renew it, and what a renewal sends; the "Choose where data
  lives" row says a call in the second half of the lifetime gets a new
  token.
- docs_site/content/security.md: points to the `_max_age` subsection
  for the setting and keeps the renewal and replay explanation.
- docs_site/content/about/versioning.md, docs_site/content/performance/
  cache-backends.md: #855's text plus the renewal sentence.
- packages/py/citry/citry/component.py: the State docstring keeps #855's
  per-setting list and its `_max_age` entry gains the renewal sentence.

Wording fix: a token reaches server and proxy logs only for a handler
that opts into GET (`methods=("GET",)` or `_methods`) and declares
`state`; handlers use POST by default. security.md, design section 7.1,
and the State token row in trust_boundaries.md now say so.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Put the advice on choosing `_max_age` ahead of the side effects, define
"renewal" where it first appears, explain `detached=True`, and say that a
renewal Citry cannot make does not fail the call. The versioning,
cache-backends, and API reference text no longer implies that every call
renews a token.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 519cbcc into main Nov 24, 2026
33 checks passed
@JuroOravec
JuroOravec deleted the feat/state-token-renewal branch November 24, 2026 17:53
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.

Renew a State token once it is past half its lifetime

1 participant