aider icon indicating copy to clipboard operation
aider copied to clipboard

BUG: `Coder.format_chat_chunks` appends two newlines for an empty system reminder

Open yifanxiong272 opened this issue 3 months ago • 2 comments

Issue

Summary

In aider/coders/base_coder.py, Coder.format_chat_chunks can append two newline characters to the final user message even when the active coder has no system reminder.

This occurs when:

  • the model has no max_input_tokens metadata;
  • the model uses the default reminder="user" placement;
  • the active coder has an empty system_reminder.

This combination is reachable through the help coder: HelpPrompts.system_reminder is empty, while custom or unknown models can have an empty model-info dictionary.

Steps to reproduce

  1. Check out Aider main at commit 5dc9490bb35f9729ef2c95d00a19ccd30c26339c.

  2. Add the following test inside TestCoder in tests/basic/test_coder.py:

def test_format_chat_chunks_empty_reminder_preserves_user_message(self):
    model = Model("openai/my-custom-model")
    io = InputOutput(pretty=False, fancy_input=False, yes=True)
    coder = Coder.create(
        model,
        "help",
        io=io,
        stream=False,
        map_tokens=0,
        use_git=False,
    )

    self.assertEqual(model.info, {})
    self.assertEqual(model.reminder, "user")
    self.assertEqual(coder.gpt_prompts.system_reminder, "")

    coder.cur_messages = [
        {"role": "user", "content": "original user content"},
    ]

    chunks = coder.format_chat_chunks()

    self.assertEqual(
        chunks.cur[-1]["content"],
        "original user content",
    )
  1. Run:
python -m pytest tests/basic/test_coder.py -q
  1. Observe that the newly added assertion fails while the 42 existing tests pass.

Expected behavior

Because the help coder's system reminder is empty, formatting the chat chunks should leave the final user message unchanged:

"original user content"

Actual behavior

format_chat_chunks appends two newline characters:

"original user content\n\n"

The run reports:

.................F.........................                              [100%]

FAILED tests/basic/test_coder.py::TestCoder::test_format_chat_chunks_empty_reminder_preserves_user_message

AssertionError: 'original user content\n\n' != 'original user content'
  original user content
-
-

1 failed, 42 passed, 29 subtests passed

Impact

For a custom or otherwise unknown model used through the help coder, the message payload sent to the provider can differ from the user's actual input even though no reminder content exists.

The observed effect is limited to two trailing newline characters; no crash or file modification was observed.

Root cause

format_chat_chunks obtains a missing context limit as zero:

max_input_tokens = self.main_model.info.get("max_input_tokens") or 0

It then checks:

if (
    not max_input_tokens
    or total_tokens < max_input_tokens
    and self.gpt_prompts.system_reminder
):

Because and has higher precedence than or, this is equivalent to:

if (
    not max_input_tokens
    or (
        total_tokens < max_input_tokens
        and self.gpt_prompts.system_reminder
    )
):

When max_input_tokens is zero, the condition succeeds independently of whether system_reminder is empty. The user-reminder branch then constructs:

new_content = (
    final["content"]
    + "\n\n"
    + self.fmt_system_prompt(self.gpt_prompts.system_reminder)
)

Formatting the empty reminder produces an empty string, but the two separator newlines are still appended.

The /help command constructs a coder with edit_format="help", and HelpPrompts.system_reminder is currently empty.

Possible fix direction

Require a non-empty reminder before entering either reminder-placement branch:

if self.gpt_prompts.system_reminder and (
    not max_input_tokens
    or total_tokens < max_input_tokens
):

Regression coverage should include empty and non-empty system reminders with both known and missing max_input_tokens metadata.

Version and model info

Aider: 0.86.3.dev, main@5dc9490bb35f9729ef2c95d00a19ccd30c26339c Python: 3.12.13 Operating system: macOS 15.7.3 Installation: source checkout; focused pytest reproduction Edit format: help Model: custom openai/my-custom-model metadata used only to construct the coder; no provider request is made Configuration: empty model metadata, default reminder="user", map_tokens=0, use_git=False, stream=False

yifanxiong272 avatar Aug 23 '26 06:08 yifanxiong272

Confirmed, including the operator-precedence reading — Patch is a fork of aider, and the same three lines are here unchanged.

One correction to the reproduction, and it makes the bug look worse rather than better. This fork has no help coder, so Coder.create(model, "help", ...) raises UnknownEditFormat here. But ArchitectPrompts.system_reminder is "" as well, and architect is a first-class mode reached by --architect, /architect and --chat-mode architect. Substituting "architect" for "help" in your test reproduces it exactly: 'original user content\n\n'. Worth checking upstream too, since aider/coders/architect_prompts.py has the same empty reminder — if so, the reachable surface is not only /help, and an empty info dict is the normal case for a self-hosted or proxied model rather than an exotic one.

We took your suggested fix verbatim (PierrunoYT/patch#131) — the reminder test moved outside the budget test. It is behaviour-preserving in every other combination, and it makes this condition agree with the two above it, which already gate on self.gpt_prompts.system_reminder before building main_sys and reminder_message.

Two tests: architect with an unknown model, asserting the user message is untouched (fails before the fix), and a diff coder asserting the reminder is still appended after \n\n (passes either way, on purpose — it is the guard against over-fixing).

One line in aider/coders/base_coder.py; happy to open a PR, though your own would be the same diff.

PierrunoYT avatar Aug 31 '26 11:08 PierrunoYT

Related angle from outside aider: per-request overhead like this is one half of the token bill; the other half in my experience was re-sent history - every turn shipping the accumulated context again, and every fresh session paying to rebuild what the last one knew.

I ended up moving memory outside the context entirely: a local engine (Postgres, over MCP) encodes decisions as sessions happen, and later sessions recall a compact constructed answer - a few hundred tokens flat regardless of project age - instead of replaying history. The two fixes stack: trim the per-request waste AND stop the context from growing with time.

Demo at https://juvina.ai if the mechanism interests anyone (a token receipt prints under each answer). Disclosure: my tool, free VS Code extension.

1picassoai avatar Sep 06 '26 16:09 1picassoai