A desktop coding agent with free access to frontier models.
TypeScript
feat(server): install skills as folders with their helper files
Samrat Talukdar1daf90e89021
diff --git a/apps/server/src/server.ts b/apps/server/src/server.tsindex 47c10d2c..2964cff7 100644--- a/apps/server/src/server.ts+++ b/apps/server/src/server.ts@@ -63,6 +63,17 @@ import {parseMcpAppSandboxCsp,} from "./mcp-app-sandbox.js";import { deleteSkill, listSkills, renderSkillContentForResponse, upsertSkill } from "./skills.js";+import {+ findSkillConflict,+ installSkillBundle,+ listSkillTree,+ prepareSkillBundle,+ readBoundedJsonBody,+ resolveProjectSkillDir,+ SKILL_BUNDLE_MAX_REQUEST_BYTES,+ summarizeSkillBundle,+ updateSkillFiles,+} from "./skill-bundle.js";import { deleteCommand, listCommands, repairCommands, upsertCommand } from "./commands.js";import { ApiError, formatError } from "./errors.js";import { errorCode } from "./atomic-write.js";@@ -4175,6 +4186,100 @@ function createRoutes(return jsonResponse({ name, path: result.path, description: description ?? "", scope: "project" });});+ // Skill folders: SKILL.md plus helper files, uploaded as a folder or a .zip+ // that the client flattened to relative paths + base64 bytes. The preview+ // runs every check the install runs (and reports a name collision) without+ // writing anything.+ addRoute(routes, "POST", "/workspace/:id/skills/bundle/preview", "client", async (ctx) => {+ requireClientScope(ctx, "collaborator");+ const workspace = await resolveWorkspace(config, ctx.params.id);+ const body = await readBoundedJsonBody(ctx.request, SKILL_BUNDLE_MAX_REQUEST_BYTES);+ const bundle = prepareSkillBundle({ files: body.files, name: typeof body.name === "string" ? body.name : null });+ const conflict = await findSkillConflict(workspace.path, bundle.name);+ return jsonResponse({ ...summarizeSkillBundle(bundle), conflict });+ });++ addRoute(routes, "POST", "/workspace/:id/skills/bundle", "client", async (ctx) => {+ ensureWritable(config);+ requireClientScope(ctx, "collaborator");+ const workspace = await resolveWorkspace(config, ctx.params.id);+ const body = await readBoundedJsonBody(ctx.request, SKILL_BUNDLE_MAX_REQUEST_BYTES);+ const bundle = prepareSkillBundle({ files: body.files, name: typeof body.name === "string" ? body.name : null });+ const onConflict = body.onConflict === "replace" ? "replace" : "fail";+ const targetDir = join(workspace.path, ".opencode", "skills", bundle.name);+ await requireApproval(ctx, {+ workspaceId: workspace.id,+ action: "skills.upsert",+ summary: `Install skill ${bundle.name} (${bundle.files.length} files)`,+ paths: [targetDir],+ });+ const result = await installSkillBundle(workspace.path, bundle, { onConflict });+ await recordAudit(workspace.path, {+ id: shortId(),+ workspaceId: workspace.id,+ actor: ctx.actor ?? { type: "remote" },+ action: "skills.upsert",+ target: result.dir,+ summary: `${result.action === "updated" ? "Replaced" : "Installed"} skill ${bundle.name} from an upload (${bundle.files.length} files)`,+ timestamp: Date.now(),+ });+ emitReloadEvent(ctx.reloadEvents, workspace, "skills", {+ type: "skill",+ name: bundle.name,+ action: result.action,+ path: result.path,+ });+ return jsonResponse({+ ...summarizeSkillBundle(bundle),+ path: result.path,+ dir: result.dir,+ action: result.action,+ scope: "project",+ });+ });++ addRoute(routes, "GET", "/workspace/:id/skills/:name/files", "client", async (ctx) => {+ const workspace = await resolveWorkspace(config, ctx.params.id);+ const dir = await resolveProjectSkillDir(workspace.path, String(ctx.params.name ?? ""));+ const tree = await listSkillTree(dir);+ return jsonResponse({ name: String(ctx.params.name ?? "").trim(), dir, ...tree });+ });++ addRoute(routes, "POST", "/workspace/:id/skills/:name/files", "client", async (ctx) => {+ ensureWritable(config);+ requireClientScope(ctx, "collaborator");+ const workspace = await resolveWorkspace(config, ctx.params.id);+ const name = String(ctx.params.name ?? "").trim();+ const dir = await resolveProjectSkillDir(workspace.path, name);+ const body = await readBoundedJsonBody(ctx.request, SKILL_BUNDLE_MAX_REQUEST_BYTES);+ const addPaths = Array.isArray(body.add) ? body.add.map((file) => String((file as { path?: unknown })?.path ?? "")) : [];+ const removePaths = Array.isArray(body.remove) ? body.remove.map(String) : [];+ await requireApproval(ctx, {+ workspaceId: workspace.id,+ action: "skills.upsert",+ summary: `Update files of skill ${name}`,+ paths: [...addPaths, ...removePaths].map((path) => join(dir, path)),+ });+ const result = await updateSkillFiles(dir, { add: body.add, remove: body.remove });+ await recordAudit(workspace.path, {+ id: shortId(),+ workspaceId: workspace.id,+ actor: ctx.actor ?? { type: "remote" },+ action: "skills.upsert",+ target: dir,+ summary: `Updated files of skill ${name} (+${result.added.length} -${result.removed.length})`,+ timestamp: Date.now(),+ });+ emitReloadEvent(ctx.reloadEvents, workspace, "skills", {+ type: "skill",+ name,+ action: "updated",+ path: join(dir, "SKILL.md"),+ });+ const tree = await listSkillTree(dir);+ return jsonResponse({ name, dir, ...result, ...tree });+ });+addRoute(routes, "DELETE", "/workspace/:id/skills/:name", "client", async (ctx) => {ensureWritable(config);requireClientScope(ctx, "collaborator");diff --git a/apps/server/src/skill-bundle.e2e.test.ts b/apps/server/src/skill-bundle.e2e.test.tsnew file mode 100644index 00000000..ea45ed4a--- /dev/null+++ b/apps/server/src/skill-bundle.e2e.test.ts@@ -0,0 +1,148 @@+import { afterEach, describe, expect, test } from "bun:test";+import { mkdir, mkdtemp, readFile, rm, stat } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";++import { startServer } from "./server.js";+import type { ServerConfig } from "./types.js";++type Served = { port: number; stop: (closeActiveConnections?: boolean) => void | Promise<void> };++const stops: Array<() => void | Promise<void>> = [];+const roots: string[] = [];+const savedEnv = { HOME: process.env.HOME, XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME };++afterEach(async () => {+ for (const [key, value] of Object.entries(savedEnv)) {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+ }+ while (stops.length) await stops.pop()?.();+ while (roots.length) await rm(roots.pop()!, { recursive: true, force: true });+});++async function start() {+ const root = await mkdtemp(join(tmpdir(), "omnirush-skill-bundle-e2e-"));+ roots.push(root);+ await mkdir(join(root, ".git"), { recursive: true });+ // Keep global skills (~/.config/opencode/skills, ~/.claude/skills) out of collision checks.+ process.env.HOME = join(root, "home");+ process.env.XDG_CONFIG_HOME = join(root, "home", ".config");+ const config: ServerConfig = {+ host: "127.0.0.1",+ port: 0,+ token: "owt_test_token",+ hostToken: "owt_host_token",+ approval: { mode: "auto", timeoutMs: 1000 },+ corsOrigins: ["*"],+ workspaces: [{ id: "ws_1", name: "Workspace", path: root, preset: "starter", workspaceType: "local" }],+ authorizedRoots: [root],+ readOnly: false,+ startedAt: Date.now(),+ tokenSource: "cli",+ hostTokenSource: "cli",+ logFormat: "pretty",+ logRequests: false,+ };+ const server = await startServer(config) as Served;+ stops.push(() => server.stop(true));+ const base = `http://127.0.0.1:${server.port}/workspace/ws_1`;+ const call = async (method: string, path: string, body?: unknown) => {+ const response = await fetch(base + path, {+ method,+ headers: { Authorization: `Bearer ${config.token}`, "content-type": "application/json" },+ body: body === undefined ? undefined : JSON.stringify(body),+ });+ return { status: response.status, json: await response.json() as Record<string, any> };+ };+ return { root, call };+}++const b64 = (text: string) => Buffer.from(text).toString("base64");+const SKILL = "---\nname: greeter\ndescription: Greets people with the helper script\n---\n\nRun `scripts/greet.sh <name>` from this skill's base directory.\n";++describe("skill folder upload routes", () => {+ test("preview, install, conflict + replace/rename, list and edit helper files", async () => {+ const { root, call } = await start();+ const files = [+ { path: "greeter/SKILL.md", contentBase64: b64(SKILL) },+ { path: "greeter/scripts/greet.sh", contentBase64: b64("#!/bin/sh\necho \"hello $1\"\n") },+ { path: "greeter/references/tone.md", contentBase64: b64("Be warm.\n") },+ { path: "greeter/.DS_Store", contentBase64: b64("junk") },+ ];++ const preview = await call("POST", "/skills/bundle/preview", { files });+ expect(preview.status).toBe(200);+ expect(preview.json).toMatchObject({+ name: "greeter",+ strippedRoot: "greeter",+ skipped: ["greeter/.DS_Store"],+ conflict: null,+ });+ expect(preview.json.files.map((f: { path: string }) => f.path)).toEqual(["SKILL.md", "references/tone.md", "scripts/greet.sh"]);+ // A preview writes nothing.+ await expect(stat(join(root, ".opencode", "skills", "greeter"))).rejects.toThrow();++ const installed = await call("POST", "/skills/bundle", { files });+ expect(installed.status).toBe(200);+ expect(installed.json.action).toBe("added");+ const dir = join(root, ".opencode", "skills", "greeter");+ expect((await stat(join(dir, "scripts", "greet.sh"))).mode & 0o100).toBe(0o100);++ const listed = await call("GET", "/skills");+ expect(listed.json.items.map((s: { name: string }) => s.name)).toEqual(["greeter"]);++ // Same name again: preview reports it, install refuses without "replace".+ const again = await call("POST", "/skills/bundle/preview", { files });+ expect(again.json.conflict).toMatchObject({ name: "greeter", scope: "project", replaceable: true });+ expect((await call("POST", "/skills/bundle", { files })).status).toBe(409);+ expect((await call("POST", "/skills/bundle", { files, onConflict: "replace" })).json.action).toBe("updated");++ // Rename installs a second copy with a rewritten frontmatter name.+ const renamed = await call("POST", "/skills/bundle", { files, name: "greeter-copy" });+ expect(renamed.status).toBe(200);+ expect(await readFile(join(root, ".opencode", "skills", "greeter-copy", "SKILL.md"), "utf8")).toContain("name: greeter-copy");++ const tree = await call("GET", "/skills/greeter/files");+ expect(tree.status).toBe(200);+ expect(tree.json.files.map((f: { path: string }) => f.path)).toEqual(["SKILL.md", "references/tone.md", "scripts/greet.sh"]);++ const edited = await call("POST", "/skills/greeter/files", {+ add: [{ path: "scripts/farewell.py", contentBase64: b64("print('bye')\n") }],+ remove: ["references/tone.md"],+ });+ expect(edited.status).toBe(200);+ expect(edited.json.files.map((f: { path: string }) => f.path)).toEqual(["SKILL.md", "scripts/farewell.py", "scripts/greet.sh"]);++ // Editing SKILL.md through the existing upsert keeps the helper files.+ const upsert = await call("POST", "/skills", { name: "greeter", content: SKILL.replace("Greets people", "Greets everyone") });+ expect(upsert.status).toBe(200);+ expect((await call("GET", "/skills/greeter/files")).json.files).toHaveLength(3);+ });++ test("refuses unsafe uploads with readable errors", async () => {+ const { call } = await start();+ const skill = { path: "SKILL.md", contentBase64: b64(SKILL) };+ const cases: Array<[unknown[], number, string]> = [+ [[skill, { path: "../evil.sh", contentBase64: b64("x") }], 422, "invalid_skill_path"],+ [[skill, { path: "/abs.sh", contentBase64: b64("x") }], 422, "invalid_skill_path"],+ [[skill, { path: ".env", contentBase64: b64("TOKEN=1") }], 422, "skill_bundle_credentials"],+ [[{ path: "notes.md", contentBase64: b64("x") }], 422, "skill_md_missing"],+ [[{ path: "SKILL.md", contentBase64: b64("no frontmatter") }], 422, "invalid_skill_frontmatter"],+ ];+ for (const [files, status, code] of cases) {+ const response = await call("POST", "/skills/bundle", { files });+ expect({ status: response.status, code: response.json.code }).toEqual({ status, code });+ }+ const credential = await call("POST", "/skills/bundle/preview", { files: [skill, { path: "keys/id_ed25519", contentBase64: b64("x") }] });+ expect(credential.json.details).toEqual({ paths: ["keys/id_ed25519"] });+ expect((await call("GET", "/skills/nope/files")).status).toBe(404);+ });++ test("refuses an oversized body before buffering it", async () => {+ const { call } = await start();+ const huge = "A".repeat(40 * 1024 * 1024);+ const response = await call("POST", "/skills/bundle/preview", { files: [{ path: "SKILL.md", contentBase64: huge }] });+ expect(response.status).toBe(413);+ });+});diff --git a/apps/server/src/skill-bundle.test.ts b/apps/server/src/skill-bundle.test.tsnew file mode 100644index 00000000..3a8eb34c--- /dev/null+++ b/apps/server/src/skill-bundle.test.ts@@ -0,0 +1,257 @@+import { afterEach, beforeEach, describe, expect, test } from "bun:test";+import { lstat, mkdir, mkdtemp, readFile, readdir, rm, stat, symlink, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";++import {+ installSkillBundle,+ isSkillBundleJunk,+ listSkillTree,+ normalizeSkillBundlePath,+ prepareSkillBundle,+ resolveProjectSkillDir,+ SKILL_BUNDLE_LIMITS,+ updateSkillFiles,+} from "./skill-bundle.js";+import { listSkills } from "./skills.js";+import { ApiError } from "./errors.js";++const b64 = (text: string) => Buffer.from(text, "utf8").toString("base64");+const skillMd = (name = "pdf-tools", description = "Work with PDFs") => `---\nname: ${name}\ndescription: ${description}\n---\n\n# ${name}\n\nRun scripts/extract.sh\n`;+const file = (path: string, text: string, executable?: boolean) => ({ path, contentBase64: b64(text), ...(executable ? { executable } : {}) });++function codeOf(fn: () => unknown): string {+ try {+ fn();+ } catch (error) {+ return error instanceof ApiError ? error.code : `non-api:${String(error)}`;+ }+ return "no-error";+}++let workspace: string;+beforeEach(async () => {+ workspace = await mkdtemp(join(tmpdir(), "omnirush-skill-bundle-"));+ await mkdir(join(workspace, ".git"), { recursive: true });+});+afterEach(async () => {+ await rm(workspace, { recursive: true, force: true });+});++describe("normalizeSkillBundlePath", () => {+ test("accepts nested relative paths and a leading ./", () => {+ expect(normalizeSkillBundlePath("scripts/extract.sh")).toBe("scripts/extract.sh");+ expect(normalizeSkillBundlePath("./references/api.md")).toBe("references/api.md");+ });++ test.each([+ ["../escape.sh"],+ ["scripts/../../escape.sh"],+ ["/etc/passwd"],+ ["C:/Windows/evil.dll"],+ ["scripts\\evil.sh"],+ ["scripts//double.sh"],+ ["scripts/./x.sh"],+ ["a:b"],+ ["nul.txt"],+ ["trailing-dot."],+ ["ctrl\u0001char"],+ [""],+ ])("refuses %p", (path) => {+ expect(codeOf(() => normalizeSkillBundlePath(path))).toBe("invalid_skill_path");+ });++ test("refuses over-deep and over-long paths", () => {+ expect(codeOf(() => normalizeSkillBundlePath(Array.from({ length: SKILL_BUNDLE_LIMITS.maxDepth + 1 }, () => "d").join("/")))).toBe("invalid_skill_path");+ expect(codeOf(() => normalizeSkillBundlePath("a".repeat(SKILL_BUNDLE_LIMITS.maxPathLength + 1)))).toBe("invalid_skill_path");+ });+});++describe("prepareSkillBundle", () => {+ test("accepts SKILL.md at the root with helpers, marking shebang scripts executable", () => {+ const bundle = prepareSkillBundle({+ files: [+ file("scripts/extract.sh", "#!/bin/sh\necho hi\n"),+ file("SKILL.md", skillMd()),+ file("references/guide.md", "# Guide\n"),+ file("assets/logo.bin", "\u0000\u0001", true),+ ],+ });+ expect(bundle.name).toBe("pdf-tools");+ expect(bundle.description).toBe("Work with PDFs");+ expect(bundle.strippedRoot).toBeNull();+ expect(bundle.files.map((f) => f.path)).toEqual(["SKILL.md", "assets/logo.bin", "references/guide.md", "scripts/extract.sh"]);+ expect(bundle.files.find((f) => f.path === "scripts/extract.sh")?.executable).toBe(true);+ expect(bundle.files.find((f) => f.path === "references/guide.md")?.executable).toBe(false);+ expect(bundle.files.find((f) => f.path === "assets/logo.bin")?.executable).toBe(true);+ });++ test("strips a single wrapping folder (a zipped skill folder)", () => {+ const bundle = prepareSkillBundle({+ files: [file("pdf-tools/SKILL.md", skillMd()), file("pdf-tools/scripts/run.py", "print(1)\n")],+ });+ expect(bundle.strippedRoot).toBe("pdf-tools");+ expect(bundle.files.map((f) => f.path)).toEqual(["SKILL.md", "scripts/run.py"]);+ });++ test("drops OS and VCS junk and reports it", () => {+ const bundle = prepareSkillBundle({+ files: [+ file("SKILL.md", skillMd()),+ file(".DS_Store", "x"),+ file("__MACOSX/._SKILL.md", "x"),+ file(".git/config", "[core]"),+ file("node_modules/x/index.js", "x"),+ file("scripts/__pycache__/a.cpython-312.pyc", "x"),+ ],+ });+ expect(bundle.files.map((f) => f.path)).toEqual(["SKILL.md"]);+ expect(bundle.skipped.sort()).toEqual([".DS_Store", ".git/config", "__MACOSX/._SKILL.md", "node_modules/x/index.js", "scripts/__pycache__/a.cpython-312.pyc"]);+ expect(isSkillBundleJunk("scripts/run.sh")).toBe(false);+ });++ test("requires SKILL.md at the root or under one wrapping folder", () => {+ expect(codeOf(() => prepareSkillBundle({ files: [file("README.md", "x")] }))).toBe("skill_md_missing");+ expect(codeOf(() => prepareSkillBundle({ files: [file("a/SKILL.md", skillMd()), file("b/x.md", "x")] }))).toBe("skill_md_missing");+ expect(codeOf(() => prepareSkillBundle({ files: [file("skill.md", skillMd())] }))).toBe("skill_md_missing");+ expect(codeOf(() => prepareSkillBundle({ files: [] }))).toBe("skill_md_missing");+ });++ test("requires frontmatter with a kebab-case name and a description", () => {+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", "# no frontmatter\n")] }))).toBe("invalid_skill_frontmatter");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", "---\ndescription: x\n---\n")] }))).toBe("invalid_skill_frontmatter");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", "---\nname: a\n---\n")] }))).toBe("invalid_skill_frontmatter");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", "---\nname: [unclosed\n---\n")] }))).toBe("invalid_skill_frontmatter");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd("Not Kebab"))] }))).toBe("invalid_skill_name");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd("ok", "x".repeat(1025)))] }))).toBe("invalid_skill_frontmatter");+ });++ test("a rename rewrites the frontmatter name and keeps other keys", () => {+ const bundle = prepareSkillBundle({+ files: [file("SKILL.md", "---\nname: pdf-tools\ndescription: Work with PDFs\nlicense: MIT\n---\n\nBody\n")],+ name: "pdf-tools-2",+ });+ const text = bundle.files[0]!.bytes.toString("utf8");+ expect(bundle.name).toBe("pdf-tools-2");+ expect(text).toContain("name: pdf-tools-2");+ expect(text).toContain("license: MIT");+ expect(text.endsWith("Body\n")).toBe(true);+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd())], name: "Bad Name" }))).toBe("invalid_skill_name");+ });++ test("refuses credential-like files by the collector denylist and private key blocks", () => {+ for (const path of [".env", ".env.local", "config/.env.production", "certs/server.pem", "keys/deploy", "id_rsa", "secrets/prod.yaml", "api token.csv", "credentials.json"]) {+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file(path, "x")] }))).toBe("skill_bundle_credentials");+ }+ expect(codeOf(() => prepareSkillBundle({+ files: [file("SKILL.md", skillMd()), file("scripts/deploy.sh", "cat <<EOF\n-----BEGIN OPENSSH PRIVATE KEY-----\nabc\n-----END OPENSSH PRIVATE KEY-----\nEOF\n")],+ }))).toBe("skill_bundle_credentials");+ // Source and docs that only mention the words are fine.+ expect(prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("references/token-handling.md", "About tokens")] }).files).toHaveLength(2);+ });++ test("refuses case-insensitive duplicates and file/folder clashes", () => {+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("a.md", "x"), file("A.md", "y")] }))).toBe("skill_path_collision");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("a", "x"), file("a/b.md", "y")] }))).toBe("skill_path_collision");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("a/b.md", "y"), file("a", "x")] }))).toBe("skill_path_collision");+ });++ test("enforces file-count and size caps", () => {+ const many = Array.from({ length: SKILL_BUNDLE_LIMITS.maxFiles }, (_, i) => file(`refs/${i}.md`, "x"));+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), ...many] }))).toBe("skill_bundle_too_many_files");+ const big = Buffer.alloc(SKILL_BUNDLE_LIMITS.maxFileBytes + 1, 97).toString("base64");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), { path: "big.bin", contentBase64: big }] }))).toBe("skill_file_too_large");+ const chunk = Buffer.alloc(SKILL_BUNDLE_LIMITS.maxFileBytes - 10, 97).toString("base64");+ const total = Array.from({ length: 3 }, (_, i) => ({ path: `big-${i}.bin`, contentBase64: chunk }));+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), ...total] }))).toBe("skill_bundle_too_large");+ expect(codeOf(() => prepareSkillBundle({ files: [file("SKILL.md", skillMd()), { path: "x.bin", contentBase64: "not base64!" }] }))).toBe("invalid_skill_file");+ });+});++describe("installSkillBundle", () => {+ test("installs every file with its mode where the engine and the Library find it", async () => {+ const bundle = prepareSkillBundle({+ files: [file("SKILL.md", skillMd()), file("scripts/extract.sh", "#!/bin/sh\necho hi\n"), file("references/guide.md", "# Guide\n")],+ });+ const result = await installSkillBundle(workspace, bundle, { onConflict: "fail" });+ const dir = join(workspace, ".opencode", "skills", "pdf-tools");+ expect(result).toEqual({ path: join(dir, "SKILL.md"), dir, action: "added" });+ expect(await readFile(join(dir, "references", "guide.md"), "utf8")).toBe("# Guide\n");+ expect((await stat(join(dir, "scripts", "extract.sh"))).mode & 0o111).not.toBe(0);+ expect((await stat(join(dir, "references", "guide.md"))).mode & 0o111).toBe(0);+ expect((await listSkills(workspace, false)).map((s) => s.name)).toEqual(["pdf-tools"]);+ // No staging directory is left behind.+ expect((await readdir(join(workspace, ".opencode"))).sort()).toEqual(["skills"]);+ });++ test("a name collision fails unless replacing, and replace swaps the whole folder", async () => {+ const first = prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("scripts/old.sh", "#!/bin/sh\n")] });+ await installSkillBundle(workspace, first, { onConflict: "fail" });+ const second = prepareSkillBundle({ files: [file("SKILL.md", skillMd("pdf-tools", "v2")), file("scripts/new.sh", "#!/bin/sh\n")] });+ await expect(installSkillBundle(workspace, second, { onConflict: "fail" })).rejects.toMatchObject({ status: 409, code: "skill_exists" });+ const replaced = await installSkillBundle(workspace, second, { onConflict: "replace" });+ expect(replaced.action).toBe("updated");+ const tree = await listSkillTree(replaced.dir);+ expect(tree.files.map((f) => f.path)).toEqual(["SKILL.md", "scripts/new.sh"]);+ expect((await listSkills(workspace, false))[0]?.description).toBe("v2");+ });++ test("a collision with a skill outside .opencode/skills/<name> cannot be replaced", async () => {+ await mkdir(join(workspace, ".claude", "skills", "pdf-tools"), { recursive: true });+ await writeFile(join(workspace, ".claude", "skills", "pdf-tools", "SKILL.md"), skillMd());+ const bundle = prepareSkillBundle({ files: [file("SKILL.md", skillMd())] });+ await expect(installSkillBundle(workspace, bundle, { onConflict: "replace" })).rejects.toMatchObject({+ status: 409,+ details: { replaceable: false },+ });+ });+});++describe("updateSkillFiles", () => {+ async function installed() {+ const bundle = prepareSkillBundle({ files: [file("SKILL.md", skillMd()), file("scripts/a.sh", "#!/bin/sh\n"), file("refs/one.md", "1")] });+ await installSkillBundle(workspace, bundle, { onConflict: "fail" });+ return resolveProjectSkillDir(workspace, "pdf-tools");+ }++ test("adds, replaces and removes helper files and prunes empty folders", async () => {+ const dir = await installed();+ const result = await updateSkillFiles(dir, {+ add: [file("scripts/b.py", "print(2)\n"), file("scripts/a.sh", "#!/bin/sh\necho new\n")],+ remove: ["refs/one.md"],+ });+ expect(result).toEqual({ added: ["scripts/b.py", "scripts/a.sh"], removed: ["refs/one.md"] });+ const tree = await listSkillTree(dir);+ expect(tree.files.map((f) => f.path)).toEqual(["SKILL.md", "scripts/a.sh", "scripts/b.py"]);+ expect(await readFile(join(dir, "scripts", "a.sh"), "utf8")).toContain("echo new");+ await expect(lstat(join(dir, "refs"))).rejects.toThrow();+ });++ test("protects SKILL.md and refuses unsafe or credential paths", async () => {+ const dir = await installed();+ await expect(updateSkillFiles(dir, { remove: ["SKILL.md"] })).rejects.toMatchObject({ code: "skill_md_protected" });+ await expect(updateSkillFiles(dir, { add: [file("skill.md", "x")] })).rejects.toMatchObject({ code: "skill_md_protected" });+ await expect(updateSkillFiles(dir, { add: [file("../x.sh", "x")] })).rejects.toMatchObject({ code: "invalid_skill_path" });+ await expect(updateSkillFiles(dir, { add: [file(".env", "A=1")] })).rejects.toMatchObject({ code: "skill_bundle_credentials" });+ await expect(updateSkillFiles(dir, { remove: ["missing.md"] })).rejects.toMatchObject({ code: "skill_file_not_found" });+ await expect(updateSkillFiles(dir, { add: [file("scripts", "x")] })).rejects.toMatchObject({ code: "skill_path_collision" });+ });++ test("never writes through a linked folder inside the skill", async () => {+ const dir = await installed();+ const outside = await mkdtemp(join(tmpdir(), "omnirush-skill-outside-"));+ try {+ await symlink(outside, join(dir, "linked"));+ const tree = await listSkillTree(dir);+ expect(tree.files.find((f) => f.path === "linked")?.kind).toBe("symlink");+ await expect(updateSkillFiles(dir, { add: [file("linked/evil.sh", "x")] })).rejects.toMatchObject({ code: "skill_path_collision" });+ expect(await readdir(outside)).toEqual([]);+ } finally {+ await rm(outside, { recursive: true, force: true });+ }+ });++ test("only project skills resolve", async () => {+ await expect(resolveProjectSkillDir(workspace, "nope")).rejects.toMatchObject({ status: 404 });+ });+});diff --git a/apps/server/src/skill-bundle.ts b/apps/server/src/skill-bundle.tsnew file mode 100644index 00000000..61a7632b--- /dev/null+++ b/apps/server/src/skill-bundle.ts@@ -0,0 +1,546 @@+/**+ * Skill folders: a skill is a directory holding `SKILL.md` plus optional+ * helper files (`scripts/`, `references/`, `assets/`, ...). The engine loads+ * `<skills dir>/<name>/SKILL.md`, tells the model the skill's base directory,+ * and the model reads or runs the helpers by path relative to it.+ *+ * The desktop uploads a folder or a .zip as a flat list of relative paths and+ * bytes. Everything the client checked is checked again here, because this is+ * the only place that writes the files:+ *+ * - paths are relative, `/`-separated, with no `..`, `.`, empty, absolute,+ * drive-letter, backslash, colon, control-character or Windows-reserved+ * component, and no two paths collide case-insensitively (or as file vs dir);+ * - OS/VCS junk (`.DS_Store`, `__MACOSX/`, `.git/`, `node_modules/`, ...) is+ * dropped and reported as skipped;+ * - anything on the collector's credential denylist (`.env*`, `*.pem`,+ * `id_rsa`, `secrets/`, "api token.txt", ...) or holding a private-key block+ * refuses the upload, so a skill never carries a secret into the workspace+ * (and from there into a project archive);+ * - `SKILL.md` must sit at the root, or inside a single top-level folder that+ * is then stripped, with YAML frontmatter carrying a kebab-case `name` and a+ * 1-1024 character `description`;+ * - at most {@link SKILL_BUNDLE_LIMITS} files and bytes.+ *+ * Only regular files are ever written (symlinks cannot be expressed in the+ * upload format; the client refuses them earlier with a clearer message), and+ * an install lands atomically: files are staged outside the engine's skill+ * scan directories and renamed into place.+ */+import { chmod, lstat, mkdir, readdir, rename, rm, rmdir, writeFile } from "node:fs/promises";+import { randomBytes } from "node:crypto";+import { dirname, join, relative, resolve, sep } from "node:path";++import { ApiError } from "./errors.js";+import { buildFrontmatter, parseFrontmatter } from "./frontmatter.js";+import { listSkills } from "./skills.js";+import { exists } from "./utils.js";+import { validateDescription, validateSkillName } from "./validators.js";+import { isCollectorPathDenied } from "./workspace-collector.js";+import { projectSkillsDir } from "./workspace-files.js";+import { renameWithRetry } from "./atomic-write.js";++export const SKILL_BUNDLE_LIMITS = {+ /** Files kept after junk is dropped (SKILL.md included). */+ maxFiles: 200,+ /** Entries in the raw upload, junk included. */+ maxRawEntries: 2_000,+ maxFileBytes: 10 * 1024 * 1024,+ maxTotalBytes: 25 * 1024 * 1024,+ maxPathLength: 512,+ maxDepth: 16,+ maxSegmentBytes: 255,+} as const;++/** Upper bound of a bundle request body: base64 inflates by 4/3, plus JSON framing. */+export const SKILL_BUNDLE_MAX_REQUEST_BYTES = Math.ceil(SKILL_BUNDLE_LIMITS.maxTotalBytes * 4 / 3) + 2 * 1024 * 1024;++export const SKILL_MD = "SKILL.md";++export type SkillBundleInputFile = {+ path: string;+ /** File bytes, base64. */+ contentBase64: string;+ /** The source had an executable bit (zip unix mode, local stat). */+ executable?: boolean;+};++export type PreparedSkillFile = {+ path: string;+ bytes: Buffer;+ executable: boolean;+};++export type PreparedSkillBundle = {+ name: string;+ description: string;+ files: PreparedSkillFile[];+ skipped: string[];+ /** The single top-level folder that wrapped SKILL.md and was stripped. */+ strippedRoot: string | null;+ totalBytes: number;+};++export type SkillBundleFileSummary = { path: string; size: number; executable: boolean };++export type SkillConflict = {+ name: string;+ path: string;+ scope: "project" | "global";+ /** Only a skill folder in this workspace's .opencode/skills can be replaced in place. */+ replaceable: boolean;+};++const JUNK_NAMES = new Set([".ds_store", "thumbs.db", "desktop.ini", ".localized", "icon\r"]);+const JUNK_DIRS = new Set(["__macosx", ".git", "node_modules", "__pycache__", ".svn", ".hg", ".venv", ".idea", ".vscode"]);+const WINDOWS_RESERVED = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;+const PRIVATE_KEY_BLOCK = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----/;++function invalid(code: string, message: string, paths?: string[]): ApiError {+ return new ApiError(422, code, message, paths && paths.length ? { paths } : undefined);+}++/** Whether a (normalized) relative path is OS/VCS litter that is dropped, not refused. */+export function isSkillBundleJunk(path: string): boolean {+ const parts = path.split("/");+ const name = parts.at(-1)?.toLowerCase() ?? "";+ if (JUNK_NAMES.has(name) || name.startsWith("._") || name.endsWith(".pyc")) return true;+ return parts.slice(0, -1).some((part) => JUNK_DIRS.has(part.toLowerCase()));+}++/**+ * Normalizes one upload path or throws. A leading "./" is tolerated; nothing+ * else is rewritten, so what the preview shows is what lands on disk.+ */+export function normalizeSkillBundlePath(raw: string): string {+ if (typeof raw !== "string" || !raw) throw invalid("invalid_skill_path", "A file in the upload has no path");+ const path = raw.startsWith("./") ? raw.slice(2) : raw;+ const refuse = (why: string) => invalid("invalid_skill_path", `Unsafe path in upload (${why}): ${raw.slice(0, 200)}`, [raw.slice(0, 200)]);+ if (path.length > SKILL_BUNDLE_LIMITS.maxPathLength) throw refuse("too long");+ // eslint-disable-next-line no-control-regex+ if (/[\u0000-\u001f\u007f]/.test(path)) throw refuse("control character");+ if (path.includes("\\")) throw refuse("backslash");+ if (path.startsWith("/") || /^[A-Za-z]:/.test(path)) throw refuse("absolute path");+ if (path.includes(":")) throw refuse("colon");+ const parts = path.split("/");+ if (parts.length > SKILL_BUNDLE_LIMITS.maxDepth) throw refuse("nested too deep");+ for (const part of parts) {+ if (!part) throw refuse("empty component");+ if (part === "." || part === "..") throw refuse("parent or current directory reference");+ if (Buffer.byteLength(part) > SKILL_BUNDLE_LIMITS.maxSegmentBytes) throw refuse("name too long");+ if (WINDOWS_RESERVED.test(part) || /[. ]$/.test(part)) throw refuse("name not valid on Windows");+ }+ return parts.join("/");+}++/** Credential-like paths, by the collector's denylist (git internals and node_modules are junk, handled before). */+export function isSkillBundleCredentialPath(path: string): boolean {+ return isCollectorPathDenied(path);+}++function decodeBase64(value: unknown, path: string): Buffer {+ if (typeof value !== "string") throw invalid("invalid_skill_file", `File has no content: ${path}`, [path]);+ if (value.length > Math.ceil(SKILL_BUNDLE_LIMITS.maxFileBytes * 4 / 3) + 4) {+ throw invalid("skill_file_too_large", `File is larger than ${formatBytes(SKILL_BUNDLE_LIMITS.maxFileBytes)}: ${path}`, [path]);+ }+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(value)) throw invalid("invalid_skill_file", `File content is not base64: ${path}`, [path]);+ return Buffer.from(value, "base64");+}++export function formatBytes(bytes: number): string {+ if (bytes < 1024) return `${bytes} B`;+ if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;+ return `${Math.round((bytes / (1024 * 1024)) * 10) / 10} MB`;+}++type CheckedFile = { path: string; bytes: Buffer; executable: boolean };++/**+ * Normalizes, de-junks and checks a set of files against the path, collision,+ * credential and size rules. Shared by a fresh upload and by adding helper+ * files to an installed skill (`existing` are the paths already there).+ */+function checkFiles(+ input: unknown,+ options: { existing?: SkillBundleFileSummary[] } = {},+): { files: CheckedFile[]; skipped: string[] } {+ if (!Array.isArray(input)) throw invalid("invalid_skill_bundle", "Upload has no file list");+ if (input.length > SKILL_BUNDLE_LIMITS.maxRawEntries) {+ throw invalid("skill_bundle_too_many_files", `Upload has more than ${SKILL_BUNDLE_LIMITS.maxRawEntries} entries`);+ }+ const files: CheckedFile[] = [];+ const skipped: string[] = [];+ const credentials: string[] = [];+ const keyed = new Map<string, string>();+ const dirs = new Map<string, string>();+ for (const entry of options.existing ?? []) {+ keyed.set(entry.path.toLowerCase(), entry.path);+ const parts = entry.path.split("/");+ for (let i = 1; i < parts.length; i += 1) dirs.set(parts.slice(0, i).join("/").toLowerCase(), entry.path);+ }+ const existingKeys = new Set(keyed.keys());+ const uploaded = new Set<string>();+ for (const raw of input) {+ const item = raw as Partial<SkillBundleInputFile> | null;+ const path = normalizeSkillBundlePath(String(item?.path ?? ""));+ if (isSkillBundleJunk(path)) {+ skipped.push(path);+ continue;+ }+ if (isSkillBundleCredentialPath(path)) {+ credentials.push(path);+ continue;+ }+ const key = path.toLowerCase();+ if (uploaded.has(key) || (keyed.has(key) && !existingKeys.has(key))) {+ throw invalid("skill_path_collision", `Two files in the upload share a path (letter case differs at most): ${path}`, [path]);+ }+ uploaded.add(key);+ if (dirs.has(key)) throw invalid("skill_path_collision", `A file and a folder share a path: ${path}`, [path]);+ const parts = path.split("/");+ for (let i = 1; i < parts.length; i += 1) {+ const prefix = parts.slice(0, i).join("/").toLowerCase();+ if (keyed.has(prefix)) throw invalid("skill_path_collision", `A file and a folder share a path: ${parts.slice(0, i).join("/")}`, [path]);+ dirs.set(prefix, path);+ }+ keyed.set(key, path);+ const bytes = decodeBase64(item?.contentBase64, path);+ if (bytes.length > SKILL_BUNDLE_LIMITS.maxFileBytes) {+ throw invalid("skill_file_too_large", `File is larger than ${formatBytes(SKILL_BUNDLE_LIMITS.maxFileBytes)}: ${path}`, [path]);+ }+ if (PRIVATE_KEY_BLOCK.test(bytes.toString("latin1"))) {+ credentials.push(path);+ continue;+ }+ const shebang = bytes.length > 2 && bytes[0] === 0x23 && bytes[1] === 0x21;+ files.push({ path, bytes, executable: item?.executable === true || shebang });+ }+ if (credentials.length) {+ throw invalid(+ "skill_bundle_credentials",+ `The upload contains credential-like files, which skills must not carry. Remove them and try again: ${credentials.slice(0, 10).join(", ")}${credentials.length > 10 ? `, and ${credentials.length - 10} more` : ""}`,+ credentials,+ );+ }+ return { files, skipped };+}++function checkTotals(files: Array<{ bytes?: Buffer; size?: number }>): number {+ if (files.length > SKILL_BUNDLE_LIMITS.maxFiles) {+ throw invalid("skill_bundle_too_many_files", `A skill can hold at most ${SKILL_BUNDLE_LIMITS.maxFiles} files (this one has ${files.length})`);+ }+ const total = files.reduce((sum, file) => sum + (file.bytes?.length ?? file.size ?? 0), 0);+ if (total > SKILL_BUNDLE_LIMITS.maxTotalBytes) {+ throw invalid("skill_bundle_too_large", `A skill can hold at most ${formatBytes(SKILL_BUNDLE_LIMITS.maxTotalBytes)} (this one is ${formatBytes(total)})`);+ }+ return total;+}++/** Parses SKILL.md and returns its frontmatter name/description, or throws a readable 422. */+export function readSkillFrontmatter(content: string): { name: string; description: string; data: Record<string, unknown>; body: string } {+ let data: Record<string, unknown>;+ let body: string;+ try {+ ({ data, body } = parseFrontmatter(content.replace(/^/, "")));+ } catch (error) {+ const reason = error instanceof Error ? error.message.split("\n", 1)[0] : String(error);+ throw invalid("invalid_skill_frontmatter", `SKILL.md frontmatter is not valid YAML: ${reason}`, [SKILL_MD]);+ }+ if (!/^?---\r?\n/.test(content)) {+ throw invalid("invalid_skill_frontmatter", "SKILL.md must start with YAML frontmatter (--- name: ... description: ... ---)", [SKILL_MD]);+ }+ const name = typeof data.name === "string" ? data.name.trim() : "";+ const description = typeof data.description === "string" ? data.description.trim() : "";+ if (!name) throw invalid("invalid_skill_frontmatter", "SKILL.md frontmatter needs a name", [SKILL_MD]);+ if (!description) throw invalid("invalid_skill_frontmatter", "SKILL.md frontmatter needs a description", [SKILL_MD]);+ try {+ validateDescription(description);+ } catch {+ throw invalid("invalid_skill_frontmatter", "SKILL.md description must be 1-1024 characters", [SKILL_MD]);+ }+ return { name, description, data, body };+}++/**+ * Validates an upload and resolves the skill's name. `nameOverride` renames+ * the skill (the frontmatter `name` is rewritten to match, since the engine+ * registers a skill under its frontmatter name).+ */+export function prepareSkillBundle(input: { files: unknown; name?: string | null }): PreparedSkillBundle {+ const { files: checked, skipped } = checkFiles(input.files);+ if (!checked.length) throw invalid("skill_md_missing", "The upload is empty (nothing left after skipping system files)");++ let strippedRoot: string | null = null;+ let files = checked;+ if (!files.some((file) => file.path === SKILL_MD)) {+ const tops = new Set(files.map((file) => (file.path.includes("/") ? file.path.split("/")[0]! : "")));+ const [top] = [...tops];+ if (tops.size === 1 && top && files.some((file) => file.path === `${top}/${SKILL_MD}`)) {+ strippedRoot = top;+ files = files.map((file) => ({ ...file, path: file.path.slice(top.length + 1) }));+ } else {+ const nearMiss = files.find((file) => /(^|\/)skill\.md$/i.test(file.path));+ throw invalid(+ "skill_md_missing",+ nearMiss+ ? `SKILL.md must be at the top of the skill folder (found ${nearMiss.path}; the name is case-sensitive)`+ : "No SKILL.md at the top of the upload. A skill is a folder with SKILL.md at its root (or a .zip of that folder).",+ );+ }+ }+ const totalBytes = checkTotals(files);++ const skillFile = files.find((file) => file.path === SKILL_MD)!;+ const text = skillFile.bytes.toString("utf8");+ const frontmatter = readSkillFrontmatter(text);+ const override = typeof input.name === "string" ? input.name.trim() : "";+ const name = override || frontmatter.name;+ try {+ validateSkillName(name);+ } catch {+ throw invalid(+ "invalid_skill_name",+ override+ ? "Skill name must be kebab-case (lowercase letters, digits and single hyphens, 1-64 chars)"+ : `SKILL.md name "${frontmatter.name.slice(0, 80)}" must be kebab-case (lowercase letters, digits and single hyphens, 1-64 chars). Choose another name to install it.`,+ [SKILL_MD],+ );+ }+ if (name !== frontmatter.name) {+ const rewritten = buildFrontmatter({ ...frontmatter.data, name }) + frontmatter.body.replace(/^\r?\n/, "");+ skillFile.bytes = Buffer.from(rewritten, "utf8");+ }+ return {+ name,+ description: frontmatter.description,+ files: files.sort((a, b) => (a.path === SKILL_MD ? -1 : b.path === SKILL_MD ? 1 : a.path.localeCompare(b.path))),+ skipped,+ strippedRoot,+ totalBytes,+ };+}++export function summarizeSkillBundle(bundle: PreparedSkillBundle) {+ return {+ name: bundle.name,+ description: bundle.description,+ files: bundle.files.map((file) => ({ path: file.path, size: file.bytes.length, executable: file.executable })),+ skipped: bundle.skipped,+ strippedRoot: bundle.strippedRoot,+ totalBytes: bundle.totalBytes,+ limits: SKILL_BUNDLE_LIMITS,+ };+}++function isInside(parent: string, child: string): boolean {+ const rel = relative(resolve(parent), resolve(child));+ return rel === "" || (!!rel && !rel.startsWith("..") && !rel.startsWith(sep) && !/^[A-Za-z]:/.test(rel));+}++/** An installed skill with the same name, anywhere the engine would load it from. */+export async function findSkillConflict(workspaceRoot: string, name: string): Promise<SkillConflict | null> {+ const flatDir = join(projectSkillsDir(workspaceRoot), name);+ const items = await listSkills(workspaceRoot, true);+ const item = items.find((skill) => skill.name === name);+ if (item) {+ const replaceable = item.scope === "project" && resolve(dirname(item.path)) === resolve(flatDir);+ return { name, path: item.path, scope: item.scope, replaceable };+ }+ // A leftover folder without a loadable SKILL.md still blocks the rename.+ if (await exists(flatDir)) return { name, path: flatDir, scope: "project", replaceable: true };+ return null;+}++async function writeTree(dir: string, files: Array<{ path: string; bytes: Buffer; executable: boolean }>): Promise<void> {+ for (const file of files) {+ const target = join(dir, ...file.path.split("/"));+ if (!isInside(dir, target)) throw invalid("invalid_skill_path", `Unsafe path in upload: ${file.path}`, [file.path]);+ await mkdir(dirname(target), { recursive: true });+ await writeFile(target, file.bytes, { flag: "wx", mode: file.executable ? 0o755 : 0o644 });+ if (file.executable && process.platform !== "win32") await chmod(target, 0o755);+ }+}++function stagingRoot(workspaceRoot: string): string {+ // Beside, not inside, the skill scan directories: a half-written skill must+ // never be visible to the engine or the Library.+ return join(workspaceRoot, ".opencode", `.skill-upload-${randomBytes(6).toString("hex")}`);+}++export async function installSkillBundle(+ workspaceRoot: string,+ bundle: PreparedSkillBundle,+ options: { onConflict: "fail" | "replace" },+): Promise<{ path: string; dir: string; action: "added" | "updated" }> {+ const skillsDir = projectSkillsDir(workspaceRoot);+ const target = join(skillsDir, bundle.name);+ const conflict = await findSkillConflict(workspaceRoot, bundle.name);+ if (conflict && (options.onConflict !== "replace" || !conflict.replaceable)) {+ throw new ApiError(+ 409,+ "skill_exists",+ conflict.replaceable+ ? `A skill named ${bundle.name} already exists in this workspace`+ : `A skill named ${bundle.name} is already installed (${conflict.scope === "global" ? "globally" : "in another skill folder"}); choose another name`,+ conflict,+ );+ }+ const staging = stagingRoot(workspaceRoot);+ const fresh = join(staging, "new");+ const previous = join(staging, "old");+ await mkdir(fresh, { recursive: true });+ try {+ await writeTree(fresh, bundle.files);+ await mkdir(skillsDir, { recursive: true });+ const replacing = await exists(target);+ if (replacing) await renameWithRetry(target, previous);+ try {+ await renameWithRetry(fresh, target);+ } catch (error) {+ if (replacing) await rename(previous, target).catch(() => undefined);+ throw error;+ }+ return { path: join(target, SKILL_MD), dir: target, action: replacing ? "updated" : "added" };+ } finally {+ await rm(staging, { recursive: true, force: true });+ }+}++// --- helper files of an installed skill --------------------------------------------++/** The directory of a project skill (the only kind the desktop edits). */+export async function resolveProjectSkillDir(workspaceRoot: string, name: string): Promise<string> {+ const trimmed = name.trim();+ validateSkillName(trimmed);+ const items = await listSkills(workspaceRoot, false);+ const item = items.find((skill) => skill.name === trimmed && skill.scope === "project");+ if (!item) throw new ApiError(404, "skill_not_found", `Skill not found in this workspace: ${trimmed}`);+ const dir = dirname(item.path);+ if (!isInside(workspaceRoot, dir)) throw new ApiError(404, "skill_not_found", `Skill not found in this workspace: ${trimmed}`);+ return dir;+}++export type SkillTreeEntry = SkillBundleFileSummary & { kind: "file" | "symlink" | "other" };++/** Lists a skill folder without following links; stops at the bundle caps. */+export async function listSkillTree(dir: string): Promise<{ files: SkillTreeEntry[]; truncated: boolean }> {+ const files: SkillTreeEntry[] = [];+ let truncated = false;+ const walk = async (current: string, depth: number): Promise<void> => {+ if (truncated) return;+ const entries = await readdir(current, { withFileTypes: true });+ entries.sort((a, b) => a.name.localeCompare(b.name));+ for (const entry of entries) {+ if (files.length >= SKILL_BUNDLE_LIMITS.maxRawEntries) {+ truncated = true;+ return;+ }+ const full = join(current, entry.name);+ const rel = relative(dir, full).split(sep).join("/");+ if (entry.isDirectory() && !entry.isSymbolicLink()) {+ if (depth < SKILL_BUNDLE_LIMITS.maxDepth) await walk(full, depth + 1);+ continue;+ }+ const info = await lstat(full);+ files.push({+ path: rel,+ size: info.size,+ executable: info.isFile() && (info.mode & 0o111) !== 0,+ kind: info.isSymbolicLink() ? "symlink" : info.isFile() ? "file" : "other",+ });+ }+ };+ await walk(dir, 1);+ files.sort((a, b) => (a.path === SKILL_MD ? -1 : b.path === SKILL_MD ? 1 : a.path.localeCompare(b.path)));+ return { files, truncated };+}++/**+ * Adds, replaces and removes helper files of an installed skill. SKILL.md is+ * edited through the skill upsert (it carries the frontmatter the engine+ * needs), so it can be neither added nor removed here.+ */+export async function updateSkillFiles(+ dir: string,+ input: { add?: unknown; remove?: unknown },+): Promise<{ added: string[]; removed: string[] }> {+ const removeRaw = input.remove === undefined ? [] : input.remove;+ if (!Array.isArray(removeRaw)) throw invalid("invalid_skill_bundle", "remove must be a list of paths");+ const remove = removeRaw.map((path) => normalizeSkillBundlePath(String(path)));+ const tree = await listSkillTree(dir);+ const existing = tree.files.filter((file) => !remove.includes(file.path));+ const { files: add } = checkFiles(input.add ?? [], { existing });+ for (const path of [...remove, ...add.map((file) => file.path)]) {+ if (path.toLowerCase() === SKILL_MD.toLowerCase()) {+ throw invalid("skill_md_protected", "Edit SKILL.md with the skill editor; it cannot be uploaded or removed as a helper file", [path]);+ }+ }+ for (const path of remove) {+ if (!tree.files.some((file) => file.path === path)) throw new ApiError(404, "skill_file_not_found", `No such file in the skill: ${path}`);+ }+ const replaced = new Set(add.map((file) => file.path.toLowerCase()));+ const kept = existing.filter((file) => !replaced.has(file.path.toLowerCase()));+ checkTotals([...kept, ...add.map((file) => ({ size: file.bytes.length }))]);++ for (const path of remove) {+ const target = join(dir, ...path.split("/"));+ if (!isInside(dir, target)) continue;+ await rm(target, { force: true });+ await pruneEmptyDirs(dir, dirname(target));+ }+ for (const file of add) {+ const target = join(dir, ...file.path.split("/"));+ if (!isInside(dir, target)) throw invalid("invalid_skill_path", `Unsafe path: ${file.path}`, [file.path]);+ // Never write through a link that sits in the way.+ for (let cursor = dirname(target); isInside(dir, cursor) && cursor !== resolve(dir); cursor = dirname(cursor)) {+ const info = await lstat(cursor).catch(() => null);+ if (info?.isSymbolicLink()) throw invalid("invalid_skill_path", `A folder on this path is a link: ${file.path}`, [file.path]);+ }+ // Replacing "scripts/Run.sh" with "scripts/run.sh" must not leave both behind on a case-sensitive disk.+ const caseVariant = tree.files.find((entry) => entry.path !== file.path && entry.path.toLowerCase() === file.path.toLowerCase());+ if (caseVariant?.kind === "file") await rm(join(dir, ...caseVariant.path.split("/")), { force: true });+ const info = await lstat(target).catch(() => null);+ if (info && !info.isFile()) throw invalid("invalid_skill_path", `Cannot replace a non-file: ${file.path}`, [file.path]);+ if (info) await rm(target, { force: true });+ await mkdir(dirname(target), { recursive: true });+ await writeFile(target, file.bytes, { flag: "wx", mode: file.executable ? 0o755 : 0o644 });+ if (file.executable && process.platform !== "win32") await chmod(target, 0o755);+ }+ return { added: add.map((file) => file.path), removed: remove };+}++async function pruneEmptyDirs(root: string, from: string): Promise<void> {+ for (let cursor = from; isInside(root, cursor) && resolve(cursor) !== resolve(root); cursor = dirname(cursor)) {+ const entries = await readdir(cursor).catch(() => null);+ if (!entries || entries.length) return;+ await rmdir(cursor).catch(() => undefined);+ }+}++/** Reads a JSON request body, refusing one larger than `maxBytes` before buffering it all. */+export async function readBoundedJsonBody(request: Request, maxBytes: number): Promise<Record<string, unknown>> {+ const tooLarge = () => new ApiError(413, "skill_bundle_too_large", `Upload is larger than ${formatBytes(maxBytes)}`);+ const declared = Number(request.headers.get("content-length"));+ if (Number.isFinite(declared) && declared > maxBytes) throw tooLarge();+ if (!request.body) throw new ApiError(400, "invalid_json", "Invalid JSON body");+ const reader = request.body.getReader();+ const chunks: Uint8Array[] = [];+ let size = 0;+ for (;;) {+ const { done, value } = await reader.read();+ if (done) break;+ size += value.byteLength;+ if (size > maxBytes) {+ await reader.cancel().catch(() => undefined);+ throw tooLarge();+ }+ chunks.push(value);+ }+ try {+ const parsed: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8"));+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("not an object");+ return parsed as Record<string, unknown>;+ } catch {+ throw new ApiError(400, "invalid_json", "Invalid JSON body");+ }+}