Primera fase context

This commit is contained in:
Jordan Diaz
2026-04-09 18:27:36 +00:00
parent 993e7d3000
commit 4c73d848bb
8 changed files with 424 additions and 40 deletions

View File

@@ -47,25 +47,41 @@ class ContextCompactor:
# ------------------------------------------------------------------
def compact_sections(
self, sections: list[ContextSection]
) -> list[ContextSection]:
self,
sections: list[ContextSection],
max_tokens: int | None = None,
) -> tuple[list[ContextSection], dict[str, Any]]:
"""Remove redundancy and trim low-priority sections to fit budget."""
budget = max_tokens if max_tokens is not None else self.max_tokens
original_count = len(sections)
# 1. Deduplicate identical content across sections
sections = self._deduplicate(sections)
duplicates_removed = original_count - len(sections)
# 2. Estimate tokens per section
for s in sections:
s.token_estimate = estimate_tokens(s.content)
total = sum(s.token_estimate for s in sections)
if total <= self.max_tokens:
return sections
meta = {
"budget_tokens": budget,
"input_tokens": total,
"output_tokens": total,
"sections_input": original_count,
"sections_output": len(sections),
"duplicates_removed": duplicates_removed,
"sections_compacted": 0,
"sections_removed": 0,
}
if total <= budget:
return sections, meta
# 3. Sort by priority (highest first) — immutable_rules never trimmed
sections.sort(key=lambda s: s.priority, reverse=True)
# 4. Progressively trim lowest-priority sections
while total > self.max_tokens and sections:
while total > budget and sections:
lowest = sections[-1]
if lowest.section_type == ContextSectionType.IMMUTABLE_RULES:
break # Never trim rules
@@ -77,12 +93,16 @@ class ContextCompactor:
lowest.content = compacted
lowest.token_estimate = new_estimate
total -= saved
meta["sections_compacted"] += 1
else:
# Remove the section entirely
total -= lowest.token_estimate
sections.pop()
meta["sections_removed"] += 1
return sections
meta["output_tokens"] = total
meta["sections_output"] = len(sections)
return sections, meta
def summarize_tool_output(
self,