glassbrain 0.2.0-alpha.7 → 0.2.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- package/dist/chunks/{chunk-GZSWZSSE.js → chunk-3CNHFYHG.js} +1317 -1650
- package/dist/chunks/chunk-7PWRJEZT.js +35 -0
- package/dist/chunks/chunk-CHOCJIOD.js +1340 -0
- package/dist/chunks/chunk-DHYIMBHU.js +960 -0
- package/dist/chunks/{chunk-FNTYI7VP.js → chunk-NDUF3CBQ.js} +340 -996
- package/dist/cli/glassbrain.js +338 -142
- package/dist/cli/viewer.js +218 -213
- package/dist/core.d.ts +1 -0
- package/dist/core.js +50 -30
- package/dist/diff.d.ts +9 -1
- package/dist/edit.d.ts +10 -0
- package/dist/index.js +57 -37
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +98 -76
- package/dist/layers.d.ts +3 -1
- package/dist/lint-report.d.ts +70 -0
- package/dist/profile.d.ts +10 -2
- package/dist/react/DetailPanel.d.ts +31 -1
- package/dist/react/DetailPanel.js +8 -4
- package/dist/react/EdgeMark.d.ts +22 -0
- package/dist/react/EditPanel.d.ts +6 -6
- package/dist/react/GraphCanvas.d.ts +17 -6
- package/dist/react/GraphCanvas.js +4 -4
- package/dist/react/Inspector.d.ts +72 -0
- package/dist/react/ObjectRef.d.ts +12 -0
- package/dist/react/Toolbar.d.ts +10 -3
- package/dist/react/ZoomBoundary.d.ts +4 -0
- package/dist/react/camera.d.ts +7 -0
- package/dist/react/i18n.d.ts +2 -2
- package/dist/react/icons.d.ts +7 -3
- package/dist/react/input.d.ts +5 -3
- package/dist/react/useSubgraph.d.ts +15 -0
- package/dist/shell.d.ts +1 -0
- package/dist/strings.d.ts +23 -2
- package/dist/style.d.ts +2 -0
- package/dist/subgraph.d.ts +13 -0
- package/dist/tree.d.ts +1 -0
- package/dist/view.d.ts +3 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-3KUNYSJE.js +0 -246
- package/dist/chunks/chunk-LK77SYCN.js +0 -100
- package/dist/chunks/chunk-Q35ZKWKF.js +0 -99
- package/dist/react/GlassPlanes.d.ts +0 -11
package/dist/cli/glassbrain.js
CHANGED
|
@@ -41,10 +41,10 @@ function mergeRuntimeSlices(slices) {
|
|
|
41
41
|
return dup.size ? { ...m, duplicateIds: [...dup] } : m;
|
|
42
42
|
}
|
|
43
43
|
var defOf = ({ origin: _o, origins: _os, ...rest }) => JSON.stringify(rest);
|
|
44
|
-
function addOrigin(
|
|
45
|
-
if (!origin) return
|
|
46
|
-
if (!
|
|
47
|
-
return
|
|
44
|
+
function addOrigin(list2, origin) {
|
|
45
|
+
if (!origin) return list2 ?? [];
|
|
46
|
+
if (!list2) return [origin];
|
|
47
|
+
return list2.includes(origin) ? list2 : [...list2, origin];
|
|
48
48
|
}
|
|
49
49
|
function originsOfNode(n) {
|
|
50
50
|
return n.origins?.length ? n.origins : n.origin ? [n.origin] : [];
|
|
@@ -102,7 +102,7 @@ function indexModel(model) {
|
|
|
102
102
|
// src/tree.ts
|
|
103
103
|
var object = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
104
104
|
var ref = (x) => typeof x === "string" || typeof x === "number" ? String(x) : void 0;
|
|
105
|
-
var
|
|
105
|
+
var stableJson = (x) => JSON.stringify(x, function(_key, value) {
|
|
106
106
|
return object(value) ? Object.fromEntries(Object.keys(value).sort().map((k) => [k, value[k]])) : value;
|
|
107
107
|
});
|
|
108
108
|
function treeDeclarations(raw, origin) {
|
|
@@ -168,12 +168,12 @@ function composeTrees(input) {
|
|
|
168
168
|
if (!r.origins.includes(slice.origin)) r.origins.push(slice.origin);
|
|
169
169
|
for (const [field, value] of [...Object.entries(d.fields), ...d.children === void 0 ? [] : [["children", d.children]]]) {
|
|
170
170
|
const previous = field === "children" ? r.children : r.fields[field];
|
|
171
|
-
if (r.locations.has(field) &&
|
|
171
|
+
if (r.locations.has(field) && stableJson(previous) !== stableJson(value)) {
|
|
172
172
|
errors.push({
|
|
173
173
|
origin: slice.origin,
|
|
174
174
|
node: d.id,
|
|
175
175
|
code: field === "children" ? "children-conflict" : "field-conflict",
|
|
176
|
-
message: `"${d.id}".${field}: ${r.locations.get(field)} = ${
|
|
176
|
+
message: `"${d.id}".${field}: ${r.locations.get(field)} = ${stableJson(previous)} conflicts with ${d.path} = ${stableJson(value)}.`
|
|
177
177
|
});
|
|
178
178
|
} else if (!r.locations.has(field)) {
|
|
179
179
|
if (field === "children") r.children = value;
|
|
@@ -248,7 +248,7 @@ function serializeTree(model) {
|
|
|
248
248
|
group.push(n);
|
|
249
249
|
groups.set(n.parent, group);
|
|
250
250
|
}
|
|
251
|
-
for (const
|
|
251
|
+
for (const list2 of groups.values()) list2.sort((a2, b) => (a2.order ?? Infinity) - (b.order ?? Infinity));
|
|
252
252
|
const roots = groups.get(void 0) ?? [];
|
|
253
253
|
const visited = /* @__PURE__ */ new Set(), stack = [...roots];
|
|
254
254
|
while (stack.length) {
|
|
@@ -299,6 +299,7 @@ var DEFAULT_LAYER = { id: "default", label: "Graph", z: 0, structural: true };
|
|
|
299
299
|
function kindTable(...entries) {
|
|
300
300
|
return Object.assign(/* @__PURE__ */ Object.create(null), ...entries);
|
|
301
301
|
}
|
|
302
|
+
var noDataReview = () => [];
|
|
302
303
|
function defineProfile(input) {
|
|
303
304
|
const layers = input.layers?.length ? input.layers : [DEFAULT_LAYER];
|
|
304
305
|
const fallbackLayer = layers[0].id;
|
|
@@ -307,6 +308,7 @@ function defineProfile(input) {
|
|
|
307
308
|
parse: input.parse ?? parsePhysical,
|
|
308
309
|
merge: input.merge ?? mergeSlices,
|
|
309
310
|
layers,
|
|
311
|
+
structuralLayers: input.structuralLayers ?? !input.layers?.length,
|
|
310
312
|
layerOf: input.layerOf ?? ((n) => n.layer ?? fallbackLayer),
|
|
311
313
|
nodePalette: kindTable(input.nodePalette),
|
|
312
314
|
edgePalette: kindTable(input.edgePalette),
|
|
@@ -318,7 +320,7 @@ function defineProfile(input) {
|
|
|
318
320
|
edgeClass: input.edgeClass ?? ((e) => e.kind ?? ""),
|
|
319
321
|
kindLabels: kindTable(input.kindLabels),
|
|
320
322
|
detail: input.detail ?? {},
|
|
321
|
-
validate: input.validate ??
|
|
323
|
+
validate: input.validate ?? noDataReview,
|
|
322
324
|
layoutEdge: input.layoutEdge ?? (() => false),
|
|
323
325
|
singleRoot: input.singleRoot ?? true,
|
|
324
326
|
rootLabel: input.rootLabel ?? "Overview"
|
|
@@ -975,12 +977,16 @@ var natural = (a2, b) => a2.localeCompare(b, void 0, { numeric: true });
|
|
|
975
977
|
function load(spec, opts = {}) {
|
|
976
978
|
const tree2 = readSpec(spec, opts.cwd);
|
|
977
979
|
const abs = path.resolve(opts.cwd ?? process.cwd(), spec);
|
|
980
|
+
let profileFile = "_profile.json";
|
|
978
981
|
if (fs.existsSync(abs) && !tree2.files.some((f) => f.path === "_profile.json")) {
|
|
979
982
|
const dir = tree2.isDir ? abs : path.dirname(abs);
|
|
980
983
|
const candidates = [path.join(dir, "_profile.json")];
|
|
981
984
|
if (/^\d+-/.test(path.basename(dir))) candidates.push(path.join(path.dirname(dir), "_profile.json"));
|
|
982
985
|
const file = candidates.find((p) => fs.existsSync(p));
|
|
983
|
-
if (file)
|
|
986
|
+
if (file) {
|
|
987
|
+
profileFile = path.relative(dir, file);
|
|
988
|
+
tree2.files.push({ path: "_profile.json", text: fs.readFileSync(file, "utf8") });
|
|
989
|
+
}
|
|
984
990
|
}
|
|
985
991
|
const metaErrors = [];
|
|
986
992
|
const meta = (name) => {
|
|
@@ -989,12 +995,12 @@ function load(spec, opts = {}) {
|
|
|
989
995
|
try {
|
|
990
996
|
const value = JSON.parse(f.text);
|
|
991
997
|
if (name === "_profile.json" && (!value || typeof value !== "object" || Array.isArray(value))) {
|
|
992
|
-
metaErrors.push({ file: name, message: "expected a profile object" });
|
|
998
|
+
metaErrors.push({ file: name === "_profile.json" ? profileFile : name, message: "expected a profile object" });
|
|
993
999
|
return null;
|
|
994
1000
|
}
|
|
995
1001
|
return value;
|
|
996
1002
|
} catch (e) {
|
|
997
|
-
metaErrors.push({ file: name, message: `not JSON: ${e.message}` });
|
|
1003
|
+
metaErrors.push({ file: name === "_profile.json" ? profileFile : name, message: `not JSON: ${e.message}` });
|
|
998
1004
|
return null;
|
|
999
1005
|
}
|
|
1000
1006
|
};
|
|
@@ -1007,7 +1013,8 @@ function load(spec, opts = {}) {
|
|
|
1007
1013
|
return null;
|
|
1008
1014
|
}
|
|
1009
1015
|
};
|
|
1010
|
-
const
|
|
1016
|
+
const embedded = bundled();
|
|
1017
|
+
const explicit = embedded ?? meta("_profile.json");
|
|
1011
1018
|
const data = tree2.files.filter((f) => !META.has(path.posix.basename(f.path)));
|
|
1012
1019
|
const isJson = (f) => /\.json$/i.test(f.path);
|
|
1013
1020
|
const dirs = [...new Set(data.filter((f) => f.path.includes("/")).map((f) => f.path.split("/")[0]))].sort(natural);
|
|
@@ -1027,6 +1034,7 @@ function load(spec, opts = {}) {
|
|
|
1027
1034
|
});
|
|
1028
1035
|
return {
|
|
1029
1036
|
title: tree2.title,
|
|
1037
|
+
paletteTarget: embedded ? { file: tree2.files[0].path, field: "profile.nodePalette" } : { file: profileFile, field: "nodePalette" },
|
|
1030
1038
|
versions,
|
|
1031
1039
|
// A `_profile.json` that only carries demo metadata used to *replace* the adapter's profile, taking its palette
|
|
1032
1040
|
// with it: a converted graph then drew every kind as skeleton. Explicit keys win; the adapter fills the rest.
|
|
@@ -1048,7 +1056,7 @@ var DIFF_FLAG = {
|
|
|
1048
1056
|
merged: "gb-merged"
|
|
1049
1057
|
};
|
|
1050
1058
|
var KEPT_FLAG = "gb-kept";
|
|
1051
|
-
var
|
|
1059
|
+
var stable = (v, ignore) => {
|
|
1052
1060
|
const walk2 = (x) => {
|
|
1053
1061
|
if (Array.isArray(x)) return x.map(walk2);
|
|
1054
1062
|
if (x && typeof x === "object") {
|
|
@@ -1066,7 +1074,7 @@ function changedFields(a2, b, ignore) {
|
|
|
1066
1074
|
const ob = b ?? {};
|
|
1067
1075
|
const keys = new Set([...Object.keys(oa), ...Object.keys(ob)].filter((k) => !ignore.has(k)));
|
|
1068
1076
|
const out = [];
|
|
1069
|
-
for (const k of keys) if (
|
|
1077
|
+
for (const k of keys) if (stable(oa[k], ignore) !== stable(ob[k], ignore)) out.push(k);
|
|
1070
1078
|
return out.sort();
|
|
1071
1079
|
}
|
|
1072
1080
|
var EDIT_CAP = 512;
|
|
@@ -1101,10 +1109,17 @@ function editDistance(a2, b) {
|
|
|
1101
1109
|
return prev[m] + over;
|
|
1102
1110
|
}
|
|
1103
1111
|
function changeAmount(a2, b, fields, ignore) {
|
|
1104
|
-
const pick = (n) => fields.map((k) =>
|
|
1112
|
+
const pick = (n) => fields.map((k) => stable((n.data ?? {})[k], ignore)).join("\0");
|
|
1105
1113
|
return editDistance(pick(a2), pick(b)) + editDistance(a2.label ?? "", b.label ?? "");
|
|
1106
1114
|
}
|
|
1107
|
-
var edgeKey = (e) =>
|
|
1115
|
+
var edgeKey = (e) => JSON.stringify([e.source, e.target, e.kind ?? ""]);
|
|
1116
|
+
function edgeContent(edge) {
|
|
1117
|
+
const { source, target, from, to, kind, ...data } = edge.data ?? {};
|
|
1118
|
+
return { ...data, label: edge.label ?? data.label, weight: edge.weight ?? data.weight };
|
|
1119
|
+
}
|
|
1120
|
+
function edgeContentChanged(before, after, ignore) {
|
|
1121
|
+
return stable(edgeContent(before), ignore) !== stable(edgeContent(after), ignore);
|
|
1122
|
+
}
|
|
1108
1123
|
var withFlag = (x, flag) => ({ ...x, flags: [...x.flags ?? [], flag] });
|
|
1109
1124
|
function diffModels(before, after, opts = {}) {
|
|
1110
1125
|
const ignore = new Set(opts.ignore ?? []);
|
|
@@ -1120,9 +1135,9 @@ function diffModels(before, after, opts = {}) {
|
|
|
1120
1135
|
const afterByKey = /* @__PURE__ */ new Map();
|
|
1121
1136
|
for (const b of after.nodes) if (!afterByKey.has(keyOf(b))) afterByKey.set(keyOf(b), b);
|
|
1122
1137
|
const resolve = /* @__PURE__ */ new Map();
|
|
1123
|
-
for (const [k,
|
|
1138
|
+
for (const [k, list2] of beforeByKey) {
|
|
1124
1139
|
const b = afterByKey.get(k);
|
|
1125
|
-
if (b) resolve.set(
|
|
1140
|
+
if (b) resolve.set(list2[0].id, b.id);
|
|
1126
1141
|
}
|
|
1127
1142
|
const ref2 = (id) => id === void 0 ? void 0 : resolve.get(id) ?? id;
|
|
1128
1143
|
const consumed = new Set(resolve.keys());
|
|
@@ -1218,7 +1233,9 @@ function diffModels(before, after, opts = {}) {
|
|
|
1218
1233
|
);
|
|
1219
1234
|
const edges = [];
|
|
1220
1235
|
for (const [k, e] of afterEdges) {
|
|
1221
|
-
|
|
1236
|
+
const previous = beforeEdges.get(k);
|
|
1237
|
+
const flag = !previous ? DIFF_FLAG.added : edgeContentChanged(previous, e, ignore) ? DIFF_FLAG.changed : KEPT_FLAG;
|
|
1238
|
+
edges.push(withFlag(e, flag));
|
|
1222
1239
|
}
|
|
1223
1240
|
for (const [k, e] of beforeEdges) {
|
|
1224
1241
|
if (afterEdges.has(k)) continue;
|
|
@@ -1241,7 +1258,9 @@ function diffModels(before, after, opts = {}) {
|
|
|
1241
1258
|
prevData,
|
|
1242
1259
|
labels,
|
|
1243
1260
|
before,
|
|
1244
|
-
after
|
|
1261
|
+
after,
|
|
1262
|
+
ignore: opts.ignore,
|
|
1263
|
+
options: opts
|
|
1245
1264
|
};
|
|
1246
1265
|
}
|
|
1247
1266
|
|
|
@@ -1666,6 +1685,9 @@ function checkSchema(root, value, limit = 50) {
|
|
|
1666
1685
|
return out;
|
|
1667
1686
|
}
|
|
1668
1687
|
|
|
1688
|
+
// src/shell.ts
|
|
1689
|
+
var shellArg = (s) => "'" + s.replace(/'/g, `'"'"'`) + "'";
|
|
1690
|
+
|
|
1669
1691
|
// src/kinds.ts
|
|
1670
1692
|
function kindDistribution(items) {
|
|
1671
1693
|
const counts = /* @__PURE__ */ new Map();
|
|
@@ -1688,13 +1710,6 @@ function kindDistribution(items) {
|
|
|
1688
1710
|
function kindHeading(space, d) {
|
|
1689
1711
|
return `${space} kinds: ${d.count}${d.reason ? ` (${d.reason})` : ""}`;
|
|
1690
1712
|
}
|
|
1691
|
-
function kindSummary(space, d, labels = {}) {
|
|
1692
|
-
return [
|
|
1693
|
-
kindHeading(space, d),
|
|
1694
|
-
...d.sizes.map((row) => `${Object.hasOwn(labels, row.kind) ? labels[row.kind] : row.kind} ${row.count}/${d.total}`),
|
|
1695
|
-
...d.missing ? [`(no kind) ${d.missing}/${d.total}`] : []
|
|
1696
|
-
].join(" | ");
|
|
1697
|
-
}
|
|
1698
1713
|
|
|
1699
1714
|
// cli/shape.ts
|
|
1700
1715
|
var rounded = (x) => +x.toFixed(6);
|
|
@@ -1930,6 +1945,8 @@ var misplacedField = {
|
|
|
1930
1945
|
if (!found.length) continue;
|
|
1931
1946
|
out.push({
|
|
1932
1947
|
node: n.id,
|
|
1948
|
+
where: n.origins?.length === 1 ? n.origin : void 0,
|
|
1949
|
+
facts: { type: "misplaced", fields: found },
|
|
1933
1950
|
message: `"${n.id}" keeps ${found.map((k) => `"${k}"`).join(", ")} under "data"; only top-level fields are read`,
|
|
1934
1951
|
fix: `move ${found.length > 1 ? "them" : "it"} up one level, next to "id":
|
|
1935
1952
|
- { "id": "${n.id}", "data": { ${found.map((k) => `"${k}": \u2026`).join(", ")} } }
|
|
@@ -2011,16 +2028,16 @@ var conflictingDefinition = {
|
|
|
2011
2028
|
var danglingEdge = {
|
|
2012
2029
|
code: "dangling-edge",
|
|
2013
2030
|
level: "error",
|
|
2014
|
-
run: ({ model, index
|
|
2031
|
+
run: ({ model, index }) => {
|
|
2015
2032
|
const out = [];
|
|
2016
2033
|
for (const e of model.edges) {
|
|
2017
|
-
const missing = [
|
|
2018
|
-
if (!missing) continue;
|
|
2034
|
+
const missing = [...new Set([e.source, e.target].filter((id) => !index.has(id)))];
|
|
2035
|
+
if (!missing.length) continue;
|
|
2019
2036
|
out.push({
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2037
|
+
where: e.origin,
|
|
2038
|
+
facts: { type: "dangling", source: e.source, target: e.target, kind: e.kind, missing },
|
|
2039
|
+
message: `${e.source} \u2192 ${e.target}: missing ${missing.join(", ")}`,
|
|
2040
|
+
fix: "Add the missing node or correct the endpoint ID."
|
|
2024
2041
|
});
|
|
2025
2042
|
}
|
|
2026
2043
|
return out;
|
|
@@ -2029,20 +2046,32 @@ var danglingEdge = {
|
|
|
2029
2046
|
var duplicateEdge = {
|
|
2030
2047
|
code: "duplicate-edge",
|
|
2031
2048
|
level: "warn",
|
|
2032
|
-
run: ({ model }) => {
|
|
2033
|
-
const
|
|
2049
|
+
run: ({ model, prefix }) => {
|
|
2050
|
+
const copies = /* @__PURE__ */ new Map();
|
|
2034
2051
|
for (const e of model.edges) {
|
|
2035
|
-
const key =
|
|
2036
|
-
|
|
2052
|
+
const key = JSON.stringify([e.source, e.target, e.kind ?? ""]);
|
|
2053
|
+
const group = copies.get(key);
|
|
2054
|
+
if (group) group.push(e);
|
|
2055
|
+
else copies.set(key, [e]);
|
|
2037
2056
|
}
|
|
2038
2057
|
const out = [];
|
|
2039
|
-
for (const
|
|
2040
|
-
if (
|
|
2041
|
-
const
|
|
2058
|
+
for (const edges of copies.values()) {
|
|
2059
|
+
if (edges.length < 2) continue;
|
|
2060
|
+
const { source, target, kind } = edges[0];
|
|
2061
|
+
const files = [...new Set(edges.flatMap((e) => e.origin ? [prefix + e.origin] : []))].sort();
|
|
2062
|
+
const records = edges.map((e) => ({ ...e.data, ...e.label !== void 0 ? { label: e.label } : {}, ...e.weight !== void 0 ? { weight: e.weight } : {} }));
|
|
2063
|
+
const differences = [...new Set(records.flatMap(Object.keys))].flatMap((field) => {
|
|
2064
|
+
const copies2 = records.map((r, i) => ({
|
|
2065
|
+
file: edges[i].origin ? prefix + edges[i].origin : void 0,
|
|
2066
|
+
value: Object.hasOwn(r, field) ? stableJson(r[field]) : void 0
|
|
2067
|
+
}));
|
|
2068
|
+
return new Set(copies2.map((c) => c.value)).size > 1 ? [{ field, copies: copies2 }] : [];
|
|
2069
|
+
});
|
|
2070
|
+
const differing = differences.length > 0;
|
|
2042
2071
|
out.push({
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2072
|
+
facts: { type: "duplicate", source, target, kind, count: edges.length, files, differing, ...differing ? { differences } : {} },
|
|
2073
|
+
message: `${source} \u2192 ${target}${kind ? ` (${kind})` : ""} appears ${edges.length} times`,
|
|
2074
|
+
fix: differing ? "Compare the differing attributes and combine the intended values in one entry." : "Keep one copy of this relation."
|
|
2046
2075
|
});
|
|
2047
2076
|
}
|
|
2048
2077
|
return out;
|
|
@@ -2053,10 +2082,9 @@ var unevenKinds = {
|
|
|
2053
2082
|
level: "warn",
|
|
2054
2083
|
run: ({ model }) => analyzeShape(model.nodes).branches.filter((b) => b.score >= 0.2).sort((a2, b) => b.score - a2.score).map((b) => ({
|
|
2055
2084
|
node: b.parent,
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
glassbrain restructure <path> --dry-run --json shows grouping suggestions; no content is deleted.`
|
|
2085
|
+
facts: { type: "uneven", closest: b.closest },
|
|
2086
|
+
message: `Sibling branches under "${b.parent}" differ in shape.`,
|
|
2087
|
+
fix: `Inspect ${b.closest?.a} and ${b.closest?.b} with glassbrain tree <path> --path ${shellArg(b.parent)}. Group them only if they share a subject.`
|
|
2060
2088
|
}))
|
|
2061
2089
|
};
|
|
2062
2090
|
var wideNode = {
|
|
@@ -2064,17 +2092,16 @@ var wideNode = {
|
|
|
2064
2092
|
level: "warn",
|
|
2065
2093
|
run: ({ children, labelOf, layers }) => {
|
|
2066
2094
|
const out = [];
|
|
2067
|
-
|
|
2095
|
+
const wideParents = [...children].filter(([, kids]) => kids.length > WIDE_WARN).sort(([, a2], [, b]) => b.length - a2.length);
|
|
2096
|
+
for (const [id, kids] of wideParents) {
|
|
2068
2097
|
const n = kids.length;
|
|
2069
|
-
if (n <= WIDE_WARN) continue;
|
|
2070
2098
|
const isAnchor = layers.anchor === id;
|
|
2071
2099
|
const what = isAnchor ? `${n} layers` : `${n} children`;
|
|
2072
2100
|
out.push({
|
|
2073
2101
|
node: id,
|
|
2102
|
+
facts: { type: "wide", count: n, layers: isAnchor },
|
|
2074
2103
|
message: `"${labelOf(id)}" holds ${what}; ${isAnchor ? "review whether each floor is a major section" : "review whether this is one natural collection"}`,
|
|
2075
|
-
fix: `
|
|
2076
|
-
glassbrain restructure <path> --dry-run shows candidate groups and their ids.
|
|
2077
|
-
Prefer fewer than 5 children per group; this is guidance, not a quota. Keep a natural collection together; add a named group only when it explains membership.${isAnchor ? "\n Each root child can be shown as a floor; use the view switch to read them together." : ""}`
|
|
2104
|
+
fix: `Inspect glassbrain tree <path> --path ${shellArg(id)}. ${isAnchor ? "Keep major sections as floors." : "Group by subject where useful."}`
|
|
2078
2105
|
});
|
|
2079
2106
|
}
|
|
2080
2107
|
return out;
|
|
@@ -2083,18 +2110,14 @@ var wideNode = {
|
|
|
2083
2110
|
var uncoloredKind = {
|
|
2084
2111
|
code: "uncolored-kind",
|
|
2085
2112
|
level: "warn",
|
|
2086
|
-
run: ({ model, profile }) => {
|
|
2113
|
+
run: ({ model, profile, paletteTarget }) => {
|
|
2087
2114
|
const used = [...new Set(model.nodes.map((n) => n.kind).filter((k) => !!k))];
|
|
2088
2115
|
const missing = used.filter((k) => !Object.hasOwn(profile.nodePalette, k));
|
|
2089
2116
|
if (!missing.length) return [];
|
|
2090
|
-
const shown = missing.slice(0, 5);
|
|
2091
|
-
const palette = Object.fromEntries(shown.map((k, i) => [k, ["#6366f1", "#f59e0b", "#10b981", "#ef4444", "#8b5cf6"][i % 5]]));
|
|
2092
2117
|
return [{
|
|
2118
|
+
facts: { type: "palette", kinds: missing, target: paletteTarget },
|
|
2093
2119
|
message: `${missing.length} node kind${missing.length > 1 ? "s are" : " is"} not in nodePalette, so ${missing.length > 1 ? "they are" : "it is"} drawn as skeleton: ${names(missing)}`,
|
|
2094
|
-
fix: `
|
|
2095
|
-
{ "nodePalette": ${JSON.stringify(palette)} }
|
|
2096
|
-
${missing.length > shown.length ? ` \u2026 and ${missing.length - shown.length} more; every kind in the data needs an entry.
|
|
2097
|
-
` : ""} Colors that land closer than 10 in Lab distance read as one kind (lint notes it).`
|
|
2120
|
+
fix: `Add a distinct color for each missing kind in ${paletteTarget?.file ?? "_profile.json"} under ${paletteTarget?.field ?? "nodePalette"}.`
|
|
2098
2121
|
}];
|
|
2099
2122
|
}
|
|
2100
2123
|
};
|
|
@@ -2104,8 +2127,9 @@ var kindNote = {
|
|
|
2104
2127
|
run: ({ model }) => ["node", "edge"].flatMap((space) => {
|
|
2105
2128
|
const d = kindDistribution(space === "node" ? model.nodes : model.edges);
|
|
2106
2129
|
return d.reason ? [{
|
|
2130
|
+
facts: { type: "vocabulary", space, kinds: d.sizes.map((row) => row.kind) },
|
|
2107
2131
|
message: `${kindHeading(space, d)}; ${d.typed} typed ${space}s`,
|
|
2108
|
-
fix: "
|
|
2132
|
+
fix: "Merge synonymous kinds. Keep distinct meanings, including rare ones."
|
|
2109
2133
|
}] : [];
|
|
2110
2134
|
})
|
|
2111
2135
|
};
|
|
@@ -2115,19 +2139,19 @@ var lowCoverage = {
|
|
|
2115
2139
|
run: ({ model, children }) => {
|
|
2116
2140
|
const leaves = model.nodes.filter((n) => !children.has(n.id));
|
|
2117
2141
|
if (leaves.length < 20) return [];
|
|
2118
|
-
const
|
|
2119
|
-
if (
|
|
2142
|
+
const missing = leaves.filter((n) => !(typeof n.data?.description === "string" && n.data.description.trim())).map((n) => n.id);
|
|
2143
|
+
if (missing.length <= leaves.length / 2) return [];
|
|
2120
2144
|
return [{
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2145
|
+
facts: { type: "coverage", leaves: missing },
|
|
2146
|
+
message: `${missing.length} of ${leaves.length} leaves have no description`,
|
|
2147
|
+
fix: "Add a sentence in each leaf\u2019s description."
|
|
2124
2148
|
}];
|
|
2125
2149
|
}
|
|
2126
2150
|
};
|
|
2127
2151
|
var colorNote = {
|
|
2128
2152
|
code: "color-note",
|
|
2129
2153
|
level: "info",
|
|
2130
|
-
run: ({ profile }) => {
|
|
2154
|
+
run: ({ profile, paletteTarget }) => {
|
|
2131
2155
|
const colors = Object.entries(profile.nodePalette).map(([kind, hex]) => ({ kind, lab: hexLab(hex) }));
|
|
2132
2156
|
const usable = colors.filter((c) => c.lab !== null);
|
|
2133
2157
|
if (usable.length < 6) return [];
|
|
@@ -2140,6 +2164,7 @@ var colorNote = {
|
|
|
2140
2164
|
}
|
|
2141
2165
|
if (worst.d >= 10) return [];
|
|
2142
2166
|
return [{
|
|
2167
|
+
facts: { type: "colors", kinds: [worst.a, worst.b], target: paletteTarget },
|
|
2143
2168
|
message: `"${worst.a}" and "${worst.b}" are ${worst.d.toFixed(1)} apart in color (under 10 reads as the same kind)`,
|
|
2144
2169
|
fix: `push them apart in lightness, not just hue:
|
|
2145
2170
|
"${worst.a}": darker \u2192 e.g. #4338ca
|
|
@@ -2172,6 +2197,8 @@ function checkGraph(model, profile, opts) {
|
|
|
2172
2197
|
else children.set(n.parent, [n]);
|
|
2173
2198
|
}
|
|
2174
2199
|
const ctx = {
|
|
2200
|
+
paletteTarget: opts.paletteTarget,
|
|
2201
|
+
prefix: opts.prefix ?? "",
|
|
2175
2202
|
model,
|
|
2176
2203
|
profile,
|
|
2177
2204
|
where: opts.where,
|
|
@@ -2185,7 +2212,9 @@ function checkGraph(model, profile, opts) {
|
|
|
2185
2212
|
const out = applicable.filter((rule) => !invalidStructure || rule !== unevenKinds).flatMap((rule) => rule.run(ctx).map((issue) => ({
|
|
2186
2213
|
level: issue.level ?? rule.level,
|
|
2187
2214
|
code: rule.code,
|
|
2188
|
-
where: opts.
|
|
2215
|
+
scope: issue.where ? void 0 : opts.prefix ? "version" : "graph",
|
|
2216
|
+
where: issue.where ? (opts.prefix ?? "") + issue.where : opts.where,
|
|
2217
|
+
...issue.facts ? { facts: issue.facts } : {},
|
|
2189
2218
|
message: issue.message,
|
|
2190
2219
|
...issue.fix !== void 0 ? { fix: issue.fix } : {},
|
|
2191
2220
|
...issue.node !== void 0 ? { node: issue.node } : {}
|
|
@@ -2264,17 +2293,17 @@ function derivedProfile(base2, sources, json, force) {
|
|
|
2264
2293
|
}
|
|
2265
2294
|
var modelOf = (v, profile) => parseSources(v.sources, profile);
|
|
2266
2295
|
var RANK = { error: 0, warn: 1, info: 2 };
|
|
2267
|
-
function inspectProfile(json) {
|
|
2296
|
+
function inspectProfile(json, file = "_profile.json") {
|
|
2268
2297
|
if (!json) return [];
|
|
2269
|
-
const out = checkSchema(profile_schema_default, json).map((e) => ({ level: "error", code: "schema", where:
|
|
2298
|
+
const out = checkSchema(profile_schema_default, json).map((e) => ({ level: "error", code: "schema", where: file, message: `${e.path}: ${e.message}` }));
|
|
2270
2299
|
if (typeof json !== "object" || Array.isArray(json)) return out;
|
|
2271
2300
|
const retired = ["layers", "singleRoot"].filter((k) => k in json);
|
|
2272
2301
|
if (retired.length) out.push({
|
|
2273
2302
|
level: "warn",
|
|
2274
2303
|
code: "schema",
|
|
2275
|
-
where:
|
|
2304
|
+
where: file,
|
|
2276
2305
|
message: `${retired.map((k) => `"${k}"`).join(" and ")} no longer configure anything: layers come from the ordered children structure`,
|
|
2277
|
-
fix: '
|
|
2306
|
+
fix: 'Remove the retired keys from the profile. Use "appearance": { "defaultLayers": false } to open flat.'
|
|
2278
2307
|
});
|
|
2279
2308
|
return out;
|
|
2280
2309
|
}
|
|
@@ -2298,12 +2327,12 @@ function inspectGraph(model, profile, sources, opts) {
|
|
|
2298
2327
|
for (const e of checkSchema(graph_schema_default, doc)) out.push({ level: "error", code: "schema", where: prefix + f.origin, message: `${e.path}: ${e.message}` });
|
|
2299
2328
|
}
|
|
2300
2329
|
for (const e of model.errors) if (!notJson.has(e.origin)) out.push({ level: "error", code: e.code ?? "schema", where: prefix + e.origin, message: e.message, node: e.node });
|
|
2301
|
-
out.push(...checkGraph(model, profile, { where: opts.where, quality: opts.quality }));
|
|
2302
|
-
if (!model.nodes.length) out.push({ level: "warn", code: "schema", where: opts.where, message: "No nodes found" });
|
|
2330
|
+
out.push(...checkGraph(model, profile, { where: opts.where, prefix, paletteTarget: opts.paletteTarget, quality: opts.quality }));
|
|
2331
|
+
if (!model.nodes.length) out.push({ level: "warn", code: "schema", where: opts.where, scope: prefix ? "version" : "graph", message: "No nodes found" });
|
|
2303
2332
|
return out.sort((a2, b) => RANK[a2.level] - RANK[b.level]);
|
|
2304
2333
|
}
|
|
2305
2334
|
function lint(loaded) {
|
|
2306
|
-
const out = inspectProfile(loaded.profile);
|
|
2335
|
+
const out = inspectProfile(loaded.profile, loaded.paletteTarget?.file);
|
|
2307
2336
|
const profile = profileOf(loaded);
|
|
2308
2337
|
for (const e of loaded.metaErrors) out.push({ level: "error", code: "schema", where: e.file, message: e.message });
|
|
2309
2338
|
const latest = loaded.raw[loaded.raw.length - 1]?.version;
|
|
@@ -2314,7 +2343,7 @@ function lint(loaded) {
|
|
|
2314
2343
|
modelOf(v, profile),
|
|
2315
2344
|
profile,
|
|
2316
2345
|
files.filter((f) => /\.json$/i.test(f.path)).map((f) => ({ origin: f.path, text: f.text })),
|
|
2317
|
-
{ where: prefix || loaded.title, prefix, native: (v.format ?? loaded.format) === "native", quality: version === latest }
|
|
2346
|
+
{ where: prefix || loaded.title, prefix, paletteTarget: loaded.paletteTarget, native: (v.format ?? loaded.format) === "native", quality: version === latest }
|
|
2318
2347
|
));
|
|
2319
2348
|
}
|
|
2320
2349
|
return out.sort((a2, b) => RANK[a2.level] - RANK[b.level]);
|
|
@@ -2365,22 +2394,181 @@ function diffText(s, labelOf, limit = 20) {
|
|
|
2365
2394
|
L.push(`edges: +${s.edges.added} \u2212${s.edges.removed} (${s.edges.kept} unchanged)`);
|
|
2366
2395
|
const SIGN = { added: "+", removed: "\u2212", changed: "~", moved: "\u21B7", merged: "\u21C9" };
|
|
2367
2396
|
for (const kind of ["added", "removed", "changed", "moved", "merged"]) {
|
|
2368
|
-
const
|
|
2369
|
-
if (!
|
|
2370
|
-
L.push("", `${kind} (${
|
|
2371
|
-
for (const c of
|
|
2372
|
-
if (
|
|
2397
|
+
const list2 = s.changes.filter((c) => c.kind === kind);
|
|
2398
|
+
if (!list2.length) continue;
|
|
2399
|
+
L.push("", `${kind} (${list2.length})`);
|
|
2400
|
+
for (const c of list2.slice(0, limit)) L.push(` ${SIGN[kind]} ${c.label}${c.label !== c.id ? ` [${c.id}]` : ""}${c.detail ? ` \u2014 ${c.detail}` : ""}`);
|
|
2401
|
+
if (list2.length > limit) L.push(` \u2026 ${list2.length - limit} more (--all)`);
|
|
2373
2402
|
}
|
|
2374
2403
|
for (const kind of ["added", "removed"]) {
|
|
2375
|
-
const
|
|
2376
|
-
if (!
|
|
2377
|
-
L.push("", `edges ${kind} (${
|
|
2378
|
-
for (const e of
|
|
2379
|
-
if (
|
|
2404
|
+
const list2 = s.edgeChanges.filter((e) => e.kind === kind);
|
|
2405
|
+
if (!list2.length) continue;
|
|
2406
|
+
L.push("", `edges ${kind} (${list2.length})`);
|
|
2407
|
+
for (const e of list2.slice(0, limit)) L.push(` ${kind === "added" ? "+" : "\u2212"} ${labelOf(e.source)} \u2192 ${labelOf(e.target)}${e.relation ? ` (${e.relation})` : ""}`);
|
|
2408
|
+
if (list2.length > limit) L.push(` \u2026 ${list2.length - limit} more (--all)`);
|
|
2380
2409
|
}
|
|
2381
2410
|
return L.join("\n");
|
|
2382
2411
|
}
|
|
2383
2412
|
|
|
2413
|
+
// src/lint-report.ts
|
|
2414
|
+
var headings = {
|
|
2415
|
+
"bad-structure": "Sibling subtrees differ in shape or node kind.",
|
|
2416
|
+
"wide-node": "These parent nodes have many direct children.",
|
|
2417
|
+
"duplicate-edge": "Multiple edge records have the same source ID, kind and target ID.",
|
|
2418
|
+
"dangling-edge": "An edge references a missing node ID. Add the node or correct the endpoint.",
|
|
2419
|
+
"field-conflict": "Choose one value for the conflicting node field.",
|
|
2420
|
+
"children-conflict": "Use the same ordered child IDs in each declaration, with each child listed once.",
|
|
2421
|
+
"parent-error": "Repair the parent\u2013child placement.",
|
|
2422
|
+
"duplicate-id": "Resolve conflicting definitions of a node ID.",
|
|
2423
|
+
"schema": "Check the named fields against the graph or profile format.",
|
|
2424
|
+
"kind-note": "Review kind names. Merge synonyms and keep distinct meanings.",
|
|
2425
|
+
"uncolored-kind": "Assign a distinct color to each listed node kind.",
|
|
2426
|
+
"coverage-note": "These leaf nodes have no `description`. Add a sentence explaining each one.",
|
|
2427
|
+
"color-note": "These node kinds have similar colors. Make one lighter or darker."
|
|
2428
|
+
};
|
|
2429
|
+
function delimiter(text, minimum) {
|
|
2430
|
+
let length = minimum;
|
|
2431
|
+
for (const run of text.matchAll(/`+/g)) length = Math.max(length, run[0].length + 1);
|
|
2432
|
+
return "`".repeat(length);
|
|
2433
|
+
}
|
|
2434
|
+
function codeBlock(text, indent, language = "") {
|
|
2435
|
+
const fence = delimiter(text, 3);
|
|
2436
|
+
return [indent + fence + language, ...text.split("\n").map((line) => indent + line), indent + fence];
|
|
2437
|
+
}
|
|
2438
|
+
function literal(value) {
|
|
2439
|
+
const text = /[\u0000-\u001f]|^\s|\s$|^$/.test(value) ? JSON.stringify(value) : value;
|
|
2440
|
+
const fence = delimiter(text, 1);
|
|
2441
|
+
const pad = text.startsWith("`") || text.endsWith("`") ? " " : "";
|
|
2442
|
+
return fence + pad + text + pad + fence;
|
|
2443
|
+
}
|
|
2444
|
+
function plain(value) {
|
|
2445
|
+
return value.replace(/[\\`*_\[\]<>#&~!|]/g, "\\$&").replace(/[\r\n]+/g, " ");
|
|
2446
|
+
}
|
|
2447
|
+
var list = (values) => values.map(literal).join(", ");
|
|
2448
|
+
function groupBy(items, keyOf) {
|
|
2449
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2450
|
+
for (const item of items) {
|
|
2451
|
+
const key = keyOf(item);
|
|
2452
|
+
const group = groups.get(key);
|
|
2453
|
+
if (group) group.push(item);
|
|
2454
|
+
else groups.set(key, [item]);
|
|
2455
|
+
}
|
|
2456
|
+
return groups;
|
|
2457
|
+
}
|
|
2458
|
+
function duplicateLines(edges) {
|
|
2459
|
+
const lines = [];
|
|
2460
|
+
const batches = [...groupBy(edges, (e) => JSON.stringify([e.differing, e.count, e.files])).values()];
|
|
2461
|
+
for (const group of batches.sort((a2, b) => Number(b[0].differing) - Number(a2[0].differing))) {
|
|
2462
|
+
const { differing, count: count2, files } = group[0];
|
|
2463
|
+
const redundant = group.length * (count2 - 1);
|
|
2464
|
+
const action = differing ? "Attributes differ. Decide which values to retain before merging." : `Remove ${redundant} identical ${redundant === 1 ? "copy" : "copies"}, leaving one record per edge.`;
|
|
2465
|
+
lines.push(` - ${group.length} ${group.length === 1 ? "edge" : "edges"}, ${count2} copies each${files.length ? (files.length === 1 ? " in " : " across ") + list(files) : ""}. ${action}`);
|
|
2466
|
+
for (const [source, outgoing] of groupBy(group, (e) => e.source)) {
|
|
2467
|
+
const targets = [...groupBy(outgoing, (e) => JSON.stringify(e.kind ?? null)).values()].map((sameKind) => {
|
|
2468
|
+
const kind = sameKind[0].kind;
|
|
2469
|
+
return `${kind === void 0 ? "(no kind)" : literal(kind)} \u2192 ${list(sameKind.map((e) => e.target))}`;
|
|
2470
|
+
});
|
|
2471
|
+
lines.push(` - From node ID ${literal(source)}: ${targets.join(" | ")}`);
|
|
2472
|
+
for (const edge of outgoing) for (const diff2 of edge.differences ?? []) {
|
|
2473
|
+
const values = diff2.copies.map((c) => (c.value === void 0 ? "(absent)" : literal(c.value)) + (c.file ? " in " + literal(c.file) : ""));
|
|
2474
|
+
lines.push(` - ${edge.kind ? "Kind " + literal(edge.kind) + ", " : ""}target ID ${literal(edge.target)}, field ${literal(diff2.field)}: ${values.join(" / ")}.`);
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
return lines;
|
|
2479
|
+
}
|
|
2480
|
+
function ruleLines(rows, input) {
|
|
2481
|
+
const lines = [];
|
|
2482
|
+
const duplicates = [];
|
|
2483
|
+
const uneven = [];
|
|
2484
|
+
for (const f of rows) {
|
|
2485
|
+
const d = f.facts;
|
|
2486
|
+
const place = f.where && f.scope !== "graph" ? literal(f.where) : "";
|
|
2487
|
+
const location = place ? " in " + (f.scope === "version" ? "version " : "") + place : "";
|
|
2488
|
+
switch (d?.type) {
|
|
2489
|
+
case "duplicate":
|
|
2490
|
+
duplicates.push(d);
|
|
2491
|
+
break;
|
|
2492
|
+
case "uneven":
|
|
2493
|
+
uneven.push(f);
|
|
2494
|
+
break;
|
|
2495
|
+
case "wide":
|
|
2496
|
+
lines.push(` - Node ID ${literal(f.node)}: ${d.count} direct children${d.layers ? " (floors; check that each is a major section)" : ""}.`);
|
|
2497
|
+
break;
|
|
2498
|
+
case "dangling":
|
|
2499
|
+
lines.push(` - Source ID ${literal(d.source)} \u2192 target ID ${literal(d.target)}${d.kind ? ", edge kind " + literal(d.kind) : ""}${location}. Missing: ${list(d.missing)}.`);
|
|
2500
|
+
break;
|
|
2501
|
+
case "misplaced":
|
|
2502
|
+
lines.push(` - On node ID ${literal(f.node)}${location}, move ${list(d.fields)} out of \`data\` and beside \`id\`.`);
|
|
2503
|
+
break;
|
|
2504
|
+
case "palette":
|
|
2505
|
+
case "colors": {
|
|
2506
|
+
const target = d.target ?? { file: "_profile.json", field: "nodePalette" };
|
|
2507
|
+
lines.push(` - In ${literal(target.file)}, edit ${literal(target.field)} for node kinds: ${list(d.kinds)}`);
|
|
2508
|
+
break;
|
|
2509
|
+
}
|
|
2510
|
+
case "coverage":
|
|
2511
|
+
lines.push(" - Leaf node IDs: " + list(d.leaves));
|
|
2512
|
+
break;
|
|
2513
|
+
case "vocabulary":
|
|
2514
|
+
lines.push(` - ${d.space === "node" ? "Node" : "Edge"} kinds (${d.kinds.length}): ${list(d.kinds)}`);
|
|
2515
|
+
break;
|
|
2516
|
+
default:
|
|
2517
|
+
lines.push(" - " + (place ? (f.scope === "version" ? "Version " : "File ") + place + ": " : "") + plain(f.message));
|
|
2518
|
+
if (f.fix) {
|
|
2519
|
+
const [action, ...example] = f.fix.split("\n");
|
|
2520
|
+
lines.push(" - " + plain(action));
|
|
2521
|
+
if (example.length) lines.push(...codeBlock(example.join("\n"), " "));
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
if (uneven.length) {
|
|
2526
|
+
const example = uneven[0];
|
|
2527
|
+
lines.push(" - Parent node IDs: " + list(uneven.map((f) => f.node)));
|
|
2528
|
+
if (example.facts?.type === "uneven" && example.facts.closest) {
|
|
2529
|
+
const { a: a2, b } = example.facts.closest;
|
|
2530
|
+
lines.push(` - Example: Under the node with ID ${literal(example.node)}, child nodes ${literal(a2)} and ${literal(b)} have the most similar subtrees.`);
|
|
2531
|
+
}
|
|
2532
|
+
const commands = [
|
|
2533
|
+
"glassbrain tree " + shellArg(input) + " --path " + shellArg(example.node),
|
|
2534
|
+
"glassbrain restructure " + shellArg(input) + " --dry-run"
|
|
2535
|
+
].join("\n");
|
|
2536
|
+
lines.push(
|
|
2537
|
+
" - If two siblings share a subject, move both under a new group node. Inspect and preview:",
|
|
2538
|
+
...codeBlock(commands, " ", "sh")
|
|
2539
|
+
);
|
|
2540
|
+
const pairs = uneven.slice(1).flatMap((f) => f.facts?.type === "uneven" && f.facts.closest ? [`${literal(f.node)} \u2192 ${list([f.facts.closest.a, f.facts.closest.b])}`] : []);
|
|
2541
|
+
if (pairs.length) {
|
|
2542
|
+
lines.push(" - Other pairs to inspect (parent node ID \u2192 two child node IDs):");
|
|
2543
|
+
for (const pair of pairs) lines.push(" - " + pair);
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
if (rows.some((f) => f.facts?.type === "wide" && !f.facts.layers)) lines.push(" - Group children by subject where useful. Keep a natural collection together.");
|
|
2547
|
+
if (duplicates.length) {
|
|
2548
|
+
lines.push(" - Each source below lists edge kind \u2192 target node IDs.");
|
|
2549
|
+
for (const line of duplicateLines(duplicates)) lines.push(line);
|
|
2550
|
+
}
|
|
2551
|
+
return lines;
|
|
2552
|
+
}
|
|
2553
|
+
var severities = [["error", "Errors"], ["warn", "Warnings"], ["info", "Info"]];
|
|
2554
|
+
function reportText(found, input, versions = 1) {
|
|
2555
|
+
const lines = ["# Graph lint"];
|
|
2556
|
+
if (versions > 1) lines.push(`${versions} versions checked. Quality warnings apply to the latest version.`);
|
|
2557
|
+
for (const [level, heading] of severities) {
|
|
2558
|
+
const findings = found.filter((f) => f.level === level);
|
|
2559
|
+
const policy = { error: "Blocks export.", warn: "Fails lint with `--strict`.", info: "Advisory. Info alone passes lint, including with `--strict`." };
|
|
2560
|
+
lines.push(`## ${heading} (${findings.length})${findings.length ? " \u2014 " + policy[level] : ""}`);
|
|
2561
|
+
const groups = groupBy(findings, (f) => f.code);
|
|
2562
|
+
const codes = [...groups.keys()].sort((a2, b) => Number(a2 === "duplicate-edge") - Number(b === "duplicate-edge"));
|
|
2563
|
+
for (const code of codes) {
|
|
2564
|
+
const rows = groups.get(code);
|
|
2565
|
+
lines.push(`- **${plain(code)}** (${rows.length})${Object.hasOwn(headings, code) ? " \u2014 " + headings[code] : ""}`);
|
|
2566
|
+
for (const line of ruleLines(rows, input)) lines.push(line);
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
return lines.join("\n") + "\n";
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2384
2572
|
// cli/tree.ts
|
|
2385
2573
|
var one = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
2386
2574
|
var MAX_SIBLINGS = 12;
|
|
@@ -2388,7 +2576,6 @@ var MAX_LINES = 200;
|
|
|
2388
2576
|
var MAX_EDGES = 6;
|
|
2389
2577
|
function tree(model, opts) {
|
|
2390
2578
|
const derived = deriveLayers(model);
|
|
2391
|
-
const evidence = new Map(analyzeShape(model.nodes).branches.map((b) => [b.parent, b]));
|
|
2392
2579
|
const layerOf = (n) => derived.layerOf.get(n.id);
|
|
2393
2580
|
const labelOf = (id) => model.index.get(id)?.label ?? id;
|
|
2394
2581
|
const depth = Math.max(1, opts.depth ?? 3);
|
|
@@ -2423,8 +2610,8 @@ function tree(model, opts) {
|
|
|
2423
2610
|
for (const n of model.nodes) measure(n.id);
|
|
2424
2611
|
const bySize = (a2, b) => (size.get(b.id) ?? 1) - (size.get(a2.id) ?? 1) || labelOf(a2.id).localeCompare(labelOf(b.id));
|
|
2425
2612
|
const sorted = /* @__PURE__ */ new Map();
|
|
2426
|
-
for (const [id,
|
|
2427
|
-
sorted.set(id, orderedChildren(kids, id, model.index.get(id)?.data?.unordered === true) ?? [...
|
|
2613
|
+
for (const [id, list2] of kids) {
|
|
2614
|
+
sorted.set(id, orderedChildren(kids, id, model.index.get(id)?.data?.unordered === true) ?? [...list2].sort(bySize));
|
|
2428
2615
|
}
|
|
2429
2616
|
const outgoing = /* @__PURE__ */ new Map();
|
|
2430
2617
|
for (const e of model.edges) {
|
|
@@ -2434,22 +2621,19 @@ function tree(model, opts) {
|
|
|
2434
2621
|
if (arr) arr.push(item);
|
|
2435
2622
|
else outgoing.set(e.source, [item]);
|
|
2436
2623
|
}
|
|
2437
|
-
for (const
|
|
2624
|
+
for (const list2 of outgoing.values()) list2.sort();
|
|
2438
2625
|
const entries = [];
|
|
2439
2626
|
const edgeText = (all) => `(${all.slice(0, MAX_EDGES).join(", ")}${all.length > MAX_EDGES ? ` \u2026 ${all.length - MAX_EDGES} more` : ""})`;
|
|
2440
2627
|
const broken = (n) => n.parent && !model.index.has(n.parent);
|
|
2441
2628
|
const visit = (node, level, prefix, connector) => {
|
|
2442
|
-
const
|
|
2443
|
-
const canExpand = level + 1 < depth &&
|
|
2444
|
-
const collapsed =
|
|
2629
|
+
const list2 = sorted.get(node.id) ?? [];
|
|
2630
|
+
const canExpand = level + 1 < depth && list2.length > 0;
|
|
2631
|
+
const collapsed = list2.length > 0 && !canExpand;
|
|
2445
2632
|
const inside = (size.get(node.id) ?? 1) - 1;
|
|
2446
2633
|
const tall = height.get(node.id) ?? 0;
|
|
2447
2634
|
const edges = collapsed ? [] : outgoing.get(node.id) ?? [];
|
|
2448
2635
|
const bits = [];
|
|
2449
|
-
|
|
2450
|
-
if (assessment) bits.push(`${list.length} ${list.length === 1 ? "child" : "children"} \xB7 ${assessment.assessed ? `pressure ${assessment.score.toFixed(3)}` : "unassessed"}`);
|
|
2451
|
-
if (inside > 0) bits.push(one(inside, "node"));
|
|
2452
|
-
if (collapsed && tall > 0) bits.push(`${tall} deep`);
|
|
2636
|
+
if (collapsed) bits.push(`${one(inside, "node")} below \xB7 ${tall} deep`);
|
|
2453
2637
|
if (edges.length) bits.push(edgeText(edges));
|
|
2454
2638
|
const label = `${labelOf(node.id)}${broken(node) ? " (parent missing)" : ""}`;
|
|
2455
2639
|
const order = node.parent && model.index.get(node.parent)?.data?.unordered === true ? void 0 : node.order;
|
|
@@ -2467,31 +2651,28 @@ function tree(model, opts) {
|
|
|
2467
2651
|
text: `${prefix}${connector}${orderTag}${label}${labelOf(node.id) !== node.id ? ` [${node.id}]` : ""}${bits.length ? ` ${bits.join(" \xB7 ")}` : ""}`
|
|
2468
2652
|
});
|
|
2469
2653
|
if (!canExpand) return;
|
|
2470
|
-
const shown =
|
|
2654
|
+
const shown = list2.slice(0, perNode);
|
|
2471
2655
|
const childPrefix = prefix + (level === 0 ? "" : connector.includes("\u2514") ? " " : "\u2502 ");
|
|
2472
2656
|
shown.forEach((child, i) => {
|
|
2473
|
-
const last = i === shown.length - 1 &&
|
|
2657
|
+
const last = i === shown.length - 1 && list2.length <= perNode;
|
|
2474
2658
|
visit(child, level + 1, childPrefix, last ? "\u2514\u2500 " : "\u251C\u2500 ");
|
|
2475
2659
|
});
|
|
2476
|
-
if (
|
|
2660
|
+
if (list2.length > perNode) {
|
|
2477
2661
|
entries.push({
|
|
2478
2662
|
id: null,
|
|
2479
|
-
label: `\u2026 ${
|
|
2663
|
+
label: `\u2026 ${list2.length - perNode} more`,
|
|
2480
2664
|
depth: level + 1,
|
|
2481
2665
|
nodes: 0,
|
|
2482
2666
|
deep: 0,
|
|
2483
2667
|
collapsed: false,
|
|
2484
2668
|
edges: [],
|
|
2485
|
-
text: `${childPrefix}\u2514\u2500 \u2026 ${
|
|
2669
|
+
text: `${childPrefix}\u2514\u2500 \u2026 ${list2.length - perNode} more`
|
|
2486
2670
|
});
|
|
2487
2671
|
}
|
|
2488
2672
|
};
|
|
2489
2673
|
const structure = structureOf(model);
|
|
2490
|
-
let header = `${opts.title} ${one(structure.nodes, "node")} \xB7 ${one(structure.edges, "edge")} \xB7 ${one(structure.roots, "root")} \xB7 depth ${structure.depth}${structure.layers ? ` \xB7 ${one(structure.layers, "layer")}` : ""}
|
|
2674
|
+
let header = `${opts.title} ${one(structure.nodes, "node")} \xB7 ${one(structure.edges, "edge")} \xB7 ${one(structure.roots, "root")} \xB7 depth ${structure.depth}${structure.layers ? ` \xB7 ${one(structure.layers, "layer")}` : ""}`;
|
|
2491
2675
|
const kinds = { nodes: kindDistribution(model.nodes), edges: kindDistribution(model.edges) };
|
|
2492
|
-
header += `
|
|
2493
|
-
${kindSummary("node", kinds.nodes, opts.kindLabels)}
|
|
2494
|
-
${kindSummary("edge", kinds.edges, opts.kindLabels)}`;
|
|
2495
2676
|
const roots = model.nodes.filter((n) => !n.parent || broken(n));
|
|
2496
2677
|
if (opts.path) {
|
|
2497
2678
|
const start = model.index.get(opts.path);
|
|
@@ -2538,7 +2719,9 @@ path: ${[...chain, labelOf(start.id)].join(" \u203A ")}`;
|
|
|
2538
2719
|
}
|
|
2539
2720
|
function treeText(result) {
|
|
2540
2721
|
const lines = [result.header, ...result.entries.map((e) => e.text)];
|
|
2541
|
-
if (result.more) lines.push(`\u2026 ${result.more} more lines
|
|
2722
|
+
if (result.more) lines.push(`\u2026 ${result.more} more lines`);
|
|
2723
|
+
if (result.more || result.entries.some((e) => e.collapsed || e.id === null))
|
|
2724
|
+
lines.push("Explore a branch with --path <id> and --depth <levels>.");
|
|
2542
2725
|
return lines.join("\n");
|
|
2543
2726
|
}
|
|
2544
2727
|
|
|
@@ -2705,14 +2888,34 @@ function restructure(input, edges, opts = {}) {
|
|
|
2705
2888
|
note: "Structure pressure is not correctness. No original node or relation was removed. Group suggestions require --group; lower pressure after grouping is not proof of improvement."
|
|
2706
2889
|
};
|
|
2707
2890
|
}
|
|
2891
|
+
function repairLines(r) {
|
|
2892
|
+
const lines = [];
|
|
2893
|
+
const root = "top level";
|
|
2894
|
+
for (const n of r.created)
|
|
2895
|
+
lines.push(`Create ${n.label} [${n.id}] under ${n.parent ?? root}`);
|
|
2896
|
+
const moves = /* @__PURE__ */ new Map();
|
|
2897
|
+
for (const m of r.moves) {
|
|
2898
|
+
const key = JSON.stringify([m.from, m.to, m.why]);
|
|
2899
|
+
const group = moves.get(key) ?? { from: m.from ?? root, to: m.to ?? root, why: m.why, ids: [] };
|
|
2900
|
+
group.ids.push(m.id);
|
|
2901
|
+
moves.set(key, group);
|
|
2902
|
+
}
|
|
2903
|
+
for (const m of moves.values())
|
|
2904
|
+
lines.push(`${m.from} \u2192 ${m.to} \xB7 ${m.ids.join(", ")} (${m.why})`);
|
|
2905
|
+
for (const o of r.orders)
|
|
2906
|
+
lines.push(`Reorder ${o.id}: ${o.from ?? "unset"} \u2192 ${o.to}`);
|
|
2907
|
+
return lines;
|
|
2908
|
+
}
|
|
2708
2909
|
function planText(r, title, inputPath = "<path>") {
|
|
2709
|
-
const
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
for (const s of r.suggestions)
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2910
|
+
const repairs = repairLines(r);
|
|
2911
|
+
const lines = [`${title} \u2014 ${repairs.length ? "Planned changes" : "No automatic changes needed."}`, ...repairs.map((line) => " " + line)];
|
|
2912
|
+
if (r.suggestions.length) lines.push("Optional groups \xB7 These siblings have similar branches. Group them if they share a subject.");
|
|
2913
|
+
for (const s of r.suggestions) {
|
|
2914
|
+
const members = s.members.join(",");
|
|
2915
|
+
lines.push(` ${s.parent} \u2192 one new group for ${s.members.join(", ")}`);
|
|
2916
|
+
if (s.members.every((id) => !id.includes(","))) lines.push(` glassbrain restructure ${inputPath} --group ${shellArg(members)}`);
|
|
2917
|
+
}
|
|
2918
|
+
return lines.join("\n");
|
|
2716
2919
|
}
|
|
2717
2920
|
function planNodesOf(model) {
|
|
2718
2921
|
return model.nodes.map((n, seq) => ({
|
|
@@ -2811,7 +3014,7 @@ var HELP = (schema) => `glassbrain: open, diff and lint a knowledge graph, writt
|
|
|
2811
3014
|
--out <file.html> write the page here instead of a temp file (view, diff)
|
|
2812
3015
|
--open / --no-open open the browser or not (diff opens only in a terminal)
|
|
2813
3016
|
--exit-code exit 1 when anything changed (diff)
|
|
2814
|
-
--all list every change
|
|
3017
|
+
--all list every change (diff); lint always lists all findings
|
|
2815
3018
|
--ignore <a,b> fields that do not count as a change (diff)
|
|
2816
3019
|
--format <id> force a format: native, graphify, understand-anything, memory, obsidian
|
|
2817
3020
|
-h, --help / -v, --version this help / the version
|
|
@@ -2867,18 +3070,13 @@ function writePage(payload, out) {
|
|
|
2867
3070
|
return file;
|
|
2868
3071
|
}
|
|
2869
3072
|
var count = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
2870
|
-
var TAG = { error: "error", warn: "warn ", info: "info " };
|
|
2871
3073
|
var tally = (found) => ({
|
|
2872
3074
|
error: found.filter((f) => f.level === "error").length,
|
|
2873
3075
|
warn: found.filter((f) => f.level === "warn").length,
|
|
2874
3076
|
info: found.filter((f) => f.level === "info").length
|
|
2875
3077
|
});
|
|
2876
|
-
function printFindings(found,
|
|
2877
|
-
|
|
2878
|
-
console.log(`${TAG[f.level]} ${f.code} ${f.where} ${f.message}`);
|
|
2879
|
-
if (f.fix) console.log(f.fix.split("\n").map((l) => ` ${l}`).join("\n"));
|
|
2880
|
-
}
|
|
2881
|
-
if (found.length > limit) console.log(`\u2026 ${found.length - limit} more (--json)`);
|
|
3078
|
+
function printFindings(found, input, versions = 1) {
|
|
3079
|
+
process.stdout.write(reportText(found, input, versions));
|
|
2882
3080
|
}
|
|
2883
3081
|
var slug = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 40) || "graph";
|
|
2884
3082
|
function openFile(file) {
|
|
@@ -2904,28 +3102,27 @@ function notes(l, models) {
|
|
|
2904
3102
|
});
|
|
2905
3103
|
}
|
|
2906
3104
|
var summaryOf = (l, m) => `${l.title}: ${l.versions.length > 1 ? `${l.versions.length} versions, latest ` : ""}${count(m.nodes.length, "node")}, ${count(m.edges.length, "edge")}${l.format !== "native" ? ` (read as ${l.format})` : ""}`;
|
|
2907
|
-
var
|
|
2908
|
-
var findingsAt = (l, input) => lint(l).map((f) => ({ ...f, ...f.fix ? { fix: f.fix.replaceAll("<path>", shellArg(input)) } : {} }));
|
|
3105
|
+
var findingsAt = (l, input) => lint(l).map((f) => ({ ...f, ...f.fix ? { fix: f.fix.replaceAll("<path>", () => shellArg(input)) } : {} }));
|
|
2909
3106
|
function view(a2) {
|
|
2910
3107
|
if (a2.pos.length !== 1) fail("view needs exactly one <path>");
|
|
2911
3108
|
const l = load(a2.pos[0], { format: a2.flags.format });
|
|
2912
3109
|
const models = nodesOf(l);
|
|
2913
|
-
notes(l,
|
|
3110
|
+
notes(l, []);
|
|
2914
3111
|
const found = findingsAt(l, a2.pos[0]);
|
|
2915
3112
|
const n = tally(found);
|
|
2916
3113
|
if (n.error || a2.flags.strict && n.warn) {
|
|
2917
3114
|
console.log(summaryOf(l, models[models.length - 1]));
|
|
2918
|
-
printFindings(found);
|
|
3115
|
+
printFindings(found, a2.pos[0], l.versions.length);
|
|
2919
3116
|
console.log(`
|
|
2920
3117
|
${count(n.error, "error")} \xB7 ${count(n.warn, "warning")} \u2014 page not written. Fix them, then lint again.`);
|
|
2921
3118
|
process.exitCode = 1;
|
|
2922
3119
|
return;
|
|
2923
3120
|
}
|
|
2924
|
-
const file = writePage({ title: l.title, profile: l.profile, versions: l.versions, layouts: l.layouts, start: { at: l.versions.length - 1, compare: false } }, a2.flags.out);
|
|
3121
|
+
const file = writePage({ lint: { input: a2.pos[0], findings: found, paletteTarget: l.paletteTarget }, title: l.title, profile: l.profile, versions: l.versions, layouts: l.layouts, start: { at: l.versions.length - 1, compare: false } }, a2.flags.out);
|
|
2925
3122
|
console.log(summaryOf(l, models[models.length - 1]));
|
|
2926
3123
|
console.log(file);
|
|
2927
3124
|
if (n.warn) {
|
|
2928
|
-
printFindings(found.
|
|
3125
|
+
printFindings(found, a2.pos[0], l.versions.length);
|
|
2929
3126
|
console.log(`${count(n.warn, "warning")} (not blocking; \`glassbrain lint\` has the details)`);
|
|
2930
3127
|
}
|
|
2931
3128
|
if (a2.flags.open !== false) openFile(file);
|
|
@@ -2964,10 +3161,14 @@ function runDiff(a2) {
|
|
|
2964
3161
|
const wantOpen = a2.flags.open === true || a2.flags.open === void 0 && !a2.flags.json && process.stdout.isTTY;
|
|
2965
3162
|
let file = null;
|
|
2966
3163
|
if (wantOpen || a2.flags.out) {
|
|
3164
|
+
const versionInput = (spec, loaded) => loaded.versions.length > 1 ? spec.replace(/\/$/, "") + "/" + pick(loaded).name : spec;
|
|
2967
3165
|
file = writePage({
|
|
2968
3166
|
title: `${labels.before} \u2192 ${labels.after}`,
|
|
2969
3167
|
profile: profileJson || ignore.length ? { ...profileJson, diffIgnore: ignore } : null,
|
|
2970
|
-
versions: [
|
|
3168
|
+
versions: [
|
|
3169
|
+
{ name: labels.before, input: versionInput(oldSpec, before), sources: vb.sources },
|
|
3170
|
+
{ name: labels.after, input: versionInput(newSpec, after), sources: va.sources }
|
|
3171
|
+
],
|
|
2971
3172
|
layouts: null,
|
|
2972
3173
|
start: { at: 1, compare: true }
|
|
2973
3174
|
}, a2.flags.out);
|
|
@@ -2988,7 +3189,6 @@ function runLint(a2) {
|
|
|
2988
3189
|
const models = nodesOf(l);
|
|
2989
3190
|
const latest = models[models.length - 1];
|
|
2990
3191
|
const n = tally(found);
|
|
2991
|
-
const structure = structureOf(latest);
|
|
2992
3192
|
const failing = n.error > 0 || a2.flags.strict && n.warn > 0;
|
|
2993
3193
|
if (a2.flags.json) {
|
|
2994
3194
|
console.log(JSON.stringify({
|
|
@@ -2998,16 +3198,11 @@ function runLint(a2) {
|
|
|
2998
3198
|
errors: n.error,
|
|
2999
3199
|
warnings: n.warn,
|
|
3000
3200
|
infos: n.info,
|
|
3001
|
-
structure,
|
|
3201
|
+
structure: structureOf(latest),
|
|
3002
3202
|
issues: found
|
|
3003
3203
|
}, null, 1));
|
|
3004
3204
|
} else {
|
|
3005
|
-
|
|
3006
|
-
if (found.length) console.log("");
|
|
3007
|
-
printFindings(found);
|
|
3008
|
-
console.log(`${count(n.error, "error")} \xB7 ${count(n.warn, "warning")} \xB7 ${count(n.info, "info")}`);
|
|
3009
|
-
console.log(`structure: ${count(structure.roots, "root")} \xB7 depth ${structure.depth}${structure.layers ? ` \xB7 ${count(structure.layers, "layer")}` : ""} \xB7 ${structure.parented}/${structure.nodes} parented \xB7 structure pressure ${structure.shape.peak.toFixed(3)} \xB7 ${structure.shape.assessed} assessed / ${structure.shape.unassessed} unassessed`);
|
|
3010
|
-
console.log(`tree: glassbrain tree ${shellArg(a2.pos[0])} --depth=3`);
|
|
3205
|
+
printFindings(found, a2.pos[0], l.versions.length);
|
|
3011
3206
|
if (l.ignored.length) process.stderr.write(`note: not read: ${l.ignored.slice(0, 5).join(", ")}${l.ignored.length > 5 ? ` and ${l.ignored.length - 5} more` : ""}
|
|
3012
3207
|
`);
|
|
3013
3208
|
}
|
|
@@ -3019,8 +3214,8 @@ function runTree(a2) {
|
|
|
3019
3214
|
if (!Number.isInteger(depth) || depth < 1) fail("--depth needs a positive integer");
|
|
3020
3215
|
const l = load(a2.pos[0], { format: a2.flags.format });
|
|
3021
3216
|
const models = nodesOf(l);
|
|
3022
|
-
notes(l, models);
|
|
3023
3217
|
const latest = models[models.length - 1];
|
|
3218
|
+
notes(l, models.slice(0, -1));
|
|
3024
3219
|
if (latest.errors.length) fail(latest.errors.map((e) => `${e.origin}: ${e.message}`).join("\n"), 1);
|
|
3025
3220
|
const version = l.versions[l.versions.length - 1].name;
|
|
3026
3221
|
const result = tree(latest, {
|
|
@@ -3040,11 +3235,12 @@ function runRestructure(a2) {
|
|
|
3040
3235
|
if (a2.pos.length !== 1) fail("restructure needs exactly one <path>");
|
|
3041
3236
|
const l = load(a2.pos[0], { format: a2.flags.format });
|
|
3042
3237
|
const blockers = lint(l).filter((f) => ["duplicate-id", "dangling-edge", "field-conflict", "children-conflict"].includes(f.code));
|
|
3043
|
-
if (blockers.length) fail(`
|
|
3238
|
+
if (blockers.length) fail(`Repair these before restructuring:
|
|
3239
|
+
${reportText(blockers, a2.pos[0]).trimEnd()}`, 1);
|
|
3044
3240
|
const models = nodesOf(l);
|
|
3045
|
-
notes(l, models);
|
|
3046
3241
|
const latest = models[models.length - 1];
|
|
3047
|
-
|
|
3242
|
+
notes(l, models.slice(0, -1));
|
|
3243
|
+
if (latest.errors.length) fail(latest.errors.map((e) => `${e.origin}: ${e.message}`).join("\n"), 1);
|
|
3048
3244
|
const phys = toPhysical(latest);
|
|
3049
3245
|
const nodes = planNodesOf(latest);
|
|
3050
3246
|
const edges = phys.edges.map((e) => ({
|