fix: keep root session span open across session.idle#64
Conversation
The opencode.session span and accumulated session totals were ended and deleted on the first session.idle event. Since a session keeps receiving turns after going idle, every subsequent turn's LLM/tool spans had no local session span to nest under and became orphaned root traces, and their token/cost contributions were silently dropped from session.total_*. Keep the session span and totals alive across session.idle (only snapshotting attributes), and finalize them in a new session.deleted handler, on session.error, or on process shutdown.
📝 WalkthroughWalkthroughThis PR extends session span lifetime across idle events instead of closing them, deferring final cleanup to an explicit deletion handler. A new ChangesSession Lifecycle Management
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/index.ts (1)
145-147: ⚡ Quick winRemove newly added inline comments in TypeScript source.
These
//comments violate the repository rule for**/*.ts; please move this explanation to PR/docs or keep it in JSDoc where applicable.Suggested cleanup
- // Session spans are kept open across `session.idle` so later turns nest under them - // (see handleSessionIdle). On process exit, end any still-open session spans so - // they're flushed rather than dropped. for (const [sessionID, sessionSpan] of sessionSpans) {As per coding guidelines:
**/*.ts: "Do not include comments unless explicitly requested".🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/index.ts` around lines 145 - 147, Remove the newly added inline // comments that explain session span behavior near the process-exit handling (the comments describing "Session spans are kept open across `session.idle`..." and "On process exit, end any still-open session spans..."). Delete these two-line inline comments in the vicinity of the process exit / session flush code and either move that explanation into PR/docs or into a proper JSDoc block attached to the relevant symbol (e.g., the handleSessionIdle function or the session management module) if in-source documentation is required.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/index.ts`:
- Around line 145-147: Remove the newly added inline // comments that explain
session span behavior near the process-exit handling (the comments describing
"Session spans are kept open across `session.idle`..." and "On process exit, end
any still-open session spans..."). Delete these two-line inline comments in the
vicinity of the process exit / session flush code and either move that
explanation into PR/docs or into a proper JSDoc block attached to the relevant
symbol (e.g., the handleSessionIdle function or the session management module)
if in-source documentation is required.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 23114d65-ac82-49dd-845b-fab4325c085e
📒 Files selected for processing (4)
src/handlers/session.tssrc/index.tstests/handlers/session.test.tstests/handlers/spans.test.ts
Summary
opencode.sessionspan and accumulated session totals were ended and deleted on the firstsession.idleevent. Since a session keeps receiving turns after going idle, every subsequent turn's LLM/tool spans had no local session span to nest under and became orphaned root traces, with their token/cost contributions silently dropped fromsession.total_*.session.idle(only snapshotting attributes), and finalize them in a newsession.deletedhandler, onsession.error, or on process shutdown.Test plan
bun test tests/handlers/session.test.ts tests/handlers/spans.test.ts(87 pass)bun run typecheckbun --bun eslint src/handlers/session.ts src/index.tsSummary by CodeRabbit
Release Notes
New Features
Bug Fixes