@staix/vault-mem 0.4.0 → 0.5.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.
@@ -16,8 +16,8 @@ export const MAX_RESULTS = 500;
16
16
  * share the unversioned `<digest>.sqlite`; the first run of a newer version builds its own file once.
17
17
  */
18
18
  export const LINK_RESOLUTION = 2;
19
- /** Bump when tables or FTS columns change. Named per value like LINK_RESOLUTION. 2 = CJK bigram table. */
20
- export const INDEX_SCHEMA = 2;
19
+ /** Bump when tables or FTS columns change. Named per value like LINK_RESOLUTION. 2 = CJK bigram table; 3 = reference identifiers in FTS. */
20
+ export const INDEX_SCHEMA = 3;
21
21
  const indexName = (root: string) => `${digest(root)}-links${LINK_RESOLUTION}-schema${INDEX_SCHEMA}.sqlite`;
22
22
  /**
23
23
  * unicode61 keeps each Hangul/CJK run as one token, so `사업단` misses `사업단은`. A side table holds the
@@ -221,7 +221,7 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
221
221
  if (size.count > 10000 || size.bytes > 64 * 1024 * 1024) throw new Error("analysis: cached snapshot exceeds bound");
222
222
  for (const row of disk.query<Omit<StoredNote,"aliases"> & {aliases:string}, []>("SELECT * FROM notes").all()) {
223
223
  this.db.query("INSERT INTO notes VALUES (?,?,?,?,?,?,?,?,?,?)").run(row.path,row.hash,row.metadata,row.title,row.description,row.body,row.type,row.domain,row.aliases,row.source);
224
- this.indexText(row.path, row.title, row.description, row.body);
224
+ this.indexText(row.path, row.title, row.description, !this.adapter && row.type === "reference" ? this.searchBody(row.body, parseFrontmatter(row.source).data) : row.body);
225
225
  }
226
226
  for (const row of disk.query<{source:string;ordinal:number;data:string}, []>("SELECT * FROM links").all()) this.db.query("INSERT INTO links VALUES (?,?,?)").run(row.source,row.ordinal,row.data);
227
227
  this.db.run(`PRAGMA user_version = ${Number(disk.query<{ user_version: number }, []>("PRAGMA user_version").get()?.user_version ?? 0)}`);
@@ -242,6 +242,12 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
242
242
  }
243
243
  close(): void { this.db.close(); }
244
244
  // ponytail: grams stores its bigram text (about 2.3x the CJK bytes); contentless with stable rowids would save it.
245
+ /** Keep the stored body and link/analysis input unchanged; identifiers join only the FTS text. */
246
+ private searchBody(body: string, data: Record<string, unknown>): string {
247
+ if (this.adapter || data.type !== "reference") return body;
248
+ const identifiers = [text(data.doi), text(data.citekey)].filter(Boolean);
249
+ return identifiers.length ? body + "\n" + identifiers.join("\n") : body;
250
+ }
245
251
  private indexText(path: string, title: string, description: string, body: string): void {
246
252
  this.db.query("INSERT INTO fts VALUES (?,?,?,?)").run(path, title, description, body);
247
253
  this.db.query("INSERT INTO grams VALUES (?,?,?,?)").run(path, grams(title), grams(description), grams(body));
@@ -350,7 +356,7 @@ export class VaultIndex<Source extends VaultSpec | SpecSourceSpec = VaultSpec> {
350
356
  this.db.query("INSERT OR REPLACE INTO notes VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(path, hash, stamp,
351
357
  title, description, body, text(data.type), text(data.domain), JSON.stringify(aliases(data.aliases ?? data.alias)), source);
352
358
  this.unindex(path);
353
- this.indexText(path, title, description, body);
359
+ this.indexText(path, title, description, this.searchBody(body, data));
354
360
  changed++;
355
361
  }
356
362
  for (const row of this.db.query<{path:string}, []>("SELECT path FROM analysis_notes").all()) if (!present.has(row.path)) this.db.query("DELETE FROM analysis_notes WHERE path=?").run(row.path);
package/src/vault-lint.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { BUILTIN_DOMAINS } from "./config.ts";
1
2
  import { parseAnalysisYaml, scalarText, yamlTruthy, AnalysisYamlUnsupported } from "./analysis-yaml.ts";
2
3
  import type { AnalysisSnapshot, LintCode, LintReport } from "./analysis-types.ts";
3
4
  const TYPES = ["decision", "insight", "method", "moc", "observation", "person", "project", "reference"];
@@ -70,7 +71,9 @@ function graphChecks(snapshot: AnalysisSnapshot): { l11: string[]; l12: string[]
70
71
  singles.sort();
71
72
  return { l11, l12, info: `nodes ${graph.nodes?.length ?? 0} / edges ${edges.length} / communities ${communities.size}; singleton ${singles.length}개${singles.length ? ": " + singles.slice(0,10).map(s => `\`${s}\``).join(", ") : ""}` };
72
73
  }
73
- export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport {
74
+ export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport { return lintDetails(snapshot, only).report; }
75
+ /** The report plus what only its markdown carries: the note count, check titles and per-check info lines, for the CLI text view. */
76
+ export function lintDetails(snapshot: AnalysisSnapshot, only?: string): { report: LintReport; notes: number; titles: Record<LintCode, string>; info: Partial<Record<LintCode, string>> } {
74
77
  const findings: Record<LintCode,string[]> = {L01:[],L02:[],L03:[],L09:[],L10:[],L11:[],L12:[],L13:[],L14:[]};
75
78
  const notes = [...snapshot.notes].sort((a,b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0).map(n => ({...n,...legacyDocument(n.source),stem: n.path.replace(/^notes\//, "").slice(0,-3)}));
76
79
  const stems = new Set(notes.map(n => n.stem)), inlinks = new Map(notes.map(n => [n.stem,0]));
@@ -88,6 +91,9 @@ export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport
88
91
  if (fm === null) { findings.L02.push(prefix + "frontmatter 없음/파싱 실패"); continue; }
89
92
  if (fm.status === "superseded") superseded++;
90
93
  if (!TYPES.includes(String(fm.type))) findings.L02.push(prefix + `\`type: ${py(fm.type)}\` 허용값 아님 (${TYPES.join(" | ")})`);
