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

88 lines
2.3 KiB
TypeScript

import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Git Storage (Notes & Orphan Branches)
*
* This module demonstrates how we can use Deno's `Deno.Command` API to interact
* with Git Notes and Orphan Branches to store agent state and metadata without
* polluting the main working tree.
*/
async function runGitCmd(args: string[]): Promise<string> {
const cmd = new Deno.Command("git", {
args,
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(
ref: string,
message: string,
targetRef: string = "HEAD",
) {
// First, check if a note already exists to avoid overwriting blindly
// For this PoC, we will append or overwrite
await runGitCmd([
"notes",
"--ref",
ref,
"add",
"-f",
"-m",
message,
targetRef,
]);
}
export async function readGitNote(
ref: string,
targetRef: string = "HEAD",
): Promise<string> {
try {
return await runGitCmd(["notes", "--ref", ref, "show", targetRef]);
} catch (error: any) {
if (error.message.includes("No note found")) {
return "";
}
throw error;
}
}
// In a real scenario, tests would be separated. For this PoC, we will run the tests here.
if (import.meta.main) {
console.log("Running Git Storage PoC tests...");
// 1. Test Git Notes
const customRef = "forum/test-reasoning";
const testMessage = JSON.stringify({
agent: "poc-agent",
risk: "low",
thought: "This is a hidden thought stored in a git note.",
});
try {
console.log(`Adding note to current HEAD under ref ${customRef}...`);
await addGitNote(customRef, testMessage);
console.log(`Reading note back...`);
const readMessage = await readGitNote(customRef);
assertEquals(readMessage, testMessage);
console.log("✅ Git Notes PoC successful: Read/Write worked as expected.");
// Clean up
await runGitCmd(["notes", "--ref", customRef, "remove", "HEAD"]);
} catch (err) {
console.error("❌ Git Notes PoC failed:", err);
}
}