fix(dfc): 修复 default_chatter 提示词重复注入导致的上下文污染#74
Open
tt-P607 wants to merge 1 commit into
Open
Conversation
…tions This commit implements a post-request sanitization mechanism in the Default Chatter session to prevent dynamic extra prompts (injected via `on_prompt_build`) from being permanently added to the dialogue history. By introducing a `clean_mode` flag, the `unread_user_prompt` is cleansed immediately after the LLM request returns, effectively preventing duplicate injections in subsequent dialogue turns.
Contributor
Reviewer's GuideImplements a post-request sanitization mechanism for default_chatter so that system_reminder-style dynamic injections affect only the current LLM call but are not persisted into conversation history, by adding a clean_mode to user prompt building and rewriting the last user payload after the request returns. Sequence diagram for post-request sanitization in default_chattersequenceDiagram
participant Session
participant State
participant LLM
participant PromptAdapter
participant PromptBuilder
Session->>State: execute_with_stream
State->>State: detect last_user_payload (ROLE.USER)
State->>LLM: response.send(stream=use_stream)
LLMI-->>State: LLM reply
alt last_user_payload exists
Session->>PromptAdapter: _build_user_prompt(chat_stream, history_text, unread_lines, extra, clean_mode=True)
PromptAdapter->>PromptBuilder: build_user_prompt(stream_name, history_text, unread_lines, extra, clean_mode=True)
PromptBuilder->>PromptBuilder: tmpl.clone() and rename to default_chatter_user_prompt_clean
PromptBuilder-->>PromptAdapter: clean_text
PromptAdapter-->>Session: clean_text
Session->>State: last_user_payload.content = [Text(clean_text)]
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
clean_modeboolean is threaded through multiple layers and toggles fairly different behavior; consider extracting a dedicated "build_clean_user_prompt" helper to keep the main adapter API simpler and avoid flag-based branching. - In
session.execute_with_stream, the logic that findslast_user_payloadassumes the last payload is always the user prompt you want to sanitize; it may be safer to identify the specific payload more robustly (e.g. by type or metadata) to avoid unintended replacement if the payload ordering changes.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `clean_mode` boolean is threaded through multiple layers and toggles fairly different behavior; consider extracting a dedicated "build_clean_user_prompt" helper to keep the main adapter API simpler and avoid flag-based branching.
- In `session.execute_with_stream`, the logic that finds `last_user_payload` assumes the last payload is always the user prompt you want to sanitize; it may be safer to identify the specific payload more robustly (e.g. by type or metadata) to avoid unintended replacement if the payload ordering changes.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
描述 / Description
修复了
default_chatter中因提示词重复注入导致的严重上下文污染问题。在此之前,各种插件(如
booku_memory、feeling等)通过on_prompt_build注入的system_reminder,会随着完整的 user prompt 被持久化写入对话历史(保存在state.response.payloads中)。这导致在多轮对话中,每一轮都会叠加前一轮所有的动态注入内容,引发上下文体积爆炸,浪费大量 Token,并导致 LLM 逻辑降级或对旧设定的过度反应。解决方案 / Solution
引入了 请求后净化 (Post-request Sanitization) 机制,不改变底层架构而优雅地解决问题:
_build_user_prompt方法中新增了clean_mode标识。prompt_builder.py中,当clean_mode为True时,通过克隆模板并重命名为default_chatter_user_prompt_clean,成功绕过所有的on_prompt_build事件订阅者,生成了一份仅包含纯净对话文本的 Prompt。session.py的处理流中,带有完整注入信息的 Payload 正常被发送给 LLM。而在发送请求返回后,立即调用clean_mode生成纯净文本,并覆盖刚才发送出去的那个 Payload 的内容。这就使得那些动态注入的 Prompt 仅作为“瞬态载荷(Transient Payload)”在本次请求中对 LLM 可见,但绝对不会被作为历史记忆进入下一轮。
测试 / Testing
已通过实际抓包确认:
<system_reminder>信息。Summary by Sourcery
Prevent prompt injection reminders from polluting persisted conversation history in default_chatter.
Bug Fixes:
Enhancements: