@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/CHANGELOG.md +24 -0
- package/README.md +67 -15
- package/package.json +1 -1
- package/src/cli.ts +20 -4
- package/src/commands-table.ts +15 -12
- package/src/index.ts +6 -1
- package/src/log-format.ts +3 -0
- package/src/log-types.ts +5 -1
- package/src/mcp-spec.ts +68 -0
- package/src/mcp-vault.ts +27 -12
- package/src/spec-command-table.ts +15 -0
- package/src/spec-commands.ts +174 -0
- package/src/spec-freshness.ts +163 -0
- package/src/spec-git.ts +33 -0
- package/src/spec-graph.ts +19 -8
- package/src/spec-source.ts +53 -32
- package/src/spec-validate.ts +241 -0
- package/src/vault-files.ts +74 -0
- package/src/vault-index.ts +156 -38
- package/src/vault-log.ts +12 -1
- package/src/vault-stats.ts +50 -0
- package/src/write-commands.ts +27 -6
- package/src/write-operations.ts +23 -6
- package/src/write-types.ts +3 -1
- package/src/writer.ts +19 -7
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(
|
|
106
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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");
|