Skip to content

fix agent name showing 'unknown' issue#73

Closed
chingjustwe wants to merge 2 commits into
DEVtheOPS:mainfrom
chingjustwe:feature/fix-agent-unknown-issue
Closed

fix agent name showing 'unknown' issue#73
chingjustwe wants to merge 2 commits into
DEVtheOPS:mainfrom
chingjustwe:feature/fix-agent-unknown-issue

Conversation

@chingjustwe

@chingjustwe chingjustwe commented Jun 24, 2026

Copy link
Copy Markdown

Description

Fix Issue: #72

When a user sends a message to an existing session (no session.created event), the sessionTotals map has no entry for that session. The chat.message hook only mutated an existing entry but did not create one. This caused getSessionAgentMeta to fall back to "unknown" for all subsequent metric counters, trace span attributes, and log event attributes.
This fix creates a sessionTotals entry in the chat.message hook when one does not exist, using input.agent as the agent name. Since chat.message fires before any code that reads sessionTotals, this ensures the correct agent is always available.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Chore (dependency updates, etc.)

Checklist

  • I have read the CONTRIBUTING.md document
  • My code follows the style guidelines of this project
  • bun run lint passes with no errors
  • bun run check:jsdoc-coverage passes with no errors
  • bun run typecheck passes with no errors
  • bun test passes with no errors
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly: no need to update document
  • My commits follow the Conventional Commits specification

Related issues

#72

Additional context

image

Summary by CodeRabbit

  • Bug Fixes
    • Fixed chat session initialization to properly handle edge cases where session data is absent, ensuring more reliable and consistent tracking of chat sessions.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

In src/index.ts, the "chat.message" handler is updated to handle the case where sessionTotals has no entry for input.sessionID. It now initializes a new totals object when one is absent and conditionally updates totals.agent only when input.agent is provided.

Changes

Session Totals Initialization

Layer / File(s) Summary
chat.message handler: sessionTotals init and conditional agent update
src/index.ts
Adds a branch to check if sessionTotals.get(input.sessionID) returns a value; if missing, creates and stores a new totals object with startMs: Date.now(), zeroed token/cost/message counters, agent, and agentType: "primary"; if present, updates totals.agent only when input.agent is defined.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related issues

  • [Bug]: Agent name shows "unknown" on grafana #72: The initialization of a missing sessionTotals entry directly addresses the reported bug where sessions that existed before the plugin process restarted would have no totals entry, causing agent metadata to be untracked and producing "unknown" agent names in telemetry signals.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix agent name showing unknown issue' directly summarizes the main change: fixing the bug where agent names were displaying as 'unknown' due to missing sessionTotals entries.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@dialupdisaster

Copy link
Copy Markdown
Contributor

Thanks for jumping on this. The unknown agent symptom is real, but after #74 merged I think there’s a more targeted way to fix it than initializing sessionTotals in chat.message.

Why I’m hesitant about the current approach:

  • sessionTotals is now effectively accounting state for session duration / token / cost totals
  • seeding it on a resumed pre-existing session with startMs: Date.now() and zero counters fixes the agent tag, but it also makes later session.* totals look authoritative even though they only reflect activity observed after the plugin attached

I think a cleaner fix in the current codebase would be:

  • keep sessionTotals strictly for totals
  • add a separate metadata cache, something like sessionAgents: Map<string, { agent: string; agentType: SessionAgentType }>
  • update that from chat.message / handleRunStarted
  • have getSessionAgentMeta() read from that cache first, then fall back to sessionTotals, then "unknown"

That would fix the agent-name bug without fabricating partial totals for resumed sessions.

If you want, I think that would still be a very nice contribution and a cleaner follow-up on top of the run-by-user-message tracing work in #74.

@dialupdisaster dialupdisaster added the bug Something isn't working label Jun 25, 2026
@chingjustwe

Copy link
Copy Markdown
Author

make sense, let me do it.

@chingjustwe

Copy link
Copy Markdown
Author

I found below code in #74 already fixed the "unknown" agent name bug:

      const startTime = Date.now()
      const existingTotals = sessionTotals.get(input.sessionID)
      const nextTotals: SessionTotals = {
        startMs: existingTotals?.startMs ?? startTime,
        tokens: existingTotals?.tokens ?? 0,
        cost: existingTotals?.cost ?? 0,
        messages: existingTotals?.messages ?? 0,
        agent,
        agentType: existingTotals?.agentType ?? "primary",
      }
      setBoundedMap(sessionTotals, input.sessionID, nextTotals)

I'm OK to close this PR and Issue #72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants