diff --git a/README.md b/README.md index 57512cf..f2745a4 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ This repository now includes a viable alpha foundation slice of the ASP.NET Core - member-facing motions with category thresholds aligned to the labor-platform doc - health and readiness endpoints plus startup config validation - GitHub App webhook ingestion for issue, pull request, and review sync +- local bridge tooling for agent-owned GitHub draft PRs and Discord posts - xUnit integration tests under `tests/Bifrost.Web.Tests` - matching deploy artifacts and runbooks in `E:\Projects\gamecult-ops` diff --git a/docs/agent-transport.md b/docs/agent-transport.md index c9930c8..c2bb944 100644 --- a/docs/agent-transport.md +++ b/docs/agent-transport.md @@ -83,6 +83,5 @@ The plugin is listed in `.agents/plugins/marketplace.json` as `bifrost-intake`. ## Next Integration Points -- Teach the VoidBot consensus feeder to enqueue Bifrost update requests after it writes a packet. -- Let repo Faces claim by `targetRepoName`, then inject the claimed packet into Codex only when the repo matches their jurisdiction. +- Route claimed agent work that produces reviewable artifacts through `tools/bifrost-bridge.mjs` so Bifrost, not VoidBot, owns GitHub and Discord crossing receipts. - Use CultNet direct pipes when a long-running agent runtime is available; keep raw snapshot files as the dead-simple bridge until then. diff --git a/docs/bridge.md b/docs/bridge.md new file mode 100644 index 0000000..b17abbd --- /dev/null +++ b/docs/bridge.md @@ -0,0 +1,79 @@ +# Bifrost Bridge + +Bifrost is the bridge for GameCult agent work that needs to cross into GitHub, Discord, or another governed external surface. + +That means Bifrost owns the operational meaning of the crossing: + +- what action is being requested +- which repo, channel, PR, or discussion surface it targets +- which agent or account is acting +- what permission or review state made the action acceptable +- what receipt proves the crossing happened + +It does not mean Bifrost should own OAuth sludge. Heimdall owns OAuth, linked identities, token custody, grants, and signed account claims. Bifrost consumes Heimdall identity and permission facts, then decides whether a bridge action is allowed in the Bifrost domain. + +## Ownership + +- Heimdall owns provider OAuth and account-linked credentials. +- Bifrost owns bridge action policy, routing, execution receipts, and governance transport. +- VoidBot observes Discord, packages conversation, and speaks through registered personas. It should not be the durable authority that mutates GitHub. +- CultCache stores lightweight agent transport packets when the bridge needs file-native state. +- CultNet carries those packets between runtimes. + +## Local Bridge CLI + +`tools/bifrost-bridge.mjs` is the current local bridge executor. + +It intentionally uses boring local credentials while Heimdall's managed GitHub credential runtime is still unfinished: + +- GitHub actions use the local `gh` authenticated account. +- Discord posts use `BIFROST_DISCORD_BOT_TOKEN` or `DISCORD_BOT_TOKEN`. + +The CLI is not the final permission system. It is the working bridge actuator Bifrost owns now, so VoidBot and repo Faces can stop carrying this machinery themselves. + +### GitHub Draft PR + +```powershell +node .\tools\bifrost-bridge.mjs github-draft-pr ` + --repo-root E:\Projects\AetheriaLore ` + --identity nibu ` + --title "Nibu: Glitchcraft and reset-loop continuity" ` + --path Aetheria/Articles/Nibu/glitchcraft-and-reset-loops.md ` + --content-file E:\tmp\article.md ` + --body "Drafted from recent Aquarium consensus." ` + --base main +``` + +The command: + +1. checks the target repo is clean unless `--allow-dirty` is set +2. creates a branch +3. writes the target file +4. commits and pushes +5. opens a draft PR through `gh pr create` +6. restores the original branch +7. prints a JSON receipt + +Use `--dry-run` to print the planned action without writing or posting. + +### Discord Post + +```powershell +node .\tools\bifrost-bridge.mjs discord-post ` + --channel-id 1501196543150264332 ` + --content "Nibu drafted the article and put it in a PR: https://github.com/..." +``` + +The command posts through Discord's REST API using the configured bot token and prints a JSON receipt. + +## Heimdall Integration Target + +The correct future credential path is: + +1. Heimdall implements GitHub OAuth callback/runtime and managed credential custody. +2. Heimdall issues Bifrost-verifiable claims for account identity and bridge capabilities. +3. Bifrost records a bridge action request and checks the Heimdall-derived permission facts. +4. Bifrost executes or queues the action through the appropriate bridge executor. +5. Bifrost records the GitHub/Discord receipt and exposes it back to the requesting agent/runtime. + +In that shape, Bifrost is the shiny bridge. Heimdall is the gatehouse with the keys. VoidBot is not hiding a bolt cutter in its coat. diff --git a/tools/bifrost-bridge.mjs b/tools/bifrost-bridge.mjs new file mode 100644 index 0000000..db3ac3d --- /dev/null +++ b/tools/bifrost-bridge.mjs @@ -0,0 +1,277 @@ +#!/usr/bin/env node +import { spawnSync } from "node:child_process"; +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { dirname, resolve } from "node:path"; + +async function main() { + const [command, ...rawArgs] = process.argv.slice(2); + const options = parseArgs(rawArgs); + + if (!command || command === "help" || command === "--help" || command === "-h") { + printHelp(); + return; + } + + switch (command) { + case "github-draft-pr": + await createGitHubDraftPr(options); + return; + case "discord-post": + await postDiscordMessage(options); + return; + default: + throw new Error(`Unknown command "${command}". Run "node tools/bifrost-bridge.mjs help".`); + } +} + +async function createGitHubDraftPr(options) { + const repoRoot = resolve(requireOption(options, "repo-root")); + const identity = slugify(requireOption(options, "identity")); + const title = requireOption(options, "title"); + const relativePath = normalizeRelativePath(requireOption(options, "path")); + const body = await readOptionText(options, "body", "body-file", "Draft PR opened by Bifrost."); + const content = await readOptionText(options, "content", "content-file"); + const base = options.base ?? "main"; + const branch = options.branch ?? `bifrost/${identity}/${slugify(title)}-${timestampSlug(new Date())}`; + const commitMessage = options["commit-message"] ?? title; + const allowDirty = options["allow-dirty"] === "true"; + const dryRun = options["dry-run"] === "true"; + + const originalBranch = git(["branch", "--show-current"], repoRoot).stdout.trim(); + const status = git(["status", "--short"], repoRoot).stdout.trim(); + if (status.length > 0 && !allowDirty && !dryRun) { + throw new Error(`Target repo has uncommitted changes. Refusing bridge write without --allow-dirty.\n${status}`); + } + + const targetPath = resolve(repoRoot, relativePath); + if (!targetPath.toLowerCase().startsWith(`${repoRoot.toLowerCase()}\\`) && targetPath.toLowerCase() !== repoRoot.toLowerCase()) { + throw new Error(`Target path escapes repo root: ${relativePath}`); + } + + if (dryRun) { + printJson({ + dryRun: true, + action: "github-draft-pr", + repoRoot, + identity, + title, + path: relativePath, + base, + branch, + originalBranch, + commitMessage, + dirty: status.length > 0, + }); + return; + } + + let pushed = false; + let prUrl = ""; + try { + git(["switch", "-c", branch], repoRoot); + await mkdir(dirname(targetPath), { recursive: true }); + await writeFile(targetPath, content, "utf8"); + git(["add", "--", relativePath], repoRoot); + git(["commit", "-m", commitMessage], repoRoot); + git(["push", "-u", "origin", branch], repoRoot); + pushed = true; + + const pr = run("gh", [ + "pr", + "create", + "--draft", + "--title", + title, + "--body", + body, + "--base", + base, + "--head", + branch, + ], repoRoot); + prUrl = pr.stdout.trim(); + } finally { + if (originalBranch) { + git(["switch", originalBranch], repoRoot, { allowFailure: true }); + } + } + + printJson({ + action: "github-draft-pr", + ok: true, + repoRoot, + identity, + title, + path: relativePath, + base, + branch, + pushed, + prUrl, + }); +} + +async function postDiscordMessage(options) { + const token = process.env.BIFROST_DISCORD_BOT_TOKEN ?? process.env.DISCORD_BOT_TOKEN; + const channelId = requireOption(options, "channel-id"); + const content = await readOptionText(options, "content", "content-file"); + const dryRun = options["dry-run"] === "true"; + + if (dryRun) { + printJson({ + dryRun: true, + action: "discord-post", + channelId, + content, + }); + return; + } + + if (!token) { + throw new Error("Set BIFROST_DISCORD_BOT_TOKEN or DISCORD_BOT_TOKEN before posting to Discord."); + } + + const response = await fetch(`https://discord.com/api/v10/channels/${channelId}/messages`, { + method: "POST", + headers: { + Authorization: `Bot ${token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ content }), + }); + + const text = await response.text(); + if (!response.ok) { + throw new Error(`Discord post failed with ${response.status}: ${text}`); + } + + const message = JSON.parse(text); + printJson({ + action: "discord-post", + ok: true, + channelId, + messageId: message.id, + url: `https://discord.com/channels/${message.guild_id ?? "@me"}/${channelId}/${message.id}`, + }); +} + +async function readOptionText(options, inlineName, fileName, fallback) { + const inline = optionalString(options[inlineName]); + if (inline) { + return inline; + } + + const file = optionalString(options[fileName]); + if (file) { + return readFile(resolve(file), "utf8"); + } + + if (fallback !== undefined) { + return fallback; + } + + throw new Error(`Missing required option --${inlineName} or --${fileName}.`); +} + +function git(args, cwd, options = {}) { + return run("git", args, cwd, options); +} + +function run(command, args, cwd, options = {}) { + const result = spawnSync(command, args, { + cwd, + encoding: "utf8", + stdio: ["ignore", "pipe", "pipe"], + windowsHide: true, + }); + + if (result.status !== 0 && !options.allowFailure) { + throw new Error(`${command} ${args.join(" ")} failed with ${result.status ?? "unknown"}:\n${result.stderr || result.stdout}`); + } + + return { + stdout: result.stdout ?? "", + stderr: result.stderr ?? "", + status: result.status, + }; +} + +function parseArgs(args) { + const options = {}; + for (let index = 0; index < args.length; index += 1) { + const token = args[index]; + if (!token.startsWith("--")) { + throw new Error(`Unexpected argument "${token}".`); + } + + const key = token.slice(2); + const next = args[index + 1]; + if (!next || next.startsWith("--")) { + options[key] = "true"; + continue; + } + + options[key] = next; + index += 1; + } + + return options; +} + +function requireOption(options, name) { + const value = optionalString(options[name]); + if (!value) { + throw new Error(`Missing required option --${name}.`); + } + return value; +} + +function optionalString(value) { + if (typeof value !== "string") { + return undefined; + } + const trimmed = value.trim(); + return trimmed.length > 0 ? trimmed : undefined; +} + +function normalizeRelativePath(value) { + const normalized = value.replace(/\\/g, "/").replace(/^\/+/, ""); + if (normalized.includes("../") || normalized === "..") { + throw new Error(`Relative path may not escape the repo: ${value}`); + } + return normalized; +} + +function slugify(value) { + return String(value) + .trim() + .toLowerCase() + .replace(/[^a-z0-9._-]+/g, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 80) || "bridge"; +} + +function timestampSlug(date) { + return date.toISOString().replace(/[:.]/g, "-"); +} + +function printJson(value) { + process.stdout.write(`${JSON.stringify(value, null, 2)}\n`); +} + +function printHelp() { + process.stdout.write(`Bifrost bridge + +Commands: + github-draft-pr Write one file in a target repo and open a draft PR through gh + discord-post Post a message to Discord through the configured bot token + +Examples: + node tools/bifrost-bridge.mjs github-draft-pr --repo-root E:/Projects/AetheriaLore --identity nibu --title "Nibu: Glitchcraft" --path Aetheria/Articles/Nibu/glitchcraft.md --content-file article.md + node tools/bifrost-bridge.mjs discord-post --channel-id 1501196543150264332 --content "Draft PR opened: https://github.com/..." +`); +} + +main().catch((error) => { + process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); + process.exitCode = 1; +});