mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-13 15:55:46 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Native agents use tools to read reusable company skills. > - The existing native skill tools read only skills assigned to the agent. > - An agent can stop when a task needs another skill that is already in the company library. > - This pull request adds tools to search and read accessible company skills on demand. > - Agents can use the saved instructions without a persistent assignment change. ## Linked Issues or Issue Description **Problem or motivation** An agent cannot use an unassigned company skill through the standard native skill tools. A generic API workaround needs extra discovery and can return the full library. **Proposed solution** Add `search_skills` and `load_skill` to the native tool catalog. Search returns small pages of stored metadata. Load returns text from a saved version and lists its files. Both tools use the current run identity and existing company and project access checks. **Alternatives considered** Assign each skill before a read, or use generic API discovery. Both add work to a read that the company already permits. **Roadmap alignment** This extends the existing skills discovery path. It does not add a store, permission model or installation flow. Related: #15409 adds an assigned-skill loader to a different adapter. ## What Changed - Add bounded company skill search. Search does not synchronize sources or fetch repositories. - Add versioned text reads for SKILL.md and supporting files. Page text and file lists. Reject unsafe paths and binary files. - Recheck access after storage reads. Keep the existing closed Muse, skill-test and completion-review tool scopes. - Add integration tests with isolated PostgreSQL and real local package storage. Update the native tool count assertion and skill documentation. - Limit text files to 16 MiB before body reads. Accept saved file paths up to 4,096 characters plus the repository prefix. Return the response digest only on the first page. - Add optional byte limits to the package readers. Select only the requested legacy file in PostgreSQL. Cast the size comparison to `bigint` so existing callers can omit the limit. - Cache decoded text and its digest for one run. Limit the cache to 64 MiB and 64 files. Check access on each page and clear the cache when the run closes. - Read repository source files and wrapper inputs through verified warm reads or isolated recovery. These reads do not hold the company startup lock. ## Verification - Local verification tested tree `7ba5828387ca0a9c2ee99655818a28de1235a854`, published as commit `c9d04cba601541cca506f5b72aa6f7272a25379f`. The published tree matches that tested checkout. Current head `33fa3d3d4c7988288685ce7c6499d064e0cb9079` (tree `8daaa2f3264d1cd65a9b6d85dbb9d3729ac4a29d`) removes six comment lines from three source files; its executable code is unchanged from the locally tested revision. - Four native skill and authority Vitest suites: 72 passed and 7 existing sandbox checks skipped. All 20 skill-library integration tests passed. - Skill-library and package-store suites: 65 passed. This includes the 45 package-store cases and the same 20 integration cases. Ran with umask `022` for executable-file tests. - Server source and test typecheck layers passed in low-memory mode. The affected source cycle also passed a final separate check. - `git diff --check` and `node scripts/check-module-boundaries.mjs` passed. - Red/green evidence: the new default inline-read case reproduced PostgreSQL error `22003`. Two max-size paging cases reproduced extra body reads. Warm and cold repository reads timed out while another connection held the company startup lock. All five cases pass after the fixes. - Both 16 MiB paging cases check that the second page performs no additional body read. They also check that source-access revocation blocks a cached page. Warm and cold lock cases load both the generated wrapper and a supporting file while the startup lock remains held. - The first cold-read test attempt had an incorrect helper import. Corrected the test import, reproduced the lock timeout, then verified the fix. - [CI on the previous revision](https://github.com/paperclipai/paperclip/actions/runs/42319652795) passed typecheck, build and policy. Serialized shard 2 failed in the unchanged test `schedules bounded retries for failed accepted interaction continuation wakes`. The same case passed locally: 1 passed; 416 cases were excluded by the filter. The cause of the earlier CI failure remains unknown. [CI forc9d04cba](https://github.com/paperclipai/paperclip/actions/runs/42323260426) passed: 52 successful jobs and one skipped selector, including typecheck, build, all serialized server shards, native server tests, Runner/Rust, and e2e. This establishes a later pass; the earlier heartbeat failure's cause remains unknown. [CI for current head33fa3d3d](https://github.com/paperclipai/paperclip/actions/runs/42325148224) passed: all 52 applicable workflow jobs succeeded, with one skipped selector. The head has 58 successful check runs and three skipped checks; Snyk also passed. - Earlier failures remain recorded. Preflight cannot build Rust because Cargo is absent. The first catalog assertion expected 51 tools instead of 53. Test startup needed the plugin SDK build. An earlier typecheck hit the 8 GiB memory limit; low-memory checks passed. The previous package test failed under umask `077`; it passed under `022`, and this run's complete package suite passes with `022`. - Greptile reviewed current head `33fa3d3d` at 2026-11-07 01:40 UTC: 5/5 with no outstanding findings. The three older Ponytail threads are resolved after checking the size/cache, long-path, and bigint fixes and regression tests. Ponytail's supplemental refreshed assessment stopped before completing and posted no new findings. Its earlier findings are fixed and all seven review threads are resolved. The completed Greptile assessment covers current head. - No hosted deployment or live provider run was performed. ## Risks - Package reads can recover stored packages through the existing storage layer. Search reads only stored database metadata. - Load is a read. It does not install files into the provider workspace or change runtime bundles. It does not grant tool access or authorize actions in the skill text. - Callers must retain the returned version ID for later pages. A missing version returns an error. - Files above 16 MiB return a size-limit error. The run cache retains at most 64 MiB of decoded text and 64 entries. A cache miss or eviction requires a full verified file read. Each page still checks current access. The SHA-256 response field is null after the first text or file-list page. - No database migration or UI change is required. ## Model Used - OpenAI Codex, GPT-6 family, with reasoning, repository tools and code execution. The exact model ID and context window are not exposed to this agent. ## 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
131 lines
8.1 KiB
TypeScript
131 lines
8.1 KiB
TypeScript
import { measureRunPhase } from "./execution-preparation-timing.js";
|
|
import { promises as fs } from "node:fs";
|
|
import path from "node:path";
|
|
import type { Db } from "@paperclipai/db";
|
|
import type { CompanySkillVersionFileInventoryEntry } from "@paperclipai/shared";
|
|
import { skillPackageStore, type StoredSkillVersion } from "./skill-package-store.js";
|
|
import { assertSkillPackageFileSize, packageSha256, verifySkillPackageDirectory, relocateSkillPackageVerification, type SkillPackageFile, type SkillPackageManifest } from "./skill-package-files.js";
|
|
import { REPOSITORY_CONTENT_DIR, repositorySkillProjection } from "./skill-repository-package.js";
|
|
import { assertSkillSnapshotPath, skillFileBytes } from "./skill-snapshot.js";
|
|
import { ensureSkillCacheDirectory } from "./skill-version-cache.js";
|
|
|
|
type ProjectedFile = SkillPackageFile & { sourcePath?: string; generated?: Buffer };
|
|
// Only the wrapper/policy bytes and file metadata are retained, never a decoded repository.
|
|
const projections = new Map<string, { files: ProjectedFile[]; bytes: number }>();
|
|
let projectionBytes = 0;
|
|
const MAX_PROJECTION_BYTES = 16 * 1024 * 1024;
|
|
const MAX_PROJECTIONS = 256;
|
|
const repositoryFiles = new WeakMap<SkillPackageManifest, ProjectedFile[]>();
|
|
function originalFiles(version: StoredSkillVersion, manifest: SkillPackageManifest): ProjectedFile[] {
|
|
if (version.repositorySnapshotId) {
|
|
let shared = repositoryFiles.get(manifest);
|
|
if (!shared) {
|
|
shared = manifest.files.map(file => ({ ...file, sourcePath: file.path, path: `${REPOSITORY_CONTENT_DIR}/${file.path}` }));
|
|
repositoryFiles.set(manifest, shared);
|
|
}
|
|
return [...shared];
|
|
}
|
|
if (!version.packageFileMap) return manifest.files.map(file => ({ ...file, sourcePath: file.path }));
|
|
const byPath = new Map(manifest.files.map(file => [file.path, file]));
|
|
return version.packageFileMap.map(mapping => {
|
|
assertSkillSnapshotPath(mapping.path);
|
|
assertSkillSnapshotPath(mapping.sourcePath);
|
|
const file = byPath.get(mapping.sourcePath);
|
|
if (!file) throw new Error("Skill version references a missing package file");
|
|
return { ...file, ...mapping, encoding: mapping.encoding ?? file.encoding };
|
|
});
|
|
}
|
|
export function skillPackageRuntime(db: Db) {
|
|
const store = skillPackageStore(db);
|
|
function readOriginalFile(version: StoredSkillVersion, sourcePath: string, maxBytes?: number, projected?: ProjectedFile) {
|
|
return store.readVersionFile({ ...version, packageSnapshotId: version.repositorySnapshotId ?? version.packageSnapshotId,
|
|
packageFileMap: projected ? [{ path: sourcePath, sourcePath, kind: projected.kind, encoding: projected.encoding }] : null }, sourcePath, true, maxBytes);
|
|
}
|
|
async function inventory(version: StoredSkillVersion) {
|
|
const id = version.repositorySnapshotId ?? version.packageSnapshotId;
|
|
if (!id) throw new Error("Skill version has no package");
|
|
const manifest = await store.manifest(version.companyId, id);
|
|
const files = originalFiles(version, manifest).map(({ path, kind }) => ({ path, kind }));
|
|
if (version.repositorySnapshotId) {
|
|
files.unshift({ path: "SKILL.md", kind: "skill" });
|
|
const policy = path.posix.join(path.posix.dirname(version.repositorySkillPath!), "agents/openai.yaml");
|
|
const file = manifest.files.find(file => file.path === policy);
|
|
if (file) files.push({ path: "agents/openai.yaml", kind: file.kind });
|
|
}
|
|
return files;
|
|
}
|
|
async function projection(version: StoredSkillVersion, maxBytes?: number): Promise<ProjectedFile[]> {
|
|
const id = version.repositorySnapshotId ?? version.packageSnapshotId;
|
|
if (!id) throw new Error("Skill version has no package");
|
|
const manifest = await store.manifest(version.companyId, id);
|
|
const key = `${version.companyId}:${version.id}:${manifest.contentHash}`;
|
|
const files = originalFiles(version, manifest);
|
|
const cached = projections.get(key);
|
|
if (cached) return [...files, ...cached.files];
|
|
const generatedFiles: ProjectedFile[] = [];
|
|
if (version.repositorySnapshotId) {
|
|
if (!version.repositorySkillPath) throw new Error("Repository skill entrypoint is missing");
|
|
const wanted = new Set([version.repositorySkillPath, path.posix.join(path.posix.dirname(version.repositorySkillPath), "agents/openai.yaml")]);
|
|
for (const file of manifest.files.filter(file => wanted.has(file.path))) assertSkillPackageFileSize(file.size, maxBytes);
|
|
const selected: CompanySkillVersionFileInventoryEntry[] = [];
|
|
for (const file of manifest.files.filter(file => wanted.has(file.path))) {
|
|
const original = await readOriginalFile(version, file.path, maxBytes);
|
|
if (!original) throw new Error("Repository skill file is missing");
|
|
selected.push(original);
|
|
}
|
|
const generated = repositorySkillProjection(selected, version.repositorySkillPath!).filter(file => !file.path.startsWith(`${REPOSITORY_CONTENT_DIR}/`));
|
|
for (const file of generated) {
|
|
const bytes = skillFileBytes(file);
|
|
generatedFiles.push({ path: file.path, kind: file.kind, encoding: file.encoding ?? "utf8", executable: Boolean(file.executable),
|
|
size: bytes.length, sha256: packageSha256(bytes), generated: bytes });
|
|
}
|
|
}
|
|
// Retain only tiny generated wrappers/policies per version. Shared package
|
|
// file metadata follows the bounded manifest cache through the WeakMap.
|
|
const bytes = generatedFiles.reduce((sum, file) => sum + (file.generated?.length ?? 0) + 512, 0);
|
|
while (projections.size && (projections.size >= MAX_PROJECTIONS || projectionBytes + bytes > MAX_PROJECTION_BYTES)) {
|
|
const first = projections.keys().next().value!;
|
|
projectionBytes -= projections.get(first)!.bytes;
|
|
projections.delete(first);
|
|
}
|
|
if (generatedFiles.length && bytes <= MAX_PROJECTION_BYTES) { projections.set(key, { files: generatedFiles, bytes }); projectionBytes += bytes; }
|
|
return [...files, ...generatedFiles];
|
|
}
|
|
async function materialize(version: StoredSkillVersion, directory: string) {
|
|
const files = await measureRunPhase("skills.repository.read", () => projection(version));
|
|
const parent = path.dirname(directory);
|
|
await ensureSkillCacheDirectory(parent, true);
|
|
if (await measureRunPhase("skills.version.verify", () => verifySkillPackageDirectory(directory, files), { fileCount: files.length })) return directory;
|
|
return store.withPackage(version.companyId, (version.repositorySnapshotId ?? version.packageSnapshotId)!, async root => {
|
|
const staging = await fs.mkdtemp(path.join(parent, `.${version.id}-`));
|
|
try {
|
|
for (const file of files) {
|
|
const target = path.join(staging, file.path);
|
|
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
if (file.generated) await fs.writeFile(target, file.generated);
|
|
else await fs.copyFile(path.join(root, file.sourcePath!), target);
|
|
await fs.chmod(target, file.executable ? 0o555 : 0o444);
|
|
}
|
|
if (!await verifySkillPackageDirectory(staging, files)) throw new Error("Prepared skill version failed integrity verification");
|
|
await fs.rm(directory, { recursive: true, force: true });
|
|
await fs.rename(staging, directory);
|
|
relocateSkillPackageVerification(staging, directory, files);
|
|
return directory;
|
|
} finally { await fs.rm(staging, { recursive: true, force: true }); }
|
|
});
|
|
}
|
|
async function readFile(version: StoredSkillVersion, relative: string, maxBytes?: number) {
|
|
const id = (version.repositorySnapshotId ?? version.packageSnapshotId)!;
|
|
const manifest = await store.manifest(version.companyId, id);
|
|
const file = originalFiles(version, manifest).find(file => file.path === relative)
|
|
?? (await projection(version, maxBytes)).find(file => file.path === relative);
|
|
if (!file) return null;
|
|
assertSkillPackageFileSize(file.size, maxBytes);
|
|
const content = file.generated ? file.generated.toString(file.encoding)
|
|
: (await readOriginalFile(version, file.sourcePath!, maxBytes, file))?.content;
|
|
if (content === undefined) return null;
|
|
return { path: file.path, kind: file.kind, content, encoding: file.encoding, executable: file.executable };
|
|
}
|
|
return { inventory, materialize, readFile };
|
|
}
|