Files
PaperClipAI/packages/db/src/migrations/0175_nested_skill_folders.sql
T
DottaandPaperclip 52aea90263 feat: organize skills with nested folders and My Skills (#9633)
## Thinking Path

> - Paperclip is the open source control plane people use to organize
and govern AI-agent companies
> - Company skills are durable resources that users browse, import,
assign, and maintain over time
> - A flat skill list plus tags does not provide a stable location or
hierarchy for personal, company, project-imported, and bundled skills
> - Folder paths need to be canonical, company-scoped, safe to move, and
preserved across re-imports without changing skill IDs
> - The `/skills` UI also needs traversal, breadcrumbs, move/create
flows, and a dedicated My Skills namespace that work on desktop and
mobile
> - This pull request adds the folder data model and APIs, reserved-root
lifecycle, project import behavior, and the folder-first skills
experience
> - The benefit is a predictable filesystem-like organization model
while tags remain available for cross-cutting classification

## Linked Issues or Issue Description

Refs #9619 — the reviewed folder foundation was intentionally closed and
folded into this combined feature PR.
Refs #9026 — earlier flat-folder attempt superseded by this integrated
implementation.
Refs #3281 — related skill organization proposal; this PR uses canonical
persisted folders rather than deriving groups from skill keys, and does
not add hidden-skill behavior.

**Feature request**

- **Problem:** Skills currently lack a canonical hierarchical location,
making personal skills, project imports, bundled skills, and
company-authored skills difficult to traverse and manage at scale.
- **Proposed behavior:** Add nested company-scoped folders with stable
paths, reserved My/Projects/Bundled roots, subtree queries, safe
move/create operations, and a folder-first `/skills` library UI.
- **Import behavior:** New project scans file skills under
`projects/<project-slug>`; later imports update content without
overriding a user-selected folder.
- **Alternatives considered:** Tags alone remain useful for
cross-cutting classification, but they do not provide canonical
location, nesting, reserved namespaces, or stable import placement.
- **Roadmap alignment:** Extends the completed Skills Manager and
Scheduled Routines capabilities without duplicating an active roadmap
item.

## What Changed

- Adds `folders` persistence for routine and skill folders, nested
canonical paths, parent/slug/system-key fields, migration backfills, and
reapply-safe migrations `0174`–`0175` after current master migrations.
- Adds company-scoped folder CRUD, cycle/depth/namespace validation,
reserved My/Projects/Bundled lifecycle, item moves, subtree filtering,
and folder paths on skill results.
- Preserves project-import placement: first import files into the
project folder, while re-import keeps user-owned placement and stable
skill IDs.
- Adds the `/skills` folder tree rail, tags facet, breadcrumbs,
subfolder browser, move/new-folder dialog, canonical detail location,
inline tag editing, and folder-aware Studio creation.
- Keeps bundled skills read-only even when their source metadata is
incomplete by detecting the reserved Bundled folder and hiding
selection/move actions.
- Extends routine folder UI and OpenAPI coverage, and adds regression
tests across migrations, services, routes, tree helpers, pages, and
Studio creation.

## Verification

- `pnpm exec vitest run
packages/db/src/nested-skill-folders-migration.test.ts
server/src/__tests__/folders-routes.test.ts
server/src/__tests__/folders-service.test.ts
server/src/__tests__/company-skills-service.test.ts
server/src/__tests__/routines-service.test.ts
ui/src/components/folders/FolderControls.test.tsx
ui/src/components/folders/SkillFolderTree.test.tsx
ui/src/components/folders/skill-folder-tree.test.ts
ui/src/pages/CompanySkills.test.tsx ui/src/pages/Routines.test.tsx
ui/src/pages/SkillStudio.test.tsx
ui/src/lib/company-skill-routes.test.ts ui/src/lib/skill-create.test.ts`
— 13 files, 192 tests passed.
- `pnpm exec vitest run ui/src/pages/CompanySkills.test.tsx
ui/src/components/folders/SkillFolderTree.test.tsx` — 2 files, 20 tests
passed after preserving the existing PR's bundled-skill fixes.
- `pnpm -r typecheck` — passed for all workspace packages.
- `pnpm test:run` — passed in an isolated CI-like environment with
inherited Paperclip runtime identity and static AWS credential variables
removed.
- `pnpm build` — production build passed for all workspace packages.
- Greptile iteration 2 — 5/5 confidence with zero unresolved threads on
commit `ff2d67aa71`.
- Latest-head GitHub checks — all success, neutral, or skipped; PR is
mergeable with a clean merge state.
- `pnpm check:token-gates` — reports nine existing `#9627` comment false
positives already present on `master`; this PR introduces no new token
violation.

## Risks

- **Migration/backfill:** `0174` creates the foundation and `0175` adds
nested/reserved semantics. Both are ordered after current master
migration `0173`, are covered by numbering/safety checks, and are
designed to be reapply-safe.
- **Reserved namespaces:** My, Projects, and Bundled roots are
service-managed. Regression coverage prevents namespace squatting,
cross-company folder use, bundled writes, cycles, and excessive depth.
- **Behavioral change:** Project scans choose a project folder only on
initial creation; existing skills deliberately retain their current
folder during refresh.
- **UI scope:** The folder rail applies to the Installed library;
Catalog retains the discovery-oriented category sidebar.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI `gpt-5.5` in Codex CLI, medium reasoning mode; runtime did not
expose a context-window value. Used repository/file tools, terminal
execution, Git/GitHub operations, test execution, and code editing.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-07-16 15:50:45 -05:00

233 lines
9.0 KiB
SQL

ALTER TABLE "folders" ADD COLUMN IF NOT EXISTS "parent_id" uuid;
--> statement-breakpoint
ALTER TABLE "folders" ADD COLUMN IF NOT EXISTS "slug" text;
--> statement-breakpoint
ALTER TABLE "folders" ADD COLUMN IF NOT EXISTS "system_key" text;
--> statement-breakpoint
WITH normalized AS (
SELECT
"id",
COALESCE(
NULLIF(TRIM(BOTH '-' FROM REGEXP_REPLACE(LOWER("name"), '[^a-z0-9]+', '-', 'g')), ''),
'folder'
) AS base_slug,
ROW_NUMBER() OVER (
PARTITION BY "company_id", "kind", COALESCE(
NULLIF(TRIM(BOTH '-' FROM REGEXP_REPLACE(LOWER("name"), '[^a-z0-9]+', '-', 'g')), ''),
'folder'
)
ORDER BY "id"
) AS duplicate_number
FROM "folders"
WHERE "slug" IS NULL
)
UPDATE "folders" AS folder
SET "slug" = CASE
WHEN normalized.duplicate_number = 1 THEN normalized.base_slug
ELSE normalized.base_slug || '-' || LEFT(folder."id"::text, 8)
END
FROM normalized
WHERE folder."id" = normalized."id";
--> statement-breakpoint
ALTER TABLE "folders" ALTER COLUMN "slug" SET NOT NULL;
--> statement-breakpoint
DROP INDEX IF EXISTS "folders_company_kind_name_uq";
--> statement-breakpoint
DO $$ BEGIN
IF NOT EXISTS (
SELECT 1 FROM "pg_constraint" WHERE "conname" = 'folders_parent_id_folders_id_fk'
) THEN
ALTER TABLE "folders" ADD CONSTRAINT "folders_parent_id_folders_id_fk"
FOREIGN KEY ("parent_id") REFERENCES "public"."folders"("id") ON DELETE restrict ON UPDATE no action;
END IF;
END $$;
--> statement-breakpoint
ALTER TABLE "folders" DROP CONSTRAINT IF EXISTS "folders_company_kind_parent_slug_uq";
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "folders_company_kind_root_slug_uq"
ON "folders" USING btree ("company_id", "kind", "slug")
WHERE "parent_id" IS NULL;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "folders_company_kind_parent_slug_uq"
ON "folders" USING btree ("company_id", "kind", "parent_id", "slug")
WHERE "parent_id" IS NOT NULL;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "folders_company_kind_system_key_uq"
ON "folders" USING btree ("company_id", "kind", "system_key")
WHERE "system_key" IS NOT NULL;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "folders_company_kind_parent_position_idx"
ON "folders" USING btree ("company_id", "kind", "parent_id", "position", "name");
--> statement-breakpoint
UPDATE "folders" AS folder
SET "slug" = 'bundled-' || LEFT(REPLACE(folder."id"::text, '-', ''), 8), "updated_at" = now()
WHERE folder."kind" = 'skill'
AND folder."parent_id" IS NULL
AND folder."slug" = 'bundled'
AND folder."system_key" IS NULL
AND EXISTS (
SELECT 1
FROM "company_skills" AS skill
WHERE skill."company_id" = folder."company_id"
AND skill."folder_id" IS NULL
AND (skill."key" LIKE 'paperclipai/bundled/%' OR skill."metadata"->>'sourceKind' = 'paperclip_bundled')
);
--> statement-breakpoint
INSERT INTO "folders" ("company_id", "kind", "parent_id", "name", "slug", "system_key", "position")
SELECT DISTINCT skill."company_id", 'skill', NULL::uuid, 'Bundled', 'bundled', 'bundled', 0
FROM "company_skills" AS skill
WHERE skill."folder_id" IS NULL
AND (skill."key" LIKE 'paperclipai/bundled/%' OR skill."metadata"->>'sourceKind' = 'paperclip_bundled')
AND NOT EXISTS (
SELECT 1 FROM "folders" AS folder
WHERE folder."company_id" = skill."company_id"
AND folder."kind" = 'skill'
AND folder."system_key" = 'bundled'
)
ON CONFLICT DO NOTHING;
--> statement-breakpoint
WITH bundled_categories AS (
SELECT DISTINCT ON (skill."company_id", category.slug)
skill."company_id",
category.name,
category.slug
FROM "company_skills" AS skill
CROSS JOIN LATERAL (
SELECT
COALESCE(NULLIF(SPLIT_PART(skill."key", '/', 3), ''), 'other') AS name,
COALESCE(
NULLIF(TRIM(BOTH '-' FROM REGEXP_REPLACE(LOWER(COALESCE(NULLIF(SPLIT_PART(skill."key", '/', 3), ''), 'other')), '[^a-z0-9]+', '-', 'g')), ''),
'other'
) AS slug
) AS category
WHERE skill."folder_id" IS NULL
AND (skill."key" LIKE 'paperclipai/bundled/%' OR skill."metadata"->>'sourceKind' = 'paperclip_bundled')
ORDER BY skill."company_id", category.slug, category.name
)
INSERT INTO "folders" ("company_id", "kind", "parent_id", "name", "slug", "system_key", "position")
SELECT category."company_id", 'skill', root."id", category."name", category."slug", 'bundled:' || category."slug", 0
FROM bundled_categories AS category
JOIN "folders" AS root
ON root."company_id" = category."company_id"
AND root."kind" = 'skill'
AND root."system_key" = 'bundled'
ON CONFLICT DO NOTHING;
--> statement-breakpoint
UPDATE "company_skills" AS skill
SET "folder_id" = category_folder."id", "updated_at" = now()
FROM "folders" AS root
JOIN "folders" AS category_folder
ON category_folder."company_id" = root."company_id"
AND category_folder."kind" = 'skill'
AND category_folder."parent_id" = root."id"
WHERE skill."company_id" = root."company_id"
AND root."kind" = 'skill'
AND root."system_key" = 'bundled'
AND skill."folder_id" IS NULL
AND (skill."key" LIKE 'paperclipai/bundled/%' OR skill."metadata"->>'sourceKind' = 'paperclip_bundled')
AND category_folder."system_key" = 'bundled:' || COALESCE(
NULLIF(TRIM(BOTH '-' FROM REGEXP_REPLACE(LOWER(COALESCE(NULLIF(SPLIT_PART(skill."key", '/', 3), ''), 'other')), '[^a-z0-9]+', '-', 'g')), ''),
'other'
);
--> statement-breakpoint
UPDATE "folders" AS folder
SET "slug" = 'projects-' || LEFT(REPLACE(folder."id"::text, '-', ''), 8), "updated_at" = now()
WHERE folder."kind" = 'skill'
AND folder."parent_id" IS NULL
AND folder."slug" = 'projects'
AND folder."system_key" IS NULL
AND EXISTS (
SELECT 1
FROM "company_skills" AS skill
WHERE skill."company_id" = folder."company_id"
AND skill."folder_id" IS NULL
AND skill."metadata"->>'sourceKind' = 'project_scan'
AND skill."metadata"->>'projectId' IS NOT NULL
);
--> statement-breakpoint
INSERT INTO "folders" ("company_id", "kind", "parent_id", "name", "slug", "system_key", "position")
SELECT DISTINCT skill."company_id", 'skill', NULL::uuid, 'Projects', 'projects', 'projects', 1
FROM "company_skills" AS skill
WHERE skill."folder_id" IS NULL
AND skill."metadata"->>'sourceKind' = 'project_scan'
AND skill."metadata"->>'projectId' IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM "folders" AS folder
WHERE folder."company_id" = skill."company_id"
AND folder."kind" = 'skill'
AND folder."system_key" = 'projects'
)
ON CONFLICT DO NOTHING;
--> statement-breakpoint
WITH project_sources AS (
SELECT DISTINCT ON (skill."company_id", skill."metadata"->>'projectId')
skill."company_id",
skill."metadata"->>'projectId' AS project_id,
COALESCE(project."name", NULLIF(skill."metadata"->>'projectName', ''), 'Project') AS project_name
FROM "company_skills" AS skill
LEFT JOIN "projects" AS project
ON project."id"::text = skill."metadata"->>'projectId'
AND project."company_id" = skill."company_id"
WHERE skill."folder_id" IS NULL
AND skill."metadata"->>'sourceKind' = 'project_scan'
AND skill."metadata"->>'projectId' IS NOT NULL
ORDER BY skill."company_id", skill."metadata"->>'projectId', skill."id"
), project_folders AS (
SELECT
source."company_id",
source.project_id,
source.project_name,
container."id" AS parent_id,
COALESCE(
NULLIF(TRIM(BOTH '-' FROM REGEXP_REPLACE(LOWER(source.project_name), '[^a-z0-9]+', '-', 'g')), ''),
'project'
) AS base_slug
FROM project_sources AS source
JOIN "folders" AS container
ON container."company_id" = source."company_id"
AND container."kind" = 'skill'
AND container."system_key" = 'projects'
), ranked_project_folders AS (
SELECT
source.*,
ROW_NUMBER() OVER (
PARTITION BY source."company_id", source.parent_id, source.base_slug
ORDER BY source.project_id
) AS duplicate_number
FROM project_folders AS source
)
INSERT INTO "folders" ("company_id", "kind", "parent_id", "name", "slug", "system_key", "position")
SELECT
source."company_id",
'skill',
source.parent_id,
source.project_name,
CASE
WHEN sibling."id" IS NULL AND source.duplicate_number = 1 THEN source.base_slug
ELSE source.base_slug || '-' || LEFT(source.project_id, 8)
END,
'project:' || source.project_id,
0
FROM ranked_project_folders AS source
LEFT JOIN "folders" AS sibling
ON sibling."company_id" = source."company_id"
AND sibling."kind" = 'skill'
AND sibling."parent_id" = source.parent_id
AND sibling."slug" = source.base_slug
WHERE NOT EXISTS (
SELECT 1 FROM "folders" AS existing
WHERE existing."company_id" = source."company_id"
AND existing."kind" = 'skill'
AND existing."system_key" = 'project:' || source.project_id
)
ON CONFLICT DO NOTHING;
--> statement-breakpoint
UPDATE "company_skills" AS skill
SET "folder_id" = project_folder."id", "updated_at" = now()
FROM "folders" AS project_folder
WHERE project_folder."company_id" = skill."company_id"
AND project_folder."kind" = 'skill'
AND project_folder."system_key" = 'project:' || (skill."metadata"->>'projectId')
AND skill."folder_id" IS NULL
AND skill."metadata"->>'sourceKind' = 'project_scan';