fix: normalizer preserves compaction summaries, dedupes goal_context, no auto-reset on compacted input
This commit is contained in:
@@ -4310,13 +4310,19 @@ def _antigravity_normalize_context(input_data):
|
|||||||
if os.environ.get("ANTIGRAVITY_AUTO_RESET_POLLUTED_CONTEXT", "1") != "1":
|
if os.environ.get("ANTIGRAVITY_AUTO_RESET_POLLUTED_CONTEXT", "1") != "1":
|
||||||
auto_reset = False
|
auto_reset = False
|
||||||
|
|
||||||
if is_simple and (auto_reset or n_tool_outputs == 0):
|
has_compaction_summary = any(
|
||||||
|
isinstance(it, dict) and it.get("type") == "message" and it.get("role") == "user"
|
||||||
|
and ("Auto-compacted" in str(it.get("content", "")) or "auto-compacted" in str(it.get("content", "")).lower())
|
||||||
|
for it in input_data
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_simple and auto_reset and not has_compaction_summary:
|
||||||
system_items = [it for it in input_data if isinstance(it, dict) and it.get("type") == "message" and it.get("role") in ("developer", "system")]
|
system_items = [it for it in input_data if isinstance(it, dict) and it.get("type") == "message" and it.get("role") in ("developer", "system")]
|
||||||
user_item = input_data[latest_user_idx]
|
user_item = input_data[latest_user_idx]
|
||||||
result = system_items + [user_item] if system_items else [user_item]
|
result = system_items + [user_item] if system_items else [user_item]
|
||||||
print(f"[antigravity-context] raw_items={n_raw} compacted_items={n_raw} final_items={len(result)}", file=sys.stderr)
|
print(f"[antigravity-context] raw_items={n_raw} compacted_items={n_raw} final_items={len(result)}", file=sys.stderr)
|
||||||
print(f"[antigravity-context] raw_tool_outputs={n_tool_outputs} kept_tool_outputs=0", file=sys.stderr)
|
print(f"[antigravity-context] raw_tool_outputs={n_tool_outputs} kept_tool_outputs=0", file=sys.stderr)
|
||||||
print(f"[antigravity-context] simple_latest_user=true auto_reset={auto_reset}", file=sys.stderr)
|
print(f"[antigravity-context] simple_latest_user=true auto_reset={auto_reset} has_compaction={has_compaction_summary}", file=sys.stderr)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
dev_messages = []
|
dev_messages = []
|
||||||
@@ -4357,6 +4363,22 @@ def _antigravity_normalize_context(input_data):
|
|||||||
tail_start = max(0, len(recent_items) - 6)
|
tail_start = max(0, len(recent_items) - 6)
|
||||||
recent_tail = recent_items[tail_start:]
|
recent_tail = recent_items[tail_start:]
|
||||||
|
|
||||||
|
deduped_tail = []
|
||||||
|
seen_goal_context = False
|
||||||
|
for idx, msg_item in recent_tail:
|
||||||
|
content_str = ""
|
||||||
|
c = msg_item.get("content", "")
|
||||||
|
if isinstance(c, str):
|
||||||
|
content_str = c
|
||||||
|
elif isinstance(c, list):
|
||||||
|
content_str = " ".join(p.get("text", p.get("input_text", "")) for p in c if isinstance(p, dict))
|
||||||
|
if "<goal_context>" in content_str:
|
||||||
|
if seen_goal_context:
|
||||||
|
continue
|
||||||
|
seen_goal_context = True
|
||||||
|
deduped_tail.append((idx, msg_item))
|
||||||
|
recent_tail = deduped_tail if deduped_tail else recent_tail
|
||||||
|
|
||||||
tool_call_ids = set()
|
tool_call_ids = set()
|
||||||
for _, t_item in kept_tools:
|
for _, t_item in kept_tools:
|
||||||
cid = t_item.get("call_id", t_item.get("id", ""))
|
cid = t_item.get("call_id", t_item.get("id", ""))
|
||||||
@@ -4371,6 +4393,15 @@ def _antigravity_normalize_context(input_data):
|
|||||||
|
|
||||||
result = list(dev_messages)
|
result = list(dev_messages)
|
||||||
|
|
||||||
|
compaction_summaries = []
|
||||||
|
for idx, msg_item in recent_items:
|
||||||
|
if msg_item is input_data[latest_user_idx]:
|
||||||
|
continue
|
||||||
|
c = msg_item.get("content", "")
|
||||||
|
content_str = c if isinstance(c, str) else " ".join(p.get("text", p.get("input_text", "")) for p in c if isinstance(p, dict)) if isinstance(c, list) else ""
|
||||||
|
if "Auto-compacted" in content_str or "auto-compacted" in content_str.lower():
|
||||||
|
compaction_summaries.append(msg_item)
|
||||||
|
|
||||||
if n_summarized > 0:
|
if n_summarized > 0:
|
||||||
summary_text = f"[Tool history summary: {n_summarized} older tool outputs omitted. {n_tool_calls} prior function calls were made for file inspection/editing.]"
|
summary_text = f"[Tool history summary: {n_summarized} older tool outputs omitted. {n_tool_calls} prior function calls were made for file inspection/editing.]"
|
||||||
result.append({"type": "message", "role": "user", "content": [{"type": "input_text", "text": summary_text}]})
|
result.append({"type": "message", "role": "user", "content": [{"type": "input_text", "text": summary_text}]})
|
||||||
@@ -4381,6 +4412,9 @@ def _antigravity_normalize_context(input_data):
|
|||||||
for _, tool_item in kept_tools:
|
for _, tool_item in kept_tools:
|
||||||
result.append(tool_item)
|
result.append(tool_item)
|
||||||
|
|
||||||
|
for cs_item in compaction_summaries:
|
||||||
|
result.append(cs_item)
|
||||||
|
|
||||||
for _, msg_item in recent_tail:
|
for _, msg_item in recent_tail:
|
||||||
if msg_item is not input_data[latest_user_idx]:
|
if msg_item is not input_data[latest_user_idx]:
|
||||||
result.append(msg_item)
|
result.append(msg_item)
|
||||||
@@ -4408,7 +4442,10 @@ def _antigravity_normalize_context(input_data):
|
|||||||
|
|
||||||
if total_chars > _ANTIGRAVITY_EMERGENCY_CHARS:
|
if total_chars > _ANTIGRAVITY_EMERGENCY_CHARS:
|
||||||
print(f"[antigravity-context] EMERGENCY: {total_chars} chars exceeds limit, resetting to minimal", file=sys.stderr)
|
print(f"[antigravity-context] EMERGENCY: {total_chars} chars exceeds limit, resetting to minimal", file=sys.stderr)
|
||||||
result = list(dev_messages) + [input_data[latest_user_idx]]
|
result = list(dev_messages)
|
||||||
|
if compaction_summaries:
|
||||||
|
result.extend(compaction_summaries)
|
||||||
|
result.append(input_data[latest_user_idx])
|
||||||
total_chars = sum(len(json.dumps(it, ensure_ascii=False)) for it in result)
|
total_chars = sum(len(json.dumps(it, ensure_ascii=False)) for it in result)
|
||||||
|
|
||||||
while len(result) > _ANTIGRAVITY_MAX_CONTENTS and total_chars > _ANTIGRAVITY_SOFT_CHARS:
|
while len(result) > _ANTIGRAVITY_MAX_CONTENTS and total_chars > _ANTIGRAVITY_SOFT_CHARS:
|
||||||
|
|||||||
Reference in New Issue
Block a user