feat(core): inject context env vars (session/agent/prompt ID) into shell subprocesses#4649
Conversation
…ell subprocesses When SubAgents execute SQL or Python scripts via Bash tool, the scripts have no way to know their execution context. This adds automatic injection of QWEN_CODE_SESSION_ID, QWEN_CODE_AGENT_ID, and QWEN_CODE_PROMPT_ID into all shell subprocess environments, enabling downstream scripts to perform trace correlation, audit logging, and business context attribution. Closes #4645
There was a problem hiding this comment.
Pull request overview
This PR adds context environment variable injection for shell subprocesses so scripts can access session, agent, and prompt metadata.
Changes:
- Adds
getShellContextEnvVars()and unit tests. - Injects context env vars into shell execution, monitor subprocesses, and hook runner.
- Sets
QWEN_CODE_SESSION_IDduring Config session initialization and session switches.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
packages/core/src/utils/shellContextEnv.ts |
Adds centralized context env var helper. |
packages/core/src/utils/shellContextEnv.test.ts |
Adds unit coverage for helper behavior. |
packages/core/src/tools/monitor.ts |
Injects context env vars into monitor subprocesses. |
packages/core/src/services/shellExecutionService.ts |
Injects context env vars into shell subprocess spawn paths. |
packages/core/src/hooks/hookRunner.ts |
Injects context env vars into hook command execution. |
packages/core/src/config/config.ts |
Sets session ID in process env during session lifecycle. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📋 Review SummaryThis PR implements context environment variable injection ( 🔍 General Feedback
🎯 Specific Feedback🟡 High
🟢 Medium
🔵 Low
✅ Highlights
|
Prevents nested qwen-code processes from inheriting the parent's session ID. The previous `if (!process.env[...])` check would keep the parent's value; a module-level flag ensures each process claims its own session ID on first Config construction.
CI tsc --build enforces noPropertyAccessFromIndexSignature; use env['KEY'] instead of env.KEY to satisfy strict type checking.
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
Some test suites mock node:process without providing env, causing TypeError when Config constructor assigns QWEN_CODE_SESSION_ID. Add defensive check before env writes.
E2E 验证报告通过 测试结果:8/8 通过 ✅
Test 4 完整输出验证方法
|
Addresses reviewer feedback (wenshao) requesting test coverage for the module-level `sessionEnvClaimed` guard in Config constructor. Tests verify: 1. First Config instance sets process.env['QWEN_CODE_SESSION_ID'] 2. Subsequent Config instances do not overwrite the env var
- Add test verifying startNewSession updates process.env to new session ID - Add comment explaining why startNewSession bypasses sessionEnvClaimed guard (only callable on the canonical Config instance that already claimed)
wenshao
left a comment
There was a problem hiding this comment.
No issues found. LGTM! ✅ — claude-opus-4-6 via Qwen Code /review
tanzhenxin
left a comment
There was a problem hiding this comment.
LGTM — clean single-purpose change, centralized helper, good test coverage (unit + E2E). Nit (non-blocking): consider Refs #4645 instead of Closes since the issue also asks for PARENT_AGENT_ID and a custom env-injection mechanism (settings.json env / /env), which aren't in scope here.
Summary
QWEN_CODE_SESSION_ID、QWEN_CODE_AGENT_ID、QWEN_CODE_PROMPT_ID环境变量getShellContextEnvVars()工具函数,ALS 动态读取 agent/prompt 上下文Motivation
Closes #4645
业务脚本(SQL/Python)在 SubAgent 执行时无法得知自己的上下文,导致无法做链路追踪、审计日志、业务关联。参考 Claude Code 的
CLAUDE_CODE_SESSION_ID机制(anthropics/claude-code#14859, #63758, #60697),为 Qwen Code 实现类似功能。Changes
packages/core/src/utils/shellContextEnv.tspackages/core/src/config/config.tsprocess.env['QWEN_CODE_SESSION_ID'](带 guard 防止临时 Config 覆盖)packages/core/src/services/shellExecutionService.tspackages/core/src/tools/monitor.tspackages/core/src/hooks/hookRunner.tspackages/core/src/utils/shellContextEnv.test.tsDesign Decisions
process.env— Config 构造时设置,static ShellExecutionService 无需接收 config 实例Test Plan
shellContextEnv.test.ts6 个测试全部通过shellExecutionService.test.ts83 个已有测试无回归echo $QWEN_CODE_SESSION_ID输出 UUID