Files
PaperClipAI/server/src/__tests__/plugin-managed-skills.test.ts
T
Nicky LeachandPaperclip 4ff493fb40 feat(plugins): include managed skills for selected environments (#17774)
## Thinking Path

> - Paperclip manages agents and their execution environments.
> - Plugins can vendor company skills and provide environments.
> - Environment tools need their instructions before the provider
starts.
> - Persistent computers also need those skills before any run begins.
> - This change adds environment requirements to normal skill selection
and lifecycle synchronization.
> - Plugin names prevent ambiguity, and required skills take delivery
precedence without deleting company data.

## Linked Issues or Issue Description

**Problem or motivation**

An environment can provide desktop tools while its agent receives no
instructions for those tools. A first-run-only import also leaves idle
computers without required skills. Same-name company skills can make
provider discovery ambiguous.

**Proposed solution**

Let plugin environment drivers declare their managed `skillKeys`.
Install missing requirements during lifecycle reconciliation or new-run
preparation. Deliver updates through the durable runtime event journal.
Give managed imports stable plugin names and report shadowed
assignments.

**Alternatives considered**

ROM links do not make a skill a normal Paperclip selection. Polling
duplicates the existing lifecycle delivery queue. Replacing a same-name
company record would destroy independent data and assignments.

**Roadmap alignment**

This extends the shipped Plugin system and Skills Manager. The existing
related-PR search found no duplicate contract.

## What Changed

- Validate `environmentDrivers[].skillKeys` as unique references to the
same plugin's managed skills. Require a ready, enabled plugin with
`skills.managed` authority.
- Resolve company-scoped skills before version capture. Preserve
explicit pins, company edits, and saved recovery selections. Serialize
concurrent first installs.
- Allow lifecycle callers to request missing-skill installation through
`agents.getRuntime`. Ordinary task and recovery reads do not install new
declarations. Pass task-selected environment IDs through the SDK.
- Add SQL migration `0378_environment_skill_runtime_hooks`. Required
skill edits, effective default changes, environment changes, and plugin
requirement changes enqueue durable runtime events. Reuse lifecycle
delivery and retries; add no polling.
- Import managed defaults as `plugin-<short-plugin-id>-<skill-slug>`,
including the SKILL.md name. Preserve canonical keys. Keep same-name
company records separate without counter suffixes.
- Required environment skills win delivery over same-name company
assignments. Preserve saved assignments and library content. Record
shadowed and winning keys in run warnings, run snapshots, and plugin
runtime state. Removing the environment requirement restores the
ordinary assignment.
- Document the contract for `driver: "plugin"`. Other drivers and
plugins without requirements keep existing behavior. Existing managed
copies retain edits and names until reset.

## Verification

- Latest targeted local checks: environment skills (5), managed skills
(9), heartbeat execution configuration (27), and SDK worker RPC host
(23): **65 tests passed** (including the targeted schema-drift check).
Disposable PostgreSQL exercised the migration, first install before any
run, concurrent lifecycle reads, implicit edit events, inherited
defaults, rollback, plugin requirement changes, shadowing, preserved
records, version capture, and recovery.
- Review follow-up also verifies case-insensitive/runtime-name aliases,
recovery of a task-mentioned shadowed skill, preserved quoted/multiline
YAML, and zero runtime events for repeated local-folder settings writes.
- Migration numbering and safety checks passed. Whitespace and
secret/PII scans passed.
- Earlier targeted manifest validation passed (43 tests). No validator
behavior changed in this follow-up.
- Broad tests, typecheck, build and end-to-end verification passed in
[GitHub
CI](https://github.com/paperclipai/paperclip/actions/runs/40387647219)
for current head `ce57f2a4d`. Greptile is 5/5 with no unresolved
threads. The [commitperclip review
check](https://github.com/paperclipai/paperclip/actions/runs/40387646208)
also passed after restoring its required duplicate-search checkbox.
- Earlier attempts found a SQL alias collision, an unparenthesized CASE
expression, and fixture assumptions about bundled catalog rows and YAML
quoting. These were corrected before the passing checks. The first broad
CI run also exposed a schema-drift check that assumed every journal
entry had a schema snapshot; it now reads the latest actual snapshot, so
SQL-only migrations need no invented snapshot. A test callback
replacement also affected an unrelated fixture; it was restored before
the final passing execution-configuration suite. Sandbox runs could not
start PostgreSQL; the reported database tests used local socket access.

## Risks

- Apply migration 0378 before using the updated lifecycle contract. It
adds trigger functions and event producers; it does not rewrite agent
assignments or skill content.
- Required environment skills take precedence over same-name
assignments, including task-selected skills. Both library records
remain. Run warnings identify omissions.
- Company edits are preserved. Plugin upgrades do not reset installed
content or rename existing copies. Explicit reset adopts current
defaults.
- Namespaced library slugs can be shared by distinct canonical records.
Callers must use IDs or canonical keys when a slug is ambiguous.
- No live provider request or deployment was performed. The downstream
plugin must be built against this Core revision. Persistent-home
synchronization retains its existing edit-conflict and retry behavior.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, repository tools, and code
execution. The exact model identifier and context-window size are not
exposed in this task context.

## Checklist

- [x] Thinking path and issue description included
- [x] Model and available capabilities recorded
- [x] Roadmap and related PRs checked
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] Public references only; no internal issue IDs or instance URLs
- [x] Targeted local tests pass; broad verification delegated to GitHub
CI
- [x] New behavior covered and documentation updated
- [x] Current-head CI green and Greptile 5/5 with no unresolved comments

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-26 14:11:35 -07:00

325 lines
13 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { eq } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import {
activityLog,
companies,
companySkills,
createDb,
pluginManagedResources,
plugins,
} from "@paperclipai/db";
import type { PaperclipPluginManifestV1 } from "@paperclipai/shared";
import {
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
} from "./helpers/embedded-postgres.js";
import { companySkillService } from "../services/company-skills.js";
import { buildHostServices } from "../services/plugin-host-services.js";
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
function createEventBusStub() {
return {
forPlugin() {
return {
emit: async () => {},
subscribe: () => {},
};
},
} as any;
}
function issuePrefix(id: string) {
return `T${id.replace(/-/g, "").slice(0, 6).toUpperCase()}`;
}
function manifest(): PaperclipPluginManifestV1 {
return {
id: "paperclip.managed-skills-test",
apiVersion: 1,
version: "0.1.0",
displayName: "Managed Skills Test",
description: "Test plugin",
author: "Paperclip",
categories: ["automation"],
capabilities: ["skills.managed"],
entrypoints: { worker: "./dist/worker.js" },
skills: [{
skillKey: "wiki-maintainer",
displayName: "Wiki Maintainer Skill",
description: "Use LLM Wiki tools to maintain company knowledge.",
files: [{
path: "references/wiki-style.md",
content: "# Wiki style\n\nKeep pages cited and terse.\n",
}],
}],
};
}
if (!embeddedPostgresSupport.supported) {
console.warn(
`Skipping embedded Postgres plugin-managed skill tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
);
}
describeEmbeddedPostgres("plugin-managed skills", () => {
let db!: ReturnType<typeof createDb>;
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
beforeAll(async () => {
tempDb = await startEmbeddedPostgresTestDatabase("paperclip-plugin-managed-skills-");
db = createDb(tempDb.connectionString);
}, 20_000);
afterEach(async () => {
await db.delete(activityLog);
await db.delete(pluginManagedResources);
await db.delete(companySkills);
await db.delete(plugins);
await db.delete(companies);
});
afterAll(async () => {
await tempDb?.cleanup();
});
async function seedCompanyAndPlugin(pluginManifest = manifest()) {
const companyId = randomUUID();
const pluginId = randomUUID();
await db.insert(companies).values({
id: companyId,
name: "Paperclip",
issuePrefix: issuePrefix(companyId),
});
await db.insert(plugins).values({
id: pluginId,
pluginKey: pluginManifest.id,
packageName: "@paperclipai/plugin-managed-skills-test",
version: pluginManifest.version,
apiVersion: pluginManifest.apiVersion,
categories: pluginManifest.categories,
manifestJson: pluginManifest,
status: "ready",
installOrder: 1,
});
const services = buildHostServices(db, pluginId, pluginManifest.id, createEventBusStub(), undefined, {
manifest: pluginManifest,
});
return { companyId, pluginId, pluginManifest, services };
}
it("installs and resolves managed company skills by stable resource key", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const created = await services.skills.managedReconcile({
companyId,
skillKey: "wiki-maintainer",
});
expect(created.status).toBe("created");
expect(created.skill).toMatchObject({
name: "plugin-managed-skills-test-wiki-maintainer",
slug: "plugin-managed-skills-test-wiki-maintainer",
key: "plugin/paperclip-managed-skills-test/wiki-maintainer",
sourceType: "catalog",
fileInventory: expect.arrayContaining([
expect.objectContaining({ path: "SKILL.md", kind: "skill" }),
expect.objectContaining({ path: "references/wiki-style.md", kind: "reference" }),
]),
});
const resolved = await services.skills.managedGet({
companyId,
skillKey: "wiki-maintainer",
});
expect(resolved.status).toBe("resolved");
expect(resolved.skillId).toBe(created.skillId);
const [binding] = await db.select().from(pluginManagedResources);
expect(binding).toMatchObject({
companyId,
resourceKind: "skill",
resourceKey: "wiki-maintainer",
resourceId: created.skillId,
});
});
it("keeps a same-slug user skill separate through automatic install, reset, and relinking", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const original = await companySkillService(db).createLocalSkill(companyId, { name: "Wiki Maintainer Skill", markdown: "# User instructions\n" });
const created = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(created.skillId).not.toBe(original.id);
expect(created.skill!.key).toBe("plugin/paperclip-managed-skills-test/wiki-maintainer");
expect(created.skill!.slug).toBe("plugin-managed-skills-test-wiki-maintainer");
expect(created.skill!.markdown).toContain('name: "plugin-managed-skills-test-wiki-maintainer"');
await services.skills.managedReset({ companyId, skillKey: "wiki-maintainer" });
await db.delete(pluginManagedResources).where(eq(pluginManagedResources.resourceId, created.skillId!));
const relinked = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(relinked.skillId).toBe(created.skillId);
expect((await db.select().from(companySkills).where(eq(companySkills.id, original.id)))[0]).toMatchObject({ key: original.key, slug: original.slug, markdown: original.markdown });
});
it("claims the plugin name while preserving a conflicting company skill as a separate record", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const original = await companySkillService(db).createLocalSkill(companyId, {
name: "plugin-managed-skills-test-wiki-maintainer", markdown: "# User instructions\n",
});
const installed = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(installed.skill!.slug).toBe(original.slug);
expect(installed.skill!.id).not.toBe(original.id);
await services.skills.managedReset({ companyId, skillKey: "wiki-maintainer" });
const records = (await db.select().from(companySkills)).filter(skill => [original.key, installed.skill!.key].includes(skill.key));
expect(records).toHaveLength(2);
expect(records.find(skill => skill.id === original.id)).toMatchObject({ key: original.key, markdown: original.markdown });
expect(await db.select().from(pluginManagedResources)).toEqual([expect.objectContaining({ resourceId: installed.skill!.id })]);
});
it("preserves operator edits during reconcile and restores manifest defaults on reset", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const created = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(created.skillId).toBeTruthy();
await db
.update(companySkills)
.set({
name: "Custom Wiki Skill",
markdown: "# Custom instructions\n",
updatedAt: new Date(),
})
.where(eq(companySkills.id, created.skillId!));
const reconciled = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(reconciled.status).toBe("resolved");
expect(reconciled.skill).toMatchObject({
name: "Custom Wiki Skill",
markdown: "# Custom instructions\n",
});
const reset = await services.skills.managedReset({ companyId, skillKey: "wiki-maintainer" });
expect(reset.status).toBe("reset");
expect(reset.skill).toMatchObject({
name: "plugin-managed-skills-test-wiki-maintainer",
description: "Use LLM Wiki tools to maintain company knowledge.",
});
expect(reset.skill?.markdown).toContain("key: \"plugin/paperclip-managed-skills-test/wiki-maintainer\"");
});
it("does not rewrite managed skill bindings when defaults are unchanged", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const created = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(created.skillId).toBeTruthy();
const [binding] = await db.select().from(pluginManagedResources);
const oldUpdatedAt = new Date("2026-01-01T00:00:00.000Z");
await db
.update(pluginManagedResources)
.set({ updatedAt: oldUpdatedAt })
.where(eq(pluginManagedResources.id, binding.id));
const reconciled = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
const [bindingAfter] = await db.select().from(pluginManagedResources);
expect(reconciled.status).toBe("resolved");
expect(bindingAfter.updatedAt.toISOString()).toBe(oldUpdatedAt.toISOString());
});
it("relinks an existing canonical skill without overwriting operator edits", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const created = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(created.skillId).toBeTruthy();
await db.delete(pluginManagedResources).where(eq(pluginManagedResources.resourceId, created.skillId!));
await db
.update(companySkills)
.set({
name: "Existing Customized Wiki Skill",
markdown: "# Existing customized instructions\n",
updatedAt: new Date(),
})
.where(eq(companySkills.id, created.skillId!));
const relinked = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(relinked.status).toBe("relinked");
expect(relinked.skillId).toBe(created.skillId);
expect(relinked.skill).toMatchObject({
name: "Existing Customized Wiki Skill",
markdown: "# Existing customized instructions\n",
});
expect(relinked.defaultDrift).toEqual({ changedFiles: ["SKILL.md"] });
const [binding] = await db.select().from(pluginManagedResources);
expect(binding).toMatchObject({
companyId,
resourceKind: "skill",
resourceKey: "wiki-maintainer",
resourceId: created.skillId,
});
});
it("reports drift when installed skill files differ from plugin defaults", async () => {
const { companyId, services } = await seedCompanyAndPlugin();
const created = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(created.defaultDrift).toBeNull();
await db
.update(companySkills)
.set({
markdown: "# Custom instructions\n",
updatedAt: new Date(),
})
.where(eq(companySkills.id, created.skillId!));
const drifted = await services.skills.managedReconcile({ companyId, skillKey: "wiki-maintainer" });
expect(drifted.status).toBe("resolved");
expect(drifted.defaultDrift).toEqual({ changedFiles: ["SKILL.md"] });
const reset = await services.skills.managedReset({ companyId, skillKey: "wiki-maintainer" });
expect(reset.defaultDrift).toBeNull();
});
it("preserves unrelated YAML while replacing quoted and multiline identity fields", async () => {
const declaration = manifest();
declaration.skills = [{ skillKey: "yaml-skill", displayName: "YAML skill", markdown:
'---\n"name": |\n Previous name\nslug: old-slug\ndescription: >\n Keep this folded description.\nmetadata:\n "category": desktop\n# Keep this comment.\n---\nBody stays unchanged.\n' }];
const { companyId, services } = await seedCompanyAndPlugin(declaration);
const created = await services.skills.managedReconcile({ companyId, skillKey: "yaml-skill" });
expect(created.skill!.markdown).toContain('name: "plugin-managed-skills-test-yaml-skill"');
expect(created.skill!.markdown).not.toContain("Previous name");
expect(created.skill!.markdown).toContain('description: >\n Keep this folded description.\nmetadata:\n "category": desktop\n# Keep this comment.');
expect(created.skill!.markdown).toMatch(/Body stays unchanged\.\n$/);
await services.skills.managedReset({ companyId, skillKey: "yaml-skill" });
});
it("adds the canonical managed key to manifest-provided markdown skills", async () => {
const pluginManifest = manifest();
pluginManifest.skills = [
...(pluginManifest.skills ?? []),
{
skillKey: "markdown-skill",
displayName: "Markdown Skill",
markdown: [
"---",
"name: markdown-skill",
"description: Markdown source without a key.",
"---",
"",
"# Markdown Skill",
"",
"Follow the managed markdown.",
].join("\n"),
},
];
const { companyId, services } = await seedCompanyAndPlugin(pluginManifest);
const created = await services.skills.managedReconcile({ companyId, skillKey: "markdown-skill" });
expect(created.status).toBe("created");
expect(created.skill).toMatchObject({
key: "plugin/paperclip-managed-skills-test/markdown-skill",
name: "plugin-managed-skills-test-markdown-skill",
});
expect(created.skill?.markdown).toContain('key: "plugin/paperclip-managed-skills-test/markdown-skill"');
});
});