Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

perf(toby): keep the system prompt stable across senders for the prompt cache (#3805)

OliverBryanta4709354f38e
diff --git a/src/xagent/core/agent/context/execution.py b/src/xagent/core/agent/context/execution.py
index 945fe471e..c22f4c3bb 100644
--- a/src/xagent/core/agent/context/execution.py
+++ b/src/xagent/core/agent/context/execution.py
@@ -1231,6 +1231,7 @@ class ExecutionContext:
max_tokens: int | None = None,
*,
form_answer_continuation: bool = False,
+ extra_turn_text: str = "",
) -> list[dict[str, Any]]:
# Computed once per call (not once per candidate message, and not
# again inside ``_system_context``): the system-context instruction
@@ -1255,6 +1256,7 @@ class ExecutionContext:
request_in_latest_user_message=(
self._request_is_latest_user_message(visible_messages)
),
+ extra_turn_text=extra_turn_text,
)
system_parts.append(static_context)
turn_anchor = self._turn_context_anchor(visible_messages)
@@ -1528,6 +1530,7 @@ class ExecutionContext:
*,
form_answer_continuation_target: Message | None = None,
request_in_latest_user_message: bool = False,
+ extra_turn_text: str = "",
) -> tuple[str, str]:
"""Return ``(static_text, turn_text)``.
@@ -1769,6 +1772,10 @@ class ExecutionContext:
"and describe in the prompt what a reference would have "
"contributed."
)
+ if extra_turn_text.strip():
+ # Per-call facts of the pattern (the tool names and connector
+ # inventory), which vary by sender and must not enter the system.
+ turn_parts.append(extra_turn_text)
turn_parts.append(self._current_time_context())
static_text = "\n\n".join(part for part in parts if part.strip())
turn_text = "\n\n".join(part.strip() for part in turn_parts if part.strip())
diff --git a/src/xagent/core/agent/deferred_tools.py b/src/xagent/core/agent/deferred_tools.py
index f7585a3ea..684e2d9cf 100644
--- a/src/xagent/core/agent/deferred_tools.py
+++ b/src/xagent/core/agent/deferred_tools.py
@@ -108,7 +108,7 @@ def _normalize(text: str) -> str:
DEFERRED_RULE = (
- "Connector tools in the inventory above are deferred: their schemas are "
+ "Connector tools in the inventory in the turn context are deferred: their schemas are "
f"not sent with every call. To use one, first call {LOAD_TOOLS_NAME} "
"with the names you need, copied exactly as listed (capitalization "
"included); they can be called from the next step. "
diff --git a/src/xagent/core/agent/pattern/react/react.py b/src/xagent/core/agent/pattern/react/react.py
index 98511b0a9..a3970b3fd 100644
--- a/src/xagent/core/agent/pattern/react/react.py
+++ b/src/xagent/core/agent/pattern/react/react.py
@@ -177,7 +177,11 @@ from ...runtime import (
resolve_compact_llm,
resolved_llm_metadata,
)
-from ...tool_access import connector_tool_sources, tool_access_context
+from ...tool_access import (
+ TOOL_ACCESS_RULES,
+ connector_tool_sources,
+ tool_access_fact,
+)
from ...trace import TraceAction, TraceCategory, TraceEventType, TraceScope
from ..base import (
AgentPattern,
@@ -2005,7 +2009,12 @@ class ReActPattern(AgentPattern):
) -> list[dict[str, Any]]:
messages = list(
context.get_messages_for_llm(
- form_answer_continuation=form_answer_continuation
+ form_answer_continuation=form_answer_continuation,
+ extra_turn_text=(
+ self._tool_turn_facts(context, tool_names or [])
+ if has_tools and not force_final_answer
+ else ""
+ ),
)
)
if force_final_answer:
@@ -2097,31 +2106,13 @@ class ReActPattern(AgentPattern):
)
elif has_tools:
active_tool_names = tool_names or []
- # Names and the connector inventory must not change when a tool is
- # loaded mid-run, or the system message (and every cached prefix
- # after it) would change on the call that follows a load. Loaded
- # deferred tools are therefore left out of the list below and the
- # inventory is built from the whole deferred index.
- # The stored-result reader joins the schema mid-run, once a result
- # has been stored, so it is kept out of the names too and the
- # sentence below states its availability instead.
- stable_tool_names = [
- n
- for n in active_tool_names
- if n not in self._deferred_tool_names and n != SPILL_READ_TOOL_NAME
- ]
- inventory_tool_names = [*stable_tool_names, *self._deferred_order]
- available_tools = ", ".join(stable_tool_names) or "(none)"
+ # Everything that varies by sender or by call (the date, the tool
+ # names, the connector inventory) is in the turn context, see
+ # ``_tool_turn_facts``: the system message is the same for every
+ # run of a workspace, so a provider caches it across senders.
can_lookup_output_files = (
WORKSPACE_OUTPUT_FILES_TOOL_NAME in active_tool_names
)
- clock_zone = context.clock_zone()
- current_date = (
- context.created_at.astimezone(clock_zone or timezone.utc)
- .date()
- .isoformat()
- )
- clock_zone_label = clock_zone.key if clock_zone is not None else "UTC"
missing_information_instruction = (
"If a tool needs missing information from the user, including a "
"fact-carrying argument value (one that asserts a real-world "
@@ -2200,13 +2191,13 @@ class ReActPattern(AgentPattern):
"When writing any final user-facing response, including plain "
"assistant text: "
f"{final_deliverable_file_reference_instructions(can_lookup=can_lookup_output_files, include_heading=False)}\n\n"
- f"Turn-start date ({clock_zone_label}): {current_date}. "
- "For recent, latest, current, or time-sensitive requests, use this "
- "date when forming search queries and judging source relevance. If the "
+ "For recent, latest, current, or time-sensitive requests, use the "
+ "turn-start date in the turn context when forming search queries "
+ "and judging source relevance. If the "
"exact current time matters or the turn may have crossed midnight, call "
"the get_current_time tool if it is available. "
"Only call tools that are present in the current tool schema for this "
- "LLM call; "
+ "LLM call, whose names the turn context lists; "
"tool names mentioned in memory, previous tasks, plans, or error "
"messages are unavailable unless they are included in the current "
"schema. If a selected skill is already present in the system "
@@ -2214,19 +2205,10 @@ class ReActPattern(AgentPattern):
"skill documentation tools only when you need an additional "
"referenced file, example, asset, or detail that is not already in "
"the provided skill context."
- f"\n\nAvailable tool names for this LLM call are exactly: {available_tools}. "
- "Never call a tool name that is not in this list."
- f"{AVAILABLE_NAMES_SUFFIX if self._deferred_tool_names else ''}"
- f"{SPILL_READ_AVAILABLE_NOTE if self._spill_reader_present else ''}"
- )
- access_context = tool_access_context(
- self._connector_tool_sources,
- inventory_tool_names
- if self._deferred_tool_names
- else active_tool_names,
- )
- if access_context:
- instruction = f"{instruction}\n\n{access_context}"
+ "\n\nNever call a tool name that is not in the list of available "
+ "tool names for this LLM call in the turn context."
+ )
+ instruction = f"{instruction}\n\n{TOOL_ACCESS_RULES}"
if self._deferred_tool_names:
instruction = f"{instruction}\n\n{DEFERRED_RULE}"
if self._deferred_builtin_categories:
@@ -4445,6 +4427,49 @@ class ReActPattern(AgentPattern):
]
return schemas
+ def _tool_turn_facts(self, context: Any, tool_names: list[str]) -> str:
+ """This call's date, tool names and connector inventory, for the turn
+ context.
+
+ They vary by sender and with what is loaded, and the system message
+ comes before the tool schemas in the request, so any of them in the
+ system message would leave the rest of it and every schema uncached on
+ the first call of each task. Names and inventory must still not change
+ when a tool is loaded mid-run, or the message carrying them (and every
+ cached prefix after it) would change on the call that follows a load:
+ loaded deferred tools and the stored-result reader are therefore left
+ out of the names, and the inventory is built from the whole deferred
+ index.
+ """
+ stable_tool_names = [
+ n
+ for n in tool_names
+ if n not in self._deferred_tool_names and n != SPILL_READ_TOOL_NAME
+ ]
+ inventory_tool_names = (
+ [*stable_tool_names, *self._deferred_order]
+ if self._deferred_tool_names
+ else tool_names
+ )
+ clock_zone = context.clock_zone()
+ current_date = (
+ context.created_at.astimezone(clock_zone or timezone.utc).date().isoformat()
+ )
+ clock_zone_label = clock_zone.key if clock_zone is not None else "UTC"
+ names = ", ".join(stable_tool_names) or "(none)"
+ parts = [
+ f"Turn-start date ({clock_zone_label}): {current_date}.",
+ f"Available tool names for this LLM call are exactly: {names}."
+ f"{AVAILABLE_NAMES_SUFFIX if self._deferred_tool_names else ''}"
+ f"{SPILL_READ_AVAILABLE_NOTE if self._spill_reader_present else ''}",
+ ]
+ access_fact = tool_access_fact(
+ self._connector_tool_sources, inventory_tool_names
+ )
+ if access_fact:
+ parts.append(access_fact)
+ return "\n".join(parts)
+
def _tool_schemas_with_builtin_controls(
self,
tools: list[Any],
@@ -4467,11 +4492,27 @@ class ReActPattern(AgentPattern):
schema.get("function", {}).get("name") == WORKSPACE_OUTPUT_FILES_TOOL_NAME
for schema in external_tools
)
+ # Order is what a provider's prefix cache sees: the tools every sender
+ # has and the control tools first, connector tools, which differ by
+ # sender, last. Two senders of a workspace then share the longest
+ # possible tool prefix. Stable within each group.
+ connector_names = set(connector_tool_sources(tools))
+ shared = [
+ schema
+ for schema in external_tools
+ if schema.get("function", {}).get("name") not in connector_names
+ ]
+ connectors = [
+ schema
+ for schema in external_tools
+ if schema.get("function", {}).get("name") in connector_names
+ ]
return [
- *external_tools,
+ *shared,
*self._builtin_tool_schemas(
can_lookup_output_files=can_lookup_output_files
),
+ *connectors,
]
def _spilled_records(self, context: Any) -> Any:
diff --git a/src/xagent/core/agent/tool_access.py b/src/xagent/core/agent/tool_access.py
index 51ef416f9..51674dd8c 100644
--- a/src/xagent/core/agent/tool_access.py
+++ b/src/xagent/core/agent/tool_access.py
@@ -19,8 +19,31 @@ def connector_tool_sources(tools: Sequence[Any]) -> dict[str, str]:
return sources
-def tool_access_context(sources: Mapping[str, str], tool_names: Sequence[str]) -> str:
- """Render current exposure, without claiming successful authorization."""
+# Static: the rules for reading the connector-tool inventory. The inventory
+# itself varies per sender and goes in the turn context (``tool_access_fact``),
+# so this text never changes between runs of a workspace.
+TOOL_ACCESS_RULES = (
+ "Connector tool access: the turn context may list this call's "
+ "connector-tool inventory (source -> exposed tool names). It lists only "
+ "this call's tools, not all connections in the user's account, and does "
+ "not prove that authorization or a request will succeed. An empty "
+ "inventory means no connector-backed tool is exposed in this call. "
+ "api_call, when it is a tool of this call, is a generic HTTP client, not "
+ "a configured connection to the user's systems; it has no preconfigured "
+ "endpoint or stored service credentials. Use supplied files or data, a "
+ "relevant exposed connector, or an endpoint established by the user, "
+ "configuration, documentation or tool results. A system's name or "
+ "business purpose is not an endpoint. When the required source is "
+ "missing, do not probe invented URLs. When ask_user_question is a tool of "
+ "this call, use it to request the missing connection or uploaded/pasted "
+ "data; otherwise report the missing source without claiming completion. "
+ "Do not claim a connection is absent from the account just because this "
+ "task cannot access it."
+)
+
+
+def tool_access_fact(sources: Mapping[str, str], tool_names: Sequence[str]) -> str:
+ """Render current exposure as a fact, without claiming authorization."""
exposed: dict[str, list[str]] = {}
for name in tool_names:
if name in sources:
@@ -30,28 +53,5 @@ def tool_access_context(sources: Mapping[str, str], tool_names: Sequence[str]) -
return ""
inventory = json.dumps(exposed, ensure_ascii=True, sort_keys=True)
return (
- "Current connector-tool inventory (source -> exposed tool names): "
- f"{inventory}. "
- "This lists only this call's tools, not all connections in the user's "
- "account, and does not prove that authorization or a request will succeed. "
- "An empty inventory means no connector-backed tool is exposed in this call. "
- + (
- "api_call is a generic HTTP client, not a configured connection to "
- "the user's systems; it has no preconfigured endpoint or stored "
- "service credentials. "
- if generic_http
- else ""
- )
- + "Use supplied files or data, a relevant exposed connector, or an "
- "endpoint established by the user, configuration, documentation or tool "
- "results. A system's name or business purpose is not an endpoint. When "
- "the required source is missing, do not probe invented URLs. "
- + (
- "Use ask_user_question to request the missing connection or "
- "uploaded/pasted data. "
- if "ask_user_question" in tool_names
- else "Report the missing source without claiming completion. "
- )
- + "Do not claim a connection is absent from "
- "the account just because this task cannot access it."
+ f"Current connector-tool inventory (source -> exposed tool names): {inventory}."
)
diff --git a/tests/core/agent/test_execution_adapter.py b/tests/core/agent/test_execution_adapter.py
index cb79bc9c1..3de8910e5 100644
--- a/tests/core/agent/test_execution_adapter.py
+++ b/tests/core/agent/test_execution_adapter.py
@@ -664,7 +664,7 @@ async def test_request_context_timezone_reaches_both_prompt_clocks() -> None:
f"(Australia/Melbourne, UTC+" in system_prompt
)
assert (
- f"Turn-start date (Australia/Melbourne): {expected_local.date().isoformat()}. "
+ f"Turn-start date (Australia/Melbourne): {expected_local.date().isoformat()}."
in system_prompt
)
diff --git a/tests/core/agent/test_react.py b/tests/core/agent/test_react.py
index 95798f5a9..522bff8b9 100644
--- a/tests/core/agent/test_react.py
+++ b/tests/core/agent/test_react.py
@@ -1390,8 +1390,11 @@ async def test_react_pattern_runs_tool_call_then_final_answer() -> None:
assert llm.calls[0]["tools"][0]["function"]["name"] == "calculator"
system_prompt = llm.calls[0]["messages"][0]["content"]
assert "latest user message" in system_prompt
- assert re.search(r"Turn-start date \(UTC\): \d{4}-\d{2}-\d{2}", system_prompt)
- assert "use this date when forming search queries" in system_prompt
+ # The date is a per-turn fact, so it is in the turn context, not the system.
+ turn_prompt = llm.calls[0]["messages"][-1]["content"]
+ assert re.search(r"Turn-start date \(UTC\): \d{4}-\d{2}-\d{2}", turn_prompt)
+ assert "Turn-start date (" not in system_prompt
+ assert "use the turn-start date in the turn context" in system_prompt
assert "not supported by the conversation" in system_prompt
assert "available context is insufficient" in system_prompt
assert "quantitative data" in system_prompt
@@ -12591,7 +12594,7 @@ async def test_blank_streaming_arguments_flow_from_adapter_into_react(mocker) ->
assert tool.calls == [{}]
-def _react_clock_prompt(timezone_name: str | None) -> str:
+def _react_clock_messages(timezone_name: str | None) -> tuple[str, str]:
context = ExecutionContext(
created_at=datetime(2026, 8, 24, 22, 3, 37, tzinfo=timezone.utc)
)
@@ -12600,43 +12603,41 @@ def _react_clock_prompt(timezone_name: str | None) -> str:
context.add_user_message("how many shifts do we have on tomorrow?")
messages = ReActPattern()._messages_for_llm(context, has_tools=True)
- return messages[0]["content"]
+ return messages[0]["content"], messages[-1]["content"]
-# Covers the whole fallback instruction through the get_current_time pointer, so
-# reverting any part of it fails rather than only a change to the prefix.
-UTC_DATE_INSTRUCTION = (
- "Turn-start date (UTC): 2026-08-24. "
- "For recent, latest, current, or time-sensitive requests, use this "
- "date when forming search queries and judging source relevance. If the "
+# The date is a per-turn fact in the turn context; the rule that tells the
+# model what to do with it is static and stays in the system message.
+DATE_RULE = (
+ "For recent, latest, current, or time-sensitive requests, use the "
+ "turn-start date in the turn context when forming search queries "
+ "and judging source relevance. If the "
"exact current time matters or the turn may have crossed midnight, call "
"the get_current_time tool if it is available."
)
-def _date_instruction(prompt: str) -> str:
- start = prompt.index("Turn-start date (")
- end = prompt.index("if it is available.", start) + len("if it is available.")
- return prompt[start:end]
-
-
def test_tool_call_date_line_keeps_utc_wording_without_a_timezone() -> None:
- assert _date_instruction(_react_clock_prompt(None)) == UTC_DATE_INSTRUCTION
+ system, turn = _react_clock_messages(None)
+
+ assert DATE_RULE in system
+ assert "Turn-start date (UTC): 2026-08-24." in turn
+ assert "Turn-start date (" not in system
def test_tool_call_date_line_uses_the_caller_timezone() -> None:
- prompt = _react_clock_prompt("Australia/Melbourne")
+ system, turn = _react_clock_messages("Australia/Melbourne")
- assert _date_instruction(prompt) == UTC_DATE_INSTRUCTION.replace(
- "Turn-start date (UTC): 2026-08-24. ",
- "Turn-start date (Australia/Melbourne): 2026-08-25. ",
- )
+ assert "Turn-start date (Australia/Melbourne): 2026-08-25." in turn
# The UTC date is what produced the wrong "tomorrow" in production.
- assert "Turn-start date (UTC)" not in prompt
+ assert "Turn-start date (UTC)" not in turn
+ assert "Turn-start date (" not in system
def test_tool_call_date_line_degrades_to_utc_for_an_unusable_timezone() -> None:
- assert _date_instruction(_react_clock_prompt("Not/AZone")) == UTC_DATE_INSTRUCTION
+ _system, turn = _react_clock_messages("Not/AZone")
+
+ assert "Turn-start date (UTC): 2026-08-24." in turn
# ---------------------------------------------------------------------------
diff --git a/tests/core/agent/test_react_deferred_builtin_schemas.py b/tests/core/agent/test_react_deferred_builtin_schemas.py
index 8d879e189..7d121af28 100644
--- a/tests/core/agent/test_react_deferred_builtin_schemas.py
+++ b/tests/core/agent/test_react_deferred_builtin_schemas.py
@@ -74,6 +74,11 @@ def _system(call: dict[str, Any]) -> str:
)
+def _prompt(call: dict[str, Any]) -> str:
+ # System message plus the turn context, which carries the tool catalog.
+ return "\n".join(str(m.get("content")) for m in call["messages"])
+
+
def _tools() -> list[Any]:
return [
BuiltinTool("read_file", ToolCategory.FILE),
@@ -127,10 +132,12 @@ async def test_deferred_categories_leave_the_schemas_but_stay_in_the_catalog() -
names = _names(llm.calls[0])
assert not DEFERRED & set(names)
assert {"read_file", "web_search", LOAD_TOOLS_NAME} <= set(names)
- system = _system(llm.calls[0])
+ system = _prompt(llm.calls[0])
for name in DEFERRED:
assert name in system
assert "built-in image" in system
+ # The catalog is per-run, so it is not in the system message.
+ assert "built-in image" not in _system(llm.calls[0])
@pytest.mark.asyncio
@@ -245,7 +252,7 @@ async def test_excluded_categories_are_not_in_the_catalog() -> None:
llm = ScriptedLLM([{"content": "hi"}])
await _pattern().run(context=_ctx(), tools=tools, llm=llm)
- system = _system(llm.calls[0])
+ system = _prompt(llm.calls[0])
assert "generate_video" not in system and "built-in video" not in system
assert "generate_image" in system
@@ -273,7 +280,7 @@ async def test_connectors_and_builtins_defer_together() -> None:
names = set(_names(llm.calls[0]))
assert not ({"hubspot_search"} | DEFERRED) & names
assert {"read_file", LOAD_TOOLS_NAME} <= names
- system = _system(llm.calls[0])
+ system = _prompt(llm.calls[0])
assert "hubspot_search" in system and "built-in image" in system
diff --git a/tests/core/agent/test_react_deferred_connector_schemas.py b/tests/core/agent/test_react_deferred_connector_schemas.py
index d1baa318e..e0f077c14 100644
--- a/tests/core/agent/test_react_deferred_connector_schemas.py
+++ b/tests/core/agent/test_react_deferred_connector_schemas.py
@@ -89,6 +89,11 @@ def _system_text(call: dict[str, Any]) -> str:
)
+def _prompt_text(call: dict[str, Any]) -> str:
+ # System message plus the turn context, which carries the tool inventory.
+ return "\n".join(str(m.get("content")) for m in call["messages"])
+
+
def _tools() -> list[Any]:
return [
PlainTool("get_current_time"),
@@ -134,8 +139,10 @@ async def test_on_withholds_connector_schemas_and_lists_an_index() -> None:
"hubspot_create_note",
"notion_search",
} & set(names)
- system = _system_text(llm.calls[0])
+ system = _prompt_text(llm.calls[0])
assert "are deferred" in system and system.count("notion_search") == 1
+ # The inventory varies per sender, so it is not in the system message.
+ assert "notion_search" not in _system_text(llm.calls[0])
for name in ("hubspot_search_contacts", "hubspot_create_note", "notion_search"):
assert name in system
@@ -294,7 +301,7 @@ async def test_index_lists_every_connector_tool_including_failing_ones() -> None
llm = ScriptedLLM([{"content": "hi"}])
await _pattern().run(context=_ctx(), tools=tools, llm=llm)
- assert "xero_list_invoices" in _system_text(llm.calls[0])
+ assert "xero_list_invoices" in _prompt_text(llm.calls[0])
assert "xero_list_invoices" not in _names(llm.calls[0])
@@ -816,7 +823,7 @@ async def test_the_prompt_allows_preloaded_connector_tools() -> None:
llm = ScriptedLLM([{"content": "hi"}])
await _pattern().run(context=_ctx("look in HubSpot"), tools=_tools(), llm=llm)
- assert "whether preloaded or loaded" in _system_text(llm.calls[0])
+ assert "whether preloaded or loaded" in _prompt_text(llm.calls[0])
@pytest.mark.asyncio
diff --git a/tests/core/agent/test_react_prefix_stability.py b/tests/core/agent/test_react_prefix_stability.py
index 1b022bfaf..95f8fc020 100644
--- a/tests/core/agent/test_react_prefix_stability.py
+++ b/tests/core/agent/test_react_prefix_stability.py
@@ -24,7 +24,10 @@ from tests.core.agent.test_react_deferred_builtin_schemas import (
BuiltinTool,
)
from tests.core.agent.test_react_deferred_connector_schemas import ConnectorTool
-from xagent.core.agent.context.execution import ExecutionContext
+from xagent.core.agent.context.execution import (
+ CLOCK_TIMEZONE_METADATA_KEY,
+ ExecutionContext,
+)
from xagent.core.agent.deferred_tools import LOAD_TOOLS_NAME
from xagent.core.agent.pattern.react import ReActPattern
from xagent.core.tools.adapters.vibe.base import ToolCategory
@@ -103,7 +106,9 @@ async def test_spill_reader_joining_mid_run_leaves_the_system_message_alone(
assert _names(llm.calls[1])[-1] == SPILL_READ_TOOL_NAME
_assert_prefix_stable(llm.calls[0], llm.calls[1])
# The reader is still announced, by a sentence that never changes.
- assert SPILL_READ_TOOL_NAME in _system(llm.calls[0])
+ turn = llm.calls[0]["messages"][1]["content"]
+ assert SPILL_READ_TOOL_NAME in turn
+ assert turn == llm.calls[1]["messages"][1]["content"]
@pytest.mark.asyncio
@@ -139,3 +144,61 @@ async def test_loads_then_a_spill_never_touch_the_system_message(
# Each load only appends. (The reader is placed ahead of loaded tools, so
# the spill call is checked on the system message alone.)
assert _tools(llm.calls[2])[: len(_tools(llm.calls[1]))] == _tools(llm.calls[1])
+
+
+class _FakeSender:
+ """A run of one workspace for one sender: same built-ins, own connectors."""
+
+ def __init__(self, *connectors: tuple[str, str]) -> None:
+ self.tools: list[Any] = [
+ FakeTool(),
+ BuiltinTool("web_search", ToolCategory.WEB_TOOLS),
+ *(ConnectorTool(name, source) for name, source in connectors),
+ ]
+
+
+async def _first_call(
+ sender: _FakeSender, *, zone: str, **pattern_kwargs: Any
+) -> dict[str, Any]:
+ context = ExecutionContext()
+ context.metadata[CLOCK_TIMEZONE_METADATA_KEY] = zone
+ context.add_user_message("What is on my calendar?")
+ llm = FakeLLM(responses=[_final()])
+ await ReActPattern(max_iterations=2, **pattern_kwargs).run(
+ context=context, tools=sender.tools, llm=llm
+ )
+ return llm.calls[0]
+
+
[email protected]("defer", [True, False])
+async def test_senders_of_a_workspace_share_system_and_a_tool_prefix(
+ defer: bool,
+) -> None:
+ a = _FakeSender(("hubspot_search", "hubspot"), ("notion_search", "notion"))
+ b = _FakeSender(("slack_post", "slack"))
+ call_a = await _first_call(
+ a, zone="Australia/Melbourne", defer_connector_schemas=defer
+ )
+ call_b = await _first_call(
+ b, zone="America/New_York", defer_connector_schemas=defer
+ )
+
+ # Different senders, different zones and connectors: same system bytes.
+ assert _system(call_a) == _system(call_b)
+ names_a = _names(call_a)
+ connector_names = {"hubspot_search", "notion_search", "slack_post"}
+ lead = next(
+ (i for i, n in enumerate(names_a) if n in connector_names), len(names_a)
+ )
+ # Everything before the first sender-specific tool is shared, and holds
+ # the shared tools and the control tools.
+ assert lead >= 3 and "final_answer" in names_a[:lead]
+ assert _tools(call_a)[:lead] == _tools(call_b)[:lead]
+ if not defer:
+ assert names_a[lead] in connector_names
+ assert not connector_names & set(names_a[:lead])
+ # The per-sender facts are still sent, in the turn context.
+ turn_a = call_a["messages"][-1]["content"]
+ assert "Turn-start date (Australia/Melbourne)" in turn_a
+ assert "hubspot_search" in turn_a
diff --git a/tests/core/agent/test_stable_prompt_prefix.py b/tests/core/agent/test_stable_prompt_prefix.py
index 90066f918..1c26b902b 100644
--- a/tests/core/agent/test_stable_prompt_prefix.py
+++ b/tests/core/agent/test_stable_prompt_prefix.py
@@ -355,11 +355,14 @@ async def test_a_host_without_turn_notes_gets_only_the_clock() -> None:
await adapter.execute(task="Say hi", task_id="prefix-plain", context={})
user = _non_system(llm.calls[0])
- request, header, clock, end = user.split("\n\n")
+ request, header, facts, clock, end = user.split("\n\n")
assert request == "Say hi"
- # Exactly the header, the clock paragraph and the end marker: no host,
- # evidence or memory text leaks in when the host passes none.
+ # Exactly the header, the engine's own per-call facts (date and tool
+ # names), the clock paragraph and the end marker: no host, evidence or
+ # memory text leaks in when the host passes none.
assert header == TURN_CONTEXT_HEADER
+ assert facts.startswith("Turn-start date (UTC): ")
+ assert "Available tool names for this LLM call are exactly:" in facts
assert clock.startswith("Turn started at: ")
assert clock.endswith("instead of computing from this value.")
assert end == TURN_CONTEXT_END
diff --git a/tests/core/agent/test_tool_access.py b/tests/core/agent/test_tool_access.py
index 9a70bba01..2b698d4a9 100644
--- a/tests/core/agent/test_tool_access.py
+++ b/tests/core/agent/test_tool_access.py
@@ -6,7 +6,11 @@ from typing import Any
import pytest
from xagent.core.agent import ExecutionContext, ReActPattern
-from xagent.core.agent.tool_access import connector_tool_sources, tool_access_context
+from xagent.core.agent.tool_access import (
+ TOOL_ACCESS_RULES,
+ connector_tool_sources,
+ tool_access_fact,
+)
from xagent.core.tools.adapters.vibe.api_tool import APITool
from xagent.core.tools.adapters.vibe.api_tool_adapter import CustomApiTool
from xagent.core.tools.adapters.vibe.mcp_adapter import UnavailableMCPTool
@@ -43,7 +47,7 @@ def test_sources_use_metadata_not_names_or_configuration() -> None:
"api_Inventory_call": "inventory",
"actual_schema_name": "selected_server",
}
- text = tool_access_context(sources, ["api_Inventory_call", "api_call"])
+ text = tool_access_fact(sources, ["api_Inventory_call", "api_call"])
assert '"inventory": ["api_Inventory_call"]' in text
assert "selected_server" not in text
assert "private.invalid" not in text
@@ -51,34 +55,37 @@ def test_sources_use_metadata_not_names_or_configuration() -> None:
assert "secret" not in text
-def test_empty_inventory_is_not_account_wide_absence() -> None:
- text = tool_access_context({}, ["api_call", "ask_user_question"])
+def test_empty_inventory_is_a_fact_and_the_rules_are_static() -> None:
+ text = tool_access_fact({}, ["api_call", "ask_user_question"])
assert "{}" in text
- assert "not all connections in the user's account" in text
- assert "no preconfigured endpoint" in text
- assert "Use ask_user_question" in text
- assert "uploaded/pasted data" in text
- assert "documentation or tool results" in text
-
-
-def test_no_interaction_prompt_does_not_request_a_question() -> None:
- text = tool_access_context({}, ["api_call", "final_answer"])
- assert "ask_user_question" not in text
- assert "Report the missing source" in text
+ # The rules never depend on the run, so they can sit in the system message.
+ assert "not all connections in the user's account" in TOOL_ACCESS_RULES
+ assert "no preconfigured endpoint" in TOOL_ACCESS_RULES
+ assert "use it to request the missing connection" in TOOL_ACCESS_RULES
+ assert "uploaded/pasted data" in TOOL_ACCESS_RULES
+ assert "documentation or tool results" in TOOL_ACCESS_RULES
+ assert "otherwise report the missing source" in TOOL_ACCESS_RULES
def test_unavailable_connector_is_not_described_as_authorized() -> None:
tool = UnavailableMCPTool(server_name="CRM", server_id=1)
- text = tool_access_context(connector_tool_sources([tool]), [tool.name])
+ text = tool_access_fact(connector_tool_sources([tool]), [tool.name])
assert '"crm"' in text
- assert "does not prove that authorization or a request will succeed" in text
+ assert "does not prove that authorization or a request will succeed" in (
+ TOOL_ACCESS_RULES
+ )
@pytest.mark.parametrize("names", [[], ["calculator"], ["final_answer"]])
def test_unrelated_or_narrowed_calls_do_not_receive_connector_guidance(
names: list[str],
) -> None:
- assert tool_access_context({"hidden_tool": "private_connection"}, names) == ""
+ assert tool_access_fact({"hidden_tool": "private_connection"}, names) == ""
+
+
+def _prompt_text(call: dict[str, Any]) -> str:
+ # The inventory is a per-call fact in the turn context, not in the system.
+ return "\n".join(str(m.get("content")) for m in call["messages"])
class RecordingLLM:
@@ -126,7 +133,7 @@ async def test_react_refreshes_inventory_after_waiting(
if remove_connector
else "inventory (source -> exposed tool names): {}"
)
- assert old_inventory in llm.calls[0]["messages"][0]["content"]
+ assert old_inventory in _prompt_text(llm.calls[0])
if restore_checkpoint:
resumed = ReActPattern(max_iterations=3)
resumed.load_state(pattern.get_state())
@@ -137,13 +144,12 @@ async def test_react_refreshes_inventory_after_waiting(
final_llm = RecordingLLM([{"content": "Connection tools are now exposed."}])
result = await pattern.run(context=context, tools=after, llm=final_llm)
assert result["success"] is True
- prompt = final_llm.calls[0]["messages"][0]["content"]
+ prompt = _prompt_text(final_llm.calls[0])
assert old_inventory not in prompt
if remove_connector:
assert "inventory (source -> exposed tool names): {}" in prompt
else:
assert '"crm": ["api_CRM_call"]' in prompt
- assert "api_call is a generic HTTP client" not in prompt
def test_forced_answer_does_not_advertise_connector_tools() -> None: