Skip to content

Commit b9f19df

Browse files
authored
feat: Re.Pack DevTools plugin with agent tools for runtime debugging (#1510)
1 parent f9491e6 commit b9f19df

86 files changed

Lines changed: 10501 additions & 398 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
---
2+
name: repack-debug-runtime
3+
description: >
4+
Use when inspecting a running Re.Pack app or dev server to find out why
5+
something fails at runtime: chunks or scripts not loading (404, timeouts,
6+
cache), Module Federation remotes/shared packages misbehaving, compilation or
7+
HMR errors, LogBox stacks that are not symbolicated, or uncaught JS errors.
8+
Covers starting the tester apps with the Re.Pack DevTools plugin, the
9+
`rozenite agent` tools of `callstack/repack-devtools`, the
10+
`/repack-devtools/*` dev-server endpoints, and playbooks per failure type.
11+
---
12+
13+
# Debug a running Re.Pack app
14+
15+
`packages/repack-devtools-plugin` (private, workspace only) records what
16+
Re.Pack does at runtime and on the dev server, computes diagnostics, and
17+
exposes them two ways:
18+
19+
- **App side** (needs a running app): `rozenite agent callstack/repack-devtools <tool>`
20+
- **Dev-server side** (works without an app, even when the bundle fails to
21+
build): `curl <origin>/repack-devtools/state`
22+
23+
Both are wired into every tester app behind `WITH_ROZENITE=true`; without it
24+
nothing is loaded or bundled. Never touch
25+
ports or devices you did not start: other sessions often own `8081` and
26+
`emulator-5554`.
27+
28+
## 1. Start
29+
30+
```sh
31+
cd apps/tester-app # or tester-federation, tester-federation-v2, tester-expo
32+
WITH_ROZENITE=true pnpm start --port 8091 # MF testers: start:hostapp / start:miniapp
33+
```
34+
35+
The log must show `[Rozenite] Loaded N plugin(s)` including
36+
`@callstack/repack-devtools-plugin`. Check the endpoint before launching the app:
37+
38+
```sh
39+
curl -s localhost:8091/repack-devtools/state?requests=none
40+
```
41+
42+
Point a debug build at the port (Android; package ids are in each `app.json`):
43+
44+
```sh
45+
adb -s <serial> reverse tcp:8091 tcp:8091
46+
adb -s <serial> shell "run-as <pkg> sh -c 'mkdir -p shared_prefs && echo \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\" standalone=\\\"yes\\\" ?><map><string name=\\\"debug_http_host\\\">localhost:8091</string></map>\" > shared_prefs/<pkg>_preferences.xml'"
47+
adb -s <serial> shell am force-stop <pkg>
48+
adb -s <serial> shell am start -n <pkg>/com.microsoft.reacttestapp.MainActivity
49+
```
50+
51+
MF remotes have fixed URLs in the host config (`localhost:8082`, `8083`); also
52+
`adb reverse` those ports. On iOS simulators, `localhost` already reaches the
53+
host; use the dev menu or `RCT_jsLocation` to change the port.
54+
55+
## 2. Inspect
56+
57+
Run from an app directory so `rozenite` resolves (`pnpm exec rozenite …`).
58+
Pass `--port <dev server port>` to every call.
59+
60+
```sh
61+
pnpm exec rozenite agent targets --port 8091 # app connected?
62+
pnpm exec rozenite agent callstack/repack-devtools get-overview --port 8091
63+
pnpm exec rozenite agent session stop --port 8091 # when done
64+
```
65+
66+
**Always start with `get-overview`.** It returns app info (platform, RN
67+
version, bundle URL, public path, dev server), record counts, and
68+
severity-sorted `diagnostics` merged from the app, its dev server and the dev
69+
servers of MF remotes (those are prefixed with `[origin]`; see
70+
`remoteDevServers` for their compile status). Every
71+
diagnostic has a `ref` that tells you which tool gives details:
72+
73+
| `ref` | Follow up with |
74+
| --- | --- |
75+
| `<script uniqueId>` | `get-script --uniqueId <ref>`, `get-script-manager` |
76+
| `call:<seq>` | `list-script-calls --uniqueId <id>` |
77+
| `fault-<n>` / `server-fault-<n>` | `list-faults` (injected by you; not a real bug) |
78+
| `federation-event:<seq>` | `list-federation-events --errorsOnly true` |
79+
| `<instance>:<scope>/<pkg>` | `get-federation` |
80+
| `err-<n>` | `get-error --id <ref>`, then `symbolicate --errorId <ref>` |
81+
| `request:<seq>` | `get-dev-server --requests failed` |
82+
| `compilation:<platform>` | `curl <origin>/repack-devtools/compilation?platform=<platform>` |
83+
84+
### Tools
85+
86+
Read-only: `get-overview [--devServer false]`, `list-scripts [--status failed] [--search x]`,
87+
`get-script --uniqueId`, `list-script-events [--uniqueId] [--since seq]`,
88+
`get-federation`, `list-federation-events [--type] [--errorsOnly true]`,
89+
`list-errors [--source global-handler|script-manager|federation]`, `get-error --id`,
90+
`symbolicate [--stack "<raw stack>"] [--errorId]`,
91+
`get-dev-server [--origin http://localhost:8082] [--requests failed|all|none]`,
92+
`list-script-calls [--uniqueId] [--method loadScript] [--errorsOnly true]`,
93+
`get-script-manager`, `list-faults [--origin]`.
94+
List tools paginate with `--limit` and `--cursor` (take the cursor from `page`).
95+
96+
Actions, to reproduce issues without driving the UI:
97+
`load-script --scriptId <id> [--caller <id>]`, `prefetch-script`,
98+
`invalidate-scripts [--scriptIds a,b]`, `load-remote --id Remote/Module [--instance]`,
99+
`clear [--scope scripts|federation|errors|all]`, `reload`.
100+
101+
Fault injection, to test error handling and recovery without editing app code:
102+
103+
- `inject-script-fault --stage resolve|load [--scriptId x] [--caller y]
104+
[--action fail|delay] [--delayMs n] [--times n] [--code c]`: through
105+
ScriptManager hooks. `resolve` throws before resolvers run (a resolver that
106+
fetches a manifest while offline); `load` throws before the download with
107+
`NetworkFailure` (loadScript only).
108+
- `inject-server-fault --match <substring|/regex/> [--status 503]
109+
[--delayMs n] [--drop true] [--times n] [--platform] [--origin]`: on any
110+
dev server wrapped with `withRepackDevTools`, including MF remotes. Faulted
111+
responses carry `fault` in the request log; dropped connections are logged
112+
with `status: 0`.
113+
- `clear-faults [--id] [--origin]`. Faults stay active until `--times` is
114+
used up; always clear them when done, and remember that the overview's
115+
`faults` diagnostics mean failures you caused yourself.
116+
117+
### Dev-server endpoints (curl, no app needed)
118+
119+
- `GET /repack-devtools/state?requests=failed|all|none`: per-platform
120+
compilation (`status`, `builds`, errors, warnings, assets, changed files),
121+
request log (failed requests keep the first 4 KB of the response body), and
122+
diagnostics.
123+
- `GET /repack-devtools/compilation?platform=android`: one compilation in full.
124+
- `POST /repack-devtools/clear`: empties the request log.
125+
- `GET|POST|DELETE /repack-devtools/faults[?id=]`: list, add (JSON body like
126+
`inject-server-fault`) or remove server faults.
127+
128+
Every MF dev server has its own endpoint (host and each mini app).
129+
`builds: 0` with `status: compiling` means that platform was never requested.
130+
131+
## 3. Playbooks
132+
133+
**Chunk / script fails to load.** `list-scripts --status failed`, then
134+
`get-script`. `error.stage` says where it broke (`resolve` = no resolver
135+
matched or the resolver threw; `load` = download or evaluation;
136+
`create` = bad locator). Compare `locator.url` with the dev server: a 404
137+
diagnostic lists emitted assets with similar names. If the record shows
138+
`source: cache`, call `invalidate-scripts` and `load-script` again to tell
139+
cache problems from server problems.
140+
141+
**Retry / recovery after a failure.** Inject the failure once
142+
(`inject-script-fault --stage resolve --scriptId X --times 1`, or
143+
`inject-server-fault --match X --status 503 --times 1`), call `load-script`
144+
twice, then `list-script-calls --uniqueId X`. A healthy retry shows
145+
`resolverRan: true` on the second call. `resolverRan: false` with
146+
`joined: rejected load` means ScriptManager returned the stored failure
147+
instead of trying again; `get-script-manager` then lists that promise as
148+
`rejected` (issue #1506). The same check works for prefetch-then-load and for
149+
concurrent callers (`joined: pending ...` is expected deduplication).
150+
151+
**Resolver and cache behaviour.** `get-script-manager` lists resolvers with
152+
priorities, taps per hook and cache entries. `list-script-calls` tells how
153+
often resolvers really ran versus calls served from a stored promise; compare
154+
with the dev server request log to see what was actually downloaded.
155+
156+
**Module Federation.** `get-federation` shows each host instance's remotes
157+
(`loaded`), shared packages per scope with every registered version, who
158+
provided it (`from`), and which one is `loaded`. Diagnostics flag singletons
159+
loaded twice and loaded versions that miss a `requiredVersion`. For load
160+
failures, `list-federation-events --errorsOnly true` gives the hook,
161+
remote entry URL and error. Then check the remote's own dev server:
162+
`get-dev-server --origin http://localhost:8082 --requests failed`.
163+
Use `load-remote --id MiniApp/<exposed>` to retry a single remote load.
164+
If `runtimePluginInstalled` is false, MF events are not recorded: add
165+
`require.resolve('@callstack/repack-devtools-plugin/mf-runtime-plugin')` to
166+
`runtimePlugins`, or call `installRepackDevTools()` in the entry.
167+
168+
**Compilation or HMR broken.** Use curl, since the app may not load.
169+
`compilations[].errors` carry ANSI-stripped messages with module names, and
170+
`changedFiles` lists what triggered the last rebuild.
171+
172+
**Unsymbolicated stacks / `/symbolicate` 400.** Run `symbolicate` (no args
173+
throws and symbolicates a probe error in the app) or
174+
`symbolicate --errorId err-N`. Frames with `unmapped: true` still point at a
175+
bundle URL; `sourceMaps[]` gives the HTTP status of each bundle's `.map`.
176+
Re.Pack's `/symbolicate` only keeps frames whose `file` is an http(s) URL, and
177+
answers 400 "Cannot infer platform" when none carries a platform (query
178+
`platform=` or `name.<platform>.bundle`).
179+
180+
**Uncaught errors.** `list-errors`, `get-error --id`, then
181+
`symbolicate --errorId`.
182+
183+
## Gotchas
184+
185+
- **Agent tool calls time out after about 30 s**: a `load-script` that waits
186+
on a long injected delay times out on the agent side while the app keeps
187+
going. Read the outcome afterwards with `list-script-calls`.
188+
- **`resolverRan` reads as "ScriptManager reached its resolve step during
189+
this call".** It is false when an injected `resolve` fault fired (check the
190+
`faults` column) and for calls that only joined another attempt (`joined`
191+
set).
192+
- **MF manifest errors hide the HTTP status**: MF parses error pages as JSON,
193+
so a 4xx/5xx shows up as `JSON Parse error`. The `afterLoadManifest` event
194+
`detail` has `HTTP <status>`.
195+
196+
- **Startup is only captured** when the entry calls
197+
`installRepackDevTools({ boot: true })` before other code
198+
(`app.bootRecording: true`). The testers do this.
199+
- **After a reload the agent session goes stale**: tool calls time out with
200+
`Tool call timeout` or `ReactNativeApplication.enable timed out`. Run
201+
`rozenite agent session stop` and retry; the new runtime starts with empty
202+
records.
203+
- **Rebuilding the plugin package while a dev server runs** (`pnpm build` wipes
204+
`dist` first) leaves the server's resolver with a stale
205+
`Cannot find module '@callstack/repack-devtools-plugin'`. Restart the dev
206+
server, then relaunch the app.
207+
- **"More than one device is connected"**: every app pointed at the port
208+
(including a previous tester left running on the same emulator) is a
209+
target. Pass `--device <id>` from `rozenite agent targets` or set
210+
`ROZENITE_DEVICE`, or force-stop the other app.
211+
- **`Unknown domain "callstack/repack-devtools"`** means the app has not
212+
called `useRepackDevTools()` yet: it crashed or is stuck before the first
213+
render. Read `rozenite agent console getMessages --limit 40` and take a
214+
screenshot.
215+
- Launch Android apps with
216+
`adb shell am start -n <pkg>/com.microsoft.reacttestapp.MainActivity` for
217+
the testers; `monkey` launches are unreliable.
218+
- Ignore the `/_expo/*` and `/json*` requests made by DevTools; the request log
219+
already filters them.
220+
- `rozenite agent domains` lists only built-in domains on some versions. Call
221+
`rozenite agent callstack/repack-devtools` to list the plugin's tools.
222+
- Rozenite's own docs: `pnpm exec rozenite skills list` / `skills show <name>`.
223+
- From scripts, use `@rozenite/agent-sdk` with the typed descriptors in
224+
`@callstack/repack-devtools-plugin/sdk` (`repackDevToolsTools.getOverview`).
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
name: repack-reproduce-issue
3+
description: >
4+
Use when asked to reproduce, root-cause or fix a Re.Pack bug report (GitHub
5+
issue, crash log, "X doesn't work with Re.Pack") end to end and unattended:
6+
pick the right tester app, reproduce the failure on a device or simulator,
7+
gather evidence with the Re.Pack DevTools agent tools, find the root cause in
8+
packages/, fix it, and add a regression test.
9+
---
10+
11+
# Reproduce and root-cause a Re.Pack issue
12+
13+
Work through these steps in order and keep notes of every command and its output.
14+
The final report must say what was observed, not only what was inferred.
15+
Runtime inspection is covered by the `repack-debug-runtime` skill; load it
16+
alongside this one.
17+
18+
## 1. Pin the report down
19+
20+
Extract: platform(s), RN version, bundler (Rspack/webpack, `USE_WEBPACK`),
21+
Module Federation version (none, v1, v2), dev vs release, Hermes, and the exact
22+
symptom (error text, stack, which screen). If the report has a repro repo,
23+
read its config before touching ours.
24+
25+
## 2. Pick the closest tester
26+
27+
| Area | Tester |
28+
| --- | --- |
29+
| Async/remote chunks, ScriptManager, assets, NativeWind, Reanimated | `apps/tester-app` |
30+
| Module Federation v1 (`Federated`, `ModuleFederationPluginV1`) | `apps/tester-federation` |
31+
| Module Federation v2, manifests, `preloadRemote`, runtime plugins; NativeWind, Reanimated, assets or chunks inside an MF host/remote | `apps/tester-federation-v2` (has the E2E suite) |
32+
| Expo modules / expo-router / `@callstack/repack-expo` | `apps/tester-expo` |
33+
| Expo as a Module Federation remote (`@callstack/repack-expo` widget) | `apps/tester-expo-widget` |
34+
| Pure bundling/resolution, no device needed | `tests/integration`, `tests/resolver-cases`, `tests/metro-compat` |
35+
36+
Prefer a no-device reproduction when the bug is in compilation or resolution:
37+
an integration test or a `pnpm bundle:<platform>` run is faster and deterministic.
38+
39+
## 3. Reproduce
40+
41+
1. `pnpm install && pnpm build` at the root (stale `dist` output causes false
42+
failures).
43+
2. Pick free ports and your own device. Do not reuse a device or port that
44+
another session is using (`adb devices`, `lsof -iTCP -sTCP:LISTEN`).
45+
3. Start the dev server(s) with `WITH_ROZENITE=true` and launch the debug build
46+
(see `repack-debug-runtime` §1). Build native apps once per RN upgrade:
47+
`cd android && ./gradlew assembleDebug`, or `pnpm ios` for the simulator.
48+
4. Make the minimal code/config change in the tester that triggers the bug.
49+
Keep it as a separate commit so it can be dropped or turned into a test.
50+
For failure and recovery bugs (offline, flaky CDN, 5xx, timeouts, a
51+
resolver that throws) try `inject-script-fault` / `inject-server-fault`
52+
first: they need no app change and work on every tester.
53+
5. Trigger it. Prefer agent tools over UI driving:
54+
`load-script`, `prefetch-script`, `invalidate-scripts`, `load-remote`,
55+
`reload`. When the bug needs UI interaction, drive the device with
56+
`agent-device` if it is available, or with `adb shell input`.
57+
6. Confirm the failure: `get-overview` must show the diagnostic that matches
58+
the report, or `list-errors` must show the reported error. Take a
59+
screenshot (`adb exec-out screencap -p`) when the symptom is visual.
60+
61+
If it does not reproduce, record the environment differences (versions,
62+
config, platform) and stop guessing; ask for the missing detail or try the
63+
reporter's repro.
64+
65+
## 4. Find the root cause
66+
67+
- Follow each diagnostic's `ref` to the raw record (`get-script`,
68+
`get-federation`, `list-federation-events`, `get-dev-server`).
69+
- Compare app-side and server-side evidence. A script 404 plus a
70+
"similar asset" hint means a URL/filename mismatch; a 200 from the dev server
71+
plus a ScriptManager `load` error means evaluation or native-side failure.
72+
- Map the failure to source in `packages/repack/src` (ScriptManager, plugins,
73+
loaders) or `packages/dev-server/src` (routes, symbolication). Read
74+
`agent_context/` for the area first.
75+
- State the root cause as a falsifiable claim, then verify it: revert or patch
76+
the suspected line and show the symptom disappears and reappears.
77+
78+
## 5. Fix and lock it in
79+
80+
1. Fix in `packages/`, not in the tester (shipped code must not depend on
81+
monorepo layout).
82+
2. Add a regression test. It must fail without the fix. Pick the lowest
83+
layer that shows the bug:
84+
- unit test next to the code: `jest` in `packages/repack`, `vitest` in
85+
`packages/dev-server` and `packages/repack-devtools-plugin`;
86+
- compilation/resolution: `tests/integration` or `tests/resolver-cases`
87+
(`vitest`), Metro parity in `tests/metro-compat` (`jest`);
88+
- runtime behaviour on a device: an E2E spec in
89+
`apps/tester-federation-v2/e2e/*.e2e.ts` (`@e2e-dev/mobile`, run with
90+
`pnpm e2e`; see `agent_context/e2e-oct2026/design.md`).
91+
3. Re-run the reproduction: the diagnostic is gone and the feature works on
92+
device. Run the package tests plus `pnpm typecheck` and `pnpm lint`.
93+
4. Add a changeset for published packages (`pnpm changeset`).
94+
95+
## 6. Report
96+
97+
Include: environment (device, ports, versions), the repro steps as commands,
98+
the evidence (tool outputs trimmed to the relevant fields), root cause with
99+
file and line, the fix, and the test. Clean up: stop dev servers you started,
100+
`rozenite agent session stop`, and shut down emulators you created.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.agents/skills/repack-debug-runtime
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.agents/skills/repack-reproduce-issue

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,6 @@ packages/**/docs
389389

