open-memex 0.3.0 → 0.4.0-alpha.1

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/src/review.ts ADDED
@@ -0,0 +1,504 @@
1
+ /**
2
+ * 2B/D26: propose → submit → promote → resolve workflow.
3
+ *
4
+ * - `propose <id> --to project`: copy a personal memory into the project
5
+ * outbox (appdata) as a draft. Never moves — the personal original stays
6
+ * put. Outbox drafts are git-invisible until submitted.
7
+ * - `submit <ids>`: move outbox drafts into a git branch (new `mem/sync-*`
8
+ * by default), as `proposed`, commit locally, print push + PR commands.
9
+ * Push/PR need the user's explicit approval — the tool never pushes.
10
+ * - `promote <id>`: advance a project memory one step up the review ladder
11
+ * (proposed → approved → published), or --reject it with a note.
12
+ * - `resolve [id]`: assist with git merge conflicts inside the in-repo
13
+ * memory dir. Frontmatter gets a field-level 3-way merge; semantic
14
+ * conflicts are reported, never auto-resolved.
15
+ *
16
+ * Git automation line (D25 as revised by D26): local git only (submit's
17
+ * branch + commit). Nothing leaves the machine without approval.
18
+ */
19
+
20
+ import { execFileSync } from "node:child_process";
21
+ import * as fs from "node:fs";
22
+ import * as os from "node:os";
23
+ import * as path from "node:path";
24
+ import { loadConfig } from "./config.ts";
25
+ import { inRepoMemoriesDirPath, projectRoot } from "./paths.ts";
26
+ import { resolveProjectScope } from "./scope.ts";
27
+ import {
28
+ parse as parseMemory,
29
+ serialize,
30
+ ulid,
31
+ writeMemoryFile,
32
+ type Frontmatter,
33
+ type ReviewState,
34
+ type ReviewTransition,
35
+ } from "./store/markdown.ts";
36
+ import { findMemoryFile } from "./store/lifecycle.ts";
37
+ import { upsertFromFile } from "./store/sync.ts";
38
+
39
+ /** Best-effort author identity: git user.name, else the OS user. */
40
+ export function currentAuthor(): string {
41
+ try {
42
+ const name = execFileSync("git", ["config", "user.name"], {
43
+ encoding: "utf8",
44
+ stdio: ["ignore", "pipe", "ignore"],
45
+ }).trim();
46
+ if (name) return name;
47
+ } catch {
48
+ /* git unavailable — fall through */
49
+ }
50
+ try {
51
+ return os.userInfo().username || "unknown";
52
+ } catch {
53
+ return "unknown";
54
+ }
55
+ }
56
+
57
+ function fail(msg: string): never {
58
+ throw new Error(`[open-memex] ${msg}`);
59
+ }
60
+
61
+ // ---------------------------------------------------------------------------
62
+ // propose
63
+ // ---------------------------------------------------------------------------
64
+
65
+ export interface ProposeResult {
66
+ id: string;
67
+ filePath: string;
68
+ reviewState: ReviewState;
69
+ }
70
+
71
+ /**
72
+ * Copy one personal memory into the project scope as a review candidate.
73
+ * The source memory is untouched (copy, not move — D25).
74
+ * Batch-friendly: validates nothing, copies one — use proposeMemories()
75
+ * for the all-or-nothing multi-id path.
76
+ */
77
+ export function proposeMemory(
78
+ sourceId: string,
79
+ opts: { localApprove?: boolean } = {},
80
+ ): ProposeResult {
81
+ const src = findMemoryFile(sourceId);
82
+ if (!src) fail(`no memory found with id "${sourceId}".`);
83
+ return copyPersonalToProject(sourceId, src.fm, src.body, opts);
84
+ }
85
+
86
+ function copyPersonalToProject(
87
+ sourceId: string,
88
+ srcFm: Frontmatter,
89
+ srcBody: string,
90
+ opts: { localApprove?: boolean } = {},
91
+ ): ProposeResult {
92
+ if (srcFm.scope === "project")
93
+ fail(`memory "${sourceId}" is already in project scope — nothing to propose.`);
94
+ if (srcFm.scope === "org")
95
+ fail(`memory "${sourceId}" is already shared at org scope.`);
96
+ if (srcFm.status === "retracted" || srcFm.status === "archived")
97
+ fail(`memory "${sourceId}" is ${srcFm.status} — cannot propose it.`);
98
+
99
+ const project = resolveProjectScope(projectRoot());
100
+ const author = currentAuthor();
101
+ const now = new Date().toISOString();
102
+ // propose lands in the outbox as a draft (submit moves it to proposed);
103
+ // --local-approve is the solo-dev shortcut straight to approved.
104
+ const targetState: ReviewState = opts.localApprove ? "approved" : "draft";
105
+ const fm: Frontmatter = {
106
+ ...srcFm,
107
+ id: ulid(),
108
+ scope: "project",
109
+ scope_key: project.key,
110
+ project_name: project.projectName,
111
+ created_at: now,
112
+ updated_at: now,
113
+ supersedes: null,
114
+ superseded_by: null,
115
+ review_state: targetState,
116
+ proposed_by: author,
117
+ approved_by: opts.localApprove ? author : null,
118
+ derived_from: srcFm.id,
119
+ review_history: [
120
+ {
121
+ at: now,
122
+ by: author,
123
+ from: "draft",
124
+ to: targetState,
125
+ note: opts.localApprove
126
+ ? `proposed from personal memory ${srcFm.id} (local-approved)`
127
+ : `proposed from personal memory ${srcFm.id}`,
128
+ },
129
+ ],
130
+ };
131
+ const { filePath } = writeMemoryFile(fm, srcBody);
132
+ upsertFromFile({ fm, body: srcBody, filePath, mtimeMs: Date.now() });
133
+ return { id: fm.id, filePath, reviewState: fm.review_state };
134
+ }
135
+
136
+ /**
137
+ * Propose several personal memories at once — one branch, one PR.
138
+ * All-or-nothing: every id is validated before anything is copied, so a bad
139
+ * id never leaves a half-proposed batch behind.
140
+ */
141
+ export function proposeMemories(
142
+ sourceIds: string[],
143
+ opts: { localApprove?: boolean } = {},
144
+ ): { sourceId: string; result: ProposeResult }[] {
145
+ if (sourceIds.length === 0) fail("propose needs at least one memory id.");
146
+ const seen = new Set<string>();
147
+ const sources = sourceIds.map((sourceId) => {
148
+ if (seen.has(sourceId)) fail(`duplicate id "${sourceId}" — list each memory once.`);
149
+ seen.add(sourceId);
150
+ const src = findMemoryFile(sourceId);
151
+ if (!src) fail(`no memory found with id "${sourceId}".`);
152
+ if (src.fm.scope !== "personal")
153
+ fail(`only personal memories can be proposed (memory "${sourceId}" is ${src.fm.scope}).`);
154
+ if (src.fm.status === "retracted" || src.fm.status === "archived")
155
+ fail(`memory "${sourceId}" is ${src.fm.status} — cannot propose it.`);
156
+ return { sourceId, fm: src.fm, body: src.body };
157
+ });
158
+ return sources.map(({ sourceId, fm, body }) => ({
159
+ sourceId,
160
+ result: copyPersonalToProject(sourceId, fm, body, opts),
161
+ }));
162
+ }
163
+
164
+ // ---------------------------------------------------------------------------
165
+ // promote
166
+ // ---------------------------------------------------------------------------
167
+
168
+ export interface PromoteResult {
169
+ id: string;
170
+ from: ReviewState;
171
+ to: ReviewState;
172
+ /** Full audit trail after this transition (D29). */
173
+ history: ReviewTransition[];
174
+ }
175
+
176
+ const NEXT_STATE: Partial<Record<ReviewState, ReviewState>> = {
177
+ proposed: "approved",
178
+ approved: "published",
179
+ };
180
+
181
+ /**
182
+ * Advance a project memory one step up the review ladder
183
+ * (proposed → approved → published), reject it with --reject, or send a
184
+ * rejected memory back for another round with --resubmit.
185
+ *
186
+ * A rejection never deletes anything: the file stays on the author's branch.
187
+ * What happens next is the human's call — accept it (close the PR, delete the
188
+ * branch), revise + --resubmit, or keep the rejected file as a record.
189
+ */
190
+ export function promoteMemory(
191
+ id: string,
192
+ opts: { reject?: boolean; resubmit?: boolean; note?: string; by?: string } = {},
193
+ ): PromoteResult {
194
+ const mf = findMemoryFile(id);
195
+ if (!mf) fail(`no memory found with id "${id}".`);
196
+ if (mf.fm.scope !== "project")
197
+ fail(`only project-scope memories go through review (memory "${id}" is ${mf.fm.scope}).`);
198
+
199
+ const from = mf.fm.review_state ?? "draft";
200
+ const by = opts.by?.trim() || currentAuthor();
201
+ let to: ReviewState;
202
+ if (opts.resubmit) {
203
+ if (opts.reject) fail("choose one: --resubmit or --reject, not both.");
204
+ if (from !== "rejected")
205
+ fail(`only a "rejected" memory can be resubmitted (memory "${id}" is "${from}").`);
206
+ to = "proposed";
207
+ } else if (opts.reject) {
208
+ if (from !== "proposed" && from !== "approved")
209
+ fail(`cannot reject from "${from}" — only "proposed" or "approved" can be rejected.`);
210
+ to = "rejected";
211
+ } else {
212
+ const next = NEXT_STATE[from];
213
+ if (!next)
214
+ fail(
215
+ from === "draft"
216
+ ? `memory "${id}" is still a draft — run "open-memex propose ${id} --to project" first.`
217
+ : `memory "${id}" is "${from}" — a terminal state, nothing to promote.`,
218
+ );
219
+ to = next;
220
+ }
221
+
222
+ const fm: Frontmatter = {
223
+ ...mf.fm,
224
+ review_state: to,
225
+ updated_at: new Date().toISOString(),
226
+ // A rejection withdraws any earlier approval; a resubmission clears it too
227
+ // (the memory must earn approval again).
228
+ approved_by: to === "approved" ? by : to === "rejected" || to === "proposed" ? null : mf.fm.approved_by,
229
+ review_note: opts.note?.trim() ? opts.note.trim() : mf.fm.review_note,
230
+ // D29: every transition is appended to the audit trail — the note is
231
+ // preserved here even when review_note later gets overwritten.
232
+ review_history: [
233
+ ...(mf.fm.review_history ?? []),
234
+ {
235
+ at: new Date().toISOString(),
236
+ by,
237
+ from,
238
+ to,
239
+ note: opts.note?.trim() ? opts.note.trim() : null,
240
+ },
241
+ ],
242
+ };
243
+ fs.writeFileSync(mf.filePath, serialize(fm, mf.body), "utf8");
244
+ upsertFromFile({ fm, body: mf.body, filePath: mf.filePath, mtimeMs: Date.now() });
245
+ return { id, from, to, history: fm.review_history };
246
+ }
247
+
248
+ /** Render a memory's audit trail as compact human-readable lines (D29). */
249
+ export function formatReviewHistory(history: ReviewTransition[]): string[] {
250
+ return history.map(
251
+ (t) =>
252
+ ` ${t.at.slice(0, 10)} ${t.by}: ${t.from} → ${t.to}` +
253
+ (t.note ? ` — ${t.note}` : ""),
254
+ );
255
+ }
256
+
257
+ // ---------------------------------------------------------------------------
258
+ // resolve
259
+ // ---------------------------------------------------------------------------
260
+
261
+ export interface ConflictEntry {
262
+ id: string;
263
+ filePath: string;
264
+ }
265
+
266
+ function git(args: string[], cwd: string): string {
267
+ return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
268
+ }
269
+
270
+ function inRepoDir(): { root: string; dir: string } {
271
+ const root = projectRoot();
272
+ try {
273
+ git(["rev-parse", "--git-dir"], root);
274
+ } catch {
275
+ fail("resolve needs a git repo — this directory is not one.");
276
+ }
277
+ return { root, dir: inRepoMemoriesDirPath(root, loadConfig().memoryDir) };
278
+ }
279
+
280
+ /** List in-repo memory files currently in an unmerged (conflicted) state. */
281
+ export function listConflicts(): ConflictEntry[] {
282
+ const { root, dir } = inRepoDir();
283
+ let out: string;
284
+ try {
285
+ out = git(["diff", "--name-only", "--diff-filter=U", "--", dir], root);
286
+ } catch {
287
+ fail("git is unavailable or the repo state cannot be read.");
288
+ }
289
+ return out
290
+ .split("\n")
291
+ .map((s) => s.trim())
292
+ .filter((s) => s && s.endsWith(".md"))
293
+ .map((rel) => ({
294
+ id: path.basename(rel, ".md"),
295
+ filePath: path.join(root, rel),
296
+ }));
297
+ }
298
+
299
+ export interface FieldConflict {
300
+ field: string;
301
+ base: string;
302
+ ours: string;
303
+ theirs: string;
304
+ }
305
+
306
+ export type ResolveOutcome =
307
+ | { ok: true; filePath: string; autoMerged: string[] }
308
+ | { ok: false; filePath: string; conflicts: FieldConflict[] };
309
+
310
+ function show(v: unknown): string {
311
+ if (v === null || v === undefined) return "(empty)";
312
+ if (Array.isArray(v)) return v.length ? v.join(", ") : "(empty)";
313
+ return String(v);
314
+ }
315
+
316
+ /**
317
+ * Attempt a field-level 3-way merge of a conflicted memory file.
318
+ * Never auto-resolves semantic conflicts (same field changed differently
319
+ * on both sides) — those are reported for the human to decide.
320
+ */
321
+ export function resolveConflict(target: string): ResolveOutcome {
322
+ const { root, dir } = inRepoDir();
323
+
324
+ // Locate the file: by memory id, or as a direct path. Note findMemoryFile
325
+ // returns null for a file with conflict markers (YAML won't parse), so
326
+ // fall back to a direct <id>.md lookup inside the in-repo dir.
327
+ let filePath: string | null = null;
328
+ const byId = findMemoryFile(target);
329
+ if (byId) {
330
+ filePath = byId.filePath;
331
+ } else {
332
+ const base = path.basename(target).replace(/\.md$/, "");
333
+ const candidates = [
334
+ path.join(dir, `${base}.md`),
335
+ path.isAbsolute(target) ? target : path.join(root, target),
336
+ path.isAbsolute(target) ? `${target}.md` : path.join(root, `${target}.md`),
337
+ ];
338
+ for (const c of candidates) {
339
+ if (fs.existsSync(c) && fs.statSync(c).isFile()) {
340
+ filePath = c;
341
+ break;
342
+ }
343
+ }
344
+ }
345
+ if (!filePath) fail(`no memory or file found for "${target}".`);
346
+
347
+ // Must actually be in conflict (stages 1/2/3 present).
348
+ const stages = git(["ls-files", "-u", "--", filePath], root)
349
+ .split("\n")
350
+ .map((l) => l.trim())
351
+ .filter(Boolean);
352
+ if (stages.length === 0)
353
+ fail(`"${path.basename(filePath)}" is not in a merge-conflict state.`);
354
+
355
+ const blob = (stage: string): string => {
356
+ // Git wants forward slashes even on Windows.
357
+ const rel = path.relative(root, filePath).split(path.sep).join("/");
358
+ try {
359
+ return git(["show", `:${stage}:${rel}`], root);
360
+ } catch {
361
+ fail(`cannot read git stage ${stage} for "${path.basename(filePath)}".`);
362
+ throw new Error("unreachable");
363
+ }
364
+ };
365
+ const base = parseMemory(blob("1"));
366
+ const ours = parseMemory(blob("2"));
367
+ const theirs = parseMemory(blob("3"));
368
+ if (!base || !ours || !theirs)
369
+ fail(`one of the conflict stages for "${path.basename(filePath)}" is not a valid memory file.`);
370
+
371
+ const b = base.fm;
372
+ const o = ours.fm;
373
+ const t = theirs.fm;
374
+ const conflicts: FieldConflict[] = [];
375
+ const autoMerged: string[] = [];
376
+ const merged: Record<string, unknown> = {};
377
+
378
+ // Identity fields: both sides must agree, else this is not the same memory.
379
+ for (const f of ["id", "scope_key", "scope", "project_name", "schema_version"] as const) {
380
+ if (o[f] !== t[f]) {
381
+ conflicts.push({ field: f, base: show(b[f]), ours: show(o[f]), theirs: show(t[f]) });
382
+ } else {
383
+ merged[f] = o[f];
384
+ }
385
+ }
386
+
387
+ // Plain scalar fields: standard 3-way.
388
+ const scalars = [
389
+ "visibility",
390
+ "type",
391
+ "role",
392
+ "importance",
393
+ "status",
394
+ "source",
395
+ "created_at",
396
+ "proposed_by",
397
+ "approved_by",
398
+ "derived_from",
399
+ "review_note",
400
+ "supersedes",
401
+ "superseded_by",
402
+ ] as const;
403
+ for (const f of scalars) {
404
+ const bv = b[f] as unknown;
405
+ const ov = o[f] as unknown;
406
+ const tv = t[f] as unknown;
407
+ if (eq(ov, tv)) {
408
+ merged[f] = ov;
409
+ } else if (eq(bv, ov)) {
410
+ merged[f] = tv;
411
+ autoMerged.push(f);
412
+ } else if (eq(bv, tv)) {
413
+ merged[f] = ov;
414
+ autoMerged.push(f);
415
+ } else {
416
+ conflicts.push({ field: f, base: show(bv), ours: show(ov), theirs: show(tv) });
417
+ }
418
+ }
419
+
420
+ // review_state: 3-way, but never silently drop a rejection.
421
+ {
422
+ const bv = b.review_state;
423
+ const ov = o.review_state;
424
+ const tv = t.review_state;
425
+ if (ov === tv) merged.review_state = ov;
426
+ else if (bv === ov) merged.review_state = tv;
427
+ else if (bv === tv) merged.review_state = ov;
428
+ else if (ov === "rejected" || tv === "rejected")
429
+ conflicts.push({ field: "review_state", base: bv, ours: ov, theirs: tv });
430
+ else {
431
+ // Both advanced along the ladder; keep the further one.
432
+ const order: ReviewState[] = ["draft", "proposed", "approved", "published"];
433
+ merged.review_state = order.indexOf(tv) > order.indexOf(ov) ? tv : ov;
434
+ }
435
+ if (!conflicts.some((c) => c.field === "review_state") && merged.review_state !== bv)
436
+ autoMerged.push("review_state");
437
+ }
438
+
439
+ // tags: union when both sides changed.
440
+ {
441
+ const bs = new Set(b.tags ?? []);
442
+ const os = new Set(o.tags ?? []);
443
+ const ts = new Set(t.tags ?? []);
444
+ if (eq([...os].sort(), [...ts].sort())) merged.tags = [...os];
445
+ else {
446
+ const union = [...new Set([...os, ...ts])].sort();
447
+ merged.tags = union;
448
+ if (!eq([...bs].sort(), union)) autoMerged.push("tags");
449
+ }
450
+ }
451
+
452
+ // review_history (D29 audit trail): union by identity — a transition that
453
+ // happened on either side is kept; identical entries collapse.
454
+ {
455
+ const key = (e: ReviewTransition) => `${e.at}|${e.by}|${e.from}|${e.to}`;
456
+ const seen = new Set<string>();
457
+ const union: ReviewTransition[] = [];
458
+ const all = [...(b.review_history ?? []), ...(o.review_history ?? []), ...(t.review_history ?? [])];
459
+ for (const e of all) {
460
+ const k = key(e);
461
+ if (!seen.has(k)) {
462
+ seen.add(k);
463
+ union.push(e);
464
+ }
465
+ }
466
+ merged.review_history = union;
467
+ if (union.length !== (b.review_history ?? []).length) autoMerged.push("review_history");
468
+ }
469
+
470
+ // updated_at: always moves forward — take the latest.
471
+ {
472
+ const latest = [b.updated_at, o.updated_at, t.updated_at].sort().at(-1)!;
473
+ merged.updated_at = latest;
474
+ if (latest !== b.updated_at) autoMerged.push("updated_at");
475
+ }
476
+
477
+ // body: 3-way text merge; divergent edits are a human decision.
478
+ const bb = base.body.trimEnd();
479
+ const ob = ours.body.trimEnd();
480
+ const tb = theirs.body.trimEnd();
481
+ let mergedBody: string;
482
+ if (ob === tb) mergedBody = ob;
483
+ else if (bb === ob) {
484
+ mergedBody = tb;
485
+ autoMerged.push("body");
486
+ } else if (bb === tb) {
487
+ mergedBody = ob;
488
+ autoMerged.push("body");
489
+ } else {
490
+ conflicts.push({ field: "body", base: bb.slice(0, 120), ours: ob.slice(0, 120), theirs: tb.slice(0, 120) });
491
+ mergedBody = bb; // unused when conflicts exist
492
+ }
493
+
494
+ if (conflicts.length > 0) return { ok: false, filePath, conflicts };
495
+
496
+ const fm = merged as unknown as Frontmatter;
497
+ fs.writeFileSync(filePath, serialize(fm, mergedBody), "utf8");
498
+ upsertFromFile({ fm, body: mergedBody, filePath, mtimeMs: Date.now() });
499
+ return { ok: true, filePath, autoMerged: [...new Set(autoMerged)] };
500
+ }
501
+
502
+ function eq(a: unknown, b: unknown): boolean {
503
+ return JSON.stringify(a) === JSON.stringify(b);
504
+ }
package/src/store/db.ts CHANGED
@@ -43,7 +43,8 @@ CREATE TABLE IF NOT EXISTS memories (
43
43
  file_path TEXT NOT NULL,
44
44
  mtime_ms REAL NOT NULL,
45
45
  created_at INTEGER NOT NULL,
46
- updated_at INTEGER NOT NULL
46
+ updated_at INTEGER NOT NULL,
47
+ review_state TEXT NOT NULL DEFAULT 'draft'
47
48
  );
48
49
 
49
50
  CREATE INDEX IF NOT EXISTS idx_memories_scope_updated
@@ -88,7 +89,7 @@ END;
88
89
  `;
89
90
 
90
91
  /** Current index schema version. Bump when TABLE_SCHEMA/FTS_SCHEMA change. */
91
- const SCHEMA_VERSION = 5;
92
+ const SCHEMA_VERSION = 6;
92
93
 
93
94
  function userVersion(d: AnyDatabase): number {
94
95
  const row = d.prepare("PRAGMA user_version").get() as { user_version: number };
@@ -2,7 +2,12 @@ import { createHash } from "node:crypto";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
4
  import { db } from "./db.ts";
5
- import { paths, memoriesDirPath } from "../paths.ts";
5
+ import {
6
+ paths,
7
+ projectRoot,
8
+ inRepoMemoriesDirPath,
9
+ } from "../paths.ts";
10
+ import { loadConfig } from "../config.ts";
6
11
  import { cjkIndexText } from "../retrieve/cjk.ts";
7
12
  import {
8
13
  parse,
@@ -97,25 +102,33 @@ export function findDuplicates(
97
102
  return { exact, near: near.slice(0, 3) };
98
103
  }
99
104
 
100
- /** Locate a memory file by id: index first, then a full dir scan fallback. */
105
+ /** Locate a memory file by id: index file_path first, then dir-scan fallback. */
101
106
  export function findMemoryFile(id: string): MemoryFile | null {
102
107
  const { memories } = paths();
103
108
  let filePath: string | null = null;
104
109
  try {
105
110
  const row = db()
106
- .prepare(`SELECT scope_key FROM memories WHERE id = ?`)
107
- .get(id) as { scope_key: string } | undefined;
108
- if (row) {
109
- const p = path.join(memoriesDirPath(row.scope_key), `${id}.md`);
110
- if (fs.existsSync(p)) filePath = p;
111
- }
111
+ .prepare(`SELECT file_path FROM memories WHERE id = ?`)
112
+ .get(id) as { file_path: string } | undefined;
113
+ if (row && fs.existsSync(row.file_path)) filePath = row.file_path;
112
114
  } catch {
113
115
  // index unavailable — fall through to scan
114
116
  }
115
- if (!filePath && fs.existsSync(memories)) {
116
- for (const entry of fs.readdirSync(memories, { withFileTypes: true })) {
117
- if (!entry.isDirectory()) continue;
118
- const p = path.join(memories, entry.name, `${id}.md`);
117
+ if (!filePath) {
118
+ // Fallback scan: appdata scope dirs + the in-repo dir (2B/D24).
119
+ const dirs: string[] = [];
120
+ if (fs.existsSync(memories)) {
121
+ for (const entry of fs.readdirSync(memories, { withFileTypes: true })) {
122
+ if (entry.isDirectory()) dirs.push(path.join(memories, entry.name));
123
+ }
124
+ }
125
+ try {
126
+ dirs.push(inRepoMemoriesDirPath(projectRoot(), loadConfig().memoryDir));
127
+ } catch {
128
+ /* ignore */
129
+ }
130
+ for (const dir of dirs) {
131
+ const p = path.join(dir, `${id}.md`);
119
132
  if (fs.existsSync(p)) {
120
133
  filePath = p;
121
134
  break;
@@ -225,6 +238,11 @@ export function supersede(
225
238
  updated_at: now,
226
239
  supersedes: oldId,
227
240
  superseded_by: null,
241
+ review_state: "draft",
242
+ proposed_by: null,
243
+ approved_by: null,
244
+ derived_from: null,
245
+ review_note: null,
228
246
  };
229
247
  const dir = path.dirname(oldMf.filePath);
230
248
  const newPath = path.join(dir, `${newFm.id}.md`);