@bbcsoftwarefactory/cli 0.1.1 → 1.1.2
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/dist/audit-ledger.d.ts +2 -0
- package/dist/audit-ledger.js +71 -0
- package/dist/bbc-install.d.ts +2 -0
- package/dist/bbc-install.js +1250 -0
- package/dist/bbc.js +87 -3
- package/dist/change-lane/classification.js +3 -1
- package/dist/check-issue-claim.d.ts +2 -0
- package/dist/check-issue-claim.js +146 -0
- package/dist/check-worktree-guard.d.ts +2 -0
- package/dist/check-worktree-guard.js +114 -0
- package/dist/error-scan.d.ts +3 -0
- package/dist/error-scan.js +216 -0
- package/dist/export-public.d.ts +9 -0
- package/dist/export-public.js +274 -0
- package/dist/fetch-issue-attachments.d.ts +22 -0
- package/dist/fetch-issue-attachments.js +325 -0
- package/dist/issue-claim/evaluation.d.ts +66 -0
- package/dist/issue-claim/evaluation.js +259 -0
- package/dist/issue-claim/loaders.d.ts +5 -0
- package/dist/issue-claim/loaders.js +116 -0
- package/dist/list-zed-commands.d.ts +7 -0
- package/dist/list-zed-commands.js +288 -0
- package/dist/plan-issue-triage.d.ts +3 -0
- package/dist/plan-issue-triage.js +404 -0
- package/dist/question-gate/ledger.d.ts +5 -0
- package/dist/question-gate/ledger.js +115 -0
- package/dist/question-gate/rules.d.ts +40 -0
- package/dist/question-gate/rules.js +483 -0
- package/dist/recap/board.d.ts +5 -0
- package/dist/recap/board.js +384 -0
- package/dist/recap/git-and-github.d.ts +11 -0
- package/dist/recap/git-and-github.js +620 -0
- package/dist/recap/report-and-hud.d.ts +2 -0
- package/dist/recap/report-and-hud.js +294 -0
- package/dist/recap/time-and-refs.d.ts +27 -0
- package/dist/recap/time-and-refs.js +148 -0
- package/dist/recap.d.ts +2 -0
- package/dist/recap.js +217 -0
- package/dist/render-workflow.js +12 -14
- package/dist/resolve-containers.d.ts +2 -0
- package/dist/resolve-containers.js +96 -0
- package/dist/review-loop.d.ts +7 -0
- package/dist/review-loop.js +622 -0
- package/dist/save-counts.d.ts +2 -0
- package/dist/save-counts.js +191 -0
- package/dist/summarize-logs.d.ts +2 -0
- package/dist/summarize-logs.js +41 -0
- package/dist/sweep-github.d.ts +10 -0
- package/dist/sweep-github.js +453 -0
- package/dist/sweep-issues.d.ts +56 -0
- package/dist/sweep-issues.js +650 -0
- package/dist/sweep.d.ts +2 -0
- package/dist/sweep.js +301 -0
- package/dist/triage-question.d.ts +2 -0
- package/dist/triage-question.js +189 -0
- package/dist/validate-skills.d.ts +2 -0
- package/dist/validate-skills.js +48 -0
- package/dist/watch-live.d.ts +7 -0
- package/dist/watch-live.js +83 -0
- package/dist/watcher/config.d.ts +11 -0
- package/dist/watcher/config.js +265 -0
- package/dist/watcher/live-classifier.d.ts +52 -0
- package/dist/watcher/live-classifier.js +510 -0
- package/dist/watcher/safe-fs.d.ts +9 -0
- package/dist/watcher/safe-fs.js +159 -0
- package/dist/watcher/stream.d.ts +27 -0
- package/dist/watcher/stream.js +340 -0
- package/dist/watcher/summarizer.d.ts +19 -0
- package/dist/watcher/summarizer.js +121 -0
- package/dist/wave3/process.d.ts +14 -0
- package/dist/wave3/process.js +84 -0
- package/dist/wave4/process.d.ts +14 -0
- package/dist/wave4/process.js +128 -0
- package/dist/workflow/render.d.ts +1 -1
- package/dist/workflow/render.js +1 -1
- package/dist/workflow-auditor/audit.d.ts +27 -0
- package/dist/workflow-auditor/audit.js +583 -0
- package/dist/workflow-auditor/ledger-io.d.ts +5 -0
- package/dist/workflow-auditor/ledger-io.js +99 -0
- package/dist/workflow-auditor/skills-validator.d.ts +10 -0
- package/dist/workflow-auditor/skills-validator.js +533 -0
- package/dist/worktree-guard/evaluation.d.ts +19 -0
- package/dist/worktree-guard/evaluation.js +84 -0
- package/dist/worktree-guard/git.d.ts +3 -0
- package/dist/worktree-guard/git.js +61 -0
- package/package.json +32 -33
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Infrastructure
|
|
3
|
+
* Contains: File loading, catalog parsing, syslog parsing, and git worktree ledger discovery.
|
|
4
|
+
* Rules: Read-only git rev-parse and file reading; never write files.
|
|
5
|
+
* BigO: O(n^2) score:1
|
|
6
|
+
* keywords: [LedgerLoader, CatalogLoader, WatcherSyslog]
|
|
7
|
+
* GOAL: Load and normalize ledger, Tool Depot catalog, and watcher-gate log inputs.
|
|
8
|
+
*/
|
|
9
|
+
import { spawnSync } from 'node:child_process';
|
|
10
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { AuditUnusableError } from "./audit.js";
|
|
13
|
+
const LEDGER_NAME = 'bbc-delivery-ledger.json';
|
|
14
|
+
const GATE_ENTRY_RE = /(?:^|\swatcher-gate(?:\[\d+\])?:\s)(allowed|denied): (.*)$/;
|
|
15
|
+
export function defaultCatalogPath() {
|
|
16
|
+
const fromRepo = fileURLToPath(new URL('../../../../skills/tool-depot/references/tool-catalog.json', import.meta.url));
|
|
17
|
+
if (existsSync(fromRepo))
|
|
18
|
+
return fromRepo;
|
|
19
|
+
const fromPackage = fileURLToPath(new URL('../../../skills/dist/skills/tool-depot/references/tool-catalog.json', import.meta.url));
|
|
20
|
+
if (existsSync(fromPackage))
|
|
21
|
+
return fromPackage;
|
|
22
|
+
return fromRepo;
|
|
23
|
+
}
|
|
24
|
+
export function loadJsonFile(path, label) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new AuditUnusableError(`${label} is missing or is not valid JSON.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function loadCatalogFile(path) {
|
|
33
|
+
const catalog = loadJsonFile(path, 'Tool Depot catalog');
|
|
34
|
+
const tools = typeof catalog === 'object' && catalog !== null && !Array.isArray(catalog)
|
|
35
|
+
? catalog.tools
|
|
36
|
+
: undefined;
|
|
37
|
+
if (!Array.isArray(tools))
|
|
38
|
+
throw new AuditUnusableError('Tool Depot catalog has no tools array.');
|
|
39
|
+
const purposes = new Map();
|
|
40
|
+
for (const tool of tools) {
|
|
41
|
+
if (typeof tool !== 'object' || tool === null || Array.isArray(tool))
|
|
42
|
+
continue;
|
|
43
|
+
const obj = tool;
|
|
44
|
+
if (typeof obj.id === 'string' && obj.id.trim() !== '' && Array.isArray(obj.purposes)) {
|
|
45
|
+
purposes.set(obj.id, new Set(obj.purposes.filter((p) => typeof p === 'string')));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (purposes.size === 0)
|
|
49
|
+
throw new AuditUnusableError('Tool Depot catalog lists no usable tools.');
|
|
50
|
+
return purposes;
|
|
51
|
+
}
|
|
52
|
+
export function loadServerLogFile(path) {
|
|
53
|
+
let raw;
|
|
54
|
+
try {
|
|
55
|
+
raw = readFileSync(path, 'utf8');
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
throw new AuditUnusableError('Server log cannot be read.');
|
|
59
|
+
}
|
|
60
|
+
const entries = [];
|
|
61
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
62
|
+
let message = line;
|
|
63
|
+
if (line.trimStart().startsWith('{')) {
|
|
64
|
+
let record;
|
|
65
|
+
try {
|
|
66
|
+
record = JSON.parse(line);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (typeof record !== 'object' || record === null || Array.isArray(record))
|
|
72
|
+
continue;
|
|
73
|
+
const obj = record;
|
|
74
|
+
if (obj.SYSLOG_IDENTIFIER !== 'watcher-gate')
|
|
75
|
+
continue;
|
|
76
|
+
message = String(obj.MESSAGE ?? '');
|
|
77
|
+
}
|
|
78
|
+
else if (!line.includes('watcher-gate')) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const match = GATE_ENTRY_RE.exec(message);
|
|
82
|
+
if (match !== null)
|
|
83
|
+
entries.push([match[1], match[2]]);
|
|
84
|
+
}
|
|
85
|
+
if (entries.length === 0) {
|
|
86
|
+
throw new AuditUnusableError('Server log has no watcher-gate entries; export it with journalctl -t watcher-gate -o json.');
|
|
87
|
+
}
|
|
88
|
+
return entries.filter(([decision]) => decision === 'allowed').map(([, command]) => command);
|
|
89
|
+
}
|
|
90
|
+
export function resolveDefaultLedgerPath() {
|
|
91
|
+
const result = spawnSync('git', ['rev-parse', '--git-path', LEDGER_NAME], {
|
|
92
|
+
timeout: 10_000,
|
|
93
|
+
encoding: 'utf8',
|
|
94
|
+
});
|
|
95
|
+
if (result.error || result.signal || result.status !== 0) {
|
|
96
|
+
throw new AuditUnusableError('No ledger path given and the current directory is not a Git worktree.');
|
|
97
|
+
}
|
|
98
|
+
return result.stdout.trim();
|
|
99
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const ZED_CATALOG_LIMIT_BYTES: number;
|
|
2
|
+
export declare const SEMVER_RE: RegExp;
|
|
3
|
+
export interface ValidateReport {
|
|
4
|
+
verdict: 'pass' | 'fail';
|
|
5
|
+
checked: number;
|
|
6
|
+
frontmatter_bytes: number;
|
|
7
|
+
errors: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare function readPackageVersion(root: string): string | null;
|
|
10
|
+
export declare function validateRepo(root: string): ValidateReport;
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Infrastructure
|
|
3
|
+
* Contains: Repository skill, command, link, diagram, catalog, license, and version checks.
|
|
4
|
+
* Rules: Read-only repository inspection matching validate-skills.py diagnostics byte-for-byte.
|
|
5
|
+
* BigO: O(n^3) score:1
|
|
6
|
+
* keywords: [SkillsValidator, CatalogIntegrity, DiagramDrift]
|
|
7
|
+
* GOAL: Validate SKILL.md frontmatter, scripts/adapters, links, diagrams, and Tool Depot catalog.
|
|
8
|
+
*/
|
|
9
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
10
|
+
import { dirname, join, relative } from 'node:path';
|
|
11
|
+
import { compareCodePoints, pythonListRepr, pythonRepr } from '@bbcsoftwarefactory/cli-core/python-compat';
|
|
12
|
+
export const ZED_CATALOG_LIMIT_BYTES = 50 * 1024;
|
|
13
|
+
const METADATA_KEYS = new Set([
|
|
14
|
+
'author', 'version', 'pre_hook', 'post_hook', 'post_install', 'scripts', 'adapters',
|
|
15
|
+
]);
|
|
16
|
+
const CATALOG_REL = join('skills', 'tool-depot', 'references', 'tool-catalog.json');
|
|
17
|
+
const MD_LINK_RE = /\]\(([^)\s]+)\)/g;
|
|
18
|
+
const HOOK_FILE_RE = /\b((?:scripts|references|adapters)\/[A-Za-z0-9_.-]+\.[A-Za-z0-9]+)\b/g;
|
|
19
|
+
const CITED_ID_RE = /`([a-z]+(?:-[a-z0-9]+){2,})`/g;
|
|
20
|
+
const DIAGRAM_ALIASES = { 'watcher-observability': ['Sleuth'] };
|
|
21
|
+
const KNOWN_DIAGRAM_GAPS = new Map();
|
|
22
|
+
const SKILL_ROW_RE = /^\| \[([^\]]+)\]\(skills\/([^/]+)\/SKILL\.md\)/gm;
|
|
23
|
+
const GOVERNANCE_DOCS = ['SECURITY.md', 'SUPPORT.md', 'CONTRIBUTING.md'];
|
|
24
|
+
const EMAIL_RE = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/;
|
|
25
|
+
export const SEMVER_RE = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
26
|
+
const CHANGELOG_ENTRY_RE = /^## \[([^\]]+)\]/gm;
|
|
27
|
+
function isFile(path) {
|
|
28
|
+
try {
|
|
29
|
+
return statSync(path).isFile();
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function isDir(path) {
|
|
36
|
+
try {
|
|
37
|
+
return statSync(path).isDirectory();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function listFilesSorted(dir, predicate) {
|
|
44
|
+
if (!isDir(dir))
|
|
45
|
+
return [];
|
|
46
|
+
const collected = [];
|
|
47
|
+
const walk = (current) => {
|
|
48
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
49
|
+
const full = join(current, entry.name);
|
|
50
|
+
if (entry.isDirectory())
|
|
51
|
+
walk(full);
|
|
52
|
+
else if (entry.isFile() && predicate(full, entry.name))
|
|
53
|
+
collected.push(full);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
walk(dir);
|
|
57
|
+
collected.sort(compareCodePoints);
|
|
58
|
+
return collected;
|
|
59
|
+
}
|
|
60
|
+
function listDirectNamesSorted(dir, kind) {
|
|
61
|
+
if (!isDir(dir))
|
|
62
|
+
return [];
|
|
63
|
+
const names = [];
|
|
64
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
65
|
+
if (kind === 'dir' && entry.isDirectory())
|
|
66
|
+
names.push(entry.name);
|
|
67
|
+
if (kind === 'file' && entry.isFile())
|
|
68
|
+
names.push(entry.name);
|
|
69
|
+
}
|
|
70
|
+
names.sort(compareCodePoints);
|
|
71
|
+
return names;
|
|
72
|
+
}
|
|
73
|
+
function markdownFiles(root) {
|
|
74
|
+
const files = [];
|
|
75
|
+
for (const name of ['README.md', 'AGENTS.md', 'INSTALLATION.md']) {
|
|
76
|
+
const full = join(root, name);
|
|
77
|
+
if (isFile(full))
|
|
78
|
+
files.push(full);
|
|
79
|
+
}
|
|
80
|
+
for (const dirName of ['skills', 'commands', 'docs']) {
|
|
81
|
+
files.push(...listFilesSorted(join(root, dirName), (_full, name) => name.endsWith('.md')));
|
|
82
|
+
}
|
|
83
|
+
return files;
|
|
84
|
+
}
|
|
85
|
+
function checkLinks(root, mdFiles, errors) {
|
|
86
|
+
for (const md of mdFiles) {
|
|
87
|
+
const content = readFileSync(md, 'utf8');
|
|
88
|
+
const mdDir = dirname(md);
|
|
89
|
+
for (const match of content.matchAll(MD_LINK_RE)) {
|
|
90
|
+
const target = match[1];
|
|
91
|
+
if (/^[a-z][a-z0-9+.-]*:/.test(target) || target.startsWith('#') || target.includes('<'))
|
|
92
|
+
continue;
|
|
93
|
+
const pathPart = target.split('#', 1)[0];
|
|
94
|
+
if (pathPart === '' || /^[.\u2026]+$/.test(pathPart))
|
|
95
|
+
continue;
|
|
96
|
+
if (!existsSync(join(mdDir, pathPart))) {
|
|
97
|
+
errors.push(`${relative(root, md)}: broken link ${target}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function unescapeHtml(text) {
|
|
103
|
+
return text
|
|
104
|
+
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCodePoint(Number.parseInt(hex, 16)))
|
|
105
|
+
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(Number.parseInt(dec, 10)))
|
|
106
|
+
.replaceAll('"', '"')
|
|
107
|
+
.replaceAll(''', "'")
|
|
108
|
+
.replaceAll(''', "'")
|
|
109
|
+
.replaceAll('<', '<')
|
|
110
|
+
.replaceAll('>', '>')
|
|
111
|
+
.replaceAll('&', '&');
|
|
112
|
+
}
|
|
113
|
+
function diagramSources(root) {
|
|
114
|
+
const readme = join(root, 'README.md');
|
|
115
|
+
const agents = join(root, 'AGENTS.md');
|
|
116
|
+
const svg = join(root, 'docs', 'diagrams', 'skill-workflow.svg');
|
|
117
|
+
const excalidraw = join(root, 'docs', 'diagrams', 'skill-workflow.excalidraw');
|
|
118
|
+
if (![readme, agents, svg, excalidraw].every(isFile))
|
|
119
|
+
return null;
|
|
120
|
+
const readmeText = readFileSync(readme, 'utf8');
|
|
121
|
+
const agentsText = readFileSync(agents, 'utf8');
|
|
122
|
+
const mermaid = /```mermaid\r?\n([\s\S]*?)```/.exec(readmeText);
|
|
123
|
+
const handoffs = /## Skill handoffs\s+```text\r?\n([\s\S]*?)```/.exec(agentsText);
|
|
124
|
+
const svgRaw = readFileSync(svg, 'utf8');
|
|
125
|
+
const svgParts = [...svgRaw.matchAll(/>([^<>]+)</g)].map((match) => match[1]);
|
|
126
|
+
const svgText = unescapeHtml(svgParts.join(' '));
|
|
127
|
+
let elements = [];
|
|
128
|
+
try {
|
|
129
|
+
const parsed = JSON.parse(readFileSync(excalidraw, 'utf8'));
|
|
130
|
+
if (Array.isArray(parsed.elements))
|
|
131
|
+
elements = parsed.elements;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
elements = [];
|
|
135
|
+
}
|
|
136
|
+
const exText = elements
|
|
137
|
+
.filter((el) => typeof el === 'object' && el !== null && !Array.isArray(el) && el.type === 'text')
|
|
138
|
+
.map((el) => (typeof el.text === 'string' ? el.text : ''))
|
|
139
|
+
.join(' ');
|
|
140
|
+
return {
|
|
141
|
+
readmeText,
|
|
142
|
+
sources: {
|
|
143
|
+
mermaid: mermaid?.[1] ?? '',
|
|
144
|
+
agents: handoffs?.[1] ?? '',
|
|
145
|
+
svg: svgText,
|
|
146
|
+
excalidraw: exText,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function checkDiagram(root, errors) {
|
|
151
|
+
const loaded = diagramSources(root);
|
|
152
|
+
if (loaded === null)
|
|
153
|
+
return;
|
|
154
|
+
const { readmeText, sources } = loaded;
|
|
155
|
+
const names = new Map();
|
|
156
|
+
for (const match of readmeText.matchAll(SKILL_ROW_RE)) {
|
|
157
|
+
names.set(match[2], match[1]);
|
|
158
|
+
}
|
|
159
|
+
const loweredSources = Object.entries(sources).map(([source, text]) => [source, text.toLowerCase()]);
|
|
160
|
+
const skillsDir = join(root, 'skills');
|
|
161
|
+
for (const slug of listDirectNamesSorted(skillsDir, 'dir')) {
|
|
162
|
+
if (!isFile(join(skillsDir, slug, 'SKILL.md')))
|
|
163
|
+
continue;
|
|
164
|
+
const displayName = names.get(slug);
|
|
165
|
+
if (displayName === undefined) {
|
|
166
|
+
errors.push(`README.md: skill ${slug} is missing from the skill table`);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const aliases = DIAGRAM_ALIASES[slug] ?? [];
|
|
170
|
+
const labels = [displayName.toLowerCase(), slug, ...aliases.map((a) => a.toLowerCase())];
|
|
171
|
+
for (const [source, lower] of loweredSources) {
|
|
172
|
+
const found = labels.some((label) => lower.includes(label));
|
|
173
|
+
const gap = KNOWN_DIAGRAM_GAPS.get(`${slug}/${source}`);
|
|
174
|
+
if (!found && gap === undefined) {
|
|
175
|
+
errors.push(`diagram drift: ${slug} (${displayName}) is missing from the ${source} workflow`);
|
|
176
|
+
}
|
|
177
|
+
else if (found && gap !== undefined) {
|
|
178
|
+
errors.push(`diagram drift: stale known gap ${slug}/${source} (${gap}); remove it from KNOWN_DIAGRAM_GAPS`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function checkCatalog(root, errors) {
|
|
184
|
+
const path = join(root, CATALOG_REL);
|
|
185
|
+
if (!isFile(path))
|
|
186
|
+
return null;
|
|
187
|
+
let catalog;
|
|
188
|
+
try {
|
|
189
|
+
catalog = JSON.parse(readFileSync(path, 'utf8'));
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
193
|
+
errors.push(`${CATALOG_REL}: invalid JSON (${msg})`);
|
|
194
|
+
return new Set();
|
|
195
|
+
}
|
|
196
|
+
const tools = typeof catalog === 'object' && catalog !== null && !Array.isArray(catalog)
|
|
197
|
+
? catalog.tools
|
|
198
|
+
: undefined;
|
|
199
|
+
if (!Array.isArray(tools) || tools.length === 0) {
|
|
200
|
+
errors.push(`${CATALOG_REL}: "tools" must be a non-empty array`);
|
|
201
|
+
return new Set();
|
|
202
|
+
}
|
|
203
|
+
const purposes = new Set();
|
|
204
|
+
const ids = new Set();
|
|
205
|
+
const catalogDir = dirname(path);
|
|
206
|
+
for (let index = 0; index < tools.length; index++) {
|
|
207
|
+
const tool = tools[index];
|
|
208
|
+
const where = `${CATALOG_REL}: tools[${index}]`;
|
|
209
|
+
if (typeof tool !== 'object' || tool === null || Array.isArray(tool)) {
|
|
210
|
+
errors.push(`${where}: missing string id`);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const obj = tool;
|
|
214
|
+
if (typeof obj.id !== 'string' || obj.id === '') {
|
|
215
|
+
errors.push(`${where}: missing string id`);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (ids.has(obj.id))
|
|
219
|
+
errors.push(`${where}: duplicate tool id ${obj.id}`);
|
|
220
|
+
ids.add(obj.id);
|
|
221
|
+
const listed = Array.isArray(obj.purposes) && obj.purposes.length > 0 && obj.purposes.every((p) => typeof p === 'string' && p !== '')
|
|
222
|
+
? obj.purposes
|
|
223
|
+
: [];
|
|
224
|
+
if (listed.length === 0)
|
|
225
|
+
errors.push(`${where}: purposes must be a non-empty array of strings`);
|
|
226
|
+
for (const purpose of listed) {
|
|
227
|
+
if (purposes.has(purpose))
|
|
228
|
+
errors.push(`${where}: purpose ${purpose} is listed more than once`);
|
|
229
|
+
purposes.add(purpose);
|
|
230
|
+
}
|
|
231
|
+
if (typeof obj.scope !== 'string' || obj.scope.trim() === '')
|
|
232
|
+
errors.push(`${where}: missing scope`);
|
|
233
|
+
if (Array.isArray(obj.protocols)) {
|
|
234
|
+
for (const proto of obj.protocols) {
|
|
235
|
+
if (typeof proto === 'string' && !isFile(join(catalogDir, proto))) {
|
|
236
|
+
errors.push(`${where}: protocol ${proto} does not resolve`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return purposes;
|
|
242
|
+
}
|
|
243
|
+
function checkCitedPurposes(root, mdFiles, purposes, errors) {
|
|
244
|
+
const verbs = new Set();
|
|
245
|
+
for (const purpose of purposes)
|
|
246
|
+
verbs.add(purpose.split('-', 1)[0]);
|
|
247
|
+
for (const md of mdFiles) {
|
|
248
|
+
const content = readFileSync(md, 'utf8');
|
|
249
|
+
for (const match of content.matchAll(CITED_ID_RE)) {
|
|
250
|
+
const cited = match[1];
|
|
251
|
+
const verb = cited.split('-', 1)[0];
|
|
252
|
+
if (verbs.has(verb) && !purposes.has(cited)) {
|
|
253
|
+
errors.push(`${relative(root, md)}: purpose \`${cited}\` is not in the Tool Depot catalog`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function unquote(val) {
|
|
259
|
+
if (val.length >= 2 && ((val[0] === '"' && val.at(-1) === '"') || (val[0] === "'" && val.at(-1) === "'"))) {
|
|
260
|
+
return val.slice(1, -1);
|
|
261
|
+
}
|
|
262
|
+
return val;
|
|
263
|
+
}
|
|
264
|
+
function parseFrontmatter(text) {
|
|
265
|
+
const lines = text.split(/\r?\n/);
|
|
266
|
+
if (lines.length === 0 || lines[0].trim() !== '---') {
|
|
267
|
+
return { fm: null, bytes: 0, err: 'Missing opening --- frontmatter fence' };
|
|
268
|
+
}
|
|
269
|
+
let endIdx = -1;
|
|
270
|
+
for (let idx = 1; idx < lines.length; idx++) {
|
|
271
|
+
if (lines[idx].trim() === '---') {
|
|
272
|
+
endIdx = idx;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (endIdx === -1)
|
|
277
|
+
return { fm: null, bytes: 0, err: 'Missing closing --- frontmatter fence' };
|
|
278
|
+
const rawBlock = lines.slice(1, endIdx).join('\n');
|
|
279
|
+
const bytes = Buffer.byteLength(rawBlock, 'utf8');
|
|
280
|
+
const frontmatter = {};
|
|
281
|
+
let currentMap = null;
|
|
282
|
+
for (const line of lines.slice(1, endIdx)) {
|
|
283
|
+
if (line.trim() === '' || line.trimStart().startsWith('#'))
|
|
284
|
+
continue;
|
|
285
|
+
if (line.startsWith(' ') && currentMap !== null) {
|
|
286
|
+
const sub = line.trim();
|
|
287
|
+
const colon = sub.indexOf(':');
|
|
288
|
+
if (colon === -1)
|
|
289
|
+
return { fm: null, bytes, err: `Invalid metadata entry: ${sub}` };
|
|
290
|
+
currentMap[sub.slice(0, colon).trim()] = unquote(sub.slice(colon + 1).trim());
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
currentMap = null;
|
|
294
|
+
const colon = line.indexOf(':');
|
|
295
|
+
if (colon === -1)
|
|
296
|
+
return { fm: null, bytes, err: `Invalid frontmatter line: ${line}` };
|
|
297
|
+
const key = line.slice(0, colon).trim();
|
|
298
|
+
const val = line.slice(colon + 1).trim();
|
|
299
|
+
if (val === '') {
|
|
300
|
+
const nested = {};
|
|
301
|
+
frontmatter[key] = nested;
|
|
302
|
+
currentMap = nested;
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
frontmatter[key] = unquote(val);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return { fm: frontmatter, bytes, err: null };
|
|
309
|
+
}
|
|
310
|
+
function csvItems(value) {
|
|
311
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
312
|
+
return [];
|
|
313
|
+
return value.split(',').map((item) => item.trim()).filter((item) => item !== '');
|
|
314
|
+
}
|
|
315
|
+
function licenseSpdx(root) {
|
|
316
|
+
const path = join(root, 'LICENSE');
|
|
317
|
+
if (!isFile(path))
|
|
318
|
+
return null;
|
|
319
|
+
const text = readFileSync(path, 'utf8');
|
|
320
|
+
if (text.includes('Apache License') && text.includes('Version 2.0'))
|
|
321
|
+
return 'Apache-2.0';
|
|
322
|
+
if (text.includes('MIT License') || text.includes('Permission is hereby granted, free of charge'))
|
|
323
|
+
return 'MIT';
|
|
324
|
+
return '';
|
|
325
|
+
}
|
|
326
|
+
function profileIdentifiers(root) {
|
|
327
|
+
const found = new Map();
|
|
328
|
+
const commandsDir = join(root, 'commands');
|
|
329
|
+
for (const name of listDirectNamesSorted(commandsDir, 'dir')) {
|
|
330
|
+
const file = join(commandsDir, name, 'profile-identifiers.txt');
|
|
331
|
+
if (!isFile(file))
|
|
332
|
+
continue;
|
|
333
|
+
for (const line of readFileSync(file, 'utf8').split(/\r?\n/)) {
|
|
334
|
+
const word = line.split('#', 1)[0].trim();
|
|
335
|
+
if (word !== '')
|
|
336
|
+
found.set(word.toLowerCase(), name);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return found;
|
|
340
|
+
}
|
|
341
|
+
function coreFiles(root) {
|
|
342
|
+
const bases = [join(root, 'skills')];
|
|
343
|
+
const commandsDir = join(root, 'commands');
|
|
344
|
+
for (const name of listDirectNamesSorted(commandsDir, 'dir')) {
|
|
345
|
+
const entry = join(commandsDir, name);
|
|
346
|
+
if (!isFile(join(entry, 'profile-identifiers.txt')))
|
|
347
|
+
bases.push(entry);
|
|
348
|
+
}
|
|
349
|
+
const files = [];
|
|
350
|
+
for (const base of bases) {
|
|
351
|
+
files.push(...listFilesSorted(base, () => true));
|
|
352
|
+
}
|
|
353
|
+
return files;
|
|
354
|
+
}
|
|
355
|
+
function escapeRegex(text) {
|
|
356
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
357
|
+
}
|
|
358
|
+
function checkCoreGeneric(root, errors) {
|
|
359
|
+
const ids = profileIdentifiers(root);
|
|
360
|
+
if (ids.size === 0)
|
|
361
|
+
return;
|
|
362
|
+
const words = [...ids.keys()].sort((a, b) => b.length - a.length || compareCodePoints(a, b)).map(escapeRegex).join('|');
|
|
363
|
+
const leak = new RegExp(`(?<![A-Za-z0-9])(?:${words})(?![A-Za-z0-9])`, 'gi');
|
|
364
|
+
for (const filePath of coreFiles(root)) {
|
|
365
|
+
const text = readFileSync(filePath, 'utf8');
|
|
366
|
+
const hits = new Set();
|
|
367
|
+
for (const match of text.matchAll(leak))
|
|
368
|
+
hits.add(match[0].toLowerCase());
|
|
369
|
+
const relPosix = relative(root, filePath).replaceAll('\\', '/');
|
|
370
|
+
for (const hit of [...hits].sort(compareCodePoints)) {
|
|
371
|
+
errors.push(`${relPosix}: contains ${pythonRepr(hit)}, an identifier of the ${ids.get(hit)} profile; the core must stay generic`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
export function readPackageVersion(root) {
|
|
376
|
+
const path = join(root, 'VERSION');
|
|
377
|
+
return isFile(path) ? readFileSync(path, 'utf8').trim() : null;
|
|
378
|
+
}
|
|
379
|
+
function checkVersion(root, errors) {
|
|
380
|
+
const version = readPackageVersion(root);
|
|
381
|
+
if (version === null) {
|
|
382
|
+
errors.push('VERSION: missing; a licensed package declares its SemVer version');
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (!SEMVER_RE.test(version)) {
|
|
386
|
+
errors.push(`VERSION: ${pythonRepr(version)} is not SemVer (MAJOR.MINOR.PATCH)`);
|
|
387
|
+
}
|
|
388
|
+
const changelog = join(root, 'CHANGELOG.md');
|
|
389
|
+
if (!isFile(changelog)) {
|
|
390
|
+
errors.push('CHANGELOG.md: missing; every version needs a changelog entry');
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const entries = [...readFileSync(changelog, 'utf8').matchAll(CHANGELOG_ENTRY_RE)]
|
|
394
|
+
.map((m) => m[1])
|
|
395
|
+
.filter((entry) => entry.toLowerCase() !== 'unreleased');
|
|
396
|
+
if (entries.length === 0 || entries[0] !== version) {
|
|
397
|
+
const newest = entries[0] ?? 'none';
|
|
398
|
+
errors.push(`CHANGELOG.md: newest entry is ${pythonRepr(newest)}, but VERSION is ${pythonRepr(version)}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function checkGovernance(root, spdx, errors) {
|
|
402
|
+
if (spdx === null)
|
|
403
|
+
return;
|
|
404
|
+
for (const name of GOVERNANCE_DOCS) {
|
|
405
|
+
const path = join(root, name);
|
|
406
|
+
if (!isFile(path)) {
|
|
407
|
+
errors.push(`${name}: missing; a licensed package must ship it`);
|
|
408
|
+
}
|
|
409
|
+
else if (EMAIL_RE.test(readFileSync(path, 'utf8'))) {
|
|
410
|
+
errors.push(`${name}: contains an e-mail address; use the GitHub channels, no personal contact data`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
function validateEntry(group, entryName, entryDir, spdx, errors) {
|
|
415
|
+
const skillMd = join(entryDir, 'SKILL.md');
|
|
416
|
+
if (!isFile(skillMd)) {
|
|
417
|
+
errors.push(`${group}/${entryName}: missing SKILL.md`);
|
|
418
|
+
return { checked: 0, bytes: 0 };
|
|
419
|
+
}
|
|
420
|
+
const { fm, bytes, err } = parseFrontmatter(readFileSync(skillMd, 'utf8'));
|
|
421
|
+
if (err !== null || fm === null) {
|
|
422
|
+
errors.push(`${group}/${entryName}: ${err}`);
|
|
423
|
+
return { checked: 1, bytes };
|
|
424
|
+
}
|
|
425
|
+
const licenseId = fm.license;
|
|
426
|
+
if (spdx !== null && !licenseId) {
|
|
427
|
+
errors.push(`${group}/${entryName}: missing license (LICENSE is ${spdx})`);
|
|
428
|
+
}
|
|
429
|
+
else if (spdx && licenseId !== spdx) {
|
|
430
|
+
errors.push(`${group}/${entryName}: license ${pythonRepr(String(licenseId ?? ''))} != LICENSE (${spdx})`);
|
|
431
|
+
}
|
|
432
|
+
const name = fm.name;
|
|
433
|
+
if (name !== entryName) {
|
|
434
|
+
errors.push(`${group}/${entryName}: frontmatter name ${typeof name === 'string' ? pythonRepr(name) : 'None'} != folder name`);
|
|
435
|
+
}
|
|
436
|
+
if (typeof fm.description !== 'string' || fm.description.trim() === '') {
|
|
437
|
+
errors.push(`${group}/${entryName}: missing description`);
|
|
438
|
+
}
|
|
439
|
+
const meta = fm.metadata;
|
|
440
|
+
if (typeof meta !== 'object' || meta === null || Array.isArray(meta)) {
|
|
441
|
+
errors.push(`${group}/${entryName}: metadata must be a YAML mapping`);
|
|
442
|
+
return { checked: 1, bytes };
|
|
443
|
+
}
|
|
444
|
+
const metaMap = meta;
|
|
445
|
+
if (!metaMap.author || !metaMap.version) {
|
|
446
|
+
errors.push(`${group}/${entryName}: metadata requires author and version`);
|
|
447
|
+
}
|
|
448
|
+
const unknown = Object.keys(metaMap).filter((k) => !METADATA_KEYS.has(k)).sort(compareCodePoints);
|
|
449
|
+
if (unknown.length > 0) {
|
|
450
|
+
const allowedSorted = [...METADATA_KEYS].sort(compareCodePoints);
|
|
451
|
+
errors.push(`${group}/${entryName}: unknown metadata key(s) ${pythonListRepr(unknown)}; allowed: ${pythonListRepr(allowedSorted)}`);
|
|
452
|
+
}
|
|
453
|
+
const actualScripts = listDirectNamesSorted(join(entryDir, 'scripts'), 'file').map((n) => `scripts/${n}`);
|
|
454
|
+
const declaredScripts = csvItems(metaMap.scripts);
|
|
455
|
+
if (actualScripts.length > 0) {
|
|
456
|
+
if (!fm.compatibility)
|
|
457
|
+
errors.push(`${group}/${entryName}: scripted skill missing compatibility field`);
|
|
458
|
+
const sortedDeclared = [...declaredScripts].sort(compareCodePoints);
|
|
459
|
+
if (JSON.stringify(sortedDeclared) !== JSON.stringify(actualScripts)) {
|
|
460
|
+
errors.push(`${group}/${entryName}: metadata.scripts ${pythonListRepr(declaredScripts)} != disk ${pythonListRepr(actualScripts)}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else if (declaredScripts.length > 0) {
|
|
464
|
+
errors.push(`${group}/${entryName}: metadata.scripts declared without scripts/ files`);
|
|
465
|
+
}
|
|
466
|
+
for (const rel of declaredScripts) {
|
|
467
|
+
if (!isFile(join(entryDir, rel)))
|
|
468
|
+
errors.push(`${group}/${entryName}: missing declared script ${rel}`);
|
|
469
|
+
}
|
|
470
|
+
const actualAdapters = listDirectNamesSorted(join(entryDir, 'adapters'), 'file').map((n) => `adapters/${n}`);
|
|
471
|
+
const declaredAdapters = csvItems(metaMap.adapters);
|
|
472
|
+
if (actualAdapters.length > 0) {
|
|
473
|
+
const sortedAdapters = [...declaredAdapters].sort(compareCodePoints);
|
|
474
|
+
if (JSON.stringify(sortedAdapters) !== JSON.stringify(actualAdapters)) {
|
|
475
|
+
errors.push(`${group}/${entryName}: metadata.adapters ${pythonListRepr(declaredAdapters)} != disk ${pythonListRepr(actualAdapters)}`);
|
|
476
|
+
}
|
|
477
|
+
if (!metaMap.post_install) {
|
|
478
|
+
errors.push(`${group}/${entryName}: command with adapters missing metadata.post_install`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
else if (declaredAdapters.length > 0) {
|
|
482
|
+
errors.push(`${group}/${entryName}: metadata.adapters declared without adapters/ files`);
|
|
483
|
+
}
|
|
484
|
+
for (const key of ['pre_hook', 'post_hook', 'post_install']) {
|
|
485
|
+
const value = String(metaMap[key] ?? '');
|
|
486
|
+
for (const match of value.matchAll(HOOK_FILE_RE)) {
|
|
487
|
+
const rel = match[1];
|
|
488
|
+
if (!isFile(join(entryDir, rel))) {
|
|
489
|
+
errors.push(`${group}/${entryName}: metadata.${key} names missing file ${rel}`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (group === 'skills' && entryName === 'question-gate') {
|
|
494
|
+
if (!metaMap.pre_hook || !metaMap.post_hook) {
|
|
495
|
+
errors.push('skills/question-gate: must declare metadata.pre_hook and metadata.post_hook');
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return { checked: 1, bytes };
|
|
499
|
+
}
|
|
500
|
+
export function validateRepo(root) {
|
|
501
|
+
const errors = [];
|
|
502
|
+
const spdx = licenseSpdx(root);
|
|
503
|
+
let checked = 0;
|
|
504
|
+
let totalFrontmatterBytes = 0;
|
|
505
|
+
for (const group of ['skills', 'commands']) {
|
|
506
|
+
const groupDir = join(root, group);
|
|
507
|
+
for (const entryName of listDirectNamesSorted(groupDir, 'dir')) {
|
|
508
|
+
const result = validateEntry(group, entryName, join(groupDir, entryName), spdx, errors);
|
|
509
|
+
checked += result.checked;
|
|
510
|
+
totalFrontmatterBytes += result.bytes;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
checkGovernance(root, spdx, errors);
|
|
514
|
+
checkCoreGeneric(root, errors);
|
|
515
|
+
if (spdx !== null)
|
|
516
|
+
checkVersion(root, errors);
|
|
517
|
+
const mdFiles = markdownFiles(root);
|
|
518
|
+
checkLinks(root, mdFiles, errors);
|
|
519
|
+
checkDiagram(root, errors);
|
|
520
|
+
const purposes = checkCatalog(root, errors);
|
|
521
|
+
if (purposes !== null && purposes.size > 0) {
|
|
522
|
+
checkCitedPurposes(root, mdFiles, purposes, errors);
|
|
523
|
+
}
|
|
524
|
+
if (totalFrontmatterBytes > ZED_CATALOG_LIMIT_BYTES) {
|
|
525
|
+
errors.push(`Aggregate frontmatter ${totalFrontmatterBytes} bytes exceeds Zed ${ZED_CATALOG_LIMIT_BYTES} byte limit`);
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
verdict: errors.length === 0 ? 'pass' : 'fail',
|
|
529
|
+
checked,
|
|
530
|
+
frontmatter_bytes: totalFrontmatterBytes,
|
|
531
|
+
errors,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type HookFormat = 'json' | 'claude-hook' | 'cursor-hook' | 'codex-hook';
|
|
2
|
+
export interface GitWorktreeInfo {
|
|
3
|
+
is_linked_worktree: boolean;
|
|
4
|
+
branch: string;
|
|
5
|
+
toplevel: string;
|
|
6
|
+
}
|
|
7
|
+
export interface GuardReport {
|
|
8
|
+
decision: 'allow' | 'block' | 'unusable';
|
|
9
|
+
is_linked_worktree?: boolean;
|
|
10
|
+
branch?: string;
|
|
11
|
+
toplevel?: string;
|
|
12
|
+
reason: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function evaluateGitInfo(info: GitWorktreeInfo): {
|
|
15
|
+
report: GuardReport;
|
|
16
|
+
status: 0 | 1;
|
|
17
|
+
};
|
|
18
|
+
export declare function extractPayloadPath(payload: unknown): string | undefined;
|
|
19
|
+
export declare function formatHookOutput(report: GuardReport, status: number, format: Exclude<HookFormat, 'json'>): Record<string, string>;
|