Skip to content

prepare_messages_context discards the overwrite flag set by the history sanitizer #374

Open

Description

@BrennanTM

What happens

prepare_messages_context (base.py:1054) threads a full_overwrite flag through its steps so that a maintained history is written back to state instead of appended. The sanitizer added in #339 sets that flag whenever it drops something (_sanitize_history, base.py:859), and the _ensure_system_first branch sets it too. The summarization step then assigns rather than combines: new_state, full_overwrite = self._summarize_context(...), and _summarize_context has no incoming flag and returns False whenever it does not summarize (base.py:973, :979, :988, :1001). So the sanitizer's flag survives only when summarization or dangling-tool patching happens to fire on the same turn.

Two consequences follow. First, the request and the stored thread diverge: the sanitizer removes the same messages again on every turn, and the stored thread keeps them. The resumed named ChatAgent in #331 shows the shape: the request is system, human, ai, human while the stored thread still holds the duplicated system prompt. Second, when summarization or dangling patching does fire, the flag is honored and the drops are written into the persistent thread, so whether a persisted history is rewritten depends on an unrelated trigger.

In v0.17.0 the same assignment was harmless because nothing ran before it; #339 added two flag-setting steps in front of it. (Line references are as of current main, 128b811.)

Fix, and one ordering note

        if summarize:
            new_state, summarized = self._summarize_context(
                new_state,
                system_prompt=system_prompt,
                summary_prompt=summary_prompt,
            )
            full_overwrite = full_overwrite or summarized

This should land after #372: honoring the flag while the first-turn drop is still in place would write the system-prompt-less history into persistent threads for good.

Whether sanitizer drops should persist at all is your call. The method's docstring and the flag the sanitizer sets say the maintained history is meant to be written back; the alternative is a request-only sanitizer that never sets the flag. Happy to PR either shape.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    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