@harnesspm/cli 0.0.0-snapshot.412a718
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 +259 -0
- package/LICENSE +21 -0
- package/README.md +70 -0
- 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 +181 -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 +73 -0
- package/schema/harnesspm.schema.json +58 -0
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// harnesspm fetch: the harness packages in the lock, without an install (spec 4.2, 13.6).
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import * as fs from 'node:fs';
|
|
5
|
+
import * as os from 'node:os';
|
|
6
|
+
import * as path from 'node:path';
|
|
7
|
+
import { LOCK_FILE, packageManagerLock, readLock } from './lock.js';
|
|
8
|
+
import { addExcludes, computePlan, DOCS_DIR, DOCS_EXCLUDE, DOCS_PROBE, docsIgnoreAdvice, RGIGNORE_EXCLUDE, STATE_DIR, writeRgignore } from './outputs.js';
|
|
9
|
+
import { extractBlock } from './render.js';
|
|
10
|
+
import { resolve, resolvePackageDir } from './resolve.js';
|
|
11
|
+
import { HarnessError, NPX_ANY, ensureDir, gitKeepsOut, isInside, isRecord, linkIfChanged, readJsonIfExists, readTextIfExists, writeIfChanged } from './util.js';
|
|
12
|
+
function sri(file, algorithm) {
|
|
13
|
+
return `${algorithm}-${createHash(algorithm).update(fs.readFileSync(file)).digest('base64')}`;
|
|
14
|
+
}
|
|
15
|
+
function installedVersion(dir) {
|
|
16
|
+
const pkg = readJsonIfExists(path.join(dir, 'package.json'));
|
|
17
|
+
return isRecord(pkg) && typeof pkg['version'] === 'string' ? pkg['version'] : undefined;
|
|
18
|
+
}
|
|
19
|
+
/** Downloads one package with npm, checks its integrity and unpacks it. Never runs its code. */
|
|
20
|
+
function download(projectDir, name, spec, integrity, target) {
|
|
21
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'harnesspm-fetch-'));
|
|
22
|
+
try {
|
|
23
|
+
const packSpec = spec.startsWith('file:') ? path.resolve(projectDir, spec.slice('file:'.length)) : spec;
|
|
24
|
+
const packed = spawnSync('npm', ['pack', packSpec, '--silent', '--ignore-scripts', '--pack-destination', tmp], { cwd: projectDir, encoding: 'utf8' });
|
|
25
|
+
if (packed.status !== 0)
|
|
26
|
+
throw new HarnessError(`Cannot download ${name} (${spec}): ${packed.stderr.trim()}`);
|
|
27
|
+
const tarball = path.join(tmp, packed.stdout.trim().split('\n').pop().trim());
|
|
28
|
+
if (integrity !== undefined) {
|
|
29
|
+
const algorithm = integrity.split('-')[0];
|
|
30
|
+
const actual = sri(tarball, algorithm);
|
|
31
|
+
if (actual !== integrity)
|
|
32
|
+
throw new HarnessError(`${name}: the downloaded tarball does not match the integrity in the lock`);
|
|
33
|
+
}
|
|
34
|
+
const unpacked = path.join(tmp, 'unpacked');
|
|
35
|
+
ensureDir(unpacked);
|
|
36
|
+
const tar = spawnSync('tar', ['-xzf', tarball, '-C', unpacked], { encoding: 'utf8' });
|
|
37
|
+
if (tar.status !== 0)
|
|
38
|
+
throw new HarnessError(`Cannot unpack ${name}: ${tar.stderr.trim()}`);
|
|
39
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
40
|
+
ensureDir(path.dirname(target));
|
|
41
|
+
fs.renameSync(path.join(unpacked, 'package'), target);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Where `fetch` puts a package. */
|
|
48
|
+
export const fetchedDir = (projectDir, name) => path.join(projectDir, STATE_DIR, 'fetched', name);
|
|
49
|
+
/**
|
|
50
|
+
* Where each harness package in the lock is on this machine: installed in the version the lock names, else fetched in
|
|
51
|
+
* that version. `missing` names the packages that are neither. Undefined when the project has no lock.
|
|
52
|
+
*/
|
|
53
|
+
export function locateLocked(projectDir) {
|
|
54
|
+
const lock = readLock(projectDir);
|
|
55
|
+
if (!lock)
|
|
56
|
+
return undefined;
|
|
57
|
+
const located = new Map();
|
|
58
|
+
const missing = [];
|
|
59
|
+
for (const [name, entry] of Object.entries(lock.packages)) {
|
|
60
|
+
if (entry.project || entry.version === undefined)
|
|
61
|
+
continue;
|
|
62
|
+
const installed = resolvePackageDir(name, projectDir);
|
|
63
|
+
if (installed && installedVersion(installed) === entry.version) {
|
|
64
|
+
located.set(name, installed);
|
|
65
|
+
}
|
|
66
|
+
else if (installedVersion(fetchedDir(projectDir, name)) === entry.version)
|
|
67
|
+
located.set(name, fetchedDir(projectDir, name));
|
|
68
|
+
else
|
|
69
|
+
missing.push(name);
|
|
70
|
+
}
|
|
71
|
+
return { located, missing };
|
|
72
|
+
}
|
|
73
|
+
export function fetchLocked(projectDir) {
|
|
74
|
+
const lock = readLock(projectDir);
|
|
75
|
+
if (!lock)
|
|
76
|
+
throw new HarnessError(`No harnesspm.lock.json here. Run \`${NPX_ANY} sync\` where the dependencies are installed, and commit the lock.`);
|
|
77
|
+
const report = { fetched: [], reused: [], warnings: [] };
|
|
78
|
+
const { located } = locateLocked(projectDir);
|
|
79
|
+
report.reused.push(...located.keys());
|
|
80
|
+
// A lock without integrity, such as one that harnesspm 0.3 wrote with pnpm, can still be checked against the package manager's lockfile.
|
|
81
|
+
const lookup = packageManagerLock(projectDir);
|
|
82
|
+
for (const [name, entry] of Object.entries(lock.packages)) {
|
|
83
|
+
if (entry.project || entry.version === undefined || located.has(name))
|
|
84
|
+
continue;
|
|
85
|
+
const target = fetchedDir(projectDir, name);
|
|
86
|
+
const found = entry.integrity === undefined ? lookup(name, entry.version) : {};
|
|
87
|
+
const spec = entry.resolved ?? found.resolved ?? `${name}@${entry.version}`;
|
|
88
|
+
const integrity = entry.integrity ?? found.integrity;
|
|
89
|
+
if (integrity === undefined)
|
|
90
|
+
report.warnings.push(`${name} ${entry.version}: no integrity in ${LOCK_FILE} or the package manager's lockfile, so the download is not checked`);
|
|
91
|
+
download(projectDir, name, spec, integrity, target);
|
|
92
|
+
report.fetched.push(`${name} ${entry.version}`);
|
|
93
|
+
located.set(name, target);
|
|
94
|
+
}
|
|
95
|
+
// Stable paths and generated files (docs indexes, patched rules), so the committed pointers work.
|
|
96
|
+
const team = resolve(projectDir, { locate: (name) => located.get(name) });
|
|
97
|
+
report.warnings.push(...team.warnings);
|
|
98
|
+
const plan = computePlan(team, team);
|
|
99
|
+
const agents = readTextIfExists(path.join(projectDir, 'AGENTS.md'));
|
|
100
|
+
if (extractBlock(agents) !== extractBlock(plan.committed.agentsMd))
|
|
101
|
+
report.warnings.push(`AGENTS.md does not match the packages in the lock. Run \`${NPX_ANY} sync\` where the dependencies are installed.`);
|
|
102
|
+
// Only files under node_modules/.harnesspm, the docs copies in .harnesspm/docs, and the .rgignore that lets search
|
|
103
|
+
// tools into them: a job without an install needs no agent settings.
|
|
104
|
+
const state = path.join(projectDir, STATE_DIR);
|
|
105
|
+
const docsRoot = path.join(projectDir, DOCS_DIR);
|
|
106
|
+
// The copies and the .rgignore only once git ignores them, so that an agent's `git add -A` does not take them. A
|
|
107
|
+
// sandbox may keep .git read-only: then only a .gitignore that lists .harnesspm/ lets the copies be written.
|
|
108
|
+
const excluded = (entries) => {
|
|
109
|
+
try {
|
|
110
|
+
addExcludes(projectDir, entries);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const wanted = plan.teamExcludes?.includes(DOCS_EXCLUDE) === true;
|
|
118
|
+
const listed = wanted && excluded([DOCS_EXCLUDE]);
|
|
119
|
+
const copies = gitKeepsOut(projectDir, DOCS_PROBE);
|
|
120
|
+
if (!copies && wanted) {
|
|
121
|
+
const why = listed ? '' : ', and .git/info/exclude cannot be written';
|
|
122
|
+
report.warnings.push(`The docs copies are not written: git does not ignore .harnesspm/${why}. ${docsIgnoreAdvice(projectDir)}`);
|
|
123
|
+
}
|
|
124
|
+
const ours = (file) => isInside(state, file) || (copies && isInside(docsRoot, file));
|
|
125
|
+
for (const root of plan.generatedRoots)
|
|
126
|
+
if (ours(root))
|
|
127
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
128
|
+
for (const [file, content] of plan.generated)
|
|
129
|
+
if (ours(file))
|
|
130
|
+
writeIfChanged(file, content);
|
|
131
|
+
for (const [link, target] of plan.links)
|
|
132
|
+
if (isInside(path.join(state, 'pkgs'), link))
|
|
133
|
+
linkIfChanged(link, target);
|
|
134
|
+
if (copies && (typeof plan.rgignore !== 'string' || excluded([RGIGNORE_EXCLUDE]))) {
|
|
135
|
+
try {
|
|
136
|
+
writeRgignore(projectDir, plan);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
report.warnings.push(`.rgignore not written, so search tools skip the docs copies: ${error instanceof Error ? error.message : String(error)}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (copies)
|
|
143
|
+
report.warnings.push('.rgignore not written, so search tools skip the docs copies: .git/info/exclude cannot be written.');
|
|
144
|
+
return report;
|
|
145
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Parsed {
|
|
2
|
+
data: Record<string, unknown>;
|
|
3
|
+
body: string;
|
|
4
|
+
hasFrontmatter: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** Splits a Markdown file into its YAML frontmatter and its body. */
|
|
7
|
+
export declare function parseFrontmatter(text: string, file?: string): Parsed;
|
|
8
|
+
/**
|
|
9
|
+
* For a file that harnesspm only lists, such as a docs page: invalid frontmatter gives no data and an `error`,
|
|
10
|
+
* so one bad page in a dependency cannot fail a sync.
|
|
11
|
+
*/
|
|
12
|
+
export declare function tryFrontmatter(text: string, file?: string): Parsed & {
|
|
13
|
+
error?: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function withFrontmatter(data: Record<string, unknown>, body: string): string;
|
|
16
|
+
export declare function stringList(value: unknown): string[] | undefined;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { parse, stringify } from 'yaml';
|
|
2
|
+
import { HarnessError, isRecord } from './util.js';
|
|
3
|
+
const FRONTMATTER = /^---\r?\n(?:([\s\S]*?)\r?\n)?---[ \t]*(?:\r?\n|$)/;
|
|
4
|
+
/** Splits a Markdown file into its YAML frontmatter and its body. */
|
|
5
|
+
export function parseFrontmatter(text, file = 'file') {
|
|
6
|
+
const match = FRONTMATTER.exec(text);
|
|
7
|
+
if (!match)
|
|
8
|
+
return { data: {}, body: text, hasFrontmatter: false };
|
|
9
|
+
let data;
|
|
10
|
+
try {
|
|
11
|
+
data = match[1] ? parse(match[1]) : {};
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
throw new HarnessError(`Invalid frontmatter in ${file}: ${error.message}`);
|
|
15
|
+
}
|
|
16
|
+
if (data === null || data === undefined)
|
|
17
|
+
data = {};
|
|
18
|
+
if (!isRecord(data))
|
|
19
|
+
throw new HarnessError(`Frontmatter in ${file} must be a mapping`);
|
|
20
|
+
return { data, body: text.slice(match[0].length), hasFrontmatter: true };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* For a file that harnesspm only lists, such as a docs page: invalid frontmatter gives no data and an `error`,
|
|
24
|
+
* so one bad page in a dependency cannot fail a sync.
|
|
25
|
+
*/
|
|
26
|
+
export function tryFrontmatter(text, file = 'file') {
|
|
27
|
+
try {
|
|
28
|
+
return parseFrontmatter(text, file);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// The first line says what is wrong and where. The rest is a copy of the YAML.
|
|
32
|
+
const message = error.message.split('\n')[0].replace(/:$/, '');
|
|
33
|
+
return { data: {}, body: text.replace(FRONTMATTER, ''), hasFrontmatter: true, error: message };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function withFrontmatter(data, body) {
|
|
37
|
+
if (Object.keys(data).length === 0)
|
|
38
|
+
return body;
|
|
39
|
+
return `---\n${stringify(data).trimEnd()}\n---\n\n${body.replace(/^\n+/, '')}`;
|
|
40
|
+
}
|
|
41
|
+
export function stringList(value) {
|
|
42
|
+
if (value === undefined)
|
|
43
|
+
return undefined;
|
|
44
|
+
if (typeof value === 'string')
|
|
45
|
+
return [value];
|
|
46
|
+
if (Array.isArray(value) && value.every((v) => typeof v === 'string'))
|
|
47
|
+
return value;
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
package/dist/homes.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function claudeHome(): string;
|
|
2
|
+
/** Claude Code keeps MCP servers of user and local scope here. */
|
|
3
|
+
export declare function claudeJsonFile(): string;
|
|
4
|
+
export declare function codexHome(): string;
|
|
5
|
+
export declare function agentsHome(): string;
|
|
6
|
+
/** harnesspm's own folder for working copies, caches and state. */
|
|
7
|
+
export declare function harnesspmHome(): string;
|
package/dist/homes.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Where each agent keeps its user-level files. Tests and people can move them with environment variables.
|
|
2
|
+
import * as os from 'node:os';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
export function claudeHome() {
|
|
5
|
+
return process.env['CLAUDE_CONFIG_DIR'] ?? path.join(os.homedir(), '.claude');
|
|
6
|
+
}
|
|
7
|
+
/** Claude Code keeps MCP servers of user and local scope here. */
|
|
8
|
+
export function claudeJsonFile() {
|
|
9
|
+
const dir = process.env['CLAUDE_CONFIG_DIR'];
|
|
10
|
+
return dir !== undefined ? path.join(dir, '.claude.json') : path.join(os.homedir(), '.claude.json');
|
|
11
|
+
}
|
|
12
|
+
export function codexHome() {
|
|
13
|
+
return process.env['CODEX_HOME'] ?? path.join(os.homedir(), '.codex');
|
|
14
|
+
}
|
|
15
|
+
export function agentsHome() {
|
|
16
|
+
return path.join(os.homedir(), '.agents');
|
|
17
|
+
}
|
|
18
|
+
/** harnesspm's own folder for working copies, caches and state. */
|
|
19
|
+
export function harnesspmHome() {
|
|
20
|
+
return process.env['HARNESSPM_HOME'] ?? path.join(os.homedir(), '.harnesspm');
|
|
21
|
+
}
|
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Resolution } from './resolve.ts';
|
|
2
|
+
import type { Send } from './telemetry.ts';
|
|
3
|
+
export declare const LOCK_FILE = "harnesspm.lock.json";
|
|
4
|
+
export interface LockPackage {
|
|
5
|
+
/** Left out for the project's own harness: a release would change it, and unit hashes already show every change. */
|
|
6
|
+
version?: string;
|
|
7
|
+
plugin: string;
|
|
8
|
+
project?: true;
|
|
9
|
+
resolved?: string;
|
|
10
|
+
integrity?: string;
|
|
11
|
+
approved?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface Lock {
|
|
14
|
+
lockfileVersion: 1;
|
|
15
|
+
packages: Record<string, LockPackage>;
|
|
16
|
+
units: Record<string, {
|
|
17
|
+
kind: string;
|
|
18
|
+
hash: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** Rule ID → patch file → section heading → hash of the section the patch changes. */
|
|
21
|
+
patches: Record<string, Record<string, Record<string, string>>>;
|
|
22
|
+
/** The telemetry handler that a person's sync recorded (spec 14.5). */
|
|
23
|
+
telemetry?: {
|
|
24
|
+
send: Send;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** Where a package's tarball comes from, as the package manager's lockfile records it. */
|
|
28
|
+
export interface Origin {
|
|
29
|
+
resolved?: string;
|
|
30
|
+
integrity?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Looks up a package's tarball URL and integrity in the project's package-manager lockfiles: package-lock.json,
|
|
34
|
+
* pnpm-lock.yaml or Yarn 1's yarn.lock (spec 6.3). Each file is read once, when it is first needed.
|
|
35
|
+
*/
|
|
36
|
+
export declare function packageManagerLock(projectDir: string): (name: string, version: string) => Origin;
|
|
37
|
+
/** `telemetry` is the handler to record: harnesspm.json's for a person's sync, the previous one for a model run (spec 14.5). */
|
|
38
|
+
export declare function buildLock(res: Resolution, previous: Lock | undefined, telemetry?: Send): Lock;
|
|
39
|
+
export declare function serializeLock(lock: Lock): string;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the lock text on disk records the same as `expected`. A lock that harnesspm 0.3 wrote, with the
|
|
42
|
+
* project's own version, still counts as up to date; the next sync drops the version.
|
|
43
|
+
*/
|
|
44
|
+
export declare function sameLock(text: string | undefined, expected: string): boolean;
|
|
45
|
+
export declare function readLock(projectDir: string): Lock | undefined;
|
|
46
|
+
/** Warnings for upstream changes to sections that a patch replaces or removes (spec 3.4). */
|
|
47
|
+
export declare function patchDrift(previous: Lock | undefined, next: Lock): string[];
|
package/dist/lock.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// harnesspm.lock.json (spec 6.3). The lock holds no per-machine state.
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { rulesById } from './resolve.js';
|
|
6
|
+
import { gitKeeps, hash, isFile, isRecord, readJsonIfExists, readText, readTextIfExists, relPosix } from './util.js';
|
|
7
|
+
export const LOCK_FILE = 'harnesspm.lock.json';
|
|
8
|
+
function hashDir(dir, includes = () => true) {
|
|
9
|
+
const parts = [];
|
|
10
|
+
const walk = (current) => {
|
|
11
|
+
for (const entry of fs.readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
12
|
+
const full = path.join(current, entry.name);
|
|
13
|
+
const rel = relPosix(dir, full);
|
|
14
|
+
if (entry.isDirectory())
|
|
15
|
+
walk(full);
|
|
16
|
+
else if (entry.isFile() && includes(rel))
|
|
17
|
+
parts.push(`${rel}\n${readText(full)}`);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
walk(dir);
|
|
21
|
+
return hash(parts.join('\n\0\n'));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A docs unit is its `llms.txt` and its `.md` and `.mdx` pages that git does not ignore (spec 2.7). A docs folder
|
|
25
|
+
* can be a package's source folder, with pages next to the code, so a change to any other file leaves the docs
|
|
26
|
+
* hash as it is.
|
|
27
|
+
*/
|
|
28
|
+
function docsHash(dir) {
|
|
29
|
+
const keeps = gitKeeps(dir);
|
|
30
|
+
return hashDir(dir, (rel) => (rel === 'llms.txt' || /\.mdx?$/.test(rel)) && keeps(rel));
|
|
31
|
+
}
|
|
32
|
+
const origin = (resolved, integrity) => ({
|
|
33
|
+
...(typeof resolved === 'string' && resolved !== '' ? { resolved } : {}),
|
|
34
|
+
...(typeof integrity === 'string' && integrity !== '' ? { integrity } : {}),
|
|
35
|
+
});
|
|
36
|
+
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
37
|
+
/** package-lock.json (lockfile 2 and 3): `packages["…node_modules/<name>"]`. */
|
|
38
|
+
function fromPackageLock(text, name, version) {
|
|
39
|
+
const lock = JSON.parse(text);
|
|
40
|
+
if (!isRecord(lock) || !isRecord(lock['packages']))
|
|
41
|
+
return undefined;
|
|
42
|
+
for (const [key, entry] of Object.entries(lock['packages'])) {
|
|
43
|
+
if (!isRecord(entry) || entry['version'] !== version || (key !== `node_modules/${name}` && !key.endsWith(`/node_modules/${name}`)))
|
|
44
|
+
continue;
|
|
45
|
+
return origin(entry['resolved'], entry['integrity']);
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* pnpm-lock.yaml (lockfile 6 and 9): `packages["<name>@<version>"].resolution`, with `integrity` and, outside the
|
|
51
|
+
* registry, `tarball`. Only the entries of `name` are parsed, because a large lockfile takes long to parse whole.
|
|
52
|
+
*/
|
|
53
|
+
function fromPnpmLock(text, name, version) {
|
|
54
|
+
const entries = new RegExp(`^ {2}['"]?/?${escapeRegExp(name)}@([^\n]*?)['"]?:\n((?: {4}[^\n]*\n?)*)`, 'gm');
|
|
55
|
+
for (const [, ref, body] of text.matchAll(entries)) {
|
|
56
|
+
const entry = parse(body.replace(/^ {4}/gm, ''));
|
|
57
|
+
if (!isRecord(entry) || !isRecord(entry['resolution']))
|
|
58
|
+
continue;
|
|
59
|
+
// A peer suffix such as `(react@18.3.1)` follows the version. A `file:` entry keeps its version in `version`.
|
|
60
|
+
const entryVersion = typeof entry['version'] === 'string' ? entry['version'] : ref.replace(/\(.*$/, '');
|
|
61
|
+
if (entryVersion !== version)
|
|
62
|
+
continue;
|
|
63
|
+
return origin(entry['resolution']['tarball'], entry['resolution']['integrity']);
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
/** Yarn 1 yarn.lock: blocks headed by one or more `"<name>@<range>"`, with `version`, `resolved` and `integrity`. */
|
|
68
|
+
function fromYarnLock(text, name, version) {
|
|
69
|
+
if (!text.includes('# yarn lockfile v1'))
|
|
70
|
+
return undefined;
|
|
71
|
+
for (const block of text.split(/\n\s*\n/)) {
|
|
72
|
+
const [header, ...fields] = block.split('\n');
|
|
73
|
+
if (header === undefined || !header.endsWith(':') || /^[\s#]/.test(header))
|
|
74
|
+
continue;
|
|
75
|
+
const names = header.slice(0, -1).split(/,\s*/).map((spec) => spec.replace(/^"|"$/g, '')).map((spec) => spec.slice(0, spec.lastIndexOf('@')));
|
|
76
|
+
if (!names.includes(name))
|
|
77
|
+
continue;
|
|
78
|
+
const field = (key) => {
|
|
79
|
+
for (const line of fields) {
|
|
80
|
+
const match = new RegExp(`^ {2}${key} "?([^"]*)"?$`).exec(line);
|
|
81
|
+
if (match)
|
|
82
|
+
return match[1];
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
if (field('version') !== version)
|
|
87
|
+
continue;
|
|
88
|
+
return origin(field('resolved')?.replace(/#.*$/, ''), field('integrity'));
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Looks up a package's tarball URL and integrity in the project's package-manager lockfiles: package-lock.json,
|
|
94
|
+
* pnpm-lock.yaml or Yarn 1's yarn.lock (spec 6.3). Each file is read once, when it is first needed.
|
|
95
|
+
*/
|
|
96
|
+
export function packageManagerLock(projectDir) {
|
|
97
|
+
const readers = [
|
|
98
|
+
['package-lock.json', fromPackageLock],
|
|
99
|
+
['pnpm-lock.yaml', fromPnpmLock],
|
|
100
|
+
['yarn.lock', fromYarnLock],
|
|
101
|
+
];
|
|
102
|
+
const texts = new Map();
|
|
103
|
+
return (name, version) => {
|
|
104
|
+
for (const [file, read] of readers) {
|
|
105
|
+
if (!texts.has(file))
|
|
106
|
+
texts.set(file, readTextIfExists(path.join(projectDir, file)));
|
|
107
|
+
const text = texts.get(file);
|
|
108
|
+
if (text === undefined)
|
|
109
|
+
continue;
|
|
110
|
+
try {
|
|
111
|
+
const found = read(text, name, version);
|
|
112
|
+
if (found !== undefined)
|
|
113
|
+
return found;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// A lockfile that does not parse gives nothing; fetch then says the download is not checked.
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {};
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Keep a single hash per rule ID, recording which workspaces receive each text when they differ. */
|
|
123
|
+
function ruleHash(variants) {
|
|
124
|
+
const members = new Map();
|
|
125
|
+
for (const rule of variants) {
|
|
126
|
+
const digest = hash(rule.text);
|
|
127
|
+
for (const key of rule.members)
|
|
128
|
+
members.set(key, digest);
|
|
129
|
+
}
|
|
130
|
+
return workspaceHash(members);
|
|
131
|
+
}
|
|
132
|
+
/** Preserve old hashes when all workspaces have identical content. */
|
|
133
|
+
function workspaceHash(members) {
|
|
134
|
+
const hashes = new Set(members.values());
|
|
135
|
+
return hashes.size === 1 ? hashes.values().next().value : hash(JSON.stringify(sortKeys(Object.fromEntries(members))));
|
|
136
|
+
}
|
|
137
|
+
function patchHashes(variants) {
|
|
138
|
+
const sections = new Map();
|
|
139
|
+
for (const rule of variants) {
|
|
140
|
+
for (const applied of rule.patches) {
|
|
141
|
+
if (applied.sections.length === 0)
|
|
142
|
+
continue;
|
|
143
|
+
const headings = sections.get(applied.patch) ?? new Map();
|
|
144
|
+
for (const section of applied.sections) {
|
|
145
|
+
const members = headings.get(section.heading) ?? new Map();
|
|
146
|
+
for (const key of rule.members)
|
|
147
|
+
members.set(key, section.hash);
|
|
148
|
+
headings.set(section.heading, members);
|
|
149
|
+
}
|
|
150
|
+
sections.set(applied.patch, headings);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return Object.fromEntries([...sections].map(([patch, headings]) => [
|
|
154
|
+
patch, Object.fromEntries([...headings].map(([heading, members]) => [heading, workspaceHash(members)])),
|
|
155
|
+
]));
|
|
156
|
+
}
|
|
157
|
+
/** `telemetry` is the handler to record: harnesspm.json's for a person's sync, the previous one for a model run (spec 14.5). */
|
|
158
|
+
export function buildLock(res, previous, telemetry) {
|
|
159
|
+
const packages = {};
|
|
160
|
+
const lookup = packageManagerLock(res.projectDir);
|
|
161
|
+
for (const pkg of res.packages) {
|
|
162
|
+
if (pkg.layer === 'project') {
|
|
163
|
+
packages[pkg.name] = { plugin: pkg.plugin, project: true };
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const entry = { version: pkg.version, plugin: pkg.plugin, ...lookup(pkg.name, pkg.version) };
|
|
167
|
+
if (res.config.trust[pkg.name] === 'pin') {
|
|
168
|
+
const approved = previous?.packages[pkg.name]?.approved;
|
|
169
|
+
entry.approved = approved ?? pkg.version;
|
|
170
|
+
}
|
|
171
|
+
packages[pkg.name] = entry;
|
|
172
|
+
}
|
|
173
|
+
const units = {};
|
|
174
|
+
const patches = {};
|
|
175
|
+
for (const [id, variants] of rulesById(res.rules)) {
|
|
176
|
+
units[id] = { kind: 'rules', hash: ruleHash(variants) };
|
|
177
|
+
const sections = patchHashes(variants);
|
|
178
|
+
if (Object.keys(sections).length > 0)
|
|
179
|
+
patches[id] = sections;
|
|
180
|
+
}
|
|
181
|
+
for (const skill of res.skills)
|
|
182
|
+
units[skill.id] = { kind: 'skills', hash: hashDir(skill.dir) };
|
|
183
|
+
for (const docs of res.docs)
|
|
184
|
+
units[docs.id] = { kind: 'docs', hash: docsHash(docs.dir) };
|
|
185
|
+
for (const script of res.scripts)
|
|
186
|
+
units[script.id] = { kind: 'scripts', hash: isFile(script.file) ? hash(readText(script.file)) : 'missing' };
|
|
187
|
+
for (const hook of res.hooks)
|
|
188
|
+
units[hook.id] = { kind: 'hooks', hash: hash(JSON.stringify([hook.event, hook.matchers, hook.run, hook.timeout ?? null])) };
|
|
189
|
+
for (const server of res.mcp)
|
|
190
|
+
units[server.id] = { kind: 'mcp', hash: hash(JSON.stringify(server.config)) };
|
|
191
|
+
for (const perms of res.permissions)
|
|
192
|
+
units[perms.id] = { kind: 'permissions', hash: hash(JSON.stringify([perms.deny, perms.ask])) };
|
|
193
|
+
for (const agent of res.agents)
|
|
194
|
+
units[agent.id] = { kind: 'agents', hash: hash(readText(agent.file)) };
|
|
195
|
+
return { lockfileVersion: 1, packages: sortKeys(packages), units: sortKeys(units), patches: sortKeys(patches), ...(telemetry !== undefined ? { telemetry: { send: telemetry } } : {}) };
|
|
196
|
+
}
|
|
197
|
+
function sortKeys(record) {
|
|
198
|
+
return Object.fromEntries(Object.entries(record).sort(([a], [b]) => a.localeCompare(b)));
|
|
199
|
+
}
|
|
200
|
+
export function serializeLock(lock) {
|
|
201
|
+
return `${JSON.stringify(lock, null, 2)}\n`;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Whether the lock text on disk records the same as `expected`. A lock that harnesspm 0.3 wrote, with the
|
|
205
|
+
* project's own version, still counts as up to date; the next sync drops the version.
|
|
206
|
+
*/
|
|
207
|
+
export function sameLock(text, expected) {
|
|
208
|
+
if (text === expected)
|
|
209
|
+
return true;
|
|
210
|
+
try {
|
|
211
|
+
const lock = JSON.parse(text ?? '');
|
|
212
|
+
if (!isRecord(lock) || !isRecord(lock['packages']) || `${JSON.stringify(lock, null, 2)}\n` !== text)
|
|
213
|
+
return false;
|
|
214
|
+
for (const entry of Object.values(lock['packages']))
|
|
215
|
+
if (isRecord(entry) && entry['project'] === true)
|
|
216
|
+
delete entry['version'];
|
|
217
|
+
return `${JSON.stringify(lock, null, 2)}\n` === expected;
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
export function readLock(projectDir) {
|
|
224
|
+
const raw = readJsonIfExists(path.join(projectDir, LOCK_FILE));
|
|
225
|
+
if (!isRecord(raw) || raw['lockfileVersion'] !== 1)
|
|
226
|
+
return undefined;
|
|
227
|
+
return raw;
|
|
228
|
+
}
|
|
229
|
+
/** Warnings for upstream changes to sections that a patch replaces or removes (spec 3.4). */
|
|
230
|
+
export function patchDrift(previous, next) {
|
|
231
|
+
if (!previous)
|
|
232
|
+
return [];
|
|
233
|
+
const warnings = [];
|
|
234
|
+
for (const [ruleId, byPatch] of Object.entries(next.patches)) {
|
|
235
|
+
for (const [patch, sections] of Object.entries(byPatch)) {
|
|
236
|
+
for (const [heading, sectionHash] of Object.entries(sections)) {
|
|
237
|
+
const before = previous.patches[ruleId]?.[patch]?.[heading];
|
|
238
|
+
if (before !== undefined && before !== sectionHash) {
|
|
239
|
+
warnings.push(`${ruleId}: upstream changed the section "${heading}" that ${patch} changes. Check that the patch is still right.`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return warnings;
|
|
245
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export declare const NS = "dev.harnesspm";
|
|
2
|
+
export declare const PLUGIN_SCHEMA = "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json";
|
|
3
|
+
export type Layer = 'dependencies' | 'project' | 'added';
|
|
4
|
+
export interface ScriptDef {
|
|
5
|
+
file: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
/** The arguments and options the script takes, for `help <tool>` (spec 5.1). */
|
|
8
|
+
usage?: string;
|
|
9
|
+
/** Exit codes that are answers, not failures, such as 1 for "found problems" (spec 5.1, 14.2). */
|
|
10
|
+
answers?: number[];
|
|
11
|
+
allow?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface Extension {
|
|
14
|
+
extends: string[];
|
|
15
|
+
exclude: string[];
|
|
16
|
+
replace: Record<string, string>;
|
|
17
|
+
patch: Record<string, string>;
|
|
18
|
+
docs?: string;
|
|
19
|
+
scripts: Record<string, ScriptDef>;
|
|
20
|
+
projects: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
export interface HarnessPackage {
|
|
23
|
+
/** npm package name, or the project's package name for its own harness. */
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
plugin: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
repository?: string;
|
|
29
|
+
/** Package root, as a real path. */
|
|
30
|
+
dir: string;
|
|
31
|
+
harnessDir: string;
|
|
32
|
+
ext: Extension;
|
|
33
|
+
layer: Layer;
|
|
34
|
+
/** Problems that `harnesspm check` reports. `sync` prints them as warnings. */
|
|
35
|
+
problems: string[];
|
|
36
|
+
}
|
|
37
|
+
/** Reads `<dir>/harness/plugin.json`. Returns undefined when the folder has no harness. */
|
|
38
|
+
export declare function loadHarnessPackage(dir: string, layer: Layer): HarnessPackage | undefined;
|
|
39
|
+
/** Reads `extensions["dev.harnesspm"]` of a manifest. Problems go into `problems`. */
|
|
40
|
+
export declare function parseExtension(manifest: Record<string, unknown>, problems: string[]): Extension;
|
|
41
|
+
export type Activation = 'always' | 'file' | 'demand';
|
|
42
|
+
export interface RuleSource {
|
|
43
|
+
kind: 'rules';
|
|
44
|
+
id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
pkg: HarnessPackage;
|
|
47
|
+
file: string;
|
|
48
|
+
}
|
|
49
|
+
export interface SkillSource {
|
|
50
|
+
kind: 'skills';
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
pkg: HarnessPackage;
|
|
54
|
+
dir: string;
|
|
55
|
+
}
|
|
56
|
+
export interface DocsSource {
|
|
57
|
+
kind: 'docs';
|
|
58
|
+
id: string;
|
|
59
|
+
pkg: HarnessPackage;
|
|
60
|
+
dir: string;
|
|
61
|
+
}
|
|
62
|
+
export interface PackageUnits {
|
|
63
|
+
rules: RuleSource[];
|
|
64
|
+
skills: SkillSource[];
|
|
65
|
+
docs?: DocsSource;
|
|
66
|
+
}
|
|
67
|
+
/** Finds a package's units. Files and folders in `skip` (replacements and patches) are not units. */
|
|
68
|
+
export declare function discoverUnits(pkg: HarnessPackage, skip: Set<string>): PackageUnits;
|
|
69
|
+
export interface RuleMeta {
|
|
70
|
+
activation: Activation;
|
|
71
|
+
description?: string;
|
|
72
|
+
paths?: string[];
|
|
73
|
+
title: string;
|
|
74
|
+
body: string;
|
|
75
|
+
}
|
|
76
|
+
/** Reads a rule's activation from its frontmatter (spec 2.5). */
|
|
77
|
+
export declare function ruleMeta(text: string, fallbackTitle: string, file: string): RuleMeta;
|
|
78
|
+
export declare function readRule(file: string): string;
|