把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到 third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或 改写 tag 都不再影响本仓库构建。 - 已验证 GOPROXY=off 下可完整编译并打出发行包 - go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希) - 升级方法与授权注意事项见 third_party/README.md
97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
package harness
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/norma/llm"
|
|
"github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
func asst(text string) llm.Message {
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{llm.TextBlock(text)}}
|
|
}
|
|
|
|
func todoWriteAsst() llm.Message {
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{
|
|
{Type: llm.BlockToolUse, ID: "t1", Name: "TodoWrite", Input: []byte(`{}`)},
|
|
}}
|
|
}
|
|
|
|
func TestTodoReminderTurnCounts(t *testing.T) {
|
|
msgs := []llm.Message{
|
|
llm.UserText("go"),
|
|
todoWriteAsst(), // the write turn — not counted
|
|
{Role: llm.RoleUser, Content: []llm.ContentBlock{llm.ToolResultText("t1", "ok", false)}},
|
|
asst("thinking about it"), // +1 since write
|
|
llm.UserText("more"),
|
|
asst("still working"), // +1 since write
|
|
}
|
|
sinceWrite, sinceReminder := todoReminderTurnCounts(msgs)
|
|
if sinceWrite != 2 {
|
|
t.Fatalf("sinceWrite=%d, want 2", sinceWrite)
|
|
}
|
|
// No reminder in history → counts every assistant turn (3: two text + the write turn).
|
|
if sinceReminder != 3 {
|
|
t.Fatalf("sinceReminder=%d, want 3", sinceReminder)
|
|
}
|
|
|
|
// Insert a reminder; sinceReminder should count only assistant turns after it.
|
|
withReminder := append([]llm.Message{}, msgs...)
|
|
withReminder = append(withReminder, llm.TodoReminderMessage("1. [pending] x"), asst("after reminder"))
|
|
_, sinceReminder = todoReminderTurnCounts(withReminder)
|
|
if sinceReminder != 1 {
|
|
t.Fatalf("sinceReminder after a reminder=%d, want 1", sinceReminder)
|
|
}
|
|
}
|
|
|
|
func TestMaybeInjectTodoReminder(t *testing.T) {
|
|
store := tool.NewTodoStore()
|
|
|
|
// Disabled / empty list → never injects.
|
|
l := &loop{in: QueryInput{}}
|
|
l.maybeInjectTodoReminder()
|
|
if len(l.messages) != 0 {
|
|
t.Fatal("no injection when Todos is nil")
|
|
}
|
|
l = &loop{in: QueryInput{Todos: store}}
|
|
l.maybeInjectTodoReminder()
|
|
if len(l.messages) != 0 {
|
|
t.Fatal("no injection for an empty todo list")
|
|
}
|
|
|
|
// Seed a list; 10 assistant turns with no TodoWrite → injects.
|
|
store.Tool().Call(context.Background(), []byte(`{"todos":[{"content":"build","status":"in_progress"}]}`), &tool.ToolContext{})
|
|
l = &loop{in: QueryInput{Todos: store}}
|
|
for i := 0; i < 10; i++ {
|
|
l.messages = append(l.messages, asst("work"))
|
|
}
|
|
l.maybeInjectTodoReminder()
|
|
if len(l.messages) != 11 || !llm.IsTodoReminder(l.messages[10]) {
|
|
t.Fatalf("expected a todo reminder appended, got %d msgs", len(l.messages))
|
|
}
|
|
if !strings.Contains(l.messages[10].Text(), "build") {
|
|
t.Fatalf("reminder should carry the current list: %q", l.messages[10].Text())
|
|
}
|
|
|
|
// Immediately calling again → sinceReminder=0 < gap → no second injection.
|
|
l.maybeInjectTodoReminder()
|
|
if len(l.messages) != 11 {
|
|
t.Fatal("should not inject a second reminder within the throttle gap")
|
|
}
|
|
}
|
|
|
|
func TestMaybeInjectTodoReminderSkipsAfterRecentWrite(t *testing.T) {
|
|
store := tool.NewTodoStore()
|
|
store.Tool().Call(context.Background(), []byte(`{"todos":[{"content":"x","status":"pending"}]}`), &tool.ToolContext{})
|
|
|
|
l := &loop{in: QueryInput{Todos: store}}
|
|
// Recent TodoWrite (only 2 turns ago) → sinceWrite < 10 → no injection.
|
|
l.messages = []llm.Message{todoWriteAsst(), asst("a"), asst("b")}
|
|
l.maybeInjectTodoReminder()
|
|
if len(l.messages) != 3 {
|
|
t.Fatalf("should not inject shortly after a TodoWrite, got %d msgs", len(l.messages))
|
|
}
|
|
}
|