fix: handle bare filenames in regenerate-claude-md.ts (fixes #1514)

This commit is contained in:
Octopus
2026-04-03 10:07:11 +08:00
parent 1b5d1a1234
commit c74101b7f7
+25 -4
View File
@@ -94,9 +94,12 @@ function getTrackedFolders(workingDir: string): Set<string> {
const absPath = path.join(workingDir, file); const absPath = path.join(workingDir, file);
let dir = path.dirname(absPath); let dir = path.dirname(absPath);
// Add all parent directories up to (but not including) the working dir // Add all parent directories up to and including the working dir itself.
while (dir.length > workingDir.length && dir.startsWith(workingDir)) { // The working dir is included so that root-level files (stored in the DB
// as bare filenames with no directory component) can be matched. Fixes #1514.
while (dir.length >= workingDir.length && dir.startsWith(workingDir)) {
folders.add(dir); folders.add(dir);
if (dir === workingDir) break;
dir = path.dirname(dir); dir = path.dirname(dir);
} }
} }
@@ -164,6 +167,24 @@ function findObservationsByFolder(db: Database, relativeFolderPath: string, proj
// Query more results than needed since we'll filter some out // Query more results than needed since we'll filter some out
const queryLimit = limit * 3; const queryLimit = limit * 3;
// For the root folder (empty relativeFolderPath), observations may have bare
// filenames stored without any directory component (e.g. ["dashboard.html"]).
// In that case the LIKE pattern below would never match, so we fetch all
// observations for the project and let isDirectChild filter to root-level files.
// Fixes #1514.
let allMatches: ObservationRow[];
if (relativeFolderPath === '' || relativeFolderPath === '.') {
const sql = `
SELECT o.*, o.discovery_tokens
FROM observations o
WHERE o.project = ?
AND (o.files_modified IS NOT NULL OR o.files_read IS NOT NULL)
ORDER BY o.created_at_epoch DESC
LIMIT ?
`;
allMatches = db.prepare(sql).all(project, queryLimit) as ObservationRow[];
} else {
const sql = ` const sql = `
SELECT o.*, o.discovery_tokens SELECT o.*, o.discovery_tokens
FROM observations o FROM observations o
@@ -172,11 +193,11 @@ function findObservationsByFolder(db: Database, relativeFolderPath: string, proj
ORDER BY o.created_at_epoch DESC ORDER BY o.created_at_epoch DESC
LIMIT ? LIMIT ?
`; `;
// Files in DB are stored as relative paths like "src/services/foo.ts" // Files in DB are stored as relative paths like "src/services/foo.ts"
// Match any file that starts with this folder path (we'll filter to direct children below) // Match any file that starts with this folder path (we'll filter to direct children below)
const likePattern = `%"${relativeFolderPath}/%`; const likePattern = `%"${relativeFolderPath}/%`;
const allMatches = db.prepare(sql).all(project, likePattern, likePattern, queryLimit) as ObservationRow[]; allMatches = db.prepare(sql).all(project, likePattern, likePattern, queryLimit) as ObservationRow[];
}
// Filter to only observations with direct child files (not in subfolders) // Filter to only observations with direct child files (not in subfolders)
return allMatches.filter(obs => hasDirectChildFile(obs, relativeFolderPath)).slice(0, limit); return allMatches.filter(obs => hasDirectChildFile(obs, relativeFolderPath)).slice(0, limit);