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: recheck the queue for a receipt that lands mid-sweep (#12247)

[._.]/ Adam Eivy04506a6fa46d
diff --git a/docs/features/chief-of-staff.md b/docs/features/chief-of-staff.md
index f068eb6a8a..f9f4f4403b 100644
--- a/docs/features/chief-of-staff.md
+++ b/docs/features/chief-of-staff.md
@@ -124,7 +124,9 @@ the published local head, so the merge-gate check finalizes without a nudge
audit treats the queued entry as the branch's owner. The receipt starts one
immediate sweep of the app, which merges a head whose CI already settled
without waiting for the 30-minute timer and spends no tick budget. Overlapping
-sweeps of one app (timer, task generator, receipt) join a single pass.
+sweeps of one app (timer, task generator, receipt) join a single pass; a receipt
+that lands during a pass which already read the queue gets one follow-up pass
+after it settles.
Leases extend the existing machine-local CoS runtime ownership in
`data/cos/state.json`, under its existing write queue; there is no new store,
diff --git a/server/services/cosMergeHandoff.test.js b/server/services/cosMergeHandoff.test.js
index f55910fb5e..8bc4fc152c 100644
--- a/server/services/cosMergeHandoff.test.js
+++ b/server/services/cosMergeHandoff.test.js
@@ -165,6 +165,28 @@ describe('pending-merge handoff', () => {
expect(mergePrMock).toHaveBeenCalledWith('/repos/source', 7, { forgeAccount: null, expectedHeadSha: HEAD });
});
+ it('rechecks the queue after an overlapping sweep that read it before the receipt', async () => {
+ greenChecks();
+ let releaseRead;
+ const blocked = new Promise((resolve) => { releaseRead = resolve; });
+ const other = { ...f.pr, number: 8, headRefName: 'cos/other', headRefOid: 'b'.repeat(40), statusCheckRollup: [{ status: 'IN_PROGRESS' }] };
+ const forge = execGhMock.getMockImplementation();
+ execGhMock.mockImplementation(async (args) => (args[0] === 'pr' && args[1] === 'view' && args[2] === '8'
+ ? blocked.then(() => JSON.stringify(other)) : forge(args)));
+ f.apps.set('app1', { ...f.apps.get('app1'), pendingMergePrs: [{
+ prUrl: 'https://github.com/example/repo/pull/8', prNumber: 8, prBranch: 'cos/other', ticks: 0,
+ sourceTask: { id: 'task-other', priority: 'MEDIUM', description: 'Other change', metadata: { app: 'app1' } },
+ }] });
+
+ // The live module instance, the one the handoff's receipt sweep joins.
+ const timer = (await import('./prWatcher.js')).processPendingMergePrs(f.apps.get('app1'));
+ expect(await handoff()).toMatchObject({ queued: true });
+ releaseRead();
+ await timer;
+ await vi.waitFor(() => expect(mergePrMock).toHaveBeenCalledWith('/repos/source', 7, { forgeAccount: null, expectedHeadSha: HEAD }));
+ expect(pending().map((entry) => entry.prNumber)).toEqual([8]);
+ });
+
it('reports a queued receipt only for the exact handed-off PR, branch and head', async () => {
await handedOff();
const target = { prUrl: 'https://GitHub.com/Example/Repo/pull/7', prBranch: 'claim/issue-1', headSha: HEAD.toUpperCase() };
diff --git a/server/services/prWatcher.js b/server/services/prWatcher.js
index 8bd15835f2..a81291c906 100644
--- a/server/services/prWatcher.js
+++ b/server/services/prWatcher.js
@@ -51,6 +51,7 @@ const GREEN_CHECK_VERDICTS = new Set(['SUCCESS', 'NEUTRAL', 'SKIPPED']);
// otherwise still load stale whole-file snapshots and clobber one another.
let pendingMergeWriteTail = Promise.resolve();
const pendingMergeSweeps = new Map();
+const pendingMergeResweeps = new Map();
// Cache the gh-authenticated login PER HOST, for the process lifetime. Host-keyed
// for two reasons: (1) `gh api` does NOT infer the host from the working
@@ -585,6 +586,20 @@ export async function sweepPendingMergePrs() {
* never throws: the timer sweep remains the owner of every queued entry.
*/
export function sweepPendingMergeApp(appId) {
+ const inFlight = pendingMergeSweeps.get(appId);
+ if (!inFlight) return receiptSweep(appId);
+ // That pass read the queue before this receipt was written: reload it once the
+ // pass settles. Receipts arriving meanwhile share the one follow-up pass.
+ if (!pendingMergeResweeps.has(appId)) {
+ pendingMergeResweeps.set(appId, inFlight.catch(() => null).then(() => {
+ pendingMergeResweeps.delete(appId);
+ return receiptSweep(appId);
+ }));
+ }
+ return pendingMergeResweeps.get(appId);
+}
+
+function receiptSweep(appId) {
return joinPendingMergeSweep(appId, async () => {
// A timer sweep may join this pass, so it answers in the same result shape.
const app = await getAppById(appId);