Repository navigation
fix(diagnostics-otel): agent turns split into one-span traces instead of a trace tree - #116246
Conversation
Retained trusted span contexts translate a completed lifecycle span's diagnostic span id to the real OTel span context. That table was evicted 5s after run.completed, so children of a longer turn missed their parent and OTel minted a fresh trace id for each one, silently splitting a turn into single-span traces. Retention is now identity-based and lives for the service lifetime, bounded by MAX_RETAINED_TRUSTED_SPAN_CONTEXTS, which removes the timing race and the drain/timeout cleanup subsystem. Also nests openclaw.exec under its run: the exec event now carries a trusted trace context and the recorder resolves the ambient run scope.
Retained lifecycle span contexts were stored under the span's OTel trace id but looked up with the event's diagnostic trace id. Those are different id spaces, so every post-completion parent lookup missed and each straggler span started a brand-new trace. The mocked suite could not catch this: its tracer reports the same trace id the test feeds in, collapsing both id spaces into one value. Adds a boundary test that runs the real OTel SDK and OTLP/protobuf exporter against a local receiver and asserts on exported span bytes.
|
Codex review: needs maintainer review before merge. Reviewed July 30, 2026, 6:39 AM ET / 10:39 UTC. ClawSweeper reviewWhat this changesThe PR repairs the diagnostics-otel plugin so late agent lifecycle, tool, usage, and exec spans inherit the original exported OpenTelemetry parent context instead of becoming separate traces. Merge readinessKeep this PR open for maintainer landing review. The branch has strong real exporter proof and no remaining concrete patch defect, but it is behind current Priority: P2 Review scores
Verification
How this fits togetherThe agent runtime emits diagnostic lifecycle events with OpenClaw-owned trace metadata, and the diagnostics-otel plugin records those as OpenTelemetry spans. A retained exported parent context lets later events attach to the completed lifecycle span, preserving one trace tree for an agent turn. flowchart LR
A[Agent runtime] --> B[Diagnostic lifecycle events]
B --> C[Trusted trace metadata]
C --> D[Diagnostics OTel recorder]
D --> E[Retained exported parent context]
E --> F[Late tool usage and exec spans]
F --> G[Single agent-turn trace tree]
Decision needed
Why: The implementation and exporter proof are strong, but the branch is behind Before merge
Agent review detailsSecurityNone. PR surfaceSource +5, Tests +346, Docs +1. Total +352 across 12 files. View PR surface stats
Review metricsNone. Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Preserve the bounded retained-context design, run the required hosted validation on the current merge candidate, and merge if it confirms the same one-tree exporter behavior without regressions. Do we have a high-confidence way to reproduce the issue? No high-confidence current-main reproduction was run in this read-only review, but the PR supplies a focused real-SDK exporter regression path that demonstrated the split-trace failure on its base and the repaired trace shape on its head. Is this the best way to solve the issue? Yes, subject to current-base validation. Translating OpenClaw diagnostic context to the actual exported OTel parent context is the narrow owner-boundary fix and avoids trusting exec payload data or adding configuration. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 8a3f5c862b56. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (12 earlier review cycles; latest 8 shown)
|
Review findings on the parent-span linking fix: - completeTrackedLifecycleSpan now takes the DiagnosticTraceContext instead of a bare trace id beside a span id. Two adjacent hex strings let a transposition compile clean and silently reinstate split traces; DiagnosticTraceContext is the only shape an OTel SpanContext cannot satisfy (traceFlags string vs number). - internalOrTrustedTraceContext composes normalizedTrustedTraceContext rather than spelling the trust predicate a third time. - Corrected comments that claimed OTel ids never equal diagnostic ids. Spans parented from an upstream traceparent do adopt the diagnostic trace id; the removed guard only ever misfired on root lifecycle spans. - Exec comment now states the real parent: the openclaw harness opens no run scope, so exec nests under openclaw.harness.run. Test gaps closed: - The core emitter switch was untested; reverting it kept every test green. The pty-fallback test now asserts the emitted metadata. - The boundary test covered only run.completed of three lifecycle owners; it now covers harness.run.completed and message.processed too, each with a straggler. - Dropped the service-context cast for the typed helper, which caught a bogus context.assembled field, and moved teardown into afterEach so a failed assertion cannot leak the real NodeSDK across files. - Scoped the one-trace assertion to this turn's spans; faked Date so a lazy Date.now expiry cannot pass. Docs: exec and tool spans now listed as staying on an upstream request trace.
…nator Native review found three correctness gaps in the previous commit: - Exec spans could be exported naming a parent span id nothing ever emits. The ambient lookup fell through to a remote-parent fallback built from diagnostic ids, which is a regression from the previous clean-root behavior and breaks waterfalls and parent-id-keyed backends. Exec now resolves only spans this process exported and stays a root on a miss; message spans keep the remote fallback because their context can come from an inbound traceparent. - harness.run.error never retained its span context, so an aborted turn still split. For the openclaw harness that span is the only ancestor a late child has, and aborted turns emit no run.completed. - completeTrackedLifecycleSpan no longer takes a redundant spanId. The boundary test now runs through the OPENCLAW_OTEL_PRELOADED seam with an in-memory exporter and releases the global tracer provider in teardown. A NodeSDK cannot be unregistered, so the old version would have made any later real-SDK test silently export nothing. That also deletes the hand-rolled OTLP protobuf decoder. Added guards for the aborted-turn path and for exec staying parentless; both are mutation-verified.
Retention had no time bound at all, so a command backgrounded by a turn could finish hours later and still attach to a parent that ended long before. Backends derive trace latency from the span envelope, so a 30s turn rendered as hours with a child extending past its parent. The horizon is checked lazily on lookup rather than on a timer: a timer firing mid-turn is exactly what silently split traces before. Past the horizon a straggler starts its own trace, which only loses causality for a span that would have skewed its parent's duration anyway. The two bounds now pin each other - shrinking the horizon to the old 5s window fails the original regression tests, and removing it fails the new horizon test.
|
/clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
/clawsweeper re-review |
1 similar comment
|
/clawsweeper re-review |
|
/clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
/clawsweeper re-review |
2 similar comments
|
/clawsweeper re-review |
|
/clawsweeper re-review |
|
/clawsweeper re-review |
|
Merged via squash.
|
… of a trace tree (openclaw#116246) * fix(diagnostics-otel): keep parent span links across long turns Retained trusted span contexts translate a completed lifecycle span's diagnostic span id to the real OTel span context. That table was evicted 5s after run.completed, so children of a longer turn missed their parent and OTel minted a fresh trace id for each one, silently splitting a turn into single-span traces. Retention is now identity-based and lives for the service lifetime, bounded by MAX_RETAINED_TRUSTED_SPAN_CONTEXTS, which removes the timing race and the drain/timeout cleanup subsystem. Also nests openclaw.exec under its run: the exec event now carries a trusted trace context and the recorder resolves the ambient run scope. * fix(diagnostics-otel): key retained span contexts by diagnostic trace id Retained lifecycle span contexts were stored under the span's OTel trace id but looked up with the event's diagnostic trace id. Those are different id spaces, so every post-completion parent lookup missed and each straggler span started a brand-new trace. The mocked suite could not catch this: its tracer reports the same trace id the test feeds in, collapsing both id spaces into one value. Adds a boundary test that runs the real OTel SDK and OTLP/protobuf exporter against a local receiver and asserts on exported span bytes. * fix(diagnostics-otel): harden span-linking contract after review Review findings on the parent-span linking fix: - completeTrackedLifecycleSpan now takes the DiagnosticTraceContext instead of a bare trace id beside a span id. Two adjacent hex strings let a transposition compile clean and silently reinstate split traces; DiagnosticTraceContext is the only shape an OTel SpanContext cannot satisfy (traceFlags string vs number). - internalOrTrustedTraceContext composes normalizedTrustedTraceContext rather than spelling the trust predicate a third time. - Corrected comments that claimed OTel ids never equal diagnostic ids. Spans parented from an upstream traceparent do adopt the diagnostic trace id; the removed guard only ever misfired on root lifecycle spans. - Exec comment now states the real parent: the openclaw harness opens no run scope, so exec nests under openclaw.harness.run. Test gaps closed: - The core emitter switch was untested; reverting it kept every test green. The pty-fallback test now asserts the emitted metadata. - The boundary test covered only run.completed of three lifecycle owners; it now covers harness.run.completed and message.processed too, each with a straggler. - Dropped the service-context cast for the typed helper, which caught a bogus context.assembled field, and moved teardown into afterEach so a failed assertion cannot leak the real NodeSDK across files. - Scoped the one-trace assertion to this turn's spans; faked Date so a lazy Date.now expiry cannot pass. Docs: exec and tool spans now listed as staying on an upstream request trace. * fix(diagnostics-otel): keep late children on the trace on every terminator Native review found three correctness gaps in the previous commit: - Exec spans could be exported naming a parent span id nothing ever emits. The ambient lookup fell through to a remote-parent fallback built from diagnostic ids, which is a regression from the previous clean-root behavior and breaks waterfalls and parent-id-keyed backends. Exec now resolves only spans this process exported and stays a root on a miss; message spans keep the remote fallback because their context can come from an inbound traceparent. - harness.run.error never retained its span context, so an aborted turn still split. For the openclaw harness that span is the only ancestor a late child has, and aborted turns emit no run.completed. - completeTrackedLifecycleSpan no longer takes a redundant spanId. The boundary test now runs through the OPENCLAW_OTEL_PRELOADED seam with an in-memory exporter and releases the global tracer provider in teardown. A NodeSDK cannot be unregistered, so the old version would have made any later real-SDK test silently export nothing. That also deletes the hand-rolled OTLP protobuf decoder. Added guards for the aborted-turn path and for exec staying parentless; both are mutation-verified. * fix(diagnostics-otel): age retained parent contexts out after 10 minutes Retention had no time bound at all, so a command backgrounded by a turn could finish hours later and still attach to a parent that ended long before. Backends derive trace latency from the span envelope, so a 30s turn rendered as hours with a child extending past its parent. The horizon is checked lazily on lookup rather than on a timer: a timer firing mid-turn is exactly what silently split traces before. Past the horizon a straggler starts its own trace, which only loses causality for a span that would have skewed its parent's duration anyway. The two bounds now pin each other - shrinking the horizon to the old 5s window fails the original regression tests, and removing it fails the new horizon test. * fix(diagnostics-otel): retain parent contexts until capacity
New behavior
Operators using the
diagnostics-otelplugin now see one agent turn as onereadable trace tree, including work that finishes more than 30 minutes after its
lifecycle parent ended.
The previous failure was path-dependent:
trace id, so the old map key happened to match. They still lost late children
when the retained parent expired after five seconds.
and OTel trace ids differed, so the old retained-parent lookup missed even
before the five-second expiry.
openclaw.execalso never requested a parent, so shell-process spans wereexported as roots.
How it works
Completed lifecycle spans now retain a translation from the event's diagnostic
ids to the real exported OTel
SpanContext. Lookups use the diagnostic trace idthat later events carry, while child spans receive the actual OTel parent.
The retained mapping no longer expires based on elapsed time. Completion paths
for message, harness, run, and harness error spans all retain their exported
context. Exec completion events preserve their OpenClaw-owned ambient trace
context without marking the payload itself as trusted, and the recorder parents
them only when this process exported the resolved ancestor.
User Impact
Normal agent turns now export as one trace tree, including model calls, context
assembly, usage, skills, tool execution, and exec spans that finish after their
lifecycle parent. No configuration or migration is required.
Evidence
A/B exporter proof
The same regression test ran against both production implementations in one
Blacksmith Testbox run.
A used the PR base production code with the PR's test; B used the current PR
production code. The test uses the real OTel SDK with an
InMemorySpanExporter,so diagnostic ids and exported OTel ids remain distinct.
Normal turn on A — PR base:
The same turn on B — current PR:
The aborted-turn case showed the same repair: A exported
openclaw.harness.runand its lateopenclaw.tool.executionchild as twotraces; B exported one trace with the tool span under the harness span.
This covers the message, harness, run, harness-error, late-child, and
missing-exported-parent boundaries.
Checks
node scripts/run-vitest.mjs extensions/diagnostics-otel— 116 passed.node scripts/run-vitest.mjs src/agents/bash-tools.exec-runtime.pty-fallback.test.ts src/agents/bash-tools.exec-runtime.test.ts— 37 passed.— 116 diagnostics tests and 37 exec-runtime tests passed; extension production
and test typechecks, lint, boundary guards, dead-export checks, and import-cycle
checks also passed.
node scripts/run-tsgo.mjsextension/core production and test lanes — passed.node scripts/run-oxlint.mjs extensions/diagnostics-otel src/agents— passed.git diff --check— passed.Remaining proof and bounds
Maintainer decision (2026-07-30): accept the capacity-bounded retained-context lifetime with no elapsed-time expiry for this change. Retained
mappings do not expire by time. Each record is an id-to-
SpanContextlookup,not a running span. The map holds at most 1,024 records, evicts the oldest
insertion when full, and is cleared when the diagnostics service stops.
A rebase-only proof refresh is intentionally skipped: the branch is conflict-free,
and both Blacksmith runs tested the exact PR head. The required landing workflow
will verify that same head with hosted CI/Testbox before merge.