v3.9.2: Aggressive Gemini context compaction for follow-up turns

This commit is contained in:
2026-05-24 19:49:44 +00:00
Unverified
parent 6d4800fe41
commit 438f2eefa5

View File

@@ -4180,6 +4180,30 @@ class Handler(http.server.BaseHTTPRequestHandler):
def _handle_gemini_oauth(self, body, model, stream, tracker=None):
input_data = body.get("input", "")
policy = provider_policy()
if isinstance(input_data, list) and len(input_data) > 8:
n_tool_outputs = sum(1 for it in input_data if isinstance(it, dict) and it.get("type") == "function_call_output")
if n_tool_outputs > 2:
compacted_data = []
last_fc_idx = None
for i in range(len(input_data) - 1, -1, -1):
if isinstance(input_data[i], dict) and input_data[i].get("type") == "function_call":
last_fc_idx = i
break
keep_from = last_fc_idx if last_fc_idx is not None else len(input_data)
for i, item in enumerate(input_data):
if isinstance(item, dict) and item.get("type") == "function_call_output":
o = item.get("output", "")
if i < keep_from and len(o) > 1500:
item = dict(item)
summary = o[:600] + f"\n... [compacted {len(o) - 600} chars - earlier tool result]"
item["output"] = summary
compacted_data.append(item)
input_data = compacted_data
body = dict(body)
body["input"] = input_data
print(f"[gemini-compact] {n_tool_outputs} tool outputs, compacted earlier ones to 600 chars", file=sys.stderr)
if OAUTH_PROVIDER == "google-antigravity":
alias_map = {
"antigravity-gemini-3-flash": "gemini-3-flash",
@@ -4383,7 +4407,16 @@ class Handler(http.server.BaseHTTPRequestHandler):
headers["X-Goog-Api-Client"] = "gl-node/22.17.0"
headers["Client-Metadata"] = "ideType=IDE_UNSPECIFIED,platform=PLATFORM_UNSPECIFIED,pluginType=GEMINI"
body_b = json.dumps(wrapped).encode()
print(f"[{self._session_id}] model={model} stream={stream} items={len(input_data) if isinstance(input_data, list) else 1} project={project_id}", file=sys.stderr)
n_contents = len(contents)
has_tools = bool(gemini_tools)
print(f"[{self._session_id}] model={model} stream={stream} items={len(input_data) if isinstance(input_data, list) else 1} project={project_id} contents={n_contents} tools={has_tools}", file=sys.stderr)
if n_contents > 10:
debug_path = os.path.join(_LOG_DIR, f"gemini-long-ctx-{self._session_id}.json")
try:
with open(debug_path, "w") as dbg:
json.dump({"contents_count": n_contents, "contents_roles": [c.get("role") for c in contents], "has_tools": has_tools, "model": model, "wrapped_size": len(body_b)}, dbg, indent=2)
except Exception:
pass
for ep in endpoints:
target = f"{ep}/{url_suffix}"