Skip to content

[BUG] AI ignores AGENTS.md bat-file rules on Windows (CRLF + encoding) despite existing guardrails #31275

Closed

Description

Problem

Despite AGENTS.md containing explicit Windows bat-file rules (UTF-8 without BOM, CRLF line endings, chcp 65001 as second line, no BOM before @echo off), the AI agent violates these rules in practice. On the same system that hosts the AGENTS.md file, the AI has:

  1. Written .bat files with LF-only line endings (causes instant crash on cmd.exe)
  2. Written .bat files with GBK encoding instead of UTF-8 (because the system code page is 936 and the AI incorrectly chose GBK over the AGENTS.md rule)
  3. Failed to add chcp 65001 as the second line

Root Cause (Three Layers)

Layer 1 — AI Model Behavior

The AI model generates LF (\n) line endings natively. There is no post-processing step in the write tool that detects .bat/.cmd files on Windows and converts endings to CRLF.

Layer 2 — bash Tool Serialization

When the AI passes content through the bash tool (PowerShell command string via JSON), any CRLF in the content is stripped to LF. Even if the AI intended CRLF, the transport layer loses it. See also issue #31224.

Layer 3 — No AGENTS.md Enforcement at Write Time

The write tool at packages/opencode/src/tool/write.ts has no mechanism to check AGENTS.md rules before writing. The AI must consciously remember to apply the rules, which is unreliable.

Existing AGENTS.md Rules (correct but unenforced)

  • Encoding: UTF-8 without BOM
  • Line endings: CRLF (\r\n)
  • First line: @echo off
  • Second line: chcp 65001 >nul (if file contains non-ASCII)
  • No BOM before @echo off (causes crash)

Proposed Fix

Option A — Tool-Level Guard (Recommended)

Add a pre-write hook in write.ts that detects .bat/.cmd file extensions on Windows (process.platform === 'win32') and automatically:

  • Converts LF to CRLF
  • Ensures first line is @echo off
  • Ensures second line is chcp 65001 >nul
  • Strips any UTF-8 BOM

Option B — Validation After Write

Run a lightweight validator after write completes that warns (or re-writes) if the file violates the known rules.

Option C — AGENTS.md-Aware Tool

Generalize the concept: allow AGENTS.md to define file-type-specific invariants that the tool layer enforces automatically.

Related Issues

Environment

  • OpenCode latest dev (commit 914a643)
  • Windows 11, code page 936 (Chinese)
  • System has AGENTS.md with bat-file rules

Activity

  1. github-actions commented on Jun 7, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    The scope here (AGENTS.md enforcement at the tool layer) extends slightly beyond #31224, but the root cause and proposed fix largely overlap. You may want to consider whether this should be consolidated with #31224 or kept separate to track the AGENTS.md-awareness angle specifically.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions