@harnesspm/cli 0.0.1 → 0.17.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 +269 -0
- package/LICENSE +1 -1
- package/README.md +68 -3
- package/dist/added.d.ts +51 -0
- package/dist/added.js +192 -0
- package/dist/check.d.ts +14 -0
- package/dist/check.js +263 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +218 -0
- package/dist/commands.d.ts +58 -0
- package/dist/commands.js +1299 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.js +45 -0
- package/dist/deliver.d.ts +1 -0
- package/dist/deliver.js +31 -0
- package/dist/doctor.d.ts +18 -0
- package/dist/doctor.js +99 -0
- package/dist/exec-outputs.d.ts +75 -0
- package/dist/exec-outputs.js +425 -0
- package/dist/exec-units.d.ts +68 -0
- package/dist/exec-units.js +151 -0
- package/dist/fetch.d.ts +16 -0
- package/dist/fetch.js +145 -0
- package/dist/frontmatter.d.ts +16 -0
- package/dist/frontmatter.js +49 -0
- package/dist/homes.d.ts +7 -0
- package/dist/homes.js +21 -0
- package/dist/lock.d.ts +47 -0
- package/dist/lock.js +245 -0
- package/dist/manifest.d.ts +78 -0
- package/dist/manifest.js +169 -0
- package/dist/new.d.ts +33 -0
- package/dist/new.js +228 -0
- package/dist/outputs.d.ts +124 -0
- package/dist/outputs.js +875 -0
- package/dist/parent.d.ts +41 -0
- package/dist/parent.js +182 -0
- package/dist/render.d.ts +91 -0
- package/dist/render.js +221 -0
- package/dist/resolve.d.ts +132 -0
- package/dist/resolve.js +522 -0
- package/dist/reviewed.d.ts +15 -0
- package/dist/reviewed.js +41 -0
- package/dist/run.d.ts +18 -0
- package/dist/run.js +133 -0
- package/dist/sections.d.ts +32 -0
- package/dist/sections.js +121 -0
- package/dist/telemetry-api.d.ts +26 -0
- package/dist/telemetry-api.js +60 -0
- package/dist/telemetry.d.ts +119 -0
- package/dist/telemetry.js +401 -0
- package/dist/user-outputs.d.ts +30 -0
- package/dist/user-outputs.js +239 -0
- package/dist/util.d.ts +137 -0
- package/dist/util.js +370 -0
- package/dist/workspaces.d.ts +2 -0
- package/dist/workspaces.js +62 -0
- package/docs/spec/01-overview.md +149 -0
- package/docs/spec/02-package-format.md +252 -0
- package/docs/spec/03-resolution.md +108 -0
- package/docs/spec/04-outputs.md +194 -0
- package/docs/spec/05-executables.md +170 -0
- package/docs/spec/06-trust.md +88 -0
- package/docs/spec/07-added.md +109 -0
- package/docs/spec/08-improving.md +79 -0
- package/docs/spec/09-cloud.md +68 -0
- package/docs/spec/10-cli.md +187 -0
- package/docs/spec/11-standards.md +52 -0
- package/docs/spec/12-dogfooding.md +51 -0
- package/docs/spec/13-open-questions.md +97 -0
- package/docs/spec/14-telemetry.md +199 -0
- package/docs/spec/README.md +33 -0
- package/harness/dev.harnesspm/rules/generated-files.md +9 -0
- package/harness/dev.harnesspm/rules/harnesspm-config.md +12 -0
- package/harness/plugin.json +13 -0
- package/harness/skills/harnesspm-export/SKILL.md +43 -0
- package/harness/skills/harnesspm-import/SKILL.md +40 -0
- package/harness/skills/harnesspm-improve/SKILL.md +38 -0
- package/package.json +66 -11
- package/schema/harnesspm.schema.json +58 -0
- package/index.js +0 -4
package/dist/manifest.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Reading one harness package (spec chapter 2).
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { parseFrontmatter, stringList } from './frontmatter.js';
|
|
4
|
+
import { HarnessError, isDir, isFile, isRecord, listDir, readJson, readJsonIfExists, readText } from './util.js';
|
|
5
|
+
export const NS = 'dev.harnesspm';
|
|
6
|
+
export const PLUGIN_SCHEMA = 'https://agent-plugins.org/schemas/1.0.0/plugin.schema.json';
|
|
7
|
+
const CORE_FIELDS = new Set(['$schema', 'name', 'version', 'description', 'author', 'homepage', 'repository', 'license', 'keywords', 'extensions']);
|
|
8
|
+
const EXT_FIELDS = new Set(['extends', 'exclude', 'replace', 'patch', 'docs', 'scripts', 'projects']);
|
|
9
|
+
const PLUGIN_NAME = /^[a-z0-9][a-z0-9.-]*$/;
|
|
10
|
+
function stringMap(value, field, problems) {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
return {};
|
|
13
|
+
if (isRecord(value) && Object.values(value).every((v) => typeof v === 'string'))
|
|
14
|
+
return value;
|
|
15
|
+
problems.push(`"${field}" must map strings to strings`);
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
function repositoryUrl(value) {
|
|
19
|
+
if (typeof value === 'string')
|
|
20
|
+
return value;
|
|
21
|
+
if (isRecord(value) && typeof value['url'] === 'string')
|
|
22
|
+
return value['url'];
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Reads `<dir>/harness/plugin.json`. Returns undefined when the folder has no harness. */
|
|
26
|
+
export function loadHarnessPackage(dir, layer) {
|
|
27
|
+
const harnessDir = path.join(dir, 'harness');
|
|
28
|
+
const manifestFile = path.join(harnessDir, 'plugin.json');
|
|
29
|
+
if (!isFile(manifestFile))
|
|
30
|
+
return undefined;
|
|
31
|
+
const manifest = readJson(manifestFile);
|
|
32
|
+
if (!isRecord(manifest))
|
|
33
|
+
throw new HarnessError(`${manifestFile} must be a JSON object`);
|
|
34
|
+
const pkgJson = readJsonIfExists(path.join(dir, 'package.json'));
|
|
35
|
+
const pkg = isRecord(pkgJson) ? pkgJson : {};
|
|
36
|
+
const problems = [];
|
|
37
|
+
if (manifest['$schema'] !== PLUGIN_SCHEMA)
|
|
38
|
+
problems.push(`"$schema" must be "${PLUGIN_SCHEMA}"`);
|
|
39
|
+
for (const key of Object.keys(manifest))
|
|
40
|
+
if (!CORE_FIELDS.has(key))
|
|
41
|
+
problems.push(`unknown field "${key}" in plugin.json`);
|
|
42
|
+
const plugin = manifest['name'];
|
|
43
|
+
if (typeof plugin !== 'string' || !PLUGIN_NAME.test(plugin)) {
|
|
44
|
+
throw new HarnessError(`${manifestFile}: "name" must be lowercase letters, digits, "-" and "."`);
|
|
45
|
+
}
|
|
46
|
+
const ext = parseExtension(manifest, problems);
|
|
47
|
+
const version = typeof manifest['version'] === 'string' ? manifest['version'] : typeof pkg['version'] === 'string' ? pkg['version'] : '0.0.0';
|
|
48
|
+
const description = typeof manifest['description'] === 'string' ? manifest['description'] : undefined;
|
|
49
|
+
const repository = repositoryUrl(manifest['repository']) ?? repositoryUrl(pkg['repository']);
|
|
50
|
+
return {
|
|
51
|
+
name: typeof pkg['name'] === 'string' ? pkg['name'] : path.basename(dir),
|
|
52
|
+
version,
|
|
53
|
+
plugin,
|
|
54
|
+
...(description !== undefined ? { description } : {}),
|
|
55
|
+
...(repository !== undefined ? { repository } : {}),
|
|
56
|
+
dir,
|
|
57
|
+
harnessDir,
|
|
58
|
+
ext,
|
|
59
|
+
layer,
|
|
60
|
+
problems,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** A script's `answers`: sorted exit codes from 1 to 255. Absent is `[]`; undefined means the value is invalid. */
|
|
64
|
+
function answerCodes(value) {
|
|
65
|
+
if (value === undefined)
|
|
66
|
+
return [];
|
|
67
|
+
if (!Array.isArray(value) || !value.every((code) => Number.isInteger(code) && code >= 1 && code <= 255))
|
|
68
|
+
return undefined;
|
|
69
|
+
return [...new Set(value)].sort((a, b) => a - b);
|
|
70
|
+
}
|
|
71
|
+
/** Reads `extensions["dev.harnesspm"]` of a manifest. Problems go into `problems`. */
|
|
72
|
+
export function parseExtension(manifest, problems) {
|
|
73
|
+
const extensions = manifest['extensions'];
|
|
74
|
+
const rawExt = isRecord(extensions) && isRecord(extensions[NS]) ? extensions[NS] : {};
|
|
75
|
+
for (const key of Object.keys(rawExt))
|
|
76
|
+
if (!EXT_FIELDS.has(key))
|
|
77
|
+
problems.push(`unknown field "${key}" in extensions["${NS}"]`);
|
|
78
|
+
const list = (field) => {
|
|
79
|
+
const value = rawExt[field];
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
return [];
|
|
82
|
+
const result = stringList(value);
|
|
83
|
+
if (!result || typeof value === 'string') {
|
|
84
|
+
problems.push(`"${field}" must be an array of strings`);
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
const scripts = {};
|
|
90
|
+
if (rawExt['scripts'] !== undefined) {
|
|
91
|
+
if (!isRecord(rawExt['scripts']))
|
|
92
|
+
problems.push('"scripts" must be an object');
|
|
93
|
+
else {
|
|
94
|
+
for (const [name, def] of Object.entries(rawExt['scripts'])) {
|
|
95
|
+
if (!isRecord(def) || typeof def['file'] !== 'string') {
|
|
96
|
+
problems.push(`script "${name}" needs a "file"`);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (def['usage'] !== undefined && typeof def['usage'] !== 'string')
|
|
100
|
+
problems.push(`script "${name}": "usage" must be a string`);
|
|
101
|
+
const answers = answerCodes(def['answers']);
|
|
102
|
+
if (answers === undefined)
|
|
103
|
+
problems.push(`script "${name}": "answers" must be an array of exit codes from 1 to 255`);
|
|
104
|
+
scripts[name] = {
|
|
105
|
+
file: def['file'],
|
|
106
|
+
...(typeof def['description'] === 'string' ? { description: def['description'] } : {}),
|
|
107
|
+
...(typeof def['usage'] === 'string' && def['usage'].trim() !== '' ? { usage: def['usage'].trim() } : {}),
|
|
108
|
+
...(answers !== undefined && answers.length > 0 ? { answers } : {}),
|
|
109
|
+
...(def['allow'] === true ? { allow: true } : {}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
let docs;
|
|
115
|
+
if (rawExt['docs'] !== undefined) {
|
|
116
|
+
if (typeof rawExt['docs'] === 'string')
|
|
117
|
+
docs = rawExt['docs'];
|
|
118
|
+
else
|
|
119
|
+
problems.push('"docs" must be a path');
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
extends: list('extends'),
|
|
123
|
+
exclude: list('exclude'),
|
|
124
|
+
replace: stringMap(rawExt['replace'], 'replace', problems),
|
|
125
|
+
patch: stringMap(rawExt['patch'], 'patch', problems),
|
|
126
|
+
...(docs !== undefined ? { docs } : {}),
|
|
127
|
+
scripts,
|
|
128
|
+
projects: stringMap(rawExt['projects'], 'projects', problems),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** Finds a package's units. Files and folders in `skip` (replacements and patches) are not units. */
|
|
132
|
+
export function discoverUnits(pkg, skip) {
|
|
133
|
+
const rulesDir = path.join(pkg.harnessDir, NS, 'rules');
|
|
134
|
+
const rules = listDir(rulesDir)
|
|
135
|
+
.filter((f) => f.endsWith('.md') && !skip.has(path.join(rulesDir, f)))
|
|
136
|
+
.map((f) => {
|
|
137
|
+
const name = f.slice(0, -3);
|
|
138
|
+
return { kind: 'rules', id: `${pkg.plugin}:rules/${name}`, name, pkg, file: path.join(rulesDir, f) };
|
|
139
|
+
});
|
|
140
|
+
const skillsDir = path.join(pkg.harnessDir, 'skills');
|
|
141
|
+
const skills = listDir(skillsDir)
|
|
142
|
+
.filter((f) => isFile(path.join(skillsDir, f, 'SKILL.md')) && !skip.has(path.join(skillsDir, f)))
|
|
143
|
+
.map((name) => ({ kind: 'skills', id: `${pkg.plugin}:skills/${name}`, name, pkg, dir: path.join(skillsDir, name) }));
|
|
144
|
+
const units = { rules, skills };
|
|
145
|
+
if (pkg.ext.docs !== undefined) {
|
|
146
|
+
const dir = path.resolve(pkg.harnessDir, pkg.ext.docs);
|
|
147
|
+
if (isDir(dir))
|
|
148
|
+
units.docs = { kind: 'docs', id: `${pkg.plugin}:docs`, pkg, dir };
|
|
149
|
+
}
|
|
150
|
+
return units;
|
|
151
|
+
}
|
|
152
|
+
/** Reads a rule's activation from its frontmatter (spec 2.5). */
|
|
153
|
+
export function ruleMeta(text, fallbackTitle, file) {
|
|
154
|
+
const { data, body } = parseFrontmatter(text, file);
|
|
155
|
+
const description = typeof data['description'] === 'string' ? data['description'] : undefined;
|
|
156
|
+
const paths = stringList(data['paths']);
|
|
157
|
+
const activation = paths ? 'file' : description !== undefined ? 'demand' : 'always';
|
|
158
|
+
const titleMatch = /^#\s+(.+?)\s*$/m.exec(body);
|
|
159
|
+
return {
|
|
160
|
+
activation,
|
|
161
|
+
...(description !== undefined ? { description } : {}),
|
|
162
|
+
...(paths ? { paths } : {}),
|
|
163
|
+
title: titleMatch ? titleMatch[1] : fallbackTitle,
|
|
164
|
+
body,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function readRule(file) {
|
|
168
|
+
return readText(file);
|
|
169
|
+
}
|
package/dist/new.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Resolution } from './resolve.ts';
|
|
2
|
+
export declare const KINDS: readonly ['rule', 'skill', 'agent', 'script', 'hook', 'mcp'];
|
|
3
|
+
export type Kind = (typeof KINDS)[number];
|
|
4
|
+
export interface Target {
|
|
5
|
+
/** The folder with the layout of harness/: a harness/ folder or a project entry. */
|
|
6
|
+
dir: string;
|
|
7
|
+
plugin: string;
|
|
8
|
+
/** The manifest that declares scripts, patches and project entries for this target. */
|
|
9
|
+
manifest: string;
|
|
10
|
+
entry: boolean;
|
|
11
|
+
label: string;
|
|
12
|
+
/** Files that choosing the target wrote: a new harness/plugin.json, or an added harness's manifest that now lists the entry. */
|
|
13
|
+
written: string[];
|
|
14
|
+
}
|
|
15
|
+
/** Where `new` writes: the project's harness/, an added harness (`--to`), or its entry (`--entry`). */
|
|
16
|
+
export declare function resolveTarget(projectDir: string, options: {
|
|
17
|
+
to?: string;
|
|
18
|
+
entry?: boolean;
|
|
19
|
+
}): Target;
|
|
20
|
+
/** Creates a unit. Returns the files it wrote. */
|
|
21
|
+
export declare function createUnit(target: Target, kind: Kind, name: string): string[];
|
|
22
|
+
/**
|
|
23
|
+
* Writes a patch with a copy of the rule's sections and registers it (spec 10.2): in harness/plugin.json
|
|
24
|
+
* when the rule reaches the project through its own harness's `extends`, otherwise in harnesspm.json;
|
|
25
|
+
* with `--to`, in that added harness or its entry.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createPatch(projectDir: string, res: Resolution, ruleId: string, options: {
|
|
28
|
+
to?: string;
|
|
29
|
+
entry?: boolean;
|
|
30
|
+
}): {
|
|
31
|
+
file: string;
|
|
32
|
+
registeredIn: string;
|
|
33
|
+
};
|
package/dist/new.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// harnesspm new: units from templates, and patches (spec 10.2).
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { loadAdded, remoteKey } from './added.js';
|
|
5
|
+
import { CONFIG_FILE } from './config.js';
|
|
6
|
+
import { NS, PLUGIN_SCHEMA } from './manifest.js';
|
|
7
|
+
import { headings } from './sections.js';
|
|
8
|
+
import { parseFrontmatter } from './frontmatter.js';
|
|
9
|
+
import { HarnessError, NPX_ANY, ensureDir, exists, isFile, isRecord, readJson, readJsonIfExists, relPosix } from './util.js';
|
|
10
|
+
export const KINDS = ['rule', 'skill', 'agent', 'script', 'hook', 'mcp'];
|
|
11
|
+
const MCP_TEMPLATE = `// A minimal MCP server over stdio. Replace the tool with your own.
|
|
12
|
+
const send = (message) => process.stdout.write(JSON.stringify(message) + '\\n');
|
|
13
|
+
let buffer = '';
|
|
14
|
+
process.stdin.setEncoding('utf8');
|
|
15
|
+
process.stdin.on('data', (chunk) => {
|
|
16
|
+
buffer += chunk;
|
|
17
|
+
let index;
|
|
18
|
+
while ((index = buffer.indexOf('\\n')) >= 0) {
|
|
19
|
+
const line = buffer.slice(0, index).trim();
|
|
20
|
+
buffer = buffer.slice(index + 1);
|
|
21
|
+
if (!line) continue;
|
|
22
|
+
const message = JSON.parse(line);
|
|
23
|
+
if (message.method === 'initialize') {
|
|
24
|
+
send({ jsonrpc: '2.0', id: message.id, result: { protocolVersion: message.params?.protocolVersion ?? '2025-06-18', capabilities: { tools: {} }, serverInfo: { name: 'NAME', version: '0.1.0' } } });
|
|
25
|
+
} else if (message.method === 'tools/list') {
|
|
26
|
+
send({ jsonrpc: '2.0', id: message.id, result: { tools: [{ name: 'hello', description: 'Says hello', inputSchema: { type: 'object', properties: {} } }] } });
|
|
27
|
+
} else if (message.method === 'tools/call') {
|
|
28
|
+
send({ jsonrpc: '2.0', id: message.id, result: { content: [{ type: 'text', text: 'Hello from NAME' }] } });
|
|
29
|
+
} else if (message.id !== undefined) {
|
|
30
|
+
send({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Method not found' } });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
`;
|
|
35
|
+
const title = (name) => name.replace(/[-_]+/g, ' ').replace(/^\w/, (c) => c.toUpperCase());
|
|
36
|
+
function writeNew(file, content) {
|
|
37
|
+
if (exists(file))
|
|
38
|
+
throw new HarnessError(`${file} already exists`);
|
|
39
|
+
ensureDir(path.dirname(file));
|
|
40
|
+
fs.writeFileSync(file, content);
|
|
41
|
+
}
|
|
42
|
+
function editManifest(file, edit, plugin) {
|
|
43
|
+
const raw = readJsonIfExists(file);
|
|
44
|
+
const manifest = isRecord(raw) ? raw : { $schema: PLUGIN_SCHEMA, name: plugin };
|
|
45
|
+
const extensions = isRecord(manifest['extensions']) ? manifest['extensions'] : {};
|
|
46
|
+
const ext = isRecord(extensions[NS]) ? extensions[NS] : {};
|
|
47
|
+
edit(ext);
|
|
48
|
+
extensions[NS] = ext;
|
|
49
|
+
manifest['extensions'] = extensions;
|
|
50
|
+
ensureDir(path.dirname(file));
|
|
51
|
+
fs.writeFileSync(file, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
52
|
+
}
|
|
53
|
+
function pluginNameFor(projectDir) {
|
|
54
|
+
const pkg = readJsonIfExists(path.join(projectDir, 'package.json'));
|
|
55
|
+
const name = isRecord(pkg) && typeof pkg['name'] === 'string' ? pkg['name'] : path.basename(projectDir);
|
|
56
|
+
return name.replace(/^@[^/]+\//, '').toLowerCase().replace(/[^a-z0-9.-]+/g, '-').replace(/^[^a-z0-9]+/, '') || 'project';
|
|
57
|
+
}
|
|
58
|
+
/** Where `new` writes: the project's harness/, an added harness (`--to`), or its entry (`--entry`). */
|
|
59
|
+
export function resolveTarget(projectDir, options) {
|
|
60
|
+
if (options.to === undefined) {
|
|
61
|
+
if (options.entry)
|
|
62
|
+
throw new HarnessError('--entry needs --to <plugin>');
|
|
63
|
+
const dir = path.join(projectDir, 'harness');
|
|
64
|
+
const manifest = path.join(dir, 'plugin.json');
|
|
65
|
+
let plugin = pluginNameFor(projectDir);
|
|
66
|
+
const written = [];
|
|
67
|
+
if (isFile(manifest)) {
|
|
68
|
+
const raw = readJson(manifest);
|
|
69
|
+
if (isRecord(raw) && typeof raw['name'] === 'string')
|
|
70
|
+
plugin = raw['name'];
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
writeNew(manifest, `${JSON.stringify({ $schema: PLUGIN_SCHEMA, name: plugin }, null, 2)}\n`);
|
|
74
|
+
written.push(manifest);
|
|
75
|
+
}
|
|
76
|
+
return { dir, plugin, manifest, entry: false, label: `the project's own harness (${relPosix(projectDir, dir)}/)`, written };
|
|
77
|
+
}
|
|
78
|
+
const warnings = [];
|
|
79
|
+
const harness = loadAdded(projectDir, warnings).find((h) => h.pkg.plugin === options.to);
|
|
80
|
+
if (!harness)
|
|
81
|
+
throw new HarnessError(`No added harness with the plugin name "${options.to}". See \`${NPX_ANY} status\`.`);
|
|
82
|
+
if (!harness.source.editable)
|
|
83
|
+
throw new HarnessError(`${harness.source.spec} comes from npm and cannot be edited here. Change it in its own repository.`);
|
|
84
|
+
const manifest = path.join(harness.pkg.harnessDir, 'plugin.json');
|
|
85
|
+
if (!options.entry)
|
|
86
|
+
return { dir: harness.pkg.harnessDir, plugin: harness.pkg.plugin, manifest, entry: false, label: `${harness.pkg.plugin} (${harness.source.dir})`, written: [] };
|
|
87
|
+
const key = remoteKey(projectDir);
|
|
88
|
+
if (key === undefined)
|
|
89
|
+
throw new HarnessError('--entry needs a repository with an "origin" remote');
|
|
90
|
+
let rel = harness.pkg.ext.projects[key];
|
|
91
|
+
const written = [];
|
|
92
|
+
if (rel === undefined) {
|
|
93
|
+
rel = `./projects/${key.split('/').pop()}`;
|
|
94
|
+
editManifest(manifest, (ext) => {
|
|
95
|
+
const projects = isRecord(ext['projects']) ? ext['projects'] : {};
|
|
96
|
+
projects[key] = rel;
|
|
97
|
+
ext['projects'] = projects;
|
|
98
|
+
}, harness.pkg.plugin);
|
|
99
|
+
written.push(manifest);
|
|
100
|
+
}
|
|
101
|
+
const dir = path.resolve(harness.pkg.harnessDir, rel);
|
|
102
|
+
ensureDir(dir);
|
|
103
|
+
return { dir, plugin: harness.pkg.plugin, manifest: path.join(dir, 'plugin.json'), entry: true, label: `the entry of ${harness.pkg.plugin} for ${key} (${dir})`, written };
|
|
104
|
+
}
|
|
105
|
+
/** Creates a unit. Returns the files it wrote. */
|
|
106
|
+
export function createUnit(target, kind, name) {
|
|
107
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name))
|
|
108
|
+
throw new HarnessError('Use lowercase letters, digits and "-" in names');
|
|
109
|
+
if (target.entry && (kind === 'script' || kind === 'hook' || kind === 'mcp')) {
|
|
110
|
+
throw new HarnessError(`A project entry holds rules, skills, sub-agents and docs. Add the ${kind} to the harness itself (without --entry).`);
|
|
111
|
+
}
|
|
112
|
+
const files = [];
|
|
113
|
+
const add = (file, content) => {
|
|
114
|
+
writeNew(file, content);
|
|
115
|
+
files.push(file);
|
|
116
|
+
};
|
|
117
|
+
const scriptFile = (script) => path.join(target.dir, NS, 'scripts', `${script}.mjs`);
|
|
118
|
+
const declareScript = (script, description, usage) => {
|
|
119
|
+
editManifest(target.manifest, (ext) => {
|
|
120
|
+
const scripts = isRecord(ext['scripts']) ? ext['scripts'] : {};
|
|
121
|
+
if (scripts[script] !== undefined)
|
|
122
|
+
throw new HarnessError(`The script "${script}" already exists`);
|
|
123
|
+
scripts[script] = { file: `./${NS}/scripts/${script}.mjs`, ...(description !== undefined ? { description } : {}), ...(usage !== undefined ? { usage } : {}) };
|
|
124
|
+
ext['scripts'] = scripts;
|
|
125
|
+
}, target.plugin);
|
|
126
|
+
files.push(target.manifest);
|
|
127
|
+
};
|
|
128
|
+
switch (kind) {
|
|
129
|
+
case 'rule':
|
|
130
|
+
add(path.join(target.dir, NS, 'rules', `${name}.md`), `# ${title(name)}\n\n- Write the rule here. Keep an always-on rule short: it is in every session.\n`);
|
|
131
|
+
break;
|
|
132
|
+
case 'skill': {
|
|
133
|
+
const skill = name.startsWith(`${target.plugin}-`) ? name : `${target.plugin}-${name}`;
|
|
134
|
+
add(path.join(target.dir, 'skills', skill, 'SKILL.md'), `---\nname: ${skill}\ndescription: What the skill does. Use when the user asks to … or mentions ….\n---\n\n# ${title(name)}\n\n1. First step.\n2. Next step.\n`);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'agent':
|
|
138
|
+
add(path.join(target.dir, NS, 'agents', `${name}.md`), `---\nname: ${name}\ndescription: What the sub-agent does. Use when ….\n---\n\nInstructions for the sub-agent.\n`);
|
|
139
|
+
break;
|
|
140
|
+
case 'script':
|
|
141
|
+
add(scriptFile(name), `// Run it with \`${NPX_ANY} run ${target.plugin}:${name} -- <args>\`. \`help ${target.plugin}:${name}\` prints its "usage".\n// PLUGIN_ROOT, PLUGIN_DATA, HARNESSPM_PROJECT_DIR and HARNESSPM_AGENT are set by the runner.\nconsole.log('${name}', process.argv.slice(2).join(' '));\n`);
|
|
142
|
+
// `usage` is what `help <tool>` prints (spec 5.1). This template echoes its arguments.
|
|
143
|
+
declareScript(name, `What ${name} does`, '[<words>...]');
|
|
144
|
+
break;
|
|
145
|
+
case 'hook': {
|
|
146
|
+
add(scriptFile(name), `// A hook script: the agent's JSON event is on stdin. Exit 0 to continue, 2 to block (reason on stderr).\nimport { readFileSync } from 'node:fs';\nconst event = JSON.parse(readFileSync(0, 'utf8') || '{}');\nvoid event;\nprocess.exit(0);\n`);
|
|
147
|
+
declareScript(name);
|
|
148
|
+
const hooksFile = path.join(target.dir, NS, 'hooks.json');
|
|
149
|
+
const raw = readJsonIfExists(hooksFile);
|
|
150
|
+
const data = isRecord(raw) ? raw : {};
|
|
151
|
+
const hooks = isRecord(data['hooks']) ? data['hooks'] : {};
|
|
152
|
+
const list = Array.isArray(hooks['PostToolUse']) ? hooks['PostToolUse'] : [];
|
|
153
|
+
list.push({ id: name, matcher: { claude: 'Edit|Write', codex: 'apply_patch' }, run: name, timeout: 30 });
|
|
154
|
+
hooks['PostToolUse'] = list;
|
|
155
|
+
data['hooks'] = hooks;
|
|
156
|
+
fs.writeFileSync(hooksFile, `${JSON.stringify(data, null, 2)}\n`);
|
|
157
|
+
files.push(hooksFile);
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'mcp': {
|
|
161
|
+
const script = `${name}-mcp`;
|
|
162
|
+
add(scriptFile(script), MCP_TEMPLATE.replaceAll('NAME', `${target.plugin}-${name}`));
|
|
163
|
+
const mcpFile = path.join(target.dir, 'mcp.json');
|
|
164
|
+
const raw = readJsonIfExists(mcpFile);
|
|
165
|
+
const data = isRecord(raw) ? raw : { $schema: 'https://agent-plugins.org/schemas/1.0.0/mcp.schema.json' };
|
|
166
|
+
const servers = isRecord(data['mcpServers']) ? data['mcpServers'] : {};
|
|
167
|
+
if (servers[name] !== undefined)
|
|
168
|
+
throw new HarnessError(`The MCP server "${name}" already exists`);
|
|
169
|
+
servers[name] = { type: 'stdio', command: 'node', args: [`\${PLUGIN_ROOT}/${NS}/scripts/${script}.mjs`] };
|
|
170
|
+
data['mcpServers'] = servers;
|
|
171
|
+
fs.writeFileSync(mcpFile, `${JSON.stringify(data, null, 2)}\n`);
|
|
172
|
+
files.push(mcpFile);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return files;
|
|
177
|
+
}
|
|
178
|
+
/** The sections of a rule below its title: what a patch can replace. */
|
|
179
|
+
function patchTemplate(body) {
|
|
180
|
+
const lines = body.split('\n');
|
|
181
|
+
const hs = headings(lines);
|
|
182
|
+
const top = hs[0]?.level === 1 ? 1 : 0;
|
|
183
|
+
const first = hs.find((h) => h.level > top);
|
|
184
|
+
if (first)
|
|
185
|
+
return `${lines.slice(first.line).join('\n').trim()}\n`;
|
|
186
|
+
// Without sections below the title, the title's section is the whole rule: a patch of it replaces the rule.
|
|
187
|
+
return hs[0] ? `${lines.slice(hs[0].line).join('\n').trim()}\n` : '';
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Writes a patch with a copy of the rule's sections and registers it (spec 10.2): in harness/plugin.json
|
|
191
|
+
* when the rule reaches the project through its own harness's `extends`, otherwise in harnesspm.json;
|
|
192
|
+
* with `--to`, in that added harness or its entry.
|
|
193
|
+
*/
|
|
194
|
+
export function createPatch(projectDir, res, ruleId, options) {
|
|
195
|
+
const rule = res.rules.find((r) => r.id === ruleId);
|
|
196
|
+
if (!rule)
|
|
197
|
+
throw new HarnessError(`No rule ${ruleId}. Find IDs in the AGENTS.md block or with \`${NPX_ANY} why <ID>\`.`);
|
|
198
|
+
const fileName = `${ruleId.replace(':rules/', '-').replace(/[^a-z0-9.-]+/gi, '-')}.patch.md`;
|
|
199
|
+
const body = parseFrontmatter(rule.text, rule.source).body;
|
|
200
|
+
const content = patchTemplate(body) || `Text to add at the end of ${ruleId}.\n`;
|
|
201
|
+
const register = (manifest, baseDir, file, plugin) => {
|
|
202
|
+
writeNew(file, content);
|
|
203
|
+
editManifest(manifest, (ext) => {
|
|
204
|
+
const patch = isRecord(ext['patch']) ? ext['patch'] : {};
|
|
205
|
+
patch[ruleId] = `./${relPosix(baseDir, file)}`;
|
|
206
|
+
ext['patch'] = patch;
|
|
207
|
+
}, plugin);
|
|
208
|
+
return { file, registeredIn: manifest };
|
|
209
|
+
};
|
|
210
|
+
if (options.to !== undefined) {
|
|
211
|
+
const target = resolveTarget(projectDir, options);
|
|
212
|
+
return register(target.manifest, target.dir, path.join(target.dir, NS, 'patches', fileName), target.plugin);
|
|
213
|
+
}
|
|
214
|
+
const own = res.packages.find((p) => p.layer === 'project');
|
|
215
|
+
if (own && own.closure.has(rule.pkg.name)) {
|
|
216
|
+
return register(path.join(own.harnessDir, 'plugin.json'), own.harnessDir, path.join(own.harnessDir, NS, 'patches', fileName), own.plugin);
|
|
217
|
+
}
|
|
218
|
+
const file = path.join(projectDir, 'harnesspm', 'patches', fileName);
|
|
219
|
+
writeNew(file, content);
|
|
220
|
+
const configFile = path.join(projectDir, CONFIG_FILE);
|
|
221
|
+
const raw = readJsonIfExists(configFile);
|
|
222
|
+
const config = isRecord(raw) ? raw : {};
|
|
223
|
+
const patch = isRecord(config['patch']) ? config['patch'] : {};
|
|
224
|
+
patch[ruleId] = `./${relPosix(projectDir, file)}`;
|
|
225
|
+
config['patch'] = patch;
|
|
226
|
+
fs.writeFileSync(configFile, `${JSON.stringify(config, null, 2)}\n`);
|
|
227
|
+
return { file, registeredIn: configFile };
|
|
228
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type ExecEntries, type Targets } from './exec-outputs.ts';
|
|
2
|
+
import { type PathFor } from './render.ts';
|
|
3
|
+
import { type FinalRule, type Resolution, type ResolvedPackage } from './resolve.ts';
|
|
4
|
+
export declare const STATE_DIR: string;
|
|
5
|
+
/** Where the dependencies' docs are copied to, at the project root and outside node_modules (spec 4.3, note 8). */
|
|
6
|
+
export declare const DOCS_DIR: string;
|
|
7
|
+
/** The exclude entry and the `.gitignore` line that keep the root `.harnesspm/` folder out of git and of formatters. */
|
|
8
|
+
export declare const DOCS_EXCLUDE = "/.harnesspm/";
|
|
9
|
+
/** What harnesspm asks git about: the folder of the copies, so that a pattern for some files, such as `*.txt`, does not answer for all. */
|
|
10
|
+
export declare const DOCS_PROBE = ".harnesspm/docs/";
|
|
11
|
+
export declare const RGIGNORE_EXCLUDE = "/.rgignore";
|
|
12
|
+
export interface Plan {
|
|
13
|
+
/** Committed outputs. Undefined when the team layers were not resolved. */
|
|
14
|
+
committed?: {
|
|
15
|
+
agentsMd: string;
|
|
16
|
+
lock: string;
|
|
17
|
+
/** A monorepo's workspace files (spec 4.5). Undefined `agentsMd` removes a file that held only the block. */
|
|
18
|
+
workspaces: {
|
|
19
|
+
dir: string;
|
|
20
|
+
agentsMd: string | undefined;
|
|
21
|
+
}[];
|
|
22
|
+
/** The files that make Claude Code read each AGENTS.md, root first, with their text (spec 4.3, note 7). */
|
|
23
|
+
claudeMds: ClaudeImport[];
|
|
24
|
+
};
|
|
25
|
+
/** Per-machine files: absolute path → content. */
|
|
26
|
+
generated: Map<string, string>;
|
|
27
|
+
/** Folders whose generated files this plan replaces. */
|
|
28
|
+
generatedRoots: string[];
|
|
29
|
+
/** Per-machine links: absolute link path → absolute target. */
|
|
30
|
+
links: Map<string, string>;
|
|
31
|
+
/** Entries for the team section of .git/info/exclude. Undefined leaves the section as it is. */
|
|
32
|
+
teamExcludes?: string[];
|
|
33
|
+
addedExcludes: string[];
|
|
34
|
+
/** New CLAUDE.local.md, or undefined to remove harnesspm's block from it. */
|
|
35
|
+
localMd: string | undefined;
|
|
36
|
+
/** Text for Codex's developer_instructions. Empty when there is none. */
|
|
37
|
+
codexInstructions: string;
|
|
38
|
+
/** False when .codex/ belongs to the repository. */
|
|
39
|
+
codexOurs: boolean;
|
|
40
|
+
/** False when the repository tracks .claude/settings.local.json. */
|
|
41
|
+
claudeOurs: boolean;
|
|
42
|
+
/** Executable entries per group. The team group is missing when the team layers were not resolved. */
|
|
43
|
+
exec: {
|
|
44
|
+
team?: ExecEntries;
|
|
45
|
+
added: ExecEntries;
|
|
46
|
+
};
|
|
47
|
+
/** The `.rgignore` that lets search tools into the docs copies: its text, `null` to remove harnesspm's, or undefined to leave the file as it is. */
|
|
48
|
+
rgignore?: string | null;
|
|
49
|
+
/** The index that harnesspm's agent hooks read (spec 14.4), or undefined to remove it. */
|
|
50
|
+
telemetryIndex?: string;
|
|
51
|
+
/** A telemetry handler that this run may not record (spec 14.5). */
|
|
52
|
+
telemetryHeld?: string;
|
|
53
|
+
warnings: string[];
|
|
54
|
+
}
|
|
55
|
+
export interface ClaudeImport {
|
|
56
|
+
/** The folder of the AGENTS.md. */
|
|
57
|
+
dir: string;
|
|
58
|
+
file: string;
|
|
59
|
+
/** The file's text after sync. Equal to the file on disk when nothing is missing. */
|
|
60
|
+
text: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The CLAUDE.md that makes Claude Code read the AGENTS.md in `dir` (spec 4.3, note 7): `CLAUDE.md` gets `@AGENTS.md`,
|
|
64
|
+
* unless the folder's `.claude/CLAUDE.md` already imports `@../AGENTS.md`.
|
|
65
|
+
*/
|
|
66
|
+
export declare function claudeImport(dir: string): ClaudeImport;
|
|
67
|
+
export declare const isUserLevel: (pkg: ResolvedPackage) => boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Paths that outputs show: stable package paths for dependencies, project-relative paths inside the
|
|
70
|
+
* project, absolute paths elsewhere. Generated docs indexes are added to `generated`. `absolute` shows every path
|
|
71
|
+
* in full, for user-level files: an agent reads them from any folder (spec 4.3, note 4).
|
|
72
|
+
*/
|
|
73
|
+
export declare function pathTools(res: Resolution, generatedRoot: (pkg: ResolvedPackage) => string, generated: Map<string, string>, warnings: string[], options?: {
|
|
74
|
+
absolute?: boolean;
|
|
75
|
+
}): {
|
|
76
|
+
show: (abs: string) => string;
|
|
77
|
+
display: (file: string) => string;
|
|
78
|
+
stablePath: (file: string) => string | undefined;
|
|
79
|
+
generatedRule: (rule: FinalRule) => string;
|
|
80
|
+
docsCopy: (pkg: ResolvedPackage) => string | undefined;
|
|
81
|
+
paths: PathFor;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* `team` is the dependency and project layers alone, for committed outputs. `full` adds the added
|
|
85
|
+
* harnesses, for per-machine outputs. Without `team`, only the outputs of added harnesses change.
|
|
86
|
+
*/
|
|
87
|
+
export declare function computePlan(team: Resolution | undefined, full: Resolution, options?: {
|
|
88
|
+
widen?: boolean;
|
|
89
|
+
}): Plan;
|
|
90
|
+
/** The per-machine paths that harnesspm's last sync listed in .git/info/exclude, from the project folder, such as `/CLAUDE.local.md`. */
|
|
91
|
+
export declare function excludedBySync(projectDir: string): string[];
|
|
92
|
+
/**
|
|
93
|
+
* The per-machine record of the entries harnesspm wrote for this project (spec 4.7): in the clone's git folder, or,
|
|
94
|
+
* in a folder that is not a git repository, in ~/.harnesspm/state/, so sync never creates a `.git` folder there.
|
|
95
|
+
* A project below the git root has its own, such as `harnesspm/docs-site/state.json`, so that two projects in one
|
|
96
|
+
* repository never take each other's entries for their own.
|
|
97
|
+
*/
|
|
98
|
+
export declare function repoStateFile(projectDir: string): string;
|
|
99
|
+
export declare function repoTargets(projectDir: string, plan: Pick<Plan, 'claudeOurs' | 'codexOurs'>): Targets;
|
|
100
|
+
export interface ApplyResult {
|
|
101
|
+
changed: string[];
|
|
102
|
+
/** Entries a model run could not add (spec 6.2, rule 4). */
|
|
103
|
+
held: string[];
|
|
104
|
+
/** True when some of them come from the repository's layers, not from added harnesses. */
|
|
105
|
+
teamHeld: boolean;
|
|
106
|
+
}
|
|
107
|
+
/** Writes or removes the `.rgignore` that lets search tools into the docs copies (spec 4.3). */
|
|
108
|
+
export declare function writeRgignore(projectDir: string, plan: Plan): void;
|
|
109
|
+
/** Where the `/.harnesspm/` line goes: the .gitignore of the project folder, named when it is below the git root. */
|
|
110
|
+
export declare function docsIgnoreAdvice(projectDir: string): string;
|
|
111
|
+
/** Adds `entries` to the project's section of .git/info/exclude and keeps the entries a sync wrote there. */
|
|
112
|
+
export declare function addExcludes(projectDir: string, entries: string[]): void;
|
|
113
|
+
/**
|
|
114
|
+
* Writes the plan. Returns the project-relative paths that changed and the entries held back.
|
|
115
|
+
* `committed: false` writes only the per-machine outputs, as a sync with `CI=true` does (spec 6.4).
|
|
116
|
+
* `reviewed: true` lets a model run write the repository's entries, because they are as a person reviewed them (spec
|
|
117
|
+
* 6.2, rule 4). Entries of added harnesses still wait for a person.
|
|
118
|
+
*/
|
|
119
|
+
export declare function applyPlan(projectDir: string, plan: Plan, options?: {
|
|
120
|
+
committed?: boolean;
|
|
121
|
+
reviewed?: boolean;
|
|
122
|
+
}): ApplyResult;
|
|
123
|
+
/** Compares the committed outputs with the plan (spec 6.4). Returns the differences. */
|
|
124
|
+
export declare function checkPlan(projectDir: string, plan: Plan): string[];
|