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: guard GitHub backoff outcome settlement

[._.]/ Adam Eivyc92b89bc20ed
diff --git a/server/services/blockedIssueReconcile.js b/server/services/blockedIssueReconcile.js
index a1a47ae367..e23f3aa389 100644
--- a/server/services/blockedIssueReconcile.js
+++ b/server/services/blockedIssueReconcile.js
@@ -78,7 +78,7 @@ async function getGithubBlockedState(repoSpec, apiHost, fullName) {
const forge = await ensureForgeReachable('blocked-issue-reconcile', { hostname: apiHost });
if (!forge.ok) return null;
- const ghList = (args, what) => execGh(args).catch((err) => {
+ const ghList = (args, what) => execGh(args, undefined, { backoffKey: repoSpec }).catch((err) => {
console.error(`❌ blocked-issue-reconcile: ${what} failed for ${repoSpec}: ${err.message}`);
return null;
});
diff --git a/server/services/blockedIssueReconcile.test.js b/server/services/blockedIssueReconcile.test.js
index 17b3b318be..d784dcb896 100644
--- a/server/services/blockedIssueReconcile.test.js
+++ b/server/services/blockedIssueReconcile.test.js
@@ -120,6 +120,10 @@ describe('gatherBlockedIssueState (GitHub)', () => {
const result = await gatherBlockedIssueState('/repo');
expect(result.forge).toBe('github');
expect(result.ready).toEqual([]);
+ expect(execGhMock.mock.calls.slice(0, 2).map(([, , options]) => options)).toEqual([
+ { backoffKey: 'github.com/atomantic/PortOS' },
+ { backoffKey: 'github.com/atomantic/PortOS' },
+ ]);
});
it('resolves a blocked issue whose named blocker is now closed', async () => {
diff --git a/server/services/github.js b/server/services/github.js
index 4d61256007..bc375a268f 100644
--- a/server/services/github.js
+++ b/server/services/github.js
@@ -136,7 +136,12 @@ export function execGh(args, timeoutMs = DEFAULT_EXEC_GH_TIMEOUT_MS, { cwd = nul
let stdout = '';
let stderr = '';
let timedOut = false;
- const settle = (ok) => { if (backoffKey !== null) recordGhCallOutcome(backoffKey, ok); };
+ let settled = false;
+ const settle = (ok) => {
+ if (settled) return;
+ settled = true;
+ if (backoffKey !== null) recordGhCallOutcome(backoffKey, ok);
+ };
const timer = setTimeout(() => {
timedOut = true;
// setTimeout callback boundary — guard so a kill() throw can't crash
diff --git a/server/services/github.test.js b/server/services/github.test.js
index b6aa1d0f9c..8911590652 100644
--- a/server/services/github.test.js
+++ b/server/services/github.test.js
@@ -190,6 +190,20 @@ describe('execGh backoffKey', () => {
expect(spawn).toHaveBeenCalledTimes(1); // second call never spawned a child
});
+ it('records a child error only once when close follows it', async () => {
+ const failing = makeChild();
+ spawn.mockReturnValueOnce(failing);
+ const first = execGh(['pr', 'list'], 5000, { backoffKey: 'github.com/o/r' });
+ failing.emit('error', new Error('spawn gh ENOENT'));
+ await expect(first).rejects.toThrow(/ENOENT/);
+
+ // ChildProcess emits close after error; it must not settle this invocation again.
+ failing.emit('close', 0);
+ await expect(execGh(['pr', 'list'], 5000, { backoffKey: 'github.com/o/r' }))
+ .rejects.toThrow(/backing off/);
+ expect(spawn).toHaveBeenCalledTimes(1);
+ });
+
it('spawns again once the backoff window elapses', async () => {
const failing = makeChild();
spawn.mockReturnValueOnce(failing);
diff --git a/server/services/issueReconcile.js b/server/services/issueReconcile.js
index 0519971873..0322ea5dd3 100644
--- a/server/services/issueReconcile.js
+++ b/server/services/issueReconcile.js
@@ -323,7 +323,7 @@ async function getGithubState(repoSpec, fullName, apiHost = null) {
const forge = await ensureForgeReachable('issue-reconcile', { hostname: apiHost });
if (!forge.ok) return null;
- const ghList = (args, what) => execGh(args).catch((err) => {
+ const ghList = (args, what) => execGh(args, undefined, { backoffKey: repoSpec }).catch((err) => {
console.error(`❌ issue-reconcile: ${what} failed for ${fullName}: ${err.message}`);
return null;
});
diff --git a/server/services/issueReconcile.test.js b/server/services/issueReconcile.test.js
index 2246f75dde..e30bc86969 100644
--- a/server/services/issueReconcile.test.js
+++ b/server/services/issueReconcile.test.js
@@ -367,6 +367,19 @@ describe('reconcile', () => {
.toEqual(expect.arrayContaining(['--hostname', 'github.acme.example']));
});
+ it('keys every periodic GitHub read by its repository selector', async () => {
+ mockGh({ issues: [], merged: [], open: [] });
+
+ const result = await gatherIssueState('/repo');
+
+ expect(result).not.toBeNull();
+ const periodicReads = execGh.mock.calls.filter(([argv]) => argv[0] === 'issue' || argv[0] === 'pr');
+ expect(periodicReads).toHaveLength(3);
+ for (const [, , options] of periodicReads) {
+ expect(options).toEqual({ backoffKey: 'github.com/atomantic/PortOS' });
+ }
+ });
+
it('scans a custom-hostname self-hosted forge when the app pins workTracker (issue #3767)', async () => {
// Neither `github.*` nor `gitlab.*` — no hostname heuristic can classify a
// self-hosted forge, so the app's own pin is the only signal. Without the
diff --git a/server/services/prWatcher.js b/server/services/prWatcher.js
index 87678c1b83..03d95077e9 100644
--- a/server/services/prWatcher.js
+++ b/server/services/prWatcher.js
@@ -68,7 +68,11 @@ export async function getSelfLogin(host) {
if (_selfLoginCache.has(host)) return _selfLoginCache.get(host);
// `--hostname` is required: without it `gh api` targets github.com regardless
// of cwd, resolving the wrong identity for an enterprise repo.
- const login = await execGh(['api', 'user', '--hostname', host, '--jq', '.login']).catch((err) => {
+ const login = await execGh(
+ ['api', 'user', '--hostname', host, '--jq', '.login'],
+ undefined,
+ { backoffKey: host }
+ ).catch((err) => {
// Log rather than swallow (#3358): without this a firewalled/unauthenticated
// gh is indistinguishable from "this host has no login", and every self/others
// gate silently stops firing with nothing in the log to explain it.
@@ -97,7 +101,11 @@ export function __resetSelfLoginCache() {
* cwd-based auto-detection would resolve ambiguously. Returns null on failure.
*/
async function getDefaultBranch(repoSpec) {
- const name = await execGh(['repo', 'view', repoSpec, '--json', 'defaultBranchRef', '-q', '.defaultBranchRef.name'])
+ const name = await execGh(
+ ['repo', 'view', repoSpec, '--json', 'defaultBranchRef', '-q', '.defaultBranchRef.name'],
+ undefined,
+ { backoffKey: repoSpec }
+ )
.catch((err) => {
console.error(`❌ pr-watcher: could not resolve the default branch for ${repoSpec}: ${err.message}`);
return null;
@@ -117,7 +125,7 @@ async function listOpenPullRequests(repoSpec, baseBranch) {
'--base', baseBranch, '--state', 'open',
'--limit', String(PR_LIST_LIMIT),
'--json', 'number,title,author,url,createdAt,updatedAt,isDraft,headRefName,headRefOid,mergeStateStatus,statusCheckRollup'
- ]).catch((err) => {
+ ], undefined, { backoffKey: repoSpec }).catch((err) => {
console.error(`❌ pr-watcher: gh pr list failed for ${repoSpec}: ${err.message}`);
return null;
});
@@ -281,7 +289,7 @@ async function readPendingPullRequest(repoSpec, prNumber) {
const raw = await execGh([
'pr', 'view', String(prNumber), '--repo', repoSpec,
'--json', 'state,mergeStateStatus,statusCheckRollup'
- ]).catch(() => null);
+ ], undefined, { backoffKey: repoSpec }).catch(() => null);
const parsed = raw === null ? null : safeJSONParse(raw, null);
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
}
diff --git a/server/services/prWatcher.test.js b/server/services/prWatcher.test.js
index 85f3a6f06d..bacd967714 100644
--- a/server/services/prWatcher.test.js
+++ b/server/services/prWatcher.test.js
@@ -216,7 +216,7 @@ describe('merge-only PR watcher', () => {
expect(execGhMock).toHaveBeenCalledWith([
'pr', 'view', '88', '--repo', 'github.com/o/r',
'--json', 'state,mergeStateStatus,statusCheckRollup'
- ]);
+ ], undefined, { backoffKey: 'github.com/o/r' });
});
it.each([
@@ -369,7 +369,11 @@ describe('getSelfLogin', () => {
expect(execGhMock).toHaveBeenCalledTimes(1);
// --hostname is required: without it `gh api` hits github.com regardless of
// cwd and would resolve the wrong identity on an enterprise repo.
- expect(execGhMock).toHaveBeenCalledWith(['api', 'user', '--hostname', 'github.com', '--jq', '.login']);
+ expect(execGhMock).toHaveBeenCalledWith(
+ ['api', 'user', '--hostname', 'github.com', '--jq', '.login'],
+ undefined,
+ { backoffKey: 'github.com' }
+ );
});
it('caches each host independently — a different login per host', async () => {
@@ -451,6 +455,9 @@ describe('checkPullRequests trusted maintenance boundary', () => {
expect((await checkPullRequests(app)).newPrs).toHaveLength(1);
expect(execGhMock.mock.calls.filter(([a]) => a[0] === 'api').every(([a]) => a.includes('github.enterprise.example'))).toBe(true);
expect(execGhMock.mock.calls.filter(([a]) => a.includes('--repo')).every(([a]) => a.includes('github.enterprise.example/example/project'))).toBe(true);
+ expect(execGhMock.mock.calls
+ .filter(([a]) => a[0] === 'repo' || (a[0] === 'pr' && a[1] === 'list'))
+ .every(([, , options]) => options?.backoffKey === 'github.enterprise.example/example/project')).toBe(true);
forge([rawPr(10, 'collaborator')], { permission: 'read' });
expect((await checkPullRequests(app)).newPrs).toEqual([]);
execGhMock.mockImplementation(async args => args[0] === 'repo' ? 'main' : args[0] === 'pr' ? JSON.stringify([rawPr(10, 'collaborator')]) : Promise.reject(new Error('unavailable')));