GitHub 0
Skip to content

Sync

Run the sync engine with a plan and a SyncHost, preview it with a dry run, and read the report and the state it keeps.

sync(places, host, options?) runs one sync. It fetches every source in the plan, links their skills and files into each agent's folders, merges their MCP servers and hooks into each agent's config, runs the plan's hooks, and returns a report.

import { sync } from "skillrc/sync";

const report = await sync(places, host);
if (!report.ok) console.error(report.errors.join("\n"));
ts

skillrc doesn't read your program's config. host does: it loads the plan, says where each agent keeps its skills, and names the command your users run. The engine does the rest.

A Plan is the list of sources to sync, read from your program's config. Your program parses its own format and builds one.

import type { GitSource, LocalSource, Plan } from "skillrc/sync";

const file = "/home/me/.config/mytool/mytool.json";
const team: GitSource = {
  kind: "git",
  id: "team",
  git: "https://github.com/acme/skills.git",
  skills: "skills/*",
  update: "auto",
  origin: file,
};
const mine: LocalSource = {
  kind: "local",
  id: "mine",
  local: "/home/me/skills",
  adopt: false,
  update: "auto",
  origin: file,
};

const plan: Plan = {
  root: { file, agents: "all", sources: [team, mine], forks: {}, hooks: [] },
  sources: [team, mine],
  warnings: [],
};
ts

root is what the user's own file says. sources is every source the sync runs: the root's first, then those of any file the root extends. failed lists extended files that didn't load; while it is set, sources missing from the plan keep what they last linked instead of being removed.

KindFieldWhat it is
gitgit, ref?A repository skillrc keeps a copy of, fetched through the git cache
npmnpm, registry?A package from a registry, unpacked into the store
indexindexA site that serves /.well-known/agent-skills/index.json
clonegit, ref?, pathA repository you work in, updated by fetch and fast-forward and linked in place
locallocalA folder on this machine, linked in place

git, npm and index sources are vendored: their skills are copied into <state>/store/<id> and linked from there. clone and local sources are linked where they are, so an edit shows in every agent at once.

Every source has an id, an update mode and the origin file that declared it. The rest is optional:

