Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
preserve public PR context in mirror commits (#2457)
robert-inkeepd52114dd5219
diff --git a/.github/scripts/bridge-public-pr-to-monorepo.mjs b/.github/scripts/bridge-public-pr-to-monorepo.mjsindex 66bc552be..2c683837c 100644--- a/.github/scripts/bridge-public-pr-to-monorepo.mjs+++ b/.github/scripts/bridge-public-pr-to-monorepo.mjs@@ -310,6 +310,22 @@ function internalPullRequestTitle(publicPr) {return `Sync public PR #${publicPr.number}: ${publicPr.title}`;}+function singleLineCommitSubject(value) {+ return String(value ?? '')+ .replace(/\s+/g, ' ')+ .trim();+}++function bridgeCommitSubject({ publicRepo, publicPr, hasConflicts }) {+ const title = singleLineCommitSubject(publicPr.title);+ const suffix = hasConflicts ? ` (${CONFLICT_COMMIT_MARKER})` : '';+ return [+ `chore(sync): mirror ${publicRepo}#${publicPr.number}`,+ title ? `: ${title}` : '',+ suffix,+ ].join('');+}+function buildBridgeMetadata(publicPr, mirrorPath) {return ['<!-- public-pr-sync',@@ -362,27 +378,73 @@ function uniqueCommitAuthors(authors, fallbackAuthor) {return unique.size > 0 ? [...unique.values()] : [fallbackAuthor];}-async function listPublicPrCommitAuthors({ token, repo, prNumber, request = githubRequest }) {- const commitAuthors = [];+function normalizePublicPrCommit(commit) {+ return {+ sha: typeof commit?.sha === 'string' ? commit.sha : null,+ author: normalizeGitHubUserAuthor(commit?.author) ?? commit?.commit?.author,+ message: typeof commit?.commit?.message === 'string' ? commit.commit.message : '',+ };+}++async function listPublicPrCommits({ token, repo, prNumber, request = githubRequest }) {+ const publicCommits = [];let page = 1;while (true) {const commits = await request({token,path: `/repos/${repo}/pulls/${prNumber}/commits?per_page=100&page=${page}`,});- commitAuthors.push(- ...commits.map((commit) => normalizeGitHubUserAuthor(commit.author) ?? commit.commit?.author),- );+ publicCommits.push(...commits.map((commit) => normalizePublicPrCommit(commit)));if (commits.length < 100) break;page++;}- return commitAuthors;+ return publicCommits;+}++async function listPublicPrCommitAuthors({ token, repo, prNumber, request = githubRequest }) {+ const commits = await listPublicPrCommits({ token, repo, prNumber, request });+ return commits.map((commit) => commit.author);+}++function normalizeCommitMessage(message) {+ if (typeof message !== 'string') return '';+ return message.replace(/\r\n?/g, '\n').replace(/\0/g, '').trim();+}++function formatOriginalCommitMessages(commitMessages) {+ const entries = commitMessages+ .map((commit) => {+ const message = normalizeCommitMessage(commit?.message);+ if (!message) return null;+ const shortSha =+ typeof commit?.sha === 'string' && /^[0-9a-f]{7,40}$/i.test(commit.sha)+ ? commit.sha.slice(0, 7)+ : null;+ return { shortSha, message };+ })+ .filter(Boolean);++ if (entries.length === 0) return '';++ const formatted = entries.map((entry, index) => {+ const [subject, ...bodyLines] = entry.message.split('\n');+ const prefix = entry.shortSha+ ? `${index + 1}. ${entry.shortSha} ${subject}`+ : `${index + 1}. ${subject}`;+ const body =+ bodyLines.length > 0 ? `\n\n${bodyLines.map((line) => ` ${line}`).join('\n')}` : '';+ return `${prefix}${body}`;+ });++ return ['Original public commit messages:', '', ...formatted].join('\n');}-function buildCommitAttribution({ commitAuthors, fallbackAuthor }) {+function buildCommitAttribution({ commitAuthors, commitMessages = [], fallbackAuthor }) {const authors = uniqueCommitAuthors(commitAuthors, fallbackAuthor);const trailers = authors.map((author) => `Co-authored-by: ${author.name} <${author.email}>`);- return { trailers };+ const originalCommitMessages = formatOriginalCommitMessages(commitMessages);+ const body = [originalCommitMessages, trailers.join('\n')].filter(Boolean).join('\n\n');+ return { trailers, originalCommitMessages, body };}// GitHub PR body hard limit. Exceeding returns 422 "body is too long".@@ -576,7 +638,15 @@ async function checkOrgMembership({ token, org, login, request = githubRequest }// Post a commit status — used for the bridge's own `cla/verified` context on the// internal PR head, the signal the agents-private branch ruleset requires.-async function postCommitStatus({ token, repo, sha, state, context, description, request = githubRequest }) {+async function postCommitStatus({+ token,+ repo,+ sha,+ state,+ context,+ description,+ request = githubRequest,+}) {await request({token,method: 'POST',@@ -886,33 +956,33 @@ async function syncPublicPr() {]);- let commitAuthors = [];+ let publicCommits = [];try {- commitAuthors = await listPublicPrCommitAuthors({+ publicCommits = await listPublicPrCommits({token: publicToken,repo: publicRepo,prNumber: publicPr.number,});} catch (error) {console.warn(- `Bridge: could not fetch public PR commit authors; falling back to PR opener attribution: ${error.message}`,+ `Bridge: could not fetch public PR commits; falling back to PR opener attribution: ${error.message}`,);}- const { trailers } = buildCommitAttribution({- commitAuthors,+ const { body: commitBody } = buildCommitAttribution({+ commitAuthors: publicCommits.map((commit) => commit.author),+ commitMessages: publicCommits,fallbackAuthor: fallbackPublicAuthor(publicPr),});- const commitMessage = hasConflicts- ? `chore(sync): mirror ${publicRepo}#${publicPr.number} (${CONFLICT_COMMIT_MARKER})`- : `chore(sync): mirror ${publicRepo}#${publicPr.number}`;+ const commitMessage = bridgeCommitSubject({ publicRepo, publicPr, hasConflicts });run('git', ['-C',internalRepoDir,'commit',+ '--cleanup=verbatim','-m',commitMessage,'-m',- trailers.join('\n'),+ commitBody,]);run('git', [@@ -1136,6 +1206,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)export {applyPatchWithConflictDetection,+ bridgeCommitSubject,buildCommitAttribution,buildInternalPrBody,buildPublicComment,@@ -1143,6 +1214,7 @@ export {commitIndicatesConflicts,createClaGateGh,listPublicPrCommitAuthors,+ listPublicPrCommits,normalizeGitHubUserAuthor,postCommitStatus,prefixPatchPaths,diff --git a/.github/scripts/bridge-public-pr-to-monorepo.test.mjs b/.github/scripts/bridge-public-pr-to-monorepo.test.mjsindex d72c5b619..617dca411 100644--- a/.github/scripts/bridge-public-pr-to-monorepo.test.mjs+++ b/.github/scripts/bridge-public-pr-to-monorepo.test.mjs@@ -1,21 +1,23 @@-import { describe, expect, test } from "bun:test";-import { execFileSync } from "node:child_process";-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";-import { tmpdir } from "node:os";-import path from "node:path";+import { describe, expect, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import path from 'node:path';import {applyPatchWithConflictDetection,+ bridgeCommitSubject,buildCommitAttribution,buildPublicComment,checkOrgMembership,commitIndicatesConflicts,createClaGateGh,listPublicPrCommitAuthors,+ listPublicPrCommits,normalizeGitHubUserAuthor,postCommitStatus,readCommitClaStatus,syncPublicPr,-} from "./bridge-public-pr-to-monorepo.mjs";+} from './bridge-public-pr-to-monorepo.mjs';// A fake `githubRequest`: records every call and returns the queued response.// The bridge's GitHub adapters are the only seam where the `license/cla` and@@ -30,63 +32,156 @@ const fakeRequest = (response) => {return { request, calls };};-describe("buildCommitAttribution", () => {- test("credits every unique human author with native coauthor trailers", () => {+describe('buildCommitAttribution', () => {+ test('credits every unique human author with native coauthor trailers', () => {const attribution = buildCommitAttribution({commitAuthors: [- { name: "Sarah Inkeep", email: "[email protected]" },- { name: "github-actions[bot]", email: "41898282+github-actions[bot]@users.noreply.github.com" },- { name: "Sarah Inkeep", email: "[email protected]" },- { name: "Robert Inkeep", email: "[email protected]" },+ { name: 'Sarah Inkeep', email: '[email protected]' },+ {+ name: 'github-actions[bot]',+ email: '41898282+github-actions[bot]@users.noreply.github.com',+ },+ { name: 'Sarah Inkeep', email: '[email protected]' },+ { name: 'Robert Inkeep', email: '[email protected]' },],- fallbackAuthor: { name: "octocat", email: "[email protected]" },+ fallbackAuthor: { name: 'octocat', email: '[email protected]' },});expect(attribution.trailers).toEqual([- "Co-authored-by: Sarah Inkeep <[email protected]>",- "Co-authored-by: Robert Inkeep <[email protected]>",+ 'Co-authored-by: Sarah Inkeep <[email protected]>',+ 'Co-authored-by: Robert Inkeep <[email protected]>',]);});- test("falls back to the public PR author when commits only have invalid authors", () => {+ test('preserves original public commit messages before coauthor trailers', () => {+ const attribution = buildCommitAttribution({+ commitAuthors: [{ name: 'Sarah Inkeep', email: '[email protected]' }],+ commitMessages: [+ { sha: '1234567890abcdef', message: 'Add search\n\nExplain the indexing path.' },+ {+ sha: 'fedcba9876543210',+ message: 'Fix empty query\r\n\r\nCo-authored-by: A <[email protected]>',+ },+ ],+ fallbackAuthor: { name: 'octocat', email: '[email protected]' },+ });++ expect(attribution.originalCommitMessages).toBe(+ [+ 'Original public commit messages:',+ '',+ '1. 1234567 Add search',+ '',+ ' ',+ ' Explain the indexing path.',+ '2. fedcba9 Fix empty query',+ '',+ ' ',+ ' Co-authored-by: A <[email protected]>',+ ].join('\n'),+ );+ expect(attribution.body).toContain('Original public commit messages:');+ expect(+ attribution.body.trim().endsWith('Co-authored-by: Sarah Inkeep <[email protected]>'),+ ).toBe(true);+ });++ test('falls back to the public PR author when commits only have invalid authors', () => {const attribution = buildCommitAttribution({commitAuthors: [- { name: "github-actions[bot]", email: "41898282+github-actions[bot]@users.noreply.github.com" },- { name: "Bad\nName", email: "[email protected]" },+ {+ name: 'github-actions[bot]',+ email: '41898282+github-actions[bot]@users.noreply.github.com',+ },+ { name: 'Bad\nName', email: '[email protected]' },],- fallbackAuthor: { name: "octocat", email: "[email protected]" },+ fallbackAuthor: { name: 'octocat', email: '[email protected]' },});expect(attribution.trailers).toEqual([- "Co-authored-by: octocat <[email protected]>",+ 'Co-authored-by: octocat <[email protected]>',]);});});-describe("normalizeGitHubUserAuthor", () => {- test("uses the GitHub-resolved user id/login noreply identity", () => {- expect(normalizeGitHubUserAuthor({ login: "rtran9", id: 11001605 })).toEqual({- name: "rtran9",- email: "[email protected]",+describe('bridgeCommitSubject', () => {+ test('includes the original public PR title while keeping the sync prefix', () => {+ expect(+ bridgeCommitSubject({+ publicRepo: 'inkeep/open-knowledge',+ publicPr: { number: 361, title: 'Preserve contributor commit messages' },+ hasConflicts: false,+ }),+ ).toBe('chore(sync): mirror inkeep/open-knowledge#361: Preserve contributor commit messages');+ });++ test('normalizes multiline titles and preserves the conflict marker', () => {+ expect(+ bridgeCommitSubject({+ publicRepo: 'inkeep/open-knowledge',+ publicPr: { number: 361, title: 'Fix bridge\n\nsubject' },+ hasConflicts: true,+ }),+ ).toBe(+ 'chore(sync): mirror inkeep/open-knowledge#361: Fix bridge subject (with conflicts; needs manual resolution)',+ );+ });+});++describe('listPublicPrCommits', () => {+ test('returns public PR commit authors and messages', async () => {+ const { request } = fakeRequest([+ {+ sha: 'abcdef1234567890',+ author: { login: 'linked-author', id: 7 },+ commit: {+ author: { name: 'Raw Author', email: '[email protected]' },+ message: 'Add bridge attribution\n\nKeep the public commit message.',+ },+ },+ ]);++ await expect(+ listPublicPrCommits({+ token: 't',+ repo: 'owner/repo',+ prNumber: 123,+ request,+ }),+ ).resolves.toEqual([+ {+ sha: 'abcdef1234567890',+ author: { name: 'linked-author', email: '[email protected]' },+ message: 'Add bridge attribution\n\nKeep the public commit message.',+ },+ ]);+ });+});++describe('normalizeGitHubUserAuthor', () => {+ test('uses the GitHub-resolved user id/login noreply identity', () => {+ expect(normalizeGitHubUserAuthor({ login: 'rtran9', id: 11001605 })).toEqual({+ name: 'rtran9',+ email: '[email protected]',});});- test("ignores unresolved and bot users so callers can fall back", () => {+ test('ignores unresolved and bot users so callers can fall back', () => {expect(normalizeGitHubUserAuthor(null)).toBeNull();- expect(normalizeGitHubUserAuthor({ login: "github-actions[bot]", id: 41898282 })).toBeNull();+ expect(normalizeGitHubUserAuthor({ login: 'github-actions[bot]', id: 41898282 })).toBeNull();});});-describe("listPublicPrCommitAuthors", () => {- test("paginates through every public PR commit author", async () => {+describe('listPublicPrCommitAuthors', () => {+ test('paginates through every public PR commit author', async () => {const pageOne = Array.from({ length: 100 }, (_, index) => ({author: { login: `author-${index}`, id: index + 1 },commit: { author: { name: `Commit ${index}`, email: `commit-${index}@example.com` } },}));const pageTwo = [{- author: { login: "final-author", id: 101 },- commit: { author: { name: "Final Commit", email: "[email protected]" } },+ author: { login: 'final-author', id: 101 },+ commit: { author: { name: 'Final Commit', email: '[email protected]' } },},];const calls = [];@@ -96,167 +191,167 @@ describe("listPublicPrCommitAuthors", () => {};const authors = await listPublicPrCommitAuthors({- token: "t",- repo: "owner/repo",+ token: 't',+ repo: 'owner/repo',prNumber: 123,request,});expect(authors).toHaveLength(101);expect(authors.at(-1)).toEqual({- name: "final-author",- email: "[email protected]",+ name: 'final-author',+ email: '[email protected]',});expect(calls.map((call) => call.path)).toEqual([- "/repos/owner/repo/pulls/123/commits?per_page=100&page=1",- "/repos/owner/repo/pulls/123/commits?per_page=100&page=2",+ '/repos/owner/repo/pulls/123/commits?per_page=100&page=1',+ '/repos/owner/repo/pulls/123/commits?per_page=100&page=2',]);});- test("falls back to raw commit author when GitHub has no linked user", async () => {+ test('falls back to raw commit author when GitHub has no linked user', async () => {const { request } = fakeRequest([{author: null,- commit: { author: { name: "Patch Author", email: "[email protected]" } },+ commit: { author: { name: 'Patch Author', email: '[email protected]' } },},]);await expect(listPublicPrCommitAuthors({- token: "t",- repo: "owner/repo",+ token: 't',+ repo: 'owner/repo',prNumber: 123,request,}),- ).resolves.toEqual([{ name: "Patch Author", email: "[email protected]" }]);+ ).resolves.toEqual([{ name: 'Patch Author', email: '[email protected]' }]);});});-describe("readCommitClaStatus", () => {- test("extracts the license/cla state from the combined status", async () => {+describe('readCommitClaStatus', () => {+ test('extracts the license/cla state from the combined status', async () => {const { request } = fakeRequest({statuses: [- { context: "ci/build", state: "success" },- { context: "license/cla", state: "success" },+ { context: 'ci/build', state: 'success' },+ { context: 'license/cla', state: 'success' },],});- expect(await readCommitClaStatus({ token: "t", repo: "o/r", sha: "abc", request })).toBe(- "success",+ expect(await readCommitClaStatus({ token: 't', repo: 'o/r', sha: 'abc', request })).toBe(+ 'success',);});- test("returns null when the license/cla context is absent", async () => {- const { request } = fakeRequest({ statuses: [{ context: "ci/build", state: "failure" }] });- expect(await readCommitClaStatus({ token: "t", repo: "o/r", sha: "abc", request })).toBeNull();+ test('returns null when the license/cla context is absent', async () => {+ const { request } = fakeRequest({ statuses: [{ context: 'ci/build', state: 'failure' }] });+ expect(await readCommitClaStatus({ token: 't', repo: 'o/r', sha: 'abc', request })).toBeNull();});- test("returns null for an empty status set", async () => {+ test('returns null for an empty status set', async () => {const { request } = fakeRequest({ statuses: [] });- expect(await readCommitClaStatus({ token: "t", repo: "o/r", sha: "abc", request })).toBeNull();+ expect(await readCommitClaStatus({ token: 't', repo: 'o/r', sha: 'abc', request })).toBeNull();});test("requests the combined status with per_page=100 so license/cla can't fall off page 1", async () => {const { request, calls } = fakeRequest({ statuses: [] });- await readCommitClaStatus({ token: "t", repo: "owner/repo", sha: "deadbeef", request });+ await readCommitClaStatus({ token: 't', repo: 'owner/repo', sha: 'deadbeef', request });expect(calls).toHaveLength(1);- expect(calls[0].path).toBe("/repos/owner/repo/commits/deadbeef/status?per_page=100");+ expect(calls[0].path).toBe('/repos/owner/repo/commits/deadbeef/status?per_page=100');});});-describe("postCommitStatus", () => {+describe('postCommitStatus', () => {test("POSTs the given state/context/description to the commit's statuses endpoint", async () => {const { request, calls } = fakeRequest(undefined);await postCommitStatus({- token: "t",- repo: "owner/repo",- sha: "abc123",- state: "failure",- context: "cla/verified",- description: "held",+ token: 't',+ repo: 'owner/repo',+ sha: 'abc123',+ state: 'failure',+ context: 'cla/verified',+ description: 'held',request,});expect(calls).toHaveLength(1);expect(calls[0]).toMatchObject({- method: "POST",- path: "/repos/owner/repo/statuses/abc123",- body: { state: "failure", context: "cla/verified", description: "held" },+ method: 'POST',+ path: '/repos/owner/repo/statuses/abc123',+ body: { state: 'failure', context: 'cla/verified', description: 'held' },});});});-describe("checkOrgMembership", () => {- test("returns true on a 204 (member)", async () => {+describe('checkOrgMembership', () => {+ test('returns true on a 204 (member)', async () => {const { request, calls } = fakeRequest(null);- expect(- await checkOrgMembership({ token: "t", org: "inkeep", login: "octocat", request }),- ).toBe(true);- expect(calls[0].path).toBe("/orgs/inkeep/members/octocat");+ expect(await checkOrgMembership({ token: 't', org: 'inkeep', login: 'octocat', request })).toBe(+ true,+ );+ expect(calls[0].path).toBe('/orgs/inkeep/members/octocat');});- test("returns false on a 404 (non-member)", async () => {+ test('returns false on a 404 (non-member)', async () => {const request = async () => {- const error = new Error("not found (404)");+ const error = new Error('not found (404)');error.status = 404;throw error;};expect(- await checkOrgMembership({ token: "t", org: "inkeep", login: "outsider", request }),+ await checkOrgMembership({ token: 't', org: 'inkeep', login: 'outsider', request }),).toBe(false);});- test("propagates non-404 errors so the gate fails closed", async () => {+ test('propagates non-404 errors so the gate fails closed', async () => {const request = async () => {- const error = new Error("forbidden (403)");+ const error = new Error('forbidden (403)');error.status = 403;throw error;};await expect(- checkOrgMembership({ token: "t", org: "inkeep", login: "x", request }),+ checkOrgMembership({ token: 't', org: 'inkeep', login: 'x', request }),).rejects.toThrow(/403/);});});-describe("createClaGateGh", () => {+describe('createClaGateGh', () => {const deps = {- publicToken: "public-token",- publicRepo: "inkeep/open-knowledge",- internalToken: "internal-token",- internalRepo: "inkeep/agents-private",+ publicToken: 'public-token',+ publicRepo: 'inkeep/open-knowledge',+ internalToken: 'internal-token',+ internalRepo: 'inkeep/agents-private',};- test("readClaStatus reads license/cla from the public PR head, on the public token", async () => {+ test('readClaStatus reads license/cla from the public PR head, on the public token', async () => {const { request, calls } = fakeRequest({- statuses: [{ context: "license/cla", state: "pending" }],+ statuses: [{ context: 'license/cla', state: 'pending' }],});const gh = createClaGateGh({ ...deps, request });- const state = await gh.readClaStatus({ head: { sha: "public-head" } });- expect(state).toBe("pending");- expect(calls[0].token).toBe("public-token");+ const state = await gh.readClaStatus({ head: { sha: 'public-head' } });+ expect(state).toBe('pending');+ expect(calls[0].token).toBe('public-token');expect(calls[0].path).toBe(- "/repos/inkeep/open-knowledge/commits/public-head/status?per_page=100",+ '/repos/inkeep/open-knowledge/commits/public-head/status?per_page=100',);});- test("setVerifiedStatus posts the cla/verified context to the internal PR head", async () => {+ test('setVerifiedStatus posts the cla/verified context to the internal PR head', async () => {const { request, calls } = fakeRequest(undefined);const gh = createClaGateGh({ ...deps, request });- await gh.setVerifiedStatus({ head: { sha: "internal-head" } }, "failure", "needs signature");+ await gh.setVerifiedStatus({ head: { sha: 'internal-head' } }, 'failure', 'needs signature');expect(calls).toHaveLength(1);- expect(calls[0].token).toBe("internal-token");- expect(calls[0].path).toBe("/repos/inkeep/agents-private/statuses/internal-head");+ expect(calls[0].token).toBe('internal-token');+ expect(calls[0].path).toBe('/repos/inkeep/agents-private/statuses/internal-head');expect(calls[0].body).toEqual({- state: "failure",- context: "cla/verified",- description: "needs signature",+ state: 'failure',+ context: 'cla/verified',+ description: 'needs signature',});});test("isOrgMember checks the internal repo's org on the internal token", async () => {const { request, calls } = fakeRequest(null);const gh = createClaGateGh({ ...deps, request });- expect(await gh.isOrgMember("octocat")).toBe(true);- expect(calls[0].token).toBe("internal-token");- expect(calls[0].path).toBe("/orgs/inkeep/members/octocat");+ expect(await gh.isOrgMember('octocat')).toBe(true);+ expect(calls[0].token).toBe('internal-token');+ expect(calls[0].path).toBe('/orgs/inkeep/members/octocat');});});@@ -270,35 +365,35 @@ describe("createClaGateGh", () => {// command, not a mock.const git = (dir, ...args) =>- execFileSync("git", ["-C", dir, ...args], { encoding: "utf8" }).trim();+ execFileSync('git', ['-C', dir, ...args], { encoding: 'utf8' }).trim();// Build a real temp git repo: BASE = the comment-stripped public version (so the// patch's base blob is reachable for the 3-way), a patch = the contributor's// change against BASE, and the working tree = OURS (the comment-rich internal// version) the bridge applies onto. Returns { repoDir, patchFile, cleanup }.function setupBridgeRepo({ baseContent, oursContent, theirsContent }) {- const root = mkdtempSync(path.join(tmpdir(), "bridge-canary-"));- const repoDir = path.join(root, "repo");+ const root = mkdtempSync(path.join(tmpdir(), 'bridge-canary-'));+ const repoDir = path.join(root, 'repo');mkdirSync(repoDir);- git(repoDir, "init", "-q");- git(repoDir, "config", "user.email", "[email protected]");- git(repoDir, "config", "user.name", "canary");- writeFileSync(path.join(repoDir, "f.ts"), baseContent);- git(repoDir, "add", "-A");- git(repoDir, "commit", "-qm", "base");- const base = git(repoDir, "rev-parse", "HEAD");- const defaultBranch = git(repoDir, "rev-parse", "--abbrev-ref", "HEAD");+ git(repoDir, 'init', '-q');+ git(repoDir, 'config', 'user.email', '[email protected]');+ git(repoDir, 'config', 'user.name', 'canary');+ writeFileSync(path.join(repoDir, 'f.ts'), baseContent);+ git(repoDir, 'add', '-A');+ git(repoDir, 'commit', '-qm', 'base');+ const base = git(repoDir, 'rev-parse', 'HEAD');+ const defaultBranch = git(repoDir, 'rev-parse', '--abbrev-ref', 'HEAD');// theirs = the contributor's change against the stripped base, captured as a diff- git(repoDir, "checkout", "-q", "-b", "contributor");- writeFileSync(path.join(repoDir, "f.ts"), theirsContent);- git(repoDir, "commit", "-qam", "contributor change");- const patch = git(repoDir, "diff", base, "HEAD");- const patchFile = path.join(root, "contributor.patch");+ git(repoDir, 'checkout', '-q', '-b', 'contributor');+ writeFileSync(path.join(repoDir, 'f.ts'), theirsContent);+ git(repoDir, 'commit', '-qam', 'contributor change');+ const patch = git(repoDir, 'diff', base, 'HEAD');+ const patchFile = path.join(root, 'contributor.patch');writeFileSync(patchFile, `${patch}\n`);// ours = the comment-rich internal version, checked out as the apply target- git(repoDir, "checkout", "-q", defaultBranch);- writeFileSync(path.join(repoDir, "f.ts"), oursContent);- git(repoDir, "commit", "-qam", "internal (comment-rich)");+ git(repoDir, 'checkout', '-q', defaultBranch);+ writeFileSync(path.join(repoDir, 'f.ts'), oursContent);+ git(repoDir, 'commit', '-qam', 'internal (comment-rich)');return {repoDir,patchFile,@@ -306,22 +401,22 @@ function setupBridgeRepo({ baseContent, oursContent, theirsContent }) {};}-const STRIPPED_BASE = "export const A = 1;\nexport const B = 2;\nexport const C = 3;\n";+const STRIPPED_BASE = 'export const A = 1;\nexport const B = 2;\nexport const C = 3;\n';-describe("applyPatchWithConflictDetection (graceful conflict routing canary)", () => {+describe('applyPatchWithConflictDetection (graceful conflict routing canary)', () => {test("a comment-adjacency 3-way conflict is classified 'conflicts', not a hard failure", () => {// OURS adds a comment on the SAME line the contributor edits — the exact// comment-strip divergence that broke the bridge. Guaranteed 3-way conflict.const { repoDir, patchFile, cleanup } = setupBridgeRepo({baseContent: STRIPPED_BASE,- theirsContent: "export const A = 1;\nexport const B = 22;\nexport const C = 3;\n",+ theirsContent: 'export const A = 1;\nexport const B = 22;\nexport const C = 3;\n',oursContent:- "export const A = 1;\nexport const B = 2; // important constant (mirror strips this)\nexport const C = 3;\n",+ 'export const A = 1;\nexport const B = 2; // important constant (mirror strips this)\nexport const C = 3;\n',});try {const result = applyPatchWithConflictDetection(repoDir, patchFile);- expect(result.outcome).toBe("conflicts");- expect(result.conflictedPaths).toContain("f.ts");+ expect(result.outcome).toBe('conflicts');+ expect(result.conflictedPaths).toContain('f.ts');} finally {cleanup();}@@ -333,20 +428,20 @@ describe("applyPatchWithConflictDetection (graceful conflict routing canary)", (// cleanly. (Adjacent divergences share a hunk and conflict; that's exactly// why the bridge conflicts on comment-dense regions.)const longBase =- "export const A = 1;\nexport const B = 2;\n" +- "const p = 0;\nconst q = 0;\nconst r = 0;\nconst s = 0;\nconst u = 0;\nconst v = 0;\n" +- "export const C = 3;\n";+ 'export const A = 1;\nexport const B = 2;\n' ++ 'const p = 0;\nconst q = 0;\nconst r = 0;\nconst s = 0;\nconst u = 0;\nconst v = 0;\n' ++ 'export const C = 3;\n';const { repoDir, patchFile, cleanup } = setupBridgeRepo({baseContent: longBase,- theirsContent: longBase.replace("export const B = 2;", "export const B = 22;"),+ theirsContent: longBase.replace('export const B = 2;', 'export const B = 22;'),oursContent: longBase.replace(- "export const C = 3;",- "export const C = 3; // note on C (mirror strips this)",+ 'export const C = 3;',+ 'export const C = 3; // note on C (mirror strips this)',),});try {const result = applyPatchWithConflictDetection(repoDir, patchFile);- expect(result.outcome).toBe("clean");+ expect(result.outcome).toBe('clean');expect(result.conflictedPaths).toHaveLength(0);} finally {cleanup();@@ -354,32 +449,32 @@ describe("applyPatchWithConflictDetection (graceful conflict routing canary)", (});test("a genuinely un-appliable patch stays 'failed' (fail-closed, not swallowed)", () => {- const root = mkdtempSync(path.join(tmpdir(), "bridge-canary-failed-"));- const repoDir = path.join(root, "repo");+ const root = mkdtempSync(path.join(tmpdir(), 'bridge-canary-failed-'));+ const repoDir = path.join(root, 'repo');mkdirSync(repoDir);- git(repoDir, "init", "-q");- git(repoDir, "config", "user.email", "[email protected]");- git(repoDir, "config", "user.name", "canary");- writeFileSync(path.join(repoDir, "f.ts"), STRIPPED_BASE);- git(repoDir, "add", "-A");- git(repoDir, "commit", "-qm", "base");+ git(repoDir, 'init', '-q');+ git(repoDir, 'config', 'user.email', '[email protected]');+ git(repoDir, 'config', 'user.name', 'canary');+ writeFileSync(path.join(repoDir, 'f.ts'), STRIPPED_BASE);+ git(repoDir, 'add', '-A');+ git(repoDir, 'commit', '-qm', 'base');// A patch against a file that does not exist, whose base blobs are absent —// git apply --3way can neither apply it directly nor reconstruct a 3-way base.const ghostPatch = [- "diff --git a/ghost.ts b/ghost.ts",- "index 1111111..2222222 100644",- "--- a/ghost.ts",- "+++ b/ghost.ts",- "@@ -1 +1 @@",- "-old",- "+new",- "",- ].join("\n");- const patchFile = path.join(root, "ghost.patch");+ 'diff --git a/ghost.ts b/ghost.ts',+ 'index 1111111..2222222 100644',+ '--- a/ghost.ts',+ '+++ b/ghost.ts',+ '@@ -1 +1 @@',+ '-old',+ '+new',+ '',+ ].join('\n');+ const patchFile = path.join(root, 'ghost.patch');writeFileSync(patchFile, ghostPatch);try {const result = applyPatchWithConflictDetection(repoDir, patchFile);- expect(result.outcome).toBe("failed");+ expect(result.outcome).toBe('failed');expect(result.conflictedPaths).toHaveLength(0);expect(result.message).toBeTruthy();} finally {@@ -387,42 +482,42 @@ describe("applyPatchWithConflictDetection (graceful conflict routing canary)", (}});- test("conflict and failure comments give accurate guidance and never advise a rebase", () => {+ test('conflict and failure comments give accurate guidance and never advise a rebase', () => {const publicPr = {- user: { login: "octocat" },- base: { repo: { full_name: "inkeep/open-knowledge" } },+ user: { login: 'octocat' },+ base: { repo: { full_name: 'inkeep/open-knowledge' } },};- const conflicts = buildPublicComment({ publicPr, status: "conflicts" });+ const conflicts = buildPublicComment({ publicPr, status: 'conflicts' });const failed = buildPublicComment({publicPr,- status: "failed",- details: "The diff could not be applied to the internal monorepo.",+ status: 'failed',+ details: 'The diff could not be applied to the internal monorepo.',});for (const body of [conflicts, failed]) {- expect(body.toLowerCase()).not.toContain("rebase");+ expect(body.toLowerCase()).not.toContain('rebase');}- expect(conflicts).toContain("No action is needed from you");- expect(conflicts).toContain("@octocat");+ expect(conflicts).toContain('No action is needed from you');+ expect(conflicts).toContain('@octocat');// upsertIssueComment finds+updates the existing comment by this marker; if the// conflicts branch dropped it, every metadata re-sync would post a duplicate.- expect(conflicts).toContain("<!-- monorepo-pr-bridge -->");+ expect(conflicts).toContain('<!-- monorepo-pr-bridge -->');});});-describe("commitIndicatesConflicts (metadata-re-sync conflict-hold guard)", () => {- test("true for a conflict-marker mirror commit message", () => {+describe('commitIndicatesConflicts (metadata-re-sync conflict-hold guard)', () => {+ test('true for a conflict-marker mirror commit message', () => {expect(commitIndicatesConflicts(- "chore(sync): mirror inkeep/open-knowledge#310 (with conflicts; needs manual resolution)",+ 'chore(sync): mirror inkeep/open-knowledge#310 (with conflicts; needs manual resolution)',),).toBe(true);});- test("false for a clean mirror commit message", () => {- expect(commitIndicatesConflicts("chore(sync): mirror inkeep/open-knowledge#310")).toBe(false);+ test('false for a clean mirror commit message', () => {+ expect(commitIndicatesConflicts('chore(sync): mirror inkeep/open-knowledge#310')).toBe(false);});- test("false for absent / non-string input", () => {+ test('false for absent / non-string input', () => {expect(commitIndicatesConflicts(undefined)).toBe(false);expect(commitIndicatesConflicts(null)).toBe(false);expect(commitIndicatesConflicts(42)).toBe(false);@@ -441,8 +536,8 @@ describe("commitIndicatesConflicts (metadata-re-sync conflict-hold guard)", () =// observable draft/comment OUTCOME, not call counts.const CONFLICT_HEAD =- "chore(sync): mirror inkeep/open-knowledge#310 (with conflicts; needs manual resolution)";-const CLEAN_HEAD = "chore(sync): mirror inkeep/open-knowledge#310";+ 'chore(sync): mirror inkeep/open-knowledge#310 (with conflicts; needs manual resolution)';+const CLEAN_HEAD = 'chore(sync): mirror inkeep/open-knowledge#310';// Drive the real syncPublicPr through a metadata-only ('edited') event with the// GitHub API faked. Returns the load-bearing observable mutations it made.@@ -450,19 +545,19 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {const recorded = { draftMutation: null, comment: null };const internalPr = {number: 42,- node_id: "PR_node_42",+ node_id: 'PR_node_42',draft: internalPrStartsDraft,- head: { sha: "internal-head-sha" },- html_url: "https://github.com/inkeep/agents-private/pull/42",+ head: { sha: 'internal-head-sha' },+ html_url: 'https://github.com/inkeep/agents-private/pull/42',};const publicPr = {number: 310,- title: "Fix something",- body: "body",- html_url: "https://github.com/inkeep/open-knowledge/pull/310",- user: { login: "octocat", id: 99 },- base: { ref: "main", repo: { full_name: "inkeep/open-knowledge" } },- head: { label: "octocat:branch", sha: "public-head-sha" },+ title: 'Fix something',+ body: 'body',+ html_url: 'https://github.com/inkeep/open-knowledge/pull/310',+ user: { login: 'octocat', id: 99 },+ base: { ref: 'main', repo: { full_name: 'inkeep/open-knowledge' } },+ head: { label: 'octocat:branch', sha: 'public-head-sha' },draft: false,};const json = (obj, status = 200) => ({@@ -473,46 +568,51 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {const realFetch = globalThis.fetch;globalThis.fetch = async (url, init = {}) => {- const method = init.method || "GET";- if (method === "GET" && url.includes("/repos/inkeep/open-knowledge/pulls/310")) return json(publicPr);- if (method === "GET" && url.includes("/pulls?state=open")) return json([internalPr]);- if (method === "GET" && url.includes("/commits/internal-head-sha") && !url.includes("/status")) {+ const method = init.method || 'GET';+ if (method === 'GET' && url.includes('/repos/inkeep/open-knowledge/pulls/310'))+ return json(publicPr);+ if (method === 'GET' && url.includes('/pulls?state=open')) return json([internalPr]);+ if (+ method === 'GET' &&+ url.includes('/commits/internal-head-sha') &&+ !url.includes('/status')+ ) {return json({ commit: { message: headCommitMessage } });}- if (method === "PATCH" && url.includes("/pulls/42")) return json(internalPr);- if (method === "GET" && url.includes("/orgs/")) return json({ message: "Not Found" }, 404); // non-member- if (method === "GET" && url.includes("/commits/public-head-sha/status")) {- return json({ statuses: [{ context: "license/cla", state: "success" }] }); // CLA signed -> not gated+ if (method === 'PATCH' && url.includes('/pulls/42')) return json(internalPr);+ if (method === 'GET' && url.includes('/orgs/')) return json({ message: 'Not Found' }, 404); // non-member+ if (method === 'GET' && url.includes('/commits/public-head-sha/status')) {+ return json({ statuses: [{ context: 'license/cla', state: 'success' }] }); // CLA signed -> not gated}- if (method === "POST" && url.endsWith("/graphql")) {+ if (method === 'POST' && url.endsWith('/graphql')) {const q = JSON.parse(init.body).query;- recorded.draftMutation = q.includes("convertPullRequestToDraft")- ? "to-draft"- : q.includes("markPullRequestReadyForReview")- ? "to-ready"- : "unknown";+ recorded.draftMutation = q.includes('convertPullRequestToDraft')+ ? 'to-draft'+ : q.includes('markPullRequestReadyForReview')+ ? 'to-ready'+ : 'unknown';return json({ data: {} });}- if (method === "POST" && url.includes("/statuses/")) return json({});- if (method === "GET" && url.includes("/issues/310/comments")) return json([]);- if (method === "POST" && url.includes("/issues/310/comments")) {+ if (method === 'POST' && url.includes('/statuses/')) return json({});+ if (method === 'GET' && url.includes('/issues/310/comments')) return json([]);+ if (method === 'POST' && url.includes('/issues/310/comments')) {recorded.comment = JSON.parse(init.body).body;- return json({ html_url: "https://github.com/x/comments/1" });+ return json({ html_url: 'https://github.com/x/comments/1' });}throw new Error(`unrouted request: ${method} ${url}`);};const setKeys = {- PUBLIC_TOKEN: "pub",- INTERNAL_TOKEN: "int",- PUBLIC_REPO: "inkeep/open-knowledge",- INTERNAL_REPO: "inkeep/agents-private",- INTERNAL_REPO_DIR: "/tmp/unused-on-metadata-path",- MONOREPO_PATH_PREFIX: "public/open-knowledge",- INTERNAL_BASE_REF: "main",- INTERNAL_BRANCH_PREFIX: "public-pr/open-knowledge",- PUBLIC_PR_NUMBER: "310",- PUBLIC_PR_ACTION: "edited",+ PUBLIC_TOKEN: 'pub',+ INTERNAL_TOKEN: 'int',+ PUBLIC_REPO: 'inkeep/open-knowledge',+ INTERNAL_REPO: 'inkeep/agents-private',+ INTERNAL_REPO_DIR: '/tmp/unused-on-metadata-path',+ MONOREPO_PATH_PREFIX: 'public/open-knowledge',+ INTERNAL_BASE_REF: 'main',+ INTERNAL_BRANCH_PREFIX: 'public-pr/open-knowledge',+ PUBLIC_PR_NUMBER: '310',+ PUBLIC_PR_ACTION: 'edited',};const saved = {};for (const k of Object.keys(setKeys)) saved[k] = process.env[k];@@ -529,29 +629,35 @@ async function runMetadataSync({ headCommitMessage, internalPrStartsDraft }) {return recorded;}-describe("syncPublicPr metadata-event composition (conflict-hold fail-open guard)", () => {+describe('syncPublicPr metadata-event composition (conflict-hold fail-open guard)', () => {test("a metadata event on a DRAFT conflict PR keeps it draft and posts 'conflicts' (not un-draft + 'synced')", async () => {- const r = await runMetadataSync({ headCommitMessage: CONFLICT_HEAD, internalPrStartsDraft: true });+ const r = await runMetadataSync({+ headCommitMessage: CONFLICT_HEAD,+ internalPrStartsDraft: true,+ });// Without the conflict-hold re-derivation, this fires// markPullRequestReadyForReview. Correct: the conflict hold is re-derived// true -> shouldBeDraft true -> already draft -> NO transition fires.expect(r.draftMutation).toBeNull();- expect(r.comment).toContain("No action is needed from you");- expect(r.comment.toLowerCase()).not.toContain("rebase");- expect(r.comment).not.toContain("review and merge your PR"); // not the 'synced' body+ expect(r.comment).toContain('No action is needed from you');+ expect(r.comment.toLowerCase()).not.toContain('rebase');+ expect(r.comment).not.toContain('review and merge your PR'); // not the 'synced' body});test("a metadata event on a clean PR readies it and posts 'synced'", async () => {const r = await runMetadataSync({ headCommitMessage: CLEAN_HEAD, internalPrStartsDraft: true });- expect(r.draftMutation).toBe("to-ready");- expect(r.comment).toContain("review and merge your PR"); // the 'synced' body+ expect(r.draftMutation).toBe('to-ready');+ expect(r.comment).toContain('review and merge your PR'); // the 'synced' body});- test("a metadata event on a non-draft PR whose head now carries conflicts re-drafts it", async () => {- const r = await runMetadataSync({ headCommitMessage: CONFLICT_HEAD, internalPrStartsDraft: false });+ test('a metadata event on a non-draft PR whose head now carries conflicts re-drafts it', async () => {+ const r = await runMetadataSync({+ headCommitMessage: CONFLICT_HEAD,+ internalPrStartsDraft: false,+ });// Symmetric to the draft case: the conflict hold is re-derived true ->// shouldBeDraft true -> PR is currently ready -> convertPullRequestToDraft fires.- expect(r.draftMutation).toBe("to-draft");- expect(r.comment).toContain("No action is needed from you");+ expect(r.draftMutation).toBe('to-draft');+ expect(r.comment).toContain('No action is needed from you');});});