Files
PaperClipAI/server
Nicky LeachandClaude Opus 5.5 ba521863f6 perf(skills): import environment skills when a setup draft is created (#23672)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A plugin environment driver can declare `skillKeys`. Core adds those
managed skills to each run on the environment.
> - Before a run can use a skill, Core imports it into the company's
skill library. This occurs once for each company.
> - The import occurs when the first task starts. So the first task in a
new company waits for it.
> - Each missing skill is imported in its own transaction. Each import
checks all bundled skills two times, and the transactions do not share
that check.
> - This pull request starts the import when a person creates a setup
draft, and checks the bundled skills once.
> - The benefit is that the first task starts sooner. The setup does not
wait for the import.

## Linked Issues or Issue Description

I searched the open and closed issues and pull requests. I did not find
a duplicate.

**What happened?**

The first task on a plugin environment with four managed skills spent
more than ten seconds in skill import. Most of this time was the check
and first import of Core's bundled skills. That check ran again for each
managed skill.

**Expected behavior**

The import must start before the first task, while the person completes
setup. One import must check the bundled skills once.

**Steps to reproduce**

1. Install a plugin with an environment driver that declares several
`skillKeys`.
2. Make that environment the instance default.
3. Create a company, set up its first agent, and give it a task.
4. Measure the skill selection part of the first run's preparation.

**Paperclip version or commit**

`7e025ae65f`

**Deployment mode**

Self-hosted server with a plugin environment that declares managed
skills.

## What Changed

- When a person creates a setup draft, the server imports the
environment's missing skills in the background. The response does not
wait for this import.
- A failed background import writes a warning. It does not change the
draft. Run admission still installs the skills that are missing.
- The background import is counted as active work, so an idle drain
waits for it. It runs once and does not poll or retry.
- An environment skill import checks the bundled skills once before it
installs the missing skills. Before, each skill did this check two times
in its own transaction.
- `importPackageFiles` reads the library rows after its own inventory
check. It no longer does the check a second time.
- `doc/skills-and-teams-catalog.md` describes the new behavior.

Skill selection, version pins, and the files that a run receives do not
change.

## Verification

- `npx vitest run src/__tests__/environment-skills.test.ts`: 7 passed.
Two tests are new. They fail when I remove the single check and the
background import.
- `npx vitest run` on the company skill service, managed skill content,
company portability, agent adapter validation, and agent lifecycle
tests: all passed.
- `src/__tests__/ai-connections.test.ts`: 5 of its tests failed in the
same local run. The reconnect tests also fail at `7e025ae65f` without
this change on the same local database. They do not use this code.
- `pnpm --filter @paperclipai/server typecheck`: passed.
- Not done: a measurement of the first task with this change.

## Risks

- A draft that a person abandons leaves the imported skills in the
company library. The same skills are imported for the next agent on that
environment.
- The background import and a first task can run together. The existing
lock for each skill makes the second one wait and use the first result.
- The bundled skill check now runs outside the skill's transaction. If
the skill import then fails, the bundled skills remain imported. They
are imported by other library reads too.
- No migration.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code, with
extended thinking, tool use and code execution.

## 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 15:29:50 -08:00
..