WeKnora/internal/agent/engine_image_test.go

226 lines
6.8 KiB
Go
Raw Normal View History

package agent
import (
"context"
"encoding/base64"
"errors"
"strings"
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
"sync/atomic"
"testing"
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
"time"
)
func TestDecodeDataURIBytes_Valid(t *testing.T) {
raw := []byte{0xFF, 0xD8, 0xFF} // minimal bytes
encoded := base64.StdEncoding.EncodeToString(raw)
dataURI := "data:image/jpeg;base64," + encoded
decoded, err := decodeDataURIBytes(dataURI)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(decoded) != len(raw) {
t.Errorf("expected %d bytes, got %d", len(raw), len(decoded))
}
}
func TestDecodeDataURIBytes_NoPaddingFallback(t *testing.T) {
raw := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
// RawStdEncoding omits padding '='
encoded := base64.RawStdEncoding.EncodeToString(raw)
dataURI := "data:image/png;base64," + encoded
decoded, err := decodeDataURIBytes(dataURI)
if err != nil {
t.Fatalf("unexpected error with padding fallback: %v", err)
}
if len(decoded) != len(raw) {
t.Errorf("expected %d bytes, got %d", len(raw), len(decoded))
}
}
func TestDecodeDataURIBytes_NoDataPrefix(t *testing.T) {
_, err := decodeDataURIBytes("image/png;base64,AAAA")
if err == nil {
t.Fatal("expected error for missing data: prefix")
}
if !strings.Contains(err.Error(), "not a data URI") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestDecodeDataURIBytes_NoBase64Marker(t *testing.T) {
_, err := decodeDataURIBytes("data:image/png,rawdata")
if err == nil {
t.Fatal("expected error for missing ;base64, marker")
}
if !strings.Contains(err.Error(), "unsupported") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestDecodeDataURIBytes_EmptyBase64(t *testing.T) {
decoded, err := decodeDataURIBytes("data:image/png;base64,")
if err != nil {
t.Fatalf("unexpected error for empty base64: %v", err)
}
if len(decoded) != 0 {
t.Errorf("expected 0 bytes for empty base64, got %d", len(decoded))
}
}
func TestDescribeImages_WithDescriber(t *testing.T) {
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
return "A red square image", nil
},
}
raw := []byte{0x89, 0x50, 0x4E, 0x47} // fake PNG header
dataURI := "data:image/png;base64," + base64.StdEncoding.EncodeToString(raw)
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(context.Background(), []string{dataURI}, nil)
if len(descriptions) != 1 {
t.Fatalf("expected 1 description, got %d", len(descriptions))
}
if descriptions[0] != "A red square image" {
t.Errorf("unexpected description: %s", descriptions[0])
}
}
func TestDescribeImages_VLMFailure(t *testing.T) {
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
return "", errors.New("VLM service unavailable")
},
}
raw := []byte{0x89, 0x50}
dataURI := "data:image/png;base64," + base64.StdEncoding.EncodeToString(raw)
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(context.Background(), []string{dataURI}, nil)
if len(descriptions) != 1 || descriptions[0] != "" {
t.Errorf("expected one empty description on VLM failure, got %q", descriptions)
}
}
func TestDescribeImages_InvalidDataURI(t *testing.T) {
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
t.Fatal("imageDescriber should not be called for invalid data URI")
return "", nil
},
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(context.Background(), []string{"not-a-data-uri"}, nil)
if len(descriptions) != 1 || descriptions[0] != "" {
t.Errorf("expected one empty description for invalid URI, got %q", descriptions)
}
}
func TestDescribeImages_ContextCancelled(t *testing.T) {
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
var callCount atomic.Int32
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
callCount.Add(1)
return "desc", nil
},
}
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
raw := []byte{0x89}
dataURI := "data:image/png;base64," + base64.StdEncoding.EncodeToString(raw)
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(ctx, []string{dataURI, dataURI, dataURI}, nil)
if n := callCount.Load(); n != 0 {
t.Errorf("expected 0 VLM calls with cancelled context, got %d", n)
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
if strings.Join(descriptions, "") != "" {
t.Errorf("expected no descriptions, got %q", descriptions)
}
}
func TestDescribeImages_NilDescriber(t *testing.T) {
engine := &AgentEngine{
imageDescriber: nil,
}
// Should not panic even with nil describer
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(context.Background(), []string{"data:image/png;base64,AAAA"}, nil)
if strings.Join(descriptions, "") != "" {
t.Errorf("expected no descriptions with nil describer, got %q", descriptions)
}
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
func TestDescribeImages_MixedSuccessStaysAligned(t *testing.T) {
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
if string(imgBytes) == "2" {
return "", errors.New("fail on second")
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
return "desc " + string(imgBytes), nil
},
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
uris := []string{
"data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte("1")),
"data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte("2")),
"data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte("3")),
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
descriptions := engine.describeImages(context.Background(), uris, nil)
want := []string{"desc 1", "", "desc 3"}
if strings.Join(descriptions, "|") != strings.Join(want, "|") {
t.Errorf("descriptions = %q; want %q", descriptions, want)
}
}
feat(agent): read chat attachments on demand (#4203) * feat(agent): read chat attachments on demand Agent turns pasted query-selected attachment excerpts into the prompt before the model could act. List stored files as a manifest so the agent can page and search them through read_attachment. * fix(agent): preserve attachment access and image reads * fix(agent): bound attachment cleanup and image processing * Remove standalone read_attachment tool * Read attachments and images through read_file Merge read_attachment into read_file (attachment:// paths, numbered document images, staged-copy aliasing, query search), return sandbox images as images, describe images for non-vision models inside the tool call with bounded concurrency and model-chosen detail/focus, render image reads in the UI, and record image metadata in Langfuse. * fix(agent): make attachment pagination complete and bound image reads * refactor(agent): separate attachment reads and cache image observations * fix(attachment): read image batches concurrently and harden description cache - ReadImages authorizes once and describes images in parallel - start VLM deadlines after a slot is held; longer timeout for full reads - detach shared description calls from the first caller's context - skip rewriting unchanged sandbox projections - strip the description cache from temporary document API responses - render upload text before calling the vision model * fix(attachment): number images in document order and polish attachment UI - return resolved images in document order instead of reverse - show attachment file names instead of attachment:// IDs in tool titles - use the shared file icon and a lighter card for chat attachments * fix(ci): renumber attachment migrations and satisfy lint Main already took sqlite 000035 and versioned 000116, so the attachment lookup indexes move to the next free numbers. Also wrap the ReadImages signature and drop an unused test parameter. * fix(ci): expect sqlite migrations to reach version 36
2026-10-13 06:37:17 +00:00
func TestDescribeImages_RunsConcurrentlyWithPerImageTimeout(t *testing.T) {
var running, peak atomic.Int32
release := make(chan struct{})
engine := &AgentEngine{
imageDescriber: func(ctx context.Context, _ []byte, _ string) (string, error) {
n := running.Add(1)
defer running.Add(-1)
for {
old := peak.Load()
if n <= old || peak.CompareAndSwap(old, n) {
break
}
}
if _, ok := ctx.Deadline(); !ok {
return "", errors.New("missing per-image deadline")
}
<-release
return "ok", nil
},
}
dataURI := "data:image/png;base64," + base64.StdEncoding.EncodeToString([]byte{0x89})
uris := make([]string, 6)
for i := range uris {
uris[i] = dataURI
}
done := make(chan []string)
go func() { done <- engine.describeImages(context.Background(), uris, nil) }()
deadline := time.After(5 * time.Second)
for peak.Load() < toolImageDescribeConcurrency {
select {
case <-deadline:
t.Fatalf("peak concurrency = %d; want %d", peak.Load(), toolImageDescribeConcurrency)
case <-time.After(5 * time.Millisecond):
}
}
close(release)
descriptions := <-done
if peak.Load() != toolImageDescribeConcurrency {
t.Errorf("peak concurrency = %d; want %d", peak.Load(), toolImageDescribeConcurrency)
}
for i, desc := range descriptions {
if desc != "ok" {
t.Errorf("description %d = %q; want ok", i, desc)
}
}
}