@staix/vault-mem 0.5.1 → 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/CHANGELOG.md +68 -0
- package/README.md +147 -23
- package/package.json +5 -4
- package/src/amem/seam.ts +15 -6
- package/src/analysis-commands.ts +1 -1
- package/src/analysis-input.ts +24 -3
- package/src/analysis-reports.ts +1 -1
- package/src/analysis-types.ts +13 -1
- package/src/canon-tables.ts +190 -0
- package/src/cli-output.ts +15 -1
- package/src/cli.ts +3 -1
- package/src/commands-table.ts +7 -2
- package/src/config.ts +31 -3
- package/src/frontmatter.ts +10 -3
- package/src/links.ts +13 -1
- package/src/mcp-spec.ts +19 -11
- package/src/mcp-vault.ts +20 -12
- package/src/mcp.ts +4 -2
- package/src/org-promote.ts +29 -12
- package/src/promote.ts +25 -13
- package/src/read-commands.ts +1 -2
- package/src/read-output.ts +7 -5
- package/src/registry.ts +1 -0
- package/src/skills-mine.ts +5 -4
- package/src/spec-command-table.ts +10 -1
- package/src/spec-commands.ts +7 -3
- package/src/spec-gitlinks.ts +106 -0
- package/src/spec-output.ts +30 -0
- package/src/spec-source.ts +9 -3
- package/src/spec-validate.ts +6 -3
- package/src/spec-write.ts +469 -0
- package/src/ui/inbox.ts +28 -10
- package/src/ui/panel.js +26 -5
- package/src/ui/plugin.ts +38 -12
- package/src/ui/snapshot-cache.ts +123 -0
- package/src/vault-brief.ts +90 -0
- package/src/vault-index.ts +158 -21
- package/src/vault-lint.ts +53 -5
- package/src/vault-log.ts +3 -2
- package/src/vault-stats.ts +110 -5
- package/src/write-commands.ts +4 -3
- package/src/write-files.ts +72 -0
- package/src/write-operations.ts +175 -25
- package/src/write-output.ts +20 -9
- package/src/write-types.ts +8 -1
- package/src/write-validation.ts +46 -17
- package/src/writer.ts +145 -42
- package/src/yaml-quoted.ts +12 -2
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Shared path-based publication and descriptor-owned cleanup. These checks narrow races;
|
|
2
|
+
* Node fs cannot make identity checks and namespace mutations one atomic operation. */
|
|
3
|
+
import { fstatSync, linkSync, lstatSync, realpathSync, renameSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { dirname, isAbsolute, relative, sep } from "node:path";
|
|
5
|
+
|
|
6
|
+
export class PublicationDrift extends Error {
|
|
7
|
+
constructor(readonly file: string, readonly published = false, readonly targetExists = false) {
|
|
8
|
+
super(`publication path identity or containment changed; ${published
|
|
9
|
+
? "this step was published; stop concurrent writers, inspect the registered root, target and listed retained backups, restore the original plain parent directories, and recover the recorded write before another mutation"
|
|
10
|
+
: "this step was not published; stop concurrent writers, inspect the registered root and target, restore the original plain parent directories, and review any earlier applied changes before another mutation"}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Only failures of the native hard-link operation, rather than either path check. */
|
|
14
|
+
export class PublicationLinkFailure extends Error {
|
|
15
|
+
constructor(cause: unknown) { super("hard-link publication failed", { cause }); }
|
|
16
|
+
}
|
|
17
|
+
export interface PublicationPath { root: string; file: string; parents: { path: string; identity: string }[] }
|
|
18
|
+
export type PublicationOperation = { kind: "rename" | "link"; temp: string } | { kind: "unlink" };
|
|
19
|
+
const identity = (s: { dev: bigint; ino: bigint }) => `${s.dev}:${s.ino}`;
|
|
20
|
+
const within = (root: string, file: string) => { const p = relative(root, file); return p === "" || !isAbsolute(p) && p !== ".." && !p.startsWith(`..${sep}`); };
|
|
21
|
+
|
|
22
|
+
export function capturePublicationPath(root: string, file: string): PublicationPath {
|
|
23
|
+
if (!within(root, file)) throw new PublicationDrift(file);
|
|
24
|
+
const parents: PublicationPath["parents"] = [];
|
|
25
|
+
for (let parent = dirname(file); ; parent = dirname(parent)) {
|
|
26
|
+
const stat = lstatSync(parent, { bigint: true });
|
|
27
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || !within(root, realpathSync(parent))) throw new PublicationDrift(file);
|
|
28
|
+
parents.push({ path: parent, identity: identity(stat) });
|
|
29
|
+
if (parent === root) break;
|
|
30
|
+
if (dirname(parent) === parent) throw new PublicationDrift(file);
|
|
31
|
+
}
|
|
32
|
+
return { root, file, parents };
|
|
33
|
+
}
|
|
34
|
+
function checkPublicationPath(path: PublicationPath, deleted: boolean, published = false): void {
|
|
35
|
+
try {
|
|
36
|
+
for (const parent of path.parents) {
|
|
37
|
+
const stat = lstatSync(parent.path, { bigint: true });
|
|
38
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || identity(stat) !== parent.identity || !within(path.root, realpathSync(parent.path))) throw new PublicationDrift(path.file, published);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const stat = lstatSync(path.file);
|
|
42
|
+
if (!stat.isFile() || stat.isSymbolicLink() || !within(path.root, realpathSync(path.file))) throw new PublicationDrift(path.file, published, true);
|
|
43
|
+
} catch (error) { if (!deleted || (error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error instanceof PublicationDrift) throw error;
|
|
46
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") throw new PublicationDrift(path.file, published);
|
|
47
|
+
// A permissions, storage or descriptor failure is not proof that the namespace changed.
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function publishFile(path: PublicationPath, operation: PublicationOperation, published: () => void): void {
|
|
52
|
+
// The target may be absent for create; existing-content guards remain the writer's responsibility.
|
|
53
|
+
checkPublicationPath(path, true);
|
|
54
|
+
if (operation.kind === "rename") renameSync(operation.temp, path.file);
|
|
55
|
+
else if (operation.kind === "link") {
|
|
56
|
+
try { linkSync(operation.temp, path.file); }
|
|
57
|
+
catch (cause) { throw new PublicationLinkFailure(cause); }
|
|
58
|
+
}
|
|
59
|
+
else unlinkSync(path.file);
|
|
60
|
+
// Record persisted state before the post-publication guard can fail.
|
|
61
|
+
published();
|
|
62
|
+
checkPublicationPath(path, operation.kind === "unlink", true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Caller keeps fd open throughout this call and closes it independently, including on failure. */
|
|
66
|
+
export function cleanupOwnedFile(file: string, fd: number): void {
|
|
67
|
+
const owned = fstatSync(fd, { bigint: true }), named = lstatSync(file, { bigint: true });
|
|
68
|
+
if (!owned.isFile() || !named.isFile() || named.isSymbolicLink() || identity(owned) !== identity(named)) {
|
|
69
|
+
throw new Error("owned file identity changed; replacement retained; inspect the temporary or lock path before manual recovery");
|
|
70
|
+
}
|
|
71
|
+
unlinkSync(file);
|
|
72
|
+
}
|
package/src/write-operations.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from "node:util";
|
|
2
|
-
import { normalizeSource } from "./source.ts";
|
|
3
|
-
import { parseFrontmatter, emitFrontmatter } from "./frontmatter.ts";
|
|
4
|
-
import { LINK_RELATIONS, normalizeTarget, proseLines, rewriteLinkTargets, extractLinks, resolveLinks, type LinkNote } from "./links.ts";
|
|
5
|
-
import type
|
|
2
|
+
import { normalizeSource, sourceReferences } from "./source.ts";
|
|
3
|
+
import { parseFrontmatter, emitFrontmatter, frontmatterReferences } from "./frontmatter.ts";
|
|
4
|
+
import { LINK_RELATIONS, normalizeTarget, proseLines, visibleProseLines, rewriteLinkTargets, extractLinks, resolveLinks, type LinkNote } from "./links.ts";
|
|
5
|
+
import { MAX_WRITE_BYTES, type MergeInboundLink, type MergePreview, type PatchEdit, type PlannedChange, type WriteIssue, type WriteOp } from "./write-types.ts";
|
|
6
6
|
export class WriteRefusal extends Error {
|
|
7
7
|
constructor(readonly issues: WriteIssue[]) { super(issues[0]?.message ?? "write refused"); }
|
|
8
8
|
}
|
|
@@ -126,6 +126,173 @@ function mayHaveDoi(source: string): boolean {
|
|
|
126
126
|
const header = source.slice(0, closing.exec(source)?.index ?? source.length);
|
|
127
127
|
return /(?:^|[{\n,])[ \t]*(?:doi|["']doi["'])[ \t]*:|[\\!&*?]/.test(header);
|
|
128
128
|
}
|
|
129
|
+
/** Shared date emission for validated note content, including promotion previews. */
|
|
130
|
+
export function stampNoteContent(source: string, today: string, create: boolean, warning?: (message: string) => void): string {
|
|
131
|
+
const doc = parseFrontmatter(source);
|
|
132
|
+
const dates = { updated: today, ...(create && !Object.hasOwn(doc.data, "created") ? { created: today } : {}) };
|
|
133
|
+
const references = frontmatterReferences(doc);
|
|
134
|
+
const skipStamp = (cause: string, nextStep?: string) => {
|
|
135
|
+
const repair = nextStep ?? "open this note in a text editor and rewrite the header as a block mapping with plain literal values" + (references.length ? ", replacing any aliases with their literal values and removing anchors" : "");
|
|
136
|
+
warning?.(`automatic dates were not set: ${cause}; ${repair}; later authored writes stamp updated automatically${create && !Object.hasOwn(doc.data, "created") ? "; set created explicitly in the repaired header if it was also skipped" : ""}`);
|
|
137
|
+
return source;
|
|
138
|
+
};
|
|
139
|
+
let candidate: string;
|
|
140
|
+
try { candidate = emitFrontmatter(doc, dates); }
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (!(error instanceof Error) || error.message !== "frontmatter: editing requires a block mapping") throw error;
|
|
143
|
+
return skipStamp(references.length
|
|
144
|
+
? "the header cannot be edited in place without rewriting comments, formatting or YAML anchor/alias relationships"
|
|
145
|
+
: "the header cannot be edited in place without rewriting comments or formatting");
|
|
146
|
+
}
|
|
147
|
+
if (Buffer.byteLength(candidate) > MAX_WRITE_BYTES && Buffer.byteLength(source) <= MAX_WRITE_BYTES) {
|
|
148
|
+
return skipStamp("adding automatic dates would exceed the 256 KiB note limit", "shorten this note to leave room below 256 KiB for automatic dates");
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
const stamped = parseFrontmatter(candidate);
|
|
152
|
+
if (!isDeepStrictEqual(references, frontmatterReferences(stamped))) return skipStamp(references.length ? "date emission would change YAML anchor/alias relationships" : "the header could not be rewritten without changing its structure");
|
|
153
|
+
if (!isDeepStrictEqual(stamped.data, { ...doc.data, ...dates })) return skipStamp(references.length ? "date emission would alter metadata or YAML anchor/alias values" : "the header could not be rewritten without changing a value");
|
|
154
|
+
} catch { return skipStamp(references.length ? "the stamped YAML header would not parse after an anchor or alias changed" : "the stamped YAML header could not be parsed"); }
|
|
155
|
+
return candidate;
|
|
156
|
+
}
|
|
157
|
+
/** Date authored content only; mechanical rewrites retain their existing freshness. */
|
|
158
|
+
export function stampContentChanges(operation: WriteOp, changes: PlannedChange[], today: string, issues: WriteIssue[] = []): PlannedChange[] {
|
|
159
|
+
if (["check", "report", "move", "tidy", "sanitize"].includes(operation.op)) return changes;
|
|
160
|
+
return changes.map(change => {
|
|
161
|
+
if (change.after === null || change.after === change.before) return change;
|
|
162
|
+
if (operation.op === "merge" && change.path !== notePath(operation.keep) && change.path !== notePath(operation.absorb)) return change;
|
|
163
|
+
return { ...change, after: stampNoteContent(change.after, today, operation.op === "create", message => issues.push({ code: "V01", severity: "warning", path: change.path, message })) };
|
|
164
|
+
}).filter(change => change.before !== change.after);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Path-only reference identity shared by move and merge; alias-only links remain untouched. */
|
|
168
|
+
function referenceReplacement(notes: Map<string, string>, from: string, to: string): (target: string) => string | undefined {
|
|
169
|
+
const names = new Map<string, Set<string>>();
|
|
170
|
+
for (const [path, source] of notes) {
|
|
171
|
+
document(source, path);
|
|
172
|
+
const key = normalizeTarget(path.slice(6, -3)), values = names.get(key) ?? new Set<string>(); values.add(path); names.set(key, values);
|
|
173
|
+
}
|
|
174
|
+
return target => {
|
|
175
|
+
const candidates = names.get(normalizeTarget(target.replace(/^\.\//, "").replace(/^notes\//i, "").replace(/\.md$/i, "")));
|
|
176
|
+
return candidates?.size === 1 && candidates.has(from) ? to.slice(6, -3) : undefined;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/** Same metadata recursion and visible-body rewrite that move has always used. */
|
|
180
|
+
function rewriteNoteReferences(source: string, path: string, replace: (target: string) => string | undefined, inbound?: MergeInboundLink[], preserve: ReadonlySet<string> = new Set()): string {
|
|
181
|
+
const doc = document(source, path);
|
|
182
|
+
const record = (value: string, field: string, body = false) => {
|
|
183
|
+
if (!inbound) return;
|
|
184
|
+
for (const link of extractLinks(value)) {
|
|
185
|
+
const replacement = replace(link.target);
|
|
186
|
+
if (replacement !== undefined) inbound.push({ path, field, target: link.target, replacement, ...(body ? { line: link.line } : {}), ...(link.heading ? { heading: link.heading } : {}), ...(link.block ? { block: link.block } : {}) });
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const rewriteValue = (value: unknown, field: string): unknown => {
|
|
190
|
+
if (typeof value === "string") { record(value, field); return rewriteLinkTargets(value, replace); }
|
|
191
|
+
if (Array.isArray(value)) return value.map((item, index) => rewriteValue(item, `${field}[${index}]`));
|
|
192
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, rewriteValue(item, `${field}.${key}`)]));
|
|
193
|
+
return value;
|
|
194
|
+
};
|
|
195
|
+
const changes: Record<string, unknown> = {};
|
|
196
|
+
for (const [key, value] of Object.entries(doc.data)) changes[key] = preserve.has(key) ? value : rewriteValue(value, key);
|
|
197
|
+
record(doc.body, "body", true);
|
|
198
|
+
const frontmatter = emitFrontmatter(doc, changes).slice(0, -doc.body.length || undefined);
|
|
199
|
+
return (doc.body.length ? frontmatter : emitFrontmatter(doc, changes)) + rewriteLinkTargets(doc.body, replace);
|
|
200
|
+
}
|
|
201
|
+
function supersededNote(source: string, path: string, next: string): string {
|
|
202
|
+
return emitFrontmatter(document(source, path), { status: "superseded", superseded_by: `[[${next.slice(6, -3)}]]` });
|
|
203
|
+
}
|
|
204
|
+
function topicKey(value: string): string { return normalizeTarget(value.slice(2, -2).trim().replace(/^\.\//, "").replace(/^notes\//i, "").replace(/\.md$/i, "")); }
|
|
205
|
+
function mergeTopics(value: unknown, path: string): string[] {
|
|
206
|
+
if (value === undefined) return [];
|
|
207
|
+
if (!Array.isArray(value) || value.some(topic => typeof topic !== "string" || !/^\[\[[^\]|#]+\]\]$/.test(topic))) refuse("V01", "merge topics must be a list of plain wiki targets; repair topics in this note before merging", path);
|
|
208
|
+
return value as string[];
|
|
209
|
+
}
|
|
210
|
+
/** Remove only a plain Topics list; unusual authored contents need an explicit repair, never silent deletion. */
|
|
211
|
+
function withoutMergeTopics(body: string, topics: string[], path: string): string {
|
|
212
|
+
const visible = mergeHeadingLines(body), raw = body.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
213
|
+
if (visible.some((line, index) => /^Topics[ \t]*$/i.test(line.trim()) && /^ {0,3}-+[ \t]*$/.test(visible[index + 1] ?? ""))) refuse("V05", "merge cannot consolidate a Setext Topics heading safely; convert it to a plain ## Topics list before merging", path);
|
|
214
|
+
const headings = visible.flatMap((line, index) => /^ {0,3}##[ \t]+Topics(?:[ \t]+#+)?[ \t]*$/i.test(line) ? [index] : []);
|
|
215
|
+
if (headings.length > 1) refuse("V05", "merge found multiple Topics sections; consolidate them into one matching topic list before merging", path);
|
|
216
|
+
const start = headings[0]; if (start === undefined) return body;
|
|
217
|
+
let end = start + 1;
|
|
218
|
+
while (end < raw.length && !/^ {0,3}#{1,2}(?:[ \t]+|$)/.test(visible[end] ?? "")) end++;
|
|
219
|
+
const found: string[] = [];
|
|
220
|
+
for (const line of raw.slice(start + 1, end)) {
|
|
221
|
+
if (!line.trim()) continue;
|
|
222
|
+
const wiki = /^[ \t]*(?:[-*+][ \t]+)?(\[\[[^\]|#]+\]\])[ \t]*(?:\r?\n)?$/.exec(line);
|
|
223
|
+
if (!wiki) refuse("V05", "merge cannot discard prose, headings or code inside Topics; move that content outside Topics and retry the merge", path);
|
|
224
|
+
found.push(topicKey(wiki[1]!));
|
|
225
|
+
}
|
|
226
|
+
const expected = topics.map(topicKey).sort(); found.sort();
|
|
227
|
+
if (found.length !== expected.length || found.some((value, index) => value !== expected[index])) refuse("V05", "merge Topics list does not match its metadata; align the body list with frontmatter topics before merging", path);
|
|
228
|
+
return raw.slice(0, start).join("") + raw.slice(end).join("");
|
|
229
|
+
}
|
|
230
|
+
/** Merge structural edits ignore raw HTML conservatively, through the next blank line. */
|
|
231
|
+
function mergeHeadingLines(body: string): string[] {
|
|
232
|
+
const raw = body.split(/\r?\n/); let html = false;
|
|
233
|
+
return visibleProseLines(body).map((line, index) => {
|
|
234
|
+
if (!raw[index]?.trim()) html = false;
|
|
235
|
+
else if (/^ {0,3}</.test(line)) html = true;
|
|
236
|
+
return html ? "" : line;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/** Remove only a leading title H1; demote other ATX headings and leave Setext/code/HTML untouched. */
|
|
240
|
+
function demoteMergeHeadings(body: string, removeLeadingTitle: boolean): string {
|
|
241
|
+
const visible = mergeHeadingLines(body), raw = body.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
242
|
+
const leading = raw.findIndex(line => line.trim());
|
|
243
|
+
return raw.map((line, index) => {
|
|
244
|
+
const heading = /^ {0,3}(#{1,6})(?:[ \t]+|$)/.exec(visible[index] ?? "");
|
|
245
|
+
if (!heading) return line;
|
|
246
|
+
if (removeLeadingTitle && index === leading && heading[1]!.length === 1 && /^ {0,3}#[ \t]+\S/.test(line)) return "";
|
|
247
|
+
return heading[1]!.length < 6 ? line.replace(/^( {0,3})(#+)/, "$1#$2") : line;
|
|
248
|
+
}).join("");
|
|
249
|
+
}
|
|
250
|
+
function mergeGap(body: string, newline: string): string {
|
|
251
|
+
if (body.endsWith("\n\n") || body.endsWith("\r\n\r\n")) return body;
|
|
252
|
+
return body + (body.endsWith("\n") ? newline : newline + newline);
|
|
253
|
+
}
|
|
254
|
+
/** Pure all-note merge plan, composed after rewriting existing references so its new provenance link is retained. */
|
|
255
|
+
export function planMerge(operation: Extract<WriteOp, { op: "merge" }>, notes: Map<string, string>): { changes: PlannedChange[]; preview: MergePreview; copiedBody: string } {
|
|
256
|
+
const keep = notePath(operation.keep), absorb = notePath(operation.absorb);
|
|
257
|
+
if (keep === absorb) refuse("V10", "merge endpoints must differ; choose separate keep and absorb notes", keep);
|
|
258
|
+
const kept = document(needed(notes, keep), keep), absorbed = document(needed(notes, absorb), absorb);
|
|
259
|
+
if (typeof kept.data.type !== "string" || !kept.data.type.trim() || kept.data.type !== absorbed.data.type || [kept.data.type, absorbed.data.type].some(type => type === "person" || type === "moc")) refuse("V01", "merge requires matching types other than person or moc; choose compatible notes", keep);
|
|
260
|
+
const replace = referenceReplacement(notes, absorb, keep), inboundLinks: MergeInboundLink[] = [], rewritten = new Map<string, string>();
|
|
261
|
+
for (const [path, source] of notes) {
|
|
262
|
+
try { rewritten.set(path, rewriteNoteReferences(source, path, replace, inboundLinks, path === keep ? new Set(["supersedes"]) : undefined)); }
|
|
263
|
+
catch (error) {
|
|
264
|
+
if (error instanceof WriteRefusal) throw error;
|
|
265
|
+
refuse("V01", "merge cannot rewrite this note's metadata losslessly; repair its YAML or convert the header to a block mapping, then request a fresh merge preview", path);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const keepDoc = document(rewritten.get(keep)!, keep), absorbDoc = document(rewritten.get(absorb)!, absorb);
|
|
269
|
+
const sources: string[] = [];
|
|
270
|
+
for (const [path, doc] of [[keep, keepDoc], [absorb, absorbDoc]] as const) if (doc.data.source !== undefined) {
|
|
271
|
+
let refs: string[];
|
|
272
|
+
try { refs = sourceReferences(doc.data.source).map(reference => reference.value); }
|
|
273
|
+
catch { refuse("V01", "merge source must contain at most 32 valid references; repair source entries in either note before merging", path); }
|
|
274
|
+
for (const value of refs) if (!sources.includes(value)) sources.push(value);
|
|
275
|
+
}
|
|
276
|
+
if (sources.length > 32) refuse("V01", "merge source union exceeds 32 entries; trim sources in either note first, then retry the merge", keep);
|
|
277
|
+
const keepTopics = mergeTopics(keepDoc.data.topics, keep), absorbTopics = mergeTopics(absorbDoc.data.topics, absorb), topics: string[] = [], seen = new Set<string>();
|
|
278
|
+
for (const topic of [...keepTopics, ...absorbTopics]) if (!seen.has(topicKey(topic))) { seen.add(topicKey(topic)); topics.push(topic); }
|
|
279
|
+
const newline = /\r?\n/.exec(keepDoc.body)?.[0] ?? "\n";
|
|
280
|
+
const originalLeadingLine = absorbed.body.split(/\r?\n/).find(line => line.trim()) ?? "";
|
|
281
|
+
const removeLeadingTitle = /^ {0,3}#[ \t]+\S/.test(originalLeadingLine);
|
|
282
|
+
const first = withoutMergeTopics(keepDoc.body, keepTopics, keep), second = demoteMergeHeadings(withoutMergeTopics(absorbDoc.body, absorbTopics, absorb), removeLeadingTitle);
|
|
283
|
+
const body = mergeGap(mergeGap(first, newline) + `## Merged from [[${absorb.slice(6, -3)}]]${newline}${newline}` + second, newline) + `## Topics${newline}${newline}` + topics.map(topic => `- ${topic}${newline}`).join("");
|
|
284
|
+
if (Buffer.byteLength(body) > MAX_WRITE_BYTES) refuse("V09", "merged body exceeds 256 KiB; shorten either note's content before retrying the merge", keep);
|
|
285
|
+
try {
|
|
286
|
+
const metadata = emitFrontmatter(keepDoc, { source: sources.length ? sources : undefined, topics });
|
|
287
|
+
rewritten.set(keep, (parseFrontmatter(metadata).frontmatter ?? "") + body);
|
|
288
|
+
rewritten.set(absorb, supersededNote(rewritten.get(absorb)!, absorb, keep));
|
|
289
|
+
} catch { refuse("V01", "merge metadata cannot be edited losslessly; convert endpoint headers to editable block mappings and retry the merge", keep); }
|
|
290
|
+
const changes = [...rewritten].filter(([path, after]) => after !== notes.get(path)).map(([path, after]) => ({ path, before: notes.get(path)!, after })).sort((a, b) => a.path.localeCompare(b.path));
|
|
291
|
+
const oversized = changes.find(change => Buffer.byteLength(change.after) > MAX_WRITE_BYTES);
|
|
292
|
+
if (oversized) refuse("V09", "merged file or inbound rewrite exceeds 256 KiB; shorten the affected note before retrying the merge", oversized.path);
|
|
293
|
+
return { changes, preview: { keep, absorb, body, source: sources, topics, inboundLinks }, copiedBody: second };
|
|
294
|
+
}
|
|
295
|
+
|
|
129
296
|
/** Plan transformations in memory. This function never touches files. */
|
|
130
297
|
export function planOperation(operation: WriteOp, notes: Map<string, string>, reportBefore?: string | null): PlannedChange[] {
|
|
131
298
|
const planned = new Map<string, PlannedChange>();
|
|
@@ -226,35 +393,18 @@ export function planOperation(operation: WriteOp, notes: Map<string, string>, re
|
|
|
226
393
|
case "supersede": {
|
|
227
394
|
const old = existing(operation.old), next = existing(operation.next);
|
|
228
395
|
if (old.path === next.path) refuse("V10", "supersession endpoints must differ");
|
|
229
|
-
set(old.path,
|
|
396
|
+
set(old.path, supersededNote(old.source, old.path, next.path));
|
|
230
397
|
set(next.path, appendSection(next.source, "Relevant Notes", `- Supersedes [[${old.path.slice(6, -3)}]]`, next.path));
|
|
231
398
|
break;
|
|
232
399
|
}
|
|
400
|
+
case "merge": return planMerge(operation, notes).changes;
|
|
233
401
|
case "delete": { const { path } = existing(operation.path); set(path, null); break; }
|
|
234
402
|
case "move": {
|
|
235
403
|
const fromPath = moveSourcePath(operation.from, notes), from = { path: fromPath, source: needed(notes, fromPath) }, to = notePath(operation.to);
|
|
236
404
|
if (notes.has(to)) refuse("V08", "move destination already exists", to);
|
|
237
|
-
const
|
|
238
|
-
for (const [path, source] of notes) {
|
|
239
|
-
document(source, path); // Every note must still parse before a move rewrites links.
|
|
240
|
-
// Links resolve by path only, so alias-only links are left untouched.
|
|
241
|
-
const key = normalizeTarget(path.slice(6, -3)), values = names.get(key) ?? new Set<string>(); values.add(path); names.set(key, values);
|
|
242
|
-
}
|
|
243
|
-
const replace = (target: string): string | undefined => {
|
|
244
|
-
const candidates = names.get(normalizeTarget(target.replace(/^\.\//, "").replace(/^notes\//i, "").replace(/\.md$/i, "")));
|
|
245
|
-
return candidates?.size === 1 && candidates.has(from.path) ? to.slice(6, -3) : undefined;
|
|
246
|
-
};
|
|
247
|
-
const rewriteValue = (value: unknown): unknown => {
|
|
248
|
-
if (typeof value === "string") return rewriteLinkTargets(value, replace);
|
|
249
|
-
if (Array.isArray(value)) return value.map(rewriteValue);
|
|
250
|
-
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, rewriteValue(item)]));
|
|
251
|
-
return value;
|
|
252
|
-
};
|
|
405
|
+
const replace = referenceReplacement(notes, from.path, to);
|
|
253
406
|
for (const [path, source] of notes) {
|
|
254
|
-
const
|
|
255
|
-
for (const [key, value] of Object.entries(doc.data)) changes[key] = rewriteValue(value);
|
|
256
|
-
const frontmatter = emitFrontmatter(doc, changes).slice(0, -doc.body.length || undefined);
|
|
257
|
-
const output = (doc.body.length ? frontmatter : emitFrontmatter(doc, changes)) + rewriteLinkTargets(doc.body, replace);
|
|
407
|
+
const output = rewriteNoteReferences(source, path, replace);
|
|
258
408
|
if (path === from.path) { set(path, null); set(to, output); }
|
|
259
409
|
else set(path, output);
|
|
260
410
|
}
|
package/src/write-output.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CanonRowMatch } from "./canon-tables.ts";
|
|
1
2
|
/**
|
|
2
3
|
* Text renderers for writes, two-step previews and promotion (#114): one result line, then changes, issues, the
|
|
3
4
|
* log receipt and, for a preview, the exact confirm command. Note bodies and drafts are never printed; `--json` has them.
|
|
@@ -9,7 +10,7 @@ import type { OrgPromoteResult } from "./org-promote.ts";
|
|
|
9
10
|
import type { SyncResult } from "./sync.ts";
|
|
10
11
|
import type { PromoteResult } from "./promote.ts";
|
|
11
12
|
import { notePath } from "./write-operations.ts";
|
|
12
|
-
import { blocks, clean, fields, issueTable, nextCommand, paragraph, plural, shellWord, table, type TextOptions, type Tone } from "./text-output.ts";
|
|
13
|
+
import { blocks, clean, fields, heading, issueTable, nextCommand, paragraph, plural, shellWord, table, type TextOptions, type Tone } from "./text-output.ts";
|
|
13
14
|
|
|
14
15
|
/** The words the command was run with (`[command, ...args]`, no `--color`) and its positional arguments. */
|
|
15
16
|
export interface WriteContext { argv: string[]; positional: string[] }
|
|
@@ -17,6 +18,7 @@ type Candidates = { projects: { project: string; candidates: { id: string; proje
|
|
|
17
18
|
|
|
18
19
|
const line = (text: string, o: TextOptions, t?: Tone) => paragraph(text, o, t);
|
|
19
20
|
const list = (label: string, items: readonly string[] | undefined) => items?.length ? [`${label}:`, ...items.map(item => ` - ${clean(item)}`)].join("\n") : undefined;
|
|
21
|
+
const canonList = (rows: readonly CanonRowMatch[] | undefined) => list("canonical rows", rows?.map(row => `${row.path}:${row.line} ${row.key}: ${row.row}`));
|
|
20
22
|
const short = (sha: string | null) => sha ? sha.slice(0, 12) : "-";
|
|
21
23
|
const action = (c: ChangeReceipt) => c.beforeSha === null ? "create" : c.sha256 === null ? "delete" : "update";
|
|
22
24
|
const errors = (issues: readonly WriteIssue[]) => issues.filter(issue => issue.severity === "error").length;
|
|
@@ -40,7 +42,7 @@ function logLine(log: LogReceipt | undefined, o: TextOptions, label = "log"): st
|
|
|
40
42
|
|
|
41
43
|
const VERBS: Record<string, [string, string]> = {
|
|
42
44
|
new: ["create", "created"], patch: ["patch", "patched"], append: ["append to", "appended to"], frontmatter: ["update the frontmatter of", "updated the frontmatter of"],
|
|
43
|
-
link: ["link", "linked"], supersede: ["supersede", "superseded"], move: ["move", "moved"], delete: ["delete", "deleted"], report: ["write", "wrote"],
|
|
45
|
+
link: ["link", "linked"], supersede: ["supersede", "superseded"], merge: ["merge", "merged"], move: ["move", "moved"], delete: ["delete", "deleted"], report: ["write", "wrote"],
|
|
44
46
|
};
|
|
45
47
|
function what(command: string, changes: ChangeReceipt[], targets: string[], past: boolean): string {
|
|
46
48
|
const verb = VERBS[command]?.[past ? 1 : 0] ?? command;
|
|
@@ -48,13 +50,14 @@ function what(command: string, changes: ChangeReceipt[], targets: string[], past
|
|
|
48
50
|
const deleted = changes.filter(c => action(c) === "delete").map(c => c.path), created = changes.filter(c => action(c) === "create").map(c => c.path);
|
|
49
51
|
const updated = changes.filter(c => action(c) === "update").map(c => c.path);
|
|
50
52
|
if (command === "move") return `${verb} ${deleted[0] ?? stem(targets[0])} to ${created[0] ?? stem(targets[1])}${others(updated, past ? "rewrote links in" : "rewrite links in")}`;
|
|
53
|
+
if (command === "merge") return `${verb} ${stem(targets[1])} into ${stem(targets[0])}${others(updated.filter(path => path !== stem(targets[0]) && path !== stem(targets[1])), past ? "rewrote links in" : "rewrite links in")}`;
|
|
51
54
|
if (command === "delete") return `${verb} ${deleted[0] ?? stem(targets[0])}${others(updated, past ? "updated" : "update")}`;
|
|
52
55
|
if (command === "link") return `${verb} ${stem(targets[0])} and ${stem(targets[1])}`;
|
|
53
56
|
if (command === "supersede") return `${verb} ${stem(targets[0])} with ${stem(targets[1])}`;
|
|
54
57
|
return `${verb} ${changes[0]?.path ?? (command === "report" ? targets[0] : stem(targets[0]))}`;
|
|
55
58
|
}
|
|
56
59
|
|
|
57
|
-
/** `new`, `patch`, `append`, `frontmatter`, `link`, `supersede`, `move`, `delete`, `report` and `check`. */
|
|
60
|
+
/** `new`, `patch`, `append`, `frontmatter`, `link`, `supersede`, `merge`, `move`, `delete`, `report` and `check`. */
|
|
58
61
|
export function renderWriteReceipt(r: WriteReceipt, o: TextOptions, ctx: WriteContext): string {
|
|
59
62
|
const command = ctx.argv[0]!, targets = ctx.positional.slice(1), issues = issueTable(r.issues, o);
|
|
60
63
|
if (command === "check") {
|
|
@@ -62,18 +65,26 @@ export function renderWriteReceipt(r: WriteReceipt, o: TextOptions, ctx: WriteCo
|
|
|
62
65
|
const verdict = r.ok ? `ok: ${path} passes validation${w ? ` (${plural(w, "warning")})` : ""}` : `fail: ${path} has ${plural(e, "error")}${w ? ` and ${plural(w, "warning")}` : ""}`;
|
|
63
66
|
return blocks(issues, line(verdict, o, r.ok ? "ok" : "fail"));
|
|
64
67
|
}
|
|
65
|
-
const twoStep = command === "move" || command === "delete";
|
|
68
|
+
const twoStep = command === "merge" || command === "move" || command === "delete";
|
|
69
|
+
const hasState = r.changes.some(change => change.applied !== undefined);
|
|
70
|
+
const appliedSummary = hasState ? r.changes.filter(change => change.applied).map(change => change.path).join(", ") : what(command, r.changes, targets, true);
|
|
66
71
|
let head: string, t: Tone;
|
|
67
|
-
if (!r.ok) [head, t] = r.applied ? [`failed after writing: ${
|
|
72
|
+
if (!r.ok) [head, t] = r.applied ? [`failed after writing: ${appliedSummary}; see the issues`, "fail"] : ["refused: nothing written", "fail"];
|
|
68
73
|
else if (r.preview) [head, t] = [`dry run: would ${what(command, r.changes, targets, false)}`, "info"];
|
|
69
74
|
else if (r.unchanged) [head, t] = ["unchanged: links already present", "muted"];
|
|
70
75
|
else if (!r.applied) [head, t] = ["unchanged: nothing to write", "muted"];
|
|
71
76
|
else [head, t] = [what(command, r.changes, targets, true), "ok"];
|
|
72
77
|
const one = r.changes.length === 1 && r.ok && !r.preview && r.changes[0]!.sha256 ? fields([["sha256", r.changes[0]!.sha256]], o) : undefined;
|
|
73
|
-
const changes = r.changes.length > 1 ? table(["PATH", "ACTION", "BYTES", "SHA"], r.changes.map(c => [c.path, action(c), c.sha256 === null ? "-" : c.bytes, short(c.sha256)]), o) : undefined;
|
|
78
|
+
const changes = r.changes.length > 1 || hasState ? table(["PATH", "ACTION", "BYTES", "SHA", ...(hasState ? ["STATE"] : [])], r.changes.map(c => [c.path, action(c), c.sha256 === null ? "-" : c.bytes, short(c.sha256), ...(hasState ? [c.applied ? "applied" : "not applied"] : [])]), o) : undefined;
|
|
74
79
|
const log = r.log ? logLine(r.log, o) : r.ok && r.preview ? line(twoStep ? "log: written on confirm" : "log: none (dry run)", o, "muted") : undefined;
|
|
75
80
|
const confirm = twoStep && r.ok && r.preview && r.fingerprint ? nextCommand("confirm", confirmCommand(ctx.argv, r.fingerprint), o) : undefined;
|
|
76
|
-
|
|
81
|
+
const merge = r.merge && r.preview ? blocks(
|
|
82
|
+
fields([["keep", r.merge.keep], ["absorb", r.merge.absorb]], o),
|
|
83
|
+
line(`merged body: ${plural(Buffer.byteLength(r.merge.body), "byte")}; --json shows the full body`, o, "muted"),
|
|
84
|
+
list("merged source", r.merge.source), list("merged topics", r.merge.topics),
|
|
85
|
+
r.merge.inboundLinks.length ? heading("INBOUND LINKS", o) + "\n" + table(["PATH", "FIELD", "TARGET", "REPLACEMENT", "LINE"], r.merge.inboundLinks.map(link => [link.path, link.field, link.target + (link.block ? `#^${link.block}` : link.heading ? `#${link.heading}` : ""), link.replacement + (link.block ? `#^${link.block}` : link.heading ? `#${link.heading}` : ""), link.line ?? "-"]), o) : line("inbound links: none", o, "muted"),
|
|
86
|
+
) : undefined;
|
|
87
|
+
return blocks([line(head, o, t), one].filter(Boolean).join("\n"), changes, merge, issues, list("possible duplicates", r.possibleDuplicates), list("retained backups", r.retainedBackups), log, confirm);
|
|
77
88
|
}
|
|
78
89
|
|
|
79
90
|
/** `tidy`: counts per rule and the notes touched, then the confirm command. */
|
|
@@ -106,7 +117,7 @@ export function renderOrgPromote(r: OrgPromoteResult, o: TextOptions, ctx: Write
|
|
|
106
117
|
const lint = r.lint ? line(`org lint: ${r.lint.passed ? "passed" : "failed"}${r.lint.summary ? ` (${r.lint.summary})` : ""}`, o, r.lint.passed ? "ok" : "fail") : undefined;
|
|
107
118
|
const logs = r.logs ? [logLine(r.logs.target ?? undefined, o, "target log"), logLine(r.logs.source ?? undefined, o, "source log")].filter(Boolean).join("\n") : undefined;
|
|
108
119
|
const confirm = r.ok && r.preview && r.fingerprint ? nextCommand("confirm", confirmCommand(ctx.argv, r.fingerprint), o) : undefined;
|
|
109
|
-
return blocks(line(head, o, t), issueTable(issues, o), list("dropped links", r.droppedLinks), lint, logs, confirm);
|
|
120
|
+
return blocks(line(head, o, t), issueTable(issues, o), list("dropped links", r.droppedLinks), canonList(r.canonRowMatches), list("canonical table warnings", r.canonTableWarnings), list("choices", r.choices), lint, logs, confirm);
|
|
110
121
|
}
|
|
111
122
|
|
|
112
123
|
/** `sync <vault>`. */
|
|
@@ -146,6 +157,6 @@ export function renderPromote(r: PromoteResult, o: TextOptions, ctx: WriteContex
|
|
|
146
157
|
const info = fields([["linked", r.linked ?? ""]], o);
|
|
147
158
|
const hint = edited && r.preview ? line("edited text is only previewed: save it and confirm with --body <file> and this fingerprint", o, "warn") : undefined;
|
|
148
159
|
const command = r.ok && r.preview && !r.alreadyLinked && r.fingerprint ? confirmCommand(ctx.argv, r.fingerprint, edited ? ["--edit"] : []) : undefined;
|
|
149
|
-
return blocks([line(head, o, t), info].filter(Boolean).join("\n"), draft, issueTable(r.issues, o), list("possible duplicates", r.possibleDuplicates), list("choices", r.choices),
|
|
160
|
+
return blocks([line(head, o, t), info].filter(Boolean).join("\n"), draft, issueTable(r.issues, o), list("possible duplicates", r.possibleDuplicates), canonList(r.canonRowMatches), list("canonical table warnings", r.canonTableWarnings), list("choices", r.choices),
|
|
150
161
|
logLine(r.note?.log, o), hint, command && nextCommand("confirm", edited ? command.replace(/ --confirm /, " --body <file> --confirm ") : command, o));
|
|
151
162
|
}
|
package/src/write-types.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { PiiSurface } from "./config.ts";
|
|
2
2
|
import type { LogEvent, LogReceipt } from "./log-types.ts";
|
|
3
3
|
import type { VaultSpec, VmemConfig } from "./config.ts";
|
|
4
|
+
export const MAX_WRITE_BYTES = 256 * 1024;
|
|
4
5
|
export interface PatchEdit { oldString: string; newString: string }
|
|
6
|
+
export interface MergeInboundLink { path: string; field: string; target: string; replacement: string; line?: number; heading?: string; block?: string }
|
|
7
|
+
export interface MergePreview { keep: string; absorb: string; body: string; source: string[]; topics: string[]; inboundLinks: MergeInboundLink[] }
|
|
5
8
|
export type WriteOp =
|
|
6
9
|
| { op: "check"; path: string; text: string }
|
|
7
10
|
| { op: "create"; path: string; frontmatter: Record<string, unknown>; body?: string }
|
|
@@ -11,6 +14,7 @@ export type WriteOp =
|
|
|
11
14
|
| { op: "frontmatter"; path: string; set: Record<string, unknown>; unset?: string[]; expectedSha?: string }
|
|
12
15
|
| { op: "link"; from: string; to: string; relation: "supports" | "contradicts" | "extends" | "applies-to" | "context-for"; context?: string; bidirectional?: boolean }
|
|
13
16
|
| { op: "supersede"; old: string; next: string }
|
|
17
|
+
| { op: "merge"; keep: string; absorb: string }
|
|
14
18
|
| { op: "move"; from: string; to: string }
|
|
15
19
|
| { op: "delete"; path: string }
|
|
16
20
|
| { op: "tidy" }
|
|
@@ -38,7 +42,7 @@ export interface ValidationContext {
|
|
|
38
42
|
/** PII surface for content screening; `notes` unless an internal caller (org promotion) sets it. */
|
|
39
43
|
piiSurface?: PiiSurface;
|
|
40
44
|
}
|
|
41
|
-
export interface ChangeReceipt { path: string; beforeSha: string | null; sha256: string | null; bytes: number }
|
|
45
|
+
export interface ChangeReceipt { applied?: boolean; path: string; beforeSha: string | null; sha256: string | null; bytes: number }
|
|
42
46
|
export interface WriteReceipt {
|
|
43
47
|
ok: boolean;
|
|
44
48
|
applied: boolean;
|
|
@@ -49,7 +53,10 @@ export interface WriteReceipt {
|
|
|
49
53
|
fingerprint?: string;
|
|
50
54
|
preview?: boolean;
|
|
51
55
|
possibleDuplicates?: string[];
|
|
56
|
+
merge?: MergePreview;
|
|
52
57
|
indexUpdated: boolean;
|
|
58
|
+
/** Original relative names of backups retained for recovery after drift/failed rollback. */
|
|
59
|
+
retainedBackups?: string[];
|
|
53
60
|
logged?: boolean;
|
|
54
61
|
log?: LogReceipt;
|
|
55
62
|
}
|
package/src/write-validation.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { sourceReferences, sourceWorkspaceRoot, missingSourceWarnings } from "./
|
|
|
3
3
|
import { basename, resolve } from "node:path";
|
|
4
4
|
import { domainProblem, type VaultSpec, type VmemConfig } from "./config.ts";
|
|
5
5
|
import { parseFrontmatter } from "./frontmatter.ts";
|
|
6
|
-
import { extractLinks, normalizeTarget, proseLines, resolveLinks, type Link, type LinkNote, type ResolvedLink } from "./links.ts";
|
|
6
|
+
import { extractLinks, normalizeTarget, proseLines, visibleProseLines, resolveLinks, type Link, type LinkNote, type ResolvedLink } from "./links.ts";
|
|
7
7
|
import { canonicalPath, isDenied } from "./platform.ts";
|
|
8
|
-
import type
|
|
8
|
+
import { MAX_WRITE_BYTES, type PlannedChange, type ValidationContext, type WriteIssue } from "./write-types.ts";
|
|
9
9
|
import type { PiiSurface } from "./config.ts";
|
|
10
10
|
import { screenPii } from "./pii.ts";
|
|
11
11
|
import { moveSourcePath } from "./write-operations.ts";
|
|
@@ -34,7 +34,7 @@ const SECRETS = [/-----BEGIN [A-Z ]*PRIVATE KEY-----/, /\bAKIA[0-9A-Z]{16}\b/, /
|
|
|
34
34
|
/\bxox[abprs]-[A-Za-z0-9-]{10,}/, /\bAIza[0-9A-Za-z_-]{35}\b/, /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/, /\b(?:password|passwd|secret|api[_-]?key|token)\s*[:=]\s*\S{8,}/i];
|
|
35
35
|
export const SECRET_PATTERNS: readonly RegExp[] = SECRETS;
|
|
36
36
|
export { EMAIL_PATTERN } from "./pii.ts";
|
|
37
|
-
const MAX_BYTES =
|
|
37
|
+
const MAX_BYTES = MAX_WRITE_BYTES;
|
|
38
38
|
export const MAX_REPORT_BYTES = 16 * 1024 * 1024;
|
|
39
39
|
function issue(code: string, message: string, severity: WriteIssue["severity"] = "error", path?: string): WriteIssue {
|
|
40
40
|
return { code, severity, message, ...(path ? { path } : {}) };
|
|
@@ -47,7 +47,7 @@ export function screenContent(text: string, vault: VaultSpec, config: VmemConfig
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** Quoted escapes must not hide semantic metadata from the same content policy. */
|
|
50
|
-
function screenMetadata(data:Record<string,unknown>,context:ValidationContext,path:string):WriteIssue[]{
|
|
50
|
+
export function screenMetadata(data:Record<string,unknown>,context:Pick<ValidationContext,"vault"|"config"|"piiSurface">,path:string):WriteIssue[]{
|
|
51
51
|
const found=new Map<string,WriteIssue>(),active=new WeakSet<object>();let nodes=0,bytes=0;
|
|
52
52
|
const inspect=(text:string)=>{
|
|
53
53
|
bytes+=Buffer.byteLength(text);if(bytes>MAX_BYTES*4)throw new Error("budget");
|
|
@@ -74,11 +74,12 @@ function strings(value: unknown): string[] {
|
|
|
74
74
|
}
|
|
75
75
|
function fold(value: string): string { return value.normalize("NFKC").toLowerCase().replace(/[-_\s]+/g, " ").trim(); }
|
|
76
76
|
/** Notes whose path and text match `reuse` keep that parsed object, so link analysis is shared too. */
|
|
77
|
-
function parseNotes(notes: Map<string, string>, reuse?: Map<string, ParsedNote>): Map<string, ParsedNote> {
|
|
77
|
+
function parseNotes(notes: Map<string, string>, reuse?: Map<string, ParsedNote>, baseline?: Map<string, ParsedNote>): Map<string, ParsedNote> {
|
|
78
78
|
const parsed = new Map<string, ParsedNote>();
|
|
79
79
|
for (const [path, text] of notes) {
|
|
80
|
-
const previous = reuse?.get(path);
|
|
81
|
-
|
|
80
|
+
const previous = reuse?.get(path), original = baseline?.get(path);
|
|
81
|
+
const cached = previous?.text === text ? previous : original?.text === text ? original : undefined;
|
|
82
|
+
if (cached) { parsed.set(path, cached); continue; }
|
|
82
83
|
try {
|
|
83
84
|
const { data, body } = parseFrontmatter(text);
|
|
84
85
|
const h1 = proseLines(body).map(line => /^ {0,3}#(?:[ \t]+(.*)|[ \t]*)$/.exec(line)).find(match => match)?.[1]?.replace(/[ \t]+#+[ \t]*$/, "").trim() ?? "";
|
|
@@ -126,13 +127,14 @@ function movedFrom(value: string, context: ValidationContext): string {
|
|
|
126
127
|
function changedBrokenLinks(oldNotes: Map<string, ParsedNote>, newNotes: Map<string, ParsedNote>, context: ValidationContext): WriteIssue[] {
|
|
127
128
|
const oldCounts = new Map<string, number>();
|
|
128
129
|
const move = context.operation.op === "move" ? context.operation : undefined;
|
|
129
|
-
const
|
|
130
|
-
const
|
|
131
|
-
const
|
|
130
|
+
const merge = context.operation.op === "merge" ? context.operation : undefined;
|
|
131
|
+
const from = move ? movedFrom(move.from, context) : merge ? `notes/${merge.absorb.replace(/^notes\//, "").replace(/\.md$/, "")}.md` : "";
|
|
132
|
+
const to = move ? `notes/${move.to.replace(/^notes\//, "").replace(/\.md$/, "")}.md` : merge ? `notes/${merge.keep.replace(/^notes\//, "").replace(/\.md$/, "")}.md` : "";
|
|
133
|
+
const oldNames = move || merge ? names(oldNotes) : undefined;
|
|
132
134
|
for (const link of references(oldNotes)) if (link.reason) {
|
|
133
135
|
const candidates = oldNames?.get(noteKey(link.target));
|
|
134
136
|
const renamedTarget = candidates?.size === 1 && [...candidates][0]!.path === from;
|
|
135
|
-
const signature = linkSignature(renamedTarget ? { ...link, target: to } : link, link.source === from ? to : link.source);
|
|
137
|
+
const signature = linkSignature(renamedTarget ? { ...link, target: to } : link, move && link.source === from ? to : link.source);
|
|
136
138
|
oldCounts.set(signature, (oldCounts.get(signature) ?? 0) + 1);
|
|
137
139
|
}
|
|
138
140
|
const issues: WriteIssue[] = [];
|
|
@@ -195,7 +197,7 @@ function topicIssues(note: ParsedNote, index: Map<string, Set<ParsedNote>>, seve
|
|
|
195
197
|
function topicProblems(note: ParsedNote): Set<"placement" | "content"> {
|
|
196
198
|
const problems = new Set<"placement" | "content">();
|
|
197
199
|
if (note.data.type === "moc" && note.data.topics === undefined) return problems;
|
|
198
|
-
const lines =
|
|
200
|
+
const lines = visibleProseLines(note.body), h2s = lines.flatMap((line, index) => /^ {0,3}##[ \t]+/.test(line) ? [index] : []);
|
|
199
201
|
const topics = h2s.filter(index => /^ {0,3}##[ \t]+Topics(?:[ \t]+#+)?[ \t]*$/i.test(lines[index]!));
|
|
200
202
|
if (topics.length !== 1 || h2s[h2s.length - 1] !== topics[0]) problems.add("placement");
|
|
201
203
|
const last = topics[topics.length - 1], end = h2s.find(index => last !== undefined && index > last) ?? lines.length;
|
|
@@ -221,13 +223,13 @@ function topicPlacement(note: ParsedNote, old: ParsedNote | undefined, context:
|
|
|
221
223
|
return [issue("V05", "Topics must be the final H2 and list exactly the frontmatter topics", retained ? "warning" : "error", note.path)];
|
|
222
224
|
}
|
|
223
225
|
|
|
224
|
-
/**
|
|
225
|
-
|
|
226
|
+
/** Run every validation rule over one candidate, reusing only source-identical parsed notes. */
|
|
227
|
+
function validateChanges(changes: PlannedChange[], context: ValidationContext, oldNotes: Map<string, ParsedNote>, oldNames: Map<string, Set<ParsedNote>>, reuse?: Map<string, ParsedNote>): { issues: WriteIssue[]; notes: Map<string, ParsedNote> } {
|
|
226
228
|
const issues: WriteIssue[] = [], final = new Map(context.notes), touched = new Set(changes.map(change => change.path));
|
|
227
229
|
const report = context.operation.op === "report";
|
|
228
230
|
for (const change of changes) {
|
|
229
231
|
// Sanitize and move may touch an existing legacy-named note (L13 names stay until moved); move destinations stay strict.
|
|
230
|
-
const legacy = context.notes.has(change.path) && (context.operation.op === "sanitize" || context.operation.op === "move");
|
|
232
|
+
const legacy = context.notes.has(change.path) && (context.operation.op === "sanitize" || context.operation.op === "move" || context.operation.op === "merge");
|
|
231
233
|
if (!validPath(change.path, report) && !legacy) issues.push(issue("V04", "invalid mutation path"));
|
|
232
234
|
if (change.after === null) final.delete(change.path);
|
|
233
235
|
else if (change.path.startsWith("notes/")) final.set(change.path, change.after);
|
|
@@ -237,7 +239,7 @@ export function validateWrite(changes: PlannedChange[], context: ValidationConte
|
|
|
237
239
|
try{issues.push(...screenMetadata(parseFrontmatter(change.after).data,context,change.path));}catch{ /* Invalid note YAML is reported separately; reports remain schema-exempt. */ }
|
|
238
240
|
}
|
|
239
241
|
const sourceRoot=sourceWorkspaceRoot(context.vault);
|
|
240
|
-
const
|
|
242
|
+
const newNotes = parseNotes(final, reuse ?? oldNotes, oldNotes), newNames = names(newNotes);
|
|
241
243
|
const citekeys = new Map<string, ParsedNote[]>();
|
|
242
244
|
for (const note of newNotes.values()) if (typeof note.data.citekey === "string") {
|
|
243
245
|
const peers = citekeys.get(note.data.citekey.toLowerCase()) ?? [];
|
|
@@ -291,5 +293,32 @@ export function validateWrite(changes: PlannedChange[], context: ValidationConte
|
|
|
291
293
|
}
|
|
292
294
|
issues.push(...changedBrokenLinks(oldNotes, newNotes, context));
|
|
293
295
|
}
|
|
294
|
-
return issues;
|
|
296
|
+
return { issues, notes: newNotes };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function sameSources(notes: Map<string, string>, previous: Map<string, string> | undefined): boolean {
|
|
300
|
+
if (!previous || notes.size !== previous.size) return false;
|
|
301
|
+
const entries = previous.entries();
|
|
302
|
+
for (const [path, source] of notes) {
|
|
303
|
+
const before = entries.next().value;
|
|
304
|
+
if (!before || before[0] !== path || before[1] !== source) return false;
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
/** A write-local raw/stamped validation session. Policy, screens and graph rules run on every call. */
|
|
309
|
+
export function createWriteValidator(context: ValidationContext): (changes: PlannedChange[]) => WriteIssue[] {
|
|
310
|
+
let sources: Map<string, string> | undefined, baseline = new Map<string, ParsedNote>(), baselineNames = names(baseline), prospective: Map<string, ParsedNote> | undefined;
|
|
311
|
+
return changes => {
|
|
312
|
+
if (!sameSources(context.notes, sources)) {
|
|
313
|
+
baseline = parseNotes(context.notes, baseline); baselineNames = names(baseline);
|
|
314
|
+
sources = new Map(context.notes); prospective = undefined;
|
|
315
|
+
}
|
|
316
|
+
const result = validateChanges(changes, context, baseline, baselineNames, prospective);
|
|
317
|
+
prospective = result.notes;
|
|
318
|
+
return result.issues;
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/** Compatible one-shot validation; callers with raw/stamped passes can share a write-local session. */
|
|
322
|
+
export function validateWrite(changes: PlannedChange[], context: ValidationContext): WriteIssue[] {
|
|
323
|
+
return createWriteValidator(context)(changes);
|
|
295
324
|
}
|