94
+ if (fm.domain !== undefined && (typeof fm.domain !== "string" || !(snapshot.domains ?? BUILTIN_DOMAINS).includes(fm.domain))) {
95
+ findings.L02.push(prefix + `\`domain: ${py(fm.domain)}\` 허용값 아님 (${(snapshot.domains ?? BUILTIN_DOMAINS).join(" | ")}); vmem.config.json의 이 vault \`domains\`에 id 추가 가능`);
96
+ }
91
97
  if (fm.type === "moc") { if (!truth(fm.description)) findings.L02.push(prefix + "`type: moc` 노트는 `description` 필수"); }
92
98
  else if (!truth(fm.topics)) findings.L02.push(prefix + "`topics` 없음/빈 배열");
93
99
  for (const [key,allowed] of [["confidence",CONFIDENCE],["status",STATUS]] as const) if (fm[key] !== undefined && fm[key] !== null && !allowed.includes(py(fm[key]))) findings.L02.push(prefix + `\`${key}: ${py(fm[key])}\` 허용값 아님 (${allowed.join(" | ")})${key === "status" ? " (status는 노트 생애주기이지 사업 진행 상태가 아님 — 작업 상태는 본문에)" : ""}`);
@@ -131,5 +137,5 @@ export function lintVault(snapshot: AnalysisSnapshot, only?: string): LintReport
131
137
  if (extra[code]) lines.push("",extra[code]!);
132
138
  lines.push("");
133
139
  }
134
- return { findings, errors, warnings, markdown: lines.join("\n"), summary };
140
+ return { report: { findings, errors, warnings, markdown: lines.join("\n"), summary }, notes: notes.length, titles: CODE_TITLES, info: extra };
135
141
  }
package/src/vault-log.ts CHANGED
@@ -2,6 +2,7 @@ import { compileLogFilter } from "./log-filter.ts";
2
2
  import { closeSync, constants, fstatSync, fsyncSync, ftruncateSync, lstatSync, openSync, readSync, realpathSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
  import { hostname } from "node:os";
5
+ import { createHash } from "node:crypto";
5
6
  import { loadRegistry, syncDirectory } from "./registry.ts";
6
7
  import { canonicalPath, isDenied } from "./platform.ts";
7
8
  import { formatLogEvent, LOG_TYPES } from "./log-format.ts";
@@ -262,3 +263,99 @@ export function readLog(home: string, options: LogReadOptions): LogReadResult {
262
263
  }
263
264
  return result;
264
265
  }
266
+
267
+ /** Bytes a cursor poll may read forward; a larger gap answers with the latest rows and `reset: true`. */
268
+ export const MAX_LOG_CURSOR_GAP = 1024 * 1024;
269
+ /** Bytes read from the end of the log for a first call or a reset. */
270
+ const LOG_TAIL_WINDOW = 256 * 1024;
271
+ const CURSOR_ANCHOR = 256;
272
+ export interface LogSinceResult { ok: boolean; lines: string[]; cursor: string; reset: boolean; more: boolean; warnings: string[]; issues: WriteIssue[] }
273
+ /**
274
+ * Incremental tail for pollers. The cursor is opaque: base64url of `v1.<offset>.<sha256 of up to 256 bytes before offset>`,
275
+ * where offset is the end of the last complete line read. Rows come from the byte range after it, so identical lines
276
+ * are kept apart; a cursor past the end, with a different anchor (rewritten or truncated log) or more than
277
+ * MAX_LOG_CURSOR_GAP behind answers with the latest rows and `reset: true`. Every call reads a bounded range.
278
+ */
279
+ export function readLogSince(home: string, options: { vault: string; after?: string; type?: string; limit?: number }): LogSinceResult {
280
+ const result: LogSinceResult = { ok: false, lines: [], cursor: "", reset: false, more: false, warnings: [], issues: [] };
281
+ let fd: number | undefined;
282
+ try {
283
+ const limit = options.limit ?? 100;
284
+ if (!Number.isInteger(limit) || limit < 1 || limit > 200) refuse("L09", "log limit must be an integer 1..200");
285
+ if (options.type !== undefined && !(LOG_TYPES as readonly string[]).includes(options.type)) refuse("L09", "unknown log type filter");
286
+ if (options.after !== undefined && (typeof options.after !== "string" || options.after.length > 256)) refuse("L09", "invalid log cursor");
287
+ const ctx = context(home, options.vault, undefined, false), file = join(ctx.vault.path, "log");
288
+ try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
289
+ catch (caught) { if ((caught as NodeJS.ErrnoException).code === "ENOENT") { result.ok = true; result.cursor = encodeCursor(0, Buffer.alloc(0)); result.reset = options.after !== undefined && options.after !== result.cursor; return result; } throw caught; }
290
+ const stat = fstatSync(fd);
291
+ if (!stat.isFile()) refuse("L09", "log must be a plain file");
292
+ const size = stat.size, read = (start: number, end: number) => {
293
+ const out = Buffer.alloc(end - start);
294
+ for (let done = 0; done < out.length;) { const n = readSync(fd!, out, done, out.length - done, start + done); if (!n) refuse("LOG_READ", "log changed while reading"); done += n; }
295
+ return out;
296
+ };
297
+ const anchor = (offset: number) => read(Math.max(0, offset - CURSOR_ANCHOR), offset);
298
+ let start: number | undefined;
299
+ if (options.after !== undefined) {
300
+ const at = decodeCursor(options.after);
301
+ if (at && at.offset <= size && size - at.offset <= MAX_LOG_CURSOR_GAP && hashOf(anchor(at.offset)) === at.hash) start = at.offset;
302
+ else result.reset = true;
303
+ }
304
+ let skipFirst = false;
305
+ if (start === undefined) { start = Math.max(0, size - LOG_TAIL_WINDOW); skipFirst = start > 0 && read(start - 1, start)[0] !== 0x0a; }
306
+ const bytes = read(start, size);
307
+ let from = 0;
308
+ if (skipFirst) {
309
+ const nl = bytes.indexOf(0x0a);
310
+ if (nl < 0) {
311
+ // The whole window is inside one line: point the cursor at the newline before it (scanning back at most
312
+ // MAX_LOG_CURSOR_GAP), or at offset 0, which is a line start and, that far behind, always answers with a reset.
313
+ let boundary = 0;
314
+ for (let at = start; at > 0 && start - at < MAX_LOG_CURSOR_GAP;) {
315
+ const chunk = read(Math.max(0, at - 64 * 1024), at), i = chunk.lastIndexOf(0x0a);
316
+ if (i >= 0) { boundary = at - chunk.length + i + 1; break; }
317
+ at -= chunk.length;
318
+ }
319
+ result.warnings.push("historical log line over 4096 bytes omitted");
320
+ result.cursor = encodeCursor(boundary, anchor(boundary));
321
+ result.ok = true;
322
+ return result;
323
+ }
324
+ from = nl + 1;
325
+ }
326
+ const lastNl = bytes.lastIndexOf(0x0a), end = lastNl >= from ? lastNl + 1 : from;
327
+ if (end < bytes.length) result.warnings.push("incomplete final log line not yet read");
328
+ const decoder = new TextDecoder("utf-8", { fatal: true });
329
+ // A poll from a valid cursor delivers the oldest `limit` new rows and stops its cursor after the last one, so the
330
+ // next poll continues there; a first call or a reset shows the newest rows and moves the cursor to the end.
331
+ const incremental = options.after !== undefined && !result.reset;
332
+ let cursorAt = start + end;
333
+ for (let at = from; at < end;) {
334
+ const nl = bytes.indexOf(0x0a, at), next = nl + 1;
335
+ const raw = decoder.decode(bytes.subarray(at, nl)), line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
336
+ at = next;
337
+ if (!line.trim() || line.trimStart().startsWith("#")) continue;
338
+ if (Buffer.byteLength(line) > MAX_READ_LINE_BYTES) { if (!result.warnings.includes("historical log line over 4096 bytes omitted")) result.warnings.push("historical log line over 4096 bytes omitted"); continue; }
339
+ const type = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\s+(\S+)/.exec(line)?.[1];
340
+ if (options.type && options.type !== type) continue;
341
+ if (incremental && result.lines.length === limit) { cursorAt = start + bytes.lastIndexOf(0x0a, at - 2) + 1; result.more = true; result.warnings.push(`more rows follow; poll again with the returned cursor`); break; }
342
+ result.lines.push(line);
343
+ }
344
+ if (result.lines.length > limit) { result.lines = result.lines.slice(-limit); result.warnings.push(`only the newest ${limit} rows returned`); }
345
+ result.cursor = encodeCursor(cursorAt, anchor(cursorAt));
346
+ result.ok = true;
347
+ } catch (caught) {
348
+ result.lines = [];
349
+ if (caught instanceof LogRefused) result.issues.push(...caught.issues);
350
+ else result.issues.push({ code: "LOG_READ", severity: "error", message: "cannot read plain UTF-8 log" });
351
+ } finally {
352
+ if (fd !== undefined) { try { closeSync(fd); } catch { result.ok = false; result.issues.push({ code: "LOG_CLOSE", severity: "error", message: "log descriptor close failed" }); } }
353
+ }
354
+ return result;
355
+ }
356
+ const hashOf = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
357
+ function encodeCursor(offset: number, before: Buffer): string { return Buffer.from(`v1.${offset}.${hashOf(before)}`).toString("base64url"); }
358
+ function decodeCursor(cursor: string): { offset: number; hash: string } | undefined {
359
+ const m = /^v1\.(\d{1,15})\.([0-9a-f]{64})$/.exec(Buffer.from(cursor, "base64url").toString("latin1"));
360
+ return m ? { offset: Number(m[1]), hash: m[2]! } : undefined;
361
+ }
@@ -16,6 +16,13 @@ const FRONTMATTER = /^---\s*\n([\s\S]*?)\n---\s*\n/;
16
16
  const WIKI = /\[\[([^\]|]+?)(?:\|[^\]]+?)?\]\]/g;
