fix(agent): refine status output and CLI rendering

Keep status output responsive while estimating current context from session history, dropping low-value queue/subagent counters, and marking command-style replies for plain-text rendering in CLI. Also route direct CLI calls through outbound metadata so help/status formatting stays explicit instead of relying on content heuristics.

Made-with: Cursor
This commit is contained in:
Xubin Ren
2026-03-21 15:52:10 +00:00
parent 4d1897609d
commit e430b1daf5
4 changed files with 142 additions and 24 deletions
+32 -8
View File
@@ -189,7 +189,6 @@ class AgentLoop:
"""Build a human-readable runtime status snapshot."""
history = session.get_history(max_messages=0)
msg_count = len(history)
active_subs = self.subagents.get_running_count()
uptime_s = int(time.time() - self._start_time)
uptime = (
@@ -201,7 +200,13 @@ class AgentLoop:
last_in = self._last_usage.get("prompt_tokens", 0)
last_out = self._last_usage.get("completion_tokens", 0)
ctx_used = last_in
ctx_used = 0
try:
ctx_used, _ = self.memory_consolidator.estimate_session_prompt_tokens(session)
except Exception:
ctx_used = 0
if ctx_used <= 0:
ctx_used = last_in
ctx_total_tokens = max(self.context_window_tokens, 0)
ctx_pct = int((ctx_used / ctx_total_tokens) * 100) if ctx_total_tokens > 0 else 0
ctx_used_str = f"{ctx_used // 1000}k" if ctx_used >= 1000 else str(ctx_used)
@@ -213,8 +218,6 @@ class AgentLoop:
f"📊 Tokens: {last_in} in / {last_out} out",
f"📚 Context: {ctx_used_str}/{ctx_total_str} ({ctx_pct}%)",
f"💬 Session: {msg_count} messages",
f"👾 Subagents: {active_subs} active",
f"🪢 Queue: {self.bus.inbound.qsize()} pending",
f"⏱ Uptime: {uptime}",
])
@@ -224,6 +227,7 @@ class AgentLoop:
channel=msg.channel,
chat_id=msg.chat_id,
content=self._build_status_content(session),
metadata={"render_as": "text"},
)
async def _run_agent_loop(
@@ -475,7 +479,10 @@ class AgentLoop:
"/help — Show available commands",
]
return OutboundMessage(
channel=msg.channel, chat_id=msg.chat_id, content="\n".join(lines),
channel=msg.channel,
chat_id=msg.chat_id,
content="\n".join(lines),
metadata={"render_as": "text"},
)
await self.memory_consolidator.maybe_consolidate_by_tokens(session)
@@ -600,6 +607,19 @@ class AgentLoop:
session.messages.append(entry)
session.updated_at = datetime.now()
async def process_direct_outbound(
self,
content: str,
session_key: str = "cli:direct",
channel: str = "cli",
chat_id: str = "direct",
on_progress: Callable[[str], Awaitable[None]] | None = None,
) -> OutboundMessage | None:
"""Process a message directly and return the outbound payload."""
await self._connect_mcp()
msg = InboundMessage(channel=channel, sender_id="user", chat_id=chat_id, content=content)
return await self._process_message(msg, session_key=session_key, on_progress=on_progress)
async def process_direct(
self,
content: str,
@@ -609,7 +629,11 @@ class AgentLoop:
on_progress: Callable[[str], Awaitable[None]] | None = None,
) -> str:
"""Process a message directly (for CLI or cron usage)."""
await self._connect_mcp()
msg = InboundMessage(channel=channel, sender_id="user", chat_id=chat_id, content=content)
response = await self._process_message(msg, session_key=session_key, on_progress=on_progress)
response = await self.process_direct_outbound(
content,
session_key=session_key,
channel=channel,
chat_id=chat_id,
on_progress=on_progress,
)
return response.content if response else ""