Skip to content

Dev observability: emit agent turns as MLflow traces via OTLP (assembled prompt, tools, iterations) - #1343

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1341-mlflow-otlp-traces
Jun 5, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1341-mlflow-otlp-traces

Conversation

@malibio

@malibio malibio commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Closes #1341

Emits one trace per agent turn when NODESPACE_MLFLOW_URL is set:
- agent_turn root span: session_id, model_id, user_message
- prompt_assembly child: full assembled system prompt, workspace_context, tools_offered JSON
- react_iteration_N child: messages_sent, raw_response, tool_calls_parsed, tool_results, token counts
- response_processing child: raw_input, normalized_output, strippers_fired

Zero overhead when env var is absent (no-op OTel provider, no network connections).
Adds normalize_response_traced() to capture which response strippers fired.
Docs: scripts/mlflow-dev.md covers start-server + run-daemon + MCP query workflow.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This is a clean, well-scoped implementation of dev-only OTLP tracing. The zero-overhead guarantee is correctly implemented (no-op OTel provider when env var is absent, no network connections, no feature flag needed). The span hierarchy matches the issue spec, context propagation is correct (turn_cx properly parents all child spans), and the SdkTracerProvider lifetime is handled correctly with _otlp_provider in main(). The new normalize_response_traced() function is functionally correct.

No blockers. Three improvements are recommended before merge.


Findings

Suggested Improvements

[Improvement] packages/agent/Cargo.toml line 33 — unused grpc-tonic feature pulls in a large transitive dependency tree

The opentelemetry-otlp dependency enables the grpc-tonic feature, but otlp_tracer.rs calls .with_http() which only needs http-proto + reqwest-rustls. The grpc-tonic feature is never exercised at runtime. This causes Cargo to compile tonic 0.14, prost 0.14, quinn (QUIC), aws-lc-rs, and aws-lc-sys — all visible in the Cargo.lock additions. Removing the unused feature reduces the build footprint significantly.

# Current (pulls in grpc-tonic, quinn, aws-lc-rs, etc.)
opentelemetry-otlp = { version = "0.32", features = ["trace", "grpc-tonic", "http-proto", "reqwest-rustls"] }

# Corrected (only what .with_http() needs)
opentelemetry-otlp = { version = "0.32", features = ["trace", "http-proto", "reqwest-rustls"] }

[Improvement] packages/agent/src/local_agent/response_processing.rs — normalize_response and normalize_response_traced will silently diverge if a stripper is added to one but not the other

