@warpgogol/forge 9.6.1 → 9.6.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/CHANGELOG.md +20 -0
- package/README.md +42 -18
- package/README.uk.md +60 -36
- package/dist/os/core/core.module.d.ts.map +1 -1
- package/dist/os/core/core.module.js +58 -5
- package/dist/os/core/core.module.js.map +1 -1
- package/dist/os/core/handlers/package-health.d.ts.map +1 -1
- package/dist/os/core/handlers/package-health.js +95 -8
- package/dist/os/core/handlers/package-health.js.map +1 -1
- package/dist/os/flow/handlers/flow-cut.d.ts.map +1 -1
- package/dist/os/flow/handlers/flow-cut.js +27 -6
- package/dist/os/flow/handlers/flow-cut.js.map +1 -1
- package/dist/src/onboarding/scaffold-project.d.ts.map +1 -1
- package/dist/src/onboarding/scaffold-project.js +21 -2
- package/dist/src/onboarding/scaffold-project.js.map +1 -1
- package/dist/src/validators/skill-doc-sync.d.ts +76 -0
- package/dist/src/validators/skill-doc-sync.d.ts.map +1 -0
- package/dist/src/validators/skill-doc-sync.js +333 -0
- package/dist/src/validators/skill-doc-sync.js.map +1 -0
- package/docs/concepts/governance-model.md +9 -1
- package/docs/concepts/verification-model.md +31 -0
- package/docs/guides/existing-project.md +12 -0
- package/docs/guides/skills.md +27 -8
- package/docs/guides/upgrading.md +6 -2
- package/docs/guides/verification-and-evidence.md +45 -0
- package/docs/integrations/devin.md +49 -0
- package/docs/reference/cli.md +12 -1
- package/docs/reference/fleet-measurement.md +52 -0
- package/docs/reference/programmatic-api.md +62 -33
- package/os/core/core.module.ts +70 -11
- package/os/core/handlers/package-health.test.ts +104 -23
- package/os/core/handlers/package-health.ts +133 -19
- package/os/flow/handlers/flow-cut.ts +27 -6
- package/os/flow/tests/flow-cut.rerun.test.ts +54 -4
- package/package.json +1 -1
- package/profiles/forge-shell.yaml +1 -0
- package/profiles/godot-game.yaml +1 -0
- package/profiles/phaser-game.yaml +1 -0
- package/profiles/site-workshop.yaml +1 -0
- package/profiles/system.yaml +1 -0
- package/profiles/typescript.yaml +1 -0
- package/src/onboarding/scaffold-project.ts +19 -2
- package/src/tests/skill-doc-sync.test.ts +233 -0
- package/src/validators/skill-doc-sync.ts +449 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Unit tests for RFC-1585 skill.doc-sync — marked-region render/drift, claim lint grammar,
|
|
5
|
+
doc-surface discovery, and the --write path.
|
|
6
|
+
</purpose>
|
|
7
|
+
<non-goals>
|
|
8
|
+
<item>Not a registry contract test — fixtures use synthetic ForgeSkillEntry lists, never FORGE_SKILLS.</item>
|
|
9
|
+
</non-goals>
|
|
10
|
+
</MODULE_CONTRACT>
|
|
11
|
+
<CHANGE_SUMMARY>
|
|
12
|
+
<item>RFC-1585: initial suite — requirement AC-1 (marked-region render), AC-2 (drift detection), AC-3 (claim lint), AC-4 (check gate exit).</item>
|
|
13
|
+
<item>RFC-1585: review wave — write-path corruptions, --check flag, scaffold hydration
|
|
14
|
+
|
|
15
|
+
Independent fo-review (isolation: subagent) found: empty marker pairs lost
|
|
16
|
+
their open tag on --write, multi-line regions collapsed line numbers, a
|
|
17
|
+
leading fence injected a phantom line, --check was undocumented-undeclared,
|
|
18
|
+
and --write exited 1 on a successful heal. Fixes: open-marker capture,
|
|
19
|
+
newline-preserving placeholder, empty-chunk skip, check flag, healedRegions
|
|
20
|
+
split (write always exits 0), dryRun/io threading, byte-exact inventory
|
|
21
|
+
compare on the prettier-stable canonical form, CHECK-13 delegated through
|
|
22
|
+
commandRegistry/invoke so the registry-integrity scan stays within its
|
|
23
|
+
slice cap (KNOWN_TRUNCATED_SCANS is shrink-only), record-tree exclusions
|
|
24
|
+
extended, two real stale claims corrected, profile templates ship empty
|
|
25
|
+
markers hydrated at scaffold write time. RFC amended to implemented
|
|
26
|
+
grammar/result/exclusions; command moved to commands.added.</item>
|
|
27
|
+
</CHANGE_SUMMARY>
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { describe, expect, test } from "vitest";
|
|
31
|
+
import * as sfs from "../utils/sync-fs.ts";
|
|
32
|
+
import path from "node:path";
|
|
33
|
+
import os from "node:os";
|
|
34
|
+
|
|
35
|
+
import type { ForgeSkillEntry } from "../registry.ts";
|
|
36
|
+
import {
|
|
37
|
+
collectSkillDocFiles,
|
|
38
|
+
hydrateSkillDocMarkers,
|
|
39
|
+
lintSkillDocContent,
|
|
40
|
+
lintSkillDocFiles,
|
|
41
|
+
renderCountLiteral,
|
|
42
|
+
renderInventoryBlock,
|
|
43
|
+
runSkillDocSync,
|
|
44
|
+
SKILL_DOC_MARKERS,
|
|
45
|
+
} from "../validators/skill-doc-sync.ts";
|
|
46
|
+
|
|
47
|
+
const skill = (name: string, category: ForgeSkillEntry["category"]): ForgeSkillEntry => ({
|
|
48
|
+
name,
|
|
49
|
+
category,
|
|
50
|
+
invocation: "user",
|
|
51
|
+
concerns: "read-only",
|
|
52
|
+
dependsOn: [],
|
|
53
|
+
path: `${category}/${name}/SKILL.md`,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// 5 skills: 3 fo + 1 shared + 1 meta — small enough to assert exact strings.
|
|
57
|
+
const SKILLS: ForgeSkillEntry[] = [
|
|
58
|
+
skill("fo-aaa", "fo"),
|
|
59
|
+
skill("fo-bbb", "fo"),
|
|
60
|
+
skill("fo-ccc", "fo"),
|
|
61
|
+
skill("shared-aaa", "shared"),
|
|
62
|
+
skill("meta-aaa", "meta"),
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
describe("rendering (requirement AC-1)", () => {
|
|
66
|
+
test("count literal decomposes the registry", () => {
|
|
67
|
+
expect(renderCountLiteral(SKILLS)).toBe("5 skills (3 fo + 1 shared + 1 meta)");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("inventory block lists every registry name grouped by category", () => {
|
|
71
|
+
const block = renderInventoryBlock(SKILLS);
|
|
72
|
+
expect(block).toContain("**5 skills** — 3 fo, 1 shared, 1 meta.");
|
|
73
|
+
expect(block).toContain("- **fo (3):** `fo-aaa`, `fo-bbb`, `fo-ccc`");
|
|
74
|
+
expect(block).toContain("- **shared (1):** `shared-aaa`");
|
|
75
|
+
expect(block).toContain("- **meta (1):** `meta-aaa`");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("marked regions (requirement AC-1, AC-2)", () => {
|
|
80
|
+
test("fresh region passes; stale region drifts", () => {
|
|
81
|
+
const good = `<!-- ${SKILL_DOC_MARKERS.count} -->${renderCountLiteral(SKILLS)}<!-- /${SKILL_DOC_MARKERS.count} -->`;
|
|
82
|
+
expect(lintSkillDocContent("README.md", good, SKILLS).drift).toEqual([]);
|
|
83
|
+
|
|
84
|
+
const stale = `<!-- ${SKILL_DOC_MARKERS.count} -->99 skills (9 fo + 9 shared + 9 meta)<!-- /${SKILL_DOC_MARKERS.count} -->`;
|
|
85
|
+
const outcome = lintSkillDocContent("README.md", stale, SKILLS);
|
|
86
|
+
expect(outcome.markedRegions).toBe(1);
|
|
87
|
+
expect(outcome.drift).toHaveLength(1);
|
|
88
|
+
expect(outcome.drift[0]!.kind).toBe("marked-region");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("--write re-renders only inside markers and stays idempotent", () => {
|
|
92
|
+
const doc = `# Title\n\n<!-- ${SKILL_DOC_MARKERS.count} -->STALE<!-- /${SKILL_DOC_MARKERS.count} -->\n\nTail 7 skills stay authored.\n`;
|
|
93
|
+
// The authored tail claim drifts too — suppress it so only the region is judged.
|
|
94
|
+
const clean = doc.replace("Tail 7 skills stay authored.", "Tail.");
|
|
95
|
+
const once = lintSkillDocContent("README.md", clean, SKILLS, true).rewritten!;
|
|
96
|
+
expect(once).toContain(
|
|
97
|
+
`<!-- ${SKILL_DOC_MARKERS.count} -->${renderCountLiteral(SKILLS)}<!-- /${SKILL_DOC_MARKERS.count} -->`,
|
|
98
|
+
);
|
|
99
|
+
expect(once).toContain("# Title");
|
|
100
|
+
const twice = lintSkillDocContent("README.md", once, SKILLS, true).rewritten!;
|
|
101
|
+
expect(twice).toBe(once);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("inventory region renders inside markers, authored text outside survives", () => {
|
|
105
|
+
const doc = `Intro.\n\n<!-- ${SKILL_DOC_MARKERS.inventory} -->\nold\n<!-- /${SKILL_DOC_MARKERS.inventory} -->\n\nOutro.\n`;
|
|
106
|
+
const out = lintSkillDocContent("docs/guides/skills.md", doc, SKILLS, true).rewritten!;
|
|
107
|
+
expect(out).toContain(renderInventoryBlock(SKILLS));
|
|
108
|
+
expect(out).toContain("Intro.");
|
|
109
|
+
expect(out).toContain("Outro.");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("claim lint (requirement AC-3)", () => {
|
|
114
|
+
test("stale total, decomposition, and category claims all flag", () => {
|
|
115
|
+
const doc = [
|
|
116
|
+
"Forge ships 41 skills today.", // stale total
|
|
117
|
+
"Split: 33 fo + 5 shared + 3 meta.", // stale decomposition
|
|
118
|
+
"There are 9 fo skills.", // stale category
|
|
119
|
+
].join("\n");
|
|
120
|
+
const drift = lintSkillDocContent("README.md", doc, SKILLS).drift;
|
|
121
|
+
expect(drift.map((d) => d.kind).sort()).toEqual([
|
|
122
|
+
"claim-category",
|
|
123
|
+
"claim-decomposition",
|
|
124
|
+
"claim-total",
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("correct claims, exempted lines, and fenced blocks pass", () => {
|
|
129
|
+
const doc = [
|
|
130
|
+
"Forge ships 5 skills today.",
|
|
131
|
+
"Split: 3 fo + 1 shared + 1 meta.",
|
|
132
|
+
"A pack owns 7 skills. <!-- forge-skill-count:ok: project pack count -->",
|
|
133
|
+
"```",
|
|
134
|
+
"example says 99 skills",
|
|
135
|
+
"```",
|
|
136
|
+
].join("\n");
|
|
137
|
+
expect(lintSkillDocContent("README.md", doc, SKILLS).drift).toEqual([]);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("surface + command runner (requirement AC-4)", () => {
|
|
142
|
+
test("collectSkillDocFiles covers README variants and docs, skips record trees", () => {
|
|
143
|
+
const root = sfs.mkdtempSync(path.join(os.tmpdir(), "skill-doc-sync-"));
|
|
144
|
+
sfs.mkdirSync(path.join(root, "docs", "guides"), { recursive: true });
|
|
145
|
+
sfs.mkdirSync(path.join(root, "docs", "rfcs"), { recursive: true });
|
|
146
|
+
sfs.writeFileSync(path.join(root, "README.md"), "x");
|
|
147
|
+
sfs.writeFileSync(path.join(root, "README.uk.md"), "x");
|
|
148
|
+
sfs.writeFileSync(path.join(root, "AGENTS.md"), "x");
|
|
149
|
+
sfs.writeFileSync(path.join(root, "docs", "guides", "a.md"), "x");
|
|
150
|
+
sfs.writeFileSync(path.join(root, "docs", "rfcs", "rfc-1.md"), "x");
|
|
151
|
+
sfs.writeFileSync(path.join(root, "CHANGELOG.md"), "x");
|
|
152
|
+
const rel = collectSkillDocFiles(root)
|
|
153
|
+
.map((f) => path.relative(root, f))
|
|
154
|
+
.sort();
|
|
155
|
+
expect(rel).toEqual(["AGENTS.md", "README.md", "README.uk.md", "docs/guides/a.md"]);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("runSkillDocSync fails on drift and --write heals marked regions", async () => {
|
|
159
|
+
const root = sfs.mkdtempSync(path.join(os.tmpdir(), "skill-doc-sync-"));
|
|
160
|
+
const readme = path.join(root, "README.md");
|
|
161
|
+
sfs.writeFileSync(
|
|
162
|
+
readme,
|
|
163
|
+
`<!-- ${SKILL_DOC_MARKERS.count} -->STALE<!-- /${SKILL_DOC_MARKERS.count} -->\n`,
|
|
164
|
+
);
|
|
165
|
+
const check = await runSkillDocSync({ flags: {} }, { workspaceRoot: root });
|
|
166
|
+
expect(check.status).toBe("fail");
|
|
167
|
+
expect(check.drift[0]!.kind).toBe("marked-region");
|
|
168
|
+
|
|
169
|
+
// --write re-renders; a re-check passes with the REAL registry rendered.
|
|
170
|
+
const heal = await runSkillDocSync({ flags: { write: true } }, { workspaceRoot: root });
|
|
171
|
+
expect(heal.written).toEqual(["README.md"]);
|
|
172
|
+
expect(sfs.readFileSync(readme, "utf8")).toContain(renderCountLiteral());
|
|
173
|
+
expect((await runSkillDocSync({ flags: {} }, { workspaceRoot: root })).status).toBe("pass");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("lintSkillDocFiles is the shared per-package composition entry", async () => {
|
|
177
|
+
const root = sfs.mkdtempSync(path.join(os.tmpdir(), "skill-doc-sync-"));
|
|
178
|
+
const f = path.join(root, "README.md");
|
|
179
|
+
sfs.writeFileSync(f, "We ship 41 skills.");
|
|
180
|
+
const outcome = await lintSkillDocFiles(root, [f], SKILLS);
|
|
181
|
+
expect(outcome.drift).toHaveLength(1);
|
|
182
|
+
expect(outcome.drift[0]!.file).toBe("README.md");
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("write-path edge cases (defect REVIEW-1585-F1/F3/F5)", () => {
|
|
187
|
+
test("adjacent empty marker pair heals without deleting the open marker (F-1)", () => {
|
|
188
|
+
const doc = `<!-- ${SKILL_DOC_MARKERS.count} --><!-- /${SKILL_DOC_MARKERS.count} -->\n`;
|
|
189
|
+
const out = lintSkillDocContent("README.md", doc, SKILLS, true).rewritten!;
|
|
190
|
+
expect(out).toContain(`<!-- ${SKILL_DOC_MARKERS.count} -->`);
|
|
191
|
+
expect(out).toContain(`<!-- /${SKILL_DOC_MARKERS.count} -->`);
|
|
192
|
+
expect(out).toContain(renderCountLiteral(SKILLS));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("a file starting with a fence gains no phantom line on --write (F-3)", () => {
|
|
196
|
+
const doc = "```\ncode block\n```\n\nText.\n";
|
|
197
|
+
const out = lintSkillDocContent("README.md", doc, SKILLS, true).rewritten!;
|
|
198
|
+
expect(out).toBe(doc);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("claim line numbers after a multi-line region stay true (F-2)", () => {
|
|
202
|
+
const doc = `Intro.\n\n<!-- ${SKILL_DOC_MARKERS.inventory} -->\nold\nold\nold\n<!-- /${SKILL_DOC_MARKERS.inventory} -->\n\nWe ship 99 skills.\n`;
|
|
203
|
+
const drift = lintSkillDocContent("README.md", doc, SKILLS).drift;
|
|
204
|
+
const claim = drift.find((d) => d.kind === "claim-total")!;
|
|
205
|
+
expect(claim.line).toBe(9);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("--write exits pass even when regions were healed (F-5)", async () => {
|
|
209
|
+
const root = sfs.mkdtempSync(path.join(os.tmpdir(), "skill-doc-sync-"));
|
|
210
|
+
sfs.writeFileSync(
|
|
211
|
+
path.join(root, "README.md"),
|
|
212
|
+
`<!-- ${SKILL_DOC_MARKERS.count} -->STALE<!-- /${SKILL_DOC_MARKERS.count} -->\n`,
|
|
213
|
+
);
|
|
214
|
+
const heal = await runSkillDocSync({ flags: { write: true } }, { workspaceRoot: root });
|
|
215
|
+
expect(heal.status).toBe("pass");
|
|
216
|
+
expect(heal.healedRegions).toHaveLength(1);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("--check is the explicit form of the default", async () => {
|
|
220
|
+
const root = sfs.mkdtempSync(path.join(os.tmpdir(), "skill-doc-sync-"));
|
|
221
|
+
sfs.writeFileSync(path.join(root, "README.md"), "We ship 99 skills.\n");
|
|
222
|
+
expect(
|
|
223
|
+
(await runSkillDocSync({ flags: { check: true } }, { workspaceRoot: root })).status,
|
|
224
|
+
).toBe("fail");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("scaffold hydration (requirement AC-5)", () => {
|
|
229
|
+
test("empty marker pairs in templates render at create time", () => {
|
|
230
|
+
const tpl = `- Skills: <!-- ${SKILL_DOC_MARKERS.count} --><!-- /${SKILL_DOC_MARKERS.count} -->\n`;
|
|
231
|
+
expect(hydrateSkillDocMarkers(tpl, SKILLS)).toContain(renderCountLiteral(SKILLS));
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
RFC-1585: derive skill counts/inventory in docs from FORGE_SKILLS instead of
|
|
5
|
+
hand-maintained literals. `--write` re-renders marked regions
|
|
6
|
+
(`<!-- forge:skills-count -->` / `<!-- forge:skills-inventory -->`) in place;
|
|
7
|
+
`--check` (default) fails on marked-region drift and on skill-count claims
|
|
8
|
+
outside markers that contradict the registry.
|
|
9
|
+
</purpose>
|
|
10
|
+
<non-goals>
|
|
11
|
+
<item>Does not rewrite prose outside marked regions — claim drift is reported, never auto-fixed.</item>
|
|
12
|
+
<item>Does not count project skill packs (RFC-0539) — the rendered registry is FORGE_SKILLS; pack-specific count claims carry an exemption marker.</item>
|
|
13
|
+
</non-goals>
|
|
14
|
+
<KEY_DECISIONS>
|
|
15
|
+
<item>Paired open/close HTML markers (forge:custom discipline, RFC-1153) — content between markers is generated, everything outside is authored.</item>
|
|
16
|
+
<item>Claim grammar is intentionally narrow (three literal forms) — ambiguous prose is never flagged; authors exempt intentional claims with a same-line `<!-- forge-skill-count:ok: <reason> -->` marker.</item>
|
|
17
|
+
<item>Historical record trees (rfcs/adrs/audits/reviews/plans/queues, changelogs, generated docs) are excluded from the claim lint — dated statements quote the registry of their day.</item>
|
|
18
|
+
</KEY_DECISIONS>
|
|
19
|
+
<CHANGE_SUMMARY>
|
|
20
|
+
<item>RFC-1585: initial implementation — markers, render, drift check, claim lint, doc surface discovery.</item>
|
|
21
|
+
<item>RFC-1585: review wave — write-path corruptions, --check flag, scaffold hydration
|
|
22
|
+
|
|
23
|
+
Independent fo-review (isolation: subagent) found: empty marker pairs lost
|
|
24
|
+
their open tag on --write, multi-line regions collapsed line numbers, a
|
|
25
|
+
leading fence injected a phantom line, --check was undocumented-undeclared,
|
|
26
|
+
and --write exited 1 on a successful heal. Fixes: open-marker capture,
|
|
27
|
+
newline-preserving placeholder, empty-chunk skip, check flag, healedRegions
|
|
28
|
+
split (write always exits 0), dryRun/io threading, byte-exact inventory
|
|
29
|
+
compare on the prettier-stable canonical form, CHECK-13 delegated through
|
|
30
|
+
commandRegistry/invoke so the registry-integrity scan stays within its
|
|
31
|
+
slice cap (KNOWN_TRUNCATED_SCANS is shrink-only), record-tree exclusions
|
|
32
|
+
extended, two real stale claims corrected, profile templates ship empty
|
|
33
|
+
markers hydrated at scaffold write time. RFC amended to implemented
|
|
34
|
+
grammar/result/exclusions; command moved to commands.added.</item>
|
|
35
|
+
</CHANGE_SUMMARY>
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import path from "node:path";
|
|
39
|
+
import * as sfs from "../utils/sync-fs.ts";
|
|
40
|
+
import { writeFileIfChanged } from "../utils/fs-idempotent.ts";
|
|
41
|
+
import { FORGE_SKILLS } from "../registry.ts";
|
|
42
|
+
import type { ForgeSkillEntry } from "../registry.ts";
|
|
43
|
+
import type { WorkspaceIO } from "../types.ts";
|
|
44
|
+
|
|
45
|
+
// ─── Markers ────────────────────────────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
export const SKILL_DOC_MARKERS = {
|
|
48
|
+
inventory: "forge:skills-inventory",
|
|
49
|
+
count: "forge:skills-count",
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
export type SkillDocMarkerKind = keyof typeof SKILL_DOC_MARKERS;
|
|
53
|
+
|
|
54
|
+
/** `<!-- forge-skill-count:ok: <reason> -->` exempts one line from the claim lint. */
|
|
55
|
+
export const SKILL_CLAIM_EXEMPT_RE = /forge-skill-count:ok(?:\s*:\s*[^\n]*?)?\s*(?:-->\s*)?$/;
|
|
56
|
+
|
|
57
|
+
/** Paired `<!-- forge:X -->…<!-- /forge:X -->` region. */
|
|
58
|
+
const REGION_RE =
|
|
59
|
+
/<!--\s*forge:(skills-inventory|skills-count)\s*-->([\s\S]*?)<!--\s*\/forge:\1\s*-->/g;
|
|
60
|
+
|
|
61
|
+
// ─── Registry projection ────────────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
export interface SkillCounts {
|
|
64
|
+
total: number;
|
|
65
|
+
fo: number;
|
|
66
|
+
shared: number;
|
|
67
|
+
meta: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function skillCounts(skills: ForgeSkillEntry[] = FORGE_SKILLS): SkillCounts {
|
|
71
|
+
const by = (category: ForgeSkillEntry["category"]) =>
|
|
72
|
+
skills.filter((s) => s.category === category).length;
|
|
73
|
+
return { total: skills.length, fo: by("fo"), shared: by("shared"), meta: by("meta") };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Canonical one-line literal used by the `skills-count` marker and the claim lint. */
|
|
77
|
+
export function renderCountLiteral(skills: ForgeSkillEntry[] = FORGE_SKILLS): string {
|
|
78
|
+
const c = skillCounts(skills);
|
|
79
|
+
return `${c.total} skills (${c.fo} fo + ${c.shared} shared + ${c.meta} meta)`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Canonical inventory block for the `skills-inventory` marker. */
|
|
83
|
+
export function renderInventoryBlock(skills: ForgeSkillEntry[] = FORGE_SKILLS): string {
|
|
84
|
+
const c = skillCounts(skills);
|
|
85
|
+
const names = (category: ForgeSkillEntry["category"]) =>
|
|
86
|
+
skills
|
|
87
|
+
.filter((s) => s.category === category)
|
|
88
|
+
.map((s) => s.name)
|
|
89
|
+
.sort()
|
|
90
|
+
.map((n) => `\`${n}\``)
|
|
91
|
+
.join(", ");
|
|
92
|
+
return [
|
|
93
|
+
`**${c.total} skills** — ${c.fo} fo, ${c.shared} shared, ${c.meta} meta.`,
|
|
94
|
+
``,
|
|
95
|
+
`- **fo (${c.fo}):** ${names("fo")}`,
|
|
96
|
+
`- **shared (${c.shared}):** ${names("shared")}`,
|
|
97
|
+
`- **meta (${c.meta}):** ${names("meta")}`,
|
|
98
|
+
].join("\n");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function renderRegion(kind: SkillDocMarkerKind, skills: ForgeSkillEntry[]): string {
|
|
102
|
+
return kind === "count" ? renderCountLiteral(skills) : renderInventoryBlock(skills);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ─── Drift model ────────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
export interface SkillDocDrift {
|
|
108
|
+
file: string;
|
|
109
|
+
line: number;
|
|
110
|
+
kind:
|
|
111
|
+
"marked-region" | "claim-total" | "claim-decomposition" | "claim-category" | "inline-inventory";
|
|
112
|
+
expected: string;
|
|
113
|
+
found: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface SkillDocSyncResult {
|
|
117
|
+
command: "forge.skill.doc-sync";
|
|
118
|
+
status: "pass" | "fail";
|
|
119
|
+
filesScanned: number;
|
|
120
|
+
markedRegions: number;
|
|
121
|
+
/** Residual drift — unhealed claims in check mode, all drift in write mode. */
|
|
122
|
+
drift: SkillDocDrift[];
|
|
123
|
+
/** Marked regions healed by --write. */
|
|
124
|
+
healedRegions: SkillDocDrift[];
|
|
125
|
+
written: string[];
|
|
126
|
+
dryRun: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ─── Claim grammar (unmarked regions only) ──────────────────────────────────
|
|
130
|
+
|
|
131
|
+
/** `34 fo + 4 shared + 3 meta` — ordered decomposition, all three required. */
|
|
132
|
+
const CLAIM_DECOMPOSITION_RE = /(\d+)\s*fo\D{0,10}?\+\s*(\d+)\s*shared\D{0,10}?\+\s*(\d+)\s*meta/g;
|
|
133
|
+
/** `41 skills`, `41 skill definitions`, `41 built-in skills`, `41 forge skills`. */
|
|
134
|
+
const CLAIM_TOTAL_RE =
|
|
135
|
+
/\b(\d+)\s+(?:built-in\s+|forge(?:-managed)?\s+)?skill\s*(?:definitions|s)?\b/gi;
|
|
136
|
+
/** `34 fo skills`, `4 shared skills`, `3 meta skills`. */
|
|
137
|
+
const CLAIM_CATEGORY_RE = /\b(\d+)\s+(fo|shared|meta)\s+skills?\b/g;
|
|
138
|
+
|
|
139
|
+
const FENCE_RE = /^```/;
|
|
140
|
+
|
|
141
|
+
interface LintOutcome {
|
|
142
|
+
drift: SkillDocDrift[];
|
|
143
|
+
markedRegions: number;
|
|
144
|
+
/** Marked regions --write just healed (not residual drift). */
|
|
145
|
+
healedRegions: SkillDocDrift[];
|
|
146
|
+
rewritten?: string;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Strip marked regions and fenced blocks, then lint remaining lines for stale skill-count claims. */
|
|
150
|
+
export function lintSkillDocContent(
|
|
151
|
+
relFile: string,
|
|
152
|
+
content: string,
|
|
153
|
+
skills: ForgeSkillEntry[] = FORGE_SKILLS,
|
|
154
|
+
write = false,
|
|
155
|
+
): LintOutcome {
|
|
156
|
+
const counts = skillCounts(skills);
|
|
157
|
+
const drift: SkillDocDrift[] = [];
|
|
158
|
+
let markedRegions = 0;
|
|
159
|
+
let healedRegions: SkillDocDrift[] = [];
|
|
160
|
+
|
|
161
|
+
// 1. Marked regions — compare or rewrite. Fenced code blocks are doc
|
|
162
|
+
// examples, never live regions — regions are scanned per unfenced chunk.
|
|
163
|
+
const chunks: { text: string; fenced: boolean }[] = [];
|
|
164
|
+
{
|
|
165
|
+
let fenced = false;
|
|
166
|
+
let buf: string[] = [];
|
|
167
|
+
const flush = (): void => {
|
|
168
|
+
if (buf.length > 0) chunks.push({ text: buf.join("\n"), fenced });
|
|
169
|
+
buf = [];
|
|
170
|
+
};
|
|
171
|
+
for (const l of content.split("\n")) {
|
|
172
|
+
if (FENCE_RE.test(l.trimStart())) {
|
|
173
|
+
flush();
|
|
174
|
+
buf.push(l);
|
|
175
|
+
fenced = !fenced;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
buf.push(l);
|
|
179
|
+
}
|
|
180
|
+
flush();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const healed: SkillDocDrift[] = [];
|
|
184
|
+
const processRegion = (
|
|
185
|
+
whole: string,
|
|
186
|
+
kind: string,
|
|
187
|
+
inner: string,
|
|
188
|
+
offset: number,
|
|
189
|
+
chunkText: string,
|
|
190
|
+
lineBase: number,
|
|
191
|
+
): string => {
|
|
192
|
+
markedRegions++;
|
|
193
|
+
const line = lineBase + chunkText.slice(0, offset).split("\n").length - 1;
|
|
194
|
+
const marker = kind === "skills-inventory" ? "inventory" : "count";
|
|
195
|
+
const rendered = renderRegion(marker as SkillDocMarkerKind, skills);
|
|
196
|
+
if (marker === "inventory" && !whole.includes("\n")) {
|
|
197
|
+
// Inline inventory marker would corrupt layout — flag, never rewrite.
|
|
198
|
+
drift.push({
|
|
199
|
+
file: relFile,
|
|
200
|
+
line,
|
|
201
|
+
kind: "inline-inventory",
|
|
202
|
+
expected: "inventory markers on their own lines",
|
|
203
|
+
found: whole.slice(0, 80),
|
|
204
|
+
});
|
|
205
|
+
return whole;
|
|
206
|
+
}
|
|
207
|
+
// Inventory's canonical inner keeps the blank lines prettier inserts
|
|
208
|
+
// around HTML comment blocks — byte-exact AND format-stable.
|
|
209
|
+
const expectedInner = marker === "count" ? rendered : `\n\n${rendered}\n\n`;
|
|
210
|
+
// Count regions tolerate padding (prettier aligns table cells); inventory
|
|
211
|
+
// compares byte-exact — whitespace drift inside a generated block is real drift.
|
|
212
|
+
const stale = marker === "count" ? inner.trim() !== rendered : inner !== expectedInner;
|
|
213
|
+
if (stale) {
|
|
214
|
+
const entry: SkillDocDrift = {
|
|
215
|
+
file: relFile,
|
|
216
|
+
line,
|
|
217
|
+
kind: "marked-region",
|
|
218
|
+
expected: rendered.split("\n")[0]!,
|
|
219
|
+
found: inner.trim().split("\n")[0]!.slice(0, 80) || "(empty)",
|
|
220
|
+
};
|
|
221
|
+
(write ? healed : drift).push(entry);
|
|
222
|
+
}
|
|
223
|
+
if (!write || !stale) return whole;
|
|
224
|
+
const openEnd = whole.indexOf("-->");
|
|
225
|
+
const open = openEnd === -1 ? whole : whole.slice(0, openEnd + 3);
|
|
226
|
+
const close = `<!-- /forge:${kind} -->`;
|
|
227
|
+
return `${open}${expectedInner}${close}`;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const rewrittenParts: string[] = [];
|
|
231
|
+
let lineBase = 1;
|
|
232
|
+
for (const chunk of chunks) {
|
|
233
|
+
if (chunk.fenced) {
|
|
234
|
+
rewrittenParts.push(chunk.text);
|
|
235
|
+
} else {
|
|
236
|
+
rewrittenParts.push(
|
|
237
|
+
chunk.text.replace(REGION_RE, (whole, kind, inner, offset) =>
|
|
238
|
+
processRegion(whole, kind, inner, offset, chunk.text, lineBase),
|
|
239
|
+
),
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
lineBase += chunk.text.split("\n").length;
|
|
243
|
+
}
|
|
244
|
+
const rewritten = rewrittenParts.join("\n");
|
|
245
|
+
healedRegions = healed;
|
|
246
|
+
|
|
247
|
+
// 2. Claim lint — only outside marked regions and fenced blocks. Regions
|
|
248
|
+
// collapse to whitespace that preserves their newline count — a multi-line
|
|
249
|
+
// region must not shift the line numbers of claims that follow it.
|
|
250
|
+
const body = (write ? rewritten : content)
|
|
251
|
+
.replace(REGION_RE, (m) => m.replace(/[^\n]/g, " "))
|
|
252
|
+
.split("\n")
|
|
253
|
+
.map((l, i) => ({ line: i + 1, text: l }));
|
|
254
|
+
let inFence = false;
|
|
255
|
+
for (const { line, text } of body) {
|
|
256
|
+
if (FENCE_RE.test(text.trimStart())) {
|
|
257
|
+
inFence = !inFence;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (inFence || SKILL_CLAIM_EXEMPT_RE.test(text)) continue;
|
|
261
|
+
|
|
262
|
+
for (const m of text.matchAll(CLAIM_DECOMPOSITION_RE)) {
|
|
263
|
+
const [fo, shared, meta] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
264
|
+
if (fo !== counts.fo || shared !== counts.shared || meta !== counts.meta) {
|
|
265
|
+
drift.push({
|
|
266
|
+
file: relFile,
|
|
267
|
+
line,
|
|
268
|
+
kind: "claim-decomposition",
|
|
269
|
+
expected: `${counts.fo} fo + ${counts.shared} shared + ${counts.meta} meta`,
|
|
270
|
+
found: m[0].trim(),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
for (const m of text.matchAll(CLAIM_CATEGORY_RE)) {
|
|
275
|
+
const cat = m[2] as "fo" | "shared" | "meta";
|
|
276
|
+
if (Number(m[1]) !== counts[cat]) {
|
|
277
|
+
drift.push({
|
|
278
|
+
file: relFile,
|
|
279
|
+
line,
|
|
280
|
+
kind: "claim-category",
|
|
281
|
+
expected: `${counts[cat]} ${cat} skills`,
|
|
282
|
+
found: m[0].trim(),
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
for (const m of text.matchAll(CLAIM_TOTAL_RE)) {
|
|
287
|
+
if (Number(m[1]) !== counts.total) {
|
|
288
|
+
drift.push({
|
|
289
|
+
file: relFile,
|
|
290
|
+
line,
|
|
291
|
+
kind: "claim-total",
|
|
292
|
+
expected: `${counts.total} skills`,
|
|
293
|
+
found: m[0].trim(),
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return { drift, markedRegions, healedRegions, rewritten: write ? rewritten : undefined };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ─── Doc surface ────────────────────────────────────────────────────────────
|
|
303
|
+
|
|
304
|
+
/** Historical record trees quote the registry of their day — never claim-linted. */
|
|
305
|
+
export const SKILL_DOC_SURFACE_EXCLUDE_RES = [
|
|
306
|
+
/(^|\/)CHANGELOG\.md$/,
|
|
307
|
+
/\.generated\.md$/,
|
|
308
|
+
/(^|\/)docs\/(rfcs|adrs|audits|reviews|plans|queues|archive|sessions|specs|historical|summits|metrics|explorations|evidence|outbox|frictions|decisions)\//,
|
|
309
|
+
/(^|\/)lagebild-archive\//,
|
|
310
|
+
];
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Collect the claim-lint surface under a package root: README*.md, AGENTS.md,
|
|
314
|
+
* docs/**. Intentionally a different surface than package-health's
|
|
315
|
+
* DOCS_SURFACE — it adds README locale variants and drops the record trees
|
|
316
|
+
* (rfcs/audits/…) DOCS_SURFACE includes, since dated records quote old counts.
|
|
317
|
+
*/
|
|
318
|
+
export function collectSkillDocFiles(root: string): string[] {
|
|
319
|
+
const files: string[] = [];
|
|
320
|
+
const walk = (dir: string): void => {
|
|
321
|
+
let entries: sfs.Dirent[];
|
|
322
|
+
try {
|
|
323
|
+
entries = sfs.readdirSync(dir, { withFileTypes: true });
|
|
324
|
+
} catch {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
for (const e of entries) {
|
|
328
|
+
const abs = path.join(dir, e.name);
|
|
329
|
+
if (e.isDirectory()) walk(abs);
|
|
330
|
+
else if (e.isFile() && e.name.endsWith(".md")) files.push(abs);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
const docsDir = path.join(root, "docs");
|
|
334
|
+
if (sfs.existsSync(docsDir)) walk(docsDir);
|
|
335
|
+
for (const name of sfs.existsSync(root) ? sfs.readdirSync(root) : []) {
|
|
336
|
+
if (/^README.*\.md$/.test(name) || name === "AGENTS.md") files.push(path.join(root, name));
|
|
337
|
+
}
|
|
338
|
+
return files
|
|
339
|
+
.filter((f) => {
|
|
340
|
+
const rel = path.relative(root, f).split(path.sep).join("/");
|
|
341
|
+
return !SKILL_DOC_SURFACE_EXCLUDE_RES.some((re) => re.test(rel));
|
|
342
|
+
})
|
|
343
|
+
.sort();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Lint a concrete file list — shared by the command and the package-health composition. */
|
|
347
|
+
export async function lintSkillDocFiles(
|
|
348
|
+
root: string,
|
|
349
|
+
files: string[],
|
|
350
|
+
skills: ForgeSkillEntry[] = FORGE_SKILLS,
|
|
351
|
+
write = false,
|
|
352
|
+
opts: { io?: WorkspaceIO; dryRun?: boolean } = {},
|
|
353
|
+
): Promise<{
|
|
354
|
+
drift: SkillDocDrift[];
|
|
355
|
+
healedRegions: SkillDocDrift[];
|
|
356
|
+
markedRegions: number;
|
|
357
|
+
written: string[];
|
|
358
|
+
}> {
|
|
359
|
+
const drift: SkillDocDrift[] = [];
|
|
360
|
+
const healedRegions: SkillDocDrift[] = [];
|
|
361
|
+
const written: string[] = [];
|
|
362
|
+
let markedRegions = 0;
|
|
363
|
+
for (const abs of files) {
|
|
364
|
+
let content: string;
|
|
365
|
+
try {
|
|
366
|
+
content = sfs.readFileSync(abs, "utf8");
|
|
367
|
+
} catch {
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const rel = path.relative(root, abs).split(path.sep).join("/");
|
|
371
|
+
const outcome = lintSkillDocContent(rel, content, skills, write);
|
|
372
|
+
drift.push(...outcome.drift);
|
|
373
|
+
healedRegions.push(...outcome.healedRegions);
|
|
374
|
+
markedRegions += outcome.markedRegions;
|
|
375
|
+
if (write && outcome.rewritten !== undefined && outcome.rewritten !== content) {
|
|
376
|
+
if (!opts.dryRun) await writeFileIfChanged(abs, outcome.rewritten, opts.io);
|
|
377
|
+
written.push(rel);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return { drift, healedRegions, markedRegions, written };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Fill marked regions in scaffolded content at create time — the literals are
|
|
385
|
+
* rendered from the registry of the forge doing the scaffolding, so profile
|
|
386
|
+
* templates never carry a hand-bumped count (RFC-1585, review F-12).
|
|
387
|
+
*/
|
|
388
|
+
export function hydrateSkillDocMarkers(
|
|
389
|
+
content: string,
|
|
390
|
+
skills: ForgeSkillEntry[] = FORGE_SKILLS,
|
|
391
|
+
): string {
|
|
392
|
+
return content.replace(REGION_RE, (_whole, kind: string) => {
|
|
393
|
+
const marker = kind === "skills-inventory" ? "inventory" : "count";
|
|
394
|
+
const rendered = renderRegion(marker as SkillDocMarkerKind, skills);
|
|
395
|
+
const inner = marker === "count" ? rendered : `\n\n${rendered}\n\n`;
|
|
396
|
+
return `<!-- forge:${kind} -->${inner}<!-- /forge:${kind} -->`;
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ─── Command runner ─────────────────────────────────────────────────────────
|
|
401
|
+
|
|
402
|
+
interface SkillDocSyncFlags {
|
|
403
|
+
write?: boolean;
|
|
404
|
+
check?: boolean;
|
|
405
|
+
files?: string;
|
|
406
|
+
root?: string;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export async function runSkillDocSync(
|
|
410
|
+
input: unknown,
|
|
411
|
+
context: unknown,
|
|
412
|
+
): Promise<SkillDocSyncResult> {
|
|
413
|
+
const flags =
|
|
414
|
+
(input as { flags?: SkillDocSyncFlags } | undefined)?.flags ??
|
|
415
|
+
(input as SkillDocSyncFlags) ??
|
|
416
|
+
{};
|
|
417
|
+
const ctx = (context ?? {}) as {
|
|
418
|
+
workspaceRoot?: string;
|
|
419
|
+
io?: WorkspaceIO;
|
|
420
|
+
dryRun?: boolean;
|
|
421
|
+
};
|
|
422
|
+
const root = flags.root
|
|
423
|
+
? path.resolve(ctx.workspaceRoot ?? process.cwd(), flags.root)
|
|
424
|
+
: (ctx.workspaceRoot ?? process.cwd());
|
|
425
|
+
const files = flags.files
|
|
426
|
+
? flags.files.split(",").map((f) => path.resolve(root, f.trim()))
|
|
427
|
+
: collectSkillDocFiles(root);
|
|
428
|
+
// --check is the explicit form of the default; --write renders and always
|
|
429
|
+
// exits 0 (claim drift it cannot heal is reported, never auto-fixed).
|
|
430
|
+
const write = flags.write === true && flags.check !== true;
|
|
431
|
+
|
|
432
|
+
const { drift, healedRegions, markedRegions, written } = await lintSkillDocFiles(
|
|
433
|
+
root,
|
|
434
|
+
files,
|
|
435
|
+
FORGE_SKILLS,
|
|
436
|
+
write,
|
|
437
|
+
{ io: ctx.io, dryRun: ctx.dryRun === true },
|
|
438
|
+
);
|
|
439
|
+
return {
|
|
440
|
+
command: "forge.skill.doc-sync",
|
|
441
|
+
status: !write && drift.length > 0 ? "fail" : "pass",
|
|
442
|
+
filesScanned: files.length,
|
|
443
|
+
markedRegions,
|
|
444
|
+
drift,
|
|
445
|
+
healedRegions,
|
|
446
|
+
written,
|
|
447
|
+
dryRun: ctx.dryRun === true,
|
|
448
|
+
};
|
|
449
|
+
}
|