Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(init): only report and send skills this run wrote

Shrish Dwivedi3e3107b80d68
diff --git a/src/cloud/pair.ts b/src/cloud/pair.ts
index 4fba83ba..b94a0a72 100644
--- a/src/cloud/pair.ts
+++ b/src/cloud/pair.ts
@@ -15,11 +15,12 @@
import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
+import { join, resolve } from "node:path";
import { graftCliPath } from "../claude/paths.js";
import { noteAuthor } from "../notes/git-facts.js";
import { repoName, repoPlace } from "../notes/home.js";
import type { SavedSkill } from "../skills/found-skill.js";
-import { skillName } from "../skills/skills.js";
+import { skillDirs, skillName } from "../skills/skills.js";
import { matter } from "../util/front-matter.js";
import { newState, pickerRepos } from "./nudge.js";
import type { TeammateSummary, TokensSummary } from "./onboarding.js";
@@ -51,8 +52,15 @@ export interface PairFindings {
teammates?: TeammateSummary[];
}
-/** One team skill as a found item, or nothing when its file can't be read. */
+/**
+ * One team skill init just wrote as a found item. Nothing for a skill that was
+ * already there, a skill kept for the person alone, or a file that isn't under
+ * the repo's own `.claude/skills/`: only what init wrote into the repo goes up.
+ */
function foundSkillItem(checkout: string, s: SavedSkill): Record<string, unknown>[] {
+ const name = skillName(s.skill.name);
+ const expected = join(skillDirs(checkout).repo, name, "SKILL.md");
+ if (!s.created || s.skill.for !== "team" || !name || resolve(s.path) !== resolve(expected)) return [];
let text: string;
try {
text = readFileSync(s.path, "utf8");
@@ -62,7 +70,7 @@ function foundSkillItem(checkout: string, s: SavedSkill): Record<string, unknown
return [
{
kind: "skill",
- path: `.claude/skills/${skillName(s.skill.name)}/SKILL.md`,
+ path: `.claude/skills/${name}/SKILL.md`,
title: s.skill.title,
author: noteAuthor(checkout),
body: matter(text).content.trim(),
@@ -156,7 +164,7 @@ export class PairRun {
...learningsPayload(place.checkout)
.filter((l) => l.ref === "")
.map((l) => ({ kind: "decision", path: l.path, title: l.title, author: l.author, body: l.body, hash: l.hash, touches: l.touches })),
- ...skills.filter((s) => s.skill.for === "team").flatMap((s) => foundSkillItem(place.checkout, s)),
+ ...skills.flatMap((s) => foundSkillItem(place.checkout, s)),
]
: [];
const n = found.length;
diff --git a/src/notes/cold-start.ts b/src/notes/cold-start.ts
index 5f234195..59fb1c0d 100644
--- a/src/notes/cold-start.ts
+++ b/src/notes/cold-start.ts
@@ -15,13 +15,13 @@ import { spawn, spawnSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { basename, dirname, join, resolve, sep } from "node:path";
+import { saveFoundSkill, type FoundSkill, type SavedSkill } from "../skills/found-skill.js";
+import { OWN_SKILLS, skillName } from "../skills/skills.js";
import { countBucket, durationBucket } from "../telemetry/contract.js";
import { track } from "../telemetry/track.js";
import { readJson, writeJsonAtomic } from "../util/state.js";
import { noteAuthor } from "./git-facts.js";
import { ensureRepoHome } from "./home.js";
-import { saveFoundSkill, type FoundSkill, type SavedSkill } from "../skills/found-skill.js";
-import { OWN_SKILLS, skillName } from "../skills/skills.js";
import { mentionedFiles, shortDate, writeNote, type Note } from "./notes.js";
import { claudeProjectDir } from "./session-cost.js";
@@ -669,12 +669,17 @@ export function saveFindings(repo: string, findings: Finding[], author = noteAut
);
}
-/** Each found skill as a SKILL.md: the team's in the repo, the person's own in ~/.trail. One that can't be written is skipped. */
+/**
+ * Each found skill as a SKILL.md: the team's in the repo, the person's own in
+ * ~/.trail. Only the ones this call wrote come back: a skill of that name that
+ * was already there is left alone, and one that can't be written is skipped.
+ */
export function saveSkills(repo: string, skills: FoundSkill[]): SavedSkill[] {
const out: SavedSkill[] = [];
for (const skill of skills) {
try {
- out.push({ skill, path: saveFoundSkill(repo, skill) });
+ const { path, created } = saveFoundSkill(repo, skill);
+ if (created) out.push({ skill, path, created });
} catch {
/* read-only checkout, say: the learnings still saved */
}
diff --git a/src/skills/found-skill.ts b/src/skills/found-skill.ts
index a658a1b3..f09ac0d6 100644
--- a/src/skills/found-skill.ts
+++ b/src/skills/found-skill.ts
@@ -24,10 +24,11 @@ export interface FoundSkill {
quotes: string[];
}
-/** A skill as written: where its SKILL.md is, whether this run wrote it or it was there already. */
+/** A skill's SKILL.md: where it is, and whether this run wrote it (`false`: one of that name was already there). */
export interface SavedSkill {
skill: FoundSkill;
path: string;
+ created: boolean;
}
/** SKILL.md for a found skill: the frontmatter every skill has, then its steps as a numbered list. */
@@ -38,21 +39,23 @@ export function renderFoundSkill(s: FoundSkill): string {
}
/**
- * Write the skill, and return the path of its SKILL.md. A skill of that name
- * the repo or this machine already has is left as it is: its path comes back.
+ * Write the skill, and return the path of its SKILL.md and whether this call
+ * wrote it. A skill of that name the repo or this machine already has is left
+ * as it is: its path comes back with `created: false`, and it isn't ours to
+ * report or send anywhere.
*/
-export function saveFoundSkill(repo: string, s: FoundSkill): string {
+export function saveFoundSkill(repo: string, s: FoundSkill): { path: string; created: boolean } {
const name = skillName(s.name);
if (!name) throw new Error("a skill needs a name");
const dirs = skillDirs(repo);
for (const d of [dirs.repo, dirs.home]) {
const there = join(d, name, "SKILL.md");
- if (existsSync(there)) return there;
+ if (existsSync(there)) return { path: there, created: false };
}
if (s.for === "you") ensureRepoHome(repo);
const dir = join(s.for === "you" ? dirs.home : dirs.repo, name);
mkdirSync(dir, { recursive: true });
const file = join(dir, "SKILL.md");
writeFileSync(file, renderFoundSkill(s));
- return file;
+ return { path: file, created: true };
}
diff --git a/src/skills/skills.ts b/src/skills/skills.ts
index 1de5304a..9f99f918 100644
--- a/src/skills/skills.ts
+++ b/src/skills/skills.ts
@@ -24,7 +24,7 @@ import { shortDate, slug } from "../notes/notes.js";
import { ensureRepoHome, repoPlace } from "../notes/home.js";
/** Trail's and graft's own skills, rewritten by every `init`: never a place for takeaways. */
-export const OWN_SKILLS =new Set(["trail", "graft"]);
+export const OWN_SKILLS = new Set(["trail", "graft"]);
/** Where a repo's skills are: the team's in the repo, and this machine's in ~/.trail. */
export function skillDirs(dir: string): { repo: string; home: string } {
diff --git a/test/cold-start-skills.test.ts b/test/cold-start-skills.test.ts
index eb792107..129326fb 100644
--- a/test/cold-start-skills.test.ts
+++ b/test/cold-start-skills.test.ts
@@ -10,8 +10,8 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileS
import { tmpdir } from "node:os";
import { join } from "node:path";
import { PairRun } from "../src/cloud/pair.js";
-import { finishPastRead, findLearnings, type PastSession } from "../src/notes/cold-start.js";
-import { saveFoundSkill, type FoundSkill } from "../src/skills/found-skill.js";
+import { finishPastRead, findLearnings, saveSkills, type PastSession } from "../src/notes/cold-start.js";
+import { saveFoundSkill, type FoundSkill, type SavedSkill } from "../src/skills/found-skill.js";
import { getSkill, listSkills, skillDirs } from "../src/skills/skills.js";
import { writeNote } from "../src/notes/notes.js";
import { tmpRepo } from "./helpers.js";
@@ -147,15 +147,17 @@ test("saving a skill never overwrites one of that name, the team's or this machi
const theirs = join(repo, ".claude", "skills", "cut-a-release");
mkdirSync(theirs, { recursive: true });
writeFileSync(join(theirs, "SKILL.md"), "---\nname: cut-a-release\ndescription: theirs\n---\n\nTheirs.\n");
- assert.equal(saveFoundSkill(repo, found), join(theirs, "SKILL.md"));
+ assert.deepEqual(saveFoundSkill(repo, found), { path: join(theirs, "SKILL.md"), created: false });
assert.match(readFileSync(join(theirs, "SKILL.md"), "utf8"), /Theirs\./);
// The same name for you finds the team's, and leaves it be.
- assert.equal(saveFoundSkill(repo, { ...found, for: "you" }), join(theirs, "SKILL.md"));
+ assert.deepEqual(saveFoundSkill(repo, { ...found, for: "you" }), { path: join(theirs, "SKILL.md"), created: false });
// Saved once for you, the second save returns the first's path.
const other: FoundSkill = { ...found, name: "ship it", for: "you" };
- const first = saveFoundSkill(repo, other);
+ const wrote = saveFoundSkill(repo, other);
+ assert.equal(wrote.created, true);
+ const first = wrote.path;
writeFileSync(first, "edited by hand");
- assert.equal(saveFoundSkill(repo, other), first);
+ assert.deepEqual(saveFoundSkill(repo, other), { path: first, created: false });
assert.equal(readFileSync(first, "utf8"), "edited by hand");
});
@@ -169,7 +171,7 @@ test("init --pair hands over found items with a kind: learnings are decisions, s
writeNote(dir, { title: "Keep payment retries at 4", body: "## Decided\nFour retries.", author: "Sam", date: "2026-10-07", touches: [] });
const team: FoundSkill = { name: "cut a release", title: "Cut a release", for: "team", steps: STEPS, sessions: ["a", "b"], quotes: [] };
const mine: FoundSkill = { name: "my own habit", title: "My own habit", for: "you", steps: STEPS, sessions: ["a", "b"], quotes: [] };
- const saved = [team, mine].map((skill) => ({ skill, path: saveFoundSkill(dir, skill) }));
+ const saved = [team, mine].map((skill) => ({ skill, ...saveFoundSkill(dir, skill) }));
const calls: { path: string; body: Record<string, unknown> }[] = [];
const fetchImpl = (async (url: string | URL, init?: RequestInit) => {
@@ -188,3 +190,62 @@ test("init --pair hands over found items with a kind: learnings are decisions, s
assert.match(String(skill.body), /1\. Run `npm run build`/);
assert.equal(typeof skill.hash, "string");
});
+
+/** What init --pair sends as found items for `saved`, given a repo with one learning. */
+async function pairFound(dir: string, saved: SavedSkill[]): Promise<Record<string, unknown>[]> {
+ process.env.DO_NOT_TRACK = "1";
+ process.env.TRAIL_URL = "http://trail.test";
+ const calls: { path: string; body: Record<string, unknown> }[] = [];
+ const fetchImpl = (async (url: string | URL, init?: RequestInit) => {
+ const u = new URL(String(url));
+ calls.push({ path: u.pathname, body: init?.body ? JSON.parse(String(init.body)) : {} });
+ return u.pathname === "/api/cli/handoffs" ? new Response(JSON.stringify({ url: "http://trail.test/c" }), { status: 201 }) : new Response("{}", { status: 202 });
+ }) as typeof fetch;
+ const run = await PairRun.start(dir, "X4GM5OQU4NIA65ZR", { agent: "claude", version: "0.23.1", fetchImpl });
+ assert.ok(run instanceof PairRun);
+ await run.finish(dir, saved);
+ return calls.filter((c) => c.path === "/api/cli/handoffs/progress").flatMap((c) => (c.body.found as Record<string, unknown>[] | undefined) ?? []);
+}
+
+function pairRepo(tag: string): string {
+ process.env.TRAIL_HOME = mkdtempSync(join(tmpdir(), "skills-collide-"));
+ const dir = tmpRepo(tag);
+ spawnSync("git", ["init", "-q", "-b", "main"], { cwd: dir });
+ spawnSync("git", ["remote", "add", "origin", "https://github.com/acme/billing.git"], { cwd: dir });
+ writeNote(dir, { title: "Keep payment retries at 4", body: "## Decided\nFour retries.", author: "Sam", date: "2026-10-07", touches: [] });
+ return dir;
+}
+
+const COLLIDING: FoundSkill = { name: "cut a release", title: "Cut a release", for: "team", steps: STEPS, sessions: ["a", "b"], quotes: [] };
+
+test("a team skill named like one of this machine's skills sends nothing and leaves that skill alone", async () => {
+ const dir = pairRepo("pair-collide-home");
+ const mine = join(skillDirs(dir).home, "cut-a-release");
+ mkdirSync(mine, { recursive: true });
+ const personal = "---\nname: cut-a-release\ndescription: personal\n---\n\nMy private steps, with a secret.\n";
+ writeFileSync(join(mine, "SKILL.md"), personal);
+
+ const saved = saveSkills(dir, [COLLIDING]);
+ assert.deepEqual(saved, [], "not written, so not reported");
+ assert.equal(existsSync(join(dir, ".claude")), false, "nothing written into the repo");
+ assert.equal(readFileSync(join(mine, "SKILL.md"), "utf8"), personal);
+
+ // Even handed the existing path by a caller, the personal file never goes up.
+ const found = await pairFound(dir, [{ skill: COLLIDING, path: join(mine, "SKILL.md"), created: true }, ...saved]);
+ assert.deepEqual(found.map((f) => f.kind), ["decision"]);
+ assert.doesNotMatch(JSON.stringify(found), /private steps/);
+});
+
+test("a team skill named like one the repo already has is not reported as found, and the file is untouched", async () => {
+ const dir = pairRepo("pair-collide-repo");
+ const theirs = join(dir, ".claude", "skills", "cut-a-release");
+ mkdirSync(theirs, { recursive: true });
+ const text = "---\nname: cut-a-release\ndescription: theirs\n---\n\nA teammate's steps.\n";
+ writeFileSync(join(theirs, "SKILL.md"), text);
+
+ const saved = saveSkills(dir, [COLLIDING]);
+ assert.deepEqual(saved, []);
+ assert.equal(readFileSync(join(theirs, "SKILL.md"), "utf8"), text);
+ const found = await pairFound(dir, [{ skill: COLLIDING, path: join(theirs, "SKILL.md"), created: false }]);
+ assert.deepEqual(found.map((f) => f.kind), ["decision"]);
+});