The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: first contact goes back to the profile-build offer
alt-glitch4391c24d95eb
diff --git a/agent/onboarding.py b/agent/onboarding.pyindex a96367e214b..a2a02219cad 100644--- a/agent/onboarding.py+++ b/agent/onboarding.py@@ -118,14 +118,15 @@ def _onboarding_section(config: Mapping[str, Any]) -> Mapping[str, Any]:def profile_build_mode(config: Mapping[str, Any]) -> str:"""``config.onboarding.profile_build``: ``"off"`` never offers; anything else -> ``"ask"``.- Only governs whether the offer is made.+ Only governs whether the offer is made; lookups inside the flow are+ consented to separately in conversation."""mode = _onboarding_section(config).get("profile_build")return "off" if isinstance(mode, str) and mode.strip().lower() == "off" else "ask"# Shared by both first-contact notes so a real first-message task is never-# replaced by the intro.+# replaced by the intro (plain or profile-build offer).TASK_FIRST_CLAUSE = ("If this message is itself a real request or task, DO THE TASK FIRST -- call ""whatever tools it needs -- and only then, in the closing sentences of that same "@@ -140,22 +141,6 @@ PLAIN_INTRO_NOTE = ("available commands, in one or two sentences.]")-SETUP_OFFER_NOTE = (- "[System note: This is the user's very first message ever. "- + TASK_FIRST_CLAUSE- + "What this note asks: briefly introduce yourself, mention that /help shows available "- "commands, and end with this one line: \"I can run a quick setup so I can help you better. "- "Send {command} when you want it.\"]"-)---# Telegram commands take no hyphens, and Slack reaches /initiate-setup through /hermes (50-slash cap).-_SETUP_COMMAND_BY_PLATFORM = {"telegram": "/initiate_setup", "slack": "/hermes initiate-setup"}---def setup_command(platform: str) -> str:- """How a platform's user types /initiate-setup."""- return _SETUP_COMMAND_BY_PLATFORM.get(platform, "/initiate-setup")def first_contact_turn_note(config: Mapping[str, Any],@@ -163,32 +148,18 @@ def first_contact_turn_note(*,session_history_empty: bool,install_has_prior_sessions: bool,- message: str,- command: str = "/initiate-setup",- setup_handoff: bool = False,onboarding_run_active: bool = False,) -> Optional[str]:"""Return a one-shot sidecar note for the install's first-ever message.Matches the gateway first-contact path: when ``profile_build`` is ``ask``- and the offer has not been latched yet, return the opt-in+ and the offer has not been latched yet, return the opt-in profile-builddirective and persist ``onboarding.seen.profile_build_offered``. Otherwisereturn the plain intro note. Returns ``None`` when this is not the first- contact (non-empty session history or prior sessions exist on the install). ``setup_handoff``- marks the task chat setup started (``start_chat``), told by the caller, never read from the text.+ contact (non-empty session history or prior sessions exist on the install).``onboarding_run_active``: the chat is running first-run onboarding, whose opening replaces this note."""- from agent.initiate_setup_prompt import HEADER- from hermes_cli.profiles import SETUP_PROFILE_MARKER-- if (not session_history_empty or install_has_prior_sessions or onboarding_run_active- or message.startswith(HEADER)):- return None- # The task chat setup hands off to is the user's first chat in their own profile; setup already ran.- if setup_handoff:- return None- # The setup chat opens with its own welcome, whatever its first message says.- if (config_path.parent / SETUP_PROFILE_MARKER).is_file():+ if not session_history_empty or install_has_prior_sessions or onboarding_run_active:return Nonetry:if (@@ -196,10 +167,7 @@ def first_contact_turn_note(and not is_seen(config, PROFILE_BUILD_FLAG)):mark_seen(config_path, PROFILE_BUILD_FLAG)- from hermes_cli.anon_auth import guest_enabled-- # /initiate-setup is the offer only where the setup flow ships; elsewhere keep the memory profile offer.- return SETUP_OFFER_NOTE.format(command=command) if guest_enabled() else profile_build_directive().strip()+ return profile_build_directive().strip()return PLAIN_INTRO_NOTEexcept Exception as e:logger.debug("first_contact_turn_note failed, using plain intro: %s", e)@@ -266,7 +234,6 @@ __all__ = ["OPENCLAW_RESIDUE_FLAG","PLAIN_INTRO_NOTE","PROFILE_BUILD_FLAG",- "SETUP_OFFER_NOTE","TOOL_PROGRESS_FLAG","busy_input_hint_cli","busy_input_hint_gateway",@@ -278,7 +245,6 @@ __all__ = ["openclaw_residue_hint_cli","profile_build_directive","profile_build_mode",- "setup_command","tool_progress_hint_cli","tool_progress_hint_gateway",]diff --git a/gateway/run_turn.py b/gateway/run_turn.pyindex 068f8240ed7..d0f94d61615 100644--- a/gateway/run_turn.py+++ b/gateway/run_turn.py@@ -1408,7 +1408,7 @@ class GatewayTurnMixin:)return bounded- async def _hmwa_first_contact_notes(self, source, history, turn_sidecar_notes, message, internal=False):+ async def _hmwa_first_contact_notes(self, source, history, turn_sidecar_notes, internal=False):"""First-ever-message onboarding note + one-time 'no home channel' prompt (both only whenthe session has no history). Delivered on the user message (sidecar), NOT the ephemeralsystem prompt: present-on-turn-1/absent-on-turn-2 was a guaranteed prompt diff + rebuild."""@@ -1417,13 +1417,13 @@ class GatewayTurnMixin:returnhuman_platform = bool(source.platform) and source.platform not in (Platform.LOCAL, Platform.WEBHOOK)if human_platform and source.chat_type == "dm" and not await self.async_session_store.has_any_sessions():- # Same branch logic as the TUI (offer once when "ask", else plain intro);+ # Same branch logic as the TUI (profile-build offer once when "ask", else plain intro);# first_contact_turn_note already falls back to the plain intro on error.- from agent.onboarding import first_contact_turn_note, setup_command+ from agent.onboarding import first_contact_turn_notenote = first_contact_turn_note(_load_gateway_config(), _gateway_config_home() / "config.yaml",- session_history_empty=True, install_has_prior_sessions=False, message=message,- command=setup_command(source.platform.value))+ session_history_empty=True, install_has_prior_sessions=False,+ )if note:turn_sidecar_notes.append(note)@@ -2101,7 +2101,7 @@ class GatewayTurnMixin:self._clear_session_env(_session_env_tokens)return t("gateway.errors.history_unavailable"), _session_env_tokens- await self._hmwa_first_contact_notes(source, history, turn_sidecar_notes, event.text, internal=event.internal)+ await self._hmwa_first_contact_notes(source, history, turn_sidecar_notes, internal=event.internal)# Voice channel state rides the user message ONLY when changed (in the system prompt it# forced a rebuild + prompt-cache re-key per message).diff --git a/tests/agent/test_onboarding.py b/tests/agent/test_onboarding.pyindex 260626a2b8f..b23096f8953 100644--- a/tests/agent/test_onboarding.py+++ b/tests/agent/test_onboarding.py@@ -3,7 +3,6 @@from __future__ import annotationsimport hermes_yaml as yaml-import pytestfrom agent.onboarding import (BUSY_INPUT_FLAG,@@ -99,20 +98,13 @@ class TestProfileBuildMode:class TestFirstContactTurnNote:- @pytest.mark.parametrize("guest", [False, True])- def test_first_contact_offer_follows_the_onboarding_gate(self, tmp_path, monkeypatch, guest):- # The /initiate-setup offer ships with guest onboarding; everyone else keeps the memory profile offer.+ def test_returns_profile_directive_and_marks_seen(self, tmp_path):from agent.onboarding import (PROFILE_BUILD_FLAG,- SETUP_OFFER_NOTE,first_contact_turn_note,profile_build_directive,)- if guest:- monkeypatch.setenv("HERMES_GUEST_ONBOARDING", "1")- else:- monkeypatch.delenv("HERMES_GUEST_ONBOARDING", raising=False)cfg_path = tmp_path / "config.yaml"cfg = {"onboarding": {"profile_build": "ask"}}note = first_contact_turn_note(@@ -120,15 +112,13 @@ class TestFirstContactTurnNote:cfg_path,session_history_empty=True,install_has_prior_sessions=False,- message="hello",)- expected = SETUP_OFFER_NOTE.format(command="/initiate-setup") if guest else profile_build_directive().strip()- assert note == expected+ assert note == profile_build_directive().strip()loaded = yaml.safe_load(cfg_path.read_text())assert loaded["onboarding"]["seen"][PROFILE_BUILD_FLAG] is Truedef test_every_first_contact_note_puts_a_real_task_first(self, tmp_path):- # Default "ask" (offer) and "off" (plain intro) must both+ # Default "ask" (profile-build offer) and "off" (plain intro) must both# tell the model to do a first-message task before the intro/offer.from agent.onboarding import TASK_FIRST_CLAUSE, first_contact_turn_note@@ -138,44 +128,9 @@ class TestFirstContactTurnNote:tmp_path / f"{mode}.yaml",session_history_empty=True,install_has_prior_sessions=False,- message="hello",)assert TASK_FIRST_CLAUSE in note, mode- def test_no_note_in_the_task_chat_setup_hands_off_to(self, tmp_path, monkeypatch):- # The first-task chat is the first chat in the user's own profile, but setup already ran:- # no setup offer, no intro, and the offer latch stays unset.- from agent.first_task_prompt import MARKER- from agent.onboarding import PROFILE_BUILD_FLAG, first_contact_turn_note-- monkeypatch.setenv("HERMES_GUEST_ONBOARDING", "1")- cfg_path = tmp_path / "config.yaml"- note = first_contact_turn_note(- {"onboarding": {"profile_build": "ask"}},- cfg_path,- session_history_empty=True,- install_has_prior_sessions=False,- message=f"Set up Blender for me{MARKER}skill text",- setup_handoff=True,- )- assert note is None- assert not cfg_path.exists() or PROFILE_BUILD_FLAG not in cfg_path.read_text()-- def test_a_first_message_quoting_the_handoff_marker_still_gets_the_offer(self, tmp_path, monkeypatch):- # Only start_chat marks a handoff; a user who types the marker text is still a first contact.- from agent.first_task_prompt import MARKER- from agent.onboarding import SETUP_OFFER_NOTE, first_contact_turn_note-- monkeypatch.setenv("HERMES_GUEST_ONBOARDING", "1")- note = first_contact_turn_note(- {"onboarding": {"profile_build": "ask"}},- tmp_path / "config.yaml",- session_history_empty=True,- install_has_prior_sessions=False,- message=f"what does this mean?{MARKER}",- )- assert note == SETUP_OFFER_NOTE.format(command="/initiate-setup")-def test_returns_none_when_not_first_contact(self, tmp_path):from agent.onboarding import first_contact_turn_note@@ -186,7 +141,6 @@ class TestFirstContactTurnNote:cfg_path,session_history_empty=False,install_has_prior_sessions=False,- message="hello",)is None)@@ -196,7 +150,6 @@ class TestFirstContactTurnNote:cfg_path,session_history_empty=True,install_has_prior_sessions=True,- message="hello",)is None)diff --git a/tests/gateway/test_first_contact_dm.py b/tests/gateway/test_first_contact_dm.pynew file mode 100644index 00000000000..ebeaabba43c--- /dev/null+++ b/tests/gateway/test_first_contact_dm.py@@ -0,0 +1,41 @@+"""A messaging gateway's first-ever DM carries the baseline profile-build offer."""++from unittest.mock import AsyncMock++import hermes_yaml as yaml+import pytest++from gateway.config import GatewayConfig, Platform+from gateway.run import GatewayRunner+from gateway.session import SessionSource, SessionStore+++def _runner(tmp_path, monkeypatch):+ monkeypatch.setattr("gateway.run._hermes_home", tmp_path)+ monkeypatch.setenv("TELEGRAM_HOME_CHANNEL", "home")+ runner = GatewayRunner.__new__(GatewayRunner)+ runner.config = GatewayConfig()+ runner.session_store = SessionStore(sessions_dir=tmp_path / "sessions", config=runner.config)+ runner._deliver_platform_notice = AsyncMock()+ return runner++[email protected]("guest", ["1", "0"])+async def test_first_dm_gets_the_profile_build_offer_once(tmp_path, monkeypatch, guest):+ from agent.onboarding import PROFILE_BUILD_FLAG, profile_build_directive++ monkeypatch.setenv("HERMES_GUEST_ONBOARDING", guest)+ (tmp_path / "config.yaml").write_text("model: test\n", encoding="utf-8")+ runner = _runner(tmp_path, monkeypatch)+ source = SessionSource(platform=Platform.TELEGRAM, chat_id="dm", user_id="u", chat_type="dm")++ first: list[str] = []+ await runner._hmwa_first_contact_notes(source, [], first)+ second: list[str] = []+ await runner._hmwa_first_contact_notes(source, [], second)++ assert first == [profile_build_directive().strip()]+ assert len(second) == 1 and second[0] != first[0]+ seen = yaml.safe_load((tmp_path / "config.yaml").read_text(encoding="utf-8"))["onboarding"]["seen"]+ assert seen[PROFILE_BUILD_FLAG] is Truediff --git a/tests/tui_gateway/test_first_contact_onboarding.py b/tests/tui_gateway/test_first_contact_onboarding.pyindex c13cde76f0e..9b016995d6c 100644--- a/tests/tui_gateway/test_first_contact_onboarding.py+++ b/tests/tui_gateway/test_first_contact_onboarding.py@@ -1,6 +1,6 @@-"""Desktop/TUI first-contact onboarding via tui_gateway (#82750).+"""Desktop/TUI first-contact profile-build onboarding via tui_gateway (#82750).-The messaging gateway stages the offer on the+The messaging gateway stages the consent-gated profile-build offer on theinstall's very first message (gateway/run_turn.py ``_hmwa_first_contact_notes``);the TUI/Desktop surface must do the same through``_stage_first_contact_onboarding_note`` in the prompt turn.@@ -13,7 +13,7 @@ import typesimport pytest-from agent.onboarding import PROFILE_BUILD_FLAG, SETUP_OFFER_NOTE, profile_build_directive+from agent.onboarding import PROFILE_BUILD_FLAG, profile_build_directivefrom hermes_yaml import safe_dump, safe_loadfrom tui_gateway import server@@ -38,34 +38,22 @@ def onboarding_home(monkeypatch, tmp_path):def _stage(session, agent, history_empty):- server._stage_first_contact_onboarding_note(session, agent, history_empty, "hello")+ server._stage_first_contact_onboarding_note(session, agent, history_empty)-def test_stages_setup_offer_on_first_contact(monkeypatch, onboarding_home):- """Fresh install + empty history under guest onboarding: the /initiate-setup offer is staged- on the agent and the offered flag is persisted before the turn runs."""- monkeypatch.setenv("HERMES_GUEST_ONBOARDING", "1")+def test_stages_profile_build_directive_on_first_contact(monkeypatch, onboarding_home):+ """Fresh install + empty history: the opt-in directive is staged on the agent+ and the offered flag is persisted before the turn runs."""monkeypatch.setattr(server, "_install_has_prior_sessions", lambda _s: False)agent = types.SimpleNamespace()_stage(_session(agent), agent, history_empty=True)- assert agent._gateway_turn_context_notes == SETUP_OFFER_NOTE.format(command="/initiate-setup")+ assert agent._gateway_turn_context_notes == profile_build_directive().strip()loaded = safe_load((onboarding_home / "config.yaml").read_text())assert loaded["onboarding"]["seen"][PROFILE_BUILD_FLAG] is True-def test_stages_profile_build_offer_without_guest_onboarding(monkeypatch, onboarding_home):- """Without guest onboarding the first message keeps main's memory profile offer."""- monkeypatch.delenv("HERMES_GUEST_ONBOARDING", raising=False)- monkeypatch.setattr(server, "_install_has_prior_sessions", lambda _s: False)-- agent = types.SimpleNamespace()- _stage(_session(agent), agent, history_empty=True)-- assert agent._gateway_turn_context_notes == profile_build_directive().strip()--def test_skips_first_contact_when_prior_sessions_exist(monkeypatch, onboarding_home):"""An install that already holds conversations must not re-offer; nothing isstaged and the config is untouched."""@@ -141,16 +129,3 @@ def test_installs_prior_sessions_probe_counts_the_install(monkeypatch):monkeypatch.setattr(server, "_session_db", lambda _s: _Ctx(_DB(2)))assert server._install_has_prior_sessions(fresh) is True---def test_the_task_chat_setup_hands_off_to_gets_no_note(monkeypatch, onboarding_home):- """start_chat marks the session it opens from setup; the prompt turn passes that on."""- monkeypatch.setenv("HERMES_GUEST_ONBOARDING", "1")- monkeypatch.setattr(server, "_install_has_prior_sessions", lambda _s: False)-- agent = types.SimpleNamespace()- session = {**_session(agent), "setup_handoff": True}- _stage(session, agent, history_empty=True)-- assert getattr(agent, "_gateway_turn_context_notes", None) is None- assert PROFILE_BUILD_FLAG not in (onboarding_home / "config.yaml").read_text()diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.pyindex 95dd75bfc22..af72efa51eb 100644--- a/tui_gateway/prompt_turn.py+++ b/tui_gateway/prompt_turn.py@@ -621,12 +621,13 @@ def _install_has_prior_sessions(session: dict) -> bool:return False-def _stage_first_contact_onboarding_note(session: dict, agent, history_empty: bool, message: str) -> None:+def _stage_first_contact_onboarding_note(session: dict, agent, history_empty: bool) -> None:"""Stage the install's first-message onboarding note for THIS turn (#82750).- The messaging gateway appends the directive to+ The messaging gateway appends the consent-gated profile-build directive tothe very first message ever (``_hmwa_first_contact_notes``); the- TUI/Desktop surface never did. Stage the same note through+ TUI/Desktop surface never did, so a fresh install's first Desktop chat+ skipped the opt-in profile flow entirely. Stage the same note through``agent._gateway_turn_context_notes`` — consumed by``agent.turn_context`` on the user message — never the ephemeral systemprompt, which must stay byte-stable for the conversation (prompt-cache@@ -643,8 +644,6 @@ def _stage_first_contact_onboarding_note(session: dict, agent, history_empty: boget_hermes_home() / "config.yaml",session_history_empty=history_empty,install_has_prior_sessions=_install_has_prior_sessions(session),- message=message,- setup_handoff=bool(session.get("setup_handoff")),onboarding_run_active="setup_beat" in (getattr(agent, "valid_tool_names", None) or ()),)if not note:@@ -697,7 +696,7 @@ def _prepare_turn_input(sid: str, session: dict, st: _TurnRun, text: Any, imagesst.history_version = int(session.get("history_version", 0))# Install-first-message onboarding (#82750): gateway parity for the TUI/Desktop# surface — no-op unless this is the install's very first message ever.- _stage_first_contact_onboarding_note(session, agent, not st.history, text if isinstance(text, str) else "")+ _stage_first_contact_onboarding_note(session, agent, not st.history)_stage_onboarding_turn_note(sid, agent, not st.history)cwd = _session_cwd(session)_register_session_cwd(session)