Skip to content

Fix null safety in response array loops and OpenCode method reference#532

Merged
rajbos merged 1 commit intomainfrom
fix/session-jsonl-null-safety
Mar 30, 2026
Merged

Fix null safety in response array loops and OpenCode method reference#532
rajbos merged 1 commit intomainfrom
fix/session-jsonl-null-safety

Conversation

@rajbos
Copy link
Copy Markdown
Owner

@rajbos rajbos commented Mar 30, 2026

Problem

Three distinct WARNING log entries appeared during backend sync:

  1. TypeError: tokenTracker.getOpenCodeSessionData is not a function — fired for every OpenCode session file during sync.
  2. TypeError: Cannot read properties of undefined (reading 'value') — from getModelUsageFromSession when processing VS Code Insiders JSONL delta-format files.
  3. TypeError: Cannot read properties of undefined (reading 'kind') — from analyzeSessionUsage / trackEnhancedMetrics for the same files.

Root Causes

1. Wrong receiver for getOpenCodeSessionData (extension.ts)

The backend facade wired getOpenCodeSessionData by calling it on tokenTracker directly, but the method lives on tokenTracker.openCode (the OpenCodeDataAccess instance):

// Before (broken)
(tokenTracker as any).getOpenCodeSessionData(sessionFile)

// After (fixed)
(tokenTracker as any).openCode.getOpenCodeSessionData(sessionFile)

2. Null entries in reconstructed response arrays (usageAnalysis.ts)

VS Code Copilot Chat now stores some sessions as JSONL delta streams ({"kind":0/1/2,...}). When the applyDelta reconstruction rebuilds requests[].response, the resulting array can contain null/undefined entries. Multiple loops iterated over these arrays and accessed .kind or .value without guarding for nulls.

Fixes

File Change
extension.ts Route getOpenCodeSessionData through tokenTracker.openCode
usageAnalysis.ts Add if (!responseItem) continue guard in 5 response-array loops (analyzeSessionUsage delta-path x2, JSON-path x1, trackEnhancedMetrics x2)
usageAnalysis.ts Use responseItem?.value (optional chain) instead of responseItem.value in getModelUsageFromSession (x2)

Notes on other logged messages

  • "spans 2 days" — informational log() messages (not warnings), correctly noting a session file spans midnight. No change needed.
  • type=undefined — debug log for the first 3 lines of today's JSONL files; VS Code delta-format events use kind not type, so event.type is undefined. The sync service already skips those lines harmlessly. No change needed.

- Fix TypeError: getOpenCodeSessionData was called on tokenTracker directly
  instead of tokenTracker.openCode where the method actually lives; this
  caused 'tokenTracker.getOpenCodeSessionData is not a function' warnings
  during backend sync for every OpenCode session file.

- Fix TypeError: Cannot read properties of undefined (reading 'value')
  in getModelUsageFromSession: response arrays reconstructed from VS Code
  delta-based JSONL can contain null/undefined entries; use optional
  chaining (responseItem?.value) instead of bare property access.

- Fix TypeError: Cannot read properties of undefined (reading 'kind')
  in analyzeSessionUsage and trackEnhancedMetrics: add an explicit
  'if (!responseItem) continue' guard at the top of every loop that
  iterates over request.response or event.v arrays, covering both the
  delta-path and JSON-path code branches.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rajbos rajbos merged commit 7bdfe49 into main Mar 30, 2026
18 checks passed
@rajbos rajbos deleted the fix/session-jsonl-null-safety branch March 30, 2026 20:38
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