fix(session): remove message time replay prefixes

This commit is contained in:
chengyongru
2026-06-27 11:04:36 +08:00
committed by Xubin Ren
parent 8656549129
commit 9b45fc1172
6 changed files with 21 additions and 53 deletions
+1 -1
View File
@@ -223,7 +223,7 @@ class TestAgentLoopTTLParam:
kwargs = session.get_history.call_args.kwargs
assert isinstance(kwargs.get("max_tokens"), int)
assert kwargs["max_tokens"] > 0
assert kwargs["include_timestamps"] is True
assert set(kwargs) == {"max_messages", "max_tokens", "extend_to_user"}
@pytest.mark.asyncio
async def test_session_file_cap_archives_and_trims_old_messages(self, tmp_path):
+3 -5
View File
@@ -1222,11 +1222,9 @@ async def test_system_subagent_followup_is_persisted_before_prompt_assembly(tmp_
non_system = [m for m in seen["initial_messages"] if m.get("role") != "system"]
assert "question" in non_system[0]["content"]
assert "working" in non_system[1]["content"]
# User turns carry the timestamp prefix so the model can reason about
# relative time. Assistant turns do NOT, otherwise the model treats those
# past replies as in-context examples and starts its own outputs with
# ``[Message Time: ...]`` (which then leaks back to the user).
assert "[Message Time:" in non_system[0]["content"]
# Persisted timestamps stay in session records, but replay content is not
# rewritten with volatile ``[Message Time: ...]`` prefixes.
assert "[Message Time:" not in non_system[0]["content"]
assert "[Message Time:" not in non_system[1]["content"]
assert non_system[2]["content"].count("subagent result") == 1
assert "Current Time:" in non_system[2]["content"]
+15 -16
View File
@@ -266,13 +266,8 @@ def test_get_history_preserves_reasoning_content():
]
def test_get_history_annotates_user_turns_but_not_assistant_turns():
"""Only user turns carry the timestamp prefix.
Annotating assistant turns trains the model (via in-context examples) to
start its own replies with ``[Message Time: ...]``. User-side stamps are
enough to pin adjacent assistant replies for relative-time reasoning.
"""
def test_get_history_does_not_inject_persisted_timestamps_into_replay_content():
"""Persisted timestamps are session metadata, not prompt content."""
session = Session(key="test:timestamps")
session.messages.append({
"role": "user",
@@ -285,12 +280,14 @@ def test_get_history_annotates_user_turns_but_not_assistant_turns():
"timestamp": "2026-04-26T22:00:05",
})
history = session.get_history(max_messages=500, include_timestamps=True)
history = session.get_history(max_messages=500)
assert session.messages[0]["timestamp"] == "2026-04-26T22:00:00"
assert session.messages[1]["timestamp"] == "2026-04-26T22:00:05"
assert history == [
{
"role": "user",
"content": "[Message Time: 2026-04-26T22:00:00]\n10 点提醒是昨天发生的",
"content": "10 点提醒是昨天发生的",
},
{
"role": "assistant",
@@ -299,8 +296,8 @@ def test_get_history_annotates_user_turns_but_not_assistant_turns():
]
def test_get_history_does_not_annotate_proactive_assistant_deliveries_with_timestamps():
"""Assistant-side timestamp examples can leak back into future replies."""
def test_get_history_keeps_proactive_delivery_timestamps_out_of_replay_content():
"""Timestamp metadata remains persisted without becoming prompt text."""
session = Session(key="test:proactive-timestamps")
session.messages.append({
"role": "assistant",
@@ -314,8 +311,10 @@ def test_get_history_does_not_annotate_proactive_assistant_deliveries_with_times
"timestamp": "2026-04-26T18:00:00",
})
history = session.get_history(max_messages=500, include_timestamps=True)
history = session.get_history(max_messages=500)
assert session.messages[0]["timestamp"] == "2026-04-26T15:00:00"
assert session.messages[1]["timestamp"] == "2026-04-26T18:00:00"
assert history == [
{
"role": "assistant",
@@ -323,18 +322,18 @@ def test_get_history_does_not_annotate_proactive_assistant_deliveries_with_times
},
{
"role": "user",
"content": "[Message Time: 2026-04-26T18:00:00]\n",
"content": "",
},
]
def test_get_history_does_not_annotate_tool_results_with_timestamps():
def test_get_history_does_not_inject_tool_result_timestamps():
session = Session(key="test:tool-timestamps")
session.messages.append({"role": "user", "content": "run tool"})
session.messages.extend(_tool_turn("ts", 0))
session.messages[-1]["timestamp"] = "2026-04-26T22:00:10"
history = session.get_history(max_messages=500, include_timestamps=True)
history = session.get_history(max_messages=500)
tool_result = history[-1]
assert tool_result["role"] == "tool"
@@ -555,7 +554,7 @@ def test_get_history_sanitizes_existing_assistant_replay_artifacts():
}
)
history = session.get_history(max_messages=500, include_timestamps=True)
history = session.get_history(max_messages=500)
assert history == [{"role": "assistant", "content": "来了 🎨"}]