@sabaiway/agent-workflow-kit 14.39.0 → 14.41.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 +63 -0
- package/SKILL.md +1 -1
- package/capability.json +1 -1
- package/package.json +1 -1
- package/references/advisor-writers.md +3 -0
- package/references/modes/bootstrap.md +1 -0
- package/references/modes/gates.md +1 -1
- package/references/modes/procedures.md +1 -1
- package/references/modes/recommendations.md +9 -8
- package/references/modes/rules.md +4 -3
- package/references/scripts/install-git-hooks-repo-exec.test.mjs +56 -6
- package/references/scripts/install-git-hooks.mjs +60 -4
- package/references/scripts/install-git-hooks.test.mjs +173 -75
- package/tools/apply-danger-check.mjs +16 -6
- package/tools/gates-init.mjs +17 -8
- package/tools/git-hooks.mjs +146 -20
- package/tools/node-evidence.mjs +10 -2
- package/tools/procedures.mjs +9 -2
- package/tools/recommendations.mjs +85 -15
- package/tools/run-gates.mjs +13 -3
- package/tools/script-priors.mjs +11 -8
- package/tools/source-size/index.mjs +36 -0
- package/tools/source-size/layout.mjs +136 -0
- package/tools/source-size/limits.mjs +110 -0
- package/tools/source-size/record.mjs +143 -0
- package/tools/source-size/setup.mjs +40 -0
- package/tools/source-size-check.mjs +196 -127
- package/tools/source-size-config.mjs +92 -27
- package/tools/source-size-core.mjs +50 -4
- package/tools/source-size-judge.mjs +143 -77
- package/tools/source-size-refusal.mjs +8 -3
- package/tools/source-size-report.mjs +182 -46
- package/tools/source-size-scope.mjs +31 -6
- package/tools/write-lanes.mjs +7 -2
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// index.mjs — the door of the size check's module: every kit module outside tools/source-size/
|
|
2
|
+
// reaches it through this file alone.
|
|
3
|
+
|
|
4
|
+
export {
|
|
5
|
+
KIT_LIMITS,
|
|
6
|
+
SCHEMA1_REASON,
|
|
7
|
+
applyOverride,
|
|
8
|
+
limitLines,
|
|
9
|
+
limitsDefect,
|
|
10
|
+
migrateSchema1,
|
|
11
|
+
resolveLimits,
|
|
12
|
+
} from './limits.mjs';
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
SPEC_STORE_ROOT,
|
|
16
|
+
bandOf,
|
|
17
|
+
layoutCounts,
|
|
18
|
+
layoutDefect,
|
|
19
|
+
walkSpecStore,
|
|
20
|
+
} from './layout.mjs';
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
changesFor,
|
|
24
|
+
isRaise,
|
|
25
|
+
materialize,
|
|
26
|
+
ownEntry,
|
|
27
|
+
planRecord,
|
|
28
|
+
projectLayout,
|
|
29
|
+
serialize,
|
|
30
|
+
} from './record.mjs';
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
SETUP_ANSWERS,
|
|
34
|
+
checkTargetOf,
|
|
35
|
+
layoutSetupOf,
|
|
36
|
+
} from './setup.mjs';
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// layout.mjs — the layout limits: the band every limit is judged with, the layout count over a list
|
|
2
|
+
// of entries, the layout record's grammar and the spec store's walk. It writes no file, spawns no
|
|
3
|
+
// process and imports no file of the practice, so the record's reason rule is handed in by its
|
|
4
|
+
// caller; the walk alone reads the disk, through the readdir and lstat it is handed.
|
|
5
|
+
//
|
|
6
|
+
// A name is carried as a binary string — one code point per byte — so names are compared as bytes
|
|
7
|
+
// and a name that is not valid UTF-8 is still counted; the caller decodes a key only where a record
|
|
8
|
+
// needs one.
|
|
9
|
+
//
|
|
10
|
+
// Dependency-free, Node >= 22. No side effects on import.
|
|
11
|
+
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
// The spec store: one more judged root, at a fixed path, walked from disk because a deployment may
|
|
15
|
+
// hide it from git.
|
|
16
|
+
export const SPEC_STORE_ROOT = 'docs/ai/specs';
|
|
17
|
+
|
|
18
|
+
// bandOf(value, tolerance) → ceil(value × tolerance / 100), in integers so no float rounds it.
|
|
19
|
+
export const bandOf = (value, tolerance) => Math.floor((value * tolerance + 99) / 100);
|
|
20
|
+
|
|
21
|
+
// layoutCounts(entries) → { files, directories }: `entries` are { path, dir } relative to one root,
|
|
22
|
+
// '/'-separated; `directories` maps each directory ('' for the root's own) to its entries (the
|
|
23
|
+
// distinct names directly under it), its depth (its segments below the root) and its reach (the
|
|
24
|
+
// deepest level its subtree holds). A file's own name is no level; a non-directory entry is a file.
|
|
25
|
+
export const layoutCounts = (entries) => {
|
|
26
|
+
const children = new Map([['', new Set()]]);
|
|
27
|
+
const namesOf = (dir) => {
|
|
28
|
+
if (!children.has(dir)) children.set(dir, new Set());
|
|
29
|
+
return children.get(dir);
|
|
30
|
+
};
|
|
31
|
+
let files = 0;
|
|
32
|
+
for (const { path, dir } of entries) {
|
|
33
|
+
const segments = path.split('/');
|
|
34
|
+
segments.forEach((name, at) => namesOf(segments.slice(0, at).join('/')).add(name));
|
|
35
|
+
if (dir) namesOf(path);
|
|
36
|
+
else files += 1;
|
|
37
|
+
}
|
|
38
|
+
const directories = new Map([...children].map(([key, names]) => {
|
|
39
|
+
const depth = key === '' ? 0 : key.split('/').length;
|
|
40
|
+
return [key, { entries: names.size, depth, reach: depth }];
|
|
41
|
+
}));
|
|
42
|
+
for (const [key, { depth }] of directories) {
|
|
43
|
+
const segments = key === '' ? [] : key.split('/');
|
|
44
|
+
for (let at = 0; at < segments.length; at += 1) {
|
|
45
|
+
const ancestor = directories.get(segments.slice(0, at).join('/'));
|
|
46
|
+
if (ancestor.reach < depth) ancestor.reach = depth;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { files, directories };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
53
|
+
const BAD_SEGMENTS = new Set(['', '.', '..']);
|
|
54
|
+
|
|
55
|
+
const keysDefect = (value, what, allowed, required) => {
|
|
56
|
+
if (!isPlainObject(value)) return `${what} must be an object holding ${allowed.join(', ')}`;
|
|
57
|
+
const unknown = Object.keys(value).filter((key) => !allowed.includes(key));
|
|
58
|
+
if (unknown.length > 0) return `${what} carries unknown key(s): ${unknown.join(', ')}`;
|
|
59
|
+
const missing = required.filter((key) => !Object.hasOwn(value, key));
|
|
60
|
+
return missing.length > 0 ? `${what} is missing ${missing.join(', ')}` : null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const entryDefect = (key, entry, what, counts, reasonDefect) => {
|
|
64
|
+
if (key !== '.' && key.split('/').some((segment) => BAD_SEGMENTS.has(segment))) {
|
|
65
|
+
return `${what} key "${key}" must be ".", or a path with no empty, "." or ".." segment`;
|
|
66
|
+
}
|
|
67
|
+
const at = `${what}."${key}"`;
|
|
68
|
+
const shape = keysDefect(entry, at, [...counts, 'reason'], counts.length === 1 ? [...counts, 'reason'] : ['reason']);
|
|
69
|
+
if (shape) return shape;
|
|
70
|
+
if (!counts.some((count) => Object.hasOwn(entry, count))) return `${at} must record ${counts.join(' or ')}`;
|
|
71
|
+
for (const count of counts.filter((name) => Object.hasOwn(entry, name))) {
|
|
72
|
+
if (!(Number.isSafeInteger(entry[count]) && entry[count] >= 0)) {
|
|
73
|
+
return `${at}.${count} must be a non-negative integer, got ${JSON.stringify(entry[count])}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const reason = reasonDefect(entry.reason);
|
|
77
|
+
return reason ? `${at}.reason: ${reason}` : null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// layoutDefect(layout, reasonDefect) → the first defect naming its key, or null. The record holds
|
|
81
|
+
// exactly roots (each exactly files and reason) and directories (entries, depth or both, and reason).
|
|
82
|
+
export const layoutDefect = (layout, reasonDefect) => {
|
|
83
|
+
const shape = keysDefect(layout, '"layout"', ['roots', 'directories'], ['roots', 'directories']);
|
|
84
|
+
if (shape) return shape;
|
|
85
|
+
for (const [map, counts] of [['roots', ['files']], ['directories', ['entries', 'depth']]]) {
|
|
86
|
+
const what = `"layout"."${map}"`;
|
|
87
|
+
if (!isPlainObject(layout[map])) return `${what} must be an object`;
|
|
88
|
+
for (const [key, entry] of Object.entries(layout[map])) {
|
|
89
|
+
const defect = entryDefect(key, entry, what, counts, reasonDefect);
|
|
90
|
+
if (defect) return defect;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const SLASH = Buffer.from('/');
|
|
97
|
+
|
|
98
|
+
// walkSpecStore(cwd, { readdir, lstat }) → { state: 'absent' } | { state: 'present', entries } |
|
|
99
|
+
// { state: 'refused', path, cause }. Every entry is read by lstat and no symlink is followed, so a
|
|
100
|
+
// link — one pointing at its own parent included — is counted once as an entry and never entered.
|
|
101
|
+
export const walkSpecStore = (cwd, { readdir, lstat }) => {
|
|
102
|
+
const root = Buffer.from(join(cwd, SPEC_STORE_ROOT));
|
|
103
|
+
const refused = (path, err) => ({ state: 'refused', path: path.toString(), cause: err?.message ?? String(err) });
|
|
104
|
+
let stat;
|
|
105
|
+
try {
|
|
106
|
+
stat = lstat(root);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
return err?.code === 'ENOENT' || err?.code === 'ENOTDIR' ? { state: 'absent' } : refused(root, err);
|
|
109
|
+
}
|
|
110
|
+
if (!stat.isDirectory()) return refused(root, new Error('present but not a directory'));
|
|
111
|
+
const entries = [];
|
|
112
|
+
const pending = [{ abs: root, rel: '' }];
|
|
113
|
+
while (pending.length > 0) {
|
|
114
|
+
const { abs, rel } = pending.pop();
|
|
115
|
+
let names;
|
|
116
|
+
try {
|
|
117
|
+
names = readdir(abs, { encoding: 'buffer' });
|
|
118
|
+
} catch (err) {
|
|
119
|
+
return refused(abs, err);
|
|
120
|
+
}
|
|
121
|
+
for (const raw of names) {
|
|
122
|
+
const name = typeof raw === 'string' ? Buffer.from(raw) : raw;
|
|
123
|
+
const path = Buffer.concat([abs, SLASH, name]);
|
|
124
|
+
let entry;
|
|
125
|
+
try {
|
|
126
|
+
entry = lstat(path);
|
|
127
|
+
} catch (err) {
|
|
128
|
+
return refused(path, err);
|
|
129
|
+
}
|
|
130
|
+
const child = rel === '' ? name.toString('latin1') : `${rel}/${name.toString('latin1')}`;
|
|
131
|
+
entries.push({ path: child, dir: entry.isDirectory() });
|
|
132
|
+
if (entry.isDirectory()) pending.push({ abs: path, rel: child });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { state: 'present', entries };
|
|
136
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// limits.mjs — the kit's five size limits and a project's reasoned overrides of them: the frozen
|
|
2
|
+
// table, the override grammar, the resolution every reader gets, the lines that state it, and the two
|
|
3
|
+
// transforms the writer applies (the schema-1 move and one override). Pure: it writes no file, spawns no process and
|
|
4
|
+
// imports no file of the practice, so the record's reason rule is handed in by its caller.
|
|
5
|
+
//
|
|
6
|
+
// Dependency-free, Node >= 22. No side effects on import.
|
|
7
|
+
|
|
8
|
+
const limit = (value, tolerance) => Object.freeze({ value, tolerance });
|
|
9
|
+
|
|
10
|
+
// `files-per-root` has no kit value: its count is the one a project records.
|
|
11
|
+
export const KIT_LIMITS = Object.freeze({
|
|
12
|
+
'lines-per-file': limit(400, 10),
|
|
13
|
+
'bytes-per-line': limit(1000, 0),
|
|
14
|
+
'entries-per-directory': limit(20, 10),
|
|
15
|
+
depth: limit(5, 0),
|
|
16
|
+
'files-per-root': limit(null, 10),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const SCHEMA1_REASON = 'carried from schema 1';
|
|
20
|
+
|
|
21
|
+
// The schema-1 `defaults` key each carried limit was spelled under.
|
|
22
|
+
const SCHEMA1_KEY = Object.freeze({ 'lines-per-file': 'maxLines', 'bytes-per-line': 'maxLineBytes' });
|
|
23
|
+
|
|
24
|
+
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
25
|
+
const hasValue = (id) => KIT_LIMITS[id].value !== null;
|
|
26
|
+
const overrideKeys = (id) => (hasValue(id) ? ['value', 'tolerance', 'reason'] : ['tolerance', 'reason']);
|
|
27
|
+
|
|
28
|
+
const overrideDefect = (id, override, reasonDefect) => {
|
|
29
|
+
if (!isPlainObject(override)) return `"limits"."${id}" must be an object holding ${overrideKeys(id).join(', ')}`;
|
|
30
|
+
const keys = overrideKeys(id);
|
|
31
|
+
const unknown = Object.keys(override).filter((key) => !keys.includes(key));
|
|
32
|
+
if (unknown.length > 0) {
|
|
33
|
+
const why = hasValue(id) ? '' : ` — ${id} takes no value, its count being the one a project records`;
|
|
34
|
+
return `"limits"."${id}" carries unknown key(s): ${unknown.join(', ')}${why}`;
|
|
35
|
+
}
|
|
36
|
+
const missing = keys.filter((key) => !Object.hasOwn(override, key));
|
|
37
|
+
if (missing.length > 0) return `"limits"."${id}" is missing ${missing.join(', ')} — an override holds exactly ${keys.join(', ')}`;
|
|
38
|
+
if (hasValue(id) && !(Number.isSafeInteger(override.value) && override.value > 0)) {
|
|
39
|
+
return `"limits"."${id}".value must be a positive integer, got ${JSON.stringify(override.value)}`;
|
|
40
|
+
}
|
|
41
|
+
if (!(Number.isSafeInteger(override.tolerance) && override.tolerance >= 0 && override.tolerance <= 100)) {
|
|
42
|
+
return `"limits"."${id}".tolerance must be an integer percent from 0 to 100, got ${JSON.stringify(override.tolerance)}`;
|
|
43
|
+
}
|
|
44
|
+
const reason = reasonDefect(override.reason);
|
|
45
|
+
return reason ? `"limits"."${id}".reason: ${reason}` : null;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// limitsDefect(limits, reasonDefect) → the first defect naming its key, or null. A mistyped override
|
|
49
|
+
// would otherwise leave the kit's value in force while its author believes it changed.
|
|
50
|
+
export const limitsDefect = (limits, reasonDefect) => {
|
|
51
|
+
if (!isPlainObject(limits)) return '"limits" must be an object mapping a kit limit id to its override';
|
|
52
|
+
for (const [id, override] of Object.entries(limits)) {
|
|
53
|
+
if (!Object.hasOwn(KIT_LIMITS, id)) return `"limits" names "${id}", which is no kit limit — the kit's limits are ${Object.keys(KIT_LIMITS).join(', ')}`;
|
|
54
|
+
const defect = overrideDefect(id, override, reasonDefect);
|
|
55
|
+
if (defect) return defect;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// resolveLimits(limits) → { limits, defaults }: every limit with its value, tolerance and source, an
|
|
61
|
+
// override carrying its reason, and `defaults` in the shape the judge and the facts already read.
|
|
62
|
+
export const resolveLimits = (limits = {}) => {
|
|
63
|
+
const resolved = Object.fromEntries(Object.entries(KIT_LIMITS).map(([id, kit]) => {
|
|
64
|
+
if (!Object.hasOwn(limits, id)) return [id, { value: kit.value, tolerance: kit.tolerance, source: 'kit' }];
|
|
65
|
+
const override = limits[id];
|
|
66
|
+
return [id, { value: hasValue(id) ? override.value : null, tolerance: override.tolerance, source: 'override', reason: override.reason }];
|
|
67
|
+
}));
|
|
68
|
+
return {
|
|
69
|
+
limits: resolved,
|
|
70
|
+
defaults: { maxLines: resolved['lines-per-file'].value, maxLineBytes: resolved['bytes-per-line'].value },
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// limitLines(limits, { escape, recorded }) → one line per resolved limit, all five, in the table's
|
|
75
|
+
// order: value, tolerance and source. The escape that makes a reason safe on a line is handed in, so
|
|
76
|
+
// the check and the plan-time render state the same five lines from this one function.
|
|
77
|
+
export const limitLines = (limits, { escape = String, recorded = false } = {}) => Object.keys(KIT_LIMITS).map((id) => {
|
|
78
|
+
const { value, tolerance, source, reason } = limits[id];
|
|
79
|
+
const amount = value !== null ? `${value}` : recorded ? "each judged root's recorded count" : 'none recorded';
|
|
80
|
+
return `limit ${id} ${amount}, tolerance ${tolerance}% — ${source === 'kit' ? 'from kit' : `from override: ${escape(reason)}`}`;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// migrateSchema1(parsed, readmes) → the schema-2 config the writer writes, every key in its place. A
|
|
84
|
+
// `_README` byte-equal to `readmes.schema1` becomes `readmes.schema2`; any other is carried verbatim.
|
|
85
|
+
export const migrateSchema1 = (parsed, readmes = {}) => {
|
|
86
|
+
const overrides = Object.fromEntries(Object.entries(SCHEMA1_KEY)
|
|
87
|
+
.filter(([id, key]) => parsed.defaults[key] !== KIT_LIMITS[id].value)
|
|
88
|
+
.map(([id, key]) => [id, { value: parsed.defaults[key], tolerance: KIT_LIMITS[id].tolerance, reason: SCHEMA1_REASON }]));
|
|
89
|
+
return Object.fromEntries(Object.entries(parsed).flatMap(([key, value]) => {
|
|
90
|
+
if (key === 'schema') return [[key, 2]];
|
|
91
|
+
if (key === '_README') return [[key, value === readmes.schema1 ? readmes.schema2 : value]];
|
|
92
|
+
if (key === 'defaults') return Object.keys(overrides).length === 0 ? [] : [['limits', overrides]];
|
|
93
|
+
return [[key, value]];
|
|
94
|
+
}));
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// applyOverride(parsed, id, override) → the config with one id's override set from { value,
|
|
98
|
+
// tolerance, reason } or, given null, removed; a `limits` left empty is dropped and a new one is
|
|
99
|
+
// appended. It judges nothing about the tree: the writer regenerates the record from the result.
|
|
100
|
+
export const applyOverride = (parsed, id, override) => {
|
|
101
|
+
const limits = { ...(parsed.limits ?? {}) };
|
|
102
|
+
if (override === null) delete limits[id];
|
|
103
|
+
else limits[id] = Object.fromEntries(overrideKeys(id).map((key) => [key, override[key]]));
|
|
104
|
+
const kept = Object.keys(limits).length > 0;
|
|
105
|
+
const entries = Object.entries(parsed)
|
|
106
|
+
.filter(([key]) => key !== 'limits' || kept)
|
|
107
|
+
.map(([key, value]) => [key, key === 'limits' ? limits : value]);
|
|
108
|
+
if (kept && !Object.hasOwn(parsed, 'limits')) entries.push(['limits', limits]);
|
|
109
|
+
return Object.fromEntries(entries);
|
|
110
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// record.mjs — the recorded ratchet as data: the delta rules every half reads, the layout record a
|
|
2
|
+
// regeneration would write, the regeneration plan, its materialization with each reason, and the
|
|
3
|
+
// canonical serialization. Pure: it writes no file and spawns no process — the writer half applies
|
|
4
|
+
// it and owns the one write.
|
|
5
|
+
//
|
|
6
|
+
// Dependency-free, Node >= 22. No side effects on import.
|
|
7
|
+
|
|
8
|
+
import { bandOf } from './layout.mjs';
|
|
9
|
+
|
|
10
|
+
const FILE_DIMENSIONS = Object.freeze(['lines', 'maxLineBytes']);
|
|
11
|
+
const LAYOUT_DIMENSIONS = Object.freeze(['entries', 'depth']);
|
|
12
|
+
const MACHINE_KEYS = Object.freeze(['baseline', 'aggregate', 'layout']);
|
|
13
|
+
|
|
14
|
+
// A recorded entry is read by OWN key only. A declared root may legitimately be named like an
|
|
15
|
+
// Object.prototype member ("constructor", "toString"), and a plain lookup would then answer with an
|
|
16
|
+
// INHERITED value — which reads as "already recorded", so the raise goes unnoticed and the entry is
|
|
17
|
+
// written with no reason at all, producing a config the validator refuses.
|
|
18
|
+
export const ownEntry = (map, key) => (Object.hasOwn(map, key) ? map[key] : undefined);
|
|
19
|
+
|
|
20
|
+
// A change is a RAISE when it puts a number where there was none, or a bigger one where there was a
|
|
21
|
+
// smaller — the whole class a human's reason is required for. Everything else lowers or removes.
|
|
22
|
+
export const isRaise = ({ from, to }) => to !== null && (from === null || to > from);
|
|
23
|
+
|
|
24
|
+
// changesFor(target, projected, recorded, dimensions) → the per-dimension old→new pairs that differ.
|
|
25
|
+
// The refusal, the printed delta and the reason an entry ends up with all read THIS.
|
|
26
|
+
export const changesFor = (target, projected, recorded, dimensions = FILE_DIMENSIONS) => dimensions
|
|
27
|
+
.map((dimension) => ({
|
|
28
|
+
target,
|
|
29
|
+
dimension,
|
|
30
|
+
from: recorded && Object.hasOwn(recorded, dimension) ? recorded[dimension] : null,
|
|
31
|
+
to: Object.hasOwn(projected, dimension) ? projected[dimension] : null,
|
|
32
|
+
}))
|
|
33
|
+
.filter(({ from, to }) => from !== to);
|
|
34
|
+
|
|
35
|
+
const ceilingOf = ({ value, tolerance }) => value + bandOf(value, tolerance);
|
|
36
|
+
|
|
37
|
+
// projectLayout(measured, recorded, limits) → the layout record a regeneration would write, without
|
|
38
|
+
// reasons. A root's files are written as measured, except a count above its record and within its
|
|
39
|
+
// band, which keeps the record: a warning never raises. A directory is recorded only beyond its band:
|
|
40
|
+
// `entries` its count, `depth` on the shallowest directory beyond the depth band, holding the deepest
|
|
41
|
+
// level its subtree reaches.
|
|
42
|
+
export const projectLayout = (measured, recorded, limits) => {
|
|
43
|
+
const tolerance = limits['files-per-root'].tolerance;
|
|
44
|
+
const roots = Object.fromEntries([...measured.roots].map(([root, files]) => {
|
|
45
|
+
const kept = recorded ? ownEntry(recorded.roots, root) : undefined;
|
|
46
|
+
const keep = kept !== undefined && files > kept.files && files <= kept.files + bandOf(kept.files, tolerance);
|
|
47
|
+
return [root, keep ? kept.files : files];
|
|
48
|
+
}));
|
|
49
|
+
const entries = ceilingOf(limits['entries-per-directory']);
|
|
50
|
+
const holder = ceilingOf(limits.depth) + 1;
|
|
51
|
+
const directories = Object.fromEntries([...measured.directories].flatMap(([key, facts]) => {
|
|
52
|
+
const next = {
|
|
53
|
+
...(facts.entries > entries ? { entries: facts.entries } : {}),
|
|
54
|
+
...(facts.depth === holder ? { depth: facts.reach } : {}),
|
|
55
|
+
};
|
|
56
|
+
return Object.keys(next).length > 0 ? [[key, next]] : [];
|
|
57
|
+
}));
|
|
58
|
+
return { roots, directories };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const layoutPlan = (recorded, projected) => {
|
|
62
|
+
const old = recorded ?? { roots: {}, directories: {} };
|
|
63
|
+
const roots = Object.entries(projected.roots).map(([root, next]) => ({ root, next, recorded: ownEntry(old.roots, root) }));
|
|
64
|
+
const directories = Object.entries(projected.directories).map(([key, next]) => {
|
|
65
|
+
const was = ownEntry(old.directories, key);
|
|
66
|
+
return { key, next, recorded: was, changes: changesFor(key, next, was, LAYOUT_DIMENSIONS) };
|
|
67
|
+
});
|
|
68
|
+
const deltas = [
|
|
69
|
+
...roots
|
|
70
|
+
.filter(({ next, recorded: was }) => (was ? was.files : null) !== next)
|
|
71
|
+
.map(({ root, next, recorded: was }) => ({ target: root, dimension: 'files', from: was ? was.files : null, to: next })),
|
|
72
|
+
...Object.entries(old.roots)
|
|
73
|
+
.filter(([root]) => !Object.hasOwn(projected.roots, root))
|
|
74
|
+
.map(([root, was]) => ({ target: root, dimension: 'files', from: was.files, to: null })),
|
|
75
|
+
...directories.flatMap(({ changes }) => changes),
|
|
76
|
+
...Object.entries(old.directories)
|
|
77
|
+
.filter(([key]) => !Object.hasOwn(projected.directories, key))
|
|
78
|
+
.flatMap(([key, was]) => changesFor(key, {}, was, LAYOUT_DIMENSIONS)),
|
|
79
|
+
];
|
|
80
|
+
return { roots, directories, deltas };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// planRecord({ config, verdict }) computes what the regeneration WOULD write, before any reason is
|
|
84
|
+
// applied — so a raise can be refused without ever building an entry that carries no reason. It reads
|
|
85
|
+
// the JUDGE's projection, never its own: the checker's verdict and the file the writer produces are
|
|
86
|
+
// then two renderings of one computation.
|
|
87
|
+
export const planRecord = ({ config, verdict }) => {
|
|
88
|
+
const oldBaseline = config.baseline ?? {};
|
|
89
|
+
const oldAggregate = config.aggregate ?? {};
|
|
90
|
+
const files = verdict.scope.files.map((rel) => {
|
|
91
|
+
const next = verdict.projected.get(rel);
|
|
92
|
+
const recorded = ownEntry(oldBaseline, rel);
|
|
93
|
+
return { rel, next, recorded, changes: changesFor(rel, next, recorded) };
|
|
94
|
+
});
|
|
95
|
+
const roots = config.roots.map((root) => ({ root, lines: verdict.rootLines.get(root) ?? 0, recorded: ownEntry(oldAggregate, root) }));
|
|
96
|
+
const layout = verdict.layout ? layoutPlan(config.layout, verdict.layout.projected) : null;
|
|
97
|
+
const deltas = [
|
|
98
|
+
...files.flatMap(({ changes }) => changes),
|
|
99
|
+
// A record whose file left scope is REMOVED, never kept: that is what makes a split or a rename
|
|
100
|
+
// visible in the delta instead of silently surviving as headroom.
|
|
101
|
+
...Object.entries(oldBaseline)
|
|
102
|
+
.filter(([rel]) => !verdict.measured.has(rel))
|
|
103
|
+
.flatMap(([rel, recorded]) => changesFor(rel, {}, recorded)),
|
|
104
|
+
...roots
|
|
105
|
+
.filter(({ lines, recorded }) => (recorded ? recorded.lines : null) !== lines)
|
|
106
|
+
.map(({ root, lines, recorded }) => ({ target: root, dimension: 'aggregate lines', from: recorded ? recorded.lines : null, to: lines })),
|
|
107
|
+
...Object.entries(oldAggregate)
|
|
108
|
+
.filter(([root]) => !config.roots.includes(root))
|
|
109
|
+
.map(([root, recorded]) => ({ target: root, dimension: 'aggregate lines', from: recorded.lines, to: null })),
|
|
110
|
+
...(layout ? layout.deltas : []),
|
|
111
|
+
];
|
|
112
|
+
return { files, roots, layout, deltas, raises: deltas.filter(isRaise) };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const byKey = (entries) => Object.fromEntries(entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
116
|
+
|
|
117
|
+
// The reason of an entry the regeneration did NOT raise is the one already recorded — a tighten
|
|
118
|
+
// rewrites a number, never the human sentence that justified it.
|
|
119
|
+
export const materialize = ({ files, roots, layout }, reason) => ({
|
|
120
|
+
baseline: Object.fromEntries(
|
|
121
|
+
files
|
|
122
|
+
.filter(({ next }) => Object.keys(next).length > 0)
|
|
123
|
+
.map(({ rel, next, recorded, changes }) => [rel, { ...next, reason: changes.some(isRaise) ? reason : recorded.reason }]),
|
|
124
|
+
),
|
|
125
|
+
aggregate: Object.fromEntries(
|
|
126
|
+
roots.map(({ root, lines, recorded }) => [root, { lines, reason: !recorded || lines > recorded.lines ? reason : recorded.reason }]),
|
|
127
|
+
),
|
|
128
|
+
...(layout === null ? {} : {
|
|
129
|
+
layout: {
|
|
130
|
+
roots: byKey(layout.roots.map(({ root, next, recorded }) => [root, { files: next, reason: !recorded || next > recorded.files ? reason : recorded.reason }])),
|
|
131
|
+
directories: byKey(layout.directories.map(({ key, next, recorded, changes }) => [key, { ...next, reason: changes.some(isRaise) ? reason : recorded.reason }])),
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// The file is CANONICALLY serialized: the authored VALUES and the ORDER the human wrote them in are
|
|
137
|
+
// preserved exactly, their formatting is not — this file is machine-maintained and the writer owns
|
|
138
|
+
// half of it, so one deterministic rendering is what makes "regenerate an unchanged tree and get the
|
|
139
|
+
// same bytes" true at all. The machine keys follow in their fixed order.
|
|
140
|
+
export const serialize = (parsed, machine) => {
|
|
141
|
+
const authored = Object.fromEntries(Object.entries(parsed).filter(([key]) => !MACHINE_KEYS.includes(key)));
|
|
142
|
+
return `${JSON.stringify({ ...authored, ...machine }, null, 2)}\n`;
|
|
143
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// setup.mjs — the layout setup: its closed answers, the one statement of what a --check run judges,
|
|
2
|
+
// and the setup's answer for a project. It writes no file, spawns no process of its own and imports no
|
|
3
|
+
// file of the practice: every read reaches it through the values its caller hands it, so the advisor,
|
|
4
|
+
// the git-hooks leaf and the check ask one rule.
|
|
5
|
+
//
|
|
6
|
+
// Dependency-free, Node >= 22. No side effects on import.
|
|
7
|
+
|
|
8
|
+
import { SPEC_STORE_ROOT } from './layout.mjs';
|
|
9
|
+
|
|
10
|
+
export const SETUP_ANSWERS = Object.freeze(['armable', 'record', 'held']);
|
|
11
|
+
|
|
12
|
+
// checkTargetOf({ loaded, absentConfig }) → exactly one of: the schema-1 stop, the unminted stop (a
|
|
13
|
+
// present config authored or incomplete), or the config to judge with the dimensions judged.
|
|
14
|
+
export const checkTargetOf = ({ loaded, absentConfig }) => {
|
|
15
|
+
const absent = loaded.state === 'absent';
|
|
16
|
+
const config = absent ? absentConfig : loaded.config;
|
|
17
|
+
if (config.schema === 1) return { stop: 'schema-1', config };
|
|
18
|
+
if (!absent && loaded.state !== 'minted') return { stop: 'unminted', config, state: loaded.state, missing: loaded.missingMachineKeys };
|
|
19
|
+
return { stop: null, config, judged: { absent, layout: absent || config.layout !== null } };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// The store-root rule of a config that starts layout: the store is a root of its own there.
|
|
23
|
+
const insideStore = (root) => root === SPEC_STORE_ROOT || root.startsWith(`${SPEC_STORE_ROOT}/`);
|
|
24
|
+
|
|
25
|
+
// layoutSetupOf(root, practice, deps) → { answer: 'armable' } | { answer: 'record', args } |
|
|
26
|
+
// { answer: 'held', reason } with reason schema-1, unminted, store-root (with the root) or stops.
|
|
27
|
+
// `practice` carries the loader, judgeTree, stopsOf, the absent config the check judges and the reason
|
|
28
|
+
// the record's first entries take.
|
|
29
|
+
export const layoutSetupOf = (root, practice, deps = {}) => {
|
|
30
|
+
const target = checkTargetOf({ loaded: practice.loadSourceSizeConfig(root, deps), absentConfig: practice.absentConfig });
|
|
31
|
+
if (target.stop !== null) return { answer: 'held', reason: target.stop };
|
|
32
|
+
const { config, judged } = target;
|
|
33
|
+
const starts = !judged.layout;
|
|
34
|
+
const storeRoot = starts ? config.roots.find(insideStore) : undefined;
|
|
35
|
+
if (storeRoot !== undefined) return { answer: 'held', reason: 'store-root', root: storeRoot };
|
|
36
|
+
const stops = practice.stopsOf(practice.judgeTree(root, config, deps, judged).findings).length > 0;
|
|
37
|
+
if (judged.absent) return stops ? { answer: 'record', args: ['--write-baseline', '--reason', practice.reason] } : { answer: 'armable' };
|
|
38
|
+
if (stops) return { answer: 'held', reason: 'stops' };
|
|
39
|
+
return starts ? { answer: 'record', args: ['--write-baseline', '--layout', '--reason', practice.reason] } : { answer: 'armable' };
|
|
40
|
+
};
|