Your question is Debug a Broken Code Snippet. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Axonius pulls device inventory from a dozen security adapters (CrowdStrike, Qualys, Tanium) and merges them into one canonical asset list. A teammate wrote the merge step below for the batch worker, and it behaves differently every time it runs.
def merge_assets(devices, seen=set()):
# merge device records from multiple security adapters by device id
merged = {}
for device in devices:
device_id = device["id"]
if device_id in seen:
continue
seen.add(device_id)
merged[device_id] = device
for device_id in merged:
adapters = merged[device_id]["adapters"]
primary = adapters[0]["source"]
merged[device_id]["primary_source"] = primary
if primary == "unmanaged":
del merged[device_id]
print("merged", len(merged), "assets")
The worker process stays alive between batches and calls merge_assets again for the next set of devices pulled from the adapters. Walk through everything wrong with this function and explain what you would change.