diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index ca220a386..2561c9006 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -9,12 +9,15 @@ import ( "net/http" "os" "path/filepath" + "regexp" "slices" "sort" "strings" "sync" "sync/atomic" "time" + "unicode" + "unicode/utf8" "agentflow/internal/application" "agentflow/internal/domain" @@ -850,6 +853,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() @@ -921,7 +925,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.roundEvents()), req.Messages) { if progressRetries >= 2 { fail(502, "模型连续只返回进度说明,尚未完成请求;执行记录已保留,请核对后继续。") return @@ -1168,9 +1172,246 @@ 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, 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 { + if event.Kind == "tool" && event.Parent == "" { + last = i + } + } + 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") +} + +// 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]*(再|然后|接着|随后)?)`) + +// stripAssistantRerunFailedTarget drops "失败的" from a rerun instruction so +// 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 { + return text + } + var b strings.Builder + last := 0 + for _, m := range matches { + // 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 := "" + 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) { + continue + } + } else if !assistantRerunStandaloneStep(text[m[1]:]) { + 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() +} + +// 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) + if line == "" { + continue + } + if rest, ok := cutAssistantListMarker(line); ok { + line = rest + } + line = strings.TrimRight(line, "。.!!;;") + if !assistantRerunPlanClauses(line) { + return false + } + } + 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{"还是", "仍", "依然", "依旧", "照样", "又", "再次", "发现", "表明", "原来", "变绿", "变红"} + +// 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(`^(?:查看|查一下|查查|查|检查|核对|核查|对比|比对|比较|修复|修改|修正|修|定位|排查|整理|分析|汇总|收集|读取|阅读|看看|看下|看一下|看|确认|验证|复查|复核|调查|追查|记录|更新|调整|补充|补上|重跑|重试|重新运行|重新执行)`) + +// 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(`是否|是不是|有没有|能否|能不能|会不会|吗|什么|哪[个些里]|为什么|为何|怎么|多少`) + +// assistantRerunStepConnector opens a later clause of the same plan step. +var assistantRerunStepConnector = regexp.MustCompile(`^(?:然后|接着|随后|之后|再|并且|并|同时)\s*`) + +// 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 +// 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 + } + } + if !assistantRerunQuestion.MatchString(clause) { + // The step's own object may name what failed ("修复报错", + // "定位超时原因"); only a finding or a verdict after the object + // is a result. + object := clause[loc[1]:] + 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 + } + } + } + } + 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) || strings.Contains(object, "的"+word) + } + return false +} + +func assistantRerunReportsOutcome(rest string) bool { + if assistantRerunOutcome.MatchString(rest) { + return true + } + for _, word := range assistantRerunOutcomeWord { + if strings.Contains(rest, word) { + return true + } + } + 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 + } + 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 len([]rune(text)) > 600 || strings.Contains(text, "```") { + if text == "" || len([]rune(text)) > 600 || strings.Contains(text, "```") { return false } if len(messages) > 0 { @@ -1181,19 +1422,1034 @@ func assistantProgressOnly(message string, messages []assistantMessage) bool { } } } + text = stripAssistantRerunFailedTarget(text) for _, blocker := range []string{"无法", "不能", "失败", "缺少", "不可用", "需要你", "请提供", "请确认", "请选择", "?", "?", "已完成", "已生成", "已创建", "已启动", "已保存", "run-"} { if strings.Contains(text, blocker) { return false } } + // A progress note opens with a plan and only continues it: more 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, announced, firm := false, false, false + for line := range strings.SplitSeq(text, "\n") { + line = strings.TrimSpace(line) + if line == "" { + continue + } + if !opened { + if !assistantProgressSentence(line) { + return false + } + opened, announced = true, assistantAnnouncesAnswer(line) + firm = announced && assistantAnswerFirm.MatchString(line) + // "我先说结论:可以合并。" carries the answer on its opening line; + // "我先说结论,再展开原因。" only announces it and goes on planning. + if firm && assistantAnnouncementRest(line) != "" && !assistantAnnouncementPlansAhead(line) { + return false + } + continue + } + if announced { + // "我先说结论:" 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 + } + } + 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(`[::,,。]`) + +// 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 { + text = strings.TrimSpace(text) for _, prefix := range []string{"稍等", "请稍等", "好的,我先", "我先", "让我先", "接下来我会", "接下来我将", "我会先", "I'll first", "Let me first"} { - if strings.HasPrefix(text, prefix) { + if rest, ok := strings.CutPrefix(text, prefix); ok { + return !assistantReportsDoneWork(rest) + } + } + 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 { + // 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) + } + } + 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) && !assistantPlanStatesVerdict(rest) + } + for _, prefix := range []string{"然后", "接着", "随后", "之后", "再", "Then", "Next"} { + if rest, ok := strings.CutPrefix(line, prefix); ok { + return !assistantReportsDoneWork(rest) && !assistantPlanStatesVerdict(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(`^(?:到|下来|了|出[^输错]|完[^整成])`) + +// 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 ("定位原因为端口冲突", "确认问题在配置文件"). +// 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 ("检查日志没有异常", "检查端口已释放", "检查日志已无明显的异常"). +// 无法 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, 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: "已有的", "无用的", "没有提交的", "已经存在的". +const assistantStepQualifierSpan = 3 + +// assistantStepStateQualifies reports whether the state word at +// object[start:end] only qualifies the step's object ("检查已有的测试用例", +// "检查无用的依赖", "核对所有这些没有提交的改动", "检查项目里已有的配置"). +// 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 { + 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) +} + +// 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, "") + 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 !assistantStepThen.MatchString(object) + } + return assistantStepFindingIn(rest[:loc[1]], object) +} + +// assistantStepGoalStart follows a perfective 后 that opens a precondition +// 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*`) + +// 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 "查看现在 +// 的配置:超时太短" 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) || + assistantStepListPartState.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 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 ("检查范围:配置文件和测试") 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 { + before := object[:i] + header := false + for _, lead := range assistantPlanLeadIn { + if strings.HasPrefix(strings.TrimSpace(before), lead) { + header = true + break + } + } + _, size := utf8.DecodeRuneInString(object[i:]) + if !header && !assistantStepColonLists(before, object[i+size:]) { + return true + } + object = object[i+size:] + } + if strings.HasPrefix(object, "后") { + tail := strings.TrimSpace(object[len("后"):]) + for _, goal := range assistantStepGoalStart { + if strings.HasPrefix(tail, goal) { + return false + } + } + return true + } + if strings.Contains(object, "确实") { + return true + } + if strings.HasPrefix(object, "为") && (assistantStepJudgeVerb.MatchString(verb) || + assistantStepAnalyseVerb.MatchString(verb) && assistantStepCause.MatchString(object)) { + return true + } + if assistantStepFindingSubject.MatchString(object) { + return true + } + for _, loc := range assistantStepStateWord.FindAllStringIndex(object, -1) { + if object[loc[0]:loc[1]] == "无" && strings.HasPrefix(object[loc[1]:], "法") { + continue + } + if !assistantStepStateQualifies(object, loc[0], loc[1]) { + return true + } + } + return false +} + +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). +// 测试 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 +// not plan a read. +var assistantPlanVerbEN = regexp.MustCompile(`(?i)\b(?:review|read|check|run|test)\b`) + +// 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{"全过", "都过", "通过", "没问题", "没有问题", "无异常", "没有异常", "干净"} + +// 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"} + +// 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 也干净"). +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. + rest, ok := assistantPlanPassVerdict(line) + return ok && !assistantPlanCheckIntentIn(rest) + } + if i := strings.Index(line, "干净"); i >= 0 && !assistantPlanCheckIntentIn(line) { + for _, verb := range assistantPlanCleanupVerb { + if strings.Contains(line[:i], verb) { + return false + } + } + return !assistantPlanCleanGoal(line, i) + } + 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 +// ("测试都通过,可以合并。"). 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 line, false + } + j += i + before := line[:j] + for _, skip := range []string{"全部", "都", "均", "也", "已经", "已"} { + for strings.HasSuffix(before, skip) { + before = strings.TrimSuffix(before, skip) + } + } + 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 + } + } + 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("通过") + } +} + +// 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 assistantPlanListRange = regexp.MustCompile(`^(?:显示|返回)(?:最近|最新|最早|前|最后)(?:的)?\s*(?:\d+|[一二两三四五六七八九十]+)\s*[条次个项行名份]`) + +// 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. +// 月/日/号 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, "") + } + 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(`[,,。;;!!]`) + +// These imperatives can follow a project, branch or object before the verb. +// Digits in that scope (such as "在 v2 分支上") are part of the target. +// 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(`^(?:快|慢|好|差|高|低|多|少|简单|复杂|稳定|清晰|方便)(?:[,,。;;!!]|$)`) + +// 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. +func assistantPlanProse(line string) bool { + line = strings.TrimSpace(line) + if line == "" { + return false + } + 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 + } + } + 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 + // 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 strings.HasPrefix(line, lead) { + 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 "重点关注 + // 最近 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)]) +} + +// 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 + } + line = assistantPlanStatementNoise.Replace(line) + line = assistantPlanAttributive.ReplaceAllString(line, "$1") + return !assistantPlanStatement.MatchString(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 +// (运行中/还在排队), 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 ("是否", +// "有没有", "改了没有") 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"} + +// 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 ("先确认服务是否正常") or the object of a step +// ("先修复报错", "最后排查异常"). +func assistantPlanStatesStatus(line string) bool { + 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 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. 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, 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, true + } + } + // "拿到 token 后接口返回 200" reports what followed. + return assistantPlanResultStart(rest, true) == len(rest), true +} + +// 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 { + 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 { + limit = loc[0] + } + } + } + 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 + } + } + 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. + // "导出结果到 CSV。" or "把结果发给你。" names where the result goes. + end := strings.TrimSpace(line[i+len("结果"):]) + 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. + return assistantPlanAwaitStart.MatchString(clause) + } + return false +} + +// 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(`^(?:(?:稍后|随后|接着|然后|再)\s*)*(?:等待?|等一下|查看|看看|看一下|拿|获取|收集|输出|给出|发出?|发送|展示|贴出|列出|对比|比较|整理|汇总|核对|分析|记录|写出|写入|汇报|报告|告诉你?|导出|保存|存下?|上传|下载|打印|回复|返回|提交|同步|推送|转发|附上|粘贴|复制|反馈|分享|公布|呈现)`) + +// 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 了. +var assistantPlanAwaitStart = regexp.MustCompile(`^(?:等待?|等到|等一下)[^了]*$`) + +// 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 assistantPlanVerbEN.MatchString(text) +} + +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) + } + 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) { raw = trimAssistantReplyText(raw) if raw == "" { 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 new file mode 100644 index 000000000..db913a31e --- /dev/null +++ b/internal/httpapi/assistant_progress_text_test.go @@ -0,0 +1,1091 @@ +package httpapi + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "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成员:只有负责人 alice。" + 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 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. 汇总节点状态`}, + {"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然后修改代码,让测试通过。`}, + {"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修到测试全部通过为止。`}, + {"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直到单测全部通过。`}, + {"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", `我先汇报结果,接着检查日志。`}, + {"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等构建完成。`}, + {"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重跑失败的测试,然后检查完依赖再升级。`}, + {"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 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) { + runMultiLinePlanCase(t, tc.session, tc.plan, "2", "均在正常运行") + }) + } +} + +// 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, "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, "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-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-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") + 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", "工作区已经干净") + 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) { + t.Helper() + 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":"%[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":"%[1]s"} +JSON +` + 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: 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) + 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()) + } +} + +// 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 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 + }{ + {"我先查了本项目的代码仓库和成员,结论如下。\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}, + {"我先看一下。\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}, + // 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}, + // 有/没有 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}, + // 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}, + {"我先看一下。\n重跑失败的测试用例。", true}, + {"我先看一下。\n重跑有失败的用例。", true}, + {"我先看一下。\n重跑失败的用例,仍然失败。", false}, + {"我先看一下。\n重跑失败的用例后还是报错。", false}, + {"我先看一下。\n重跑失败的测试后问题依旧。", false}, + {"我先看一下。\n重新运行失败的测试,依然超时。", false}, + {"我先看一下。\n重新执行失败的检查,仍未通过。", false}, + {"我先看一下。\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}, + // 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}, + // 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}, + // 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}, + // 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}, + // 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}, + {"我先看一下。\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}, + // 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}, + // 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}, + {"我先看一下。\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}, + {"我先看一下。\n重跑失败的测试,然后查看完整日志。", true}, + {"我先看一下。\n重跑失败的用例,再查看日志。\n再修复报错。", true}, + {"我先看一下。\n重跑失败的测试。\n- 查看日志\n- 修复报错", true}, + {"我先看一下。\n重跑失败的用例,然后确认为端口冲突。\n端口被占用。", false}, + {"我先看一下。\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等待构建结束,再给出结论。", true}, + {"我先看一下。\n显示最近 3 次提交。", true}, + {"我先看一下。\n返回最近 3 条记录。", true}, + {"我先看一下。\n返回最近 3 条记录时出错了。", false}, + {"我先看一下。\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}, + {"我先看一下。\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}, + {"我先看一下。\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}, + {"我先看一下。\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}, + {"我先了解一下需求。", 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}, + // 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}, + // 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}, + // 通过 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}, + // 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. + {"我先看一下仓库。\n检查通过,工作区干净。", false}, + {"我先看一下仓库。\n校验通过。", false}, + {"我先看一下仓库。\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确保 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}, + {"我先看一下提交。\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}, + // 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}, + // 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}, + // 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}, + {"我先看一下。\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}, + } + 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) + } + } +} + +// 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。\n在 v2 分支上跑测试。"}]}} +{"type":"result","result":"我先查看项目里的 LOOP。\n在 v2 分支上跑测试。"} +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) + } + }) +} + +// A colon is skipped by its width: after a full-width ":" the object is read +// 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 + want bool + }{ + {"确认", "范围:为端口冲突", true}, + {"确认", "范围:为端口冲突", true}, + {"确认", "范围:后全绿", true}, + {"确认", "范围:后全绿", true}, + {"检查", "范围:配置文件和测试", false}, + {"检查", "范围:配置文件和测试", false}, + {"修改", "范围:后确保接口没问题", false}, + {"修改", "范围:后确保接口没问题", false}, + // A colon after a scope noun that lists bare names, either width. + {"查看", "现在的配置:端口和超时", false}, + {"查看", "现在的配置:端口和超时", false}, + {"查看", "日志:端口冲突和超时", 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}, + {"分析", "为端口冲突导致", 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) + } + } +} diff --git a/internal/httpapi/bot_interactions.go b/internal/httpapi/bot_interactions.go index 3131119ac..a9866203e 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") { @@ -182,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 { @@ -190,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(cand.Message, 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 } }