@reticlehq/react 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,7 +31,7 @@
31
31
  * an opaque evaluation error).
32
32
  */
33
33
  export function readComponentAt(el) {
34
- if (el === null)
34
+ if (null === el)
35
35
  return { ok: false, reason: 'no-element' };
36
36
  try {
37
37
  const FIBER_PREFIXES = ['__reactFiber$', '__reactInternalInstance$'];
@@ -39,6 +39,23 @@ export function readComponentAt(el) {
39
39
  if (key === undefined)
40
40
  return { ok: false, reason: 'no-fiber' };
41
41
  let fiber = el[key];
42
+ // React keeps two fibers per element (current ↔ work-in-progress) and swaps them on every commit,
43
+ // but the `__reactFiber$…` key keeps pointing at the one created at mount — so from the second
44
+ // render on it is the PREVIOUS commit's fiber half the time, and hooks read one commit behind.
45
+ // Climb to the HostRoot: the tree is the committed one iff its FiberRoot's `current` is the top
46
+ // fiber reached. Otherwise the committed counterpart is `alternate`. Unknown shape => leave as-is.
47
+ const alternate = fiber?.alternate;
48
+ if (fiber !== null && fiber !== undefined && alternate !== null && alternate !== undefined) {
49
+ let top = fiber;
50
+ let climbed = 0;
51
+ while (top.return !== null && top.return !== undefined && climbed < 200) {
52
+ top = top.return;
53
+ climbed += 1;
54
+ }
55
+ const committed = top.stateNode?.current;
56
+ if ('object' === typeof committed && null !== committed && committed !== top)
57
+ fiber = alternate;
58
+ }
42
59
  // Climb to the nearest function-component fiber; host fibers (strings) carry no hook state.
43
60
  let guard = 0;
44
61
  while (fiber !== null &&
@@ -48,20 +65,20 @@ export function readComponentAt(el) {
48
65
  fiber = fiber.return;
49
66
  guard += 1;
50
67
  }
51
- if (fiber === null || fiber === undefined)
68
+ if (null === fiber || fiber === undefined)
52
69
  return { ok: false, reason: 'no-component-fiber' };
53
70
  const type = fiber.type;
54
71
  const component = type?.displayName ?? type?.name ?? undefined;
55
72
  const hooks = [];
56
73
  let hook = fiber.memoizedState;
57
74
  let i = 0;
58
- while (hook !== null && typeof hook === 'object' && i < 100) {
75
+ while (hook !== null && 'object' === typeof hook && i < 100) {
59
76
  const state = hook.memoizedState;
60
77
  // Only carry JSON-serializable primitives/plain values across the boundary; a function or DOM
61
78
  // node in a hook (a ref, a callback) is dropped rather than breaking the whole read.
62
- hooks.push(typeof state === 'function' ? '[fn]' : state);
79
+ hooks.push('function' === typeof state ? '[fn]' : state);
63
80
  const next = hook.next;
64
- if (next === null || typeof next !== 'object')
81
+ if (null === next || typeof next !== 'object')
65
82
  break;
66
83
  hook = next;
67
84
  i += 1;
@@ -86,7 +103,7 @@ export function buildReaderExpression(selector) {
86
103
  * rather than surfacing as a confusing tool output. */
87
104
  export function parseComponentRead(raw) {
88
105
  if (typeof raw !== 'object' ||
89
- raw === null ||
106
+ null === raw ||
90
107
  typeof raw.ok !== 'boolean') {
91
108
  return { ok: false, reason: 'malformed-read' };
92
109
  }
@@ -7,10 +7,11 @@
7
7
  export interface CommitAggregator {
8
8
  onCommit(): void;
9
9
  }
10
- export interface CommitAggregatorOptions {
10
+ interface CommitAggregatorOptions {
11
11
  /** Injected scheduler (rAF / setTimeout) — deterministic in tests. */
12
12
  schedule: (fn: () => void) => void;
13
13
  /** Called once per window with the number of commits accumulated since the last flush. */
14
14
  flush: (commits: number) => void;
15
15
  }
16
16
  export declare function createCommitAggregator(options: CommitAggregatorOptions): CommitAggregator;
17
+ export {};
@@ -8,7 +8,7 @@ import { RETICLE_ERROR_BOUNDARY_SIGNAL, TRANSPORT_LIMITS } from '@reticlehq/core
8
8
  */
9
9
  const MAX_STACK_LEN = TRANSPORT_LIMITS.MAX_STACK_LENGTH;
10
10
  function cap(value) {
11
- if (value === undefined || value.length === 0)
11
+ if (value === undefined || 0 === value.length)
12
12
  return undefined;
13
13
  return value.length > MAX_STACK_LEN ? value.slice(0, MAX_STACK_LEN) : value;
14
14
  }
@@ -20,7 +20,7 @@ const HYDRATION_ERROR_CODES = new Set([
20
20
  '425',
21
21
  ]);
22
22
  function cap(value) {
23
- if (value === undefined || value.length === 0)
23
+ if (value === undefined || 0 === value.length)
24
24
  return undefined;
25
25
  return value.length > MAX_STACK_LEN ? value.slice(0, MAX_STACK_LEN) : value;
26
26
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,24 @@
1
1
  import { type ComponentInfo } from '@reticlehq/browser';
2
2
  import { type ComponentStateResult } from '@reticlehq/core';
3
+ /**
4
+ * Make a source pointer repo-relative.
5
+ *
6
+ * Both sides of this comparison are native paths, and on Windows that means BACKSLASHES on both:
7
+ * `C:\Users\dev\app` and `C:\Users\dev\app\src\Counter.tsx`. Building the prefix as
8
+ * `root + '/'` produced `C:\Users\dev\app/` — a mixed separator that can never match the start of
9
+ * the file — so on Windows the root was never stripped and every pointer came back as an absolute
10
+ * path from the developer's own machine. Exactly what the root exists to prevent, on the platform
11
+ * carrying two thirds of Reticle's users.
12
+ *
13
+ * Normalizing to posix fixes three cases at once: native Windows, the mixed pair Vite produces (it
14
+ * normalizes module ids to forward slashes while `cwd()` stays native), and plain posix. The result
15
+ * is always forward-slashed, because flow anchors and `file:line` pointers all speak posix.
16
+ *
17
+ * The drive letter is compared case-insensitively — Windows treats `C:\App` and `c:\app` as the
18
+ * same path, and the two spellings routinely come from different APIs — but the returned suffix is
19
+ * sliced from the ORIGINAL file, so nothing else gets case-folded.
20
+ */
21
+ export declare function relativeToRoot(file: string): string;
3
22
  /** Walk the fiber tree from a DOM node up to the root, collecting components + source. */
4
23
  export declare function identify(el: Element): ComponentInfo | null;
5
24
  export declare function readState(el: Element): ComponentStateResult;
package/dist/index.js CHANGED
@@ -1,5 +1,46 @@
1
1
  import { registerAdapter } from '@reticlehq/browser';
2
- import { ComponentStateReason, DATA_RETICLE_SOURCE_ATTR, } from '@reticlehq/core';
2
+ import { ComponentStateReason, DATA_RETICLE_SOURCE_ATTR, RETICLE_ROOT_GLOBAL, } from '@reticlehq/core';
3
+ /**
4
+ * React's `_debugSource.fileName` is ABSOLUTE; the babel stamp is repo-relative. Both surface to the
5
+ * agent as `source`, so the same product emitted two path shapes depending on which React version an
6
+ * app was on — and `/Users/someone/tmp/wt/apps/x/src/A.tsx` is noise in a report, not a pointer.
7
+ * The build plugins define the root; without one we return the path unchanged rather than guessing.
8
+ */
9
+ /** Backslashes to forward slashes, so a Windows path can be compared with a posix one. */
10
+ function toPosix(path) {
11
+ return path.replace(/\\/g, '/');
12
+ }
13
+ /**
14
+ * Make a source pointer repo-relative.
15
+ *
16
+ * Both sides of this comparison are native paths, and on Windows that means BACKSLASHES on both:
17
+ * `C:\Users\dev\app` and `C:\Users\dev\app\src\Counter.tsx`. Building the prefix as
18
+ * `root + '/'` produced `C:\Users\dev\app/` — a mixed separator that can never match the start of
19
+ * the file — so on Windows the root was never stripped and every pointer came back as an absolute
20
+ * path from the developer's own machine. Exactly what the root exists to prevent, on the platform
21
+ * carrying two thirds of Reticle's users.
22
+ *
23
+ * Normalizing to posix fixes three cases at once: native Windows, the mixed pair Vite produces (it
24
+ * normalizes module ids to forward slashes while `cwd()` stays native), and plain posix. The result
25
+ * is always forward-slashed, because flow anchors and `file:line` pointers all speak posix.
26
+ *
27
+ * The drive letter is compared case-insensitively — Windows treats `C:\App` and `c:\app` as the
28
+ * same path, and the two spellings routinely come from different APIs — but the returned suffix is
29
+ * sliced from the ORIGINAL file, so nothing else gets case-folded.
30
+ */
31
+ export function relativeToRoot(file) {
32
+ const root = globalThis[RETICLE_ROOT_GLOBAL];
33
+ if (typeof root !== 'string' || 0 === root.length)
34
+ return file;
35
+ const normalizedRoot = toPosix(root).replace(/\/+$/, '');
36
+ if (0 === normalizedRoot.length)
37
+ return file;
38
+ const normalizedFile = toPosix(file);
39
+ const prefix = `${normalizedRoot}/`;
40
+ const matches = normalizedFile.startsWith(prefix) ||
41
+ normalizedFile.toLowerCase().startsWith(prefix.toLowerCase());
42
+ return matches ? normalizedFile.slice(prefix.length) : file;
43
+ }
3
44
  const FIBER_PREFIXES = ['__reactFiber$', '__reactInternalInstance$'];
4
45
  const MAX_DEPTH = 200;
5
46
  const MAX_HOOKS = 100;
@@ -20,22 +61,56 @@ const FRAMEWORK_NOISE = /(\.Provider|\.Consumer)$|^(Fragment|Suspense|StrictMode
20
61
  function isFrameworkNoise(name) {
21
62
  return FRAMEWORK_NOISE.test(name);
22
63
  }
64
+ /**
65
+ * Resolve a fiber to the one in the COMMITTED tree.
66
+ *
67
+ * React keeps two fibers per element — `current` and its `alternate` (the work-in-progress) — and
68
+ * swaps which is which on every commit. The `__reactFiber$…` key on a host DOM node keeps pointing at
69
+ * the fiber object created at mount, so from the second render on it is the previous commit's fiber
70
+ * half the time: hooks read off it alternate correct/stale, one commit behind.
71
+ *
72
+ * The principled test, not a heuristic: climb `return` to the HostRoot, whose `stateNode` is the
73
+ * FiberRoot. The tree we climbed is the committed one iff `fiberRoot.current` is the top fiber we
74
+ * reached. If it is not, the committed counterpart is `fiber.alternate`. Anything unrecognizable
75
+ * (no alternate, no FiberRoot, a detached fiber) returns the fiber unchanged — a possibly-stale read
76
+ * is still better than none, and this must never throw on a React version we have not seen.
77
+ */
78
+ function currentFiber(fiber) {
79
+ const alternate = fiber.alternate;
80
+ if (alternate === undefined || null === alternate)
81
+ return fiber;
82
+ let top = fiber;
83
+ let depth = 0;
84
+ while (top.return !== null && depth < MAX_DEPTH) {
85
+ top = top.return;
86
+ depth += 1;
87
+ }
88
+ const fiberRoot = top.stateNode;
89
+ if (typeof fiberRoot !== 'object' || null === fiberRoot)
90
+ return fiber;
91
+ const committed = fiberRoot.current;
92
+ if (typeof committed !== 'object' || null === committed)
93
+ return fiber;
94
+ return committed === top ? fiber : alternate;
95
+ }
23
96
  function getFiber(el) {
24
97
  const key = Object.keys(el).find((k) => FIBER_PREFIXES.some((p) => k.startsWith(p)));
25
98
  if (key === undefined)
26
99
  return null;
27
100
  const value = el[key];
28
- return (value ?? null);
101
+ if (value === undefined || null === value)
102
+ return null;
103
+ return currentFiber(value);
29
104
  }
30
105
  /** Display name of a component type (function, forwardRef/memo object, or host string). */
31
106
  function componentName(type) {
32
- if (typeof type === 'function') {
107
+ if ('function' === typeof type) {
33
108
  const fn = type;
34
109
  if (fn.displayName !== undefined && fn.displayName.length > 0)
35
110
  return fn.displayName;
36
111
  return fn.name !== undefined && fn.name.length > 0 ? fn.name : null;
37
112
  }
38
- if (typeof type === 'object' && type !== null) {
113
+ if ('object' === typeof type && type !== null) {
39
114
  const obj = type;
40
115
  return obj.displayName !== undefined && obj.displayName.length > 0 ? obj.displayName : null;
41
116
  }
@@ -56,11 +131,17 @@ export function identify(el) {
56
131
  if (!isFrameworkNoise(name) && stack[stack.length - 1] !== name)
57
132
  stack.push(name);
58
133
  }
59
- if (source === undefined && fiber._debugSource !== undefined) {
60
- source = { file: fiber._debugSource.fileName, line: fiber._debugSource.lineNumber };
61
- if (fiber._debugSource.columnNumber !== undefined) {
62
- source.column = fiber._debugSource.columnNumber;
63
- }
134
+ // `identify()` is on the ACT path as well as the inspect path, so one null fiber anywhere in
135
+ // this walk used to take out reticle_act / reticle_act_and_wait / reticle_inspect for the whole
136
+ // app: the read-only tools kept working, so the agent could see the app perfectly and could not
137
+ // touch it. The throw also pre-empted the React 19 attribute fallback below, which would have
138
+ // produced the source anyway.
139
+ const debugSource = fiber._debugSource;
140
+ if (source === undefined && debugSource !== undefined && debugSource !== null) {
141
+ source = { file: relativeToRoot(debugSource.fileName), line: debugSource.lineNumber };
142
+ const column = debugSource.columnNumber;
143
+ if (column !== undefined && column !== null)
144
+ source.column = column;
64
145
  }
65
146
  fiber = fiber.return;
66
147
  }
@@ -71,7 +152,7 @@ export function identify(el) {
71
152
  }
72
153
  // Prefer the de-noised stack; fall back to the nearest raw name if filtering left nothing.
73
154
  const componentStack = stack.length > 0 ? stack : rawStack.slice(0, 1);
74
- if (componentStack.length === 0 && source === undefined)
155
+ if (0 === componentStack.length && source === undefined)
75
156
  return null;
76
157
  const info = { componentStack };
77
158
  if (source !== undefined)
@@ -81,10 +162,10 @@ export function identify(el) {
81
162
  function sourceFromAttribute(el) {
82
163
  const stamped = el.closest(`[${DATA_RETICLE_SOURCE_ATTR}]`);
83
164
  const raw = stamped?.getAttribute(DATA_RETICLE_SOURCE_ATTR);
84
- if (raw === null || raw === undefined)
165
+ if (null === raw || raw === undefined)
85
166
  return undefined;
86
167
  const match = /^(.*):(\d+):(\d+)$/.exec(raw);
87
- if (match === null)
168
+ if (null === match)
88
169
  return undefined;
89
170
  const [, file, line, column] = match;
90
171
  if (file === undefined || line === undefined || column === undefined)
@@ -97,7 +178,7 @@ function nearestComponentFiber(el) {
97
178
  let depth = 0;
98
179
  while (fiber !== null && depth < MAX_DEPTH) {
99
180
  depth += 1;
100
- if (typeof fiber.type === 'function' || typeof fiber.elementType === 'function')
181
+ if ('function' === typeof fiber.type || 'function' === typeof fiber.elementType)
101
182
  return fiber;
102
183
  fiber = fiber.return;
103
184
  }
@@ -110,12 +191,12 @@ function nearestComponentFiber(el) {
110
191
  * serializing those un-guarded can cause hangs. Never throws.
111
192
  */
112
193
  function safeValue(value, depth, seen) {
113
- if (value === null)
194
+ if (null === value)
114
195
  return null;
115
196
  const t = typeof value;
116
- if (t === 'string' || t === 'number' || t === 'boolean')
197
+ if ('string' === t || 'number' === t || 'boolean' === t)
117
198
  return value;
118
- if (t === 'undefined' || t === 'function' || t === 'symbol' || t === 'bigint')
199
+ if ('undefined' === t || 'function' === t || 'symbol' === t || 'bigint' === t)
119
200
  return null;
120
201
  if (typeof Node !== 'undefined' && value instanceof Node)
121
202
  return DOM_NODE_MARKER;
@@ -146,27 +227,27 @@ function safeValue(value, depth, seen) {
146
227
  */
147
228
  /** Build a success result, omitting `component` when the name is unknown (exactOptional-safe). */
148
229
  function ok(name, hooks) {
149
- return name === null ? { ok: true, hooks } : { ok: true, component: name, hooks };
230
+ return null === name ? { ok: true, hooks } : { ok: true, component: name, hooks };
150
231
  }
151
232
  export function readState(el) {
152
233
  try {
153
234
  const fiber = nearestComponentFiber(el);
154
- if (fiber === null) {
235
+ if (null === fiber) {
155
236
  return { ok: false, reason: ComponentStateReason.UNAVAILABLE };
156
237
  }
157
238
  const name = componentName(fiber.elementType ?? fiber.type);
158
239
  const head = fiber.memoizedState;
159
- if (typeof head !== 'object' || head === null) {
240
+ if (typeof head !== 'object' || null === head) {
160
241
  return ok(name, []);
161
242
  }
162
243
  const hooks = [];
163
244
  const seen = new WeakSet();
164
245
  let hook = head;
165
246
  let i = 0;
166
- while (typeof hook === 'object' && i < MAX_HOOKS) {
247
+ while ('object' === typeof hook && i < MAX_HOOKS) {
167
248
  hooks.push(safeValue(hook.memoizedState, 0, seen));
168
249
  const next = hook.next;
169
- if (next === null || typeof next !== 'object')
250
+ if (null === next || typeof next !== 'object')
170
251
  break;
171
252
  hook = next;
172
253
  i += 1;
@@ -191,10 +272,10 @@ const HOVER_HANDLER_KEYS = [
191
272
  export function hasHoverHandlers(el) {
192
273
  const fiber = getFiber(el);
193
274
  const props = fiber?.memoizedProps;
194
- if (typeof props !== 'object' || props === null)
275
+ if (typeof props !== 'object' || null === props)
195
276
  return false;
196
277
  const p = props;
197
- return HOVER_HANDLER_KEYS.some((k) => typeof p[k] === 'function');
278
+ return HOVER_HANDLER_KEYS.some((k) => 'function' === typeof p[k]);
198
279
  }
199
280
  import { installRenderMeter } from './render-meter.js';
200
281
  let installed = false;
@@ -38,7 +38,7 @@ function reticleInstance() {
38
38
  // storm is one event of magnitude N, never a per-render flood. requestAnimationFrame when available.
39
39
  const commitStream = createCommitAggregator({
40
40
  schedule: (fn) => {
41
- if (typeof requestAnimationFrame === 'function')
41
+ if ('function' === typeof requestAnimationFrame)
42
42
  requestAnimationFrame(() => fn());
43
43
  else
44
44
  setTimeout(fn, 16);
@@ -110,7 +110,7 @@ export function installRenderMeter() {
110
110
  };
111
111
  }
112
112
  else {
113
- const original = typeof existing.onCommitFiberRoot === 'function'
113
+ const original = 'function' === typeof existing.onCommitFiberRoot
114
114
  ? existing.onCommitFiberRoot.bind(existing)
115
115
  : undefined;
116
116
  existing.onCommitFiberRoot = (...args) => {
package/dist/store.js CHANGED
@@ -29,7 +29,7 @@ export function useReticleStore(name, value) {
29
29
  handle.current ??= pushStore(value);
30
30
  useEffect(() => {
31
31
  const current = handle.current;
32
- if (current === null)
32
+ if (null === current)
33
33
  return;
34
34
  registerStore(name, current.store);
35
35
  return () => unregisterStore(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/react",
3
- "version": "2.3.0",
3
+ "version": "2.5.0",
4
4
  "description": "Reticle React adapter — maps a DOM ref to its component stack and source file via the fiber tree.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,11 +13,12 @@
13
13
  "./store": {
14
14
  "types": "./dist/store.d.ts",
15
15
  "default": "./dist/store.js"
16
- }
16
+ },
17
+ "./package.json": "./package.json"
17
18
  },
18
19
  "dependencies": {
19
- "@reticlehq/browser": "2.3.0",
20
- "@reticlehq/core": "2.3.0"
20
+ "@reticlehq/browser": "2.5.0",
21
+ "@reticlehq/core": "2.5.0"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "react": ">=18"
@@ -60,6 +61,7 @@
60
61
  "build": "tsc -b",
61
62
  "typecheck": "tsc -b",
62
63
  "lint": "eslint src",
63
- "test:unit": "vitest run src --passWithNoTests"
64
+ "test:unit": "vitest run src --passWithNoTests",
65
+ "test:coverage": "vitest run src --passWithNoTests --coverage.enabled --coverage.provider=v8 --coverage.reporter=text-summary --coverage.include='src/**/*.ts' --coverage.exclude='src/**/*.test.ts'"
64
66
  }
65
67
  }