390390
# watchman
391391
.watchman-cookie*
392+
393+
# Rozenite plugin build intermediates
394+
.rozenite/

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ All development is done directly on GitHub, and all work is public.
2323

2424
To get extra [Rozenite](https://www.rozenite.dev) panels (network activity, plus navigation and storage in the Module Federation hosts) in React Native DevTools while testing, start a tester app's dev server with `WITH_ROZENITE=true`, e.g. `WITH_ROZENITE=true pnpm start`. In `tester-federation-v2`, the Storage panel shows ScriptManager's script cache only when the host also runs with `WITH_PRELOAD=true` (e.g. `WITH_ROZENITE=true WITH_PRELOAD=true pnpm start:hostapp`), since the cache is enabled only in that mode.
2525

26+
This also enables the workspace-only Re.Pack DevTools plugin (`packages/repack-devtools-plugin`): panels for ScriptManager, Module Federation, the dev server and symbolication, the same data as `rozenite agent callstack/repack-devtools` tools, and `GET /repack-devtools/state` on every dev server. The `repack-debug-runtime` and `repack-reproduce-issue` skills in `.agents/skills` describe how to use them.
27+
2628
Without the flag, nothing Rozenite-related is loaded by the dev server or bundled into the app, so default runs and E2E tests are unaffected. Keep it that way when adding a plugin: import it only from the tester's `rozenitePlugins.ts`, which is required only when `__WITH_ROZENITE__` is true.
2729

2830
### Commit message convention

‎agent_context/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ works, and is kept in sync with the implementation as it evolves.
1010
| --- | --- |
1111
| [rspackv2-jul2026](./rspackv2-jul2026/design.md) | Dual Rspack 1.x/2.x support |
1212
| [e2e-oct2026](./e2e-oct2026/design.md) | e2e tests on an emulator, on CI |
13+
| [repack-devtools-oct2026](./repack-devtools-oct2026/design.md) | Re.Pack DevTools plugin: runtime/dev-server diagnostics for panels and agents |

0 commit comments

Comments
 (0)