Repository navigation
fix(adb): count UTF-8 bytes in logcat total_bytes - #2212
Conversation
read_adb_logcat assigned the decoded stdout string's UTF-16 code-unit length to total_bytes, undercounting non-ASCII dumps (a 37-byte CJK line reported 33). Measure the UTF-8 byte length instead, matching the field's byte-unit contract and the repo's Buffer.byteLength convention. Fixes morluto#2175
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c30d44273
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
💡 Codex ReviewLine 785 in 5c30d44 When AGENTS.md reference: AGENTS.md:L21-L23 ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Carry the pre-decode stdout byte length through runAdb so malformed UTF-8 (replaced by U+FFFD on decode) still reports the true captured byte count instead of the re-encoded replacement size.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
TL;DR: This merged as ec2d251 while I was testing. The merged commit has the same changes as the head I tested. On a real Android tablet, with the fix, ResultsEach row is one log line, read twice on base and twice on head. All 32 runs agreed. "True bytes" is the size of the raw adb output, copied to a file as REA read it. Each dump is a 28-byte header line plus the log line.
Head matched the true size every time. Base matched the UTF-16 unit count every time, which is the cause in the issue. The returned lines were the same on both. Each true size also matches a direct The PR's 37 and 33 cannot show on a tablet, since a real log line has a longer header. The same 4-byte gap does show: 81 true, 77 on base.
Two small notes:
Not run
Thanks, sx4im, for the fix and the tests. How I testedOne thing I saw that is not new in this PR: with no pid filter, REA read 515 lines for a count of 500. It kept 500, and the byte count covered all 515. Base does the same. I used a Samsung Galaxy Tab A8 on Android 14 (arm64), plugged in by USB. For each case, I wrote one log line with REA has no tag filter, so the pid pins the dump. The writer has exited by then, so the dump cannot change between runs. I wrote only my own log lines to the tablet. Test setup
CommandsREA ran only two adb calls: The 1,294 bytes are the 28-byte header, the 16 test lines (1,202 bytes) and the 64-byte probe line. Example outputMixed line, head ( 🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊 |
Summary
Fixes #2175.
read_adb_logcatreportedtotal_bytesin UTF-16 code units instead of bytes. Any dump with CJK or emoji undercounted: a 37-byte CJK line came back as 33.Problem and expected behavior
AdbProviderdecodes the captured stdout buffer as UTF-8, then assigns the resulting string's.lengthtototal_bytes..lengthcounts UTF-16 code units, so multi-byte characters shrink the number. The field is namedtotal_bytes, so it should be a byte count. ASCII output was unaffected.Change and scope
One line in
src/adb/AdbProvider.ts:Buffer.byteLengthis already the codebase convention for this. I grepped every producer of this field: this is the only placeread_adb_logcat'stotal_bytesis computed, so no sibling call sites needed the same fix. An alternative was threading the raw capture buffer throughrunAdbinstead of re-encoding the decoded string; for valid UTF-8 the re-encoded count equals the captured byte count, so the extra plumbing buys nothing.Contract and boundary impact
AdbProviderread_adb_logcatresult projection; no schema change, the field stays a nonnegative int.total_bytesnow matches the captured byte count; message text, line splitting, and coverage are untouched.docs/public/product-catalog.json), package, or installation impact: none.Evidence and regression coverage
src/adb/AdbProvider.test.tsgains "reports UTF-8 byte counts in total_bytes for non-ASCII logcat output". A stub adb emits one CJK and one emoji log line; the test asserts the decoded lines are intact andtotal_bytesis 72 (37 + 35).total_bytesin theread-adb-logcatresult changes for non-ASCII dumps; ASCII dumps are byte-identical.For evidence-bearing changes:
Validation performed
npx vitest run src/adb/— 4 files, 98 tests, all pass (new test failed pre-fix: expected 72, got 66)npm run check:fast— typecheck + lint, 2 tasks successfulnpx tsc --noEmit— cleanCompatibility, safety, and release
Review checklist
type(scope): outcome.