qwen-code/packages/core/src/utils
jinye 4bb8dc894a
fix(telemetry): address PR #3847 review follow-ups for trace correlation (#4058)
* fix(telemetry): address PR #3847 review follow-ups for trace correlation

Addresses unresolved review feedback from PR #3847:

- Respect OTEL_TRACES_SAMPLER env var when setting TraceFlags on the
  synthetic session root, so custom samplers (e.g. traceidratio) are
  not bypassed by forced SAMPLED flag
- Store current session ID in session-context.ts and use it as a
  fallback in LogToSpanProcessor when the OTel Resource session.id
  attribute is stale after /clear or /resume
- Wrap for-await loop body in runInSpan() so debug logs emitted
  during stream iteration see the stream span as active
- Add autoOkOnSuccess option to withSpan, eliminating the need for
  the load-bearing setStatus(UNSET) hack in cancellation paths
- Add defensive 5-minute timeout for stream spans to prevent leaks
  from abandoned generators

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(telemetry): address review issues in PR #4058

- Fix sdk.test.ts assertion to match new two-arg setSessionContext call
- Add spanEnded guard to prevent double span.end() when timeout fires
- Add .trim() to OTEL_TRACES_SAMPLER env var for robustness
- Pass sessionId in debugLogger.test.ts for signature completeness
- Clarify log-to-span-processor comment: fallback covers "missing" not "stale"

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(telemetry): adopt review feedback for sampler and idle timeout

- Fix shouldForceSampled to force SAMPLED for all parentbased_* samplers,
  not just parentbased_always_on — parentbased samplers delegate to
  localParentNotSampled (default AlwaysOff) when parent has NONE, which
  silently drops all traces
- Convert stream span timeout from fixed wall-clock to idle timeout:
  reset timer on each chunk so legitimately long streams are never
  affected; timeout only fires when no chunks arrive for 5 minutes
- Add test for parentbased_traceidratio → TraceFlags.SAMPLED

* fix(telemetry): guard resetSpanTimeout against already-ended span

Prevent zombie timer accumulation when chunks arrive after idle timeout
has already ended the span.

* fix(telemetry): handle parentbased_always_off sampler and fill test gaps

- shouldForceSampled() now returns false for parentbased_always_off,
  preventing silent over-sampling that contradicts user intent.
- Updated JSDoc to document the always_on exception for non-parentbased
  samplers.
- Added test for parentbased_always_off → TraceFlags.NONE.
- Added test for success path resilience when safeSetStatus throws in
  coreToolScheduler.

* fix(telemetry): harden span timeout ordering and session ID fallback

- Swap spanEnded/span.end() order so finally block can retry if end()
  throws.
- Clear idle timeout immediately after for-await loop exits, before
  post-loop processing.
- Use || instead of ?? for session.id fallback to handle empty strings.

* fix(telemetry): address review round 4 — docs, cleanup, and test gaps

- Expand shouldForceSampled JSDoc: document env-var assumption,
  parentbased_traceidratio 100% sampling semantics.
- Remove unnecessary runInSpan wrapper around chunk field assignments.
- Add stream.timed_out span attribute when idle timeout fires.
- Deduplicate config.getSessionId() call in initializeTelemetry.
- Add tests for always_on and always_off sampler values.

* fix(telemetry): harden timeout callback ordering and add || comment

- Move safeSetStatus before setAttribute in timeout callback so ERROR
  status is set even if setAttribute throws.
- Set spanEnded=true before span.end() so finally block never overwrites
  ERROR with OK if end() throws.
