Skip to content

feat(handlers): add agent metadata to logs and spans#69

Merged
dialupdisaster merged 1 commit into
mainfrom
feat/agent-metadata
Jun 20, 2026
Merged

feat(handlers): add agent metadata to logs and spans#69
dialupdisaster merged 1 commit into
mainfrom
feat/agent-metadata

Conversation

@dialupdisaster

@dialupdisaster dialupdisaster commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • persist per-session agent metadata so primary agents and subagents keep distinct agent.name and agent.type values
  • attach agent metadata to session, llm, and tool spans and refresh span attributes when the agent name becomes known later in the session
  • add the same metadata to session-scoped OTLP log events including prompts, requests, tool results, permissions, commits, and subtask events

Testing

  • bun run typecheck
  • bun test

Summary by CodeRabbit

  • Improvements
    • Enhanced observability with consistent tracking of agent identity and type (primary or subagent) across all telemetry and logs
    • Agent attribution now included in command execution, message processing, permission decisions, and session lifecycle events
    • Improved tracing to distinguish activity origins between primary agents and subagents

@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7df22185-1b9e-46bf-b8f0-b04b47f2854c

📥 Commits

Reviewing files that changed from the base of the PR and between 257984c and c2759e9.

📒 Files selected for processing (12)
  • src/handlers/activity.ts
  • src/handlers/message.ts
  • src/handlers/permission.ts
  • src/handlers/session.ts
  • src/index.ts
  • src/types.ts
  • src/util.ts
  • tests/handlers/activity.test.ts
  • tests/handlers/message.test.ts
  • tests/handlers/permission.test.ts
  • tests/handlers/session.test.ts
  • tests/handlers/spans.test.ts

📝 Walkthrough

Walkthrough

Adds SessionAgentType ("primary" | "subagent") to the SessionTotals type and two new utility functions (getSessionAgentMeta, agentAttrs) in src/util.ts. All five handlers (session, message, activity, permission, and index.ts) are updated to propagate agent.name and agent.type attributes into every span, log event, and metric emission.

Changes

Agent type telemetry propagation

Layer / File(s) Summary
Type contract and utility helpers
src/types.ts, src/util.ts
Adds SessionAgentType union ("primary" | "subagent") and an agentType field to SessionTotals. Exports getSessionAgentMeta to read agent name/type from ctx.sessionTotals with "unknown" fallbacks, and agentAttrs to build a stable OTLP attribute object from those values.
Session lifecycle and index agent attribution
src/handlers/session.ts, src/index.ts, tests/handlers/session.test.ts, tests/handlers/spans.test.ts
handleSessionCreated computes agentType from subagent status, persists it to sessionTotals, and adds it to the session span and session.created log. handleSessionIdle and handleSessionError read it back via getSessionAgentMeta to enrich finalization spans and logs. src/index.ts chat.message handler sets both AGENT_NAME and "agent.type" on the session span and user_prompt event.
Message handler agent attribution
src/handlers/message.ts, tests/handlers/message.test.ts, tests/handlers/spans.test.ts
Every telemetry emission in handleMessageUpdated, handleMessagePartUpdated, and startMessageSpan — including LLM spans, tool spans, subtask counter dimensions, and api_request/api_error/tool_result/subtask_invoked log events — now derives agentName/agentType via getSessionAgentMeta and emits them via agentAttrs.
Activity and permission handler attribution
src/handlers/activity.ts, src/handlers/permission.ts, tests/handlers/activity.test.ts, tests/handlers/permission.test.ts
handleCommandExecuted enriches the commit log event with agentAttrs; handlePermissionReplied enriches the tool_decision log event similarly. Tests seed ctx.sessionTotals and assert agent.name/agent.type on both log records.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • DEVtheOPS/opencode-plugin-otel#8: Modifies the same handleCommandExecuted git commit path in src/handlers/activity.ts where this PR adds agent metadata to the emitted commit log event.
  • DEVtheOPS/opencode-plugin-otel#17: Introduced sub-agent tracking and subtask_invoked handling in src/handlers/message.ts that this PR extends with agentName/agentType attribution.
  • DEVtheOPS/opencode-plugin-otel#20: Established the span lifecycle for sessions, tools, and LLM messages across src/handlers/message.ts, src/handlers/session.ts, and src/index.ts that this PR further enriches with agent.name and agent.type attributes.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding agent metadata to observability signals (logs and spans across the codebase).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/agent-metadata

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dialupdisaster dialupdisaster merged commit 307fabc into main Jun 20, 2026
5 of 6 checks passed
@dialupdisaster dialupdisaster deleted the feat/agent-metadata branch June 20, 2026 18:37
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.

1 participant