Skip to content

Commit 612551c

Browse files
committed
refresh re-asks only its own mount's hoisted awaits
The compiled runtime and the oracle kept an equation's hoisted awaits by name for the whole app, so after a component remounted, `refresh` also re-asked the disposed mount's question. When that question was still in flight at disposal it never answered, the action never ended, and its change held every guess on the store: a declined close's row stayed hidden and the detail stayed on loading at rest (round 10 fuzz rerun, r7-chain4-rx-2 seed 49). Hoisted awaits are now claimed per instance (by the equation's accessor in the runtime, by the declaring scope in the oracle). Scenario S-remount-refresh fails before and passes after on all runners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0117BoWKEv4MghW8Ns9LeUUX
1 parent 8518fa1 commit 612551c

4 files changed

Lines changed: 58 additions & 9 deletions

File tree

‎ref/compile/rt-web.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ export function createApp(opts: { root: any; api?: Record<string, (...a: any[])
106106
const contexts = new Map<string, ReturnType<typeof createContext<Entry | undefined>>>();
107107
const storeAccs = new WeakSet<object>();
108108
const modules = new Map<unknown, Record<string, unknown>>();
109-
const hoistedBy = new Map<string, Acc[]>(); // an equation's hoisted awaits, re-asked with it by `refresh`
109+
// An equation's hoisted awaits, re-asked with it by `refresh`. They are emitted just before the equation and claimed
110+
// by its accessor when it registers, so a refresh reaches only its own instance's (never a disposed mount's).
111+
const hoistedBy = new Map<string, Acc[]>();
112+
const hoistedOf = new WeakMap<Acc, Acc[]>();
110113
const log: string[] = [];
111114
let currentEq: string | null = null; let serverSeq = 0; let classSeq = 0;
112115
const jsErrors = new Map<string, unknown>(); // MO-8: one class per declared JS error, per app
@@ -142,7 +145,7 @@ export function createApp(opts: { root: any; api?: Record<string, (...a: any[])
142145
api: (name: string) => opts.api ? (opts.api[name] ?? (() => { throw new Error(`./api has no ${name}`); })) : (...a: unknown[]) => flight(`${name}:${a.map(fmt).join(",")}`),
143146

144147
// ---- the scenario harness's view of names (handler rows reach any live binding by name) ----
145-
reg(name: string, e: Entry) { let l = registry.get(name); if (!l) registry.set(name, (l = [])); l.push(e); },
148+
reg(name: string, e: Entry) { let l = registry.get(name); if (!l) registry.set(name, (l = [])); l.push(e); const h = hoistedBy.get(name); if (h && e.get) { hoistedOf.set(e.get, h); hoistedBy.delete(name); } },
146149
lookup(name: string): Entry { const l = registry.get(name); if (!l?.length) throw new Error(`unbound ${name}`); for (let i = l.length - 1; i >= 0; i--) if (l[i].w) return l[i]; return l[l.length - 1]; },
147150
dget(name: string, live = false) { const e = app.lookup(name); return e.get ? (live ? e.get() : app.snapshot(e.get)) : e.value; }, // live: inside a probe (R-12), the read itself
148151
dset(name: string, keys: (string | number)[], v: unknown, guess = false) { const w = app.lookup(name).w; if (!w) throw new Error(`W-1: write to non-source ${name}`); if (guess) w.guess(v, keys); else if (keys.length) w.path(keys, v); else w.set(v); },
@@ -250,7 +253,7 @@ export function createApp(opts: { root: any; api?: Record<string, (...a: any[])
250253
until: (p: () => unknown) => until(p), // AC-6
251254
onSettled: (f: () => void) => onSettled(f), // EF-7
252255
onFlush: (f: () => void) => onFlush(f), // EF-8
253-
refresh(name: string, get: Acc) { const key = lastKey.get(name); if (key) flights.delete(key); for (const h of hoistedBy.get(name) ?? []) refresh(h as any); const t = truthOf.get(get) ?? get; refresh((storeAccs.has(t) ? t() : t) as any); },
256+
refresh(name: string, get: Acc) { const key = lastKey.get(name); if (key) flights.delete(key); for (const h of hoistedOf.get(get) ?? []) refresh(h as any); const t = truthOf.get(get) ?? get; refresh((storeAccs.has(t) ? t() : t) as any); },
254257
affects: (get: Acc) => affects(get as any),
255258
preOr: (v: unknown, init: () => unknown) => v === undefined ? init() : v,
256259

‎ref/compile/rt.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ export function createApp(opts: { api?: Record<string, (...a: any[]) => unknown>
116116
const contexts = new Map<string, ReturnType<typeof createContext<Entry | undefined>>>();
117117
const storeAccs = new WeakSet<object>();
118118
const modules = new Map<unknown, Record<string, unknown>>();
119-
const hoistedBy = new Map<string, Acc[]>(); // an equation's hoisted awaits, re-asked with it by `refresh`
119+
// An equation's hoisted awaits, re-asked with it by `refresh`. They are emitted just before the equation and claimed
120+
// by its accessor when it registers, so a refresh reaches only its own instance's (never a disposed mount's).
121+
const hoistedBy = new Map<string, Acc[]>();
122+
const hoistedOf = new WeakMap<Acc, Acc[]>();
120123
const log: string[] = [];
121124
let currentEq: string | null = null; let serverSeq = 0;
122125
const jsErrors = new Map<string, unknown>(); // MO-8: one class per declared JS error, per app
@@ -152,7 +155,7 @@ export function createApp(opts: { api?: Record<string, (...a: any[]) => unknown>
152155
api: (name: string) => opts.api ? (opts.api[name] ?? (() => { throw new Error(`./api has no ${name}`); })) : (...a: unknown[]) => flight(`${name}:${a.map(fmt).join(",")}`),
153156

154157
// ---- the scenario harness's view of names (handler rows reach any live binding by name) ----
155-
reg(name: string, e: Entry) { let l = registry.get(name); if (!l) registry.set(name, (l = [])); l.push(e); },
158+
reg(name: string, e: Entry) { let l = registry.get(name); if (!l) registry.set(name, (l = [])); l.push(e); const h = hoistedBy.get(name); if (h && e.get) { hoistedOf.set(e.get, h); hoistedBy.delete(name); } },
156159
lookup(name: string): Entry { const l = registry.get(name); if (!l?.length) throw new Error(`unbound ${name}`); for (let i = l.length - 1; i >= 0; i--) if (l[i].w) return l[i]; return l[l.length - 1]; },
157160
dget(name: string, live = false) { const e = app.lookup(name); return e.get ? (live ? e.get() : app.snapshot(e.get)) : e.value; }, // live: inside a probe (R-12), the read itself
158161
dset(name: string, keys: (string | number)[], v: unknown, guess = false) { const w = app.lookup(name).w; if (!w) throw new Error(`W-1: write to non-source ${name}`); if (guess) w.guess(v, keys); else if (keys.length) w.path(keys, v); else w.set(v); },
@@ -258,7 +261,7 @@ export function createApp(opts: { api?: Record<string, (...a: any[]) => unknown>
258261
until: (p: () => unknown) => until(p), // AC-6
259262
onSettled: (f: () => void) => onSettled(f), // EF-7
260263
onFlush: (f: () => void) => onFlush(f), // EF-8
261-
refresh(name: string, get: Acc) { const key = lastKey.get(name); if (key) flights.delete(key); for (const h of hoistedBy.get(name) ?? []) refresh(h as any); const t = truthOf.get(get) ?? get; refresh((storeAccs.has(t) ? t() : t) as any); },
264+
refresh(name: string, get: Acc) { const key = lastKey.get(name); if (key) flights.delete(key); for (const h of hoistedOf.get(get) ?? []) refresh(h as any); const t = truthOf.get(get) ?? get; refresh((storeAccs.has(t) ? t() : t) as any); },
262265
affects: (get: Acc) => affects(get as any),
263266
preOr: (v: unknown, init: () => unknown) => v === undefined ? init() : v,
264267

‎ref/oracle/lower.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class Oracle {
192192
}
193193
/** 07 §3 / R-1: each `await` that is not the equation's result becomes its own async memo, asked only while the conditions
194194
* that reach it hold (E-11); the equation reads it where the `await` was (ref/src/awaits.ts). */
195-
hoistedByName = new Map<string, (() => unknown)[]>();
195+
hoistedByName = new WeakMap<Scope, Map<string, (() => unknown)[]>>();
196196
private hoistAwaits(name: string | null, e: Expr, scope: Scope, tail = true) {
197197
for (const h of nonTailAwaits(e, tail)) {
198198
const lv = h.node.init ? { loadingValue: untrack(() => this.ev(h.node.init!, scope)) } : undefined;
@@ -203,7 +203,7 @@ export class Oracle {
203203
}, lv as any);
204204
let map = hoistedOf.get(scope); if (!map) hoistedOf.set(scope, (map = new Map())); map.set(h.node, m);
205205
if (name === null) continue;
206-
let byName = this.hoistedByName.get(name); if (!byName) this.hoistedByName.set(name, (byName = [])); byName.push(m);
206+
let byName = this.hoistedByName.get(scope); if (!byName) this.hoistedByName.set(scope, (byName = new Map())); let l = byName.get(name); if (!l) byName.set(name, (l = [])); l.push(m); // per scope: a refresh never reaches a disposed mount's
207207
}
208208
}
209209
/** A region: its declarations, the awaits its own view reads (07 §3), then its view. */
@@ -458,7 +458,7 @@ export class Oracle {
458458
}
459459
case "throw": throw untrack(() => this.ev(s.e, scope, env));
460460
case "action": this.lowerAction([], s.body, scope, env)(); return;
461-
case "refresh": { const b = scope.lookup(s.name); if (!b || !("get" in b)) throw new Error("INVALID_REFRESH_TARGET"); const key = this.lastKey.get(s.name); if (key) this.forget(key); for (const h of this.hoistedByName.get(s.name) ?? []) refresh(h as any); refresh((b.k === "store" ? b.get() : (b.k === "sig" && b.truth) || b.get) as any); return; }
461+
case "refresh": { const b = scope.lookup(s.name); if (!b || !("get" in b)) throw new Error("INVALID_REFRESH_TARGET"); const key = this.lastKey.get(s.name); if (key) this.forget(key); const d = Scope.all.find(x => x.vars.get(s.name) === b); for (const h of (d && this.hoistedByName.get(d)?.get(s.name)) ?? []) refresh(h as any); refresh((b.k === "store" ? b.get() : (b.k === "sig" && b.truth) || b.get) as any); return; }
462462
case "affects": { const b = scope.lookup(s.name); if (!b || !("get" in b)) throw new Error("INVALID_AFFECTS_TARGET"); affects(b.get as any); return; }
463463
case "optimistic": case "write": {
464464
const b = s.target.ctx ? this.provision(s.target.name, scope) : scope.lookup(s.target.name);

‎scenarios/S-remount-refresh.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# S-remount-refresh — a refresh after a remount re-asks only the live mount's question
2+
3+
Rules exercised: AC-4, H-11, ST-4, O-2.
4+
5+
## Program
6+
7+
```
8+
component List() {
9+
source rows key id = tag(await fetch("l"))
10+
action ren() { try { optimistic rows[0].title := "x"; await fetch("save"); refresh rows } catch (e) {} }
11+
errored fallback(e) { <p>error</p> } { loading fallback { <p>loading</p> } { <b>{for r in rows { <i>{r.title}</i> }}</b> } }
12+
}
13+
fn tag(rs) => rs.map(r => ({ id: r.id, title: r.title, seen: true }))
14+
export default component App() {
15+
source tab := "list"
16+
{if tab == "list" { <List /> } else { <p>help</p> }}
17+
}
18+
```
19+
20+
`rows` awaits inside a call, so its question is asked by a hoisted await (07 §3) that `refresh rows` re-asks with it.
21+
The first mount's action is still waiting on its re-ask when the tab is left (instant 4); the new mount asks the same
22+
question and one answer serves both (instant 6). The new mount's action re-asks that mount's question only, and its
23+
guess ends when that answer lands (instant 9).
24+
25+
## Frames
26+
27+
| instant | action | frame |
28+
| ------- | ------ | ----- |
29+
| 0 | mount | `loading` |
30+
| 1 | resolve "l" [{"id":1,"title":"A"}] | `A` |
31+
| 2 | handler `ren()` | `x` |
32+
| 3 | resolve "save" 0 | `x` |
33+
| 4 | handler `tab := "help"` | `help` |
34+
| 5 | handler `tab := "list"` | `loading` |
35+
| 6 | resolve "l" [{"id":1,"title":"B"}] | `B` |
36+
| 7 | handler `ren()` | `x` |
37+
| 8 | resolve "save" 0 | `x` |
38+
| 9 | resolve "l" [{"id":1,"title":"C"}] | `C` |
39+
40+
Found by the timing fuzzer (round 10, r7-chain4-rx-2 seed 49): after leaving the Issues tab and coming back, a declined
41+
close's row stayed hidden and the detail stayed on "loading" at rest. The compiled runtime and the oracle kept an
42+
equation's hoisted awaits by name for the whole app, so `refresh` also re-asked the disposed mount's question, which
43+
never answers: the action never ended and its change held every guess on the store. They are now kept per instance.

0 commit comments

Comments
 (0)