Skip to content

Agent Zero AI framework

Document and verify greeting history and log ownership

Alessandrobf31f47c7bb0
diff --git a/extensions/python/agent_init/AGENTS.md b/extensions/python/agent_init/AGENTS.md
index 7d0adbcae..e958fa34c 100644
--- a/extensions/python/agent_init/AGENTS.md
+++ b/extensions/python/agent_init/AGENTS.md
@@ -15,7 +15,7 @@
- Keep a placeholder user turn (`fw.initial_user_message.md`) ahead of the AI greeting (`fw.initial_message.md`) so `output_langchain` never pops the greeting as a leading `AIMessage`; do not remove either prompt without replacing the turn-order guarantee.
- Minify the initial AI message JSON before storage; preserve raw text as the displayed greeting when JSON parsing fails.
- `_10_initial_message.py` passes `LLMResult.non_llm()` to `hist_add_ai_response` so the Responses-API state seam runs uniformly; the sentinel carries no `response_id` and marks `mode=""`/`state="off"` so stored metadata does not claim a Responses-API turn.
-- The synthetic `Hello!` user turn and the AI greeting are intentionally hidden: they have no matching UI log item (branching links log↔history by ID, but these seeding turns predate the log). They serve as a few-shot example of the expected user→AI turn ordering so `output_langchain` does not pop the greeting as a leading `AIMessage`. Do not add log items for seeding turns without replacing the few-shot guarantee.
+- The synthetic `Hello!` user turn has no UI log. The assistant greeting is displayed as a response log with the same ID as its history message, preserving the log/history link for branching. Keep the user-first seed without duplicating the visible greeting on reload.
## Work Guidance
diff --git a/tests/test_initial_message.py b/tests/test_initial_message.py
new file mode 100644
index 000000000..25fff908b
--- /dev/null
+++ b/tests/test_initial_message.py
@@ -0,0 +1,52 @@
+import json
+from types import SimpleNamespace
+
+import pytest
+from langchain_core.messages import HumanMessage, AIMessage
+
+from extensions.python.agent_init._10_initial_message import InitialMessage
+from helpers import history
+from helpers.llm_result import result_from_metadata
+from helpers.log import Log
+
+
[email protected]("greeting", [
+ json.dumps({"tool_name": "response", "tool_args": {"text": "Welcome."}}, indent=2),
+ "Welcome to this profile.",
+])
+def test_initial_greeting_keeps_user_first_order_and_linked_log_after_reload(greeting):
+ messages = []
+ log = Log()
+
+ def add_ai(content, llm_result):
+ message = history.Message(True, content, metadata=llm_result.metadata())
+ messages.append(message)
+ return message
+
+ agent = SimpleNamespace(
+ number=0,
+ context=SimpleNamespace(log=log),
+ read_prompt=lambda name: "Hello!" if name == "fw.initial_user_message.md" else greeting,
+ hist_add_user_message=lambda message: messages.append(history.Message(False, message.message)),
+ hist_add_ai_response=add_ai,
+ )
+ extension = InitialMessage(agent)
+ extension.execute()
+ extension.execute()
+ assert len(messages) == 2
+ assert len(log.logs) == 1
+ assert log.logs[0].id == messages[1].id
+ assert log.logs[0].id != messages[0].id
+
+ reloaded = [history.Message.from_dict(message.to_dict(), history=None) for message in messages]
+ rendered = history.output_langchain([item for message in reloaded for item in message.output()])
+ assert [type(item) for item in rendered] == [HumanMessage, AIMessage]
+ assert rendered[0].content == "Hello!"
+ assert rendered[1].content == messages[1].content
+ result = result_from_metadata(reloaded[1].metadata)
+ assert result.mode == "" and result.state == "off" and not result.response_id
+ if greeting.startswith("{"):
+ assert "\n" not in rendered[1].content
+ assert log.logs[0].content == "Welcome."
+ else:
+ assert log.logs[0].content == greeting