Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(cli): strip the tag: prefix in decision query --filter, not leading characters (#1764)

Shubham Srivastava03ac37360eb0
diff --git a/semantica/cli.py b/semantica/cli.py
index d69517db..47776f17 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -3147,6 +3147,17 @@ def decision_list(cli_ctx: CLIContext, limit: int, fmt: str, local_json: bool) -
_run_with_error_handling(_action)
+def _tag_filter_value(filter_str: str) -> str:
+ """Return the value part of a ``tag:<value>`` decision filter, lowercased.
+
+ ``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.
+ """
+ return filter_str.removeprefix("tag:").lower()
+
+
@decision.command("query")
@click.option("--filter", "filter_str", default=None, help="e.g. tag:finance")
@click.option("--since", default=None, help="ISO 8601 date.")
@@ -3170,7 +3181,7 @@ def decision_query(cli_ctx: CLIContext, filter_str: Optional[str],
"outcome": d.outcome, "confidence": d.confidence}
for d in (raw or [])
if filter_str is None
- or filter_str.lstrip("tag:").lower() in d.category.lower()
+ or _tag_filter_value(filter_str) 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 eceb91aa..6b95fb8c 100644
--- a/tests/test_cli_commands.py
+++ b/tests/test_cli_commands.py
@@ -1637,6 +1637,53 @@ class TestDecision:
result = runner.invoke(cli_module.main, ["decision", "list", "--format", "json"])
_ok(result)
+ # (filter, matching category, decoy category the mangled filter also hits)
+ # lstrip("tag:") strips leading t/a/g/: characters, so "tag:auth" became
+ # "uth" — which still substring-matches "auth", but ALSO matches "south".
+ @pytest.mark.parametrize("tag,decoy", [
+ ("auth", "south"),
+ ("git", "editor"),
+ ("testing", "nesting"),
+ ("tag", "anything-at-all"),
+ ])
+ def test_query_tag_filter_does_not_match_on_mangled_prefix(
+ self, runner, monkeypatch, tag, decoy
+ ):
+ """--filter tag:<value> must strip the prefix, not leading characters."""
+ fake_dq = MagicMock()
+
+ def _decision(did, category):
+ d = MagicMock()
+ d.decision_id, d.scenario, d.category = did, "T", category
+ d.outcome, d.confidence = "ok", 1.0
+ return d
+
+ fake_dq.find_by_time_range.return_value = [
+ _decision("d1", tag), _decision("d2", decoy),
+ ]
+ 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", f"tag:{tag}", "--format", "json"],
+ )
+ _ok(result)
+ ids = [d["id"] for d in json.loads(result.output)]
+ assert ids == ["d1"], (
+ f"tag:{tag} matched {decoy!r} too — the prefix was stripped as "
+ f"characters, leaving a shorter filter"
+ )
+
+ def test_query_tag_filter_value_strips_prefix_only(self):
+ assert cli_module._tag_filter_value("tag:auth") == "auth"
+ assert cli_module._tag_filter_value("tag:git") == "git"
+ assert cli_module._tag_filter_value("tag:tag") == "tag"
+ assert cli_module._tag_filter_value("plain") == "plain"
+
def test_trace_import_error_is_clean(self, runner):
with patch("builtins.__import__", side_effect=lambda n, *a, **k: (
(_ for _ in ()).throw(ImportError(n))