mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-15 22:33:05 -07:00
fix: add back global string limit gracefully returning a valid json string
This commit is contained in:
@@ -124,6 +124,23 @@ describe('truncateForTelemetry', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should enforce a global payload string limit without breaking JSON', () => {
|
||||
const obj = {
|
||||
a: 'x'.repeat(100),
|
||||
b: 'y'.repeat(100),
|
||||
};
|
||||
// Capping global string length to 50
|
||||
const result = truncateForTelemetry(obj, 100, 100, 4, 50) as string;
|
||||
|
||||
// It should replace the entire object with a valid JSON string indicating truncation
|
||||
expect(result).toBe(
|
||||
'"[TRUNCATED: Payload exceeded global limit of 50 characters. Original length: 215]"',
|
||||
);
|
||||
|
||||
// Prove it remains perfectly parseable JSON
|
||||
expect(() => JSON.parse(result)).not.toThrow();
|
||||
});
|
||||
|
||||
it('should stringify objects unchanged if within maxLength', () => {
|
||||
const obj = { a: 1 };
|
||||
expect(truncateForTelemetry(obj, 100)).toBe(JSON.stringify(obj));
|
||||
|
||||
@@ -67,6 +67,7 @@ export function truncateForTelemetry(
|
||||
maxStringLength = 10000,
|
||||
maxArrayLength = 100,
|
||||
maxDepth = 4,
|
||||
maxGlobalStringLength = 50000,
|
||||
): AttributeValue | undefined {
|
||||
const truncateObj = (v: unknown, depth: number): unknown => {
|
||||
if (typeof v === 'string') {
|
||||
@@ -149,6 +150,13 @@ export function truncateForTelemetry(
|
||||
|
||||
const stringified = safeJsonStringify(truncated);
|
||||
|
||||
if (stringified.length > maxGlobalStringLength) {
|
||||
const graphemes = Array.from(stringified);
|
||||
if (graphemes.length > maxGlobalStringLength) {
|
||||
return `"[TRUNCATED: Payload exceeded global limit of ${maxGlobalStringLength} characters. Original length: ${graphemes.length}]"`;
|
||||
}
|
||||
}
|
||||
|
||||
return stringified as AttributeValue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user