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); } }