Skip to content

The agent that grows with you

fix(telemetry): network-address model ids collapse; subscriber re-runs the catalog (m12)

teknium1ac372200b15b
diff --git a/hermes_cli/observability/shared_metrics_catalog.py b/hermes_cli/observability/shared_metrics_catalog.py
index e3b4fbd833f..0a7fe3566a3 100644
--- a/hermes_cli/observability/shared_metrics_catalog.py
+++ b/hermes_cli/observability/shared_metrics_catalog.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import functools
import logging
+import re
from pathlib import Path
logger = logging.getLogger(__name__)
@@ -283,11 +284,18 @@ def model_metric_name(raw: object, provider: str, *, max_length: int) -> str:
model = _metric_identifier(raw, max_length=max_length)
if model == "unknown":
return model
- if provider == "unknown" or "://" in model or ":/" in model or model.endswith((".gguf", ".bin", ".safetensors")):
+ if provider == "unknown" or _LOCATION_MODEL.match(model):
return CUSTOM
return model
+# The user's own server, never a public model id: a URL or path (``:/``), a weight file, a loopback
+# or IPv4 host, or ``host:port`` (a 2-5 digit port, so Bedrock's ``...-v1:0`` stays readable).
+_LOCATION_MODEL = re.compile(
+ r".*:/|.*\.(?:gguf|bin|safetensors)$|(?:localhost|\d{1,3}(?:\.\d{1,3}){3})(?:[:/]|$)|[^/:]+:\d{2,5}(?:/|$)"
+)
+
+
def display_language_metric_name(raw: object) -> str:
name = _norm(raw) or "en"
return name if name in _safe(display_languages) else "other"
diff --git a/hermes_cli/observability/shared_metrics_contract.py b/hermes_cli/observability/shared_metrics_contract.py
index 56254effdca..4ef21b9e3c1 100644
--- a/hermes_cli/observability/shared_metrics_contract.py
+++ b/hermes_cli/observability/shared_metrics_contract.py
@@ -1016,7 +1016,27 @@ def _bounded_dimensions(metric_name: str, data: Any) -> dict[str, str] | None:
if not isinstance(data, dict) or set(data) != expected_fields:
return None
dimensions = {field: data.get(field) for field in sorted(expected_fields)}
- return dimensions if counter_dimensions_are_valid(metric_name, dimensions) else None
+ valid = counter_dimensions_are_valid(metric_name, dimensions) and _catalog_holds(metric_name, dimensions)
+ return dimensions if valid else None
+
+
+def _catalog_holds(metric_name: str, dimensions: dict[str, str]) -> bool:
+ """Re-run the producer's catalog pass on a mark's provider/model fields: an identifier it would
+ rewrite (a user-named provider, a local model id) never reaches a counter. Record-time only, so
+ catalog drift can never block packaging rows already stored. ``none`` is the unset-provider value."""
+ from .shared_metrics_catalog import model_metric_name, provider_metric_name
+
+ for field, max_length in _IDENTIFIER_FIELDS.get(metric_name, {}).items():
+ value = dimensions[field]
+ if field.endswith("provider"):
+ expected = value if value == "none" else provider_metric_name(value)
+ elif field.endswith("model"):
+ expected = model_metric_name(value, dimensions[field[:-5] + "provider"], max_length=max_length)
+ else:
+ continue
+ if value != expected:
+ return False
+ return True
def _valid_shape(event: Any, **shape: Any) -> bool:
@@ -1173,7 +1193,9 @@ def model_token_counters(event: Any) -> list[tuple[str, dict[str, str], int]]:
if isinstance(amount, bool) or not isinstance(amount, int) or amount < 0:
return []
dimensions = {**base, "token_type": token_type}
- if not counter_dimensions_are_valid(MODEL_TOKENS_METRIC, dimensions):
+ if not counter_dimensions_are_valid(MODEL_TOKENS_METRIC, dimensions) or not _catalog_holds(
+ MODEL_TOKENS_METRIC, dimensions,
+ ):
return []
if amount:
counters.append((MODEL_TOKENS_METRIC, dimensions, amount))
diff --git a/tests/hermes_cli/test_shared_metrics_model_attribution.py b/tests/hermes_cli/test_shared_metrics_model_attribution.py
index caa915ea4e6..0838798160f 100644
--- a/tests/hermes_cli/test_shared_metrics_model_attribution.py
+++ b/tests/hermes_cli/test_shared_metrics_model_attribution.py
@@ -155,6 +155,31 @@ def test_user_provider_plugin_name_and_model_never_leave(direct_runtime, tmp_pat
assert catalog.provider_metric_name("deepinfra") == "deepinfra"
+def test_network_address_model_ids_and_raw_injected_marks_never_reach_counters(direct_runtime, tmp_path):
+ """A ``host:port``/IP/localhost model id on a public provider reads ``custom`` (Bedrock's
+ ``-v1:0`` and OpenRouter's ``:free`` stay), and the subscriber re-runs the catalog on a mark's
+ provider/model so a raw mark that skipped the producer's pass stores nothing."""
+ from hermes_cli.observability import shared_metrics_contract as contract
+ from hermes_cli.observability.shared_metrics_model import model_route
+
+ for model in ("127.0.0.1:8080/x", "localhost/qwen", "10.0.0.5/x", "gpu-box.lan:8000/qwen", "gpu-box.lan:8000"):
+ assert model_route("openrouter", model)["model"] == "custom", model
+ for provider, model in (("bedrock", "anthropic.claude-3-5-sonnet-20241022-v2:0"),
+ ("openrouter", "openai/gpt-4o:free"), ("ollama-cloud", "gpt-oss:120b")):
+ assert model_route(provider, model)["model"] == model
+
+ runtime = relay_shared_metrics._get_runtime(retry_failed=True)
+ for provider, model in (("openrouter", "127.0.0.1:8080/x"), ("openrouter", "http://127.0.0.1:8080/v1"),
+ ("custom:acme", "x"), ("ollama", "qwen-private"), ("acmecorp-gateway", "acme-7b"),
+ ("anthropic", "claude-sonnet-4-5")):
+ runtime.record_process_mark(contract.MODEL_SWITCH_AFTER_MARK, {
+ "model": model, "provider": provider, "turns_before_switch_bucket": "1"})
+ _flush()
+
+ assert _stored_values(tmp_path, contract.MODEL_SWITCH_AFTER_METRIC) == [
+ ({"model": "claude-sonnet-4-5", "provider": "anthropic", "turns_before_switch_bucket": "1"}, 1)]
+
+
@pytest.mark.parametrize(("configured", "expected"), [
(("custom", "acme-private-llama", "http://localhost:8080/v1"), ("custom", "custom")),
(("openrouter", PUBLIC, ""), ("openrouter", PUBLIC)),
diff --git a/website/docs/developer-guide/relay-shared-metrics.md b/website/docs/developer-guide/relay-shared-metrics.md
index 4c6604bfd19..eebcc50aaea 100644
--- a/website/docs/developer-guide/relay-shared-metrics.md
+++ b/website/docs/developer-guide/relay-shared-metrics.md
@@ -354,7 +354,9 @@ model id; custom endpoints, provider plugins installed under
`$HERMES_HOME/plugins/model-providers/` or from pip (names and aliases included),
the local-server aliases of `custom` (`ollama`, `local`, `vllm`, `llamacpp`,
`llama.cpp`) and loopback servers read `custom`, and a model whose provider is
-unknown reads `custom`.
+unknown, or whose id is a URL, a file path or a network address (`host:port`, an
+IP address, `localhost`), reads `custom`. The local subscriber re-runs these rules
+on the provider/model fields of every mark and drops a row they would rewrite.
| Metric | Dimensions | Question it answers |
|---|---|---|