oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package pi
2
3import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "testing"
8)
9
10// Pi gates every call in a message before running them together. A file
11// tool and a shell command in one message must never both run: the shell
12// could swap a checked path for a link before the file tool opens it.
13func TestPiBatchSeparatesShellAndFileTools(t *testing.T) {
14 h := newHarness(t, realInstall(t))
15 work := t.TempDir()
16 for _, name := range []string{"a.txt", "b.txt"} {
17 if err := os.WriteFile(filepath.Join(work, name), []byte("plain\n"), 0600); err != nil {
18 t.Fatal(err)
19 }
20 }
21 r := h.open(work, "implement", []string{"submit_candidate"}, "")
22 batch := func(calls ...map[string]any) []ToolResult {
23 t.Helper()
24 raw, _ := json.Marshal(calls)
25 got := results(r.prompt(t, "BATCH "+string(raw), "allow"))
26 if len(got) != len(calls) {
27 t.Fatalf("batch returned %d results, want %d", len(got), len(calls))
28 }
29 return got
30 }
31 bash := map[string]any{"name": "bash", "arguments": map[string]string{"command": "echo shell-ran"}}
32 read := func(name string) map[string]any {
33 return map[string]any{"name": "read", "arguments": map[string]string{"path": "/workspace/" + name}}
34 }
35 // Results arrive in completion order; a refused call is an error result
36 // (the files exist, so a read that runs does not error).
37 refused := func(res ToolResult) bool { return res.IsError }
38 byTool := func(rs []ToolResult, tool string) ToolResult {
39 t.Helper()
40 for _, r := range rs {
41 if r.Tool == tool {
42 return r
43 }
44 }
45 t.Fatalf("no %s result in %+v", tool, rs)
46 return ToolResult{}
47 }
48 // Positive control: file tools alone, and a shell command alone, run.
49 for _, res := range batch(read("a.txt"), read("b.txt")) {
50 if refused(res) {
51 t.Fatal("two file reads in one message were refused")
52 }
53 }
54 if res := batch(bash); refused(res[0]) {
55 t.Fatal("a lone shell command was refused")
56 }
57 // Shell first: the read must not run beside it.
58 if res := batch(bash, read("a.txt")); refused(byTool(res, "bash")) || !refused(byTool(res, "read")) {
59 t.Fatalf("shell then read: %+v", res)
60 }
61 // File tool first: the shell command must not run beside it.
62 if res := batch(read("a.txt"), bash); refused(byTool(res, "read")) || !refused(byTool(res, "bash")) {
63 t.Fatalf("read then shell: %+v", res)
64 }
65 // Separate messages still work in turn.
66 if res := batch(read("a.txt")); refused(res[0]) {
67 t.Fatal("a read after a mixed batch was refused")
68 }
69}
70
71func TestPiShellInheritedDescriptors(t *testing.T) {
72 h := newHarness(t, realInstall(t))
73 r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "")
74 shellOK(t, h, r, `python3 - <<'PY'
75import os
76fds=[]
77for name in os.listdir('/proc/self/fd'):
78 try: os.fstat(int(name))
79 except OSError: continue
80 fds.append(int(name))
81assert sorted(fds)==[0,1,2],sorted(fds)
82print('stdio-only')
83PY`, "stdio-only")
84}
85