import { assertEquals, assertNotEquals, } from "https://deno.land/std@0.224.0/testing/asserts.ts"; /** * Generation 2 Proof of Concept: Git Merkle DAG Diffing * * This module demonstrates using Git's Merkle Tree structure to perform * O(1) diffing (`git diff-tree` and `git ls-tree`). To prove production readiness * and avoid failures from shallow clones or dirty states, we instantiate * a clean, isolated Git repository with a deterministic commit history. */ async function runGitCmd(args: string[], cwd?: string): Promise { 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; } if (import.meta.main) { console.log("Running Gen 2 Git Merkle DAG Diffing PoC tests..."); const tempDir = await Deno.makeTempDir({ prefix: "agent-forum-merkle-poc-" }); console.log(`Created isolated Git repo at ${tempDir}`); try { // 0. Setup an isolated Git repo with two commits await runGitCmd(["init"], tempDir); await runGitCmd(["config", "user.email", "poc@example.com"], tempDir); await runGitCmd(["config", "user.name", "PoC Tester"], tempDir); const fileA = `${tempDir}/fileA.txt`; const fileB = `${tempDir}/fileB.txt`; // Commit 1 await Deno.writeTextFile(fileA, "Hello World A"); await Deno.writeTextFile(fileB, "Hello World B"); await runGitCmd(["add", "."], tempDir); await runGitCmd(["commit", "-m", "Commit 1"], tempDir); // Commit 2: Modify fileA only await Deno.writeTextFile(fileA, "Hello World A - Modified"); await runGitCmd(["add", "."], tempDir); await runGitCmd(["commit", "-m", "Commit 2"], tempDir); // 1. Get the current HEAD commit hash const headHash = await runGitCmd(["rev-parse", "HEAD"], tempDir); console.log(`Current HEAD: ${headHash}`); // 2. Get the tree hash of HEAD const treeHash = await runGitCmd(["rev-parse", "HEAD^{tree}"], tempDir); console.log(`Tree Hash of HEAD: ${treeHash}`); // 3. Diff HEAD against HEAD~1 const diffTreeOutput = await runGitCmd([ "diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD~1", "HEAD", ], tempDir); console.log( `\nChanged files between HEAD~1 and HEAD (O(1) diffing):\n${diffTreeOutput}`, ); // Assert that only fileA.txt changed assertEquals(diffTreeOutput, "fileA.txt"); assertNotEquals(treeHash, ""); // 4. Also use ls-tree to read the structure const lsTreeOutput = await runGitCmd(["ls-tree", "HEAD"], tempDir); console.log(`\nls-tree of HEAD:\n${lsTreeOutput}`); console.log( "\nāœ… Gen 2 Git Merkle DAG Diffing PoC successful: Deterministic isolated diffing achieved.", ); } catch (err) { console.error("āŒ Gen 2 Git Merkle DAG Diffing PoC failed:", err); Deno.exit(1); } finally { console.log(`Cleaning up isolated Git repo...`); await Deno.remove(tempDir, { recursive: true }); } }