Skip to content

Commit f3a6c99

Browse files
committed
Fix save doesn't store default agent.
1 parent 126bcfc commit f3a6c99

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎python_agent_harness/entry/restore.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ def load_saved_session(path: str) -> SavedSession:
7979

8080

8181
def restore_agent(controller: Controller, agent: str | None, report: Report) -> None:
82-
"""Switch to the saved AGENT; fall back to the default agent (so no
83-
stale tool exclusions linger) when it no longer exists."""
82+
"""Switch to the saved AGENT; keep the current agent when the session
83+
was saved without one (pre-agent-era file), and fall back to the
84+
default agent (so no stale tool exclusions linger) when the saved
85+
name no longer exists. A session saved on the default agent records
86+
``default`` explicitly (see SessionPersistence.metadata_block), so it
87+
still switches back even when a custom agent is active now."""
8488
if not agent:
8589
return
8690
try:

‎python_agent_harness/io/persistence.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,18 @@ def first_user_message(self) -> str | None:
170170
# -- saving ---------------------------------------------------------------
171171
def metadata_block(self) -> str:
172172
lines = [";; Local Variables:"]
173+
# The agent is ALWAYS recorded: the built-in default is stored
174+
# as "default" rather than omitted, so "saved on the default
175+
# agent" stays distinguishable from a pre-agent-era file (no
176+
# key at all) and /restore can switch a currently-active custom
177+
# agent back to the default prompt. switch_agent also stores
178+
# None there on a default switch (see Session.switch_agent),
179+
# which is normalized to the reserved name here.
180+
agent = self.agent if self.agent else "default"
173181
pairs = [
174182
("python-agent-harness--project-dir", self.project_dir),
175183
("python-agent-harness--model", self.model),
176-
("python-agent-harness--agent", self.agent),
184+
("python-agent-harness--agent", agent),
177185
]
178186
for name, value in pairs:
179187
if value is None:

‎tests/io/test_persistence.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,18 @@ def test_agent_persisted_in_metadata(self):
7474
parsed = SessionPersistence.parse_metadata(text)
7575
self.assertEqual(parsed["python-agent-harness--agent"], "reviewer")
7676

77-
def test_agent_absent_when_none(self):
78-
"""No custom agent -> no agent line in the metadata block, so
79-
old sessions and default-agent sessions parse identically."""
77+
def test_default_agent_recorded_explicitly(self):
78+
"""A session saved on the built-in default agent records
79+
``default`` explicitly: without it, "saved on default" would be
80+
indistinguishable from a pre-agent-era file (no key at all), and
81+
/restore would keep whatever custom agent happens to be active
82+
in the current session instead of switching back to default."""
8083
store = SessionPersistence(project_dir="/tmp/proj", model="m", agent=None)
81-
self.assertNotIn("python-agent-harness--agent", store.metadata_block())
84+
meta = store.metadata_block()
85+
self.assertIn("python-agent-harness--agent: 'default'", meta)
86+
text = "conversation...\n\n" + meta + "\n"
87+
parsed = SessionPersistence.parse_metadata(text)
88+
self.assertEqual(parsed["python-agent-harness--agent"], "default")
8289

8390
def test_save_and_restore_flow(self):
8491
import os

0 commit comments

Comments
 (0)