sensemaking 0.19.2 → 0.20.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/dist/cjs/chunk/extract.d.cts +1 -0
- package/dist/cjs/chunk/extract.d.ts +1 -0
- package/dist/cjs/chunk/extract.js +23 -5
- package/dist/cjs/chunk/extract.js.map +1 -1
- package/dist/cjs/chunk/index.d.cts +1 -1
- package/dist/cjs/chunk/index.d.ts +1 -1
- package/dist/cjs/chunk/index.js +3 -0
- package/dist/cjs/chunk/index.js.map +1 -1
- package/dist/cjs/scan/pool.d.cts +15 -0
- package/dist/cjs/scan/pool.d.ts +15 -0
- package/dist/cjs/scan/pool.js +263 -0
- package/dist/cjs/scan/pool.js.map +1 -0
- package/dist/cjs/scan/reparse.d.cts +2 -0
- package/dist/cjs/scan/reparse.d.ts +2 -0
- package/dist/cjs/scan/reparse.js +15 -76
- package/dist/cjs/scan/reparse.js.map +1 -1
- package/dist/cjs/store/builder.d.cts +11 -0
- package/dist/cjs/store/builder.d.ts +11 -0
- package/dist/cjs/store/builder.js +27 -0
- package/dist/cjs/store/builder.js.map +1 -0
- package/dist/cjs/store/duckdb/lexical.d.cts +1 -0
- package/dist/cjs/store/duckdb/lexical.d.ts +1 -0
- package/dist/cjs/store/duckdb/lexical.js +21 -5
- package/dist/cjs/store/duckdb/lexical.js.map +1 -1
- package/dist/cjs/store/duckdb/open.d.cts +9 -8
- package/dist/cjs/store/duckdb/open.d.ts +9 -8
- package/dist/cjs/store/duckdb/open.js +70 -122
- package/dist/cjs/store/duckdb/open.js.map +1 -1
- package/dist/cjs/store/duckdb/reconcile.js +4 -0
- package/dist/cjs/store/duckdb/reconcile.js.map +1 -1
- package/dist/cjs/store/duckdb/store.d.cts +1 -2
- package/dist/cjs/store/duckdb/store.d.ts +1 -2
- package/dist/cjs/store/duckdb/store.js +1 -26
- package/dist/cjs/store/duckdb/store.js.map +1 -1
- package/dist/cjs/store/index.d.cts +8 -3
- package/dist/cjs/store/index.d.ts +8 -3
- package/dist/cjs/store/index.js +36 -7
- package/dist/cjs/store/index.js.map +1 -1
- package/dist/cjs/store/open.d.cts +15 -0
- package/dist/cjs/store/open.d.ts +15 -0
- package/dist/cjs/store/open.js +516 -0
- package/dist/cjs/store/open.js.map +1 -0
- package/dist/cjs/store/reconcile.d.cts +2 -1
- package/dist/cjs/store/reconcile.d.ts +2 -1
- package/dist/cjs/store/reconcile.js +4 -2
- package/dist/cjs/store/reconcile.js.map +1 -1
- package/dist/cjs/store/{sqlite/signature.d.cts → signature.d.cts} +0 -3
- package/dist/cjs/store/{sqlite/signature.d.ts → signature.d.ts} +0 -3
- package/dist/cjs/store/signature.js +162 -0
- package/dist/cjs/store/signature.js.map +1 -0
- package/dist/cjs/store/sqlite/open.d.cts +9 -9
- package/dist/cjs/store/sqlite/open.d.ts +9 -9
- package/dist/cjs/store/sqlite/open.js +189 -201
- package/dist/cjs/store/sqlite/open.js.map +1 -1
- package/dist/cjs/store/sqlite/store.d.cts +1 -1
- package/dist/cjs/store/sqlite/store.d.ts +1 -1
- package/dist/cjs/store/sqlite/store.js +1 -13
- package/dist/cjs/store/sqlite/store.js.map +1 -1
- package/dist/cjs/store/turso/open.d.cts +6 -11
- package/dist/cjs/store/turso/open.d.ts +6 -11
- package/dist/cjs/store/turso/open.js +79 -171
- package/dist/cjs/store/turso/open.js.map +1 -1
- package/dist/cjs/store/turso/reconcile.d.cts +2 -0
- package/dist/cjs/store/turso/reconcile.d.ts +2 -0
- package/dist/cjs/store/turso/reconcile.js +16 -5
- package/dist/cjs/store/turso/reconcile.js.map +1 -1
- package/dist/cjs/store/turso/store.d.cts +1 -2
- package/dist/cjs/store/turso/store.d.ts +1 -2
- package/dist/cjs/store/turso/store.js +1 -13
- package/dist/cjs/store/turso/store.js.map +1 -1
- package/dist/cjs/store/types.d.cts +17 -5
- package/dist/cjs/store/types.d.ts +17 -5
- package/dist/cjs/store/types.js.map +1 -1
- package/dist/cjs/text/strip.js +3 -6
- package/dist/cjs/text/strip.js.map +1 -1
- package/dist/cjs/watch.js +28 -16
- package/dist/cjs/watch.js.map +1 -1
- package/dist/esm/chunk/extract.d.ts +1 -0
- package/dist/esm/chunk/extract.js +10 -2
- package/dist/esm/chunk/extract.js.map +1 -1
- package/dist/esm/chunk/index.d.ts +1 -1
- package/dist/esm/chunk/index.js +1 -1
- package/dist/esm/chunk/index.js.map +1 -1
- package/dist/esm/scan/pool.d.ts +15 -0
- package/dist/esm/scan/pool.js +77 -0
- package/dist/esm/scan/pool.js.map +1 -0
- package/dist/esm/scan/reparse.d.ts +2 -0
- package/dist/esm/scan/reparse.js +11 -58
- package/dist/esm/scan/reparse.js.map +1 -1
- package/dist/esm/store/builder.d.ts +11 -0
- package/dist/esm/store/builder.js +14 -0
- package/dist/esm/store/builder.js.map +1 -0
- package/dist/esm/store/duckdb/lexical.d.ts +1 -0
- package/dist/esm/store/duckdb/lexical.js +22 -9
- package/dist/esm/store/duckdb/lexical.js.map +1 -1
- package/dist/esm/store/duckdb/open.d.ts +9 -8
- package/dist/esm/store/duckdb/open.js +31 -57
- package/dist/esm/store/duckdb/open.js.map +1 -1
- package/dist/esm/store/duckdb/reconcile.js +4 -0
- package/dist/esm/store/duckdb/reconcile.js.map +1 -1
- package/dist/esm/store/duckdb/store.d.ts +1 -2
- package/dist/esm/store/duckdb/store.js +2 -11
- package/dist/esm/store/duckdb/store.js.map +1 -1
- package/dist/esm/store/index.d.ts +8 -3
- package/dist/esm/store/index.js +22 -7
- package/dist/esm/store/index.js.map +1 -1
- package/dist/esm/store/open.d.ts +15 -0
- package/dist/esm/store/open.js +137 -0
- package/dist/esm/store/open.js.map +1 -0
- package/dist/esm/store/reconcile.d.ts +2 -1
- package/dist/esm/store/reconcile.js +4 -2
- package/dist/esm/store/reconcile.js.map +1 -1
- package/dist/esm/store/{sqlite/signature.d.ts → signature.d.ts} +0 -3
- package/dist/esm/store/{sqlite/signature.js → signature.js} +3 -26
- package/dist/esm/store/signature.js.map +1 -0
- package/dist/esm/store/sqlite/open.d.ts +9 -9
- package/dist/esm/store/sqlite/open.js +81 -113
- package/dist/esm/store/sqlite/open.js.map +1 -1
- package/dist/esm/store/sqlite/store.d.ts +1 -1
- package/dist/esm/store/sqlite/store.js +2 -7
- package/dist/esm/store/sqlite/store.js.map +1 -1
- package/dist/esm/store/turso/open.d.ts +6 -11
- package/dist/esm/store/turso/open.js +30 -78
- package/dist/esm/store/turso/open.js.map +1 -1
- package/dist/esm/store/turso/reconcile.d.ts +2 -0
- package/dist/esm/store/turso/reconcile.js +12 -1
- package/dist/esm/store/turso/reconcile.js.map +1 -1
- package/dist/esm/store/turso/store.d.ts +1 -2
- package/dist/esm/store/turso/store.js +2 -7
- package/dist/esm/store/turso/store.js.map +1 -1
- package/dist/esm/store/types.d.ts +17 -5
- package/dist/esm/store/types.js.map +1 -1
- package/dist/esm/text/strip.js +3 -4
- package/dist/esm/text/strip.js.map +1 -1
- package/dist/esm/watch.js +5 -3
- package/dist/esm/watch.js.map +1 -1
- package/package.json +2 -2
- package/dist/cjs/store/sqlite/signature.js +0 -380
- package/dist/cjs/store/sqlite/signature.js.map +0 -1
- package/dist/esm/store/sqlite/signature.js.map +0 -1
|
@@ -1,25 +1,21 @@
|
|
|
1
1
|
// The Node floor (>=22.20) is explained here and nowhere else: 22.20 is the first release with
|
|
2
2
|
// both FTS5 and row-returning INSERT ... RETURNING. Raise it only for a load-bearing capability.
|
|
3
|
-
import { mkdirSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
3
|
import { DatabaseSync } from 'node:sqlite';
|
|
6
|
-
import { contentTokenize, featureSignature
|
|
7
|
-
import { rekeyChunkText } from '../../embed/handoff.js';
|
|
4
|
+
import { contentTokenize, featureSignature } from '../../config/index.js';
|
|
8
5
|
import { SenseError } from '../../errors.js';
|
|
9
6
|
import { activeFeatures, FEATURES } from '../../features/index.js';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
7
|
+
import { listFiles, parseFile } from '../../scan/index.js';
|
|
8
|
+
import { openWithDialect } from '../open.js';
|
|
12
9
|
import { getMeta, setMeta } from '../shared.js';
|
|
13
10
|
import { BEGIN_WRITE, withTransaction } from '../transaction.js';
|
|
14
11
|
import { createConnection } from './connection.js';
|
|
15
|
-
import { sqliteDialect } from './reconcile.js';
|
|
16
|
-
import { changedSignatureKeys, embedIdentityAdopted, rebuildContentTable, signatureDiff } from './signature.js';
|
|
12
|
+
import { contentRow, INSERT_CONTENT_SQL, sqliteDialect } from './reconcile.js';
|
|
17
13
|
import { registerFunctions } from './sql-functions.js';
|
|
18
14
|
import { createStore } from './store.js';
|
|
19
15
|
export const DB_FILENAME = 'cache.db';
|
|
20
16
|
// Cache shape version, independent of the config's own `version`. Bumping it rebuilds
|
|
21
17
|
// existing trees on first query.
|
|
22
|
-
export const SCHEMA_VERSION = '
|
|
18
|
+
export const SCHEMA_VERSION = '19';
|
|
23
19
|
// unicode61 splits on spaces, so a language written without them indexes a whole run as one
|
|
24
20
|
// token and word search finds nothing; `content.tokenize` is how such a tree picks trigram.
|
|
25
21
|
const DEFAULT_TOKENIZE = 'porter unicode61';
|
|
@@ -53,7 +49,7 @@ async function createContentTable(conn, tokenize) {
|
|
|
53
49
|
}
|
|
54
50
|
// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`
|
|
55
51
|
// can't dump file text into context. Features add their own tables after the core ones.
|
|
56
|
-
async function
|
|
52
|
+
async function ensureSchemaTables(conn, cfg, tokenize) {
|
|
57
53
|
await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter ("path" TEXT PRIMARY KEY, "_mtime" REAL, "_ctime" REAL, "_size" INTEGER, "_parse_error" TEXT)`);
|
|
58
54
|
// IF NOT EXISTS is safe against a tokenizer change: open() compares the table's own DDL
|
|
59
55
|
// against the resolved tokenizer before this runs, so a stale table is already gone by now.
|
|
@@ -80,18 +76,9 @@ function setJournalWal(db) {
|
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
78
|
}
|
|
83
|
-
async function connect(cfg) {
|
|
84
|
-
const stateDir = join(cfg.baseDir, STATE_DIR);
|
|
85
|
-
mkdirSync(stateDir, {
|
|
86
|
-
recursive: true
|
|
87
|
-
});
|
|
88
|
-
const dbPath = join(stateDir, DB_FILENAME);
|
|
79
|
+
async function connect(dbPath, cfg) {
|
|
89
80
|
const db = new DatabaseSync(dbPath);
|
|
90
|
-
// A throw below must release this handle, or on Windows the leaked WAL db is undeletable and
|
|
91
|
-
// scratch cleanup fails with EPERM/EBUSY. `closed` keeps the catch from double-closing.
|
|
92
|
-
let closed = false;
|
|
93
81
|
try {
|
|
94
|
-
var _ref;
|
|
95
82
|
// Before journal_mode, not after: converting a fresh database to WAL takes a brief exclusive
|
|
96
83
|
// lock, and with no timeout set yet a second process opening the same tree fails in 1ms.
|
|
97
84
|
db.exec('PRAGMA busy_timeout = 30000');
|
|
@@ -100,107 +87,88 @@ async function connect(cfg) {
|
|
|
100
87
|
setJournalWal(db);
|
|
101
88
|
registerFunctions(db, contentTokenize(cfg) === undefined);
|
|
102
89
|
const conn = createConnection(db);
|
|
103
|
-
db.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');
|
|
104
90
|
// Before the rebuild branch below, never after: that branch deletes the cache, so a typo'd
|
|
105
91
|
// tokenizer validated later would cost a full re-index (and re-embed) to reach its own error.
|
|
106
92
|
const tokenize = resolveTokenize(db, cfg);
|
|
107
|
-
// Schema-version or feature-set mismatch: reconcile only reparses changed files, so an
|
|
108
|
-
// old cache can't be patched incrementally -- rebuild instead (cheap: nothing expensive lives here).
|
|
109
|
-
const version = await getMeta(conn, 'schema_version');
|
|
110
|
-
const features = await getMeta(conn, 'features');
|
|
111
|
-
const wantFeatures = featureSignature(cfg, FEATURES);
|
|
112
|
-
let tokenizeOnlyRebuild = false;
|
|
113
|
-
if (version !== null && version !== SCHEMA_VERSION || features !== null && features !== wantFeatures) {
|
|
114
|
-
// Indexing derives from presets, so a config edit rebuilding the cache must say so and
|
|
115
|
-
// name what changed -- silent rebuilds make derived indexing look like a hang or a bug.
|
|
116
|
-
if (version !== null && version !== SCHEMA_VERSION) {
|
|
117
|
-
console.error('sense: cache format changed (new sensemaking version); rebuilding the index');
|
|
118
|
-
closed = true;
|
|
119
|
-
db.close();
|
|
120
|
-
clearCache(cfg);
|
|
121
|
-
return connect(cfg);
|
|
122
|
-
}
|
|
123
|
-
const changedKeys = changedSignatureKeys(features !== null && features !== void 0 ? features : '', wantFeatures);
|
|
124
|
-
// Only the tokenizer moved, and frontmatter, links, sections and embeddings are file-derived
|
|
125
|
-
// and tokenizer-independent. Any other signature change takes the full clear/reopen below.
|
|
126
|
-
if (changedKeys.size === 1 && changedKeys.has('tokenize')) {
|
|
127
|
-
console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');
|
|
128
|
-
// One transaction, so a crash before COMMIT rolls back to the old tokenizer's table rather
|
|
129
|
-
// than to no table. IF EXISTS makes a retry after such a crash a no-op, not a raw error.
|
|
130
|
-
await withTransaction(conn, async ()=>{
|
|
131
|
-
await conn.exec('DROP TABLE IF EXISTS content');
|
|
132
|
-
await createContentTable(conn, tokenize);
|
|
133
|
-
}, BEGIN_WRITE);
|
|
134
|
-
tokenizeOnlyRebuild = true;
|
|
135
|
-
} else if (changedKeys.size === 1 && changedKeys.has('embed') && embedIdentityAdopted(features !== null && features !== void 0 ? features : '', wantFeatures)) {
|
|
136
|
-
// First sight of a resolved weight identity: the model itself hasn't changed, so
|
|
137
|
-
// adopt it into meta with no rebuild and no re-embed, mirroring the tokenize precedent.
|
|
138
|
-
console.error("sense: recorded the embedding model's resolved identity; vectors are unaffected");
|
|
139
|
-
await setMeta(conn, 'features', wantFeatures);
|
|
140
|
-
} else {
|
|
141
|
-
const changed = signatureDiff(features !== null && features !== void 0 ? features : '', wantFeatures);
|
|
142
|
-
console.error(`sense: config change (${changed}) rebuilds the index`);
|
|
143
|
-
closed = true;
|
|
144
|
-
db.close();
|
|
145
|
-
clearCache(cfg);
|
|
146
|
-
return connect(cfg);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
// Meta can lie after a crash between table creation and the signature write; the table's own
|
|
150
|
-
// DDL cannot, so a mismatch here rebuilds whatever meta says.
|
|
151
|
-
const stored = storedTokenize(db);
|
|
152
|
-
if (stored !== null && stored !== tokenize) {
|
|
153
|
-
console.error('sense: cache was built with a different content tokenizer; rebuilding the index');
|
|
154
|
-
closed = true;
|
|
155
|
-
db.close();
|
|
156
|
-
clearCache(cfg);
|
|
157
|
-
return connect(cfg);
|
|
158
|
-
}
|
|
159
|
-
// One writer at a time: the feature hooks check a column then add it, so two cold opens racing
|
|
160
|
-
// here both see it missing and the second ALTER fails with a duplicate column.
|
|
161
|
-
await withTransaction(conn, ()=>ensureSchema(conn, cfg, tokenize), BEGIN_WRITE);
|
|
162
|
-
let rebuildWarnings = [];
|
|
163
|
-
if (tokenizeOnlyRebuild) {
|
|
164
|
-
rebuildWarnings = await rebuildContentTable(conn, cfg, cfg.baseDir);
|
|
165
|
-
await setMeta(conn, 'features', wantFeatures);
|
|
166
|
-
}
|
|
167
|
-
// 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.
|
|
168
|
-
// Installed before reconcile() -- that call is the one that races a watcher's transaction.
|
|
169
|
-
const recordedMaxMs = Number((_ref = await getMeta(conn, 'reconcile_max_ms')) !== null && _ref !== void 0 ? _ref : '0');
|
|
170
|
-
db.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600000)}`);
|
|
171
|
-
const { parsed, warnings } = await reconcile(conn, cfg, cfg.baseDir, sqliteDialect);
|
|
172
93
|
return {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
warnings: [
|
|
179
|
-
...rebuildWarnings,
|
|
180
|
-
...warnings
|
|
181
|
-
]
|
|
94
|
+
handle: {
|
|
95
|
+
db,
|
|
96
|
+
tokenize
|
|
97
|
+
},
|
|
98
|
+
conn
|
|
182
99
|
};
|
|
183
100
|
} catch (err) {
|
|
184
|
-
|
|
101
|
+
// A throw here must release this handle, or on Windows the leaked WAL db is undeletable and
|
|
102
|
+
// scratch cleanup fails with EPERM/EBUSY.
|
|
103
|
+
db.close();
|
|
185
104
|
throw err;
|
|
186
105
|
}
|
|
187
106
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
};
|
|
107
|
+
async function close(handle) {
|
|
108
|
+
handle.db.close();
|
|
109
|
+
}
|
|
110
|
+
// Only the tokenizer moved, and frontmatter, links, sections and embeddings are file-derived
|
|
111
|
+
// and tokenizer-independent. Any other signature change takes the full clear/reopen in open.ts.
|
|
112
|
+
async function partialRebuild(handle, conn, _cfg, changedKeys) {
|
|
113
|
+
if (!(changedKeys.size === 1 && changedKeys.has('tokenize'))) return false;
|
|
114
|
+
console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');
|
|
115
|
+
// One transaction, so a crash before COMMIT rolls back to the old tokenizer's table rather
|
|
116
|
+
// than to no table. IF EXISTS makes a retry after such a crash a no-op, not a raw error.
|
|
117
|
+
await withTransaction(conn, async ()=>{
|
|
118
|
+
await conn.exec('DROP TABLE IF EXISTS content');
|
|
119
|
+
await createContentTable(conn, handle.tokenize);
|
|
120
|
+
}, BEGIN_WRITE);
|
|
121
|
+
return true;
|
|
202
122
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
123
|
+
// Tokenize-only rebuild: only `content` is repopulated (the other tables are tokenizer-independent)
|
|
124
|
+
// and no feature extractors run. Warnings come from the reparse, since mtimes stay untouched and reconcile would never reparse them.
|
|
125
|
+
async function postSchemaRebuild(_handle, conn, cfg, baseDir, wantFeatures) {
|
|
126
|
+
const stmt = await conn.prepare('SELECT "path" FROM frontmatter');
|
|
127
|
+
const known = new Set((await stmt.all()).map((r)=>r.path));
|
|
128
|
+
const files = listFiles(cfg, baseDir).filter((f)=>known.has(f.relPath));
|
|
129
|
+
const segmenting = contentTokenize(cfg) === undefined;
|
|
130
|
+
const warnings = [];
|
|
131
|
+
const rows = [];
|
|
132
|
+
for (const file of files){
|
|
133
|
+
const { doc, warnings: fileWarnings } = parseFile(file);
|
|
134
|
+
warnings.push(...fileWarnings);
|
|
135
|
+
rows.push(contentRow(doc, segmenting));
|
|
136
|
+
}
|
|
137
|
+
await withTransaction(conn, async ()=>{
|
|
138
|
+
if (rows.length > 0) await conn.runBatch(INSERT_CONTENT_SQL, rows);
|
|
139
|
+
}, BEGIN_WRITE);
|
|
140
|
+
await setMeta(conn, 'features', wantFeatures);
|
|
141
|
+
return warnings;
|
|
142
|
+
}
|
|
143
|
+
// Meta can lie after a crash between table creation and the signature write; the table's own
|
|
144
|
+
// DDL cannot, so a mismatch here rebuilds whatever meta says.
|
|
145
|
+
function extraRebuildReason(handle) {
|
|
146
|
+
const stored = storedTokenize(handle.db);
|
|
147
|
+
if (stored !== null && stored !== handle.tokenize) return 'cache was built with a different content tokenizer';
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
async function ensureSchema(handle, conn, cfg) {
|
|
151
|
+
// One writer at a time: the feature hooks check a column then add it, so two cold opens racing
|
|
152
|
+
// here both see it missing and the second ALTER fails with a duplicate column.
|
|
153
|
+
await withTransaction(conn, ()=>ensureSchemaTables(conn, cfg, handle.tokenize), BEGIN_WRITE);
|
|
154
|
+
}
|
|
155
|
+
async function setDerivedBusyTimeout(handle, _conn, ms) {
|
|
156
|
+
handle.db.exec(`PRAGMA busy_timeout = ${ms}`);
|
|
157
|
+
}
|
|
158
|
+
// This store's dialect (types.ts's OpenDialect) for the shared orchestration in store/open.ts.
|
|
159
|
+
export const sqliteOpenDialect = {
|
|
160
|
+
filename: DB_FILENAME,
|
|
161
|
+
schemaVersion: SCHEMA_VERSION,
|
|
162
|
+
reconcileDialect: sqliteDialect,
|
|
163
|
+
connect,
|
|
164
|
+
close,
|
|
165
|
+
partialRebuild,
|
|
166
|
+
postSchemaRebuild,
|
|
167
|
+
extraRebuildReason,
|
|
168
|
+
ensureSchema,
|
|
169
|
+
setDerivedBusyTimeout,
|
|
170
|
+
createStore: (handle, conn, cfg)=>createStore(handle.db, conn, cfg)
|
|
171
|
+
};
|
|
172
|
+
export async function openSqlite(cfg) {
|
|
173
|
+
return openWithDialect(cfg, sqliteOpenDialect);
|
|
206
174
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/sqlite/open.ts"],"sourcesContent":["// The Node floor (>=22.20) is explained here and nowhere else: 22.20 is the first release with\n// both FTS5 and row-returning INSERT ... RETURNING. Raise it only for a load-bearing capability.\nimport { mkdirSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { DatabaseSync } from 'node:sqlite';\nimport type { Config, ResolvedConfig } from '../../config/index.ts';\nimport { contentTokenize, featureSignature, STATE_DIR } from '../../config/index.ts';\nimport { rekeyChunkText } from '../../embed/handoff.ts';\nimport { SenseError } from '../../errors.ts';\nimport { activeFeatures, FEATURES } from '../../features/index.ts';\nimport { clearCache } from '../cache.ts';\nimport { reconcile } from '../reconcile.ts';\nimport { getMeta, setMeta } from '../shared.ts';\nimport { BEGIN_WRITE, withTransaction } from '../transaction.ts';\nimport type { Connection, Store } from '../types.ts';\nimport { createConnection } from './connection.ts';\nimport { sqliteDialect } from './reconcile.ts';\nimport { changedSignatureKeys, embedIdentityAdopted, rebuildContentTable, signatureDiff } from './signature.ts';\nimport { registerFunctions } from './sql-functions.ts';\nimport { createStore } from './store.ts';\n\nexport const DB_FILENAME = 'cache.db';\n// Cache shape version, independent of the config's own `version`. Bumping it rebuilds\n// existing trees on first query.\nexport const SCHEMA_VERSION = '18';\n\ninterface ConnectResult {\n db: DatabaseSync;\n conn: Connection;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\nexport interface OpenResult {\n store: Store;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\n// unicode61 splits on spaces, so a language written without them indexes a whole run as one\n// token and word search finds nothing; `content.tokenize` is how such a tree picks trigram.\nconst DEFAULT_TOKENIZE = 'porter unicode61';\n\n// FTS5 takes its tokenizer as a DDL string literal where nothing can bind, so the configured\n// value is concatenated; probing a throwaway table first is both the safety and the validation.\nfunction resolveTokenize(db: DatabaseSync, cfg: Config): string {\n const configured = contentTokenize(cfg);\n if (configured === undefined) return DEFAULT_TOKENIZE;\n const literal = configured.replace(/'/g, \"''\");\n try {\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n db.exec(`CREATE VIRTUAL TABLE temp.sense_tokenize_probe USING fts5(x, tokenize = '${literal}')`);\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n } catch (err) {\n throw new SenseError('CONFIG_INVALID', `content.tokenize \"${configured}\" is not a tokenizer this SQLite accepts (${(err as Error).message}); the built-in choices are unicode61, ascii, porter, and trigram, each with their own options`);\n }\n return literal;\n}\n\n// The tokenizer the content table was actually built with, from its own DDL -- the one\n// record that cannot desynchronize from the table. NULL when the table does not exist yet.\nfunction storedTokenize(db: DatabaseSync): string | null {\n const row = db.prepare(`SELECT sql FROM sqlite_master WHERE name = 'content'`).get() as { sql: string } | undefined;\n if (!row) return null;\n const m = row.sql.match(/tokenize = '((?:[^']|'')*)'/);\n return m ? m[1] : null;\n}\n\n// The `_seg` sidecars are appended after path, never inserted: bm25() and snippet(content, 2)\n// are documented against the first three columns. Each holds its field's exploded unspaced runs.\nasync function createContentTable(conn: Connection, tokenize: string): Promise<void> {\n await conn.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS content USING fts5(title, summary, text, path UNINDEXED, title_seg, summary_seg, text_seg, tokenize = '${tokenize}')`);\n}\n\n// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`\n// can't dump file text into context. Features add their own tables after the core ones.\nasync function ensureSchema(conn: Connection, cfg: Config, tokenize: string): Promise<void> {\n await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_ctime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n // IF NOT EXISTS is safe against a tokenizer change: open() compares the table's own DDL\n // against the resolved tokenizer before this runs, so a stale table is already gone by now.\n await createContentTable(conn, tokenize);\n // Coverage, not ownership: a path can appear under several presets. path leads the PK so the\n // per-doc delete is an index hit -- keyed the other way, cold builds went quadratic.\n await conn.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n await conn.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) await feature.schema(conn);\n if ((await getMeta(conn, 'schema_version')) === null) await setMeta(conn, 'schema_version', SCHEMA_VERSION);\n if ((await getMeta(conn, 'features')) === null) await setMeta(conn, 'features', featureSignature(cfg, FEATURES));\n}\n\n// Two processes opening the same fresh tree both try to convert it, and the loser gets SQLITE_BUSY\n// with no busy handler behind it. Bounded because a lock held past this is a real problem, not a race.\nfunction setJournalWal(db: DatabaseSync): void {\n const deadline = Date.now() + 5000;\n for (;;) {\n try {\n db.exec('PRAGMA journal_mode = WAL');\n return;\n } catch (err) {\n if (Date.now() >= deadline || !/database is locked|busy/i.test((err as Error).message)) throw err;\n Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);\n }\n }\n}\n\nasync function connect(cfg: ResolvedConfig): Promise<ConnectResult> {\n const stateDir = join(cfg.baseDir, STATE_DIR);\n mkdirSync(stateDir, { recursive: true });\n const dbPath = join(stateDir, DB_FILENAME);\n\n const db = new DatabaseSync(dbPath);\n // A throw below must release this handle, or on Windows the leaked WAL db is undeletable and\n // scratch cleanup fails with EPERM/EBUSY. `closed` keeps the catch from double-closing.\n let closed = false;\n try {\n // Before journal_mode, not after: converting a fresh database to WAL takes a brief exclusive\n // lock, and with no timeout set yet a second process opening the same tree fails in 1ms.\n db.exec('PRAGMA busy_timeout = 30000');\n // busy_timeout does not cover the WAL conversion itself: SQLite does not invoke the busy\n // handler for it, so a concurrent cold open needs its own bounded wait.\n setJournalWal(db);\n registerFunctions(db, contentTokenize(cfg) === undefined);\n const conn = createConnection(db);\n\n db.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');\n\n // Before the rebuild branch below, never after: that branch deletes the cache, so a typo'd\n // tokenizer validated later would cost a full re-index (and re-embed) to reach its own error.\n const tokenize = resolveTokenize(db, cfg);\n\n // Schema-version or feature-set mismatch: reconcile only reparses changed files, so an\n // old cache can't be patched incrementally -- rebuild instead (cheap: nothing expensive lives here).\n const version = await getMeta(conn, 'schema_version');\n const features = await getMeta(conn, 'features');\n const wantFeatures = featureSignature(cfg, FEATURES);\n let tokenizeOnlyRebuild = false;\n if ((version !== null && version !== SCHEMA_VERSION) || (features !== null && features !== wantFeatures)) {\n // Indexing derives from presets, so a config edit rebuilding the cache must say so and\n // name what changed -- silent rebuilds make derived indexing look like a hang or a bug.\n if (version !== null && version !== SCHEMA_VERSION) {\n console.error('sense: cache format changed (new sensemaking version); rebuilding the index');\n closed = true;\n db.close();\n clearCache(cfg);\n return connect(cfg);\n }\n const changedKeys = changedSignatureKeys(features ?? '', wantFeatures);\n // Only the tokenizer moved, and frontmatter, links, sections and embeddings are file-derived\n // and tokenizer-independent. Any other signature change takes the full clear/reopen below.\n if (changedKeys.size === 1 && changedKeys.has('tokenize')) {\n console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');\n // One transaction, so a crash before COMMIT rolls back to the old tokenizer's table rather\n // than to no table. IF EXISTS makes a retry after such a crash a no-op, not a raw error.\n await withTransaction(\n conn,\n async () => {\n await conn.exec('DROP TABLE IF EXISTS content');\n await createContentTable(conn, tokenize);\n },\n BEGIN_WRITE\n );\n tokenizeOnlyRebuild = true;\n } else if (changedKeys.size === 1 && changedKeys.has('embed') && embedIdentityAdopted(features ?? '', wantFeatures)) {\n // First sight of a resolved weight identity: the model itself hasn't changed, so\n // adopt it into meta with no rebuild and no re-embed, mirroring the tokenize precedent.\n console.error(\"sense: recorded the embedding model's resolved identity; vectors are unaffected\");\n await setMeta(conn, 'features', wantFeatures);\n } else {\n const changed = signatureDiff(features ?? '', wantFeatures);\n console.error(`sense: config change (${changed}) rebuilds the index`);\n closed = true;\n db.close();\n clearCache(cfg);\n return connect(cfg);\n }\n }\n\n // Meta can lie after a crash between table creation and the signature write; the table's own\n // DDL cannot, so a mismatch here rebuilds whatever meta says.\n const stored = storedTokenize(db);\n if (stored !== null && stored !== tokenize) {\n console.error('sense: cache was built with a different content tokenizer; rebuilding the index');\n closed = true;\n db.close();\n clearCache(cfg);\n return connect(cfg);\n }\n\n // One writer at a time: the feature hooks check a column then add it, so two cold opens racing\n // here both see it missing and the second ALTER fails with a duplicate column.\n await withTransaction(conn, () => ensureSchema(conn, cfg, tokenize), BEGIN_WRITE);\n\n let rebuildWarnings: string[] = [];\n if (tokenizeOnlyRebuild) {\n rebuildWarnings = await rebuildContentTable(conn, cfg, cfg.baseDir);\n await setMeta(conn, 'features', wantFeatures);\n }\n\n // 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.\n // Installed before reconcile() -- that call is the one that races a watcher's transaction.\n const recordedMaxMs = Number((await getMeta(conn, 'reconcile_max_ms')) ?? '0');\n db.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600_000)}`);\n\n const { parsed, warnings } = await reconcile(conn, cfg, cfg.baseDir, sqliteDialect);\n\n return { db, conn, cfg, dbPath, parsed, warnings: [...rebuildWarnings, ...warnings] };\n } catch (err) {\n if (!closed) db.close();\n throw err;\n }\n}\n\n// The sqlite store's open: connects synchronously (see connect() above), then wraps the\n// resulting connection in the async Store interface.\nexport async function openSqlite(cfg: ResolvedConfig): Promise<OpenResult> {\n const { db, conn, cfg: resolvedCfg, dbPath, parsed, warnings } = await connect(cfg);\n const store = createStore(db, conn, resolvedCfg, resolvedCfg.baseDir);\n // reconcile ran before this object existed, so its chunk text is keyed by the connection.\n rekeyChunkText(conn, store);\n return { store: store, cfg: resolvedCfg, dbPath, parsed, warnings };\n}\n\nexport async function docCount(store: Store): Promise<number> {\n const stmt = await store.prepare('SELECT COUNT(*) AS n FROM frontmatter');\n return ((await stmt.get()) as { n: number }).n;\n}\n"],"names":["mkdirSync","join","DatabaseSync","contentTokenize","featureSignature","STATE_DIR","rekeyChunkText","SenseError","activeFeatures","FEATURES","clearCache","reconcile","getMeta","setMeta","BEGIN_WRITE","withTransaction","createConnection","sqliteDialect","changedSignatureKeys","embedIdentityAdopted","rebuildContentTable","signatureDiff","registerFunctions","createStore","DB_FILENAME","SCHEMA_VERSION","DEFAULT_TOKENIZE","resolveTokenize","db","cfg","configured","undefined","literal","replace","exec","err","message","storedTokenize","row","prepare","get","m","sql","match","createContentTable","conn","tokenize","ensureSchema","feature","schema","setJournalWal","deadline","Date","now","test","Atomics","wait","Int32Array","SharedArrayBuffer","connect","stateDir","baseDir","recursive","dbPath","closed","version","features","wantFeatures","tokenizeOnlyRebuild","console","error","close","changedKeys","size","has","changed","stored","rebuildWarnings","recordedMaxMs","Number","Math","min","max","parsed","warnings","openSqlite","resolvedCfg","store","docCount","stmt","n"],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,SAASA,SAAS,QAAQ,UAAU;AACpC,SAASC,IAAI,QAAQ,YAAY;AACjC,SAASC,YAAY,QAAQ,cAAc;AAE3C,SAASC,eAAe,EAAEC,gBAAgB,EAAEC,SAAS,QAAQ,wBAAwB;AACrF,SAASC,cAAc,QAAQ,yBAAyB;AACxD,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,SAASC,cAAc,EAAEC,QAAQ,QAAQ,0BAA0B;AACnE,SA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KAAKA,IAAIgC,OAAO,EAAE5C;QAErE,OAAO;YAAEW;YAAIiB;YAAMhB;YAAKkC;YAAQoB;YAAQC,UAAU;mBAAIP;mBAAoBO;aAAS;QAAC;IACtF,EAAE,OAAOjD,KAAK;QACZ,IAAI,CAAC6B,QAAQpC,GAAG2C,KAAK;QACrB,MAAMpC;IACR;AACF;AAEA,wFAAwF;AACxF,qDAAqD;AACrD,OAAO,eAAekD,WAAWxD,GAAmB;IAClD,MAAM,EAAED,EAAE,EAAEiB,IAAI,EAAEhB,KAAKyD,WAAW,EAAEvB,MAAM,EAAEoB,MAAM,EAAEC,QAAQ,EAAE,GAAG,MAAMzB,QAAQ9B;IAC/E,MAAM0D,QAAQhE,YAAYK,IAAIiB,MAAMyC,aAAaA,YAAYzB,OAAO;IACpE,0FAA0F;IAC1FvD,eAAeuC,MAAM0C;IACrB,OAAO;QAAEA,OAAOA;QAAO1D,KAAKyD;QAAavB;QAAQoB;QAAQC;IAAS;AACpE;AAEA,OAAO,eAAeI,SAASD,KAAY;IACzC,MAAME,OAAO,MAAMF,MAAMhD,OAAO,CAAC;IACjC,OAAO,AAAE,CAAA,MAAMkD,KAAKjD,GAAG,EAAC,EAAqBkD,CAAC;AAChD"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/sqlite/open.ts"],"sourcesContent":["// The Node floor (>=22.20) is explained here and nowhere else: 22.20 is the first release with\n// both FTS5 and row-returning INSERT ... RETURNING. Raise it only for a load-bearing capability.\nimport { DatabaseSync } from 'node:sqlite';\nimport type { Config, ResolvedConfig } from '../../config/index.ts';\nimport { contentTokenize, featureSignature } from '../../config/index.ts';\nimport { SenseError } from '../../errors.ts';\nimport { activeFeatures, FEATURES } from '../../features/index.ts';\nimport { listFiles, parseFile } from '../../scan/index.ts';\nimport type { OpenResult } from '../open.ts';\nimport { openWithDialect } from '../open.ts';\nimport { getMeta, setMeta } from '../shared.ts';\nimport { BEGIN_WRITE, withTransaction } from '../transaction.ts';\nimport type { Connection, OpenDialect } from '../types.ts';\nimport { createConnection } from './connection.ts';\nimport { contentRow, INSERT_CONTENT_SQL, sqliteDialect } from './reconcile.ts';\nimport { registerFunctions } from './sql-functions.ts';\nimport { createStore } from './store.ts';\n\nexport const DB_FILENAME = 'cache.db';\n// Cache shape version, independent of the config's own `version`. Bumping it rebuilds\n// existing trees on first query.\nexport const SCHEMA_VERSION = '19';\n\nexport type { OpenResult };\n\n// unicode61 splits on spaces, so a language written without them indexes a whole run as one\n// token and word search finds nothing; `content.tokenize` is how such a tree picks trigram.\nconst DEFAULT_TOKENIZE = 'porter unicode61';\n\n// The connection plus this store's own extra state (types.ts's OpenDialect<Handle>): the\n// resolved tokenizer, threaded through connect, the tokenize-only rebuild, and ensureSchema.\ninterface SqliteHandle {\n db: DatabaseSync;\n tokenize: string;\n}\n\n// FTS5 takes its tokenizer as a DDL string literal where nothing can bind, so the configured\n// value is concatenated; probing a throwaway table first is both the safety and the validation.\nfunction resolveTokenize(db: DatabaseSync, cfg: Config): string {\n const configured = contentTokenize(cfg);\n if (configured === undefined) return DEFAULT_TOKENIZE;\n const literal = configured.replace(/'/g, \"''\");\n try {\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n db.exec(`CREATE VIRTUAL TABLE temp.sense_tokenize_probe USING fts5(x, tokenize = '${literal}')`);\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n } catch (err) {\n throw new SenseError('CONFIG_INVALID', `content.tokenize \"${configured}\" is not a tokenizer this SQLite accepts (${(err as Error).message}); the built-in choices are unicode61, ascii, porter, and trigram, each with their own options`);\n }\n return literal;\n}\n\n// The tokenizer the content table was actually built with, from its own DDL -- the one\n// record that cannot desynchronize from the table. NULL when the table does not exist yet.\nfunction storedTokenize(db: DatabaseSync): string | null {\n const row = db.prepare(`SELECT sql FROM sqlite_master WHERE name = 'content'`).get() as { sql: string } | undefined;\n if (!row) return null;\n const m = row.sql.match(/tokenize = '((?:[^']|'')*)'/);\n return m ? m[1] : null;\n}\n\n// The `_seg` sidecars are appended after path, never inserted: bm25() and snippet(content, 2)\n// are documented against the first three columns. Each holds its field's exploded unspaced runs.\nasync function createContentTable(conn: Connection, tokenize: string): Promise<void> {\n await conn.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS content USING fts5(title, summary, text, path UNINDEXED, title_seg, summary_seg, text_seg, tokenize = '${tokenize}')`);\n}\n\n// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`\n// can't dump file text into context. Features add their own tables after the core ones.\nasync function ensureSchemaTables(conn: Connection, cfg: Config, tokenize: string): Promise<void> {\n await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_ctime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n // IF NOT EXISTS is safe against a tokenizer change: open() compares the table's own DDL\n // against the resolved tokenizer before this runs, so a stale table is already gone by now.\n await createContentTable(conn, tokenize);\n // Coverage, not ownership: a path can appear under several presets. path leads the PK so the\n // per-doc delete is an index hit -- keyed the other way, cold builds went quadratic.\n await conn.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n await conn.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) await feature.schema(conn);\n if ((await getMeta(conn, 'schema_version')) === null) await setMeta(conn, 'schema_version', SCHEMA_VERSION);\n if ((await getMeta(conn, 'features')) === null) await setMeta(conn, 'features', featureSignature(cfg, FEATURES));\n}\n\n// Two processes opening the same fresh tree both try to convert it, and the loser gets SQLITE_BUSY\n// with no busy handler behind it. Bounded because a lock held past this is a real problem, not a race.\nfunction setJournalWal(db: DatabaseSync): void {\n const deadline = Date.now() + 5000;\n for (;;) {\n try {\n db.exec('PRAGMA journal_mode = WAL');\n return;\n } catch (err) {\n if (Date.now() >= deadline || !/database is locked|busy/i.test((err as Error).message)) throw err;\n Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);\n }\n }\n}\n\nasync function connect(dbPath: string, cfg: ResolvedConfig): Promise<{ handle: SqliteHandle; conn: Connection }> {\n const db = new DatabaseSync(dbPath);\n try {\n // Before journal_mode, not after: converting a fresh database to WAL takes a brief exclusive\n // lock, and with no timeout set yet a second process opening the same tree fails in 1ms.\n db.exec('PRAGMA busy_timeout = 30000');\n // busy_timeout does not cover the WAL conversion itself: SQLite does not invoke the busy\n // handler for it, so a concurrent cold open needs its own bounded wait.\n setJournalWal(db);\n registerFunctions(db, contentTokenize(cfg) === undefined);\n const conn = createConnection(db);\n // Before the rebuild branch below, never after: that branch deletes the cache, so a typo'd\n // tokenizer validated later would cost a full re-index (and re-embed) to reach its own error.\n const tokenize = resolveTokenize(db, cfg);\n return { handle: { db, tokenize }, conn };\n } catch (err) {\n // A throw here must release this handle, or on Windows the leaked WAL db is undeletable and\n // scratch cleanup fails with EPERM/EBUSY.\n db.close();\n throw err;\n }\n}\n\nasync function close(handle: SqliteHandle): Promise<void> {\n handle.db.close();\n}\n\n// Only the tokenizer moved, and frontmatter, links, sections and embeddings are file-derived\n// and tokenizer-independent. Any other signature change takes the full clear/reopen in open.ts.\nasync function partialRebuild(handle: SqliteHandle, conn: Connection, _cfg: Config, changedKeys: Set<string>): Promise<boolean> {\n if (!(changedKeys.size === 1 && changedKeys.has('tokenize'))) return false;\n console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');\n // One transaction, so a crash before COMMIT rolls back to the old tokenizer's table rather\n // than to no table. IF EXISTS makes a retry after such a crash a no-op, not a raw error.\n await withTransaction(\n conn,\n async () => {\n await conn.exec('DROP TABLE IF EXISTS content');\n await createContentTable(conn, handle.tokenize);\n },\n BEGIN_WRITE\n );\n return true;\n}\n\n// Tokenize-only rebuild: only `content` is repopulated (the other tables are tokenizer-independent)\n// and no feature extractors run. Warnings come from the reparse, since mtimes stay untouched and reconcile would never reparse them.\nasync function postSchemaRebuild(_handle: SqliteHandle, conn: Connection, cfg: Config, baseDir: string, wantFeatures: string): Promise<string[]> {\n const stmt = await conn.prepare('SELECT \"path\" FROM frontmatter');\n const known = new Set(((await stmt.all()) as Array<{ path: string }>).map((r) => r.path));\n const files = listFiles(cfg, baseDir).filter((f) => known.has(f.relPath));\n const segmenting = contentTokenize(cfg) === undefined;\n const warnings: string[] = [];\n const rows: unknown[][] = [];\n for (const file of files) {\n const { doc, warnings: fileWarnings } = parseFile(file);\n warnings.push(...fileWarnings);\n rows.push(contentRow(doc, segmenting));\n }\n await withTransaction(\n conn,\n async () => {\n if (rows.length > 0) await conn.runBatch(INSERT_CONTENT_SQL, rows);\n },\n BEGIN_WRITE\n );\n await setMeta(conn, 'features', wantFeatures);\n return warnings;\n}\n\n// Meta can lie after a crash between table creation and the signature write; the table's own\n// DDL cannot, so a mismatch here rebuilds whatever meta says.\nfunction extraRebuildReason(handle: SqliteHandle): string | null {\n const stored = storedTokenize(handle.db);\n if (stored !== null && stored !== handle.tokenize) return 'cache was built with a different content tokenizer';\n return null;\n}\n\nasync function ensureSchema(handle: SqliteHandle, conn: Connection, cfg: Config): Promise<void> {\n // One writer at a time: the feature hooks check a column then add it, so two cold opens racing\n // here both see it missing and the second ALTER fails with a duplicate column.\n await withTransaction(conn, () => ensureSchemaTables(conn, cfg, handle.tokenize), BEGIN_WRITE);\n}\n\nasync function setDerivedBusyTimeout(handle: SqliteHandle, _conn: Connection, ms: number): Promise<void> {\n handle.db.exec(`PRAGMA busy_timeout = ${ms}`);\n}\n\n// This store's dialect (types.ts's OpenDialect) for the shared orchestration in store/open.ts.\nexport const sqliteOpenDialect: OpenDialect<SqliteHandle> = {\n filename: DB_FILENAME,\n schemaVersion: SCHEMA_VERSION,\n reconcileDialect: sqliteDialect,\n connect,\n close,\n partialRebuild,\n postSchemaRebuild,\n extraRebuildReason,\n ensureSchema,\n setDerivedBusyTimeout,\n createStore: (handle, conn, cfg) => createStore(handle.db, conn, cfg),\n};\n\nexport async function openSqlite(cfg: ResolvedConfig): Promise<OpenResult> {\n return openWithDialect(cfg, sqliteOpenDialect);\n}\n"],"names":["DatabaseSync","contentTokenize","featureSignature","SenseError","activeFeatures","FEATURES","listFiles","parseFile","openWithDialect","getMeta","setMeta","BEGIN_WRITE","withTransaction","createConnection","contentRow","INSERT_CONTENT_SQL","sqliteDialect","registerFunctions","createStore","DB_FILENAME","SCHEMA_VERSION","DEFAULT_TOKENIZE","resolveTokenize","db","cfg","configured","undefined","literal","replace","exec","err","message","storedTokenize","row","prepare","get","m","sql","match","createContentTable","conn","tokenize","ensureSchemaTables","feature","schema","setJournalWal","deadline","Date","now","test","Atomics","wait","Int32Array","SharedArrayBuffer","connect","dbPath","handle","close","partialRebuild","_cfg","changedKeys","size","has","console","error","postSchemaRebuild","_handle","baseDir","wantFeatures","stmt","known","Set","all","map","r","path","files","filter","f","relPath","segmenting","warnings","rows","file","doc","fileWarnings","push","length","runBatch","extraRebuildReason","stored","ensureSchema","setDerivedBusyTimeout","_conn","ms","sqliteOpenDialect","filename","schemaVersion","reconcileDialect","openSqlite"],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,SAASA,YAAY,QAAQ,cAAc;AAE3C,SAASC,eAAe,EAAEC,gBAAgB,QAAQ,wBAAwB;AAC1E,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,SAASC,cAAc,EAAEC,QAAQ,QAAQ,0BAA0B;AACnE,SAASC,SAAS,EAAEC,SAAS,QAAQ,sBAAsB;AAE3D,SAASC,eAAe,QAAQ,aAAa;AAC7C,SAASC,OAAO,EAAEC,OAAO,QAAQ,eAAe;AAChD,SAASC,WAAW,EAAEC,eAAe,QAAQ,oBAAoB;AAEjE,SAASC,gBAAgB,QAAQ,kBAAkB;AACnD,SAASC,UAAU,EAAEC,kBAAkB,EAAEC,aAAa,QAAQ,iBAAiB;AAC/E,SAASC,iBAAiB,QAAQ,qBAAqB;AACvD,SAASC,WAAW,QAAQ,aAA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AAG,GAAG,MAAM/C,KAAKgD,QAAQ,CAACzE,oBAAoBmE;IAC/D,GACAvE;IAEF,MAAMD,QAAQ8B,MAAM,YAAY4B;IAChC,OAAOa;AACT;AAEA,6FAA6F;AAC7F,8DAA8D;AAC9D,SAASQ,mBAAmBjC,MAAoB;IAC9C,MAAMkC,SAAS1D,eAAewB,OAAOjC,EAAE;IACvC,IAAImE,WAAW,QAAQA,WAAWlC,OAAOf,QAAQ,EAAE,OAAO;IAC1D,OAAO;AACT;AAEA,eAAekD,aAAanC,MAAoB,EAAEhB,IAAgB,EAAEhB,GAAW;IAC7E,+FAA+F;IAC/F,+EAA+E;IAC/E,MAAMZ,gBAAgB4B,MAAM,IAAME,mBAAmBF,MAAMhB,KAAKgC,OAAOf,QAAQ,GAAG9B;AACpF;AAEA,eAAeiF,sBAAsBpC,MAAoB,EAAEqC,KAAiB,EAAEC,EAAU;IACtFtC,OAAOjC,EAAE,CAACM,IAAI,CAAC,CAAC,sBAAsB,EAAEiE,IAAI;AAC9C;AAEA,+FAA+F;AAC/F,OAAO,MAAMC,oBAA+C;IAC1DC,UAAU7E;IACV8E,eAAe7E;IACf8E,kBAAkBlF;IAClBsC;IACAG;IACAC;IACAO;IACAwB;IACAE;IACAC;IACA1E,aAAa,CAACsC,QAAQhB,MAAMhB,MAAQN,YAAYsC,OAAOjC,EAAE,EAAEiB,MAAMhB;AACnE,EAAE;AAEF,OAAO,eAAe2E,WAAW3E,GAAmB;IAClD,OAAOhB,gBAAgBgB,KAAKuE;AAC9B"}
|
|
@@ -2,4 +2,4 @@ import type { DatabaseSync } from 'node:sqlite';
|
|
|
2
2
|
import type { Config } from '../../config/index.js';
|
|
3
3
|
import type { Capability, Connection, Store } from '../types.js';
|
|
4
4
|
export declare const CAPABILITIES: ReadonlySet<Capability>;
|
|
5
|
-
export declare function createStore(db: DatabaseSync, conn: Connection, cfg: Config
|
|
5
|
+
export declare function createStore(db: DatabaseSync, conn: Connection, cfg: Config): Store;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { contentTokenize } from '../../config/index.js';
|
|
2
|
-
import { reconcile } from '../reconcile.js';
|
|
3
2
|
import { getColumns } from '../shared.js';
|
|
4
3
|
import { withTransaction } from '../transaction.js';
|
|
5
4
|
import { hasVectorRow, pendingRows } from '../vectors.js';
|
|
6
5
|
import { fieldStats } from './fieldStats.js';
|
|
7
6
|
import { queryLexical } from './lexical.js';
|
|
8
|
-
import { sqliteDialect } from './reconcile.js';
|
|
9
7
|
import { scanCandidates, scanSimilar, writeVectorBatch } from './vectors.js';
|
|
10
8
|
export const CAPABILITIES = new Set([
|
|
11
9
|
'phrases',
|
|
@@ -16,8 +14,8 @@ export const CAPABILITIES = new Set([
|
|
|
16
14
|
'sql-functions'
|
|
17
15
|
]);
|
|
18
16
|
// Wraps the synchronous DatabaseSync connection in the async Store interface, sharing one
|
|
19
|
-
// Connection instance (conn) with
|
|
20
|
-
export function createStore(db, conn, cfg
|
|
17
|
+
// Connection instance (conn) with the builder's own reconcile call so transaction depth is tracked against the same object everywhere.
|
|
18
|
+
export function createStore(db, conn, cfg) {
|
|
21
19
|
return {
|
|
22
20
|
name: 'sqlite',
|
|
23
21
|
capabilities: CAPABILITIES,
|
|
@@ -33,9 +31,6 @@ export function createStore(db, conn, cfg, baseDir) {
|
|
|
33
31
|
async transaction (fn) {
|
|
34
32
|
return withTransaction(conn, fn);
|
|
35
33
|
},
|
|
36
|
-
async reconcile () {
|
|
37
|
-
return reconcile(conn, cfg, baseDir, sqliteDialect);
|
|
38
|
-
},
|
|
39
34
|
docs: {
|
|
40
35
|
async columns () {
|
|
41
36
|
return [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/sqlite/store.ts"],"sourcesContent":["import type { DatabaseSync } from 'node:sqlite';\nimport type { Config } from '../../config/index.ts';\nimport { contentTokenize } from '../../config/index.ts';\nimport {
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/sqlite/store.ts"],"sourcesContent":["import type { DatabaseSync } from 'node:sqlite';\nimport type { Config } from '../../config/index.ts';\nimport { contentTokenize } from '../../config/index.ts';\nimport { getColumns } from '../shared.ts';\nimport { withTransaction } from '../transaction.ts';\nimport type { Capability, Connection, Statement, Store, VectorWriteRow } from '../types.ts';\nimport { hasVectorRow, pendingRows } from '../vectors.ts';\nimport { fieldStats } from './fieldStats.ts';\nimport { queryLexical } from './lexical.ts';\nimport { scanCandidates, scanSimilar, writeVectorBatch } from './vectors.ts';\n\nexport const CAPABILITIES: ReadonlySet<Capability> = new Set(['phrases', 'snippets', 'watch-concurrency', 'lexical', 'vectors', 'sql-functions']);\n\n// Wraps the synchronous DatabaseSync connection in the async Store interface, sharing one\n// Connection instance (conn) with the builder's own reconcile call so transaction depth is tracked against the same object everywhere.\nexport function createStore(db: DatabaseSync, conn: Connection, cfg: Config): Store {\n return {\n name: 'sqlite',\n capabilities: CAPABILITIES,\n async exec(sql: string): Promise<void> {\n await conn.exec(sql);\n },\n async prepare(sql: string): Promise<Statement> {\n return conn.prepare(sql);\n },\n async runBatch(sql: string, paramRows: unknown[][]): Promise<void> {\n await conn.runBatch(sql, paramRows);\n },\n async transaction<T>(fn: () => Promise<T>): Promise<T> {\n return withTransaction(conn, fn);\n },\n docs: {\n async columns() {\n return [...(await getColumns(conn))];\n },\n async fieldStats(columns, scopeWhere) {\n return fieldStats(conn, columns, scopeWhere);\n },\n },\n lexical: {\n async query(terms, opts) {\n return queryLexical(conn, terms, opts, contentTokenize(cfg) === undefined);\n },\n },\n vectors: {\n async pending() {\n return pendingRows(conn);\n },\n async writeVectors(rows: VectorWriteRow[]) {\n await writeVectorBatch(conn, rows);\n },\n async candidates(qv, storeDims, fetch, allowed) {\n return scanCandidates(conn, qv, storeDims, fetch, allowed);\n },\n async similar(path, opts) {\n return scanSimilar(conn, path, opts);\n },\n async hasVector(path) {\n return hasVectorRow(conn, path);\n },\n },\n async engineStatus() {\n // Read back rather than recomputed: this is what open() actually set (3x the largest\n // recorded reconcile, floored at 30s, capped at 10min), not a value re-derived here.\n const row = db.prepare('PRAGMA busy_timeout').get() as { timeout: number };\n return { busy_timeout: `${row.timeout}ms (derived: 3x the largest reconcile this cache has recorded, floored at 30000ms)` };\n },\n raw: {\n async prepare(sql: string) {\n const stmt = db.prepare(sql);\n stmt.setReadBigInts(true); // int64 past 2^53 arrives as BigInt instead of throwing at step time\n return {\n columns: () => stmt.columns(),\n // sense sql streams through an async iterator wrapping the sync iterate().\n iterate: async function* (...params: unknown[]) {\n yield* stmt.iterate(...(params as Parameters<typeof stmt.iterate>));\n },\n };\n },\n },\n async close() {\n db.close();\n },\n };\n}\n"],"names":["contentTokenize","getColumns","withTransaction","hasVectorRow","pendingRows","fieldStats","queryLexical","scanCandidates","scanSimilar","writeVectorBatch","CAPABILITIES","Set","createStore","db","conn","cfg","name","capabilities","exec","sql","prepare","runBatch","paramRows","transaction","fn","docs","columns","scopeWhere","lexical","query","terms","opts","undefined","vectors","pending","writeVectors","rows","candidates","qv","storeDims","fetch","allowed","similar","path","hasVector","engineStatus","row","get","busy_timeout","timeout","raw","stmt","setReadBigInts","iterate","params","close"],"mappings":"AAEA,SAASA,eAAe,QAAQ,wBAAwB;AACxD,SAASC,UAAU,QAAQ,eAAe;AAC1C,SAASC,eAAe,QAAQ,oBAAoB;AAEpD,SAASC,YAAY,EAAEC,WAAW,QAAQ,gBAAgB;AAC1D,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,SAASC,YAAY,QAAQ,eAAe;AAC5C,SAASC,cAAc,EAAEC,WAAW,EAAEC,gBAAgB,QAAQ,eAAe;AAE7E,OAAO,MAAMC,eAAwC,IAAIC,IAAI;IAAC;IAAW;IAAY;IAAqB;IAAW;IAAW;CAAgB,EAAE;AAElJ,0FAA0F;AAC1F,uIAAuI;AACvI,OAAO,SAASC,YAAYC,EAAgB,EAAEC,IAAgB,EAAEC,GAAW;IACzE,OAAO;QAC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AGO,OAAO,CAACD;gBACxBgC,KAAKC,cAAc,CAAC,OAAO,qEAAqE;gBAChG,OAAO;oBACL1B,SAAS,IAAMyB,KAAKzB,OAAO;oBAC3B,2EAA2E;oBAC3E2B,SAAS,gBAAiB,GAAGC,MAAiB;wBAC5C,OAAOH,KAAKE,OAAO,IAAKC;oBAC1B;gBACF;YACF;QACF;QACA,MAAMC;YACJ1C,GAAG0C,KAAK;QACV;IACF;AACF"}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
+
import type { Database } from '@tursodatabase/database';
|
|
1
2
|
import type { ResolvedConfig } from '../../config/index.js';
|
|
2
|
-
import type {
|
|
3
|
+
import type { OpenResult } from '../open.js';
|
|
4
|
+
import type { OpenDialect } from '../types.js';
|
|
3
5
|
export declare const DB_FILENAME = "cache.turso.db";
|
|
4
|
-
export declare const SCHEMA_VERSION = "
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
cfg: ResolvedConfig;
|
|
8
|
-
dbPath: string;
|
|
9
|
-
parsed: number;
|
|
10
|
-
warnings: string[];
|
|
11
|
-
}
|
|
12
|
-
export declare const CONTENT_FTS_DDL: readonly ["CREATE INDEX IF NOT EXISTS content_fts ON content USING fts (title, summary, text) WITH (weights = 'title=10.0,summary=5.0,text=1.0')", "CREATE INDEX IF NOT EXISTS content_fts_ngram ON content USING fts (title_ngram, summary_ngram, text_ngram) WITH (tokenizer='ngram', weights='title_ngram=10.0,summary_ngram=5.0,text_ngram=1.0')"];
|
|
13
|
-
export declare const CONTENT_FTS_NAMES: readonly ["content_fts", "content_fts_ngram"];
|
|
6
|
+
export declare const SCHEMA_VERSION = "4";
|
|
7
|
+
export type { OpenResult };
|
|
8
|
+
export declare const tursoOpenDialect: OpenDialect<Database>;
|
|
14
9
|
export declare function openTurso(cfg: ResolvedConfig): Promise<OpenResult>;
|
|
@@ -1,34 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
import { featureSignature, STATE_DIR } from '../../config/index.js';
|
|
4
|
-
import { rekeyChunkText } from '../../embed/handoff.js';
|
|
1
|
+
import { featureSignature } from '../../config/index.js';
|
|
5
2
|
import { STORE_DIMS } from '../../embed/types.js';
|
|
6
3
|
import { SenseError } from '../../errors.js';
|
|
7
4
|
import { activeFeatures, FEATURES } from '../../features/index.js';
|
|
8
|
-
import {
|
|
9
|
-
import { reconcile } from '../reconcile.js';
|
|
5
|
+
import { openWithDialect } from '../open.js';
|
|
10
6
|
import { getMeta, setMeta } from '../shared.js';
|
|
11
7
|
import { createConnection } from './connection.js';
|
|
12
8
|
import { TURSO_PACKAGE, tursoApi } from './native.js';
|
|
13
|
-
import { tursoDialect } from './reconcile.js';
|
|
9
|
+
import { CONTENT_FTS_DDL, tursoDialect } from './reconcile.js';
|
|
14
10
|
import { createStore } from './store.js';
|
|
15
11
|
export const DB_FILENAME = 'cache.turso.db';
|
|
16
12
|
// Independent of sqlite's and duckdb's SCHEMA_VERSION: each store's cache shape evolves
|
|
17
13
|
// separately. Covers the FTS indexes, the "_ngram" sidecar columns, and embeddings.vector's width.
|
|
18
|
-
export const SCHEMA_VERSION = '
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
// Tantivy maintains them per inserted row, which is quadratic in what is already indexed.
|
|
23
|
-
export const CONTENT_FTS_DDL = [
|
|
24
|
-
`CREATE INDEX IF NOT EXISTS content_fts ON content USING fts (title, summary, text) WITH (weights = 'title=10.0,summary=5.0,text=1.0')`,
|
|
25
|
-
`CREATE INDEX IF NOT EXISTS content_fts_ngram ON content USING fts (title_ngram, summary_ngram, text_ngram) WITH (tokenizer='ngram', weights='title_ngram=10.0,summary_ngram=5.0,text_ngram=1.0')`
|
|
26
|
-
];
|
|
27
|
-
export const CONTENT_FTS_NAMES = [
|
|
28
|
-
'content_fts',
|
|
29
|
-
'content_fts_ngram'
|
|
30
|
-
];
|
|
31
|
-
async function ensureSchema(conn, cfg) {
|
|
14
|
+
export const SCHEMA_VERSION = '4';
|
|
15
|
+
// This store's Handle (types.ts's OpenDialect<Handle>) is the connected Database itself: no
|
|
16
|
+
// extra native state to thread, unlike duckdb's separate instance/connection pair.
|
|
17
|
+
async function ensureSchema(_handle, conn, cfg) {
|
|
32
18
|
await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter ("path" TEXT PRIMARY KEY, "_mtime" REAL, "_ctime" REAL, "_size" INTEGER, "_parse_error" TEXT)`);
|
|
33
19
|
await conn.exec(`CREATE TABLE IF NOT EXISTS content ("path" TEXT PRIMARY KEY, title TEXT, summary TEXT, text TEXT, title_ngram TEXT, summary_ngram TEXT, text_ngram TEXT)`);
|
|
34
20
|
for (const ddl of CONTENT_FTS_DDL)await conn.exec(ddl);
|
|
@@ -46,12 +32,13 @@ async function ensureSchema(conn, cfg) {
|
|
|
46
32
|
if (await getMeta(conn, 'schema_version') === null) await setMeta(conn, 'schema_version', SCHEMA_VERSION);
|
|
47
33
|
if (await getMeta(conn, 'features') === null) await setMeta(conn, 'features', featureSignature(cfg, FEATURES));
|
|
48
34
|
}
|
|
49
|
-
async function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
|
|
35
|
+
async function close(handle) {
|
|
36
|
+
await handle.close();
|
|
37
|
+
}
|
|
38
|
+
async function setDerivedBusyTimeout(_handle, conn, ms) {
|
|
39
|
+
await conn.exec(`PRAGMA busy_timeout = ${ms}`);
|
|
40
|
+
}
|
|
41
|
+
async function connect(dbPath, _cfg) {
|
|
55
42
|
// Dynamic, not a top-level import: a sqlite or duckdb tree must never attempt to resolve this
|
|
56
43
|
// optional dependency until a turso tree is actually opened. Installed on first use if missing.
|
|
57
44
|
let turso;
|
|
@@ -74,57 +61,22 @@ async function connect(cfg) {
|
|
|
74
61
|
} catch (err) {
|
|
75
62
|
throw new SenseError('STORE_DEPENDENCY_MISSING', `store "turso" failed to open ${dbPath}: ${err.message}`);
|
|
76
63
|
}
|
|
77
|
-
// A throw below must release this handle, or the leaked WAL makes the db file undeletable on
|
|
78
|
-
// Windows. closed guards the catch against double-closing.
|
|
79
|
-
let closed = false;
|
|
80
|
-
try {
|
|
81
|
-
var _ref;
|
|
82
|
-
const conn = createConnection(db);
|
|
83
|
-
await conn.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');
|
|
84
|
-
const version = await getMeta(conn, 'schema_version');
|
|
85
|
-
const features = await getMeta(conn, 'features');
|
|
86
|
-
const wantFeatures = featureSignature(cfg, FEATURES);
|
|
87
|
-
if (version !== null && version !== SCHEMA_VERSION || features !== null && features !== wantFeatures) {
|
|
88
|
-
// Reconcile only reparses changed files, so a stale cache is rebuilt rather than patched
|
|
89
|
-
// incrementally. No tokenize-only or embed-identity-adopted partial path: any mismatch rebuilds.
|
|
90
|
-
const reason = version !== null && version !== SCHEMA_VERSION ? 'cache format changed (new sensemaking version)' : 'config change (features, tokenizer, or presets)';
|
|
91
|
-
console.error(`sense: ${reason}; rebuilding the index`);
|
|
92
|
-
closed = true;
|
|
93
|
-
await db.close();
|
|
94
|
-
clearCache(cfg);
|
|
95
|
-
return connect(cfg);
|
|
96
|
-
}
|
|
97
|
-
await ensureSchema(conn, cfg);
|
|
98
|
-
// 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.
|
|
99
|
-
// Installed before reconcile() -- that call is the one that races a watcher's transaction.
|
|
100
|
-
const recordedMaxMs = Number((_ref = await getMeta(conn, 'reconcile_max_ms')) !== null && _ref !== void 0 ? _ref : '0');
|
|
101
|
-
await conn.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600000)}`);
|
|
102
|
-
const { parsed, warnings } = await reconcile(conn, cfg, cfg.baseDir, tursoDialect);
|
|
103
|
-
return {
|
|
104
|
-
db,
|
|
105
|
-
conn,
|
|
106
|
-
cfg,
|
|
107
|
-
dbPath,
|
|
108
|
-
parsed,
|
|
109
|
-
warnings
|
|
110
|
-
};
|
|
111
|
-
} catch (err) {
|
|
112
|
-
if (!closed) await db.close();
|
|
113
|
-
throw err;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
// The turso store's open: connects (fully async, see connect() above), then wraps the resulting
|
|
117
|
-
// connection in the Store interface.
|
|
118
|
-
export async function openTurso(cfg) {
|
|
119
|
-
const { db, conn, cfg: resolvedCfg, dbPath, parsed, warnings } = await connect(cfg);
|
|
120
|
-
const store = createStore(db, conn, resolvedCfg, resolvedCfg.baseDir);
|
|
121
|
-
// reconcile ran before this object existed, so its chunk text is keyed by the connection.
|
|
122
|
-
rekeyChunkText(conn, store);
|
|
123
64
|
return {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
dbPath,
|
|
127
|
-
parsed,
|
|
128
|
-
warnings
|
|
65
|
+
handle: db,
|
|
66
|
+
conn: createConnection(db)
|
|
129
67
|
};
|
|
130
68
|
}
|
|
69
|
+
// This store's dialect (types.ts's OpenDialect) for the shared orchestration in store/open.ts.
|
|
70
|
+
export const tursoOpenDialect = {
|
|
71
|
+
filename: DB_FILENAME,
|
|
72
|
+
schemaVersion: SCHEMA_VERSION,
|
|
73
|
+
reconcileDialect: tursoDialect,
|
|
74
|
+
connect,
|
|
75
|
+
close,
|
|
76
|
+
ensureSchema,
|
|
77
|
+
setDerivedBusyTimeout,
|
|
78
|
+
createStore: (handle, conn)=>createStore(handle, conn)
|
|
79
|
+
};
|
|
80
|
+
export async function openTurso(cfg) {
|
|
81
|
+
return openWithDialect(cfg, tursoOpenDialect);
|
|
82
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/turso/open.ts"],"sourcesContent":["import { mkdirSync } from 'node:fs';\nimport { join } from 'node:path';\nimport type { Database } from '@tursodatabase/database';\nimport type { Config, ResolvedConfig } from '../../config/index.ts';\nimport { featureSignature, STATE_DIR } from '../../config/index.ts';\nimport { rekeyChunkText } from '../../embed/handoff.ts';\nimport { STORE_DIMS } from '../../embed/types.ts';\nimport { SenseError } from '../../errors.ts';\nimport { activeFeatures, FEATURES } from '../../features/index.ts';\nimport { clearCache } from '../cache.ts';\nimport { reconcile } from '../reconcile.ts';\nimport { getMeta, setMeta } from '../shared.ts';\nimport type { Connection, Store } from '../types.ts';\nimport { createConnection } from './connection.ts';\nimport { TURSO_PACKAGE, tursoApi } from './native.ts';\nimport { tursoDialect } from './reconcile.ts';\nimport { createStore } from './store.ts';\n\nexport const DB_FILENAME = 'cache.turso.db';\n// Independent of sqlite's and duckdb's SCHEMA_VERSION: each store's cache shape evolves\n// separately. Covers the FTS indexes, the \"_ngram\" sidecar columns, and embeddings.vector's width.\nexport const SCHEMA_VERSION = '3';\n\nexport interface OpenResult {\n store: Store;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\n// The ngram index is scoped to disjoint \"_ngram\" sidecar columns: a second index over the same\n// columns makes a bare substring match a whole word, defeating the prefix-query rejection.\n// The two FTS indexes, named here because reconcile drops and rebuilds them around a bulk load:\n// Tantivy maintains them per inserted row, which is quadratic in what is already indexed.\nexport const CONTENT_FTS_DDL = [\n `CREATE INDEX IF NOT EXISTS content_fts ON content USING fts (title, summary, text) WITH (weights = 'title=10.0,summary=5.0,text=1.0')`,\n `CREATE INDEX IF NOT EXISTS content_fts_ngram ON content USING fts (title_ngram, summary_ngram, text_ngram) WITH (tokenizer='ngram', weights='title_ngram=10.0,summary_ngram=5.0,text_ngram=1.0')`,\n] as const;\nexport const CONTENT_FTS_NAMES = ['content_fts', 'content_fts_ngram'] as const;\n\nasync function ensureSchema(conn: Connection, cfg: Config): Promise<void> {\n await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_ctime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n await conn.exec(`CREATE TABLE IF NOT EXISTS content (\"path\" TEXT PRIMARY KEY, title TEXT, summary TEXT, text TEXT, title_ngram TEXT, summary_ngram TEXT, text_ngram TEXT)`);\n for (const ddl of CONTENT_FTS_DDL) await conn.exec(ddl);\n await conn.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n await conn.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) {\n // Native F32_BLOB(STORE_DIMS) instead of the embed feature's engine-neutral BLOB DDL. `scale`\n // is kept unused, so the shared reconcile-time INSERT/DELETE names a column both stores have.\n if (feature.name === 'embed') {\n await conn.exec(`CREATE TABLE IF NOT EXISTS embeddings (\"path\" TEXT, chunk INTEGER, start_line INTEGER, end_line INTEGER, scale REAL, vector F32_BLOB(${STORE_DIMS}), PRIMARY KEY (\"path\", chunk))`);\n continue;\n }\n await feature.schema(conn);\n }\n if ((await getMeta(conn, 'schema_version')) === null) await setMeta(conn, 'schema_version', SCHEMA_VERSION);\n if ((await getMeta(conn, 'features')) === null) await setMeta(conn, 'features', featureSignature(cfg, FEATURES));\n}\n\ninterface ConnectResult {\n db: Database;\n conn: Connection;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\nasync function connect(cfg: ResolvedConfig): Promise<ConnectResult> {\n const stateDir = join(cfg.baseDir, STATE_DIR);\n mkdirSync(stateDir, { recursive: true });\n const dbPath = join(stateDir, DB_FILENAME);\n\n // Dynamic, not a top-level import: a sqlite or duckdb tree must never attempt to resolve this\n // optional dependency until a turso tree is actually opened. Installed on first use if missing.\n let turso: Awaited<ReturnType<typeof tursoApi>>;\n try {\n turso = await tursoApi();\n } catch (err) {\n if (err instanceof SenseError) throw err;\n throw new SenseError('STORE_DEPENDENCY_MISSING', `store \"turso\" needs the ${TURSO_PACKAGE} package (${(err as Error).message})`);\n }\n\n let db: Database;\n try {\n // Floored at the same 30s sqlite opens with. `timeout` is connect-time only in this client;\n // the derived value is set via runtime PRAGMA below. `index_method` is required for ensureSchema()'s FTS indexes (T1).\n db = await turso.connect(dbPath, { timeout: 30_000, experimental: ['index_method'] });\n } catch (err) {\n throw new SenseError('STORE_DEPENDENCY_MISSING', `store \"turso\" failed to open ${dbPath}: ${(err as Error).message}`);\n }\n // A throw below must release this handle, or the leaked WAL makes the db file undeletable on\n // Windows. closed guards the catch against double-closing.\n let closed = false;\n try {\n const conn = createConnection(db);\n\n await conn.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');\n\n const version = await getMeta(conn, 'schema_version');\n const features = await getMeta(conn, 'features');\n const wantFeatures = featureSignature(cfg, FEATURES);\n if ((version !== null && version !== SCHEMA_VERSION) || (features !== null && features !== wantFeatures)) {\n // Reconcile only reparses changed files, so a stale cache is rebuilt rather than patched\n // incrementally. No tokenize-only or embed-identity-adopted partial path: any mismatch rebuilds.\n const reason = version !== null && version !== SCHEMA_VERSION ? 'cache format changed (new sensemaking version)' : 'config change (features, tokenizer, or presets)';\n console.error(`sense: ${reason}; rebuilding the index`);\n closed = true;\n await db.close();\n clearCache(cfg);\n return connect(cfg);\n }\n\n await ensureSchema(conn, cfg);\n\n // 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.\n // Installed before reconcile() -- that call is the one that races a watcher's transaction.\n const recordedMaxMs = Number((await getMeta(conn, 'reconcile_max_ms')) ?? '0');\n await conn.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600_000)}`);\n\n const { parsed, warnings } = await reconcile(conn, cfg, cfg.baseDir, tursoDialect);\n\n return { db, conn, cfg, dbPath, parsed, warnings };\n } catch (err) {\n if (!closed) await db.close();\n throw err;\n }\n}\n\n// The turso store's open: connects (fully async, see connect() above), then wraps the resulting\n// connection in the Store interface.\nexport async function openTurso(cfg: ResolvedConfig): Promise<OpenResult> {\n const { db, conn, cfg: resolvedCfg, dbPath, parsed, warnings } = await connect(cfg);\n const store = createStore(db, conn, resolvedCfg, resolvedCfg.baseDir);\n // reconcile ran before this object existed, so its chunk text is keyed by the connection.\n rekeyChunkText(conn, store);\n return { store: store, cfg: resolvedCfg, dbPath, parsed, warnings };\n}\n"],"names":["mkdirSync","join","featureSignature","STATE_DIR","rekeyChunkText","STORE_DIMS","SenseError","activeFeatures","FEATURES","clearCache","reconcile","getMeta","setMeta","createConnection","TURSO_PACKAGE","tursoApi","tursoDialect","createStore","DB_FILENAME","SCHEMA_VERSION","CONTENT_FTS_DDL","CONTENT_FTS_NAMES","ensureSchema","conn","cfg","exec","ddl","feature","name","schema","connect","stateDir","baseDir","recursive","dbPath","turso","err","message","db","timeout","experimental","closed","version","features","wantFeatures","reason","console","error","close","recordedMaxMs","Number","Math","min","max","parsed","warnings","openTurso","resolvedCfg","store"],"mappings":"AAAA,SAASA,SAAS,QAAQ,UAAU;AACpC,SAASC,IAAI,QAAQ,YAAY;AAGjC,SAASC,gBAAgB,EAAEC,SAAS,QAAQ,wBAAwB;AACpE,SAASC,cAAc,QAAQ,yBAAyB;AACxD,SAASC,UAAU,QAAQ,uBAAuB;AAClD,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,SAASC,cAAc,EAAEC,QAAQ,QAAQ,0BAA0B;AACnE,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,SAAS,QAAQ,kBAAkB;AAC5C,SAASC,OAAO,EAAEC,OAAO,QAAQ,eAAe;AAEhD,SAASC,gBAAgB,QAAQ,kBAAkB;AACnD,SAASC,aAAa,EAAEC,QAAQ,QAAQ,cAAc;AACtD,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,WAAW,QAAQ,aAAa;AAEzC,OAAO,MAAMC,cAAc,iBAAiB;AAC5C,wFAAwF;AACxF,mGAAmG;AACnG,OAAO,MAAMC,iBAAiB,IAAI;AAUlC,+FAA+F;AAC/F,2FAA2F;AAC3F,gGAAgG;AAChG,0FAA0F;AAC1F,OAAO,MAAMC,kBAAkB;IAC7B,CAAC,qIAAqI,CAAC;IACvI,CAAC,gMAAgM,CAAC;CACnM,CAAU;A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AQoB;YAAQC;QAAS;IACnD,EAAE,OAAOnB,KAAK;QACZ,IAAI,CAACK,QAAQ,MAAMH,GAAGU,KAAK;QAC3B,MAAMZ;IACR;AACF;AAEA,gGAAgG;AAChG,qCAAqC;AACrC,OAAO,eAAeoB,UAAUhC,GAAmB;IACjD,MAAM,EAAEc,EAAE,EAAEf,IAAI,EAAEC,KAAKiC,WAAW,EAAEvB,MAAM,EAAEoB,MAAM,EAAEC,QAAQ,EAAE,GAAG,MAAMzB,QAAQN;IAC/E,MAAMkC,QAAQzC,YAAYqB,IAAIf,MAAMkC,aAAaA,YAAYzB,OAAO;IACpE,0FAA0F;IAC1F5B,eAAemB,MAAMmC;IACrB,OAAO;QAAEA,OAAOA;QAAOlC,KAAKiC;QAAavB;QAAQoB;QAAQC;IAAS;AACpE"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/store/turso/open.ts"],"sourcesContent":["import type { Database } from '@tursodatabase/database';\nimport type { Config, ResolvedConfig } from '../../config/index.ts';\nimport { featureSignature } from '../../config/index.ts';\nimport { STORE_DIMS } from '../../embed/types.ts';\nimport { SenseError } from '../../errors.ts';\nimport { activeFeatures, FEATURES } from '../../features/index.ts';\nimport type { OpenResult } from '../open.ts';\nimport { openWithDialect } from '../open.ts';\nimport { getMeta, setMeta } from '../shared.ts';\nimport type { Connection, OpenDialect } from '../types.ts';\nimport { createConnection } from './connection.ts';\nimport { TURSO_PACKAGE, tursoApi } from './native.ts';\nimport { CONTENT_FTS_DDL, tursoDialect } from './reconcile.ts';\nimport { createStore } from './store.ts';\n\nexport const DB_FILENAME = 'cache.turso.db';\n// Independent of sqlite's and duckdb's SCHEMA_VERSION: each store's cache shape evolves\n// separately. Covers the FTS indexes, the \"_ngram\" sidecar columns, and embeddings.vector's width.\nexport const SCHEMA_VERSION = '4';\n\nexport type { OpenResult };\n\n// This store's Handle (types.ts's OpenDialect<Handle>) is the connected Database itself: no\n// extra native state to thread, unlike duckdb's separate instance/connection pair.\nasync function ensureSchema(_handle: Database, conn: Connection, cfg: Config): Promise<void> {\n await conn.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_ctime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n await conn.exec(`CREATE TABLE IF NOT EXISTS content (\"path\" TEXT PRIMARY KEY, title TEXT, summary TEXT, text TEXT, title_ngram TEXT, summary_ngram TEXT, text_ngram TEXT)`);\n for (const ddl of CONTENT_FTS_DDL) await conn.exec(ddl);\n await conn.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n await conn.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) {\n // Native F32_BLOB(STORE_DIMS) instead of the embed feature's engine-neutral BLOB DDL. `scale`\n // is kept unused, so the shared reconcile-time INSERT/DELETE names a column both stores have.\n if (feature.name === 'embed') {\n await conn.exec(`CREATE TABLE IF NOT EXISTS embeddings (\"path\" TEXT, chunk INTEGER, start_line INTEGER, end_line INTEGER, scale REAL, vector F32_BLOB(${STORE_DIMS}), PRIMARY KEY (\"path\", chunk))`);\n continue;\n }\n await feature.schema(conn);\n }\n if ((await getMeta(conn, 'schema_version')) === null) await setMeta(conn, 'schema_version', SCHEMA_VERSION);\n if ((await getMeta(conn, 'features')) === null) await setMeta(conn, 'features', featureSignature(cfg, FEATURES));\n}\n\nasync function close(handle: Database): Promise<void> {\n await handle.close();\n}\n\nasync function setDerivedBusyTimeout(_handle: Database, conn: Connection, ms: number): Promise<void> {\n await conn.exec(`PRAGMA busy_timeout = ${ms}`);\n}\n\nasync function connect(dbPath: string, _cfg: ResolvedConfig): Promise<{ handle: Database; conn: Connection }> {\n // Dynamic, not a top-level import: a sqlite or duckdb tree must never attempt to resolve this\n // optional dependency until a turso tree is actually opened. Installed on first use if missing.\n let turso: Awaited<ReturnType<typeof tursoApi>>;\n try {\n turso = await tursoApi();\n } catch (err) {\n if (err instanceof SenseError) throw err;\n throw new SenseError('STORE_DEPENDENCY_MISSING', `store \"turso\" needs the ${TURSO_PACKAGE} package (${(err as Error).message})`);\n }\n\n let db: Database;\n try {\n // Floored at the same 30s sqlite opens with. `timeout` is connect-time only in this client;\n // the derived value is set via runtime PRAGMA below. `index_method` is required for ensureSchema()'s FTS indexes (T1).\n db = await turso.connect(dbPath, { timeout: 30_000, experimental: ['index_method'] });\n } catch (err) {\n throw new SenseError('STORE_DEPENDENCY_MISSING', `store \"turso\" failed to open ${dbPath}: ${(err as Error).message}`);\n }\n return { handle: db, conn: createConnection(db) };\n}\n\n// This store's dialect (types.ts's OpenDialect) for the shared orchestration in store/open.ts.\nexport const tursoOpenDialect: OpenDialect<Database> = {\n filename: DB_FILENAME,\n schemaVersion: SCHEMA_VERSION,\n reconcileDialect: tursoDialect,\n connect,\n close,\n ensureSchema,\n setDerivedBusyTimeout,\n createStore: (handle, conn) => createStore(handle, conn),\n};\n\nexport async function openTurso(cfg: ResolvedConfig): Promise<OpenResult> {\n return openWithDialect(cfg, tursoOpenDialect);\n}\n"],"names":["featureSignature","STORE_DIMS","SenseError","activeFeatures","FEATURES","openWithDialect","getMeta","setMeta","createConnection","TURSO_PACKAGE","tursoApi","CONTENT_FTS_DDL","tursoDialect","createStore","DB_FILENAME","SCHEMA_VERSION","ensureSchema","_handle","conn","cfg","exec","ddl","feature","name","schema","close","handle","setDerivedBusyTimeout","ms","connect","dbPath","_cfg","turso","err","message","db","timeout","experimental","tursoOpenDialect","filename","schemaVersion","reconcileDialect","openTurso"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,UAAU,QAAQ,uBAAuB;AAClD,SAASC,UAAU,QAAQ,kBAAkB;AAC7C,SAASC,cAAc,EAAEC,QAAQ,QAAQ,0BAA0B;AAEnE,SAASC,eAAe,QAAQ,aAAa;AAC7C,SAASC,OAAO,EAAEC,OAA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