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.
feat(backend): teach AutoPilot to propose the first expert hire (#14466)
Abhimanyu Yadav57037db4a803
diff --git a/.github/workflows/scripts/check_actions_status.py b/.github/workflows/scripts/check_actions_status.pyindex 0e618a13ec..8b142f9381 100644--- a/.github/workflows/scripts/check_actions_status.py+++ b/.github/workflows/scripts/check_actions_status.py@@ -104,7 +104,13 @@ def is_superseded_cancellation(continueif candidate.get("event") != source_run.get("event"):continue- if action_run_order(candidate) <= action_run_order(source_run):+ concurrent_run = bool(source_run.get("created_at")) and (+ candidate.get("created_at") == source_run["created_at"]+ )+ if (+ action_run_order(candidate) <= action_run_order(source_run)+ and not concurrent_run+ ):continueif (candidate.get("status") == "completed"@@ -135,7 +141,7 @@ def process_check_runs():print(f"Ignoring canceled check run {run['name']} "- f"(ID: {run['id']}) because a newer run of the "+ f"(ID: {run['id']}) because an equivalent run of the ""same workflow passed.")continuediff --git a/.github/workflows/scripts/test_check_actions_status.py b/.github/workflows/scripts/test_check_actions_status.pyindex c78181b65e..dd0461ca2a 100644--- a/.github/workflows/scripts/test_check_actions_status.py+++ b/.github/workflows/scripts/test_check_actions_status.py@@ -51,6 +51,39 @@ class ProcessCheckRunsTests(unittest.TestCase):self.assertEqual(result, (False, True))+ def test_accepts_passing_concurrent_run_despite_reversed_run_order(self):+ source = {**CANCELED_RUN, "created_at": "2026-09-09T08:51:20Z"}+ candidate = {+ **PASSING_RUN,+ "run_number": 10,+ "created_at": source["created_at"],+ }+ self.assertEqual(+ process_check_runs([CANCELED_CHECK], [source, candidate]), (False, True)+ )++ def test_rejects_older_or_unrelated_passing_run(self):+ source = {**CANCELED_RUN, "created_at": "2026-09-09T08:51:20Z"}+ candidate = {+ **PASSING_RUN,+ "run_number": 10,+ "created_at": source["created_at"],+ }+ for change in (+ {"created_at": "2026-09-09T08:51:19Z"},+ {"created_at": None},+ {"head_sha": "different"},+ {"event": "merge_group"},+ {"conclusion": "failure"},+ ):+ with self.subTest(change=change):+ self.assertEqual(+ process_check_runs(+ [CANCELED_CHECK], [source, {**candidate, **change}]+ ),+ (False, False),+ )+def test_rejects_cancellation_without_replacement(self):result = process_check_runs([CANCELED_CHECK], [CANCELED_RUN])diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 2c68deff9d..37b49246e7 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -81,6 +81,7 @@ from backend.copilot.prompting import (get_delegation_supplement,get_expert_oversight_supplement,get_graphiti_supplement,+ get_team_building_supplement,)from backend.copilot.provider_failure import classify as classify_provider_failurefrom backend.copilot.rate_limit import build_budget_ctx@@ -1882,6 +1883,9 @@ async def stream_chat_completion_baseline(oversight_supplement = get_expert_oversight_supplement(experts_enabled=experts_enabled, expert_id=session.expert_id)+ team_building_supplement = get_team_building_supplement(+ experts_enabled=experts_enabled, expert_id=session.expert_id+ )# Append the builder-session block (graph id+name + full building guide)# AFTER the shared supplements so the system prompt is byte-identical# across turns of the same builder session — Claude's prompt cache keeps@@ -1893,6 +1897,7 @@ async def stream_chat_completion_baseline(+ SHARED_TOOL_NOTES+ delegation_supplement+ oversight_supplement+ + team_building_supplement+ graphiti_supplement+ builder_session_suffix+ expert_session_suffixdiff --git a/autogpt_platform/backend/backend/copilot/eval/style/assembly.py b/autogpt_platform/backend/backend/copilot/eval/style/assembly.pyindex 896612d294..139226b3c3 100644--- a/autogpt_platform/backend/backend/copilot/eval/style/assembly.py+++ b/autogpt_platform/backend/backend/copilot/eval/style/assembly.py@@ -32,6 +32,7 @@ from backend.copilot.prompting import (get_expert_oversight_supplement,get_graphiti_supplement,get_sdk_supplement,+ get_team_building_supplement,)from backend.copilot.service import CACHEABLE_SYSTEM_PROMPT@@ -137,6 +138,10 @@ def chat_system_prompt(expert: Expert | None) -> str:experts_enabled=DELEGATION_ENABLED,expert_id=expert.id if expert else None,)+ + get_team_building_supplement(+ experts_enabled=DELEGATION_ENABLED,+ expert_id=expert.id if expert else None,+ )+ get_graphiti_supplement()+ suffix)diff --git a/autogpt_platform/backend/backend/copilot/expert_context.py b/autogpt_platform/backend/backend/copilot/expert_context.pyindex 38929828be..2cb63217ce 100644--- a/autogpt_platform/backend/backend/copilot/expert_context.py+++ b/autogpt_platform/backend/backend/copilot/expert_context.py@@ -267,12 +267,29 @@ async def _team_context(With the hire-experts flag off the roster still helps the model route arequest, but the rule falls back to pointing at the expert's thread —naming a tool the turn cannot execute is worse than saying nothing.++ An empty roster on a plain session is the Head-of-AI moment: instead of+ saying nothing, hand the model the hiring roster so it can propose a+ first teammate. The flag and the template read are paid only there — an+ expert session's empty teammate list is just a solo roster, not a user+ without a team."""experts = await experts_db().list_experts(user_id, with_metrics=False)+ hiring_roster: list[Expert] | None = None+ if (+ not any(e.id != exclude_expert_id for e in experts)+ and exclude_expert_id is None+ and delegation_enabled+ and await is_feature_enabled(+ Flag.ONBOARDING_EXPERT_TEAM, user_id, default=False+ )+ ):+ hiring_roster = await experts_db().list_templates()return render_team_context(experts,delegation_enabled=delegation_enabled,exclude_expert_id=exclude_expert_id,+ hiring_roster=hiring_roster,)@@ -281,10 +298,16 @@ def render_team_context(*,delegation_enabled: bool,exclude_expert_id: str | None = None,+ hiring_roster: list[Expert] | None = None,) -> str:+ """Pure renderer; ``hiring_roster`` is the template list for the+ Head-of-AI block and is only passed when the caller already checked the+ flag and found nobody hired. ``None`` keeps the empty roster silent."""teammates = [e for e in experts if e.id != exclude_expert_id]if not teammates:- return ""+ if hiring_roster is None:+ return ""+ return _empty_team_context(hiring_roster)lines = "\n".join(_team_line(e) for e in teammates)rule = _team_rule(@@ -340,3 +363,45 @@ def _team_line(expert: Expert) -> str:f"{escape_prompt_xml_tags(expert.role)} (expert id: {expert.id}); "f"installed workflows: {workflow_names}")+++def _empty_team_context(templates: list[Expert]) -> str:+ """Head-of-AI block for a user who has hired nobody yet.++ The roster is inlined rather than exposed as a tool: the tool schema has+ a character budget, and this block is only ever built once per session.+ """+ roster = (+ "Roster:\n" + "\n".join(_template_line(t) for t in templates)+ if templates+ else "Roster: none available yet — offer to raise a custom expert."+ )+ return (+ "<team_context>\n"+ "The user has not hired any experts yet. You are their Head of AI: "+ "when recurring work shows up, propose hiring one expert from the "+ "roster below with `hire_expert(template_id=...)`, or raising a "+ "custom one with `raise_expert(...)`, and say why. Always offer both "+ "paths (hire from the roster, or raise your own). Propose one hire "+ "at a time. Never hire silently — both tools return an approval card "+ "the user must confirm; do not describe the card's contents, the "+ "user sees it.\n"+ f"{roster}\n"+ "</team_context>\n\n"+ )+++def _template_line(template: Expert) -> str:+ workflow_names = (+ ", ".join(+ escape_prompt_xml_tags(w.name or "Unnamed workflow")+ for w in template.workflows+ )+ or "none installed"+ )+ return (+ f"- {escape_prompt_xml_tags(template.name)} — "+ f"{escape_prompt_xml_tags(template.role)} (template_id: {template.id}); "+ f"{escape_prompt_xml_tags(template.tagline or 'No tagline')}; "+ f"workflows: {workflow_names}"+ )diff --git a/autogpt_platform/backend/backend/copilot/expert_context_test.py b/autogpt_platform/backend/backend/copilot/expert_context_test.pyindex 9dd0912431..e02f98013f 100644--- a/autogpt_platform/backend/backend/copilot/expert_context_test.py+++ b/autogpt_platform/backend/backend/copilot/expert_context_test.py@@ -28,10 +28,27 @@ from backend.copilot.expert_context import (ExpertSessionUnavailableError,build_expert_identity_suffix,)+from backend.util.feature_flag import Flag_EC = "backend.copilot.expert_context"+def _flag_mock(**overrides: bool) -> AsyncMock:+ """``is_feature_enabled`` stub answering per flag.++ Defaults mirror production for an existing user: hire-experts on, the+ onboarding-team child flag off. Pass ``ONBOARDING_EXPERT_TEAM=True`` to+ opt a test into the Head-of-AI cohort.+ """+ enabled = {Flag.HIRE_EXPERTS: True, Flag.ONBOARDING_EXPERT_TEAM: False}+ enabled |= {Flag[name]: value for name, value in overrides.items()}++ async def is_enabled(flag: Flag, _user_id: str, default: bool = False) -> bool:+ return enabled.get(flag, default)++ return AsyncMock(side_effect=is_enabled)++@pytest.fixture(autouse=True)def hire_experts_flag_on():"""Pin the hire-experts flag on.@@ -40,7 +57,7 @@ def hire_experts_flag_on():name ``delegate_to_expert``; without pinning it these tests would followwhatever LaunchDarkly (or a local ``FORCE_FLAG_`` override) says."""- with patch(f"{_EC}.is_feature_enabled", AsyncMock(return_value=True)):+ with patch(f"{_EC}.is_feature_enabled", _flag_mock()):yield@@ -95,6 +112,18 @@ def _expert()+def _template(+ template_id: str = "tpl-1",+ name: str = "Maria",+ role: str = "Marketing Lead",+ tagline: str | None = "Runs your <campaigns>.",+ workflows: list[ExpertWorkflowRef] | None = None,+) -> Expert:+ return _expert(+ expert_id=template_id, name=name, role=role, workflows=workflows+ ).model_copy(update={"is_template": True, "tagline": tagline})++class TestBuildExpertIdentitySuffix:"""Identity lives in the per-session system-prompt suffix (samemechanism as building mode) so it outranks the first-message context.@@ -437,6 +466,27 @@ class TestBuildExpertContextExpertSession:assert "<team_context>" not in result+ @pytest.mark.asyncio+ async def test_solo_expert_in_onboarding_cohort_gets_no_hiring_roster(self):+ """An expert session's empty teammate list is a solo roster, not a+ user without a team: even with the onboarding-team flag on it must+ not be handed the Head-of-AI block or pay for the template read."""+ 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()])+ mock_db.list_templates = AsyncMock(return_value=[_template()])+ with (+ patch(f"{_EC}.is_feature_enabled", _flag_mock(ONBOARDING_EXPERT_TEAM=True)),+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ ):+ result = await build_expert_context("user-1", "exp-1")++ assert "<team_context>" not in result+ assert "Head of AI" not in result+ mock_db.list_templates.assert_not_called()+@pytest.mark.asyncioasync def test_teammate_lookup_failure_keeps_workflows(self):from backend.copilot.expert_context import build_expert_context@@ -601,6 +651,101 @@ class TestBuildExpertContextPlainSession:assert result == ""+ @pytest.mark.asyncio+ async def test_no_experts_with_team_flag_renders_head_of_ai_block(self):+ """Flag-on, nothing hired: AutoPilot gets the roster and its+ Head-of-AI brief instead of silence, so a recurring-work request can+ turn into a hire proposal."""+ from backend.copilot.expert_context import build_expert_context++ templates = [+ _template(),+ _template(+ template_id="tpl-2",+ name="Max",+ role="Sales Rep",+ tagline="Finds your leads.",+ workflows=[],+ ),+ ]+ mock_db = MagicMock()+ mock_db.list_experts = AsyncMock(return_value=[])+ mock_db.list_templates = AsyncMock(return_value=templates)+ with (+ patch(f"{_EC}.is_feature_enabled", _flag_mock(ONBOARDING_EXPERT_TEAM=True)),+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ ):+ result = await build_expert_context("user-1", None)++ assert "<team_context>" in result+ assert "</team_context>" in result+ assert "Head of AI" in result+ assert "hire_expert(template_id=...)" in result+ assert "raise_expert(...)" in result+ assert "Propose one hire at a time." in result+ assert (+ "- Maria — Marketing Lead (template_id: tpl-1); "+ "Runs your <campaigns>.; workflows: SEO Audit"+ ) in result+ assert (+ "- Max — Sales Rep (template_id: tpl-2); Finds your leads.; "+ "workflows: none installed"+ ) in result+ assert "<campaigns>" not in result++ @pytest.mark.asyncio+ async def test_no_experts_with_team_flag_and_empty_roster_offers_raise(self):+ from backend.copilot.expert_context import build_expert_context++ mock_db = MagicMock()+ mock_db.list_experts = AsyncMock(return_value=[])+ mock_db.list_templates = AsyncMock(return_value=[])+ with (+ patch(f"{_EC}.is_feature_enabled", _flag_mock(ONBOARDING_EXPERT_TEAM=True)),+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ ):+ result = await build_expert_context("user-1", None)++ assert "<team_context>" in result+ assert "Roster: none available yet — offer to raise a custom expert." in result++ @pytest.mark.asyncio+ async def test_no_experts_flag_on_but_delegation_off_returns_empty(self):+ """``hire_expert`` rides the same tool group as ``delegate_to_expert``+ — with hire-experts off the block would name a tool the turn cannot+ execute."""+ from backend.copilot.expert_context import build_expert_context++ mock_db = MagicMock()+ mock_db.list_experts = AsyncMock(return_value=[])+ mock_db.list_templates = AsyncMock(return_value=[_template()])+ with (+ patch(+ f"{_EC}.is_feature_enabled",+ _flag_mock(HIRE_EXPERTS=False, ONBOARDING_EXPERT_TEAM=True),+ ),+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ ):+ result = await build_expert_context("user-1", None)++ assert result == ""+ mock_db.list_templates.assert_not_called()++ @pytest.mark.asyncio+ async def test_template_lookup_failure_returns_empty(self):+ from backend.copilot.expert_context import build_expert_context++ mock_db = MagicMock()+ mock_db.list_experts = AsyncMock(return_value=[])+ mock_db.list_templates = AsyncMock(side_effect=RuntimeError("db down"))+ with (+ patch(f"{_EC}.is_feature_enabled", _flag_mock(ONBOARDING_EXPERT_TEAM=True)),+ patch(f"{_EC}.experts_db", MagicMock(return_value=mock_db)),+ ):+ result = await build_expert_context("user-1", None)++ assert result == ""+@pytest.mark.asyncioasync def test_team_context_escapes_expert_role_tags(self):from backend.copilot.expert_context import build_expert_contextdiff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 15c5dfdde1..1a878408cc 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -696,6 +696,32 @@ def get_delegation_supplement() -> str:"""+def get_team_building_supplement(+ *, experts_enabled: bool, expert_id: str | None+) -> str:+ """Head-of-AI rules for growing the roster, not just using it.++ Gated like ``get_expert_oversight_supplement`` rather than folded into+ ``get_delegation_supplement``: ``hire_expert`` and ``raise_expert`` sit in+ the ``expert_admin`` tool group, which an expert session's ``execute_tool``+ refuses, so only a plain AutoPilot turn with the team flag on is told to+ grow the roster. Naming the tools to anyone else advertises a refusal.+ """+ if not experts_enabled or expert_id:+ return ""+ return """++### Building the team+- You are the user's Head of AI. When recurring work has no owner, propose a+ teammate for it: `hire_expert` for a roster template, `raise_expert` for a+ custom one. Offer both paths and say which you'd pick and why.+- One proposal at a time — never a slate of hires in a single turn.+- Never hire silently. Both tools only propose: the user sees an approval+ card and confirms it. Don't restate what's on the card; one short line,+ then wait.+"""++def get_expert_oversight_supplement(*, experts_enabled: bool, expert_id: str | None) -> str:diff --git a/autogpt_platform/backend/backend/copilot/prompting_test.py b/autogpt_platform/backend/backend/copilot/prompting_test.pyindex 1c296182f9..39c44ef1a8 100644--- a/autogpt_platform/backend/backend/copilot/prompting_test.py+++ b/autogpt_platform/backend/backend/copilot/prompting_test.py@@ -101,6 +101,45 @@ class TestGraphitiMemoryScope:assert "Memory is private to this user — no other user can see it" not in result+class TestTeamBuildingSupplement:+ """``hire_expert`` / ``raise_expert`` are ``expert_admin`` tools, so only a+ plain AutoPilot turn with the team flag on may be told to grow the roster.+ An expert session sees both sides of a delegation but cannot hire."""++ def test_an_autopilot_turn_with_the_flag_on_is_head_of_ai(self):+ result = prompting.get_team_building_supplement(+ experts_enabled=True, expert_id=None+ )++ assert "Building the team" in result+ assert "hire_expert" in result+ assert "raise_expert" in result+ assert "One proposal at a time" in result+ assert "Never hire silently" in result++ def test_an_expert_session_is_not_told_to_hire(self):+ result = prompting.get_team_building_supplement(+ experts_enabled=True, expert_id="expert-a"+ )++ assert result == ""++ def test_the_flag_off_tells_nobody(self):+ assert (+ prompting.get_team_building_supplement(+ experts_enabled=False, expert_id=None+ )+ == ""+ )++ def test_delegation_supplement_no_longer_carries_hiring_rules(self):+ result = prompting.get_delegation_supplement()++ assert "Delegating to a teammate" in result+ assert "Building the team" not in result+ assert "hire_expert" not in result++class TestExpertOversightSupplement:"""The chat-reading tools are in the ``expert_admin`` group, so only anAutopilot session with the team flag on can call them — a turn thatdiff --git a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.pyindex 61edfaa207..8b359969cd 100644--- a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py@@ -78,6 +78,7 @@ class TestApplyBuildingModeRestart:thinking_reprompted: bool = False,delegation_supplement: str = "",oversight_supplement: str = "",+ team_building_supplement: str = "",):from backend.copilot.sdk.service import (_BUILDING_MODE_CONTINUATION,@@ -100,6 +101,7 @@ class TestApplyBuildingModeRestart:base_system_prompt="BASE",delegation_supplement=delegation_supplement,oversight_supplement=oversight_supplement,+ team_building_supplement=team_building_supplement,graphiti_supplement="",use_e2b=False,session_id="sess-1",@@ -187,6 +189,7 @@ class TestApplyBuildingModeRestart:base_system_prompt="BASE",delegation_supplement="",oversight_supplement="",+ team_building_supplement="",graphiti_supplement="",use_e2b=False,session_id="sess-1",diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 8954cd6cd3..8c09194f25 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -118,6 +118,7 @@ from ..permissions import (from ..prompting import (get_delegation_supplement,get_expert_oversight_supplement,+ get_team_building_supplement,get_graphiti_supplement,get_sdk_supplement,)@@ -1697,6 +1698,7 @@ async def _apply_building_mode_restart(base_system_prompt: str,delegation_supplement: str,oversight_supplement: str,+ team_building_supplement: str,graphiti_supplement: str,use_e2b: bool,session_id: str,@@ -1735,6 +1737,7 @@ async def _apply_building_mode_restart(+ get_sdk_supplement(use_e2b=use_e2b)+ delegation_supplement+ oversight_supplement+ + team_building_supplement+ graphiti_supplement+ building_suffix+ expert_session_suffix@@ -4796,6 +4799,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesoversight_supplement = get_expert_oversight_supplement(experts_enabled=experts_enabled, expert_id=session.expert_id)+ team_building_supplement = get_team_building_supplement(+ experts_enabled=experts_enabled, expert_id=session.expert_id+ )# Append the builder-session block (graph id+name + full building# guide) AFTER the shared supplements so the system prompt is# byte-identical across turns of the same builder session — Claude's@@ -4812,6 +4818,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues+ get_sdk_supplement(use_e2b=use_e2b)+ delegation_supplement+ oversight_supplement+ + team_building_supplement+ graphiti_supplement+ builder_session_suffix+ expert_session_suffix@@ -5473,17 +5480,18 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# falls back to full session.messages[:-1] from DB — the authoritative# source. transcript+gap is an optimisation for the first attempt only;# on retry the extra overhead of full-DB context is acceptable.- state.query_message, state.compaction_stats = (- await _build_query_message(- current_message,- session,- state.use_resume,- state.transcript_msg_count,- session_id,- session_msg_ceiling=_pre_drain_msg_count,- target_tokens=state.target_tokens,- expect_compaction=True,- )+ (+ state.query_message,+ state.compaction_stats,+ ) = await _build_query_message(+ current_message,+ session,+ state.use_resume,+ state.transcript_msg_count,+ session_id,+ session_msg_ceiling=_pre_drain_msg_count,+ target_tokens=state.target_tokens,+ expect_compaction=True,)if _retry_reduced_context(reduced=ctx,@@ -5582,6 +5590,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesbase_system_prompt=base_system_prompt,delegation_supplement=delegation_supplement,oversight_supplement=oversight_supplement,+ team_building_supplement=team_building_supplement,graphiti_supplement=graphiti_supplement,use_e2b=use_e2b,session_id=session_id,