Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(cli): reject an empty tag: value in decision query --filter

wolfgang-auraa066362e715e
diff --git a/semantica/cli.py b/semantica/cli.py
index 7be2b023..07921de7 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -3180,9 +3180,16 @@ def _tag_filter_value(filter_str: str) -> str:
``str.lstrip("tag:")`` strips any leading ``t``, ``a``, ``g`` or ``:``
*characters*, not the prefix: ``tag:auth`` became ``uth`` and ``tag:git``
became ``it``, so the filter silently matched the wrong text. Anything
- without the prefix is passed through unchanged.
+ without the prefix is passed through unchanged. An empty value is rejected,
+ since ``"" in category`` is true for every decision.
"""
- return filter_str.removeprefix("tag:").lower()
+ value = filter_str.removeprefix("tag:").lower()
+ if not value:
+ raise click.BadParameter(
+ "tag: filter requires a non-empty value, e.g. tag:finance",
+ param_hint="'--filter'",
+ )
+ return value
@decision.command("query")
@@ -3197,6 +3204,7 @@ def decision_query(cli_ctx: CLIContext, filter_str: Optional[str],
cli_ctx = _require_ctx(cli_ctx)
def _action() -> None:
+ tag_value = _tag_filter_value(filter_str) if filter_str is not None else None
try:
from .context.decision_query import DecisionQuery
import datetime as _dt
@@ -3207,8 +3215,7 @@ def decision_query(cli_ctx: CLIContext, filter_str: Optional[str],
{"id": d.decision_id, "category": d.category, "scenario": d.scenario,
"outcome": d.outcome, "confidence": d.confidence}
for d in (raw or [])
- if filter_str is None
- or _tag_filter_value(filter_str) in d.category.lower()
+ if tag_value is None or tag_value in d.category.lower()
]
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.py
index d8ff55c9..4b53b58b 100644
--- a/tests/test_cli_commands.py
+++ b/tests/test_cli_commands.py
@@ -1751,6 +1751,28 @@ class TestDecision:
assert cli_module._tag_filter_value("tag:tag") == "tag"
assert cli_module._tag_filter_value("plain") == "plain"
+ @pytest.mark.parametrize("filter_str", ["tag:", ""])
+ def test_query_empty_tag_filter_is_rejected(self, runner, monkeypatch, filter_str):
+ """An empty tag value matched every decision; it must be a usage error."""
+ fake_dq = MagicMock()
+ monkeypatch.setitem(
+ __import__("sys").modules,
+ "semantica.context.decision_query",
+ _fake_module(DecisionQuery=lambda *a, **kw: fake_dq),
+ )
+ monkeypatch.setitem(
+ __import__("sys").modules,
+ "semantica.graph_store",
+ _fake_module(GraphStore=MagicMock(return_value=MagicMock())),
+ )
+
+ result = runner.invoke(
+ cli_module.main,
+ ["decision", "query", "--filter", filter_str, "--format", "json"],
+ )
+ assert result.exit_code != 0
+ fake_dq.find_by_time_range.assert_not_called()
+
def test_trace_import_error_is_clean(self, runner):
with patch("builtins.__import__", side_effect=lambda n, *a, **k: (
(_ for _ in ()).throw(ImportError(n))