Fix the lock check behind thread-safety comments

Review the AI's codeEasy10 minAnthropic style

Your task

A teammate ran an AI over a Java concurrency utility, and it added comments like "thread-safe: guarded by the lock above". A lock near one access proves nothing. The claim holds only if every access to the field takes the same lock. Your team checks claims like this against a log of every access.

An assistant wrote find_unguarded_fields(access_lines). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • access_lines: one line per access to a shared field, written field|access|lock. The access is read or write. The lock is the name of the lock held, or none when no lock is held. Every line has exactly these three fields, and nothing is trimmed.
  • Return the names of the fields that are not guarded, without repeats, sorted by plain character order.

Rules the tests check:

  • A field is guarded only when every access to it holds the same lock, and that lock is not none.
  • So a field fails when any of its accesses holds no lock, or when two of its accesses hold different locks.
  • Lock names are compared exactly, case included: Lock and lock are different locks.
  • Reads count as much as writes.

Examples

find_unguarded_fields(access_lines: list[str]) -> list[str]
Example 1
Inputfind_unguarded_fields(["count|write|stateLock", "count|read|stateLock"])
Output[]
Example 2
Inputfind_unguarded_fields(["count|write|stateLock", "count|read|none"])
Output["count"]
Example 3
Inputfind_unguarded_fields(["total|write|lockA", "total|read|lockB"])
Output["total"]