Detect a Stuck Agent Loop
Problem Statement
An agent stuck in a loop repeats the same action. Detect whether the last k actions in a trace are all identical, a simple no-progress guard.
Background
A cheap loop-guard fires when the most recent k actions are the same string. If the trace has fewer than k actions, it cannot yet be flagged.
Your Task
def is_stuck(actions, k):
Return True if the last k actions exist and are all equal, else False.
Input Format
- actions (list of strings), k (int, >= 1).
Output Format
- A boolean.
Sample
print(is_stuck(["a", "b", "b", "b"], 3))
Output:
True
Example:
print(is_stuck(["a", "b", "b", "b"], 3))
True
- Check if the trace length is sufficient to evaluate the last k actions: the list
["a", "b", "b", "b"]has a length of 4, which is greater than or equal to k=3, so the check proceeds. - Extract the most recent k actions from the end of the list: the last 3 elements are
["b", "b", "b"]. - Verify if all actions in this subset are identical by comparing each element to the first one:
"b" == "b","b" == "b", and"b" == "b"all evaluate to true. - Since every action in the window matches, the condition for being stuck is satisfied.
- The final output is True
Constraints:
- Need at least
kactions to flag (else False). - The last
kmust be identical. k >= 1.
1. Background Knowledge
In AI agent systems, an agent loop repeatedly selects an action based on the current state. A common failure mode is a stuck agent, where the agent repeats the same action indefinitely without making progress. This can happen due to poor planning, a degenerate policy, or a state that does not change after the action is executed.
A no-progress guard is a lightweight safety mechanism that monitors the agentβs recent behavior. By checking whether the last k actions are identical, the system can detect a potential loop early and trigger a recovery strategy, such as resetting the state, selecting a random alternative action, or escalating to a higher-level planner. This guard is intentionally simple and cheap, making it suitable for real-time monitoring.
The problem reduces to a basic list inspection task: given a sequence of actions, determine if the final k elements are all the same string. This is a common pattern in monitoring and logging systems where you need to detect repetitive behavior in a stream of events.
2. Algorithm Approach
The approach is a direct slice-and-compare strategy:
- Boundary Check: First, verify that the list actions contains at least k elements. If len(actions) < k, return False immediately because there are not enough actions to evaluate.
- Extract Window: Use Python slicing to extract the last k actions: actions[-k:].
- Uniformity Check: Determine if all elements in this slice are identical. This can be done by:
- Comparing each element to the first element of the slice.
- Using set() to check if the slice contains only one unique value.
- Using all() with a generator expression.
The most Pythonic and efficient method is to compare the slice to a list of repeated first elements or use set to check for uniqueness.
3. Step-by-Step Strategy
- Validate Input Length:
- Check if len(actions) < k.
- If true, return False.
- Extract the Last k Actions:
- Create a variable window = actions[-k:].
- This slice contains exactly k elements, representing the most recent actions.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.