FieldWhat it does
skillsA glob of skill folders inside the source, such as skills/*
include, excludeSkill names to keep or drop
filesInstructions, subagents, commands, mcp and agentHooks files the source gives agents, as a glob per agent id
update"auto" applies upstream changes; "review" holds them until a sync passes apply with this source's id

Only the root plan's sources may give mcp and agentHooks files, since an MCP server or a hook runs a command. See Definitions.

Two sources with a skill of the same name: the first one wins, and the report lists the other under shadowed. Names that differ only in case or Unicode form count as one, since macOS and Windows keep them in one folder.

A plan hook runs a shell command after a sync changed files that match its patterns. Each pattern is <source id>:<glob>, or *:<glob> for any source.

const hooks = [{ name: "reindex", run: "mytool-index", on: ["team:skills/**"] }];
ts

The command gets the report as JSON on stdin, <PROGRAM>_HOOK with the hook's name and <PROGRAM>_CHANGED with the matched <source>:<file> lines. It is stopped after 5 minutes, and a non-zero exit is an error in the report. A sync stopped before its hooks ran leaves them due, and the next sync runs them.

SyncHost is everything the engine asks of your program.

MemberWhat it does
load(ctx)Reads the plan and returns { plan, name }. It runs inside the lock. name is what the report's manifest says. Throw to stop the sync.
targets(agents, signal)The global skills folders the plan's agents read, as { dir, agents, aliases? }
formerTargets(targets)Folders that are no longer targets, where links made earlier may remain
replacementTargets?(targets)For each former folder, the targets that took its place
kindsWhere each agent reads instructions, subagents, commands, MCP servers and hooks, by agent id
notes?(plan, state)Warnings a real sync adds, such as a timer whose schedule drifted
hookEnv?(hook, changed)More variables for a plan hook's command
beforeReport?(plan, signal)Runs under the lock, after the hooks and before the report is saved and sent
commandThe command messages tell the user to run, such as mytool
versionYour program's version, written into the report

load gets offline, dryRun, signal, warnings to push to, only when one source was asked for, and cacheRoot in a dry run. Fetch any remote file the plan extends into cacheRoot then, so the real cache doesn't move.

Skills folders are found by the agents. An agent with no entry in kinds still gets its skills, but no files, servers or hooks.

const host: SyncHost = {
  load: async () => ({ plan, name: file }),
  targets: async () => [
    { dir: "/home/me/.claude/skills", agents: ["claude-code"] },
    { dir: "/home/me/.agents/skills", agents: ["codex", "opencode"] },
  ],
  formerTargets: () => [],
  kinds: {
    codex: {
      home: () => "/home/me/.codex",
      mcp: { file: () => "/home/me/.codex/config.toml", format: "toml", key: ["mcp_servers"] },
    },
  },
  command: "mytool",
  version: "1.0.0",
};
ts
  1. Takes the sync lock, <state>/sync.lock. Another live sync makes it throw LockHeldError; a stale lock is broken and the report says why.
  2. Calls host.load and reads state.json.
  3. Fetches each source in order, and saves the state after each one. A sync stopped later leaves nothing in the store that state.json doesn't record.
  4. Links each winning skill into every target folder. A folder that can't be written doesn't stop the others.
  5. Removes links it made in former folders, and keeps a skill there while a folder that replaced it has no usable copy.
  6. Links instructions, subagents and commands, then merges MCP servers and hooks into each agent's config.
  7. Saves the state, runs the plan's hooks, calls beforeReport, and writes <state>/last-run.json.
  8. Runs the plan's report.run command with the report on stdin, and posts the report to hub when the plan names one.

A whole sync is stopped after 60 minutes. The error names the step it was on.

OptionWhat it does
dryRunReports what the sync would do and writes nothing
offlineFetches nothing and syncs from what is on disk
onlyFetches only this source; the others link from disk
applyApplies the held updates of this review source
copyCopies skills into agents' folders instead of linking them

copy is for agents that skip a skill whose SKILL.md is a symlink. Codex and Cursor do. A copy that someone edits counts as an edit of the skill: the next sync moves the change back to where the skill came from.

const preview = await sync(places, host, { dryRun: true });
console.log(preview.skills.added, preview.skills.removed);
ts

A dry run takes no lock and writes nothing outside a scratch folder. It fetches into a temporary cache, so the real cache never moves past what was synced. With offline too, it reads the real cache instead, since a scratch one would be empty.

It fails where the real run would. A source that can't be fetched is an error in both. Where the preview can't model a change in full, such as an edited copy that a real sync would move back first, previewLimitations in the report says so.

sync returns a Report, and a real sync also writes it to <state>/last-run.json.

FieldWhat it holds
okNo errors
sourcesEach source's status: installed, updated, unchanged, held, review, error or offline
skillstotal, names, and what was added, removed and updated
filesFiles linked into agents' folders, and those left alone because a person made them
entriesMCP servers and hooks merged, held because someone edited them, or skipped because the file was locked
pendingUpdates not applied: waiting for review, held for a local edit, or made to a skill you forked
shadowed, renamedSkills a former source still had, and skills renamed upstream in case only
hooksEach plan hook that ran, its exit code and the end of its output
warnings, errorsWhat to tell the user

errors means something was not synced. warnings means it was, with something to know.

<state>/state.json records what each source last installed: its commit or version, each skill's hashes, the links and the config entries skillrc owns. It is how a sync tells an upstream change from a local edit, and which config entries are its own to change.

import { loadState, saveState } from "skillrc/sync";

const state = loadState(places);
console.log(Object.keys(state.skills));
ts

A state.json that doesn't parse, or that another version wrote, reads as empty. A sync then rebuilds what it can from the store, and treats vendored copies it can't vouch for as edited, so nothing local is overwritten. With backupInvalid, the old file is copied to <state>/backup first.

Commands that change the state between syncs, such as a fork or a merge, run under the same lock:

import { withSyncLock } from "skillrc/sync";

await withSyncLock(places, "merge", async () => {
  // read state.json, change it, save it
});
ts

withSyncLock waits up to 10 seconds for a running sync, then throws a UserError that says to try again. A sync that the command runs inside it shares the lock.