@bluenyang/reins 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -24
- package/cli/dist/commands/check.js +180 -90
- package/cli/dist/commands/configure.js +79 -0
- package/cli/dist/commands/gate.js +54 -0
- package/cli/dist/commands/install.js +80 -30
- package/cli/dist/commands/metrics.js +138 -0
- package/cli/dist/commands/stages.js +435 -0
- package/cli/dist/commands/status.js +249 -0
- package/cli/dist/commands/update.js +70 -0
- package/cli/dist/lib/blocks.js +6 -1
- package/cli/dist/lib/detect.js +91 -1
- package/cli/dist/lib/fsx.js +8 -3
- package/cli/dist/lib/generate.js +143 -0
- package/cli/dist/lib/git.js +12 -1
- package/cli/dist/lib/harness.js +15 -4
- package/cli/dist/lib/metrics.js +223 -0
- package/cli/dist/lib/reader.js +47 -0
- package/cli/dist/lib/schema.js +23 -2
- package/cli/dist/lib/skills.js +13 -2
- package/cli/dist/lib/upstream.js +8 -0
- package/cli/dist/lib/vendor-write.js +136 -0
- package/cli/dist/lib/work.js +106 -0
- package/cli/dist/reins.js +161 -7
- package/cli/dist/scripts/sync-version.js +16 -0
- package/docs/README.ko.md +31 -25
- package/harness/CHANGELOG.md +5 -0
- package/harness/VERSION +1 -1
- package/harness/policy/stops.yaml +2 -2
- package/harness/policy/transitions.yaml +1 -1
- package/harness/roles/code-review.md +6 -3
- package/harness/roles/pl.md +2 -1
- package/harness/roles/qa-design.md +1 -1
- package/harness/roles/qa-run.md +1 -1
- package/harness/templates/bus/request.yaml +8 -2
- package/harness/templates/bus/response.yaml +1 -0
- package/package.json +10 -2
- package/skills/reins-loop/SKILL.md +5 -1
- package/skills/reins-loop/references/vendor-cli.md +40 -0
- package/skills/reins-setup/SKILL.md +7 -2
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
export function workTreeReader(root) {
|
|
5
|
+
const walk = (rel, out) => {
|
|
6
|
+
const abs = join(root, rel);
|
|
7
|
+
if (!existsSync(abs))
|
|
8
|
+
return out;
|
|
9
|
+
for (const e of readdirSync(abs)) {
|
|
10
|
+
const r = `${rel}/${e}`;
|
|
11
|
+
if (statSync(join(root, r)).isDirectory())
|
|
12
|
+
walk(r, out);
|
|
13
|
+
else
|
|
14
|
+
out.push(r);
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
read: (rel) => {
|
|
20
|
+
const abs = join(root, rel);
|
|
21
|
+
return existsSync(abs) && statSync(abs).isFile() ? readFileSync(abs, 'utf8') : undefined;
|
|
22
|
+
},
|
|
23
|
+
list: (relDir) => walk(relDir.replace(/\/$/, ''), []),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Reads with `git show :<path>`; a path that is not in the index is absent. Only the paths asked for are read. */
|
|
27
|
+
export function indexReader(root) {
|
|
28
|
+
const run = (args) => {
|
|
29
|
+
try {
|
|
30
|
+
return execFileSync('git', args, {
|
|
31
|
+
cwd: root,
|
|
32
|
+
encoding: 'utf8',
|
|
33
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
34
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
read: (rel) => run(['show', `:${rel}`]),
|
|
43
|
+
list: (relDir) => (run(['ls-files', '-z', '--', `${relDir.replace(/\/$/, '')}/`]) ?? '')
|
|
44
|
+
.split('\0')
|
|
45
|
+
.filter(Boolean),
|
|
46
|
+
};
|
|
47
|
+
}
|
package/cli/dist/lib/schema.js
CHANGED
|
@@ -3,6 +3,27 @@ import { basename, join } from 'node:path';
|
|
|
3
3
|
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
4
4
|
import { parse } from 'yaml';
|
|
5
5
|
import { splitFrontmatter } from './skills.js';
|
|
6
|
+
/** The same, read through a `Reader` from a repository-relative harness directory. */
|
|
7
|
+
export function loadSchemasFrom(rd, harnessRel) {
|
|
8
|
+
const dir = `${harnessRel}/schemas`;
|
|
9
|
+
const ajv = new Ajv2020({ allErrors: true, strict: false });
|
|
10
|
+
const ids = new Map();
|
|
11
|
+
for (const p of rd
|
|
12
|
+
.list(dir)
|
|
13
|
+
.filter((x) => /^[^/]+\.schema\.json$/.test(x.slice(dir.length + 1)))) {
|
|
14
|
+
const schema = JSON.parse(rd.read(p) ?? '{}');
|
|
15
|
+
ajv.addSchema(schema);
|
|
16
|
+
if (schema.$id)
|
|
17
|
+
ids.set(p.slice(dir.length + 1).replace(/\.schema\.json$/, ''), schema.$id);
|
|
18
|
+
}
|
|
19
|
+
const out = new Map();
|
|
20
|
+
for (const [name, id] of ids) {
|
|
21
|
+
const v = ajv.getSchema(id);
|
|
22
|
+
if (v)
|
|
23
|
+
out.set(name, v);
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
6
27
|
/** Loads every `*.schema.json` in the harness and compiles them by file name (`intent`, `bus-request`, …). */
|
|
7
28
|
export function loadSchemas(harness) {
|
|
8
29
|
const dir = join(harness, 'schemas');
|
|
@@ -30,8 +51,8 @@ function errors(v, path) {
|
|
|
30
51
|
}));
|
|
31
52
|
}
|
|
32
53
|
/** Validates a Markdown file's YAML frontmatter, or a whole YAML file when `whole` is set. */
|
|
33
|
-
export function validateFile(v, path, whole = false) {
|
|
34
|
-
const text = readFileSync(path, 'utf8');
|
|
54
|
+
export function validateFile(v, path, whole = false, content) {
|
|
55
|
+
const text = content ?? readFileSync(path, 'utf8');
|
|
35
56
|
let data;
|
|
36
57
|
let error;
|
|
37
58
|
if (whole) {
|
package/cli/dist/lib/skills.js
CHANGED
|
@@ -16,10 +16,10 @@ export function splitFrontmatter(text) {
|
|
|
16
16
|
return { data: null, body: m[2] ?? '', error: e.message.split('\n')[0] };
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
export function validateSkillFile(path) {
|
|
19
|
+
export function validateSkillFile(path, content) {
|
|
20
20
|
const issues = [];
|
|
21
21
|
const dir = basename(join(path, '..'));
|
|
22
|
-
const text = readFileSync(path, 'utf8');
|
|
22
|
+
const text = content ?? readFileSync(path, 'utf8');
|
|
23
23
|
const { data, body, error } = splitFrontmatter(text);
|
|
24
24
|
if (!data)
|
|
25
25
|
return [
|
|
@@ -61,3 +61,14 @@ export function findSkillFiles(roots) {
|
|
|
61
61
|
}
|
|
62
62
|
return out;
|
|
63
63
|
}
|
|
64
|
+
export const SKILLS_INDEX_HEADER = '<!-- generated by reins from .agents/skills/*/SKILL.md — edit the skills, not this table -->';
|
|
65
|
+
/** `.agents/SKILLS.md`: one row per skill, the first sentence of its description. */
|
|
66
|
+
export function skillsIndex(skills) {
|
|
67
|
+
const rows = skills
|
|
68
|
+
.toSorted((a, b) => a.name.localeCompare(b.name))
|
|
69
|
+
.map((s) => {
|
|
70
|
+
const first = s.description.split(/(?<=\.)\s| — /)[0] ?? s.description;
|
|
71
|
+
return `| \`${s.name}\` | ${first.replace(/\|/g, '\\|').trim()} |`;
|
|
72
|
+
});
|
|
73
|
+
return `${SKILLS_INDEX_HEADER}\n# Skills\n\n| skill | does |\n|---|---|\n${rows.join('\n')}\n`;
|
|
74
|
+
}
|
package/cli/dist/lib/upstream.js
CHANGED
|
@@ -14,3 +14,11 @@ export function packageHarness() {
|
|
|
14
14
|
export function harnessVersion(dir) {
|
|
15
15
|
return readFileSync(join(dir, 'VERSION'), 'utf8').trim();
|
|
16
16
|
}
|
|
17
|
+
/** The skill entry points shipped with this package: `<pkg>/skills`, next to `harness/`. */
|
|
18
|
+
export function packageSkills() {
|
|
19
|
+
return join(packageHarness(), '..', 'skills');
|
|
20
|
+
}
|
|
21
|
+
/** Which packaged skills a tier installs: the harness tier has no loop to run. */
|
|
22
|
+
export function tierSkills(tier) {
|
|
23
|
+
return tier === 'harness' ? ['reins-setup'] : ['reins-setup', 'reins-intent', 'reins-loop'];
|
|
24
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { parse, stringify } from 'yaml';
|
|
4
|
+
import { MANIFEST, generateVendorFiles, sha } from './generate.js';
|
|
5
|
+
import { workTreeReader } from './reader.js';
|
|
6
|
+
export function readManifest(root, rd = workTreeReader(root)) {
|
|
7
|
+
const text = rd.read(MANIFEST);
|
|
8
|
+
if (text === undefined)
|
|
9
|
+
return { schema: 'reins/generated@1', files: {} };
|
|
10
|
+
const m = parse(text);
|
|
11
|
+
return { schema: 'reins/generated@1', files: m?.files ?? {} };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Write generated vendor files without trampling anyone (design §6, red line C3):
|
|
15
|
+
* reins overwrites only what it wrote and nobody touched since; a file it never wrote is left alone.
|
|
16
|
+
*/
|
|
17
|
+
export function writeGenerated(root, gen, dryRun) {
|
|
18
|
+
const res = {
|
|
19
|
+
written: [],
|
|
20
|
+
removed: [],
|
|
21
|
+
unchanged: [],
|
|
22
|
+
refused: [],
|
|
23
|
+
skipped: gen.skipped,
|
|
24
|
+
};
|
|
25
|
+
const before = readManifest(root).files;
|
|
26
|
+
const after = {};
|
|
27
|
+
const wanted = new Set(gen.writes.map((w) => w.path));
|
|
28
|
+
for (const w of gen.writes) {
|
|
29
|
+
const abs = join(root, w.path);
|
|
30
|
+
const next = sha(w.content);
|
|
31
|
+
if (!existsSync(abs)) {
|
|
32
|
+
if (!dryRun) {
|
|
33
|
+
mkdirSync(dirname(abs), { recursive: true });
|
|
34
|
+
writeFileSync(abs, w.content);
|
|
35
|
+
}
|
|
36
|
+
res.written.push(w.path);
|
|
37
|
+
after[w.path] = next;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const disk = sha(readFileSync(abs, 'utf8'));
|
|
41
|
+
if (disk === next) {
|
|
42
|
+
res.unchanged.push(w.path);
|
|
43
|
+
after[w.path] = next;
|
|
44
|
+
}
|
|
45
|
+
else if (before[w.path] === disk) {
|
|
46
|
+
if (!dryRun)
|
|
47
|
+
writeFileSync(abs, w.content);
|
|
48
|
+
res.written.push(w.path);
|
|
49
|
+
after[w.path] = next;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
res.refused.push({
|
|
53
|
+
path: w.path,
|
|
54
|
+
why: before[w.path]
|
|
55
|
+
? 'edited by hand since reins generated it (C3) — move the change into .agents/harness and run `reins update`'
|
|
56
|
+
: 'exists and was not generated by reins — merge it by hand, or delete it and run `reins update`',
|
|
57
|
+
});
|
|
58
|
+
const prior = before[w.path];
|
|
59
|
+
if (prior)
|
|
60
|
+
after[w.path] = prior;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// files reins wrote earlier that are no longer generated (a role re-bound to another vendor)
|
|
64
|
+
for (const [path, hash] of Object.entries(before)) {
|
|
65
|
+
if (wanted.has(path))
|
|
66
|
+
continue;
|
|
67
|
+
const abs = join(root, path);
|
|
68
|
+
if (!existsSync(abs))
|
|
69
|
+
continue;
|
|
70
|
+
if (sha(readFileSync(abs, 'utf8')) === hash) {
|
|
71
|
+
if (!dryRun)
|
|
72
|
+
unlinkSync(abs);
|
|
73
|
+
res.removed.push(path);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
res.refused.push({
|
|
77
|
+
path,
|
|
78
|
+
why: 'no longer generated, but edited by hand — delete it yourself if it is not needed',
|
|
79
|
+
});
|
|
80
|
+
after[path] = hash;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const changed = JSON.stringify(after) !== JSON.stringify(before);
|
|
84
|
+
if (changed && !dryRun) {
|
|
85
|
+
mkdirSync(dirname(join(root, MANIFEST)), { recursive: true });
|
|
86
|
+
writeFileSync(join(root, MANIFEST), `# vendor files generated by reins — do not edit; \`reins update\` rewrites it\n${stringify({ schema: 'reins/generated@1', files: after })}`);
|
|
87
|
+
}
|
|
88
|
+
return res;
|
|
89
|
+
}
|
|
90
|
+
/** Drift, match layer for generated files: on disk = what reins wrote = what it would write now. */
|
|
91
|
+
export function driftGenerated(root, rd = workTreeReader(root)) {
|
|
92
|
+
if (rd.read(MANIFEST) === undefined)
|
|
93
|
+
return [];
|
|
94
|
+
const findings = [];
|
|
95
|
+
const recorded = readManifest(root, rd).files;
|
|
96
|
+
const now = new Map(generateVendorFiles(root, rd).writes.map((w) => [w.path, sha(w.content)]));
|
|
97
|
+
for (const [path, hash] of Object.entries(recorded)) {
|
|
98
|
+
const onDisk = rd.read(path);
|
|
99
|
+
if (onDisk === undefined) {
|
|
100
|
+
findings.push({
|
|
101
|
+
reason: 'F2',
|
|
102
|
+
verdict: 'STOP',
|
|
103
|
+
path,
|
|
104
|
+
message: 'generated file is missing',
|
|
105
|
+
resume: 'run `reins update`',
|
|
106
|
+
});
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (sha(onDisk) !== hash)
|
|
110
|
+
findings.push({
|
|
111
|
+
reason: 'C3',
|
|
112
|
+
verdict: 'STOP',
|
|
113
|
+
path,
|
|
114
|
+
message: 'generated file was edited by hand',
|
|
115
|
+
resume: 'change .agents/harness instead, then `reins update`',
|
|
116
|
+
});
|
|
117
|
+
else if (now.has(path) && now.get(path) !== hash)
|
|
118
|
+
findings.push({
|
|
119
|
+
reason: 'F2',
|
|
120
|
+
verdict: 'STOP',
|
|
121
|
+
path,
|
|
122
|
+
message: 'stale — the harness changed since it was generated',
|
|
123
|
+
resume: 'run `reins update`',
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
for (const path of now.keys())
|
|
127
|
+
if (!(path in recorded) && !existsSync(join(root, path)))
|
|
128
|
+
findings.push({
|
|
129
|
+
reason: 'F2',
|
|
130
|
+
verdict: 'STOP',
|
|
131
|
+
path,
|
|
132
|
+
message: 'should be generated but is not',
|
|
133
|
+
resume: 'run `reins update`',
|
|
134
|
+
});
|
|
135
|
+
return findings;
|
|
136
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { relPath } from './fsx.js';
|
|
6
|
+
import { splitFrontmatter } from './skills.js';
|
|
7
|
+
function read(root, abs) {
|
|
8
|
+
const { data, body, error } = splitFrontmatter(readFileSync(abs, 'utf8'));
|
|
9
|
+
return { path: relPath(root, abs), data: data ?? {}, body, error };
|
|
10
|
+
}
|
|
11
|
+
const ls = (dir) => (existsSync(dir) ? readdirSync(dir).toSorted() : []);
|
|
12
|
+
/** `.agents/intent/<id>_<slug>/` */
|
|
13
|
+
export function intentDir(root, id) {
|
|
14
|
+
const base = join(root, '.agents', 'intent');
|
|
15
|
+
const d = ls(base).find((e) => e === id || e.startsWith(`${id}_`));
|
|
16
|
+
return d ? join(base, d) : null;
|
|
17
|
+
}
|
|
18
|
+
export function intentIds(root) {
|
|
19
|
+
return ls(join(root, '.agents', 'intent'))
|
|
20
|
+
.map((e) => /^(\d{3})(_|$)/.exec(e)?.[1])
|
|
21
|
+
.filter((x) => !!x);
|
|
22
|
+
}
|
|
23
|
+
/** intent.md, spec.md or plan.md of an intent, or null when the file is not written yet. */
|
|
24
|
+
export function intentFile(root, id, kind) {
|
|
25
|
+
const d = intentDir(root, id);
|
|
26
|
+
const p = d && join(d, `${kind}.md`);
|
|
27
|
+
return p && existsSync(p) ? read(root, p) : null;
|
|
28
|
+
}
|
|
29
|
+
/** `.loctx/ticket/<id>_<slug>.md` — a ticket that is done is deleted, so null may also mean done. */
|
|
30
|
+
export function ticketFile(root, id) {
|
|
31
|
+
const dir = join(root, '.loctx', 'ticket');
|
|
32
|
+
const f = ls(dir).find((e) => e === `${id}.md` || e.startsWith(`${id}_`));
|
|
33
|
+
return f ? read(root, join(dir, f)) : null;
|
|
34
|
+
}
|
|
35
|
+
export function openTickets(root) {
|
|
36
|
+
const dir = join(root, '.loctx', 'ticket');
|
|
37
|
+
return ls(dir)
|
|
38
|
+
.filter((e) => e.endsWith('.md'))
|
|
39
|
+
.map((e) => read(root, join(dir, e)));
|
|
40
|
+
}
|
|
41
|
+
function loadYaml(root, p, broken) {
|
|
42
|
+
if (!existsSync(p))
|
|
43
|
+
return null;
|
|
44
|
+
try {
|
|
45
|
+
return parse(readFileSync(p, 'utf8')) ?? null;
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
const message = (e.message ?? String(e)).split('\n')[0] ?? '';
|
|
49
|
+
broken.push({ path: relPath(root, p), message });
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Bus exchanges of one ticket, in request-id order (`<ticket>_<nn>`). */
|
|
54
|
+
export function busFor(root, ticket) {
|
|
55
|
+
const dir = join(root, '.loctx', 'bus');
|
|
56
|
+
const ids = new Set(ls(dir)
|
|
57
|
+
.map((e) => /^(.+)\.(request|response)\.yaml$/.exec(e)?.[1])
|
|
58
|
+
.filter((x) => !!x && x.startsWith(`${ticket}_`)));
|
|
59
|
+
return [...ids].toSorted().map((id) => {
|
|
60
|
+
const broken = [];
|
|
61
|
+
return {
|
|
62
|
+
id,
|
|
63
|
+
request: loadYaml(root, join(dir, `${id}.request.yaml`), broken),
|
|
64
|
+
response: loadYaml(root, join(dir, `${id}.response.yaml`), broken),
|
|
65
|
+
broken,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** Every history page's text, newest last. */
|
|
70
|
+
export function historyPages(root) {
|
|
71
|
+
const dir = join(root, '.loctx', 'history');
|
|
72
|
+
return ls(dir)
|
|
73
|
+
.filter((e) => e.endsWith('.md'))
|
|
74
|
+
.map((e) => ({ path: `.loctx/history/${e}`, text: readFileSync(join(dir, e), 'utf8') }));
|
|
75
|
+
}
|
|
76
|
+
/** Recorded decisions: knowledge `decisions:` entries and history `decisions:` lines, with where they live. */
|
|
77
|
+
export function recordedDecisions(root) {
|
|
78
|
+
const out = [];
|
|
79
|
+
const kdir = join(root, '.agents', 'knowledge');
|
|
80
|
+
for (const e of ls(kdir).filter((x) => x.endsWith('.md'))) {
|
|
81
|
+
const d = read(root, join(kdir, e)).data['decisions'];
|
|
82
|
+
if (Array.isArray(d))
|
|
83
|
+
for (const x of d)
|
|
84
|
+
out.push({ path: `.agents/knowledge/${e}`, decision: String(x) });
|
|
85
|
+
}
|
|
86
|
+
for (const h of historyPages(root)) {
|
|
87
|
+
const line = /^decisions:\s*(.+)$/m.exec(h.text)?.[1];
|
|
88
|
+
if (line)
|
|
89
|
+
for (const x of line.split(','))
|
|
90
|
+
out.push({ path: h.path, decision: x.trim() });
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/** Commits that touched a path and carry an `Approved-by:` trailer (design §4: approval = frontmatter + trailer). */
|
|
95
|
+
export function approvedBy(root, path) {
|
|
96
|
+
try {
|
|
97
|
+
const out = execFileSync('git', ['log', '--format=%(trailers:key=Approved-by,valueonly,separator=%x2C)', '--', path], { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
98
|
+
return out
|
|
99
|
+
.split(/[\n,]/)
|
|
100
|
+
.map((s) => s.trim())
|
|
101
|
+
.filter(Boolean);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
}
|
package/cli/dist/reins.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
2
3
|
import { Command } from 'commander';
|
|
3
|
-
import { runCheck } from './commands/check.js';
|
|
4
|
+
import { runCheck, STAGES } from './commands/check.js';
|
|
4
5
|
import { runInstall } from './commands/install.js';
|
|
5
6
|
import { runMigrate } from './commands/migrate.js';
|
|
7
|
+
import { runStatus } from './commands/status.js';
|
|
8
|
+
import { runUpdate } from './commands/update.js';
|
|
9
|
+
import { configureBindings } from './commands/configure.js';
|
|
10
|
+
import { runGates } from './commands/gate.js';
|
|
11
|
+
import { collectMetrics, formatMetrics, logCheck, logGates, recordUsage, } from './commands/metrics.js';
|
|
6
12
|
import { defaultsAsker, stdinAsker } from './lib/ask.js';
|
|
7
13
|
import { EXIT } from './lib/verdict.js';
|
|
14
|
+
// the version npm installed — cli/dist/reins.js sits two levels below package.json
|
|
15
|
+
const VERSION = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version;
|
|
8
16
|
const program = new Command()
|
|
9
17
|
.name('reins')
|
|
10
18
|
.description('agent harness — install, migrate, update; check keeps agents inside their roles')
|
|
11
|
-
.version(
|
|
19
|
+
.version(VERSION)
|
|
20
|
+
.enablePositionalOptions();
|
|
12
21
|
const notYet = (name) => () => {
|
|
13
22
|
process.stderr.write(`reins ${name}: not implemented yet (see docs/design.md §11)\n`);
|
|
14
23
|
process.exit(2);
|
|
@@ -93,23 +102,168 @@ program
|
|
|
93
102
|
if (r.history)
|
|
94
103
|
process.stdout.write(`recorded: ${r.history}\n`);
|
|
95
104
|
});
|
|
96
|
-
|
|
105
|
+
program
|
|
106
|
+
.command('update')
|
|
107
|
+
.description('re-apply the packaged harness: upstream files, managed blocks, vendor files, version pin')
|
|
108
|
+
.option('--dry-run', 'show what would change')
|
|
109
|
+
.option('--json')
|
|
110
|
+
.action(async (o) => {
|
|
111
|
+
const r = await runUpdate({ dryRun: o.dryRun });
|
|
112
|
+
if (o.json) {
|
|
113
|
+
process.stdout.write(`${JSON.stringify(r, null, 2)}\n`);
|
|
114
|
+
process.exit(r.refused ? 2 : 0);
|
|
115
|
+
}
|
|
116
|
+
if (r.refused) {
|
|
117
|
+
process.stderr.write(`reins update: ${r.refused}\n`);
|
|
118
|
+
process.exit(2);
|
|
119
|
+
}
|
|
120
|
+
const a = r.install?.applied;
|
|
121
|
+
process.stdout.write(`reins update — harness v${r.from} → v${r.to}${o.dryRun ? ' (dry run)' : ''}\n`);
|
|
122
|
+
for (const c of r.changes)
|
|
123
|
+
process.stdout.write(` · ${c}\n`);
|
|
124
|
+
if (a) {
|
|
125
|
+
process.stdout.write(`${o.dryRun ? 'would write' : 'wrote'}: ${a.created.length} new, ${a.changed.length} changed\n`);
|
|
126
|
+
for (const p of [...a.created, ...a.changed])
|
|
127
|
+
process.stdout.write(` ~ ${p}\n`);
|
|
128
|
+
}
|
|
129
|
+
for (const n of r.overrides)
|
|
130
|
+
process.stdout.write(`• block "${n}" is overridden by the project — compare it with the new upstream block\n`);
|
|
131
|
+
for (const n of r.install?.notes.filter((x) => x.startsWith('left alone')) ?? [])
|
|
132
|
+
process.stdout.write(`• ${n}\n`);
|
|
133
|
+
if (r.needsSkill)
|
|
134
|
+
process.stdout.write('• this update needs project sections changed: run /reins-update in your agent\n');
|
|
135
|
+
if (r.install?.history)
|
|
136
|
+
process.stdout.write(`recorded: ${r.install.history}\n`);
|
|
137
|
+
});
|
|
138
|
+
const configure = program.command('configure').description('change project-owned settings');
|
|
139
|
+
configure
|
|
140
|
+
.command('bindings')
|
|
141
|
+
.description('bind roles to models: --set role=model … or --vendors claude,openai,…; no options asks')
|
|
142
|
+
.option('--set <pairs...>', 'role=model pairs')
|
|
143
|
+
.option('--vendors <list>', 'rebuild every role from its defaults for these vendors (comma-separated)')
|
|
144
|
+
.option('--json')
|
|
145
|
+
.action(async (o) => {
|
|
146
|
+
const asker = !o.set && !o.vendors ? await stdinAsker() : undefined;
|
|
147
|
+
const r = await configureBindings({
|
|
148
|
+
set: o.set,
|
|
149
|
+
vendors: o.vendors?.split(',').map((s) => s.trim()),
|
|
150
|
+
asker,
|
|
151
|
+
});
|
|
152
|
+
asker?.close();
|
|
153
|
+
if (o.json) {
|
|
154
|
+
process.stdout.write(`${JSON.stringify(r, null, 2)}\n`);
|
|
155
|
+
process.exit(r.refused ? 2 : 0);
|
|
156
|
+
}
|
|
157
|
+
if (r.refused) {
|
|
158
|
+
process.stderr.write(`reins configure bindings: ${r.refused}\n`);
|
|
159
|
+
process.exit(2);
|
|
160
|
+
}
|
|
161
|
+
for (const t of r.table)
|
|
162
|
+
process.stdout.write(` ${r.changed.includes(t.role) ? '*' : ' '} ${t.role.padEnd(12)} ${t.vendor.padEnd(7)} ${t.model}\n`);
|
|
163
|
+
process.stdout.write(r.changed.length ? `changed: ${r.changed.join(', ')}\n` : 'no change\n');
|
|
164
|
+
const g = r.generated;
|
|
165
|
+
if (g) {
|
|
166
|
+
for (const p of g.written)
|
|
167
|
+
process.stdout.write(` ~ ${p}\n`);
|
|
168
|
+
for (const p of g.removed)
|
|
169
|
+
process.stdout.write(` - ${p}\n`);
|
|
170
|
+
for (const x of g.refused)
|
|
171
|
+
process.stdout.write(`• left alone ${x.path}: ${x.why}\n`);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
program
|
|
175
|
+
.command('gate')
|
|
176
|
+
.description('run gates.yaml gates as declared; raw output goes to .loctx/reports/ as T3 evidence')
|
|
177
|
+
.argument('[names...]', 'gates to run (default: the declared order)')
|
|
178
|
+
.option('--ticket <id>', 'name the reports after this ticket')
|
|
179
|
+
.option('--all', 'keep going after a failure')
|
|
180
|
+
.option('--json')
|
|
181
|
+
.action((names, o) => {
|
|
182
|
+
const r = runGates({ names, ticket: o.ticket, all: o.all });
|
|
183
|
+
logGates(r.runs, { ticket: o.ticket });
|
|
184
|
+
if (o.json)
|
|
185
|
+
process.stdout.write(`${JSON.stringify(r, null, 2)}\n`);
|
|
186
|
+
else if (r.refused)
|
|
187
|
+
process.stderr.write(`reins gate: ${r.refused}\n`);
|
|
188
|
+
else
|
|
189
|
+
for (const g of r.runs)
|
|
190
|
+
process.stdout.write(` ${g.skipped ? '-' : g.exit === 0 ? 'ok' : 'FAIL'} ${g.gate.padEnd(10)} ${g.skipped ?? `exit ${g.exit} → ${g.report}`}\n`);
|
|
191
|
+
process.exit(r.refused ? 2 : r.runs.every((g) => g.exit === 0) ? 0 : 1);
|
|
192
|
+
});
|
|
193
|
+
program
|
|
194
|
+
.command('status')
|
|
195
|
+
.description('where the project stands: harness, intents, tickets, bus, last history (read-only)')
|
|
196
|
+
.option('--json')
|
|
197
|
+
.action((o) => {
|
|
198
|
+
const r = runStatus({ json: o.json });
|
|
199
|
+
process.stdout.write(r.stdout);
|
|
200
|
+
process.stderr.write(r.stderr);
|
|
201
|
+
process.exit(r.code);
|
|
202
|
+
});
|
|
203
|
+
const metrics = program
|
|
204
|
+
.command('metrics')
|
|
205
|
+
.enablePositionalOptions()
|
|
206
|
+
.description('what the loop cost and where it stopped: counts, tokens by role and vendor, per ticket and intent')
|
|
207
|
+
.option('--intent <NNN>', 'only this intent')
|
|
208
|
+
.option('--json')
|
|
209
|
+
.action((o) => {
|
|
210
|
+
const m = collectMetrics({ intent: o.intent });
|
|
211
|
+
process.stdout.write(o.json ? `${JSON.stringify(m, null, 2)}\n` : formatMetrics(m));
|
|
212
|
+
});
|
|
213
|
+
metrics
|
|
214
|
+
.command('record')
|
|
215
|
+
.description('append a usage line: tokens a role run used (the caller reports; reins calls no model)')
|
|
216
|
+
.requiredOption('--role <role>', 'a role in bindings.yaml')
|
|
217
|
+
.requiredOption('--tokens <n>', 'whole number ≥ 0')
|
|
218
|
+
.option('--ticket <id>', 'the ticket the run served (fills the intent)')
|
|
219
|
+
.option('--intent <NNN>', 'the intent, when there is no ticket')
|
|
220
|
+
.option('--vendor <v>', 'default: the binding of --role')
|
|
221
|
+
.option('--model <m>', 'default: the binding of --role')
|
|
222
|
+
.option('--note <text>')
|
|
223
|
+
.option('--json')
|
|
224
|
+
.action((o) => {
|
|
225
|
+
const r = recordUsage(o);
|
|
226
|
+
if (r.refused) {
|
|
227
|
+
process.stderr.write(`reins metrics record: ${r.refused}\n`);
|
|
228
|
+
process.exit(2);
|
|
229
|
+
}
|
|
230
|
+
const u = r.record;
|
|
231
|
+
process.stdout.write(o.json
|
|
232
|
+
? `${JSON.stringify(u, null, 2)}\n`
|
|
233
|
+
: `recorded: ${u.role} (${u.vendor}/${u.model}) ${u.tokens} tokens${u.ticket ? ` · ticket ${u.ticket}` : ''}${u.intent ? ` · intent ${u.intent}` : ''}\n`);
|
|
234
|
+
});
|
|
235
|
+
for (const n of ['recall', 'doctor'])
|
|
97
236
|
program.command(n).allowUnknownOption().allowExcessArguments().action(notYet(n));
|
|
98
237
|
program
|
|
99
238
|
.command('check')
|
|
100
|
-
.description(
|
|
101
|
-
.option('--stage <stage>')
|
|
239
|
+
.description(`harness verifier: --stage ${STAGES.join('|')}, or --drift`)
|
|
240
|
+
.option('--stage <stage>', 'T1 ticket-start · T2 pre-commit · T3 verdict · T4 pre-push · T5 rc-close')
|
|
241
|
+
.option('--ticket <id>', 'ticket-start, verdict: the ticket id')
|
|
242
|
+
.option('--release <version>', 'rc-close: the version the rc closes')
|
|
102
243
|
.option('--drift')
|
|
103
244
|
.option('--json')
|
|
104
245
|
.action(async (o) => {
|
|
246
|
+
if (o.stage && !STAGES.includes(o.stage)) {
|
|
247
|
+
process.stderr.write(`reins check: unknown stage "${o.stage}" (${STAGES.join(', ')})\n`);
|
|
248
|
+
process.exit(2);
|
|
249
|
+
}
|
|
250
|
+
const stage = o.stage;
|
|
105
251
|
let pushRefs = [];
|
|
106
|
-
if (
|
|
252
|
+
if (stage === 'pre-push' && !process.stdin.isTTY) {
|
|
107
253
|
const chunks = [];
|
|
108
254
|
for await (const c of process.stdin)
|
|
109
255
|
chunks.push(c);
|
|
110
256
|
pushRefs = Buffer.concat(chunks).toString('utf8').split('\n').filter(Boolean);
|
|
111
257
|
}
|
|
112
|
-
const r = runCheck({ stage: o.
|
|
258
|
+
const r = runCheck({ stage, drift: o.drift, pushRefs, ticket: o.ticket, release: o.release });
|
|
259
|
+
// S-8: the verdict code is set before anything is printed, so a closed pipe cannot change it
|
|
260
|
+
process.exitCode = EXIT[r.verdict];
|
|
261
|
+
process.stdout.on('error', (e) => {
|
|
262
|
+
if (e.code === 'EPIPE')
|
|
263
|
+
process.exit(process.exitCode ?? 0);
|
|
264
|
+
throw e;
|
|
265
|
+
});
|
|
266
|
+
logCheck(r, { ticket: o.ticket });
|
|
113
267
|
if (o.json)
|
|
114
268
|
process.stdout.write(`${JSON.stringify(r, null, 2)}\n`);
|
|
115
269
|
else {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Sync plugin.json `version` and the pinned CLI version in the reins-setup skill to package.json.
|
|
2
|
+
// Run by the `version` lifecycle script (npm/pnpm version), after package.json is bumped.
|
|
3
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
// compiled to cli/dist/scripts/: the repository root is three levels up (or the first argument)
|
|
7
|
+
const root = pathToFileURL(`${resolve(process.argv[2] ?? new URL('../../../', import.meta.url).pathname)}/`);
|
|
8
|
+
const read = (p) => readFileSync(new URL(p, root), 'utf8');
|
|
9
|
+
const write = (p, s) => writeFileSync(new URL(p, root), s);
|
|
10
|
+
const version = JSON.parse(read('package.json')).version;
|
|
11
|
+
const plugin = JSON.parse(read('.claude-plugin/plugin.json'));
|
|
12
|
+
plugin.version = version;
|
|
13
|
+
write('.claude-plugin/plugin.json', `${JSON.stringify(plugin, null, 2)}\n`);
|
|
14
|
+
const skillPath = 'skills/reins-setup/SKILL.md';
|
|
15
|
+
write(skillPath, read(skillPath).replace(/@bluenyang\/reins@[0-9][\w.+-]*/g, `@bluenyang/reins@${version}`));
|
|
16
|
+
console.log(`synced to ${version}`);
|