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(platform): hiring an expert returns at once; its skills and workflows install in the background (#14849)
Reinier van der Leerb4315fcd01b5
diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db.py b/autogpt_platform/backend/backend/api/features/experts/experts_db.pyindex 25cb46c5cc..fe759c4560 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db.py@@ -2,7 +2,7 @@ import asyncioimport loggingfrom collections import defaultdictfrom collections.abc import Callable, Sequence-from datetime import date, datetime, time, timedelta+from datetime import date, datetime, time, timedelta, timezonefrom typing import Literal, castfrom zoneinfo import ZoneInfo@@ -55,6 +55,7 @@ from backend.api.features.experts.models import (ExpertRun,ExpertRunSource,ExpertRunStatus,+ ExpertSetupStatus,ExpertSoulFieldsPatch,ExpertSoulUpdate,ExpertTemplate,@@ -115,6 +116,7 @@ from backend.data.expert_spend import get_weekly_spendfrom backend.data.model import NodeExecutionStatsfrom backend.data.user import get_user_by_idfrom backend.util import type as type_utils+from backend.util.background import spawn_background_taskfrom backend.util.exceptions import (ConflictError,ExpertNotFoundError,@@ -128,6 +130,12 @@ from backend.util.timezone_utils import get_user_timezone_or_utclogger = logging.getLogger(__name__)+# A setup job holding its lease longer than this died with its process; the+# longest measured setup (8 skills) is ~25 s per attempt.+SETUP_LEASE = timedelta(minutes=10)+_SETUP_ATTEMPTS = 3+_SETUP_RETRY_DELAY_SECONDS = 5+def _raised_identity(name: str) -> str:# f-string, not str.format on a template: user names may contain { or },@@ -286,9 +294,21 @@ def _to_model(weekly_spend=weekly_spend,schedules_paused_at=row.schedulesPausedAt,pod_id=row.podId,+ setup_status=_setup_status(row),+ setup_failures=row.setupFailures or [],)+def _setup_status(row: prisma.models.Expert) -> ExpertSetupStatus:+ # A job whose lease ran out died with its process; show it as retryable.+ if row.setupStatus == prisma.enums.ExpertSetupStatus.INSTALLING and (+ row.setupStartedAt is None+ or row.setupStartedAt < datetime.now(timezone.utc) - SETUP_LEASE+ ):+ return "failed"+ return cast(ExpertSetupStatus, row.setupStatus.lower())++async def _latest_runs(expert_ids: list[str],) -> dict[str, prisma.models.AgentGraphExecution]:@@ -861,6 +881,14 @@ async def expert_row_exists(user_id: str, expert_id: str) -> bool:return count > 0+async def expert_setup_status(user_id: str, expert_id: str) -> ExpertSetupStatus:+ """Where the owner's expert is in its hire setup; "ready" if not found."""+ row = await prisma.models.Expert.prisma().find_first(+ where={"id": expert_id, "ownerUserId": user_id}+ )+ return _setup_status(row) if row is not None else "ready"++async def resolve_private_expert_tenancy(user_id: str, expert_id: str) -> tuple[str, str | None]:@@ -890,8 +918,14 @@ async def resolve_private_expert_tenancy(async def hire_expert(user_id: str, template_id: str, name: str | None) -> HireResult:+ """Hire *template_id* and return as soon as the expert's row exists.++ Its workflows, skills and routines install afterwards (``_run_hire_setup``);+ the expert reports progress through ``setup_status``. Re-hiring an expert+ whose setup failed or was abandoned starts that setup again.+ """try:- result, state = await _hire_expert_impl(user_id, template_id, name)+ return await _hire_expert_impl(user_id, template_id, name)except Exception:emit_funnel_event(user_id,@@ -899,24 +933,11 @@ async def hire_expert(user_id: str, template_id: str, name: str | None) -> HireR{"template_id": template_id, "failed_preloads_count": 0},)raise- # An idempotent re-hire of an already-active expert is not a hire.- if state != "existing":- emit_funnel_event(- user_id,- "hire_completed",- {- "template_id": template_id,- "failed_preloads_count": len(result.failed_preloads),- },- )- return resultasync def _hire_expert_impl(user_id: str, template_id: str, name: str | None-) -> tuple[HireResult, Literal["existing", "revived", "created"]]:- """The hire result plus which transition produced it, so the funnel can- count a genuine hire once and stay silent on an idempotent retry."""+) -> HireResult:template = await prisma.models.Expert.prisma().find_first(where={"id": template_id, "isTemplate": True, "isArchived": False},include=_WORKFLOW_INCLUDE,@@ -951,6 +972,8 @@ async def _hire_expert_impl(}if template.toolProfile is not None:create_data["toolProfile"] = template.toolProfile+ create_data["setupStatus"] = prisma.enums.ExpertSetupStatus.INSTALLING+ create_data["setupStartedAt"] = datetime.now(timezone.utc)try:expert, state = await _reserve_hired_expert(user_id, template_id, create_data)@@ -960,22 +983,165 @@ async def _hire_expert_impl(# the same capacity-aware path.expert, state = await _reserve_hired_expert(user_id, template_id, create_data)- if state == "existing":- return HireResult(expert=_to_model(expert), failed_preloads=[]), stateif state == "revived":expert = await _resume_revived_hire(expert)- return HireResult(expert=_to_model(expert), failed_preloads=[]), state+ # A hire that runs setup is counted by its job, once its preloads are+ # known; an idempotent re-hire of an already-active expert is not a hire.+ if state == "created" or await _claim_setup(expert.id):+ spawn_background_task(+ _run_hire_setup(+ user_id, expert.id, template.id, count_hire=state != "existing"+ ),+ name=f"hire-setup-{expert.id}",+ )+ if state != "created":+ expert = await _reload_expert(expert)+ elif state == "revived":+ emit_funnel_event(+ user_id,+ "hire_completed",+ {"template_id": template.id, "failed_preloads_count": 0},+ )+ return HireResult(expert=_to_model(expert))+++async def _reload_expert(row: prisma.models.Expert) -> prisma.models.Expert:+ refreshed = await prisma.models.Expert.prisma().find_unique(+ where={"id": row.id}, include=_WORKFLOW_INCLUDE+ )+ return refreshed or row+++async def _claim_setup(expert_id: str) -> bool:+ """Take the setup lease of a hire whose setup failed or whose job died.++ One conditional update, so two concurrent re-hires start one job.+ """+ now = datetime.now(timezone.utc)+ claimed = await prisma.models.Expert.prisma().update_many(+ where={+ "id": expert_id,+ "OR": [+ {"setupStatus": prisma.enums.ExpertSetupStatus.FAILED},+ {+ "setupStatus": prisma.enums.ExpertSetupStatus.INSTALLING,+ "setupStartedAt": {"lt": now - SETUP_LEASE},+ },+ ],+ },+ data={+ "setupStatus": prisma.enums.ExpertSetupStatus.INSTALLING,+ "setupStartedAt": now,+ },+ )+ return claimed == 1+++async def _run_hire_setup(+ user_id: str, expert_id: str, template_id: str, *, count_hire: bool = False+) -> None:+ """Install a hire's workflows, skills and routines, retrying what failed.++ Every step skips what an earlier run installed, so a retry or a re-claimed+ setup finishes the job instead of duplicating it. *count_hire* emits the+ hire's ``hire_completed`` once the failed preloads are known.+ """+ failures: list[str] | None = None+ failed_preloads: list[str] = []+ ready = False+ for attempt in range(_SETUP_ATTEMPTS):+ if attempt:+ await asyncio.sleep(_SETUP_RETRY_DELAY_SECONDS * attempt)+ try:+ failed_preloads, failed_rest = await _install_hire_contents(+ user_id, expert_id, template_id+ )+ except Exception:+ # Keeps the last named failures: a raise tells us nothing new.+ logger.exception(f"Setup attempt {attempt + 1} failed for #{expert_id}")+ continue+ failures = failed_preloads + failed_rest+ ready = not failures+ if ready:+ break+ if failures is None:+ failed_preloads = await _uninstalled_preloads(expert_id, template_id)+ failures = failed_preloads+ await prisma.models.Expert.prisma().update(+ where={"id": expert_id},+ data={+ "setupStatus": (+ prisma.enums.ExpertSetupStatus.READY+ if ready+ else prisma.enums.ExpertSetupStatus.FAILED+ ),+ "setupFailures": failures or [],+ },+ )+ if count_hire:+ emit_funnel_event(+ user_id,+ "hire_completed",+ {"template_id": template_id, "failed_preloads_count": len(failed_preloads)},+ )+++async def _uninstalled_preloads(expert_id: str, template_id: str) -> list[str]:+ """The template's preloads the hire still lacks, for a setup that never+ got far enough to name its failures."""+ template = await prisma.models.Expert.prisma().find_unique(+ where={"id": template_id}, include=_WORKFLOW_INCLUDE+ )+ installed = {+ w.storeListingVersionId+ for w in await prisma.models.ExpertWorkflow.prisma().find_many(+ where={"expertId": expert_id}+ )+ }+ return [+ w.StoreListingVersion.name if w.StoreListingVersion else w.storeListingVersionId+ for w in (template.Workflows if template else None) or []+ if w.storeListingVersionId and w.storeListingVersionId not in installed+ ]- failed = await _install_preloads(expert.id, user_id, template.Workflows or [])- await _install_bundled_skills(user_id, expert.id, template.id)- await install_routines(expert.id, await _template_routines(template.id))- hydrated = await prisma.models.Expert.prisma().find_unique(- where={"id": expert.id}, include=_WORKFLOW_INCLUDE+async def _install_hire_contents(+ user_id: str, expert_id: str, template_id: str+) -> tuple[list[str], list[str]]:+ """Install what the hire does not have yet; return the preloads, then+ the skills and routines, that still failed."""+ template = await prisma.models.Expert.prisma().find_unique(+ where={"id": template_id}, include=_WORKFLOW_INCLUDE+ )+ expert = await prisma.models.Expert.prisma().find_unique(+ where={"id": expert_id},+ include={"Workflows": True, "Routines": True},+ )+ if template is None or expert is None:+ raise ExpertNotFoundError(expert_id)+ installed_listings = {w.storeListingVersionId for w in expert.Workflows or []}+ installed_routines = {r.key for r in expert.Routines or []}+ failed_preloads = await _install_preloads(+ expert_id,+ user_id,+ [+ w+ for w in template.Workflows or []+ if w.storeListingVersionId not in installed_listings+ ],+ )+ failed = await _install_bundled_skills(+ user_id, expert_id, template_id, installed=set(expert.skills or [])+ )+ failed += await install_routines(+ expert_id,+ [+ r+ for r in await _template_routines(template_id)+ if r.key not in installed_routines+ ],)- if hydrated is None:- raise ExpertNotFoundError(expert.id)- return HireResult(expert=_to_model(hydrated), failed_preloads=failed), state+ return failed_preloads, failedasync def _reserve_hired_expert(@@ -1797,31 +1963,35 @@ async def _template_routines(async def _install_bundled_skills(- user_id: str, expert_id: str, template_id: str-) -> None:- """Install the Hub skills the template bundles into the new expert's folder.+ user_id: str, expert_id: str, template_id: str, *, installed: set[str]+) -> list[str]:+ """Install the Hub skills the template bundles into the new expert's+ folder, skipping names already *installed*; return the titles that failed.Each install records its name on the row; a failed one is logged andleaves no name, so the hire never lists a skill it does not have."""bundled = await _live_bundled_skills(user_id, [template_id])- skills = bundled.get(template_id, [])- if not skills:- return+ missing = [s for s in bundled.get(template_id, []) if s.slug not in installed]+ if not missing:+ return []try:outcomes: list[object] = list(await skill_db.install_marketplace_skills(- user_id, [s.slug for s in skills], expert_id=expert_id+ user_id, [s.slug for s in missing], expert_id=expert_id))except Exception as e:- outcomes = [e] * len(skills)- for skill, outcome in zip(skills, outcomes):+ outcomes = [e] * len(missing)+ failed: list[str] = []+ for skill, outcome in zip(missing, outcomes):if isinstance(outcome, Exception):logger.error(f"Failed to install bundled skill {skill.slug!r} on expert #{expert_id}",exc_info=outcome,)+ failed.append(skill.title)+ return failedasync def install_workflow(diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.pyindex 8a1d55d8a6..46aee982ce 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py@@ -331,6 +331,35 @@ def mock_embedding_functions():_seeded_template_ids: list[str] = []_seeded_user_ids: list[str] = []+# The hire as production calls it: returns before its setup job has run.+_hire_without_waiting = experts_db.hire_expert++[email protected](autouse=True)+def hire_waits_for_setup(monkeypatch):+ """Most tests assert on what a hire installs, so let them read it back+ once the background setup has finished."""++ async def hire_and_finish_setup(user_id, template_id, name):+ result = await _hire_without_waiting(user_id, template_id, name)+ await _finish_hire_setup()+ row = await prisma.models.Expert.prisma().find_unique(+ where={"id": result.expert.id}, include=experts_db._WORKFLOW_INCLUDE+ )+ assert row is not None+ return HireResult(expert=experts_db._to_model(row))++ monkeypatch.setattr(experts_db, "hire_expert", hire_and_finish_setup)+ monkeypatch.setattr(experts_db, "_SETUP_RETRY_DELAY_SECONDS", 0)+++async def _finish_hire_setup() -> None:+ from backend.util.background import _background_tasks++ await asyncio.gather(+ *(t for t in _background_tasks if t.get_name().startswith("hire-setup-"))+ )+@pytest.fixture(autouse=True)async def delete_rows_this_test_seeded():@@ -648,7 +677,7 @@ async def _hire_roster_and_assert_preloads(result = await experts_db.hire_expert(hire_user.id, templates[entry["name"]].id, None)- assert result.failed_preloads == []+ assert result.expert.setup_status == "ready"assert {w.store_listing_version_id for w in result.expert.workflows} == {expected[p["slug"]] for p in entry["preloads"]}@@ -812,8 +841,10 @@ async def test_a_failed_bundled_skill_install_does_not_fail_the_hire(hired = await experts_db.hire_expert(test_user.id, template.id, None)- install.assert_awaited_once()+ assert install.await_count == experts_db._SETUP_ATTEMPTSassert hired.expert.skills == []+ assert hired.expert.setup_status == "failed"+ assert len(hired.expert.setup_failures) == 1@pytest.mark.asyncio(loop_scope="session")@@ -847,6 +878,167 @@ def _patch_install(monkeypatch, install: AsyncMock) -> None:monkeypatch.setattr(experts_db.skill_db, "install_marketplace_skills", batch)+def _recording_install(**kwargs):+ """A skill install that records the name the way the real one does."""++ async def install(user_id, slug, *, expert_id):+ await experts_db.add_expert_skill_name(user_id, expert_id, slug)++ return AsyncMock(side_effect=install, **kwargs)+++async def _template_with_setup(server, hub_listing) -> prisma.models.Expert:+ """A template that ships a preload, a bundled skill and a routine."""+ slv_id = await _seed_store_listing(server)+ template = await _seed_template(+ name="Maria", preload_listings=[slv_id], bundled=[hub_listing.id]+ )+ await prisma.models.ExpertRoutine.prisma().create(+ data={+ "expertId": template.id,+ "key": "queue-sweep",+ "title": "Sweep the queue",+ "prompt": "Read the queue.",+ "crons": ["H 9 * * 1-5"],+ }+ )+ return template++[email protected](loop_scope="session")+async def test_hire_returns_before_any_skill_is_installed(+ server: SpinTestServer, test_user, hub_listing, skills_hub_on, monkeypatch+):+ release = asyncio.Event()+ record = _recording_install()++ async def slow_install(user_id, slug, *, expert_id):+ await release.wait()+ await record(user_id, slug, expert_id=expert_id)++ _patch_install(monkeypatch, AsyncMock(side_effect=slow_install))+ template = await _seed_template(+ name="Maria", preload_listings=[], bundled=[hub_listing.id]+ )++ # Kills: awaiting the setup inside the request (the hire never returns).+ hired = await asyncio.wait_for(+ _hire_without_waiting(test_user.id, template.id, None), timeout=10+ )+ assert hired.expert.setup_status == "installing"+ assert hired.expert.skills == []+ assert (+ await experts_db.expert_setup_status(test_user.id, hired.expert.id)+ == "installing"+ )++ release.set()+ await _finish_hire_setup()+ done = await experts_db.get_expert(test_user.id, hired.expert.id)+ assert done is not None+ assert done.setup_status == "ready"+ assert done.skills == [hub_listing.slug]++[email protected](loop_scope="session")+async def test_hire_setup_installs_everything_once_and_reruns_as_a_no_op(+ server: SpinTestServer, test_user, hub_listing, skills_hub_on, monkeypatch+):+ install = _recording_install()+ _patch_install(monkeypatch, install)+ template = await _template_with_setup(server, hub_listing)+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ assert hired.expert.setup_status == "ready"+ assert hired.expert.skills == [hub_listing.slug]+ assert len(hired.expert.workflows) == 1+ install.reset_mock()++ # Kills: dropping any of the three "already installed" filters.+ with patch.object(+ experts_db.library_db, "add_store_agent_to_library", new_callable=AsyncMock+ ) as add_agent:+ await experts_db._run_hire_setup(test_user.id, hired.expert.id, template.id)+ install.assert_not_awaited()+ add_agent.assert_not_awaited()+ routines = await prisma.models.ExpertRoutine.prisma().find_many(+ where={"expertId": hired.expert.id}+ )+ assert [r.key for r in routines] == ["queue-sweep"]+ assert (+ await experts_db.expert_setup_status(test_user.id, hired.expert.id) == "ready"+ )++[email protected](loop_scope="session")+async def test_rehire_retries_a_failed_setup(+ server: SpinTestServer, test_user, hub_listing, skills_hub_on, monkeypatch+):+ _patch_install(monkeypatch, AsyncMock(side_effect=RuntimeError("storage down")))+ template = await _seed_template(+ name="Maria", preload_listings=[], bundled=[hub_listing.id]+ )+ failed = await experts_db.hire_expert(test_user.id, template.id, None)+ assert failed.expert.setup_status == "failed"++ _patch_install(monkeypatch, _recording_install())+ # Kills: an "existing" re-hire that does not re-claim the setup.+ with patch.object(experts_db, "emit_funnel_event") as emit:+ retried = await experts_db.hire_expert(test_user.id, template.id, None)+ emit.assert_not_called()+ assert retried.expert.id == failed.expert.id+ assert retried.expert.setup_status == "ready"+ assert retried.expert.setup_failures == []+ assert retried.expert.skills == [hub_listing.slug]++[email protected](loop_scope="session")+async def test_concurrent_rehires_start_one_setup(+ server: SpinTestServer, test_user, monkeypatch+):+ template = await _seed_template(name="Maria", preload_listings=[])+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ await prisma.models.Expert.prisma().update(+ where={"id": hired.expert.id},+ data={"setupStatus": prisma.enums.ExpertSetupStatus.FAILED},+ )+ spawn = MagicMock(side_effect=lambda coro, name: coro.close())+ monkeypatch.setattr(experts_db, "spawn_background_task", spawn)++ # Kills: a claim that is not one conditional update.+ await asyncio.gather(+ _hire_without_waiting(test_user.id, template.id, None),+ _hire_without_waiting(test_user.id, template.id, None),+ )+ assert spawn.call_count == 1++[email protected](loop_scope="session")+async def test_a_setup_whose_lease_ran_out_reads_failed_and_can_be_reclaimed(+ server: SpinTestServer, test_user+):+ template = await _seed_template(name="Maria", preload_listings=[])+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ fresh = datetime.now(timezone.utc)+ await prisma.models.Expert.prisma().update(+ where={"id": hired.expert.id},+ data={+ "setupStatus": prisma.enums.ExpertSetupStatus.INSTALLING,+ "setupStartedAt": fresh,+ },+ )+ assert not await experts_db._claim_setup(hired.expert.id)++ await prisma.models.Expert.prisma().update(+ where={"id": hired.expert.id},+ data={"setupStartedAt": fresh - experts_db.SETUP_LEASE - timedelta(seconds=1)},+ )+ # Kills: trusting INSTALLING forever after the job's process died.+ assert (+ await experts_db.expert_setup_status(test_user.id, hired.expert.id) == "failed"+ )+ assert await experts_db._claim_setup(hired.expert.id)++@pytest.mark.asyncio(loop_scope="session")async def test_hire_expert_is_idempotent_at_active_cap(server: SpinTestServer):owner = await _create_seed_user()@@ -1978,7 +2170,7 @@ async def test_hire_installs_preloads_into_library(server: SpinTestServer, test_wf = result.expert.workflows[0]assert wf.library_agent_id is not Noneassert wf.store_listing_version_id == slv_id- assert result.failed_preloads == []+ assert result.expert.setup_status == "ready"@pytest.mark.asyncio(loop_scope="session")@@ -1996,7 +2188,8 @@ async def test_hire_reports_failed_preload_without_sinking_hire(result = await experts_db.hire_expert(test_user.id, template.id, None)assert not result.expert.is_templateassert result.expert.workflows == []- assert len(result.failed_preloads) == 1+ assert result.expert.setup_status == "failed"+ assert len(result.expert.setup_failures) == 1@pytest.mark.asyncio(loop_scope="session")@@ -3045,7 +3238,7 @@ async def test_hire_creates_schedule_from_template_cadence(wf = result.expert.workflows[0]assert wf.schedule_cron == "40 7 * * *"assert wf.schedule_id == "sched-1"- assert result.failed_preloads == []+ assert result.expert.setup_status == "ready"call_kwargs = mock_scheduler.add_execution_schedule.call_args.kwargsassert call_kwargs["cron"] == "40 7 * * *"assert call_kwargs["expert_id"] == result.expert.id@@ -3102,7 +3295,7 @@ async def test_hire_skips_the_schedule_when_the_graph_needs_user_input(wf = result.expert.workflows[0]assert wf.schedule_cron == "40 7 * * *"assert wf.schedule_id is None- assert result.failed_preloads == []+ assert result.expert.setup_status == "ready"mock_scheduler.add_execution_schedule.assert_not_awaited()@@ -3157,7 +3350,7 @@ async def test_hire_schedule_failure_marks_needs_setup(assert wf.library_agent_id is not Noneassert wf.schedule_cron == "40 7 * * *"assert wf.schedule_id is None- assert result.failed_preloads == []+ assert result.expert.setup_status == "ready"@pytest.mark.asyncio(loop_scope="session")@@ -5907,6 +6100,77 @@ async def test_hire_completed_reports_failed_preloads_count()[email protected](loop_scope="session")+async def test_a_raising_retry_keeps_the_failures_an_earlier_attempt_named(+ server: SpinTestServer, test_user, monkeypatch+):+ template = await _seed_template(name="Maria", preload_listings=[])+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ monkeypatch.setattr(+ experts_db,+ "_install_hire_contents",+ AsyncMock(+ side_effect=[+ (["Agent X"], ["skill-a"]),+ RuntimeError("db"),+ RuntimeError("db"),+ ]+ ),+ )++ with patch.object(experts_db, "emit_funnel_event") as emit:+ await experts_db._run_hire_setup(+ test_user.id, hired.expert.id, template.id, count_hire=True+ )++ # Kills: resetting the failures when a later attempt raises.+ done = await experts_db.get_expert(test_user.id, hired.expert.id)+ assert done is not None+ assert done.setup_status == "failed"+ assert done.setup_failures == ["Agent X", "skill-a"]+ emit.assert_called_once_with(+ test_user.id,+ "hire_completed",+ {"template_id": template.id, "failed_preloads_count": 1},+ )++[email protected](loop_scope="session")+async def test_a_setup_that_only_raised_reports_the_preloads_it_lacks(+ server: SpinTestServer, test_user, monkeypatch+):+ slv_id = await _seed_store_listing(server)+ template = await _seed_template(name="Maria", preload_listings=[slv_id])+ with patch.object(+ experts_db.library_db,+ "add_store_agent_to_library",+ new_callable=AsyncMock,+ side_effect=RuntimeError("install exploded"),+ ):+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ named = hired.expert.setup_failures+ assert len(named) == 1+ monkeypatch.setattr(+ experts_db, "_install_hire_contents", AsyncMock(side_effect=RuntimeError("db"))+ )++ with patch.object(experts_db, "emit_funnel_event") as emit:+ await experts_db._run_hire_setup(+ test_user.id, hired.expert.id, template.id, count_hire=True+ )++ # Kills: settling a total failure with no failures and a zero count.+ done = await experts_db.get_expert(test_user.id, hired.expert.id)+ assert done is not None+ assert done.setup_status == "failed"+ assert done.setup_failures == named+ emit.assert_called_once_with(+ test_user.id,+ "hire_completed",+ {"template_id": template.id, "failed_preloads_count": 1},+ )++@pytest.mark.asyncio(loop_scope="session")async def test_idempotent_rehire_does_not_reemit_hire_completed(server: SpinTestServer, test_user@@ -5934,6 +6198,32 @@ async def test_reviving_archived_expert_emits_hire_completed()[email protected](loop_scope="session")+async def test_reviving_a_failed_setup_counts_the_hire_once_setup_settles(+ server: SpinTestServer, test_user+):+ slv_id = await _seed_store_listing(server)+ template = await _seed_template(name="Maria", preload_listings=[slv_id])+ with patch.object(+ experts_db.library_db,+ "add_store_agent_to_library",+ new_callable=AsyncMock,+ side_effect=RuntimeError("install exploded"),+ ):+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ assert hired.expert.setup_status == "failed"+ await experts_db.archive_expert(test_user.id, hired.expert.id)+ with patch.object(experts_db, "emit_funnel_event") as emit:+ await experts_db.hire_expert(test_user.id, template.id, None)++ # Kills: emitting from the request before the re-claimed setup has run.+ emit.assert_called_once_with(+ test_user.id,+ "hire_completed",+ {"template_id": template.id, "failed_preloads_count": 1},+ )++@pytest.mark.asyncio(loop_scope="session")async def test_concurrent_revival_emits_hire_completed_once(server: SpinTestServer, test_userdiff --git a/autogpt_platform/backend/backend/api/features/experts/models.py b/autogpt_platform/backend/backend/api/features/experts/models.pyindex ca8d528851..7c13caa25b 100644--- a/autogpt_platform/backend/backend/api/features/experts/models.py+++ b/autogpt_platform/backend/backend/api/features/experts/models.py@@ -238,6 +238,9 @@ class ExpertRoutine(BaseModel):return bool(self.crons)+ExpertSetupStatus = Literal["installing", "ready", "failed"]++class Expert(BaseModel):id: strname: str@@ -282,6 +285,10 @@ class Expert(BaseModel):schedules_paused_at: datetime | None = None# Owner-scoped grouping. None = ungrouped ("unpodded").pod_id: str | None = None+ # A hire's workflows, skills and routines land after it is returned.+ setup_status: ExpertSetupStatus = "ready"+ # What setup could not install; re-hiring the template retries it.+ setup_failures: list[str] = []class ExpertBundledSkill(BaseModel):@@ -360,7 +367,6 @@ class ExpertDetachPreview(BaseModel):class HireResult(BaseModel):expert: Expert- failed_preloads: list[str]RaiseAttachmentKind = Literal["workflow", "skill"]diff --git a/autogpt_platform/backend/backend/api/features/experts/routes_test.py b/autogpt_platform/backend/backend/api/features/experts/routes_test.pyindex e149292512..023153b873 100644--- a/autogpt_platform/backend/backend/api/features/experts/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/routes_test.py@@ -268,7 +268,7 @@ def _mock_templates_with_hub_skill(# ─── Hire ──────────────────────────────────────────────────────────────-def test_hire_expert_returns_expert_and_empty_failed_preloads(+def test_hire_expert_returns_expert(mocker: pytest_mock.MockerFixture,test_user_id: str,) -> None:@@ -276,7 +276,7 @@ def test_hire_expert_returns_expert_and_empty_failed_preloads(mock_hire = mocker.patch("backend.api.features.experts.routes.experts_db.hire_expert",new_callable=AsyncMock,- return_value=HireResult(expert=hired, failed_preloads=[]),+ return_value=HireResult(expert=hired),)response = client.post("/experts", json={"template_id": "template-1"})@@ -284,7 +284,6 @@ def test_hire_expert_returns_expert_and_empty_failed_preloads(assert response.status_code == 200data = response.json()assert data["expert"]["id"] == "expert-1"- assert data["failed_preloads"] == []mock_hire.assert_awaited_once_with(test_user_id, "template-1", None)@@ -295,7 +294,7 @@ def test_hire_expert_twice_returns_same_expert_id(mocker.patch("backend.api.features.experts.routes.experts_db.hire_expert",new_callable=AsyncMock,- return_value=HireResult(expert=hired, failed_preloads=[]),+ return_value=HireResult(expert=hired),)first = client.post("/experts", json={"template_id": "template-1"})diff --git a/autogpt_platform/backend/backend/api/features/experts/routines.py b/autogpt_platform/backend/backend/api/features/experts/routines.pyindex c2def226b1..bf5d73463f 100644--- a/autogpt_platform/backend/backend/api/features/experts/routines.py+++ b/autogpt_platform/backend/backend/api/features/experts/routines.py@@ -104,13 +104,15 @@ class RoutineUnansweredAsksError(Exception):async def install_routines(expert_id: str, template_routines: list[prisma.models.ExpertRoutine]-) -> None:+) -> list[str]:"""Copy a template's routine proposals onto a new hire, all switched off.Deliberately creates no scheduler jobs: a cadence that fires unattendedfrom the day of hire is a much bigger promise than a skill, and nobody hasmade it yet. The rows exist so the expert can offer them on its first turn.+ Returns the titles that failed to install."""+ failed: list[str] = []for routine in template_routines:data: prisma.types.ExpertRoutineCreateInput = {"expertId": expert_id,@@ -133,6 +135,8 @@ async def install_routines(logger.exception(f"Failed to install routine {routine.key!r} on expert #{expert_id}")+ failed.append(routine.title)+ return failedasync def list_routines(diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.pyindex 04caef6456..ce27730a95 100644--- a/autogpt_platform/backend/backend/copilot/executor/processor.py+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py@@ -79,6 +79,10 @@ _CANCEL_DRAIN_LOG_INTERVAL_SECONDS = 1.0# at least the pool worker thread isn't blocked forever._FAIL_CLOSE_REDIS_TIMEOUT = 10.0_CODEX_CREDENTIAL_ACQUIRE_TIMEOUT_SECONDS = 5.0+# A hire's setup runs after the hire returns (~25 s for 8 skills); its first+# turn waits this long for the skills and workflows the turn is built from.+EXPERT_SETUP_WAIT_SECONDS = 60.0+EXPERT_SETUP_POLL_SECONDS = 1.0# Module-level symbol preserved for backward-compat with callers that import@@ -213,6 +217,27 @@ async def _normalize_private_expert_session_tenancy(return await upsert_chat_session(session, persist_tenancy=True)+async def _wait_for_expert_setup(session: "ChatSession") -> None:+ """Hold a fresh hire's turn until its skills and workflows are installed,+ or give up after ``EXPERT_SETUP_WAIT_SECONDS`` and run without them."""+ if session.expert_id is None:+ return+ from backend.data.db_accessors import experts_db++ deadline = time.monotonic() + EXPERT_SETUP_WAIT_SECONDS+ while (+ await experts_db().expert_setup_status(session.user_id, session.expert_id)+ == "installing"+ ):+ if time.monotonic() >= deadline:+ logger.warning(+ f"Expert #{session.expert_id} still setting up after "+ f"{EXPERT_SETUP_WAIT_SECONDS}s; running the turn without waiting"+ )+ return+ await asyncio.sleep(EXPERT_SETUP_POLL_SECONDS)++def execute_copilot_turn(entry: CoPilotExecutionEntry,cancel: threading.Event,@@ -551,6 +576,7 @@ class CoPilotProcessor:raise RuntimeError("codex_session_route_mismatch")session = await _normalize_private_expert_session_tenancy(session)+ await _wait_for_expert_setup(session)if entry.llm_auth_provider == "codex":from backend.integrations.codex.access import enforce_codex_accessdiff --git a/autogpt_platform/backend/backend/copilot/executor/processor_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_test.pyindex 2e8420165e..c0bc016b32 100644--- a/autogpt_platform/backend/backend/copilot/executor/processor_test.py+++ b/autogpt_platform/backend/backend/copilot/executor/processor_test.py@@ -23,6 +23,7 @@ from backend.copilot.executor.processor import (_CODEX_CREDENTIAL_ACQUIRE_TIMEOUT_SECONDS,CoPilotProcessor,_normalize_private_expert_session_tenancy,+ _wait_for_expert_setup,sync_fail_close_session,)from backend.copilot.executor.utils import CoPilotExecutionEntry, CoPilotLogMetadata@@ -279,6 +280,7 @@ class TestExecuteAsyncAclose:expert_store.resolve_private_expert_tenancy = AsyncMock(return_value=("current-personal-org", "current-personal-team"))+ expert_store.expert_setup_status = AsyncMock(return_value="ready")async def persist(value, *, persist_tenancy: bool = False):assert persist_tenancy is True@@ -400,6 +402,7 @@ async def test_failed_expert_rehome_reloads_db_before_retrying_engine() -> None:expert_store.resolve_private_expert_tenancy = AsyncMock(return_value=("current-personal-org", "current-personal-team"))+ expert_store.expert_setup_status = AsyncMock(return_value="ready")session_db = MagicMock()session_db.get_next_sequence = AsyncMock(return_value=1)published = _TrackedStream(events=[])@@ -493,6 +496,35 @@ async def test_current_expert_session_stays_pinned_and_keeps_credentials() -> Noupsert.assert_not_awaited()+ "statuses, expected_reads",+ [+ (["ready"], 1),+ (["installing", "installing", "ready"], 3),+ # A setup that never finishes: poll until the deadline, then run.+ (["installing"] * 50, None),+ ],+)+async def test_expert_turn_waits_for_hire_setup(statuses, expected_reads) -> None:+ session = ChatSession.new("user-1", dry_run=False, expert_id="expert-1")+ expert_store = MagicMock()+ expert_store.expert_setup_status = AsyncMock(side_effect=statuses)++ with (+ patch("backend.data.db_accessors.experts_db", return_value=expert_store),+ patch("backend.copilot.executor.processor.EXPERT_SETUP_WAIT_SECONDS", 0.05),+ patch("backend.copilot.executor.processor.EXPERT_SETUP_POLL_SECONDS", 0.005),+ ):+ await _wait_for_expert_setup(session)++ reads = expert_store.expert_setup_status.await_count+ if expected_reads is None:+ assert 1 < reads < len(statuses)+ else:+ assert reads == expected_reads++@pytest.mark.asyncioasync def test_autopilot_session_skips_expert_tenancy_normalization() -> None:published = _TrackedStream(events=[])diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py b/autogpt_platform/backend/backend/copilot/tools/expert_change_test.pyindex 0c1633e79d..114ce3dff6 100644--- a/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/expert_change_test.py@@ -140,8 +140,7 @@ def _env(db.count_raised_experts = AsyncMock(return_value=raised_count)db.hire_expert = AsyncMock(side_effect=hire_error,- return_value=hire_result- or SimpleNamespace(expert=_created(), failed_preloads=[]),+ return_value=hire_result or SimpleNamespace(expert=_created()),)db.create_raised_expert = AsyncMock(side_effect=raise_error,@@ -855,32 +854,9 @@ class TestApplyProposalDispatch:db.hire_expert.assert_not_called()-class TestPartialHire:- """A hire whose workflows failed to install leaves an expert that cannot- do part of its job — the message must say so, or the user only finds out- when the work silently doesn't happen."""-- @pytest.mark.asyncio(loop_scope="session")- async def test_failed_workflows_are_named_in_the_message(self):- partial = SimpleNamespace(- expert=_created(),- failed_preloads=["Inbox triage", "Daily digest"],- )- with _env(hire_result=partial):- session = make_session(_USER)- preview = await _hire(session, template_id="tpl-scout")- assert isinstance(preview, ExpertChangeProposedResponse)- resp = await _confirm(- _approve(session), confirmation_id=preview.confirmation_id- )- assert isinstance(resp, ExpertChangeAppliedResponse)- assert resp.failed_workflows == ["Inbox triage", "Daily digest"]- assert "Inbox triage" in resp.message- assert "Daily digest" in resp.message- assert "is hired and on the team." not in resp.message-+class TestHireMessage:@pytest.mark.asyncio(loop_scope="session")- async def test_a_clean_hire_still_reads_as_a_clean_hire(self):+ async def test_hire_says_setup_continues_in_the_background(self):with _env():session = make_session(_USER)preview = await _hire(session, template_id="tpl-scout")@@ -889,8 +865,8 @@ class TestPartialHire:_approve(session), confirmation_id=preview.confirmation_id)assert isinstance(resp, ExpertChangeAppliedResponse)- assert resp.failed_workflows == []- assert "could not be installed" not in resp.message+ assert "is hired and on the team" in resp.message+ assert "background" in resp.messageclass TestLegacySessionsCannotStaff:diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py b/autogpt_platform/backend/backend/copilot/tools/expert_proposal.pyindex b97586cf81..b3bc56da96 100644--- a/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py+++ b/autogpt_platform/backend/backend/copilot/tools/expert_proposal.py@@ -343,29 +343,17 @@ async def _apply_hire(except Exception as e:return _hire_failure_response(e, session_id)return ExpertChangeAppliedResponse(- message=_hire_message(result.expert.name, result.failed_preloads),+ message=_hire_message(result.expert.name),session_id=session_id,kind="hire",expert=_summary(result.expert),- failed_workflows=result.failed_preloads,)-def _hire_message(name: str, failed_workflows: list[str]) -> str:- """A hire whose workflows didn't install must not read as a clean one.-- The expert exists either way, but until the listed workflows are added- back it cannot do the part of the job they carried — so the model is- told to name them rather than announce an unqualified success.- """- if not failed_workflows:- return f"{name} is hired and on the team. Tell the user who joined and what they own."- workflows = ", ".join(failed_workflows)+def _hire_message(name: str) -> str:return (- f"{name} joined the team, but {len(failed_workflows)} of their "- f"workflows could not be installed: {workflows}. Tell the user who "- "joined, name the workflows that failed, and say those need to be "- f"added from {name}'s team page before that part of the job can run."+ f"{name} is hired and on the team; their workflows and skills finish "+ "installing in the background. Tell the user who joined and what they own.")diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.pyindex 4e92ccd475..cbbd06cefa 100644--- a/autogpt_platform/backend/backend/data/db_manager.py+++ b/autogpt_platform/backend/backend/data/db_manager.py@@ -571,6 +571,7 @@ class DatabaseManager(AppService):# preview step uses to refuse a change that could never land.list_templates = _(experts_db.list_templates)hire_expert = _(experts_db.hire_expert)+ expert_setup_status = _(experts_db.expert_setup_status)create_raised_expert = _(experts_db.create_raised_expert)count_active_experts = _(experts_db.count_active_experts)count_raised_experts = _(experts_db.count_raised_experts)@@ -977,6 +978,7 @@ class DatabaseManagerAsyncClient(AppServiceClient):list_expert_credentials = d.list_expert_credentialslist_templates = d.list_templateshire_expert = d.hire_expert+ expert_setup_status = d.expert_setup_statuscreate_raised_expert = d.create_raised_expertcount_active_experts = d.count_active_expertscount_raised_experts = d.count_raised_expertsdiff --git a/autogpt_platform/backend/migrations/20260923190000_add_expert_setup_status/migration.sql b/autogpt_platform/backend/migrations/20260923190000_add_expert_setup_status/migration.sqlnew file mode 100644index 0000000000..b5869f328f--- /dev/null+++ b/autogpt_platform/backend/migrations/20260923190000_add_expert_setup_status/migration.sql@@ -0,0 +1,7 @@+-- CreateEnum+CREATE TYPE "ExpertSetupStatus" AS ENUM ('INSTALLING', 'READY', 'FAILED');++-- AlterTable+ALTER TABLE "Expert" ADD COLUMN "setupFailures" TEXT[] DEFAULT ARRAY[]::TEXT[],+ADD COLUMN "setupStartedAt" TIMESTAMP(3),+ADD COLUMN "setupStatus" "ExpertSetupStatus" NOT NULL DEFAULT 'READY';diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 0ff8778010..202c72e466 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -1150,6 +1150,13 @@ model Expert {// identical to a fresh expert and re-seed the grant straight back.credentialsSeededAt DateTime?+ // A hire returns once its row exists; its preloads, skills and routines are+ // installed after the response. setupStartedAt is the running job's lease.+ setupStatus ExpertSetupStatus @default(READY)+ setupStartedAt DateTime?+ // What the last setup run could not install, by display name.+ setupFailures String[] @default([])+Workflows ExpertWorkflow[]Credentials ExpertCredential[]ChatSessions ChatSession[]@@ -1209,6 +1216,12 @@ model ExpertWorkflow {@@index([expertId, createdAt, id])}+enum ExpertSetupStatus {+ INSTALLING+ READY+ FAILED+}+// Where a routine's turn lands each time it fires.enum ExpertRoutineSource {// Shipped by a roster or a shared package. Its prompt was written by someonediff --git a/autogpt_platform/backend/snapshots/expert_pod_assign b/autogpt_platform/backend/snapshots/expert_pod_assignindex 9bccc3ea6d..79e5586319 100644--- a/autogpt_platform/backend/snapshots/expert_pod_assign+++ b/autogpt_platform/backend/snapshots/expert_pod_assign@@ -22,6 +22,8 @@],"role": "Marketing Specialist","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": "template-1","tagline": "Grows your audience",diff --git a/autogpt_platform/backend/snapshots/expert_raise_attachment_installation_failure b/autogpt_platform/backend/snapshots/expert_raise_attachment_installation_failureindex b314d4336e..074442795c 100644--- a/autogpt_platform/backend/snapshots/expert_raise_attachment_installation_failure+++ b/autogpt_platform/backend/snapshots/expert_raise_attachment_installation_failure@@ -23,6 +23,8 @@],"role": "","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": null,"tagline": null,diff --git a/autogpt_platform/backend/snapshots/expert_raise_attachments b/autogpt_platform/backend/snapshots/expert_raise_attachmentsindex 46dc2aadee..99cac5a8d6 100644--- a/autogpt_platform/backend/snapshots/expert_raise_attachments+++ b/autogpt_platform/backend/snapshots/expert_raise_attachments@@ -23,6 +23,8 @@],"role": "Research Assistant","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": null,"tagline": null,diff --git a/autogpt_platform/backend/snapshots/expert_raise_default b/autogpt_platform/backend/snapshots/expert_raise_defaultindex e0b4797792..bf1f1b45c7 100644--- a/autogpt_platform/backend/snapshots/expert_raise_default+++ b/autogpt_platform/backend/snapshots/expert_raise_default@@ -23,6 +23,8 @@],"role": "","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": null,"tagline": null,diff --git a/autogpt_platform/backend/snapshots/expert_soul_update b/autogpt_platform/backend/snapshots/expert_soul_updateindex fc22f18700..e471399652 100644--- a/autogpt_platform/backend/snapshots/expert_soul_update+++ b/autogpt_platform/backend/snapshots/expert_soul_update@@ -22,6 +22,8 @@],"role": "Marketing Specialist","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": "template-1","tagline": "Grows your audience",diff --git a/autogpt_platform/backend/snapshots/expert_templates_list b/autogpt_platform/backend/snapshots/expert_templates_listindex f098e91666..412106b1bd 100644--- a/autogpt_platform/backend/snapshots/expert_templates_list+++ b/autogpt_platform/backend/snapshots/expert_templates_list@@ -30,6 +30,8 @@],"role": "Marketing Specialist","schedules_paused_at": null,+ "setup_failures": [],+ "setup_status": "ready","skills": [],"source_template_id": null,"tagline": "Grows your audience",diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsxindex c53c714c94..e80aee049e 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/HireStep/__tests__/HireStep.test.tsx@@ -163,7 +163,6 @@ describe("HireStep — hiring", () => {hires.push(await request.json());return HttpResponse.json({expert: { id: "exp-1", name: "Maria" },- failed_preloads: [],});}),);diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsxindex 7745df9dea..039e05ecfc 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/experts/__tests__/expert-page.test.tsx@@ -186,7 +186,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaTemplate]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),);renderPage();@@ -218,7 +218,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaTemplate]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),);renderPage();@@ -502,7 +502,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaWithSamples]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),getUpdateExpertSoulMockHandler(async (info) => {const body = (await info.request.json()) as {voice_preferences: string;@@ -534,7 +534,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaWithSamples]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),getUpdateExpertSoulMockHandler(() => {soulPatched = true;return hiredMaria;@@ -560,7 +560,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaWithSamples]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),http.patch("/api/proxy/api/experts/:expertId/soul", () => {patchAttempts += 1;return patchAttempts === 1@@ -598,7 +598,7 @@ describe("Marketplace expert page", () => {server.use(getListExpertTemplatesMockHandler([mariaWithSamples]),getListExpertsMockHandler([]),- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),);renderPage();diff --git a/autogpt_platform/frontend/src/app/(platform)/team/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/team/__tests__/main.test.tsxindex 69341fefd3..7522029d54 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/__tests__/main.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/team/__tests__/main.test.tsx@@ -1412,4 +1412,77 @@ describe("TeamPage - setup needed card", () => {expect(within(dialog).getByRole("button", { name: "Back" })).toBeDefined();expect(within(dialog).getAllByText(/Notion/).length).toBeGreaterThan(0);});++ test("follows a fresh hire's setup until its skills land", async () => {+ let reads = 0;+ server.use(+ getListExpertsMockHandler(() => {+ reads += 1;+ return reads === 1+ ? [{ ...hiredMaria, setup_status: "installing", skills: [] }]+ : [{ ...hiredMaria, setup_status: "ready", skills: ["a", "b"] }];+ }),+ );++ render(<TeamPage />);++ expect(+ await screen.findByText("Setting up Maria's skills and workflows…"),+ ).toBeDefined();+ const card = screen.getByRole("link", { name: "View Maria" });+ await waitFor(() => expect(getStatValue(card, "Skills")).toBe("2"), {+ timeout: 5_000,+ });+ expect(+ screen.queryByText("Setting up Maria's skills and workflows…"),+ ).toBeNull();+ });++ test("a failed setup offers a retry that re-hires the template", async () => {+ const hires: unknown[] = [];+ server.use(+ getListExpertsMockHandler([+ {+ ...hiredMaria,+ source_template_id: "template-maria",+ setup_status: "failed",+ setup_failures: ["SEO Audit"],+ },+ ]),+ http.post("/api/proxy/api/experts", async ({ request }) => {+ hires.push(await request.json());+ return HttpResponse.json({ expert: hiredMaria });+ }),+ );++ render(<TeamPage />);++ expect(+ await screen.findByText("Couldn't install: SEO Audit"),+ ).toBeDefined();+ await userEvent.click(screen.getByRole("button", { name: "Retry setup" }));+ await waitFor(() =>+ expect(hires).toEqual([{ template_id: "template-maria" }]),+ );+ });++ test("a failed setup with no template offers no retry", async () => {+ server.use(+ getListExpertsMockHandler([+ {+ ...hiredMaria,+ source_template_id: null,+ setup_status: "failed",+ setup_failures: ["SEO Audit"],+ },+ ]),+ );++ render(<TeamPage />);++ expect(+ await screen.findByText("Couldn't install: SEO Audit"),+ ).toBeDefined();+ expect(screen.queryByRole("button", { name: "Retry setup" })).toBeNull();+ });});diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/ExpertTeamCard.tsx b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/ExpertTeamCard.tsxindex 4c5a5e1e52..c0bf0be463 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/ExpertTeamCard.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/ExpertTeamCard.tsx@@ -19,6 +19,7 @@ import { MouseEvent } from "react";import { ExpertCover } from "./components/ExpertCover";import { IntegrationIcons } from "./components/IntegrationIcons";+import { SetupStatus } from "./components/SetupStatus";import { SpendMeter } from "./components/SpendMeter";import {@@ -49,8 +50,15 @@ export function ExpertTeamCard({const rosterStatus = getExpertRosterStatus(expert);const weeklySpend = getWeeklySpend(expert);const cover = getExpertCover(expert);- const { handleResume, isResuming, isFireOpen, openFire, closeFire } =- useExpertTeamCard(expert.id);+ const {+ handleResume,+ isResuming,+ retrySetup,+ isRetryingSetup,+ isFireOpen,+ openFire,+ closeFire,+ } = useExpertTeamCard(expert);const isPaused = Boolean(expert.schedules_paused_at);function handleInstallClick() {@@ -165,6 +173,12 @@ export function ExpertTeamCard({</div></Link>+ <SetupStatus+ expert={expert}+ isRetrying={isRetryingSetup}+ onRetry={retrySetup}+ />+{isPaused ? (<div className="mx-4 mb-3 flex items-center justify-between gap-2 rounded-lg bg-amber-50 px-3 py-2 ring-1 ring-inset ring-amber-200"><Text variant="body" className="text-amber-700">diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/components/SetupStatus.tsx b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/components/SetupStatus.tsxnew file mode 100644index 0000000000..3e30fd3e1f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/components/SetupStatus.tsx@@ -0,0 +1,48 @@+import { Expert } from "@/app/api/__generated__/models/expert";+import { Button } from "@/components/atoms/Button/Button";+import { Text } from "@/components/atoms/Text/Text";++interface Props {+ expert: Expert;+ isRetrying: boolean;+ onRetry: () => void;+}++export function SetupStatus({ expert, isRetrying, onRetry }: Props) {+ if (expert.setup_status === "installing") {+ return (+ <div+ role="status"+ className="mx-4 mb-3 rounded-lg bg-sky-50 px-3 py-2 ring-1 ring-inset ring-sky-200"+ >+ <Text variant="body" className="text-sky-800">+ Setting up {expert.name}'s skills and workflows…+ </Text>+ </div>+ );+ }+ if (expert.setup_status !== "failed") return null;+ const failures = expert.setup_failures ?? [];+ return (+ <div+ role="alert"+ className="mx-4 mb-3 flex items-center justify-between gap-2 rounded-lg bg-amber-50 px-3 py-2 ring-1 ring-inset ring-amber-200"+ >+ <Text variant="body" className="text-amber-700">+ {failures.length+ ? `Couldn't install: ${failures.join(", ")}`+ : "Setup didn't finish"}+ </Text>+ {expert.source_template_id ? (+ <Button+ variant="secondary"+ size="small"+ loading={isRetrying}+ onClick={onRetry}+ >+ Retry setup+ </Button>+ ) : null}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/useExpertTeamCard.ts b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/useExpertTeamCard.tsindex a5e8e8ab4f..22ca97d5c4 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/useExpertTeamCard.ts+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertTeamCard/useExpertTeamCard.ts@@ -1,12 +1,15 @@import {getListExpertsQueryKey,+ useHireExpert,useResumeExpertSchedules,} from "@/app/api/__generated__/endpoints/experts/experts";+import { Expert } from "@/app/api/__generated__/models/expert";import { useToast } from "@/components/molecules/Toast/use-toast";import { useQueryClient } from "@tanstack/react-query";import { useState } from "react";-export function useExpertTeamCard(expertId: string) {+export function useExpertTeamCard(expert: Expert) {+ const expertId = expert.id;const queryClient = useQueryClient();const { toast } = useToast();const [isFireOpen, setIsFireOpen] = useState(false);@@ -27,6 +30,23 @@ export function useExpertTeamCard(expertId: string) {},});+ // Re-hiring the template is idempotent and restarts a failed setup.+ const { mutate: rehire, isPending: isRetryingSetup } = useHireExpert({+ mutation: {+ onSuccess: () => {+ queryClient.invalidateQueries({ queryKey: getListExpertsQueryKey() });+ },+ onError: () => {+ toast({ title: "Could not retry setup", variant: "destructive" });+ },+ },+ });++ function retrySetup() {+ if (!expert.source_template_id) return;+ rehire({ data: { template_id: expert.source_template_id } });+ }+function handleResume() {resumeSchedules({ expertId });}@@ -42,6 +62,8 @@ export function useExpertTeamCard(expertId: string) {return {handleResume,isResuming,+ retrySetup,+ isRetryingSetup,isFireOpen,openFire,closeFire,diff --git a/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/team/helpers.tsindex 3e081e43f5..ae82d5301f 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts@@ -414,3 +414,9 @@ export function filterExpertSchedules(return untilNext > 7 * DAY_MS;});}++export const SETUP_POLL_MS = 2_000;++export function isSettingUp(experts: Expert[]) {+ return experts.some((expert) => expert.setup_status === "installing");+}diff --git a/autogpt_platform/frontend/src/app/(platform)/team/useTeamPage.ts b/autogpt_platform/frontend/src/app/(platform)/team/useTeamPage.tsindex 99dbeecc96..dfc800aab8 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/useTeamPage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/team/useTeamPage.ts@@ -8,7 +8,12 @@ import {ChatTarget,expertToChatTarget,} from "./components/ExpertChatDrawer/helpers";-import { getExpertSchedules, getHiredExperts } from "./helpers";+import {+ getExpertSchedules,+ getHiredExperts,+ isSettingUp,+ SETUP_POLL_MS,+} from "./helpers";interface Args {enabled: boolean;@@ -22,7 +27,15 @@ export function useTeamPage({ enabled }: Args) {const [chatDrawerKey, setChatDrawerKey] = useState(0);const expertsQuery = useListExperts({- query: { select: (res) => (okData(res) ?? []) as Expert[], enabled },+ query: {+ select: (res) => (okData(res) ?? []) as Expert[],+ enabled,+ // A fresh hire's skills land after the hire returns; follow them in.+ refetchInterval: (query) =>+ isSettingUp((okData(query.state.data) ?? []) as Expert[])+ ? SETUP_POLL_MS+ : false,+ },});const schedulesQuery = useGetV1ListExecutionSchedulesForAUser({query: { select: (res) => okData(res) ?? [], enabled },diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 15ea5d24e7..438027682f 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -21635,6 +21635,18 @@"pod_id": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Pod Id"+ },+ "setup_status": {+ "type": "string",+ "enum": ["installing", "ready", "failed"],+ "title": "Setup Status",+ "default": "ready"+ },+ "setup_failures": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Setup Failures",+ "default": []}},"type": "object",@@ -22200,6 +22212,18 @@"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Pod Id"},+ "setup_status": {+ "type": "string",+ "enum": ["installing", "ready", "failed"],+ "title": "Setup Status",+ "default": "ready"+ },+ "setup_failures": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Setup Failures",+ "default": []+ },"bundled_skills": {"items": { "$ref": "#/components/schemas/ExpertBundledSkill" },"type": "array",@@ -23575,16 +23599,9 @@"title": "HireRequest"},"HireResult": {- "properties": {- "expert": { "$ref": "#/components/schemas/Expert" },- "failed_preloads": {- "items": { "type": "string" },- "type": "array",- "title": "Failed Preloads"- }- },+ "properties": { "expert": { "$ref": "#/components/schemas/Expert" } },"type": "object",- "required": ["expert", "failed_preloads"],+ "required": ["expert"],"title": "HireResult"},"HomeAction": {diff --git a/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx b/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsxindex e572f02db0..30daa917a1 100644--- a/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx+++ b/autogpt_platform/frontend/src/services/experts/__tests__/useHireFlow.test.tsx@@ -104,7 +104,7 @@ describe("useHireFlow", () => {pushMock.mockReset();window.sessionStorage.clear();server.use(- getHireExpertMockHandler({ expert: hiredMaria, failed_preloads: [] }),+ getHireExpertMockHandler({ expert: hiredMaria }),getUpdateExpertSoulMockHandler(hiredMaria),);});diff --git a/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx b/autogpt_platform/frontend/src/services/experts/useHireFlow.tsxindex b494f5c191..86b879c9d9 100644--- a/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx+++ b/autogpt_platform/frontend/src/services/experts/useHireFlow.tsx@@ -22,9 +22,10 @@ import { useRef, useState } from "react";function celebrate(result: HireResult) {toast({title: `${result.expert.name} joined your team`,- description: result.failed_preloads.length- ? `Couldn't attach: ${result.failed_preloads.join(", ")}`- : undefined,+ description:+ result.expert.setup_status === "installing"+ ? "Their skills and workflows are installing."+ : undefined,variant: "success",action: (<Button