File size: 13,446 Bytes
37b8384 148e33b 37b8384 148e33b 37b8384 148e33b 1195870 148e33b 90e80f9 148e33b 37b8384 148e33b 37b8384 148e33b 1195870 148e33b 37b8384 148e33b 1195870 148e33b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | """DataAccessToolInvoker β the data-access tool family (KM-465 / KM-630).
Implements the `ToolInvoker` Protocol (src/agents/slow_path/invoker.py) for the
data-access family. Unlike the stateless `AnalyticsToolInvoker`, these tools
read the user's catalog / sources, so the invoker is constructed per-request
with the authenticated `user_id` and its dependencies (dependency injection β
the runtime/Coordinator supplies them; INV-7 keeps the agent layer
tool-agnostic).
Tools implemented here:
- `list_sources` β the user's data sources (id, name, type, table count).
- `describe_source` β tables/columns of one source (one row per column,
metadata only β exposes `pii_flag`, never sample values).
- `query_structured` β runs a pre-built `QueryIR` (validate -> dispatch ->
execute, skipping the planner) and returns rows as
`ToolOutput(kind="table")` β the Pattern A handoff the
`analyze_*` tools consume.
- `retrieve_documents` β dense retrieval over unstructured sources, returns
`ToolOutput(kind="documents")`.
Frozen guarantee (Β§8.4): **never throws.** Any failure returns
`ToolOutput(kind="error", error=...)`.
"""
from __future__ import annotations
from collections.abc import Callable
from decimal import Decimal
from typing import Any, Protocol
from pydantic import ValidationError
from src.catalog.models import Catalog
from src.catalog.reader import CatalogReader
from src.query.executor.dispatcher import ExecutorDispatcher
from src.query.ir.models import QueryIR
from src.query.ir.validator import IRValidationError, IRValidator
from src.retrieval.base import RetrievalResult
from src.tools.contracts import ToolOutput
DispatcherFactory = Callable[[Catalog], ExecutorDispatcher]
# Canonical set of data-access tool names β the single source of truth for which
# tools this invoker serves. `CompositeToolInvoker` imports it to route by name;
# the planner registry should derive its data-access spec names from it (agent ->
# tool is the correct dependency direction). Defining it once here means
# adding/renaming a data-access tool can't silently drift the router out of sync
# from the registry (R11). Must match the names in `DataAccessToolInvoker.invoke`.
DATA_ACCESS_TOOLS: frozenset[str] = frozenset(
{"list_sources", "describe_source", "query_structured", "retrieve_documents"}
)
class Retriever(Protocol):
"""Minimal interface this invoker needs from the retrieval layer."""
async def retrieve(
self, query: str, user_id: str, k: int = 5
) -> list[RetrievalResult]: ...
class DataAccessToolInvoker:
"""Never-throwing invoker for catalog-introspection tools (implements ToolInvoker)."""
def __init__(
self,
user_id: str,
catalog_reader: CatalogReader,
*,
ir_validator: IRValidator | None = None,
dispatcher_factory: DispatcherFactory | None = None,
document_retriever: Retriever | None = None,
) -> None:
self._user_id = user_id
self._reader = catalog_reader
# query_structured deps β injectable so tests need no real LLM/DB. The
# validator is stateless; the dispatcher is built per-call from the
# request's catalog (executors are picked by source_type).
self._validator = ir_validator or IRValidator()
self._dispatcher_factory: DispatcherFactory = (
dispatcher_factory or ExecutorDispatcher
)
# retrieve_documents dep β the module singleton by default, injectable
# for tests (the real one pulls PGVector + Redis). Lazy-imported on first
# use so importing this module stays cheap.
self._retriever = document_retriever
async def invoke(self, tool_name: str, args: dict[str, Any]) -> ToolOutput:
try:
if tool_name == "list_sources":
return await self._list_sources()
if tool_name == "describe_source":
return await self._describe_source(args)
if tool_name == "query_structured":
return await self._query_structured(args)
if tool_name == "retrieve_documents":
return await self._retrieve_documents(args)
return ToolOutput(
tool=tool_name, kind="error", error=f"unknown tool {tool_name!r}"
)
except Exception as exc: # noqa: BLE001 β never-throw seam (Β§8.4)
return ToolOutput(
tool=tool_name, kind="error", error=f"{type(exc).__name__}: {exc}"
)
async def _list_sources(self) -> ToolOutput:
"""List the user's data sources (structured + unstructured)."""
structured = await self._reader.read(self._user_id, "structured")
unstructured = await self._reader.read(self._user_id, "unstructured")
sources = list(structured.sources) + list(unstructured.sources)
rows = [
[s.source_id, s.name, s.source_type, len(s.tables)] for s in sources
]
return ToolOutput(
tool="list_sources",
kind="table",
columns=["source_id", "name", "source_type", "table_count"],
rows=rows,
meta={"source_count": len(sources)},
)
async def _describe_source(self, args: dict[str, Any]) -> ToolOutput:
"""Describe one source: one row per column across its tables.
Pattern A note: this is catalog metadata only β never returns row
data or PII sample values (only the `pii_flag` boolean per column).
"""
source_id = args.get("source_id")
if not source_id:
return ToolOutput(
tool="describe_source",
kind="error",
error="missing 'source_id' argument",
)
structured = await self._reader.read(self._user_id, "structured")
unstructured = await self._reader.read(self._user_id, "unstructured")
sources = list(structured.sources) + list(unstructured.sources)
source = next((s for s in sources if s.source_id == source_id), None)
if source is None:
return ToolOutput(
tool="describe_source",
kind="error",
error=f"source {source_id!r} not found",
)
rows = [
[
t.table_id,
t.name,
t.row_count,
c.column_id,
c.name,
c.data_type,
c.nullable,
c.pii_flag,
]
for t in source.tables
for c in t.columns
]
return ToolOutput(
tool="describe_source",
kind="table",
columns=[
"table_id",
"table_name",
"table_row_count",
"column_id",
"column_name",
"data_type",
"nullable",
"pii_flag",
],
rows=rows,
meta={
"source_id": source.source_id,
"source_name": source.name,
"source_type": source.source_type,
"table_count": len(source.tables),
"column_count": len(rows),
},
)
async def _query_structured(self, args: dict[str, Any]) -> ToolOutput:
"""Run one validated, single-table QueryIR and return rows as a table.
This is the spine of the slow path (Pattern A): the `analyze_*` tools
take this output as their `data` arg. We receive an already-built `ir`
from the Planner (never SQL, never an NL question), so we skip the
planner and run validate -> dispatch -> execute directly (the tail of
QueryService.run). Output is `kind="table"` with `columns` + `rows`
(rows are list[list], converted from the executor's list[dict]).
"""
raw = args.get("ir")
if raw is None:
return ToolOutput(
tool="query_structured", kind="error", error="missing 'ir' argument"
)
try:
ir = raw if isinstance(raw, QueryIR) else QueryIR.model_validate(raw)
except ValidationError as exc:
return ToolOutput(
tool="query_structured", kind="error", error=f"invalid IR: {exc}"
)
catalog = await self._reader.read(self._user_id, "structured")
try:
self._validator.validate(ir, catalog)
except IRValidationError as exc:
return ToolOutput(
tool="query_structured",
kind="error",
error=f"IR validation failed: {exc}",
)
dispatcher = self._dispatcher_factory(catalog)
executor = dispatcher.pick(ir)
result = await executor.run(ir)
if result.error:
return ToolOutput(
tool="query_structured", kind="error", error=result.error
)
# QueryResult.rows is list[dict]; ToolOutput.rows is list[list] ordered
# by `columns` so downstream materialization is positional. DB NUMERIC
# columns arrive as `Decimal` (asyncpg) β coerce to float here so the
# output is JSON-serializable (SSE / analysis_record persistence) and
# plays nicely with the float math in the analyze_* tools.
rows = [
[_json_safe(row.get(c)) for c in result.columns] for row in result.rows
]
return ToolOutput(
tool="query_structured",
kind="table",
columns=result.columns,
rows=rows,
meta={
"source_id": result.source_id,
"source_name": result.source_name,
"table_id": result.table_id,
"table_name": result.table_name,
"backend": result.backend,
"row_count": result.row_count,
"truncated": result.truncated,
"elapsed_ms": result.elapsed_ms,
},
)
async def _retrieve_documents(self, args: dict[str, Any]) -> ToolOutput:
"""Dense-retrieve relevant chunks from the user's unstructured sources.
Pulls qualitative context (PDF/DOCX/TXT) for a natural-language `query`
via the retrieval router. `top_k` caps the number of chunks; optional
`source_id` scopes to one source (best-effort metadata filter β the
router itself does not yet scope by source, so this prunes the results).
TODO(retrieval scoping): the Planner few-shot has no `retrieve_documents`
example, so `source_id` is rarely emitted today and this post-filter is
adequate. If source-scoped retrieval becomes common, push scoping down
into RetrievalRouter.retrieve()/DocumentRetriever (WHERE
cmetadata->>'source_id' = :source_id) and drop this post-filter β more
correct than pruning an already-top_k'd unscoped result set.
"""
query = args.get("query")
if not isinstance(query, str) or not query.strip():
return ToolOutput(
tool="retrieve_documents",
kind="error",
error="missing 'query' argument",
)
try:
top_k = int(args.get("top_k", 5))
except (TypeError, ValueError):
top_k = 5
source_id = args.get("source_id")
retriever = self._retriever
if retriever is None:
from src.retrieval.router import retrieval_router
retriever = retrieval_router
results = await retriever.retrieve(query, self._user_id, top_k)
if source_id:
results = [r for r in results if _result_source_id(r) == source_id]
documents = [
{
"content": r.content,
"score": r.score,
"source_type": r.source_type,
"metadata": r.metadata,
}
for r in results
]
return ToolOutput(
tool="retrieve_documents",
kind="documents",
value=documents,
meta={
"count": len(documents),
"query": query,
"top_k": top_k,
"source_id": source_id,
},
)
def _json_safe(value: Any) -> Any:
"""Coerce DB scalar types that JSON can't represent into plain Python.
DB drivers return NUMERIC/DECIMAL as `decimal.Decimal`, which is neither
JSON-serializable nor mixable with `float` math. Convert those to `float`;
everything else passes through unchanged.
"""
if isinstance(value, Decimal):
return float(value)
return value
def _result_source_id(result: RetrievalResult) -> str | None:
"""Best-effort extraction of a source_id from a retrieval result's metadata.
The chunk metadata schema is owned by the Go ingestion service; the key may
live at the top level or nested under "data". Returns None if absent.
"""
meta = result.metadata or {}
top = meta.get("source_id")
if isinstance(top, str):
return top
data = meta.get("data")
if isinstance(data, dict):
nested = data.get("source_id")
if isinstance(nested, str):
return nested
return None
|