Skip to content

fix(adb): count UTF-8 bytes in logcat total_bytes - #2212

Merged
morluto merged 2 commits into
morluto:mainfrom
sx4im:fix/adb-logcat-total-bytes-utf8
Oct 12, 2026
Merged

morluto merged 2 commits into
morluto:mainfrom
sx4im:fix/adb-logcat-total-bytes-utf8

Conversation

@sx4im

@sx4im sx4im commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2175. read_adb_logcat reported total_bytes in 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

AdbProvider decodes the captured stdout buffer as UTF-8, then assigns the resulting string's .length to total_bytes. .length counts UTF-16 code units, so multi-byte characters shrink the number. The field is named total_bytes, so it should be a byte count. ASCII output was unaffected.

Change and scope

One line in src/adb/AdbProvider.ts:

total_bytes: Buffer.byteLength(ran.value.stdout, "utf8"),

Buffer.byteLength is already the codebase convention for this. I grepped every producer of this field: this is the only place read_adb_logcat's total_bytes is computed, so no sibling call sites needed the same fix. An alternative was threading the raw capture buffer through runAdb instead 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

  • Semantic owner and earliest changed stage: AdbProvider read_adb_logcat result projection; no schema change, the field stays a nonnegative int.
  • CLI and MCP/tool-catalog contract: none, same schema, corrected value.
  • Provider, bridge, target-format, or platform compatibility: none.
  • Evidence, artifact, provenance, or reconstruction contract: total_bytes now matches the captured byte count; message text, line splitting, and coverage are untouched.
  • Process execution, authorization, cleanup, or containment impact: none.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none.

Evidence and regression coverage

  • Tests added or updated: src/adb/AdbProvider.test.ts gains "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 and total_bytes is 72 (37 + 35).
  • Base reproduction or other evidence: the new test fails on the pre-fix revision, reporting 66 (the UTF-16 unit count), and passes after the fix.
  • User-visible CLI/MCP output (if applicable): total_bytes in the read-adb-logcat result changes for non-ASCII dumps; ASCII dumps are byte-identical.
  • Remaining proof gaps: reproduction used a synthetic adb stub; no real device was available. The issue itself notes this gap.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

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 successful
  • npx tsc --noEmit — clean

Compatibility, safety, and release

  • Breaking changes or migration steps: none. This corrects a value to match the field's byte unit; non-ASCII dumps will report larger (correct) numbers.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; ADB provider only, synthetic stub, no real device.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: none; no new process execution or data capture.

Review checklist

  • This PR addresses a concrete problem or an agreed enhancement.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T14:33:14.230566Z 5c30d44 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/adb/AdbProvider.ts Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

total_bytes: Buffer.byteLength(ran.value.stdout, "utf8"),

P2 Badge Count the captured bytes before UTF-8 decoding

When adb logcat emits malformed UTF-8 (for example, a native log message containing byte 0xff), runAdb has already converted the original Buffer with toString("utf8"), replacing that byte with U+FFFD; this expression then counts the replacement as three bytes, so total_bytes still differs from the captured byte count. Carry the raw stdout length through runAdb or use runAdbCapture for this path instead of reconstructing byte provenance from decoded text.

AGENTS.md reference: AGENTS.md:L21-L23

ℹ️ 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".

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.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@morluto
morluto merged commit ec2d251 into morluto:main Oct 12, 2026
30 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 12, 2026
3 tasks done
@CassieFox23

Copy link
Copy Markdown
Contributor

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, total_bytes matches the true byte count on all 8 lines I tried, with CJK text and emoji. Without it, the count was too low on 7 of the 8.

Results

Each 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.

Case True bytes Base Head
ASCII text 98 98 98
CJK, 2 characters (the PR's example) 81 77 81
Japanese 126 92 126
Chinese and ASCII mix 107 93 107
One emoji 79 77 79
Two flag emoji 91 83 91
Family emoji (joined with ZWJ) 100 86 100
Mixed ASCII, CJK, kana, emoji, flag, family, accent 143 112 143

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 adb read of the same dump.

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.

vitest run src/adb/ passes on head on Node 22, 24 and 26 (99 tests).

Two small notes:

  • The bad-bytes test also passes on base. Base counts the one bad byte as one unit, and that is 34 too. It fails on the first commit 5c30d44 (36, not 34). So it guards the second commit, not the original bug. The UTF-8 test fails on base (66, not 72), as the PR says.
  • The PR text still says the fix is Buffer.byteLength. The merged code uses the captured byte count instead.

Not run

  • The MCP tool. I ran only the CLI command.
  • A bad byte from the real tablet. The stub test covers it.
  • Other devices, Android versions or adb builds.
  • The full test suite. I ran only src/adb/.
  • A build of main after the merge. Its two changed files carry the same changes as the head I tested.

Thanks, sx4im, for the fix and the tests.

How I tested

One 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 adb shell log. Then I ran rea read-adb-logcat on base and on head. I set --pid to the writer's process id, so base and head read the same bytes.

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
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1 (device runs used 24.18.0)
  • Head: 50fc35d
  • Base: 7ae3c50
  • Device: Samsung Galaxy Tab A8, Android 14 (arm64)
  • Other: adb 37.0.0 (Apache-2.0 among others)
  • Other agents may share the box. I did not time anything.
Commands

REA ran only two adb calls: adb version, and the logcat read below. I set REA_ADB_PATH to a small script that passes both to the real adb and copies the logcat output to a file. The script refuses any other call. A second run without the file copy gave the same numbers for the mixed line (base 112, head 143, true 143).

adb -s <serial> shell log -t reatest2212 '<text>'                           # exit 0, 18 times: 16 cases, 1 probe, 1 rerun without the file copy
adb -s <serial> logcat -d -b main -v threadtime -s reatest2212:*            # find the writer's pid, exit 0
adb -s <serial> shell logcat -d -b main -t 500 --pid <pid>                  # direct read, exit 0
node <tree>/scripts/rea.mjs read-adb-logcat <serial> --count 500 --buffer main --pid <pid> --json   # base and head, exit 0, 32 runs
adb -s <serial> logcat -d -s reatest2212:* | wc -c                          # 1,294 bytes, exit 0
vitest run src/adb/ --reporter=verbose                                      # head, Node 22, 24, 26: exit 0 each
vitest run src/adb/AdbProvider.test.ts                                      # head's tests on base source, Node 24: exit 1 (UTF-8 test fails)
vitest run src/adb/AdbProvider.test.ts                                      # head's tests on first-commit source 5c30d442, Node 24: exit 1 (bad-bytes test fails)

The 1,294 bytes are the 28-byte header, the 16 test lines (1,202 bytes) and the 64-byte probe line.

Example output

Mixed line, head (lines and total_bytes from the JSON result; base gave 112 for the same lines):

lines: ["--------- beginning of main", "10-12 13:45:53.916  6233  6233 I reatest2212: ASCII 中文 日本語 😀 🇯🇵 👨‍👩‍👧‍👦 café"]
total_bytes: 143

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime Process capture and runtime observation bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(adb): logcat reports character units in total_bytes

3 participants