Problem
api/utils/process.py emits command-substrate telemetry by reading WINEBOT_SESSION_DIR from process environment. In concurrent request/task flows, env-based attribution can drift and write metrics to the wrong session artifact path.
Why this matters
- Misattributed telemetry reduces trust in per-session diagnostics and performance analysis.
- Race-prone attribution can hide regressions or create false positives in automation traces.
Proposed fix
- Introduce request/task-scoped session context using
contextvars.ContextVar (for example, current_session_dir).
- Set and reset context explicitly at API and session lifecycle boundaries.
- Update process telemetry path resolution to prefer contextvar over env fallback.
- Add regression tests covering concurrent operations with different session IDs to verify no cross-session bleed.
Acceptance criteria
- Command telemetry events are attributed to active request/session context under concurrency.
- Existing behavior remains backward compatible when no context is set.
- Tests fail without the fix and pass with it.
Problem
api/utils/process.pyemits command-substrate telemetry by readingWINEBOT_SESSION_DIRfrom process environment. In concurrent request/task flows, env-based attribution can drift and write metrics to the wrong session artifact path.Why this matters
Proposed fix
contextvars.ContextVar(for example,current_session_dir).Acceptance criteria