fix(trace-opencode): lazy-init SessionState in chat.message for API-created sessions#5
Open
ekeith (evanmkeith) wants to merge 1 commit intomainfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sessions created via the OpenCode HTTP API (
POST /sessions) were silently dropping all trace data. OpenCode publishessession.createdinternally but does not deliver it toplugins for API-created sessions. Since the plugin initializes
SessionStateexclusively in thesession.createdhandler, 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.messagehook: changeconst state→let state; replace theif (!state) { return }early-exit with lazy initialization that creates a root span andSessionStateon demand, identical to thesession.createdroot-session pathsrc/tracing.ts—session.createdhandler: add a guard (if (sessionStates.has(sessionKey)) return) so a late-arrivingsession.createdevent doesn't overwrite an already lazy-initialized state and orphan the root spanTesting
POST /sessions→ send message via API → spans appear in Braintrust0.0.6inpackage.jsonNotes
startTimefor lazily-initialized sessions is set at the firstchat.messagerather than true session creation. This is acceptable — API-created sessions have no meaningful"open" period before the first message.
Fixes BT-4649.