Fix planned task splitting and progress summaries

This commit is contained in:
lpf
2026-03-06 19:22:57 +08:00
parent 623b401850
commit b4cf4a123b
3 changed files with 92 additions and 2 deletions

View File

@@ -103,7 +103,10 @@ func splitPlannedSegments(content string) []string {
return bullet
}
replaced := strings.NewReplacer("", ";", "\n", ";", "。然后", ";", " 然后 ", ";", " and then ", ";")
// Only split implicit plans on strong separators. Plain newlines are often
// just formatting inside a single request, and "然后/and then" frequently
// describes execution order inside one task rather than separate tasks.
replaced := strings.NewReplacer("", ";")
norm := replaced.Replace(content)
parts := strings.Split(norm, ";")
out := make([]string, 0, len(parts))
@@ -180,7 +183,7 @@ func (al *AgentLoop) publishPlannedTaskProgress(msg bus.InboundMessage, total in
if body == "" {
body = "(无输出)"
}
body = truncate(strings.ReplaceAll(body, "\n", " "), 280)
body = summarizePlannedTaskProgressBody(body, 6, 320)
content := fmt.Sprintf("进度 %d/%d任务%d已%s\n%s", idx, total, idx, status, body)
al.bus.PublishOutbound(bus.OutboundMessage{
Channel: msg.Channel,
@@ -189,6 +192,37 @@ func (al *AgentLoop) publishPlannedTaskProgress(msg bus.InboundMessage, total in
})
}
func summarizePlannedTaskProgressBody(body string, maxLines, maxChars int) string {
body = strings.ReplaceAll(body, "\r\n", "\n")
body = strings.TrimSpace(body)
if body == "" {
return "(无输出)"
}
lines := strings.Split(body, "\n")
out := make([]string, 0, len(lines))
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
out = append(out, line)
if maxLines > 0 && len(out) >= maxLines {
break
}
}
if len(out) == 0 {
return "(无输出)"
}
joined := strings.Join(out, "\n")
if maxChars > 0 && len(joined) > maxChars {
joined = truncate(joined, maxChars)
}
if len(lines) > len(out) && !strings.HasSuffix(joined, "...") {
joined += "\n..."
}
return joined
}
func (al *AgentLoop) enrichTaskContentWithMemoryAndEKG(ctx context.Context, task plannedTask) string {
base := strings.TrimSpace(task.Content)
if base == "" {

View File

@@ -0,0 +1,33 @@
package agent
import "testing"
func TestSplitPlannedSegmentsDoesNotSplitPlainNewlines(t *testing.T) {
t.Parallel()
content := "编写ai漫画创作平台demo\n让产品出方案方案出完让前端后端开始编写写完后交个测试过一下"
got := splitPlannedSegments(content)
if len(got) != 1 {
t.Fatalf("expected 1 segment, got %d: %#v", len(got), got)
}
}
func TestSplitPlannedSegmentsStillSplitsBullets(t *testing.T) {
t.Parallel()
content := "1. 先实现前端\n2. 再补测试"
got := splitPlannedSegments(content)
if len(got) != 2 {
t.Fatalf("expected 2 segments, got %d: %#v", len(got), got)
}
}
func TestSplitPlannedSegmentsStillSplitsSemicolons(t *testing.T) {
t.Parallel()
content := "先实现前端;再补测试"
got := splitPlannedSegments(content)
if len(got) != 2 {
t.Fatalf("expected 2 segments, got %d: %#v", len(got), got)
}
}

View File

@@ -0,0 +1,23 @@
package agent
import (
"strings"
"testing"
)
func TestSummarizePlannedTaskProgressBodyPreservesUsefulLines(t *testing.T) {
t.Parallel()
body := "subagent 已写入 config.json。\npath: /root/.clawgo/config.json\nagent_id: tester\nrole: testing\ndisplay_name: Test Agent\ntool_allowlist: [filesystem shell]\nrouting_keywords: [test qa]\nsystem_prompt_file: agents/tester/AGENT.md"
out := summarizePlannedTaskProgressBody(body, 6, 320)
if !strings.Contains(out, "subagent 已写入 config.json。") {
t.Fatalf("expected title line, got:\n%s", out)
}
if !strings.Contains(out, "agent_id: tester") {
t.Fatalf("expected agent id line, got:\n%s", out)
}
if strings.Contains(out, "subagent 已写入 config.json。 path:") {
t.Fatalf("expected multi-line formatting, got:\n%s", out)
}
}