A multi-agent system can stall in two ways. Agents can keep sending the same messages to each other forever, or agents can each wait for another agent in a circle so nobody moves. Write the watchdog that detects both.
Implement diagnose(trace, waits, min_repeats=3).
Inputs
trace: the message log in order, a list of (sender, receiver, content) tuples of strings.waits: a dict mapping an agent name to the list of agents it is currently blocked on. Agents missing from the dict, or with an empty list, are not waiting on anyone.min_repeats: an int, at least 2.Output: a dict with three keys:
"loop_period": the smallest such that the last min_repeats messages of trace are one block of messages repeated min_repeats times in a row; 0 if no such exists."deadlocked": sorted list of agents that lie on a cycle of the wait-for graph (an edge means waits on ). An agent waiting on itself counts."blocked": sorted list of agents that are not deadlocked themselves but can reach a deadlocked agent by following wait-for edges. They will never be released either.Two messages are equal when all three fields are equal.