Live context-window usage for the status bar (the original's get_context_usage). Best-effort — any failure degrades to just the protocol version so the client never hangs or crashes.
(self)
| 988 | return str(sp) |
| 989 | |
| 990 | def _context_usage(self) -> dict: |
| 991 | """Live context-window usage for the status bar (the original's |
| 992 | get_context_usage). Best-effort — any failure degrades to just the |
| 993 | protocol version so the client never hangs or crashes.""" |
| 994 | out: dict = {"protocol_version": PROTOCOL_VERSION} |
| 995 | try: |
| 996 | from src.context_system.context_analyzer import analyze_context |
| 997 | |
| 998 | model = getattr(self.provider, "model", None) or self.config.model or "" |
| 999 | messages = ( |
| 1000 | self.session.conversation.get_messages() if self.session is not None else [] |
| 1001 | ) |
| 1002 | data = analyze_context( |
| 1003 | conversation_api_messages=messages, |
| 1004 | model=model, |
| 1005 | system_prompt=self._system_prompt_text(), |
| 1006 | tool_schemas=_tool_schemas(self.tool_registry), |
| 1007 | claude_md_content="", |
| 1008 | ) |
| 1009 | out.update({ |
| 1010 | "total_tokens": data.total_tokens, |
| 1011 | "max_tokens": data.max_tokens, |
| 1012 | "percentage": round(data.percentage, 1), |
| 1013 | "categories": [ |
| 1014 | {"name": c.name, "tokens": c.tokens} |
| 1015 | for c in data.categories |
| 1016 | if not c.is_deferred and c.name != "Free space" |
| 1017 | ], |
| 1018 | }) |
| 1019 | except Exception as exc: # noqa: BLE001 — never let a usage pull break the session |
| 1020 | out["error"] = str(exc) |
| 1021 | return out |
| 1022 | |
| 1023 | async def _do_compact(self, request_id: object, instructions: object) -> None: |
| 1024 | """Manually compact the conversation (the original's /compact). Idle-only: |
no test coverage detected