Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(skillpack): sync registers the skills it scaffolded in manifest.json (#4831)

time-attack41ca1e3c776c
diff --git a/src/commands/skillpack.ts b/src/commands/skillpack.ts
index 232201e6be..b95f8c849e 100644
--- a/src/commands/skillpack.ts
+++ b/src/commands/skillpack.ts
@@ -27,6 +27,7 @@ import { runMigrateFence } from '../core/skillpack/migrate-fence.ts';
import { runScrubLegacy } from '../core/skillpack/scrub-legacy.ts';
import { runHarvest, HarvestError } from '../core/skillpack/harvest.ts';
import { computeSkillCurrency } from '../core/skillpack/skill-currency.ts';
+import { registerManifestEntries } from '../core/skill-manifest.ts';
import { parseSkillFrontmatter } from '../core/skill-frontmatter.ts';
import { parsePrecondition, type Precondition } from '../core/skillpack/preconditions.ts';
import { resolveAbs, findGbrainOrDie, resolveWorkspace } from './skillpack/shared.ts';
@@ -399,9 +400,10 @@ async function cmdSync(args: string[]): Promise<void> {
runScaffold({ gbrainRoot, targetWorkspace, skillSlug: slug, dryRun });
scaffolded.push(slug);
}
+ const registered = dryRun ? [] : registerManifestEntries(join(targetWorkspace, 'skills'), scaffolded);
if (json) {
- console.log(JSON.stringify({ scaffolded, drifted: driftedSlugs, dryRun }, null, 2));
+ console.log(JSON.stringify({ scaffolded, registered, drifted: driftedSlugs, dryRun }, null, 2));
process.exit(0);
}
@@ -409,6 +411,7 @@ async function cmdSync(args: string[]): Promise<void> {
console.log(
`${verb} ${scaffolded.length} new skill(s)${scaffolded.length ? ': ' + scaffolded.join(', ') : ''}`,
);
+ if (registered.length) console.log(`Registered ${registered.length} of them in skills/manifest.json.`);
if (driftedSlugs.length) {
console.log(
`${driftedSlugs.length} skill(s) drifted from the bundle — run \`gbrain skillpack reference <slug>\` to review (not overwritten).`,
diff --git a/src/core/skill-manifest.ts b/src/core/skill-manifest.ts
index 8df681c59b..4ccc035e40 100644
--- a/src/core/skill-manifest.ts
+++ b/src/core/skill-manifest.ts
@@ -27,6 +27,7 @@
import { existsSync, readFileSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
+import { atomicWriteFileSync } from './atomic-write.ts';
export interface ManifestEntry {
name: string;
@@ -144,3 +145,22 @@ export function loadOrDeriveManifest(skillsDir: string): ManifestLoadResult {
return { skills: deriveManifest(skillsDir), derived: true };
}
+
+/**
+ * #4831: register exactly the given just-scaffolded skills in an existing,
+ * valid `skillsDir/manifest.json`. Appends `{name, path}` for each slug not
+ * already listed (by name or path); never removes or reorders entries and
+ * keeps every other key. A missing or malformed manifest is left alone: the
+ * loader derives the skill set from the walk there. Returns the names added.
+ */
+export function registerManifestEntries(skillsDir: string, slugs: readonly string[]): string[] {
+ const manifestPath = join(skillsDir, 'manifest.json');
+ if (!slugs.length || !existsSync(manifestPath) || loadOrDeriveManifest(skillsDir).derived) return [];
+ const content = JSON.parse(readFileSync(manifestPath, 'utf-8')) as { skills: ManifestEntry[] };
+ const known = new Set(content.skills.flatMap(entry => [entry.name, entry.path]));
+ const added = slugs.filter(slug => !known.has(slug) && !known.has(`${slug}/SKILL.md`))
+ .map(slug => ({ name: parseSkillName(join(skillsDir, slug, 'SKILL.md')) ?? slug, path: `${slug}/SKILL.md` }));
+ if (!added.length) return [];
+ atomicWriteFileSync(manifestPath, JSON.stringify({ ...content, skills: [...content.skills, ...added] }, null, 2) + '\n');
+ return added.map(entry => entry.name);
+}
diff --git a/test/skillpack-sync-register-4831.test.ts b/test/skillpack-sync-register-4831.test.ts
new file mode 100644
index 0000000000..8cb567f291
--- /dev/null
+++ b/test/skillpack-sync-register-4831.test.ts
@@ -0,0 +1,51 @@
+/**
+ * #4831: `gbrain skillpack sync` registers exactly the skills it scaffolded in
+ * an existing workspace skills/manifest.json (append-only, user entries and
+ * order untouched), so check-resolvable no longer reports each one as an
+ * orphan trigger. A workspace without a manifest keeps deriving from the walk.
+ */
+import { afterAll, describe, expect, test } from 'bun:test';
+import { spawnSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, resolve } from 'node:path';
+
+const REPO = resolve(import.meta.dir, '..');
+const dirs: string[] = [];
+afterAll(() => { for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); });
+function gbrain(args: string[], home: string) {
+ return spawnSync(process.execPath, ['run', join(REPO, 'src/cli.ts'), ...args], { cwd: REPO, encoding: 'utf8', timeout: 120_000,
+ env: { ...process.env, GBRAIN_HOME: home, HOME: home } });
+}
+
+describe('skillpack sync registers what it scaffolded (#4831)', () => {
+ test('appends scaffolded skills to an existing manifest and leaves user entries first and unchanged', () => {
+ const workspace = mkdtempSync(join(tmpdir(), 'gbrain-4831-ws-')), home = mkdtempSync(join(tmpdir(), 'gbrain-4831-home-'));
+ dirs.push(workspace, home);
+ mkdirSync(join(workspace, 'skills', 'my-own'), { recursive: true });
+ writeFileSync(join(workspace, 'skills', 'my-own', 'SKILL.md'), '---\nname: my-own\ndescription: A user skill\n---\nBody\n');
+ writeFileSync(join(workspace, 'skills', 'manifest.json'), JSON.stringify({ skills: [{ name: 'my-own', path: 'my-own/SKILL.md' }], custom: 'kept' }, null, 2));
+
+ const sync = gbrain(['skillpack', 'sync', '--workspace', workspace, '--json'], home);
+ expect(sync.status, sync.stderr).toBe(0);
+ const out = JSON.parse(sync.stdout) as { scaffolded: string[]; registered: string[] };
+ expect(out.scaffolded.length).toBeGreaterThan(10);
+ expect(out.registered.sort()).toEqual([...out.scaffolded].sort());
+
+ const manifest = JSON.parse(readFileSync(join(workspace, 'skills', 'manifest.json'), 'utf8')) as { skills: Array<{ name: string; path: string }>; custom: string };
+ expect(manifest.custom).toBe('kept');
+ expect(manifest.skills[0]).toEqual({ name: 'my-own', path: 'my-own/SKILL.md' });
+ for (const slug of out.scaffolded) {
+ expect(manifest.skills.some(entry => entry.path === `${slug}/SKILL.md`)).toBe(true);
+ expect(existsSync(join(workspace, 'skills', slug, 'SKILL.md'))).toBe(true);
+ }
+
+ const again = JSON.parse(gbrain(['skillpack', 'sync', '--workspace', workspace, '--json'], home).stdout) as { registered: string[] };
+ expect(again.registered).toEqual([]);
+
+ const check = gbrain(['check-resolvable', '--skills-dir', join(workspace, 'skills'), '--json'], home);
+ const report = JSON.parse(check.stdout) as { report: { issues: Array<{ type: string; skill?: string }> } };
+ expect(Array.isArray(report.report.issues)).toBe(true);
+ expect(report.report.issues.filter(issue => issue.type === 'orphan_trigger')).toEqual([]);
+ }, 300_000);
+});