diff --git a/internal/gateway/image_mcp.go b/internal/gateway/image_mcp.go index 3f1d34eea..13e08419e 100644 --- a/internal/gateway/image_mcp.go +++ b/internal/gateway/image_mcp.go @@ -14,6 +14,10 @@ import ( "fmt" "io" "net/http" + "os" + "path/filepath" + "runtime/debug" + "slices" "strconv" "strings" "sync" @@ -195,11 +199,21 @@ type imageJob struct { } type imageFile struct { - data []byte + path string // the bytes, on disk: a kept image is never held in memory + size int mime, key string made time.Time + mem []byte // only when the file couldn't be written } +// imageFilesDir is the folder kept images are written to, emptied the first +// time it is used in a run (the store is lost at a restart, and so are its URLs). +var imageFilesDir = sync.OnceValue(func() string { + d := filepath.Join(settings.Dir(), "image-mcp") + os.RemoveAll(d) + return d +}) + func (s *Server) imageMCP(w http.ResponseWriter, r *http.Request) { serveMCP(w, r, "magpie's image generation", imageServerName, visionMCPBody, s.imageMCPCall) } @@ -361,6 +375,9 @@ func (s *Server) imageMCPDrawn(rec *replyRecorder, key, base string) map[string] if rec.code() != http.StatusOK { return toolResult("no image: "+imageMCPSaid(rec.body.Bytes()), true) } + // the recorded reply is the images' base64 once more: the strings + // parsed out of it are what is read from here + rec.body = bytes.Buffer{} if len(out.Data) == 0 { return toolResult("no image came back", true) } @@ -377,6 +394,7 @@ func (s *Server) imageMCPDrawn(rec *replyRecorder, key, base string) map[string] lost = append(lost, fmt.Sprintf("image %d came back broken: %v", i+1, err)) continue } + out.Data[i].B64 = "" // each image's base64 goes as it is decoded case d.URL != "": ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) img, mt, err = FetchImage(ctx, PublicClient(), d.URL) @@ -616,6 +634,9 @@ func (s *Server) imageMCPStart(key string, use *mcpUse, draw func() map[string]a defer st.running.Done() defer close(j.done) j.result = draw() + // a drawing holds the reply, its copies and the images for a moment: + // give the heap back to the system at once, as it is not asked again + debug.FreeOSMemory() }() return j } @@ -650,19 +671,51 @@ func (s *Server) imageMCPKeepFile(img []byte, mt, key string) string { st.secret = make([]byte, 32) rand.Read(st.secret) } - st.files[name] = &imageFile{data: img, mime: mt, key: key, made: made} + // on disk: images held for an hour in memory were up to 256 MB of the + // process's, for as long as no new image pushed them out + dir := imageFilesDir() + path := filepath.Join(dir, name) + if err := os.MkdirAll(dir, 0o700); err != nil || os.WriteFile(path, img, 0o600) != nil { + path = "" + } + f := &imageFile{path: path, size: len(img), mime: mt, key: key, made: made} + if path == "" { + f.mem = img + } + st.files[name] = f at := name + "?" + imageTokenParam + "=" + imageFileToken(st.secret, name, key, made.Add(imageMCPKeep).Unix()) st.order = append(st.order, name) st.bytes += len(img) for len(st.order) > 1 && (st.bytes > imageMCPFilesMost || time.Since(st.files[st.order[0]].made) > imageMCPKeep) { old := st.order[0] st.order = st.order[1:] - st.bytes -= len(st.files[old].data) - delete(st.files, old) + st.drop(old) } + // and gone at its hour even when no image follows it + time.AfterFunc(imageMCPKeep+time.Minute, func() { + st.mu.Lock() + defer st.mu.Unlock() + if f := st.files[name]; f != nil { + st.order = slices.DeleteFunc(st.order, func(n string) bool { return n == name }) + st.drop(name) + } + }) return at } +// drop forgets the file name and removes its bytes; under st.mu. +func (st *imageMCPStore) drop(name string) { + f := st.files[name] + if f == nil { + return + } + st.bytes -= f.size + delete(st.files, name) + if f.path != "" { + os.Remove(f.path) + } +} + // imageTokenParam is the query parameter of a file's URL that holds its // token (imageFileToken). const imageTokenParam = "t" @@ -752,7 +805,17 @@ func (s *Server) imageMCPFile(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "private, max-age=3600") // ranges, which a player asks a video in (Safari only plays an MP4 // served so) - http.ServeContent(w, r, "", f.made, bytes.NewReader(f.data)) + if f.path == "" { + http.ServeContent(w, r, "", f.made, bytes.NewReader(f.mem)) + return + } + file, err := os.Open(f.path) + if err != nil { + http.Error(w, "no such image", http.StatusNotFound) + return + } + defer file.Close() + http.ServeContent(w, r, "", f.made, file) } // imageMCPBase is the address the agent reaches the gateway at, for the diff --git a/internal/gateway/image_mcp_memory_test.go b/internal/gateway/image_mcp_memory_test.go new file mode 100644 index 000000000..457c773a6 --- /dev/null +++ b/internal/gateway/image_mcp_memory_test.go @@ -0,0 +1,62 @@ +package gateway + +import ( + "crypto/rand" + "encoding/base64" + "encoding/json" + "net/http" + "runtime" + "testing" +) + +// Images the image MCP server draws are kept for an hour so a link can +// download them. Kept in memory, five 12 MB drawings were 60 MB of the +// process's heap until an hour later or the next drawing (tnt12231 on X: +// magpie's memory at 1 GB+ while drawing). They are kept on disk, and the +// reply they came in is let go. +func TestImageMCPKeepsDrawingsOffTheHeap(t *testing.T) { + s, _ := easeled(t) + t.Setenv("MAGPIE_PUBLIC_URL", "") + heap := func() uint64 { + var m runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&m) + return m.HeapAlloc + } + before := heap() + body := func() []byte { + img := make([]byte, 12<<20) + rand.Read(img) + copy(img, "\x89PNG\r\n\x1a\n") + b64 := base64.StdEncoding.EncodeToString(img) + type item struct { + B64 string `json:"b64_json"` + } + b, _ := json.Marshal(map[string]any{"model": "m", "data": []item{{b64}, {b64}}}) + return b + }() + + var urls []string + for range 3 { + rec := &replyRecorder{header: http.Header{}} + rec.body.Write(body) + res := s.imageMCPDrawn(rec, "", "http://127.0.0.1:3425") + if res["isError"] == true { + t.Fatalf("%v", res) + } + urls = append(urls, "x") + } + after := heap() - uint64(len(body)) + runtime.KeepAlive(body) + st := &s.imageStore + st.mu.Lock() + n, kept := len(st.files), st.bytes + st.mu.Unlock() + if n != 6 || kept < 6*(12<<20) { + t.Fatalf("kept %d files, %d bytes", n, kept) + } + // 72 MB of drawings are kept: the heap holds none of them + if grew := int64(after) - int64(before); grew > 16<<20 { + t.Fatalf("the heap grew by %d MB with %d MB of drawings kept", grew>>20, kept>>20) + } +} diff --git a/internal/imagemcp/imagemcp.go b/internal/imagemcp/imagemcp.go index 8906cd131..2011ed0e0 100644 --- a/internal/imagemcp/imagemcp.go +++ b/internal/imagemcp/imagemcp.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "runtime" + "runtime/debug" "strconv" "strings" "sync" @@ -525,6 +526,9 @@ func (s *server) generate(raw json.RawMessage, used *string, c caller) (string, if len(out.Data) == 0 { return "", errors.New("no image came back") } + // the reply's bytes are the images' base64 a second time: what is read + // from here is the strings parsed out of them, each dropped once decoded + defer debug.FreeOSMemory() // an image that was drawn, and paid for, is never dropped unsaid: one // that can't be had is answered by where the vendor keeps it, beside // those saved (#1576) @@ -537,6 +541,7 @@ func (s *server) generate(raw json.RawMessage, used *string, c caller) (string, lost = append(lost, fmt.Sprintf("image %d came back broken: %v", i+1, err)) continue } + out.Data[i].B64 = "" case d.URL != "": if img, mt, err = gateway.FetchImage(context.Background(), gateway.PublicClient(), d.URL); err != nil { lost = append(lost, fmt.Sprintf("image %d was drawn but couldn't be downloaded (%v); the vendor keeps it at %s for a while: download it from there", i+1, err, d.URL))