Skip to content

Commit 11db18a

Browse files
authored
Merge pull request #144 from PhysShell/claude/multilang-poc-useeffect-27n6xg
feat(ownts): мультиланг-PoC — React useEffect через OwnIR + EFF001 (effect storm) как настоящий core-анализ
2 parents 98ebb51 + f61a1ca commit 11db18a

14 files changed

Lines changed: 1312 additions & 7 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,72 @@ jobs:
10191019
|| { echo "FAIL: explain --json did not harvest OWN001 from the SARIF log"; exit 1; }
10201020
echo "OK: explain answers a code and harvests codes from a real findings/SARIF file"
10211021
1022+
# The OwnTS frontend spike (P-020 Own.React): the SAME OwnIR seam, fed from a
1023+
# React .tsx instead of C#. Two analyses over the one core: (1) a useEffect
1024+
# acquire (timer / subscribe / listener) with no cleanup return is the core's
1025+
# OWN001 — the cross-language leak model; (2) EFF001, a NEW core analysis
1026+
# (ownlang/effects.py) — an IO effect whose dependency identity is unstable
1027+
# (a render-scope object literal) re-fires every render: the effect storm. The
1028+
# frontend emits only facts; the stability verdict is the core's. No dotnet.
1029+
ownts-react-effects:
1030+
name: OwnTS (React useEffect) -> OwnIR -> core
1031+
runs-on: ubuntu-latest
1032+
steps:
1033+
- uses: actions/checkout@v4
1034+
- uses: actions/setup-python@v5
1035+
with:
1036+
python-version: "3.13"
1037+
- name: Pin the spike (leaky=3xOWN001+EFF001, clean=0, showcase=2xEFF001)
1038+
run: python frontend/ownts/test_ownts.py
1039+
- name: Extract OwnIR facts from a React .tsx and check through the core
1040+
run: |
1041+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx \
1042+
-o "$RUNNER_TEMP/dash.facts.json"
1043+
cat "$RUNNER_TEMP/dash.facts.json"
1044+
out=$(python -m ownlang ownir "$RUNNER_TEMP/dash.facts.json" || true)
1045+
echo "$out"
1046+
echo "$out" | grep -q "Dashboard.tsx" \
1047+
|| { echo "FAIL: expected findings located at the .tsx"; exit 1; }
1048+
echo "$out" | grep -q "resource: timer" \
1049+
|| { echo "FAIL: expected the setInterval [resource: timer] leak"; exit 1; }
1050+
[ "$(echo "$out" | grep -c 'OWN001')" -eq 3 ] \
1051+
|| { echo "FAIL: expected three OWN001 leaks"; exit 1; }
1052+
echo "$out" | grep -q "\[EFF001\].*request storm" \
1053+
|| { echo "FAIL: expected the EFF001 effect-storm verdict"; exit 1; }
1054+
# exact finding count (a code-tagged line each), not a substring of "4 finding"
1055+
[ "$(echo "$out" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 4 ] \
1056+
|| { echo "FAIL: expected 3 OWN001 + 1 EFF001 = 4 findings"; exit 1; }
1057+
- name: EFF001 stability showcase — only provable storms fire (low FP)
1058+
run: |
1059+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectStorm.tsx \
1060+
-o "$RUNNER_TEMP/storm.facts.json"
1061+
storm=$(python -m ownlang ownir "$RUNNER_TEMP/storm.facts.json" || true)
1062+
echo "$storm"
1063+
# the direct object dep and its derived alias fire; memo/ref/call/primitive/no-IO stay silent
1064+
[ "$(echo "$storm" | grep -c '\[EFF001\]')" -eq 2 ] \
1065+
|| { echo "FAIL: expected exactly two EFF001 (object dep + derived alias)"; exit 1; }
1066+
echo "$storm" | grep -q "derives from" \
1067+
|| { echo "FAIL: expected the derivation (propagation) verdict"; exit 1; }
1068+
- name: Edge cases — partial timer cleanup + nested-scope shadow
1069+
run: |
1070+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectEdges.tsx \
1071+
-o "$RUNNER_TEMP/edges.facts.json"
1072+
edges=$(python -m ownlang ownir "$RUNNER_TEMP/edges.facts.json" || true)
1073+
echo "$edges"
1074+
# only the SECOND, uncleared interval leaks; the memoized dep is not shadowed
1075+
[ "$(echo "$edges" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 1 ] \
1076+
|| { echo "FAIL: expected exactly one OWN001 (the uncleared timer)"; exit 1; }
1077+
echo "$edges" | grep -q "pollB" \
1078+
|| { echo "FAIL: the leak must be the second (uncleared) interval"; exit 1; }
1079+
- name: The clean fixture (cleanups + useMemo'd dep) is silent
1080+
run: |
1081+
python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx \
1082+
-o "$RUNNER_TEMP/clean.facts.json"
1083+
clean=$(python -m ownlang ownir "$RUNNER_TEMP/clean.facts.json" || true)
1084+
echo "$clean"
1085+
[ "$(echo "$clean" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 0 ] \
1086+
|| { echo "FAIL: cleaned-up + memoised effects must not fire"; exit 1; }
1087+
10221088
# The distribution surface (Уровень 1): the own-check.sh orchestrator walks a
10231089
# directory of real C# and prints findings in the host-parseable formats the
10241090
# GitHub Action (PR annotations) and a VS Error List (MSBuild) consume — and

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
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).
9+
- **`EFF001` is now a real core analysis** (`ownlang/effects.py`), answering Open
10+
question 1 below: a self-contained **dependency-identity stability** lattice with
11+
reference propagation, computed by the **core** over an OwnIR `effects` fact block
12+
the frontend emits — *not* a frontend heuristic, and *not* an `OWN001`. It is its
13+
own core code (`EFF001`), exactly as the DI captive check is `DI001`. The frontend
14+
states only what each render-scope binding syntactically is; the core decides
15+
stability. `EFF002` (network IO with no stable guard) is governed by the same
16+
analysis and left as the next increment.
617
- **Depends on:** [P-017](P-017-multi-stack-frontends.md) (the OwnTS frontend &
718
confidence tiers that this profile feeds), `spec/OwnCore.md` (the acquire/release
819
vocabulary), [P-004](P-004-wpf-lifetime-profile.md) (WPF — the same lifecycle
@@ -145,12 +156,18 @@ has on the .NET side (P-013/P-015); 1/2/6/7 are this proposal's actual work.
145156

146157
## Open questions
147158

148-
1. **The new analysis for `EFF001/002`.** Detecting "dependency identity is unstable
149-
across renders" needs render-scope object-literal/identity reasoning the core
150-
has no model for. Is that a small, self-contained *stability* fact
151-
(`unstable(dep, effect)`) the OwnTS frontend can emit and the core treat like an
152-
acquire-site, or a genuinely new core analysis? This is the gating question — do
153-
not let `EFF001` masquerade as an `OWN001` leak.
159+
1. **The new analysis for `EFF001`.** ✅ **Answered (implemented).** It is a
160+
genuinely new core analysis, not an `OWN001` acquire-site — and *not* a frontend
161+
verdict either. The resolution: the frontend emits per-binding **facts** (each
162+
render-scope binding's syntactic `init` kind + the names it references, the dep
163+
list, whether the body does IO) in an OwnIR `effects` block; the **core**
164+
(`ownlang/effects.py`) runs an identity-stability lattice (`STABLE < UNKNOWN <
165+
UNSTABLE`) to a fixpoint over the references and decides `unstable(dep)`. An
166+
effect is `EFF001` iff it does IO **and** a dep is *provably* `UNSTABLE`
167+
(`UNKNOWN`/memoised/primitive clear it — low false positives). The gating
168+
discipline held: `EFF001` is its own core code (like `DI001`), never an `OWN001`.
169+
`EFF002` (network IO with no stable guard) is intended to reuse this same
170+
lattice but is **not yet implemented** — it remains the next increment.
154171
2. **Confidence tier.** `EFF001` clearly wants TS-mode type info (is the dep an
155172
object literal? does the body do IO?). What, if anything, survives into JS mode
156173
(P-017's heuristic tier) as a warning?

‎frontend/ownts/README.md‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
# the EFF001 stability showcase (propagation + the conservative silent cases)
26+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectStorm.tsx --check
27+
28+
# or the real two-step CLI, same as the C# side:
29+
python frontend/ownts/ownts.py frontend/ownts/examples/Dashboard.tsx -o facts.json
30+
python -m ownlang ownir facts.json --format sarif
31+
32+
# pin the spike
33+
python frontend/ownts/test_ownts.py
34+
```
35+
36+
`Dashboard.tsx` drops three `OWN001` leaks **and** one `EFF001` effect storm;
37+
`DashboardClean.tsx` (every acquire has a cleanup `return`, and the unstable dep is
38+
`useMemo`'d) is silent.
39+
40+
```text
41+
Dashboard.tsx:13: error: [OWN001] timer 'setInterval(() =>' ... never stopped ... (leak) [resource: timer]
42+
Dashboard.tsx:21: error: [OWN001] the result of '.subscribe(...)' is ignored ... (leak) [resource: subscription token]
43+
Dashboard.tsx:27: error: [OWN001] event '.addEventListener(...)' ... never unsubscribed ... (leak) [resource: subscription token]
44+
Dashboard.tsx:34: error: [EFF001] effect re-runs on every render: dependency 'filters' is an object literal ... can become a request storm ... [resource: react effect]
45+
```
46+
47+
## What it catches — the honest `Own.React` slice
48+
49+
| EFF | Pattern | OwnIR fact | Core verdict |
50+
|-----|---------|-----------|--------------|
51+
| `EFF004` | `setInterval`/`setTimeout` in an effect, no `clearInterval`/`clearTimeout` cleanup | `resource: timer` | `OWN001` |
52+
| `EFF003` | `X.subscribe(...)` with no `unsubscribe` cleanup | `resource: subscribe` | `OWN001` |
53+
| `EFF003` | `addEventListener` with no `removeEventListener` cleanup | `resource: subscription` | `OWN001` |
54+
| `EFF001` | IO effect with a render-unstable dependency identity | `effects` block | `EFF001` (new core analysis — see below) |
55+
56+
These three **are** the existing acquire→release model, just emitted by an OwnTS
57+
frontend. The core is untouched.
58+
59+
## EFF001 — a real core analysis (not a heuristic, not OWN001)
60+
61+
`EFF001` — the unstable-dependency "effect storm" (the Cloudflare 12-Sep-2025
62+
shape: a `useEffect` whose dep object is re-created each render, re-firing the
63+
effect and storming the API) — **is not an acquire/release leak.** It is a new core
64+
analysis: **dependency-identity stability** (`ownlang/effects.py`), its own core
65+
code like `DI001`, *never* an `OWN001`.
66+
67+
The honest split is preserved end-to-end. The frontend emits only **facts** — for
68+
each `useEffect`, its dep list, whether the body does IO, and a render-scope
69+
**binding table** (what each binding syntactically is: `object`/`array`/`new`,
70+
`memo`/`callback`/`ref`, `ident` derivation + the names it references, `call`, …).
71+
It does **not** pre-judge stability. The **core** runs an identity-stability lattice
72+
(`STABLE < UNKNOWN < UNSTABLE`) to a fixpoint over the references and decides:
73+
74+
```text
75+
EFF001 fires ⟺ the effect does IO ∧ some dep is *provably* UNSTABLE
76+
```
77+
78+
- object/array literal in render scope → UNSTABLE (fresh identity every render)
79+
- `useMemo`/`useCallback`/`useRef`, prop, primitive → STABLE
80+
- an alias/derivation → the worst of what it references (instability *propagates*)
81+
- an opaque `call(...)` → UNKNOWN → **no finding** (conservative; low false positives)
82+
83+
See `examples/EffectStorm.tsx`: two storms fire (a direct object dep and the alias
84+
that derives from it — `derives from 'filters' ... (via alias -> filters)`), while
85+
the memoised, ref, opaque-call, primitive, and no-IO effects all stay silent.
86+
87+
The one-liner the spike pays for: **"Not all lifecycle bugs leak memory. Some
88+
leak requests."** We make **no** "Own would have prevented the Cloudflare outage"
89+
claim — see P-020's Non-goals.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// plus one EFF001 effect-storm finding (the unstable `filters` dependency below).
9+
import { useEffect } from "react";
10+
11+
export function Dashboard({ tenantId }: { tenantId: string }) {
12+
// EFF004 — interval started, never cleared: the timer keeps the component alive.
13+
useEffect(() => {
14+
const id = setInterval(() => {
15+
fetch(`/api/tenant/${tenantId}/metrics`);
16+
}, 1000);
17+
// no `return () => clearInterval(id)` — leak
18+
}, [tenantId]);
19+
20+
// EFF003 — observable subscription whose teardown is never returned.
21+
useEffect(() => {
22+
bus.subscribe((msg) => console.log(msg));
23+
// no `return () => sub.unsubscribe()` — leak
24+
}, []);
25+
26+
// EFF003 — DOM listener added, never removed.
27+
useEffect(() => {
28+
window.addEventListener("resize", onResize);
29+
// no `return () => window.removeEventListener("resize", onResize)` — leak
30+
}, []);
31+
32+
// EFF001 (a separate core analysis — NOT an OWN001 leak): `filters` is a fresh
33+
// object identity every render, and the effect does IO -> request storm.
34+
const filters = { tenantId };
35+
useEffect(() => {
36+
fetch(`/api/tenant/${filters.tenantId}`);
37+
}, [filters]);
38+
39+
return <div>dashboard</div>;
40+
}
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Edge cases the per-resource + render-scope fixes must get right (Codex/CodeRabbit):
2+
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectEdges.tsx --check
3+
// Expect exactly ONE finding: OWN001 on the second, uncleared interval. No EFF001.
4+
import { useEffect, useMemo } from "react";
5+
6+
export function EdgeBoard({ id }: { id: string }) {
7+
// Two timers; only the first is cleared -> the SECOND still leaks. A kind-level
8+
// "is there any clearInterval?" check would wrongly mark both released.
9+
useEffect(() => {
10+
const a = setInterval(pollA, 1000);
11+
const b = setInterval(pollB, 2000);
12+
return () => clearInterval(a); // only `a` cleared; `b` leaks (one OWN001)
13+
}, []);
14+
15+
// The render-scope dep is memoized (stable). A like-named local INSIDE the effect
16+
// callback must not shadow it into a false EFF001 storm.
17+
const filters = useMemo(() => ({ id }), [id]);
18+
useEffect(() => {
19+
const filters = { id }; // nested-scope local — NOT the component's render scope
20+
fetch(`/api/${filters.id}`);
21+
}, [filters]); // refers to the stable outer (memoized) `filters`
22+
23+
return <div>edges</div>;
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// EFF001 showcase — the core's dependency-identity *stability* analysis, not a
2+
// leak. Only the IO effects whose dep is PROVABLY unstable fire; memoised,
3+
// primitive, opaque-call and no-IO cases stay silent (low false positives).
4+
//
5+
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectStorm.tsx --check
6+
//
7+
// Expect exactly two EFF001 findings: the direct object dep, and the alias that
8+
// derives from it.
9+
import { useEffect, useMemo, useRef } from "react";
10+
11+
export function StormBoard({ tenantId }: { tenantId: string }) {
12+
// (1) FIRES — fresh object identity every render + IO.
13+
const filters = { tenantId };
14+
useEffect(() => {
15+
fetch(`/api/tenant/${filters.tenantId}`);
16+
}, [filters]);
17+
18+
// (2) FIRES — `alias` derives from the unstable `filters`; instability propagates.
19+
const alias = filters;
20+
useEffect(() => {
21+
fetch(`/api/alias/${alias.tenantId}`);
22+
}, [alias]);
23+
24+
// (3) SILENT — useMemo gives a stable identity across renders.
25+
const stable = useMemo(() => ({ tenantId }), [tenantId]);
26+
useEffect(() => {
27+
fetch(`/api/stable/${stable.tenantId}`);
28+
}, [stable]);
29+
30+
// (4) SILENT — useRef identity is stable.
31+
const box = useRef({ tenantId });
32+
useEffect(() => {
33+
fetch(`/api/ref/${box.current.tenantId}`);
34+
}, [box]);
35+
36+
// (5) SILENT — opaque call: the core stays conservative (UNKNOWN, no finding).
37+
const computed = makeFilters(tenantId);
38+
useEffect(() => {
39+
fetch(`/api/computed/${computed.tenantId}`);
40+
}, [computed]);
41+
42+
// (6) SILENT — primitive dependency has a stable value identity.
43+
useEffect(() => {
44+
fetch(`/api/primitive/${tenantId}`);
45+
}, [tenantId]);
46+
47+
// (7) SILENT — unstable dep but NO IO: re-running is cheap, not a storm.
48+
const opts = { verbose: true };
49+
useEffect(() => {
50+
console.log(opts.verbose);
51+
}, [opts]);
52+
53+
return <div>storm board</div>;
54+
}

0 commit comments

Comments
 (0)