- Add comment explaining || vs ?? choice for session.id fallback.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-13 22:02:20 +08:00
..
filesearch feat(core): replace fdir crawler with git ls-files + ripgrep fallback (#3214) 2026-05-12 13:58:16 +08:00
request-tokenizer feat(core): migrate console calls to debugLogger (M3 Phase 6) 2026-01-25 22:12:45 +08:00
asyncMessageQueue.test.ts feat(core,cli)!: Implement in-process agent backend for arenas 2026-02-21 21:08:20 +08:00
asyncMessageQueue.ts feat(core,cli)!: Implement in-process agent backend for arenas 2026-02-21 21:08:20 +08:00
atomicFileWrite.test.ts feat(arena): add system reminder and status file support for agent collaboration 2026-03-11 11:56:05 +08:00
atomicFileWrite.ts feat(arena): add system reminder and status file support for agent collaboration 2026-03-11 11:56:05 +08:00
bareMode.ts feat(cli): add bare startup mode (#3448) 2026-04-20 10:01:59 +08:00
browser.ts # 🚀 Sync Gemini CLI v0.2.1 - Major Feature Update (#483) 2025-09-01 14:48:55 +08:00
configResolver.test.ts refactor: update authentication handling and model configuration 2026-01-07 22:58:09 +08:00
configResolver.ts refactor: update authentication handling and model configuration 2026-01-07 22:58:09 +08:00
contextLengthError.test.ts feat(core): add reactive compression on context overflow (#3879) 2026-05-09 11:06:49 +08:00
contextLengthError.ts feat(core): add reactive compression on context overflow (#3879) 2026-05-09 11:06:49 +08:00
cronDisplay.ts feat(loop): add subcommands and improve cron display output 2026-03-29 12:28:57 +08:00
cronParser.test.ts fix(cron): improve jitter strategy and fix vixie-cron day matching semantics 2026-03-29 12:52:11 +08:00
cronParser.ts fix(cron): improve jitter strategy and fix vixie-cron day matching semantics 2026-03-29 12:52:11 +08:00
debugLogger.test.ts fix(telemetry): address PR #3847 review follow-ups for trace correlation (#4058) 2026-05-13 22:02:20 +08:00
debugLogger.ts feat(telemetry): inject traceId/spanId into debug log files for OTel correlation (#3847) 2026-05-11 07:42:56 +08:00
editHelper.test.ts fix(core): Preserve trailing whitespace in newString during edits 2026-02-02 11:39:42 +08:00
editHelper.ts fix(core): Preserve trailing whitespace in newString during edits 2026-02-02 11:39:42 +08:00
editor.test.ts feat(retry): add persistent retry mode for unattended CI/CD environments (#3080) 2026-04-21 22:08:11 +08:00
editor.ts feat(retry): add persistent retry mode for unattended CI/CD environments (#3080) 2026-04-21 22:08:11 +08:00
environmentContext.test.ts feat(arena): forward chat history to spawned agents 2026-03-10 19:45:14 +08:00
environmentContext.ts feat(vscode): add message edit/rewind and message metadata UI (#3762) 2026-05-09 16:52:46 +08:00
envVarResolver.test.ts feat: move extension to core package 2026-01-14 15:30:27 +08:00
envVarResolver.ts feat: move extension to core package 2026-01-14 15:30:27 +08:00
errorParsing.test.ts fix(cli): stop double-wrapping and double-printing API errors in non-interactive mode (#3749) 2026-05-03 08:39:31 +08:00
errorParsing.ts fix(cli): stop double-wrapping and double-printing API errors in non-interactive mode (#3749) 2026-05-03 08:39:31 +08:00
errorReporting.test.ts refactor(debug): replace ConsolePatcher with debugLogger and update error reporting 2026-02-02 17:37:54 +08:00
errorReporting.ts refactor(debug): replace ConsolePatcher with debugLogger and update error reporting 2026-02-02 17:37:54 +08:00
errors.test.ts fix: suppress error logging for user-cancelled requests (#1182) 2026-01-18 14:45:50 +08:00
errors.ts fix(core): detect rate-limit errors from streamed SSE frames (#3246) 2026-04-14 19:58:26 +08:00
fetch.test.ts refactor(core): extract fetch error troubleshooting 2026-01-12 12:00:01 +08:00
fetch.ts feat(tools): add Markdown for Agents support to WebFetch tool (#2734) 2026-04-19 17:23:09 +08:00
fileUtils.test.ts fix(core): unify Edit/WriteFile prior-read with Claude Code; close #3964 + #3945 (#4002) 2026-05-10 14:29:08 +08:00
fileUtils.ts fix(core): unify Edit/WriteFile prior-read with Claude Code; close #3964 + #3945 (#4002) 2026-05-10 14:29:08 +08:00
forkedAgent.agent.test.ts feat(session): add /branch to fork the current conversation (#3539) 2026-05-08 19:34:11 +08:00
forkedAgent.cache.test.ts feat(memory): managed auto-memory and auto-dream system (#3087) 2026-04-16 20:05:45 +08:00
forkedAgent.ts fix(core): close bound-tool gap on runForkedAgent's YOLO wrapper (#3892) 2026-05-08 13:42:15 +08:00
formatters.ts refactor(core): Centralize shell logic into ShellExecutionService (#4823) 2025-07-26 01:56:49 +00:00
generateContentResponseUtilities.test.ts feat(commands): Enable @file processing in TOML commands (#6716) 2025-08-28 03:22:21 +00:00
generateContentResponseUtilities.ts fix: handle special case for merging function calls with args and names 2026-02-10 11:47:15 +08:00
getFolderStructure.test.ts refactor: remove read_many_files tool, add readManyFiles utility for user @-commands 2026-01-31 12:17:46 +08:00
getFolderStructure.ts Merge remote-tracking branch 'origin/main' into feat/debug-logging-refactor 2026-02-05 20:23:48 +08:00
getPty.ts # 🚀 Sync Gemini CLI v0.2.1 - Major Feature Update (#483) 2025-09-01 14:48:55 +08:00
gitDiff.test.ts feat: add /diff command and git diff statistics utility (#3491) 2026-05-10 11:15:59 +08:00
gitDiff.ts feat: add /diff command and git diff statistics utility (#3491) 2026-05-10 11:15:59 +08:00
gitIgnoreParser.test.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
gitIgnoreParser.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
gitUtils.ts feat(export): refactor HTML export components and improve metadata 2026-03-19 14:10:39 +08:00
iconvHelper.ts fix: reduce redundant file I/O and fix BOM preservation for non-UTF-8 encodings 2026-03-04 16:03:17 +08:00
ignorePatterns.test.ts feat(memory): managed auto-memory and auto-dream system (#3087) 2026-04-16 20:05:45 +08:00
ignorePatterns.ts feat(memory): managed auto-memory and auto-dream system (#3087) 2026-04-16 20:05:45 +08:00
installationManager.test.ts refactor(debug): replace ConsolePatcher with debugLogger and update error reporting 2026-02-02 17:37:54 +08:00
installationManager.ts feat(core): migrate console calls to debugLogger (M3 Phase 6) 2026-01-25 22:12:45 +08:00
internalPromptIds.test.ts refactor(core): route side-query LLM calls through runSideQuery chokepoint (#3775) 2026-05-11 19:03:14 +08:00
internalPromptIds.ts fix(core): log internal OpenAI JSON requests (#4081) 2026-05-13 09:19:44 +08:00
jsonl-utils.test.ts feat(cli): core built-in i18n coverage (#3871) 2026-05-10 22:35:03 +08:00
jsonl-utils.ts feat(cli): core built-in i18n coverage (#3871) 2026-05-10 22:35:03 +08:00
language-detection.ts chore: consistently import node modules with prefix (#3013) 2025-08-25 20:11:27 +00:00
LruCache.ts pre-release commit 2025-07-22 23:26:01 +08:00
memoryDiscovery.test.ts feat(cli): add bare startup mode (#3448) 2026-04-20 10:01:59 +08:00
memoryDiscovery.ts feat(core): support QWEN_HOME env var to customize config directory (#2953) 2026-05-09 15:51:52 +08:00
memoryImportProcessor.test.ts feat(core,cli): migrate console.debug to debugLogger (M3 Phase 1-3) 2026-01-25 20:57:25 +08:00
memoryImportProcessor.ts feat(core,cli): migrate console.debug to debugLogger (M3 Phase 1-3) 2026-01-25 20:57:25 +08:00
messageInspectors.ts Explict imports & exports with type modifier (#3774) 2025-08-25 22:04:53 +00:00
nextSpeakerChecker.test.ts special handling for summarized thinking 2025-12-22 14:07:23 +08:00
nextSpeakerChecker.ts feat(memory): managed auto-memory and auto-dream system (#3087) 2026-04-16 20:05:45 +08:00
notebook.test.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
notebook.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
openaiLogger.test.ts fix(core): tag subagent OpenAI JSON logs (#4099) 2026-05-13 16:26:38 +08:00
openaiLogger.ts fix(core): tag subagent OpenAI JSON logs (#4099) 2026-05-13 16:26:38 +08:00
partUtils.test.ts feat(commands): Enable @file processing in TOML commands (#6716) 2025-08-28 03:22:21 +00:00
partUtils.ts minor updates to address review comments 2025-12-23 14:35:41 +08:00
pathReader.test.ts feat(core): implement fork subagent for context sharing (#2936) 2026-04-14 14:27:38 +08:00
pathReader.ts fix(core): respect respectGitIgnore setting in @file injection path (#3197) 2026-04-13 17:56:35 +08:00
paths.test.ts fix(memory): address code review feedback for auto-memory recall (#3866) 2026-05-07 23:01:09 +08:00
paths.ts fix(memory): address code review feedback for auto-memory recall (#3866) 2026-05-07 23:01:09 +08:00
pdf.test.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
pdf.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
projectPath.ts feat(skills): parallelize loading + add path-conditional activation (#3604) 2026-05-05 00:28:53 +08:00
projectSummary.test.ts fix(cli): remember "Start new chat session" until summary changes (#3308) 2026-04-16 13:54:14 +08:00
projectSummary.ts fix(cli): remember "Start new chat session" until summary changes (#3308) 2026-04-16 13:54:14 +08:00
promptIdContext.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
proxyUtils.test.ts fix normalize in different place 2026-03-30 16:06:31 +08:00
proxyUtils.ts add @license 2026-03-30 16:11:26 +08:00
quotaErrorDetection.test.ts refactor(core): improve error handling and quota detection 2026-03-18 16:07:35 +08:00
quotaErrorDetection.ts refactor(core): improve error handling and quota detection 2026-03-18 16:07:35 +08:00
qwenIgnoreParser.test.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
qwenIgnoreParser.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
rateLimit.test.ts fix(core): improve stream rate-limit retry handling (#3790) 2026-05-07 11:58:55 +08:00
rateLimit.ts fix(core): improve stream rate-limit retry handling (#3790) 2026-05-07 11:58:55 +08:00
readManyFiles.test.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
readManyFiles.ts feat(core): PDF text extraction fallback and Jupyter notebook parsing (#3160) 2026-04-20 11:09:50 +08:00
retry.test.ts feat(retry): add persistent retry mode for unattended CI/CD environments (#3080) 2026-04-21 22:08:11 +08:00
retry.ts feat(retry): add persistent retry mode for unattended CI/CD environments (#3080) 2026-04-21 22:08:11 +08:00
ripgrepUtils.test.ts Replace spawn with execFile for memory-safe command execution (#1068) 2025-11-20 15:04:00 +08:00
ripgrepUtils.ts Merge branch 'main' into feat/debug-logging-refactor 2026-02-04 17:30:58 +08:00
rulesDiscovery.test.ts feat(core): support QWEN_HOME env var to customize config directory (#2953) 2026-05-09 15:51:52 +08:00
rulesDiscovery.ts feat(core): support QWEN_HOME env var to customize config directory (#2953) 2026-05-09 15:51:52 +08:00
runtimeFetchOptions.test.ts fix(core): improve runtime fetch options error handling and documentation (#3997) 2026-05-13 21:52:08 +08:00
runtimeFetchOptions.ts fix(core): improve runtime fetch options error handling and documentation (#3997) 2026-05-13 21:52:08 +08:00
runtimeStatus.config.test.ts chore(core): runtime.json sidecar follow-ups from PR #3714 review (#4030) 2026-05-12 06:55:52 +08:00
runtimeStatus.test.ts feat(core): write runtime.json sidecar for active sessions (#3714) 2026-05-11 01:21:49 +08:00
runtimeStatus.ts chore(core): runtime.json sidecar follow-ups from PR #3714 review (#4030) 2026-05-12 06:55:52 +08:00
safeJsonParse.test.ts fix: OpenAI tools (#328) 2025-08-14 21:18:26 +08:00
safeJsonParse.ts feat(core): migrate console calls to debugLogger (M3 Phase 6) 2026-01-25 22:12:45 +08:00
safeJsonStringify.test.ts pre-release commit 2025-07-22 23:26:01 +08:00
safeJsonStringify.ts pre-release commit 2025-07-22 23:26:01 +08:00
schemaConverter.test.ts fix: improve Gemini compatibility by adding configurable schema converter 2025-12-11 14:23:27 +08:00
schemaConverter.ts fix: improve Gemini compatibility by adding configurable schema converter 2025-12-11 14:23:27 +08:00
schemaValidator.test.ts feat(tools): add ToolSearch for on-demand loading of deferred tool schemas (#3589) 2026-05-10 14:29:25 +08:00
schemaValidator.ts feat(cli): add --json-schema for structured output in headless mode (#3598) 2026-05-11 14:21:55 +08:00
secure-browser-launcher.test.ts feat(session): add rename, delete, and auto-title generation for session (#3093) 2026-04-22 11:48:01 +08:00
secure-browser-launcher.ts feat(session): add rename, delete, and auto-title generation for session (#3093) 2026-04-22 11:48:01 +08:00
sessionStorageUtils.test.ts perf(core): bound session-list metadata reads to head/tail 64KB; pool buffer; lazy message count (#3897) 2026-05-10 18:13:33 +08:00
sessionStorageUtils.ts perf(core): bound session-list metadata reads to head/tail 64KB; pool buffer; lazy message count (#3897) 2026-05-10 18:13:33 +08:00
shell-utils.test.ts feat(core): event monitor tool with throttled stdout streaming (Phase C) (#3684) 2026-05-02 20:57:26 +08:00
shell-utils.ts feat(core): event monitor tool with throttled stdout streaming (Phase C) (#3684) 2026-05-02 20:57:26 +08:00
shellAstParser.test.ts feat: add wasm build config (#2985) 2026-04-09 14:21:00 +08:00
shellAstParser.ts feat: add wasm build config (#2985) 2026-04-09 14:21:00 +08:00
shellReadOnlyChecker.test.ts fix(security): treat newlines as command separators to prevent command injection 2026-01-28 10:23:08 +08:00
shellReadOnlyChecker.ts feat test tool permissions 2026-03-10 16:30:22 +08:00
sideQuery.test.ts refactor(core): route side-query LLM calls through runSideQuery chokepoint (#3775) 2026-05-11 19:03:14 +08:00
sideQuery.ts refactor(core): route side-query LLM calls through runSideQuery chokepoint (#3775) 2026-05-11 19:03:14 +08:00
subagentGenerator.test.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
subagentGenerator.ts refactor(core): route side-query LLM calls through runSideQuery chokepoint (#3775) 2026-05-11 19:03:14 +08:00
subagentNameContext.ts feat(cli): attribute /stats rows to the originating subagent (#3229) 2026-04-21 11:44:10 +08:00
symlink.ts feat(shell): enable PTY by default and various enhancements 2026-03-05 11:28:13 +08:00
systemEncoding.test.ts fix(shell): force UTF-8 output for PowerShell on Windows 2026-03-16 17:39:47 +08:00
systemEncoding.ts refactor(core): improve platform-specific encoding and shell utilities 2026-03-16 20:29:21 +08:00
terminalSafe.ts feat(cli): customize banner area (logo, title, hide) (#3710) 2026-05-07 10:17:53 +08:00
terminalSerializer.test.ts Feat/openrouter auth (#3576) 2026-04-27 14:47:44 +08:00
terminalSerializer.ts fix(cli): add TUI flicker foundation fixes (#3591) 2026-04-25 10:13:34 +08:00
testUtils.ts pre-release commit 2025-07-22 23:26:01 +08:00
textUtils.test.ts refactor(cli): reuse normalizeContent for markdown frontmatter parsing 2026-03-04 17:34:26 +08:00
textUtils.ts refactor(cli): reuse normalizeContent for markdown frontmatter parsing 2026-03-04 17:34:26 +08:00
thoughtUtils.test.ts Sync upstream Gemini-CLI v0.8.2 (#838) 2025-10-23 09:27:04 +08:00
thoughtUtils.ts special handling for summarized thinking 2025-12-22 14:07:23 +08:00
toml-to-markdown-converter.test.ts feat: move extension to core package 2026-01-14 15:30:27 +08:00
toml-to-markdown-converter.ts feat: move extension to core package 2026-01-14 15:30:27 +08:00
tool-utils.test.ts Refactor: Standardize Tool Naming and Configuration System (#1004) 2025-11-12 19:46:05 +08:00
tool-utils.ts Refactor: Standardize Tool Naming and Configuration System (#1004) 2025-11-12 19:46:05 +08:00
truncation.test.ts Create project temp dir before saving truncated output (#3875) 2026-05-07 11:58:46 +08:00
truncation.ts Create project temp dir before saving truncated output (#3875) 2026-05-07 11:58:46 +08:00
windowsPath.ts fix(core): normalize Windows PATH for MCP stdio servers (#3451) 2026-04-20 15:22:01 +08:00
workspaceContext.test.ts fix workspace dirs 2026-03-11 15:24:08 +08:00
workspaceContext.ts perf(core): cut runtime sync I/O on tool hot path by 91% (#3581) 2026-04-24 21:17:51 +08:00
xml.ts feat(skills): parallelize loading + add path-conditional activation (#3604) 2026-05-05 00:28:53 +08:00
yaml-parser.test.ts fix: tests fail on Windows 2025-09-10 16:24:59 +08:00
yaml-parser.ts fix: tests fail on Windows 2025-09-10 16:24:59 +08:00