Skip to content

Commit cfa2719

Browse files
committed
feat: enhance AI tool descriptions and execution results with clear guidance
Tool descriptions: - compile_and_test: add warning against repeated calls after success - retrieve_file_content: add constraint that file must be modified after retrieve Execution results: - Add next_steps field to all compile_and_test outcomes - Guide AI on what to do next based on result (success/failure/timeout) - Explicitly forbid retrieve-compile loops without modifications
1 parent 296292f commit cfa2719

29 files changed

Lines changed: 1195 additions & 17 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_816 {
5+
6+
using Func = std::function<vector<string>(string)>;
7+
8+
class AmbiguousCoordinatesSolution : public SolutionBase<Func> {
9+
public:
10+
vector<string> ambiguousCoordinates(string s);
11+
12+
AmbiguousCoordinatesSolution();
13+
};
14+
15+
} // namespace problem_816
16+
} // namespace leetcode
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_1626 {
5+
6+
using Func = std::function<int(vector<int>&, vector<int>&)>;
7+
8+
class BestTeamWithNoConflictsSolution : public SolutionBase<Func> {
9+
public:
10+
int bestTeamScore(vector<int>& scores, vector<int>& ages);
11+
12+
BestTeamWithNoConflictsSolution();
13+
};
14+
15+
} // namespace problem_1626
16+
} // namespace leetcode
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "leetcode/core.h"
3+
4+
namespace leetcode {
5+
namespace problem_3254 {
6+
7+
using Func = std::function<vector<int>(vector<int>&, int)>;
8+
9+
class FindThePowerOfKSizeSubarraysISolution : public SolutionBase<Func> {
10+
public:
11+
//! 3254. Find the Power of K-Size Subarrays I
12+
//! https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/
13+
vector<int> resultsArray(vector<int>& nums, int k);
14+
15+
FindThePowerOfKSizeSubarraysISolution();
16+
};
17+
18+
} // namespace problem_3254
19+
} // namespace leetcode
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "leetcode/core.h"
3+
4+
namespace leetcode {
5+
namespace problem_3658 {
6+
7+
using Func = std::function<int(int)>;
8+
9+
class GcdOfOddAndEvenSumsSolution : public SolutionBase<Func> {
10+
public:
11+
//! 3658. GCD of Odd and Even Sums
12+
//! https://leetcode.com/problems/gcd-of-odd-and-even-sums/
13+
int gcdOfOddEvenSums(int n);
14+
15+
GcdOfOddAndEvenSumsSolution();
16+
};
17+
18+
} // namespace problem_3658
19+
} // namespace leetcode
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_3719 {
5+
6+
using Func = std::function<int(vector<int>&)>;
7+
8+
class LongestBalancedSubarrayISolution : public SolutionBase<Func> {
9+
public:
10+
int longestBalanced(vector<int>& nums);
11+
12+
LongestBalancedSubarrayISolution();
13+
};
14+
15+
} // namespace problem_3719
16+
} // namespace leetcode
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_3418 {
5+
6+
using Func = std::function<int(vector<vector<int>>&)>;
7+
8+
class MaximumAmountOfMoneyRobotCanEarnSolution : public SolutionBase<Func> {
9+
public:
10+
//! 3418. Maximum Amount of Money Robot Can Earn
11+
//! https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/
12+
int maximumAmount(vector<vector<int>>& coins);
13+
14+
MaximumAmountOfMoneyRobotCanEarnSolution();
15+
};
16+
17+
} // namespace problem_3418
18+
} // namespace leetcode
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#include "leetcode/core.h"
3+
4+
namespace leetcode {
5+
namespace problem_132 {
6+
7+
using Func = std::function<int(string)>;
8+
9+
class PalindromePartitioningIiSolution : public SolutionBase<Func> {
10+
public:
11+
//! 132. Palindrome Partitioning II
12+
//! https://leetcode.com/problems/palindrome-partitioning-ii/
13+
int minCut(string s);
14+
15+
PalindromePartitioningIiSolution();
16+
};
17+
18+
} // namespace problem_132
19+
} // namespace leetcode
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_915 {
5+
6+
using Func = std::function<int(vector<int>&)>;
7+
8+
class PartitionArrayIntoDisjointIntervalsSolution : public SolutionBase<Func> {
9+
public:
10+
//! 915. Partition Array into Disjoint Intervals
11+
//! https://leetcode.com/problems/partition-array-into-disjoint-intervals/
12+
int partitionDisjoint(vector<int>& nums);
13+
14+
PartitionArrayIntoDisjointIntervalsSolution();
15+
};
16+
17+
} // namespace problem_915
18+
} // namespace leetcode
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "leetcode/core.h"
2+
3+
namespace leetcode {
4+
namespace problem_3597 {
5+
6+
using Func = std::function<vector<string>(string)>;
7+
8+
class PartitionStringSolution : public SolutionBase<Func> {
9+
public:
10+
vector<string> partitionString(string s);
11+
12+
PartitionStringSolution();
13+
};
14+
15+
} // namespace problem_3597
16+
} // namespace leetcode

