Skip to content

feat(runtime): add agent label to librefang_tool_call_total (#6226)#6229

Merged
houko merged 6 commits into
mainfrom
feat/6226-tool-call-agent-label
Jun 19, 2026
Merged

feat(runtime): add agent label to librefang_tool_call_total (#6226)#6229
houko merged 6 commits into
mainfrom
feat/6226-tool-call-agent-label

Conversation

@houko

@houko houko commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

librefang_tool_call_total (added in #3495) was labeled only by tool and outcome, so tool failures could not be attributed per-agent.
This adds an agent dimension so the counter becomes librefang_tool_call_total{agent, tool, outcome}, mirroring the existing per-agent librefang_cron_fires_total{agent} precedent already accepted in the codebase.

Changes

  • crates/librefang-runtime/src/agent_loop/tool_call.rs
    • record_tool_call_metric now takes an agent_id: &str first argument and emits an agent label.
    • The agent id is sanitized and length-capped via the shared sanitize_tool_label helper — identical handling to the tool label — so a hallucinated or hostile caller id cannot blow up metric cardinality. The helper doc-comment is generalized to cover both dimensions.
    • All call sites updated to thread the agent id from session.agent_id (ctx.agent_id_str):
      • execute_single_tool_call (Ok + Err paths).
      • execute_tool_group Phase 1 serial pre-check (Blocked + CircuitBreak).
      • execute_tool_group Phase 2 concurrent collector (Ok + Err) — the agent id is captured once from the shared &ctx borrow so the concurrent loop needs no re-borrow.
  • crates/librefang-runtime/src/agent_loop/tests/utilities.rs
  • CHANGELOG.md: entry under ## [Unreleased]### Added.

Verification

Run via the repo's Docker dev image (librefang-rust-dev:latest), scoped to librefang-runtime, per CLAUDE.md (no native cargo on host).

  • cargo check -p librefang-runtime --lib — Finished, no errors.
  • cargo clippy -p librefang-runtime --all-targets -- -D warnings — Finished, zero warnings.
  • cargo test -p librefang-runtime --lib record_tool_call_metric — 3 passed; 0 failed:
    • test_record_tool_call_metric_success_outcome
    • test_record_tool_call_metric_failure_outcome
    • test_record_tool_call_metric_carries_agent_label
  • cargo fmt -p librefang-runtime applied.

Deferred work: none.

Closes #6226

Evan and others added 2 commits June 19, 2026 14:04
The tool-call counter added in #3495 was labeled only by tool and
outcome, so tool failures could not be attributed to the agent that
issued them.

Add an agent dimension so the counter becomes
librefang_tool_call_total{agent, tool, outcome}, mirroring the existing
per-agent librefang_cron_fires_total{agent} precedent.

record_tool_call_metric now takes the agent id, threaded from
session.agent_id (ctx.agent_id_str) at every call site — the serial
execute_single_tool_call wrapper and both the serial and concurrent
arms of execute_tool_group.
The agent id is sanitized and length-capped via the shared
sanitize_tool_label helper, identical to the tool label, so a
hallucinated or hostile caller id cannot blow up metric cardinality.

Tests: existing failure/success metric tests updated for the new
signature; new test_record_tool_call_metric_carries_agent_label asserts
the counter emits agent/tool/outcome labels and that the agent value is
control-char-stripped and capped at 64 chars.
…nd inline comments

The five comment blocks added by this PR split sentences across multiple
lines, violating the repo's prose-wrapping rule (one sentence = one line
in all prose, including doc-comments and inline comments).
Unfold each to a single line per sentence; no logic change.
@github-actions github-actions Bot added area/docs Documentation and guides area/runtime Agent loop, LLM drivers, WASM sandbox labels Jun 19, 2026
@github-actions github-actions Bot added size/M 50-249 lines changed has-conflicts PR has merge conflicts that need resolution labels Jun 19, 2026
The #6226 change added an `agent` dimension to
`librefang_tool_call_total` in record_tool_call_metric, but the
metric's describe_counter! help string still read "labeled by tool
name and outcome".
Operators reading /metrics would not know the agent label exists, and
the cron description that cites this counter as its cardinality
precedent was now inconsistent.
Update the help string to "labeled by agent, tool name, and outcome"
to match the emitted labels.
@houko

houko commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Adversarial review follow-up: the agent label was emitted but the metric's describe_counter! help string in crates/librefang-telemetry/src/metrics.rs still read "labeled by tool name and outcome".
Operators reading /metrics wouldn't know the agent dimension exists, and it left the metric inconsistent with the cron description that cites it as the cardinality precedent.
Fixed the help string to "labeled by agent, tool name, and outcome (#3495, #6226)".
Verified in the dev container: cargo check + cargo clippy --all-targets -- -D warnings clean on librefang-runtime/librefang-telemetry, and the new test_record_tool_call_metric_carries_agent_label regression test plus the two existing outcome tests pass.

@houko houko closed this Jun 19, 2026
@houko houko reopened this Jun 19, 2026
@houko
houko enabled auto-merge (squash) June 19, 2026 07:28
…gent-label

# Conflicts:
#	crates/librefang-runtime/src/agent_loop/tests/utilities.rs
@github-actions github-actions Bot added ready-for-review PR is ready for maintainer review and removed has-conflicts PR has merge conflicts that need resolution labels Jun 19, 2026
@houko
houko merged commit 12423f5 into main Jun 19, 2026
31 checks passed
@houko
houko deleted the feat/6226-tool-call-agent-label branch June 19, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs Documentation and guides area/runtime Agent loop, LLM drivers, WASM sandbox ready-for-review PR is ready for maintainer review size/M 50-249 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

librefang_tool_call_total has no agent dimension — tool failures can't be attributed per-agent

2 participants