Repository navigation
feat(events): renew a State token once it is past half its lifetime - #858
Merged
Merged
Conversation
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]>
…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]>
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #856
A page that is in use no longer gets
stale_statewhen 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 astateaction, 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_tokennow returnsrenew_after, the half-way point of the token's own lifetime (t + (x - t) / 2; a token from before 1.0.0 with"x": nullcounts as 24 hours).state_renewal_due()compares it with the server's clock._resign_statestep as a State change, so a call returns at most one token.Citry skips the renewal:
stateaction, or the call is a form posted without JavaScript;_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
stateaction 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
TestStateRenewaltest_a_call_that_changes_state_sends_one_tokenstateaction; the render's token is freshtest_a_render_of_the_caller_already_carries_the_new_tokentest_a_batch_*,test_a_later_read_*,test_a_change_after_*,test_a_render_of_another_instance_*TestGetHandlersintest_events_routes.pytest_a_render_replayed_from_the_cache_carries_a_new_token"x": null)test_a_token_from_before_1_0_0_*,TestRenewalTimetest_a_token_minted_by_a_server_whose_clock_runs_ahead_still_workstest_an_old_token_keeps_working_until_its_own_expirytest_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:_modelfield, 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 declaresstate; whoever reads those logs can keep that token alive.security.md,docs/design/events.md7.1, andtrust_boundaries.mdnow say so. Tokens still give no permission; every handler checks it.publicStatein a renewal holds only_publicfields.callerRenderIdreaches no new place.Fixed during the review: a cache-write failure during renewal answered 500 on a read-only call; a failed
stateaction 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$stateruns).Left out of scope, and older than this PR: when a State change's
stateaction 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_ageafter Citry issues it, andVerifiedStategains 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
--platform linuxoncitryand the typing contract tests.python -m docs_site build-check --strict(0 errors, 0 warnings) andscripts/validate.pypass on the merged branch.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 theComponent.Statedocstring 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