Skip to content

Commit 8048cf2

Browse files
committed
Effects see a store's contents; a caught store read tracks its keys
An effect input that is a store enters whole, as a snapshot (deep), and inputs compare structurally (EF-2), so an effect on a store reruns when a guess or an answer changes it in place. `catch` in an equation reads a store's keys inside its boundary, so it follows the store back from a failure. Found by the round 10 fuzz rerun (r7-chain3-rx-2 seed 27: the tab count stayed 0 with three rows on screen). Scenarios S-effect-store-1 and -2. replay.mjs: TAB=1 prints the issues tab label, EFFLOG=1 prints an instrumented effect log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0117BoWKEv4MghW8Ns9LeUUX
1 parent 6a86747 commit 8048cf2

6 files changed

Lines changed: 116 additions & 10 deletions

File tree

‎eval/harness/replay.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Round 8: replay one fuzz trail ("step ; step ; …", as `fuzz.mjs` prints it) against an app and print, after each step,
22
// the rows, the start of the detail and the requests in the air. DUMP=1 also prints the issues store (when the page
3-
// exposes it as globalThis.__stores); DIST=<dir> serves a prebuilt dist instead of building <dir>.
3+
// exposes it as globalThis.__stores); DIST=<dir> serves a prebuilt dist instead of building <dir>; TAB=1 adds the issues
4+
// tab's label; EFFLOG=1 prints and clears globalThis.__efflog (for a dist patched to push to it).
45
// Usage: node harness/replay.mjs <dir> "<trail>"
56
import { build } from "./build.mjs"; import { launch, serve, runTest } from "./driver.mjs"; import { freshServer, answer } from "./fuzz.mjs";
67
const [dir, trail] = process.argv.slice(2);
@@ -20,7 +21,8 @@ const r = await runTest(br, srv.url, { run: async app => {
2021
else if (["row-close", "row-assign", "remove-label"].includes(op)) await app.click(op, Number(arg));
2122
else await app.click(op);
2223
if (process.env.DUMP) console.log(" store:", await app.page.evaluate(() => { const s = globalThis.__stores?.issues; if (!s) return ""; return JSON.stringify([...s].map(x => ({ keys: Object.keys(x), id: x.id, t: x.title, a: x.assignee }))); }));
23-
console.log(s.slice(0, 32).padEnd(32), "|", JSON.stringify(await app.items("issue")), "|", (await app.text("detail") ?? "").slice(0, 30), "| air", JSON.stringify(await app.pending()));
24+
if (process.env.EFFLOG) console.log(" fx:", await app.page.evaluate(() => { const l = globalThis.__efflog ?? []; globalThis.__efflog = []; return l.join(" | "); }));
25+
console.log(s.slice(0, 32).padEnd(32), ...(process.env.TAB ? [`[${await app.text("tab-issues")}]`] : []), "|", JSON.stringify(await app.items("issue")), "|", (await app.text("detail") ?? "").slice(0, 30), "| air", JSON.stringify(await app.pending()));
2426
}
2527
} });
2628
console.log(r.ok ? "ok" : r.error); await br.close(); srv.close();

