返回 DeepSeek-Reasonix
repeat_guard_e2e_test.go
根目录 / desktop / repeat_guard_e2e_test.go
1 package main
2
3 import (
4 "context"
5 "encoding/json"
6 "strings"
7 "sync/atomic"
8 "testing"
9 "time"
10
11 "reasonix/internal/agent"
12 agenttest "reasonix/internal/agent/testutil"
13 "reasonix/internal/control"
14 "reasonix/internal/event"
15 "reasonix/internal/provider"
16 "reasonix/internal/tool"
17 )
18
19 type desktopCountingTool struct {
20 name string
21 readOnly bool
22 calls *int32
23 }
24
25 func (t desktopCountingTool) Name() string { return t.name }
26 func (t desktopCountingTool) Description() string { return "test tool" }
27 func (t desktopCountingTool) Schema() json.RawMessage {
28 return json.RawMessage(`{"type":"object","properties":{"command":{"type":"string"}}}`)
29 }
30 func (t desktopCountingTool) ReadOnly() bool { return t.readOnly }
31 func (t desktopCountingTool) Execute(context.Context, json.RawMessage) (string, error) {
32 atomic.AddInt32(t.calls, 1)
33 return "ok", nil
34 }
35
36 func TestDesktopE2ERemindsWithoutBlockingRepeatedSuccessfulBashFileWrite(t *testing.T) {
37 if testing.Short() {
38 t.Skip("skipping desktop E2E repeat-guard test in short mode")
39 }
40
41 var calls int32
42 reg := tool.NewRegistry()
43 todoWrite, ok := tool.LookupBuiltin("todo_write")
44 if !ok {
45 t.Fatal("todo_write builtin is not registered")
46 }
47 reg.Add(todoWrite)
48 reg.Add(desktopCountingTool{name: "bash", calls: &calls})
49 args := `{"command":"printf 'hello' > prompt.txt"}`
50 prov := agenttest.NewMock("scripted-desktop",
51 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "todo-1", Name: "todo_write", Arguments: `{"todos":[{"content":"update the prompt file","status":"in_progress"}]}`}}},
52 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c1", Name: "bash", Arguments: args}}},
53 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c2", Name: "bash", Arguments: args}}},
54 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c3", Name: "bash", Arguments: args}}},
55 agenttest.Turn{Text: "done"},
56 )
57 events := make(chan event.Event, 32)
58 sink := event.FuncSink(func(e event.Event) { events <- e })
59 ag := agent.New(prov, reg, agent.NewSession(""), agent.Options{}, sink)
60 ctrl := control.New(control.Options{Runner: ag, Executor: ag, Sink: sink})
61 app := NewApp()
62 app.setTestCtrl(ctrl, "scripted-desktop")
63
64 app.SubmitToTab("test", "update the prompt file")
65
66 var results []event.Event
67 deadline := time.After(5 * time.Second)
68 for {
69 select {
70 case e := <-events:
71 if e.Kind == event.ToolResult && e.Tool.Name == "bash" {
72 results = append(results, e)
73 }
74 if e.Kind == event.TurnDone {
75 if e.Err != nil {
76 t.Fatalf("turn failed: %v", e.Err)
77 }
78 if got := atomic.LoadInt32(&calls); got != 3 {
79 t.Fatalf("bash executed %d times, want all 3 calls to run", got)
80 }
81 if len(results) != 3 {
82 t.Fatalf("tool results = %d, want 3", len(results))
83 }
84 last := results[len(results)-1].Tool.Output
85 if !strings.Contains(last, "[repeat reminder]") || !strings.Contains(last, "called 3 consecutive times") {
86 t.Fatalf("third repeated write should add a non-blocking reminder, got %q", last)
87 }
88 return
89 }
90 case <-deadline:
91 t.Fatal("timed out waiting for desktop turn to finish")
92 }
93 }
94 }
95
95 lines GO