lustro-cli 0.1.0__tar.gz

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.
Files changed (81) hide show
  1. lustro_cli-0.1.0/.gitignore +33 -0
  2. lustro_cli-0.1.0/PKG-INFO +585 -0
  3. lustro_cli-0.1.0/README.md +561 -0
  4. lustro_cli-0.1.0/pyproject.toml +66 -0
  5. lustro_cli-0.1.0/src/lustro_cli/__init__.py +29 -0
  6. lustro_cli-0.1.0/src/lustro_cli/cli.py +2460 -0
  7. lustro_cli-0.1.0/src/lustro_cli/client.py +434 -0
  8. lustro_cli-0.1.0/src/lustro_cli/discovery.py +1033 -0
  9. lustro_cli-0.1.0/src/lustro_cli/exitcodes.py +25 -0
  10. lustro_cli-0.1.0/src/lustro_cli/mcp.py +867 -0
  11. lustro_cli-0.1.0/src/lustro_cli/wire/__init__.py +62 -0
  12. lustro_cli-0.1.0/src/lustro_cli/wire/_schema.json +97 -0
  13. lustro_cli-0.1.0/src/lustro_cli/wire/cursor-envelope.schema.json +28 -0
  14. lustro_cli-0.1.0/src/lustro_cli/wire/error-envelope.schema.json +46 -0
  15. lustro_cli-0.1.0/src/lustro_cli/wire/golden/cursor-delta.json +91 -0
  16. lustro_cli-0.1.0/src/lustro_cli/wire/golden/cursor-reset.json +65 -0
  17. lustro_cli-0.1.0/src/lustro_cli/wire/golden/cursor-unchanged.json +15 -0
  18. lustro_cli-0.1.0/src/lustro_cli/wire/golden/error-envelope.json +7 -0
  19. lustro_cli-0.1.0/src/lustro_cli/wire/golden/export-har-websocket.json +148 -0
  20. lustro_cli-0.1.0/src/lustro_cli/wire/golden/export-har.json +359 -0
  21. lustro_cli-0.1.0/src/lustro_cli/wire/golden/logs-delta.json +44 -0
  22. lustro_cli-0.1.0/src/lustro_cli/wire/golden/logs-reset.json +73 -0
  23. lustro_cli-0.1.0/src/lustro_cli/wire/golden/logs-unchanged.json +9 -0
  24. lustro_cli-0.1.0/src/lustro_cli/wire/golden/meta.json +12 -0
  25. lustro_cli-0.1.0/src/lustro_cli/wire/golden/prefs-delta.json +58 -0
  26. lustro_cli-0.1.0/src/lustro_cli/wire/golden/prefs-files.json +64 -0
  27. lustro_cli-0.1.0/src/lustro_cli/wire/golden/prefs-reset.json +58 -0
  28. lustro_cli-0.1.0/src/lustro_cli/wire/golden/prefs-unchanged.json +4 -0
  29. lustro_cli-0.1.0/src/lustro_cli/wire/golden/rules-list.json +44 -0
  30. lustro_cli-0.1.0/src/lustro_cli/wire/golden/screen-tree-views.json +596 -0
  31. lustro_cli-0.1.0/src/lustro_cli/wire/golden/screen-tree.json +689 -0
  32. lustro_cli-0.1.0/src/lustro_cli/wire/golden/send-result.json +7 -0
  33. lustro_cli-0.1.0/src/lustro_cli/wire/golden/senders-list.json +6 -0
  34. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-databases.json +37 -0
  35. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-exec.json +4 -0
  36. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-query.json +109 -0
  37. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-rows.json +109 -0
  38. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-schema.json +98 -0
  39. lustro_cli-0.1.0/src/lustro_cli/wire/golden/sqlite-tables.json +57 -0
  40. lustro_cli-0.1.0/src/lustro_cli/wire/golden/stream-delta.json +48 -0
  41. lustro_cli-0.1.0/src/lustro_cli/wire/golden/stream-reset.json +66 -0
  42. lustro_cli-0.1.0/src/lustro_cli/wire/golden/stream-unchanged.json +4 -0
  43. lustro_cli-0.1.0/src/lustro_cli/wire/golden/transaction-image.json +43 -0
  44. lustro_cli-0.1.0/src/lustro_cli/wire/golden/transaction.json +41 -0
  45. lustro_cli-0.1.0/src/lustro_cli/wire/golden/websocket.json +37 -0
  46. lustro_cli-0.1.0/src/lustro_cli/wire/golden/websockets-reset.json +89 -0
  47. lustro_cli-0.1.0/src/lustro_cli/wire/golden/work-events.json +152 -0
  48. lustro_cli-0.1.0/src/lustro_cli/wire/golden/work-item.json +118 -0
  49. lustro_cli-0.1.0/src/lustro_cli/wire/golden/work-items.json +129 -0
  50. lustro_cli-0.1.0/src/lustro_cli/wire/golden/work-metrics.json +40 -0
  51. lustro_cli-0.1.0/src/lustro_cli/wire/logs.openapi.json +622 -0
  52. lustro_cli-0.1.0/src/lustro_cli/wire/meta.schema.json +47 -0
  53. lustro_cli-0.1.0/src/lustro_cli/wire/network.openapi.json +2458 -0
  54. lustro_cli-0.1.0/src/lustro_cli/wire/pagination.schema.json +20 -0
  55. lustro_cli-0.1.0/src/lustro_cli/wire/prefs.openapi.json +699 -0
  56. lustro_cli-0.1.0/src/lustro_cli/wire/screen.openapi.json +512 -0
  57. lustro_cli-0.1.0/src/lustro_cli/wire/sqlite.openapi.json +1233 -0
  58. lustro_cli-0.1.0/src/lustro_cli/wire/stream-envelope.schema.json +33 -0
  59. lustro_cli-0.1.0/src/lustro_cli/wire/sync.py +80 -0
  60. lustro_cli-0.1.0/src/lustro_cli/wire/work.openapi.json +1188 -0
  61. lustro_cli-0.1.0/tests/adb_parts.py +47 -0
  62. lustro_cli-0.1.0/tests/conftest.py +80 -0
  63. lustro_cli-0.1.0/tests/fake_apk.py +49 -0
  64. lustro_cli-0.1.0/tests/test_cli_api.py +287 -0
  65. lustro_cli-0.1.0/tests/test_cli_dispatch.py +465 -0
  66. lustro_cli-0.1.0/tests/test_cli_export.py +175 -0
  67. lustro_cli-0.1.0/tests/test_cli_output.py +781 -0
  68. lustro_cli-0.1.0/tests/test_cli_poll_once.py +140 -0
  69. lustro_cli-0.1.0/tests/test_cli_screen.py +120 -0
  70. lustro_cli-0.1.0/tests/test_cli_websockets.py +205 -0
  71. lustro_cli-0.1.0/tests/test_client_smoke.py +271 -0
  72. lustro_cli-0.1.0/tests/test_connection_errors.py +268 -0
  73. lustro_cli-0.1.0/tests/test_cursor_state.py +117 -0
  74. lustro_cli-0.1.0/tests/test_discovery.py +1077 -0
  75. lustro_cli-0.1.0/tests/test_envelope_parity.py +88 -0
  76. lustro_cli-0.1.0/tests/test_error_envelope.py +117 -0
  77. lustro_cli-0.1.0/tests/test_golden_schemas.py +754 -0
  78. lustro_cli-0.1.0/tests/test_local_port.py +362 -0
  79. lustro_cli-0.1.0/tests/test_mcp.py +740 -0
  80. lustro_cli-0.1.0/tests/test_protocol_check.py +327 -0
  81. lustro_cli-0.1.0/tests/test_ready.py +1130 -0