‎ref/compile/rt-web.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ const readWhole = (v: unknown) => { if (v !== null && typeof v === "object") Ref
1818
/** `latest x` (R-12) of a store is its whole value, read inside `latest` and handed out as a snapshot: a later read of a
1919
* field outside `latest` would be a tracked read of the store (found by desk9-rx-7: `len(latest rows)` held a `loading on`). */
2020
const latestWhole = (v: unknown) => v !== null && typeof v === "object" && (v as any)[$TARGET] !== undefined ? deep(v) : v;
21+
/** EF-2: effect inputs compare as values: arrays and plain objects (a store's snapshot) by their contents. */
22+
const sameInput = (a: unknown, b: unknown): boolean => {
23+
if (Object.is(a, b)) return true;
24+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
25+
const pa = Object.getPrototypeOf(a), plain = (p: unknown) => p === Object.prototype || p === null || p === Array.prototype;
26+
if (pa !== Object.getPrototypeOf(b) || !plain(pa)) return false;
27+
const ka = Object.keys(a), kb = Object.keys(b);
28+
return ka.length === kb.length && ka.every(k => Object.prototype.hasOwnProperty.call(b, k) && sameInput((a as any)[k], (b as any)[k]));
29+
};
2130
export { flush };
2231
export type Acc = () => unknown;
2332
export type Writer = { set: (v: unknown) => void; path: (keys: (string | number)[], v: unknown) => void; guess: (v: unknown, keys?: (string | number)[]) => void };
@@ -52,7 +61,7 @@ const isA = (err: unknown, classes: unknown[] | undefined): boolean => !classes
5261
/** `e catch (x: C) => alt`, reactive: an error boundary around the read whose fallback is the value (Solid: an errored read
5362
* reaches the nearest boundary even inside a JS `try`). The fallback is decided when read, so a change of class re-decides it. */
5463
function catchAcc(f: () => unknown, match: (err: unknown) => boolean, alt: (err: unknown) => unknown): () => unknown {
55-
const acc = untrack(() => createErrorBoundary(f, (err: () => unknown) => new Fallback(() => { const e0 = err(); return match(e0) ? alt(e0) : new Forward(e0); })));
64+
const acc = untrack(() => createErrorBoundary(() => readWhole(f()), (err: () => unknown) => new Fallback(() => { const e0 = err(); return match(e0) ? alt(e0) : new Forward(e0); })));
5665
return () => unwrapFallback(acc());
5766
}
5867
/** Imperative scope: an ordinary try. */
@@ -187,11 +196,11 @@ export function createApp(opts: { root: any; api?: Record<string, (...a: any[])
187196
/** `pre y` of another node (C-2): a companion memo carrying (current, previous), owned where `y` is declared. */
188197
pre: (get: Acc): Acc => { const m = createMemo((st: any) => ({ cur: get(), prev: st ? st.cur : undefined })); return () => (m() as { prev: unknown }).prev; },
189198
firstPaint: (f: () => unknown) => ({ loadingValue: untrack(f) }),
190-
/** `effect (…) { … }`: compute, then apply; an error is an answer the apply receives (E-12); fires on a differing value (EF-2). */
199+
/** `effect (…) { … }`: compute, then apply; an error is an answer the apply receives (E-12); fires on a differing value (EF-2); a store enters whole, as a snapshot. */
191200
effect(n: number, inputs: () => unknown[], apply: (p: unknown[]) => unknown) {
192201
const run = (vals: unknown[]) => { const r = apply(vals); return typeof r === "function" ? (r as () => void) : undefined; };
193202
let last: unknown[] | null = null;
194-
createEffect(inputs as any, { effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => Object.is(v, last![i]))) return; last = vals; return run(vals); }, error: () => { last = null; return run(Array.from({ length: n }, () => ERR)); } } as any);
203+
createEffect((() => inputs().map(latestWhole)) as any, { effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => sameInput(v, last![i]))) return; last = vals; return run(vals); }, error: () => { last = null; return run(Array.from({ length: n }, () => ERR)); } } as any);
195204
},
196205
/** `action f(…) { … }` (AC-1): a generator; a statement-level `await` is a hold-safe yield. */
197206
action(gen: (...a: unknown[]) => Generator<unknown, unknown, unknown>) {

‎ref/compile/rt.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ const readWhole = (v: unknown) => { if (v !== null && typeof v === "object") Ref
1717
/** `latest x` (R-12) of a store is its whole value, read inside `latest` and handed out as a snapshot: a later read of a
1818
* field outside `latest` would be a tracked read of the store (found by desk9-rx-7: `len(latest rows)` held a `loading on`). */
1919
const latestWhole = (v: unknown) => v !== null && typeof v === "object" && (v as any)[$TARGET] !== undefined ? deep(v) : v;
20+
/** EF-2: effect inputs compare as values: arrays and plain objects (a store's snapshot) by their contents. */
21+
const sameInput = (a: unknown, b: unknown): boolean => {
22+
if (Object.is(a, b)) return true;
23+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
24+
const pa = Object.getPrototypeOf(a), plain = (p: unknown) => p === Object.prototype || p === null || p === Array.prototype;
25+
if (pa !== Object.getPrototypeOf(b) || !plain(pa)) return false;
26+
const ka = Object.keys(a), kb = Object.keys(b);
27+
return ka.length === kb.length && ka.every(k => Object.prototype.hasOwnProperty.call(b, k) && sameInput((a as any)[k], (b as any)[k]));
28+
};
2029
export { flush };
2130
export type Acc = () => unknown;
2231
export type Writer = { set: (v: unknown) => void; path: (keys: (string | number)[], v: unknown) => void; guess: (v: unknown, keys?: (string | number)[]) => void };
@@ -43,7 +52,7 @@ const isA = (err: unknown, classes: unknown[] | undefined): boolean => !classes
4352
/** `e catch (x: C) => alt`, reactive: an error boundary around the read whose fallback is the value (Solid: an errored read
4453
* reaches the nearest boundary even inside a JS `try`). The fallback is decided when read, so a change of class re-decides it. */
4554
function catchAcc(f: () => unknown, match: (err: unknown) => boolean, alt: (err: unknown) => unknown): () => unknown {
46-
const acc = untrack(() => createErrorBoundary(f, (err: () => unknown) => new Fallback(() => { const e0 = err(); return match(e0) ? alt(e0) : new Forward(e0); })));
55+
const acc = untrack(() => createErrorBoundary(() => readWhole(f()), (err: () => unknown) => new Fallback(() => { const e0 = err(); return match(e0) ? alt(e0) : new Forward(e0); })));
4756
return () => unwrapFallback(acc());
4857
}
4958
/** Imperative scope: an ordinary try. */
@@ -197,11 +206,11 @@ export function createApp(opts: { api?: Record<string, (...a: any[]) => unknown>
197206
/** `pre y` of another node (C-2): a companion memo carrying (current, previous), owned where `y` is declared. */
198207
pre: (get: Acc): Acc => { const m = createMemo((st: any) => ({ cur: get(), prev: st ? st.cur : undefined })); return () => (m() as { prev: unknown }).prev; },
199208
firstPaint: (f: () => unknown) => ({ loadingValue: untrack(f) }),
200-
/** `effect (…) { … }`: compute, then apply; an error is an answer the apply receives (E-12); fires on a differing value (EF-2). */
209+
/** `effect (…) { … }`: compute, then apply; an error is an answer the apply receives (E-12); fires on a differing value (EF-2); a store enters whole, as a snapshot. */
201210
effect(n: number, inputs: () => unknown[], apply: (p: unknown[]) => unknown) {
202211
const run = (vals: unknown[]) => { const r = apply(vals); return typeof r === "function" ? (r as () => void) : undefined; };
203212
let last: unknown[] | null = null;
204-
createEffect(inputs as any, { effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => Object.is(v, last![i]))) return; last = vals; return run(vals); }, error: () => { last = null; return run(Array.from({ length: n }, () => ERR)); } } as any);
213+
createEffect((() => inputs().map(latestWhole)) as any, { effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => sameInput(v, last![i]))) return; last = vals; return run(vals); }, error: () => { last = null; return run(Array.from({ length: n }, () => ERR)); } } as any);
205214
},
206215
/** `action f(…) { … }` (AC-1): a generator; a statement-level `await` is a hold-safe yield. */
207216
action(gen: (...a: unknown[]) => Generator<unknown, unknown, unknown>) {

‎ref/oracle/lower.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function assignDraft(draft: any, v: unknown) {
4343
Object.assign(draft, v);
4444
}
4545

46+
/** EF-2: effect inputs compare as values: arrays and plain objects (a store's snapshot) by their contents. */
47+
const sameInput = (a: unknown, b: unknown): boolean => {
48+
if (Object.is(a, b)) return true;
49+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
50+
const pa = Object.getPrototypeOf(a), plain = (p: unknown) => p === Object.prototype || p === null || p === Array.prototype;
51+
if (pa !== Object.getPrototypeOf(b) || !plain(pa)) return false;
52+
const ka = Object.keys(a), kb = Object.keys(b);
53+
return ka.length === kb.length && ka.every(k => Object.prototype.hasOwnProperty.call(b, k) && sameInput((a as any)[k], (b as any)[k]));
54+
};
4655
export class Unsupported extends Error { name = "Unsupported"; static last = ""; constructor(m: string) { super(m); Unsupported.last = m; } }
4756

4857
// ---------------------------------------------------------------- string-tree renderer
@@ -296,8 +305,8 @@ export class Oracle {
296305
const inputs = d.inputs; for (const e of inputs) this.hoistAwaits(null, e, scope, false);
297306
const apply = (vals: unknown[]) => { const env = new Map<string, unknown>(); vals.forEach((v, i) => env.set(`$${i}`, v)); const r = this.runStmts(d.body, scope, env); return typeof r === "function" ? (r as () => void) : undefined; };
298307
let last: unknown[] | null = null;
299-
createEffect(() => inputs.map(e => this.ev(e, scope)), {
300-
effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => Object.is(v, last![i]))) return; last = vals; return apply(vals); }, // EF-3: fires when an input changed (an error in between is a change)
308+
createEffect(() => inputs.map(e => { const v = this.ev(e, scope); return v !== null && typeof v === "object" && (v as any)[$TARGET] !== undefined ? deep(v) : v; }), { // a store enters whole, as a snapshot
309+
effect: (vals: unknown[]) => { if (last && last.length === vals.length && vals.every((v, i) => sameInput(v, last![i]))) return; last = vals; return apply(vals); }, // EF-3: fires when an input changed (an error in between is a change)
301310
error: () => { last = null; return apply(inputs.map(() => ERR)); }, // E-12: an error is an answer the apply receives
302311
} as any);
303312
break;

‎scenarios/S-effect-store-1.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# S-effect-store-1 — an effect on a store fires when the store's contents change
2+
3+
Rules exercised: EF-1, EF-2, H-11, ST-4, AC-3, AC-4.
4+
5+
## Program
6+
7+
```
8+
export default component App() {
9+
source rows key id = await fetch("l")
10+
effect (rows) { log(rows_title($0)) }
11+
action ren() { try { optimistic rows[0].title := "x"; await fetch("save"); refresh rows } catch (e) {} }
12+
errored fallback(e) { <p>error</p> } { loading fallback { <p>loading</p> } { <b>{for t in rows { <i>{t.title}</i> }}</b> } }
13+
}
14+
fn rows_title(rs) => rs.map(r => r.title).join(",")
15+
```
16+
17+
`rows` is a keyed store: a guess and each new answer change it in place, so the value the effect receives is the same
18+
object every time. EF-2 compares values, and the list's value changes when the guess shows (instant 2, H-11: an input
19+
that derives from a guess fires with the reveal) and when the new answer lands (instant 4).
20+
21+
## Frames
22+
23+
| instant | action | frame | effect |
24+
| ------- | ------ | ----- | ------ |
25+
| 0 | mount | `loading` | |
26+
| 1 | resolve "l" [{"id":1,"title":"A"},{"id":2,"title":"C"}] | `AC` | `log(A,C)` |
27+
| 2 | handler `ren()` | `xC` | `log(x,C)` |
28+
| 3 | resolve "save" 0 | `xC` | |
29+
| 4 | resolve "l" [{"id":1,"title":"B"},{"id":2,"title":"C"}] | `BC` | `log(B,C)` |
30+
31+
Found by the timing fuzzer (round 10, r7-chain3-rx-2 seed 27): an app reported the open list's length to its tab from
32+
`effect (filter, issues catch (e) => null)`, and the tab kept `0` with three rows on screen at rest. The compiled
33+
runtime handed the effect the store itself, whose identity never changes, and read none of its keys, so nothing after
34+
the first answer reran the effect. An input that is a store now enters the effect as a snapshot of the whole store
35+
(`deep`), which tracks every key, and inputs are compared structurally, so an equal answer is still not a change.

