Fix discover, improve LLM heuristics, add integration tests

- discover.ts: Fix ignore package path handling for root '.' directories,
  add .pi-map.md to default ignore list
- llm-extract.ts: Fix export regex to only match line-start exports and
  handle 'export async function', fix root package role to 'Project root'
- init.ts: Minor cleanup
- .gitignore: Add .pi-map.md
- tests: 6 integration tests covering init, patch (small/large packages),
  validate (missing, orphaned, stale-signature detection)

All 14 tests pass. TypeScript compiles clean.
This commit is contained in:
2026-06-09 19:14:41 +02:00
parent d311c8fb3c
commit 3dbb3cb7b2
9 changed files with 733 additions and 34 deletions
+32 -7
View File
@@ -69,7 +69,11 @@ function extractExportsFromTree(tree: Tree, langName: string): string[] {
const root = tree.rootNode;
function visit(node: SyntaxNode) {
if (langName === "typescript" || langName === "tsx" || langName === "javascript") {
if (
langName === "typescript" ||
langName === "tsx" ||
langName === "javascript"
) {
if (node.type === "export_statement") {
// export function foo
// export class Foo
@@ -97,7 +101,10 @@ function extractExportsFromTree(tree: Tree, langName: string): string[] {
}
}
} else if (langName === "python") {
if (node.type === "function_definition" || node.type === "class_definition") {
if (
node.type === "function_definition" ||
node.type === "class_definition"
) {
const nameNode = node.childForFieldName?.("name");
if (nameNode) exports.push(nameNode.text);
}
@@ -114,7 +121,11 @@ function extractExportsFromTree(tree: Tree, langName: string): string[] {
}
}
} else if (langName === "rust") {
if (node.type === "function_item" || node.type === "struct_item" || node.type === "enum_item") {
if (
node.type === "function_item" ||
node.type === "struct_item" ||
node.type === "enum_item"
) {
const nameNode = node.childForFieldName?.("name");
if (nameNode) exports.push(nameNode.text);
}
@@ -135,8 +146,15 @@ function extractDepsFromTree(tree: Tree, langName: string): string[] {
const root = tree.rootNode;
function visit(node: SyntaxNode) {
if (langName === "typescript" || langName === "tsx" || langName === "javascript") {
if (node.type === "import_statement" || node.type === "import_declaration") {
if (
langName === "typescript" ||
langName === "tsx" ||
langName === "javascript"
) {
if (
node.type === "import_statement" ||
node.type === "import_declaration"
) {
const source = node.childForFieldName?.("source");
if (source) {
const text = source.text;
@@ -158,7 +176,10 @@ function extractDepsFromTree(tree: Tree, langName: string): string[] {
}
}
} else if (langName === "python") {
if (node.type === "import_statement" || node.type === "import_from_statement") {
if (
node.type === "import_statement" ||
node.type === "import_from_statement"
) {
const nameNode = node.childForFieldName?.("name");
if (nameNode) deps.push(nameNode.text);
}
@@ -185,7 +206,11 @@ function extractDepsFromTree(tree: Tree, langName: string): string[] {
}
function findIdentifier(node: SyntaxNode): SyntaxNode | null {
if (node.type === "identifier" || node.type === "type_identifier" || node.type === "property_identifier") {
if (
node.type === "identifier" ||
node.type === "type_identifier" ||
node.type === "property_identifier"
) {
return node;
}
for (let i = 0; i < node.childCount; i++) {
+4 -2
View File
@@ -13,6 +13,7 @@ const DEFAULT_IGNORE = [
"__pycache__",
".DS_Store",
"*.log",
".pi-map.md",
];
export interface DirectoryEntry {
@@ -35,14 +36,15 @@ export function discoverProject(rootPath: string): DirectoryEntry[] {
function walk(dir: string) {
const relDir = relative(rootPath, dir) || ".";
if (ig.ignores(relDir)) return;
// The ignore package doesn't accept "." — skip check for root
if (relDir !== "." && ig.ignores(relDir.replace(/^\.\//, ""))) return;
const items = readdirSync(dir);
const files: string[] = [];
const subdirs: string[] = [];
for (const item of items) {
const relPath = join(relDir, item);
const relPath = join(relDir, item).replace(/^\.\//, "");
if (ig.ignores(relPath)) continue;
const fullPath = join(dir, item);
+5 -1
View File
@@ -1,5 +1,9 @@
import { discoverProject, type DirectoryEntry } from "./discover.js";
import { renderPackageMap, type PackageMapData, type FileEntry } from "./format.js";
import {
renderPackageMap,
type PackageMapData,
type FileEntry,
} from "./format.js";
import { extractFileLLM, extractPackageLLM } from "./llm-extract.js";
import { extractFileAST } from "./ast-extract.js";
import { mergeFileData } from "./merge.js";
+45 -14
View File
@@ -41,7 +41,7 @@ const FILE_TYPE_PURPOSES: Record<string, string> = {
".ini": "Configuration",
".env": "Environment config",
".dockerfile": "Docker image definition",
"dockerfile": "Docker image definition",
dockerfile: "Docker image definition",
".sql": "Database schema/queries",
".css": "Stylesheet",
".scss": "SCSS stylesheet",
@@ -77,13 +77,19 @@ export async function extractFileLLM(filePath: string): Promise<LLMFileData> {
return result;
}
function extractExports(content: string, ext: string, _filename: string): string[] {
function extractExports(
content: string,
ext: string,
_filename: string,
): string[] {
const exports: string[] = [];
if ([".ts", ".tsx", ".js", ".jsx", ".mjs"].includes(ext)) {
// ES module exports
// ES module exports — only match at start of line (after optional whitespace)
// Handles: export function foo, export async function foo, export class Foo,
// export const foo, export { foo, bar }, export default foo
const exportRegex =
/export\s+(?:default\s+)?(?:function\s+|class\s+|const\s+|let\s+|var\s+|interface\s+|type\s+|enum\s+)?([A-Za-z_$][A-Za-z0-9_$]*)/g;
/(?:^|\n)\s*export\s+(?:default\s+)?(?:async\s+)?(?:function\s+|class\s+|const\s+|let\s+|var\s+|interface\s+|type\s+|enum\s+)?([A-Za-z_$][A-Za-z0-9_$]*)/g;
let match: RegExpExecArray | null;
match = exportRegex.exec(content);
while (match !== null) {
@@ -92,16 +98,22 @@ function extractExports(content: string, ext: string, _filename: string): string
}
// Named export destructuring: export { foo, bar }
const namedExportRegex = /export\s*\{\s*([^}]+)\s*\}/g;
const namedExportRegex = /(?:^|\n)\s*export\s*\{\s*([^}]+)\s*\}/g;
match = namedExportRegex.exec(content);
while (match !== null) {
const names = match[1].split(",").map((s) => s.trim().split(/\s+as\s+/)[0].trim());
const names = match[1].split(",").map((s) =>
s
.trim()
.split(/\s+as\s+/)[0]
.trim(),
);
exports.push(...names);
match = namedExportRegex.exec(content);
}
} else if (ext === ".py") {
// Python exports (top-level functions/classes)
const pyRegex = /^(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)|class\s+([A-Za-z_][A-Za-z0-9_]*)/gm;
const pyRegex =
/^(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)|class\s+([A-Za-z_][A-Za-z0-9_]*)/gm;
let match: RegExpExecArray | null = pyRegex.exec(content);
while (match !== null) {
exports.push(match[1] || match[2]);
@@ -117,7 +129,8 @@ function extractExports(content: string, ext: string, _filename: string): string
}
} else if (ext === ".rs") {
// Rust exports (pub items)
const rsRegex = /pub\s+(?:fn|struct|enum|trait|type|const|static|use)\s+([A-Za-z_][A-Za-z0-9_]*)/g;
const rsRegex =
/pub\s+(?:fn|struct|enum|trait|type|const|static|use)\s+([A-Za-z_][A-Za-z0-9_]*)/g;
let match: RegExpExecArray | null = rsRegex.exec(content);
while (match !== null) {
exports.push(match[1]);
@@ -199,7 +212,12 @@ function generatePurpose(
if (/service/.test(name)) return "Service layer";
if (/model/.test(name)) return "Data model";
if (/schema/.test(name)) return "Data schema";
if (/component/.test(name) || /\.tsx$/.test(name) || /\.vue$/.test(name) || /\.svelte$/.test(name)) {
if (
/component/.test(name) ||
/\.tsx$/.test(name) ||
/\.vue$/.test(name) ||
/\.svelte$/.test(name)
) {
return "UI component";
}
if (/hook|use[A-Z]/.test(name)) return "React hook";
@@ -216,7 +234,10 @@ function generatePurpose(
}
// Fallback to file type
return FILE_TYPE_PURPOSES[ext] || (ext ? `${ext.slice(1).toUpperCase()} file` : `${name} file`);
return (
FILE_TYPE_PURPOSES[ext] ||
(ext ? `${ext.slice(1).toUpperCase()} file` : `${name} file`)
);
}
export async function extractPackageLLM(
@@ -226,7 +247,7 @@ export async function extractPackageLLM(
const dirName = basename(relativePath);
// Infer role from directory name
let role = `Package ${dirName}`;
let role = dirName === "." ? "Project root" : `Package ${dirName}`;
if (dirName === "src" || dirName === "lib" || dirName === "source") {
role = "Source code";
} else if (dirName === "test" || dirName === "tests" || dirName === "spec") {
@@ -235,7 +256,11 @@ export async function extractPackageLLM(
role = "Documentation";
} else if (dirName === "config" || dirName === "configuration") {
role = "Configuration";
} else if (dirName === "utils" || dirName === "helpers" || dirName === "util") {
} else if (
dirName === "utils" ||
dirName === "helpers" ||
dirName === "util"
) {
role = "Utility functions";
} else if (dirName === "types" || dirName === "type") {
role = "Type definitions";
@@ -245,7 +270,11 @@ export async function extractPackageLLM(
role = "Custom hooks";
} else if (dirName === "api" || dirName === "apis") {
role = "API endpoints/handlers";
} else if (dirName === "db" || dirName === "database" || dirName === "models") {
} else if (
dirName === "db" ||
dirName === "database" ||
dirName === "models"
) {
role = "Database layer";
} else if (dirName === "auth" || dirName === "authentication") {
role = "Authentication layer";
@@ -255,7 +284,9 @@ export async function extractPackageLLM(
const purposes = fileData.map((f) => f.purpose);
const hasTests = purposes.some((p) => p.includes("Test"));
const hasTypes = purposes.some((p) => p.includes("Type"));
const hasComponents = purposes.some((p) => p.includes("component") || p.includes("Component"));
const hasComponents = purposes.some(
(p) => p.includes("component") || p.includes("Component"),
);
const hasUtils = purposes.some((p) => p.includes("Utility"));
let arch = "";