@@ -0,0 +1,33 @@
1
+ # Gradle
2
+ .gradle/
3
+ build/
4
+ !**/src/**/build/
5
+
6
+ # Android / IDE
7
+ local.properties
8
+ *.iml
9
+ .idea/
10
+ .kotlin/
11
+ captures/
12
+ .cxx/
13
+
14
+ # OS
15
+ .DS_Store
16
+
17
+ # Python (lustro-cli)
18
+ __pycache__/
19
+ *.py[cod]
20
+ .venv/
21
+ venv/
22
+ *.egg-info/
23
+ dist/
24
+ .pytest_cache/
25
+
26
+ # Secrets / signing
27
+ *.keystore
28
+ *.jks
29
+ secrets.properties
30
+
31
+ # Docs site (zensical build)
32
+ site/
33
+ .cache/
@@ -0,0 +1,585 @@
1
+ Metadata-Version: 2.5
2
+ Name: lustro-cli
3
+ Version: 0.1.0
4
+ Summary: Command-line client for the Lustro Android debug server (HTTP wire protocol).
5
+ Project-URL: Homepage, https://github.com/Twinsen81/Lustro
6
+ Project-URL: Documentation, https://twinsen81.github.io/Lustro/
7
+ Project-URL: Repository, https://github.com/Twinsen81/Lustro
8
+ Project-URL: Changelog, https://github.com/Twinsen81/Lustro/blob/main/CHANGELOG.md
9
+ Author: Evgenii Plokhov
10
+ License: Apache-2.0
11
+ Keywords: android,cli,debug,lustro,network
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Topic :: Software Development :: Debuggers
19
+ Requires-Python: >=3.9
20
+ Provides-Extra: test
21
+ Requires-Dist: jsonschema>=4; extra == 'test'
22
+ Requires-Dist: pytest>=7; extra == 'test'
23
+ Description-Content-Type: text/markdown
24
+
25
+ # lustro CLI
26
+
27
+ A command-line client for the [Lustro](https://github.com/Twinsen81/Lustro)
28
+ Android debug server. It talks **only** to the HTTP wire protocol under
29
+ `/api/v1/` — the public, SemVer-governed contract — so it installs with **zero
30
+ third-party runtime dependencies** (Python standard library only) and works
31
+ without a source checkout.
32
+
33
+ Target: Python 3.9+.
34
+
35
+ ## Install
36
+
37
+ Each Lustro release publishes the CLI to PyPI as `lustro-cli`, with the version
38
+ of the library. Install it with [uv](https://docs.astral.sh/uv/) or
39
+ [pipx](https://pipx.pypa.io/), which keep it apart from other Python packages.
40
+ uv also installs a Python when the computer has none.
41
+
42
+ ```bash
43
+ uv tool install lustro-cli # or: pipx install lustro-cli, or: pip install lustro-cli
44
+ lustro open
45
+ uvx lustro-cli net list # runs the CLI without an install
46
+ ```
47
+
48
+ The package installs one command under two names, `lustro` and `lustro-cli`, so
49
+ that `uvx lustro-cli` finds it by the package's name.
50
+
51
+ > **`lustro` on PyPI is a different project.** It also installs a `lustro`
52
+ > command, so `pip install lustro` and `uvx lustro` install that project, not
53
+ > this CLI.
54
+
55
+ To get the CLI of `main` before a release, install it from GitHub, or from a
56
+ checkout:
57
+
58
+ ```bash
59
+ uv tool install "git+https://github.com/Twinsen81/Lustro#subdirectory=lustro-cli" # pipx takes the same URL
60
+ uv tool install ./lustro-cli # from a checkout; or: pipx install ./lustro-cli
61
+ pip install -e 'lustro-cli[test]' # to work on the CLI: adds pytest and jsonschema for the contract tests
62
+ ```
63
+
64
+ The wheel bundles the wire-protocol schemas, the Network OpenAPI document, and
65
+ the golden fixtures (`lustro_cli/wire/`).
66
+
67
+ `lustro --version` prints the version of the CLI, the version of the wire
68
+ protocol that it implements, and the revision of the CLI, such as `lustro 0.1.0
69
+ (protocol 1.17, revision 1)`. Each protocol version adds routes or fields, so two
70
+ builds of the CLI with the same version can differ in what they do: compare the
71
+ protocol. When an app's protocol, `protocolVersion` in `GET /api/v1/_meta`, is
72
+ newer, `lustro ready` and `lustro meta` print a warning on stderr: update the CLI.
73
+
74
+ The revision is a whole number that each change to the CLI on `main` increases
75
+ by one. The version changes only at a release, and the protocol only with the
76
+ wire protocol, so a fix to the CLI alone changes only the revision. A script that
77
+ needs a fix requires the revision of that fix or a later one, such as "revision 1
78
+ or later". The [CHANGELOG](https://github.com/Twinsen81/Lustro/blob/main/CHANGELOG.md) names the revision of each change to
79
+ the CLI:
80
+
81
+ ```bash
82
+ revision=$(lustro --version | sed -n 's/.*revision \([0-9][0-9]*\).*/\1/p')
83
+ if [ "${revision:-0}" -lt 1 ]; then
84
+ echo "lustro is older than revision 1: install it again" >&2
85
+ exit 1
86
+ fi
87
+ ```
88
+
89
+ A CLI from before the revision prints no revision: count it as 0. `lustro
90
+ --version --json`, with the options in either order, prints the three values as
91
+ a JSON object, such as `{"version":"0.1.0","protocol":"1.17","revision":1}`; a
92
+ CLI from before the revision prints the line of text there too. `lustro mcp`
93
+ reports the revision in its server info, next to the version.
94
+
95
+ ### An app on an older protocol
96
+
97
+ An app can stay on an older Lustro release, and so on an older protocol, and most
98
+ commands work with it. But an older app ignores a request field or a query
99
+ parameter that its protocol does not have, and answers `200`, so a command with
100
+ such an option would succeed with a wrong result: a mock rule that answers instead
101
+ of failing, or a text search in place of a regex. So when a command uses one of
102
+ these options, the CLI first reads `protocolVersion` from `_meta`, and refuses the
103
+ command when the app is older, before it sends anything, with exit status 1:
104
+
105
+ | Option | Command | Protocol |
106
+ | --- | --- | --- |
107
+ | `--sender` | `send` | 1.7 |
108
+ | `--regex` | `net list`, `net poll`, `net wait`, `net ws events` | 1.8 |
109
+ | `--failure` | `mock add` | 1.12 |
110
+ | `failure` in a rule of the file | `mock sync` | 1.12 |
111
+
112
+ ```
113
+ error: protocol_too_old: --failure needs wire protocol 1.12, and the app uses 1.11, which ignores it
114
+ hint: update Lustro in the app, or leave out --failure
115
+ ```
116
+
117
+ The MCP tools refuse `regex` of `list_requests` and `failure` of `add_mock_rule`
118
+ in the same way. A command without such an option sends no extra request. A
119
+ command for a route that an older app does not have, such as `net ws list` before
120
+ 1.3, gets a `404` from the app. `lustro api` does not know the route, so it
121
+ refuses nothing; see [Any route](#any-route).
122
+
123
+ The MCP tool `read_logs` refuses `from_ms` and `to_ms` in the same way when the
124
+ app's Logs tab is older than 1.1, the version in `tabs[].version` of `_meta`,
125
+ because a Logs tab 1.0 returns the whole log. An app that gives the tab its
126
+ `protocolVersion` there is from before the tabs had versions of their own, and
127
+ its Logs tab is 1.0. `mock sync` rejects two rules with
128
+ the same `id` itself, with exit status 2, because an app before protocol 1.11
129
+ keeps the first of them and loses the other. Some values that a newer app
130
+ rejects, an older app accepts without an error: before 1.16, `mock toggle` and
131
+ `mock delete` answer `200` for an id that no rule has, and change nothing, and
132
+ `net throttle` takes a delay over 600000 ms and sets 0 for a negative one. Check
133
+ the result with `mock list` or `net state`.
134
+
135
+ ## Endpoint and token discovery
136
+
137
+ The CLI resolves the endpoint+token in this order (first hit wins, per field):
138
+
139
+ 1. Explicit `--token` / `--host` / `--port`.
140
+ 2. The `LUSTRO_TOKEN` environment variable (token only).
141
+ 3. With `--package <id>`, the app's prefs: `adb [-s <serial>] shell run-as <id> cat
142
+ shared_prefs/lustro_debug.xml` on debuggable builds gives the token, and the host and port
143
+ of the app's last bind. They are the named app's own file. Each process of the app writes its
144
+ token when it makes it, and each bind writes the host and port, so while the app is in the
145
+ foreground and its server listens, they are the values of its current process. Before the
146
+ bind, or while the app is in the background, they are those of its last bind. This read takes
147
+ about 0.2 s. The dump of the log in step 4 takes longer as the
148
+ device keeps more log, and on a physical device with full log buffers it can take 10 s. When
149
+ the prefs give the token, the host, and the port, the CLI runs no `logcat`, also with
150
+ `--token` or `LUSTRO_TOKEN`. When `run-as` fails, as for a build that is not debuggable,
151
+ or the prefs do not have all three, as before the app writes them, the CLI goes to step 4
152
+ for the rest. When the app does not run, its prefs keep the values of its last process: the
153
+ connection then fails, and the CLI prints the cause.
154
+ 4. The `LustroToken` logcat ready line: `adb [-s <serial>] logcat -d -v threadtime -s
155
+ LustroToken`, which shows the process ID of each line, is parsed for the most recent
156
+ `Lustro ready endpoint=http://<host>:<port> token=<token>`. Without
157
+ `--package`, this is the first source for host/port/token, **including a fallback port**,
158
+ and the last line counts, whatever app logged it. With
159
+ `--package <id>`, only the lines of that app's current process count: the CLI
160
+ gets its process IDs with `adb shell pidof <id>`, with the uid of each, and takes
161
+ the last line of these processes. The line of another app, such as another
162
+ build of the same app, and the line of an earlier process of the app are not
163
+ used. That line can name a port that the app no longer uses, and a token that
164
+ is no longer valid: a server that listens beyond loopback gets a new token in
165
+ each process. If this gives no token either, the CLI prints the cause.
166
+ 5. (best effort, without `--package`) the `run-as` read of step 3, of the app in the
167
+ foreground, because Lustro listens only while its app is in the foreground. This finds the
168
+ app on a device that drops Info logs, such as one with `adb shell getprop log.tag` set to
169
+ `E`, where the ready line never reaches logcat. It also replaces a ready line whose app does
170
+ not listen on the line's port, or whose process has ended, when the app in the foreground
171
+ listens on the port of its prefs.
172
+
173
+ **The token goes only to its app's server.** Any app on the device can log a line with the tag
174
+ `LustroToken`, and bind the app's port while the app is in the background. So the CLI sends a
175
+ token that it found in steps 3 to 5 only to a server whose listening socket has the uid of the
176
+ app that owns the token: for the prefs, the uid that `run-as` runs with (the `cat` that reads them
177
+ also reads its own `/proc/self/status`), and for a log line, the uid of the process that logged it
178
+ (`stat -c %u /proc/<pid>`). The process name is not
179
+ enough: `pidof` finds processes by name, and an app can give its process another app's name with
180
+ `android:process`. The CLI reads the uids that listen on the port of the device in
181
+ `/proc/net/tcp` and `/proc/net/tcp6`. With `--package`, the adb call of step 3 reads them, so the
182
+ check costs no extra adb call; without it, one adb call reads the uid of the line's process and
183
+ the sockets. A command checks before its first request, and again before a later request when
184
+ one second has passed or a connection failed, so that `net poll` and `net wait` check while the
185
+ app comes and goes. When another uid listens on the port, the CLI sends no request: it names that
186
+ app, from `pm list packages -U`, or before API 26 from `dumpsys package packages`, and exits 6.
187
+ When nothing listens, it sends nothing either, and prints the cause from adb, as for a connection
188
+ that fails. A token from the prefs goes to a host that is not loopback only when the prefs name
189
+ that host and port: a line of another app can name any host. A token from `--token` or
190
+ `LUSTRO_TOKEN`, and a port from `--port`, which is a local port, are your choice and get no
191
+ check. A host from `--host` that is not loopback gets none either.
192
+
193
+ If none of these determine the token, the CLI looks for the cause with adb, and
194
+ prints it and exits with its code (see [Exit codes](#exit-codes)). It checks, in
195
+ this order: that adb finds one device that it can use (`adb get-state`), that the
196
+ app of `--package` is installed (`adb shell pm path <pkg>`), that its build
197
+ contains the Lustro runtime, that it runs (`pidof`), and that it is in the
198
+ foreground. For the runtime, it reads the entry names in the central directory of
199
+ the installed `base.apk` and looks for an entry under `assets/lustro/`. It reads
200
+ the end of the file with `tail -c` through `adb exec-out`: 64 KB, and a second
201
+ read when the central directory is larger. It does not use `unzip`, which API 28
202
+ emulator images do not have, or `grep`, which does not find the names on API 30
203
+ to 33. Every build with the Lustro runtime has such entries. A build without them
204
+ is a build with the `-noop` artifacts, which has no server, or a build from
205
+ before the app added Lustro: install a debug build that uses the Lustro
206
+ artifacts. Without `--package`, the CLI checks only the runtime of the app in the
207
+ foreground, which can be another app, such as the launcher when your app is not
208
+ running. Pass `--package` in a script, so that the CLI checks your app. A
209
+ connection that fails gets the same checks, because a ready line or the prefs from an
210
+ earlier start of the app give a token and a port that no longer answer. A command with
211
+ `--port` gets them only with `--device` or `--package`, which say that the app is
212
+ on a device, for example behind a forward that the script made itself. Without
213
+ them, the server of `--port` can be on this computer, and adb finds nothing about it.
214
+
215
+ A token from the ready line can be stale: when the app was installed again and the device
216
+ dropped the new line, the last line in the log is from the earlier install. A token from the
217
+ prefs can be stale too, when a new process of an app that listens beyond loopback answers
218
+ before it writes its new token. When the server answers `401` to a token that discovery found,
219
+ the CLI reads the prefs of the app (of `--package`, or in the foreground) and sends the request
220
+ once more with the token there, when that app's uid listens on the port (see above). When that
221
+ gets `401` too, or the CLI does not send it, the CLI looks for the cause with adb, as for a
222
+ connection that fails, and prints it with its exit code. In every other case, the CLI prints the
223
+ `401` with exit 1. Right after an app starts, the server of another app with Lustro on the same
224
+ port can still listen there: the CLI does not send it the token, names that app, and exits 6.
225
+ That server closes when its app goes to the background, and the app's own server then binds the
226
+ port; `lustro ready --package` waits for it. Give each app with Lustro on one device its own
227
+ `serverPort` to prevent this. Without `--package`, `lustro open` compares the token of the line
228
+ with the one in the prefs of the app in the foreground before it prints the URL, because the
229
+ browser gets no second try.
230
+
231
+ The port that discovery finds is the app's port on the device. The CLI reaches
232
+ it through an `adb forward`, and the local port of the forward can be a
233
+ different number. So when the host is a loopback address and `--port` is not
234
+ given, the CLI reads `adb forward --list` and connects to the local port of the
235
+ first forward to the app's port on the selected device: `--device`, else
236
+ `ANDROID_SERIAL`, else the only device. Without such a forward, it makes one
237
+ on a local port that adb chooses (see [The local port](#the-local-port)). When
238
+ adb can't make it, the command prints the device's cause and exits with its
239
+ code: 2 for more than one device with none selected, 3 for no device. Without
240
+ such a cause, it prints `connection_failed` and exits 6. It does not connect to
241
+ the same
242
+ port number on the computer instead: with several devices, that port can be a
243
+ forward to the app on another device. Without adb installed, there is no device
244
+ to forward to, and a command connects to that port on the computer. A command
245
+ with `--port` connects to that port and looks for no forward, for example when
246
+ the server runs on the computer. For `lustro open`, `--port` is the app's port,
247
+ which it forwards to.
248
+
249
+ ## Global flags
250
+
251
+ `--host` (default `127.0.0.1`), `--port` (default `8080`), `--token` (else
252
+ `$LUSTRO_TOKEN`), `--device <serial>` (adb `-s`), `--package <id>` (run-as
253
+ discovery), `--json` (JSON output). They go before or after the command:
254
+ `lustro --json net list` and `lustro net list --json` are the same.
255
+
256
+ ## Commands
257
+
258
+ | Command | Route |
259
+ | --- | --- |
260
+ | `lustro open [--local-port N]` | discovery + `adb forward` + print/open `…/#lustro_token=<token>`; exits 1 when `adb forward` fails |
261
+ | `lustro ready [--timeout SECONDS]` | `GET /api/v1/_meta` until the server answers, then prints the endpoint; at the timeout (30 s by default), prints the cause and exits with its code, and with `--package` exits 4 or 5 at once (see [Wait until the app is ready](#wait-until-the-app-is-ready)) |
262
+ | `lustro meta` | `GET /api/v1/_meta` |
263
+ | `lustro schema [tabId]` | `GET /api/v1/_schema` or `/api/v1/<tab>/_schema` |
264
+ | `lustro net list` | `GET network/transactions` (one poll): the newest 50 transactions, newest first |
265
+ | `lustro net state` | the capture state from `GET network/transactions`: pause, overwrite mode, throttle, capture filter, and the number of enabled mock rules |
266
+ | `lustro net poll` | cursor loop; prints new transactions, and prints one again when it finishes |
267
+ | `lustro net wait [-- COMMAND]` | cursor loop; runs `COMMAND`, prints the first matching request that finishes, and exits |
268
+ | `lustro net get <id>` | `GET network/transactions/<id>`, with each body cut at 2 KB |
269
+ | `lustro net body <id> [request\|response] [-o FILE]` | `GET network/transactions/<id>/body/<direction>`: saves a body as it is stored, the response by default; stdout without `-o` |
270
+ | `lustro net export --har FILE [--ids ID ...]` | `GET network/transactions/_/export`: saves the transactions, or only those in `--ids`, as a HAR file; stdout for `--har -` |
271
+ | `lustro net ws list` | `GET network/websockets`: the WebSocket connections, newest first |
272
+ | `lustro net ws get <id>` | `GET network/websockets/<id>`: one connection, with the headers of its handshake |
273
+ | `lustro net ws events <id> [--follow]` | `GET network/websockets/<id>/events`: the last 50 messages and lifecycle events, oldest first; with `--follow`, each new one as it comes |
274
+ | `lustro net ws payload <id> <seq> [-o FILE]` | `GET network/websockets/<id>/events/<seq>/payload`: saves a message's payload as it is stored; stdout without `-o` |
275
+ | `lustro net clear` | `POST network/clear` |
276
+ | `lustro net pause` | `POST network/pause` |
277
+ | `lustro net overwrite` | `POST network/overwrite-mode` |
278
+ | `lustro net throttle <ms>` | `POST network/throttle` |
279
+ | `lustro mock list` | `GET network/rules` |
280
+ | `lustro mock add --url-pattern … [--failure NAME]` | `POST network/rules` |
281
+ | `lustro mock delete <id>` | `POST network/rules/delete` |
282
+ | `lustro mock toggle <id>` | `POST network/rules/toggle` |
283
+ | `lustro mock sync <file.json>` | `POST network/rules/_/sync` (atomic): a JSON array of rules, or the output of `lustro mock list --json` |
284
+ | `lustro senders` | `GET network/senders`: the clients that `send` can use, the default first |
285
+ | `lustro send --url … [--sender NAME]` | `POST network/send` (synchronous), through the first sender or the one named |
286
+ | `lustro screen tree [--unmerged] [--settle MS] [--all]` | `GET screen/tree`: each window of the app, and its nodes on the screen with a text, a tag, a role, an action, or a state, one line each (see [The screen](#the-screen)) |
287
+ | `lustro api METHOD PATH [--param K=V] [--body JSON \| --input FILE]` | any route: prints the response body (see [Any route](#any-route)) |
288
+ | `lustro mcp` | an MCP server on stdin and stdout, whose tools call the routes above (see [The MCP server](#the-mcp-server)) |
289
+
290
+ A test that changes the mock rules can save them first and restore them after:
291
+
292
+ ```bash
293
+ lustro mock list --json > rules.json # {"items": [...]}
294
+ lustro mock add --url-pattern /v1/orders --status 500
295
+ # ... the test ...
296
+ lustro mock sync rules.json # the rules as they were
297
+ ```
298
+
299
+ ## Wait until the app is ready
300
+
301
+ A script that starts the app waits for its server with `lustro ready`, before
302
+ its first command:
303
+
304
+ ```bash
305
+ adb shell am start -W -n com.example.app/.MainActivity
306
+ lustro ready --package com.example.app --timeout 30 # prints http://127.0.0.1:8080
307
+ ```
308
+
309
+ Each try runs discovery again and sends `GET /api/v1/_meta`, once a second,
310
+ because the app logs its ready line and writes its prefs only when its server starts, and
311
+ those of an earlier start name a token and a port that may not answer. When the server
312
+ answers, `ready` prints the endpoint that the CLI reaches it at and exits 0;
313
+ `--json` prints `{"endpoint", "libraryVersion", "protocolVersion"}`, without the
314
+ token. When the time is over, it prints the cause and exits with its code: no
315
+ device, an app that is not installed, a build without the Lustro runtime, an app
316
+ that is not running or not in the foreground, or a port that does not answer. It
317
+ makes the checks of the cause at the end. With `--package`, it also makes them
318
+ once after the first try that fails (while a device starts, after the first try
319
+ on which adb reaches it and its package manager answers), and stops at once
320
+ with exit 4 for an app
321
+ that is not installed, or exit 5 for a build without the Lustro runtime: the
322
+ installed APK does not change while `ready` waits. For every other cause, such
323
+ as an app that is not running yet, it waits. So an `adb install` that runs at
324
+ the same time as `ready --package` can end it at once with exit 4: install the
325
+ app before `ready`. Without `--package`, the checks run only at the end,
326
+ because the app in the foreground changes while the app starts, and it can be
327
+ the launcher. An error response that a new try
328
+ can't change, such as a `401` for a `--token` that is not the app's, ends the
329
+ wait at once with exit 1; `ready` tries again after a `5xx` or a `429`, and after
330
+ a `401` for a token that discovery found, which the next try reads again. While
331
+ another app's uid listens on the app's port, the CLI sends it no token, and `ready`
332
+ waits; when that lasts until the timeout, `ready` names that app and exits 6.
333
+
334
+ A `404` for `GET /api/v1/_meta` is not from Lustro, which always has that route.
335
+ It comes from another server on the port, such as the debug server of another
336
+ app, on the port that the app's prefs keep from its last start while the app is
337
+ stopped. `ready` waits, as for a port that does not answer. At the timeout, it
338
+ prints the cause that adb finds and says that another server answers on the
339
+ port, and exits with the code of the cause, or 6.
340
+
341
+ A script that needs the token, for the console URL or a `curl` call, gets it from
342
+ `lustro open --print-only --json`, which prints `url`, `token`, `host`, and
343
+ `port` and opens no browser. Like `lustro open`, it forwards the port; add
344
+ `--no-forward` when the script forwards it itself:
345
+
346
+ ```bash
347
+ lustro ready --package com.example.app
348
+ TOKEN=$(lustro --package com.example.app open --print-only --json | jq -r .token)
349
+ ```
350
+
351
+ ## Exit codes
352
+
353
+ Each cause has its own exit code, so that a script can act on it without reading
354
+ the message:
355
+
356
+ | Code | Cause | What to do |
357
+ | --- | --- | --- |
358
+ | 0 | The command did its work. | |
359
+ | 1 | The app answered with an error, such as `404 not_found`, or the command did not get what it waited for: `net wait` at its timeout, and `open` when `adb forward` fails. | Read the error on stderr. |
360
+ | 2 | The command can't run as given: a bad flag or input, an id that more than one item starts with, a search that the app rejects, or more than one device with none selected. | Change the command, or pass `--device`. |
361
+ | 3 | No device: adb is not installed, or it finds no device, or the device is offline or not authorized. | Connect a device, or start an emulator. |
362
+ | 4 | The app of `--package` is not installed. | Install a debug build that uses the Lustro artifacts. |
363
+ | 5 | The installed build of `--package` does not contain the Lustro runtime: a build with the `-noop` artifacts, or one from before the app added Lustro. | Install a debug build that uses the Lustro artifacts. This replaces the installed build. |
364
+ | 6 | The app can't be reached: it is not running or not in the foreground, it logged no ready line, its port does not answer, or another app holds its port, so the CLI did not send the app's token there. | Start the app, and keep it in the foreground. For another app on the port, run `lustro ready --package`, which waits for the app's own server, and give each app its own `serverPort`. |
365
+
366
+ Without `--package`, an app in the foreground without the runtime exits 6, not 5:
367
+ that app can be the launcher, or another app than yours. Every command that
368
+ finds the app exits with codes 3 to 6, and the error text of an MCP tool names
369
+ the same cause.
370
+
371
+ > **Live use of `lustro open`** requires a running app: the server only listens
372
+ > while the app is foregrounded, and discovery reads the `LustroToken` log line
373
+ > via `adb`. With no device, discovery falls back to flags / `LUSTRO_TOKEN`.
374
+
375
+ ## The local port
376
+
377
+ `lustro open` forwards a local port to the app's port on the device, and prints
378
+ the console URL with the local port. It uses the same port number, 8080 by
379
+ default: a forward on it, or else a new one. So a person gets the same URL each
380
+ time, also after an earlier command, such as `lustro ready`, made a forward on
381
+ a port that adb chose. That matters because the console keeps its state in the
382
+ browser for each origin, and the port is part of the origin. When another
383
+ process, such as a local development server or a forward to another device,
384
+ holds that port on the computer, `open` uses an existing forward to the app, or
385
+ else a free local port that adb chooses. To choose the local port yourself:
386
+
387
+ ```bash
388
+ lustro open --local-port 18080 # forward local port 18080 to the app's port
389
+ lustro open --local-port 0 # adb chooses a free local port
390
+ lustro net list # connects through the forward, without --port
391
+ ```
392
+
393
+ `open --local-port N` exits 1 when port N is taken. The other commands find
394
+ the forward in `adb forward --list`, as
395
+ [discovery](#endpoint-and-token-discovery) describes, so they need no `--port`.
396
+ This also applies to a forward that you make yourself, such as
397
+ `adb forward tcp:18080 tcp:8080`. Without a forward, a command makes one on a
398
+ local port that adb chooses, so it needs no `open` first. It does not take the
399
+ app's port number: the forward stays until adb restarts, and on local port 8080
400
+ it would keep a tool that starts later, such as a proxy or a development server,
401
+ from binding 8080. Only `open` prefers the same port number. The CLI runs
402
+ `adb forward --no-rebind`, so it does not take over a local port that a forward
403
+ of another device holds. `--no-forward` skips the forward, and the URL then
404
+ uses `--local-port N`, else the app's port.
405
+
406
+ The commands connect to the forward directly, whatever `http_proxy`,
407
+ `HTTPS_PROXY`, or the system's proxy settings say: the server is an adb forward
408
+ on this computer, which a proxy cannot reach, and the token must not leave it.
409
+
410
+ ### Several devices
411
+
412
+ The app on each device or emulator listens on port 8080 of that device, but
413
+ the computer has only one local port 8080. Select the device with
414
+ `--device SERIAL` or `ANDROID_SERIAL` for each command. The CLI then connects
415
+ through a forward to that device, on whatever local port the forward has, and
416
+ not through a forward to another device. Several agents or scripts can each
417
+ drive the app on their own device at the same time.
418
+
419
+ ## Output
420
+
421
+ The output is small by default, because an agent reads all of it.
422
+
423
+ - **Rows.** Each `net list`, `net poll`, and `net wait` row starts with a short
424
+ transaction id, then the start time, the method, the status (`...` in flight,
425
+ `ERR` for a failed request), the duration, and the URL. The short id is the
426
+ first 8 characters of the id, or more characters when two listed ids start
427
+ with the same 8. JSON output has the full id.
428
+ - **Ids.** `net get`, `net body`, and `net export --ids` take a full id, or the
429
+ start of one, such as the short id of a row. They find the id that starts with it in the transaction
430
+ list, which is one more request. When more than one id starts with it, they
431
+ print the matches and exit 2. When no id starts with it, they exit 1. The wire
432
+ routes take only the full id.
433
+ - **The newest 50.** `net list` prints the newest 50 transactions, and says on
434
+ stderr how many it left out. `--last N` prints the newest N, and `--all`
435
+ prints every one.
436
+ - **Filters.** `net list`, `net poll`, and `net wait` take `--url TEXT` (the URL
437
+ contains it, ignoring case), `--method`, `--status` (a code such as `404`, or a
438
+ class such as `5xx`), and `--errors` (status 400 or higher, or a failed
439
+ request). They combine with each other and with `--search TEXT`, which the
440
+ app runs over the URL, the method, the headers, and the bodies, ignoring
441
+ case. `--regex` makes each word of the text a Java regular expression: all
442
+ words must match, each in any field, and `-word` leaves out. A regex that
443
+ doesn't compile exits 2 with the reason. `--url` is always plain text.
444
+ - **JSON Lines.** With `--json`, these commands print one compact JSON object
445
+ per transaction, so `head` and `jq` can cut the output. `--fields
446
+ id,statusCode,url` keeps only those keys, and implies `--json`. Other
447
+ commands print one JSON document: compact when stdout is not a terminal, and
448
+ indented when it is.
449
+ - **Bodies.** `net get` cuts each body at 2 KB and ends it with a marker such as
450
+ `[... 18251 more bytes: lustro net body <id> response]`. `--max-body BYTES`
451
+ sets the cut, `--no-body` leaves the bodies out, and `--full` prints them
452
+ whole. `net body` prints one body as it is stored.
453
+ - **Updates.** `net poll` prints a transaction again, marked `[update]`, when it
454
+ was in flight and finishes or fails. With `--json`, the later line for an id
455
+ replaces the earlier one.
456
+ - **WebSockets.** `net ws list` prints a row for each connection: a short id,
457
+ the start time, the state (`open`, `closed 1000`, `failed 403`, `canceled`),
458
+ the counts of sent and received messages, and the URL. `net ws events` prints
459
+ the last 50 events of one connection, oldest first; `--last N` and `--all`
460
+ set how many. A message row has `->` for sent or `<-` for received, the type,
461
+ the size, and the start of the payload; a lifecycle row has `--`. A sent
462
+ message means that OkHttp queued it, which does not show that the server
463
+ received it, and `[not sent]` marks one that `send()` refused. A long text
464
+ ends with a marker such as
465
+ `[... 299961 more bytes: lustro net ws payload 3d2a7f10 5]`, and
466
+ `net ws payload` prints a payload as it is stored. `--sent`, `--received`,
467
+ and `--search TEXT` (with `--regex`, a regular expression) select events on
468
+ the server. `--follow` then prints each
469
+ new event as it comes, until Ctrl-C or until the app clears the connection.
470
+ `--json` prints JSON Lines for both commands, and `--fields` keeps only the
471
+ keys you name. A connection id can be its start, as for a transaction.
472
+
473
+ `net wait` polls until a matching request finishes, prints it, and exits 0. It
474
+ exits 1 after `--timeout` seconds (30 by default). It ignores the requests that
475
+ finished before it started, and a mocked request finishes in a few
476
+ milliseconds, so give the action that sends the request after `--`. `net wait`
477
+ runs it after its first poll:
478
+
479
+ ```bash
480
+ lustro net wait --url /v1/orders --errors -- adb shell input tap 540 1200
481
+ ```
482
+
483
+ ## The screen
484
+
485
+ `lustro screen tree` reads the Screen tab, when the app registers it: each window of the app,
486
+ the activity first and each dialog and popup after it, and in each the nodes on the screen that
487
+ have a text, a tag, a role, an action, or a state. A line has the node's id, its role or class,
488
+ its text, `#tag`, its states and actions, and its visible bounds in uiautomator's format:
489
+
490
+ ```text
491
+ window 0 activity com.example/com.example.MainActivity no-focus [0,0][1080,2400]
492
+ c5.15 Button "Save" #save click [53,1585][985,1667]
493
+ window 1 dialog com.example/com.example.MainActivity focus [120,963][960,1510]
494
+ c13.267 Button "OK" #dialog-ok click [745,1332][897,1437]
495
+ ```
496
+
497
+ `--all` prints every node, also the containers and the nodes that are not on the screen.
498
+ `--unmerged` reads the unmerged Compose semantics tree, `--settle MS` waits until two reads in a
499
+ row are the same, for up to MS milliseconds, and `--json` prints the whole response. Each call
500
+ looks up the token and the port again, so a script that reads the screen often sends the GET
501
+ itself (see [docs/AGENTS.md](https://github.com/Twinsen81/Lustro/blob/main/docs/AGENTS.md#screen-tab-operations)).
502
+
503
+ ## Any route
504
+
505
+ `lustro api` sends one request to any route, with the token and the port that
506
+ the other commands find, and prints the response body: JSON as JSON (compact
507
+ when stdout is not a terminal), any other body as it is. Use it for a tab that
508
+ has no commands of its own; `lustro schema <tabId>` prints the tab's routes.
509
+
510
+ ```bash
511
+ lustro api GET network/transactions --param search=orders # PATH without / is under /api/v1/
512
+ lustro api GET /api/v1/network/transactions/<id>
513
+ lustro api POST network/throttle --body '{"delayMs": 2000}'
514
+ lustro api POST network/rules/_/sync --input rules.json # --input - reads stdin
515
+ ```
516
+
517
+ `--param KEY=VALUE` adds a query parameter, and can repeat a key. A route ignores a
518
+ query parameter that it does not know, so the app names each one that the tab's
519
+ OpenAPI document does not declare for the route in a `Lustro-Warning` response
520
+ header, and every command prints it on stderr, once, such as
521
+ `warning: unknown query parameter "uniqueName"`. An app older than protocol
522
+ 1.15 sends no such header, so for a request with query parameters to that app,
523
+ `lustro api` prints a note on stderr that says so; `lustro schema <tabId>`
524
+ lists the parameters of each route. An error
525
+ response prints the error from its envelope on stderr, such as
526
+ `error: 404 not_found: ...`, and exits 1; nothing goes to stdout. A search
527
+ that the app rejects (a `400` with `field: "search"`) exits 2, as a bad flag
528
+ does. See [Exit codes](#exit-codes) for the others.
529
+
530
+ ## The MCP server
531
+
532
+ `lustro mcp` is a [Model Context Protocol](https://modelcontextprotocol.io) server on stdin and
533
+ stdout, for an agent that can only call MCP tools, such as Claude Desktop. An agent that can run
534
+ shell commands should run the commands above: the client puts the server's tools into the
535
+ agent's context in every session, and a command costs context only when the agent runs it.
536
+
537
+ The agent's client starts the server when a session starts, and stops it when the session ends.
538
+ Add it to the client's MCP config:
539
+
540
+ ```json
541
+ {
542
+ "mcpServers": {
543
+ "lustro": {
544
+ "command": "lustro",
545
+ "args": ["mcp"]
546
+ }
547
+ }
548
+ }
549
+ ```
550
+
551
+ [With a coding agent](https://github.com/Twinsen81/Lustro/blob/main/docs/get-started/coding-agent.md#agents-without-a-shell-the-mcp-server)
552
+ has the config for Claude Desktop, Claude Code, Cursor, VS Code, Codex, and Gemini CLI. A desktop
553
+ app starts the server with a short `PATH`: give `command` the full path that `which lustro`
554
+ prints. When `adb` is not on the `PATH`, the server looks for it in `$ANDROID_HOME/platform-tools`,
555
+ then in the default place of the Android SDK.
556
+
557
+ The tools: `list_requests`, `get_request`, `list_mock_rules`, `add_mock_rule`,
558
+ `delete_mock_rule`, `set_throttle`, `read_logs`, `call_api`, which reaches any route as
559
+ `lustro api` does, and `get_schema`, which returns the tabs or a tab's OpenAPI document. Each says
560
+ in its annotations and its description whether it changes the app.
561
+ [docs/AGENTS.md](https://github.com/Twinsen81/Lustro/blob/main/docs/AGENTS.md#the-mcp-server-for-agents-without-a-shell) lists the route of
562
+ each.
563
+
564
+ Each tool call finds the app as a command does, so the global flags apply to every call:
565
+ `lustro mcp --device SERIAL` selects the device. The server implements JSON-RPC 2.0 with the
566
+ standard library, one message per line, and serves the protocol versions from `2024-11-05` to
567
+ `2026-07-28`. Only protocol messages go to stdout; its own messages go to stderr. A warning from
568
+ the app, such as `warning: unknown query parameter "uniqueName"` for a `call_api` with a parameter
569
+ that the route does not declare, is a text block of its own after the output of the tool, and so is
570
+ the note for a `call_api` with query parameters to an app older than protocol 1.15. A tool argument
571
+ that the app's protocol is too old for is a tool error, as for a command (see
572
+ [An app on an older protocol](#an-app-on-an-older-protocol)).
573
+
574
+ ## Contract tests
575
+
576
+ ```bash
577
+ cd lustro-cli
578
+ pytest
579
+ ```
580
+
581
+ The tests validate every golden fixture against its JSON Schema / OpenAPI
582
+ component, exercise the cursor-polling state machine, the error-envelope parser,
583
+ the `LustroToken` log-line parser, the output of each command, the MCP server's
584
+ messages and the route of each tool, and a client smoke test against a local
585
+ `http.server`.