Skip to content

fix: prevent agent loops when ReadMediaFile is called on text files (#1218)#1263

Open
Goodeve0 wants to merge 2 commits into
MoonshotAI:mainfrom
Goodeve0:fix/read-media-description
Open

fix: prevent agent loops when ReadMediaFile is called on text files (#1218)#1263
Goodeve0 wants to merge 2 commits into
MoonshotAI:mainfrom
Goodeve0:fix/read-media-description

Conversation

@Goodeve0

@Goodeve0 Goodeve0 commented Jul 1, 2026

Copy link
Copy Markdown

Related Issue

Resolve #1218

Problem

When an agent uses ReadMediaFile on a text file (e.g. a UTF-16 LE encoded AGENTS.md), the tool returns an error like "is a text file". On retry the model often picks ReadMediaFile again, leading to an unproductive loop until the repeat-dedup escalation finally kicks in.

What changed

Multi-layered fix:

  1. read-media.md: Moved the file-type constraint to the first line with a clear statement that this tool only supports image and video files — to reduce the probability the model picks it for text files in the first place.

  2. read-media.ts: Strengthened the error messages and parameter descriptions to be more explicit about supported vs unsupported file types.

  3. tool-dedup.ts: Added wrong-tool error detection — a new fast-escalation path for categorical tool errors where the model fundamentally chose the wrong tool (e.g. ReadMediaFile on text). These errors escalate in 2 reminder rounds instead of waiting for generic repeat-dedup thresholds (r2 reminder at streak 2, force-stop at streak 4).

  4. Tests: Added tool-dedup.test.ts covering wrong-tool error classification and escalation timing; updated read-media.test.ts for new error message wording; tool-call.e2e.test.ts gets a new assertion for the e2e path.

Benchmark note

A/B benchmark was run (5 runs per group, UTF-16 LE AGENTS.md + "请阅读 AGENTS.md 并总结" prompt). Both groups showed 0% ReadMediaFile hit rate — the bug is intermittent in this setup and could not be reproduced with a single-shot prompt. The fix remains semantically correct and the multi-layer mitigation (description + error messages + fast escalation) addresses the root cause.

…oonshotAI#1218)

- read-media.md: move file-type constraint to first line with CRITICAL prefix
- read-media.ts: strengthen error messages and parameter descriptions
- tool-dedup.ts: add wrong-tool error detection that escalates faster
  (r2 reminder at streak 2, force-stop at streak 4 for category errors)
- Add unit tests for wrong-tool error classification and escalation
- Update existing test assertions for new error message wording

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c89c6d8eb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


// Wrong-tool errors (category errors that retries will never fix)
// escalate faster than generic transient failures.
if (isWrongToolError(result.output)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Constrain wrong-tool detection to tool errors

When the repeated tool result is successful output that happens to contain one of these phrases (for example Reading a file that says "is a text file" or rerunning a command whose stdout includes "must use the Read tool"), this branch treats it as a wrong-tool error because it only inspects result.output. That changes normal repeat handling from the generic thresholds to an R2 reminder on the 2nd repeat and stopTurn on the 4th, even though no tool error occurred; gate this path on result.isError === true (and ideally the relevant tool/error class) before fast-stopping the turn.

Useful? React with 👍 / 👎.

@@ -1,13 +1,14 @@
**CRITICAL: This tool ONLY reads image files (PNG, JPG, GIF, WebP, SVG, etc.) and video files (MP4, MOV, AVI, etc.). It CANNOT read text files — never pass a .md, .ts, .json, .txt, or any other text file path to this tool. For text files, use the Read tool. To list directories, use `ls` via Bash for a known directory, or Glob for pattern search.**

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove unsupported SVG from media examples

For SVG inputs, this new leading description now tells the model that ReadMediaFile supports SVG, but detectFileType maps .svg through TEXT_MIME_BY_SUFFIX as text and ReadMediaFile.execution rejects fileType.kind === 'text'. In the common case where a user asks to inspect an SVG asset, this prompt encourages the exact wrong tool and will produce the text-file error instead of reading the file with Read; either remove SVG from the supported examples or add real SVG support.

Useful? React with 👍 / 👎.

Co-authored-by: Cursor <cursoragent@cursor.com>
@changeset-bot

changeset-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 075e3a7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Goodeve0

Goodeve0 commented Jul 1, 2026

Copy link
Copy Markdown
Author

Update (second commit)

Addressed both P2 Codex review items in a follow-up commit:

  1. P2: Constrain wrong-tool detection to tool errors (075e3a7)

    • Added result.isError === true guard before isWrongToolError() in tool-dedup.ts:286
    • This prevents successful outputs containing wrong-tool phrases from being fast-tracked through the wrong-tool escalation path (r2 at streak 2, stop at streak 4) instead of generic thresholds
  2. P2: Remove unsupported SVG from media examples (075e3a7)

    • Removed SVG from the CRITICAL header in read-media.md
    • .svg is classified as text by detectFileType() and rejected by ReadMediaFile.execution(), so listing it as supported was misleading

New tests added:

  • isWrongToolError is a pure text matcher — documents that isWrongToolError is a pure text matcher; the isError guard lives in finalizeResult
  • does NOT fast-escalate successful results with wrong-tool-like text — verifies the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent 反复使用 ReadMediaFile 读取文本文件,无视系统提示

1 participant