Skip to content
Open
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
19 changes: 13 additions & 6 deletions src/lang2sql/tools/resolve_business_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ def spec(self):
},
)

def definitions(self, terms, ctx):
def definitions(self, terms, ctx, *, missing_terms=None):
if ctx.store is None:
if missing_terms is not None:
missing_terms.extend(terms)
return None
entries = _load_all(ctx.store, ctx.identity.kv_scope)
resolved = []
missing = []
for term in terms:
# Exact canonical names only: fuzzy/alias guessing defeats clarification.
entry = _resolve_entry(
Expand All @@ -63,9 +66,12 @@ def definitions(self, terms, ctx):
ctx.identity.user_id,
)
if entry is None or entry.inferred or not entry.definition.strip():
return None
resolved.append(asdict(entry))
return resolved
missing.append(term)
else:
resolved.append(asdict(entry))
if missing_terms is not None:
missing_terms.extend(missing)
return None if missing else resolved

async def reject(self, ctx, reason):
logger.warning(
Expand Down Expand Up @@ -101,11 +107,12 @@ async def run(self, args, ctx):
or any(not isinstance(t, str) or not t.strip() for t in terms)
):
return await self.reject(ctx, "Supply all business terms as nonempty names")
definitions = self.definitions(terms, ctx)
missing = []
definitions = self.definitions(terms, ctx, missing_terms=missing)
if definitions is None:
return await self.reject(
ctx,
"At least one requested term has no confirmed definition in this scope",
"No confirmed definition in this scope for: " + json.dumps(missing, ensure_ascii=False),
)
token = secrets.token_urlsafe()
self.receipt = (token, tuple(terms), definitions, ctx.identity.session_key())
Expand Down
21 changes: 21 additions & 0 deletions tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,24 @@ async def test_literal_query_without_catalog_still_requires_single_use_receipt(
"resolve_business_terms", {"terms": ["undefined_net_revenue"]}
)
assert missing.isError and "DEFINITION_REQUIRED" in missing.content[0].text


@pytest.mark.asyncio
async def test_missing_definitions_are_reported_together_and_revoke_receipt(tmp_path):
async with connected(fixture_db(tmp_path), tmp_path / "metadata.db") as client:
await client.call_tool("term_custom", {
"term": "known", "definition": "SUM(orders.amount)", "layer": "team"
})
check = await client.call_tool("resolve_business_terms", {"terms": ["known"]})
token = json.loads(check.content[0].text)["definition_check_id"]
check = await client.call_tool("resolve_business_terms", {
"terms": ["missing_a", "known", "missing_b"]
})
assert check.isError
message = check.content[0].text
assert '["missing_a", "missing_b"]' in message
assert '"known"' not in message
rejected = await client.call_tool("run_sql", {
"sql": "SELECT COUNT(*) FROM orders", "definition_check_id": token
})
assert rejected.isError