mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-24 06:06:02 +09:00
fix(graph): migrate agentdb-tools.ts db.exec(sql, params) to better-sqlite3 API; checkpoint trajectory WAL
Two distinct issues, both downstream of graph-schema-smoke being unblocked. 1. **agentdb_graph-query (4 call sites) — SQLITE_MISMATCH "datatype mismatch"** After #2431 migrated the graph writer from sql.js to better-sqlite3, the corresponding *query* paths in agentdb-tools.ts still called \`db.exec(sql, params)\` (sql.js semantic). better-sqlite3's exec is a parameterless runner — the second arg is silently ignored, the \`?\` placeholder in \`LIMIT ?\` binds to nothing, SQLite rejects with "datatype mismatch" (SQLITE_MISMATCH). Migrated all 4 sites — k-hop CTE, semantic mode, pagerank mode, pathfinder — to \`db.prepare(sql).raw().all(params)\`, which gives the same array-of-arrays shape the downstream code expects. Reproduced bare-metal: \`db.exec("SELECT * FROM foo LIMIT ?", [5])\` → SqliteError: datatype mismatch (SQLITE_MISMATCH) \`db.prepare("SELECT * FROM foo LIMIT ?").raw().all(5)\` → OK. 2. **trajectory smoke — WAL not checkpointed before sql.js readback** Same fix pattern as smoke-graph-schema-migration.mjs in this PR. The trajectory hooks fire-and-forget insertGraphEdge through the better-sqlite3 writer; without closing the writer connection the data sits in .db-wal and sql.js (which reads only the main file) reports count=0. Call _resetBridgeDb() before sql.js readback. Local results after fixes: smoke-graph-query-dispatch.mjs → 21/21 smoke-graph-pathfinder.mjs → 20/20 smoke-trajectory-graph-edges.mjs → 10/10 Co-Authored-By: RuFlo <ruv@ruv.net>
This commit is contained in:
1 parent
2137822578
commit
c73c8d96e7
2 files changed
+27
-17
No files matched your search
@@ -54,8 +54,15 @@ async function sleep(ms) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function countEdgesByRelation(relation) {
|
async function countEdgesByRelation(relation) {
|
||||||
// Read directly from file without resetting the module cache
|
// Close the better-sqlite3 writer connection so its WAL is checkpointed
|
||||||
// (resetting during an in-flight write would break flushDb)
|
// back into the main DB file before we re-read via sql.js. #2431 put
|
||||||
|
// the writer in WAL mode and without this checkpoint sql.js sees only
|
||||||
|
// the pre-insert main file (the trajectory edges live in the .db-wal
|
||||||
|
// sidecar until the writer closes).
|
||||||
|
try {
|
||||||
|
const { _resetBridgeDb } = await distImport('memory/graph-edge-writer.js');
|
||||||
|
_resetBridgeDb();
|
||||||
|
} catch { /* writer never opened — no WAL to checkpoint */ }
|
||||||
try {
|
try {
|
||||||
const SQL = await loadSqlJs();
|
const SQL = await loadSqlJs();
|
||||||
if (!fs.existsSync(dbPath)) return 0;
|
if (!fs.existsSync(dbPath)) return 0;
|
||||||
|
|||||||
@@ -974,8 +974,11 @@ export const agentdbGraphQuery: MCPTool = {
|
|||||||
const db = await getBridgeDb();
|
const db = await getBridgeDb();
|
||||||
if (db) {
|
if (db) {
|
||||||
const cteSql = buildKHopCTE(nodeId, Math.min(depth, 3), relation, budget.maxNodesVisited);
|
const cteSql = buildKHopCTE(nodeId, Math.min(depth, 3), relation, budget.maxNodesVisited);
|
||||||
const result = db.exec(cteSql);
|
// graph-edge-writer returns a better-sqlite3 Database after #2431.
|
||||||
const rows = result?.[0]?.values ?? [];
|
// `db.exec(sql, params)` (sql.js style) is a runner with no result
|
||||||
|
// on better-sqlite3 — use `prepare(sql).raw().all(...)` to get the
|
||||||
|
// same array-of-arrays shape the downstream code expects.
|
||||||
|
const rows = db.prepare(cteSql).raw().all() as unknown[][];
|
||||||
return {
|
return {
|
||||||
success: true, mode, nodeId, depth,
|
success: true, mode, nodeId, depth,
|
||||||
results: rows.map((r: unknown[]) => ({ nodeId: r[0], depth: r[1] })),
|
results: rows.map((r: unknown[]) => ({ nodeId: r[0], depth: r[1] })),
|
||||||
@@ -1002,12 +1005,13 @@ export const agentdbGraphQuery: MCPTool = {
|
|||||||
const db = await getBridgeDb(undefined, { createIfMissing: true });
|
const db = await getBridgeDb(undefined, { createIfMissing: true });
|
||||||
if (!db) return { success: false, error: 'graph_edges DB unavailable (sql.js could not load)', hint: 'Check Node version + try `ruflo memory init` to initialize manually.', mode, nodeId };
|
if (!db) return { success: false, error: 'graph_edges DB unavailable (sql.js could not load)', hint: 'Check Node version + try `ruflo memory init` to initialize manually.', mode, nodeId };
|
||||||
|
|
||||||
// Load all rows with embedding_ref and score by cosine
|
// Load all rows with embedding_ref and score by cosine.
|
||||||
const rowResult = db.exec(
|
// better-sqlite3 API — `db.exec(sql, params)` (sql.js) silently
|
||||||
|
// throws "datatype mismatch" because exec ignores params, so `?`
|
||||||
|
// binds to nothing and SQLite rejects the LIMIT clause.
|
||||||
|
const rows = db.prepare(
|
||||||
`SELECT id, source_id, target_id, relation, weight, embedding_ref FROM graph_edges WHERE embedding_ref IS NOT NULL LIMIT ?`,
|
`SELECT id, source_id, target_id, relation, weight, embedding_ref FROM graph_edges WHERE embedding_ref IS NOT NULL LIMIT ?`,
|
||||||
[budget.maxNodesVisited],
|
).raw().all(budget.maxNodesVisited) as unknown[][];
|
||||||
);
|
|
||||||
const rows = rowResult?.[0]?.values ?? [];
|
|
||||||
const { decodeEmbedding } = await import('../memory/embedding-quantization.js');
|
const { decodeEmbedding } = await import('../memory/embedding-quantization.js');
|
||||||
|
|
||||||
const scored: Array<{ nodeId: string; score: number; relation: string }> = [];
|
const scored: Array<{ nodeId: string; score: number; relation: string }> = [];
|
||||||
@@ -1045,11 +1049,10 @@ export const agentdbGraphQuery: MCPTool = {
|
|||||||
const db = await getBridgeDb(undefined, { createIfMissing: true });
|
const db = await getBridgeDb(undefined, { createIfMissing: true });
|
||||||
if (!db) return { success: false, error: 'graph_edges DB unavailable (sql.js could not load)', hint: 'Check Node version + try `ruflo memory init` to initialize manually.', mode, nodeId };
|
if (!db) return { success: false, error: 'graph_edges DB unavailable (sql.js could not load)', hint: 'Check Node version + try `ruflo memory init` to initialize manually.', mode, nodeId };
|
||||||
|
|
||||||
const edgeResult = db.exec(
|
// better-sqlite3 API — see semantic-mode comment above.
|
||||||
|
const edges = db.prepare(
|
||||||
`SELECT source_id, target_id, weight FROM graph_edges LIMIT ?`,
|
`SELECT source_id, target_id, weight FROM graph_edges LIMIT ?`,
|
||||||
[budget.maxNodesVisited],
|
).raw().all(budget.maxNodesVisited) as unknown[][];
|
||||||
);
|
|
||||||
const edges = edgeResult?.[0]?.values ?? [];
|
|
||||||
if (edges.length === 0) {
|
if (edges.length === 0) {
|
||||||
return { success: true, mode, nodeId, results: [], count: 0, message: 'graph_edges is empty', elapsedMs: Date.now() - t0 };
|
return { success: true, mode, nodeId, results: [], count: 0, message: 'graph_edges is empty', elapsedMs: Date.now() - t0 };
|
||||||
}
|
}
|
||||||
@@ -1255,11 +1258,11 @@ export const agentdbGraphPathfinder: MCPTool = {
|
|||||||
? 'source_id, target_id, weight, last_reinforced, confidence'
|
? 'source_id, target_id, weight, last_reinforced, confidence'
|
||||||
: 'source_id, target_id, weight';
|
: 'source_id, target_id, weight';
|
||||||
|
|
||||||
const edgeResult = db.exec(
|
// better-sqlite3 API — see graph-query comment above; sql.js-style
|
||||||
|
// `db.exec(sql, params)` throws "datatype mismatch" here.
|
||||||
|
const rawEdges = db.prepare(
|
||||||
`SELECT ${colsSql} FROM graph_edges LIMIT ?`,
|
`SELECT ${colsSql} FROM graph_edges LIMIT ?`,
|
||||||
[budget.maxNodesVisited],
|
).raw().all(budget.maxNodesVisited) as unknown[][];
|
||||||
);
|
|
||||||
const rawEdges = edgeResult?.[0]?.values ?? [];
|
|
||||||
|
|
||||||
if (rawEdges.length === 0) {
|
if (rawEdges.length === 0) {
|
||||||
return { success: true, paths: [], count: 0, message: `no edges found from seedNodeId`, seedNodeId, algorithm, elapsedMs: Date.now() - t0 };
|
return { success: true, paths: [], count: 0, message: `no edges found from seedNodeId`, seedNodeId, algorithm, elapsedMs: Date.now() - t0 };
|
||||||
|
|||||||
Reference in new issue
Block a user