From a773dc5fe420f42c2770e2990759e7b2573e475e Mon Sep 17 00:00:00 2001 From: Anton Gulin Date: Tue, 1 Sep 2026 19:51:03 -0700 Subject: [PATCH 1/2] fix(activity): apply activity_max_response_size on the write path activity_max_response_size was declared in config with a 64KB default and never read. truncateResponse and its exported wrapper TruncateActivityResponse had no production caller either, so nothing bounded a single activity record: the full response text was persisted for every record type. Retention prunes the log by age, count and total size, but a single proxied upstream response could still be many megabytes. On one real deployment this produced 82 records over 200KB in activity_records, the largest 10.75MB, or 160x the configured cap, with nothing logged to suggest a problem. Wire the existing truncation into ActivityService: - carry maxResponseSize on the service, defaulting to the documented 64KB - SetMaxResponseSize, matching the "ignore non-positive" shape of the other retention setters so an unset config cannot silently disable the cap - apply it in handleToolCallCompleted, handleInternalToolCall and handlePromptGet, which are the three sites that build a record - feed it cfg.ActivityMaxResponseSize from runtime.New The truncation result is OR-ed into ResponseTruncated rather than assigned. That field already carries the Spec 103 meaning "the recorded response is larger than the one the agent received", which is the opposite direction, so neither meaning may clear the other. handlePromptGet did not set the field at all and now does. Tests drive the real event path and assert on what reaches storage. Verified they fail with the truncation call neutered and pass with it in place. Fixes #1173 Co-Authored-By: Claude Opus 5 --- .../activity_response_truncation_test.go | 162 ++++++++++++++++++ internal/runtime/activity_service.go | 74 ++++++-- internal/runtime/runtime.go | 2 + 3 files changed, 222 insertions(+), 16 deletions(-) create mode 100644 internal/runtime/activity_response_truncation_test.go diff --git a/internal/runtime/activity_response_truncation_test.go b/internal/runtime/activity_response_truncation_test.go new file mode 100644 index 000000000..0ca05bee4 --- /dev/null +++ b/internal/runtime/activity_response_truncation_test.go @@ -0,0 +1,162 @@ +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/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. +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), + }, + }) + + 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, records[0].ResponseTruncated) +} + +// 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) +} diff --git a/internal/runtime/activity_service.go b/internal/runtime/activity_service.go index 264baeed8..4aae9d4d7 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,30 @@ 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. That field +// already carries the Spec 103 meaning "the recorded response is larger than the +// one the agent received"; storage truncation is the other direction, but the +// field documents itself as "true if response was truncated" and a consumer +// wanting the untruncated text cannot get it in either case. +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 +588,8 @@ func (s *ActivityService) handleToolCallCompleted(evt Event) { arguments := getMapPayload(evt.Payload, "arguments") response := getStringPayload(evt.Payload, "response") responseTruncated := getBoolPayload(evt.Payload, "response_truncated") + response, storageTruncated := s.truncateForStorage(response) + responseTruncated = responseTruncated || storageTruncated durationMs := getInt64Payload(evt.Payload, "duration_ms") // Extract intent metadata if present (Spec 018) @@ -857,6 +894,8 @@ 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") + responseStr, storageTruncated := s.truncateForStorage(responseStr) + responseTruncated = responseTruncated || storageTruncated // Spec 103: pre-truncation sizes, emitted for built-ins as well as upstream // dispatches. 0 stays UNKNOWN rather than free. @@ -972,21 +1011,24 @@ func (s *ActivityService) handlePromptGet(evt Event) { // Name the MCP client on the record so it survives session eviction. metadata := s.withClientInfo(nil, sessionID) + 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. 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), From 69014875032237356d84dc33c66282514f95534a Mon Sep 17 00:00:00 2001 From: Anton Gulin Date: Thu, 3 Sep 2026 18:05:05 -0700 Subject: [PATCH 2/2] fix(activity): scan before truncating, and keep the built-in cost flag honest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two defects in the review of #1174, both in the wiring rather than the truncation itself. 1. Storage truncation ran before the Spec 026 sensitive-data scan. handleToolCallCompleted reassigned response, and handlePromptGet reassigned responseStr, before the detector read them. The detector applies its own cap (sensitive_data_detection.max_payload_size_kb, 1024KB by default), so the effect was to shrink the scan window to activity_max_response_size — 64KB in the default configuration, a 16x reduction — and nothing recorded that it had moved. A secret past the storage cut simply stopped being detected. Both handlers now capture the response as received and pass that to runAsyncDetection, while still storing the cut copy. detection_text continues to win where the TOON seam supplies it (spec 084 FR-007b). 2. OR-ing storage truncation into ResponseTruncated broke delivered bytes. On an internal_tool_call that flag means "the agent received LESS than ResponseBytes", and two consumers read it as "exclude this row from delivered traffic": truncatedBuiltinOverstatesDelivery, which feeds the usage timeline and is rebuilt from persisted records on cold start, and bench/replaycorpus/flags.go. A built-in cut only on the way into the log was delivered whole with an honest ResponseBytes, so the flag erased it — reachable in default config for describe_tool, and for retrieve_tools/code_execution whenever activity_max_response_size < tool_response_limit. handleInternalToolCall now truncates the stored text without touching the flag. The "...[truncated]" suffix still marks the text. tool_call and prompt_get keep the OR, where the meaning already matches. A dedicated storage_truncated field would be cleaner but reaches contracts, swagger, CSV export and three UI badges; left for a follow-up. Tests: - TestDetectionScansUntruncatedResponse places an AWS key past the 64KB boundary under the default cap and asserts it is still detected, on both the tool and prompt paths, while absent from the stored response - TestStorageTruncatedBuiltinStillCountsDeliveredBytes drives an 80,000-byte describe_tool through the event path and asserts 80,000 delivered bytes, mirroring TestUsageAggregate_TruncatedBuiltinDoesNotInflateDeliveredBytes from the other side - TestInternalToolCallResponseTruncatedForStorage now asserts the flag stays false, which is the corrected behaviour - TestPromptGetResponseTruncatedForStorage covers the prompt path, which had no test Both fixes negative-controlled: restoring either defect fails the new tests and nothing else. docs/features/activity-log.md now states what the setting enforces, which record types it covers, and that detection is unaffected. Related #1173 Co-Authored-By: Claude Opus 5 --- docs/features/activity-log.md | 2 +- .../activity_response_truncation_test.go | 154 +++++++++++++++++- internal/runtime/activity_service.go | 39 ++++- 3 files changed, 184 insertions(+), 11 deletions(-) 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 index 0ca05bee4..de78a9e10 100644 --- a/internal/runtime/activity_response_truncation_test.go +++ b/internal/runtime/activity_response_truncation_test.go @@ -9,6 +9,8 @@ import ( "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" ) @@ -61,6 +63,13 @@ func TestToolCallResponseTruncatedForStorage(t *testing.T) { // 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() @@ -75,6 +84,7 @@ func TestInternalToolCallResponseTruncatedForStorage(t *testing.T) { "internal_tool_name": "call_tool_read", "status": "success", "response": strings.Repeat("y", 50_000), + "response_bytes": int64(50_000), }, }) @@ -83,7 +93,78 @@ func TestInternalToolCallResponseTruncatedForStorage(t *testing.T) { assert.Less(t, len(records[0].Response), 50_000, "internal tool calls take the same cap as upstream calls") - assert.True(t, records[0].ResponseTruncated) + 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 @@ -160,3 +241,74 @@ func TestSetMaxResponseSizeIgnoresNonPositive(t *testing.T) { 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 4aae9d4d7..02264ec4b 100644 --- a/internal/runtime/activity_service.go +++ b/internal/runtime/activity_service.go @@ -245,11 +245,16 @@ func (s *ActivityService) SetMaxResponseSize(maxResponseSize int) { // 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. That field -// already carries the Spec 103 meaning "the recorded response is larger than the -// one the agent received"; storage truncation is the other direction, but the -// field documents itself as "true if response was truncated" and a consumer -// wanting the untruncated text cannot get it in either case. +// 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) } @@ -588,6 +593,13 @@ 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") @@ -703,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 } @@ -894,8 +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") - responseStr, storageTruncated := s.truncateForStorage(responseStr) - responseTruncated = responseTruncated || storageTruncated + // 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. @@ -1011,6 +1029,9 @@ 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{ @@ -1059,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) }() } }