The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(onboarding): an ending closes the install flag first, and deleting a compression parent ends nothing
alt-glitch2787212ce8b9
diff --git a/agent/onboarding_record.py b/agent/onboarding_record.pyindex 40fc68b81e0..cf155e7a433 100644--- a/agent/onboarding_record.py+++ b/agent/onboarding_record.py@@ -8,6 +8,7 @@ run ends write the install-level flag in the root config so the next launch doesfrom __future__ import annotations+import copyimport jsonimport loggingimport time@@ -38,12 +39,22 @@ def load(db, session_id: str) -> dict | None:return record if isinstance(record, dict) else None+def _ends_last_open_run(db, session_id: str, event: Event) -> bool:+ current = load(db, session_id)+ if not is_active(current):+ return False+ after = apply(copy.deepcopy(current), event, Facts.from_stored(current["facts"]), now=time.time()).record+ return not is_active(after) and all(sid == session_id for sid in open_session_ids(db))++def _transition(db, session_id: str, event: Event) -> Step | None:"""Apply *event* and write the result in one database transaction at the compression tip, so Skipsetup pressed while a card answer is being applied is never overwritten by the older copy, and an id- read before a rotation published still lands on the copy the chat reads. An ended run then closes the- install flag (a Skip or Done retried after a failed config write repairs it), unless another run- is still open: the flag belongs to that run, and an old chat must not end it."""+ read before a rotation published still lands on the copy the chat reads. An ending closes the install+ flag first: a failed config write raises before the run ends, so the window keeps the run and the+ same Skip, Done or delete can be retried. Another open run keeps the flag; it belongs to that run."""+ if _ends_last_open_run(db, session_id, event):+ _close_install_flag()holder: dict[str, Any] = {}def update(current: Any) -> Any:@@ -63,7 +74,7 @@ def _transition(db, session_id: str, event: Event) -> Step | None:for listener in _ended_listeners:listener(ended)if step is not None and not is_active(step.record) and active_session_id(db) is None:- _close_install_flag()+ _close_install_flag() # no-op unless a concurrent change ended the run after the check abovereturn step@@ -120,10 +131,10 @@ def active_session_id(db) -> str | None:def end_before_delete(db, session_id: str) -> None:"""Deleting the onboarding chat ends its run first (``deleted``), so the install flag closes and the- next launch does not open a fresh run. The run lives at the chat's compression tip."""- for sid in dict.fromkeys((session_id, db.resolve_resume_session_id(session_id) or session_id)):- if is_active(load(db, sid)):- advance(db, sid, End("deleted"))+ next launch does not open a fresh run. Only the compression tip holds the live run: deleting a+ parent keeps the tip, and the parent's copy is a stale one."""+ if (db.resolve_resume_session_id(session_id) or session_id) == session_id and is_active(load(db, session_id)):+ advance(db, session_id, End("deleted"))def observe_tool_result(agent, name: str, args: Mapping[str, Any], result: Any) -> Any:diff --git a/tests/tui_gateway/test_onboarding_session.py b/tests/tui_gateway/test_onboarding_session.pyindex 34ca020b387..1467756a88d 100644--- a/tests/tui_gateway/test_onboarding_session.py+++ b/tests/tui_gateway/test_onboarding_session.py@@ -224,6 +224,9 @@ def test_a_compression_rotation_carries_the_run_record(root):assert db.get_session_model_config_value("child", "codex_thread_id") is None# The parent keeps its active copy; the run is read at the tip, so skipping the child ends it.assert onboarding_record.active_session_id(db) == "child"+ # Deleting the parent keeps the tip, so the run stays in progress there.+ onboarding_record.end_before_delete(db, "parent")+ assert onboarding_record.active_session_id(db) == "child"onboarding_record.advance(db, "child", End())assert onboarding_record.active_session_id(db) is None@@ -246,7 +249,7 @@ def test_skip_that_read_the_id_before_a_rotation_ends_the_run_at_the_tip(root, massert read_user_config_raw(root / "config.yaml")["onboarding"]["run"] is False-def test_a_skip_retried_after_a_failed_config_write_closes_the_install_flag(root, monkeypatch):+def test_a_failed_config_write_leaves_the_run_open_so_the_skip_can_be_retried(root, monkeypatch):from hermes_cli import onboarding_runfrom hermes_cli.config import read_user_config_rawdb = server._get_db()@@ -257,15 +260,18 @@ def test_a_skip_retried_after_a_failed_config_write_closes_the_install_flag(rootdef disk_full(value, **kw):raise OSError("disk full")+ ended: list[dict] = []+ monkeypatch.setattr(onboarding_record, "_ended_listeners", [ended.append])monkeypatch.setattr(onboarding_run, "set_run", disk_full)with pytest.raises(OSError):onboarding_record.advance(db, "onb", End())monkeypatch.setattr(onboarding_run, "set_run", real_set_run)- assert onboarding_record.load(db, "onb")["phase"] == "skipped"- assert read_user_config_raw(root / "config.yaml")["onboarding"]["run"] is True+ # Nothing ended: no window drops its Skip button, and the next launch resumes this run.+ assert onboarding_record.active_session_id(db) == "onb" and ended == []onboarding_record.advance(db, "onb", End())assert read_user_config_raw(root / "config.yaml")["onboarding"]["run"] is False+ assert [e["outcome"] for e in ended] == ["skipped"]def test_an_old_ended_chat_never_closes_the_flag_of_a_newer_run(root):diff --git a/tui_gateway/methods_onboarding.py b/tui_gateway/methods_onboarding.pyindex 0b743cbf99c..eed9dc9af05 100644--- a/tui_gateway/methods_onboarding.py+++ b/tui_gateway/methods_onboarding.py@@ -88,7 +88,9 @@ def _active_run() -> dict:from agent.onboarding_record import active_session_id, loadwith _profile_db(_ONBOARDING_PROFILE) as db:session_id = None if db is None else active_session_id(db)- return {"session_id": session_id, "run_id": (load(db, session_id) or {}).get("run_id") if session_id else None}+ # "" for a run started before runs had ids, the same value onboarding.start returns for it.+ return {"session_id": session_id,+ "run_id": (load(db, session_id) or {}).get("run_id") or "" if session_id else None}@method("onboarding.start")