feat: indice locale SQLite/FTS5 + fallback testuale offline per qmem
Il gateway remoto non è sempre raggiungibile (VPN/nodi giù): finora le ricerche
fallivano e la conoscenza non era consultabile. Ora l'estensione mantiene un
indice locale testuale e vi degrada automaticamente.
Core (extensions/local-db.ts):
- schema SQLite con FTS5 (unicode61 remove_diacritics 2), trigger di sync,
tabella meta per cursori/stato; usa node:sqlite (Node >= 22.5, nessuna
dipendenza esterna), con soppressione del warning "experimental"
- import idempotente dalle sessioni pi (tutte le directory di progetto):
qmem_store/qmem_correct (ID + testo integrale), qmem_get (payload completo),
qmem_search (record osservati, anche creati da altri agenti)
- merge senza regressioni: le osservazioni povere (es. search senza
project_id) non azzerano i campi già noti; superseded_by monotono
- ricerca FTS5 con filtri (kind/project/scope/level/topic), esclusione di
superseduti e privati, ranking bm25, snippet, ripiego AND -> OR dichiarato
- enrich dal gateway (GET /v1/memories/{id}, pacing < rate limit, timeout 8s
per richiesta, stop al primo guasto) e pull da /v1/memories:export (endpoint
lato gateway previsto: se assente lo segnala senza errore)
- localGet per il recupero puntuale offline
Estensione:
- qmem_search: su 0/429/5xx degrada all'indice locale, risultati etichettati
"INDICE LOCALE, ricerca testuale non neurale" + details.fallback=local_sqlite
- qmem_get: fallback locale per UUID
- qmem_store: avviso esplicito che il record NON è salvato (nessuna coda)
- rendering arricchito con project_id e flag privato (anche per il gateway)
- comando /qmem:local status|import|find|enrich|pull
- regole e skill aggiornate: quando si usa l'indice locale non applicare le
soglie 0.45/0.60 (sono semantiche)
CLI standalone (stesso core): scripts/qmem-sqlite.mjs status|import|find|
enrich|pull (+ --json). Test: scripts/test-local.mjs (14 controlli, HOME
temporanea, sessioni sintetiche, gateway black-hole e stub HTTP).
Verifiche: 14/14 test superati; import reale 185 sessioni -> 1046 record unici
(1032 con testo, 986 attivi, 60 superseduti, 672 con project_id, 20 gruppi di
duplicati) in 2,8 MB; enrich con gateway giù si ferma in ~16s con messaggio
chiaro invece di restare appeso.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import { Type } from "typebox";
|
||||
import { gatewayRequest, loadConfig } from "../shared";
|
||||
import { gatewayRequest, loadConfig } from "../shared.ts";
|
||||
import { localDbPath, localSearch, type LocalSearchHit } from "../local-db.ts";
|
||||
|
||||
export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
pi.registerTool({
|
||||
@@ -86,6 +87,60 @@ export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
signal,
|
||||
);
|
||||
if (!ok) {
|
||||
// Gateway non raggiungibile → fallback sull'indice locale SQLite/FTS5
|
||||
const canFallback = cfg.localFallback !== false && (status === 0 || status >= 500 || status === 429);
|
||||
if (canFallback) {
|
||||
const dbFile = localDbPath(cfg);
|
||||
try {
|
||||
const hits = await localSearch(
|
||||
{
|
||||
query: String(p.query ?? ""),
|
||||
kind: p.kind,
|
||||
project_id: p.project_id,
|
||||
scope: p.scope,
|
||||
level: p.level,
|
||||
topic: p.topic,
|
||||
include_superseded: p.include_superseded ?? false,
|
||||
include_private: p.include_private ?? false,
|
||||
top_k: p.top_k ?? 5,
|
||||
},
|
||||
{ dbFile },
|
||||
);
|
||||
if (hits.length) {
|
||||
const lines = hits.map(
|
||||
(h: LocalSearchHit, i: number) =>
|
||||
`${i + 1}. [${h.kind ?? "?"}/${h.scope ?? "?"}${h.project_id ? ` project=${h.project_id}` : ""} locale${h.match_mode === "or" ? " match-parziale(OR)" : ""}${h.superseded_by ? " ⚠️ superseduto" : ""}] ${h.snippet}\n (id: ${h.memory_id}, creato: ${h.created_at ?? "?"}, agente: ${h.agent_id ?? "?"}, fonti: ${h.sources ?? "?"})`,
|
||||
);
|
||||
const header =
|
||||
`⚠️ Gateway non raggiungibile (HTTP ${status}): risultati dall'INDICE LOCALE (SQLite/FTS5).\n` +
|
||||
`Ricerca TESTUALE, non neurale: nessuno score semantico, nessuna soglia 0.45/0.60 — verifica i risultati prima dell'uso.\n` +
|
||||
`DB: ${dbFile}`;
|
||||
return {
|
||||
content: [{ type: "text", text: `${header}\n${lines.join("\n")}` }],
|
||||
details: {
|
||||
fallback: "local_sqlite",
|
||||
hits: hits.length,
|
||||
gateway_status: status,
|
||||
match_mode: hits[0]?.match_mode ?? "and",
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`Gateway non raggiungibile (HTTP ${status}) e nessun risultato nell'indice locale (${dbFile}).\n` +
|
||||
`Se il DB è assente o vecchio: /qmem:local import (ricostruisce l'indice dalle sessioni pi).`,
|
||||
},
|
||||
],
|
||||
details: { error: "gateway_error", status, fallback: "local_sqlite", hits: 0 },
|
||||
};
|
||||
} catch (e) {
|
||||
// nessun node:sqlite o DB illeggibile: si prosegue con l'errore del gateway
|
||||
void e;
|
||||
}
|
||||
}
|
||||
return {
|
||||
content: [{ type: "text", text: `Errore ${status}: ${JSON.stringify(data)}` }],
|
||||
details: { error: "gateway_error", status },
|
||||
@@ -109,7 +164,9 @@ export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
const top = r.topic ? ` (${r.topic})` : "";
|
||||
const parent = r.parent_id ? `, parent: ${r.parent_id}` : "";
|
||||
const links = r.links && r.links.length > 0 ? `, links: ${r.links.length}` : "";
|
||||
return `${i + 1}. [${r.kind}/${r.scope}${lvl}${top} score=${r.score}${r.score < 0.6 ? " ⚠️" : ""}${r.rerank_score != null ? ` rerank=${r.rerank_score}` : ""}${r.composite_score != null ? ` composite=${r.composite_score}` : ""}${r.confidence ? ` conf=${r.confidence}` : ""}] ${r.text}\n (id: ${r.memory_id}${parent}${links}, agente: ${r.agent_id ?? "?"}, creato: ${r.created_at ?? "?"}${r.importance != null && r.importance !== 0.5 ? `, importanza: ${r.importance}` : ""}${r.source ? `, fonte: ${r.source}` : ""}${r.supersedes_id ? `, supersede ${r.supersedes_id}` : ""}${r.superseded_by ? `, ⚠️ superseduto da ${r.superseded_by}` : ""})`;
|
||||
const proj = ` project=${r.project_id ?? "?"}`;
|
||||
const priv = r.private ? ", 🔒 privato" : "";
|
||||
return `${i + 1}. [${r.kind}/${r.scope}${lvl}${top}${proj} score=${r.score}${r.score < 0.6 ? " ⚠️" : ""}${r.rerank_score != null ? ` rerank=${r.rerank_score}` : ""}${r.composite_score != null ? ` composite=${r.composite_score}` : ""}${r.confidence ? ` conf=${r.confidence}` : ""}] ${r.text}\n (id: ${r.memory_id}${parent}${links}, agente: ${r.agent_id ?? "?"}, creato: ${r.created_at ?? "?"}${r.importance != null && r.importance !== 0.5 ? `, importanza: ${r.importance}` : ""}${r.source ? `, fonte: ${r.source}` : ""}${r.supersedes_id ? `, supersede ${r.supersedes_id}` : ""}${r.superseded_by ? `, ⚠️ superseduto da ${r.superseded_by}` : ""}${priv})`;
|
||||
},
|
||||
);
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user