Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c748459
fix(mcp): forward image tool results on the existing image channel
cairn-intern Aug 28, 2026
2c260ee
fix(mcp): bound total forwarded image bytes in one MCP result
cairn-intern Aug 28, 2026
96f27ce
fix(mcp): decode each MCP image payload at most once
cairn-intern Aug 28, 2026
3898049
fix(mcp): placeholder for image-only results and distinct budget-drop…
cairn-intern Aug 28, 2026
144b0e3
fix(mcp): gate tool images on vision support and accept at-limit padding
cairn-intern Sep 1, 2026
6af970d
fix(agent): evaluate tool image vision gate after turn model switch
cairn-intern Sep 1, 2026
a614863
fix(mcp): align SSE payload limits, separate uninspected budget block…
cairn-intern Sep 1, 2026
3a5389d
test(mcp): use valid JSON-RPC structure for oversized SSE event test
cairn-intern Sep 1, 2026
c502e7c
fix(mcp): snapshot vision capabilities, expand SSE payload bound, and…
cairn-intern Sep 2, 2026
fe7c4a1
fix(tui): snapshot provider models before command and prioritize acti…
euxaristia Sep 5, 2026
ca811fd
Avoid mutating shared model variable in discovery race test
euxaristia Sep 5, 2026
b39a2bc
Address review feedback on MCP image forwarding and capability discovery
euxaristia Sep 6, 2026
ca25d7b
Bound MCP image counts and recognize supported local vision models.
euxaristia Sep 7, 2026
a1ab45c
fix(mcp): bound candidate image inspection and align vision recovery …
euxaristia Sep 12, 2026
2e85269
fix(tui): qualify authoritative discovery by active endpoint
euxaristia Sep 15, 2026
34c10af
fix(acp,mcp): preserve clean user history and remove unused writer he…
euxaristia Sep 15, 2026
d693792
fix(mcp,tui): relocate test-only helpers and restore escalation comment
euxaristia Sep 17, 2026
05a584f
fix(mcp): report accurate limiting cause for uninspected images
euxaristia Sep 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion internal/acp/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/Gitlawb/zero/internal/agent"
"github.com/Gitlawb/zero/internal/config"
"github.com/Gitlawb/zero/internal/modelregistry"
"github.com/Gitlawb/zero/internal/providercatalog"
"github.com/Gitlawb/zero/internal/providermodelcatalog"
"github.com/Gitlawb/zero/internal/providermodeldiscovery"
Expand Down Expand Up @@ -508,6 +509,30 @@ func (a *Agent) runTurn(ctx context.Context, sess *acpSession, userText string,
}
queue(messageEvent("user", userText))

visionCache := make(map[string]bool)
var visionCacheMu sync.Mutex
supportsVision := func(modelID string) bool {
modelID = strings.TrimSpace(modelID)
if modelID == "" {
return false
}
visionCacheMu.Lock()
defer visionCacheMu.Unlock()
if cached, ok := visionCache[modelID]; ok {
return cached
}
supported := a.modelSupportsVision(ctx, resolved.Provider, modelID)
visionCache[modelID] = supported
return supported
}
effectivePrompt := userText
if len(images) > 0 && !supportsVision(resolved.Provider.Model) {
msg := fmt.Sprintf("Model %s does not support image input; ignoring %d prompt image(s).", resolved.Provider.Model, len(images))
note.text("[zero] " + msg + "\n\n")
effectivePrompt = fmt.Sprintf("[Note: %s]\n\n%s", msg, userText)
images = nil
}

