Skip to content

fix: keep root session span open across session.idle#64

Open
schemaitat wants to merge 1 commit into
DEVtheOPS:mainfrom
schemaitat:fix/session-span-spans-whole-session
Open

fix: keep root session span open across session.idle#64
schemaitat wants to merge 1 commit into
DEVtheOPS:mainfrom
schemaitat:fix/session-span-spans-whole-session

Conversation

@schemaitat

@schemaitat schemaitat commented Jun 13, 2026

Copy link
Copy Markdown

Summary

  • 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, with their token/cost contributions 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.

Test plan

  • bun test tests/handlers/session.test.ts tests/handlers/spans.test.ts (87 pass)
  • bun run typecheck
  • bun --bun eslint src/handlers/session.ts src/index.ts

Summary by CodeRabbit

Release Notes

  • New Features

    • Sessions now retain accumulated data across idle periods, improving continuity for multi-turn interactions.
    • Added proper session cleanup and finalization when sessions terminate or the application shuts down.
  • Bug Fixes

    • Session telemetry is now correctly preserved and spans are properly closed during application shutdown.

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.
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR extends session span lifetime across idle events instead of closing them, deferring final cleanup to an explicit deletion handler. A new handleSessionDeleted event handler ends the session span on session.deleted, the dispatcher routes deletion events to this handler, and shutdown cleanup closes any remaining spans on process exit.

Changes

Session Lifecycle Management

Layer / File(s) Summary
Session lifecycle: idle retention and deletion handler
src/handlers/session.ts
handleSessionIdle retains session totals and span across idle events instead of finalizing them; new handleSessionDeleted handler performs final cleanup by deleting totals, sweeping pending state, and ending the session span with OK status.
Event handler registration
src/index.ts
Imports EventSessionDeleted and registers the new handleSessionDeleted handler for "session.deleted" events in the dispatcher.
Shutdown procedure: lingering span cleanup
src/index.ts
Plugin shutdown now iterates remaining open session spans, applies final totals from sessionTotals, sets status OK, ends each span, and clears the map to prevent dropped telemetry.
Session handler tests: idle and deleted behavior
tests/handlers/session.test.ts
Updated handleSessionIdle test expectations to verify sessionTotals are retained across idle; added new test suite for handleSessionDeleted verifying totals deletion and span closure.
Span lifecycle tests: multi-turn and deletion coverage
tests/handlers/spans.test.ts
Extended span tests to verify session spans remain unended after idle, accept child LLM spans in subsequent turns, and are properly closed with status OK and totals cleared on session.deleted.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • DEVtheOPS/opencode-plugin-otel#8: Both PRs modify src/handlers/session.ts session lifecycle around ctx.sessionTotals and handleSessionIdle instrumentation, overlapping in the same session-totals code paths.
  • DEVtheOPS/opencode-plugin-otel#44: Both PRs update session lifecycle cleanup logic to manage ctx session-level diff totals across idle/deletion, overlapping in telemetry state retention logic.

Suggested reviewers

  • dialupdisaster
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: keeping the root session span open across session.idle events instead of closing it prematurely.
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 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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/index.ts (1)

145-147: ⚡ Quick win

Remove 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

📥 Commits

Reviewing files that changed from the base of the PR and between 7153352 and d3ed25f.

📒 Files selected for processing (4)
  • src/handlers/session.ts
  • src/index.ts
  • tests/handlers/session.test.ts
  • tests/handlers/spans.test.ts

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