simt-dev 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +239 -0
- package/bin/simt-dev.mjs +21 -2
- package/docs/decisions/ADR-064-the-context-meter-counts-what-the-tool-wrote.md +107 -0
- package/docs/decisions/ADR-065-a-pack-is-one-shared-rule-set-pinned-by-digest.md +97 -0
- package/docs/decisions/ADR-066-a-monorepo-projects-once-per-package.md +115 -0
- package/docs/decisions/ADR-067-which-harnesses-read-agents-md-is-a-column.md +110 -0
- package/docs/decisions/ADR-068-the-mcp-revision-list-cites-what-was-read.md +111 -0
- package/docs/decisions/ADR-069-the-conformance-lint-checks-the-published-specification.md +90 -0
- package/docs/decisions/ADR-070-the-json-payload-carries-its-own-version.md +91 -0
- package/docs/decisions/ADR-071-a-rule-that-can-be-narrowed-is-written-narrow.md +106 -0
- package/docs/decisions/ADR-072-the-budget-is-a-declaration-beside-the-gates.md +104 -0
- package/docs/decisions/ADR-073-a-kernel-is-a-smaller-projection-the-repository-asks-for.md +92 -0
- package/docs/decisions/ADR-074-a-rule-and-a-decision-are-readable-by-uri.md +99 -0
- package/docs/decisions/ADR-075-nothing-leaves-the-machine.md +56 -0
- package/docs/decisions/ADR-076-a-change-to-what-agents-read-is-measured.md +70 -0
- package/docs/decisions/ADR-077-the-command-card-leads-the-region-and-reads-committed-evidence.md +91 -0
- package/docs/decisions/ADR-078-a-line-the-repository-already-proves-does-not-ship.md +117 -0
- package/docs/decisions/ADR-079-the-rules-are-enforced-at-the-tool-call-from-one-policy-table.md +162 -0
- package/docs/decisions/ADR-080-the-tools-own-names-are-not-repositories-and-neither-reader-decides-by-shape.md +99 -0
- package/docs/decisions/ADR-081-a-tracker-is-read-through-the-repositorys-template.md +36 -0
- package/docs/decisions/ADR-082-the-tracker-override-is-a-manifest-key.md +35 -0
- package/package.json +2 -1
- package/rules/core.md +1 -0
- package/rules/stacks/android.md +2 -0
- package/rules/stacks/cloudflare-workers.md +2 -0
- package/rules/stacks/connectiq.md +1 -0
- package/rules/stacks/container.md +2 -0
- package/rules/stacks/dart.md +2 -0
- package/rules/stacks/deno.md +2 -0
- package/rules/stacks/dotnet.md +2 -0
- package/rules/stacks/elixir.md +2 -0
- package/rules/stacks/go.md +2 -0
- package/rules/stacks/jvm.md +2 -0
- package/rules/stacks/node.md +2 -0
- package/rules/stacks/python.md +2 -0
- package/rules/stacks/react-native.md +2 -0
- package/rules/stacks/react-web.md +2 -0
- package/rules/stacks/ruby.md +2 -0
- package/rules/stacks/rust.md +2 -0
- package/rules/stacks/swift.md +2 -0
- package/rules/stacks/typescript.md +2 -0
- package/rules/stacks/uv.md +2 -0
- package/src/agenthooks/decide.mjs +123 -0
- package/src/agenthooks/policy.mjs +78 -0
- package/src/agenthooks/render.mjs +240 -0
- package/src/argv.mjs +55 -1
- package/src/bench/ablate.mjs +630 -0
- package/src/bench/adapters.json +149 -0
- package/src/bench/grade.mjs +229 -0
- package/src/bench/mine.mjs +811 -0
- package/src/bench/probes.mjs +1350 -0
- package/src/bench/report.mjs +899 -0
- package/src/bench/run.mjs +1002 -0
- package/src/commands/agenthook.mjs +282 -0
- package/src/commands/bench.mjs +359 -0
- package/src/commands/compare.mjs +15 -0
- package/src/commands/graph.mjs +45 -21
- package/src/commands/harness.mjs +301 -0
- package/src/commands/help.mjs +73 -2
- package/src/commands/index.mjs +67 -25
- package/src/commands/init.mjs +12 -8
- package/src/commands/mcp.mjs +20 -5
- package/src/commands/plan.mjs +99 -0
- package/src/commands/rules.mjs +42 -57
- package/src/commands/skills.mjs +67 -92
- package/src/commands/skillsinstall.mjs +17 -2
- package/src/commands/sync.mjs +62 -6
- package/src/commands/tokens.mjs +169 -0
- package/src/compare/row.mjs +73 -2
- package/src/decisions/index.mjs +23 -5
- package/src/graph/spec.mjs +227 -0
- package/src/graph/speccheck.mjs +68 -0
- package/src/hooks/dispatcher.mjs +35 -3
- package/src/kernel/card.mjs +254 -0
- package/src/kernel/inferability.mjs +188 -0
- package/src/mcp/resources.mjs +157 -0
- package/src/mcp/schema.mjs +139 -0
- package/src/mcp/server.mjs +228 -20
- package/src/mcp/tools.mjs +372 -1
- package/src/measure/context.mjs +542 -0
- package/src/measure/loaderView.mjs +435 -0
- package/src/measure/session.mjs +267 -0
- package/src/monorepo.mjs +339 -0
- package/src/output/index.mjs +78 -2
- package/src/packs.mjs +182 -0
- package/src/plan/index.mjs +650 -0
- package/src/plan/template.mjs +331 -0
- package/src/projection/loaders.mjs +689 -0
- package/src/projection/manifest.mjs +114 -3
- package/src/projection/project.mjs +140 -9
- package/src/projection/rules.mjs +365 -22
- package/src/projection/surfaces.mjs +155 -23
- package/src/projection/targets.mjs +37 -0
- package/src/toolwords.mjs +69 -0
- package/templates/plan/tracker.json +36 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// The inferability filter: a rule line the repository already proves does not ship
|
|
2
|
+
// (plan 0003 R09).
|
|
3
|
+
//
|
|
4
|
+
// The costliest byte in a projection is the one the reader already knows. A Go repository
|
|
5
|
+
// with one runner does not need to be told to use it; a repository whose manifest declares
|
|
6
|
+
// a format gate does not need to be told how it indents. The evidence is committed — the
|
|
7
|
+
// marker files, the declared gates, the command card — so the same repository filters the
|
|
8
|
+
// same way on every machine (plan 0003 P4, R13).
|
|
9
|
+
//
|
|
10
|
+
// Nothing is dropped by guesswork. A rule author marks the line with
|
|
11
|
+
// `<!-- simt-inferable-when: <expression> -->`, naming the evidence that makes it
|
|
12
|
+
// redundant, and the line leaves only when the repository proves every term. A line nobody
|
|
13
|
+
// marked is a line nobody claimed was inferable, and stays.
|
|
14
|
+
//
|
|
15
|
+
// The unit dropped is the blank-line-delimited block, so a wrapped bullet is marked once
|
|
16
|
+
// and leaves whole rather than leaving half a sentence behind. The marker is stripped from
|
|
17
|
+
// the region either way: `simt-` vocabulary is for the rule's author, not its reader.
|
|
18
|
+
|
|
19
|
+
import { gateNames } from '../hooks/gates.mjs';
|
|
20
|
+
import { KNOWN_TAGS } from '../resolve/fingerprint.mjs';
|
|
21
|
+
|
|
22
|
+
import { CARD_VERBS } from './card.mjs';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The marker a rule line carries. Named here rather than at its use, because the defect
|
|
26
|
+
* message has to spell the thing the author must type.
|
|
27
|
+
*/
|
|
28
|
+
export const INFERABLE_MARKER = 'simt-inferable-when';
|
|
29
|
+
|
|
30
|
+
const MARKER = /<!--\s*simt-inferable-when:\s*([^>]*?)\s*-->/g;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Marks the trailing position of a marker on a line, so the text a reader keeps is the
|
|
34
|
+
* line without the comment and not the line with a hole in it.
|
|
35
|
+
*/
|
|
36
|
+
const MARKER_AT = /\s*<!--\s*simt-inferable-when:\s*[^>]*?-->/g;
|
|
37
|
+
|
|
38
|
+
/** A non-global probe, because a global regex carries `lastIndex` between calls. */
|
|
39
|
+
const HAS_MARKER = /<!--\s*simt-inferable-when:/;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Split a body into the units a marker drops. Blank lines separate blocks, except inside a
|
|
43
|
+
* fenced code block, where a blank line is part of the sample and splitting there would
|
|
44
|
+
* cut a fence in half.
|
|
45
|
+
*/
|
|
46
|
+
function blocks(text) {
|
|
47
|
+
const out = [];
|
|
48
|
+
let current = [];
|
|
49
|
+
let fenced = false;
|
|
50
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
51
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
52
|
+
if (current.length > 0 || fenced) current.push(line);
|
|
53
|
+
fenced = !fenced;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (!fenced && line.trim() === '') {
|
|
57
|
+
if (current.length > 0) out.push(current.join('\n'));
|
|
58
|
+
current = [];
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
current.push(line);
|
|
62
|
+
}
|
|
63
|
+
if (current.length > 0) out.push(current.join('\n'));
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Every marker in one block, as `{line, expr}`. More than one is a defect. */
|
|
68
|
+
function markers(block) {
|
|
69
|
+
const found = [];
|
|
70
|
+
for (const line of block.split('\n')) {
|
|
71
|
+
for (const match of line.matchAll(MARKER)) found.push({ line, expr: match[1] });
|
|
72
|
+
}
|
|
73
|
+
return found;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* An expression is a conjunction of terms: `term + term`, and nothing else. There is no
|
|
78
|
+
* negation, because every reason a line is redundant is a positive proof the repository
|
|
79
|
+
* offers, and a grammar for a case nobody has is a grammar nobody can be taught.
|
|
80
|
+
*/
|
|
81
|
+
export function parseInferable(expression) {
|
|
82
|
+
return String(expression ?? '')
|
|
83
|
+
.split('+')
|
|
84
|
+
.map((term) => term.trim())
|
|
85
|
+
.filter((term) => term !== '');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether a term is evidence this tool can check: a fact or stack tag the repository
|
|
90
|
+
* proves by its files, a verb its command card names, or a gate it declares. Anything else
|
|
91
|
+
* is a typo the author has to see, not a term that happens to be unproven — a marker
|
|
92
|
+
* naming evidence the tool never gathered would drop the line on a claim nothing verified.
|
|
93
|
+
*/
|
|
94
|
+
function knownTerm(term) {
|
|
95
|
+
if (term.startsWith('card:')) return CARD_VERBS.includes(term.slice('card:'.length));
|
|
96
|
+
if (term.startsWith('gate:')) return gateNames().includes(term.slice('gate:'.length));
|
|
97
|
+
return KNOWN_TAGS.has(term);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The defect in a body's markers, or null. Reported through the same field as a
|
|
102
|
+
* frontmatter defect, so `check` and `rules list` need no second path to a broken rule.
|
|
103
|
+
*/
|
|
104
|
+
export function inferableDefect(body) {
|
|
105
|
+
for (const block of blocks(body)) {
|
|
106
|
+
const found = markers(block);
|
|
107
|
+
if (found.length === 0) continue;
|
|
108
|
+
if (found.length > 1) {
|
|
109
|
+
return `carries more than one ${INFERABLE_MARKER} marker; a block is dropped whole, so one marker is one claim`;
|
|
110
|
+
}
|
|
111
|
+
const terms = parseInferable(found[0].expr);
|
|
112
|
+
if (terms.length === 0) {
|
|
113
|
+
return `${INFERABLE_MARKER} names no evidence, so nothing can decide whether the line is redundant`;
|
|
114
|
+
}
|
|
115
|
+
for (const term of terms) {
|
|
116
|
+
if (!knownTerm(term)) {
|
|
117
|
+
return `${INFERABLE_MARKER} names "${term}", which is not evidence this repository can prove; use a marker-file fact, card:${CARD_VERBS.join('|card:')} or gate:<name>`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The evidence a projection may treat as already known. Built from committed bytes only —
|
|
126
|
+
* never PATH or `node_modules` — because a proof read off the machine would make the
|
|
127
|
+
* region differ between a laptop and CI for no reason a diff could explain.
|
|
128
|
+
*/
|
|
129
|
+
export function inferableProofs({ facts = [], stack = [], card = null, manifest = null } = {}) {
|
|
130
|
+
const proofs = new Set([...facts, ...stack]);
|
|
131
|
+
for (const line of card?.lines ?? []) {
|
|
132
|
+
if (line.verb) proofs.add(`card:${line.verb}`);
|
|
133
|
+
}
|
|
134
|
+
const gates = manifest?.gates ?? {};
|
|
135
|
+
for (const name of [...(gates.preCommit ?? []), ...(gates.prePush ?? [])]) {
|
|
136
|
+
proofs.add(`gate:${name}`);
|
|
137
|
+
}
|
|
138
|
+
return proofs;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The line a reader would have read, collapsed and clipped for a one-line note. */
|
|
142
|
+
function excerpt(text) {
|
|
143
|
+
const one = text.replace(/\s+/g, ' ').trim();
|
|
144
|
+
return one.length > 80 ? `${one.slice(0, 79)}…` : one;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The block without its markers, and without the line a marker stood on alone. */
|
|
148
|
+
function stripMarkers(block) {
|
|
149
|
+
const kept = [];
|
|
150
|
+
for (const line of block.split('\n')) {
|
|
151
|
+
const stripped = line.replace(MARKER_AT, '').replace(/\s+$/, '');
|
|
152
|
+
if (stripped === '' && HAS_MARKER.test(line)) continue;
|
|
153
|
+
kept.push(stripped);
|
|
154
|
+
}
|
|
155
|
+
return kept.join('\n');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Drop every marked block whose expression the repository proves, and strip the markers
|
|
160
|
+
* from the rest.
|
|
161
|
+
*
|
|
162
|
+
* `changed` is false when the body carries no marker, and then `body` is the input by
|
|
163
|
+
* identity: a projection of a rule nobody marked must not move a byte, or every
|
|
164
|
+
* repository in the estate would diff on a change none of them made.
|
|
165
|
+
*
|
|
166
|
+
* @returns {{body: string, removed: {text: string, expr: string}[], changed: boolean}}
|
|
167
|
+
*/
|
|
168
|
+
export function filterInferable(body, proofs) {
|
|
169
|
+
const parts = blocks(body);
|
|
170
|
+
if (!parts.some((block) => markers(block).length > 0)) {
|
|
171
|
+
return { body, removed: [], changed: false };
|
|
172
|
+
}
|
|
173
|
+
const kept = [];
|
|
174
|
+
const removed = [];
|
|
175
|
+
for (const block of parts) {
|
|
176
|
+
const found = markers(block);
|
|
177
|
+
if (found.length > 0) {
|
|
178
|
+
const terms = parseInferable(found[0].expr);
|
|
179
|
+
if (terms.length > 0 && terms.every((term) => proofs.has(term))) {
|
|
180
|
+
removed.push({ text: excerpt(stripMarkers(block)), expr: found[0].expr.trim() });
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const clean = stripMarkers(block).replace(/\s+$/, '');
|
|
185
|
+
if (clean.trim() !== '') kept.push(clean);
|
|
186
|
+
}
|
|
187
|
+
return { body: kept.join('\n\n'), removed, changed: true };
|
|
188
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Rules and decisions as MCP resources.
|
|
2
|
+
//
|
|
3
|
+
// The tools answer questions and the answers are arguments to the next call. These two
|
|
4
|
+
// answer a different request: "what is the rule you would apply, in full". A client that
|
|
5
|
+
// reads them can hold the text rather than reconstructing it from a tool's summary, and
|
|
6
|
+
// a reader outside this estate gets the same bytes an adopting repository's agent reads -
|
|
7
|
+
// redacted, because a decision record cites its own rulings and not this repository's
|
|
8
|
+
// layout (ADR-055, ADR-061).
|
|
9
|
+
//
|
|
10
|
+
// Both revisions agree on the shape here. They disagree about what a missing resource
|
|
11
|
+
// returns, and that difference is handled in the server, not here: this module answers
|
|
12
|
+
// with a resource or with nothing at all, and never with an empty `contents` array,
|
|
13
|
+
// which the newest revision forbids because it is indistinguishable from an empty rule.
|
|
14
|
+
|
|
15
|
+
import { readFile } from 'node:fs/promises';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
|
|
18
|
+
import { DECISIONS_DIR, loadDecisions, redactEstate, redactInternalPaths } from '../decisions/index.mjs';
|
|
19
|
+
import { CLI } from '../name.mjs';
|
|
20
|
+
import { loadMdcRules } from '../projection/mdc.mjs';
|
|
21
|
+
import { loadRules } from '../projection/rules.mjs';
|
|
22
|
+
|
|
23
|
+
const RULES_SCHEME = 'simt://rules/';
|
|
24
|
+
const DECISIONS_SCHEME = 'simt://decisions/';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* RFC 6570 templates, so a client can discover the shape without a hard-coded string.
|
|
28
|
+
*
|
|
29
|
+
* `id` and `adr` are the handles a reader already has: a gate names a rule id and a
|
|
30
|
+
* finding names an ADR, so the template's variable names match what those print.
|
|
31
|
+
*/
|
|
32
|
+
export const RESOURCE_TEMPLATES = [
|
|
33
|
+
{
|
|
34
|
+
uriTemplate: `${RULES_SCHEME}{id}`,
|
|
35
|
+
name: 'rules',
|
|
36
|
+
title: 'Projected rules',
|
|
37
|
+
description: 'One rule as this tool projects it: its frontmatter, its body and its decision citations.',
|
|
38
|
+
mimeType: 'text/markdown',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
uriTemplate: `${DECISIONS_SCHEME}{adr}`,
|
|
42
|
+
name: 'decisions',
|
|
43
|
+
title: 'Decision records',
|
|
44
|
+
description: 'One decision record, including the rulings and the reasons they were given.',
|
|
45
|
+
mimeType: 'text/markdown',
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Split a resource URI into its collection and its handle.
|
|
51
|
+
*
|
|
52
|
+
* String handling rather than `new URL`, because a custom scheme is parsed by the WHATWG
|
|
53
|
+
* URL machinery according to rules written for http, and the one thing this has to get
|
|
54
|
+
* right is which character separates `simt://rules` from the id. A handle that carries a
|
|
55
|
+
* slash or a dot segment is refused rather than resolved: a resource URI is not a file
|
|
56
|
+
* path, and the specification requires a server to reject one that tries to be.
|
|
57
|
+
*/
|
|
58
|
+
function parseResourceUri(uri) {
|
|
59
|
+
if (typeof uri !== 'string') return null;
|
|
60
|
+
const [collection, ...rest] = uri.split('://');
|
|
61
|
+
if (collection !== 'simt') return null;
|
|
62
|
+
const [name, ...handle] = rest.join('://').split('/');
|
|
63
|
+
const id = handle.join('/');
|
|
64
|
+
if (!id || id === '.' || id === '..' || id.includes('..')) return null;
|
|
65
|
+
if (name === 'rules' && id.includes('/')) return null;
|
|
66
|
+
if (name !== 'rules' && name !== 'decisions') return null;
|
|
67
|
+
return { collection: name, id };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function resource(collection, id, title, bytes) {
|
|
71
|
+
return {
|
|
72
|
+
uri: `simt://${collection}/${id}`,
|
|
73
|
+
name: id,
|
|
74
|
+
title,
|
|
75
|
+
description: `${title}, as this tool projects it`,
|
|
76
|
+
mimeType: 'text/markdown',
|
|
77
|
+
size: bytes,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Every rule and every decision record, one entry each.
|
|
83
|
+
*
|
|
84
|
+
* The list is built from what is on disk rather than from a hand-kept index, so it cannot
|
|
85
|
+
* describe a rule that was renamed or a record that was never written. Order is sorted:
|
|
86
|
+
* the newest revision forbids a set that varies per connection or as a side effect of
|
|
87
|
+
* another request, and a list built from a directory read is the only shape that satisfies
|
|
88
|
+
* that without a cache to invalidate.
|
|
89
|
+
*/
|
|
90
|
+
export async function listResources() {
|
|
91
|
+
const [rules, mdc, { decisions }] = await Promise.all([loadRules(null), loadMdcRules(), loadDecisions({ all: true })]);
|
|
92
|
+
// `size` is the bytes on disk rather than the bytes served: `read` redacts a decision
|
|
93
|
+
// before sending it, so the served length is not knowable without reading the file, and
|
|
94
|
+
// a size that disagrees with the payload is worse than a size that rounds.
|
|
95
|
+
return [
|
|
96
|
+
...[...rules, ...mdc]
|
|
97
|
+
.filter((rule) => rule.id !== null)
|
|
98
|
+
.map((rule) => resource('rules', rule.id, rule.title ?? rule.id, rule.bytes)),
|
|
99
|
+
...decisions.map((decision) => resource('decisions', decision.id, decision.title ?? decision.id, decision.bytes)),
|
|
100
|
+
].sort((a, b) => a.uri.localeCompare(b.uri));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One resource, or null when there is no such id.
|
|
105
|
+
*
|
|
106
|
+
* `null` is not a failure the caller swallows: the specification makes a missing resource
|
|
107
|
+
* a protocol error with a code that CHANGED between revisions, so the server decides the
|
|
108
|
+
* code and this module only declines to invent content.
|
|
109
|
+
*/
|
|
110
|
+
export async function readResource(uri) {
|
|
111
|
+
const parsed = parseResourceUri(uri);
|
|
112
|
+
if (parsed === null) return null;
|
|
113
|
+
|
|
114
|
+
if (parsed.collection === 'rules') {
|
|
115
|
+
const [rules, mdc] = await Promise.all([loadRules(null), loadMdcRules()]);
|
|
116
|
+
const rule = [...rules, ...mdc].find((candidate) => candidate.id === parsed.id);
|
|
117
|
+
if (rule === undefined) return null;
|
|
118
|
+
const text = `${frontmatterOf(rule)}\n${rule.body ?? ''}`.trim();
|
|
119
|
+
return {
|
|
120
|
+
uri,
|
|
121
|
+
mimeType: 'text/markdown',
|
|
122
|
+
text,
|
|
123
|
+
// The body a tool returns is redacted for the same reason: a reader outside this
|
|
124
|
+
// estate is told what the rule says and not which directory here it is filed in.
|
|
125
|
+
bytes: Buffer.byteLength(text),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// `loadDecisions` reads the index, not the records: it reports status, currency and
|
|
130
|
+
// byte count, and keeps no body. Reading the file the index named is one read of a
|
|
131
|
+
// record the reader has already asked for by id.
|
|
132
|
+
const { decisions } = await loadDecisions({ all: true });
|
|
133
|
+
const decision = decisions.find((candidate) => candidate.id === parsed.id);
|
|
134
|
+
if (decision === undefined) return null;
|
|
135
|
+
const raw = await readFile(join(DECISIONS_DIR, decision.file), 'utf8');
|
|
136
|
+
const text = redactInternalPaths(redactEstate(raw)).trim();
|
|
137
|
+
return { uri, mimeType: 'text/markdown', text, bytes: Buffer.byteLength(text) };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function frontmatterOf(rule) {
|
|
141
|
+
const keys = ['id', 'title', 'applies', 'priority'];
|
|
142
|
+
const present = keys.filter((key) => rule[key] !== undefined && rule[key] !== null);
|
|
143
|
+
if (present.length === 0) return '';
|
|
144
|
+
return ['---', ...present.map((key) => `${key}: ${formatValue(rule[key])}`), '---'].join('\n');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function formatValue(value) {
|
|
148
|
+
return Array.isArray(value) ? `[${value.join(', ')}]` : String(value);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The remedy a client gets when it asks for something that is not here.
|
|
153
|
+
*
|
|
154
|
+
* A command, not a path, for the reason every remedy in this package is a command: the
|
|
155
|
+
* reader runs this tool, not this checkout.
|
|
156
|
+
*/
|
|
157
|
+
const MISSING_REMEDY = `run: ${CLI} rules list, or ${CLI} graph lint to see what this copy can read`;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// A JSON Schema checker, for the subset these schemas use.
|
|
2
|
+
//
|
|
3
|
+
// Written here rather than taken from npm because a tool that validates a payload has
|
|
4
|
+
// to keep working on a machine where nothing was installed (ADR-012), and because the
|
|
5
|
+
// schemas it checks are in this file: a dependency would be a second place for the same
|
|
6
|
+
// rules to live, and the one thing a validator must not be is quietly out of date.
|
|
7
|
+
//
|
|
8
|
+
// The subset is deliberately small and deliberately loud. A keyword this file does not
|
|
9
|
+
// understand is reported as an error rather than skipped, because a keyword silently
|
|
10
|
+
// ignored turns a schema into decoration - the check passes, the payload does not match,
|
|
11
|
+
// and nobody can say which of the two was wrong. `assertValid` is the only entry point
|
|
12
|
+
// the tests use, and it names the failing path.
|
|
13
|
+
//
|
|
14
|
+
// Supported: type (including union arrays), properties, required, items, enum,
|
|
15
|
+
// additionalProperties (boolean or a schema), anyOf.
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check a value against a schema.
|
|
19
|
+
*
|
|
20
|
+
* @param {unknown} value
|
|
21
|
+
* @param {object} schema
|
|
22
|
+
* @param {string} [path] JSON pointer-ish location, for the message
|
|
23
|
+
* @returns {string|null} the first failure, or null when it validates
|
|
24
|
+
*/
|
|
25
|
+
export function check(value, schema, path = '$') {
|
|
26
|
+
if (schema === true || schema === undefined) return null;
|
|
27
|
+
if (schema === false) return `${path}: no value is allowed here`;
|
|
28
|
+
if (typeof schema !== 'object' || schema === null) return `${path}: schema must be an object`;
|
|
29
|
+
|
|
30
|
+
for (const keyword of Object.keys(schema)) {
|
|
31
|
+
if (!SUPPORTED.has(keyword)) {
|
|
32
|
+
// Loud on purpose: see the header. A keyword nobody implements is a promise the
|
|
33
|
+
// schema does not keep, and this is the only place that can notice.
|
|
34
|
+
throw new Error(`schema at ${path} uses "${keyword}", which this checker does not implement`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (schema.type !== undefined && !typeMatches(value, schema.type)) {
|
|
39
|
+
return `${path}: expected ${describe(schema.type)}, got ${describeValue(value)}`;
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(schema.enum) && !schema.enum.some((allowed) => same(allowed, value))) {
|
|
42
|
+
return `${path}: ${JSON.stringify(value)} is not one of ${JSON.stringify(schema.enum)}`;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(schema.anyOf)) {
|
|
45
|
+
const matched = schema.anyOf.some((option) => check(value, option, path) === null);
|
|
46
|
+
if (!matched) return `${path}: matched none of the ${schema.anyOf.length} allowed shapes`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (schema.type === 'object' || schema.properties !== undefined || schema.required !== undefined) {
|
|
50
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
51
|
+
for (const key of schema.required ?? []) {
|
|
52
|
+
if (!(key in value)) return `${path}: missing required property "${key}"`;
|
|
53
|
+
}
|
|
54
|
+
for (const [key, sub] of Object.entries(schema.properties ?? {})) {
|
|
55
|
+
if (!(key in value)) continue;
|
|
56
|
+
const failure = check(value[key], sub, `${path}.${key}`);
|
|
57
|
+
if (failure !== null) return failure;
|
|
58
|
+
}
|
|
59
|
+
if (schema.additionalProperties !== undefined) {
|
|
60
|
+
const known = new Set(Object.keys(schema.properties ?? {}));
|
|
61
|
+
for (const [key, sub] of Object.entries(value)) {
|
|
62
|
+
if (known.has(key)) continue;
|
|
63
|
+
if (schema.additionalProperties === false) return `${path}: unexpected property "${key}"`;
|
|
64
|
+
if (schema.additionalProperties !== true) {
|
|
65
|
+
const failure = check(sub, schema.additionalProperties, `${path}.${key}`);
|
|
66
|
+
if (failure !== null) return failure;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (schema.items !== undefined && Array.isArray(value)) {
|
|
73
|
+
for (const [index, item] of value.entries()) {
|
|
74
|
+
const failure = check(item, schema.items, `${path}[${index}]`);
|
|
75
|
+
if (failure !== null) return failure;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Throws with the first failure, named. The shape a test wants. */
|
|
82
|
+
export function assertValid(value, schema, what = 'value') {
|
|
83
|
+
const failure = check(value, schema);
|
|
84
|
+
if (failure !== null) throw new Error(`${what} does not match its schema: ${failure}`);
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const SUPPORTED = new Set([
|
|
89
|
+
'type',
|
|
90
|
+
'properties',
|
|
91
|
+
'required',
|
|
92
|
+
'items',
|
|
93
|
+
'enum',
|
|
94
|
+
'additionalProperties',
|
|
95
|
+
'anyOf',
|
|
96
|
+
'description',
|
|
97
|
+
'$schema',
|
|
98
|
+
]);
|
|
99
|
+
|
|
100
|
+
function typeMatches(value, type) {
|
|
101
|
+
const types = Array.isArray(type) ? type : [type];
|
|
102
|
+
return types.some((t) => singleTypeMatches(value, t));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function singleTypeMatches(value, type) {
|
|
106
|
+
switch (type) {
|
|
107
|
+
case 'object':
|
|
108
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
109
|
+
case 'array':
|
|
110
|
+
return Array.isArray(value);
|
|
111
|
+
case 'string':
|
|
112
|
+
return typeof value === 'string';
|
|
113
|
+
case 'number':
|
|
114
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
115
|
+
case 'integer':
|
|
116
|
+
return Number.isInteger(value);
|
|
117
|
+
case 'boolean':
|
|
118
|
+
return typeof value === 'boolean';
|
|
119
|
+
case 'null':
|
|
120
|
+
return value === null;
|
|
121
|
+
default:
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function describe(type) {
|
|
127
|
+
return Array.isArray(type) ? type.join('|') : type;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What a value IS, for the failure message. Distinct from `describe(type)`. */
|
|
131
|
+
function describeValue(value) {
|
|
132
|
+
if (value === null) return 'null';
|
|
133
|
+
if (Array.isArray(value)) return 'array';
|
|
134
|
+
return typeof value;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function same(a, b) {
|
|
138
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
139
|
+
}
|