The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): carry the curator caller's contextvars into the review thread (#125032, salvage #125038)
Yuan Lib8cf1a3c71bf
diff --git a/agent/curator.py b/agent/curator.pyindex 9f08609278c..28f2616ba7e 100644--- a/agent/curator.py+++ b/agent/curator.py@@ -16,6 +16,7 @@ import reimport threadingimport timefrom collections import Counter+from contextvars import copy_contextfrom datetime import datetime, timedelta, timezonefrom pathlib import Pathfrom typing import Any, Callable, Dict, List, NamedTuple, Optional, Set@@ -983,7 +984,16 @@ def run_curator_review(if synchronous:_llm_pass()else:- threading.Thread(target=_llm_pass, daemon=True, name="curator-review").start()+ # The curator tick runs inside profile_scoped_chore() on a multiplexed gateway, which+ # installs the home override and secret scope as contextvars. A bare thread starts with+ # an EMPTY context, so _llm_pass used to lose the profile scope: provider resolution hit+ # UnscopedSecretError and every home lookup (skill snapshot, run.json/REPORT.md,+ # .curator_state) fell back to the process home — the root home's library was read,+ # reported and overwritten under another profile's run. Copy the caller's context into+ # the thread, the same way the gateway already carries scope into executor work+ # (_run_in_executor_with_context, MCP discovery #95518).+ ctx = copy_context()+ threading.Thread(target=ctx.run, args=(_llm_pass,), daemon=True, name="curator-review").start()return {"started_at": start.isoformat(), "auto_transitions": counts, "summary_so_far": auto_summary}diff --git a/tests/agent/test_curator.py b/tests/agent/test_curator.pyindex 13d63b2a81c..7b154609632 100644--- a/tests/agent/test_curator.py+++ b/tests/agent/test_curator.py@@ -7,6 +7,7 @@ tests run fully offline and the curator module doesn't need real credentials.from __future__ import annotationsimport importlib+import jsonimport threadingfrom datetime import datetime, timedelta, timezonefrom pathlib import Path@@ -1121,3 +1122,39 @@ def test_review_fork_seeds_shared_read_marks(curator_env, monkeypatch):"run_conversation, or every copied tool-worker context keeps private ""marks and the read-before-write guard refuses all patches")+++def test_threaded_llm_pass_keeps_callers_profile_scope(curator_env, tmp_path, monkeypatch):+ """#125032: the daemon ``curator-review`` thread must inherit the caller's contextvars (home+ override + secret scope), else on a multiplexed gateway it runs unscoped against the ROOT home."""+ from agent import secret_scope as ss+ from hermes_constants import get_hermes_home, reset_hermes_home_override, set_hermes_home_override++ c, root = curator_env["curator"], curator_env["home"]+ profile = tmp_path / "profiles" / "served"+ (profile / "skills").mkdir(parents=True)+ seen = {}++ def _pass(prefix, auto_summary, dry_run, before_names):+ seen["home"] = get_hermes_home()+ seen["secret"] = ss.get_secret("CURATOR_PROBE_KEY")+ return f"{prefix}{auto_summary}; llm: stub", c._llm_meta("stub")++ monkeypatch.setattr(c, "_consolidation_pass", _pass)+ ss.set_multiplex_active(True)+ home_tok = set_hermes_home_override(profile)+ scope_tok = ss.set_secret_scope({"CURATOR_PROBE_KEY": "served"}, profile_home=str(profile))+ try:+ c.run_curator_review(synchronous=False, consolidate=True)+ for t in threading.enumerate():+ if t.name == "curator-review":+ t.join(timeout=10.0)+ finally:+ ss.reset_secret_scope(scope_tok)+ reset_hermes_home_override(home_tok)+ ss.set_multiplex_active(False)++ assert seen == {"home": profile, "secret": "served"}+ assert not (root / "skills" / ".curator_state").exists(), "thread wrote the ROOT home's state"+ state = json.loads((profile / "skills" / ".curator_state").read_text(encoding="utf-8-sig"))+ assert state["last_run_summary"].endswith("llm: stub")