auth-yes/forum/poc-g2/constitution_poc.ts
Tyler Gillispie e5855248e6
feat: Port all agent-forum-v4 Gen 1 PoCs to Gen 2 using real tooling (#68)
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>
2026-08-28 22:11:53 -07:00

78 lines
2.5 KiB
TypeScript

import { assert, assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
import { parse as yamlParse } from "jsr:@std/yaml";
/**
* Proof of Concept: The Constitution (Gen 2)
*
* Demonstrates a mechanism to parse an actual markdown file with YAML frontmatter
* and Markdown AST or structured regex to programmatically restrict an agent's
* proposed actions. Here we read from real filesystem I/O instead of a string mock.
*/
export async function parseConstitution(filePath: string) {
const content = await Deno.readTextFile(filePath);
const allowedSection = content.match(/## Allowed Tech Stack\n([\s\S]*?)(?=##|$)/);
const deniedSection = content.match(/## Denied Libraries\n([\s\S]*?)(?=##|$)/);
const allowed = allowedSection ? allowedSection[1].split("\n").map(l => l.replace(/^- /, "").trim()).filter(Boolean) : [];
const denied = deniedSection ? deniedSection[1].split("\n").map(l => l.replace(/^- /, "").trim()).filter(Boolean) : [];
return { allowed, denied };
}
function evaluateAgentProposal(
proposal: string[],
allowed: string[],
denied: string[],
): { valid: boolean; violations: string[] } {
const violations = [];
for (const tech of proposal) {
if (denied.includes(tech)) {
violations.push(`${tech} is explicitly forbidden.`);
} else if (!allowed.includes(tech)) {
violations.push(`${tech} is not in the approved tech stack.`);
}
}
return { valid: violations.length === 0, violations };
}
if (import.meta.main) {
console.log("Running The Constitution PoC (Gen 2) tests...");
try {
const tempFile = await Deno.makeTempFile({ suffix: ".md" });
await Deno.writeTextFile(tempFile, `
# System Constitution
## Allowed Tech Stack
- Deno
- TypeScript
- PostgreSQL
## Denied Libraries
- React
- MongoDB
`);
const { allowed, denied } = await parseConstitution(tempFile);
await Deno.remove(tempFile);
const safeProposal = ["Deno", "TypeScript"];
const safeResult = evaluateAgentProposal(safeProposal, allowed, denied);
assert(safeResult.valid, "Expected safe proposal to be valid");
const unsafeProposal = ["Deno", "React", "MongoDB"];
const unsafeResult = evaluateAgentProposal(unsafeProposal, allowed, denied);
assert(!unsafeResult.valid, "Expected unsafe proposal to be invalid");
assertEquals(unsafeResult.violations.length, 2);
console.log("✅ The Constitution PoC (Gen 2) successful: Real File I/O constraints parsed and enforced.");
} catch (err) {
console.error("❌ The Constitution PoC (Gen 2) failed:", err);
Deno.exit(1);
}
}