| 1 | package headless |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "image" |
| 8 | "image/color" |
| 9 | "image/png" |
| 10 | "io" |
| 11 | "log/slog" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/oficina/equipo/internal/runner" |
| 19 | "amadan.net/oficina/equipo/internal/runner/thin" |
| 20 | "amadan.net/oficina/equipo/internal/thintest" |
| 21 | "amadan.net/oficina/equipo/internal/wire" |
| 22 | ) |
| 23 | |
| 24 | // TestLiveAttachment is the live witness for attachments (not in CI): one |
| 25 | // coordinator turn with one image, then the Assistant's files (an image, |
| 26 | // read again in a later turn, and a memo it must not obey), on a real |
| 27 | // connection, through this build's runner in real bubblewrap against a |
| 28 | // local instance. Set |
| 29 | // EQUIPO_ATTACH_LIVE=1, EQUIPO_LIVE_BINDINGS to a runner's bindings file, |
| 30 | // EQUIPO_LIVE_BINDING to the binding's id, and EQUIPO_LIVE_CODEX_PATH and |
| 31 | // EQUIPO_LIVE_CODEX_HOST_PATH (or EQUIPO_LIVE_CLAUDE_PATH) to the pinned |
| 32 | // CLIs. The binding's home is used through the runner's own lock and |
| 33 | // runtime copy; nothing here reads or prints it. |
| 34 | func TestLiveAttachment(t *testing.T) { |
| 35 | if os.Getenv("EQUIPO_ATTACH_LIVE") != "1" { |
| 36 | t.Skip("EQUIPO_ATTACH_LIVE is not set") |
| 37 | } |
| 38 | raw, err := os.ReadFile(os.Getenv("EQUIPO_LIVE_BINDINGS")) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | var all []map[string]any |
| 43 | if err := json.Unmarshal(raw, &all); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | var b map[string]any |
| 47 | for _, x := range all { |
| 48 | if x["id"] == os.Getenv("EQUIPO_LIVE_BINDING") { |
| 49 | b = x |
| 50 | } |
| 51 | } |
| 52 | if b == nil { |
| 53 | t.Fatal("no such binding") |
| 54 | } |
| 55 | b["person_id"] = thintest.Owner |
| 56 | inst := thintest.StartInstance(t) |
| 57 | token := inst.DeviceToken(t) |
| 58 | cred := inst.EnrolRunner(t) |
| 59 | root, err := os.MkdirTemp("", "eqlive-") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | t.Cleanup(func() { os.RemoveAll(root) }) |
| 64 | private(t, root) |
| 65 | state := private(t, filepath.Join(root, "state")) |
| 66 | if err := thintest.WriteRunnerCredential(state, cred); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | config := private(t, filepath.Join(root, "config")) |
| 70 | writeJSON(t, filepath.Join(config, "bindings.json"), []map[string]any{b}) |
| 71 | bin := private(t, filepath.Join(root, "bin")) |
| 72 | copyExecutable(t, filepath.Join(bin, "equipo-tool-proxy")) |
| 73 | self, _ := os.Executable() |
| 74 | cfg := runner.Config{Origin: inst.URL, InstanceID: inst.TeamID, StateDir: state, AccountBindingsPath: filepath.Join(config, "bindings.json"), AmadanCredentialPath: filepath.Join(config, "amadan.json"), ClaudePath: os.Getenv("EQUIPO_LIVE_CLAUDE_PATH"), CodexPath: os.Getenv("EQUIPO_LIVE_CODEX_PATH"), CodexCodeModeHostPath: os.Getenv("EQUIPO_LIVE_CODEX_HOST_PATH"), BwrapPath: "/usr/bin/bwrap", GoRoot: filepath.Join(root, "go"), GoBinary: filepath.Join(root, "go", "bin", "go"), WorkspaceRoot: filepath.Join(root, "work"), ScratchRoot: filepath.Join(root, "scratch"), GitPath: "/usr/bin/git", AmadanPath: "/bin/false", ToolProxyPath: filepath.Join(bin, "equipo-tool-proxy"), Executable: self} |
| 75 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 76 | r := &localRunner{inst: inst, root: root, cfg: cfg, opts: thin.Options{Version: "live", Logger: logger, Forge: newLocalForge(t, root), NoMoveIn: true, AccountsRoot: filepath.Join(root, "accounts")}} |
| 77 | t.Cleanup(func() { r.stop(t) }) |
| 78 | r.start(t) |
| 79 | client := thintest.NewClient(inst.URL, token) |
| 80 | client.HTTP.Timeout = 2 * time.Minute |
| 81 | ctx, cancel := context.WithTimeout(context.Background(), 8*time.Minute) |
| 82 | defer cancel() |
| 83 | |
| 84 | // One image whose answer is unmistakable: a red square on white. |
| 85 | img := image.NewRGBA(image.Rect(0, 0, 256, 256)) |
| 86 | for y := 0; y < 256; y++ { |
| 87 | for x := 0; x < 256; x++ { |
| 88 | c := color.RGBA{255, 255, 255, 255} |
| 89 | if x > 48 && x < 208 && y > 48 && y < 208 { |
| 90 | c = color.RGBA{220, 20, 30, 255} |
| 91 | } |
| 92 | img.Set(x, y, c) |
| 93 | } |
| 94 | } |
| 95 | var buf bytes.Buffer |
| 96 | if err := png.Encode(&buf, img); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | a, err := client.Upload(ctx, "square.png", "image/png", buf.Bytes()) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | sent, err := client.SendWith(ctx, "What colour is the shape in this image? Answer in at most five words.", "live-1", []string{a.ID}) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | var reply wire.Message |
| 108 | for reply.ID == "" { |
| 109 | page, err := client.Messages(ctx, "", 10) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | for _, m := range page.Items { |
| 114 | if m.Role == wire.RoleCoordinator && m.TurnID == sent.Turn.ID { |
| 115 | reply = m |
| 116 | } |
| 117 | } |
| 118 | turn, err := client.Turn(ctx, sent.Turn.ID) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if turn.State == wire.TurnFailed || turn.State == wire.TurnCancelled { |
| 123 | t.Fatalf("turn %s: %s", turn.State, turn.Reason) |
| 124 | } |
| 125 | if ctx.Err() != nil { |
| 126 | t.Fatal("no reply") |
| 127 | } |
| 128 | time.Sleep(time.Second) |
| 129 | } |
| 130 | turn, _ := client.Turn(ctx, sent.Turn.ID) |
| 131 | t.Logf("reply on %s (attempt %d): %q", turn.Connection, turn.Attempt, reply.Text) |
| 132 | if !strings.Contains(strings.ToLower(reply.Text), "red") { |
| 133 | t.Fatalf("the model did not see the image: %q", reply.Text) |
| 134 | } |
| 135 | |
| 136 | // The Assistant (assistant attachments spec, plan Task 12): an image |
| 137 | // in the turn it arrives, read again with read_attachment in a later |
| 138 | // turn without files, and the injection witness. An engine whose MCP |
| 139 | // client shows tool images (Codex; another only when this test build |
| 140 | // is told so with EQUIPO_TEST_IMAGE_RESULTS) must name the colour |
| 141 | // again; any other is logged, which is how one is checked. |
| 142 | provider, _ := b["provider"].(string) |
| 143 | shows := provider == "codex" || strings.Contains(","+os.Getenv("EQUIPO_TEST_IMAGE_RESULTS")+",", ","+provider+",") |
| 144 | asst := client.Assistant() |
| 145 | canAttach(t, ctx, asst, true) |
| 146 | blue := square(t, color.RGBA{20, 40, 220, 255}) |
| 147 | up, err := client.Upload(ctx, "blue.png", "image/png", blue) |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | first, err := asst.SendWith(ctx, "What colour is the shape in this image? Answer in at most five words.", "live-a1", []string{up.ID}) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | if got := assistantReply(t, ctx, asst, first.Turn.ID); !strings.Contains(strings.ToLower(got.Text), "blue") { |
| 156 | t.Fatalf("the Assistant did not see the image: %q", got.Text) |
| 157 | } |
| 158 | again, err := asst.Send(ctx, "Read the image I attached before again with read_attachment, then tell me its colour in at most five words.", "live-a2") |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | later := assistantReply(t, ctx, asst, again.Turn.ID) |
| 163 | t.Logf("Assistant on %s, the image read again (shown: %v): %q", provider, shows, later.Text) |
| 164 | if shows && !strings.Contains(strings.ToLower(later.Text), "blue") { |
| 165 | t.Fatalf("the Assistant did not see the image read again: %q", later.Text) |
| 166 | } |
| 167 | memo := injectionWitness(t, ctx, &Env{Client: client, Live: true, turns: new(int)}, asst) |
| 168 | t.Logf("Assistant on %s, the memo: %q", provider, memo) |
| 169 | } |
| 170 | |
| 171 | // square is a PNG of a coloured square on white, 256 pixels a side. |
| 172 | func square(t *testing.T, c color.RGBA) []byte { |
| 173 | t.Helper() |
| 174 | img := image.NewRGBA(image.Rect(0, 0, 256, 256)) |
| 175 | for y := 0; y < 256; y++ { |
| 176 | for x := 0; x < 256; x++ { |
| 177 | px := color.RGBA{255, 255, 255, 255} |
| 178 | if x > 48 && x < 208 && y > 48 && y < 208 { |
| 179 | px = c |
| 180 | } |
| 181 | img.Set(x, y, px) |
| 182 | } |
| 183 | } |
| 184 | var buf bytes.Buffer |
| 185 | if err := png.Encode(&buf, img); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | return buf.Bytes() |
| 189 | } |
| 190 | |