返回 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 TestDesktopE2EBlocksRepeatedSuccessfulBashFileWrite(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 reg.Add(desktopCountingTool{name: "bash", calls: &calls})
44 args := `{"command":"python -c \"with open('prompt.txt', 'w') as f: f.write('hello')\""}`
45 prov := agenttest.NewMock("scripted-desktop",
46 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c1", Name: "bash", Arguments: args}}},
47 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c2", Name: "bash", Arguments: args}}},
48 agenttest.Turn{ToolCalls: []provider.ToolCall{{ID: "c3", Name: "bash", Arguments: args}}},
49 agenttest.Turn{Text: "done"},
50 )
51 events := make(chan event.Event, 32)
52 sink := event.FuncSink(func(e event.Event) { events <- e })
53 ag := agent.New(prov, reg, agent.NewSession(""), agent.Options{}, sink)
54 ctrl := control.New(control.Options{Runner: ag, Executor: ag, Sink: sink})
55 app := NewApp()
56 app.setTestCtrl(ctrl, "scripted-desktop")
57
58 app.SubmitToTab("test", "update the prompt file")
59
60 var results []event.Event
61 deadline := time.After(5 * time.Second)
62 for {
63 select {
64 case e := <-events:
65 if e.Kind == event.ToolResult {
66 results = append(results, e)
67 }
68 if e.Kind == event.TurnDone {
69 if e.Err != nil {
70 t.Fatalf("turn failed: %v", e.Err)
71 }
72 if got := atomic.LoadInt32(&calls); got != 2 {
73 t.Fatalf("bash executed %d times, want 2 before the repeat guard blocks", got)
74 }
75 if len(results) != 3 {
76 t.Fatalf("tool results = %d, want 3", len(results))
77 }
78 last := results[len(results)-1].Tool.Output
79 if !strings.Contains(last, "[loop guard]") || !strings.Contains(last, "edit_file") {
80 t.Fatalf("third repeated write should nudge the model to change tools, got %q", last)
81 }
82 return
83 }
84 case <-deadline:
85 t.Fatal("timed out waiting for desktop turn to finish")
86 }
87 }
88 }
89
89 lines GO