script/leetcode/ai/tools.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ def _retrieve_file_content() -> Dict[str, Any]:
122122
"type": "function",
123123
"function": {
124124
"name": "retrieve_file_content",
125-
"description": "读取已生成的文件内容。测试失败或编译错误时,使用此工具查看当前文件内容进行分析",
125+
"description": """读取已生成的文件内容。测试失败或编译错误时,使用此工具查看当前文件内容进行分析。
126+
127+
⚠️ 重要约束:
128+
- **retrieve 后必须修改文件!** 禁止「retrieve → compile」不改文件的无效循环
129+
- 如果只是验证修复思路,直接在脑中分析,不要 retrieve
130+
- 只有在不确定当前代码状态、需要确认细节时才 retrieve
131+
- retrieve 后必须调用 create_or_update_file(overwrite_existing=true)更新文件""",
126132
"parameters": {
127133
"type": "object",
128134
"properties": {
@@ -222,7 +228,12 @@ def _compile_and_test() -> Dict[str, Any]:
222228
223229
使用场景:
224230
- 生成题目代码后,立即验证正确性
225-
- 修复代码后,快速验证修复结果""",
231+
- 修复代码后,快速验证修复结果
232+
233+
⚠️ 重要约束:
234+
- **如果上一次调用返回了 "编译成功,所有测试通过",禁止再次调用!**
235+
- 测试通过后,等待系统处理(LeetCode 提交或生成报告),不要重复测试
236+
- 只有在修改了代码(create_or_update_file)或添加了测试用例(append_test_case)后才再次调用""",
226237
"parameters": {
227238
"type": "object",
228239
"properties": {
@@ -605,7 +616,8 @@ def _compile_and_test(self, problem_id: int) -> Dict[str, Any]:
605616
"error_type": error_analysis["type"],
606617
"error_details": error_analysis["details"],
607618
"suggestion": error_analysis["suggestion"],
608-
"output": output
619+
"output": output,
620+
"next_steps": "❌ 编译失败。修复流程:1) 仔细阅读错误信息和错误详情;2) 分析问题原因(语法?类型?头文件?);3) 直接调用 create_or_update_file 修复代码(overwrite_existing=true);4) 修复后再次调用 compile_and_test 验证。⚠️ 禁止仅 retrieve 不修改的循环!"
609621
}
610622

611623
# 编译成功,检查测试结果
@@ -619,7 +631,8 @@ def _compile_and_test(self, problem_id: int) -> Dict[str, Any]:
619631
return {
620632
"is_successful": True,
621633
"status_message": f"编译成功,{test_count} 个测试通过",
622-
"output": output[-2000:] if len(output) > 2000 else output
634+
"output": output[-2000:] if len(output) > 2000 else output,
635+
"next_steps": "✅ 本地测试已通过。现在:1) 等待系统自动提交到 LeetCode 验证;2) 禁止再次调用 compile_and_test;3) 如果 LeetCode 返回失败,按 Wrong Answer 流程处理(添加测试用例 → 修复代码 → 重新测试)"
623636
}
624637
elif "FAILED" in output:
625638
# 测试失败,分析失败原因
@@ -630,7 +643,8 @@ def _compile_and_test(self, problem_id: int) -> Dict[str, Any]:
630643
"error_type": error_analysis["type"],
631644
"error_details": error_analysis["details"],
632645
"suggestion": error_analysis["suggestion"],
633-
"output": output
646+
"output": output,
647+
"next_steps": "❌ 测试失败。修复流程:1) 分析错误信息和失败详情;2) 确定修复方案;3) 直接调用 create_or_update_file 修复代码(overwrite_existing=true);4) 修复后再次调用 compile_and_test 验证。⚠️ 禁止 retrieve_file_content → compile 不改文件的循环!"
634648
}
635649
else:
636650
# 无法确定结果
@@ -645,7 +659,8 @@ def _compile_and_test(self, problem_id: int) -> Dict[str, Any]:
645659
"is_successful": False,
646660
"error_message": "编译或测试超时",
647661
"error_type": "timeout",
648-
"suggestion": "编译或测试时间过长,请检查代码是否有无限循环或其他问题"
662+
"suggestion": "编译或测试时间过长,请检查代码是否有无限循环或其他问题",
663+
"next_steps": "⏱️ 超时。检查:1) 是否有死循环;2) 算法时间复杂度是否过高;3) 测试用例是否过大。修复后重新调用 compile_and_test"
649664
}
650665
except Exception as e:
651666
return {"is_successful": False, "error_message": str(e)}

0 commit comments

Comments
 (0)