Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(skillpack): steer reference --all --apply-clean-hunks to the per-skill apply (#5517)
time-attackbc760d50c3bc
diff --git a/src/commands/skillpack/harness.ts b/src/commands/skillpack/harness.tsindex 12aa99fdf7..9ddf9325c8 100644--- a/src/commands/skillpack/harness.ts+++ b/src/commands/skillpack/harness.ts@@ -525,14 +525,15 @@ export async function cmdScaffoldHarness(args: string[]): Promise<void> {export async function cmdReferenceHarness(args: string[]): Promise<void> {if (args.includes('--help') || args.includes('-h')) {console.log(- 'gbrain skillpack reference --harness <h> [<slug>] [--dest PATH] [--scope user|project] [--workspace PATH] [--apply-clean-hunks] [--dry-run] [--json]\n\n' ++ 'gbrain skillpack reference --harness <h> [<slug>] [--skill <slug>] [--dest PATH] [--scope user|project] [--workspace PATH] [--apply-clean-hunks] [--dry-run] [--json]\n\n' +'Targeting resolves like scaffold: explicit dest wins, else the harness\n' +'default for the scope (claude-code user scope unless --scope project).\n\n' +'Diff a harness install against gbrain\'s current bundle. Stub installs\n' +'diff against the RENDERED stub (a fresh stub reads identical). With the\n' +'install-time hash ledger, differs splits into local_edit vs\n' +'upstream_drift. --apply-clean-hunks lands non-conflicting hunks on\n' +- 'full-mode files only (stubs are regenerated by re-running scaffold).',+ 'full-mode files only (stubs are regenerated by re-running scaffold).\n' ++ '--apply-clean-hunks needs exactly one skill (<slug> or a single --skill).',);process.exit(0);}diff --git a/src/commands/skillpack/reference.ts b/src/commands/skillpack/reference.tsindex 66059af2ca..27da0c37b3 100644--- a/src/commands/skillpack/reference.ts+++ b/src/commands/skillpack/reference.ts@@ -25,7 +25,14 @@ export async function cmdReference(args: string[]): Promise<void> {}if (args.includes('--help') || args.includes('-h')) {console.log(- 'gbrain skillpack reference <name> | --all [--workspace PATH] [--apply-clean-hunks] [--since <version>] [--dry-run] [--json]\n\n' ++ 'gbrain skillpack reference <name> [--workspace PATH] [--apply-clean-hunks [--dry-run]] [--json]\n' ++ 'gbrain skillpack reference --all [--workspace PATH] [--since <version>] [--json]\n\n' ++ ' --apply-clean-hunks Aligns every clean hunk in ONE skill to gbrain,\n' ++ ' including intentional local edits; preview with\n' ++ ' --dry-run. Not available with --all (it would do that\n' ++ ' to every skill at once): sweep with --all, then apply\n' ++ ' per skill.\n' ++ ' --dry-run With --apply-clean-hunks, report outcomes, write nothing.\n' +' --since <version> With --all, restrict the sweep to skills whose source\n' +' changed in gbrain between <version> and HEAD. Useful\n' +' after `gbrain upgrade` to see only what moved.',@@ -59,18 +66,25 @@ export async function cmdReference(args: string[]): Promise<void> {console.error('Error: pass a skill name or --all.');process.exit(2);}+ if (all && apply) {+ const { exitCliError, usageError } = await import('../../cli/cli-error.ts');+ exitCliError(usageError(+ '--apply-clean-hunks works on one skill at a time, not with --all.',+ 'List which skills differ with `gbrain skillpack reference --all`, inspect one with `gbrain skillpack reference <slug>`, '+ + 'then apply it with `gbrain skillpack reference <slug> --apply-clean-hunks` (add --dry-run to preview; keep any --workspace you passed).',+ {+ why: 'The apply aligns every clean hunk to gbrain, including intentional local edits; with --all it would do that to every skill at once.',+ fix: { argv: ['gbrain', 'skillpack', 'reference', '--all'], consent: [], actor: 'agent', requires_exclusive: false,+ why: 'Lists which skills differ, read-only, so one skill can be applied at a time.' },+ },+ ), 'skillpack', { json });+ }const gbrainRoot = findGbrainOrDie();const targetWorkspace = resolveWorkspace({ workspace });try {if (apply) {- if (all) {- console.error(- 'Error: --apply-clean-hunks is intentionally NOT supported with --all. Apply one skill at a time.',- );- process.exit(2);- }// Two-way merge warning fires BEFORE the apply. Goes to stderr so// it survives stdout redirection. Suppressed in --json mode so// machine consumers (CI, agent scripts) get a clean envelope; thediff --git a/test/skillpack-reference-cli.test.ts b/test/skillpack-reference-cli.test.tsnew file mode 100644index 0000000000..fa23e43b3f--- /dev/null+++ b/test/skillpack-reference-cli.test.ts@@ -0,0 +1,110 @@+/**+ * `gbrain skillpack reference` help and the --all + --apply-clean-hunks+ * refusal (#5491).+ *+ * Protects: an operator or agent reading `reference --help` sees that+ * --apply-clean-hunks works per skill only, and the --all refusal names the+ * per-skill path through the agent-operator contract before any workspace+ * lookup can mask it. Fails when help shows one combined form, the refusal+ * loses its contract fields, or workspace resolution runs first. Existing+ * coverage (skillpack-reference-apply.test.ts) exercises the core apply, not+ * this CLI surface.+ */++import { afterAll, describe, expect, test } from 'bun:test';+import { appendFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { runCli } from './helpers/cli-spawn.ts';++const root = mkdtempSync(join(tmpdir(), 'sp-refcli-'));+afterAll(() => rmSync(root, { recursive: true, force: true }));++let n = 0;+function dir(prefix: string): string {+ return mkdtempSync(join(root, `${prefix}-${n++}-`));+}++function skillpack(args: string[], opts: { cwd?: string } = {}) {+ return runCli(['skillpack', ...args], {+ home: dir('home'),+ cwd: opts.cwd,+ env: { OPENCLAW_WORKSPACE: '', GBRAIN_SKILLS_DIR: undefined },+ timeoutMs: 120_000,+ });+}++/** A workspace with book-mirror scaffolded and one intentional local edit. */+async function editedWorkspace() {+ const ws = dir('ws');+ const scaffold = await skillpack(['scaffold', 'book-mirror', '--workspace', ws]);+ expect(scaffold.exitCode, scaffold.stderr).toBe(0);+ const skillPath = join(ws, 'skills', 'book-mirror', 'SKILL.md');+ appendFileSync(skillPath, '\nIntentional local edit.\n');+ return { ws, skillPath, edited: readFileSync(skillPath, 'utf-8') };+}++describe('skillpack reference help (#5491)', () => {+ test('--help shows --apply-clean-hunks only on the per-skill form, with the local-edit caveat', async () => {+ const r = await skillpack(['reference', '--help']);+ expect(r.exitCode).toBe(0);+ const usage = r.stdout.split('\n').filter(l => l.startsWith('gbrain skillpack reference'));+ const nameForm = usage.find(l => l.includes('<name>'));+ const allForm = usage.find(l => l.includes('--all'));+ expect(nameForm).toContain('--apply-clean-hunks');+ expect(nameForm).not.toContain('--all');+ expect(allForm).not.toContain('--apply-clean-hunks');+ expect(allForm).toContain('--since');+ expect(r.stdout).toContain('including intentional local edits');+ }, 120_000);++ test('reference --harness --help says --apply-clean-hunks takes exactly one skill', async () => {+ const r = await skillpack(['reference', '--harness', 'claude-code', '--help']);+ expect(r.exitCode).toBe(0);+ expect(r.stdout).toContain('[--skill <slug>]');+ expect(r.stdout).toContain('--apply-clean-hunks needs exactly one skill');+ }, 120_000);+});++describe('skillpack reference --all --apply-clean-hunks refusal (#5491)', () => {+ test('human: exit 2, the contract lines name the per-skill command, nothing is written', async () => {+ const { ws, skillPath, edited } = await editedWorkspace();+ const r = await skillpack(['reference', '--all', '--apply-clean-hunks', '--workspace', ws]);+ expect(r.exitCode).toBe(2);+ expect(r.stderr).toContain('Error [invalid_params]: --apply-clean-hunks works on one skill at a time, not with --all.');+ expect(r.stderr).toContain('Fix: gbrain skillpack reference --all');+ expect(r.stderr).toContain('Why: The apply aligns every clean hunk to gbrain, including intentional local edits');+ expect(readFileSync(skillPath, 'utf-8')).toBe(edited);+ }, 180_000);++ test('--json: one invalid_params envelope with suggestion, why and the read-only sweep as fix', async () => {+ const r = await skillpack(['reference', '--all', '--apply-clean-hunks', '--json'], { cwd: dir('cwd') });+ expect(r.exitCode).toBe(2);+ const doc = JSON.parse(r.stdout.trim());+ expect(doc).toMatchObject({ code: 'invalid_params', class: 'caller', contract_version: 1 });+ expect(doc.suggestion).toContain('gbrain skillpack reference <slug> --apply-clean-hunks');+ expect(doc.why).toContain('every skill at once');+ expect(doc.fix).toMatchObject({ argv: ['gbrain', 'skillpack', 'reference', '--all'], next: 'run' });+ }, 120_000);++ test('the refusal fires before workspace resolution', async () => {+ const r = await skillpack(['reference', '--all', '--apply-clean-hunks'], { cwd: dir('cwd') });+ expect(r.exitCode).toBe(2);+ expect(r.stderr).toContain('not with --all');+ expect(r.stderr).not.toContain('auto-detect');+ }, 120_000);+});++describe('skillpack reference read-only and per-skill paths still run (#5491 negative controls)', () => {+ test('per-skill --apply-clean-hunks --dry-run and the --all sweep both exit 0 and write nothing', async () => {+ const { ws, skillPath, edited } = await editedWorkspace();+ const apply = await skillpack(['reference', 'book-mirror', '--apply-clean-hunks', '--dry-run', '--json', '--workspace', ws]);+ expect(apply.exitCode, apply.stderr).toBe(0);+ expect(JSON.parse(apply.stdout).summary.totalHunksApplied).toBeGreaterThanOrEqual(1);+ const sweep = await skillpack(['reference', '--all', '--json', '--workspace', ws]);+ expect(sweep.exitCode, sweep.stderr).toBe(0);+ const bookMirror = (JSON.parse(sweep.stdout).skills as Array<{ slug: string; summary: { differs: number } }>).find(s => s.slug === 'book-mirror');+ expect(bookMirror?.summary.differs).toBeGreaterThanOrEqual(1);+ expect(readFileSync(skillPath, 'utf-8')).toBe(edited);+ }, 180_000);+});