Files
pi-qmem/scripts/test-local.mjs
T
Matteo Benedetto 322b4cf446 feat(outbox): store offline con coda locale e sincronizzazione al ritorno della rete
Prima qmem_store falliva se il gateway non era raggiungibile: la conoscenza
andava persa. Ora il record entra in una coda locale persistente e viene
inviato automaticamente quando la connessione torna.

Core (extensions/local-db.ts):
- tabella `pending` (local_id, payload JSON, attempts, last_error, status,
  remote_id) + colonna `records.pending` (migrazione automatica dei DB esistenti)
- queueStore(): accoda e crea subito il placeholder locale ricercabile ()
- flushQueue(): POST /v1/memories con Idempotency-Key = local_id (retry senza
  duplicati), FIFO, pacing sotto il rate limit, timeout 12s per richiesta
- esiti: synced (il record locale adotta l'ID remoto, niente duplicati) ·
  duplicate (409: registra l'ID del match e NON sovrascrive il testo locale
  autorevole) · failed (4xx di validazione, non ritentato) · 0/429/5xx: resta in
  coda e il flush si ferma
- supersede offline: supersedes_id che punta a un local_id viene rimappato al
  remote_id al flush (se il genitore non è sincronizzato → failed esplicito)
- submitOrQueue(): online → gateway + indicizzazione locale; offline → coda
- maybeBackgroundFlush() (single-flight) e flushQueueIfPending() per session_start
- stato/report: queued/synced/duplicate/failed, più vecchio, ultimo errore,
  last_flush, record pendenti in indice

Estensione:
- qmem_store: gateway giù → accoda e risponde con id locale, dimensione coda e
  spiegazione (details.queued/local_id/queue_size)
- fallback offline di session_start: flush in background (non blocca l'avvio)
- /qmem:local queue|flush; status con la coda; marker " in coda" nei risultati
  locali di qmem_search/qmem_get
- regole e skill: un record in coda NON è ancora nella memoria condivisa

CLI: store [--queue-only], queue, flush (+ status con la coda).
Test: scripts/test-local.mjs ora copre anche outbox → 24 controlli (flush con
2 sync + 1 duplicato 409 + 1 fallito 422, Idempotency-Key, rimappatura del
supersede, ricerca del record con l'ID remoto dopo il sync).

Verifiche: 24/24 test superati; demo reale su DB temporaneo: store accodato,
queue con local_id, flush con gateway giù → "fermato: HTTP 0" e voce che resta
in coda con l'errore registrato.
2026-09-13 17:29:09 +02:00

256 lines
17 KiB
JavaScript

#!/usr/bin/env node
/**
* Test dell'indice locale qmem (SQLite/FTS5) e del fallback offline.
*
* Isola tutto in una HOME temporanea:
* - sessioni pi sintetiche (store / correct / search / get)
* - config con gateway "black hole" (127.0.0.1:9 → connessione rifiutata)
* - stub HTTP locale per testare enrich e pull
*
* Uso: node scripts/test-local.mjs (esce != 0 se un controllo fallisce)
*/
import { spawn, spawnSync } from "node:child_process";
import * as crypto from "node:crypto";
import { createServer } from "node:http";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const REPO = path.dirname(HERE);
const PI_BASE =
process.env.PI_BASE ??
"/home/enne2/.local/share/pi-node/node-v22.23.2-linux-x64/lib/node_modules/@earendil-works/pi-coding-agent";
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "qmem-test-"));
const HOME = path.join(TMP, "home");
const SESS_DIR = path.join(HOME, ".pi", "agent", "sessions", "--tmp--");
const DB = path.join(HOME, "qmem.sqlite");
const CONFIG = path.join(HOME, ".config", "pi-qmem", "config.json");
const ID_A = "11111111-1111-4111-8111-111111111111"; // store, attivo
const ID_B = "22222222-2222-4222-8222-222222222222"; // store poi superseduto
const ID_C = "33333333-3333-4333-8333-333333333333"; // correzione (nuovo)
const ID_D = "44444444-4444-4444-8444-444444444444"; // visto in qmem_search (senza project_id)
const ID_E = "55555555-5555-4555-8555-555555555555"; // visto in qmem_get
fs.mkdirSync(SESS_DIR, { recursive: true });
fs.mkdirSync(path.dirname(CONFIG), { recursive: true });
// ---------------------------------------------------------------- sessioni sintetiche
const entry = (obj) => JSON.stringify(obj);
const call = (id, name, args, ts) => entry({ type: "message", id, timestamp: ts, message: { role: "assistant", content: [{ type: "toolCall", id: `c-${id}`, name, arguments: args }] } });
const result = (id, name, text, details, ts) => entry({ type: "message", id, timestamp: ts, message: { role: "toolResult", toolCallId: `c-${id}`, toolName: name, content: [{ type: "text", text }], details } });
const T = "2026-09-01T10:00:0";
const lines = [
entry({ type: "session", version: 3, id: "synthetic", timestamp: `${T}0.000Z`, cwd: "/tmp" }),
// 1) store attivo
call("m1", "qmem_store", { text: "Il fallback locale usa SQLite FTS5 con tokenizer unicode61 per la ricerca testuale offline.", kind: "decision", project_id: "test-project", scope: "agent", agent_id: "tester" }, `${T}1.000Z`),
result("m1", "qmem_store", `Memoria salvata: ${ID_A} (decision, scope agent)`, { memory_id: ID_A, created_at: `${T}1.000Z` }, `${T}1.100Z`),
// 2) store che verrà corretto
call("m2", "qmem_store", { text: "Il gateway remoto è sempre raggiungibile via VPN.", kind: "fact", project_id: "test-project", scope: "agent" }, `${T}2.000Z`),
result("m2", "qmem_store", `Memoria salvata: ${ID_B} (fact, scope agent)`, { memory_id: ID_B, created_at: `${T}2.000Z` }, `${T}2.100Z`),
// 3) correzione: il gateway remoto NON è sempre raggiungibile
call("m3", "qmem_correct", { memory_id: ID_B, text: "Il gateway remoto NON è sempre raggiungibile: serve un fallback locale per la ricerca.", reason: "verificato outage" }, `${T}3.000Z`),
result("m3", "qmem_correct", `Correzione applicata: nuovo record ${ID_C} supersede ${ID_B}.`, { new_id: ID_C, superseded_id: ID_B, reparented: 0 }, `${T}3.100Z`),
// 4) risultato di ricerca (record creato altrove, senza project_id nel rendering)
call("m4", "qmem_search", { query: "backup qdrant snapshot" }, `${T}4.000Z`),
result("m4", "qmem_search", `1. [fact/org score=0.71] Backup giornaliero: snapshot Qdrant + rsync in /home/enne2/archive/backups\n (id: ${ID_D}, agente: pi, creato: 2026-08-16T06:00:00Z)`, { hits: 1 }, `${T}4.100Z`),
// 5) qmem_get
call("m5", "qmem_get", { memory_id: ID_E }, `${T}5.000Z`),
result("m5", "qmem_get", `memory_id: ${ID_E}\n[episode/agent conf=medium] | project: infra-security | agente: shared, creato: 2026-08-14T09:00:00Z\n\nRollback del firewall: ripristinare la regola precedente e verificare con nmap.`, { memory_id: ID_E, kind: "episode", scope: "agent", project_id: "infra-security" }, `${T}5.100Z`),
];
fs.writeFileSync(path.join(SESS_DIR, "2026-09-01T10-00-00-000Z_test.jsonl"), lines.join("\n") + "\n");
// ---------------------------------------------------------------- config black-hole
fs.writeFileSync(CONFIG, JSON.stringify({ url: "http://127.0.0.1:9", apiKey: "test-key", timeoutMs: 1500, localDbPath: DB }, null, 2), { mode: 0o600 });
const env = { ...process.env, HOME, QMEM_SQLITE: DB, QMEM_SESSIONS_DIR: path.join(HOME, ".pi", "agent", "sessions") };
// isolamento anche per il processo di test: l'estensione caricata in-process
// deve leggere la config/DB di test (non quelli reali dell'utente)
process.env.HOME = HOME;
process.env.QMEM_SQLITE = DB;
process.env.QMEM_SESSIONS_DIR = path.join(HOME, ".pi", "agent", "sessions");
const results = [];
const check = (name, ok, info = "") => {
results.push({ name, ok, info });
console.log(`${ok ? "✅" : "❌"} ${name}${info ? ` — ${info}` : ""}`);
};
// ---------------------------------------------------------------- 1) CLI import
const runCli = (args) => spawnSync("node", [path.join(HERE, "qmem-sqlite.mjs"), ...args], { env, encoding: "utf8" });
// async: spawnSync bloccherebbe l'event loop e lo stub in-process non risponderebbe
const runCliAsync = (args) =>
new Promise((resolve) => {
const child = spawn("node", [path.join(HERE, "qmem-sqlite.mjs"), ...args], { env });
let out = "";
let err = "";
child.stdout.on("data", (d) => (out += d));
child.stderr.on("data", (d) => (err += d));
child.on("close", (code) => {
if (process.env.DEBUG_CLI && (!out.trim() || code !== 0)) {
console.log(` [cli ${args.join(" ")}] code=${code} stdout=${out.trim().slice(0, 200)} stderr=${err.trim().slice(0, 200)}`);
}
resolve(out);
});
});
const imp = runCli(["import", "--json"]);
let impJson = null;
try {
impJson = JSON.parse(imp.stdout);
} catch {
/* fallback su output testuale */
}
check("CLI import dai sessioni sintetiche", imp.status === 0 && impJson?.stats?.records >= 5, `record=${impJson?.stats?.records} store=${impJson?.stats?.store} correct=${impJson?.stats?.correct} get=${impJson?.stats?.get} search=${impJson?.stats?.searchHits}`);
// ---------------------------------------------------------------- 2) CLI find
const findOut = runCli(["find", "sqlite", "--json"]);
const hits = JSON.parse(findOut.stdout || "[]");
check("ricerca lessicale trova il record attivo", hits.some((h) => h.memory_id === ID_A), `${hits.length} hit`);
const findAll = JSON.parse(runCli(["find", "raggiungibile", "--all", "--json"]).stdout || "[]");
check("--all include i superseduti", findAll.some((h) => h.memory_id === ID_B || h.memory_id === ID_C), `${findAll.length} hit`);
const findActive = JSON.parse(runCli(["find", "raggiungibile", "--json"]).stdout || "[]");
check("default esclude i superseduti", !findActive.some((h) => h.memory_id === ID_B), `${findActive.length} hit`);
const projectFilter = JSON.parse(runCli(["find", "sqlite", "--project", "test-project", "--json"]).stdout || "[]");
check("filtro --project funziona", projectFilter.length === 1 && projectFilter[0].memory_id === ID_A);
const orMode = JSON.parse(runCli(["find", "firewall sqlite", "--json"]).stdout || "[]"); // termini non co-occorrenti → AND=0 → OR
check("ripiego OR su match parziale", orMode.length > 0 && orMode[0].match_mode === "or", `${orMode.length} hit`);
// ---------------------------------------------------------------- 3) estensione: fallback offline
const { createJiti } = await import(`${PI_BASE}/node_modules/jiti/lib/jiti.mjs`);
const jiti = createJiti(import.meta.url, {
interopDefault: true,
alias: {
"@earendil-works/pi-coding-agent": PI_BASE,
"@earendil-works/pi-tui": path.join(PI_BASE, "node_modules/@earendil-works/pi-tui"),
"@earendil-works/pi-ai": path.join(PI_BASE, "node_modules/@earendil-works/pi-ai"),
"@earendil-works/pi-agent-core": path.join(PI_BASE, "node_modules/@earendil-works/pi-agent-core"),
typebox: path.join(PI_BASE, "node_modules/typebox/build/index.mjs"),
},
});
const mod = await jiti.import(path.join(REPO, "extensions/index.ts"));
const tools = new Map();
const commands = new Map();
(mod.default ?? mod)({
on() {},
registerTool: (t) => tools.set(t.name, t),
registerCommand: (n, d) => commands.set(n, d),
registerShortcut() {},
registerFlag() {},
appendEntry() {},
});
check("estensione caricata (6 tool + comandi)", tools.size >= 6 && commands.has("qmem:local"), `tool=${[...tools.keys()].join(",")} cmd=${[...commands.keys()].join(",")}`);
const notices = [];
const ctx = {
mode: "print",
hasUI: false,
cwd: REPO,
ui: { notify: (m) => notices.push(m), setStatus() {}, select: async () => null, input: async () => null, confirm: async () => false, custom: () => ({}) },
sessionManager: { getSessionId: () => "test", getEntries: () => [], getSessionFile: () => undefined },
};
const search = await tools.get("qmem_search").execute("t1", { query: "sqlite fts5" }, undefined, undefined, ctx);
check("qmem_search → fallback locale con gateway giù", search.details?.fallback === "local_sqlite" && search.details?.hits > 0, `hits=${search.details?.hits} status=${search.details?.gateway_status}`);
check("risultato locale etichettato come testuale", /INDICE LOCALE/i.test(search.content[0].text) && /non neurale/i.test(search.content[0].text) && /sqlite/i.test(search.content[0].text));
const getRes = await tools.get("qmem_get").execute("t2", { memory_id: ID_E }, undefined, undefined, ctx);
if (process.env.DEBUG_GET) console.log("GET RESULT:", JSON.stringify(getRes, null, 1).slice(0, 900));
check("qmem_get → fallback locale per UUID", getRes.details?.fallback === "local_sqlite" && /nmap/.test(getRes.content[0].text));
const storeRes = await tools.get("qmem_store").execute("t3", { text: "Record creato offline durante un outage del gateway.", project_id: "test-project", kind: "fact" }, undefined, undefined, ctx);
const queuedLocalId = storeRes.details?.local_id;
check("qmem_store offline → accoda nell'outbox", storeRes.details?.queued === true && /ACCODATO/i.test(storeRes.content[0].text), `local_id=${String(queuedLocalId).slice(0, 8)} coda=${storeRes.details?.queue_size}`);
// ---------------------------------------------------------------- 4) arricchimento dal gateway (stub)
const posted = [];
const server = createServer((req, res) => {
if (req.method === "POST" && req.url === "/v1/memories") {
let raw = "";
req.on("data", (c) => (raw += c));
req.on("end", () => {
let body = {};
try { body = JSON.parse(raw); } catch { /* ignore */ }
posted.push({ body, idem: req.headers["idempotency-key"] });
if (/duplicato/i.test(body.text ?? "")) {
res.writeHead(409, { "content-type": "application/json" }).end(JSON.stringify({ detail: { error: "duplicate_memory", reason: "KNOWN_SOLUTION", matches: [{ memory_id: ID_A, score: 0.93 }] } }));
} else if (/invalido/i.test(body.text ?? "")) {
res.writeHead(422, { "content-type": "application/json" }).end(JSON.stringify({ detail: [{ loc: ["body", "project_id"], msg: "campo obbligatorio" }] }));
} else {
res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ memory_id: crypto.randomUUID(), text: body.text, kind: body.kind, project_id: body.project_id, scope: body.scope, created_at: new Date().toISOString(), supersedes_id: body.supersedes_id }));
}
});
return;
}
if (req.url?.startsWith("/v1/memories:export")) {
res.writeHead(404, { "content-type": "application/json" }).end(JSON.stringify({ detail: "non trovato" }));
return;
}
if (req.url === `/v1/memories/${ID_D}`) {
res.writeHead(200, { "content-type": "application/json" }).end(
JSON.stringify({ memory_id: ID_D, text: "Backup giornaliero: snapshot Qdrant + rsync in /home/enne2/archive/backups (verificato)", kind: "fact", scope: "org", project_id: "infra-security", agent_id: "pi", created_at: "2026-08-16T06:00:00Z", private: true }),
);
return;
}
res.writeHead(404, { "content-type": "application/json" }).end(JSON.stringify({ detail: "Memoria non trovata" }));
});
await new Promise((r) => server.listen(0, "127.0.0.1", r));
const stub = `http://127.0.0.1:${server.address().port}`;
fs.writeFileSync(CONFIG, JSON.stringify({ url: stub, apiKey: "test-key", timeoutMs: 3000, localDbPath: DB }, null, 2), { mode: 0o600 });
const enrichOut = await runCliAsync(["enrich", "--json"]);
let enrich = { stdout: enrichOut };
let enrichJson = null;
try {
enrichJson = JSON.parse(enrich.stdout);
} catch {
/* ignore */
}
check("enrich dal gateway aggiorna i campi mancanti", (enrichJson?.updated ?? 0) >= 1, `requested=${enrichJson?.requested} updated=${enrichJson?.updated}`);
const after = runCli(["status", "--json"]);
const afterJson = JSON.parse(after.stdout || "{}");
check("project_id/private arricchiti nel DB", afterJson.withProject > 0, `con project_id: ${afterJson.withProject}/${afterJson.total}, privati: ${afterJson.private}`);
const pullOut = await runCliAsync(["pull", "--json"]);
const pull = { stdout: pullOut };
const pullJson = JSON.parse(pull.stdout || "{}");
check("pull segnala endpoint export assente", pullJson.supported === false, pullJson.message ?? "");
// ---------------------------------------------------------------- 5) outbox: flush, 409, 422, supersede
const q1 = JSON.parse((await runCliAsync(["queue", "--json"])) || "{}");
check("coda: 1 record in attesa dopo lo store offline", q1.stats?.queued >= 1, `queued=${q1.stats?.queued} localId=${String(queuedLocalId).slice(0, 8)}`);
const findPending = JSON.parse(runCli(["find", "outage gateway", "--json"]).stdout || "[]");
check("il record accodato è già ricercabile offline (pending=1)", findPending.some((h) => h.pending === 1 && h.memory_id === queuedLocalId), `${findPending.length} hit`);
// seconda voce: duplicato (409) e terza: invalida (422), più un supersede verso un local_id
const dupStore = JSON.parse((await runCliAsync(["store", "--project", "test-project", "--text", "Questo è un duplicato noto del gateway", "--queue-only", "--json"])) || "{}");
const badStore = JSON.parse((await runCliAsync(["store", "--project", "test-project", "--text", "Record invalido per test 422", "--queue-only", "--json"])) || "{}");
const childStore = JSON.parse((await runCliAsync(["store", "--project", "test-project", "--text", "Correzione offline di un record locale", "--queue-only", "--json"])) || "{}");
// supersede verso il record locale: il flush deve rimappare local_id → remote_id
const dbMod = spawnSync("node", ["-e", `
const { DatabaseSync } = require("node:sqlite");
const db = new DatabaseSync(process.env.QMEM_SQLITE);
const row = db.prepare("SELECT payload FROM pending WHERE local_id = ?").get(${JSON.stringify(childStore.local_id)});
const p = JSON.parse(row.payload); p.supersedes_id = ${JSON.stringify(queuedLocalId)};
db.prepare("UPDATE pending SET payload = ? WHERE local_id = ?").run(JSON.stringify(p), ${JSON.stringify(childStore.local_id)});
`], { env, encoding: "utf8" });
check("setup supersede offline (payload con supersedes_id locale)", dbMod.status === 0, dbMod.stderr?.slice(0, 80) ?? "");
const flush1 = JSON.parse((await runCliAsync(["flush", "--json"])) || "{}");
check("flush: 2 sincronizzati, 1 duplicato (409), 1 fallito (422), coda vuota", flush1.synced === 2 && flush1.duplicates === 1 && flush1.failed === 1 && flush1.remaining === 0, `processed=${flush1.processed} synced=${flush1.synced} duplicates=${flush1.duplicates} failed=${flush1.failed} remaining=${flush1.remaining}`);
const remoteOfQueued = flush1.remoteIds?.[queuedLocalId];
check("Idempotency-Key = local_id inviato al gateway", posted.every((p) => typeof p.idem === "string" && p.idem.length === 36), `${posted.length} POST`);
check("supersede rimappato da local_id a remote_id", posted.some((p) => p.body.supersedes_id === remoteOfQueued && remoteOfQueued), `remote=${String(remoteOfQueued).slice(0, 8)}`);
const afterFlush = JSON.parse((await runCliAsync(["status", "--json"])) || "{}");
check("dopo il flush resta pendente solo il record fallito (422)", afterFlush.queued === 0 && afterFlush.pendingInIndex === 1 && afterFlush.failedQueue === 1, `queued=${afterFlush.queued} pending_in_index=${afterFlush.pendingInIndex} failed=${afterFlush.failedQueue}`);
const findRemote = JSON.parse(runCli(["find", "outage gateway", "--json"]).stdout || "[]");
check("il record è ricercabile con l'ID remoto", findRemote.some((h) => h.memory_id === remoteOfQueued && h.pending === 0), `${findRemote.length} hit`);
// duplicato (409) e invalido (422)
const dupFlush = JSON.parse((await runCliAsync(["flush", "--json"])) || "{}");
const qFinal = JSON.parse((await runCliAsync(["queue", "--json"])) || "{}");
const dupItem = (qFinal.items ?? []).find((i) => i.local_id === dupStore.local_id);
const badItem = (qFinal.items ?? []).find((i) => i.local_id === badStore.local_id);
check("duplicato marcato con remote_id del match", dupItem?.status === "duplicate" && dupItem?.remote_id === ID_A, `status=${dupItem?.status} remote=${String(dupItem?.remote_id).slice(0, 8)}`);
check("422 marcato failed con errore conservato", badItem?.status === "failed" && /422/.test(badItem?.last_error ?? ""), `err=${String(badItem?.last_error).slice(0, 40)}`);
// ---------------------------------------------------------------- report
const failed = results.filter((r) => !r.ok);
console.log(`\n${results.length - failed.length}/${results.length} controlli superati — HOME di test: ${HOME}`);
process.exit(failed.length ? 1 : 0);