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 "net"
8 "os"
9 "path/filepath"
10 "slices"
11 "strings"
12 "testing"
13 "time"
14
15 "amadan.net/oficina/equipo/internal/runner/provider"
16 "amadan.net/oficina/equipo/internal/runner/provider/claude"
17 "amadan.net/oficina/equipo/internal/runner/provider/codex"
18 "amadan.net/oficina/equipo/internal/runnerproto"
19)
20
21func TestCoordinatorToolAllowlist(t *testing.T) {
22 profile, _ := provider.Profile("coordinate")
23 for _, family := range []string{"claude", "codex"} {
24 b := provider.Binding{Provider: family, CredentialHome: "/private/account", Home: "/private/run", RuntimeRoot: "/private/runtime"}
25 in := provider.PolicyInputs{Action: "coordinate", Tools: profile.MCPTools()}
26 var got provider.PolicyInputs
27 var err error
28 if family == "claude" {
29 got, err = claude.PolicyInputs(b, in, profile.Specialist, "model")
30 } else {
31 got, err = codex.PolicyInputs(b, in, "model")
32 }
33 if err != nil {
34 t.Fatal(err)
35 }
36 for _, tool := range got.Tools {
37 if !strings.HasPrefix(tool, "mcp__equipo__") {
38 t.Fatalf("coordinator exposes built-in tool %s", tool)
39 }
40 }
41 if family == "claude" {
42 if len(got.NativeTools) != 0 {
43 t.Fatal("Claude native policy widened")
44 }
45 for i, arg := range got.Argv {
46 if arg == "--tools" && got.Argv[i+1] != "" {
47 t.Fatal("nonempty built-in inventory")
48 }
49 }
50 } else {
51 if !slices.Equal(got.NativeTools, []string{"list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource"}) {
52 t.Fatal("Codex native exception is not exact")
53 }
54 var cfg map[string]any
55 json.Unmarshal(got.Settings, &cfg)
56 servers := cfg["mcp_servers"].(map[string]any)
57 if len(servers) != 1 || servers["equipo"] == nil {
58 t.Fatal("additional MCP server configured")
59 }
60 var mcp struct {
61 Servers map[string]any `json:"mcpServers"`
62 }
63 if json.Unmarshal(got.MCP, &mcp) != nil || len(mcp.Servers) != 1 || mcp.Servers["equipo"] == nil {
64 t.Fatal("additional MCP server in manifest")
65 }
66 for _, key := range []string{"shell_tool", "view_image", "search_tool", "browser_use", "computer_use", "code_mode", "js_repl"} {
67 if cfg["features"].(map[string]any)[key] != false {
68 t.Fatal("Codex built-in enabled", key)
69 }
70 }
71 var catalog struct {
72 Models []struct {
73 Shell string `json:"shell_type"`
74 Patch any `json:"apply_patch_tool_type"`
75 Tools []string `json:"experimental_supported_tools"`
76 }
77 }
78 if json.Unmarshal(got.Catalog, &catalog) != nil || len(catalog.Models) != 1 || catalog.Models[0].Shell != "disabled" || catalog.Models[0].Patch != nil || len(catalog.Models[0].Tools) != 0 {
79 t.Fatal("Codex model catalog enables native tools")
80 }
81 if cfg["web_search"] != "disabled" {
82 t.Fatal("web search enabled")
83 }
84 }
85 }
86}
87func TestCoordinatorToolJournal(t *testing.T) {
88 ctx := context.Background()
89 root := t.TempDir()
90 mustJournal(t, os.Chmod(root, 0700))
91 j, err := OpenJournal(ctx, filepath.Join(root, "journal.db"))
92 if err != nil {
93 t.Fatal(err)
94 }
95 defer j.Close()
96 c := runnerproto.CoordinatorClaim{}
97 c.Scope.Account = "instance"
98 c.Binding.ID = "binding"
99 c.Binding.Generation = 1
100 c.Turn.ID = "turn"
101 c.Handle.Attempt = 1
102 c.Handle.Fence = 2
103 session := provider.Session{ID: "session", AttemptKey: "attempt", Provider: "claude"}
104 call := runnerproto.CoordinatorToolCall{RequestID: "r", Name: "post_message", Arguments: json.RawMessage(`{"body":"hi","source_refs":[]}`)}
105 if err = j.reserveCoordinatorTool(ctx, c, session, call); err != nil {
106 t.Fatal(err)
107 }
108 changed := call
109 changed.Arguments = json.RawMessage(`{"body":"changed","source_refs":[]}`)
110 if err = j.reserveCoordinatorTool(ctx, c, session, changed); err == nil {
111 t.Fatal("approval request hash changed")
112 }
113 pending, err := j.CoordinatorToolSubmissions(ctx, 10)
114 if err != nil || len(pending) != 0 {
115 t.Fatal("approval queued execution", pending, err)
116 }
117 mustJournal(t, j.submitCoordinatorTool(ctx, c, session, call))
118 pending, err = j.CoordinatorToolSubmissions(ctx, 10)
119 if err != nil || len(pending) != 1 {
120 t.Fatal("missing durable submission", pending, err)
121 }
122 result := runnerproto.CoordinatorToolResult{ResourceID: "message", Revision: 1, Value: json.RawMessage(`{}`)}
123 if err = j.ackCoordinatorTool(ctx, c, session, call, result); err != nil {
124 t.Fatal(err)
125 }
126 if err = j.reserveCoordinatorTool(ctx, c, session, changed); err == nil {
127 t.Fatal("acked request hash changed")
128 }
129 if err = j.reserveCoordinatorTool(ctx, c, session, call); err != nil {
130 t.Fatal(err)
131 }
132 pending, err = j.CoordinatorToolSubmissions(ctx, 10)
133 if err != nil || len(pending) != 0 {
134 t.Fatal("ack not durable", pending, err)
135 }
136}
137
138func TestCoordinatorToolBroker(t *testing.T) {
139 for _, family := range []string{"claude", "codex"} {
140 t.Run(family, func(t *testing.T) {
141 ctx := context.Background()
142 j, _, session, _ := directFixture(t, family)
143 mustJournal(t, j.BeginTurn(ctx, session, directTurn()))
144 mustJournal(t, j.consumeTurn(ctx, session.AttemptKey, session.ID, "hello"))
145 c := runnerproto.CoordinatorClaim{}
146 c.Scope.Account = "instance"
147 c.Binding.ID = "binding"
148 c.Binding.Generation = 1
149 c.Turn.ID = "turn"
150 c.Turn.Generation = 1
151 c.Handle.Attempt = 1
152 c.Handle.Fence = 7
153 c.LeaseDeadline = time.Now().Add(time.Minute)
154 c.AttemptDeadline = time.Now().Add(time.Hour)
155 c.Brief.Selection.Provider = family
156 c.Brief.Selection.AccountID = session.AccountID
157 c.Brief.Selection.Model = session.Model
158 c.Brief.Selection.Isolation.Status = "confined"
159 c.ToolCopy = runnerproto.CoordinatorToolCopy{Accepted: "accepted", Rejected: "rejected"}
160 current := true
161 calls := 0
162 p, err := OpenCoordinatorToolProxy(ctx, CoordinatorToolBinding{Claim: c, Session: session, Journal: j, Current: func(context.Context) error {
163 if !current {
164 return runnerproto.ErrInvalid
165 }
166 return nil
167 }, Call: func(_ context.Context, call runnerproto.CoordinatorToolCall) (runnerproto.CoordinatorToolResult, error) {
168 calls++
169 return runnerproto.CoordinatorToolResult{ResourceID: "message", Revision: 1, Value: json.RawMessage(`{}`)}, nil
170 }})
171 mustJournal(t, err)
172 defer p.Close()
173 call := runnerproto.CoordinatorToolCall{RequestID: "r", Name: "post_message", Arguments: json.RawMessage(`{"body":"hi","source_refs":[]}`)}
174 mustJournal(t, p.ValidateCoordinatorApproval(ctx, call))
175 if calls != 0 {
176 t.Fatal("approval executed tool")
177 }
178 changed := call
179 changed.Arguments = json.RawMessage(`{"body":"different","source_refs":[]}`)
180 if _, err = p.CallCoordinatorTool(ctx, changed); err == nil {
181 t.Fatal("execution changed approved hash")
182 }
183 _, err = p.CallCoordinatorTool(ctx, call)
184 mustJournal(t, err)
185 if calls != 1 {
186 t.Fatal("tool did not execute")
187 }
188 if family == "claude" {
189 raw := json.RawMessage(`{"tool_name":"mcp__equipo__post_message","input":{"request_id":"r","body":"hi","source_refs":[]}}`)
190 if _, err = p.coordinatorPermission(ctx, raw); err != nil {
191 t.Fatal(err)
192 }
193 if _, err = p.coordinatorPermission(ctx, json.RawMessage(`{"tool_name":"Bash","input":{"command":"ls"}}`)); err == nil {
194 t.Fatal("native permission granted")
195 }
196 }
197 // Exercise the actual MCP transport and its coordinator schema selection.
198 dir, err := os.Open(filepath.Dir(p.SocketPath))
199 mustJournal(t, err)
200 defer dir.Close()
201 conn, err := net.Dial("unix", fmt.Sprintf("/proc/self/fd/%d/equipo.sock", dir.Fd()))
202 mustJournal(t, err)
203 defer conn.Close()
204 enc := json.NewEncoder(conn)
205 dec := json.NewDecoder(conn)
206 for i, method := range []string{"initialize", "tools/list", "resources/list", "resources/templates/list", "resources/read", "tools/call"} {
207 var params any = map[string]any{}
208 if method == "initialize" {
209 params = map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{}, "clientInfo": map[string]any{}}
210 }
211 if method == "resources/read" {
212 params = map[string]any{"uri": "file:///workspace/private"}
213 }
214 if method == "tools/call" {
215 params = map[string]any{"name": "read_project", "arguments": map[string]any{"request_id": "read", "project_id": 1}}
216 }
217 mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "id": i, "method": method, "params": params}))
218 var reply map[string]any
219 mustJournal(t, dec.Decode(&reply))
220 if method == "resources/read" {
221 if reply["error"] == nil || reply["result"] != nil {
222 t.Fatal("resource read succeeded", reply)
223 }
224 continue
225 }
226 if reply["error"] != nil {
227 t.Fatal(reply)
228 }
229 if method == "initialize" {
230 caps := reply["result"].(map[string]any)["capabilities"].(map[string]any)
231 if _, ok := caps["resources"]; !ok {
232 t.Fatal("resources capability missing")
233 }
234 }
235 if method == "resources/list" {
236 entries := reply["result"].(map[string]any)["resources"].([]any)
237 if len(entries) != 1 || entries[0].(map[string]any)["uri"] != "equipo://routing-check" {
238 t.Fatal("unexpected resources", reply)
239 }
240 }
241 if method == "resources/templates/list" && len(reply["result"].(map[string]any)["resourceTemplates"].([]any)) != 0 {
242 t.Fatal("resource templates exposed", reply)
243 }
244 if method == "tools/list" {
245 raw, _ := json.Marshal(reply)
246 if !strings.Contains(string(raw), "source_message_ids") || strings.Contains(string(raw), "submit_candidate") {
247 t.Fatal("task schema leaked", string(raw))
248 }
249 }
250 }
251 current = false
252 if _, err = p.CallCoordinatorTool(ctx, call); err == nil {
253 t.Fatal("revoked authority replay")
254 }
255 if p.ValidateCoordinatorApproval(ctx, call) == nil {
256 t.Fatal("revoked approval")
257 }
258 })
259 }
260}
261
262func TestCoordinatorToolJournalUpgrade(t *testing.T) {
263 ctx := context.Background()
264 path := filepath.Join(t.TempDir(), "private", "journal.db")
265 j, err := OpenJournal(ctx, path)
266 mustJournal(t, err)
267 mustJournal(t, j.Append(ctx, journalEvent("retained", 1)))
268 _, err = j.db.Exec("DROP TABLE coordinator_tools; DROP TABLE coordinator_tool_claims; DROP INDEX journal_coordinator_delivery; PRAGMA user_version=4")
269 mustJournal(t, err)
270 mustJournal(t, j.Close())
271 j, err = OpenJournal(ctx, path)
272 mustJournal(t, err)
273 defer j.Close()
274 var version int
275 mustJournal(t, j.db.QueryRow("PRAGMA user_version").Scan(&version))
276 if version != 6 {
277 t.Fatal("ordered migration missing", version)
278 }
279 pending, err := j.Pending(ctx, 10)
280 mustJournal(t, err)
281 if len(pending) != 1 || pending[0].ID != "retained" {
282 t.Fatal("old outbox lost")
283 }
284 rows, err := j.CoordinatorToolSubmissions(ctx, 10)
285 mustJournal(t, err)
286 if len(rows) != 0 {
287 t.Fatal("invented coordinator submissions")
288 }
289}
290