Skip to content

RFC 34: SNAPSHOT_KEPT: refuse a kept peek of what changes #35

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: none beyond upstream PR solidjs#69.

Summary

peek e reads e once, without subscribing (E-5). When the peek sits in an expression that is kept and read again, and nothing else in that expression changes, the expression never recomputes, so every reader keeps the first value of e. If e can change, that is almost always a bug. This RFC makes it a compile error, SNAPSHOT_KEPT (E-5a), checked one module at a time.

Motivation

In ReactiveScript an equation (x = …) is a derived value (a Solid memo); a view's {…} is a live read. peek exists for samples: read a value at the moment something else makes the expression recompute. Used alone, it quietly freezes:

export default component App() {
  source n := 1
  shown = peek n          // 1 forever
  <p>{shown}</p>
  <button onClick={() => { n := n + 1 }}>+</button>
}

The same happens with a provision: provide page = peek data, where data loads and is refreshed, hands every consumer the first value. In Solid this is untrack(data) in a memo with no other source, or <Ctx value={untrack(data)}>. Upstream accepts both without a word, and at run time nothing fails: the page just stops updating.

Proposal

E-5a (A kept peek of what changes). A peek is kept when its expression is kept and read again:

  • an equation; a provision or a context's default;
  • what a view reads (a {…}, an attribute, an if condition, a for list);
  • a component's argument; a ref argument.

Not kept: a source's seed (E-4) and a value argument (E-10), which are snapshots by rule; an effect's compute half; a closure, which reads when called.

A kept peek e is refused with SNAPSHOT_KEPT (bin S, a compile error) when nothing else in the expression changes and e can change: a source that is written, guessed, refreshed or affected; something that awaits; a ref parameter; a JS reader or a field declared to hold; a field read through the instance a declared primitive made (peek size.width), or an equation bound to one; a field read through a for row (peek r.x) whose list can change. The row and the instance themselves never change (peek r is accepted, as is a method called on an instance), but a row's fields are written by path or reconciled by key while the row lives, and an instance's fields track.

Accepted:

twice = n * 2 + peek n         // a sample: n * 2 changes, so peek n is read again
source draft := peek props.start   // a named snapshot (E-4)
fixed = peek label             // label is an equation that never changes

The fix the message suggests: drop peek to read live, or name the snapshot, source saved := peek e.

Per module. Writes to a source are always in its own module (MO-2), and a context is read-only beneath its provision (RG-7c). What one module cannot see is who applies its components, who provides its contexts, and what an imported .rx export does. Checked one module at a time (the default), each of those counts as changing:

// show.rx: refused when checked alone, whoever applies Show
export component Show(props: { value: number }) {
  <b>{peek props.value}</b>
}

RFC 35 proposes a whole-program mode that decides these from every application and provision.

Drawbacks and alternatives

  • It refuses some correct programs: a peek of a prop that every caller passes a constant (above). The per-module default is conservative on purpose; RFC 35 is the escape.
  • A prop is assumed to change for the peek, but does not count as "something else that changes" for the rest of the expression. So v = props.k + peek n with n written is refused per module, even though props.k may well change. This keeps the check one-sided (it never accepts a freeze it cannot see past), at the cost of more refusals.
  • Alternative: a warning. Under CN-1a there is no warning tier, so a bin-S rule is an error or nothing.
  • Alternative: forbid peek outside samples entirely. That would refuse fixed = peek label, which is harmless, and named snapshots, which are the point.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript: commit 8b62574 (the per-module check in ref/src/check.ts and compiler/src/check.rs). Spec rule E-5a in spec/02-core.md; SNAPSHOT_KEPT in spec/99-diagnostics.md and guide/diagnostics.md. Scenario scenarios/S-surface-6.md: the accepted program (a sample, a named snapshot, a peek of a constant equation, with frames) and rejected variants for an equation, a provision of loaded data, and a prop peeked in another module.

Open questions

  • Should a prop on the other side of the expression (props.k + peek n) count as changing per module, making the peek a sample? The prototype says no.
  • Resolved in f031b0b (both checkers, S-surface-8): a kept peek of a field of a primitive's instance, and of a field of a for row whose list can change, were accepted and froze. A row's field counts as changing whenever its list does, which also refuses a list that is only ever replaced whole with untouched rows; dropping the peek there costs nothing. Should that case be told apart?
  • Not covered: the same field read as a value argument (createLabel(r.x) in a row, E-10) or a source's seed is still read once without a diagnostic for a row; for an instance RFC 14 refuses it.

Revised after the second review (2026-11-21, cdc00d4)

RFC 34, after the second review. Aligned with RFC 14's bounded exemption: a kept peek inst.m() of a method the primitive does not declare in methods(…) is SNAPSHOT_KEPT, as the same call in an argument is SNAPSHOT_ARGUMENT (S-module-20).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions