@pinelabs/skills-cli 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/README.md +144 -2
- package/dist/agents.d.ts +26 -0
- package/dist/agents.js +74 -0
- package/dist/args.d.ts +5 -0
- package/dist/args.js +40 -0
- package/dist/artifacts.d.ts +16 -0
- package/dist/artifacts.js +206 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +86 -0
- package/dist/core.d.ts +111 -0
- package/dist/core.js +140 -0
- package/dist/engine.d.ts +18 -0
- package/dist/engine.js +275 -0
- package/dist/transaction.d.ts +19 -0
- package/dist/transaction.js +277 -0
- package/package.json +43 -4
- package/resources/pinelabs-architecture-0.5.0.skill.json +1 -0
- package/resources/registry.json +28 -0
- package/schemas/manifest-v1.schema.json +166 -0
- package/schemas/manifest-v2.schema.json +192 -0
- package/schemas/manifest-v3.schema.json +212 -0
- package/schemas/registry-v1.schema.json +130 -0
- package/schemas/skill-bundle-v1.schema.json +59 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from './args.js';
|
|
3
|
+
import { parseAgents } from './agents.js';
|
|
4
|
+
import { loadRegistry } from './artifacts.js';
|
|
5
|
+
import { execute, recover, validateProject } from './engine.js';
|
|
6
|
+
import { CliError, VERSION, projectRoot, ensure } from './core.js';
|
|
7
|
+
const HELP = `Pine Labs Skills CLI ${VERSION}
|
|
8
|
+
|
|
9
|
+
Usage: pinelabs-skills <command> [options]
|
|
10
|
+
list
|
|
11
|
+
add [skills] <id...|all>
|
|
12
|
+
update
|
|
13
|
+
remove [skills] <id...|all>
|
|
14
|
+
validate
|
|
15
|
+
recover Roll back an interrupted, journaled transaction
|
|
16
|
+
|
|
17
|
+
Options:
|
|
18
|
+
--project <directory> Existing project root (default: current directory)
|
|
19
|
+
--registry <bundled|path|https-url> Explicit trusted source (default: bundled local snapshot)
|
|
20
|
+
--registry-sha256 <digest> Required trust pin for remote registries
|
|
21
|
+
--agent codex|copilot|both|none Override best-effort repository detection
|
|
22
|
+
--dry-run Validate/plan without project writes
|
|
23
|
+
--non-interactive No prompts (also the default; --yes never forces conflicts)
|
|
24
|
+
--json One JSON result on stdout; structured errors on stderr
|
|
25
|
+
--help, --version
|
|
26
|
+
|
|
27
|
+
Only existing registry skills can be added; pinelabs-security is not supplied.
|
|
28
|
+
Install: npx @pinelabs/skills-cli add skills pinelabs-architecture
|
|
29
|
+
`;
|
|
30
|
+
async function main(argv) {
|
|
31
|
+
const asJson = argv.includes('--json');
|
|
32
|
+
try {
|
|
33
|
+
ensure([24, 26].includes(Number(process.versions.node.split('.')[0])), 'Use Node.js 24 or 26 LTS (24 is the tested baseline)', 'ERUNTIME', 2);
|
|
34
|
+
const args = parseArgs(argv), flag = (name) => args.flags.get(name);
|
|
35
|
+
if (args.command === 'help') {
|
|
36
|
+
console.log(HELP);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
if (args.command === 'version') {
|
|
40
|
+
console.log(VERSION);
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
const root = await projectRoot(flag('project') ?? process.cwd());
|
|
44
|
+
const registry = flag('registry') ?? process.env.PINELABS_SKILLS_REGISTRY;
|
|
45
|
+
const pin = flag('pin') ?? process.env.PINELABS_SKILLS_REGISTRY_SHA256;
|
|
46
|
+
const options = { project: root, dryRun: args.flags.has('dryRun') };
|
|
47
|
+
if (registry !== undefined)
|
|
48
|
+
options.registry = registry;
|
|
49
|
+
if (pin !== undefined)
|
|
50
|
+
options.registrySha256 = pin;
|
|
51
|
+
if (flag('agent') !== undefined)
|
|
52
|
+
options.agents = parseAgents(flag('agent'));
|
|
53
|
+
let result;
|
|
54
|
+
if (args.command === 'list') {
|
|
55
|
+
const loaded = await loadRegistry(registry ?? 'bundled', pin);
|
|
56
|
+
result = { command: 'list', status: 'ok', project: root, source: loaded.source, skills: loaded.entries.map(({ id, aliases, name, version, description, agents }) => ({ id, aliases, name, version, description, agents })), warnings: ['Registry availability does not imply organizational approval; default source is a bundled local snapshot.'] };
|
|
57
|
+
}
|
|
58
|
+
else if (args.command === 'validate')
|
|
59
|
+
result = await validateProject(root);
|
|
60
|
+
else if (args.command === 'recover')
|
|
61
|
+
result = { command: 'recover', project: root, ...await recover(root, args.flags.has('dryRun')) };
|
|
62
|
+
else
|
|
63
|
+
result = await execute(args.command, args.ids, options);
|
|
64
|
+
if (asJson)
|
|
65
|
+
console.log(JSON.stringify({ ok: true, ...result }));
|
|
66
|
+
else {
|
|
67
|
+
console.log(`${result.command}: ${result.status}`);
|
|
68
|
+
if (args.command === 'list')
|
|
69
|
+
for (const skill of result.skills)
|
|
70
|
+
console.log(` ${skill.id}@${skill.version} -> ${skill.name}`);
|
|
71
|
+
for (const line of result.changes ?? [])
|
|
72
|
+
console.log(` ${line}`);
|
|
73
|
+
for (const line of result.retained ?? [])
|
|
74
|
+
console.log(` retained: ${line}`);
|
|
75
|
+
for (const line of result.warnings ?? [])
|
|
76
|
+
console.log(` note: ${line}`);
|
|
77
|
+
}
|
|
78
|
+
return result.status === 'partial' ? 3 : 0;
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
const error = e instanceof CliError ? e : new CliError('EIO', e instanceof Error ? e.message : 'Unexpected I/O failure', 4);
|
|
82
|
+
console.error(asJson ? JSON.stringify({ ok: false, error: { code: error.code, message: error.message }, exitCode: error.exitCode }) : `${error.code}: ${error.message}`);
|
|
83
|
+
return error.exitCode;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
process.exitCode = await main(process.argv.slice(2));
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export declare const VERSION = "0.5.0";
|
|
2
|
+
export declare const MANIFEST = ".pinelabs/skills.json";
|
|
3
|
+
export declare const JOURNAL = ".pinelabs/skills-transaction.json";
|
|
4
|
+
export declare const LOCK = ".pinelabs/skills.lock";
|
|
5
|
+
export declare const SKILLS = ".agents/skills";
|
|
6
|
+
export declare const LIMIT: number;
|
|
7
|
+
export declare const START = "<!-- pinelabs-skills-cli:start:v1 -->";
|
|
8
|
+
export declare const END = "<!-- pinelabs-skills-cli:end:v1 -->";
|
|
9
|
+
export declare class CliError extends Error {
|
|
10
|
+
code: string;
|
|
11
|
+
exitCode: number;
|
|
12
|
+
constructor(code: string, message: string, exitCode?: number);
|
|
13
|
+
}
|
|
14
|
+
export declare function ensure(value: unknown, message: string, code?: string, exit?: number): asserts value;
|
|
15
|
+
export declare function object(value: unknown): asserts value is Record<string, unknown>;
|
|
16
|
+
export declare function keys(value: Record<string, unknown>, allowed: string[]): void;
|
|
17
|
+
export declare function text(value: unknown, label: string): asserts value is string;
|
|
18
|
+
export declare function identifier(value: unknown): asserts value is string;
|
|
19
|
+
export declare function version(value: unknown): asserts value is string;
|
|
20
|
+
export declare function compare(a: string, b: string): -1 | 0 | 1;
|
|
21
|
+
export declare function sha(data: Uint8Array | string): string;
|
|
22
|
+
export declare function digest(value: unknown): asserts value is string;
|
|
23
|
+
export declare function json(bytes: Uint8Array): unknown;
|
|
24
|
+
export declare function portable(value: unknown): asserts value is string;
|
|
25
|
+
export declare function allowedTarget(rel: string): void;
|
|
26
|
+
export declare function projectRoot(input: string): Promise<string>;
|
|
27
|
+
/** No symlinks, including internal links. Call again immediately before mutation. */
|
|
28
|
+
export declare function safe(root: string, rel: string): Promise<string>;
|
|
29
|
+
export declare function read(root: string, rel: string): Promise<Buffer | null>;
|
|
30
|
+
export declare function walk(root: string, rel: string): Promise<string[]>;
|
|
31
|
+
export type Agent = 'codex' | 'copilot';
|
|
32
|
+
export interface FileValue {
|
|
33
|
+
bytes: Buffer;
|
|
34
|
+
executable: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface Entry {
|
|
37
|
+
id: string;
|
|
38
|
+
aliases: string[];
|
|
39
|
+
name: string;
|
|
40
|
+
description: string;
|
|
41
|
+
version: string;
|
|
42
|
+
minimumInstallerVersion: string;
|
|
43
|
+
maximumInstallerMajor: number;
|
|
44
|
+
artifact: {
|
|
45
|
+
location: string;
|
|
46
|
+
sha256: string;
|
|
47
|
+
format: 'pinelabs-skill-bundle-v1';
|
|
48
|
+
};
|
|
49
|
+
agents: Agent[];
|
|
50
|
+
}
|
|
51
|
+
export type Source = {
|
|
52
|
+
kind: 'bundled';
|
|
53
|
+
sha256: string;
|
|
54
|
+
} | {
|
|
55
|
+
kind: 'local' | 'remote' | 'legacy-local';
|
|
56
|
+
location: string;
|
|
57
|
+
sha256: string;
|
|
58
|
+
};
|
|
59
|
+
export interface OwnedSkill {
|
|
60
|
+
id: string;
|
|
61
|
+
aliases: string[];
|
|
62
|
+
name: string;
|
|
63
|
+
version: string;
|
|
64
|
+
artifactSha256: string;
|
|
65
|
+
source: Source;
|
|
66
|
+
path: string;
|
|
67
|
+
agents: Agent[] | null;
|
|
68
|
+
files: Record<string, {
|
|
69
|
+
sha256: string;
|
|
70
|
+
executable: boolean;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
export interface Guidance {
|
|
74
|
+
path: string;
|
|
75
|
+
section: string;
|
|
76
|
+
created: boolean;
|
|
77
|
+
}
|
|
78
|
+
export interface Manifest {
|
|
79
|
+
schemaVersion: 1 | 2 | 3;
|
|
80
|
+
installerVersion: string;
|
|
81
|
+
skills: OwnedSkill[];
|
|
82
|
+
agents: Agent[];
|
|
83
|
+
guidance: Guidance[];
|
|
84
|
+
installedAt: string;
|
|
85
|
+
updatedAt: string;
|
|
86
|
+
}
|
|
87
|
+
export interface Operation {
|
|
88
|
+
path: string;
|
|
89
|
+
before: string | null;
|
|
90
|
+
after: string | null;
|
|
91
|
+
executable: boolean;
|
|
92
|
+
beforeMode: number | null;
|
|
93
|
+
afterMode: number | null;
|
|
94
|
+
}
|
|
95
|
+
export interface Journal {
|
|
96
|
+
schemaVersion: 1;
|
|
97
|
+
state: 'applying';
|
|
98
|
+
id: string;
|
|
99
|
+
operations: Operation[];
|
|
100
|
+
createdDirectories: string[];
|
|
101
|
+
}
|
|
102
|
+
export interface Result {
|
|
103
|
+
command: string;
|
|
104
|
+
status: 'ok' | 'noop' | 'dry-run' | 'partial';
|
|
105
|
+
project: string;
|
|
106
|
+
agents: Agent[];
|
|
107
|
+
changes: string[];
|
|
108
|
+
retained: string[];
|
|
109
|
+
warnings: string[];
|
|
110
|
+
[key: string]: unknown;
|
|
111
|
+
}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import * as fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
export const VERSION = '0.5.0';
|
|
5
|
+
export const MANIFEST = '.pinelabs/skills.json';
|
|
6
|
+
export const JOURNAL = '.pinelabs/skills-transaction.json';
|
|
7
|
+
export const LOCK = '.pinelabs/skills.lock';
|
|
8
|
+
export const SKILLS = '.agents/skills';
|
|
9
|
+
export const LIMIT = 16 * 1024 * 1024;
|
|
10
|
+
export const START = '<!-- pinelabs-skills-cli:start:v1 -->';
|
|
11
|
+
export const END = '<!-- pinelabs-skills-cli:end:v1 -->';
|
|
12
|
+
export class CliError extends Error {
|
|
13
|
+
code;
|
|
14
|
+
exitCode;
|
|
15
|
+
constructor(code, message, exitCode = 4) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.exitCode = exitCode;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function ensure(value, message, code = 'EVALIDATION', exit = 4) {
|
|
22
|
+
if (!value)
|
|
23
|
+
throw new CliError(code, message, exit);
|
|
24
|
+
}
|
|
25
|
+
export function object(value) {
|
|
26
|
+
ensure(value !== null && typeof value === 'object' && !Array.isArray(value), 'Expected an object');
|
|
27
|
+
}
|
|
28
|
+
export function keys(value, allowed) {
|
|
29
|
+
ensure(Object.keys(value).every(k => allowed.includes(k)), `Unexpected metadata fields: ${Object.keys(value).filter(k => !allowed.includes(k)).join(', ')}`);
|
|
30
|
+
}
|
|
31
|
+
export function text(value, label) {
|
|
32
|
+
ensure(typeof value === 'string' && value.length > 0 && value.length < 4096 && !/[\x00-\x1f]/.test(value), `Invalid ${label}`);
|
|
33
|
+
}
|
|
34
|
+
export function identifier(value) {
|
|
35
|
+
ensure(typeof value === 'string' && value.length <= 64 && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value), 'Invalid skill identifier/name');
|
|
36
|
+
}
|
|
37
|
+
export function version(value) {
|
|
38
|
+
ensure(typeof value === 'string' && value.length <= 48 && value.split('.').every(p => Number.isSafeInteger(Number(p))) && /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(value), 'Expected a stable x.y.z version');
|
|
39
|
+
}
|
|
40
|
+
export function compare(a, b) {
|
|
41
|
+
const aa = a.split('.').map(Number), bb = b.split('.').map(Number);
|
|
42
|
+
for (let i = 0; i < 3; i++)
|
|
43
|
+
if (aa[i] !== bb[i])
|
|
44
|
+
return aa[i] < bb[i] ? -1 : 1;
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
export function sha(data) { return createHash('sha256').update(data).digest('hex'); }
|
|
48
|
+
export function digest(value) {
|
|
49
|
+
ensure(typeof value === 'string' && /^[a-f0-9]{64}$/.test(value), 'Expected a lowercase SHA-256 digest');
|
|
50
|
+
}
|
|
51
|
+
export function json(bytes) {
|
|
52
|
+
ensure(bytes.length <= LIMIT, 'JSON exceeds size limit');
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new CliError('EVALIDATION', 'Invalid UTF-8 JSON or unsupported archive format (only skill-bundle-v1 JSON is accepted)');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function portable(value) {
|
|
61
|
+
text(value, 'relative path');
|
|
62
|
+
const s = value;
|
|
63
|
+
ensure(!s.includes('\\') && !path.posix.isAbsolute(s) && s.split('/').every(p => p !== '.' && p !== '..' && /^[A-Za-z0-9._ -]+$/.test(p) && p.trim() === p && !/[. ]$/.test(p) &&
|
|
64
|
+
!/^(con|prn|aux|nul|com[0-9]|lpt[0-9])(?:\.|$)/i.test(p)), `Unsafe or nonportable path: ${s}`);
|
|
65
|
+
}
|
|
66
|
+
export function allowedTarget(rel) {
|
|
67
|
+
portable(rel);
|
|
68
|
+
ensure(rel === MANIFEST || rel === 'AGENTS.md' || rel === '.github/copilot-instructions.md' ||
|
|
69
|
+
/^\.agents\/skills\/[a-z0-9]+(?:-[a-z0-9]+)*\/.+/.test(rel), `Not an installer target: ${rel}`);
|
|
70
|
+
}
|
|
71
|
+
export async function projectRoot(input) {
|
|
72
|
+
const root = await fs.realpath(path.resolve(input)).catch(() => { throw new CliError('EPROJECT', 'Project directory must already exist', 2); });
|
|
73
|
+
ensure((await fs.stat(root)).isDirectory(), 'Project is not a directory', 'EPROJECT', 2);
|
|
74
|
+
return root;
|
|
75
|
+
}
|
|
76
|
+
/** Existing project names are native filesystem names, not incoming artifact paths. */
|
|
77
|
+
function projectRelative(rel) {
|
|
78
|
+
ensure(typeof rel === 'string' && rel.length > 0 && !rel.includes('\0') && !path.isAbsolute(rel) &&
|
|
79
|
+
(process.platform !== 'win32' || !rel.includes('\\')) && rel.split('/').every(p => p.length > 0 && p !== '.' && p !== '..'), 'Unsafe project-relative path', 'ECONFLICT', 3);
|
|
80
|
+
}
|
|
81
|
+
/** No symlinks, including internal links. Call again immediately before mutation. */
|
|
82
|
+
export async function safe(root, rel) {
|
|
83
|
+
projectRelative(rel);
|
|
84
|
+
let current = root;
|
|
85
|
+
const parts = rel.split('/');
|
|
86
|
+
for (let i = 0; i < parts.length; i++) {
|
|
87
|
+
current = path.join(current, parts[i]);
|
|
88
|
+
let st;
|
|
89
|
+
try {
|
|
90
|
+
st = await fs.lstat(current);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e.code === 'ENOENT')
|
|
94
|
+
break;
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
ensure(!st.isSymbolicLink(), `Symlink/junction refused: ${path.relative(root, current)}`, 'ECONFLICT', 3);
|
|
98
|
+
ensure(i === parts.length - 1 || st.isDirectory(), `Non-directory ancestor: ${current}`, 'ECONFLICT', 3);
|
|
99
|
+
}
|
|
100
|
+
return path.join(root, rel);
|
|
101
|
+
}
|
|
102
|
+
export async function read(root, rel) {
|
|
103
|
+
const target = await safe(root, rel);
|
|
104
|
+
try {
|
|
105
|
+
const st = await fs.lstat(target);
|
|
106
|
+
ensure(st.isFile() && st.size <= LIMIT * 4, `Not a bounded regular file: ${rel}`, 'ECONFLICT', 3);
|
|
107
|
+
return await fs.readFile(target);
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (e.code === 'ENOENT')
|
|
111
|
+
return null;
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export async function walk(root, rel) {
|
|
116
|
+
const dir = await safe(root, rel);
|
|
117
|
+
let entries;
|
|
118
|
+
try {
|
|
119
|
+
entries = await fs.readdir(dir, { withFileTypes: true });
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
if (e.code === 'ENOENT')
|
|
123
|
+
return [];
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
const result = [];
|
|
127
|
+
if (entries.length === 0)
|
|
128
|
+
return [rel];
|
|
129
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
130
|
+
const child = `${rel}/${entry.name}`;
|
|
131
|
+
// readdir names cannot introduce traversal; never follow unowned links or read special files.
|
|
132
|
+
projectRelative(child);
|
|
133
|
+
if (entry.isDirectory())
|
|
134
|
+
result.push(...await walk(root, child));
|
|
135
|
+
else
|
|
136
|
+
result.push(child);
|
|
137
|
+
ensure(result.length <= 10000, 'Too many project skill files');
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { recover, type Hooks } from './transaction.js';
|
|
2
|
+
import { type Agent, type Manifest, type Result } from './core.js';
|
|
3
|
+
export interface Options {
|
|
4
|
+
project: string;
|
|
5
|
+
registry?: string;
|
|
6
|
+
registrySha256?: string;
|
|
7
|
+
agents?: Agent[];
|
|
8
|
+
dryRun?: boolean;
|
|
9
|
+
hooks?: Hooks;
|
|
10
|
+
}
|
|
11
|
+
export declare function parseManifest(data: Buffer): Manifest;
|
|
12
|
+
export declare function inspect(root: string): Promise<{
|
|
13
|
+
bytes: Buffer<ArrayBufferLike> | null;
|
|
14
|
+
manifest: Manifest | null;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function validateProject(root: string): Promise<Result>;
|
|
17
|
+
export declare function execute(command: 'add' | 'update' | 'remove', ids: string[], options: Options): Promise<Result>;
|
|
18
|
+
export { recover };
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import { ADAPTERS, detectAgents, guidanceChange, sectionRange } from './agents.js';
|
|
3
|
+
import { loadRegistry, resolveRequested, stageArtifact, decodeBundle } from './artifacts.js';
|
|
4
|
+
import { commit, operation, pending, recover } from './transaction.js';
|
|
5
|
+
import { ensure, object, keys, identifier, version, digest, json, portable, safe, read, walk, sha, VERSION, MANIFEST, SKILLS, START, END } from './core.js';
|
|
6
|
+
function changed(a, b) { return a === null || b === null ? a !== b : !a.equals(b); }
|
|
7
|
+
export function parseManifest(data) {
|
|
8
|
+
const value = json(data);
|
|
9
|
+
object(value);
|
|
10
|
+
keys(value, ['schemaVersion', 'installerVersion', 'skills', 'agents', 'guidance', 'installedAt', 'updatedAt']);
|
|
11
|
+
ensure((value.schemaVersion === 1 || value.schemaVersion === 2 || value.schemaVersion === 3), 'Unsupported ownership manifest schema');
|
|
12
|
+
version(value.installerVersion);
|
|
13
|
+
ensure(Array.isArray(value.skills) && value.skills.length <= 1000 && Array.isArray(value.guidance) && value.guidance.length <= 2, 'Invalid ownership manifest');
|
|
14
|
+
ensure(Array.isArray(value.agents) && value.agents.every(a => a === 'codex' || a === 'copilot') && new Set(value.agents).size === value.agents.length, 'Invalid manifest agents');
|
|
15
|
+
ensure(typeof value.installedAt === 'string' && typeof value.updatedAt === 'string', 'Invalid installation metadata');
|
|
16
|
+
const ids = new Set(), names = new Set();
|
|
17
|
+
for (const s of value.skills) {
|
|
18
|
+
object(s);
|
|
19
|
+
keys(s, ['id', 'aliases', 'name', 'version', 'artifactSha256', 'source', 'path', 'files', ...(value.schemaVersion === 3 ? ['agents'] : [])]);
|
|
20
|
+
identifier(s.id);
|
|
21
|
+
identifier(s.name);
|
|
22
|
+
version(s.version);
|
|
23
|
+
digest(s.artifactSha256);
|
|
24
|
+
ensure(!ids.has(s.id) && !names.has(s.name), 'Duplicate manifest ownership');
|
|
25
|
+
ids.add(s.id);
|
|
26
|
+
names.add(s.name);
|
|
27
|
+
ensure(Array.isArray(s.aliases) && s.aliases.every(a => { identifier(a); return true; }), 'Invalid installed aliases');
|
|
28
|
+
if (value.schemaVersion === 3)
|
|
29
|
+
ensure(s.agents === null || Array.isArray(s.agents) && s.agents.length > 0 && new Set(s.agents).size === s.agents.length && s.agents.every(a => a === 'codex' || a === 'copilot'), 'Invalid installed agent compatibility');
|
|
30
|
+
else
|
|
31
|
+
s.agents = null; // Legacy records did not attest per-skill capabilities.
|
|
32
|
+
ensure(s.path === `${SKILLS}/${s.name}`, 'Manifest ownership path does not match canonical name');
|
|
33
|
+
object(s.files);
|
|
34
|
+
ensure(Object.keys(s.files).length > 0 && Object.keys(s.files).length <= 3000, 'Invalid manifest file set');
|
|
35
|
+
for (const [p, rec] of Object.entries(s.files)) {
|
|
36
|
+
portable(p);
|
|
37
|
+
ensure(p.startsWith(`${s.path}/`), 'Manifest attempts ownership outside canonical skill');
|
|
38
|
+
// Path is checked by safe() before any filesystem access; no arbitrary project file ownership.
|
|
39
|
+
object(rec);
|
|
40
|
+
keys(rec, ['sha256', 'executable']);
|
|
41
|
+
digest(rec.sha256);
|
|
42
|
+
ensure(typeof rec.executable === 'boolean', 'Invalid owned executable flag');
|
|
43
|
+
}
|
|
44
|
+
ensure(Object.hasOwn(s.files, `${s.path}/SKILL.md`), 'Owned skill has no canonical entrypoint');
|
|
45
|
+
object(s.source);
|
|
46
|
+
digest(s.source.sha256);
|
|
47
|
+
if (value.schemaVersion === 1) {
|
|
48
|
+
keys(s.source, ['location', 'sha256', 'remote']);
|
|
49
|
+
ensure(typeof s.source.location === 'string' && typeof s.source.remote === 'boolean', 'Invalid legacy source record');
|
|
50
|
+
s.source = { kind: s.source.remote ? 'remote' : 'legacy-local', location: s.source.location, sha256: s.source.sha256 };
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
ensure(['bundled', 'local', 'remote', 'legacy-local'].includes(String(s.source.kind)), 'Invalid source kind');
|
|
54
|
+
keys(s.source, s.source.kind === 'bundled' ? ['kind', 'sha256'] : ['kind', 'location', 'sha256']);
|
|
55
|
+
if (s.source.kind !== 'bundled')
|
|
56
|
+
ensure(typeof s.source.location === 'string' && s.source.location.length > 0, 'Invalid source location');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const guidancePaths = new Set();
|
|
60
|
+
for (const g of value.guidance) {
|
|
61
|
+
object(g);
|
|
62
|
+
keys(g, ['path', 'section', 'created']);
|
|
63
|
+
ensure(typeof g.path === 'string' && Object.values(ADAPTERS).some(a => a.guidance === g.path) && !guidancePaths.has(g.path), 'Invalid guidance ownership');
|
|
64
|
+
guidancePaths.add(g.path);
|
|
65
|
+
ensure(typeof g.section === 'string' && g.section.length < 1024 * 1024 && new RegExp('^\\n{0,2}' + START).test(g.section) && g.section.endsWith(END + '\n') && typeof g.created === 'boolean', 'Invalid managed section');
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
export async function inspect(root) {
|
|
70
|
+
const bytes = await read(root, MANIFEST);
|
|
71
|
+
return { bytes, manifest: bytes === null ? null : parseManifest(bytes) };
|
|
72
|
+
}
|
|
73
|
+
async function inspectSkill(root, skill) {
|
|
74
|
+
const modified = [];
|
|
75
|
+
for (const [rel, record] of Object.entries(skill.files)) {
|
|
76
|
+
const data = await read(root, rel);
|
|
77
|
+
if (data === null || sha(data) !== record.sha256) {
|
|
78
|
+
modified.push(rel);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (process.platform !== 'win32') {
|
|
82
|
+
const mode = (await fs.stat(await safe(root, rel))).mode;
|
|
83
|
+
if (((mode & 0o111) !== 0) !== record.executable)
|
|
84
|
+
modified.push(rel);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const extra = (await walk(root, skill.path)).filter(p => !Object.hasOwn(skill.files, p));
|
|
88
|
+
return { modified, extra };
|
|
89
|
+
}
|
|
90
|
+
export async function validateProject(root) {
|
|
91
|
+
ensure(!await pending(root), 'Interrupted transaction exists; run recover', 'ERECOVERY', 5);
|
|
92
|
+
const { manifest } = await inspect(root);
|
|
93
|
+
const result = { command: 'validate', status: manifest ? 'ok' : 'noop', project: root, agents: manifest?.agents ?? [], changes: [], retained: [], warnings: [] };
|
|
94
|
+
if (!manifest) {
|
|
95
|
+
result.warnings.push('No installer ownership manifest; unmanaged skills were not adopted.');
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
for (const s of manifest.skills) {
|
|
99
|
+
const { modified, extra } = await inspectSkill(root, s);
|
|
100
|
+
ensure(modified.length === 0, `Local changes/missing owned files: ${modified.join(', ')}`, 'ECONFLICT', 3);
|
|
101
|
+
result.retained.push(...extra);
|
|
102
|
+
const files = await Promise.all(Object.keys(s.files).map(async (p) => ({ path: p.slice(s.path.length + 1), type: 'file', content: (await read(root, p)).toString('base64'), executable: s.files[p].executable })));
|
|
103
|
+
// Validate runtime closure/metadata as well as ownership hashes, without executing installed scripts.
|
|
104
|
+
const entrypoint = await read(root, `${s.path}/SKILL.md`);
|
|
105
|
+
const { metadata } = await import('./artifacts.js');
|
|
106
|
+
const meta = metadata(entrypoint);
|
|
107
|
+
decodeBundle(Buffer.from(JSON.stringify({ format: 'pinelabs-skill-bundle-v1', name: s.name, version: s.version, files })), { name: s.name, version: s.version, description: meta.description });
|
|
108
|
+
}
|
|
109
|
+
for (const g of manifest.guidance) {
|
|
110
|
+
const data = await read(root, g.path);
|
|
111
|
+
ensure(data, `Missing guidance ${g.path}`, 'ECONFLICT', 3);
|
|
112
|
+
sectionRange(data.toString('utf8'), g);
|
|
113
|
+
}
|
|
114
|
+
result.skills = manifest.skills.map(s => ({ id: s.id, name: s.name, version: s.version }));
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
export async function execute(command, ids, options) {
|
|
118
|
+
const root = options.project;
|
|
119
|
+
ensure(!await pending(root), 'Interrupted transaction exists; use recover (or recover --dry-run)', 'ERECOVERY', 5);
|
|
120
|
+
const state = await inspect(root), previous = state.manifest;
|
|
121
|
+
const detected = options.agents !== undefined || previous ? { agents: [], signals: {}, warnings: [] } : await detectAgents(root);
|
|
122
|
+
const agents = options.agents ?? previous?.agents ?? detected.agents;
|
|
123
|
+
const result = { command, status: 'noop', project: root, agents, changes: [], retained: [], warnings: options.agents || previous ? [] : detected.warnings };
|
|
124
|
+
result.detection = detected.signals;
|
|
125
|
+
if (!previous && (command === 'update' || command === 'remove')) {
|
|
126
|
+
if (command === 'remove')
|
|
127
|
+
ensure(ids.length === 0, 'No installer-owned skills to remove', 'EUNKNOWN', 2);
|
|
128
|
+
result.warnings.push('No installer-owned skills found');
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
const oldSkills = previous?.skills ?? [];
|
|
132
|
+
let next = [...oldSkills];
|
|
133
|
+
const operations = [];
|
|
134
|
+
const requestedRoots = new Set();
|
|
135
|
+
const op = async (rel, after, executable) => {
|
|
136
|
+
const before = await read(root, rel);
|
|
137
|
+
const beforeMode = before === null ? 0o644 : (await fs.stat(await safe(root, rel))).mode & 0o777;
|
|
138
|
+
const afterMode = rel === MANIFEST ? 0o600 : executable === undefined ? beforeMode : executable ? 0o755 : 0o644;
|
|
139
|
+
if (changed(before, after) || after !== null && before !== null && beforeMode !== afterMode)
|
|
140
|
+
operations.push(operation(rel, before, after, executable ?? (afterMode & 0o111) !== 0, beforeMode, afterMode));
|
|
141
|
+
};
|
|
142
|
+
if (command === 'remove') {
|
|
143
|
+
ensure(ids.length > 0, 'Specify installed IDs or all', 'EUSAGE', 2);
|
|
144
|
+
const selection = ids.includes('all') ? (ensure(ids.length === 1, 'Use all alone', 'EUSAGE', 2), oldSkills) : ids.map(id => {
|
|
145
|
+
const found = oldSkills.find(s => s.id === id || s.name === id || s.aliases.includes(id));
|
|
146
|
+
ensure(found, `Not installer-owned: ${id}`, 'EUNKNOWN', 2);
|
|
147
|
+
return found;
|
|
148
|
+
});
|
|
149
|
+
const selected = [...new Map(selection.map(s => [s.id, s])).values()];
|
|
150
|
+
const removeIDs = new Set(selected.map(s => s.id));
|
|
151
|
+
for (const s of selected) {
|
|
152
|
+
const { modified, extra } = await inspectSkill(root, s);
|
|
153
|
+
for (const rel of modified)
|
|
154
|
+
if (await read(root, rel) !== null)
|
|
155
|
+
result.retained.push(rel);
|
|
156
|
+
result.retained.push(...extra);
|
|
157
|
+
for (const rel of Object.keys(s.files)) {
|
|
158
|
+
const current = await read(root, rel);
|
|
159
|
+
if (current !== null && !modified.includes(rel))
|
|
160
|
+
await op(rel, null);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
next = oldSkills.filter(s => !removeIDs.has(s.id));
|
|
164
|
+
if (result.retained.length)
|
|
165
|
+
result.warnings.push('Retained files are now user-owned. A retained modified SKILL.md can still be discovered by agents.');
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
const sources = new Map();
|
|
169
|
+
async function source(location, pin) {
|
|
170
|
+
const key = `${location}:${pin ?? ''}`;
|
|
171
|
+
let loaded = sources.get(key);
|
|
172
|
+
if (!loaded) {
|
|
173
|
+
loaded = await loadRegistry(location, pin);
|
|
174
|
+
sources.set(key, loaded);
|
|
175
|
+
}
|
|
176
|
+
return loaded;
|
|
177
|
+
}
|
|
178
|
+
const requested = [];
|
|
179
|
+
if (command === 'add') {
|
|
180
|
+
const registry = await source(options.registry ?? 'bundled', options.registrySha256);
|
|
181
|
+
for (const entry of resolveRequested(registry.entries, ids))
|
|
182
|
+
requested.push({ entry, registry });
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
ensure(ids.length === 0, 'update operates on all installer-owned skills', 'EUSAGE', 2);
|
|
186
|
+
for (const old of oldSkills) {
|
|
187
|
+
ensure(options.registry !== undefined || old.source.kind !== 'legacy-local', 'Legacy manifest cannot distinguish bundled from explicit local source. Choose --registry bundled or --registry <trusted-path> to update; validation/removal remain available.', 'ELEGACYSOURCE', 3);
|
|
188
|
+
const location = options.registry ?? (old.source.kind === 'bundled' ? 'bundled' : old.source.location);
|
|
189
|
+
const pin = options.registrySha256 ?? (options.registry === undefined && old.source.kind === 'remote' ? old.source.sha256 : undefined);
|
|
190
|
+
const registry = await source(location, pin), entry = registry.entries.find(e => e.id === old.id);
|
|
191
|
+
ensure(entry, `Installed skill missing from trusted registry: ${old.id}`);
|
|
192
|
+
requested.push({ entry, registry });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Resolve and verify every artifact before even creating a project lock/state directory.
|
|
196
|
+
const staged = await Promise.all(requested.map(async (req) => ({ ...req, files: await stageArtifact(req.registry.source, req.entry) })));
|
|
197
|
+
for (const { entry, registry, files } of staged) {
|
|
198
|
+
ensure(agents.every(a => entry.agents.includes(a)), `Skill ${entry.id} does not declare support for selected agents`, 'EINCOMPATIBLE');
|
|
199
|
+
const old = oldSkills.find(s => s.id === entry.id), target = `${SKILLS}/${entry.name}`;
|
|
200
|
+
ensure(!requestedRoots.has(target), 'Two requested IDs target one directory');
|
|
201
|
+
requestedRoots.add(target);
|
|
202
|
+
ensure(!oldSkills.some(s => s.id !== entry.id && s.path === target), `Canonical name collision: ${target}`, 'ECONFLICT', 3);
|
|
203
|
+
if (old) {
|
|
204
|
+
ensure(old.name === entry.name, 'Canonical rename needs explicit remove/add migration', 'ECONFLICT', 3);
|
|
205
|
+
const { modified, extra } = await inspectSkill(root, old);
|
|
206
|
+
result.retained.push(...extra);
|
|
207
|
+
ensure(modified.length === 0, `Local changes: ${modified.join(', ')}. Preserve or restore these before update.`, 'ECONFLICT', 3);
|
|
208
|
+
ensure(compareVersions(entry.version, old.version) >= 0, `Registry downgrade refused for ${entry.id}`, 'ECONFLICT', 3);
|
|
209
|
+
if (entry.version === old.version) {
|
|
210
|
+
ensure(entry.artifact.sha256 === old.artifactSha256, 'Same version has a different digest; release is not immutable', 'ECONFLICT', 3);
|
|
211
|
+
next = next.map(s => s.id === old.id ? { ...s, source: registry.source, agents: entry.agents } : s);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
try {
|
|
217
|
+
await fs.lstat(await safe(root, target));
|
|
218
|
+
throw new Error('collision');
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
ensure(e.code === 'ENOENT', `Unmanaged directory already exists: ${target}. No implicit adoption.`, 'ECONFLICT', 3);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const owned = { id: entry.id, aliases: entry.aliases, name: entry.name, version: entry.version, artifactSha256: entry.artifact.sha256, source: registry.source, path: target, agents: entry.agents, files: {} };
|
|
225
|
+
for (const [name, value] of files) {
|
|
226
|
+
const rel = `${target}/${name}`, current = await read(root, rel);
|
|
227
|
+
ensure(current === null || old && Object.hasOwn(old.files, rel), `Artifact collides with user file: ${rel}`, 'ECONFLICT', 3);
|
|
228
|
+
owned.files[rel] = { sha256: sha(value.bytes), executable: value.executable };
|
|
229
|
+
await op(rel, value.bytes, value.executable);
|
|
230
|
+
}
|
|
231
|
+
if (old)
|
|
232
|
+
for (const rel of Object.keys(old.files))
|
|
233
|
+
if (!Object.hasOwn(owned.files, rel))
|
|
234
|
+
await op(rel, null);
|
|
235
|
+
next = [...next.filter(s => s.id !== owned.id), owned];
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// Guidance covers the complete resulting set, including releases not requested in this batch.
|
|
239
|
+
for (const skill of next) {
|
|
240
|
+
const supported = skill.agents ?? [];
|
|
241
|
+
ensure(agents.every(a => supported.includes(a)), `Retained skill ${skill.id} does not declare support for selected agents. Update all installed skills to refresh legacy capability records, or keep compatible agents.`, 'EINCOMPATIBLE');
|
|
242
|
+
}
|
|
243
|
+
next.sort((a, b) => a.id.localeCompare(b.id));
|
|
244
|
+
const guidance = [];
|
|
245
|
+
for (const agent of ['codex', 'copilot']) {
|
|
246
|
+
const filename = ADAPTERS[agent].guidance, owned = previous?.guidance.find(g => g.path === filename);
|
|
247
|
+
if (!owned && !agents.includes(agent))
|
|
248
|
+
continue;
|
|
249
|
+
const before = await read(root, filename);
|
|
250
|
+
const edit = guidanceChange(agent, before, owned, agents.includes(agent) ? next : []);
|
|
251
|
+
await op(filename, edit.after);
|
|
252
|
+
if (edit.guidance)
|
|
253
|
+
guidance.push(edit.guidance);
|
|
254
|
+
}
|
|
255
|
+
const now = new Date().toISOString();
|
|
256
|
+
const same = previous && previous.schemaVersion === 3 && JSON.stringify(previous.skills) === JSON.stringify(next) && JSON.stringify(previous.agents) === JSON.stringify(agents) && JSON.stringify(previous.guidance) === JSON.stringify(guidance);
|
|
257
|
+
if (!same) {
|
|
258
|
+
const manifest = { schemaVersion: 3, installerVersion: VERSION, skills: next, agents, guidance, installedAt: previous?.installedAt ?? now, updatedAt: now };
|
|
259
|
+
await op(MANIFEST, next.length || guidance.length ? Buffer.from(JSON.stringify(manifest, null, 2) + '\n') : null);
|
|
260
|
+
}
|
|
261
|
+
if (operations.length === 0)
|
|
262
|
+
return result;
|
|
263
|
+
// Reference writes, then entrypoints, then deletions/guidance, manifest last. This is journaled, not multi-file OS atomicity.
|
|
264
|
+
operations.sort((a, b) => rank(a) - rank(b));
|
|
265
|
+
ensure(operations.at(-1)?.path === MANIFEST, 'Ownership must commit last');
|
|
266
|
+
result.changes = operations.map(o => o.path);
|
|
267
|
+
result.status = options.dryRun ? 'dry-run' : result.retained.length && command === 'remove' ? 'partial' : 'ok';
|
|
268
|
+
await commit(root, operations, options.hooks, options.dryRun);
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
function rank(op) { return op.path === MANIFEST ? 5 : !op.path.startsWith(`${SKILLS}/`) ? 4 : op.after === null ? 3 : op.path.endsWith('/SKILL.md') ? 2 : 1; }
|
|
272
|
+
function compareVersions(a, b) { const aa = a.split('.').map(Number), bb = b.split('.').map(Number); for (let i = 0; i < 3; i++)
|
|
273
|
+
if (aa[i] !== bb[i])
|
|
274
|
+
return aa[i] < bb[i] ? -1 : 1; return 0; }
|
|
275
|
+
export { recover };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Operation } from './core.js';
|
|
2
|
+
export interface Hooks {
|
|
3
|
+
afterLock?: () => void | Promise<void>;
|
|
4
|
+
afterJournalRemoval?: () => void | Promise<void>;
|
|
5
|
+
afterOperation?: (index: number) => void | Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function operation(rel: string, before: Buffer | null, after: Buffer | null, executable?: boolean, beforeMode?: number, afterMode?: number): Operation;
|
|
8
|
+
export declare function pending(root: string): Promise<boolean>;
|
|
9
|
+
export declare function commit(root: string, operations: Operation[], hooks?: Hooks, dryRun?: boolean): Promise<void>;
|
|
10
|
+
export declare function recover(root: string, dryRun: boolean): Promise<{
|
|
11
|
+
status: 'noop';
|
|
12
|
+
changes: string[];
|
|
13
|
+
} | {
|
|
14
|
+
status: 'dry-run';
|
|
15
|
+
changes: string[];
|
|
16
|
+
} | {
|
|
17
|
+
status: 'ok';
|
|
18
|
+
changes: string[];
|
|
19
|
+
}>;
|