Skip to content

The agent that grows with you

fix(agent): don't restore a foreign Session ID or pin an empty workspace snapshot

teknium1027809f88eae
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index d830d525588..fb79d6242f3 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -822,6 +822,12 @@ def _stored_prompt_matches_runtime(agent, prompt: str) -> bool:
current = str(getattr(agent, attr, "") or "").strip()
if stored and current and stored != current:
return False
+ # A prompt stamped for another session (a /branch child copies its parent's bytes) must not
+ # tell the model a foreign Session ID. Checked only when the trailer is on: with it off, a
+ # "Session ID:" line in project text would read as a mismatch and rebuild every turn.
+ stored_sid = identity_line_value(prompt, "Session ID")
+ if stored_sid and getattr(agent, "pass_session_id", False) and stored_sid != agent.session_id:
+ return False
# Compare against resolve_agent_cwd() — the SAME resolver used to build the
# prompt — so TERMINAL_CWD sessions are not falsely rejected.
stored_cwd = runtime_host_value(prompt, "Current working directory")
diff --git a/agent/system_prompt.py b/agent/system_prompt.py
index dd9a1e3173d..853462571cc 100644
--- a/agent/system_prompt.py
+++ b/agent/system_prompt.py
@@ -650,9 +650,9 @@ def _seed_workspace_pin(agent: Any, key: str) -> None:
if stored_cwd and stored_cwd != key:
return
block = _persisted_workspace_block(prompt, key)
- # "" is only trustworthy when the prompt names this cwd: a legacy prompt without the
- # runtime line might have been built with tools off.
- if block or (block == "" and stored_cwd):
+ # Only a real snapshot is adopted: a prompt without one (built on a surface without the
+ # coding posture, or with tools off) leaves the pin open so this build captures one.
+ if block:
agent._frozen_workspace_snapshot = (key, block)
diff --git a/tests/agent/test_compaction_prompt_rebuild.py b/tests/agent/test_compaction_prompt_rebuild.py
index a9f40ab3090..c6f4545d5ef 100644
--- a/tests/agent/test_compaction_prompt_rebuild.py
+++ b/tests/agent/test_compaction_prompt_rebuild.py
@@ -283,6 +283,27 @@ class TestWorkspaceSnapshotPinnedAcrossCompaction(unittest.TestCase):
finally:
shutil.rmtree(tmp, ignore_errors=True)
+ def test_persisted_prompt_without_a_snapshot_does_not_pin_an_empty_one(self):
+ """A session row built where no workspace block was emitted (a messaging surface) and
+ resumed in the same repo must capture a real snapshot, not pin "no workspace" for good."""
+ import tempfile, shutil
+ from pathlib import Path
+ from agent.system_prompt import build_system_prompt
+
+ tmp = Path(tempfile.mkdtemp(prefix="test-pinned-empty-"))
+ try:
+ repo = _init_repo(tmp / "proj", "init commit")
+ stored = f"Host: x\nUser home directory: /h\nCurrent working directory: {repo}\n\nBODY"
+ db = SimpleNamespace(get_session=lambda sid: {"system_prompt": stored})
+ with patch("agent.prompt_builder.load_soul_md", return_value=""), \
+ patch("agent.prompt_builder.build_environment_hints",
+ return_value=f"Host: x\nUser home directory: /h\nCurrent working directory: {repo}"), \
+ patch("agent.system_prompt.resolve_context_cwd", return_value=repo):
+ resumed = self._pin_agent(_cached_system_prompt=None, _session_db=db)
+ self.assertIn(f"- Root: {repo}", build_system_prompt(resumed))
+ finally:
+ shutil.rmtree(tmp, ignore_errors=True)
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/agent/test_system_prompt.py b/tests/agent/test_system_prompt.py
index b14f0384b5f..f9d680985c5 100644
--- a/tests/agent/test_system_prompt.py
+++ b/tests/agent/test_system_prompt.py
@@ -280,6 +280,19 @@ def test_stored_prompt_cwd_ignores_project_host_decoys(monkeypatch, tmp_path):
assert _stored_prompt_matches_runtime(agent, legacy)
+def test_stored_prompt_stamped_for_another_session_is_not_restored(monkeypatch, tmp_path):
+ """With the Session ID trailer on, a prompt persisted for another session (a /branch child
+ copies its parent's bytes) must rebuild instead of telling the model the parent's id."""
+ from agent.conversation_loop import _stored_prompt_matches_runtime
+
+ monkeypatch.setenv("TERMINAL_ENV", "local")
+ monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
+ fields = dict(platform="cli", model="test-model", provider="test-provider", pass_session_id=True)
+ parent_prompt = build_system_prompt(_make_agent(session_id="parent-sid", **fields))
+ assert _stored_prompt_matches_runtime(_make_agent(session_id="parent-sid", **fields), parent_prompt)
+ assert not _stored_prompt_matches_runtime(_make_agent(session_id="child-sid", **fields), parent_prompt)
+
+
class TestExecutionGuidanceInjection:
"""Injection gate for OPENAI_MODEL_EXECUTION_GUIDANCE via
``agent.execution_guidance`` (auto/true/false/list).