From eec312dcb41f757c43d642c5353b75a37d0756c5 Mon Sep 17 00:00:00 2001 From: Apunkt Date: Wed, 3 Jun 2026 16:50:02 +0200 Subject: [PATCH] feat(http_server): add section headers to _payload_to_text for model parsing The flattened text output now uses markdown headers so the model can distinguish L0 identity from L1 summary from L2 episodes from handles. This replaces undifferentiated newline-separated text with structured sections: ## L0 Identity, ## L1 Recent Summary, ## Rich Club, ## L2 Episode, ## Handles. --- src/iai_mcp/http_server.py | 7 ++++--- tests/test_http_server.py | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/iai_mcp/http_server.py b/src/iai_mcp/http_server.py index 19a72ed..edd9202 100644 --- a/src/iai_mcp/http_server.py +++ b/src/iai_mcp/http_server.py @@ -73,16 +73,17 @@ def _payload_to_text(result: Any) -> str: if not isinstance(result, dict): return str(result) parts: list[str] = [] + headers = {"l0": "## L0 Identity", "l1": "## L1 Recent Summary", "rich_club": "## Rich Club"} for key in ("l0", "l1", "rich_club"): val = result.get(key) if isinstance(val, str) and val.strip(): - parts.append(val.strip()) + parts.append(f"{headers[key]}\n{val.strip()}") l2 = result.get("l2") if isinstance(l2, list): for item in l2: text = item if isinstance(item, str) else json.dumps(item, ensure_ascii=False) if text.strip(): - parts.append(text.strip()) + parts.append(f"## L2 Episode\n{text.strip()}") # Compact handles (populated at minimal; also present at standard/deep). handles = [ result.get(k) @@ -90,7 +91,7 @@ def _payload_to_text(result: Any) -> str: ] handles = [h for h in handles if isinstance(h, str) and h.strip()] if handles: - parts.append(" ".join(handles)) + parts.append(f"## Handles\n{' '.join(handles)}") return "\n\n".join(parts) diff --git a/tests/test_http_server.py b/tests/test_http_server.py index 06da13c..fdb5b02 100644 --- a/tests/test_http_server.py +++ b/tests/test_http_server.py @@ -158,7 +158,8 @@ def test_session_context_text_format(tmp_path, monkeypatch): assert headers["content-type"].startswith("text/plain") # Flattened block keeps the human layers in order, drops token ints. assert body == ( - "pinned identity\n\nrecent summary\n\nhub concepts\n\nepisode one\n\nepisode two" + "## L0 Identity\npinned identity\n\n## L1 Recent Summary\nrecent summary\n\n" + "## Rich Club\nhub concepts\n\n## L2 Episode\nepisode one\n\n## L2 Episode\nepisode two" ) @@ -168,7 +169,7 @@ def test_payload_to_text_renders_handles_at_minimal(): from iai_mcp.http_server import _payload_to_text text = _payload_to_text(_fake_dispatch(None, "minimal_payload", {})) - assert text == " " + assert text == "## Handles\n " def test_rpc_post_passthrough(tmp_path, monkeypatch):