Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions code_locator/tools/validate_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ValidateSymbolsTool:

def __init__(self, db: SymbolDB, config: CodeLocatorConfig) -> None:
self.config = config
# Retained so code_locator.adapter.ground_mappings() can reach
# db.lookup_by_file() during auto-grounding. See adapters/code_locator.py:190.
self._db = db
# Cache symbol list at init (not per-call)
self._symbols: list[tuple[int, str, str]] = db.get_all_symbol_names()

Expand Down
2 changes: 2 additions & 0 deletions contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ class IngestStats(BaseModel):
symbols_mapped: int
regions_linked: int
ungrounded: int
grounded: int = 0
grounded_pct: float = 0.0 # grounded / intents_created, 0.0 when intents_created == 0
grounding_deferred: int = 0 # index not ready at ingest time — re-ingest after build_index


Expand Down
20 changes: 18 additions & 2 deletions handlers/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,32 @@ async def handle_ingest(
source_refs.append(ref)

stats = result.get("stats", {})
intents_created = int(stats.get("intents_created", 0))
ungrounded_count = int(stats.get("ungrounded", 0))
grounded_count = max(intents_created - ungrounded_count, 0)
grounded_pct = (grounded_count / intents_created) if intents_created > 0 else 0.0

logger.info(
"[ingest] complete: %d/%d grounded (%.0f%%) | deferred=%d | source_refs=%s",
grounded_count,
intents_created,
grounded_pct * 100.0,
grounding_deferred,
source_refs,
)

return IngestResponse(
ingested=bool(result.get("ingested", False)),
repo=str(result.get("repo", repo)),
query=str(payload.get("query", "")),
source_refs=source_refs,
stats=IngestStats(
intents_created=int(stats.get("intents_created", 0)),
intents_created=intents_created,
symbols_mapped=int(stats.get("symbols_mapped", 0)),
regions_linked=int(stats.get("regions_linked", 0)),
ungrounded=int(stats.get("ungrounded", 0)),
ungrounded=ungrounded_count,
grounded=grounded_count,
grounded_pct=grounded_pct,
grounding_deferred=grounding_deferred,
),
ungrounded_intents=list(result.get("ungrounded_intents", [])),
Expand Down
53 changes: 53 additions & 0 deletions ledger/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,59 @@ async def get_regions_for_files(
# ── Helpers ───────────────────────────────────────────────────────────────


async def get_grounding_breakdown(
client: LedgerClient,
source_type: str | None = None,
source_scope: str | None = None,
) -> list[dict]:
"""Per-source_ref grounded/ungrounded/total/pct breakdown.

Used by the M1 decision-relevance eval. Returns one row per distinct
source_ref in the intent table. An intent is "grounded" when its status
is anything other than "ungrounded" (pending/reflected/drifted all mean
the intent is linked to at least one code region).
"""
where_clauses: list[str] = []
vars: dict = {}
if source_type:
where_clauses.append("source_type = $source_type")
vars["source_type"] = source_type
if source_scope:
where_clauses.append("source_ref CONTAINS $source_scope")
vars["source_scope"] = source_scope
where = "WHERE " + " AND ".join(where_clauses) if where_clauses else ""

rows = await client.query(
f"""
SELECT source_ref, status
FROM intent
{where}
""",
vars or None,
)

buckets: dict[str, dict] = {}
for row in rows:
ref = str(row.get("source_ref") or "")
status = str(row.get("status") or "")
b = buckets.setdefault(
ref,
{"source_ref": ref, "grounded": 0, "ungrounded": 0, "total": 0, "grounded_pct": 0.0},
)
b["total"] += 1
if status == "ungrounded":
b["ungrounded"] += 1
else:
b["grounded"] += 1
Comment on lines +571 to +580

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Do not treat empty/missing status as grounded.

At Line 571-580, status == "" (or missing) currently increments grounded, which can overstate grounded_pct and skew eval outcomes.

Suggested fix
-        status = str(row.get("status") or "")
+        status = str(row.get("status") or "")
         b = buckets.setdefault(
             ref,
             {"source_ref": ref, "grounded": 0, "ungrounded": 0, "total": 0, "grounded_pct": 0.0},
         )
         b["total"] += 1
-        if status == "ungrounded":
+        if not status or status == "ungrounded":
             b["ungrounded"] += 1
         else:
             b["grounded"] += 1
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ledger/queries.py` around lines 571 - 580, The code currently treats any
non-"ungrounded" status (including empty or missing status) as grounded; update
the logic in the block that obtains status and updates the bucket (the variables
status, buckets.setdefault, and keys "grounded"/"ungrounded"/"total") so that
only an explicit "grounded" value increments b["grounded"], and all other cases
(including empty string or missing status) increment b["ungrounded"] (or are
handled as ungrounded) before updating totals and grounded_pct.


out = []
for b in buckets.values():
b["grounded_pct"] = (b["grounded"] / b["total"]) if b["total"] > 0 else 0.0
out.append(b)
out.sort(key=lambda r: r["source_ref"])
return out


def _normalize_decisions(rows: list[dict]) -> list[dict]:
"""Ensure code_regions always have a 'lines' tuple + consistent shape."""
for row in rows:
Expand Down
Loading