Skip to content

The agent that grows with you

Merge simp/r3-33 (late tail) into hermes/simplify-codebase

Tekniumd38bb1b3aa02
diff --cc tools/mcp_tool_handlers.py
index af16b4ed650,f1c519bca38..a799aa8b8f1
--- a/tools/mcp_tool_handlers.py
+++ b/tools/mcp_tool_handlers.py
@@@ -338,11 -295,9 +311,10 @@@ def _render_content_blocks(result, serv
rendered = _cache_mcp_image_block(block) or _cache_mcp_audio_block(block) or _render_mcp_resource_block(block, server_name)
if rendered:
parts.append(rendered)
+ usable_parts += 1
continue
- # Benign empty renders log at debug; warn only for unknown shapes.
block_type = getattr(block, "type", None) or type(block).__name__
- if block_type in {"text", "resource", "audio", "image"}:
+ if block_type in {"text", "resource", "audio", "image"}: # benign empty render
logger.debug("MCP %s: content block type %r rendered empty", server_name, block_type)
else:
logger.warning("MCP %s: dropping unsupported content block type %r", server_name, block_type)
@@@ -359,29 -316,14 +331,28 @@@ def _capped_structured_content(result)
structured = mcp_field(result, "structured_content", "structuredContent")
try:
as_json = json.dumps(structured, ensure_ascii=False, default=str) if structured is not None else ""
- if len(as_json) > _MCP_HARD_RESULT_CAP_CHARS:
- structured = _truncate_mcp_text_result(as_json)
except (TypeError, ValueError):
- pass
+ return structured
+ return _truncate_mcp_text_result(as_json) if len(as_json) > _MCP_HARD_RESULT_CAP_CHARS else structured
+
+
+def _render_call_tool_result(result, server_name: str) -> str:
+ """Pure: ``CallToolResult`` -> handler JSON. ``content`` and ``structuredContent`` are
+ ALTERNATIVES, never both forwarded (kimi-code#3234): spec-following servers already render
+ their data into content, so forwarding both sent it twice. content wins whenever it rendered
+ anything usable (no richness heuristic is attempted — none is reliable); structuredContent
+ fills in only when the blocks rendered effectively empty, keeping structuredContent-only
+ servers working. ``_meta`` minus reserved keys is always surfaced."""
+ if mcp_field(result, "is_error", "isError", False):
+ return tool_error(_sanitize_error(_truncate_mcp_text_result(_error_result_text(result) or "MCP tool returned an error")))
+ text_result, usable_parts = _render_content_blocks(result, server_name)
+ structured = _capped_structured_content(result)
meta = _strip_reserved_meta_keys(mcp_field(result, "meta", "meta"))
+ if structured is not None and usable_parts > 0:
+ structured = None # drop notices do not count as usable content
if structured is None and meta is None:
return json.dumps({"result": text_result}, ensure_ascii=False)
- # Key order is part of the output: "result" leads when there is text, otherwise "_meta"
- # precedes the (empty) "result".
+ # Key order is part of the output: "result" leads when there is text, otherwise "_meta" precedes it.
payload: Dict[str, Any] = {"result": text_result} if text_result else {}
if structured is not None:
payload["structuredContent" if text_result else "result"] = structured
diff --cc tools/session_search_tool.py
index 0eed7517238,03ab134f36a..91313695ce7
--- a/tools/session_search_tool.py
+++ b/tools/session_search_tool.py
@@@ -418,33 -384,22 +383,28 @@@ def _read_with_profile_fallback(db, sid
def _list_recent_sessions(db, limit: int, current_session_id: str = None, link_profile: str = None) -> str:
"""Browse shape: metadata for the most recent sessions (no LLM, no FTS5)."""
def _browse():
- # list_sessions_rich already applies the canonical child classifier (roots,
- # /branch and /new-reset children admitted; delegation/compression children
- # hidden). Re-classifying here re-hid legacy reset children — trust the query.
- sessions = db.list_sessions_rich(
+ # Never use list_sessions_rich(order_by_last_active=True) here: it walks every
+ # compression chain and derives activity/previews before LIMIT, which can
+ # monopolise a gateway callback for minutes on a multi-GB state.db. The
+ # bounded browse query preselects an indexed candidate set and carries a
+ # cooperative SQLite VM cancellation deadline. Fail closed rather than
+ # silently falling back to the whole-database query shape.
+ bounded_list = getattr(db, "list_recent_sessions_bounded", None)
+ if bounded_list is None:
+ raise RuntimeError("session database does not support bounded recent-session browse")
+ sessions = bounded_list(
limit=limit + 15, # extra so we can skip current / compression roots
- exclude_sources=list(_HIDDEN_SESSION_SOURCES), order_by_last_active=True)
+ exclude_sources=list(_HIDDEN_SESSION_SOURCES), timeout_seconds=3.0)
current_root, has_compression_hop = (
_resolve_to_parent(db, current_session_id) if current_session_id else (None, False))
- results = []
- for s in sessions:
- sid = s.get("id", "")
- # Compression continuation: the root was summarised into the live child, so
- # hide it. /new-reset children carry no transcript — keep that root browsable.
- if sid == current_session_id or (has_compression_hop and current_root and sid == current_root):
- continue
- results.append({
- "session_id": sid, "link": _session_link(sid, link_profile), "title": s.get("title") or None,
- **{k: s.get(k, "") for k in ("source", "started_at", "last_active")},
- "message_count": s.get("message_count", 0), "preview": s.get("preview", "")})
- if len(results) >= limit:
- break
+ # Compression continuation: the root was summarised into the live child, so hide
+ # it. /new-reset children carry no transcript — keep that root browsable.
+ hidden = {current_session_id, current_root if has_compression_hop and current_root else None}
+ results = [{
+ "session_id": s.get("id", ""), "link": _session_link(s.get("id", ""), link_profile),
+ "title": s.get("title") or None, **{k: s.get(k, "") for k in ("source", "started_at", "last_active")},
+ "message_count": s.get("message_count", 0), "preview": s.get("preview", "")}
+ for s in [x for x in sessions if x.get("id", "") not in hidden][:limit]]
return _ok(mode="browse", results=results, count=len(results), message=(
f"Showing {len(results)} most recent sessions. Pass a query= to search, "
"or session_id+around_message_id to scroll."))