fix(delegation): preserve parent session identity across child runs - #74858
fix(delegation): preserve parent session identity across child runs#74858Xipong wants to merge 1 commit into
Conversation
…cess env AIAgent.__init__ calls set_current_session_id(self.session_id), which mutated both the task-local ContextVar and the process-global os.environ. Because _build_child_agent wraps construction in delegated_child_context(), the ContextVar write is harmless (task-local), but the os.environ write clobbered the parent's HERMES_SESSION_ID for the rest of the process — leaking the child id into parent tools and subprocesses spawned after the child was built. Root cause of HermesPRDelegationSessionContext: parent 20260729_212118_5d797e dispatched child 20260730_160515_736ea1; later parent terminal inherited HERMES_SESSION_ID=the child. Fix: set_current_session_id() skips the process-global os.environ write when called from within a delegated_child_context(). The child's own tools and subprocesses still resolve their id through the ContextVar (task-local), while the parent's process-wide env keeps the parent's session identity. Root agents (CLI, gateway, cron) retain both paths. Adds 7 regression tests covering single child, concurrent children (8 parallel), parent-tool observation after construction, and root-agent session rotation backward compatibility. All pass; ruff clean.
|
Thanks for the focused fix. The premise is confirmed on current main: The proposed construction boundary, explicit child execution binding at Automated hermes-sweeper review. |
|
Merged via #75856 — your commit(s) were cherry-picked onto current main with your authorship preserved in git history (rebase merge). Thanks for the contribution! |
Summary
Delegated child construction currently calls
set_current_session_id(child.session_id), which publishes the child ID into the process-wideos.environ["HERMES_SESSION_ID"]. The parent continues running in the same process, so later parent terminal/execute-code subprocesses can inherit the last child's identity.This PR:
delegated_child_context();ContextVarto construction/execution and restores the parent value on exit;child.session_idduring the child run, so child terminal subprocesses still receive the correct identity through the existing subprocess-environment bridge;No new configuration, schema, model-facing tools, or persistence behavior is introduced.
Reproduction
delegate_taskchildren.HERMES_SESSION_ID.state.dbto asource='subagent'session whoseparent_session_idis the real parent.Verification
build_subprocess_envcontracts: 7 passedpy_compile: passedCurrent head:
56b8f3417521c627db605ed4c1060435a08b4ce1.