Both functions implement the same 7-step pipeline independently. If someone adds a new stripper to normalize_response (the single maintained entry point) and forgets normalize_response_traced, the strippers_fired attribute will silently give incorrect data — exactly the kind of invisible error that undermines observability tooling. This violates DRY (Don't Repeat Yourself). The apply closure already captures the needed abstraction. Consider refactoring normalize_response to delegate to normalize_response_traced (discarding the fired list), making the traced version the single source of truth:

pub fn normalize_response(text: &str) -> String {
    normalize_response_traced(text).0
}

This eliminates the synchronization requirement entirely and has no overhead when tracing is disabled, since the fired-list allocation is always Vec::new() + a few push calls — negligible.

[Improvement] packages/agent/src/local_agent/otlp_tracer.rs — NODESPACE_MLFLOW_URL is not validated to be a localhost address

Acceptance criterion AC5 states "No sensitive user data leaves the machine." The code correctly documents this in mlflow-dev.md, but the runtime does not enforce it. If a developer sets NODESPACE_MLFLOW_URL=https://external-server.com, the assembled system prompt, user messages, tool call arguments, and tool results would all be sent to that remote endpoint. Given that this feature explicitly captures the full system prompt and conversation, a URL validation check (warn or reject if the host is not 127.0.0.1/localhost) would make the security boundary mechanically enforced rather than documentation-only:

let host = url::Url::parse(&endpoint).ok()
    .and_then(|u| u.host_str().map(|h| h.to_string()))
    .unwrap_or_default();
if host != "localhost" && host != "127.0.0.1" && host != "::1" {
    tracing::warn!(host = %host, "NODESPACE_MLFLOW_URL points to a non-localhost host — traces contain assembled prompts and user messages");
}

This does not need to be a hard block (it's a dev tool), but a warn! at startup makes the operator aware.


Nitpicks

  • Nit: packages/agent/Cargo.toml — opentelemetry-semantic-conventions = "0.32" is declared as a dependency but never imported in any source file. Delete it.

  • Nit: packages/agent/src/local_agent/response_processing.rs line 103-108 — strip_channel_blocks is the only stripper handled with an inline block rather than the apply() closure, even though its signature fn(&str) -> String satisfies the closure's f parameter. This inconsistency makes the intent of the block slightly harder to follow. Could be unified (only meaningful if normalize_response is not refactored to delegate per the Improvement above).

  • Nit: packages/agent/src/local_agent/agent_loop.rs line 559 — the last-iteration forced-text path calls normalize_response(&final_text) but does not emit a response_processing span. This is a minor blind spot: when the agent hits the iteration cap, the normalization of the forced response is invisible to the trace. Worth noting for future consistency.

  • Nit: packages/agent/src/local_agent/response_processing.rs — normalize_response_traced has no unit tests. The existing test suite only exercises normalize_response. A single test asserting that strippers_fired is populated correctly (e.g., input with call:foo{bar:baz} should produce fired = ["strip_tool_call_prose"]) would give confidence that the tracing data is accurate.

  • Nit: scripts/mlflow-dev.md line 17 — mlflow server --host 0.0.0.0 binds to all interfaces. For a localhost-only dev workflow, --host 127.0.0.1 is the safer default and reinforces the "all traces go to localhost only" guarantee documented in the Notes section.


Acceptance Criteria Check

  • AC1 (zero overhead when unset): Confirmed — no-op OTel provider, no network, no compilation flags.
  • AC2 (trace per turn when set): Confirmed in code — execute_turn wraps in agent_turn root span with all required child spans.
  • AC3 (mlflow mcp run): Documentation covers this; not mechanically verifiable in code review.
  • AC4 (span latencies within 5%): Not verifiable statically — OTel span timing is wall-clock by construction.
  • AC5 (no sensitive data leaves machine): Code does not enforce this — see Improvement Desktop shell with resizable panels #3 above.

The grpc-tonic issue (Improvement #1) is the highest-priority fix before merge; it adds significant unnecessary build weight.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three improvements recommended before merge — see full review comment. The grpc-tonic unused feature is the priority fix (eliminates aws-lc-rs/quinn from the build). No blockers / security vulns.

- Drop grpc-tonic feature and opentelemetry-semantic-conventions from
  agent Cargo.toml — only http-proto + reqwest-rustls are used; grpc-tonic
  was pulling in tonic 0.14, prost 0.14, quinn, aws-lc-rs unnecessarily
- Refactor normalize_response to delegate to normalize_response_traced,
  making the traced version the single pipeline definition — adding a
  stripper can no longer silently diverge between the two entry-points
- Fix strip_channel_blocks inline block to use the apply() closure for
  consistency with all other strippers
- Add is_localhost() guard in init_tracer: warn at startup when
  NODESPACE_MLFLOW_URL points to a non-localhost host (traces carry full
  assembled prompts and user messages)
- Add 4 unit tests for normalize_response_traced: clean text fires nothing,
  strip_tool_call_prose fires, strip_channel_blocks fires, and a parity
  test asserting normalize_response == normalize_response_traced().0
- Fix mlflow-dev.md to use --host 127.0.0.1 (not 0.0.0.0)

Skipped: last-iteration response_processing span — the forced-no-tools
path is an edge case fallback and adding a span there is scope expansion,
not a fix for a review finding.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Review Addressed

All reviewer recommendations implemented in commit e492e5b.

✅ Addressed (6):

  1. Removed grpc-tonic + opentelemetry-semantic-conventions from Cargo.toml — eliminates tonic 0.14, prost 0.14, quinn, aws-lc-rs from the build
  2. Unified pipeline — normalize_response now delegates to normalize_response_traced; the traced version is the single source of truth, divergence is impossible
  3. Fixed strip_channel_blocks inconsistency — now uses the same apply() closure as all other strippers
  4. Added is_localhost() guard in init_tracer() — warns at startup if NODESPACE_MLFLOW_URL points to a non-localhost host
  5. Added 4 unit tests for normalize_response_traced (clean text, tool-call-prose fires, channel-blocks fires, parity test)
  6. Fixed scripts/mlflow-dev.md — --host 127.0.0.1 instead of 0.0.0.0

⏭️ Skipped (1):

  • Last-iteration response_processing span — the forced-no-tools path at max iterations is an edge-case fallback, not part of the normal turn flow; adding a span there is scope expansion beyond the issue spec, not fixing a review finding

All tests pass (228 Rust + 4 new response_processing + 3844 frontend). cargo clippy clean.

@malibio
malibio merged commit b12f6dc into main Jun 5, 2026
@malibio
malibio deleted the worktree-issue-1341-mlflow-otlp-traces branch June 5, 2026 16:58
malibio added a commit that referenced this pull request Jun 22, 2026
…led prompt, tools, iterations) (#1343)

* Add OTLP agent tracing via MLflow for dev observability (closes #1341)

Emits one trace per agent turn when NODESPACE_MLFLOW_URL is set:
- agent_turn root span: session_id, model_id, user_message
- prompt_assembly child: full assembled system prompt, workspace_context, tools_offered JSON
- react_iteration_N child: messages_sent, raw_response, tool_calls_parsed, tool_results, token counts
- response_processing child: raw_input, normalized_output, strippers_fired

Zero overhead when env var is absent (no-op OTel provider, no network connections).
Adds normalize_response_traced() to capture which response strippers fired.
Docs: scripts/mlflow-dev.md covers start-server + run-daemon + MCP query workflow.

Co-Authored-By: Claude <[email protected]>

* Address review: remove unused deps, unify pipeline, add localhost guard

- Drop grpc-tonic feature and opentelemetry-semantic-conventions from
  agent Cargo.toml — only http-proto + reqwest-rustls are used; grpc-tonic
  was pulling in tonic 0.14, prost 0.14, quinn, aws-lc-rs unnecessarily
- Refactor normalize_response to delegate to normalize_response_traced,
  making the traced version the single pipeline definition — adding a
  stripper can no longer silently diverge between the two entry-points
- Fix strip_channel_blocks inline block to use the apply() closure for
  consistency with all other strippers
- Add is_localhost() guard in init_tracer: warn at startup when
  NODESPACE_MLFLOW_URL points to a non-localhost host (traces carry full
  assembled prompts and user messages)
- Add 4 unit tests for normalize_response_traced: clean text fires nothing,
  strip_tool_call_prose fires, strip_channel_blocks fires, and a parity
  test asserting normalize_response == normalize_response_traced().0
- Fix mlflow-dev.md to use --host 127.0.0.1 (not 0.0.0.0)

Skipped: last-iteration response_processing span — the forced-no-tools
path is an edge case fallback and adding a span there is scope expansion,
not a fix for a review finding.

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dev observability: emit agent turns as MLflow traces via OTLP (assembled prompt, tools, iterations)

1 participant