This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-22 13:56:59 +02:00
1 parent a1920d0be1
commit d1abb32ae2
5 files changed
+155 -15

No files matched your search

+14 -2
View File
@@ -11,7 +11,7 @@ import logging
import uuid
from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_MAX_CHARS
from cptr.utils.context import should_compact
from cptr.utils.context import resolve_compact_token_threshold, should_compact
from cptr.utils.skills import discover_skills, load_skill, format_skill_content
from cptr.utils.summarize import summarize_messages
from cptr.models import Chat, ChatMessage, Config
@@ -1149,6 +1149,7 @@ async def run_chat_task(
chat_obj = await Chat.get_by_id(chat_id)
chat_params = (chat_obj.meta or {}).get("params", {}) if chat_obj else {}
configured_model = (msg.model if msg else None) or model
system = await _load_system_prompt(workspace, model, user_id=user_id)
messages, loaded_summary = await _load_message_history(chat_id, message_id)
if loaded_summary:
@@ -1222,17 +1223,28 @@ async def run_chat_task(
chat_request_params = chat_params.get("request_params") or {}
global_rp = {}
model_rp = {}
compact_token_threshold = None
try:
chat_models_config = await Config.get("chat.models") or {}
global_rp = chat_models_config.get("*", {}).get("params", {}).get("request_params", {})
model_rp = chat_models_config.get(model, {}).get("params", {}).get("request_params", {})
compact_token_threshold = resolve_compact_token_threshold(
configured_model, chat_models_config=chat_models_config
)
except Exception:
pass
compact_token_threshold = compact_token_threshold or resolve_compact_token_threshold()
request_params = {**global_rp, **model_rp, **chat_request_params} or None
for _iteration in range(CHAT_MAX_ITERATIONS):
# ── Context compaction: summarize older messages if too large ──
if should_compact(messages, system, last_usage, new_messages_since):
if should_compact(
messages,
system,
last_usage,
new_messages_since,
threshold=compact_token_threshold,
):
target_keep = max(2, len(messages) * 2 // 5)
split_idx = _find_safe_split(messages, target_keep)
drop_zone = messages[:split_idx]
+59 -6
View File
@@ -35,6 +35,7 @@ def should_compact(
system_prompt: str,
last_usage: dict | None = None,
new_messages_since: int = 0,
threshold: int | None = None,
) -> bool:
"""True when estimated tokens exceed the compact token threshold.
@@ -43,7 +44,7 @@ def should_compact(
estimates the new messages appended since that call.
Falls back to full estimation when no usage data exists.
"""
threshold = _get_threshold()
resolved_threshold = threshold or _get_threshold()
if last_usage and last_usage.get("input_tokens"):
# Real base from last API call + estimate only new additions
@@ -51,11 +52,11 @@ def should_compact(
if new_messages_since > 0:
new_msgs = messages[-new_messages_since:]
base += estimate_messages_tokens(new_msgs)
return base > threshold
return base > resolved_threshold
# Full estimation fallback
total = estimate_tokens(system_prompt) + estimate_messages_tokens(messages)
return total > threshold
return total > resolved_threshold
def build_context_usage(tokens: int, *, threshold: int | None = None, source: str) -> dict:
@@ -71,11 +72,63 @@ def build_context_usage(tokens: int, *, threshold: int | None = None, source: st
}
def estimate_context_usage(messages: list[dict], system_prompt: str) -> dict:
def estimate_context_usage(
messages: list[dict], system_prompt: str, *, threshold: int | None = None
) -> dict:
"""Return context fullness stats using the same estimate as compaction."""
threshold = _get_threshold()
resolved_threshold = threshold or _get_threshold()
estimated_tokens = estimate_tokens(system_prompt) + estimate_messages_tokens(messages)
return build_context_usage(estimated_tokens, threshold=threshold, source="estimated")
return build_context_usage(estimated_tokens, threshold=resolved_threshold, source="estimated")
def _parse_positive_int(value: object) -> int | None:
try:
parsed = int(value)
except (TypeError, ValueError):
return None
return parsed if parsed > 0 else None
def resolve_compact_token_threshold(
model: str | None = None,
*,
chat_models_config: dict | None = None,
global_cap: int | None = None,
) -> int:
"""Return the effective compaction threshold for a model.
The existing global threshold is the cap. Model-level values can lower it
for smaller context windows, but never raise it.
"""
cap = global_cap or _get_threshold()
model_threshold = None
if chat_models_config:
candidate_keys = []
if model:
candidate_keys.append(model)
if "/" in model:
candidate_keys.append(model.split("/", 1)[1])
candidate_keys.append("*")
for key in candidate_keys:
params = chat_models_config.get(key, {}).get("params", {})
model_threshold = _parse_positive_int(params.get("compact_token_threshold"))
if model_threshold:
break
return min(model_threshold, cap) if model_threshold else cap
async def load_compact_token_threshold(model: str | None = None) -> int:
"""Load the effective compaction threshold from persisted model config."""
try:
from cptr.models import Config
chat_models_config = await Config.get("chat.models") or {}
except Exception:
chat_models_config = {}
return resolve_compact_token_threshold(model, chat_models_config=chat_models_config)
def _get_threshold() -> int: