auth-yes/forum/poc-g1/state_machine_poc.ts
Tyler Gillispie 8f61cbdc49
feat(forum): add Generation 2 agent forum PoCs with production tools (#67)
- Renamed `forum/experiments` to `forum/poc-g1` to designate generation 1.
- Created `forum/poc-g2` and a new `lab.ts` runner.
- Non-destructively migrated `dag_engine_poc.ts`, `git_storage_poc.ts`, `merkle_diff_poc.ts`, and `frontmatter_poc.ts` to `poc-g2`.
- Upgraded migrated PoCs to utilize actual production-ready tools (e.g. `std/yaml` parsing and isolated `Deno.Command` Git repos) per blueprint constraints.

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 21:10:21 -07:00

112 lines
3.3 KiB
TypeScript

import { assert } from "https://deno.land/std@0.224.0/assert/mod.ts";
// Define the structure of the transitions.json configuration
interface TransitionRule {
requires: string[];
}
type TransitionsConfig = Record<string, TransitionRule>;
// Mock transitions.json content (in a real scenario, this is read from .forum/transitions.json)
const MOCK_TRANSITIONS_JSON = `
{
"Coder": {
"requires": ["Gatekeeper_Approval"]
},
"Gatekeeper": {
"requires": []
},
"Evaluator": {
"requires": ["Coder_Completion"]
}
}
`;
// Simulated state of the repository/pipeline
const currentSystemState = {
activeFlags: new Set<string>(), // e.g., 'Gatekeeper_Approval', 'Coder_Completion'
};
/**
* The Evaluator script that mathematically enforces pipeline progression.
* It checks the transitions matrix to see if a specific Agent Role is allowed to execute based on the current flags.
*/
function canAgentExecute(
roleName: string,
config: TransitionsConfig,
currentState: Set<string>,
): boolean {
const rule = config[roleName];
if (!rule) {
throw new Error(
`Role ${roleName} is not defined in the transitions matrix. Execution denied.`,
);
}
// Bounded Model Checking: All required flags must be present in the current state.
return rule.requires.every((req) => currentState.has(req));
}
async function runStateMachinePoC() {
console.log("--- Agent Forum: State Machine & Governance PoC ---");
// 1. Parse the machine-readable matrix
const matrix: TransitionsConfig = JSON.parse(MOCK_TRANSITIONS_JSON);
console.log("Loaded Transitions Matrix:", Object.keys(matrix));
// 2. Attempt to run Coder BEFORE Gatekeeper has approved
console.log(
"\nScenario 1: Attempting to run 'Coder' with empty system state...",
);
const canCoderRunInitial = canAgentExecute(
"Coder",
matrix,
currentSystemState.activeFlags,
);
console.log(`Result: Coder execution allowed? ${canCoderRunInitial}`);
assert(
canCoderRunInitial === false,
"Coder should NOT be able to run without Gatekeeper_Approval",
);
// 3. Gatekeeper runs (it has no requirements)
console.log("\nScenario 2: Running 'Gatekeeper'...");
const canGatekeeperRun = canAgentExecute(
"Gatekeeper",
matrix,
currentSystemState.activeFlags,
);
console.log(`Result: Gatekeeper execution allowed? ${canGatekeeperRun}`);
assert(canGatekeeperRun === true, "Gatekeeper should be able to run");
// Simulate Gatekeeper finishing its job and setting the flag in the meta-state
console.log("Gatekeeper finished. Setting 'Gatekeeper_Approval' flag...");
currentSystemState.activeFlags.add("Gatekeeper_Approval");
// 4. Attempt to run Coder AFTER Gatekeeper has approved
console.log(
"\nScenario 3: Attempting to run 'Coder' with updated system state...",
);
const canCoderRunNow = canAgentExecute(
"Coder",
matrix,
currentSystemState.activeFlags,
);
console.log(`Result: Coder execution allowed? ${canCoderRunNow}`);
assert(
canCoderRunNow === true,
"Coder SHOULD be able to run now that Gatekeeper_Approval is present",
);
console.log(
"\nPoC Successful: Bounded Model Checking mathematically prevents out-of-order execution.",
);
}
if (import.meta.main) {
runStateMachinePoC().catch((err) => {
console.error("PoC Failed:", err);
Deno.exit(1);
});
}