Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat(cloud): upload the repo's skills as learnings of kind skill
Shrish Dwivedif5630ea582aa
diff --git a/src/cloud/pair.ts b/src/cloud/pair.tsindex b94a0a72..00ebd917 100644--- a/src/cloud/pair.ts+++ b/src/cloud/pair.ts@@ -122,7 +122,7 @@ export class PairRun {const current = place.git ? { key: place.key, name: repoName(place.key, place.checkout) } : null;// This repo first, with the learnings it already has: init hasn't told// Trail about it yet, so the list of known repos may not have it.- const here = place.git ? learningsPayload(place.checkout).filter((l) => l.ref === "").length : 0;+ const here = place.git ? learningsPayload(place.checkout).filter((l) => l.ref === "" && l.kind !== "skill").length : 0;const known = pickerRepos().filter((r) => r.key !== current?.key);if (current) known.unshift({ ...current, learnings: here });const reg = await registerHandoff(@@ -162,7 +162,7 @@ export class PairRun {const found: Record<string, unknown>[] = place.git? [...learningsPayload(place.checkout)- .filter((l) => l.ref === "")+ .filter((l) => l.ref === "" && l.kind !== "skill").map((l) => ({ kind: "decision", path: l.path, title: l.title, author: l.author, body: l.body, hash: l.hash, touches: l.touches })),...skills.flatMap((s) => foundSkillItem(place.checkout, s)),]diff --git a/src/cloud/workspace-sync.ts b/src/cloud/workspace-sync.tsindex a9bf5e36..98073951 100644--- a/src/cloud/workspace-sync.ts+++ b/src/cloud/workspace-sync.ts@@ -19,13 +19,15 @@*/import { createHash } from "node:crypto";import { spawn } from "node:child_process";-import { appendFileSync, existsSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";-import { basename, join } from "node:path";+import { appendFileSync, existsSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";+import { basename, join, sep } from "node:path";import { graftCliPath } from "../claude/paths.js";import { readBranchCache, refreshBranches } from "../notes/branches.js";import { knownRepos, repoName, repoPlace, trailHome } from "../notes/home.js";import { LEARNINGS_DIR, learningFiles, readLearningFile } from "../notes/repo-trail.js";import { parseNote } from "../notes/notes.js";+import { OWN_SKILLS, skillDirs } from "../skills/skills.js";+import { matter } from "../util/front-matter.js";import { cacheDir, readJson, writeJsonAtomic } from "../util/state.js";import { isError, postActivity, readCloud, uploadLearnings, type ActivityEvent, type ApiError, type Cloud, type UploadedLearning } from "./workspace.js";@@ -39,10 +41,65 @@ function sha256(text: string): string {return createHash("sha256").update(text).digest("hex");}+/** A skill folder name safe to send as a path part. */+const SKILL_FOLDER = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;++function inside(root: string, p: string): boolean {+ return p === root || p.startsWith(root + sep);+}++/**+ * The repo's skills, as the workspace takes them: each `.claude/skills/<name>/SKILL.md`+ * in the working tree. Same item as the one init sends for a skill it just found+ * (the server keeps one per repo and path): the body is SKILL.md without its+ * frontmatter, the hash is of the whole file. A symlinked skill is read only when+ * it resolves inside the checkout. A SKILL.md that can't be read or parsed is+ * skipped. Skills kept in ~/.trail are never sent.+ */+function skillItems(checkout: string): UploadedLearning[] {+ const dir = skillDirs(checkout).repo;+ let root: string;+ let names: string[];+ try {+ root = realpathSync(join(dir, "..", ".."));+ names = readdirSync(dir).sort();+ } catch {+ return [];+ }+ const out: UploadedLearning[] = [];+ for (const name of names) {+ if (!SKILL_FOLDER.test(name) || OWN_SKILLS.has(name)) continue;+ try {+ const file = join(dir, name, "SKILL.md");+ if (!statSync(join(dir, name)).isDirectory() || !statSync(file).isFile()) continue;+ if (!inside(root, realpathSync(join(dir, name))) || !inside(root, realpathSync(file))) continue;+ const text = readFileSync(file, "utf8");+ const parsed = matter(text);+ const d = parsed.data as Record<string, unknown>;+ const description = typeof d.description === "string" ? (d.description.trim().split("\n")[0] ?? "").trim() : "";+ out.push({+ path: `.claude/skills/${name}/SKILL.md`,+ kind: "skill",+ ref: "",+ title: description || name,+ author: typeof d.author === "string" ? d.author.trim() : "",+ date: "",+ touches: [],+ body: parsed.content.trim(),+ hash: sha256(text),+ });+ } catch {+ /* no SKILL.md, or one that can't be read or parsed */+ }+ }+ return out;+}+/*** Every learning in the repo's `.trail/`, as the workspace takes them: the* working tree's, then the ones only a pushed branch has (from the branch- * cache). Paths are repo-relative; nothing about this machine is sent.+ * cache), then the repo's skills. Paths are repo-relative; nothing about this+ * machine is sent.*/export function learningsPayload(checkout: string): UploadedLearning[] {const out: UploadedLearning[] = [];@@ -71,6 +128,7 @@ export function learningsPayload(checkout: string): UploadedLearning[] {if (text) add(name, text, "");}for (const b of readBranchCache(checkout)?.branches ?? []) for (const l of b.learnings) add(l.name, l.text, b.ref);+ out.push(...skillItems(checkout));return out;}diff --git a/src/cloud/workspace.ts b/src/cloud/workspace.tsindex 8a79ce98..adffb630 100644--- a/src/cloud/workspace.ts+++ b/src/cloud/workspace.ts@@ -225,8 +225,10 @@ export function addRepo(c: Cloud, repo: { key: string; name: string }, fetchImpl}export interface UploadedLearning {- /** `.trail/learnings/<name>`. */+ /** `.trail/learnings/<name>`, or `.claude/skills/<name>/SKILL.md` for a skill. */path: string;+ /** "skill" for a skill of the repo; a plain learning leaves it out. */+ kind?: "skill";/** "" for the working tree, or the pushed branch it was found on (`origin/kumar/x`). */ref: string;branch?: string;diff --git a/test/workspace-sync-skills.test.ts b/test/workspace-sync-skills.test.tsnew file mode 100644index 00000000..2ef3f6a9--- /dev/null+++ b/test/workspace-sync-skills.test.ts@@ -0,0 +1,107 @@+/**+ * The repo's committed skills go up to the workspace with its learnings, so+ * the team page can show them. Only the working tree's `.claude/skills/`:+ * never a skill kept on this machine, never one that points outside the repo.+ */+import { test } from "node:test";+import assert from "node:assert/strict";+import { createHash } from "node:crypto";+import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { BRAND_ENV } from "../src/brand.js";+import { learningsPayload } from "../src/cloud/workspace-sync.js";+import { skillDirs } from "../src/skills/skills.js";++process.env.DO_NOT_TRACK = "1";+process.env[BRAND_ENV] = "trail";+process.env.TRAIL_HOME = mkdtempSync(join(tmpdir(), "skills-sync-home-"));++const sha = (t: string) => createHash("sha256").update(t).digest("hex");++const HOTFIX = `---+name: release-hotfix+description: Cut a hotfix release from a tagged commit+author: Anirudh+---++# Release a hotfix++1. Branch from the tag.+2. Cherry-pick the fix.+`;++function checkout(): string {+ return mkdtempSync(join(tmpdir(), "skills-sync-repo-"));+}++function writeSkill(root: string, name: string, text: string): void {+ mkdirSync(join(root, name), { recursive: true });+ writeFileSync(join(root, name, "SKILL.md"), text);+}++const skillItems = (dir: string) => learningsPayload(dir).filter((l) => l.kind === "skill");++test("the repo's skill goes up as a skill learning, and a skill kept on this machine does not", () => {+ const dir = checkout();+ const dirs = skillDirs(dir);+ writeSkill(dirs.repo, "release-hotfix", HOTFIX);+ writeSkill(dirs.home, "my-private-habit", "---\ndescription: Only mine\n---\n\nSecret steps.\n");++ const items = skillItems(dir);+ assert.equal(items.length, 1);+ const s = items[0]!;+ assert.equal(s.path, ".claude/skills/release-hotfix/SKILL.md");+ assert.equal(s.kind, "skill");+ assert.equal(s.title, "Cut a hotfix release from a tagged commit");+ assert.equal(s.body, "# Release a hotfix\n\n1. Branch from the tag.\n2. Cherry-pick the fix.");+ assert.equal(s.hash, sha(HOTFIX));+ assert.equal(s.author, "Anirudh");+ assert.equal(s.ref, "");+ assert.deepEqual(s.touches, []);+ assert.ok(!learningsPayload(dir).some((l) => l.path.includes("my-private-habit")));+});++test("a skill without a description is titled by its folder, and plain learnings carry no kind", () => {+ const dir = checkout();+ writeSkill(skillDirs(dir).repo, "pdf-coords", "---\nname: pdf-coords\n---\n\nUse points, not pixels.\n");+ const [s] = skillItems(dir);+ assert.equal(s!.title, "pdf-coords");+ assert.equal(s!.author, "");+ assert.ok(learningsPayload(dir).every((l) => l.kind === undefined || l.kind === "skill"));+});++test("editing a skill changes its hash in the payload", () => {+ const dir = checkout();+ const repo = skillDirs(dir).repo;+ writeSkill(repo, "release-hotfix", HOTFIX);+ const before = skillItems(dir)[0]!;+ writeSkill(repo, "release-hotfix", HOTFIX + "3. Tag the release.\n");+ const after = skillItems(dir)[0]!;+ assert.notEqual(before.hash, after.hash);+ assert.match(after.body, /Tag the release/);+});++test("a SKILL.md that can't be parsed is skipped, and the others still go up", () => {+ const dir = checkout();+ const repo = skillDirs(dir).repo;+ writeSkill(repo, "broken", "---\ndescription: [unclosed\n - : :\n---\n\nbody\n");+ writeSkill(repo, "release-hotfix", HOTFIX);+ const items = skillItems(dir);+ assert.deepEqual(items.map((i) => i.path), [".claude/skills/release-hotfix/SKILL.md"]);+});++test("a skill folder that points outside the repo is skipped, one that points inside is kept", () => {+ const dir = checkout();+ const outside = mkdtempSync(join(tmpdir(), "skills-sync-outside-"));+ writeSkill(outside, "stolen", "---\ndescription: Not in this repo\n---\n\nOutside text.\n");+ const repo = skillDirs(dir).repo;+ mkdirSync(repo, { recursive: true });+ symlinkSync(join(outside, "stolen"), join(repo, "stolen"), "dir");+ mkdirSync(join(repo, "viafile"), { recursive: true });+ symlinkSync(join(outside, "stolen", "SKILL.md"), join(repo, "viafile", "SKILL.md"));+ writeSkill(join(dir, ".agents", "skills"), "shared", "---\ndescription: Shared across agents\n---\n\nSteps.\n");+ symlinkSync(join(dir, ".agents", "skills", "shared"), join(repo, "shared"), "dir");++ assert.deepEqual(skillItems(dir).map((i) => i.path), [".claude/skills/shared/SKILL.md"]);+});