CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
feat(agent): add messages=... pre-seed kw with deep-copy isolation
xf gonge6059245b471
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.pyindex 33a59fc..0e52b53 100644--- a/cubepi/agent/agent.py+++ b/cubepi/agent/agent.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport asyncioimport time+from collections.abc import Sequencefrom dataclasses import dataclass, fieldfrom typing import Callable, Generic, TypeVar@@ -153,13 +154,25 @@ class Agent(Generic[TMessage]):thread_id: str | None = None,middleware: list[Middleware] | None = None,channel: HitlChannel | None = None,+ messages: Sequence[Message] | None = None,) -> None:self._provider: Provider = model.provider+ self._model = modelself._state = AgentState(system_prompt=system_prompt,model=model.spec,thinking=thinking,)+ if messages is not None:+ if thread_id is not None and checkpointer is not None:+ raise ValueError(+ "Agent(messages=...) cannot be combined with "+ "thread_id + checkpointer (pre-seed conflicts with lazy "+ "load). Construct an ephemeral Agent without those for "+ "fork_once-style usage."+ )+ seeded = [m.model_copy(deep=True) for m in messages]+ self._state.messages = list(seeded)if tools:self._state.tools = toolsmiddleware = middleware or []diff --git a/tests/agent/test_agent_messages_kw.py b/tests/agent/test_agent_messages_kw.pynew file mode 100644index 0000000..fb3a3f4--- /dev/null+++ b/tests/agent/test_agent_messages_kw.py@@ -0,0 +1,62 @@+import pytest++from cubepi.agent.agent import Agent+from cubepi.providers.base import (+ AssistantMessage,+ TextContent,+ ToolCall,+ ToolResultMessage,+ UserMessage,+)+from cubepi.providers.faux import FauxProvider+++def _agent(**kw):+ return Agent(model=FauxProvider().model("faux-model"), **kw)+++def test_messages_kw_none_keeps_default():+ a = _agent()+ assert a.state.messages == []+++def test_messages_kw_seeds_initial_history():+ msgs = [UserMessage(content=[TextContent(text="hi")])]+ a = _agent(messages=msgs)+ assert len(a.state.messages) == 1+++def test_messages_kw_conflicts_with_thread_id_checkpointer():+ from cubepi.checkpointer.memory import MemoryCheckpointer++ msgs = [UserMessage(content=[TextContent(text="hi")])]+ with pytest.raises(ValueError):+ _agent(+ messages=msgs,+ thread_id="t",+ checkpointer=MemoryCheckpointer(),+ )+++def test_messages_kw_deep_copies_all_three_variants():+ user = UserMessage(content=[TextContent(text="u")], metadata={"k": "v"})+ assistant = AssistantMessage(+ content=[ToolCall(id="c1", name="t", arguments={"k": [1, 2]})],+ metadata={},+ )+ tool = ToolResultMessage(+ tool_call_id="c1", tool_name="t",+ content=[TextContent(text="r")], metadata={"x": 1},+ )+ a = _agent(messages=[user, assistant, tool])+ # Mutate originals.+ user.metadata["k"] = "MUT"+ assistant.content[0].arguments["k"].append(99)+ tool.metadata["x"] = 999+ # Internal copies untouched.+ assert a.state.messages[0].metadata["k"] == "v"+ assert a.state.messages[1].content[0].arguments["k"] == [1, 2]+ assert a.state.messages[2].metadata["x"] == 1+ # Mutate agent's copies; originals untouched.+ a.state.messages[0].metadata["k"] = "AGENT"+ assert user.metadata["k"] == "MUT"