v9.1.1
Menu

Detect a Stuck Agent Loop

easyPROAgent Loop Control & PlanningAI AgentsAI Agents 1

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

Input
print(is_stuck(["a", "b", "b", "b"], 3))
Output
True
Reasoning
  • Inputs: actions is the agent's trace in time order, oldest first, so the most recent action is the last entry. k is the window size: how many of the newest actions must repeat before the guard fires.
  • Is there a full window? The trace has n=4n = 4 actions and k=3k = 3. Since n≥kn \ge k, a window of the 3 newest actions exists, so a verdict is possible.
  • The window: the newest 3 actions are positions 2,3,42, 3, 4 of the trace: "b", "b", "b". The oldest action "a" falls outside it and plays no part.
  • The test: the window holds exactly one distinct value, {b}\{\text{b}\}, so every action in it is the same. The output is True.
  • Notice: with k=4k = 4 the window would include "a", giving two distinct values, so the answer would be False. And when n<kn < k there is no full window at all, which is also False rather than an error.

Constraints

  • Need at least k actions to flag (else False).
  • The last k must be identical.
  • k >= 1.

Learn this in

More from AI Agents 1

PRO

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.

solution.py

Test results

Sign in to run your code against the test cases.