Fix the main thread audit for Android startup
Your task
An AI wrote an Android settings screen that reads a config file and parses its JSON inside onCreate, on the main thread. It works on the emulator, but older devices freeze for about a second, and production reports ANRs (Application Not Responding errors, raised when the main thread stalls too long). Your team audits startup traces the way StrictMode would.
An assistant wrote audit_main_thread_work(op_lines, blocking_kinds, anr_limit_ms), the audit. It is in the editor. It has a bug. Find it and fix it.
Input and output:
op_lines: the startup trace. Each line isthread|kind|ms: the thread, the kind of work, and how long it took in milliseconds.blocking_kinds: kinds of work that must never run on the main thread, such asdisk_readornetwork.anr_limit_ms: how long the main thread may stall before the system raises an ANR.- Return the findings in input order, then one summary entry, always last.
Rules the tests check:
- A line is malformed unless it splits on
|into exactly 3 parts and the ms part is one or more digits from 0 to 9. Reportmalformed:<line>and skip it. - Only lines whose thread is exactly
maincount. Ignore all others. - For a main thread line whose kind is in
blocking_kinds, by exact match, reportblocking:<kind>:<ms>. - The main thread total is the sum of the ms of every main thread line, blocking or not.
- The summary:
anr_risk:<total>ifanr_limit_msis greater than 0 and the total is at leastanr_limit_ms- otherwise
jank:<total>if the total is over 16, one frame at 60 frames a second - otherwise
ok:<total>
Examples
audit_main_thread_work(op_lines: list[str], blocking_kinds: list[str], anr_limit_ms: int) -> list[str]Example 1
Input
audit_main_thread_work(["main|disk_read|800", "main|json_parse|250", "io|network|1200"], ["disk_read", "network", "database"], 5000)Output
["blocking:disk_read:800", "jank:1050"]Example 2
Input
audit_main_thread_work(["io|disk_read|800", "main|bind_ui|4"], ["disk_read"], 5000)Output
["ok:4"]