Why: token budget let the current tool batch run, compaction could split a tool pair or race an append, writes were not crash-atomic, and several MCP and session edges only existed as comments. Evidence: GOWORK=off go test -count=1 -race ./runtime/ ./runtime/llm/ ./runtime/session/ ./runtime/tools/ ./runtime/mcp/ ./adapters/native/ ./adapters/intentgate/ passed, including the TestR2* cases for budget, replacement, cancel, compaction, SSE, UTF-8, schema, MCP retry, session terminate, poison reload, and registry reload. Impact: over-budget tool calls are skipped and paired; Stop.Replacement is appended to the session; the intent gate can follow Registry.Reload. Refs: N5, N6, N7, N8, N9, N11, N12, N15, N16, N17, N18, N19, N21, I9 Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func TestR2WriteFileIsAtomic(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewRegistry(dir)
|
|
for _, tool := range []Tool{&writeFile{root: dir}} {
|
|
reg.Register(tool)
|
|
}
|
|
res := reg.Execute(context.Background(), "write_file", []byte(`{"path":"note.txt","content":"完整"}`))
|
|
if res.IsError {
|
|
t.Fatal(res.Content)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "note.txt.tmp")); !os.IsNotExist(err) {
|
|
t.Fatal("temp file left behind")
|
|
}
|
|
b, err := os.ReadFile(filepath.Join(dir, "note.txt"))
|
|
if err != nil || string(b) != "完整" {
|
|
t.Fatalf("file: %q %v", b, err)
|
|
}
|
|
}
|
|
|
|
func TestR2CutUTF8DoesNotSplitRunes(t *testing.T) {
|
|
s := strings.Repeat("界", 5)
|
|
cut := CutUTF8(s, 4)
|
|
if !utf8.ValidString(cut) {
|
|
t.Fatalf("invalid utf-8 %q", cut)
|
|
}
|
|
if cut != "界" {
|
|
t.Fatalf("cut %q", cut)
|
|
}
|
|
}
|
|
|
|
func TestR2SchemaRejectsUnknownKeys(t *testing.T) {
|
|
dir := t.TempDir()
|
|
reg := NewRegistry(dir)
|
|
reg.Register(&writeFile{root: dir})
|
|
res := reg.Execute(context.Background(), "write_file", []byte(`{"path":"a.txt","content":"x","extra":1}`))
|
|
if !res.IsError || !strings.Contains(res.Content, "unknown argument") {
|
|
t.Fatalf("result: %+v", res)
|
|
}
|
|
}
|