fix compaction in a0_connector plugin

Alessandro committed Apr 14, 2026 at 14:51 UTC 1d8bc2b2c5b1bc211da01f7648ef193e8ec33c3f
2 files changed +27 -4
plugins/_a0_connector/api/v1/compact_chat.py
+11 -4
@@ -57,15 +57,22 @@ class CompactChat(connector_base.ProtectedConnectorApiHandler):
57 return Response("Cannot compact while agent is running", 409)
58
59 stats = await get_compaction_stats(context)
60 - if stats["token_count"] < MIN_COMPACTION_TOKENS:
60 + can_compact = stats["token_count"] >= MIN_COMPACTION_TOKENS
61 +
62 + if action == "stats":
63 + payload: dict = {"ok": can_compact, "stats": stats}
64 + if not can_compact:
65 + payload["message"] = (
66 + f"Not enough content to compact (minimum {MIN_COMPACTION_TOKENS:,} tokens)"
67 + )
68 + return payload
69 +
70 + if not can_compact:
71 return {
72 "ok": False,
73 "message": f"Not enough content to compact (minimum {MIN_COMPACTION_TOKENS:,} tokens)",
74 }
75
66 - if action == "stats":
67 - return {"ok": True, "stats": stats}
68 -
76 if action == "compact":
77 use_chat_model = _coerce_bool(input.get("use_chat_model", True), default=True)
78 preset_name = str(input.get("preset_name", "")).strip() or None
plugins/_a0_connector/api/v1/token_status.py
+16
@@ -37,6 +37,22 @@ class TokenStatus(connector_base.ProtectedConnectorApiHandler):
37 except (TypeError, ValueError):
38 token_count = None
39
40 + # ctx_window is only refreshed inside prepare_prompt (LLM turns). After load or
41 + # between runs it is often missing while history already holds the conversation.
42 + if token_count is None and agent is not None:
43 + try:
44 + from helpers import tokens as tokens_helper
45 + from helpers.history import output_text
46 +
47 + history_output = agent.history.output()
48 + full_text = output_text(
49 + history_output, ai_label="assistant", human_label="user"
50 + )
51 + if full_text.strip():
52 + token_count = tokens_helper.approximate_tokens(full_text)
53 + except Exception:
54 + token_count = None
55 +
56 context_window: int | None = None
57 try:
58 from plugins._model_config.helpers.model_config import get_chat_model_config