This commit ports the remaining Gen 1 proof-of-concept experiments from `forum/poc-g1` into `forum/poc-g2` while substituting naive mocks with real, production-ready mechanisms. Key advancements include: - `code_intelligence_poc.ts` and `cfg_poc.ts`: Swapped regex matching for actual Javascript AST traversal using `acorn`. - `static_analysis_poc.ts`: Replaced mock payloads with real `deno lint --json` output executed via `Deno.Command`. - `vector_db_poc.ts` and `multi_vec_poc.ts`: Replaced basic JS arrays with actual `jsr:@db/sqlite` instances utilizing User-Defined Functions (UDFs) to perform native vector cosine similarity queries in memory or on disk. - `protobuf_poc.ts`: Implemented robust protobuf serialization/deserialization via `protobufjs`. - Semantic/Governance PoCs (`constitution_poc.ts`, `ontology_poc.ts`, `state_machine_poc.ts`, `orphan_branch_poc.ts`, etc): Replaced string-mock I/O with absolute filesystem reads, real YAML parsing using `jsr:@std/yaml`, and isolated `Deno.Command` Git sandboxes. - Updated `forum/poc-g2/lab.ts` to orchestrate and execute all 19 experiments, proving 100% test pass rate with Gen 2 tooling. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
72 lines
2.9 KiB
TypeScript
72 lines
2.9 KiB
TypeScript
import { Database } from "jsr:@db/sqlite";
|
|
import {
|
|
assert,
|
|
assertEquals,
|
|
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
|
|
|
|
/**
|
|
* Proof of Concept: Multi-Vec Isolation (Gen 2)
|
|
*
|
|
* Demonstrates the concept of preventing semantic bleed by using physically
|
|
* isolated SQLite vector databases instead of dumping all embeddings into a
|
|
* single database. We use actual SQLite databases for this in Gen 2.
|
|
*/
|
|
|
|
// User-defined function for similarity
|
|
function cosineSimilarity(vecA: number[], vecB: number[]): number {
|
|
let dotProduct = 0, normA = 0, normB = 0;
|
|
for (let i = 0; i < vecA.length; i++) {
|
|
dotProduct += vecA[i] * vecB[i];
|
|
normA += vecA[i] ** 2;
|
|
normB += vecB[i] ** 2;
|
|
}
|
|
if (normA === 0 || normB === 0) return 0;
|
|
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
}
|
|
|
|
function initDb(name: string): Database {
|
|
// Use independent named files in /tmp so they aren't the same memory db
|
|
const db = new Database(`/tmp/${name}.db`);
|
|
db.function("vec_distance", (a: string, b: string) => cosineSimilarity(JSON.parse(a), JSON.parse(b)));
|
|
db.exec("CREATE TABLE IF NOT EXISTS embeddings (id TEXT, text TEXT, vector TEXT)");
|
|
db.exec("DELETE FROM embeddings"); // clear from previous runs
|
|
return db;
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
console.log("Running Multi-Vec Isolation PoC (Gen 2) tests...");
|
|
|
|
try {
|
|
const docsDb = initDb("docs_graph");
|
|
const telemetryDb = initDb("telemetry_graph");
|
|
|
|
const insertDocs = docsDb.prepare("INSERT INTO embeddings VALUES (?, ?, ?)");
|
|
insertDocs.run("docs-1", "High performance server scaling", JSON.stringify([0.9, 0.1, 0.2]));
|
|
insertDocs.finalize();
|
|
|
|
const insertTelemetry = telemetryDb.prepare("INSERT INTO embeddings VALUES (?, ?, ?)");
|
|
insertTelemetry.run("telemetry-1", "Memory leak in main process", JSON.stringify([0.1, 0.9, 0.2]));
|
|
insertTelemetry.finalize();
|
|
|
|
// The user asks about "Performance and scaling"
|
|
const queryVector = JSON.stringify([0.85, 0.15, 0.1]);
|
|
|
|
const docsResults = docsDb.prepare("SELECT id, vec_distance(vector, ?) as score FROM embeddings ORDER BY score DESC LIMIT 1").get(queryVector) as { id: string, score: number };
|
|
const telemetryResults = telemetryDb.prepare("SELECT id, vec_distance(vector, ?) as score FROM embeddings ORDER BY score DESC LIMIT 1").get(queryVector) as { id: string, score: number };
|
|
|
|
console.log("Docs graph match:", docsResults?.id, docsResults?.score);
|
|
console.log("Telemetry graph match:", telemetryResults?.id, telemetryResults?.score);
|
|
|
|
assert(docsResults.score > 0.9, "Should find a high match in docs");
|
|
assert(telemetryResults.score < docsResults.score, "Telemetry should be less relevant for this query");
|
|
|
|
docsDb.close();
|
|
telemetryDb.close();
|
|
|
|
console.log("✅ Multi-Vec Isolation PoC (Gen 2) successful: Isolated graphs prevented cross-contamination.");
|
|
} catch (err) {
|
|
console.error("❌ Multi-Vec Isolation PoC (Gen 2) failed:", err);
|
|
Deno.exit(1);
|
|
}
|
|
}
|