Skip to content

Commit 634594f

Browse files
committed
spike(ownts): React useEffect leaks through the OwnIR seam (P-020 Own.React)
Add an OwnTS frontend spike — the TS/TSX sibling of the Roslyn C# extractor. It scans a React .tsx, emits OwnIR facts, and lets the existing Python core flag the leak: a useEffect acquire (setInterval/setTimeout → timer, .subscribe → subscribe, addEventListener → subscription) with no cleanup return is the core's OWN001, exactly as a C# `event +=` without `-=` is. Same seam, same one checker, different skin — proving the leak model is cross-language, not .NET-only. Honest scope, per P-020: this is the EFF003/EFF004 slice that *is* the existing acquire→release model. EFF001/002 (the unstable-dependency effect storm) is NOT built — it needs a new core stability analysis and must not masquerade as OWN001; the scanner only emits a clearly-labelled, frontend-only heuristic note for it on stderr, never a core finding. - frontend/ownts/ownts.py: heuristic extractor (brace match + cleanup-verb detection; not a TS parser) with --check to run straight through the core - frontend/ownts/examples/: leaky Dashboard.tsx (3× OWN001) + clean variant - frontend/ownts/test_ownts.py: pins leaky=3, clean=0, kinds, EFF001 note - CI: new ownts-react-effects job (.tsx → OwnIR → core, no dotnet needed) - docs/proposals/P-020: status note that the EFF003/004 spike landed Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PVeuchch67CtjSbizYm8gi
1 parent b9bbb71 commit 634594f

7 files changed

Lines changed: 526 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,42 @@ jobs:
998998
|| { echo "FAIL: --project flag and positional .csproj emit different OwnIR facts"; exit 1; }
999999
echo "OK: .csproj input resolves to its source set and feeds the core identically (positional == --project, fact-level parity)"
10001000
1001+
# The OwnTS frontend spike (P-020 Own.React): the SAME OwnIR seam, fed from a
1002+
# React .tsx instead of C#. A useEffect acquire (timer / subscribe / listener)
1003+
# with no cleanup return is the core's OWN001 — proving the leak model is
1004+
# cross-language, not .NET-only. No dotnet needed; pure-Python frontend.
1005+
ownts-react-effects:
1006+
name: OwnTS (React useEffect) -> OwnIR -> core
1007+
runs-on: ubuntu-latest
1008+
steps:
1009+
- uses: actions/checkout@v4
1010+
- uses: actions/setup-python@v5
1011+
with:
1012+
python-version: "3.13"
1013+
- name: Pin the spike (leaky=3xOWN001, clean=0, EFF001 heuristic)
1014+
run: python frontend/ownts/test_ownts.py
1015+
- name: Extract OwnIR facts from a React .tsx and check through the core
1016+
run: |
1017+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx \
1018+
-o "$RUNNER_TEMP/dash.facts.json"
1019+
cat "$RUNNER_TEMP/dash.facts.json"
1020+
out=$(python -m ownlang ownir "$RUNNER_TEMP/dash.facts.json" || true)
1021+
echo "$out"
1022+
echo "$out" | grep -q "Dashboard.tsx" \
1023+
|| { echo "FAIL: expected findings located at the .tsx"; exit 1; }
1024+
echo "$out" | grep -q "resource: timer" \
1025+
|| { echo "FAIL: expected the setInterval [resource: timer] leak"; exit 1; }
1026+
echo "$out" | grep -qE "3 finding" \
1027+
|| { echo "FAIL: expected three OWN001 leaks from the leaky effects"; exit 1; }
1028+
- name: The clean fixture (cleanup returns) is silent
1029+
run: |
1030+
python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx \
1031+
-o "$RUNNER_TEMP/clean.facts.json"
1032+
clean=$(python -m ownlang ownir "$RUNNER_TEMP/clean.facts.json" || true)
1033+
echo "$clean"
1034+
echo "$clean" | grep -q "0 finding" \
1035+
|| { echo "FAIL: cleaned-up effects must not leak"; exit 1; }
1036+
10011037
# The distribution surface (Уровень 1): the own-check.sh orchestrator walks a
10021038
# directory of real C# and prints findings in the host-parseable formats the
10031039
# GitHub Action (PR annotations) and a VS Error List (MSBuild) consume — and

