# How should an instance attribute an anomalous span to the right agent when trace clusters merge on shared tool-call signatures **Date of Record:** 2026 November 23 **Scope:** Applies to observability pipelines that cluster agent spans by structural signature (tool name + argument shape + call graph position) and then attribute anomalies to a responsible agent instance. Does not apply to single-agent traces with a unique root span, nor to pipelines that attribute purely by wall-clock latency without clustering. ## Core Distinction There are two fundamentally different things an anomaly detector can report, and conflating them is the root of misattribution: 1. **Span-level anomaly** — a single span deviated from its own historical distribution (latency, token count, tool error rate, argument entropy). 2. **Cluster-level anomaly** — a *set* of spans, grouped by structural signature, shifted in aggregate. The failure mode examined here is **cluster collapse**: when two or more distinct agents emit structurally identical spans (same tool, same argument schema, overlapping call-graph position), the clustering step merges them into one cluster. A cluster-level anomaly then fires, but the attribution step—which resolves "which agent caused this"—has no discriminator left, because the merge destroyed the very field (agent identity) that would have separated them. The detector is correct that *something* anomalous happened; the attributor is now guessing. This is not a data-loss bug in the usual sense. The spans still carry `agent_id`. The collapse happens because the *clustering key* excluded `agent_id` (deliberately, to allow cross-agent pattern detection), and the downstream attributor assumed the cluster was homogeneous. The invariant violated is: **a cluster used for attribution must be homogeneous in the attribution dimension.** ## What Was Observed A three-agent swarm (planner, retriever, verifier) ran a document-analysis task. All three called `search_documents(query: str, top_k: int)`. The clustering key was `(tool_name, arg_schema_hash, depth_in_call_graph)`. All three calls landed at depth 2 with identical schema hash `a7f3…`. One cluster formed spanning all three agents. During the run, the retriever agent entered a degraded loop: it retried `search_documents` with marginally mutated queries (token-budget exhaustion caused truncated query strings, so it re-issued near-duplicates). The retriever's spans had elevated `retry_count` and depressed `top_k`, and their `query` argument entropy collapsed (many near-identical strings). The cluster-level anomaly fired correctly: aggregate `retry_count` for cluster `a7f3…` exceeded the 99th percentile. But attribution reported the **planner** as the responsible agent, because the planner was the cluster's plurality contributor (it issued the most `search_documents` calls overall) and the attributor assigned cluster-level blame to the modal member. The retriever—the actual cause—was ranked third. The trace showed the truth only when reconstructed by hand: filtering cluster `a7f3…` by `agent_id == retriever` and plotting `query` string entropy over time showed a clean monotonic collapse beginning at token-budget 87% consumption. The anomaly was real, localized, and *caused by a single agent whose identity had been averaged away at the cluster boundary.* ## Condition of Validity The attribution is only valid when the following holds for the cluster under inspection: - **Homogeneity in the attribution dimension.** All spans in the cluster share the same value of the field you intend to blame by (here, `agent_id`). If the clustering key omitted that field, the cluster is not attributable without a re-partition. - **Stationarity of membership.** The cluster's membership did not change during the anomaly window. If an agent *joined* the cluster mid-window (e.g., it started calling `search_documents` only after a fallback triggered), the aggregate shift may be a composition change, not a behavioral change. This is Simpson's-paradox-on-traces: the cluster got worse because a worse agent joined, not because any member degraded. - **Argument-space comparability.** Structural signature equality does not imply semantic comparability. Two agents calling `search_documents(query, top_k)` with different query distributions are not samples from the same population; their aggregate statistics are meaningless when pooled. When any of these fails, the cluster-level anomaly is still a valid *detection* signal but an invalid *attribution* signal. The system must fall back to a finer partition before naming an agent. ## Decision Boundary The operational rule: **attribute at the finest partition that is homogeneous in the attribution dimension and stationary over the anomaly window; if no such partition exists, report "unattributed cluster anomaly" rather than a modal-member guess.** Concretely, the pipeline must, before emitting an attribution: 1. Re-partition the anomalous cluster by the candidate attribution field (`agent_id`). 2. For each partition, test membership stationarity across the window (did the partition's span count change by more than a threshold fraction?). A non-stationary partition is flagged, not attributed. 3. Test argument-distribution comparability across partitions (e.g., entropy or KL divergence between partitions' argument distributions). If partitions are non-comparable, the pooled cluster statistic is suppressed and per-partition statistics are reported instead. 4. Emit attribution only for partitions that pass both tests; otherwise emit an explicit `UnattributedClusterAnomaly` with the candidate partitions listed for human or downstream-agent review. The boundary is sharp: a modal-member attribution is *never* acceptable when the homogeneity test fails, because the plurality contributor is systematically the agent that calls the tool most, which is uncorrelated with—and often anti-correlated to—the agent that degraded. ## Operational Effect Downstream consumers of the attribution (auto-remediation, circuit breakers, agent quarantine) act on a wrong target. In the observed run, the planner was throttled—reducing its `search_documents` call rate—which did nothing to the retriever's retry loop, and the task still failed its token budget. The quarantine bought ~40 seconds and cost a planner restart with cold-start amnesia (the planner's episodic memory was not checkpointed before throttle, so on resume it re-derived its plan from scratch, doubling planner token spend). The guardrail changes the failure from *silent misattribution with side effects* to *loud non-attribution with a candidate list*. The latter is recoverable: a downstream agent or operator can re-query the trace with the correct partition. The former is not, because the wrong action has already been taken and the trace's own attribution record now asserts a falsehood that future post-mortems will trust. The cost is an increase in `unattributed` outcomes, which is the correct trade: an unattributed anomaly is an observability gap to be closed by better clustering keys, whereas a misattributed anomaly is an active corruption of the operational record. ```python from dataclasses import dataclass, field from typing import Sequence class UnattributableClusterError(RuntimeError): """Raised when a cluster anomaly cannot be safely attributed to one agent.""" pass @dataclass class Span: agent_id: str tool_name: str arg_schema_hash: str depth: int retry_count: int query_entropy: float window_bucket: int # coarse time bucket for stationarity test @dataclass class Cluster: key: tuple spans: list = field(default_factory=list) def attribute_anomaly( cluster: Cluster, anomaly_window: tuple, stationarity_tol: float = 0.35, entropy_tol: float = 0.25, ) -> str: """ Return the agent_id to blame, or raise UnattributableClusterError. Invariant: attribution requires a partition homogeneous in agent_id, stationary in membership over the window, and comparable in argument distribution to the cluster's other partitions. """ lo, hi = anomaly_window in_window = [s for s in cluster.spans if lo <= s.window_bucket <= hi] if not in_window: raise UnattributableClusterError( f"cluster {cluster.key} has no spans in anomaly window {anomaly_window}" ) # Partition by the attribution dimension. partitions: dict[str, list[Span]] = {} for s in in_window: partitions.setdefault(s.agent_id, []).append(s) if len(partitions) == 1: return next(iter(partitions)) # Stationarity: did each partition's share of the window change? buckets = sorted({s.window_bucket for s in in_window}) if len(buckets) < 2: raise UnattributableClusterError( f"cluster {cluster.key} spans a single bucket; stationarity untestable" ) first, last = buckets[0], buckets[-1] shares = {} for agent, spans in partitions.items(): n_first = sum(1 for s in spans if s.window_bucket == first) n_last = sum(1 for s in spans if s.window_bucket == last) total_first = sum(1 for s in in_window if s.window_bucket == first) total_last = sum(1 for s in in_window if s.window_bucket == last) share_first = n_first / total_first if total_first else 0.0 share_last = n_last / total_last if total_last else 0.0 shares[agent] = abs(share_last - share_first) unstable = [a for a, d in shares.items() if d > stationarity_tol] if unstable: raise UnattributableClusterError( f"cluster {cluster.key} membership non-stationary for {unstable}; " "aggregate shift may be composition change, not degradation" ) # Comparability: mean argument entropy across partitions. means = { a: sum(s.query_entropy for s in spans) / len(spans) for a, spans in partitions.items() } spread = max(means.values()) - min(means.values()) if spread > entropy_tol: raise UnattributableClusterError( f"cluster {cluster.key} partitions non-comparable in argument " f"distribution (entropy spread {spread:.3f} > {entropy_tol}); " f"per-partition stats: {means}" ) # Homogeneous, stationary, comparable: name the worst-degraded partition. blame = max( partitions, key=lambda a: sum(s.retry_count for s in partitions[a]) / len(partitions[a]), ) return blame ```