oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo/team"
14 "amadan.net/oficina/equipo/internal/runnerproto"
15)
16
17func toolRequestFixture(s *AttemptSocket, b ToolBinding) toolRequest {
18 return toolRequest{Capability: s.capability.Secret, InstanceID: b.InstanceID, TaskID: b.TaskID, Attempt: b.Attempt, Fence: b.Fence, RequestID: "round1", Name: "ask_person", Arguments: json.RawMessage(`{"question":"Which?"}`)}
19}
20
21func TestToolSocketRenewedLease(t *testing.T) {
22 base, b := toolFixture(t)
23 b.LeaseDeadline = time.Now().Add(300 * time.Millisecond)
24 root := filepath.Dir(filepath.Dir(filepath.Dir(base.SocketPath)))
25 s, err := NewAttemptSocket(context.Background(), root, b)
26 mustJournal(t, err)
27 defer s.Cleanup(context.Background(), func(context.Context) error { return nil })
28 renewal, ok := any(s).(interface {
29 RenewLease(context.Context, time.Time) error
30 })
31 if !ok {
32 t.Fatal("socket has no trusted lease renewal")
33 }
34 deadline := time.Now().Add(time.Second)
35 mustJournal(t, renewal.RenewLease(context.Background(), deadline))
36 mustJournal(t, renewal.RenewLease(context.Background(), b.LeaseDeadline))
37 time.Sleep(time.Until(b.LeaseDeadline) + 50*time.Millisecond)
38 input := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{}}}` + "\n" +
39 `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"ask_person","arguments":{"request_id":"renewed","question":"Which?"}}}` + "\n"
40 var out strings.Builder
41 mustJournal(t, ServeTools(context.Background(), strings.NewReader(input), &out, s.SocketPath, s.CapabilityPath))
42 if strings.Contains(out.String(), `"error"`) {
43 t.Fatal("renewed lease tool failed", out.String())
44 }
45 rows, err := b.Journal.ToolSubmissions(context.Background(), 10)
46 mustJournal(t, err)
47 if len(rows) != 1 {
48 t.Fatal("renewed lease lost submission")
49 }
50 select {
51 case <-s.done:
52 t.Fatal("renewal did not extend timer")
53 default:
54 }
55 select {
56 case <-s.done:
57 case <-time.After(time.Until(deadline) + time.Second):
58 t.Fatal("lost lease did not invalidate idle socket")
59 }
60 if renewal.RenewLease(context.Background(), time.Now().Add(time.Minute)) == nil {
61 t.Fatal("lost lease revived")
62 }
63 if callToolSocket(context.Background(), s.SocketPath, toolRequestFixture(s, b)) == nil {
64 t.Fatal("lost lease accepted tool")
65 }
66}
67
68func TestToolSocketUnauthenticatedCannotInvalidate(t *testing.T) {
69 s, b := toolFixture(t)
70 calls := 0
71 s.mu.Lock()
72 s.binding.Current = func(context.Context) error { calls++; return runnerproto.ErrInvalid }
73 s.mu.Unlock()
74 q := toolRequestFixture(s, b)
75 q.Capability = "wrong"
76 if callToolSocket(context.Background(), s.SocketPath, q) == nil {
77 t.Fatal("unauthenticated call accepted")
78 }
79 s.mu.Lock()
80 invalid := s.invalid
81 checked := calls
82 s.binding.Current = func(context.Context) error { return nil }
83 s.mu.Unlock()
84 if invalid || checked != 0 {
85 t.Fatal("unauthenticated frame checked authority or closed socket")
86 }
87 q.Capability = s.capability.Secret
88 mustJournal(t, callToolSocket(context.Background(), s.SocketPath, q))
89}
90
91func TestAgentToolJournalCapacity(t *testing.T) {
92 s, b := toolFixture(t)
93 q := toolRequestFixture(s, b)
94 b.Journal.maxBytes = 1
95 if s.submit(context.Background(), q) == nil {
96 t.Fatal("full journal accepted tool")
97 }
98 mustJournal(t, b.Journal.AdmissionAllowed(context.Background()))
99 b.Journal.maxBytes = 128 << 20
100 mustJournal(t, s.submit(context.Background(), q))
101}
102
103func TestAgentToolAttemptCapacity(t *testing.T) {
104 s, b := toolFixture(t)
105 q := toolRequestFixture(s, b)
106 appendRaw := func() error {
107 return b.Journal.appendTool(context.Background(), ToolSubmission{InstanceID: b.InstanceID, TaskID: q.TaskID, Attempt: b.Attempt, Fence: b.Fence, RequestID: q.RequestID, Name: q.Name, Arguments: q.Arguments, Agent: b.Agent, Isolation: b.Isolation}, runnerproto.EventHash(q.Arguments))
108 }
109 for i := 0; i < 128; i++ {
110 q.RequestID = fmt.Sprint(i)
111 mustJournal(t, appendRaw())
112 }
113 q.RequestID = "overflow"
114 if appendRaw() == nil {
115 t.Fatal("attempt exhausted shared journal without a cap")
116 }
117 mustJournal(t, b.Journal.AdmissionAllowed(context.Background()))
118 b.TaskID = "other-task"
119 other, err := NewAttemptSocket(context.Background(), filepath.Dir(filepath.Dir(filepath.Dir(s.SocketPath))), b)
120 mustJournal(t, err)
121 defer other.Cleanup(context.Background(), func(context.Context) error { return nil })
122 mustJournal(t, other.submit(context.Background(), toolRequestFixture(other, b)))
123 rows, err := b.Journal.ToolSubmissions(context.Background(), 1024)
124 mustJournal(t, err)
125 mustJournal(t, b.Journal.AckTool(context.Background(), rows[0]))
126 mustJournal(t, appendRaw())
127}
128
129func TestAgentToolReproductionFields(t *testing.T) {
130 good := runnerproto.Reproduction{ExpectedSymptom: "wrong value", Files: []runnerproto.CheckFile{{Path: "sub/bug_test.go", Bytes: []byte("test")}}, TestNames: []team.TestIdentity{{Package: "fixture", Name: "TestBug"}}}
131 for _, field := range []string{"valid", "package", "name", "nested_git"} {
132 t.Run(field, func(t *testing.T) {
133 v := good
134 v.Files = append([]runnerproto.CheckFile(nil), good.Files...)
135 v.TestNames = append([]team.TestIdentity(nil), good.TestNames...)
136 switch field {
137 case "package":
138 v.TestNames[0].Package = ""
139 case "name":
140 v.TestNames[0].Name = ""
141 case "nested_git":
142 v.Files[0].Path = "sub/.git/config"
143 }
144 raw, err := json.Marshal(v)
145 mustJournal(t, err)
146 err = validateToolArguments("submit_reproduction", raw)
147 if field == "valid" {
148 mustJournal(t, err)
149 } else if err == nil {
150 t.Fatal("invalid reproduction accepted", field)
151 }
152 })
153 }
154}
155
156func TestToolSocketCapabilityEncodingFailure(t *testing.T) {
157 s, b := toolFixture(t)
158 root := filepath.Dir(filepath.Dir(filepath.Dir(s.SocketPath)))
159 dir := filepath.Join(root, "encoding-failure")
160 mustJournal(t, os.Mkdir(dir, 0700))
161 b.LeaseDeadline = time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)
162 bad, err := ServeAttemptSocket(context.Background(), filepath.Join(dir, "tool.sock"), b)
163 if err == nil {
164 bad.Cleanup(context.Background(), func(context.Context) error { return nil })
165 t.Fatal("unencodable capability published")
166 }
167 entries, err := os.ReadDir(dir)
168 mustJournal(t, err)
169 if len(entries) != 0 {
170 t.Fatal("encoding failure left authority files")
171 }
172}
173
174func TestAgentToolAttemptByteCapacity(t *testing.T) {
175 s, b := toolFixture(t)
176 b.Journal.maxBytes = 8192
177 q := toolRequestFixture(s, b)
178 q.Arguments = json.RawMessage(`{"question":"` + strings.Repeat("x", 2048) + `"}`)
179 if s.submit(context.Background(), q) == nil {
180 t.Fatal("attempt exceeded its byte share")
181 }
182 mustJournal(t, b.Journal.AdmissionAllowed(context.Background()))
183 q.Arguments = json.RawMessage(`{"question":"Which?"}`)
184 mustJournal(t, s.submit(context.Background(), q))
185}
186