@oakgreencc/cambium 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,522 @@
1
+ // @ts-check
2
+ /**
3
+ * OpenTelemetry for cambium: `cambium.node` spans per step-node run,
4
+ * `cambium.wait` spans per wait period (emitted when it ends), a root
5
+ * `cambium.issue` span per issue trace, and a `cambium.wait.not_ready` counter.
6
+ *
7
+ * Configured only by the standard `OTEL_*` env vars, with mycelium's on/off
8
+ * rule (its `createTracing` is reused with `service.name=cambium`): no OTLP
9
+ * endpoint, `OTEL_SDK_DISABLED=true` or `OTEL_TRACES_EXPORTER=none` → no-op,
10
+ * and the SDK is never imported. Metrics follow the same rule for their own
11
+ * signal (`OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`, `OTEL_METRICS_EXPORTER`).
12
+ *
13
+ * No span stays open across ticks: the issue root is ended as soon as it is
14
+ * started (its traceparent, stored in the marker, is what later ticks parent
15
+ * on), and a wait span is created only once the wait is over, back-dated to
16
+ * the marker's `enteredAt`.
17
+ */
18
+ import { metrics, ROOT_CONTEXT, SpanStatusCode, trace } from '@opentelemetry/api'
19
+ import {
20
+ createTracing,
21
+ formatTraceparent,
22
+ NO_TRACING,
23
+ otlpConfigured,
24
+ parseTraceparent,
25
+ spanContextOf,
26
+ } from '@oakgreencc/mycelium'
27
+
28
+ export const SERVICE_NAME = 'cambium'
29
+
30
+ /**
31
+ * @typedef {import('@opentelemetry/api').Span} Span
32
+ * @typedef {import('@opentelemetry/api').SpanContext} SpanContext
33
+ * @typedef {import('@opentelemetry/api').Counter} Counter
34
+ * @typedef {{
35
+ * graph?: string, graphVersion?: number, node?: string, attempt?: number,
36
+ * repo?: string, issue?: number, edge?: number | string | null, outcome?: string,
37
+ * }} NodeAttrs
38
+ * @typedef {NodeAttrs & {
39
+ * traceparent?: string | null,
40
+ * issueBody?: string | null,
41
+ * }} TraceInfo
42
+ * `traceparent` is the marker's; `issueBody` lets a new issue trace continue mycelium's intake trace.
43
+ * @typedef {{
44
+ * span: Span,
45
+ * issueTraceparent: string | null,
46
+ * traceparent: string | null,
47
+ * set: (attrs: NodeAttrs) => void,
48
+ * }} NodeRun
49
+ * `issueTraceparent` is what the marker should hold (new when a root was started); `traceparent` is this
50
+ * span's, for a worker's `TRACEPARENT`.
51
+ */
52
+
53
+ /** W3C `traceparent` of a span context, or null for an invalid (no-op) one. @param {SpanContext | undefined} sc */
54
+ export const traceparentFor = (sc) => (sc ? formatTraceparent(sc) : null)
55
+
56
+ /** @param {NodeAttrs} a */
57
+ function attributes(a) {
58
+ /** @type {Record<string, string | number>} */
59
+ const out = {}
60
+ const put = (/** @type {string} */ k, /** @type {unknown} */ v) => {
61
+ if (v !== undefined && v !== null) out[k] = /** @type {string | number} */ (v)
62
+ }
63
+ put('cambium.graph', a.graph)
64
+ put('cambium.graph.version', a.graphVersion)
65
+ put('cambium.node', a.node)
66
+ put('cambium.attempt', a.attempt)
67
+ put('cambium.repo', a.repo)
68
+ put('cambium.issue.number', a.issue)
69
+ put('cambium.edge', a.edge)
70
+ put('cambium.outcome', a.outcome)
71
+ return out
72
+ }
73
+
74
+ /** Metrics on/off for the metrics signal, mirroring mycelium's traces rule. @param {Record<string, string | undefined>} env */
75
+ export function otlpMetricsConfigured(env) {
76
+ if (
77
+ String(env.OTEL_SDK_DISABLED ?? '')
78
+ .trim()
79
+ .toLowerCase() === 'true'
80
+ )
81
+ return false
82
+ const exporters = String(env.OTEL_METRICS_EXPORTER ?? 'otlp')
83
+ .split(',')
84
+ .map((s) => s.trim().toLowerCase())
85
+ if (exporters.includes('none')) return false
86
+ return Boolean(env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT || env.OTEL_EXPORTER_OTLP_ENDPOINT)
87
+ }
88
+
89
+ /**
90
+ * @param {{
91
+ * env?: Record<string, string | undefined>,
92
+ * exporter?: import('@opentelemetry/sdk-trace').SpanExporter,
93
+ * metricReader?: import('@opentelemetry/sdk-metrics').MetricReader,
94
+ * }} [opts]
95
+ */
96
+ async function createMeter({ env = process.env, metricReader } = {}) {
97
+ if (!metricReader && !otlpMetricsConfigured(env)) return null
98
+ const [sdk, res] = await Promise.all([import('@opentelemetry/sdk-metrics'), import('@opentelemetry/resources')])
99
+ const reader =
100
+ metricReader ??
101
+ new sdk.PeriodicExportingMetricReader({
102
+ exporter: new (await import('@opentelemetry/exporter-metrics-otlp-http')).OTLPMetricExporter(),
103
+ })
104
+ const resource = res
105
+ .defaultResource()
106
+ .merge(res.resourceFromAttributes({ 'service.name': SERVICE_NAME }))
107
+ .merge(res.detectResources({ detectors: [res.envDetector] }))
108
+ const provider = new sdk.MeterProvider({ resource, readers: [reader] })
109
+ return { provider, meter: provider.getMeter(SERVICE_NAME) }
110
+ }
111
+
112
+ /**
113
+ * cambium's telemetry. Without OTLP config (and no injected exporter/reader) everything is a no-op that costs a
114
+ * function call: `nodeSpan` just runs `fn`, traceparents are null, so nothing is written to the marker.
115
+ *
116
+ * @param {{
117
+ * env?: Record<string, string | undefined>,
118
+ * exporter?: import('@opentelemetry/sdk-trace').SpanExporter,
119
+ * metricReader?: import('@opentelemetry/sdk-metrics').MetricReader,
120
+ * now?: () => number,
121
+ * }} [opts]
122
+ */
123
+ export async function createTelemetry(opts = {}) {
124
+ const env = opts.env ?? process.env
125
+ const [tracing, metered] = await Promise.all([
126
+ opts.exporter || otlpConfigured(env)
127
+ ? createTracing({ env, exporter: opts.exporter, serviceName: SERVICE_NAME })
128
+ : NO_TRACING,
129
+ createMeter({ ...opts, env }),
130
+ ])
131
+ const { tracer } = tracing
132
+ const now = opts.now ?? Date.now
133
+ /** @type {Counter} */
134
+ const notReadyCounter = (metered?.meter ?? metrics.getMeter('noop')).createCounter('cambium.wait.not_ready', {
135
+ description: 'Polls of a wait node that found it not ready',
136
+ })
137
+
138
+ /**
139
+ * The context to parent on: the marker's traceparent; failing that a new `cambium.issue` root (a child of the
140
+ * intake trace when the issue body carries mycelium's traceparent), ended at once.
141
+ * @param {TraceInfo} info
142
+ */
143
+ function issueTrace(info) {
144
+ const known = spanContextOf(info.traceparent)
145
+ if (known) return { parent: trace.setSpanContext(ROOT_CONTEXT, known), traceparent: known.traceparent }
146
+ const intake = parseTraceparent(info.issueBody)
147
+ const root = tracer.startSpan(
148
+ 'cambium.issue',
149
+ {
150
+ root: !intake,
151
+ attributes: attributes({ graph: info.graph, repo: info.repo, issue: info.issue }),
152
+ startTime: now(),
153
+ },
154
+ intake ? trace.setSpanContext(ROOT_CONTEXT, intake) : ROOT_CONTEXT,
155
+ )
156
+ root.end()
157
+ const traceparent = traceparentFor(root.spanContext())
158
+ return { parent: trace.setSpan(ROOT_CONTEXT, root), traceparent }
159
+ }
160
+
161
+ /**
162
+ * One `cambium.node` span around a step-node run. `fn` gets the span, the issue traceparent the marker should
163
+ * hold, and this span's traceparent (pass it to a worker as `TRACEPARENT`); `run.set({ edge, outcome })` adds
164
+ * attributes known only after the run. A throw marks the span an error (outcome `error`) and is rethrown.
165
+ *
166
+ * @template T
167
+ * @param {TraceInfo} info
168
+ * @param {(run: NodeRun) => T | Promise<T>} fn
169
+ * @returns {Promise<T>}
170
+ */
171
+ async function nodeSpan(info, fn) {
172
+ const { parent, traceparent: issueTraceparent } = issueTrace(info)
173
+ const span = tracer.startSpan('cambium.node', { attributes: attributes(info), startTime: now() }, parent)
174
+ let outcome = info.outcome !== undefined
175
+ /** @type {NodeRun} */
176
+ const run = {
177
+ span,
178
+ issueTraceparent,
179
+ traceparent: traceparentFor(span.spanContext()),
180
+ set: (a) => {
181
+ if (a.outcome !== undefined) outcome = true
182
+ span.setAttributes(attributes(a))
183
+ },
184
+ }
185
+ try {
186
+ const out = await fn(run)
187
+ if (!outcome) span.setAttribute('cambium.outcome', 'ok')
188
+ return out
189
+ } catch (err) {
190
+ span.recordException(/** @type {Error} */ (err))
191
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(/** @type {any} */ (err)?.message ?? err) })
192
+ span.setAttribute('cambium.outcome', 'error')
193
+ throw err
194
+ } finally {
195
+ span.end(now())
196
+ }
197
+ }
198
+
199
+ /**
200
+ * A `cambium.wait` span for a wait period that just ended: `startTime` = `enteredAt` (the marker's), end = now.
201
+ * Returns the issue traceparent (non-null and new when a root had to be started).
202
+ *
203
+ * @param {TraceInfo & { enteredAt?: string | number | Date | null, polls?: number }} info
204
+ */
205
+ function waitSpan(info) {
206
+ const { parent, traceparent } = issueTrace(info)
207
+ const end = now()
208
+ const start = info.enteredAt == null ? end : new Date(info.enteredAt).getTime()
209
+ const span = tracer.startSpan(
210
+ 'cambium.wait',
211
+ { attributes: attributes(info), startTime: Number.isNaN(start) ? end : Math.min(start, end) },
212
+ parent,
213
+ )
214
+ if (info.polls !== undefined) span.setAttribute('cambium.wait.polls', info.polls)
215
+ span.end(end)
216
+ return traceparent
217
+ }
218
+
219
+ /** A wait was polled and is not ready. @param {NodeAttrs} [info] */
220
+ const notReady = (info = {}) =>
221
+ notReadyCounter.add(1, attributes({ graph: info.graph, node: info.node, repo: info.repo }))
222
+
223
+ return {
224
+ enabled: tracing.enabled,
225
+ metricsEnabled: Boolean(metered),
226
+ tracer,
227
+ issueTrace: (/** @type {TraceInfo} */ info) => issueTrace(info).traceparent,
228
+ nodeSpan,
229
+ waitSpan,
230
+ notReady,
231
+ traceparentFor,
232
+ /** {@link workerSpans} with this tracer. @param {Parameters<typeof workerSpans>[0]} stream @param {string | null} [parent] */
233
+ workerSpans: (stream, parent) => workerSpans(stream, { tracer, parent }),
234
+ now,
235
+ /** Exports what is queued now. */
236
+ flush: async () => {
237
+ await Promise.all([tracing.flush(), metered?.provider.forceFlush()])
238
+ },
239
+ /** Flushes, then stops; call it in a `finally`. Safe to call twice. */
240
+ shutdown: async () => {
241
+ await Promise.all([tracing.shutdown(), metered?.provider.shutdown().catch(() => {})])
242
+ },
243
+ }
244
+ }
245
+
246
+ /**
247
+ * @typedef {Awaited<ReturnType<typeof createTelemetry>>} Telemetry
248
+ */
249
+
250
+ /**
251
+ * An `advance` `instrument` hook (see `graph/advance.mjs`) that traces one advance from a marker position: step
252
+ * nodes run inside `nodeSpan` (and get `ctx.traceparent` for a worker's `TRACEPARENT`), a ready wait emits its
253
+ * `waitSpan` (from `enteredAt` for the first node, else from when the previous transition landed), a not-ready
254
+ * wait bumps the counter. `hooks.traceparent` is the issue traceparent to store in the marker afterwards.
255
+ *
256
+ * @param {Telemetry} telemetry
257
+ * @param {TraceInfo & { enteredAt?: string | null }} base the marker's graph, graphVersion, attempt, traceparent,
258
+ * enteredAt, plus repo, issue and optionally the issue body
259
+ */
260
+ export function advanceHooks(telemetry, base) {
261
+ let traceparent = base.traceparent ?? null
262
+ /** @type {string | number | null | undefined} */
263
+ let enteredAt = base.enteredAt
264
+ /** @type {import('./graph/advance.mjs').Instrument} */
265
+ const instrument = async (visit, step) => {
266
+ const info = { ...base, traceparent, node: visit.node }
267
+ if (visit.kind === 'wait') {
268
+ const res = await step()
269
+ if (!res) telemetry.notReady(info)
270
+ else {
271
+ traceparent = telemetry.waitSpan({ ...info, enteredAt, edge: res.edge, outcome: 'ready' }) ?? traceparent
272
+ enteredAt = telemetry.now()
273
+ }
274
+ return res
275
+ }
276
+ const res = await telemetry.nodeSpan(info, async (run) => {
277
+ traceparent = run.issueTraceparent ?? traceparent
278
+ const t = await step({ traceparent: run.traceparent })
279
+ run.set({ edge: t?.edge ?? null, outcome: outcomeOf(t?.output) })
280
+ return t
281
+ })
282
+ enteredAt = telemetry.now()
283
+ return res
284
+ }
285
+ return {
286
+ instrument,
287
+ get traceparent() {
288
+ return traceparent
289
+ },
290
+ }
291
+ }
292
+
293
+ /** A node output's own `outcome` string when it has one, else `ok`. @param {unknown} output */
294
+ const outcomeOf = (output) => {
295
+ const o = /** @type {any} */ (output)?.outcome
296
+ return typeof o === 'string' ? o : 'ok'
297
+ }
298
+
299
+ const COMMAND_MAX = 200
300
+
301
+ /** @param {unknown} s @param {number} [max] */
302
+ const clip = (s, max = COMMAND_MAX) => {
303
+ if (typeof s !== 'string') return undefined
304
+ return s.length > max ? `${s.slice(0, max - 1)}…` : s
305
+ }
306
+
307
+ /** @param {Record<string, unknown>} a */
308
+ const defined = (a) =>
309
+ /** @type {import('@opentelemetry/api').Attributes} */ (
310
+ Object.fromEntries(Object.entries(a).filter(([, v]) => v !== undefined && v !== null))
311
+ )
312
+
313
+ /** @param {string | Iterable<string | Record<string, any>>} stream @returns {Generator<Record<string, any>>} */
314
+ function* events(stream) {
315
+ const items = typeof stream === 'string' ? stream.split('\n') : stream
316
+ for (const item of items) {
317
+ if (typeof item !== 'string') {
318
+ if (item && typeof item === 'object') yield item
319
+ continue
320
+ }
321
+ if (!item.trim()) continue
322
+ try {
323
+ const e = JSON.parse(item)
324
+ yield e && typeof e === 'object' ? e : { type: '__malformed' }
325
+ } catch {
326
+ yield { type: '__malformed' }
327
+ }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * @typedef {{ id: string, name: string, input: any, start: number, end: number | null, error: boolean, parentToolId: string | null }} ToolUse
333
+ * @typedef {{ id: string, toolUseId: string | null, type?: string, description?: string, start: number, end: number | null, status?: string }} BgTask
334
+ * @typedef {{
335
+ * sessionId: string | null, complete: boolean, ok: boolean, start: number | null, end: number | null,
336
+ * tools: number, tasks: number, malformed: number, costUsd: number | null,
337
+ * }} WorkerSummary
338
+ */
339
+
340
+ /**
341
+ * Spans for one `claude -p --output-format stream-json --verbose` worker run, from its stdout log. Pure apart from
342
+ * the spans it emits (all already ended); call it once the worker has exited.
343
+ *
344
+ * - `claude.session`, child of `parent`, from the first to the last `timestamp` in the stream; attributes from the
345
+ * `result` event (cost, tokens, turns, terminal_reason, model) and `system/init` (claude_code version, session id).
346
+ * No `result` (a killed worker, a truncated log) or an error result → ERROR status.
347
+ * - `claude.tool` per `tool_use` → matching `tool_result` (a subagent's tools nest under its Agent/Task tool span),
348
+ * with the tool name, ok/error and, for Bash, the command clipped to 200 chars. Unpaired → ends at the last
349
+ * timestamp, ERROR.
350
+ * - `claude.task` per background task, `task_started` → `task_notification` (or `task_updated`'s `end_time`). These
351
+ * system events carry no timestamp, so their times are those of the latest timestamped event before them.
352
+ *
353
+ * Malformed lines are counted and skipped. Without any timestamp nothing is emitted.
354
+ *
355
+ * @param {string | Iterable<string | Record<string, any>>} stream the log text, its lines, or parsed events
356
+ * @param {{ tracer: import('@opentelemetry/api').Tracer, parent?: import('@opentelemetry/api').Context | string | null }} opts
357
+ * `parent`: the start-worker span's context or its traceparent
358
+ * @returns {WorkerSummary}
359
+ */
360
+ export function workerSpans(stream, { tracer, parent }) {
361
+ /** @type {number | null} */
362
+ let first = null
363
+ /** @type {number | null} */
364
+ let last = null
365
+ let malformed = 0
366
+ /** @type {any} */
367
+ let init = null
368
+ /** @type {any} */
369
+ let result = null
370
+ /** @type {Map<string, ToolUse>} */
371
+ const tools = new Map()
372
+ /** @type {Map<string, BgTask>} */
373
+ const tasks = new Map()
374
+
375
+ for (const e of events(stream)) {
376
+ if (e.type === '__malformed') {
377
+ malformed++
378
+ continue
379
+ }
380
+ const ts = typeof e.timestamp === 'string' ? Date.parse(e.timestamp) : Number.NaN
381
+ if (!Number.isNaN(ts)) {
382
+ first ??= ts
383
+ last = Math.max(last ?? ts, ts)
384
+ }
385
+ const at = Number.isNaN(ts) ? last : ts
386
+ const content = Array.isArray(e.message?.content) ? e.message.content : []
387
+ if (e.type === 'system' && e.subtype === 'init') init ??= e
388
+ else if (e.type === 'result') result = e
389
+ else if (e.type === 'assistant' && at !== null) {
390
+ for (const c of content)
391
+ if (c?.type === 'tool_use' && typeof c.id === 'string' && !tools.has(c.id))
392
+ tools.set(c.id, {
393
+ id: c.id,
394
+ name: String(c.name ?? 'unknown'),
395
+ input: c.input,
396
+ start: at,
397
+ end: null,
398
+ error: false,
399
+ parentToolId: typeof e.parent_tool_use_id === 'string' ? e.parent_tool_use_id : null,
400
+ })
401
+ } else if (e.type === 'user' && at !== null) {
402
+ for (const c of content) {
403
+ const t = c?.type === 'tool_result' ? tools.get(c.tool_use_id) : undefined
404
+ if (t && t.end === null) {
405
+ t.end = at
406
+ t.error = c.is_error === true
407
+ }
408
+ }
409
+ } else if (e.type === 'system' && typeof e.task_id === 'string' && last !== null) {
410
+ const known = tasks.get(e.task_id)
411
+ if (e.subtype === 'task_started' && !known)
412
+ tasks.set(e.task_id, {
413
+ id: e.task_id,
414
+ toolUseId: typeof e.tool_use_id === 'string' ? e.tool_use_id : null,
415
+ type: e.task_type,
416
+ description: clip(e.description),
417
+ start: last,
418
+ end: null,
419
+ })
420
+ else if (known && e.subtype === 'task_updated') {
421
+ const end = Number(e.patch?.end_time)
422
+ if (Number.isFinite(end) && end > 0) known.end = end
423
+ if (typeof e.patch?.status === 'string') known.status = e.patch.status
424
+ } else if (known && e.subtype === 'task_notification') {
425
+ known.end ??= last
426
+ if (typeof e.status === 'string') known.status = e.status
427
+ }
428
+ }
429
+ }
430
+
431
+ const sessionId = result?.session_id ?? init?.session_id ?? null
432
+ const ok = Boolean(result) && result.is_error !== true && result.subtype === 'success'
433
+ /** @type {WorkerSummary} */
434
+ const summary = {
435
+ sessionId,
436
+ complete: Boolean(result),
437
+ ok,
438
+ start: first,
439
+ end: last,
440
+ tools: tools.size,
441
+ tasks: tasks.size,
442
+ malformed,
443
+ costUsd: typeof result?.total_cost_usd === 'number' ? result.total_cost_usd : null,
444
+ }
445
+ if (first === null || last === null) return summary
446
+ const end = last
447
+
448
+ const sc = typeof parent === 'string' || parent == null ? spanContextOf(parent) : null
449
+ const parentCtx =
450
+ typeof parent === 'object' && parent !== null ? parent : sc ? trace.setSpanContext(ROOT_CONTEXT, sc) : ROOT_CONTEXT
451
+ const usage = result?.usage ?? {}
452
+ const session = tracer.startSpan(
453
+ 'claude.session',
454
+ {
455
+ startTime: first,
456
+ attributes: defined({
457
+ 'claude.session_id': sessionId,
458
+ 'claude.model': Object.keys(result?.modelUsage ?? {})[0] ?? init?.model,
459
+ 'claude_code.version': init?.claude_code_version,
460
+ 'claude.cost_usd': result?.total_cost_usd,
461
+ 'claude.tokens.input': usage.input_tokens,
462
+ 'claude.tokens.output': usage.output_tokens,
463
+ 'claude.tokens.cache_read': usage.cache_read_input_tokens,
464
+ 'claude.tokens.cache_creation': usage.cache_creation_input_tokens,
465
+ 'claude.turns': result?.num_turns,
466
+ 'claude.duration_ms': result?.duration_ms,
467
+ 'claude.terminal_reason': result?.terminal_reason,
468
+ 'claude.result': result?.subtype ?? 'missing',
469
+ 'claude.permission_denials': Array.isArray(result?.permission_denials)
470
+ ? result.permission_denials.length
471
+ : undefined,
472
+ 'claude.stream.malformed': malformed || undefined,
473
+ }),
474
+ },
475
+ parentCtx,
476
+ )
477
+ if (!result) session.setStatus({ code: SpanStatusCode.ERROR, message: 'no result: worker killed or log truncated' })
478
+ else if (!ok) session.setStatus({ code: SpanStatusCode.ERROR, message: String(result.subtype ?? 'error') })
479
+ const sessionCtx = trace.setSpan(parentCtx, session)
480
+
481
+ /** @type {Map<string, import('@opentelemetry/api').Context>} */
482
+ const toolCtx = new Map()
483
+ for (const t of [...tools.values()].sort((a, b) => a.start - b.start)) {
484
+ const span = tracer.startSpan(
485
+ 'claude.tool',
486
+ {
487
+ startTime: t.start,
488
+ attributes: defined({
489
+ 'claude.tool.name': t.name,
490
+ 'claude.tool.use_id': t.id,
491
+ 'claude.tool.outcome': t.end === null ? 'unfinished' : t.error ? 'error' : 'ok',
492
+ 'claude.tool.command': t.name === 'Bash' ? clip(t.input?.command) : undefined,
493
+ }),
494
+ },
495
+ (t.parentToolId && toolCtx.get(t.parentToolId)) || sessionCtx,
496
+ )
497
+ if (t.end === null) span.setStatus({ code: SpanStatusCode.ERROR, message: 'no tool_result' })
498
+ else if (t.error) span.setStatus({ code: SpanStatusCode.ERROR })
499
+ span.end(t.end ?? end)
500
+ toolCtx.set(t.id, trace.setSpan(sessionCtx, span))
501
+ }
502
+ for (const k of tasks.values()) {
503
+ const span = tracer.startSpan(
504
+ 'claude.task',
505
+ {
506
+ startTime: k.start,
507
+ attributes: defined({
508
+ 'claude.task.id': k.id,
509
+ 'claude.task.type': k.type,
510
+ 'claude.task.status': k.status ?? 'unfinished',
511
+ 'claude.task.description': k.description,
512
+ }),
513
+ },
514
+ (k.toolUseId && toolCtx.get(k.toolUseId)) || sessionCtx,
515
+ )
516
+ if (k.end === null || (k.status && k.status !== 'completed'))
517
+ span.setStatus({ code: SpanStatusCode.ERROR, message: k.status ?? 'no task_notification' })
518
+ span.end(Math.max(k.start, k.end ?? end))
519
+ }
520
+ session.end(end)
521
+ return summary
522
+ }