Preserve native Codex history and add configurable cross-model compression
Server CI and Windows package / verify (push) Successful in 4m47s
Server CI and Windows package / windows-package (push) Successful in 8m47s

Separate encrypted reasoning from actual compaction, stop migration after native return, and retain full history and recent exchanges. Add typed settings, Gemini fallback, verified provider windows, bounded summaries and durable concurrent checkpoints.

Validation: 245 bridge tests plus focused concurrency tests; 71 settings/connection tests; 66 supplementary tests; TypeScript checks; renderer and Electron bundles; real primary/native/fallback summary probes and 700K input acceptance.
This commit is contained in:
yilsem committed 2026-10-13 03:36:42 +03:00
1 parent 1c6e3656b8
commit 91ecad8e5c
27 files changed
+1119 -117

No files matched your search

@@ -11,9 +11,10 @@ import threading
from collections import OrderedDict
import ozet_kopru
import compression_settings
PREFIXES = {"custom_tool_call": "ctc", "custom_tool_call_output": "ctco",
"function_call": "fc", "function_call_output": "fc"}
"function_call": "fc", "function_call_output": "fco"}
RATIO = 0.70
@@ -30,8 +31,13 @@ def tokens(value):
return (len(encoded(value).encode("utf-8")) + 1) // 2
def input_budget(req, window):
return min(int(window * RATIO), window - max(1024, int(req.get("max_output_tokens") or 0)))
def input_budget(req, window, settings=None, automatic=False):
limit = int(window * RATIO)
if settings is not None:
limit = (int(window * settings['targetPercent'] / 100) if automatic else
settings['maxTokens'] if settings['budgetMode'] == 'tokens' else
int(window * settings['transferPercent'] / 100))
return min(limit, window - max(1024, int(req.get("max_output_tokens") or 0)))
def foreign(req):
@@ -83,13 +89,45 @@ def normalize(req):
return out
def portable_reasoning(req):
"""Project reasoning onto its visible text for a cross-provider request.
Encrypted reasoning is provider execution state, not a conversation
compaction. Never ask another model to interpret that ciphertext. Messages,
tool pairs and actual compaction items remain intact; callers must still
render opaque compaction with its owner. Native-only requests bypass this
projection, and the saved transcript is never modified.
"""
out = copy.deepcopy(req)
items = out.get("input")
if not isinstance(items, list):
return out
result = []
for item in items:
if not isinstance(item, dict) or item.get("type") != "reasoning":
result.append(item)
continue
visible = []
for field in ("summary", "content"):
for part in item.get(field) or []:
if isinstance(part, dict) and isinstance(part.get("text"), str) and part["text"]:
visible.append(part["text"])
if visible:
# Keep this on the assistant side: a synthetic user message would
# change split_history's count of protected user turns.
result.append({"type": "message", "role": "assistant",
"content": [{"type": "output_text", "text": "\n".join(visible)}]})
out["input"] = result
return out
def split_history(items, keep_users=2):
"""Keep the last two user turns and complete tool pairs; never split a call.
System/developer items are kept verbatim regardless of their position.
"""
users = [i for i, x in enumerate(items) if isinstance(x, dict) and x.get("role") == "user"]
cut = users[-keep_users] if len(users) >= keep_users else (users[-1] if users else len(items))
cut = users[-keep_users] if len(users) >= keep_users else (users[0] if users else len(items))
# Some clients put a user/hook message between a call and its result.
while cut:
tail_calls = {x.get("call_id") for x in items[cut:] if isinstance(x, dict) and x.get("call_id")}
@@ -113,36 +151,45 @@ class Preparer:
self.cache = OrderedDict()
self.lock = threading.Lock()
def prepare(self, request, window, summarize, scope="", force=False):
def prepare(self, request, window, summarize, scope="", force=False, settings=None,
automatic=False, checkpoints=None):
"""Summarize old history to fit 70%, leaving the original untouched.
summarize(text, max_tokens) must return a completed semantic summary or
raise. Tool execution is forbidden for that separate model request.
Cache keys include the caller scope, exact history and available budget.
"""
req = normalize(request)
req = portable_reasoning(normalize(request))
window = int(window)
if window < 1024:
raise TransferError("Hedef modelin bağlam kapasitesi doğrulanamadı.")
limit = input_budget(req, window)
limit = input_budget(req, window, settings, automatic)
keep = settings['keepTurns'] if settings else 2
protected, old, recent = split_history(req.get("input") or [], keep)
original_old = old
saved = checkpoints.get(scope, old, settings) if checkpoints and old else None
if saved:
old = [ozet_kopru._ozet_mesaji(saved[1])] + old[saved[0]:]
req = dict(req, input=protected + old + recent)
if not force and tokens(req) <= limit:
return req
protected, old, recent = split_history(req.get("input") or [])
base = dict(req, input=protected + recent)
available = limit - tokens(base) - 256
# Preserve the last user turn even if keeping two turns is too expensive.
if available < 1024 or not old:
if settings is None and (available < 1024 or not old):
protected, old, recent = split_history(req.get("input") or [], keep_users=1)
base = dict(req, input=protected + recent)
available = limit - tokens(base) - 256
if available < 512 or not old:
raise TransferError("Son mesaj ve zorunlu talimatlar hedefe sığmıyor; geçmiş korundu, modele eksik içerik gönderilmedi.")
# Encrypted items must be rendered by their owner, never summarized as ciphertext.
# Actual compaction/unknown opaque items still require owner rendering.
# Reasoning execution state has already been projected to visible text.
if any(isinstance(x, dict) and x.get("encrypted_content") for x in old):
raise TransferError("Şifreli geçmiş önce sahibi tarafından okunabilir özete çevrilmeli.")
source = encoded(old)
budget = min(12000, max(512, available // 2))
key = hashlib.sha256((scope + str(budget) + source).encode()).hexdigest()
factor = compression_settings.LEVELS[settings['level']] if settings else .5
budget = max(512, int(available * factor))
key = hashlib.sha256((scope + str(budget) + encoded(settings) + source).encode()).hexdigest()
with self.lock:
summary = self.cache.get(key)
if summary is None:
@@ -152,6 +199,8 @@ class Preparer:
candidate = dict(req, input=protected + [ozet_kopru._ozet_mesaji(summary)] + recent)
if tokens(candidate) > limit:
raise TransferError("Hazırlanan özet hedef bütçesine sığmadı; eski geçmiş korundu.")
if checkpoints:
checkpoints.save(scope, original_old, settings, summary)
with self.lock:
self.cache[key] = summary
self.cache.move_to_end(key)