File size: 14,934 Bytes
81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0e5fdb5 81e5fe7 0e5fdb5 81e5fe7 0e5fdb5 81e5fe7 0721bb4 81e5fe7 0e5fdb5 81e5fe7 0721bb4 81e5fe7 0e5fdb5 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 49b0848 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0e5fdb5 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 f873f92 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 0721bb4 81e5fe7 | 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | """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:
- `check_data` β structured data sources (DB + tabular). No `source_id`
β list sources (id, name, type, table count); with a
`source_id` β that source's tables/columns (one row per
column, metadata only β exposes `pii_flag`, never
sample values).
- `check_knowledge` β the user's unstructured sources / documents (id, name,
type).
- `retrieve_data` β 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_knowledge` β 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.middlewares.logging import get_logger
from src.query.executor.dispatcher import ExecutorDispatcher
from src.query.ir.models import QueryIR
from src.query.ir.repair import IRRepairer
from src.query.ir.validator import IRValidationError, IRValidator
from src.retrieval.base import RetrievalResult
from src.tools.contracts import ToolOutput
logger = get_logger("ir_repair")
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(
{"check_data", "check_knowledge", "retrieve_data", "retrieve_knowledge"}
)
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,
ir_repairer: IRRepairer | None = None,
dispatcher_factory: DispatcherFactory | None = None,
document_retriever: Retriever | None = None,
) -> None:
self._user_id = user_id
self._reader = catalog_reader
# retrieve_data 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._repairer = ir_repairer or IRRepairer()
self._dispatcher_factory: DispatcherFactory = (
dispatcher_factory or ExecutorDispatcher
)
# retrieve_knowledge 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 == "check_data":
return await self._check_data(args)
if tool_name == "check_knowledge":
return await self._check_knowledge()
if tool_name == "retrieve_data":
return await self._retrieve_data(args)
if tool_name == "retrieve_knowledge":
return await self._retrieve_knowledge(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 _check_data(self, args: dict[str, Any]) -> ToolOutput:
"""Inspect the user's structured data sources (DB + tabular).
No `source_id` β an overview: one row per structured source (id, name,
type, table count). With a `source_id` β that source's schema: one row
per column across its tables.
Pattern A note: schema is catalog metadata only β never returns row
data or PII sample values (only the `pii_flag` boolean per column).
Unstructured documents are covered by `check_knowledge`.
"""
structured = await self._reader.read(self._user_id, "structured")
source_id = args.get("source_id")
if not source_id:
rows = [
[s.source_id, s.name, s.source_type, len(s.tables)]
for s in structured.sources
]
return ToolOutput(
tool="check_data",
kind="table",
columns=["source_id", "name", "source_type", "table_count"],
rows=rows,
meta={"source_count": len(structured.sources)},
)
source = next(
(s for s in structured.sources if s.source_id == source_id), None
)
if source is None:
return ToolOutput(
tool="check_data",
kind="error",
error=f"structured source {source_id!r} not found",
)
rows = [
[
t.table_id,
t.name,
# dedorch catalogs mark an uncounted table as -1; surface None so
# the planner prompt never sees a nonsensical "-1 rows".
t.row_count if (t.row_count or 0) >= 0 else None,
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="check_data",
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 _check_knowledge(self) -> ToolOutput:
"""List the user's unstructured sources (documents).
Documents have no column schema to drill into, so there is no
`source_id` mode β reading document content is `retrieve_knowledge`'s
job.
"""
unstructured = await self._reader.read(self._user_id, "unstructured")
rows = [[s.source_id, s.name, s.source_type] for s in unstructured.sources]
return ToolOutput(
tool="check_knowledge",
kind="table",
columns=["source_id", "name", "source_type"],
rows=rows,
meta={"source_count": len(unstructured.sources)},
)
async def _retrieve_data(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="retrieve_data", 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="retrieve_data", kind="error", error=f"invalid IR: {exc}"
)
catalog = await self._reader.read(self._user_id, "structured")
# Repair near-miss ids (an LLM-mangled catalog id) before validating, so a
# direct retrieve_data call is as resilient as the planner path.
ir, repairs = self._repairer.repair(ir, catalog)
for r in repairs:
logger.info(
"repaired ir id", where=r.where, from_id=r.from_id, to_id=r.to_id
)
try:
self._validator.validate(ir, catalog)
except IRValidationError as exc:
return ToolOutput(
tool="retrieve_data",
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="retrieve_data", 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="retrieve_data",
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,
# Executed query for traceability (KM-691); None if unavailable.
"query": result.query,
},
)
async def _retrieve_knowledge(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_knowledge`
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_knowledge",
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_knowledge",
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
|