Skip to content

Commit 913097a

Browse files
authored
fix(test): size TestDetectionScansUntruncatedResponse for the race detector (#1207)
The test added in #1174 failed in both Unit Tests and End-to-End Tests, and the defect is its payload sizing rather than anything it guards. To place a secret past the storage cut it padded the response to storage.DefaultMaxResponseSize+10_000 (~75KB), relying on the 64KB default cap. security.Detector.Scan costs ~5.5us/byte, and the race detector multiplies that by ~24x (measured: 114-137us/byte at 1K-75K). So the scan took ~9.5s against the 5s poll deadline in waitForDetectionMetadata. Because runAsyncDetection only writes metadata on a hit, an over-budget scan surfaces as "timed out waiting for detection metadata" rather than a failed assertion, which is why it read as a logic failure. CI runs -race in both workflows; a plain local run does not. Set the cap explicitly with SetMaxResponseSize(1024) and pad to 4KB instead. The seam is identical - the AWS key sits at bytes [4113, 4133), 3089 bytes past the 1024-byte cut - and truncateResponse honours any positive maxSize with no minimum floor. Runtime drops from ~9.5s to ~0.5s per subtest. Verified: - 5/5 runs under -race, 0.45-0.50s per subtest against the 5s deadline - negative control: moving the detectionSource capture back to after truncateForStorage in both handlers still fails the test at the smaller payload, so it is not passing vacuously - second control: stubbing truncateForStorage to a no-op fails the NotContains assertion, proving the key is genuinely past the cut - whole internal/runtime package green under -race, under the unit-tests.yml invocation and under the e2e-tests.yml -short invocation - headroom measured under CPU oversubscription: still passes at 8x slowdown (2.96-3.76s), breaks only around 10.8x TestDefaultResponseCapAppliesWithoutConfig continues to cover the 64KB default separately, so no coverage is lost.
1 parent 7a27851 commit 913097a

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎internal/runtime/activity_response_truncation_test.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,25 @@ func TestSetMaxResponseSizeIgnoresNonPositive(t *testing.T) {
253253
//
254254
// Each case puts the key AFTER the storage cut, so it is absent from the
255255
// persisted response and can only be found via the pre-truncation string.
256+
//
257+
// The cap is set explicitly rather than left at the 64KB default, which keeps
258+
// the payload small. Detector.Scan costs roughly 5.5us/byte, and the race
259+
// detector multiplies that by ~23x: a payload sized to clear the default cap
260+
// took 9.5s to scan under -race against waitForDetectionMetadata's 5s poll
261+
// deadline, so the test failed in CI (which runs -race everywhere) while
262+
// passing locally without it. A 1KB cap exercises the identical seam.
256263
func TestDetectionScansUntruncatedResponse(t *testing.T) {
257264
const awsKey = "AKIA1234567890ABCDEF"
258-
// Comfortably past the 64KB default cap, well inside the detector's 1024KB.
259-
padding := strings.Repeat("x", storage.DefaultMaxResponseSize+10_000)
265+
const storageCap = 1024
266+
// Past the storage cut, well inside the detector's own 1024KB window.
267+
padding := strings.Repeat("x", storageCap*4)
260268

261269
t.Run("tool_call", func(t *testing.T) {
262270
store, cleanup := setupTestStorage(t)
263271
defer cleanup()
264272

265273
svc := NewActivityService(store, zap.NewNop())
274+
svc.SetMaxResponseSize(storageCap)
266275
svc.SetDetector(security.NewDetector(config.DefaultSensitiveDataDetectionConfig()))
267276

268277
svc.handleEvent(Event{
@@ -291,6 +300,7 @@ func TestDetectionScansUntruncatedResponse(t *testing.T) {
291300
defer cleanup()
292301

293302
svc := NewActivityService(store, zap.NewNop())
303+
svc.SetMaxResponseSize(storageCap)
294304
svc.SetDetector(security.NewDetector(config.DefaultSensitiveDataDetectionConfig()))
295305

296306
svc.handleEvent(Event{

0 commit comments

Comments
 (0)