Skip to content

Commit 2efaac7

Browse files
author
huzijie.sea
committed
feat(循环检测): 改进循环检测机制和警告提示
1 parent 22ec1d3 commit 2efaac7

3 files changed

Lines changed: 44 additions & 25 deletions

File tree

src/agent/Agent.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class Agent extends EventEmitter {
195195
enableDynamicThreshold: true, // 启用动态阈值调整
196196
enableLlmDetection: true, // 启用LLM智能检测
197197
whitelistedTools: [], // 白名单工具(如监控工具)
198-
maxWarnings: 2, // 最大警告次数(默认2次
198+
maxWarnings: 3, // 最大警告次数(从2提高到3,给模型更多机会改正
199199
};
200200
this.loopDetector = new LoopDetectionService(loopConfig, this.chatService);
201201

@@ -1017,7 +1017,13 @@ export class Agent extends EventEmitter {
10171017

10181018
if (loopDetected?.detected) {
10191019
// 渐进式策略: 先警告,多次后才停止
1020-
const warningMsg = `⚠️ Loop detected (${loopDetected.warningCount}/${this.loopDetector['maxWarnings']}): ${loopDetected.reason}\nPlease try a different approach.`;
1020+
// 关键改进:给出具体指示,而不是让模型解释自己
1021+
const warningMsg = `⚠️ Loop detected (${loopDetected.warningCount}/${this.loopDetector['maxWarnings']}): ${loopDetected.reason}
1022+
1023+
IMPORTANT: Do NOT explain or justify yourself. Instead:
1024+
1. If you were about to call a tool, call it NOW
1025+
2. If you need to do something different, do it NOW
1026+
3. No filler text - action only`;
10211027

10221028
if (loopDetected.shouldStop) {
10231029
// 超过最大警告次数,停止任务

src/agent/LoopDetectionService.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,24 @@ export class LoopDetectionService {
271271
return false; // 无 ChatService 则跳过
272272
}
273273

274-
const LOOP_DETECTION_PROMPT = `你是AI循环诊断专家。分析以下对话历史,判断AI是否陷入无效状态:
274+
const LOOP_DETECTION_PROMPT = `You are an AI loop detection expert. Analyze the conversation history below and determine if the AI is stuck in a **genuine infinite loop**.
275275
276-
无效状态特征:
277-
- 重复操作: 相同工具/响应重复多次
278-
- 认知循环: 无法决定下一步,表达困惑
276+
## What IS a genuine loop (answer YES)
277+
- Repeatedly attempting the same FAILED operation (e.g., same tool call failing 3+ times)
278+
- Explicitly expressing confusion (e.g., "I'm not sure...", "I'm stuck...")
279+
- Repeatedly asking the same question without progress
279280
280-
关键: 区分真正的死循环 vs 正常的渐进式进展
281+
## What is NOT a loop (answer NO)
282+
- Transitional/filler text (e.g., "OK, I'll continue", "Let me proceed") - this is just unnecessary but harmless politeness
283+
- Executing tasks sequentially (even if outputs look similar, but processing different steps)
284+
- Any response immediately after receiving a loop warning (give the AI a chance to correct)
285+
- Updating Todo progress before moving to the next task
281286
282-
最近对话历史:
287+
Recent conversation history:
283288
${this.formatMessagesForDetection(messages.slice(-10))}
284289
285-
回答 "YES" (陷入循环) 或 "NO" (正常进展)`;
290+
Answer "YES" ONLY if you are **certain** this is a genuine infinite loop.
291+
When in doubt, answer "NO" to give the AI more chances.`;
286292

287293
try {
288294
const response = await this.chatService.chat([

src/prompts/default.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ IMPORTANT: You must NEVER generate or guess URLs for the user unless you are con
99
1010
If the user asks for help or wants to give feedback inform them of the following:
1111
- /help: Get help with using Blade Code
12-
- To give feedback, users should report the issue at https://github.com/anthropics/claude-code/issues
12+
- To give feedback, users should report the issue at https://github.com/echoVic/blade-code/issues
1313
1414
## Tone and style
1515
- Only use emojis if the user explicitly requests it. Avoid using emojis in all communication unless asked.
@@ -23,6 +23,25 @@ Prioritize technical accuracy and truthfulness over validating the user's belief
2323
## Planning without timelines
2424
When planning tasks, provide concrete implementation steps without time estimates. Never suggest timelines like "this will take 2-3 weeks" or "we can do this later." Focus on what needs to be done, not when. Break work into actionable steps and let users decide scheduling.
2525
26+
## Execution Efficiency (CRITICAL)
27+
When executing tasks autonomously:
28+
- **NO filler text**: Never output transitional phrases like "Let me continue...", "Now I will...", "OK, next step is..." between tool calls
29+
- **Action over narration**: After completing a tool call, immediately proceed to the next tool call without announcing your intentions
30+
- **Report only when done**: Only output text when you have meaningful results to report or need user input
31+
- **If warned about loops**: Do NOT explain yourself - immediately call the next required tool
32+
33+
<example-bad>
34+
// ❌ BAD: Wastes tokens and triggers loop detection
35+
[TodoWrite completed]
36+
"OK, I will continue with the next task. Let me now implement..."
37+
</example-bad>
38+
39+
<example-good>
40+
// ✅ GOOD: Efficient execution
41+
[TodoWrite completed]
42+
[Immediately calls Read/Write/Edit tool]
43+
</example-good>
44+
2645
## Task Management
2746
You have access to the TodoWrite tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
2847
These tools are also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
@@ -86,21 +105,9 @@ The user will primarily request you perform software engineering tasks. This inc
86105
- The conversation has unlimited context through automatic summarization.
87106
88107
## Tool usage policy
89-
- When doing file search, prefer to use the Task tool in order to reduce context usage.
90-
- You should proactively use the Task tool with specialized agents when the task at hand matches the agent's description.
91-
- When WebFetch returns a message about a redirect to a different host, you should immediately make a new WebFetch request with the redirect URL provided in the response.
92-
- You can call multiple tools in a single response. If you intend to call multiple tools and there are no dependencies between them, make all independent tool calls in parallel. Maximize use of parallel tool calls where possible to increase efficiency. However, if some tool calls depend on previous calls to inform dependent values, do NOT call these tools in parallel and instead call them sequentially. For instance, if one operation must complete before another starts, run these operations sequentially instead. Never use placeholders or guess missing parameters in tool calls.
93-
- If the user specifies that they want you to run tools "in parallel", you MUST send a single message with multiple tool use content blocks. For example, if you need to launch multiple agents in parallel, send a single message with multiple Task tool calls.
94-
- Use specialized tools instead of bash commands when possible, as this provides a better user experience. For file operations, use dedicated tools: Read for reading files instead of cat/head/tail, Edit for editing instead of sed/awk, and Write for creating files instead of cat with heredoc or echo redirection. Reserve bash tools exclusively for actual system commands and terminal operations that require shell execution. NEVER use bash echo or other command-line tools to communicate thoughts, explanations, or instructions to the user. Output all communication directly in your response text instead.
95-
- VERY IMPORTANT: When exploring the codebase to gather context or to answer a question that is not a needle query for a specific file/class/function, it is CRITICAL that you use the Task tool with subagent_type=Explore instead of running search commands directly.
96-
<example>
97-
user: Where are errors from the client handled?
98-
assistant: [Uses the Task tool with subagent_type=Explore to find the files that handle client errors instead of using Glob or Grep directly]
99-
</example>
100-
<example>
101-
user: What is the codebase structure?
102-
assistant: [Uses the Task tool with subagent_type=Explore]
103-
</example>
108+
- When WebFetch returns a redirect to a different host, make a new WebFetch request with the redirect URL.
109+
- You can call multiple tools in a single response. Make independent tool calls in parallel. If calls depend on previous results, run them sequentially. Never use placeholders or guess missing parameters.
110+
- Use specialized tools instead of bash commands: Read for files, Edit for editing, Write for creating. Reserve Bash for system commands only.
104111
105112
## Code References
106113

0 commit comments

Comments
 (0)