mirror of
https://github.com/RightNow-AI/openfang.git
synced 2026-07-30 23:05:08 +00:00
bug fixes
This commit is contained in:
@@ -51,6 +51,20 @@ const MAX_CONTINUATIONS: u32 = 5;
|
||||
/// Maximum message history size before auto-trimming to prevent context overflow.
|
||||
const MAX_HISTORY_MESSAGES: usize = 20;
|
||||
|
||||
/// Detect when the LLM claims to have performed an action (sent, posted, emailed)
|
||||
/// without actually calling any tools. Prevents hallucinated completions.
|
||||
fn phantom_action_detected(text: &str) -> bool {
|
||||
let lower = text.to_lowercase();
|
||||
let action_verbs = ["sent ", "posted ", "emailed ", "delivered ", "forwarded "];
|
||||
let channel_refs = [
|
||||
"telegram", "whatsapp", "slack", "discord", "email", "channel",
|
||||
"message sent", "successfully sent", "has been sent",
|
||||
];
|
||||
let has_action = action_verbs.iter().any(|v| lower.contains(v));
|
||||
let has_channel = channel_refs.iter().any(|c| lower.contains(c));
|
||||
has_action && has_channel
|
||||
}
|
||||
|
||||
/// Extra guidance injected after failed tool calls to prevent fabricated follow-up actions.
|
||||
const TOOL_ERROR_GUIDANCE: &str =
|
||||
"[System: One or more tool calls failed. Failed tools did not produce usable data. Do NOT invent missing results, cite nonexistent search results, or pretend failed tools succeeded. If your next steps depend on a failed tool, either retry with a materially different approach or explain the failure to the user and stop. Do not write files, store memory, or take downstream actions based on failed tool outputs.]";
|
||||
@@ -481,6 +495,23 @@ pub async fn run_agent_loop(
|
||||
} else {
|
||||
text
|
||||
};
|
||||
// Phantom action detection: if the LLM claims it performed a
|
||||
// channel action (send, post, email, etc.) but never actually
|
||||
// called the corresponding tool, re-prompt once to force real
|
||||
// tool usage instead of hallucinated completion.
|
||||
let text = if !any_tools_executed && iteration == 0 && phantom_action_detected(&text) {
|
||||
warn!(agent = %manifest.name, "Phantom action detected — re-prompting for real tool use");
|
||||
messages.push(Message::assistant(text));
|
||||
messages.push(Message::user(
|
||||
"[System: You claimed to perform an action but did not call any tools. \
|
||||
You must use the appropriate tool (e.g., channel_send, web_fetch, file_write) \
|
||||
to actually perform the action. Do not claim completion without executing tools.]"
|
||||
));
|
||||
continue;
|
||||
} else {
|
||||
text
|
||||
};
|
||||
|
||||
final_response = text.clone();
|
||||
session.messages.push(Message::assistant(text));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user