Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: close the remaining ambient-gh gaps in the pr-watcher and issue-reconcile paths (#7540)

[._.]/ Adam Eivy565fd596fde0
diff --git a/server/lib/forgeAccessErrors.js b/server/lib/forgeAccessErrors.js
index 4c16081aa2..e2e6868fc1 100644
--- a/server/lib/forgeAccessErrors.js
+++ b/server/lib/forgeAccessErrors.js
@@ -17,12 +17,16 @@
// `gh pr list` / `gh repo view` answer a no-access repo through GraphQL, which
// reports it as an unresolvable node rather than an HTTP status; `gh api` and
-// the REST-backed subcommands answer with the status line instead. Match both,
-// plus gh's bare `Not Found` phrasing for a resource the token cannot read.
+// the REST-backed subcommands answer with the status line instead (gh's own
+// "Not Found" phrasing carries it — `gh: Not Found (HTTP 404)`), so these two
+// patterns cover both transports.
+//
+// Deliberately narrow. Widening the GraphQL arm to other node types would
+// classify a deleted PR or a renamed user — an ordinary missing SUB-resource —
+// as a credential problem and print a remedy that does not apply.
const NO_ACCESS_PATTERNS = [
- /could not resolve to a (?:repository|pullrequest|issue|user|organization)/i,
+ /could not resolve to a repository/i,
/\bHTTP 404\b/i,
- /^gh: not found\b/im,
];
/**
diff --git a/server/services/cosTaskPreStepBlocks.js b/server/services/cosTaskPreStepBlocks.js
index c3eae2abb3..ca76ec689d 100644
--- a/server/services/cosTaskPreStepBlocks.js
+++ b/server/services/cosTaskPreStepBlocks.js
@@ -653,9 +653,15 @@ export async function resolveIssueReconcileBlock(app, taskType, metadata, taskSc
if (result.forge === 'github') {
const { screenForgeMaintenance } = await import('./forgeMaintenanceEvidence.js');
const { execGh } = await import('./github.js');
+ const { resolveForgeExecOptions } = await import('./forgeExecOptions.js');
+ // The screening `gh api` reads hit the SAME private repo the poll just read,
+ // so they need the same per-repo credential — ambient gh 404s on a repo owned
+ // by another account and the whole dispatch is held every tick (#7540).
+ const { cwd, env } = await resolveForgeExecOptions(app.repoPath, { forgeAccount: app.forgeAccount });
+ const runGh = (args, timeoutMs) => execGh(args, timeoutMs, { cwd, env });
const screened = await screenForgeMaintenance({
records: result.zombies, kind: 'issue', host: result.repoSpec.split('/')[0],
- repoFullName: result.fullName, runGh: execGh,
+ repoFullName: result.fullName, runGh,
});
if (!screened.ok) {
emitLog('warn', `issue-reconcile held: ${screened.code}`, { appId: app.id });
@@ -756,14 +762,20 @@ export async function resolvePrWatcherBlock(app, taskType, metadata, taskSchedul
let screeningError = null;
if (!check.firstRun && check.newPrs.length) {
- const { screenForgeMaintenance } = await import('./forgeMaintenanceEvidence.js');
- const { execGh } = await import('./github.js');
const { getOriginInfo } = await import('../lib/gitRemote.js');
const { githubApiHost } = await import('../lib/workTracker.js');
const origin = await getOriginInfo(app.repoPath);
+ const { screenForgeMaintenance } = await import('./forgeMaintenanceEvidence.js');
+ const { execGh } = await import('./github.js');
+ const { resolveForgeExecOptions } = await import('./forgeExecOptions.js');
+ // The screening `gh api` reads hit the SAME private repo the poll just read,
+ // so they need the same per-repo credential — ambient gh 404s on a repo owned
+ // by another account and the whole dispatch is held every tick (#7540).
+ const { cwd, env } = await resolveForgeExecOptions(app.repoPath, { forgeAccount: app.forgeAccount });
+ const runGh = (args, timeoutMs) => execGh(args, timeoutMs, { cwd, env });
const screened = await screenForgeMaintenance({
records: check.newPrs, kind: 'pr', host: githubApiHost(origin.host),
- repoFullName: check.repoFullName, runGh: execGh,
+ repoFullName: check.repoFullName, runGh,
});
if (!screened.ok) {
await prWatcher.persistPrWatcherState(app.id, { lastCheckedAt: checkedAt, lastError: screened.code });
diff --git a/server/services/git.js b/server/services/git.js
index 726e797bfc..be482272e8 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -427,8 +427,10 @@ export async function createPR(dir, { title, body, base, head }) {
* @param {number|string} prNumber - Pull request number
* @returns {Promise<{success: boolean, error?: string, cli?: string, account?: string|null, owner?: string|null, host?: string|null}>}
*/
-export async function mergePR(dir, prNumber) {
- const { cli, env, host, owner, account } = await resolveForgeForRepo(dir);
+export async function mergePR(dir, prNumber, { forgeAccount = null } = {}) {
+ // The pin matters here as much as on the read side: an owner-match alone picks
+ // the wrong account for an org repo, and the merge fails authorization (#7540).
+ const { cli, env, host, owner, account } = await resolveForgeForRepo(dir, { forgeAccount });
const meta = { cli, account, owner, host };
if (cli !== 'gh') {
return { success: false, error: `Merge-only PR automation requires gh, not ${cli}`, ...meta };
diff --git a/server/services/prWatcher.js b/server/services/prWatcher.js
index 0305d9e91a..d718b3612f 100644
--- a/server/services/prWatcher.js
+++ b/server/services/prWatcher.js
@@ -404,7 +404,8 @@ export async function processPendingMergePrs(app) {
}
if (isPendingMergeReady(prView)) {
- const merge = await git.mergePR(app.repoPath, entry.prNumber).catch((err) => ({ success: false, error: err.message }));
+ const merge = await git.mergePR(app.repoPath, entry.prNumber, { forgeAccount: app?.forgeAccount || null })
+ .catch((err) => ({ success: false, error: err.message }));
if (merge.success) {
outcomes.set(key, null);
result.merged += 1;
diff --git a/server/services/prWatcher.test.js b/server/services/prWatcher.test.js
index c47c8933d3..abbbfb650c 100644
--- a/server/services/prWatcher.test.js
+++ b/server/services/prWatcher.test.js
@@ -182,6 +182,9 @@ const pendingMerge = (overrides = {}) => ({
const pendingApp = (entries = [pendingMerge()]) => ({
id: 'app1',
repoPath: '/repos/app1',
+ // Pinned so the merge path proves it carries the account down (#7540) — an
+ // owner-match alone picks the wrong account for an org repo.
+ forgeAccount: 'other-account',
pendingMergePrs: entries
});
@@ -223,7 +226,7 @@ describe('merge-only PR watcher', () => {
const result = await processPendingMergePrs(app);
expect(result).toMatchObject({ ok: true, checked: 1, merged: 1, escalated: 0 });
- expect(mergePrMock).toHaveBeenCalledWith('/repos/app1', 88);
+ expect(mergePrMock).toHaveBeenCalledWith('/repos/app1', 88, { forgeAccount: 'other-account' });
expect(spawnReviewLoopFollowUpMock).not.toHaveBeenCalled();
expect(readPendingMergePrs(mockApps.get('app1'))).toEqual([]);
expect(execGhMock).toHaveBeenCalledWith([