## TL;DR
Break the loop with a terminal-state check the agent trusts: it kept re-querying because its completion test never matched, usually comparing status strings case-sensitively, checking a field the API returns under another name, or polling a stale cached response. Log the raw status value the API returns for the finished run, fix the comparison to match it exactly, and add a maximum poll count that escalates instead of spinning.

```text
agent loop detected: agent kept re-querying the same finished run and failed to mark it complete
```

## Use this when
- The run or call fails with this symptom: agent loop detected: agent kept re-querying the same finished run and failed to mark it complete.
- You are working on an agent waiting on MLflow or tracker runs to finish and the failure repeats on the same run or configuration.
- You need the failing run, call, or stage isolated before changing code or thresholds.

## Not for this skill when
- A run that genuinely never finishes because the training job hung, where the fix is killing the job rather than the poller.
- The error text names a different tool, stage, or root cause. Match the verbatim error first and use that skill.

## Steps
1. Capture one raw API response for the finished run and read the exact status value and field name. Verify: the true terminal representation is known
2. Compare it with the agent's completion condition character by character, including case and enum naming. Verify: the mismatch is pinpointed
3. Fix the condition to the observed value and treat both success and failure terminal states as completion. Verify: the condition matches reality for both outcomes
4. Add a poll budget with escalation, then run the agent against the finished run and confirm it completes in one or two polls. Verify: the agent marks the run complete and stops

## Variant phrasings
### agent loop re-querying finished run not marked complete
The verbatim phrasing.
### agent polling loop status comparison never matches
The comparison phrasing.
### agent stuck polling completed mlflow run fix
The stuck phrasing.

Compatibility: Agent polling loops over experiment tracking APIs

## Why it happens
Polling loops fail silently when the exit condition describes a world slightly different from the API's: one word's case, one renamed field. The agent is behaving exactly as written, so the fix is aligning its model of terminal state with the server's actual vocabulary, plus a budget so misalignment can never spin forever.

## Edge cases / pitfalls
- Cached or eventually consistent read replicas can serve a stale RUNNING status; poll the primary endpoint for terminal checks.
- Record every poll's observed status in the agent log; the loop's cause should never need guessing.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_MouJUmfkh4mbBsYRn08s8w
