@danielstyle/featflow 0.6.5 → 0.7.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.
- package/README.md +10 -1
- package/README.zh-CN.md +10 -1
- package/bin/featflow.mjs +11 -0
- package/package.json +6 -2
- package/skills/featflow/SKILL.md +8 -6
- package/src/commands/doctor.mjs +19 -0
- package/src/commands/finish.mjs +42 -1
- package/src/commands/init.mjs +28 -0
- package/src/commands/new.mjs +21 -5
- package/src/commands/notes-cmd.mjs +366 -0
- package/src/commands/pr-cleanup.mjs +53 -0
- package/src/commands/setup.mjs +18 -0
- package/src/commands/status.mjs +8 -0
- package/src/commands/sync-dev.mjs +9 -0
- package/src/commands/worktree.mjs +10 -0
- package/src/config.mjs +64 -0
- package/src/hook-adapters.mjs +2 -2
- package/src/hooks-core.mjs +46 -9
- package/src/index.mjs +4 -0
- package/src/init-args.mjs +17 -0
- package/src/notes-migrate.mjs +399 -0
- package/src/notes-query.mjs +493 -0
- package/src/notes-recall.mjs +193 -0
- package/src/notes.mjs +549 -0
- package/templates/snippets/AGENTS-workflow.md +3 -2
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import { changedFiles, matchesPattern, normalizeRelPath } from "./changed-files.mjs";
|
|
5
|
+
import {
|
|
6
|
+
listNoteEntries,
|
|
7
|
+
parseFramedRecords,
|
|
8
|
+
readBlobsBatch,
|
|
9
|
+
readRecordsFromCommit,
|
|
10
|
+
refExists,
|
|
11
|
+
resolveNotesRef,
|
|
12
|
+
runGitRaw,
|
|
13
|
+
} from "./notes.mjs";
|
|
14
|
+
import { extractFrontmatter, extractSection } from "./plan.mjs";
|
|
15
|
+
|
|
16
|
+
const LOCAL_FEATURES_DIR = ".featflow/features";
|
|
17
|
+
|
|
18
|
+
function normalizePathSlashes(raw) {
|
|
19
|
+
return String(raw ?? "").replaceAll("\\", "/").replace(/^\.\//, "");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function parseBulletItems(sectionText, maxItems = 10) {
|
|
23
|
+
if (!sectionText) return [];
|
|
24
|
+
const items = [];
|
|
25
|
+
for (const line of sectionText.split(/\r?\n/)) {
|
|
26
|
+
const match = line.match(/^\s*[-*]\s+(?:\[[ xX]\]\s+)?(.+)$/);
|
|
27
|
+
if (match) {
|
|
28
|
+
const cleaned = match[1].trim();
|
|
29
|
+
if (cleaned && !/^Replace with\b|^None\b/i.test(cleaned)) {
|
|
30
|
+
items.push(cleaned);
|
|
31
|
+
if (items.length >= maxItems) break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return items;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function extractChangedFilesFromMarkdown(markdown) {
|
|
39
|
+
const section = extractSection(markdown, "# Changed Files");
|
|
40
|
+
if (!section) return [];
|
|
41
|
+
const files = [];
|
|
42
|
+
for (const line of section.split(/\r?\n/)) {
|
|
43
|
+
const codeMatch = line.match(/`([^`]+)`/);
|
|
44
|
+
if (codeMatch) {
|
|
45
|
+
const candidate = normalizePathSlashes(codeMatch[1].trim());
|
|
46
|
+
if (candidate && !candidate.includes(" ") && candidate !== "path/to/file") {
|
|
47
|
+
files.push(candidate);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const bulletMatch = line.match(/^\s*[-*]\s+([^\s(]+)/);
|
|
52
|
+
if (bulletMatch) {
|
|
53
|
+
const candidate = normalizePathSlashes(bulletMatch[1].trim());
|
|
54
|
+
if (candidate.includes("/") && candidate !== "path/to/file") {
|
|
55
|
+
files.push(candidate);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [...new Set(files)];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function parseSurfaces(rawSurfaces) {
|
|
63
|
+
const raw = String(rawSurfaces ?? "").trim();
|
|
64
|
+
if (!raw || raw === "[]") return [];
|
|
65
|
+
const inner = raw.startsWith("[") && raw.endsWith("]") ? raw.slice(1, -1) : raw;
|
|
66
|
+
return inner
|
|
67
|
+
.split(",")
|
|
68
|
+
.map((item) => item.trim().replace(/^['"]|['"]$/g, ""))
|
|
69
|
+
.filter(Boolean);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function buildRecordView(raw, meta = {}) {
|
|
73
|
+
const content = String(raw ?? "");
|
|
74
|
+
const fm = extractFrontmatter(content);
|
|
75
|
+
const id = meta.id || fm.id || (meta.path ? path.basename(meta.path, ".md") : "unknown");
|
|
76
|
+
const intentSection = extractSection(content, "## Intent");
|
|
77
|
+
const intent = intentSection ? intentSection.split(/\r?\n\r?\n/)[0].replace(/\s+/g, " ").trim() : "";
|
|
78
|
+
const decisions = parseBulletItems(extractSection(content, "# Decisions"), 8);
|
|
79
|
+
const verification = parseBulletItems(extractSection(content, "# Verification"), 8);
|
|
80
|
+
const changed = extractChangedFilesFromMarkdown(content);
|
|
81
|
+
|
|
82
|
+
let status = fm.status || meta.bucket || "done";
|
|
83
|
+
if (meta.bucket === "active" && (!fm.status || fm.status === "planned")) {
|
|
84
|
+
status = fm.status || "active";
|
|
85
|
+
} else if (meta.bucket === "paused") {
|
|
86
|
+
status = "paused";
|
|
87
|
+
} else if (meta.bucket === "done" || meta.source === "note") {
|
|
88
|
+
status = fm.status || "done";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
id,
|
|
93
|
+
title: fm.title || id,
|
|
94
|
+
status,
|
|
95
|
+
bucket: meta.bucket || (meta.source === "note" ? "done" : "active"),
|
|
96
|
+
branch: fm.branch || "",
|
|
97
|
+
owner: fm.owner || "",
|
|
98
|
+
created: fm.created || "",
|
|
99
|
+
updated: fm.updated || fm.created || "",
|
|
100
|
+
surfaces: parseSurfaces(fm.surfaces),
|
|
101
|
+
source: meta.source || "file",
|
|
102
|
+
commitSha: meta.commitSha || null,
|
|
103
|
+
path: meta.path || meta.legacyPath || "",
|
|
104
|
+
intent,
|
|
105
|
+
decisions,
|
|
106
|
+
verification,
|
|
107
|
+
changedFiles: changed,
|
|
108
|
+
content,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function scanDirectoryRecords(repoRoot, baseDir, sourceLabel, buckets = ["active", "paused", "done"]) {
|
|
113
|
+
const results = [];
|
|
114
|
+
for (const bucket of buckets) {
|
|
115
|
+
const dir = path.resolve(repoRoot, baseDir, bucket);
|
|
116
|
+
if (!existsSync(dir)) continue;
|
|
117
|
+
for (const name of readdirSync(dir).sort()) {
|
|
118
|
+
if (!name.endsWith(".md") || name.startsWith("_")) continue;
|
|
119
|
+
const absPath = path.join(dir, name);
|
|
120
|
+
const relPath = normalizePathSlashes(path.posix.join(normalizePathSlashes(baseDir), bucket, name));
|
|
121
|
+
try {
|
|
122
|
+
const content = readFileSync(absPath, "utf8");
|
|
123
|
+
results.push(
|
|
124
|
+
buildRecordView(content, {
|
|
125
|
+
id: path.basename(name, ".md"),
|
|
126
|
+
source: sourceLabel,
|
|
127
|
+
bucket,
|
|
128
|
+
path: relPath,
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
} catch {
|
|
132
|
+
// Ignore unreadable files
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return results;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function loadAllNoteRecords(repoRoot, config) {
|
|
140
|
+
const notesRef = resolveNotesRef(config);
|
|
141
|
+
const entries = listNoteEntries(repoRoot, notesRef);
|
|
142
|
+
if (entries.length === 0) return [];
|
|
143
|
+
const blobMap = readBlobsBatch(
|
|
144
|
+
entries.map((entry) => entry.noteBlob),
|
|
145
|
+
repoRoot,
|
|
146
|
+
);
|
|
147
|
+
const records = [];
|
|
148
|
+
for (const entry of entries) {
|
|
149
|
+
const raw = blobMap.get(entry.noteBlob);
|
|
150
|
+
if (!raw) continue;
|
|
151
|
+
for (const parsed of parseFramedRecords(raw, entry.commitSha.slice(0, 12))) {
|
|
152
|
+
records.push(
|
|
153
|
+
buildRecordView(parsed.content, {
|
|
154
|
+
id: parsed.id,
|
|
155
|
+
source: "note",
|
|
156
|
+
bucket: "done",
|
|
157
|
+
commitSha: entry.commitSha,
|
|
158
|
+
legacyPath: parsed.legacyPath,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return records;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function recordPriority(record) {
|
|
167
|
+
if (record.bucket === "active") return 4;
|
|
168
|
+
if (record.bucket === "paused") return 3;
|
|
169
|
+
if (record.source === "note") return 2;
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function dedupeRecords(records) {
|
|
174
|
+
const byId = new Map();
|
|
175
|
+
for (const rec of records) {
|
|
176
|
+
const existing = byId.get(rec.id);
|
|
177
|
+
if (!existing) {
|
|
178
|
+
byId.set(rec.id, rec);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const existingPrio = recordPriority(existing);
|
|
182
|
+
const nextPrio = recordPriority(rec);
|
|
183
|
+
if (nextPrio > existingPrio) {
|
|
184
|
+
byId.set(rec.id, { ...rec, path: rec.path || existing.path });
|
|
185
|
+
} else if (nextPrio === existingPrio && rec.updated > existing.updated) {
|
|
186
|
+
byId.set(rec.id, { ...rec, path: rec.path || existing.path });
|
|
187
|
+
} else if (!existing.path && rec.path) {
|
|
188
|
+
byId.set(rec.id, { ...existing, path: rec.path });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return [...byId.values()];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function sortNewestFirst(records) {
|
|
195
|
+
return records.sort((a, b) => {
|
|
196
|
+
const keyA = a.updated || a.created || a.id;
|
|
197
|
+
const keyB = b.updated || b.created || b.id;
|
|
198
|
+
if (keyA !== keyB) return keyB.localeCompare(keyA);
|
|
199
|
+
return b.id.localeCompare(a.id);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function listFeatureRecords(repoRoot, config, options = {}) {
|
|
204
|
+
const sourceFilter = options.source || "all";
|
|
205
|
+
const statusFilter = options.status || "all";
|
|
206
|
+
const limit = Number(options.limit) > 0 ? Math.floor(Number(options.limit)) : 0;
|
|
207
|
+
const featuresDir = config?.featuresDir || "features";
|
|
208
|
+
const buckets =
|
|
209
|
+
statusFilter === "active"
|
|
210
|
+
? ["active"]
|
|
211
|
+
: statusFilter === "paused"
|
|
212
|
+
? ["paused"]
|
|
213
|
+
: statusFilter === "done"
|
|
214
|
+
? ["done"]
|
|
215
|
+
: ["active", "paused", "done"];
|
|
216
|
+
|
|
217
|
+
const collected = [];
|
|
218
|
+
if (sourceFilter === "all" || sourceFilter === "files") {
|
|
219
|
+
collected.push(...scanDirectoryRecords(repoRoot, featuresDir, "file", buckets));
|
|
220
|
+
if (featuresDir !== LOCAL_FEATURES_DIR) {
|
|
221
|
+
collected.push(...scanDirectoryRecords(repoRoot, LOCAL_FEATURES_DIR, "local", buckets));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if ((sourceFilter === "all" || sourceFilter === "notes") && (statusFilter === "all" || statusFilter === "done")) {
|
|
225
|
+
collected.push(...loadAllNoteRecords(repoRoot, config));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let deduped = dedupeRecords(collected);
|
|
229
|
+
if (statusFilter !== "all") {
|
|
230
|
+
deduped = deduped.filter((rec) => {
|
|
231
|
+
if (statusFilter === "active") return rec.bucket === "active" || ["planned", "in_progress", "review", "blocked"].includes(rec.status);
|
|
232
|
+
if (statusFilter === "paused") return rec.bucket === "paused" || rec.status === "paused";
|
|
233
|
+
if (statusFilter === "done") return rec.bucket === "done" || rec.status === "done";
|
|
234
|
+
return rec.status === statusFilter;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const sorted = sortNewestFirst(deduped);
|
|
239
|
+
return limit > 0 ? sorted.slice(0, limit) : sorted;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function showFeatureRecord(repoRoot, config, identifier) {
|
|
243
|
+
const query = String(identifier ?? "").trim();
|
|
244
|
+
if (!query) {
|
|
245
|
+
return { records: [], matchedBy: "none" };
|
|
246
|
+
}
|
|
247
|
+
const featuresDir = config?.featuresDir || "features";
|
|
248
|
+
const notesRef = resolveNotesRef(config);
|
|
249
|
+
|
|
250
|
+
const directCandidates = [
|
|
251
|
+
path.resolve(repoRoot, query),
|
|
252
|
+
path.resolve(repoRoot, featuresDir, query),
|
|
253
|
+
path.resolve(repoRoot, LOCAL_FEATURES_DIR, query),
|
|
254
|
+
];
|
|
255
|
+
for (const absPath of directCandidates) {
|
|
256
|
+
if (existsSync(absPath) && absPath.endsWith(".md")) {
|
|
257
|
+
const rel = normalizePathSlashes(path.relative(repoRoot, absPath));
|
|
258
|
+
const bucket = rel.includes("/active/") ? "active" : rel.includes("/paused/") ? "paused" : "done";
|
|
259
|
+
const source = rel.startsWith(`${LOCAL_FEATURES_DIR}/`) ? "local" : "file";
|
|
260
|
+
const content = readFileSync(absPath, "utf8");
|
|
261
|
+
return {
|
|
262
|
+
records: [buildRecordView(content, { id: path.basename(absPath, ".md"), source, bucket, path: rel })],
|
|
263
|
+
matchedBy: "file-path",
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (/^[0-9a-f]{7,40}$/i.test(query)) {
|
|
269
|
+
const rev = runGitRaw(["rev-parse", "--verify", `${query}^{commit}`], repoRoot);
|
|
270
|
+
if (rev.status === 0 && rev.stdout.trim()) {
|
|
271
|
+
const sha = rev.stdout.trim();
|
|
272
|
+
const parsed = readRecordsFromCommit(sha, repoRoot, notesRef);
|
|
273
|
+
if (parsed.length > 0) {
|
|
274
|
+
return {
|
|
275
|
+
records: parsed.map((rec) =>
|
|
276
|
+
buildRecordView(rec.content, {
|
|
277
|
+
id: rec.id,
|
|
278
|
+
source: "note",
|
|
279
|
+
bucket: "done",
|
|
280
|
+
commitSha: sha,
|
|
281
|
+
legacyPath: rec.legacyPath,
|
|
282
|
+
}),
|
|
283
|
+
),
|
|
284
|
+
matchedBy: "commit-sha",
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const normalizedId = query.endsWith(".md") ? path.basename(query, ".md") : query;
|
|
291
|
+
const all = listFeatureRecords(repoRoot, config, { source: "all", status: "all" });
|
|
292
|
+
const exact = all.filter((rec) => rec.id === normalizedId || (rec.path && normalizePathSlashes(rec.path) === normalizePathSlashes(query)));
|
|
293
|
+
if (exact.length > 0) {
|
|
294
|
+
return { records: exact, matchedBy: "id" };
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const partial = all.filter((rec) => rec.id.includes(normalizedId));
|
|
298
|
+
if (partial.length === 1) {
|
|
299
|
+
return { records: partial, matchedBy: "partial-id" };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return { records: [], matchedBy: "none", candidates: partial.slice(0, 10).map((r) => r.id) };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function collectSnippets(content, needleLower, maxSnippets = 3) {
|
|
306
|
+
const snippets = [];
|
|
307
|
+
const lines = content.split(/\r?\n/);
|
|
308
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
309
|
+
if (lines[i].toLowerCase().includes(needleLower)) {
|
|
310
|
+
snippets.push({ line: i + 1, text: lines[i].trim().slice(0, 180) });
|
|
311
|
+
if (snippets.length >= maxSnippets) break;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return snippets;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function searchFeatureRecords(repoRoot, config, query, options = {}) {
|
|
318
|
+
const needle = String(query ?? "").trim();
|
|
319
|
+
if (!needle) return [];
|
|
320
|
+
const needleLower = needle.toLowerCase();
|
|
321
|
+
const limit = Number(options.limit) > 0 ? Math.floor(Number(options.limit)) : 20;
|
|
322
|
+
const sourceFilter = options.source || "all";
|
|
323
|
+
const featuresDir = config?.featuresDir || "features";
|
|
324
|
+
const notesRef = resolveNotesRef(config);
|
|
325
|
+
const hits = [];
|
|
326
|
+
|
|
327
|
+
if ((sourceFilter === "all" || sourceFilter === "notes") && refExists(notesRef, repoRoot)) {
|
|
328
|
+
const grepRes = runGitRaw(["grep", "-i", "-l", "-F", "--", needle, notesRef], repoRoot);
|
|
329
|
+
if (grepRes.status === 0 && grepRes.stdout.trim()) {
|
|
330
|
+
const shas = grepRes.stdout
|
|
331
|
+
.split(/\r?\n/)
|
|
332
|
+
.map((line) => line.trim())
|
|
333
|
+
.filter(Boolean)
|
|
334
|
+
.map((line) => {
|
|
335
|
+
const colon = line.indexOf(":");
|
|
336
|
+
return colon >= 0 ? line.slice(colon + 1).replaceAll("/", "") : "";
|
|
337
|
+
})
|
|
338
|
+
.filter((sha) => /^[0-9a-f]{40}$/i.test(sha));
|
|
339
|
+
|
|
340
|
+
const uniqueShas = [...new Set(shas)];
|
|
341
|
+
for (const sha of uniqueShas) {
|
|
342
|
+
const parsed = readRecordsFromCommit(sha, repoRoot, notesRef);
|
|
343
|
+
for (const rec of parsed) {
|
|
344
|
+
if (!rec.content.toLowerCase().includes(needleLower)) continue;
|
|
345
|
+
const view = buildRecordView(rec.content, {
|
|
346
|
+
id: rec.id,
|
|
347
|
+
source: "note",
|
|
348
|
+
bucket: "done",
|
|
349
|
+
commitSha: sha,
|
|
350
|
+
legacyPath: rec.legacyPath,
|
|
351
|
+
});
|
|
352
|
+
hits.push({ ...view, snippets: collectSnippets(rec.content, needleLower) });
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (sourceFilter === "all" || sourceFilter === "files") {
|
|
359
|
+
const fileRecords = [
|
|
360
|
+
...scanDirectoryRecords(repoRoot, featuresDir, "file"),
|
|
361
|
+
...(featuresDir === LOCAL_FEATURES_DIR ? [] : scanDirectoryRecords(repoRoot, LOCAL_FEATURES_DIR, "local")),
|
|
362
|
+
];
|
|
363
|
+
for (const rec of fileRecords) {
|
|
364
|
+
if (!rec.content.toLowerCase().includes(needleLower) && !rec.id.toLowerCase().includes(needleLower)) continue;
|
|
365
|
+
hits.push({ ...rec, snippets: collectSnippets(rec.content, needleLower) });
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const deduped = dedupeRecords(hits);
|
|
370
|
+
return sortNewestFirst(deduped).slice(0, limit);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function pathMatchesTarget(changedFile, targetPath) {
|
|
374
|
+
const normChanged = normalizeRelPath(changedFile);
|
|
375
|
+
const normTarget = normalizeRelPath(targetPath).replace(/\/+$/, "");
|
|
376
|
+
if (!normChanged || !normTarget) return false;
|
|
377
|
+
if (normChanged === normTarget) return true;
|
|
378
|
+
if (normChanged.startsWith(`${normTarget}/`) || normTarget.startsWith(`${normChanged}/`)) return true;
|
|
379
|
+
return matchesPattern(normChanged, normTarget);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function historyForPaths(repoRoot, config, rawPaths = [], options = {}) {
|
|
383
|
+
const limit = Number(options.limit) > 0 ? Math.floor(Number(options.limit)) : 10;
|
|
384
|
+
const targets = (options.changed ? changedFiles(repoRoot, config) : rawPaths)
|
|
385
|
+
.map((p) => normalizeRelPath(p))
|
|
386
|
+
.filter(Boolean);
|
|
387
|
+
|
|
388
|
+
if (targets.length === 0) {
|
|
389
|
+
return { paths: [], records: [] };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const notesRef = resolveNotesRef(config);
|
|
393
|
+
const byId = new Map();
|
|
394
|
+
|
|
395
|
+
const recordHit = (rec, reason, matchedPaths) => {
|
|
396
|
+
const prev = byId.get(rec.id);
|
|
397
|
+
if (!prev) {
|
|
398
|
+
byId.set(rec.id, {
|
|
399
|
+
...rec,
|
|
400
|
+
matchedBy: reason,
|
|
401
|
+
matchedPaths: [...new Set(matchedPaths)],
|
|
402
|
+
});
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const mergedReason = prev.matchedBy === reason ? reason : "both";
|
|
406
|
+
const mergedPaths = [...new Set([...prev.matchedPaths, ...matchedPaths])];
|
|
407
|
+
const preferNext = recordPriority(rec) > recordPriority(prev);
|
|
408
|
+
byId.set(rec.id, {
|
|
409
|
+
...(preferNext ? rec : prev),
|
|
410
|
+
matchedBy: mergedReason,
|
|
411
|
+
matchedPaths: mergedPaths,
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
if (refExists(notesRef, repoRoot)) {
|
|
416
|
+
const noteEntries = listNoteEntries(repoRoot, notesRef);
|
|
417
|
+
const noteCommitSet = new Set(noteEntries.map((entry) => entry.commitSha));
|
|
418
|
+
|
|
419
|
+
const logRes = runGitRaw(["log", "-n", "300", "--format=%H", "--", ...targets], repoRoot);
|
|
420
|
+
if (logRes.status === 0 && logRes.stdout.trim()) {
|
|
421
|
+
const touchedCommits = logRes.stdout
|
|
422
|
+
.split(/\r?\n/)
|
|
423
|
+
.map((line) => line.trim())
|
|
424
|
+
.filter((sha) => noteCommitSet.has(sha));
|
|
425
|
+
for (const sha of touchedCommits) {
|
|
426
|
+
const parsed = readRecordsFromCommit(sha, repoRoot, notesRef);
|
|
427
|
+
for (const rec of parsed) {
|
|
428
|
+
const view = buildRecordView(rec.content, {
|
|
429
|
+
id: rec.id,
|
|
430
|
+
source: "note",
|
|
431
|
+
bucket: "done",
|
|
432
|
+
commitSha: sha,
|
|
433
|
+
legacyPath: rec.legacyPath,
|
|
434
|
+
});
|
|
435
|
+
recordHit(view, "commit", targets);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const grepArgs = ["grep", "-l", "-F"];
|
|
441
|
+
for (const target of targets.slice(0, 20)) {
|
|
442
|
+
grepArgs.push("-e", target.replace(/\/+$/, ""));
|
|
443
|
+
}
|
|
444
|
+
grepArgs.push(notesRef);
|
|
445
|
+
const grepRes = runGitRaw(grepArgs, repoRoot);
|
|
446
|
+
if (grepRes.status === 0 && grepRes.stdout.trim()) {
|
|
447
|
+
const shas = [...new Set(
|
|
448
|
+
grepRes.stdout
|
|
449
|
+
.split(/\r?\n/)
|
|
450
|
+
.map((line) => line.trim())
|
|
451
|
+
.filter(Boolean)
|
|
452
|
+
.map((line) => {
|
|
453
|
+
const colon = line.indexOf(":");
|
|
454
|
+
return colon >= 0 ? line.slice(colon + 1).replaceAll("/", "") : "";
|
|
455
|
+
})
|
|
456
|
+
.filter((sha) => /^[0-9a-f]{40}$/i.test(sha)),
|
|
457
|
+
)];
|
|
458
|
+
for (const sha of shas) {
|
|
459
|
+
for (const rec of readRecordsFromCommit(sha, repoRoot, notesRef)) {
|
|
460
|
+
const view = buildRecordView(rec.content, {
|
|
461
|
+
id: rec.id,
|
|
462
|
+
source: "note",
|
|
463
|
+
bucket: "done",
|
|
464
|
+
commitSha: sha,
|
|
465
|
+
legacyPath: rec.legacyPath,
|
|
466
|
+
});
|
|
467
|
+
const matched = targets.filter((t) => view.changedFiles.some((cf) => pathMatchesTarget(cf, t)));
|
|
468
|
+
if (matched.length > 0) {
|
|
469
|
+
recordHit(view, "changed-files", matched);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const featuresDir = config?.featuresDir || "features";
|
|
477
|
+
const fileRecords = [
|
|
478
|
+
...scanDirectoryRecords(repoRoot, featuresDir, "file", ["done", "active", "paused"]),
|
|
479
|
+
...(featuresDir === LOCAL_FEATURES_DIR ? [] : scanDirectoryRecords(repoRoot, LOCAL_FEATURES_DIR, "local", ["done", "active", "paused"])),
|
|
480
|
+
];
|
|
481
|
+
for (const view of fileRecords) {
|
|
482
|
+
const matched = targets.filter((t) => view.changedFiles.some((cf) => pathMatchesTarget(cf, t)));
|
|
483
|
+
if (matched.length > 0) {
|
|
484
|
+
recordHit(view, "changed-files", matched);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return {
|
|
489
|
+
paths: targets,
|
|
490
|
+
records: sortNewestFirst([...byId.values()]).slice(0, limit),
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
readRecordsFromCommit,
|
|
6
|
+
refExists,
|
|
7
|
+
resolveNotesRef,
|
|
8
|
+
runGitRaw,
|
|
9
|
+
} from "./notes.mjs";
|
|
10
|
+
import { buildRecordView, dedupeRecords } from "./notes-query.mjs";
|
|
11
|
+
|
|
12
|
+
const EN_STOPWORDS = new Set([
|
|
13
|
+
"the", "and", "for", "with", "that", "this", "from", "into", "have", "has", "are", "was", "were",
|
|
14
|
+
"will", "would", "could", "should", "can", "may", "not", "but", "all", "any", "one", "two", "new",
|
|
15
|
+
"use", "using", "used", "add", "update", "fix", "bug", "feat", "feature", "plan", "code", "file",
|
|
16
|
+
"files", "test", "tests", "check", "run", "make", "need", "want", "please", "help", "work", "repo",
|
|
17
|
+
"git", "branch", "commit", "merge", "push", "pull", "worktree", "featflow", "notes", "note", "done",
|
|
18
|
+
"active", "paused", "status", "record", "records", "change", "changes", "refactor", "implement",
|
|
19
|
+
"issue", "issues", "docs", "doc", "also", "just", "only", "when", "then", "now", "here", "there",
|
|
20
|
+
"what", "where", "which", "how", "why", "who", "about", "after", "before", "more", "some", "each",
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const ZH_STOPWORDS = new Set([
|
|
24
|
+
"帮我", "一下", "实现", "修复", "问题", "功能", "代码", "文件", "提交", "分支", "计划", "可以",
|
|
25
|
+
"需要", "看一下", "目前", "现在", "已经", "这个", "那个", "相关", "如果", "然后", "还是", "我们",
|
|
26
|
+
"你们", "他们", "就是", "不是", "没有", "一个", "一些", "所有", "进行", "使用", "支持", "添加",
|
|
27
|
+
"修改", "删除", "更新", "检查", "确认", "开始", "完成", "直接", "这里", "那里", "什么", "怎么",
|
|
28
|
+
"为什么", "是否", "能否", "看看", "处理", "一下吧", "好的", "可以吗",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function normalizeSlashes(raw) {
|
|
32
|
+
return String(raw ?? "").replaceAll("\\", "/").replace(/^\.\//, "");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function extractPromptTokens(prompt) {
|
|
36
|
+
const text = String(prompt ?? "");
|
|
37
|
+
const tokens = [];
|
|
38
|
+
|
|
39
|
+
for (const match of text.matchAll(/[A-Za-z0-9][A-Za-z0-9_./-]{2,}/g)) {
|
|
40
|
+
const raw = match[0].toLowerCase().replace(/^[./-]+|[./-]+$/g, "");
|
|
41
|
+
if (!raw || raw.length < 3 || EN_STOPWORDS.has(raw) || /^\d+$/.test(raw)) continue;
|
|
42
|
+
tokens.push(raw);
|
|
43
|
+
for (const part of raw.split(/[/_.-]+/)) {
|
|
44
|
+
if (part.length >= 3 && !EN_STOPWORDS.has(part) && !/^\d+$/.test(part)) {
|
|
45
|
+
tokens.push(part);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const match of text.matchAll(/[\u4e00-\u9fff]{2,8}/g)) {
|
|
51
|
+
const chunk = match[0];
|
|
52
|
+
if (ZH_STOPWORDS.has(chunk)) continue;
|
|
53
|
+
if (chunk.length <= 4) {
|
|
54
|
+
tokens.push(chunk);
|
|
55
|
+
} else {
|
|
56
|
+
for (let i = 0; i <= chunk.length - 2; i += 2) {
|
|
57
|
+
const sub = chunk.slice(i, i + 2);
|
|
58
|
+
if (!ZH_STOPWORDS.has(sub)) tokens.push(sub);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const unique = [...new Set(tokens)];
|
|
64
|
+
unique.sort((a, b) => {
|
|
65
|
+
const aSpecial = /[-_/.]/.test(a) ? 1 : 0;
|
|
66
|
+
const bSpecial = /[-_/.]/.test(b) ? 1 : 0;
|
|
67
|
+
if (aSpecial !== bSpecial) return bSpecial - aSpecial;
|
|
68
|
+
return b.length - a.length;
|
|
69
|
+
});
|
|
70
|
+
return unique.slice(0, 8);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function scoreCandidate(record, tokens) {
|
|
74
|
+
const idLower = record.id.toLowerCase();
|
|
75
|
+
const titleLower = record.title.toLowerCase();
|
|
76
|
+
const surfaceLower = record.surfaces.join(" ").toLowerCase();
|
|
77
|
+
const bodyLower = record.content.toLowerCase();
|
|
78
|
+
let score = 0;
|
|
79
|
+
let matchedTokens = 0;
|
|
80
|
+
|
|
81
|
+
for (const tok of tokens) {
|
|
82
|
+
let hit = false;
|
|
83
|
+
if (idLower.includes(tok)) {
|
|
84
|
+
score += 4;
|
|
85
|
+
hit = true;
|
|
86
|
+
}
|
|
87
|
+
if (titleLower.includes(tok)) {
|
|
88
|
+
score += 3;
|
|
89
|
+
hit = true;
|
|
90
|
+
}
|
|
91
|
+
if (surfaceLower.includes(tok)) {
|
|
92
|
+
score += 2;
|
|
93
|
+
hit = true;
|
|
94
|
+
}
|
|
95
|
+
if (!hit && bodyLower.includes(tok)) {
|
|
96
|
+
score += 1;
|
|
97
|
+
hit = true;
|
|
98
|
+
}
|
|
99
|
+
if (hit) matchedTokens += 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { score, matchedTokens };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function recallFeaturesForPrompt(repoRoot, config, prompt, options = {}) {
|
|
106
|
+
const limit = Number(options.limit ?? config?.notes?.recallLimit ?? 3);
|
|
107
|
+
if (limit <= 0) return [];
|
|
108
|
+
const tokens = extractPromptTokens(prompt);
|
|
109
|
+
if (tokens.length === 0) return [];
|
|
110
|
+
|
|
111
|
+
const notesRef = resolveNotesRef(config);
|
|
112
|
+
const candidates = [];
|
|
113
|
+
|
|
114
|
+
if (refExists(notesRef, repoRoot)) {
|
|
115
|
+
const grepArgs = ["grep", "-i", "-l", "-F"];
|
|
116
|
+
for (const tok of tokens) {
|
|
117
|
+
grepArgs.push("-e", tok);
|
|
118
|
+
}
|
|
119
|
+
grepArgs.push(notesRef);
|
|
120
|
+
const grepRes = runGitRaw(grepArgs, repoRoot);
|
|
121
|
+
if (grepRes.status === 0 && grepRes.stdout.trim()) {
|
|
122
|
+
const shas = [...new Set(
|
|
123
|
+
grepRes.stdout
|
|
124
|
+
.split(/\r?\n/)
|
|
125
|
+
.map((line) => line.trim())
|
|
126
|
+
.filter(Boolean)
|
|
127
|
+
.map((line) => {
|
|
128
|
+
const colon = line.indexOf(":");
|
|
129
|
+
return colon >= 0 ? line.slice(colon + 1).replaceAll("/", "") : "";
|
|
130
|
+
})
|
|
131
|
+
.filter((sha) => /^[0-9a-f]{40}$/i.test(sha)),
|
|
132
|
+
)].slice(0, 40);
|
|
133
|
+
|
|
134
|
+
for (const sha of shas) {
|
|
135
|
+
for (const rec of readRecordsFromCommit(sha, repoRoot, notesRef)) {
|
|
136
|
+
candidates.push(
|
|
137
|
+
buildRecordView(rec.content, {
|
|
138
|
+
id: rec.id,
|
|
139
|
+
source: "note",
|
|
140
|
+
bucket: "done",
|
|
141
|
+
commitSha: sha,
|
|
142
|
+
legacyPath: rec.legacyPath,
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const featuresDir = config?.featuresDir || "features";
|
|
151
|
+
const doneDir = path.resolve(repoRoot, featuresDir, "done");
|
|
152
|
+
if (existsSync(doneDir)) {
|
|
153
|
+
const names = readdirSync(doneDir).filter((name) => name.endsWith(".md") && !name.startsWith("_"));
|
|
154
|
+
for (const name of names) {
|
|
155
|
+
const lowerName = name.toLowerCase();
|
|
156
|
+
if (!tokens.some((tok) => lowerName.includes(tok))) continue;
|
|
157
|
+
const absPath = path.join(doneDir, name);
|
|
158
|
+
try {
|
|
159
|
+
const content = readFileSync(absPath, "utf8");
|
|
160
|
+
candidates.push(
|
|
161
|
+
buildRecordView(content, {
|
|
162
|
+
id: path.basename(name, ".md"),
|
|
163
|
+
source: "file",
|
|
164
|
+
bucket: "done",
|
|
165
|
+
path: normalizeSlashes(path.posix.join(normalizeSlashes(featuresDir), "done", name)),
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
} catch {
|
|
169
|
+
// Skip unreadable file
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const deduped = dedupeRecords(candidates);
|
|
175
|
+
const ranked = deduped
|
|
176
|
+
.map((rec) => ({ rec, ...scoreCandidate(rec, tokens) }))
|
|
177
|
+
.filter((item) => item.score >= 3 || item.matchedTokens >= 2)
|
|
178
|
+
.sort((a, b) => {
|
|
179
|
+
if (b.score !== a.score) return b.score - a.score;
|
|
180
|
+
return (b.rec.updated || b.rec.id).localeCompare(a.rec.updated || a.rec.id);
|
|
181
|
+
})
|
|
182
|
+
.slice(0, limit);
|
|
183
|
+
|
|
184
|
+
return ranked.map(({ rec, score }) => ({
|
|
185
|
+
id: rec.id,
|
|
186
|
+
title: rec.title,
|
|
187
|
+
source: rec.source,
|
|
188
|
+
commitSha: rec.commitSha,
|
|
189
|
+
path: rec.path,
|
|
190
|
+
decision: rec.decisions[0] || "",
|
|
191
|
+
score,
|
|
192
|
+
}));
|
|
193
|
+
}
|