From 3a7fa5d7e3b757cbe432ca258889399700c34e98 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 05:11:24 +0000 Subject: [PATCH 01/28] fix(assistant): judge progress-only replies by the text after the last tool call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cursor reports every paragraph of a turn joined as its result. A reply that opened with a plan ("我先查…") before calling tools therefore still started with that plan after the answer followed, assistantProgressOnly took it for a progress note, and the turn was retried: the answer ran and was stored again, or the turn failed with 模型连续只返回进度说明. Bot replies on such a CLI failed with 助手未提交回复 after three runs. - Judge Main Bot's text written after its last tool call, as the other CLIs report it; without a tool call or text after it the whole reply is judged. Studio chat and Bot replies use the same judgment (Bots now keep the stream activity for it). - A reply is a progress note only when every line is a plan, so an answer that opens with "我先查了…,结论如下。" and then lists results is kept. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 48 +++- .../httpapi/assistant_progress_text_test.go | 243 ++++++++++++++++++ internal/httpapi/bot_interactions.go | 6 +- 3 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 internal/httpapi/assistant_progress_text_test.go diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index f69745444..fff9e9546 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -853,7 +853,7 @@ func (h *assistantHandlers) chat(c *gin.Context) { continue } } - if len(reply.Drafts) == 0 && assistantProgressOnly(reply.Message, req.Messages) { + if len(reply.Drafts) == 0 && assistantProgressOnly(assistantProgressText(reply.Message, activity.snapshot()), req.Messages) { if progressRetries >= 2 { fail(502, "模型连续只返回进度说明,尚未完成请求;执行记录已保留,请核对后继续。") return @@ -1085,9 +1085,41 @@ func assistantNativeRetryPrompt() string { return "本轮尚未完成,请沿当前上下文继续,核对已执行操作,避免重复写入。需要待确认的平台操作时通过 mcp__agentflow_session__submit_assistant_reply 提交完整草稿;普通问答和已实际完成的操作用自然语言总结结果;若工具参数校验失败,修正字段后再提交。不要提交空草稿或进度占位回复。" } +// assistantProgressText is the part of a reply that tells whether the turn +// stopped at a progress note. Most CLIs report only the text written after +// their last tool call as the result, but Cursor joins every paragraph of the +// turn, so a reply that opened with "我先…" before calling tools still starts +// with that plan after the answer followed. Judge Main Bot's text after its +// last tool call, as the other CLIs report it; without a tool call, or with +// no text after it, the whole reply is judged. +func assistantProgressText(message string, events []domain.AssistantActivityEvent) string { + last := -1 + for i, event := range events { + if event.Kind == "tool" && event.Parent == "" { + last = i + } + } + if last < 0 { + return message + } + var parts []string + for _, event := range events[last+1:] { + if event.Kind != "text" || event.Parent != "" { + continue + } + if text := strings.TrimSpace(event.Text); text != "" && !assistantCLINoticeText(text) { + parts = append(parts, text) + } + } + if len(parts) == 0 { + return message + } + return strings.Join(parts, "\n\n") +} + func assistantProgressOnly(message string, messages []assistantMessage) bool { text := strings.TrimSpace(message) - if len([]rune(text)) > 600 || strings.Contains(text, "```") { + if text == "" || len([]rune(text)) > 600 || strings.Contains(text, "```") { return false } if len(messages) > 0 { @@ -1103,6 +1135,18 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { return false } } + // Every line must be a plan: an answer that opens with "我先查了…,结论 + // 如下。" and then lists the results is not a progress note. + for line := range strings.SplitSeq(text, "\n") { + if strings.TrimSpace(line) != "" && !assistantProgressSentence(line) { + return false + } + } + return true +} + +func assistantProgressSentence(text string) bool { + text = strings.TrimSpace(text) for _, prefix := range []string{"稍等", "请稍等", "好的,我先", "我先", "让我先", "接下来我会", "接下来我将", "我会先", "I'll first", "Let me first"} { if strings.HasPrefix(text, prefix) { return true diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go new file mode 100644 index 000000000..783b41e5f --- /dev/null +++ b/internal/httpapi/assistant_progress_text_test.go @@ -0,0 +1,243 @@ +package httpapi + +import ( + "bytes" + "context" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "agentflow/internal/domain" +) + +// Cursor reports every paragraph of the turn joined as its result. A reply +// that opened with a plan before calling tools must be judged by the text +// written after the tools, as the other CLIs report it. +func TestAssistantProgressTextJudgesTextAfterLastToolCall(t *testing.T) { + answer := "仓库:登记 1 个 GitHub 仓库,默认分支 main。\n\n成员:只有负责人 roberttwang。" + plan := "我先查本项目的代码仓库和成员,再分段总结。" + cases := []struct { + name string + message string + events []domain.AssistantActivityEvent + progress bool + }{ + { + name: "plan, tools, answer joined by Cursor", + message: plan + answer, + events: []domain.AssistantActivityEvent{ + {Kind: "text", Text: plan}, + {Kind: "tool", Tool: "agentflow__repositories_list"}, + {Kind: "toolDone"}, + {Kind: "thinking", Text: "整理结果"}, + {Kind: "text", Text: answer}, + }, + }, + { + name: "plan then tools without any answer", + message: plan, + events: []domain.AssistantActivityEvent{{Kind: "text", Text: plan}, {Kind: "tool", Tool: "Read"}, {Kind: "toolDone"}}, + progress: true, + }, + { + name: "text after the last tool is itself a progress note", + message: plan + "接下来我会修改 store.py 并补测试。", + events: []domain.AssistantActivityEvent{ + {Kind: "text", Text: plan}, + {Kind: "tool", Tool: "Read"}, + {Kind: "text", Text: "接下来我会修改 store.py 并补测试。"}, + }, + progress: true, + }, + { + name: "no tool call judges the whole reply", + message: "稍等,我先检查一下。", + events: []domain.AssistantActivityEvent{{Kind: "text", Text: "稍等,我先检查一下。"}}, + progress: true, + }, + { + name: "subagent tools do not hide Main Bot's answer", + message: plan + answer, + events: []domain.AssistantActivityEvent{ + {Kind: "text", Text: plan}, + {Kind: "tool", Tool: "Agent", ID: "dispatch-1"}, + {Kind: "tool", Tool: "Read", Parent: "dispatch-1"}, + {Kind: "agentText", Text: "我先读取文件", Parent: "dispatch-1"}, + {Kind: "toolDone", ID: "dispatch-1"}, + {Kind: "text", Text: answer}, + {Kind: "tool", Tool: "Grep", Parent: "dispatch-1"}, + }, + }, + { + name: "a CLI notice after the tools is not an answer", + message: plan, + events: []domain.AssistantActivityEvent{{Kind: "text", Text: plan}, {Kind: "tool", Tool: "Read"}, {Kind: "text", Text: cliBashNotice}}, + progress: true, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := assistantProgressOnly(assistantProgressText(tc.message, tc.events), nil); got != tc.progress { + t.Fatalf("progress-only = %v, want %v (judged %q)", got, tc.progress, assistantProgressText(tc.message, tc.events)) + } + }) + } +} + +// End to end through the chat handler: a CLI that streams a plan, calls a +// tool, streams the answer and then reports all of it joined (as Cursor +// does) must finish in one run. Before the fix the joined result was taken +// for a progress note, the CLI ran again and the answer was stored twice. +func TestAssistantJoinedResultAfterToolsIsNotRetriedAsProgress(t *testing.T) { + h, user := workbenchFixture(t) + bin := filepath.Join(t.TempDir(), "codebuddy") + script := `#!/bin/sh +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP。"}]}} +{"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} +{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} +{"type":"assistant","message":{"content":[{"type":"text","text":"项目中有后端研发 LOOP,共四个节点。\n\n建议直接用它派发需求。"}]}} +{"type":"result","result":"我先查看项目里的 LOOP。项目中有后端研发 LOOP,共四个节点。\n\n建议直接用它派发需求。"} +JSON +` + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + sid := "joined-progress" + body, _ := json.Marshal(assistantRequest{SessionID: sid, ProjectID: "workbench", Messages: []assistantMessage{{Role: "user", Content: "先说一句你要做什么,然后查看已有 LOOP,分 2 段说明"}}}) + c, rec := workbenchContext(user, "POST", "/api/assistant/generations", bytes.NewBuffer(body)) + h.startGeneration(c) + if rec.Code != 202 { + t.Fatalf("start: %d %s", rec.Code, rec.Body.String()) + } + deadline := time.Now().Add(10 * time.Second) + for { + g, _ := h.repo.AssistantGeneration(user.ID, sid) + if g.Status == "completed" { + break + } + if g.Status != "running" || time.Now().After(deadline) { + // Before the fix: retried as progress until 模型连续只返回进度说明. + t.Fatalf("generation %s: %s", g.Status, g.Error) + } + time.Sleep(10 * time.Millisecond) + } + saved, ok := h.repo.AssistantSession(user.ID, sid) + if !ok || len(saved.Messages) != 2 { + t.Fatalf("saved session: %+v", saved) + } + var stored struct { + Message string `json:"message"` + } + if err := json.Unmarshal([]byte(saved.Messages[1].Content), &stored); err != nil { + t.Fatal(err) + } + if strings.Count(stored.Message, "共四个节点") != 1 || !strings.Contains(stored.Message, "我先查看项目里的 LOOP。") { + t.Fatalf("stored reply = %q", stored.Message) + } + for _, event := range saved.Messages[1].Activity { + if event.Kind == "status" && strings.Contains(event.Text, "继续执行尚未完成的步骤") { + t.Fatalf("progress retry status recorded: %+v", saved.Messages[1].Activity) + } + } +} + +// The plan-only case still continues: text before the tools and nothing +// after them is a progress note even when the CLI streamed it. +func TestAssistantPlanBeforeToolsWithoutAnswerStillContinues(t *testing.T) { + h, user := workbenchFixture(t) + // Count only reply runs: the background title summary uses the CLI too. + h.titleSummary = func(context.Context, domain.User, string) (string, error) { return "", nil } + dir := t.TempDir() + counter := filepath.Join(dir, "calls") + bin := filepath.Join(dir, "codebuddy") + script := `#!/bin/sh +n=0 +if [ -f '` + counter + `' ]; then read -r n < '` + counter + `'; fi +n=$((n + 1)) +echo "$n" > '` + counter + `' +if [ "$n" -gt 1 ]; then +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"项目中有后端研发 LOOP,共四个节点。"}]}} +{"type":"result","result":"项目中有后端研发 LOOP,共四个节点。"} +JSON +exit 0 +fi +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP。"}]}} +{"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} +{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} +{"type":"result","result":"我先查看项目里的 LOOP。"} +JSON +` + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + h.generate = nil + body, _ := json.Marshal(assistantRequest{SessionID: "plan-only", ProjectID: "workbench", Messages: []assistantMessage{{Role: "user", Content: "查看已有 LOOP 并给出结果"}}}) + c, rec := workbenchContext(user, "POST", "/api/assistant/chat", bytes.NewBuffer(body)) + h.chat(c) + calls, _ := os.ReadFile(counter) + if rec.Code != 200 || strings.TrimSpace(string(calls)) != "2" || !strings.Contains(rec.Body.String(), "共四个节点") { + t.Fatalf("calls=%s code=%d body=%s", strings.TrimSpace(string(calls)), rec.Code, rec.Body.String()) + } +} + +// A Bot reply is judged the same way: the joined plan and answer is the +// reply, and the CLI is not run again in terminal-only mode for it. +func TestBotJoinedResultAfterToolsIsNotRetriedAsProgress(t *testing.T) { + stubCLIModelDiscovery(t) + _, h, bot := botFixture(t) + dir := t.TempDir() + counter := filepath.Join(dir, "calls") + bin := filepath.Join(dir, "codebuddy") + script := `#!/bin/sh +n=0 +if [ -f '` + counter + `' ]; then read -r n < '` + counter + `'; fi +echo "$((n + 1))" > '` + counter + `' +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查询一下项目状态。"}]}} +{"type":"assistant","message":{"content":[{"type":"tool_use","id":"runs-1","name":"mcp__agentflow__runs_list","input":{}}]}} +{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"runs-1","content":"[]"}]}} +{"type":"assistant","message":{"content":[{"type":"text","text":"项目当前有 2 个运行中的 LOOP,均在实现节点。"}]}} +{"type":"result","result":"我先查询一下项目状态。项目当前有 2 个运行中的 LOOP,均在实现节点。"} +JSON +` + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-joined", ChatType: "single"}, nil, domain.BotInteraction{SenderID: "new-wecom-user", Content: "项目状态"}) + calls, _ := os.ReadFile(counter) + if err != nil || strings.TrimSpace(string(calls)) != "1" || !strings.Contains(reply, "均在实现节点") { + t.Fatalf("calls=%s err=%v reply=%q", strings.TrimSpace(string(calls)), err, reply) + } +} + +// A reply is a progress note only when every line is a plan. The first case +// is the text a real Cursor (grok-4.7) turn wrote after its last tool call. +func TestAssistantProgressOnlyRequiresEveryLineToBeAPlan(t *testing.T) { + cases := []struct { + text string + progress bool + }{ + {"我先查了本项目的代码仓库和成员,结论如下。\n\n仅 1 个 GitHub 仓:loopforge-sweep-20261127-1200,分支 main。\n\n所有者编号 2,与当前用户 roberttwang 一致。\n\n加入申请 0 条,接口未返回其他成员。", false}, + {"我先查了仓库:\n- 1 个 GitHub 仓库\n- 默认分支 main", false}, + {"我先查看项目里的 LOOP。", true}, + {"稍等,我先检查", true}, + {"我先看看 store.py。\n接下来我会补测试。", true}, + {"我先读代码。\n\n接下来我将运行测试。", true}, + {"", false}, + {" \n\n ", false}, + } + for _, tc := range cases { + if got := assistantProgressOnly(tc.text, nil); got != tc.progress { + t.Errorf("assistantProgressOnly(%q) = %v, want %v", tc.text, got, tc.progress) + } + } +} diff --git a/internal/httpapi/bot_interactions.go b/internal/httpapi/bot_interactions.go index 3131119ac..56b794423 100644 --- a/internal/httpapi/bot_interactions.go +++ b/internal/httpapi/bot_interactions.go @@ -146,6 +146,9 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not policy = "当前 Bot 为只读模式:只使用只读工具查询项目状态、LOOP 进度和待办;不要尝试修改平台数据或外部系统。用户要求修改时,说明需要到平台页面操作。用自然语言回答。" } full := h.turnPrompt(req, h.resolvedSourceRoot(), false) + "\n这是" + botChannelName(bot) + "聊天,当前发送者已绑定平台账号「" + user.LoginName + "」,只使用该账号的执行身份、个人凭据和项目权限;当前请求按当前项目处理。历史中其他发送者的授权不能沿用到当前请求。当前消息来自" + conv.ChatType + "聊天。" + policy + // activity records Main Bot's text and tool calls so a progress note that + // preceded the tools is not mistaken for the whole reply. + var activity assistantActivityLog generate := h.generate if generate == nil { cliSessionID := newAssistantSessionID() @@ -157,6 +160,7 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not return "", err } raw, nativeID, err := runAssistantCLI(callCtx, assistantCLIRequest{Prompt: prompt, Model: model, SessionID: cliSessionID, Resume: resume, UserID: u.ID, Native: true, MCPConfig: files.MCPConfig, Settings: files.Settings, SystemPrompt: assistantSystemPromptForMode(approval)}, u, func(ev assistantStreamEvent) { + activity.add(ev) switch ev.Kind { case "tool": if strings.Contains(ev.Tool, "submit_assistant_reply") { @@ -190,7 +194,7 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not // from a successful run is accepted: output carrying protocol // JSON (a draft attempt, possibly filtered during parsing) and // progress-only text still go through the terminal retry. - if cand, recovered := recoverNativeAssistantReply(raw, bot.ProjectID); cliErr == nil && recovered && !assistantProtocolJSONPresent(raw) && len(cand.Drafts) == 0 && !assistantProgressOnly(cand.Message, messages) { + if cand, recovered := recoverNativeAssistantReply(raw, bot.ProjectID); cliErr == nil && recovered && !assistantProtocolJSONPresent(raw) && len(cand.Drafts) == 0 && !assistantProgressOnly(assistantProgressText(cand.Message, activity.snapshot()), messages) { final, ok = AgentTerminalResult{Kind: AgentTerminalAssistant, Assistant: &cand}, true } } From 8b293211d3d163bbb973a374ee51399e3ae5c552 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 06:35:33 +0000 Subject: [PATCH 02/28] fix(assistant): keep multi-line plans a progress note, drop internal name from tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A plan written as a numbered or bulleted list (or continued by a "然后…" line) after the last tool call is still a progress note: only a plan opening and plan steps continue it, and an opening that reports work already done ("我先查了…,结论如下。") starts an answer. Restores the continuation the every-line rule removed while keeping the Cursor joined result an answer. The new tests used a real login name and private repo in their fixtures, which check_public.py (core) rejects; they use neutral names now. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 66 +++++++++++++++++-- .../httpapi/assistant_progress_text_test.go | 64 +++++++++++++++++- 2 files changed, 121 insertions(+), 9 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index fff9e9546..019d9395a 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "sort" "strings" "sync" @@ -1135,26 +1136,79 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { return false } } - // Every line must be a plan: an answer that opens with "我先查了…,结论 - // 如下。" and then lists the results is not a progress note. + // A progress note opens with a plan and only continues it: more plan + // sentences, "然后…" steps or the list of steps it is about to take. Any + // other line is an answer, and an opening that reports work already done + // ("我先查了…,结论如下。") is the start of an answer, not a plan. + opened := false for line := range strings.SplitSeq(text, "\n") { - if strings.TrimSpace(line) != "" && !assistantProgressSentence(line) { + line = strings.TrimSpace(line) + if line == "" { + continue + } + if !opened { + if !assistantProgressSentence(line) { + return false + } + opened = true + continue + } + if !assistantProgressSentence(line) && !assistantPlanStep(line) { return false } } - return true + return opened } +// assistantProgressSentence reports whether a line announces what the model +// is about to do. "我先查了…" reports what it did and is not a plan. func assistantProgressSentence(text string) bool { text = strings.TrimSpace(text) for _, prefix := range []string{"稍等", "请稍等", "好的,我先", "我先", "让我先", "接下来我会", "接下来我将", "我会先", "I'll first", "Let me first"} { - if strings.HasPrefix(text, prefix) { - return true + if rest, ok := strings.CutPrefix(text, prefix); ok { + return !assistantReportsDoneWork(rest) } } return false } +// assistantPlanStep reports whether a line after a plan opening continues +// it: a "然后…" step or a list item naming a step still to take. +func assistantPlanStep(line string) bool { + if rest, ok := cutAssistantListMarker(line); ok { + return rest != "" && !assistantReportsDoneWork(rest) + } + for _, prefix := range []string{"然后", "接着", "随后", "之后", "再", "Then", "Next"} { + if rest, ok := strings.CutPrefix(line, prefix); ok { + return !assistantReportsDoneWork(rest) + } + } + return false +} + +var assistantListMarker = regexp.MustCompile(`^(?:[-*•·]|\d{1,2}[.、))]|[((]\d{1,2}[))]|[①-⑳]|[一二三四五六七八九十]{1,3}、)\s*`) + +func cutAssistantListMarker(line string) (string, bool) { + marker := assistantListMarker.FindString(line) + if marker == "" { + return "", false + } + return strings.TrimSpace(line[len(marker):]), true +} + +// assistantDoneWork matches a clause that opens with a short verb carrying the +// perfective 了, as in "查了本项目的代码仓库", "检查了配置", "看了一下". A +// sentence-final 了 further on ("看看为什么测试挂了") is not matched. +var assistantDoneWork = regexp.MustCompile(`^[^,,。.::;;!!\s]{1,3}了`) + +func assistantReportsDoneWork(rest string) bool { + rest = strings.TrimLeft(rest, ",,、 ") + for _, prefix := range []string{"我先", "我会先", "让我先"} { + rest = strings.TrimPrefix(rest, prefix) + } + return assistantDoneWork.MatchString(strings.ReplaceAll(rest, "了解", "理解")) +} + func recoverNativeAssistantReply(raw, projectID string) (assistantReply, bool) { raw = trimAssistantReplyText(raw) if raw == "" { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 783b41e5f..408bddb63 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -17,7 +17,7 @@ import ( // that opened with a plan before calling tools must be judged by the text // written after the tools, as the other CLIs report it. func TestAssistantProgressTextJudgesTextAfterLastToolCall(t *testing.T) { - answer := "仓库:登记 1 个 GitHub 仓库,默认分支 main。\n\n成员:只有负责人 roberttwang。" + answer := "仓库:登记 1 个 GitHub 仓库,默认分支 main。\n\n成员:只有负责人 alice。" plan := "我先查本项目的代码仓库和成员,再分段总结。" cases := []struct { name string @@ -188,6 +188,48 @@ JSON } } +// A plan written as a numbered list before the tools, with nothing after +// them, is still a progress note: the turn continues instead of delivering +// the plan as the answer. +func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { + h, user := workbenchFixture(t) + h.titleSummary = func(context.Context, domain.User, string) (string, error) { return "", nil } + dir := t.TempDir() + counter := filepath.Join(dir, "calls") + bin := filepath.Join(dir, "codebuddy") + script := `#!/bin/sh +n=0 +if [ -f '` + counter + `' ]; then read -r n < '` + counter + `'; fi +n=$((n + 1)) +echo "$n" > '` + counter + `' +if [ "$n" -gt 1 ]; then +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"项目里有四个 LOOP,均在正常运行。"}]}} +{"type":"result","result":"项目里有四个 LOOP,均在正常运行。"} +JSON +exit 0 +fi +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态"}]}} +{"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} +{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} +{"type":"result","result":"我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态"} +JSON +` + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + h.generate = nil + body, _ := json.Marshal(assistantRequest{SessionID: "multi-line-plan", ProjectID: "workbench", Messages: []assistantMessage{{Role: "user", Content: "查看已有 LOOP 并给出结果"}}}) + c, rec := workbenchContext(user, "POST", "/api/assistant/chat", bytes.NewBuffer(body)) + h.chat(c) + calls, _ := os.ReadFile(counter) + if rec.Code != 200 || strings.TrimSpace(string(calls)) != "2" || !strings.Contains(rec.Body.String(), "均在正常运行") { + t.Fatalf("calls=%s code=%d body=%s", strings.TrimSpace(string(calls)), rec.Code, rec.Body.String()) + } +} + // A Bot reply is judged the same way: the joined plan and answer is the // reply, and the CLI is not run again in terminal-only mode for it. func TestBotJoinedResultAfterToolsIsNotRetriedAsProgress(t *testing.T) { @@ -220,18 +262,34 @@ JSON } // A reply is a progress note only when every line is a plan. The first case -// is the text a real Cursor (grok-4.7) turn wrote after its last tool call. +// is the text a real Cursor (grok-4.7) turn wrote after its last tool call, +// with the repository and login renamed. func TestAssistantProgressOnlyRequiresEveryLineToBeAPlan(t *testing.T) { cases := []struct { text string progress bool }{ - {"我先查了本项目的代码仓库和成员,结论如下。\n\n仅 1 个 GitHub 仓:loopforge-sweep-20261127-1200,分支 main。\n\n所有者编号 2,与当前用户 roberttwang 一致。\n\n加入申请 0 条,接口未返回其他成员。", false}, + {"我先查了本项目的代码仓库和成员,结论如下。\n\n仅 1 个 GitHub 仓:demo-repo,分支 main。\n\n所有者编号 2,与当前用户 alice 一致。\n\n加入申请 0 条,接口未返回其他成员。", false}, {"我先查了仓库:\n- 1 个 GitHub 仓库\n- 默认分支 main", false}, + {"我先查了本项目的代码仓库和成员,仓库只有 demo-repo。", false}, + {"稍等,我先查了一下:项目有 4 个 LOOP。", false}, + {"我先查看项目里的 LOOP。\n项目里有 4 个 LOOP,都在运行。", false}, {"我先查看项目里的 LOOP。", true}, {"稍等,我先检查", true}, {"我先看看 store.py。\n接下来我会补测试。", true}, {"我先读代码。\n\n接下来我将运行测试。", true}, + {"我先了解一下需求。", true}, + {"我先看看为什么测试挂了。", true}, + {"我先检查了配置,项目用 MariaDB。", false}, + // Multi-line plans: numbered or bulleted steps and "然后…" lines + // continue the plan. + {"我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态", true}, + {"我先查看项目里的 LOOP。\n1. 读取工作流配置\n2. 汇总节点", true}, + {"我先检查一下当前仓库。\n- 读取配置文件\n- 运行测试", true}, + {"稍等,我先确认环境。\n然后读取 store.py。", true}, + {"Let me first check the repository:\n1) read the config\n2) run the tests", true}, + {"我先按下面的步骤处理:\n① 读取 README\n② 汇总结论", true}, + {"我先查看仓库:\n- 读取配置\n- 查了 README,项目用 Go 写成", false}, {"", false}, {" \n\n ", false}, } From 9925a5b27a3a9332a41bc4a906da97481bc1ee1e Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 08:14:29 +0000 Subject: [PATCH 03/28] fix(assistant): treat prose plan steps as progress, not an answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A plan written as prose ("先确认分支,再读取提交记录。") after the opening line was not recognised as a continuation, so the turn ended with the plan stored as its answer. A further line is now a plan line when it opens with a plan lead-in or names a step to take and carries no result of work already done (perfective 了, counts, dates, 结论/结果…). The unit test is renamed to TestAssistantProgressOnlyRequiresPlanLines, and the end-to-end case now covers both a numbered list and prose steps. --- internal/httpapi/assistant.go | 73 ++++++++++++++++++- .../httpapi/assistant_progress_text_test.go | 55 +++++++++++--- 2 files changed, 113 insertions(+), 15 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index fe15a98e7..1295bb333 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1174,9 +1174,11 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } } // A progress note opens with a plan and only continues it: more plan - // sentences, "然后…" steps or the list of steps it is about to take. Any - // other line is an answer, and an opening that reports work already done - // ("我先查了…,结论如下。") is the start of an answer, not a plan. + // sentences, "然后…" steps, the list of steps it is about to take, or + // plain prose steps ("先确认分支,再读取提交记录。"). A line carrying a + // result (perfective 了, counts, dates, 结论/结果…) is an answer, and an + // opening that reports work already done ("我先查了…,结论如下。") is the + // start of an answer, not a plan. opened := false for line := range strings.SplitSeq(text, "\n") { line = strings.TrimSpace(line) @@ -1190,7 +1192,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { opened = true continue } - if !assistantProgressSentence(line) && !assistantPlanStep(line) { + if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) { return false } } @@ -1225,6 +1227,69 @@ func assistantPlanStep(line string) bool { var assistantListMarker = regexp.MustCompile(`^(?:[-*•·]|\d{1,2}[.、))]|[((]\d{1,2}[))]|[①-⑳]|[一二三四五六七八九十]{1,3}、)\s*`) +// assistantPlanLeadIn opens a line that names a step of the plan in prose +// rather than as a list item, as in "首先读取配置文件和测试。" or +// "检查范围:配置文件和测试。". +var assistantPlanLeadIn = []string{"首先", "先", "第一步", "第二步", "第三步", "最后", "具体步骤", "步骤", "计划", "检查范围", "检查", "目标", "范围", "First", "first", "Step"} + +// assistantPlanVerb is an action a plan step is made of, without the +// perfective 了 ("先确认分支" plans the check; "先确认了分支" reports it). +var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现", "review", "read", "check", "run", "test"} + +// assistantPlanResultMark is a word a plan line does not carry: it reports +// what was found (conclusions) instead of what is next. +var assistantPlanResultMark = []string{"结论", "结果", "如下", "共", "只有", "一致", "返回", "显示", "分别", "目前", "已经", "完成", "失败", "正常"} + +// assistantPlanCJKNumber matches counts written as Chinese numerals, as in +// "项目里有两个 LOOP". +var assistantPlanCJKNumber = regexp.MustCompile(`[一二两三四五六七八九十][个条位项次]`) + +// assistantPlanDigitsOrDate matches numbers, dates and versions. +var assistantPlanDigitsOrDate = regexp.MustCompile(`\d|月|日|号`) + +// assistantPlanPerfective matches the perfective 了 that reports a step as +// done. 了解/除了/为了 are plain words, not completions. +var assistantPlanPerfective = regexp.MustCompile(`了`) + +func assistantPlanHasPerfective(line string) bool { + for _, word := range []string{"了解", "除了", "为了"} { + line = strings.ReplaceAll(line, word, "") + } + return assistantPlanPerfective.MatchString(line) +} + +// assistantPlanProse reports whether a line after a plan opening continues +// the plan in prose: it opens with a plan lead-in or names a step to take, +// and it carries no result of work already done. +func assistantPlanProse(line string) bool { + line = strings.TrimSpace(line) + if line == "" { + return false + } + for _, mark := range assistantPlanResultMark { + if strings.Contains(line, mark) { + return false + } + } + if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) || assistantPlanHasPerfective(line) { + return false + } + if assistantReportsDoneWork(line) { + return false + } + for _, lead := range assistantPlanLeadIn { + if strings.HasPrefix(line, lead) { + return true + } + } + for _, verb := range assistantPlanVerb { + if strings.Contains(line, verb) { + return true + } + } + return false +} + func cutAssistantListMarker(line string) (string, bool) { marker := assistantListMarker.FindString(line) if marker == "" { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 408bddb63..3923bc358 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "os" "path/filepath" "strings" @@ -188,10 +189,25 @@ JSON } } -// A plan written as a numbered list before the tools, with nothing after -// them, is still a progress note: the turn continues instead of delivering -// the plan as the answer. +// A plan written before the tools, with nothing after them, is still a +// progress note: the turn continues instead of delivering the plan as the +// answer. The plan may be a numbered list or plain prose +// ("先确认分支,再读取提交记录。"). func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { + for _, tc := range []struct { + name, session, plan string + }{ + {"numbered list", "multi-line-plan", `我先查看项目里的 LOOP:\\n1. 读取工作流配置\\n2. 汇总节点状态`}, + {"prose steps", "prose-plan", `我先看一下仓库:\\n先确认分支,再读取提交记录。`}, + } { + t.Run(tc.name, func(t *testing.T) { + runMultiLinePlanCase(t, tc.session, tc.plan) + }) + } +} + +func runMultiLinePlanCase(t *testing.T, session, plan string) { + t.Helper() h, user := workbenchFixture(t) h.titleSummary = func(context.Context, domain.User, string) (string, error) { return "", nil } dir := t.TempDir() @@ -210,18 +226,18 @@ JSON exit 0 fi cat <<'JSON' -{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态"}]}} +{"type":"assistant","message":{"content":[{"type":"text","text":"%[1]s"}]}} {"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} {"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} -{"type":"result","result":"我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态"} +{"type":"result","result":"%[1]s"} JSON ` - if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + if err := os.WriteFile(bin, []byte(fmt.Sprintf(script, plan)), 0o700); err != nil { t.Fatal(err) } t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) h.generate = nil - body, _ := json.Marshal(assistantRequest{SessionID: "multi-line-plan", ProjectID: "workbench", Messages: []assistantMessage{{Role: "user", Content: "查看已有 LOOP 并给出结果"}}}) + body, _ := json.Marshal(assistantRequest{SessionID: session, ProjectID: "workbench", Messages: []assistantMessage{{Role: "user", Content: "查看已有 LOOP 并给出结果"}}}) c, rec := workbenchContext(user, "POST", "/api/assistant/chat", bytes.NewBuffer(body)) h.chat(c) calls, _ := os.ReadFile(counter) @@ -261,10 +277,12 @@ JSON } } -// A reply is a progress note only when every line is a plan. The first case -// is the text a real Cursor (grok-4.7) turn wrote after its last tool call, -// with the repository and login renamed. -func TestAssistantProgressOnlyRequiresEveryLineToBeAPlan(t *testing.T) { +// A reply is a progress note when it opens with a plan and every further line +// only continues that plan, in a list or in prose; a line that carries a +// result of work already done makes the reply an answer. The first case is +// the text a real Cursor (grok-4.7) turn wrote after its last tool call, with +// the repository and login renamed. +func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { cases := []struct { text string progress bool @@ -290,6 +308,21 @@ func TestAssistantProgressOnlyRequiresEveryLineToBeAPlan(t *testing.T) { {"Let me first check the repository:\n1) read the config\n2) run the tests", true}, {"我先按下面的步骤处理:\n① 读取 README\n② 汇总结论", true}, {"我先查看仓库:\n- 读取配置\n- 查了 README,项目用 Go 写成", false}, + // Prose steps that neither open with a whitelisted prefix nor use a + // list: "先确认分支,再读取提交记录。". + {"我先看一下仓库:\n先确认分支,再读取提交记录。", true}, + {"我先看一下仓库。\n首先读取配置文件,然后运行测试。", true}, + {"我先看一下仓库。\n先读取配置文件,再运行测试。", true}, + {"我先看一下仓库。\n具体步骤:读取配置、运行测试。", true}, + {"我先看一下仓库。\n第一步:读取配置文件。", true}, + {"我先看一下仓库。\n检查范围:配置文件和测试。", true}, + {"我先看一下仓库。\n检查范围:配置文件和测试。\n先运行测试,再读取配置。", true}, + // A prose line that reports a result is an answer, not a step. + {"我先看一下仓库。\n仓库默认分支是 main,最近提交在 11 月 26 日。", false}, + {"我先看一下仓库。\n项目里有 4 个 LOOP,均正常运行。", false}, + {"我先看一下仓库。\n共有 2 个成员:alice、bob。", false}, + {"我先看一下仓库。\n先确认分支,再读取提交记录。\n最近提交是 abc1234。", false}, + {"我先看一下仓库。\n先看看仓库,查了 README。", false}, {"", false}, {" \n\n ", false}, } From d6b89cbbd9e897165742ed78df03c203cbe2d886 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 09:30:33 +0000 Subject: [PATCH 04/28] fix(assistant): treat a stated verdict after a plan opening as the answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 9925a5b2 counted any line naming an action as a plan step, so a reply like "我先跑测试。\n测试全过。" or "Let me first check the file.\nIt reads the config on start." was retried as a progress note and could end in a 502 after two retries. A line stating how things are (全过/通过/没问题/干净…) is now an answer unless it also says the state is still to be checked (确认/看看/是否…), for prose lines, list items and 然后 steps alike, and English plan verbs match whole words only. The end-to-end plan cases now send real newlines, so they exercise multi-line plans, and a verdict case checks that the answer is delivered without another CLI run. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 39 ++++++++++++++++--- .../httpapi/assistant_progress_text_test.go | 30 +++++++++++--- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 1295bb333..7389c2b27 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1215,11 +1215,11 @@ func assistantProgressSentence(text string) bool { // it: a "然后…" step or a list item naming a step still to take. func assistantPlanStep(line string) bool { if rest, ok := cutAssistantListMarker(line); ok { - return rest != "" && !assistantReportsDoneWork(rest) + return rest != "" && !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) } for _, prefix := range []string{"然后", "接着", "随后", "之后", "再", "Then", "Next"} { if rest, ok := strings.CutPrefix(line, prefix); ok { - return !assistantReportsDoneWork(rest) + return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) } } return false @@ -1234,7 +1234,36 @@ var assistantPlanLeadIn = []string{"首先", "先", "第一步", "第二步", " // assistantPlanVerb is an action a plan step is made of, without the // perfective 了 ("先确认分支" plans the check; "先确认了分支" reports it). -var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现", "review", "read", "check", "run", "test"} +var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现"} + +// assistantPlanVerbEN is the English counterpart of assistantPlanVerb, matched +// as whole words: "It reads the config on start." describes the code, it does +// not plan a read. +var assistantPlanVerbEN = regexp.MustCompile(`(?i)\b(?:review|read|check|run|test)\b`) + +// assistantPlanVerdictMark states how things are ("测试全过", "接口没问题"). +// A plan names such a state only as something still to verify, so a line +// carrying it without a check intent is an answer. +var assistantPlanVerdictMark = []string{"全过", "都过", "通过", "没问题", "没有问题", "无异常", "没有异常", "干净"} + +// assistantPlanCheckIntent marks a verdict as still to be verified, as in +// "确认测试都通过" or "看看有没有异常". +var assistantPlanCheckIntent = []string{"确认", "检查", "验证", "看看", "看下", "看一下", "是否", "有没有", "能否", "会不会"} + +func assistantPlanStatesVerdict(line string) bool { + for _, mark := range assistantPlanVerdictMark { + if !strings.Contains(line, mark) { + continue + } + for _, intent := range assistantPlanCheckIntent { + if strings.Contains(line, intent) { + return false + } + } + return true + } + return false +} // assistantPlanResultMark is a word a plan line does not carry: it reports // what was found (conclusions) instead of what is next. @@ -1274,7 +1303,7 @@ func assistantPlanProse(line string) bool { if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) || assistantPlanHasPerfective(line) { return false } - if assistantReportsDoneWork(line) { + if assistantReportsDoneWork(line) || assistantPlanStatesVerdict(line) { return false } for _, lead := range assistantPlanLeadIn { @@ -1287,7 +1316,7 @@ func assistantPlanProse(line string) bool { return true } } - return false + return assistantPlanVerbEN.MatchString(line) } func cutAssistantListMarker(line string) (string, bool) { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 3923bc358..c4370d9a7 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -197,16 +197,22 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { for _, tc := range []struct { name, session, plan string }{ - {"numbered list", "multi-line-plan", `我先查看项目里的 LOOP:\\n1. 读取工作流配置\\n2. 汇总节点状态`}, - {"prose steps", "prose-plan", `我先看一下仓库:\\n先确认分支,再读取提交记录。`}, + {"numbered list", "multi-line-plan", `我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态`}, + {"prose steps", "prose-plan", `我先看一下仓库:\n先确认分支,再读取提交记录。`}, } { t.Run(tc.name, func(t *testing.T) { - runMultiLinePlanCase(t, tc.session, tc.plan) + runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") }) } } -func runMultiLinePlanCase(t *testing.T, session, plan string) { +// A reply that opens like a plan but states a verdict ("测试全过。") is the +// answer: it is delivered as is, without running the CLI again. +func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { + runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") +} + +func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { t.Helper() h, user := workbenchFixture(t) h.titleSummary = func(context.Context, domain.User, string) (string, error) { return "", nil } @@ -241,7 +247,7 @@ JSON c, rec := workbenchContext(user, "POST", "/api/assistant/chat", bytes.NewBuffer(body)) h.chat(c) calls, _ := os.ReadFile(counter) - if rec.Code != 200 || strings.TrimSpace(string(calls)) != "2" || !strings.Contains(rec.Body.String(), "均在正常运行") { + if rec.Code != 200 || strings.TrimSpace(string(calls)) != wantCalls || !strings.Contains(rec.Body.String(), wantBody) { t.Fatalf("calls=%s code=%d body=%s", strings.TrimSpace(string(calls)), rec.Code, rec.Body.String()) } } @@ -323,6 +329,20 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下仓库。\n共有 2 个成员:alice、bob。", false}, {"我先看一下仓库。\n先确认分支,再读取提交记录。\n最近提交是 abc1234。", false}, {"我先看一下仓库。\n先看看仓库,查了 README。", false}, + // A line stating a verdict is an answer unless it is still to be + // checked; English verbs count only as whole words. + {"我先跑测试。\n测试全过。", false}, + {"我先跑测试。\n单元测试都通过,lint 也干净。", false}, + {"我先看下日志。\n接口没问题。", false}, + {"我先跑测试。\n- 单元测试全过\n- lint 干净", false}, + {"我先跑测试。\n然后测试全过。", false}, + {"Let me first check the file.\nIt reads the config on start.", false}, + {"我先检查配置。\n跑一遍测试看看有没有异常。", true}, + {"我先跑测试。\n确认单元测试都通过。", true}, + {"我先跑测试。\n先运行测试,再检查 lint 是否干净。", true}, + {"我先跑测试。\n- 确认测试全部通过\n- 看看日志有没有问题", true}, + {"Let me first check the file.\nThen read the config and run the tests.", true}, + {"Let me first look around.\nCheck the config, then run the tests.", true}, {"", false}, {" \n\n ", false}, } From d3b9d85136c21a80598a9c47ae08a133a43d427a Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 10:09:51 +0000 Subject: [PATCH 05/28] fix(assistant): read verdict words in context when judging plan lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit d6b89cb matched 通过/干净 as bare substrings, so plan steps such as "再通过 API 拉取提交记录" or "再把临时文件清理干净" were taken as a stated verdict and the plan was stored as the final reply. 通过 now counts as a verdict only after a check subject (测试/检查/审核/ 校验/构建/CI/门禁/lint) or at the end of a clause; 干净 does not count when a cleanup verb comes before it. A plan lead-in (先…再…/然后/首先/ 重点关注 …) now wins over numbers and result words, and action verbs only count before the first result mark, so "先查看最近的 3 次提交,再汇总变更" is retried as progress while "测试全过" / "共 3 个失败" stay answers. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 112 ++++++++++++++---- .../httpapi/assistant_progress_text_test.go | 12 ++ 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 7389c2b27..6b2663a7d 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1234,15 +1234,15 @@ var assistantPlanLeadIn = []string{"首先", "先", "第一步", "第二步", " // assistantPlanVerb is an action a plan step is made of, without the // perfective 了 ("先确认分支" plans the check; "先确认了分支" reports it). -var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现"} +var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现", "关注", "核对", "排查", "梳理"} // assistantPlanVerbEN is the English counterpart of assistantPlanVerb, matched // as whole words: "It reads the config on start." describes the code, it does // not plan a read. var assistantPlanVerbEN = regexp.MustCompile(`(?i)\b(?:review|read|check|run|test)\b`) -// assistantPlanVerdictMark states how things are ("测试全过", "接口没问题"). -// A plan names such a state only as something still to verify, so a line +// assistantPlanVerdictMark states how things are ("测试全过", "接口没问题", "lint 干净"). +// A plan names such a state only as something still to be verified, so a line // carrying it without a check intent is an answer. var assistantPlanVerdictMark = []string{"全过", "都过", "通过", "没问题", "没有问题", "无异常", "没有异常", "干净"} @@ -1250,21 +1250,78 @@ var assistantPlanVerdictMark = []string{"全过", "都过", "通过", "没问题 // "确认测试都通过" or "看看有没有异常". var assistantPlanCheckIntent = []string{"确认", "检查", "验证", "看看", "看下", "看一下", "是否", "有没有", "能否", "会不会"} +// assistantPlanPassSubject is what 通过 can be a verdict about. "再通过 API +// 拉取提交记录" uses 通过 as "via" and stays a step. +var assistantPlanPassSubject = []string{"测试", "单测", "用例", "检查", "审核", "校验", "构建", "CI", "ci", "门禁", "lint"} + +// assistantPlanCleanupVerb precedes 干净 in a step ("把临时目录清理干净") +// rather than describing a state ("lint 也干净"). +var assistantPlanCleanupVerb = []string{"清理", "清空", "清除", "删除", "删掉", "擦", "洗", "扫", "收拾"} + func assistantPlanStatesVerdict(line string) bool { - for _, mark := range assistantPlanVerdictMark { - if !strings.Contains(line, mark) { - continue - } - for _, intent := range assistantPlanCheckIntent { - if strings.Contains(line, intent) { + if strings.Contains(line, "通过") { + return assistantPlanPassVerdict(line) && !assistantPlanCheckIntentIn(line) + } + if i := strings.Index(line, "干净"); i >= 0 && !assistantPlanCheckIntentIn(line) { + for _, verb := range assistantPlanCleanupVerb { + if strings.Contains(line[:i], verb) { return false } } return true } + if strings.Contains(line, "干净") { + return false + } + if assistantPlanCheckIntentIn(line) { + return false + } + for _, mark := range assistantPlanVerdictMark { + if strings.Contains(line, mark) { + return true + } + } return false } +func assistantPlanCheckIntentIn(line string) bool { + for _, intent := range assistantPlanCheckIntent { + if strings.Contains(line, intent) { + return true + } + } + return false +} + +// assistantPlanPassVerdict reports whether 通过 is a verdict: it follows a +// subject it passes ("测试通过", "单测都通过") or closes a clause +// ("测试都通过,可以合并。"). +func assistantPlanPassVerdict(line string) bool { + for i := 0; ; { + j := strings.Index(line[i:], "通过") + if j < 0 { + return false + } + j += i + before := line[:j] + for _, skip := range []string{"全部", "都", "均", "也", "已经", "已"} { + for strings.HasSuffix(before, skip) { + before = strings.TrimSuffix(before, skip) + } + } + for _, subject := range assistantPlanPassSubject { + if strings.HasSuffix(before, subject) { + return true + } + } + rest := strings.TrimSpace(line[j+len("通过"):]) + if rest == "" || strings.ContainsAny(rest[:1], ",,。.;;!!、") { + return true + } + i = j + len("通过") + } +} + // assistantPlanResultMark is a word a plan line does not carry: it reports // what was found (conclusions) instead of what is next. var assistantPlanResultMark = []string{"结论", "结果", "如下", "共", "只有", "一致", "返回", "显示", "分别", "目前", "已经", "完成", "失败", "正常"} @@ -1295,28 +1352,43 @@ func assistantPlanProse(line string) bool { if line == "" { return false } - for _, mark := range assistantPlanResultMark { - if strings.Contains(line, mark) { - return false - } - } - if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) || assistantPlanHasPerfective(line) { - return false - } - if assistantReportsDoneWork(line) || assistantPlanStatesVerdict(line) { + if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) { return false } + // A step opens with a plan lead-in ("先确认服务是否正常") even though the + // line also names a state or a count. for _, lead := range assistantPlanLeadIn { if strings.HasPrefix(line, lead) { return true } } + // A count makes a line a report unless it opened with a plan lead-in + // ("先查看最近的 3 次提交"), as in "项目里有 4 个 LOOP,都在运行。". + if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) { + return false + } + // An action word names a step only before any result it might be + // reporting ("接口看着正常" reports the state 正常). + limit := len(line) + for _, mark := range assistantPlanResultMark { + if i := strings.Index(line, mark); i >= 0 && i < limit { + limit = i + } + } for _, verb := range assistantPlanVerb { - if strings.Contains(line, verb) { + if strings.Contains(line[:limit], verb) { return true } } - return assistantPlanVerbEN.MatchString(line) + if assistantPlanVerbEN.MatchString(line[:limit]) { + return true + } + for _, mark := range assistantPlanResultMark { + if strings.Contains(line, mark) { + return false + } + } + return false } func cutAssistantListMarker(line string) (string, bool) { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index c4370d9a7..e04ab90a0 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -342,6 +342,18 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先跑测试。\n先运行测试,再检查 lint 是否干净。", true}, {"我先跑测试。\n- 确认测试全部通过\n- 看看日志有没有问题", true}, {"Let me first check the file.\nThen read the config and run the tests.", true}, + // 通过 as "via" and 干净 as a complement are steps, not verdicts; + // plan lead-ins still open a step that names a state or a count. + {"我先看一下仓库:\n先读取配置,再通过 API 拉取提交记录。", true}, + {"稍等,我先确认环境。\n然后通过数据库查看运行记录。", true}, + {"我先整理一下工作区。\n先删掉临时文件,再清理干净,最后运行测试。", true}, + {"我先看一下仓库。\n先读取 README,再把临时文件清理干净。", true}, + {"让我先看一下仓库:\n先把测试目录清理干净,再运行测试。", true}, + {"我先看一下环境。\n先确认服务是否正常,再读取日志。", true}, + {"我先看一下仓库:\n先查看最近的 3 次提交,再汇总变更。", true}, + {"我先看一下仓库。\n重点关注 store.py 和 handler 的调用关系。", true}, + {"我先跑测试。\n测试都通过,可以合并。", false}, + {"我先跑测试。\n单测都通过。", false}, {"Let me first look around.\nCheck the config, then run the tests.", true}, {"", false}, {" \n\n ", false}, From 156a960eed7261cb3396da1a5e699aedee734429 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 10:15:02 +0000 Subject: [PATCH 06/28] fix(assistant): keep a verdict's own subject out of the check intent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "检查通过,工作区干净。" names 检查 as the subject that passed, not as a plan to check, so it is an answer. Look for a check intent only outside the verdict itself: "确认检查通过" still plans the verification. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 19 ++++++++++++------- .../httpapi/assistant_progress_text_test.go | 5 +++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 6b2663a7d..5c4777063 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1260,7 +1260,10 @@ var assistantPlanCleanupVerb = []string{"清理", "清空", "清除", "删除", func assistantPlanStatesVerdict(line string) bool { if strings.Contains(line, "通过") { - return assistantPlanPassVerdict(line) && !assistantPlanCheckIntentIn(line) + // The verdict's own subject is not a check intent: "检查通过" states + // the result, "确认检查通过" still plans to verify it. + rest, ok := assistantPlanPassVerdict(line) + return ok && !assistantPlanCheckIntentIn(rest) } if i := strings.Index(line, "干净"); i >= 0 && !assistantPlanCheckIntentIn(line) { for _, verb := range assistantPlanCleanupVerb { @@ -1295,12 +1298,13 @@ func assistantPlanCheckIntentIn(line string) bool { // assistantPlanPassVerdict reports whether 通过 is a verdict: it follows a // subject it passes ("测试通过", "单测都通过") or closes a clause -// ("测试都通过,可以合并。"). -func assistantPlanPassVerdict(line string) bool { +// ("测试都通过,可以合并。"). It also returns the line without that verdict +// so the caller can look for a check intent around it. +func assistantPlanPassVerdict(line string) (string, bool) { for i := 0; ; { j := strings.Index(line[i:], "通过") if j < 0 { - return false + return line, false } j += i before := line[:j] @@ -1309,14 +1313,15 @@ func assistantPlanPassVerdict(line string) bool { before = strings.TrimSuffix(before, skip) } } + after := line[j+len("通过"):] for _, subject := range assistantPlanPassSubject { if strings.HasSuffix(before, subject) { - return true + return strings.TrimSuffix(before, subject) + after, true } } - rest := strings.TrimSpace(line[j+len("通过"):]) + rest := strings.TrimSpace(after) if rest == "" || strings.ContainsAny(rest[:1], ",,。.;;!!、") { - return true + return line[:j] + after, true } i = j + len("通过") } diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index e04ab90a0..b67541acb 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -354,6 +354,11 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下仓库。\n重点关注 store.py 和 handler 的调用关系。", true}, {"我先跑测试。\n测试都通过,可以合并。", false}, {"我先跑测试。\n单测都通过。", false}, + // The verdict's subject is not a check intent of its own. + {"我先看一下仓库。\n检查通过,工作区干净。", false}, + {"我先看一下仓库。\n校验通过。", false}, + {"我先看一下仓库。\n确认检查通过。", true}, + {"我先看一下仓库。\n检查测试是否通过。", true}, {"Let me first look around.\nCheck the config, then run the tests.", true}, {"", false}, {" \n\n ", false}, From 6750dbde919f8de4cf05479970c2a0ed87b5e911 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 11:22:36 +0000 Subject: [PATCH 07/28] fix(assistant): let a plan lead-in open a step only when an action precedes its result MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 156a960 made a plan lead-in (先/首先/最后/目标/范围/计划/检查…) win over result words and counts so "先确认服务是否正常" and "先查看最近的 3 次提交" stay plan steps. It also turned answers that merely start with one of those words into plan lines: "检查结果:共 3 处问题", "最后结论:可以合并。", "目标分支是 main,共 3 个提交". After a "我先…" opening such a reply was judged progress-only, retried twice, and either failed with 502 or stored the answer three times (reproduced with Cursor grok-4.7). A lead-in line now counts as a step only if it carries no result or count, or an action word appears before the first one. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 45 ++++++++++++------- .../httpapi/assistant_progress_text_test.go | 8 ++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 5c4777063..7cbd3daeb 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1360,40 +1360,53 @@ func assistantPlanProse(line string) bool { if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) { return false } - // A step opens with a plan lead-in ("先确认服务是否正常") even though the - // line also names a state or a count. + // A plan lead-in opens a step even when the line goes on to name a state + // or a count ("先确认服务是否正常", "先查看最近的 3 次提交"), as long as an + // action comes before it. "检查结果:共 3 处问题" and "最后结论:可以合并。" + // open with a lead-in but report what was found. for _, lead := range assistantPlanLeadIn { - if strings.HasPrefix(line, lead) { - return true + if rest, ok := strings.CutPrefix(line, lead); ok { + limit := assistantPlanResultStart(rest, true) + return limit == len(rest) || assistantPlanNamesAction(rest[:limit]) } } - // A count makes a line a report unless it opened with a plan lead-in - // ("先查看最近的 3 次提交"), as in "项目里有 4 个 LOOP,都在运行。". + // A count makes a line a report, as in "项目里有 4 个 LOOP,都在运行。". if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) { return false } // An action word names a step only before any result it might be // reporting ("接口看着正常" reports the state 正常). + return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) +} + +// assistantPlanResultStart returns where the first result word of line +// starts, or len(line) if it has none. With counts set, a count or date +// ("3 次", "两个") also starts a result. +func assistantPlanResultStart(line string, counts bool) int { limit := len(line) for _, mark := range assistantPlanResultMark { if i := strings.Index(line, mark); i >= 0 && i < limit { limit = i } } - for _, verb := range assistantPlanVerb { - if strings.Contains(line[:limit], verb) { - return true + if counts { + for _, re := range []*regexp.Regexp{assistantPlanCJKNumber, assistantPlanDigitsOrDate} { + if loc := re.FindStringIndex(line); loc != nil && loc[0] < limit { + limit = loc[0] + } } } - if assistantPlanVerbEN.MatchString(line[:limit]) { - return true - } - for _, mark := range assistantPlanResultMark { - if strings.Contains(line, mark) { - return false + return limit +} + +// assistantPlanNamesAction reports whether text names a step to take. +func assistantPlanNamesAction(text string) bool { + for _, verb := range assistantPlanVerb { + if strings.Contains(text, verb) { + return true } } - return false + return assistantPlanVerbEN.MatchString(text) } func cutAssistantListMarker(line string) (string, bool) { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index b67541acb..b027f3151 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -352,6 +352,14 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下环境。\n先确认服务是否正常,再读取日志。", true}, {"我先看一下仓库:\n先查看最近的 3 次提交,再汇总变更。", true}, {"我先看一下仓库。\n重点关注 store.py 和 handler 的调用关系。", true}, + // A lead-in followed by a result before any action reports findings. + {"我先看下 PR 的改动。\n检查结果:共 3 处问题,都在 store.py。", false}, + {"我先看一下。\n目标分支是 main,共 3 个提交,最近一次是修复登录。", false}, + {"我先汇总一下。\n最后结论:可以合并。", false}, + {"我先说下范围。\n范围只有 internal/httpapi 一个包。", false}, + {"我先看下日志。\n计划任务每天 9 点跑,最近一次成功。", false}, + {"我先看一下仓库。\n最后汇总结果。", true}, + {"我先看一下仓库。\n目标:把登录修好。", true}, {"我先跑测试。\n测试都通过,可以合并。", false}, {"我先跑测试。\n单测都通过。", false}, // The verdict's subject is not a check intent of its own. From 12e92cc590329dce8a91dabdf7043de8ef4783a5 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 12:24:30 +0000 Subject: [PATCH 08/28] fix(assistant): keep plan steps with counts, goals and "via" checks a progress note MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crispinhu27's review of 156a960e: after a plan opening, these steps were taken as the answer and stored instead of continuing the turn: - a step with a count or version before which an action is named ("重点关注最近 3 次提交", "查看两个小组的成员", "读一下 v2 的配置"); a count now only closes the action window, as it already did after a lead-in, so "项目里有 4 个 LOOP,都在运行。" stays an answer - goal clauses ("让测试通过", "确保 lint 干净", "保证构建通过", "直到全部通过") read as verdicts; 让/确保/保证/直到 now mark a goal - 检查/审核/校验 before a "via" 通过 ("检查通过 API 拿到的数据") were taken as the verdict's subject; they are now the subject only when 通过 closes the clause. The clause-end test also decoded only one byte, so full-width punctuation after 通过 never matched - 了 inside an embedded question ("看看为什么测试挂了", "看看改了哪些文件") read as reported work - 目前 and the noun 返回值 counted as result words Answers stay answers: report headers ("检查结果:", "运行结果:", "结论:") and completive 到 ("查到 3 个失败用例", "读取到的配置是 v2") now mark a result explicitly. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 139 +++++++++++++++--- .../httpapi/assistant_progress_text_test.go | 51 +++++++ 2 files changed, 172 insertions(+), 18 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 7cbd3daeb..94cb70a8c 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -16,6 +16,7 @@ import ( "sync" "sync/atomic" "time" + "unicode/utf8" "agentflow/internal/application" "agentflow/internal/domain" @@ -1234,7 +1235,9 @@ var assistantPlanLeadIn = []string{"首先", "先", "第一步", "第二步", " // assistantPlanVerb is an action a plan step is made of, without the // perfective 了 ("先确认分支" plans the check; "先确认了分支" reports it). -var assistantPlanVerb = []string{"读", "看", "查", "搜", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "测试", "验证", "修改", "实现", "记录", "列出", "定位", "复现", "关注", "核对", "排查", "梳理"} +// 测试 is left out: as a noun it opens reports ("测试失败 3 个", "测试挂在 +// 网络超时"), and a step that runs tests names 跑/运行 as well. +var assistantPlanVerb = []string{"读", "看", "查", "搜", "跑", "运行", "执行", "检查", "确认", "汇总", "整理", "对比", "分析", "验证", "修改", "实现", "记录", "列出", "定位", "复现", "关注", "核对", "排查", "梳理"} // assistantPlanVerbEN is the English counterpart of assistantPlanVerb, matched // as whole words: "It reads the config on start." describes the code, it does @@ -1246,13 +1249,20 @@ var assistantPlanVerbEN = regexp.MustCompile(`(?i)\b(?:review|read|check|run|tes // carrying it without a check intent is an answer. var assistantPlanVerdictMark = []string{"全过", "都过", "通过", "没问题", "没有问题", "无异常", "没有异常", "干净"} -// assistantPlanCheckIntent marks a verdict as still to be verified, as in -// "确认测试都通过" or "看看有没有异常". -var assistantPlanCheckIntent = []string{"确认", "检查", "验证", "看看", "看下", "看一下", "是否", "有没有", "能否", "会不会"} +// assistantPlanCheckIntent marks a verdict as still to be verified or +// reached, as in "确认测试都通过", "看看有没有异常", "改代码,让测试通过" or +// "跑测试直到全部通过". +var assistantPlanCheckIntent = []string{"确认", "检查", "验证", "看看", "看下", "看一下", "是否", "有没有", "能否", "会不会", "让", "确保", "保证", "直到", "使得", "使其"} // assistantPlanPassSubject is what 通过 can be a verdict about. "再通过 API // 拉取提交记录" uses 通过 as "via" and stays a step. -var assistantPlanPassSubject = []string{"测试", "单测", "用例", "检查", "审核", "校验", "构建", "CI", "ci", "门禁", "lint"} +var assistantPlanPassSubject = []string{"测试", "单测", "用例", "构建", "CI", "ci", "门禁", "lint"} + +// assistantPlanPassActSubject is a check that can also be the verb before a +// "via" 通过: "检查通过,工作区干净。" states the verdict, "检查通过 API 拿到 +// 的数据" plans a check. It is the verdict's subject only when 通过 closes +// the clause. +var assistantPlanPassActSubject = []string{"检查", "审核", "校验"} // assistantPlanCleanupVerb precedes 干净 in a step ("把临时目录清理干净") // rather than describing a state ("lint 也干净"). @@ -1314,13 +1324,20 @@ func assistantPlanPassVerdict(line string) (string, bool) { } } after := line[j+len("通过"):] + rest := strings.TrimSpace(after) + next, _ := utf8.DecodeRuneInString(rest) + closes := rest == "" || strings.ContainsRune(",,。.;;!!、", next) for _, subject := range assistantPlanPassSubject { if strings.HasSuffix(before, subject) { return strings.TrimSuffix(before, subject) + after, true } } - rest := strings.TrimSpace(after) - if rest == "" || strings.ContainsAny(rest[:1], ",,。.;;!!、") { + for _, subject := range assistantPlanPassActSubject { + if closes && strings.HasSuffix(before, subject) { + return strings.TrimSuffix(before, subject) + after, true + } + } + if closes { return line[:j] + after, true } i = j + len("通过") @@ -1329,26 +1346,61 @@ func assistantPlanPassVerdict(line string) (string, bool) { // assistantPlanResultMark is a word a plan line does not carry: it reports // what was found (conclusions) instead of what is next. -var assistantPlanResultMark = []string{"结论", "结果", "如下", "共", "只有", "一致", "返回", "显示", "分别", "目前", "已经", "完成", "失败", "正常"} +var assistantPlanResultMark = []string{"结论", "结果", "如下", "共", "只有", "一致", "返回", "显示", "分别", "已经", "完成", "失败", "正常"} + +// assistantPlanResultNoun is a noun that contains a result mark without +// reporting anything ("把返回值和文档核对" plans a check). +var assistantPlanResultNoun = []string{"返回值"} + +// assistantPlanFound matches a verb with the completive 到 that reports +// what a step turned up, as in "查到 3 个失败用例", "找到两个仓库" or +// "读取到的配置是 v2". +var assistantPlanFound = regexp.MustCompile(`\p{Han}?(?:查|找|看|搜|拿|读|取|收|得)到`) + +// assistantPlanReportHeader opens a report under a header such as +// "检查结果:" or "最后结论:", which no plan step uses. +var assistantPlanReportHeader = regexp.MustCompile(`(?:结论|结果|如下)\s*[::]`) // assistantPlanCJKNumber matches counts written as Chinese numerals, as in // "项目里有两个 LOOP". var assistantPlanCJKNumber = regexp.MustCompile(`[一二两三四五六七八九十][个条位项次]`) // assistantPlanDigitsOrDate matches numbers, dates and versions. -var assistantPlanDigitsOrDate = regexp.MustCompile(`\d|月|日|号`) +// 月/日/号 count only after a numeral: "日志" and "号码" are plain words. +var assistantPlanDigitsOrDate = regexp.MustCompile(`\d|[一二三四五六七八九十][月日号]`) // assistantPlanPerfective matches the perfective 了 that reports a step as // done. 了解/除了/为了 are plain words, not completions. var assistantPlanPerfective = regexp.MustCompile(`了`) +// assistantPlanQuestion opens an embedded question whose 了 belongs to what +// is being looked into, as in "看看为什么测试挂了" or "看看改了哪些文件". +var assistantPlanQuestion = []string{"为什么", "为何", "怎么", "哪", "什么", "是否", "有没有", "多少"} + func assistantPlanHasPerfective(line string) bool { for _, word := range []string{"了解", "除了", "为了"} { line = strings.ReplaceAll(line, word, "") } - return assistantPlanPerfective.MatchString(line) + for _, clause := range assistantPlanClause.Split(line, -1) { + if !assistantPlanPerfective.MatchString(clause) { + continue + } + question := false + for _, word := range assistantPlanQuestion { + if strings.Contains(clause, word) { + question = true + break + } + } + if !question { + return true + } + } + return false } +var assistantPlanClause = regexp.MustCompile(`[,,。;;!!]`) + // assistantPlanProse reports whether a line after a plan opening continues // the plan in prose: it opens with a plan lead-in or names a step to take, // and it carries no result of work already done. @@ -1357,7 +1409,7 @@ func assistantPlanProse(line string) bool { if line == "" { return false } - if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) { + if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) || assistantPlanReportHeader.MatchString(line) { return false } // A plan lead-in opens a step even when the line goes on to name a state @@ -1370,12 +1422,14 @@ func assistantPlanProse(line string) bool { return limit == len(rest) || assistantPlanNamesAction(rest[:limit]) } } - // A count makes a line a report, as in "项目里有 4 个 LOOP,都在运行。". + // An action word names a step only before any result it might be + // reporting ("接口看着正常" reports the state 正常). A line with a count + // or date is a step only when it opens with the action, as in "重点关注 + // 最近 3 次提交" or "读一下 v2 的配置"; "项目里有 4 个 LOOP,都在运 + // 行。" reports what was found. if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) { - return false + return assistantPlanOpensWithAction(line) && assistantPlanNamesAction(line[:assistantPlanResultStart(line, true)]) } - // An action word names a step only before any result it might be - // reporting ("接口看着正常" reports the state 正常). return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) } @@ -1385,10 +1439,24 @@ func assistantPlanProse(line string) bool { func assistantPlanResultStart(line string, counts bool) int { limit := len(line) for _, mark := range assistantPlanResultMark { - if i := strings.Index(line, mark); i >= 0 && i < limit { - limit = i + for from := 0; from < limit; { + i := strings.Index(line[from:], mark) + if i < 0 { + break + } + i += from + if !assistantPlanResultNounAt(line, i) { + if i < limit { + limit = i + } + break + } + from = i + len(mark) } } + if loc := assistantPlanFound.FindStringIndex(line); loc != nil && loc[0] < limit { + limit = loc[0] + } if counts { for _, re := range []*regexp.Regexp{assistantPlanCJKNumber, assistantPlanDigitsOrDate} { if loc := re.FindStringIndex(line); loc != nil && loc[0] < limit { @@ -1399,6 +1467,29 @@ func assistantPlanResultStart(line string, counts bool) int { return limit } +// assistantPlanOpensWithAction reports whether line starts with an action, +// after an adverb such as 重点 or 逐项. +func assistantPlanOpensWithAction(line string) bool { + for _, adverb := range []string{"重点", "逐项", "逐个", "分别", "也", "还要", "需要"} { + line = strings.TrimPrefix(line, adverb) + } + for _, verb := range assistantPlanVerb { + if strings.HasPrefix(line, verb) { + return true + } + } + return false +} + +func assistantPlanResultNounAt(line string, i int) bool { + for _, noun := range assistantPlanResultNoun { + if strings.HasPrefix(line[i:], noun) { + return true + } + } + return false +} + // assistantPlanNamesAction reports whether text names a step to take. func assistantPlanNamesAction(text string) bool { for _, verb := range assistantPlanVerb { @@ -1427,7 +1518,19 @@ func assistantReportsDoneWork(rest string) bool { for _, prefix := range []string{"我先", "我会先", "让我先"} { rest = strings.TrimPrefix(rest, prefix) } - return assistantDoneWork.MatchString(strings.ReplaceAll(rest, "了解", "理解")) + done := assistantDoneWork.FindString(strings.ReplaceAll(rest, "了解", "理解")) + if done == "" { + return false + } + // "看看改了哪些文件" looks into what changed; "检查了配置" and "检查完了" + // report a check. + verb := strings.TrimSuffix(done, "了") + for _, intent := range []string{"看看", "看下", "查看", "确认", "检查"} { + if rest, ok := strings.CutPrefix(verb, intent); ok && rest != "" && !strings.ContainsAny(rest, "完好过到掉") { + return false + } + } + return true } func recoverNativeAssistantReply(raw, projectID string) (assistantReply, bool) { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index b027f3151..b36ec5bbc 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -199,6 +199,10 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { }{ {"numbered list", "multi-line-plan", `我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态`}, {"prose steps", "prose-plan", `我先看一下仓库:\n先确认分支,再读取提交记录。`}, + {"step with a count", "count-step", `我先看一下仓库。\n重点关注最近 3 次提交。`}, + {"goal clause", "goal-step", `我先看一下报错的用例。\n然后修改代码,让测试通过。`}, + {"check via", "check-via", `我先看一下数据。\n先检查通过 API 拿到的数据,再对比数据库。`}, + {"embedded question", "question-step", `我先看一下测试。\n先看看为什么测试挂了,再修复。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -210,6 +214,8 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { // answer: it is delivered as is, without running the CLI again. func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") + runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") + runMultiLinePlanCase(t, "report-header-answer", `我先跑一下检查。\n检查结果:共 3 处告警,都在 store 包。`, "1", "都在 store 包") } func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { @@ -368,6 +374,51 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下仓库。\n确认检查通过。", true}, {"我先看一下仓库。\n检查测试是否通过。", true}, {"Let me first look around.\nCheck the config, then run the tests.", true}, + // Steps written without a whitelisted lead-in still plan when an + // action comes before any count or result; purpose clauses (让/确保/ + // 保证/直到 … 通过|干净|没问题) are goals, not verdicts; 检查 before a + // "via" 通过 is the step's verb; 了 inside an embedded question is + // what the step looks into. + {"我先看一下仓库。\n重点关注最近 3 次提交。", true}, + {"我先看一下团队。\n查看两个小组的成员。", true}, + {"我先看一下测试。\n先看看为什么测试挂了,再修复。", true}, + {"我先看一下改动。\n然后看看改了哪些文件。", true}, + {"我先看一下仓库。\n目前只需要读取配置和日志。", true}, + {"我先看一下接口。\n把返回值和文档逐项核对。", true}, + {"我先确认一下。\n读一下 v2 的配置。", true}, + {"我先看一下报错的用例。\n然后修改代码,让测试通过。", true}, + {"我先整理一下。\n先改代码,确保 lint 干净。", true}, + {"我先看一下接口。\n修改后确保接口没问题。", true}, + {"我先看一下报错。\n先修复报错,保证构建通过。", true}, + {"我先修一下。\n- 改配置\n- 跑测试直到全部通过", true}, + {"我先看一下数据。\n先检查通过 API 拿到的数据,再对比数据库。", true}, + {"我先看一下流程。\n检查通过 MCP 调用工具的流程。", true}, + {"我先看一下提交。\n然后检查通过 webhook 推送的事件。", true}, + {"我先看一下。\n看看日志里有没有报错。", true}, + {"我先查一下。\n对比 main 和 dev 两个分支的差异。", true}, + {"我先看一下。\n- 看看改了哪些文件\n- 运行测试", true}, + // Lines reporting what was found stay answers. + {"我先跑一下检查。\n测试都通过,可以合并。", false}, + {"我先跑一下检查。\n检查通过,工作区干净。", false}, + {"我先跑一下检查。\n检查结果:共 3 处告警,都在 store 包。", false}, + {"我先查一下仓库。\n首先,仓库有两个分支:main 和 dev。", false}, + {"我先查一下。\n最后结论:可以合并。", false}, + {"我先查一下。\n查到 3 个失败用例,都在 store 包。", false}, + {"我先查一下。\n找到两个仓库:demo 和 web。", false}, + {"我先看一下。\n查看了日志,没有异常。", false}, + {"我先看一下。\n看了下日志,接口返回 200。", false}, + {"我先查一下。\n读取到的配置是 v2。", false}, + {"我先看一下。\n日志显示接口超时。", false}, + {"我先看一下仓库。\n仓库里没有 README。", false}, + {"我先查一下。\n确认过了,配置没问题。", false}, + {"我先跑一下测试。\n运行结果:2 个失败。", false}, + {"我先跑一下测试。\n测试结果如下:\n- 2 个失败", false}, + {"我先查一下。\n先检查完了再说。", false}, + {"我先看一下测试。\n测试失败 3 个,都在 store 包。", false}, + {"我先看一下测试。\n测试挂在网络超时。", false}, + {"我先看一下日志。\n11 月 26 日的任务失败了两次。", false}, + {"我先看一下仓库。\n仓库有 2 个分支,查看 main 即可。", false}, + {"我先看一下。\n日志里先看报错,再读配置。", true}, {"", false}, {" \n\n ", false}, } From 9600152f5c176ca605be91bc33004dcee1205812 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 12:46:48 +0000 Subject: [PATCH 09/28] fix(assistant): judge a continuation by its own run; lead-ins plan unless they report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crispinhu27's review of 6750dbde: - [P1] A continuation that answered without tools was judged together with the plan the previous CLI run ended with. The activity log is shared by the runs of a turn, so "text after the last tool call" was the old plan plus the new answer: Studio retried list answers until 502 and the Bot rejected every answer. The log now marks where each run starts (startRound) and the judgment reads only that run's events (roundEvents); with no tool call in the run its own text is judged. - [P1] After a plan lead-in (先/最后/第一步/步骤/Step/First…), an action that came after a count or a result word ("先拉取最新的 5 条记录", "最后给出结论。", "Step 1: read the config.") made the line an answer. A lead-in line is now a step unless it reports what was found: a report header, a stated total ("共 3 个提交", "只有…一个包", "有两个分支"), a status closing a clause ("最近一次成功。"), a verdict or done work. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 47 ++++++--- internal/httpapi/assistant_activity.go | 45 ++++++++- .../httpapi/assistant_progress_text_test.go | 95 +++++++++++++++++++ internal/httpapi/bot_interactions.go | 3 +- 4 files changed, 174 insertions(+), 16 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 94cb70a8c..4e07e8839 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -810,6 +810,7 @@ func (h *assistantHandlers) chat(c *gin.Context) { fail(504, "生成已取消或超时,请重试") return } + activity.startRound() raw, err := generate(modelCtx, turn, user) if err == nil && modelCtx.Err() != nil { err = modelCtx.Err() @@ -881,7 +882,7 @@ func (h *assistantHandlers) chat(c *gin.Context) { continue } } - if len(reply.Drafts) == 0 && assistantProgressOnly(assistantProgressText(reply.Message, activity.snapshot()), req.Messages) { + if len(reply.Drafts) == 0 && assistantProgressOnly(assistantProgressText(reply.Message, activity.roundEvents()), req.Messages) { if progressRetries >= 2 { fail(502, "模型连续只返回进度说明,尚未完成请求;执行记录已保留,请核对后继续。") return @@ -1129,8 +1130,11 @@ func assistantNativeRetryPrompt() string { // their last tool call as the result, but Cursor joins every paragraph of the // turn, so a reply that opened with "我先…" before calling tools still starts // with that plan after the answer followed. Judge Main Bot's text after its -// last tool call, as the other CLIs report it; without a tool call, or with -// no text after it, the whole reply is judged. +// last tool call, as the other CLIs report it; without a tool call, the text +// Main Bot wrote in events is judged, and without any such text the whole +// reply is. events are those of the current CLI run: a continuation that +// answers without tools must not be read together with the plan the previous +// run ended with, which a streamed reply still carries. func assistantProgressText(message string, events []domain.AssistantActivityEvent) string { last := -1 for i, event := range events { @@ -1138,9 +1142,6 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven last = i } } - if last < 0 { - return message - } var parts []string for _, event := range events[last+1:] { if event.Kind != "text" || event.Parent != "" { @@ -1412,14 +1413,14 @@ func assistantPlanProse(line string) bool { if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) || assistantPlanReportHeader.MatchString(line) { return false } - // A plan lead-in opens a step even when the line goes on to name a state - // or a count ("先确认服务是否正常", "先查看最近的 3 次提交"), as long as an - // action comes before it. "检查结果:共 3 处问题" and "最后结论:可以合并。" - // open with a lead-in but report what was found. + // A plan lead-in ("先…", "最后…", "第一步:…", "Step 1: …") opens a step + // unless the line is one of the forms that report what was found: a + // report header, a stated total, a status that closes a clause, a + // verdict or work already done. Its action may come after a count or a + // result word, as in "先拉取最新的 5 条记录" or "最后给出结论。". for _, lead := range assistantPlanLeadIn { - if rest, ok := strings.CutPrefix(line, lead); ok { - limit := assistantPlanResultStart(rest, true) - return limit == len(rest) || assistantPlanNamesAction(rest[:limit]) + if strings.HasPrefix(line, lead) { + return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) } } // An action word names a step only before any result it might be @@ -1433,6 +1434,26 @@ func assistantPlanProse(line string) bool { return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) } +// assistantPlanTotal states how many there are: "共 3 个提交", "范围只有 +// internal/httpapi 一个包", "仓库有两个分支". +var assistantPlanTotal = regexp.MustCompile(`(?:共|只有|仅有|有)[^,,。;;]*?(?:\d+|[一二两三四五六七八九十]+)\s*[个条位项次处份家包件种组台]`) + +// assistantPlanStatus is a state that closes a clause, as in "最近一次成功。" +// or "接口看着正常". +var assistantPlanStatus = regexp.MustCompile(`(?:成功|正常|失败|异常|报错|超时)(?:了)?(?:[,,。;;!!]|$)`) + +// assistantPlanStatesTotal reports whether line states a total. "看看有没有 +// 3 个以上的失败" still checks for one. +func assistantPlanStatesTotal(line string) bool { + return assistantPlanTotal.MatchString(strings.ReplaceAll(line, "有没有", "")) && !assistantPlanCheckIntentIn(line) +} + +// assistantPlanStatesStatus reports whether line states a status rather than +// one still to be checked ("先确认服务是否正常"). +func assistantPlanStatesStatus(line string) bool { + return assistantPlanStatus.MatchString(line) && !assistantPlanCheckIntentIn(line) +} + // assistantPlanResultStart returns where the first result word of line // starts, or len(line) if it has none. With counts set, a count or date // ("3 次", "两个") also starts a result. diff --git a/internal/httpapi/assistant_activity.go b/internal/httpapi/assistant_activity.go index 522fead80..8f71ffa39 100644 --- a/internal/httpapi/assistant_activity.go +++ b/internal/httpapi/assistant_activity.go @@ -17,6 +17,11 @@ type assistantActivityLog struct { leadingSpace string bytes int truncated bool + // round and roundText mark where the current CLI run began: events from + // index round on, plus text appended to a text event that ended at + // roundText, belong to it. See startRound. + round int + roundText string } func (l *assistantActivityLog) add(event assistantStreamEvent) { @@ -167,14 +172,18 @@ func (l *assistantActivityLog) appendTextLocked(event assistantStreamEvent) { func (l *assistantActivityLog) dropTextLocked() { kept := make([]domain.AssistantActivityEvent, 0, len(l.events)) - used := 0 - for _, event := range l.events { + used, round := 0, 0 + for i, event := range l.events { if event.Kind == "text" { continue } kept = append(kept, event) used += len(event.Text) + len(event.Detail) + if i < l.round { + round++ + } } + l.round, l.roundText = round, "" l.events = kept l.bytes = used l.leadingSpace = "" @@ -328,6 +337,38 @@ func transcriptWorthKeeping(text string, events []domain.AssistantActivityEvent) } return false } + +// startRound marks the start of another CLI run in the same turn, such as +// the continuation after a progress-only reply. Text the CLI streams next may +// still be appended to the last text event. +func (l *assistantActivityLog) startRound() { + l.mu.Lock() + defer l.mu.Unlock() + l.round, l.roundText = len(l.events), "" + if n := len(l.events); n > 0 && l.events[n-1].Kind == "text" { + l.roundText = l.events[n-1].Text + } +} + +// roundEvents returns what the current CLI run recorded. A continuation is +// judged by its own text and tools: joined with the plan the previous run +// ended with, its answer would read as more of that plan. +func (l *assistantActivityLog) roundEvents() []domain.AssistantActivityEvent { + l.mu.Lock() + defer l.mu.Unlock() + start := min(l.round, len(l.events)) + var out []domain.AssistantActivityEvent + if start > 0 && l.roundText != "" { + if last := l.events[start-1]; last.Kind == "text" { + if rest, ok := strings.CutPrefix(last.Text, l.roundText); ok && strings.TrimSpace(rest) != "" { + last.Text = rest + out = append(out, last) + } + } + } + return append(out, l.events[start:]...) +} + func (l *assistantActivityLog) snapshot() []domain.AssistantActivityEvent { l.mu.Lock() defer l.mu.Unlock() diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index b36ec5bbc..5f9e77f0b 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -419,6 +419,25 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下日志。\n11 月 26 日的任务失败了两次。", false}, {"我先看一下仓库。\n仓库有 2 个分支,查看 main 即可。", false}, {"我先看一下。\n日志里先看报错,再读配置。", true}, + // A lead-in opens a step even when its action comes after a count or + // a result word; only the forms that report what was found end the + // plan (a header, a stated total, a closing status). + {"我先看一下。\n最后给出结论。", true}, + {"我先确认一下。\n步骤:1. 读取配置;2. 运行测试。", true}, + {"我先整理一下。\n先把结果整理成表格。", true}, + {"我先看一下。\n先拉取最新的 5 条记录。", true}, + {"Let me first check the repo.\nStep 1: read the config.", true}, + {"我先看一下。\n先跟目前线上的配置做对比。", true}, + {"我先看一下。\n先打印返回值,再核对文档。", true}, + {"我先看一下。\n首先在 11 月 30 日之前确认窗口。", true}, + {"我先看一下。\n先在两个分支上分别跑测试。", true}, + {"我先看一下。\n最后把结论写进文档。", true}, + {"我先看一下。\n第一步:把 2 个服务的日志拉下来。", true}, + {"我先看一下。\n最后输出结果。", true}, + {"Let me first check the repo.\nFirst, pull the 3 latest runs.", true}, + {"我先看一下。\n先看看有没有 3 个以上的报错。", true}, + {"我先看一下。\n最后,测试全部成功。", false}, + {"我先看一下。\n首先,接口返回正常。", false}, {"", false}, {" \n\n ", false}, } @@ -428,3 +447,79 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { } } } + +// A continuation is judged by its own output. The first run ended with only +// a plan after its tools; the continuation answers without tools. Joined with +// that plan, the answer read as more of it and was retried until 502 (Studio) +// or rejected (Bot). +func TestAssistantContinuationAnswerIsJudgedOnItsOwn(t *testing.T) { + const answer = `- LOOP:后端研发\n- 状态:运行中` + script := func(counter string) string { + return `#!/bin/sh +if [ "$1" = "--version" ]; then echo "1.0.0"; exit 0; fi +n=0 +if [ -f '` + counter + `' ]; then read -r n < '` + counter + `'; fi +n=$((n + 1)) +echo "$n" > '` + counter + `' +if [ "$n" -gt 1 ]; then +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"text","text":"` + answer + `"}]}} +{"type":"result","result":"` + answer + `"} +JSON +exit 0 +fi +cat <<'JSON' +{"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} +{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP。"}]}} +{"type":"result","result":"我先查看项目里的 LOOP。"} +JSON +` + } + t.Run("studio generation", func(t *testing.T) { + h, user := workbenchFixture(t) + h.titleSummary = func(context.Context, domain.User, string) (string, error) { return "", nil } + dir := t.TempDir() + counter, bin := filepath.Join(dir, "calls"), filepath.Join(dir, "codebuddy") + if err := os.WriteFile(bin, []byte(script(counter)), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + h.generate = nil + c, rec := workbenchContext(user, "POST", "/api/assistant/generations", bytes.NewBufferString(`{"projectId":"workbench","sessionId":"continued","clientRequestId":"continued-1","messages":[{"role":"user","content":"查看已有 LOOP 并给出结果"}]}`)) + h.startGeneration(c) + if rec.Code != 202 { + t.Fatalf("start: %s", rec.Body.String()) + } + deadline := time.Now().Add(20 * time.Second) + for { + g, _ := h.repo.AssistantGeneration(user.ID, "continued") + if g.Status != "running" { + calls, _ := os.ReadFile(counter) + if g.Status != "completed" || strings.TrimSpace(string(calls)) != "2" || !strings.Contains(string(g.Reply), "后端研发") { + t.Fatalf("calls=%s status=%s error=%s reply=%s", strings.TrimSpace(string(calls)), g.Status, g.Error, g.Reply) + } + break + } + if time.Now().After(deadline) { + t.Fatal("generation timed out") + } + time.Sleep(20 * time.Millisecond) + } + }) + t.Run("bot", func(t *testing.T) { + stubCLIModelDiscovery(t) + _, h, bot := botFixture(t) + dir := t.TempDir() + counter, bin := filepath.Join(dir, "calls"), filepath.Join(dir, "codebuddy") + if err := os.WriteFile(bin, []byte(script(counter)), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + reply, err := h.generateBotReply(context.Background(), bot, domain.BotConversation{SessionID: "bot-continued", ChatType: "single"}, nil, domain.BotInteraction{SenderID: "new-wecom-user", Content: "项目里的 LOOP"}) + calls, _ := os.ReadFile(counter) + if err != nil || strings.TrimSpace(string(calls)) != "2" || !strings.Contains(reply, "后端研发") { + t.Fatalf("calls=%s err=%v reply=%q", strings.TrimSpace(string(calls)), err, reply) + } + }) +} diff --git a/internal/httpapi/bot_interactions.go b/internal/httpapi/bot_interactions.go index 56b794423..a9866203e 100644 --- a/internal/httpapi/bot_interactions.go +++ b/internal/httpapi/bot_interactions.go @@ -186,6 +186,7 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not botinteraction.ReportProgress(ctx, "正在提交回复") session.SetTerminalOnly() } + activity.startRound() raw, cliErr := generate(modelCtx, prompt, user) final, ok := session.takeFinal() if !ok || final.Assistant == nil { @@ -194,7 +195,7 @@ func (h *assistantHandlers) generateBotReply(ctx context.Context, bot domain.Not // from a successful run is accepted: output carrying protocol // JSON (a draft attempt, possibly filtered during parsing) and // progress-only text still go through the terminal retry. - if cand, recovered := recoverNativeAssistantReply(raw, bot.ProjectID); cliErr == nil && recovered && !assistantProtocolJSONPresent(raw) && len(cand.Drafts) == 0 && !assistantProgressOnly(assistantProgressText(cand.Message, activity.snapshot()), messages) { + if cand, recovered := recoverNativeAssistantReply(raw, bot.ProjectID); cliErr == nil && recovered && !assistantProtocolJSONPresent(raw) && len(cand.Drafts) == 0 && !assistantProgressOnly(assistantProgressText(cand.Message, activity.roundEvents()), messages) { final, ok = AgentTerminalResult{Kind: AgentTerminalAssistant, Assistant: &cand}, true } } From 145148fb28dd409743767feeee0ec7991acd5a0f Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 13:13:08 +0000 Subject: [PATCH 10/28] =?UTF-8?q?fix(assistant):=20treat=20a=20goal=20line?= =?UTF-8?q?=20(=E7=A1=AE=E4=BF=9D/=E4=BF=9D=E8=AF=81/make=20sure=20?= =?UTF-8?q?=E2=80=A6)=20as=20a=20plan=20step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A plan opened with "我先…" can continue with the state its work is to reach, as in "确保 lint 干净。" or "保证构建通过。". Such a line has a verdict word but a goal, not a finding, so keep the turn a progress note. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 10 ++++++++++ internal/httpapi/assistant_progress_text_test.go | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 4e07e8839..573033d45 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1413,6 +1413,13 @@ func assistantPlanProse(line string) bool { if assistantReportsDoneWork(strings.TrimSpace(line)) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) || assistantPlanReportHeader.MatchString(line) { return false } + // A goal ("确保 lint 干净", "保证构建通过") names the state a step is to + // reach, not one already found. + for _, goal := range assistantPlanGoal { + if strings.HasPrefix(line, goal) { + return true + } + } // A plan lead-in ("先…", "最后…", "第一步:…", "Step 1: …") opens a step // unless the line is one of the forms that report what was found: a // report header, a stated total, a status that closes a clause, a @@ -1434,6 +1441,9 @@ func assistantPlanProse(line string) bool { return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) } +// assistantPlanGoal opens a line that names the state the work is to reach. +var assistantPlanGoal = []string{"确保", "保证", "务必", "Make sure", "make sure", "Ensure", "ensure"} + // assistantPlanTotal states how many there are: "共 3 个提交", "范围只有 // internal/httpapi 一个包", "仓库有两个分支". var assistantPlanTotal = regexp.MustCompile(`(?:共|只有|仅有|有)[^,,。;;]*?(?:\d+|[一二两三四五六七八九十]+)\s*[个条位项次处份家包件种组台]`) diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 5f9e77f0b..a7646f955 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -390,6 +390,10 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先整理一下。\n先改代码,确保 lint 干净。", true}, {"我先看一下接口。\n修改后确保接口没问题。", true}, {"我先看一下报错。\n先修复报错,保证构建通过。", true}, + {"我先看一下。\n确保 lint 干净。", true}, + {"我先看一下。\n确保接口没问题。", true}, + {"我先看一下。\n保证构建通过。", true}, + {"Let me first check the build.\nMake sure the tests pass.", true}, {"我先修一下。\n- 改配置\n- 跑测试直到全部通过", true}, {"我先看一下数据。\n先检查通过 API 拿到的数据,再对比数据库。", true}, {"我先看一下流程。\n检查通过 MCP 调用工具的流程。", true}, From 7fe2b8aab51c357da84d722febe4f3310b2b7111 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 14:16:31 +0000 Subject: [PATCH 11/28] fix(assistant): keep preconditions, status objects and clean/until goals a plan step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crispinhu27's review of 145148fb found three kinds of plan lines still delivered as the answer after a tool call: - A 到 the next step waits for ("拿到 token 后调用接口", "读到文件末尾后 统计行数", "等拿到权限再部署") was read as something already found. A 到 clause closed by 后/之后/以后/再/才 is now a precondition and the line plans a step, unless what follows reports a finding (发现/显示/…, a result word or a count). Words such as 后端/后续 do not close one. - 报错/异常/超时 right after an action ("先修复报错", "最后排查异常", "再定位这个报错") are the object of the step, not a status that closes the clause. 成功/失败/正常 still state an outcome ("最后,修复失败。"). - 干净 right after its verb or closing a 把 clause ("清干净", "把分支整理 干净", "改干净") and 通过 before 为止 ("修到测试全部通过为止") are the state a step is to reach. "工作区已经干净", "改动干净" and "到目前为止 测试全部通过" remain verdicts. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 129 +++++++++++++++++- .../httpapi/assistant_progress_text_test.go | 47 +++++++ 2 files changed, 173 insertions(+), 3 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 573033d45..e8fbc3a38 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1269,7 +1269,42 @@ var assistantPlanPassActSubject = []string{"检查", "审核", "校验"} // rather than describing a state ("lint 也干净"). var assistantPlanCleanupVerb = []string{"清理", "清空", "清除", "删除", "删掉", "擦", "洗", "扫", "收拾"} +// assistantPlanCleanComplement is a verb that takes 干净 as its result +// complement right after it ("清干净", "整理干净", "改干净"): the step makes +// something clean. A state is stated with an adverb or a subject instead +// ("工作区已经干净", "lint 也干净"). +var assistantPlanCleanComplement = []string{"清", "理", "扫", "弄", "搞", "洗", "擦", "删", "改", "收拾", "打扫"} + +// assistantPlanCleanGoal reports whether the 干净 at i is the result a step +// is to reach: a complement right after its verb, or the end of a 把 +// construction ("把分支整理干净", "把环境弄得干净"). +func assistantPlanCleanGoal(line string, i int) bool { + before := strings.TrimSuffix(line[:i], "得") + for _, verb := range assistantPlanCleanComplement { + if strings.HasSuffix(before, verb) { + return true + } + } + clause := before + if loc := assistantPlanClause.FindAllStringIndex(clause, -1); len(loc) > 0 { + clause = clause[loc[len(loc)-1][1]:] + } + return strings.Contains(clause, "把") +} + +// assistantPlanTargetIn reports whether line names the state a step runs +// until, as in "修到测试全部通过为止". "到目前为止" dates a report instead. +func assistantPlanTargetIn(line string) bool { + for _, since := range []string{"目前为止", "现在为止", "今为止", "此为止"} { + line = strings.ReplaceAll(line, since, "") + } + return strings.Contains(line, "为止") +} + func assistantPlanStatesVerdict(line string) bool { + if assistantPlanTargetIn(line) { + return false + } if strings.Contains(line, "通过") { // The verdict's own subject is not a check intent: "检查通过" states // the result, "确认检查通过" still plans to verify it. @@ -1282,7 +1317,7 @@ func assistantPlanStatesVerdict(line string) bool { return false } } - return true + return !assistantPlanCleanGoal(line, i) } if strings.Contains(line, "干净") { return false @@ -1420,6 +1455,9 @@ func assistantPlanProse(line string) bool { return true } } + if assistantPlanTargetIn(line) { + return true + } // A plan lead-in ("先…", "最后…", "第一步:…", "Step 1: …") opens a step // unless the line is one of the forms that report what was found: a // report header, a stated total, a status that closes a clause, a @@ -1430,6 +1468,11 @@ func assistantPlanProse(line string) bool { return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) } } + // "拿到 token 后调用接口" or "等拿到权限再部署" names what a step waits + // for: the 到 is a precondition, not something found. + if assistantPlanStatesPrecondition(line) { + return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) + } // An action word names a step only before any result it might be // reporting ("接口看着正常" reports the state 正常). A line with a count // or date is a step only when it opens with the action, as in "重点关注 @@ -1459,9 +1502,89 @@ func assistantPlanStatesTotal(line string) bool { } // assistantPlanStatesStatus reports whether line states a status rather than -// one still to be checked ("先确认服务是否正常"). +// one still to be checked ("先确认服务是否正常") or the object of a step +// ("先修复报错", "最后排查异常"). func assistantPlanStatesStatus(line string) bool { - return assistantPlanStatus.MatchString(line) && !assistantPlanCheckIntentIn(line) + if assistantPlanCheckIntentIn(line) { + return false + } + for _, loc := range assistantPlanStatus.FindAllStringIndex(line, -1) { + if !assistantPlanStatusIsObject(line[:loc[0]], line[loc[0]:loc[1]]) { + return true + } + } + return false +} + +// assistantPlanStatusNoun is a status that is also a noun a step can act on +// ("修复报错", "处理超时"). 成功/失败/正常 after a verb state its outcome +// ("部署失败", "运行成功"), so they are left out. +var assistantPlanStatusNoun = []string{"报错", "异常", "超时"} + +// assistantPlanStatusVerb is an action that takes such a noun as its object. +var assistantPlanStatusVerb = []string{"修复", "修", "排查", "定位", "复现", "分析", "处理", "解决", "查看", "检查", "调查", "跟进", "诊断", "避免", "规避", "消除", "捕获", "查", "看", "找"} + +// assistantPlanStatusModifier may stand between the verb and its object, as +// in "定位这个报错" or "处理相关的超时". +var assistantPlanStatusModifier = []string{"的", "这个", "这些", "那个", "那些", "该", "此", "相关", "所有", "全部"} + +func assistantPlanStatusIsObject(before, status string) bool { + noun := false + for _, word := range assistantPlanStatusNoun { + if strings.HasPrefix(status, word) { + noun = true + break + } + } + if !noun { + return false + } + before = strings.TrimSpace(before) + for trimmed := true; trimmed; { + trimmed = false + for _, modifier := range assistantPlanStatusModifier { + if rest, ok := strings.CutSuffix(before, modifier); ok { + before, trimmed = strings.TrimSpace(rest), true + } + } + } + for _, verb := range assistantPlanStatusVerb { + if strings.HasSuffix(before, verb) { + return true + } + } + return false +} + +// assistantPlanPrecondition matches a 到 the next step waits for, as in "拿到 +// token 后调用接口", "读到文件末尾后统计行数", "收到回调之后更新状态" or "等 +// 拿到权限再部署". +var assistantPlanPrecondition = regexp.MustCompile(`(?:查|找|看|搜|拿|读|取|收|得)到[^,,。;;!!::]*?(?:之后|以后|后|再|才)`) + +// assistantPlanFinding follows a precondition in a report of what it turned +// up: "查到原因后发现是配置错误". +var assistantPlanFinding = []string{"发现", "显示", "表明", "原因是", "结论", "确认是", "原来"} + +// assistantPlanStatesPrecondition reports whether line plans a step after a +// precondition it waits for. Words that start with 后 (后端, 后续…) do not +// close one. +func assistantPlanStatesPrecondition(line string) bool { + masked := line + for _, word := range []string{"后端", "后台", "后续", "后面", "后者", "后缀", "后果", "后门", "后来", "再次"} { + masked = strings.ReplaceAll(masked, word, strings.Repeat("·", utf8.RuneCountInString(word))) + } + loc := assistantPlanPrecondition.FindStringIndex(masked) + if loc == nil { + return false + } + rest := masked[loc[1]:] + for _, finding := range assistantPlanFinding { + if strings.Contains(rest, finding) { + return false + } + } + // "拿到 token 后接口返回 200" reports what followed. + return assistantPlanResultStart(rest, true) == len(rest) } // assistantPlanResultStart returns where the first result word of line diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index a7646f955..8bb384a2a 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -203,6 +203,10 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"goal clause", "goal-step", `我先看一下报错的用例。\n然后修改代码,让测试通过。`}, {"check via", "check-via", `我先看一下数据。\n先检查通过 API 拿到的数据,再对比数据库。`}, {"embedded question", "question-step", `我先看一下测试。\n先看看为什么测试挂了,再修复。`}, + {"precondition", "precondition-step", `我先看一下。\n拿到 token 后调用接口。`}, + {"status as object", "status-object-step", `我先看一下。\n先看日志,再定位报错。`}, + {"clean complement", "clean-complement-step", `我先看一下。\n先把环境清干净,再跑测试。`}, + {"run until", "run-until-step", `我先看一下。\n修到测试全部通过为止。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -216,6 +220,9 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") runMultiLinePlanCase(t, "report-header-answer", `我先跑一下检查。\n检查结果:共 3 处告警,都在 store 包。`, "1", "都在 store 包") + runMultiLinePlanCase(t, "found-answer", `我先查一下。\n查到 3 个失败用例。`, "1", "查到 3 个失败用例") + runMultiLinePlanCase(t, "status-answer", `我先看一下。\n接口超时。`, "1", "接口超时") + runMultiLinePlanCase(t, "clean-state-answer", `我先看一下。\n工作区已经干净。`, "1", "工作区已经干净") } func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { @@ -442,6 +449,46 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n先看看有没有 3 个以上的报错。", true}, {"我先看一下。\n最后,测试全部成功。", false}, {"我先看一下。\n首先,接口返回正常。", false}, + // A 到 the next step waits for is a precondition, not something + // found; 报错/异常/超时 right after an action are its object; 干净 + // right after its verb or closing a 把 clause, and 通过 before 为止, + // are the state a step is to reach. + {"我先看一下。\n读到文件末尾后统计行数。", true}, + {"我先看一下。\n查到对应提交后回滚。", true}, + {"我先看一下。\n拿到 token 后调用接口。", true}, + {"我先看一下。\n等拿到权限再部署。", true}, + {"我先看一下。\n拿到结果后整理成表格。", true}, + {"我先看一下。\n找到入口函数后再加日志。", true}, + {"我先看一下。\n收到回调之后更新状态。", true}, + {"我先看一下。\n先修复报错。", true}, + {"我先看一下。\n最后排查异常。", true}, + {"我先看一下。\n先看日志,再定位报错。", true}, + {"我先看一下。\n首先复现超时,然后修复。", true}, + {"我先看一下。\n先下载日志,再分析报错。", true}, + {"我先看一下。\n先处理超时。", true}, + {"我先看一下。\n先定位这个报错。", true}, + {"我先看一下。\n先把环境清干净,再跑测试。", true}, + {"我先看一下。\n然后把分支整理干净。", true}, + {"我先看一下。\n先把工作区改干净再提交。", true}, + {"我先看一下。\n修到测试全部通过为止。", true}, + {"我先看一下。\n- 把临时文件删干净", true}, + // The same words still report what was found. + {"我先看一下。\n查到 3 个失败用例。", false}, + {"我先看一下。\n找到两个仓库:api 和 web。", false}, + {"我先看一下。\n读取到的配置是 v2。", false}, + {"我先看一下。\n拿到了 token,接口返回 200。", false}, + {"我先看一下。\n查到原因后发现是配置错误。", false}, + {"我先看一下。\n查到后端日志里有 3 处报错。", false}, + {"我先看一下。\n最近一次成功。", false}, + {"我先看一下。\n接口超时。", false}, + {"我先看一下。\n测试全部通过。", false}, + {"我先看一下。\n构建报错。", false}, + {"我先看一下。\n日志里有 2 处报错。", false}, + {"我先看一下。\n工作区已经干净。", false}, + {"我先看一下。\n改动干净。", false}, + {"我先看一下。\n首先,部署报错。", false}, + {"我先看一下。\n最后,修复失败。", false}, + {"我先看一下。\n到目前为止测试全部通过。", false}, {"", false}, {" \n\n ", false}, } From 1cd1b9213c5be90cda90ecc10b4f12931248ccd8 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 16:56:11 +0000 Subject: [PATCH 12/28] =?UTF-8?q?fix(assistant):=20read=20any=20=E5=88=B0-?= =?UTF-8?q?precondition=20and=20a=20completion=20+=20=E5=90=8E=20as=20a=20?= =?UTF-8?q?plan=20step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A line that names what a step waits for ("扫描到本地目录后比较差异。", "下载完成后运行脚本。", "部署完成后验证接口是否正常。") keeps planning the next step; only a precondition followed by a report of what came of it ("部署完成后接口返回 200。", "检查到一半后报错。") states a result. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 64 ++++++++++++++----- .../httpapi/assistant_progress_text_test.go | 16 +++++ 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index e8fbc3a38..ae2281482 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1458,6 +1458,16 @@ func assistantPlanProse(line string) bool { if assistantPlanTargetIn(line) { return true } + // "拿到 token 后调用接口", "扫描到本地目录后比较差异" or "等拿到权限再 + // 部署" names what a step waits for: the 到 is a precondition, not + // something found. "部署完成后接口返回 200" turns out to report what + // followed, so it is no plan either. + if plan, matched := assistantPlanStatesPrecondition(line); matched { + if !plan { + return false + } + return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) + } // A plan lead-in ("先…", "最后…", "第一步:…", "Step 1: …") opens a step // unless the line is one of the forms that report what was found: a // report header, a stated total, a status that closes a clause, a @@ -1468,11 +1478,6 @@ func assistantPlanProse(line string) bool { return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) } } - // "拿到 token 后调用接口" or "等拿到权限再部署" names what a step waits - // for: the 到 is a precondition, not something found. - if assistantPlanStatesPrecondition(line) { - return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) - } // An action word names a step only before any result it might be // reporting ("接口看着正常" reports the state 正常). A line with a count // or date is a step only when it opens with the action, as in "重点关注 @@ -1556,35 +1561,64 @@ func assistantPlanStatusIsObject(before, status string) bool { return false } -// assistantPlanPrecondition matches a 到 the next step waits for, as in "拿到 -// token 后调用接口", "读到文件末尾后统计行数", "收到回调之后更新状态" or "等 -// 拿到权限再部署". -var assistantPlanPrecondition = regexp.MustCompile(`(?:查|找|看|搜|拿|读|取|收|得)到[^,,。;;!!::]*?(?:之后|以后|后|再|才)`) +// assistantPlanPrecondition matches a condition the next step waits for, as in +// "拿到 token 后调用接口", "扫描到本地目录后比较差异", "部署完成后验证接口是否 +// 正常" or "等拿到权限再部署". It is any verb with the completive 到 or a +// completion (完成/结束/完毕) followed by a temporal 后/再/才. +var assistantPlanPrecondition = regexp.MustCompile(`(?:\p{Han}到|完成|结束|完毕)[^,,。;;!!::]*?(?:之后|以后|后|再|才)`) + +// assistantPlanEventStart is a past-tense event: "检查到一半后报错" reports +// what happened, it does not plan a step after the 报错. +var assistantPlanEventStart = []string{"报错", "崩溃", "挂掉", "挂了", "中断", "退出", "卡住", "卡死"} // assistantPlanFinding follows a precondition in a report of what it turned // up: "查到原因后发现是配置错误". var assistantPlanFinding = []string{"发现", "显示", "表明", "原因是", "结论", "确认是", "原来"} +// assistantPlanCheckTarget is an interrogative tail: it asks about the state +// the step checks ("验证接口是否正常") instead of reporting it. +var assistantPlanCheckTarget = []string{"是否", "有没有", "能否", "会不会"} + +func assistantPlanCheckTargetIn(line string) bool { + for _, target := range assistantPlanCheckTarget { + if strings.Contains(line, target) { + return true + } + } + return false +} + // assistantPlanStatesPrecondition reports whether line plans a step after a -// precondition it waits for. Words that start with 后 (后端, 后续…) do not -// close one. -func assistantPlanStatesPrecondition(line string) bool { +// precondition it waits for. matched reports whether the line names such a +// precondition at all, so a caller can rule the line out instead of judging it +// by its action word. Words that start with 后 (后端, 后续…) do not close one. +func assistantPlanStatesPrecondition(line string) (plan, matched bool) { masked := line for _, word := range []string{"后端", "后台", "后续", "后面", "后者", "后缀", "后果", "后门", "后来", "再次"} { masked = strings.ReplaceAll(masked, word, strings.Repeat("·", utf8.RuneCountInString(word))) } loc := assistantPlanPrecondition.FindStringIndex(masked) if loc == nil { - return false + return false, false } rest := masked[loc[1]:] + // "部署完成后验证接口是否正常" still asks rather than reports; "查到对 + // 应提交后,确认是 abc123 引入的" reports what the check found. + if assistantPlanCheckTargetIn(rest) { + return true, true + } + for _, event := range assistantPlanEventStart { + if strings.HasPrefix(strings.TrimSpace(rest), event) { + return false, true + } + } for _, finding := range assistantPlanFinding { if strings.Contains(rest, finding) { - return false + return false, true } } // "拿到 token 后接口返回 200" reports what followed. - return assistantPlanResultStart(rest, true) == len(rest) + return assistantPlanResultStart(rest, true) == len(rest), true } // assistantPlanResultStart returns where the first result word of line diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 8bb384a2a..755e23eee 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -207,6 +207,9 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"status as object", "status-object-step", `我先看一下。\n先看日志,再定位报错。`}, {"clean complement", "clean-complement-step", `我先看一下。\n先把环境清干净,再跑测试。`}, {"run until", "run-until-step", `我先看一下。\n修到测试全部通过为止。`}, + {"scan precondition", "scan-precondition-step", `我先看一下。\n扫描到本地目录后比较差异。`}, + {"completion precondition", "completion-step", `我先看一下。\n下载完成后运行脚本。`}, + {"completion check", "completion-check-step", `我先看一下。\n部署完成后验证接口是否正常。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -223,6 +226,7 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "found-answer", `我先查一下。\n查到 3 个失败用例。`, "1", "查到 3 个失败用例") runMultiLinePlanCase(t, "status-answer", `我先看一下。\n接口超时。`, "1", "接口超时") runMultiLinePlanCase(t, "clean-state-answer", `我先看一下。\n工作区已经干净。`, "1", "工作区已经干净") + runMultiLinePlanCase(t, "completion-answer", `我先看一下。\n部署完成后接口返回 200。`, "1", "接口返回 200") } func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { @@ -472,6 +476,18 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n先把工作区改干净再提交。", true}, {"我先看一下。\n修到测试全部通过为止。", true}, {"我先看一下。\n- 把临时文件删干净", true}, + // Any verb with the completive 到, and a completion followed by 后/ + // 再/才, names what the next step waits for. + {"我先看一下。\n扫描到本地目录后比较差异。", true}, + {"我先看一下。\n下载完成后运行脚本。", true}, + {"我先看一下。\n上传完成后对比差异。", true}, + {"我先看一下。\n导入完成后重启服务。", true}, + {"我先看一下。\n同步完成后再部署。", true}, + {"我先看一下。\n构建完成后跑测试。", true}, + {"我先看一下。\n部署完成后验证接口是否正常。", true}, + {"我先看一下。\n安装完毕后再启动。", true}, + {"我先看一下。\n部署完成后接口返回 200。", false}, + {"我先看一下。\n检查到一半后报错。", false}, // The same words still report what was found. {"我先看一下。\n查到 3 个失败用例。", false}, {"我先看一下。\n找到两个仓库:api 和 web。", false}, From 563d8ae1f07719904d2c1bde40fbde41e1c77213 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 11:59:04 +0800 Subject: [PATCH 13/28] fix(assistant): continue ordinary tool-following plan steps --- internal/httpapi/assistant.go | 19 ++++++++++++ .../httpapi/assistant_progress_text_test.go | 30 +++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index ae2281482..8a81190c0 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1437,6 +1437,16 @@ func assistantPlanHasPerfective(line string) bool { var assistantPlanClause = regexp.MustCompile(`[,,。;;!!]`) +// These imperatives can follow a project, branch or object before the verb. +// Digits in that scope (such as "在 v2 分支上") are part of the target. +var assistantPlanCommand = regexp.MustCompile(`^(?:(?:在|对|把)[^,,。;;::!?!?\n]+?(?:跑|运行|执行|检查|验证|核对|列出|对比|比较|拉取|打开|编译)|(?:比较|拉取|打开|编译))`) + +// 比较 also modifies adjectives ("比较稳定" describes a result). +var assistantPlanComparisonDegree = regexp.MustCompile(`^(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)(?:[,,。;;!!]|$)`) + +// An action used as a noun or followed by a measured outcome reports a result. +var assistantPlanCommandReport = regexp.MustCompile(`^(?:的|了|器|状态|速度|时间)|(?:耗时|用时|发现|得到|结果(?:是|为))`) + // assistantPlanProse reports whether a line after a plan opening continues // the plan in prose: it opens with a plan lead-in or names a step to take, // and it carries no result of work already done. @@ -1478,6 +1488,15 @@ func assistantPlanProse(line string) bool { return !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) } } + if command := assistantPlanCommand.FindString(line); command != "" { + object := strings.TrimSpace(line[len(command):]) + if strings.HasSuffix(command, "比较") && assistantPlanComparisonDegree.MatchString(object) { + return false + } + return !assistantPlanCommandReport.MatchString(object) && + !assistantPlanStatesTotal(line) && !assistantPlanStatesStatus(line) && + assistantPlanResultStart(line, false) == len(line) + } // An action word names a step only before any result it might be // reporting ("接口看着正常" reports the state 正常). A line with a count // or date is a step only when it opens with the action, as in "重点关注 diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 755e23eee..b727cf63a 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -198,6 +198,10 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { name, session, plan string }{ {"numbered list", "multi-line-plan", `我先查看项目里的 LOOP:\n1. 读取工作流配置\n2. 汇总节点状态`}, + {"compare members", "plan-0", `我先看一下。\n比较两个小组的成员。`}, + {"versioned scope", "plan-1", `我先看一下。\n在 v2 分支上跑测试。`}, + {"fetch and compile", "plan-2", `我先看一下。\n拉取最新代码并编译。`}, + {"open configuration", "plan-3", `我先看一下。\n打开配置文件。`}, {"prose steps", "prose-plan", `我先看一下仓库:\n先确认分支,再读取提交记录。`}, {"step with a count", "count-step", `我先看一下仓库。\n重点关注最近 3 次提交。`}, {"goal clause", "goal-step", `我先看一下报错的用例。\n然后修改代码,让测试通过。`}, @@ -316,6 +320,28 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"稍等,我先查了一下:项目有 4 个 LOOP。", false}, {"我先查看项目里的 LOOP。\n项目里有 4 个 LOOP,都在运行。", false}, {"我先查看项目里的 LOOP。", true}, + {"我先看一下。\n比较两个小组的成员。", true}, + {"我先看一下。\n比较两个版本的配置文件。", true}, + {"我先看一下。\n比较项目和仓库的默认配置。", true}, + {"我先看一下。\n比较慢。", false}, + {"我先看一下。\n比较稳定。", false}, + {"我先看一下。\n在 v2 分支上比较稳定。", false}, + {"我先看一下。\n在 v2 分支上跑测试。", true}, + {"我先看一下。\n拉取最新代码并编译。", true}, + {"我先看一下。\n打开配置文件。", true}, + {"我先看一下。\n对两个服务运行测试。", true}, + {"我先看一下。\n把这两个小组的成员列出。", true}, + {"我先看一下。\n在 v2 分支上跑了 3 次测试。", false}, + {"我先看一下。\n打开的文件是配置模板。", false}, + {"我先看一下。\n比较结果是两个小组相同。", false}, + {"我先看一下。\n这个连接比较慢。", false}, + {"我先看一下。\n在 v2 分支上运行状态正常。", false}, + {"我先看一下。\n拉取最新代码后编译失败。", false}, + {"我先看一下。\n编译器是 Go 1.24。", false}, + {"我先看一下。\n在 v2 分支上跑测试,返回 2 个错误。", false}, + {"我先看一下。\n拉取最新代码耗时 5 秒。", false}, + {"我先看一下。\n这次打开配置文件耗时 3 秒。", false}, + {"我先看一下。\n对两个服务运行测试,发现三个错误。", false}, {"稍等,我先检查", true}, {"我先看看 store.py。\n接下来我会补测试。", true}, {"我先读代码。\n\n接下来我将运行测试。", true}, @@ -538,8 +564,8 @@ fi cat <<'JSON' {"type":"assistant","message":{"content":[{"type":"tool_use","id":"list-1","name":"mcp__agentflow__workflows_list","input":{}}]}} {"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"list-1","content":"[]"}]}} -{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP。"}]}} -{"type":"result","result":"我先查看项目里的 LOOP。"} +{"type":"assistant","message":{"content":[{"type":"text","text":"我先查看项目里的 LOOP。\n在 v2 分支上跑测试。"}]}} +{"type":"result","result":"我先查看项目里的 LOOP。\n在 v2 分支上跑测试。"} JSON ` } From 46bc676e50e78c21e7a8b4e0d112c7e80a237ca5 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 04:21:02 +0000 Subject: [PATCH 14/28] fix(assistant): continue plan steps outside the action list, as main did MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After a plan opening, a line whose action is not in assistantPlanVerb ("重启服务", "部署到测试环境", "统计各成员的提交数", "看看谁改了配置", "直到单测全部通过") was taken as the answer, so the turn ended with the plan where main continued it. Such a line is now a step unless it states something: work already done, a verdict, a total or status, a count, a result word, or a statement mark (copula, existence, negation, location, judgement, need, owner, ongoing state). Embedded questions with 谁/吗/了没 keep their 了 on the plan side. An opening that introduces the answer ("我先说结论:", "我先总结一下:") followed by more lines is the answer, not a plan. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 95 ++++++++++++++++++- .../httpapi/assistant_progress_text_test.go | 41 ++++++++ 2 files changed, 132 insertions(+), 4 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 8a81190c0..d02a8dbc6 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -16,6 +16,7 @@ import ( "sync" "sync/atomic" "time" + "unicode" "unicode/utf8" "agentflow/internal/application" @@ -1181,7 +1182,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { // result (perfective 了, counts, dates, 结论/结果…) is an answer, and an // opening that reports work already done ("我先查了…,结论如下。") is the // start of an answer, not a plan. - opened := false + opened, announced := false, false for line := range strings.SplitSeq(text, "\n") { line = strings.TrimSpace(line) if line == "" { @@ -1191,10 +1192,14 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { if !assistantProgressSentence(line) { return false } - opened = true + opened, announced = true, assistantAnnouncesAnswer(line) continue } - if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) { + // "我先说结论:" followed by more lines opens the answer. + if announced { + return false + } + if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) && !assistantPlanBareStep(line) { return false } } @@ -1213,6 +1218,19 @@ func assistantProgressSentence(text string) bool { return false } +// assistantAnswerOpening follows "我先" in a line that introduces the answer +// written below it, as in "我先说结论:" or "我先总结一下:". +var assistantAnswerOpening = regexp.MustCompile(`^(?:我先|让我先|我会先)?(?:说|讲|给|报告|汇报|总结|概括|回答|简单说)(?:一下)?(?:结论|结果|答案|现状)?\s*[::,,。]?\s*$|^(?:我先|让我先)(?:说|讲|给出?|汇报)(?:一下)?(?:结论|结果|答案)`) + +func assistantAnnouncesAnswer(line string) bool { + for _, prefix := range []string{"好的,我先", "我先", "让我先", "我会先"} { + if rest, ok := strings.CutPrefix(line, prefix); ok { + return assistantAnswerOpening.MatchString(rest) || assistantAnswerOpening.MatchString(line) + } + } + return false +} + // assistantPlanStep reports whether a line after a plan opening continues // it: a "然后…" step or a list item naming a step still to take. func assistantPlanStep(line string) bool { @@ -1411,7 +1429,7 @@ var assistantPlanPerfective = regexp.MustCompile(`了`) // assistantPlanQuestion opens an embedded question whose 了 belongs to what // is being looked into, as in "看看为什么测试挂了" or "看看改了哪些文件". -var assistantPlanQuestion = []string{"为什么", "为何", "怎么", "哪", "什么", "是否", "有没有", "多少"} +var assistantPlanQuestion = []string{"为什么", "为何", "怎么", "哪", "什么", "是否", "有没有", "多少", "谁", "了没", "吗"} func assistantPlanHasPerfective(line string) bool { for _, word := range []string{"了解", "除了", "为了"} { @@ -1508,6 +1526,75 @@ func assistantPlanProse(line string) bool { return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) } +// assistantPlanBareStep reports whether a line after a plan opening that +// names no known action or lead-in still continues the plan. Steps take +// open-ended verbs ("重启服务", "部署到测试环境", "统计各成员的提交数"), so +// instead of listing them all, such a line is a step unless it states +// something: work already done, a verdict, a total or status, a result word, +// or a statement such as "配置没改过", "负责人是 bob" or "这个连接比较慢". +// List items and "然后…" steps are judged by assistantPlanStep alone. +func assistantPlanBareStep(line string) bool { + line = strings.TrimSpace(line) + first, _ := utf8.DecodeRuneInString(line) + if !unicode.Is(unicode.Han, first) || utf8.RuneCountInString(line) > assistantPlanBareStepMax { + return assistantPlanBareStepEN(line) + } + if _, ok := cutAssistantListMarker(line); ok { + return false + } + if assistantReportsDoneWork(line) || assistantPlanHasPerfective(line) || assistantPlanStatesVerdict(line) || + assistantPlanReportHeader.MatchString(line) || assistantPlanStatesTotal(line) || assistantPlanStatesStatus(line) { + return false + } + // assistantPlanProse already accepts a precondition that plans a step; + // the rest report what followed it. + if _, matched := assistantPlanStatesPrecondition(line); matched { + return false + } + // Digits, dates and versions report what was found ("拉取最新代码耗时 5 + // 秒"); a step that names one is accepted by assistantPlanProse when it + // opens with a known action or scope ("在 v2 分支上跑测试"). A count in + // words after the verb ("合并两个分支", "回滚最近一次提交") names the + // object; one that opens the line ("两个服务都在线") is its subject. + if assistantPlanResultStart(line, false) < len(line) || assistantPlanDigitsOrDate.MatchString(line) { + return false + } + if loc := assistantPlanCJKNumber.FindStringIndex(line); loc != nil && loc[0] == 0 { + return false + } + return !assistantPlanStatement.MatchString(assistantPlanStatementNoise.Replace(line)) +} + +// assistantPlanBareStepMax bounds a bare step: a longer line without a known +// action is more likely an explanation than a step. +const assistantPlanBareStepMax = 40 + +// assistantPlanStatement marks a line that states how things are rather than +// a step to take: a subject such as 这个/当前 opening it, a copula, an +// existence or negation, a location (配置文件在…), a degree adverb before a +// quality, a judgement (应该/可能/看起来/建议), a finding, a need or +// possibility (需要/可以/即可), who owns it (由 bob 负责), an ongoing state +// (运行中/还在排队), or a comparison or state word. +var assistantPlanStatement = regexp.MustCompile(`^(?:这|那|该|它|其|当前|目前|现在|大部分|大多数|每|总体|整体|全部|都)|是|有|没|不是|不在|不会|不用|不需要|已|正在|\p{Han}在|很|非常|挺|太|比较(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)|应该|可能|看起来|看着|似乎|估计|大概|建议|推荐|因为|所以|说明|出在|落后|领先|在线|离线|才能|一样|相同|发现|得到|耗时|用时|找到|问题|需要|只需|无需|不必|即可|可以|只能|能够|导致|仍|还在|暂|最新|为空|开着|关着|不够|^(?:由|归)|负责[。.!!]?$|中[。.!!]?$`) + +// assistantPlanStatementNoise holds words that contain a statement mark +// without stating anything: "所有", an embedded yes/no question ("是否", +// "有没有", "改了没有") or a filter ("已合并的 PR"). +var assistantPlanStatementNoise = strings.NewReplacer("所有", "", "是否", "", "是不是", "", "还是", "", "有没有", "", "有无", "", "了没有", "", "了没", "", "已合并", "", "已关闭", "") + +// assistantPlanBareStepVerbEN opens an English imperative step: "Open the +// config file." plans, "It reads the config on start." describes the code. +var assistantPlanBareStepVerbEN = regexp.MustCompile(`^(?:Open|Compare|Pull|Fetch|Build|Compile|List|Update|Restart|Deploy|Install|Inspect|Search|Find|Verify|Confirm|Look|Rerun|Re-run|Diff|Sync|Merge|Rebase|Grep)\b`) + +func assistantPlanBareStepEN(line string) bool { + if !assistantPlanBareStepVerbEN.MatchString(line) || utf8.RuneCountInString(line) > assistantPlanBareStepMax*2 { + return false + } + return !assistantPlanDigitsOrDate.MatchString(line) && !assistantPlanStatementEN.MatchString(line) +} + +var assistantPlanStatementEN = regexp.MustCompile(`(?i)\b(?:is|are|was|were|found|returned|passed|failed)\b`) + // assistantPlanGoal opens a line that names the state the work is to reach. var assistantPlanGoal = []string{"确保", "保证", "务必", "Make sure", "make sure", "Ensure", "ensure"} diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index b727cf63a..c87eac36b 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -214,6 +214,9 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"scan precondition", "scan-precondition-step", `我先看一下。\n扫描到本地目录后比较差异。`}, {"completion precondition", "completion-step", `我先看一下。\n下载完成后运行脚本。`}, {"completion check", "completion-check-step", `我先看一下。\n部署完成后验证接口是否正常。`}, + {"step outside the verb list", "bare-step", `我先看一下。\n重启服务。`}, + {"embedded who question", "who-step", `我先看一下。\n看看谁改了配置。`}, + {"until goal", "until-step", `我先看一下。\n直到单测全部通过。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -231,6 +234,8 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "status-answer", `我先看一下。\n接口超时。`, "1", "接口超时") runMultiLinePlanCase(t, "clean-state-answer", `我先看一下。\n工作区已经干净。`, "1", "工作区已经干净") runMultiLinePlanCase(t, "completion-answer", `我先看一下。\n部署完成后接口返回 200。`, "1", "接口返回 200") + runMultiLinePlanCase(t, "statement-answer", `我先看一下。\n配置没改过。`, "1", "配置没改过") + runMultiLinePlanCase(t, "announced-answer", `我先说结论:\n可以合并。`, "1", "可以合并") } func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { @@ -342,6 +347,42 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n拉取最新代码耗时 5 秒。", false}, {"我先看一下。\n这次打开配置文件耗时 3 秒。", false}, {"我先看一下。\n对两个服务运行测试,发现三个错误。", false}, + // A step with an action outside assistantPlanVerb still continues + // the plan, as main did; a statement after the opener is the answer. + {"我先看一下。\n看看谁改了配置。", true}, + {"我先看一下。\n看看配置改了没有。", true}, + {"我先看一下。\n直到单测全部通过。", true}, + {"我先看一下。\n统计各成员的提交数。", true}, + {"我先看一下。\n重启服务。", true}, + {"我先看一下。\n部署到测试环境。", true}, + {"我先看一下。\n合并两个分支。", true}, + {"我先看一下。\n回滚最近一次提交。", true}, + {"我先看一下。\n调用接口获取数据。", true}, + {"我先看一下。\n在测试库里创建需求。", true}, + {"我先看一下。\n把日志发给你。", true}, + {"我先看一下。\n问一下负责人改了没有。", true}, + {"我先看一下。\n把已合并的 PR 列出来。", true}, + {"Let me first check the repo.\nOpen the config file.", true}, + {"我先看一下。\n配置文件在 config/app.yaml。", false}, + {"我先看一下。\n两个小组的成员完全一样。", false}, + {"我先看一下。\n是 alice 改的配置。", false}, + {"我先看一下。\n配置没改过。", false}, + {"我先看一下。\n负责人是 bob。", false}, + {"我先看一下。\n两个服务都在线。", false}, + {"我先看一下。\n建议用方案 A。", false}, + {"我先看一下。\n应该是网络问题。", false}, + {"我先看一下。\n总体上没什么问题。", false}, + {"我先看一下。\n分支 v2 落后 main 5 个提交。", false}, + {"我先看一下。\n仓库为空。", false}, + {"我先看一下。\n只需改一行。", false}, + {"我先看一下。\n由 bob 负责。", false}, + {"我先看一下。\n等待审批中。", false}, + {"我先看一下。\n缓存过期导致的。", false}, + {"我先总结一下:\n合并前需要补测试。", false}, + {"我先说结论:\n可以合并。", false}, + {"我先说结论。\n修改超时配置即可。", false}, + {"我先说结论:", true}, + {"Let me first check the file.\nThe config is at app.yaml.", false}, {"稍等,我先检查", true}, {"我先看看 store.py。\n接下来我会补测试。", true}, {"我先读代码。\n\n接下来我将运行测试。", true}, From 85f961a6367a5f57ac36b4e278f2e331b08c2e52 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 06:19:57 +0000 Subject: [PATCH 15/28] =?UTF-8?q?fix(assistant):=20keep=20modifier=20and?= =?UTF-8?q?=20"=E6=88=91=E5=85=88=E8=AF=B4=E4=B8=80=E4=B8=8B=EF=BC=9A"=20p?= =?UTF-8?q?lan=20lines=20on=20the=20plan=20side?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crispinhu27's re-review of 46bc676e found two ways a plan still ended the turn where main continued it: - A bare step with 有/没有 inside a modifier ("合并有冲突的分支", "处理有问题的节点") or naming 问题/最新 as its object ("更新最新版本的 依赖") read as a statement. 有/没有 before a 的 modifier now describes the object, and 问题/最新 are statement marks only where they open the line. - "我先说一下:" or "我先讲一下:" followed by a numbered plan was taken as an announced answer. Only an opening that names the answer (结论/结果/总结/汇报…) makes the next lines the answer; after the others a list item continues the plan when it names a step. Also from the review's notes: "我先说结论:可以合并。" on one line is the answer, and short outcomes after the opener ("重启服务就能恢复。", "稍后会自动恢复。", "改完记得重新部署。") are no longer continued. Lines that wait for a result ("等待 CI 结果。"), act on a status ("收集最新的报错。") or archive something ("归档…") stay steps. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 68 ++++++++++++++++--- .../httpapi/assistant_progress_text_test.go | 43 ++++++++++++ 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index d02a8dbc6..56ad48922 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1182,7 +1182,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { // result (perfective 了, counts, dates, 结论/结果…) is an answer, and an // opening that reports work already done ("我先查了…,结论如下。") is the // start of an answer, not a plan. - opened, announced := false, false + opened, announced, firm := false, false, false for line := range strings.SplitSeq(text, "\n") { line = strings.TrimSpace(line) if line == "" { @@ -1193,11 +1193,28 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { return false } opened, announced = true, assistantAnnouncesAnswer(line) + firm = announced && assistantAnswerFirm.MatchString(line) + // "我先说结论:可以合并。" carries the answer on its opening line. + if firm && assistantAnnouncementRest(line) != "" { + return false + } continue } - // "我先说结论:" followed by more lines opens the answer. if announced { - return false + // "我先说结论:" or "我先总结一下:" followed by more lines opens + // the answer. + if firm { + return false + } + // "我先说一下:" opens a list of steps as readily as an answer: + // a list item continues the plan only when it names a step to + // take, and any other line is judged as after a plan opening. + if rest, ok := cutAssistantListMarker(line); ok { + if !assistantPlanStep(line) || !(assistantPlanProse(rest) || assistantPlanBareStep(rest)) { + return false + } + continue + } } if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) && !assistantPlanBareStep(line) { return false @@ -1206,6 +1223,23 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { return opened } +// assistantAnswerFirm names the answer in an opening that introduces it +// ("我先说结论:", "我先总结一下:"); "我先说一下:" or "我先讲一下:" +// introduce a plan as readily as an answer. +var assistantAnswerFirm = regexp.MustCompile(`结论|结果|答案|现状|总结|概括|报告|汇报|回答`) + +// assistantAnnouncementRest is what follows the announcement on its own +// line, as "可以合并。" in "我先说结论:可以合并。". +func assistantAnnouncementRest(line string) string { + loc := assistantAnnouncementBreak.FindStringIndex(line) + if loc == nil { + return "" + } + return strings.TrimSpace(line[loc[1]:]) +} + +var assistantAnnouncementBreak = regexp.MustCompile(`[::,,。]`) + // assistantProgressSentence reports whether a line announces what the model // is about to do. "我先查了…" reports what it did and is not a plan. func assistantProgressSentence(text string) bool { @@ -1562,7 +1596,9 @@ func assistantPlanBareStep(line string) bool { if loc := assistantPlanCJKNumber.FindStringIndex(line); loc != nil && loc[0] == 0 { return false } - return !assistantPlanStatement.MatchString(assistantPlanStatementNoise.Replace(line)) + line = assistantPlanStatementNoise.Replace(line) + line = assistantPlanAttributive.ReplaceAllString(line, "$1") + return !assistantPlanStatement.MatchString(line) } // assistantPlanBareStepMax bounds a bare step: a longer line without a known @@ -1574,8 +1610,17 @@ const assistantPlanBareStepMax = 40 // existence or negation, a location (配置文件在…), a degree adverb before a // quality, a judgement (应该/可能/看起来/建议), a finding, a need or // possibility (需要/可以/即可), who owns it (由 bob 负责), an ongoing state -// (运行中/还在排队), or a comparison or state word. -var assistantPlanStatement = regexp.MustCompile(`^(?:这|那|该|它|其|当前|目前|现在|大部分|大多数|每|总体|整体|全部|都)|是|有|没|不是|不在|不会|不用|不需要|已|正在|\p{Han}在|很|非常|挺|太|比较(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)|应该|可能|看起来|看着|似乎|估计|大概|建议|推荐|因为|所以|说明|出在|落后|领先|在线|离线|才能|一样|相同|发现|得到|耗时|用时|找到|问题|需要|只需|无需|不必|即可|可以|只能|能够|导致|仍|还在|暂|最新|为空|开着|关着|不够|^(?:由|归)|负责[。.!!]?$|中[。.!!]?$`) +// (运行中/还在排队), an outcome to expect (重启就能恢复, 会自动重启), or a +// comparison or state word. 问题 and 最新 alone are what a step acts on +// ("排查问题根因", "拉取最新代码"). +var assistantPlanStatement = regexp.MustCompile(`^(?:这|那|该|它|其|当前|目前|现在|大部分|大多数|每|总体|整体|全部|都)|是|有|没|不是|不在|不会|不用|不需要|已|正在|\p{Han}在|很|非常|挺|太|比较(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)|应该|可能|看起来|看着|似乎|估计|大概|建议|推荐|因为|所以|^说明|说明了|出在|落后|领先|在线|离线|才能|一样|相同|发现|得到|耗时|用时|找到|^问题|需要|只需|无需|不必|即可|可以|只能|能够|导致|仍|还在|暂|为空|就能|就可以|就行|就好|就会|会自动|会恢复|会生效|记得|别忘|位于|开着|关着|不够|^由|^归[^档类纳并还零]|负责[。.!!]?$|中[。.!!]?$`) + +// assistantPlanAttributive matches 有/没有 inside a modifier after a word, +// as in "合并有冲突的分支" or "补齐没有测试的模块": it describes the +// object and states nothing. "接口有问题。" has no 的 modifier and stays a +// statement; "项目里有冲突的文件。" is read as a step and continued, which +// is what main did for every line after a plan opening. +var assistantPlanAttributive = regexp.MustCompile(`(\p{Han})(?:没有|有)[^,,。;;!!::的\s]{1,8}的`) // assistantPlanStatementNoise holds words that contain a statement mark // without stating anything: "所有", an embedded yes/no question ("是否", @@ -1633,11 +1678,11 @@ func assistantPlanStatesStatus(line string) bool { var assistantPlanStatusNoun = []string{"报错", "异常", "超时"} // assistantPlanStatusVerb is an action that takes such a noun as its object. -var assistantPlanStatusVerb = []string{"修复", "修", "排查", "定位", "复现", "分析", "处理", "解决", "查看", "检查", "调查", "跟进", "诊断", "避免", "规避", "消除", "捕获", "查", "看", "找"} +var assistantPlanStatusVerb = []string{"修复", "修", "排查", "定位", "复现", "分析", "处理", "解决", "查看", "检查", "调查", "跟进", "诊断", "避免", "规避", "消除", "捕获", "收集", "统计", "汇总", "记录", "忽略", "屏蔽", "查", "看", "找"} // assistantPlanStatusModifier may stand between the verb and its object, as // in "定位这个报错" or "处理相关的超时". -var assistantPlanStatusModifier = []string{"的", "这个", "这些", "那个", "那些", "该", "此", "相关", "所有", "全部"} +var assistantPlanStatusModifier = []string{"的", "这个", "这些", "那个", "那些", "该", "此", "相关", "所有", "全部", "最新", "最近", "新增"} func assistantPlanStatusIsObject(before, status string) bool { noun := false @@ -1781,9 +1826,16 @@ func assistantPlanResultNounAt(line string, i int) bool { return true } } + // "等待 CI 结果。" or "查看构建结果。" waits for or reads the result + // that closes the line; it reports none. + if strings.HasPrefix(line[i:], "结果") && i == strings.LastIndex(line, "结果") { + return assistantPlanAwaitResult.MatchString(line) + } return false } +var assistantPlanAwaitResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集)[^,,。;;::]*结果[。.!!]?$`) + // assistantPlanNamesAction reports whether text names a step to take. func assistantPlanNamesAction(text string) bool { for _, verb := range assistantPlanVerb { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index c87eac36b..309d73654 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -217,6 +217,8 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"step outside the verb list", "bare-step", `我先看一下。\n重启服务。`}, {"embedded who question", "who-step", `我先看一下。\n看看谁改了配置。`}, {"until goal", "until-step", `我先看一下。\n直到单测全部通过。`}, + {"attributive 有", "attributive-step", `我先看一下。\n合并有冲突的分支。`}, + {"announced plan list", "announced-plan-list", `我先说一下:\n1. 读取配置\n2. 运行测试`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -236,6 +238,8 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "completion-answer", `我先看一下。\n部署完成后接口返回 200。`, "1", "接口返回 200") runMultiLinePlanCase(t, "statement-answer", `我先看一下。\n配置没改过。`, "1", "配置没改过") runMultiLinePlanCase(t, "announced-answer", `我先说结论:\n可以合并。`, "1", "可以合并") + runMultiLinePlanCase(t, "announced-inline-answer", `我先说结论:可以合并。`, "1", "可以合并") + runMultiLinePlanCase(t, "short-verdict-answer", `我先看一下。\n重启服务就能恢复。`, "1", "重启服务就能恢复") } func runMultiLinePlanCase(t *testing.T, session, plan, wantCalls, wantBody string) { @@ -362,6 +366,45 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n把日志发给你。", true}, {"我先看一下。\n问一下负责人改了没有。", true}, {"我先看一下。\n把已合并的 PR 列出来。", true}, + // 有/没有 inside a modifier ("有冲突的分支") describes the object; + // 问题 and 最新 are nouns of the step, not statements. + {"我先看一下。\n合并有冲突的分支。", true}, + {"我先看一下。\n处理有问题的节点。", true}, + {"我先看一下。\n更新最新版本的依赖。", true}, + {"我先看一下。\n修复有问题的依赖。", true}, + {"我先看一下。\n找出有问题的提交。", true}, + {"我先看一下。\n重新部署有问题的服务。", true}, + {"我先看一下。\n补齐没有测试的模块。", true}, + {"我先看一下。\n拉取最新代码。", true}, + {"我先看一下。\n排查问题根因。", true}, + {"我先看一下。\n写一份变更说明。", true}, + {"我先看一下。\n等待 CI 结果。", true}, + {"我先看一下。\n归档没有活跃的项目。", true}, + {"我先看一下。\n收集最新的报错。", true}, + {"我先看一下。\n归 alice 负责。", false}, + {"我先看一下。\n查看构建结果。", true}, + {"我先看一下。\n等待结果:构建通过。", false}, + // "我先说一下:" opens a plan list as readily as an answer. + {"我先说一下:\n1. 读取配置\n2. 运行测试", true}, + {"我先讲一下:\n1. 先跑单测\n2. 再看日志", true}, + {"我先说一下:\n- 合并有冲突的分支\n- 重启服务", true}, + {"我先说一下:\n1. 配置文件在 config/app.yaml\n2. 负责人是 bob", false}, + {"我先说一下:\n1. 测试全过\n2. lint 干净", false}, + {"我先说一下:\n配置没改过。", false}, + {"我先总结一下:\n1. 读取配置\n2. 运行测试", false}, + {"我先说结论:\n1. 可以合并\n2. 无需改动", false}, + {"我先说结论:可以合并。", false}, + {"我先说结论,可以合并。", false}, + {"我先说结论。", true}, + // A short verdict after the opener is the answer. + {"我先看一下。\n重启服务就能恢复。", false}, + {"我先看一下。\n稍后会自动恢复。", false}, + {"我先看一下。\n改完记得重新部署。", false}, + {"我先看一下。\n配置文件位于 config 目录。", false}, + {"我先看一下。\n接口有问题。", false}, + {"我先看一下。\n依赖已是最新。", false}, + {"我先看一下。\n问题出在缓存。", false}, + {"我先看一下。\n问题不大。", false}, {"Let me first check the repo.\nOpen the config file.", true}, {"我先看一下。\n配置文件在 config/app.yaml。", false}, {"我先看一下。\n两个小组的成员完全一样。", false}, From 3f0df6f30cf22c4100e028da85880c06d774ced4 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 07:21:34 +0000 Subject: [PATCH 16/28] =?UTF-8?q?fix(assistant):=20keep=20"=E6=88=91?= =?UTF-8?q?=E5=85=88=E8=AF=B4=E7=BB=93=E8=AE=BA=EF=BC=8C=E5=86=8D=E2=80=A6?= =?UTF-8?q?"=20on=20the=20plan=20side=20when=20no=20answer=20follows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A single line that announced the answer and then went straight on to the next step ("我先说结论,再展开原因。", "我先汇报结果,接着检查日志。") was read as carrying the answer on its opening line, so the plan was stored as the final reply instead of continuing one more turn. Only treat the text after the announcement as the answer when it is not just more steps: a colon still introduces the answer, and any clause that is not a 然后/再/接着… step ("再说原因:缓存过期。", "可以合并,再补测试。") still carries it. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 36 +++++++++++++++++-- .../httpapi/assistant_progress_text_test.go | 15 ++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 56ad48922..8a3cf1f65 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1194,8 +1194,9 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } opened, announced = true, assistantAnnouncesAnswer(line) firm = announced && assistantAnswerFirm.MatchString(line) - // "我先说结论:可以合并。" carries the answer on its opening line. - if firm && assistantAnnouncementRest(line) != "" { + // "我先说结论:可以合并。" carries the answer on its opening line; + // "我先说结论,再展开原因。" only announces it and goes on planning. + if firm && assistantAnnouncementRest(line) != "" && !assistantAnnouncementPlansAhead(line) { return false } continue @@ -1240,6 +1241,37 @@ func assistantAnnouncementRest(line string) string { var assistantAnnouncementBreak = regexp.MustCompile(`[::,,。]`) +// assistantAnnouncementPlansAhead reports whether what follows the answer +// announcement on its line is only the next steps, as "再展开原因。" in +// "我先说结论,再展开原因。" or "然后继续排查日志。" in "我先说一下结论,然后 +// 继续排查日志。": the answer itself has not been written yet. A colon +// introduces the answer ("我先说结论:再等等就好。"), and every clause must +// be such a step, so "我先说结论,再说原因:缓存过期。" carries the answer. +func assistantAnnouncementPlansAhead(line string) bool { + loc := assistantAnnouncementBreak.FindStringIndex(line) + if loc == nil { + return false + } + if brk := line[loc[0]:loc[1]]; brk == ":" || brk == ":" { + return false + } + rest := strings.TrimSpace(line[loc[1]:]) + for rest != "" { + clause, next := rest, "" + if loc := assistantAnnouncementBreak.FindStringIndex(rest); loc != nil { + clause, next = rest[:loc[0]], rest[loc[1]:] + } + clause, next = strings.TrimSpace(clause), strings.TrimSpace(next) + if clause != "" { + if _, listed := cutAssistantListMarker(clause); listed || !assistantPlanStep(clause) { + return false + } + } + rest = next + } + return true +} + // assistantProgressSentence reports whether a line announces what the model // is about to do. "我先查了…" reports what it did and is not a plan. func assistantProgressSentence(text string) bool { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 309d73654..e77852014 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -219,6 +219,8 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"until goal", "until-step", `我先看一下。\n直到单测全部通过。`}, {"attributive 有", "attributive-step", `我先看一下。\n合并有冲突的分支。`}, {"announced plan list", "announced-plan-list", `我先说一下:\n1. 读取配置\n2. 运行测试`}, + {"announced answer then steps", "announced-then-steps", `我先说结论,再展开原因。`}, + {"announced result then check", "announced-result-then-check", `我先汇报结果,接着检查日志。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -396,6 +398,19 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先说结论:可以合并。", false}, {"我先说结论,可以合并。", false}, {"我先说结论。", true}, + // An announcement followed only by the next steps has not given the + // answer yet; a colon, or any clause that is not a step, has. + {"我先说结论,再展开原因。", true}, + {"我先给出结论,再说明依据。", true}, + {"我先说一下结论,然后继续排查日志。", true}, + {"我先汇报结果,接着检查日志。", true}, + {"我先说结论,再展开原因,然后给出改法。", true}, + {"我先说结论,再说原因:缓存过期。", false}, + {"我先说结论,再说原因,缓存过期。", false}, + {"我先说结论:再等等就好。", false}, + {"我先说结论,可以合并,再补测试。", false}, + {"我先说结论,再展开原因。\n可以合并。", false}, + {"我先说结论,然后跑了测试。", false}, // A short verdict after the opener is the answer. {"我先看一下。\n重启服务就能恢复。", false}, {"我先看一下。\n稍后会自动恢复。", false}, From aee85c043037914180820971159789e3f7928e68 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 16:59:27 +0800 Subject: [PATCH 17/28] fix(assistant): distinguish rerun instructions from inline conclusions --- internal/httpapi/assistant.go | 9 +++++++++ internal/httpapi/assistant_progress_text_test.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 8a3cf1f65..55f951987 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1158,6 +1158,10 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven return strings.Join(parts, "\n\n") } +// In an imperative, "failed" may describe the tests to rerun. An outcome +// such as "重跑失败" must still stop the turn. +var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)`) + func assistantProgressOnly(message string, messages []assistantMessage) bool { text := strings.TrimSpace(message) if text == "" || len([]rune(text)) > 600 || strings.Contains(text, "```") { @@ -1171,6 +1175,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } } } + text = assistantRerunFailedTarget.ReplaceAllString(text, "${1}${2}") for _, blocker := range []string{"无法", "不能", "失败", "缺少", "不可用", "需要你", "请提供", "请确认", "请选择", "?", "?", "已完成", "已生成", "已创建", "已启动", "已保存", "run-"} { if strings.Contains(text, blocker) { return false @@ -1289,6 +1294,10 @@ func assistantProgressSentence(text string) bool { var assistantAnswerOpening = regexp.MustCompile(`^(?:我先|让我先|我会先)?(?:说|讲|给|报告|汇报|总结|概括|回答|简单说)(?:一下)?(?:结论|结果|答案|现状)?\s*[::,,。]?\s*$|^(?:我先|让我先)(?:说|讲|给出?|汇报)(?:一下)?(?:结论|结果|答案)`) func assistantAnnouncesAnswer(line string) bool { + // Identify the announcement independently of the inline answer after it. + if loc := assistantAnnouncementBreak.FindStringIndex(line); loc != nil { + line = line[:loc[0]] + } for _, prefix := range []string{"好的,我先", "我先", "让我先", "我会先"} { if rest, ok := strings.CutPrefix(line, prefix); ok { return assistantAnswerOpening.MatchString(rest) || assistantAnswerOpening.MatchString(line) diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index e77852014..31ab9bf4b 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -219,6 +219,7 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"until goal", "until-step", `我先看一下。\n直到单测全部通过。`}, {"attributive 有", "attributive-step", `我先看一下。\n合并有冲突的分支。`}, {"announced plan list", "announced-plan-list", `我先说一下:\n1. 读取配置\n2. 运行测试`}, + {"rerun failed cases", "rerun-failed-cases", `我先看一下。\n重跑有失败的用例。`}, {"announced answer then steps", "announced-then-steps", `我先说结论,再展开原因。`}, {"announced result then check", "announced-result-then-check", `我先汇报结果,接着检查日志。`}, } { @@ -231,6 +232,7 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { // A reply that opens like a plan but states a verdict ("测试全过。") is the // answer: it is delivered as is, without running the CLI again. func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { + runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") runMultiLinePlanCase(t, "report-header-answer", `我先跑一下检查。\n检查结果:共 3 处告警,都在 store 包。`, "1", "都在 store 包") @@ -411,6 +413,13 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先说结论,可以合并,再补测试。", false}, {"我先说结论,再展开原因。\n可以合并。", false}, {"我先说结论,然后跑了测试。", false}, + {"我先看一下。\n重跑失败的测试用例。", true}, + {"我先看一下。\n重跑有失败的用例。", true}, + {"我先看一下。\n重跑失败的用例,仍然失败。", false}, + {"我先看一下。\n重跑失败。", false}, + {"我先看一下。\n项目里有失败的用例。", false}, + {"我先总结一下,项目里有 4 个 LOOP。", false}, + {"我先总结一下,然后列出原因。", true}, // A short verdict after the opener is the answer. {"我先看一下。\n重启服务就能恢复。", false}, {"我先看一下。\n稍后会自动恢复。", false}, From 630c695ae54dcee13f93d23f663a79bc89b964ee Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 09:15:04 +0000 Subject: [PATCH 18/28] fix(assistant): keep a step that acts on its result on the plan side MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A result word the step acts on no longer reads as a reported result: "把结果整理成表格。", "把结论写进文档。", "输出最终结果。" and "对比两次运行 的结果。" take 结果/结论 as their object, an opening "显示所有成员。" is the imperative, and "等构建完成。" waits for the completion. 对比 at the start of a line is the verb, not 对 plus a scope, so "对比结果一致。" reports the comparison. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 42 ++++++++++++++++--- .../httpapi/assistant_progress_text_test.go | 25 +++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 55f951987..8bc0b85e2 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1532,7 +1532,9 @@ var assistantPlanClause = regexp.MustCompile(`[,,。;;!!]`) // These imperatives can follow a project, branch or object before the verb. // Digits in that scope (such as "在 v2 分支上") are part of the target. -var assistantPlanCommand = regexp.MustCompile(`^(?:(?:在|对|把)[^,,。;;::!?!?\n]+?(?:跑|运行|执行|检查|验证|核对|列出|对比|比较|拉取|打开|编译)|(?:比较|拉取|打开|编译))`) +// A line opening with 对比 starts with the verb itself, not with 对 and a +// scope ("对比两次运行的结果" is not 对 + "比两次运行"). +var assistantPlanCommand = regexp.MustCompile(`^(?:(?:(?:在|把)[^,,。;;::!?!?\n]+?|对[^比,,。;;::!?!?\n][^,,。;;::!?!?\n]*?)(?:跑|运行|执行|检查|验证|核对|列出|对比|比较|拉取|打开|编译)|(?:比较|对比|拉取|打开|编译))`) // 比较 also modifies adjectives ("比较稳定" describes a result). var assistantPlanComparisonDegree = regexp.MustCompile(`^(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)(?:[,,。;;!!]|$)`) @@ -1867,15 +1869,43 @@ func assistantPlanResultNounAt(line string, i int) bool { return true } } - // "等待 CI 结果。" or "查看构建结果。" waits for or reads the result - // that closes the line; it reports none. - if strings.HasPrefix(line[i:], "结果") && i == strings.LastIndex(line, "结果") { - return assistantPlanAwaitResult.MatchString(line) + clause := line[:i] + if loc := assistantPlanClause.FindAllStringIndex(clause, -1); len(loc) > 0 { + clause = clause[loc[len(loc)-1][1]:] + } + clause = strings.TrimSpace(clause) + switch { + case strings.HasPrefix(line[i:], "结果") || strings.HasPrefix(line[i:], "结论"): + // "把结果整理成表格。" or "把结论写进文档。" acts on the result: 把 + // puts it before the verb as the object of the step. + if strings.Contains(clause, "把") { + return true + } + // "等待 CI 结果。", "查看构建结果。" or "输出最终结果。" waits for, + // reads or writes the result that closes the line; it reports none. + end := strings.TrimSpace(line[i+len("结果"):]) + return (end == "" || assistantPlanClauseEnd.MatchString(end)) && assistantPlanActOnResult.MatchString(clause) + case strings.HasPrefix(line[i:], "显示"): + // "显示所有成员。" opens with the imperative; "日志显示…" or "结果 + // 显示…" reports what something shows. + return i == 0 + case strings.HasPrefix(line[i:], "完成"): + // "等构建完成。" or "等待 CI 完成。" waits for the completion. + return assistantPlanAwaitStart.MatchString(clause) } return false } -var assistantPlanAwaitResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集)[^,,。;;::]*结果[。.!!]?$`) +// assistantPlanClauseEnd is the punctuation that may close a clause. +var assistantPlanClauseEnd = regexp.MustCompile(`^[。.!!]$`) + +// assistantPlanActOnResult opens a clause whose action takes the result that +// closes it as its object. +var assistantPlanActOnResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|汇报|报告|告诉你?)`) + +// assistantPlanAwaitStart opens a clause that waits for something to happen +// ("等构建完成"); "等了很久才完成" reports the wait and carries 了. +var assistantPlanAwaitStart = regexp.MustCompile(`^(?:等待?|等到|等一下)[^了]*$`) // assistantPlanNamesAction reports whether text names a step to take. func assistantPlanNamesAction(text string) bool { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 31ab9bf4b..cf97563a2 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -222,6 +222,10 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"rerun failed cases", "rerun-failed-cases", `我先看一下。\n重跑有失败的用例。`}, {"announced answer then steps", "announced-then-steps", `我先说结论,再展开原因。`}, {"announced result then check", "announced-result-then-check", `我先汇报结果,接着检查日志。`}, + {"result as 把 object", "result-object-step", `我先看一下。\n把结果整理成表格。`}, + {"result closing a step", "result-closing-step", `我先看一下。\n输出最终结果。`}, + {"imperative 显示", "show-step", `我先看一下。\n显示所有成员。`}, + {"wait for completion", "await-completion-step", `我先看一下。\n等构建完成。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -420,6 +424,27 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, {"我先总结一下,然后列出原因。", true}, + // A result word the step acts on (把结果…, 输出…结果, 等…完成) or an + // imperative 显示 keeps the step on the plan side. + {"我先看一下。\n把结果整理成表格。", true}, + {"我先看一下。\n把结果发给你。", true}, + {"我先看一下。\n输出最终结果。", true}, + {"我先看一下。\n对比两次运行的结果。", true}, + {"我先看一下。\n把结论写进文档。", true}, + {"我先看一下。\n显示所有成员。", true}, + {"我先看一下。\n等构建完成。", true}, + {"我先看一下。\n等待 CI 完成。", true}, + {"我先看一下。\n结果显示 3 个失败。", false}, + {"我先看一下。\n对比结果一致。", false}, + {"我先看一下。\n对比结果:两个分支相同。", false}, + {"我先看一下。\n日志显示连接超时。", false}, + {"我先看一下。\n显示的是旧版本。", false}, + {"我先看一下。\n页面显示 5 个成员。", false}, + {"我先看一下。\n输出结果是空的。", false}, + {"我先看一下。\n构建已经完成。", false}, + {"我先看一下。\n部署完成。", false}, + {"我先看一下。\n构建等了很久才完成。", false}, + {"我先看一下。\n把结果看完了,都没问题。", false}, // A short verdict after the opener is the answer. {"我先看一下。\n重启服务就能恢复。", false}, {"我先看一下。\n稍后会自动恢复。", false}, From f637a7cda65b36d32141b48e28daa8cd791df28e Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 09:43:21 +0000 Subject: [PATCH 19/28] fix(assistant): keep steps that export, save or return a result on the plan side MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "导出结果到 CSV。", "保存结果到文件。", "上传结果。", "打印结果。", "回复结论。" and "返回结果。" act on the result like "输出最终结果。" did, so they continue the plan. A result followed by where it goes (到/给/进…) is still a step; "结果为空" or "接口返回 404" still report. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 16 +++++++++++----- internal/httpapi/assistant_progress_text_test.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 8bc0b85e2..3774c62aa 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1883,11 +1883,13 @@ func assistantPlanResultNounAt(line string, i int) bool { } // "等待 CI 结果。", "查看构建结果。" or "输出最终结果。" waits for, // reads or writes the result that closes the line; it reports none. + // "导出结果到 CSV。" or "把结果发给你。" names where the result goes. end := strings.TrimSpace(line[i+len("结果"):]) - return (end == "" || assistantPlanClauseEnd.MatchString(end)) && assistantPlanActOnResult.MatchString(clause) - case strings.HasPrefix(line[i:], "显示"): - // "显示所有成员。" opens with the imperative; "日志显示…" or "结果 - // 显示…" reports what something shows. + return (end == "" || assistantPlanClauseEnd.MatchString(end) || assistantPlanResultDestination.MatchString(end)) && + assistantPlanActOnResult.MatchString(clause) + case strings.HasPrefix(line[i:], "显示"), strings.HasPrefix(line[i:], "返回"): + // "显示所有成员。" or "返回结果。" opens with the imperative; "日志 + // 显示…" or "接口返回…" reports what something shows or returns. return i == 0 case strings.HasPrefix(line[i:], "完成"): // "等构建完成。" or "等待 CI 完成。" waits for the completion. @@ -1901,7 +1903,11 @@ var assistantPlanClauseEnd = regexp.MustCompile(`^[。.!!]$`) // assistantPlanActOnResult opens a clause whose action takes the result that // closes it as its object. -var assistantPlanActOnResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|汇报|报告|告诉你?)`) +var assistantPlanActOnResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|写入|汇报|报告|告诉你?|导出|保存|存下?|上传|下载|打印|回复|返回|提交|同步|推送|转发|附上|粘贴|复制|反馈|分享|公布|呈现)`) + +// assistantPlanResultDestination follows a result a step sends somewhere, +// as "到 CSV" in "导出结果到 CSV。"; "结果是…" or "结果为空" report it. +var assistantPlanResultDestination = regexp.MustCompile(`^(?:到|至|给|进|入)`) // assistantPlanAwaitStart opens a clause that waits for something to happen // ("等构建完成"); "等了很久才完成" reports the wait and carries 了. diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index cf97563a2..83e7109c9 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -226,6 +226,7 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"result closing a step", "result-closing-step", `我先看一下。\n输出最终结果。`}, {"imperative 显示", "show-step", `我先看一下。\n显示所有成员。`}, {"wait for completion", "await-completion-step", `我先看一下。\n等构建完成。`}, + {"result sent somewhere", "result-destination-step", `我先看一下。\n导出结果到 CSV。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -434,6 +435,17 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n显示所有成员。", true}, {"我先看一下。\n等构建完成。", true}, {"我先看一下。\n等待 CI 完成。", true}, + {"我先看一下。\n导出结果到 CSV。", true}, + {"我先看一下。\n保存结果到文件。", true}, + {"我先看一下。\n上传结果。", true}, + {"我先看一下。\n打印结果。", true}, + {"我先看一下。\n回复结论。", true}, + {"我先看一下。\n返回结果。", true}, + {"我先看一下。\n接口返回结果为空。", false}, + {"我先看一下。\n返回结果是空的。", false}, + {"我先看一下。\n导出的结果在 CSV 里。", false}, + {"我先看一下。\n导出结果为 3 行。", false}, + {"我先看一下。\n接口返回 404。", false}, {"我先看一下。\n结果显示 3 个失败。", false}, {"我先看一下。\n对比结果一致。", false}, {"我先看一下。\n对比结果:两个分支相同。", false}, From 9c73e464468cd74a34a194d28b37840e3635bf34 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 20:35:20 +0800 Subject: [PATCH 20/28] fix(assistant): distinguish completed reruns from a promised conclusion --- internal/httpapi/assistant.go | 19 ++++++++++++++----- .../httpapi/assistant_progress_text_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 78ad0f79e..239d15b7f 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1204,9 +1204,10 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven return strings.Join(parts, "\n\n") } -// In an imperative, "failed" may describe the tests to rerun. An outcome -// such as "重跑失败" must still stop the turn. -var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)`) +// In an imperative, "failed" may describe the tests to rerun. The exception +// is preserved only for a standalone instruction or its next planned step. +// A trailing outcome ("重跑失败的用例后还是报错") must be delivered. +var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(?:再|然后|接着|随后))`) func assistantProgressOnly(message string, messages []assistantMessage) bool { text := strings.TrimSpace(message) @@ -1221,7 +1222,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } } } - text = assistantRerunFailedTarget.ReplaceAllString(text, "${1}${2}") + text = assistantRerunFailedTarget.ReplaceAllString(text, "${1}${2}${3}") for _, blocker := range []string{"无法", "不能", "失败", "缺少", "不可用", "需要你", "请提供", "请确认", "请选择", "?", "?", "已完成", "已生成", "已创建", "已启动", "已保存", "run-"} { if strings.Contains(text, blocker) { return false @@ -1523,6 +1524,8 @@ func assistantPlanPassVerdict(line string) (string, bool) { // what was found (conclusions) instead of what is next. var assistantPlanResultMark = []string{"结论", "结果", "如下", "共", "只有", "一致", "返回", "显示", "分别", "已经", "完成", "失败", "正常"} +var assistantPlanListRange = regexp.MustCompile(`^(?:显示|返回)(?:最近|最新|最早|前|最后)(?:的)?\s*(?:\d+|[一二两三四五六七八九十]+)\s*[条次个项行名份]`) + // assistantPlanResultNoun is a noun that contains a result mark without // reporting anything ("把返回值和文档核对" plans a check). var assistantPlanResultNoun = []string{"返回值"} @@ -1644,6 +1647,12 @@ func assistantPlanProse(line string) bool { // 最近 3 次提交" or "读一下 v2 的配置"; "项目里有 4 个 LOOP,都在运 // 行。" reports what was found. if assistantPlanCJKNumber.MatchString(line) || assistantPlanDigitsOrDate.MatchString(line) { + // A count can limit the requested list rather than report a result. + if loc := assistantPlanListRange.FindStringIndex(line); loc != nil { + object := line[loc[1]:] + return !assistantPlanStatement.MatchString(object) && !assistantPlanStatesStatus(object) && + assistantPlanResultStart(object, false) == len(object) + } return assistantPlanOpensWithAction(line) && assistantPlanNamesAction(line[:assistantPlanResultStart(line, true)]) } return assistantPlanNamesAction(line[:assistantPlanResultStart(line, false)]) @@ -1949,7 +1958,7 @@ var assistantPlanClauseEnd = regexp.MustCompile(`^[。.!!]$`) // assistantPlanActOnResult opens a clause whose action takes the result that // closes it as its object. -var assistantPlanActOnResult = regexp.MustCompile(`^(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|写入|汇报|报告|告诉你?|导出|保存|存下?|上传|下载|打印|回复|返回|提交|同步|推送|转发|附上|粘贴|复制|反馈|分享|公布|呈现)`) +var assistantPlanActOnResult = regexp.MustCompile(`^(?:(?:稍后|随后|接着|然后|再)\s*)*(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|写入|汇报|报告|告诉你?|导出|保存|存下?|上传|下载|打印|回复|返回|提交|同步|推送|转发|附上|粘贴|复制|反馈|分享|公布|呈现)`) // assistantPlanResultDestination follows a result a step sends somewhere, // as "到 CSV" in "导出结果到 CSV。"; "结果是…" or "结果为空" report it. diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 83e7109c9..9d3f598ce 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -219,6 +219,7 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"until goal", "until-step", `我先看一下。\n直到单测全部通过。`}, {"attributive 有", "attributive-step", `我先看一下。\n合并有冲突的分支。`}, {"announced plan list", "announced-plan-list", `我先说一下:\n1. 读取配置\n2. 运行测试`}, + {"deferred conclusion", "later-conclusion", `我先看一下。\n稍后给出结论。`}, {"rerun failed cases", "rerun-failed-cases", `我先看一下。\n重跑有失败的用例。`}, {"announced answer then steps", "announced-then-steps", `我先说结论,再展开原因。`}, {"announced result then check", "announced-result-then-check", `我先汇报结果,接着检查日志。`}, @@ -237,6 +238,7 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { // A reply that opens like a plan but states a verdict ("测试全过。") is the // answer: it is delivered as is, without running the CLI again. func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { + runMultiLinePlanCase(t, "failed-rerun-outcome", `我先看一下。\n重跑失败的用例后还是报错。`, "1", "还是报错") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") @@ -421,6 +423,13 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试用例。", true}, {"我先看一下。\n重跑有失败的用例。", true}, {"我先看一下。\n重跑失败的用例,仍然失败。", false}, + {"我先看一下。\n重跑失败的用例后还是报错。", false}, + {"我先看一下。\n重跑失败的测试后问题依旧。", false}, + {"我先看一下。\n重新运行失败的测试,依然超时。", false}, + {"我先看一下。\n重新执行失败的检查,仍未通过。", false}, + {"我先看一下。\n重跑失败的测试后全部变绿。", false}, + {"我先看一下。\n重跑失败的用例,报错没有变化。", false}, + {"我先看一下。\n重跑失败的用例,再查看日志。", true}, {"我先看一下。\n重跑失败。", false}, {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, @@ -433,6 +442,11 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n对比两次运行的结果。", true}, {"我先看一下。\n把结论写进文档。", true}, {"我先看一下。\n显示所有成员。", true}, + {"我先看一下。\n稍后给出结论。", true}, + {"我先看一下。\n等待构建结束,再给出结论。", true}, + {"我先看一下。\n显示最近 3 次提交。", true}, + {"我先看一下。\n返回最近 3 条记录。", true}, + {"我先看一下。\n返回最近 3 条记录时出错了。", false}, {"我先看一下。\n等构建完成。", true}, {"我先看一下。\n等待 CI 完成。", true}, {"我先看一下。\n导出结果到 CSV。", true}, From 96fbc8034ffd92d9f4f07440241fe52c96616690 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 14:27:53 +0000 Subject: [PATCH 21/28] fix(assistant): keep a rerun outcome after a connector from reading as a plan step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit '重跑失败的测试,再次超时' matched the connector branch of the rerun exception, so '失败的' was dropped and the outcome was judged a plan. The connector now keeps the exception only when the clause after it is itself a plan step; 再次/又/发现/还是… report how the rerun went. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 53 ++++++++++++++++++- .../httpapi/assistant_progress_text_test.go | 17 ++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 239d15b7f..9a4cd2d62 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1207,7 +1207,56 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven // In an imperative, "failed" may describe the tests to rerun. The exception // is preserved only for a standalone instruction or its next planned step. // A trailing outcome ("重跑失败的用例后还是报错") must be delivered. -var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(?:再|然后|接着|随后))`) +var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(再|然后|接着|随后))`) + +// assistantRerunOutcome opens what follows a connector when it reports how +// the rerun went rather than naming the next step: "再次超时", "然后又卡住", +// "接着发现还是报错". +var assistantRerunOutcome = regexp.MustCompile(`^(?:次|一次|又|也|还|仍|依然|依旧|照样|同样|发现|结果|就|却|竟|全部|都)`) + +// assistantRerunSentenceEnd ends the clause after a connector that +// assistantRerunConnectsStep judges. +var assistantRerunSentenceEnd = regexp.MustCompile(`[。.!!;;\n]`) + +// stripAssistantRerunFailedTarget drops "失败的" from a rerun instruction so +// the blocker list does not read it as a failure report. A connector keeps +// it a plan only when the clause after it is itself a step ("重跑失败的用 +// 例,再查看日志。"); "重跑失败的测试,再次超时。" reports the outcome. +func stripAssistantRerunFailedTarget(text string) string { + matches := assistantRerunFailedTarget.FindAllStringSubmatchIndex(text, -1) + if len(matches) == 0 { + return text + } + var b strings.Builder + last := 0 + for _, m := range matches { + if m[8] >= 0 { + rest := text[m[9]:] + if loc := assistantRerunSentenceEnd.FindStringIndex(rest); loc != nil { + rest = rest[:loc[0]] + } + rest = strings.TrimSpace(rest) + connector := text[m[8]:m[9]] + if !assistantRerunConnectsStep(connector, rest) { + continue + } + } + b.WriteString(text[last:m[0]]) + b.WriteString(text[m[2]:m[3]]) + b.WriteString(text[m[4]:m[5]]) + b.WriteString(text[m[6]:m[7]]) + last = m[1] + } + b.WriteString(text[last:]) + return b.String() +} + +func assistantRerunConnectsStep(connector, rest string) bool { + if rest == "" || assistantRerunOutcome.MatchString(rest) { + return false + } + return assistantPlanStep(connector+rest) && (assistantPlanProse(rest) || assistantPlanBareStep(rest)) +} func assistantProgressOnly(message string, messages []assistantMessage) bool { text := strings.TrimSpace(message) @@ -1222,7 +1271,7 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } } } - text = assistantRerunFailedTarget.ReplaceAllString(text, "${1}${2}${3}") + text = stripAssistantRerunFailedTarget(text) for _, blocker := range []string{"无法", "不能", "失败", "缺少", "不可用", "需要你", "请提供", "请确认", "请选择", "?", "?", "已完成", "已生成", "已创建", "已启动", "已保存", "run-"} { if strings.Contains(text, blocker) { return false diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 9d3f598ce..fd5c323c6 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -239,6 +239,8 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { // answer: it is delivered as is, without running the CLI again. func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-outcome", `我先看一下。\n重跑失败的用例后还是报错。`, "1", "还是报错") + runMultiLinePlanCase(t, "failed-rerun-again", `我先看一下。\n重跑失败的测试,再次超时。`, "1", "再次超时") + runMultiLinePlanCase(t, "failed-rerun-found", `我先看一下。\n重跑失败的用例,然后发现还是报错。`, "1", "还是报错") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") @@ -430,6 +432,21 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试后全部变绿。", false}, {"我先看一下。\n重跑失败的用例,报错没有变化。", false}, {"我先看一下。\n重跑失败的用例,再查看日志。", true}, + {"我先看一下。\n重跑失败的用例后再查看日志。", true}, + {"我先看一下。\n重跑失败的测试,然后对比两次结果。", true}, + {"我先看一下。\n重试失败的任务,接着检查日志。", true}, + {"我先看一下。\n重跑失败的用例,再查看日志,确认原因。", true}, + // A connector followed by how the rerun went reports the outcome. + {"我先看一下。\n重跑失败的测试,再次超时。", false}, + {"我先看一下。\n重跑失败的用例后再次报错。", false}, + {"我先看一下。\n重试失败的任务,然后又卡住。", false}, + {"我先看一下。\n重跑失败的用例,然后发现还是报错。", false}, + {"我先看一下。\n重新运行失败的测试,再次报同样的错。", false}, + {"我先看一下。\n重跑失败的检查,再次报红。", false}, + {"我先看一下。\n重跑失败的测试,接着又超时。", false}, + {"我先看一下。\n重跑失败的测试,然后超时了。", false}, + {"我先看一下。\n重跑失败的用例,随后全部通过。", false}, + {"我先看一下。\n重跑失败的用例,然后仍然报错。", false}, {"我先看一下。\n重跑失败。", false}, {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, From dabf264271895d371247ccca7d4ff685f9e672a5 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 15:09:17 +0000 Subject: [PATCH 22/28] fix(assistant): read a rerun outcome anywhere in the clause after a connector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit '重跑失败的测试,再跑还是红' and '…再查日志发现是端口冲突' carry the result after the clause's own verb, so checking only its opening still dropped '失败的'. Outcome words now count anywhere in the clause; verdict words (是/全部/都/正常…) count unless the clause checks for them or asks a question ('然后确认是否全部通过', '再查一下原因是什么'). Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 34 ++++++++++++++++++- .../httpapi/assistant_progress_text_test.go | 14 ++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 9a4cd2d62..2bb0fec92 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1251,8 +1251,40 @@ func stripAssistantRerunFailedTarget(text string) string { return b.String() } +// assistantRerunOutcomeWord reports how the rerun went wherever it appears +// in the clause: "再跑还是红", "再查日志发现是端口冲突". +var assistantRerunOutcomeWord = []string{"还是", "仍", "依然", "依旧", "照样", "又", "再次", "发现", "表明", "原来", "变绿", "变红"} + +// assistantRerunVerdictWord states a result unless the clause is checking for +// it ("然后确认是否全部通过"): "然后排查出是依赖版本不对", "然后运行结果全部正常". +var assistantRerunVerdictWord = []string{"是", "全部", "都", "正常", "成功", "没问题", "一样"} + +// assistantRerunQuestion asks what the next step looks for ("再查一下原因是 +// 什么"), so a verdict word in it is not a result. +var assistantRerunQuestion = regexp.MustCompile(`什么|哪[个些里]|为什么|怎么|多少`) + +func assistantRerunReportsOutcome(rest string) bool { + if assistantRerunOutcome.MatchString(rest) { + return true + } + for _, word := range assistantRerunOutcomeWord { + if strings.Contains(rest, word) { + return true + } + } + if assistantPlanCheckIntentIn(rest) || assistantRerunQuestion.MatchString(rest) { + return false + } + for _, word := range assistantRerunVerdictWord { + if strings.Contains(rest, word) { + return true + } + } + return false +} + func assistantRerunConnectsStep(connector, rest string) bool { - if rest == "" || assistantRerunOutcome.MatchString(rest) { + if rest == "" || assistantRerunReportsOutcome(rest) { return false } return assistantPlanStep(connector+rest) && (assistantPlanProse(rest) || assistantPlanBareStep(rest)) diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index fd5c323c6..60c84a425 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -241,6 +241,8 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-outcome", `我先看一下。\n重跑失败的用例后还是报错。`, "1", "还是报错") runMultiLinePlanCase(t, "failed-rerun-again", `我先看一下。\n重跑失败的测试,再次超时。`, "1", "再次超时") runMultiLinePlanCase(t, "failed-rerun-found", `我先看一下。\n重跑失败的用例,然后发现还是报错。`, "1", "还是报错") + runMultiLinePlanCase(t, "failed-rerun-cause", `我先看一下。\n重跑失败的用例,再查日志发现是端口冲突。`, "1", "端口冲突") + runMultiLinePlanCase(t, "failed-rerun-still-red", `我先看一下。\n重跑失败的测试,再跑还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") @@ -447,6 +449,18 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试,然后超时了。", false}, {"我先看一下。\n重跑失败的用例,随后全部通过。", false}, {"我先看一下。\n重跑失败的用例,然后仍然报错。", false}, + // The outcome may come after the clause's own verb. + {"我先看一下。\n重跑失败的测试,再跑还是红。", false}, + {"我先看一下。\n重跑失败的用例,然后日志里还是同一个断言错误。", false}, + {"我先看一下。\n重跑失败的用例,再查日志发现是端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后排查出是依赖版本不对。", false}, + {"我先看一下。\n重跑失败的用例,然后运行结果全部正常。", false}, + {"我先看一下。\n重跑失败的测试,然后定位到是超时配置太短。", false}, + // A verdict the next step still checks for keeps it a plan. + {"我先看一下。\n重跑失败的用例,然后确认是否全部通过。", true}, + {"我先看一下。\n重跑失败的测试,再看看日志是否正常。", true}, + {"我先看一下。\n重跑失败的用例,然后修复报错。", true}, + {"我先看一下。\n重跑失败的测试,接着定位超时原因。", true}, {"我先看一下。\n重跑失败。", false}, {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, From 83d928f41609a6de5bd6f30cf3e43316db53b4f4 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 16:28:38 +0000 Subject: [PATCH 23/28] =?UTF-8?q?fix(assistant):=20keep=20a=20rerun's=20?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=20unless=20the=20next=20clause=20is=20clearl?= =?UTF-8?q?y=20a=20step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crispinhu27's re-test on dabf2642 still found continuations that report how the rerun went ("然后确认是端口冲突", "再跑一遍也报错", "然后定位到超时配置 太短", "重跑失败的测试。\n还是红。") read as plan steps, which re-ran the turn and could end in HTTP 502. Invert the rule instead of growing the outcome word list: 失败的 is dropped only when every comma clause after the connector opens with a step verb, has no result complement (到/出/下来/了/完) and states no verdict it does not ask about. A standalone rerun instruction keeps the exception only when it ends the reply or the next line is itself such a step. Everything else keeps 失败 and is delivered, as on main. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 138 ++++++++++++++---- .../httpapi/assistant_progress_text_test.go | 27 ++++ 2 files changed, 139 insertions(+), 26 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 2bb0fec92..71d125b7a 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1209,19 +1209,13 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven // A trailing outcome ("重跑失败的用例后还是报错") must be delivered. var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(再|然后|接着|随后))`) -// assistantRerunOutcome opens what follows a connector when it reports how -// the rerun went rather than naming the next step: "再次超时", "然后又卡住", -// "接着发现还是报错". -var assistantRerunOutcome = regexp.MustCompile(`^(?:次|一次|又|也|还|仍|依然|依旧|照样|同样|发现|结果|就|却|竟|全部|都)`) - -// assistantRerunSentenceEnd ends the clause after a connector that -// assistantRerunConnectsStep judges. -var assistantRerunSentenceEnd = regexp.MustCompile(`[。.!!;;\n]`) - // stripAssistantRerunFailedTarget drops "失败的" from a rerun instruction so -// the blocker list does not read it as a failure report. A connector keeps -// it a plan only when the clause after it is itself a step ("重跑失败的用 -// 例,再查看日志。"); "重跑失败的测试,再次超时。" reports the outcome. +// the blocker list does not read it as a failure report. It does so only when +// what follows is clearly the next step ("重跑失败的用例,再查看日志。"); any +// other continuation ("重跑失败的测试,再次超时。", "…,然后定位到超时配置太 +// 短。") keeps 失败, so the reply is delivered as main did. A standalone +// instruction keeps the exception only when it ends the reply or the next +// line is itself a step: "重跑失败的测试。\n还是红。" reports the outcome. func stripAssistantRerunFailedTarget(text string) string { matches := assistantRerunFailedTarget.FindAllStringSubmatchIndex(text, -1) if len(matches) == 0 { @@ -1240,6 +1234,8 @@ func stripAssistantRerunFailedTarget(text string) string { if !assistantRerunConnectsStep(connector, rest) { continue } + } else if !assistantRerunStandaloneStep(text[m[1]:]) { + continue } b.WriteString(text[last:m[0]]) b.WriteString(text[m[2]:m[3]]) @@ -1251,17 +1247,115 @@ func stripAssistantRerunFailedTarget(text string) string { return b.String() } +// assistantRerunStandaloneStep reports whether a standalone rerun instruction +// is still a plan: it ends the reply, or the next line is the next step. +func assistantRerunStandaloneStep(after string) bool { + for line := range strings.SplitSeq(after, "\n") { + line = strings.TrimSpace(line) + if line == "" { + continue + } + if rest, ok := cutAssistantListMarker(line); ok { + line = rest + } + line = strings.TrimRight(line, "。.!!;;") + return assistantRerunPlanClauses(line) + } + return true +} + +// assistantRerunOutcome opens what follows a connector when it reports how +// the rerun went rather than naming the next step: "再次超时", "然后又卡住", +// "接着发现还是报错". +var assistantRerunOutcome = regexp.MustCompile(`^(?:次|一次|又|也|还|仍|依然|依旧|照样|同样|发现|结果|就|却|竟|全部|都)`) + +// assistantRerunSentenceEnd ends the clause after a connector that +// assistantRerunConnectsStep judges. +var assistantRerunSentenceEnd = regexp.MustCompile(`[。.!!;;\n]`) + // assistantRerunOutcomeWord reports how the rerun went wherever it appears // in the clause: "再跑还是红", "再查日志发现是端口冲突". var assistantRerunOutcomeWord = []string{"还是", "仍", "依然", "依旧", "照样", "又", "再次", "发现", "表明", "原来", "变绿", "变红"} -// assistantRerunVerdictWord states a result unless the clause is checking for -// it ("然后确认是否全部通过"): "然后排查出是依赖版本不对", "然后运行结果全部正常". -var assistantRerunVerdictWord = []string{"是", "全部", "都", "正常", "成功", "没问题", "一样"} +// assistantRerunPlanVerb opens a clause that names the next step after a +// rerun. Anything else after a connector ("再跑一遍也报错", "然后全绿", +// "然后报错消失") is read as the outcome. +var assistantRerunPlanVerb = regexp.MustCompile(`^(?:查看|查一下|查查|查|检查|核对|核查|对比|比对|比较|修复|修改|修正|修|定位|排查|整理|分析|汇总|收集|读取|阅读|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查|记录|更新|调整|补充|补上|重跑|重试|重新运行|重新执行)`) + +// assistantRerunResultComplement turns a step verb into a finding: "定位到", +// "排查出", "验证下来", "查了". +var assistantRerunResultComplement = []string{"到", "出", "下来", "了", "完"} + +// assistantRerunVerdictWord states a result unless the clause asks about it +// ("然后确认是否全部通过"): "然后确认是端口冲突", "再检查一遍全部正常". +var assistantRerunVerdictWord = []string{"是", "全部", "都", "正常", "成功", "没问题", "一样", "相同", "通过", "失败", "报错", "超时", "被"} // assistantRerunQuestion asks what the next step looks for ("再查一下原因是 -// 什么"), so a verdict word in it is not a result. -var assistantRerunQuestion = regexp.MustCompile(`什么|哪[个些里]|为什么|怎么|多少`) +// 什么", "再看看日志是否正常"), so a verdict word in it is not a result. +var assistantRerunQuestion = regexp.MustCompile(`是否|是不是|有没有|能否|能不能|会不会|吗|什么|哪[个些里]|为什么|怎么|多少`) + +// assistantRerunStepConnector opens a later clause of the same plan step. +var assistantRerunStepConnector = regexp.MustCompile(`^(?:然后|接着|随后|之后|再|并且|并|同时)\s*`) + +// assistantRerunClauseSep splits a continuation into its comma clauses. +var assistantRerunClauseSep = regexp.MustCompile(`[,,、]`) + +// assistantRerunPlanClauses reports whether every comma clause of rest names +// a next step: it opens with a step verb, has no result complement, and +// states no verdict except one it asks about. +func assistantRerunPlanClauses(rest string) bool { + rest = strings.TrimSpace(rest) + if rest == "" { + return false + } + for _, clause := range assistantRerunClauseSep.Split(rest, -1) { + clause = strings.TrimSpace(clause) + clause = assistantRerunStepConnector.ReplaceAllString(clause, "") + if clause == "" { + continue + } + loc := assistantRerunPlanVerb.FindStringIndex(clause) + if loc == nil { + return false + } + if assistantRerunOutcome.MatchString(clause) { + return false + } + for _, word := range assistantRerunOutcomeWord { + if strings.Contains(clause, word) { + return false + } + } + for _, word := range assistantRerunResultComplement { + if strings.Contains(clause, word) { + return false + } + } + if !assistantRerunQuestion.MatchString(clause) { + // The step's own object may name what failed ("修复报错", + // "定位超时原因"); only a verdict after the object is a result. + object := clause[loc[1]:] + for _, word := range assistantRerunVerdictWord { + if strings.Contains(object, word) && !assistantRerunObjectNoun(object, word) { + return false + } + } + } + } + return true +} + +// assistantRerunObjectNoun reports whether word is the head of the step's +// object ("修复报错", "定位超时原因", "确认失败的用例") rather than a finding +// ("看一下日志,端口被占用", "检查一遍全部正常"). +func assistantRerunObjectNoun(object, word string) bool { + switch word { + case "报错", "超时", "失败": + trimmed := strings.TrimLeft(object, " \t一下遍次") + return strings.HasPrefix(trimmed, word) + } + return false +} func assistantRerunReportsOutcome(rest string) bool { if assistantRerunOutcome.MatchString(rest) { @@ -1272,15 +1366,7 @@ func assistantRerunReportsOutcome(rest string) bool { return true } } - if assistantPlanCheckIntentIn(rest) || assistantRerunQuestion.MatchString(rest) { - return false - } - for _, word := range assistantRerunVerdictWord { - if strings.Contains(rest, word) { - return true - } - } - return false + return !assistantRerunPlanClauses(rest) } func assistantRerunConnectsStep(connector, rest string) bool { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 60c84a425..c37e3bf97 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -243,6 +243,10 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-found", `我先看一下。\n重跑失败的用例,然后发现还是报错。`, "1", "还是报错") runMultiLinePlanCase(t, "failed-rerun-cause", `我先看一下。\n重跑失败的用例,再查日志发现是端口冲突。`, "1", "端口冲突") runMultiLinePlanCase(t, "failed-rerun-still-red", `我先看一下。\n重跑失败的测试,再跑还是红。`, "1", "还是红") + runMultiLinePlanCase(t, "failed-rerun-confirmed", `我先看一下。\n重跑失败的用例,然后确认是端口冲突。`, "1", "端口冲突") + runMultiLinePlanCase(t, "failed-rerun-again-error", `我先看一下。\n重跑失败的测试,再跑一遍也报错。`, "1", "也报错") + runMultiLinePlanCase(t, "failed-rerun-located", `我先看一下。\n重跑失败的用例,然后定位到超时配置太短。`, "1", "超时配置太短") + runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") runMultiLinePlanCase(t, "check-verdict-answer", `我先跑一下检查。\n检查通过,工作区干净。`, "1", "工作区干净") @@ -462,6 +466,29 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的用例,然后修复报错。", true}, {"我先看一下。\n重跑失败的测试,接着定位超时原因。", true}, {"我先看一下。\n重跑失败。", false}, + // Only a clause that opens with a step verb, without a result + // complement or a verdict it does not ask about, stays a plan. + {"我先看一下。\n重跑失败的用例,然后确认是端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后检查出是依赖版本不对。", false}, + {"我先看一下。\n重跑失败的用例,再检查一遍全部正常。", false}, + {"我先看一下。\n重跑失败的用例,然后验证下来是端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,再看一下日志,端口被占用。", false}, + {"我先看一下。\n重跑失败的用例,然后定位到超时配置太短。", false}, + {"我先看一下。\n重跑失败的测试,然后排查出依赖版本不对。", false}, + {"我先看一下。\n重跑失败的测试,再跑一遍也报错。", false}, + {"我先看一下。\n重跑失败的测试,再跑一遍超时。", false}, + {"我先看一下。\n重跑失败的测试,然后全绿。", false}, + {"我先看一下。\n重跑失败的测试,然后报错消失。", false}, + {"我先看一下。\n重跑失败的测试,再跑结果相同。", false}, + {"我先看一下。\n重跑失败的测试。\n还是红。", false}, + {"我先看一下。\n重跑失败的测试。\n全部通过。", false}, + {"我先看一下。\n重跑失败的用例,然后查了日志。", false}, + {"我先看一下。\n重跑失败的测试。\n然后查看日志。", true}, + {"我先看一下。\n重跑失败的测试。\n再对比两次结果。", true}, + {"我先看一下。\n重跑失败的用例,再检查日志里是否有端口冲突。", true}, + {"我先看一下。\n重跑失败的用例,然后修复超时。", true}, + {"我先看一下。\n重跑失败的用例,然后排查报错原因。", true}, + {"我先看一下。\n重跑失败的用例,再看看是哪个用例超时。", true}, {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, {"我先总结一下,然后列出原因。", true}, From 1d563e934565fbce8ca28174e96e1ebe286bd5f2 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 20:47:26 +0000 Subject: [PATCH 24/28] fix(assistant): read a rerun step's finding on its next line or in its object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 83d928f4 kept 失败 only when the clause after the connector was a step. Two kinds of findings still read as plans: one written on the line after the rerun step ("再查看日志。\n端口被占用。"), and one outside the verdict list ("确认为端口冲突", "排查原因:端口冲突", "检查日志没有异常", "检查端口端口已释放", "修复后全绿", "确认问题在配置文件"). Those replies were rerun until 502 or their answer was dropped. - The connector branch also checks what follows the clause: the instruction is a plan only when every further line names the next step. A standalone rerun instruction now reads all following lines instead of only one. - A step's object is a finding when a result complement follows the verb ("定位到", "排查出", "查了", "修复后") or it carries 为/:/在/没有/无/ 已/确实. 出 and 完 inside a noun ("检查输出", "查看完整日志") stay part of the object, so those steps continue the plan. - Rerun clauses and prose "然后…" steps share one judgment, so the same wording is delivered without a preceding rerun too. New cases in TestAssistantProgressOnlyRequiresPlanLines and route-level cases fail on 83d928f4. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 129 +++++++++++++++--- .../httpapi/assistant_progress_text_test.go | 30 ++++ 2 files changed, 143 insertions(+), 16 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 71d125b7a..ffb56f730 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1225,13 +1225,16 @@ func stripAssistantRerunFailedTarget(text string) string { last := 0 for _, m := range matches { if m[8] >= 0 { - rest := text[m[9]:] + rest, after := text[m[9]:], "" if loc := assistantRerunSentenceEnd.FindStringIndex(rest); loc != nil { + after = rest[loc[1]:] rest = rest[:loc[0]] } rest = strings.TrimSpace(rest) connector := text[m[8]:m[9]] - if !assistantRerunConnectsStep(connector, rest) { + // A step on this clause does not make the rerun a plan when the + // next line already reports the outcome ("…再查看日志。\n端口被占用。"). + if !assistantRerunConnectsStep(connector, rest) || !assistantRerunStandaloneStep(after) { continue } } else if !assistantRerunStandaloneStep(text[m[1]:]) { @@ -1247,8 +1250,10 @@ func stripAssistantRerunFailedTarget(text string) string { return b.String() } -// assistantRerunStandaloneStep reports whether a standalone rerun instruction -// is still a plan: it ends the reply, or the next line is the next step. +// assistantRerunStandaloneStep reports whether what follows a rerun +// instruction is still the plan: no text at all, or every further line names +// the next step. "重跑失败的测试。\n然后查看日志。" stays a plan; +// "重跑失败的测试。\n然后查看日志。\n还是红。" reports the outcome. func assistantRerunStandaloneStep(after string) bool { for line := range strings.SplitSeq(after, "\n") { line = strings.TrimSpace(line) @@ -1259,7 +1264,9 @@ func assistantRerunStandaloneStep(after string) bool { line = rest } line = strings.TrimRight(line, "。.!!;;") - return assistantRerunPlanClauses(line) + if !assistantRerunPlanClauses(line) { + return false + } } return true } @@ -1282,10 +1289,6 @@ var assistantRerunOutcomeWord = []string{"还是", "仍", "依然", "依旧", " // "然后报错消失") is read as the outcome. var assistantRerunPlanVerb = regexp.MustCompile(`^(?:查看|查一下|查查|查|检查|核对|核查|对比|比对|比较|修复|修改|修正|修|定位|排查|整理|分析|汇总|收集|读取|阅读|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查|记录|更新|调整|补充|补上|重跑|重试|重新运行|重新执行)`) -// assistantRerunResultComplement turns a step verb into a finding: "定位到", -// "排查出", "验证下来", "查了". -var assistantRerunResultComplement = []string{"到", "出", "下来", "了", "完"} - // assistantRerunVerdictWord states a result unless the clause asks about it // ("然后确认是否全部通过"): "然后确认是端口冲突", "再检查一遍全部正常". var assistantRerunVerdictWord = []string{"是", "全部", "都", "正常", "成功", "没问题", "一样", "相同", "通过", "失败", "报错", "超时", "被"} @@ -1326,15 +1329,14 @@ func assistantRerunPlanClauses(rest string) bool { return false } } - for _, word := range assistantRerunResultComplement { - if strings.Contains(clause, word) { - return false - } - } if !assistantRerunQuestion.MatchString(clause) { // The step's own object may name what failed ("修复报错", - // "定位超时原因"); only a verdict after the object is a result. + // "定位超时原因"); only a finding or a verdict after the object + // is a result. object := clause[loc[1]:] + if assistantStepReportsOutcome(clause) { + return false + } for _, word := range assistantRerunVerdictWord { if strings.Contains(object, word) && !assistantRerunObjectNoun(object, word) { return false @@ -1436,6 +1438,12 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { continue } } + // "然后确认为端口冲突。", "再查看日志:端口冲突。" or + // "然后检查日志没有异常。" reads as a connector step but states how + // the step turned out, so it is the answer. + if assistantStepReportsOutcome(line) { + return false + } if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) && !assistantPlanBareStep(line) { return false } @@ -1528,12 +1536,101 @@ func assistantPlanStep(line string) bool { } for _, prefix := range []string{"然后", "接着", "随后", "之后", "再", "Then", "Next"} { if rest, ok := strings.CutPrefix(line, prefix); ok { - return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) + return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) && !assistantStepReportsOutcome(rest) } } return false } +// assistantStepOpener opens the clause of a step that follows a connector, +// as in "然后确认为端口冲突" or "再查看日志:端口冲突". +var assistantStepOpener = regexp.MustCompile(`^(?:重新)?(?:查看|查一下|查查|查|检查|核对|核查|对比|比对|比较|修复|修改|修正|修|定位|排查|整理|分析|汇总|收集|读取|阅读|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查|记录|更新|调整|补充|补上|重跑|重试|重新运行|重新执行)`) + +// assistantStepQuestion asks about a state ("然后确认是否全部通过"), so +// what follows the question verb is not a finding. +var assistantStepQuestion = regexp.MustCompile(`是否|有没有|能否|能不能|会不会|吗|什么|哪[个些里]?|为什么|怎么|多少|谁|了没`) + +// assistantStepComplement turns a step verb into a finding: "定位到", +// "排查出", "查了". It must follow the verb directly, and 出/完 inside a +// noun ("检查输出", "查看完整日志", "查看完成情况") is part of the object. +var assistantStepComplement = regexp.MustCompile(`^(?:到|下来|了|出[^输错]|完[^整成])`) + +// assistantStepFinding states how things turned out instead of naming what +// is left to do: "确认为端口冲突", "排查原因:端口冲突", "检查日志没有异常", +// "检查端口已释放", "修复后全绿", "确认问题在配置文件". +var assistantStepFinding = regexp.MustCompile(`[::]|没有|已|确实|为|在|^后|(?:^|[^并])无(?:[^法]|$)`) + +// assistantStepReportsOutcome reports whether a clause after a connector +// names a step to take ("然后修复报错", "再查看日志") or states its outcome +// ("然后确认为端口冲突", "再查看日志:端口冲突"). It is deliberately +// conservative: a clause it cannot read as a step is delivered as main did. +func assistantStepReportsOutcome(rest string) bool { + rest = strings.TrimSpace(rest) + rest = assistantRerunStepConnector.ReplaceAllString(rest, "") + if rest == "" { + return false + } + loc := assistantStepOpener.FindStringIndex(rest) + if loc == nil { + return false + } + if assistantStepQuestion.MatchString(rest) { + return false + } + object := rest[loc[1]:] + if object == "" { + return false + } + // "修到测试全部通过为止" names the state a step runs until, and + // "查到对应提交后回滚" names a precondition for the next step. + if strings.Contains(rest, "为止") { + return false + } + if plan, matched := assistantPlanStatesPrecondition(rest); matched && plan { + return false + } + if assistantStepComplement.MatchString(object) { + return true + } + return assistantStepFindingIn(rest, object) +} + +// assistantStepGoalStart follows a perfective 后 that opens a precondition +// the next step is to reach ("修改后确保接口没问题"), not a finding. +var assistantStepGoalStart = []string{"确保", "保证", "验证", "确认", "检查", "再", "继续", "然后", "接着", "随后"} + +// assistantStepFindingIn reports whether a step's object states how things +// turned out: "确认为端口冲突", "排查原因:端口冲突", "检查日志没有异常", +// "检查端口已释放", "修复后全绿", "确认问题在配置文件". A colon that opens a +// plan header ("检查范围:配置文件和测试") is part of the step. +func assistantStepFindingIn(rest, object string) bool { + i := strings.IndexAny(object, "::") + if i >= 0 { + before := object[:i] + header := false + for _, lead := range assistantPlanLeadIn { + if strings.HasPrefix(strings.TrimSpace(before), lead) { + header = true + break + } + } + if !header { + return true + } + object = object[i+1:] + } + if strings.HasPrefix(object, "后") { + tail := strings.TrimSpace(object[len("后"):]) + for _, goal := range assistantStepGoalStart { + if strings.HasPrefix(tail, goal) { + return false + } + } + return true + } + return assistantStepFinding.MatchString(object) +} + var assistantListMarker = regexp.MustCompile(`^(?:[-*•·]|\d{1,2}[.、))]|[((]\d{1,2}[))]|[①-⑳]|[一二三四五六七八九十]{1,3}、)\s*`) // assistantPlanLeadIn opens a line that names a step of the plan in prose diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index c37e3bf97..cf3597f14 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -246,6 +246,10 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-confirmed", `我先看一下。\n重跑失败的用例,然后确认是端口冲突。`, "1", "端口冲突") runMultiLinePlanCase(t, "failed-rerun-again-error", `我先看一下。\n重跑失败的测试,再跑一遍也报错。`, "1", "也报错") runMultiLinePlanCase(t, "failed-rerun-located", `我先看一下。\n重跑失败的用例,然后定位到超时配置太短。`, "1", "超时配置太短") + runMultiLinePlanCase(t, "failed-rerun-confirmed-as", `我先看一下。\n重跑失败的用例,然后确认为端口冲突。`, "1", "端口冲突") + runMultiLinePlanCase(t, "failed-rerun-next-line-outcome", `我先看一下。\n重跑失败的测试,再查看日志。\n端口被占用。`, "1", "端口被占用") + runMultiLinePlanCase(t, "failed-rerun-colon-cause", `我先看一下。\n重跑失败的测试,然后排查原因:端口冲突。`, "1", "端口冲突") + runMultiLinePlanCase(t, "failed-rerun-no-anomaly", `我先看一下。\n然后检查日志没有异常。`, "1", "没有异常") runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") @@ -489,6 +493,32 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的用例,然后修复超时。", true}, {"我先看一下。\n重跑失败的用例,然后排查报错原因。", true}, {"我先看一下。\n重跑失败的用例,再看看是哪个用例超时。", true}, + // The outcome may be written on the line after the rerun step, and a + // step may be followed by an outcome line even when the step itself + // ends right after the connector. + {"我先看一下。\n重跑失败的测试,再查看日志。\n端口被占用。", false}, + {"我先看一下。\n重跑失败的用例,再查看日志。\n还是红。", false}, + {"我先看一下。\n重跑失败的测试。\n然后查看日志。\n还是红。", false}, + {"我先看一下。\n重跑失败的用例,再查看日志。\n日志里没有端口冲突。", false}, + // A finding uses wording outside the verdict list: 为, a colon, 在, + // 没有/无, 已, 确实, or a perfective 后 after the step verb. + {"我先看一下。\n重跑失败的用例,然后确认为端口冲突。", false}, + {"我先看一下。\n然后定位原因为端口冲突。", false}, + {"我先看一下。\n然后排查原因:端口冲突。", false}, + {"我先看一下。\n再查看日志:端口冲突。", false}, + {"我先看一下。\n然后检查日志没有异常。", false}, + {"我先看一下。\n然后检查端口已释放。", false}, + {"我先看一下。\n然后检查日志无异常。", false}, + {"我先看一下。\n然后修复后全绿。", false}, + {"我先看一下。\n然后确认问题在配置文件。", false}, + {"我先看一下。\n然后确认原因确实是缓存。", false}, + // The same steps without a finding still continue the plan; 出/完 + // inside a step's object is part of the noun ("检查输出", "完整日志"). + {"我先看一下。\n重跑失败的测试,然后检查输出。", true}, + {"我先看一下。\n重跑失败的测试,然后查看完整日志。", true}, + {"我先看一下。\n重跑失败的用例,再查看日志。\n再修复报错。", true}, + {"我先看一下。\n重跑失败的测试。\n- 查看日志\n- 修复报错", true}, + {"我先看一下。\n重跑失败的用例,然后确认为端口冲突。\n端口被占用。", false}, {"我先看一下。\n项目里有失败的用例。", false}, {"我先总结一下,项目里有 4 个 LOOP。", false}, {"我先总结一下,然后列出原因。", true}, From a3bf382ea8deb09180c4f43e319cc69439da64a0 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 22:25:23 +0000 Subject: [PATCH 25/28] fix(assistant): judge step findings only after a rerun, and by position MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1d563e93 ran assistantStepReportsOutcome on every plan line, so an ordinary step whose object holds 为/在/已/无/没有/: ("然后整理为表格", "然后检查已有 的测试用例", "然后记录在文档里") was delivered as the answer instead of being continued. - Ordinary "然后…" steps and plain plan lines no longer go through the finding check; they behave as in 83d928f4 and main. - The check now only decides whether a rerun keeps 失败, and reads each marker by position: 为 after a judging verb (确认为) or a finding subject (原因为), 在 after a finding subject (问题在), 没有/无/已 as the predicate rather than before a 的 that qualifies the object, and a completed verb (检查完) followed by the next step is an order, not a finding. 为何 is a question. - Tests: the reviewer's 14 plan steps continue (function and route level), 8 of them after a rerun also continue; the earlier findings are kept under a rerun prefix and are still delivered. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 83 +++++++++++++------ .../httpapi/assistant_progress_text_test.go | 69 ++++++++++++--- 2 files changed, 115 insertions(+), 37 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index ffb56f730..00aa7a13b 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1295,7 +1295,7 @@ var assistantRerunVerdictWord = []string{"是", "全部", "都", "正常", "成 // assistantRerunQuestion asks what the next step looks for ("再查一下原因是 // 什么", "再看看日志是否正常"), so a verdict word in it is not a result. -var assistantRerunQuestion = regexp.MustCompile(`是否|是不是|有没有|能否|能不能|会不会|吗|什么|哪[个些里]|为什么|怎么|多少`) +var assistantRerunQuestion = regexp.MustCompile(`是否|是不是|有没有|能否|能不能|会不会|吗|什么|哪[个些里]|为什么|为何|怎么|多少`) // assistantRerunStepConnector opens a later clause of the same plan step. var assistantRerunStepConnector = regexp.MustCompile(`^(?:然后|接着|随后|之后|再|并且|并|同时)\s*`) @@ -1438,12 +1438,6 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { continue } } - // "然后确认为端口冲突。", "再查看日志:端口冲突。" or - // "然后检查日志没有异常。" reads as a connector step but states how - // the step turned out, so it is the answer. - if assistantStepReportsOutcome(line) { - return false - } if !assistantProgressSentence(line) && !assistantPlanStep(line) && !assistantPlanProse(line) && !assistantPlanBareStep(line) { return false } @@ -1536,7 +1530,7 @@ func assistantPlanStep(line string) bool { } for _, prefix := range []string{"然后", "接着", "随后", "之后", "再", "Then", "Next"} { if rest, ok := strings.CutPrefix(line, prefix); ok { - return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) && !assistantStepReportsOutcome(rest) + return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(rest) } } return false @@ -1548,22 +1542,42 @@ var assistantStepOpener = regexp.MustCompile(`^(?:重新)?(?:查看|查一下| // assistantStepQuestion asks about a state ("然后确认是否全部通过"), so // what follows the question verb is not a finding. -var assistantStepQuestion = regexp.MustCompile(`是否|有没有|能否|能不能|会不会|吗|什么|哪[个些里]?|为什么|怎么|多少|谁|了没`) +var assistantStepQuestion = regexp.MustCompile(`是否|有没有|能否|能不能|会不会|吗|什么|哪[个些里]?|为什么|为何|怎么|多少|谁|了没`) // assistantStepComplement turns a step verb into a finding: "定位到", // "排查出", "查了". It must follow the verb directly, and 出/完 inside a // noun ("检查输出", "查看完整日志", "查看完成情况") is part of the object. var assistantStepComplement = regexp.MustCompile(`^(?:到|下来|了|出[^输错]|完[^整成])`) -// assistantStepFinding states how things turned out instead of naming what -// is left to do: "确认为端口冲突", "排查原因:端口冲突", "检查日志没有异常", -// "检查端口已释放", "修复后全绿", "确认问题在配置文件". -var assistantStepFinding = regexp.MustCompile(`[::]|没有|已|确实|为|在|^后|(?:^|[^并])无(?:[^法]|$)`) - -// assistantStepReportsOutcome reports whether a clause after a connector -// names a step to take ("然后修复报错", "再查看日志") or states its outcome -// ("然后确认为端口冲突", "再查看日志:端口冲突"). It is deliberately -// conservative: a clause it cannot read as a step is delivered as main did. +// assistantStepJudgeVerb opens a step that judges what it looks at, so a +// 为 right after it states the verdict ("确认为端口冲突", "定位为超时"). After +// any other step verb 为 names the target of the step ("整理为表格", +// "修改为异步调用", "更新为最新版本"). +var assistantStepJudgeVerb = regexp.MustCompile(`^(?:重新)?(?:查看|查一下|查查|查|检查|核对|核查|定位|排查|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查)$`) + +// assistantStepFindingSubject names what a finding is about, so 为 or 在 +// after it states the finding ("定位原因为端口冲突", "确认问题在配置文件"). +// Without such a subject 在 opens a place in the step's own object +// ("记录在文档里", "修复在 Windows 上的路径问题"). +var assistantStepFindingSubject = regexp.MustCompile(`(?:原因|根因|问题|结果|结论|故障|错误|报错|异常|症结|瓶颈|bug|Bug|BUG)(?:出|就|应该|应当|主要)?(?:为|在)`) + +// assistantStepStateWord states a state when it is the predicate of the +// clause ("检查日志没有异常", "检查端口已释放"). Followed by 的 it only +// qualifies the step's object ("检查已有的测试用例", "检查无用的依赖", +// "核对没有提交的改动"), which is still a step to take. +var assistantStepStateWord = regexp.MustCompile(`没有|已|(?:^|[^并])无(?:[^法]|$)`) + +// assistantStepThen opens the next step after a completed action in the same +// clause: "检查完依赖再升级" names an order, not what the check found. +var assistantStepThen = regexp.MustCompile(`再|然后|接着|随后|之后`) + +// assistantStepReportsOutcome reports whether a clause after a rerun +// instruction names a step to take ("然后修复报错", "再查看日志") or states +// its outcome ("然后确认为端口冲突", "再查看日志:端口冲突"). It only decides +// whether a rerun keeps 失败: an outcome keeps it, so the reply is delivered +// as main did. Ordinary "然后…" plan steps do not go through it; there the +// same markers are as often part of the step ("然后整理为表格", "然后检查已有 +// 的测试用例"). func assistantStepReportsOutcome(rest string) bool { rest = strings.TrimSpace(rest) rest = assistantRerunStepConnector.ReplaceAllString(rest, "") @@ -1590,9 +1604,9 @@ func assistantStepReportsOutcome(rest string) bool { return false } if assistantStepComplement.MatchString(object) { - return true + return !assistantStepThen.MatchString(object) } - return assistantStepFindingIn(rest, object) + return assistantStepFindingIn(rest[:loc[1]], object) } // assistantStepGoalStart follows a perfective 后 that opens a precondition @@ -1600,10 +1614,15 @@ func assistantStepReportsOutcome(rest string) bool { var assistantStepGoalStart = []string{"确保", "保证", "验证", "确认", "检查", "再", "继续", "然后", "接着", "随后"} // assistantStepFindingIn reports whether a step's object states how things -// turned out: "确认为端口冲突", "排查原因:端口冲突", "检查日志没有异常", -// "检查端口已释放", "修复后全绿", "确认问题在配置文件". A colon that opens a -// plan header ("检查范围:配置文件和测试") is part of the step. -func assistantStepFindingIn(rest, object string) bool { +// turned out: "确认为端口冲突", "定位原因为端口冲突", "排查原因:端口冲突", +// "检查日志没有异常", "检查端口已释放", "修复后全绿", "确认问题在配置文件". +// verb is the step verb the object follows. Each marker counts only where it +// states a finding: 为 after a judging verb or a finding subject, 在 after a +// finding subject, 没有/无/已 as the predicate rather than before a 的 that +// qualifies the object. "整理为表格", "记录在文档里", "检查已有的测试用例" +// and "检查无用的依赖" are still steps. A colon that opens a plan header +// ("检查范围:配置文件和测试") is part of the step. +func assistantStepFindingIn(verb, object string) bool { i := strings.IndexAny(object, "::") if i >= 0 { before := object[:i] @@ -1628,7 +1647,21 @@ func assistantStepFindingIn(rest, object string) bool { } return true } - return assistantStepFinding.MatchString(object) + if strings.Contains(object, "确实") { + return true + } + if strings.HasPrefix(object, "为") && assistantStepJudgeVerb.MatchString(verb) { + return true + } + if assistantStepFindingSubject.MatchString(object) { + return true + } + for _, loc := range assistantStepStateWord.FindAllStringIndex(object, -1) { + if !strings.Contains(object[loc[1]:], "的") { + return true + } + } + return false } var assistantListMarker = regexp.MustCompile(`^(?:[-*•·]|\d{1,2}[.、))]|[((]\d{1,2}[))]|[①-⑳]|[一二三四五六七八九十]{1,3}、)\s*`) diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index cf3597f14..3532dc1f6 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -228,6 +228,20 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"imperative 显示", "show-step", `我先看一下。\n显示所有成员。`}, {"wait for completion", "await-completion-step", `我先看一下。\n等构建完成。`}, {"result sent somewhere", "result-destination-step", `我先看一下。\n导出结果到 CSV。`}, + // 为/在/已/无/没有 or a colon in an ordinary "然后…" step is part of + // the step, not a finding. + {"step into a table", "into-table-step", `我先看一下。\n然后整理为表格。`}, + {"bare step into a table", "bare-into-table-step", `我先看一下。\n整理为表格。`}, + {"step changed to async", "changed-to-async-step", `我先看一下。\n然后修改为异步调用。`}, + {"step updated to latest", "updated-to-latest-step", `我先看一下。\n然后更新为最新版本。`}, + {"step summed into a report", "summed-into-report-step", `我先看一下。\n然后汇总为一份报告。`}, + {"step asks why", "asks-why-step", `我先看一下。\n然后分析为何超时。`}, + {"step on existing tests", "existing-tests-step", `我先看一下。\n然后检查已有的测试用例。`}, + {"step on unused deps", "unused-deps-step", `我先看一下。\n然后检查无用的依赖。`}, + {"step with a colon header", "colon-header-step", `我先看一下。\n然后查看现在的配置:端口和超时。`}, + {"rerun then into a table", "rerun-into-table-step", `我先看一下。\n重跑失败的测试,然后整理为表格。`}, + {"rerun then record in docs", "rerun-record-in-docs-step", `我先看一下。\n重跑失败的测试,然后记录在文档里。`}, + {"rerun then check then upgrade", "rerun-check-then-upgrade-step", `我先看一下。\n重跑失败的测试,然后检查完依赖再升级。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -249,7 +263,9 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-confirmed-as", `我先看一下。\n重跑失败的用例,然后确认为端口冲突。`, "1", "端口冲突") runMultiLinePlanCase(t, "failed-rerun-next-line-outcome", `我先看一下。\n重跑失败的测试,再查看日志。\n端口被占用。`, "1", "端口被占用") runMultiLinePlanCase(t, "failed-rerun-colon-cause", `我先看一下。\n重跑失败的测试,然后排查原因:端口冲突。`, "1", "端口冲突") - runMultiLinePlanCase(t, "failed-rerun-no-anomaly", `我先看一下。\n然后检查日志没有异常。`, "1", "没有异常") + runMultiLinePlanCase(t, "failed-rerun-no-anomaly", `我先看一下。\n重跑失败的测试,然后检查日志没有异常。`, "1", "没有异常") + runMultiLinePlanCase(t, "failed-rerun-port-released", `我先看一下。\n重跑失败的测试,然后检查端口已释放。`, "1", "端口已释放") + runMultiLinePlanCase(t, "failed-rerun-cause-in-config", `我先看一下。\n重跑失败的用例,然后确认问题在配置文件。`, "1", "配置文件") runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") @@ -500,18 +516,47 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的用例,再查看日志。\n还是红。", false}, {"我先看一下。\n重跑失败的测试。\n然后查看日志。\n还是红。", false}, {"我先看一下。\n重跑失败的用例,再查看日志。\n日志里没有端口冲突。", false}, - // A finding uses wording outside the verdict list: 为, a colon, 在, - // 没有/无, 已, 确实, or a perfective 后 after the step verb. + // After a rerun, a finding uses wording outside the verdict list: 为, + // a colon, 在, 没有/无, 已, 确实, or a perfective 后 after the step + // verb. It keeps 失败, so the reply is delivered as main did. {"我先看一下。\n重跑失败的用例,然后确认为端口冲突。", false}, - {"我先看一下。\n然后定位原因为端口冲突。", false}, - {"我先看一下。\n然后排查原因:端口冲突。", false}, - {"我先看一下。\n再查看日志:端口冲突。", false}, - {"我先看一下。\n然后检查日志没有异常。", false}, - {"我先看一下。\n然后检查端口已释放。", false}, - {"我先看一下。\n然后检查日志无异常。", false}, - {"我先看一下。\n然后修复后全绿。", false}, - {"我先看一下。\n然后确认问题在配置文件。", false}, - {"我先看一下。\n然后确认原因确实是缓存。", false}, + {"我先看一下。\n重跑失败的用例,然后定位原因为端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后排查原因:端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,再查看日志:端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志没有异常。", false}, + {"我先看一下。\n重跑失败的测试,然后检查端口已释放。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志无异常。", false}, + {"我先看一下。\n重跑失败的测试,然后修复后全绿。", false}, + {"我先看一下。\n重跑失败的用例,然后确认问题在配置文件。", false}, + {"我先看一下。\n重跑失败的用例,然后确认原因确实是缓存。", false}, + {"我先看一下。\n重跑失败的测试,然后查到问题在配置文件。", false}, + // The same markers in an ordinary "然后…" step are part of the step: + // 为 after a step that transforms its object or in 为何, 在 opening a + // place, 已/无/没有 qualifying the object before 的, a colon listing + // what to look at, or 完 followed by the next step. + {"我先看一下。\n然后整理为表格。", true}, + {"我先看一下。\n整理为表格。", true}, + {"我先看一下。\n然后修改为异步调用。", true}, + {"我先看一下。\n然后更新为最新版本。", true}, + {"我先看一下。\n然后汇总为一份报告。", true}, + {"我先看一下。\n然后分析为何超时。", true}, + {"我先看一下。\n然后检查已有的测试用例。", true}, + {"我先看一下。\n然后记录在文档里。", true}, + {"我先看一下。\n然后查看在 main 分支上的提交记录。", true}, + {"我先看一下。\n然后修复在 Windows 上的路径问题。", true}, + {"我先看一下。\n然后检查无用的依赖。", true}, + {"我先看一下。\n然后核对没有提交的改动。", true}, + {"我先看一下。\n然后查看现在的配置:端口和超时。", true}, + {"我先看一下。\n然后检查完依赖再升级。", true}, + // After a rerun these steps are still the plan. + {"我先看一下。\n重跑失败的测试,然后整理为表格。", true}, + {"我先看一下。\n重跑失败的测试,然后修改为异步调用。", true}, + {"我先看一下。\n重跑失败的测试,然后分析为何超时。", true}, + {"我先看一下。\n重跑失败的测试,然后检查已有的测试用例。", true}, + {"我先看一下。\n重跑失败的测试,然后记录在文档里。", true}, + {"我先看一下。\n重跑失败的测试,然后检查无用的依赖。", true}, + {"我先看一下。\n重跑失败的测试,然后核对没有提交的改动。", true}, + {"我先看一下。\n重跑失败的测试,然后检查完依赖再升级。", true}, // The same steps without a finding still continue the plan; 出/完 // inside a step's object is part of the noun ("检查输出", "完整日志"). {"我先看一下。\n重跑失败的测试,然后检查输出。", true}, From ed8a6b016528e0ce6eb3c56c35edef08efacd7a8 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Mon, 30 Nov 2026 23:13:46 +0000 Subject: [PATCH 26/28] fix(assistant): read a rerun step's state word as an attribute only when it opens the object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a3bf382e let any 的 later in a rerun step's object turn 已/无/没有 into an attribute, so a finding that holds 的 ("重跑失败的测试,然后检查日志已无明显的 异常。") was read as a plan and the CLI ran again, overwriting the answer. It also skipped a full-width colon by one byte, so 为/后 after ":" were never matched, and only a fixed list of judging verbs read 为 as a verdict ("然后分析为端口冲突导致" became a plan). - 已/已经/无/没有 qualify the object only when they open it (after at most 一下/所有-style leads) and a 的 closes an attribute of at most 3 runes right after them with no further state word ("检查已有的测试用例", "核对没有提交的改动", "确认没有遗漏的文件"). With a subject in front ("检查日志已无明显的异常", "检查日志没有新的异常") they are the predicate. - 为 right after the step verb names a target only for verbs that transform their object (整理/修改/更新/汇总/调整…); after any other step verb it is the verdict, which is how main reads the clause after a rerun. - The colon is skipped by its rune width. - Tests: 9 findings with 的/为/full-width colon are delivered, 9 more attribute and target steps after a rerun still continue, a half/full-width colon table for assistantStepFindingIn, and 4 route-level cases; all of the new findings fail on a3bf382e. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 68 ++++++++++++++----- .../httpapi/assistant_progress_text_test.go | 56 +++++++++++++++ 2 files changed, 107 insertions(+), 17 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 00aa7a13b..419e50afa 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1549,11 +1549,13 @@ var assistantStepQuestion = regexp.MustCompile(`是否|有没有|能否|能不 // noun ("检查输出", "查看完整日志", "查看完成情况") is part of the object. var assistantStepComplement = regexp.MustCompile(`^(?:到|下来|了|出[^输错]|完[^整成])`) -// assistantStepJudgeVerb opens a step that judges what it looks at, so a -// 为 right after it states the verdict ("确认为端口冲突", "定位为超时"). After -// any other step verb 为 names the target of the step ("整理为表格", -// "修改为异步调用", "更新为最新版本"). -var assistantStepJudgeVerb = regexp.MustCompile(`^(?:重新)?(?:查看|查一下|查查|查|检查|核对|核查|定位|排查|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查)$`) +// assistantStepTargetVerb turns its object into something, so a 为 right +// after it names the target of the step ("整理为表格", "修改为异步调用", +// "更新为最新版本", "汇总为一份报告"). After any other step verb 为 states +// the verdict of what the step looked at ("确认为端口冲突", "定位为超时", +// "分析为端口冲突导致"); that is also how main reads such a clause after a +// rerun. +var assistantStepTargetVerb = regexp.MustCompile(`^(?:重新)?(?:修复|修改|修正|修|整理|汇总|收集|读取|记录|更新|调整|补充|补上)$`) // assistantStepFindingSubject names what a finding is about, so 为 or 在 // after it states the finding ("定位原因为端口冲突", "确认问题在配置文件"). @@ -1562,10 +1564,36 @@ var assistantStepJudgeVerb = regexp.MustCompile(`^(?:重新)?(?:查看|查一下 var assistantStepFindingSubject = regexp.MustCompile(`(?:原因|根因|问题|结果|结论|故障|错误|报错|异常|症结|瓶颈|bug|Bug|BUG)(?:出|就|应该|应当|主要)?(?:为|在)`) // assistantStepStateWord states a state when it is the predicate of the -// clause ("检查日志没有异常", "检查端口已释放"). Followed by 的 it only -// qualifies the step's object ("检查已有的测试用例", "检查无用的依赖", -// "核对没有提交的改动"), which is still a step to take. -var assistantStepStateWord = regexp.MustCompile(`没有|已|(?:^|[^并])无(?:[^法]|$)`) +// clause ("检查日志没有异常", "检查端口已释放", "检查日志已无明显的异常"). +// 无法 is not a state, and 并无 is matched as 无. +var assistantStepStateWord = regexp.MustCompile(`没有|已经|已|无`) + +// assistantStepObjectLead may open a step's object before a state word that +// qualifies it: "检查一下已有的测试用例", "核对所有没有提交的改动". +var assistantStepObjectLead = regexp.MustCompile(`^(?:一下|一遍|一次|下|遍|所有|这些|那些|各个|每个)?$`) + +// assistantStepQualifierSpan is how far, in runes, the 的 of an attribute +// may follow its state word: "已有的", "无用的", "没有提交的", "已经存在的". +const assistantStepQualifierSpan = 3 + +// assistantStepStateQualifies reports whether the state word at +// object[start:end] only qualifies the step's object ("检查已有的测试用例", +// "检查无用的依赖", "核对没有提交的改动", "确认没有遗漏的文件"). It does so +// only when it opens the object and a 的 closes a short attribute right after +// it. With a subject in front ("检查日志已无明显的异常") or a longer phrase +// after it ("检查日志没有新的异常" has 日志 in front; "确认已经没有之前的异常" +// has another state word before 的) it is the predicate and states the +// finding. +func assistantStepStateQualifies(object string, start, end int) bool { + if !assistantStepObjectLead.MatchString(strings.TrimSpace(object[:start])) { + return false + } + attr, _, ok := strings.Cut(object[end:], "的") + if !ok || attr == "" || utf8.RuneCountInString(attr) > assistantStepQualifierSpan { + return false + } + return !strings.ContainsFunc(attr, unicode.IsSpace) && !strings.ContainsAny(attr, ",,、。;;::") && !assistantStepStateWord.MatchString(attr) +} // assistantStepThen opens the next step after a completed action in the same // clause: "检查完依赖再升级" names an order, not what the check found. @@ -1617,11 +1645,13 @@ var assistantStepGoalStart = []string{"确保", "保证", "验证", "确认", " // turned out: "确认为端口冲突", "定位原因为端口冲突", "排查原因:端口冲突", // "检查日志没有异常", "检查端口已释放", "修复后全绿", "确认问题在配置文件". // verb is the step verb the object follows. Each marker counts only where it -// states a finding: 为 after a judging verb or a finding subject, 在 after a -// finding subject, 没有/无/已 as the predicate rather than before a 的 that -// qualifies the object. "整理为表格", "记录在文档里", "检查已有的测试用例" -// and "检查无用的依赖" are still steps. A colon that opens a plan header -// ("检查范围:配置文件和测试") is part of the step. +// states a finding: 为 after a step that does not transform its object or +// after a finding subject, 在 after a finding subject, 没有/无/已 as the +// predicate rather than opening a short attribute of the object +// (assistantStepStateQualifies). "整理为表格", "记录在文档里", "检查已有的 +// 测试用例" and "检查无用的依赖" are still steps. A colon, half or full +// width, that opens a plan header ("检查范围:配置文件和测试") is part of the +// step. func assistantStepFindingIn(verb, object string) bool { i := strings.IndexAny(object, "::") if i >= 0 { @@ -1636,7 +1666,8 @@ func assistantStepFindingIn(verb, object string) bool { if !header { return true } - object = object[i+1:] + _, size := utf8.DecodeRuneInString(object[i:]) + object = object[i+size:] } if strings.HasPrefix(object, "后") { tail := strings.TrimSpace(object[len("后"):]) @@ -1650,14 +1681,17 @@ func assistantStepFindingIn(verb, object string) bool { if strings.Contains(object, "确实") { return true } - if strings.HasPrefix(object, "为") && assistantStepJudgeVerb.MatchString(verb) { + if strings.HasPrefix(object, "为") && !assistantStepTargetVerb.MatchString(verb) { return true } if assistantStepFindingSubject.MatchString(object) { return true } for _, loc := range assistantStepStateWord.FindAllStringIndex(object, -1) { - if !strings.Contains(object[loc[1]:], "的") { + if object[loc[0]:loc[1]] == "无" && strings.HasPrefix(object[loc[1]:], "法") { + continue + } + if !assistantStepStateQualifies(object, loc[0], loc[1]) { return true } } diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 3532dc1f6..12b0b25d2 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -242,6 +242,8 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"rerun then into a table", "rerun-into-table-step", `我先看一下。\n重跑失败的测试,然后整理为表格。`}, {"rerun then record in docs", "rerun-record-in-docs-step", `我先看一下。\n重跑失败的测试,然后记录在文档里。`}, {"rerun then check then upgrade", "rerun-check-then-upgrade-step", `我先看一下。\n重跑失败的测试,然后检查完依赖再升级。`}, + {"rerun then no missed files", "rerun-no-missed-files-step", `我先看一下。\n重跑失败的测试,然后确认没有遗漏的文件。`}, + {"rerun then uncommitted changes", "rerun-uncommitted-step", `我先看一下。\n重跑失败的测试,然后核对所有没有提交的改动。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -266,6 +268,10 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-no-anomaly", `我先看一下。\n重跑失败的测试,然后检查日志没有异常。`, "1", "没有异常") runMultiLinePlanCase(t, "failed-rerun-port-released", `我先看一下。\n重跑失败的测试,然后检查端口已释放。`, "1", "端口已释放") runMultiLinePlanCase(t, "failed-rerun-cause-in-config", `我先看一下。\n重跑失败的用例,然后确认问题在配置文件。`, "1", "配置文件") + runMultiLinePlanCase(t, "failed-rerun-no-obvious-anomaly", `我先看一下。\n重跑失败的测试,然后检查日志已无明显的异常。`, "1", "已无明显的异常") + runMultiLinePlanCase(t, "failed-rerun-no-new-anomaly", `我先看一下。\n重跑失败的测试,然后检查日志没有新的异常。`, "1", "没有新的异常") + runMultiLinePlanCase(t, "failed-rerun-analysed-as", `我先看一下。\n重跑失败的测试,然后分析为端口冲突导致。`, "1", "端口冲突导致") + runMultiLinePlanCase(t, "failed-rerun-full-width-colon", `我先看一下。\n重跑失败的测试,然后确认范围:为端口冲突。`, "1", "为端口冲突") runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") @@ -557,6 +563,34 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试,然后检查无用的依赖。", true}, {"我先看一下。\n重跑失败的测试,然后核对没有提交的改动。", true}, {"我先看一下。\n重跑失败的测试,然后检查完依赖再升级。", true}, + {"我先看一下。\n重跑失败的测试,然后确认没有遗漏的文件。", true}, + {"我先看一下。\n重跑失败的测试,然后检查一下已有的测试用例。", true}, + {"我先看一下。\n重跑失败的测试,然后核对所有没有提交的改动。", true}, + {"我先看一下。\n重跑失败的测试,然后检查已经存在的配置文件。", true}, + {"我先看一下。\n重跑失败的测试,然后更新为最新版本。", true}, + {"我先看一下。\n重跑失败的测试,然后汇总为表格。", true}, + {"我先看一下。\n重跑失败的测试,然后调整为异步调用。", true}, + {"我先看一下。\n重跑失败的测试,然后检查范围:配置文件和测试。", true}, + {"我先看一下。\n重跑失败的测试,然后检查范围:配置文件和测试。", true}, + // A finding that itself holds 的 is still the finding: the state word + // has a subject in front (日志已无…) or a longer phrase after it, so + // it is the predicate, not an attribute of the step's object. + {"我先看一下。\n重跑失败的测试,然后检查日志已无明显的异常。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志没有新的异常。", false}, + {"我先看一下。\n重跑失败的测试,然后查看日志已无多余的连接。", false}, + {"我先看一下。\n重跑失败的测试,然后核对配置无多余的字段。", false}, + {"我先看一下。\n重跑失败的测试,然后检查依赖已无冲突的版本。", false}, + {"我先看一下。\n重跑失败的测试,然后确认日志里已经没有之前的异常。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志无明显异常。", false}, + // 为 after a step that does not transform its object is the verdict. + {"我先看一下。\n重跑失败的测试,然后分析为端口冲突导致。", false}, + {"我先看一下。\n重跑失败的测试,然后分析为端口冲突导致的超时。", false}, + // A full-width colon is skipped whole, so 为/后 after it are read as + // after a half-width one. + {"我先看一下。\n重跑失败的测试,然后确认范围:为端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后确认范围:为端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后定位范围:后全绿。", false}, + {"我先看一下。\n重跑失败的测试,然后定位范围:后全绿。", false}, // The same steps without a finding still continue the plan; 出/完 // inside a step's object is part of the noun ("检查输出", "完整日志"). {"我先看一下。\n重跑失败的测试,然后检查输出。", true}, @@ -908,3 +942,25 @@ JSON } }) } + +// A colon is skipped by its width: after a full-width ":" the object is read +// exactly as after a half-width ":". +func TestAssistantStepFindingInColonWidth(t *testing.T) { + for _, tc := range []struct { + verb, object string + want bool + }{ + {"确认", "范围:为端口冲突", true}, + {"确认", "范围:为端口冲突", true}, + {"确认", "范围:后全绿", true}, + {"确认", "范围:后全绿", true}, + {"检查", "范围:配置文件和测试", false}, + {"检查", "范围:配置文件和测试", false}, + {"修改", "范围:后确保接口没问题", false}, + {"修改", "范围:后确保接口没问题", false}, + } { + if got := assistantStepFindingIn(tc.verb, tc.object); got != tc.want { + t.Errorf("assistantStepFindingIn(%q, %q) = %v, want %v", tc.verb, tc.object, got, tc.want) + } + } +} From 9983ae4e0090c2bddc9383e44ebbfadbe0dd83d1 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Tue, 1 Dec 2026 11:29:52 +0000 Subject: [PATCH 27/28] fix(assistant): read a place, an analysing verb and a colon list in a rerun step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 已/已经 after a place ("检查项目里已有的配置", "查看日志里已经存在的错误") or after several quantifiers ("核对所有这些没有提交的改动", "检查一遍所有 已有的测试用例") qualifies the step's object again; 没有/无 after a place ("检查日志里没有新的异常") and 已经有… ("检查日志已经有新的报错") stay findings. - 为 is a verdict after a judging verb (确认/定位/排查/检查/核对…). After 分析/对比/比对/比较 it names the form the step produces ("分析为三大类", "对比为表格") unless it names a cause ("分析为端口冲突导致"); after any other verb it is the step's target. - A colon after a scope noun that lists bare names ("查看现在的配置:端口和 超时") is part of the step; "查看日志:端口冲突和超时" stays a finding. - "在 Windows 上的" only places the object of a step after a rerun. - A step may follow a rerun after a comma without a connector ("重跑失败的测试,整理为表格"); an outcome there is still delivered. Co-Authored-By: Claude Code --- internal/httpapi/assistant.go | 159 ++++++++++++++---- .../httpapi/assistant_progress_text_test.go | 104 +++++++++++- 2 files changed, 230 insertions(+), 33 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 419e50afa..2c88be3a8 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1207,7 +1207,7 @@ func assistantProgressText(message string, events []domain.AssistantActivityEven // In an imperative, "failed" may describe the tests to rerun. The exception // is preserved only for a standalone instruction or its next planned step. // A trailing outcome ("重跑失败的用例后还是报错") must be delivered. -var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(再|然后|接着|随后))`) +var assistantRerunFailedTarget = regexp.MustCompile(`(?m)^([ \t]*(?:(?:然后|接着|随后|先|再)[ \t]*)?(?:重跑|重试|重新运行|重新执行))[ \t]*(?:有失败的|失败的)[ \t]*(测试用例|用例|测试|任务|检查)([ \t]*[。.!!]?[ \t]*$|(?:后[,,]?|[,,])[ \t]*(再|然后|接着|随后)?)`) // stripAssistantRerunFailedTarget drops "失败的" from a rerun instruction so // the blocker list does not read it as a failure report. It does so only when @@ -1224,14 +1224,20 @@ func stripAssistantRerunFailedTarget(text string) string { var b strings.Builder last := 0 for _, m := range matches { - if m[8] >= 0 { - rest, after := text[m[9]:], "" + // The instruction either ends its line or goes on after 后 or a + // comma, with or without a connector: "重跑失败的测试,然后整理为 + // 表格", "重跑失败的测试,整理为表格". + if tail := text[m[6]:m[7]]; strings.ContainsAny(tail, ",,") || strings.Contains(tail, "后") { + rest, after := text[m[7]:], "" if loc := assistantRerunSentenceEnd.FindStringIndex(rest); loc != nil { after = rest[loc[1]:] rest = rest[:loc[0]] } rest = strings.TrimSpace(rest) - connector := text[m[8]:m[9]] + connector := "" + if m[8] >= 0 { + connector = text[m[8]:m[9]] + } // A step on this clause does not make the rerun a plan when the // next line already reports the outcome ("…再查看日志。\n端口被占用。"). if !assistantRerunConnectsStep(connector, rest) || !assistantRerunStandaloneStep(after) { @@ -1337,6 +1343,14 @@ func assistantRerunPlanClauses(rest string) bool { if assistantStepReportsOutcome(clause) { return false } + // The parts a colon lists were already vetted as bare names + // ("查看现在的配置:端口和超时"). + if i := strings.IndexAny(object, "::"); i >= 0 { + _, size := utf8.DecodeRuneInString(object[i:]) + if assistantStepColonLists(object[:i], object[i+size:]) { + object = object[:i] + } + } for _, word := range assistantRerunVerdictWord { if strings.Contains(object, word) && !assistantRerunObjectNoun(object, word) { return false @@ -1348,13 +1362,13 @@ func assistantRerunPlanClauses(rest string) bool { } // assistantRerunObjectNoun reports whether word is the head of the step's -// object ("修复报错", "定位超时原因", "确认失败的用例") rather than a finding -// ("看一下日志,端口被占用", "检查一遍全部正常"). +// object ("修复报错", "定位超时原因", "确认失败的用例", "查看日志里已有的报 +// 错") rather than a finding ("看一下日志,端口被占用", "检查一遍全部正常"). func assistantRerunObjectNoun(object, word string) bool { switch word { case "报错", "超时", "失败": trimmed := strings.TrimLeft(object, " \t一下遍次") - return strings.HasPrefix(trimmed, word) + return strings.HasPrefix(trimmed, word) || strings.Contains(object, "的"+word) } return false } @@ -1371,13 +1385,26 @@ func assistantRerunReportsOutcome(rest string) bool { return !assistantRerunPlanClauses(rest) } +// assistantRerunConnectsStep reports whether the clause after a rerun +// instruction names the next step. A clause without a connector +// ("重跑失败的测试,整理为表格") is read as if it had one. func assistantRerunConnectsStep(connector, rest string) bool { if rest == "" || assistantRerunReportsOutcome(rest) { return false } - return assistantPlanStep(connector+rest) && (assistantPlanProse(rest) || assistantPlanBareStep(rest)) + if connector == "" { + connector = "然后" + } + // "在 Windows 上的" only places the step's object ("修复在 Windows 上的 + // 路径问题"); its 在 is not a statement that something is somewhere. + step := assistantStepPlaceAttr.ReplaceAllString(rest, "") + return assistantPlanStep(connector+rest) && (assistantPlanProse(step) || assistantPlanBareStep(step)) } +// assistantStepPlaceAttr is a place that qualifies a step's object: +// "在 Windows 上的", "在 main 分支上的", "在日志里的". +var assistantStepPlaceAttr = regexp.MustCompile(`在[^,,、。;;::!!??]{1,20}?(?:上|里|中|内|下)的`) + func assistantProgressOnly(message string, messages []assistantMessage) bool { text := strings.TrimSpace(message) if text == "" || len([]rune(text)) > 600 || strings.Contains(text, "```") { @@ -1549,13 +1576,20 @@ var assistantStepQuestion = regexp.MustCompile(`是否|有没有|能否|能不 // noun ("检查输出", "查看完整日志", "查看完成情况") is part of the object. var assistantStepComplement = regexp.MustCompile(`^(?:到|下来|了|出[^输错]|完[^整成])`) -// assistantStepTargetVerb turns its object into something, so a 为 right -// after it names the target of the step ("整理为表格", "修改为异步调用", -// "更新为最新版本", "汇总为一份报告"). After any other step verb 为 states -// the verdict of what the step looked at ("确认为端口冲突", "定位为超时", -// "分析为端口冲突导致"); that is also how main reads such a clause after a -// rerun. -var assistantStepTargetVerb = regexp.MustCompile(`^(?:重新)?(?:修复|修改|修正|修|整理|汇总|收集|读取|记录|更新|调整|补充|补上)$`) +// assistantStepJudgeVerb opens a step that judges what it looks at, so a +// 为 right after it states the verdict ("确认为端口冲突", "定位为超时", +// "排查为缓存问题"). After a step verb that transforms its object 为 names +// the target ("整理为表格", "修改为异步调用", "更新为最新版本"). +var assistantStepJudgeVerb = regexp.MustCompile(`^(?:重新)?(?:查看|查一下|查查|查|检查|核对|核查|定位|排查|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查)$`) + +// assistantStepAnalyseVerb takes 为 either way: "分析为三大类" and "对比为 +// 表格" name the form the step produces, "分析为端口冲突导致" states what +// the analysis found. Only a cause (assistantStepCause) makes it a verdict. +var assistantStepAnalyseVerb = regexp.MustCompile(`^(?:重新)?(?:分析|对比|比对|比较)$`) + +// assistantStepCause closes a verdict that names what caused the outcome: +// "分析为端口冲突导致", "分析为端口冲突导致的超时", "分析为配置错误所致". +var assistantStepCause = regexp.MustCompile(`导致|引起|造成|所致|引发|致使`) // assistantStepFindingSubject names what a finding is about, so 为 or 在 // after it states the finding ("定位原因为端口冲突", "确认问题在配置文件"). @@ -1569,8 +1603,15 @@ var assistantStepFindingSubject = regexp.MustCompile(`(?:原因|根因|问题| var assistantStepStateWord = regexp.MustCompile(`没有|已经|已|无`) // assistantStepObjectLead may open a step's object before a state word that -// qualifies it: "检查一下已有的测试用例", "核对所有没有提交的改动". -var assistantStepObjectLead = regexp.MustCompile(`^(?:一下|一遍|一次|下|遍|所有|这些|那些|各个|每个)?$`) +// qualifies it, any number of times: "检查一下已有的测试用例", "核对所有这些 +// 没有提交的改动", "检查一遍所有已有的测试用例". +var assistantStepObjectLead = regexp.MustCompile(`^(?:一下|一遍|一次|下|遍|所有|全部|这些|那些|这个|那个|各个|每个|\s)*$`) + +// assistantStepObjectPlace is a place in front of the state word ("项目里", +// "日志里", "仓库中", "main 分支上"). It may open an object qualified by +// 已/已经 ("检查项目里已有的配置", "查看日志里已经存在的错误"), but before +// 没有/无 it makes an existential finding ("检查日志里没有新的异常"). +var assistantStepObjectPlace = regexp.MustCompile(`^(?:一下|一遍|一次|下|遍|所有|全部|这些|那些|各个|每个|\s)*(?:在\s*)?[^,,、。;;::!!??\s]+(?:\s+[^,,、。;;::!!??\s]+)*?(?:里面|里|中|上|内)(?:一下|一遍|所有|全部|这些|那些|各个|每个|\s)*$`) // assistantStepQualifierSpan is how far, in runes, the 的 of an attribute // may follow its state word: "已有的", "无用的", "没有提交的", "已经存在的". @@ -1578,20 +1619,29 @@ const assistantStepQualifierSpan = 3 // assistantStepStateQualifies reports whether the state word at // object[start:end] only qualifies the step's object ("检查已有的测试用例", -// "检查无用的依赖", "核对没有提交的改动", "确认没有遗漏的文件"). It does so -// only when it opens the object and a 的 closes a short attribute right after -// it. With a subject in front ("检查日志已无明显的异常") or a longer phrase -// after it ("检查日志没有新的异常" has 日志 in front; "确认已经没有之前的异常" -// has another state word before 的) it is the predicate and states the -// finding. +// "检查无用的依赖", "核对所有这些没有提交的改动", "检查项目里已有的配置"). +// It does so only when a 的 closes a short attribute right after it, and +// what comes before it is a quantifier or demonstrative +// (assistantStepObjectLead) or, for 已/已经, a place (assistantStepObjectPlace). +// With a subject in front ("检查日志已无明显的异常", "检查日志没有新的异常"), +// an existential after a place ("检查日志里没有新的异常"), another state word +// before 的 ("确认日志里已经没有之前的异常") or a verb phrase ("检查日志已经 +// 有新的报错") it is the predicate and states the finding. func assistantStepStateQualifies(object string, start, end int) bool { - if !assistantStepObjectLead.MatchString(strings.TrimSpace(object[:start])) { - return false + lead, word := strings.TrimSpace(object[:start]), object[start:end] + if !assistantStepObjectLead.MatchString(lead) { + if !strings.HasPrefix(word, "已") || assistantStepStateWord.MatchString(lead) || !assistantStepObjectPlace.MatchString(lead) { + return false + } } attr, _, ok := strings.Cut(object[end:], "的") if !ok || attr == "" || utf8.RuneCountInString(attr) > assistantStepQualifierSpan { return false } + // "已有的" qualifies; "已经有新的报错" states what is there now. + if strings.HasPrefix(attr, "有") && attr != "有" { + return false + } return !strings.ContainsFunc(attr, unicode.IsSpace) && !strings.ContainsAny(attr, ",,、。;;::") && !assistantStepStateWord.MatchString(attr) } @@ -1641,17 +1691,61 @@ func assistantStepReportsOutcome(rest string) bool { // the next step is to reach ("修改后确保接口没问题"), not a finding. var assistantStepGoalStart = []string{"确保", "保证", "验证", "确认", "检查", "再", "继续", "然后", "接着", "随后"} +// assistantStepScopeNoun closes an object whose parts a colon goes on to +// list: "查看现在的配置:端口和超时", "核对这些参数:并发和重试次数". +var assistantStepScopeNoun = regexp.MustCompile(`(?:配置|设置|参数|选项|字段|范围|内容|重点|几项|几点|方面|指标)$`) + +// assistantStepListSep joins the parts of such a list. +var assistantStepListSep = regexp.MustCompile(`\s*(?:以及|和|及|与|跟|、)\s*`) + +// assistantStepColonLists reports whether a colon after a step's object +// lists the parts the step covers rather than stating what it found: the +// object ends in a scope noun and the colon is followed by two or more bare +// names ("查看现在的配置:端口和超时"). "再查看日志:端口冲突" and "查看现在 +// 的配置:超时太短" state a finding. +func assistantStepColonLists(object, list string) bool { + if !assistantStepScopeNoun.MatchString(strings.TrimSpace(object)) { + return false + } + parts := assistantStepListSep.Split(strings.TrimSpace(list), -1) + if len(parts) < 2 { + return false + } + for _, part := range parts { + if part == "" || utf8.RuneCountInString(part) > assistantStepListPartMax || assistantStepStateWord.MatchString(part) || + strings.ContainsAny(part, ",,。;;::") || assistantPlanStatement.MatchString(part) { + return false + } + for _, word := range assistantRerunVerdictWord { + // 报错, 超时 and 失败 also name a setting or a kind of + // error in such a list ("端口和超时"). + if strings.Contains(part, word) && !(strings.HasSuffix(part, word) && assistantRerunObjectNoun(word, word)) { + return false + } + } + for _, word := range assistantRerunOutcomeWord { + if strings.Contains(part, word) { + return false + } + } + } + return true +} + +// assistantStepListPartMax bounds, in runes, one name in such a list. +const assistantStepListPartMax = 8 + // assistantStepFindingIn reports whether a step's object states how things // turned out: "确认为端口冲突", "定位原因为端口冲突", "排查原因:端口冲突", // "检查日志没有异常", "检查端口已释放", "修复后全绿", "确认问题在配置文件". // verb is the step verb the object follows. Each marker counts only where it -// states a finding: 为 after a step that does not transform its object or -// after a finding subject, 在 after a finding subject, 没有/无/已 as the +// states a finding: 为 after a judging verb, after an analysing verb when it +// names a cause, or after a finding subject, 在 after a finding subject, 没有/无/已 as the // predicate rather than opening a short attribute of the object // (assistantStepStateQualifies). "整理为表格", "记录在文档里", "检查已有的 // 测试用例" and "检查无用的依赖" are still steps. A colon, half or full -// width, that opens a plan header ("检查范围:配置文件和测试") is part of the -// step. +// width, that opens a plan header ("检查范围:配置文件和测试") or lists the +// parts of the object ("查看现在的配置:端口和超时") is part of the step. func assistantStepFindingIn(verb, object string) bool { i := strings.IndexAny(object, "::") if i >= 0 { @@ -1663,10 +1757,10 @@ func assistantStepFindingIn(verb, object string) bool { break } } - if !header { + _, size := utf8.DecodeRuneInString(object[i:]) + if !header && !assistantStepColonLists(before, object[i+size:]) { return true } - _, size := utf8.DecodeRuneInString(object[i:]) object = object[i+size:] } if strings.HasPrefix(object, "后") { @@ -1681,7 +1775,8 @@ func assistantStepFindingIn(verb, object string) bool { if strings.Contains(object, "确实") { return true } - if strings.HasPrefix(object, "为") && !assistantStepTargetVerb.MatchString(verb) { + if strings.HasPrefix(object, "为") && (assistantStepJudgeVerb.MatchString(verb) || + assistantStepAnalyseVerb.MatchString(verb) && assistantStepCause.MatchString(object)) { return true } if assistantStepFindingSubject.MatchString(object) { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 12b0b25d2..0fd89cc0f 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -244,6 +244,15 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"rerun then check then upgrade", "rerun-check-then-upgrade-step", `我先看一下。\n重跑失败的测试,然后检查完依赖再升级。`}, {"rerun then no missed files", "rerun-no-missed-files-step", `我先看一下。\n重跑失败的测试,然后确认没有遗漏的文件。`}, {"rerun then uncommitted changes", "rerun-uncommitted-step", `我先看一下。\n重跑失败的测试,然后核对所有没有提交的改动。`}, + {"rerun then config in project", "rerun-config-in-project-step", `我先看一下。\n重跑失败的测试,然后检查项目里已有的配置。`}, + {"rerun then all these uncommitted", "rerun-all-these-uncommitted-step", `我先看一下。\n重跑失败的测试,然后核对所有这些没有提交的改动。`}, + {"rerun then every existing test", "rerun-every-existing-test-step", `我先看一下。\n重跑失败的测试,然后检查一遍所有已有的测试用例。`}, + {"rerun then errors in log", "rerun-errors-in-log-step", `我先看一下。\n重跑失败的测试,然后查看日志里已经存在的错误。`}, + {"rerun then analysed into groups", "rerun-analysed-into-groups-step", `我先看一下。\n重跑失败的测试,然后分析为三大类。`}, + {"rerun then compared into a table", "rerun-compared-into-table-step", `我先看一下。\n重跑失败的测试,然后对比为表格。`}, + {"rerun then fix on Windows", "rerun-fix-on-windows-step", `我先看一下。\n重跑失败的测试,然后修复在 Windows 上的路径问题。`}, + {"rerun then config colon list", "rerun-config-colon-list-step", `我先看一下。\n重跑失败的测试,然后查看现在的配置:端口和超时。`}, + {"rerun comma into a table", "rerun-comma-into-table-step", `我先看一下。\n重跑失败的测试,整理为表格。`}, } { t.Run(tc.name, func(t *testing.T) { runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") @@ -272,6 +281,10 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-no-new-anomaly", `我先看一下。\n重跑失败的测试,然后检查日志没有新的异常。`, "1", "没有新的异常") runMultiLinePlanCase(t, "failed-rerun-analysed-as", `我先看一下。\n重跑失败的测试,然后分析为端口冲突导致。`, "1", "端口冲突导致") runMultiLinePlanCase(t, "failed-rerun-full-width-colon", `我先看一下。\n重跑失败的测试,然后确认范围:为端口冲突。`, "1", "为端口冲突") + runMultiLinePlanCase(t, "failed-rerun-none-in-log", `我先看一下。\n重跑失败的测试,然后检查日志里没有新的异常。`, "1", "没有新的异常") + runMultiLinePlanCase(t, "failed-rerun-caused-by", `我先看一下。\n重跑失败的测试,然后分析为端口冲突引起。`, "1", "端口冲突引起") + runMultiLinePlanCase(t, "failed-rerun-log-colon-list", `我先看一下。\n重跑失败的测试,然后查看日志:端口冲突和超时。`, "1", "端口冲突和超时") + runMultiLinePlanCase(t, "failed-rerun-comma-outcome", `我先看一下。\n重跑失败的测试,全部通过。`, "1", "全部通过") runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") runMultiLinePlanCase(t, "verdict-answer", `我先跑测试。\n测试全过。`, "1", "测试全过") @@ -591,6 +604,82 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试,然后确认范围:为端口冲突。", false}, {"我先看一下。\n重跑失败的测试,然后定位范围:后全绿。", false}, {"我先看一下。\n重跑失败的测试,然后定位范围:后全绿。", false}, + // A place or several quantifiers may come before 已/已经 that only + // qualify the step's object; before 没有/无 a place makes an + // existential finding ("日志里没有新的异常"). + {"我先看一下。\n重跑失败的测试,然后检查项目里已有的配置。", true}, + {"我先看一下。\n重跑失败的测试,然后核对所有这些没有提交的改动。", true}, + {"我先看一下。\n重跑失败的测试,然后检查一遍所有已有的测试用例。", true}, + {"我先看一下。\n重跑失败的测试,然后查看日志里已经存在的错误。", true}, + {"我先看一下。\n重跑失败的测试,然后检查仓库中已有的分支。", true}, + {"我先看一下。\n重跑失败的测试,然后核对 main 分支上已有的提交。", true}, + {"我先看一下。\n重跑失败的测试,然后核对这些已有的配置项。", true}, + {"我先看一下。\n重跑失败的测试,再查看日志里已有的报错。", true}, + {"我先看一下。\n重跑失败的测试,然后检查日志里没有新的异常。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志里无多余的连接。", false}, + {"我先看一下。\n重跑失败的测试,然后确认项目里已无冲突的配置。", false}, + {"我先看一下。\n重跑失败的测试,然后确认仓库中已经没有冲突的文件。", false}, + {"我先看一下。\n重跑失败的测试,然后检查日志已经有新的报错。", false}, + {"我先看一下。\n重跑失败的测试,然后查看日志里已经有新的报错。", false}, + // 为 after an analysing verb names the form the step produces unless + // it names a cause; after a judging verb it is the verdict. + {"我先看一下。\n重跑失败的测试,然后分析为三大类。", true}, + {"我先看一下。\n重跑失败的测试,然后对比为表格。", true}, + {"我先看一下。\n重跑失败的测试,然后分析为几个阶段。", true}, + {"我先看一下。\n重跑失败的测试,然后比较为两列。", true}, + {"我先看一下。\n重跑失败的测试,然后汇总为三大类。", true}, + {"我先看一下。\n重跑失败的测试,然后整理为端口冲突导致的问题清单。", true}, + {"我先看一下。\n然后分析为三大类。", true}, + {"我先看一下。\n然后对比为表格。", true}, + {"我先看一下。\n重跑失败的测试,然后分析为端口冲突引起。", false}, + {"我先看一下。\n重跑失败的测试,然后分析为内存泄漏引发的崩溃。", false}, + {"我先看一下。\n重跑失败的测试,然后对比为配置错误所致。", false}, + {"我先看一下。\n重跑失败的测试,然后定位为端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,然后排查为缓存问题。", false}, + {"我先看一下。\n重跑失败的测试,然后检查为配置错误。", false}, + {"我先看一下。\n重跑失败的测试,然后对比为两次结果相同。", false}, + // "在 X 上的" places the step's object. + {"我先看一下。\n重跑失败的测试,然后修复在 Windows 上的路径问题。", true}, + {"我先看一下。\n重跑失败的测试,然后修复在 macOS 上的编译问题。", true}, + {"我先看一下。\n重跑失败的测试,然后修复在测试环境里的权限问题。", true}, + // A colon after a scope noun that lists bare names is part of the + // step; one that states something is the finding. + {"我先看一下。\n重跑失败的测试,然后查看现在的配置:端口和超时。", true}, + {"我先看一下。\n重跑失败的测试,然后查看当前的设置:端口和超时。", true}, + {"我先看一下。\n重跑失败的测试,然后核对这些参数:并发和重试次数。", true}, + {"我先看一下。\n重跑失败的测试,然后查看日志:端口冲突和超时。", false}, + {"我先看一下。\n重跑失败的测试,然后查看现在的配置:端口已占用。", false}, + {"我先看一下。\n重跑失败的测试,然后查看现在的配置:超时太短。", false}, + {"我先看一下。\n重跑失败的测试,查看现在的配置:端口和超时都正常。", false}, + {"我先看一下。\n重跑失败的测试,然后查看现在的配置:端口被占用和超时。", false}, + // A step may follow the rerun after a comma without a connector; an + // outcome there is still delivered. + {"我先看一下。\n重跑失败的测试,整理为表格。", true}, + {"我先看一下。\n重跑失败的测试,查看日志。", true}, + {"我先看一下。\n重跑失败的用例,修复报错。", true}, + {"我先看一下。\n重跑失败的测试后整理为表格。", true}, + {"我先看一下。\n重跑失败的用例后,查看日志。", true}, + {"我先看一下。\n重跑失败的测试,汇总为一份报告。", true}, + {"我先看一下。\n重跑失败的测试,检查一下已有的测试用例。", true}, + {"我先看一下。\n重跑失败的测试,端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,还是红。", false}, + {"我先看一下。\n重跑失败的测试,全部通过。", false}, + {"我先看一下。\n重跑失败的测试,结果一样。", false}, + {"我先看一下。\n重跑失败的测试,3 个通过。", false}, + {"我先看一下。\n重跑失败的测试,确认为端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,查看日志:端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,修复了报错。", false}, + {"我先看一下。\n重跑失败的测试,修复后全绿。", false}, + {"我先看一下。\n重跑失败的测试,排查出端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,定位到超时配置太短。", false}, + {"我先看一下。\n重跑失败的测试,查看日志发现端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,查看日志确认是端口冲突。", false}, + {"我先看一下。\n重跑失败的测试,查看日志,端口被占用。", false}, + {"我先看一下。\n重跑失败的测试,日志显示连接超时。", false}, + {"我先看一下。\n重跑失败的测试,日志里没有异常。", false}, + {"我先看一下。\n重跑失败的测试,检查日志没有异常。", false}, + {"我先看一下。\n重跑失败的用例后,仍然报错。", false}, + {"我先看一下。\n重跑失败的用例后,确认为端口冲突。", false}, // The same steps without a finding still continue the plan; 出/完 // inside a step's object is part of the noun ("检查输出", "完整日志"). {"我先看一下。\n重跑失败的测试,然后检查输出。", true}, @@ -944,7 +1033,8 @@ JSON } // A colon is skipped by its width: after a full-width ":" the object is read -// exactly as after a half-width ":". +// exactly as after a half-width ":". The same table pins how a colon list and +// 为 after each kind of verb are read. func TestAssistantStepFindingInColonWidth(t *testing.T) { for _, tc := range []struct { verb, object string @@ -958,6 +1048,18 @@ func TestAssistantStepFindingInColonWidth(t *testing.T) { {"检查", "范围:配置文件和测试", false}, {"修改", "范围:后确保接口没问题", false}, {"修改", "范围:后确保接口没问题", false}, + // A colon after a scope noun that lists bare names, either width. + {"查看", "现在的配置:端口和超时", false}, + {"查看", "现在的配置:端口和超时", false}, + {"查看", "日志:端口冲突和超时", true}, + {"查看", "现在的配置:超时太短", true}, + {"查看", "现在的配置:端口已占用", true}, + // 为 after an analysing verb is a verdict only when it names a cause. + {"分析", "为三大类", false}, + {"对比", "为表格", false}, + {"分析", "为端口冲突导致", true}, + {"确认", "为端口冲突", true}, + {"整理", "为端口冲突导致的问题清单", false}, } { if got := assistantStepFindingIn(tc.verb, tc.object); got != tc.want { t.Errorf("assistantStepFindingIn(%q, %q) = %v, want %v", tc.verb, tc.object, got, tc.want) From e7912fe3a264cfd9d99727319ca22ccb16eef94c Mon Sep 17 00:00:00 2001 From: roberttwang Date: Thu, 3 Dec 2026 03:52:54 +0000 Subject: [PATCH 28/28] fix(assistant): keep a rerun's dunhao colon list within one clause MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit assistantRerunClauseSep also split on the enumeration comma 、, so the bare names of a colon list after a scope noun ("重跑失败的用例,然后检查 现在的配置:端口、超时、并发。") were handed to the clause verb check as clause fragments without a verb, and the plan read as the final answer on 9983ae4e where dabf2642 continued. The comma no longer splits; the list stays within its clause for assistantStepColonLists, which also grows the scope noun 日志 ("查看日志:端口、超时") and rejects parts that name a state (端口冲突) so a colon that states what the log showed still delivers ("查看日志:端口冲突、超时"). --- internal/httpapi/assistant.go | 25 ++++++++++++++----- .../httpapi/assistant_progress_text_test.go | 23 +++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 2c88be3a8..2561c9006 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1306,8 +1306,12 @@ var assistantRerunQuestion = regexp.MustCompile(`是否|是不是|有没有|能 // assistantRerunStepConnector opens a later clause of the same plan step. var assistantRerunStepConnector = regexp.MustCompile(`^(?:然后|接着|随后|之后|再|并且|并|同时)\s*`) -// assistantRerunClauseSep splits a continuation into its comma clauses. -var assistantRerunClauseSep = regexp.MustCompile(`[,,、]`) +// assistantRerunClauseSep splits a continuation into its comma clauses. The +// enumeration comma 、 does not split: it either lists the parts a colon +// already announced ("检查现在的配置:端口、超时、并发") or joins parallel +// steps within one clause ("然后查看日志、对比配置"), and splitting it would +// hand the list's bare names to the clause verb check below. +var assistantRerunClauseSep = regexp.MustCompile(`[,,]`) // assistantRerunPlanClauses reports whether every comma clause of rest names // a next step: it opens with a step verb, has no result complement, and @@ -1692,16 +1696,24 @@ func assistantStepReportsOutcome(rest string) bool { var assistantStepGoalStart = []string{"确保", "保证", "验证", "确认", "检查", "再", "继续", "然后", "接着", "随后"} // assistantStepScopeNoun closes an object whose parts a colon goes on to -// list: "查看现在的配置:端口和超时", "核对这些参数:并发和重试次数". -var assistantStepScopeNoun = regexp.MustCompile(`(?:配置|设置|参数|选项|字段|范围|内容|重点|几项|几点|方面|指标)$`) +// list: "查看现在的配置:端口和超时", "核对这些参数:并发和重试次数", +// "查看日志:端口、超时". +var assistantStepScopeNoun = regexp.MustCompile(`(?:配置|设置|参数|选项|字段|范围|内容|重点|几项|几点|方面|指标|日志)$`) // assistantStepListSep joins the parts of such a list. var assistantStepListSep = regexp.MustCompile(`\s*(?:以及|和|及|与|跟|、)\s*`) +// assistantStepListPartState names the state a thing is in ("端口冲突", +// "端口占用") rather than an aspect of the object, so a part carrying it +// states a finding even behind a scope noun that also opens bare names +// ("查看日志:端口冲突" vs "查看日志:端口、超时"). +var assistantStepListPartState = regexp.MustCompile(`冲突|异常|占用|死锁|崩溃|挂掉|泄露|泄漏`) + // assistantStepColonLists reports whether a colon after a step's object // lists the parts the step covers rather than stating what it found: the // object ends in a scope noun and the colon is followed by two or more bare -// names ("查看现在的配置:端口和超时"). "再查看日志:端口冲突" and "查看现在 +// names ("查看现在的配置:端口和超时", "检查现在的配置:端口、超时、并发", +// "查看日志:端口、超时"). "再查看日志:端口冲突" and "查看现在 // 的配置:超时太短" state a finding. func assistantStepColonLists(object, list string) bool { if !assistantStepScopeNoun.MatchString(strings.TrimSpace(object)) { @@ -1713,7 +1725,8 @@ func assistantStepColonLists(object, list string) bool { } for _, part := range parts { if part == "" || utf8.RuneCountInString(part) > assistantStepListPartMax || assistantStepStateWord.MatchString(part) || - strings.ContainsAny(part, ",,。;;::") || assistantPlanStatement.MatchString(part) { + strings.ContainsAny(part, ",,。;;::") || assistantPlanStatement.MatchString(part) || + assistantStepListPartState.MatchString(part) { return false } for _, word := range assistantRerunVerdictWord { diff --git a/internal/httpapi/assistant_progress_text_test.go b/internal/httpapi/assistant_progress_text_test.go index 0fd89cc0f..db913a31e 100644 --- a/internal/httpapi/assistant_progress_text_test.go +++ b/internal/httpapi/assistant_progress_text_test.go @@ -252,6 +252,10 @@ func TestAssistantMultiLinePlanBeforeToolsStillContinues(t *testing.T) { {"rerun then compared into a table", "rerun-compared-into-table-step", `我先看一下。\n重跑失败的测试,然后对比为表格。`}, {"rerun then fix on Windows", "rerun-fix-on-windows-step", `我先看一下。\n重跑失败的测试,然后修复在 Windows 上的路径问题。`}, {"rerun then config colon list", "rerun-config-colon-list-step", `我先看一下。\n重跑失败的测试,然后查看现在的配置:端口和超时。`}, + {"rerun then config dunhao colon list", "rerun-config-dunhao-colon-list-step", `我先看一下。\n重跑失败的用例,然后检查现在的配置:端口、超时、并发。`}, + {"rerun then config dunhao colon pair", "rerun-config-dunhao-colon-pair-step", `我先看一下。\n重跑失败的测试,然后查看配置:端口、重试次数。`}, + {"rerun then params dunhao colon pair", "rerun-params-dunhao-colon-pair-step", `我先看一下。\n重跑失败的测试,然后核对参数:并发、超时。`}, + {"rerun then log dunhao colon pair", "rerun-log-dunhao-colon-pair-step", `我先看一下。\n重跑失败的测试,然后查看日志:端口、超时。`}, {"rerun comma into a table", "rerun-comma-into-table-step", `我先看一下。\n重跑失败的测试,整理为表格。`}, } { t.Run(tc.name, func(t *testing.T) { @@ -284,6 +288,9 @@ func TestAssistantPlanOpeningWithVerdictIsTheAnswer(t *testing.T) { runMultiLinePlanCase(t, "failed-rerun-none-in-log", `我先看一下。\n重跑失败的测试,然后检查日志里没有新的异常。`, "1", "没有新的异常") runMultiLinePlanCase(t, "failed-rerun-caused-by", `我先看一下。\n重跑失败的测试,然后分析为端口冲突引起。`, "1", "端口冲突引起") runMultiLinePlanCase(t, "failed-rerun-log-colon-list", `我先看一下。\n重跑失败的测试,然后查看日志:端口冲突和超时。`, "1", "端口冲突和超时") + runMultiLinePlanCase(t, "failed-rerun-log-colon-single", `我先看一下。\n重跑失败的测试,然后查看日志:端口冲突。`, "1", "端口冲突") + runMultiLinePlanCase(t, "failed-rerun-log-dunhao-finding", `我先看一下。\n重跑失败的测试,然后查看日志:端口冲突、超时。`, "1", "端口冲突、超时") + runMultiLinePlanCase(t, "failed-rerun-config-dunhao-finding", `我先看一下。\n重跑失败的测试,然后查看现在的配置:端口冲突、超时。`, "1", "端口冲突、超时") runMultiLinePlanCase(t, "failed-rerun-comma-outcome", `我先看一下。\n重跑失败的测试,全部通过。`, "1", "全部通过") runMultiLinePlanCase(t, "failed-rerun-next-line", `我先看一下。\n重跑失败的测试。\n还是红。`, "1", "还是红") runMultiLinePlanCase(t, "inline-summary", `我先总结一下,项目里有 4 个 LOOP。`, "1", "4 个 LOOP") @@ -648,6 +655,15 @@ func TestAssistantProgressOnlyRequiresPlanLines(t *testing.T) { {"我先看一下。\n重跑失败的测试,然后查看当前的设置:端口和超时。", true}, {"我先看一下。\n重跑失败的测试,然后核对这些参数:并发和重试次数。", true}, {"我先看一下。\n重跑失败的测试,然后查看日志:端口冲突和超时。", false}, + // The enumeration comma 、 joins the listed parts as well; a part + // that names a state ("端口冲突") still states the finding. + {"我先看一下。\n重跑失败的用例,然后检查现在的配置:端口、超时、并发。", true}, + {"我先看一下。\n重跑失败的测试,然后查看配置:端口、重试次数。", true}, + {"我先看一下。\n重跑失败的测试,然后核对参数:并发、超时。", true}, + {"我先看一下。\n重跑失败的测试,然后查看日志:端口、超时。", true}, + {"我先看一下。\n重跑失败的测试,然后查看配置:端口、重试次数、并发上限。", true}, + {"我先看一下。\n重跑失败的测试,然后查看日志:端口冲突、超时。", false}, + {"我先看一下。\n重跑失败的测试,然后查看现在的配置:端口冲突、超时。", false}, {"我先看一下。\n重跑失败的测试,然后查看现在的配置:端口已占用。", false}, {"我先看一下。\n重跑失败的测试,然后查看现在的配置:超时太短。", false}, {"我先看一下。\n重跑失败的测试,查看现在的配置:端口和超时都正常。", false}, @@ -1054,6 +1070,13 @@ func TestAssistantStepFindingInColonWidth(t *testing.T) { {"查看", "日志:端口冲突和超时", true}, {"查看", "现在的配置:超时太短", true}, {"查看", "现在的配置:端口已占用", true}, + // The enumeration comma 、 joins the listed parts as well; a part + // that names a state ("端口冲突") still states the finding. + {"查看", "现在的配置:端口、超时、并发", false}, + {"查看", "日志:端口、超时", false}, + {"查看", "参数:并发、重试次数", false}, + {"查看", "日志:端口冲突、超时", true}, + {"查看", "现在的配置:端口冲突、超时", true}, // 为 after an analysing verb is a verdict only when it names a cause. {"分析", "为三大类", false}, {"对比", "为表格", false},