Skip to content

Commit 91a459c

Browse files
committed
refactor(source/cloud-storage): move max-read constant into source package
DefaultMaxReadBytes doesn't belong in errors.go — the limit is a source-side invariant, not an error-classification concern. The sentinel ErrReadSizeLimitExceeded stays in cloudstoragecommon because the classifier still needs to recognize it.
1 parent f5fdac8 commit 91a459c

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

internal/sources/cloudstorage/cloudstorage.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import (
3232

3333
const SourceType string = "cloud-storage"
3434

35+
// defaultMaxReadBytes caps the payload ReadObject will return per call,
36+
// protecting the server from OOM and keeping LLM contexts manageable. Objects
37+
// or ranges exceeding this are rejected with ErrReadSizeLimitExceeded.
38+
const defaultMaxReadBytes int64 = 8 << 20 // 8 MiB
39+
3540
// validate interface
3641
var _ sources.SourceConfig = Config{}
3742

@@ -136,18 +141,19 @@ func (s *Source) ListObjects(ctx context.Context, bucket, prefix, delimiter stri
136141
// offset and length follow storage.ObjectHandle.NewRangeReader semantics:
137142
// length == -1 means "read to end of object"; a negative offset means "suffix
138143
// from end" (in which case length must be -1). Reads larger than
139-
// cloudstoragecommon.DefaultMaxReadBytes are rejected with
140-
// ErrReadSizeLimitExceeded so the caller can narrow the range.
144+
// defaultMaxReadBytes are rejected with
145+
// cloudstoragecommon.ErrReadSizeLimitExceeded so the caller can narrow the
146+
// range.
141147
func (s *Source) ReadObject(ctx context.Context, bucket, object string, offset, length int64) (map[string]any, error) {
142148
reader, err := s.Client.Bucket(bucket).Object(object).NewRangeReader(ctx, offset, length)
143149
if err != nil {
144150
return nil, fmt.Errorf("failed to open object %q in bucket %q: %w", object, bucket, err)
145151
}
146152
defer reader.Close()
147153

148-
if remain := reader.Remain(); remain > cloudstoragecommon.DefaultMaxReadBytes {
154+
if remain := reader.Remain(); remain > defaultMaxReadBytes {
149155
return nil, fmt.Errorf("object %q: %d bytes exceeds %d byte limit: %w",
150-
object, remain, cloudstoragecommon.DefaultMaxReadBytes,
156+
object, remain, defaultMaxReadBytes,
151157
cloudstoragecommon.ErrReadSizeLimitExceeded)
152158
}
153159

internal/tools/cloudstorage/cloudstoragecommon/errors.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
// Package cloudstoragecommon holds helpers shared across the Cloud Storage
16-
// tool implementations, including error classification and read-size limits.
16+
// tool implementations, chiefly error classification.
1717
package cloudstoragecommon
1818

1919
import (
@@ -26,14 +26,10 @@ import (
2626
"google.golang.org/api/googleapi"
2727
)
2828

29-
// DefaultMaxReadBytes is the default cap for ReadObject payloads. Objects (or
30-
// ranges) larger than this are rejected with ErrReadSizeLimitExceeded so they
31-
// can't OOM the server or blow an LLM's context window.
32-
const DefaultMaxReadBytes int64 = 8 << 20 // 8 MiB
33-
34-
// ErrReadSizeLimitExceeded is returned by the source when an object/range would
35-
// exceed the configured byte limit. ProcessGCSError maps this to an Agent
36-
// error because the LLM can fix the call by narrowing the 'range' parameter.
29+
// ErrReadSizeLimitExceeded is returned by the source when an object/range
30+
// would exceed the source's configured byte limit. ProcessGCSError maps this
31+
// to an Agent error because the LLM can fix the call by narrowing the 'range'
32+
// parameter.
3733
var ErrReadSizeLimitExceeded = errors.New("cloud storage read size limit exceeded")
3834

3935
// ProcessGCSError classifies an error from the Cloud Storage Go client into

0 commit comments

Comments
 (0)