@blackbelt-technology/pi-dashboard-kb 0.7.0 → 0.9.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/README.md +69 -0
- package/bin/kb.mjs +84 -0
- package/bin/lib/engine-fingerprint.mjs +183 -0
- package/dist/adoc-chunker.d.ts +29 -0
- package/dist/adoc-chunker.js +274 -0
- package/dist/archive-guard.d.ts +12 -0
- package/dist/archive-guard.js +154 -0
- package/dist/chunker.d.ts +3 -0
- package/dist/chunker.js +5 -3
- package/dist/cli.js +137 -19
- package/dist/config.d.ts +49 -0
- package/dist/config.js +43 -6
- package/dist/dox-triage.d.ts +67 -0
- package/dist/dox-triage.js +183 -0
- package/dist/dox.d.ts +68 -7
- package/dist/dox.js +370 -94
- package/dist/eval.d.ts +30 -0
- package/dist/eval.js +120 -13
- package/dist/gitignore.d.ts +24 -0
- package/dist/gitignore.js +205 -0
- package/dist/index.d.ts +25 -17
- package/dist/index.js +17 -8
- package/dist/indexer.d.ts +9 -1
- package/dist/indexer.js +40 -11
- package/dist/net-guard.d.ts +35 -0
- package/dist/net-guard.js +285 -0
- package/dist/render.js +37 -2
- package/dist/search-opts.d.ts +22 -0
- package/dist/search-opts.js +21 -0
- package/dist/sources.d.ts +29 -0
- package/dist/sources.js +230 -32
- package/dist/sqlite-store.d.ts +72 -2
- package/dist/sqlite-store.js +482 -78
- package/dist/staleness.d.ts +25 -0
- package/dist/staleness.js +69 -0
- package/dist/trust.d.ts +33 -1
- package/dist/trust.js +72 -5
- package/dist/types.d.ts +59 -0
- package/dist/verdict.d.ts +45 -0
- package/dist/verdict.js +368 -0
- package/engine-fingerprint.json +5 -0
- package/package.json +8 -4
- package/src/adoc-chunker.ts +308 -0
- package/src/archive-guard.ts +144 -0
- package/src/chunker.ts +5 -3
- package/src/cli.ts +130 -20
- package/src/config.ts +81 -6
- package/src/dox-triage.ts +231 -0
- package/src/dox.ts +390 -70
- package/src/eval.ts +136 -14
- package/src/gitignore.ts +215 -0
- package/src/index.ts +45 -20
- package/src/indexer.ts +46 -10
- package/src/net-guard.ts +288 -0
- package/src/render.ts +35 -3
- package/src/search-opts.ts +52 -0
- package/src/sources.ts +232 -28
- package/src/sqlite-store.ts +448 -67
- package/src/staleness.ts +76 -0
- package/src/trust.ts +81 -8
- package/src/types.ts +58 -0
- package/src/verdict.ts +412 -0
- package/src/AGENTS.md +0 -23
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// Stale-row triage: judge a DOX row against the REAL git diff since it was
|
|
2
|
+
// last acknowledged, instead of guessing.
|
|
3
|
+
//
|
|
4
|
+
// `kb dox lint` marks a row "stale" when the sha256 of its file differs from the
|
|
5
|
+
// acknowledged hash. That only means BYTES CHANGED — it is not evidence the row
|
|
6
|
+
// text is wrong. The two obvious responses are both bad: bulk re-acking launders
|
|
7
|
+
// genuine drift, and a symbol-presence heuristic produces false positives (it
|
|
8
|
+
// will flag a row that correctly documents a symbol as *removed*).
|
|
9
|
+
//
|
|
10
|
+
// This module recovers real evidence. The acknowledged hash identifies a past
|
|
11
|
+
// state of the file, so walking the file's history for the commit whose blob
|
|
12
|
+
// hashes to that value pinpoints what a human last signed off on. `git diff`
|
|
13
|
+
// from there is exactly what the row has never been reconciled against — small
|
|
14
|
+
// enough to hand to a cheap model, specific enough to judge.
|
|
15
|
+
import { execFileSync } from "node:child_process"; // ban:child_process-ok (kb package is self-contained; owns the git history walk, no pi-dashboard-shared dep)
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { dirname, join, relative } from "node:path";
|
|
19
|
+
import { STALENESS_VERSION, readStaleness, stalenessVersionOnDisk } from "./staleness.js";
|
|
20
|
+
import { resolveRowPath } from "./dox.js";
|
|
21
|
+
export const ROW_RE = /^\|\s*`([^`]+)`\s*\|\s*(.*?)\s*\|\s*$/;
|
|
22
|
+
/** History window scanned for a blob matching the acked hash. */
|
|
23
|
+
export const DEFAULT_DEPTH = 60;
|
|
24
|
+
/** Per-item diff budget. Keeps a @fast subagent's context small and cheap. */
|
|
25
|
+
export const DIFF_CAP = 6000;
|
|
26
|
+
export function parseRows(md) {
|
|
27
|
+
const rows = [];
|
|
28
|
+
md.split("\n").forEach((line, i) => {
|
|
29
|
+
const m = line.match(ROW_RE);
|
|
30
|
+
if (!m)
|
|
31
|
+
return;
|
|
32
|
+
if (/^-+$/.test(m[1]))
|
|
33
|
+
return;
|
|
34
|
+
rows.push({ line: i, path: m[1], purpose: m[2] });
|
|
35
|
+
});
|
|
36
|
+
return rows;
|
|
37
|
+
}
|
|
38
|
+
/** Replace one row's purpose cell. Byte-identical elsewhere; no-op if absent. */
|
|
39
|
+
export function replaceRowPurpose(md, rowPath, newPurpose) {
|
|
40
|
+
let hit = false;
|
|
41
|
+
const out = md.split("\n").map((line) => {
|
|
42
|
+
if (hit)
|
|
43
|
+
return line;
|
|
44
|
+
const m = line.match(ROW_RE);
|
|
45
|
+
if (!m || m[1] !== rowPath)
|
|
46
|
+
return line;
|
|
47
|
+
hit = true;
|
|
48
|
+
return `| \`${m[1]}\` | ${newPurpose} |`;
|
|
49
|
+
});
|
|
50
|
+
return hit ? out.join("\n") : md;
|
|
51
|
+
}
|
|
52
|
+
const sha256 = (buf) => createHash("sha256").update(buf).digest("hex");
|
|
53
|
+
function git(cwd, args) {
|
|
54
|
+
return execFileSync("git", args, { cwd, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
55
|
+
}
|
|
56
|
+
export function resolveBaseline(opts) {
|
|
57
|
+
const { cwd, file, ackedSha, depth = DEFAULT_DEPTH } = opts;
|
|
58
|
+
let commits = [];
|
|
59
|
+
try {
|
|
60
|
+
commits = git(cwd, ["rev-list", `-n${depth}`, "HEAD", "--", file]).trim().split("\n").filter(Boolean);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return { found: false, commit: null, diff: "", reason: "no-history" };
|
|
64
|
+
}
|
|
65
|
+
if (!commits.length)
|
|
66
|
+
return { found: false, commit: null, diff: "", reason: "untracked" };
|
|
67
|
+
for (const c of commits) {
|
|
68
|
+
let blob;
|
|
69
|
+
try {
|
|
70
|
+
blob = execFileSync("git", ["show", `${c}:${file}`], { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (sha256(blob) !== ackedSha)
|
|
76
|
+
continue;
|
|
77
|
+
let diff = "";
|
|
78
|
+
try {
|
|
79
|
+
// `<commit> --` (NOT `<commit>..HEAD`): staleness is hashed from the WORKING
|
|
80
|
+
// TREE, so the diff must span uncommitted edits too. Using ..HEAD reports an
|
|
81
|
+
// empty diff for a row made stale purely by unstaged work, which would then
|
|
82
|
+
// be judged "unchanged" on no evidence at all.
|
|
83
|
+
diff = git(cwd, ["diff", c, "--", file]);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
diff = "";
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
found: true,
|
|
90
|
+
commit: c,
|
|
91
|
+
diff: diff.length > DIFF_CAP ? `${diff.slice(0, DIFF_CAP)}\n…[diff truncated]` : diff,
|
|
92
|
+
reason: null,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
// Acked state predates the window, or was acked from an uncommitted tree.
|
|
96
|
+
// Say so rather than diffing against an arbitrary commit.
|
|
97
|
+
return { found: false, commit: null, diff: "", reason: `no-match-in-last-${depth}` };
|
|
98
|
+
}
|
|
99
|
+
export function buildWorkItems(opts) {
|
|
100
|
+
const { cwd, issues, staleness, depth = DEFAULT_DEPTH, limit = Number.POSITIVE_INFINITY } = opts;
|
|
101
|
+
const items = [];
|
|
102
|
+
for (const iss of issues) {
|
|
103
|
+
if (iss.kind !== "stale" || !iss.path)
|
|
104
|
+
continue;
|
|
105
|
+
if (items.length >= limit)
|
|
106
|
+
break;
|
|
107
|
+
const agentsAbs = join(cwd, iss.agentsFile);
|
|
108
|
+
if (!existsSync(agentsAbs))
|
|
109
|
+
continue;
|
|
110
|
+
// Reuse the lint's own resolver so triage and lint never disagree about
|
|
111
|
+
// which file a row points at (incl. the repo-root fallback for config rows).
|
|
112
|
+
const abs = resolveRowPath(dirname(agentsAbs), cwd, iss.path);
|
|
113
|
+
if (!existsSync(abs) || !statSync(abs).isFile())
|
|
114
|
+
continue;
|
|
115
|
+
const target = relative(cwd, abs);
|
|
116
|
+
const row = parseRows(readFileSync(agentsAbs, "utf8")).find((r) => r.path === iss.path);
|
|
117
|
+
if (!row)
|
|
118
|
+
continue;
|
|
119
|
+
const acked = staleness[target] ?? staleness[iss.path];
|
|
120
|
+
const base = acked?.sha256
|
|
121
|
+
? resolveBaseline({ cwd, file: target, ackedSha: acked.sha256, depth })
|
|
122
|
+
: { found: false, commit: null, diff: "", reason: "not-acked" };
|
|
123
|
+
items.push({
|
|
124
|
+
agentsFile: iss.agentsFile,
|
|
125
|
+
row: iss.path,
|
|
126
|
+
target,
|
|
127
|
+
purpose: row.purpose,
|
|
128
|
+
baseline: base.commit,
|
|
129
|
+
baselineFound: base.found,
|
|
130
|
+
reason: base.reason,
|
|
131
|
+
diff: base.diff,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return items;
|
|
135
|
+
}
|
|
136
|
+
export function applyDecisions(opts) {
|
|
137
|
+
const { cwd, decisions, write = false } = opts;
|
|
138
|
+
const res = { rewritten: 0, kept: 0, skipped: [] };
|
|
139
|
+
for (const d of decisions) {
|
|
140
|
+
if (d.verdict !== "REWRITE" || !d.purpose) {
|
|
141
|
+
res.kept++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const p = join(cwd, d.agentsFile);
|
|
145
|
+
if (!existsSync(p)) {
|
|
146
|
+
res.skipped.push(`${d.agentsFile} (missing)`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const before = readFileSync(p, "utf8");
|
|
150
|
+
const after = replaceRowPurpose(before, d.row, d.purpose);
|
|
151
|
+
if (after === before) {
|
|
152
|
+
res.skipped.push(`${d.agentsFile} :: ${d.row} (row not found)`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (write)
|
|
156
|
+
writeFileSync(p, after, "utf8");
|
|
157
|
+
res.rewritten++;
|
|
158
|
+
}
|
|
159
|
+
return res;
|
|
160
|
+
}
|
|
161
|
+
/** Re-acknowledge rows: record each target's sha256 + stat baseline (v2). */
|
|
162
|
+
export function ackTargets(opts) {
|
|
163
|
+
const { cwd, targets, stalenessFile } = opts;
|
|
164
|
+
// Fail closed: a sidecar from a NEWER version carries records this code
|
|
165
|
+
// cannot see — rewriting it as v2 would silently delete them.
|
|
166
|
+
const onDisk = stalenessVersionOnDisk(stalenessFile);
|
|
167
|
+
if (onDisk != null && onDisk > STALENESS_VERSION) {
|
|
168
|
+
throw new Error(`dox-staleness.json is version ${onDisk} (> supported ${STALENESS_VERSION}); refusing to overwrite — upgrade the kb package first`);
|
|
169
|
+
}
|
|
170
|
+
const map = readStaleness(stalenessFile);
|
|
171
|
+
let n = 0;
|
|
172
|
+
for (const t of targets) {
|
|
173
|
+
const p = join(cwd, t);
|
|
174
|
+
if (!existsSync(p) || !statSync(p).isFile())
|
|
175
|
+
continue;
|
|
176
|
+
const st = statSync(p);
|
|
177
|
+
map[t] = { sha256: sha256(readFileSync(p)), size: st.size, mtimeMs: st.mtimeMs };
|
|
178
|
+
n++;
|
|
179
|
+
}
|
|
180
|
+
const file = { version: STALENESS_VERSION, files: map };
|
|
181
|
+
writeFileSync(stalenessFile, `${JSON.stringify(file, null, 2)}\n`, "utf8");
|
|
182
|
+
return n;
|
|
183
|
+
}
|
package/dist/dox.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { type GitignoreMatcher } from "./gitignore.js";
|
|
1
2
|
import type { KbStore } from "./types.js";
|
|
2
|
-
export declare const AREA_FILE_THRESHOLD = 8;
|
|
3
|
-
export declare const ROW_CAP = 40;
|
|
4
3
|
export declare const AGENTS_BYTE_CAP = 30000;
|
|
5
4
|
export interface AgentsChainOpts {
|
|
6
5
|
claudeMd?: boolean;
|
|
@@ -20,6 +19,24 @@ export declare function agentsChain(cwd: string, targetPath: string, opts?: Agen
|
|
|
20
19
|
/** KB-generated routing manifest when no AGENTS.md exists on the path: lists
|
|
21
20
|
* markdown files under the target subtree as a path → heading map. */
|
|
22
21
|
export declare function fallbackManifest(cwd: string, targetPath: string, store?: KbStore): string;
|
|
22
|
+
export interface DoxRow {
|
|
23
|
+
path: string;
|
|
24
|
+
line: string;
|
|
25
|
+
lineIndex: number;
|
|
26
|
+
}
|
|
27
|
+
export interface DoxScan {
|
|
28
|
+
rows: DoxRow[];
|
|
29
|
+
/** File-row tables from which zero rows were recognized (header line
|
|
30
|
+
* indexes). A header nobody filled is a finding, not silence. */
|
|
31
|
+
emptyFileTables: {
|
|
32
|
+
line: number;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
35
|
+
/** Recognize DOX file rows by table header (design D1). One state machine,
|
|
36
|
+
* three consumers (parseRowPaths, countInlineRows, doxLint). Opens on the
|
|
37
|
+
* header line; stays open while lines start with `|` and are not a second
|
|
38
|
+
* delimiter; closes on the first non-`|` line or the second delimiter. */
|
|
39
|
+
export declare function scanDoxRows(text: string): DoxScan;
|
|
23
40
|
export interface DoxInitOptions {
|
|
24
41
|
dryRun?: boolean;
|
|
25
42
|
cwd: string;
|
|
@@ -43,29 +60,73 @@ export declare function resolveRowPath(agentsDir: string, cwd: string, rp: strin
|
|
|
43
60
|
* string[] (consumed cross-package by kb-extension acknowledgeRows/decideNudge
|
|
44
61
|
* + the missing/orphan/staleness checks); the exclusion is count-only. */
|
|
45
62
|
export declare function countInlineRows(agentsFile: string): number;
|
|
46
|
-
/** Parse existing row paths from an AGENTS.md file.
|
|
63
|
+
/** Parse existing row paths from an AGENTS.md file. Rows are recognized by
|
|
64
|
+
* their table's `| File | Purpose |` header (design D1) — independent of
|
|
65
|
+
* heading structure. */
|
|
47
66
|
export declare function parseRowPaths(agentsFile: string): string[];
|
|
67
|
+
export interface EmptyPurposeList {
|
|
68
|
+
groups: Array<{
|
|
69
|
+
agentsPath: string;
|
|
70
|
+
subjects: string[];
|
|
71
|
+
}>;
|
|
72
|
+
total: number;
|
|
73
|
+
}
|
|
74
|
+
/** Enumerate every `| File | Purpose |` row whose Purpose is empty or
|
|
75
|
+
* whitespace-only, grouped by owning `AGENTS.md`. `dir` restricts the walk to
|
|
76
|
+
* one subtree. Read-only: no model, no writes (design D8). Rows are recognized
|
|
77
|
+
* by the table header (`scanDoxRows`), so a prose-only file is skipped. */
|
|
78
|
+
export declare function listEmptyPurposeRows(opts: {
|
|
79
|
+
cwd: string;
|
|
80
|
+
dir?: string;
|
|
81
|
+
}): EmptyPurposeList;
|
|
48
82
|
/** Source-file walk (delta ①②), exported for the file-index migration. */
|
|
49
|
-
export declare function sourceFiles(cwd: string): string[];
|
|
50
|
-
export declare function areaFiles(cwd: string): Map<string, string[]>;
|
|
83
|
+
export declare function sourceFiles(cwd: string, ignore?: GitignoreMatcher): string[];
|
|
84
|
+
export declare function areaFiles(cwd: string, ignore?: GitignoreMatcher): Map<string, string[]>;
|
|
51
85
|
/** Plan (and optionally write) a DOX tree. Idempotent: never clobbers existing
|
|
52
86
|
* AGENTS.md; only adds missing files + missing path rows. */
|
|
53
87
|
export declare function doxInit(opts: DoxInitOptions): DoxInitPlan;
|
|
88
|
+
/**
|
|
89
|
+
* Pull repo-path REFERENCES out of a row's purpose cell.
|
|
90
|
+
*
|
|
91
|
+
* The lint hashes the file behind each row but never validates paths written
|
|
92
|
+
* inside the prose, so a directory move silently rots cross-references (this is
|
|
93
|
+
* how a routing rule kept pointing at two deleted dirs). The hard part is not
|
|
94
|
+
* finding candidates — it is rejecting the ~99% that are not repo paths at all:
|
|
95
|
+
* URL routes, MIME types, npm specifiers, `~`/absolute paths, model ids, code
|
|
96
|
+
* fragments like `get/list/remove`, and descriptions of OTHER projects' layouts.
|
|
97
|
+
*
|
|
98
|
+
* Discriminators, in order of how much noise each removes:
|
|
99
|
+
* 1. first segment must be a real top-level entry of THIS repo — kills
|
|
100
|
+
* `lib/validations.ts` (consumer-project prose) and `provider/model`
|
|
101
|
+
* 2. must carry a source-file extension or be a glob — kills bare route paths
|
|
102
|
+
* 3. structural rejects: leading `~` or `/`, `@` scopes, and any char that
|
|
103
|
+
* cannot appear in a path we would write (`:?="'()[]{}<>` , whitespace…)
|
|
104
|
+
*/
|
|
105
|
+
export declare function extractRefPaths(cell: string, topLevel: Set<string>): string[];
|
|
54
106
|
export interface DoxIssue {
|
|
55
|
-
kind: "stale" | "orphan" | "missing" | "missing-companion" | "broken-pointer" | "over-threshold";
|
|
107
|
+
kind: "stale" | "orphan" | "missing" | "missing-companion" | "broken-pointer" | "broken-ref" | "zero-row-table" | "over-threshold";
|
|
56
108
|
agentsFile: string;
|
|
57
109
|
path?: string;
|
|
58
110
|
detail: string;
|
|
59
|
-
arm?: "bytes" | "rows";
|
|
111
|
+
arm?: "bytes" | "rows" | "source";
|
|
60
112
|
}
|
|
61
113
|
export interface DoxLintOptions {
|
|
62
114
|
json?: boolean;
|
|
63
115
|
fix?: boolean;
|
|
64
116
|
cwd: string;
|
|
65
117
|
stalenessFile?: string;
|
|
118
|
+
/** Opt-in: report undocumented SOURCE files as `missing` (design D9). OFF by
|
|
119
|
+
* default so an existing tree can adopt the arm incrementally instead of
|
|
120
|
+
* red-walling CI with a large one-time finding count. */
|
|
121
|
+
sourceFileRows?: boolean;
|
|
66
122
|
}
|
|
67
123
|
export interface DoxLintResult {
|
|
68
124
|
issues: DoxIssue[];
|
|
69
125
|
fixed: number;
|
|
126
|
+
/** Coverage (design D4): AGENTS.md files row-parsed, so a clean verdict can
|
|
127
|
+
* be distinguished from an unread file. */
|
|
128
|
+
filesScanned: number;
|
|
129
|
+
/** Recognized rows across all scanned files. */
|
|
130
|
+
rowsScanned: number;
|
|
70
131
|
}
|
|
71
132
|
export declare function doxLint(opts: DoxLintOptions): DoxLintResult;
|