‎scenarios/S-effect-store-2.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# S-effect-store-2 — an effect on a store under `catch` follows the store back from an error
2+
3+
Rules exercised: EF-1, EF-2, E-12, ST-4, AC-4.
4+
5+
## Program
6+
7+
```
8+
export default component App() {
9+
source rows key id = await fetch("l")
10+
effect (rows catch (e) => null) { log($0 == null ? "none" : len($0)) }
11+
errored fallback(e) { <p>error</p> } { loading fallback { <p>loading</p> } { <b>{len(rows)}</b> } }
12+
}
13+
```
14+
15+
The effect reads `rows` through `catch`, so a failed answer reaches it as `null` (instant 3). The next answer
16+
(instant 5) is a store again, with different contents, and the apply runs with it.
17+
18+
## Frames
19+
20+
| instant | action | frame | effect |
21+
| ------- | ------ | ----- | ------ |
22+
| 0 | mount | `loading` | |
23+
| 1 | resolve "l" [{"id":1},{"id":3},{"id":4}] | `3` | `log(3)` |
24+
| 2 | handler `refresh rows` | `3` | |
25+
| 3 | reject "l" "down" | `error` | `log(none)` |
26+
| 4 | handler `refresh rows` | `error` | |
27+
| 5 | resolve "l" [{"id":1},{"id":4}] | `2` | `log(2)` |
28+
29+
Found by the timing fuzzer (round 10, r7-chain3-rx-2 seed 27): an app reported the open list's length to its tab from
30+
`effect (filter, issues catch (e) => null)`; after a failed answer the tab kept its last count with three rows on
31+
screen at rest. `catch` in an equation lowers to an error boundary around the read, and the read of a store returned
32+
the store without reading any of its keys, so once the boundary had caught the failure nothing it tracked changed again.
33+
The boundary now reads a store's keys (as `pending?` does), and an effect input that is a store enters whole, as a
34+
snapshot (S-effect-store-1).
35+
36+
## Variant: the first answer fails
37+
38+
| instant | action | frame | effect |
39+
| ------- | ------ | ----- | ------ |
40+
| 1 | reject "l" "down" | `error` | `log(none)` |
41+
| 2 | handler `refresh rows` | `error` | |
42+
| 3 | resolve "l" [{"id":1},{"id":4}] | `2` | `log(2)` |

0 commit comments

Comments
 (0)