Skip to content

Link V2 child sessions prompted before their creation event - #56

Open
JoshuaMart wants to merge 1 commit into
langfuse:mainfrom
JoshuaMart:fix/v2-subagent-parent
Open

JoshuaMart wants to merge 1 commit into
langfuse:mainfrom
JoshuaMart:fix/v2-subagent-parent

Conversation

@JoshuaMart

Copy link
Copy Markdown

Problem

With OpenCode 2, a subagent session is not nested under its parent's trace. It starts a separate root trace, and its opencode.turn metadata has no parentSessionID.

In src/v2.ts, the parent of a session is learned only from the session.created event, which the background loop over ctx.event.subscribe() handles. The turn is opened earlier, in the prompt hook (traceUserPrompt → getSessionParentSpan). OpenCode prompts a child session right after creating it, so the prompt hook runs before the plugin has processed session.created. sessionParentIds has 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

  • In the v2 prompt hook, if the session's parent has not been resolved yet, fetch the session with ctx.session.get({ sessionID }) and call rememberSessionParent with its parentID before traceUserPrompt.
  • Each session is resolved once per plugin instance, through either session.created or the lookup. Root sessions don't trigger a lookup on every prompt. The set is cleared on session.deleted.
  • Fail open: if the lookup throws, the plugin logs a warning and keeps the previous behaviour.
  • The session.created path is unchanged.

Tests

  • New v2 integration test, "links V2 child sessions prompted before their creation event to the parent trace". The child's prompt hook runs while the parent generation is active and before the child's session.created event. The test asserts that the child turn shares the parent's trace, is parented under the parent's generation, and has parentSessionID in its metadata. It fails without the fix: the child turn has a different trace ID.
  • createPluginContext takes an optional getSession. 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 knip and pnpm run format:check are clean.

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>
@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants