File size: 4,828 Bytes
8240467 ec22719 8240467 ec22719 8240467 ec22719 8240467 ec22719 8240467 ec22719 8240467 4858143 8240467 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #!/usr/bin/env python3
"""
Adaptive Token-Aware Compression Strategy
Based on research insights:
- Dynamically adjust compression ratio based on message type
- Preserve high-importance messages (errors, file changes)
- Aggressively compress low-importance messages (verbose output)
- Token-budget aware
"""
from __future__ import annotations
import re
from typing import Any
def estimate_tokens(text: str) -> int:
"""Rough token estimate: ~4 chars per token"""
return len(text) // 4
def get_message_importance(msg: dict[str, Any]) -> float:
"""
Score message importance (0-10).
Higher score = keep more content.
"""
content = str(msg.get('content', ''))
# High importance signals
score = 5.0 # baseline
if 'error' in content.lower() or 'exception' in content.lower():
score += 3.0
if 'failed' in content.lower() or 'failure' in content.lower():
score += 2.0
if re.search(r'\btest\b.*\b(?:pass|fail)', content.lower()):
score += 2.0
if re.search(r'\bfile\b.*\b(?:changed|modified|created)', content.lower()):
score += 2.0
if '```' in content: # code blocks
score += 1.5
# Low importance signals
if re.search(r'^\s*(?:ok|done|success|completed)\s*$', content.lower()):
score -= 2.0
if len(content) > 2000 and content.count('\n') > 50: # verbose output
score -= 1.0
return max(0.0, min(10.0, score))
def compress_by_importance(content: str, importance: float, max_tokens: int = 200) -> str:
"""
Compress content based on importance score.
Higher importance = keep more content.
Splits are budgeted so head+tail is always a strict subset of the input,
and the result is never longer than what was passed in.
"""
current_tokens = estimate_tokens(content)
# Scale max_tokens by importance (0.5x to 2x)
importance_factor = 0.5 + (importance / 10.0) * 1.5
target_tokens = int(max_tokens * importance_factor)
if current_tokens <= target_tokens:
return content
lines = content.split('\n')
# Fraction of lines to retain, by importance band. These are total
# budgets (<1.0) so the elision marker always replaces real content.
if importance >= 7.0:
keep_fraction = 0.60
elif importance >= 4.0:
keep_fraction = 0.40
else:
keep_fraction = 0.20
# Nothing to gain from eliding two lines or fewer.
if len(lines) <= 3:
return content
budget = max(2, int(len(lines) * keep_fraction))
if budget >= len(lines) - 1:
budget = len(lines) - 2
keep_start = max(1, (budget * 3) // 4)
keep_end = max(1, budget - keep_start)
head = lines[:keep_start]
tail = lines[len(lines) - keep_end:]
omitted = len(lines) - len(head) - len(tail)
if omitted <= 0:
return content
result = '\n'.join(head + [f'[... {omitted} lines omitted ...]'] + tail)
# Hard guarantee: never return more than we were given.
return result if len(result) < len(content) else content
def compress_messages(
messages: list[Any] | None = None,
path: str | None = None,
metadata: dict[str, Any] | None = None,
) -> list[Any]:
"""
Adaptive compression based on message importance.
"""
del path, metadata
if not isinstance(messages, list):
return []
compressed = []
for msg in messages:
if not isinstance(msg, dict):
compressed.append(msg)
continue
new_msg = msg.copy()
if isinstance(new_msg.get('content'), str):
importance = get_message_importance(new_msg)
content = new_msg['content']
# Apply adaptive compression
compressed_content = compress_by_importance(content, importance)
# Preserve the message even when compression yields nothing.
# Six legacy compressors returned 0 messages for a 1-message input
# with empty or whitespace-only content, and 1 message for a 2-message
# input where the first was empty -- silently deleting a conversation
# turn and changing the transcript structure the model sees. Emitting
# the (empty) message keeps the message count invariant.
new_msg['content'] = compressed_content
compressed.append(new_msg)
else:
compressed.append(new_msg)
return compressed
if __name__ == '__main__':
# Test
test_msg = {
'role': 'assistant',
'content': 'Error: Authentication failed\n' + 'x' * 1000
}
imp = get_message_importance(test_msg)
print(f"Importance: {imp}/10")
result = compress_messages([test_msg])
print(f"Original: {len(test_msg['content'])} chars")
print(f"Compressed: {len(result[0]['content'])} chars")
|