[Bug] AIChatBot plugin: conversation history corruption and system prompt removal
type: :bug: bug
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe your issue
## Summary
Three issues in the AI ChatBot plugin (`AIChatBot.java`):
### Bug 1: Wrong variable saved to assistant history (line ~75)
```java
// Current (wrong):
chatHistory.add(ChatCompletionMessageParam.ofAssistant(...
.content(...Content.ofText(message)) // ❌ User's message
// Should be:
.content(...Content.ofText(response)) // ✅ AI's response
```
### Bug 2: System prompt removed when history exceeds max length (line ~79)
```java
// Current (wrong):
if (chatHistory.size() > maxHistory) {
chatHistory.removeFirst(); // ❌ Removes system prompt at index 0
}
// Should be:
while (chatHistory.size() > maxHistory && chatHistory.size() > 1) {
chatHistory.remove(1); // ✅ Keep system prompt at index 0
}
```
### Security Enhancement: Sandwich the system prompt
To mitigate prompt injection attacks, the system prompt should be reinforced at **both the start AND end** of the message list before sending to the API:
```java
// Before API call, add a reminder at the end:
var messagesWithReminder = new ArrayList<>(chatHistory);
messagesWithReminder.add(ChatCompletionMessageParam.ofSystem(
ChatCompletionSystemMessageParam.builder()
.content(Content.ofText("REMINDER: " + systemPrompt))
.build()
));
```
**Why?**
- LLMs have recency bias (prioritize recent context)
- Attackers can inject "ignore previous instructions" in chat
- Sandwiching instructions makes jailbreaking significantly harder
### Impact
- Bug 1: Corrupted conversation history
- Bug 2: System prompt deleted after N messages → trivial prompt injection
- Without sandwich: Easier prompt injection via chat spam
### Affected file
`mod/src/main/java/com/soulfiremc/server/plugins/AIChatBot.java`
### How can we reproduce what you got?
_No response_
### SoulFire/Error Logs
_No response_
### SoulFire Version
2.3.0
关闭于 2026-02-21 1 条评论