fix(kiro): scale description compression by needed size

Compute a size-reduction based keep ratio and use it to trim
tool descriptions, avoiding forced minimum truncation when the
target size already fits. This aligns compression with actual
payload reduction needs and prevents over-compression.
This commit is contained in:
ZqinKing
2026-01-14 16:22:46 +08:00
parent 5b433f962f
commit 83e5f60b8b
@@ -97,9 +97,6 @@ func compressToolDescription(description string, targetLength int) string {
// Find a safe truncation point (UTF-8 boundary) // Find a safe truncation point (UTF-8 boundary)
truncLen := targetLength - 3 // Leave room for "..." truncLen := targetLength - 3 // Leave room for "..."
if truncLen < kirocommon.MinToolDescriptionLength-3 {
truncLen = kirocommon.MinToolDescriptionLength - 3
}
// Ensure we don't cut in the middle of a UTF-8 character // Ensure we don't cut in the middle of a UTF-8 character
for truncLen > 0 && !utf8.RuneStart(description[truncLen]) { for truncLen > 0 && !utf8.RuneStart(description[truncLen]) {
@@ -164,30 +161,27 @@ func compressToolsIfNeeded(tools []KiroToolWrapper) []KiroToolWrapper {
} }
// Step 2: Compress descriptions proportionally // Step 2: Compress descriptions proportionally
// Calculate the compression ratio needed sizeToReduce := float64(sizeAfterSchemaSimplification - kirocommon.ToolCompressionTargetSize)
compressionRatio := float64(kirocommon.ToolCompressionTargetSize) / float64(sizeAfterSchemaSimplification) var totalDescLen float64
if compressionRatio > 1.0 {
compressionRatio = 1.0
}
// Calculate total description length and target
totalDescLen := 0
for _, tool := range compressedTools { for _, tool := range compressedTools {
totalDescLen += len(tool.ToolSpecification.Description) totalDescLen += float64(len(tool.ToolSpecification.Description))
} }
// Estimate how much we need to reduce descriptions if totalDescLen > 0 {
// Assume descriptions account for roughly 50% of the payload // Assume size reduction comes primarily from descriptions.
targetDescRatio := compressionRatio * 0.8 // Be more aggressive with description compression keepRatio := 1.0 - (sizeToReduce / totalDescLen)
if keepRatio > 1.0 {
keepRatio = 1.0
} else if keepRatio < 0 {
keepRatio = 0
}
for i := range compressedTools { for i := range compressedTools {
desc := compressedTools[i].ToolSpecification.Description desc := compressedTools[i].ToolSpecification.Description
targetLen := int(float64(len(desc)) * targetDescRatio) targetLen := int(float64(len(desc)) * keepRatio)
if targetLen < kirocommon.MinToolDescriptionLength {
targetLen = kirocommon.MinToolDescriptionLength
}
compressedTools[i].ToolSpecification.Description = compressToolDescription(desc, targetLen) compressedTools[i].ToolSpecification.Description = compressToolDescription(desc, targetLen)
} }
}
finalSize := calculateToolsSize(compressedTools) finalSize := calculateToolsSize(compressedTools)
log.Infof("kiro: compression complete, original: %d bytes, final: %d bytes (%.1f%% reduction)", log.Infof("kiro: compression complete, original: %d bytes, final: %d bytes (%.1f%% reduction)",