Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot): keep a hire's onboarding card on the expert's own role (#14547)

Ubbe194b6d78dd50
diff --git a/autogpt_platform/backend/backend/copilot/expert_context.py b/autogpt_platform/backend/backend/copilot/expert_context.py
index bfae9a9f06..14094e0ab7 100644
--- a/autogpt_platform/backend/backend/copilot/expert_context.py
+++ b/autogpt_platform/backend/backend/copilot/expert_context.py
@@ -116,8 +116,12 @@ def render_expert_identity_suffix(expert: Expert) -> str:
f"Your first turn after being hired arrives as a hidden instruction "
f"that names `expert_onboarding`. On that turn call "
f"`expert_onboarding` exactly once and nothing else. Do not use "
- f"`ask_question` for it; that tool is for later turns. Once the "
- f"card's answers come back, continue as normal.\n"
+ f"`ask_question` for it; that tool is for later turns. Every "
+ f"question and option on that card must be about your own role as "
+ f"{escape_prompt_xml_tags(expert.role)} and the workflows installed "
+ f"on you: never about a teammate's area or work outside your role, "
+ f"whatever other context suggests. Once the card's answers come "
+ f"back, continue as normal.\n"
f"</first_turn>\n"
f"The base instructions above describe Otto, the platform's default "
f"assistant. All platform capabilities and tools remain "
@@ -172,9 +176,19 @@ def fence_voice_preferences(voice: str) -> str:
)
-async def build_expert_context(user_id: str | None, expert_id: str | None) -> str:
+async def build_expert_context(
+ user_id: str | None,
+ expert_id: str | None,
+ *,
+ include_teammates: bool = True,
+) -> str:
"""Build the expert/team context prefix for the first user message.
+ ``include_teammates=False`` drops the roster from an expert session's
+ prefix. The kickoff turn uses it: the card must come from the expert's
+ own role, and a teammate's workflows are the easiest thing for the model
+ to borrow questions from. Plain sessions always get their roster.
+
Returns ``""`` when there is nothing to inject or any lookup fails.
"""
if not user_id:
@@ -189,7 +203,10 @@ async def build_expert_context(user_id: str | None, expert_id: str | None) -> st
)
if expert_id:
return await _expert_session_context(
- user_id, expert_id, delegation_enabled=delegation_enabled
+ user_id,
+ expert_id,
+ delegation_enabled=delegation_enabled,
+ include_teammates=include_teammates,
)
return await _team_context(user_id, delegation_enabled=delegation_enabled)
except Exception as e:
@@ -198,9 +215,15 @@ async def build_expert_context(user_id: str | None, expert_id: str | None) -> st
async def _expert_session_context(
- user_id: str, expert_id: str, *, delegation_enabled: bool
+ user_id: str,
+ expert_id: str,
+ *,
+ delegation_enabled: bool,
+ include_teammates: bool,
) -> str:
async def _load_teammates() -> str:
+ if not include_teammates:
+ return ""
# The roster is an optional extra here; a failed lookup must not cost
# the expert its own workflow block, which is the load-bearing half.
try:
diff --git a/autogpt_platform/backend/backend/copilot/expert_context_test.py b/autogpt_platform/backend/backend/copilot/expert_context_test.py
index 980046cbb1..bef15cc025 100644
--- a/autogpt_platform/backend/backend/copilot/expert_context_test.py
+++ b/autogpt_platform/backend/backend/copilot/expert_context_test.py
@@ -28,6 +28,7 @@ from backend.copilot.expert_context import (
ExpertSessionUnavailableError,
build_expert_identity_suffix,
)
+from backend.copilot.rate_limit import SubscriptionTier
from backend.util.feature_flag import Flag
_EC = "backend.copilot.expert_context"
@@ -457,6 +458,24 @@ class TestBuildExpertContextExpertSession:
assert "Maria" not in result.split("<team_context>")[1]
assert "delegate_to_expert" in result
+ @pytest.mark.asyncio
+ async def test_teammates_can_be_left_out_without_a_roster_lookup(self):
+ from backend.copilot.expert_context import build_expert_context
+
+ mock_db = MagicMock()
+ mock_db.get_expert = AsyncMock(return_value=_expert())
+ mock_db.list_experts = AsyncMock(
+ return_value=[_expert(), _expert(expert_id="exp-2", name="Otto")]
+ )
+ with patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)):
+ result = await build_expert_context(
+ "user-1", "exp-1", include_teammates=False
+ )
+
+ assert "<expert_workflows>" in result
+ assert "<team_context>" not in result
+ mock_db.list_experts.assert_not_called()
+
@pytest.mark.asyncio
async def test_solo_expert_gets_no_team_block(self):
from backend.copilot.expert_context import build_expert_context
@@ -806,6 +825,96 @@ class TestInjectUserContextExpertWiring:
assert "<expert_workflows>" in result
assert result.endswith("hello")
+ @pytest.mark.asyncio
+ async def test_kickoff_turn_keeps_only_the_experts_own_workflows(self):
+ """The card must come from the expert's own role: the user's pain
+ points and a teammate's workflows are exactly what the model would
+ otherwise borrow its questions from."""
+ from backend.copilot.expert_kickoff import expert_kickoff_metadata
+ from backend.copilot.model import ChatMessage
+ from backend.copilot.service import inject_user_context
+ from backend.data.understanding import BusinessUnderstanding
+
+ understanding = BusinessUnderstanding(
+ id="u-1",
+ user_id="user-1",
+ created_at=datetime.now(timezone.utc),
+ updated_at=datetime.now(timezone.utc),
+ pain_points=["Finding leads"],
+ )
+ kickoff = ChatMessage(
+ role="user",
+ content="You were just hired.",
+ metadata=expert_kickoff_metadata("exp-1"),
+ sequence=None,
+ )
+ mock_db = MagicMock()
+ mock_db.get_expert = AsyncMock(return_value=_expert())
+ mock_db.list_experts = AsyncMock(
+ return_value=[_expert(), _expert(expert_id="exp-2", name="Max")]
+ )
+ with patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)):
+ result = await inject_user_context(
+ understanding,
+ "You were just hired.",
+ "sess-1",
+ [kickoff],
+ user_id="user-1",
+ expert_id="exp-1",
+ )
+
+ assert result is not None
+ assert "<expert_workflows>" in result
+ assert "<user_context>" not in result
+ assert "Finding leads" not in result
+ assert "<team_context>" not in result
+ assert "Max" not in result
+ assert result.endswith("You were just hired.")
+
+ @pytest.mark.asyncio
+ async def test_typed_first_turn_in_expert_session_keeps_user_and_team_context(
+ self,
+ ):
+ from backend.copilot.model import ChatMessage
+ from backend.copilot.service import inject_user_context
+ from backend.data.understanding import BusinessUnderstanding
+
+ understanding = BusinessUnderstanding(
+ id="u-1",
+ user_id="user-1",
+ created_at=datetime.now(timezone.utc),
+ updated_at=datetime.now(timezone.utc),
+ pain_points=["Finding leads"],
+ )
+ msg = ChatMessage(role="user", content="hello", sequence=None)
+ mock_db = MagicMock()
+ mock_db.get_expert = AsyncMock(return_value=_expert())
+ mock_db.list_experts = AsyncMock(
+ return_value=[_expert(), _expert(expert_id="exp-2", name="Max")]
+ )
+ with (
+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),
+ patch(
+ "backend.copilot.rate_limit.get_user_tier",
+ new=AsyncMock(return_value=SubscriptionTier.NO_TIER),
+ ),
+ ):
+ result = await inject_user_context(
+ understanding,
+ "hello",
+ "sess-1",
+ [msg],
+ user_id="user-1",
+ expert_id="exp-1",
+ )
+
+ assert result is not None
+ assert "<expert_workflows>" in result
+ assert "<team_context>" in result
+ assert "Max" in result
+ assert "<user_context>" in result
+ assert "Finding leads" in result
+
@pytest.mark.asyncio
async def test_no_expert_block_without_expert_or_team(self):
from backend.copilot.model import ChatMessage
diff --git a/autogpt_platform/backend/backend/copilot/expert_kickoff.py b/autogpt_platform/backend/backend/copilot/expert_kickoff.py
index 5666f22f87..4fc0e0676c 100644
--- a/autogpt_platform/backend/backend/copilot/expert_kickoff.py
+++ b/autogpt_platform/backend/backend/copilot/expert_kickoff.py
@@ -5,7 +5,7 @@ from collections.abc import Mapping
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
- from backend.copilot.model import ChatSession
+ from backend.copilot.model import ChatMessage, ChatSession
EXPERT_KICKOFF_KIND = "expert_kickoff"
@@ -59,18 +59,26 @@ def is_hidden_chat_message(metadata: Mapping[str, Any] | None) -> bool:
return metadata is not None and metadata.get("hidden") is True
-def is_expert_kickoff_turn(session: "ChatSession") -> bool:
- """Whether the turn being served answers the hire's kickoff message.
+def is_expert_kickoff_message(message: "ChatMessage") -> bool:
+ """Whether a persisted user row is the server-written kickoff message.
``expert_kickoff_metadata`` is the only writer of this ``kind`` and the
chat route never copies request fields into message metadata, so a true
- here means the server wrote the last user message itself. Nobody has
- asked for anything yet, which is what makes it safe for callers to
- narrow the turn down to the onboarding card.
+ here means the server wrote the row itself.
+ """
+ if message.role != "user":
+ return False
+ metadata = message.metadata
+ return metadata is not None and metadata.get("kind") == EXPERT_KICKOFF_KIND
+
+
+def is_expert_kickoff_turn(session: "ChatSession") -> bool:
+ """Whether the turn being served answers the hire's kickoff message.
+
+ Nobody has asked for anything yet on that turn, which is what makes it
+ safe for callers to narrow it down to the onboarding card.
"""
for message in reversed(session.messages):
- if message.role != "user":
- continue
- metadata = message.metadata
- return metadata is not None and metadata.get("kind") == EXPERT_KICKOFF_KIND
+ if message.role == "user":
+ return is_expert_kickoff_message(message)
return False
diff --git a/autogpt_platform/backend/backend/copilot/expert_kickoff_test.py b/autogpt_platform/backend/backend/copilot/expert_kickoff_test.py
index ba9fb08814..9b71cc2123 100644
--- a/autogpt_platform/backend/backend/copilot/expert_kickoff_test.py
+++ b/autogpt_platform/backend/backend/copilot/expert_kickoff_test.py
@@ -1,6 +1,7 @@
from backend.copilot.expert_kickoff import (
expert_kickoff_message_id,
expert_kickoff_metadata,
+ is_expert_kickoff_message,
is_expert_kickoff_turn,
is_hidden_chat_message,
scoped_client_message_id,
@@ -67,6 +68,18 @@ def test_metadata_marks_kickoff_hidden_without_hiding_other_messages() -> None:
assert not is_hidden_chat_message({"hidden": False})
+def test_only_the_server_written_user_row_is_a_kickoff_message() -> None:
+ assert is_expert_kickoff_message(_kickoff_message())
+ assert not is_expert_kickoff_message(ChatMessage(role="user", content="hi"))
+ assert not is_expert_kickoff_message(
+ ChatMessage(
+ role="assistant",
+ content="You were just hired.",
+ metadata=expert_kickoff_metadata(EXPERT_ID),
+ )
+ )
+
+
def test_kickoff_turn_is_the_one_answering_the_server_written_message() -> None:
assert is_expert_kickoff_turn(_expert_session(_kickoff_message()))
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.py
index fbc7590b10..c2f93f985e 100644
--- a/autogpt_platform/backend/backend/copilot/service.py
+++ b/autogpt_platform/backend/backend/copilot/service.py
@@ -32,6 +32,7 @@ from backend.util.settings import AppEnvironment, Settings
from .anthropic_rate_card import compute_anthropic_cost_usd
from .config import ChatConfig, CopilotLLMModel
from .expert_context import build_expert_context, escape_prompt_xml_tags
+from .expert_kickoff import is_expert_kickoff_message
from .model import (
ChatMessage,
ChatSessionInfo,
@@ -606,6 +607,14 @@ async def inject_user_context(
content to the DB so resumed sessions and page reloads retain
personalisation.
+ A hire's kickoff turn (the server-written message that opens the
+ onboarding card) gets neither ``<user_context>`` nor the teammate roster.
+ The card must come from the expert's own role, and both blocks are
+ exactly the kind of context the model otherwise borrows its questions
+ from — a "Finding leads" pain point or a sales teammate's workflows put
+ lead-gen options on a developer's card. What the expert needs to know
+ about the user, the card asks for itself.
+
Untrusted input — both the user-supplied ``message`` and the user-owned
fields inside ``understanding`` — is stripped/escaped before being placed
inside the trusted ``<user_context>`` block. This prevents a user from
@@ -667,8 +676,9 @@ async def inject_user_context(
# tests) without prior sanitization — and because the operation is
# idempotent (a second pass over already-clean text is a no-op).
sanitized_message = sanitize_user_supplied_context(message)
+ kickoff_turn = _is_expert_kickoff_turn(session_messages)
- if understanding is None:
+ if understanding is None or kickoff_turn:
# No trusted context to inject — but we still need to persist the
# sanitised message so a later resume / page-reload replay doesn't
# feed the attacker tags back into the LLM.
@@ -732,7 +742,9 @@ async def inject_user_context(
# like the other trusted blocks; degrades to "" on any lookup failure so
# the turn proceeds as plain Otto. Per-session dynamic, so it sits
# below the cached <available_skills> prefix.
- expert_ctx = await build_expert_context(user_id, expert_id)
+ expert_ctx = await build_expert_context(
+ user_id, expert_id, include_teammates=not kickoff_turn
+ )
if expert_ctx:
final_message = expert_ctx + final_message
# Prepend Graphiti warm context as a <memory_context> block AFTER
@@ -781,6 +793,14 @@ async def inject_user_context(
return None
+def _is_expert_kickoff_turn(session_messages: list[ChatMessage]) -> bool:
+ """Whether the current turn's user message is the hire's kickoff."""
+ for session_msg in reversed(session_messages):
+ if session_msg.role == "user":
+ return is_expert_kickoff_message(session_msg)
+ return False
+
+
def _normalize_title_model_for_aux() -> str:
"""Return the title model in the form the aux client's transport expects.
diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_onboarding.py b/autogpt_platform/backend/backend/copilot/tools/expert_onboarding.py
index e43c2e58b3..dcf5e3fc1f 100644
--- a/autogpt_platform/backend/backend/copilot/tools/expert_onboarding.py
+++ b/autogpt_platform/backend/backend/copilot/tools/expert_onboarding.py
@@ -99,9 +99,10 @@ class ExpertOnboardingTool(BaseTool):
"required": ["question", "options"],
},
"description": (
- "3-5 questions, shown one per step. Order them from "
- "what you most need to know to what is merely nice "
- "to know."
+ "3-5 questions, shown one per step, all about your "
+ "own role and the workflows installed on you. Order "
+ "them from what you most need to know to what is "
+ "merely nice to know."
),
},
},
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expertKickoff.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expertKickoff.test.ts
index 080bf52617..840d6b0f9f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expertKickoff.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expertKickoff.test.ts
@@ -64,6 +64,9 @@ describe("buildKickoffMessage", () => {
expect(message.text).toContain("expert_onboarding");
// The hire's first turn is an intake, not a first job.
expect(message.text).toContain("Do not run a workflow");
+ // The card is about the expert's own role, not whatever else the model
+ // knows about the user or their teammates.
+ expect(message.text).toContain("Do not ask about work outside your role");
expect(message.text).not.toContain("run_agent");
expect(message.text).not.toContain("EXPERT_KICKOFF");
expect(message.text).not.toContain(EXPERT_ID);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/expertKickoff.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/expertKickoff.ts
index 50cd9100ee..97549a6124 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/expertKickoff.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/expertKickoff.ts
@@ -14,12 +14,14 @@ const PENDING_TTL_MS = 2 * 60 * 1000;
const KICKOFF_PROMPT =
"You were just hired. Call expert_onboarding once, and nothing else, this " +
"turn: a greeting of 1-2 sentences introducing yourself in your voice, " +
- "then 3-5 questions the user can answer by tapping. Draw the questions and " +
- "their options from your own role and skills and from the workflows " +
- "installed on you — ask which outcome to start with, and which of the " +
- "services your work depends on you should be connected to, naming the real " +
- "providers rather than asking in the abstract. Do not run a workflow, " +
- "create a schedule, or start any other work before the answers come back.";
+ "then 3-5 questions the user can answer by tapping. Every question and " +
+ "every option must come from your own role and area of expertise and from " +
+ "the workflows installed on you — ask which outcome in your area to start " +
+ "with, and which of the services your work depends on you should be " +
+ "connected to, naming the real providers rather than asking in the " +
+ "abstract. Do not ask about work outside your role, whatever else you " +
+ "know about the user or their team. Do not run a workflow, create a " +
+ "schedule, or start any other work before the answers come back.";
export interface ExpertKickoffMetadata {
kind: typeof EXPERT_KICKOFF_KIND;