17
17
  const PRIORITY = { wiki_link: 0, fm_ref: 1, bu_member: 2, source_ref: 3, related: 4, supersedes: 5 };
18
18
  export type WorkCounters = Record<"work_docs" | "yaml_fail" | "template_skip" | "related_unresolved" | "related_ambiguous" | "related_dir_skip" | "related_root_escape" | "related_home_escape" | "source_stub" | "source_dead" | "source_nonmd" | "source_skiproot" | "collisions" | "dup_edges", number>;
19
+ /**
20
+ * Edge attributes `scripts/workspace-parity.ts` compares with `build-graph.py --work-root`. The Python reference puts no
21
+ * `rel` on wiki_link edges; vmem keeps the `vmem link --relation` type there, so that `rel` is outside parity.
22
+ */
23
+ export function workspaceParityEdgeKey(e: Record<string, unknown>): string {
24
+ return (["relation", "field", "rel"] as const).map(a => e[a] === undefined || (a === "rel" && e.relation === "wiki_link") ? "" : String(e[a])).join("|");
25
+ }
19
26
  export function workSkipped(path: string, roots: string[]): boolean {
20
27
  const normalized = path.normalize("NFC").replace(/^\/+|\/+$/g, "");
21
28
  return roots.some(root => normalized === root || normalized.startsWith(root + "/"));
@@ -129,7 +136,8 @@ export function buildWorkspaceGraph(snapshot: AnalysisSnapshot, work: WorkspaceS
129
136
  if (!current || PRIORITY[edge.relation] > PRIORITY[current.relation]) best.set(key, edge);
130
137
  }
131
138
  const edges = new Map(graph.edges.map(edge => [pair(edge), edge]));
132
- for (const [key, edge] of best) edges.set(key, { ...edges.get(key), ...edge });
139
+ // A vault wiki link's `rel` belongs to that relation only: a higher-priority work edge on the pair drops it.
140
+ for (const [key, edge] of best) { const prior = edges.get(key); edges.set(key, prior && prior.relation === edge.relation ? { ...prior, ...edge } : edge); }
133
141
  graph.edges = [...edges.values()];
134
142
  if (graph.nodes.length > 20000 || graph.edges.length > 100000) throw new Error("workspace graph: node or edge bound exceeded");
135
143
  finalizeGraph(graph);
@@ -10,6 +10,8 @@ import type { LogEvent } from "./log-types.ts";
10
10
  import { LOG_EVENT_SCHEMA } from "./log-format.ts";
11
11
  import { argumentError } from "./mcp.ts";
12
12
  import { parseTidyMap, tidyVault, TIDY_RULES } from "./tidy.ts";
13
+ import { PLAIN, type TextOptions } from "./text-output.ts";
14
+ import { renderTidy, renderWriteReceipt } from "./write-output.ts";
13
15
  const COMMANDS = new Set(["new", "patch", "append", "frontmatter", "link", "supersede", "move", "delete", "tidy", "report", "check"]);
14
16
  export function isWriteCommand(command: string): boolean { return COMMANDS.has(command); }
15
17
  function object(text: string, label: string): Record<string, unknown> {
@@ -41,7 +43,7 @@ function logOption(text: string | undefined): { log?: LogEvent } {
41
43
  if (error) throw new Error(`${error}; expected {"type", "project", "source", "note"[, "dest"]} as for log append`);
42
44
  return { log: value as LogEvent };
43
45
  }
44
- export function runWriteCommand(command: string, args: string[], home: string, env: NodeJS.ProcessEnv = process.env): { text: string; code: number } {
46
+ export function runWriteCommand(command: string, args: string[], home: string, env: NodeJS.ProcessEnv = process.env, text: TextOptions = PLAIN): { text: string; code: number } {
45
47
  const valueOptions: Record<string, string[]> = {
46
48
  new: ["--type", "--domain", "--topics", "--description", "--title", "--source", "--confidence", "--status", "--extra", "--body"],
47
49
  patch: ["--old", "--new", "--edits", "--expected-sha"], append: ["--section", "--markdown", "--expected-sha"],
@@ -81,7 +83,7 @@ export function runWriteCommand(command: string, args: string[], home: string, e
81
83
  if (flags["--dry-run"] && flags["--confirm"]) throw new Error("tidy: --dry-run and --confirm are exclusive");
82
84
  const mapText = optional("--map") !== undefined ? input(required("--map"), home) : undefined;
83
85
  const result = tidyVault(home, { vault, rules, ...(mapText !== undefined ? { map: parseTidyMap(mapText), mapText } : {}), confirm: flags["--confirm"] === true && !flags["--dry-run"], fingerprint: optional("--fingerprint"), channel: "cli" }, { env });
84
- return { text: JSON.stringify(result, null, flags["--json"] ? undefined : 2), code: result.ok ? 0 : 1 };
86
+ return { text: flags["--json"] ? JSON.stringify(result) : renderTidy(result, text, { argv: [command, ...args], positional }), code: result.ok ? 0 : 1 };
85
87
  }
86
88
  let operation: WriteOp;
87
89
  switch (command) {
@@ -115,5 +117,5 @@ export function runWriteCommand(command: string, args: string[], home: string, e
115
117
  }
116
118
  const request: WriteRequest = { vault, operation, channel: "cli", dryRun: flags["--dry-run"] === true, confirm: flags["--confirm"] === true, fingerprint: optional("--fingerprint"), ...logOption(optional("--log")) };
117
119
  const result = applyWrite(home, request, { env });
118
- return { text: JSON.stringify(result, null, flags["--json"] ? undefined : 2), code: result.ok && result.log?.ok !== false ? 0 : 1 };
120
+ return { text: flags["--json"] ? JSON.stringify(result) : renderWriteReceipt(result, text, { argv: [command, ...args], positional }), code: result.ok && result.log?.ok !== false ? 0 : 1 };
119
121
  }
@@ -1,7 +1,7 @@
1
1
  import { isDeepStrictEqual } from "node:util";
2
2
  import { normalizeSource } from "./source.ts";
3
3
  import { parseFrontmatter, emitFrontmatter } from "./frontmatter.ts";
4
- import { normalizeTarget, proseLines, rewriteLinkTargets, extractLinks, resolveLinks, type LinkNote } from "./links.ts";
4
+ import { LINK_RELATIONS, normalizeTarget, proseLines, rewriteLinkTargets, extractLinks, resolveLinks, type LinkNote } from "./links.ts";
5
5
  import type { PatchEdit, PlannedChange, WriteIssue, WriteOp } from "./write-types.ts";
6
6
  export class WriteRefusal extends Error {
7
7
  constructor(readonly issues: WriteIssue[]) { super(issues[0]?.message ?? "write refused"); }
@@ -119,11 +119,32 @@ function bodyAlreadyLinks(notes:Map<string,string>,from:string,to:string):boolea
119
119
  }catch{ /* Unrelated malformed legacy YAML cannot resolve an alias. */ }
120
120
  return resolveLinks(metadata,new Map([[from,references]])).some(link=>link.source===from&&link.resolved===to);
121
121
  }
122
+ /** Conservative lexical fast path; escaped, tagged or anchored YAML keys still take semantic parsing. */
123
+ function mayHaveDoi(source: string): boolean {
124
+ const closing = /^(?:---|\.\.\.)[ \t]*\r?$/gm;
125
+ closing.lastIndex = source.indexOf("\n") + 1;
126
+ const header = source.slice(0, closing.exec(source)?.index ?? source.length);
127
+ return /(?:^|[{\n,])[ \t]*(?:doi|["']doi["'])[ \t]*:|[\\!&*?]/.test(header);
128
+ }
122
129
  /** Plan transformations in memory. This function never touches files. */
123
130
  export function planOperation(operation: WriteOp, notes: Map<string, string>, reportBefore?: string | null): PlannedChange[] {
124
131
  const planned = new Map<string, PlannedChange>();
125
132
  const set = (path: string, after: string | null) => {
126
133
  const before = notes.get(path) ?? (operation.op === "report" ? reportBefore ?? null : null);
134
+ if (after === before) return;
135
+ if (after !== null && operation.op !== "report" && mayHaveDoi(after)) {
136
+ try {
137
+ const previousText = operation.op === "move" && path === notePath(operation.to)
138
+ ? needed(notes, moveSourcePath(operation.from, notes)) : before;
139
+ const doc = parseFrontmatter(after), previous = previousText === null ? undefined : parseFrontmatter(previousText).data;
140
+ // Normalize newly assigned identifiers; edits and moves leave untouched legacy metadata byte-identical.
141
+ if (doc.data.type === "reference" && typeof doc.data.doi === "string" &&
142
+ (previous?.type !== "reference" || previous.doi !== doc.data.doi ||
143
+ (operation.op === "frontmatter" && Object.hasOwn(operation.set, "doi")))) {
144
+ after = emitFrontmatter(doc, { doi: doc.data.doi.toLowerCase() });
145
+ }
146
+ } catch { /* Invalid YAML is reported by validation, without obscuring its named issue. */ }
147
+ }
127
148
  if (after !== before) planned.set(path, { path, before, after });
128
149
  };
129
150
  const existing = (value: string) => { const path = notePath(value); return { path, source: needed(notes, path) }; };
@@ -181,6 +202,8 @@ export function planOperation(operation: WriteOp, notes: Map<string, string>, re
181
202
  for (const key of operation.unset ?? []) changes[key] = undefined;
182
203
  try {
183
204
  const before = document(source, path);
205
+ // An explicit equal legacy DOI assignment still canonicalizes before the unchanged-text fast path.
206
+ if ((Object.hasOwn(changes, "type") ? changes.type : before.data.type) === "reference" && typeof changes.doi === "string") changes.doi = changes.doi.toLowerCase();
184
207
  let after = emitFrontmatter(before, normalizedSourceChanges(changes));
185
208
  if (Object.hasOwn(operation.set, "topics") && !isDeepStrictEqual(changes.topics, before.data.topics) && Array.isArray(changes.topics) && changes.topics.every((value): value is string => typeof value === "string")) {
186
209
  const doc = document(after, path);
@@ -192,7 +215,7 @@ export function planOperation(operation: WriteOp, notes: Map<string, string>, re
192
215
  }
193
216
  case "link": {
194
217
  const from = existing(operation.from), to = existing(operation.to);
195
- if (!["supports", "contradicts", "extends", "applies-to", "context-for"].includes(operation.relation)) refuse("V01", "invalid relation");
218
+ if (!(LINK_RELATIONS as readonly string[]).includes(operation.relation)) refuse("V01", "invalid relation");
196
219
  if (operation.context !== undefined && (typeof operation.context !== "string" || /[\r\n]/.test(operation.context) || operation.context.length > 500)) refuse("V01", "link context must be one line <=500 characters");
197
220
  if (from.path === to.path) refuse("V01", "link endpoints must differ");
198
221
  const line = (path: string) => `- [[${path.slice(6, -3)}]] (${operation.relation})${operation.context ? `: ${operation.context}` : ""}`;
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Text renderers for writes, two-step previews and promotion (#114): one result line, then changes, issues, the
3
+ * log receipt and, for a preview, the exact confirm command. Note bodies and drafts are never printed; `--json` has them.
4
+ */
5
+ import type { ChangeReceipt, WriteIssue, WriteReceipt } from "./write-types.ts";
6
+ import type { LogReceipt } from "./log-types.ts";
7
+ import type { TidyResult } from "./tidy.ts";
8
+ import type { OrgPromoteResult } from "./org-promote.ts";
9
+ import type { SyncResult } from "./sync.ts";
10
+ import type { PromoteResult } from "./promote.ts";
11
+ 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
+
14
+ /** The words the command was run with (`[command, ...args]`, no `--color`) and its positional arguments. */
15
+ export interface WriteContext { argv: string[]; positional: string[] }
16
+ type Candidates = { projects: { project: string; candidates: { id: string; project: string; kind: string; title: string | null; at: string }[] }[] };
17
+
18
+ const line = (text: string, o: TextOptions, t?: Tone) => paragraph(text, o, t);
19
+ const list = (label: string, items: readonly string[] | undefined) => items?.length ? [`${label}:`, ...items.map(item => ` - ${clean(item)}`)].join("\n") : undefined;
20
+ const short = (sha: string | null) => sha ? sha.slice(0, 12) : "-";
21
+ const action = (c: ChangeReceipt) => c.beforeSha === null ? "create" : c.sha256 === null ? "delete" : "update";
22
+ const errors = (issues: readonly WriteIssue[]) => issues.filter(issue => issue.severity === "error").length;
23
+ const stem = (target: string | undefined) => { try { return notePath(target ?? ""); } catch { return target ?? "?"; } };
24
+
25
+ /** The preview's own words with `--confirm --fingerprint <sha>`, quoted so it can be pasted. */
26
+ export function confirmCommand(argv: readonly string[], fingerprint: string, drop: readonly string[] = []): string {
27
+ const kept: string[] = [];
28
+ for (let i = 0; i < argv.length; i++) {
29
+ const word = argv[i]!;
30
+ if (word === "--fingerprint") { i++; continue; }
31
+ if (!["--json", "--dry-run", "--confirm", ...drop].includes(word)) kept.push(word);
32
+ }
33
+ return ["vmem", ...kept, "--confirm", "--fingerprint", fingerprint].map(shellWord).join(" ");
34
+ }
35
+ function logLine(log: LogReceipt | undefined, o: TextOptions, label = "log"): string | undefined {
36
+ if (!log) return undefined;
37
+ if (log.ok && log.appended) return line(`${label}: appended${log.bytesWritten !== null ? ` (${plural(log.bytesWritten, "byte")})` : ""}${log.synced ? "" : ", not yet synced to disk"}`, o);
38
+ return blocks(line(`${label}: not appended${log.rolledBack ? " (rolled back)" : ""}`, o, "fail"), issueTable(log.issues, o));
39
+ }
40
+
41
+ const VERBS: Record<string, [string, string]> = {
42
+ 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"],
44
+ };
45
+ function what(command: string, changes: ChangeReceipt[], targets: string[], past: boolean): string {
46
+ const verb = VERBS[command]?.[past ? 1 : 0] ?? command;
47
+ const others = (paths: string[], how: string) => paths.length ? ` and ${how} ${plural(paths.length, "note")}` : "";
48
+ const deleted = changes.filter(c => action(c) === "delete").map(c => c.path), created = changes.filter(c => action(c) === "create").map(c => c.path);
49
+ const updated = changes.filter(c => action(c) === "update").map(c => c.path);
50
+ 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")}`;
51
+ if (command === "delete") return `${verb} ${deleted[0] ?? stem(targets[0])}${others(updated, past ? "updated" : "update")}`;
52
+ if (command === "link") return `${verb} ${stem(targets[0])} and ${stem(targets[1])}`;
53
+ if (command === "supersede") return `${verb} ${stem(targets[0])} with ${stem(targets[1])}`;
54
+ return `${verb} ${changes[0]?.path ?? (command === "report" ? targets[0] : stem(targets[0]))}`;
55
+ }
56
+
57
+ /** `new`, `patch`, `append`, `frontmatter`, `link`, `supersede`, `move`, `delete`, `report` and `check`. */
58
+ export function renderWriteReceipt(r: WriteReceipt, o: TextOptions, ctx: WriteContext): string {
59
+ const command = ctx.argv[0]!, targets = ctx.positional.slice(1), issues = issueTable(r.issues, o);
60
+ if (command === "check") {
61
+ const path = r.changes[0]?.path ?? r.issues.find(issue => issue.path)?.path ?? "the note", e = errors(r.issues), w = r.issues.length - e;
62
+ const verdict = r.ok ? `ok: ${path} passes validation${w ? ` (${plural(w, "warning")})` : ""}` : `fail: ${path} has ${plural(e, "error")}${w ? ` and ${plural(w, "warning")}` : ""}`;
63
+ return blocks(issues, line(verdict, o, r.ok ? "ok" : "fail"));
64
+ }
65
+ const twoStep = command === "move" || command === "delete";
66
+ let head: string, t: Tone;
67
+ if (!r.ok) [head, t] = r.applied ? [`failed after writing: ${what(command, r.changes, targets, true)}; see the issues`, "fail"] : ["refused: nothing written", "fail"];
68
+ else if (r.preview) [head, t] = [`dry run: would ${what(command, r.changes, targets, false)}`, "info"];
69
+ else if (r.unchanged) [head, t] = ["unchanged: links already present", "muted"];
70
+ else if (!r.applied) [head, t] = ["unchanged: nothing to write", "muted"];
71
+ else [head, t] = [what(command, r.changes, targets, true), "ok"];
72
+ 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;
74
+ 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
+ const confirm = twoStep && r.ok && r.preview && r.fingerprint ? nextCommand("confirm", confirmCommand(ctx.argv, r.fingerprint), o) : undefined;
76
+ return blocks([line(head, o, t), one].filter(Boolean).join("\n"), changes, issues, list("possible duplicates", r.possibleDuplicates), log, confirm);
77
+ }
78
+
79
+ /** `tidy`: counts per rule and the notes touched, then the confirm command. */
80
+ export function renderTidy(r: TidyResult, o: TextOptions, ctx: WriteContext): string {
81
+ const vault = ctx.positional[0] ?? "?", notes = r.notes.length, logChanges = r.counts.log ?? 0;
82
+ const touched = [notes ? plural(notes, "note") : "", logChanges ? "the log" : ""].filter(Boolean).join(" and ");
83
+ let head: string, t: Tone;
84
+ if (!r.ok) [head, t] = r.applied ? [`failed after writing ${plural(r.written.length, "file")} in ${vault}; rerun to finish`, "fail"] : ["refused: nothing written", "fail"];
85
+ else if (!touched) [head, t] = [`unchanged: nothing to tidy in ${vault}`, "muted"];
86
+ else if (r.preview) [head, t] = [`dry run: would tidy ${touched} in ${vault}`, "info"];
87
+ else [head, t] = [`tidied ${touched} in ${vault}`, "ok"];
88
+ const counted = r.rules.filter(rule => (r.counts[rule] ?? 0) > 0);
89
+ const rules = counted.length ? table(["RULE", "CHANGES"], counted.map(rule => [rule, r.counts[rule]!]), o) : undefined;
90
+ const noteTable = notes ? table(["NOTE", "CHANGES"], r.notes.map(note => [note.path, note.changes.length]), o) : undefined;
91
+ const reported = r.reported.length ? table(["LEFT UNCHANGED", "REASON"], r.reported.map(item => [item.path, item.message]), o) : undefined;
92
+ const log = r.log && (r.log.redactions || r.log.formats) ? line(`log: ${plural(r.log.redactions, "redaction")} on ${plural(r.log.lines, "line")}, ${plural(r.log.formats, "format fix", "format fixes")}`, o) : undefined;
93
+ const event = r.ok && r.applied && r.event ? line(`logged: LINT ${r.event}`, o) : undefined;
94
+ const confirm = r.ok && r.preview && touched && r.fingerprint ? nextCommand("confirm", confirmCommand(ctx.argv, r.fingerprint), o) : undefined;
95
+ return blocks(line(head, o, t), rules, noteTable, reported, issueTable(r.issues, o), log, event, confirm);
96
+ }
97
+
98
+ /** `promote <vault> <note> --to <org>`. */
99
+ export function renderOrgPromote(r: OrgPromoteResult, o: TextOptions, ctx: WriteContext): string {
100
+ const source = r.source ?? ctx.positional[1] ?? "?", target = r.target ?? "the org vault";
101
+ let head: string, t: Tone;
102
+ if (!r.ok) [head, t] = r.applied ? [`promoted ${source} to ${target} with problems; see the issues`, "fail"] : ["refused: nothing written", "fail"];
103
+ else if (r.preview) [head, t] = [r.recovery ? `dry run: would record on ${source} that ${target} already holds it` : `dry run: would promote ${source} to ${target}`, "info"];
104
+ else [head, t] = [r.recovery ? `recorded on ${source} that ${target} holds it` : `promoted ${source} to ${target}`, "ok"];
105
+ const seen = new Set<string>(), issues = [...r.issues, ...r.screen.issues, ...(r.lint?.issues ?? [])].filter(issue => !seen.has(JSON.stringify(issue)) && !!seen.add(JSON.stringify(issue)));
106
+ 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
+ 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
+ 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);
110
+ }
111
+
112
+ /** `sync <vault>`. */
113
+ export function renderSync(r: SyncResult, o: TextOptions, ctx: WriteContext): string {
114
+ const branch = r.branch ?? "?", origin = r.remote.origin ?? r.remote.configured ?? "origin";
115
+ const work = [r.paths.length ? `commit ${plural(r.paths.length, "path")}` : "", r.behind ? `rebase onto origin/${branch}` : "", r.paths.length || r.ahead ? `push ${branch} to ${origin}` : ""].filter(Boolean).join(", ");
116
+ let head: string, t: Tone;
117
+ if (!r.ok) [head, t] = r.commit ? [`committed ${short(r.commit)} in ${r.vault}; not pushed`, "fail"] : ["refused: nothing synced", "fail"];
118
+ else if (r.preview) [head, t] = work ? [`dry run: would ${work}`, "info"] : [`unchanged: ${r.vault} has nothing to sync`, "muted"];
119
+ else if (!r.applied) [head, t] = [`unchanged: ${r.vault} is up to date with origin/${branch}`, "muted"];
120
+ else [head, t] = [`synced ${r.vault}: ${r.commit ? `${short(r.commit)} ` : ""}pushed to ${origin} ${branch}`, "ok"];
121
+ const info = r.branch === null ? "" : fields([["vault", r.vault], ["branch", r.branch], ["remote", r.remote.configured], ["origin", r.remote.origin], ["ahead", r.ahead], ["behind", r.behind]], o);
122
+ const paths = r.paths.length ? table(["PATH"], r.paths.map(path => [path]), o) : undefined;
123
+ const confirm = r.ok && r.preview && work && r.fingerprint ? nextCommand("confirm", confirmCommand(ctx.argv, r.fingerprint), o) : undefined;
124
+ return blocks([line(head, o, t), info].filter(Boolean).join("\n"), paths, list("not synced (outside notes/, reports/ and log)", r.ignored), issueTable(r.issues, o), confirm);
125
+ }
126
+
127
+ /** `promote-from-amem <vault>`: the candidate table per project. */
128
+ export function renderCandidates(r: Candidates, o: TextOptions, ctx: WriteContext): string {
129
+ const vault = ctx.positional[0] ?? "?", found = r.projects.filter(p => p.candidates.length);
130
+ if (!found.length) return line("no promotion candidates", o, "muted");
131
+ const first = found[0]!.candidates[0]!;
132
+ return blocks(...found.map(p => [line(`${p.project}: ${plural(p.candidates.length, "candidate")}`, o, "bold"), table(["ID", "KIND", "AT", "TITLE"], p.candidates.map(c => [c.id, c.kind, c.at, c.title ?? "-"]), o)].join("\n")),
133
+ nextCommand("preview one", ["vmem", "promote-from-amem", vault, first.id, "--project", first.project].map(shellWord).join(" "), o));
134
+ }
135
+
136
+ /** `promote-from-amem <vault> <mem-id>`: preview or confirm one promotion; `edited` when `--edit` changed the draft. */
137
+ export function renderPromote(r: PromoteResult, o: TextOptions, ctx: WriteContext, edited = false): string {
138
+ const mem = `mem:${r.id}`, path = r.path ?? "a new note";
139
+ let head: string, t: Tone;
140
+ if (!r.ok) [head, t] = r.applied ? [`created ${path} from ${mem}, but the promotion is incomplete; see the issues`, "fail"] : ["refused: nothing written", "fail"];
141
+ else if (r.preview && r.action === "link") [head, t] = r.alreadyLinked ? [`unchanged: ${mem} already links ${path}`, "muted"] : [`dry run: would link ${mem} to ${path}`, "info"];
142
+ else if (r.preview) [head, t] = [`dry run: would create ${path} from ${mem}`, "info"];
143
+ else if (!r.applied) [head, t] = [`unchanged: ${mem} already links ${path}`, "muted"];
144
+ else [head, t] = [r.action === "link" ? `linked ${mem} to ${path}` : `created ${path} from ${mem}`, "ok"];
145
+ const draft = r.text !== undefined && r.preview ? line(`draft: ${plural(Buffer.byteLength(r.text), "byte")}; --json shows the text, --edit opens it in $EDITOR`, o, "muted") : undefined;
146
+ const info = fields([["linked", r.linked ?? ""]], o);
147
+ 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
+ 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),
150
+ logLine(r.note?.log, o), hint, command && nextCommand("confirm", edited ? command.replace(/ --confirm /, " --body <file> --confirm ") : command, o));
151
+ }
@@ -1,7 +1,7 @@
1
1
  import { isDeepStrictEqual } from "node:util";
2
2
  import { sourceReferences, sourceWorkspaceRoot, missingSourceWarnings } from "./source.ts";
3
3
  import { basename, resolve } from "node:path";
4
- import type { VaultSpec, VmemConfig } from "./config.ts";
4
+ import { domainProblem, type VaultSpec, type VmemConfig } from "./config.ts";
5
5
  import { parseFrontmatter } from "./frontmatter.ts";
6
6
  import { extractLinks, normalizeTarget, proseLines, resolveLinks, type Link, type LinkNote, type ResolvedLink } from "./links.ts";
7
7
  import { canonicalPath, isDenied } from "./platform.ts";
@@ -11,7 +11,6 @@ import { screenPii } from "./pii.ts";
11
11
  import { moveSourcePath } from "./write-operations.ts";
12
12
 
13
13
  const TYPES = new Set(["insight", "decision", "observation", "person", "project", "method", "moc", "reference"]);
14
- const DOMAINS = new Set(["research", "projects", "teaching", "operations", "people", "ai-practice"]);
15
14
  const CONFIDENCE = new Set(["proven", "likely", "experimental"]);
16
15
  const STATUS = new Set(["active", "superseded", "archived"]);
17
16
  const CATEGORY = new Set(["gap", "prompt", "workflow"]);
@@ -23,9 +22,11 @@ const KEYS = new Set([
23
22
  "promoted_from", "promoted_at", "promoted_to", "source_ref",
24
23
  // Adopted in the V2 cutover (#22): decision dates/owners and person contact fields.
25
24
  "decided_at", "decided_by", "email", "phone",
25
+ "citekey", "doi", "arxiv_id", "authors", "year", "venue",
26
26
  ]);
27
27
  /** Adopted keys valid only on one note type (contact fields additionally only in personal vaults, by PII policy). */
28
- export const TYPED_KEYS: Readonly<Record<string, string>> = { decided_at: "decision", decided_by: "decision", email: "person", phone: "person" };
28
+ export const TYPED_KEYS: Readonly<Record<string, string>> = { decided_at: "decision", decided_by: "decision", email: "person", phone: "person",
29
+ citekey: "reference", doi: "reference", arxiv_id: "reference", authors: "reference", year: "reference", venue: "reference" };
29
30
  export const SCHEMA_KEYS: ReadonlySet<string> = KEYS;
30
31
  const WIKI_FIELDS = ["topics", "project", "superseded_by", "supersedes", "source_ref"];
31
32
  // Exact regex set from @staix/agent-mem v0.17.0 src/memory/screen.ts; parity-tested against its root export.
@@ -143,29 +144,38 @@ function changedBrokenLinks(oldNotes: Map<string, ParsedNote>, newNotes: Map<str
143
144
  return issues;
144
145
  }
145
146
  interface SchemaIssue extends WriteIssue { fields: string[] }
146
- function schema(note: ParsedNote): SchemaIssue[] {
147
+ function schema(note: ParsedNote, vault: VaultSpec): SchemaIssue[] {
147
148
  const issues: SchemaIssue[] = [], { data, path } = note;
148
149
  const add=(field:string,message:string,dependencies=[field])=>issues.push({...issue("V01",message,"error",path),fields:dependencies});
149
- for(const key of Object.keys(data))if(!KEYS.has(key)||(TYPED_KEYS[key]!==undefined&&data.type!==TYPED_KEYS[key]))add(key,"unknown frontmatter key",[key,"type"]);
150
+ for(const key of Object.keys(data))if(!KEYS.has(key)||(TYPED_KEYS[key]!==undefined&&data.type!==TYPED_KEYS[key]))add(key,TYPED_KEYS[key] === "reference" ? `${key} is only valid on reference notes; set type: reference or unset ${key}` : "unknown frontmatter key",[key,"type"]);
150
151
  const description = data.description;
151
152
  if (typeof description !== "string" || !description.trim() || [...description].length > 200) add("description","description must contain 1..200 Unicode characters");
152
153
  else if ([note.title, note.h1, basename(path, ".md")].some(value => value && fold(description) === fold(value))) add("description","description must supplement the title");
153
154
  if (typeof data.type !== "string" || !TYPES.has(data.type)) add("type","invalid or missing note type");
154
155
  if (data.type !== "moc" || data.domain !== undefined) {
155
- if (typeof data.domain !== "string" || !DOMAINS.has(data.domain)) add("domain","invalid or missing domain");
156
+ const problem = domainProblem(data.domain, vault);
157
+ if (problem) add("domain", problem);
156
158
  }
157
159
  for (const [key, allowed] of [["confidence", CONFIDENCE], ["status", STATUS], ["category", CATEGORY]] as const) {
158
160
  if (data[key] !== undefined && (typeof data[key] !== "string" || !allowed.has(data[key]))) add(key,`invalid ${key} enum`);
159
161
  }
160
162
  if(data.source!==undefined)try{sourceReferences(data.source);}catch{add("source","source must contain workspace-relative paths, mem ids or URLs");}
163
+ if (data.type === "reference") {
164
+ if (data.citekey !== undefined && (typeof data.citekey !== "string" || !/^[A-Za-z0-9_:-]{1,64}(?![\s\S])/.test(data.citekey))) add("citekey", "citekey must match [A-Za-z0-9_:-]{1,64}");
165
+ if (data.doi !== undefined && (typeof data.doi !== "string" || !/^10\.[0-9]{4,9}\/[^\s\p{C}]+(?![\s\S])/u.test(data.doi))) add("doi", "doi must be 10.<4..9 digit registrant>/<nonempty suffix>, without whitespace or control characters");
166
+ if (data.arxiv_id !== undefined && (typeof data.arxiv_id !== "string" || !/^(?:(?:0[0-9]|1[0-4])(?:0[1-9]|1[0-2])\.[0-9]{4}|(?:1[5-9]|[2-9][0-9])(?:0[1-9]|1[0-2])\.[0-9]{5}|[a-z]+(?:[.-][A-Za-z]+)*\/[0-9]{2}(?:0[1-9]|1[0-2])[0-9]{3})(?:v[1-9][0-9]*)?(?![\s\S])/.test(data.arxiv_id))) add("arxiv_id", "arxiv_id must use 4 digits after YYMM. for YY < 15, 5 digits for YY >= 15, or archive/YYMMNNN; month 01..12 and optional vN (N >= 1)");
167
+ if (data.authors !== undefined && (!Array.isArray(data.authors) || data.authors.length < 1 || data.authors.length > 100 || data.authors.some(author => typeof author !== "string" || !author.trim()))) add("authors", "authors must be a list of 1..100 nonempty strings");
168
+ if (data.year !== undefined && (typeof data.year !== "number" || !Number.isInteger(data.year) || data.year < 1000 || data.year > 2100)) add("year", "year must be an integer from 1000 to 2100");
169
+ if (data.venue !== undefined && (typeof data.venue !== "string" || [...data.venue].length < 1 || [...data.venue].length > 200)) add("venue", "venue must contain 1..200 Unicode characters");
170
+ }
161
171
  return issues;
162
172
  }
163
173
  function schemaIssues(note:ParsedNote,old:ParsedNote|undefined,context:ValidationContext):WriteIssue[]{
164
174
  const op=context.operation;
165
175
  const explicit=op.op==="frontmatter"&&noteKey(op.path)===noteKey(note.path)?new Set(Object.keys(op.set)):new Set<string>();
166
176
  const value=(n:ParsedNote,key:string)=>n.data[key];
167
- const previous=old?schema(old):[];
168
- return schema(note).map(({fields,...finding})=>{
177
+ const previous=old?schema(old,context.vault):[];
178
+ return schema(note,context.vault).map(({fields,...finding})=>{
169
179
  const retained=op.op!=="create"&&old&&!fields.some(key=>explicit.has(key))&&fields.every(key=>isDeepStrictEqual(value(note,key),value(old,key)))&&previous.some(before=>before.message===finding.message&&isDeepStrictEqual(before.fields,fields));
170
180
  return {...finding,severity:retained?"warning":"error"};
171
181
  });
@@ -228,6 +238,11 @@ export function validateWrite(changes: PlannedChange[], context: ValidationConte
228
238
  }
229
239
  const sourceRoot=sourceWorkspaceRoot(context.vault);
230
240
  const oldNotes = parseNotes(context.notes), newNotes = parseNotes(final, oldNotes), oldNames = names(oldNotes), newNames = names(newNotes);
241
+ const citekeys = new Map<string, ParsedNote[]>();
242
+ for (const note of newNotes.values()) if (typeof note.data.citekey === "string") {
243
+ const peers = citekeys.get(note.data.citekey.toLowerCase()) ?? [];
244
+ peers.push(note); citekeys.set(note.data.citekey.toLowerCase(), peers);
245
+ }
231
246
  const candidates=[...changes];
232
247
  if(context.operation.op==="frontmatter"){
233
248
  const key=noteKey(context.operation.path),existing=[...newNotes.values()].find(note=>noteKey(note.path)===key);
@@ -239,6 +254,13 @@ export function validateWrite(changes: PlannedChange[], context: ValidationConte
239
254
  if (!note) { issues.push(issue("V01", "invalid frontmatter", "error", change.path)); continue; }
240
255
  const prior=context.operation.op==="move"&&noteKey(context.operation.to)===noteKey(note.path)?oldNotes.get(movedFrom(context.operation.from, context)):oldNotes.get(note.path);
241
256
  issues.push(...schemaIssues(note,prior,context), ...topicPlacement(note,prior,context));
257
+ if (typeof note.data.citekey === "string" && (citekeys.get(note.data.citekey.toLowerCase())?.length ?? 0) > 1) {
258
+ const explicit = context.operation.op === "frontmatter" && noteKey(context.operation.path) === noteKey(note.path) && Object.hasOwn(context.operation.set, "citekey");
259
+ const retained = context.operation.op !== "create" && !explicit && prior?.data.citekey === note.data.citekey &&
260
+ [...oldNotes.values()].some(other => other.path !== prior.path && typeof other.data.citekey === "string" && other.data.citekey.toLowerCase() === (note.data.citekey as string).toLowerCase());
261
+ const holder = citekeys.get(note.data.citekey.toLowerCase())!.find(other => other.path !== note.path)!;
262
+ issues.push(issue("V08", `citekey is already used by ${holder.path}; choose another citekey`, retained ? "warning" : "error", change.path));
263
+ }
242
264
  const source = note.data.source;
243
265
  let sourceDenied=strings(source).some(entry=>isDenied(entry.replaceAll("\\","/"),context.config.denyPaths));
244
266
  if(source!==undefined&&!sourceDenied)try{