Skip to content

Conversation

@1A7432
Copy link
Contributor

@1A7432 1A7432 commented Dec 23, 2025

Summary

This PR fixes a critical bug where AGENTS.md, CLAUDE.md, and other instruction files' content would be duplicated and accumulated in the system prompt with each conversation turn.

Root Cause

In the generate_prompts() function (lua/avante/llm.lua:457-460):

  • When opts.prompt_opts.system_prompt exists, it's reused from the previous conversation
  • agents_rules and cursor_rules are then blindly appended each time
  • This causes exponential growth: 1x → 2x → 3x → 4x... content duplication per turn

Changes

  • Added duplicate check before appending agents_rules
  • Added duplicate check before appending cursor_rules
  • Uses string.find() with literal matching to detect existing content

Impact

  • ✅ Prevents token waste and context pollution
  • ✅ Fixes infinite loop behavior reported by users
  • ✅ Maintains backward compatibility
  • ✅ No performance impact (string.find is O(n) but runs once per turn)

Testing

Manually tested with GLM-4.7 provider:

  • Before fix: AGENTS.md content duplicated 2x → 3x → 4x → 5x → 6x across turns
  • After fix: AGENTS.md content appears exactly once, consistently

Related Issues

This PR addresses user-reported issues where:

  • AGENTS.md content appears 5+ times in prompts
  • Exponentially growing token usage (e.g., 18k tokens for simple inputs)
  • Model confusion due to repeated instructions

References

@1A7432 1A7432 force-pushed the fix/agents-rules-duplicate branch 2 times, most recently from 7779541 to a0bc008 Compare December 23, 2025 09:13
This commit comprehensively fixes critical bugs causing AGENTS.md and other
instruction files to be duplicated in system prompts, leading to exponential
token growth and context pollution.

Root Causes Fixed:

1. session_ctx.system_prompt timing bug:
   - Previously saved at line 446 BEFORE appending agents_rules/cursor_rules
   - Next conversation turn would re-append these rules to incomplete prompt
   - Resulted in: 1x -> 2x -> 3x -> 4x... exponential growth per turn

2. Double reading when instructions_file = "AGENTS.md":
   - First read at line 261-270 as user message (instructions_file)
   - Second read at line 457 as system prompt (agents_rules)
   - Same file content added twice in different locations

Fixes Applied:

1. Fixed session_ctx.system_prompt update timing (llm.lua:475):
   - Removed premature save at line 446
   - Now saves AFTER all appends complete
   - Ensures complete prompt is cached for next turn

2. Prevented duplicate file reading (llm.lua:266-275, prompts.lua:150-175):
   - Track instruction_file_read to record already-read files
   - Pass exclude_file to get_agents_rules_prompt()
   - Skip files already read via instructions_file

3. Added duplicate detection as safety net (llm.lua:459-467):
   - Use string.find() to check existing content
   - Prevents append if already present
   - Handles edge cases and legacy sessions

Impact:
- Eliminates token waste and context pollution
- Fixes infinite loop behavior
- Maintains backward compatibility
- Allows users to safely use instructions_file = "AGENTS.md"

Test Scenarios:
✅ instructions_file = "AGENTS.md" - no duplication
✅ Multi-turn conversations - stable token usage
✅ Session restoration - no re-appending
✅ Legacy sessions with old prompts - graceful handling
@1A7432 1A7432 force-pushed the fix/agents-rules-duplicate branch from a0bc008 to 4505a91 Compare December 23, 2025 10:13
pre-commit-ci-lite bot and others added 2 commits December 23, 2025 10:16
Root Cause:
- When opts.prompt_opts.system_prompt exists (from chat_history), it was directly reused
- This historical system_prompt contained old selected_files content
- Rendering the template again would add current selected_files on top
- Result: selected_files content accumulated exponentially per conversation turn

Fix:
- Always regenerate system_prompt by rendering template from scratch
- Never reuse opts.prompt_opts.system_prompt from history
- This ensures selected_files are only included once per turn

Impact:
- Fixes the critical bug where avante.md/AGENTS.md content multiplied (2x -> 3x -> 9x)
- Ensures clean system_prompt generation every conversation turn
- Maintains all other functionality (agents_rules, cursor_rules, custom system_prompt)
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.

1 participant