Files
ARTEX/third_party/norma/harness/streaming_test.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 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
2026-10-20 21:10:41 -07:00

108 lines
3.5 KiB
Go

package harness
import (
"context"
"encoding/json"
"iter"
"sync/atomic"
"testing"
"time"
"github.com/Autumn-27/norma/llm"
"github.com/Autumn-27/norma/permission"
"github.com/Autumn-27/norma/tool"
)
// TestStreamingToolExecutionOverlapsStream proves FR-01.4: a read-only,
// concurrency-safe tool begins executing as soon as its tool_use block finishes
// streaming, while the rest of the assistant message is still being produced.
//
// The scripted model emits tool A, then tool B (whose start finalizes A and
// dispatches it), then BLOCKS — refusing to finish the message until A has
// actually run. If tools only ran after the stream completed, this would
// deadlock; the test's timeout would then fire.
func TestStreamingToolExecutionOverlapsStream(t *testing.T) {
ran := make(chan string, 4)
probe := tool.Build(tool.Spec{
Name: "probe",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{"id": map[string]any{"type": "string"}},
},
ReadOnly: func(json.RawMessage) bool { return true },
Concurrent: func(json.RawMessage) bool { return true },
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
return permission.Allowed()
},
Run: func(_ context.Context, in json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
var v struct {
ID string `json:"id"`
}
json.Unmarshal(in, &v)
ran <- v.ID
return tool.Text("ok"), nil
},
})
var turn int32
callModel := func(_ context.Context, _ llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
n := atomic.AddInt32(&turn, 1)
return func(yield func(llm.StreamEvent, error) bool) {
if n > 1 { // second turn: finish.
yield(llm.StreamEvent{Type: llm.SETextDelta, Text: "done"}, nil)
yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "end_turn"}, nil)
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
return
}
yield(llm.StreamEvent{Type: llm.SEToolUseStart, ToolID: "a", ToolName: "probe"}, nil)
yield(llm.StreamEvent{Type: llm.SEToolInputJSON, Text: `{"id":"a"}`}, nil)
// Starting B finalizes A, so A is dispatched and runs concurrently.
yield(llm.StreamEvent{Type: llm.SEToolUseStart, ToolID: "b", ToolName: "probe"}, nil)
yield(llm.StreamEvent{Type: llm.SEToolInputJSON, Text: `{"id":"b"}`}, nil)
// Refuse to finish the message until A has actually executed.
for v := range ran {
if v == "a" {
break
}
}
yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "tool_use"}, nil)
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
in := QueryInput{
Messages: []llm.Message{llm.UserText("go")},
Tools: tool.NewRegistry(probe),
PermissionMode: permission.ModeBypass,
}
done := make(chan *Terminal, 1)
go func() {
var term *Terminal
for ev, err := range Query(context.Background(), in, QueryDeps{CallModel: callModel}) {
if err != nil {
t.Errorf("event err: %v", err)
break
}
if ev.Kind == KindResult {
term = ev.Terminal
}
}
done <- term
}()
select {
case term := <-done:
if term == nil || term.Reason != ReasonCompleted {
t.Fatalf("terminal=%+v", term)
}
// Both probes produced tool_results, paired in order.
results := term.Messages[2].Content
if len(results) != 2 || results[0].ToolUseID != "a" || results[1].ToolUseID != "b" {
t.Fatalf("results=%+v", results)
}
case <-time.After(3 * time.Second):
t.Fatal("deadlock: tool did not execute during streaming (FR-01.4 regression)")
}
}