auth-yes/.forum/poc-g2/git_storage_poc.ts

104 lines
3.0 KiB
TypeScript

import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Generation 2 Proof of Concept: Git Storage (Notes)
*
* This module demonstrates interacting with Git Notes in a completely
* isolated, real Git environment using Deno.Command. It sets up a local
* Git repository on the fly to avoid polluting the main project repo,
* thereby proving production-readiness of the local-first storage mechanism.
*/
async function runGitCmd(args: string[], cwd?: string): Promise<string> {
const cmd = new Deno.Command("git", {
args,
cwd,
stdout: "piped",
stderr: "piped",
});
const output = await cmd.output();
const stdout = new TextDecoder().decode(output.stdout).trim();
const stderr = new TextDecoder().decode(output.stderr).trim();
if (!output.success) {
throw new Error(`Git command failed: git ${args.join(" ")}\n${stderr}`);
}
return stdout;
}
export async function addGitNote(
cwd: string,
ref: string,
message: string,
targetRef: string = "HEAD",
) {
await runGitCmd([
"notes",
"--ref",
ref,
"add",
"-f",
"-m",
message,
targetRef,
], cwd);
}
export async function readGitNote(
cwd: string,
ref: string,
targetRef: string = "HEAD",
): Promise<string> {
try {
return await runGitCmd(["notes", "--ref", ref, "show", targetRef], cwd);
} catch (error: any) {
if (error.message.includes("No note found") || error.message.includes("does not exist")) {
return "";
}
throw error;
}
}
if (import.meta.main) {
console.log("Running Gen 2 Git Storage PoC tests...");
// Setup an isolated Git repository
const tempDir = await Deno.makeTempDir({ prefix: "agent-forum-git-poc-" });
console.log(`Created isolated Git repo at ${tempDir}`);
try {
await runGitCmd(["init"], tempDir);
await runGitCmd(["config", "user.email", "poc@example.com"], tempDir);
await runGitCmd(["config", "user.name", "PoC Tester"], tempDir);
// Create an initial commit so we have a HEAD
const dummyFile = `${tempDir}/README.md`;
await Deno.writeTextFile(dummyFile, "# Dummy");
await runGitCmd(["add", "README.md"], tempDir);
await runGitCmd(["commit", "-m", "Initial commit"], tempDir);
const customRef = "forum/test-reasoning";
const testMessage = JSON.stringify({
agent: "poc-g2-agent",
risk: "low",
thought: "This is a hidden thought stored in an isolated git note.",
});
console.log(`Adding note to current HEAD under ref ${customRef}...`);
await addGitNote(tempDir, customRef, testMessage);
console.log(`Reading note back...`);
const readMessage = await readGitNote(tempDir, customRef);
assertEquals(readMessage, testMessage);
console.log("✅ Gen 2 Git Storage PoC successful: Read/Write worked securely in an isolated Git environment.");
} catch (err) {
console.error("❌ Gen 2 Git Storage PoC failed:", err);
Deno.exit(1);
} finally {
console.log(`Cleaning up isolated Git repo...`);
await Deno.remove(tempDir, { recursive: true });
}
}