Skip to content

fix(streaming): pass agentId to resolveStorePath to fix non-default agent footer metrics#386

Closed
smallmj wants to merge 2 commits into
larksuite:mainfrom
smallmj:fix/footer-metrics-agent-session-path
Closed

fix(streaming): pass agentId to resolveStorePath to fix non-default agent footer metrics#386
smallmj wants to merge 2 commits into
larksuite:mainfrom
smallmj:fix/footer-metrics-agent-session-path

Conversation

@smallmj
Copy link
Copy Markdown

@smallmj smallmj commented Apr 4, 2026

Summary

Footer metrics (tokens/cache/context/model) were always missing for non-default agents because resolveStorePath() was called without passing the agentId, causing the SDK to default to 'main' and look up the wrong per-agent session store file.

Root Cause

The SDK's resolveStorePath(store, opts?: { agentId?: string }) was being called as:
resolveStorePath(sessionStorePath) — missing the agentId argument.

This means regardless of which agent handled the request, the metrics lookup always went to ~/.openclaw/agents/main/sessions/sessions.json instead of the correct agent's session file.

Fix

Extract agentId from sessionKey (format: 'agent:<agentId>:feishu:...') and pass it to both resolveStorePath() calls (runtime.agent.session path and channel.session path).

Change Type

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope

  • Gateway / orchestration (streaming card controller)
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Compatibility

  • Backward compatible: Yes — if agentId is undefined (malformed sessionKey), it falls back to the default behavior, exactly as before.
  • Config changes: None
  • Migration needed: None

Linked Issue

Testing

  • All 141 existing tests pass ✓
  • Build succeeds ✓
  • The fix is additive: if agentId is undefined (non-standard sessionKey), it gracefully degrades to the old behavior

…gent footer metrics

Footer metrics (tokens/cache/context/model) were always missing for
non-default agents because resolveStorePath() was called without passing
the agentId, causing the SDK to default to 'main' and look up the wrong
per-agent session store file.

Fix: extract agentId from sessionKey (format: 'agent:<agentId>:...')
and pass it to resolveStorePath() in both the runtime.agent.session
and channel.session code paths.

Fixes larksuite#347
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 4, 2026

CLA assistant check
All committers have signed the CLA.

…agents

The fallback mechanism was wrong because:
1. It tried to read from the default agent's session file when the correct
   agent's file didn't have the entry
2. This resulted in all non-default agents showing the same metrics (from
   the main agent's session), which is incorrect

The agentId-based routing via resolveStorePath is sufficient to correctly
read metrics from each agent's own session file.
@smallmj
Copy link
Copy Markdown
Author

smallmj commented Apr 16, 2026

为什么删除 fallback 逻辑

PR #386 原本包含了 fallback 机制:当在当前 agent 的 session 文件中找不到 metrics 时,尝试去 default agent (main) 的 session 文件中查找。

问题在于: 这个 fallback 找到了错误的数据。

经过本地验证:

  • 每个 agent 的 metrics 正确写入了自己的 session 文件(如 的 metrics 在 )
  • 但 fallback 会去 的 session 文件中查找,并返回找到的数据
  • 导致所有非 main agent 都显示 main agent 的 metrics 值

结论: fallback 是冗余且有害的。 路由机制已经能正确将读操作定向到对应 agent 的 session 文件,不需要 fallback 来读取其他 agent 的数据。

删除 fallback 后,每个 agent 现在显示的是自己真实的 metrics 值。

@smallmj
Copy link
Copy Markdown
Author

smallmj commented Apr 16, 2026

This PR has been replaced by the new PR #422 which is based on the latest main branch with a cleaner history.

The fix in #422 has been locally verified to work correctly - each non-default agent now shows its own metrics instead of all showing the same values from the main agent.

Please close this PR in favor of #422. Thanks!

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.

Footer metrics (tokens/cache/context/model) always missing for non-default agents

2 participants