把 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
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package harness
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/norma/llm"
|
|
)
|
|
|
|
// noopCompactor implements Compactor but NOT ContextView: the loop must fall
|
|
// back to llm.MessagesForAPI, i.e. behaviour is unchanged.
|
|
type noopCompactor struct{}
|
|
|
|
func (noopCompactor) Pre(_ context.Context, msgs []llm.Message, _ int) []llm.Message { return msgs }
|
|
func (noopCompactor) Reactive(_ context.Context, msgs []llm.Message) ([]llm.Message, bool) {
|
|
return msgs, false
|
|
}
|
|
func (noopCompactor) IsOverflow(error) bool { return false }
|
|
|
|
// viewCompactor implements both Compactor and ContextView. It records what it
|
|
// was handed and returns a projection, so the test can assert that the loop
|
|
// uses the returned slice and never writes it back.
|
|
type viewCompactor struct {
|
|
noopCompactor
|
|
sawLen int
|
|
calls int
|
|
project func([]llm.Message) []llm.Message
|
|
}
|
|
|
|
func (v *viewCompactor) View(_ context.Context, msgs []llm.Message) []llm.Message {
|
|
v.calls++
|
|
v.sawLen = len(msgs)
|
|
return v.project(msgs)
|
|
}
|
|
|
|
func TestRequestMessagesFallsBackWithoutContextView(t *testing.T) {
|
|
msgs := []llm.Message{
|
|
llm.UserText("a"),
|
|
llm.BoundaryMessage(llm.BoundaryMeta{Trigger: "auto"}),
|
|
llm.UserText("b"),
|
|
}
|
|
l := &loop{ctx: context.Background(), messages: msgs, in: QueryInput{Compactor: noopCompactor{}}}
|
|
|
|
got := l.requestMessages()
|
|
want := llm.MessagesForAPI(msgs)
|
|
if len(got) != len(want) {
|
|
t.Fatalf("requestMessages() len = %d, want %d (MessagesForAPI fallback)", len(got), len(want))
|
|
}
|
|
for i := range got {
|
|
if got[i].Text() != want[i].Text() {
|
|
t.Fatalf("requestMessages()[%d] = %q, want %q", i, got[i].Text(), want[i].Text())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRequestMessagesUsesContextView(t *testing.T) {
|
|
msgs := []llm.Message{llm.UserText("a"), llm.UserText("b"), llm.UserText("c")}
|
|
v := &viewCompactor{project: func(in []llm.Message) []llm.Message {
|
|
return []llm.Message{llm.UserText("projected")}
|
|
}}
|
|
l := &loop{ctx: context.Background(), messages: msgs, in: QueryInput{Compactor: v}}
|
|
|
|
got := l.requestMessages()
|
|
if v.calls != 1 {
|
|
t.Fatalf("View called %d times, want 1", v.calls)
|
|
}
|
|
if v.sawLen != 3 {
|
|
t.Fatalf("View saw %d messages, want the full history (3)", v.sawLen)
|
|
}
|
|
if len(got) != 1 || got[0].Text() != "projected" {
|
|
t.Fatalf("requestMessages() = %+v, want the View projection", got)
|
|
}
|
|
// The projection must not be written back: l.messages is the durable history
|
|
// and message identity (content hashing) depends on it staying byte-stable.
|
|
if len(l.messages) != 3 {
|
|
t.Fatalf("l.messages len = %d after View, want 3 (View must not write back)", len(l.messages))
|
|
}
|
|
}
|
|
|
|
// A ContextView bypasses the boundary slice entirely — it owns the projection.
|
|
func TestContextViewBypassesBoundarySlicing(t *testing.T) {
|
|
msgs := []llm.Message{
|
|
llm.UserText("pre-boundary"),
|
|
llm.BoundaryMessage(llm.BoundaryMeta{Trigger: "auto"}),
|
|
llm.UserText("post-boundary"),
|
|
}
|
|
v := &viewCompactor{project: func(in []llm.Message) []llm.Message { return in }}
|
|
l := &loop{ctx: context.Background(), messages: msgs, in: QueryInput{Compactor: v}}
|
|
|
|
got := l.requestMessages()
|
|
if len(got) != 3 {
|
|
t.Fatalf("requestMessages() len = %d, want 3 — a ContextView receives the raw history, not the post-boundary slice", len(got))
|
|
}
|
|
if got[0].Text() != "pre-boundary" {
|
|
t.Fatalf("requestMessages()[0] = %q, want the pre-boundary message to be visible to View", got[0].Text())
|
|
}
|
|
}
|
|
|
|
func TestRequestMessagesWithNilCompactor(t *testing.T) {
|
|
msgs := []llm.Message{llm.UserText("a")}
|
|
l := &loop{ctx: context.Background(), messages: msgs, in: QueryInput{}}
|
|
if got := l.requestMessages(); len(got) != 1 || got[0].Text() != "a" {
|
|
t.Fatalf("requestMessages() with nil Compactor = %+v, want the MessagesForAPI fallback", got)
|
|
}
|
|
}
|