Repository: vercel/next.js
File: packages/next/src/trace/trace-uploader.ts (TypeScript)

Code before:

      if (
        // Always include root spans
        event.parentId === undefined ||
        shouldUploadFullTrace ||
        (mode === 'dev'
          ? DEV_ALLOWED_EVENTS.has(event.name)
          : BUILD_ALLOWED_EVENTS.has(event.name))
      ) {
        sessionTrace.push(event)
      }
    }
  }

  // Apply experimental flag inheritance to session trace
  const sessionTraceWithInheritance = sessionTrace.map((event) => {
    if (event.parentId !== undefined) {
      const parentFlags = spanExperimentalFlags.get(event.parentId)
      if (parentFlags && Object.keys(parentFlags).length > 0) {
        const inheritedTags: Record<string, unknown> = { ...event.tags }
        for (const [key, value] of Object.entries(parentFlags)) {
          // Only inherit if child doesn't already have this key
          if (!(key in inheritedTags)) {
            inheritedTags[key] = value
          }
        }
        return { ...event, tags: inheritedTags }
      }
    }
    return event
  })

  const body: TraceRequestBody = {
    metadata: {
      anonymousId,
      arch: os.arch(),
      commit,
      cpus: os.cpus().length,
      isTurboSession,
      mode,
      nextVersion,
      pkgName,
      platform: os.platform(),
      sessionId,
      experimentalFlags: sessionExperimentalFlags,
    },
    // The trace file can contain events spanning multiple sessions.
    // Only submit traces for the current session, as the metadata we send is
    // intended for this session only.
    traces: [sessionTraceWithInheritance],
  }

Review comment (suggestion):
could this be `const inheritedTags = {...parentFlags, ...event.tags}`

Code after:

      if (
        // Always include root spans
        event.parentId === undefined ||
        shouldUploadFullTrace ||
        (mode === 'dev'
          ? DEV_ALLOWED_EVENTS.has(event.name)
          : BUILD_ALLOWED_EVENTS.has(event.name))
      ) {
        sessionTrace.push(event)
      }
    }
  }

  // Apply feature tag inheritance to session trace
  const sessionTraceWithInheritance = sessionTrace.map((event) => {
    if (event.parentId !== undefined) {
      const parentFlags = spanEnabledFeatures.get(event.parentId)
      if (parentFlags && Object.keys(parentFlags).length > 0) {
        // Inherit parent flags, but child's own tags take precedence
        return { ...event, tags: { ...parentFlags, ...event.tags } }
      }
    }
    return event
  })

  const body: TraceRequestBody = {
    metadata: {
      anonymousId,
      arch: os.arch(),
      commit,
      cpus: os.cpus().length,
      isTurboSession,
      mode,
      nextVersion,
      pkgName,
      platform: os.platform(),
      sessionId,
      enabledFeatures: sessionEnabledFeatures,
    },
    // The trace file can contain events spanning multiple sessions.
    // Only submit traces for the current session, as the metadata we send is
    // intended for this session only.
    traces: [sessionTraceWithInheritance],
  }

  if (isDebugEnabled) {
    console.log('Sending request with body', JSON.stringify(body, null, 2))
  }

  let res = await fetch(traceUploadUrl, {