knodin 0.5.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.
Files changed (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +590 -0
  3. package/dist/bin/cli.js +1704 -0
  4. package/dist/src/agent-integration.js +250 -0
  5. package/dist/src/artifact-refresh.js +81 -0
  6. package/dist/src/cli-args.js +267 -0
  7. package/dist/src/cli-model.js +324 -0
  8. package/dist/src/compact-structural.js +96 -0
  9. package/dist/src/competitive-constraints.js +20 -0
  10. package/dist/src/competitive-manifest.js +330 -0
  11. package/dist/src/competitive-measurement.js +183 -0
  12. package/dist/src/competitive-runner.js +453 -0
  13. package/dist/src/competitive-sandbox.js +108 -0
  14. package/dist/src/context-export.js +422 -0
  15. package/dist/src/context.js +102 -0
  16. package/dist/src/docs-sections.js +141 -0
  17. package/dist/src/doctor.js +380 -0
  18. package/dist/src/engine/ann-hnsw.js +271 -0
  19. package/dist/src/engine/embeddings.js +193 -0
  20. package/dist/src/engine/file-walker.js +43 -0
  21. package/dist/src/engine/index.js +13030 -0
  22. package/dist/src/engine/perf.js +115 -0
  23. package/dist/src/engine/prune.js +112 -0
  24. package/dist/src/engine/source-policy.js +69 -0
  25. package/dist/src/engine/sqlite.js +71 -0
  26. package/dist/src/engine/symbol-delete.js +58 -0
  27. package/dist/src/failure-diagnosis.js +590 -0
  28. package/dist/src/fleet.js +7 -0
  29. package/dist/src/git-executable.js +31 -0
  30. package/dist/src/graph-query-health.js +115 -0
  31. package/dist/src/index-activity.js +125 -0
  32. package/dist/src/init-progress-worker.js +107 -0
  33. package/dist/src/init-progress.js +155 -0
  34. package/dist/src/init.js +985 -0
  35. package/dist/src/lifecycle-health.js +213 -0
  36. package/dist/src/lsp-readonly.js +217 -0
  37. package/dist/src/output-compression.js +629 -0
  38. package/dist/src/output-telemetry.js +359 -0
  39. package/dist/src/pr-triage.js +638 -0
  40. package/dist/src/relationship-adapters.js +370 -0
  41. package/dist/src/release-attestation.js +533 -0
  42. package/dist/src/repair-progress-worker.js +121 -0
  43. package/dist/src/repair-progress.js +262 -0
  44. package/dist/src/repository-init-process.js +173 -0
  45. package/dist/src/repository-management.js +1089 -0
  46. package/dist/src/response-budget.js +184 -0
  47. package/dist/src/server.js +53 -0
  48. package/dist/src/system-config.js +615 -0
  49. package/dist/src/terminal-help.js +83 -0
  50. package/dist/src/tools/knodin-tools.js +1438 -0
  51. package/dist/src/tools/reckon-tools.js +5 -0
  52. package/dist/src/update-policy.js +944 -0
  53. package/dist/src/update-trust.js +503 -0
  54. package/dist/src/version.js +13 -0
  55. package/dist/src/visualization.js +162 -0
  56. package/dist/src/wait-for-fresh.js +98 -0
  57. package/dist/src/worktree-lifecycle.js +231 -0
  58. package/docs/CLI.md +39 -0
  59. package/docs/COMMAND-OUTPUT-COMPRESSION.md +194 -0
  60. package/docs/DEAD-CODE-AND-IMPACT.md +27 -0
  61. package/docs/DOCTOR-AND-UPDATES.md +84 -0
  62. package/docs/INDEXING-POLICY-AND-PROVENANCE.md +37 -0
  63. package/docs/INSTALLATION.md +208 -0
  64. package/docs/MCP.md +100 -0
  65. package/docs/PT-ACCESS-RECOMMENDATION.md +91 -0
  66. package/docs/RELEASE-0.3-EVIDENCE.md +73 -0
  67. package/docs/REPOSITORIES-AND-WORKTREES.md +81 -0
  68. package/docs/SIGNED-UPDATES.md +146 -0
  69. package/docs/SYSTEMS-AND-RELATIONSHIPS.md +45 -0
  70. package/docs/TELEMETRY.md +42 -0
  71. package/docs/releases/0.3.0.md +46 -0
  72. package/docs/releases/0.4.0.md +68 -0
  73. package/docs/releases/0.4.1.md +28 -0
  74. package/docs/releases/0.4.2.md +27 -0
  75. package/docs/releases/0.4.3.md +23 -0
  76. package/docs/releases/0.5.0.md +29 -0
  77. package/package.json +110 -0
  78. package/schemas/release-attestation-v1.schema.json +210 -0
  79. package/tree-sitter-prisma.wasm +0 -0
  80. package/tree-sitter-sql.wasm +0 -0
  81. package/tree-sitter-xml.wasm +0 -0
@@ -0,0 +1,370 @@
1
+ import childProcess from "node:child_process";
2
+ import crypto from "node:crypto";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { gitExecutable } from "./git-executable.js";
6
+ const MAX_FILES = 5_000;
7
+ const MAX_CONTRACT_BYTES = 2 * 1024 * 1024;
8
+ export function trackedRepositoryFiles(repository) {
9
+ try {
10
+ return childProcess
11
+ .execFileSync(gitExecutable(), ["ls-files", "-z"], {
12
+ cwd: repository,
13
+ encoding: "utf-8",
14
+ stdio: ["ignore", "pipe", "ignore"],
15
+ })
16
+ .split("\0")
17
+ .filter(Boolean)
18
+ .sort((left, right) => left.localeCompare(right))
19
+ .slice(0, MAX_FILES);
20
+ }
21
+ catch {
22
+ return [];
23
+ }
24
+ }
25
+ function lineNumber(content, offset) {
26
+ return content.slice(0, offset).split("\n").length;
27
+ }
28
+ function matchAll(content, pattern, build) {
29
+ const findings = [];
30
+ for (const match of content.matchAll(pattern)) {
31
+ findings.push({ ...build(match), line: lineNumber(content, match.index ?? 0) });
32
+ }
33
+ return findings;
34
+ }
35
+ function packageFindings(file, content) {
36
+ if (file === "package.json") {
37
+ try {
38
+ const manifest = JSON.parse(content);
39
+ const findings = [];
40
+ for (const section of ["dependencies", "devDependencies", "peerDependencies"]) {
41
+ const dependencies = manifest[section];
42
+ if (typeof dependencies !== "object" || dependencies === null)
43
+ continue;
44
+ for (const name of Object.keys(dependencies).sort((left, right) => left.localeCompare(right))) {
45
+ findings.push({
46
+ type: "depends_on",
47
+ target: `pkg:npm/${encodeURIComponent(name)}`,
48
+ line: lineNumber(content, Math.max(content.indexOf(`"${name}"`), 0)),
49
+ adapter: "package-manifest",
50
+ confidence: 1,
51
+ });
52
+ }
53
+ }
54
+ return findings;
55
+ }
56
+ catch {
57
+ return [];
58
+ }
59
+ }
60
+ if (file === "go.mod") {
61
+ return content.split(/\r?\n/).flatMap((line, index) => {
62
+ const [moduleName, version] = line.trim().split(/\s+/, 2);
63
+ if (!moduleName?.includes(".") || !version?.startsWith("v"))
64
+ return [];
65
+ return [
66
+ {
67
+ type: "depends_on",
68
+ target: `pkg:golang/${moduleName}`,
69
+ line: index + 1,
70
+ adapter: "package-manifest",
71
+ confidence: 1,
72
+ },
73
+ ];
74
+ });
75
+ }
76
+ if (file === "Cargo.toml" || file === "pyproject.toml") {
77
+ return matchAll(content, /^([A-Za-z0-9_.-]+)[ \t]*=[ \t]*(?:["'{])/gm, (match) => ({
78
+ type: "depends_on",
79
+ target: `pkg:${file === "Cargo.toml" ? "cargo" : "pypi"}/${match[1]}`,
80
+ adapter: "package-manifest",
81
+ confidence: 0.9,
82
+ }));
83
+ }
84
+ return [];
85
+ }
86
+ function contractFindings(file, content) {
87
+ const lower = file.toLowerCase();
88
+ if (/openapi|swagger/.test(lower) && /\.(json|ya?ml)$/.test(lower)) {
89
+ return matchAll(content, /^[ \t]{0,8}(\/[^:\s]+)[ \t]*:/gm, (match) => ({
90
+ type: "provides_api",
91
+ target: `openapi:${match[1]}`,
92
+ adapter: "openapi",
93
+ confidence: 1,
94
+ }));
95
+ }
96
+ if (/asyncapi/.test(lower)) {
97
+ return matchAll(content, /^[ \t]{2,8}([^:\s]+)[ \t]*:/gm, (match) => ({
98
+ type: "publishes_event",
99
+ target: `asyncapi:${match[1]}`,
100
+ adapter: "asyncapi",
101
+ confidence: 0.9,
102
+ }));
103
+ }
104
+ if (lower.endsWith(".graphql") || lower.endsWith(".gql")) {
105
+ return matchAll(content, /^[ \t]*(?:type|interface|input)[ \t]+([A-Za-z_]\w*)/gm, (match) => ({
106
+ type: "provides_api",
107
+ target: `graphql:${match[1]}`,
108
+ adapter: "graphql",
109
+ confidence: 1,
110
+ }));
111
+ }
112
+ if (lower.endsWith(".proto")) {
113
+ return matchAll(content, /^[ \t]*service[ \t]+([A-Za-z_]\w*)/gm, (match) => ({
114
+ type: "provides_api",
115
+ target: `protobuf:${match[1]}`,
116
+ adapter: "protobuf",
117
+ confidence: 1,
118
+ }));
119
+ }
120
+ return [];
121
+ }
122
+ function infrastructureFindings(file, content) {
123
+ const lower = file.toLowerCase();
124
+ const findings = [];
125
+ if (lower.endsWith(".tf")) {
126
+ findings.push(...matchAll(content, /^[ \t]*source[ \t]*=[ \t]*"([^"\r\n]+)"/gm, (match) => ({
127
+ type: "depends_on",
128
+ target: `terraform:${match[1]}`,
129
+ adapter: "terraform",
130
+ confidence: 1,
131
+ })), ...matchAll(content, /^[ \t]*backend[ \t]+"([^"\r\n]+)"/gm, (match) => ({
132
+ type: "reads_output",
133
+ target: `terraform-state:${match[1]}`,
134
+ adapter: "terraform",
135
+ confidence: 0.9,
136
+ })), ...matchAll(content, /^[ \t]*output[ \t]+"([^"\r\n]+)"/gm, (match) => ({
137
+ type: "provisions",
138
+ target: `terraform-output:${match[1]}`,
139
+ adapter: "terraform",
140
+ confidence: 1,
141
+ })));
142
+ }
143
+ if (/\.(ya?ml|tpl)$/.test(lower)) {
144
+ findings.push(...matchAll(content, /^[ \t]*image:[ \t]*["']?([^"'\s]+)["']?/gm, (match) => ({
145
+ type: "uses_image",
146
+ target: `oci:${match[1]}`,
147
+ adapter: lower.includes("helm") || lower.includes("chart") ? "helm" : "kubernetes",
148
+ confidence: 1,
149
+ })));
150
+ }
151
+ return findings;
152
+ }
153
+ function provenanceFindings(file, content) {
154
+ const lower = file.toLowerCase();
155
+ if (lower.endsWith(".map")) {
156
+ try {
157
+ const sourceMap = JSON.parse(content);
158
+ if (!Array.isArray(sourceMap.sources))
159
+ return [];
160
+ return sourceMap.sources.flatMap((source) => typeof source === "string"
161
+ ? [
162
+ {
163
+ type: "generated_from",
164
+ target: `file:${source}`,
165
+ line: 1,
166
+ adapter: "source-map",
167
+ confidence: 1,
168
+ },
169
+ ]
170
+ : []);
171
+ }
172
+ catch {
173
+ return [];
174
+ }
175
+ }
176
+ if (/(?:^|\/)(?:bom|sbom)(?:\.[^/]*)?\.json$/.test(lower)) {
177
+ try {
178
+ const sbom = JSON.parse(content);
179
+ if (!Array.isArray(sbom.components))
180
+ return [];
181
+ return sbom.components.flatMap((component) => {
182
+ if (!component || typeof component !== "object")
183
+ return [];
184
+ const purl = component.purl;
185
+ return typeof purl === "string"
186
+ ? [
187
+ {
188
+ type: "depends_on",
189
+ target: purl,
190
+ line: lineNumber(content, Math.max(content.indexOf(purl), 0)),
191
+ adapter: "cyclonedx-sbom",
192
+ confidence: 1,
193
+ },
194
+ ]
195
+ : [];
196
+ });
197
+ }
198
+ catch {
199
+ return [];
200
+ }
201
+ }
202
+ if (lower.endsWith(".intoto.jsonl") || lower.includes("provenance")) {
203
+ return matchAll(content, /"uri"[ \t]*:[ \t]*"([^"\r\n]+)"/g, (match) => ({
204
+ type: "generated_from",
205
+ target: match[1] ?? "unknown",
206
+ adapter: "slsa-provenance",
207
+ confidence: 1,
208
+ }));
209
+ }
210
+ return [];
211
+ }
212
+ function sourceFindings(content, repository, knownPaths) {
213
+ const findings = [];
214
+ for (const [knownPath, identity] of knownPaths) {
215
+ for (const match of content.matchAll(/(["'`])([^"'`\n]+)\1/g)) {
216
+ const literal = match[2];
217
+ if (!literal || path.resolve(repository, literal) !== path.resolve(knownPath))
218
+ continue;
219
+ findings.push({
220
+ type: "references_path",
221
+ target: identity,
222
+ line: lineNumber(content, match.index ?? 0),
223
+ adapter: "exact-path-literal",
224
+ confidence: 1,
225
+ });
226
+ }
227
+ }
228
+ findings.push(...matchAll(content, /\b(?:process\.env\.|os\.environ\[["'])([A-Z][A-Z0-9_]{2,})/g, (match) => ({
229
+ type: "reads_config",
230
+ target: `config:${match[1]}`,
231
+ adapter: "configuration-key",
232
+ confidence: 0.85,
233
+ })));
234
+ return findings;
235
+ }
236
+ /** Extract bounded, source-evidenced non-symbol relationships from one checkout. */
237
+ export function extractRepositoryRelationships(repositoryId, repository, knownPaths = new Map()) {
238
+ const relationships = [];
239
+ for (const file of trackedRepositoryFiles(repository)) {
240
+ const absolute = path.join(repository, file);
241
+ let stat;
242
+ try {
243
+ stat = fs.statSync(absolute);
244
+ }
245
+ catch {
246
+ continue;
247
+ }
248
+ if (!stat.isFile() || stat.size > MAX_CONTRACT_BYTES)
249
+ continue;
250
+ let content;
251
+ try {
252
+ content = fs.readFileSync(absolute, "utf-8");
253
+ }
254
+ catch {
255
+ continue;
256
+ }
257
+ const findings = [
258
+ ...packageFindings(file, content),
259
+ ...contractFindings(file, content),
260
+ ...infrastructureFindings(file, content),
261
+ ...provenanceFindings(file, content),
262
+ ...sourceFindings(content, repository, knownPaths),
263
+ ];
264
+ for (const finding of findings) {
265
+ relationships.push({
266
+ type: finding.type,
267
+ from: repositoryId,
268
+ to: finding.target,
269
+ targetIdentity: finding.target,
270
+ evidence: {
271
+ location: `${file}:${finding.line}`,
272
+ line: finding.line,
273
+ adapter: finding.adapter,
274
+ kind: "extracted",
275
+ confidence: finding.confidence,
276
+ freshness: "current",
277
+ indexGeneration: null,
278
+ },
279
+ });
280
+ }
281
+ }
282
+ return relationships.sort((left, right) => `${left.from}\0${left.type}\0${left.to}\0${left.evidence.location}`.localeCompare(`${right.from}\0${right.type}\0${right.to}\0${right.evidence.location}`));
283
+ }
284
+ /** Stream a file hash so duplicate detection never buffers an entire artifact. */
285
+ export async function hashFileStream(file) {
286
+ const digest = crypto.createHash("sha256");
287
+ await new Promise((resolve, reject) => {
288
+ const stream = fs.createReadStream(file);
289
+ stream.on("data", (chunk) => digest.update(chunk));
290
+ stream.on("error", reject);
291
+ stream.on("end", resolve);
292
+ });
293
+ return digest.digest("hex");
294
+ }
295
+ function filesGroupedBySize(repositories) {
296
+ const bySize = new Map();
297
+ const orderedRepositories = [...repositories].sort((left, right) => left.id.localeCompare(right.id));
298
+ for (const repository of orderedRepositories) {
299
+ for (const file of trackedRepositoryFiles(repository.path)) {
300
+ const absolute = path.join(repository.path, file);
301
+ try {
302
+ const stat = fs.statSync(absolute);
303
+ if (!stat.isFile())
304
+ continue;
305
+ const candidates = bySize.get(stat.size) ?? [];
306
+ candidates.push({ repositoryId: repository.id, file, absolute });
307
+ bySize.set(stat.size, candidates);
308
+ }
309
+ catch {
310
+ // A concurrently removed file is absent from this evidence snapshot.
311
+ }
312
+ }
313
+ }
314
+ return bySize;
315
+ }
316
+ async function filesGroupedByHash(candidates) {
317
+ const byHash = new Map();
318
+ for (const candidate of candidates) {
319
+ const hash = await hashFileStream(candidate.absolute);
320
+ const matches = byHash.get(hash) ?? [];
321
+ matches.push(candidate);
322
+ byHash.set(hash, matches);
323
+ }
324
+ return byHash;
325
+ }
326
+ function duplicateRelationships(matches) {
327
+ const repositoryIds = new Set(matches.map(({ repositoryId }) => repositoryId));
328
+ if (repositoryIds.size < 2)
329
+ return [];
330
+ const ordered = [...matches].sort((left, right) => `${left.repositoryId}\0${left.file}`.localeCompare(`${right.repositoryId}\0${right.file}`));
331
+ const canonical = ordered[0];
332
+ if (!canonical)
333
+ return [];
334
+ return ordered.slice(1).flatMap((duplicate) => {
335
+ if (duplicate.repositoryId === canonical.repositoryId)
336
+ return [];
337
+ return [
338
+ {
339
+ type: "duplicate_candidate",
340
+ from: duplicate.repositoryId,
341
+ to: canonical.repositoryId,
342
+ targetIdentity: canonical.repositoryId,
343
+ evidence: {
344
+ location: duplicate.file,
345
+ adapter: "streaming-sha256",
346
+ kind: "inferred",
347
+ confidence: 1,
348
+ freshness: "current",
349
+ indexGeneration: null,
350
+ },
351
+ },
352
+ ];
353
+ });
354
+ }
355
+ /**
356
+ * Detect exact tracked-file matches across repositories. Equality is reported
357
+ * only as a duplicate candidate; it never asserts generated/vendor lineage.
358
+ */
359
+ export async function detectDuplicateCandidates(repositories) {
360
+ const relationships = [];
361
+ for (const candidates of filesGroupedBySize(repositories).values()) {
362
+ if (candidates.length < 2)
363
+ continue;
364
+ const byHash = await filesGroupedByHash(candidates);
365
+ for (const matches of byHash.values()) {
366
+ relationships.push(...duplicateRelationships(matches));
367
+ }
368
+ }
369
+ return relationships.sort((left, right) => `${left.from}\0${left.to}\0${left.evidence.location}`.localeCompare(`${right.from}\0${right.to}\0${right.evidence.location}`));
370
+ }