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): stop update_skills deleting skills the client never saw (#14884)
Bentlyc7bbae78647d
diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db.py b/autogpt_platform/backend/backend/api/features/experts/experts_db.pyindex a41a2824eb..19e20e96f7 100644--- a/autogpt_platform/backend/backend/api/features/experts/experts_db.py+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db.py@@ -1444,14 +1444,18 @@ async def update_skills(expert_id: str,skills: list[str],marketplace_listing_ids: list[str] | None = None,+ remove: list[str] | None = None,) -> Expert:- """Replace an expert's skill list.+ """Attach ``skills`` to an expert and remove the names in ``remove``.Names the expert does not already carry must resolve to a library skill.A personal-Otto skill is copied into the expert's own folder so the- expert owns it from then on; names dropped from the list delete the- expert's copy. The stored name is the skill's canonical one so display- and lookup agree."""+ expert owns it from then on; a removed name deletes the expert's copy.+ The stored name is the skill's canonical one so display and lookup agree.++ Only an explicit ``remove`` deletes anything. The expert distils new+ skills into its own folder at any time, so a list the client read earlier+ can be missing one; treating an absent name as a removal destroyed it."""row = await prisma.models.Expert.prisma().find_first(where={"id": expert_id,@@ -1477,6 +1481,16 @@ async def update_skills(await _resolve_marketplace_skill_name(listing_id)for listing_id in marketplace_listing_ids or []]+ removed = {skill_name_key(name) for name in remove or []}+ # A listing resolves to a name only here, so the request validator could+ # not see this contradiction: attaching a marketplace skill and removing+ # it in the same call would delete the copy and then recreate it. Checked+ # before the copies below so a rejected request writes nothing.+ both = sorted(n for n in marketplace if skill_name_key(n) in removed)+ if both:+ raise ValueError(+ f"Skills cannot be both attached and removed: {', '.join(both)}"+ )resolved: list[str] = []for canonical, folder in plan:if folder is not None:@@ -1495,8 +1509,7 @@ async def update_skills(for name in marketplace:if name.lower() not in {r.lower() for r in resolved}:resolved.append(name)- kept = {r.lower() for r in resolved}- for dropped in [name for name in current.values() if name.lower() not in kept]:+ for dropped in [n for n in current.values() if skill_name_key(n) in removed]:# delete_user_skill drops the row name itself — except for a built-in,# where it raises first and _detach_expert_skill swallows that.await _detach_expert_skill(user_id, expert_id, dropped)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 bf307753b6..b9ebbf95a9 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@@ -5677,6 +5677,39 @@ async def test_update_skills_resolves_every_name_before_copying(assert copies == [][email protected](loop_scope="session")+async def test_update_skills_rejects_attach_and_remove_before_copying(+ server: SpinTestServer, test_user, monkeypatch+):+ copies: list[str] = []++ async def _find(user_id, names):+ return {n.strip().lower(): n for n in names}++ async def _copy(user_id, expert_id, name):+ copies.append(name)+ return name++ async def _listing_name(listing_id):+ return "Deep Research"++ monkeypatch.setattr(experts_db, "find_user_skill_slugs", _find)+ monkeypatch.setattr(experts_db, "copy_skill_to_expert", _copy)+ monkeypatch.setattr(experts_db, "_resolve_marketplace_skill_name", _listing_name)+ template = await _seed_template(name="Maria", preload_listings=[])+ hired = await experts_db.hire_expert(test_user.id, template.id, None)++ with pytest.raises(ValueError, match="both attached and removed"):+ await experts_db.update_skills(+ test_user.id,+ hired.expert.id,+ ["valid"],+ marketplace_listing_ids=["listing-1"],+ remove=["deep_research"],+ )+ assert copies == []++@pytest.mark.asyncio(loop_scope="session")async def test_update_skills_keeps_the_display_name_of_a_skill_already_carried(server: SpinTestServer, test_user, monkeypatch@@ -5826,12 +5859,56 @@ async def test_update_skills_dropping_a_skill_removes_the_copy_and_the_row_name("backend.copilot.tools.skills._get_user_skill_manager",new=AsyncMock(return_value=fake),):- updated = await experts_db.update_skills(test_user.id, expert_id, [])+ updated = await experts_db.update_skills(+ test_user.id, expert_id, [], remove=[name]+ )assert copy not in fake.filesassert name not in {s.lower() for s in updated.skills}[email protected](loop_scope="session")+async def test_update_skills_from_a_stale_list_keeps_a_skill_the_client_never_saw(+ server: SpinTestServer, test_user, monkeypatch+):+ """SECRT-2693: the expert distils a skill after the client read its list;+ saving that stale list must not delete the new skill, and only a name the+ client asks to remove is removed."""+ monkeypatch.setattr(experts_db, "find_user_skill_slugs", _no_library_skills)+ template = await _seed_template(name="Maria", preload_listings=[])+ hired = await experts_db.hire_expert(test_user.id, template.id, None)+ expert_id = hired.expert.id+ await experts_db.add_expert_skill_name(test_user.id, expert_id, "kept")+ fake = _FakeWorkspaceManager()+ fresh = f"/experts/{expert_id}/skills/fresh-skill/SKILL.md"+ fake.files[fresh] = b"---\nname: fresh-skill\ndescription: d\n---\nsteps\n"+ kept = f"/experts/{expert_id}/skills/kept/SKILL.md"+ fake.files[kept] = b"---\nname: kept\ndescription: d\n---\nsteps\n"+ await experts_db.add_expert_skill_name(test_user.id, expert_id, "fresh-skill")++ with patch(+ "backend.copilot.tools.skills._get_user_skill_manager",+ new=AsyncMock(return_value=fake),+ ):+ stale = await experts_db.update_skills(test_user.id, expert_id, ["kept"])+ assert fresh in fake.files+ assert {"kept", "fresh-skill"} <= {s.lower() for s in stale.skills}++ removed = await experts_db.update_skills(+ test_user.id, expert_id, [], remove=["kept"]+ )++ assert kept not in fake.files+ assert fresh in fake.files+ names = {s.lower() for s in removed.skills}+ assert "kept" not in names+ assert "fresh-skill" in names+++async def _no_library_skills(_user_id, _names):+ return {}++@pytest.mark.asyncio(loop_scope="session")async def test_expert_skill_names_treat_a_display_name_and_its_slug_as_one(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 e17269b9f0..3843d0bf26 100644--- a/autogpt_platform/backend/backend/api/features/experts/models.py+++ b/autogpt_platform/backend/backend/api/features/experts/models.py@@ -3,11 +3,18 @@ from datetime import date, datetimefrom typing import Annotated, Literalfrom urllib.parse import urlparse-from pydantic import BaseModel, Field, TypeAdapter, ValidationError, field_validator+from pydantic import (+ BaseModel,+ Field,+ TypeAdapter,+ ValidationError,+ field_validator,+ model_validator,+)from backend.api.features.experts.avatar_catalog import resolve_avatar_urlfrom backend.data.expert_run_output import OutputType-from backend.data.skill_capacity import MAX_SKILLS_PER_EXPERT+from backend.data.skill_capacity import MAX_SKILLS_PER_EXPERT, skill_name_keyExpertRunStatus = Literal["incomplete",@@ -446,16 +453,39 @@ class RaiseResult(BaseModel):class ExpertSkillsUpdate(BaseModel):- """The full list of skill names an expert should carry. Names new to the- expert must be library skills (default or uploaded); names already on- the expert are kept as-is so marketplace skills survive a round-trip."""+ """Skills to attach to an expert and skills to remove from it. Names new+ to the expert must be library skills (default or uploaded); names already+ on the expert are kept as-is so marketplace skills survive a round-trip.++ Only names listed in ``remove`` are removed. A skill the expert carries+ that appears in neither list is left alone, so a client holding a stale+ list can never delete a skill it has not seen (the expert can distil new+ ones at any time).++ A normalized name must not appear in both ``skills`` and ``remove``;+ such a request is rejected with a validation error. A marketplace listing+ whose name matches a ``remove`` entry is rejected with a 400."""- skills: list[str] = Field(max_length=MAX_SKILLS_PER_EXPERT)+ skills: list[str] = Field(default_factory=list, max_length=MAX_SKILLS_PER_EXPERT)+ remove: list[str] = Field(default_factory=list, max_length=MAX_SKILLS_PER_EXPERT)# Store listing versions to attach as marketplace skills; each resolves# to the listing's public name, the same way the raise flow records them.marketplace_listing_ids: list[str] = Field(default_factory=list, max_length=20)- @field_validator("skills", mode="before")+ @model_validator(mode="after")+ def reject_names_both_kept_and_removed(self) -> "ExpertSkillsUpdate":+ # The same key update_skills removes by, so a name cannot pass this+ # check as "different" and then match a removal on spelling alone.+ both = {skill_name_key(n) for n in self.skills} & {+ skill_name_key(n) for n in self.remove+ }+ if both:+ raise ValueError(+ f"Skills cannot be both added and removed: {', '.join(sorted(both))}"+ )+ return self++ @field_validator("skills", "remove", mode="before")@classmethoddef strip_and_dedupe(cls, value: object) -> object:if not isinstance(value, list):@@ -468,9 +498,12 @@ class ExpertSkillsUpdate(BaseModel):name = item.strip()if not name or len(name) > 100:raise ValueError("Skill names must be 1-100 characters")- if name.lower() in seen:+ # Same key the overlap check and update_skills use, so+ # "Deep Research" and "deep_research" count as one skill.+ key = skill_name_key(name)+ if key in seen:continue- seen.add(name.lower())+ seen.add(key)cleaned.append(name)return cleaneddiff --git a/autogpt_platform/backend/backend/api/features/experts/routes.py b/autogpt_platform/backend/backend/api/features/experts/routes.pyindex 64816e8197..97268d5e9e 100644--- a/autogpt_platform/backend/backend/api/features/experts/routes.py+++ b/autogpt_platform/backend/backend/api/features/experts/routes.py@@ -530,7 +530,10 @@ async def update_expert_soul(@router.put("/{expert_id}/skills",operation_id="update_expert_skills",- responses={404: {"description": "Expert or skill not found"}},+ responses={+ 400: {"description": "A marketplace skill is both attached and removed"},+ 404: {"description": "Expert or skill not found"},+ },)async def update_expert_skills(expert_id: str,@@ -543,9 +546,14 @@ async def update_expert_skills(expert_id,request.skills,marketplace_listing_ids=request.marketplace_listing_ids,+ remove=request.remove,)except NotFoundError as e:raise fastapi.HTTPException(status_code=404, detail=str(e))+ except ValueError as e:+ # A listing resolves to its name only inside update_skills, so this+ # attach-and-remove contradiction can't be caught by the request model.+ raise fastapi.HTTPException(status_code=400, detail=str(e))@router.patch(diff --git a/autogpt_platform/backend/backend/api/features/experts/routes_test.py b/autogpt_platform/backend/backend/api/features/experts/routes_test.pyindex 8195661332..8b7f8e0ff8 100644--- a/autogpt_platform/backend/backend/api/features/experts/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/experts/routes_test.py@@ -1072,10 +1072,111 @@ def test_update_expert_skills_replaces_the_list(assert response.status_code == 200assert response.json()["skills"] == ["Deep Research", "SEO"]mock_update.assert_awaited_once_with(- test_user_id, "expert-1", ["Deep Research", "SEO"], marketplace_listing_ids=[]+ test_user_id,+ "expert-1",+ ["Deep Research", "SEO"],+ marketplace_listing_ids=[],+ remove=[],+ )+++def test_update_expert_skills_passes_explicit_removals_through(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ mock_update = mocker.patch(+ "backend.api.features.experts.routes.experts_db.update_skills",+ new_callable=AsyncMock,+ return_value=_make_expert(name="Maria", skills=[]),+ )++ response = client.put("/experts/expert-1/skills", json={"remove": [" SEO ", "seo"]})++ assert response.status_code == 200+ mock_update.assert_awaited_once_with(+ test_user_id, "expert-1", [], marketplace_listing_ids=[], remove=["SEO"])+def test_update_expert_skills_dedupes_names_by_skill_key(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ mock_update = mocker.patch(+ "backend.api.features.experts.routes.experts_db.update_skills",+ new_callable=AsyncMock,+ return_value=_make_expert(name="Maria", skills=[]),+ )++ response = client.put(+ "/experts/expert-1/skills",+ json={"remove": ["Deep Research", "deep_research", "deep-research"]},+ )++ assert response.status_code == 200+ mock_update.assert_awaited_once_with(+ test_user_id,+ "expert-1",+ [],+ marketplace_listing_ids=[],+ remove=["Deep Research"],+ )+++def test_update_expert_skills_returns_400_when_a_listing_is_also_removed(+ mocker: pytest_mock.MockerFixture,+) -> None:+ mocker.patch(+ "backend.api.features.experts.routes.experts_db.update_skills",+ new_callable=AsyncMock,+ side_effect=ValueError("Skills cannot be both attached and removed: SEO"),+ )++ response = client.put(+ "/experts/expert-1/skills",+ json={"marketplace_listing_ids": ["listing-1"], "remove": ["SEO"]},+ )++ assert response.status_code == 400+ assert "both attached and removed" in response.json()["detail"]+++def test_update_expert_skills_rejects_a_name_both_added_and_removed(+ mocker: pytest_mock.MockerFixture,+) -> None:+ mock_update = mocker.patch(+ "backend.api.features.experts.routes.experts_db.update_skills",+ new_callable=AsyncMock,+ )++ response = client.put(+ "/experts/expert-1/skills", json={"skills": ["SEO"], "remove": ["seo"]}+ )++ assert response.status_code == 422+ mock_update.assert_not_awaited()+++def test_update_expert_skills_rejects_a_contradiction_spelled_two_ways(+ mocker: pytest_mock.MockerFixture,+) -> None:+ """The removal loop matches names by skill_name_key (case, spaces,+ underscores and hyphens folded), so the contradiction check must too, or+ "Deep Research" + remove "deep_research" would pass and then be removed."""+ mock_update = mocker.patch(+ "backend.api.features.experts.routes.experts_db.update_skills",+ new_callable=AsyncMock,+ )++ response = client.put(+ "/experts/expert-1/skills",+ json={"skills": ["Deep Research"], "remove": ["deep_research"]},+ )++ assert response.status_code == 422+ mock_update.assert_not_awaited()++def test_update_expert_skills_unknown_skill_returns_404(mocker: pytest_mock.MockerFixture,) -> None:diff --git a/autogpt_platform/backend/backend/copilot/tools/skills.py b/autogpt_platform/backend/backend/copilot/tools/skills.pyindex 7601d601fc..9e988dc209 100644--- a/autogpt_platform/backend/backend/copilot/tools/skills.py+++ b/autogpt_platform/backend/backend/copilot/tools/skills.py@@ -49,6 +49,7 @@ from backend.data.skill_capacity import SKILL_ORIGIN_USERfrom backend.data.skill_capacity import SKILL_ORIGINS as _SKILL_ORIGINSfrom backend.data.skill_capacity import SkillLimitError, SkillOwnedErrorfrom backend.data.skill_capacity import normalize_skill_origin as _normalize_origin+from backend.data.skill_capacity import skill_name_keyfrom backend.data.skill_package import (SKILL_MD,MergedPackage,@@ -1522,12 +1523,6 @@ async def _copy_assigned_skills_not_yet_owned(return copied-def skill_name_key(name: str) -> str:- """Compare key for skill names: the row may carry a display name ("Deep- Research") for the skill whose folder is ``deep-research``."""- return re.sub(r"[\s_-]+", "-", name.strip().lower())--def _heal_backoff_key(user_id: str, expert_id: str) -> str:return SKILLS_HEAL_BACKOFF_KEY.format(user_id=user_id, expert_id=expert_id)diff --git a/autogpt_platform/backend/backend/data/skill_capacity.py b/autogpt_platform/backend/backend/data/skill_capacity.pyindex 6e2a563e49..61396cff44 100644--- a/autogpt_platform/backend/backend/data/skill_capacity.py+++ b/autogpt_platform/backend/backend/data/skill_capacity.py@@ -32,3 +32,11 @@ def skill_origin(metadata: dict | None) -> str | None:def skill_owner_folder(path: str) -> str | None:match = _ROOT_PATH.fullmatch(path)return match.group(1) if match else None+++def skill_name_key(name: str) -> str:+ """Compare key for skill names: the row may carry a display name ("Deep+ Research") for the skill whose folder is ``deep-research``. Every place+ that decides whether two names are the same skill uses this one key, so+ a request cannot pass one check and fail another on spelling alone."""+ return re.sub(r"[\s_-]+", "-", name.strip().lower())diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsxindex 06377c0ce9..ffc866f310 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx@@ -27,6 +27,7 @@ import {getGetV1ListExecutionSchedulesForAUserMockHandler,} from "@/app/api/__generated__/endpoints/schedules/schedules.msw";import { Expert } from "@/app/api/__generated__/models/expert";+import { ExpertSkillsUpdate } from "@/app/api/__generated__/models/expertSkillsUpdate";import { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";import {getGetV2GetSessionMockHandler200,@@ -684,7 +685,7 @@ describe("ExpertDetailPage", () => {test("lists the expert's skills with library details and adds one", async () => {const user = userEvent.setup();- const puts: string[][] = [];+ const puts: ExpertSkillsUpdate[] = [];let skills = ["Content strategy"];server.use(getGetExpertMockHandler(() => ({ ...maria, skills })),@@ -697,9 +698,12 @@ describe("ExpertDetailPage", () => {{ name: "Deep Research", description: "Research anything thoroughly" },]),getUpdateExpertSkillsMockHandler200(async ({ request }) => {- const body = (await request.json()) as { skills: string[] };- puts.push(body.skills);- skills = body.skills;+ const body = (await request.json()) as ExpertSkillsUpdate;+ puts.push(body);+ skills = [+ ...skills.filter((name) => !body.remove?.includes(name)),+ ...(body.skills ?? []),+ ];return { ...maria, skills };}),);@@ -719,7 +723,7 @@ describe("ExpertDetailPage", () => {await user.click(within(dialog).getByRole("button", { name: "Add" }));await waitFor(() => {- expect(puts).toEqual([["Content strategy", "Deep Research"]]);+ expect(puts).toEqual([{ skills: ["Deep Research"] }]);});expect(await within(list).findByText("Deep Research")).toBeDefined();});@@ -809,15 +813,18 @@ describe("ExpertDetailPage", () => {test("removes a skill from the expert", async () => {const user = userEvent.setup();- const puts: string[][] = [];+ const puts: ExpertSkillsUpdate[] = [];let skills = ["Content strategy"];server.use(getGetExpertMockHandler(() => ({ ...maria, skills })),getListCopilotSkillsMockHandler200([]),getUpdateExpertSkillsMockHandler200(async ({ request }) => {- const body = (await request.json()) as { skills: string[] };- puts.push(body.skills);- skills = body.skills;+ const body = (await request.json()) as ExpertSkillsUpdate;+ puts.push(body);+ skills = [+ ...skills.filter((name) => !body.remove?.includes(name)),+ ...(body.skills ?? []),+ ];return { ...maria, skills };}),);@@ -829,7 +836,7 @@ describe("ExpertDetailPage", () => {);await waitFor(() => {- expect(puts).toEqual([[]]);+ expect(puts).toEqual([{ remove: ["Content strategy"] }]);});expect(await screen.findByText(/No skills yet/)).toBeDefined();});diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertSkills.ts b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertSkills.tsindex 1195521da8..a2e83e289d 100644--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertSkills.ts+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertSkills.ts@@ -14,6 +14,7 @@ import { MarketplaceSkill } from "@/app/api/__generated__/models/marketplaceSkilimport { useDebouncedValue } from "@/hooks/useDebouncedValue";import { CopilotSkillInfo } from "@/app/api/__generated__/models/copilotSkillInfo";import { Expert } from "@/app/api/__generated__/models/expert";+import { ExpertSkillsUpdate } from "@/app/api/__generated__/models/expertSkillsUpdate";import { okData } from "@/app/api/helpers";import { ApiError } from "@/lib/autogpt-server-api/helpers";import { useToast } from "@/components/molecules/Toast/use-toast";@@ -88,9 +89,9 @@ export function useExpertSkills(expert: Expert) {): attached;- async function save(skills: string[], successTitle: string) {+ async function save(data: ExpertSkillsUpdate, successTitle: string) {try {- await updateSkills({ expertId: expert.id, data: { skills } });+ await updateSkills({ expertId: expert.id, data });await refreshExpert();toast({ title: successTitle, variant: "success" });return true;@@ -105,7 +106,7 @@ export function useExpertSkills(expert: Expert) {}async function addSkill(name: string) {- const saved = await save([...expert.skills, name], `Added ${name}`);+ const saved = await save({ skills: [name] }, `Added ${name}`);if (saved) setIsAddOpen(false);}@@ -143,10 +144,7 @@ export function useExpertSkills(expert: Expert) {}function removeSkill(name: string) {- return save(- expert.skills.filter((skill) => skill !== name),- `Removed ${name}`,- );+ return save({ remove: [name] }, `Removed ${name}`);}return {diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 80dd60f3de..782655882d 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -7610,6 +7610,9 @@}}},+ "400": {+ "description": "A marketplace skill is both attached and removed"+ },"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"},@@ -22331,6 +22334,12 @@"maxItems": 150,"title": "Skills"},+ "remove": {+ "items": { "type": "string" },+ "type": "array",+ "maxItems": 150,+ "title": "Remove"+ },"marketplace_listing_ids": {"items": { "type": "string" },"type": "array",@@ -22339,9 +22348,8 @@}},"type": "object",- "required": ["skills"],"title": "ExpertSkillsUpdate",- "description": "The full list of skill names an expert should carry. Names new to the\nexpert must be library skills (default or uploaded); names already on\nthe expert are kept as-is so marketplace skills survive a round-trip."+ "description": "Skills to attach to an expert and skills to remove from it. Names new\nto the expert must be library skills (default or uploaded); names already\non the expert are kept as-is so marketplace skills survive a round-trip.\n\nOnly names listed in ``remove`` are removed. A skill the expert carries\nthat appears in neither list is left alone, so a client holding a stale\nlist can never delete a skill it has not seen (the expert can distil new\nones at any time).\n\nA normalized name must not appear in both ``skills`` and ``remove``;\nsuch a request is rejected with a validation error. A marketplace listing\nwhose name matches a ``remove`` entry is rejected with a 400."},"ExpertSoulUpdate": {"properties": {