Skip to content

fix(trace-opencode): lazy-init SessionState in chat.message for API-created sessions#5

Open
ekeith (evanmkeith) wants to merge 1 commit intomainfrom
04-06-fix(trace-opencode)-lazy-init-session-state-for-API-created-sessions
Open

fix(trace-opencode): lazy-init SessionState in chat.message for API-created sessions#5
ekeith (evanmkeith) wants to merge 1 commit intomainfrom
04-06-fix(trace-opencode)-lazy-init-session-state-for-API-created-sessions

Conversation

@evanmkeith
Copy link
Copy Markdown

Summary

Sessions created via the OpenCode HTTP API (POST /sessions) were silently dropping all trace data. OpenCode publishes session.created internally but does not deliver it to
plugins for API-created sessions. Since the plugin initializes SessionState exclusively in the session.created handler, every subsequent hook (chat.message,
message.part.updated, message.updated, tool.execute.*) found no state and returned early — resulting in complete, silent data loss.

Changes

  • src/tracing.tschat.message hook: change const statelet state; replace the if (!state) { return } early-exit with lazy initialization that creates a root span and SessionState on demand, identical to the session.created root-session path
  • src/tracing.tssession.created handler: add a guard (if (sessionStates.has(sessionKey)) return) so a late-arriving session.created event doesn't overwrite an already lazy-initialized state and orphan the root span

Testing

  • API-created session: POST /sessions → send message via API → spans appear in Braintrust
  • Interactive session (regression): open OpenCode normally → send message → traces unchanged
  • Bump version to 0.0.6 in package.json

Notes

startTime for lazily-initialized sessions is set at the first chat.message rather than true session creation. This is acceptable — API-created sessions have no meaningful
"open" period before the first message.

Fixes BT-4649.

…reated sessions

## Summary

  Sessions created via the OpenCode HTTP API (`POST /sessions`) were silently dropping all trace data. OpenCode publishes `session.created` internally but does not deliver it to
  plugins for API-created sessions. Since the plugin initializes `SessionState` exclusively in the `session.created` handler, every subsequent hook (`chat.message`,
  `message.part.updated`, `message.updated`, `tool.execute.*`) found no state and returned early — resulting in complete, silent data loss.

  ## Changes

  - **`src/tracing.ts`** — `chat.message` hook: change `const state` → `let state`; replace the `if (!state) { return }` early-exit with lazy initialization that creates a root
  span and `SessionState` on demand, identical to the `session.created` root-session path
  - **`src/tracing.ts`** — `session.created` handler: add a guard (`if (sessionStates.has(sessionKey)) return`) so a late-arriving `session.created` event doesn't overwrite an
  already lazy-initialized state and orphan the root span

  ## Testing

  - [ ] API-created session: `POST /sessions` → send message via API → spans appear in Braintrust
  - [ ] Interactive session (regression): open OpenCode normally → send message → traces unchanged
  - [ ] Bump version to `0.0.6` in `package.json`

  ## Notes

  `startTime` for lazily-initialized sessions is set at the first `chat.message` rather than true session creation. This is acceptable — API-created sessions have no meaningful
  "open" period before the first message.

  Fixes BT-4649.
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