Files
aiturk-hermes-ide/apps/desktop/assets/codex-bridge/compression_settings.py
T
yilsem 91ecad8e5c
Server CI and Windows package / verify (push) Successful in 4m47s
Server CI and Windows package / windows-package (push) Successful in 8m47s
Preserve native Codex history and add configurable cross-model compression
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.
2026-10-13 03:36:42 +03:00

126 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Validated snapshots and durable, source-bound context projections.
Only prepared copies live here. Codex owns the transcript and its Chat ID.
"""
import copy
import hashlib
import json
import os
import sqlite3
import threading
from pathlib import Path
from contextlib import contextmanager
DEFAULTS = json.loads(Path(__file__).with_name('compression-defaults.json').read_text(encoding='utf-8'))
LEVELS = {'mini': .85, 'az': .65, 'orta': .45, 'fazla': .3, 'cok-fazla': .15}
def validate(value):
if not isinstance(value, dict) or set(value) != set(DEFAULTS):
raise ValueError('Geçersiz sıkıştırma ayarları')
v = copy.deepcopy(value)
if v['schemaVersion'] != 1 or v['level'] not in LEVELS or v['budgetMode'] not in ('percent', 'tokens'):
raise ValueError('Geçersiz sıkıştırma sürümü veya seviyesi')
for field in ('primaryModel', 'fallbackModel'):
if not isinstance(v[field], str) or len(v[field]) > 256:
raise ValueError('Geçersiz sıkıştırıcı model')
for field in ('currentModelFallback', 'autoEnabled'):
if type(v[field]) is not bool:
raise ValueError('Geçersiz sıkıştırma seçeneği')
for field, low, high in [('keepTurns', 1, 100), ('transferPercent', 10, 90),
('triggerPercent', 20, 90), ('targetPercent', 10, 80),
('maxTokens', 1024, 2000000), ('timeoutSeconds', 1, 3600)]:
if type(v[field]) is not int or not low <= v[field] <= high:
raise ValueError('Geçersiz sıkıştırma sınırı: ' + field)
if v['targetPercent'] >= v['triggerPercent']:
raise ValueError('Sıkıştırma hedefi başlama eşiğinden küçük olmalı')
return v
def read(home):
try:
return validate(json.loads((Path(home) / 'compression-settings.json').read_text(encoding='utf-8')))
except FileNotFoundError:
return copy.deepcopy(DEFAULTS)
def digest(value):
return hashlib.sha256(json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
class Checkpoints:
def __init__(self, home):
self.path = Path(home) / 'compression-checkpoints.sqlite3'
self.lock = threading.RLock()
self.inflight = {}
@contextmanager
def connect(self):
self.path.parent.mkdir(parents=True, exist_ok=True)
db = sqlite3.connect(self.path, timeout=30)
db.execute('CREATE TABLE IF NOT EXISTS versions (id INTEGER PRIMARY KEY, scope TEXT, source_hash TEXT, source_count INTEGER, settings_hash TEXT, summary TEXT, created TEXT DEFAULT CURRENT_TIMESTAMP)')
db.execute('CREATE INDEX IF NOT EXISTS versions_scope ON versions(scope, id)')
db.execute('CREATE TABLE IF NOT EXISTS active (scope TEXT PRIMARY KEY, version INTEGER)')
db.execute('CREATE TABLE IF NOT EXISTS latest (scope TEXT PRIMARY KEY, request_hash TEXT)')
try:
with db:
yield db
finally:
db.close()
def get(self, scope, items, settings):
if not scope:
return None
with self.lock, self.connect() as db:
row = db.execute('SELECT v.source_hash,v.source_count,v.summary FROM versions v JOIN active a ON a.version=v.id WHERE a.scope=? AND v.settings_hash=?', (scope, digest(settings))).fetchone()
if row and len(items) >= row[1] and digest(items[:row[1]]) == row[0]:
return row[1], row[2]
return None
def save(self, scope, items, settings, summary):
if not scope:
return
with self.lock, self.connect() as db:
cursor = db.execute('INSERT INTO versions(scope,source_hash,source_count,settings_hash,summary) VALUES(?,?,?,?,?)', (scope, digest(items), len(items), digest(settings), summary))
# A late summary of an earlier prefix cannot replace a newer one.
old = db.execute('SELECT v.source_count FROM versions v JOIN active a ON a.version=v.id WHERE a.scope=?', (scope,)).fetchone()
latest = db.execute('SELECT request_hash FROM latest WHERE scope=?', (scope,)).fetchone()
current = not latest or latest[0] == digest([items, settings])
if current and (not old or old[0] <= len(items)):
db.execute('INSERT OR REPLACE INTO active(scope,version) VALUES(?,?)', (scope, cursor.lastrowid))
def begin(self, scope, items, settings):
if scope:
with self.lock, self.connect() as db:
db.execute('INSERT OR REPLACE INTO latest(scope,request_hash) VALUES(?,?)',
(scope, digest([items, settings])))
def restore(self, scope, version):
with self.lock, self.connect() as db:
if not db.execute('SELECT id FROM versions WHERE id=? AND scope=?', (version, scope)).fetchone():
raise ValueError('Sıkıştırma sürümü bu sohbete ait değil')
db.execute('INSERT OR REPLACE INTO active(scope,version) VALUES(?,?)', (scope, version))
def coalesce(self, key, run):
with self.lock:
entry = self.inflight.get(key)
owner = entry is None
if owner:
entry = {'event': threading.Event()}
self.inflight[key] = entry
if not owner:
entry['event'].wait()
if 'error' in entry:
raise entry['error']
return copy.deepcopy(entry['result'])
try:
entry['result'] = run()
return copy.deepcopy(entry['result'])
except Exception as error:
entry['error'] = error
raise
finally:
with self.lock:
entry['event'].set()
self.inflight.pop(key, None)