auth-yes/.forum/poc-g1/constitution_poc.ts

100 lines
2.7 KiB
TypeScript

import {
assert,
assertEquals,
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: The Constitution (AGENTS.md)
*
* Demonstrates a mechanism to parse an AGENTS.md rule file and use it to
* programmatically restrict an agent's proposed actions (e.g. stack validation).
*/
const mockAgentsMd = `
# System Constitution
## Allowed Tech Stack
- Deno
- TypeScript
- Vue.js
- Tailwind CSS
- PostgreSQL
## Denied Libraries
- React
- Express
- MongoDB
`;
function extractAllowedStack(markdown: string): string[] {
const allowedSection = markdown.match(
/## Allowed Tech Stack\n([\s\S]*?)(?=##|$)/,
);
if (!allowedSection) return [];
return allowedSection[1]
.split("\n")
.map((line) => line.replace(/^- /, "").trim())
.filter((line) => line.length > 0);
}
function extractDeniedStack(markdown: string): string[] {
const deniedSection = markdown.match(
/## Denied Libraries\n([\s\S]*?)(?=##|$)/,
);
if (!deniedSection) return [];
return deniedSection[1]
.split("\n")
.map((line) => line.replace(/^- /, "").trim())
.filter((line) => line.length > 0);
}
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 tests...");
try {
const allowed = extractAllowedStack(mockAgentsMd);
const denied = extractDeniedStack(mockAgentsMd);
// Scenario 1: Agent proposes an allowed stack update
const safeProposal = ["Deno", "TypeScript"];
const safeResult = evaluateAgentProposal(safeProposal, allowed, denied);
assert(safeResult.valid, "Expected safe proposal to be valid");
console.log("✅ Safe proposal accepted.");
// Scenario 2: Agent hallucinates a React/Mongo app
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(
"✅ Unsafe proposal correctly rejected based on AGENTS.md rules:",
);
unsafeResult.violations.forEach((v) => console.log(` - ${v}`));
console.log("✅ The Constitution PoC successful.");
} catch (err) {
console.error("❌ The Constitution PoC failed:", err);
Deno.exit(1);
}
}