feat: AI 助手工具预算放大到 60 次 / 20 轮往返,单次提问限时 5 分钟
2 个文件变更
+20
-7
fqh@example.com
| •internal/server/assistant.go | +11 -3 |
| •internal/server/assistant_test.go | +9 -4 |
变更内容
diff --git a/internal/server/assistant.go b/internal/server/assistant.go
index 4c70cc3..5114cc2 100644
--- a/internal/server/assistant.go
+++ b/internal/server/assistant.go
@@ -47,10 +47,13 @@ const (
maxToolEventRunes = 800
// assistantTimeout 是一次提问的总时限(含模型调用与工具执行)。
- assistantTimeout = 100 * time.Second
+ // 工具预算放大后,深度探索要跑几十轮模型往返,100 秒远远不够。
+ assistantTimeout = 300 * time.Second
// maxToolCalls / maxToolRounds 限定单次提问最多查多少次仓库、最多来回几轮。
- maxToolCalls = 6
- maxToolRounds = 4
+ // 每轮模型通常只发 1~3 个工具调用,调用预算要能被轮次用满才有意义:
+ // 60 次 ≈ 20 轮 × 每轮 2~3 个。
+ maxToolCalls = 60
+ maxToolRounds = 20
// 项目概览与工具结果的体积上限:模型上下文要可控,仓库再大也不能整份塞进去。
maxBriefPaths = 400
@@ -151,6 +154,11 @@ func (s *Server) handleAssistantChat(w http.ResponseWriter, r *http.Request) {
failJSON(w, http.StatusNotFound, "项目不存在")
return
}
+ // 私有仓库对游客关闭:助手能读到代码内容,门槛必须与页面一致。
+ if !canReadRepo(userFrom(r.Context()), repo) {
+ failJSON(w, http.StatusUnauthorized, "该仓库为私有,登录后才能使用 AI 助手")
+ return
+ }
dir := s.repoPath(repo.Name)
if !gitx.IsBareRepo(dir) {
failJSON(w, http.StatusInternalServerError, "仓库数据缺失,请联系管理员")
diff --git a/internal/server/assistant_test.go b/internal/server/assistant_test.go
index 4f6ef3f..2e6a70e 100644
--- a/internal/server/assistant_test.go
+++ b/internal/server/assistant_test.go
@@ -565,17 +565,22 @@ func TestAssistantFallsBackWithoutTools(t *testing.T) {
}
// TestAssistantToolBudget 模型反复要求调用工具时要有预算上限:
-// 每一轮要求两个工具(总量超过 maxToolCalls),最终仍应基于已有信息作答,
-// 而不是死循环或把错误抛给访客。
+// 每一轮要求一批工具(20 轮 × 7 个 = 140 次,远超 maxToolCalls=60),
+// 最终仍应基于已有信息作答,而不是死循环或把错误抛给访客。
func TestAssistantToolBudget(t *testing.T) {
srv, st, _, _ := newTestServer(t)
dir := makeRepo(t, srv, st, "budget", nil)
commitFiles(t, dir, "first", map[string]string{"main.go": "package main\n"})
- twoTools := aiToolCalls(toolReq{"c1", "list_files", `{}`}, toolReq{"c2", "list_files", `{}`})
+ // 每轮 7 个工具调用:保证 maxToolRounds 轮内总调用数必然突破 60 次预算
+ perRound := make([]toolReq, 0, 7)
+ for i := 0; i < 7; i++ {
+ perRound = append(perRound, toolReq{id: fmt.Sprintf("c%d", i), name: "list_files", args: `{}`})
+ }
+ sevenTools := aiToolCalls(perRound...)
replies := make([]string, 0, maxToolRounds+1)
for i := 0; i < maxToolRounds; i++ {
- replies = append(replies, twoTools)
+ replies = append(replies, sevenTools)
}
replies = append(replies, aiText("最终回答"))
fake := newFakeAI(t, replies...)