(controller)
| 157 | |
| 158 | return new ReadableStream({ |
| 159 | async start(controller) { |
| 160 | publisher.attach(controller) |
| 161 | |
| 162 | // Re-enter the root OTel context — ALS doesn't survive the |
| 163 | // Next handler → ReadableStream.start boundary. |
| 164 | await otelContextApi.with(activeOtelRoot.context, async () => { |
| 165 | const otelContext = activeOtelRoot.context |
| 166 | let rootOutcome: CopilotLifecycleOutcome = RequestTraceV1Outcome.error |
| 167 | let rootError: unknown |
| 168 | // `cancelReason` must be declared OUTSIDE the outer `try` so |
| 169 | // it remains in scope for the outer `finally` that calls |
| 170 | // `activeOtelRoot.finish(rootOutcome, rootError, cancelReason)`. |
| 171 | // `let` bindings declared inside a `try` block are NOT visible |
| 172 | // in the paired `finally`; referencing one there raises a |
| 173 | // TDZ ReferenceError, skipping `finish()`, leaving the root |
| 174 | // span never-ended, and making Tempo see every child as an |
| 175 | // orphan under a phantom parent. (Regression landed 2026-04-21.) |
| 176 | let cancelReason: CopilotRequestCancelReasonValue | undefined |
| 177 | try { |
| 178 | const requestSpan = collector.startSpan('Sim Agent Request', 'request', { |
| 179 | streamId, |
| 180 | chatId, |
| 181 | runId, |
| 182 | }) |
| 183 | let outcome: CopilotLifecycleOutcome = RequestTraceV1Outcome.error |
| 184 | let lifecycleResult: |
| 185 | | { |
| 186 | usage?: { prompt: number; completion: number } |
| 187 | cost?: { input: number; output: number; total: number } |
| 188 | } |
| 189 | | undefined |
| 190 | |
| 191 | await Promise.all([resetBuffer(streamId), clearFilePreviewSessions(streamId)]) |
| 192 | |
| 193 | if (chatId) { |
| 194 | createRunSegment({ |
| 195 | id: runId, |
| 196 | executionId, |
| 197 | chatId, |
| 198 | userId, |
| 199 | workflowId: (requestPayload.workflowId as string | undefined) || null, |
| 200 | workspaceId, |
| 201 | streamId, |
| 202 | model: (requestPayload.model as string | undefined) || null, |
| 203 | provider: (requestPayload.provider as string | undefined) || null, |
| 204 | requestContext: { requestId }, |
| 205 | }).catch((error) => { |
| 206 | logger.warn(`[${requestId}] Failed to create copilot run segment`, { |
| 207 | error: getErrorMessage(error), |
| 208 | }) |
| 209 | }) |
| 210 | } |
| 211 | |
| 212 | const abortPoller = startAbortPoller(streamId, abortController, { |
| 213 | requestId, |
| 214 | chatId, |
| 215 | }) |
| 216 | publisher.startKeepalive() |
nothing calls this directly
no test coverage detected