‎docs/proposals/P-020-ownts-react-effects.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
- **Status:** draft — **experiment / proposal, explicitly not mainline.** A
44
marketing-shaped spike under the OwnTS frontend, on the record so the framing is
55
honest before any code.
6+
- **Spike landed:** the honest `EFF003`/`EFF004` slice (timer / subscribe /
7+
listener acquire with no cleanup `return` → core `OWN001`) is implemented in
8+
`frontend/ownts/` and CI-pinned (the `ownts-react-effects` job). `EFF001/002`
9+
(the effect-storm stability analysis) is **not** built — the spike only emits a
10+
clearly-labelled frontend-only heuristic note for it, never a core finding, per
11+
Open question 1 below.
612
- **Depends on:** [P-017](P-017-multi-stack-frontends.md) (the OwnTS frontend &
713
confidence tiers that this profile feeds), `spec/OwnCore.md` (the acquire/release
814
vocabulary), [P-004](P-004-wpf-lifetime-profile.md) (WPF — the same lifecycle

‎frontend/ownts/README.md‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# OwnTS — React `useEffect` frontend spike (`Own.React`)
2+
3+
The TypeScript sibling of the [Roslyn C# extractor](../roslyn/). It scans a
4+
React `.tsx`, emits **OwnIR** facts, and lets the **existing** Python core
5+
(`python -m ownlang ownir`) flag the leak. Same seam, same checker, different
6+
skin — a `useEffect` acquire without a cleanup `return` is the core's `OWN001`,
7+
exactly as a C# `event +=` without `-=` is.
8+
9+
> The same OwnIR idea behind WPF subscription leaks can model React effect storms.
10+
11+
This is a **spike**, per [P-020](../../docs/proposals/P-020-ownts-react-effects.md)
12+
— deliberately *not* a TypeScript analyzer. Extraction is heuristic (brace
13+
matching + cleanup-verb detection), not a real TS parse. Its only job is to prove
14+
the seam is cross-language.
15+
16+
## Run it
17+
18+
```bash
19+
# print the OwnIR facts extracted from a .tsx
20+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx
21+
22+
# extract + run straight through the core (the "catch")
23+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx --check
24+
25+
# or the real two-step CLI, same as the C# side:
26+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx -o facts.json
27+
python -m ownlang ownir facts.json --format sarif
28+
29+
# pin the spike
30+
python frontend/ownts/test_ownts.py
31+
```
32+
33+
`Dashboard.tsx` drops three `OWN001` leaks; `DashboardClean.tsx` (every acquire
34+
has a cleanup `return`) is silent.
35+
36+
```
37+
Dashboard.tsx:13: error: [OWN001] timer 'setInterval(() =>' ... never stopped ... (leak) [resource: timer]
38+
Dashboard.tsx:21: error: [OWN001] the result of '.subscribe(...)' is ignored ... (leak) [resource: subscription token]
39+
Dashboard.tsx:27: error: [OWN001] event '.addEventListener(...)' ... never unsubscribed ... (leak) [resource: subscription token]
40+
```
41+
42+
## What it catches — the honest `Own.React` slice
43+
44+
| EFF | Pattern | OwnIR `resource` | Core verdict |
45+
|-----|---------|------------------|--------------|
46+
| `EFF004` | `setInterval`/`setTimeout` in an effect, no `clearInterval`/`clearTimeout` cleanup | `timer` | `OWN001` |
47+
| `EFF003` | `X.subscribe(...)` with no `unsubscribe` cleanup | `subscribe` | `OWN001` |
48+
| `EFF003` | `addEventListener` with no `removeEventListener` cleanup | `subscription` | `OWN001` |
49+
50+
These three **are** the existing acquire→release model, just emitted by an OwnTS
51+
frontend. The core is untouched.
52+
53+
## What it does NOT do (and why that's honest)
54+
55+
`EFF001/002` — the unstable-dependency "effect storm" (the Cloudflare 12-Sep-2025
56+
shape: a `useEffect` whose dep object is re-created each render, re-firing the
57+
effect and storming the API) — **is not an acquire/release leak.** It needs a new
58+
core capability (dependency-identity *stability*), which the core does not have.
59+
Per P-020, `EFF001` must **not** masquerade as `OWN001`.
60+
61+
So the scanner only emits a *frontend-only, clearly-labelled* heuristic note for
62+
`EFF001` candidates on **stderr** — never a core-verified finding:
63+
64+
```
65+
Dashboard.tsx:34: EFF001 (frontend heuristic, NOT core-verified): dependency
66+
'filters' is a fresh object/array identity every render; the effect does IO —
67+
possible request storm. Stabilise with useMemo.
68+
```
69+
70+
The one-liner the spike pays for: **"Not all lifecycle bugs leak memory. Some
71+
leak requests."** We make **no** "Own would have prevented the Cloudflare outage"
72+
claim — see P-020's Non-goals.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Leaky dashboard — three useEffect acquires with NO cleanup return.
2+
// Each is the React skin of the same acquire->release contract the .NET core
3+
// already checks (timer / subscription token). Run:
4+
//
5+
// python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx --check
6+
//
7+
// Expect three OWN001 findings (EFF004 timer, EFF003 subscribe, EFF003 listener).
8+
import { useEffect } from "react";
9+
10+
export function Dashboard({ tenantId }: { tenantId: string }) {
11+
// EFF004 — interval started, never cleared: the timer keeps the component alive.
12+
useEffect(() => {
13+
const id = setInterval(() => {
14+
fetch(`/api/tenant/${tenantId}/metrics`);
15+
}, 1000);
16+
// no `return () => clearInterval(id)` — leak
17+
}, [tenantId]);
18+
19+
// EFF003 — observable subscription whose teardown is never returned.
20+
useEffect(() => {
21+
bus.subscribe((msg) => console.log(msg));
22+
// no `return () => sub.unsubscribe()` — leak
23+
}, []);
24+
25+
// EFF003 — DOM listener added, never removed.
26+
useEffect(() => {
27+
window.addEventListener("resize", onResize);
28+
// no `return () => window.removeEventListener("resize", onResize)` — leak
29+
}, []);
30+
31+
// EFF001 (frontend heuristic only — NOT a core OWN001): `filters` is a fresh
32+
// object identity every render, and the effect does IO -> request storm.
33+
const filters = { tenantId };
34+
useEffect(() => {
35+
fetch(`/api/tenant/${filters.tenantId}`);
36+
}, [filters]);
37+
38+
return <div>dashboard</div>;
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Clean dashboard — every acquire has a matching cleanup return, and the unstable
2+
// dependency is stabilised with useMemo. The extractor sees `released: true` for
3+
// each resource, so the core stays silent (zero findings). Run:
4+
//
5+
// python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx --check
6+
import { useEffect, useMemo } from "react";
7+
8+
export function Dashboard({ tenantId }: { tenantId: string }) {
9+
useEffect(() => {
10+
const id = setInterval(() => {
11+
fetch(`/api/tenant/${tenantId}/metrics`);
12+
}, 1000);
13+
return () => clearInterval(id); // released
14+
}, [tenantId]);
15+
16+
useEffect(() => {
17+
const sub = bus.subscribe((msg) => console.log(msg));
18+
return () => sub.unsubscribe(); // released
19+
}, []);
20+
21+
useEffect(() => {
22+
window.addEventListener("resize", onResize);
23+
return () => window.removeEventListener("resize", onResize); // released
24+
}, []);
25+
26+
// stable identity across renders -> no effect storm
27+
const filters = useMemo(() => ({ tenantId }), [tenantId]);
28+
useEffect(() => {
29+
fetch(`/api/tenant/${filters.tenantId}`);
30+
}, [filters]);
31+
32+
return <div>dashboard</div>;
33+
}

0 commit comments

Comments
 (0)