-
Notifications
You must be signed in to change notification settings - Fork 365
fix: reduce retained tool output memory #2854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "log/slog" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
@@ -714,6 +715,8 @@ func isInitNotificationSendError(err error) bool { | |
| return false | ||
| } | ||
|
|
||
| const maxInlineMediaBytes = 256 * 1024 | ||
|
|
||
| func processMCPContent(toolResult *mcp.CallToolResult) *tools.ToolCallResult { | ||
| var text strings.Builder | ||
| var images, audios []tools.MediaContent | ||
|
|
@@ -760,12 +763,54 @@ func processMCPContent(toolResult *mcp.CallToolResult) *tools.ToolCallResult { | |
| } | ||
| } | ||
|
|
||
| // encodeMedia re-encodes raw bytes (as decoded by the MCP SDK) back to base64 | ||
| // for our internal MediaContent representation. | ||
| // encodeMedia keeps small payloads inline and spools larger ones to disk so the | ||
| // session and TUI do not retain duplicate base64 copies. | ||
| func encodeMedia(data []byte, mimeType string) tools.MediaContent { | ||
| return tools.MediaContent{ | ||
| Data: base64.StdEncoding.EncodeToString(data), | ||
| MimeType: mimeType, | ||
| media := tools.MediaContent{MimeType: mimeType} | ||
| if len(data) <= maxInlineMediaBytes { | ||
| media.Data = base64.StdEncoding.EncodeToString(data) | ||
| return media | ||
| } | ||
|
|
||
| path, err := writeMediaFile(data, mimeType) | ||
| if err != nil { | ||
| slog.Warn("failed to spool MCP media to disk", "mime_type", mimeType, "bytes", len(data), "error", err) | ||
| media.Data = base64.StdEncoding.EncodeToString(data) | ||
| return media | ||
| } | ||
| media.FilePath = path | ||
| return media | ||
| } | ||
|
|
||
| func writeMediaFile(data []byte, mimeType string) (string, error) { | ||
| dir, err := os.MkdirTemp("", "docker-agent-mcp-media-*") | ||
| if err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [BLOCKING] Disk leak — these temp directories are never removed.
Suggested fix: introduce a session-scoped temp directory (created once, removed on |
||
| return "", err | ||
| } | ||
| path := filepath.Join(dir, "media"+mediaExtension(mimeType)) | ||
| if err := os.WriteFile(path, data, 0o600); err != nil { | ||
| _ = os.RemoveAll(dir) | ||
| return "", err | ||
| } | ||
| return path, nil | ||
| } | ||
|
|
||
| func mediaExtension(mimeType string) string { | ||
| switch mimeType { | ||
| case "image/png": | ||
| return ".png" | ||
| case "image/jpeg": | ||
| return ".jpg" | ||
| case "image/gif": | ||
| return ".gif" | ||
| case "image/webp": | ||
| return ".webp" | ||
| case "audio/wav", "audio/wave", "audio/x-wav": | ||
| return ".wav" | ||
| case "audio/mpeg", "audio/mp3": | ||
| return ".mp3" | ||
| default: | ||
| return ".bin" | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "iter" | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
@@ -536,3 +539,19 @@ func TestCallToolRecoversFromErrSessionMissing(t *testing.T) { | |
| assert.Equal(t, "recovered", result.Output) | ||
| assert.Equal(t, int32(2), callCount.Load(), "expected exactly 2 CallTool invocations (1 failed + 1 retry)") | ||
| } | ||
|
|
||
| func TestProcessMCPContentSpoolsLargeMedia(t *testing.T) { | ||
| large := bytes.Repeat([]byte("x"), maxInlineMediaBytes+1) | ||
| result := processMCPContent(callToolResult(&mcp.ImageContent{Data: large, MIMEType: "image/png"})) | ||
|
|
||
| require.Len(t, result.Images, 1) | ||
| img := result.Images[0] | ||
| assert.Empty(t, img.Data) | ||
| assert.Equal(t, "image/png", img.MimeType) | ||
| require.NotEmpty(t, img.FilePath) | ||
| defer os.RemoveAll(filepath.Dir(img.FilePath)) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test correctly cleans up with |
||
| got, err := os.ReadFile(img.FilePath) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, large, got) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -240,7 +240,7 @@ func (m *Model) UpdateToolResult(toolCallID, content string, status types.ToolSt | |
|
|
||
| entry.msg.Content = strings.ReplaceAll(content, "\t", " ") | ||
| entry.msg.ToolStatus = status | ||
| entry.msg.ToolResult = result | ||
| entry.msg.ToolResult = result.WithoutPayload() | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Non-blocking] Double The caller in |
||
| // Set grace period if transitioning from in-progress to completed | ||
| // Total visible time = completedToolVisibleDuration + completedToolFadeDuration | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Non-blocking] Brittle timing assertion and weak concurrency coverage.
This
< time.Secondbound will be flaky under heavy CI load. More importantly, the test only covers the sequential Start→Stop path, not the concurrent-Stop path that thes.stoppingguard was added to fix. A test with two goroutines callingStopconcurrently while the watcher is alive (e.g. blocked insess.Wait()) would be a stronger regression guard. Consider usinggoleakto assert no goroutines are left behind.