fix(diagnostics): harden trace context parsing#70955
Conversation
Greptile SummaryThis PR hardens trace context parsing by (1) wrapping Confidence Score: 5/5Safe to merge — changes are self-contained, well-tested, and spec-compliant. All findings are P2 (style/clarity only). The non-zero retry loops are provably correct (probability of an all-zero 128-bit id is 1/2^128), the future-version parsing logic matches W3C spec, and the new tests lock in every stated invariant. No data integrity, security, or runtime correctness concerns. No files require special attention. Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/infra/diagnostic-trace-context.ts
Line: 7-8
Comment:
**Duplicate regex constant**
`TRACEPARENT_VERSION_RE` is identical to `TRACE_FLAGS_RE` (`/^[0-9a-f]{2}$/`). Both constants match the same two-hex-digit pattern; using separate names is fine for readability, but a comment noting the intentional duplication (or a reference to the shared shape) would clarify this isn't an oversight.
```suggestion
const TRACE_FLAGS_RE = /^[0-9a-f]{2}$/;
/** Same shape as TRACE_FLAGS_RE; kept separate for semantic clarity. */
const TRACEPARENT_VERSION_RE = /^[0-9a-f]{2}$/;
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(diagnostics): harden trace context p..." | Re-trigger Greptile |
| const TRACE_FLAGS_RE = /^[0-9a-f]{2}$/; | ||
| const TRACEPARENT_VERSION_RE = /^[0-9a-f]{2}$/; |
There was a problem hiding this comment.
TRACEPARENT_VERSION_RE is identical to TRACE_FLAGS_RE (/^[0-9a-f]{2}$/). Both constants match the same two-hex-digit pattern; using separate names is fine for readability, but a comment noting the intentional duplication (or a reference to the shared shape) would clarify this isn't an oversight.
| const TRACE_FLAGS_RE = /^[0-9a-f]{2}$/; | |
| const TRACEPARENT_VERSION_RE = /^[0-9a-f]{2}$/; | |
| const TRACE_FLAGS_RE = /^[0-9a-f]{2}$/; | |
| /** Same shape as TRACE_FLAGS_RE; kept separate for semantic clarity. */ | |
| const TRACEPARENT_VERSION_RE = /^[0-9a-f]{2}$/; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/infra/diagnostic-trace-context.ts
Line: 7-8
Comment:
**Duplicate regex constant**
`TRACEPARENT_VERSION_RE` is identical to `TRACE_FLAGS_RE` (`/^[0-9a-f]{2}$/`). Both constants match the same two-hex-digit pattern; using separate names is fine for readability, but a comment noting the intentional duplication (or a reference to the shared shape) would clarify this isn't an oversight.
```suggestion
const TRACE_FLAGS_RE = /^[0-9a-f]{2}$/;
/** Same shape as TRACE_FLAGS_RE; kept separate for semantic clarity. */
const TRACEPARENT_VERSION_RE = /^[0-9a-f]{2}$/;
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
7438da9 to
cf0ff21
Compare
Summary
fftraceparent versions from the first four fields while keeping version00strict.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Root Cause (if applicable)
randomBytes().toString("hex"), bypassing the same non-zero validation path used for inbound ids; parsing also treated all non-00traceparent versions as invalid instead of preserving the first four fields for future versions.Regression Test Plan (if applicable)
src/infra/diagnostic-trace-context.test.ts00remains strict, and future non-ffversions continue from the first four fields.User-visible / Behavior Changes
None.
Diagram (if applicable)
N/A
Security Impact (required)
Yes, explain risk + mitigation: N/ARepro + Verification
Environment
Steps
pnpm test src/infra/diagnostic-trace-context.test.ts.pnpm format:check -- src/infra/diagnostic-trace-context.ts src/infra/diagnostic-trace-context.test.ts.pnpm exec oxlint src/infra/diagnostic-trace-context.ts src/infra/diagnostic-trace-context.test.ts.Expected
Actual
Evidence
Human Verification (required)
00extra-field rejection, generated fallback ids validate and format.ffversion, all-zero inbound trace/span ids, invalid trace flags.Review Conversations
Compatibility / Migration
Risks and Mitigations
fftwo-hex versions are accepted,traceId,spanId, andtraceFlagsstill use the existing validators, and version00remains strict at exactly four fields.