Repository navigation
Link V2 child sessions prompted before their creation event - #56
Open
JoshuaMart wants to merge 1 commit into
Open
JoshuaMart wants to merge 1 commit into
JoshuaMart wants to merge 1 commit into
Conversation
OpenCode 2 prompts a subagent session right after creating it, so the prompt hook can run before the plugin's event loop has processed session.created. The child turn then had no known parent and started a separate root trace. Look up the session with ctx.session.get in the prompt hook when its parent is not known yet, and fall back to the previous behaviour if the lookup fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
With OpenCode 2, a subagent session is not nested under its parent's trace. It starts a separate root trace, and its
opencode.turnmetadata has noparentSessionID.In
src/v2.ts, the parent of a session is learned only from thesession.createdevent, which the background loop overctx.event.subscribe()handles. The turn is opened earlier, in theprompthook (traceUserPrompt→getSessionParentSpan). OpenCode prompts a child session right after creating it, so the prompt hook runs before the plugin has processedsession.created.sessionParentIdshas no entry yet, and the child turn becomes a root.Reproduced on OpenCode 2.1.2 with a primary agent delegating to a subagent. The v1 integration test "links child agent sessions to the parent trace" covers v1 only.
Change
prompthook, if the session's parent has not been resolved yet, fetch the session withctx.session.get({ sessionID })and callrememberSessionParentwith itsparentIDbeforetraceUserPrompt.session.createdor the lookup. Root sessions don't trigger a lookup on every prompt. The set is cleared onsession.deleted.session.createdpath is unchanged.Tests
prompthook runs while the parent generation is active and before the child'ssession.createdevent. The test asserts that the child turn shares the parent's trace, is parented under the parent's generation, and hasparentSessionIDin its metadata. It fails without the fix: the child turn has a different trace ID.createPluginContexttakes an optionalgetSession. Existing tests keep the throwing mock, so they go through the fail-open path.pnpm run test:integration(build + typecheck + vitest): 63/63 pass.pnpm run lint,pnpm run knipandpnpm run format:checkare clean.