:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(skills): add a command to normalize legacy bundled file paths (#99964)
posthog[bot]4d7b1a92ff05
diff --git a/products/skills/backend/api/test/test_normalize_skill_file_paths.py b/products/skills/backend/api/test/test_normalize_skill_file_paths.pynew file mode 100644index 000000000000..481cf1a53e72--- /dev/null+++ b/products/skills/backend/api/test/test_normalize_skill_file_paths.py@@ -0,0 +1,146 @@+import json+from io import StringIO+from uuid import UUID++from posthog.test.base import BaseTest++from django.core.management import call_command+from django.test import SimpleTestCase++from parameterized import parameterized++from products.skills.backend.management.commands.normalize_skill_file_paths import plan_skill_paths+from products.skills.backend.marketplace.adapters import SkillBundle, build_skill_bundle, build_team_marketplace_tree+from products.skills.backend.marketplace.git_smart_http import synthesize_repo+from products.skills.backend.models import LLMSkill, LLMSkillFile++ROW_A = UUID("0198f000-0000-7000-8000-00000000000a")+ROW_B = UUID("0198f000-0000-7000-8000-00000000000b")+++class TestPlanSkillPaths(SimpleTestCase):+ @parameterized.expand(+ [+ ("separator", [(ROW_A, "refs\\guide.md")], [(ROW_A, "refs\\guide.md", "refs/guide.md")], [], [], False),+ ("already_canonical", [(ROW_A, "refs/guide.md")], [], [], [], False),+ (+ "collision_with_stored_path",+ [(ROW_A, "refs\\guide.md"), (ROW_B, "refs/Guide.md")],+ [],+ [("refs\\guide.md", "refs/Guide.md")],+ [],+ False,+ ),+ (+ "collision_between_rewrites",+ [(ROW_A, "refs\\guide.md"), (ROW_B, "Refs\\Guide.md")],+ [],+ [("Refs\\Guide.md", "refs\\guide.md"), ("refs\\guide.md", "Refs\\Guide.md")],+ [],+ False,+ ),+ ("trailing_slash", [(ROW_A, "refs/")], [], [], ["refs/"], False),+ ("absolute", [(ROW_A, "/refs/guide.md")], [], [], ["/refs/guide.md"], False),+ (+ "rewrite_would_shadow_a_stored_file",+ [(ROW_A, "assets"), (ROW_B, "assets\\logo.png")],+ [],+ [],+ [],+ True,+ ),+ ("rewrite_would_shadow_the_generated_sidecar", [(ROW_A, "Agents\\OpenAI.yaml")], [], [], [], True),+ ]+ )+ def test_plan(self, _name, rows, rewrites, collisions, unfixable, unsafe) -> None:+ plan = plan_skill_paths(rows)++ assert plan.rewrites == rewrites+ assert plan.collisions == collisions+ assert [path for path, _ in plan.unfixable] == unfixable+ assert plan.unsafe == unsafe++ def test_plan_does_not_depend_on_row_order(self) -> None:+ rows = [(ROW_A, "refs\\guide.md"), (ROW_B, "Refs\\Guide.md")]++ assert plan_skill_paths(rows) == plan_skill_paths(list(reversed(rows)))+++class TestNormalizeSkillFilePathsCommand(BaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.skill = LLMSkill.objects.create(+ team=self.team,+ name="legacy-skill",+ description="A skill stored before the path validator.",+ body="# Legacy",+ created_by=self.user,+ )+ self.file = LLMSkillFile.objects.create(skill=self.skill, path="references\\guide.md", content="# Guide")++ def _run(self, *args: str) -> str:+ out = StringIO()+ call_command("normalize_skill_file_paths", *args, stdout=out)+ return out.getvalue()++ def _bundle(self) -> SkillBundle:+ return build_skill_bundle(self.team, self.user, LLMSkill.objects.all(), content="full")++ def _published_version_millis(self) -> int:+ marketplace = build_team_marketplace_tree(self.team)[".claude-plugin/marketplace.json"]+ return int(json.loads(marketplace)["plugins"][0]["version"].rsplit(".", 1)[1])++ def test_dry_run_reports_without_writing(self) -> None:+ output = self._run()++ self.file.refresh_from_db()+ assert self.file.path == "references\\guide.md"+ assert "references/guide.md" in output+ assert f"team_id={self.team.id}" in output+ assert "name='legacy-skill'" in output++ def test_apply_returns_the_skill_to_the_bundle(self) -> None:+ assert self._bundle().included == []++ self._run("--apply")++ self.file.refresh_from_db()+ assert self.file.path == "references/guide.md"+ assert self._bundle().included == ["legacy-skill"]++ def test_apply_advances_the_published_marketplace_version(self) -> None:+ before = self._published_version_millis()++ self._run("--apply")++ assert self._published_version_millis() > before++ def test_dry_run_leaves_the_published_marketplace_version_alone(self) -> None:+ before = self._published_version_millis()++ self._run()++ assert self._published_version_millis() == before++ def test_team_id_scopes_the_rewrite(self) -> None:+ self._run("--apply", "--team-id", str(self.team.id + 1))++ self.file.refresh_from_db()+ assert self.file.path == "references\\guide.md"++ def test_apply_leaves_a_skill_whose_rewrite_would_break_the_clone(self) -> None:+ shadowed = LLMSkill.objects.create(+ team=self.team,+ name="shadowed-skill",+ description="A skill whose rewrite would shadow a stored file.",+ body="# Shadowed",+ created_by=self.user,+ )+ LLMSkillFile.objects.create(skill=shadowed, path="assets", content="not a directory")+ logo = LLMSkillFile.objects.create(skill=shadowed, path="assets\\logo.png", content="png")++ self._run("--apply")++ logo.refresh_from_db()+ assert logo.path == "assets\\logo.png"+ synthesize_repo(build_team_marketplace_tree(self.team), author="a <[email protected]>", message="m")diff --git a/products/skills/backend/management/__init__.py b/products/skills/backend/management/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/skills/backend/management/commands/__init__.py b/products/skills/backend/management/commands/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/skills/backend/management/commands/normalize_skill_file_paths.py b/products/skills/backend/management/commands/normalize_skill_file_paths.pynew file mode 100644index 000000000000..e218a5a250b3--- /dev/null+++ b/products/skills/backend/management/commands/normalize_skill_file_paths.py@@ -0,0 +1,183 @@+from collections import defaultdict+from collections.abc import Iterator+from itertools import groupby+from uuid import UUID++from django.core.management.base import BaseCommand+from django.db import transaction+from django.utils import timezone++from posthog.dataclasses import frozen++from products.skills.backend.api.skill_services import normalize_skill_file_path+from products.skills.backend.marketplace.adapters import bundle_paths_are_safe+from products.skills.backend.models import LLMSkill, LLMSkillFile++READ_CHUNK_SIZE = 1000+++@frozen+class SkillPathPlan:+ rewrites: list[tuple[UUID, str, str]]+ collisions: list[tuple[str, str]]+ unfixable: list[tuple[str, str]]+ unsafe: bool+++@frozen+class SkillFileRows:+ skill_id: UUID+ team_id: int+ name: str+ version: int+ rows: list[tuple[UUID, str]]++ @property+ def label(self) -> str:+ return f"skill id={self.skill_id} team_id={self.team_id} name='{self.name}' version={self.version}"+++def plan_skill_paths(rows: list[tuple[UUID, str]]) -> SkillPathPlan:+ """Decide what to do with one skill's file rows, given `(row id, stored path)` pairs.++ A path that already normalizes to itself stays untouched. Rows are grouped by the destination+ they want rather than walked in arrival order, so the plan is a function of the row set alone:+ `_rows_by_skill` orders by skill id only, which leaves two rows of one skill free to arrive+ either way round. Every row of a group that two rows want is reported, and none of them is+ rewritten — the rows hold different content, so choosing a winner would silently drop one. A+ report names the stored paths of the group, because the shared destination is a path the+ operator cannot look up.++ The rewrites are then kept only if the whole resulting path set passes the same gate the bundle+ uses. A rewrite turns one flat name into a directory, so `assets\\logo.png` beside a file named+ `assets` clones today and fails to clone once it becomes `assets/logo.png`. The gate counts the+ generated SKILL.md and Codex sidecar and rejects those ancestor conflicts, so a skill it turns+ down keeps every stored path and is reported for manual repair.+ """+ unfixable: list[tuple[str, str]] = []+ claimants: dict[str, list[tuple[UUID, str, str]]] = defaultdict(list)+ for row_id, path in rows:+ try:+ canonical = normalize_skill_file_path(path)+ except ValueError as error:+ unfixable.append((path, str(error)))+ continue+ claimants[canonical.lower()].append((row_id, path, canonical))++ rewrites: list[tuple[UUID, str, str]] = []+ collisions: list[tuple[str, str]] = []+ for group in claimants.values():+ if len(group) > 1:+ stored = sorted(path for _, path, _ in group)+ for _, path, canonical in group:+ if canonical != path:+ collisions.extend((path, conflict) for conflict in stored if conflict != path)+ continue+ row_id, path, canonical = group[0]+ if canonical != path:+ rewrites.append((row_id, path, canonical))++ rewrites.sort(key=lambda rewrite: rewrite[1])+ collisions.sort()+ unfixable.sort()+ rewritten = {path: canonical for _, path, canonical in rewrites}+ if rewrites and not bundle_paths_are_safe([rewritten.get(path, path) for _, path in rows]):+ return SkillPathPlan(rewrites=[], collisions=collisions, unfixable=unfixable, unsafe=True)+ return SkillPathPlan(rewrites=rewrites, collisions=collisions, unfixable=unfixable, unsafe=False)+++class Command(BaseCommand):+ help = (+ "Rewrite bundled skill file paths stored before the write-time path validator landed. "+ "A skill that holds one is skipped from the marketplace bundle entirely."+ )++ def add_arguments(self, parser) -> None:+ parser.add_argument("--team-id", type=int, default=None, help="Only process files of this team's skills")+ parser.add_argument("--apply", action="store_true", help="Write the rewrites. Without it, this is a dry run.")++ def handle(self, *args, **options) -> None:+ apply: bool = options["apply"]+ rewritten = collided = unfixable = unsafe = 0++ if not apply:+ self.stdout.write(self.style.WARNING("Dry run — pass --apply to write the rewrites."))++ for skill_files in self._rows_by_skill(options["team_id"]):+ plan = plan_skill_paths(skill_files.rows)+ if plan.unsafe:+ unsafe += 1+ self.stdout.write(+ self.style.WARNING(+ f"{skill_files.label}: left alone — the rewritten paths would still be unsafe to clone"+ )+ )+ for path, conflict in plan.collisions:+ collided += 1+ self.stdout.write(+ self.style.WARNING(f"{skill_files.label}: '{path}' would collide with stored '{conflict}'")+ )+ for path, reason in plan.unfixable:+ unfixable += 1+ self.stdout.write(self.style.WARNING(f"{skill_files.label}: '{path}' has no canonical form — {reason}"))+ for _, path, canonical in plan.rewrites:+ rewritten += 1+ self.stdout.write(f"{skill_files.label}: '{path}' -> '{canonical}'")+ if apply and plan.rewrites:+ self._apply(skill_files.skill_id, plan.rewrites)++ verb = "Rewrote" if apply else "Would rewrite"+ self.stdout.write(+ self.style.SUCCESS(+ f"{verb} {rewritten} path(s); {collided} collision(s); "+ f"{unfixable} unfixable path(s); {unsafe} skill(s) left alone."+ )+ )++ def _apply(self, skill_id: UUID, rewrites: list[tuple[UUID, str, str]]) -> None:+ """Write one skill's rewrites and advance the version that makes clients pull them.++ `LLMSkillFile` carries no timestamp, so writing a file row moves nothing on its own. The+ marketplace plugin version is Max(updated_at) over a team's skill rows, the synthesized repo+ is cached under it, and `marketplace.json` publishes it as the label that triggers a re-pull+ — the same bump `archive_skill` and `rename_skill` make for the same reason. The two writes+ share a transaction because a rewrite that lands without its bump is invisible for good: a+ re-run finds the paths canonical, plans nothing, and never bumps.+ """+ with transaction.atomic():+ for row_id, _, canonical in rewrites:+ LLMSkillFile.objects.filter(pk=row_id).update(path=canonical)+ # A queryset update bypasses auto_now, so the timestamp is set explicitly.+ LLMSkill.objects.filter(pk=skill_id).update(updated_at=timezone.now())++ def _rows_by_skill(self, team_id: int | None) -> Iterator[SkillFileRows]:+ """Yield each skill's `(row id, path)` pairs as one group.++ Ordering by skill id makes the rows arrive grouped, so collision checking sees a whole+ skill without one query per skill. The skill's team, name and version join onto the same+ query, so a reported row names a skill an operator can find without a second lookup.++ `chunk_size` bounds the fetch only where server-side cursors are on. Cloud sets+ `DISABLE_SERVER_SIDE_CURSORS` behind PgBouncer, so `.iterator()` buffers the whole result+ set and an unfiltered run holds one tuple per file row. `LLMSkillFile` is per-team authored+ config with a per-skill file cap, not an event table, so that is acceptable for a one-off+ operator command. Pass `--team-id` to bound a run.+ """+ files = LLMSkillFile.objects.all()+ if team_id is not None:+ files = files.filter(skill__team_id=team_id)+ rows = (+ files.order_by("skill_id")+ .values_list("id", "skill_id", "skill__team_id", "skill__name", "skill__version", "path")+ .iterator(chunk_size=READ_CHUNK_SIZE)+ )+ for skill_id, group in groupby(rows, key=lambda row: row[1]):+ grouped = list(group)+ _, _, skill_team_id, name, version, _ = grouped[0]+ yield SkillFileRows(+ skill_id=skill_id,+ team_id=skill_team_id,+ name=name,+ version=version,+ rows=[(row_id, path) for row_id, _, _, _, _, path in grouped],+ )diff --git a/products/skills/backend/marketplace/adapters.py b/products/skills/backend/marketplace/adapters.pyindex 79ab34f599e0..2b73cfeb27f9 100644--- a/products/skills/backend/marketplace/adapters.py+++ b/products/skills/backend/marketplace/adapters.py@@ -424,7 +424,7 @@ def _name_and_description_are_valid(name: str, description: str) -> bool:_GENERATED_ENTRIES = ("SKILL.md", CODEX_METADATA_PATH)-def _bundle_paths_are_safe(paths: list[str]) -> bool:+def bundle_paths_are_safe(paths: list[str]) -> bool:"""True when a skill's archive entries unpack cleanly into a home directory on any filesystem.Every stored path must already be canonical (a legacy ``refs\\guide.md`` would be archived@@ -485,7 +485,7 @@ def _walk_full(candidates: QuerySet[LLMSkill], limit: int) -> _BundleWalk:.annotate(content_bytes=_octet_length(F("content"))).values_list("path", "content_bytes"))- if not _bundle_paths_are_safe([path for path, _ in sized_files]):+ if not bundle_paths_are_safe([path for path, _ in sized_files]):skipped_count = _record_skip(skipped_count, skipped_sample, name)continue# The stored bytes are a floor for the rendered tree, so a skill that fails here would fail