@interf/compiler 0.1.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/LICENSE +183 -0
- package/README.md +1008 -0
- package/TRADEMARKS.md +19 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +30 -0
- package/dist/bin.js.map +1 -0
- package/dist/commands/benchmark.d.ts +3 -0
- package/dist/commands/benchmark.d.ts.map +1 -0
- package/dist/commands/benchmark.js +400 -0
- package/dist/commands/benchmark.js.map +1 -0
- package/dist/commands/compile.d.ts +3 -0
- package/dist/commands/compile.d.ts.map +1 -0
- package/dist/commands/compile.js +139 -0
- package/dist/commands/compile.js.map +1 -0
- package/dist/commands/create.d.ts +49 -0
- package/dist/commands/create.d.ts.map +1 -0
- package/dist/commands/create.js +813 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/default.d.ts +3 -0
- package/dist/commands/default.d.ts.map +1 -0
- package/dist/commands/default.js +52 -0
- package/dist/commands/default.js.map +1 -0
- package/dist/commands/doctor.d.ts +3 -0
- package/dist/commands/doctor.d.ts.map +1 -0
- package/dist/commands/doctor.js +146 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +235 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/list.d.ts +3 -0
- package/dist/commands/list.d.ts.map +1 -0
- package/dist/commands/list.js +32 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/reset.d.ts +3 -0
- package/dist/commands/reset.d.ts.map +1 -0
- package/dist/commands/reset.js +136 -0
- package/dist/commands/reset.js.map +1 -0
- package/dist/commands/status.d.ts +3 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +67 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/verify.d.ts +3 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +112 -0
- package/dist/commands/verify.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/agents.d.ts +59 -0
- package/dist/lib/agents.d.ts.map +1 -0
- package/dist/lib/agents.js +760 -0
- package/dist/lib/agents.js.map +1 -0
- package/dist/lib/benchmark.d.ts +30 -0
- package/dist/lib/benchmark.d.ts.map +1 -0
- package/dist/lib/benchmark.js +325 -0
- package/dist/lib/benchmark.js.map +1 -0
- package/dist/lib/config.d.ts +6 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +15 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/discovery.d.ts +8 -0
- package/dist/lib/discovery.d.ts.map +1 -0
- package/dist/lib/discovery.js +80 -0
- package/dist/lib/discovery.js.map +1 -0
- package/dist/lib/executors.d.ts +33 -0
- package/dist/lib/executors.d.ts.map +1 -0
- package/dist/lib/executors.js +44 -0
- package/dist/lib/executors.js.map +1 -0
- package/dist/lib/filesystem.d.ts +4 -0
- package/dist/lib/filesystem.d.ts.map +1 -0
- package/dist/lib/filesystem.js +61 -0
- package/dist/lib/filesystem.js.map +1 -0
- package/dist/lib/interf.d.ts +37 -0
- package/dist/lib/interf.d.ts.map +1 -0
- package/dist/lib/interf.js +1104 -0
- package/dist/lib/interf.js.map +1 -0
- package/dist/lib/local-workflows.d.ts +35 -0
- package/dist/lib/local-workflows.d.ts.map +1 -0
- package/dist/lib/local-workflows.js +149 -0
- package/dist/lib/local-workflows.js.map +1 -0
- package/dist/lib/parse.d.ts +9 -0
- package/dist/lib/parse.d.ts.map +1 -0
- package/dist/lib/parse.js +51 -0
- package/dist/lib/parse.js.map +1 -0
- package/dist/lib/registry.d.ts +28 -0
- package/dist/lib/registry.d.ts.map +1 -0
- package/dist/lib/registry.js +80 -0
- package/dist/lib/registry.js.map +1 -0
- package/dist/lib/runtime.d.ts +59 -0
- package/dist/lib/runtime.d.ts.map +1 -0
- package/dist/lib/runtime.js +615 -0
- package/dist/lib/runtime.js.map +1 -0
- package/dist/lib/schema.d.ts +705 -0
- package/dist/lib/schema.d.ts.map +1 -0
- package/dist/lib/schema.js +443 -0
- package/dist/lib/schema.js.map +1 -0
- package/dist/lib/state.d.ts +49 -0
- package/dist/lib/state.d.ts.map +1 -0
- package/dist/lib/state.js +633 -0
- package/dist/lib/state.js.map +1 -0
- package/dist/lib/summarize-plan.d.ts +16 -0
- package/dist/lib/summarize-plan.d.ts.map +1 -0
- package/dist/lib/summarize-plan.js +112 -0
- package/dist/lib/summarize-plan.js.map +1 -0
- package/dist/lib/user-config.d.ts +6 -0
- package/dist/lib/user-config.d.ts.map +1 -0
- package/dist/lib/user-config.js +16 -0
- package/dist/lib/user-config.js.map +1 -0
- package/dist/lib/validate.d.ts +149 -0
- package/dist/lib/validate.d.ts.map +1 -0
- package/dist/lib/validate.js +838 -0
- package/dist/lib/validate.js.map +1 -0
- package/dist/lib/workflow-definitions.d.ts +79 -0
- package/dist/lib/workflow-definitions.d.ts.map +1 -0
- package/dist/lib/workflow-definitions.js +565 -0
- package/dist/lib/workflow-definitions.js.map +1 -0
- package/dist/lib/workflows.d.ts +125 -0
- package/dist/lib/workflows.d.ts.map +1 -0
- package/dist/lib/workflows.js +1107 -0
- package/dist/lib/workflows.js.map +1 -0
- package/package.json +73 -0
- package/skills/benchmark/SKILL.md +129 -0
- package/skills/interface/analyze/SKILL.md +140 -0
- package/skills/interface/compile/SKILL.md +153 -0
- package/skills/interface/compile/references/output-format.md +48 -0
- package/skills/interface/create/SKILL.md +264 -0
- package/skills/interface/create/references/compile-plan-format.md +109 -0
- package/skills/interface/create/references/workflows.md +50 -0
- package/skills/interface/query/SKILL.md +34 -0
- package/skills/interface/retrieve/SKILL.md +166 -0
- package/skills/knowledge-base/compile/SKILL.md +220 -0
- package/skills/knowledge-base/compile/references/output-format.md +48 -0
- package/skills/knowledge-base/compile/references/stage-claims.md +60 -0
- package/skills/knowledge-base/compile/references/stage-entities.md +46 -0
- package/skills/knowledge-base/query/SKILL.md +33 -0
- package/skills/knowledge-base/summarize/SKILL.md +122 -0
- package/skills/workflow/create/SKILL.md +126 -0
- package/templates/interface/README.md +158 -0
- package/templates/interface/interfaces.md +99 -0
- package/templates/knowledge-base/README.md +138 -0
- package/templates/knowledge-base/interfignore +19 -0
- package/templates/knowledge-base/registry.md +118 -0
|
@@ -0,0 +1,838 @@
|
|
|
1
|
+
import { existsSync, readFileSync, } from "node:fs";
|
|
2
|
+
import { basename, extname, join, resolve } from "node:path";
|
|
3
|
+
import { countFilesRecursive, listFilesRecursive } from "./filesystem.js";
|
|
4
|
+
import { discoverSourceFiles } from "./discovery.js";
|
|
5
|
+
import { readInterfConfig, resolveKnowledgeBaseSourcePath } from "./interf.js";
|
|
6
|
+
import { parseJsonFrontmatter, safeReadJsonFile } from "./parse.js";
|
|
7
|
+
import { getInterfaceWorkflow, resolveInterfaceWorkflowFromConfig, } from "./workflow-definitions.js";
|
|
8
|
+
import { InterfaceCoverageSchema, InterfaceRelevantSchema, InterfaceStateSchema, KnowledgeBaseInventorySchema, KnowledgeBaseStateSchema, } from "./schema.js";
|
|
9
|
+
function extractFirstSection(text, headings) {
|
|
10
|
+
for (const heading of headings) {
|
|
11
|
+
const section = extractSection(text, heading);
|
|
12
|
+
if (section)
|
|
13
|
+
return section;
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
function sectionHasAnyMarker(section, markers) {
|
|
18
|
+
if (typeof section !== "string")
|
|
19
|
+
return false;
|
|
20
|
+
return markers.some((marker) => new RegExp(`(^|\\n)\\s{0,3}#{0,6}\\s*${escapeRegExp(marker)}`, "im").test(section));
|
|
21
|
+
}
|
|
22
|
+
const REQUIRED_DIGEST_FIELDS = [
|
|
23
|
+
"source_kind",
|
|
24
|
+
"evidence_tier",
|
|
25
|
+
"truth_mode",
|
|
26
|
+
"state",
|
|
27
|
+
];
|
|
28
|
+
const MIN_ABSTRACT_WORDS = 5;
|
|
29
|
+
const WIKILINK_PATTERN = /!?\[\[([^[\]]+)\]\]/g;
|
|
30
|
+
export function validateKnowledgeBase(dirPath) {
|
|
31
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
32
|
+
const summaryFiles = listFilesRecursive(join(dirPath, "summaries"), isMarkdownFile);
|
|
33
|
+
const summaryValidation = validateSynthFiles(summaryFiles);
|
|
34
|
+
const brokenLinks = countBrokenWikilinks(dirPath, [join(dirPath, "summaries"), join(dirPath, "knowledge")], [join(dirPath, "summaries"), join(dirPath, "knowledge")]);
|
|
35
|
+
return {
|
|
36
|
+
config_present: config.present,
|
|
37
|
+
config_valid: config.valid,
|
|
38
|
+
config_type_match: config.typeMatch("knowledge-base"),
|
|
39
|
+
summary_frontmatter_valid: summaryValidation.invalid_frontmatter === 0,
|
|
40
|
+
abstracts_valid: summaryValidation.short_abstracts === 0,
|
|
41
|
+
invalid_frontmatter: summaryValidation.invalid_frontmatter,
|
|
42
|
+
short_abstracts: summaryValidation.short_abstracts,
|
|
43
|
+
broken_links: brokenLinks,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function validateInterfaceKnowledgeBase(dirPath) {
|
|
47
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
48
|
+
// Resolve knowledge-base path from config
|
|
49
|
+
const configValue = config.value;
|
|
50
|
+
const knowledgeBaseConfig = configValue?.knowledge_base;
|
|
51
|
+
const knowledgeBasePath = knowledgeBaseConfig?.path;
|
|
52
|
+
const resolvedKnowledgeBasePath = knowledgeBasePath ? resolve(dirPath, knowledgeBasePath) : null;
|
|
53
|
+
const knowledgeBasePathValid = resolvedKnowledgeBasePath !== null && existsSync(join(resolvedKnowledgeBasePath, "summaries"));
|
|
54
|
+
const brokenLinks = countBrokenWikilinks(dirPath, [join(dirPath, "knowledge"), join(dirPath, "briefs"), join(dirPath, "summaries")], [join(dirPath, "knowledge"), join(dirPath, "briefs"), join(dirPath, "summaries")]);
|
|
55
|
+
return {
|
|
56
|
+
config_present: config.present,
|
|
57
|
+
config_valid: config.valid,
|
|
58
|
+
config_type_match: config.typeMatch("interface"),
|
|
59
|
+
knowledge_base_path_valid: knowledgeBasePathValid,
|
|
60
|
+
broken_links: brokenLinks,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function validateKnowledgeBaseSummarize(dirPath) {
|
|
64
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
65
|
+
const configValue = config.value;
|
|
66
|
+
const sourceConfig = configValue?.source;
|
|
67
|
+
const sourcePath = resolveKnowledgeBaseSourcePath(dirPath, sourceConfig && typeof sourceConfig.path === "string"
|
|
68
|
+
? { type: "knowledge-base", name: basename(dirPath), source: { path: sourceConfig.path } }
|
|
69
|
+
: null);
|
|
70
|
+
const discovery = discoverSourceFiles(sourcePath, dirPath);
|
|
71
|
+
const sourceTotal = discovery.totalCount;
|
|
72
|
+
const summariesPresent = existsSync(join(dirPath, "summaries"));
|
|
73
|
+
const summaryFiles = listFilesRecursive(join(dirPath, "summaries"), isMarkdownFile);
|
|
74
|
+
const summaryValidation = validateSynthFiles(summaryFiles);
|
|
75
|
+
const summarized = summaryFiles.length;
|
|
76
|
+
const sourceCovered = Math.min(sourceTotal, summarized);
|
|
77
|
+
const toExtract = Math.max(0, sourceTotal - sourceCovered);
|
|
78
|
+
const required = sourceTotal > 0 || summarized > 0;
|
|
79
|
+
const checks = {
|
|
80
|
+
config_present: config.present,
|
|
81
|
+
config_valid: config.valid,
|
|
82
|
+
config_type_match: config.typeMatch("knowledge-base"),
|
|
83
|
+
summaries_present: summariesPresent,
|
|
84
|
+
full_source_coverage: toExtract === 0,
|
|
85
|
+
summary_frontmatter_valid: summaryValidation.invalid_frontmatter === 0,
|
|
86
|
+
abstracts_valid: summaryValidation.short_abstracts === 0,
|
|
87
|
+
};
|
|
88
|
+
const errors = [];
|
|
89
|
+
if (!checks.config_present)
|
|
90
|
+
errors.push("Missing interf.json.");
|
|
91
|
+
else if (!checks.config_valid)
|
|
92
|
+
errors.push("Could not parse interf.json.");
|
|
93
|
+
else if (!checks.config_type_match)
|
|
94
|
+
errors.push("Config is not a knowledge base.");
|
|
95
|
+
if (required && !checks.summaries_present)
|
|
96
|
+
errors.push("Missing summaries/ directory.");
|
|
97
|
+
if (!checks.full_source_coverage)
|
|
98
|
+
errors.push("Not every source file has a summary yet.");
|
|
99
|
+
if (!checks.summary_frontmatter_valid)
|
|
100
|
+
errors.push("Some summary files have invalid or incomplete frontmatter.");
|
|
101
|
+
if (!checks.abstracts_valid)
|
|
102
|
+
errors.push("Some summary files are missing a valid abstract block.");
|
|
103
|
+
const ok = errors.length === 0;
|
|
104
|
+
const summary = !required
|
|
105
|
+
? "Extract not required yet — no source files discovered."
|
|
106
|
+
: ok
|
|
107
|
+
? `Extract verified — ${sourceCovered}/${sourceTotal} source files are covered by valid summaries.`
|
|
108
|
+
: `Extract failed — ${errors[0] ?? "incomplete summary coverage."}`;
|
|
109
|
+
return {
|
|
110
|
+
ok,
|
|
111
|
+
required,
|
|
112
|
+
summary,
|
|
113
|
+
counts: {
|
|
114
|
+
source_total: sourceTotal,
|
|
115
|
+
source_covered: sourceCovered,
|
|
116
|
+
summarized,
|
|
117
|
+
to_summarize: toExtract,
|
|
118
|
+
invalid_frontmatter: summaryValidation.invalid_frontmatter,
|
|
119
|
+
short_abstracts: summaryValidation.short_abstracts,
|
|
120
|
+
},
|
|
121
|
+
checks,
|
|
122
|
+
errors,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export function validateKnowledgeBaseCompile(dirPath) {
|
|
126
|
+
const extractValidation = validateKnowledgeBaseSummarize(dirPath);
|
|
127
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
128
|
+
const statePath = join(dirPath, ".interf", "state.json");
|
|
129
|
+
const inventoryPath = join(dirPath, ".interf", "inventory.json");
|
|
130
|
+
const statePresent = existsSync(statePath);
|
|
131
|
+
const inventoryPresent = existsSync(inventoryPath);
|
|
132
|
+
const state = statePresent
|
|
133
|
+
? safeReadJsonFile(statePath, "knowledge-base state")
|
|
134
|
+
: null;
|
|
135
|
+
const inventory = inventoryPresent
|
|
136
|
+
? safeReadJsonFile(inventoryPath, "knowledge-base inventory")
|
|
137
|
+
: null;
|
|
138
|
+
const summarized = countFilesRecursive(join(dirPath, "summaries"));
|
|
139
|
+
const compiled = asNumber(state?.compiled);
|
|
140
|
+
const legacyInventoryFiles = Array.isArray(inventory?.files)
|
|
141
|
+
? inventory.files.filter((item) => !!item && typeof item === "object")
|
|
142
|
+
: [];
|
|
143
|
+
const summaryInventoryFiles = Array.isArray(inventory?.summaries)
|
|
144
|
+
? inventory.summaries.filter((item) => !!item && typeof item === "object")
|
|
145
|
+
: [];
|
|
146
|
+
const entryInventoryFiles = Array.isArray(inventory?.entries)
|
|
147
|
+
? inventory.entries.filter((item) => !!item && typeof item === "object")
|
|
148
|
+
: [];
|
|
149
|
+
const inventoryTotal = typeof inventory?.total === "number"
|
|
150
|
+
? inventory.total
|
|
151
|
+
: typeof inventory?.summary_total === "number"
|
|
152
|
+
? inventory.summary_total
|
|
153
|
+
: entryInventoryFiles.length;
|
|
154
|
+
const inventoryFiles = legacyInventoryFiles.length > 0
|
|
155
|
+
? legacyInventoryFiles
|
|
156
|
+
: summaryInventoryFiles.length > 0
|
|
157
|
+
? summaryInventoryFiles
|
|
158
|
+
: entryInventoryFiles;
|
|
159
|
+
const frontmatterScanned = legacyInventoryFiles.length > 0
|
|
160
|
+
? inventoryFiles.filter((file) => file.frontmatter_scanned === true).length
|
|
161
|
+
: summaryInventoryFiles.length > 0
|
|
162
|
+
? inventoryFiles.filter((file) => typeof file.file === "string" && typeof file.source === "string").length
|
|
163
|
+
: inventoryFiles.filter((file) => (typeof file.summary === "string" || typeof file.digest === "string") &&
|
|
164
|
+
typeof file.source === "string").length;
|
|
165
|
+
const abstractsRead = legacyInventoryFiles.length > 0
|
|
166
|
+
? inventoryFiles.filter((file) => file.abstract_read === true).length
|
|
167
|
+
: summaryInventoryFiles.length > 0
|
|
168
|
+
? inventoryFiles.filter((file) => typeof file.status === "string").length
|
|
169
|
+
: inventoryFiles.filter((file) => typeof file.state === "string").length;
|
|
170
|
+
const entryAbstractsRead = entryInventoryFiles.length > 0
|
|
171
|
+
? entryInventoryFiles.filter((file) => typeof file.abstract === "string" && file.abstract.trim().length > 0).length
|
|
172
|
+
: 0;
|
|
173
|
+
const effectiveAbstractsRead = entryInventoryFiles.length > 0 && summaryInventoryFiles.length === 0 && legacyInventoryFiles.length === 0
|
|
174
|
+
? Math.max(abstractsRead, entryAbstractsRead)
|
|
175
|
+
: abstractsRead;
|
|
176
|
+
const fullReads = asNumber(state?.full_reads);
|
|
177
|
+
const entityFiles = countFilesRecursive(join(dirPath, "knowledge", "entities"));
|
|
178
|
+
const claimFiles = countFilesRecursive(join(dirPath, "knowledge", "claims"));
|
|
179
|
+
const indexFiles = countFilesRecursive(join(dirPath, "knowledge", "indexes"));
|
|
180
|
+
const outputs = entityFiles + claimFiles + indexFiles;
|
|
181
|
+
const brokenLinks = countBrokenWikilinks(dirPath, [join(dirPath, "summaries"), join(dirPath, "knowledge")], [join(dirPath, "summaries"), join(dirPath, "knowledge")]);
|
|
182
|
+
const required = extractValidation.required || outputs > 0 || compiled > 0 || inventoryPresent;
|
|
183
|
+
const checks = {
|
|
184
|
+
config_present: config.present,
|
|
185
|
+
config_valid: config.valid,
|
|
186
|
+
config_type_match: config.typeMatch("knowledge-base"),
|
|
187
|
+
summarize_complete: extractValidation.ok && extractValidation.required,
|
|
188
|
+
state_present: statePresent,
|
|
189
|
+
state_valid: state !== null && KnowledgeBaseStateSchema.safeParse(state).success,
|
|
190
|
+
inventory_present: inventoryPresent,
|
|
191
|
+
inventory_valid: inventory !== null && KnowledgeBaseInventorySchema.safeParse(inventory).success,
|
|
192
|
+
inventory_complete: Boolean(state?.inventory_complete) === true,
|
|
193
|
+
inventory_matches_summaries: inventoryTotal === summarized && inventoryFiles.length === summarized,
|
|
194
|
+
frontmatter_inventory_complete: frontmatterScanned === summarized,
|
|
195
|
+
abstracts_cover_summaries: asNumber(state?.abstracts_read) >= summarized && effectiveAbstractsRead >= summarized,
|
|
196
|
+
synced_complete: compiled >= summarized && summarized > 0,
|
|
197
|
+
outputs_present: outputs > 0,
|
|
198
|
+
links_valid: brokenLinks === 0,
|
|
199
|
+
};
|
|
200
|
+
const errors = [];
|
|
201
|
+
if (!checks.config_present)
|
|
202
|
+
errors.push("Missing interf.json.");
|
|
203
|
+
else if (!checks.config_valid)
|
|
204
|
+
errors.push("Could not parse interf.json.");
|
|
205
|
+
else if (!checks.config_type_match)
|
|
206
|
+
errors.push("Config is not a knowledge base.");
|
|
207
|
+
if (!checks.summarize_complete)
|
|
208
|
+
errors.push("Knowledge-base summarize is incomplete or invalid.");
|
|
209
|
+
if (!checks.state_present)
|
|
210
|
+
errors.push("Missing .interf/state.json.");
|
|
211
|
+
else if (!checks.state_valid)
|
|
212
|
+
errors.push("Could not parse .interf/state.json.");
|
|
213
|
+
if (!checks.inventory_present)
|
|
214
|
+
errors.push("Missing .interf/inventory.json.");
|
|
215
|
+
else if (!checks.inventory_valid)
|
|
216
|
+
errors.push("Could not parse .interf/inventory.json or it has the wrong shape.");
|
|
217
|
+
if (checks.inventory_present && checks.inventory_valid && !checks.inventory_complete) {
|
|
218
|
+
errors.push("state.json does not mark inventory_complete.");
|
|
219
|
+
}
|
|
220
|
+
if (checks.inventory_present && checks.inventory_valid && !checks.inventory_matches_summaries) {
|
|
221
|
+
errors.push("Inventory does not match the summary set.");
|
|
222
|
+
}
|
|
223
|
+
if (checks.inventory_present && checks.inventory_valid && !checks.frontmatter_inventory_complete) {
|
|
224
|
+
errors.push("Not every summary is marked frontmatter_scanned in inventory.");
|
|
225
|
+
}
|
|
226
|
+
if (checks.state_present && checks.state_valid && !checks.abstracts_cover_summaries) {
|
|
227
|
+
errors.push("Abstract review does not cover the full summary set.");
|
|
228
|
+
}
|
|
229
|
+
if (checks.state_present && checks.state_valid && !checks.synced_complete) {
|
|
230
|
+
errors.push("state.json compiled count does not cover the full summary set.");
|
|
231
|
+
}
|
|
232
|
+
if (!checks.outputs_present)
|
|
233
|
+
errors.push("Knowledge-base compile outputs are missing or empty.");
|
|
234
|
+
const ok = required ? errors.length === 0 : true;
|
|
235
|
+
const summary = !required
|
|
236
|
+
? "Knowledge-base compile not required yet — no summaries exist yet."
|
|
237
|
+
: ok
|
|
238
|
+
? `Knowledge-base compile verified — ${compiled}/${summarized} summaries compiled, ${entityFiles} entities, ${claimFiles} claims.`
|
|
239
|
+
: `Knowledge-base compile failed — ${errors[0] ?? "incomplete graph compile."}`;
|
|
240
|
+
return {
|
|
241
|
+
ok,
|
|
242
|
+
required,
|
|
243
|
+
summary,
|
|
244
|
+
counts: {
|
|
245
|
+
source_total: extractValidation.counts.source_total,
|
|
246
|
+
summarized,
|
|
247
|
+
compiled,
|
|
248
|
+
inventory_total: inventoryTotal,
|
|
249
|
+
frontmatter_scanned: frontmatterScanned,
|
|
250
|
+
abstracts_read: Math.max(asNumber(state?.abstracts_read), effectiveAbstractsRead),
|
|
251
|
+
full_reads: fullReads,
|
|
252
|
+
entity_files: entityFiles,
|
|
253
|
+
claim_files: claimFiles,
|
|
254
|
+
index_files: indexFiles,
|
|
255
|
+
outputs,
|
|
256
|
+
},
|
|
257
|
+
checks,
|
|
258
|
+
errors,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
export function validateInterfacePlan(dirPath) {
|
|
262
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
263
|
+
const configValue = config.value;
|
|
264
|
+
const knowledgeBaseConfig = configValue?.knowledge_base;
|
|
265
|
+
const knowledgeBasePath = knowledgeBaseConfig?.path;
|
|
266
|
+
const resolvedKnowledgeBasePath = knowledgeBasePath ? resolve(dirPath, knowledgeBasePath) : null;
|
|
267
|
+
const knowledgeBasePathValid = resolvedKnowledgeBasePath !== null && existsSync(join(resolvedKnowledgeBasePath, "summaries"));
|
|
268
|
+
const compilePlanPath = join(dirPath, "compile-plan.md");
|
|
269
|
+
const agentsPath = join(dirPath, "AGENTS.md");
|
|
270
|
+
const homePath = join(dirPath, "home.md");
|
|
271
|
+
const compilePlanPresent = existsSync(compilePlanPath);
|
|
272
|
+
const compilePlanText = compilePlanPresent ? safeReadText(compilePlanPath) : null;
|
|
273
|
+
const workflowId = resolveInterfaceWorkflowFromConfig(configValue);
|
|
274
|
+
const sourcePath = knowledgeBasePathValid && resolvedKnowledgeBasePath
|
|
275
|
+
? resolveKnowledgeBaseSourcePath(resolvedKnowledgeBasePath)
|
|
276
|
+
: undefined;
|
|
277
|
+
const workflow = getInterfaceWorkflow(workflowId, { sourcePath, workspacePath: dirPath });
|
|
278
|
+
const stageChecks = workflow.stages.map((stage, index) => {
|
|
279
|
+
const section = compilePlanText
|
|
280
|
+
? extractFirstSection(compilePlanText, [
|
|
281
|
+
`Stage ${index + 1}: ${stage.label}`,
|
|
282
|
+
`Stage ${index + 1} — ${stage.label}`,
|
|
283
|
+
])
|
|
284
|
+
: null;
|
|
285
|
+
return {
|
|
286
|
+
id: stage.id,
|
|
287
|
+
label: stage.label,
|
|
288
|
+
contract_type: stage.contractType,
|
|
289
|
+
present: section !== null,
|
|
290
|
+
defined: sectionMatchesInterfaceContract(section, stage.contractType),
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
const compilePlanSections = stageChecks.every((stage) => stage.present);
|
|
294
|
+
const stage1Defined = stageChecks[0]?.defined ?? false;
|
|
295
|
+
const stage2Defined = stageChecks[1]?.defined ?? false;
|
|
296
|
+
const stage3Defined = stageChecks[2]?.defined ?? false;
|
|
297
|
+
const allStageDefinitionsValid = stageChecks.every((stage) => stage.defined);
|
|
298
|
+
const agentsPresent = existsSync(agentsPath);
|
|
299
|
+
const homePresent = existsSync(homePath);
|
|
300
|
+
const outputMentions = countMatches(compilePlanText ?? "", /(?:briefs\/|summaries\/|knowledge\/|home\.md)/gi);
|
|
301
|
+
const required = true;
|
|
302
|
+
const checks = {
|
|
303
|
+
config_present: config.present,
|
|
304
|
+
config_valid: config.valid,
|
|
305
|
+
config_type_match: config.typeMatch("interface"),
|
|
306
|
+
knowledge_base_path_valid: knowledgeBasePathValid,
|
|
307
|
+
compile_plan_present: compilePlanPresent,
|
|
308
|
+
compile_plan_sections: compilePlanSections,
|
|
309
|
+
stage1_defined: stage1Defined,
|
|
310
|
+
stage2_defined: stage2Defined,
|
|
311
|
+
stage3_defined: stage3Defined,
|
|
312
|
+
all_stage_definitions_valid: allStageDefinitionsValid,
|
|
313
|
+
stage_checks: stageChecks,
|
|
314
|
+
agents_present: agentsPresent,
|
|
315
|
+
home_present: homePresent,
|
|
316
|
+
};
|
|
317
|
+
const errors = [];
|
|
318
|
+
if (!checks.config_present)
|
|
319
|
+
errors.push("Missing interf.json.");
|
|
320
|
+
else if (!checks.config_valid)
|
|
321
|
+
errors.push("Could not parse interf.json.");
|
|
322
|
+
else if (!checks.config_type_match)
|
|
323
|
+
errors.push("Config is not an interface.");
|
|
324
|
+
if (!checks.knowledge_base_path_valid)
|
|
325
|
+
errors.push("knowledgeBase.path does not resolve to a valid knowledgeBase.");
|
|
326
|
+
if (!checks.compile_plan_present)
|
|
327
|
+
errors.push("Missing compile-plan.md.");
|
|
328
|
+
else if (!checks.compile_plan_sections)
|
|
329
|
+
errors.push("compile-plan.md is missing one or more required stage sections.");
|
|
330
|
+
if (checks.compile_plan_present) {
|
|
331
|
+
for (const [index, stage] of stageChecks.entries()) {
|
|
332
|
+
if (!stage.defined) {
|
|
333
|
+
errors.push(`Stage ${index + 1} ${stage.label.toLowerCase()} plan is incomplete.`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (!checks.agents_present)
|
|
338
|
+
errors.push("Missing AGENTS.md.");
|
|
339
|
+
if (!checks.home_present)
|
|
340
|
+
errors.push("Missing home.md.");
|
|
341
|
+
const ok = errors.length === 0;
|
|
342
|
+
const summary = ok
|
|
343
|
+
? `Interface plan verified — ${stageChecks.filter((stage) => stage.defined).length}/${stageChecks.length} stages defined with ${outputMentions} output references.`
|
|
344
|
+
: `Interface plan failed — ${errors[0] ?? "compile plan is incomplete."}`;
|
|
345
|
+
return {
|
|
346
|
+
ok,
|
|
347
|
+
required,
|
|
348
|
+
summary,
|
|
349
|
+
counts: {
|
|
350
|
+
stage_sections_expected: stageChecks.length,
|
|
351
|
+
stage_sections_present: stageChecks.filter((stage) => stage.present).length,
|
|
352
|
+
output_mentions: outputMentions,
|
|
353
|
+
},
|
|
354
|
+
checks,
|
|
355
|
+
errors,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
export function validateInterfaceRetrieve(dirPath) {
|
|
359
|
+
const config = readKnowledgeBaseConfig(dirPath);
|
|
360
|
+
const configValue = config.value;
|
|
361
|
+
const knowledgeBaseConfig = configValue?.knowledge_base;
|
|
362
|
+
const knowledgeBasePath = knowledgeBaseConfig?.path;
|
|
363
|
+
const resolvedKnowledgeBasePath = knowledgeBasePath ? resolve(dirPath, knowledgeBasePath) : null;
|
|
364
|
+
const knowledgeBasePathValid = resolvedKnowledgeBasePath !== null && existsSync(join(resolvedKnowledgeBasePath, "summaries"));
|
|
365
|
+
const knowledgeBaseTotal = knowledgeBasePathValid ? countFilesRecursive(join(resolvedKnowledgeBasePath, "summaries")) : 0;
|
|
366
|
+
const statePath = join(dirPath, ".interf", "state.json");
|
|
367
|
+
const relevantPath = join(dirPath, ".interf", "relevant.json");
|
|
368
|
+
const coveragePath = join(dirPath, ".interf", "coverage.json");
|
|
369
|
+
const statePresent = existsSync(statePath);
|
|
370
|
+
const relevantPresent = existsSync(relevantPath);
|
|
371
|
+
const coveragePresent = existsSync(coveragePath);
|
|
372
|
+
const state = statePresent
|
|
373
|
+
? safeReadJsonFile(statePath, "interface state")
|
|
374
|
+
: null;
|
|
375
|
+
const relevant = relevantPresent
|
|
376
|
+
? safeReadJsonFile(relevantPath, "interface retrieved set")
|
|
377
|
+
: null;
|
|
378
|
+
const coverage = coveragePresent
|
|
379
|
+
? safeReadJsonFile(coveragePath, "interface coverage proof")
|
|
380
|
+
: null;
|
|
381
|
+
const queryStarted = Boolean(state?.retrieve_complete) ||
|
|
382
|
+
Boolean(state?.analyze_complete) ||
|
|
383
|
+
Boolean(state?.compile_complete) ||
|
|
384
|
+
typeof state?.last_retrieve === "string" ||
|
|
385
|
+
typeof state?.last_analyze === "string" ||
|
|
386
|
+
typeof state?.last_compile === "string" ||
|
|
387
|
+
relevantPresent ||
|
|
388
|
+
coveragePresent;
|
|
389
|
+
const proof = coverage && typeof coverage.proof === "object" && coverage.proof
|
|
390
|
+
? coverage.proof
|
|
391
|
+
: null;
|
|
392
|
+
const bridgeRelevantEntries = asFileEntries(relevant?.relevant);
|
|
393
|
+
const bridgeRejectedEntries = asFileEntries(relevant?.rejected);
|
|
394
|
+
const bridgeCoverageShape = coverage !== null &&
|
|
395
|
+
typeof coverage?.query_timestamp === "string" &&
|
|
396
|
+
typeof coverage?.coverage_complete === "boolean" &&
|
|
397
|
+
!!coverage?.entities &&
|
|
398
|
+
typeof coverage.entities === "object" &&
|
|
399
|
+
!!coverage?.comparators &&
|
|
400
|
+
typeof coverage.comparators === "object" &&
|
|
401
|
+
Array.isArray(coverage?.gaps);
|
|
402
|
+
const derivedRelevantFiles = asStringArray(proof?.retrieved).length > 0
|
|
403
|
+
? asStringArray(proof?.retrieved)
|
|
404
|
+
: bridgeRelevantEntries.map((entry) => entry.file);
|
|
405
|
+
const relevantFiles = asStringArray(relevant?.relevant_files).length > 0
|
|
406
|
+
? asStringArray(relevant?.relevant_files)
|
|
407
|
+
: derivedRelevantFiles;
|
|
408
|
+
const deltaFiles = asStringArray(relevant?.delta_files).length > 0
|
|
409
|
+
? asStringArray(relevant?.delta_files)
|
|
410
|
+
: asStringArray(state?.delta_files);
|
|
411
|
+
const explicitCandidateFiles = asStringArray(coverage?.candidate_files);
|
|
412
|
+
const scannedFiles = asStringArray(proof?.scanned).length > 0
|
|
413
|
+
? asStringArray(proof?.scanned)
|
|
414
|
+
: dedupeStrings([
|
|
415
|
+
...bridgeRelevantEntries.map((entry) => entry.file),
|
|
416
|
+
...bridgeRejectedEntries.map((entry) => entry.file),
|
|
417
|
+
]);
|
|
418
|
+
const candidateFiles = explicitCandidateFiles.length > 0
|
|
419
|
+
? explicitCandidateFiles
|
|
420
|
+
: scannedFiles;
|
|
421
|
+
const abstractReviewedFiles = asStringArray(coverage?.abstract_reviewed_files).length > 0
|
|
422
|
+
? asStringArray(coverage?.abstract_reviewed_files)
|
|
423
|
+
: asStringArray(proof?.reviewed).length > 0
|
|
424
|
+
? asStringArray(proof?.reviewed)
|
|
425
|
+
: candidateFiles;
|
|
426
|
+
const selectedFiles = asStringArray(coverage?.selected_files).length > 0
|
|
427
|
+
? asStringArray(coverage?.selected_files)
|
|
428
|
+
: derivedRelevantFiles;
|
|
429
|
+
const rejectedFiles = asRejectedEntries(coverage?.rejected_files);
|
|
430
|
+
const rejectedPaths = rejectedFiles.length > 0
|
|
431
|
+
? rejectedFiles.map((entry) => entry.path)
|
|
432
|
+
: asStringArray(proof?.excluded).length > 0
|
|
433
|
+
? asStringArray(proof?.excluded)
|
|
434
|
+
: bridgeRejectedEntries.map((entry) => entry.file);
|
|
435
|
+
const frontmatterScanned = asNumber(coverage?.frontmatter_scanned) ||
|
|
436
|
+
asNumber(state?.frontmatter_scanned) ||
|
|
437
|
+
asNumber(relevant?.total_scanned);
|
|
438
|
+
const candidateCount = asNumber(coverage?.candidates_after_frontmatter) ||
|
|
439
|
+
asNumber(state?.candidate_count) ||
|
|
440
|
+
candidateFiles.length;
|
|
441
|
+
const abstractsRead = asNumber(coverage?.abstracts_reviewed) ||
|
|
442
|
+
asNumber(state?.abstracts_read) ||
|
|
443
|
+
abstractReviewedFiles.length;
|
|
444
|
+
const selectedCount = asNumber(coverage?.relevant_selected) ||
|
|
445
|
+
asNumber(state?.relevant_count) ||
|
|
446
|
+
asNumber(relevant?.total_relevant) ||
|
|
447
|
+
selectedFiles.length;
|
|
448
|
+
const rejectedCount = asNumber(coverage?.rejected) ||
|
|
449
|
+
asNumber(state?.rejected_count) ||
|
|
450
|
+
asNumber(relevant?.total_rejected) ||
|
|
451
|
+
rejectedPaths.length;
|
|
452
|
+
const deltaCount = asNumber(state?.delta_count) || deltaFiles.length;
|
|
453
|
+
const expansionPasses = asNumber(coverage?.expansion_passes) ||
|
|
454
|
+
asNumber(state?.expansion_passes);
|
|
455
|
+
const relevantValid = (relevant !== null &&
|
|
456
|
+
typeof relevant?.generated_at === "string" &&
|
|
457
|
+
typeof relevant?.knowledge_base_summary_count === "number" &&
|
|
458
|
+
Array.isArray(relevant?.relevant_files) &&
|
|
459
|
+
Array.isArray(relevant?.delta_files)) ||
|
|
460
|
+
(relevant !== null &&
|
|
461
|
+
typeof relevant?.query_timestamp === "string" &&
|
|
462
|
+
Array.isArray(relevant?.relevant) &&
|
|
463
|
+
Array.isArray(relevant?.rejected) &&
|
|
464
|
+
typeof relevant?.total_scanned === "number" &&
|
|
465
|
+
typeof relevant?.total_relevant === "number" &&
|
|
466
|
+
typeof relevant?.total_rejected === "number") ||
|
|
467
|
+
Array.isArray(proof?.retrieved);
|
|
468
|
+
const coverageValid = (coverage !== null &&
|
|
469
|
+
typeof coverage?.generated_at === "string" &&
|
|
470
|
+
typeof coverage?.knowledge_base_summary_count === "number" &&
|
|
471
|
+
typeof coverage?.frontmatter_scanned === "number" &&
|
|
472
|
+
typeof coverage?.expansion_passes === "number" &&
|
|
473
|
+
Array.isArray(coverage?.candidate_files) &&
|
|
474
|
+
Array.isArray(coverage?.abstract_reviewed_files) &&
|
|
475
|
+
Array.isArray(coverage?.selected_files) &&
|
|
476
|
+
Array.isArray(coverage?.rejected_files) &&
|
|
477
|
+
rejectedFiles.length === rejectedPaths.length) ||
|
|
478
|
+
(coverage !== null &&
|
|
479
|
+
typeof coverage?.generated_at === "string" &&
|
|
480
|
+
typeof coverage?.knowledge_base_summary_total === "number" &&
|
|
481
|
+
typeof coverage?.frontmatter_scanned === "number" &&
|
|
482
|
+
typeof coverage?.candidates_after_frontmatter === "number" &&
|
|
483
|
+
typeof coverage?.abstracts_reviewed === "number" &&
|
|
484
|
+
typeof coverage?.relevant_selected === "number" &&
|
|
485
|
+
typeof coverage?.rejected === "number" &&
|
|
486
|
+
typeof coverage?.expansion_passes === "number" &&
|
|
487
|
+
typeof coverage?.coverage_complete === "boolean" &&
|
|
488
|
+
proof !== null &&
|
|
489
|
+
Array.isArray(proof.scanned) &&
|
|
490
|
+
Array.isArray(proof.reviewed) &&
|
|
491
|
+
Array.isArray(proof.retrieved) &&
|
|
492
|
+
Array.isArray(proof.excluded)) ||
|
|
493
|
+
bridgeCoverageShape;
|
|
494
|
+
const selectedSet = new Set(selectedFiles);
|
|
495
|
+
const rejectedSet = new Set(rejectedPaths);
|
|
496
|
+
const candidateSet = new Set(candidateFiles);
|
|
497
|
+
const scannedSet = new Set(scannedFiles);
|
|
498
|
+
const abstractSet = new Set(abstractReviewedFiles);
|
|
499
|
+
const relevantSet = new Set(relevantFiles);
|
|
500
|
+
const selectedRejectedUnion = new Set([...selectedFiles, ...rejectedPaths]);
|
|
501
|
+
const usesProofScanCoverage = explicitCandidateFiles.length === 0 &&
|
|
502
|
+
Array.isArray(proof?.scanned) &&
|
|
503
|
+
typeof coverage?.candidates_after_frontmatter === "number";
|
|
504
|
+
const knowledgeBaseCountMatch = knowledgeBasePathValid &&
|
|
505
|
+
coverageValid &&
|
|
506
|
+
(relevant !== null && typeof relevant?.knowledge_base_summary_count === "number"
|
|
507
|
+
? asNumber(relevant.knowledge_base_summary_count) === knowledgeBaseTotal
|
|
508
|
+
: frontmatterScanned === knowledgeBaseTotal || candidateCount <= knowledgeBaseTotal) &&
|
|
509
|
+
(typeof coverage?.knowledge_base_summary_count === "number"
|
|
510
|
+
? asNumber(coverage.knowledge_base_summary_count) === knowledgeBaseTotal
|
|
511
|
+
: typeof coverage?.knowledge_base_summary_total === "number"
|
|
512
|
+
? asNumber(coverage?.knowledge_base_summary_total) === knowledgeBaseTotal
|
|
513
|
+
: frontmatterScanned === knowledgeBaseTotal);
|
|
514
|
+
const fullFrontmatterScan = knowledgeBasePathValid && coverageValid && frontmatterScanned === knowledgeBaseTotal;
|
|
515
|
+
const candidatePartitionValid = coverageValid &&
|
|
516
|
+
setIntersectionSize(selectedSet, rejectedSet) === 0 &&
|
|
517
|
+
(usesProofScanCoverage
|
|
518
|
+
? scannedSet.size === selectedRejectedUnion.size &&
|
|
519
|
+
isSubset(selectedSet, scannedSet) &&
|
|
520
|
+
isSubset(rejectedSet, scannedSet)
|
|
521
|
+
: candidateSet.size === selectedRejectedUnion.size &&
|
|
522
|
+
isSubset(selectedSet, candidateSet) &&
|
|
523
|
+
isSubset(rejectedSet, candidateSet));
|
|
524
|
+
const abstractsCoverCandidates = coverageValid &&
|
|
525
|
+
(usesProofScanCoverage
|
|
526
|
+
? selectedFiles.every((file) => abstractSet.has(file)) &&
|
|
527
|
+
abstractSet.size >= candidateCount &&
|
|
528
|
+
abstractsRead >= candidateCount
|
|
529
|
+
: isSubset(candidateSet, abstractSet));
|
|
530
|
+
const selectedMatchRelevant = coverageValid &&
|
|
531
|
+
relevantValid &&
|
|
532
|
+
setsEqual(selectedSet, relevantSet);
|
|
533
|
+
const deltaSubset = relevantValid &&
|
|
534
|
+
isSubset(new Set(deltaFiles), selectedSet);
|
|
535
|
+
const stateCountsMatch = state !== null &&
|
|
536
|
+
asNumber(state.frontmatter_scanned) === frontmatterScanned &&
|
|
537
|
+
asNumber(state.candidate_count) === candidateCount &&
|
|
538
|
+
asNumber(state.abstracts_read) === abstractsRead &&
|
|
539
|
+
asNumber(state.expansion_passes) === expansionPasses &&
|
|
540
|
+
asNumber(state.relevant_count) === selectedCount &&
|
|
541
|
+
asNumber(state.rejected_count) === rejectedCount &&
|
|
542
|
+
asNumber(state.delta_count) === deltaCount;
|
|
543
|
+
const stateFlagsMatch = state !== null &&
|
|
544
|
+
state.retrieve_complete === true &&
|
|
545
|
+
state.coverage_complete === true;
|
|
546
|
+
const checks = {
|
|
547
|
+
knowledge_base_path_valid: knowledgeBasePathValid,
|
|
548
|
+
state_present: statePresent,
|
|
549
|
+
state_valid: state !== null && InterfaceStateSchema.safeParse(state).success,
|
|
550
|
+
relevant_present: relevantPresent || Array.isArray(proof?.retrieved),
|
|
551
|
+
relevant_valid: relevantValid ||
|
|
552
|
+
(relevant !== null && InterfaceRelevantSchema.safeParse(relevant).success),
|
|
553
|
+
coverage_present: coveragePresent,
|
|
554
|
+
coverage_valid: coverageValid ||
|
|
555
|
+
(coverage !== null && InterfaceCoverageSchema.safeParse(coverage).success),
|
|
556
|
+
knowledge_base_count_match: knowledgeBaseCountMatch,
|
|
557
|
+
full_frontmatter_scan: fullFrontmatterScan,
|
|
558
|
+
candidate_partition_valid: candidatePartitionValid,
|
|
559
|
+
abstracts_cover_candidates: abstractsCoverCandidates,
|
|
560
|
+
selected_match_relevant: selectedMatchRelevant,
|
|
561
|
+
delta_subset: deltaSubset,
|
|
562
|
+
state_counts_match: stateCountsMatch,
|
|
563
|
+
state_flags_match: stateFlagsMatch,
|
|
564
|
+
};
|
|
565
|
+
const errors = [];
|
|
566
|
+
if (queryStarted) {
|
|
567
|
+
if (!knowledgeBasePathValid)
|
|
568
|
+
errors.push("Knowledge base is missing or invalid.");
|
|
569
|
+
if (!statePresent)
|
|
570
|
+
errors.push("Missing .interf/state.json.");
|
|
571
|
+
else if (state === null)
|
|
572
|
+
errors.push("Could not parse .interf/state.json.");
|
|
573
|
+
if (!relevantPresent && !Array.isArray(proof?.retrieved))
|
|
574
|
+
errors.push("Missing .interf/relevant.json.");
|
|
575
|
+
else if (!relevantValid)
|
|
576
|
+
errors.push("Could not parse .interf/relevant.json or it has the wrong shape.");
|
|
577
|
+
if (!coveragePresent)
|
|
578
|
+
errors.push("Missing .interf/coverage.json.");
|
|
579
|
+
else if (!coverageValid)
|
|
580
|
+
errors.push("Could not parse .interf/coverage.json or it has the wrong shape.");
|
|
581
|
+
if (coveragePresent && coverageValid && !knowledgeBaseCountMatch)
|
|
582
|
+
errors.push("Coverage proof does not match the current knowledge-base summary count.");
|
|
583
|
+
if (coveragePresent && coverageValid && !fullFrontmatterScan)
|
|
584
|
+
errors.push("Coverage proof does not show a full frontmatter scan.");
|
|
585
|
+
if (coveragePresent && coverageValid && !candidatePartitionValid)
|
|
586
|
+
errors.push("Candidate, selected, and rejected sets are inconsistent.");
|
|
587
|
+
if (coveragePresent && coverageValid && !abstractsCoverCandidates)
|
|
588
|
+
errors.push("Not every candidate file was abstract-reviewed.");
|
|
589
|
+
if (relevantPresent && coveragePresent && relevantValid && coverageValid && !selectedMatchRelevant) {
|
|
590
|
+
errors.push("Selected files in coverage proof do not match relevant_files.");
|
|
591
|
+
}
|
|
592
|
+
if (relevantPresent && coveragePresent && relevantValid && coverageValid && !deltaSubset) {
|
|
593
|
+
errors.push("delta_files are not a subset of selected relevant files.");
|
|
594
|
+
}
|
|
595
|
+
if (statePresent && state !== null && coveragePresent && coverageValid && relevantPresent && relevantValid && !stateCountsMatch) {
|
|
596
|
+
errors.push("state.json counts do not match the retrieved set and coverage proof.");
|
|
597
|
+
}
|
|
598
|
+
if (statePresent && state !== null && !stateFlagsMatch) {
|
|
599
|
+
errors.push("state.json is missing retrieve_complete or coverage_complete.");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const ok = queryStarted ? errors.length === 0 : true;
|
|
603
|
+
const summary = !queryStarted
|
|
604
|
+
? "Coverage not required yet — interface retrieve has not started."
|
|
605
|
+
: ok
|
|
606
|
+
? `Coverage verified — scanned ${frontmatterScanned}/${knowledgeBaseTotal}, reviewed ${abstractsRead} abstracts, selected ${selectedCount}, rejected ${rejectedCount}.`
|
|
607
|
+
: `Coverage failed — ${errors[0] ?? "inconsistent retrieval proof."}`;
|
|
608
|
+
return {
|
|
609
|
+
ok,
|
|
610
|
+
required: queryStarted,
|
|
611
|
+
summary,
|
|
612
|
+
counts: {
|
|
613
|
+
knowledge_base_total: knowledgeBaseTotal,
|
|
614
|
+
frontmatter_scanned: frontmatterScanned,
|
|
615
|
+
candidate_count: candidateCount,
|
|
616
|
+
abstracts_read: abstractsRead,
|
|
617
|
+
selected_count: selectedCount,
|
|
618
|
+
rejected_count: rejectedCount,
|
|
619
|
+
delta_count: deltaCount,
|
|
620
|
+
expansion_passes: expansionPasses,
|
|
621
|
+
},
|
|
622
|
+
checks,
|
|
623
|
+
errors,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
function readKnowledgeBaseConfig(dirPath) {
|
|
627
|
+
const configPath = join(dirPath, "interf.json");
|
|
628
|
+
const present = existsSync(configPath);
|
|
629
|
+
if (!present) {
|
|
630
|
+
return {
|
|
631
|
+
present: false,
|
|
632
|
+
valid: false,
|
|
633
|
+
value: null,
|
|
634
|
+
typeMatch: () => false,
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
const value = readInterfConfig(dirPath);
|
|
638
|
+
return {
|
|
639
|
+
present: true,
|
|
640
|
+
valid: value !== null,
|
|
641
|
+
value,
|
|
642
|
+
typeMatch(expected) {
|
|
643
|
+
return value?.type === expected;
|
|
644
|
+
},
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
function validateSynthFiles(files) {
|
|
648
|
+
let invalidFrontmatter = 0;
|
|
649
|
+
let shortAbstracts = 0;
|
|
650
|
+
for (const filePath of files) {
|
|
651
|
+
const content = safeReadText(filePath);
|
|
652
|
+
if (content === null)
|
|
653
|
+
continue;
|
|
654
|
+
const parsed = parseMarkdownFrontmatter(content);
|
|
655
|
+
if (!parsed || !hasRequiredSynthFields(parsed.frontmatter)) {
|
|
656
|
+
invalidFrontmatter += 1;
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const abstractWords = countAbstractWords(parsed.frontmatter, parsed.body);
|
|
660
|
+
if (abstractWords < MIN_ABSTRACT_WORDS) {
|
|
661
|
+
shortAbstracts += 1;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return {
|
|
665
|
+
invalid_frontmatter: invalidFrontmatter,
|
|
666
|
+
short_abstracts: shortAbstracts,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function countBrokenWikilinks(knowledgeBaseRoot, noteIndexRoots, linkScanRoots) {
|
|
670
|
+
const noteIndex = new Set();
|
|
671
|
+
for (const filePath of dedupeFiles(noteIndexRoots)) {
|
|
672
|
+
noteIndex.add(noteName(filePath).toLowerCase());
|
|
673
|
+
}
|
|
674
|
+
let brokenLinks = 0;
|
|
675
|
+
for (const filePath of dedupeFiles(linkScanRoots)) {
|
|
676
|
+
const content = safeReadText(filePath);
|
|
677
|
+
if (content === null)
|
|
678
|
+
continue;
|
|
679
|
+
const matches = content.matchAll(WIKILINK_PATTERN);
|
|
680
|
+
for (const match of matches) {
|
|
681
|
+
const rawTarget = match[1]?.trim();
|
|
682
|
+
if (!rawTarget)
|
|
683
|
+
continue;
|
|
684
|
+
const target = rawTarget.split("|")[0]?.split("#")[0]?.trim();
|
|
685
|
+
if (!target)
|
|
686
|
+
continue;
|
|
687
|
+
if (target.includes("/")) {
|
|
688
|
+
const directPath = join(knowledgeBaseRoot, target);
|
|
689
|
+
const markdownPath = directPath.endsWith(".md")
|
|
690
|
+
? directPath
|
|
691
|
+
: `${directPath}.md`;
|
|
692
|
+
if (!existsSync(directPath) && !existsSync(markdownPath)) {
|
|
693
|
+
brokenLinks += 1;
|
|
694
|
+
}
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
if (!noteIndex.has(target.toLowerCase())) {
|
|
698
|
+
brokenLinks += 1;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return brokenLinks;
|
|
703
|
+
}
|
|
704
|
+
function asStringArray(value) {
|
|
705
|
+
if (!Array.isArray(value))
|
|
706
|
+
return [];
|
|
707
|
+
return value.filter((item) => typeof item === "string");
|
|
708
|
+
}
|
|
709
|
+
function asRejectedEntries(value) {
|
|
710
|
+
if (!Array.isArray(value))
|
|
711
|
+
return [];
|
|
712
|
+
return value.filter((item) => !!item &&
|
|
713
|
+
typeof item === "object" &&
|
|
714
|
+
typeof item.path === "string" &&
|
|
715
|
+
typeof item.reason === "string");
|
|
716
|
+
}
|
|
717
|
+
function asFileEntries(value) {
|
|
718
|
+
if (!Array.isArray(value))
|
|
719
|
+
return [];
|
|
720
|
+
return value.filter((item) => !!item &&
|
|
721
|
+
typeof item === "object" &&
|
|
722
|
+
typeof item.file === "string");
|
|
723
|
+
}
|
|
724
|
+
function asNumber(value) {
|
|
725
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
726
|
+
}
|
|
727
|
+
function isSubset(left, right) {
|
|
728
|
+
for (const item of left) {
|
|
729
|
+
if (!right.has(item))
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
return true;
|
|
733
|
+
}
|
|
734
|
+
function setsEqual(left, right) {
|
|
735
|
+
return left.size === right.size && isSubset(left, right);
|
|
736
|
+
}
|
|
737
|
+
function setIntersectionSize(left, right) {
|
|
738
|
+
let count = 0;
|
|
739
|
+
for (const item of left) {
|
|
740
|
+
if (right.has(item))
|
|
741
|
+
count += 1;
|
|
742
|
+
}
|
|
743
|
+
return count;
|
|
744
|
+
}
|
|
745
|
+
function dedupeStrings(values) {
|
|
746
|
+
return Array.from(new Set(values));
|
|
747
|
+
}
|
|
748
|
+
function extractSection(content, heading) {
|
|
749
|
+
const pattern = new RegExp(`(?:^|\\n)## ${escapeRegExp(heading)}\\s*\\n([\\s\\S]*?)(?=\\n## |\\s*$)`);
|
|
750
|
+
const match = content.match(pattern);
|
|
751
|
+
return match?.[1]?.trim() ?? null;
|
|
752
|
+
}
|
|
753
|
+
function sectionMatchesInterfaceContract(section, contractType) {
|
|
754
|
+
switch (contractType) {
|
|
755
|
+
case "interface-retrieval":
|
|
756
|
+
return sectionHasAnyMarker(section, [
|
|
757
|
+
"Filter criteria",
|
|
758
|
+
"Categories:",
|
|
759
|
+
"Priority evidence tiers:",
|
|
760
|
+
"Truth modes to prioritize or downweight:",
|
|
761
|
+
"Key entities:",
|
|
762
|
+
"Expected relevant file count:",
|
|
763
|
+
]);
|
|
764
|
+
case "interface-analysis":
|
|
765
|
+
return sectionHasAnyMarker(section, [
|
|
766
|
+
"Batching:",
|
|
767
|
+
"Claims:",
|
|
768
|
+
"Entities:",
|
|
769
|
+
"extract",
|
|
770
|
+
]);
|
|
771
|
+
case "interface-output":
|
|
772
|
+
return sectionHasAnyMarker(section, [
|
|
773
|
+
"home.md",
|
|
774
|
+
"Output files",
|
|
775
|
+
"briefs/",
|
|
776
|
+
"knowledge/",
|
|
777
|
+
]);
|
|
778
|
+
default:
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
function countMatches(content, pattern) {
|
|
783
|
+
return [...content.matchAll(pattern)].length;
|
|
784
|
+
}
|
|
785
|
+
function escapeRegExp(value) {
|
|
786
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
787
|
+
}
|
|
788
|
+
function dedupeFiles(dirPaths) {
|
|
789
|
+
const seen = new Set();
|
|
790
|
+
const files = [];
|
|
791
|
+
for (const dirPath of dirPaths) {
|
|
792
|
+
for (const filePath of listFilesRecursive(dirPath, isMarkdownFile)) {
|
|
793
|
+
if (seen.has(filePath))
|
|
794
|
+
continue;
|
|
795
|
+
seen.add(filePath);
|
|
796
|
+
files.push(filePath);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return files;
|
|
800
|
+
}
|
|
801
|
+
function parseMarkdownFrontmatter(content) {
|
|
802
|
+
return parseJsonFrontmatter(content);
|
|
803
|
+
}
|
|
804
|
+
function hasRequiredSynthFields(frontmatter) {
|
|
805
|
+
const hasSourceReference = "source" in frontmatter || "source_file" in frontmatter || "raw" in frontmatter;
|
|
806
|
+
return hasSourceReference && REQUIRED_DIGEST_FIELDS.every((field) => field in frontmatter);
|
|
807
|
+
}
|
|
808
|
+
function countAbstractWords(frontmatter, body) {
|
|
809
|
+
const frontmatterAbstract = typeof frontmatter.abstract === "string" ? frontmatter.abstract : null;
|
|
810
|
+
if (frontmatterAbstract && frontmatterAbstract.trim().length > 0) {
|
|
811
|
+
return countWords(frontmatterAbstract);
|
|
812
|
+
}
|
|
813
|
+
const match = body.match(/^## Abstract\s+([\s\S]*?)(?:\n## |\n# |$)/m);
|
|
814
|
+
if (!match)
|
|
815
|
+
return 0;
|
|
816
|
+
return countWords(match[1]);
|
|
817
|
+
}
|
|
818
|
+
function safeReadText(filePath) {
|
|
819
|
+
try {
|
|
820
|
+
return readFileSync(filePath, "utf-8");
|
|
821
|
+
}
|
|
822
|
+
catch {
|
|
823
|
+
return null;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
function countWords(text) {
|
|
827
|
+
return text
|
|
828
|
+
.split(/\s+/)
|
|
829
|
+
.map((word) => word.trim())
|
|
830
|
+
.filter((word) => /[A-Za-z0-9]/.test(word)).length;
|
|
831
|
+
}
|
|
832
|
+
function noteName(filePath) {
|
|
833
|
+
return basename(filePath, extname(filePath));
|
|
834
|
+
}
|
|
835
|
+
function isMarkdownFile(filePath) {
|
|
836
|
+
return extname(filePath) === ".md" && basename(filePath) !== "AGENTS.md";
|
|
837
|
+
}
|
|
838
|
+
//# sourceMappingURL=validate.js.map
|