fix(rag): decode jsonb metadata returned as a string by asyncpg

VectorStore.search / list_docs called dict(row["metadata"]), but asyncpg
returns jsonb as a JSON *string* (no codec on the pool), so dict() iterated
characters and raised 'dictionary update sequence element #0 has length 1; 2 is
required' — making every KB search fail at the row-mapping step once migration
030 let the query reach rows (it was masked before by the missing content
column). Add _as_dict(): json.loads a string, pass dicts through, null/non-object
-> {}. Caught by live end-to-end verification on the NAS.
This commit is contained in:
Renn F
2026-06-15 05:56:15 +02:00
parent 6422f77bb9
commit dfd0d1f188
2 changed files with 52 additions and 2 deletions
@@ -0,0 +1,34 @@
"""VectorStore must decode jsonb metadata returned by asyncpg as a string.
asyncpg returns ``jsonb`` columns as a JSON *string* (no codec on the pool), so
``dict(value)`` iterated characters and raised "dictionary update sequence
element #0 has length 1; 2 is required" — which made every KB search fail at the
row-mapping step once the schema (migration 030) let the query reach rows.
"""
from __future__ import annotations
from roboco.services.optimal_brain.vector_store import _as_dict
def test_decodes_jsonb_string() -> None:
# The bug case: asyncpg hands back a JSON string, not a dict.
assert _as_dict('{"agent": "be-dev-1", "type": "journal"}') == {
"agent": "be-dev-1",
"type": "journal",
}
def test_passes_through_mapping() -> None:
assert _as_dict({"k": "v"}) == {"k": "v"}
def test_none_and_empty_become_empty_dict() -> None:
assert _as_dict(None) == {}
assert _as_dict("") == {}
def test_non_object_json_becomes_empty_dict() -> None:
# A bare JSON scalar/array is not a metadata mapping.
assert _as_dict("[1, 2, 3]") == {}
assert _as_dict('"just a string"') == {}