knodin 0.6.0 → 0.7.4

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 (49) hide show
  1. package/README.md +26 -3
  2. package/dist/bin/cli.js +168 -12
  3. package/dist/bin/launcher.js +11 -0
  4. package/dist/src/agent-integration.js +3 -1
  5. package/dist/src/cli-args.js +8 -1
  6. package/dist/src/cli-model.js +35 -1
  7. package/dist/src/codeflow-replay.js +80 -0
  8. package/dist/src/competitive-cold-mcp.js +40 -0
  9. package/dist/src/competitive-manifest.js +106 -25
  10. package/dist/src/competitive-runner.js +37 -3
  11. package/dist/src/competitive-sandbox.js +1 -1
  12. package/dist/src/diagnostics.js +449 -0
  13. package/dist/src/engine/git-history.js +289 -0
  14. package/dist/src/engine/index.js +417 -70
  15. package/dist/src/engine/scip-import.js +408 -0
  16. package/dist/src/execution-profile.js +203 -0
  17. package/dist/src/failure-diagnosis.js +69 -10
  18. package/dist/src/hook-manager-integration.js +156 -0
  19. package/dist/src/init.js +319 -33
  20. package/dist/src/lifecycle-health.js +42 -4
  21. package/dist/src/output-telemetry.js +4 -0
  22. package/dist/src/progressive-evidence.js +473 -0
  23. package/dist/src/pure-compression-cli.js +101 -0
  24. package/dist/src/release-preflight.js +510 -0
  25. package/dist/src/repository-management.js +142 -0
  26. package/dist/src/response-budget.js +11 -1
  27. package/dist/src/server.js +22 -2
  28. package/dist/src/structural-fast-path.js +338 -0
  29. package/dist/src/structural-snapshot.js +33 -0
  30. package/dist/src/tools/knodin-tools.js +105 -14
  31. package/dist/src/update-ceremony.js +158 -0
  32. package/docs/CLI.md +22 -0
  33. package/docs/COMMAND-OUTPUT-COMPRESSION.md +31 -15
  34. package/docs/CONTAINED-EXECUTION.md +77 -0
  35. package/docs/DIAGNOSTICS.md +45 -0
  36. package/docs/DOCTOR-AND-UPDATES.md +5 -2
  37. package/docs/GIT-HISTORY-REVIEW.md +39 -0
  38. package/docs/MCP.md +15 -0
  39. package/docs/PROGRESSIVE-EVIDENCE.md +37 -0
  40. package/docs/REPOSITORIES-AND-WORKTREES.md +30 -0
  41. package/docs/SCIP-IMPORT.md +57 -0
  42. package/docs/SIGNED-UPDATES.md +5 -0
  43. package/docs/TELEMETRY.md +4 -0
  44. package/docs/releases/0.7.0.md +24 -0
  45. package/docs/releases/0.7.1.md +21 -0
  46. package/docs/releases/0.7.2.md +21 -0
  47. package/docs/releases/0.7.3.md +23 -0
  48. package/docs/releases/0.7.4.md +17 -0
  49. package/package.json +34 -2
@@ -11,6 +11,139 @@ const MAX_GIT_ERROR_BYTES = 16 * 1024;
11
11
  const MAX_TRACKED_PATH_BYTES = 1024 * 1024;
12
12
  const MAX_DOCUMENT_CANDIDATES = 20;
13
13
  const SKIPPED_DIRECTORIES = new Set([".git", ".knodin", "node_modules"]);