opts := agent.Options{
Cwd: sess.cwd,
SessionID: sess.id,
Expand All @@ -519,6 +544,7 @@ func (a *Agent) runTurn(ctx context.Context, sess *acpSession, userText string,
DeferThreshold: workspace.DeferThreshold,
MaxTurns: resolved.MaxTurns,
Images: images,
SupportsVision: supportsVision,
OnText: note.text,
OnReasoning: note.thought,
OnToolCall: func(call agent.ToolCall) {
Expand All @@ -537,7 +563,7 @@ func (a *Agent) runTurn(ctx context.Context, sess *acpSession, userText string,
},
}

agentPrompt := buildPrompt(sess.snapshotHistory(), userText)
agentPrompt := buildPrompt(sess.snapshotHistory(), effectivePrompt)
result, runErr := a.deps.RunAgent(ctx, agentPrompt, provider, opts)
if result.FinalAnswer != "" {
queue(messageEvent("assistant", result.FinalAnswer))
Expand Down Expand Up @@ -1305,3 +1331,32 @@ func sameWorkspace(left, right string) bool {
}
return os.SameFile(leftInfo, rightInfo)
}

func (a *Agent) modelSupportsVision(ctx context.Context, profile config.ProviderProfile, modelID string) bool {
trimmed := strings.TrimSpace(modelID)
if trimmed == "" {
return false
}
if a.deps.DiscoverModels != nil {
if discovered, err := a.deps.DiscoverModels(ctx, profile); err == nil {
for _, dm := range discovered {
if strings.EqualFold(strings.TrimSpace(dm.ID), trimmed) {
if len(dm.InputModalities) > 0 {
for _, mod := range dm.InputModalities {
if strings.EqualFold(strings.TrimSpace(mod), "image") {
return true
}
}
return false
}
break
}
}
}
}
reg, _ := modelregistry.DefaultRegistry()
if entry, known := reg.Resolve(trimmed); known {
return entry.Supports(modelregistry.ModelCapabilityVision)
}
return modelregistry.SupportsVision(reg, trimmed)
}
241 changes: 241 additions & 0 deletions internal/acp/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package acp

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -847,6 +849,48 @@ func TestACPRunTurnClosesWorkspaceAfterSuccessAndFailure(t *testing.T) {
}
}

func TestACPWiresSupportsVision(t *testing.T) {
deps := testDeps(t)
var captured agent.Options
deps.RunAgent = func(_ context.Context, _ string, _ zeroruntime.Provider, opts agent.Options) (agent.Result, error) {
captured = opts
return agent.Result{FinalAnswer: "done"}, nil
}
deps.DiscoverModels = func(_ context.Context, _ config.ProviderProfile) ([]providermodeldiscovery.Model, error) {
return []providermodeldiscovery.Model{
{ID: "custom-vision", InputModalities: []string{"text", "image"}},
{ID: "custom-text", InputModalities: []string{"text"}},
}, nil
}
h := newHarness(t, deps)
defer h.stop()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var newRes NewSessionResult
if err := h.client.Call(ctx, MethodSessionNew, NewSessionParams{Cwd: t.TempDir(), McpServers: []McpServer{}}, &newRes); err != nil {
t.Fatalf("session/new: %v", err)
}
var promptRes PromptResult
if err := h.client.Call(ctx, MethodSessionPrompt, PromptParams{
SessionID: newRes.SessionID,
Prompt: []ContentBlock{
{Type: "text", Text: "hello"},
},
}, &promptRes); err != nil {
t.Fatalf("session/prompt: %v", err)
}
if captured.SupportsVision == nil {
t.Fatal("SupportsVision was not wired into agent.Options")
}
if !captured.SupportsVision("custom-vision") {
t.Fatal("SupportsVision(custom-vision) = false, want true")
}
if captured.SupportsVision("custom-text") {
t.Fatal("SupportsVision(custom-text) = true, want false")
}
}

// TestACPRejectsInvalidCwd confirms session/new fails when the workspace root
// resolver rejects the client cwd (e.g. filesystem root).
func TestACPRejectsInvalidCwd(t *testing.T) {
Expand Down Expand Up @@ -2418,3 +2462,200 @@ func payloadString(payload any, key string) string {
value, _ := decoded[key].(string)
return value
}

func TestACPPromptUnsupportedModelDropsImagesAndNotifies(t *testing.T) {
deps := testDeps(t)
deps.DiscoverModels = func(ctx context.Context, p config.ProviderProfile) ([]providermodeldiscovery.Model, error) {
return []providermodeldiscovery.Model{
{ID: "fake-model", InputModalities: []string{"text"}},
}, nil
}
var capturedOpts agent.Options
var capturedPrompt string
deps.RunAgent = func(ctx context.Context, prompt string, provider zeroruntime.Provider, opts agent.Options) (agent.Result, error) {
capturedPrompt = prompt
capturedOpts = opts
return agent.Result{FinalAnswer: "Answered without images"}, nil
}

h := newHarness(t, deps)
defer h.stop()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var initRes InitializeResult
if err := h.client.Call(ctx, MethodInitialize, InitializeParams{ProtocolVersion: ProtocolVersion}, &initRes); err != nil {
t.Fatalf("initialize: %v", err)
}

var newRes NewSessionResult
if err := h.client.Call(ctx, MethodSessionNew, NewSessionParams{Cwd: t.TempDir(), McpServers: []McpServer{}}, &newRes); err != nil {
t.Fatalf("session/new: %v", err)
}

rawPng := "\x89PNG\r\n\x1a\nfake-image-bytes"
b64Png := base64.StdEncoding.EncodeToString([]byte(rawPng))
var promptRes PromptResult
if err := h.client.Call(ctx, MethodSessionPrompt, PromptParams{
SessionID: newRes.SessionID,
Prompt: []ContentBlock{
TextBlock("look at this image"),
ImageBlock(b64Png, "image/png"),
},
}, &promptRes); err != nil {
t.Fatalf("session/prompt: %v", err)
}
if promptRes.StopReason != StopEndTurn {
t.Fatalf("stopReason = %q, want %q", promptRes.StopReason, StopEndTurn)
}

got := drainTextUntil(t, h.updates, func(text string) bool {
return strings.Contains(text, "does not support image input")
})
if !strings.Contains(got, "Model fake-model does not support image input; ignoring 1 prompt image(s).") {
t.Fatalf("streamed text = %q, want drop notice", got)
}
if len(capturedOpts.Images) != 0 {
t.Fatalf("captured %d images, want 0 (images should be withheld)", len(capturedOpts.Images))
}
if !strings.Contains(capturedPrompt, "[Note: Model fake-model does not support image input; ignoring 1 prompt image(s).]") {
t.Fatalf("captured prompt = %q, want prompt note", capturedPrompt)
}
}

func TestACPSessionPrompt_SupportsVisionMemoizedPerRun(t *testing.T) {
deps := testDeps(t)
var discoverCount int
var discoverMu sync.Mutex
deps.DiscoverModels = func(ctx context.Context, p config.ProviderProfile) ([]providermodeldiscovery.Model, error) {
discoverMu.Lock()
discoverCount++
discoverMu.Unlock()
return []providermodeldiscovery.Model{
{ID: "fake-model", InputModalities: []string{"text", "image"}},
}, nil
}
deps.RunAgent = func(ctx context.Context, prompt string, provider zeroruntime.Provider, opts agent.Options) (agent.Result, error) {
for i := 0; i < 5; i++ {
if !opts.SupportsVision(opts.Model) {
t.Error("expected SupportsVision to report true")
}
}
return agent.Result{FinalAnswer: "Done"}, nil
}

h := newHarness(t, deps)
defer h.stop()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var initRes InitializeResult
if err := h.client.Call(ctx, MethodInitialize, InitializeParams{ProtocolVersion: ProtocolVersion}, &initRes); err != nil {
t.Fatalf("initialize: %v", err)
}

var newRes NewSessionResult
if err := h.client.Call(ctx, MethodSessionNew, NewSessionParams{Cwd: t.TempDir(), McpServers: []McpServer{}}, &newRes); err != nil {
t.Fatalf("session/new: %v", err)
}

discoverMu.Lock()
discoverCount = 0
discoverMu.Unlock()

rawPng := "\x89PNG\r\n\x1a\nfake-image-bytes"
b64Png := base64.StdEncoding.EncodeToString([]byte(rawPng))
var promptRes PromptResult
if err := h.client.Call(ctx, MethodSessionPrompt, PromptParams{
SessionID: newRes.SessionID,
Prompt: []ContentBlock{
TextBlock("look at this"),
ImageBlock(b64Png, "image/png"),
},
}, &promptRes); err != nil {
t.Fatalf("session/prompt: %v", err)
}
if promptRes.StopReason != StopEndTurn {
t.Fatalf("stopReason = %q, want %q", promptRes.StopReason, StopEndTurn)
}

discoverMu.Lock()
count := discoverCount
discoverMu.Unlock()
if count != 1 {
t.Fatalf("DiscoverModels was called %d times, want exactly 1 call (memoized per run)", count)
}
}

func TestACPEndToEndImagePromptSupportedModel(t *testing.T) {
deps := testDeps(t)
deps.DiscoverModels = func(ctx context.Context, p config.ProviderProfile) ([]providermodeldiscovery.Model, error) {
return []providermodeldiscovery.Model{
{ID: "discovered-vision-model", InputModalities: []string{"text", "image"}},
}, nil
}
deps.ResolveConfig = func(_ string, o config.Overrides) (config.ResolvedConfig, error) {
model := "discovered-vision-model"
if o.Provider.Model != "" {
model = o.Provider.Model
}
return config.ResolvedConfig{
Provider: config.ProviderProfile{Name: "fake", Model: model},
MaxTurns: 4,
}, nil
}
var capturedOpts agent.Options
var capturedPrompt string
deps.RunAgent = func(ctx context.Context, prompt string, provider zeroruntime.Provider, opts agent.Options) (agent.Result, error) {
capturedPrompt = prompt
capturedOpts = opts
return agent.Result{FinalAnswer: "I can see the image!"}, nil
}

h := newHarness(t, deps)
defer h.stop()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var initRes InitializeResult
if err := h.client.Call(ctx, MethodInitialize, InitializeParams{ProtocolVersion: ProtocolVersion}, &initRes); err != nil {
t.Fatalf("initialize: %v", err)
}

var newRes NewSessionResult
if err := h.client.Call(ctx, MethodSessionNew, NewSessionParams{Cwd: t.TempDir(), McpServers: []McpServer{}}, &newRes); err != nil {
t.Fatalf("session/new: %v", err)
}

rawPng := []byte("\x89PNG\r\n\x1a\nreal-png-bytes")
b64Png := base64.StdEncoding.EncodeToString(rawPng)
var promptRes PromptResult
if err := h.client.Call(ctx, MethodSessionPrompt, PromptParams{
SessionID: newRes.SessionID,
Prompt: []ContentBlock{
TextBlock("analyze this image"),
ImageBlock(b64Png, "image/png"),
},
}, &promptRes); err != nil {
t.Fatalf("session/prompt: %v", err)
}
if promptRes.StopReason != StopEndTurn {
t.Fatalf("stopReason = %q, want %q", promptRes.StopReason, StopEndTurn)
}

if len(capturedOpts.Images) != 1 {
t.Fatalf("captured %d images, want 1", len(capturedOpts.Images))
}
if capturedOpts.Images[0].MediaType != "image/png" {
t.Fatalf("mediaType = %q, want image/png", capturedOpts.Images[0].MediaType)
}
if !bytes.Equal(capturedOpts.Images[0].Data, rawPng) {
t.Fatalf("image bytes mismatch: got %v, want %v", capturedOpts.Images[0].Data, rawPng)
}
if strings.Contains(capturedPrompt, "does not support image input") {
t.Fatalf("prompt unexpectedly contains refusal note: %q", capturedPrompt)
}
}
Loading
Loading