Skip to content

Events: write the same HTML on a render cache hit as on a fresh render - #871

Merged
JuroOravec merged 2 commits into
mainfrom
events/cache-replay-matches-live
Nov 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
events/cache-replay-matches-live

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Opus 5.5)

What was wrong

When the render cache served a component with Events, the root element
got data-cid="<render id>". A fresh render of the same component does
not write that attribute. So the cached HTML differed from the fresh
HTML, and the component's compiled browser template differed too (its
citryAttrs held the extra attribute). A page that mixed fresh and
cached copies of one component loaded two templates for it.

Which output is correct

The fresh render's. Citry's browser code never reads a fixed-name
data-cid: _vue/client.js, dev_checks.js and
packages/js/citry-client do not mention it. The client finds an Events
instance by occurrence.renderId in the manifest the server sends with
the page (client.js around 3770-3814). The per-render data-cid-<id>
markers are a different attribute and are unchanged.

What this changes

  • ext/events/cache.py: replay stages no root marker for an Events
    instance. Its manifest entry, with the fresh render ID, is unchanged.
  • ext/events/emission.py: removes the branch that wrote data-cid when
    direct_prepared_render_active() was False. Every render enters
    typed_render_scope(direct=True), so the branch never ran. I checked
    this by making the branch log each run: the whole non-e2e suite
    (15834 tests) never reached it.
  • Comments that described a fixed data-cid marker (attrs.py,
    serialize.py) are updated. The name stays reserved for c-bind and
    Citry UI attributes, and serialize.py still drops a valued
    data-cid=" marker in security_javascript omit and forbid modes as
    a guard, though no first-party code writes one now.
  • docs/design/caching.md section 8.2 says that replay writes the same
    HTML as a fresh render, apart from new State tokens.
  • CHANGELOG.md: a Fixes entry under v1.3.0.

Older design docs (events.md section 5.5, events_plan.md,
dependencies.md) describe the Alpine-era fixed data-cid. They were
already out of date before this change and are left as they are.

Tests

  • New test_cache_replay_of_an_events_component_writes_the_live_html
    (component cache and <c-cache> fragment): with the same render IDs
    and Vue app ID, the replayed serialized page equals the fresh one byte
    for byte. The test also checks that the component built only once.
    Both cases fail without the change.
  • Two existing assertions that checked the staged data-cid marker now
    check the staged entry's render ID instead (they prove the stored
    State token was accepted).
  • Sweep for other differences (Mechanism 3): a harness that compares a
    fresh render with a replay for Events with signed, server-stored and
    Pydantic State, css_data, js_data, $c-tr bindings, nested and
    multi-root components, shared roots, slot fills, <c-element> and
    security_javascript="omit". All outputs are equal after the change,
    apart from the newly signed State tokens. All differ before it.
  • Browser: tests/e2e/test_cache_replay_e2e.py and
    test_vue_events_default_e2e.py (79 tests) pass in Chromium. The
    replay tests click cached Events components, which now have no
    data-cid.
  • Non-e2e suite of packages/py/citry: 15870 passed. ruff,
    ruff format --check, mypy (host and --platform linux) and
    scripts/validate.py pass.

Review

An independent reviewer checked correctness, the reachability claim, the
sweep, the tests, versioning and the prose. It also ran a security pass
for markers for Citry's browser code (Mechanism 7) and a check against
the advisory survey (Mechanism 8). It found no hole. Data cannot write a
data-cid that Citry's browser code trusts, because no browser code
reads it, and c-bind and Citry UI still refuse the name. Its prose and
doc findings are folded in.

Versioning

  • The render cache payload is unchanged, so no contract version moves.
    An entry written by citry 1.2.0 replays under this code without the
    attribute. 1.3.0 starts with an empty cache anyway, because
    _RENDER_CONTRACT includes the Citry version.
  • The 1.x promise does not cover the exact attributes in rendered HTML,
    and data-cid stays reserved. This is a fix, so it fits 1.3.0.

Closes #659

🤖 Generated with Claude Code

JuroOravec and others added 2 commits November 24, 2026 21:17
A render cache replay of a component with Events gave its root element a
data-cid="<render id>" attribute, which a fresh render does not write.
Citry's browser code finds an Events instance through the render ID in
the manifest the server sends with the page, never through that
attribute, so the replay now writes none.

The fresh-render branch that would have written it ran only when
direct_prepared_render_active() was False, and every render sets it to
True (render_impl and render_prepared_direct both enter
typed_render_scope(direct=True)), so it is removed too. Comments that
described a fixed data-cid marker are updated.

Closes #659

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JuroOravec
JuroOravec merged commit 6baaaa6 into main Nov 24, 2026
1 of 16 checks passed
@JuroOravec
JuroOravec deleted the events/cache-replay-matches-live branch November 24, 2026 22:40
JuroOravec added a commit that referenced this pull request Nov 24, 2026
#871 (#878)

Records the Mechanism 7 security reviews of six merged Events changes, in
the form docs/security/reviews/README.md describes. Each record is built
from the pull request description, the merged diff and its tests; where a
description does not record something the template asks for, the record
says so.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

Cache replay of a component with Events adds a data-cid that a live render skips

1 participant