package headless import ( "bytes" "context" "encoding/json" "image" "image/color" "image/png" "io" "log/slog" "os" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/thin" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // TestLiveAttachment is the live witness for attachments (not in CI): one // coordinator turn with one image, then the Assistant's files (an image, // read again in a later turn, and a memo it must not obey), on a real // connection, through this build's runner in real bubblewrap against a // local instance. Set // EQUIPO_ATTACH_LIVE=1, EQUIPO_LIVE_BINDINGS to a runner's bindings file, // EQUIPO_LIVE_BINDING to the binding's id, and EQUIPO_LIVE_CODEX_PATH and // EQUIPO_LIVE_CODEX_HOST_PATH (or EQUIPO_LIVE_CLAUDE_PATH) to the pinned // CLIs. The binding's home is used through the runner's own lock and // runtime copy; nothing here reads or prints it. func TestLiveAttachment(t *testing.T) { if os.Getenv("EQUIPO_ATTACH_LIVE") != "1" { t.Skip("EQUIPO_ATTACH_LIVE is not set") } raw, err := os.ReadFile(os.Getenv("EQUIPO_LIVE_BINDINGS")) if err != nil { t.Fatal(err) } var all []map[string]any if err := json.Unmarshal(raw, &all); err != nil { t.Fatal(err) } var b map[string]any for _, x := range all { if x["id"] == os.Getenv("EQUIPO_LIVE_BINDING") { b = x } } if b == nil { t.Fatal("no such binding") } b["person_id"] = thintest.Owner inst := thintest.StartInstance(t) token := inst.DeviceToken(t) cred := inst.EnrolRunner(t) root, err := os.MkdirTemp("", "eqlive-") if err != nil { t.Fatal(err) } t.Cleanup(func() { os.RemoveAll(root) }) private(t, root) state := private(t, filepath.Join(root, "state")) if err := thintest.WriteRunnerCredential(state, cred); err != nil { t.Fatal(err) } config := private(t, filepath.Join(root, "config")) writeJSON(t, filepath.Join(config, "bindings.json"), []map[string]any{b}) bin := private(t, filepath.Join(root, "bin")) copyExecutable(t, filepath.Join(bin, "equipo-tool-proxy")) self, _ := os.Executable() 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} logger := slog.New(slog.NewTextHandler(io.Discard, nil)) 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")}} t.Cleanup(func() { r.stop(t) }) r.start(t) client := thintest.NewClient(inst.URL, token) client.HTTP.Timeout = 2 * time.Minute ctx, cancel := context.WithTimeout(context.Background(), 8*time.Minute) defer cancel() // One image whose answer is unmistakable: a red square on white. img := image.NewRGBA(image.Rect(0, 0, 256, 256)) for y := 0; y < 256; y++ { for x := 0; x < 256; x++ { c := color.RGBA{255, 255, 255, 255} if x > 48 && x < 208 && y > 48 && y < 208 { c = color.RGBA{220, 20, 30, 255} } img.Set(x, y, c) } } var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { t.Fatal(err) } a, err := client.Upload(ctx, "square.png", "image/png", buf.Bytes()) if err != nil { t.Fatal(err) } sent, err := client.SendWith(ctx, "What colour is the shape in this image? Answer in at most five words.", "live-1", []string{a.ID}) if err != nil { t.Fatal(err) } var reply wire.Message for reply.ID == "" { page, err := client.Messages(ctx, "", 10) if err != nil { t.Fatal(err) } for _, m := range page.Items { if m.Role == wire.RoleCoordinator && m.TurnID == sent.Turn.ID { reply = m } } turn, err := client.Turn(ctx, sent.Turn.ID) if err != nil { t.Fatal(err) } if turn.State == wire.TurnFailed || turn.State == wire.TurnCancelled { t.Fatalf("turn %s: %s", turn.State, turn.Reason) } if ctx.Err() != nil { t.Fatal("no reply") } time.Sleep(time.Second) } turn, _ := client.Turn(ctx, sent.Turn.ID) t.Logf("reply on %s (attempt %d): %q", turn.Connection, turn.Attempt, reply.Text) if !strings.Contains(strings.ToLower(reply.Text), "red") { t.Fatalf("the model did not see the image: %q", reply.Text) } // The Assistant (assistant attachments spec, plan Task 12): an image // in the turn it arrives, read again with read_attachment in a later // turn without files, and the injection witness. An engine whose MCP // client shows tool images (Codex; another only when this test build // is told so with EQUIPO_TEST_IMAGE_RESULTS) must name the colour // again; any other is logged, which is how one is checked. provider, _ := b["provider"].(string) shows := provider == "codex" || strings.Contains(","+os.Getenv("EQUIPO_TEST_IMAGE_RESULTS")+",", ","+provider+",") asst := client.Assistant() canAttach(t, ctx, asst, true) blue := square(t, color.RGBA{20, 40, 220, 255}) up, err := client.Upload(ctx, "blue.png", "image/png", blue) if err != nil { t.Fatal(err) } first, err := asst.SendWith(ctx, "What colour is the shape in this image? Answer in at most five words.", "live-a1", []string{up.ID}) if err != nil { t.Fatal(err) } if got := assistantReply(t, ctx, asst, first.Turn.ID); !strings.Contains(strings.ToLower(got.Text), "blue") { t.Fatalf("the Assistant did not see the image: %q", got.Text) } 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") if err != nil { t.Fatal(err) } later := assistantReply(t, ctx, asst, again.Turn.ID) t.Logf("Assistant on %s, the image read again (shown: %v): %q", provider, shows, later.Text) if shows && !strings.Contains(strings.ToLower(later.Text), "blue") { t.Fatalf("the Assistant did not see the image read again: %q", later.Text) } memo := injectionWitness(t, ctx, &Env{Client: client, Live: true, turns: new(int)}, asst) t.Logf("Assistant on %s, the memo: %q", provider, memo) } // square is a PNG of a coloured square on white, 256 pixels a side. func square(t *testing.T, c color.RGBA) []byte { t.Helper() img := image.NewRGBA(image.Rect(0, 0, 256, 256)) for y := 0; y < 256; y++ { for x := 0; x < 256; x++ { px := color.RGBA{255, 255, 255, 255} if x > 48 && x < 208 && y > 48 && y < 208 { px = c } img.Set(x, y, px) } } var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { t.Fatal(err) } return buf.Bytes() }