diff --git a/docs/features/activity-log.md b/docs/features/activity-log.md index c6ce59cbc..d08c80754 100644 --- a/docs/features/activity-log.md +++ b/docs/features/activity-log.md @@ -474,7 +474,7 @@ Activity logging is enabled by default. Configure via `mcp_config.json`: | `activity_retention_days` | 90 | Days to retain activity records | | `activity_max_records` | 100000 | Maximum records before pruning oldest | | `activity_max_size_mb` | 256 | Maximum total activity-log size in MB before pruning oldest (`0` disables). Runs alongside the age and count caps to bound `config.db` growth when records carry large payloads. | -| `activity_max_response_size` | 65536 | Max response size stored (bytes) | +| `activity_max_response_size` | 65536 | Max response text stored per record (bytes). Applies to `tool_call`, `internal_tool_call` and `prompt_get`; `0` or absent falls back to 65536 and it cannot be disabled. Read at startup only, like its retention siblings. Truncated text keeps a `...[truncated]` suffix. Already-stored records are not rewritten, and (as with pruning) the BBolt file does not shrink on disk. Sensitive-data detection still scans the untruncated response, under its own `sensitive_data_detection.max_payload_size_kb` cap. | | `activity_cleanup_interval_min` | 60 | Background cleanup interval (minutes) | > **Why the size cap?** The age and count caps alone do not bound disk: with large per-record payloads the log can reach hundreds of MB while still under 100k records / 90 days. `activity_max_size_mb` removes the oldest records (always keeping the newest) until the log is within the byte budget. Note: pruning frees pages for reuse but does not shrink the database file on disk (BBolt does not return freed pages to the OS). diff --git a/internal/runtime/activity_response_truncation_test.go b/internal/runtime/activity_response_truncation_test.go new file mode 100644 index 000000000..de78a9e10 --- /dev/null +++ b/internal/runtime/activity_response_truncation_test.go @@ -0,0 +1,314 @@ +package runtime + +import ( + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/security" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/storage" +) + +// allActivities lists without DefaultActivityFilter's ExcludeCallToolSuccess, +// which would drop the successful call_tool_* records this file is about. +func allActivities(t *testing.T, store *storage.Manager) []*storage.ActivityRecord { + t.Helper() + records, _, err := store.ListActivities(storage.ActivityFilter{Limit: 100}) + require.NoError(t, err) + return records +} + +// Retention bounds the activity log by age, count and TOTAL size, but nothing +// bounded a SINGLE record: activity_max_response_size was declared in config and +// never read, and TruncateActivityResponse had no production caller. A proxied +// upstream response was persisted whole, so a handful of large calls could add +// hundreds of MB to config.db with nothing logged. +// +// Each case drives the real event path and asserts against what actually landed +// in storage, so it fails on the pre-fix code rather than on a mock. + +func TestToolCallResponseTruncatedForStorage(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetMaxResponseSize(1024) + + svc.handleEvent(Event{ + Type: EventTypeActivityToolCallCompleted, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "tool_name": "big_tool", + "status": "success", + "response": strings.Repeat("x", 50_000), + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + + assert.Less(t, len(records[0].Response), 50_000, + "a 50KB response must not be persisted whole under a 1KB cap") + assert.True(t, strings.HasSuffix(records[0].Response, "...[truncated]"), + "truncation must be visible in the stored text") + assert.True(t, records[0].ResponseTruncated, + "a record shortened on the write path must say so") +} + +// call_tool_read proxies whole upstream payloads, which is where the largest +// records in a real database came from. +// +// The flag assertion is the interesting half. On an internal record +// ResponseTruncated means "the agent received LESS than ResponseBytes", and two +// cost consumers drop such rows from delivered traffic +// (truncatedBuiltinOverstatesDelivery, bench/replaycorpus). A built-in cut only +// on the way into the log was still delivered whole, so the flag must stay off +// or the row vanishes from the usage timeline with an honest ResponseBytes. +func TestInternalToolCallResponseTruncatedForStorage(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetMaxResponseSize(1024) + + svc.handleEvent(Event{ + Type: EventTypeActivityInternalToolCall, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "internal_tool_name": "call_tool_read", + "status": "success", + "response": strings.Repeat("y", 50_000), + "response_bytes": int64(50_000), + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + + assert.Less(t, len(records[0].Response), 50_000, + "internal tool calls take the same cap as upstream calls") + assert.True(t, strings.HasSuffix(records[0].Response, "...[truncated]"), + "the stored text still shows it was cut") + assert.False(t, records[0].ResponseTruncated, + "a storage-only cut must not set the flag that excludes the row from delivered bytes") +} + +// The delivered-bytes consequence of the assertion above, driven end to end: +// a built-in cut only for storage must still be counted in the usage timeline. +// Mirrors TestUsageAggregate_TruncatedBuiltinDoesNotInflateDeliveredBytes from +// the other side — that one proves an emitter-flagged record is excluded, this +// one proves a storage-cut record is not. +func TestStorageTruncatedBuiltinStillCountsDeliveredBytes(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetMaxResponseSize(1024) + + svc.handleEvent(Event{ + Type: EventTypeActivityInternalToolCall, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "internal_tool_name": "describe_tool", + "status": "success", + "response": strings.Repeat("z", 80_000), + "response_bytes": int64(80_000), + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + require.EqualValues(t, 80_000, records[0].ResponseBytes, + "ResponseBytes describes what the agent received, before any storage cut") + + agg := newUsageAggregate() + agg.Apply(records[0]) + var delivered int64 + for _, b := range agg.Buckets { + delivered += b.RespBytesSum + } + assert.EqualValues(t, 80_000, delivered, + "describe_tool delivers its response whole; only the stored copy was cut") +} + +// handlePromptGet builds its record separately from the tool paths, so it needs +// its own case — the rest of the package passes with only this path neutered. +func TestPromptGetResponseTruncatedForStorage(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetMaxResponseSize(1024) + + svc.handleEvent(Event{ + Type: EventTypeActivityPromptGet, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "prompt_name": "big_prompt", + "status": "success", + "response": strings.Repeat("p", 50_000), + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + + assert.Less(t, len(records[0].Response), 50_000, + "prompts take the same per-record cap as tool responses") + assert.True(t, strings.HasSuffix(records[0].Response, "...[truncated]")) + assert.True(t, records[0].ResponseTruncated, + "on a prompt record the flag means the stored copy was cut, as on tool_call") +} + +// An emitter-set response_truncated flag (Spec 103: recorded response larger +// than the one the agent received) must survive a record that storage does NOT +// shorten. The two meanings are OR-ed, so neither may clear the other. +func TestEmitterTruncationFlagSurvivesUntruncatedRecord(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetMaxResponseSize(1024) + + svc.handleEvent(Event{ + Type: EventTypeActivityToolCallCompleted, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "tool_name": "small_tool", + "status": "success", + "response": "short", + "response_truncated": true, + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + + assert.Equal(t, "short", records[0].Response, "a short response is stored verbatim") + assert.True(t, records[0].ResponseTruncated, + "storage truncation must not clear a flag the emitter set") +} + +// Guards the default: an operator who never sets activity_max_response_size +// still gets the documented 64KB bound rather than unbounded growth. +func TestDefaultResponseCapAppliesWithoutConfig(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + + svc.handleEvent(Event{ + Type: EventTypeActivityToolCallCompleted, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "tool_name": "huge_tool", + "status": "success", + "response": strings.Repeat("z", DefaultActivityMaxResponseSize*3), + }, + }) + + records := allActivities(t, store) + require.Len(t, records, 1) + + assert.LessOrEqual(t, len(records[0].Response), + DefaultActivityMaxResponseSize+len("...[truncated]"), + "the 64KB default must bound a record with no explicit config") + assert.True(t, records[0].ResponseTruncated) +} + +// SetMaxResponseSize takes the same "ignore non-positive" shape as the other +// retention setters, so a zero-valued config cannot silently disable the cap. +func TestSetMaxResponseSizeIgnoresNonPositive(t *testing.T) { + svc := NewActivityService(nil, zap.NewNop()) + require.Equal(t, DefaultActivityMaxResponseSize, svc.maxResponseSize) + + svc.SetMaxResponseSize(0) + assert.Equal(t, DefaultActivityMaxResponseSize, svc.maxResponseSize, + "an unset config value must leave the default in place") + + svc.SetMaxResponseSize(-1) + assert.Equal(t, DefaultActivityMaxResponseSize, svc.maxResponseSize) + + svc.SetMaxResponseSize(2048) + assert.Equal(t, 2048, svc.maxResponseSize) +} + +// Storage truncation must not narrow the Spec 026 scan window. +// +// The detector has its own, far larger cap (sensitive_data_detection. +// max_payload_size_kb, 1024KB by default). If the response were cut to +// activity_max_response_size (64KB) before the scan, every secret past that +// boundary would silently stop being detected — a 16x smaller window in the +// DEFAULT configuration, with nothing recording that it had moved. Both +// handlers therefore scan the text as received and store the cut copy. +// +// Each case puts the key AFTER the storage cut, so it is absent from the +// persisted response and can only be found via the pre-truncation string. +func TestDetectionScansUntruncatedResponse(t *testing.T) { + const awsKey = "AKIA1234567890ABCDEF" + // Comfortably past the 64KB default cap, well inside the detector's 1024KB. + padding := strings.Repeat("x", storage.DefaultMaxResponseSize+10_000) + + t.Run("tool_call", func(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetDetector(security.NewDetector(config.DefaultSensitiveDataDetectionConfig())) + + svc.handleEvent(Event{ + Type: EventTypeActivityToolCallCompleted, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "tool_name": "dump_config", + "status": "success", + "response": padding + " trailing secret " + awsKey, + }, + }) + + det := waitForDetectionMetadata(t, store, true) + assert.Equal(t, true, det["detected"], + "a secret past the storage cut must still be detected") + + records := allActivities(t, store) + require.Len(t, records, 1) + assert.NotContains(t, records[0].Response, awsKey, + "the stored copy is still truncated — the scan is what sees the whole text") + }) + + t.Run("prompt_get", func(t *testing.T) { + store, cleanup := setupTestStorage(t) + defer cleanup() + + svc := NewActivityService(store, zap.NewNop()) + svc.SetDetector(security.NewDetector(config.DefaultSensitiveDataDetectionConfig())) + + svc.handleEvent(Event{ + Type: EventTypeActivityPromptGet, + Timestamp: time.Now().UTC(), + Payload: map[string]any{ + "server_name": "upstream", + "prompt_name": "render_context", + "status": "success", + "response": padding + " trailing secret " + awsKey, + }, + }) + + det := waitForDetectionMetadata(t, store, true) + assert.Equal(t, true, det["detected"]) + + records := allActivities(t, store) + require.Len(t, records, 1) + assert.NotContains(t, records[0].Response, awsKey) + }) +} diff --git a/internal/runtime/activity_service.go b/internal/runtime/activity_service.go index 264baeed8..02264ec4b 100644 --- a/internal/runtime/activity_service.go +++ b/internal/runtime/activity_service.go @@ -23,6 +23,10 @@ const ( DefaultRetentionCheckInterval = 1 * time.Hour // DefaultRetentionMaxSizeBytes is the default total activity-log size cap (256MB) DefaultRetentionMaxSizeBytes int64 = 256 * 1024 * 1024 + // DefaultActivityMaxResponseSize is the default per-record response cap (64KB). + // Aliased here because NewActivityService's first parameter shadows the + // storage package inside that function body. + DefaultActivityMaxResponseSize = storage.DefaultMaxResponseSize ) // SensitiveDataEventEmitter provides the ability to emit sensitive data detection events. @@ -103,6 +107,11 @@ type ActivityService struct { maxSizeBytes int64 // total activity-log size cap in bytes (0 = disabled) checkInterval time.Duration + // maxResponseSize bounds the response text persisted on a single activity + // record (activity_max_response_size). Applied to every record type on the + // write path, so one huge upstream payload cannot outweigh the whole log. + maxResponseSize int + // Sensitive data detector (Spec 026) detector *security.Detector @@ -128,8 +137,10 @@ func NewActivityService(storage *storage.Manager, logger *zap.Logger) *ActivityS maxRecords: DefaultRetentionMaxRecords, maxSizeBytes: DefaultRetentionMaxSizeBytes, checkInterval: DefaultRetentionCheckInterval, - detector: nil, // Detector is optional, set via SetDetector - usage: newUsageStore(), + + maxResponseSize: DefaultActivityMaxResponseSize, + detector: nil, // Detector is optional, set via SetDetector + usage: newUsageStore(), } s.usagePersistIntervalNs.Store(int64(DefaultUsagePersistInterval)) return s @@ -219,6 +230,35 @@ func (s *ActivityService) SetRetentionConfig(maxAge time.Duration, maxRecords in } } +// SetMaxResponseSize sets the per-record response size cap in bytes. +// Values <= 0 are ignored, leaving the current cap in place; truncateForStorage +// falls back to storage.DefaultMaxResponseSize if it is ever unset. +func (s *ActivityService) SetMaxResponseSize(maxResponseSize int) { + if maxResponseSize > 0 { + s.maxResponseSize = maxResponseSize + } +} + +// truncateForStorage caps the response text persisted on an activity record. +// +// This is the only thing bounding a single record's size: retention prunes the +// log by age, count and total bytes, but nothing else limits one record, so a +// multi-megabyte upstream response was previously stored whole. +// +// The returned bool is OR-ed into ActivityRecord.ResponseTruncated for tool_call +// and prompt_get, where the field means "cut on the way into the log, ResponseBytes +// still honest" — the same direction as a storage cut. +// +// It is deliberately NOT applied to internal_tool_call. There the flag carries the +// Spec 103 meaning "the agent received LESS than ResponseBytes", which two cost +// consumers read as "exclude this row from delivered traffic" +// (truncatedBuiltinOverstatesDelivery and bench/replaycorpus). A built-in cut only +// for storage was still delivered whole, so setting the flag would silently erase +// it from the usage timeline. The "...[truncated]" suffix still marks the text. +func (s *ActivityService) truncateForStorage(response string) (string, bool) { + return storage.TruncateActivityResponse(response, s.maxResponseSize) +} + // Start begins listening for activity events and persisting them. // It should be called as a goroutine: go svc.Start(ctx, runtime) func (s *ActivityService) Start(ctx context.Context, rt *Runtime) { @@ -553,6 +593,15 @@ func (s *ActivityService) handleToolCallCompleted(evt Event) { arguments := getMapPayload(evt.Payload, "arguments") response := getStringPayload(evt.Payload, "response") responseTruncated := getBoolPayload(evt.Payload, "response_truncated") + // Spec 026 must keep scanning the response the agent actually received, not + // the shortened copy that lands on the record. Truncating first would narrow + // the detector's window from sensitive_data_detection.max_payload_size_kb + // (1024KB by default) to activity_max_response_size (64KB), so a secret past + // the storage cut would stop being detected with nothing to say the window + // had moved. The detector applies its own, much larger cap to this string. + detectionSource := response + response, storageTruncated := s.truncateForStorage(response) + responseTruncated = responseTruncated || storageTruncated durationMs := getInt64Payload(evt.Payload, "duration_ms") // Extract intent metadata if present (Spec 018) @@ -666,7 +715,7 @@ func (s *ActivityService) handleToolCallCompleted(evt Event) { // detection_text (feature off, non-call_tool_* paths) keeps today's // behavior byte-for-byte. if s.detector != nil { - scanText := response + scanText := detectionSource if detectionText != "" { scanText = detectionText } @@ -857,6 +906,14 @@ func (s *ActivityService) handleInternalToolCall(evt Event) { // flag onto the record so a cost recomputation can exclude it instead of // tokenizing text the agent never paid for. responseTruncated := getBoolPayload(evt.Payload, "response_truncated") + // Truncate the stored text but deliberately do NOT touch responseTruncated. + // On an internal record that flag has one specific meaning for cost: + // "the agent received LESS than ResponseBytes, so exclude the row" + // (truncatedBuiltinOverstatesDelivery, and bench/replaycorpus). A built-in + // that was cut only on the way into the log was still delivered whole with + // an honest ResponseBytes, so setting the flag here would drop it from + // delivered traffic. The "...[truncated]" suffix still marks the stored text. + responseStr, _ = s.truncateForStorage(responseStr) // Spec 103: pre-truncation sizes, emitted for built-ins as well as upstream // dispatches. 0 stays UNKNOWN rather than free. @@ -972,21 +1029,27 @@ func (s *ActivityService) handlePromptGet(evt Event) { // Name the MCP client on the record so it survives session eviction. metadata := s.withClientInfo(nil, sessionID) + // Same as the tool path: scan before the storage cut, or the detector's + // window shrinks to activity_max_response_size. + detectionSource := responseStr + responseStr, promptTruncated := s.truncateForStorage(responseStr) + record := &storage.ActivityRecord{ - Type: storage.ActivityTypePromptGet, - Source: storage.ActivitySourceMCP, - ServerName: serverName, - ToolName: promptName, - Arguments: arguments, - Response: responseStr, - Status: status, - ErrorMessage: errorMsg, - DurationMs: durationMs, - Timestamp: evt.Timestamp, - SessionID: sessionID, - WorkSessionID: s.resolveWorkSession(sessionID), - RequestID: requestID, - Metadata: metadata, + Type: storage.ActivityTypePromptGet, + Source: storage.ActivitySourceMCP, + ServerName: serverName, + ToolName: promptName, + Arguments: arguments, + Response: responseStr, + ResponseTruncated: promptTruncated, + Status: status, + ErrorMessage: errorMsg, + DurationMs: durationMs, + Timestamp: evt.Timestamp, + SessionID: sessionID, + WorkSessionID: s.resolveWorkSession(sessionID), + RequestID: requestID, + Metadata: metadata, } // Server-edition identity, mirroring the tool path. @@ -1017,7 +1080,7 @@ func (s *ActivityService) handlePromptGet(evt Event) { s.workersWG.Add(1) go func() { defer s.workersWG.Done() - s.runAsyncDetection(record.ID, arguments, responseStr) + s.runAsyncDetection(record.ID, arguments, detectionSource) }() } } diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index f0d3b5fa7..4299d7dd5 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -327,7 +327,9 @@ func New(cfg *config.Config, cfgPath string, logger *zap.Logger) (*Runtime, erro // value (>= 0 is applied; -1 would mean "unchanged"). maxSizeBytes := int64(cfg.ActivityMaxSizeMB) * 1024 * 1024 activityService.SetRetentionConfig(maxAge, cfg.ActivityMaxRecords, checkInterval, maxSizeBytes) + activityService.SetMaxResponseSize(cfg.ActivityMaxResponseSize) logger.Info("Activity retention config applied", + zap.Int("max_response_size", cfg.ActivityMaxResponseSize), zap.Int("retention_days", cfg.ActivityRetentionDays), zap.Int("max_records", cfg.ActivityMaxRecords), zap.Int("max_size_mb", cfg.ActivityMaxSizeMB),