@harnesspm/cli 0.0.1 → 0.18.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 +275 -0
- package/LICENSE +1 -1
- package/README.md +71 -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 +230 -0
- package/dist/commands.d.ts +68 -0
- package/dist/commands.js +1441 -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 +77 -0
- package/dist/exec-outputs.js +426 -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/start.d.ts +18 -0
- package/dist/start.js +136 -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 +156 -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 +215 -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 +32 -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/resolve.js
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
// Resolution of the dependency and project layers (spec chapter 3).
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { CONFIG_FILE, emptyConfig, readConfig } from './config.js';
|
|
4
|
+
import { discoverUnits, loadHarnessPackage, ruleMeta, } from './manifest.js';
|
|
5
|
+
import { discoverExecUnits } from './exec-units.js';
|
|
6
|
+
import { applyPatch } from './sections.js';
|
|
7
|
+
import { findWorkspaces } from './workspaces.js';
|
|
8
|
+
import { HarnessError, OLD_PACKAGE_NAMES, PACKAGE_NAME, compareVersions, gitIgnoresDir, insideDir, isFile, isRecord, readJsonIfExists, readText, realpath, relPosix } from './util.js';
|
|
9
|
+
export function rulesById(rules) {
|
|
10
|
+
const groups = new Map();
|
|
11
|
+
for (const rule of rules) {
|
|
12
|
+
const group = groups.get(rule.id) ?? [];
|
|
13
|
+
group.push(rule);
|
|
14
|
+
groups.set(rule.id, group);
|
|
15
|
+
}
|
|
16
|
+
return groups;
|
|
17
|
+
}
|
|
18
|
+
/** Inside a workspace, select its rules. At the root, include workspace-only rules in the repository overview. */
|
|
19
|
+
export function rulesForDirectory(res, dir) {
|
|
20
|
+
const ws = res.workspaces.findLast((ws) => insideDir(ws.dir, dir));
|
|
21
|
+
if (ws)
|
|
22
|
+
return res.rules.filter((rule) => rule.members.has(ws.key));
|
|
23
|
+
return [...rulesById(res.rules).values()].flatMap((variants) => {
|
|
24
|
+
const root = variants.find((rule) => rule.members.has(''));
|
|
25
|
+
return root ? [root] : variants;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export const describeMembers = (members) => [...members].map((key) => key === '' ? 'repository root' : `\`${key}\``).join(', ');
|
|
29
|
+
/** Changes to the team's rule text on this machine, with the affected members of each version. */
|
|
30
|
+
export function localRuleChanges(team, full) {
|
|
31
|
+
const fullRules = rulesById(full.rules);
|
|
32
|
+
return [...rulesById(team.rules)].flatMap(([id, previous]) => {
|
|
33
|
+
const current = fullRules.get(id);
|
|
34
|
+
if (!current)
|
|
35
|
+
return [{ id, members: new Set(previous.flatMap((rule) => [...rule.members])) }];
|
|
36
|
+
return current.flatMap((changed) => {
|
|
37
|
+
const members = new Set(previous
|
|
38
|
+
.filter((rule) => rule.text !== changed.text)
|
|
39
|
+
.flatMap((rule) => [...rule.members].filter((key) => changed.members.has(key))));
|
|
40
|
+
return members.size > 0 ? [{ id, changed, members }] : [];
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/** Units that `replace` works on, and what a replacement is: a rule's file, or a skill's folder with its SKILL.md (spec 3.4). */
|
|
45
|
+
const replaceable = (id) => (id.includes(':rules/') ? 'rule' : id.includes(':skills/') ? 'skill' : undefined);
|
|
46
|
+
const replacementExists = (id, file) => (replaceable(id) === 'skill' ? isFile(path.join(file, 'SKILL.md')) : isFile(file));
|
|
47
|
+
/**
|
|
48
|
+
* The package whose files a skill uses: the one whose `replace` put them in the skill's place, else its own. Trust
|
|
49
|
+
* follows these files (spec 6.1). Undefined when `harnesspm.json` replaced the skill with a file of the project.
|
|
50
|
+
*/
|
|
51
|
+
export const skillAuthor = (skill) => (skill.replacedBy ? skill.replacedBy.from : skill.pkg);
|
|
52
|
+
/** Finds `<name>` in node_modules from `fromDir` upward, the way Node.js does. */
|
|
53
|
+
export function resolvePackageDir(name, fromDir) {
|
|
54
|
+
let dir = fromDir;
|
|
55
|
+
for (;;) {
|
|
56
|
+
const candidate = path.join(dir, 'node_modules', name, 'package.json');
|
|
57
|
+
if (isFile(candidate))
|
|
58
|
+
return realpath(path.dirname(candidate));
|
|
59
|
+
const parent = path.dirname(dir);
|
|
60
|
+
if (parent === dir)
|
|
61
|
+
return undefined;
|
|
62
|
+
dir = parent;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function dependencyNames(pkgJson) {
|
|
66
|
+
const names = new Set();
|
|
67
|
+
for (const field of ['dependencies', 'devDependencies', 'optionalDependencies']) {
|
|
68
|
+
const deps = pkgJson[field];
|
|
69
|
+
if (isRecord(deps))
|
|
70
|
+
for (const name of Object.keys(deps))
|
|
71
|
+
names.add(name);
|
|
72
|
+
}
|
|
73
|
+
return [...names].sort();
|
|
74
|
+
}
|
|
75
|
+
const isUnitId = (entry) => entry.includes(':');
|
|
76
|
+
/** True when `name` is in `peerDependencies` of the package at `dir` and marked optional in `peerDependenciesMeta`. */
|
|
77
|
+
function isOptionalPeer(dir, name) {
|
|
78
|
+
const raw = readJsonIfExists(path.join(dir, 'package.json'));
|
|
79
|
+
if (!isRecord(raw) || !isRecord(raw['peerDependencies']) || raw['peerDependencies'][name] === undefined)
|
|
80
|
+
return false;
|
|
81
|
+
const meta = isRecord(raw['peerDependenciesMeta']) ? raw['peerDependenciesMeta'][name] : undefined;
|
|
82
|
+
return isRecord(meta) && meta['optional'] === true;
|
|
83
|
+
}
|
|
84
|
+
export function resolve(projectDirInput, options = {}) {
|
|
85
|
+
const projectDir = realpath(projectDirInput);
|
|
86
|
+
const team = options.team ?? true;
|
|
87
|
+
const warnings = [];
|
|
88
|
+
const config = team ? readConfig(projectDir) : emptyConfig();
|
|
89
|
+
warnings.push(...config.warnings);
|
|
90
|
+
const pkgJsonRaw = readJsonIfExists(path.join(projectDir, 'package.json'));
|
|
91
|
+
const pkgJson = isRecord(pkgJsonRaw) ? pkgJsonRaw : {};
|
|
92
|
+
const projectName = typeof pkgJson['name'] === 'string' ? pkgJson['name'] : path.basename(projectDir);
|
|
93
|
+
const cache = new Map();
|
|
94
|
+
const load = (dir, layer) => {
|
|
95
|
+
if (!cache.has(dir))
|
|
96
|
+
cache.set(dir, loadHarnessPackage(dir, layer) ?? null);
|
|
97
|
+
return cache.get(dir) ?? undefined;
|
|
98
|
+
};
|
|
99
|
+
const occurrences = new Map();
|
|
100
|
+
const members = new Map();
|
|
101
|
+
const addMember = (name, key) => {
|
|
102
|
+
const set = members.get(name) ?? new Set();
|
|
103
|
+
set.add(key);
|
|
104
|
+
members.set(name, set);
|
|
105
|
+
};
|
|
106
|
+
const edges = new Map();
|
|
107
|
+
const workspaceClosures = new Map();
|
|
108
|
+
const excludedPackages = [];
|
|
109
|
+
const addOccurrence = (name, occurrence) => {
|
|
110
|
+
const list = occurrences.get(name) ?? [];
|
|
111
|
+
list.push(occurrence);
|
|
112
|
+
occurrences.set(name, list);
|
|
113
|
+
};
|
|
114
|
+
const addEdge = (from, to) => {
|
|
115
|
+
const set = edges.get(from) ?? new Set();
|
|
116
|
+
set.add(to);
|
|
117
|
+
edges.set(from, set);
|
|
118
|
+
};
|
|
119
|
+
/** `excluded` maps a package name to whoever excluded it on this path. */
|
|
120
|
+
const visit = (pkg, depth, excluded, stack, root) => {
|
|
121
|
+
const childExcluded = new Map(excluded);
|
|
122
|
+
const reached = new Set();
|
|
123
|
+
for (const name of pkg.ext.exclude.filter((e) => !isUnitId(e)))
|
|
124
|
+
childExcluded.set(name, pkg.name);
|
|
125
|
+
for (const name of pkg.ext.extends) {
|
|
126
|
+
const by = childExcluded.get(name);
|
|
127
|
+
if (by !== undefined) {
|
|
128
|
+
excludedPackages.push({ name, by });
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const dir = resolvePackageDir(name, pkg.dir) ?? options.locate?.(name);
|
|
132
|
+
if (!dir) {
|
|
133
|
+
// An optional peer that the project did not install is skipped quietly (spec 3.3).
|
|
134
|
+
if (!isOptionalPeer(pkg.dir, name))
|
|
135
|
+
warnings.push(`${pkg.name} extends ${name}, which is not installed`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (stack.includes(dir)) {
|
|
139
|
+
throw new HarnessError(`extends cycle: ${[...stack.map((d) => cache.get(d)?.name ?? d), name].join(' → ')}`);
|
|
140
|
+
}
|
|
141
|
+
const child = load(dir, 'dependencies');
|
|
142
|
+
if (!child) {
|
|
143
|
+
warnings.push(`${pkg.name} extends ${name}, which has no harness/ folder`);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
addEdge(pkg.name, name);
|
|
147
|
+
addOccurrence(name, { dir, depth: depth + 1, direct: false, via: pkg.name });
|
|
148
|
+
addMember(name, root);
|
|
149
|
+
reached.add(name);
|
|
150
|
+
for (const descendant of visit(child, depth + 1, childExcluded, [...stack, dir], root))
|
|
151
|
+
reached.add(descendant);
|
|
152
|
+
}
|
|
153
|
+
const closures = workspaceClosures.get(root) ?? new Map();
|
|
154
|
+
const closure = closures.get(pkg.name) ?? new Set();
|
|
155
|
+
for (const name of reached)
|
|
156
|
+
closure.add(name);
|
|
157
|
+
closures.set(pkg.name, closure);
|
|
158
|
+
workspaceClosures.set(root, closures);
|
|
159
|
+
return reached;
|
|
160
|
+
};
|
|
161
|
+
// Each workspace is resolved on its own dependencies, from its own folder (spec 3.6).
|
|
162
|
+
const workspaces = team ? findWorkspaces(projectDir).map((dir) => ({ key: relPosix(projectDir, dir), dir: realpath(dir) })) : [];
|
|
163
|
+
const roots = [{ key: '', dir: projectDir, deps: dependencyNames(pkgJson) }, ...workspaces.map((ws) => {
|
|
164
|
+
const raw = readJsonIfExists(path.join(ws.dir, 'package.json'));
|
|
165
|
+
return { key: ws.key, dir: ws.dir, deps: dependencyNames(isRecord(raw) ? raw : {}) };
|
|
166
|
+
})];
|
|
167
|
+
const rootExcluded = new Map(config.exclude.filter((e) => !isUnitId(e)).map((name) => [name, CONFIG_FILE]));
|
|
168
|
+
for (const root of team ? roots : []) {
|
|
169
|
+
for (const name of root.deps) {
|
|
170
|
+
if (rootExcluded.has(name)) {
|
|
171
|
+
excludedPackages.push({ name, by: CONFIG_FILE });
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const dir = resolvePackageDir(name, root.dir) ?? options.locate?.(name);
|
|
175
|
+
if (!dir)
|
|
176
|
+
continue;
|
|
177
|
+
const pkg = load(dir, 'dependencies');
|
|
178
|
+
if (!pkg)
|
|
179
|
+
continue;
|
|
180
|
+
addOccurrence(name, { dir, depth: 0, direct: true });
|
|
181
|
+
addMember(name, root.key);
|
|
182
|
+
visit(pkg, 0, rootExcluded, [dir], root.key);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const projectHarness = team ? loadHarnessPackage(projectDir, 'project') : undefined;
|
|
186
|
+
if (projectHarness) {
|
|
187
|
+
projectHarness.name = projectName;
|
|
188
|
+
visit(projectHarness, 0, rootExcluded, [projectDir], '');
|
|
189
|
+
}
|
|
190
|
+
// A workspace's own harness belongs to that workspace, unless another workspace already reaches it as a dependency.
|
|
191
|
+
const workspaceHarnesses = [];
|
|
192
|
+
for (const ws of workspaces) {
|
|
193
|
+
const harness = loadHarnessPackage(ws.dir, 'project');
|
|
194
|
+
if (!harness)
|
|
195
|
+
continue;
|
|
196
|
+
workspaceHarnesses.push({ pkg: harness, key: ws.key });
|
|
197
|
+
visit(harness, 0, rootExcluded, [ws.dir], ws.key);
|
|
198
|
+
}
|
|
199
|
+
// One version per package in a repository (spec 3.5).
|
|
200
|
+
const chosen = new Map();
|
|
201
|
+
for (const [name, list] of occurrences) {
|
|
202
|
+
const sorted = [...list].sort((a, b) => {
|
|
203
|
+
if (a.direct !== b.direct)
|
|
204
|
+
return a.direct ? -1 : 1;
|
|
205
|
+
if (a.depth !== b.depth)
|
|
206
|
+
return a.depth - b.depth;
|
|
207
|
+
return compareVersions(load(b.dir, 'dependencies').version, load(a.dir, 'dependencies').version);
|
|
208
|
+
});
|
|
209
|
+
const first = sorted[0];
|
|
210
|
+
const versions = [...new Set(list.map((o) => load(o.dir, 'dependencies').version))];
|
|
211
|
+
if (new Set(list.map((o) => o.dir)).size > 1 && versions.length > 1) {
|
|
212
|
+
warnings.push(`${name}: found versions ${versions.join(', ')}; using ${load(first.dir, 'dependencies').version}`);
|
|
213
|
+
}
|
|
214
|
+
chosen.set(name, { pkg: load(first.dir, 'dependencies'), occurrence: first });
|
|
215
|
+
}
|
|
216
|
+
const closureOf = (name) => {
|
|
217
|
+
const result = new Set();
|
|
218
|
+
const stack = [...(edges.get(name) ?? [])];
|
|
219
|
+
while (stack.length > 0) {
|
|
220
|
+
const next = stack.pop();
|
|
221
|
+
if (result.has(next) || !chosen.has(next))
|
|
222
|
+
continue;
|
|
223
|
+
result.add(next);
|
|
224
|
+
stack.push(...(edges.get(next) ?? []));
|
|
225
|
+
}
|
|
226
|
+
return result;
|
|
227
|
+
};
|
|
228
|
+
// Upstream first: a package comes after the packages it extends. Unrelated packages by name.
|
|
229
|
+
const order = [];
|
|
230
|
+
const seen = new Set();
|
|
231
|
+
const walk = (name) => {
|
|
232
|
+
if (seen.has(name) || !chosen.has(name))
|
|
233
|
+
return;
|
|
234
|
+
seen.add(name);
|
|
235
|
+
for (const child of [...(edges.get(name) ?? [])].sort())
|
|
236
|
+
walk(child);
|
|
237
|
+
order.push(name);
|
|
238
|
+
};
|
|
239
|
+
for (const [name, { occurrence }] of [...chosen].sort(([a], [b]) => a.localeCompare(b)))
|
|
240
|
+
if (occurrence.direct)
|
|
241
|
+
walk(name);
|
|
242
|
+
for (const child of [...(edges.get(projectName) ?? [])].sort())
|
|
243
|
+
walk(child);
|
|
244
|
+
for (const name of [...chosen.keys()].sort())
|
|
245
|
+
walk(name);
|
|
246
|
+
const workspaceDirs = new Map(workspaces.map((ws) => [ws.dir, ws.key]));
|
|
247
|
+
const packages = order.map((name) => {
|
|
248
|
+
const { pkg, occurrence } = chosen.get(name);
|
|
249
|
+
const own = workspaceDirs.get(pkg.dir);
|
|
250
|
+
const set = new Set(members.get(name) ?? []);
|
|
251
|
+
if (own !== undefined)
|
|
252
|
+
set.add(own);
|
|
253
|
+
return {
|
|
254
|
+
...pkg,
|
|
255
|
+
// A workspace that another workspace depends on is still this repository's own code.
|
|
256
|
+
...(own !== undefined ? { layer: 'project' } : {}),
|
|
257
|
+
direct: occurrence.direct,
|
|
258
|
+
...(occurrence.via !== undefined ? { via: occurrence.via } : {}),
|
|
259
|
+
closure: closureOf(name),
|
|
260
|
+
members: set,
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
if (projectHarness)
|
|
264
|
+
packages.push({ ...projectHarness, direct: true, closure: closureOf(projectName), members: new Set(['']) });
|
|
265
|
+
for (const { pkg, key } of workspaceHarnesses) {
|
|
266
|
+
if (packages.some((p) => p.dir === pkg.dir))
|
|
267
|
+
continue;
|
|
268
|
+
packages.push({ ...pkg, direct: true, closure: closureOf(pkg.name), members: new Set([key]) });
|
|
269
|
+
}
|
|
270
|
+
const everywhere = new Set(['', ...workspaces.map((ws) => ws.key)]);
|
|
271
|
+
for (const added of options.added ?? []) {
|
|
272
|
+
packages.push({ ...added.pkg, layer: 'added', direct: true, closure: new Set(), scope: added.scope, added, members: everywhere });
|
|
273
|
+
if (added.entry)
|
|
274
|
+
packages.push({ ...added.entry, layer: 'added', direct: true, closure: new Set(), scope: added.scope, added, entry: true, members: everywhere });
|
|
275
|
+
}
|
|
276
|
+
// While a project moves from an earlier name of harnesspm, both copies may be installed. They ship the same harness, so
|
|
277
|
+
// the current one wins (spec 10.1).
|
|
278
|
+
if (packages.some((p) => p.name === PACKAGE_NAME)) {
|
|
279
|
+
for (let i = packages.length - 1; i >= 0; i--)
|
|
280
|
+
if (OLD_PACKAGE_NAMES.includes(packages[i].name) && packages[i].layer !== 'added')
|
|
281
|
+
packages.splice(i, 1);
|
|
282
|
+
}
|
|
283
|
+
// Plugin names must be unique (spec 2.9). An entry shares its harness's name.
|
|
284
|
+
const byPlugin = new Map();
|
|
285
|
+
for (const pkg of packages) {
|
|
286
|
+
if (pkg.entry)
|
|
287
|
+
continue;
|
|
288
|
+
const other = byPlugin.get(pkg.plugin);
|
|
289
|
+
if (other !== undefined) {
|
|
290
|
+
throw new HarnessError(`${other} and ${pkg.name} both use the plugin name "${pkg.plugin}". Exclude one of them in ${CONFIG_FILE}.`);
|
|
291
|
+
}
|
|
292
|
+
byPlugin.set(pkg.plugin, pkg.name);
|
|
293
|
+
}
|
|
294
|
+
for (const pkg of packages)
|
|
295
|
+
for (const problem of pkg.problems)
|
|
296
|
+
warnings.push(`${pkg.name}: ${problem}`);
|
|
297
|
+
// Overrides, lowest first: upstream packages, then downstream ones, then the project's harness, then harnesspm.json.
|
|
298
|
+
const overrides = [];
|
|
299
|
+
const add = (from, scope, ext, baseDir) => {
|
|
300
|
+
const by = from?.name ?? CONFIG_FILE;
|
|
301
|
+
const base = { by, ...(from ? { from } : {}), scope };
|
|
302
|
+
for (const target of ext.exclude.filter(isUnitId))
|
|
303
|
+
overrides.push({ kind: 'exclude', target, ...base, order: overrides.length });
|
|
304
|
+
for (const [target, file] of Object.entries(ext.replace))
|
|
305
|
+
overrides.push({ kind: 'replace', target, file: path.resolve(baseDir, file), ...base, order: overrides.length });
|
|
306
|
+
for (const [target, file] of Object.entries(ext.patch))
|
|
307
|
+
overrides.push({ kind: 'patch', target, file: path.resolve(baseDir, file), ...base, order: overrides.length });
|
|
308
|
+
};
|
|
309
|
+
for (const pkg of packages.filter((p) => p.layer !== 'added'))
|
|
310
|
+
add(pkg, pkg.closure, pkg.ext, pkg.harnessDir);
|
|
311
|
+
add(undefined, 'all', config, projectDir);
|
|
312
|
+
// Added harnesses sit above everything, so their changes reach every unit below them.
|
|
313
|
+
for (const pkg of packages.filter((p) => p.layer === 'added'))
|
|
314
|
+
add(pkg, 'all', pkg.ext, pkg.harnessDir);
|
|
315
|
+
const overrideFiles = new Set(overrides.flatMap((o) => (o.file !== undefined ? [o.file] : [])));
|
|
316
|
+
const inScope = (o, pkgName) => o.scope === 'all' || o.scope.has(pkgName);
|
|
317
|
+
const appliesIn = (o, key, owner) => o.scope === 'all' || o.from?.dir === projectDir || workspaceClosures.get(key)?.get(o.by)?.has(owner) === true;
|
|
318
|
+
const allRules = [];
|
|
319
|
+
const allSkills = [];
|
|
320
|
+
const allDocs = [];
|
|
321
|
+
const allScripts = [];
|
|
322
|
+
const allHooks = [];
|
|
323
|
+
const allMcp = [];
|
|
324
|
+
const allPermissions = [];
|
|
325
|
+
const allAgents = [];
|
|
326
|
+
// Docs of the project layer that a build writes: git ignores the folder, or it is not built yet (spec 2.7, rule 6).
|
|
327
|
+
const builtDocs = new Set();
|
|
328
|
+
for (const pkg of packages) {
|
|
329
|
+
const units = discoverUnits(pkg, overrideFiles);
|
|
330
|
+
for (const source of units.rules)
|
|
331
|
+
allRules.push({ source, pkg });
|
|
332
|
+
for (const skill of units.skills)
|
|
333
|
+
allSkills.push({ ...skill, pkg, original: skill.dir });
|
|
334
|
+
const built = pkg.layer === 'project' && pkg.ext.docs !== undefined && (!units.docs || (gitIgnoresDir(units.docs.dir) && !gitIgnoresDir(pkg.dir)));
|
|
335
|
+
if (built)
|
|
336
|
+
builtDocs.add(`${pkg.plugin}:docs`);
|
|
337
|
+
else if (units.docs)
|
|
338
|
+
allDocs.push({ ...units.docs, pkg });
|
|
339
|
+
const problems = [];
|
|
340
|
+
const exec = discoverExecUnits(pkg, problems);
|
|
341
|
+
for (const problem of problems)
|
|
342
|
+
warnings.push(`${pkg.name}: ${problem}`);
|
|
343
|
+
allScripts.push(...exec.scripts.map((u) => ({ ...u, pkg })));
|
|
344
|
+
allHooks.push(...exec.hooks.map((u) => ({ ...u, pkg })));
|
|
345
|
+
allMcp.push(...exec.mcp.map((u) => ({ ...u, pkg })));
|
|
346
|
+
if (exec.permissions)
|
|
347
|
+
allPermissions.push({ ...exec.permissions, pkg });
|
|
348
|
+
allAgents.push(...exec.agents.map((u) => ({ ...u, pkg })));
|
|
349
|
+
}
|
|
350
|
+
const knownIds = new Map();
|
|
351
|
+
for (const { source, pkg } of allRules)
|
|
352
|
+
knownIds.set(source.id, pkg.name);
|
|
353
|
+
for (const skill of allSkills)
|
|
354
|
+
knownIds.set(skill.id, skill.pkg.name);
|
|
355
|
+
for (const docs of allDocs)
|
|
356
|
+
knownIds.set(docs.id, docs.pkg.name);
|
|
357
|
+
for (const unit of [...allScripts, ...allHooks, ...allMcp, ...allPermissions, ...allAgents])
|
|
358
|
+
knownIds.set(unit.id, unit.pkg.name);
|
|
359
|
+
const addedNames = new Set(packages.filter((p) => p.layer === 'added').map((p) => p.name));
|
|
360
|
+
for (const o of overrides) {
|
|
361
|
+
const owner = knownIds.get(o.target);
|
|
362
|
+
// Without the team layers, an added harness's change to a team unit has nothing to point at yet.
|
|
363
|
+
if (owner === undefined && !team && addedNames.has(o.by))
|
|
364
|
+
continue;
|
|
365
|
+
// Built docs of the project layer are never a unit, built or not, so excluding them changes nothing.
|
|
366
|
+
if (owner === undefined && o.kind === 'exclude' && builtDocs.has(o.target))
|
|
367
|
+
continue;
|
|
368
|
+
if (owner === undefined)
|
|
369
|
+
warnings.push(`${o.by}: ${o.kind} names ${o.target}, which does not exist. It may have been renamed upstream.`);
|
|
370
|
+
else if (!inScope(o, owner))
|
|
371
|
+
warnings.push(`${o.by}: ${o.kind} names ${o.target}, which ${o.by} does not pass on`);
|
|
372
|
+
else if (o.kind === 'patch' && replaceable(o.target) !== 'rule')
|
|
373
|
+
warnings.push(`${o.by}: patch works on rules only: ${o.target}`);
|
|
374
|
+
else if (o.kind === 'replace' && replaceable(o.target) === undefined)
|
|
375
|
+
warnings.push(`${o.by}: replace works on rules and skills only: ${o.target}. Exclude it and add a unit of your own instead.`);
|
|
376
|
+
if (o.file !== undefined && !(o.kind === 'replace' ? replacementExists(o.target, o.file) : isFile(o.file))) {
|
|
377
|
+
const skillFolder = o.kind === 'replace' && replaceable(o.target) === 'skill';
|
|
378
|
+
warnings.push(`${o.by}: ${skillFolder ? 'replace needs a skill folder with a SKILL.md' : `${o.kind} file not found`}: ${relPosix(projectDir, o.file)}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const excludedUnits = [];
|
|
382
|
+
const excludedBy = (id, pkgName) => overrides.find((o) => o.kind === 'exclude' && o.target === id && inScope(o, pkgName));
|
|
383
|
+
// An entry's unit wins over a unit with the same ID in its own harness (spec 7.2).
|
|
384
|
+
const entryIds = new Set(allRules.filter((r) => r.pkg.entry).map((r) => r.source.id));
|
|
385
|
+
const rules = [];
|
|
386
|
+
for (const { source, pkg } of allRules) {
|
|
387
|
+
if (!pkg.entry && pkg.layer === 'added' && entryIds.has(source.id))
|
|
388
|
+
continue;
|
|
389
|
+
const exclusion = excludedBy(source.id, pkg.name);
|
|
390
|
+
if (exclusion) {
|
|
391
|
+
excludedUnits.push({ id: source.id, by: exclusion.by });
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const relevant = overrides.filter((o) => o.kind !== 'exclude' && o.target === source.id && inScope(o, pkg.name) && isFile(o.file));
|
|
395
|
+
const variants = new Map();
|
|
396
|
+
// Workspaces also inherit root rules. Apply only the overrides available in each workspace.
|
|
397
|
+
for (const key of pkg.members.has('') ? everywhere : pkg.members) {
|
|
398
|
+
const applicable = relevant.filter((o) => appliesIn(o, key, pkg.name));
|
|
399
|
+
const replace = applicable.findLast((o) => o.kind === 'replace');
|
|
400
|
+
const patches = applicable.filter((o) => {
|
|
401
|
+
if (o.kind !== 'patch')
|
|
402
|
+
return false;
|
|
403
|
+
if (!replace || o.order > replace.order)
|
|
404
|
+
return true;
|
|
405
|
+
warnings.push(`${o.by}: the patch of ${source.id} is dropped, because ${replace.by} replaces the rule`);
|
|
406
|
+
return false;
|
|
407
|
+
});
|
|
408
|
+
const signature = JSON.stringify([replace?.order, ...patches.map((o) => o.order)]);
|
|
409
|
+
const variant = variants.get(signature) ?? { members: new Set(), replace, patches };
|
|
410
|
+
variant.members.add(key);
|
|
411
|
+
variants.set(signature, variant);
|
|
412
|
+
}
|
|
413
|
+
for (const variant of variants.values()) {
|
|
414
|
+
rules.push(resolveRule(source, pkg, variant.replace, variant.patches, variant.members, variants.size > 1));
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function resolveRule(source, pkg, replace, patchOverrides, members, variant) {
|
|
418
|
+
const from = replace?.file ?? source.file;
|
|
419
|
+
let text = readText(from);
|
|
420
|
+
const patches = [];
|
|
421
|
+
for (const p of patchOverrides) {
|
|
422
|
+
const patchName = relPosix(projectDir, p.file);
|
|
423
|
+
const result = applyPatch(text, readText(p.file), patchName);
|
|
424
|
+
text = result.text;
|
|
425
|
+
warnings.push(...result.warnings);
|
|
426
|
+
patches.push({ patch: patchName, by: p.by, sections: result.touched });
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
...ruleMeta(text, source.name, from),
|
|
430
|
+
id: source.id,
|
|
431
|
+
name: source.name,
|
|
432
|
+
pkg,
|
|
433
|
+
original: source.file,
|
|
434
|
+
source: from,
|
|
435
|
+
text,
|
|
436
|
+
...(replace ? { replacedBy: replace } : {}),
|
|
437
|
+
patches,
|
|
438
|
+
members,
|
|
439
|
+
variant,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
// Skills: a later package wins on the same name, with a warning (spec 2.9). The highest `replace` gives the folder.
|
|
443
|
+
const skillsByName = new Map();
|
|
444
|
+
for (const found of allSkills) {
|
|
445
|
+
const exclusion = excludedBy(found.id, found.pkg.name);
|
|
446
|
+
if (exclusion) {
|
|
447
|
+
excludedUnits.push({ id: found.id, by: exclusion.by });
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const replacements = overrides.filter((o) => o.kind === 'replace' && o.target === found.id && inScope(o, found.pkg.name) && replacementExists(o.target, o.file));
|
|
451
|
+
const winners = new Map();
|
|
452
|
+
for (const key of found.pkg.members) {
|
|
453
|
+
const replace = replacements.findLast((o) => appliesIn(o, key, found.pkg.name));
|
|
454
|
+
const keys = winners.get(replace) ?? new Set();
|
|
455
|
+
keys.add(key);
|
|
456
|
+
winners.set(replace, keys);
|
|
457
|
+
}
|
|
458
|
+
let skill = found;
|
|
459
|
+
if (winners.size > 1) {
|
|
460
|
+
const details = [...winners].map(([replace, keys]) => `${replace ? `The replacement from ${replace.by}` : 'The original'} applies in ${describeMembers(keys)}.`);
|
|
461
|
+
const replacementConflict = ['Keeping the original skill because its consumers resolve different versions.', ...details].join(' ');
|
|
462
|
+
warnings.push(`${found.id}: ${replacementConflict}`);
|
|
463
|
+
skill = { ...found, replacementConflict };
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
const replace = winners.keys().next().value;
|
|
467
|
+
if (replace)
|
|
468
|
+
skill = { ...found, dir: replace.file, replacedBy: replace };
|
|
469
|
+
}
|
|
470
|
+
const previous = skillsByName.get(skill.name);
|
|
471
|
+
if (previous && !(skill.pkg.entry && previous.pkg.plugin === skill.pkg.plugin))
|
|
472
|
+
warnings.push(`skill "${skill.name}" comes from ${previous.pkg.name} and ${skill.pkg.name}; using ${skill.pkg.name}`);
|
|
473
|
+
skillsByName.set(skill.name, skill);
|
|
474
|
+
}
|
|
475
|
+
const docs = [];
|
|
476
|
+
for (const unit of allDocs) {
|
|
477
|
+
const exclusion = excludedBy(unit.id, unit.pkg.name);
|
|
478
|
+
if (exclusion)
|
|
479
|
+
excludedUnits.push({ id: unit.id, by: exclusion.by });
|
|
480
|
+
else
|
|
481
|
+
docs.push(unit);
|
|
482
|
+
}
|
|
483
|
+
const keep = (units) => units.filter((unit) => {
|
|
484
|
+
const exclusion = excludedBy(unit.id, unit.pkg.name);
|
|
485
|
+
if (exclusion)
|
|
486
|
+
excludedUnits.push({ id: unit.id, by: exclusion.by });
|
|
487
|
+
return !exclusion;
|
|
488
|
+
});
|
|
489
|
+
const scripts = keep(allScripts);
|
|
490
|
+
const hooks = keep(allHooks).filter((hook) => {
|
|
491
|
+
const ok = scripts.some((s) => s.pkg === hook.pkg && s.name === hook.run);
|
|
492
|
+
if (!ok)
|
|
493
|
+
warnings.push(`${hook.id} is dropped: its script ${hook.pkg.plugin}:${hook.run} is excluded or missing`);
|
|
494
|
+
return ok;
|
|
495
|
+
});
|
|
496
|
+
const agentsByName = new Map();
|
|
497
|
+
for (const agent of keep(allAgents)) {
|
|
498
|
+
const previous = agentsByName.get(agent.name);
|
|
499
|
+
if (previous && !(agent.pkg.entry && previous.pkg.plugin === agent.pkg.plugin))
|
|
500
|
+
warnings.push(`sub-agent "${agent.name}" comes from ${previous.pkg.name} and ${agent.pkg.name}; using ${agent.pkg.name}`);
|
|
501
|
+
agentsByName.set(agent.name, agent);
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
projectDir,
|
|
505
|
+
projectName,
|
|
506
|
+
workspaces,
|
|
507
|
+
config,
|
|
508
|
+
packages,
|
|
509
|
+
rules,
|
|
510
|
+
skills: [...skillsByName.values()],
|
|
511
|
+
docs,
|
|
512
|
+
scripts,
|
|
513
|
+
hooks,
|
|
514
|
+
mcp: keep(allMcp),
|
|
515
|
+
permissions: keep(allPermissions),
|
|
516
|
+
agents: [...agentsByName.values()],
|
|
517
|
+
overrides,
|
|
518
|
+
excludedUnits,
|
|
519
|
+
excludedPackages,
|
|
520
|
+
warnings,
|
|
521
|
+
};
|
|
522
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Resolution } from './resolve.ts';
|
|
2
|
+
export type Reviewed = {
|
|
3
|
+
ok: true;
|
|
4
|
+
branch: string;
|
|
5
|
+
} | {
|
|
6
|
+
ok: false;
|
|
7
|
+
reason: string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The repository is as reviewed when its trust inputs are the same as where HEAD meets the remote default branch,
|
|
11
|
+
* with no untracked file among them, and this sync computes the committed lock (`lock`), so the installed packages
|
|
12
|
+
* are the ones the lock records. Only local refs are read: a stale `origin/HEAD` means an older reviewed state.
|
|
13
|
+
* `reason` says what differs, so a person knows whether a review or an install is due.
|
|
14
|
+
*/
|
|
15
|
+
export declare function reviewedState(projectDir: string, team: Resolution, lock: string): Reviewed;
|
package/dist/reviewed.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Whether the repository is as a person reviewed it, so a model run may write its entries (spec 6.2, rule 4).
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { CONFIG_FILE } from './config.js';
|
|
5
|
+
import { LOCK_FILE, sameLock } from './lock.js';
|
|
6
|
+
import { readTextIfExists } from './util.js';
|
|
7
|
+
/**
|
|
8
|
+
* The repository is as reviewed when its trust inputs are the same as where HEAD meets the remote default branch,
|
|
9
|
+
* with no untracked file among them, and this sync computes the committed lock (`lock`), so the installed packages
|
|
10
|
+
* are the ones the lock records. Only local refs are read: a stale `origin/HEAD` means an older reviewed state.
|
|
11
|
+
* `reason` says what differs, so a person knows whether a review or an install is due.
|
|
12
|
+
*/
|
|
13
|
+
export function reviewedState(projectDir, team, lock) {
|
|
14
|
+
const git = (...args) => {
|
|
15
|
+
const result = spawnSync('git', args, { cwd: projectDir, encoding: 'utf8' });
|
|
16
|
+
return result.status === 0 ? result.stdout.trim() : undefined;
|
|
17
|
+
};
|
|
18
|
+
const branch = git('symbolic-ref', '--quiet', '--short', 'refs/remotes/origin/HEAD');
|
|
19
|
+
if (branch === undefined) {
|
|
20
|
+
if (git('rev-parse', '--git-dir') === undefined)
|
|
21
|
+
return { ok: false, reason: 'the project is not in a git repository' };
|
|
22
|
+
return { ok: false, reason: 'origin/HEAD is not set (`git remote set-head origin --auto` sets it)' };
|
|
23
|
+
}
|
|
24
|
+
const base = git('merge-base', 'HEAD', branch);
|
|
25
|
+
if (base === undefined)
|
|
26
|
+
return { ok: false, reason: `HEAD has no commit in common with ${branch}` };
|
|
27
|
+
const folders = ['', ...team.workspaces.map((ws) => `${ws.key}/`)].flatMap((dir) => [`${dir}harness`, `${dir}harnesspm/patches`]);
|
|
28
|
+
const inputs = [CONFIG_FILE, LOCK_FILE, ...folders];
|
|
29
|
+
const changed = git('diff', '--name-only', '--relative', '--no-renames', base, '--', ...inputs);
|
|
30
|
+
const untracked = git('ls-files', '--others', '--exclude-standard', '--', ...inputs);
|
|
31
|
+
if (changed === undefined || untracked === undefined)
|
|
32
|
+
return { ok: false, reason: `git could not compare the trust inputs with ${branch}` };
|
|
33
|
+
const files = [...changed.split('\n'), ...untracked.split('\n')].filter((file) => file !== '');
|
|
34
|
+
if (files.length > 0) {
|
|
35
|
+
const more = files.length > 1 ? ` and ${files.length - 1} more file${files.length > 2 ? 's' : ''}` : '';
|
|
36
|
+
return { ok: false, reason: `${files[0]}${more} differ${files.length > 1 ? '' : 's'} from ${branch}` };
|
|
37
|
+
}
|
|
38
|
+
if (!sameLock(readTextIfExists(path.join(projectDir, LOCK_FILE)), lock))
|
|
39
|
+
return { ok: false, reason: `the installed packages do not match ${LOCK_FILE}` };
|
|
40
|
+
return { ok: true, branch };
|
|
41
|
+
}
|
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Resolution } from './resolve.ts';
|
|
2
|
+
import { HarnessError } from './util.ts';
|
|
3
|
+
/** Thrown when the runner refuses a script. The CLI exits with code 2 (spec 10.4). */
|
|
4
|
+
export declare class Refused extends HarnessError {
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Where a script's dependencies come from. A project without an install runs scripts from fetched packages, which have
|
|
8
|
+
* no dependencies of their own; `install` is then the command that installs the project.
|
|
9
|
+
*/
|
|
10
|
+
export interface RunContext {
|
|
11
|
+
install?: string;
|
|
12
|
+
/** Where the script runs. Default: the caller's folder. */
|
|
13
|
+
cwd?: string;
|
|
14
|
+
/** stdin for the script, already read, such as a hook's JSON. Default: harnesspm's own stdin. */
|
|
15
|
+
input?: Buffer;
|
|
16
|
+
}
|
|
17
|
+
export declare function runScript(res: Resolution, ref: string, args: string[], agent: string | undefined, context?: RunContext): Promise<number>;
|
|
18
|
+
export declare function runMcp(res: Resolution, ref: string, agent: string | undefined): number;
|