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: dedupe merge-gate recovery by head and sweep per app

[._.]/ Adam Eivya1d6c5bbb380
diff --git a/server/lib/mergeGateAudit.js b/server/lib/mergeGateAudit.js
index 65c2bafe20..6a3b2f4e22 100644
--- a/server/lib/mergeGateAudit.js
+++ b/server/lib/mergeGateAudit.js
@@ -40,7 +40,8 @@ const isGreen = (check, mergedMs) => check.status === 'completed' && GREEN_CONCL
* re-posted in place, so the newest record can postdate the merge. Each check's
* attempts are dated by when they existed: a run's first attempt by its suite's
* creation (a run queued before the merge may not start until well after it), a
- * rerun by its start, a status by its post time. A check none of whose attempts
+ * rerun by its start (the check-runs API exposes no creation time, so a rerun
+ * queued before the merge but started after it is not seen), a status by its post time. A check none of whose attempts
* existed at `mergedAt` was never something the merge could wait for. Of the rest,
* an attempt that was green before the merge stands for the check; otherwise the
* newest attempt does, so a head still red (or fixed only after the merge) is
diff --git a/server/services/mergeGateAudit.js b/server/services/mergeGateAudit.js
index 9d103a3834..95f5278767 100644
--- a/server/services/mergeGateAudit.js
+++ b/server/services/mergeGateAudit.js
@@ -123,7 +123,8 @@ function recoveryContext(watch, failing, appName) {
async function fileRecoveryTask(watch, failing, app) {
const { addTask } = await import('./cosTaskStore.js');
const created = await addTask({
- description: `${RECOVERY_TASK_PREFIX} Default branch ${watch.baseBranch} is red after PR #${watch.prNumber} merged past the merge gate (${watch.headSha.slice(0, SHORT_SHA)}) in ${app.name || watch.repository}`,
+ // First line is the queue's dedupe identity (base branch + head + app): no PR number, so two PRs backed by one head share a recovery.
+ description: `${RECOVERY_TASK_PREFIX} Default branch ${watch.baseBranch} is red after a PR merged past the merge gate (${watch.headSha.slice(0, SHORT_SHA)}) in ${app.name || watch.repository}`,
priority: 'HIGH',
app: app.id,
isRecovery: true,
@@ -317,7 +318,8 @@ export function __cancelMergeGatePolls() {
pollers.clear();
}
-let sweepInFlight = null;
+// One sweep per scope: a poll for one app must not borrow another app's result.
+const sweepsInFlight = new Map();
// True once the audit ran to a verdict. A run whose record is gone cannot be
// audited again; the entry waits out its TTL like any other unreadable evidence.
@@ -374,9 +376,10 @@ async function sweepApp(app) {
* @returns {Promise<{ checked: number, recovered: number, remaining: number }>}
*/
export function sweepMergeGateWatches({ appId = null } = {}) {
- // A concurrent sweep joins the one in flight; recovery dedupe rides on the task queue.
- if (sweepInFlight) return sweepInFlight;
- sweepInFlight = (async () => {
+ // A concurrent sweep of the same scope joins the one in flight; recovery dedupe rides on the task queue.
+ const scope = appId || '*';
+ if (sweepsInFlight.has(scope)) return sweepsInFlight.get(scope);
+ const sweep = (async () => {
const totals = { checked: 0, recovered: 0, remaining: 0 };
const apps = appId ? [await getAppById(appId).catch(() => null)].filter(Boolean) : await getActiveApps().catch(() => []);
for (const app of apps.filter((candidate) => readWatches(candidate).length > 0)) {
@@ -389,6 +392,7 @@ export function sweepMergeGateWatches({ appId = null } = {}) {
totals.remaining += result.remaining;
}
return totals;
- })().finally(() => { sweepInFlight = null; });
- return sweepInFlight;
+ })().finally(() => { sweepsInFlight.delete(scope); });
+ sweepsInFlight.set(scope, sweep);
+ return sweep;
}
diff --git a/server/services/mergeGateAudit.test.js b/server/services/mergeGateAudit.test.js
index 4235a8b75d..05bd38d72a 100644
--- a/server/services/mergeGateAudit.test.js
+++ b/server/services/mergeGateAudit.test.js
@@ -259,6 +259,26 @@ describe('post-merge gate audit', () => {
expect(f.apps.get('app1').mergeGateWatches).toEqual([expect.objectContaining({ kind: 'audit', agentId: 'agent-1' })]);
});
+ it('queues one recovery for two merged PRs backed by the same head', async () => {
+ f.checks = [check({ conclusion: 'failure', completed_at: '2026-10-20T02:20:00Z' })];
+ await run();
+ f.prs = [{ ...f.prs[0], number: 8, url: 'https://github.com/example/repo/pull/8' }];
+ f.agent.metadata.mergeGateAudits = [];
+ await run();
+ expect(f.agent.metadata.mergeGateViolations).toHaveLength(2);
+ expect(f.tasks).toHaveLength(1);
+ });
+
+ it('sweeps each app on its own, so one app\'s in-flight sweep never answers for another', async () => {
+ f.apps.set('app2', { id: 'app2', name: 'Other App', repoPath: '/repos/other', forgeAccount: null });
+ f.apps.get('app1').mergeGateWatches = [];
+ f.apps.get('app2').mergeGateWatches = [{ key: 'github.com/example/repo#b', kind: 'audit', agentId: 'agent-1', flaggedAt: new Date().toISOString() }];
+ f.statuses = null; // app2's audit retry cannot read the forge, so it stays watched
+ const [first, second] = [sweepMergeGateWatches({ appId: 'app1' }), sweepMergeGateWatches({ appId: 'app2' })];
+ expect(await first).toMatchObject({ checked: 0, remaining: 0 });
+ expect(await second).toMatchObject({ checked: 1, remaining: 1 });
+ });
+
it('keeps an audit the forge could not answer and completes it from the durable sweep', async () => {
f.statuses = null;
expect(await run()).toMatchObject({ audited: 0, violations: 0, unresolved: true });