14
+ export function repositorySignalInspectionLimit(options) {
15
+ const bytes = Math.min(options.byteBudget ?? 65_536, (options.tokenBudget ?? 16_384) * 4);
16
+ return Math.max(1, Math.min(options.itemBudget ?? 100, Math.floor(bytes / 256)));
17
+ }
18
+ const SIGNAL_MARKERS = [
19
+ ".circleci/config.yml",
20
+ ".github/workflows",
21
+ ".gitlab-ci.yml",
22
+ ".mcp.json",
23
+ ".vscode/mcp.json",
24
+ "AGENTS.md",
25
+ "CLAUDE.md",
26
+ "Jenkinsfile",
27
+ "azure-pipelines.yml",
28
+ "sonar-project.properties",
29
+ ];
30
+ const HOOK_CONFIGS = [
31
+ { path: "lefthook.yml", manager: "lefthook", readPaths: ["lefthook.yml"] },
32
+ { path: "lefthook.yaml", manager: "lefthook", readPaths: ["lefthook.yaml"] },
33
+ {
34
+ path: ".husky",
35
+ manager: "husky",
36
+ readPaths: [".husky/pre-commit", ".husky/commit-msg", ".husky/pre-push"],
37
+ },
38
+ {
39
+ path: ".pre-commit-config.yaml",
40
+ manager: "pre-commit",
41
+ readPaths: [".pre-commit-config.yaml"],
42
+ },
43
+ ];
44
+ const MAX_HOOK_CONFIG_BYTES = 64 * 1024;
45
+ function parseRemote(name, raw) {
46
+ try {
47
+ const normalized = /^[^@/:]+@[^:]+:.+/.test(raw)
48
+ ? raw.replace(/^([^@/:]+)@([^:]+):/, "ssh://$1@$2/")
49
+ : raw;
50
+ const url = new URL(normalized);
51
+ const owner = url.pathname.replace(/^\//, "").split("/")[0];
52
+ if (!url.hostname || !owner)
53
+ return undefined;
54
+ return { name, host: url.hostname.toLowerCase(), owner };
55
+ }
56
+ catch {
57
+ return undefined;
58
+ }
59
+ }
60
+ /** Inspect a fixed, path-only allowlist plus bounded hook configuration. */
61
+ export async function detectRepositorySignals(repository) {
62
+ const markerFiles = [];
63
+ for (const marker of SIGNAL_MARKERS) {
64
+ try {
65
+ await fs.promises.lstat(path.join(repository, marker));
66
+ markerFiles.push(marker);
67
+ }
68
+ catch { }
69
+ }
70
+ let hookManager;
71
+ let aidevTrackReferenced;
72
+ for (const config of HOOK_CONFIGS) {
73
+ try {
74
+ await fs.promises.lstat(path.join(repository, config.path));
75
+ if (!hookManager) {
76
+ hookManager = config.manager;
77
+ aidevTrackReferenced = false;
78
+ }
79
+ let remainingBytes = MAX_HOOK_CONFIG_BYTES;
80
+ for (const readPath of config.readPaths) {
81
+ if (remainingBytes === 0 || aidevTrackReferenced)
82
+ break;
83
+ try {
84
+ const readStat = await fs.promises.lstat(path.join(repository, readPath));
85
+ if (!readStat.isFile())
86
+ continue;
87
+ const handle = await fs.promises.open(path.join(repository, readPath), "r");
88
+ try {
89
+ const buffer = Buffer.alloc(Math.min(readStat.size, remainingBytes));
90
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
91
+ remainingBytes -= bytesRead;
92
+ aidevTrackReferenced = /(?:^|[^\w-])aidev-track(?:$|[^\w-])/m.test(buffer.subarray(0, bytesRead).toString("utf8"));
93
+ }
94
+ finally {
95
+ await handle.close();
96
+ }
97
+ }
98
+ catch { }
99
+ }
100
+ break;
101
+ }
102
+ catch { }
103
+ }
104
+ const remotes = [];
105
+ try {
106
+ const lines = child_process.execFileSync(gitExecutable(), ["remote", "-v"], {
107
+ cwd: repository,
108
+ encoding: "utf8",
109
+ stdio: ["ignore", "pipe", "ignore"],
110
+ maxBuffer: MAX_GIT_ERROR_BYTES,
111
+ });
112
+ const seen = new Set();
113
+ for (const line of lines.split("\n")) {
114
+ const [name, raw] = line.trim().split(/\s+/);
115
+ if (!name || !raw || seen.has(name))
116
+ continue;
117
+ const remote = parseRemote(name, raw);
118
+ if (remote) {
119
+ seen.add(name);
120
+ remotes.push(remote);
121
+ }
122
+ }
123
+ }
124
+ catch { }
125
+ remotes.sort((a, b) => a.name.localeCompare(b.name));
126
+ const ciProviders = [
127
+ markerFiles.includes(".github/workflows") ? "github-actions" : undefined,
128
+ markerFiles.includes(".gitlab-ci.yml") ? "gitlab-ci" : undefined,
129
+ markerFiles.includes(".circleci/config.yml") ? "circleci" : undefined,
130
+ markerFiles.includes("azure-pipelines.yml") ? "azure-pipelines" : undefined,
131
+ markerFiles.includes("Jenkinsfile") ? "jenkins" : undefined,
132
+ ].filter((value) => Boolean(value));
133
+ const agentConfigs = markerFiles.filter((marker) => marker === "AGENTS.md" || marker === "CLAUDE.md");
134
+ return {
135
+ ...(hookManager ? { hookManager } : {}),
136
+ ...(markerFiles.length ? { markerFiles } : {}),
137
+ ...(remotes.length ? { remotes } : {}),
138
+ ...(ciProviders.length ? { ciProviders } : {}),
139
+ ...(agentConfigs.length ? { agentConfigs } : {}),
140
+ ...(aidevTrackReferenced === undefined ? {} : { aidevTrackReferenced }),
141
+ };
142
+ }
143
+ /** Preserve the pre-C79 byte shape unless the caller explicitly opts in. */
144
+ export function withRepositorySignals(record, enabled, signals) {
145
+ return enabled ? { ...record, signals } : record;
146
+ }
14
147
  function gitTopLevel(directory) {
15
148
  return child_process
16
149
  .execFileSync(gitExecutable(), ["rev-parse", "--show-toplevel"], {
@@ -175,6 +308,9 @@ function parseRepositoryOption(args, index, cwd, command, state) {
175
308
  case "--json":
176
309
  state.json = true;
177
310
  return index;
311
+ case "--signals":
312
+ state.signals = true;
313
+ return index;
178
314
  case "--allow-partial":
179
315
  state.allowPartial = true;
180
316
  return index;
@@ -232,6 +368,7 @@ export function parseRepositoryCommandArgs(args, cwd) {
232
368
  include: [],
233
369
  exclude: [],
234
370
  allowPartial: false,
371
+ signals: false,
235
372
  };
236
373
  let index = 1;
237
374
  while (index < args.length) {
@@ -250,17 +387,22 @@ export function parseRepositoryCommandArgs(args, cwd) {
250
387
  throw new Error(`knodin repos ${command}: --dry-run applies only to init`);
251
388
  if (state.manifestPath && command !== "init")
252
389
  throw new Error(`knodin repos ${command}: --manifest applies only to init`);
390
+ if (state.signals && command !== "discover")
391
+ throw new Error(`knodin repos ${command}: --signals applies only to discover`);
253
392
  if (command !== "search" &&
254
393
  command !== "init" &&
255
394
  (state.include.length > 0 || state.exclude.length > 0))
256
395
  throw new Error(`knodin repos ${command}: repository selectors apply only to init or search`);
257
396
  if (command !== "search" &&
397
+ !(command === "discover" && state.signals) &&
258
398
  (state.allowPartial ||
259
399
  state.itemBudget ||
260
400
  state.byteBudget ||
261
401
  state.tokenBudget ||
262
402
  state.cursor))
263
403
  throw new Error(`knodin repos ${command}: search options apply only to search`);
404
+ if (command === "discover" && state.cursor)
405
+ throw new Error("knodin repos discover: --cursor applies only to search");
264
406
  return { command, ...state };
265
407
  }
266
408
  /**
@@ -1,5 +1,6 @@
1
1
  const encoder = new TextEncoder();
2
2
  export const MIN_RESPONSE_BUDGET_BYTES = 256;
3
+ const MAX_REPORTED_TOTAL_PATHS = 128;
3
4
  function bytes(value) {
4
5
  return encoder.encode(JSON.stringify(value)).byteLength;
5
6
  }
@@ -67,8 +68,14 @@ export function applyResponseBudget(value, operation, request, defaults) {
67
68
  : { result: payload };
68
69
  const totals = {};
69
70
  let truncated = false;
70
- for (const entry of collectArrays(root)) {
71
+ const initialArrays = collectArrays(root);
72
+ const reportedTotals = [...initialArrays]
73
+ .sort((left, right) => left.path.split("/").length - right.path.split("/").length ||
74
+ left.path.localeCompare(right.path))
75
+ .slice(0, MAX_REPORTED_TOTAL_PATHS);
76
+ for (const entry of reportedTotals)
71
77
  totals[entry.path] = entry.value.length;
78
+ for (const entry of initialArrays) {
72
79
  if (entry.value.length > itemLimit) {
73
80
  entry.value.splice(itemLimit);
74
81
  truncated = true;
@@ -82,6 +89,9 @@ export function applyResponseBudget(value, operation, request, defaults) {
82
89
  estimatedTokens: 0,
83
90
  truncated: false,
84
91
  totals,
92
+ ...(initialArrays.length > reportedTotals.length
93
+ ? { omittedTotalPaths: initialArrays.length - reportedTotals.length }
94
+ : {}),
85
95
  };
86
96
  root.responseBudget = metadata;
87
97
  // Preserve collection contracts and identifiers where possible: large source,
@@ -11,12 +11,25 @@ import { fileURLToPath } from "node:url";
11
11
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
12
12
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
13
13
  import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
14
+ import { recordDiagnosticFailure } from "./diagnostics.js";
14
15
  import { getKnodinTools, handleKnodinTool } from "./tools/knodin-tools.js";
15
16
  import { KNODIN_VERSION } from "./version.js";
17
+ export function recordMcpDiagnosticFailure(args, error, defaultRepo = process.cwd()) {
18
+ const effectiveRepo = typeof args?.repoPath === "string" ? args.repoPath : defaultRepo;
19
+ const diagnostic = recordDiagnosticFailure(effectiveRepo, {
20
+ surface: "mcp",
21
+ operation: typeof args?.operation === "string" ? args.operation : "unknown",
22
+ phase: "dispatch",
23
+ error,
24
+ });
25
+ return diagnostic.recorded
26
+ ? new Error(`${error instanceof Error ? error.message : String(error)} [diagnostic ${diagnostic.correlationId}]`, { cause: error })
27
+ : error;
28
+ }
16
29
  export function createServer() {
17
30
  const server = new Server({ name: "knodin", version: KNODIN_VERSION }, {
18
31
  capabilities: { tools: {} },
19
- instructions: "Use knodin first for cold or unfamiliar codebase work: context to orient, explain for source-evidenced symbol context, query impact before meaningful edits, and review before handoff. Prefer direct reads for exact literals, non-code files, or files just edited this turn. If status reports repair-needed, repair before relying on graph evidence.",
32
+ instructions: "Use knodin first for cold or unfamiliar codebase work: context to orient, explain for source-evidenced symbol context, query impact before meaningful edits, and review before handoff. Prefer direct reads for exact literals, non-code files, or files just edited this turn. If status reports repair-needed, follow its typed repair steps: repair graph damage or init displaced lifecycle routing before relying on graph evidence.",
20
33
  });
21
34
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
22
35
  tools: getKnodinTools(),
@@ -26,7 +39,14 @@ export function createServer() {
26
39
  if (name !== "knodin") {
27
40
  throw new Error(`unknown tool: ${name}`);
28
41
  }
29
- const result = await handleKnodinTool(args);
42
+ let result;
43
+ try {
44
+ result = await handleKnodinTool(args);
45
+ }
46
+ catch (error) {
47
+ const values = args;
48
+ throw recordMcpDiagnosticFailure(values, error);
49
+ }
30
50
  return {
31
51
  content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
32
52
  };
@@ -0,0 +1,338 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { contentFingerprint, readStructuralSnapshot, } from "./structural-snapshot.js";
4
+ function exactFile(repo, file) {
5
+ try {
6
+ return (contentFingerprint(fs.readFileSync(path.resolve(repo, file.path))) === file.contentFingerprint);
7
+ }
8
+ catch {
9
+ return false;
10
+ }
11
+ }
12
+ const DIRECT_FILE_EXTENSIONS = new Set([
13
+ ".cjs",
14
+ ".cs",
15
+ ".cts",
16
+ ".go",
17
+ ".java",
18
+ ".js",
19
+ ".jsx",
20
+ ".mjs",
21
+ ".mts",
22
+ ".py",
23
+ ".rs",
24
+ ".ts",
25
+ ".tsx",
26
+ ]);
27
+ function lexicalEndLine(lines, startIndex, python) {
28
+ if (python) {
29
+ const indent = /^\s*/.exec(lines[startIndex])?.[0].length ?? 0;
30
+ let end = startIndex + 1;
31
+ for (let index = startIndex + 1; index < lines.length; index++) {
32
+ const line = lines[index];
33
+ if (line.trim() && (/^\s*/.exec(line)?.[0].length ?? 0) <= indent)
34
+ break;
35
+ end = index + 1;
36
+ }
37
+ return end;
38
+ }
39
+ let depth = 0;
40
+ let opened = false;
41
+ for (let index = startIndex; index < lines.length; index++) {
42
+ for (const character of lines[index]) {
43
+ if (character === "{") {
44
+ opened = true;
45
+ depth++;
46
+ }
47
+ else if (character === "}")
48
+ depth--;
49
+ }
50
+ if (opened && depth <= 0)
51
+ return index + 1;
52
+ if (!opened && /[;}]\s*$/.test(lines[index]))
53
+ return index + 1;
54
+ }
55
+ return startIndex + 1;
56
+ }
57
+ function genericFunctionSymbol(line) {
58
+ const opening = line.indexOf("(");
59
+ if (opening <= 0)
60
+ return null;
61
+ let depth = 0;
62
+ let closing = -1;
63
+ for (let index = opening; index < line.length; index += 1) {
64
+ if (line[index] === "(")
65
+ depth += 1;
66
+ if (line[index] !== ")")
67
+ continue;
68
+ depth -= 1;
69
+ if (depth === 0) {
70
+ closing = index;
71
+ break;
72
+ }
73
+ }
74
+ if (closing < 0)
75
+ return null;
76
+ const suffix = line.slice(closing + 1).trimStart();
77
+ if (!suffix.startsWith("{") && !suffix.startsWith("=>"))
78
+ return null;
79
+ const prefix = line.slice(0, opening).trim();
80
+ if (/[=;{}()]/.test(prefix))
81
+ return null;
82
+ const tokens = prefix.split(/\s+/);
83
+ const symbol = tokens.at(-1) ?? "";
84
+ return tokens.length >= 2 && /^[A-Za-z_$][\w$]*$/.test(symbol) ? symbol : null;
85
+ }
86
+ function firstPatternSymbol(line, patterns) {
87
+ for (const { kind, expression } of patterns) {
88
+ const match = expression.exec(line);
89
+ if (match)
90
+ return { kind, symbol: match[1] };
91
+ }
92
+ return null;
93
+ }
94
+ function directSymbols(filePath, content) {
95
+ const extension = path.extname(filePath).toLowerCase();
96
+ const python = extension === ".py";
97
+ const lines = content.split(/\r?\n/);
98
+ const symbols = [];
99
+ const patterns = python
100
+ ? [
101
+ { kind: "class", expression: /^\s*class\s+([A-Za-z_$][\w$]*)\b/ },
102
+ { kind: "function", expression: /^\s*(?:async\s+)?def\s+([A-Za-z_$][\w$]*)\s*\(/ },
103
+ ]
104
+ : [
105
+ {
106
+ kind: "class",
107
+ expression: /^\s*(?:(?:export|public|private|protected|internal|abstract|final|sealed|static)\s+)*(?:class|interface|enum|struct|trait|record|type)\s+([A-Za-z_$][\w$]*)\b/,
108
+ },
109
+ {
110
+ kind: "function",
111
+ expression: /^\s*(?:(?:export|public|private|protected|internal|async|static|final|pub(?:\([^)]*\))?)\s+)*(?:func|fn|function)\s+([A-Za-z_$][\w$]*)\s*\(/,
112
+ },
113
+ {
114
+ kind: "function",
115
+ expression: /^\s*(?:(?:export|const|let|var|public|private|protected|static|readonly)\s+)*([A-Za-z_$][\w$]*)\s*=\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>/,
116
+ },
117
+ ];
118
+ for (const [index, line] of lines.entries()) {
119
+ let found = firstPatternSymbol(line, patterns);
120
+ if (!found && !python) {
121
+ const symbol = genericFunctionSymbol(line);
122
+ if (symbol)
123
+ found = { kind: "function", symbol };
124
+ }
125
+ if (found) {
126
+ const { kind, symbol } = found;
127
+ const visibility = line.match(/\b(public|private|protected|internal)\b/)?.[1] ??
128
+ (python && symbol.startsWith("_") ? "private" : "default");
129
+ symbols.push({
130
+ identity: null,
131
+ symbol,
132
+ kind,
133
+ line: index + 1,
134
+ endLine: lexicalEndLine(lines, index, python),
135
+ signature: line
136
+ .trim()
137
+ .split(/\s*(?:\{|=>)\s*/, 1)[0]
138
+ .slice(0, 500),
139
+ visibility,
140
+ exported: /\bexport\b/.test(line) || /\bpub\b/.test(line),
141
+ evidenceQuality: "lexical-structural-fallback",
142
+ });
143
+ }
144
+ }
145
+ return symbols;
146
+ }
147
+ function directFile(repo, filePath) {
148
+ if (!DIRECT_FILE_EXTENSIONS.has(path.extname(filePath).toLowerCase()))
149
+ return null;
150
+ const root = fs.realpathSync(repo);
151
+ const absolute = path.resolve(root, filePath);
152
+ let real;
153
+ try {
154
+ real = fs.realpathSync(absolute);
155
+ }
156
+ catch {
157
+ return null;
158
+ }
159
+ const relative = path.relative(root, real);
160
+ if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
161
+ return null;
162
+ const content = fs.readFileSync(real, "utf8");
163
+ return {
164
+ path: relative.split(path.sep).join("/"),
165
+ sizeBytes: Buffer.byteLength(content),
166
+ mtimeMs: fs.statSync(real).mtimeMs,
167
+ contentFingerprint: contentFingerprint(content),
168
+ symbols: directSymbols(filePath, content),
169
+ };
170
+ }
171
+ export function runStructuralFastPath(repo, pattern, target, limit = 100) {
172
+ const snapshot = readStructuralSnapshot(repo);
173
+ if (!snapshot && pattern !== "file_summary")
174
+ return null;
175
+ let matching = (snapshot?.files ?? []).filter((file) => !target || file.path.includes(target));
176
+ if (pattern === "file_summary") {
177
+ const indexed = matching.find((file) => file.path === target);
178
+ if (!indexed || !exactFile(repo, indexed)) {
179
+ const direct = directFile(repo, target);
180
+ if (!direct)
181
+ return null;
182
+ matching = [direct];
183
+ }
184
+ }
185
+ else if (pattern === "batch_outline") {
186
+ matching = matching.flatMap((file) => {
187
+ if (exactFile(repo, file))
188
+ return [file];
189
+ const direct = directFile(repo, file.path);
190
+ return direct ? [direct] : [];
191
+ });
192
+ }
193
+ let rows = [];
194
+ let fingerprint;
195
+ let fresh = false;
196
+ if (pattern === "project_overview") {
197
+ fresh = matching.every((file) => {
198
+ try {
199
+ const stat = fs.statSync(path.resolve(repo, file.path));
200
+ return stat.size === file.sizeBytes && stat.mtimeMs === file.mtimeMs;
201
+ }
202
+ catch {
203
+ return false;
204
+ }
205
+ });
206
+ const directories = new Map();
207
+ for (const file of matching) {
208
+ const directory = file.path.includes("/") ? file.path.split("/", 1)[0] : ".";
209
+ const value = directories.get(directory) ?? { fileCount: 0, sizeBytes: 0 };
210
+ value.fileCount += 1;
211
+ value.sizeBytes += file.sizeBytes;
212
+ directories.set(directory, value);
213
+ }
214
+ rows = [...directories]
215
+ .sort(([a], [b]) => a.localeCompare(b))
216
+ .map(([symbol, value]) => ({ symbol, kind: "directory", ...value }));
217
+ fingerprint = contentFingerprint(JSON.stringify(matching.map(({ path: file, contentFingerprint: hash }) => [file, hash])));
218
+ }
219
+ else {
220
+ const files = pattern === "file_summary" ? matching.filter((file) => file.path === target) : matching;
221
+ fresh = files.length > 0 && files.every((file) => exactFile(repo, file));
222
+ fingerprint =
223
+ files.length === 1
224
+ ? files[0].contentFingerprint
225
+ : contentFingerprint(JSON.stringify(files.map((file) => [file.path, file.contentFingerprint])));
226
+ rows = files.flatMap((file) => file.symbols.map((symbol) => ({ ...symbol, file: file.path })));
227
+ }
228
+ const selected = rows.slice(0, limit);
229
+ return {
230
+ pattern,
231
+ target,
232
+ count: selected.length,
233
+ results: selected,
234
+ ...(rows.length > selected.length ? { hasMore: true } : {}),
235
+ staleness: fresh ? "fresh" : "unknown",
236
+ graphEnriched: false,
237
+ evidence: {
238
+ ...(pattern === "file_summary" ? { path: target } : {}),
239
+ contentFingerprint: fingerprint,
240
+ evidenceQuality: matching.some((file) => file.symbols.some((symbol) => symbol.evidenceQuality === "lexical-structural-fallback"))
241
+ ? "direct-lexical-parser"
242
+ : "indexed-structural-snapshot",
243
+ uniqueness: pattern === "project_overview" ? "not-applicable" : "file-scoped-only",
244
+ evidenceGeneratedAt: snapshot?.generatedAt ?? new Date().toISOString(),
245
+ },
246
+ upgrade: pattern === "file_summary"
247
+ ? { operation: "explain", target }
248
+ : pattern === "project_overview"
249
+ ? { operation: "query", pattern: "architecture_overview", target: "" }
250
+ : { operation: "query", pattern: "impact", target },
251
+ };
252
+ }
253
+ export function runExactSymbolFastPath(repo, symbol, filePath) {
254
+ const snapshot = readStructuralSnapshot(repo);
255
+ const indexed = snapshot?.files.find((candidate) => candidate.path === filePath);
256
+ const file = indexed && exactFile(repo, indexed) ? indexed : directFile(repo, filePath);
257
+ if (!file)
258
+ return null;
259
+ const matches = file.symbols.filter((candidate) => candidate.symbol === symbol);
260
+ if (matches.length === 0)
261
+ return null;
262
+ const fresh = exactFile(repo, file);
263
+ const evidence = {
264
+ path: filePath,
265
+ contentFingerprint: file.contentFingerprint,
266
+ evidenceQuality: file.symbols.some((candidate) => candidate.evidenceQuality === "lexical-structural-fallback")
267
+ ? "direct-lexical-parser"
268
+ : "indexed-structural-snapshot",
269
+ uniqueness: "file-scoped-only",
270
+ evidenceGeneratedAt: snapshot?.generatedAt ?? new Date().toISOString(),
271
+ };
272
+ if (matches.length > 1) {
273
+ return {
274
+ symbol,
275
+ file: filePath,
276
+ ambiguity: {
277
+ candidates: matches.map((match) => ({
278
+ identity: match.identity ?? "",
279
+ file: filePath,
280
+ kind: match.kind,
281
+ line: match.line,
282
+ })),
283
+ },
284
+ staleness: fresh ? "fresh" : "unknown",
285
+ graphEnriched: false,
286
+ evidence,
287
+ upgrade: { operation: "explain", target: symbol },
288
+ };
289
+ }
290
+ const match = matches[0];
291
+ const lines = fs.readFileSync(path.resolve(repo, filePath), "utf8").split(/\r?\n/);
292
+ const source = fresh
293
+ ? lines
294
+ .slice(match.line - 1, match.endLine)
295
+ .map((line, offset) => `${match.line + offset}: ${line}`)
296
+ .join("\n")
297
+ : undefined;
298
+ return {
299
+ symbol,
300
+ ...(match.identity ? { identity: match.identity } : {}),
301
+ file: filePath,
302
+ ...(source ? { source } : {}),
303
+ staleness: fresh ? "fresh" : "unknown",
304
+ graphEnriched: false,
305
+ evidence,
306
+ upgrade: { operation: "explain", target: symbol },
307
+ };
308
+ }
309
+ export function tryStructuralFastPath(argv, cwd = process.cwd()) {
310
+ const valueAfter = (flag) => {
311
+ const index = argv.indexOf(flag);
312
+ return index >= 0
313
+ ? argv[index + 1]
314
+ : argv.find((value) => value.startsWith(`${flag}=`))?.slice(flag.length + 1);
315
+ };
316
+ if (argv[0] === "explain" && argv[1] && valueAfter("--file")) {
317
+ const repo = path.resolve(valueAfter("--repo") ?? cwd);
318
+ const result = runExactSymbolFastPath(repo, argv[1], valueAfter("--file"));
319
+ if (!result)
320
+ return false;
321
+ process.stdout.write(`${JSON.stringify(result)}\n`);
322
+ return true;
323
+ }
324
+ if (argv[0] !== "query" ||
325
+ !["file_summary", "batch_outline", "project_overview"].includes(argv[1] ?? ""))
326
+ return false;
327
+ const pattern = argv[1];
328
+ const repo = path.resolve(valueAfter("--repo") ?? cwd);
329
+ const target = pattern === "project_overview" ? "" : (argv[2] ?? "");
330
+ const limit = Number(valueAfter("--limit") ?? 100);
331
+ if (!target && pattern !== "project_overview")
332
+ return false;
333
+ const result = runStructuralFastPath(repo, pattern, target, Number.isInteger(limit) && limit > 0 ? limit : 100);
334
+ if (!result)
335
+ return false;
336
+ process.stdout.write(`${JSON.stringify(result)}\n`);
337
+ return true;
338
+ }
@@ -0,0 +1,33 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ export const STRUCTURAL_SNAPSHOT_VERSION = 1;
5
+ export function contentFingerprint(content) {
6
+ return `sha256:${crypto.createHash("sha256").update(content).digest("hex")}`;
7
+ }
8
+ export function structuralSnapshotPath(repo) {
9
+ return path.join(path.resolve(repo), ".knodin", "structural-v1.json");
10
+ }
11
+ export function writeStructuralSnapshot(repo, files) {
12
+ const destination = structuralSnapshotPath(repo);
13
+ fs.mkdirSync(path.dirname(destination), { recursive: true });
14
+ const snapshot = {
15
+ schemaVersion: STRUCTURAL_SNAPSHOT_VERSION,
16
+ generatedAt: new Date().toISOString(),
17
+ files: [...files].sort((left, right) => left.path.localeCompare(right.path)),
18
+ };
19
+ const temporary = `${destination}.${process.pid}.tmp`;
20
+ fs.writeFileSync(temporary, `${JSON.stringify(snapshot)}\n`, { mode: 0o600 });
21
+ fs.renameSync(temporary, destination);
22
+ }
23
+ export function readStructuralSnapshot(repo) {
24
+ try {
25
+ const parsed = JSON.parse(fs.readFileSync(structuralSnapshotPath(repo), "utf8"));
26
+ return parsed?.schemaVersion === STRUCTURAL_SNAPSHOT_VERSION && Array.isArray(parsed.files)
27
+ ? parsed
28
+ : null;
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ }