@openparachute/vault 0.7.9-rc.4 → 0.7.9-rc.5

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/core/src/mcp.ts CHANGED
@@ -184,7 +184,7 @@ function requireNoteReference(value: unknown): string {
184
184
  * match required. Same [[wikilink]] semantics as `resolveWikilink`; exact
185
185
  * id/path always wins first.
186
186
  */
187
- function resolveNote(db: Database, idOrPath: string): Note | null {
187
+ export function resolveNote(db: Database, idOrPath: string): Note | null {
188
188
  // Try ID match first (fast, indexed)
189
189
  const byId = noteOps.getNote(db, idOrPath);
190
190
  if (byId) return byId;
@@ -692,6 +692,24 @@ export function generateMcpTools(store: Store, opts?: GenerateMcpToolsOpts): Mcp
692
692
  // stays byte-identical to the no-pagination behavior.
693
693
  const contentRange = parseContentRange(params.content_offset, params.content_length);
694
694
 
695
+ if (params.versions) {
696
+ for (const key of ["search", "near", "cursor", "aggregate", "semantic", "id"] as const) {
697
+ if (params[key] !== undefined) throw new QueryError(`versions is incompatible with ${key}`, "INVALID_QUERY", {
698
+ error_type: "invalid_query", field: "versions", hint: `drop ${key} when using versions`,
699
+ });
700
+ }
701
+ const v = params.versions as { note_id: string; version_ix?: number; limit?: number; offset?: number };
702
+ const note = requireNote(db, requireNoteReference(v.note_id));
703
+ if (typeof v.version_ix === "number") {
704
+ const version = await store.getNoteVersion(note.id, v.version_ix);
705
+ if (!version) return { error: `Version not found: "${v.note_id}"@${v.version_ix}`, error_type: "not_found", id: v.note_id, version_ix: v.version_ix };
706
+ return version;
707
+ }
708
+ const versions = await store.listNoteVersions(note.id, { limit: Math.max(0, Math.min(v.limit ?? 50, 200)), offset: v.offset ?? 0 });
709
+ const total = await store.countNoteVersions(note.id);
710
+ return { versions, total };
711
+ }
712
+
695
713
  // --- Single note by ID/path ---
696
714
  if (params.id) {
697
715
  const note = resolveNote(db, params.id as string);
@@ -2280,7 +2298,7 @@ export function generateMcpTools(store: Store, opts?: GenerateMcpToolsOpts): Mcp
2280
2298
  name: "delete-note",
2281
2299
  execute: async (params) => {
2282
2300
  const note = requireNote(db, requireNoteReference(params.id));
2283
- await store.deleteNote(note.id);
2301
+ await store.deleteNote(note.id, { actor: writeActor, via: writeVia });
2284
2302
  return { deleted: true, id: note.id };
2285
2303
  },
2286
2304
  },
@@ -2494,7 +2512,7 @@ export function generateMcpTools(store: Store, opts?: GenerateMcpToolsOpts): Mcp
2494
2512
  field: "new_name",
2495
2513
  });
2496
2514
  }
2497
- const result = await store.renameTag(oldName, newName);
2515
+ const result = await store.renameTag(oldName, newName, { actor: writeActor ?? undefined, via: writeVia ?? undefined });
2498
2516
  if ("error" in result) {
2499
2517
  if (result.error === "not_found") {
2500
2518
  throw structuredError(`rename-tag: tag "${oldName}" not found`, {
package/core/src/notes.ts CHANGED
@@ -31,6 +31,7 @@ import {
31
31
  type SearchMode,
32
32
  } from "./search-query.js";
33
33
  import { generateUlid } from "./ulid.js";
34
+ import { captureVersion, readPriorNoteRow, DEFAULT_HISTORY_POLICY, type HistoryPolicy } from "./history.js";
34
35
 
35
36
  /**
36
37
  * Write-attribution context (vault#298) — the two axes of provenance threaded
@@ -2390,7 +2391,7 @@ export type RenameTagResult =
2390
2391
  * (the store wrapper) bust both `_tagHierarchy` and `_schemaConfig`
2391
2392
  * after the cascade returns.
2392
2393
  */
2393
- export function renameTag(db: Database, oldName: string, newName: string): RenameTagResult {
2394
+ export function renameTag(db: Database, oldName: string, newName: string, attr?: { actor?: string | null; via?: string | null }, policy: HistoryPolicy = DEFAULT_HISTORY_POLICY): RenameTagResult {
2394
2395
  // Normalize the TARGET so a rename can never create a `#`-prefixed tag. The
2395
2396
  // SOURCE (`oldName`) is left LITERAL on purpose — it's the transitional escape
2396
2397
  // hatch that lets the `#legacy/*` → `legacy/*` data migration find the
@@ -2590,6 +2591,8 @@ export function renameTag(db: Database, oldName: string, newName: string): Renam
2590
2591
  for (const row of candidates) {
2591
2592
  const next = rewriteNoteBody(row.content, renames);
2592
2593
  if (next === row.content) continue;
2594
+ const prior = readPriorNoteRow(db, row.id);
2595
+ if (prior) captureVersion(db, prior, { actor: attr?.actor ?? null, via: attr?.via ?? null, op: "tag-rename", policy });
2593
2596
  updateStmt.run(next, now, nowMs, row.id);
2594
2597
  notesRewritten++;
2595
2598
  }
@@ -2612,6 +2615,8 @@ export function renameTag(db: Database, oldName: string, newName: string): Renam
2612
2615
  for (const row of candidates) {
2613
2616
  const next = rewriteTagConfigPath(row.path, renames);
2614
2617
  if (next === row.path) continue;
2618
+ const prior = readPriorNoteRow(db, row.id);
2619
+ if (prior) captureVersion(db, prior, { actor: attr?.actor ?? null, via: attr?.via ?? null, op: "tag-rename", policy });
2615
2620
  updateStmt.run(next, now, nowMs, row.id);
2616
2621
  pathsRenamed++;
2617
2622
  }
@@ -1961,7 +1961,7 @@ async function importVaultReplay(
1961
1961
  const batch = await store.queryNotes({ sort: "asc", limit: EXPORT_BATCH_SIZE });
1962
1962
  if (batch.length === 0) break;
1963
1963
  for (const note of batch) {
1964
- await store.deleteNote(note.id);
1964
+ await store.deleteNote(note.id, { captureHistory: false });
1965
1965
  stats.notes_wiped++;
1966
1966
  }
1967
1967
  }
@@ -0,0 +1,63 @@
1
+ import { it, expect, beforeEach, afterEach } from "bun:test";
2
+ import { Database } from "bun:sqlite";
3
+ import { initSchema } from "./schema.js";
4
+ import { BunSqliteStore } from "./store.js";
5
+ let db: Database;
6
+ beforeEach(() => {
7
+ db = new Database(":memory:");
8
+ initSchema(db);
9
+ });
10
+ afterEach(() => db.close());
11
+ function dropHistory() {
12
+ db.exec(
13
+ "DROP TABLE IF EXISTS note_versions; DROP TABLE IF EXISTS note_blobs;",
14
+ );
15
+ }
16
+ it("P14a v28 upgrade creates both tables and three indexes without backfill", () => {
17
+ dropHistory();
18
+ db.prepare("UPDATE schema_version SET version=28").run();
19
+ initSchema(db);
20
+ expect(
21
+ db.prepare("SELECT MAX(version) AS version FROM schema_version").get(),
22
+ ).toEqual({ version: 30 });
23
+ const tables = db
24
+ .prepare(
25
+ "SELECT name FROM sqlite_master WHERE type='table' AND name IN ('note_versions','note_blobs') ORDER BY name",
26
+ )
27
+ .all();
28
+ expect(tables).toEqual([{ name: "note_blobs" }, { name: "note_versions" }]);
29
+ expect(
30
+ db
31
+ .prepare(
32
+ "SELECT name FROM sqlite_master WHERE type='index' AND name IN ('idx_note_versions_note','idx_note_versions_hash','idx_note_versions_superseded')",
33
+ )
34
+ .all(),
35
+ ).toHaveLength(3);
36
+ expect(db.prepare("SELECT COUNT(*) AS n FROM note_versions").get()).toEqual({
37
+ n: 0,
38
+ });
39
+ });
40
+ it("P14b repeated init is idempotent", () => {
41
+ initSchema(db);
42
+ initSchema(db);
43
+ expect(db.prepare("SELECT COUNT(*) AS n FROM note_versions").get()).toEqual({
44
+ n: 0,
45
+ });
46
+ });
47
+ it("P14c rerunning the v29 migration preserves captured rows", async () => {
48
+ const store = new BunSqliteStore(db);
49
+ const n = await store.createNote("a");
50
+ await store.updateNote(n.id, { content: "b" });
51
+ db.prepare("UPDATE schema_version SET version=28").run();
52
+ initSchema(db);
53
+ expect((await store.getNoteVersion(n.id, 0))!.content).toBe("a");
54
+ });
55
+ it("P14d missing tables before the first capture degrade to no history", async () => {
56
+ const store = new BunSqliteStore(db);
57
+ const n = await store.createNote("a");
58
+ // Drop before this handle has probed true. A drop after capture is an
59
+ // accepted throw because historyTablesPresent memoises true per handle.
60
+ dropHistory();
61
+ await store.updateNote(n.id, { content: "b" });
62
+ expect((await store.getNote(n.id))!.content).toBe("b");
63
+ });
@@ -0,0 +1,51 @@
1
+ import { test, expect } from "bun:test";
2
+ import { Database } from "bun:sqlite";
3
+ import { BunSqliteStore } from "./store.js";
4
+ import { initSchema, SCHEMA_VERSION } from "./schema.js";
5
+ test("P12 v29 opens losslessly, fresh and upgraded columns/indexes agree", async () => {
6
+ const db = new Database(":memory:"), fresh = new Database(":memory:");
7
+ try {
8
+ const store = new BunSqliteStore(db);
9
+ const a = await store.createNote("one"), b = await store.createNote("other");
10
+ await store.updateNote(a.id, { content: "two" });
11
+ await store.updateNote(a.id, { content: "three" });
12
+ await store.deleteNote(a.id);
13
+ await store.updateNote(b.id, { content: "changed" });
14
+ // Remove only v30 additions to recreate the actual v29 table shape.
15
+ db.exec("DROP INDEX IF EXISTS idx_note_blobs_delta_of");
16
+ const cols = (table: string) => db.prepare(`PRAGMA table_info(${table})`).all() as {
17
+ name: string;
18
+ }[];
19
+ if (cols("note_blobs").some(c => c.name === "delta_of"))
20
+ db.exec("ALTER TABLE note_blobs DROP COLUMN delta_of");
21
+ if (cols("note_blobs").some(c => c.name === "encoding"))
22
+ db.exec("ALTER TABLE note_blobs DROP COLUMN encoding");
23
+ if (cols("note_versions").some(c => c.name === "created_at"))
24
+ db.exec("ALTER TABLE note_versions DROP COLUMN created_at");
25
+ db.exec("UPDATE schema_version SET version=29");
26
+ const snap = () => ({ blobs: db.prepare("SELECT hash,content,byte_size FROM note_blobs ORDER BY hash").all(), versions: db.prepare("SELECT note_id,version_ix,content_hash,path,metadata,extension,superseded_at,actor,via,op,content_len,encoding FROM note_versions ORDER BY note_id,version_ix").all() });
27
+ const before = snap();
28
+ expect(() => initSchema(db)).not.toThrow();
29
+ expect(SCHEMA_VERSION).toBe(30);
30
+ expect(snap()).toEqual(before);
31
+ expect(db.prepare("SELECT hash FROM note_blobs WHERE delta_of IS NOT NULL OR encoding IS NOT NULL").all()).toEqual([]);
32
+ expect(db.prepare("SELECT note_id FROM note_versions WHERE created_at IS NOT NULL").all()).toEqual([]);
33
+ initSchema(fresh);
34
+ for (const table of ["note_blobs", "note_versions"]) {
35
+ expect(cols(table).map(c => c.name).sort()).toEqual((fresh.prepare(`PRAGMA table_info(${table})`).all() as {
36
+ name: string;
37
+ }[]).map(c => c.name).sort());
38
+ }
39
+ for (const handle of [db, fresh])
40
+ expect(handle.prepare("SELECT name FROM sqlite_master WHERE name='idx_note_blobs_delta_of'").get()).not.toBeNull();
41
+ expect(db.prepare("PRAGMA integrity_check").get()).toEqual({ integrity_check: "ok" });
42
+ expect(db.prepare("PRAGMA foreign_key_check").all()).toEqual([]);
43
+ initSchema(db);
44
+ expect(snap()).toEqual(before);
45
+ expect(() => db.exec("UPDATE note_blobs SET delta_of='nosuchhash'")).toThrow("FOREIGN KEY");
46
+ }
47
+ finally {
48
+ db.close();
49
+ fresh.close();
50
+ }
51
+ });
@@ -6,7 +6,7 @@ import { transaction } from "./txn.js";
6
6
  import { timestampToMs } from "./cursor.js";
7
7
  import { ensureRelationshipColumn } from "./wikilinks.js";
8
8
 
9
- export const SCHEMA_VERSION = 28;
9
+ export const SCHEMA_VERSION = 30;
10
10
 
11
11
  /**
12
12
  * Deterministic last-resort epoch for a note whose `updated_at` AND
@@ -142,6 +142,114 @@ CREATE TABLE IF NOT EXISTS note_vectors (
142
142
  -- sweep (a model <> ? scan) the backfill/drain use to find pending work.
143
143
  CREATE INDEX IF NOT EXISTS idx_note_vectors_stale ON note_vectors(model, content_hash);
144
144
 
145
+ -- note_blobs + note_versions (v29, vault#524): note-level version history.
146
+ --
147
+ -- STORAGE. A version row is skinny; the BODY lives once per distinct content,
148
+ -- keyed by its sha256, in note_blobs. A metadata-only edit on a 116 KB note
149
+ -- costs one ~300-byte version row and a no-op \`INSERT OR IGNORE\` on the blob —
150
+ -- the whole answer to "does every little update store an entirely new copy?"
151
+ -- Live \`notes.content\` stays on \`notes\`: no join on the hot read path.
152
+ --
153
+ -- encoding IS NULL FOR EVERY ORDINARY ROW IN v29. NULL means "content_hash
154
+ -- names a WHOLE blob". It exists so the PR-2 compactor can rewrite an old row
155
+ -- to reference a delta (setting a format marker) with no second migration and
156
+ -- no version-id change. The ONE non-NULL value v29 ever writes is the literal
157
+ -- 'overflow', on a delete tombstone for a note whose body exceeded
158
+ -- VERSION_MAX_BYTES: the row records that the note WAS deleted and how big it
159
+ -- was, with content_hash NULL because the bytes were never blobbed. A delete
160
+ -- must never be blocked by history (see history.ts captureVersion step 5).
161
+ -- A restore marker copied from that tombstone also retains overflow encoding
162
+ -- and a NULL hash, including after the note has been recreated.
163
+ --
164
+ -- content_hash IS NULLABLE, and it is nullable ONLY for that overflow
165
+ -- tombstone or its copied restore marker. Every consumer must filter NULL —
166
+ -- in particular gcBlobs's \`NOT IN (SELECT content_hash ...)\` MUST carry
167
+ -- \`WHERE content_hash IS NOT NULL\`, or one NULL in the subquery makes the
168
+ -- whole NOT IN evaluate to NULL and the sweep silently deletes nothing.
169
+ --
170
+ -- NO \`REFERENCES notes(id) ON DELETE CASCADE\` — the one deviation from
171
+ -- note_vectors above, deliberate. deleteNote writes a final op='delete' row and
172
+ -- the \`notes\` row then goes away; a cascade would delete the only surviving
173
+ -- copy at the instant it became the only copy. Deleted-note history is reaped
174
+ -- by the configurable sweep (history.deleted_retention_days) or an explicit
175
+ -- erase, never by the FK.
176
+ --
177
+ -- content_hash DOES have a real FK to note_blobs with the default NO ACTION,
178
+ -- so SQLite itself enforces the one invariant refcount GC protects: deleting a
179
+ -- still-referenced blob raises instead of silently orphaning a version.
180
+ -- foreign_keys is turned ON per connection in applyConnectionPragmas
181
+ -- (schema.ts:486) — but inside a SWALLOWING try/catch, so on a handle where
182
+ -- the pragma throws there is no FK enforcement at all and no signal. The
183
+ -- NOT EXISTS guard on every blob delete is therefore the PRIMARY defence and
184
+ -- the FK is the backstop, not the other way round (§4.6).
185
+ -- note_blobs.encoding IS NULL -> \`content\` is the literal body, \`byte_size\`
186
+ -- is its UTF-8 byte length, and \`hash\` = sha256(content). This is every row
187
+ -- v29 ever wrote.
188
+ -- note_blobs.encoding = 'fossil-delta' -> \`content\` is the BASE64 of a Fossil
189
+ -- delta that reconstructs this row's logical body from the body of the blob
190
+ -- named by \`delta_of\`; \`byte_size\` is the length of that base64 payload, i.e.
191
+ -- the bytes actually stored. \`hash\` is STILL sha256 of the LOGICAL body, so
192
+ -- every note_versions.content_hash reference and its FK stay valid and
193
+ -- content-addressed identity is never a delta hash (ClaudeJi R3).
194
+ -- The LOGICAL byte length is note_versions.content_len, which PR 1
195
+ -- denormalised for exactly this moment (PR 1 §2.1: "it must stay readable
196
+ -- after PR 2 deltifies the blob").
197
+ -- ANY OTHER \`encoding\` VALUE IS CORRUPTION. It is not a future format marker to
198
+ -- be tolerated: a read of such a row answers 409 (materialise's
199
+ -- "unknown_encoding" branch) and the doctor counts it (historyStorageStats's
200
+ -- unknown_encoding_blobs). A later format adds its value here AND to both.
201
+ --
202
+ -- DEPTH IS EXACTLY ONE, AND THAT IS AN INVARIANT, NOT A TUNING CHOICE.
203
+ -- delta_of MUST name a row whose \`encoding IS NULL\`. A delta is never the
204
+ -- base of another delta. Rebuilding any version is therefore ONE applyDelta,
205
+ -- measured p95 3.73 ms / p99 4.68 ms on a 116 KB note, against 38.98 ms /
206
+ -- 49.99 ms for a 24-link chain on the same note -- the chain sits ON the
207
+ -- 50 ms budget with no headroom. Star also confines corruption: a damaged
208
+ -- delta loses one version, not every older one. See §11.1 D1.
209
+ --
210
+ -- delta_of carries a REAL self-referential FK with the default NO ACTION, and
211
+ -- it IS enforced on a bun handle (probe E: deleting a referenced base raises
212
+ -- FOREIGN KEY constraint failed). It is the backstop; the NOT EXISTS guards in
213
+ -- pruneVersions and gcBlobs are the primary defence, exactly as for
214
+ -- content_hash in v29 (PR 1 §4.6).
215
+ --
216
+ -- note_versions.created_at (#735) is the captured note's OWN creation time,
217
+ -- copied from notes.created_at at capture. NULL on every pre-v30 row; a
218
+ -- deleted-note restore falls back to the tombstone's superseded_at when it is
219
+ -- NULL, which is precisely v29's behaviour.
220
+ CREATE TABLE IF NOT EXISTS note_blobs (
221
+ hash TEXT PRIMARY KEY,
222
+ content TEXT NOT NULL,
223
+ byte_size INTEGER NOT NULL,
224
+ encoding TEXT,
225
+ delta_of TEXT REFERENCES note_blobs(hash)
226
+ );
227
+
228
+ CREATE TABLE IF NOT EXISTS note_versions (
229
+ note_id TEXT NOT NULL,
230
+ version_ix INTEGER NOT NULL,
231
+ content_hash TEXT REFERENCES note_blobs(hash),
232
+ path TEXT,
233
+ metadata TEXT,
234
+ extension TEXT,
235
+ superseded_at TEXT NOT NULL,
236
+ actor TEXT,
237
+ via TEXT,
238
+ op TEXT NOT NULL,
239
+ content_len INTEGER NOT NULL,
240
+ encoding TEXT,
241
+ created_at TEXT,
242
+ PRIMARY KEY (note_id, version_ix)
243
+ );
244
+ -- list/get (newest first) + the prune walk.
245
+ CREATE INDEX IF NOT EXISTS idx_note_versions_note ON note_versions(note_id, version_ix DESC);
246
+ -- the age half of retention + the deleted-note sweep.
247
+ CREATE INDEX IF NOT EXISTS idx_note_versions_superseded ON note_versions(superseded_at);
248
+ -- refcount GC: "is any version still pointing at this blob?" This index is
249
+ -- what keeps gcBlobs's per-vault-open anti-join off a full table scan; the
250
+ -- boot sweep runs it on an opted-in vault's open path (§4.6).
251
+ CREATE INDEX IF NOT EXISTS idx_note_versions_hash ON note_versions(content_hash);
252
+
145
253
  -- tag_schemas (v6) was retired in v14; description + fields lifted onto the
146
254
  -- tags row directly. The CREATE TABLE was removed from SCHEMA_SQL after the
147
255
  -- v14 data migration drops the table; existing v6+ vaults pick up the
@@ -629,6 +737,11 @@ export function initSchema(db: Database): void {
629
737
  // queued an unresolved link, and does not rewrite notes.
630
738
  migrateToV28(db);
631
739
 
740
+ // v29: note-level history, no backfill.
741
+ migrateToV29(db);
742
+ // v30: depth-one history deltas and captured creation time, no backfill.
743
+ migrateToV30(db);
744
+
632
745
  // Rebuild any generated columns + indexes declared in indexed_fields.
633
746
  // No-op for a fresh vault; idempotent on existing vaults.
634
747
  rebuildIndexes(db);
@@ -1756,6 +1869,94 @@ function migrateToV28(db: Database): void {
1756
1869
  ensureRelationshipColumn(db);
1757
1870
  }
1758
1871
 
1872
+ /** Add history tables and indexes without moving existing note data. */
1873
+ function migrateToV29(db: Database): void {
1874
+ transaction(db, () => {
1875
+ db.exec(`
1876
+ -- note_blobs + note_versions (v29, vault#524): note-level version history.
1877
+ --
1878
+ -- STORAGE. A version row is skinny; the BODY lives once per distinct content,
1879
+ -- keyed by its sha256, in note_blobs. A metadata-only edit on a 116 KB note
1880
+ -- costs one ~300-byte version row and a no-op \`INSERT OR IGNORE\` on the blob —
1881
+ -- the whole answer to "does every little update store an entirely new copy?"
1882
+ -- Live \`notes.content\` stays on \`notes\`: no join on the hot read path.
1883
+ --
1884
+ -- encoding IS NULL FOR EVERY ORDINARY ROW IN v29. NULL means "content_hash
1885
+ -- names a WHOLE blob". It exists so the PR-2 compactor can rewrite an old row
1886
+ -- to reference a delta (setting a format marker) with no second migration and
1887
+ -- no version-id change. The ONE non-NULL value v29 ever writes is the literal
1888
+ -- 'overflow', on a delete tombstone for a note whose body exceeded
1889
+ -- VERSION_MAX_BYTES: the row records that the note WAS deleted and how big it
1890
+ -- was, with content_hash NULL because the bytes were never blobbed. A delete
1891
+ -- must never be blocked by history (see history.ts captureVersion step 5).
1892
+ -- A restore marker copied from that tombstone also retains overflow encoding
1893
+ -- and a NULL hash, including after the note has been recreated.
1894
+ --
1895
+ -- content_hash IS NULLABLE, and it is nullable ONLY for that overflow
1896
+ -- tombstone or its copied restore marker. Every consumer must filter NULL —
1897
+ -- in particular gcBlobs's \`NOT IN (SELECT content_hash ...)\` MUST carry
1898
+ -- \`WHERE content_hash IS NOT NULL\`, or one NULL in the subquery makes the
1899
+ -- whole NOT IN evaluate to NULL and the sweep silently deletes nothing.
1900
+ --
1901
+ -- NO \`REFERENCES notes(id) ON DELETE CASCADE\` — the one deviation from
1902
+ -- note_vectors above, deliberate. deleteNote writes a final op='delete' row and
1903
+ -- the \`notes\` row then goes away; a cascade would delete the only surviving
1904
+ -- copy at the instant it became the only copy. Deleted-note history is reaped
1905
+ -- by the configurable sweep (history.deleted_retention_days) or an explicit
1906
+ -- erase, never by the FK.
1907
+ --
1908
+ -- content_hash DOES have a real FK to note_blobs with the default NO ACTION,
1909
+ -- so SQLite itself enforces the one invariant refcount GC protects: deleting a
1910
+ -- still-referenced blob raises instead of silently orphaning a version.
1911
+ -- foreign_keys is turned ON per connection in applyConnectionPragmas
1912
+ -- (schema.ts:486) — but inside a SWALLOWING try/catch, so on a handle where
1913
+ -- the pragma throws there is no FK enforcement at all and no signal. The
1914
+ -- NOT EXISTS guard on every blob delete is therefore the PRIMARY defence and
1915
+ -- the FK is the backstop, not the other way round (§4.6).
1916
+ CREATE TABLE IF NOT EXISTS note_blobs (
1917
+ hash TEXT PRIMARY KEY,
1918
+ content TEXT NOT NULL,
1919
+ byte_size INTEGER NOT NULL
1920
+ );
1921
+
1922
+ CREATE TABLE IF NOT EXISTS note_versions (
1923
+ note_id TEXT NOT NULL,
1924
+ version_ix INTEGER NOT NULL,
1925
+ content_hash TEXT REFERENCES note_blobs(hash),
1926
+ path TEXT,
1927
+ metadata TEXT,
1928
+ extension TEXT,
1929
+ superseded_at TEXT NOT NULL,
1930
+ actor TEXT,
1931
+ via TEXT,
1932
+ op TEXT NOT NULL,
1933
+ content_len INTEGER NOT NULL,
1934
+ encoding TEXT,
1935
+ PRIMARY KEY (note_id, version_ix)
1936
+ );
1937
+ -- list/get (newest first) + the prune walk.
1938
+ CREATE INDEX IF NOT EXISTS idx_note_versions_note ON note_versions(note_id, version_ix DESC);
1939
+ -- the age half of retention + the deleted-note sweep.
1940
+ CREATE INDEX IF NOT EXISTS idx_note_versions_superseded ON note_versions(superseded_at);
1941
+ -- refcount GC: "is any version still pointing at this blob?" This index is
1942
+ -- what keeps gcBlobs's per-vault-open anti-join off a full table scan; the
1943
+ -- boot sweep runs it on an opted-in vault's open path (§4.6).
1944
+ CREATE INDEX IF NOT EXISTS idx_note_versions_hash ON note_versions(content_hash);
1945
+ `);
1946
+ });
1947
+ }
1948
+
1949
+ /** Add delta representation and creation time without moving prior rows. */
1950
+ function migrateToV30(db: Database): void {
1951
+ transaction(db, () => {
1952
+ if (hasTable(db, "note_blobs") && !hasColumn(db, "note_blobs", "encoding")) db.exec("ALTER TABLE note_blobs ADD COLUMN encoding TEXT");
1953
+ if (hasTable(db, "note_blobs") && !hasColumn(db, "note_blobs", "delta_of")) db.exec("ALTER TABLE note_blobs ADD COLUMN delta_of TEXT REFERENCES note_blobs(hash)");
1954
+ if (hasTable(db, "note_versions") && !hasColumn(db, "note_versions", "created_at")) db.exec("ALTER TABLE note_versions ADD COLUMN created_at TEXT");
1955
+ // SCHEMA_SQL runs before migrations: the index must be created here.
1956
+ db.exec("CREATE INDEX IF NOT EXISTS idx_note_blobs_delta_of ON note_blobs(delta_of)");
1957
+ });
1958
+ }
1959
+
1759
1960
  function hasTable(db: Database, name: string): boolean {
1760
1961
  const row = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?").get(name);
1761
1962
  return !!row;
package/core/src/store.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Database } from "bun:sqlite";
2
2
  import type { Store, Note, Link, Attachment, QueryOpts, QueryNotesPage, AggregateRow, SemanticSearchResult } from "./types.js";
3
+ import { compactNote, compactVault, countNoteVersions, type CompactResult, type CompactSummary, captureVersion, readPriorNoteRow, resolveHistoryPolicy, appendRestoreMarker, listVersions, getVersion, latestTombstone, eraseHistory, sweepDeletedHistory, deletedHistoryStats, HistoryNotFoundError, HistoryOverflowError, HistoryUnrecoverableError, DEFAULT_HISTORY_POLICY, VERSION_MAX_BYTES, type VersionRow, type HistoryPolicy, type HistoryOp } from "./history.js";
3
4
  import { initSchema } from "./schema.js";
4
5
  import * as noteOps from "./notes.js";
5
6
  import * as linkOps from "./links.js";
@@ -98,6 +99,7 @@ function setsEqual(a: Set<string>, b: Set<string>): boolean {
98
99
  */
99
100
  export class BunSqliteStore implements Store {
100
101
  public readonly hooks: HookRegistry;
102
+ private readonly historyPolicy: HistoryPolicy;
101
103
 
102
104
  // Lazy-built caches over the post-v14 `tags` table — hierarchy via
103
105
  // parent_names, schema validation via `fields`. Null means "not yet
@@ -133,9 +135,10 @@ export class BunSqliteStore implements Store {
133
135
 
134
136
  constructor(
135
137
  public readonly db: Database,
136
- opts?: { hooks?: HookRegistry; embeddingProvider?: EmbeddingProvider; embeddingDisabledReason?: string },
138
+ opts?: { history?: Partial<HistoryPolicy>; hooks?: HookRegistry; embeddingProvider?: EmbeddingProvider; embeddingDisabledReason?: string },
137
139
  ) {
138
140
  initSchema(db);
141
+ this.historyPolicy = resolveHistoryPolicy(opts?.history);
139
142
  this.hooks = opts?.hooks ?? new HookRegistry();
140
143
  this.embeddingProvider = opts?.embeddingProvider;
141
144
  this.embeddingDisabledReason = opts?.embeddingDisabledReason;
@@ -574,6 +577,27 @@ export class BunSqliteStore implements Store {
574
577
  metadata?: Record<string, unknown>;
575
578
  created_at?: string;
576
579
  skipUpdatedAt?: boolean;
580
+ /**
581
+ * State-transition compare-and-set (vault#299 Part B). DECLARED here as
582
+ * of vault#524 — it always arrived at runtime (src/routes.ts:3214 sets
583
+ * it on a `const updates: any = {}` at :3167 and passes it at :3257, and
584
+ * noteOps.updateNote has read it at notes.ts:708 since #299) but it was
585
+ * never on the declared type, so an object LITERAL carrying it did not
586
+ * compile. Declaring it is what lets a core test construct the shape
587
+ * directly (P1, P11b) instead of laundering it through `any`.
588
+ */
589
+ state_transition?: { field: string; from: unknown; to: unknown };
590
+ /**
591
+ * INTERNAL — the `op` stamped on the version row this write captures
592
+ * (vault#524). Set ONLY by Store.restoreNoteVersion, to "restore".
593
+ * noteOps.updateNote ignores it (it reads named fields, never
594
+ * Object.keys), so it never reaches SQL. Do NOT accept it from REST or
595
+ * MCP: src/routes.ts's PATCH handler builds `updates` field by field and
596
+ * must not copy it out of the request body, and core/src/mcp.ts's
597
+ * update-note executor must not either. §10.24 pins that.
598
+ */
599
+ historyOp?: HistoryOp;
600
+
577
601
  // Write-attribution (vault#298) — principal + interface of this edit.
578
602
  actor?: string | null;
579
603
  via?: string | null;
@@ -633,6 +657,10 @@ export class BunSqliteStore implements Store {
633
657
  // atomic unit. Any later failure must leave the note and its indexes at
634
658
  // the pre-update state rather than committing only the first SQL write.
635
659
  const note = this.transaction(() => {
660
+ if (willWriteNoteRow(updates)) {
661
+ const prior = readPriorNoteRow(this.db, id);
662
+ if (prior) captureVersion(this.db, prior, { actor: updates.actor ?? null, via: updates.via ?? null, op: historyOpFor(updates), policy: this.historyPolicy });
663
+ }
636
664
  const note = noteOps.updateNote(this.db, id, updates);
637
665
 
638
666
  // Wikilink sync runs against the *resulting* content. For append/prepend
@@ -644,7 +672,7 @@ export class BunSqliteStore implements Store {
644
672
 
645
673
  if (updates.path !== undefined && note.path) {
646
674
  if (cascadePlan && oldPath && oldPath !== note.path) {
647
- this.cascadeRename(cascadePlan, note, oldPath);
675
+ this.cascadeRename(cascadePlan, note, oldPath, { actor: updates.actor ?? null, via: updates.via ?? null });
648
676
  }
649
677
  resolveUnresolvedWikilinks(this.db, note.path, id);
650
678
  // vault#581 — a rename is one of the two ways an ambiguity stops being
@@ -747,7 +775,7 @@ export class BunSqliteStore implements Store {
747
775
  * `unresolved_wikilinks` and `ambiguous_wikilinks` stay consistent with
748
776
  * the new text.
749
777
  */
750
- private cascadeRename(plan: Map<string, string[]>, note: Note, oldPath: string): void {
778
+ private cascadeRename(plan: Map<string, string[]>, note: Note, oldPath: string, attr: { actor: string | null; via: string | null }): void {
751
779
  if (plan.size === 0 || !note.path) return;
752
780
  const newPath = note.path;
753
781
 
@@ -785,12 +813,53 @@ export class BunSqliteStore implements Store {
785
813
  (target) => mapping.get(target.toLowerCase()) ?? null,
786
814
  );
787
815
  if (updated !== row.content) {
816
+ const prior = readPriorNoteRow(this.db, sourceId);
817
+ if (prior) captureVersion(this.db, prior, { ...attr, op: "cascade-rename", policy: this.historyPolicy });
788
818
  noteOps.updateNote(this.db, sourceId, { content: updated });
789
819
  syncWikilinks(this.db, sourceId, updated);
790
820
  }
791
821
  }
792
822
  }
793
823
 
824
+ async listNoteVersions(id: string, opts?: { limit?: number; offset?: number }) { return listVersions(this.db, id, opts); }
825
+ async getNoteVersion(id: string, versionIx: number) { return getVersion(this.db, id, versionIx); }
826
+ async eraseNoteHistory(id: string) { return eraseHistory(this.db, id); }
827
+ sweepDeletedHistory() { return sweepDeletedHistory(this.db, this.historyPolicy); }
828
+ async countNoteVersions(id: string) { return countNoteVersions(this.db, id); }
829
+ compactNote(id: string) { return compactNote(this.db, id, this.historyPolicy); }
830
+ compactHistory(opts?: { noteId?: string; budgetMs?: number | null; maxNotes?: number | null }) { return compactVault(this.db, this.historyPolicy, opts); }
831
+ async deletedHistoryStats() { return deletedHistoryStats(this.db); }
832
+ async restoreNoteVersion(id: string, versionIx: number, opts: { actor?: string | null; via?: string | null; if_updated_at?: string }): Promise<Note> {
833
+ if (noteOps.getNote(this.db, id)) {
834
+ const v = getVersion(this.db, id, versionIx);
835
+ if (!v) throw new HistoryNotFoundError(id, versionIx);
836
+ if (v.encoding === "overflow") throw new HistoryUnrecoverableError(id, versionIx);
837
+ return await this.updateNote(id, {
838
+ content: v.content!, metadata: v.metadata, extension: v.extension ?? undefined,
839
+ actor: opts.actor ?? null, via: opts.via ?? null,
840
+ ...(opts.if_updated_at !== undefined ? { if_updated_at: opts.if_updated_at } : {}),
841
+ historyOp: "restore",
842
+ });
843
+ }
844
+ const tomb = latestTombstone(this.db, id);
845
+ if (!tomb) throw new HistoryNotFoundError(id, null);
846
+ const v = getVersion(this.db, id, versionIx);
847
+ if (!v) throw new HistoryNotFoundError(id, versionIx);
848
+ if (v.encoding === "overflow") throw new HistoryUnrecoverableError(id, versionIx);
849
+ if (opts.if_updated_at !== undefined) throw new noteOps.ConflictError(id, tomb.path, null, opts.if_updated_at);
850
+ return this.transaction(() => {
851
+ const note = noteOps.createNote(this.db, v.content!, {
852
+ id, path: tomb.path ?? undefined, metadata: v.metadata,
853
+ extension: v.extension ?? undefined, created_at: v.created_at ?? tomb.superseded_at,
854
+ actor: opts.actor ?? null, via: opts.via ?? null,
855
+ });
856
+ appendRestoreMarker(this.db, id, tomb, { actor: opts.actor ?? null, via: opts.via ?? null }, this.historyPolicy);
857
+ if (note.content) syncWikilinks(this.db, id, note.content);
858
+ if (note.path) resolveUnresolvedWikilinks(this.db, note.path, id);
859
+ return note;
860
+ });
861
+ }
862
+
794
863
  async restoreNoteTimestamps(id: string, createdAt: string, updatedAt: string): Promise<void> {
795
864
  // Import-only: direct UPDATE so the importer can restore a note's
796
865
  // historical `created_at`/`updated_at` from the portable-md export
@@ -813,7 +882,7 @@ export class BunSqliteStore implements Store {
813
882
  .run(createdAt, updatedAt, updatedAtMs, id);
814
883
  }
815
884
 
816
- async deleteNote(id: string): Promise<void> {
885
+ async deleteNote(id: string, opts?: { actor?: string | null; via?: string | null; captureHistory?: boolean }): Promise<void> {
817
886
  // Read before delete so we can invalidate config caches on the way out
818
887
  // AND so the post-delete hook dispatch carries the minimum payload
819
888
  // ({ id, path }). The full note can't be reconstructed post-delete —
@@ -826,14 +895,20 @@ export class BunSqliteStore implements Store {
826
895
  // it's individually re-saved. See requeueInboundWikilinksForDelete's doc
827
896
  // comment for why this must run pre-delete and what it deliberately
828
897
  // excludes (typed `links`, not just wikilinks).
829
- requeueInboundWikilinksForDelete(this.db, id);
830
- // vault#581 — the deleted note's resolution keys, captured BEFORE the row
831
- // goes away and swept AFTER, so a `[[Dup]]` that was ambiguous only
832
- // because of THIS note resolves (or, if it was the last candidate,
833
- // demotes to an ordinary broken link).
834
- const ambiguityKeys = noteResolutionKeys(existing);
835
- noteOps.deleteNote(this.db, id);
836
- refreshAmbiguousLinks(this.db, ambiguityKeys);
898
+ this.transaction(() => {
899
+ requeueInboundWikilinksForDelete(this.db, id);
900
+ // vault#581 — the deleted note's resolution keys, captured BEFORE the row
901
+ // goes away and swept AFTER, so a `[[Dup]]` that was ambiguous only
902
+ // because of THIS note resolves (or, if it was the last candidate,
903
+ // demotes to an ordinary broken link).
904
+ const ambiguityKeys = noteResolutionKeys(existing);
905
+ if (opts?.captureHistory !== false) {
906
+ const prior = readPriorNoteRow(this.db, id);
907
+ if (prior) captureVersion(this.db, prior, { actor: opts?.actor ?? null, via: opts?.via ?? null, op: "delete", policy: this.historyPolicy });
908
+ }
909
+ noteOps.deleteNote(this.db, id);
910
+ refreshAmbiguousLinks(this.db, ambiguityKeys);
911
+ });
837
912
  if (existing?.path) this.invalidateConfigCachesForPath(existing.path);
838
913
  // Dispatch even when `existing` was null — the caller asked for a
839
914
  // deletion, and downstream consumers (e.g. the mirror) reconcile via
@@ -1094,8 +1169,8 @@ export class BunSqliteStore implements Store {
1094
1169
  return result;
1095
1170
  }
1096
1171
 
1097
- async renameTag(oldName: string, newName: string): Promise<noteOps.RenameTagResult> {
1098
- const result = noteOps.renameTag(this.db, oldName, newName);
1172
+ async renameTag(oldName: string, newName: string, opts?: { actor?: string; via?: string }): Promise<noteOps.RenameTagResult> {
1173
+ const result = noteOps.renameTag(this.db, oldName, newName, opts, this.historyPolicy);
1099
1174
  // Vault#240: the cascade rewrites parent_names in OTHER tag rows as
1100
1175
  // part of the same transaction, plus tokens.scoped_tags and
1101
1176
  // indexed_fields.declarer_tags. Both caches are tag-keyed, so they
@@ -1728,3 +1803,17 @@ export class BunSqliteStore implements Store {
1728
1803
  export const SqliteStore = BunSqliteStore;
1729
1804
  /** @deprecated Renamed to `BunSqliteStore`. */
1730
1805
  export type SqliteStore = BunSqliteStore;
1806
+
1807
+ const MUTATING_UPDATE_KEYS = ["content", "append", "prepend", "path", "extension", "metadata", "created_at", "state_transition"] as const;
1808
+ function willWriteNoteRow(u: Parameters<Store["updateNote"]>[1]): boolean {
1809
+ for (const key of MUTATING_UPDATE_KEYS) if (u[key] !== undefined) return true;
1810
+ return u.skipUpdatedAt !== true;
1811
+ }
1812
+ function historyOpFor(u: Parameters<Store["updateNote"]>[1]): HistoryOp {
1813
+ if (typeof u.historyOp === "string") return u.historyOp;
1814
+ if (u.append !== undefined) return "append";
1815
+ if (u.prepend !== undefined) return "prepend";
1816
+ return "update";
1817
+ }
1818
+ export { HistoryNotFoundError, HistoryOverflowError, HistoryUnrecoverableError, DEFAULT_HISTORY_POLICY, VERSION_MAX_BYTES };
1819
+ export type { HistoryPolicy, HistoryOp, VersionRow, CompactResult, CompactSummary };