Skip to content

The agent that grows with you

fix: create message_metadata module and update chat_completion_helpers

Tuckca7715763931
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index ad6d0f1320c..e998f1a85d0 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -38,6 +38,7 @@ from agent.turn_context import substitute_api_content
from agent.gemini_native_adapter import is_native_gemini_base_url
from agent.model_metadata import is_local_endpoint
from agent.message_content import flatten_message_text
+from agent.message_metadata import append_message, stamp_message_timestamp
from agent.message_sanitization import (
_sanitize_surrogates,
_repair_tool_call_arguments,
@@ -2156,12 +2157,12 @@ def build_assistant_message(agent, assistant_message, finish_reason: str) -> dic
# a DB-side pad can't survive ``_rows_to_conversation``'s whitespace strip
# anyway. Repair belongs at the send boundary, once.
- msg = {
+ msg = stamp_message_timestamp({
"role": "assistant",
"content": _san_content,
"reasoning": reasoning_text,
"finish_reason": finish_reason,
- }
+ })
raw_reasoning_content = getattr(assistant_message, "reasoning_content", None)
if raw_reasoning_content is None and hasattr(assistant_message, "model_extra"):
@@ -2839,7 +2840,7 @@ def handle_max_iterations(agent, messages: list, api_call_count: int) -> str:
from agent.context_compressor import MAX_ITERATIONS_SUMMARY_REQUEST
summary_request = MAX_ITERATIONS_SUMMARY_REQUEST
- messages.append({"role": "user", "content": summary_request})
+ append_message(messages, {"role": "user", "content": summary_request})
try:
# Build API messages, stripping internal-only fields
@@ -3062,7 +3063,10 @@ def handle_max_iterations(agent, messages: list, api_call_count: int) -> str:
final_response = re.sub(r'<think>.*?</think>\s*', '', final_response, flags=re.DOTALL).strip()
if final_response:
summary_call_outcome = "success"
- messages.append({"role": "assistant", "content": final_response})
+ append_message(
+ messages,
+ {"role": "assistant", "content": final_response},
+ )
else:
final_response = "I reached the iteration limit and couldn't generate a summary."
else:
@@ -3124,7 +3128,10 @@ def handle_max_iterations(agent, messages: list, api_call_count: int) -> str:
final_response = re.sub(r'<think>.*?</think>\s*', '', final_response, flags=re.DOTALL).strip()
if final_response:
summary_call_outcome = "success"
- messages.append({"role": "assistant", "content": final_response})
+ append_message(
+ messages,
+ {"role": "assistant", "content": final_response},
+ )
else:
final_response = "I reached the iteration limit and couldn't generate a summary."
else:
diff --git a/agent/message_metadata.py b/agent/message_metadata.py
new file mode 100644
index 00000000000..284edc523d3
--- /dev/null
+++ b/agent/message_metadata.py
@@ -0,0 +1,41 @@
+"""Internal metadata attached to durable conversation messages."""
+
+from __future__ import annotations
+
+from time import time as wall_time
+from typing import Any, MutableMapping, Optional, TypeVar
+
+
+# These fields describe Hermes' durable record, not provider-visible message
+# content. They must not influence context-pressure decisions.
+PERSISTENCE_ONLY_MESSAGE_FIELDS = frozenset({"timestamp"})
+
+_Message = TypeVar("_Message", bound=MutableMapping[str, Any])
+
+
+def stamp_message_timestamp(
+ message: _Message,
+ *,
+ timestamp: Optional[float] = None,
+) -> _Message:
+ """Attach a creation timestamp without replacing source-provided time.
+
+ Gateway adapters can supply the platform event time. All other callers use
+ the local wall clock at the point the message enters the live transcript.
+ Returning the same mapping keeps the helper convenient at append sites.
+ """
+ if message.get("timestamp") is None:
+ message["timestamp"] = wall_time() if timestamp is None else timestamp
+ return message
+
+
+def append_message(
+ messages: list[Any],
+ message: _Message,
+ *,
+ timestamp: Optional[float] = None,
+) -> _Message:
+ """Stamp and append one live transcript message."""
+ stamp_message_timestamp(message, timestamp=timestamp)
+ messages.append(message)
+ return message