This commit is contained in:
Timothy Jaeryang Baek committed 2026-06-11 12:02:00 +01:00
1 parent ad97cdac8f
commit 2b32568c37
7 files changed
+262 -4

No files matched your search

+80 -4
View File
@@ -11,7 +11,9 @@ import logging
import uuid
from pathlib import Path
from cptr.env import CHAT_MAX_ITERATIONS
from cptr.env import CHAT_MAX_ITERATIONS, CHAT_TOOL_MAX_CHARS
from cptr.utils.context import should_compact
from cptr.utils.summarize import summarize_messages
from cptr.models import Chat, ChatMessage, Config
from cptr.socket.main import emit_to_user
from cptr.utils.ai import (
@@ -398,12 +400,19 @@ async def generate_chat_title(
# ── Message history ─────────────────────────────────────────
async def _load_message_history(chat_id: str, message_id: str) -> list[dict]:
async def _load_message_history(
chat_id: str, message_id: str
) -> tuple[list[dict], str | None]:
"""Load the ancestor chain from message_id to root as LLM messages.
Walks up via parent_id so only the active branch is included.
The current message (message_id) is always included even if done=False,
since it may contain completed tool calls from prior approval rounds.
If any message in the chain has a chat_summary, everything before it
is skipped and the summary is returned separately for the system prompt.
Returns (messages, chat_summary_or_None).
"""
all_msgs = await ChatMessage.get_all_by_chat(chat_id)
msg_map = {m.id: m for m in all_msgs}
@@ -416,6 +425,14 @@ async def _load_message_history(chat_id: str, message_id: str) -> list[dict]:
cur = msg_map.get(cur.parent_id) if cur.parent_id else None
chain.reverse() # root → leaf
# Find the most recent message with a chat_summary
existing_summary = None
for i, m in enumerate(chain):
if m.chat_summary:
chain = chain[i:] # keep this message and everything after
existing_summary = m.chat_summary
break
result = []
for m in chain:
# Skip in-progress assistant placeholders, but NOT the current
@@ -511,11 +528,16 @@ async def _load_message_history(chat_id: str, message_id: str) -> list[dict]:
}
result.append(entry)
return result
return result, existing_summary
def _append_tool_to_messages(messages: list[dict], event: dict, result: str, provider: str):
"""Append a tool call + result to the message history for the next API call."""
# Guard against oversized tool outputs
if len(result) > CHAT_TOOL_MAX_CHARS:
half = CHAT_TOOL_MAX_CHARS // 2
result = result[:half] + "\n\n...(truncated)...\n\n" + result[-half:]
# Add assistant message with tool_call
messages.append(
{
@@ -543,6 +565,30 @@ def _append_tool_to_messages(messages: list[dict], event: dict, result: str, pro
)
def _find_safe_split(messages: list[dict], target_keep: int) -> int:
"""Find a safe split index that doesn't break tool call pairs.
Returns the index where keep_zone starts. Ensures:
- Never splits between an assistant tool_call and its tool result
- keep_zone doesn't start with a tool result message
- At least 2 messages are kept
"""
n = len(messages)
split = max(2, n - target_keep)
# Walk forward from the initial split to find a safe boundary
while split < n - 1:
msg = messages[split]
# Don't start keep_zone with a tool result — it needs its preceding assistant
if msg.get("role") == "tool":
split += 1
continue
break
return min(split, n - 2) # always keep at least 2
# ── Connection resolution ───────────────────────────────────
@@ -651,7 +697,9 @@ async def run_chat_task(
base_url = connection.get("base_url") or _default_base_url(provider)
system = _load_system_prompt(workspace)
messages = await _load_message_history(chat_id, message_id)
messages, loaded_summary = await _load_message_history(chat_id, message_id)
if loaded_summary:
system += f"\n\n[CONVERSATION SUMMARY]\n{loaded_summary}"
if regeneration_prompt:
messages.append({"role": "user", "content": regeneration_prompt})
tools = get_tool_list()
@@ -679,6 +727,34 @@ async def run_chat_task(
approval_mode = "full" if chat_params["auto_approve_tools"] else "auto"
for _iteration in range(CHAT_MAX_ITERATIONS):
# ── Context compaction: summarize older messages if too large ──
if should_compact(messages, system):
target_keep = max(2, len(messages) * 2 // 5)
split_idx = _find_safe_split(messages, target_keep)
drop_zone = messages[:split_idx]
keep_zone = messages[split_idx:]
api_type = connection.get("api_type", "chat_completions")
summary = await summarize_messages(
drop_zone, loaded_summary,
provider, base_url, api_key, model,
api_type=api_type,
)
# Store on the current message — this IS the cutoff
await ChatMessage.update(message_id, chat_summary=summary)
loaded_summary = summary
# Append summary to system prompt (works for all providers)
system = _load_system_prompt(workspace)
system += f"\n\n[CONVERSATION SUMMARY]\n{summary}"
messages = keep_zone
logger.info(
"[task %s] compacted: dropped %d msgs, kept %d, summary=%d chars",
message_id[:8], len(drop_zone), len(keep_zone), len(summary),
)
form_data = ChatCompletionForm(
model=model,
messages=messages,