@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
package/src/notes.mjs
ADDED
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
import { isAncestor, remoteExists, runStatus } from "./git.mjs";
|
|
5
|
+
import { extractFrontmatter } from "./plan.mjs";
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_NOTES_REF = "refs/notes/featflow";
|
|
8
|
+
export const RECORD_SCHEMA_VERSION = "1";
|
|
9
|
+
|
|
10
|
+
const OPEN_TAG_RE = /^<!-- featflow:record ([^\n]+?) -->$/;
|
|
11
|
+
|
|
12
|
+
export function runGitRaw(args, cwd = process.cwd(), extra = {}) {
|
|
13
|
+
const result = spawnSync("git", args, {
|
|
14
|
+
cwd,
|
|
15
|
+
encoding: "utf8",
|
|
16
|
+
input: extra.input,
|
|
17
|
+
maxBuffer: extra.maxBuffer ?? 64 * 1024 * 1024,
|
|
18
|
+
stdio: [extra.input !== undefined ? "pipe" : "ignore", "pipe", "pipe"],
|
|
19
|
+
env: extra.env ? { ...process.env, ...extra.env } : process.env,
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
status: result.status ?? -1,
|
|
23
|
+
stdout: result.stdout ?? "",
|
|
24
|
+
stderr: (result.stderr ?? "").trim(),
|
|
25
|
+
error: result.error ?? null,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function normalizeNotesRef(rawRef) {
|
|
30
|
+
const trimmed = String(rawRef ?? "").trim();
|
|
31
|
+
if (!trimmed) {
|
|
32
|
+
return DEFAULT_NOTES_REF;
|
|
33
|
+
}
|
|
34
|
+
let full = trimmed;
|
|
35
|
+
if (trimmed.startsWith("refs/notes/")) {
|
|
36
|
+
full = trimmed;
|
|
37
|
+
} else if (trimmed.startsWith("notes/")) {
|
|
38
|
+
full = `refs/${trimmed}`;
|
|
39
|
+
} else if (!trimmed.startsWith("refs/")) {
|
|
40
|
+
full = `refs/notes/${trimmed}`;
|
|
41
|
+
}
|
|
42
|
+
if (/\s|\.\.|\/\/|^\/|\/$/.test(full) || !full.startsWith("refs/notes/")) {
|
|
43
|
+
throw new Error(`Invalid notes ref "${rawRef}". Expected a valid ref under refs/notes/.`);
|
|
44
|
+
}
|
|
45
|
+
return full;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function resolveNotesRef(config) {
|
|
49
|
+
return normalizeNotesRef(config?.notes?.ref ?? DEFAULT_NOTES_REF);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isNotesStorageEnabled(config) {
|
|
53
|
+
return config?.featureStorage === "notes";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function remoteTrackingNotesRef(remote, notesRef = DEFAULT_NOTES_REF) {
|
|
57
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
58
|
+
const suffix = normalized.slice("refs/notes/".length);
|
|
59
|
+
const cleanRemote = String(remote || "origin").trim();
|
|
60
|
+
return `refs/notes/${cleanRemote}/${suffix}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function sha256Text(text) {
|
|
64
|
+
return createHash("sha256").update(String(text ?? ""), "utf8").digest("hex");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function sanitizeAttrValue(value) {
|
|
68
|
+
return String(value ?? "")
|
|
69
|
+
.trim()
|
|
70
|
+
.replace(/\s+/g, "_")
|
|
71
|
+
.replaceAll("-->", "");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function parseTagAttrs(raw) {
|
|
75
|
+
const attrs = {};
|
|
76
|
+
for (const token of raw.trim().split(/\s+/)) {
|
|
77
|
+
const eq = token.indexOf("=");
|
|
78
|
+
if (eq > 0) {
|
|
79
|
+
attrs[token.slice(0, eq)] = token.slice(eq + 1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return attrs;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function bodyNeedsCustomFence(body) {
|
|
86
|
+
const lines = String(body ?? "").split(/\r?\n/);
|
|
87
|
+
return lines.some((line) => line === "<!-- /featflow:record -->" || OPEN_TAG_RE.test(line));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function serializeFramedRecord(record) {
|
|
91
|
+
const body = String(record.content ?? "");
|
|
92
|
+
const frontmatter = extractFrontmatter(body);
|
|
93
|
+
const id = sanitizeAttrValue(record.id || frontmatter.id || "unknown");
|
|
94
|
+
const hasTrailingNewline = body.endsWith("\n");
|
|
95
|
+
const attrs = [`id=${id}`, `schema=${RECORD_SCHEMA_VERSION}`];
|
|
96
|
+
if (record.legacyPath) {
|
|
97
|
+
attrs.push(`legacy_path=${sanitizeAttrValue(record.legacyPath)}`);
|
|
98
|
+
}
|
|
99
|
+
if (!hasTrailingNewline) {
|
|
100
|
+
attrs.push("eof_nl=0");
|
|
101
|
+
}
|
|
102
|
+
let closeTag = "<!-- /featflow:record -->";
|
|
103
|
+
if (bodyNeedsCustomFence(body)) {
|
|
104
|
+
const fence = sha256Text(`${id}:${body}`).slice(0, 10);
|
|
105
|
+
attrs.push(`fence=${fence}`);
|
|
106
|
+
closeTag = `<!-- /featflow:record:${fence} -->`;
|
|
107
|
+
}
|
|
108
|
+
const openTag = `<!-- featflow:record ${attrs.join(" ")} -->`;
|
|
109
|
+
return `${openTag}\n${body}${hasTrailingNewline ? "" : "\n"}${closeTag}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function serializeFramedRecords(records) {
|
|
113
|
+
return `${records.map((record) => serializeFramedRecord(record)).join("\n\n")}\n`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function parseFramedRecords(rawText, fallbackId = "") {
|
|
117
|
+
const text = String(rawText ?? "");
|
|
118
|
+
if (!text) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
const lines = text.split("\n");
|
|
122
|
+
const records = [];
|
|
123
|
+
let i = 0;
|
|
124
|
+
|
|
125
|
+
while (i < lines.length) {
|
|
126
|
+
const line = lines[i].endsWith("\r") ? lines[i].slice(0, -1) : lines[i];
|
|
127
|
+
const openMatch = line.match(OPEN_TAG_RE);
|
|
128
|
+
if (!openMatch) {
|
|
129
|
+
i += 1;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const attrs = parseTagAttrs(openMatch[1]);
|
|
133
|
+
const expectedClose = attrs.fence ? `<!-- /featflow:record:${attrs.fence} -->` : "<!-- /featflow:record -->";
|
|
134
|
+
const bodyLines = [];
|
|
135
|
+
i += 1;
|
|
136
|
+
let foundClose = false;
|
|
137
|
+
while (i < lines.length) {
|
|
138
|
+
const cur = lines[i];
|
|
139
|
+
const normalized = cur.endsWith("\r") ? cur.slice(0, -1) : cur;
|
|
140
|
+
if (normalized === expectedClose) {
|
|
141
|
+
foundClose = true;
|
|
142
|
+
i += 1;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
bodyLines.push(cur);
|
|
146
|
+
i += 1;
|
|
147
|
+
}
|
|
148
|
+
if (!foundClose) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
const joined = bodyLines.join("\n");
|
|
152
|
+
const content = attrs.eof_nl === "0" ? joined : `${joined}\n`;
|
|
153
|
+
const frontmatter = extractFrontmatter(content);
|
|
154
|
+
records.push({
|
|
155
|
+
id: attrs.id || frontmatter.id || fallbackId || "unknown",
|
|
156
|
+
schema: attrs.schema || RECORD_SCHEMA_VERSION,
|
|
157
|
+
legacyPath: attrs.legacy_path || "",
|
|
158
|
+
content,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (records.length > 0) {
|
|
163
|
+
return records;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const frontmatter = extractFrontmatter(text);
|
|
167
|
+
return [
|
|
168
|
+
{
|
|
169
|
+
id: frontmatter.id || fallbackId || "unknown",
|
|
170
|
+
schema: "0",
|
|
171
|
+
legacyPath: "",
|
|
172
|
+
content: text,
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function mergeRecordLists(oursRecords, theirsRecords) {
|
|
178
|
+
const merged = new Map();
|
|
179
|
+
const order = [];
|
|
180
|
+
|
|
181
|
+
const put = (rec) => {
|
|
182
|
+
if (!rec?.id) return;
|
|
183
|
+
if (!merged.has(rec.id)) {
|
|
184
|
+
merged.set(rec.id, rec);
|
|
185
|
+
order.push(rec.id);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const prev = merged.get(rec.id);
|
|
189
|
+
if (prev.content === rec.content) {
|
|
190
|
+
if (!prev.legacyPath && rec.legacyPath) {
|
|
191
|
+
merged.set(rec.id, { ...prev, legacyPath: rec.legacyPath });
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const prevUpdated = extractFrontmatter(prev.content).updated || "";
|
|
196
|
+
const nextUpdated = extractFrontmatter(rec.content).updated || "";
|
|
197
|
+
if (nextUpdated && (!prevUpdated || nextUpdated >= prevUpdated)) {
|
|
198
|
+
merged.set(rec.id, {
|
|
199
|
+
...rec,
|
|
200
|
+
legacyPath: rec.legacyPath || prev.legacyPath,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
for (const rec of oursRecords) put(rec);
|
|
206
|
+
for (const rec of theirsRecords) put(rec);
|
|
207
|
+
|
|
208
|
+
return order.map((id) => merged.get(id));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function refExists(refName, cwd) {
|
|
212
|
+
return runStatus("git", ["show-ref", "--verify", "--quiet", refName], cwd) === 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function writeGitBlob(content, cwd) {
|
|
216
|
+
const res = runGitRaw(["hash-object", "-w", "--stdin"], cwd, { input: String(content ?? "") });
|
|
217
|
+
if (res.status !== 0 || !res.stdout.trim()) {
|
|
218
|
+
throw new Error(`git hash-object failed${res.stderr ? `:\n${res.stderr}` : "."}`);
|
|
219
|
+
}
|
|
220
|
+
return res.stdout.trim();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function readNoteRaw(commitSha, cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
224
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
225
|
+
const res = runGitRaw(["notes", `--ref=${normalized}`, "show", commitSha], cwd);
|
|
226
|
+
if (res.status !== 0) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
return res.stdout;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function writeNoteRaw(commitSha, rawContent, cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
233
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
234
|
+
const blobSha = writeGitBlob(rawContent, cwd);
|
|
235
|
+
const res = runGitRaw(["notes", `--ref=${normalized}`, "add", "-f", "-C", blobSha, commitSha], cwd);
|
|
236
|
+
if (res.status !== 0) {
|
|
237
|
+
throw new Error(`git notes add failed for ${commitSha}${res.stderr ? `:\n${res.stderr}` : "."}`);
|
|
238
|
+
}
|
|
239
|
+
return blobSha;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function readRecordsFromCommit(commitSha, cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
243
|
+
const raw = readNoteRaw(commitSha, cwd, notesRef);
|
|
244
|
+
if (raw === null) {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
return parseFramedRecords(raw, commitSha.slice(0, 12));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function upsertRecordsOnCommit(commitSha, incomingRecords, cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
251
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
252
|
+
const existing = readRecordsFromCommit(commitSha, cwd, normalized);
|
|
253
|
+
const incomingList = Array.isArray(incomingRecords) ? incomingRecords : [incomingRecords];
|
|
254
|
+
const byId = new Map();
|
|
255
|
+
const order = [];
|
|
256
|
+
|
|
257
|
+
for (const rec of existing) {
|
|
258
|
+
byId.set(rec.id, rec);
|
|
259
|
+
order.push(rec.id);
|
|
260
|
+
}
|
|
261
|
+
for (const rec of incomingList) {
|
|
262
|
+
const id = rec.id || extractFrontmatter(rec.content).id || commitSha.slice(0, 12);
|
|
263
|
+
const normalizedRec = {
|
|
264
|
+
id,
|
|
265
|
+
legacyPath: rec.legacyPath || byId.get(id)?.legacyPath || "",
|
|
266
|
+
content: String(rec.content ?? ""),
|
|
267
|
+
};
|
|
268
|
+
if (!byId.has(id)) {
|
|
269
|
+
order.push(id);
|
|
270
|
+
}
|
|
271
|
+
byId.set(id, normalizedRec);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const finalRecords = order.map((id) => byId.get(id));
|
|
275
|
+
const serialized = serializeFramedRecords(finalRecords);
|
|
276
|
+
const blobSha = writeNoteRaw(commitSha, serialized, cwd, normalized);
|
|
277
|
+
return { commitSha, blobSha, recordCount: finalRecords.length, records: finalRecords };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function listNoteEntries(cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
281
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
282
|
+
if (!refExists(normalized, cwd)) {
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
const res = runGitRaw(["notes", `--ref=${normalized}`, "list"], cwd);
|
|
286
|
+
if (res.status !== 0 || !res.stdout.trim()) {
|
|
287
|
+
return [];
|
|
288
|
+
}
|
|
289
|
+
return res.stdout
|
|
290
|
+
.split(/\r?\n/)
|
|
291
|
+
.map((line) => line.trim())
|
|
292
|
+
.filter(Boolean)
|
|
293
|
+
.map((line) => {
|
|
294
|
+
const [noteBlob, commitSha] = line.split(/\s+/);
|
|
295
|
+
return { noteBlob, commitSha };
|
|
296
|
+
})
|
|
297
|
+
.filter((entry) => Boolean(entry.noteBlob && entry.commitSha));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function readBlobsBatch(blobShas, cwd) {
|
|
301
|
+
if (!blobShas || blobShas.length === 0) {
|
|
302
|
+
return new Map();
|
|
303
|
+
}
|
|
304
|
+
const input = `${blobShas.join("\n")}\n`;
|
|
305
|
+
const result = spawnSync("git", ["cat-file", "--batch"], {
|
|
306
|
+
cwd,
|
|
307
|
+
input: Buffer.from(input, "utf8"),
|
|
308
|
+
maxBuffer: 128 * 1024 * 1024,
|
|
309
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
310
|
+
});
|
|
311
|
+
if (result.error || result.status !== 0 || !Buffer.isBuffer(result.stdout)) {
|
|
312
|
+
return new Map();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const buf = result.stdout;
|
|
316
|
+
const out = new Map();
|
|
317
|
+
let offset = 0;
|
|
318
|
+
|
|
319
|
+
while (offset < buf.length) {
|
|
320
|
+
const nl = buf.indexOf(0x0a, offset);
|
|
321
|
+
if (nl === -1) break;
|
|
322
|
+
const header = buf.subarray(offset, nl).toString("utf8");
|
|
323
|
+
offset = nl + 1;
|
|
324
|
+
const parts = header.split(" ");
|
|
325
|
+
if (parts.length >= 2 && parts[1] === "missing") {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (parts.length < 3) break;
|
|
329
|
+
const sha = parts[0];
|
|
330
|
+
const size = Number(parts[2]);
|
|
331
|
+
if (!Number.isFinite(size) || size < 0) break;
|
|
332
|
+
const content = buf.subarray(offset, offset + size).toString("utf8");
|
|
333
|
+
out.set(sha, content);
|
|
334
|
+
offset += size + 1;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function copyCommitNotes(fromSha, toSha, cwd, notesRef = DEFAULT_NOTES_REF) {
|
|
341
|
+
const normalized = normalizeNotesRef(notesRef);
|
|
342
|
+
if (!fromSha || !toSha || fromSha === toSha) {
|
|
343
|
+
return { copied: false, reason: "same-or-empty-sha", count: 0 };
|
|
344
|
+
}
|
|
345
|
+
const records = readRecordsFromCommit(fromSha, cwd, normalized);
|
|
346
|
+
if (records.length === 0) {
|
|
347
|
+
return { copied: false, reason: "no-source-notes", count: 0 };
|
|
348
|
+
}
|
|
349
|
+
const result = upsertRecordsOnCommit(toSha, records, cwd, normalized);
|
|
350
|
+
return { copied: true, count: records.length, commitSha: toSha, blobSha: result.blobSha };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function inspectNotesGitConfig(cwd, config, remoteOverride = "") {
|
|
354
|
+
const notesRef = resolveNotesRef(config);
|
|
355
|
+
const remote = remoteOverride || config?.remote || "origin";
|
|
356
|
+
const trackingRef = remoteTrackingNotesRef(remote, notesRef);
|
|
357
|
+
const expectedFetchSpec = `+${notesRef}:${trackingRef}`;
|
|
358
|
+
const hasRemote = remoteExists(remote, cwd);
|
|
359
|
+
|
|
360
|
+
const fetchSpecs = hasRemote
|
|
361
|
+
? runGitRaw(["config", "--get-all", `remote.${remote}.fetch`], cwd)
|
|
362
|
+
.stdout.split(/\r?\n/)
|
|
363
|
+
.map((line) => line.trim())
|
|
364
|
+
.filter(Boolean)
|
|
365
|
+
: [];
|
|
366
|
+
const rewriteRefs = runGitRaw(["config", "--get-all", "notes.rewriteRef"], cwd)
|
|
367
|
+
.stdout.split(/\r?\n/)
|
|
368
|
+
.map((line) => line.trim())
|
|
369
|
+
.filter(Boolean);
|
|
370
|
+
const rewriteAmend = runGitRaw(["config", "--get", "notes.rewrite.amend"], cwd).stdout.trim();
|
|
371
|
+
const rewriteRebase = runGitRaw(["config", "--get", "notes.rewrite.rebase"], cwd).stdout.trim();
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
notesRef,
|
|
375
|
+
remote,
|
|
376
|
+
trackingRef,
|
|
377
|
+
expectedFetchSpec,
|
|
378
|
+
hasRemote,
|
|
379
|
+
hasFetchRefspec: fetchSpecs.includes(expectedFetchSpec),
|
|
380
|
+
hasRewriteRef: rewriteRefs.includes(notesRef),
|
|
381
|
+
rewriteAmend: rewriteAmend === "true",
|
|
382
|
+
rewriteRebase: rewriteRebase === "true",
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function configureNotesGitConfig(cwd, config, options = {}) {
|
|
387
|
+
const state = inspectNotesGitConfig(cwd, config, options.remote);
|
|
388
|
+
const dryRun = Boolean(options.dryRun);
|
|
389
|
+
const applied = [];
|
|
390
|
+
|
|
391
|
+
const applyConfig = (args, label) => {
|
|
392
|
+
applied.push(label);
|
|
393
|
+
if (!dryRun) {
|
|
394
|
+
const res = runGitRaw(["config", ...args], cwd);
|
|
395
|
+
if (res.status !== 0) {
|
|
396
|
+
throw new Error(`git config ${args.join(" ")} failed${res.stderr ? `:\n${res.stderr}` : "."}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
if (state.hasRemote && !state.hasFetchRefspec) {
|
|
402
|
+
applyConfig(["--add", `remote.${state.remote}.fetch`, state.expectedFetchSpec], `remote.${state.remote}.fetch`);
|
|
403
|
+
}
|
|
404
|
+
if (!state.hasRewriteRef) {
|
|
405
|
+
applyConfig(["--add", "notes.rewriteRef", state.notesRef], "notes.rewriteRef");
|
|
406
|
+
}
|
|
407
|
+
if (!state.rewriteAmend) {
|
|
408
|
+
applyConfig(["notes.rewrite.amend", "true"], "notes.rewrite.amend");
|
|
409
|
+
}
|
|
410
|
+
if (!state.rewriteRebase) {
|
|
411
|
+
applyConfig(["notes.rewrite.rebase", "true"], "notes.rewrite.rebase");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return { ...state, applied };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function mergeNotesRefs(cwd, localRef, remoteRef) {
|
|
418
|
+
const normalizedLocal = normalizeNotesRef(localRef);
|
|
419
|
+
const normalizedRemote = normalizeNotesRef(remoteRef);
|
|
420
|
+
|
|
421
|
+
if (!refExists(normalizedRemote, cwd)) {
|
|
422
|
+
return { status: "no-remote-ref", conflictedCommits: 0 };
|
|
423
|
+
}
|
|
424
|
+
if (!refExists(normalizedLocal, cwd)) {
|
|
425
|
+
const res = runGitRaw(["update-ref", normalizedLocal, normalizedRemote], cwd);
|
|
426
|
+
if (res.status !== 0) {
|
|
427
|
+
throw new Error(`Failed to initialize ${normalizedLocal} from ${normalizedRemote}: ${res.stderr}`);
|
|
428
|
+
}
|
|
429
|
+
return { status: "fast-forwarded", conflictedCommits: 0 };
|
|
430
|
+
}
|
|
431
|
+
if (isAncestor(normalizedRemote, normalizedLocal, cwd)) {
|
|
432
|
+
return { status: "up-to-date", conflictedCommits: 0 };
|
|
433
|
+
}
|
|
434
|
+
if (isAncestor(normalizedLocal, normalizedRemote, cwd)) {
|
|
435
|
+
const res = runGitRaw(["update-ref", normalizedLocal, normalizedRemote], cwd);
|
|
436
|
+
if (res.status !== 0) {
|
|
437
|
+
throw new Error(`Failed to fast-forward ${normalizedLocal}: ${res.stderr}`);
|
|
438
|
+
}
|
|
439
|
+
return { status: "fast-forwarded", conflictedCommits: 0 };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const oursEntries = listNoteEntries(cwd, normalizedLocal);
|
|
443
|
+
const theirsEntries = listNoteEntries(cwd, normalizedRemote);
|
|
444
|
+
const oursMap = new Map(oursEntries.map((entry) => [entry.commitSha, entry.noteBlob]));
|
|
445
|
+
const conflicted = [];
|
|
446
|
+
|
|
447
|
+
for (const entry of theirsEntries) {
|
|
448
|
+
const oursBlob = oursMap.get(entry.commitSha);
|
|
449
|
+
if (oursBlob && oursBlob !== entry.noteBlob) {
|
|
450
|
+
conflicted.push({ commitSha: entry.commitSha, oursBlob, theirsBlob: entry.noteBlob });
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
let resolvedPayloads = [];
|
|
455
|
+
if (conflicted.length > 0) {
|
|
456
|
+
const blobMap = readBlobsBatch(
|
|
457
|
+
conflicted.flatMap((item) => [item.oursBlob, item.theirsBlob]),
|
|
458
|
+
cwd,
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
resolvedPayloads = conflicted.map((item) => {
|
|
462
|
+
const oursRecords = parseFramedRecords(blobMap.get(item.oursBlob) ?? "", item.commitSha.slice(0, 12));
|
|
463
|
+
const theirsRecords = parseFramedRecords(blobMap.get(item.theirsBlob) ?? "", item.commitSha.slice(0, 12));
|
|
464
|
+
const mergedRecords = mergeRecordLists(oursRecords, theirsRecords);
|
|
465
|
+
return {
|
|
466
|
+
commitSha: item.commitSha,
|
|
467
|
+
content: serializeFramedRecords(mergedRecords),
|
|
468
|
+
};
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const oursMergeRes = runGitRaw(["notes", `--ref=${normalizedLocal}`, "merge", "-s", "ours", normalizedRemote], cwd);
|
|
473
|
+
if (oursMergeRes.status !== 0) {
|
|
474
|
+
throw new Error(`Failed to merge ${normalizedRemote} into ${normalizedLocal}: ${oursMergeRes.stderr}`);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
for (const item of resolvedPayloads) {
|
|
478
|
+
writeNoteRaw(item.commitSha, item.content, cwd, normalizedLocal);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
status: resolvedPayloads.length > 0 ? "merged-framed" : "merged",
|
|
483
|
+
conflictedCommits: resolvedPayloads.length,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function syncNotesWithRemote(cwd, config, options = {}) {
|
|
488
|
+
const notesRef = normalizeNotesRef(options.notesRef || resolveNotesRef(config));
|
|
489
|
+
const remote = options.remote || config?.remote || "origin";
|
|
490
|
+
const trackingRef = remoteTrackingNotesRef(remote, notesRef);
|
|
491
|
+
const push = options.push !== false;
|
|
492
|
+
const dryRun = Boolean(options.dryRun);
|
|
493
|
+
const bestEffort = Boolean(options.bestEffort);
|
|
494
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
495
|
+
|
|
496
|
+
if (!remoteExists(remote, cwd)) {
|
|
497
|
+
return { status: "skipped", reason: "no-remote", notesRef, remote, pushed: false };
|
|
498
|
+
}
|
|
499
|
+
if (dryRun) {
|
|
500
|
+
return { status: "dry-run", notesRef, remote, trackingRef, pushed: false };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
let lastMerge = { status: "up-to-date", conflictedCommits: 0 };
|
|
504
|
+
let lastError = "";
|
|
505
|
+
|
|
506
|
+
for (let attempt = 1; attempt <= maxRetries; attempt += 1) {
|
|
507
|
+
const fetchRes = runGitRaw(["fetch", "--no-tags", remote, `+${notesRef}:${trackingRef}`], cwd);
|
|
508
|
+
if (fetchRes.status === 0) {
|
|
509
|
+
lastMerge = mergeNotesRefs(cwd, notesRef, trackingRef);
|
|
510
|
+
} else if (!/couldn't find remote ref|does not match any|no such ref/i.test(fetchRes.stderr)) {
|
|
511
|
+
lastError = fetchRes.stderr || `git fetch ${remote} ${notesRef} failed`;
|
|
512
|
+
if (bestEffort) {
|
|
513
|
+
return { status: "warning", warning: lastError, notesRef, remote, pushed: false };
|
|
514
|
+
}
|
|
515
|
+
throw new Error(`Failed to fetch ${notesRef} from ${remote}:\n${lastError}`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (!push) {
|
|
519
|
+
return { status: lastMerge.status, notesRef, remote, pushed: false, conflictedCommits: lastMerge.conflictedCommits };
|
|
520
|
+
}
|
|
521
|
+
if (!refExists(notesRef, cwd)) {
|
|
522
|
+
return { status: "empty", notesRef, remote, pushed: false };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const pushRes = runGitRaw(["push", remote, `${notesRef}:${notesRef}`], cwd);
|
|
526
|
+
if (pushRes.status === 0) {
|
|
527
|
+
return {
|
|
528
|
+
status: "synced",
|
|
529
|
+
mergeStatus: lastMerge.status,
|
|
530
|
+
conflictedCommits: lastMerge.conflictedCommits,
|
|
531
|
+
notesRef,
|
|
532
|
+
remote,
|
|
533
|
+
pushed: true,
|
|
534
|
+
attempts: attempt,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
lastError = pushRes.stderr || `git push ${remote} ${notesRef} failed`;
|
|
539
|
+
const isNonFastForward = /non-fast-forward|fetch first|rejected/i.test(lastError);
|
|
540
|
+
if (!isNonFastForward || attempt === maxRetries) {
|
|
541
|
+
if (bestEffort) {
|
|
542
|
+
return { status: "warning", warning: lastError, notesRef, remote, pushed: false };
|
|
543
|
+
}
|
|
544
|
+
throw new Error(`Failed to push ${notesRef} to ${remote}:\n${lastError}`);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return { status: "warning", warning: lastError, notesRef, remote, pushed: false };
|
|
549
|
+
}
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
### Feature Planning Workflow
|
|
17
17
|
|
|
18
18
|
- For substantial features, cross-module changes, long-running tasks, or multi-phase initiatives, read and follow the `featflow` skill (installed globally via `featflow skills install` or in `.agents/skills/featflow/SKILL.md`).
|
|
19
|
-
- Draft complete `Feature Brief` and `Plan Details` (including `Pre-Implementation Approval`) via `featflow new <short-kebab-title>` (or `featflow new <short-kebab-title> --local` when feature records are not tracked in Git) before editing product code, and verify with `featflow plan-check`.
|
|
20
|
-
-
|
|
19
|
+
- Draft complete `Feature Brief` and `Plan Details` (including `Pre-Implementation Approval`) via `featflow new <short-kebab-title>` (or `featflow new <short-kebab-title> --local` when feature records are not tracked in Git; repos with `"featureStorage": "notes"` default to local active records and auto-archive to `refs/notes/featflow` on `featflow finish`) before editing product code, and verify with `featflow plan-check`.
|
|
20
|
+
- Query past feature plans, decisions, and handoffs across both `refs/notes/featflow` and legacy `features/done/*.md` via `featflow history <path...>`, `featflow search <query>`, and `featflow show <id|path|sha>`.
|
|
21
|
+
- Move completed records to `done/` (in `files` storage mode) before committing and running `featflow finish <branch>`, or let `featflow finish <branch>` auto-archive local active records into Git Notes (in `notes` storage mode).
|
|
21
22
|
- Tiny one-off edits do not require a feature record, but still follow the Universal Pre-Implementation Gate and dedicated task worktree flow.
|