@staix/vault-mem 0.3.2 → 0.4.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/src/writer.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { appendLog, matchesBinding } from "./vault-log.ts";
1
+ import { appendLog, checkLogEvent, matchesBinding } from "./vault-log.ts";
2
2
  import type { LogEvent } from "./log-types.ts";
3
3
  import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readSync, readdirSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
4
4
  import { createHash, randomUUID } from "node:crypto";
@@ -6,7 +6,7 @@ import { dirname, join } from "node:path";
6
6
  import { hostname } from "node:os";
7
7
  import { Database } from "bun:sqlite";
8
8
  import { loadRegistry, syncDirectory } from "./registry.ts";
9
- import { VaultIndex, MAX_NOTE_BYTES } from "./vault-index.ts";
9
+ import { VaultIndex, MAX_NOTE_BYTES, GRAMS_COLUMNS, gramPhrase, grams, splitTerms } from "./vault-index.ts";
10
10
  import { parseFrontmatter } from "./frontmatter.ts";
11
11
  import { canonicalPath, isDenied } from "./platform.ts";
12
12
  import { moveSourcePath, notePath, planOperation, refuse, WriteRefusal } from "./write-operations.ts";
@@ -102,14 +102,20 @@ function duplicateWarnings(request: WriteRequest, notes: Map<string, string>, ch
102
102
  // Disposable FTS preserves dry-run's no-disk-write contract, including on a fresh home.
103
103
  const db = new Database(":memory:");
104
104
  try {
105
- db.run("CREATE VIRTUAL TABLE candidates USING fts5(path UNINDEXED,title,description,body,tokenize='unicode61')");
106
- const insert = db.query("INSERT INTO candidates VALUES (?,?,?,?)");
105
+ db.run(`CREATE VIRTUAL TABLE candidates USING fts5(path UNINDEXED,title,description,body,tokenize='unicode61');
106
+ CREATE VIRTUAL TABLE grams USING fts5(${GRAMS_COLUMNS},tokenize='unicode61');`);
107
+ const insert = db.query("INSERT INTO candidates VALUES (?,?,?,?)"), insertGrams = db.query("INSERT INTO grams VALUES (?,?,?,?)");
107
108
  for (const [path, source] of notes) {
108
109
  let value: ReturnType<typeof parseFrontmatter>;
109
110
  try { value = parseFrontmatter(source); } catch { continue; } // Unrelated legacy YAML is not a duplicate candidate.
110
- insert.run(path, typeof value.data.title === "string" ? value.data.title : path, typeof value.data.description === "string" ? value.data.description : "", value.body);
111
+ const title = typeof value.data.title === "string" ? value.data.title : path, description = typeof value.data.description === "string" ? value.data.description : "";
112
+ insert.run(path, title, description, value.body); insertGrams.run(path, grams(title), grams(description), grams(value.body));
111
113
  }
112
- const hits = db.query<{ path: string; score: number }, [string]>("SELECT path,bm25(candidates,0,5,2,1) AS score FROM candidates WHERE candidates MATCH ? ORDER BY score,path LIMIT 3").all(terms.map(term => `"${term}"`).join(" AND "));
114
+ // Same term split as VaultIndex.search: CJK terms match substrings through the bigram table.
115
+ const { tokens, runs } = splitTerms(terms);
116
+ const hits = runs.length
117
+ ? db.query<{ path: string; score: number }, string[]>(`SELECT path,bm25(grams,0,5,2,1) AS score FROM grams WHERE grams MATCH ? ${tokens.length ? "AND path IN (SELECT path FROM candidates WHERE candidates MATCH ?)" : ""} ORDER BY score,path LIMIT 3`).all(runs.map(gramPhrase).join(" AND "), ...(tokens.length ? [tokens.join(" AND ")] : []))
118
+ : db.query<{ path: string; score: number }, [string]>("SELECT path,bm25(candidates,0,5,2,1) AS score FROM candidates WHERE candidates MATCH ? ORDER BY score,path LIMIT 3").all(tokens.join(" AND "));
113
119
  return hits.filter(hit => hit.score <= -0.000001).map(hit => ({ code: "V08", severity: "warning", path: hit.path, message: "possible duplicate: FTS title terms match an existing note" }));
114
120
  } finally { db.close(); }
115
121
  }
@@ -136,6 +142,11 @@ export function applyWrite(home: string, request: WriteRequest, options: { env?:
136
142
  root = realpathSync(vault.path);
137
143
  if (root !== vault.path || isDenied(root, config.denyPaths)) refuse("V06", "vault path denied or changed");
138
144
  if (config.vaults.some(value => canonicalPath(home) === value.path || canonicalPath(home).startsWith(`${value.path}/`))) refuse("V04", "VMEM_HOME must be outside registered vaults");
145
+ // A given event is checked like `log append` before any lock or note change, so a bad event never leaves an unlogged write.
146
+ if (request.log !== undefined && request.log !== false && request.operation.op !== "check") {
147
+ const logIssues = checkLogEvent(home, { vault: request.vault, event: request.log, channel: request.channel }, options.env);
148
+ if (logIssues.length) throw new WriteRefusal(logIssues.map(issue => ({ ...issue, message: `log event refused, nothing written: ${issue.message}; fix the event fields (rules as for \`log append\`) or omit the event for the default line` })));
149
+ }
139
150
  const twoStep = ["move", "delete", "tidy"].includes(request.operation.op);
140
151
  const preview = request.operation.op === "check" || request.dryRun === true || (twoStep && request.confirm !== true);
141
152
  if (!preview) {
@@ -159,7 +170,8 @@ export function applyWrite(home: string, request: WriteRequest, options: { env?:
159
170
  for (const change of changes) if (isDenied(join(root, change.path), config.denyPaths)) refuse("V06", "planned mutation path is denied", change.path);
160
171
  issues = validateWrite(changes, { vault, config, notes, operation: request.operation, ...(options.piiSurface ? { piiSurface: options.piiSurface } : {}) });
161
172
  if (!issues.some(issue => issue.severity === "error")) issues.push(...duplicateWarnings(request, notes, changes));
162
- const input = { vault: request.vault, channel: request.channel ?? "cli", operation: request.operation, expectedShas: request.expectedShas ?? {}, log: request.log ?? null };
173
+ // The log event is not part of the fingerprint: move/delete take it on the confirm call only.
174
+ const input = { vault: request.vault, channel: request.channel ?? "cli", operation: request.operation, expectedShas: request.expectedShas ?? {} };
163
175
  const identity = lstatSync(root);
164
176
  // The identity every later commit check uses must still be the bound root, not a directory swapped in meanwhile.
165
177
  if (options.binding && (Number(identity.dev) !== options.binding.dev || Number(identity.ino) !== options.binding.ino)) refuse("VAULT_DRIFT", "vault root changed since the run started; nothing written");